3 * Copyright (C) 2018 Cumulus Networks, Inc.
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
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.
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
27 #include "nexthop_group.h"
28 #include "nexthop_group_private.h"
33 #include "pbrd/pbr_nht.h"
34 #include "pbrd/pbr_map.h"
35 #include "pbrd/pbr_zebra.h"
36 #include "pbrd/pbr_vty.h"
37 #include "pbrd/pbr_debug.h"
38 #ifndef VTYSH_EXTRACT_PL
39 #include "pbrd/pbr_vty_clippy.c"
42 DEFUN_NOSH(pbr_map
, pbr_map_cmd
, "pbr-map PBRMAP seq (1-700)",
43 "Create pbr-map or enter pbr-map command mode\n"
44 "The name of the PBR MAP\n"
45 "Sequence to insert in existing pbr-map entry\n"
48 const char *pbrm_name
= argv
[1]->arg
;
49 uint32_t seqno
= atoi(argv
[3]->arg
);
50 struct pbr_map_sequence
*pbrms
;
52 pbrms
= pbrms_get(pbrm_name
, seqno
);
53 VTY_PUSH_CONTEXT(PBRMAP_NODE
, pbrms
);
58 DEFUN_NOSH(no_pbr_map
, no_pbr_map_cmd
, "no pbr-map PBRMAP [seq (1-700)]",
61 "The name of the PBR MAP\n"
62 "Sequence to delete from existing pbr-map entry\n"
65 const char *pbrm_name
= argv
[2]->arg
;
67 struct pbr_map
*pbrm
= pbrm_find(pbrm_name
);
68 struct pbr_map_sequence
*pbrms
;
69 struct listnode
*node
, *next_node
;
72 seqno
= atoi(argv
[4]->arg
);
75 vty_out(vty
, "pbr-map %s not found\n", pbrm_name
);
79 for (ALL_LIST_ELEMENTS(pbrm
->seqnumbers
, node
, next_node
, pbrms
)) {
80 if (seqno
&& pbrms
->seqno
!= seqno
)
83 pbr_map_delete(pbrms
);
89 DEFPY(pbr_set_table_range
,
90 pbr_set_table_range_cmd
,
91 "[no] pbr table range (10000-4294966272)$lb (10000-4294966272)$ub",
94 "Set table ID range\n"
95 "Set table ID range\n"
96 "Lower bound for table ID range\n"
97 "Upper bound for table ID range\n")
99 /* upper bound is 2^32 - 2^10 */
100 int ret
= CMD_WARNING
;
101 const int minrange
= 1000;
103 /* validate given bounds */
105 vty_out(vty
, "%% Lower bound must be less than upper bound\n");
106 else if (ub
- lb
< minrange
)
107 vty_out(vty
, "%% Range breadth must be at least %d\n", minrange
);
110 pbr_nht_set_tableid_range((uint32_t) lb
, (uint32_t) ub
);
117 DEFPY(pbr_map_match_src
, pbr_map_match_src_cmd
,
118 "[no] match src-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
120 "Match the rest of the command\n"
121 "Choose the src ip or ipv6 prefix to use\n"
125 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
127 pbrms
->family
= prefix
->family
;
130 if (prefix_same(pbrms
->src
, prefix
))
134 pbrms
->src
= prefix_new();
135 prefix_copy(pbrms
->src
, prefix
);
137 prefix_free(pbrms
->src
);
141 pbr_map_check(pbrms
);
146 DEFPY(pbr_map_match_dst
, pbr_map_match_dst_cmd
,
147 "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
149 "Match the rest of the command\n"
150 "Choose the src ip or ipv6 prefix to use\n"
154 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
156 pbrms
->family
= prefix
->family
;
159 if (prefix_same(pbrms
->dst
, prefix
))
163 pbrms
->dst
= prefix_new();
164 prefix_copy(pbrms
->dst
, prefix
);
166 prefix_free(pbrms
->dst
);
170 pbr_map_check(pbrms
);
175 DEFPY(pbr_map_match_mark
, pbr_map_match_mark_cmd
,
176 "[no] match mark (1-4294967295)$mark",
178 "Match the rest of the command\n"
179 "Choose the mark value to use\n"
182 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
185 vty_out(vty
, "pbr marks are not supported on this platform");
186 return CMD_WARNING_CONFIG_FAILED
;
190 if (pbrms
->mark
== (uint32_t) mark
)
192 pbrms
->mark
= (uint32_t) mark
;
197 pbr_map_check(pbrms
);
202 DEFPY(pbr_map_nexthop_group
, pbr_map_nexthop_group_cmd
,
203 "[no] set nexthop-group NHGNAME$name",
205 "Set for the PBR-MAP\n"
206 "nexthop-group to use\n"
207 "The name of the nexthop-group\n")
209 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
210 struct nexthop_group_cmd
*nhgc
;
214 "A `set nexthop XX` command already exists, please remove that first\n");
215 return CMD_WARNING_CONFIG_FAILED
;
218 nhgc
= nhgc_find(name
);
220 vty_out(vty
, "Specified nexthop-group %s does not exist\n",
222 vty_out(vty
, "PBR-MAP will not be applied until it is created\n");
226 if (pbrms
->nhgrp_name
&& strcmp(name
, pbrms
->nhgrp_name
) == 0)
227 pbr_map_delete_nexthop_group(pbrms
);
230 "Nexthop Group specified: %s does not exist to remove",
232 return CMD_WARNING_CONFIG_FAILED
;
235 if (pbrms
->nhgrp_name
) {
236 if (strcmp(name
, pbrms
->nhgrp_name
) != 0) {
238 "Please delete current nexthop group before modifying current one");
239 return CMD_WARNING_CONFIG_FAILED
;
244 pbrms
->nhgrp_name
= XSTRDUP(MTYPE_TMP
, name
);
245 pbr_map_check(pbrms
);
251 DEFPY(pbr_map_nexthop
, pbr_map_nexthop_cmd
,
254 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
257 [nexthop-vrf NAME$vrf_name]",
259 "Set for the PBR-MAP\n"
260 "Specify one of the nexthops in this map\n"
265 "If the nexthop is in a different vrf tell us\n"
266 "The nexthop-vrf Name\n")
268 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
273 if (pbrms
->nhgrp_name
) {
275 "Please unconfigure the nexthop group before adding an individual nexthop");
276 return CMD_WARNING_CONFIG_FAILED
;
280 vrf
= vrf_lookup_by_name(vrf_name
);
282 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
285 vty_out(vty
, "Specified: %s is non-existent\n", vrf_name
);
286 return CMD_WARNING_CONFIG_FAILED
;
289 memset(&nhop
, 0, sizeof(nhop
));
290 nhop
.vrf_id
= vrf
->vrf_id
;
293 nhop
.ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
294 if (nhop
.ifindex
== IFINDEX_INTERNAL
) {
296 "Specified Intf %s does not exist in vrf: %s\n",
298 return CMD_WARNING_CONFIG_FAILED
;
303 if (addr
->sa
.sa_family
== AF_INET
) {
304 nhop
.gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
306 nhop
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
308 nhop
.type
= NEXTHOP_TYPE_IPV4
;
310 nhop
.gate
.ipv6
= addr
->sin6
.sin6_addr
;
312 nhop
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
314 if (IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
316 "Specified a v6 LL with no interface, rejecting\n");
317 return CMD_WARNING_CONFIG_FAILED
;
319 nhop
.type
= NEXTHOP_TYPE_IPV6
;
323 nhop
.type
= NEXTHOP_TYPE_IFINDEX
;
326 nh
= nexthop_exists(pbrms
->nhg
, &nhop
);
328 char buf
[PBR_NHC_NAMELEN
];
331 vty_out(vty
, "No nexthops to delete\n");
332 return CMD_WARNING_CONFIG_FAILED
;
335 pbrms
->nhg
= nexthop_group_new();
336 pbrms
->internal_nhg_name
=
338 pbr_nht_nexthop_make_name(pbrms
->parent
->name
,
347 pbr_nht_delete_individual_nexthop(pbrms
);
350 if (pbrms
->nhg
->nexthop
) {
352 "If you would like more than one nexthop please use nexthop-groups");
353 return CMD_WARNING_CONFIG_FAILED
;
356 /* must be adding new nexthop since !no and !nexthop_exists */
359 memcpy(nh
, &nhop
, sizeof(nhop
));
360 _nexthop_add(&pbrms
->nhg
->nexthop
, nh
);
362 pbr_nht_add_individual_nexthop(pbrms
);
363 pbr_map_check(pbrms
);
366 if (nhop
.type
== NEXTHOP_TYPE_IFINDEX
) {
367 struct interface
*ifp
;
369 ifp
= if_lookup_by_index(nhop
.ifindex
, nhop
.vrf_id
);
371 pbr_nht_nexthop_interface_update(ifp
);
379 "[no] pbr-policy PBRMAP$mapname",
382 "Name of the pbr-map to apply\n")
384 VTY_DECLVAR_CONTEXT(interface
, ifp
);
385 struct pbr_map
*pbrm
, *old_pbrm
;
386 struct pbr_interface
*pbr_ifp
= ifp
->info
;
389 pbrm
= pbrm_find(mapname
);
392 /* we don't want one and we don't have one, so... */
396 /* Some one could have fat fingered the interface name */
397 pbr_ifp
= pbr_if_new(ifp
);
401 if (strcmp(pbr_ifp
->mapname
, mapname
) == 0) {
402 pbr_ifp
->mapname
[0] = '\0';
404 pbr_map_interface_delete(pbrm
, ifp
);
407 if (strcmp(pbr_ifp
->mapname
, "") != 0) {
408 old_pbrm
= pbrm_find(pbr_ifp
->mapname
);
411 * So if we have an old pbrm we should only
412 * delete it if we are actually deleting and
413 * moving to a new pbrm
415 if (old_pbrm
&& old_pbrm
!= pbrm
)
416 pbr_map_interface_delete(old_pbrm
, ifp
);
418 snprintf(pbr_ifp
->mapname
, sizeof(pbr_ifp
->mapname
),
422 * So only reinstall if the old_pbrm and this pbrm are
425 if (pbrm
&& pbrm
!= old_pbrm
)
426 pbr_map_add_interface(pbrm
, ifp
);
438 pbr_nht_write_table_range(vty
);
439 pbr_nht_write_rule_range(vty
);
446 "show pbr map [NAME$name] [detail$detail]",
451 "Detailed information\n")
453 struct pbr_map_sequence
*pbrms
;
454 struct pbr_map
*pbrm
;
455 struct listnode
*node
;
456 char buf
[PREFIX_STRLEN
];
459 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
460 if (name
&& strcmp(name
, pbrm
->name
) != 0)
463 vty_out(vty
, " pbr-map %s valid: %d\n", pbrm
->name
,
466 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
468 pbr_map_reason_string(pbrms
->reason
, rbuf
,
471 " Seq: %u rule: %u Installed: %" PRIu64
"(%u) Reason: %s\n",
472 pbrms
->seqno
, pbrms
->ruleno
, pbrms
->installed
,
473 pbrms
->unique
, pbrms
->reason
? rbuf
: "Valid");
476 vty_out(vty
, "\tSRC Match: %s\n",
477 prefix2str(pbrms
->src
, buf
,
480 vty_out(vty
, "\tDST Match: %s\n",
481 prefix2str(pbrms
->dst
, buf
,
484 vty_out(vty
, "\tMARK Match: %u\n", pbrms
->mark
);
486 if (pbrms
->nhgrp_name
) {
488 "\tNexthop-Group: %s(%u) Installed: %u(%d)\n",
490 pbr_nht_get_table(pbrms
->nhgrp_name
),
491 pbrms
->nhs_installed
,
492 pbr_nht_get_installed(
494 } else if (pbrms
->nhg
) {
496 nexthop_group_write_nexthop(
497 vty
, pbrms
->nhg
->nexthop
);
499 "\tInstalled: %u(%d) Tableid: %d\n",
500 pbrms
->nhs_installed
,
501 pbr_nht_get_installed(
502 pbrms
->internal_nhg_name
),
504 pbrms
->internal_nhg_name
));
507 "\tNexthop-Group: Unknown Installed: 0(0)\n");
514 DEFPY(show_pbr_nexthop_group
,
515 show_pbr_nexthop_group_cmd
,
516 "show pbr nexthop-groups [WORD$word]",
520 "Optional Name of the nexthop group\n")
522 pbr_nht_show_nexthop_group(vty
, word
);
527 DEFPY (show_pbr_interface
,
528 show_pbr_interface_cmd
,
529 "show pbr interface [NAME$name]",
533 "PBR Interface Name\n")
535 struct interface
*ifp
;
537 struct pbr_interface
*pbr_ifp
;
539 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
) {
540 FOR_ALL_INTERFACES(vrf
, ifp
) {
541 struct pbr_map
*pbrm
;
546 if (name
&& strcmp(ifp
->name
, name
) != 0)
551 if (strcmp(pbr_ifp
->mapname
, "") == 0)
554 pbrm
= pbrm_find(pbr_ifp
->mapname
);
555 vty_out(vty
, " %s(%d) with pbr-policy %s", ifp
->name
,
556 ifp
->ifindex
, pbr_ifp
->mapname
);
558 vty_out(vty
, " (map doesn't exist)");
566 /* PBR debugging CLI ------------------------------------------------------- */
568 static struct cmd_node debug_node
= {DEBUG_NODE
, "", 1};
572 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
577 "PBRD <-> Zebra communications\n"
581 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
584 DEBUG_MODE_SET(&pbr_dbg_map
, mode
, !no
);
586 DEBUG_MODE_SET(&pbr_dbg_zebra
, mode
, !no
);
588 DEBUG_MODE_SET(&pbr_dbg_nht
, mode
, !no
);
590 DEBUG_MODE_SET(&pbr_dbg_event
, mode
, !no
);
592 /* no specific debug --> act on all of them */
593 if (strmatch(argv
[argc
- 1]->text
, "pbr"))
594 pbr_debug_set_all(mode
, !no
);
599 DEFUN_NOSH(show_debugging_pbr
,
600 show_debugging_pbr_cmd
,
601 "show debugging [pbr]",
606 vty_out(vty
, "PBR debugging status:\n");
608 pbr_debug_config_write_helper(vty
, false);
613 /* ------------------------------------------------------------------------- */
616 static struct cmd_node interface_node
= {
617 INTERFACE_NODE
, "%s(config-if)# ", 1 /* vtysh ? yes */
620 static int pbr_interface_config_write(struct vty
*vty
)
622 struct interface
*ifp
;
625 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
626 FOR_ALL_INTERFACES (vrf
, ifp
) {
627 if (vrf
->vrf_id
== VRF_DEFAULT
)
628 vty_frame(vty
, "interface %s\n", ifp
->name
);
630 vty_frame(vty
, "interface %s vrf %s\n",
631 ifp
->name
, vrf
->name
);
634 vty_out(vty
, " description %s\n", ifp
->desc
);
636 pbr_map_write_interfaces(vty
, ifp
);
638 vty_endframe(vty
, "!\n");
645 /* PBR map node structure. */
646 static struct cmd_node pbr_map_node
= {PBRMAP_NODE
, "%s(config-pbr-map)# ", 1};
648 static int pbr_vty_map_config_write_sequence(struct vty
*vty
,
649 struct pbr_map
*pbrm
,
650 struct pbr_map_sequence
*pbrms
)
652 char buff
[PREFIX_STRLEN
];
654 vty_out(vty
, "pbr-map %s seq %u\n", pbrm
->name
, pbrms
->seqno
);
657 vty_out(vty
, " match src-ip %s\n",
658 prefix2str(pbrms
->src
, buff
, sizeof(buff
)));
661 vty_out(vty
, " match dst-ip %s\n",
662 prefix2str(pbrms
->dst
, buff
, sizeof(buff
)));
665 vty_out(vty
, " match mark %u\n", pbrms
->mark
);
667 if (pbrms
->nhgrp_name
)
668 vty_out(vty
, " set nexthop-group %s\n", pbrms
->nhgrp_name
);
671 vty_out(vty
, " set ");
672 nexthop_group_write_nexthop(vty
, pbrms
->nhg
->nexthop
);
679 static int pbr_vty_map_config_write(struct vty
*vty
)
681 struct pbr_map
*pbrm
;
683 pbr_nht_write_table_range(vty
);
684 pbr_nht_write_rule_range(vty
);
686 RB_FOREACH(pbrm
, pbr_map_entry_head
, &pbr_maps
) {
687 struct pbr_map_sequence
*pbrms
;
688 struct listnode
*node
;
690 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
691 pbr_vty_map_config_write_sequence(vty
, pbrm
, pbrms
);
697 static void pbr_map_completer(vector comps
, struct cmd_token
*token
)
699 struct pbr_map
*pbrm
;
701 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
)
702 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, pbrm
->name
));
705 static const struct cmd_variable_handler pbr_map_name
[] = {
707 .tokenname
= "PBRMAP", .completions
= pbr_map_completer
,
714 void pbr_vty_init(void)
716 cmd_variable_handler_register(pbr_map_name
);
718 install_node(&interface_node
,
719 pbr_interface_config_write
);
722 install_node(&pbr_map_node
,
723 pbr_vty_map_config_write
);
726 install_node(&debug_node
, pbr_debug_config_write
);
727 install_element(VIEW_NODE
, &debug_pbr_cmd
);
728 install_element(CONFIG_NODE
, &debug_pbr_cmd
);
729 install_element(VIEW_NODE
, &show_debugging_pbr_cmd
);
731 install_default(PBRMAP_NODE
);
733 install_element(CONFIG_NODE
, &pbr_map_cmd
);
734 install_element(CONFIG_NODE
, &no_pbr_map_cmd
);
735 install_element(CONFIG_NODE
, &pbr_set_table_range_cmd
);
736 install_element(INTERFACE_NODE
, &pbr_policy_cmd
);
737 install_element(PBRMAP_NODE
, &pbr_map_match_src_cmd
);
738 install_element(PBRMAP_NODE
, &pbr_map_match_dst_cmd
);
739 install_element(PBRMAP_NODE
, &pbr_map_match_mark_cmd
);
740 install_element(PBRMAP_NODE
, &pbr_map_nexthop_group_cmd
);
741 install_element(PBRMAP_NODE
, &pbr_map_nexthop_cmd
);
742 install_element(VIEW_NODE
, &show_pbr_cmd
);
743 install_element(VIEW_NODE
, &show_pbr_map_cmd
);
744 install_element(VIEW_NODE
, &show_pbr_interface_cmd
);
745 install_element(VIEW_NODE
, &show_pbr_nexthop_group_cmd
);