Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2021 Graphiant, Inc. |
| 3 | |
| 4 | import unittest |
| 5 | import scapy.compat |
| 6 | from scapy.layers.inet import IP, UDP |
| 7 | from scapy.layers.l2 import Ether |
| 8 | from scapy.packet import Raw |
| 9 | from framework import VppTestCase, VppTestRunner |
| 10 | from vpp_papi import VppEnum |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 11 | from vpp_policer import VppPolicer, PolicerAction, Dir |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 12 | |
| 13 | NUM_PKTS = 67 |
| 14 | |
| 15 | |
| 16 | class TestPolicerInput(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 17 | """Policer on an interface""" |
| 18 | |
Klement Sekera | 8d81502 | 2021-03-15 16:58:10 +0100 | [diff] [blame] | 19 | vpp_worker_count = 2 |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 20 | |
| 21 | def setUp(self): |
| 22 | super(TestPolicerInput, self).setUp() |
| 23 | |
| 24 | self.create_pg_interfaces(range(2)) |
| 25 | for i in self.pg_interfaces: |
| 26 | i.admin_up() |
| 27 | i.config_ip4() |
| 28 | i.resolve_arp() |
| 29 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 30 | self.pkt = ( |
| 31 | Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
| 32 | / IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4) |
| 33 | / UDP(sport=1234, dport=1234) |
| 34 | / Raw(b"\xa5" * 100) |
| 35 | ) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 36 | |
| 37 | def tearDown(self): |
| 38 | for i in self.pg_interfaces: |
| 39 | i.unconfig_ip4() |
| 40 | i.admin_down() |
| 41 | super(TestPolicerInput, self).tearDown() |
| 42 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 43 | def policer_interface_test(self, dir: Dir): |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 44 | pkts = self.pkt * NUM_PKTS |
| 45 | |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 46 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 47 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 48 | ) |
| 49 | policer = VppPolicer( |
| 50 | self, |
| 51 | "pol1", |
| 52 | 80, |
| 53 | 0, |
| 54 | 1000, |
| 55 | 0, |
| 56 | conform_action=action_tx, |
| 57 | exceed_action=action_tx, |
| 58 | violate_action=action_tx, |
| 59 | ) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 60 | policer.add_vpp_config() |
| 61 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 62 | sw_if_index = self.pg0.sw_if_index if dir == Dir.RX else self.pg1.sw_if_index |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 63 | |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 64 | # Start policing on pg0 |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 65 | policer.apply_vpp_config(sw_if_index, dir, True) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 66 | |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 67 | rx = self.send_and_expect(self.pg0, pkts, self.pg1, worker=0) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 68 | stats = policer.get_stats() |
| 69 | |
| 70 | # Single rate, 2 colour policer - expect conform, violate but no exceed |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | self.assertGreater(stats["conform_packets"], 0) |
| 72 | self.assertEqual(stats["exceed_packets"], 0) |
| 73 | self.assertGreater(stats["violate_packets"], 0) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 74 | |
| 75 | # Stop policing on pg0 |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 76 | policer.apply_vpp_config(sw_if_index, dir, False) |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 77 | |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 78 | rx = self.send_and_expect(self.pg0, pkts, self.pg1, worker=0) |
| 79 | |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 80 | statsnew = policer.get_stats() |
| 81 | |
| 82 | # No new packets counted |
| 83 | self.assertEqual(stats, statsnew) |
| 84 | |
| 85 | policer.remove_vpp_config() |
| 86 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 87 | def test_policer_input(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 88 | """Input Policing""" |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 89 | self.policer_interface_test(Dir.RX) |
| 90 | |
| 91 | def test_policer_output(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | """Output Policing""" |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 93 | self.policer_interface_test(Dir.TX) |
| 94 | |
Maxime Peim | 2d1a62b | 2023-01-06 11:57:38 +0000 | [diff] [blame] | 95 | def test_policer_reset(self): |
| 96 | """Policer reset bucket""" |
| 97 | pkts = self.pkt * NUM_PKTS |
| 98 | |
| 99 | action_tx = PolicerAction( |
| 100 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 101 | ) |
| 102 | policer = VppPolicer( |
| 103 | self, |
| 104 | "pol1", |
| 105 | 1, |
| 106 | 0, |
| 107 | 10000, |
| 108 | 0, |
| 109 | conform_action=action_tx, |
| 110 | exceed_action=action_tx, |
| 111 | violate_action=action_tx, |
| 112 | ) |
| 113 | policer.add_vpp_config() |
| 114 | |
| 115 | # Start policing on pg0 |
| 116 | policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, True) |
| 117 | |
| 118 | self.send_and_expect(self.pg0, pkts, self.pg1, worker=0) |
| 119 | details = policer.get_details() |
| 120 | |
| 121 | self.assertGreater(details.current_limit, details.current_bucket) |
| 122 | |
| 123 | self.send_and_expect(self.pg0, pkts, self.pg1, worker=0) |
| 124 | self.vapi.policer_reset(policer_index=policer.policer_index) |
| 125 | details = policer.get_details() |
| 126 | |
| 127 | self.assertEqual(details.current_limit, details.current_bucket) |
| 128 | |
| 129 | policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, False) |
| 130 | |
| 131 | policer.remove_vpp_config() |
| 132 | |
| 133 | def test_policer_update(self): |
| 134 | """Policer update""" |
| 135 | pkts = self.pkt * NUM_PKTS |
| 136 | |
| 137 | action_tx = PolicerAction( |
| 138 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 139 | ) |
| 140 | policer = VppPolicer( |
| 141 | self, |
| 142 | "pol1", |
| 143 | 1, |
| 144 | 0, |
| 145 | 10000, |
| 146 | 0, |
| 147 | conform_action=action_tx, |
| 148 | exceed_action=action_tx, |
| 149 | violate_action=action_tx, |
| 150 | ) |
| 151 | policer.add_vpp_config() |
| 152 | |
| 153 | # Start policing on pg0 |
| 154 | policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, True) |
| 155 | |
| 156 | self.send_and_expect(self.pg0, pkts, self.pg1, worker=0) |
| 157 | details_before = policer.get_details() |
| 158 | |
| 159 | self.assertGreater(details_before.current_limit, details_before.current_bucket) |
| 160 | |
| 161 | policer.cir = 8000 |
| 162 | policer.commited_burst = 100000 |
| 163 | policer.update() |
| 164 | |
| 165 | details_after = policer.get_details() |
| 166 | |
| 167 | self.assertGreater(details_after.cir, details_before.cir) |
| 168 | self.assertGreater(details_after.cb, details_before.cb) |
| 169 | |
| 170 | policer.apply_vpp_config(self.pg0.sw_if_index, Dir.RX, False) |
| 171 | |
| 172 | policer.remove_vpp_config() |
| 173 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 174 | def policer_handoff_test(self, dir: Dir): |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 175 | pkts = self.pkt * NUM_PKTS |
| 176 | |
| 177 | action_tx = PolicerAction( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 178 | VppEnum.vl_api_sse2_qos_action_type_t.SSE2_QOS_ACTION_API_TRANSMIT, 0 |
| 179 | ) |
| 180 | policer = VppPolicer( |
| 181 | self, |
| 182 | "pol2", |
| 183 | 80, |
| 184 | 0, |
| 185 | 1000, |
| 186 | 0, |
| 187 | conform_action=action_tx, |
| 188 | exceed_action=action_tx, |
| 189 | violate_action=action_tx, |
| 190 | ) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 191 | policer.add_vpp_config() |
| 192 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 193 | sw_if_index = self.pg0.sw_if_index if dir == Dir.RX else self.pg1.sw_if_index |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 194 | |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 195 | # Bind the policer to worker 1 |
| 196 | policer.bind_vpp_config(1, True) |
| 197 | |
| 198 | # Start policing on pg0 |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 199 | policer.apply_vpp_config(sw_if_index, dir, True) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 200 | |
| 201 | for worker in [0, 1]: |
| 202 | self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker) |
| 203 | self.logger.debug(self.vapi.cli("show trace max 100")) |
| 204 | |
| 205 | stats = policer.get_stats() |
| 206 | stats0 = policer.get_stats(worker=0) |
| 207 | stats1 = policer.get_stats(worker=1) |
| 208 | |
| 209 | # Worker 1, should have done all the policing |
| 210 | self.assertEqual(stats, stats1) |
| 211 | |
| 212 | # Worker 0, should have handed everything off |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 213 | self.assertEqual(stats0["conform_packets"], 0) |
| 214 | self.assertEqual(stats0["exceed_packets"], 0) |
| 215 | self.assertEqual(stats0["violate_packets"], 0) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 216 | |
| 217 | # Unbind the policer from worker 1 and repeat |
| 218 | policer.bind_vpp_config(1, False) |
| 219 | for worker in [0, 1]: |
| 220 | self.send_and_expect(self.pg0, pkts, self.pg1, worker=worker) |
| 221 | self.logger.debug(self.vapi.cli("show trace max 100")) |
| 222 | |
| 223 | # The policer should auto-bind to worker 0 when packets arrive |
| 224 | stats = policer.get_stats() |
| 225 | |
| 226 | # The 2 workers should now have policed the same amount |
| 227 | stats = policer.get_stats() |
| 228 | stats0 = policer.get_stats(worker=0) |
| 229 | stats1 = policer.get_stats(worker=1) |
| 230 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 231 | self.assertGreater(stats0["conform_packets"], 0) |
| 232 | self.assertEqual(stats0["exceed_packets"], 0) |
| 233 | self.assertGreater(stats0["violate_packets"], 0) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 234 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 235 | self.assertGreater(stats1["conform_packets"], 0) |
| 236 | self.assertEqual(stats1["exceed_packets"], 0) |
| 237 | self.assertGreater(stats1["violate_packets"], 0) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 238 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 239 | self.assertEqual( |
| 240 | stats0["conform_packets"] + stats1["conform_packets"], |
| 241 | stats["conform_packets"], |
| 242 | ) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 243 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 244 | self.assertEqual( |
| 245 | stats0["violate_packets"] + stats1["violate_packets"], |
| 246 | stats["violate_packets"], |
| 247 | ) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 248 | |
| 249 | # Stop policing on pg0 |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 250 | policer.apply_vpp_config(sw_if_index, dir, False) |
Brian Russell | 26bcbc1 | 2021-02-18 11:02:29 +0000 | [diff] [blame] | 251 | |
| 252 | policer.remove_vpp_config() |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 253 | |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 254 | def test_policer_handoff_input(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 255 | """Worker thread handoff policer input""" |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 256 | self.policer_handoff_test(Dir.RX) |
| 257 | |
| 258 | def test_policer_handoff_output(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 259 | """Worker thread handoff policer output""" |
Stanislav Zaikin | e5a3ae0 | 2022-04-05 19:23:12 +0200 | [diff] [blame] | 260 | self.policer_handoff_test(Dir.TX) |
| 261 | |
| 262 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 263 | if __name__ == "__main__": |
Brian Russell | 6e6920d | 2021-02-17 15:54:52 +0000 | [diff] [blame] | 264 | unittest.main(testRunner=VppTestRunner) |