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