tests: replace pycodestyle with black
[vpp.git] / test / test_policer.py
1 #!/usr/bin/env python3
2 # Copyright (c) 2021 Graphiant, Inc.
3
4 import unittest
5
6 from framework import VppTestCase, VppTestRunner
7 from vpp_policer import VppPolicer, PolicerAction
8
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.
11
12 TEST_RATE = 8000  # kbps
13 TEST_BURST = 10000  # ms
14
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
19
20 NUM_PKTS = 20000
21
22 CBURST = 100000  # Committed burst in bytes
23 EBURST = 200000  # Excess burst in bytes
24
25
26 class TestPolicer(VppTestCase):
27     """Policer Test Case"""
28
29     def run_policer_test(
30         self, type, cir, cb, eir, eb, rate=8000, burst=10000, colour=0
31     ):
32         """
33         Configure a Policer and push traffic through it.
34         """
35         types = {
36             "1R2C": 0,
37             "1R3C": 1,
38             "2R3C": 3,
39         }
40
41         pol_type = types.get(type)
42         policer = VppPolicer(
43             self,
44             "pol1",
45             cir,
46             eir,
47             cb,
48             eb,
49             rate_type=0,
50             type=pol_type,
51             color_aware=colour,
52         )
53         policer.add_vpp_config()
54
55         error = self.vapi.cli(
56             f"test policing index {policer.policer_index} rate {rate} "
57             f"burst {burst} colour {colour}"
58         )
59
60         stats = policer.get_stats()
61         policer.remove_vpp_config()
62
63         return stats
64
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)
69
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)
74
75         stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
76         self.assertEqual(stats["violate_packets"], NUM_PKTS)
77
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)
82
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)
87
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)
92
93         stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST, colour=2)
94         self.assertEqual(stats["violate_packets"], NUM_PKTS)
95
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)
100
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)
105
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)
110
111         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=1)
112         self.assertEqual(stats["exceed_packets"], NUM_PKTS)
113
114         stats = self.run_policer_test(
115             "2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST, colour=1
116         )
117         self.assertEqual(stats["conform_packets"], 0)
118         self.assertGreater(stats["exceed_packets"], 0)
119         self.assertGreater(stats["violate_packets"], 0)
120
121         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=2)
122         self.assertEqual(stats["violate_packets"], NUM_PKTS)
123
124
125 if __name__ == "__main__":
126     unittest.main(testRunner=VppTestRunner)