Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 2 | import unittest |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 3 | from framework import VppTestCase, VppTestRunner |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 4 | |
Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 5 | from vpp_udp_encap import find_udp_encap, VppUdpEncap |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 6 | from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable, VppMplsLabel, \ |
| 7 | FibPathType |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 8 | |
| 9 | from scapy.packet import Raw |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 10 | from scapy.layers.l2 import Ether |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 11 | from scapy.layers.inet import IP, UDP |
| 12 | from scapy.layers.inet6 import IPv6 |
| 13 | from scapy.contrib.mpls import MPLS |
| 14 | |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 15 | NUM_PKTS = 67 |
| 16 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 17 | |
| 18 | class TestUdpEncap(VppTestCase): |
| 19 | """ UDP Encap Test Case """ |
| 20 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | super(TestUdpEncap, cls).setUpClass() |
| 24 | |
| 25 | @classmethod |
| 26 | def tearDownClass(cls): |
| 27 | super(TestUdpEncap, cls).tearDownClass() |
| 28 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 29 | def setUp(self): |
| 30 | super(TestUdpEncap, self).setUp() |
| 31 | |
| 32 | # create 2 pg interfaces |
| 33 | self.create_pg_interfaces(range(4)) |
| 34 | |
| 35 | # setup interfaces |
| 36 | # assign them different tables. |
| 37 | table_id = 0 |
| 38 | self.tables = [] |
| 39 | |
| 40 | for i in self.pg_interfaces: |
| 41 | i.admin_up() |
| 42 | |
| 43 | if table_id != 0: |
| 44 | tbl = VppIpTable(self, table_id) |
| 45 | tbl.add_vpp_config() |
| 46 | self.tables.append(tbl) |
| 47 | tbl = VppIpTable(self, table_id, is_ip6=1) |
| 48 | tbl.add_vpp_config() |
| 49 | self.tables.append(tbl) |
| 50 | |
| 51 | i.set_table_ip4(table_id) |
| 52 | i.set_table_ip6(table_id) |
| 53 | i.config_ip4() |
| 54 | i.resolve_arp() |
| 55 | i.config_ip6() |
| 56 | i.resolve_ndp() |
| 57 | table_id += 1 |
| 58 | |
| 59 | def tearDown(self): |
| 60 | for i in self.pg_interfaces: |
| 61 | i.unconfig_ip4() |
| 62 | i.unconfig_ip6() |
| 63 | i.ip6_disable() |
| 64 | i.set_table_ip4(0) |
| 65 | i.set_table_ip6(0) |
| 66 | i.admin_down() |
| 67 | super(TestUdpEncap, self).tearDown() |
| 68 | |
| 69 | def validate_outer4(self, rx, encap_obj): |
| 70 | self.assertEqual(rx[IP].src, encap_obj.src_ip_s) |
| 71 | self.assertEqual(rx[IP].dst, encap_obj.dst_ip_s) |
| 72 | self.assertEqual(rx[UDP].sport, encap_obj.src_port) |
| 73 | self.assertEqual(rx[UDP].dport, encap_obj.dst_port) |
| 74 | |
| 75 | def validate_outer6(self, rx, encap_obj): |
| 76 | self.assertEqual(rx[IPv6].src, encap_obj.src_ip_s) |
| 77 | self.assertEqual(rx[IPv6].dst, encap_obj.dst_ip_s) |
| 78 | self.assertEqual(rx[UDP].sport, encap_obj.src_port) |
| 79 | self.assertEqual(rx[UDP].dport, encap_obj.dst_port) |
| 80 | |
| 81 | def validate_inner4(self, rx, tx, ttl=None): |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 82 | self.assertEqual(rx[IP].src, tx[IP].src) |
| 83 | self.assertEqual(rx[IP].dst, tx[IP].dst) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 84 | if ttl: |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 85 | self.assertEqual(rx[IP].ttl, ttl) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 86 | else: |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 87 | self.assertEqual(rx[IP].ttl, tx[IP].ttl) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 88 | |
| 89 | def validate_inner6(self, rx, tx): |
| 90 | self.assertEqual(rx.src, tx[IPv6].src) |
| 91 | self.assertEqual(rx.dst, tx[IPv6].dst) |
| 92 | self.assertEqual(rx.hlim, tx[IPv6].hlim) |
| 93 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 94 | def test_udp_encap(self): |
| 95 | """ UDP Encap test |
| 96 | """ |
| 97 | |
| 98 | # |
| 99 | # construct a UDP encap object through each of the peers |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 100 | # v4 through the first two peers, v6 through the second. |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 101 | # |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 102 | udp_encap_0 = VppUdpEncap(self, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 103 | self.pg0.local_ip4, |
| 104 | self.pg0.remote_ip4, |
| 105 | 330, 440) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 106 | udp_encap_1 = VppUdpEncap(self, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 107 | self.pg1.local_ip4, |
| 108 | self.pg1.remote_ip4, |
| 109 | 331, 441, |
| 110 | table_id=1) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 111 | udp_encap_2 = VppUdpEncap(self, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 112 | self.pg2.local_ip6, |
| 113 | self.pg2.remote_ip6, |
| 114 | 332, 442, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 115 | table_id=2) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 116 | udp_encap_3 = VppUdpEncap(self, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 117 | self.pg3.local_ip6, |
| 118 | self.pg3.remote_ip6, |
| 119 | 333, 443, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 120 | table_id=3) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 121 | udp_encap_0.add_vpp_config() |
| 122 | udp_encap_1.add_vpp_config() |
| 123 | udp_encap_2.add_vpp_config() |
| 124 | udp_encap_3.add_vpp_config() |
| 125 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 126 | self.logger.info(self.vapi.cli("sh udp encap")) |
| 127 | |
| 128 | self.assertTrue(find_udp_encap(self, udp_encap_2)) |
| 129 | self.assertTrue(find_udp_encap(self, udp_encap_3)) |
| 130 | self.assertTrue(find_udp_encap(self, udp_encap_0)) |
| 131 | self.assertTrue(find_udp_encap(self, udp_encap_1)) |
| 132 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 133 | # |
| 134 | # Routes via each UDP encap object - all combinations of v4 and v6. |
| 135 | # |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 136 | route_4o4 = VppIpRoute( |
| 137 | self, "1.1.0.1", 32, |
| 138 | [VppRoutePath("0.0.0.0", |
| 139 | 0xFFFFFFFF, |
| 140 | type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP, |
| 141 | next_hop_id=udp_encap_0.id)]) |
| 142 | route_4o6 = VppIpRoute( |
| 143 | self, "1.1.2.1", 32, |
| 144 | [VppRoutePath("0.0.0.0", |
| 145 | 0xFFFFFFFF, |
| 146 | type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP, |
| 147 | next_hop_id=udp_encap_2.id)]) |
| 148 | route_6o4 = VppIpRoute( |
| 149 | self, "2001::1", 128, |
| 150 | [VppRoutePath("0.0.0.0", |
| 151 | 0xFFFFFFFF, |
| 152 | type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP, |
| 153 | next_hop_id=udp_encap_1.id)]) |
| 154 | route_6o6 = VppIpRoute( |
| 155 | self, "2001::3", 128, |
| 156 | [VppRoutePath("0.0.0.0", |
| 157 | 0xFFFFFFFF, |
| 158 | type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP, |
| 159 | next_hop_id=udp_encap_3.id)]) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 160 | route_4o6.add_vpp_config() |
| 161 | route_6o6.add_vpp_config() |
| 162 | route_6o4.add_vpp_config() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 163 | route_4o4.add_vpp_config() |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 164 | |
| 165 | # |
| 166 | # 4o4 encap |
| 167 | # |
| 168 | p_4o4 = (Ether(src=self.pg0.remote_mac, |
| 169 | dst=self.pg0.local_mac) / |
| 170 | IP(src="2.2.2.2", dst="1.1.0.1") / |
| 171 | UDP(sport=1234, dport=1234) / |
| 172 | Raw('\xa5' * 100)) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 173 | rx = self.send_and_expect(self.pg0, p_4o4*NUM_PKTS, self.pg0) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 174 | for p in rx: |
| 175 | self.validate_outer4(p, udp_encap_0) |
| 176 | p = IP(p["UDP"].payload.load) |
| 177 | self.validate_inner4(p, p_4o4) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 178 | self.assertEqual(udp_encap_0.get_stats()['packets'], NUM_PKTS) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 179 | |
| 180 | # |
| 181 | # 4o6 encap |
| 182 | # |
| 183 | p_4o6 = (Ether(src=self.pg0.remote_mac, |
| 184 | dst=self.pg0.local_mac) / |
| 185 | IP(src="2.2.2.2", dst="1.1.2.1") / |
| 186 | UDP(sport=1234, dport=1234) / |
| 187 | Raw('\xa5' * 100)) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 188 | rx = self.send_and_expect(self.pg0, p_4o6*NUM_PKTS, self.pg2) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 189 | for p in rx: |
| 190 | self.validate_outer6(p, udp_encap_2) |
| 191 | p = IP(p["UDP"].payload.load) |
| 192 | self.validate_inner4(p, p_4o6) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 193 | self.assertEqual(udp_encap_2.get_stats()['packets'], NUM_PKTS) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 194 | |
| 195 | # |
| 196 | # 6o4 encap |
| 197 | # |
| 198 | p_6o4 = (Ether(src=self.pg0.remote_mac, |
| 199 | dst=self.pg0.local_mac) / |
| 200 | IPv6(src="2001::100", dst="2001::1") / |
| 201 | UDP(sport=1234, dport=1234) / |
| 202 | Raw('\xa5' * 100)) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 203 | rx = self.send_and_expect(self.pg0, p_6o4*NUM_PKTS, self.pg1) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 204 | for p in rx: |
| 205 | self.validate_outer4(p, udp_encap_1) |
| 206 | p = IPv6(p["UDP"].payload.load) |
| 207 | self.validate_inner6(p, p_6o4) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 208 | self.assertEqual(udp_encap_1.get_stats()['packets'], NUM_PKTS) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 209 | |
| 210 | # |
| 211 | # 6o6 encap |
| 212 | # |
| 213 | p_6o6 = (Ether(src=self.pg0.remote_mac, |
| 214 | dst=self.pg0.local_mac) / |
| 215 | IPv6(src="2001::100", dst="2001::3") / |
| 216 | UDP(sport=1234, dport=1234) / |
| 217 | Raw('\xa5' * 100)) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 218 | rx = self.send_and_expect(self.pg0, p_6o6*NUM_PKTS, self.pg3) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 219 | for p in rx: |
| 220 | self.validate_outer6(p, udp_encap_3) |
| 221 | p = IPv6(p["UDP"].payload.load) |
| 222 | self.validate_inner6(p, p_6o6) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 223 | self.assertEqual(udp_encap_3.get_stats()['packets'], NUM_PKTS) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 224 | |
| 225 | # |
| 226 | # A route with an output label |
| 227 | # the TTL of the inner packet is decremented on LSP ingress |
| 228 | # |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 229 | route_4oMPLSo4 = VppIpRoute( |
| 230 | self, "1.1.2.22", 32, |
| 231 | [VppRoutePath("0.0.0.0", |
| 232 | 0xFFFFFFFF, |
| 233 | type=FibPathType.FIB_PATH_TYPE_UDP_ENCAP, |
| 234 | next_hop_id=1, |
| 235 | labels=[VppMplsLabel(66)])]) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 236 | route_4oMPLSo4.add_vpp_config() |
| 237 | |
| 238 | p_4omo4 = (Ether(src=self.pg0.remote_mac, |
| 239 | dst=self.pg0.local_mac) / |
| 240 | IP(src="2.2.2.2", dst="1.1.2.22") / |
| 241 | UDP(sport=1234, dport=1234) / |
| 242 | Raw('\xa5' * 100)) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 243 | rx = self.send_and_expect(self.pg0, p_4omo4*NUM_PKTS, self.pg1) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 244 | for p in rx: |
| 245 | self.validate_outer4(p, udp_encap_1) |
| 246 | p = MPLS(p["UDP"].payload.load) |
| 247 | self.validate_inner4(p, p_4omo4, ttl=63) |
Paul Vinciguerra | 4271c97 | 2019-05-14 13:25:49 -0400 | [diff] [blame] | 248 | self.assertEqual(udp_encap_1.get_stats()['packets'], 2*NUM_PKTS) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 249 | |
| 250 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 251 | class TestUDP(VppTestCase): |
| 252 | """ UDP Test Case """ |
| 253 | |
| 254 | @classmethod |
| 255 | def setUpClass(cls): |
| 256 | super(TestUDP, cls).setUpClass() |
| 257 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 258 | @classmethod |
| 259 | def tearDownClass(cls): |
| 260 | super(TestUDP, cls).tearDownClass() |
| 261 | |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 262 | def setUp(self): |
| 263 | super(TestUDP, self).setUp() |
| 264 | self.vapi.session_enable_disable(is_enabled=1) |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 265 | self.create_loopback_interfaces(2) |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 266 | |
| 267 | table_id = 0 |
| 268 | |
| 269 | for i in self.lo_interfaces: |
| 270 | i.admin_up() |
| 271 | |
| 272 | if table_id != 0: |
| 273 | tbl = VppIpTable(self, table_id) |
| 274 | tbl.add_vpp_config() |
| 275 | |
| 276 | i.set_table_ip4(table_id) |
| 277 | i.config_ip4() |
| 278 | table_id += 1 |
| 279 | |
| 280 | # Configure namespaces |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 281 | self.vapi.app_namespace_add_del(namespace_id="0", |
| 282 | sw_if_index=self.loop0.sw_if_index) |
| 283 | self.vapi.app_namespace_add_del(namespace_id="1", |
| 284 | sw_if_index=self.loop1.sw_if_index) |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 285 | |
| 286 | def tearDown(self): |
| 287 | for i in self.lo_interfaces: |
| 288 | i.unconfig_ip4() |
| 289 | i.set_table_ip4(0) |
| 290 | i.admin_down() |
| 291 | self.vapi.session_enable_disable(is_enabled=0) |
| 292 | super(TestUDP, self).tearDown() |
| 293 | |
| 294 | def test_udp_transfer(self): |
| 295 | """ UDP echo client/server transfer """ |
| 296 | |
| 297 | # Add inter-table routes |
| 298 | ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32, |
| 299 | [VppRoutePath("0.0.0.0", |
| 300 | 0xffffffff, |
| 301 | nh_table_id=1)]) |
| 302 | ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32, |
| 303 | [VppRoutePath("0.0.0.0", |
| 304 | 0xffffffff, |
| 305 | nh_table_id=0)], table_id=1) |
| 306 | ip_t01.add_vpp_config() |
| 307 | ip_t10.add_vpp_config() |
| 308 | |
| 309 | # Start builtin server and client |
| 310 | uri = "udp://" + self.loop0.local_ip4 + "/1234" |
| 311 | error = self.vapi.cli("test echo server appns 0 fifo-size 4 no-echo" + |
| 312 | "uri " + uri) |
| 313 | if error: |
| 314 | self.logger.critical(error) |
Paul Vinciguerra | 9a6dafd | 2019-03-06 15:11:28 -0800 | [diff] [blame] | 315 | self.assertNotIn("failed", error) |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 316 | |
| 317 | error = self.vapi.cli("test echo client mbytes 10 appns 1 " + |
| 318 | "fifo-size 4 no-output test-bytes " + |
| 319 | "syn-timeout 2 no-return uri " + uri) |
| 320 | if error: |
| 321 | self.logger.critical(error) |
Paul Vinciguerra | 9a6dafd | 2019-03-06 15:11:28 -0800 | [diff] [blame] | 322 | self.assertNotIn("failed", error) |
Florin Coras | 40903ac | 2018-06-10 14:41:23 -0700 | [diff] [blame] | 323 | |
| 324 | # Delete inter-table routes |
| 325 | ip_t01.remove_vpp_config() |
| 326 | ip_t10.remove_vpp_config() |
| 327 | |
| 328 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 329 | if __name__ == '__main__': |
| 330 | unittest.main(testRunner=VppTestRunner) |