6 from parameterized import parameterized
7 import scapy.layers.inet6 as inet6
8 from scapy.contrib.mpls import MPLS
9 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_RS, \
10 ICMPv6ND_RA, ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
11 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
12 ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply
13 from scapy.layers.l2 import Ether, Dot1Q
14 from scapy.packet import Raw
15 from scapy.utils import inet_pton, inet_ntop
16 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
20 from framework import VppTestCase, VppTestRunner
21 from util import ppp, ip6_normalize
22 from vpp_ip import DpoProto
23 from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
24 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
25 VppMplsRoute, VppMplsTable, VppIpTable
26 from vpp_neighbor import find_nbr, VppNeighbor
27 from vpp_pg_interface import is_ipv6_misc
28 from vpp_sub_interface import VppSubInterface, VppDot1QSubint
29 from ipaddress import IPv6Network, IPv4Network
31 AF_INET6 = socket.AF_INET6
40 euid = in6_mactoifaceid(mac)
41 addr = "fe80::" + euid
45 class TestIPv6ND(VppTestCase):
46 def validate_ra(self, intf, rx, dst_ip=None):
48 dst_ip = intf.remote_ip6
50 # unicasted packets must come to the unicast mac
51 self.assertEqual(rx[Ether].dst, intf.remote_mac)
53 # and from the router's MAC
54 self.assertEqual(rx[Ether].src, intf.local_mac)
56 # the rx'd RA should be addressed to the sender's source
57 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
58 self.assertEqual(in6_ptop(rx[IPv6].dst),
61 # and come from the router's link local
62 self.assertTrue(in6_islladdr(rx[IPv6].src))
63 self.assertEqual(in6_ptop(rx[IPv6].src),
64 in6_ptop(mk_ll_addr(intf.local_mac)))
66 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
68 dst_ip = intf.remote_ip6
70 dst_ip = intf.local_ip6
72 # unicasted packets must come to the unicast mac
73 self.assertEqual(rx[Ether].dst, intf.remote_mac)
75 # and from the router's MAC
76 self.assertEqual(rx[Ether].src, intf.local_mac)
78 # the rx'd NA should be addressed to the sender's source
79 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
80 self.assertEqual(in6_ptop(rx[IPv6].dst),
83 # and come from the target address
85 in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
87 # Dest link-layer options should have the router's MAC
88 dll = rx[ICMPv6NDOptDstLLAddr]
89 self.assertEqual(dll.lladdr, intf.local_mac)
91 def validate_ns(self, intf, rx, tgt_ip):
92 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
93 dst_ip = inet_ntop(AF_INET6, nsma)
96 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
98 # and from the router's MAC
99 self.assertEqual(rx[Ether].src, intf.local_mac)
101 # the rx'd NS should be addressed to an mcast address
102 # derived from the target address
104 in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
106 # expect the tgt IP in the NS header
108 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
110 # packet is from the router's local address
112 in6_ptop(rx[IPv6].src), intf.local_ip6)
114 # Src link-layer options should have the router's MAC
115 sll = rx[ICMPv6NDOptSrcLLAddr]
116 self.assertEqual(sll.lladdr, intf.local_mac)
118 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
119 filter_out_fn=is_ipv6_misc):
120 intf.add_stream(pkts)
121 self.pg_enable_capture(self.pg_interfaces)
123 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
125 self.assertEqual(len(rx), 1)
127 self.validate_ra(intf, rx, dst_ip)
129 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
131 filter_out_fn=is_ipv6_misc):
132 intf.add_stream(pkts)
133 self.pg_enable_capture(self.pg_interfaces)
135 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
137 self.assertEqual(len(rx), 1)
139 self.validate_na(intf, rx, dst_ip, tgt_ip)
141 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
142 filter_out_fn=is_ipv6_misc):
143 tx_intf.add_stream(pkts)
144 self.pg_enable_capture(self.pg_interfaces)
146 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
148 self.assertEqual(len(rx), 1)
150 self.validate_ns(rx_intf, rx, tgt_ip)
152 def verify_ip(self, rx, smac, dmac, sip, dip):
154 self.assertEqual(ether.dst, dmac)
155 self.assertEqual(ether.src, smac)
158 self.assertEqual(ip.src, sip)
159 self.assertEqual(ip.dst, dip)
162 class TestIPv6(TestIPv6ND):
163 """ IPv6 Test Case """
167 super(TestIPv6, cls).setUpClass()
171 Perform test setup before test case.
174 - create 3 pg interfaces
175 - untagged pg0 interface
176 - Dot1Q subinterface on pg1
177 - Dot1AD subinterface on pg2
179 - put it into UP state
181 - resolve neighbor address using NDP
182 - configure 200 fib entries
184 :ivar list interfaces: pg interfaces and subinterfaces.
185 :ivar dict flows: IPv4 packet flows in test.
187 *TODO:* Create AD sub interface
189 super(TestIPv6, self).setUp()
191 # create 3 pg interfaces
192 self.create_pg_interfaces(range(3))
194 # create 2 subinterfaces for p1 and pg2
195 self.sub_interfaces = [
196 VppDot1QSubint(self, self.pg1, 100),
197 VppDot1QSubint(self, self.pg2, 200)
198 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
201 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
203 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
204 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
205 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
208 self.pg_if_packet_sizes = [64, 1500, 9020]
210 self.interfaces = list(self.pg_interfaces)
211 self.interfaces.extend(self.sub_interfaces)
213 # setup all interfaces
214 for i in self.interfaces:
219 # config 2M FIB entries
220 self.config_fib_entries(200)
223 """Run standard test teardown and log ``show ip6 neighbors``."""
224 for i in self.interfaces:
228 for i in self.sub_interfaces:
229 i.remove_vpp_config()
231 super(TestIPv6, self).tearDown()
232 if not self.vpp_dead:
233 self.logger.info(self.vapi.cli("show ip6 neighbors"))
234 # info(self.vapi.cli("show ip6 fib")) # many entries
236 def config_fib_entries(self, count):
237 """For each interface add to the FIB table *count* routes to
238 "fd02::1/128" destination with interface's local address as next-hop
241 :param int count: Number of FIB entries.
243 - *TODO:* check if the next-hop address shouldn't be remote address
244 instead of local address.
246 n_int = len(self.interfaces)
249 dest_addr = inet_pton(AF_INET6, "fd02::1")
251 for i in self.interfaces:
252 next_hop_address = i.local_ip6n
253 for j in range(count / n_int):
254 self.vapi.ip_add_del_route(
255 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
257 if counter / count * 100 > percent:
258 self.logger.info("Configure %d FIB entries .. %d%% done" %
262 def modify_packet(self, src_if, packet_size, pkt):
263 """Add load, set destination IP and extend packet to required packet
264 size for defined interface.
266 :param VppInterface src_if: Interface to create packet for.
267 :param int packet_size: Required packet size.
268 :param Scapy pkt: Packet to be modified.
270 dst_if_idx = packet_size / 10 % 2
271 dst_if = self.flows[src_if][dst_if_idx]
272 info = self.create_packet_info(src_if, dst_if)
273 payload = self.info_to_payload(info)
274 p = pkt / Raw(payload)
275 p[IPv6].dst = dst_if.remote_ip6
277 if isinstance(src_if, VppSubInterface):
278 p = src_if.add_dot1_layer(p)
279 self.extend_packet(p, packet_size)
283 def create_stream(self, src_if):
284 """Create input packet stream for defined interface.
286 :param VppInterface src_if: Interface to create packet stream for.
288 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
289 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
290 IPv6(src=src_if.remote_ip6) /
291 inet6.UDP(sport=1234, dport=1234))
293 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
294 for i in moves.range(self.pg_if_packet_sizes[0],
295 self.pg_if_packet_sizes[1], 10)]
296 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
297 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
298 self.pg_if_packet_sizes[2] + hdr_ext,
304 def verify_capture(self, dst_if, capture):
305 """Verify captured input packet stream for defined interface.
307 :param VppInterface dst_if: Interface to verify captured packet stream
309 :param list capture: Captured packet stream.
311 self.logger.info("Verifying capture on interface %s" % dst_if.name)
313 for i in self.interfaces:
314 last_info[i.sw_if_index] = None
316 dst_sw_if_index = dst_if.sw_if_index
317 if hasattr(dst_if, 'parent'):
319 for packet in capture:
321 # Check VLAN tags and Ethernet header
322 packet = dst_if.remove_dot1_layer(packet)
323 self.assertTrue(Dot1Q not in packet)
326 udp = packet[inet6.UDP]
327 payload_info = self.payload_to_info(str(packet[Raw]))
328 packet_index = payload_info.index
329 self.assertEqual(payload_info.dst, dst_sw_if_index)
331 "Got packet on port %s: src=%u (id=%u)" %
332 (dst_if.name, payload_info.src, packet_index))
333 next_info = self.get_next_packet_info_for_interface2(
334 payload_info.src, dst_sw_if_index,
335 last_info[payload_info.src])
336 last_info[payload_info.src] = next_info
337 self.assertTrue(next_info is not None)
338 self.assertEqual(packet_index, next_info.index)
339 saved_packet = next_info.data
340 # Check standard fields
342 ip.src, saved_packet[IPv6].src)
344 ip.dst, saved_packet[IPv6].dst)
346 udp.sport, saved_packet[inet6.UDP].sport)
348 udp.dport, saved_packet[inet6.UDP].dport)
350 self.logger.error(ppp("Unexpected or invalid packet:", packet))
352 for i in self.interfaces:
353 remaining_packet = self.get_next_packet_info_for_interface2(
354 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
355 self.assertTrue(remaining_packet is None,
356 "Interface %s: Packet expected from interface %s "
357 "didn't arrive" % (dst_if.name, i.name))
363 - Create IPv6 stream for pg0 interface
364 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
365 - Send and verify received packets on each interface.
368 pkts = self.create_stream(self.pg0)
369 self.pg0.add_stream(pkts)
371 for i in self.sub_interfaces:
372 pkts = self.create_stream(i)
373 i.parent.add_stream(pkts)
375 self.pg_enable_capture(self.pg_interfaces)
378 pkts = self.pg0.get_capture()
379 self.verify_capture(self.pg0, pkts)
381 for i in self.sub_interfaces:
382 pkts = i.parent.get_capture()
383 self.verify_capture(i, pkts)
386 """ IPv6 Neighbour Solicitation Exceptions
389 - Send an NS Sourced from an address not covered by the link sub-net
390 - Send an NS to an mcast address the router has not joined
391 - Send NS for a target address the router does not onn.
395 # An NS from a non link source address
397 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
398 d = inet_ntop(AF_INET6, nsma)
400 p = (Ether(dst=in6_getnsmac(nsma)) /
401 IPv6(dst=d, src="2002::2") /
402 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
403 ICMPv6NDOptSrcLLAddr(
404 lladdr=self.pg0.remote_mac))
407 self.send_and_assert_no_replies(
409 "No response to NS source by address not on sub-net")
412 # An NS for sent to a solicited mcast group the router is
413 # not a member of FAILS
416 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
417 d = inet_ntop(AF_INET6, nsma)
419 p = (Ether(dst=in6_getnsmac(nsma)) /
420 IPv6(dst=d, src=self.pg0.remote_ip6) /
421 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
422 ICMPv6NDOptSrcLLAddr(
423 lladdr=self.pg0.remote_mac))
426 self.send_and_assert_no_replies(
428 "No response to NS sent to unjoined mcast address")
431 # An NS whose target address is one the router does not own
433 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
434 d = inet_ntop(AF_INET6, nsma)
436 p = (Ether(dst=in6_getnsmac(nsma)) /
437 IPv6(dst=d, src=self.pg0.remote_ip6) /
438 ICMPv6ND_NS(tgt="fd::ffff") /
439 ICMPv6NDOptSrcLLAddr(
440 lladdr=self.pg0.remote_mac))
443 self.send_and_assert_no_replies(self.pg0, pkts,
444 "No response to NS for unknown target")
447 # A neighbor entry that has no associated FIB-entry
449 self.pg0.generate_remote_hosts(4)
450 nd_entry = VppNeighbor(self,
451 self.pg0.sw_if_index,
452 self.pg0.remote_hosts[2].mac,
453 self.pg0.remote_hosts[2].ip6,
455 nd_entry.add_vpp_config()
458 # check we have the neighbor, but no route
460 self.assertTrue(find_nbr(self,
461 self.pg0.sw_if_index,
462 self.pg0._remote_hosts[2].ip6))
463 self.assertFalse(find_route(self,
464 self.pg0._remote_hosts[2].ip6,
469 # send an NS from a link local address to the interface's global
472 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
474 dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
475 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
476 ICMPv6NDOptSrcLLAddr(
477 lladdr=self.pg0.remote_mac))
479 self.send_and_expect_na(self.pg0, p,
480 "NS from link-local",
481 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
482 tgt_ip=self.pg0.local_ip6)
485 # we should have learned an ND entry for the peer's link-local
486 # but not inserted a route to it in the FIB
488 self.assertTrue(find_nbr(self,
489 self.pg0.sw_if_index,
490 self.pg0._remote_hosts[2].ip6_ll))
491 self.assertFalse(find_route(self,
492 self.pg0._remote_hosts[2].ip6_ll,
497 # An NS to the router's own Link-local
499 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
501 dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
502 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
503 ICMPv6NDOptSrcLLAddr(
504 lladdr=self.pg0.remote_mac))
506 self.send_and_expect_na(self.pg0, p,
507 "NS to/from link-local",
508 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
509 tgt_ip=self.pg0.local_ip6_ll)
512 # we should have learned an ND entry for the peer's link-local
513 # but not inserted a route to it in the FIB
515 self.assertTrue(find_nbr(self,
516 self.pg0.sw_if_index,
517 self.pg0._remote_hosts[3].ip6_ll))
518 self.assertFalse(find_route(self,
519 self.pg0._remote_hosts[3].ip6_ll,
523 def test_ns_duplicates(self):
527 # Generate some hosts on the LAN
529 self.pg1.generate_remote_hosts(3)
532 # Add host 1 on pg1 and pg2
534 ns_pg1 = VppNeighbor(self,
535 self.pg1.sw_if_index,
536 self.pg1.remote_hosts[1].mac,
537 self.pg1.remote_hosts[1].ip6)
538 ns_pg1.add_vpp_config()
539 ns_pg2 = VppNeighbor(self,
540 self.pg2.sw_if_index,
542 self.pg1.remote_hosts[1].ip6)
543 ns_pg2.add_vpp_config()
546 # IP packet destined for pg1 remote host arrives on pg1 again.
548 p = (Ether(dst=self.pg0.local_mac,
549 src=self.pg0.remote_mac) /
550 IPv6(src=self.pg0.remote_ip6,
551 dst=self.pg1.remote_hosts[1].ip6) /
552 inet6.UDP(sport=1234, dport=1234) /
555 self.pg0.add_stream(p)
556 self.pg_enable_capture(self.pg_interfaces)
559 rx1 = self.pg1.get_capture(1)
561 self.verify_ip(rx1[0],
563 self.pg1.remote_hosts[1].mac,
565 self.pg1.remote_hosts[1].ip6)
568 # remove the duplicate on pg1
569 # packet stream shoud generate NSs out of pg1
571 ns_pg1.remove_vpp_config()
573 self.send_and_expect_ns(self.pg0, self.pg1,
574 p, self.pg1.remote_hosts[1].ip6)
579 ns_pg1.add_vpp_config()
581 self.pg0.add_stream(p)
582 self.pg_enable_capture(self.pg_interfaces)
585 rx1 = self.pg1.get_capture(1)
587 self.verify_ip(rx1[0],
589 self.pg1.remote_hosts[1].mac,
591 self.pg1.remote_hosts[1].ip6)
593 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
595 dst_ip = intf.remote_ip6
597 # unicasted packets must come to the unicast mac
598 self.assertEqual(rx[Ether].dst, intf.remote_mac)
600 # and from the router's MAC
601 self.assertEqual(rx[Ether].src, intf.local_mac)
603 # the rx'd RA should be addressed to the sender's source
604 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
605 self.assertEqual(in6_ptop(rx[IPv6].dst),
608 # and come from the router's link local
609 self.assertTrue(in6_islladdr(rx[IPv6].src))
610 self.assertEqual(in6_ptop(rx[IPv6].src),
611 in6_ptop(mk_ll_addr(intf.local_mac)))
613 # it should contain the links MTU
615 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
617 # it should contain the source's link layer address option
618 sll = ra[ICMPv6NDOptSrcLLAddr]
619 self.assertEqual(sll.lladdr, intf.local_mac)
622 # the RA should not contain prefix information
623 self.assertFalse(ra.haslayer(
624 ICMPv6NDOptPrefixInfo))
626 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
628 # the options are nested in the scapy packet in way that i cannot
629 # decipher how to decode. this 1st layer of option always returns
630 # nested classes, so a direct obj1=obj2 comparison always fails.
631 # however, the getlayer(.., 2) does give one instnace.
632 # so we cheat here and construct a new opt instnace for comparison
633 rd = ICMPv6NDOptPrefixInfo(
634 prefixlen=raos.prefixlen,
638 if type(pi_opt) is list:
639 for ii in range(len(pi_opt)):
640 self.assertEqual(pi_opt[ii], rd)
642 ICMPv6NDOptPrefixInfo, ii + 2)
644 self.assertEqual(pi_opt, raos)
646 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
647 filter_out_fn=is_ipv6_misc,
649 intf.add_stream(pkts)
650 self.pg_enable_capture(self.pg_interfaces)
652 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
654 self.assertEqual(len(rx), 1)
656 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
659 """ IPv6 Router Solicitation Exceptions
665 # Before we begin change the IPv6 RA responses to use the unicast
666 # address - that way we will not confuse them with the periodic
667 # RAs which go to the mcast address
668 # Sit and wait for the first periodic RA.
672 self.pg0.ip6_ra_config(send_unicast=1)
675 # An RS from a link source address
676 # - expect an RA in return
678 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
680 dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
683 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
686 # For the next RS sent the RA should be rate limited
688 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
691 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
692 # so we need to do this before each test below so as not to drop
693 # packets for rate limiting reasons. Test this works here.
695 self.pg0.ip6_ra_config(send_unicast=1)
696 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
699 # An RS sent from a non-link local source
701 self.pg0.ip6_ra_config(send_unicast=1)
702 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
703 IPv6(dst=self.pg0.local_ip6,
707 self.send_and_assert_no_replies(self.pg0, pkts,
708 "RS from non-link source")
711 # Source an RS from a link local address
713 self.pg0.ip6_ra_config(send_unicast=1)
714 ll = mk_ll_addr(self.pg0.remote_mac)
715 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
716 IPv6(dst=self.pg0.local_ip6, src=ll) /
719 self.send_and_expect_ra(self.pg0, pkts,
720 "RS sourced from link-local",
724 # Send the RS multicast
726 self.pg0.ip6_ra_config(send_unicast=1)
727 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
728 ll = mk_ll_addr(self.pg0.remote_mac)
729 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
730 IPv6(dst="ff02::2", src=ll) /
733 self.send_and_expect_ra(self.pg0, pkts,
734 "RS sourced from link-local",
738 # Source from the unspecified address ::. This happens when the RS
739 # is sent before the host has a configured address/sub-net,
740 # i.e. auto-config. Since the sender has no IP address, the reply
741 # comes back mcast - so the capture needs to not filter this.
742 # If we happen to pick up the periodic RA at this point then so be it,
745 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
746 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
747 IPv6(dst="ff02::2", src="::") /
750 self.send_and_expect_ra(self.pg0, pkts,
751 "RS sourced from unspecified",
756 # Configure The RA to announce the links prefix
758 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
759 self.pg0.local_ip6_prefix_len)
762 # RAs should now contain the prefix information option
764 opt = ICMPv6NDOptPrefixInfo(
765 prefixlen=self.pg0.local_ip6_prefix_len,
766 prefix=self.pg0.local_ip6,
770 self.pg0.ip6_ra_config(send_unicast=1)
771 ll = mk_ll_addr(self.pg0.remote_mac)
772 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
773 IPv6(dst=self.pg0.local_ip6, src=ll) /
775 self.send_and_expect_ra(self.pg0, p,
776 "RA with prefix-info",
781 # Change the prefix info to not off-link
784 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
785 self.pg0.local_ip6_prefix_len,
788 opt = ICMPv6NDOptPrefixInfo(
789 prefixlen=self.pg0.local_ip6_prefix_len,
790 prefix=self.pg0.local_ip6,
794 self.pg0.ip6_ra_config(send_unicast=1)
795 self.send_and_expect_ra(self.pg0, p,
796 "RA with Prefix info with L-flag=0",
801 # Change the prefix info to not off-link, no-autoconfig
802 # L and A flag are clear in the advert
804 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
805 self.pg0.local_ip6_prefix_len,
809 opt = ICMPv6NDOptPrefixInfo(
810 prefixlen=self.pg0.local_ip6_prefix_len,
811 prefix=self.pg0.local_ip6,
815 self.pg0.ip6_ra_config(send_unicast=1)
816 self.send_and_expect_ra(self.pg0, p,
817 "RA with Prefix info with A & L-flag=0",
822 # Change the flag settings back to the defaults
823 # L and A flag are set in the advert
825 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
826 self.pg0.local_ip6_prefix_len)
828 opt = ICMPv6NDOptPrefixInfo(
829 prefixlen=self.pg0.local_ip6_prefix_len,
830 prefix=self.pg0.local_ip6,
834 self.pg0.ip6_ra_config(send_unicast=1)
835 self.send_and_expect_ra(self.pg0, p,
836 "RA with Prefix info",
841 # Change the prefix info to not off-link, no-autoconfig
842 # L and A flag are clear in the advert
844 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
845 self.pg0.local_ip6_prefix_len,
849 opt = ICMPv6NDOptPrefixInfo(
850 prefixlen=self.pg0.local_ip6_prefix_len,
851 prefix=self.pg0.local_ip6,
855 self.pg0.ip6_ra_config(send_unicast=1)
856 self.send_and_expect_ra(self.pg0, p,
857 "RA with Prefix info with A & L-flag=0",
862 # Use the reset to defults option to revert to defaults
863 # L and A flag are clear in the advert
865 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
866 self.pg0.local_ip6_prefix_len,
869 opt = ICMPv6NDOptPrefixInfo(
870 prefixlen=self.pg0.local_ip6_prefix_len,
871 prefix=self.pg0.local_ip6,
875 self.pg0.ip6_ra_config(send_unicast=1)
876 self.send_and_expect_ra(self.pg0, p,
877 "RA with Prefix reverted to defaults",
882 # Advertise Another prefix. With no L-flag/A-flag
884 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
885 self.pg1.local_ip6_prefix_len,
889 opt = [ICMPv6NDOptPrefixInfo(
890 prefixlen=self.pg0.local_ip6_prefix_len,
891 prefix=self.pg0.local_ip6,
894 ICMPv6NDOptPrefixInfo(
895 prefixlen=self.pg1.local_ip6_prefix_len,
896 prefix=self.pg1.local_ip6,
900 self.pg0.ip6_ra_config(send_unicast=1)
901 ll = mk_ll_addr(self.pg0.remote_mac)
902 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
903 IPv6(dst=self.pg0.local_ip6, src=ll) /
905 self.send_and_expect_ra(self.pg0, p,
906 "RA with multiple Prefix infos",
911 # Remove the first refix-info - expect the second is still in the
914 self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
915 self.pg0.local_ip6_prefix_len,
918 opt = ICMPv6NDOptPrefixInfo(
919 prefixlen=self.pg1.local_ip6_prefix_len,
920 prefix=self.pg1.local_ip6,
924 self.pg0.ip6_ra_config(send_unicast=1)
925 self.send_and_expect_ra(self.pg0, p,
926 "RA with Prefix reverted to defaults",
931 # Remove the second prefix-info - expect no prefix-info i nthe adverts
933 self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
934 self.pg1.local_ip6_prefix_len,
937 self.pg0.ip6_ra_config(send_unicast=1)
938 self.send_and_expect_ra(self.pg0, p,
939 "RA with Prefix reverted to defaults",
943 # Reset the periodic advertisements back to default values
945 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
948 class TestICMPv6Echo(VppTestCase):
949 """ ICMPv6 Echo Test Case """
952 super(TestICMPv6Echo, self).setUp()
954 # create 1 pg interface
955 self.create_pg_interfaces(range(1))
957 for i in self.pg_interfaces:
963 super(TestICMPv6Echo, self).tearDown()
964 for i in self.pg_interfaces:
969 def test_icmpv6_echo(self):
970 """ VPP replies to ICMPv6 Echo Request
974 - Receive ICMPv6 Echo Request message on pg0 interface.
975 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
980 icmpv6_data = '\x0a' * 18
981 p_echo_request = (Ether(src=self.pg0.remote_mac,
982 dst=self.pg0.local_mac) /
983 IPv6(src=self.pg0.remote_ip6,
984 dst=self.pg0.local_ip6) /
990 self.pg0.add_stream(p_echo_request)
991 self.pg_enable_capture(self.pg_interfaces)
994 rx = self.pg0.get_capture(1)
998 icmpv6 = rx[ICMPv6EchoReply]
1000 self.assertEqual(ether.src, self.pg0.local_mac)
1001 self.assertEqual(ether.dst, self.pg0.remote_mac)
1003 self.assertEqual(ipv6.src, self.pg0.local_ip6)
1004 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
1007 icmp6types[icmpv6.type], "Echo Reply")
1008 self.assertEqual(icmpv6.id, icmpv6_id)
1009 self.assertEqual(icmpv6.seq, icmpv6_seq)
1010 self.assertEqual(icmpv6.data, icmpv6_data)
1013 class TestIPv6RD(TestIPv6ND):
1014 """ IPv6 Router Discovery Test Case """
1017 def setUpClass(cls):
1018 super(TestIPv6RD, cls).setUpClass()
1021 super(TestIPv6RD, self).setUp()
1023 # create 2 pg interfaces
1024 self.create_pg_interfaces(range(2))
1026 self.interfaces = list(self.pg_interfaces)
1028 # setup all interfaces
1029 for i in self.interfaces:
1034 for i in self.interfaces:
1037 super(TestIPv6RD, self).tearDown()
1039 def test_rd_send_router_solicitation(self):
1040 """ Verify router solicitation packets """
1043 self.pg_enable_capture(self.pg_interfaces)
1045 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1047 rx_list = self.pg1.get_capture(count, timeout=3)
1048 self.assertEqual(len(rx_list), count)
1049 for packet in rx_list:
1050 self.assertEqual(packet.haslayer(IPv6), 1)
1051 self.assertEqual(packet[IPv6].haslayer(
1053 dst = ip6_normalize(packet[IPv6].dst)
1054 dst2 = ip6_normalize("ff02::2")
1055 self.assert_equal(dst, dst2)
1056 src = ip6_normalize(packet[IPv6].src)
1057 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1058 self.assert_equal(src, src2)
1060 bool(packet[ICMPv6ND_RS].haslayer(
1061 ICMPv6NDOptSrcLLAddr)))
1063 packet[ICMPv6NDOptSrcLLAddr].lladdr,
1066 def verify_prefix_info(self, reported_prefix, prefix_option):
1067 prefix = IPv6Network(
1068 text_type(prefix_option.getfieldval("prefix") +
1070 text_type(prefix_option.getfieldval("prefixlen"))),
1072 self.assert_equal(reported_prefix.prefix.network_address,
1073 prefix.network_address)
1074 L = prefix_option.getfieldval("L")
1075 A = prefix_option.getfieldval("A")
1076 option_flags = (L << 7) | (A << 6)
1077 self.assert_equal(reported_prefix.flags, option_flags)
1078 self.assert_equal(reported_prefix.valid_time,
1079 prefix_option.getfieldval("validlifetime"))
1080 self.assert_equal(reported_prefix.preferred_time,
1081 prefix_option.getfieldval("preferredlifetime"))
1083 def test_rd_receive_router_advertisement(self):
1084 """ Verify events triggered by received RA packets """
1086 self.vapi.want_ip6_ra_events()
1088 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1092 preferredlifetime=500,
1097 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1101 preferredlifetime=1000,
1106 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1107 IPv6(dst=self.pg1.local_ip6_ll,
1108 src=mk_ll_addr(self.pg1.remote_mac)) /
1112 self.pg1.add_stream([p])
1115 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1117 self.assert_equal(ev.current_hop_limit, 0)
1118 self.assert_equal(ev.flags, 8)
1119 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1120 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1122 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1124 self.assert_equal(ev.n_prefixes, 2)
1126 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1127 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1130 class TestIPv6RDControlPlane(TestIPv6ND):
1131 """ IPv6 Router Discovery Control Plane Test Case """
1134 def setUpClass(cls):
1135 super(TestIPv6RDControlPlane, cls).setUpClass()
1138 super(TestIPv6RDControlPlane, self).setUp()
1140 # create 1 pg interface
1141 self.create_pg_interfaces(range(1))
1143 self.interfaces = list(self.pg_interfaces)
1145 # setup all interfaces
1146 for i in self.interfaces:
1151 super(TestIPv6RDControlPlane, self).tearDown()
1154 def create_ra_packet(pg, routerlifetime=None):
1155 src_ip = pg.remote_ip6_ll
1156 dst_ip = pg.local_ip6
1157 if routerlifetime is not None:
1158 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1161 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1162 IPv6(dst=dst_ip, src=src_ip) / ra)
1166 def get_default_routes(fib):
1169 if entry.address_length == 0:
1170 for path in entry.path:
1171 if path.sw_if_index != 0xFFFFFFFF:
1173 defaut_route['sw_if_index'] = path.sw_if_index
1174 defaut_route['next_hop'] = path.next_hop
1175 list.append(defaut_route)
1179 def get_interface_addresses(fib, pg):
1182 if entry.address_length == 128:
1183 path = entry.path[0]
1184 if path.sw_if_index == pg.sw_if_index:
1185 list.append(entry.address)
1189 """ Test handling of SLAAC addresses and default routes """
1191 fib = self.vapi.ip6_fib_dump()
1192 default_routes = self.get_default_routes(fib)
1193 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1194 self.assertEqual(default_routes, [])
1195 router_address = self.pg0.remote_ip6n_ll
1197 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1202 packet = (self.create_ra_packet(
1203 self.pg0) / ICMPv6NDOptPrefixInfo(
1207 preferredlifetime=2,
1210 ) / ICMPv6NDOptPrefixInfo(
1214 preferredlifetime=1000,
1218 self.pg0.add_stream([packet])
1223 fib = self.vapi.ip6_fib_dump()
1225 # check FIB for new address
1226 addresses = set(self.get_interface_addresses(fib, self.pg0))
1227 new_addresses = addresses.difference(initial_addresses)
1228 self.assertEqual(len(new_addresses), 1)
1229 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1230 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1232 # check FIB for new default route
1233 default_routes = self.get_default_routes(fib)
1234 self.assertEqual(len(default_routes), 1)
1235 dr = default_routes[0]
1236 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1237 self.assertEqual(dr['next_hop'], router_address)
1239 # send RA to delete default route
1240 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1241 self.pg0.add_stream([packet])
1246 # check that default route is deleted
1247 fib = self.vapi.ip6_fib_dump()
1248 default_routes = self.get_default_routes(fib)
1249 self.assertEqual(len(default_routes), 0)
1254 packet = self.create_ra_packet(self.pg0)
1255 self.pg0.add_stream([packet])
1260 # check FIB for new default route
1261 fib = self.vapi.ip6_fib_dump()
1262 default_routes = self.get_default_routes(fib)
1263 self.assertEqual(len(default_routes), 1)
1264 dr = default_routes[0]
1265 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1266 self.assertEqual(dr['next_hop'], router_address)
1268 # send RA, updating router lifetime to 1s
1269 packet = self.create_ra_packet(self.pg0, 1)
1270 self.pg0.add_stream([packet])
1275 # check that default route still exists
1276 fib = self.vapi.ip6_fib_dump()
1277 default_routes = self.get_default_routes(fib)
1278 self.assertEqual(len(default_routes), 1)
1279 dr = default_routes[0]
1280 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1281 self.assertEqual(dr['next_hop'], router_address)
1285 # check that default route is deleted
1286 fib = self.vapi.ip6_fib_dump()
1287 default_routes = self.get_default_routes(fib)
1288 self.assertEqual(len(default_routes), 0)
1290 # check FIB still contains the SLAAC address
1291 addresses = set(self.get_interface_addresses(fib, self.pg0))
1292 new_addresses = addresses.difference(initial_addresses)
1293 self.assertEqual(len(new_addresses), 1)
1294 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1295 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1299 # check that SLAAC address is deleted
1300 fib = self.vapi.ip6_fib_dump()
1301 addresses = set(self.get_interface_addresses(fib, self.pg0))
1302 new_addresses = addresses.difference(initial_addresses)
1303 self.assertEqual(len(new_addresses), 0)
1306 class IPv6NDProxyTest(TestIPv6ND):
1307 """ IPv6 ND ProxyTest Case """
1310 super(IPv6NDProxyTest, self).setUp()
1312 # create 3 pg interfaces
1313 self.create_pg_interfaces(range(3))
1315 # pg0 is the master interface, with the configured subnet
1317 self.pg0.config_ip6()
1318 self.pg0.resolve_ndp()
1320 self.pg1.ip6_enable()
1321 self.pg2.ip6_enable()
1324 super(IPv6NDProxyTest, self).tearDown()
1326 def test_nd_proxy(self):
1327 """ IPv6 Proxy ND """
1330 # Generate some hosts in the subnet that we are proxying
1332 self.pg0.generate_remote_hosts(8)
1334 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1335 d = inet_ntop(AF_INET6, nsma)
1338 # Send an NS for one of those remote hosts on one of the proxy links
1339 # expect no response since it's from an address that is not
1340 # on the link that has the prefix configured
1342 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
1344 src=self.pg0._remote_hosts[2].ip6) /
1345 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1346 ICMPv6NDOptSrcLLAddr(
1347 lladdr=self.pg0._remote_hosts[2].mac))
1349 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1352 # Add proxy support for the host
1354 self.vapi.ip6_nd_proxy(
1355 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1356 self.pg1.sw_if_index)
1359 # try that NS again. this time we expect an NA back
1361 self.send_and_expect_na(self.pg1, ns_pg1,
1362 "NS to proxy entry",
1363 dst_ip=self.pg0._remote_hosts[2].ip6,
1364 tgt_ip=self.pg0.local_ip6)
1367 # ... and that we have an entry in the ND cache
1369 self.assertTrue(find_nbr(self,
1370 self.pg1.sw_if_index,
1371 self.pg0._remote_hosts[2].ip6))
1374 # ... and we can route traffic to it
1376 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1377 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1378 src=self.pg0.remote_ip6) /
1379 inet6.UDP(sport=10000, dport=20000) /
1382 self.pg0.add_stream(t)
1383 self.pg_enable_capture(self.pg_interfaces)
1385 rx = self.pg1.get_capture(1)
1388 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1389 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1391 self.assertEqual(rx[IPv6].src,
1393 self.assertEqual(rx[IPv6].dst,
1397 # Test we proxy for the host on the main interface
1399 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1400 IPv6(dst=d, src=self.pg0.remote_ip6) /
1402 tgt=self.pg0._remote_hosts[2].ip6) /
1403 ICMPv6NDOptSrcLLAddr(
1404 lladdr=self.pg0.remote_mac))
1406 self.send_and_expect_na(self.pg0, ns_pg0,
1407 "NS to proxy entry on main",
1408 tgt_ip=self.pg0._remote_hosts[2].ip6,
1409 dst_ip=self.pg0.remote_ip6)
1412 # Setup and resolve proxy for another host on another interface
1414 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1416 src=self.pg0._remote_hosts[3].ip6) /
1417 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1418 ICMPv6NDOptSrcLLAddr(
1419 lladdr=self.pg0._remote_hosts[2].mac))
1421 self.vapi.ip6_nd_proxy(
1422 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1423 self.pg2.sw_if_index)
1425 self.send_and_expect_na(self.pg2, ns_pg2,
1426 "NS to proxy entry other interface",
1427 dst_ip=self.pg0._remote_hosts[3].ip6,
1428 tgt_ip=self.pg0.local_ip6)
1430 self.assertTrue(find_nbr(self,
1431 self.pg2.sw_if_index,
1432 self.pg0._remote_hosts[3].ip6))
1435 # hosts can communicate. pg2->pg1
1437 t2 = (Ether(dst=self.pg2.local_mac,
1438 src=self.pg0.remote_hosts[3].mac) /
1439 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1440 src=self.pg0._remote_hosts[3].ip6) /
1441 inet6.UDP(sport=10000, dport=20000) /
1444 self.pg2.add_stream(t2)
1445 self.pg_enable_capture(self.pg_interfaces)
1447 rx = self.pg1.get_capture(1)
1450 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1451 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1453 self.assertEqual(rx[IPv6].src,
1455 self.assertEqual(rx[IPv6].dst,
1459 # remove the proxy configs
1461 self.vapi.ip6_nd_proxy(
1462 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1463 self.pg1.sw_if_index,
1465 self.vapi.ip6_nd_proxy(
1466 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1467 self.pg2.sw_if_index,
1470 self.assertFalse(find_nbr(self,
1471 self.pg2.sw_if_index,
1472 self.pg0._remote_hosts[3].ip6))
1473 self.assertFalse(find_nbr(self,
1474 self.pg1.sw_if_index,
1475 self.pg0._remote_hosts[2].ip6))
1478 # no longer proxy-ing...
1480 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1481 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1482 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1485 # no longer forwarding. traffic generates NS out of the glean/main
1488 self.pg2.add_stream(t2)
1489 self.pg_enable_capture(self.pg_interfaces)
1492 rx = self.pg0.get_capture(1)
1494 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1497 class TestIPNull(VppTestCase):
1498 """ IPv6 routes via NULL """
1501 super(TestIPNull, self).setUp()
1503 # create 2 pg interfaces
1504 self.create_pg_interfaces(range(1))
1506 for i in self.pg_interfaces:
1512 super(TestIPNull, self).tearDown()
1513 for i in self.pg_interfaces:
1517 def test_ip_null(self):
1518 """ IP NULL route """
1520 p = (Ether(src=self.pg0.remote_mac,
1521 dst=self.pg0.local_mac) /
1522 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1523 inet6.UDP(sport=1234, dport=1234) /
1527 # A route via IP NULL that will reply with ICMP unreachables
1529 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1530 ip_unreach.add_vpp_config()
1532 self.pg0.add_stream(p)
1533 self.pg_enable_capture(self.pg_interfaces)
1536 rx = self.pg0.get_capture(1)
1538 icmp = rx[ICMPv6DestUnreach]
1540 # 0 = "No route to destination"
1541 self.assertEqual(icmp.code, 0)
1543 # ICMP is rate limited. pause a bit
1547 # A route via IP NULL that will reply with ICMP prohibited
1549 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1550 is_prohibit=1, is_ip6=1)
1551 ip_prohibit.add_vpp_config()
1553 self.pg0.add_stream(p)
1554 self.pg_enable_capture(self.pg_interfaces)
1557 rx = self.pg0.get_capture(1)
1559 icmp = rx[ICMPv6DestUnreach]
1561 # 1 = "Communication with destination administratively prohibited"
1562 self.assertEqual(icmp.code, 1)
1565 class TestIPDisabled(VppTestCase):
1566 """ IPv6 disabled """
1569 super(TestIPDisabled, self).setUp()
1571 # create 2 pg interfaces
1572 self.create_pg_interfaces(range(2))
1576 self.pg0.config_ip6()
1577 self.pg0.resolve_ndp()
1579 # PG 1 is not IP enabled
1583 super(TestIPDisabled, self).tearDown()
1584 for i in self.pg_interfaces:
1588 def test_ip_disabled(self):
1593 # one accepting interface, pg0, 2 forwarding interfaces
1595 route_ff_01 = VppIpMRoute(
1599 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1600 [VppMRoutePath(self.pg1.sw_if_index,
1601 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1602 VppMRoutePath(self.pg0.sw_if_index,
1603 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1605 route_ff_01.add_vpp_config()
1607 pu = (Ether(src=self.pg1.remote_mac,
1608 dst=self.pg1.local_mac) /
1609 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1610 inet6.UDP(sport=1234, dport=1234) /
1612 pm = (Ether(src=self.pg1.remote_mac,
1613 dst=self.pg1.local_mac) /
1614 IPv6(src="2001::1", dst="ffef::1") /
1615 inet6.UDP(sport=1234, dport=1234) /
1619 # PG1 does not forward IP traffic
1621 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1622 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1627 self.pg1.config_ip6()
1630 # Now we get packets through
1632 self.pg1.add_stream(pu)
1633 self.pg_enable_capture(self.pg_interfaces)
1635 rx = self.pg0.get_capture(1)
1637 self.pg1.add_stream(pm)
1638 self.pg_enable_capture(self.pg_interfaces)
1640 rx = self.pg0.get_capture(1)
1645 self.pg1.unconfig_ip6()
1648 # PG1 does not forward IP traffic
1650 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1651 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1654 class TestIP6LoadBalance(VppTestCase):
1655 """ IPv6 Load-Balancing """
1658 super(TestIP6LoadBalance, self).setUp()
1660 self.create_pg_interfaces(range(5))
1662 mpls_tbl = VppMplsTable(self, 0)
1663 mpls_tbl.add_vpp_config()
1665 for i in self.pg_interfaces:
1672 for i in self.pg_interfaces:
1676 super(TestIP6LoadBalance, self).tearDown()
1678 def pg_send(self, input, pkts):
1679 self.vapi.cli("clear trace")
1680 input.add_stream(pkts)
1681 self.pg_enable_capture(self.pg_interfaces)
1684 def send_and_expect_load_balancing(self, input, pkts, outputs):
1685 self.pg_send(input, pkts)
1687 rx = oo._get_capture(1)
1688 self.assertNotEqual(0, len(rx))
1690 def send_and_expect_one_itf(self, input, pkts, itf):
1691 self.pg_send(input, pkts)
1692 rx = itf.get_capture(len(pkts))
1694 def test_ip6_load_balance(self):
1695 """ IPv6 Load-Balancing """
1698 # An array of packets that differ only in the destination port
1702 # - MPLS non-EOS with an entropy label
1706 port_mpls_neos_pkts = []
1710 # An array of packets that differ only in the source address
1715 for ii in range(65):
1717 IPv6(dst="3000::1", src="3000:1::1") /
1718 inet6.UDP(sport=1234, dport=1234 + ii) /
1720 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1721 dst=self.pg0.local_mac) /
1723 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1724 dst=self.pg0.local_mac) /
1725 MPLS(label=66, ttl=2) /
1727 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1728 dst=self.pg0.local_mac) /
1729 MPLS(label=67, ttl=2) /
1730 MPLS(label=77, ttl=2) /
1732 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1733 dst=self.pg0.local_mac) /
1734 MPLS(label=67, ttl=2) /
1735 MPLS(label=14, ttl=2) /
1736 MPLS(label=999, ttl=2) /
1739 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1740 inet6.UDP(sport=1234, dport=1234) /
1742 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1743 dst=self.pg0.local_mac) /
1745 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1746 dst=self.pg0.local_mac) /
1747 MPLS(label=66, ttl=2) /
1751 # A route for the IP pacekts
1753 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1754 [VppRoutePath(self.pg1.remote_ip6,
1755 self.pg1.sw_if_index,
1756 proto=DpoProto.DPO_PROTO_IP6),
1757 VppRoutePath(self.pg2.remote_ip6,
1758 self.pg2.sw_if_index,
1759 proto=DpoProto.DPO_PROTO_IP6)],
1761 route_3000_1.add_vpp_config()
1764 # a local-label for the EOS packets
1766 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1767 binding.add_vpp_config()
1770 # An MPLS route for the non-EOS packets
1772 route_67 = VppMplsRoute(self, 67, 0,
1773 [VppRoutePath(self.pg1.remote_ip6,
1774 self.pg1.sw_if_index,
1776 proto=DpoProto.DPO_PROTO_IP6),
1777 VppRoutePath(self.pg2.remote_ip6,
1778 self.pg2.sw_if_index,
1780 proto=DpoProto.DPO_PROTO_IP6)])
1781 route_67.add_vpp_config()
1784 # inject the packet on pg0 - expect load-balancing across the 2 paths
1785 # - since the default hash config is to use IP src,dst and port
1787 # We are not going to ensure equal amounts of packets across each link,
1788 # since the hash algorithm is statistical and therefore this can never
1789 # be guaranteed. But wuth 64 different packets we do expect some
1790 # balancing. So instead just ensure there is traffic on each link.
1792 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1793 [self.pg1, self.pg2])
1794 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1795 [self.pg1, self.pg2])
1796 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1797 [self.pg1, self.pg2])
1798 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1799 [self.pg1, self.pg2])
1800 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1801 [self.pg1, self.pg2])
1804 # The packets with Entropy label in should not load-balance,
1805 # since the Entorpy value is fixed.
1807 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1810 # change the flow hash config so it's only IP src,dst
1811 # - now only the stream with differing source address will
1814 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1816 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1817 [self.pg1, self.pg2])
1818 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1819 [self.pg1, self.pg2])
1820 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1823 # change the flow hash config back to defaults
1825 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1828 # Recursive prefixes
1829 # - testing that 2 stages of load-balancing occurs and there is no
1830 # polarisation (i.e. only 2 of 4 paths are used)
1835 for ii in range(257):
1836 port_pkts.append((Ether(src=self.pg0.remote_mac,
1837 dst=self.pg0.local_mac) /
1840 inet6.UDP(sport=1234,
1843 src_pkts.append((Ether(src=self.pg0.remote_mac,
1844 dst=self.pg0.local_mac) /
1846 src="4000:1::%d" % ii) /
1847 inet6.UDP(sport=1234, dport=1234) /
1850 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1851 [VppRoutePath(self.pg3.remote_ip6,
1852 self.pg3.sw_if_index,
1853 proto=DpoProto.DPO_PROTO_IP6),
1854 VppRoutePath(self.pg4.remote_ip6,
1855 self.pg4.sw_if_index,
1856 proto=DpoProto.DPO_PROTO_IP6)],
1858 route_3000_2.add_vpp_config()
1860 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1861 [VppRoutePath("3000::1",
1863 proto=DpoProto.DPO_PROTO_IP6),
1864 VppRoutePath("3000::2",
1866 proto=DpoProto.DPO_PROTO_IP6)],
1868 route_4000_1.add_vpp_config()
1871 # inject the packet on pg0 - expect load-balancing across all 4 paths
1873 self.vapi.cli("clear trace")
1874 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1875 [self.pg1, self.pg2,
1876 self.pg3, self.pg4])
1877 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1878 [self.pg1, self.pg2,
1879 self.pg3, self.pg4])
1882 # Recursive prefixes
1883 # - testing that 2 stages of load-balancing no choices
1887 for ii in range(257):
1888 port_pkts.append((Ether(src=self.pg0.remote_mac,
1889 dst=self.pg0.local_mac) /
1892 inet6.UDP(sport=1234,
1896 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1897 [VppRoutePath(self.pg3.remote_ip6,
1898 self.pg3.sw_if_index,
1899 proto=DpoProto.DPO_PROTO_IP6)],
1901 route_5000_2.add_vpp_config()
1903 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1904 [VppRoutePath("5000::2",
1906 proto=DpoProto.DPO_PROTO_IP6)],
1908 route_6000_1.add_vpp_config()
1911 # inject the packet on pg0 - expect load-balancing across all 4 paths
1913 self.vapi.cli("clear trace")
1914 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1917 class TestIP6Punt(VppTestCase):
1918 """ IPv6 Punt Police/Redirect """
1921 super(TestIP6Punt, self).setUp()
1923 self.create_pg_interfaces(range(4))
1925 for i in self.pg_interfaces:
1931 super(TestIP6Punt, self).tearDown()
1932 for i in self.pg_interfaces:
1936 def test_ip_punt(self):
1937 """ IP6 punt police and redirect """
1939 p = (Ether(src=self.pg0.remote_mac,
1940 dst=self.pg0.local_mac) /
1941 IPv6(src=self.pg0.remote_ip6,
1942 dst=self.pg0.local_ip6) /
1943 inet6.TCP(sport=1234, dport=1234) /
1949 # Configure a punt redirect via pg1.
1951 nh_addr = self.pg1.remote_ip6
1952 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1953 self.pg1.sw_if_index,
1956 self.send_and_expect(self.pg0, pkts, self.pg1)
1961 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1963 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1965 self.vapi.cli("clear trace")
1966 self.pg0.add_stream(pkts)
1967 self.pg_enable_capture(self.pg_interfaces)
1971 # the number of packet recieved should be greater than 0,
1972 # but not equal to the number sent, since some were policed
1974 rx = self.pg1._get_capture(1)
1975 self.assertGreater(len(rx), 0)
1976 self.assertLess(len(rx), len(pkts))
1979 # remove the policer. back to full rx
1981 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1982 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1983 rate_type=1, is_add=0)
1984 self.send_and_expect(self.pg0, pkts, self.pg1)
1987 # remove the redirect. expect full drop.
1989 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1990 self.pg1.sw_if_index,
1993 self.send_and_assert_no_replies(self.pg0, pkts,
1994 "IP no punt config")
1997 # Add a redirect that is not input port selective
1999 self.vapi.ip_punt_redirect(0xffffffff,
2000 self.pg1.sw_if_index,
2002 self.send_and_expect(self.pg0, pkts, self.pg1)
2004 self.vapi.ip_punt_redirect(0xffffffff,
2005 self.pg1.sw_if_index,
2009 def test_ip_punt_dump(self):
2010 """ IP6 punt redirect dump"""
2013 # Configure a punt redirects
2015 nh_addr = self.pg3.remote_ip6
2016 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2017 self.pg3.sw_if_index,
2019 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2020 self.pg3.sw_if_index,
2022 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2023 self.pg3.sw_if_index,
2027 # Dump pg0 punt redirects
2029 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2032 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2035 # Dump punt redirects for all interfaces
2037 punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2038 self.assertEqual(len(punts), 3)
2040 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
2041 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2042 self.assertEqual(str(punts[2].punt.nh), '::')
2045 class TestIPDeag(VppTestCase):
2046 """ IPv6 Deaggregate Routes """
2049 super(TestIPDeag, self).setUp()
2051 self.create_pg_interfaces(range(3))
2053 for i in self.pg_interfaces:
2059 super(TestIPDeag, self).tearDown()
2060 for i in self.pg_interfaces:
2064 def test_ip_deag(self):
2065 """ IP Deag Routes """
2068 # Create a table to be used for:
2069 # 1 - another destination address lookup
2070 # 2 - a source address lookup
2072 table_dst = VppIpTable(self, 1, is_ip6=1)
2073 table_src = VppIpTable(self, 2, is_ip6=1)
2074 table_dst.add_vpp_config()
2075 table_src.add_vpp_config()
2078 # Add a route in the default table to point to a deag/
2079 # second lookup in each of these tables
2081 route_to_dst = VppIpRoute(self, "1::1", 128,
2085 proto=DpoProto.DPO_PROTO_IP6)],
2087 route_to_src = VppIpRoute(self, "1::2", 128,
2092 proto=DpoProto.DPO_PROTO_IP6)],
2094 route_to_dst.add_vpp_config()
2095 route_to_src.add_vpp_config()
2098 # packets to these destination are dropped, since they'll
2099 # hit the respective default routes in the second table
2101 p_dst = (Ether(src=self.pg0.remote_mac,
2102 dst=self.pg0.local_mac) /
2103 IPv6(src="5::5", dst="1::1") /
2104 inet6.TCP(sport=1234, dport=1234) /
2106 p_src = (Ether(src=self.pg0.remote_mac,
2107 dst=self.pg0.local_mac) /
2108 IPv6(src="2::2", dst="1::2") /
2109 inet6.TCP(sport=1234, dport=1234) /
2111 pkts_dst = p_dst * 257
2112 pkts_src = p_src * 257
2114 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2116 self.send_and_assert_no_replies(self.pg0, pkts_src,
2120 # add a route in the dst table to forward via pg1
2122 route_in_dst = VppIpRoute(self, "1::1", 128,
2123 [VppRoutePath(self.pg1.remote_ip6,
2124 self.pg1.sw_if_index,
2125 proto=DpoProto.DPO_PROTO_IP6)],
2128 route_in_dst.add_vpp_config()
2130 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2133 # add a route in the src table to forward via pg2
2135 route_in_src = VppIpRoute(self, "2::2", 128,
2136 [VppRoutePath(self.pg2.remote_ip6,
2137 self.pg2.sw_if_index,
2138 proto=DpoProto.DPO_PROTO_IP6)],
2141 route_in_src.add_vpp_config()
2142 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2145 # loop in the lookup DP
2147 route_loop = VppIpRoute(self, "3::3", 128,
2150 proto=DpoProto.DPO_PROTO_IP6)],
2152 route_loop.add_vpp_config()
2154 p_l = (Ether(src=self.pg0.remote_mac,
2155 dst=self.pg0.local_mac) /
2156 IPv6(src="3::4", dst="3::3") /
2157 inet6.TCP(sport=1234, dport=1234) /
2160 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2164 class TestIP6Input(VppTestCase):
2165 """ IPv6 Input Exception Test Cases """
2168 super(TestIP6Input, self).setUp()
2170 self.create_pg_interfaces(range(2))
2172 for i in self.pg_interfaces:
2178 super(TestIP6Input, self).tearDown()
2179 for i in self.pg_interfaces:
2183 def test_ip_input_icmp_reply(self):
2184 """ IP6 Input Exception - Return ICMP (3,0) """
2186 # hop limit - ICMP replies
2188 p_version = (Ether(src=self.pg0.remote_mac,
2189 dst=self.pg0.local_mac) /
2190 IPv6(src=self.pg0.remote_ip6,
2191 dst=self.pg1.remote_ip6,
2193 inet6.UDP(sport=1234, dport=1234) /
2196 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2198 icmp = rx[ICMPv6TimeExceeded]
2200 # 0: "hop limit exceeded in transit",
2201 self.assertEqual((icmp.type, icmp.code), (3, 0))
2203 icmpv6_data = '\x0a' * 18
2205 all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2207 @parameterized.expand([
2208 # Name, src, dst, l4proto, msg, timeout
2209 ("src='iface', dst='iface'", None, None,
2210 inet6.UDP(sport=1234, dport=1234), "funky version", None),
2211 ("src='All 0's', dst='iface'", all_0s, None,
2212 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2213 ("src='iface', dst='All 0's'", None, all_0s,
2214 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2215 ("src='All 1's', dst='iface'", all_1s, None,
2216 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2217 ("src='iface', dst='All 1's'", None, all_1s,
2218 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2219 ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2220 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2223 def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2225 self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2227 p_version = (Ether(src=self.pg0.remote_mac,
2228 dst=self.pg0.local_mac) /
2229 IPv6(src=src or self.pg0.remote_ip6,
2230 dst=dst or self.pg1.remote_ip6,
2235 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2240 if __name__ == '__main__':
2241 unittest.main(testRunner=VppTestRunner)