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 super(TestPolicer, self).setUp()
33 super(TestPolicer, self).tearDown()
35 def run_policer_test(self, type, cir, cb, eir, eb, rate=8000, burst=10000,
38 Configure a Policer and push traffic through it.
46 pol_type = types.get(type)
47 policer = VppPolicer(self, "pol1", cir, eir, cb, eb, rate_type=0,
48 type=pol_type, color_aware=colour)
49 policer.add_vpp_config()
51 error = self.vapi.cli(
52 f"test policing index {policer.policer_index} rate {rate} "
53 f"burst {burst} colour {colour}")
55 stats = policer.get_stats()
56 policer.remove_vpp_config()
60 def test_policer_1r2c(self):
61 """ Single rate, 2 colour policer """
62 stats = self.run_policer_test("1R2C", CIR_OK, CBURST, 0, 0)
63 self.assertEqual(stats['conform_packets'], NUM_PKTS)
65 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0)
66 self.assertLess(stats['conform_packets'], NUM_PKTS)
67 self.assertEqual(stats['exceed_packets'], 0)
68 self.assertGreater(stats['violate_packets'], 0)
70 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
71 self.assertEqual(stats['violate_packets'], NUM_PKTS)
73 def test_policer_1r3c(self):
74 """ Single rate, 3 colour policer """
75 stats = self.run_policer_test("1R3C", CIR_OK, CBURST, 0, 0)
76 self.assertEqual(stats['conform_packets'], NUM_PKTS)
78 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST)
79 self.assertLess(stats['conform_packets'], NUM_PKTS)
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['conform_packets'], 0)
86 self.assertGreater(stats['exceed_packets'], 0)
87 self.assertGreater(stats['violate_packets'], 0)
89 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
91 self.assertEqual(stats['violate_packets'], NUM_PKTS)
93 def test_policer_2r3c(self):
94 """ Dual rate, 3 colour policer """
95 stats = self.run_policer_test("2R3C", CIR_OK, CBURST, EIR_OK, EBURST)
96 self.assertEqual(stats['conform_packets'], NUM_PKTS)
98 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST)
99 self.assertLess(stats['conform_packets'], NUM_PKTS)
100 self.assertGreater(stats['exceed_packets'], 0)
101 self.assertEqual(stats['violate_packets'], 0)
103 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST)
104 self.assertLess(stats['conform_packets'], NUM_PKTS)
105 self.assertGreater(stats['exceed_packets'], 0)
106 self.assertGreater(stats['violate_packets'], 0)
108 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
110 self.assertEqual(stats['exceed_packets'], NUM_PKTS)
112 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST,
114 self.assertEqual(stats['conform_packets'], 0)
115 self.assertGreater(stats['exceed_packets'], 0)
116 self.assertGreater(stats['violate_packets'], 0)
118 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
120 self.assertEqual(stats['violate_packets'], NUM_PKTS)
122 if __name__ == '__main__':
123 unittest.main(testRunner=VppTestRunner)