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