]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
tests: Add test consecutive frrscript_call
[mirror_frr.git] / lib / bfd.c
1 /**
2 * bfd.c: BFD handling routines
3 *
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24
25 #include "command.h"
26 #include "memory.h"
27 #include "prefix.h"
28 #include "thread.h"
29 #include "stream.h"
30 #include "vrf.h"
31 #include "zclient.h"
32 #include "table.h"
33 #include "vty.h"
34 #include "bfd.h"
35
36 DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info");
37
38 /**
39 * BFD protocol integration configuration.
40 */
41
42 /** Events definitions. */
43 enum bfd_session_event {
44 /** Remove the BFD session configuration. */
45 BSE_UNINSTALL,
46 /** Install the BFD session configuration. */
47 BSE_INSTALL,
48 };
49
50 /**
51 * Data structure to do the necessary tricks to hide the BFD protocol
52 * integration internals.
53 */
54 struct bfd_session_params {
55 /** Contains the session parameters and more. */
56 struct bfd_session_arg args;
57 /** Contains the session state. */
58 struct bfd_session_status bss;
59 /** Protocol implementation status update callback. */
60 bsp_status_update updatecb;
61 /** Protocol implementation custom data pointer. */
62 void *arg;
63
64 /**
65 * Next event.
66 *
67 * This variable controls what action to execute when the command batch
68 * finishes. Normally we'd use `thread_add_event` value, however since
69 * that function is going to be called multiple times and the value
70 * might be different we'll use this variable to keep track of it.
71 */
72 enum bfd_session_event lastev;
73 /**
74 * BFD session configuration event.
75 *
76 * Multiple actions might be asked during a command batch (either via
77 * configuration load or northbound batch), so we'll use this to
78 * install/uninstall the BFD session parameters only once.
79 */
80 struct thread *installev;
81
82 /** BFD session installation state. */
83 bool installed;
84
85 /** Global BFD paramaters list. */
86 TAILQ_ENTRY(bfd_session_params) entry;
87 };
88
89 struct bfd_sessions_global {
90 /**
91 * Global BFD session parameters list for (re)installation and update
92 * without code duplication among daemons.
93 */
94 TAILQ_HEAD(bsplist, bfd_session_params) bsplist;
95
96 /** Pointer to FRR's event manager. */
97 struct thread_master *tm;
98 /** Pointer to zebra client data structure. */
99 struct zclient *zc;
100
101 /** Debugging state. */
102 bool debugging;
103 /** Is shutting down? */
104 bool shutting_down;
105 };
106
107 /** Global configuration variable. */
108 static struct bfd_sessions_global bsglobal;
109
110 /** Global empty address for IPv4/IPv6. */
111 static const struct in6_addr i6a_zero;
112
113 /*
114 * bfd_get_peer_info - Extract the Peer information for which the BFD session
115 * went down from the message sent from Zebra to clients.
116 */
117 static struct interface *bfd_get_peer_info(struct stream *s, struct prefix *dp,
118 struct prefix *sp, int *status,
119 int *remote_cbit, vrf_id_t vrf_id)
120 {
121 unsigned int ifindex;
122 struct interface *ifp = NULL;
123 int plen;
124 int local_remote_cbit;
125
126 /*
127 * If the ifindex lookup fails the
128 * rest of the data in the stream is
129 * not read. All examples of this function
130 * call immediately use the dp->family which
131 * is not good. Ensure we are not using
132 * random data
133 */
134 memset(dp, 0, sizeof(*dp));
135 memset(sp, 0, sizeof(*sp));
136
137 /* Get interface index. */
138 STREAM_GETL(s, ifindex);
139
140 /* Lookup index. */
141 if (ifindex != 0) {
142 ifp = if_lookup_by_index(ifindex, vrf_id);
143 if (ifp == NULL) {
144 if (bsglobal.debugging)
145 zlog_debug(
146 "zebra_interface_bfd_read: Can't find interface by ifindex: %d ",
147 ifindex);
148 return NULL;
149 }
150 }
151
152 /* Fetch destination address. */
153 STREAM_GETC(s, dp->family);
154
155 plen = prefix_blen(dp);
156 STREAM_GET(&dp->u.prefix, s, plen);
157 STREAM_GETC(s, dp->prefixlen);
158
159 /* Get BFD status. */
160 STREAM_GETL(s, (*status));
161
162 STREAM_GETC(s, sp->family);
163
164 plen = prefix_blen(sp);
165 STREAM_GET(&sp->u.prefix, s, plen);
166 STREAM_GETC(s, sp->prefixlen);
167
168 STREAM_GETC(s, local_remote_cbit);
169 if (remote_cbit)
170 *remote_cbit = local_remote_cbit;
171 return ifp;
172
173 stream_failure:
174 return NULL;
175 }
176
177 /*
178 * bfd_get_status_str - Convert BFD status to a display string.
179 */
180 const char *bfd_get_status_str(int status)
181 {
182 switch (status) {
183 case BFD_STATUS_DOWN:
184 return "Down";
185 case BFD_STATUS_UP:
186 return "Up";
187 case BFD_STATUS_ADMIN_DOWN:
188 return "Admin Down";
189 case BFD_STATUS_UNKNOWN:
190 default:
191 return "Unknown";
192 }
193 }
194
195 /*
196 * bfd_last_update - Calculate the last BFD update time and convert it
197 * into a dd:hh:mm:ss display format.
198 */
199 static void bfd_last_update(time_t last_update, char *buf, size_t len)
200 {
201 time_t curr;
202 time_t diff;
203 struct tm tm;
204 struct timeval tv;
205
206 /* If no BFD satatus update has ever been received, print `never'. */
207 if (last_update == 0) {
208 snprintf(buf, len, "never");
209 return;
210 }
211
212 /* Get current time. */
213 monotime(&tv);
214 curr = tv.tv_sec;
215 diff = curr - last_update;
216 gmtime_r(&diff, &tm);
217
218 snprintf(buf, len, "%d:%02d:%02d:%02d", tm.tm_yday, tm.tm_hour,
219 tm.tm_min, tm.tm_sec);
220 }
221
222 /*
223 * bfd_client_sendmsg - Format and send a client register
224 * command to Zebra to be forwarded to BFD
225 */
226 void bfd_client_sendmsg(struct zclient *zclient, int command,
227 vrf_id_t vrf_id)
228 {
229 struct stream *s;
230 enum zclient_send_status ret;
231
232 /* Check socket. */
233 if (!zclient || zclient->sock < 0) {
234 if (bsglobal.debugging)
235 zlog_debug(
236 "%s: Can't send BFD client register, Zebra client not established",
237 __func__);
238 return;
239 }
240
241 s = zclient->obuf;
242 stream_reset(s);
243 zclient_create_header(s, command, vrf_id);
244
245 stream_putl(s, getpid());
246
247 stream_putw_at(s, 0, stream_get_endp(s));
248
249 ret = zclient_send_message(zclient);
250
251 if (ret == ZCLIENT_SEND_FAILURE) {
252 if (bsglobal.debugging)
253 zlog_debug(
254 "bfd_client_sendmsg %ld: zclient_send_message() failed",
255 (long)getpid());
256 return;
257 }
258
259 return;
260 }
261
262 int zclient_bfd_command(struct zclient *zc, struct bfd_session_arg *args)
263 {
264 struct stream *s;
265 size_t addrlen;
266
267 /* Individual reg/dereg messages are suppressed during shutdown. */
268 if (bsglobal.shutting_down) {
269 if (bsglobal.debugging)
270 zlog_debug(
271 "%s: Suppressing BFD peer reg/dereg messages",
272 __func__);
273 return -1;
274 }
275
276 /* Check socket. */
277 if (!zc || zc->sock < 0) {
278 if (bsglobal.debugging)
279 zlog_debug("%s: zclient unavailable", __func__);
280 return -1;
281 }
282
283 s = zc->obuf;
284 stream_reset(s);
285
286 /* Create new message. */
287 zclient_create_header(s, args->command, args->vrf_id);
288 stream_putl(s, getpid());
289
290 /* Encode destination address. */
291 stream_putw(s, args->family);
292 addrlen = (args->family == AF_INET) ? sizeof(struct in_addr)
293 : sizeof(struct in6_addr);
294 stream_put(s, &args->dst, addrlen);
295
296 /*
297 * For more BFD integration protocol details, see function
298 * `_ptm_msg_read` in `bfdd/ptm_adapter.c`.
299 */
300 #if HAVE_BFDD > 0
301 /* Session timers. */
302 stream_putl(s, args->min_rx);
303 stream_putl(s, args->min_tx);
304 stream_putc(s, args->detection_multiplier);
305
306 /* Is multi hop? */
307 stream_putc(s, args->mhop != 0);
308
309 /* Source address. */
310 stream_putw(s, args->family);
311 stream_put(s, &args->src, addrlen);
312
313 /* Send the expected TTL. */
314 stream_putc(s, args->ttl);
315
316 /* Send interface name if any. */
317 if (args->mhop) {
318 /* Don't send interface. */
319 stream_putc(s, 0);
320 if (bsglobal.debugging && args->ifnamelen)
321 zlog_debug("%s: multi hop is configured, not sending interface",
322 __func__);
323 } else {
324 stream_putc(s, args->ifnamelen);
325 if (args->ifnamelen)
326 stream_put(s, args->ifname, args->ifnamelen);
327 }
328
329 /* Send the C bit indicator. */
330 stream_putc(s, args->cbit);
331
332 /* Send profile name if any. */
333 stream_putc(s, args->profilelen);
334 if (args->profilelen)
335 stream_put(s, args->profile, args->profilelen);
336 #else /* PTM BFD */
337 /* Encode timers if this is a registration message. */
338 if (args->command != ZEBRA_BFD_DEST_DEREGISTER) {
339 stream_putl(s, args->min_rx);
340 stream_putl(s, args->min_tx);
341 stream_putc(s, args->detection_multiplier);
342 }
343
344 if (args->mhop) {
345 /* Multi hop indicator. */
346 stream_putc(s, 1);
347
348 /* Multi hop always sends the source address. */
349 stream_putw(s, args->family);
350 stream_put(s, &args->src, addrlen);
351
352 /* Send the expected TTL. */
353 stream_putc(s, args->ttl);
354 } else {
355 /* Multi hop indicator. */
356 stream_putc(s, 0);
357
358 /* Single hop only sends the source address when IPv6. */
359 if (args->family == AF_INET6) {
360 stream_putw(s, args->family);
361 stream_put(s, &args->src, addrlen);
362 }
363
364 /* Send interface name if any. */
365 stream_putc(s, args->ifnamelen);
366 if (args->ifnamelen)
367 stream_put(s, args->ifname, args->ifnamelen);
368 }
369 #endif /* HAVE_BFDD */
370
371 /* Finish the message by writing the size. */
372 stream_putw_at(s, 0, stream_get_endp(s));
373
374 /* Send message to zebra. */
375 if (zclient_send_message(zc) == ZCLIENT_SEND_FAILURE) {
376 if (bsglobal.debugging)
377 zlog_debug("%s: zclient_send_message failed", __func__);
378 return -1;
379 }
380
381 return 0;
382 }
383
384 struct bfd_session_params *bfd_sess_new(bsp_status_update updatecb, void *arg)
385 {
386 struct bfd_session_params *bsp;
387
388 bsp = XCALLOC(MTYPE_BFD_INFO, sizeof(*bsp));
389
390 /* Save application data. */
391 bsp->updatecb = updatecb;
392 bsp->arg = arg;
393
394 /* Set defaults. */
395 bsp->args.detection_multiplier = BFD_DEF_DETECT_MULT;
396 bsp->args.ttl = 1;
397 bsp->args.min_rx = BFD_DEF_MIN_RX;
398 bsp->args.min_tx = BFD_DEF_MIN_TX;
399 bsp->args.vrf_id = VRF_DEFAULT;
400
401 /* Register in global list. */
402 TAILQ_INSERT_TAIL(&bsglobal.bsplist, bsp, entry);
403
404 return bsp;
405 }
406
407 static bool _bfd_sess_valid(const struct bfd_session_params *bsp)
408 {
409 /* Peer/local address not configured. */
410 if (bsp->args.family == 0)
411 return false;
412
413 /* Address configured but invalid. */
414 if (bsp->args.family != AF_INET && bsp->args.family != AF_INET6) {
415 if (bsglobal.debugging)
416 zlog_debug("%s: invalid session family: %d", __func__,
417 bsp->args.family);
418 return false;
419 }
420
421 /* Invalid address. */
422 if (memcmp(&bsp->args.dst, &i6a_zero, sizeof(i6a_zero)) == 0) {
423 if (bsglobal.debugging) {
424 if (bsp->args.family == AF_INET)
425 zlog_debug("%s: invalid address: %pI4",
426 __func__,
427 (struct in_addr *)&bsp->args.dst);
428 else
429 zlog_debug("%s: invalid address: %pI6",
430 __func__, &bsp->args.dst);
431 }
432 return false;
433 }
434
435 /* Multi hop requires local address. */
436 if (bsp->args.mhop
437 && memcmp(&i6a_zero, &bsp->args.src, sizeof(i6a_zero)) == 0) {
438 if (bsglobal.debugging)
439 zlog_debug(
440 "%s: multi hop but no local address provided",
441 __func__);
442 return false;
443 }
444
445 /* Check VRF ID. */
446 if (bsp->args.vrf_id == VRF_UNKNOWN) {
447 if (bsglobal.debugging)
448 zlog_debug("%s: asked for unknown VRF", __func__);
449 return false;
450 }
451
452 return true;
453 }
454
455 static int _bfd_sess_send(struct thread *t)
456 {
457 struct bfd_session_params *bsp = THREAD_ARG(t);
458 int rv;
459
460 /* Validate configuration before trying to send bogus data. */
461 if (!_bfd_sess_valid(bsp))
462 return 0;
463
464 if (bsp->lastev == BSE_INSTALL) {
465 bsp->args.command = bsp->installed ? ZEBRA_BFD_DEST_UPDATE
466 : ZEBRA_BFD_DEST_REGISTER;
467 } else
468 bsp->args.command = ZEBRA_BFD_DEST_DEREGISTER;
469
470 /* If not installed and asked for uninstall, do nothing. */
471 if (!bsp->installed && bsp->args.command == ZEBRA_BFD_DEST_DEREGISTER)
472 return 0;
473
474 rv = zclient_bfd_command(bsglobal.zc, &bsp->args);
475 /* Command was sent successfully. */
476 if (rv == 0) {
477 /* Update installation status. */
478 if (bsp->args.command == ZEBRA_BFD_DEST_DEREGISTER)
479 bsp->installed = false;
480 else if (bsp->args.command == ZEBRA_BFD_DEST_REGISTER)
481 bsp->installed = true;
482 } else {
483 struct ipaddr src, dst;
484
485 src.ipa_type = bsp->args.family;
486 src.ipaddr_v6 = bsp->args.src;
487 dst.ipa_type = bsp->args.family;
488 dst.ipaddr_v6 = bsp->args.dst;
489
490 zlog_err(
491 "%s: BFD session %pIA -> %pIA interface %s VRF %s(%u) was not %s",
492 __func__, &src, &dst,
493 bsp->args.ifnamelen ? bsp->args.ifname : "*",
494 vrf_id_to_name(bsp->args.vrf_id), bsp->args.vrf_id,
495 bsp->lastev == BSE_INSTALL ? "installed"
496 : "uninstalled");
497 }
498
499 return 0;
500 }
501
502 static void _bfd_sess_remove(struct bfd_session_params *bsp)
503 {
504 /* Not installed, nothing to do. */
505 if (!bsp->installed)
506 return;
507
508 /* Cancel any pending installation request. */
509 THREAD_OFF(bsp->installev);
510
511 /* Send request to remove any session. */
512 bsp->lastev = BSE_UNINSTALL;
513 thread_execute(bsglobal.tm, _bfd_sess_send, bsp, 0);
514 }
515
516 void bfd_sess_free(struct bfd_session_params **bsp)
517 {
518 if (*bsp == NULL)
519 return;
520
521 /* Remove any installed session. */
522 _bfd_sess_remove(*bsp);
523
524 /* Remove from global list. */
525 TAILQ_REMOVE(&bsglobal.bsplist, (*bsp), entry);
526
527 /* Free the memory and point to NULL. */
528 XFREE(MTYPE_BFD_INFO, (*bsp));
529 }
530
531 static bool bfd_sess_address_changed(const struct bfd_session_params *bsp,
532 uint32_t family,
533 const struct in6_addr *src,
534 const struct in6_addr *dst)
535 {
536 size_t addrlen;
537
538 if (bsp->args.family != family)
539 return true;
540
541 addrlen = (family == AF_INET) ? sizeof(struct in_addr)
542 : sizeof(struct in6_addr);
543 if ((src == NULL && memcmp(&bsp->args.src, &i6a_zero, addrlen))
544 || (src && memcmp(src, &bsp->args.src, addrlen))
545 || memcmp(dst, &bsp->args.dst, addrlen))
546 return true;
547
548 return false;
549 }
550
551 void bfd_sess_set_ipv4_addrs(struct bfd_session_params *bsp,
552 struct in_addr *src, struct in_addr *dst)
553 {
554 if (!bfd_sess_address_changed(bsp, AF_INET, (struct in6_addr *)src,
555 (struct in6_addr *)dst))
556 return;
557
558 /* If already installed, remove the old setting. */
559 _bfd_sess_remove(bsp);
560
561 bsp->args.family = AF_INET;
562
563 /* Clean memory, set zero value and avoid static analyser warnings. */
564 memset(&bsp->args.src, 0, sizeof(bsp->args.src));
565 memset(&bsp->args.dst, 0, sizeof(bsp->args.dst));
566
567 /* Copy the equivalent of IPv4 to arguments structure. */
568 if (src)
569 memcpy(&bsp->args.src, src, sizeof(struct in_addr));
570
571 assert(dst);
572 memcpy(&bsp->args.dst, dst, sizeof(struct in_addr));
573 }
574
575 void bfd_sess_set_ipv6_addrs(struct bfd_session_params *bsp,
576 struct in6_addr *src, struct in6_addr *dst)
577 {
578 if (!bfd_sess_address_changed(bsp, AF_INET, (struct in6_addr *)src,
579 (struct in6_addr *)dst))
580 return;
581
582 /* If already installed, remove the old setting. */
583 _bfd_sess_remove(bsp);
584
585 bsp->args.family = AF_INET6;
586
587 /* Clean memory, set zero value and avoid static analyser warnings. */
588 memset(&bsp->args.src, 0, sizeof(bsp->args.src));
589
590 if (src)
591 bsp->args.src = *src;
592
593 assert(dst);
594 bsp->args.dst = *dst;
595 }
596
597 void bfd_sess_set_interface(struct bfd_session_params *bsp, const char *ifname)
598 {
599 if ((ifname == NULL && bsp->args.ifnamelen == 0)
600 || (ifname && strcmp(bsp->args.ifname, ifname) == 0))
601 return;
602
603 /* If already installed, remove the old setting. */
604 _bfd_sess_remove(bsp);
605
606 if (ifname == NULL) {
607 bsp->args.ifname[0] = 0;
608 bsp->args.ifnamelen = 0;
609 return;
610 }
611
612 if (strlcpy(bsp->args.ifname, ifname, sizeof(bsp->args.ifname))
613 > sizeof(bsp->args.ifname))
614 zlog_warn("%s: interface name truncated: %s", __func__, ifname);
615
616 bsp->args.ifnamelen = strlen(bsp->args.ifname);
617 }
618
619 void bfd_sess_set_profile(struct bfd_session_params *bsp, const char *profile)
620 {
621 if (profile == NULL) {
622 bsp->args.profile[0] = 0;
623 bsp->args.profilelen = 0;
624 return;
625 }
626
627 if (strlcpy(bsp->args.profile, profile, sizeof(bsp->args.profile))
628 > sizeof(bsp->args.profile))
629 zlog_warn("%s: profile name truncated: %s", __func__, profile);
630
631 bsp->args.profilelen = strlen(bsp->args.profile);
632 }
633
634 void bfd_sess_set_vrf(struct bfd_session_params *bsp, vrf_id_t vrf_id)
635 {
636 if (bsp->args.vrf_id == vrf_id)
637 return;
638
639 /* If already installed, remove the old setting. */
640 _bfd_sess_remove(bsp);
641
642 bsp->args.vrf_id = vrf_id;
643 }
644
645 void bfd_sess_set_mininum_ttl(struct bfd_session_params *bsp, uint8_t min_ttl)
646 {
647 assert(min_ttl != 0);
648
649 if (bsp->args.ttl == ((BFD_SINGLE_HOP_TTL + 1) - min_ttl))
650 return;
651
652 /* If already installed, remove the old setting. */
653 _bfd_sess_remove(bsp);
654
655 /* Invert TTL value: protocol expects number of hops. */
656 min_ttl = (BFD_SINGLE_HOP_TTL + 1) - min_ttl;
657 bsp->args.ttl = min_ttl;
658 bsp->args.mhop = (min_ttl > 1);
659 }
660
661 void bfd_sess_set_hop_count(struct bfd_session_params *bsp, uint8_t min_ttl)
662 {
663 if (bsp->args.ttl == min_ttl)
664 return;
665
666 /* If already installed, remove the old setting. */
667 _bfd_sess_remove(bsp);
668
669 bsp->args.ttl = min_ttl;
670 bsp->args.mhop = (min_ttl > 1);
671 }
672
673
674 void bfd_sess_set_cbit(struct bfd_session_params *bsp, bool enable)
675 {
676 bsp->args.cbit = enable;
677 }
678
679 void bfd_sess_set_timers(struct bfd_session_params *bsp,
680 uint8_t detection_multiplier, uint32_t min_rx,
681 uint32_t min_tx)
682 {
683 bsp->args.detection_multiplier = detection_multiplier;
684 bsp->args.min_rx = min_rx;
685 bsp->args.min_tx = min_tx;
686 }
687
688 void bfd_sess_install(struct bfd_session_params *bsp)
689 {
690 bsp->lastev = BSE_INSTALL;
691 thread_add_event(bsglobal.tm, _bfd_sess_send, bsp, 0, &bsp->installev);
692 }
693
694 void bfd_sess_uninstall(struct bfd_session_params *bsp)
695 {
696 bsp->lastev = BSE_UNINSTALL;
697 thread_add_event(bsglobal.tm, _bfd_sess_send, bsp, 0, &bsp->installev);
698 }
699
700 enum bfd_session_state bfd_sess_status(const struct bfd_session_params *bsp)
701 {
702 return bsp->bss.state;
703 }
704
705 uint8_t bfd_sess_minimum_ttl(const struct bfd_session_params *bsp)
706 {
707 return ((BFD_SINGLE_HOP_TTL + 1) - bsp->args.ttl);
708 }
709
710 uint8_t bfd_sess_hop_count(const struct bfd_session_params *bsp)
711 {
712 return bsp->args.ttl;
713 }
714
715 const char *bfd_sess_profile(const struct bfd_session_params *bsp)
716 {
717 return bsp->args.profilelen ? bsp->args.profile : NULL;
718 }
719
720 void bfd_sess_addresses(const struct bfd_session_params *bsp, int *family,
721 struct in6_addr *src, struct in6_addr *dst)
722 {
723 *family = bsp->args.family;
724 if (src)
725 *src = bsp->args.src;
726 if (dst)
727 *dst = bsp->args.dst;
728 }
729
730 const char *bfd_sess_interface(const struct bfd_session_params *bsp)
731 {
732 if (bsp->args.ifnamelen)
733 return bsp->args.ifname;
734
735 return NULL;
736 }
737
738 const char *bfd_sess_vrf(const struct bfd_session_params *bsp)
739 {
740 return vrf_id_to_name(bsp->args.vrf_id);
741 }
742
743 vrf_id_t bfd_sess_vrf_id(const struct bfd_session_params *bsp)
744 {
745 return bsp->args.vrf_id;
746 }
747
748 bool bfd_sess_cbit(const struct bfd_session_params *bsp)
749 {
750 return bsp->args.cbit;
751 }
752
753 void bfd_sess_timers(const struct bfd_session_params *bsp,
754 uint8_t *detection_multiplier, uint32_t *min_rx,
755 uint32_t *min_tx)
756 {
757 *detection_multiplier = bsp->args.detection_multiplier;
758 *min_rx = bsp->args.min_rx;
759 *min_tx = bsp->args.min_tx;
760 }
761
762 void bfd_sess_show(struct vty *vty, struct json_object *json,
763 struct bfd_session_params *bsp)
764 {
765 json_object *json_bfd = NULL;
766 char time_buf[64];
767
768 if (!bsp)
769 return;
770
771 /* Show type. */
772 if (json) {
773 json_bfd = json_object_new_object();
774 if (bsp->args.mhop)
775 json_object_string_add(json_bfd, "type", "multi hop");
776 else
777 json_object_string_add(json_bfd, "type", "single hop");
778 } else
779 vty_out(vty, " BFD: Type: %s\n",
780 bsp->args.mhop ? "multi hop" : "single hop");
781
782 /* Show configuration. */
783 if (json) {
784 json_object_int_add(json_bfd, "detectMultiplier",
785 bsp->args.detection_multiplier);
786 json_object_int_add(json_bfd, "rxMinInterval",
787 bsp->args.min_rx);
788 json_object_int_add(json_bfd, "txMinInterval",
789 bsp->args.min_tx);
790 } else {
791 vty_out(vty,
792 " Detect Multiplier: %d, Min Rx interval: %d, Min Tx interval: %d\n",
793 bsp->args.detection_multiplier, bsp->args.min_rx,
794 bsp->args.min_tx);
795 }
796
797 bfd_last_update(bsp->bss.last_event, time_buf, sizeof(time_buf));
798 if (json) {
799 json_object_string_add(json_bfd, "status",
800 bfd_get_status_str(bsp->bss.state));
801 json_object_string_add(json_bfd, "lastUpdate", time_buf);
802 } else
803 vty_out(vty, " Status: %s, Last update: %s\n",
804 bfd_get_status_str(bsp->bss.state), time_buf);
805
806 if (json)
807 json_object_object_add(json, "peerBfdInfo", json_bfd);
808 else
809 vty_out(vty, "\n");
810 }
811
812 /*
813 * Zebra communication related.
814 */
815
816 /**
817 * Callback for reinstallation of all registered BFD sessions.
818 *
819 * Use this as `zclient` `bfd_dest_replay` callback.
820 */
821 static int zclient_bfd_session_reply(ZAPI_CALLBACK_ARGS)
822 {
823 struct bfd_session_params *bsp;
824
825 /* Do nothing when shutting down. */
826 if (bsglobal.shutting_down)
827 return 0;
828
829 if (bsglobal.debugging)
830 zlog_debug("%s: sending all sessions registered", __func__);
831
832 /* Send the client registration */
833 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, vrf_id);
834
835 /* Replay all activated peers. */
836 TAILQ_FOREACH (bsp, &bsglobal.bsplist, entry) {
837 /* Skip not installed sessions. */
838 if (!bsp->installed)
839 continue;
840
841 /* We are reconnecting, so we must send installation. */
842 bsp->installed = false;
843
844 /* Cancel any pending installation request. */
845 THREAD_OFF(bsp->installev);
846
847 /* Ask for installation. */
848 bsp->lastev = BSE_INSTALL;
849 thread_execute(bsglobal.tm, _bfd_sess_send, bsp, 0);
850 }
851
852 return 0;
853 }
854
855 static int zclient_bfd_session_update(ZAPI_CALLBACK_ARGS)
856 {
857 struct bfd_session_params *bsp, *bspn;
858 size_t sessions_updated = 0;
859 struct interface *ifp;
860 int remote_cbit = false;
861 int state = BFD_STATUS_UNKNOWN;
862 time_t now;
863 size_t addrlen;
864 struct prefix dp;
865 struct prefix sp;
866 char ifstr[128], cbitstr[32];
867
868 /* Do nothing when shutting down. */
869 if (bsglobal.shutting_down)
870 return 0;
871
872 ifp = bfd_get_peer_info(zclient->ibuf, &dp, &sp, &state, &remote_cbit,
873 vrf_id);
874 /*
875 * When interface lookup fails or an invalid stream is read, we must
876 * not proceed otherwise it will trigger an assertion while checking
877 * family type below.
878 */
879 if (dp.family == 0 || sp.family == 0)
880 return 0;
881
882 if (bsglobal.debugging) {
883 ifstr[0] = 0;
884 if (ifp)
885 snprintf(ifstr, sizeof(ifstr), " (interface %s)",
886 ifp->name);
887
888 snprintf(cbitstr, sizeof(cbitstr), " (CPI bit %s)",
889 remote_cbit ? "yes" : "no");
890
891 zlog_debug("%s: %pFX -> %pFX%s VRF %s(%u)%s: %s", __func__, &sp,
892 &dp, ifstr, vrf_id_to_name(vrf_id), vrf_id, cbitstr,
893 bfd_get_status_str(state));
894 }
895
896 switch (dp.family) {
897 case AF_INET:
898 addrlen = sizeof(struct in_addr);
899 break;
900 case AF_INET6:
901 addrlen = sizeof(struct in6_addr);
902 break;
903
904 default:
905 /* Unexpected value. */
906 assert(0);
907 break;
908 }
909
910 /* Cache current time to avoid multiple monotime clock calls. */
911 now = monotime(NULL);
912
913 /* Notify all matching sessions about update. */
914 TAILQ_FOREACH_SAFE (bsp, &bsglobal.bsplist, entry, bspn) {
915 /* Skip not installed entries. */
916 if (!bsp->installed)
917 continue;
918 /* Skip different VRFs. */
919 if (bsp->args.vrf_id != vrf_id)
920 continue;
921 /* Skip different families. */
922 if (bsp->args.family != dp.family)
923 continue;
924 /* Skip different interface. */
925 if (bsp->args.ifnamelen && ifp
926 && strcmp(bsp->args.ifname, ifp->name) != 0)
927 continue;
928 /* Skip non matching destination addresses. */
929 if (memcmp(&bsp->args.dst, &dp.u, addrlen) != 0)
930 continue;
931 /*
932 * Source comparison test:
933 * We will only compare source if BFD daemon provided the
934 * source address and the protocol set a source address in
935 * the configuration otherwise we'll just skip it.
936 */
937 if (sp.family && memcmp(&bsp->args.src, &i6a_zero, addrlen) != 0
938 && memcmp(&sp.u, &i6a_zero, addrlen) != 0
939 && memcmp(&bsp->args.src, &sp.u, addrlen) != 0)
940 continue;
941 /* No session state change. */
942 if ((int)bsp->bss.state == state)
943 continue;
944
945 bsp->bss.last_event = now;
946 bsp->bss.previous_state = bsp->bss.state;
947 bsp->bss.state = state;
948 bsp->bss.remote_cbit = remote_cbit;
949 bsp->updatecb(bsp, &bsp->bss, bsp->arg);
950 sessions_updated++;
951 }
952
953 if (bsglobal.debugging)
954 zlog_debug("%s: sessions updated: %zu", __func__,
955 sessions_updated);
956
957 return 0;
958 }
959
960 void bfd_protocol_integration_init(struct zclient *zc, struct thread_master *tm)
961 {
962 /* Initialize data structure. */
963 TAILQ_INIT(&bsglobal.bsplist);
964
965 /* Copy pointers. */
966 bsglobal.zc = zc;
967 bsglobal.tm = tm;
968
969 /* Install our callbacks. */
970 zc->interface_bfd_dest_update = zclient_bfd_session_update;
971 zc->bfd_dest_replay = zclient_bfd_session_reply;
972
973 /* Send the client registration */
974 bfd_client_sendmsg(zc, ZEBRA_BFD_CLIENT_REGISTER, VRF_DEFAULT);
975 }
976
977 void bfd_protocol_integration_set_debug(bool enable)
978 {
979 bsglobal.debugging = enable;
980 }
981
982 void bfd_protocol_integration_set_shutdown(bool enable)
983 {
984 bsglobal.shutting_down = enable;
985 }
986
987 bool bfd_protocol_integration_debug(void)
988 {
989 return bsglobal.debugging;
990 }
991
992 bool bfd_protocol_integration_shutting_down(void)
993 {
994 return bsglobal.shutting_down;
995 }