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