]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfd.h
bfdd: slow down on peer connection loss
[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"
80edb675 34#include "lib/vrf.h"
e9e2c950
RZ
35
36#include "bfdctl.h"
37
38#define ETHERNET_ADDRESS_LENGTH 6
39
40#ifdef BFD_DEBUG
41#define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
42#else
43#define BFDD_JSON_CONV_OPTIONS (0)
44#endif
45
46DECLARE_MGROUP(BFDD);
47DECLARE_MTYPE(BFDD_TMP);
48DECLARE_MTYPE(BFDD_CONFIG);
49DECLARE_MTYPE(BFDD_LABEL);
50DECLARE_MTYPE(BFDD_CONTROL);
51DECLARE_MTYPE(BFDD_NOTIFICATION);
52
53struct bfd_timers {
54 uint32_t desired_min_tx;
55 uint32_t required_min_rx;
56 uint32_t required_min_echo;
57};
58
59struct bfd_discrs {
60 uint32_t my_discr;
61 uint32_t remote_discr;
62};
63
64/*
65 * Format of control packet. From section 4)
66 */
67struct 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 */
84struct 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
e9e2c950
RZ
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_ECHO_VERSION 1
133#define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
e9e2c950 134
40675ea9
RZ
135enum bfd_diagnosticis {
136 BD_OK = 0,
137 /* Control Detection Time Expired. */
138 BD_CONTROL_EXPIRED = 1,
139 /* Echo Function Failed. */
140 BD_ECHO_FAILED = 2,
141 /* Neighbor Signaled Session Down. */
142 BD_NEIGHBOR_DOWN = 3,
143 /* Forwarding Plane Reset. */
144 BD_FORWARDING_RESET = 4,
145 /* Path Down. */
146 BD_PATH_DOWN = 5,
147 /* Concatenated Path Down. */
148 BD_CONCATPATH_DOWN = 6,
149 /* Administratively Down. */
150 BD_ADMIN_DOWN = 7,
151 /* Reverse Concatenated Path Down. */
152 BD_REVCONCATPATH_DOWN = 8,
153 /* 9..31: reserved. */
154};
155
e9e2c950
RZ
156/* BFD session flags */
157enum bfd_session_flags {
158 BFD_SESS_FLAG_NONE = 0,
159 BFD_SESS_FLAG_ECHO = 1 << 0, /* BFD Echo functionality */
160 BFD_SESS_FLAG_ECHO_ACTIVE = 1 << 1, /* BFD Echo Packets are being sent
161 * actively
162 */
163 BFD_SESS_FLAG_MH = 1 << 2, /* BFD Multi-hop session */
e9e2c950
RZ
164 BFD_SESS_FLAG_IPV6 = 1 << 4, /* BFD IPv6 session */
165 BFD_SESS_FLAG_SEND_EVT_ACTIVE = 1 << 5, /* send event timer active */
166 BFD_SESS_FLAG_SEND_EVT_IGNORE = 1 << 6, /* ignore send event when timer
167 * expires
168 */
169 BFD_SESS_FLAG_SHUTDOWN = 1 << 7, /* disable BGP peer function */
170};
171
172#define BFD_SET_FLAG(field, flag) (field |= flag)
173#define BFD_UNSET_FLAG(field, flag) (field &= ~flag)
174#define BFD_CHECK_FLAG(field, flag) (field & flag)
175
176/* BFD session hash keys */
177struct bfd_shop_key {
178 struct sockaddr_any peer;
179 char port_name[MAXNAMELEN + 1];
180};
181
182struct bfd_mhop_key {
183 struct sockaddr_any peer;
184 struct sockaddr_any local;
185 char vrf_name[MAXNAMELEN + 1];
186};
187
188struct bfd_session_stats {
189 uint64_t rx_ctrl_pkt;
190 uint64_t tx_ctrl_pkt;
191 uint64_t rx_echo_pkt;
192 uint64_t tx_echo_pkt;
0684c9b1
RZ
193 uint64_t session_up;
194 uint64_t session_down;
195 uint64_t znotification;
e9e2c950
RZ
196};
197
e9e2c950
RZ
198/* bfd_session shortcut label forwarding. */
199struct peer_label;
200
201/*
202 * Session state information
203 */
204struct bfd_session {
205
206 /* protocol state per RFC 5880*/
207 uint8_t ses_state;
208 struct bfd_discrs discrs;
209 uint8_t local_diag;
210 uint8_t demand_mode;
211 uint8_t detect_mult;
212 uint8_t remote_detect_mult;
213 uint8_t mh_ttl;
214
215 /* Timers */
216 struct bfd_timers timers;
f43b9368 217 struct bfd_timers cur_timers;
e9e2c950
RZ
218 uint64_t detect_TO;
219 struct thread *echo_recvtimer_ev;
220 struct thread *recvtimer_ev;
221 uint64_t xmt_TO;
222 uint64_t echo_xmt_TO;
223 struct thread *xmttimer_ev;
224 struct thread *echo_xmttimer_ev;
225 uint64_t echo_detect_TO;
226
227 /* software object state */
228 uint8_t polling;
229
230 /* This and the localDiscr are the keys to state info */
231 struct peer_label *pl;
232 union {
233 struct bfd_shop_key shop;
234 struct bfd_mhop_key mhop;
235 };
236 int sock;
237
238 struct sockaddr_any local_address;
239 struct sockaddr_any local_ip;
80edb675 240 struct interface *ifp;
e9e2c950
RZ
241 uint8_t local_mac[ETHERNET_ADDRESS_LENGTH];
242 uint8_t peer_mac[ETHERNET_ADDRESS_LENGTH];
e9e2c950
RZ
243
244 /* BFD session flags */
245 enum bfd_session_flags flags;
246
e9e2c950 247 struct bfd_session_stats stats;
e9e2c950
RZ
248
249 struct timeval uptime; /* last up time */
250 struct timeval downtime; /* last down time */
251
252 /* Remote peer data (for debugging mostly) */
253 uint8_t remote_diag;
254 struct bfd_timers remote_timers;
255
256 uint64_t refcount; /* number of pointers referencing this. */
257
258 /* VTY context data. */
259 QOBJ_FIELDS;
260};
261DECLARE_QOBJ_TYPE(bfd_session);
262
263struct peer_label {
264 TAILQ_ENTRY(peer_label) pl_entry;
265
266 struct bfd_session *pl_bs;
267 char pl_label[MAXNAMELEN];
268};
269TAILQ_HEAD(pllist, peer_label);
270
271struct bfd_diag_str_list {
272 const char *str;
273 int type;
274};
275
276struct bfd_state_str_list {
277 const char *str;
278 int type;
279};
280
281struct bfd_vrf {
282 int vrf_id;
283 char name[MAXNAMELEN + 1];
284} bfd_vrf;
285
286struct bfd_iface {
287 int vrf_id;
288 char ifname[MAXNAMELEN + 1];
289} bfd_iface;
290
291
292/* States defined per 4.1 */
293#define PTM_BFD_ADM_DOWN 0
294#define PTM_BFD_DOWN 1
295#define PTM_BFD_INIT 2
296#define PTM_BFD_UP 3
297
298
299/* Various constants */
300/* Retrieved from ptm_timer.h from Cumulus PTM sources. */
301#define MSEC_PER_SEC 1000L
302#define NSEC_PER_MSEC 1000000L
303
304#define BFD_DEF_DEMAND 0
305#define BFD_DEFDETECTMULT 3
306#define BFD_DEFDESIREDMINTX (300 * MSEC_PER_SEC)
307#define BFD_DEFREQUIREDMINRX (300 * MSEC_PER_SEC)
308#define BFD_DEF_REQ_MIN_ECHO (50 * MSEC_PER_SEC)
1f2d5f91 309#define BFD_DEF_SLOWTX (1000 * MSEC_PER_SEC)
e9e2c950
RZ
310#define BFD_DEF_MHOP_TTL 5
311#define BFD_PKT_LEN 24 /* Length of control packet */
312#define BFD_TTL_VAL 255
313#define BFD_RCV_TTL_VAL 1
314#define BFD_TOS_VAL 0xC0
315#define BFD_PKT_INFO_VAL 1
316#define BFD_IPV6_PKT_INFO_VAL 1
317#define BFD_IPV6_ONLY_VAL 1
318#define BFD_SRCPORTINIT 49142
319#define BFD_SRCPORTMAX 65536
320#define BFD_DEFDESTPORT 3784
321#define BFD_DEF_ECHO_PORT 3785
322#define BFD_DEF_MHOP_DEST_PORT 4784
323#define BFD_CMD_STRING_LEN (MAXNAMELEN + 50)
324#define BFD_BUFFER_LEN (BFD_CMD_STRING_LEN + MAXNAMELEN + 1)
325
326/*
327 * control.c
328 *
329 * Daemon control code to speak with local consumers.
330 */
331
332/* See 'bfdctrl.h' for client protocol definitions. */
333
334struct bfd_control_buffer {
335 size_t bcb_left;
336 size_t bcb_pos;
337 union {
338 struct bfd_control_msg *bcb_bcm;
339 uint8_t *bcb_buf;
340 };
341};
342
343struct bfd_control_queue {
344 TAILQ_ENTRY(bfd_control_queue) bcq_entry;
345
346 struct bfd_control_buffer bcq_bcb;
347};
348TAILQ_HEAD(bcqueue, bfd_control_queue);
349
350struct bfd_notify_peer {
351 TAILQ_ENTRY(bfd_notify_peer) bnp_entry;
352
353 struct bfd_session *bnp_bs;
354};
355TAILQ_HEAD(bnplist, bfd_notify_peer);
356
357struct bfd_control_socket {
358 TAILQ_ENTRY(bfd_control_socket) bcs_entry;
359
360 int bcs_sd;
361 struct thread *bcs_ev;
362 struct thread *bcs_outev;
363 struct bcqueue bcs_bcqueue;
364
365 /* Notification data */
366 uint64_t bcs_notify;
367 struct bnplist bcs_bnplist;
368
369 enum bc_msg_version bcs_version;
370 enum bc_msg_type bcs_type;
371
372 /* Message buffering */
373 struct bfd_control_buffer bcs_bin;
374 struct bfd_control_buffer *bcs_bout;
375};
376TAILQ_HEAD(bcslist, bfd_control_socket);
377
378int control_init(const char *path);
379void control_shutdown(void);
380int control_notify(struct bfd_session *bs);
381int control_notify_config(const char *op, struct bfd_session *bs);
382int control_accept(struct thread *t);
383
384
385/*
386 * bfdd.c
387 *
388 * Daemon specific code.
389 */
390struct bfd_global {
391 int bg_shop;
392 int bg_mhop;
393 int bg_shop6;
394 int bg_mhop6;
395 int bg_echo;
2f11c53f 396 int bg_echov6;
e9e2c950
RZ
397 struct thread *bg_ev[6];
398
399 int bg_csock;
400 struct thread *bg_csockev;
401 struct bcslist bg_bcslist;
402
403 struct pllist bg_pllist;
404};
405extern struct bfd_global bglobal;
406extern struct bfd_diag_str_list diag_list[];
407extern struct bfd_state_str_list state_list[];
408
409void socket_close(int *s);
410
411
412/*
413 * config.c
414 *
415 * Contains the code related with loading/reloading configuration.
416 */
417int parse_config(const char *fname);
418int config_request_add(const char *jsonstr);
419int config_request_del(const char *jsonstr);
420char *config_response(const char *status, const char *error);
421char *config_notify(struct bfd_session *bs);
422char *config_notify_config(const char *op, struct bfd_session *bs);
423
424typedef int (*bpc_handle)(struct bfd_peer_cfg *, void *arg);
425int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
426 bpc_handle bh);
427
428struct peer_label *pl_new(const char *label, struct bfd_session *bs);
429struct peer_label *pl_find(const char *label);
430void 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 */
439enum 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
448void log_init(int foreground, enum blog_level level,
449 struct frr_daemon_info *fdi);
450void log_info(const char *fmt, ...);
451void log_debug(const char *fmt, ...);
452void log_warning(const char *fmt, ...);
453void log_error(const char *fmt, ...);
454void log_fatal(const char *fmt, ...);
455
e9e2c950
RZ
456
457/*
458 * bfd_packet.c
459 *
460 * Contains the code related with receiving/seding, packing/unpacking BFD data.
461 */
6e01e275
RZ
462int bp_set_ttlv6(int sd, uint8_t value);
463int bp_set_ttl(int sd, uint8_t value);
464int bp_set_tosv6(int sd, uint8_t value);
465int bp_set_tos(int sd, uint8_t value);
e9e2c950
RZ
466int bp_bind_dev(int sd, const char *dev);
467
468int bp_udp_shop(void);
469int bp_udp_mhop(void);
470int bp_udp6_shop(void);
471int bp_udp6_mhop(void);
472int bp_peer_socket(struct bfd_peer_cfg *bpc);
473int bp_peer_socketv6(struct bfd_peer_cfg *bpc);
2f11c53f
RZ
474int bp_echo_socket(void);
475int bp_echov6_socket(void);
e9e2c950
RZ
476
477void ptm_bfd_snd(struct bfd_session *bfd, int fbit);
478void ptm_bfd_echo_snd(struct bfd_session *bfd);
479
480int bfd_recv_cb(struct thread *t);
481
e9e2c950
RZ
482
483/*
484 * event.c
485 *
486 * Contains the code related with event loop.
487 */
488typedef void (*bfd_ev_cb)(struct thread *t);
489
490void bfd_recvtimer_update(struct bfd_session *bs);
491void bfd_echo_recvtimer_update(struct bfd_session *bs);
492void bfd_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
493void bfd_echo_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
494
495void bfd_xmttimer_delete(struct bfd_session *bs);
496void bfd_echo_xmttimer_delete(struct bfd_session *bs);
497void bfd_recvtimer_delete(struct bfd_session *bs);
498void bfd_echo_recvtimer_delete(struct bfd_session *bs);
499
500void bfd_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
501void bfd_echo_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
502void bfd_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
503void 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 */
511struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc);
512int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc);
513void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag);
514void ptm_bfd_ses_up(struct bfd_session *bfd);
8bd859f6 515void ptm_bfd_echo_stop(struct bfd_session *bfd);
e9e2c950
RZ
516void ptm_bfd_echo_start(struct bfd_session *bfd);
517void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit);
518void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo);
519struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name,
520 struct sockaddr_any *peer,
521 struct sockaddr_any *local,
522 char *vrf_name, bool is_mhop);
523
524struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc);
525int bfd_session_update_label(struct bfd_session *bs, const char *nlabel);
d3f3a2c4 526void bfd_set_polling(struct bfd_session *bs);
aef131af 527void bs_state_handler(struct bfd_session *, int);
c0ef9a8a
RZ
528void bs_echo_timer_handler(struct bfd_session *);
529void bs_final_handler(struct bfd_session *);
b912b189 530void bs_set_slow_timers(struct bfd_session *bs);
e9e2c950
RZ
531const char *satostr(struct sockaddr_any *sa);
532const char *diag2str(uint8_t diag);
533int strtosa(const char *addr, struct sockaddr_any *sa);
534void integer2timestr(uint64_t time, char *buf, size_t buflen);
03e7f088 535const char *bs_to_string(struct bfd_session *bs);
e9e2c950
RZ
536
537/* BFD hash data structures interface */
538void bfd_initialize(void);
539void bfd_shutdown(void);
540struct bfd_session *bfd_id_lookup(uint32_t id);
541struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop);
542struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop);
543struct bfd_vrf *bfd_vrf_lookup(int vrf_id);
544struct bfd_iface *bfd_iface_lookup(const char *ifname);
545
546struct bfd_session *bfd_id_delete(uint32_t id);
547struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop);
548struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop);
549struct bfd_vrf *bfd_vrf_delete(int vrf_id);
550struct bfd_iface *bfd_iface_delete(const char *ifname);
551
552bool bfd_id_insert(struct bfd_session *bs);
553bool bfd_shop_insert(struct bfd_session *bs);
554bool bfd_mhop_insert(struct bfd_session *bs);
555bool bfd_vrf_insert(struct bfd_vrf *vrf);
556bool bfd_iface_insert(struct bfd_iface *iface);
557
558typedef void (*hash_iter_func)(struct hash_backet *hb, void *arg);
559void bfd_id_iterate(hash_iter_func hif, void *arg);
560void bfd_shop_iterate(hash_iter_func hif, void *arg);
561void bfd_mhop_iterate(hash_iter_func hif, void *arg);
562void bfd_vrf_iterate(hash_iter_func hif, void *arg);
563void bfd_iface_iterate(hash_iter_func hif, void *arg);
564
565/* Export callback functions for `event.c`. */
566extern struct thread_master *master;
567
568int bfd_recvtimer_cb(struct thread *t);
569int bfd_echo_recvtimer_cb(struct thread *t);
570int bfd_xmt_cb(struct thread *t);
571int bfd_echo_xmt_cb(struct thread *t);
572
573
c2f29cf3
RZ
574/*
575 * bfdd_vty.c
576 *
577 * BFD daemon vty shell commands.
578 */
579void bfdd_vty_init(void);
580
581
d3af6147
RZ
582/*
583 * ptm_adapter.c
584 */
585void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv);
586void bfdd_zclient_stop(void);
587
588int ptm_bfd_notify(struct bfd_session *bs);
589
e9e2c950 590#endif /* _BFD_H_ */