2 * vrrp_cli.c - vrrp plugin debug CLI commands
4 * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
6 * SPDX-License-Identifier: Apache-2.0
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vrrp/vrrp.h>
14 #include <vlibapi/api.h>
15 #include <vlibmemory/api.h>
16 #include <vpp/app/version.h>
20 vrrp_vr_add_del_command_fn (vlib_main_t * vm,
21 unformat_input_t * input,
22 vlib_cli_command_t * cmd, u8 is_add)
24 vrrp_main_t *vmp = &vrrp_main;
25 vrrp_vr_config_t vr_conf;
26 u32 sw_if_index, vr_id, priority, interval;
27 ip46_address_t addr, *addrs;
28 u8 n_addrs4, n_addrs6;
29 clib_error_t *ret = 0;
32 clib_memset (&vr_conf, 0, sizeof (vr_conf));
34 /* RFC 5798 - preempt enabled by default */
35 vr_conf.flags = VRRP_VR_PREEMPT;
38 n_addrs4 = n_addrs6 = 0;
46 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
48 clib_memset (&addr, 0, sizeof (addr));
50 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
53 else if (unformat (input, "vr_id %u", &vr_id))
55 else if (unformat (input, "ipv6"))
56 vr_conf.flags |= VRRP_VR_IPV6;
57 else if (unformat (input, "priority %u", &priority))
59 else if (unformat (input, "interval %u", &interval))
61 else if (unformat (input, "no_preempt"))
62 vr_conf.flags &= ~VRRP_VR_PREEMPT;
63 else if (unformat (input, "accept_mode"))
64 vr_conf.flags |= VRRP_VR_ACCEPT;
65 else if (unformat (input, "unicast"))
66 vr_conf.flags |= VRRP_VR_UNICAST;
67 else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
70 vec_add1 (addrs, addr);
72 else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
75 vec_add1 (addrs, addr);
81 if (sw_if_index == ~0)
82 ret = clib_error_return (0, "Please specify an interface...");
83 else if (!vr_id || vr_id > 0xff)
84 ret = clib_error_return (0, "VR ID must be between 1 and 255...");
88 if (!priority || priority > 0xff)
89 ret = clib_error_return (0, "priority must be between 1 and 255...");
90 else if (interval > 0xffff)
91 ret = clib_error_return (0, "interval must be <= 65535...");
92 else if (n_addrs4 && (n_addrs6 || vr_conf.flags & VRRP_VR_IPV6))
93 ret = clib_error_return (0, "Mismatched address families");
96 if (ret) /* data validation failed */
99 vr_conf.sw_if_index = sw_if_index;
100 vr_conf.vr_id = (u8) vr_id;
101 vr_conf.priority = (u8) priority;
102 vr_conf.adv_interval = (u16) interval;
103 vr_conf.vr_addrs = addrs;
105 rv = vrrp_vr_add_del (is_add, &vr_conf, NULL);
113 case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
114 ret = clib_error_return (0, "Failed to add VR that already exists");
118 case VNET_API_ERROR_INVALID_SRC_ADDRESS:
119 ret = clib_error_return (0, "Failed to add VR with no IP addresses");
123 case VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE:
124 ret = clib_error_return (0, "Failed to add VR with priority 255 - "
125 "VR IP addresses not configured on interface");
130 case VNET_API_ERROR_NO_SUCH_ENTRY:
131 ret = clib_error_return (0, "Failed to delete VR which does not exist");
136 ret = clib_error_return (0, "vrrp_vr_add_del returned %d", rv);
147 static clib_error_t *
148 vrrp_vr_add_command_fn (vlib_main_t * vm, unformat_input_t * input,
149 vlib_cli_command_t * cmd)
151 return vrrp_vr_add_del_command_fn (vm, input, cmd, 1 /* is_add */ );
155 VLIB_CLI_COMMAND (vrrp_vr_add_command, static) =
157 .path = "vrrp vr add",
159 "vrrp vr add <interface> [vr_id <n>] [ipv6] [priority <value>] [interval <value>] [no_preempt] [accept_mode] [unicast] [<ip_addr> ...]",
160 .function = vrrp_vr_add_command_fn,
164 static clib_error_t *
165 vrrp_vr_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
166 vlib_cli_command_t * cmd)
168 return vrrp_vr_add_del_command_fn (vm, input, cmd, 0 /* is_add */ );
172 VLIB_CLI_COMMAND (vrrp_vr_del_command, static) =
174 .path = "vrrp vr del",
175 .short_help = "vrrp vr del <interface> [vr_id <n>] [ipv6]",
176 .function = vrrp_vr_del_command_fn,
180 static clib_error_t *
181 vrrp_show_vr_command_fn (vlib_main_t * vm,
182 unformat_input_t * input, vlib_cli_command_t * cmd)
184 vrrp_main_t *vmp = &vrrp_main;
186 u32 sw_if_index = ~0;
188 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
190 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
193 else if (unformat (input, "sw_if_index %u", &sw_if_index))
199 pool_foreach (vr, vmp->vrs)
202 if (sw_if_index && (sw_if_index != ~0) &&
203 (sw_if_index != vr->config.sw_if_index))
205 vlib_cli_output (vm, "%U", format_vrrp_vr, vr);
212 VLIB_CLI_COMMAND (vrrp_show_vr_command, static) =
214 .path = "show vrrp vr",
216 "show vrrp vr [(<intf_name>|sw_if_index <n>)]",
217 .function = vrrp_show_vr_command_fn,
221 static clib_error_t *
222 vrrp_proto_start_stop_command_fn (vlib_main_t * vm,
223 unformat_input_t * input,
224 vlib_cli_command_t * cmd)
226 vrrp_main_t *vmp = &vrrp_main;
227 vrrp_vr_key_t vr_key;
230 u8 is_ipv6, is_start, is_stop;
233 clib_memset (&vr_key, 0, sizeof (vr_key));
238 is_ipv6 = is_start = is_stop = 0;
240 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
242 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
245 else if (unformat (input, "vr_id %u", &vr_id))
247 else if (unformat (input, "ipv6"))
249 else if (unformat (input, "start"))
251 else if (unformat (input, "stop"))
254 return clib_error_return (0, "unknown input `%U'",
255 format_unformat_error, input);
258 if (is_start == is_stop)
259 return clib_error_return (0, "One of start or stop must be specified");
260 else if (sw_if_index == ~0)
261 return clib_error_return (0, "Please specify an interface...");
263 return clib_error_return (0, "Invalid VR ID...");
265 vr_key.sw_if_index = sw_if_index;
266 vr_key.vr_id = vr_id;
267 vr_key.is_ipv6 = (is_ipv6 != 0);
269 rv = vrrp_vr_start_stop (is_start, &vr_key);
275 case VNET_API_ERROR_INIT_FAILED:
276 return clib_error_return (0, "Cannot start unicast VR without peers");
279 return clib_error_return (0, "vrrp_vr_start_stop returned %d", rv);
286 static clib_error_t *
287 vrrp_peers_command_fn (vlib_main_t * vm, unformat_input_t * input,
288 vlib_cli_command_t * cmd)
290 vrrp_main_t *vmp = &vrrp_main;
291 vrrp_vr_key_t vr_key;
296 ip46_address_t addr, *addrs;
297 u8 n_addrs4, n_addrs6;
298 clib_error_t *ret = 0;
300 clib_memset (&vr_key, 0, sizeof (vr_key));
304 n_addrs4 = n_addrs6 = 0;
309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
311 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
314 else if (unformat (input, "vr_id %u", &vr_id))
316 else if (unformat (input, "ipv6"))
318 else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
321 vec_add1 (addrs, addr);
323 else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
326 vec_add1 (addrs, addr);
330 ret = clib_error_return (0, "unknown input `%U'",
331 format_unformat_error, input);
336 if (sw_if_index == ~0)
337 ret = clib_error_return (0, "Please specify an interface...");
339 ret = clib_error_return (0, "Invalid VR ID...");
340 else if (n_addrs4 && (n_addrs6 || is_ipv6))
341 ret = clib_error_return (0, "Mismatched address families");
343 if (ret) /* data validation failed */
346 vr_key.sw_if_index = sw_if_index;
347 vr_key.vr_id = vr_id;
348 vr_key.is_ipv6 = (is_ipv6 != 0);
350 rv = vrrp_vr_set_peers (&vr_key, addrs);
356 case VNET_API_ERROR_INVALID_ARGUMENT:
357 ret = clib_error_return (0, "Peers can only be set on a unicast VR");
359 case VNET_API_ERROR_RSRC_IN_USE:
360 ret = clib_error_return (0, "Cannot set peers on a running VR");
362 case VNET_API_ERROR_INVALID_DST_ADDRESS:
363 ret = clib_error_return (0, "No peer addresses provided");
366 ret = clib_error_return (0, "vrrp_vr_set_peers returned %d", rv);
377 VLIB_CLI_COMMAND (vrrp_proto_start_stop_command, static) =
379 .path = "vrrp proto",
381 "vrrp proto (start|stop) (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6]",
382 .function = vrrp_proto_start_stop_command_fn,
387 VLIB_CLI_COMMAND (vrrp_peers_command, static) =
389 .path = "vrrp peers",
391 "vrrp peers (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6] <peer1_addr> [<peer2_addr> ...]",
392 .function = vrrp_peers_command_fn,
396 static clib_error_t *
397 vrrp_vr_track_if_command_fn (vlib_main_t * vm,
398 unformat_input_t * input,
399 vlib_cli_command_t * cmd)
401 vnet_main_t *vnm = vnet_get_main ();
402 vrrp_main_t *vmp = &vrrp_main;
403 u32 sw_if_index, track_if_index, vr_id, priority;
405 clib_error_t *ret = 0;
406 vrrp_vr_tracking_if_t *track_intfs = 0, *track_intf;
416 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
418 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
421 else if (unformat (input, "add"))
423 else if (unformat (input, "del"))
425 else if (unformat (input, "vr_id %u", &vr_id))
427 else if (unformat (input, "ipv6"))
429 else if (unformat (input, "track-index %u priority %u", &track_if_index,
432 vec_add2 (track_intfs, track_intf, 1);;
433 track_intf->sw_if_index = track_if_index;
434 track_intf->priority = priority;
440 if (sw_if_index == ~0)
441 ret = clib_error_return (0, "Please specify an interface");
442 else if (!vr_id || vr_id > 0xff)
443 ret = clib_error_return (0, "VR ID must be between 1 and 255");
444 else if (is_add == is_del)
445 ret = clib_error_return (0, "One of add,delete must be specified");
450 vr = vrrp_vr_lookup (sw_if_index, vr_id, is_ipv6);
453 ret = clib_error_return (0, "VR not found");
457 vec_foreach (track_intf, track_intfs)
459 if (!vnet_sw_interface_is_valid (vnm, track_intf->sw_if_index))
461 ret = clib_error_return (0, "tracked intf sw_if_index %u invalid",
462 track_intf->sw_if_index);
465 if (!track_intf->priority)
467 ret = clib_error_return (0, "tracked intf priority must be > 0");
470 if (track_intf->priority >= vr->config.priority)
472 ret = clib_error_return (0, "tracked intf priority must be less "
473 "than VR priority (%u)",
474 vr->config.priority);
479 rv = vrrp_vr_tracking_ifs_add_del (vr, track_intfs, is_add);
481 ret = clib_error_return (0, "vrrp_vr_tracking_ifs_add_del returned %d",
485 vec_free (track_intfs);
491 VLIB_CLI_COMMAND (vrrp_vr_track_if_command, static) =
493 .path = "vrrp vr track-if",
495 "vrrp vr track-if (add|del) (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6] track-index <n> priority <n> [ track-index <n> priority <n> ...]",
496 .function = vrrp_vr_track_if_command_fn,
501 * fd.io coding-style-patch-verification: ON
504 * eval: (c-set-style "gnu")