blob: c23ec00956da408ab8ad9723edc0e7b1ebab6069 [file] [log] [blame]
Brian Russell130680b2021-02-04 17:53:23 +00001#!/usr/bin/env python3
2# Copyright (c) 2021 Graphiant, Inc.
3
4import unittest
5
Pratikshya Prasai657bdf72022-08-18 11:09:38 -04006from asfframework import VppTestCase, VppTestRunner
Brian Russell130680b2021-02-04 17:53:23 +00007from 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
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012TEST_RATE = 8000 # kbps
Brian Russell130680b2021-02-04 17:53:23 +000013TEST_BURST = 10000 # ms
14
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015CIR_OK = 8500 # CIR in kbps, above test rate
16CIR_LOW = 7000 # CIR in kbps, below test rate
17EIR_OK = 9000 # EIR in kbps, above test rate
18EIR_LOW = 7500 # EIR in kbps, below test rate
Brian Russell130680b2021-02-04 17:53:23 +000019
20NUM_PKTS = 20000
21
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020022CBURST = 100000 # Committed burst in bytes
23EBURST = 200000 # Excess burst in bytes
Brian Russell130680b2021-02-04 17:53:23 +000024
25
26class TestPolicer(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020027 """Policer Test Case"""
Brian Russell130680b2021-02-04 17:53:23 +000028
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 def run_policer_test(
30 self, type, cir, cb, eir, eb, rate=8000, burst=10000, colour=0
31 ):
Brian Russell130680b2021-02-04 17:53:23 +000032 """
33 Configure a Policer and push traffic through it.
34 """
35 types = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036 "1R2C": 0,
37 "1R3C": 1,
38 "2R3C": 3,
Brian Russell130680b2021-02-04 17:53:23 +000039 }
40
41 pol_type = types.get(type)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 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 )
Brian Russell130680b2021-02-04 17:53:23 +000053 policer.add_vpp_config()
54
55 error = self.vapi.cli(
56 f"test policing index {policer.policer_index} rate {rate} "
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 f"burst {burst} colour {colour}"
58 )
Brian Russell130680b2021-02-04 17:53:23 +000059
60 stats = policer.get_stats()
61 policer.remove_vpp_config()
62
63 return stats
64
65 def test_policer_1r2c(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 """Single rate, 2 colour policer"""
Brian Russell130680b2021-02-04 17:53:23 +000067 stats = self.run_policer_test("1R2C", CIR_OK, CBURST, 0, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 self.assertEqual(stats["conform_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +000069
70 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 self.assertLess(stats["conform_packets"], NUM_PKTS)
72 self.assertEqual(stats["exceed_packets"], 0)
73 self.assertGreater(stats["violate_packets"], 0)
Brian Russell130680b2021-02-04 17:53:23 +000074
75 stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 self.assertEqual(stats["violate_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +000077
78 def test_policer_1r3c(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 """Single rate, 3 colour policer"""
Brian Russell130680b2021-02-04 17:53:23 +000080 stats = self.run_policer_test("1R3C", CIR_OK, CBURST, 0, 0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 self.assertEqual(stats["conform_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +000082
83 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020084 self.assertLess(stats["conform_packets"], NUM_PKTS)
85 self.assertGreater(stats["exceed_packets"], 0)
86 self.assertGreater(stats["violate_packets"], 0)
Brian Russell130680b2021-02-04 17:53:23 +000087
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 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)
Brian Russell130680b2021-02-04 17:53:23 +000092
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020093 stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST, colour=2)
94 self.assertEqual(stats["violate_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +000095
96 def test_policer_2r3c(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 """Dual rate, 3 colour policer"""
Brian Russell130680b2021-02-04 17:53:23 +000098 stats = self.run_policer_test("2R3C", CIR_OK, CBURST, EIR_OK, EBURST)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 self.assertEqual(stats["conform_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +0000100
101 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200102 self.assertLess(stats["conform_packets"], NUM_PKTS)
103 self.assertGreater(stats["exceed_packets"], 0)
104 self.assertEqual(stats["violate_packets"], 0)
Brian Russell130680b2021-02-04 17:53:23 +0000105
106 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 self.assertLess(stats["conform_packets"], NUM_PKTS)
108 self.assertGreater(stats["exceed_packets"], 0)
109 self.assertGreater(stats["violate_packets"], 0)
Brian Russell130680b2021-02-04 17:53:23 +0000110
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=1)
112 self.assertEqual(stats["exceed_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +0000113
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200114 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)
Brian Russell130680b2021-02-04 17:53:23 +0000120
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121 stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=2)
122 self.assertEqual(stats["violate_packets"], NUM_PKTS)
Brian Russell130680b2021-02-04 17:53:23 +0000123
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124
125if __name__ == "__main__":
Brian Russell130680b2021-02-04 17:53:23 +0000126 unittest.main(testRunner=VppTestRunner)