summaryrefslogtreecommitdiffstats
path: root/src/plugins/vrrp/vrrp_cli.c
blob: 4496bbb7333812da13fbfa8617a1507e75a2f3b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * vrrp_cli.c - vrrp plugin debug CLI commands
 *
 * Copyright 2019-2020 Rubicon Communications, LLC (Netgate)
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 */

#include <vnet/vnet.h>
#include <vnet/plugin/plugin.h>
#include <vrrp/vrrp.h>

#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vpp/app/version.h>


static clib_error_t *
vrrp_vr_add_del_command_fn (vlib_main_t * vm,
			    unformat_input_t * input,
			    vlib_cli_command_t * cmd, u8 is_add)
{
  vrrp_main_t *vmp = &vrrp_main;
  vrrp_vr_config_t vr_conf;
  u32 sw_if_index, vr_id, priority, interval;
  ip46_address_t addr, *addrs;
  u8 n_addrs4, n_addrs6;
  clib_error_t *ret = 0;
  int rv;

  clib_memset (&vr_conf, 0, sizeof (vr_conf));

  /* RFC 5798 - preempt enabled by default */
  vr_conf.flags = VRRP_VR_PREEMPT;

  addrs = 0;
  n_addrs4 = n_addrs6 = 0;

  /* defaults */
  sw_if_index = ~0;
  vr_id = 0;
  priority = 100;
  interval = 100;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      clib_memset (&addr, 0, sizeof (addr));

      if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
		    &sw_if_index))
	;
      else if (unformat (input, "vr_id %u", &vr_id))
	;
      else if (unformat (input, "ipv6"))
	vr_conf.flags |= VRRP_VR_IPV6;
      else if (unformat (input, "priority %u", &priority))
	;
      else if (unformat (input, "interval %u", &interval))
	;
      else if (unformat (input, "no_preempt"))
	vr_conf.flags &= ~VRRP_VR_PREEMPT;
      else if (unformat (input, "accept_mode"))
	vr_conf.flags |= VRRP_VR_ACCEPT;
      else if (unformat (input, "unicast"))
	vr_conf.flags |= VRRP_VR_UNICAST;
      else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
	{
	  n_addrs4++;
	  vec_add1 (addrs, addr);
	}
      else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
	{
	  n_addrs6++;
	  vec_add1 (addrs, addr);
	}
      else
	break;
    }

  if (sw_if_index == ~0)
    ret = clib_error_return (0, "Please specify an interface...");
  else if (!vr_id || vr_id > 0xff)
    ret = clib_error_return (0, "VR ID must be between 1 and 255...");

  if (is_add)
    {
      if (!priority || priority > 0xff)
	ret = clib_error_return (0, "priority must be between 1 and 255...");
      else if (interval > 0xffff)
	ret = clib_error_return (0, "interval must be <= 65535...");
      else if (n_addrs4 && (n_addrs6 || vr_conf.flags & VRRP_VR_IPV6))
	ret = clib_error_return (0, "Mismatched address families");
    }

  if (ret)			/* data validation failed */
    goto done;

  vr_conf.sw_if_index = sw_if_index;
  vr_conf.vr_id = (u8) vr_id;
  vr_conf.priority = (u8) priority;
  vr_conf.adv_interval = (u16) interval;
  vr_conf.vr_addrs = addrs;

  rv = vrrp_vr_add_del (is_add, &vr_conf, NULL);

  switch (rv)
    {
    case 0:
      break;

      /* adding */
    case VNET_API_ERROR_ENTRY_ALREADY_EXISTS:
      ret = clib_error_return (0, "Failed to add VR that already exists");
      goto done;
      break;

    case VNET_API_ERROR_INVALID_SRC_ADDRESS:
      ret = clib_error_return (0, "Failed to add VR with no IP addresses");
      goto done;
      break;

    case VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE:
      ret = clib_error_return (0, "Failed to add VR with priority 255 - "
			       "VR IP addresses not configured on interface");
      goto done;
      break;

      /* deleting */
    case VNET_API_ERROR_NO_SUCH_ENTRY:
      ret = clib_error_return (0, "Failed to delete VR which does not exist");
      goto done;
      break;

    default:
      ret = clib_error_return (0, "vrrp_vr_add_del returned %d", rv);
      goto done;
      break;
    }

done:
  vec_free (addrs);

  return ret;
}

