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 """
29 def run_policer_test(self, type, cir, cb, eir, eb, rate=8000, burst=10000,
32 Configure a Policer and push traffic through it.
40 pol_type = types.get(type)
41 policer = VppPolicer(self, "pol1", cir, eir, cb, eb, rate_type=0,
42 type=pol_type, color_aware=colour)
43 policer.add_vpp_config()
45 error = self.vapi.cli(
46 f"test policing index {policer.policer_index} rate {rate} "
47 f"burst {burst} colour {colour}")
49 stats = policer.get_stats()
50 policer.remove_vpp_config()
54 def test_policer_1r2c(self):
55 """ Single rate, 2 colour policer """
56 stats = self.run_policer_test("1R2C", CIR_OK, CBURST, 0, 0)
57 self.assertEqual(stats['conform_packets'], NUM_PKTS)
59 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0)
60 self.assertLess(stats['conform_packets'], NUM_PKTS)
61 self.assertEqual(stats['exceed_packets'], 0)
62 self.assertGreater(stats['violate_packets'], 0)
64 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
65 self.assertEqual(stats['violate_packets'], NUM_PKTS)
67 def test_policer_1r3c(self):
68 """ Single rate, 3 colour policer """
69 stats = self.run_policer_test("1R3C", CIR_OK, CBURST, 0, 0)
70 self.assertEqual(stats['conform_packets'], NUM_PKTS)
72 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST)
73 self.assertLess(stats['conform_packets'], NUM_PKTS)
74 self.assertGreater(stats['exceed_packets'], 0)
75 self.assertGreater(stats['violate_packets'], 0)
77 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
79 self.assertEqual(stats['conform_packets'], 0)
80 self.assertGreater(stats['exceed_packets'], 0)
81 self.assertGreater(stats['violate_packets'], 0)
83 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
85 self.assertEqual(stats['violate_packets'], NUM_PKTS)
87 def test_policer_2r3c(self):
88 """ Dual rate, 3 colour policer """
89 stats = self.run_policer_test("2R3C", CIR_OK, CBURST, EIR_OK, EBURST)
90 self.assertEqual(stats['conform_packets'], NUM_PKTS)
92 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST)
93 self.assertLess(stats['conform_packets'], NUM_PKTS)
94 self.assertGreater(stats['exceed_packets'], 0)
95 self.assertEqual(stats['violate_packets'], 0)
97 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST)
98 self.assertLess(stats['conform_packets'], NUM_PKTS)
99 self.assertGreater(stats['exceed_packets'], 0)
100 self.assertGreater(stats['violate_packets'], 0)
102 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
104 self.assertEqual(stats['exceed_packets'], NUM_PKTS)
106 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST,
108 self.assertEqual(stats['conform_packets'], 0)
109 self.assertGreater(stats['exceed_packets'], 0)
110 self.assertGreater(stats['violate_packets'], 0)
112 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
114 self.assertEqual(stats['violate_packets'], NUM_PKTS)
116 if __name__ == '__main__':
117 unittest.main(testRunner=VppTestRunner)