8 from scapy.contrib.mpls import MPLS
9 from scapy.layers.inet import IP, UDP, TCP, ICMP, icmptypes, icmpcodes
10 from scapy.layers.l2 import Ether, Dot1Q, ARP
11 from scapy.packet import Raw
14 from framework import VppTestCase, VppTestRunner
16 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpMRoute, \
17 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
18 VppMplsTable, VppIpTable
19 from vpp_sub_interface import VppSubInterface, VppDot1QSubint, VppDot1ADSubint
24 class TestIPv4(VppTestCase):
25 """ IPv4 Test Case """
29 super(TestIPv4, cls).setUpClass()
32 def tearDownClass(cls):
33 super(TestIPv4, cls).tearDownClass()
37 Perform test setup before test case.
40 - create 3 pg interfaces
41 - untagged pg0 interface
42 - Dot1Q subinterface on pg1
43 - Dot1AD subinterface on pg2
45 - put it into UP state
47 - resolve neighbor address using ARP
48 - configure 200 fib entries
50 :ivar list interfaces: pg interfaces and subinterfaces.
51 :ivar dict flows: IPv4 packet flows in test.
53 super(TestIPv4, self).setUp()
55 # create 3 pg interfaces
56 self.create_pg_interfaces(range(3))
58 # create 2 subinterfaces for pg1 and pg2
59 self.sub_interfaces = [
60 VppDot1QSubint(self, self.pg1, 100),
61 VppDot1ADSubint(self, self.pg2, 200, 300, 400)]
63 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
65 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
66 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
67 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
70 self.pg_if_packet_sizes = [64, 1500, 9020]
72 self.interfaces = list(self.pg_interfaces)
73 self.interfaces.extend(self.sub_interfaces)
75 # setup all interfaces
76 for i in self.interfaces:
81 # config 2M FIB entries
82 self.config_fib_entries(200)
85 """Run standard test teardown and log ``show ip arp``."""
86 super(TestIPv4, self).tearDown()
88 def show_commands_at_teardown(self):
89 self.logger.info(self.vapi.cli("show ip arp"))
90 # info(self.vapi.cli("show ip fib")) # many entries
92 def config_fib_entries(self, count):
93 """For each interface add to the FIB table *count* routes to
94 "10.0.0.1/32" destination with interface's local address as next-hop
97 :param int count: Number of FIB entries.
99 - *TODO:* check if the next-hop address shouldn't be remote address
100 instead of local address.
102 n_int = len(self.interfaces)
105 dest_addr = socket.inet_pton(socket.AF_INET, "10.0.0.1")
107 for i in self.interfaces:
108 next_hop_address = i.local_ip4n
109 for j in range(count / n_int):
110 self.vapi.ip_add_del_route(dst_address=dest_addr,
111 dst_address_length=dest_addr_len,
112 next_hop_address=next_hop_address)
114 if counter / count * 100 > percent:
115 self.logger.info("Configure %d FIB entries .. %d%% done" %
119 def modify_packet(self, src_if, packet_size, pkt):
120 """Add load, set destination IP and extend packet to required packet
121 size for defined interface.
123 :param VppInterface src_if: Interface to create packet for.
124 :param int packet_size: Required packet size.
125 :param Scapy pkt: Packet to be modified.
127 dst_if_idx = packet_size / 10 % 2
128 dst_if = self.flows[src_if][dst_if_idx]
129 info = self.create_packet_info(src_if, dst_if)
130 payload = self.info_to_payload(info)
132 p[IP].dst = dst_if.remote_ip4
134 if isinstance(src_if, VppSubInterface):
135 p = src_if.add_dot1_layer(p)
136 self.extend_packet(p, packet_size)
140 def create_stream(self, src_if):
141 """Create input packet stream for defined interface.
143 :param VppInterface src_if: Interface to create packet stream for.
145 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
146 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
147 IP(src=src_if.remote_ip4) /
148 UDP(sport=1234, dport=1234))
150 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
151 for i in moves.range(self.pg_if_packet_sizes[0],
152 self.pg_if_packet_sizes[1], 10)]
153 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
154 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
155 self.pg_if_packet_sizes[2] + hdr_ext,
161 def verify_capture(self, dst_if, capture):
162 """Verify captured input packet stream for defined interface.
164 :param VppInterface dst_if: Interface to verify captured packet stream
166 :param list capture: Captured packet stream.
168 self.logger.info("Verifying capture on interface %s" % dst_if.name)
170 for i in self.interfaces:
171 last_info[i.sw_if_index] = None
173 dst_sw_if_index = dst_if.sw_if_index
174 if hasattr(dst_if, 'parent'):
176 for packet in capture:
178 # Check VLAN tags and Ethernet header
179 packet = dst_if.remove_dot1_layer(packet)
180 self.assertTrue(Dot1Q not in packet)
184 payload_info = self.payload_to_info(packet[Raw])
185 packet_index = payload_info.index
186 self.assertEqual(payload_info.dst, dst_sw_if_index)
188 "Got packet on port %s: src=%u (id=%u)" %
189 (dst_if.name, payload_info.src, packet_index))
190 next_info = self.get_next_packet_info_for_interface2(
191 payload_info.src, dst_sw_if_index,
192 last_info[payload_info.src])
193 last_info[payload_info.src] = next_info
194 self.assertTrue(next_info is not None)
195 self.assertEqual(packet_index, next_info.index)
196 saved_packet = next_info.data
197 # Check standard fields
198 self.assertEqual(ip.src, saved_packet[IP].src)
199 self.assertEqual(ip.dst, saved_packet[IP].dst)
200 self.assertEqual(udp.sport, saved_packet[UDP].sport)
201 self.assertEqual(udp.dport, saved_packet[UDP].dport)
203 self.logger.error(ppp("Unexpected or invalid packet:", packet))
205 for i in self.interfaces:
206 remaining_packet = self.get_next_packet_info_for_interface2(
207 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
208 self.assertTrue(remaining_packet is None,
209 "Interface %s: Packet expected from interface %s "
210 "didn't arrive" % (dst_if.name, i.name))
217 - Create IPv4 stream for pg0 interface
218 - Create IPv4 tagged streams for pg1's and pg2's sub-interface.
219 - Send and verify received packets on each interface.
222 pkts = self.create_stream(self.pg0)
223 self.pg0.add_stream(pkts)
225 for i in self.sub_interfaces:
226 pkts = self.create_stream(i)
227 i.parent.add_stream(pkts)
229 self.pg_enable_capture(self.pg_interfaces)
232 pkts = self.pg0.get_capture()
233 self.verify_capture(self.pg0, pkts)
235 for i in self.sub_interfaces:
236 pkts = i.parent.get_capture()
237 self.verify_capture(i, pkts)
240 class TestICMPEcho(VppTestCase):
241 """ ICMP Echo Test Case """
245 super(TestICMPEcho, cls).setUpClass()
248 def tearDownClass(cls):
249 super(TestICMPEcho, cls).tearDownClass()
252 super(TestICMPEcho, self).setUp()
254 # create 1 pg interface
255 self.create_pg_interfaces(range(1))
257 for i in self.pg_interfaces:
263 super(TestICMPEcho, self).tearDown()
264 for i in self.pg_interfaces:
268 def test_icmp_echo(self):
269 """ VPP replies to ICMP Echo Request
273 - Receive ICMP Echo Request message on pg0 interface.
274 - Check outgoing ICMP Echo Reply message on pg0 interface.
279 icmp_load = '\x0a' * 18
280 p_echo_request = (Ether(src=self.pg0.remote_mac,
281 dst=self.pg0.local_mac) /
282 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
283 ICMP(id=icmp_id, seq=icmp_seq) /
286 self.pg0.add_stream(p_echo_request)
287 self.pg_enable_capture(self.pg_interfaces)
290 rx = self.pg0.get_capture(1)
296 self.assertEqual(ether.src, self.pg0.local_mac)
297 self.assertEqual(ether.dst, self.pg0.remote_mac)
299 self.assertEqual(ipv4.src, self.pg0.local_ip4)
300 self.assertEqual(ipv4.dst, self.pg0.remote_ip4)
302 self.assertEqual(icmptypes[icmp.type], "echo-reply")
303 self.assertEqual(icmp.id, icmp_id)
304 self.assertEqual(icmp.seq, icmp_seq)
305 self.assertEqual(icmp[Raw].load, icmp_load)
308 class TestIPv4FibCrud(VppTestCase):
309 """ FIB - add/update/delete - ip4 routes
317 ..note:: Python API is too slow to add many routes, needs replacement.
320 def config_fib_many_to_one(self, start_dest_addr, next_hop_addr, count):
323 :param start_dest_addr:
324 :param next_hop_addr:
326 :return list: added ips with 32 prefix
329 dest_addr = int(binascii.hexlify(socket.inet_pton(socket.AF_INET,
330 start_dest_addr)), 16)
332 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr)
333 for _ in range(count):
334 n_dest_addr = binascii.unhexlify('{:08x}'.format(dest_addr))
335 self.vapi.ip_add_del_route(dst_address=n_dest_addr,
336 dst_address_length=dest_addr_len,
337 next_hop_address=n_next_hop_addr)
338 added_ips.append(socket.inet_ntoa(n_dest_addr))
342 def unconfig_fib_many_to_one(self, start_dest_addr, next_hop_addr, count):
345 dest_addr = int(binascii.hexlify(socket.inet_pton(socket.AF_INET,
346 start_dest_addr)), 16)
348 n_next_hop_addr = socket.inet_pton(socket.AF_INET, next_hop_addr)
349 for _ in range(count):
350 n_dest_addr = binascii.unhexlify('{:08x}'.format(dest_addr))
351 self.vapi.ip_add_del_route(dst_address=n_dest_addr,
352 dst_address_length=dest_addr_len,
353 next_hop_address=n_next_hop_addr,
355 removed_ips.append(socket.inet_ntoa(n_dest_addr))
359 def create_stream(self, src_if, dst_if, dst_ips, count):
362 for _ in range(count):
363 dst_addr = random.choice(dst_ips)
364 info = self.create_packet_info(src_if, dst_if)
365 payload = self.info_to_payload(info)
366 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
367 IP(src=src_if.remote_ip4, dst=dst_addr) /
368 UDP(sport=1234, dport=1234) /
371 self.extend_packet(p, random.choice(self.pg_if_packet_sizes))
376 def _find_ip_match(self, find_in, pkt):
378 if self.payload_to_info(p[Raw]) == \
379 self.payload_to_info(pkt[Raw]):
380 if p[IP].src != pkt[IP].src:
382 if p[IP].dst != pkt[IP].dst:
384 if p[UDP].sport != pkt[UDP].sport:
386 if p[UDP].dport != pkt[UDP].dport:
392 def _match_route_detail(route_detail, ip, address_length=32, table_id=0):
393 if route_detail.address == socket.inet_pton(socket.AF_INET, ip):
394 if route_detail.table_id != table_id:
396 elif route_detail.address_length != address_length:
403 def verify_capture(self, dst_interface, received_pkts, expected_pkts):
404 self.assertEqual(len(received_pkts), len(expected_pkts))
405 to_verify = list(expected_pkts)
406 for p in received_pkts:
407 self.assertEqual(p.src, dst_interface.local_mac)
408 self.assertEqual(p.dst, dst_interface.remote_mac)
409 x = self._find_ip_match(to_verify, p)
411 self.assertListEqual(to_verify, [])
413 def verify_route_dump(self, fib_dump, ips):
415 def _ip_in_route_dump(ip, fib_dump):
416 return next((route for route in fib_dump
417 if self._match_route_detail(route, ip)),
421 self.assertTrue(_ip_in_route_dump(ip, fib_dump),
422 'IP {!s} is not in fib dump.'.format(ip))
424 def verify_not_in_route_dump(self, fib_dump, ips):
426 def _ip_in_route_dump(ip, fib_dump):
427 return next((route for route in fib_dump
428 if self._match_route_detail(route, ip)),
432 self.assertFalse(_ip_in_route_dump(ip, fib_dump),
433 'IP {!s} is in fib dump.'.format(ip))
438 #. Create and initialize 3 pg interfaces.
439 #. initialize class attributes configured_routes and deleted_routes
440 to store information between tests.
442 super(TestIPv4FibCrud, cls).setUpClass()
445 # create 3 pg interfaces
446 cls.create_pg_interfaces(range(3))
448 cls.interfaces = list(cls.pg_interfaces)
450 # setup all interfaces
451 for i in cls.interfaces:
456 cls.configured_routes = []
457 cls.deleted_routes = []
458 cls.pg_if_packet_sizes = [64, 512, 1518, 9018]
461 super(TestIPv4FibCrud, cls).tearDownClass()
465 def tearDownClass(cls):
466 super(TestIPv4FibCrud, cls).tearDownClass()
469 super(TestIPv4FibCrud, self).setUp()
470 self.reset_packet_infos()
472 self.configured_routes = []
473 self.deleted_routes = []
475 def test_1_add_routes(self):
478 - add 100 routes check with traffic script.
480 # config 1M FIB entries
481 self.configured_routes.extend(self.config_fib_many_to_one(
482 "10.0.0.0", self.pg0.remote_ip4, 100))
484 fib_dump = self.vapi.ip_fib_dump()
485 self.verify_route_dump(fib_dump, self.configured_routes)
487 self.stream_1 = self.create_stream(
488 self.pg1, self.pg0, self.configured_routes, 100)
489 self.stream_2 = self.create_stream(
490 self.pg2, self.pg0, self.configured_routes, 100)
491 self.pg1.add_stream(self.stream_1)
492 self.pg2.add_stream(self.stream_2)
494 self.pg_enable_capture(self.pg_interfaces)
497 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
498 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
500 def test_2_del_routes(self):
501 """ Delete 100 routes
503 - delete 10 routes check with traffic script.
505 # config 1M FIB entries
506 self.configured_routes.extend(self.config_fib_many_to_one(
507 "10.0.0.0", self.pg0.remote_ip4, 100))
508 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
509 "10.0.0.10", self.pg0.remote_ip4, 10))
510 for x in self.deleted_routes:
511 self.configured_routes.remove(x)
513 fib_dump = self.vapi.ip_fib_dump()
514 self.verify_route_dump(fib_dump, self.configured_routes)
516 self.stream_1 = self.create_stream(
517 self.pg1, self.pg0, self.configured_routes, 100)
518 self.stream_2 = self.create_stream(
519 self.pg2, self.pg0, self.configured_routes, 100)
520 self.stream_3 = self.create_stream(
521 self.pg1, self.pg0, self.deleted_routes, 100)
522 self.stream_4 = self.create_stream(
523 self.pg2, self.pg0, self.deleted_routes, 100)
524 self.pg1.add_stream(self.stream_1 + self.stream_3)
525 self.pg2.add_stream(self.stream_2 + self.stream_4)
526 self.pg_enable_capture(self.pg_interfaces)
529 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
530 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
532 def test_3_add_new_routes(self):
535 - re-add 5 routes check with traffic script.
536 - add 100 routes check with traffic script.
538 # config 1M FIB entries
539 self.configured_routes.extend(self.config_fib_many_to_one(
540 "10.0.0.0", self.pg0.remote_ip4, 100))
541 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
542 "10.0.0.10", self.pg0.remote_ip4, 10))
543 for x in self.deleted_routes:
544 self.configured_routes.remove(x)
546 tmp = self.config_fib_many_to_one(
547 "10.0.0.10", self.pg0.remote_ip4, 5)
548 self.configured_routes.extend(tmp)
550 self.deleted_routes.remove(x)
552 self.configured_routes.extend(self.config_fib_many_to_one(
553 "10.0.1.0", self.pg0.remote_ip4, 100))
555 fib_dump = self.vapi.ip_fib_dump()
556 self.verify_route_dump(fib_dump, self.configured_routes)
558 self.stream_1 = self.create_stream(
559 self.pg1, self.pg0, self.configured_routes, 300)
560 self.stream_2 = self.create_stream(
561 self.pg2, self.pg0, self.configured_routes, 300)
562 self.stream_3 = self.create_stream(
563 self.pg1, self.pg0, self.deleted_routes, 100)
564 self.stream_4 = self.create_stream(
565 self.pg2, self.pg0, self.deleted_routes, 100)
567 self.pg1.add_stream(self.stream_1 + self.stream_3)
568 self.pg2.add_stream(self.stream_2 + self.stream_4)
569 self.pg_enable_capture(self.pg_interfaces)
572 pkts = self.pg0.get_capture(len(self.stream_1) + len(self.stream_2))
573 self.verify_capture(self.pg0, pkts, self.stream_1 + self.stream_2)
575 def test_4_del_routes(self):
576 """ Delete 1.5k routes
578 - delete 5 routes check with traffic script.
579 - add 100 routes check with traffic script.
581 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
582 "10.0.0.0", self.pg0.remote_ip4, 15))
583 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
584 "10.0.0.20", self.pg0.remote_ip4, 85))
585 self.deleted_routes.extend(self.unconfig_fib_many_to_one(
586 "10.0.1.0", self.pg0.remote_ip4, 100))
587 fib_dump = self.vapi.ip_fib_dump()
588 self.verify_not_in_route_dump(fib_dump, self.deleted_routes)
591 class TestIPNull(VppTestCase):
592 """ IPv4 routes via NULL """
596 super(TestIPNull, cls).setUpClass()
599 def tearDownClass(cls):
600 super(TestIPNull, cls).tearDownClass()
603 super(TestIPNull, self).setUp()
605 # create 2 pg interfaces
606 self.create_pg_interfaces(range(2))
608 for i in self.pg_interfaces:
614 super(TestIPNull, self).tearDown()
615 for i in self.pg_interfaces:
619 def test_ip_null(self):
620 """ IP NULL route """
623 # A route via IP NULL that will reply with ICMP unreachables
625 ip_unreach = VppIpRoute(self, "10.0.0.1", 32, [], is_unreach=1)
626 ip_unreach.add_vpp_config()
628 p_unreach = (Ether(src=self.pg0.remote_mac,
629 dst=self.pg0.local_mac) /
630 IP(src=self.pg0.remote_ip4, dst="10.0.0.1") /
631 UDP(sport=1234, dport=1234) /
634 self.pg0.add_stream(p_unreach)
635 self.pg_enable_capture(self.pg_interfaces)
638 rx = self.pg0.get_capture(1)
642 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
643 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-unreachable")
644 self.assertEqual(icmp.src, self.pg0.remote_ip4)
645 self.assertEqual(icmp.dst, "10.0.0.1")
648 # ICMP replies are rate limited. so sit and spin.
653 # A route via IP NULL that will reply with ICMP prohibited
655 ip_prohibit = VppIpRoute(self, "10.0.0.2", 32, [], is_prohibit=1)
656 ip_prohibit.add_vpp_config()
658 p_prohibit = (Ether(src=self.pg0.remote_mac,
659 dst=self.pg0.local_mac) /
660 IP(src=self.pg0.remote_ip4, dst="10.0.0.2") /
661 UDP(sport=1234, dport=1234) /
664 self.pg0.add_stream(p_prohibit)
665 self.pg_enable_capture(self.pg_interfaces)
668 rx = self.pg0.get_capture(1)
673 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
674 self.assertEqual(icmpcodes[icmp.type][icmp.code], "host-prohibited")
675 self.assertEqual(icmp.src, self.pg0.remote_ip4)
676 self.assertEqual(icmp.dst, "10.0.0.2")
678 def test_ip_drop(self):
679 """ IP Drop Routes """
681 p = (Ether(src=self.pg0.remote_mac,
682 dst=self.pg0.local_mac) /
683 IP(src=self.pg0.remote_ip4, dst="1.1.1.1") /
684 UDP(sport=1234, dport=1234) /
687 r1 = VppIpRoute(self, "1.1.1.0", 24,
688 [VppRoutePath(self.pg1.remote_ip4,
689 self.pg1.sw_if_index)])
692 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
695 # insert a more specific as a drop
697 r2 = VppIpRoute(self, "1.1.1.1", 32, [], is_drop=1)
700 self.send_and_assert_no_replies(self.pg0, p * NUM_PKTS, "Drop Route")
701 r2.remove_vpp_config()
702 rx = self.send_and_expect(self.pg0, p * NUM_PKTS, self.pg1)
705 class TestIPDisabled(VppTestCase):
706 """ IPv4 disabled """
710 super(TestIPDisabled, cls).setUpClass()
713 def tearDownClass(cls):
714 super(TestIPDisabled, cls).tearDownClass()
717 super(TestIPDisabled, self).setUp()
719 # create 2 pg interfaces
720 self.create_pg_interfaces(range(2))
724 self.pg0.config_ip4()
725 self.pg0.resolve_arp()
727 # PG 1 is not IP enabled
731 super(TestIPDisabled, self).tearDown()
732 for i in self.pg_interfaces:
736 def test_ip_disabled(self):
741 # one accepting interface, pg0, 2 forwarding interfaces
743 route_232_1_1_1 = VppIpMRoute(
747 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
748 [VppMRoutePath(self.pg1.sw_if_index,
749 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
750 VppMRoutePath(self.pg0.sw_if_index,
751 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
752 route_232_1_1_1.add_vpp_config()
754 pu = (Ether(src=self.pg1.remote_mac,
755 dst=self.pg1.local_mac) /
756 IP(src="10.10.10.10", dst=self.pg0.remote_ip4) /
757 UDP(sport=1234, dport=1234) /
759 pm = (Ether(src=self.pg1.remote_mac,
760 dst=self.pg1.local_mac) /
761 IP(src="10.10.10.10", dst="232.1.1.1") /
762 UDP(sport=1234, dport=1234) /
766 # PG1 does not forward IP traffic
768 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
769 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
774 self.pg1.config_ip4()
777 # Now we get packets through
779 self.pg1.add_stream(pu)
780 self.pg_enable_capture(self.pg_interfaces)
782 rx = self.pg0.get_capture(1)
784 self.pg1.add_stream(pm)
785 self.pg_enable_capture(self.pg_interfaces)
787 rx = self.pg0.get_capture(1)
792 self.pg1.unconfig_ip4()
795 # PG1 does not forward IP traffic
797 self.send_and_assert_no_replies(self.pg1, pu, "IP disabled")
798 self.send_and_assert_no_replies(self.pg1, pm, "IP disabled")
801 class TestIPSubNets(VppTestCase):
806 super(TestIPSubNets, cls).setUpClass()
809 def tearDownClass(cls):
810 super(TestIPSubNets, cls).tearDownClass()
813 super(TestIPSubNets, self).setUp()
815 # create a 2 pg interfaces
816 self.create_pg_interfaces(range(2))
818 # pg0 we will use to experiment
821 # pg1 is setup normally
823 self.pg1.config_ip4()
824 self.pg1.resolve_arp()
827 super(TestIPSubNets, self).tearDown()
828 for i in self.pg_interfaces:
831 def test_ip_sub_nets(self):
835 # Configure a covering route to forward so we know
836 # when we are dropping
838 cover_route = VppIpRoute(self, "10.0.0.0", 8,
839 [VppRoutePath(self.pg1.remote_ip4,
840 self.pg1.sw_if_index)])
841 cover_route.add_vpp_config()
843 p = (Ether(src=self.pg1.remote_mac,
844 dst=self.pg1.local_mac) /
845 IP(dst="10.10.10.10", src=self.pg0.local_ip4) /
846 UDP(sport=1234, dport=1234) /
849 self.pg1.add_stream(p)
850 self.pg_enable_capture(self.pg_interfaces)
852 rx = self.pg1.get_capture(1)
855 # Configure some non-/24 subnets on an IP interface
857 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
859 self.vapi.sw_interface_add_del_address(
860 sw_if_index=self.pg0.sw_if_index, address=ip_addr_n,
863 pn = (Ether(src=self.pg1.remote_mac,
864 dst=self.pg1.local_mac) /
865 IP(dst="10.10.0.0", src=self.pg0.local_ip4) /
866 UDP(sport=1234, dport=1234) /
868 pb = (Ether(src=self.pg1.remote_mac,
869 dst=self.pg1.local_mac) /
870 IP(dst="10.10.255.255", src=self.pg0.local_ip4) /
871 UDP(sport=1234, dport=1234) /
874 self.send_and_assert_no_replies(self.pg1, pn, "IP Network address")
875 self.send_and_assert_no_replies(self.pg1, pb, "IP Broadcast address")
877 # remove the sub-net and we are forwarding via the cover again
878 self.vapi.sw_interface_add_del_address(
879 sw_if_index=self.pg0.sw_if_index, address=ip_addr_n,
880 address_length=16, is_add=0)
881 self.pg1.add_stream(pn)
882 self.pg_enable_capture(self.pg_interfaces)
884 rx = self.pg1.get_capture(1)
885 self.pg1.add_stream(pb)
886 self.pg_enable_capture(self.pg_interfaces)
888 rx = self.pg1.get_capture(1)
891 # A /31 is a special case where the 'other-side' is an attached host
892 # packets to that peer generate ARP requests
894 ip_addr_n = socket.inet_pton(socket.AF_INET, "10.10.10.10")
896 self.vapi.sw_interface_add_del_address(
897 sw_if_index=self.pg0.sw_if_index, address=ip_addr_n,
900 pn = (Ether(src=self.pg1.remote_mac,
901 dst=self.pg1.local_mac) /
902 IP(dst="10.10.10.11", src=self.pg0.local_ip4) /
903 UDP(sport=1234, dport=1234) /
906 self.pg1.add_stream(pn)
907 self.pg_enable_capture(self.pg_interfaces)
909 rx = self.pg0.get_capture(1)
912 # remove the sub-net and we are forwarding via the cover again
913 self.vapi.sw_interface_add_del_address(
914 sw_if_index=self.pg0.sw_if_index, address=ip_addr_n,
915 address_length=31, is_add=0)
916 self.pg1.add_stream(pn)
917 self.pg_enable_capture(self.pg_interfaces)
919 rx = self.pg1.get_capture(1)
922 class TestIPLoadBalance(VppTestCase):
923 """ IPv4 Load-Balancing """
927 super(TestIPLoadBalance, cls).setUpClass()
930 def tearDownClass(cls):
931 super(TestIPLoadBalance, cls).tearDownClass()
934 super(TestIPLoadBalance, self).setUp()
936 self.create_pg_interfaces(range(5))
937 mpls_tbl = VppMplsTable(self, 0)
938 mpls_tbl.add_vpp_config()
940 for i in self.pg_interfaces:
947 for i in self.pg_interfaces:
951 super(TestIPLoadBalance, self).tearDown()
953 def send_and_expect_load_balancing(self, input, pkts, outputs):
954 input.add_stream(pkts)
955 self.pg_enable_capture(self.pg_interfaces)
959 rx = oo._get_capture(1)
960 self.assertNotEqual(0, len(rx))
965 def send_and_expect_one_itf(self, input, pkts, itf):
966 input.add_stream(pkts)
967 self.pg_enable_capture(self.pg_interfaces)
969 rx = itf.get_capture(len(pkts))
971 def test_ip_load_balance(self):
972 """ IP Load-Balancing """
975 # An array of packets that differ only in the destination port
981 # An array of packets that differ only in the source address
986 for ii in range(NUM_PKTS):
987 port_ip_hdr = (IP(dst="10.0.0.1", src="20.0.0.1") /
988 UDP(sport=1234, dport=1234 + ii) /
990 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
991 dst=self.pg0.local_mac) /
993 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
994 dst=self.pg0.local_mac) /
995 MPLS(label=66, ttl=2) /
998 src_ip_hdr = (IP(dst="10.0.0.1", src="20.0.0.%d" % ii) /
999 UDP(sport=1234, dport=1234) /
1001 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1002 dst=self.pg0.local_mac) /
1004 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1005 dst=self.pg0.local_mac) /
1006 MPLS(label=66, ttl=2) /
1009 route_10_0_0_1 = VppIpRoute(self, "10.0.0.1", 32,
1010 [VppRoutePath(self.pg1.remote_ip4,
1011 self.pg1.sw_if_index),
1012 VppRoutePath(self.pg2.remote_ip4,
1013 self.pg2.sw_if_index)])
1014 route_10_0_0_1.add_vpp_config()
1016 binding = VppMplsIpBind(self, 66, "10.0.0.1", 32)
1017 binding.add_vpp_config()
1020 # inject the packet on pg0 - expect load-balancing across the 2 paths
1021 # - since the default hash config is to use IP src,dst and port
1023 # We are not going to ensure equal amounts of packets across each link,
1024 # since the hash algorithm is statistical and therefore this can never
1025 # be guaranteed. But with 64 different packets we do expect some
1026 # balancing. So instead just ensure there is traffic on each link.
1028 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1029 [self.pg1, self.pg2])
1030 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1031 [self.pg1, self.pg2])
1032 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1033 [self.pg1, self.pg2])
1034 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1035 [self.pg1, self.pg2])
1038 # change the flow hash config so it's only IP src,dst
1039 # - now only the stream with differing source address will
1042 self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=0, dport=0)
1044 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1045 [self.pg1, self.pg2])
1046 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1047 [self.pg1, self.pg2])
1049 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1052 # change the flow hash config back to defaults
1054 self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=1, dport=1)
1057 # Recursive prefixes
1058 # - testing that 2 stages of load-balancing occurs and there is no
1059 # polarisation (i.e. only 2 of 4 paths are used)
1064 for ii in range(257):
1065 port_pkts.append((Ether(src=self.pg0.remote_mac,
1066 dst=self.pg0.local_mac) /
1067 IP(dst="1.1.1.1", src="20.0.0.1") /
1068 UDP(sport=1234, dport=1234 + ii) /
1070 src_pkts.append((Ether(src=self.pg0.remote_mac,
1071 dst=self.pg0.local_mac) /
1072 IP(dst="1.1.1.1", src="20.0.0.%d" % ii) /
1073 UDP(sport=1234, dport=1234) /
1076 route_10_0_0_2 = VppIpRoute(self, "10.0.0.2", 32,
1077 [VppRoutePath(self.pg3.remote_ip4,
1078 self.pg3.sw_if_index),
1079 VppRoutePath(self.pg4.remote_ip4,
1080 self.pg4.sw_if_index)])
1081 route_10_0_0_2.add_vpp_config()
1083 route_1_1_1_1 = VppIpRoute(self, "1.1.1.1", 32,
1084 [VppRoutePath("10.0.0.2", 0xffffffff),
1085 VppRoutePath("10.0.0.1", 0xffffffff)])
1086 route_1_1_1_1.add_vpp_config()
1089 # inject the packet on pg0 - expect load-balancing across all 4 paths
1091 self.vapi.cli("clear trace")
1092 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1093 [self.pg1, self.pg2,
1094 self.pg3, self.pg4])
1095 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1096 [self.pg1, self.pg2,
1097 self.pg3, self.pg4])
1100 # bring down pg1 expect LB to adjust to use only those that are pu
1102 self.pg1.link_down()
1104 rx = self.send_and_expect_load_balancing(self.pg0, src_pkts,
1105 [self.pg2, self.pg3,
1107 self.assertEqual(len(src_pkts), len(rx))
1110 # bring down pg2 expect LB to adjust to use only those that are pu
1112 self.pg2.link_down()
1114 rx = self.send_and_expect_load_balancing(self.pg0, src_pkts,
1115 [self.pg3, self.pg4])
1116 self.assertEqual(len(src_pkts), len(rx))
1119 # bring the links back up - expect LB over all again
1124 rx = self.send_and_expect_load_balancing(self.pg0, src_pkts,
1125 [self.pg1, self.pg2,
1126 self.pg3, self.pg4])
1127 self.assertEqual(len(src_pkts), len(rx))
1130 # The same link-up/down but this time admin state
1132 self.pg1.admin_down()
1133 self.pg2.admin_down()
1134 rx = self.send_and_expect_load_balancing(self.pg0, src_pkts,
1135 [self.pg3, self.pg4])
1136 self.assertEqual(len(src_pkts), len(rx))
1139 self.pg1.resolve_arp()
1140 self.pg2.resolve_arp()
1141 rx = self.send_and_expect_load_balancing(self.pg0, src_pkts,
1142 [self.pg1, self.pg2,
1143 self.pg3, self.pg4])
1144 self.assertEqual(len(src_pkts), len(rx))
1147 # Recursive prefixes
1148 # - testing that 2 stages of load-balancing, no choices
1152 for ii in range(257):
1153 port_pkts.append((Ether(src=self.pg0.remote_mac,
1154 dst=self.pg0.local_mac) /
1155 IP(dst="1.1.1.2", src="20.0.0.2") /
1156 UDP(sport=1234, dport=1234 + ii) /
1159 route_10_0_0_3 = VppIpRoute(self, "10.0.0.3", 32,
1160 [VppRoutePath(self.pg3.remote_ip4,
1161 self.pg3.sw_if_index)])
1162 route_10_0_0_3.add_vpp_config()
1164 route_1_1_1_2 = VppIpRoute(self, "1.1.1.2", 32,
1165 [VppRoutePath("10.0.0.3", 0xffffffff)])
1166 route_1_1_1_2.add_vpp_config()
1169 # inject the packet on pg0 - rx only on via routes output interface
1171 self.vapi.cli("clear trace")
1172 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1175 # Add a LB route in the presence of a down link - expect no
1176 # packets over the down link
1178 self.pg3.link_down()
1180 route_10_0_0_3 = VppIpRoute(self, "10.0.0.3", 32,
1181 [VppRoutePath(self.pg3.remote_ip4,
1182 self.pg3.sw_if_index),
1183 VppRoutePath(self.pg4.remote_ip4,
1184 self.pg4.sw_if_index)])
1185 route_10_0_0_3.add_vpp_config()
1188 for ii in range(257):
1189 port_pkts.append(Ether(src=self.pg0.remote_mac,
1190 dst=self.pg0.local_mac) /
1191 IP(dst="10.0.0.3", src="20.0.0.2") /
1192 UDP(sport=1234, dport=1234 + ii) /
1195 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg4)
1197 # bring the link back up
1200 rx = self.send_and_expect_load_balancing(self.pg0, port_pkts,
1201 [self.pg3, self.pg4])
1202 self.assertEqual(len(src_pkts), len(rx))
1205 class TestIPVlan0(VppTestCase):
1209 def setUpClass(cls):
1210 super(TestIPVlan0, cls).setUpClass()
1213 def tearDownClass(cls):
1214 super(TestIPVlan0, cls).tearDownClass()
1217 super(TestIPVlan0, self).setUp()
1219 self.create_pg_interfaces(range(2))
1220 mpls_tbl = VppMplsTable(self, 0)
1221 mpls_tbl.add_vpp_config()
1223 for i in self.pg_interfaces:
1230 for i in self.pg_interfaces:
1234 super(TestIPVlan0, self).tearDown()
1236 def test_ip_vlan_0(self):
1239 pkts = (Ether(src=self.pg0.remote_mac,
1240 dst=self.pg0.local_mac) /
1242 IP(dst=self.pg1.remote_ip4,
1243 src=self.pg0.remote_ip4) /
1244 UDP(sport=1234, dport=1234) /
1245 Raw('\xa5' * 100)) * NUM_PKTS
1248 # Expect that packets sent on VLAN-0 are forwarded on the
1251 self.send_and_expect(self.pg0, pkts, self.pg1)
1254 class TestIPPunt(VppTestCase):
1255 """ IPv4 Punt Police/Redirect """
1258 def setUpClass(cls):
1259 super(TestIPPunt, cls).setUpClass()
1262 def tearDownClass(cls):
1263 super(TestIPPunt, cls).tearDownClass()
1266 super(TestIPPunt, self).setUp()
1268 self.create_pg_interfaces(range(4))
1270 for i in self.pg_interfaces:
1276 super(TestIPPunt, self).tearDown()
1277 for i in self.pg_interfaces:
1281 def test_ip_punt(self):
1282 """ IP punt police and redirect """
1284 p = (Ether(src=self.pg0.remote_mac,
1285 dst=self.pg0.local_mac) /
1286 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
1287 TCP(sport=1234, dport=1234) /
1293 # Configure a punt redirect via pg1.
1295 nh_addr = self.pg1.remote_ip4
1296 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1297 self.pg1.sw_if_index,
1300 self.send_and_expect(self.pg0, pkts, self.pg1)
1305 policer = self.vapi.policer_add_del(b"ip4-punt", 400, 0, 10, 0,
1307 self.vapi.ip_punt_police(policer.policer_index)
1309 self.vapi.cli("clear trace")
1310 self.pg0.add_stream(pkts)
1311 self.pg_enable_capture(self.pg_interfaces)
1315 # the number of packet received should be greater than 0,
1316 # but not equal to the number sent, since some were policed
1318 rx = self.pg1._get_capture(1)
1319 self.assertGreater(len(rx), 0)
1320 self.assertLess(len(rx), len(pkts))
1323 # remove the policer. back to full rx
1325 self.vapi.ip_punt_police(policer.policer_index, is_add=0)
1326 self.vapi.policer_add_del(b"ip4-punt", 400, 0, 10, 0,
1327 rate_type=1, is_add=0)
1328 self.send_and_expect(self.pg0, pkts, self.pg1)
1331 # remove the redirect. expect full drop.
1333 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1334 self.pg1.sw_if_index,
1337 self.send_and_assert_no_replies(self.pg0, pkts,
1338 "IP no punt config")
1341 # Add a redirect that is not input port selective
1343 self.vapi.ip_punt_redirect(0xffffffff,
1344 self.pg1.sw_if_index,
1346 self.send_and_expect(self.pg0, pkts, self.pg1)
1348 self.vapi.ip_punt_redirect(0xffffffff,
1349 self.pg1.sw_if_index,
1353 def test_ip_punt_dump(self):
1354 """ IP4 punt redirect dump"""
1357 # Configure a punt redirects
1359 nh_address = self.pg3.remote_ip4
1360 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1361 self.pg3.sw_if_index,
1363 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
1364 self.pg3.sw_if_index,
1366 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
1367 self.pg3.sw_if_index,
1371 # Dump pg0 punt redirects
1373 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index)
1375 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
1378 # Dump punt redirects for all interfaces
1380 punts = self.vapi.ip_punt_redirect_dump(0xffffffff)
1381 self.assertEqual(len(punts), 3)
1383 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
1384 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip4)
1385 self.assertEqual(str(punts[2].punt.nh), '0.0.0.0')
1388 class TestIPDeag(VppTestCase):
1389 """ IPv4 Deaggregate Routes """
1392 def setUpClass(cls):
1393 super(TestIPDeag, cls).setUpClass()
1396 def tearDownClass(cls):
1397 super(TestIPDeag, cls).tearDownClass()
1400 super(TestIPDeag, self).setUp()
1402 self.create_pg_interfaces(range(3))
1404 for i in self.pg_interfaces:
1410 super(TestIPDeag, self).tearDown()
1411 for i in self.pg_interfaces:
1415 def test_ip_deag(self):
1416 """ IP Deag Routes """
1419 # Create a table to be used for:
1420 # 1 - another destination address lookup
1421 # 2 - a source address lookup
1423 table_dst = VppIpTable(self, 1)
1424 table_src = VppIpTable(self, 2)
1425 table_dst.add_vpp_config()
1426 table_src.add_vpp_config()
1429 # Add a route in the default table to point to a deag/
1430 # second lookup in each of these tables
1432 route_to_dst = VppIpRoute(self, "1.1.1.1", 32,
1433 [VppRoutePath("0.0.0.0",
1436 route_to_src = VppIpRoute(self, "1.1.1.2", 32,
1437 [VppRoutePath("0.0.0.0",
1440 is_source_lookup=1)])
1441 route_to_dst.add_vpp_config()
1442 route_to_src.add_vpp_config()
1445 # packets to these destination are dropped, since they'll
1446 # hit the respective default routes in the second table
1448 p_dst = (Ether(src=self.pg0.remote_mac,
1449 dst=self.pg0.local_mac) /
1450 IP(src="5.5.5.5", dst="1.1.1.1") /
1451 TCP(sport=1234, dport=1234) /
1453 p_src = (Ether(src=self.pg0.remote_mac,
1454 dst=self.pg0.local_mac) /
1455 IP(src="2.2.2.2", dst="1.1.1.2") /
1456 TCP(sport=1234, dport=1234) /
1458 pkts_dst = p_dst * 257
1459 pkts_src = p_src * 257
1461 self.send_and_assert_no_replies(self.pg0, pkts_dst,
1463 self.send_and_assert_no_replies(self.pg0, pkts_src,
1467 # add a route in the dst table to forward via pg1
1469 route_in_dst = VppIpRoute(self, "1.1.1.1", 32,
1470 [VppRoutePath(self.pg1.remote_ip4,
1471 self.pg1.sw_if_index)],
1473 route_in_dst.add_vpp_config()
1474 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
1477 # add a route in the src table to forward via pg2
1479 route_in_src = VppIpRoute(self, "2.2.2.2", 32,
1480 [VppRoutePath(self.pg2.remote_ip4,
1481 self.pg2.sw_if_index)],
1483 route_in_src.add_vpp_config()
1484 self.send_and_expect(self.pg0, pkts_src, self.pg2)
1487 # loop in the lookup DP
1489 route_loop = VppIpRoute(self, "2.2.2.3", 32,
1490 [VppRoutePath("0.0.0.0",
1493 route_loop.add_vpp_config()
1495 p_l = (Ether(src=self.pg0.remote_mac,
1496 dst=self.pg0.local_mac) /
1497 IP(src="2.2.2.4", dst="2.2.2.3") /
1498 TCP(sport=1234, dport=1234) /
1501 self.send_and_assert_no_replies(self.pg0, p_l * 257,
1505 class TestIPInput(VppTestCase):
1506 """ IPv4 Input Exceptions """
1509 def setUpClass(cls):
1510 super(TestIPInput, cls).setUpClass()
1513 def tearDownClass(cls):
1514 super(TestIPInput, cls).tearDownClass()
1517 super(TestIPInput, self).setUp()
1519 self.create_pg_interfaces(range(2))
1521 for i in self.pg_interfaces:
1527 super(TestIPInput, self).tearDown()
1528 for i in self.pg_interfaces:
1532 def test_ip_input(self):
1533 """ IP Input Exceptions """
1535 # i can't find a way in scapy to construct an IP packet
1536 # with a length less than the IP header length
1539 # Packet too short - this is forwarded
1541 p_short = (Ether(src=self.pg0.remote_mac,
1542 dst=self.pg0.local_mac) /
1543 IP(src=self.pg0.remote_ip4,
1544 dst=self.pg1.remote_ip4,
1546 UDP(sport=1234, dport=1234) /
1549 rx = self.send_and_expect(self.pg0, p_short * NUM_PKTS, self.pg1)
1552 # Packet too long - this is dropped
1554 p_long = (Ether(src=self.pg0.remote_mac,
1555 dst=self.pg0.local_mac) /
1556 IP(src=self.pg0.remote_ip4,
1557 dst=self.pg1.remote_ip4,
1559 UDP(sport=1234, dport=1234) /
1562 rx = self.send_and_assert_no_replies(self.pg0, p_long * NUM_PKTS,
1566 # bad chksum - this is dropped
1568 p_chksum = (Ether(src=self.pg0.remote_mac,
1569 dst=self.pg0.local_mac) /
1570 IP(src=self.pg0.remote_ip4,
1571 dst=self.pg1.remote_ip4,
1573 UDP(sport=1234, dport=1234) /
1576 rx = self.send_and_assert_no_replies(self.pg0, p_chksum * NUM_PKTS,
1580 # bad version - this is dropped
1582 p_ver = (Ether(src=self.pg0.remote_mac,
1583 dst=self.pg0.local_mac) /
1584 IP(src=self.pg0.remote_ip4,
1585 dst=self.pg1.remote_ip4,
1587 UDP(sport=1234, dport=1234) /
1590 rx = self.send_and_assert_no_replies(self.pg0, p_ver * NUM_PKTS,
1594 # fragment offset 1 - this is dropped
1596 p_frag = (Ether(src=self.pg0.remote_mac,
1597 dst=self.pg0.local_mac) /
1598 IP(src=self.pg0.remote_ip4,
1599 dst=self.pg1.remote_ip4,
1601 UDP(sport=1234, dport=1234) /
1604 rx = self.send_and_assert_no_replies(self.pg0, p_frag * NUM_PKTS,
1608 # TTL expired packet
1610 p_ttl = (Ether(src=self.pg0.remote_mac,
1611 dst=self.pg0.local_mac) /
1612 IP(src=self.pg0.remote_ip4,
1613 dst=self.pg1.remote_ip4,
1615 UDP(sport=1234, dport=1234) /
1618 rx = self.send_and_expect(self.pg0, p_ttl * NUM_PKTS, self.pg0)
1623 self.assertEqual(icmptypes[icmp.type], "time-exceeded")
1624 self.assertEqual(icmpcodes[icmp.type][icmp.code],
1625 "ttl-zero-during-transit")
1626 self.assertEqual(icmp.src, self.pg0.remote_ip4)
1627 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
1632 p_mtu = (Ether(src=self.pg0.remote_mac,
1633 dst=self.pg0.local_mac) /
1634 IP(src=self.pg0.remote_ip4,
1635 dst=self.pg1.remote_ip4,
1636 ttl=10, flags='DF') /
1637 UDP(sport=1234, dport=1234) /
1640 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1500, 0, 0, 0])
1642 rx = self.send_and_expect(self.pg0, p_mtu * NUM_PKTS, self.pg0)
1646 self.assertEqual(icmptypes[icmp.type], "dest-unreach")
1647 self.assertEqual(icmpcodes[icmp.type][icmp.code],
1648 "fragmentation-needed")
1649 self.assertEqual(icmp.src, self.pg0.remote_ip4)
1650 self.assertEqual(icmp.dst, self.pg1.remote_ip4)
1652 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [2500, 0, 0, 0])
1653 rx = self.send_and_expect(self.pg0, p_mtu * NUM_PKTS, self.pg1)
1655 # Reset MTU for subsequent tests
1656 self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [9000, 0, 0, 0])
1659 # source address 0.0.0.0 and 25.255.255.255 and for-us
1661 p_s0 = (Ether(src=self.pg0.remote_mac,
1662 dst=self.pg0.local_mac) /
1664 dst=self.pg0.local_ip4) /
1666 Raw(load='\x0a' * 18))
1667 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
1669 p_s0 = (Ether(src=self.pg0.remote_mac,
1670 dst=self.pg0.local_mac) /
1671 IP(src="255.255.255.255",
1672 dst=self.pg0.local_ip4) /
1674 Raw(load='\x0a' * 18))
1675 rx = self.send_and_assert_no_replies(self.pg0, p_s0 * 17)
1678 class TestIPDirectedBroadcast(VppTestCase):
1679 """ IPv4 Directed Broadcast """
1682 def setUpClass(cls):
1683 super(TestIPDirectedBroadcast, cls).setUpClass()
1686 def tearDownClass(cls):
1687 super(TestIPDirectedBroadcast, cls).tearDownClass()
1690 super(TestIPDirectedBroadcast, self).setUp()
1692 self.create_pg_interfaces(range(2))
1694 for i in self.pg_interfaces:
1698 super(TestIPDirectedBroadcast, self).tearDown()
1699 for i in self.pg_interfaces:
1702 def test_ip_input(self):
1703 """ IP Directed Broadcast """
1706 # set the directed broadcast on pg0 first, then config IP4 addresses
1707 # for pg1 directed broadcast is always disabled
1708 self.vapi.sw_interface_set_ip_directed_broadcast(
1709 self.pg0.sw_if_index, 1)
1711 p0 = (Ether(src=self.pg1.remote_mac,
1712 dst=self.pg1.local_mac) /
1714 dst=self.pg0._local_ip4_bcast) /
1715 UDP(sport=1234, dport=1234) /
1717 p1 = (Ether(src=self.pg0.remote_mac,
1718 dst=self.pg0.local_mac) /
1720 dst=self.pg1._local_ip4_bcast) /
1721 UDP(sport=1234, dport=1234) /
1724 self.pg0.config_ip4()
1725 self.pg0.resolve_arp()
1726 self.pg1.config_ip4()
1727 self.pg1.resolve_arp()
1730 # test packet is L2 broadcast
1732 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
1733 self.assertTrue(rx[0][Ether].dst, "ff:ff:ff:ff:ff:ff")
1735 self.send_and_assert_no_replies(self.pg0, p1 * NUM_PKTS,
1736 "directed broadcast disabled")
1739 # toggle directed broadcast on pg0
1741 self.vapi.sw_interface_set_ip_directed_broadcast(
1742 self.pg0.sw_if_index, 0)
1743 self.send_and_assert_no_replies(self.pg1, p0 * NUM_PKTS,
1744 "directed broadcast disabled")
1746 self.vapi.sw_interface_set_ip_directed_broadcast(
1747 self.pg0.sw_if_index, 1)
1748 rx = self.send_and_expect(self.pg1, p0 * NUM_PKTS, self.pg0)
1750 self.pg0.unconfig_ip4()
1751 self.pg1.unconfig_ip4()
1754 class TestIPLPM(VppTestCase):
1755 """ IPv4 longest Prefix Match """
1758 def setUpClass(cls):
1759 super(TestIPLPM, cls).setUpClass()
1762 def tearDownClass(cls):
1763 super(TestIPLPM, cls).tearDownClass()
1766 super(TestIPLPM, self).setUp()
1768 self.create_pg_interfaces(range(4))
1770 for i in self.pg_interfaces:
1776 super(TestIPLPM, self).tearDown()
1777 for i in self.pg_interfaces:
1781 def test_ip_lpm(self):
1782 """ IP longest Prefix Match """
1784 s_24 = VppIpRoute(self, "10.1.2.0", 24,
1785 [VppRoutePath(self.pg1.remote_ip4,
1786 self.pg1.sw_if_index)])
1787 s_24.add_vpp_config()
1788 s_8 = VppIpRoute(self, "10.0.0.0", 8,
1789 [VppRoutePath(self.pg2.remote_ip4,
1790 self.pg2.sw_if_index)])
1791 s_8.add_vpp_config()
1793 p_8 = (Ether(src=self.pg0.remote_mac,
1794 dst=self.pg0.local_mac) /
1797 UDP(sport=1234, dport=1234) /
1799 p_24 = (Ether(src=self.pg0.remote_mac,
1800 dst=self.pg0.local_mac) /
1803 UDP(sport=1234, dport=1234) /
1806 self.logger.info(self.vapi.cli("sh ip fib mtrie"))
1807 rx = self.send_and_expect(self.pg0, p_8 * NUM_PKTS, self.pg2)
1808 rx = self.send_and_expect(self.pg0, p_24 * NUM_PKTS, self.pg1)
1811 class TestIPv4Frag(VppTestCase):
1812 """ IPv4 fragmentation """
1815 def setUpClass(cls):
1816 super(TestIPv4Frag, cls).setUpClass()
1818 cls.create_pg_interfaces([0, 1])
1819 cls.src_if = cls.pg0
1820 cls.dst_if = cls.pg1
1822 # setup all interfaces
1823 for i in cls.pg_interfaces:
1829 def tearDownClass(cls):
1830 super(TestIPv4Frag, cls).tearDownClass()
1832 def test_frag_large_packets(self):
1833 """ Fragmentation of large packets """
1835 p = (Ether(dst=self.src_if.local_mac, src=self.src_if.remote_mac) /
1836 IP(src=self.src_if.remote_ip4, dst=self.dst_if.remote_ip4) /
1837 UDP(sport=1234, dport=5678) / Raw())
1838 self.extend_packet(p, 6000, "abcde")
1839 saved_payload = p[Raw].load
1841 # Force fragmentation by setting MTU of output interface
1842 # lower than packet size
1843 self.vapi.sw_interface_set_mtu(self.dst_if.sw_if_index,
1846 self.pg_enable_capture()
1847 self.src_if.add_stream(p)
1850 # Expecting 3 fragments because size of created fragments currently
1851 # cannot be larger then VPP buffer size (which is 2048)
1852 packets = self.dst_if.get_capture(3)
1854 # Assume VPP sends the fragments in order
1857 payload_offset = p.frag * 8
1858 if payload_offset > 0:
1859 payload_offset -= 8 # UDP header is not in payload
1860 self.assert_equal(payload_offset, len(payload))
1861 payload += p[Raw].load
1862 self.assert_equal(payload, saved_payload, "payload")
1865 if __name__ == '__main__':
1866 unittest.main(testRunner=VppTestRunner)