09b6e52da011261ce748f1d996154ad9b6a14486
[vpp.git] / src / vnet / policer / 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 setUp(self):
30         super(TestPolicer, self).setUp()
31
32     def tearDown(self):
33         super(TestPolicer, self).tearDown()
34
35     def run_policer_test(self, type, cir, cb, eir, eb, rate=8000, burst=10000,
36                          colour=0):
37         """
38         Configure a Policer and push traffic through it.
39         """
40         types = {
41             '1R2C': 0,
42             '1R3C': 1,
43             '2R3C': 3,
44         }
45
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()
50
51         error = self.vapi.cli(
52             f"test policing index {policer.policer_index} rate {rate} "
53             f"burst {burst} colour {colour}")
54
55         stats = policer.get_stats()
56         policer.remove_vpp_config()
57
58         return stats
59
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)
64
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)
69
70         stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
71         self.assertEqual(stats['violate_packets'], NUM_PKTS)
72
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)
77
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)
82
83         stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
84                                       colour=1)
85         self.assertEqual(stats['conform_packets'], 0)
86         self.assertGreater(stats['exceed_packets'], 0)
87         self.assertGreater(stats['violate_packets'], 0)
88
89         stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
90                                       colour=2)
91         self.assertEqual(stats['violate_packets'], NUM_PKTS)
92
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)
97
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)
102
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)
107
108         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
109                                       colour=1)
110         self.assertEqual(stats['exceed_packets'], NUM_PKTS)
111
112         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST,
113                                       colour=1)
114         self.assertEqual(stats['conform_packets'], 0)
115         self.assertGreater(stats['exceed_packets'], 0)
116         self.assertGreater(stats['violate_packets'], 0)
117
118         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
119                                       colour=2)
120         self.assertEqual(stats['violate_packets'], NUM_PKTS)
121
122 if __name__ == '__main__':
123     unittest.main(testRunner=VppTestRunner)