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