Skip to content

Commit 8314a94

Browse files
simone-dellprsunny
authored andcommitted
[acl]: Insert new acl rule with priority in between other rules (#482)
* Add new test case for ACL - Rule Priority New test case - I created 4 rules with priorities 10, 20, 30, 40, then I added a fifth rule with priority 21. Made sure they were all programmed correctly in the DB * new acl test case: added a rule in between configured rules I configured five rules with priorities 10, 20, 30, 40, then configured another rule with priority 21. Verified that all five rules were pushed to the ASICDB * Delete test_acl.py mistakenly added this file * removing redundant code by inserting via a loop * created helper function to check proper rule config
1 parent 1ac125e commit 8314a94

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

tests/test_acl.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,116 @@ def test_V6AclTableDeletion(self, dvs):
779779
keys = atbl.getKeys()
780780
# only the default table was left
781781
assert len(keys) == 1
782+
783+
#helper function to verify if rule exists
784+
def check_rule_existence(self, entry, rules, verifs):
785+
for rule in rules:
786+
ruleD = dict(rule)
787+
#find the rule to match with based on priority
788+
if ruleD["PRIORITY"] == entry['SAI_ACL_ENTRY_ATTR_PRIORITY']:
789+
ruleIndex = rules.index(rule)
790+
#use verification dictionary to match entry to rule
791+
for key in verifs[ruleIndex]:
792+
assert verifs[ruleIndex][key] == entry[key]
793+
#found the rule
794+
return True
795+
#did not find the rule
796+
return False
797+
798+
def test_InsertAclRuleBetweenPriorities(self, dvs):
799+
db = swsscommon.DBConnector(4, dvs.redis_sock, 0)
800+
adb = swsscommon.DBConnector(1, dvs.redis_sock, 0)
801+
802+
bind_ports = ["Ethernet0", "Ethernet4"]
803+
# create ACL_TABLE in config db
804+
tbl = swsscommon.Table(db, "ACL_TABLE")
805+
fvs = swsscommon.FieldValuePairs([("policy_desc", "test"), ("type", "L3"), ("ports", ",".join(bind_ports))])
806+
tbl.set("test_insert", fvs)
807+
808+
time.sleep(2)
809+
810+
num_rules = 0
811+
#create ACL rules
812+
tbl = swsscommon.Table(db, "ACL_RULE")
813+
rules = [ [("PRIORITY", "10"), ("PACKET_ACTION", "DROP"), ("SRC_IP", "10.0.0.0/32")],
814+
[("PRIORITY", "20"), ("PACKET_ACTION", "DROP"), ("DST_IP", "104.44.94.0/23")],
815+
[("PRIORITY", "30"), ("PACKET_ACTION", "DROP"), ("DST_IP", "192.168.0.16/32")],
816+
[("PRIORITY", "40"), ("PACKET_ACTION", "FORWARD"), ("DST_IP", "100.64.0.0/10")] ]
817+
#used to verify how ACL rules are programmed in ASICDB
818+
verifs = [ {'SAI_ACL_ENTRY_ATTR_PRIORITY': '10',
819+
'SAI_ACL_ENTRY_ATTR_FIELD_SRC_IP': '10.0.0.0&mask:255.255.255.255',
820+
'SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION': 'SAI_PACKET_ACTION_DROP'},
821+
{'SAI_ACL_ENTRY_ATTR_PRIORITY': '20',
822+
'SAI_ACL_ENTRY_ATTR_FIELD_DST_IP': '104.44.94.0&mask:255.255.254.0',
823+
'SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION': 'SAI_PACKET_ACTION_DROP'},
824+
{'SAI_ACL_ENTRY_ATTR_PRIORITY': '30',
825+
'SAI_ACL_ENTRY_ATTR_FIELD_DST_IP': '192.168.0.16&mask:255.255.255.255',
826+
'SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION': 'SAI_PACKET_ACTION_DROP'},
827+
{'SAI_ACL_ENTRY_ATTR_PRIORITY': '40',
828+
'SAI_ACL_ENTRY_ATTR_FIELD_DST_IP': '100.64.0.0&mask:255.192.0.0',
829+
'SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION': 'SAI_PACKET_ACTION_FORWARD'} ]
830+
#insert rules
831+
for rule in rules:
832+
fvs = swsscommon.FieldValuePairs(rule)
833+
num_rules += 1
834+
tbl.set( "test_insert|acl_test_rule%s" % num_rules, fvs)
835+
836+
time.sleep(1)
837+
838+
atbl = swsscommon.Table(adb, "ASIC_STATE:SAI_OBJECT_TYPE_ACL_ENTRY")
839+
keys = atbl.getKeys()
840+
841+
#assert that first set of rules are programmed
842+
acl_entry = [k for k in keys if k not in dvs.asicdb.default_acl_entries]
843+
assert len(acl_entry) == num_rules
844+
845+
#insert new rule with odd priority
846+
tbl = swsscommon.Table(db, "ACL_RULE")
847+
insertrule = [("PRIORITY", "21"), ("PACKET_ACTION", "DROP"), ("ETHER_TYPE", "4660")]
848+
#create verification for that rule
849+
verifs.append({'SAI_ACL_ENTRY_ATTR_PRIORITY': '21',
850+
'SAI_ACL_ENTRY_ATTR_FIELD_ETHER_TYPE': '4660&mask:0xffff',
851+
'SAI_ACL_ENTRY_ATTR_ACTION_PACKET_ACTION': 'SAI_PACKET_ACTION_DROP'})
852+
rules.append(insertrule)
853+
fvs = swsscommon.FieldValuePairs(insertrule)
854+
num_rules += 1
855+
tbl.set("test_insert|acl_test_rule%s" % num_rules, fvs)
856+
857+
time.sleep(1)
858+
859+
#assert all rules are programmed
860+
keys = atbl.getKeys()
861+
acl_entry = [k for k in keys if k not in dvs.asicdb.default_acl_entries]
862+
assert len(acl_entry) == num_rules
863+
864+
#match each entry to its corresponding verification
865+
matched_rules = 0
866+
for entry in acl_entry:
867+
(status, fvs) = atbl.get(entry)
868+
assert status == True
869+
assert len(fvs) == 6
870+
#helper function
871+
if self.check_rule_existence(dict(fvs), rules, verifs):
872+
matched_rules += 1
873+
874+
assert num_rules == matched_rules
875+
876+
#cleanup
877+
while num_rules > 0:
878+
tbl._del("test_insert|acl_test_rule%s" % num_rules)
879+
num_rules -= 1
880+
881+
time.sleep(1)
882+
883+
(status, fvs) = atbl.get(acl_entry[0])
884+
assert status == False
885+
886+
tbl = swsscommon.Table(db, "ACL_TABLE")
887+
tbl._del("test_insert")
888+
889+
time.sleep(1)
890+
891+
atbl = swsscommon.Table(adb, "ASIC_STATE:SAI_OBJECT_TYPE_ACL_TABLE")
892+
keys = atbl.getKeys()
893+
# only the default table was left
894+
assert len(keys) == 1

0 commit comments

Comments
 (0)