Skip to content

Commit 150dc56

Browse files
author
Capirca Team
committed
Introduce 3 new fields in Capirca Arista TP generator to enable traffic policing actions:
- police_kbps generates `police rate <kbps> kbps` action - police_burst generates `police rate <kbps> kbps burst-size <bytes>` option, which specifies burst size in bytes. Not supported without police_kbps. - police_pps generates `police rate <pps> pps` action. PiperOrigin-RevId: 740067310
1 parent 5a9b940 commit 150dc56

4 files changed

Lines changed: 278 additions & 14 deletions

File tree

capirca/lib/arista_tp.py

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,16 @@ def __str__(self):
372372
current_action = self._ACTIONS.get(self.term.action[0])
373373
# non-permit/drop actions should be added here
374374
has_extra_actions = (
375-
self.term.logging or
376-
self.term.counter or
377-
self.term.dscp_set or
378-
self.term.traffic_class or
379-
self.term.next_hop_group or
380-
self.term.next_interface
381-
)
375+
self.term.logging
376+
or self.term.counter
377+
or self.term.dscp_set
378+
or self.term.traffic_class
379+
or self.term.police_kbps
380+
or self.term.police_burst
381+
or self.term.police_pps
382+
or self.term.next_hop_group
383+
or self.term.next_interface
384+
)
382385

383386
# if !accept - generate an action statement
384387
# if accept and there are extra actions generate an actions statement
@@ -417,6 +420,46 @@ def __str__(self):
417420
f"set traffic class {self.term.traffic_class}",
418421
False,
419422
])
423+
if self.term.police_kbps and self.term.police_pps:
424+
logging.warning(
425+
"WARNING: term %s uses police_pps and police_kbps."
426+
"Only one of these options can be used.",
427+
self.term.name,
428+
)
429+
elif self.term.police_burst and self.term.police_pps:
430+
logging.warning(
431+
"WARNING: term %s uses police_burst, "
432+
"which is not supported with police_pps.",
433+
self.term.name,
434+
)
435+
elif self.term.police_burst and not self.term.police_kbps:
436+
logging.warning(
437+
"WARNING: term %s uses police_burst option but not police_kbps. "
438+
"police_burst will not be added.",
439+
self.term.name,
440+
)
441+
elif self.term.police_kbps and self.term.police_burst:
442+
term_block.append([
443+
ACTION_INDENT,
444+
(
445+
f"police rate {self.term.police_kbps} kbps burst-size"
446+
f" {self.term.police_burst}"
447+
),
448+
False,
449+
])
450+
elif self.term.police_kbps:
451+
term_block.append([
452+
ACTION_INDENT,
453+
f"police rate {self.term.police_kbps} kbps",
454+
False,
455+
])
456+
elif self.term.police_pps:
457+
term_block.append([
458+
ACTION_INDENT,
459+
f"police rate {self.term.police_pps} pps",
460+
False,
461+
])
462+
420463

