]> git.proxmox.com Git - mirror_frr.git/blame - lib/bfd.c
Merge pull request #5410 from ton31337/feature/bgp_default-route_with_route-map_set
[mirror_frr.git] / lib / bfd.c
CommitLineData
7f342629
DS
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 *
896014f4
DL
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
7f342629
DS
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
4a1ab8e4
DL
35DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info")
36
c17faa4b
QY
37static int bfd_debug = 0;
38static struct bfd_gbl bfd_gbl;
567b877d 39
40/*
41 * bfd_gbl_init - Initialize the BFD global structure
42 */
d62a17ae 43void bfd_gbl_init(void)
567b877d 44{
d62a17ae 45 memset(&bfd_gbl, 0, sizeof(struct bfd_gbl));
567b877d 46}
47
48/*
49 * bfd_gbl_exit - Called when daemon exits
50 */
d62a17ae 51void bfd_gbl_exit(void)
567b877d 52{
d62a17ae 53 SET_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN);
567b877d 54}
588e90ec 55
7f342629
DS
56/*
57 * bfd_info_create - Allocate the BFD information
58 */
d62a17ae 59struct bfd_info *bfd_info_create(void)
7f342629 60{
d62a17ae 61 struct bfd_info *bfd_info;
7f342629 62
d62a17ae 63 bfd_info = XCALLOC(MTYPE_BFD_INFO, sizeof(struct bfd_info));
64 assert(bfd_info);
7f342629 65
d62a17ae 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;
7f342629
DS
70}
71
72/*
73 * bfd_info_free - Free the BFD information.
74 */
d62a17ae 75void bfd_info_free(struct bfd_info **bfd_info)
7f342629 76{
d62a17ae 77 if (*bfd_info) {
78 XFREE(MTYPE_BFD_INFO, *bfd_info);
79 *bfd_info = NULL;
80 }
7f342629
DS
81}
82
83/*
84 * bfd_validate_param - Validate the BFD paramter information.
85 */
d62a17ae 86int bfd_validate_param(struct vty *vty, const char *dm_str, const char *rx_str,
d7c0a89a
QY
87 const char *tx_str, uint8_t *dm_val, uint32_t *rx_val,
88 uint32_t *tx_val)
7f342629 89{
d62a17ae 90 *dm_val = strtoul(dm_str, NULL, 10);
91 *rx_val = strtoul(rx_str, NULL, 10);
92 *tx_val = strtoul(tx_str, NULL, 10);
93 return CMD_SUCCESS;
7f342629
DS
94}
95
96/*
97 * bfd_set_param - Set the configured BFD paramter values
98 */
d7c0a89a
QY
99void bfd_set_param(struct bfd_info **bfd_info, uint32_t min_rx, uint32_t min_tx,
100 uint8_t detect_mult, int defaults, int *command)
7f342629 101{
d62a17ae 102 if (!*bfd_info) {
103 *bfd_info = bfd_info_create();
104 *command = ZEBRA_BFD_DEST_REGISTER;
105 } else {
106 if (((*bfd_info)->required_min_rx != min_rx)
107 || ((*bfd_info)->desired_min_tx != min_tx)
108 || ((*bfd_info)->detect_mult != detect_mult))
109 *command = ZEBRA_BFD_DEST_UPDATE;
110 }
111
112 if (*command) {
113 (*bfd_info)->required_min_rx = min_rx;
114 (*bfd_info)->desired_min_tx = min_tx;
115 (*bfd_info)->detect_mult = detect_mult;
116 }
117
118 if (!defaults)
119 SET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
120 else
121 UNSET_FLAG((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
7f342629
DS
122}
123
124/*
125 * bfd_peer_sendmsg - Format and send a peer register/Unregister
126 * command to Zebra to be forwarded to BFD
127 */
d62a17ae 128void bfd_peer_sendmsg(struct zclient *zclient, struct bfd_info *bfd_info,
129 int family, void *dst_ip, void *src_ip, char *if_name,
9beff0bd
PG
130 int ttl, int multihop, int cbit, int command,
131 int set_flag, vrf_id_t vrf_id)
7f342629 132{
d62a17ae 133 struct stream *s;
134 int ret;
135 int len;
136
0437e105 137 /* Individual reg/dereg messages are suppressed during shutdown. */
d62a17ae 138 if (CHECK_FLAG(bfd_gbl.flags, BFD_GBL_FLAG_IN_SHUTDOWN)) {
139 if (bfd_debug)
140 zlog_debug(
141 "%s: Suppressing BFD peer reg/dereg messages",
142 __FUNCTION__);
143 return;
144 }
145
146 /* Check socket. */
147 if (!zclient || zclient->sock < 0) {
148 if (bfd_debug)
149 zlog_debug(
150 "%s: Can't send BFD peer register, Zebra client not "
151 "established",
152 __FUNCTION__);
153 return;
154 }
155
156 s = zclient->obuf;
157 stream_reset(s);
158 zclient_create_header(s, command, vrf_id);
159
160 stream_putl(s, getpid());
161
162 stream_putw(s, family);
163 switch (family) {
164 case AF_INET:
165 stream_put_in_addr(s, (struct in_addr *)dst_ip);
166 break;
167 case AF_INET6:
168 stream_put(s, dst_ip, 16);
169 break;
170 default:
171 break;
172 }
173
174 if (command != ZEBRA_BFD_DEST_DEREGISTER) {
175 stream_putl(s, bfd_info->required_min_rx);
176 stream_putl(s, bfd_info->desired_min_tx);
177 stream_putc(s, bfd_info->detect_mult);
178 }
179
180 if (multihop) {
181 stream_putc(s, 1);
182 /* Multi-hop destination send the source IP address to BFD */
183 if (src_ip) {
184 stream_putw(s, family);
185 switch (family) {
186 case AF_INET:
187 stream_put_in_addr(s, (struct in_addr *)src_ip);
188 break;
189 case AF_INET6:
190 stream_put(s, src_ip, 16);
191 break;
192 default:
193 break;
194 }
195 }
196 stream_putc(s, ttl);
197 } else {
198 stream_putc(s, 0);
199 if ((family == AF_INET6) && (src_ip)) {
200 stream_putw(s, family);
201 stream_put(s, src_ip, 16);
202 }
203 if (if_name) {
204 len = strlen(if_name);
205 stream_putc(s, len);
206 stream_put(s, if_name, len);
207 } else {
208 stream_putc(s, 0);
209 }
210 }
9beff0bd
PG
211 /* cbit */
212 if (cbit)
213 stream_putc(s, 1);
214 else
215 stream_putc(s, 0);
d62a17ae 216
217 stream_putw_at(s, 0, stream_get_endp(s));
218
219 ret = zclient_send_message(zclient);
220
221 if (ret < 0) {
222 if (bfd_debug)
223 zlog_debug(
224 "bfd_peer_sendmsg: zclient_send_message() failed");
225 return;
226 }
227
228 if (set_flag) {
229 if (command == ZEBRA_BFD_DEST_REGISTER)
230 SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
231 else if (command == ZEBRA_BFD_DEST_DEREGISTER)
232 UNSET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
233 }
234
235 return;
7f342629
DS
236}
237
238/*
239 * bfd_get_command_dbg_str - Convert command to a debug string.
240 */
d62a17ae 241const char *bfd_get_command_dbg_str(int command)
7f342629 242{
d62a17ae 243 switch (command) {
244 case ZEBRA_BFD_DEST_REGISTER:
245 return "Register";
246 case ZEBRA_BFD_DEST_DEREGISTER:
247 return "Deregister";
248 case ZEBRA_BFD_DEST_UPDATE:
249 return "Update";
250 default:
251 return "Unknown";
252 }
7f342629
DS
253}
254
255/*
256 * bfd_get_peer_info - Extract the Peer information for which the BFD session
257 * went down from the message sent from Zebra to clients.
258 */
d62a17ae 259struct interface *bfd_get_peer_info(struct stream *s, struct prefix *dp,
260 struct prefix *sp, int *status,
9beff0bd 261 int *remote_cbit,
d62a17ae 262 vrf_id_t vrf_id)
7f342629 263{
d62a17ae 264 unsigned int ifindex;
265 struct interface *ifp = NULL;
266 int plen;
9beff0bd 267 int local_remote_cbit;
d62a17ae 268
269 /* Get interface index. */
270 ifindex = stream_getl(s);
271
272 /* Lookup index. */
273 if (ifindex != 0) {
274 ifp = if_lookup_by_index(ifindex, vrf_id);
275 if (ifp == NULL) {
276 if (bfd_debug)
277 zlog_debug(
278 "zebra_interface_bfd_read: "
279 "Can't find interface by ifindex: %d ",
280 ifindex);
281 return NULL;
282 }
283 }
284
285 /* Fetch destination address. */
286 dp->family = stream_getc(s);
287
288 plen = prefix_blen(dp);
289 stream_get(&dp->u.prefix, s, plen);
290 dp->prefixlen = stream_getc(s);
291
292 /* Get BFD status. */
293 *status = stream_getl(s);
294
295 if (sp) {
296 sp->family = stream_getc(s);
297
298 plen = prefix_blen(sp);
299 stream_get(&sp->u.prefix, s, plen);
300 sp->prefixlen = stream_getc(s);
301 }
9beff0bd
PG
302 local_remote_cbit = stream_getc(s);
303 if (remote_cbit)
304 *remote_cbit = local_remote_cbit;
d62a17ae 305 return ifp;
7f342629 306}
68fe91d6 307
308/*
309 * bfd_get_status_str - Convert BFD status to a display string.
310 */
d62a17ae 311const char *bfd_get_status_str(int status)
68fe91d6 312{
d62a17ae 313 switch (status) {
314 case BFD_STATUS_DOWN:
315 return "Down";
316 case BFD_STATUS_UP:
317 return "Up";
7555dc61
S
318 case BFD_STATUS_ADMIN_DOWN:
319 return "Admin Down";
d62a17ae 320 case BFD_STATUS_UNKNOWN:
321 default:
322 return "Unknown";
323 }
68fe91d6 324}
325
326/*
327 * bfd_last_update - Calculate the last BFD update time and convert it
328 * into a dd:hh:mm:ss display format.
329 */
d62a17ae 330static void bfd_last_update(time_t last_update, char *buf, size_t len)
68fe91d6 331{
d62a17ae 332 time_t curr;
333 time_t diff;
334 struct tm *tm;
335 struct timeval tv;
336
337 /* If no BFD satatus update has ever been received, print `never'. */
338 if (last_update == 0) {
339 snprintf(buf, len, "never");
340 return;
341 }
342
343 /* Get current time. */
344 monotime(&tv);
345 curr = tv.tv_sec;
346 diff = curr - last_update;
347 tm = gmtime(&diff);
348
349 snprintf(buf, len, "%d:%02d:%02d:%02d", tm->tm_yday, tm->tm_hour,
350 tm->tm_min, tm->tm_sec);
68fe91d6 351}
352
353/*
354 * bfd_show_param - Show the BFD parameter information.
355 */
d62a17ae 356void bfd_show_param(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
9f049418 357 int extra_space, bool use_json, json_object *json_obj)
68fe91d6 358{
d62a17ae 359 json_object *json_bfd = NULL;
360
361 if (!bfd_info)
362 return;
363
364 if (use_json) {
365 if (bfd_tag)
366 json_bfd = json_object_new_object();
367 else
368 json_bfd = json_obj;
369
370 json_object_int_add(json_bfd, "detectMultiplier",
371 bfd_info->detect_mult);
372 json_object_int_add(json_bfd, "rxMinInterval",
373 bfd_info->required_min_rx);
374 json_object_int_add(json_bfd, "txMinInterval",
375 bfd_info->desired_min_tx);
376 if (bfd_tag)
377 json_object_object_add(json_obj, "peerBfdInfo",
378 json_bfd);
379 } else {
380 vty_out(vty,
b077b2e6 381 " %s%sDetect Multiplier: %d, Min Rx interval: %d,"
d62a17ae 382 " Min Tx interval: %d\n",
383 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
384 bfd_info->detect_mult, bfd_info->required_min_rx,
385 bfd_info->desired_min_tx);
386 }
68fe91d6 387}
388
389/*
390 * bfd_show_status - Show the BFD status information.
391 */
d62a17ae 392static void bfd_show_status(struct vty *vty, struct bfd_info *bfd_info,
9f049418 393 int bfd_tag, int extra_space, bool use_json,
d62a17ae 394 json_object *json_bfd)
68fe91d6 395{
d62a17ae 396 char time_buf[32];
397
398 if (!bfd_info)
399 return;
400
401 bfd_last_update(bfd_info->last_update, time_buf, 32);
402 if (use_json) {
403 json_object_string_add(json_bfd, "status",
404 bfd_get_status_str(bfd_info->status));
405 json_object_string_add(json_bfd, "lastUpdate", time_buf);
406 } else {
407 vty_out(vty, " %s%sStatus: %s, Last update: %s\n",
408 (extra_space) ? " " : "", (bfd_tag) ? "BFD: " : " ",
409 bfd_get_status_str(bfd_info->status), time_buf);
410 }
68fe91d6 411}
412
413/*
414 * bfd_show_info - Show the BFD information.
415 */
d62a17ae 416void bfd_show_info(struct vty *vty, struct bfd_info *bfd_info, int multihop,
9f049418 417 int extra_space, bool use_json, json_object *json_obj)
68fe91d6 418{
d62a17ae 419 json_object *json_bfd = NULL;
420
421 if (!bfd_info)
422 return;
423
424 if (use_json) {
425 json_bfd = json_object_new_object();
426 if (multihop)
427 json_object_string_add(json_bfd, "type", "multi hop");
428 else
429 json_object_string_add(json_bfd, "type", "single hop");
430 } else {
431 vty_out(vty, " %sBFD: Type: %s\n", (extra_space) ? " " : "",
432 (multihop) ? "multi hop" : "single hop");
433 }
434
435 bfd_show_param(vty, bfd_info, 0, extra_space, use_json, json_bfd);
436 bfd_show_status(vty, bfd_info, 0, extra_space, use_json, json_bfd);
437
438 if (use_json)
439 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
440 else
441 vty_out(vty, "\n");
68fe91d6 442}
055c4dfc 443
444/*
445 * bfd_client_sendmsg - Format and send a client register
446 * command to Zebra to be forwarded to BFD
447 */
0945d5ed
PG
448void bfd_client_sendmsg(struct zclient *zclient, int command,
449 vrf_id_t vrf_id)
055c4dfc 450{
d62a17ae 451 struct stream *s;
452 int ret;
453
454 /* Check socket. */
455 if (!zclient || zclient->sock < 0) {
456 if (bfd_debug)
457 zlog_debug(
458 "%s: Can't send BFD client register, Zebra client not "
459 "established",
460 __FUNCTION__);
461 return;
462 }
463
464 s = zclient->obuf;
465 stream_reset(s);
0945d5ed 466 zclient_create_header(s, command, vrf_id);
d62a17ae 467
468 stream_putl(s, getpid());
469
470 stream_putw_at(s, 0, stream_get_endp(s));
471
472 ret = zclient_send_message(zclient);
473
474 if (ret < 0) {
475 if (bfd_debug)
476 zlog_debug(
477 "bfd_client_sendmsg %ld: zclient_send_message() failed",
478 (long)getpid());
479 return;
480 }
481
482 return;
055c4dfc 483}