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