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
30 AF_INET6 = socket.AF_INET6
34 euid = in6_mactoifaceid(mac)
35 addr = "fe80::" + euid
39 class TestIPv6ND(VppTestCase):
40 def validate_ra(self, intf, rx, dst_ip=None):
42 dst_ip = intf.remote_ip6
44 # unicasted packets must come to the unicast mac
45 self.assertEqual(rx[Ether].dst, intf.remote_mac)
47 # and from the router's MAC
48 self.assertEqual(rx[Ether].src, intf.local_mac)
50 # the rx'd RA should be addressed to the sender's source
51 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
52 self.assertEqual(in6_ptop(rx[IPv6].dst),
55 # and come from the router's link local
56 self.assertTrue(in6_islladdr(rx[IPv6].src))
57 self.assertEqual(in6_ptop(rx[IPv6].src),
58 in6_ptop(mk_ll_addr(intf.local_mac)))
60 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
62 dst_ip = intf.remote_ip6
64 dst_ip = intf.local_ip6
66 # unicasted packets must come to the unicast mac
67 self.assertEqual(rx[Ether].dst, intf.remote_mac)
69 # and from the router's MAC
70 self.assertEqual(rx[Ether].src, intf.local_mac)
72 # the rx'd NA should be addressed to the sender's source
73 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
74 self.assertEqual(in6_ptop(rx[IPv6].dst),
77 # and come from the target address
79 in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
81 # Dest link-layer options should have the router's MAC
82 dll = rx[ICMPv6NDOptDstLLAddr]
83 self.assertEqual(dll.lladdr, intf.local_mac)
85 def validate_ns(self, intf, rx, tgt_ip):
86 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
87 dst_ip = inet_ntop(AF_INET6, nsma)
90 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
92 # and from the router's MAC
93 self.assertEqual(rx[Ether].src, intf.local_mac)
95 # the rx'd NS should be addressed to an mcast address
96 # derived from the target address
98 in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
100 # expect the tgt IP in the NS header
102 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
104 # packet is from the router's local address
106 in6_ptop(rx[IPv6].src), intf.local_ip6)
108 # Src link-layer options should have the router's MAC
109 sll = rx[ICMPv6NDOptSrcLLAddr]
110 self.assertEqual(sll.lladdr, intf.local_mac)
112 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
113 filter_out_fn=is_ipv6_misc):
114 intf.add_stream(pkts)
115 self.pg_enable_capture(self.pg_interfaces)
117 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
119 self.assertEqual(len(rx), 1)
121 self.validate_ra(intf, rx, dst_ip)
123 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
125 filter_out_fn=is_ipv6_misc):
126 intf.add_stream(pkts)
127 self.pg_enable_capture(self.pg_interfaces)
129 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
131 self.assertEqual(len(rx), 1)
133 self.validate_na(intf, rx, dst_ip, tgt_ip)
135 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
136 filter_out_fn=is_ipv6_misc):
137 tx_intf.add_stream(pkts)
138 self.pg_enable_capture(self.pg_interfaces)
140 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
142 self.assertEqual(len(rx), 1)
144 self.validate_ns(rx_intf, rx, tgt_ip)
146 def verify_ip(self, rx, smac, dmac, sip, dip):
148 self.assertEqual(ether.dst, dmac)
149 self.assertEqual(ether.src, smac)
152 self.assertEqual(ip.src, sip)
153 self.assertEqual(ip.dst, dip)
156 class TestIPv6(TestIPv6ND):
157 """ IPv6 Test Case """
161 super(TestIPv6, cls).setUpClass()
165 Perform test setup before test case.
168 - create 3 pg interfaces
169 - untagged pg0 interface
170 - Dot1Q subinterface on pg1
171 - Dot1AD subinterface on pg2
173 - put it into UP state
175 - resolve neighbor address using NDP
176 - configure 200 fib entries
178 :ivar list interfaces: pg interfaces and subinterfaces.
179 :ivar dict flows: IPv4 packet flows in test.
181 *TODO:* Create AD sub interface
183 super(TestIPv6, self).setUp()
185 # create 3 pg interfaces
186 self.create_pg_interfaces(range(3))
188 # create 2 subinterfaces for p1 and pg2
189 self.sub_interfaces = [
190 VppDot1QSubint(self, self.pg1, 100),
191 VppDot1QSubint(self, self.pg2, 200)
192 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
195 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
197 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
198 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
199 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
202 self.pg_if_packet_sizes = [64, 1500, 9020]
204 self.interfaces = list(self.pg_interfaces)
205 self.interfaces.extend(self.sub_interfaces)
207 # setup all interfaces
208 for i in self.interfaces:
213 # config 2M FIB entries
214 self.config_fib_entries(200)
217 """Run standard test teardown and log ``show ip6 neighbors``."""
218 for i in self.interfaces:
222 for i in self.sub_interfaces:
223 i.remove_vpp_config()
225 super(TestIPv6, self).tearDown()
226 if not self.vpp_dead:
227 self.logger.info(self.vapi.cli("show ip6 neighbors"))
228 # info(self.vapi.cli("show ip6 fib")) # many entries
230 def config_fib_entries(self, count):
231 """For each interface add to the FIB table *count* routes to
232 "fd02::1/128" destination with interface's local address as next-hop
235 :param int count: Number of FIB entries.
237 - *TODO:* check if the next-hop address shouldn't be remote address
238 instead of local address.
240 n_int = len(self.interfaces)
243 dest_addr = inet_pton(AF_INET6, "fd02::1")
245 for i in self.interfaces:
246 next_hop_address = i.local_ip6n
247 for j in range(count / n_int):
248 self.vapi.ip_add_del_route(
249 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
251 if counter / count * 100 > percent:
252 self.logger.info("Configure %d FIB entries .. %d%% done" %
256 def modify_packet(self, src_if, packet_size, pkt):
257 """Add load, set destination IP and extend packet to required packet
258 size for defined interface.
260 :param VppInterface src_if: Interface to create packet for.
261 :param int packet_size: Required packet size.
262 :param Scapy pkt: Packet to be modified.
264 dst_if_idx = packet_size / 10 % 2
265 dst_if = self.flows[src_if][dst_if_idx]
266 info = self.create_packet_info(src_if, dst_if)
267 payload = self.info_to_payload(info)
268 p = pkt / Raw(payload)
269 p[IPv6].dst = dst_if.remote_ip6
271 if isinstance(src_if, VppSubInterface):
272 p = src_if.add_dot1_layer(p)
273 self.extend_packet(p, packet_size)
277 def create_stream(self, src_if):
278 """Create input packet stream for defined interface.
280 :param VppInterface src_if: Interface to create packet stream for.
282 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
283 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
284 IPv6(src=src_if.remote_ip6) /
285 inet6.UDP(sport=1234, dport=1234))
287 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
288 for i in moves.range(self.pg_if_packet_sizes[0],
289 self.pg_if_packet_sizes[1], 10)]
290 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
291 for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
292 self.pg_if_packet_sizes[2] + hdr_ext,
298 def verify_capture(self, dst_if, capture):
299 """Verify captured input packet stream for defined interface.
301 :param VppInterface dst_if: Interface to verify captured packet stream
303 :param list capture: Captured packet stream.
305 self.logger.info("Verifying capture on interface %s" % dst_if.name)
307 for i in self.interfaces:
308 last_info[i.sw_if_index] = None
310 dst_sw_if_index = dst_if.sw_if_index
311 if hasattr(dst_if, 'parent'):
313 for packet in capture:
315 # Check VLAN tags and Ethernet header
316 packet = dst_if.remove_dot1_layer(packet)
317 self.assertTrue(Dot1Q not in packet)
320 udp = packet[inet6.UDP]
321 payload_info = self.payload_to_info(str(packet[Raw]))
322 packet_index = payload_info.index
323 self.assertEqual(payload_info.dst, dst_sw_if_index)
325 "Got packet on port %s: src=%u (id=%u)" %
326 (dst_if.name, payload_info.src, packet_index))
327 next_info = self.get_next_packet_info_for_interface2(
328 payload_info.src, dst_sw_if_index,
329 last_info[payload_info.src])
330 last_info[payload_info.src] = next_info
331 self.assertTrue(next_info is not None)
332 self.assertEqual(packet_index, next_info.index)
333 saved_packet = next_info.data
334 # Check standard fields
336 ip.src, saved_packet[IPv6].src)
338 ip.dst, saved_packet[IPv6].dst)
340 udp.sport, saved_packet[inet6.UDP].sport)
342 udp.dport, saved_packet[inet6.UDP].dport)
344 self.logger.error(ppp("Unexpected or invalid packet:", packet))
346 for i in self.interfaces:
347 remaining_packet = self.get_next_packet_info_for_interface2(
348 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
349 self.assertTrue(remaining_packet is None,
350 "Interface %s: Packet expected from interface %s "
351 "didn't arrive" % (dst_if.name, i.name))
357 - Create IPv6 stream for pg0 interface
358 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
359 - Send and verify received packets on each interface.
362 pkts = self.create_stream(self.pg0)
363 self.pg0.add_stream(pkts)
365 for i in self.sub_interfaces:
366 pkts = self.create_stream(i)
367 i.parent.add_stream(pkts)
369 self.pg_enable_capture(self.pg_interfaces)
372 pkts = self.pg0.get_capture()
373 self.verify_capture(self.pg0, pkts)
375 for i in self.sub_interfaces:
376 pkts = i.parent.get_capture()
377 self.verify_capture(i, pkts)
380 """ IPv6 Neighbour Solicitation Exceptions
383 - Send an NS Sourced from an address not covered by the link sub-net
384 - Send an NS to an mcast address the router has not joined
385 - Send NS for a target address the router does not onn.
389 # An NS from a non link source address
391 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
392 d = inet_ntop(AF_INET6, nsma)
394 p = (Ether(dst=in6_getnsmac(nsma)) /
395 IPv6(dst=d, src="2002::2") /
396 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
397 ICMPv6NDOptSrcLLAddr(
398 lladdr=self.pg0.remote_mac))
401 self.send_and_assert_no_replies(
403 "No response to NS source by address not on sub-net")
406 # An NS for sent to a solicited mcast group the router is
407 # not a member of FAILS
410 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
411 d = inet_ntop(AF_INET6, nsma)
413 p = (Ether(dst=in6_getnsmac(nsma)) /
414 IPv6(dst=d, src=self.pg0.remote_ip6) /
415 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
416 ICMPv6NDOptSrcLLAddr(
417 lladdr=self.pg0.remote_mac))
420 self.send_and_assert_no_replies(
422 "No response to NS sent to unjoined mcast address")
425 # An NS whose target address is one the router does not own
427 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
428 d = inet_ntop(AF_INET6, nsma)
430 p = (Ether(dst=in6_getnsmac(nsma)) /
431 IPv6(dst=d, src=self.pg0.remote_ip6) /
432 ICMPv6ND_NS(tgt="fd::ffff") /
433 ICMPv6NDOptSrcLLAddr(
434 lladdr=self.pg0.remote_mac))
437 self.send_and_assert_no_replies(self.pg0, pkts,
438 "No response to NS for unknown target")
441 # A neighbor entry that has no associated FIB-entry
443 self.pg0.generate_remote_hosts(4)
444 nd_entry = VppNeighbor(self,
445 self.pg0.sw_if_index,
446 self.pg0.remote_hosts[2].mac,
447 self.pg0.remote_hosts[2].ip6,
450 nd_entry.add_vpp_config()
453 # check we have the neighbor, but no route
455 self.assertTrue(find_nbr(self,
456 self.pg0.sw_if_index,
457 self.pg0._remote_hosts[2].ip6,
459 self.assertFalse(find_route(self,
460 self.pg0._remote_hosts[2].ip6,
465 # send an NS from a link local address to the interface's global
468 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
470 dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
471 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
472 ICMPv6NDOptSrcLLAddr(
473 lladdr=self.pg0.remote_mac))
475 self.send_and_expect_na(self.pg0, p,
476 "NS from link-local",
477 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
478 tgt_ip=self.pg0.local_ip6)
481 # we should have learned an ND entry for the peer's link-local
482 # but not inserted a route to it in the FIB
484 self.assertTrue(find_nbr(self,
485 self.pg0.sw_if_index,
486 self.pg0._remote_hosts[2].ip6_ll,
488 self.assertFalse(find_route(self,
489 self.pg0._remote_hosts[2].ip6_ll,
494 # An NS to the router's own Link-local
496 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
498 dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
499 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
500 ICMPv6NDOptSrcLLAddr(
501 lladdr=self.pg0.remote_mac))
503 self.send_and_expect_na(self.pg0, p,
504 "NS to/from link-local",
505 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
506 tgt_ip=self.pg0.local_ip6_ll)
509 # we should have learned an ND entry for the peer's link-local
510 # but not inserted a route to it in the FIB
512 self.assertTrue(find_nbr(self,
513 self.pg0.sw_if_index,
514 self.pg0._remote_hosts[3].ip6_ll,
516 self.assertFalse(find_route(self,
517 self.pg0._remote_hosts[3].ip6_ll,
521 def test_ns_duplicates(self):
525 # Generate some hosts on the LAN
527 self.pg1.generate_remote_hosts(3)
530 # Add host 1 on pg1 and pg2
532 ns_pg1 = VppNeighbor(self,
533 self.pg1.sw_if_index,
534 self.pg1.remote_hosts[1].mac,
535 self.pg1.remote_hosts[1].ip6,
537 ns_pg1.add_vpp_config()
538 ns_pg2 = VppNeighbor(self,
539 self.pg2.sw_if_index,
541 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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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_ip6n,
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 = socket.inet_pton(socket.AF_INET6,
1068 prefix_option.getfieldval("prefix"))
1069 self.assert_equal(reported_prefix.dst_address, prefix)
1070 self.assert_equal(reported_prefix.dst_address_length,
1071 prefix_option.getfieldval("prefixlen"))
1072 L = prefix_option.getfieldval("L")
1073 A = prefix_option.getfieldval("A")
1074 option_flags = (L << 7) | (A << 6)
1075 self.assert_equal(reported_prefix.flags, option_flags)
1076 self.assert_equal(reported_prefix.valid_time,
1077 prefix_option.getfieldval("validlifetime"))
1078 self.assert_equal(reported_prefix.preferred_time,
1079 prefix_option.getfieldval("preferredlifetime"))
1081 def test_rd_receive_router_advertisement(self):
1082 """ Verify events triggered by received RA packets """
1084 self.vapi.want_ip6_ra_events()
1086 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1090 preferredlifetime=500,
1095 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1099 preferredlifetime=1000,
1104 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1105 IPv6(dst=self.pg1.local_ip6_ll,
1106 src=mk_ll_addr(self.pg1.remote_mac)) /
1110 self.pg1.add_stream([p])
1113 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1115 self.assert_equal(ev.current_hop_limit, 0)
1116 self.assert_equal(ev.flags, 8)
1117 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1118 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1120 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1122 self.assert_equal(ev.n_prefixes, 2)
1124 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1125 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1128 class TestIPv6RDControlPlane(TestIPv6ND):
1129 """ IPv6 Router Discovery Control Plane Test Case """
1132 def setUpClass(cls):
1133 super(TestIPv6RDControlPlane, cls).setUpClass()
1136 super(TestIPv6RDControlPlane, self).setUp()
1138 # create 1 pg interface
1139 self.create_pg_interfaces(range(1))
1141 self.interfaces = list(self.pg_interfaces)
1143 # setup all interfaces
1144 for i in self.interfaces:
1149 super(TestIPv6RDControlPlane, self).tearDown()
1152 def create_ra_packet(pg, routerlifetime=None):
1153 src_ip = pg.remote_ip6_ll
1154 dst_ip = pg.local_ip6
1155 if routerlifetime is not None:
1156 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1159 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1160 IPv6(dst=dst_ip, src=src_ip) / ra)
1164 def get_default_routes(fib):
1167 if entry.address_length == 0:
1168 for path in entry.path:
1169 if path.sw_if_index != 0xFFFFFFFF:
1171 defaut_route['sw_if_index'] = path.sw_if_index
1172 defaut_route['next_hop'] = path.next_hop
1173 list.append(defaut_route)
1177 def get_interface_addresses(fib, pg):
1180 if entry.address_length == 128:
1181 path = entry.path[0]
1182 if path.sw_if_index == pg.sw_if_index:
1183 list.append(entry.address)
1187 """ Test handling of SLAAC addresses and default routes """
1189 fib = self.vapi.ip6_fib_dump()
1190 default_routes = self.get_default_routes(fib)
1191 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1192 self.assertEqual(default_routes, [])
1193 router_address = self.pg0.remote_ip6n_ll
1195 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1200 packet = (self.create_ra_packet(
1201 self.pg0) / ICMPv6NDOptPrefixInfo(
1205 preferredlifetime=2,
1208 ) / ICMPv6NDOptPrefixInfo(
1212 preferredlifetime=1000,
1216 self.pg0.add_stream([packet])
1221 fib = self.vapi.ip6_fib_dump()
1223 # check FIB for new address
1224 addresses = set(self.get_interface_addresses(fib, self.pg0))
1225 new_addresses = addresses.difference(initial_addresses)
1226 self.assertEqual(len(new_addresses), 1)
1227 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1228 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1230 # check FIB for new default route
1231 default_routes = self.get_default_routes(fib)
1232 self.assertEqual(len(default_routes), 1)
1233 dr = default_routes[0]
1234 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1235 self.assertEqual(dr['next_hop'], router_address)
1237 # send RA to delete default route
1238 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1239 self.pg0.add_stream([packet])
1244 # check that default route is deleted
1245 fib = self.vapi.ip6_fib_dump()
1246 default_routes = self.get_default_routes(fib)
1247 self.assertEqual(len(default_routes), 0)
1252 packet = self.create_ra_packet(self.pg0)
1253 self.pg0.add_stream([packet])
1258 # check FIB for new default route
1259 fib = self.vapi.ip6_fib_dump()
1260 default_routes = self.get_default_routes(fib)
1261 self.assertEqual(len(default_routes), 1)
1262 dr = default_routes[0]
1263 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1264 self.assertEqual(dr['next_hop'], router_address)
1266 # send RA, updating router lifetime to 1s
1267 packet = self.create_ra_packet(self.pg0, 1)
1268 self.pg0.add_stream([packet])
1273 # check that default route still exists
1274 fib = self.vapi.ip6_fib_dump()
1275 default_routes = self.get_default_routes(fib)
1276 self.assertEqual(len(default_routes), 1)
1277 dr = default_routes[0]
1278 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1279 self.assertEqual(dr['next_hop'], router_address)
1283 # check that default route is deleted
1284 fib = self.vapi.ip6_fib_dump()
1285 default_routes = self.get_default_routes(fib)
1286 self.assertEqual(len(default_routes), 0)
1288 # check FIB still contains the SLAAC address
1289 addresses = set(self.get_interface_addresses(fib, self.pg0))
1290 new_addresses = addresses.difference(initial_addresses)
1291 self.assertEqual(len(new_addresses), 1)
1292 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1293 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1297 # check that SLAAC address is deleted
1298 fib = self.vapi.ip6_fib_dump()
1299 addresses = set(self.get_interface_addresses(fib, self.pg0))
1300 new_addresses = addresses.difference(initial_addresses)
1301 self.assertEqual(len(new_addresses), 0)
1304 class IPv6NDProxyTest(TestIPv6ND):
1305 """ IPv6 ND ProxyTest Case """
1308 super(IPv6NDProxyTest, self).setUp()
1310 # create 3 pg interfaces
1311 self.create_pg_interfaces(range(3))
1313 # pg0 is the master interface, with the configured subnet
1315 self.pg0.config_ip6()
1316 self.pg0.resolve_ndp()
1318 self.pg1.ip6_enable()
1319 self.pg2.ip6_enable()
1322 super(IPv6NDProxyTest, self).tearDown()
1324 def test_nd_proxy(self):
1325 """ IPv6 Proxy ND """
1328 # Generate some hosts in the subnet that we are proxying
1330 self.pg0.generate_remote_hosts(8)
1332 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1333 d = inet_ntop(AF_INET6, nsma)
1336 # Send an NS for one of those remote hosts on one of the proxy links
1337 # expect no response since it's from an address that is not
1338 # on the link that has the prefix configured
1340 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
1342 src=self.pg0._remote_hosts[2].ip6) /
1343 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1344 ICMPv6NDOptSrcLLAddr(
1345 lladdr=self.pg0._remote_hosts[2].mac))
1347 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1350 # Add proxy support for the host
1352 self.vapi.ip6_nd_proxy(
1353 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1354 self.pg1.sw_if_index)
1357 # try that NS again. this time we expect an NA back
1359 self.send_and_expect_na(self.pg1, ns_pg1,
1360 "NS to proxy entry",
1361 dst_ip=self.pg0._remote_hosts[2].ip6,
1362 tgt_ip=self.pg0.local_ip6)
1365 # ... and that we have an entry in the ND cache
1367 self.assertTrue(find_nbr(self,
1368 self.pg1.sw_if_index,
1369 self.pg0._remote_hosts[2].ip6,
1373 # ... and we can route traffic to it
1375 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1376 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1377 src=self.pg0.remote_ip6) /
1378 inet6.UDP(sport=10000, dport=20000) /
1381 self.pg0.add_stream(t)
1382 self.pg_enable_capture(self.pg_interfaces)
1384 rx = self.pg1.get_capture(1)
1387 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1388 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1390 self.assertEqual(rx[IPv6].src,
1392 self.assertEqual(rx[IPv6].dst,
1396 # Test we proxy for the host on the main interface
1398 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1399 IPv6(dst=d, src=self.pg0.remote_ip6) /
1401 tgt=self.pg0._remote_hosts[2].ip6) /
1402 ICMPv6NDOptSrcLLAddr(
1403 lladdr=self.pg0.remote_mac))
1405 self.send_and_expect_na(self.pg0, ns_pg0,
1406 "NS to proxy entry on main",
1407 tgt_ip=self.pg0._remote_hosts[2].ip6,
1408 dst_ip=self.pg0.remote_ip6)
1411 # Setup and resolve proxy for another host on another interface
1413 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1415 src=self.pg0._remote_hosts[3].ip6) /
1416 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1417 ICMPv6NDOptSrcLLAddr(
1418 lladdr=self.pg0._remote_hosts[2].mac))
1420 self.vapi.ip6_nd_proxy(
1421 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1422 self.pg2.sw_if_index)
1424 self.send_and_expect_na(self.pg2, ns_pg2,
1425 "NS to proxy entry other interface",
1426 dst_ip=self.pg0._remote_hosts[3].ip6,
1427 tgt_ip=self.pg0.local_ip6)
1429 self.assertTrue(find_nbr(self,
1430 self.pg2.sw_if_index,
1431 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,
1474 self.assertFalse(find_nbr(self,
1475 self.pg1.sw_if_index,
1476 self.pg0._remote_hosts[2].ip6,
1480 # no longer proxy-ing...
1482 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1483 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1484 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1487 # no longer forwarding. traffic generates NS out of the glean/main
1490 self.pg2.add_stream(t2)
1491 self.pg_enable_capture(self.pg_interfaces)
1494 rx = self.pg0.get_capture(1)
1496 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1499 class TestIPNull(VppTestCase):
1500 """ IPv6 routes via NULL """
1503 super(TestIPNull, self).setUp()
1505 # create 2 pg interfaces
1506 self.create_pg_interfaces(range(1))
1508 for i in self.pg_interfaces:
1514 super(TestIPNull, self).tearDown()
1515 for i in self.pg_interfaces:
1519 def test_ip_null(self):
1520 """ IP NULL route """
1522 p = (Ether(src=self.pg0.remote_mac,
1523 dst=self.pg0.local_mac) /
1524 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1525 inet6.UDP(sport=1234, dport=1234) /
1529 # A route via IP NULL that will reply with ICMP unreachables
1531 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1532 ip_unreach.add_vpp_config()
1534 self.pg0.add_stream(p)
1535 self.pg_enable_capture(self.pg_interfaces)
1538 rx = self.pg0.get_capture(1)
1540 icmp = rx[ICMPv6DestUnreach]
1542 # 0 = "No route to destination"
1543 self.assertEqual(icmp.code, 0)
1545 # ICMP is rate limited. pause a bit
1549 # A route via IP NULL that will reply with ICMP prohibited
1551 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1552 is_prohibit=1, is_ip6=1)
1553 ip_prohibit.add_vpp_config()
1555 self.pg0.add_stream(p)
1556 self.pg_enable_capture(self.pg_interfaces)
1559 rx = self.pg0.get_capture(1)
1561 icmp = rx[ICMPv6DestUnreach]
1563 # 1 = "Communication with destination administratively prohibited"
1564 self.assertEqual(icmp.code, 1)
1567 class TestIPDisabled(VppTestCase):
1568 """ IPv6 disabled """
1571 super(TestIPDisabled, self).setUp()
1573 # create 2 pg interfaces
1574 self.create_pg_interfaces(range(2))
1578 self.pg0.config_ip6()
1579 self.pg0.resolve_ndp()
1581 # PG 1 is not IP enabled
1585 super(TestIPDisabled, self).tearDown()
1586 for i in self.pg_interfaces:
1590 def test_ip_disabled(self):
1595 # one accepting interface, pg0, 2 forwarding interfaces
1597 route_ff_01 = VppIpMRoute(
1601 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1602 [VppMRoutePath(self.pg1.sw_if_index,
1603 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1604 VppMRoutePath(self.pg0.sw_if_index,
1605 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1607 route_ff_01.add_vpp_config()
1609 pu = (Ether(src=self.pg1.remote_mac,
1610 dst=self.pg1.local_mac) /
1611 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1612 inet6.UDP(sport=1234, dport=1234) /
1614 pm = (Ether(src=self.pg1.remote_mac,
1615 dst=self.pg1.local_mac) /
1616 IPv6(src="2001::1", dst="ffef::1") /
1617 inet6.UDP(sport=1234, dport=1234) /
1621 # PG1 does not forward IP traffic
1623 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1624 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1629 self.pg1.config_ip6()
1632 # Now we get packets through
1634 self.pg1.add_stream(pu)
1635 self.pg_enable_capture(self.pg_interfaces)
1637 rx = self.pg0.get_capture(1)
1639 self.pg1.add_stream(pm)
1640 self.pg_enable_capture(self.pg_interfaces)
1642 rx = self.pg0.get_capture(1)
1647 self.pg1.unconfig_ip6()
1650 # PG1 does not forward IP traffic
1652 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1653 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1656 class TestIP6LoadBalance(VppTestCase):
1657 """ IPv6 Load-Balancing """
1660 super(TestIP6LoadBalance, self).setUp()
1662 self.create_pg_interfaces(range(5))
1664 mpls_tbl = VppMplsTable(self, 0)
1665 mpls_tbl.add_vpp_config()
1667 for i in self.pg_interfaces:
1674 for i in self.pg_interfaces:
1678 super(TestIP6LoadBalance, self).tearDown()
1680 def send_and_expect_load_balancing(self, input, pkts, outputs):
1681 self.vapi.cli("clear trace")
1682 input.add_stream(pkts)
1683 self.pg_enable_capture(self.pg_interfaces)
1686 rx = oo._get_capture(1)
1687 self.assertNotEqual(0, len(rx))
1689 def send_and_expect_one_itf(self, input, pkts, itf):
1690 self.vapi.cli("clear trace")
1691 input.add_stream(pkts)
1692 self.pg_enable_capture(self.pg_interfaces)
1694 rx = itf.get_capture(len(pkts))
1696 def test_ip6_load_balance(self):
1697 """ IPv6 Load-Balancing """
1700 # An array of packets that differ only in the destination port
1704 # - MPLS non-EOS with an entropy label
1708 port_mpls_neos_pkts = []
1712 # An array of packets that differ only in the source address
1717 for ii in range(65):
1719 IPv6(dst="3000::1", src="3000:1::1") /
1720 inet6.UDP(sport=1234, dport=1234 + ii) /
1722 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1723 dst=self.pg0.local_mac) /
1725 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1726 dst=self.pg0.local_mac) /
1727 MPLS(label=66, ttl=2) /
1729 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1730 dst=self.pg0.local_mac) /
1731 MPLS(label=67, ttl=2) /
1732 MPLS(label=77, ttl=2) /
1734 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1735 dst=self.pg0.local_mac) /
1736 MPLS(label=67, ttl=2) /
1737 MPLS(label=14, ttl=2) /
1738 MPLS(label=999, ttl=2) /
1741 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1742 inet6.UDP(sport=1234, dport=1234) /
1744 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1745 dst=self.pg0.local_mac) /
1747 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1748 dst=self.pg0.local_mac) /
1749 MPLS(label=66, ttl=2) /
1753 # A route for the IP pacekts
1755 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1756 [VppRoutePath(self.pg1.remote_ip6,
1757 self.pg1.sw_if_index,
1758 proto=DpoProto.DPO_PROTO_IP6),
1759 VppRoutePath(self.pg2.remote_ip6,
1760 self.pg2.sw_if_index,
1761 proto=DpoProto.DPO_PROTO_IP6)],
1763 route_3000_1.add_vpp_config()
1766 # a local-label for the EOS packets
1768 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1769 binding.add_vpp_config()
1772 # An MPLS route for the non-EOS packets
1774 route_67 = VppMplsRoute(self, 67, 0,
1775 [VppRoutePath(self.pg1.remote_ip6,
1776 self.pg1.sw_if_index,
1778 proto=DpoProto.DPO_PROTO_IP6),
1779 VppRoutePath(self.pg2.remote_ip6,
1780 self.pg2.sw_if_index,
1782 proto=DpoProto.DPO_PROTO_IP6)])
1783 route_67.add_vpp_config()
1786 # inject the packet on pg0 - expect load-balancing across the 2 paths
1787 # - since the default hash config is to use IP src,dst and port
1789 # We are not going to ensure equal amounts of packets across each link,
1790 # since the hash algorithm is statistical and therefore this can never
1791 # be guaranteed. But wuth 64 different packets we do expect some
1792 # balancing. So instead just ensure there is traffic on each link.
1794 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1795 [self.pg1, self.pg2])
1796 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1797 [self.pg1, self.pg2])
1798 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1799 [self.pg1, self.pg2])
1800 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1801 [self.pg1, self.pg2])
1802 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1803 [self.pg1, self.pg2])
1806 # The packets with Entropy label in should not load-balance,
1807 # since the Entorpy value is fixed.
1809 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1812 # change the flow hash config so it's only IP src,dst
1813 # - now only the stream with differing source address will
1816 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1818 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1819 [self.pg1, self.pg2])
1820 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1821 [self.pg1, self.pg2])
1822 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1825 # change the flow hash config back to defaults
1827 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1830 # Recursive prefixes
1831 # - testing that 2 stages of load-balancing occurs and there is no
1832 # polarisation (i.e. only 2 of 4 paths are used)
1837 for ii in range(257):
1838 port_pkts.append((Ether(src=self.pg0.remote_mac,
1839 dst=self.pg0.local_mac) /
1842 inet6.UDP(sport=1234,
1845 src_pkts.append((Ether(src=self.pg0.remote_mac,
1846 dst=self.pg0.local_mac) /
1848 src="4000:1::%d" % ii) /
1849 inet6.UDP(sport=1234, dport=1234) /
1852 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1853 [VppRoutePath(self.pg3.remote_ip6,
1854 self.pg3.sw_if_index,
1855 proto=DpoProto.DPO_PROTO_IP6),
1856 VppRoutePath(self.pg4.remote_ip6,
1857 self.pg4.sw_if_index,
1858 proto=DpoProto.DPO_PROTO_IP6)],
1860 route_3000_2.add_vpp_config()
1862 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1863 [VppRoutePath("3000::1",
1865 proto=DpoProto.DPO_PROTO_IP6),
1866 VppRoutePath("3000::2",
1868 proto=DpoProto.DPO_PROTO_IP6)],
1870 route_4000_1.add_vpp_config()
1873 # inject the packet on pg0 - expect load-balancing across all 4 paths
1875 self.vapi.cli("clear trace")
1876 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1877 [self.pg1, self.pg2,
1878 self.pg3, self.pg4])
1879 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1880 [self.pg1, self.pg2,
1881 self.pg3, self.pg4])
1884 # Recursive prefixes
1885 # - testing that 2 stages of load-balancing no choices
1889 for ii in range(257):
1890 port_pkts.append((Ether(src=self.pg0.remote_mac,
1891 dst=self.pg0.local_mac) /
1894 inet6.UDP(sport=1234,
1898 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1899 [VppRoutePath(self.pg3.remote_ip6,
1900 self.pg3.sw_if_index,
1901 proto=DpoProto.DPO_PROTO_IP6)],
1903 route_5000_2.add_vpp_config()
1905 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1906 [VppRoutePath("5000::2",
1908 proto=DpoProto.DPO_PROTO_IP6)],
1910 route_6000_1.add_vpp_config()
1913 # inject the packet on pg0 - expect load-balancing across all 4 paths
1915 self.vapi.cli("clear trace")
1916 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1919 class TestIP6Punt(VppTestCase):
1920 """ IPv6 Punt Police/Redirect """
1923 super(TestIP6Punt, self).setUp()
1925 self.create_pg_interfaces(range(4))
1927 for i in self.pg_interfaces:
1933 super(TestIP6Punt, self).tearDown()
1934 for i in self.pg_interfaces:
1938 def test_ip_punt(self):
1939 """ IP6 punt police and redirect """
1941 p = (Ether(src=self.pg0.remote_mac,
1942 dst=self.pg0.local_mac) /
1943 IPv6(src=self.pg0.remote_ip6,
1944 dst=self.pg0.local_ip6) /
1945 inet6.TCP(sport=1234, dport=1234) /
1951 # Configure a punt redirect via pg1.
1953 nh_addr = self.pg1.remote_ip6
1954 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1955 self.pg1.sw_if_index,
1958 self.send_and_expect(self.pg0, pkts, self.pg1)
1963 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1965 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1967 self.vapi.cli("clear trace")
1968 self.pg0.add_stream(pkts)
1969 self.pg_enable_capture(self.pg_interfaces)
1973 # the number of packet recieved should be greater than 0,
1974 # but not equal to the number sent, since some were policed
1976 rx = self.pg1._get_capture(1)
1977 self.assertGreater(len(rx), 0)
1978 self.assertLess(len(rx), len(pkts))
1981 # remove the poilcer. back to full rx
1983 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1984 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1985 rate_type=1, is_add=0)
1986 self.send_and_expect(self.pg0, pkts, self.pg1)
1989 # remove the redirect. expect full drop.
1991 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1992 self.pg1.sw_if_index,
1995 self.send_and_assert_no_replies(self.pg0, pkts,
1996 "IP no punt config")
1999 # Add a redirect that is not input port selective
2001 self.vapi.ip_punt_redirect(0xffffffff,
2002 self.pg1.sw_if_index,
2004 self.send_and_expect(self.pg0, pkts, self.pg1)
2006 self.vapi.ip_punt_redirect(0xffffffff,
2007 self.pg1.sw_if_index,
2011 def test_ip_punt_dump(self):
2012 """ IP6 punt redirect dump"""
2015 # Configure a punt redirects
2017 nh_addr = self.pg3.remote_ip6
2018 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2019 self.pg3.sw_if_index,
2021 self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2022 self.pg3.sw_if_index,
2024 self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2025 self.pg3.sw_if_index,
2029 # Dump pg0 punt redirects
2031 punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2034 self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2037 # Dump punt redirects for all interfaces
2039 punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2040 self.assertEqual(len(punts), 3)
2042 self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
2043 self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2044 self.assertEqual(str(punts[2].punt.nh), '::')
2047 class TestIPDeag(VppTestCase):
2048 """ IPv6 Deaggregate Routes """
2051 super(TestIPDeag, self).setUp()
2053 self.create_pg_interfaces(range(3))
2055 for i in self.pg_interfaces:
2061 super(TestIPDeag, self).tearDown()
2062 for i in self.pg_interfaces:
2066 def test_ip_deag(self):
2067 """ IP Deag Routes """
2070 # Create a table to be used for:
2071 # 1 - another destination address lookup
2072 # 2 - a source address lookup
2074 table_dst = VppIpTable(self, 1, is_ip6=1)
2075 table_src = VppIpTable(self, 2, is_ip6=1)
2076 table_dst.add_vpp_config()
2077 table_src.add_vpp_config()
2080 # Add a route in the default table to point to a deag/
2081 # second lookup in each of these tables
2083 route_to_dst = VppIpRoute(self, "1::1", 128,
2087 proto=DpoProto.DPO_PROTO_IP6)],
2089 route_to_src = VppIpRoute(self, "1::2", 128,
2094 proto=DpoProto.DPO_PROTO_IP6)],
2096 route_to_dst.add_vpp_config()
2097 route_to_src.add_vpp_config()
2100 # packets to these destination are dropped, since they'll
2101 # hit the respective default routes in the second table
2103 p_dst = (Ether(src=self.pg0.remote_mac,
2104 dst=self.pg0.local_mac) /
2105 IPv6(src="5::5", dst="1::1") /
2106 inet6.TCP(sport=1234, dport=1234) /
2108 p_src = (Ether(src=self.pg0.remote_mac,
2109 dst=self.pg0.local_mac) /
2110 IPv6(src="2::2", dst="1::2") /
2111 inet6.TCP(sport=1234, dport=1234) /
2113 pkts_dst = p_dst * 257
2114 pkts_src = p_src * 257
2116 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2118 self.send_and_assert_no_replies(self.pg0, pkts_src,
2122 # add a route in the dst table to forward via pg1
2124 route_in_dst = VppIpRoute(self, "1::1", 128,
2125 [VppRoutePath(self.pg1.remote_ip6,
2126 self.pg1.sw_if_index,
2127 proto=DpoProto.DPO_PROTO_IP6)],
2130 route_in_dst.add_vpp_config()
2132 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2135 # add a route in the src table to forward via pg2
2137 route_in_src = VppIpRoute(self, "2::2", 128,
2138 [VppRoutePath(self.pg2.remote_ip6,
2139 self.pg2.sw_if_index,
2140 proto=DpoProto.DPO_PROTO_IP6)],
2143 route_in_src.add_vpp_config()
2144 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2147 # loop in the lookup DP
2149 route_loop = VppIpRoute(self, "3::3", 128,
2152 proto=DpoProto.DPO_PROTO_IP6)],
2154 route_loop.add_vpp_config()
2156 p_l = (Ether(src=self.pg0.remote_mac,
2157 dst=self.pg0.local_mac) /
2158 IPv6(src="3::4", dst="3::3") /
2159 inet6.TCP(sport=1234, dport=1234) /
2162 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2166 class TestIP6Input(VppTestCase):
2167 """ IPv6 Input Exception Test Cases """
2170 super(TestIP6Input, self).setUp()
2172 self.create_pg_interfaces(range(2))
2174 for i in self.pg_interfaces:
2180 super(TestIP6Input, self).tearDown()
2181 for i in self.pg_interfaces:
2185 def test_ip_input_icmp_reply(self):
2186 """ IP6 Input Exception - Return ICMP (3,0) """
2188 # hop limit - ICMP replies
2190 p_version = (Ether(src=self.pg0.remote_mac,
2191 dst=self.pg0.local_mac) /
2192 IPv6(src=self.pg0.remote_ip6,
2193 dst=self.pg1.remote_ip6,
2195 inet6.UDP(sport=1234, dport=1234) /
2198 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2200 icmp = rx[ICMPv6TimeExceeded]
2202 # 0: "hop limit exceeded in transit",
2203 self.assertEqual((icmp.type, icmp.code), (3, 0))
2205 icmpv6_data = '\x0a' * 18
2207 all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2209 @parameterized.expand([
2210 # Name, src, dst, l4proto, msg, timeout
2211 ("src='iface', dst='iface'", None, None,
2212 inet6.UDP(sport=1234, dport=1234), "funky version", None),
2213 ("src='All 0's', dst='iface'", all_0s, None,
2214 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2215 ("src='iface', dst='All 0's'", None, all_0s,
2216 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2217 ("src='All 1's', dst='iface'", all_1s, None,
2218 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2219 ("src='iface', dst='All 1's'", None, all_1s,
2220 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2221 ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2222 ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2225 def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2227 self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2229 p_version = (Ether(src=self.pg0.remote_mac,
2230 dst=self.pg0.local_mac) /
2231 IPv6(src=src or self.pg0.remote_ip6,
2232 dst=dst or self.pg1.remote_ip6,
2237 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2242 if __name__ == '__main__':
2243 unittest.main(testRunner=VppTestRunner)