docs: Use newer Ubuntu LTS in tutorial
[vpp.git] / src / plugins / vrrp / vrrp_cli.c
1 /*
2  * vrrp_cli.c - vrrp plugin debug CLI commands
3  *
4  * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  */
9
10 #include <vnet/vnet.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vrrp/vrrp.h>
13
14 #include <vlibapi/api.h>
15 #include <vlibmemory/api.h>
16 #include <vpp/app/version.h>
17
18
19 static clib_error_t *
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)
23 {
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;
30   int rv;
31
32   clib_memset (&vr_conf, 0, sizeof (vr_conf));
33
34   /* RFC 5798 - preempt enabled by default */
35   vr_conf.flags = VRRP_VR_PREEMPT;
36
37   addrs = 0;
38   n_addrs4 = n_addrs6 = 0;
39
40   /* defaults */
41   sw_if_index = ~0;
42   vr_id = 0;
43   priority = 100;
44   interval = 100;
45
46   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
47     {
48       clib_memset (&addr, 0, sizeof (addr));
49
50       if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
51                     &sw_if_index))
52         ;
53       else if (unformat (input, "vr_id %u", &vr_id))
54         ;
55       else if (unformat (input, "ipv6"))
56         vr_conf.flags |= VRRP_VR_IPV6;
57       else if (unformat (input, "priority %u", &priority))
58         ;
59       else if (unformat (input, "interval %u", &interval))
60         ;
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))
68         {
69           n_addrs4++;
70           vec_add1 (addrs, addr);
71         }
72       else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
73         {
74           n_addrs6++;
75           vec_add1 (addrs, addr);
76         }
77       else
78         break;
79     }
80
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...");
85
86   if (is_add)
87     {
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");
94     }
95
96   if (ret)                      /* data validation failed */
97     goto done;
98
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;
104
105   rv = vrrp_vr_add_del (is_add, &vr_conf, NULL);
106
107   switch (rv)
108     {
109     case 0:
110       break;
111
112       /* adding */
113     case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
114       ret = clib_error_return (0, "Failed to add VR that already exists");
115       goto done;
116       break;
117
118     case VNET_API_ERROR_INVALID_SRC_ADDRESS:
119       ret = clib_error_return (0, "Failed to add VR with no IP addresses");
120       goto done;
121       break;
122
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");
126       goto done;
127       break;
128
129       /* deleting */
130     case VNET_API_ERROR_NO_SUCH_ENTRY:
131       ret = clib_error_return (0, "Failed to delete VR which does not exist");
132       goto done;
133       break;
134
135     default:
136       ret = clib_error_return (0, "vrrp_vr_add_del returned %d", rv);
137       goto done;
138       break;
139     }
140
141 done:
142   vec_free (addrs);
143
144   return ret;
145 }
146
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)
150 {
151   return vrrp_vr_add_del_command_fn (vm, input, cmd, 1 /* is_add */ );
152 }
153
154 VLIB_CLI_COMMAND (vrrp_vr_add_command, static) =
155 {
156   .path = "vrrp vr add",
157   .short_help =
158   "vrrp vr add <interface> [vr_id <n>] [ipv6] [priority <value>] [interval <value>] [no_preempt] [accept_mode] [unicast] [<ip_addr> ...]",
159   .function = vrrp_vr_add_command_fn,
160 };
161
162 static clib_error_t *
163 vrrp_vr_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
164                         vlib_cli_command_t * cmd)
165 {
166   return vrrp_vr_add_del_command_fn (vm, input, cmd, 0 /* is_add */ );
167 }
168
169 VLIB_CLI_COMMAND (vrrp_vr_del_command, static) =
170 {
171   .path = "vrrp vr del",
172   .short_help = "vrrp vr del <interface> [vr_id <n>] [ipv6]",
173   .function = vrrp_vr_del_command_fn,
174 };
175
176 static clib_error_t *
177 vrrp_show_vr_command_fn (vlib_main_t * vm,
178                          unformat_input_t * input, vlib_cli_command_t * cmd)
179 {
180   vrrp_main_t *vmp = &vrrp_main;
181   vrrp_vr_t *vr;
182   u32 sw_if_index = ~0;
183
184   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
185     {
186       if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
187                     &sw_if_index))
188         ;
189       else if (unformat (input, "sw_if_index %u", &sw_if_index))
190         ;
191       else
192         break;
193     }
194
195   pool_foreach (vr, vmp->vrs)
196   {
197
198     if (sw_if_index && (sw_if_index != ~0) &&
199         (sw_if_index != vr->config.sw_if_index))
200       continue;
201     vlib_cli_output (vm, "%U", format_vrrp_vr, vr);
202   }
203
204   return 0;
205 }
206
207 VLIB_CLI_COMMAND (vrrp_show_vr_command, static) =
208 {
209   .path = "show vrrp vr",
210   .short_help =
211   "show vrrp vr [(<intf_name>|sw_if_index <n>)]",
212   .function = vrrp_show_vr_command_fn,
213 };
214
215 static clib_error_t *
216 vrrp_proto_start_stop_command_fn (vlib_main_t * vm,
217                                   unformat_input_t * input,
218                                   vlib_cli_command_t * cmd)
219 {
220   vrrp_main_t *vmp = &vrrp_main;
221   vrrp_vr_key_t vr_key;
222   u32 sw_if_index;
223   u32 vr_id;
224   u8 is_ipv6, is_start, is_stop;
225   int rv;
226
227   clib_memset (&vr_key, 0, sizeof (vr_key));
228
229   /* defaults */
230   sw_if_index = ~0;
231   vr_id = 0;
232   is_ipv6 = is_start = is_stop = 0;
233
234   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
235     {
236       if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
237                     &sw_if_index))
238         ;
239       else if (unformat (input, "sw_if_index %u", &sw_if_index))
240         ;
241       else if (unformat (input, "vr_id %u", &vr_id))
242         ;
243       else if (unformat (input, "ipv6"))
244         is_ipv6 = 1;
245       else if (unformat (input, "start"))
246         is_start = 1;
247       else if (unformat (input, "stop"))
248         is_stop = 1;
249       else
250         return clib_error_return (0, "unknown input `%U'",
251                                   format_unformat_error, input);
252     }
253
254   if (is_start == is_stop)
255     return clib_error_return (0, "One of start or stop must be specified");
256   else if (sw_if_index == ~0)
257     return clib_error_return (0, "Please specify an interface...");
258   else if (!vr_id)
259     return clib_error_return (0, "Invalid VR ID...");
260
261   vr_key.sw_if_index = sw_if_index;
262   vr_key.vr_id = vr_id;
263   vr_key.is_ipv6 = (is_ipv6 != 0);
264
265   rv = vrrp_vr_start_stop (is_start, &vr_key);
266
267   switch (rv)
268     {
269     case 0:
270       break;
271     case VNET_API_ERROR_INIT_FAILED:
272       return clib_error_return (0, "Cannot start unicast VR without peers");
273       break;
274     default:
275       return clib_error_return (0, "vrrp_vr_start_stop returned %d", rv);
276       break;
277     }
278
279   return 0;
280 }
281
282 static clib_error_t *
283 vrrp_peers_command_fn (vlib_main_t * vm, unformat_input_t * input,
284                        vlib_cli_command_t * cmd)
285 {
286   vrrp_main_t *vmp = &vrrp_main;
287   vrrp_vr_key_t vr_key;
288   u32 sw_if_index;
289   u32 vr_id;
290   u8 is_ipv6;
291   int rv;
292   ip46_address_t addr, *addrs;
293   u8 n_addrs4, n_addrs6;
294   clib_error_t *ret = 0;
295
296   clib_memset (&vr_key, 0, sizeof (vr_key));
297
298   /* defaults */
299   addrs = 0;
300   n_addrs4 = n_addrs6 = 0;
301   sw_if_index = ~0;
302   vr_id = 0;
303   is_ipv6 = 0;
304
305   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
306     {
307       if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
308                     &sw_if_index))
309         ;
310       else if (unformat (input, "sw_if_index %u", &sw_if_index))
311         ;
312       else if (unformat (input, "vr_id %u", &vr_id))
313         ;
314       else if (unformat (input, "ipv6"))
315         is_ipv6 = 1;
316       else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
317         {
318           n_addrs4++;
319           vec_add1 (addrs, addr);
320         }
321       else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
322         {
323           n_addrs6++;
324           vec_add1 (addrs, addr);
325         }
326       else
327         {
328           ret = clib_error_return (0, "unknown input `%U'",
329                                    format_unformat_error, input);
330           goto done;
331         }
332     }
333
334   if (sw_if_index == ~0)
335     ret = clib_error_return (0, "Please specify an interface...");
336   else if (!vr_id)
337     ret = clib_error_return (0, "Invalid VR ID...");
338   else if (n_addrs4 && (n_addrs6 || is_ipv6))
339     ret = clib_error_return (0, "Mismatched address families");
340
341   if (ret)                      /* data validation failed */
342     goto done;
343
344   vr_key.sw_if_index = sw_if_index;
345   vr_key.vr_id = vr_id;
346   vr_key.is_ipv6 = (is_ipv6 != 0);
347
348   rv = vrrp_vr_set_peers (&vr_key, addrs);
349
350   switch (rv)
351     {
352     case 0:
353       break;
354     case VNET_API_ERROR_INVALID_ARGUMENT:
355       ret = clib_error_return (0, "Peers can only be set on a unicast VR");
356       break;
357     case VNET_API_ERROR_RSRC_IN_USE:
358       ret = clib_error_return (0, "Cannot set peers on a running VR");
359       break;
360     case VNET_API_ERROR_INVALID_DST_ADDRESS:
361       ret = clib_error_return (0, "No peer addresses provided");
362       break;
363     default:
364       ret = clib_error_return (0, "vrrp_vr_set_peers returned %d", rv);
365       break;
366     }
367
368 done:
369   vec_free (addrs);
370
371   return ret;
372 }
373
374 VLIB_CLI_COMMAND (vrrp_proto_start_stop_command, static) =
375 {
376   .path = "vrrp proto",
377   .short_help =
378   "vrrp proto (start|stop) (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6]",
379   .function = vrrp_proto_start_stop_command_fn,
380 };
381
382 VLIB_CLI_COMMAND (vrrp_peers_command, static) =
383 {
384   .path = "vrrp peers",
385   .short_help =
386   "vrrp peers (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6] <peer1_addr> [<peer2_addr> ...]",
387   .function = vrrp_peers_command_fn,
388 };
389
390 static clib_error_t *
391 vrrp_vr_track_if_command_fn (vlib_main_t * vm,
392                              unformat_input_t * input,
393                              vlib_cli_command_t * cmd)
394 {
395   vnet_main_t *vnm = vnet_get_main ();
396   vrrp_main_t *vmp = &vrrp_main;
397   u32 sw_if_index, track_if_index, vr_id, priority;
398   u8 is_ipv6 = 0;
399   clib_error_t *ret = 0;
400   vrrp_vr_tracking_if_t *track_intfs = 0, *track_intf;
401   vrrp_vr_t *vr;
402   u8 is_add, is_del;
403   int rv;
404
405   /* defaults */
406   sw_if_index = ~0;
407   vr_id = 0;
408   is_add = is_del = 0;
409
410   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
411     {
412       if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
413                     &sw_if_index))
414         ;
415       else if (unformat (input, "sw_if_index %u", &sw_if_index))
416         ;
417       else if (unformat (input, "add"))
418         is_add = 1;
419       else if (unformat (input, "del"))
420         is_del = 1;
421       else if (unformat (input, "vr_id %u", &vr_id))
422         ;
423       else if (unformat (input, "ipv6"))
424         is_ipv6 = 1;
425       else if (unformat (input, "track-index %u priority %u", &track_if_index,
426                          &priority))
427         {
428           vec_add2 (track_intfs, track_intf, 1);;
429           track_intf->sw_if_index = track_if_index;
430           track_intf->priority = priority;
431         }
432       else
433         break;
434     }
435
436   if (sw_if_index == ~0)
437     ret = clib_error_return (0, "Please specify an interface");
438   else if (!vr_id || vr_id > 0xff)
439     ret = clib_error_return (0, "VR ID must be between 1 and 255");
440   else if (is_add == is_del)
441     ret = clib_error_return (0, "One of add,delete must be specified");
442
443   if (ret)
444     goto done;
445
446   vr = vrrp_vr_lookup (sw_if_index, vr_id, is_ipv6);
447   if (!vr)
448     {
449       ret = clib_error_return (0, "VR not found");
450       goto done;
451     }
452
453   vec_foreach (track_intf, track_intfs)
454   {
455     if (!vnet_sw_interface_is_valid (vnm, track_intf->sw_if_index))
456       {
457         ret = clib_error_return (0, "tracked intf sw_if_index %u invalid",
458                                  track_intf->sw_if_index);
459         goto done;
460       }
461     if (!track_intf->priority)
462       {
463         ret = clib_error_return (0, "tracked intf priority must be > 0");
464         goto done;
465       }
466     if (track_intf->priority >= vr->config.priority)
467       {
468         ret = clib_error_return (0, "tracked intf priority must be less "
469                                  "than VR priority (%u)",
470                                  vr->config.priority);
471         goto done;
472       }
473   }
474
475   rv = vrrp_vr_tracking_ifs_add_del (vr, track_intfs, is_add);
476   if (rv)
477     ret = clib_error_return (0, "vrrp_vr_tracking_ifs_add_del returned %d",
478                              rv);
479
480 done:
481   vec_free (track_intfs);
482
483   return ret;
484 }
485
486 VLIB_CLI_COMMAND (vrrp_vr_track_if_command, static) =
487 {
488   .path = "vrrp vr track-if",
489   .short_help =
490   "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> ...]",
491   .function = vrrp_vr_track_if_command_fn,
492 };
493
494 /*
495  * fd.io coding-style-patch-verification: ON
496  *
497  * Local Variables:
498  * eval: (c-set-style "gnu")
499  * End:
500  */