33492ca85005ee79f9a2c6ab611cfa6004fe5d90
[vpp.git] / src / vnet / bfd / bfd_cli.c
1 /*
2  * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief BFD CLI implementation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vlib/cli.h>
22 #include <vppinfra/format.h>
23 #include <vppinfra/warnings.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ip/format.h>
26 #include <vnet/bfd/bfd_api.h>
27 #include <vnet/bfd/bfd_main.h>
28
29 static u8 *
30 format_bfd_session_cli (u8 * s, va_list * args)
31 {
32   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
33   bfd_main_t *bm = va_arg (*args, bfd_main_t *);
34   bfd_session_t *bs = va_arg (*args, bfd_session_t *);
35   switch (bs->transport)
36     {
37     case BFD_TRANSPORT_UDP4:
38       s = format (s, "%=10u %-32s %20U %20U\n", bs->bs_idx, "IPv4 address",
39                   format_ip4_address, bs->udp.key.local_addr.ip4.as_u8,
40                   format_ip4_address, bs->udp.key.peer_addr.ip4.as_u8);
41       break;
42     case BFD_TRANSPORT_UDP6:
43       s = format (s, "%=10u %-32s %20U %20U\n", bs->bs_idx, "IPv6 address",
44                   format_ip6_address, &bs->udp.key.local_addr.ip6,
45                   format_ip6_address, &bs->udp.key.peer_addr.ip6);
46       break;
47     }
48   s = format (s, "%10s %-32s %20s %20s\n", "", "Session state",
49               bfd_state_string (bs->local_state),
50               bfd_state_string (bs->remote_state));
51   s = format (s, "%10s %-32s %20s %20s\n", "", "Diagnostic code",
52               bfd_diag_code_string (bs->local_diag),
53               bfd_diag_code_string (bs->remote_diag));
54   s = format (s, "%10s %-32s %20u %20u\n", "", "Detect multiplier",
55               bs->local_detect_mult, bs->remote_detect_mult);
56   s = format (s, "%10s %-32s %20u %20llu\n", "",
57               "Required Min Rx Interval (usec)",
58               bs->config_required_min_rx_usec, bs->remote_min_rx_usec);
59   s = format (s, "%10s %-32s %20u %20u\n", "",
60               "Desired Min Tx Interval (usec)",
61               bs->config_desired_min_tx_usec, bfd_clocks_to_usec (bm,
62                                                                   bs->remote_desired_min_tx_clocks));
63   s =
64     format (s, "%10s %-32s %20u\n", "", "Transmit interval",
65             bfd_clocks_to_usec (bm, bs->transmit_interval_clocks));
66   u64 now = clib_cpu_time_now ();
67   u8 *tmp = NULL;
68   if (bs->last_tx_clocks > 0)
69     {
70       tmp = format (tmp, "%.2fs ago", (now - bs->last_tx_clocks) *
71                     vm->clib_time.seconds_per_clock);
72       s = format (s, "%10s %-32s %20v\n", "", "Last control frame tx", tmp);
73       vec_reset_length (tmp);
74     }
75   if (bs->last_rx_clocks)
76     {
77       tmp = format (tmp, "%.2fs ago", (now - bs->last_rx_clocks) *
78                     vm->clib_time.seconds_per_clock);
79       s = format (s, "%10s %-32s %20v\n", "", "Last control frame rx", tmp);
80       vec_reset_length (tmp);
81     }
82   s =
83     format (s, "%10s %-32s %20u %20llu\n", "", "Min Echo Rx Interval (usec)",
84             1, bs->remote_min_echo_rx_usec);
85   if (bs->echo)
86     {
87       s = format (s, "%10s %-32s %20u\n", "", "Echo transmit interval",
88                   bfd_clocks_to_usec (bm, bs->echo_transmit_interval_clocks));
89       tmp = format (tmp, "%.2fs ago", (now - bs->echo_last_tx_clocks) *
90                     vm->clib_time.seconds_per_clock);
91       s = format (s, "%10s %-32s %20v\n", "", "Last echo frame tx", tmp);
92       vec_reset_length (tmp);
93       tmp = format (tmp, "%.6fs",
94                     (bs->echo_last_rx_clocks - bs->echo_last_tx_clocks) *
95                     vm->clib_time.seconds_per_clock);
96       s =
97         format (s, "%10s %-32s %20v\n", "", "Last echo frame roundtrip time",
98                 tmp);
99     }
100   vec_free (tmp);
101   tmp = NULL;
102   s = format (s, "%10s %-32s %20s %20s\n", "", "Demand mode", "no",
103               bs->remote_demand ? "yes" : "no");
104   s = format (s, "%10s %-32s %20s\n", "", "Poll state",
105               bfd_poll_state_string (bs->poll_state));
106   if (bs->auth.curr_key)
107     {
108       s = format (s, "%10s %-32s %20u\n", "", "Authentication config key ID",
109                   bs->auth.curr_key->conf_key_id);
110       s = format (s, "%10s %-32s %20u\n", "", "Authentication BFD key ID",
111                   bs->auth.curr_bfd_key_id);
112       s = format (s, "%10s %-32s %20u %20u\n", "", "Sequence number",
113                   bs->auth.local_seq_number, bs->auth.remote_seq_number);
114     }
115   return s;
116 }
117
118 static clib_error_t *
119 show_bfd (vlib_main_t * vm, unformat_input_t * input,
120           CLIB_UNUSED (vlib_cli_command_t * lmd))
121 {
122   bfd_main_t *bm = &bfd_main;
123   bfd_session_t *bs = NULL;
124   unformat_input_t _line_input, *line_input = &_line_input;
125
126   /* Get a line of input. */
127   if (!unformat_user (input, unformat_line_input, line_input))
128     return 0;
129
130   if (unformat (line_input, "keys"))
131     {
132       bfd_auth_key_t *key = NULL;
133       u8 *s = format (NULL, "%=10s %=25s %=10s\n", "Configuration Key ID",
134                       "Type", "Use Count");
135       /* *INDENT-OFF* */
136       pool_foreach (key, bm->auth_keys, {
137         s = format (s, "%10u %-25s %10u\n", key->conf_key_id,
138                     bfd_auth_type_str (key->auth_type), key->use_count);
139       });
140       /* *INDENT-ON* */
141       vlib_cli_output (vm, "%v\n", s);
142       vec_free (s);
143       vlib_cli_output (vm, "Number of configured BFD keys: %lu\n",
144                        (u64) pool_elts (bm->auth_keys));
145     }
146   else if (unformat (line_input, "sessions"))
147     {
148       u8 *s = format (NULL, "%=10s %=32s %=20s %=20s\n", "Index", "Property",
149                       "Local value", "Remote value");
150       /* *INDENT-OFF* */
151       pool_foreach (bs, bm->sessions, {
152         s = format (s, "%U", format_bfd_session_cli, vm, bm, bs);
153       });
154       /* *INDENT-ON* */
155       vlib_cli_output (vm, "%v", s);
156       vec_free (s);
157       vlib_cli_output (vm, "Number of configured BFD sessions: %lu\n",
158                        (u64) pool_elts (bm->sessions));
159     }
160   else if (unformat (line_input, "echo-source"))
161     {
162       int is_set;
163       u32 sw_if_index;
164       int have_usable_ip4;
165       ip4_address_t ip4;
166       int have_usable_ip6;
167       ip6_address_t ip6;
168       bfd_udp_get_echo_source (&is_set, &sw_if_index, &have_usable_ip4, &ip4,
169                                &have_usable_ip6, &ip6);
170       if (is_set)
171         {
172           vnet_sw_interface_t *sw_if =
173             vnet_get_sw_interface_safe (&vnet_main, sw_if_index);
174           vnet_hw_interface_t *hw_if =
175             vnet_get_hw_interface (&vnet_main, sw_if->hw_if_index);
176           u8 *s = format (NULL, "UDP echo source is: %v\n", hw_if->name);
177           s = format (s, "IPv4 address usable as echo source: ");
178           if (have_usable_ip4)
179             {
180               s = format (s, "%U\n", format_ip4_address, &ip4);
181             }
182           else
183             {
184               s = format (s, "none\n");
185             }
186           s = format (s, "IPv6 address usable as echo source: ");
187           if (have_usable_ip6)
188             {
189               s = format (s, "%U\n", format_ip6_address, &ip6);
190             }
191           else
192             {
193               s = format (s, "none\n");
194             }
195           vlib_cli_output (vm, "%v", s);
196           vec_free (s);
197         }
198       else
199         {
200           vlib_cli_output (vm, "UDP echo source is not set.\n");
201         }
202     }
203   else
204     {
205       vlib_cli_output (vm, "Number of configured BFD sessions: %lu\n",
206                        (u64) pool_elts (bm->sessions));
207       vlib_cli_output (vm, "Number of configured BFD keys: %lu\n",
208                        (u64) pool_elts (bm->auth_keys));
209     }
210   return 0;
211 }
212
213 /* *INDENT-OFF* */
214 VLIB_CLI_COMMAND (show_bfd_command, static) = {
215   .path = "show bfd",
216   .short_help = "show bfd [keys|sessions|echo-source]",
217   .function = show_bfd,
218 };
219 /* *INDENT-ON* */
220
221 static u8 *
222 format_vnet_api_errno (u8 * s, va_list * args)
223 {
224   vnet_api_error_t api_error = va_arg (*args, vnet_api_error_t);
225 #define _(a, b, c)           \
226   case b:                    \
227     s = format (s, "%s", c); \
228     break;
229   switch (api_error)
230     {
231       foreach_vnet_api_error default:s = format (s, "UNKNOWN");
232       break;
233     }
234   return s;
235 }
236
237 static clib_error_t *
238 bfd_cli_key_add (vlib_main_t * vm, unformat_input_t * input,
239                  CLIB_UNUSED (vlib_cli_command_t * lmd))
240 {
241   clib_error_t *ret = NULL;
242   int have_key_id = 0;
243   u32 key_id = 0;
244   u8 *vec_auth_type = NULL;
245   bfd_auth_type_e auth_type = BFD_AUTH_TYPE_reserved;
246   u8 *secret = NULL;
247   unformat_input_t _line_input, *line_input = &_line_input;
248   static const u8 keyed_sha1[] = "keyed-sha1";
249   static const u8 meticulous_keyed_sha1[] = "meticulous-keyed-sha1";
250
251   /* Get a line of input. */
252   if (!unformat_user (input, unformat_line_input, line_input))
253     return 0;
254
255   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
256     {
257       if (unformat (line_input, "conf-key-id %u", &key_id))
258         {
259           have_key_id = 1;
260         }
261       else if (unformat (line_input, "type %U", unformat_token, "a-zA-Z0-9-",
262                          &vec_auth_type))
263         {
264           if (vec_len (vec_auth_type) == sizeof (keyed_sha1) - 1 &&
265               0 == memcmp (vec_auth_type, keyed_sha1,
266                            sizeof (keyed_sha1) - 1))
267             {
268               auth_type = BFD_AUTH_TYPE_keyed_sha1;
269             }
270           else if (vec_len (vec_auth_type) ==
271                    sizeof (meticulous_keyed_sha1) - 1 &&
272                    0 == memcmp (vec_auth_type, meticulous_keyed_sha1,
273                                 sizeof (meticulous_keyed_sha1) - 1))
274             {
275               auth_type = BFD_AUTH_TYPE_meticulous_keyed_sha1;
276             }
277           else
278             {
279               ret = clib_error_return (0, "invalid type `%v'", vec_auth_type);
280               goto out;
281             }
282         }
283       else
284         if (unformat (line_input, "secret %U", unformat_hex_string, &secret))
285         {
286           /* nothing to do here */
287         }
288       else
289         {
290           ret = clib_error_return (0, "Unknown input `%U'",
291                                    format_unformat_error, line_input);
292           goto out;
293         }
294     }
295
296   if (!have_key_id)
297     {
298       ret =
299         clib_error_return (0, "required parameter missing: `conf-key-id'");
300       goto out;
301     }
302   if (!vec_auth_type)
303     {
304       ret = clib_error_return (0, "required parameter missing: `type'");
305       goto out;
306     }
307   if (!secret)
308     {
309       ret = clib_error_return (0, "required parameter missing: `secret'");
310       goto out;
311     }
312
313   vnet_api_error_t rv =
314     bfd_auth_set_key (key_id, auth_type, vec_len (secret), secret);
315   if (rv)
316     {
317       ret =
318         clib_error_return (0, "`bfd_auth_set_key' API call failed, rv=%d:%U",
319                            (int) rv, format_vnet_api_errno, rv);
320     }
321
322 out:
323   vec_free (vec_auth_type);
324   return ret;
325 }
326
327 /* *INDENT-OFF* */
328 VLIB_CLI_COMMAND (bfd_cli_key_add_command, static) = {
329   .path = "bfd key set",
330   .short_help = "bfd key set"
331                 " conf-key-id <id>"
332                 " type <keyed-sha1|meticulous-keyed-sha1> "
333                 " secret <secret>",
334   .function = bfd_cli_key_add,
335 };
336 /* *INDENT-ON* */
337
338 static clib_error_t *
339 bfd_cli_key_del (vlib_main_t * vm, unformat_input_t * input,
340                  CLIB_UNUSED (vlib_cli_command_t * lmd))
341 {
342   clib_error_t *ret = NULL;
343   u32 key_id = 0;
344   unformat_input_t _line_input, *line_input = &_line_input;
345
346   /* Get a line of input. */
347   if (!unformat_user (input, unformat_line_input, line_input))
348     return 0;
349
350   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
351     {
352       if (!unformat (line_input, "conf-key-id %u", &key_id))
353         {
354           ret = clib_error_return (0, "Unknown input `%U'",
355                                    format_unformat_error, line_input);
356           goto out;
357         }
358     }
359
360   vnet_api_error_t rv = bfd_auth_del_key (key_id);
361   if (rv)
362     {
363       ret =
364         clib_error_return (0, "`bfd_auth_del_key' API call failed, rv=%d:%U",
365                            (int) rv, format_vnet_api_errno, rv);
366     }
367
368 out:
369   return ret;
370 }
371
372 /* *INDENT-OFF* */
373 VLIB_CLI_COMMAND (bfd_cli_key_del_command, static) = {
374   .path = "bfd key del",
375   .short_help = "bfd key del conf-key-id <id>",
376   .function = bfd_cli_key_del,
377 };
378 /* *INDENT-ON* */
379
380 #define INTERFACE_STR "interface"
381 #define LOCAL_ADDR_STR "local-addr"
382 #define PEER_ADDR_STR "peer-addr"
383 #define CONF_KEY_ID_STR "conf-key-id"
384 #define BFD_KEY_ID_STR "bfd-key-id"
385 #define DESIRED_MIN_TX_STR "desired-min-tx"
386 #define REQUIRED_MIN_RX_STR "required-min-rx"
387 #define DETECT_MULT_STR "detect-mult"
388 #define ADMIN_STR "admin"
389 #define DELAYED_STR "delayed"
390
391 static const unsigned mandatory = 1;
392 static const unsigned optional = 0;
393
394 #define DECLARE(t, n, s, r, ...) \
395   int have_##n = 0;              \
396   t n;
397
398 #define UNFORMAT(t, n, s, r, ...)              \
399   if (unformat (line_input, s " " __VA_ARGS__, &n)) \
400     {                                          \
401       something_parsed = 1;                    \
402       have_##n = 1;                            \
403     }
404
405 #define CHECK_MANDATORY(t, n, s, r, ...)                                  \
406 WARN_OFF(tautological-compare)                                            \
407   if (mandatory == r && !have_##n)                                        \
408     {                                                                     \
409       WARN_ON(tautological-compare)                                       \
410       ret = clib_error_return (0, "Required parameter `%s' missing.", s); \
411       goto out;                                                           \
412     }
413
414 static clib_error_t *
415 bfd_cli_udp_session_add (vlib_main_t * vm, unformat_input_t * input,
416                          CLIB_UNUSED (vlib_cli_command_t * lmd))
417 {
418   clib_error_t *ret = NULL;
419   unformat_input_t _line_input, *line_input = &_line_input;
420 #define foreach_bfd_cli_udp_session_add_cli_param(F)              \
421   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
422      unformat_vnet_sw_interface, &vnet_main)                      \
423   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
424      unformat_ip46_address)                                       \
425   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
426      unformat_ip46_address)                                       \
427   F (u32, desired_min_tx, DESIRED_MIN_TX_STR, mandatory, "%u")    \
428   F (u32, required_min_rx, REQUIRED_MIN_RX_STR, mandatory, "%u")  \
429   F (u32, detect_mult, DETECT_MULT_STR, mandatory, "%u")          \
430   F (u32, conf_key_id, CONF_KEY_ID_STR, optional, "%u")           \
431   F (u32, bfd_key_id, BFD_KEY_ID_STR, optional, "%u")
432
433   foreach_bfd_cli_udp_session_add_cli_param (DECLARE);
434
435   /* Get a line of input. */
436   if (!unformat_user (input, unformat_line_input, line_input))
437     return 0;
438
439   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
440     {
441       int something_parsed = 0;
442       foreach_bfd_cli_udp_session_add_cli_param (UNFORMAT);
443
444       if (!something_parsed)
445         {
446           ret = clib_error_return (0, "Unknown input `%U'",
447                                    format_unformat_error, line_input);
448           goto out;
449         }
450     }
451
452   foreach_bfd_cli_udp_session_add_cli_param (CHECK_MANDATORY);
453
454   if (1 == have_conf_key_id + have_bfd_key_id)
455     {
456       ret = clib_error_return (0, "Incompatible parameter combination, `%s' "
457                                "and `%s' must be either both specified or none",
458                                CONF_KEY_ID_STR, BFD_KEY_ID_STR);
459       goto out;
460     }
461
462   if (detect_mult > 255)
463     {
464       ret = clib_error_return (0, "%s value `%u' out of range <1,255>",
465                                DETECT_MULT_STR, detect_mult);
466       goto out;
467     }
468
469   if (have_bfd_key_id && bfd_key_id > 255)
470     {
471       ret = clib_error_return (0, "%s value `%u' out of range <1,255>",
472                                BFD_KEY_ID_STR, bfd_key_id);
473       goto out;
474     }
475
476   vnet_api_error_t rv =
477     bfd_udp_add_session (sw_if_index, &local_addr, &peer_addr, desired_min_tx,
478                          required_min_rx,
479                          detect_mult, have_conf_key_id, conf_key_id,
480                          bfd_key_id);
481   if (rv)
482     {
483       ret =
484         clib_error_return (0,
485                            "`bfd_add_add_session' API call failed, rv=%d:%U",
486                            (int) rv, format_vnet_api_errno, rv);
487       goto out;
488     }
489
490 out:
491   return ret;
492 }
493
494 /* *INDENT-OFF* */
495 VLIB_CLI_COMMAND (bfd_cli_udp_session_add_command, static) = {
496   .path = "bfd udp session add",
497   .short_help = "bfd udp session add"
498                 " interface <interface>"
499                 " local-addr <local-address>"
500                 " peer-addr <peer-address>"
501                 " desired-min-tx <desired min tx interval>"
502                 " required-min-rx <required min rx interval>"
503                 " detect-mult <detect multiplier> "
504                 "["
505                 " conf-key-id <config key ID>"
506                 " bfd-key-id <BFD key ID>"
507                 "]",
508   .function = bfd_cli_udp_session_add,
509 };
510 /* *INDENT-ON* */
511
512 static clib_error_t *
513 bfd_cli_udp_session_mod (vlib_main_t * vm, unformat_input_t * input,
514                          CLIB_UNUSED (vlib_cli_command_t * lmd))
515 {
516   clib_error_t *ret = NULL;
517   unformat_input_t _line_input, *line_input = &_line_input;
518 #define foreach_bfd_cli_udp_session_mod_cli_param(F)              \
519   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
520      unformat_vnet_sw_interface, &vnet_main)                      \
521   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
522      unformat_ip46_address)                                       \
523   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
524      unformat_ip46_address)                                       \
525   F (u32, desired_min_tx, DESIRED_MIN_TX_STR, mandatory, "%u")    \
526   F (u32, required_min_rx, REQUIRED_MIN_RX_STR, mandatory, "%u")  \
527   F (u32, detect_mult, DETECT_MULT_STR, mandatory, "%u")
528
529   foreach_bfd_cli_udp_session_mod_cli_param (DECLARE);
530
531   /* Get a line of input. */
532   if (!unformat_user (input, unformat_line_input, line_input))
533     return 0;
534
535   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
536     {
537       int something_parsed = 0;
538       foreach_bfd_cli_udp_session_mod_cli_param (UNFORMAT);
539
540       if (!something_parsed)
541         {
542           ret = clib_error_return (0, "Unknown input `%U'",
543                                    format_unformat_error, line_input);
544           goto out;
545         }
546     }
547
548   foreach_bfd_cli_udp_session_mod_cli_param (CHECK_MANDATORY);
549
550   if (detect_mult > 255)
551     {
552       ret = clib_error_return (0, "%s value `%u' out of range <1,255>",
553                                DETECT_MULT_STR, detect_mult);
554       goto out;
555     }
556
557   vnet_api_error_t rv =
558     bfd_udp_mod_session (sw_if_index, &local_addr, &peer_addr,
559                          desired_min_tx, required_min_rx, detect_mult);
560   if (rv)
561     {
562       ret =
563         clib_error_return (0,
564                            "`bfd_udp_mod_session' API call failed, rv=%d:%U",
565                            (int) rv, format_vnet_api_errno, rv);
566       goto out;
567     }
568
569 out:
570   return ret;
571 }
572
573 /* *INDENT-OFF* */
574 VLIB_CLI_COMMAND (bfd_cli_udp_session_mod_command, static) = {
575   .path = "bfd udp session mod",
576   .short_help = "bfd udp session mod interface"
577                 " <interface> local-addr"
578                 " <local-address> peer-addr"
579                 " <peer-address> desired-min-tx"
580                 " <desired min tx interval> required-min-rx"
581                 " <required min rx interval> detect-mult"
582                 " <detect multiplier> ",
583   .function = bfd_cli_udp_session_mod,
584 };
585 /* *INDENT-ON* */
586
587 static clib_error_t *
588 bfd_cli_udp_session_del (vlib_main_t * vm, unformat_input_t * input,
589                          CLIB_UNUSED (vlib_cli_command_t * lmd))
590 {
591   clib_error_t *ret = NULL;
592   unformat_input_t _line_input, *line_input = &_line_input;
593 #define foreach_bfd_cli_udp_session_del_cli_param(F)              \
594   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
595      unformat_vnet_sw_interface, &vnet_main)                      \
596   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
597      unformat_ip46_address)                                       \
598   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
599      unformat_ip46_address)
600
601   foreach_bfd_cli_udp_session_del_cli_param (DECLARE);
602
603   /* Get a line of input. */
604   if (!unformat_user (input, unformat_line_input, line_input))
605     return 0;
606
607   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
608     {
609       int something_parsed = 0;
610       foreach_bfd_cli_udp_session_del_cli_param (UNFORMAT);
611
612       if (!something_parsed)
613         {
614           ret = clib_error_return (0, "Unknown input `%U'",
615                                    format_unformat_error, line_input);
616           goto out;
617         }
618     }
619
620   foreach_bfd_cli_udp_session_del_cli_param (CHECK_MANDATORY);
621
622   vnet_api_error_t rv =
623     bfd_udp_del_session (sw_if_index, &local_addr, &peer_addr);
624   if (rv)
625     {
626       ret =
627         clib_error_return (0,
628                            "`bfd_udp_del_session' API call failed, rv=%d:%U",
629                            (int) rv, format_vnet_api_errno, rv);
630       goto out;
631     }
632
633 out:
634   return ret;
635 }
636
637 /* *INDENT-OFF* */
638 VLIB_CLI_COMMAND (bfd_cli_udp_session_del_command, static) = {
639   .path = "bfd udp session del",
640   .short_help = "bfd udp session del interface"
641                 " <interface> local-addr"
642                 " <local-address> peer-addr"
643                 "<peer-address> ",
644   .function = bfd_cli_udp_session_del,
645 };
646 /* *INDENT-ON* */
647
648 static clib_error_t *
649 bfd_cli_udp_session_set_flags (vlib_main_t * vm, unformat_input_t * input,
650                                CLIB_UNUSED (vlib_cli_command_t * lmd))
651 {
652   clib_error_t *ret = NULL;
653   unformat_input_t _line_input, *line_input = &_line_input;
654 #define foreach_bfd_cli_udp_session_set_flags_cli_param(F)        \
655   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
656      unformat_vnet_sw_interface, &vnet_main)                      \
657   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
658      unformat_ip46_address)                                       \
659   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
660      unformat_ip46_address)                                       \
661   F (u8 *, admin_up_down_token, ADMIN_STR, mandatory, "%v",       \
662      &admin_up_down_token)
663
664   foreach_bfd_cli_udp_session_set_flags_cli_param (DECLARE);
665
666   /* Get a line of input. */
667   if (!unformat_user (input, unformat_line_input, line_input))
668     return 0;
669
670   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
671     {
672       int something_parsed = 0;
673       foreach_bfd_cli_udp_session_set_flags_cli_param (UNFORMAT);
674
675       if (!something_parsed)
676         {
677           ret = clib_error_return (0, "Unknown input `%U'",
678                                    format_unformat_error, line_input);
679           goto out;
680         }
681     }
682
683   foreach_bfd_cli_udp_session_set_flags_cli_param (CHECK_MANDATORY);
684
685   u8 admin_up_down;
686   static const char up[] = "up";
687   static const char down[] = "down";
688   if (!memcmp (admin_up_down_token, up, sizeof (up) - 1))
689     {
690       admin_up_down = 1;
691     }
692   else if (!memcmp (admin_up_down_token, down, sizeof (down) - 1))
693     {
694       admin_up_down = 0;
695     }
696   else
697     {
698       ret =
699         clib_error_return (0, "Unrecognized value for `%s' parameter: `%v'",
700                            ADMIN_STR, admin_up_down_token);
701       goto out;
702     }
703   vnet_api_error_t rv = bfd_udp_session_set_flags (sw_if_index, &local_addr,
704                                                    &peer_addr, admin_up_down);
705   if (rv)
706     {
707       ret =
708         clib_error_return (0,
709                            "`bfd_udp_session_set_flags' API call failed, rv=%d:%U",
710                            (int) rv, format_vnet_api_errno, rv);
711       goto out;
712     }
713
714 out:
715   return ret;
716 }
717
718 /* *INDENT-OFF* */
719 VLIB_CLI_COMMAND (bfd_cli_udp_session_set_flags_command, static) = {
720   .path = "bfd udp session set-flags",
721   .short_help = "bfd udp session set-flags"
722                 " interface <interface>"
723                 " local-addr <local-address>"
724                 " peer-addr <peer-address>"
725                 " admin <up|down>",
726   .function = bfd_cli_udp_session_set_flags,
727 };
728 /* *INDENT-ON* */
729
730 static clib_error_t *
731 bfd_cli_udp_session_auth_activate (vlib_main_t * vm,
732                                    unformat_input_t * input,
733                                    CLIB_UNUSED (vlib_cli_command_t * lmd))
734 {
735   clib_error_t *ret = NULL;
736   unformat_input_t _line_input, *line_input = &_line_input;
737 #define foreach_bfd_cli_udp_session_auth_activate_cli_param(F)    \
738   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
739      unformat_vnet_sw_interface, &vnet_main)                      \
740   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
741      unformat_ip46_address)                                       \
742   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
743      unformat_ip46_address)                                       \
744   F (u8 *, delayed_token, DELAYED_STR, optional, "%v")            \
745   F (u32, conf_key_id, CONF_KEY_ID_STR, mandatory, "%u")          \
746   F (u32, bfd_key_id, BFD_KEY_ID_STR, mandatory, "%u")
747
748   foreach_bfd_cli_udp_session_auth_activate_cli_param (DECLARE);
749
750   /* Get a line of input. */
751   if (!unformat_user (input, unformat_line_input, line_input))
752     return 0;
753
754   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
755     {
756       int something_parsed = 0;
757       foreach_bfd_cli_udp_session_auth_activate_cli_param (UNFORMAT);
758
759       if (!something_parsed)
760         {
761           ret = clib_error_return (0, "Unknown input `%U'",
762                                    format_unformat_error, line_input);
763           goto out;
764         }
765     }
766
767   foreach_bfd_cli_udp_session_auth_activate_cli_param (CHECK_MANDATORY);
768
769   u8 is_delayed = 0;
770   if (have_delayed_token)
771     {
772       static const char yes[] = "yes";
773       static const char no[] = "no";
774       if (!memcmp (delayed_token, yes, sizeof (yes) - 1))
775         {
776           is_delayed = 1;
777         }
778       else if (!memcmp (delayed_token, no, sizeof (no) - 1))
779         {
780           is_delayed = 0;
781         }
782       else
783         {
784           ret =
785             clib_error_return (0,
786                                "Unrecognized value for `%s' parameter: `%v'",
787                                DELAYED_STR, delayed_token);
788           goto out;
789         }
790     }
791
792   if (have_bfd_key_id && bfd_key_id > 255)
793     {
794       ret = clib_error_return (0, "%s value `%u' out of range <1,255>",
795                                BFD_KEY_ID_STR, bfd_key_id);
796       goto out;
797     }
798
799   vnet_api_error_t rv =
800     bfd_udp_auth_activate (sw_if_index, &local_addr, &peer_addr, conf_key_id,
801                            bfd_key_id, is_delayed);
802   if (rv)
803     {
804       ret =
805         clib_error_return (0,
806                            "`bfd_udp_auth_activate' API call failed, rv=%d:%U",
807                            (int) rv, format_vnet_api_errno, rv);
808       goto out;
809     }
810
811 out:
812   return ret;
813 }
814
815 /* *INDENT-OFF* */
816 VLIB_CLI_COMMAND (bfd_cli_udp_session_auth_activate_command, static) = {
817   .path = "bfd udp session auth activate",
818   .short_help = "bfd udp session auth activate"
819                 " interface <interface>"
820                 " local-addr <local-address>"
821                 " peer-addr <peer-address>"
822                 " conf-key-id <config key ID>"
823                 " bfd-key-id <BFD key ID>"
824                 " [ delayed <yes|no> ]",
825   .function = bfd_cli_udp_session_auth_activate,
826 };
827
828 static clib_error_t *
829 bfd_cli_udp_session_auth_deactivate (vlib_main_t *vm, unformat_input_t *input,
830                                      CLIB_UNUSED (vlib_cli_command_t *lmd))
831 {
832   clib_error_t *ret = NULL;
833   unformat_input_t _line_input, *line_input = &_line_input;
834 #define foreach_bfd_cli_udp_session_auth_deactivate_cli_param(F)  \
835   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",            \
836      unformat_vnet_sw_interface, &vnet_main)                      \
837   F (ip46_address_t, local_addr, LOCAL_ADDR_STR, mandatory, "%U", \
838      unformat_ip46_address)                                       \
839   F (ip46_address_t, peer_addr, PEER_ADDR_STR, mandatory, "%U",   \
840      unformat_ip46_address)                                       \
841   F (u8 *, delayed_token, DELAYED_STR, optional, "%v")
842
843   foreach_bfd_cli_udp_session_auth_deactivate_cli_param (DECLARE);
844
845   /* Get a line of input. */
846   if (!unformat_user (input, unformat_line_input, line_input))
847     return 0;
848
849   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
850     {
851       int something_parsed = 0;
852       foreach_bfd_cli_udp_session_auth_deactivate_cli_param (UNFORMAT);
853
854       if (!something_parsed)
855         {
856           ret = clib_error_return (0, "Unknown input `%U'",
857                                    format_unformat_error, input);
858           goto out;
859         }
860     }
861
862   foreach_bfd_cli_udp_session_auth_deactivate_cli_param (CHECK_MANDATORY);
863
864   u8 is_delayed = 0;
865   if (have_delayed_token)
866     {
867       static const char yes[] = "yes";
868       static const char no[] = "no";
869       if (!memcmp (delayed_token, yes, sizeof (yes) - 1))
870         {
871           is_delayed = 1;
872         }
873       else if (!memcmp (delayed_token, no, sizeof (no) - 1))
874         {
875           is_delayed = 0;
876         }
877       else
878         {
879           ret = clib_error_return (
880               0, "Unrecognized value for `%s' parameter: `%v'", DELAYED_STR,
881               delayed_token);
882           goto out;
883         }
884     }
885
886   vnet_api_error_t rv = bfd_udp_auth_deactivate (sw_if_index, &local_addr,
887                                                  &peer_addr, is_delayed);
888   if (rv)
889     {
890       ret = clib_error_return (
891           0, "`bfd_udp_auth_deactivate' API call failed, rv=%d:%U", (int)rv,
892           format_vnet_api_errno, rv);
893       goto out;
894     }
895
896 out:
897   return ret;
898 }
899
900 /* *INDENT-OFF* */
901 VLIB_CLI_COMMAND (bfd_cli_udp_session_auth_deactivate_command, static) = {
902   .path = "bfd udp session auth deactivate",
903   .short_help = "bfd udp session auth deactivate"
904                 " interface <interface>"
905                 " local-addr <local-address>"
906                 " peer-addr <peer-address>"
907                 "[ delayed <yes|no> ]",
908   .function = bfd_cli_udp_session_auth_deactivate,
909 };
910 /* *INDENT-ON* */
911
912 static clib_error_t *
913 bfd_cli_udp_set_echo_source (vlib_main_t * vm, unformat_input_t * input,
914                              CLIB_UNUSED (vlib_cli_command_t * lmd))
915 {
916   clib_error_t *ret = NULL;
917   unformat_input_t _line_input, *line_input = &_line_input;
918 #define foreach_bfd_cli_udp_set_echo_source_cli_param(F) \
919   F (u32, sw_if_index, INTERFACE_STR, mandatory, "%U",   \
920      unformat_vnet_sw_interface, &vnet_main)
921
922   foreach_bfd_cli_udp_set_echo_source_cli_param (DECLARE);
923
924   /* Get a line of input. */
925   if (!unformat_user (input, unformat_line_input, line_input))
926     return 0;
927
928   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
929     {
930       int something_parsed = 0;
931       foreach_bfd_cli_udp_set_echo_source_cli_param (UNFORMAT);
932
933       if (!something_parsed)
934         {
935           ret = clib_error_return (0, "Unknown input `%U'",
936                                    format_unformat_error, line_input);
937           goto out;
938         }
939     }
940
941   foreach_bfd_cli_udp_set_echo_source_cli_param (CHECK_MANDATORY);
942
943   vnet_api_error_t rv = bfd_udp_set_echo_source (sw_if_index);
944   if (rv)
945     {
946       ret =
947         clib_error_return (0,
948                            "`bfd_udp_set_echo_source' API call failed, rv=%d:%U",
949                            (int) rv, format_vnet_api_errno, rv);
950       goto out;
951     }
952
953 out:
954   return ret;
955 }
956
957 /* *INDENT-OFF* */
958 VLIB_CLI_COMMAND (bfd_cli_udp_set_echo_source_cmd, static) = {
959   .path = "bfd udp echo-source set",
960   .short_help = "bfd udp echo-source set interface <interface>",
961   .function = bfd_cli_udp_set_echo_source,
962 };
963 /* *INDENT-ON* */
964
965 static clib_error_t *
966 bfd_cli_udp_del_echo_source (vlib_main_t * vm, unformat_input_t * input,
967                              CLIB_UNUSED (vlib_cli_command_t * lmd))
968 {
969   vnet_api_error_t rv = bfd_udp_del_echo_source ();
970   if (rv)
971     {
972       return clib_error_return (0,
973                                 "`bfd_udp_del_echo_source' API call failed, rv=%d:%U",
974                                 (int) rv, format_vnet_api_errno, rv);
975     }
976
977   return 0;
978 }
979
980 /* *INDENT-OFF* */
981 VLIB_CLI_COMMAND (bfd_cli_udp_del_echo_source_cmd, static) = {
982   .path = "bfd udp echo-source del",
983   .short_help = "bfd udp echo-source del",
984   .function = bfd_cli_udp_del_echo_source,
985 };
986 /* *INDENT-ON* */
987
988 /*
989  * fd.io coding-style-patch-verification: ON
990  *
991  * Local Variables:
992  * eval: (c-set-style "gnu")
993  * End:
994  */