2 # Copyright (c) 2021 Graphiant, Inc.
6 from framework import VppTestCase, VppTestRunner
7 from vpp_policer import VppPolicer, PolicerAction
9 # Default for the tests is 10s of "Green" packets at 8Mbps, ie. 10M bytes.
10 # The policer helper CLI "sends" 500 byte packets, so default is 20000.
12 TEST_RATE = 8000 # kbps
13 TEST_BURST = 10000 # ms
15 CIR_OK = 8500 # CIR in kbps, above test rate
16 CIR_LOW = 7000 # CIR in kbps, below test rate
17 EIR_OK = 9000 # EIR in kbps, above test rate
18 EIR_LOW = 7500 # EIR in kbps, below test rate
22 CBURST = 100000 # Committed burst in bytes
23 EBURST = 200000 # Excess burst in bytes
26 class TestPolicer(VppTestCase):
27 """Policer Test Case"""
30 self, type, cir, cb, eir, eb, rate=8000, burst=10000, colour=0
33 Configure a Policer and push traffic through it.
41 pol_type = types.get(type)
53 policer.add_vpp_config()
55 error = self.vapi.cli(
56 f"test policing index {policer.policer_index} rate {rate} "
57 f"burst {burst} colour {colour}"
60 stats = policer.get_stats()
61 policer.remove_vpp_config()
65 def test_policer_1r2c(self):
66 """Single rate, 2 colour policer"""
67 stats = self.run_policer_test("1R2C", CIR_OK, CBURST, 0, 0)
68 self.assertEqual(stats["conform_packets"], NUM_PKTS)
70 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0)
71 self.assertLess(stats["conform_packets"], NUM_PKTS)
72 self.assertEqual(stats["exceed_packets"], 0)
73 self.assertGreater(stats["violate_packets"], 0)
75 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
76 self.assertEqual(stats["violate_packets"], NUM_PKTS)
78 def test_policer_1r3c(self):
79 """Single rate, 3 colour policer"""
80 stats = self.run_policer_test("1R3C", CIR_OK, CBURST, 0, 0)
81 self.assertEqual(stats["conform_packets"], NUM_PKTS)
83 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST)
84 self.assertLess(stats["conform_packets"], NUM_PKTS)
85 self.assertGreater(stats["exceed_packets"], 0)
86 self.assertGreater(stats["violate_packets"], 0)
88 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST, colour=1)
89 self.assertEqual(stats["conform_packets"], 0)
90 self.assertGreater(stats["exceed_packets"], 0)
91 self.assertGreater(stats["violate_packets"], 0)
93 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST, colour=2)
94 self.assertEqual(stats["violate_packets"], NUM_PKTS)
96 def test_policer_2r3c(self):
97 """Dual rate, 3 colour policer"""
98 stats = self.run_policer_test("2R3C", CIR_OK, CBURST, EIR_OK, EBURST)
99 self.assertEqual(stats["conform_packets"], NUM_PKTS)
101 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST)
102 self.assertLess(stats["conform_packets"], NUM_PKTS)
103 self.assertGreater(stats["exceed_packets"], 0)
104 self.assertEqual(stats["violate_packets"], 0)
106 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST)
107 self.assertLess(stats["conform_packets"], NUM_PKTS)
108 self.assertGreater(stats["exceed_packets"], 0)
109 self.assertGreater(stats["violate_packets"], 0)
111 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=1)
112 self.assertEqual(stats["exceed_packets"], NUM_PKTS)
114 stats = self.run_policer_test(
115 "2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST, colour=1
117 self.assertEqual(stats["conform_packets"], 0)
118 self.assertGreater(stats["exceed_packets"], 0)
119 self.assertGreater(stats["violate_packets"], 0)
121 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=2)
122 self.assertEqual(stats["violate_packets"], NUM_PKTS)
125 if __name__ == "__main__":
126 unittest.main(testRunner=VppTestRunner)