Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Paul Vinciguerra | 38404db | 2019-05-14 13:35:19 -0400 | [diff] [blame] | 2 | import unittest |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 3 | from ipaddress import IPv4Network |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 4 | |
| 5 | from scapy.packet import Raw |
| 6 | from scapy.layers.l2 import Ether |
| 7 | from scapy.layers.inet import IP, UDP |
| 8 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 9 | from framework import VppTestCase |
| 10 | from asfframework import VppTestRunner |
Paul Vinciguerra | 38404db | 2019-05-14 13:35:19 -0400 | [diff] [blame] | 11 | from vpp_interface import VppInterface |
| 12 | from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 13 | from vpp_acl import AclRule, VppAcl, VppAclInterface |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 14 | from config import config |
Paul Vinciguerra | 38404db | 2019-05-14 13:35:19 -0400 | [diff] [blame] | 15 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 16 | NUM_PKTS = 67 |
| 17 | |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 18 | |
| 19 | class VppPipe(VppInterface): |
| 20 | """ |
| 21 | VPP Pipe |
| 22 | """ |
| 23 | |
| 24 | @property |
| 25 | def east(self): |
| 26 | return self.result.pipe_sw_if_index[1] |
| 27 | |
| 28 | @property |
| 29 | def west(self): |
| 30 | return self.result.pipe_sw_if_index[0] |
| 31 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 32 | def __init__(self, test, instance=0xFFFFFFFF): |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 33 | super(VppPipe, self).__init__(test) |
| 34 | self._test = test |
| 35 | self.instance = instance |
| 36 | |
| 37 | def add_vpp_config(self): |
| 38 | self.result = self._test.vapi.pipe_create( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | 0 if self.instance == 0xFFFFFFFF else 1, self.instance |
| 40 | ) |
Neale Ranns | 208c29a | 2018-04-11 08:08:30 -0700 | [diff] [blame] | 41 | self.set_sw_if_index(self.result.sw_if_index) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 42 | |
| 43 | def remove_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | self._test.vapi.pipe_delete(self.result.sw_if_index) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 45 | |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 46 | def object_id(self): |
| 47 | return "pipe-%d" % (self._sw_if_index) |
| 48 | |
| 49 | def query_vpp_config(self): |
| 50 | pipes = self._test.vapi.pipe_dump() |
| 51 | for p in pipes: |
Neale Ranns | 208c29a | 2018-04-11 08:08:30 -0700 | [diff] [blame] | 52 | if p.sw_if_index == self.result.sw_if_index: |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 53 | return True |
| 54 | return False |
| 55 | |
Stanislav Zaikin | 328b5da | 2021-07-15 16:27:29 +0200 | [diff] [blame] | 56 | def set_unnumbered(self, ip_sw_if_index, is_east, is_add=True): |
| 57 | if is_east: |
| 58 | res = self._test.vapi.sw_interface_set_unnumbered( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | ip_sw_if_index, self.east, is_add |
| 60 | ) |
Stanislav Zaikin | 328b5da | 2021-07-15 16:27:29 +0200 | [diff] [blame] | 61 | else: |
| 62 | res = self._test.vapi.sw_interface_set_unnumbered( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 63 | ip_sw_if_index, self.west, is_add |
| 64 | ) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 65 | |
| 66 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 67 | @unittest.skipIf("acl" in config.excluded_plugins, "Exclude tests requiring ACL plugin") |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 68 | class TestPipe(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 69 | """Pipes""" |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 70 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 71 | @classmethod |
| 72 | def setUpClass(cls): |
| 73 | super(TestPipe, cls).setUpClass() |
| 74 | |
| 75 | @classmethod |
| 76 | def tearDownClass(cls): |
| 77 | super(TestPipe, cls).tearDownClass() |
| 78 | |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 79 | def setUp(self): |
| 80 | super(TestPipe, self).setUp() |
| 81 | |
| 82 | self.create_pg_interfaces(range(4)) |
| 83 | |
| 84 | for i in self.pg_interfaces: |
| 85 | i.admin_up() |
| 86 | |
| 87 | def tearDown(self): |
| 88 | for i in self.pg_interfaces: |
| 89 | i.admin_down() |
| 90 | |
| 91 | super(TestPipe, self).tearDown() |
| 92 | |
| 93 | def test_pipe(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 94 | """Pipes""" |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 95 | |
Neale Ranns | 208c29a | 2018-04-11 08:08:30 -0700 | [diff] [blame] | 96 | pipes = [VppPipe(self), VppPipe(self, 10)] |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 97 | |
| 98 | for p in pipes: |
| 99 | p.add_vpp_config() |
| 100 | p.admin_up() |
| 101 | |
| 102 | # |
| 103 | # L2 cross-connect pipe0 east with pg0 and west with pg1 |
| 104 | # |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 105 | self.vapi.sw_interface_set_l2_xconnect( |
| 106 | self.pg0.sw_if_index, pipes[0].east, enable=1 |
| 107 | ) |
| 108 | self.vapi.sw_interface_set_l2_xconnect( |
| 109 | pipes[0].east, self.pg0.sw_if_index, enable=1 |
| 110 | ) |
| 111 | self.vapi.sw_interface_set_l2_xconnect( |
| 112 | self.pg1.sw_if_index, pipes[0].west, enable=1 |
| 113 | ) |
| 114 | self.vapi.sw_interface_set_l2_xconnect( |
| 115 | pipes[0].west, self.pg1.sw_if_index, enable=1 |
| 116 | ) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 117 | |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 118 | # test bi-directional L2 flow pg0<->pg1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | p = ( |
| 120 | Ether(src=self.pg0.remote_mac, dst=self.pg1.remote_mac) |
| 121 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 122 | / UDP(sport=1234, dport=1234) |
| 123 | / Raw(b"\xa5" * 100) |
| 124 | ) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 125 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 126 | self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1) |
| 127 | self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 128 | |
| 129 | # |
| 130 | # Attach ACL to ensure features are run on the pipe |
| 131 | # |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 132 | rule_1 = AclRule( |
| 133 | is_permit=0, |
| 134 | proto=17, |
| 135 | src_prefix=IPv4Network("1.1.1.1/32"), |
| 136 | dst_prefix=IPv4Network("1.1.1.2/32"), |
| 137 | ports=1234, |
| 138 | ) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 139 | acl = VppAcl(self, rules=[rule_1]) |
| 140 | acl.add_vpp_config() |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 141 | |
| 142 | # Apply the ACL on the pipe on output |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 143 | acl_if_e = VppAclInterface( |
| 144 | self, sw_if_index=pipes[0].east, n_input=0, acls=[acl] |
| 145 | ) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 146 | acl_if_e.add_vpp_config() |
| 147 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 148 | self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS) |
| 149 | self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 150 | |
| 151 | # remove from output and apply on input |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 152 | acl_if_e.remove_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 153 | acl_if_w = VppAclInterface( |
| 154 | self, sw_if_index=pipes[0].west, n_input=1, acls=[acl] |
| 155 | ) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 156 | acl_if_w.add_vpp_config() |
| 157 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 158 | self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS) |
| 159 | self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 160 | |
| 161 | acl_if_w.remove_vpp_config() |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 162 | self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1) |
| 163 | self.send_and_expect(self.pg1, p * NUM_PKTS, self.pg0) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 164 | |
| 165 | # |
| 166 | # L3 routes in two separate tables so a pipe can be used to L3 |
| 167 | # x-connect |
| 168 | # |
| 169 | tables = [] |
| 170 | tables.append(VppIpTable(self, 1)) |
| 171 | tables.append(VppIpTable(self, 2)) |
| 172 | |
| 173 | for t in tables: |
| 174 | t.add_vpp_config() |
| 175 | |
| 176 | self.pg2.set_table_ip4(1) |
| 177 | self.pg2.config_ip4() |
| 178 | self.pg2.resolve_arp() |
| 179 | self.pg3.set_table_ip4(2) |
| 180 | self.pg3.config_ip4() |
| 181 | self.pg3.resolve_arp() |
| 182 | |
| 183 | routes = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 184 | routes.append( |
| 185 | VppIpRoute( |
| 186 | self, |
| 187 | "1.1.1.1", |
| 188 | 32, |
| 189 | [VppRoutePath(self.pg3.remote_ip4, self.pg3.sw_if_index)], |
| 190 | table_id=2, |
| 191 | ) |
| 192 | ) |
| 193 | routes.append( |
| 194 | VppIpRoute( |
| 195 | self, |
| 196 | "1.1.1.1", |
| 197 | 32, |
| 198 | [VppRoutePath("0.0.0.0", pipes[1].east)], |
| 199 | table_id=1, |
| 200 | ) |
| 201 | ) |
| 202 | routes.append( |
| 203 | VppIpRoute( |
| 204 | self, |
| 205 | "1.1.1.2", |
| 206 | 32, |
| 207 | [VppRoutePath("0.0.0.0", pipes[1].west)], |
| 208 | table_id=2, |
| 209 | ) |
| 210 | ) |
| 211 | routes.append( |
| 212 | VppIpRoute( |
| 213 | self, |
| 214 | "1.1.1.2", |
| 215 | 32, |
| 216 | [VppRoutePath(self.pg2.remote_ip4, self.pg2.sw_if_index)], |
| 217 | table_id=1, |
| 218 | ) |
| 219 | ) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 220 | |
| 221 | for r in routes: |
| 222 | r.add_vpp_config() |
| 223 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 224 | p_east = ( |
| 225 | Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) |
| 226 | / IP(src="1.1.1.2", dst="1.1.1.1") |
| 227 | / UDP(sport=1234, dport=1234) |
| 228 | / Raw(b"\xa5" * 100) |
| 229 | ) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 230 | |
| 231 | # bind the pipe ends to the correct tables |
| 232 | self.vapi.sw_interface_set_table(pipes[1].west, 0, 2) |
| 233 | self.vapi.sw_interface_set_table(pipes[1].east, 0, 1) |
| 234 | |
| 235 | # IP is not enabled on the pipes at this point |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 236 | self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 237 | |
| 238 | # IP enable the Pipes by making them unnumbered |
Stanislav Zaikin | 328b5da | 2021-07-15 16:27:29 +0200 | [diff] [blame] | 239 | pipes[1].set_unnumbered(self.pg2.sw_if_index, True) |
| 240 | pipes[1].set_unnumbered(self.pg3.sw_if_index, False) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 241 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 242 | self.send_and_expect(self.pg2, p_east * NUM_PKTS, self.pg3) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 243 | |
| 244 | # and the return path |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 245 | p_west = ( |
| 246 | Ether(src=self.pg3.remote_mac, dst=self.pg3.local_mac) |
| 247 | / IP(src="1.1.1.1", dst="1.1.1.2") |
| 248 | / UDP(sport=1234, dport=1234) |
| 249 | / Raw(b"\xa5" * 100) |
| 250 | ) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 251 | self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 252 | |
| 253 | # |
| 254 | # Use ACLs to test features run on the Pipes |
| 255 | # |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 256 | acl_if_e1 = VppAclInterface( |
| 257 | self, sw_if_index=pipes[1].east, n_input=0, acls=[acl] |
| 258 | ) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 259 | acl_if_e1.add_vpp_config() |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 260 | self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS) |
| 261 | self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 262 | |
| 263 | # remove from output and apply on input |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 264 | acl_if_e1.remove_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 265 | acl_if_w1 = VppAclInterface( |
| 266 | self, sw_if_index=pipes[1].west, n_input=1, acls=[acl] |
| 267 | ) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 268 | acl_if_w1.add_vpp_config() |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 269 | self.send_and_assert_no_replies(self.pg2, p_east * NUM_PKTS) |
| 270 | self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2) |
Jakub Grajciar | 2f8cd91 | 2020-03-27 06:55:06 +0100 | [diff] [blame] | 271 | acl_if_w1.remove_vpp_config() |
| 272 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 273 | self.send_and_expect(self.pg2, p_east * NUM_PKTS, self.pg3) |
| 274 | self.send_and_expect(self.pg3, p_west * NUM_PKTS, self.pg2) |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 275 | |
| 276 | # cleanup (so the tables delete) |
| 277 | self.pg2.unconfig_ip4() |
| 278 | self.pg2.set_table_ip4(0) |
| 279 | self.pg3.unconfig_ip4() |
| 280 | self.pg3.set_table_ip4(0) |
| 281 | self.vapi.sw_interface_set_table(pipes[1].west, 0, 0) |
| 282 | self.vapi.sw_interface_set_table(pipes[1].east, 0, 0) |
| 283 | |
| 284 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 285 | if __name__ == "__main__": |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 286 | unittest.main(testRunner=VppTestRunner) |