nat: nat44-ed add session timing out indicator in api
[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(self, type, cir, cb, eir, eb, rate=8000, burst=10000,
30                          colour=0):
31         """
32         Configure a Policer and push traffic through it.
33         """
34         types = {
35             '1R2C': 0,
36             '1R3C': 1,
37             '2R3C': 3,
38         }
39
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()
44
45         error = self.vapi.cli(
46             f"test policing index {policer.policer_index} rate {rate} "
47             f"burst {burst} colour {colour}")
48
49         stats = policer.get_stats()
50         policer.remove_vpp_config()
51
52         return stats
53
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)
58
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)
63
64         stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
65         self.assertEqual(stats['violate_packets'], NUM_PKTS)
66
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)
71
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)
76
77         stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST,
78                                       colour=1)
79         self.assertEqual(stats['conform_packets'], 0)
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=2)
85         self.assertEqual(stats['violate_packets'], NUM_PKTS)
86
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)
91
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)
96
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)
101
102         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
103                                       colour=1)
104         self.assertEqual(stats['exceed_packets'], NUM_PKTS)
105
106         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST,
107                                       colour=1)
108         self.assertEqual(stats['conform_packets'], 0)
109         self.assertGreater(stats['exceed_packets'], 0)
110         self.assertGreater(stats['violate_packets'], 0)
111
112         stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST,
113                                       colour=2)
114         self.assertEqual(stats['violate_packets'], NUM_PKTS)
115
116 if __name__ == '__main__':
117     unittest.main(testRunner=VppTestRunner)