]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfd.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / bfdd / bfd.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
e9e2c950
RZ
2/*********************************************************************
3 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
4 *
e9e2c950
RZ
5 * bfd.h: implements the BFD protocol.
6 */
7
8#ifndef _BFD_H_
9#define _BFD_H_
10
11#include <netinet/in.h>
12
13#include <stdbool.h>
14#include <stdarg.h>
15#include <stdint.h>
16
17#include "lib/hash.h"
18#include "lib/libfrr.h"
19#include "lib/qobj.h"
20#include "lib/queue.h"
80edb675 21#include "lib/vrf.h"
e9e2c950
RZ
22
23#include "bfdctl.h"
24
e9e2c950
RZ
25#ifdef BFD_DEBUG
26#define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
27#else
28#define BFDD_JSON_CONV_OPTIONS (0)
29#endif
30
bf8d3d6a
DL
31DECLARE_MGROUP(BFDD);
32DECLARE_MTYPE(BFDD_CONTROL);
33DECLARE_MTYPE(BFDD_NOTIFICATION);
e9e2c950
RZ
34
35struct bfd_timers {
36 uint32_t desired_min_tx;
37 uint32_t required_min_rx;
38 uint32_t required_min_echo;
39};
40
41struct bfd_discrs {
42 uint32_t my_discr;
43 uint32_t remote_discr;
44};
45
46/*
47 * Format of control packet. From section 4)
48 */
49struct bfd_pkt {
50 union {
51 uint32_t byteFields;
52 struct {
53 uint8_t diag;
54 uint8_t flags;
55 uint8_t detect_mult;
56 uint8_t len;
57 };
58 };
59 struct bfd_discrs discrs;
60 struct bfd_timers timers;
61};
62
63/*
64 * Format of Echo packet.
65 */
66struct bfd_echo_pkt {
67 union {
68 uint32_t byteFields;
69 struct {
70 uint8_t ver;
71 uint8_t len;
72 uint16_t reserved;
73 };
74 };
75 uint32_t my_discr;
618a06fe 76 uint64_t time_sent_sec;
77 uint64_t time_sent_usec;
e9e2c950
RZ
78};
79
80
81/* Macros for manipulating control packets */
82#define BFD_VERMASK 0x03
83#define BFD_DIAGMASK 0x1F
84#define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
85#define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
86#define BFD_VERSION 1
87#define BFD_PBIT 0x20
88#define BFD_FBIT 0x10
89#define BFD_CBIT 0x08
90#define BFD_ABIT 0x04
91#define BFD_DEMANDBIT 0x02
e9e2c950
RZ
92#define BFD_SETDEMANDBIT(flags, val) \
93 { \
94 if ((val)) \
95 flags |= BFD_DEMANDBIT; \
96 }
97#define BFD_SETPBIT(flags, val) \
98 { \
99 if ((val)) \
100 flags |= BFD_PBIT; \
101 }
102#define BFD_GETPBIT(flags) (flags & BFD_PBIT)
103#define BFD_SETFBIT(flags, val) \
104 { \
105 if ((val)) \
106 flags |= BFD_FBIT; \
107 }
108#define BFD_GETFBIT(flags) (flags & BFD_FBIT)
109#define BFD_SETSTATE(flags, val) \
110 { \
111 if ((val)) \
112 flags |= (val & 0x3) << 6; \
113 }
114#define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
9beff0bd
PG
115#define BFD_SETCBIT(flags, val) \
116 { \
117 if ((val)) \
118 flags |= val; \
119 }
120#define BFD_GETCBIT(flags) (flags & BFD_FBIT)
e9e2c950
RZ
121#define BFD_ECHO_VERSION 1
122#define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
e9e2c950 123
40675ea9
RZ
124enum bfd_diagnosticis {
125 BD_OK = 0,
126 /* Control Detection Time Expired. */
127 BD_CONTROL_EXPIRED = 1,
128 /* Echo Function Failed. */
129 BD_ECHO_FAILED = 2,
130 /* Neighbor Signaled Session Down. */
131 BD_NEIGHBOR_DOWN = 3,
132 /* Forwarding Plane Reset. */
133 BD_FORWARDING_RESET = 4,
134 /* Path Down. */
135 BD_PATH_DOWN = 5,
136 /* Concatenated Path Down. */
137 BD_CONCATPATH_DOWN = 6,
138 /* Administratively Down. */
139 BD_ADMIN_DOWN = 7,
140 /* Reverse Concatenated Path Down. */
141 BD_REVCONCATPATH_DOWN = 8,
142 /* 9..31: reserved. */
143};
144
e9e2c950
RZ
145/* BFD session flags */
146enum bfd_session_flags {
147 BFD_SESS_FLAG_NONE = 0,
148 BFD_SESS_FLAG_ECHO = 1 << 0, /* BFD Echo functionality */
149 BFD_SESS_FLAG_ECHO_ACTIVE = 1 << 1, /* BFD Echo Packets are being sent
150 * actively
151 */
152 BFD_SESS_FLAG_MH = 1 << 2, /* BFD Multi-hop session */
e9e2c950
RZ
153 BFD_SESS_FLAG_IPV6 = 1 << 4, /* BFD IPv6 session */
154 BFD_SESS_FLAG_SEND_EVT_ACTIVE = 1 << 5, /* send event timer active */
155 BFD_SESS_FLAG_SEND_EVT_IGNORE = 1 << 6, /* ignore send event when timer
156 * expires
157 */
158 BFD_SESS_FLAG_SHUTDOWN = 1 << 7, /* disable BGP peer function */
57485b0b 159 BFD_SESS_FLAG_CONFIG = 1 << 8, /* Session configured with bfd NB API */
160 BFD_SESS_FLAG_CBIT = 1 << 9, /* CBIT is set */
1a2e2fff 161 BFD_SESS_FLAG_PASSIVE = 1 << 10, /* Passive mode */
57485b0b 162 BFD_SESS_FLAG_MAC_SET = 1 << 11, /* MAC of peer known */
e9e2c950
RZ
163};
164
03e3333b
IR
165/*
166 * BFD session hash key.
167 *
168 * This structure must not have any padding bytes because their value is
169 * unspecified after the struct assignment. Even when all fields of two keys
170 * are the same, if the padding bytes are different, then the calculated hash
171 * value is different, and the hash lookup will fail.
172 *
173 * Currently, the structure fields are correctly aligned, and the "packed"
174 * attribute is added as a precaution. "family" and "mhop" fields are two-bytes
175 * to eliminate unaligned memory access to "peer" and "local".
176 */
79b4a6fc
RZ
177struct bfd_key {
178 uint16_t family;
03e3333b 179 uint16_t mhop;
79b4a6fc
RZ
180 struct in6_addr peer;
181 struct in6_addr local;
3682bd90
RZ
182 char ifname[INTERFACE_NAMSIZ];
183 char vrfname[VRF_NAMSIZ];
03e3333b 184} __attribute__((packed));
e9e2c950
RZ
185
186struct bfd_session_stats {
187 uint64_t rx_ctrl_pkt;
188 uint64_t tx_ctrl_pkt;
189 uint64_t rx_echo_pkt;
190 uint64_t tx_echo_pkt;
0684c9b1
RZ
191 uint64_t session_up;
192 uint64_t session_down;
193 uint64_t znotification;
e9e2c950
RZ
194};
195
ccc9ada8
RZ
196/**
197 * BFD session profile to override default configurations.
198 */
199struct bfd_profile {
200 /** Profile name. */
201 char name[64];
202
203 /** Session detection multiplier. */
204 uint8_t detection_multiplier;
205 /** Desired transmission interval (in microseconds). */
206 uint32_t min_tx;
207 /** Minimum required receive interval (in microseconds). */
208 uint32_t min_rx;
209 /** Administrative state. */
210 bool admin_shutdown;
1a2e2fff
RZ
211 /** Passive mode. */
212 bool passive;
262e1d25
RZ
213 /** Minimum expected TTL value. */
214 uint8_t minimum_ttl;
ccc9ada8
RZ
215
216 /** Echo mode (only applies to single hop). */
217 bool echo_mode;
4df3e31c
IR
218 /** Desired echo transmission interval (in microseconds). */
219 uint32_t min_echo_tx;
ccc9ada8
RZ
220 /** Minimum required echo receive interval (in microseconds). */
221 uint32_t min_echo_rx;
222
223 /** Profile list entry. */
224 TAILQ_ENTRY(bfd_profile) entry;
225};
226
227/** Profile list type. */
228TAILQ_HEAD(bfdproflist, bfd_profile);
229
e9e2c950
RZ
230/* bfd_session shortcut label forwarding. */
231struct peer_label;
232
4df3e31c
IR
233struct bfd_config_timers {
234 uint32_t desired_min_tx;
235 uint32_t required_min_rx;
236 uint32_t desired_min_echo_tx;
237 uint32_t required_min_echo_rx;
238};
239
618a06fe 240#define BFD_RTT_SAMPLE 8
241
e9e2c950
RZ
242/*
243 * Session state information
244 */
245struct bfd_session {
246
247 /* protocol state per RFC 5880*/
248 uint8_t ses_state;
249 struct bfd_discrs discrs;
250 uint8_t local_diag;
251 uint8_t demand_mode;
252 uint8_t detect_mult;
253 uint8_t remote_detect_mult;
254 uint8_t mh_ttl;
9beff0bd 255 uint8_t remote_cbit;
e9e2c950 256
ccc9ada8
RZ
257 /** BFD profile name. */
258 char *profile_name;
259 /** BFD pre configured profile. */
260 struct bfd_profile *profile;
261 /** BFD peer configuration (without profile). */
262 struct bfd_profile peer_profile;
263
e9e2c950 264 /* Timers */
4df3e31c 265 struct bfd_config_timers timers;
f43b9368 266 struct bfd_timers cur_timers;
e9e2c950 267 uint64_t detect_TO;
e6685141
DS
268 struct event *echo_recvtimer_ev;
269 struct event *recvtimer_ev;
e9e2c950
RZ
270 uint64_t xmt_TO;
271 uint64_t echo_xmt_TO;
e6685141
DS
272 struct event *xmttimer_ev;
273 struct event *echo_xmttimer_ev;
e9e2c950
RZ
274 uint64_t echo_detect_TO;
275
276 /* software object state */
277 uint8_t polling;
278
279 /* This and the localDiscr are the keys to state info */
79b4a6fc 280 struct bfd_key key;
e9e2c950 281 struct peer_label *pl;
e9e2c950 282
230aefe2 283 struct bfd_dplane_ctx *bdc;
57485b0b 284 struct sockaddr_any local_address;
285 uint8_t peer_hw_addr[ETH_ALEN];
80edb675 286 struct interface *ifp;
b333abc2 287 struct vrf *vrf;
79b4a6fc
RZ
288
289 int sock;
e9e2c950
RZ
290
291 /* BFD session flags */
292 enum bfd_session_flags flags;
293
e9e2c950 294 struct bfd_session_stats stats;
e9e2c950
RZ
295
296 struct timeval uptime; /* last up time */
297 struct timeval downtime; /* last down time */
298
299 /* Remote peer data (for debugging mostly) */
300 uint8_t remote_diag;
301 struct bfd_timers remote_timers;
302
303 uint64_t refcount; /* number of pointers referencing this. */
618a06fe 304
305 uint8_t rtt_valid; /* number of valid samples */
306 uint8_t rtt_index; /* last index added */
307 uint64_t rtt[BFD_RTT_SAMPLE]; /* RRT in usec for echo to be looped */
e9e2c950 308};
e9e2c950
RZ
309
310struct peer_label {
311 TAILQ_ENTRY(peer_label) pl_entry;
312
313 struct bfd_session *pl_bs;
314 char pl_label[MAXNAMELEN];
315};
316TAILQ_HEAD(pllist, peer_label);
317
318struct bfd_diag_str_list {
319 const char *str;
320 int type;
321};
322
323struct bfd_state_str_list {
324 const char *str;
325 int type;
326};
327
d245e522
RZ
328struct bfd_session_observer {
329 struct bfd_session *bso_bs;
ced291de
RZ
330 char bso_entryname[MAXNAMELEN];
331 struct prefix bso_addr;
d245e522
RZ
332
333 TAILQ_ENTRY(bfd_session_observer) bso_entry;
334};
335TAILQ_HEAD(obslist, bfd_session_observer);
336
e9e2c950
RZ
337
338/* States defined per 4.1 */
339#define PTM_BFD_ADM_DOWN 0
340#define PTM_BFD_DOWN 1
341#define PTM_BFD_INIT 2
342#define PTM_BFD_UP 3
343
344
345/* Various constants */
346/* Retrieved from ptm_timer.h from Cumulus PTM sources. */
e9e2c950
RZ
347#define BFD_DEF_DEMAND 0
348#define BFD_DEFDETECTMULT 3
33400b46
RZ
349#define BFD_DEFDESIREDMINTX (300 * 1000) /* microseconds. */
350#define BFD_DEFREQUIREDMINRX (300 * 1000) /* microseconds. */
4df3e31c
IR
351#define BFD_DEF_DES_MIN_ECHO_TX (50 * 1000) /* microseconds. */
352#define BFD_DEF_REQ_MIN_ECHO_RX (50 * 1000) /* microseconds. */
33400b46 353#define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
262e1d25
RZ
354/** Minimum multi hop TTL. */
355#define BFD_DEF_MHOP_TTL 254
e9e2c950
RZ
356#define BFD_PKT_LEN 24 /* Length of control packet */
357#define BFD_TTL_VAL 255
358#define BFD_RCV_TTL_VAL 1
359#define BFD_TOS_VAL 0xC0
360#define BFD_PKT_INFO_VAL 1
361#define BFD_IPV6_PKT_INFO_VAL 1
362#define BFD_IPV6_ONLY_VAL 1
545d3f70
RZ
363#define BFD_SRCPORTINIT 49152
364#define BFD_SRCPORTMAX 65535
e9e2c950
RZ
365#define BFD_DEFDESTPORT 3784
366#define BFD_DEF_ECHO_PORT 3785
367#define BFD_DEF_MHOP_DEST_PORT 4784
e9e2c950
RZ
368
369/*
370 * control.c
371 *
372 * Daemon control code to speak with local consumers.
373 */
374
375/* See 'bfdctrl.h' for client protocol definitions. */
376
377struct bfd_control_buffer {
378 size_t bcb_left;
379 size_t bcb_pos;
380 union {
381 struct bfd_control_msg *bcb_bcm;
382 uint8_t *bcb_buf;
383 };
384};
385
386struct bfd_control_queue {
387 TAILQ_ENTRY(bfd_control_queue) bcq_entry;
388
389 struct bfd_control_buffer bcq_bcb;
390};
391TAILQ_HEAD(bcqueue, bfd_control_queue);
392
393struct bfd_notify_peer {
394 TAILQ_ENTRY(bfd_notify_peer) bnp_entry;
395
396 struct bfd_session *bnp_bs;
397};
398TAILQ_HEAD(bnplist, bfd_notify_peer);
399
400struct bfd_control_socket {
401 TAILQ_ENTRY(bfd_control_socket) bcs_entry;
402
403 int bcs_sd;
e6685141
DS
404 struct event *bcs_ev;
405 struct event *bcs_outev;
e9e2c950
RZ
406 struct bcqueue bcs_bcqueue;
407
408 /* Notification data */
409 uint64_t bcs_notify;
410 struct bnplist bcs_bnplist;
411
412 enum bc_msg_version bcs_version;
413 enum bc_msg_type bcs_type;
414
415 /* Message buffering */
416 struct bfd_control_buffer bcs_bin;
417 struct bfd_control_buffer *bcs_bout;
418};
419TAILQ_HEAD(bcslist, bfd_control_socket);
420
421int control_init(const char *path);
422void control_shutdown(void);
7555dc61 423int control_notify(struct bfd_session *bs, uint8_t notify_state);
e9e2c950 424int control_notify_config(const char *op, struct bfd_session *bs);
e6685141 425void control_accept(struct event *t);
e9e2c950
RZ
426
427
428/*
429 * bfdd.c
430 *
431 * Daemon specific code.
432 */
7bcadbae 433struct bfd_vrf_global {
e9e2c950
RZ
434 int bg_shop;
435 int bg_mhop;
436 int bg_shop6;
437 int bg_mhop6;
438 int bg_echo;
2f11c53f 439 int bg_echov6;
7bcadbae
PG
440 struct vrf *vrf;
441
e6685141 442 struct event *bg_ev[6];
7bcadbae 443};
e9e2c950 444
230aefe2
RZ
445/* Forward declaration of data plane context struct. */
446struct bfd_dplane_ctx;
447TAILQ_HEAD(dplane_queue, bfd_dplane_ctx);
448
7bcadbae 449struct bfd_global {
e9e2c950 450 int bg_csock;
e6685141 451 struct event *bg_csockev;
e9e2c950
RZ
452 struct bcslist bg_bcslist;
453
454 struct pllist bg_pllist;
d245e522
RZ
455
456 struct obslist bg_obslist;
f21536d2
PG
457
458 struct zebra_privs_t bfdd_privs;
48da2c31 459
f3e1d224
RZ
460 /**
461 * Daemon is exit()ing? Use this to avoid actions that expect a
462 * running system or to avoid unnecessary operations when quitting.
463 */
464 bool bg_shutdown;
465
230aefe2
RZ
466 /* Distributed BFD items. */
467 bool bg_use_dplane;
468 int bg_dplane_sock;
e6685141 469 struct event *bg_dplane_sockev;
230aefe2
RZ
470 struct dplane_queue bg_dplaneq;
471
48da2c31 472 /* Debug options. */
230aefe2
RZ
473 /* Show distributed BFD debug messages. */
474 bool debug_dplane;
48da2c31
RZ
475 /* Show all peer state changes events. */
476 bool debug_peer_event;
477 /*
478 * Show zebra message exchanges:
479 * - Interface add/delete.
480 * - Local address add/delete.
481 * - VRF add/delete.
482 */
483 bool debug_zebra;
484 /*
485 * Show network level debug information:
486 * - Echo packets without session.
487 * - Unavailable peer sessions.
488 * - Network system call failures.
489 */
490 bool debug_network;
e9e2c950 491};
48da2c31 492
e9e2c950 493extern struct bfd_global bglobal;
2b64873d
DL
494extern const struct bfd_diag_str_list diag_list[];
495extern const struct bfd_state_str_list state_list[];
e9e2c950
RZ
496
497void socket_close(int *s);
498
499
500/*
501 * config.c
502 *
503 * Contains the code related with loading/reloading configuration.
504 */
505int parse_config(const char *fname);
506int config_request_add(const char *jsonstr);
507int config_request_del(const char *jsonstr);
508char *config_response(const char *status, const char *error);
509char *config_notify(struct bfd_session *bs);
510char *config_notify_config(const char *op, struct bfd_session *bs);
511
512typedef int (*bpc_handle)(struct bfd_peer_cfg *, void *arg);
513int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
514 bpc_handle bh);
515
516struct peer_label *pl_new(const char *label, struct bfd_session *bs);
517struct peer_label *pl_find(const char *label);
518void pl_free(struct peer_label *pl);
519
520
521/*
d85b048d 522 * logging - alias to zebra log
e9e2c950 523 */
259b64eb 524#define zlog_fatal(msg, ...) \
d85b048d 525 do { \
259b64eb 526 zlog_err(msg, ##__VA_ARGS__); \
d85b048d
DL
527 assert(!msg); \
528 abort(); \
529 } while (0)
e9e2c950 530
e9e2c950
RZ
531
532/*
533 * bfd_packet.c
534 *
535 * Contains the code related with receiving/seding, packing/unpacking BFD data.
536 */
6e01e275
RZ
537int bp_set_ttlv6(int sd, uint8_t value);
538int bp_set_ttl(int sd, uint8_t value);
539int bp_set_tosv6(int sd, uint8_t value);
540int bp_set_tos(int sd, uint8_t value);
e9e2c950
RZ
541int bp_bind_dev(int sd, const char *dev);
542
4a9feb66
RZ
543int bp_udp_shop(const struct vrf *vrf);
544int bp_udp_mhop(const struct vrf *vrf);
545int bp_udp6_shop(const struct vrf *vrf);
546int bp_udp6_mhop(const struct vrf *vrf);
9f37770f
RZ
547int bp_peer_socket(const struct bfd_session *bs);
548int bp_peer_socketv6(const struct bfd_session *bs);
4a9feb66
RZ
549int bp_echo_socket(const struct vrf *vrf);
550int bp_echov6_socket(const struct vrf *vrf);
e9e2c950
RZ
551
552void ptm_bfd_snd(struct bfd_session *bfd, int fbit);
553void ptm_bfd_echo_snd(struct bfd_session *bfd);
57485b0b 554void ptm_bfd_echo_fp_snd(struct bfd_session *bfd);
e9e2c950 555
e6685141 556void bfd_recv_cb(struct event *t);
e9e2c950 557
e9e2c950
RZ
558
559/*
560 * event.c
561 *
562 * Contains the code related with event loop.
563 */
e6685141 564typedef void (*bfd_ev_cb)(struct event *t);
e9e2c950
RZ
565
566void bfd_recvtimer_update(struct bfd_session *bs);
567void bfd_echo_recvtimer_update(struct bfd_session *bs);
568void bfd_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
569void bfd_echo_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
570
571void bfd_xmttimer_delete(struct bfd_session *bs);
572void bfd_echo_xmttimer_delete(struct bfd_session *bs);
573void bfd_recvtimer_delete(struct bfd_session *bs);
574void bfd_echo_recvtimer_delete(struct bfd_session *bs);
575
576void bfd_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
577void bfd_echo_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
578void bfd_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
579void bfd_echo_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
580
581
582/*
583 * bfd.c
584 *
585 * BFD protocol specific code.
586 */
9f37770f
RZ
587int bfd_session_enable(struct bfd_session *bs);
588void bfd_session_disable(struct bfd_session *bs);
e9e2c950 589struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc);
bc50bcc8
PG
590int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc);
591void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag);
592void ptm_bfd_sess_up(struct bfd_session *bfd);
8bd859f6 593void ptm_bfd_echo_stop(struct bfd_session *bfd);
e9e2c950
RZ
594void ptm_bfd_echo_start(struct bfd_session *bfd);
595void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit);
596void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo);
9f37770f
RZ
597struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
598 struct sockaddr_any *peer,
599 struct sockaddr_any *local,
eb4135ba
IR
600 struct interface *ifp,
601 vrf_id_t vrfid,
9f37770f 602 bool is_mhop);
e9e2c950
RZ
603
604struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc);
605int bfd_session_update_label(struct bfd_session *bs, const char *nlabel);
d3f3a2c4 606void bfd_set_polling(struct bfd_session *bs);
9f37770f
RZ
607void bs_state_handler(struct bfd_session *bs, int nstate);
608void bs_echo_timer_handler(struct bfd_session *bs);
609void bs_final_handler(struct bfd_session *bs);
b912b189 610void bs_set_slow_timers(struct bfd_session *bs);
6e10bd97 611const char *satostr(const struct sockaddr_any *sa);
e9e2c950
RZ
612const char *diag2str(uint8_t diag);
613int strtosa(const char *addr, struct sockaddr_any *sa);
614void integer2timestr(uint64_t time, char *buf, size_t buflen);
79b4a6fc 615const char *bs_to_string(const struct bfd_session *bs);
e9e2c950 616
9f37770f
RZ
617int bs_observer_add(struct bfd_session *bs);
618void bs_observer_del(struct bfd_session_observer *bso);
d245e522 619
79b4a6fc
RZ
620void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
621
014cab13
RZ
622void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
623 struct sockaddr_any *local, bool mhop, const char *ifname,
624 const char *vrfname);
625struct bfd_session *bfd_session_new(void);
626struct bfd_session *bs_registrate(struct bfd_session *bs);
627void bfd_session_free(struct bfd_session *bs);
adc26455
RZ
628const struct bfd_session *bfd_session_next(const struct bfd_session *bs,
629 bool mhop);
2a573ff6 630void bfd_sessions_remove_manual(void);
e93c3c00 631void bfd_profiles_remove(void);
618a06fe 632void bfd_rtt_init(struct bfd_session *bfd);
adc26455 633
4d12e1f9
RZ
634/**
635 * Set the BFD session echo state.
636 *
637 * \param bs the BFD session.
638 * \param echo the echo operational state.
639 */
640void bfd_set_echo(struct bfd_session *bs, bool echo);
641
642/**
643 * Set the BFD session functional state.
644 *
645 * \param bs the BFD session.
646 * \param shutdown the operational value.
647 */
648void bfd_set_shutdown(struct bfd_session *bs, bool shutdown);
649
1a2e2fff
RZ
650/**
651 * Set the BFD session passive mode.
652 *
653 * \param bs the BFD session.
654 * \param passive the passive mode.
655 */
656void bfd_set_passive_mode(struct bfd_session *bs, bool passive);
657
4e38f82a
RZ
658/**
659 * Picks the BFD session configuration from the appropriated source:
660 * if using the default peer configuration prefer profile (if it exists),
661 * otherwise use session.
662 *
663 * \param bs the BFD session.
664 */
665void bfd_session_apply(struct bfd_session *bs);
666
e9e2c950
RZ
667/* BFD hash data structures interface */
668void bfd_initialize(void);
669void bfd_shutdown(void);
9fc0bc5c 670void bfd_vrf_init(void);
7bcadbae
PG
671void bfd_vrf_terminate(void);
672struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd);
e9e2c950 673struct bfd_session *bfd_id_lookup(uint32_t id);
79b4a6fc 674struct bfd_session *bfd_key_lookup(struct bfd_key key);
e9e2c950
RZ
675
676struct bfd_session *bfd_id_delete(uint32_t id);
79b4a6fc 677struct bfd_session *bfd_key_delete(struct bfd_key key);
e9e2c950
RZ
678
679bool bfd_id_insert(struct bfd_session *bs);
79b4a6fc 680bool bfd_key_insert(struct bfd_session *bs);
e9e2c950 681
e3b78da8 682typedef void (*hash_iter_func)(struct hash_bucket *hb, void *arg);
e9e2c950 683void bfd_id_iterate(hash_iter_func hif, void *arg);
79b4a6fc 684void bfd_key_iterate(hash_iter_func hif, void *arg);
e9e2c950 685
fa6e709f
S
686unsigned long bfd_get_session_count(void);
687
e9e2c950 688/* Export callback functions for `event.c`. */
cd9d0537 689extern struct event_loop *master;
e9e2c950 690
e6685141
DS
691void bfd_recvtimer_cb(struct event *t);
692void bfd_echo_recvtimer_cb(struct event *t);
693void bfd_xmt_cb(struct event *t);
694void bfd_echo_xmt_cb(struct event *t);
e9e2c950 695
79b4a6fc
RZ
696extern struct in6_addr zero_addr;
697
ccc9ada8
RZ
698/**
699 * Creates a new profile entry and insert into the global list.
700 *
701 * \param name the BFD profile name.
702 *
703 * \returns `NULL` if it already exists otherwise the new entry.
704 */
705struct bfd_profile *bfd_profile_new(const char *name);
706
707/**
708 * Search for configured BFD profiles (profile name is case insensitive).
709 *
710 * \param name the BFD profile name.
711 *
712 * \returns `NULL` if it doesn't exist otherwise the entry.
713 */
714struct bfd_profile *bfd_profile_lookup(const char *name);
715
716/**
717 * Removes profile from list and free memory.
718 *
719 * \param bp the BFD profile.
720 */
721void bfd_profile_free(struct bfd_profile *bp);
722
723/**
724 * Apply a profile configuration to an existing BFD session. The non default
2b669d3a 725 * values will not be overridden.
ccc9ada8
RZ
726 *
727 * NOTE: if the profile doesn't exist yet, then the profile will be applied
728 * once it begins to exist.
729 *
730 * \param profile_name the BFD profile name.
731 * \param bs the BFD session.
732 */
733void bfd_profile_apply(const char *profname, struct bfd_session *bs);
734
735/**
736 * Remove any applied profile from session and revert the session
737 * configuration.
738 *
739 * \param bs the BFD session.
740 */
741void bfd_profile_remove(struct bfd_session *bs);
742
743/**
744 * Apply new profile values to sessions using it.
745 *
746 * \param[in] bp the BFD profile that got updated.
747 */
748void bfd_profile_update(struct bfd_profile *bp);
e9e2c950 749
c2f29cf3
RZ
750/*
751 * bfdd_vty.c
752 *
753 * BFD daemon vty shell commands.
754 */
755void bfdd_vty_init(void);
756
757
adc26455
RZ
758/*
759 * bfdd_cli.c
760 *
761 * BFD daemon CLI implementation.
762 */
763void bfdd_cli_init(void);
764
765
d3af6147
RZ
766/*
767 * ptm_adapter.c
768 */
769void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv);
770void bfdd_zclient_stop(void);
54aadda1
PG
771void bfdd_zclient_unregister(vrf_id_t vrf_id);
772void bfdd_zclient_register(vrf_id_t vrf_id);
d24af713
PG
773void bfdd_sessions_enable_vrf(struct vrf *vrf);
774void bfdd_sessions_disable_vrf(struct vrf *vrf);
d3af6147 775
7555dc61 776int ptm_bfd_notify(struct bfd_session *bs, uint8_t notify_state);
d3af6147 777
230aefe2
RZ
778/*
779 * dplane.c
780 */
781
782/**
783 * Initialize BFD data plane infrastructure for distributed BFD implementation.
784 *
6655b43d
RZ
785 * \param sa socket address.
786 * \param salen socket address structure length.
787 * \param client `true` means connecting socket, `false` listening socket.
230aefe2 788 */
6655b43d 789void bfd_dplane_init(const struct sockaddr *sa, socklen_t salen, bool client);
230aefe2 790
efd04d60
RZ
791/**
792 * Attempts to delegate the BFD session liveness detection to hardware.
793 *
794 * \param bs the BFD session data structure.
795 *
796 * \returns
797 * `0` on success and BFD daemon should do nothing or `-1` on failure
798 * and we should fallback to software implementation.
799 */
800int bfd_dplane_add_session(struct bfd_session *bs);
801
802/**
803 * Send new session settings to data plane.
804 *
805 * \param bs the BFD session to update.
806 */
807int bfd_dplane_update_session(const struct bfd_session *bs);
808
809/**
810 * Deletes session from data plane.
811 *
812 * \param bs the BFD session to delete.
813 *
814 * \returns `0` on success otherwise `-1`.
815 */
816int bfd_dplane_delete_session(struct bfd_session *bs);
817
400632a9
RZ
818/**
819 * Asks the data plane for updated counters and update the session data
820 * structure.
821 *
822 * \param bs the BFD session that needs updating.
823 *
824 * \returns `0` on success otherwise `-1` on failure.
825 */
826int bfd_dplane_update_session_counters(struct bfd_session *bs);
827
828void bfd_dplane_show_counters(struct vty *vty);
829
e9e2c950 830#endif /* _BFD_H_ */