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