]> git.proxmox.com Git - mirror_frr.git/blob - lib/bfd.c
Support of BFD status in Quagga
[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
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24 #include <zebra.h>
25
26 #include "command.h"
27 #include "memory.h"
28 #include "prefix.h"
29 #include "thread.h"
30 #include "stream.h"
31 #include "zclient.h"
32 #include "table.h"
33 #include "vty.h"
34 #include "bfd.h"
35
36 /*
37 * bfd_info_create - Allocate the BFD information
38 */
39 struct bfd_info *
40 bfd_info_create(void)
41 {
42 struct bfd_info *bfd_info;
43
44 bfd_info = XCALLOC (MTYPE_BFD_INFO, sizeof (struct bfd_info));
45 assert(bfd_info);
46
47 bfd_info->status = BFD_STATUS_UNKNOWN;
48 bfd_info->last_update = 0;
49 return bfd_info;
50 }
51
52 /*
53 * bfd_info_free - Free the BFD information.
54 */
55 void
56 bfd_info_free(struct bfd_info **bfd_info)
57 {
58 if (*bfd_info)
59 {
60 XFREE (MTYPE_BFD_INFO, *bfd_info);
61 *bfd_info = NULL;
62 }
63 }
64
65 /*
66 * bfd_validate_param - Validate the BFD paramter information.
67 */
68 int
69 bfd_validate_param(struct vty *vty, const char *dm_str, const char *rx_str,
70 const char *tx_str, u_int8_t *dm_val, u_int32_t *rx_val,
71 u_int32_t *tx_val)
72 {
73 VTY_GET_INTEGER_RANGE ("detect-mul", *dm_val, dm_str,
74 BFD_MIN_DETECT_MULT, BFD_MAX_DETECT_MULT);
75 VTY_GET_INTEGER_RANGE ("min-rx", *rx_val, rx_str,
76 BFD_MIN_MIN_RX, BFD_MAX_MIN_RX);
77 VTY_GET_INTEGER_RANGE ("min-tx", *tx_val, tx_str,
78 BFD_MIN_MIN_TX, BFD_MAX_MIN_TX);
79 return CMD_SUCCESS;
80 }
81
82 /*
83 * bfd_set_param - Set the configured BFD paramter values
84 */
85 void
86 bfd_set_param (struct bfd_info **bfd_info, u_int32_t min_rx, u_int32_t min_tx,
87 u_int8_t detect_mult, int defaults, int *command)
88 {
89 if (!*bfd_info)
90 {
91 *bfd_info = bfd_info_create();
92 *command = ZEBRA_BFD_DEST_REGISTER;
93 }
94 else
95 {
96 if (((*bfd_info)->required_min_rx != min_rx) ||
97 ((*bfd_info)->desired_min_tx != min_tx) ||
98 ((*bfd_info)->detect_mult != detect_mult))
99 *command = ZEBRA_BFD_DEST_UPDATE;
100 }
101
102 if (*command)
103 {
104 (*bfd_info)->required_min_rx = min_rx;
105 (*bfd_info)->desired_min_tx = min_tx;
106 (*bfd_info)->detect_mult = detect_mult;
107 }
108
109 if (!defaults)
110 SET_FLAG ((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
111 else
112 UNSET_FLAG ((*bfd_info)->flags, BFD_FLAG_PARAM_CFG);
113 }
114
115 /*
116 * bfd_peer_sendmsg - Format and send a peer register/Unregister
117 * command to Zebra to be forwarded to BFD
118 */
119 void
120 bfd_peer_sendmsg (struct zclient *zclient, struct bfd_info *bfd_info,
121 int family, void *dst_ip, void *src_ip, char *if_name,
122 int ttl, int multihop, int command, int set_flag)
123 {
124 struct stream *s;
125 int ret;
126 int len;
127
128 /* Check socket. */
129 if (!zclient || zclient->sock < 0)
130 {
131 zlog_debug("%s: Can't send BFD peer register, Zebra client not "
132 "established", __FUNCTION__);
133 return;
134 }
135
136 s = zclient->obuf;
137 stream_reset (s);
138 zclient_create_header (s, command);
139
140 stream_putw(s, family);
141 switch (family)
142 {
143 case AF_INET:
144 stream_put_in_addr (s, (struct in_addr *)dst_ip);
145 break;
146 #ifdef HAVE_IPV6
147 case AF_INET6:
148 stream_put(s, dst_ip, 16);
149 break;
150 #endif
151 default:
152 break;
153 }
154
155 if (command != ZEBRA_BFD_DEST_DEREGISTER)
156 {
157 stream_putl(s, bfd_info->required_min_rx);
158 stream_putl(s, bfd_info->desired_min_tx);
159 stream_putc(s, bfd_info->detect_mult);
160 }
161
162 if (multihop)
163 {
164 stream_putc(s, 1);
165 /* Multi-hop destination send the source IP address to BFD */
166 if (src_ip)
167 {
168 stream_putw(s, family);
169 switch (family)
170 {
171 case AF_INET:
172 stream_put_in_addr (s, (struct in_addr *) src_ip);
173 break;
174 #ifdef HAVE_IPV6
175 case AF_INET6:
176 stream_put(s, src_ip, 16);
177 break;
178 #endif
179 default:
180 break;
181 }
182 }
183 stream_putc(s, ttl);
184 }
185 else
186 {
187 stream_putc(s, 0);
188 #ifdef HAVE_IPV6
189 if ((family == AF_INET6) && (src_ip))
190 {
191 stream_putw(s, family);
192 stream_put(s, src_ip, 16);
193 }
194 #endif
195 if (if_name)
196 {
197 len = strlen(if_name);
198 stream_putc(s, len);
199 stream_put(s, if_name, len);
200 }
201 else
202 {
203 stream_putc(s, 0);
204 }
205 }
206
207 stream_putw_at (s, 0, stream_get_endp (s));
208
209 ret = zclient_send_message(zclient);
210
211 if (ret < 0)
212 {
213 zlog_warn("bfd_peer_sendmsg: zclient_send_message() failed");
214 return;
215 }
216
217 if (set_flag)
218 {
219 if (command == ZEBRA_BFD_DEST_REGISTER)
220 SET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
221 else if (command == ZEBRA_BFD_DEST_DEREGISTER)
222 UNSET_FLAG(bfd_info->flags, BFD_FLAG_BFD_REG);
223 }
224
225 return;
226 }
227
228 /*
229 * bfd_get_command_dbg_str - Convert command to a debug string.
230 */
231 const char *
232 bfd_get_command_dbg_str(int command)
233 {
234 switch (command)
235 {
236 case ZEBRA_BFD_DEST_REGISTER:
237 return "Register";
238 case ZEBRA_BFD_DEST_DEREGISTER:
239 return "Deregister";
240 case ZEBRA_BFD_DEST_UPDATE:
241 return "Update";
242 default:
243 return "Unknown";
244 }
245 }
246
247 /*
248 * bfd_get_peer_info - Extract the Peer information for which the BFD session
249 * went down from the message sent from Zebra to clients.
250 */
251 struct interface *
252 bfd_get_peer_info (struct stream *s, struct prefix *dp, struct prefix *sp,
253 int *status)
254 {
255 unsigned int ifindex;
256 struct interface *ifp = NULL;
257 int plen;
258
259 /* Get interface index. */
260 ifindex = stream_getl (s);
261
262 /* Lookup index. */
263 if (ifindex != 0)
264 {
265 ifp = if_lookup_by_index (ifindex);
266 if (ifp == NULL)
267 {
268 zlog_warn ("zebra_interface_bfd_read: "
269 "Can't find interface by ifindex: %d ", ifindex);
270 return NULL;
271 }
272 }
273
274 /* Fetch destination address. */
275 dp->family = stream_getc (s);
276
277 plen = prefix_blen (dp);
278 stream_get (&dp->u.prefix, s, plen);
279 dp->prefixlen = stream_getc (s);
280
281 /* Get BFD status. */
282 *status = stream_getl (s);
283
284 if (sp)
285 {
286 sp->family = stream_getc (s);
287
288 plen = prefix_blen (sp);
289 stream_get (&sp->u.prefix, s, plen);
290 sp->prefixlen = stream_getc (s);
291 }
292 return ifp;
293 }
294
295 /*
296 * bfd_get_status_str - Convert BFD status to a display string.
297 */
298 const char *
299 bfd_get_status_str(int status)
300 {
301 switch (status)
302 {
303 case BFD_STATUS_DOWN:
304 return "Down";
305 case BFD_STATUS_UP:
306 return "Up";
307 case BFD_STATUS_UNKNOWN:
308 default:
309 return "Unknown";
310 }
311 }
312
313 /*
314 * bfd_last_update - Calculate the last BFD update time and convert it
315 * into a dd:hh:mm:ss display format.
316 */
317 static void
318 bfd_last_update (time_t last_update, char *buf, size_t len)
319 {
320 time_t curr;
321 time_t diff;
322 struct tm *tm;
323 struct timeval tv;
324
325 /* If no BFD satatus update has ever been received, print `never'. */
326 if (last_update == 0)
327 {
328 snprintf (buf, len, "never");
329 return;
330 }
331
332 /* Get current time. */
333 quagga_gettime(QUAGGA_CLK_MONOTONIC, &tv);
334 curr = tv.tv_sec;
335 diff = curr - last_update;
336 tm = gmtime (&diff);
337
338 snprintf (buf, len, "%d:%02d:%02d:%02d",
339 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec);
340 }
341
342 /*
343 * bfd_show_param - Show the BFD parameter information.
344 */
345 void
346 bfd_show_param(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
347 int extra_space, u_char use_json, json_object *json_obj)
348 {
349 json_object *json_bfd = NULL;
350
351 if (!bfd_info)
352 return;
353
354 if (use_json)
355 {
356 if (bfd_tag)
357 json_bfd = json_object_new_object();
358 else
359 json_bfd = json_obj;
360
361 json_object_int_add(json_bfd, "detectMultiplier", bfd_info->detect_mult);
362 json_object_int_add(json_bfd, "rxMinInterval", bfd_info->required_min_rx);
363 json_object_int_add(json_bfd, "txMinInterval", bfd_info->desired_min_tx);
364 if (bfd_tag)
365 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
366 }
367 else
368 {
369 vty_out (vty, " %s%sDetect Mul: %d, Min Rx interval: %d,"
370 " Min Tx interval: %d%s",
371 (extra_space) ? " ": "", (bfd_tag) ? "BFD: " : " ",
372 bfd_info->detect_mult, bfd_info->required_min_rx,
373 bfd_info->desired_min_tx, VTY_NEWLINE);
374 }
375 }
376
377 /*
378 * bfd_show_status - Show the BFD status information.
379 */
380 static void
381 bfd_show_status(struct vty *vty, struct bfd_info *bfd_info, int bfd_tag,
382 int extra_space, u_char use_json, json_object *json_bfd)
383 {
384 char time_buf[32];
385
386 if (!bfd_info)
387 return;
388
389 bfd_last_update(bfd_info->last_update, time_buf, 32);
390 if (use_json)
391 {
392 json_object_string_add(json_bfd, "status",
393 bfd_get_status_str(bfd_info->status));
394 json_object_string_add(json_bfd, "lastUpdate", time_buf);
395 }
396 else
397 {
398 vty_out (vty, " %s%sStatus: %s, Last update: %s%s",
399 (extra_space) ? " ": "", (bfd_tag) ? "BFD: " : " ",
400 bfd_get_status_str(bfd_info->status), time_buf, VTY_NEWLINE);
401 }
402 }
403
404 /*
405 * bfd_show_info - Show the BFD information.
406 */
407 void
408 bfd_show_info(struct vty *vty, struct bfd_info *bfd_info, int multihop,
409 int extra_space, u_char use_json, json_object *json_obj)
410 {
411 json_object *json_bfd = NULL;
412
413 if (!bfd_info)
414 return;
415
416 if (use_json)
417 {
418 json_bfd = json_object_new_object();
419 if (multihop)
420 json_object_string_add(json_bfd, "type", "multi hop");
421 else
422 json_object_string_add(json_bfd, "type", "single hop");
423 }
424 else
425 {
426 vty_out (vty, " %sBFD: Type: %s%s", (extra_space) ? " " : "",
427 (multihop) ? "multi hop" : "single hop", VTY_NEWLINE);
428 }
429
430 bfd_show_param(vty, bfd_info, 0, extra_space, use_json, json_bfd);
431 bfd_show_status(vty, bfd_info, 0, extra_space, use_json, json_bfd);
432
433 if (use_json)
434 json_object_object_add(json_obj, "peerBfdInfo", json_bfd);
435 else
436 vty_out (vty, "%s", VTY_NEWLINE);
437 }