static clib_error_t *
vrrp_vr_add_command_fn (vlib_main_t * vm, unformat_input_t * input,
			vlib_cli_command_t * cmd)
{
  return vrrp_vr_add_del_command_fn (vm, input, cmd, 1 /* is_add */ );
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_vr_add_command, static) =
{
  .path = "vrrp vr add",
  .short_help =
  "vrrp vr add <interface> [vr_id <n>] [ipv6] [priority <value>] [interval <value>] [no_preempt] [accept_mode] [unicast] [<ip_addr> ...]",
  .function = vrrp_vr_add_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
vrrp_vr_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
			vlib_cli_command_t * cmd)
{
  return vrrp_vr_add_del_command_fn (vm, input, cmd, 0 /* is_add */ );
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_vr_del_command, static) =
{
  .path = "vrrp vr del",
  .short_help = "vrrp vr del <interface> [vr_id <n>] [ipv6]",
  .function = vrrp_vr_del_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
vrrp_show_vr_command_fn (vlib_main_t * vm,
			 unformat_input_t * input, vlib_cli_command_t * cmd)
{
  vrrp_main_t *vmp = &vrrp_main;
  vrrp_vr_t *vr;
  u32 sw_if_index = ~0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
		    &sw_if_index))
	;
      else if (unformat (input, "sw_if_index %u", &sw_if_index))
	;
      else
	break;
    }

  pool_foreach (vr, vmp->vrs)
  {

    if (sw_if_index && (sw_if_index != ~0) &&
	(sw_if_index != vr->config.sw_if_index))
      continue;
    vlib_cli_output (vm, "%U", format_vrrp_vr, vr);
  }

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_show_vr_command, static) =
{
  .path = "show vrrp vr",
  .short_help =
  "show vrrp vr [(<intf_name>|sw_if_index <n>)]",
  .function = vrrp_show_vr_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
vrrp_proto_start_stop_command_fn (vlib_main_t * vm,
				  unformat_input_t * input,
				  vlib_cli_command_t * cmd)
{
  vrrp_main_t *vmp = &vrrp_main;
  vrrp_vr_key_t vr_key;
  u32 sw_if_index;
  u32 vr_id;
  u8 is_ipv6, is_start, is_stop;
  int rv;

  clib_memset (&vr_key, 0, sizeof (vr_key));

  /* defaults */
  sw_if_index = ~0;
  vr_id = 0;
  is_ipv6 = is_start = is_stop = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
		    &sw_if_index))
	;
      else if (unformat (input, "sw_if_index %u", &sw_if_index))
	;
      else if (unformat (input, "vr_id %u", &vr_id))
	;
      else if (unformat (input, "ipv6"))
	is_ipv6 = 1;
      else if (unformat (input, "start"))
	is_start = 1;
      else if (unformat (input, "stop"))
	is_stop = 1;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }

  if (is_start == is_stop)
    return clib_error_return (0, "One of start or stop must be specified");
  else if (sw_if_index == ~0)
    return clib_error_return (0, "Please specify an interface...");
  else if (!vr_id)
    return clib_error_return (0, "Invalid VR ID...");

  vr_key.sw_if_index = sw_if_index;
  vr_key.vr_id = vr_id;
  vr_key.is_ipv6 = (is_ipv6 != 0);

  rv = vrrp_vr_start_stop (is_start, &vr_key);

  switch (rv)
    {
    case 0:
      break;
    case VNET_API_ERROR_INIT_FAILED:
      return clib_error_return (0, "Cannot start unicast VR without peers");
      break;
    default:
      return clib_error_return (0, "vrrp_vr_start_stop returned %d", rv);
      break;
    }

  return 0;
}

static clib_error_t *
vrrp_peers_command_fn (vlib_main_t * vm, unformat_input_t * input,
		       vlib_cli_command_t * cmd)
{
  vrrp_main_t *vmp = &vrrp_main;
  vrrp_vr_key_t vr_key;
  u32 sw_if_index;
  u32 vr_id;
  u8 is_ipv6;
  int rv;
  ip46_address_t addr, *addrs;
  u8 n_addrs4, n_addrs6;
  clib_error_t *ret = 0;

  clib_memset (&vr_key, 0, sizeof (vr_key));

  /* defaults */
  addrs = 0;
  n_addrs4 = n_addrs6 = 0;
  sw_if_index = ~0;
  vr_id = 0;
  is_ipv6 = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
		    &sw_if_index))
	;
      else if (unformat (input, "sw_if_index %u", &sw_if_index))
	;
      else if (unformat (input, "vr_id %u", &vr_id))
	;
      else if (unformat (input, "ipv6"))
	is_ipv6 = 1;
      else if (unformat (input, "%U", unformat_ip4_address, &addr.ip4))
	{
	  n_addrs4++;
	  vec_add1 (addrs, addr);
	}
      else if (unformat (input, "%U", unformat_ip6_address, &addr.ip6))
	{
	  n_addrs6++;
	  vec_add1 (addrs, addr);
	}
      else
	{
	  ret = clib_error_return (0, "unknown input `%U'",
				   format_unformat_error, input);
	  goto done;
	}
    }

  if (sw_if_index == ~0)
    ret = clib_error_return (0, "Please specify an interface...");
  else if (!vr_id)
    ret = clib_error_return (0, "Invalid VR ID...");
  else if (n_addrs4 && (n_addrs6 || is_ipv6))
    ret = clib_error_return (0, "Mismatched address families");

  if (ret)			/* data validation failed */
    goto done;

  vr_key.sw_if_index = sw_if_index;
  vr_key.vr_id = vr_id;
  vr_key.is_ipv6 = (is_ipv6 != 0);

  rv = vrrp_vr_set_peers (&vr_key, addrs);

  switch (rv)
    {
    case 0:
      break;
    case VNET_API_ERROR_INVALID_ARGUMENT:
      ret = clib_error_return (0, "Peers can only be set on a unicast VR");
      break;
    case VNET_API_ERROR_RSRC_IN_USE:
      ret = clib_error_return (0, "Cannot set peers on a running VR");
      break;
    case VNET_API_ERROR_INVALID_DST_ADDRESS:
      ret = clib_error_return (0, "No peer addresses provided");
      break;
    default:
      ret = clib_error_return (0, "vrrp_vr_set_peers returned %d", rv);
      break;
    }

