]> git.proxmox.com Git - mirror_frr.git/blob - bfdd/bfd.h
Merge pull request #4810 from qlyoung/fix-pthread-bad-pointer
[mirror_frr.git] / bfdd / bfd.h
1 /*********************************************************************
2 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * bfd.h: implements the BFD protocol.
19 */
20
21 #ifndef _BFD_H_
22 #define _BFD_H_
23
24 #include <netinet/in.h>
25
26 #include <stdbool.h>
27 #include <stdarg.h>
28 #include <stdint.h>
29
30 #include "lib/hash.h"
31 #include "lib/libfrr.h"
32 #include "lib/qobj.h"
33 #include "lib/queue.h"
34 #include "lib/vrf.h"
35
36 #include "bfdctl.h"
37
38 #ifdef BFD_DEBUG
39 #define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
40 #else
41 #define BFDD_JSON_CONV_OPTIONS (0)
42 #endif
43
44 DECLARE_MGROUP(BFDD)
45 DECLARE_MTYPE(BFDD_CONTROL)
46 DECLARE_MTYPE(BFDD_NOTIFICATION)
47
48 struct bfd_timers {
49 uint32_t desired_min_tx;
50 uint32_t required_min_rx;
51 uint32_t required_min_echo;
52 };
53
54 struct bfd_discrs {
55 uint32_t my_discr;
56 uint32_t remote_discr;
57 };
58
59 /*
60 * Format of control packet. From section 4)
61 */
62 struct bfd_pkt {
63 union {
64 uint32_t byteFields;
65 struct {
66 uint8_t diag;
67 uint8_t flags;
68 uint8_t detect_mult;
69 uint8_t len;
70 };
71 };
72 struct bfd_discrs discrs;
73 struct bfd_timers timers;
74 };
75
76 /*
77 * Format of Echo packet.
78 */
79 struct bfd_echo_pkt {
80 union {
81 uint32_t byteFields;
82 struct {
83 uint8_t ver;
84 uint8_t len;
85 uint16_t reserved;
86 };
87 };
88 uint32_t my_discr;
89 uint8_t pad[16];
90 };
91
92
93 /* Macros for manipulating control packets */
94 #define BFD_VERMASK 0x03
95 #define BFD_DIAGMASK 0x1F
96 #define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
97 #define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
98 #define BFD_VERSION 1
99 #define BFD_PBIT 0x20
100 #define BFD_FBIT 0x10
101 #define BFD_CBIT 0x08
102 #define BFD_ABIT 0x04
103 #define BFD_DEMANDBIT 0x02
104 #define BFD_SETDEMANDBIT(flags, val) \
105 { \
106 if ((val)) \
107 flags |= BFD_DEMANDBIT; \
108 }
109 #define BFD_SETPBIT(flags, val) \
110 { \
111 if ((val)) \
112 flags |= BFD_PBIT; \
113 }
114 #define BFD_GETPBIT(flags) (flags & BFD_PBIT)
115 #define BFD_SETFBIT(flags, val) \
116 { \
117 if ((val)) \
118 flags |= BFD_FBIT; \
119 }
120 #define BFD_GETFBIT(flags) (flags & BFD_FBIT)
121 #define BFD_SETSTATE(flags, val) \
122 { \
123 if ((val)) \
124 flags |= (val & 0x3) << 6; \
125 }
126 #define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
127 #define BFD_SETCBIT(flags, val) \
128 { \
129 if ((val)) \
130 flags |= val; \
131 }
132 #define BFD_GETCBIT(flags) (flags & BFD_FBIT)
133 #define BFD_ECHO_VERSION 1
134 #define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
135
136 enum bfd_diagnosticis {
137 BD_OK = 0,
138 /* Control Detection Time Expired. */
139 BD_CONTROL_EXPIRED = 1,
140 /* Echo Function Failed. */
141 BD_ECHO_FAILED = 2,
142 /* Neighbor Signaled Session Down. */
143 BD_NEIGHBOR_DOWN = 3,
144 /* Forwarding Plane Reset. */
145 BD_FORWARDING_RESET = 4,
146 /* Path Down. */
147 BD_PATH_DOWN = 5,
148 /* Concatenated Path Down. */
149 BD_CONCATPATH_DOWN = 6,
150 /* Administratively Down. */
151 BD_ADMIN_DOWN = 7,
152 /* Reverse Concatenated Path Down. */
153 BD_REVCONCATPATH_DOWN = 8,
154 /* 9..31: reserved. */
155 };
156
157 /* BFD session flags */
158 enum bfd_session_flags {
159 BFD_SESS_FLAG_NONE = 0,
160 BFD_SESS_FLAG_ECHO = 1 << 0, /* BFD Echo functionality */
161 BFD_SESS_FLAG_ECHO_ACTIVE = 1 << 1, /* BFD Echo Packets are being sent
162 * actively
163 */
164 BFD_SESS_FLAG_MH = 1 << 2, /* BFD Multi-hop session */
165 BFD_SESS_FLAG_IPV6 = 1 << 4, /* BFD IPv6 session */
166 BFD_SESS_FLAG_SEND_EVT_ACTIVE = 1 << 5, /* send event timer active */
167 BFD_SESS_FLAG_SEND_EVT_IGNORE = 1 << 6, /* ignore send event when timer
168 * expires
169 */
170 BFD_SESS_FLAG_SHUTDOWN = 1 << 7, /* disable BGP peer function */
171 BFD_SESS_FLAG_CONFIG = 1 << 8, /* Session configured with bfd NB API */
172 BFD_SESS_FLAG_CBIT = 1 << 9, /* CBIT is set */
173 };
174
175 #define BFD_SET_FLAG(field, flag) (field |= flag)
176 #define BFD_UNSET_FLAG(field, flag) (field &= ~flag)
177 #define BFD_CHECK_FLAG(field, flag) (field & flag)
178
179 /* BFD session hash keys */
180 struct bfd_key {
181 uint16_t family;
182 uint8_t mhop;
183 struct in6_addr peer;
184 struct in6_addr local;
185 char ifname[MAXNAMELEN];
186 char vrfname[MAXNAMELEN];
187 };
188
189 struct bfd_session_stats {
190 uint64_t rx_ctrl_pkt;
191 uint64_t tx_ctrl_pkt;
192 uint64_t rx_echo_pkt;
193 uint64_t tx_echo_pkt;
194 uint64_t session_up;
195 uint64_t session_down;
196 uint64_t znotification;
197 };
198
199 /* bfd_session shortcut label forwarding. */
200 struct peer_label;
201
202 /*
203 * Session state information
204 */
205 struct bfd_session {
206
207 /* protocol state per RFC 5880*/
208 uint8_t ses_state;
209 struct bfd_discrs discrs;
210 uint8_t local_diag;
211 uint8_t demand_mode;
212 uint8_t detect_mult;
213 uint8_t remote_detect_mult;
214 uint8_t mh_ttl;
215 uint8_t remote_cbit;
216
217 /* Timers */
218 struct bfd_timers timers;
219 struct bfd_timers cur_timers;
220 uint64_t detect_TO;
221 struct thread *echo_recvtimer_ev;
222 struct thread *recvtimer_ev;
223 uint64_t xmt_TO;
224 uint64_t echo_xmt_TO;
225 struct thread *xmttimer_ev;
226 struct thread *echo_xmttimer_ev;
227 uint64_t echo_detect_TO;
228
229 /* software object state */
230 uint8_t polling;
231
232 /* This and the localDiscr are the keys to state info */
233 struct bfd_key key;
234 struct peer_label *pl;
235
236 struct sockaddr_any local_address;
237 struct interface *ifp;
238 struct vrf *vrf;
239
240 int sock;
241
242 /* BFD session flags */
243 enum bfd_session_flags flags;
244
245 struct bfd_session_stats stats;
246
247 struct timeval uptime; /* last up time */
248 struct timeval downtime; /* last down time */
249
250 /* Remote peer data (for debugging mostly) */
251 uint8_t remote_diag;
252 struct bfd_timers remote_timers;
253
254 uint64_t refcount; /* number of pointers referencing this. */
255 };
256
257 struct peer_label {
258 TAILQ_ENTRY(peer_label) pl_entry;
259
260 struct bfd_session *pl_bs;
261 char pl_label[MAXNAMELEN];
262 };
263 TAILQ_HEAD(pllist, peer_label);
264
265 struct bfd_diag_str_list {
266 const char *str;
267 int type;
268 };
269
270 struct bfd_state_str_list {
271 const char *str;
272 int type;
273 };
274
275 struct bfd_session_observer {
276 struct bfd_session *bso_bs;
277 bool bso_isinterface;
278 bool bso_isaddress;
279 union {
280 char bso_entryname[MAXNAMELEN];
281 struct prefix bso_addr;
282 };
283
284 TAILQ_ENTRY(bfd_session_observer) bso_entry;
285 };
286 TAILQ_HEAD(obslist, bfd_session_observer);
287
288
289 /* States defined per 4.1 */
290 #define PTM_BFD_ADM_DOWN 0
291 #define PTM_BFD_DOWN 1
292 #define PTM_BFD_INIT 2
293 #define PTM_BFD_UP 3
294
295
296 /* Various constants */
297 /* Retrieved from ptm_timer.h from Cumulus PTM sources. */
298 #define BFD_DEF_DEMAND 0
299 #define BFD_DEFDETECTMULT 3
300 #define BFD_DEFDESIREDMINTX (300 * 1000) /* microseconds. */
301 #define BFD_DEFREQUIREDMINRX (300 * 1000) /* microseconds. */
302 #define BFD_DEF_REQ_MIN_ECHO (50 * 1000) /* microseconds. */
303 #define BFD_DEF_SLOWTX (1000 * 1000) /* microseconds. */
304 #define BFD_DEF_MHOP_TTL 5
305 #define BFD_PKT_LEN 24 /* Length of control packet */
306 #define BFD_TTL_VAL 255
307 #define BFD_RCV_TTL_VAL 1
308 #define BFD_TOS_VAL 0xC0
309 #define BFD_PKT_INFO_VAL 1
310 #define BFD_IPV6_PKT_INFO_VAL 1
311 #define BFD_IPV6_ONLY_VAL 1
312 #define BFD_SRCPORTINIT 49152
313 #define BFD_SRCPORTMAX 65535
314 #define BFD_DEFDESTPORT 3784
315 #define BFD_DEF_ECHO_PORT 3785
316 #define BFD_DEF_MHOP_DEST_PORT 4784
317
318 /*
319 * control.c
320 *
321 * Daemon control code to speak with local consumers.
322 */
323
324 /* See 'bfdctrl.h' for client protocol definitions. */
325
326 struct bfd_control_buffer {
327 size_t bcb_left;
328 size_t bcb_pos;
329 union {
330 struct bfd_control_msg *bcb_bcm;
331 uint8_t *bcb_buf;
332 };
333 };
334
335 struct bfd_control_queue {
336 TAILQ_ENTRY(bfd_control_queue) bcq_entry;
337
338 struct bfd_control_buffer bcq_bcb;
339 };
340 TAILQ_HEAD(bcqueue, bfd_control_queue);
341
342 struct bfd_notify_peer {
343 TAILQ_ENTRY(bfd_notify_peer) bnp_entry;
344
345 struct bfd_session *bnp_bs;
346 };
347 TAILQ_HEAD(bnplist, bfd_notify_peer);
348
349 struct bfd_control_socket {
350 TAILQ_ENTRY(bfd_control_socket) bcs_entry;
351
352 int bcs_sd;
353 struct thread *bcs_ev;
354 struct thread *bcs_outev;
355 struct bcqueue bcs_bcqueue;
356
357 /* Notification data */
358 uint64_t bcs_notify;
359 struct bnplist bcs_bnplist;
360
361 enum bc_msg_version bcs_version;
362 enum bc_msg_type bcs_type;
363
364 /* Message buffering */
365 struct bfd_control_buffer bcs_bin;
366 struct bfd_control_buffer *bcs_bout;
367 };
368 TAILQ_HEAD(bcslist, bfd_control_socket);
369
370 int control_init(const char *path);
371 void control_shutdown(void);
372 int control_notify(struct bfd_session *bs);
373 int control_notify_config(const char *op, struct bfd_session *bs);
374 int control_accept(struct thread *t);
375
376
377 /*
378 * bfdd.c
379 *
380 * Daemon specific code.
381 */
382 struct bfd_vrf_global {
383 int bg_shop;
384 int bg_mhop;
385 int bg_shop6;
386 int bg_mhop6;
387 int bg_echo;
388 int bg_echov6;
389 struct vrf *vrf;
390
391 struct thread *bg_ev[6];
392 };
393
394 struct bfd_global {
395 int bg_csock;
396 struct thread *bg_csockev;
397 struct bcslist bg_bcslist;
398
399 struct pllist bg_pllist;
400
401 struct obslist bg_obslist;
402
403 struct zebra_privs_t bfdd_privs;
404 };
405 extern struct bfd_global bglobal;
406 extern struct bfd_diag_str_list diag_list[];
407 extern struct bfd_state_str_list state_list[];
408
409 void socket_close(int *s);
410
411
412 /*
413 * config.c
414 *
415 * Contains the code related with loading/reloading configuration.
416 */
417 int parse_config(const char *fname);
418 int config_request_add(const char *jsonstr);
419 int config_request_del(const char *jsonstr);
420 char *config_response(const char *status, const char *error);
421 char *config_notify(struct bfd_session *bs);
422 char *config_notify_config(const char *op, struct bfd_session *bs);
423
424 typedef int (*bpc_handle)(struct bfd_peer_cfg *, void *arg);
425 int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
426 bpc_handle bh);
427
428 struct peer_label *pl_new(const char *label, struct bfd_session *bs);
429 struct peer_label *pl_find(const char *label);
430 void pl_free(struct peer_label *pl);
431
432
433 /*
434 * log.c
435 *
436 * Contains code that does the logging procedures. Might implement multiple
437 * backends (e.g. zebra log, syslog or other logging lib).
438 */
439 enum blog_level {
440 /* level vs syslog equivalent */
441 BLOG_DEBUG = 0, /* LOG_DEBUG */
442 BLOG_INFO = 1, /* LOG_INFO */
443 BLOG_WARNING = 2, /* LOG_WARNING */
444 BLOG_ERROR = 3, /* LOG_ERR */
445 BLOG_FATAL = 4, /* LOG_CRIT */
446 };
447
448 void log_init(int foreground, enum blog_level level,
449 struct frr_daemon_info *fdi);
450 void log_info(const char *fmt, ...);
451 void log_debug(const char *fmt, ...);
452 void log_warning(const char *fmt, ...);
453 void log_error(const char *fmt, ...);
454 void log_fatal(const char *fmt, ...);
455
456
457 /*
458 * bfd_packet.c
459 *
460 * Contains the code related with receiving/seding, packing/unpacking BFD data.
461 */
462 int bp_set_ttlv6(int sd, uint8_t value);
463 int bp_set_ttl(int sd, uint8_t value);
464 int bp_set_tosv6(int sd, uint8_t value);
465 int bp_set_tos(int sd, uint8_t value);
466 int bp_bind_dev(int sd, const char *dev);
467
468 int bp_udp_shop(vrf_id_t vrf_id);
469 int bp_udp_mhop(vrf_id_t vrf_id);
470 int bp_udp6_shop(vrf_id_t vrf_id);
471 int bp_udp6_mhop(vrf_id_t vrf_id);
472 int bp_peer_socket(const struct bfd_session *bs);
473 int bp_peer_socketv6(const struct bfd_session *bs);
474 int bp_echo_socket(vrf_id_t vrf_id);
475 int bp_echov6_socket(vrf_id_t vrf_id);
476
477 void ptm_bfd_snd(struct bfd_session *bfd, int fbit);
478 void ptm_bfd_echo_snd(struct bfd_session *bfd);
479
480 int bfd_recv_cb(struct thread *t);
481
482
483 /*
484 * event.c
485 *
486 * Contains the code related with event loop.
487 */
488 typedef void (*bfd_ev_cb)(struct thread *t);
489
490 void bfd_recvtimer_update(struct bfd_session *bs);
491 void bfd_echo_recvtimer_update(struct bfd_session *bs);
492 void bfd_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
493 void bfd_echo_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
494
495 void bfd_xmttimer_delete(struct bfd_session *bs);
496 void bfd_echo_xmttimer_delete(struct bfd_session *bs);
497 void bfd_recvtimer_delete(struct bfd_session *bs);
498 void bfd_echo_recvtimer_delete(struct bfd_session *bs);
499
500 void bfd_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
501 void bfd_echo_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
502 void bfd_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
503 void bfd_echo_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
504
505
506 /*
507 * bfd.c
508 *
509 * BFD protocol specific code.
510 */
511 int bfd_session_enable(struct bfd_session *bs);
512 void bfd_session_disable(struct bfd_session *bs);
513 struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc);
514 int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc);
515 void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag);
516 void ptm_bfd_sess_up(struct bfd_session *bfd);
517 void ptm_bfd_echo_stop(struct bfd_session *bfd);
518 void ptm_bfd_echo_start(struct bfd_session *bfd);
519 void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit);
520 void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo);
521 struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp,
522 struct sockaddr_any *peer,
523 struct sockaddr_any *local,
524 ifindex_t ifindex, vrf_id_t vrfid,
525 bool is_mhop);
526
527 struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc);
528 int bfd_session_update_label(struct bfd_session *bs, const char *nlabel);
529 void bfd_set_polling(struct bfd_session *bs);
530 void bs_state_handler(struct bfd_session *bs, int nstate);
531 void bs_echo_timer_handler(struct bfd_session *bs);
532 void bs_final_handler(struct bfd_session *bs);
533 void bs_set_slow_timers(struct bfd_session *bs);
534 const char *satostr(struct sockaddr_any *sa);
535 const char *diag2str(uint8_t diag);
536 int strtosa(const char *addr, struct sockaddr_any *sa);
537 void integer2timestr(uint64_t time, char *buf, size_t buflen);
538 const char *bs_to_string(const struct bfd_session *bs);
539
540 int bs_observer_add(struct bfd_session *bs);
541 void bs_observer_del(struct bfd_session_observer *bso);
542
543 void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc);
544
545 void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer,
546 struct sockaddr_any *local, bool mhop, const char *ifname,
547 const char *vrfname);
548 struct bfd_session *bfd_session_new(void);
549 struct bfd_session *bs_registrate(struct bfd_session *bs);
550 void bfd_session_free(struct bfd_session *bs);
551 const struct bfd_session *bfd_session_next(const struct bfd_session *bs,
552 bool mhop);
553 void bfd_sessions_remove_manual(void);
554
555 /* BFD hash data structures interface */
556 void bfd_initialize(void);
557 void bfd_shutdown(void);
558 void bfd_vrf_init(void);
559 void bfd_vrf_terminate(void);
560 struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd);
561 struct bfd_session *bfd_id_lookup(uint32_t id);
562 struct bfd_session *bfd_key_lookup(struct bfd_key key);
563
564 struct bfd_session *bfd_id_delete(uint32_t id);
565 struct bfd_session *bfd_key_delete(struct bfd_key key);
566
567 bool bfd_id_insert(struct bfd_session *bs);
568 bool bfd_key_insert(struct bfd_session *bs);
569
570 typedef void (*hash_iter_func)(struct hash_bucket *hb, void *arg);
571 void bfd_id_iterate(hash_iter_func hif, void *arg);
572 void bfd_key_iterate(hash_iter_func hif, void *arg);
573
574 /* Export callback functions for `event.c`. */
575 extern struct thread_master *master;
576
577 int bfd_recvtimer_cb(struct thread *t);
578 int bfd_echo_recvtimer_cb(struct thread *t);
579 int bfd_xmt_cb(struct thread *t);
580 int bfd_echo_xmt_cb(struct thread *t);
581
582 extern struct in6_addr zero_addr;
583
584
585 /*
586 * bfdd_vty.c
587 *
588 * BFD daemon vty shell commands.
589 */
590 void bfdd_vty_init(void);
591
592
593 /*
594 * bfdd_cli.c
595 *
596 * BFD daemon CLI implementation.
597 */
598 void bfdd_cli_init(void);
599
600 void bfd_cli_show_header(struct vty *vty, struct lyd_node *dnode,
601 bool show_defaults);
602 void bfd_cli_show_header_end(struct vty *vty, struct lyd_node *dnode);
603 void bfd_cli_show_single_hop_peer(struct vty *vty,
604 struct lyd_node *dnode,
605 bool show_defaults);
606 void bfd_cli_show_multi_hop_peer(struct vty *vty,
607 struct lyd_node *dnode,
608 bool show_defaults);
609 void bfd_cli_show_peer_end(struct vty *vty, struct lyd_node *dnode);
610 void bfd_cli_show_mult(struct vty *vty, struct lyd_node *dnode,
611 bool show_defaults);
612 void bfd_cli_show_tx(struct vty *vty, struct lyd_node *dnode,
613 bool show_defaults);
614 void bfd_cli_show_rx(struct vty *vty, struct lyd_node *dnode,
615 bool show_defaults);
616 void bfd_cli_show_shutdown(struct vty *vty, struct lyd_node *dnode,
617 bool show_defaults);
618 void bfd_cli_show_echo(struct vty *vty, struct lyd_node *dnode,
619 bool show_defaults);
620 void bfd_cli_show_echo_interval(struct vty *vty, struct lyd_node *dnode,
621 bool show_defaults);
622
623
624 /*
625 * ptm_adapter.c
626 */
627 void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv);
628 void bfdd_zclient_stop(void);
629 void bfdd_zclient_unregister(vrf_id_t vrf_id);
630 void bfdd_zclient_register(vrf_id_t vrf_id);
631 void bfdd_sessions_enable_vrf(struct vrf *vrf);
632 void bfdd_sessions_disable_vrf(struct vrf *vrf);
633 void bfd_session_update_vrf_name(struct bfd_session *bs, struct vrf *vrf);
634
635 int ptm_bfd_notify(struct bfd_session *bs);
636
637
638 /*
639 * bfdd_northbound.c
640 *
641 * BFD northbound callbacks.
642 */
643 extern const struct frr_yang_module_info frr_bfdd_info;
644
645 #endif /* _BFD_H_ */