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"
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"
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"
47 const char *pbrm_name
= argv
[1]->arg
;
48 uint32_t seqno
= atoi(argv
[3]->arg
);
49 struct pbr_map_sequence
*pbrms
;
51 pbrms
= pbrms_get(pbrm_name
, seqno
);
52 VTY_PUSH_CONTEXT(PBRMAP_NODE
, pbrms
);
57 DEFUN_NOSH(no_pbr_map
, no_pbr_map_cmd
, "no pbr-map WORD [seq (1-700)]",
60 "The name of the PBR MAP\n"
61 "Sequence to delete from existing pbr-map entry\n"
64 const char *pbrm_name
= argv
[2]->arg
;
66 struct pbr_map
*pbrm
= pbrm_find(pbrm_name
);
67 struct pbr_map_sequence
*pbrms
;
68 struct listnode
*node
, *next_node
;
71 seqno
= atoi(argv
[4]->arg
);
74 vty_out(vty
, "pbr-map %s not found\n", pbrm_name
);
78 for (ALL_LIST_ELEMENTS(pbrm
->seqnumbers
, node
, next_node
, pbrms
)) {
79 if (seqno
&& pbrms
->seqno
!= seqno
)
82 pbr_map_delete(pbrms
);
88 DEFPY(pbr_set_table_range
,
89 pbr_set_table_range_cmd
,
90 "[no] pbr table range (10000-4294966272)$lb (10000-4294966272)$ub",
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")
98 /* upper bound is 2^32 - 2^10 */
99 int ret
= CMD_WARNING
;
101 /* validate given bounds */
103 vty_out(vty
, "%% Lower bound must be less than upper bound\n");
104 else if (ub
- lb
< 10)
105 vty_out(vty
, "%% Range breadth must be at least 10\n");
108 pbr_nht_set_tableid_range((uint32_t) lb
, (uint32_t) ub
);
115 DEFPY(pbr_map_match_src
, pbr_map_match_src_cmd
,
116 "[no] match src-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
118 "Match the rest of the command\n"
119 "Choose the src ip or ipv6 prefix to use\n"
123 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
125 pbrms
->family
= prefix
->family
;
128 if (prefix_same(pbrms
->src
, prefix
))
132 pbrms
->src
= prefix_new();
133 prefix_copy(pbrms
->src
, prefix
);
135 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
);
168 pbr_map_check(pbrms
);
173 DEFPY(pbr_map_nexthop_group
, pbr_map_nexthop_group_cmd
,
174 "[no] set nexthop-group NAME$name",
176 "Set for the PBR-MAP\n"
177 "nexthop-group to use\n"
178 "The name of the nexthop-group\n")
180 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
181 struct nexthop_group_cmd
*nhgc
;
185 "A `set nexthop XX` command already exists, please remove that first\n");
186 return CMD_WARNING_CONFIG_FAILED
;
189 nhgc
= nhgc_find(name
);
191 vty_out(vty
, "Specified nexthop-group %s does not exist\n",
193 vty_out(vty
, "PBR-MAP will not be applied until it is created\n");
197 if (pbrms
->nhgrp_name
&& strcmp(name
, pbrms
->nhgrp_name
) == 0)
198 pbr_map_delete_nexthop_group(pbrms
);
201 "Nexthop Group specified: %s does not exist to remove",
203 return CMD_WARNING_CONFIG_FAILED
;
206 if (pbrms
->nhgrp_name
) {
207 if (strcmp(name
, pbrms
->nhgrp_name
) != 0) {
209 "Please delete current nexthop group before modifying current one");
210 return CMD_WARNING_CONFIG_FAILED
;
215 pbrms
->nhgrp_name
= XSTRDUP(MTYPE_TMP
, name
);
216 pbr_map_check(pbrms
);
222 DEFPY(pbr_map_nexthop
, pbr_map_nexthop_cmd
,
223 "[no] set nexthop <A.B.C.D|X:X::X:X>$addr [INTERFACE]$intf [nexthop-vrf NAME$name]",
225 "Set for the PBR-MAP\n"
226 "Specify one of the nexthops in this map\n"
230 "If the nexthop is in a different vrf tell us\n"
231 "The nexthop-vrf Name\n")
233 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
238 if (pbrms
->nhgrp_name
) {
240 "Please unconfigure the nexthop group before adding an individual nexthop");
241 return CMD_WARNING_CONFIG_FAILED
;
245 vrf
= vrf_lookup_by_name(name
);
247 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
250 vty_out(vty
, "Specified: %s is non-existent\n", name
);
251 return CMD_WARNING_CONFIG_FAILED
;
254 memset(&nhop
, 0, sizeof(nhop
));
255 nhop
.vrf_id
= vrf
->vrf_id
;
258 * Make SA happy. CLIPPY is not going to give us a NULL
262 if (addr
->sa
.sa_family
== AF_INET
) {
263 nhop
.gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
265 nhop
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
266 nhop
.ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
267 if (nhop
.ifindex
== IFINDEX_INTERNAL
) {
269 "Specified Intf %s does not exist in vrf: %s\n",
271 return CMD_WARNING_CONFIG_FAILED
;
274 nhop
.type
= NEXTHOP_TYPE_IPV4
;
276 memcpy(&nhop
.gate
.ipv6
, &addr
->sin6
.sin6_addr
, 16);
278 nhop
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
279 nhop
.ifindex
= ifname2ifindex(intf
, vrf
->vrf_id
);
280 if (nhop
.ifindex
== IFINDEX_INTERNAL
) {
282 "Specified Intf %s does not exist in vrf: %s\n",
284 return CMD_WARNING_CONFIG_FAILED
;
287 if (IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
289 "Specified a v6 LL with no interface, rejecting\n");
290 return CMD_WARNING_CONFIG_FAILED
;
292 nhop
.type
= NEXTHOP_TYPE_IPV6
;
297 nh
= nexthop_exists(pbrms
->nhg
, &nhop
);
299 char buf
[PBR_NHC_NAMELEN
];
302 vty_out(vty
, "No nexthops to delete");
303 return CMD_WARNING_CONFIG_FAILED
;
306 pbrms
->nhg
= nexthop_group_new();
307 pbrms
->internal_nhg_name
=
309 pbr_nht_nexthop_make_name(pbrms
->parent
->name
,
318 pbr_nht_delete_individual_nexthop(pbrms
);
321 if (pbrms
->nhg
->nexthop
) {
323 "If you would like more than one nexthop please use nexthop-groups");
324 return CMD_WARNING_CONFIG_FAILED
;
327 /* must be adding new nexthop since !no and !nexthop_exists */
330 memcpy(nh
, &nhop
, sizeof(nhop
));
331 nexthop_add(&pbrms
->nhg
->nexthop
, nh
);
333 pbr_nht_add_individual_nexthop(pbrms
);
334 pbr_map_check(pbrms
);
342 "[no] pbr-policy NAME$mapname",
345 "Name of the pbr-map to apply\n")
347 VTY_DECLVAR_CONTEXT(interface
, ifp
);
348 struct pbr_map
*pbrm
, *old_pbrm
;
349 struct pbr_interface
*pbr_ifp
= ifp
->info
;
351 pbrm
= pbrm_find(mapname
);
354 /* we don't want one and we don't have one, so... */
358 /* Some one could have fat fingered the interface name */
359 pbr_ifp
= pbr_if_new(ifp
);
363 if (strcmp(pbr_ifp
->mapname
, mapname
) == 0) {
364 pbr_ifp
->mapname
[0] = '\0';
366 pbr_map_interface_delete(pbrm
, ifp
);
369 if (strcmp(pbr_ifp
->mapname
, "") != 0) {
370 old_pbrm
= pbrm_find(pbr_ifp
->mapname
);
372 pbr_map_interface_delete(old_pbrm
, ifp
);
374 snprintf(pbr_ifp
->mapname
, sizeof(pbr_ifp
->mapname
),
377 pbr_map_add_interface(pbrm
, ifp
);
389 pbr_nht_write_table_range(vty
);
390 pbr_nht_write_rule_range(vty
);
397 "show pbr map [NAME$name] [detail$detail]",
402 "Detailed information\n")
404 struct pbr_map_sequence
*pbrms
;
405 struct pbr_map
*pbrm
;
406 struct listnode
*node
;
407 char buf
[PREFIX_STRLEN
];
410 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
411 if (name
&& strcmp(name
, pbrm
->name
) != 0)
414 vty_out(vty
, " pbr-map %s valid: %d\n", pbrm
->name
,
417 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
)) {
419 pbr_map_reason_string(pbrms
->reason
, rbuf
,
422 " Seq: %u rule: %u Installed: %" PRIu64
"(%u) Reason: %s\n",
423 pbrms
->seqno
, pbrms
->ruleno
, pbrms
->installed
,
424 pbrms
->unique
, pbrms
->reason
? rbuf
: "Valid");
427 vty_out(vty
, "\tSRC Match: %s\n",
428 prefix2str(pbrms
->src
, buf
,
431 vty_out(vty
, "\tDST Match: %s\n",
432 prefix2str(pbrms
->dst
, buf
,
435 if (pbrms
->nhgrp_name
) {
437 "\tNexthop-Group: %s(%u) Installed: %u(%d)\n",
439 pbr_nht_get_table(pbrms
->nhgrp_name
),
440 pbrms
->nhs_installed
,
441 pbr_nht_get_installed(
443 } else if (pbrms
->nhg
) {
445 nexthop_group_write_nexthop(
446 vty
, pbrms
->nhg
->nexthop
);
448 "\tInstalled: %u(%d) Tableid: %d\n",
449 pbrms
->nhs_installed
,
450 pbr_nht_get_installed(
451 pbrms
->internal_nhg_name
),
453 pbrms
->internal_nhg_name
));
456 "\tNexthop-Group: Unknown Installed: 0(0)\n");
463 DEFPY(show_pbr_nexthop_group
,
464 show_pbr_nexthop_group_cmd
,
465 "show pbr nexthop-groups [WORD$word]",
469 "Optional Name of the nexthop group\n")
471 pbr_nht_show_nexthop_group(vty
, word
);
476 DEFPY (show_pbr_interface
,
477 show_pbr_interface_cmd
,
478 "show pbr interface [NAME$name]",
482 "PBR Interface Name\n")
484 struct interface
*ifp
;
486 struct pbr_interface
*pbr_ifp
;
488 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
) {
489 FOR_ALL_INTERFACES(vrf
, ifp
) {
490 struct pbr_map
*pbrm
;
495 if (name
&& strcmp(ifp
->name
, name
) != 0)
500 if (strcmp(pbr_ifp
->mapname
, "") == 0)
503 pbrm
= pbrm_find(pbr_ifp
->mapname
);
504 vty_out(vty
, " %s(%d) with pbr-policy %s", ifp
->name
,
505 ifp
->ifindex
, pbr_ifp
->mapname
);
507 vty_out(vty
, " (map doesn't exist)");
515 /* PBR debugging CLI ------------------------------------------------------- */
517 static struct cmd_node debug_node
= {DEBUG_NODE
, "", 1};
521 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
526 "PBRD <-> Zebra communications\n"
530 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
533 DEBUG_MODE_SET(&pbr_dbg_map
, mode
, !no
);
535 DEBUG_MODE_SET(&pbr_dbg_zebra
, mode
, !no
);
537 DEBUG_MODE_SET(&pbr_dbg_nht
, mode
, !no
);
539 DEBUG_MODE_SET(&pbr_dbg_event
, mode
, !no
);
541 /* no specific debug --> act on all of them */
542 if (strmatch(argv
[argc
- 1]->text
, "pbr"))
543 pbr_debug_set_all(mode
, !no
);
548 DEFUN_NOSH(show_debugging_pbr
,
549 show_debugging_pbr_cmd
,
550 "show debugging [pbr]",
555 vty_out(vty
, "PBR debugging status:\n");
557 pbr_debug_config_write_helper(vty
, false);
562 /* ------------------------------------------------------------------------- */
565 static struct cmd_node interface_node
= {
566 INTERFACE_NODE
, "%s(config-if)# ", 1 /* vtysh ? yes */
569 static int pbr_interface_config_write(struct vty
*vty
)
571 struct interface
*ifp
;
574 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
575 FOR_ALL_INTERFACES (vrf
, ifp
) {
576 if (vrf
->vrf_id
== VRF_DEFAULT
)
577 vty_frame(vty
, "interface %s\n", ifp
->name
);
579 vty_frame(vty
, "interface %s vrf %s\n",
580 ifp
->name
, vrf
->name
);
582 pbr_map_write_interfaces(vty
, ifp
);
584 vty_endframe(vty
, "!\n");
591 /* PBR map node structure. */
592 static struct cmd_node pbr_map_node
= {PBRMAP_NODE
, "%s(config-pbr-map)# ", 1};
594 static int pbr_vty_map_config_write_sequence(struct vty
*vty
,
595 struct pbr_map
*pbrm
,
596 struct pbr_map_sequence
*pbrms
)
598 char buff
[PREFIX_STRLEN
];
600 vty_out(vty
, "pbr-map %s seq %u\n", pbrm
->name
, pbrms
->seqno
);
603 vty_out(vty
, " match src-ip %s\n",
604 prefix2str(pbrms
->src
, buff
, sizeof(buff
)));
607 vty_out(vty
, " match dst-ip %s\n",
608 prefix2str(pbrms
->dst
, buff
, sizeof(buff
)));
610 if (pbrms
->nhgrp_name
)
611 vty_out(vty
, " set nexthop-group %s\n", pbrms
->nhgrp_name
);
614 vty_out(vty
, " set ");
615 nexthop_group_write_nexthop(vty
, pbrms
->nhg
->nexthop
);
622 static int pbr_vty_map_config_write(struct vty
*vty
)
624 struct pbr_map
*pbrm
;
626 pbr_nht_write_table_range(vty
);
627 pbr_nht_write_rule_range(vty
);
629 RB_FOREACH(pbrm
, pbr_map_entry_head
, &pbr_maps
) {
630 struct pbr_map_sequence
*pbrms
;
631 struct listnode
*node
;
633 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
634 pbr_vty_map_config_write_sequence(vty
, pbrm
, pbrms
);
640 void pbr_vty_init(void)
642 install_node(&interface_node
,
643 pbr_interface_config_write
);
646 install_node(&pbr_map_node
,
647 pbr_vty_map_config_write
);
650 install_node(&debug_node
, pbr_debug_config_write
);
651 install_element(VIEW_NODE
, &debug_pbr_cmd
);
652 install_element(CONFIG_NODE
, &debug_pbr_cmd
);
653 install_element(VIEW_NODE
, &show_debugging_pbr_cmd
);
655 install_default(PBRMAP_NODE
);
657 install_element(CONFIG_NODE
, &pbr_map_cmd
);
658 install_element(CONFIG_NODE
, &no_pbr_map_cmd
);
659 install_element(CONFIG_NODE
, &pbr_set_table_range_cmd
);
660 install_element(INTERFACE_NODE
, &pbr_policy_cmd
);
661 install_element(PBRMAP_NODE
, &pbr_map_match_src_cmd
);
662 install_element(PBRMAP_NODE
, &pbr_map_match_dst_cmd
);
663 install_element(PBRMAP_NODE
, &pbr_map_nexthop_group_cmd
);
664 install_element(PBRMAP_NODE
, &pbr_map_nexthop_cmd
);
665 install_element(VIEW_NODE
, &show_pbr_cmd
);
666 install_element(VIEW_NODE
, &show_pbr_map_cmd
);
667 install_element(VIEW_NODE
, &show_pbr_interface_cmd
);
668 install_element(VIEW_NODE
, &show_pbr_nexthop_group_cmd
);