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