]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pimd.c
*: update hash_create(), hash_create_size()
[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 along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include "log.h"
23 #include "memory.h"
24 #include "if.h"
25 #include "prefix.h"
26 #include "vty.h"
27 #include "plist.h"
28 #include "hash.h"
29 #include "jhash.h"
30 #include "vrf.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 = PIM_DEFAULT_T_PERIODIC; /* Period between Join/Prune Messages */
58 struct pim_assert_metric qpim_infinite_assert_metric;
59 long qpim_rpf_cache_refresh_delay_msec = 50;
60 struct thread *qpim_rpf_cache_refresher = NULL;
61 int64_t qpim_rpf_cache_refresh_requests = 0;
62 int64_t qpim_rpf_cache_refresh_events = 0;
63 int64_t qpim_rpf_cache_refresh_last = 0;
64 struct list *qpim_ssmpingd_list = NULL;
65 struct in_addr qpim_ssmpingd_group_addr;
66 int64_t qpim_scan_oil_events = 0;
67 int64_t qpim_scan_oil_last = 0;
68 int64_t qpim_mroute_add_events = 0;
69 int64_t qpim_mroute_add_last = 0;
70 int64_t qpim_mroute_del_events = 0;
71 int64_t qpim_mroute_del_last = 0;
72 struct list *qpim_static_route_list = NULL;
73 unsigned int qpim_keep_alive_time = PIM_KEEPALIVE_PERIOD;
74 signed int qpim_rp_keep_alive_time = 0;
75 int64_t qpim_nexthop_lookups = 0;
76 int qpim_packet_process = PIM_DEFAULT_PACKET_PROCESS;
77 uint8_t qpim_ecmp_enable = 0;
78 uint8_t qpim_ecmp_rebalance_enable = 0;
79 struct pim_instance *pimg = NULL;
80
81 int32_t qpim_register_suppress_time = PIM_REGISTER_SUPPRESSION_TIME_DEFAULT;
82 int32_t qpim_register_probe_time = PIM_REGISTER_PROBE_TIME_DEFAULT;
83
84 static struct pim_instance *pim_instance_init (vrf_id_t vrf_id, afi_t afi);
85 static void pim_instance_terminate (void);
86
87 static int
88 pim_vrf_new (struct vrf *vrf)
89 {
90 zlog_debug ("VRF Created: %s(%d)", vrf->name, vrf->vrf_id);
91 return 0;
92 }
93
94 static int
95 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
102 pim_vrf_enable (struct vrf *vrf)
103 {
104
105 if (!vrf) // unexpected
106 return -1;
107
108 if (vrf->vrf_id == VRF_DEFAULT)
109 {
110 pimg = pim_instance_init (VRF_DEFAULT, AFI_IP);
111 if (pimg == NULL)
112 {
113 zlog_err ("%s %s: pim class init failure ", __FILE__,
114 __PRETTY_FUNCTION__);
115 /*
116 * We will crash and burn otherwise
117 */
118 exit(1);
119 }
120
121 pimg->send_v6_secondary = 1;
122
123 }
124 return 0;
125 }
126
127 static int
128 pim_vrf_disable (struct vrf *vrf)
129 {
130 if (vrf->vrf_id == VRF_DEFAULT)
131 return 0;
132
133 if (vrf->vrf_id == VRF_DEFAULT)
134 pim_instance_terminate ();
135
136 /* Note: This is a callback, the VRF will be deleted by the caller. */
137 return 0;
138 }
139
140 void
141 pim_vrf_init (void)
142 {
143 vrf_init (pim_vrf_new,
144 pim_vrf_enable,
145 pim_vrf_disable,
146 pim_vrf_delete);
147 }
148
149 static void
150 pim_vrf_terminate (void)
151 {
152 vrf_terminate ();
153 }
154
155 /* Key generate for pim->rpf_hash */
156 static unsigned int
157 pim_rpf_hash_key (void *arg)
158 {
159 struct pim_nexthop_cache *r = (struct pim_nexthop_cache *) arg;
160
161 return jhash_1word (r->rpf.rpf_addr.u.prefix4.s_addr, 0);
162 }
163
164 /* Compare pim->rpf_hash node data */
165 static int
166 pim_rpf_equal (const void *arg1, const void *arg2)
167 {
168 const struct pim_nexthop_cache *r1 =
169 (const struct pim_nexthop_cache *) arg1;
170 const struct pim_nexthop_cache *r2 =
171 (const struct pim_nexthop_cache *) arg2;
172
173 return prefix_same (&r1->rpf.rpf_addr, &r2->rpf.rpf_addr);
174 }
175
176 /* Cleanup pim->rpf_hash each node data */
177 static void
178 pim_rp_list_hash_clean (void *data)
179 {
180 struct pim_nexthop_cache *pnc;
181
182 pnc = (struct pim_nexthop_cache *) data;
183 if (pnc->rp_list->count)
184 list_delete_all_node (pnc->rp_list);
185 if (pnc->upstream_list->count)
186 list_delete_all_node (pnc->upstream_list);
187 }
188
189 void
190 pim_prefix_list_update (struct prefix_list *plist)
191 {
192 pim_rp_prefix_list_update (plist);
193 pim_ssm_prefix_list_update (plist);
194 pim_upstream_spt_prefix_list_update (plist);
195 }
196
197 static void
198 pim_instance_terminate (void)
199 {
200 /* Traverse and cleanup rpf_hash */
201 if (pimg->rpf_hash)
202 {
203 hash_clean (pimg->rpf_hash, (void *) pim_rp_list_hash_clean);
204 hash_free (pimg->rpf_hash);
205 pimg->rpf_hash = NULL;
206 }
207
208 if (pimg->ssm_info)
209 {
210 pim_ssm_terminate (pimg->ssm_info);
211 pimg->ssm_info = NULL;
212 }
213
214 XFREE (MTYPE_PIM_PIM_INSTANCE, pimg);
215 }
216
217 static void pim_free()
218 {
219 pim_ssmpingd_destroy();
220
221 pim_oil_terminate ();
222
223 pim_upstream_terminate ();
224
225 if (qpim_static_route_list)
226 list_free(qpim_static_route_list);
227
228 pim_if_terminate ();
229 pim_rp_free ();
230
231 pim_route_map_terminate();
232
233 zclient_lookup_free ();
234
235 zprivs_terminate(&pimd_privs);
236 }
237
238 static struct pim_instance *
239 pim_instance_init (vrf_id_t vrf_id, afi_t afi)
240 {
241 struct pim_instance *pim;
242
243 pim = XCALLOC (MTYPE_PIM_PIM_INSTANCE, sizeof (struct pim_instance));
244 if (!pim)
245 return NULL;
246
247 pim->vrf_id = vrf_id;
248 pim->afi = afi;
249
250 pim->spt.switchover = PIM_SPT_IMMEDIATE;
251 pim->spt.plist = NULL;
252
253 pim->rpf_hash = hash_create_size (256, pim_rpf_hash_key, pim_rpf_equal, NULL);
254
255 if (PIM_DEBUG_ZEBRA)
256 zlog_debug ("%s: NHT rpf hash init ", __PRETTY_FUNCTION__);
257
258 pim->ssm_info = pim_ssm_init (vrf_id);
259 if (!pim->ssm_info) {
260 pim_instance_terminate ();
261 return NULL;
262 }
263
264 return pim;
265 }
266
267 void pim_init()
268 {
269 qpim_rp_keep_alive_time = PIM_RP_KEEPALIVE_PERIOD;
270
271 pim_rp_init ();
272
273 if (!inet_aton(PIM_ALL_PIM_ROUTERS, &qpim_all_pim_routers_addr)) {
274 zlog_err("%s %s: could not solve %s to group address: errno=%d: %s",
275 __FILE__, __PRETTY_FUNCTION__,
276 PIM_ALL_PIM_ROUTERS, errno, safe_strerror(errno));
277 zassert(0);
278 return;
279 }
280
281 pim_oil_init ();
282
283 pim_upstream_init ();
284
285 qpim_static_route_list = list_new();
286 if (!qpim_static_route_list) {
287 zlog_err("%s %s: failure: static_route_list=list_new()",
288 __FILE__, __PRETTY_FUNCTION__);
289 return;
290 }
291 qpim_static_route_list->del = (void (*)(void *)) pim_static_route_free;
292
293 pim_mroute_socket_enable();
294
295
296 /*
297 RFC 4601: 4.6.3. Assert Metrics
298
299 assert_metric
300 infinite_assert_metric() {
301 return {1,infinity,infinity,0}
302 }
303 */
304 qpim_infinite_assert_metric.rpt_bit_flag = 1;
305 qpim_infinite_assert_metric.metric_preference = PIM_ASSERT_METRIC_PREFERENCE_MAX;
306 qpim_infinite_assert_metric.route_metric = PIM_ASSERT_ROUTE_METRIC_MAX;
307 qpim_infinite_assert_metric.ip_address.s_addr = INADDR_ANY;
308
309 pim_if_init();
310 pim_cmd_init();
311 pim_ssmpingd_init();
312 }
313
314 void pim_terminate()
315 {
316 struct zclient *zclient;
317
318 pim_free();
319
320 /* reverse prefix_list_init */
321 prefix_list_add_hook (NULL);
322 prefix_list_delete_hook (NULL);
323 prefix_list_reset ();
324
325 pim_vrf_terminate ();
326
327 zclient = pim_zebra_zclient_get ();
328 if (zclient)
329 {
330 zclient_stop (zclient);
331 zclient_free (zclient);
332 }
333 }