7 from scapy.packet import Raw
8 from scapy.layers.l2 import Ether
9 from scapy.layers.inet import IP, UDP
10 from scapy.layers.inet6 import IPv6
12 from framework import VppTestCase, VppTestRunner
13 from vpp_sub_interface import VppP2PSubint
14 from vpp_ip_route import VppIpRoute, VppRoutePath, DpoProto
15 from util import mactobinary
18 class P2PEthernetAPI(VppTestCase):
19 """P2P Ethernet tests"""
25 super(P2PEthernetAPI, cls).setUpClass()
27 # Create pg interfaces
28 cls.create_pg_interfaces(range(4))
30 # Set up all interfaces
31 for i in cls.pg_interfaces:
34 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
35 p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
36 self.p2p_sub_ifs.append(p2p)
38 def delete_p2p_ethernet(self, parent_if, remote_mac):
39 self.vapi.delete_p2pethernet_subif(parent_if.sw_if_index,
40 mactobinary(remote_mac))
43 """delete/create p2p subif"""
44 self.logger.info("FFP_TEST_START_0000")
46 self.create_p2p_ethernet(self.pg0, 1, "de:ad:00:00:00:01")
47 self.create_p2p_ethernet(self.pg0, 2, "de:ad:00:00:00:02")
48 intfs = self.vapi.cli("show interface")
50 self.assertNotEqual(intfs.find('pg0.1'), -1)
51 self.assertNotEqual(intfs.find('pg0.2'), -1)
52 self.assertEqual(intfs.find('pg0.5'), -1)
55 self.create_p2p_ethernet(self.pg0, 5, "de:ad:00:00:00:ff")
56 intfs = self.vapi.cli("show interface")
57 self.assertNotEqual(intfs.find('pg0.5'), -1)
59 self.delete_p2p_ethernet(self.pg0, "de:ad:00:00:00:ff")
61 intfs = self.vapi.cli("show interface")
63 self.assertNotEqual(intfs.find('pg0.1'), -1)
64 self.assertNotEqual(intfs.find('pg0.2'), -1)
65 self.assertEqual(intfs.find('pg0.5'), -1)
67 self.logger.info("FFP_TEST_FINISH_0000")
69 def test_p2p_subif_creation_1k(self):
70 """create 1k of p2p subifs"""
71 self.logger.info("FFP_TEST_START_0001")
75 mac = int("dead00000000", 16)
77 for i in range(1, clients+1):
79 macs.append(':'.join(re.findall('..', '{:02x}'.format(mac+i))))
80 self.vapi.create_p2pethernet_subif(self.pg2.sw_if_index,
81 mactobinary(macs[i-1]),
84 print "Failed to create subif %d %s" % (i, macs[i-1])
87 intfs = self.vapi.cli("show interface").split("\n")
90 if intf.startswith('pg2.'):
92 self.assertEqual(count, clients)
94 self.logger.info("FFP_TEST_FINISH_0001")
97 class P2PEthernetIPV6(VppTestCase):
98 """P2P Ethernet IPv6 tests"""
105 super(P2PEthernetIPV6, cls).setUpClass()
107 # Create pg interfaces
108 cls.create_pg_interfaces(range(3))
111 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
113 # Set up all interfaces
114 for i in cls.pg_interfaces:
117 cls.pg0.generate_remote_hosts(3)
118 cls.pg0.configure_ipv6_neighbors()
121 cls.pg1.generate_remote_hosts(3)
122 cls.pg1.configure_ipv6_neighbors()
123 cls.pg1.disable_ipv6_ra()
126 super(P2PEthernetIPV6, self).setUp()
127 for p in self.packets:
128 self.packets.remove(p)
129 self.p2p_sub_ifs.append(
130 self.create_p2p_ethernet(self.pg0, 1,
131 self.pg0._remote_hosts[0].mac))
132 self.p2p_sub_ifs.append(
133 self.create_p2p_ethernet(self.pg0, 2,
134 self.pg0._remote_hosts[1].mac))
135 self.vapi.cli("trace add p2p-ethernet-input 50")
138 while len(self.p2p_sub_ifs):
139 p2p = self.p2p_sub_ifs.pop()
140 self.delete_p2p_ethernet(p2p)
142 super(P2PEthernetIPV6, self).tearDown()
144 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
145 p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
148 p2p.disable_ipv6_ra()
151 def delete_p2p_ethernet(self, p2p):
154 self.vapi.delete_p2pethernet_subif(p2p.parent.sw_if_index,
157 def create_stream(self, src_mac=None, dst_mac=None,
158 src_ip=None, dst_ip=None, size=None):
161 pkt_size = random.choice(self.pg_if_packet_sizes)
162 p = Ether(src=src_mac, dst=dst_mac)
163 p /= IPv6(src=src_ip, dst=dst_ip)
164 p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
165 self.extend_packet(p, pkt_size)
168 def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
169 self.pg_enable_capture([dst_if])
171 packets = self.packets
172 src_if.add_stream(packets)
176 return dst_if.get_capture(count)
178 def verify_counters(self, counter_id, expected_value):
179 counters = self.vapi.cli("sh errors").split('\n')
181 for i in range(1, len(counters)-1):
182 results = counters[i].split()
183 if results[1] == counter_id:
184 counter_value = int(results[0])
186 self.assertEqual(counter_value, expected_value)
188 def test_no_p2p_subif(self):
189 """standard routing without p2p subinterfaces"""
190 self.logger.info("FFP_TEST_START_0001")
192 route_8000 = VppIpRoute(self, "8000::", 64,
193 [VppRoutePath(self.pg0.remote_ip6,
194 self.pg0.sw_if_index,
195 proto=DpoProto.DPO_PROTO_IP6)],
197 route_8000.add_vpp_config()
199 self.packets = [(Ether(dst=self.pg1.local_mac,
200 src=self.pg1.remote_mac) /
201 IPv6(src="3001::1", dst="8000::100") /
202 UDP(sport=1234, dport=1234) /
204 self.send_packets(self.pg1, self.pg0)
206 self.logger.info("FFP_TEST_FINISH_0001")
208 def test_ip6_rx_p2p_subif(self):
209 """receive ipv6 packet via p2p subinterface"""
210 self.logger.info("FFP_TEST_START_0002")
212 route_9001 = VppIpRoute(self, "9001::", 64,
213 [VppRoutePath(self.pg1.remote_ip6,
214 self.pg1.sw_if_index,
215 proto=DpoProto.DPO_PROTO_IP6)],
217 route_9001.add_vpp_config()
220 self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
221 dst_mac=self.pg0.local_mac,
222 src_ip=self.p2p_sub_ifs[0].remote_ip6,
225 self.send_packets(self.pg0, self.pg1, self.packets)
226 self.verify_counters('p2p-ethernet-input', 1)
228 route_9001.remove_vpp_config()
229 self.logger.info("FFP_TEST_FINISH_0002")
231 def test_ip6_rx_p2p_subif_route(self):
232 """route rx ip6 packet not matching p2p subinterface"""
233 self.logger.info("FFP_TEST_START_0003")
235 self.pg0.config_ip6()
237 route_3 = VppIpRoute(self, "9000::", 64,
238 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
239 self.pg1.sw_if_index,
240 proto=DpoProto.DPO_PROTO_IP6)],
242 route_3.add_vpp_config()
245 self.create_stream(src_mac="02:03:00:00:ff:ff",
246 dst_mac=self.pg0.local_mac,
250 self.send_packets(self.pg0, self.pg1)
252 self.pg0.unconfig_ip6()
254 route_3.remove_vpp_config()
256 self.logger.info("FFP_TEST_FINISH_0003")
258 def test_ip6_rx_p2p_subif_drop(self):
259 """drop rx packet not matching p2p subinterface"""
260 self.logger.info("FFP_TEST_START_0004")
262 route_9001 = VppIpRoute(self, "9000::", 64,
263 [VppRoutePath(self.pg1._remote_hosts[0].ip6,
264 self.pg1.sw_if_index,
265 proto=DpoProto.DPO_PROTO_IP6)],
267 route_9001.add_vpp_config()
270 self.create_stream(src_mac="02:03:00:00:ff:ff",
271 dst_mac=self.pg0.local_mac,
276 self.send_packets(self.pg0, self.pg1, count=0)
277 self.logger.info("FFP_TEST_FINISH_0004")
279 def test_ip6_tx_p2p_subif(self):
280 """send packet via p2p subinterface"""
281 self.logger.info("FFP_TEST_START_0005")
283 route_8000 = VppIpRoute(self, "8000::", 64,
284 [VppRoutePath(self.pg0.remote_ip6,
285 self.pg0.sw_if_index,
286 proto=DpoProto.DPO_PROTO_IP6)],
288 route_8000.add_vpp_config()
289 route_8001 = VppIpRoute(self, "8001::", 64,
290 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip6,
291 self.p2p_sub_ifs[0].sw_if_index,
292 proto=DpoProto.DPO_PROTO_IP6)],
294 route_8001.add_vpp_config()
295 route_8002 = VppIpRoute(self, "8002::", 64,
296 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip6,
297 self.p2p_sub_ifs[1].sw_if_index,
298 proto=DpoProto.DPO_PROTO_IP6)],
300 route_8002.add_vpp_config()
302 for i in range(0, 3):
304 self.create_stream(src_mac=self.pg1.remote_mac,
305 dst_mac=self.pg1.local_mac,
306 src_ip=self.pg1.remote_ip6,
307 dst_ip="800%d::100" % i))
309 self.send_packets(self.pg1, self.pg0, count=3)
311 route_8000.remove_vpp_config()
312 route_8001.remove_vpp_config()
313 route_8002.remove_vpp_config()
315 self.logger.info("FFP_TEST_FINISH_0005")
317 def test_ip6_tx_p2p_subif_drop(self):
318 """drop tx ip6 packet not matching p2p subinterface"""
319 self.logger.info("FFP_TEST_START_0006")
322 self.create_stream(src_mac="02:03:00:00:ff:ff",
323 dst_mac=self.pg0.local_mac,
328 self.send_packets(self.pg0, self.pg1, count=0)
329 self.logger.info("FFP_TEST_FINISH_0006")
332 class P2PEthernetIPV4(VppTestCase):
333 """P2P Ethernet IPv4 tests"""
340 super(P2PEthernetIPV4, cls).setUpClass()
342 # Create pg interfaces
343 cls.create_pg_interfaces(range(3))
346 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
348 # Set up all interfaces
349 for i in cls.pg_interfaces:
353 cls.pg0.generate_remote_hosts(5)
354 cls.pg0.configure_ipv4_neighbors()
357 cls.pg1.generate_remote_hosts(5)
358 cls.pg1.configure_ipv4_neighbors()
361 super(P2PEthernetIPV4, self).setUp()
362 for p in self.packets:
363 self.packets.remove(p)
364 self.p2p_sub_ifs.append(
365 self.create_p2p_ethernet(self.pg0, 1,
366 self.pg0._remote_hosts[0].mac))
367 self.p2p_sub_ifs.append(
368 self.create_p2p_ethernet(self.pg0, 2,
369 self.pg0._remote_hosts[1].mac))
370 self.vapi.cli("trace add p2p-ethernet-input 50")
373 while len(self.p2p_sub_ifs):
374 p2p = self.p2p_sub_ifs.pop()
375 self.delete_p2p_ethernet(p2p)
376 super(P2PEthernetIPV4, self).tearDown()
378 def create_stream(self, src_mac=None, dst_mac=None,
379 src_ip=None, dst_ip=None, size=None):
382 pkt_size = random.choice(self.pg_if_packet_sizes)
383 p = Ether(src=src_mac, dst=dst_mac)
384 p /= IP(src=src_ip, dst=dst_ip)
385 p /= (UDP(sport=1234, dport=4321) / Raw('\xa5' * 20))
386 self.extend_packet(p, pkt_size)
389 def send_packets(self, src_if=None, dst_if=None, packets=None, count=None):
390 self.pg_enable_capture([dst_if])
392 packets = self.packets
393 src_if.add_stream(packets)
397 return dst_if.get_capture(count)
399 def verify_counters(self, counter_id, expected_value):
400 counters = self.vapi.cli("sh errors").split('\n')
402 for i in range(1, len(counters)-1):
403 results = counters[i].split()
404 if results[1] == counter_id:
405 counter_value = int(results[0])
407 self.assertEqual(counter_value, expected_value)
409 def create_p2p_ethernet(self, parent_if, sub_id, remote_mac):
410 p2p = VppP2PSubint(self, parent_if, sub_id, mactobinary(remote_mac))
415 def delete_p2p_ethernet(self, p2p):
418 self.vapi.delete_p2pethernet_subif(p2p.parent.sw_if_index,
421 def test_ip4_rx_p2p_subif(self):
422 """receive ipv4 packet via p2p subinterface"""
423 self.logger.info("FFP_TEST_START_0002")
425 route_9000 = VppIpRoute(self, "9.0.0.0", 16,
426 [VppRoutePath(self.pg1.remote_ip4,
427 self.pg1.sw_if_index)])
428 route_9000.add_vpp_config()
431 self.create_stream(src_mac=self.pg0._remote_hosts[0].mac,
432 dst_mac=self.pg0.local_mac,
433 src_ip=self.p2p_sub_ifs[0].remote_ip4,
436 self.send_packets(self.pg0, self.pg1, self.packets)
438 self.verify_counters('p2p-ethernet-input', 1)
440 route_9000.remove_vpp_config()
441 self.logger.info("FFP_TEST_FINISH_0002")
443 def test_ip4_rx_p2p_subif_route(self):
444 """route rx packet not matching p2p subinterface"""
445 self.logger.info("FFP_TEST_START_0003")
447 route_9001 = VppIpRoute(self, "9.0.0.0", 24,
448 [VppRoutePath(self.pg1.remote_ip4,
449 self.pg1.sw_if_index)])
450 route_9001.add_vpp_config()
453 self.create_stream(src_mac="02:01:00:00:ff:ff",
454 dst_mac=self.pg0.local_mac,
458 self.send_packets(self.pg0, self.pg1)
460 route_9001.remove_vpp_config()
462 self.logger.info("FFP_TEST_FINISH_0003")
464 def test_ip4_tx_p2p_subif(self):
465 """send ip4 packet via p2p subinterface"""
466 self.logger.info("FFP_TEST_START_0005")
468 route_9100 = VppIpRoute(self, "9.1.0.100", 24,
469 [VppRoutePath(self.pg0.remote_ip4,
470 self.pg0.sw_if_index,
472 route_9100.add_vpp_config()
473 route_9200 = VppIpRoute(self, "9.2.0.100", 24,
474 [VppRoutePath(self.p2p_sub_ifs[0].remote_ip4,
475 self.p2p_sub_ifs[0].sw_if_index,
477 route_9200.add_vpp_config()
478 route_9300 = VppIpRoute(self, "9.3.0.100", 24,
479 [VppRoutePath(self.p2p_sub_ifs[1].remote_ip4,
480 self.p2p_sub_ifs[1].sw_if_index
482 route_9300.add_vpp_config()
484 for i in range(0, 3):
486 self.create_stream(src_mac=self.pg1.remote_mac,
487 dst_mac=self.pg1.local_mac,
488 src_ip=self.pg1.remote_ip4,
489 dst_ip="9.%d.0.100" % (i+1)))
491 self.send_packets(self.pg1, self.pg0)
493 # route_7000.remove_vpp_config()
494 route_9100.remove_vpp_config()
495 route_9200.remove_vpp_config()
496 route_9300.remove_vpp_config()
498 self.logger.info("FFP_TEST_FINISH_0005")
500 def test_ip4_tx_p2p_subif_drop(self):
501 """drop tx ip4 packet not matching p2p subinterface"""
502 self.logger.info("FFP_TEST_START_0006")
505 self.create_stream(src_mac="02:01:00:00:ff:ff",
506 dst_mac=self.pg0.local_mac,
511 self.send_packets(self.pg0, self.pg1, count=0)
512 self.logger.info("FFP_TEST_FINISH_0006")
515 if __name__ == '__main__':
516 unittest.main(testRunner=VppTestRunner)