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"
34 #include "pbrd/pbr_nht.h"
35 #include "pbrd/pbr_map.h"
36 #include "pbrd/pbr_zebra.h"
37 #include "pbrd/pbr_vty.h"
38 #include "pbrd/pbr_debug.h"
39 #ifndef VTYSH_EXTRACT_PL
40 #include "pbrd/pbr_vty_clippy.c"
43 DEFUN_NOSH(pbr_map
, pbr_map_cmd
, "pbr-map PBRMAP seq (1-700)",
44 "Create pbr-map or enter pbr-map command mode\n"
45 "The name of the PBR MAP\n"
46 "Sequence to insert in existing pbr-map entry\n"
49 const char *pbrm_name
= argv
[1]->arg
;
50 uint32_t seqno
= atoi(argv
[3]->arg
);
51 struct pbr_map_sequence
*pbrms
;
53 pbrms
= pbrms_get(pbrm_name
, seqno
);
54 VTY_PUSH_CONTEXT(PBRMAP_NODE
, pbrms
);
59 DEFUN_NOSH(no_pbr_map
, no_pbr_map_cmd
, "no pbr-map PBRMAP [seq (1-700)]",
62 "The name of the PBR MAP\n"
63 "Sequence to delete from existing pbr-map entry\n"
66 const char *pbrm_name
= argv
[2]->arg
;
68 struct pbr_map
*pbrm
= pbrm_find(pbrm_name
);
69 struct pbr_map_sequence
*pbrms
;
70 struct listnode
*node
, *next_node
;
73 seqno
= atoi(argv
[4]->arg
);
76 vty_out(vty
, "pbr-map %s not found\n", pbrm_name
);
80 for (ALL_LIST_ELEMENTS(pbrm
->seqnumbers
, node
, next_node
, pbrms
)) {
81 if (seqno
&& pbrms
->seqno
!= seqno
)
84 pbr_map_delete(pbrms
);
90 DEFPY(pbr_set_table_range
,
91 pbr_set_table_range_cmd
,
92 "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
);
116 DEFPY(no_pbr_set_table_range
, no_pbr_set_table_range_cmd
,
117 "no pbr table range [(10000-4294966272)$lb (10000-4294966272)$ub]",
120 "Set table ID range\n"
121 "Set table ID range\n"
122 "Lower bound for table ID range\n"
123 "Upper bound for table ID range\n")
125 pbr_nht_set_tableid_range(PBR_NHT_DEFAULT_LOW_TABLEID
,
126 PBR_NHT_DEFAULT_HIGH_TABLEID
);
130 DEFPY(pbr_map_match_src
, pbr_map_match_src_cmd
,
131 "[no] match src-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
133 "Match the rest of the command\n"
134 "Choose the src ip or ipv6 prefix to use\n"
138 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
140 if (pbrms
->dst
&& pbrms
->family
&& prefix
->family
!= pbrms
->family
) {
141 vty_out(vty
, "Cannot mismatch families within match src/dst\n");
142 return CMD_WARNING_CONFIG_FAILED
;
145 pbrms
->family
= prefix
->family
;
149 if (prefix_same(pbrms
->src
, prefix
))
152 pbrms
->src
= prefix_new();
154 prefix_copy(pbrms
->src
, prefix
);
156 prefix_free(&pbrms
->src
);
158 pbr_map_check(pbrms
, true);
163 DEFPY(pbr_map_match_dst
, pbr_map_match_dst_cmd
,
164 "[no] match dst-ip <A.B.C.D/M|X:X::X:X/M>$prefix",
166 "Match the rest of the command\n"
167 "Choose the dst ip or ipv6 prefix to use\n"
171 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
173 if (pbrms
->src
&& pbrms
->family
&& prefix
->family
!= pbrms
->family
) {
174 vty_out(vty
, "Cannot mismatch families within match src/dst\n");
175 return CMD_WARNING_CONFIG_FAILED
;
178 pbrms
->family
= prefix
->family
;
182 if (prefix_same(pbrms
->dst
, prefix
))
185 pbrms
->dst
= prefix_new();
187 prefix_copy(pbrms
->dst
, prefix
);
189 prefix_free(&pbrms
->dst
);
191 pbr_map_check(pbrms
, true);
196 DEFPY(pbr_map_match_dscp
, pbr_map_match_dscp_cmd
,
197 "[no] match dscp DSCP$dscp",
199 "Match the rest of the command\n"
200 "Match based on IP DSCP field\n"
201 "DSCP value (below 64) or standard codepoint name\n")
203 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
207 /* Discriminate dscp enums (cs0, cs1 etc.) and numbers */
208 bool isANumber
= true;
209 for (int i
= 0; i
< (int)strlen(dscp
); i
++) {
210 /* Letters are not numbers */
211 if (!isdigit(dscp
[i
]))
214 /* Lowercase the dscp enum (if needed) */
215 if (isupper(dscp
[i
]))
216 dscpname
[i
] = tolower(dscp
[i
]);
218 dscpname
[i
] = dscp
[i
];
220 dscpname
[strlen(dscp
)] = '\0';
223 /* dscp passed is a regular number */
224 long dscpAsNum
= strtol(dscp
, NULL
, 0);
226 if (dscpAsNum
> PBR_DSFIELD_DSCP
>> 2) {
227 /* Refuse to install on overflow */
228 vty_out(vty
, "dscp (%s) must be less than 64\n", dscp
);
229 return CMD_WARNING_CONFIG_FAILED
;
233 /* check dscp if it is an enum like cs0 */
234 rawDscp
= pbr_map_decode_dscp_enum(dscpname
);
235 if (rawDscp
> PBR_DSFIELD_DSCP
) {
236 vty_out(vty
, "Invalid dscp value: %s\n", dscpname
);
237 return CMD_WARNING_CONFIG_FAILED
;
242 if (((pbrms
->dsfield
& PBR_DSFIELD_DSCP
) >> 2) == rawDscp
)
245 /* Set the DSCP bits of the DSField */
247 (pbrms
->dsfield
& ~PBR_DSFIELD_DSCP
) | (rawDscp
<< 2);
249 pbrms
->dsfield
&= ~PBR_DSFIELD_DSCP
;
252 pbr_map_check(pbrms
, true);
257 DEFPY(pbr_map_match_ecn
, pbr_map_match_ecn_cmd
,
258 "[no] match ecn (0-3)$ecn",
260 "Match the rest of the command\n"
261 "Match based on IP ECN field\n"
262 "Explicit Congestion Notification\n")
264 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
267 if ((pbrms
->dsfield
& PBR_DSFIELD_ECN
) == ecn
)
270 /* Set the ECN bits of the DSField */
271 pbrms
->dsfield
= (pbrms
->dsfield
& ~PBR_DSFIELD_ECN
) | ecn
;
273 pbrms
->dsfield
&= ~PBR_DSFIELD_ECN
;
276 pbr_map_check(pbrms
, true);
281 DEFPY(pbr_map_match_mark
, pbr_map_match_mark_cmd
,
282 "[no] match mark (1-4294967295)$mark",
284 "Match the rest of the command\n"
285 "Choose the mark value to use\n"
288 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
291 vty_out(vty
, "pbr marks are not supported on this platform");
292 return CMD_WARNING_CONFIG_FAILED
;
297 if (pbrms
->mark
== (uint32_t)mark
)
300 pbrms
->mark
= (uint32_t)mark
;
304 pbr_map_check(pbrms
, true);
309 static void pbrms_clear_set_vrf_config(struct pbr_map_sequence
*pbrms
)
311 if (pbrms
->vrf_lookup
|| pbrms
->vrf_unchanged
) {
312 pbr_map_delete_vrf(pbrms
);
313 pbrms
->vrf_name
[0] = '\0';
314 pbrms
->vrf_lookup
= false;
315 pbrms
->vrf_unchanged
= false;
319 static void pbrms_clear_set_nhg_config(struct pbr_map_sequence
*pbrms
)
321 if (pbrms
->nhgrp_name
)
322 pbr_map_delete_nexthops(pbrms
);
325 static void pbrms_clear_set_nexthop_config(struct pbr_map_sequence
*pbrms
)
328 pbr_nht_delete_individual_nexthop(pbrms
);
331 static void pbrms_clear_set_config(struct pbr_map_sequence
*pbrms
)
333 pbrms_clear_set_vrf_config(pbrms
);
334 pbrms_clear_set_nhg_config(pbrms
);
335 pbrms_clear_set_nexthop_config(pbrms
);
337 pbrms
->nhs_installed
= false;
340 DEFPY(pbr_map_nexthop_group
, pbr_map_nexthop_group_cmd
,
341 "set nexthop-group NHGNAME$name",
342 "Set for the PBR-MAP\n"
343 "nexthop-group to use\n"
344 "The name of the nexthop-group\n")
346 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
347 struct nexthop_group_cmd
*nhgc
;
349 nhgc
= nhgc_find(name
);
351 vty_out(vty
, "Specified nexthop-group %s does not exist\n",
354 "PBR-MAP will not be applied until it is created\n");
357 if (pbrms
->nhgrp_name
&& strcmp(name
, pbrms
->nhgrp_name
) == 0)
360 /* This is new/replacement config */
361 pbrms_clear_set_config(pbrms
);
363 pbrms
->nhgrp_name
= XSTRDUP(MTYPE_TMP
, name
);
364 pbr_map_check(pbrms
, true);
369 DEFPY(no_pbr_map_nexthop_group
, no_pbr_map_nexthop_group_cmd
,
370 "no set nexthop-group [NHGNAME$name]",
372 "Set for the PBR-MAP\n"
373 "nexthop-group to use\n"
374 "The name of the nexthop-group\n")
376 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
378 pbrms_clear_set_config(pbrms
);
383 DEFPY(pbr_map_nexthop
, pbr_map_nexthop_cmd
,
386 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
389 [nexthop-vrf NAME$vrf_name]",
390 "Set for the PBR-MAP\n"
391 "Specify one of the nexthops in this map\n"
396 "If the nexthop is in a different vrf tell us\n"
397 "The nexthop-vrf Name\n")
399 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
402 struct nexthop
*nh
= NULL
;
405 vrf
= vrf_lookup_by_name(vrf_name
);
407 vrf
= vrf_lookup_by_id(VRF_DEFAULT
);
410 vty_out(vty
, "Specified VRF: %s is non-existent\n", vrf_name
);
411 return CMD_WARNING_CONFIG_FAILED
;
414 memset(&nhop
, 0, sizeof(nhop
));
415 nhop
.vrf_id
= vrf
->vrf_id
;
418 struct interface
*ifp
;
420 ifp
= if_lookup_by_name_all_vrf(intf
);
422 vty_out(vty
, "Specified Intf %s does not exist\n",
424 return CMD_WARNING_CONFIG_FAILED
;
426 if (ifp
->vrf_id
!= vrf
->vrf_id
) {
429 actual
= vrf_lookup_by_id(ifp
->vrf_id
);
431 "Specified Intf %s is not in vrf %s but is in vrf %s, using actual vrf\n",
432 ifp
->name
, vrf
->name
, actual
->name
);
434 nhop
.ifindex
= ifp
->ifindex
;
435 nhop
.vrf_id
= ifp
->vrf_id
;
439 if (addr
->sa
.sa_family
== AF_INET
) {
440 nhop
.gate
.ipv4
.s_addr
= addr
->sin
.sin_addr
.s_addr
;
442 nhop
.type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
444 nhop
.type
= NEXTHOP_TYPE_IPV4
;
446 nhop
.gate
.ipv6
= addr
->sin6
.sin6_addr
;
448 nhop
.type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
450 if (IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
)) {
452 "Specified a v6 LL with no interface, rejecting\n");
453 return CMD_WARNING_CONFIG_FAILED
;
455 nhop
.type
= NEXTHOP_TYPE_IPV6
;
459 nhop
.type
= NEXTHOP_TYPE_IFINDEX
;
462 nh
= nexthop_exists(pbrms
->nhg
, &nhop
);
464 if (nh
) /* Same config re-entered */
467 /* This is new/replacement config */
468 pbrms_clear_set_config(pbrms
);
470 pbr_nht_add_individual_nexthop(pbrms
, &nhop
);
472 pbr_map_check(pbrms
, true);
475 if (nhop
.type
== NEXTHOP_TYPE_IFINDEX
476 || (nhop
.type
== NEXTHOP_TYPE_IPV6_IFINDEX
477 && IN6_IS_ADDR_LINKLOCAL(&nhop
.gate
.ipv6
))) {
478 struct interface
*ifp
;
480 ifp
= if_lookup_by_index(nhop
.ifindex
, nhop
.vrf_id
);
482 pbr_nht_nexthop_interface_update(ifp
);
488 DEFPY(no_pbr_map_nexthop
, no_pbr_map_nexthop_cmd
,
491 <A.B.C.D|X:X::X:X>$addr [INTERFACE$intf]\
494 [nexthop-vrf NAME$vrf_name]]",
496 "Set for the PBR-MAP\n"
497 "Specify one of the nexthops in this map\n"
502 "If the nexthop is in a different vrf tell us\n"
503 "The nexthop-vrf Name\n")
505 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
507 pbrms_clear_set_config(pbrms
);
512 DEFPY(pbr_map_vrf
, pbr_map_vrf_cmd
,
513 "set vrf <NAME$vrf_name|unchanged>",
514 "Set for the PBR-MAP\n"
515 "Specify the VRF for this map\n"
517 "Use the interface's VRF for lookup\n")
519 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
522 * If an equivalent set vrf * exists, just return success.
524 if (vrf_name
&& pbrms
->vrf_lookup
525 && strncmp(pbrms
->vrf_name
, vrf_name
, sizeof(pbrms
->vrf_name
)) == 0)
527 else if (!vrf_name
&& pbrms
->vrf_unchanged
) /* Unchanged already set */
530 if (vrf_name
&& !pbr_vrf_lookup_by_name(vrf_name
)) {
531 vty_out(vty
, "Specified: %s is non-existent\n", vrf_name
);
532 return CMD_WARNING_CONFIG_FAILED
;
535 /* This is new/replacement config */
536 pbrms_clear_set_config(pbrms
);
539 pbrms
->vrf_lookup
= true;
540 strlcpy(pbrms
->vrf_name
, vrf_name
, sizeof(pbrms
->vrf_name
));
542 pbrms
->vrf_unchanged
= true;
544 pbr_map_check(pbrms
, true);
549 DEFPY(no_pbr_map_vrf
, no_pbr_map_vrf_cmd
,
550 "no set vrf [<NAME$vrf_name|unchanged>]",
552 "Set for the PBR-MAP\n"
553 "Specify the VRF for this map\n"
555 "Use the interface's VRF for lookup\n")
557 struct pbr_map_sequence
*pbrms
= VTY_GET_CONTEXT(pbr_map_sequence
);
559 pbrms_clear_set_config(pbrms
);
566 "[no] pbr-policy PBRMAP$mapname",
569 "Name of the pbr-map to apply\n")
571 VTY_DECLVAR_CONTEXT(interface
, ifp
);
572 struct pbr_map
*pbrm
, *old_pbrm
;
573 struct pbr_interface
*pbr_ifp
= ifp
->info
;
576 pbrm
= pbrm_find(mapname
);
579 /* we don't want one and we don't have one, so... */
583 /* Some one could have fat fingered the interface name */
584 pbr_ifp
= pbr_if_new(ifp
);
588 if (strcmp(pbr_ifp
->mapname
, mapname
) == 0) {
589 pbr_ifp
->mapname
[0] = '\0';
591 pbr_map_interface_delete(pbrm
, ifp
);
594 if (strcmp(pbr_ifp
->mapname
, "") != 0) {
595 old_pbrm
= pbrm_find(pbr_ifp
->mapname
);
598 * So if we have an old pbrm we should only
599 * delete it if we are actually deleting and
600 * moving to a new pbrm
602 if (old_pbrm
&& old_pbrm
!= pbrm
)
603 pbr_map_interface_delete(old_pbrm
, ifp
);
605 snprintf(pbr_ifp
->mapname
, sizeof(pbr_ifp
->mapname
),
609 * So only reinstall if the old_pbrm and this pbrm are
612 if (pbrm
&& pbrm
!= old_pbrm
)
613 pbr_map_add_interface(pbrm
, ifp
);
625 pbr_nht_write_table_range(vty
);
626 pbr_nht_write_rule_range(vty
);
632 pbrms_nexthop_group_write_individual_nexthop(
633 struct vty
*vty
, const struct pbr_map_sequence
*pbrms
)
635 struct pbr_nexthop_group_cache find
;
636 struct pbr_nexthop_group_cache
*pnhgc
;
637 struct pbr_nexthop_cache lookup
;
638 struct pbr_nexthop_cache
*pnhc
;
640 memset(&find
, 0, sizeof(find
));
641 strlcpy(find
.name
, pbrms
->internal_nhg_name
, sizeof(find
.name
));
643 pnhgc
= hash_lookup(pbr_nhg_hash
, &find
);
646 lookup
.nexthop
= *pbrms
->nhg
->nexthop
;
647 pnhc
= hash_lookup(pnhgc
->nhh
, &lookup
);
649 nexthop_group_write_nexthop_simple(
650 vty
, pbrms
->nhg
->nexthop
,
651 pnhc
->nexthop
.ifindex
!= 0 ? pnhc
->intf_name
: NULL
);
652 if (pnhc
->nexthop
.vrf_id
!= VRF_DEFAULT
)
653 vty_out(vty
, " nexthop-vrf %s", pnhc
->vrf_name
);
658 static void vty_show_pbrms(struct vty
*vty
,
659 const struct pbr_map_sequence
*pbrms
, bool detail
)
664 pbr_map_reason_string(pbrms
->reason
, rbuf
, sizeof(rbuf
));
666 vty_out(vty
, " Seq: %u rule: %u\n", pbrms
->seqno
, pbrms
->ruleno
);
669 vty_out(vty
, " Installed: %" PRIu64
"(%u) Reason: %s\n",
670 pbrms
->installed
, pbrms
->unique
,
671 pbrms
->reason
? rbuf
: "Valid");
673 vty_out(vty
, " Installed: %s Reason: %s\n",
674 pbrms
->installed
? "yes" : "no",
675 pbrms
->reason
? rbuf
: "Valid");
678 vty_out(vty
, " SRC Match: %pFX\n", pbrms
->src
);
680 vty_out(vty
, " DST Match: %pFX\n", pbrms
->dst
);
681 if (pbrms
->dsfield
& PBR_DSFIELD_DSCP
)
682 vty_out(vty
, " DSCP Match: %u\n",
683 (pbrms
->dsfield
& PBR_DSFIELD_DSCP
) >> 2);
684 if (pbrms
->dsfield
& PBR_DSFIELD_ECN
)
685 vty_out(vty
, " ECN Match: %u\n",
686 pbrms
->dsfield
& PBR_DSFIELD_ECN
);
688 vty_out(vty
, " MARK Match: %u\n", pbrms
->mark
);
690 if (pbrms
->nhgrp_name
) {
691 vty_out(vty
, " Nexthop-Group: %s\n", pbrms
->nhgrp_name
);
695 " Installed: %u(%d) Tableid: %d\n",
696 pbrms
->nhs_installed
,
697 pbr_nht_get_installed(pbrms
->nhgrp_name
),
698 pbr_nht_get_table(pbrms
->nhgrp_name
));
700 vty_out(vty
, " Installed: %s Tableid: %d\n",
701 pbr_nht_get_installed(pbrms
->nhgrp_name
) ? "yes"
703 pbr_nht_get_table(pbrms
->nhgrp_name
));
705 } else if (pbrms
->nhg
) {
707 pbrms_nexthop_group_write_individual_nexthop(vty
, pbrms
);
710 " Installed: %u(%d) Tableid: %d\n",
711 pbrms
->nhs_installed
,
712 pbr_nht_get_installed(pbrms
->internal_nhg_name
),
713 pbr_nht_get_table(pbrms
->internal_nhg_name
));
715 vty_out(vty
, " Installed: %s Tableid: %d\n",
716 pbr_nht_get_installed(pbrms
->internal_nhg_name
)
719 pbr_nht_get_table(pbrms
->internal_nhg_name
));
721 } else if (pbrms
->vrf_unchanged
) {
722 vty_out(vty
, " VRF Unchanged (use interface vrf)\n");
723 } else if (pbrms
->vrf_lookup
) {
724 vty_out(vty
, " VRF Lookup: %s\n", pbrms
->vrf_name
);
726 vty_out(vty
, " Nexthop-Group: Unknown Installed: no\n");
730 static void vty_json_pbrms(json_object
*j
, struct vty
*vty
,
731 const struct pbr_map_sequence
*pbrms
)
733 json_object
*jpbrm
, *nexthop_group
;
734 char *nhg_name
= pbrms
->nhgrp_name
? pbrms
->nhgrp_name
735 : pbrms
->internal_nhg_name
;
736 char buf
[PREFIX_STRLEN
];
739 jpbrm
= json_object_new_object();
741 json_object_int_add(jpbrm
, "id", pbrms
->unique
);
744 pbr_map_reason_string(pbrms
->reason
, rbuf
, sizeof(rbuf
));
746 json_object_int_add(jpbrm
, "sequenceNumber", pbrms
->seqno
);
747 json_object_int_add(jpbrm
, "ruleNumber", pbrms
->ruleno
);
748 json_object_boolean_add(jpbrm
, "vrfUnchanged", pbrms
->vrf_unchanged
);
749 json_object_boolean_add(jpbrm
, "installed",
750 pbr_nht_get_installed(nhg_name
));
751 json_object_string_add(jpbrm
, "installedReason",
752 pbrms
->reason
? rbuf
: "Valid");
755 nexthop_group
= json_object_new_object();
757 json_object_int_add(nexthop_group
, "tableId",
758 pbr_nht_get_table(nhg_name
));
759 json_object_string_add(nexthop_group
, "name", nhg_name
);
760 json_object_boolean_add(nexthop_group
, "installed",
761 pbr_nht_get_installed(nhg_name
));
762 json_object_int_add(nexthop_group
, "installedInternally",
763 pbrms
->nhs_installed
);
765 json_object_object_add(jpbrm
, "nexthopGroup", nexthop_group
);
768 if (pbrms
->vrf_lookup
)
769 json_object_string_add(jpbrm
, "vrfName", pbrms
->vrf_name
);
772 json_object_string_add(
774 prefix2str(pbrms
->src
, buf
, sizeof(buf
)));
776 json_object_string_add(
778 prefix2str(pbrms
->dst
, buf
, sizeof(buf
)));
780 json_object_int_add(jpbrm
, "matchMark", pbrms
->mark
);
781 if (pbrms
->dsfield
& PBR_DSFIELD_DSCP
)
782 json_object_int_add(jpbrm
, "matchDscp",
783 (pbrms
->dsfield
& PBR_DSFIELD_DSCP
) >> 2);
784 if (pbrms
->dsfield
& PBR_DSFIELD_ECN
)
785 json_object_int_add(jpbrm
, "matchEcn",
786 pbrms
->dsfield
& PBR_DSFIELD_ECN
);
788 json_object_array_add(j
, jpbrm
);
791 static void vty_show_pbr_map(struct vty
*vty
, const struct pbr_map
*pbrm
,
794 struct pbr_map_sequence
*pbrms
;
795 struct listnode
*node
;
797 vty_out(vty
, " pbr-map %s valid: %s\n", pbrm
->name
,
798 pbrm
->valid
? "yes" : "no");
800 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
801 vty_show_pbrms(vty
, pbrms
, detail
);
804 static void vty_json_pbr_map(json_object
*j
, struct vty
*vty
,
805 const struct pbr_map
*pbrm
)
807 struct pbr_map_sequence
*pbrms
;
808 struct listnode
*node
;
811 json_object_string_add(j
, "name", pbrm
->name
);
812 json_object_boolean_add(j
, "valid", pbrm
->valid
);
814 jpbrms
= json_object_new_array();
816 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
817 vty_json_pbrms(jpbrms
, vty
, pbrms
);
819 json_object_object_add(j
, "policies", jpbrms
);
824 "show pbr map [NAME$name] [detail$detail|json$json]",
829 "Detailed information\n"
832 struct pbr_map
*pbrm
;
833 json_object
*j
= NULL
;
836 j
= json_object_new_array();
838 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
) {
839 json_object
*this_map
= NULL
;
840 if (name
&& strcmp(name
, pbrm
->name
) != 0)
844 this_map
= json_object_new_object();
847 vty_json_pbr_map(this_map
, vty
, pbrm
);
849 json_object_array_add(j
, this_map
);
853 vty_show_pbr_map(vty
, pbrm
, detail
);
858 json_object_to_json_string_ext(
859 j
, JSON_C_TO_STRING_PRETTY
));
866 DEFPY(show_pbr_nexthop_group
,
867 show_pbr_nexthop_group_cmd
,
868 "show pbr nexthop-groups [WORD$word] [json$json]",
872 "Optional Name of the nexthop group\n"
875 json_object
*j
= NULL
;
878 j
= json_object_new_array();
881 pbr_nht_json_nexthop_group(j
, word
);
884 json_object_to_json_string_ext(
885 j
, JSON_C_TO_STRING_PRETTY
));
889 pbr_nht_show_nexthop_group(vty
, word
);
895 DEFPY (show_pbr_interface
,
896 show_pbr_interface_cmd
,
897 "show pbr interface [NAME$name] [json$json]",
901 "PBR Interface Name\n"
904 struct interface
*ifp
;
906 struct pbr_interface
*pbr_ifp
;
907 json_object
*j
= NULL
;
910 j
= json_object_new_array();
912 RB_FOREACH(vrf
, vrf_name_head
, &vrfs_by_name
) {
913 FOR_ALL_INTERFACES(vrf
, ifp
) {
914 struct pbr_map
*pbrm
;
915 json_object
*this_iface
= NULL
;
918 this_iface
= json_object_new_object();
921 json_object_free(this_iface
);
925 if (name
&& strcmp(ifp
->name
, name
) != 0) {
926 json_object_free(this_iface
);
932 if (strcmp(pbr_ifp
->mapname
, "") == 0) {
933 json_object_free(this_iface
);
937 pbrm
= pbrm_find(pbr_ifp
->mapname
);
940 json_object_string_add(this_iface
, "name",
942 json_object_int_add(this_iface
, "index",
944 json_object_string_add(this_iface
, "policy",
946 json_object_boolean_add(this_iface
, "valid",
949 json_object_array_add(j
, this_iface
);
953 vty_out(vty
, " %s(%d) with pbr-policy %s", ifp
->name
,
954 ifp
->ifindex
, pbr_ifp
->mapname
);
956 vty_out(vty
, " (map doesn't exist)");
963 json_object_to_json_string_ext(
964 j
, JSON_C_TO_STRING_PRETTY
));
971 /* PBR debugging CLI ------------------------------------------------------- */
973 static struct cmd_node debug_node
= {
977 .config_write
= pbr_debug_config_write
,
982 "[no] debug pbr [{map$map|zebra$zebra|nht$nht|events$events}]",
987 "PBRD <-> Zebra communications\n"
991 uint32_t mode
= DEBUG_NODE2MODE(vty
->node
);
994 DEBUG_MODE_SET(&pbr_dbg_map
, mode
, !no
);
996 DEBUG_MODE_SET(&pbr_dbg_zebra
, mode
, !no
);
998 DEBUG_MODE_SET(&pbr_dbg_nht
, mode
, !no
);
1000 DEBUG_MODE_SET(&pbr_dbg_event
, mode
, !no
);
1002 /* no specific debug --> act on all of them */
1003 if (strmatch(argv
[argc
- 1]->text
, "pbr"))
1004 pbr_debug_set_all(mode
, !no
);
1009 DEFUN_NOSH(show_debugging_pbr
,
1010 show_debugging_pbr_cmd
,
1011 "show debugging [pbr]",
1016 vty_out(vty
, "PBR debugging status:\n");
1018 pbr_debug_config_write_helper(vty
, false);
1023 /* ------------------------------------------------------------------------- */
1026 static int pbr_interface_config_write(struct vty
*vty
);
1027 static struct cmd_node interface_node
= {
1028 .name
= "interface",
1029 .node
= INTERFACE_NODE
,
1030 .parent_node
= CONFIG_NODE
,
1031 .prompt
= "%s(config-if)# ",
1032 .config_write
= pbr_interface_config_write
,
1035 static int pbr_interface_config_write(struct vty
*vty
)
1037 struct interface
*ifp
;
1040 RB_FOREACH (vrf
, vrf_name_head
, &vrfs_by_name
) {
1041 FOR_ALL_INTERFACES (vrf
, ifp
) {
1042 if (vrf
->vrf_id
== VRF_DEFAULT
)
1043 vty_frame(vty
, "interface %s\n", ifp
->name
);
1045 vty_frame(vty
, "interface %s vrf %s\n",
1046 ifp
->name
, vrf
->name
);
1049 vty_out(vty
, " description %s\n", ifp
->desc
);
1051 pbr_map_write_interfaces(vty
, ifp
);
1053 vty_endframe(vty
, "!\n");
1060 static int pbr_vty_map_config_write(struct vty
*vty
);
1061 /* PBR map node structure. */
1062 static struct cmd_node pbr_map_node
= {
1064 .node
= PBRMAP_NODE
,
1065 .parent_node
= CONFIG_NODE
,
1066 .prompt
= "%s(config-pbr-map)# ",
1067 .config_write
= pbr_vty_map_config_write
,
1070 static int pbr_vty_map_config_write_sequence(struct vty
*vty
,
1071 struct pbr_map
*pbrm
,
1072 struct pbr_map_sequence
*pbrms
)
1074 vty_out(vty
, "pbr-map %s seq %u\n", pbrm
->name
, pbrms
->seqno
);
1077 vty_out(vty
, " match src-ip %pFX\n", pbrms
->src
);
1080 vty_out(vty
, " match dst-ip %pFX\n", pbrms
->dst
);
1082 if (pbrms
->dsfield
& PBR_DSFIELD_DSCP
)
1083 vty_out(vty
, " match dscp %u\n",
1084 (pbrms
->dsfield
& PBR_DSFIELD_DSCP
) >> 2);
1086 if (pbrms
->dsfield
& PBR_DSFIELD_ECN
)
1087 vty_out(vty
, " match ecn %u\n",
1088 pbrms
->dsfield
& PBR_DSFIELD_ECN
);
1091 vty_out(vty
, " match mark %u\n", pbrms
->mark
);
1093 if (pbrms
->vrf_unchanged
)
1094 vty_out(vty
, " set vrf unchanged\n");
1096 if (pbrms
->vrf_lookup
)
1097 vty_out(vty
, " set vrf %s\n", pbrms
->vrf_name
);
1099 if (pbrms
->nhgrp_name
)
1100 vty_out(vty
, " set nexthop-group %s\n", pbrms
->nhgrp_name
);
1103 vty_out(vty
, " set ");
1104 pbrms_nexthop_group_write_individual_nexthop(vty
, pbrms
);
1107 vty_out(vty
, "!\n");
1111 static int pbr_vty_map_config_write(struct vty
*vty
)
1113 struct pbr_map
*pbrm
;
1115 pbr_nht_write_table_range(vty
);
1116 pbr_nht_write_rule_range(vty
);
1118 RB_FOREACH(pbrm
, pbr_map_entry_head
, &pbr_maps
) {
1119 struct pbr_map_sequence
*pbrms
;
1120 struct listnode
*node
;
1122 for (ALL_LIST_ELEMENTS_RO(pbrm
->seqnumbers
, node
, pbrms
))
1123 pbr_vty_map_config_write_sequence(vty
, pbrm
, pbrms
);
1129 static void pbr_map_completer(vector comps
, struct cmd_token
*token
)
1131 struct pbr_map
*pbrm
;
1133 RB_FOREACH (pbrm
, pbr_map_entry_head
, &pbr_maps
)
1134 vector_set(comps
, XSTRDUP(MTYPE_COMPLETION
, pbrm
->name
));
1137 static const struct cmd_variable_handler pbr_map_name
[] = {
1139 .tokenname
= "PBRMAP", .completions
= pbr_map_completer
,
1146 void pbr_vty_init(void)
1148 cmd_variable_handler_register(pbr_map_name
);
1150 install_node(&interface_node
);
1153 install_node(&pbr_map_node
);
1156 install_node(&debug_node
);
1157 install_element(ENABLE_NODE
, &debug_pbr_cmd
);
1158 install_element(CONFIG_NODE
, &debug_pbr_cmd
);
1159 install_element(ENABLE_NODE
, &show_debugging_pbr_cmd
);
1161 install_default(PBRMAP_NODE
);
1163 install_element(CONFIG_NODE
, &pbr_map_cmd
);
1164 install_element(CONFIG_NODE
, &no_pbr_map_cmd
);
1165 install_element(CONFIG_NODE
, &pbr_set_table_range_cmd
);
1166 install_element(CONFIG_NODE
, &no_pbr_set_table_range_cmd
);
1167 install_element(INTERFACE_NODE
, &pbr_policy_cmd
);
1168 install_element(PBRMAP_NODE
, &pbr_map_match_src_cmd
);
1169 install_element(PBRMAP_NODE
, &pbr_map_match_dst_cmd
);
1170 install_element(PBRMAP_NODE
, &pbr_map_match_dscp_cmd
);
1171 install_element(PBRMAP_NODE
, &pbr_map_match_ecn_cmd
);
1172 install_element(PBRMAP_NODE
, &pbr_map_match_mark_cmd
);
1173 install_element(PBRMAP_NODE
, &pbr_map_nexthop_group_cmd
);
1174 install_element(PBRMAP_NODE
, &no_pbr_map_nexthop_group_cmd
);
1175 install_element(PBRMAP_NODE
, &pbr_map_nexthop_cmd
);
1176 install_element(PBRMAP_NODE
, &no_pbr_map_nexthop_cmd
);
1177 install_element(PBRMAP_NODE
, &pbr_map_vrf_cmd
);
1178 install_element(PBRMAP_NODE
, &no_pbr_map_vrf_cmd
);
1179 install_element(VIEW_NODE
, &show_pbr_cmd
);
1180 install_element(VIEW_NODE
, &show_pbr_map_cmd
);
1181 install_element(VIEW_NODE
, &show_pbr_interface_cmd
);
1182 install_element(VIEW_NODE
, &show_pbr_nexthop_group_cmd
);