]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / bfdd / bfd.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*********************************************************************
3 * Copyright 2013 Cumulus Networks, LLC. All rights reserved.
4 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
5 *
6 * bfd.c: implements the BFD protocol.
7 *
8 * Authors
9 * -------
10 * Shrijeet Mukherjee [shm@cumulusnetworks.com]
11 * Kanna Rajagopal [kanna@cumulusnetworks.com]
12 * Radhika Mahankali [Radhika@cumulusnetworks.com]
13 */
14
15 #include <zebra.h>
16
17 #include "lib/jhash.h"
18 #include "lib/network.h"
19
20 #include "bfd.h"
21
22 DEFINE_MTYPE_STATIC(BFDD, BFDD_CONFIG, "long-lived configuration memory");
23 DEFINE_MTYPE_STATIC(BFDD, BFDD_PROFILE, "long-lived profile memory");
24 DEFINE_MTYPE_STATIC(BFDD, BFDD_SESSION_OBSERVER, "Session observer");
25 DEFINE_MTYPE_STATIC(BFDD, BFDD_VRF, "BFD VRF");
26
27 /*
28 * Prototypes
29 */
30 static uint32_t ptm_bfd_gen_ID(void);
31 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd);
32 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
33 uint32_t ldisc);
34 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
35 static const char *get_diag_str(int diag);
36
37 static void bs_admin_down_handler(struct bfd_session *bs, int nstate);
38 static void bs_down_handler(struct bfd_session *bs, int nstate);
39 static void bs_init_handler(struct bfd_session *bs, int nstate);
40 static void bs_up_handler(struct bfd_session *bs, int nstate);
41
42 /**
43 * Remove BFD profile from all BFD sessions so we don't leave dangling
44 * pointers.
45 */
46 static void bfd_profile_detach(struct bfd_profile *bp);
47
48 /* Zeroed array with the size of an IPv6 address. */
49 struct in6_addr zero_addr;
50
51 /** BFD profiles list. */
52 struct bfdproflist bplist;
53
54 /*
55 * Functions
56 */
57 struct bfd_profile *bfd_profile_lookup(const char *name)
58 {
59 struct bfd_profile *bp;
60
61 TAILQ_FOREACH (bp, &bplist, entry) {
62 if (strcmp(name, bp->name))
63 continue;
64
65 return bp;
66 }
67
68 return NULL;
69 }
70
71 static void bfd_profile_set_default(struct bfd_profile *bp)
72 {
73 bp->admin_shutdown = false;
74 bp->detection_multiplier = BFD_DEFDETECTMULT;
75 bp->echo_mode = false;
76 bp->passive = false;
77 bp->minimum_ttl = BFD_DEF_MHOP_TTL;
78 bp->min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
79 bp->min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
80 bp->min_rx = BFD_DEFREQUIREDMINRX;
81 bp->min_tx = BFD_DEFDESIREDMINTX;
82 }
83
84 struct bfd_profile *bfd_profile_new(const char *name)
85 {
86 struct bfd_profile *bp;
87
88 /* Search for duplicates. */
89 if (bfd_profile_lookup(name) != NULL)
90 return NULL;
91
92 /* Allocate, name it and put into list. */
93 bp = XCALLOC(MTYPE_BFDD_PROFILE, sizeof(*bp));
94 strlcpy(bp->name, name, sizeof(bp->name));
95 TAILQ_INSERT_TAIL(&bplist, bp, entry);
96
97 /* Set default values. */
98 bfd_profile_set_default(bp);
99
100 return bp;
101 }
102
103 void bfd_profile_free(struct bfd_profile *bp)
104 {
105 /* Detach from any session. */
106 if (bglobal.bg_shutdown == false)
107 bfd_profile_detach(bp);
108
109 /* Remove from global list. */
110 TAILQ_REMOVE(&bplist, bp, entry);
111
112 XFREE(MTYPE_BFDD_PROFILE, bp);
113 }
114
115 void bfd_profile_apply(const char *profname, struct bfd_session *bs)
116 {
117 struct bfd_profile *bp;
118
119 /* Remove previous profile if any. */
120 if (bs->profile_name) {
121 /* We are changing profiles. */
122 if (strcmp(bs->profile_name, profname)) {
123 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
124 bs->profile_name =
125 XSTRDUP(MTYPE_BFDD_PROFILE, profname);
126 }
127 } else /* Save the current profile name (in case it doesn't exist). */
128 bs->profile_name = XSTRDUP(MTYPE_BFDD_PROFILE, profname);
129
130 /* Look up new profile to apply. */
131 bp = bfd_profile_lookup(profname);
132
133 /* Point to profile if it exists. */
134 bs->profile = bp;
135
136 /* Apply configuration. */
137 bfd_session_apply(bs);
138 }
139
140 void bfd_session_apply(struct bfd_session *bs)
141 {
142 struct bfd_profile *bp;
143 uint32_t min_tx = bs->timers.desired_min_tx;
144 uint32_t min_rx = bs->timers.required_min_rx;
145
146 /* Pick the source of configuration. */
147 bp = bs->profile ? bs->profile : &bs->peer_profile;
148
149 /* Set multiplier if not the default. */
150 if (bs->peer_profile.detection_multiplier == BFD_DEFDETECTMULT)
151 bs->detect_mult = bp->detection_multiplier;
152 else
153 bs->detect_mult = bs->peer_profile.detection_multiplier;
154
155 /* Set timers if not the default. */
156 if (bs->peer_profile.min_tx == BFD_DEFDESIREDMINTX)
157 bs->timers.desired_min_tx = bp->min_tx;
158 else
159 bs->timers.desired_min_tx = bs->peer_profile.min_tx;
160
161 if (bs->peer_profile.min_rx == BFD_DEFREQUIREDMINRX)
162 bs->timers.required_min_rx = bp->min_rx;
163 else
164 bs->timers.required_min_rx = bs->peer_profile.min_rx;
165
166 /* We can only apply echo options on single hop sessions. */
167 if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) {
168 /* Configure echo timers if they were default. */
169 if (bs->peer_profile.min_echo_rx == BFD_DEF_REQ_MIN_ECHO_RX)
170 bs->timers.required_min_echo_rx = bp->min_echo_rx;
171 else
172 bs->timers.required_min_echo_rx =
173 bs->peer_profile.min_echo_rx;
174
175 if (bs->peer_profile.min_echo_tx == BFD_DEF_DES_MIN_ECHO_TX)
176 bs->timers.desired_min_echo_tx = bp->min_echo_tx;
177 else
178 bs->timers.desired_min_echo_tx =
179 bs->peer_profile.min_echo_tx;
180
181 /* Toggle echo if default value. */
182 if (bs->peer_profile.echo_mode == false)
183 bfd_set_echo(bs, bp->echo_mode);
184 else
185 bfd_set_echo(bs, bs->peer_profile.echo_mode);
186 } else {
187 /* Configure the TTL packet filter. */
188 if (bs->peer_profile.minimum_ttl == BFD_DEF_MHOP_TTL)
189 bs->mh_ttl = bp->minimum_ttl;
190 else
191 bs->mh_ttl = bs->peer_profile.minimum_ttl;
192 }
193
194 /* Toggle 'passive-mode' if default value. */
195 if (bs->peer_profile.passive == false)
196 bfd_set_passive_mode(bs, bp->passive);
197 else
198 bfd_set_passive_mode(bs, bs->peer_profile.passive);
199
200 /* Toggle 'no shutdown' if default value. */
201 if (bs->peer_profile.admin_shutdown == false)
202 bfd_set_shutdown(bs, bp->admin_shutdown);
203 else
204 bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown);
205
206 /* If session interval changed negotiate new timers. */
207 if (bs->ses_state == PTM_BFD_UP
208 && (bs->timers.desired_min_tx != min_tx
209 || bs->timers.required_min_rx != min_rx))
210 bfd_set_polling(bs);
211
212 /* Send updated information to data plane. */
213 bfd_dplane_update_session(bs);
214 }
215
216 void bfd_profile_remove(struct bfd_session *bs)
217 {
218 /* Remove any previous set profile name. */
219 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
220 bs->profile = NULL;
221
222 bfd_session_apply(bs);
223 }
224
225 void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
226 struct sockaddr_any *local, bool mhop, const char *ifname,
227 const char *vrfname)
228 {
229 memset(key, 0, sizeof(*key));
230
231 switch (peer->sa_sin.sin_family) {
232 case AF_INET:
233 key->family = AF_INET;
234 memcpy(&key->peer, &peer->sa_sin.sin_addr,
235 sizeof(peer->sa_sin.sin_addr));
236 memcpy(&key->local, &local->sa_sin.sin_addr,
237 sizeof(local->sa_sin.sin_addr));
238 break;
239 case AF_INET6:
240 key->family = AF_INET6;
241 memcpy(&key->peer, &peer->sa_sin6.sin6_addr,
242 sizeof(peer->sa_sin6.sin6_addr));
243 memcpy(&key->local, &local->sa_sin6.sin6_addr,
244 sizeof(local->sa_sin6.sin6_addr));
245 break;
246 }
247
248 key->mhop = mhop;
249 if (ifname && ifname[0])
250 strlcpy(key->ifname, ifname, sizeof(key->ifname));
251 if (vrfname && vrfname[0])
252 strlcpy(key->vrfname, vrfname, sizeof(key->vrfname));
253 else
254 strlcpy(key->vrfname, VRF_DEFAULT_NAME, sizeof(key->vrfname));
255 }
256
257 struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc)
258 {
259 struct bfd_session *bs;
260 struct peer_label *pl;
261 struct bfd_key key;
262
263 /* Try to find label first. */
264 if (bpc->bpc_has_label) {
265 pl = pl_find(bpc->bpc_label);
266 if (pl != NULL) {
267 bs = pl->pl_bs;
268 return bs;
269 }
270 }
271
272 /* Otherwise fallback to peer/local hash lookup. */
273 gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop,
274 bpc->bpc_localif, bpc->bpc_vrfname);
275
276 return bfd_key_lookup(key);
277 }
278
279 /*
280 * Starts a disabled BFD session.
281 *
282 * A session is disabled when the specified interface/VRF doesn't exist
283 * yet. It might happen on FRR boot or with virtual interfaces.
284 */
285 int bfd_session_enable(struct bfd_session *bs)
286 {
287 struct interface *ifp = NULL;
288 struct vrf *vrf = NULL;
289 int psock;
290
291 /* We are using data plane, we don't need software. */
292 if (bs->bdc)
293 return 0;
294
295 /*
296 * If the interface or VRF doesn't exist, then we must register
297 * the session but delay its start.
298 */
299 if (bs->key.vrfname[0]) {
300 vrf = vrf_lookup_by_name(bs->key.vrfname);
301 if (vrf == NULL) {
302 zlog_err(
303 "session-enable: specified VRF %s doesn't exists.",
304 bs->key.vrfname);
305 return 0;
306 }
307 } else {
308 vrf = vrf_lookup_by_id(VRF_DEFAULT);
309 }
310
311 assert(vrf);
312
313 if (bs->key.ifname[0]) {
314 ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id);
315 if (ifp == NULL) {
316 zlog_err(
317 "session-enable: specified interface %s (VRF %s) doesn't exist.",
318 bs->key.ifname, vrf->name);
319 return 0;
320 }
321 }
322
323 /* Assign interface/VRF pointers. */
324 bs->vrf = vrf;
325
326 /* Assign interface pointer (if any). */
327 bs->ifp = ifp;
328
329 /* Attempt to use data plane. */
330 if (bglobal.bg_use_dplane && bfd_dplane_add_session(bs) == 0) {
331 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bs);
332 return 0;
333 }
334
335 /* Sanity check: don't leak open sockets. */
336 if (bs->sock != -1) {
337 if (bglobal.debug_peer_event)
338 zlog_debug("%s: previous socket open", __func__);
339
340 close(bs->sock);
341 bs->sock = -1;
342 }
343
344 /*
345 * Get socket for transmitting control packets. Note that if we
346 * could use the destination port (3784) for the source
347 * port we wouldn't need a socket per session.
348 */
349 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) {
350 psock = bp_peer_socket(bs);
351 if (psock == -1)
352 return 0;
353 } else {
354 psock = bp_peer_socketv6(bs);
355 if (psock == -1)
356 return 0;
357 }
358
359 /*
360 * We've got a valid socket, lets start the timers and the
361 * protocol.
362 */
363 bs->sock = psock;
364
365 /* Only start timers if we are using active mode. */
366 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE) == 0) {
367 bfd_recvtimer_update(bs);
368 ptm_bfd_start_xmt_timer(bs, false);
369 }
370
371 /* initialize RTT */
372 bfd_rtt_init(bs);
373
374 return 0;
375 }
376
377 /*
378 * Disabled a running BFD session.
379 *
380 * A session is disabled when the specified interface/VRF gets removed
381 * (e.g. virtual interfaces).
382 */
383 void bfd_session_disable(struct bfd_session *bs)
384 {
385 /* We are using data plane, we don't need software. */
386 if (bs->bdc)
387 return;
388
389 /* Free up socket resources. */
390 if (bs->sock != -1) {
391 close(bs->sock);
392 bs->sock = -1;
393 }
394
395 /* Disable all timers. */
396 bfd_recvtimer_delete(bs);
397 bfd_xmttimer_delete(bs);
398 ptm_bfd_echo_stop(bs);
399
400 /* Set session down so it doesn't report UP and disabled. */
401 ptm_bfd_sess_dn(bs, BD_PATH_DOWN);
402 }
403
404 static uint32_t ptm_bfd_gen_ID(void)
405 {
406 uint32_t session_id;
407
408 /*
409 * RFC 5880, Section 6.8.1. recommends that we should generate
410 * random session identification numbers.
411 */
412 do {
413 session_id = ((frr_weak_random() << 16) & 0xFFFF0000)
414 | (frr_weak_random() & 0x0000FFFF);
415 } while (session_id == 0 || bfd_id_lookup(session_id) != NULL);
416
417 return session_id;
418 }
419
420 void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo)
421 {
422 uint64_t jitter, xmt_TO;
423 int maxpercent;
424
425 xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO;
426
427 /*
428 * From section 6.5.2: trasmit interval should be randomly jittered
429 * between
430 * 75% and 100% of nominal value, unless detect_mult is 1, then should
431 * be
432 * between 75% and 90%.
433 */
434 maxpercent = (bfd->detect_mult == 1) ? 16 : 26;
435 jitter = (xmt_TO * (75 + (frr_weak_random() % maxpercent))) / 100;
436 /* XXX remove that division above */
437
438 if (is_echo)
439 bfd_echo_xmttimer_update(bfd, jitter);
440 else
441 bfd_xmttimer_update(bfd, jitter);
442 }
443
444 static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd)
445 {
446 /* Send the scheduled echo packet */
447 /* if ipv4 use the new echo implementation that causes
448 * the packet to be looped in forwarding plane of peer
449 */
450 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6) == 0)
451 #ifdef BFD_LINUX
452 ptm_bfd_echo_fp_snd(bfd);
453 #else
454 ptm_bfd_echo_snd(bfd);
455 #endif
456 else
457 ptm_bfd_echo_snd(bfd);
458
459 /* Restart the timer for next time */
460 ptm_bfd_start_xmt_timer(bfd, true);
461 }
462
463 void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit)
464 {
465 /* Send the scheduled control packet */
466 ptm_bfd_snd(bfd, fbit);
467
468 /* Restart the timer for next time */
469 ptm_bfd_start_xmt_timer(bfd, false);
470 }
471
472 void ptm_bfd_echo_stop(struct bfd_session *bfd)
473 {
474 bfd->echo_xmt_TO = 0;
475 bfd->echo_detect_TO = 0;
476 UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE);
477
478 bfd_echo_xmttimer_delete(bfd);
479 bfd_echo_recvtimer_delete(bfd);
480 }
481
482 void ptm_bfd_echo_start(struct bfd_session *bfd)
483 {
484 bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO);
485 if (bfd->echo_detect_TO > 0) {
486 bfd_echo_recvtimer_update(bfd);
487 ptm_bfd_echo_xmt_TO(bfd);
488 }
489 }
490
491 void ptm_bfd_sess_up(struct bfd_session *bfd)
492 {
493 int old_state = bfd->ses_state;
494
495 bfd->local_diag = 0;
496 bfd->ses_state = PTM_BFD_UP;
497 monotime(&bfd->uptime);
498
499 /* Connection is up, lets negotiate timers. */
500 bfd_set_polling(bfd);
501
502 /* Start sending control packets with poll bit immediately. */
503 ptm_bfd_snd(bfd, 0);
504
505 control_notify(bfd, bfd->ses_state);
506
507 if (old_state != bfd->ses_state) {
508 bfd->stats.session_up++;
509 if (bglobal.debug_peer_event)
510 zlog_debug("state-change: [%s] %s -> %s",
511 bs_to_string(bfd), state_list[old_state].str,
512 state_list[bfd->ses_state].str);
513 }
514 }
515
516 void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag)
517 {
518 int old_state = bfd->ses_state;
519
520 bfd->local_diag = diag;
521 bfd->discrs.remote_discr = 0;
522 bfd->ses_state = PTM_BFD_DOWN;
523 bfd->polling = 0;
524 bfd->demand_mode = 0;
525 monotime(&bfd->downtime);
526
527 /*
528 * Only attempt to send if we have a valid socket:
529 * this function might be called by session disablers and in
530 * this case we won't have a valid socket (i.e. interface was
531 * removed or VRF doesn't exist anymore).
532 */
533 if (bfd->sock != -1)
534 ptm_bfd_snd(bfd, 0);
535
536 /* Slow down the control packets, the connection is down. */
537 bs_set_slow_timers(bfd);
538
539 /* only signal clients when going from up->down state */
540 if (old_state == PTM_BFD_UP)
541 control_notify(bfd, PTM_BFD_DOWN);
542
543 /* Stop echo packet transmission if they are active */
544 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
545 ptm_bfd_echo_stop(bfd);
546
547 /* Stop attempting to transmit or expect control packets if passive. */
548 if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_PASSIVE)) {
549 bfd_recvtimer_delete(bfd);
550 bfd_xmttimer_delete(bfd);
551 }
552
553 if (old_state != bfd->ses_state) {
554 bfd->stats.session_down++;
555 if (bglobal.debug_peer_event)
556 zlog_debug("state-change: [%s] %s -> %s reason:%s",
557 bs_to_string(bfd), state_list[old_state].str,
558 state_list[bfd->ses_state].str,
559 get_diag_str(bfd->local_diag));
560 }
561
562 /* clear peer's mac address */
563 UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET);
564 memset(bfd->peer_hw_addr, 0, sizeof(bfd->peer_hw_addr));
565 /* reset local address ,it might has been be changed after bfd is up*/
566 memset(&bfd->local_address, 0, sizeof(bfd->local_address));
567
568 /* reset RTT */
569 bfd_rtt_init(bfd);
570 }
571
572 static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa,
573 uint32_t ldisc)
574 {
575 struct bfd_session *bs;
576
577 bs = bfd_id_lookup(ldisc);
578 if (bs == NULL)
579 return NULL;
580
581 switch (bs->key.family) {
582 case AF_INET:
583 if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer,
584 sizeof(sa->sa_sin.sin_addr)))
585 return NULL;
586 break;
587 case AF_INET6:
588 if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer,
589 sizeof(sa->sa_sin6.sin6_addr)))
590 return NULL;
591 break;
592 }
593
594 return bs;
595 }
596
597 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
598 struct sockaddr_any *peer,
599 struct sockaddr_any *local,
600 struct interface *ifp,
601 vrf_id_t vrfid,
602 bool is_mhop)
603 {
604 struct vrf *vrf;
605 struct bfd_key key;
606
607 /* Find our session using the ID signaled by the remote end. */
608 if (cp->discrs.remote_discr)
609 return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr));
610
611 /* Search for session without using discriminator. */
612 vrf = vrf_lookup_by_id(vrfid);
613
614 gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL,
615 vrf ? vrf->name : VRF_DEFAULT_NAME);
616
617 /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */
618 return bfd_key_lookup(key);
619 }
620
621 void bfd_xmt_cb(struct thread *t)
622 {
623 struct bfd_session *bs = THREAD_ARG(t);
624
625 ptm_bfd_xmt_TO(bs, 0);
626 }
627
628 void bfd_echo_xmt_cb(struct thread *t)
629 {
630 struct bfd_session *bs = THREAD_ARG(t);
631
632 if (bs->echo_xmt_TO > 0)
633 ptm_bfd_echo_xmt_TO(bs);
634 }
635
636 /* Was ptm_bfd_detect_TO() */
637 void bfd_recvtimer_cb(struct thread *t)
638 {
639 struct bfd_session *bs = THREAD_ARG(t);
640
641 switch (bs->ses_state) {
642 case PTM_BFD_INIT:
643 case PTM_BFD_UP:
644 ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED);
645 break;
646 }
647 }
648
649 /* Was ptm_bfd_echo_detect_TO() */
650 void bfd_echo_recvtimer_cb(struct thread *t)
651 {
652 struct bfd_session *bs = THREAD_ARG(t);
653
654 switch (bs->ses_state) {
655 case PTM_BFD_INIT:
656 case PTM_BFD_UP:
657 ptm_bfd_sess_dn(bs, BD_ECHO_FAILED);
658 break;
659 }
660 }
661
662 struct bfd_session *bfd_session_new(void)
663 {
664 struct bfd_session *bs;
665
666 bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs));
667
668 /* Set peer session defaults. */
669 bfd_profile_set_default(&bs->peer_profile);
670
671 bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX;
672 bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX;
673 bs->timers.required_min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX;
674 bs->timers.desired_min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX;
675 bs->detect_mult = BFD_DEFDETECTMULT;
676 bs->mh_ttl = BFD_DEF_MHOP_TTL;
677 bs->ses_state = PTM_BFD_DOWN;
678
679 /* Initiate connection with slow timers. */
680 bs_set_slow_timers(bs);
681
682 /* Initiate remote settings as well. */
683 bs->remote_timers = bs->cur_timers;
684 bs->remote_detect_mult = BFD_DEFDETECTMULT;
685
686 bs->sock = -1;
687 monotime(&bs->uptime);
688 bs->downtime = bs->uptime;
689
690 return bs;
691 }
692
693 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel)
694 {
695 /* New label treatment:
696 * - Check if the label is taken;
697 * - Try to allocate the memory for it and register;
698 */
699 if (bs->pl == NULL) {
700 if (pl_find(nlabel) != NULL) {
701 /* Someone is already using it. */
702 return -1;
703 }
704
705 pl_new(nlabel, bs);
706
707 return 0;
708 }
709
710 /*
711 * Test label change consistency:
712 * - Do nothing if it's the same label;
713 * - Check if the future label is already taken;
714 * - Change label;
715 */
716 if (strcmp(nlabel, bs->pl->pl_label) == 0)
717 return -1;
718 if (pl_find(nlabel) != NULL)
719 return -1;
720
721 strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label));
722 return 0;
723 }
724
725 static void _bfd_session_update(struct bfd_session *bs,
726 struct bfd_peer_cfg *bpc)
727 {
728 if (bpc->bpc_has_txinterval) {
729 bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000;
730 bs->peer_profile.min_tx = bs->timers.desired_min_tx;
731 }
732
733 if (bpc->bpc_has_recvinterval) {
734 bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000;
735 bs->peer_profile.min_rx = bs->timers.required_min_rx;
736 }
737
738 if (bpc->bpc_has_detectmultiplier) {
739 bs->detect_mult = bpc->bpc_detectmultiplier;
740 bs->peer_profile.detection_multiplier = bs->detect_mult;
741 }
742
743 if (bpc->bpc_has_echorecvinterval) {
744 bs->timers.required_min_echo_rx = bpc->bpc_echorecvinterval * 1000;
745 bs->peer_profile.min_echo_rx = bs->timers.required_min_echo_rx;
746 }
747
748 if (bpc->bpc_has_echotxinterval) {
749 bs->timers.desired_min_echo_tx = bpc->bpc_echotxinterval * 1000;
750 bs->peer_profile.min_echo_tx = bs->timers.desired_min_echo_tx;
751 }
752
753 if (bpc->bpc_has_label)
754 bfd_session_update_label(bs, bpc->bpc_label);
755
756 if (bpc->bpc_cbit)
757 SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
758 else
759 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT);
760
761 if (bpc->bpc_has_minimum_ttl) {
762 bs->mh_ttl = bpc->bpc_minimum_ttl;
763 bs->peer_profile.minimum_ttl = bpc->bpc_minimum_ttl;
764 }
765
766 bs->peer_profile.echo_mode = bpc->bpc_echo;
767 bfd_set_echo(bs, bpc->bpc_echo);
768
769 /*
770 * Shutdown needs to be the last in order to avoid timers enable when
771 * the session is disabled.
772 */
773 bs->peer_profile.admin_shutdown = bpc->bpc_shutdown;
774 bfd_set_passive_mode(bs, bpc->bpc_passive);
775 bfd_set_shutdown(bs, bpc->bpc_shutdown);
776
777 /*
778 * Apply profile last: it also calls `bfd_set_shutdown`.
779 *
780 * There is no problem calling `shutdown` twice if the value doesn't
781 * change or if it is overridden by peer specific configuration.
782 */
783 if (bpc->bpc_has_profile)
784 bfd_profile_apply(bpc->bpc_profile, bs);
785 }
786
787 static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
788 {
789 /* User didn't want to update, return failure. */
790 if (bpc->bpc_createonly)
791 return -1;
792
793 _bfd_session_update(bs, bpc);
794
795 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
796
797 return 0;
798 }
799
800 void bfd_session_free(struct bfd_session *bs)
801 {
802 struct bfd_session_observer *bso;
803
804 bfd_session_disable(bs);
805
806 /* Remove session from data plane if any. */
807 bfd_dplane_delete_session(bs);
808
809 bfd_key_delete(bs->key);
810 bfd_id_delete(bs->discrs.my_discr);
811
812 /* Remove observer if any. */
813 TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) {
814 if (bso->bso_bs != bs)
815 continue;
816
817 break;
818 }
819 if (bso != NULL)
820 bs_observer_del(bso);
821
822 pl_free(bs->pl);
823
824 XFREE(MTYPE_BFDD_PROFILE, bs->profile_name);
825 XFREE(MTYPE_BFDD_CONFIG, bs);
826 }
827
828 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc)
829 {
830 struct bfd_session *bfd, *l_bfd;
831
832 /* check to see if this needs a new session */
833 l_bfd = bs_peer_find(bpc);
834 if (l_bfd) {
835 /* Requesting a duplicated peer means update configuration. */
836 if (bfd_session_update(l_bfd, bpc) == 0)
837 return l_bfd;
838 else
839 return NULL;
840 }
841
842 /* Get BFD session storage with its defaults. */
843 bfd = bfd_session_new();
844
845 /*
846 * Store interface/VRF name in case we need to delay session
847 * start. See `bfd_session_enable` for more information.
848 */
849 if (bpc->bpc_has_localif)
850 strlcpy(bfd->key.ifname, bpc->bpc_localif,
851 sizeof(bfd->key.ifname));
852
853 if (bpc->bpc_has_vrfname)
854 strlcpy(bfd->key.vrfname, bpc->bpc_vrfname,
855 sizeof(bfd->key.vrfname));
856 else
857 strlcpy(bfd->key.vrfname, VRF_DEFAULT_NAME,
858 sizeof(bfd->key.vrfname));
859
860 /* Copy remaining data. */
861 if (bpc->bpc_ipv4 == false)
862 SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6);
863
864 bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6;
865 switch (bfd->key.family) {
866 case AF_INET:
867 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr,
868 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
869 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr,
870 sizeof(bpc->bpc_local.sa_sin.sin_addr));
871 break;
872
873 case AF_INET6:
874 memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr,
875 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
876 memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr,
877 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
878 break;
879
880 default:
881 assert(1);
882 break;
883 }
884
885 if (bpc->bpc_mhop)
886 SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH);
887
888 bfd->key.mhop = bpc->bpc_mhop;
889
890 if (bs_registrate(bfd) == NULL)
891 return NULL;
892
893 /* Apply other configurations. */
894 _bfd_session_update(bfd, bpc);
895
896 return bfd;
897 }
898
899 struct bfd_session *bs_registrate(struct bfd_session *bfd)
900 {
901 /* Registrate session into data structures. */
902 bfd_key_insert(bfd);
903 bfd->discrs.my_discr = ptm_bfd_gen_ID();
904 bfd_id_insert(bfd);
905
906 /* Try to enable session and schedule for packet receive/send. */
907 if (bfd_session_enable(bfd) == -1) {
908 /* Unrecoverable failure, remove the session/peer. */
909 bfd_session_free(bfd);
910 return NULL;
911 }
912
913 /* Add observer if we have moving parts. */
914 if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1)
915 bs_observer_add(bfd);
916
917 if (bglobal.debug_peer_event)
918 zlog_debug("session-new: %s", bs_to_string(bfd));
919
920 control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd);
921
922 return bfd;
923 }
924
925 int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc)
926 {
927 struct bfd_session *bs;
928
929 /* Find session and call free(). */
930 bs = bs_peer_find(bpc);
931 if (bs == NULL)
932 return -1;
933
934 /* This pointer is being referenced, don't let it be deleted. */
935 if (bs->refcount > 0) {
936 zlog_err("session-delete: refcount failure: %" PRIu64" references",
937 bs->refcount);
938 return -1;
939 }
940
941 if (bglobal.debug_peer_event)
942 zlog_debug("%s: %s", __func__, bs_to_string(bs));
943
944 control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs);
945
946 bfd_session_free(bs);
947
948 return 0;
949 }
950
951 void bfd_set_polling(struct bfd_session *bs)
952 {
953 /*
954 * Start polling procedure: the only timers that require polling
955 * to change value without losing connection are:
956 *
957 * - Desired minimum transmission interval;
958 * - Required minimum receive interval;
959 *
960 * RFC 5880, Section 6.8.3.
961 */
962 bs->polling = 1;
963 }
964
965 /*
966 * bs_<state>_handler() functions implement the BFD state machine
967 * transition mechanism. `<state>` is the current session state and
968 * the parameter `nstate` is the peer new state.
969 */
970 static void bs_admin_down_handler(struct bfd_session *bs
971 __attribute__((__unused__)),
972 int nstate __attribute__((__unused__)))
973 {
974 /*
975 * We are administratively down, there is no state machine
976 * handling.
977 */
978 }
979
980 static void bs_down_handler(struct bfd_session *bs, int nstate)
981 {
982 switch (nstate) {
983 case PTM_BFD_ADM_DOWN:
984 /*
985 * Remote peer doesn't want to talk, so lets keep the
986 * connection down.
987 */
988 case PTM_BFD_UP:
989 /* Peer can't be up yet, wait it go to 'init' or 'down'. */
990 break;
991
992 case PTM_BFD_DOWN:
993 /*
994 * Remote peer agreed that the path is down, lets try to
995 * bring it up.
996 */
997 bs->ses_state = PTM_BFD_INIT;
998
999 /*
1000 * RFC 5880, Section 6.1.
1001 * A system taking the Passive role MUST NOT begin
1002 * sending BFD packets for a particular session until
1003 * it has received a BFD packet for that session, and thus
1004 * has learned the remote system's discriminator value.
1005 *
1006 * Now we can start transmission timer in passive mode.
1007 */
1008 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE))
1009 ptm_bfd_xmt_TO(bs, 0);
1010
1011 break;
1012
1013 case PTM_BFD_INIT:
1014 /*
1015 * Remote peer told us his path is up, lets turn
1016 * activate the session.
1017 */
1018 ptm_bfd_sess_up(bs);
1019 break;
1020
1021 default:
1022 if (bglobal.debug_peer_event)
1023 zlog_debug("state-change: unhandled neighbor state: %d",
1024 nstate);
1025 break;
1026 }
1027 }
1028
1029 static void bs_init_handler(struct bfd_session *bs, int nstate)
1030 {
1031 switch (nstate) {
1032 case PTM_BFD_ADM_DOWN:
1033 /*
1034 * Remote peer doesn't want to talk, so lets make the
1035 * connection down.
1036 */
1037 ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN);
1038 break;
1039
1040 case PTM_BFD_DOWN:
1041 /* Remote peer hasn't moved to first stage yet. */
1042 break;
1043
1044 case PTM_BFD_INIT:
1045 case PTM_BFD_UP:
1046 /* We agreed on the settings and the path is up. */
1047 ptm_bfd_sess_up(bs);
1048 break;
1049
1050 default:
1051 if (bglobal.debug_peer_event)
1052 zlog_debug("state-change: unhandled neighbor state: %d",
1053 nstate);
1054 break;
1055 }
1056 }
1057
1058 static void bs_up_handler(struct bfd_session *bs, int nstate)
1059 {
1060 switch (nstate) {
1061 case PTM_BFD_ADM_DOWN:
1062 case PTM_BFD_DOWN:
1063 /* Peer lost or asked to shutdown connection. */
1064 ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN);
1065 break;
1066
1067 case PTM_BFD_INIT:
1068 case PTM_BFD_UP:
1069 /* Path is up and working. */
1070 break;
1071
1072 default:
1073 if (bglobal.debug_peer_event)
1074 zlog_debug("state-change: unhandled neighbor state: %d",
1075 nstate);
1076 break;
1077 }
1078 }
1079
1080 void bs_state_handler(struct bfd_session *bs, int nstate)
1081 {
1082 switch (bs->ses_state) {
1083 case PTM_BFD_ADM_DOWN:
1084 bs_admin_down_handler(bs, nstate);
1085 break;
1086 case PTM_BFD_DOWN:
1087 bs_down_handler(bs, nstate);
1088 break;
1089 case PTM_BFD_INIT:
1090 bs_init_handler(bs, nstate);
1091 break;
1092 case PTM_BFD_UP:
1093 bs_up_handler(bs, nstate);
1094 break;
1095
1096 default:
1097 if (bglobal.debug_peer_event)
1098 zlog_debug("state-change: [%s] is in invalid state: %d",
1099 bs_to_string(bs), nstate);
1100 break;
1101 }
1102 }
1103
1104 /*
1105 * Handles echo timer manipulation after updating timer.
1106 */
1107 void bs_echo_timer_handler(struct bfd_session *bs)
1108 {
1109 uint32_t old_timer;
1110
1111 /*
1112 * Before doing any echo handling, check if it is possible to
1113 * use it.
1114 *
1115 * - Check for `echo-mode` configuration.
1116 * - Check that we are not using multi hop (RFC 5883,
1117 * Section 3).
1118 * - Check that we are already at the up state.
1119 */
1120 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0
1121 || CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)
1122 || bs->ses_state != PTM_BFD_UP)
1123 return;
1124
1125 /* Remote peer asked to stop echo. */
1126 if (bs->remote_timers.required_min_echo == 0) {
1127 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE))
1128 ptm_bfd_echo_stop(bs);
1129
1130 return;
1131 }
1132
1133 /*
1134 * Calculate the echo transmission timer: we must not send
1135 * echo packets faster than the minimum required time
1136 * announced by the remote system.
1137 *
1138 * RFC 5880, Section 6.8.9.
1139 */
1140 old_timer = bs->echo_xmt_TO;
1141 if (bs->remote_timers.required_min_echo > bs->timers.desired_min_echo_tx)
1142 bs->echo_xmt_TO = bs->remote_timers.required_min_echo;
1143 else
1144 bs->echo_xmt_TO = bs->timers.desired_min_echo_tx;
1145
1146 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0
1147 || old_timer != bs->echo_xmt_TO)
1148 ptm_bfd_echo_start(bs);
1149 }
1150
1151 /*
1152 * RFC 5880 Section 6.5.
1153 *
1154 * When a BFD control packet with the final bit is received, we must
1155 * update the session parameters.
1156 */
1157 void bs_final_handler(struct bfd_session *bs)
1158 {
1159 /* Start using our new timers. */
1160 bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx;
1161 bs->cur_timers.required_min_rx = bs->timers.required_min_rx;
1162
1163 /*
1164 * TODO: demand mode. See RFC 5880 Section 6.1.
1165 *
1166 * When using demand mode we must disable the detection timer
1167 * for lost control packets.
1168 */
1169 if (bs->demand_mode) {
1170 /* Notify watchers about changed timers. */
1171 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
1172 return;
1173 }
1174
1175 /*
1176 * Calculate transmission time based on new timers.
1177 *
1178 * Transmission calculation:
1179 * Unless specified by exceptions at the end of Section 6.8.7, the
1180 * transmission time will be determined by the system with the
1181 * slowest rate.
1182 *
1183 * RFC 5880, Section 6.8.7.
1184 */
1185 if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx)
1186 bs->xmt_TO = bs->timers.desired_min_tx;
1187 else
1188 bs->xmt_TO = bs->remote_timers.required_min_rx;
1189
1190 /* Apply new transmission timer immediately. */
1191 ptm_bfd_start_xmt_timer(bs, false);
1192
1193 /* Notify watchers about changed timers. */
1194 control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs);
1195 }
1196
1197 void bs_set_slow_timers(struct bfd_session *bs)
1198 {
1199 /*
1200 * BFD connection must use slow timers before going up or after
1201 * losing connectivity to avoid wasting bandwidth.
1202 *
1203 * RFC 5880, Section 6.8.3.
1204 */
1205 bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX;
1206 bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX;
1207 bs->cur_timers.required_min_echo = 0;
1208
1209 /* Set the appropriated timeouts for slow connection. */
1210 bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX);
1211 bs->xmt_TO = BFD_DEF_SLOWTX;
1212 }
1213
1214 void bfd_set_echo(struct bfd_session *bs, bool echo)
1215 {
1216 if (echo) {
1217 /* Check if echo mode is already active. */
1218 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
1219 return;
1220
1221 SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
1222
1223 /* Activate/update echo receive timeout timer. */
1224 if (bs->bdc == NULL)
1225 bs_echo_timer_handler(bs);
1226 } else {
1227 /* Check if echo mode is already disabled. */
1228 if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO))
1229 return;
1230
1231 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO);
1232
1233 /* Deactivate timeout timer. */
1234 if (bs->bdc == NULL)
1235 ptm_bfd_echo_stop(bs);
1236 }
1237 }
1238
1239 void bfd_set_shutdown(struct bfd_session *bs, bool shutdown)
1240 {
1241 bool is_shutdown;
1242
1243 /*
1244 * Special case: we are batching changes and the previous state was
1245 * not shutdown. Instead of potentially disconnect a running peer,
1246 * we'll get the current status to validate we were really down.
1247 */
1248 if (bs->ses_state == PTM_BFD_UP)
1249 is_shutdown = false;
1250 else
1251 is_shutdown = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1252
1253 if (shutdown) {
1254 /* Already shutdown. */
1255 if (is_shutdown)
1256 return;
1257
1258 SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1259
1260 /* Handle data plane shutdown case. */
1261 if (bs->bdc) {
1262 bs->ses_state = PTM_BFD_ADM_DOWN;
1263 bfd_dplane_update_session(bs);
1264 control_notify(bs, bs->ses_state);
1265 return;
1266 }
1267
1268 /* Disable all events. */
1269 bfd_recvtimer_delete(bs);
1270 bfd_echo_recvtimer_delete(bs);
1271 bfd_xmttimer_delete(bs);
1272 bfd_echo_xmttimer_delete(bs);
1273
1274 /* Change and notify state change. */
1275 bs->ses_state = PTM_BFD_ADM_DOWN;
1276 control_notify(bs, bs->ses_state);
1277
1278 /* Don't try to send packets with a disabled session. */
1279 if (bs->sock != -1)
1280 ptm_bfd_snd(bs, 0);
1281 } else {
1282 /* Already working. */
1283 if (!is_shutdown)
1284 return;
1285
1286 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN);
1287
1288 /* Handle data plane shutdown case. */
1289 if (bs->bdc) {
1290 bs->ses_state = PTM_BFD_DOWN;
1291 bfd_dplane_update_session(bs);
1292 control_notify(bs, bs->ses_state);
1293 return;
1294 }
1295
1296 /* Change and notify state change. */
1297 bs->ses_state = PTM_BFD_DOWN;
1298 control_notify(bs, bs->ses_state);
1299
1300 /* Enable timers if non passive, otherwise stop them. */
1301 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE)) {
1302 bfd_recvtimer_delete(bs);
1303 bfd_xmttimer_delete(bs);
1304 } else {
1305 bfd_recvtimer_update(bs);
1306 bfd_xmttimer_update(bs, bs->xmt_TO);
1307 }
1308 }
1309 }
1310
1311 void bfd_set_passive_mode(struct bfd_session *bs, bool passive)
1312 {
1313 if (passive) {
1314 SET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE);
1315
1316 /* Session is already up and running, nothing to do now. */
1317 if (bs->ses_state != PTM_BFD_DOWN)
1318 return;
1319
1320 /* Lets disable the timers since we are now passive. */
1321 bfd_recvtimer_delete(bs);
1322 bfd_xmttimer_delete(bs);
1323 } else {
1324 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE);
1325
1326 /* Session is already up and running, nothing to do now. */
1327 if (bs->ses_state != PTM_BFD_DOWN)
1328 return;
1329
1330 /* Session is down, let it attempt to start the connection. */
1331 bfd_xmttimer_update(bs, bs->xmt_TO);
1332 bfd_recvtimer_update(bs);
1333 }
1334 }
1335
1336 /*
1337 * Helper functions.
1338 */
1339 static const char *get_diag_str(int diag)
1340 {
1341 for (int i = 0; diag_list[i].str; i++) {
1342 if (diag_list[i].type == diag)
1343 return diag_list[i].str;
1344 }
1345 return "N/A";
1346 }
1347
1348 const char *satostr(const struct sockaddr_any *sa)
1349 {
1350 #define INETSTR_BUFCOUNT 8
1351 static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN];
1352 static int bufidx;
1353 const struct sockaddr_in *sin = &sa->sa_sin;
1354 const struct sockaddr_in6 *sin6 = &sa->sa_sin6;
1355
1356 bufidx += (bufidx + 1) % INETSTR_BUFCOUNT;
1357 buf[bufidx][0] = 0;
1358
1359 switch (sin->sin_family) {
1360 case AF_INET:
1361 inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx],
1362 sizeof(buf[bufidx]));
1363 break;
1364 case AF_INET6:
1365 inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx],
1366 sizeof(buf[bufidx]));
1367 break;
1368
1369 default:
1370 strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx]));
1371 break;
1372 }
1373
1374 return buf[bufidx];
1375 }
1376
1377 const char *diag2str(uint8_t diag)
1378 {
1379 switch (diag) {
1380 case 0:
1381 return "ok";
1382 case 1:
1383 return "control detection time expired";
1384 case 2:
1385 return "echo function failed";
1386 case 3:
1387 return "neighbor signaled session down";
1388 case 4:
1389 return "forwarding plane reset";
1390 case 5:
1391 return "path down";
1392 case 6:
1393 return "concatenated path down";
1394 case 7:
1395 return "administratively down";
1396 case 8:
1397 return "reverse concatenated path down";
1398 default:
1399 return "unknown";
1400 }
1401 }
1402
1403 int strtosa(const char *addr, struct sockaddr_any *sa)
1404 {
1405 memset(sa, 0, sizeof(*sa));
1406
1407 if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) {
1408 sa->sa_sin.sin_family = AF_INET;
1409 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1410 sa->sa_sin.sin_len = sizeof(sa->sa_sin);
1411 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1412 return 0;
1413 }
1414
1415 if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) {
1416 sa->sa_sin6.sin6_family = AF_INET6;
1417 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1418 sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6);
1419 #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
1420 return 0;
1421 }
1422
1423 return -1;
1424 }
1425
1426 void integer2timestr(uint64_t time, char *buf, size_t buflen)
1427 {
1428 uint64_t year, month, day, hour, minute, second;
1429 int rv;
1430
1431 #define MINUTES (60)
1432 #define HOURS (60 * MINUTES)
1433 #define DAYS (24 * HOURS)
1434 #define MONTHS (30 * DAYS)
1435 #define YEARS (12 * MONTHS)
1436 if (time >= YEARS) {
1437 year = time / YEARS;
1438 time -= year * YEARS;
1439
1440 rv = snprintfrr(buf, buflen, "%" PRIu64 " year(s), ", year);
1441 buf += rv;
1442 buflen -= rv;
1443 }
1444 if (time >= MONTHS) {
1445 month = time / MONTHS;
1446 time -= month * MONTHS;
1447
1448 rv = snprintfrr(buf, buflen, "%" PRIu64 " month(s), ", month);
1449 buf += rv;
1450 buflen -= rv;
1451 }
1452 if (time >= DAYS) {
1453 day = time / DAYS;
1454 time -= day * DAYS;
1455
1456 rv = snprintfrr(buf, buflen, "%" PRIu64 " day(s), ", day);
1457 buf += rv;
1458 buflen -= rv;
1459 }
1460 if (time >= HOURS) {
1461 hour = time / HOURS;
1462 time -= hour * HOURS;
1463
1464 rv = snprintfrr(buf, buflen, "%" PRIu64 " hour(s), ", hour);
1465 buf += rv;
1466 buflen -= rv;
1467 }
1468 if (time >= MINUTES) {
1469 minute = time / MINUTES;
1470 time -= minute * MINUTES;
1471
1472 rv = snprintfrr(buf, buflen, "%" PRIu64 " minute(s), ", minute);
1473 buf += rv;
1474 buflen -= rv;
1475 }
1476 second = time % MINUTES;
1477 snprintfrr(buf, buflen, "%" PRIu64 " second(s)", second);
1478 }
1479
1480 const char *bs_to_string(const struct bfd_session *bs)
1481 {
1482 static char buf[256];
1483 char addr_buf[INET6_ADDRSTRLEN];
1484 int pos;
1485 bool is_mhop = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH);
1486
1487 pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no");
1488 pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s",
1489 inet_ntop(bs->key.family, &bs->key.peer, addr_buf,
1490 sizeof(addr_buf)));
1491 pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s",
1492 inet_ntop(bs->key.family, &bs->key.local, addr_buf,
1493 sizeof(addr_buf)));
1494 if (bs->key.vrfname[0])
1495 pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s",
1496 bs->key.vrfname);
1497 if (bs->key.ifname[0])
1498 pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s",
1499 bs->key.ifname);
1500
1501 (void)pos;
1502
1503 return buf;
1504 }
1505
1506 int bs_observer_add(struct bfd_session *bs)
1507 {
1508 struct bfd_session_observer *bso;
1509
1510 bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso));
1511 bso->bso_bs = bs;
1512 bso->bso_addr.family = bs->key.family;
1513 memcpy(&bso->bso_addr.u.prefix, &bs->key.local,
1514 sizeof(bs->key.local));
1515
1516 TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry);
1517
1518 return 0;
1519 }
1520
1521 void bs_observer_del(struct bfd_session_observer *bso)
1522 {
1523 TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry);
1524 XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso);
1525 }
1526
1527 void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc)
1528 {
1529 memset(bpc, 0, sizeof(*bpc));
1530
1531 bpc->bpc_ipv4 = (bs->key.family == AF_INET);
1532 bpc->bpc_mhop = bs->key.mhop;
1533
1534 switch (bs->key.family) {
1535 case AF_INET:
1536 bpc->bpc_peer.sa_sin.sin_family = AF_INET;
1537 memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer,
1538 sizeof(bpc->bpc_peer.sa_sin.sin_addr));
1539
1540 if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) {
1541 bpc->bpc_local.sa_sin.sin_family = AF_INET6;
1542 memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local,
1543 sizeof(bpc->bpc_local.sa_sin.sin_addr));
1544 }
1545 break;
1546
1547 case AF_INET6:
1548 bpc->bpc_peer.sa_sin.sin_family = AF_INET6;
1549 memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer,
1550 sizeof(bpc->bpc_peer.sa_sin6.sin6_addr));
1551
1552 bpc->bpc_local.sa_sin6.sin6_family = AF_INET6;
1553 memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local,
1554 sizeof(bpc->bpc_local.sa_sin6.sin6_addr));
1555 break;
1556 }
1557
1558 if (bs->key.ifname[0]) {
1559 bpc->bpc_has_localif = true;
1560 strlcpy(bpc->bpc_localif, bs->key.ifname,
1561 sizeof(bpc->bpc_localif));
1562 }
1563
1564 if (bs->key.vrfname[0]) {
1565 bpc->bpc_has_vrfname = true;
1566 strlcpy(bpc->bpc_vrfname, bs->key.vrfname,
1567 sizeof(bpc->bpc_vrfname));
1568 }
1569 }
1570
1571
1572 /*
1573 * BFD hash data structures to find sessions.
1574 */
1575 static struct hash *bfd_id_hash;
1576 static struct hash *bfd_key_hash;
1577
1578 static unsigned int bfd_id_hash_do(const void *p);
1579 static unsigned int bfd_key_hash_do(const void *p);
1580
1581 static void _bfd_free(struct hash_bucket *hb,
1582 void *arg __attribute__((__unused__)));
1583
1584 /* BFD hash for our discriminator. */
1585 static unsigned int bfd_id_hash_do(const void *p)
1586 {
1587 const struct bfd_session *bs = p;
1588
1589 return jhash_1word(bs->discrs.my_discr, 0);
1590 }
1591
1592 static bool bfd_id_hash_cmp(const void *n1, const void *n2)
1593 {
1594 const struct bfd_session *bs1 = n1, *bs2 = n2;
1595
1596 return bs1->discrs.my_discr == bs2->discrs.my_discr;
1597 }
1598
1599 /* BFD hash for single hop. */
1600 static unsigned int bfd_key_hash_do(const void *p)
1601 {
1602 const struct bfd_session *bs = p;
1603 struct bfd_key key = bs->key;
1604
1605 /*
1606 * Local address and interface name are optional and
1607 * can be filled any time after session creation.
1608 * Hash key should not depend on these fields.
1609 */
1610 memset(&key.local, 0, sizeof(key.local));
1611 memset(key.ifname, 0, sizeof(key.ifname));
1612
1613 return jhash(&key, sizeof(key), 0);
1614 }
1615
1616 static bool bfd_key_hash_cmp(const void *n1, const void *n2)
1617 {
1618 const struct bfd_session *bs1 = n1, *bs2 = n2;
1619
1620 if (bs1->key.family != bs2->key.family)
1621 return false;
1622 if (bs1->key.mhop != bs2->key.mhop)
1623 return false;
1624 if (memcmp(&bs1->key.peer, &bs2->key.peer, sizeof(bs1->key.peer)))
1625 return false;
1626 if (memcmp(bs1->key.vrfname, bs2->key.vrfname,
1627 sizeof(bs1->key.vrfname)))
1628 return false;
1629
1630 /*
1631 * Local address is optional and can be empty.
1632 * If both addresses are not empty and different,
1633 * then the keys are different.
1634 */
1635 if (memcmp(&bs1->key.local, &zero_addr, sizeof(bs1->key.local))
1636 && memcmp(&bs2->key.local, &zero_addr, sizeof(bs2->key.local))
1637 && memcmp(&bs1->key.local, &bs2->key.local, sizeof(bs1->key.local)))
1638 return false;
1639
1640 /*
1641 * Interface name is optional and can be empty.
1642 * If both names are not empty and different,
1643 * then the keys are different.
1644 */
1645 if (bs1->key.ifname[0] && bs2->key.ifname[0]
1646 && memcmp(bs1->key.ifname, bs2->key.ifname,
1647 sizeof(bs1->key.ifname)))
1648 return false;
1649
1650 return true;
1651 }
1652
1653
1654 /*
1655 * Hash public interface / exported functions.
1656 */
1657
1658 /* Lookup functions. */
1659 struct bfd_session *bfd_id_lookup(uint32_t id)
1660 {
1661 struct bfd_session bs;
1662
1663 bs.discrs.my_discr = id;
1664
1665 return hash_lookup(bfd_id_hash, &bs);
1666 }
1667
1668 struct bfd_session *bfd_key_lookup(struct bfd_key key)
1669 {
1670 struct bfd_session bs;
1671
1672 bs.key = key;
1673
1674 return hash_lookup(bfd_key_hash, &bs);
1675 }
1676
1677 /*
1678 * Delete functions.
1679 *
1680 * Delete functions searches and remove the item from the hash and
1681 * returns a pointer to the removed item data. If the item was not found
1682 * then it returns NULL.
1683 *
1684 * The data stored inside the hash is not free()ed, so you must do it
1685 * manually after getting the pointer back.
1686 */
1687 struct bfd_session *bfd_id_delete(uint32_t id)
1688 {
1689 struct bfd_session bs;
1690
1691 bs.discrs.my_discr = id;
1692
1693 return hash_release(bfd_id_hash, &bs);
1694 }
1695
1696 struct bfd_session *bfd_key_delete(struct bfd_key key)
1697 {
1698 struct bfd_session bs;
1699
1700 bs.key = key;
1701
1702 return hash_release(bfd_key_hash, &bs);
1703 }
1704
1705 /* Iteration functions. */
1706 void bfd_id_iterate(hash_iter_func hif, void *arg)
1707 {
1708 hash_iterate(bfd_id_hash, hif, arg);
1709 }
1710
1711 void bfd_key_iterate(hash_iter_func hif, void *arg)
1712 {
1713 hash_iterate(bfd_key_hash, hif, arg);
1714 }
1715
1716 /*
1717 * Insert functions.
1718 *
1719 * Inserts session into hash and returns `true` on success, otherwise
1720 * `false`.
1721 */
1722 bool bfd_id_insert(struct bfd_session *bs)
1723 {
1724 return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs);
1725 }
1726
1727 bool bfd_key_insert(struct bfd_session *bs)
1728 {
1729 return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs);
1730 }
1731
1732 void bfd_initialize(void)
1733 {
1734 bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp,
1735 "BFD session discriminator hash");
1736 bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp,
1737 "BFD session hash");
1738 TAILQ_INIT(&bplist);
1739 }
1740
1741 static void _bfd_free(struct hash_bucket *hb,
1742 void *arg __attribute__((__unused__)))
1743 {
1744 struct bfd_session *bs = hb->data;
1745
1746 bfd_session_free(bs);
1747 }
1748
1749 void bfd_shutdown(void)
1750 {
1751 struct bfd_profile *bp;
1752
1753 /*
1754 * Close and free all BFD sessions.
1755 *
1756 * _bfd_free() will call bfd_session_free() which will take care
1757 * of removing the session from all hashes, so we just run an
1758 * assert() here to make sure it really happened.
1759 */
1760 bfd_id_iterate(_bfd_free, NULL);
1761 assert(bfd_key_hash->count == 0);
1762
1763 /* Now free the hashes themselves. */
1764 hash_free(bfd_id_hash);
1765 hash_free(bfd_key_hash);
1766
1767 /* Free all profile allocations. */
1768 while ((bp = TAILQ_FIRST(&bplist)) != NULL)
1769 bfd_profile_free(bp);
1770 }
1771
1772 struct bfd_session_iterator {
1773 int bsi_stop;
1774 bool bsi_mhop;
1775 const struct bfd_session *bsi_bs;
1776 };
1777
1778 static int _bfd_session_next(struct hash_bucket *hb, void *arg)
1779 {
1780 struct bfd_session_iterator *bsi = arg;
1781 struct bfd_session *bs = hb->data;
1782
1783 /* Previous entry signaled stop. */
1784 if (bsi->bsi_stop == 1) {
1785 /* Match the single/multi hop sessions. */
1786 if (bs->key.mhop != bsi->bsi_mhop)
1787 return HASHWALK_CONTINUE;
1788
1789 bsi->bsi_bs = bs;
1790 return HASHWALK_ABORT;
1791 }
1792
1793 /* We found the current item, stop in the next one. */
1794 if (bsi->bsi_bs == hb->data) {
1795 bsi->bsi_stop = 1;
1796 /* Set entry to NULL to signal end of list. */
1797 bsi->bsi_bs = NULL;
1798 } else if (bsi->bsi_bs == NULL && bsi->bsi_mhop == bs->key.mhop) {
1799 /* We want the first list item. */
1800 bsi->bsi_stop = 1;
1801 bsi->bsi_bs = hb->data;
1802 return HASHWALK_ABORT;
1803 }
1804
1805 return HASHWALK_CONTINUE;
1806 }
1807
1808 /*
1809 * bfd_session_next: uses the current session to find the next.
1810 *
1811 * `bs` might point to NULL to get the first item of the data structure.
1812 */
1813 const struct bfd_session *bfd_session_next(const struct bfd_session *bs,
1814 bool mhop)
1815 {
1816 struct bfd_session_iterator bsi;
1817
1818 bsi.bsi_stop = 0;
1819 bsi.bsi_bs = bs;
1820 bsi.bsi_mhop = mhop;
1821 hash_walk(bfd_key_hash, _bfd_session_next, &bsi);
1822 if (bsi.bsi_stop == 0)
1823 return NULL;
1824
1825 return bsi.bsi_bs;
1826 }
1827
1828 static void _bfd_session_remove_manual(struct hash_bucket *hb,
1829 void *arg __attribute__((__unused__)))
1830 {
1831 struct bfd_session *bs = hb->data;
1832
1833 /* Delete only manually configured sessions. */
1834 if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG) == 0)
1835 return;
1836
1837 bs->refcount--;
1838 UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG);
1839
1840 /* Don't delete sessions still in use. */
1841 if (bs->refcount != 0)
1842 return;
1843
1844 bfd_session_free(bs);
1845 }
1846
1847 /*
1848 * bfd_sessions_remove_manual: remove all manually configured sessions.
1849 *
1850 * NOTE: this function doesn't remove automatically created sessions.
1851 */
1852 void bfd_sessions_remove_manual(void)
1853 {
1854 hash_iterate(bfd_key_hash, _bfd_session_remove_manual, NULL);
1855 }
1856
1857 void bfd_profiles_remove(void)
1858 {
1859 struct bfd_profile *bp;
1860
1861 while ((bp = TAILQ_FIRST(&bplist)) != NULL)
1862 bfd_profile_free(bp);
1863 }
1864
1865 /*
1866 * Profile related hash functions.
1867 */
1868 static void _bfd_profile_update(struct hash_bucket *hb, void *arg)
1869 {
1870 struct bfd_profile *bp = arg;
1871 struct bfd_session *bs = hb->data;
1872
1873 /* This session is not using the profile. */
1874 if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0)
1875 return;
1876
1877 bfd_profile_apply(bp->name, bs);
1878 }
1879
1880 void bfd_profile_update(struct bfd_profile *bp)
1881 {
1882 hash_iterate(bfd_key_hash, _bfd_profile_update, bp);
1883 }
1884
1885 static void _bfd_profile_detach(struct hash_bucket *hb, void *arg)
1886 {
1887 struct bfd_profile *bp = arg;
1888 struct bfd_session *bs = hb->data;
1889
1890 /* This session is not using the profile. */
1891 if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0)
1892 return;
1893
1894 bfd_profile_remove(bs);
1895 }
1896
1897 static void bfd_profile_detach(struct bfd_profile *bp)
1898 {
1899 hash_iterate(bfd_key_hash, _bfd_profile_detach, bp);
1900 }
1901
1902 /*
1903 * VRF related functions.
1904 */
1905 static int bfd_vrf_new(struct vrf *vrf)
1906 {
1907 if (bglobal.debug_zebra)
1908 zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id);
1909
1910 return 0;
1911 }
1912
1913 static int bfd_vrf_delete(struct vrf *vrf)
1914 {
1915 if (bglobal.debug_zebra)
1916 zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id);
1917
1918 return 0;
1919 }
1920
1921 static int bfd_vrf_enable(struct vrf *vrf)
1922 {
1923 struct bfd_vrf_global *bvrf;
1924
1925 /* a different name */
1926 if (!vrf->info) {
1927 bvrf = XCALLOC(MTYPE_BFDD_VRF, sizeof(struct bfd_vrf_global));
1928 bvrf->vrf = vrf;
1929 vrf->info = (void *)bvrf;
1930
1931 /* Disable sockets if using data plane. */
1932 if (bglobal.bg_use_dplane) {
1933 bvrf->bg_shop = -1;
1934 bvrf->bg_mhop = -1;
1935 bvrf->bg_shop6 = -1;
1936 bvrf->bg_mhop6 = -1;
1937 bvrf->bg_echo = -1;
1938 bvrf->bg_echov6 = -1;
1939 }
1940 } else
1941 bvrf = vrf->info;
1942
1943 if (bglobal.debug_zebra)
1944 zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id);
1945
1946 if (!bvrf->bg_shop)
1947 bvrf->bg_shop = bp_udp_shop(vrf);
1948 if (!bvrf->bg_mhop)
1949 bvrf->bg_mhop = bp_udp_mhop(vrf);
1950 if (!bvrf->bg_shop6)
1951 bvrf->bg_shop6 = bp_udp6_shop(vrf);
1952 if (!bvrf->bg_mhop6)
1953 bvrf->bg_mhop6 = bp_udp6_mhop(vrf);
1954 if (!bvrf->bg_echo)
1955 bvrf->bg_echo = bp_echo_socket(vrf);
1956 if (!bvrf->bg_echov6)
1957 bvrf->bg_echov6 = bp_echov6_socket(vrf);
1958
1959 if (!bvrf->bg_ev[0] && bvrf->bg_shop != -1)
1960 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop,
1961 &bvrf->bg_ev[0]);
1962 if (!bvrf->bg_ev[1] && bvrf->bg_mhop != -1)
1963 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop,
1964 &bvrf->bg_ev[1]);
1965 if (!bvrf->bg_ev[2] && bvrf->bg_shop6 != -1)
1966 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_shop6,
1967 &bvrf->bg_ev[2]);
1968 if (!bvrf->bg_ev[3] && bvrf->bg_mhop6 != -1)
1969 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_mhop6,
1970 &bvrf->bg_ev[3]);
1971 if (!bvrf->bg_ev[4] && bvrf->bg_echo != -1)
1972 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echo,
1973 &bvrf->bg_ev[4]);
1974 if (!bvrf->bg_ev[5] && bvrf->bg_echov6 != -1)
1975 thread_add_read(master, bfd_recv_cb, bvrf, bvrf->bg_echov6,
1976 &bvrf->bg_ev[5]);
1977
1978 if (vrf->vrf_id != VRF_DEFAULT) {
1979 bfdd_zclient_register(vrf->vrf_id);
1980 bfdd_sessions_enable_vrf(vrf);
1981 }
1982 return 0;
1983 }
1984
1985 static int bfd_vrf_disable(struct vrf *vrf)
1986 {
1987 struct bfd_vrf_global *bvrf;
1988
1989 if (!vrf->info)
1990 return 0;
1991 bvrf = vrf->info;
1992
1993 if (vrf->vrf_id != VRF_DEFAULT) {
1994 bfdd_sessions_disable_vrf(vrf);
1995 bfdd_zclient_unregister(vrf->vrf_id);
1996 }
1997
1998 if (bglobal.debug_zebra)
1999 zlog_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id);
2000
2001 /* Disable read/write poll triggering. */
2002 THREAD_OFF(bvrf->bg_ev[0]);
2003 THREAD_OFF(bvrf->bg_ev[1]);
2004 THREAD_OFF(bvrf->bg_ev[2]);
2005 THREAD_OFF(bvrf->bg_ev[3]);
2006 THREAD_OFF(bvrf->bg_ev[4]);
2007 THREAD_OFF(bvrf->bg_ev[5]);
2008
2009 /* Close all descriptors. */
2010 socket_close(&bvrf->bg_echo);
2011 socket_close(&bvrf->bg_shop);
2012 socket_close(&bvrf->bg_mhop);
2013 if (bvrf->bg_shop6 != -1)
2014 socket_close(&bvrf->bg_shop6);
2015 if (bvrf->bg_mhop6 != -1)
2016 socket_close(&bvrf->bg_mhop6);
2017 socket_close(&bvrf->bg_echo);
2018 if (bvrf->bg_echov6 != -1)
2019 socket_close(&bvrf->bg_echov6);
2020
2021 /* free context */
2022 XFREE(MTYPE_BFDD_VRF, bvrf);
2023 vrf->info = NULL;
2024
2025 return 0;
2026 }
2027
2028 void bfd_vrf_init(void)
2029 {
2030 vrf_init(bfd_vrf_new, bfd_vrf_enable, bfd_vrf_disable, bfd_vrf_delete);
2031 }
2032
2033 void bfd_vrf_terminate(void)
2034 {
2035 vrf_terminate();
2036 }
2037
2038 struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd)
2039 {
2040 struct vrf *vrf;
2041
2042 if (!vrf_is_backend_netns()) {
2043 vrf = vrf_lookup_by_id(VRF_DEFAULT);
2044 if (vrf)
2045 return (struct bfd_vrf_global *)vrf->info;
2046 return NULL;
2047 }
2048 if (!bfd)
2049 return NULL;
2050 if (!bfd->vrf)
2051 return NULL;
2052 return bfd->vrf->info;
2053 }
2054
2055 unsigned long bfd_get_session_count(void)
2056 {
2057 return bfd_key_hash->count;
2058 }
2059
2060 void bfd_rtt_init(struct bfd_session *bfd)
2061 {
2062 uint8_t i;
2063
2064 /* initialize RTT */
2065 bfd->rtt_valid = 0;
2066 bfd->rtt_index = 0;
2067 for (i = 0; i < BFD_RTT_SAMPLE; i++)
2068 bfd->rtt[i] = 0;
2069 }