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);
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,
213 VLIB_CLI_COMMAND (vrrp_show_vr_command, static) =
215 .path = "show vrrp vr",
217 "show vrrp vr [(<intf_name>|sw_if_index <n>)]",
218 .function = vrrp_show_vr_command_fn,
222 static clib_error_t *
223 vrrp_proto_start_stop_command_fn (vlib_main_t * vm,
224 unformat_input_t * input,
225 vlib_cli_command_t * cmd)
227 vrrp_main_t *vmp = &vrrp_main;
228 vrrp_vr_key_t vr_key;
231 u8 is_ipv6, is_start, is_stop;
234 clib_memset (&vr_key, 0, sizeof (vr_key));
239 is_ipv6 = is_start = is_stop = 0;
241 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
243 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
246 else if (unformat (input, "vr_id %u", &vr_id))
248 else if (unformat (input, "ipv6"))
250 else if (unformat (input, "start"))
252 else if (unformat (input, "stop"))
255 return clib_error_return (0, "unknown input `%U'",
256 format_unformat_error, input);
259 if (is_start == is_stop)
260 return clib_error_return (0, "One of start or stop must be specified");
261 else if (sw_if_index == ~0)
262 return clib_error_return (0, "Please specify an interface...");
264 return clib_error_return (0, "Invalid VR ID...");
266 vr_key.sw_if_index = sw_if_index;
267 vr_key.vr_id = vr_id;
268 vr_key.is_ipv6 = (is_ipv6 != 0);
270 rv = vrrp_vr_start_stop (is_start, &vr_key);
276 case VNET_API_ERROR_INIT_FAILED:
277 return clib_error_return (0, "Cannot start unicast VR without peers");
280 return clib_error_return (0, "vrrp_vr_start_stop returned %d", rv);
287 static clib_error_t *
288 vrrp_peers_command_fn (vlib_main_t * vm, unformat_input_t * input,
289 vlib_cli_command_t * cmd)
291 vrrp_main_t *vmp = &vrrp_main;
292 vrrp_vr_key_t vr_key;
297 ip46_address_t addr, *addrs;
298 u8 n_addrs4, n_addrs6;
299 clib_error_t *ret = 0;
301 clib_memset (&vr_key, 0, sizeof (vr_key));
305 n_addrs4 = n_addrs6 = 0;
310 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
312 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
315 else if (unformat (input, "vr_id %u", &vr_id))
317 else if (unformat (input, "ipv6"))
319 else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
322 vec_add1 (addrs, addr);
324 else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
327 vec_add1 (addrs, addr);
331 ret = clib_error_return (0, "unknown input `%U'",
332 format_unformat_error, input);
337 if (sw_if_index == ~0)
338 ret = clib_error_return (0, "Please specify an interface...");
340 ret = clib_error_return (0, "Invalid VR ID...");
341 else if (n_addrs4 && (n_addrs6 || is_ipv6))
342 ret = clib_error_return (0, "Mismatched address families");
344 if (ret) /* data validation failed */
347 vr_key.sw_if_index = sw_if_index;
348 vr_key.vr_id = vr_id;
349 vr_key.is_ipv6 = (is_ipv6 != 0);
351 rv = vrrp_vr_set_peers (&vr_key, addrs);
357 case VNET_API_ERROR_INVALID_ARGUMENT:
358 ret = clib_error_return (0, "Peers can only be set on a unicast VR");
360 case VNET_API_ERROR_RSRC_IN_USE:
361 ret = clib_error_return (0, "Cannot set peers on a running VR");
363 case VNET_API_ERROR_INVALID_DST_ADDRESS:
364 ret = clib_error_return (0, "No peer addresses provided");
367 ret = clib_error_return (0, "vrrp_vr_set_peers returned %d", rv);
378 VLIB_CLI_COMMAND (vrrp_proto_start_stop_command, static) =
380 .path = "vrrp proto",
382 "vrrp proto (start|stop) (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6]",
383 .function = vrrp_proto_start_stop_command_fn,
388 VLIB_CLI_COMMAND (vrrp_peers_command, static) =
390 .path = "vrrp peers",
392 "vrrp peers (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6] <peer1_addr> [<peer2_addr> ...]",
393 .function = vrrp_peers_command_fn,
397 static clib_error_t *
398 vrrp_vr_track_if_command_fn (vlib_main_t * vm,
399 unformat_input_t * input,
400 vlib_cli_command_t * cmd)
402 vnet_main_t *vnm = vnet_get_main ();
403 vrrp_main_t *vmp = &vrrp_main;
404 u32 sw_if_index, track_if_index, vr_id, priority;
406 clib_error_t *ret = 0;
407 vrrp_vr_tracking_if_t *track_intfs = 0, *track_intf;
417 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
419 if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
422 else if (unformat (input, "add"))
424 else if (unformat (input, "del"))
426 else if (unformat (input, "vr_id %u", &vr_id))
428 else if (unformat (input, "ipv6"))
430 else if (unformat (input, "track-index %u priority %u", &track_if_index,
433 vec_add2 (track_intfs, track_intf, 1);;
434 track_intf->sw_if_index = track_if_index;
435 track_intf->priority = priority;
441 if (sw_if_index == ~0)
442 ret = clib_error_return (0, "Please specify an interface");
443 else if (!vr_id || vr_id > 0xff)
444 ret = clib_error_return (0, "VR ID must be between 1 and 255");
445 else if (is_add == is_del)
446 ret = clib_error_return (0, "One of add,delete must be specified");
451 vr = vrrp_vr_lookup (sw_if_index, vr_id, is_ipv6);
454 ret = clib_error_return (0, "VR not found");
458 vec_foreach (track_intf, track_intfs)
460 if (!vnet_sw_interface_is_valid (vnm, track_intf->sw_if_index))
462 ret = clib_error_return (0, "tracked intf sw_if_index %u invalid",
463 track_intf->sw_if_index);
466 if (!track_intf->priority)
468 ret = clib_error_return (0, "tracked intf priority must be > 0");
471 if (track_intf->priority >= vr->config.priority)
473 ret = clib_error_return (0, "tracked intf priority must be less "
474 "than VR priority (%u)",
475 vr->config.priority);
480 rv = vrrp_vr_tracking_ifs_add_del (vr, track_intfs, is_add);
482 ret = clib_error_return (0, "vrrp_vr_tracking_ifs_add_del returned %d",
486 vec_free (track_intfs);
492 VLIB_CLI_COMMAND (vrrp_vr_track_if_command, static) =
494 .path = "vrrp vr track-if",
496 "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> ...]",
497 .function = vrrp_vr_track_if_command_fn,
502 * fd.io coding-style-patch-verification: ON
505 * eval: (c-set-style "gnu")