Skip to content

Commit 5933c53

Browse files
author
Capirca Team
committed
Update Arista Traffic policy to set DSCP bits in the IP header along with a validation check to make sure it falls within the 6 bit range.
PiperOrigin-RevId: 721012361
1 parent 50da2e1 commit 5933c53

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

capirca/lib/arista_tp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ def __str__(self):
400400
[ACTION_INDENT,
401401
"count %s" % self.term.counter, False])
402402

403+
# set dscp bits if any.
404+
if self.term.dscp_set and self._is_valid_dscp_value(self.term.dscp_set):
405+
term_block.append(
406+
[ACTION_INDENT, f"set dscp {self.term.dscp_set}", False]
407+
)
408+
403409
term_block.append([MATCH_INDENT, "!", False]) # end of actions
404410
term_block.append([TERM_INDENT, "!", False]) # end of match entry
405411

@@ -408,6 +414,11 @@ def __str__(self):
408414

409415
return str(config)
410416

417+
def _is_valid_dscp_value(self, dscp):
418+
if dscp.isdigit() and 0 <= int(dscp) <= 63:
419+
return True
420+
raise ValueError(f"Invalid DSCP value: {dscp}. Valid range is 0-63.")
421+
411422
def _reflowComments(self, comments, max_length):
412423
"""reflows capirca comments to stay within max_length.
413424

tests/lib/arista_tp_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@
262262
action:: accept
263263
}
264264
"""
265+
GOOD_TERM_39 = """
266+
term good_term_39 {
267+
protocol:: tcp
268+
destination-port:: http
269+
action:: accept
270+
dscp-set:: 32
271+
}
272+
"""
265273
GOOD_TERM_COMMENT = """
266274
term good-term-comment {
267275
protocol:: udp
@@ -313,6 +321,14 @@
313321
action:: accept
314322
}
315323
"""
324+
BAD_DSCP_TERM_1 = """
325+
term bad_dscp_term_1 {
326+
protocol:: tcp
327+
destination-port:: http
328+
action:: accept
329+
dscp-set:: ef
330+
}
331+
"""
316332
DEFAULT_TERM_1 = """
317333
term default-term-1 {
318334
action:: deny
@@ -1290,6 +1306,41 @@ def testAnyMixed(self):
12901306
self.assertIn("match ipv6-ANY_MIXED ipv6", output, output)
12911307
self.assertIn("destination prefix 2001:4860:4860::8844/128", output, output)
12921308

1309+
def testDscpSet(self):
1310+
self.naming.GetNetAddr.side_effect = [[
1311+
nacaddr.IP("8.8.4.4"),
1312+
nacaddr.IP("8.8.8.8"),
1313+
nacaddr.IP("2001:4860:4860::8844"),
1314+
nacaddr.IP("2001:4860:4860::8888"),
1315+
]]
1316+
self.naming.GetServiceByProto.return_value = ["80"]
1317+
atp = arista_tp.AristaTrafficPolicy(
1318+
policy.ParsePolicy(GOOD_FIELD_SET_HEADER + GOOD_TERM_39, self.naming),
1319+
EXP_INFO,
1320+
)
1321+
output = str(atp)
1322+
self.assertIn("set dscp 32", output)
1323+
1324+
def testBadDscpSet(self):
1325+
self.naming.GetNetAddr.side_effect = [[
1326+
nacaddr.IP("8.8.4.4"),
1327+
nacaddr.IP("8.8.8.8"),
1328+
nacaddr.IP("2001:4860:4860::8844"),
1329+
nacaddr.IP("2001:4860:4860::8888"),
1330+
]]
1331+
self.naming.GetServiceByProto.return_value = ["80"]
1332+
atp = arista_tp.AristaTrafficPolicy(
1333+
policy.ParsePolicy(
1334+
GOOD_FIELD_SET_HEADER + BAD_DSCP_TERM_1, self.naming
1335+
),
1336+
EXP_INFO,
1337+
)
1338+
with self.assertRaises(ValueError) as msg:
1339+
str(atp)
1340+
self.assertEqual(
1341+
str(msg.exception), "Invalid DSCP value: ef. Valid range is 0-63."
1342+
)
1343+
12931344
def testInetInet(self):
12941345
self.naming.GetNetAddr.side_effect = [
12951346
[nacaddr.IP("8.8.4.4"), nacaddr.IP("8.8.8.8")],

0 commit comments

Comments
 (0)