]>
Commit | Line | Data |
---|---|---|
e9e2c950 RZ |
1 | /********************************************************************* |
2 | * Copyright 2013 Cumulus Networks, LLC. All rights reserved. | |
3 | * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * 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 | * bfd.c: implements the BFD protocol. | |
20 | * | |
21 | * Authors | |
22 | * ------- | |
23 | * Shrijeet Mukherjee [shm@cumulusnetworks.com] | |
24 | * Kanna Rajagopal [kanna@cumulusnetworks.com] | |
25 | * Radhika Mahankali [Radhika@cumulusnetworks.com] | |
26 | */ | |
27 | ||
28 | #include <zebra.h> | |
29 | ||
e9e2c950 RZ |
30 | #include "lib/jhash.h" |
31 | ||
32 | #include "bfd.h" | |
33 | ||
34 | DEFINE_QOBJ_TYPE(bfd_session); | |
35 | ||
36 | /* | |
37 | * Prototypes | |
38 | */ | |
39 | static uint32_t ptm_bfd_gen_ID(void); | |
40 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd); | |
41 | static void bfd_session_free(struct bfd_session *bs); | |
42 | static struct bfd_session *bfd_session_new(int sd); | |
43 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, | |
44 | uint32_t ldisc); | |
45 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc); | |
46 | static const char *get_diag_str(int diag); | |
47 | ||
48 | ||
49 | /* | |
50 | * Functions | |
51 | */ | |
52 | struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc) | |
53 | { | |
54 | struct bfd_session *bs; | |
55 | struct peer_label *pl; | |
56 | struct bfd_mhop_key mhop; | |
57 | struct bfd_shop_key shop; | |
58 | ||
59 | /* Try to find label first. */ | |
60 | if (bpc->bpc_has_label) { | |
61 | pl = pl_find(bpc->bpc_label); | |
62 | if (pl != NULL) { | |
63 | bs = pl->pl_bs; | |
64 | return bs; | |
65 | } | |
66 | } | |
67 | ||
68 | /* Otherwise fallback to peer/local hash lookup. */ | |
69 | if (bpc->bpc_mhop) { | |
70 | memset(&mhop, 0, sizeof(mhop)); | |
71 | mhop.peer = bpc->bpc_peer; | |
72 | mhop.local = bpc->bpc_local; | |
73 | if (bpc->bpc_has_vrfname) | |
74 | strlcpy(mhop.vrf_name, bpc->bpc_vrfname, | |
75 | sizeof(mhop.vrf_name)); | |
76 | ||
77 | bs = bfd_mhop_lookup(mhop); | |
78 | } else { | |
79 | memset(&shop, 0, sizeof(shop)); | |
80 | shop.peer = bpc->bpc_peer; | |
81 | if (!bpc->bpc_has_vxlan && bpc->bpc_has_localif) | |
82 | strlcpy(shop.port_name, bpc->bpc_localif, | |
83 | sizeof(shop.port_name)); | |
84 | ||
85 | bs = bfd_shop_lookup(shop); | |
86 | } | |
87 | ||
88 | return bs; | |
89 | } | |
90 | ||
91 | static uint32_t ptm_bfd_gen_ID(void) | |
92 | { | |
93 | static uint32_t sessionID = 1; | |
94 | ||
95 | return (sessionID++); | |
96 | } | |
97 | ||
98 | void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo) | |
99 | { | |
100 | uint64_t jitter, xmt_TO; | |
101 | int maxpercent; | |
102 | ||
103 | xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO; | |
104 | ||
105 | /* | |
106 | * From section 6.5.2: trasmit interval should be randomly jittered | |
107 | * between | |
108 | * 75% and 100% of nominal value, unless detect_mult is 1, then should | |
109 | * be | |
110 | * between 75% and 90%. | |
111 | */ | |
112 | maxpercent = (bfd->detect_mult == 1) ? 16 : 26; | |
113 | jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100; | |
114 | /* XXX remove that division above */ | |
115 | ||
116 | if (is_echo) | |
117 | bfd_echo_xmttimer_update(bfd, jitter); | |
118 | else | |
119 | bfd_xmttimer_update(bfd, jitter); | |
120 | } | |
121 | ||
122 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd) | |
123 | { | |
124 | /* Send the scheduled echo packet */ | |
125 | ptm_bfd_echo_snd(bfd); | |
126 | ||
127 | /* Restart the timer for next time */ | |
128 | ptm_bfd_start_xmt_timer(bfd, true); | |
129 | } | |
130 | ||
131 | void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit) | |
132 | { | |
133 | /* Send the scheduled control packet */ | |
134 | ptm_bfd_snd(bfd, fbit); | |
135 | ||
136 | /* Restart the timer for next time */ | |
137 | ptm_bfd_start_xmt_timer(bfd, false); | |
138 | } | |
139 | ||
140 | void ptm_bfd_echo_stop(struct bfd_session *bfd, int polling) | |
141 | { | |
142 | bfd->echo_xmt_TO = 0; | |
143 | bfd->echo_detect_TO = 0; | |
144 | BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE); | |
145 | ||
146 | bfd_echo_xmttimer_delete(bfd); | |
147 | bfd_echo_recvtimer_delete(bfd); | |
148 | ||
149 | if (polling) { | |
150 | bfd->polling = polling; | |
151 | bfd->new_timers.desired_min_tx = bfd->up_min_tx; | |
152 | bfd->new_timers.required_min_rx = bfd->timers.required_min_rx; | |
153 | ptm_bfd_snd(bfd, 0); | |
154 | } | |
155 | } | |
156 | ||
157 | void ptm_bfd_echo_start(struct bfd_session *bfd) | |
158 | { | |
159 | bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO); | |
160 | ptm_bfd_echo_xmt_TO(bfd); | |
161 | ||
162 | bfd->polling = 1; | |
163 | bfd->new_timers.desired_min_tx = bfd->up_min_tx; | |
164 | bfd->new_timers.required_min_rx = bfd->timers.required_min_rx; | |
165 | ptm_bfd_snd(bfd, 0); | |
166 | } | |
167 | ||
168 | void ptm_bfd_ses_up(struct bfd_session *bfd) | |
169 | { | |
03e7f088 RZ |
170 | int old_state = bfd->ses_state; |
171 | ||
e9e2c950 RZ |
172 | bfd->local_diag = 0; |
173 | bfd->ses_state = PTM_BFD_UP; | |
174 | bfd->polling = 1; | |
175 | monotime(&bfd->uptime); | |
176 | ||
177 | /* If the peer is capable to receiving Echo pkts */ | |
178 | if (bfd->echo_xmt_TO && !BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MH)) { | |
179 | ptm_bfd_echo_start(bfd); | |
180 | } else { | |
181 | bfd->new_timers.desired_min_tx = bfd->up_min_tx; | |
182 | bfd->new_timers.required_min_rx = bfd->timers.required_min_rx; | |
183 | ptm_bfd_snd(bfd, 0); | |
184 | } | |
185 | ||
186 | control_notify(bfd); | |
187 | ||
0684c9b1 RZ |
188 | if (old_state != bfd->ses_state) { |
189 | bfd->stats.session_up++; | |
03e7f088 RZ |
190 | log_info("state-change: [%s] %s -> %s", bs_to_string(bfd), |
191 | state_list[old_state].str, | |
192 | state_list[bfd->ses_state].str); | |
0684c9b1 | 193 | } |
e9e2c950 RZ |
194 | } |
195 | ||
196 | void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag) | |
197 | { | |
198 | int old_state = bfd->ses_state; | |
199 | ||
200 | bfd->local_diag = diag; | |
201 | bfd->discrs.remote_discr = 0; | |
202 | bfd->ses_state = PTM_BFD_DOWN; | |
203 | bfd->polling = 0; | |
204 | bfd->demand_mode = 0; | |
205 | monotime(&bfd->downtime); | |
206 | ||
207 | ptm_bfd_snd(bfd, 0); | |
208 | ||
209 | /* only signal clients when going from up->down state */ | |
210 | if (old_state == PTM_BFD_UP) | |
211 | control_notify(bfd); | |
212 | ||
e9e2c950 RZ |
213 | /* Stop echo packet transmission if they are active */ |
214 | if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
215 | ptm_bfd_echo_stop(bfd, 0); | |
03e7f088 | 216 | |
0684c9b1 RZ |
217 | if (old_state != bfd->ses_state) { |
218 | bfd->stats.session_down++; | |
03e7f088 RZ |
219 | log_info("state-change: [%s] %s -> %s reason:%s", |
220 | bs_to_string(bfd), state_list[old_state].str, | |
221 | state_list[bfd->ses_state].str, | |
222 | get_diag_str(bfd->local_diag)); | |
0684c9b1 | 223 | } |
e9e2c950 RZ |
224 | } |
225 | ||
226 | static int ptm_bfd_get_vrf_name(char *port_name, char *vrf_name) | |
227 | { | |
228 | struct bfd_iface *iface; | |
229 | struct bfd_vrf *vrf; | |
230 | ||
231 | if ((port_name == NULL) || (vrf_name == NULL)) | |
232 | return -1; | |
233 | ||
234 | iface = bfd_iface_lookup(port_name); | |
235 | if (iface) { | |
236 | vrf = bfd_vrf_lookup(iface->vrf_id); | |
237 | if (vrf) { | |
238 | strlcpy(vrf_name, vrf->name, sizeof(vrf->name)); | |
239 | return 0; | |
240 | } | |
241 | } | |
242 | return -1; | |
243 | } | |
244 | ||
245 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, | |
246 | uint32_t ldisc) | |
247 | { | |
248 | struct bfd_session *bs; | |
249 | ||
250 | bs = bfd_id_lookup(ldisc); | |
251 | if (bs == NULL) | |
252 | return NULL; | |
253 | ||
254 | /* Remove unused fields. */ | |
255 | switch (sa->sa_sin.sin_family) { | |
256 | case AF_INET: | |
257 | sa->sa_sin.sin_port = 0; | |
258 | if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin)) == 0) | |
259 | return bs; | |
260 | break; | |
261 | case AF_INET6: | |
262 | sa->sa_sin6.sin6_port = 0; | |
263 | if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin6)) == 0) | |
264 | return bs; | |
265 | break; | |
266 | } | |
267 | ||
268 | return NULL; | |
269 | } | |
270 | ||
271 | struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name, | |
272 | struct sockaddr_any *peer, | |
273 | struct sockaddr_any *local, | |
274 | char *vrf_name, bool is_mhop) | |
275 | { | |
276 | struct bfd_session *l_bfd = NULL; | |
277 | struct bfd_mhop_key mhop; | |
278 | struct bfd_shop_key shop; | |
9d63adda | 279 | char vrf_buf[MAXNAMELEN]; |
e9e2c950 | 280 | |
9d63adda RZ |
281 | /* Find our session using the ID signaled by the remote end. */ |
282 | if (cp->discrs.remote_discr) | |
283 | return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr)); | |
284 | ||
285 | /* Search for session without using discriminator. */ | |
286 | if (is_mhop) { | |
287 | memset(&mhop, 0, sizeof(mhop)); | |
288 | mhop.peer = *peer; | |
289 | mhop.local = *local; | |
290 | if (vrf_name && vrf_name[0]) { | |
291 | strlcpy(mhop.vrf_name, vrf_name, sizeof(mhop.vrf_name)); | |
292 | } else if (port_name && port_name[0]) { | |
293 | memset(vrf_buf, 0, sizeof(vrf_buf)); | |
294 | if (ptm_bfd_get_vrf_name(port_name, vrf_buf) != -1) | |
03e7f088 RZ |
295 | strlcpy(mhop.vrf_name, vrf_buf, |
296 | sizeof(mhop.vrf_name)); | |
e9e2c950 RZ |
297 | } |
298 | ||
9d63adda RZ |
299 | l_bfd = bfd_mhop_lookup(mhop); |
300 | } else { | |
301 | memset(&shop, 0, sizeof(shop)); | |
302 | shop.peer = *peer; | |
303 | if (port_name && port_name[0]) | |
03e7f088 RZ |
304 | strlcpy(shop.port_name, port_name, |
305 | sizeof(shop.port_name)); | |
9d63adda RZ |
306 | |
307 | l_bfd = bfd_shop_lookup(shop); | |
e9e2c950 RZ |
308 | } |
309 | ||
9d63adda RZ |
310 | /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */ |
311 | return l_bfd; | |
e9e2c950 RZ |
312 | } |
313 | ||
314 | #if 0 /* TODO VxLAN Support */ | |
315 | static void | |
316 | _update_vxlan_sess_parms(struct bfd_session *bfd, bfd_sess_parms *sess_parms) | |
317 | { | |
318 | struct bfd_session_vxlan_info *vxlan_info = &bfd->vxlan_info; | |
319 | bfd_parms_list *parms = &sess_parms->parms; | |
320 | ||
321 | vxlan_info->vnid = parms->vnid; | |
322 | vxlan_info->check_tnl_key = parms->check_tnl_key; | |
323 | vxlan_info->forwarding_if_rx = parms->forwarding_if_rx; | |
324 | vxlan_info->cpath_down = parms->cpath_down; | |
325 | vxlan_info->decay_min_rx = parms->decay_min_rx; | |
326 | ||
327 | inet_aton(parms->local_dst_ip, &vxlan_info->local_dst_ip); | |
328 | inet_aton(parms->remote_dst_ip, &vxlan_info->peer_dst_ip); | |
329 | ||
330 | memcpy(vxlan_info->local_dst_mac, parms->local_dst_mac, ETH_ALEN); | |
331 | memcpy(vxlan_info->peer_dst_mac, parms->remote_dst_mac, ETH_ALEN); | |
332 | ||
333 | /* The interface may change for Vxlan BFD sessions, so update | |
334 | * the local mac and ifindex | |
335 | */ | |
336 | bfd->ifindex = sess_parms->ifindex; | |
337 | memcpy(bfd->local_mac, sess_parms->local_mac, sizeof(bfd->local_mac)); | |
338 | } | |
339 | #endif /* VxLAN support */ | |
340 | ||
341 | int bfd_xmt_cb(struct thread *t) | |
342 | { | |
343 | struct bfd_session *bs = THREAD_ARG(t); | |
344 | ||
345 | ptm_bfd_xmt_TO(bs, 0); | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | int bfd_echo_xmt_cb(struct thread *t) | |
351 | { | |
352 | struct bfd_session *bs = THREAD_ARG(t); | |
353 | ||
354 | ptm_bfd_echo_xmt_TO(bs); | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | /* Was ptm_bfd_detect_TO() */ | |
360 | int bfd_recvtimer_cb(struct thread *t) | |
361 | { | |
362 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
363 | |
364 | switch (bs->ses_state) { | |
365 | case PTM_BFD_INIT: | |
366 | case PTM_BFD_UP: | |
367 | ptm_bfd_ses_dn(bs, BFD_DIAGDETECTTIME); | |
e9e2c950 RZ |
368 | bfd_recvtimer_update(bs); |
369 | break; | |
370 | ||
371 | default: | |
372 | /* Second detect time expiration, zero remote discr (section | |
373 | * 6.5.1) | |
374 | */ | |
375 | bs->discrs.remote_discr = 0; | |
376 | break; | |
377 | } | |
378 | ||
e9e2c950 RZ |
379 | return 0; |
380 | } | |
381 | ||
382 | /* Was ptm_bfd_echo_detect_TO() */ | |
383 | int bfd_echo_recvtimer_cb(struct thread *t) | |
384 | { | |
385 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
386 | |
387 | switch (bs->ses_state) { | |
388 | case PTM_BFD_INIT: | |
389 | case PTM_BFD_UP: | |
390 | ptm_bfd_ses_dn(bs, BFD_DIAGDETECTTIME); | |
e9e2c950 RZ |
391 | break; |
392 | } | |
393 | ||
e9e2c950 RZ |
394 | return 0; |
395 | } | |
396 | ||
397 | static struct bfd_session *bfd_session_new(int sd) | |
398 | { | |
399 | struct bfd_session *bs; | |
400 | ||
401 | bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs)); | |
402 | if (bs == NULL) | |
403 | return NULL; | |
404 | ||
405 | QOBJ_REG(bs, bfd_session); | |
406 | ||
407 | bs->up_min_tx = BFD_DEFDESIREDMINTX; | |
408 | bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX; | |
409 | bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO; | |
410 | bs->detect_mult = BFD_DEFDETECTMULT; | |
411 | bs->mh_ttl = BFD_DEF_MHOP_TTL; | |
412 | ||
413 | bs->sock = sd; | |
414 | monotime(&bs->uptime); | |
415 | bs->downtime = bs->uptime; | |
416 | ||
417 | return bs; | |
418 | } | |
419 | ||
420 | int bfd_session_update_label(struct bfd_session *bs, const char *nlabel) | |
421 | { | |
422 | /* New label treatment: | |
423 | * - Check if the label is taken; | |
424 | * - Try to allocate the memory for it and register; | |
425 | */ | |
426 | if (bs->pl == NULL) { | |
427 | if (pl_find(nlabel) != NULL) { | |
428 | /* Someone is already using it. */ | |
429 | return -1; | |
430 | } | |
431 | ||
432 | if (pl_new(nlabel, bs) == NULL) | |
433 | return -1; | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | /* | |
439 | * Test label change consistency: | |
440 | * - Do nothing if it's the same label; | |
441 | * - Check if the future label is already taken; | |
442 | * - Change label; | |
443 | */ | |
444 | if (strcmp(nlabel, bs->pl->pl_label) == 0) | |
445 | return -1; | |
446 | if (pl_find(nlabel) != NULL) | |
447 | return -1; | |
448 | ||
449 | strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label)); | |
450 | return 0; | |
451 | } | |
452 | ||
453 | static void _bfd_session_update(struct bfd_session *bs, | |
454 | struct bfd_peer_cfg *bpc) | |
455 | { | |
456 | if (bpc->bpc_echo) { | |
457 | /* Check if echo mode is already active. */ | |
458 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
459 | goto skip_echo; | |
460 | ||
461 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
462 | ptm_bfd_echo_start(bs); | |
463 | ||
464 | /* Activate/update echo receive timeout timer. */ | |
465 | bfd_echo_recvtimer_update(bs); | |
466 | } else { | |
467 | /* Check if echo mode is already disabled. */ | |
468 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
469 | goto skip_echo; | |
470 | ||
471 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
472 | ptm_bfd_echo_stop(bs, 0); | |
473 | } | |
474 | ||
475 | skip_echo: | |
476 | if (bpc->bpc_has_txinterval) | |
477 | bs->up_min_tx = bpc->bpc_txinterval * 1000; | |
478 | ||
479 | if (bpc->bpc_has_recvinterval) | |
480 | bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000; | |
481 | ||
482 | if (bpc->bpc_has_detectmultiplier) | |
483 | bs->detect_mult = bpc->bpc_detectmultiplier; | |
484 | ||
485 | if (bpc->bpc_has_echointerval) | |
486 | bs->timers.required_min_echo = bpc->bpc_echointerval * 1000; | |
487 | ||
488 | if (bpc->bpc_has_label) | |
489 | bfd_session_update_label(bs, bpc->bpc_label); | |
490 | ||
491 | if (bpc->bpc_shutdown) { | |
492 | /* Check if already shutdown. */ | |
493 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
494 | return; | |
495 | ||
496 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
497 | ||
498 | /* Disable all events. */ | |
499 | bfd_recvtimer_delete(bs); | |
500 | bfd_echo_recvtimer_delete(bs); | |
501 | bfd_xmttimer_delete(bs); | |
502 | bfd_echo_xmttimer_delete(bs); | |
503 | ||
504 | /* Change and notify state change. */ | |
505 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
506 | control_notify(bs); | |
507 | ||
508 | ptm_bfd_snd(bs, 0); | |
509 | } else { | |
510 | /* Check if already working. */ | |
511 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
512 | return; | |
513 | ||
514 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
515 | ||
516 | /* Change and notify state change. */ | |
517 | bs->ses_state = PTM_BFD_DOWN; | |
518 | control_notify(bs); | |
519 | ||
520 | /* Enable all timers. */ | |
521 | bfd_recvtimer_update(bs); | |
522 | bfd_xmttimer_update(bs, bs->xmt_TO); | |
523 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) { | |
524 | bfd_echo_recvtimer_update(bs); | |
525 | bfd_echo_xmttimer_update(bs, bs->echo_xmt_TO); | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc) | |
531 | { | |
532 | /* User didn't want to update, return failure. */ | |
533 | if (bpc->bpc_createonly) | |
534 | return -1; | |
535 | ||
536 | _bfd_session_update(bs, bpc); | |
537 | ||
538 | /* TODO add VxLAN support. */ | |
539 | ||
540 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
541 | ||
542 | return 0; | |
543 | } | |
544 | ||
545 | static void bfd_session_free(struct bfd_session *bs) | |
546 | { | |
547 | if (bs->sock != -1) | |
548 | close(bs->sock); | |
549 | ||
550 | bfd_recvtimer_delete(bs); | |
551 | bfd_echo_recvtimer_delete(bs); | |
552 | bfd_xmttimer_delete(bs); | |
553 | bfd_echo_xmttimer_delete(bs); | |
554 | ||
555 | bfd_id_delete(bs->discrs.my_discr); | |
556 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) | |
557 | bfd_mhop_delete(bs->mhop); | |
558 | else | |
559 | bfd_shop_delete(bs->shop); | |
560 | ||
561 | pl_free(bs->pl); | |
562 | ||
563 | QOBJ_UNREG(bs); | |
564 | XFREE(MTYPE_BFDD_CONFIG, bs); | |
565 | } | |
566 | ||
567 | struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc) | |
568 | { | |
569 | struct bfd_session *bfd, *l_bfd; | |
570 | int psock; | |
571 | ||
572 | /* check to see if this needs a new session */ | |
573 | l_bfd = bs_peer_find(bpc); | |
574 | if (l_bfd) { | |
575 | /* Requesting a duplicated peer means update configuration. */ | |
576 | if (bfd_session_update(l_bfd, bpc) == 0) | |
577 | return l_bfd; | |
578 | else | |
579 | return NULL; | |
580 | } | |
581 | ||
582 | /* | |
583 | * Get socket for transmitting control packets. Note that if we | |
584 | * could use the destination port (3784) for the source | |
585 | * port we wouldn't need a socket per session. | |
586 | */ | |
587 | if (bpc->bpc_ipv4) { | |
588 | psock = bp_peer_socket(bpc); | |
03e7f088 | 589 | if (psock == -1) |
e9e2c950 | 590 | return NULL; |
e9e2c950 RZ |
591 | } else { |
592 | psock = bp_peer_socketv6(bpc); | |
03e7f088 | 593 | if (psock == -1) |
e9e2c950 | 594 | return NULL; |
e9e2c950 RZ |
595 | } |
596 | ||
597 | /* Get memory */ | |
598 | bfd = bfd_session_new(psock); | |
599 | if (bfd == NULL) { | |
03e7f088 | 600 | log_error("session-new: allocation failed"); |
e9e2c950 RZ |
601 | return NULL; |
602 | } | |
603 | ||
604 | if (bpc->bpc_has_localif && !bpc->bpc_mhop) { | |
605 | bfd->ifindex = ptm_bfd_fetch_ifindex(bpc->bpc_localif); | |
606 | ptm_bfd_fetch_local_mac(bpc->bpc_localif, bfd->local_mac); | |
607 | } | |
608 | ||
609 | if (bpc->bpc_has_vxlan) | |
610 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_VXLAN); | |
611 | ||
43adc702 | 612 | if (bpc->bpc_ipv4 == false) { |
e9e2c950 RZ |
613 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6); |
614 | ||
43adc702 RZ |
615 | /* Set the IPv6 scope id for link-local addresses. */ |
616 | if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_local.sa_sin6.sin6_addr)) | |
617 | bpc->bpc_local.sa_sin6.sin6_scope_id = bfd->ifindex; | |
618 | if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_peer.sa_sin6.sin6_addr)) | |
619 | bpc->bpc_peer.sa_sin6.sin6_scope_id = bfd->ifindex; | |
620 | } | |
621 | ||
e9e2c950 RZ |
622 | /* Initialize the session */ |
623 | bfd->ses_state = PTM_BFD_DOWN; | |
624 | bfd->discrs.my_discr = ptm_bfd_gen_ID(); | |
625 | bfd->discrs.remote_discr = 0; | |
626 | bfd->local_ip = bpc->bpc_local; | |
627 | bfd->local_address = bpc->bpc_local; | |
628 | bfd->timers.desired_min_tx = bfd->up_min_tx; | |
629 | bfd->detect_TO = (bfd->detect_mult * BFD_DEF_SLOWTX); | |
630 | ||
631 | /* Use detect_TO first for slow detection, then use recvtimer_update. */ | |
632 | bfd_recvtimer_update(bfd); | |
633 | ||
634 | bfd_id_insert(bfd); | |
635 | ||
636 | if (bpc->bpc_mhop) { | |
637 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH); | |
638 | bfd->mhop.peer = bpc->bpc_peer; | |
639 | bfd->mhop.local = bpc->bpc_local; | |
640 | if (bpc->bpc_has_vrfname) | |
641 | strlcpy(bfd->mhop.vrf_name, bpc->bpc_vrfname, | |
642 | sizeof(bfd->mhop.vrf_name)); | |
643 | ||
644 | bfd_mhop_insert(bfd); | |
645 | } else { | |
646 | bfd->shop.peer = bpc->bpc_peer; | |
647 | if (!bpc->bpc_has_vxlan && bpc->bpc_has_localif) | |
648 | strlcpy(bfd->shop.port_name, bpc->bpc_localif, | |
649 | sizeof(bfd->shop.port_name)); | |
650 | ||
651 | bfd_shop_insert(bfd); | |
652 | } | |
653 | ||
654 | if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_VXLAN)) { | |
655 | static uint8_t bfd_def_vxlan_dmac[] = {0x00, 0x23, 0x20, | |
656 | 0x00, 0x00, 0x01}; | |
657 | memcpy(bfd->peer_mac, bfd_def_vxlan_dmac, | |
658 | sizeof(bfd_def_vxlan_dmac)); | |
659 | } | |
660 | #if 0 /* TODO */ | |
661 | else if (event->rmac) { | |
662 | if (sscanf(event->rmac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
663 | &bfd->peer_mac[0], &bfd->peer_mac[1], &bfd->peer_mac[2], | |
664 | &bfd->peer_mac[3], &bfd->peer_mac[4], &bfd->peer_mac[5]) | |
665 | != 6) | |
666 | DLOG("%s: Assigning remote mac = %s", __func__, | |
667 | event->rmac); | |
668 | } | |
669 | #endif | |
670 | ||
671 | /* | |
672 | * XXX: session update triggers echo start, so we must have our | |
673 | * discriminator ID set first. | |
674 | */ | |
675 | _bfd_session_update(bfd, bpc); | |
676 | ||
677 | /* Start transmitting with slow interval until peer responds */ | |
678 | bfd->xmt_TO = BFD_DEF_SLOWTX; | |
679 | ||
680 | ptm_bfd_xmt_TO(bfd, 0); | |
681 | ||
03e7f088 | 682 | log_info("session-new: %s", bs_to_string(bfd)); |
e9e2c950 RZ |
683 | |
684 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd); | |
685 | ||
686 | return bfd; | |
687 | } | |
688 | ||
689 | int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc) | |
690 | { | |
691 | struct bfd_session *bs; | |
692 | ||
693 | /* Find session and call free(). */ | |
694 | bs = bs_peer_find(bpc); | |
695 | if (bs == NULL) | |
696 | return -1; | |
697 | ||
698 | /* This pointer is being referenced, don't let it be deleted. */ | |
699 | if (bs->refcount > 0) { | |
03e7f088 RZ |
700 | log_error("session-delete: refcount failure: %" PRIu64 |
701 | " references", | |
702 | bs->refcount); | |
e9e2c950 RZ |
703 | return -1; |
704 | } | |
705 | ||
03e7f088 | 706 | log_info("session-delete: %s", bs_to_string(bs)); |
e9e2c950 RZ |
707 | |
708 | control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs); | |
709 | ||
710 | bfd_session_free(bs); | |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
d3f3a2c4 RZ |
715 | void bfd_set_polling(struct bfd_session *bs) |
716 | { | |
717 | bs->new_timers.desired_min_tx = bs->up_min_tx; | |
718 | bs->new_timers.required_min_rx = bs->timers.required_min_rx; | |
719 | bs->new_timers.required_min_echo = bs->timers.required_min_echo; | |
720 | bs->polling = 1; | |
721 | } | |
722 | ||
e9e2c950 RZ |
723 | |
724 | /* | |
725 | * Helper functions. | |
726 | */ | |
727 | static const char *get_diag_str(int diag) | |
728 | { | |
729 | for (int i = 0; diag_list[i].str; i++) { | |
730 | if (diag_list[i].type == diag) | |
731 | return diag_list[i].str; | |
732 | } | |
733 | return "N/A"; | |
734 | } | |
735 | ||
736 | const char *satostr(struct sockaddr_any *sa) | |
737 | { | |
738 | #define INETSTR_BUFCOUNT 8 | |
739 | static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN]; | |
740 | static int bufidx; | |
741 | struct sockaddr_in *sin = &sa->sa_sin; | |
742 | struct sockaddr_in6 *sin6 = &sa->sa_sin6; | |
743 | ||
744 | bufidx += (bufidx + 1) % INETSTR_BUFCOUNT; | |
745 | buf[bufidx][0] = 0; | |
746 | ||
747 | switch (sin->sin_family) { | |
748 | case AF_INET: | |
749 | inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx], | |
750 | sizeof(buf[bufidx])); | |
751 | break; | |
752 | case AF_INET6: | |
753 | inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx], | |
754 | sizeof(buf[bufidx])); | |
755 | break; | |
756 | ||
757 | default: | |
758 | strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx])); | |
759 | break; | |
760 | } | |
761 | ||
762 | return buf[bufidx]; | |
763 | } | |
764 | ||
765 | const char *diag2str(uint8_t diag) | |
766 | { | |
767 | switch (diag) { | |
768 | case 0: | |
769 | return "ok"; | |
770 | case 1: | |
771 | return "control detection time expired"; | |
772 | case 2: | |
773 | return "echo function failed"; | |
774 | case 3: | |
775 | return "neighbor signaled session down"; | |
776 | case 4: | |
777 | return "forwarding plane reset"; | |
778 | case 5: | |
779 | return "path down"; | |
780 | case 6: | |
781 | return "concatenated path down"; | |
782 | case 7: | |
783 | return "administratively down"; | |
784 | case 8: | |
785 | return "reverse concatenated path down"; | |
786 | default: | |
787 | return "unknown"; | |
788 | } | |
789 | } | |
790 | ||
791 | int strtosa(const char *addr, struct sockaddr_any *sa) | |
792 | { | |
793 | memset(sa, 0, sizeof(*sa)); | |
794 | ||
795 | if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) { | |
796 | sa->sa_sin.sin_family = AF_INET; | |
797 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
798 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
799 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
800 | return 0; | |
801 | } | |
802 | ||
803 | if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) { | |
804 | sa->sa_sin6.sin6_family = AF_INET6; | |
805 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
806 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
807 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
808 | return 0; | |
809 | } | |
810 | ||
811 | return -1; | |
812 | } | |
813 | ||
814 | void integer2timestr(uint64_t time, char *buf, size_t buflen) | |
815 | { | |
816 | unsigned int year, month, day, hour, minute, second; | |
817 | int rv; | |
818 | ||
819 | #define MINUTES (60) | |
820 | #define HOURS (24 * MINUTES) | |
821 | #define DAYS (30 * HOURS) | |
822 | #define MONTHS (12 * DAYS) | |
823 | #define YEARS (MONTHS) | |
824 | if (time >= YEARS) { | |
825 | year = time / YEARS; | |
826 | time -= year * YEARS; | |
827 | ||
828 | rv = snprintf(buf, buflen, "%u year(s), ", year); | |
829 | buf += rv; | |
830 | buflen -= rv; | |
831 | } | |
832 | if (time >= MONTHS) { | |
833 | month = time / MONTHS; | |
834 | time -= month * MONTHS; | |
835 | ||
836 | rv = snprintf(buf, buflen, "%u month(s), ", month); | |
837 | buf += rv; | |
838 | buflen -= rv; | |
839 | } | |
840 | if (time >= DAYS) { | |
841 | day = time / DAYS; | |
842 | time -= day * DAYS; | |
843 | ||
844 | rv = snprintf(buf, buflen, "%u day(s), ", day); | |
845 | buf += rv; | |
846 | buflen -= rv; | |
847 | } | |
848 | if (time >= HOURS) { | |
849 | hour = time / HOURS; | |
850 | time -= hour * HOURS; | |
851 | ||
852 | rv = snprintf(buf, buflen, "%u hour(s), ", hour); | |
853 | buf += rv; | |
854 | buflen -= rv; | |
855 | } | |
856 | if (time >= MINUTES) { | |
857 | minute = time / MINUTES; | |
858 | time -= minute * MINUTES; | |
859 | ||
860 | rv = snprintf(buf, buflen, "%u minute(s), ", minute); | |
861 | buf += rv; | |
862 | buflen -= rv; | |
863 | } | |
864 | second = time % MINUTES; | |
865 | snprintf(buf, buflen, "%u second(s)", second); | |
866 | } | |
867 | ||
03e7f088 RZ |
868 | const char *bs_to_string(struct bfd_session *bs) |
869 | { | |
870 | static char buf[256]; | |
871 | int pos; | |
872 | bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
873 | ||
874 | pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no"); | |
875 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) { | |
876 | pos += snprintf(buf + pos, sizeof(buf) - pos, | |
877 | " peer:%s local:%s", satostr(&bs->mhop.peer), | |
878 | satostr(&bs->mhop.local)); | |
879 | ||
880 | if (bs->mhop.vrf_name[0]) | |
881 | snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s", | |
882 | bs->mhop.vrf_name); | |
883 | } else { | |
884 | pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s", | |
885 | satostr(&bs->shop.peer)); | |
886 | ||
887 | if (bs->local_address.sa_sin.sin_family) | |
888 | pos += snprintf(buf + pos, sizeof(buf) - pos, | |
889 | " local:%s", | |
890 | satostr(&bs->local_address)); | |
891 | ||
892 | if (bs->shop.port_name[0]) | |
893 | snprintf(buf + pos, sizeof(buf) - pos, " interface:%s", | |
894 | bs->shop.port_name); | |
895 | } | |
896 | ||
897 | return buf; | |
898 | } | |
899 | ||
e9e2c950 RZ |
900 | |
901 | /* | |
902 | * BFD hash data structures to find sessions. | |
903 | */ | |
904 | static struct hash *bfd_id_hash; | |
905 | static struct hash *bfd_shop_hash; | |
906 | static struct hash *bfd_mhop_hash; | |
907 | static struct hash *bfd_vrf_hash; | |
908 | static struct hash *bfd_iface_hash; | |
909 | ||
910 | static unsigned int bfd_id_hash_do(void *p); | |
911 | static int bfd_id_hash_cmp(const void *n1, const void *n2); | |
912 | static unsigned int bfd_shop_hash_do(void *p); | |
913 | static int bfd_shop_hash_cmp(const void *n1, const void *n2); | |
914 | static unsigned int bfd_mhop_hash_do(void *p); | |
915 | static int bfd_mhop_hash_cmp(const void *n1, const void *n2); | |
916 | static unsigned int bfd_vrf_hash_do(void *p); | |
917 | static int bfd_vrf_hash_cmp(const void *n1, const void *n2); | |
918 | static unsigned int bfd_iface_hash_do(void *p); | |
919 | static int bfd_iface_hash_cmp(const void *n1, const void *n2); | |
920 | ||
921 | static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop); | |
922 | static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop); | |
923 | static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop); | |
924 | static int _iface_key(struct bfd_iface *iface, const char *ifname); | |
925 | ||
926 | static void _bfd_free(struct hash_backet *hb, | |
927 | void *arg __attribute__((__unused__))); | |
928 | static void _vrf_free(void *arg); | |
929 | static void _iface_free(void *arg); | |
930 | ||
931 | /* BFD hash for our discriminator. */ | |
932 | static unsigned int bfd_id_hash_do(void *p) | |
933 | { | |
934 | struct bfd_session *bs = p; | |
935 | ||
936 | return jhash_1word(bs->discrs.my_discr, 0); | |
937 | } | |
938 | ||
939 | static int bfd_id_hash_cmp(const void *n1, const void *n2) | |
940 | { | |
941 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
942 | ||
943 | return bs1->discrs.my_discr == bs2->discrs.my_discr; | |
944 | } | |
945 | ||
946 | /* BFD hash for single hop. */ | |
947 | static unsigned int bfd_shop_hash_do(void *p) | |
948 | { | |
949 | struct bfd_session *bs = p; | |
950 | ||
951 | return jhash(&bs->shop, sizeof(bs->shop), 0); | |
952 | } | |
953 | ||
954 | static int bfd_shop_hash_cmp(const void *n1, const void *n2) | |
955 | { | |
956 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
957 | ||
958 | return memcmp(&bs1->shop, &bs2->shop, sizeof(bs1->shop)) == 0; | |
959 | } | |
960 | ||
961 | /* BFD hash for multi hop. */ | |
962 | static unsigned int bfd_mhop_hash_do(void *p) | |
963 | { | |
964 | struct bfd_session *bs = p; | |
965 | ||
966 | return jhash(&bs->mhop, sizeof(bs->mhop), 0); | |
967 | } | |
968 | ||
969 | static int bfd_mhop_hash_cmp(const void *n1, const void *n2) | |
970 | { | |
971 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
972 | ||
973 | return memcmp(&bs1->mhop, &bs2->mhop, sizeof(bs1->mhop)) == 0; | |
974 | } | |
975 | ||
976 | /* BFD hash for VRFs. */ | |
977 | static unsigned int bfd_vrf_hash_do(void *p) | |
978 | { | |
979 | struct bfd_vrf *vrf = p; | |
980 | ||
981 | return jhash_1word(vrf->vrf_id, 0); | |
982 | } | |
983 | ||
984 | static int bfd_vrf_hash_cmp(const void *n1, const void *n2) | |
985 | { | |
986 | const struct bfd_vrf *v1 = n1, *v2 = n2; | |
987 | ||
988 | return v1->vrf_id == v2->vrf_id; | |
989 | } | |
990 | ||
991 | /* BFD hash for interfaces. */ | |
992 | static unsigned int bfd_iface_hash_do(void *p) | |
993 | { | |
994 | struct bfd_iface *iface = p; | |
995 | ||
996 | return string_hash_make(iface->ifname); | |
997 | } | |
998 | ||
999 | static int bfd_iface_hash_cmp(const void *n1, const void *n2) | |
1000 | { | |
1001 | const struct bfd_iface *i1 = n1, *i2 = n2; | |
1002 | ||
1003 | return strcmp(i1->ifname, i2->ifname) == 0; | |
1004 | } | |
1005 | ||
1006 | /* Helper functions */ | |
1007 | static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop) | |
1008 | { | |
1009 | bs->shop = *shop; | |
1010 | ||
1011 | /* Remove unused fields. */ | |
1012 | switch (bs->shop.peer.sa_sin.sin_family) { | |
1013 | case AF_INET: | |
1014 | bs->shop.peer.sa_sin.sin_port = 0; | |
1015 | break; | |
1016 | case AF_INET6: | |
1017 | bs->shop.peer.sa_sin6.sin6_port = 0; | |
1018 | break; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop) | |
1023 | { | |
1024 | _shop_key(bs, shop); | |
1025 | memset(bs->shop.port_name, 0, sizeof(bs->shop.port_name)); | |
1026 | } | |
1027 | ||
1028 | static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop) | |
1029 | { | |
1030 | bs->mhop = *mhop; | |
1031 | ||
1032 | /* Remove unused fields. */ | |
1033 | switch (bs->mhop.peer.sa_sin.sin_family) { | |
1034 | case AF_INET: | |
1035 | bs->mhop.peer.sa_sin.sin_port = 0; | |
1036 | bs->mhop.local.sa_sin.sin_port = 0; | |
1037 | break; | |
1038 | case AF_INET6: | |
1039 | bs->mhop.peer.sa_sin6.sin6_port = 0; | |
1040 | bs->mhop.local.sa_sin6.sin6_port = 0; | |
1041 | break; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | static int _iface_key(struct bfd_iface *iface, const char *ifname) | |
1046 | { | |
1047 | size_t slen = sizeof(iface->ifname); | |
1048 | ||
1049 | memset(iface->ifname, 0, slen); | |
1050 | if (strlcpy(iface->ifname, ifname, slen) >= slen) | |
1051 | return -1; | |
1052 | ||
1053 | return 0; | |
1054 | } | |
1055 | ||
e9e2c950 RZ |
1056 | /* |
1057 | * Hash public interface / exported functions. | |
1058 | */ | |
1059 | ||
1060 | /* Lookup functions. */ | |
1061 | struct bfd_session *bfd_id_lookup(uint32_t id) | |
1062 | { | |
1063 | struct bfd_session bs; | |
1064 | ||
1065 | bs.discrs.my_discr = id; | |
1066 | ||
1067 | return hash_lookup(bfd_id_hash, &bs); | |
1068 | } | |
1069 | ||
1070 | struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop) | |
1071 | { | |
1072 | struct bfd_session bs, *bsp; | |
1073 | ||
1074 | _shop_key(&bs, &shop); | |
1075 | ||
1076 | bsp = hash_lookup(bfd_shop_hash, &bs); | |
1077 | if (bsp == NULL && bs.shop.port_name[0] != 0) { | |
1078 | /* | |
1079 | * Since the local interface spec is optional, try | |
1080 | * searching the key without it as well. | |
1081 | */ | |
1082 | _shop_key2(&bs, &shop); | |
1083 | bsp = hash_lookup(bfd_shop_hash, &bs); | |
1084 | } | |
1085 | ||
1086 | return bsp; | |
1087 | } | |
1088 | ||
1089 | struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop) | |
1090 | { | |
1091 | struct bfd_session bs; | |
1092 | ||
1093 | _mhop_key(&bs, &mhop); | |
1094 | ||
a88f98ae | 1095 | return hash_lookup(bfd_mhop_hash, &bs); |
e9e2c950 RZ |
1096 | } |
1097 | ||
1098 | struct bfd_vrf *bfd_vrf_lookup(int vrf_id) | |
1099 | { | |
1100 | struct bfd_vrf vrf; | |
1101 | ||
1102 | vrf.vrf_id = vrf_id; | |
1103 | ||
1104 | return hash_lookup(bfd_vrf_hash, &vrf); | |
1105 | } | |
1106 | ||
1107 | struct bfd_iface *bfd_iface_lookup(const char *ifname) | |
1108 | { | |
1109 | struct bfd_iface iface; | |
1110 | ||
1111 | if (_iface_key(&iface, ifname) != 0) | |
1112 | return NULL; | |
1113 | ||
1114 | return hash_lookup(bfd_iface_hash, &iface); | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * Delete functions. | |
1119 | * | |
1120 | * Delete functions searches and remove the item from the hash and | |
1121 | * returns a pointer to the removed item data. If the item was not found | |
1122 | * then it returns NULL. | |
1123 | * | |
1124 | * The data stored inside the hash is not free()ed, so you must do it | |
1125 | * manually after getting the pointer back. | |
1126 | */ | |
1127 | struct bfd_session *bfd_id_delete(uint32_t id) | |
1128 | { | |
1129 | struct bfd_session bs; | |
1130 | ||
1131 | bs.discrs.my_discr = id; | |
1132 | ||
1133 | return hash_release(bfd_id_hash, &bs); | |
1134 | } | |
1135 | ||
1136 | struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop) | |
1137 | { | |
1138 | struct bfd_session bs, *bsp; | |
1139 | ||
1140 | _shop_key(&bs, &shop); | |
1141 | bsp = hash_release(bfd_shop_hash, &bs); | |
1142 | if (bsp == NULL && shop.port_name[0] != 0) { | |
1143 | /* | |
1144 | * Since the local interface spec is optional, try | |
1145 | * searching the key without it as well. | |
1146 | */ | |
1147 | _shop_key2(&bs, &shop); | |
1148 | bsp = hash_release(bfd_shop_hash, &bs); | |
1149 | } | |
1150 | ||
1151 | return bsp; | |
1152 | } | |
1153 | ||
1154 | struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop) | |
1155 | { | |
1156 | struct bfd_session bs; | |
1157 | ||
1158 | _mhop_key(&bs, &mhop); | |
1159 | ||
1160 | return hash_release(bfd_mhop_hash, &bs); | |
1161 | } | |
1162 | ||
1163 | struct bfd_vrf *bfd_vrf_delete(int vrf_id) | |
1164 | { | |
1165 | struct bfd_vrf vrf; | |
1166 | ||
1167 | vrf.vrf_id = vrf_id; | |
1168 | ||
1169 | return hash_release(bfd_vrf_hash, &vrf); | |
1170 | } | |
1171 | ||
1172 | struct bfd_iface *bfd_iface_delete(const char *ifname) | |
1173 | { | |
1174 | struct bfd_iface iface; | |
1175 | ||
1176 | if (_iface_key(&iface, ifname) != 0) | |
1177 | return NULL; | |
1178 | ||
1179 | return hash_release(bfd_iface_hash, &iface); | |
1180 | } | |
1181 | ||
1182 | /* Iteration functions. */ | |
1183 | void bfd_id_iterate(hash_iter_func hif, void *arg) | |
1184 | { | |
1185 | hash_iterate(bfd_id_hash, hif, arg); | |
1186 | } | |
1187 | ||
1188 | void bfd_shop_iterate(hash_iter_func hif, void *arg) | |
1189 | { | |
1190 | hash_iterate(bfd_shop_hash, hif, arg); | |
1191 | } | |
1192 | ||
1193 | void bfd_mhop_iterate(hash_iter_func hif, void *arg) | |
1194 | { | |
1195 | hash_iterate(bfd_mhop_hash, hif, arg); | |
1196 | } | |
1197 | ||
1198 | void bfd_vrf_iterate(hash_iter_func hif, void *arg) | |
1199 | { | |
1200 | hash_iterate(bfd_vrf_hash, hif, arg); | |
1201 | } | |
1202 | ||
1203 | void bfd_iface_iterate(hash_iter_func hif, void *arg) | |
1204 | { | |
1205 | hash_iterate(bfd_iface_hash, hif, arg); | |
1206 | } | |
1207 | ||
1208 | /* | |
1209 | * Insert functions. | |
1210 | * | |
1211 | * Inserts session into hash and returns `true` on success, otherwise | |
1212 | * `false`. | |
1213 | */ | |
1214 | bool bfd_id_insert(struct bfd_session *bs) | |
1215 | { | |
1216 | return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs); | |
1217 | } | |
1218 | ||
1219 | bool bfd_shop_insert(struct bfd_session *bs) | |
1220 | { | |
1221 | return (hash_get(bfd_shop_hash, bs, hash_alloc_intern) == bs); | |
1222 | } | |
1223 | ||
1224 | bool bfd_mhop_insert(struct bfd_session *bs) | |
1225 | { | |
1226 | return (hash_get(bfd_mhop_hash, bs, hash_alloc_intern) == bs); | |
1227 | } | |
1228 | ||
1229 | bool bfd_vrf_insert(struct bfd_vrf *vrf) | |
1230 | { | |
1231 | return (hash_get(bfd_vrf_hash, vrf, hash_alloc_intern) == vrf); | |
1232 | } | |
1233 | ||
1234 | bool bfd_iface_insert(struct bfd_iface *iface) | |
1235 | { | |
1236 | return (hash_get(bfd_iface_hash, iface, hash_alloc_intern) == iface); | |
1237 | } | |
1238 | ||
1239 | void bfd_initialize(void) | |
1240 | { | |
1241 | bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp, | |
1242 | "BFD discriminator hash"); | |
1243 | bfd_shop_hash = hash_create(bfd_shop_hash_do, bfd_shop_hash_cmp, | |
1244 | "BFD single hop hash"); | |
1245 | bfd_mhop_hash = hash_create(bfd_mhop_hash_do, bfd_mhop_hash_cmp, | |
1246 | "BFD multihop hop hash"); | |
1247 | bfd_vrf_hash = | |
1248 | hash_create(bfd_vrf_hash_do, bfd_vrf_hash_cmp, "BFD VRF hash"); | |
1249 | bfd_iface_hash = hash_create(bfd_iface_hash_do, bfd_iface_hash_cmp, | |
1250 | "BFD interface hash"); | |
1251 | } | |
1252 | ||
1253 | static void _bfd_free(struct hash_backet *hb, | |
1254 | void *arg __attribute__((__unused__))) | |
1255 | { | |
1256 | struct bfd_session *bs = hb->data; | |
1257 | ||
1258 | bfd_session_free(bs); | |
1259 | } | |
1260 | ||
1261 | static void _vrf_free(void *arg) | |
1262 | { | |
1263 | struct bfd_vrf *vrf = arg; | |
1264 | ||
1265 | XFREE(MTYPE_BFDD_CONFIG, vrf); | |
1266 | } | |
1267 | ||
1268 | static void _iface_free(void *arg) | |
1269 | { | |
1270 | struct bfd_iface *iface = arg; | |
1271 | ||
1272 | XFREE(MTYPE_BFDD_CONFIG, iface); | |
1273 | } | |
1274 | ||
1275 | void bfd_shutdown(void) | |
1276 | { | |
1277 | /* | |
1278 | * Close and free all BFD sessions. | |
1279 | * | |
1280 | * _bfd_free() will call bfd_session_free() which will take care | |
1281 | * of removing the session from all hashes, so we just run an | |
1282 | * assert() here to make sure it really happened. | |
1283 | */ | |
1284 | bfd_id_iterate(_bfd_free, NULL); | |
1285 | assert(bfd_shop_hash->count == 0); | |
1286 | assert(bfd_mhop_hash->count == 0); | |
1287 | ||
1288 | /* Clean the VRF and interface hashes. */ | |
1289 | hash_clean(bfd_vrf_hash, _vrf_free); | |
1290 | hash_clean(bfd_iface_hash, _iface_free); | |
1291 | ||
1292 | /* Now free the hashes themselves. */ | |
1293 | hash_free(bfd_id_hash); | |
1294 | hash_free(bfd_shop_hash); | |
1295 | hash_free(bfd_mhop_hash); | |
1296 | hash_free(bfd_vrf_hash); | |
1297 | hash_free(bfd_iface_hash); | |
1298 | } |