]> git.proxmox.com Git - mirror_frr.git/blob - pbrd/pbr_vty.c
Merge pull request #3202 from donaldsharp/evpn_dump
[mirror_frr.git] / pbrd / pbr_vty.c
1 /*
2 * PBR - vty code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * FRR is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * FRR is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <zebra.h>
21
22 #include "vty.h"
23 #include "command.h"
24 #include "prefix.h"
25 #include "vrf.h"
26 #include "nexthop.h"
27 #include "nexthop_group.h"
28 #include "log.h"
29 #include "debug.h"
30 #include "pbr.h"
31
32 #include "pbrd/pbr_nht.h"
33 #include "pbrd/pbr_map.h"
34 #include "pbrd/pbr_zebra.h"
35 #include "pbrd/pbr_vty.h"
36 #include "pbrd/pbr_debug.h"
37 #ifndef VTYSH_EXTRACT_PL
38 #include "pbrd/pbr_vty_clippy.c"
39 #endif
40
41 DEFUN_NOSH(pbr_map, pbr_map_cmd, "pbr-map WORD seq (1-700)",
42 "Create pbr-map or enter pbr-map command mode\n"
43 "The name of the PBR MAP\n"
44 "Sequence to insert in existing pbr-map entry\n"
45 "Sequence number\n")
46 {
47 const char *pbrm_name = argv[1]->arg;
48 uint32_t seqno = atoi(argv[3]->arg);
49 struct pbr_map_sequence *pbrms;
50
51 pbrms = pbrms_get(pbrm_name, seqno);
52 VTY_PUSH_CONTEXT(PBRMAP_NODE, pbrms);
53
54 return CMD_SUCCESS;
55 }
56
57 DEFUN_NOSH(no_pbr_map, no_pbr_map_cmd, "no pbr-map WORD [seq (1-700)]",
58 NO_STR
59 "Delete pbr-map\n"
60 "The name of the PBR MAP\n"
61 "Sequence to delete from existing pbr-map entry\n"
62 "Sequence number\n")
63 {
64 const char *pbrm_name = argv[2]->arg;
65 uint32_t seqno = 0;
66 struct pbr_map *pbrm = pbrm_find(pbrm_name);
67 struct pbr_map_sequence *pbrms;
68 struct listnode *node, *next_node;
69
70 if (argc > 3)
71 seqno = atoi(argv[4]->arg);
72
73 if (!pbrm) {
74 vty_out(vty, "pbr-map %s not found\n", pbrm_name);
75 return CMD_SUCCESS;
76 }
77
78 for (ALL_LIST_ELEMENTS(pbrm->seqnumbers, node, next_node, pbrms)) {
79 if (seqno && pbrms->seqno != seqno)
80 continue;
81
82 pbr_map_delete(pbrms);
83 }
84
85 return CMD_SUCCESS;
86 }
87
88 DEFPY(pbr_set_table_range,
89 pbr_set_table_range_cmd,
90 "[no] pbr table range (10000-4294966272)$lb (10000-4294966272)$ub",
91 NO_STR
92 PBR_STR
93 "Set table ID range\n"
94 "Set table ID range\n"
95 "Lower bound for table ID range\n"
96 "Upper bound for table ID range\n")
97 {
98 /* upper bound is 2^32 - 2^10 */
99 int ret = CMD_WARNING;
100 const int minrange = 1000;
101
102 /* validate given bounds */
103 if (lb > ub)
104 vty_out(vty, "%% Lower bound must be less than upper bound\n");
105 else if (ub - lb < minrange)
106 vty_out(vty, "%% Range breadth must be at least %d\n", minrange);
107 else {
108 ret = CMD_SUCCESS;
109 pbr_nht_set_tableid_range((uint32_t) lb, (uint32_t) ub);
110 }
111
112 return ret;
113 }
114
115
116 DEFPY(pbr_map_match_src, pbr_map_match_src_cmd,
117 "[no] match src-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
118 NO_STR
119 "Match the rest of the command\n"
120 "Choose the src ip or ipv6 prefix to use\n"
121 "v4 Prefix\n"
122 "v6 Prefix\n")
123 {
124 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
125
126 pbrms->family = prefix->family;
127
128 if (!no) {
129 if (prefix_same(pbrms->src, prefix))
130 return CMD_SUCCESS;
131
132 if (!pbrms->src)
133 pbrms->src = prefix_new();
134 prefix_copy(pbrms->src, prefix);
135 } else {
136 prefix_free(pbrms->src);
137 pbrms->src = 0;
138 }
139
140 pbr_map_check(pbrms);
141
142 return CMD_SUCCESS;
143 }
144
145 DEFPY(pbr_map_match_dst, pbr_map_match_dst_cmd,
146 "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
147 NO_STR
148 "Match the rest of the command\n"
149 "Choose the src ip or ipv6 prefix to use\n"
150 "v4 Prefix\n"
151 "v6 Prefix\n")
152 {
153 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
154
155 pbrms->family = prefix->family;
156
157 if (!no) {
158 if (prefix_same(pbrms->dst, prefix))
159 return CMD_SUCCESS;
160
161 if (!pbrms->dst)
162 pbrms->dst = prefix_new();
163 prefix_copy(pbrms->dst, prefix);
164 } else {
165 prefix_free(pbrms->dst);
166 pbrms->dst = NULL;
167 }
168
169 pbr_map_check(pbrms);
170
171 return CMD_SUCCESS;
172 }
173
174 DEFPY(pbr_map_nexthop_group, pbr_map_nexthop_group_cmd,
175 "[no] set nexthop-group NAME$name",
176 NO_STR
177 "Set for the PBR-MAP\n"
178 "nexthop-group to use\n"
179 "The name of the nexthop-group\n")
180 {
181 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
182 struct nexthop_group_cmd *nhgc;
183
184 if (pbrms->nhg) {
185 vty_out(vty,
186 "A `set nexthop XX` command already exists, please remove that first\n");
187 return CMD_WARNING_CONFIG_FAILED;
188 }
189
190 nhgc = nhgc_find(name);
191 if (!nhgc) {
192 vty_out(vty, "Specified nexthop-group %s does not exist\n",
193 name);
194 vty_out(vty, "PBR-MAP will not be applied until it is created\n");
195 }
196
197 if (no) {
198 if (pbrms->nhgrp_name && strcmp(name, pbrms->nhgrp_name) == 0)
199 pbr_map_delete_nexthop_group(pbrms);
200 else {
201 vty_out(vty,
202 "Nexthop Group specified: %s does not exist to remove",
203 name);
204 return CMD_WARNING_CONFIG_FAILED;
205 }
206 } else {
207 if (pbrms->nhgrp_name) {
208 if (strcmp(name, pbrms->nhgrp_name) != 0) {
209 vty_out(vty,
210 "Please delete current nexthop group before modifying current one");
211 return CMD_WARNING_CONFIG_FAILED;
212 }
213
214 return CMD_SUCCESS;
215 }
216 pbrms->nhgrp_name = XSTRDUP(MTYPE_TMP, name);
217 pbr_map_check(pbrms);
218 }
219
220 return CMD_SUCCESS;
221 }
222
223 DEFPY(pbr_map_nexthop, pbr_map_nexthop_cmd,
224 "[no] set nexthop <A.B.C.D|X:X::X:X>$addr [INTERFACE]$intf [nexthop-vrf NAME$name]",
225 NO_STR
226 "Set for the PBR-MAP\n"
227 "Specify one of the nexthops in this map\n"
228 "v4 Address\n"
229 "v6 Address\n"
230 "Interface to use\n"
231 "If the nexthop is in a different vrf tell us\n"
232 "The nexthop-vrf Name\n")
233 {
234 struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
235 struct vrf *vrf;
236 struct nexthop nhop;
237 struct nexthop *nh;
238
239 if (pbrms->nhgrp_name) {
240 vty_out(vty,
241 "Please unconfigure the nexthop group before adding an individual nexthop");
242 return CMD_WARNING_CONFIG_FAILED;
243 }
244
245 if (name)
246 vrf = vrf_lookup_by_name(name);
247 else
248 vrf = vrf_lookup_by_id(VRF_DEFAULT);
249
250 if (!vrf) {
251 vty_out(vty, "Specified: %s is non-existent\n", name);
252 return CMD_WARNING_CONFIG_FAILED;
253 }
254
255 memset(&nhop, 0, sizeof(nhop));
256 nhop.vrf_id = vrf->vrf_id;
257
258 /*
259 * Make SA happy. CLIPPY is not going to give us a NULL
260 * addr.
261 */
262 assert(addr);
263 if (addr->sa.sa_family == AF_INET) {
264 nhop.gate.ipv4.s_addr = addr->sin.sin_addr.s_addr;
265 if (intf) {
266 nhop.type = NEXTHOP_TYPE_IPV4_IFINDEX;
267 nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
268 if (nhop.ifindex == IFINDEX_INTERNAL) {
269 vty_out(vty,
270 "Specified Intf %s does not exist in vrf: %s\n",
271 intf, vrf->name);
272 return CMD_WARNING_CONFIG_FAILED;
273 }
274 } else
275 nhop.type = NEXTHOP_TYPE_IPV4;
276 } else {
277 memcpy(&nhop.gate.ipv6, &addr->sin6.sin6_addr, 16);
278 if (intf) {
279 nhop.type = NEXTHOP_TYPE_IPV6_IFINDEX;
280 nhop.ifindex = ifname2ifindex(intf, vrf->vrf_id);
281 if (nhop.ifindex == IFINDEX_INTERNAL) {
282 vty_out(vty,
283 "Specified Intf %s does not exist in vrf: %s\n",
284 intf, vrf->name);
285 return CMD_WARNING_CONFIG_FAILED;
286 }
287 } else {
288 if (IN6_IS_ADDR_LINKLOCAL(&nhop.gate.ipv6)) {
289 vty_out(vty,
290 "Specified a v6 LL with no interface, rejecting\n");
291 return CMD_WARNING_CONFIG_FAILED;
292 }
293 nhop.type = NEXTHOP_TYPE_IPV6;
294 }
295 }
296
297 if (pbrms->nhg)
298 nh = nexthop_exists(pbrms->nhg, &nhop);
299 else {
300 char buf[PBR_NHC_NAMELEN];
301
302 if (no) {
303 vty_out(vty, "No nexthops to delete");
304 return CMD_WARNING_CONFIG_FAILED;
305 }
306
307 pbrms->nhg = nexthop_group_new();
308 pbrms->internal_nhg_name =
309 XSTRDUP(MTYPE_TMP,
310 pbr_nht_nexthop_make_name(pbrms->parent->name,
311 PBR_NHC_NAMELEN,
312 pbrms->seqno,
313 buf));
314 nh = NULL;
315 }
316
317 if (no) {
318 if (nh)
319 pbr_nht_delete_individual_nexthop(pbrms);
320 } else if (!nh) {
321
322 if (pbrms->nhg->nexthop) {
323 vty_out(vty,
324 "If you would like more than one nexthop please use nexthop-groups");
325 return CMD_WARNING_CONFIG_FAILED;
326 }
327
328 /* must be adding new nexthop since !no and !nexthop_exists */
329 nh = nexthop_new();
330
331 memcpy(nh, &nhop, sizeof(nhop));
332 nexthop_add(&pbrms->nhg->nexthop, nh);
333
334 pbr_nht_add_individual_nexthop(pbrms);
335 pbr_map_check(pbrms);
336 }
337
338 return CMD_SUCCESS;
339 }
340
341 DEFPY (pbr_policy,
342 pbr_policy_cmd,
343 "[no] pbr-policy NAME$mapname",
344 NO_STR
345 "Policy to use\n"
346 "Name of the pbr-map to apply\n")
347 {
348 VTY_DECLVAR_CONTEXT(interface, ifp);
349 struct pbr_map *pbrm, *old_pbrm;
350 struct pbr_interface *pbr_ifp = ifp->info;
351
352 pbrm = pbrm_find(mapname);
353
354 if (!pbr_ifp) {
355 /* we don't want one and we don't have one, so... */
356 if (no)
357 return CMD_SUCCESS;
358
359 /* Some one could have fat fingered the interface name */
360 pbr_ifp = pbr_if_new(ifp);
361 }
362
363 if (no) {
364 if (strcmp(pbr_ifp->mapname, mapname) == 0) {
365 pbr_ifp->mapname[0] = '\0';
366 if (pbrm)
367 pbr_map_interface_delete(pbrm, ifp);
368 }
369 } else {
370 if (strcmp(pbr_ifp->mapname, "") != 0) {
371 old_pbrm = pbrm_find(pbr_ifp->mapname);
372 if (old_pbrm)
373 pbr_map_interface_delete(old_pbrm, ifp);
374 }
375 snprintf(pbr_ifp->mapname, sizeof(pbr_ifp->mapname),
376 "%s", mapname);
377 if (pbrm)
378 pbr_map_add_interface(pbrm, ifp);
379 }
380
381 return CMD_SUCCESS;
382 }
383
384 DEFPY (show_pbr,
385 show_pbr_cmd,
386 "show pbr",
387 SHOW_STR
388 PBR_STR)
389 {
390 pbr_nht_write_table_range(vty);
391 pbr_nht_write_rule_range(vty);
392
393 return CMD_SUCCESS;
394 }
395
396 DEFPY (show_pbr_map,
397 show_pbr_map_cmd,
398 "show pbr map [NAME$name] [detail$detail]",
399 SHOW_STR
400 PBR_STR
401 "PBR Map\n"
402 "PBR Map Name\n"
403 "Detailed information\n")
404 {
405 struct pbr_map_sequence *pbrms;
406 struct pbr_map *pbrm;
407 struct listnode *node;
408 char buf[PREFIX_STRLEN];
409 char rbuf[64];
410
411 RB_FOREACH (pbrm, pbr_map_entry_head, &pbr_maps) {
412 if (name && strcmp(name, pbrm->name) != 0)
413 continue;
414
415 vty_out(vty, " pbr-map %s valid: %d\n", pbrm->name,
416 pbrm->valid);
417
418 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms)) {
419 if (pbrms->reason)
420 pbr_map_reason_string(pbrms->reason, rbuf,
421 sizeof(rbuf));
422 vty_out(vty,
423 " Seq: %u rule: %u Installed: %" PRIu64 "(%u) Reason: %s\n",
424 pbrms->seqno, pbrms->ruleno, pbrms->installed,
425 pbrms->unique, pbrms->reason ? rbuf : "Valid");
426
427 if (pbrms->src)
428 vty_out(vty, "\tSRC Match: %s\n",
429 prefix2str(pbrms->src, buf,
430 sizeof(buf)));
431 if (pbrms->dst)
432 vty_out(vty, "\tDST Match: %s\n",
433 prefix2str(pbrms->dst, buf,
434 sizeof(buf)));
435
436 if (pbrms->nhgrp_name) {
437 vty_out(vty,
438 "\tNexthop-Group: %s(%u) Installed: %u(%d)\n",
439 pbrms->nhgrp_name,
440 pbr_nht_get_table(pbrms->nhgrp_name),
441 pbrms->nhs_installed,
442 pbr_nht_get_installed(
443 pbrms->nhgrp_name));
444 } else if (pbrms->nhg) {
445 vty_out(vty, " ");
446 nexthop_group_write_nexthop(
447 vty, pbrms->nhg->nexthop);
448 vty_out(vty,
449 "\tInstalled: %u(%d) Tableid: %d\n",
450 pbrms->nhs_installed,
451 pbr_nht_get_installed(
452 pbrms->internal_nhg_name),
453 pbr_nht_get_table(
454 pbrms->internal_nhg_name));
455 } else {
456 vty_out(vty,
457 "\tNexthop-Group: Unknown Installed: 0(0)\n");
458 }
459 }
460 }
461 return CMD_SUCCESS;
462 }
463
464 DEFPY(show_pbr_nexthop_group,
465 show_pbr_nexthop_group_cmd,
466 "show pbr nexthop-groups [WORD$word]",
467 SHOW_STR
468 PBR_STR
469 "Nexthop Groups\n"
470 "Optional Name of the nexthop group\n")
471 {
472 pbr_nht_show_nexthop_group(vty, word);
473
474 return CMD_SUCCESS;
475 }
476
477 DEFPY (show_pbr_interface,
478 show_pbr_interface_cmd,
479 "show pbr interface [NAME$name]",
480 SHOW_STR
481 PBR_STR
482 "PBR Interface\n"
483 "PBR Interface Name\n")
484 {
485 struct interface *ifp;
486 struct vrf *vrf;
487 struct pbr_interface *pbr_ifp;
488
489 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) {
490 FOR_ALL_INTERFACES(vrf, ifp) {
491 struct pbr_map *pbrm;
492
493 if (!ifp->info)
494 continue;
495
496 if (name && strcmp(ifp->name, name) != 0)
497 continue;
498
499 pbr_ifp = ifp->info;
500
501 if (strcmp(pbr_ifp->mapname, "") == 0)
502 continue;
503
504 pbrm = pbrm_find(pbr_ifp->mapname);
505 vty_out(vty, " %s(%d) with pbr-policy %s", ifp->name,
506 ifp->ifindex, pbr_ifp->mapname);
507 if (!pbrm)
508 vty_out(vty, " (map doesn't exist)");
509 vty_out(vty, "\n");
510 }
511 }
512
513 return CMD_SUCCESS;
514 }
515
516 /* PBR debugging CLI ------------------------------------------------------- */
517
518 static struct cmd_node debug_node = {DEBUG_NODE, "", 1};
519
520 DEFPY(debug_pbr,
521 debug_pbr_cmd,
522 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
523 NO_STR
524 DEBUG_STR
525 PBR_STR
526 "Policy maps\n"
527 "PBRD <-> Zebra communications\n"
528 "Nexthop tracking\n"
529 "Events\n")
530 {
531 uint32_t mode = DEBUG_NODE2MODE(vty->node);
532
533 if (map)
534 DEBUG_MODE_SET(&pbr_dbg_map, mode, !no);
535 if (zebra)
536 DEBUG_MODE_SET(&pbr_dbg_zebra, mode, !no);
537 if (nht)
538 DEBUG_MODE_SET(&pbr_dbg_nht, mode, !no);
539 if (events)
540 DEBUG_MODE_SET(&pbr_dbg_event, mode, !no);
541
542 /* no specific debug --> act on all of them */
543 if (strmatch(argv[argc - 1]->text, "pbr"))
544 pbr_debug_set_all(mode, !no);
545
546 return CMD_SUCCESS;
547 }
548
549 DEFUN_NOSH(show_debugging_pbr,
550 show_debugging_pbr_cmd,
551 "show debugging [pbr]",
552 SHOW_STR
553 DEBUG_STR
554 PBR_STR)
555 {
556 vty_out(vty, "PBR debugging status:\n");
557
558 pbr_debug_config_write_helper(vty, false);
559
560 return CMD_SUCCESS;
561 }
562
563 /* ------------------------------------------------------------------------- */
564
565
566 static struct cmd_node interface_node = {
567 INTERFACE_NODE, "%s(config-if)# ", 1 /* vtysh ? yes */
568 };
569
570 static int pbr_interface_config_write(struct vty *vty)
571 {
572 struct interface *ifp;
573 struct vrf *vrf;
574
575 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
576 FOR_ALL_INTERFACES (vrf, ifp) {
577 if (vrf->vrf_id == VRF_DEFAULT)
578 vty_frame(vty, "interface %s\n", ifp->name);
579 else
580 vty_frame(vty, "interface %s vrf %s\n",
581 ifp->name, vrf->name);
582
583 if (ifp->desc)
584 vty_out(vty, " description %s\n", ifp->desc);
585
586 pbr_map_write_interfaces(vty, ifp);
587
588 vty_endframe(vty, "!\n");
589 }
590 }
591
592 return 1;
593 }
594
595 /* PBR map node structure. */
596 static struct cmd_node pbr_map_node = {PBRMAP_NODE, "%s(config-pbr-map)# ", 1};
597
598 static int pbr_vty_map_config_write_sequence(struct vty *vty,
599 struct pbr_map *pbrm,
600 struct pbr_map_sequence *pbrms)
601 {
602 char buff[PREFIX_STRLEN];
603
604 vty_out(vty, "pbr-map %s seq %u\n", pbrm->name, pbrms->seqno);
605
606 if (pbrms->src)
607 vty_out(vty, " match src-ip %s\n",
608 prefix2str(pbrms->src, buff, sizeof(buff)));
609
610 if (pbrms->dst)
611 vty_out(vty, " match dst-ip %s\n",
612 prefix2str(pbrms->dst, buff, sizeof(buff)));
613
614 if (pbrms->nhgrp_name)
615 vty_out(vty, " set nexthop-group %s\n", pbrms->nhgrp_name);
616
617 if (pbrms->nhg) {
618 vty_out(vty, " set ");
619 nexthop_group_write_nexthop(vty, pbrms->nhg->nexthop);
620 }
621
622 vty_out(vty, "!\n");
623 return 1;
624 }
625
626 static int pbr_vty_map_config_write(struct vty *vty)
627 {
628 struct pbr_map *pbrm;
629
630 pbr_nht_write_table_range(vty);
631 pbr_nht_write_rule_range(vty);
632
633 RB_FOREACH(pbrm, pbr_map_entry_head, &pbr_maps) {
634 struct pbr_map_sequence *pbrms;
635 struct listnode *node;
636
637 for (ALL_LIST_ELEMENTS_RO(pbrm->seqnumbers, node, pbrms))
638 pbr_vty_map_config_write_sequence(vty, pbrm, pbrms);
639 }
640
641 return 1;
642 }
643
644 void pbr_vty_init(void)
645 {
646 install_node(&interface_node,
647 pbr_interface_config_write);
648 if_cmd_init();
649
650 install_node(&pbr_map_node,
651 pbr_vty_map_config_write);
652
653 /* debug */
654 install_node(&debug_node, pbr_debug_config_write);
655 install_element(VIEW_NODE, &debug_pbr_cmd);
656 install_element(CONFIG_NODE, &debug_pbr_cmd);
657 install_element(VIEW_NODE, &show_debugging_pbr_cmd);
658
659 install_default(PBRMAP_NODE);
660
661 install_element(CONFIG_NODE, &pbr_map_cmd);
662 install_element(CONFIG_NODE, &no_pbr_map_cmd);
663 install_element(CONFIG_NODE, &pbr_set_table_range_cmd);
664 install_element(INTERFACE_NODE, &pbr_policy_cmd);
665 install_element(PBRMAP_NODE, &pbr_map_match_src_cmd);
666 install_element(PBRMAP_NODE, &pbr_map_match_dst_cmd);
667 install_element(PBRMAP_NODE, &pbr_map_nexthop_group_cmd);
668 install_element(PBRMAP_NODE, &pbr_map_nexthop_cmd);
669 install_element(VIEW_NODE, &show_pbr_cmd);
670 install_element(VIEW_NODE, &show_pbr_map_cmd);
671 install_element(VIEW_NODE, &show_pbr_interface_cmd);
672 install_element(VIEW_NODE, &show_pbr_nexthop_group_cmd);
673 }