blob: 5200e482b04214324c4e947d4d58b02054594349 [file] [log] [blame]
Pavel Kotucek15ac81c2017-06-20 14:00:26 +02001#!/usr/bin/env python
2import 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
35 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
Ole Troan8006c6a2018-12-17 12:02:26 +010036 p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020037 self.p2p_sub_ifs.append(p2p)
38
39 def delete_p2p_ethernet(self, parent_if, remote_mac):
Ole Troane1ade682019-03-04 23:55:43 +010040 self.vapi.p2p_ethernet_del(parent_if.sw_if_index,
41 mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020042
43 def test_api(self):
44 """delete/create p2p subif"""
45 self.logger.info("FFP_TEST_START_0000")
46
47 self.create_p2p_ethernet(self.pg0, 1, "de:ad:00:00:00:01")
48 self.create_p2p_ethernet(self.pg0, 2, "de:ad:00:00:00:02")
49 intfs = self.vapi.cli("show interface")
50
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080051 self.assertIn('pg0.1', intfs)
52 self.assertIn('pg0.2', intfs)
53 self.assertNotIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020054
55 # create pg2.5 subif
56 self.create_p2p_ethernet(self.pg0, 5, "de:ad:00:00:00:ff")
57 intfs = self.vapi.cli("show interface")
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080058 self.assertIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020059 # delete pg2.5 subif
60 self.delete_p2p_ethernet(self.pg0, "de:ad:00:00:00:ff")
61
62 intfs = self.vapi.cli("show interface")
63
Paul Vinciguerra9a6dafd2019-03-06 15:11:28 -080064 self.assertIn('pg0.1', intfs)
65 self.assertIn('pg0.2', intfs)
66 self.assertNotIn('pg0.5', intfs)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020067
68 self.logger.info("FFP_TEST_FINISH_0000")
69
70 def test_p2p_subif_creation_1k(self):
71 """create 1k of p2p subifs"""
72 self.logger.info("FFP_TEST_START_0001")
73
74 macs = []
75 clients = 1000
76 mac = int("dead00000000", 16)
77
78 for i in range(1, clients+1):
79 try:
Paul Vinciguerraea2450f2019-03-06 08:23:58 -080080 macs.append(':'.join(re.findall('..', '{:02x}'.format(
81 mac+i))))
Ole Troane1ade682019-03-04 23:55:43 +010082 self.vapi.p2p_ethernet_add(self.pg2.sw_if_index,
83 mac_pton(macs[i-1]),
84 i)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020085 except Exception:
Paul Vinciguerra661f91f2018-11-28 19:06:41 -080086 self.logger.info("Failed to create subif %d %s" % (
87 i, macs[i-1]))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020088 raise
89
90 intfs = self.vapi.cli("show interface").split("\n")
91 count = 0
92 for intf in intfs:
93 if intf.startswith('pg2.'):
94 count += 1
95 self.assertEqual(count, clients)
96
97 self.logger.info("FFP_TEST_FINISH_0001")
98
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020099
100class P2PEthernetIPV6(VppTestCase):
101 """P2P Ethernet IPv6 tests"""
102
103 p2p_sub_ifs = []
104 packets = []
105
106 @classmethod
107 def setUpClass(cls):
108 super(P2PEthernetIPV6, cls).setUpClass()
109
110 # Create pg interfaces
111 cls.create_pg_interfaces(range(3))
112
113 # Packet sizes
114 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
115
116 # Set up all interfaces
117 for i in cls.pg_interfaces:
118 i.admin_up()
119
120 cls.pg0.generate_remote_hosts(3)
121 cls.pg0.configure_ipv6_neighbors()
122
123 cls.pg1.config_ip6()
124 cls.pg1.generate_remote_hosts(3)
125 cls.pg1.configure_ipv6_neighbors()
126 cls.pg1.disable_ipv6_ra()
127
128 def setUp(self):
129 super(P2PEthernetIPV6, self).setUp()
130 for p in self.packets:
131 self.packets.remove(p)
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700132 self.p2p_sub_ifs.append(
133 self.create_p2p_ethernet(self.pg0, 1,
134 self.pg0._remote_hosts[0].mac))
135 self.p2p_sub_ifs.append(
136 self.create_p2p_ethernet(self.pg0, 2,
137 self.pg0._remote_hosts[1].mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200138 self.vapi.cli("trace add p2p-ethernet-input 50")
139
140 def tearDown(self):
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700141 while len(self.p2p_sub_ifs):
142 p2p = self.p2p_sub_ifs.pop()
143 self.delete_p2p_ethernet(p2p)
144
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200145 super(P2PEthernetIPV6, self).tearDown()
146
147 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100148 p2p = VppP2PSubint(self, parent_if, sub_id, mac_pton(remote_mac))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200149 p2p.admin_up()
150 p2p.config_ip6()
151 p2p.disable_ipv6_ra()
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700152 return p2p
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200153
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700154 def delete_p2p_ethernet(self, p2p):
155 p2p.unconfig_ip6()
156 p2p.admin_down()
Ole Troane1ade682019-03-04 23:55:43 +0100157 self.vapi.p2p_ethernet_del(p2p.parent.sw_if_index,
158 p2p.p2p_remote_mac)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200159
160 def create_stream(self, src_mac=None, dst_mac=None,
161 src_ip=None, dst_ip=None, size=None):
162 pkt_size = size
163 if size is None:
164 pkt_size = random.choice(self.pg_if_packet_sizes)
165 p = Ether(src=src_mac, dst=dst_mac)
166 p /= IPv6(src=src_ip, dst=dst_ip)
167 p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
168 self.extend_packet(p, pkt_size)
169 return p
170
171 def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
172 self.pg_enable_capture([dst_if])
173 if packets is None:
174 packets = self.packets
175 src_if.add_stream(packets)
176 self.pg_start()
177 if count is None:
178 count = len(packets)
179 return dst_if.get_capture(count)
180
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200181 def test_no_p2p_subif(self):
182 """standard routing without p2p subinterfaces"""
183 self.logger.info("FFP_TEST_START_0001")
184
185 route_8000 = VppIpRoute(self, "8000::", 64,
186 [VppRoutePath(self.pg0.remote_ip6,
187 self.pg0.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700188 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200189 is_ip6=1)
190 route_8000.add_vpp_config()
191
192 self.packets = [(Ether(dst=self.pg1.local_mac,
193 src=self.pg1.remote_mac) /
194 IPv6(src="3001::1", dst="8000::100") /
195 UDP(sport=1234, dport=1234) /
196 Raw('\xa5' * 100))]
197 self.send_packets(self.pg1, self.pg0)
198
199 self.logger.info("FFP_TEST_FINISH_0001")
200
201 def test_ip6_rx_p2p_subif(self):
202 """receive ipv6 packet via p2p subinterface"""
203 self.logger.info("FFP_TEST_START_0002")
204
205 route_9001 = VppIpRoute(self, "9001::", 64,
206 [VppRoutePath(self.pg1.remote_ip6,
207 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700208 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200209 is_ip6=1)
210 route_9001.add_vpp_config()
211
212 self.packets.append(
213 self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
214 dst_mac=self.pg0.local_mac,
215 src_ip=self.p2p_sub_ifs[0].remote_ip6,
216 dst_ip="9001::100"))
217
218 self.send_packets(self.pg0, self.pg1, self.packets)
Klement Sekeraf37c3ba2018-11-08 11:24:34 +0100219 self.assert_packet_counter_equal('p2p-ethernet-input', 1)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200220
221 route_9001.remove_vpp_config()
222 self.logger.info("FFP_TEST_FINISH_0002")
223
224 def test_ip6_rx_p2p_subif_route(self):
225 """route rx ip6 packet not matching p2p subinterface"""
226 self.logger.info("FFP_TEST_START_0003")
227
228 self.pg0.config_ip6()
229
230 route_3 = VppIpRoute(self, "9000::", 64,
231 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
232 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700233 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200234 is_ip6=1)
235 route_3.add_vpp_config()
236
237 self.packets.append(
238 self.create_stream(src_mac="02:03:00:00:ff:ff",
239 dst_mac=self.pg0.local_mac,
240 src_ip="a000::100",
241 dst_ip="9000::100"))
242
243 self.send_packets(self.pg0, self.pg1)
244
245 self.pg0.unconfig_ip6()
246
247 route_3.remove_vpp_config()
248
249 self.logger.info("FFP_TEST_FINISH_0003")
250
251 def test_ip6_rx_p2p_subif_drop(self):
252 """drop rx packet not matching p2p subinterface"""
253 self.logger.info("FFP_TEST_START_0004")
254
255 route_9001 = VppIpRoute(self, "9000::", 64,
256 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
257 self.pg1.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700258 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200259 is_ip6=1)
260 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,
278 self.pg0.sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700279 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200280 is_ip6=1)
281 route_8000.add_vpp_config()
282 route_8001 = VppIpRoute(self, "8001::", 64,
283 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip6,
284 self.p2p_sub_ifs[0].sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700285 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200286 is_ip6=1)
287 route_8001.add_vpp_config()
288 route_8002 = VppIpRoute(self, "8002::", 64,
289 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip6,
290 self.p2p_sub_ifs[1].sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700291 proto=DpoProto.DPO_PROTO_IP6)],
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200292 is_ip6=1)
293 route_8002.add_vpp_config()
294
295 for i in range(0, 3):
296 self.packets.append(
297 self.create_stream(src_mac=self.pg1.remote_mac,
298 dst_mac=self.pg1.local_mac,
299 src_ip=self.pg1.remote_ip6,
300 dst_ip="800%d::100" % i))
301
302 self.send_packets(self.pg1, self.pg0, count=3)
303
304 route_8000.remove_vpp_config()
305 route_8001.remove_vpp_config()
306 route_8002.remove_vpp_config()
307
308 self.logger.info("FFP_TEST_FINISH_0005")
309
310 def test_ip6_tx_p2p_subif_drop(self):
311 """drop tx ip6 packet not matching p2p subinterface"""
312 self.logger.info("FFP_TEST_START_0006")
313
314 self.packets.append(
315 self.create_stream(src_mac="02:03:00:00:ff:ff",
316 dst_mac=self.pg0.local_mac,
317 src_ip="a000::100",
318 dst_ip="9000::100"))
319
320 # no packet received
321 self.send_packets(self.pg0, self.pg1, count=0)
322 self.logger.info("FFP_TEST_FINISH_0006")
323
324
325class P2PEthernetIPV4(VppTestCase):
326 """P2P Ethernet IPv4 tests"""
327
328 p2p_sub_ifs = []
329 packets = []
330
331 @classmethod
332 def setUpClass(cls):
333 super(P2PEthernetIPV4, cls).setUpClass()
334
335 # Create pg interfaces
336 cls.create_pg_interfaces(range(3))
337
338 # Packet sizes
339 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
340
341 # Set up all interfaces
342 for i in cls.pg_interfaces:
343 i.admin_up()
344
345 cls.pg0.config_ip4()
346 cls.pg0.generate_remote_hosts(5)
347 cls.pg0.configure_ipv4_neighbors()
348
349 cls.pg1.config_ip4()
350 cls.pg1.generate_remote_hosts(5)
351 cls.pg1.configure_ipv4_neighbors()
352
353 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)
378 p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
379 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)