forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpbr_vty.c
2243 lines (1875 loc) · 57.9 KB
/
pbr_vty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* PBR - vty code
* Copyright (C) 2018 Cumulus Networks, Inc.
* Donald Sharp
* Portions:
* Copyright (c) 2021 The MITRE Corporation.
* Copyright (c) 2023 LabN Consulting, L.L.C.
*/
#include <zebra.h>
#include "vty.h"
#include "command.h"
#include "prefix.h"
#include "vrf.h"
#include "nexthop.h"
#include "nexthop_group.h"
#include "nexthop_group_private.h"
#include "log.h"
#include "json.h"
#include "debug.h"
#include "pbr.h"
#include "pbrd/pbr_nht.h"
#include "pbrd/pbr_map.h"
#include "pbrd/pbr_zebra.h"
#include "pbrd/pbr_vty.h"
#include "pbrd/pbr_debug.h"
#include "pbrd/pbr_vty_clippy.c"
/* clang-format off */
DEFPY (pbr_set_table_range,
pbr_set_table_range_cmd,
"pbr table range (10000-4294966272)$lb (10000-4294966272)$ub",
PBR_STR
"Set table ID range\n"
"Set table ID range\n"
"Lower bound for table ID range\n"
"Upper bound for table ID range\n")
{
/* clang-format on */
/* upper bound is 2^32 - 2^10 */
int ret = CMD_WARNING;
const int minrange = 1000;
/* validate given bounds */
if (lb > ub)
vty_out(vty, "%% Lower bound must be less than upper bound\n");
else if (ub - lb < minrange)
vty_out(vty, "%% Range breadth must be at least %d\n", minrange);
else {
ret = CMD_SUCCESS;
pbr_nht_set_tableid_range((uint32_t)lb, (uint32_t)ub);
}
return ret;
}
/* clang-format off */
DEFPY (no_pbr_set_table_range,
no_pbr_set_table_range_cmd,
"no pbr table range [(10000-4294966272)$lb (10000-4294966272)$ub]",
NO_STR
PBR_STR
"Set table ID range\n"
"Set table ID range\n"
"Lower bound for table ID range\n"
"Upper bound for table ID range\n")
{
/* clang-format on */
pbr_nht_set_tableid_range(PBR_NHT_DEFAULT_LOW_TABLEID,
PBR_NHT_DEFAULT_HIGH_TABLEID);
return CMD_SUCCESS;
}
/* clang-format off */
DEFUN_NOSH(pbr_map,
pbr_map_cmd,
"pbr-map PBRMAP seq (1-700)",
"Create pbr-map or enter pbr-map command mode\n"
"The name of the PBR MAP\n"
"Sequence to insert in existing pbr-map entry\n"
"Sequence number\n")
{
/* clang-format on */
const char *pbrm_name = argv[1]->arg;
uint32_t seqno = atoi(argv[3]->arg);
struct pbr_map_sequence *pbrms;
pbrms = pbrms_get(pbrm_name, seqno);
VTY_PUSH_CONTEXT(PBRMAP_NODE, pbrms);
return CMD_SUCCESS;
}
/* clang-format off */
DEFUN_NOSH(no_pbr_map,
no_pbr_map_cmd,
"no pbr-map PBRMAP [seq (1-700)]",
NO_STR
"Delete pbr-map\n"
"The name of the PBR MAP\n"
"Sequence to delete from existing pbr-map entry\n"
"Sequence number\n")
{
/* clang-format on */
const char *pbrm_name = argv[2]->arg;
uint32_t seqno = 0;
struct pbr_map *pbrm = pbrm_find(pbrm_name);
struct pbr_map_sequence *pbrms;
struct listnode *node, *next_node;
if (argc > 3)
seqno = atoi(argv[4]->arg);
if (!pbrm) {
vty_out(vty, "pbr-map %s not found\n", pbrm_name);
return CMD_SUCCESS;
}
for (ALL_LIST_ELEMENTS(pbrm->seqnumbers, node, next_node, pbrms)) {
if (seqno && pbrms->seqno != seqno)
continue;
pbr_map_delete(pbrms);
pbr_map_sequence_delete(pbrms);
}
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Match L3 Fields
***********************************************************************/
/*
* Address Family Matters
*
* Linux Kernel constraints
* ------------------------
* The underlying linux kernel dataplane requires that rules be
* installed into an IPv4-specific or an IPv6-specific database.
*
* Not only do we need to designate an address-family for rule
* installation, but we ALSO must have the same address-family
* available to be able to delete the rule from the correct kernel
* database.
*
* Determining the address-family
* ------------------------------
* In the current code, we do our best to infer the correct family
* from any configured IP-address match or set clauses in a rule.
* Absent any of those fields, the NHT code also tries to glean the
* address family from resolved nexthops or nexthop-groups. All of
* those opportunistic address-family determinations are stored in
* the "family" field of struct pbr_map_sequence.
*
* This "family" field value is needed particularly when deleting
* a rule piece-by-piece because at the end, the match/set fields
* will be empty. Maybe it would be possible to handle this issue
* as an internal zebra matter in the future.
*
* We also attempt to maintain address-family consistency among the
* various configured fields in a rule. So far, these fields are
* src/dst IP-address match/set values.
*
* It is probably possible to perform the same address-family check in
* the CLI for single nexthops (set nexthop A.B.C.D|X:X::X:X) but the
* address-family is not immediately available for nexthop-groups.
* In both the single-nexthop and nexthop-group, the NHT resolution code
* sets the "family" field of struct pbr_map_sequence asynchronously.
*
* There isn't currently any flagging of rules that have a consistent
* set of src/dst IP-address match/set values but an asynchronously-resolved
* nexthop-group that has a different address-family.
*
* The match/set IP-address handlers below blindly set "family"; it's
* probably possible to wrongly set "family" to, e.g., IPv4 this way after
* a v6 NHG has been resolved and break rule removal. It's not clear
* how to best address this potential issue.
*/
static bool pbr_family_consistent(struct pbr_map_sequence *pbrms,
uint8_t family, uint32_t skip_filter_bm,
uint32_t skip_action_bm, const char **msg)
{
uint32_t filter_bm = pbrms->filter_bm & ~skip_filter_bm;
uint32_t action_bm = pbrms->action_bm & ~skip_action_bm;
if (CHECK_FLAG(filter_bm, PBR_FILTER_SRC_IP) &&
(family != pbrms->src->family)) {
if (msg)
*msg = "match src-ip";
return false;
}
if (CHECK_FLAG(filter_bm, PBR_FILTER_DST_IP) &&
(family != pbrms->dst->family)) {
if (msg)
*msg = "match dst-ip";
return false;
}
if (CHECK_FLAG(action_bm, PBR_ACTION_SRC_IP) &&
(family != sockunion_family(&pbrms->action_src))) {
if (msg)
*msg = "set src-ip";
return false;
}
if (CHECK_FLAG(filter_bm, PBR_ACTION_DST_IP) &&
(family != sockunion_family(&pbrms->action_dst))) {
if (msg)
*msg = "set dst-ip";
return false;
}
return true;
}
/* clang-format off */
DEFPY (pbr_map_match_src,
pbr_map_match_src_cmd,
"[no] match src-ip ![<A.B.C.D/M|X:X::X:X/M>$prefix]",
NO_STR
"Match the rest of the command\n"
"Choose the src ipv4 or ipv6 prefix to use\n"
"v4 Prefix\n"
"v6 Prefix\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
const char *fmsg = NULL;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_IP))
return CMD_SUCCESS;
prefix_free(&pbrms->src);
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_IP);
goto check;
}
assert(prefix);
if (!pbr_family_consistent(pbrms, prefix->family, PBR_FILTER_SRC_IP, 0,
&fmsg)) {
vty_out(vty, "Address family mismatch (%s)\n", fmsg);
return CMD_WARNING_CONFIG_FAILED;
}
pbrms->family = prefix->family;
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_IP)) {
if (prefix_same(pbrms->src, prefix))
return CMD_SUCCESS;
} else
pbrms->src = prefix_new();
prefix_copy(pbrms->src, prefix);
SET_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_IP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_dst,
pbr_map_match_dst_cmd,
"[no] match dst-ip ![<A.B.C.D/M|X:X::X:X/M>$prefix]",
NO_STR
"Match the rest of the command\n"
"Choose the dst ipv4 or ipv6 prefix to use\n"
"v4 Prefix\n"
"v6 Prefix\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
const char *fmsg = NULL;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DST_IP))
return CMD_SUCCESS;
prefix_free(&pbrms->dst);
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_DST_IP);
goto check;
}
assert(prefix);
if (!pbr_family_consistent(pbrms, prefix->family, PBR_FILTER_DST_IP, 0,
&fmsg)) {
vty_out(vty, "Address family mismatch (%s)\n", fmsg);
return CMD_WARNING_CONFIG_FAILED;
}
pbrms->family = prefix->family;
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DST_IP)) {
if (prefix_same(pbrms->dst, prefix))
return CMD_SUCCESS;
} else
pbrms->dst = prefix_new();
prefix_copy(pbrms->dst, prefix);
SET_FLAG(pbrms->filter_bm, PBR_FILTER_DST_IP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_ip_proto,
pbr_map_match_ip_proto_cmd,
"[no] match ip-protocol ![PROTO$ip_proto]",
NO_STR
"Match the rest of the command\n"
"Choose an ip-protocol\n"
"Protocol name\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
struct protoent *p = NULL;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_IP_PROTOCOL))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_IP_PROTOCOL);
goto check;
}
if (ip_proto)
p = getprotobyname(ip_proto);
if (!ip_proto || !p) {
vty_out(vty, "Unable to convert %s to proto id\n",
(ip_proto ? ip_proto : "(null)"));
return CMD_WARNING_CONFIG_FAILED;
}
pbrms->ip_proto = p->p_proto;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_IP_PROTOCOL);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_src_port,
pbr_map_match_src_port_cmd,
"[no] match src-port ![(1-65535)$port]",
NO_STR
"Match the rest of the command\n"
"Choose the source port to use\n"
"The Source Port\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_PORT))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_PORT);
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_PORT) &&
(pbrms->src_prt == port)) {
return CMD_SUCCESS;
}
pbrms->src_prt = port;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_SRC_PORT);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_dst_port,
pbr_map_match_dst_port_cmd,
"[no] match dst-port ![(1-65535)$port]",
NO_STR
"Match the rest of the command\n"
"Choose the destination port to use\n"
"The Destination Port\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DST_PORT))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_DST_PORT);
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DST_PORT) &&
(pbrms->dst_prt == port)) {
return CMD_SUCCESS;
}
pbrms->dst_prt = port;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_DST_PORT);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_dscp,
pbr_map_match_dscp_cmd,
"[no] match dscp ![DSCP$dscp]",
NO_STR
"Match the rest of the command\n"
"Match based on IP DSCP field\n"
"DSCP value (below 64) or standard codepoint name\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DSCP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_DSCP);
pbrms->dsfield &= ~PBR_DSFIELD_DSCP;
goto check;
}
unsigned long ul_dscp;
char *pend = NULL;
uint8_t shifted_dscp;
assert(dscp);
ul_dscp = strtoul(dscp, &pend, 0);
if (pend && *pend)
ul_dscp = pbr_map_decode_dscp_enum(dscp);
if (ul_dscp > (PBR_DSFIELD_DSCP >> 2)) {
vty_out(vty, "Invalid dscp value: %s%s\n", dscp,
((pend && *pend) ? "" : " (numeric value must be in range 0-63)"));
return CMD_WARNING_CONFIG_FAILED;
}
shifted_dscp = (ul_dscp << 2) & PBR_DSFIELD_DSCP;
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_DSCP) &&
((pbrms->dsfield & PBR_DSFIELD_DSCP) == shifted_dscp)) {
return CMD_SUCCESS;
}
/* Set the DSCP bits of the DSField */
pbrms->dsfield = (pbrms->dsfield & ~PBR_DSFIELD_DSCP) | shifted_dscp;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_DSCP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_ecn,
pbr_map_match_ecn_cmd,
"[no] match ecn ![(0-3)$ecn]",
NO_STR
"Match the rest of the command\n"
"Match based on IP ECN field\n"
"Explicit Congestion Notification\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_ECN))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_ECN);
pbrms->dsfield &= ~PBR_DSFIELD_ECN;
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_ECN) &&
((pbrms->dsfield & PBR_DSFIELD_ECN) == ecn)) {
return CMD_SUCCESS;
}
/* Set the ECN bits of the DSField */
pbrms->dsfield = (pbrms->dsfield & ~PBR_DSFIELD_ECN) | ecn;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_ECN);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Match L2 fields
***********************************************************************/
/* clang-format off */
DEFPY (pbr_map_match_pcp,
pbr_map_match_pcp_cmd,
"[no] match pcp ![(0-7)$pcp]",
NO_STR
"Match spec follows\n"
"Match based on 802.1p Priority Code Point (PCP) value\n"
"PCP value to match\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_PCP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_PCP);
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_PCP) &&
(pbrms->match_pcp == pcp)) {
return CMD_SUCCESS;
}
pbrms->match_pcp = pcp;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_PCP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_vlan_id,
pbr_map_match_vlan_id_cmd,
"[no] match vlan ![(1-4094)$vlan_id]",
NO_STR
"Match spec follows\n"
"Match based on VLAN ID\n"
"VLAN ID to match\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_ID))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_ID);
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_ID) &&
(pbrms->match_vlan_id == vlan_id)) {
return CMD_SUCCESS;
}
/*
* Maintaining previous behavior: setting a vlan_id match
* automatically clears any vlan_flags matching.
*/
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_FLAGS);
SET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_ID);
pbrms->match_vlan_id = vlan_id;
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_match_vlan_tag,
pbr_map_match_vlan_tag_cmd,
"[no] match vlan ![<tagged|untagged|untagged-or-zero>$tag_type]",
NO_STR
"Match the rest of the command\n"
"Match based on VLAN tagging\n"
"Match all tagged frames\n"
"Match all untagged frames\n"
"Match untagged frames, or tagged frames with id zero\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
uint16_t vlan_flags;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_FLAGS))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_FLAGS);
goto check;
}
assert(tag_type);
if (strmatch(tag_type, "tagged"))
vlan_flags = PBR_VLAN_FLAGS_TAGGED;
else if (strmatch(tag_type, "untagged"))
vlan_flags = PBR_VLAN_FLAGS_UNTAGGED;
else if (strmatch(tag_type, "untagged-or-zero"))
vlan_flags = PBR_VLAN_FLAGS_UNTAGGED_0;
else {
vty_out(vty, "unknown vlan flag\n");
return CMD_WARNING_CONFIG_FAILED;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_FLAGS) &&
(pbrms->match_vlan_flags == vlan_flags)) {
return CMD_SUCCESS;
}
/*
* Maintaining previous behavior: setting a vlan_flags match
* automatically clears any vlan_id matching.
*/
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_ID);
SET_FLAG(pbrms->filter_bm, PBR_FILTER_VLAN_FLAGS);
pbrms->match_vlan_flags = vlan_flags;
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Match meta
***********************************************************************/
/* clang-format off */
DEFPY (pbr_map_match_mark,
pbr_map_match_mark_cmd,
"[no] match mark ![(1-4294967295)$mark]",
NO_STR
"Match the rest of the command\n"
"Choose the mark value to use\n"
"mark\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
#ifndef GNU_LINUX
vty_out(vty, "pbr marks are not supported on this platform\n");
return CMD_WARNING_CONFIG_FAILED;
#endif
if (no) {
if (!CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_FWMARK))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->filter_bm, PBR_FILTER_FWMARK);
goto check;
}
if (CHECK_FLAG(pbrms->filter_bm, PBR_FILTER_FWMARK) &&
(pbrms->mark == (uint32_t)mark)) {
return CMD_SUCCESS;
}
pbrms->mark = (uint32_t)mark;
SET_FLAG(pbrms->filter_bm, PBR_FILTER_FWMARK);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Action Set L3 Fields
***********************************************************************/
/* clang-format off */
DEFPY (pbr_map_action_src,
pbr_map_action_src_cmd,
"[no] set src-ip ![<A.B.C.D|X:X::X:X>$su]",
NO_STR
"Set command\n"
"Set the src ipv4 or ipv6 prefix\n"
"v4 Prefix\n"
"v6 Prefix\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
const char *fmsg = NULL;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_SRC_IP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_SRC_IP);
goto check;
}
assert(su);
if (!pbr_family_consistent(pbrms, sockunion_family(su),
PBR_ACTION_SRC_IP, 0, &fmsg)) {
vty_out(vty, "Address family mismatch (%s)\n", fmsg);
return CMD_WARNING_CONFIG_FAILED;
}
pbrms->family = sockunion_family(su);
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_SRC_IP) &&
(sockunion_same(&pbrms->action_src, su))) {
return CMD_SUCCESS;
}
pbrms->action_src = *su;
SET_FLAG(pbrms->action_bm, PBR_ACTION_SRC_IP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_action_dst,
pbr_map_action_dst_cmd,
"[no] set dst-ip ![<A.B.C.D|X:X::X:X>$su]",
NO_STR
"Set command\n"
"Set the dst ipv4 or ipv6 prefix\n"
"v4 Prefix\n"
"v6 Prefix\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
const char *fmsg = NULL;
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DST_IP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_DST_IP);
goto check;
}
assert(su);
if (!pbr_family_consistent(pbrms, sockunion_family(su),
PBR_ACTION_DST_IP, 0, &fmsg)) {
vty_out(vty, "Address family mismatch (%s)\n", fmsg);
return CMD_WARNING_CONFIG_FAILED;
}
pbrms->family = sockunion_family(su);
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DST_IP) &&
(sockunion_same(&pbrms->action_dst, su))) {
return CMD_SUCCESS;
}
pbrms->action_dst = *su;
SET_FLAG(pbrms->action_bm, PBR_ACTION_DST_IP);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_action_src_port,
pbr_map_action_src_port_cmd,
"[no] set src-port ![(1-65535)$port]",
NO_STR
"Set command\n"
"Set Source Port\n"
"The Source Port\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_SRC_PORT))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_SRC_PORT);
goto check;
}
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_SRC_PORT) &&
(pbrms->action_src_port == port))
return CMD_SUCCESS;
pbrms->action_src_port = port;
SET_FLAG(pbrms->action_bm, PBR_ACTION_SRC_PORT);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_action_dst_port,
pbr_map_action_dst_port_cmd,
"[no] set dst-port ![(1-65535)$port]",
NO_STR
"Set command\n"
"Set Destination Port\n"
"The Destination Port\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DST_PORT))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_DST_PORT);
goto check;
}
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DST_PORT) &&
(pbrms->action_dst_port == port))
return CMD_SUCCESS;
SET_FLAG(pbrms->action_bm, PBR_ACTION_DST_PORT);
pbrms->action_dst_port = port;
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_action_dscp,
pbr_map_action_dscp_cmd,
"[no] set dscp ![DSCP$dscp]",
NO_STR
"Set command\n"
"Set IP DSCP field\n"
"DSCP numeric value (0-63) or standard codepoint name\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DSCP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_DSCP);
goto check;
}
unsigned long ul_dscp;
char *pend = NULL;
uint8_t shifted_dscp;
assert(dscp);
ul_dscp = strtoul(dscp, &pend, 0);
if (pend && *pend)
ul_dscp = pbr_map_decode_dscp_enum(dscp);
if (ul_dscp > (PBR_DSFIELD_DSCP >> 2)) {
vty_out(vty, "Invalid dscp value: %s%s\n", dscp,
((pend && *pend) ? "" : " (numeric value must be in range 0-63)"));
return CMD_WARNING_CONFIG_FAILED;
}
shifted_dscp = (ul_dscp << 2) & PBR_DSFIELD_DSCP;
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_DSCP) &&
(pbrms->action_dscp == shifted_dscp)) {
return CMD_SUCCESS;
}
SET_FLAG(pbrms->action_bm, PBR_ACTION_DSCP);
pbrms->action_dscp = shifted_dscp;
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/* clang-format off */
DEFPY (pbr_map_action_ecn,
pbr_map_action_ecn_cmd,
"[no] set ecn ![(0-3)$ecn]",
NO_STR
"Set command\n"
"Set IP ECN field\n"
"Explicit Congestion Notification value\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_ECN))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_ECN);
goto check;
}
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_ECN) &&
(pbrms->action_ecn == ecn)) {
return CMD_SUCCESS;
}
SET_FLAG(pbrms->action_bm, PBR_ACTION_ECN);
pbrms->action_ecn = ecn;
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Action Set Meta
***********************************************************************/
/* clang-format off */
DEFPY (pbr_map_action_queue_id,
pbr_map_action_queue_id_cmd,
"[no] set queue-id ![(1-65535)$queue_id]",
NO_STR
"Set the rest of the command\n"
"Set based on egress port queue id\n"
"A valid value in range 1..65535 \n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_QUEUE_ID))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_QUEUE_ID);
goto check;
}
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_QUEUE_ID) &&
(pbrms->action_queue_id == (uint32_t)queue_id)) {
return CMD_SUCCESS;
}
pbrms->action_queue_id = (uint32_t)queue_id;
SET_FLAG(pbrms->action_bm, PBR_ACTION_QUEUE_ID);
check:
pbr_map_check(pbrms, true);
return CMD_SUCCESS;
}
/***********************************************************************
* pbrms/rule Action Set L2 Fields
***********************************************************************/
/* clang-format off */
DEFPY (pbr_map_action_pcp,
pbr_map_action_pcp_cmd,
"[no] set pcp ![(0-7)$pcp]",
NO_STR
"Set the rest of the command\n"
"Set based on 802.1p Priority Code Point (PCP) value\n"
"A valid value in range 0..7\n")
{
/* clang-format on */
struct pbr_map_sequence *pbrms = VTY_GET_CONTEXT(pbr_map_sequence);
if (!pbrms)
return CMD_WARNING_CONFIG_FAILED;
if (no) {
if (!CHECK_FLAG(pbrms->action_bm, PBR_ACTION_PCP))
return CMD_SUCCESS;
UNSET_FLAG(pbrms->action_bm, PBR_ACTION_PCP);
goto check;
}
if (CHECK_FLAG(pbrms->action_bm, PBR_ACTION_PCP) &&