blob: 7cc6cdce48524b226f32e5e6be948cdf2e47e13d [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Pavel Kotucek15ac81c2017-06-20 14:00:26 +02002import random
3import unittest
4import datetime
5import re
6
7from scapy.packet import Raw
8from scapy.layers.l2 import Ether
9from scapy.layers.inet import IP, UDP
10from scapy.layers.inet6 import IPv6
11
Gabriel Ganne7e665d62017-11-17 09:18:53 +010012from framework import VppTestCase, VppTestRunner
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020013from vpp_sub_interface import VppP2PSubint
Neale Rannsc0a93142018-09-05 15:42:26 -070014from vpp_ip import DpoProto
15from vpp_ip_route import VppIpRoute, VppRoutePath
Ole Troan8006c6a2018-12-17 12:02:26 +010016from vpp_papi import mac_pton
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020017
18
19class P2PEthernetAPI(VppTestCase):
20 """P2P Ethernet tests"""
21
22 p2p_sub_ifs = []
23
24 @classmethod
25 def setUpClass(cls):
26 super(P2PEthernetAPI, cls).setUpClass()
27
28 # Create pg interfaces
29 cls.create_pg_interfaces(range(4))
30
31 # Set up all interfaces
32 for i in cls.pg_interfaces:
33 i.admin_up()
34
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070035 @classmethod
36 def tearDownClass(cls):
37 super(P2PEthernetAPI, cls).tearDownClass()
38
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020039 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
Ole Troan8006c6a2018-12-17 12:02:26 +010040 p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020041 self.p2p_sub_ifs.append(p2p)
42
43 def delete_p2p_ethernet(self, parent_if, remote_mac):
Ole Troane1ade682019-03-04 23:55:43 +010044 self.vapi.p2p_ethernet_del(parent_if.sw_if_index,
45 mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020046
47 def test_api(self):
48 """delete/create p2p subif"""
49 self.logger.info("FFP_TEST_START_0000")
50
51 self.create_p2p_ethernet(self.pg0, 1, "de:ad:00:00:00:01")
52 self.create_p2p_ethernet(self.pg0, 2, "de:ad:00:00:00:02")
53 intfs = self.vapi.cli("show interface")
54
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080055 self.assertIn('pg0.1', intfs)
56 self.assertIn('pg0.2', intfs)
57 self.assertNotIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020058
59 # create pg2.5 subif
60 self.create_p2p_ethernet(self.pg0, 5, "de:ad:00:00:00:ff")
61 intfs = self.vapi.cli("show interface")
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080062 self.assertIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020063 # delete pg2.5 subif
64 self.delete_p2p_ethernet(self.pg0, "de:ad:00:00:00:ff")
65
66 intfs = self.vapi.cli("show interface")
67
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080068 self.assertIn('pg0.1', intfs)
69 self.assertIn('pg0.2', intfs)
70 self.assertNotIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020071
72 self.logger.info("FFP_TEST_FINISH_0000")
73
74 def test_p2p_subif_creation_1k(self):
75 """create 1k of p2p subifs"""
76 self.logger.info("FFP_TEST_START_0001")
77
78 macs = []
79 clients = 1000
80 mac = int("dead00000000", 16)
81
82 for i in range(1, clients+1):
83 try:
Paul Vinciguerraea2450f2019-03-06 08:23:58 -080084 macs.append(':'.join(re.findall('..', '{:02x}'.format(
85 mac+i))))
Ole Troane1ade682019-03-04 23:55:43 +010086 self.vapi.p2p_ethernet_add(self.pg2.sw_if_index,
87 mac_pton(macs[i-1]),
88 i)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020089 except Exception:
Paul Vinciguerra661f91f2018-11-28 19:06:41 -080090 self.logger.info("Failed to create subif %d %s" % (
91 i, macs[i-1]))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020092 raise
93
94 intfs = self.vapi.cli("show interface").split("\n")
95 count = 0
96 for intf in intfs:
97 if intf.startswith('pg2.'):
98 count += 1
99 self.assertEqual(count, clients)
100
101 self.logger.info("FFP_TEST_FINISH_0001")
102
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200103
104class P2PEthernetIPV6(VppTestCase):
105 """P2P Ethernet IPv6 tests"""
106
107 p2p_sub_ifs = []
108 packets = []
109
110 @classmethod
111 def setUpClass(cls):
112 super(P2PEthernetIPV6, cls).setUpClass()
113
114 # Create pg interfaces
115 cls.create_pg_interfaces(range(3))
116
117 # Packet sizes
118 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
119
120 # Set up all interfaces
121 for i in cls.pg_interfaces:
122 i.admin_up()
123
124 cls.pg0.generate_remote_hosts(3)
125 cls.pg0.configure_ipv6_neighbors()
126
127 cls.pg1.config_ip6()
128 cls.pg1.generate_remote_hosts(3)
129 cls.pg1.configure_ipv6_neighbors()
130 cls.pg1.disable_ipv6_ra()
131
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700132 @classmethod
133 def tearDownClass(cls):
134 super(P2PEthernetIPV6, cls).tearDownClass()
135
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200136 def setUp(self):
137 super(P2PEthernetIPV6, self).setUp()
138 for p in self.packets:
139 self.packets.remove(p)
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700140 self.p2p_sub_ifs.append(
141 self.create_p2p_ethernet(self.pg0, 1,
142 self.pg0._remote_hosts[0].mac))
143 self.p2p_sub_ifs.append(
144 self.create_p2p_ethernet(self.pg0, 2,
145 self.pg0._remote_hosts[1].mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200146 self.vapi.cli("trace add p2p-ethernet-input 50")
147
148 def tearDown(self):
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700149 while len(self.p2p_sub_ifs):
150 p2p = self.p2p_sub_ifs.pop()
151 self.delete_p2p_ethernet(p2p)
152
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200153 super(P2PEthernetIPV6, self).tearDown()
154
155 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100156 p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200157 p2p.admin_up()
158 p2p.config_ip6()
159 p2p.disable_ipv6_ra()
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700160 return p2p
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200161
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700162 def delete_p2p_ethernet(self, p2p):
163 p2p.unconfig_ip6()
164 p2p.admin_down()
Ole Troane1ade682019-03-04 23:55:43 +0100165 self.vapi.p2p_ethernet_del(p2p.parent.sw_if_index,
166 p2p.p2p_remote_mac)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200167
168 def create_stream(self, src_mac=None, dst_mac=None,
169 src_ip=None, dst_ip=None, size=None):
170 pkt_size = size
171 if size is None:
172 pkt_size = random.choice(self.pg_if_packet_sizes)
173 p = Ether(src=src_mac, dst=dst_mac)
174 p /= IPv6(src=src_ip, dst=dst_ip)
Ole Troan770a0de2019-11-07 13:52:21 +0100175 p /= (UDP(sport=1234, dport=4321) / Raw(b'\xa5' * 20))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200176 self.extend_packet(p, pkt_size)
177 return p
178
179 def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
180 self.pg_enable_capture([dst_if])
181 if packets is None:
182 packets = self.packets
183 src_if.add_stream(packets)
184 self.pg_start()
185 if count is None:
186 count = len(packets)
187 return dst_if.get_capture(count)
188
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200189 def test_no_p2p_subif(self):
190 """standard routing without p2p subinterfaces"""
191 self.logger.info("FFP_TEST_START_0001")
192
193 route_8000 = VppIpRoute(self, "8000::", 64,
194 [VppRoutePath(self.pg0.remote_ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700195 self.pg0.sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200196 route_8000.add_vpp_config()
197
198 self.packets = [(Ether(dst=self.pg1.local_mac,
199 src=self.pg1.remote_mac) /
200 IPv6(src="3001::1", dst="8000::100") /
201 UDP(sport=1234, dport=1234) /
Ole Troan770a0de2019-11-07 13:52:21 +0100202 Raw(b'\xa5' * 100))]
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200203 self.send_packets(self.pg1, self.pg0)
204
205 self.logger.info("FFP_TEST_FINISH_0001")
206
207 def test_ip6_rx_p2p_subif(self):
208 """receive ipv6 packet via p2p subinterface"""
209 self.logger.info("FFP_TEST_START_0002")
210
211 route_9001 = VppIpRoute(self, "9001::", 64,
212 [VppRoutePath(self.pg1.remote_ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700213 self.pg1.sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200214 route_9001.add_vpp_config()
215
216 self.packets.append(
217 self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
218 dst_mac=self.pg0.local_mac,
219 src_ip=self.p2p_sub_ifs[0].remote_ip6,
220 dst_ip="9001::100"))
221
222 self.send_packets(self.pg0, self.pg1, self.packets)
Klement Sekeraf37c3ba2018-11-08 11:24:34 +0100223 self.assert_packet_counter_equal('p2p-ethernet-input', 1)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200224
225 route_9001.remove_vpp_config()
226 self.logger.info("FFP_TEST_FINISH_0002")
227
228 def test_ip6_rx_p2p_subif_route(self):
229 """route rx ip6 packet not matching p2p subinterface"""
230 self.logger.info("FFP_TEST_START_0003")
231
232 self.pg0.config_ip6()
233
234 route_3 = VppIpRoute(self, "9000::", 64,
235 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700236 self.pg1.sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200237 route_3.add_vpp_config()
238
239 self.packets.append(
240 self.create_stream(src_mac="02:03:00:00:ff:ff",
241 dst_mac=self.pg0.local_mac,
242 src_ip="a000::100",
243 dst_ip="9000::100"))
244
245 self.send_packets(self.pg0, self.pg1)
246
247 self.pg0.unconfig_ip6()
248
249 route_3.remove_vpp_config()
250
251 self.logger.info("FFP_TEST_FINISH_0003")
252
253 def test_ip6_rx_p2p_subif_drop(self):
254 """drop rx packet not matching p2p subinterface"""
255 self.logger.info("FFP_TEST_START_0004")
256
257 route_9001 = VppIpRoute(self, "9000::", 64,
258 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700259 self.pg1.sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200260 route_9001.add_vpp_config()
261
262 self.packets.append(
263 self.create_stream(src_mac="02:03:00:00:ff:ff",
264 dst_mac=self.pg0.local_mac,
265 src_ip="a000::100",
266 dst_ip="9000::100"))
267
268 # no packet received
269 self.send_packets(self.pg0, self.pg1, count=0)
270 self.logger.info("FFP_TEST_FINISH_0004")
271
272 def test_ip6_tx_p2p_subif(self):
273 """send packet via p2p subinterface"""
274 self.logger.info("FFP_TEST_START_0005")
275
276 route_8000 = VppIpRoute(self, "8000::", 64,
277 [VppRoutePath(self.pg0.remote_ip6,
Neale Ranns097fa662018-05-01 05:17:55 -0700278 self.pg0.sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200279 route_8000.add_vpp_config()
280 route_8001 = VppIpRoute(self, "8001::", 64,
Neale Ranns097fa662018-05-01 05:17:55 -0700281 [VppRoutePath(
282 self.p2p_sub_ifs[0].remote_ip6,
283 self.p2p_sub_ifs[0].sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200284 route_8001.add_vpp_config()
285 route_8002 = VppIpRoute(self, "8002::", 64,
Neale Ranns097fa662018-05-01 05:17:55 -0700286 [VppRoutePath(
287 self.p2p_sub_ifs[1].remote_ip6,
288 self.p2p_sub_ifs[1].sw_if_index)])
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200289 route_8002.add_vpp_config()
290
291 for i in range(0, 3):
292 self.packets.append(
293 self.create_stream(src_mac=self.pg1.remote_mac,
294 dst_mac=self.pg1.local_mac,
295 src_ip=self.pg1.remote_ip6,
296 dst_ip="800%d::100" % i))
297
298 self.send_packets(self.pg1, self.pg0, count=3)
299
300 route_8000.remove_vpp_config()
301 route_8001.remove_vpp_config()
302 route_8002.remove_vpp_config()
303
304 self.logger.info("FFP_TEST_FINISH_0005")
305
306 def test_ip6_tx_p2p_subif_drop(self):
307 """drop tx ip6 packet not matching p2p subinterface"""
308 self.logger.info("FFP_TEST_START_0006")
309
310 self.packets.append(
311 self.create_stream(src_mac="02:03:00:00:ff:ff",
312 dst_mac=self.pg0.local_mac,
313 src_ip="a000::100",
314 dst_ip="9000::100"))
315
316 # no packet received
317 self.send_packets(self.pg0, self.pg1, count=0)
318 self.logger.info("FFP_TEST_FINISH_0006")
319
320
321class P2PEthernetIPV4(VppTestCase):
322 """P2P Ethernet IPv4 tests"""
323
324 p2p_sub_ifs = []
325 packets = []
326
327 @classmethod
328 def setUpClass(cls):
329 super(P2PEthernetIPV4, cls).setUpClass()
330
331 # Create pg interfaces
332 cls.create_pg_interfaces(range(3))
333
334 # Packet sizes
335 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
336
337 # Set up all interfaces
338 for i in cls.pg_interfaces:
339 i.admin_up()
340
341 cls.pg0.config_ip4()
342 cls.pg0.generate_remote_hosts(5)
343 cls.pg0.configure_ipv4_neighbors()
344
345 cls.pg1.config_ip4()
346 cls.pg1.generate_remote_hosts(5)
347 cls.pg1.configure_ipv4_neighbors()
348
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700349 @classmethod
350 def tearDownClass(cls):
351 super(P2PEthernetIPV4, cls).tearDownClass()
352
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200353 def setUp(self):
354 super(P2PEthernetIPV4, self).setUp()
355 for p in self.packets:
356 self.packets.remove(p)
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700357 self.p2p_sub_ifs.append(
358 self.create_p2p_ethernet(self.pg0, 1,
359 self.pg0._remote_hosts[0].mac))
360 self.p2p_sub_ifs.append(
361 self.create_p2p_ethernet(self.pg0, 2,
362 self.pg0._remote_hosts[1].mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200363 self.vapi.cli("trace add p2p-ethernet-input 50")
364
365 def tearDown(self):
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700366 while len(self.p2p_sub_ifs):
367 p2p = self.p2p_sub_ifs.pop()
368 self.delete_p2p_ethernet(p2p)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200369 super(P2PEthernetIPV4, self).tearDown()
370
371 def create_stream(self, src_mac=None, dst_mac=None,
372 src_ip=None, dst_ip=None, size=None):
373 pkt_size = size
374 if size is None:
375 pkt_size = random.choice(self.pg_if_packet_sizes)
376 p = Ether(src=src_mac, dst=dst_mac)
377 p /= IP(src=src_ip, dst=dst_ip)
Ole Troan770a0de2019-11-07 13:52:21 +0100378 p /= (UDP(sport=1234, dport=4321) / Raw(b'\xa5' * 20))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200379 self.extend_packet(p, pkt_size)
380 return p
381
382 def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
383 self.pg_enable_capture([dst_if])
384 if packets is None:
385 packets = self.packets
386 src_if.add_stream(packets)
387 self.pg_start()
388 if count is None:
389 count = len(packets)
390 return dst_if.get_capture(count)
391
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200392 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100393 p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200394 p2p.admin_up()
395 p2p.config_ip4()
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700396 return p2p
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200397
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700398 def delete_p2p_ethernet(self, p2p):
399 p2p.unconfig_ip4()
400 p2p.admin_down()
Ole Troane1ade682019-03-04 23:55:43 +0100401 self.vapi.p2p_ethernet_del(p2p.parent.sw_if_index,
402 p2p.p2p_remote_mac)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200403
404 def test_ip4_rx_p2p_subif(self):
405 """receive ipv4 packet via p2p subinterface"""
406 self.logger.info("FFP_TEST_START_0002")
407
408 route_9000 = VppIpRoute(self, "9.0.0.0", 16,
409 [VppRoutePath(self.pg1.remote_ip4,
410 self.pg1.sw_if_index)])
411 route_9000.add_vpp_config()
412
413 self.packets.append(
414 self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
415 dst_mac=self.pg0.local_mac,
416 src_ip=self.p2p_sub_ifs[0].remote_ip4,
417 dst_ip="9.0.0.100"))
418
419 self.send_packets(self.pg0, self.pg1, self.packets)
420
Klement Sekeraf37c3ba2018-11-08 11:24:34 +0100421 self.assert_packet_counter_equal('p2p-ethernet-input', 1)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200422
423 route_9000.remove_vpp_config()
424 self.logger.info("FFP_TEST_FINISH_0002")
425
426 def test_ip4_rx_p2p_subif_route(self):
427 """route rx packet not matching p2p subinterface"""
428 self.logger.info("FFP_TEST_START_0003")
429
430 route_9001 = VppIpRoute(self, "9.0.0.0", 24,
431 [VppRoutePath(self.pg1.remote_ip4,
432 self.pg1.sw_if_index)])
433 route_9001.add_vpp_config()
434
435 self.packets.append(
436 self.create_stream(src_mac="02:01:00:00:ff:ff",
437 dst_mac=self.pg0.local_mac,
438 src_ip="8.0.0.100",
439 dst_ip="9.0.0.100"))
440
441 self.send_packets(self.pg0, self.pg1)
442
443 route_9001.remove_vpp_config()
444
445 self.logger.info("FFP_TEST_FINISH_0003")
446
447 def test_ip4_tx_p2p_subif(self):
448 """send ip4 packet via p2p subinterface"""
449 self.logger.info("FFP_TEST_START_0005")
450
451 route_9100 = VppIpRoute(self, "9.1.0.100", 24,
452 [VppRoutePath(self.pg0.remote_ip4,
453 self.pg0.sw_if_index,
454 )])
455 route_9100.add_vpp_config()
456 route_9200 = VppIpRoute(self, "9.2.0.100", 24,
457 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip4,
458 self.p2p_sub_ifs[0].sw_if_index,
459 )])
460 route_9200.add_vpp_config()
461 route_9300 = VppIpRoute(self, "9.3.0.100", 24,
462 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip4,
463 self.p2p_sub_ifs[1].sw_if_index
464 )])
465 route_9300.add_vpp_config()
466
467 for i in range(0, 3):
468 self.packets.append(
469 self.create_stream(src_mac=self.pg1.remote_mac,
470 dst_mac=self.pg1.local_mac,
471 src_ip=self.pg1.remote_ip4,
472 dst_ip="9.%d.0.100" % (i+1)))
473
474 self.send_packets(self.pg1, self.pg0)
475
476 # route_7000.remove_vpp_config()
477 route_9100.remove_vpp_config()
478 route_9200.remove_vpp_config()
479 route_9300.remove_vpp_config()
480
481 self.logger.info("FFP_TEST_FINISH_0005")
482
483 def test_ip4_tx_p2p_subif_drop(self):
484 """drop tx ip4 packet not matching p2p subinterface"""
485 self.logger.info("FFP_TEST_START_0006")
486
487 self.packets.append(
488 self.create_stream(src_mac="02:01:00:00:ff:ff",
489 dst_mac=self.pg0.local_mac,
490 src_ip="8.0.0.100",
491 dst_ip="9.0.0.100"))
492
493 # no packet received
494 self.send_packets(self.pg0, self.pg1, count=0)
495 self.logger.info("FFP_TEST_FINISH_0006")
496
497
498if __name__ == '__main__':
499 unittest.main(testRunner=VppTestRunner)