421464
if self.term.next_hop_group:
422465
term_block.append([
@@ -718,13 +761,41 @@ def _BuildTokens(self):
718761
supported_tokens, supported_sub_tokens = super()._BuildTokens()
719762

720763
supported_tokens |= {
721-
"action", "comment", "counter", "destination_address",
722-
"destination_address_exclude", "destination_port", "destination_prefix",
723-
"dscp_set", "expiration", "fragment_offset", "hop_limit", "icmp_code",
724-
"icmp_type", "logging", "name", "option", "owner", "packet_length",
725-
"platform", "platform_exclude", "port", "protocol", "protocol_except",
726-
"source_address", "source_address_exclude", "source_port", "source_prefix",
727-
"traffic_class", "ttl", "verbatim", "next_hop_group", "next_interface"
764+
"action",
765+
"comment",
766+
"counter",
767+
"destination_address",
768+
"destination_address_exclude",
769+
"destination_port",
770+
"destination_prefix",
771+
"dscp_set",
772+
"expiration",
773+
"fragment_offset",
774+
"hop_limit",
775+
"icmp_code",
776+
"icmp_type",
777+
"logging",
778+
"name",
779+
"option",
780+
"owner",
781+
"packet_length",
782+
"platform",
783+
"platform_exclude",
784+
"port",
785+
"protocol",
786+
"protocol_except",
787+
"police_burst",
788+
"police_kbps",
789+
"police_pps",
790+
"source_address",
791+
"source_address_exclude",
792+
"source_port",
793+
"source_prefix",
794+
"traffic_class",
795+
"ttl",
796+
"verbatim",
797+
"next_hop_group",
798+
"next_interface"
728799
}
729800
supported_sub_tokens.update({
730801
"option": {

capirca/lib/policy.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ class Term:
366366
pan-application: VarType.PAN_APPLICATION
367367
policer: VarType.POLICER
368368
priority: VarType.PRIORITY
369+
police_kbps: VarType.POLICE_KBPS
370+
police_burst: VarType.POLICE_BURST
371+
police_pps: VarType.POLICE_PPS
369372
destination-zone: VarType.DZONE
370373
source-service-accounts: VarType.SOURCE_SERVICE_ACCOUNTS
371374
target-service-accounts: VarType.TARGET_SERVICE_ACCOUNTS
@@ -466,6 +469,9 @@ def __init__(self, obj):
466469
self.owner = None
467470
self.policer = None
468471
self.port = []
472+
self.police_kbps = None
473+
self.police_burst = None
474+
self.police_pps = None
469475
self.precedence = []
470476
self.protocol = []
471477
self.protocol_except = []
@@ -870,6 +876,12 @@ def __str__(self):
870876
ret_str.append(' log_name: %s' % self.log_name)
871877
if self.priority:
872878
ret_str.append(' priority: %s' % self.priority)
879+
if self.police_kbps:
880+
ret_str.append(' police_kbps: %s' % self.police_kbps)
881+
if self.police_burst:
882+
ret_str.append(' police_burst: %s' % self.police_burst)
883+
if self.police_pps:
884+
ret_str.append(' police_pps: %s' % self.police_pps)
873885
if self.counter:
874886
ret_str.append(' counter: %s' % self.counter)
875887
if self.traffic_class_count:
@@ -978,6 +990,18 @@ def __eq__(self, other):
978990
if self.policer != other.policer:
979991
return False
980992

993+
# police_kbps
994+
if self.police_kbps != other.police_kbps:
995+
return False
996+
997+
# police_burst
998+
if self.police_burst != other.police_burst:
999+
return False
1000+
1001+
# police_pps
1002+
if self.police_pps != other.police_pps:
1003+
return False
1004+
9811005
# interface
9821006
if self.source_interface != other.source_interface:
9831007
return False
@@ -1372,6 +1396,12 @@ def AddObject(self, obj):
13721396
# police man, tryin'a take you jail
13731397
elif obj.var_type is VarType.POLICER:
13741398
self.policer = obj.value
1399+
elif obj.var_type is VarType.POLICE_KBPS:
1400+
self.police_kbps = int(obj.value)
1401+
elif obj.var_type is VarType.POLICE_BURST:
1402+
self.police_burst = int(obj.value)
1403+
elif obj.var_type is VarType.POLICE_PPS:
1404+
self.police_pps = int(obj.value)
13751405
elif obj.var_type is VarType.PRIORITY:
13761406
self.priority = obj.value
13771407
# qos?
@@ -1771,6 +1801,9 @@ class VarType:
17711801
TRAFFIC_CLASS = 70
17721802
NEXT_HOP_GROUP = 71
17731803
NEXT_INTERFACE = 72
1804+
POLICE_KBPS = 73
1805+
POLICE_BURST = 74
1806+
POLICE_PPS = 75
17741807

17751808
def __init__(self, var_type, value):
17761809
self.var_type = var_type
@@ -1980,6 +2013,9 @@ def __ne__(self, other):
19802013
'PLATFORM',
19812014
'PLATFORMEXCLUDE',
19822015
'POLICER',
2016+
'POLICE_KBPS',
2017+
'POLICE_BURST',
2018+
'POLICE_PPS',
19832019
'PORT',
19842020
'PORT_MIRROR',
19852021
'PRECEDENCE',
@@ -2069,6 +2105,9 @@ def __ne__(self, other):
20692105
'platform': 'PLATFORM',
20702106
'platform-exclude': 'PLATFORMEXCLUDE',
20712107
'policer': 'POLICER',
2108+
'police-kbps': 'POLICE_KBPS',
2109+
'police-burst': 'POLICE_BURST',
2110+
'police-pps': 'POLICE_PPS',
20722111
'port': 'PORT',
20732112
'port-mirror': 'PORT_MIRROR',
20742113
'precedence': 'PRECEDENCE',
@@ -2264,6 +2303,9 @@ def p_term_spec(p):
22642303
| term_spec platform_spec
22652304
| term_spec policer_spec
22662305
| term_spec port_spec
2306+
| term_spec police_kbps_spec
2307+
| term_spec police_burst_spec
2308+
| term_spec police_pps_spec
22672309
| term_spec port_mirror_spec
22682310
| term_spec precedence_spec
22692311
| term_spec priority_spec
@@ -2620,6 +2662,21 @@ def p_policer_spec(p):
26202662
p[0] = VarType(VarType.POLICER, p[4])
26212663

26222664

2665+
def p_police_kbps_spec(p):
2666+
"""police_kbps_spec : POLICE_KBPS ':' ':' INTEGER"""
2667+
p[0] = VarType(VarType.POLICE_KBPS, p[4])
2668+
2669+
2670+
def p_police_burst_spec(p):
2671+
"""police_burst_spec : POLICE_BURST ':' ':' INTEGER"""
2672+
p[0] = VarType(VarType.POLICE_BURST, p[4])
2673+
2674+
2675+
def p_police_pps_spec(p):
2676+
"""police_pps_spec : POLICE_PPS ':' ':' INTEGER"""
2677+
p[0] = VarType(VarType.POLICE_PPS, p[4])
2678+
2679+
26232680
def p_logging_spec(p):
26242681
"""logging_spec : LOGGING ':' ':' STRING"""
26252682
p[0] = VarType(VarType.LOGGING, p[4])

capirca/lib/policy_simple.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ class Policer(Field):
256256
"""A rate-limit-icmp field."""
257257

258258

259+
class PoliceKbps(Field):
260+
"""A police-kbps field."""
261+
262+
263+
class PoliceBurst(Field):
264+
"""A police-burst field."""
265+
266+
267+
class PolicePps(Field):
268+
"""A police-pps field."""
269+
270+
259271
class PortMirror(Field):
260272
"""A port-mirror field."""
261273

@@ -376,6 +388,9 @@ class Vpn(Field):
376388
'platform': Platform,
377389
'platform-exclude': PlatformExclude,
378390
'policer': Policer,
391+
'police-kbps': PoliceKbps,
392+
'police-burst': PoliceBurst,
393+
'police-pps': PolicePps,
379394
'port': Port,
380395
'port-mirror': PortMirror,
381396
'precedence': Precedence,

0 commit comments

Comments
 (0)