Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2021 Graphiant, Inc. |
| 3 | |
| 4 | import unittest |
| 5 | |
Pratikshya Prasai | 657bdf7 | 2022-08-18 11:09:38 -0400 | [diff] [blame^] | 6 | from asfframework import VppTestCase, VppTestRunner |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 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 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 12 | TEST_RATE = 8000 # kbps |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 13 | TEST_BURST = 10000 # ms |
| 14 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 19 | |
| 20 | NUM_PKTS = 20000 |
| 21 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 22 | CBURST = 100000 # Committed burst in bytes |
| 23 | EBURST = 200000 # Excess burst in bytes |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class TestPolicer(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | """Policer Test Case""" |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 28 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 29 | def run_policer_test( |
| 30 | self, type, cir, cb, eir, eb, rate=8000, burst=10000, colour=0 |
| 31 | ): |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 32 | """ |
| 33 | Configure a Policer and push traffic through it. |
| 34 | """ |
| 35 | types = { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 36 | "1R2C": 0, |
| 37 | "1R3C": 1, |
| 38 | "2R3C": 3, |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | pol_type = types.get(type) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 53 | policer.add_vpp_config() |
| 54 | |
| 55 | error = self.vapi.cli( |
| 56 | f"test policing index {policer.policer_index} rate {rate} " |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 57 | f"burst {burst} colour {colour}" |
| 58 | ) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 59 | |
| 60 | stats = policer.get_stats() |
| 61 | policer.remove_vpp_config() |
| 62 | |
| 63 | return stats |
| 64 | |
| 65 | def test_policer_1r2c(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 66 | """Single rate, 2 colour policer""" |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 67 | stats = self.run_policer_test("1R2C", CIR_OK, CBURST, 0, 0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 68 | self.assertEqual(stats["conform_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 69 | |
| 70 | stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | self.assertLess(stats["conform_packets"], NUM_PKTS) |
| 72 | self.assertEqual(stats["exceed_packets"], 0) |
| 73 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 74 | |
| 75 | stats = self.run_policer_test("1R2C", CIR_LOW, CBURST, 0, 0, colour=2) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 76 | self.assertEqual(stats["violate_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 77 | |
| 78 | def test_policer_1r3c(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | """Single rate, 3 colour policer""" |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 80 | stats = self.run_policer_test("1R3C", CIR_OK, CBURST, 0, 0) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 81 | self.assertEqual(stats["conform_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 82 | |
| 83 | stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 84 | self.assertLess(stats["conform_packets"], NUM_PKTS) |
| 85 | self.assertGreater(stats["exceed_packets"], 0) |
| 86 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 87 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 92 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 93 | stats = self.run_policer_test("1R3C", CIR_LOW, CBURST, 0, EBURST, colour=2) |
| 94 | self.assertEqual(stats["violate_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 95 | |
| 96 | def test_policer_2r3c(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 97 | """Dual rate, 3 colour policer""" |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 98 | stats = self.run_policer_test("2R3C", CIR_OK, CBURST, EIR_OK, EBURST) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 99 | self.assertEqual(stats["conform_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 100 | |
| 101 | stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | self.assertLess(stats["conform_packets"], NUM_PKTS) |
| 103 | self.assertGreater(stats["exceed_packets"], 0) |
| 104 | self.assertEqual(stats["violate_packets"], 0) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 105 | |
| 106 | stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_LOW, EBURST) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 107 | self.assertLess(stats["conform_packets"], NUM_PKTS) |
| 108 | self.assertGreater(stats["exceed_packets"], 0) |
| 109 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 110 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=1) |
| 112 | self.assertEqual(stats["exceed_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 113 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 120 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 121 | stats = self.run_policer_test("2R3C", CIR_LOW, CBURST, EIR_OK, EBURST, colour=2) |
| 122 | self.assertEqual(stats["violate_packets"], NUM_PKTS) |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 123 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 124 | |
| 125 | if __name__ == "__main__": |
Brian Russell | 130680b | 2021-02-04 17:53:23 +0000 | [diff] [blame] | 126 | unittest.main(testRunner=VppTestRunner) |