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
);
139 pbr_map_check(pbrms
);
144 DEFPY(pbr_map_match_dst
, pbr_map_match_dst_cmd
,
145 "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
147 "Match the rest of the command\n"
148 "Choose the src ip or ipv6 prefix to use\n"
152 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
154 pbrms
->family
= prefix
->family
;
157 if (prefix_same(pbrms
->dst
, prefix
))
161 pbrms
->dst
= prefix_new();
162 prefix_copy(pbrms
->dst
, prefix
);
164 prefix_free(&pbrms
->dst
);
166 pbr_map_check(pbrms
);
171 DEFPY(pbr_map_match_mark
, pbr_map_match_mark_cmd
,
172 "[no] match mark (1-4294967295)$mark",
174 "Match the rest of the command\n"
175 "Choose the mark value to use\n"
178 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
181 vty_out(vty
, "pbr marks are not supported on this platform");
182 return CMD_WARNING_CONFIG_FAILED
;
186 if (pbrms
->mark
== (uint32_t) mark
)
188 pbrms
->mark
= (uint32_t) mark
;
193 pbr_map_check(pbrms
);
198 #define SET_VRF_EXISTS_STR \
199 "A `set vrf XX` command already exists, please remove that first\n"
201 DEFPY(pbr_map_nexthop_group
, pbr_map_nexthop_group_cmd
,
202 "[no] set nexthop-group NHGNAME$name",
204 "Set for the PBR-MAP\n"
205 "nexthop-group to use\n"
206 "The name of the nexthop-group\n")
208 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
209 struct nexthop_group_cmd
*nhgc
;
213 "A `set nexthop XX` command already exists, please remove that first\n");
214 return CMD_WARNING_CONFIG_FAILED
;
217 if (pbrms
->vrf_lookup
|| pbrms
->vrf_unchanged
) {
218 vty_out(vty
, SET_VRF_EXISTS_STR
);
219 return CMD_WARNING_CONFIG_FAILED
;
222 nhgc
= nhgc_find(name
);
224 vty_out(vty
, "Specified nexthop-group %s does not exist\n",
227 "PBR-MAP will not be applied until it is created\n");
231 if (pbrms
->nhgrp_name
&& strcmp(name
, pbrms
->nhgrp_name
) == 0)
232 pbr_map_delete_nexthops(pbrms
);
235 "Nexthop Group specified: %s does not exist to remove",
237 return CMD_WARNING_CONFIG_FAILED
;
240 if (pbrms
->nhgrp_name
) {
241 if (strcmp(name
, pbrms
->nhgrp_name
) != 0) {
243 "Please delete current nexthop group before modifying current one");
244 return CMD_WARNING_CONFIG_FAILED
;
249 pbrms
->nhgrp_name
= XSTRDUP(MTYPE_TMP
, name
);
250 pbr_map_check(pbrms
);
256 DEFPY(pbr_map_nexthop
, pbr_map_nexthop_cmd
,
259 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
262 [nexthop-vrf NAME$vrf_name]",
264 "Set for the PBR-MAP\n"
265 "Specify one of the nexthops in this map\n"
270 "If the nexthop is in a different vrf tell us\n"
271 "The nexthop-vrf Name\n")
273 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
278 if (pbrms
->nhgrp_name
) {
280 "Please unconfigure the nexthop group before adding an individual nexthop");
281 return CMD_WARNING_CONFIG_FAILED
;
284 if (pbrms
->vrf_lookup
|| pbrms
->vrf_unchanged
) {
285 vty_out(vty
, SET_VRF_EXISTS_STR
);
286 return CMD_WARNING_CONFIG_FAILED
;
290 vrf
= vrf_lookup_by_name(vrf_name
);
292 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
295 vty_out(vty
, "Specified: %s is non-existent\n", vrf_name
);
296 return CMD_WARNING_CONFIG_FAILED
;
299 memset(&nhop
, 0, sizeof(nhop
));
300 nhop
.vrf_id
= vrf
->vrf_id
;
303 nhop
.ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
304 if (nhop
.ifindex
== IFINDEX_INTERNAL
) {
306 "Specified Intf %s does not exist in vrf: %s\n",
308 return CMD_WARNING_CONFIG_FAILED
;
313 if (addr
->sa
.sa_family
== AF_INET
) {
314 nhop
.gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
316 nhop
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
318 nhop
.type
= NEXTHOP_TYPE_IPV4
;
320 nhop
.gate
.ipv6
= addr
->sin6
.sin6_addr
;
322 nhop
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
324 if (IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
326 "Specified a v6 LL with no interface, rejecting\n");
327 return CMD_WARNING_CONFIG_FAILED
;
329 nhop
.type
= NEXTHOP_TYPE_IPV6
;
333 nhop
.type
= NEXTHOP_TYPE_IFINDEX
;
336 nh
= nexthop_exists(pbrms
->nhg
, &nhop
);
338 char buf
[PBR_NHC_NAMELEN
];
341 vty_out(vty
, "No nexthops to delete\n");
342 return CMD_WARNING_CONFIG_FAILED
;
345 pbrms
->nhg
= nexthop_group_new();
346 pbrms
->internal_nhg_name
=
348 pbr_nht_nexthop_make_name(pbrms
->parent
->name
,
357 pbr_nht_delete_individual_nexthop(pbrms
);
360 if (pbrms
->nhg
->nexthop
) {
362 "If you would like more than one nexthop please use nexthop-groups");
363 return CMD_WARNING_CONFIG_FAILED
;
366 /* must be adding new nexthop since !no and !nexthop_exists */
369 memcpy(nh
, &nhop
, sizeof(nhop
));
370 _nexthop_add(&pbrms
->nhg
->nexthop
, nh
);
372 pbr_nht_add_individual_nexthop(pbrms
);
373 pbr_map_check(pbrms
);
376 if (nhop
.type
== NEXTHOP_TYPE_IFINDEX
377 || (nhop
.type
== NEXTHOP_TYPE_IPV6_IFINDEX
378 && IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
))) {
379 struct interface
*ifp
;
381 ifp
= if_lookup_by_index(nhop
.ifindex
, nhop
.vrf_id
);
383 pbr_nht_nexthop_interface_update(ifp
);
389 DEFPY(pbr_map_vrf
, pbr_map_vrf_cmd
,
390 "[no] set vrf <NAME$vrf_name|unchanged>",
392 "Set for the PBR-MAP\n"
393 "Specify the VRF for this map\n"
395 "Use the interface's VRF for lookup\n")
397 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
398 int ret
= CMD_SUCCESS
;
401 pbr_map_delete_vrf(pbrms
);
404 pbrms
->nhs_installed
= false;
405 pbrms
->vrf_name
[0] = '\0';
406 pbrms
->vrf_lookup
= false;
407 pbrms
->vrf_unchanged
= false;
412 if (pbrms
->nhgrp_name
|| pbrms
->nhg
) {
414 "A `set nexthop/nexthop-group XX` command already exits, please remove that first\n");
415 ret
= CMD_WARNING_CONFIG_FAILED
;
419 if (pbrms
->vrf_lookup
|| pbrms
->vrf_unchanged
) {
420 vty_out(vty
, SET_VRF_EXISTS_STR
);
421 ret
= CMD_WARNING_CONFIG_FAILED
;
426 if (!pbr_vrf_lookup_by_name(vrf_name
)) {
427 vty_out(vty
, "Specified: %s is non-existent\n",
429 ret
= CMD_WARNING_CONFIG_FAILED
;
433 pbrms
->vrf_lookup
= true;
434 strlcpy(pbrms
->vrf_name
, vrf_name
, sizeof(pbrms
->vrf_name
));
436 pbrms
->vrf_unchanged
= true;
438 pbr_map_check(pbrms
);
446 "[no] pbr-policy PBRMAP$mapname",
449 "Name of the pbr-map to apply\n")
451 VTY_DECLVAR_CONTEXT(interface
, ifp
);
452 struct pbr_map
*pbrm
, *old_pbrm
;
453 struct pbr_interface
*pbr_ifp
= ifp
->info
;
456 pbrm
= pbrm_find(mapname
);
459 /* we don't want one and we don't have one, so... */
463 /* Some one could have fat fingered the interface name */
464 pbr_ifp
= pbr_if_new(ifp
);
468 if (strcmp(pbr_ifp
->mapname
, mapname
) == 0) {
469 pbr_ifp
->mapname
[0] = '\0';
471 pbr_map_interface_delete(pbrm
, ifp
);
474 if (strcmp(pbr_ifp
->mapname
, "") != 0) {
475 old_pbrm
= pbrm_find(pbr_ifp
->mapname
);
478 * So if we have an old pbrm we should only
479 * delete it if we are actually deleting and
480 * moving to a new pbrm
482 if (old_pbrm
&& old_pbrm
!= pbrm
)
483 pbr_map_interface_delete(old_pbrm
, ifp
);
485 snprintf(pbr_ifp
->mapname
, sizeof(pbr_ifp
->mapname
),
489 * So only reinstall if the old_pbrm and this pbrm are
492 if (pbrm
&& pbrm
!= old_pbrm
)
493 pbr_map_add_interface(pbrm
, ifp
);
505 pbr_nht_write_table_range(vty
);
506 pbr_nht_write_rule_range(vty
);
513 "show pbr map [NAME$name] [detail$detail]",
518 "Detailed information\n")
520 struct pbr_map_sequence
*pbrms
;
521 struct pbr_map
*pbrm
;
522 struct listnode
*node
;
523 char buf
[PREFIX_STRLEN
];
526 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
527 if (name
&& strcmp(name
, pbrm
->name
) != 0)
530 vty_out(vty
, " pbr-map %s valid: %d\n", pbrm
->name
,
533 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
535 pbr_map_reason_string(pbrms
->reason
, rbuf
,
538 " Seq: %u rule: %u Installed: %" PRIu64
"(%u) Reason: %s\n",
539 pbrms
->seqno
, pbrms
->ruleno
, pbrms
->installed
,
540 pbrms
->unique
, pbrms
->reason
? rbuf
: "Valid");
543 vty_out(vty
, "\tSRC Match: %s\n",
544 prefix2str(pbrms
->src
, buf
,
547 vty_out(vty
, "\tDST Match: %s\n",
548 prefix2str(pbrms
->dst
, buf
,
551 vty_out(vty
, "\tMARK Match: %u\n", pbrms
->mark
);
553 if (pbrms
->nhgrp_name
) {
555 "\tNexthop-Group: %s(%u) Installed: %u(%d)\n",
557 pbr_nht_get_table(pbrms
->nhgrp_name
),
558 pbrms
->nhs_installed
,
559 pbr_nht_get_installed(
561 } else if (pbrms
->nhg
) {
563 nexthop_group_write_nexthop(
564 vty
, pbrms
->nhg
->nexthop
);
566 "\tInstalled: %u(%d) Tableid: %d\n",
567 pbrms
->nhs_installed
,
568 pbr_nht_get_installed(
569 pbrms
->internal_nhg_name
),
571 pbrms
->internal_nhg_name
));
572 } else if (pbrms
->vrf_unchanged
) {
574 "\tVRF Unchanged (use interface vrf)\n");
575 } else if (pbrms
->vrf_lookup
) {
576 vty_out(vty
, "\tVRF Lookup: %s\n",
580 "\tNexthop-Group: Unknown Installed: 0(0)\n");
587 DEFPY(show_pbr_nexthop_group
,
588 show_pbr_nexthop_group_cmd
,
589 "show pbr nexthop-groups [WORD$word]",
593 "Optional Name of the nexthop group\n")
595 pbr_nht_show_nexthop_group(vty
, word
);
600 DEFPY (show_pbr_interface
,
601 show_pbr_interface_cmd
,
602 "show pbr interface [NAME$name]",
606 "PBR Interface Name\n")
608 struct interface
*ifp
;
610 struct pbr_interface
*pbr_ifp
;
612 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
) {
613 FOR_ALL_INTERFACES(vrf
, ifp
) {
614 struct pbr_map
*pbrm
;
619 if (name
&& strcmp(ifp
->name
, name
) != 0)
624 if (strcmp(pbr_ifp
->mapname
, "") == 0)
627 pbrm
= pbrm_find(pbr_ifp
->mapname
);
628 vty_out(vty
, " %s(%d) with pbr-policy %s", ifp
->name
,
629 ifp
->ifindex
, pbr_ifp
->mapname
);
631 vty_out(vty
, " (map doesn't exist)");
639 /* PBR debugging CLI ------------------------------------------------------- */
641 static struct cmd_node debug_node
= {DEBUG_NODE
, "", 1};
645 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
650 "PBRD <-> Zebra communications\n"
654 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
657 DEBUG_MODE_SET(&pbr_dbg_map
, mode
, !no
);
659 DEBUG_MODE_SET(&pbr_dbg_zebra
, mode
, !no
);
661 DEBUG_MODE_SET(&pbr_dbg_nht
, mode
, !no
);
663 DEBUG_MODE_SET(&pbr_dbg_event
, mode
, !no
);
665 /* no specific debug --> act on all of them */
666 if (strmatch(argv
[argc
- 1]->text
, "pbr"))
667 pbr_debug_set_all(mode
, !no
);
672 DEFUN_NOSH(show_debugging_pbr
,
673 show_debugging_pbr_cmd
,
674 "show debugging [pbr]",
679 vty_out(vty
, "PBR debugging status:\n");
681 pbr_debug_config_write_helper(vty
, false);
686 /* ------------------------------------------------------------------------- */
689 static struct cmd_node interface_node
= {
690 INTERFACE_NODE
, "%s(config-if)# ", 1 /* vtysh ? yes */
693 static int pbr_interface_config_write(struct vty
*vty
)
695 struct interface
*ifp
;
698 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
699 FOR_ALL_INTERFACES (vrf
, ifp
) {
700 if (vrf
->vrf_id
== VRF_DEFAULT
)
701 vty_frame(vty
, "interface %s\n", ifp
->name
);
703 vty_frame(vty
, "interface %s vrf %s\n",
704 ifp
->name
, vrf
->name
);
707 vty_out(vty
, " description %s\n", ifp
->desc
);
709 pbr_map_write_interfaces(vty
, ifp
);
711 vty_endframe(vty
, "!\n");
718 /* PBR map node structure. */
719 static struct cmd_node pbr_map_node
= {PBRMAP_NODE
, "%s(config-pbr-map)# ", 1};
721 static int pbr_vty_map_config_write_sequence(struct vty
*vty
,
722 struct pbr_map
*pbrm
,
723 struct pbr_map_sequence
*pbrms
)
725 char buff
[PREFIX_STRLEN
];
727 vty_out(vty
, "pbr-map %s seq %u\n", pbrm
->name
, pbrms
->seqno
);
730 vty_out(vty
, " match src-ip %s\n",
731 prefix2str(pbrms
->src
, buff
, sizeof(buff
)));
734 vty_out(vty
, " match dst-ip %s\n",
735 prefix2str(pbrms
->dst
, buff
, sizeof(buff
)));
738 vty_out(vty
, " match mark %u\n", pbrms
->mark
);
740 if (pbrms
->vrf_unchanged
)
741 vty_out(vty
, " set vrf unchanged\n");
743 if (pbrms
->vrf_lookup
)
744 vty_out(vty
, " set vrf %s\n", pbrms
->vrf_name
);
746 if (pbrms
->nhgrp_name
)
747 vty_out(vty
, " set nexthop-group %s\n", pbrms
->nhgrp_name
);
750 vty_out(vty
, " set ");
751 nexthop_group_write_nexthop(vty
, pbrms
->nhg
->nexthop
);
758 static int pbr_vty_map_config_write(struct vty
*vty
)
760 struct pbr_map
*pbrm
;
762 pbr_nht_write_table_range(vty
);
763 pbr_nht_write_rule_range(vty
);
765 RB_FOREACH(pbrm
, pbr_map_entry_head
, &pbr_maps
) {
766 struct pbr_map_sequence
*pbrms
;
767 struct listnode
*node
;
769 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
770 pbr_vty_map_config_write_sequence(vty
, pbrm
, pbrms
);
776 static void pbr_map_completer(vector comps
, struct cmd_token
*token
)
778 struct pbr_map
*pbrm
;
780 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
)
781 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, pbrm
->name
));
784 static const struct cmd_variable_handler pbr_map_name
[] = {
786 .tokenname
= "PBRMAP", .completions
= pbr_map_completer
,
793 void pbr_vty_init(void)
795 cmd_variable_handler_register(pbr_map_name
);
797 install_node(&interface_node
,
798 pbr_interface_config_write
);
801 install_node(&pbr_map_node
,
802 pbr_vty_map_config_write
);
805 install_node(&debug_node
, pbr_debug_config_write
);
806 install_element(VIEW_NODE
, &debug_pbr_cmd
);
807 install_element(CONFIG_NODE
, &debug_pbr_cmd
);
808 install_element(VIEW_NODE
, &show_debugging_pbr_cmd
);
810 install_default(PBRMAP_NODE
);
812 install_element(CONFIG_NODE
, &pbr_map_cmd
);
813 install_element(CONFIG_NODE
, &no_pbr_map_cmd
);
814 install_element(CONFIG_NODE
, &pbr_set_table_range_cmd
);
815 install_element(INTERFACE_NODE
, &pbr_policy_cmd
);
816 install_element(PBRMAP_NODE
, &pbr_map_match_src_cmd
);
817 install_element(PBRMAP_NODE
, &pbr_map_match_dst_cmd
);
818 install_element(PBRMAP_NODE
, &pbr_map_match_mark_cmd
);
819 install_element(PBRMAP_NODE
, &pbr_map_nexthop_group_cmd
);
820 install_element(PBRMAP_NODE
, &pbr_map_nexthop_cmd
);
821 install_element(PBRMAP_NODE
, &pbr_map_vrf_cmd
);
822 install_element(VIEW_NODE
, &show_pbr_cmd
);
823 install_element(VIEW_NODE
, &show_pbr_map_cmd
);
824 install_element(VIEW_NODE
, &show_pbr_interface_cmd
);
825 install_element(VIEW_NODE
, &show_pbr_nexthop_group_cmd
);