]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
Merge pull request #6628 from adharkar/frr-master-evpn_rt
[mirror_frr.git] / lib / bfd.c
1 /**
2 * bfd.c: BFD handling routines
3 *
4 * @copyright Copyright (C) 2015 Cumulus Networks, Inc.
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24
25 #include "command.h"
26 #include "memory.h"
27 #include "prefix.h"
28 #include "thread.h"
29 #include "stream.h"
30 #include "zclient.h"
31 #include "table.h"
32 #include "vty.h"
33 #include "bfd.h"
34
35 DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info")
36
37 static int bfd_debug = 0;
38 static struct bfd_gbl bfd_gbl;
39
40 /*
41 * bfd_gbl_init - Initialize the BFD global structure
42 */
43 void bfd_gbl_init(void)
44 {
45 memset(&bfd_gbl, 0, sizeof(struct bfd_gbl));
46 }
47
48 /*
49 * bfd_gbl_exit - Called when daemon exits
50 */
51 void bfd_gbl_exit(void)
52 {
53 SET_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN);
54 }
55
56 /*
57 * bfd_info_create - Allocate the BFD information
58 */
59 struct bfd_info *bfd_info_create(void)
60 {
61 struct bfd_info *bfd_info;
62
63 bfd_info = XCALLOC(MTYPE_BFD_INFO, sizeof(struct bfd_info));
64 assert(bfd_info);
65
66 bfd_info->status = BFD_STATUS_UNKNOWN;
67 bfd_info->type = BFD_TYPE_NOT_CONFIGURED;
68 bfd_info->last_update = 0;
69 return bfd_info;
70 }
71
72 /*
73 * bfd_info_free - Free the BFD information.
74 */
75 void bfd_info_free(struct bfd_info **bfd_info)
76 {
77 XFREE(MTYPE_BFD_INFO, *bfd_info);
78 }
79
80 /*
81 * bfd_validate_param - Validate the BFD paramter information.
82 */
83 int bfd_validate_param(struct vty *vty, const char *dm_str, const char *rx_str,
84 const char *tx_str, uint8_t *dm_val, uint32_t *rx_val,
85 uint32_t *tx_val)
86 {
87 *dm_val = strtoul(dm_str, NULL, 10);
88 *rx_val = strtoul(rx_str, NULL, 10);
89 *tx_val = strtoul(tx_str, NULL, 10);
90 return CMD_SUCCESS;
91 }
92
93 /*
94 * bfd_set_param - Set the configured BFD paramter values
95 */
96 void bfd_set_param(struct bfd_info **bfd_info, uint32_t min_rx, uint32_t min_tx,
97 uint8_t detect_mult, int defaults, int *command)
98 {
99 if (!*bfd_info) {
100 *bfd_info = bfd_info_create();
101 *command = ZEBRA_BFD_DEST_REGISTER;
102 } else {
103 if (((*bfd_info)->required_min_rx != min_rx)
104 || ((*bfd_info)->desired_min_tx != min_tx)
105 || ((*bfd_info)->detect_mult != detect_mult))
106 *command = ZEBRA_BFD_DEST_UPDATE;
107 }
108
109 if (*command) {
110 (*bfd_info)->required_min_rx = min_rx;
111 (*bfd_info)->desired_min_tx = min_tx;
112 (*bfd_info)->detect_mult = detect_mult;
113 }
114
115 if (!defaults)
116 SET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
117 else
118 UNSET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
119 }
120
121 /*
122 * bfd_peer_sendmsg - Format and send a peer register/Unregister
123 * command to Zebra to be forwarded to BFD
124 */
125 void bfd_peer_sendmsg(struct zclient *zclient, struct bfd_info *bfd_info,
126 int family, void *dst_ip, void *src_ip, char *if_name,
127 int ttl, int multihop, int cbit, int command,
128 int set_flag, vrf_id_t vrf_id)
129 {
130 struct bfd_session_arg args = {};
131 size_t addrlen;
132
133 /* Individual reg/dereg messages are suppressed during shutdown. */
134 if (CHECK_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN)) {
135 if (bfd_debug)
136 zlog_debug(
137 "%s: Suppressing BFD peer reg/dereg messages",
138 __func__);
139 return;
140 }
141
142 /* Check socket. */
143 if (!zclient || zclient->sock < 0) {
144 if (bfd_debug)
145 zlog_debug(
146 "%s: Can't send BFD peer register, Zebra client not "
147 "established",
148 __func__);
149 return;
150 }
151
152 /* Fill in all arguments. */
153 args.ttl = ttl;
154 args.cbit = cbit;
155 args.family = family;
156 args.mhop = multihop;
157 args.vrf_id = vrf_id;
158 args.command = command;
159 args.set_flag = set_flag;
160 args.bfd_info = bfd_info;
161 if (args.bfd_info) {
162 args.min_rx = bfd_info->required_min_rx;
163 args.min_tx = bfd_info->desired_min_tx;
164 args.detection_multiplier = bfd_info->detect_mult;
165 }
166
167 addrlen = family == AF_INET ? sizeof(struct in_addr)
168 : sizeof(struct in6_addr);
169 memcpy(&args.dst, dst_ip, addrlen);
170 if (src_ip)
171 memcpy(&args.src, src_ip, addrlen);
172
173 if (if_name)
174 args.ifnamelen =
175 strlcpy(args.ifname, if_name, sizeof(args.ifname));
176
177 zclient_bfd_command(zclient, &args);
178 }
179
180 /*
181 * bfd_get_command_dbg_str - Convert command to a debug string.
182 */
183 const char *bfd_get_command_dbg_str(int command)
184 {
185 switch (command) {
186 case ZEBRA_BFD_DEST_REGISTER:
187 return "Register";
188 case ZEBRA_BFD_DEST_DEREGISTER:
189 return "Deregister";
190 case ZEBRA_BFD_DEST_UPDATE:
191 return "Update";
192 default:
193 return "Unknown";
194 }
195 }
196
197 /*
198 * bfd_get_peer_info - Extract the Peer information for which the BFD session
199 * went down from the message sent from Zebra to clients.
200 */
201 struct interface *bfd_get_peer_info(struct stream *s, struct prefix *dp,
202 struct prefix *sp, int *status,
203 int *remote_cbit,
204 vrf_id_t vrf_id)
205 {
206 unsigned int ifindex;
207 struct interface *ifp = NULL;
208 int plen;
209 int local_remote_cbit;
210
211 /* Get interface index. */
212 ifindex = stream_getl(s);
213
214 /* Lookup index. */
215 if (ifindex != 0) {
216 ifp = if_lookup_by_index(ifindex, vrf_id);
217 if (ifp == NULL) {
218 if (bfd_debug)
219 zlog_debug(
220 "zebra_interface_bfd_read: "
221 "Can't find interface by ifindex: %d ",
222 ifindex);
223 return NULL;
224 }
225 }
226
227 /* Fetch destination address. */
228 dp->family = stream_getc(s);
229
230 plen = prefix_blen(dp);
231 stream_get(&dp->u.prefix, s, plen);
232 dp->prefixlen = stream_getc(s);
233
234 /* Get BFD status. */
235 *status = stream_getl(s);
236
237 if (sp) {
238 sp->family = stream_getc(s);
239
240 plen = prefix_blen(sp);
241 stream_get(&sp->u.prefix, s, plen);
242 sp->prefixlen = stream_getc(s);
243 }
244 local_remote_cbit = stream_getc(s);
245 if (remote_cbit)
246 *remote_cbit = local_remote_cbit;
247 return ifp;
248 }
249
250 /*
251 * bfd_get_status_str - Convert BFD status to a display string.
252 */
253 const char *bfd_get_status_str(int status)
254 {
255 switch (status) {
256 case BFD_STATUS_DOWN:
257 return "Down";
258 case BFD_STATUS_UP:
259 return "Up";
260 case BFD_STATUS_ADMIN_DOWN:
261 return "Admin Down";
262 case BFD_STATUS_UNKNOWN:
263 default:
264 return "Unknown";
265 }
266 }
267
268 /*
269 * bfd_last_update - Calculate the last BFD update time and convert it
270 * into a dd:hh:mm:ss display format.
271 */
272 static void bfd_last_update(time_t last_update, char *buf, size_t len)
273 {
274 time_t curr;
275 time_t diff;
276 struct tm tm;
277 struct timeval tv;
278
279 /* If no BFD satatus update has ever been received, print `never'. */
280 if (last_update == 0) {
281 snprintf(buf, len, "never");
282 return;
283 }
284
285 /* Get current time. */
286 monotime(&tv);
287 curr = tv.tv_sec;
288 diff = curr - last_update;
289 gmtime_r(&diff, &tm);
290
291 snprintf(buf, len, "%d:%02d:%02d:%02d", tm.tm_yday, tm.tm_hour,
292 tm.tm_min, tm.tm_sec);
293 }
294
295 /*
296 * bfd_show_param - Show the BFD parameter information.
297 */
298 void bfd_show_param(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
299 int extra_space, bool use_json, json_object *json_obj)
300 {
301 json_object *json_bfd = NULL;
302
303 if (!bfd_info)
304 return;
305
306 if (use_json) {
307 if (bfd_tag)
308 json_bfd = json_object_new_object();
309 else
310 json_bfd = json_obj;
311
312 json_object_int_add(json_bfd, "detectMultiplier",
313 bfd_info->detect_mult);
314 json_object_int_add(json_bfd, "rxMinInterval",
315 bfd_info->required_min_rx);
316 json_object_int_add(json_bfd, "txMinInterval",
317 bfd_info->desired_min_tx);
318 if (bfd_tag)
319 json_object_object_add(json_obj, "peerBfdInfo",
320 json_bfd);
321 } else {
322 vty_out(vty,
323 " %s%sDetect Multiplier: %d, Min Rx interval: %d,"
324 " Min Tx interval: %d\n",
325 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
326 bfd_info->detect_mult, bfd_info->required_min_rx,
327 bfd_info->desired_min_tx);
328 }
329 }
330
331 /*
332 * bfd_show_status - Show the BFD status information.
333 */
334 static void bfd_show_status(struct vty *vty, struct bfd_info *bfd_info,
335 int bfd_tag, int extra_space, bool use_json,
336 json_object *json_bfd)
337 {
338 char time_buf[32];
339
340 if (!bfd_info)
341 return;
342
343 bfd_last_update(bfd_info->last_update, time_buf, 32);
344 if (use_json) {
345 json_object_string_add(json_bfd, "status",
346 bfd_get_status_str(bfd_info->status));
347 json_object_string_add(json_bfd, "lastUpdate", time_buf);
348 } else {
349 vty_out(vty, " %s%sStatus: %s, Last update: %s\n",
350 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
351 bfd_get_status_str(bfd_info->status), time_buf);
352 }
353 }
354
355 /*
356 * bfd_show_info - Show the BFD information.
357 */
358 void bfd_show_info(struct vty *vty, struct bfd_info *bfd_info, int multihop,
359 int extra_space, bool use_json, json_object *json_obj)
360 {
361 json_object *json_bfd = NULL;
362
363 if (!bfd_info)
364 return;
365
366 if (use_json) {
367 json_bfd = json_object_new_object();
368 if (multihop)
369 json_object_string_add(json_bfd, "type", "multi hop");
370 else
371 json_object_string_add(json_bfd, "type", "single hop");
372 } else {
373 vty_out(vty, " %sBFD: Type: %s\n", (extra_space) ? " " : "",
374 (multihop) ? "multi hop" : "single hop");
375 }
376
377 bfd_show_param(vty, bfd_info, 0, extra_space, use_json, json_bfd);
378 bfd_show_status(vty, bfd_info, 0, extra_space, use_json, json_bfd);
379
380 if (use_json)
381 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
382 else
383 vty_out(vty, "\n");
384 }
385
386 /*
387 * bfd_client_sendmsg - Format and send a client register
388 * command to Zebra to be forwarded to BFD
389 */
390 void bfd_client_sendmsg(struct zclient *zclient, int command,
391 vrf_id_t vrf_id)
392 {
393 struct stream *s;
394 int ret;
395
396 /* Check socket. */
397 if (!zclient || zclient->sock < 0) {
398 if (bfd_debug)
399 zlog_debug(
400 "%s: Can't send BFD client register, Zebra client not "
401 "established",
402 __func__);
403 return;
404 }
405
406 s = zclient->obuf;
407 stream_reset(s);
408 zclient_create_header(s, command, vrf_id);
409
410 stream_putl(s, getpid());
411
412 stream_putw_at(s, 0, stream_get_endp(s));
413
414 ret = zclient_send_message(zclient);
415
416 if (ret < 0) {
417 if (bfd_debug)
418 zlog_debug(
419 "bfd_client_sendmsg %ld: zclient_send_message() failed",
420 (long)getpid());
421 return;
422 }
423
424 return;
425 }
426
427 int zclient_bfd_command(struct zclient *zc, struct bfd_session_arg *args)
428 {
429 struct stream *s;
430 size_t addrlen;
431
432 /* Check socket. */
433 if (!zc || zc->sock < 0) {
434 if (bfd_debug)
435 zlog_debug("%s: zclient unavailable", __func__);
436 return -1;
437 }
438
439 s = zc->obuf;
440 stream_reset(s);
441
442 /* Create new message. */
443 zclient_create_header(s, args->command, args->vrf_id);
444 stream_putl(s, getpid());
445
446 /* Encode destination address. */
447 stream_putw(s, args->family);
448 addrlen = (args->family == AF_INET) ? sizeof(struct in_addr)
449 : sizeof(struct in6_addr);
450 stream_put(s, &args->dst, addrlen);
451
452 /* Encode timers if this is a registration message. */
453 if (args->command != ZEBRA_BFD_DEST_DEREGISTER) {
454 stream_putl(s, args->min_rx);
455 stream_putl(s, args->min_tx);
456 stream_putc(s, args->detection_multiplier);
457 }
458
459 if (args->mhop) {
460 /* Multi hop indicator. */
461 stream_putc(s, 1);
462
463 /* Multi hop always sends the source address. */
464 stream_putw(s, args->family);
465 stream_put(s, &args->src, addrlen);
466
467 /* Send the expected TTL. */
468 stream_putc(s, args->ttl);
469 } else {
470 /* Multi hop indicator. */
471 stream_putc(s, 0);
472
473 /* Single hop only sends the source address when IPv6. */
474 if (args->family == AF_INET6) {
475 stream_putw(s, args->family);
476 stream_put(s, &args->src, addrlen);
477 }
478
479 /* Send interface name if any. */
480 stream_putc(s, args->ifnamelen);
481 if (args->ifnamelen)
482 stream_put(s, args->ifname, args->ifnamelen);
483 }
484
485 /* Send the C bit indicator. */
486 stream_putc(s, args->cbit);
487
488 /* `ptm-bfd` doesn't support profiles yet. */
489 #if HAVE_BFDD > 0
490 /* Send profile name if any. */
491 stream_putc(s, args->profilelen);
492 if (args->profilelen)
493 stream_put(s, args->profile, args->profilelen);
494 #endif /* HAVE_BFDD */
495
496 /* Finish the message by writing the size. */
497 stream_putw_at(s, 0, stream_get_endp(s));
498
499 /* Send message to zebra. */
500 if (zclient_send_message(zc) == -1) {
501 if (bfd_debug)
502 zlog_debug("%s: zclient_send_message failed", __func__);
503 return -1;
504 }
505
506 /* Write registration indicator into data structure. */
507 if (args->bfd_info && args->set_flag) {
508 if (args->command == ZEBRA_BFD_DEST_REGISTER)
509 SET_FLAG(args->bfd_info->flags, BFD_FLAG_BFD_REG);
510 else if (args->command == ZEBRA_BFD_DEST_DEREGISTER)
511 UNSET_FLAG(args->bfd_info->flags, BFD_FLAG_BFD_REG);
512 }
513
514 return 0;
515 }