]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pimd.c
debianpkg: Support --with-pkg-extra-version and other modifier to build custom packag...
[mirror_frr.git] / pimd / pimd.c
1 /*
2 PIM for Quagga
3 Copyright (C) 2008 Everton da Silva Marques
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "log.h"
24 #include "memory.h"
25 #include "if.h"
26 #include "prefix.h"
27 #include "vty.h"
28 #include "plist.h"
29 #include "hash.h"
30 #include "jhash.h"
31
32 #include "pimd.h"
33 #include "pim_cmd.h"
34 #include "pim_iface.h"
35 #include "pim_zebra.h"
36 #include "pim_str.h"
37 #include "pim_oil.h"
38 #include "pim_pim.h"
39 #include "pim_upstream.h"
40 #include "pim_rpf.h"
41 #include "pim_ssmpingd.h"
42 #include "pim_static.h"
43 #include "pim_rp.h"
44 #include "pim_ssm.h"
45 #include "pim_zlookup.h"
46 #include "pim_nht.h"
47
48 const char *const PIM_ALL_SYSTEMS = MCAST_ALL_SYSTEMS;
49 const char *const PIM_ALL_ROUTERS = MCAST_ALL_ROUTERS;
50 const char *const PIM_ALL_PIM_ROUTERS = MCAST_ALL_PIM_ROUTERS;
51 const char *const PIM_ALL_IGMP_ROUTERS = MCAST_ALL_IGMP_ROUTERS;
52
53 struct thread_master *master = NULL;
54 uint32_t qpim_debugs = 0;
55 int qpim_mroute_socket_fd = -1;
56 int64_t qpim_mroute_socket_creation = 0; /* timestamp of creation */
57 int qpim_t_periodic =
58 PIM_DEFAULT_T_PERIODIC; /* Period between Join/Prune Messages */
59 struct pim_assert_metric qpim_infinite_assert_metric;
60 long qpim_rpf_cache_refresh_delay_msec = 50;
61 struct thread *qpim_rpf_cache_refresher = NULL;
62 int64_t qpim_rpf_cache_refresh_requests = 0;
63 int64_t qpim_rpf_cache_refresh_events = 0;
64 int64_t qpim_rpf_cache_refresh_last = 0;
65 struct in_addr qpim_inaddr_any;
66 struct list *qpim_ssmpingd_list = NULL;
67 struct in_addr qpim_ssmpingd_group_addr;
68 int64_t qpim_scan_oil_events = 0;
69 int64_t qpim_scan_oil_last = 0;
70 int64_t qpim_mroute_add_events = 0;
71 int64_t qpim_mroute_add_last = 0;
72 int64_t qpim_mroute_del_events = 0;
73 int64_t qpim_mroute_del_last = 0;
74 struct list *qpim_static_route_list = NULL;
75 unsigned int qpim_keep_alive_time = PIM_KEEPALIVE_PERIOD;
76 signed int qpim_rp_keep_alive_time = 0;
77 int64_t qpim_nexthop_lookups = 0;
78 int qpim_packet_process = PIM_DEFAULT_PACKET_PROCESS;
79 uint8_t qpim_ecmp_enable = 0;
80 uint8_t qpim_ecmp_rebalance_enable = 0;
81 struct pim_instance *pimg = NULL;
82
83 int32_t qpim_register_suppress_time = PIM_REGISTER_SUPPRESSION_TIME_DEFAULT;
84 int32_t qpim_register_probe_time = PIM_REGISTER_PROBE_TIME_DEFAULT;
85
86 static struct pim_instance *pim_instance_init(vrf_id_t vrf_id, afi_t afi);
87 static void pim_instance_terminate(void);
88
89 static int pim_vrf_new(struct vrf *vrf)
90 {
91 zlog_debug("VRF Created: %s(%d)", vrf->name, vrf->vrf_id);
92 return 0;
93 }
94
95 static int pim_vrf_delete(struct vrf *vrf)
96 {
97 zlog_debug("VRF Deletion: %s(%d)", vrf->name, vrf->vrf_id);
98 return 0;
99 }
100
101 static int pim_vrf_enable(struct vrf *vrf)
102 {
103
104 if (!vrf) // unexpected
105 return -1;
106
107 if (vrf->vrf_id == VRF_DEFAULT) {
108 pimg = pim_instance_init(VRF_DEFAULT, AFI_IP);
109 if (pimg == NULL) {
110 zlog_err("%s %s: pim class init failure ", __FILE__,
111 __PRETTY_FUNCTION__);
112 /*
113 * We will crash and burn otherwise
114 */
115 exit(1);
116 }
117 }
118 return 0;
119 }
120
121 static int pim_vrf_disable(struct vrf *vrf)
122 {
123 if (vrf->vrf_id == VRF_DEFAULT)
124 return 0;
125
126 if (vrf->vrf_id == VRF_DEFAULT)
127 pim_instance_terminate();
128
129 /* Note: This is a callback, the VRF will be deleted by the caller. */
130 return 0;
131 }
132
133 void pim_vrf_init(void)
134 {
135 vrf_add_hook(VRF_NEW_HOOK, pim_vrf_new);
136 vrf_add_hook(VRF_ENABLE_HOOK, pim_vrf_enable);
137 vrf_add_hook(VRF_DISABLE_HOOK, pim_vrf_disable);
138 vrf_add_hook(VRF_DELETE_HOOK, pim_vrf_delete);
139
140 vrf_init();
141 }
142
143 static void pim_vrf_terminate(void)
144 {
145 vrf_add_hook(VRF_NEW_HOOK, NULL);
146 vrf_add_hook(VRF_ENABLE_HOOK, NULL);
147 vrf_add_hook(VRF_DISABLE_HOOK, NULL);
148 vrf_add_hook(VRF_DELETE_HOOK, NULL);
149
150 vrf_terminate();
151 }
152
153 /* Key generate for pim->rpf_hash */
154 static unsigned int pim_rpf_hash_key(void *arg)
155 {
156 struct pim_nexthop_cache *r = (struct pim_nexthop_cache *)arg;
157
158 return jhash_1word(r->rpf.rpf_addr.u.prefix4.s_addr, 0);
159 }
160
161 /* Compare pim->rpf_hash node data */
162 static int pim_rpf_equal(const void *arg1, const void *arg2)
163 {
164 const struct pim_nexthop_cache *r1 =
165 (const struct pim_nexthop_cache *)arg1;
166 const struct pim_nexthop_cache *r2 =
167 (const struct pim_nexthop_cache *)arg2;
168
169 return prefix_same(&r1->rpf.rpf_addr, &r2->rpf.rpf_addr);
170 }
171
172 /* Cleanup pim->rpf_hash each node data */
173 static void pim_rp_list_hash_clean(void *data)
174 {
175 struct pim_nexthop_cache *pnc;
176
177 pnc = (struct pim_nexthop_cache *)data;
178 if (pnc->rp_list->count)
179 list_delete_all_node(pnc->rp_list);
180 if (pnc->upstream_list->count)
181 list_delete_all_node(pnc->upstream_list);
182 }
183
184 void pim_prefix_list_update(struct prefix_list *plist)
185 {
186 pim_rp_prefix_list_update(plist);
187 pim_ssm_prefix_list_update(plist);
188 pim_upstream_spt_prefix_list_update(plist);
189 }
190
191 static void pim_instance_terminate(void)
192 {
193 /* Traverse and cleanup rpf_hash */
194 if (pimg->rpf_hash) {
195 hash_clean(pimg->rpf_hash, (void *)pim_rp_list_hash_clean);
196 hash_free(pimg->rpf_hash);
197 pimg->rpf_hash = NULL;
198 }
199
200 if (pimg->ssm_info) {
201 pim_ssm_terminate(pimg->ssm_info);
202 pimg->ssm_info = NULL;
203 }
204
205 XFREE(MTYPE_PIM_PIM_INSTANCE, pimg);
206 }
207
208 static void pim_free()
209 {
210 pim_ssmpingd_destroy();
211
212 pim_oil_terminate();
213
214 pim_upstream_terminate();
215
216 if (qpim_static_route_list)
217 list_free(qpim_static_route_list);
218
219 pim_if_terminate();
220 pim_rp_free();
221
222 pim_route_map_terminate();
223
224 zclient_lookup_free();
225
226 zprivs_terminate(&pimd_privs);
227 }
228
229 static struct pim_instance *pim_instance_init(vrf_id_t vrf_id, afi_t afi)
230 {
231 struct pim_instance *pim;
232
233 pim = XCALLOC(MTYPE_PIM_PIM_INSTANCE, sizeof(struct pim_instance));
234 if (!pim)
235 return NULL;
236
237 pim->vrf_id = vrf_id;
238 pim->afi = afi;
239
240 pim->spt.switchover = PIM_SPT_IMMEDIATE;
241 pim->spt.plist = NULL;
242
243 pim->rpf_hash = hash_create_size(256, pim_rpf_hash_key, pim_rpf_equal);
244
245 if (PIM_DEBUG_ZEBRA)
246 zlog_debug("%s: NHT rpf hash init ", __PRETTY_FUNCTION__);
247
248 pim->ssm_info = pim_ssm_init(vrf_id);
249 if (!pim->ssm_info) {
250 pim_instance_terminate();
251 return NULL;
252 }
253
254 return pim;
255 }
256
257 void pim_init()
258 {
259 qpim_rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;
260
261 pim_rp_init();
262
263 if (!inet_aton(PIM_ALL_PIM_ROUTERS, &qpim_all_pim_routers_addr)) {
264 zlog_err(
265 "%s %s: could not solve %s to group address: errno=%d: %s",
266 __FILE__, __PRETTY_FUNCTION__, PIM_ALL_PIM_ROUTERS,
267 errno, safe_strerror(errno));
268 zassert(0);
269 return;
270 }
271
272 pim_oil_init();
273
274 pim_upstream_init();
275
276 qpim_static_route_list = list_new();
277 if (!qpim_static_route_list) {
278 zlog_err("%s %s: failure: static_route_list=list_new()",
279 __FILE__, __PRETTY_FUNCTION__);
280 return;
281 }
282 qpim_static_route_list->del = (void (*)(void *))pim_static_route_free;
283
284 pim_mroute_socket_enable();
285
286 qpim_inaddr_any.s_addr = PIM_NET_INADDR_ANY;
287
288 /*
289 RFC 4601: 4.6.3. Assert Metrics
290
291 assert_metric
292 infinite_assert_metric() {
293 return {1,infinity,infinity,0}
294 }
295 */
296 qpim_infinite_assert_metric.rpt_bit_flag = 1;
297 qpim_infinite_assert_metric.metric_preference =
298 PIM_ASSERT_METRIC_PREFERENCE_MAX;
299 qpim_infinite_assert_metric.route_metric = PIM_ASSERT_ROUTE_METRIC_MAX;
300 qpim_infinite_assert_metric.ip_address = qpim_inaddr_any;
301
302 pim_if_init();
303 pim_cmd_init();
304 pim_ssmpingd_init();
305 }
306
307 void pim_terminate()
308 {
309 pim_free();
310
311 /* reverse prefix_list_init */
312 prefix_list_add_hook(NULL);
313 prefix_list_delete_hook(NULL);
314 prefix_list_reset();
315
316 pim_vrf_terminate();
317 }