done:
  vec_free (addrs);

  return ret;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_proto_start_stop_command, static) =
{
  .path = "vrrp proto",
  .short_help =
  "vrrp proto (start|stop) (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6]",
  .function = vrrp_proto_start_stop_command_fn,
};
/* *INDENT-ON* */

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_peers_command, static) =
{
  .path = "vrrp peers",
  .short_help =
  "vrrp peers (<intf_name>|sw_if_index <n>) vr_id <n> [ipv6] <peer1_addr> [<peer2_addr> ...]",
  .function = vrrp_peers_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
vrrp_vr_track_if_command_fn (vlib_main_t * vm,
			     unformat_input_t * input,
			     vlib_cli_command_t * cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  vrrp_main_t *vmp = &vrrp_main;
  u32 sw_if_index, track_if_index, vr_id, priority;
  u8 is_ipv6 = 0;
  clib_error_t *ret = 0;
  vrrp_vr_tracking_if_t *track_intfs = 0, *track_intf;
  vrrp_vr_t *vr;
  u8 is_add, is_del;
  int rv;

  /* defaults */
  sw_if_index = ~0;
  vr_id = 0;
  is_add = is_del = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vmp->vnet_main,
		    &sw_if_index))
	;
      else if (unformat (input, "sw_if_index %u", &sw_if_index))
	;
      else if (unformat (input, "add"))
	is_add = 1;
      else if (unformat (input, "del"))
	is_del = 1;
      else if (unformat (input, "vr_id %u", &vr_id))
	;
      else if (unformat (input, "ipv6"))
	is_ipv6 = 1;
      else if (unformat (input, "track-index %u priority %u", &track_if_index,
			 &priority))
	{
	  vec_add2 (track_intfs, track_intf, 1);;
	  track_intf->sw_if_index = track_if_index;
	  track_intf->priority = priority;
	}
      else
	break;
    }

  if (sw_if_index == ~0)
    ret = clib_error_return (0, "Please specify an interface");
  else if (!vr_id || vr_id > 0xff)
    ret = clib_error_return (0, "VR ID must be between 1 and 255");
  else if (is_add == is_del)
    ret = clib_error_return (0, "One of add,delete must be specified");

  if (ret)
    goto done;

  vr = vrrp_vr_lookup (sw_if_index, vr_id, is_ipv6);
  if (!vr)
    {
      ret = clib_error_return (0, "VR not found");
      goto done;
    }

  vec_foreach (track_intf, track_intfs)
  {
    if (!vnet_sw_interface_is_valid (vnm, track_intf->sw_if_index))
      {
	ret = clib_error_return (0, "tracked intf sw_if_index %u invalid",
				 track_intf->sw_if_index);
	goto done;
      }
    if (!track_intf->priority)
      {
	ret = clib_error_return (0, "tracked intf priority must be > 0");
	goto done;
      }
    if (track_intf->priority >= vr->config.priority)
      {
	ret = clib_error_return (0, "tracked intf priority must be less "
				 "than VR priority (%u)",
				 vr->config.priority);
	goto done;
      }
  }

  rv = vrrp_vr_tracking_ifs_add_del (vr, track_intfs, is_add);
  if (rv)
    ret = clib_error_return (0, "vrrp_vr_tracking_ifs_add_del returned %d",
			     rv);

done:
  vec_free (track_intfs);

  return ret;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (vrrp_vr_track_if_command, static) =
{
  .path = "vrrp vr track-if",
  .short_help =
  "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> ...]",
  .function = vrrp_vr_track_if_command_fn,
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */