4 from socket import AF_INET6
6 from framework import VppTestCase, VppTestRunner
7 from vpp_sub_interface import VppSubInterface, VppDot1QSubint
8 from vpp_pg_interface import is_ipv6_misc
9 from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
10 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
12 from vpp_neighbor import find_nbr, VppNeighbor
14 from scapy.packet import Raw
15 from scapy.layers.l2 import Ether, Dot1Q
16 from scapy.layers.inet6 import IPv6, UDP, ICMPv6ND_NS, ICMPv6ND_RS, \
17 ICMPv6ND_RA, ICMPv6NDOptSrcLLAddr, getmacbyip6, ICMPv6MRD_Solicitation, \
18 ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
19 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types
22 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
23 in6_mactoifaceid, in6_ismaddr
24 from scapy.utils import inet_pton, inet_ntop
25 from scapy.contrib.mpls import MPLS
29 euid = in6_mactoifaceid(mac)
30 addr = "fe80::" + euid
34 class TestIPv6ND(VppTestCase):
35 def validate_ra(self, intf, rx, dst_ip=None):
37 dst_ip = intf.remote_ip6
39 # unicasted packets must come to the unicast mac
40 self.assertEqual(rx[Ether].dst, intf.remote_mac)
42 # and from the router's MAC
43 self.assertEqual(rx[Ether].src, intf.local_mac)
45 # the rx'd RA should be addressed to the sender's source
46 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
47 self.assertEqual(in6_ptop(rx[IPv6].dst),
50 # and come from the router's link local
51 self.assertTrue(in6_islladdr(rx[IPv6].src))
52 self.assertEqual(in6_ptop(rx[IPv6].src),
53 in6_ptop(mk_ll_addr(intf.local_mac)))
55 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
57 dst_ip = intf.remote_ip6
59 dst_ip = intf.local_ip6
61 # unicasted packets must come to the unicast mac
62 self.assertEqual(rx[Ether].dst, intf.remote_mac)
64 # and from the router's MAC
65 self.assertEqual(rx[Ether].src, intf.local_mac)
67 # the rx'd NA should be addressed to the sender's source
68 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
69 self.assertEqual(in6_ptop(rx[IPv6].dst),
72 # and come from the target address
73 self.assertEqual(in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
75 # Dest link-layer options should have the router's MAC
76 dll = rx[ICMPv6NDOptDstLLAddr]
77 self.assertEqual(dll.lladdr, intf.local_mac)
79 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
80 filter_out_fn=is_ipv6_misc):
82 self.pg_enable_capture(self.pg_interfaces)
84 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
86 self.assertEqual(len(rx), 1)
88 self.validate_ra(intf, rx, dst_ip)
90 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
92 filter_out_fn=is_ipv6_misc):
94 self.pg_enable_capture(self.pg_interfaces)
96 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
98 self.assertEqual(len(rx), 1)
100 self.validate_na(intf, rx, dst_ip, tgt_ip)
102 def send_and_assert_no_replies(self, intf, pkts, remark):
103 intf.add_stream(pkts)
104 self.pg_enable_capture(self.pg_interfaces)
106 for i in self.pg_interfaces:
108 i.assert_nothing_captured(remark=remark)
111 class TestIPv6(TestIPv6ND):
112 """ IPv6 Test Case """
116 super(TestIPv6, cls).setUpClass()
120 Perform test setup before test case.
123 - create 3 pg interfaces
124 - untagged pg0 interface
125 - Dot1Q subinterface on pg1
126 - Dot1AD subinterface on pg2
128 - put it into UP state
130 - resolve neighbor address using NDP
131 - configure 200 fib entries
133 :ivar list interfaces: pg interfaces and subinterfaces.
134 :ivar dict flows: IPv4 packet flows in test.
135 :ivar list pg_if_packet_sizes: packet sizes in test.
137 *TODO:* Create AD sub interface
139 super(TestIPv6, self).setUp()
141 # create 3 pg interfaces
142 self.create_pg_interfaces(range(3))
144 # create 2 subinterfaces for p1 and pg2
145 self.sub_interfaces = [
146 VppDot1QSubint(self, self.pg1, 100),
147 VppDot1QSubint(self, self.pg2, 200)
148 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
151 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
153 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
154 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
155 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
158 self.pg_if_packet_sizes = [64, 512, 1518, 9018]
159 self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4]
161 self.interfaces = list(self.pg_interfaces)
162 self.interfaces.extend(self.sub_interfaces)
164 # setup all interfaces
165 for i in self.interfaces:
170 # config 2M FIB entries
171 self.config_fib_entries(200)
174 """Run standard test teardown and log ``show ip6 neighbors``."""
175 for i in self.sub_interfaces:
179 i.remove_vpp_config()
181 super(TestIPv6, self).tearDown()
182 if not self.vpp_dead:
183 self.logger.info(self.vapi.cli("show ip6 neighbors"))
184 # info(self.vapi.cli("show ip6 fib")) # many entries
186 def config_fib_entries(self, count):
187 """For each interface add to the FIB table *count* routes to
188 "fd02::1/128" destination with interface's local address as next-hop
191 :param int count: Number of FIB entries.
193 - *TODO:* check if the next-hop address shouldn't be remote address
194 instead of local address.
196 n_int = len(self.interfaces)
199 dest_addr = inet_pton(AF_INET6, "fd02::1")
201 for i in self.interfaces:
202 next_hop_address = i.local_ip6n
203 for j in range(count / n_int):
204 self.vapi.ip_add_del_route(
205 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
207 if counter / count * 100 > percent:
208 self.logger.info("Configure %d FIB entries .. %d%% done" %
212 def create_stream(self, src_if, packet_sizes):
213 """Create input packet stream for defined interface.
215 :param VppInterface src_if: Interface to create packet stream for.
216 :param list packet_sizes: Required packet sizes.
219 for i in range(0, 257):
220 dst_if = self.flows[src_if][i % 2]
221 info = self.create_packet_info(src_if, dst_if)
222 payload = self.info_to_payload(info)
223 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
224 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6) /
225 UDP(sport=1234, dport=1234) /
228 if isinstance(src_if, VppSubInterface):
229 p = src_if.add_dot1_layer(p)
230 size = packet_sizes[(i // 2) % len(packet_sizes)]
231 self.extend_packet(p, size)
235 def verify_capture(self, dst_if, capture):
236 """Verify captured input packet stream for defined interface.
238 :param VppInterface dst_if: Interface to verify captured packet stream
240 :param list capture: Captured packet stream.
242 self.logger.info("Verifying capture on interface %s" % dst_if.name)
244 for i in self.interfaces:
245 last_info[i.sw_if_index] = None
247 dst_sw_if_index = dst_if.sw_if_index
248 if hasattr(dst_if, 'parent'):
250 for packet in capture:
252 # Check VLAN tags and Ethernet header
253 packet = dst_if.remove_dot1_layer(packet)
254 self.assertTrue(Dot1Q not in packet)
258 payload_info = self.payload_to_info(str(packet[Raw]))
259 packet_index = payload_info.index
260 self.assertEqual(payload_info.dst, dst_sw_if_index)
262 "Got packet on port %s: src=%u (id=%u)" %
263 (dst_if.name, payload_info.src, packet_index))
264 next_info = self.get_next_packet_info_for_interface2(
265 payload_info.src, dst_sw_if_index,
266 last_info[payload_info.src])
267 last_info[payload_info.src] = next_info
268 self.assertTrue(next_info is not None)
269 self.assertEqual(packet_index, next_info.index)
270 saved_packet = next_info.data
271 # Check standard fields
272 self.assertEqual(ip.src, saved_packet[IPv6].src)
273 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
274 self.assertEqual(udp.sport, saved_packet[UDP].sport)
275 self.assertEqual(udp.dport, saved_packet[UDP].dport)
277 self.logger.error(ppp("Unexpected or invalid packet:", packet))
279 for i in self.interfaces:
280 remaining_packet = self.get_next_packet_info_for_interface2(
281 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
282 self.assertTrue(remaining_packet is None,
283 "Interface %s: Packet expected from interface %s "
284 "didn't arrive" % (dst_if.name, i.name))
290 - Create IPv6 stream for pg0 interface
291 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
292 - Send and verify received packets on each interface.
295 pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes)
296 self.pg0.add_stream(pkts)
298 for i in self.sub_interfaces:
299 pkts = self.create_stream(i, self.sub_if_packet_sizes)
300 i.parent.add_stream(pkts)
302 self.pg_enable_capture(self.pg_interfaces)
305 pkts = self.pg0.get_capture()
306 self.verify_capture(self.pg0, pkts)
308 for i in self.sub_interfaces:
309 pkts = i.parent.get_capture()
310 self.verify_capture(i, pkts)
313 """ IPv6 Neighbour Solicitation Exceptions
316 - Send an NS Sourced from an address not covered by the link sub-net
317 - Send an NS to an mcast address the router has not joined
318 - Send NS for a target address the router does not onn.
322 # An NS from a non link source address
324 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
325 d = inet_ntop(AF_INET6, nsma)
327 p = (Ether(dst=in6_getnsmac(nsma)) /
328 IPv6(dst=d, src="2002::2") /
329 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
330 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
333 self.send_and_assert_no_replies(
335 "No response to NS source by address not on sub-net")
338 # An NS for sent to a solicited mcast group the router is
339 # not a member of FAILS
342 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
343 d = inet_ntop(AF_INET6, nsma)
345 p = (Ether(dst=in6_getnsmac(nsma)) /
346 IPv6(dst=d, src=self.pg0.remote_ip6) /
347 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
348 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
351 self.send_and_assert_no_replies(
353 "No response to NS sent to unjoined mcast address")
356 # An NS whose target address is one the router does not own
358 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
359 d = inet_ntop(AF_INET6, nsma)
361 p = (Ether(dst=in6_getnsmac(nsma)) /
362 IPv6(dst=d, src=self.pg0.remote_ip6) /
363 ICMPv6ND_NS(tgt="fd::ffff") /
364 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
367 self.send_and_assert_no_replies(self.pg0, pkts,
368 "No response to NS for unknown target")
371 # A neighbor entry that has no associated FIB-entry
373 self.pg0.generate_remote_hosts(4)
374 nd_entry = VppNeighbor(self,
375 self.pg0.sw_if_index,
376 self.pg0.remote_hosts[2].mac,
377 self.pg0.remote_hosts[2].ip6,
380 nd_entry.add_vpp_config()
383 # check we have the neighbor, but no route
385 self.assertTrue(find_nbr(self,
386 self.pg0.sw_if_index,
387 self.pg0._remote_hosts[2].ip6,
389 self.assertFalse(find_route(self,
390 self.pg0._remote_hosts[2].ip6,
395 # send an NS from a link local address to the interface's global
398 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
399 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
400 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
401 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
403 self.send_and_expect_na(self.pg0, p,
404 "NS from link-local",
405 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
406 tgt_ip=self.pg0.local_ip6)
409 # we should have learned an ND entry for the peer's link-local
410 # but not inserted a route to it in the FIB
412 self.assertTrue(find_nbr(self,
413 self.pg0.sw_if_index,
414 self.pg0._remote_hosts[2].ip6_ll,
416 self.assertFalse(find_route(self,
417 self.pg0._remote_hosts[2].ip6_ll,
422 # An NS to the router's own Link-local
424 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
425 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
426 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
427 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
429 self.send_and_expect_na(self.pg0, p,
430 "NS to/from link-local",
431 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
432 tgt_ip=self.pg0.local_ip6_ll)
435 # we should have learned an ND entry for the peer's link-local
436 # but not inserted a route to it in the FIB
438 self.assertTrue(find_nbr(self,
439 self.pg0.sw_if_index,
440 self.pg0._remote_hosts[3].ip6_ll,
442 self.assertFalse(find_route(self,
443 self.pg0._remote_hosts[3].ip6_ll,
447 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
449 dst_ip = intf.remote_ip6
451 # unicasted packets must come to the unicast mac
452 self.assertEqual(rx[Ether].dst, intf.remote_mac)
454 # and from the router's MAC
455 self.assertEqual(rx[Ether].src, intf.local_mac)
457 # the rx'd RA should be addressed to the sender's source
458 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
459 self.assertEqual(in6_ptop(rx[IPv6].dst),
462 # and come from the router's link local
463 self.assertTrue(in6_islladdr(rx[IPv6].src))
464 self.assertEqual(in6_ptop(rx[IPv6].src),
465 in6_ptop(mk_ll_addr(intf.local_mac)))
467 # it should contain the links MTU
469 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
471 # it should contain the source's link layer address option
472 sll = ra[ICMPv6NDOptSrcLLAddr]
473 self.assertEqual(sll.lladdr, intf.local_mac)
476 # the RA should not contain prefix information
477 self.assertFalse(ra.haslayer(ICMPv6NDOptPrefixInfo))
479 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
481 # the options are nested in the scapy packet in way that i cannot
482 # decipher how to decode. this 1st layer of option always returns
483 # nested classes, so a direct obj1=obj2 comparison always fails.
484 # however, the getlayer(.., 2) does give one instnace.
485 # so we cheat here and construct a new opt instnace for comparison
486 rd = ICMPv6NDOptPrefixInfo(prefixlen=raos.prefixlen,
490 if type(pi_opt) is list:
491 for ii in range(len(pi_opt)):
492 self.assertEqual(pi_opt[ii], rd)
493 rd = rx.getlayer(ICMPv6NDOptPrefixInfo, ii+2)
495 self.assertEqual(pi_opt, raos)
497 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
498 filter_out_fn=is_ipv6_misc,
500 intf.add_stream(pkts)
501 self.pg_enable_capture(self.pg_interfaces)
503 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
505 self.assertEqual(len(rx), 1)
507 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
510 """ IPv6 Router Solicitation Exceptions
516 # Before we begin change the IPv6 RA responses to use the unicast
517 # address - that way we will not confuse them with the periodic
518 # RAs which go to the mcast address
519 # Sit and wait for the first periodic RA.
523 self.pg0.ip6_ra_config(send_unicast=1)
526 # An RS from a link source address
527 # - expect an RA in return
529 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
530 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
533 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
536 # For the next RS sent the RA should be rate limited
538 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
541 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
542 # so we need to do this before each test below so as not to drop
543 # packets for rate limiting reasons. Test this works here.
545 self.pg0.ip6_ra_config(send_unicast=1)
546 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
549 # An RS sent from a non-link local source
551 self.pg0.ip6_ra_config(send_unicast=1)
552 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
553 IPv6(dst=self.pg0.local_ip6, src="2002::ffff") /
556 self.send_and_assert_no_replies(self.pg0, pkts,
557 "RS from non-link source")
560 # Source an RS from a link local address
562 self.pg0.ip6_ra_config(send_unicast=1)
563 ll = mk_ll_addr(self.pg0.remote_mac)
564 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
565 IPv6(dst=self.pg0.local_ip6, src=ll) /
568 self.send_and_expect_ra(self.pg0, pkts,
569 "RS sourced from link-local",
573 # Send the RS multicast
575 self.pg0.ip6_ra_config(send_unicast=1)
576 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
577 ll = mk_ll_addr(self.pg0.remote_mac)
578 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
579 IPv6(dst="ff02::2", src=ll) /
582 self.send_and_expect_ra(self.pg0, pkts,
583 "RS sourced from link-local",
587 # Source from the unspecified address ::. This happens when the RS
588 # is sent before the host has a configured address/sub-net,
589 # i.e. auto-config. Since the sender has no IP address, the reply
590 # comes back mcast - so the capture needs to not filter this.
591 # If we happen to pick up the periodic RA at this point then so be it,
594 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
595 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
596 IPv6(dst="ff02::2", src="::") /
599 self.send_and_expect_ra(self.pg0, pkts,
600 "RS sourced from unspecified",
605 # Configure The RA to announce the links prefix
607 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
608 self.pg0.local_ip6_prefix_len)
611 # RAs should now contain the prefix information option
613 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
614 prefix=self.pg0.local_ip6,
618 self.pg0.ip6_ra_config(send_unicast=1)
619 ll = mk_ll_addr(self.pg0.remote_mac)
620 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
621 IPv6(dst=self.pg0.local_ip6, src=ll) /
623 self.send_and_expect_ra(self.pg0, p,
624 "RA with prefix-info",
629 # Change the prefix info to not off-link
632 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
633 self.pg0.local_ip6_prefix_len,
636 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
637 prefix=self.pg0.local_ip6,
641 self.pg0.ip6_ra_config(send_unicast=1)
642 self.send_and_expect_ra(self.pg0, p,
643 "RA with Prefix info with L-flag=0",
648 # Change the prefix info to not off-link, no-autoconfig
649 # L and A flag are clear in the advert
651 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
652 self.pg0.local_ip6_prefix_len,
656 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
657 prefix=self.pg0.local_ip6,
661 self.pg0.ip6_ra_config(send_unicast=1)
662 self.send_and_expect_ra(self.pg0, p,
663 "RA with Prefix info with A & L-flag=0",
668 # Change the flag settings back to the defaults
669 # L and A flag are set in the advert
671 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
672 self.pg0.local_ip6_prefix_len)
674 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
675 prefix=self.pg0.local_ip6,
679 self.pg0.ip6_ra_config(send_unicast=1)
680 self.send_and_expect_ra(self.pg0, p,
681 "RA with Prefix info",
686 # Change the prefix info to not off-link, no-autoconfig
687 # L and A flag are clear in the advert
689 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
690 self.pg0.local_ip6_prefix_len,
694 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
695 prefix=self.pg0.local_ip6,
699 self.pg0.ip6_ra_config(send_unicast=1)
700 self.send_and_expect_ra(self.pg0, p,
701 "RA with Prefix info with A & L-flag=0",
706 # Use the reset to defults option to revert to defaults
707 # L and A flag are clear in the advert
709 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
710 self.pg0.local_ip6_prefix_len,
713 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
714 prefix=self.pg0.local_ip6,
718 self.pg0.ip6_ra_config(send_unicast=1)
719 self.send_and_expect_ra(self.pg0, p,
720 "RA with Prefix reverted to defaults",
725 # Advertise Another prefix. With no L-flag/A-flag
727 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
728 self.pg1.local_ip6_prefix_len,
732 opt = [ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
733 prefix=self.pg0.local_ip6,
736 ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
737 prefix=self.pg1.local_ip6,
741 self.pg0.ip6_ra_config(send_unicast=1)
742 ll = mk_ll_addr(self.pg0.remote_mac)
743 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
744 IPv6(dst=self.pg0.local_ip6, src=ll) /
746 self.send_and_expect_ra(self.pg0, p,
747 "RA with multiple Prefix infos",
752 # Remove the first refix-info - expect the second is still in the
755 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
756 self.pg0.local_ip6_prefix_len,
759 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
760 prefix=self.pg1.local_ip6,
764 self.pg0.ip6_ra_config(send_unicast=1)
765 self.send_and_expect_ra(self.pg0, p,
766 "RA with Prefix reverted to defaults",
771 # Remove the second prefix-info - expect no prefix-info i nthe adverts
773 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
774 self.pg1.local_ip6_prefix_len,
777 self.pg0.ip6_ra_config(send_unicast=1)
778 self.send_and_expect_ra(self.pg0, p,
779 "RA with Prefix reverted to defaults",
783 # Reset the periodic advertisements back to default values
785 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
788 class IPv6NDProxyTest(TestIPv6ND):
789 """ IPv6 ND ProxyTest Case """
792 super(IPv6NDProxyTest, self).setUp()
794 # create 3 pg interfaces
795 self.create_pg_interfaces(range(3))
797 # pg0 is the master interface, with the configured subnet
799 self.pg0.config_ip6()
800 self.pg0.resolve_ndp()
802 self.pg1.ip6_enable()
803 self.pg2.ip6_enable()
806 super(IPv6NDProxyTest, self).tearDown()
808 def test_nd_proxy(self):
809 """ IPv6 Proxy ND """
812 # Generate some hosts in the subnet that we are proxying
814 self.pg0.generate_remote_hosts(8)
816 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
817 d = inet_ntop(AF_INET6, nsma)
820 # Send an NS for one of those remote hosts on one of the proxy links
821 # expect no response since it's from an address that is not
822 # on the link that has the prefix configured
824 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
825 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6) /
826 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
827 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
829 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
832 # Add proxy support for the host
834 self.vapi.ip6_nd_proxy(
835 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
836 self.pg1.sw_if_index)
839 # try that NS again. this time we expect an NA back
841 self.send_and_expect_na(self.pg1, ns_pg1,
843 dst_ip=self.pg0._remote_hosts[2].ip6,
844 tgt_ip=self.pg0.local_ip6)
847 # ... and that we have an entry in the ND cache
849 self.assertTrue(find_nbr(self,
850 self.pg1.sw_if_index,
851 self.pg0._remote_hosts[2].ip6,
855 # ... and we can route traffic to it
857 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
858 IPv6(dst=self.pg0._remote_hosts[2].ip6,
859 src=self.pg0.remote_ip6) /
860 UDP(sport=10000, dport=20000) /
863 self.pg0.add_stream(t)
864 self.pg_enable_capture(self.pg_interfaces)
866 rx = self.pg1.get_capture(1)
869 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
870 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
872 self.assertEqual(rx[IPv6].src, t[IPv6].src)
873 self.assertEqual(rx[IPv6].dst, t[IPv6].dst)
876 # Test we proxy for the host on the main interface
878 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
879 IPv6(dst=d, src=self.pg0.remote_ip6) /
880 ICMPv6ND_NS(tgt=self.pg0._remote_hosts[2].ip6) /
881 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
883 self.send_and_expect_na(self.pg0, ns_pg0,
884 "NS to proxy entry on main",
885 tgt_ip=self.pg0._remote_hosts[2].ip6,
886 dst_ip=self.pg0.remote_ip6)
889 # Setup and resolve proxy for another host on another interface
891 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
892 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6) /
893 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
894 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
896 self.vapi.ip6_nd_proxy(
897 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
898 self.pg2.sw_if_index)
900 self.send_and_expect_na(self.pg2, ns_pg2,
901 "NS to proxy entry other interface",
902 dst_ip=self.pg0._remote_hosts[3].ip6,
903 tgt_ip=self.pg0.local_ip6)
905 self.assertTrue(find_nbr(self,
906 self.pg2.sw_if_index,
907 self.pg0._remote_hosts[3].ip6,
911 # hosts can communicate. pg2->pg1
913 t2 = (Ether(dst=self.pg2.local_mac,
914 src=self.pg0.remote_hosts[3].mac) /
915 IPv6(dst=self.pg0._remote_hosts[2].ip6,
916 src=self.pg0._remote_hosts[3].ip6) /
917 UDP(sport=10000, dport=20000) /
920 self.pg2.add_stream(t2)
921 self.pg_enable_capture(self.pg_interfaces)
923 rx = self.pg1.get_capture(1)
926 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
927 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
929 self.assertEqual(rx[IPv6].src, t2[IPv6].src)
930 self.assertEqual(rx[IPv6].dst, t2[IPv6].dst)
933 # remove the proxy configs
935 self.vapi.ip6_nd_proxy(
936 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
937 self.pg1.sw_if_index,
939 self.vapi.ip6_nd_proxy(
940 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
941 self.pg2.sw_if_index,
944 self.assertFalse(find_nbr(self,
945 self.pg2.sw_if_index,
946 self.pg0._remote_hosts[3].ip6,
948 self.assertFalse(find_nbr(self,
949 self.pg1.sw_if_index,
950 self.pg0._remote_hosts[2].ip6,
954 # no longer proxy-ing...
956 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
957 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
958 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
961 # no longer forwarding. traffic generates NS out of the glean/main
964 self.pg2.add_stream(t2)
965 self.pg_enable_capture(self.pg_interfaces)
968 rx = self.pg0.get_capture(1)
970 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
973 class TestIPNull(VppTestCase):
974 """ IPv6 routes via NULL """
977 super(TestIPNull, self).setUp()
979 # create 2 pg interfaces
980 self.create_pg_interfaces(range(1))
982 for i in self.pg_interfaces:
988 super(TestIPNull, self).tearDown()
989 for i in self.pg_interfaces:
993 def test_ip_null(self):
994 """ IP NULL route """
996 p = (Ether(src=self.pg0.remote_mac,
997 dst=self.pg0.local_mac) /
998 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
999 UDP(sport=1234, dport=1234) /
1003 # A route via IP NULL that will reply with ICMP unreachables
1005 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1006 ip_unreach.add_vpp_config()
1008 self.pg0.add_stream(p)
1009 self.pg_enable_capture(self.pg_interfaces)
1012 rx = self.pg0.get_capture(1)
1014 icmp = rx[ICMPv6DestUnreach]
1016 # 0 = "No route to destination"
1017 self.assertEqual(icmp.code, 0)
1019 # ICMP is rate limited. pause a bit
1023 # A route via IP NULL that will reply with ICMP prohibited
1025 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1026 is_prohibit=1, is_ip6=1)
1027 ip_prohibit.add_vpp_config()
1029 self.pg0.add_stream(p)
1030 self.pg_enable_capture(self.pg_interfaces)
1033 rx = self.pg0.get_capture(1)
1035 icmp = rx[ICMPv6DestUnreach]
1037 # 1 = "Communication with destination administratively prohibited"
1038 self.assertEqual(icmp.code, 1)
1041 class TestIPDisabled(VppTestCase):
1042 """ IPv6 disabled """
1045 super(TestIPDisabled, self).setUp()
1047 # create 2 pg interfaces
1048 self.create_pg_interfaces(range(2))
1052 self.pg0.config_ip6()
1053 self.pg0.resolve_ndp()
1055 # PG 1 is not IP enabled
1059 super(TestIPDisabled, self).tearDown()
1060 for i in self.pg_interfaces:
1064 def send_and_assert_no_replies(self, intf, pkts, remark):
1065 intf.add_stream(pkts)
1066 self.pg_enable_capture(self.pg_interfaces)
1068 for i in self.pg_interfaces:
1070 i.assert_nothing_captured(remark=remark)
1072 def test_ip_disabled(self):
1077 # one accepting interface, pg0, 2 forwarding interfaces
1079 route_ff_01 = VppIpMRoute(
1083 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1084 [VppMRoutePath(self.pg1.sw_if_index,
1085 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1086 VppMRoutePath(self.pg0.sw_if_index,
1087 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1089 route_ff_01.add_vpp_config()
1091 pu = (Ether(src=self.pg1.remote_mac,
1092 dst=self.pg1.local_mac) /
1093 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1094 UDP(sport=1234, dport=1234) /
1096 pm = (Ether(src=self.pg1.remote_mac,
1097 dst=self.pg1.local_mac) /
1098 IPv6(src="2001::1", dst="ffef::1") /
1099 UDP(sport=1234, dport=1234) /
1103 # PG1 does not forward IP traffic
1105 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1106 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1111 self.pg1.config_ip6()
1114 # Now we get packets through
1116 self.pg1.add_stream(pu)
1117 self.pg_enable_capture(self.pg_interfaces)
1119 rx = self.pg0.get_capture(1)
1121 self.pg1.add_stream(pm)
1122 self.pg_enable_capture(self.pg_interfaces)
1124 rx = self.pg0.get_capture(1)
1129 self.pg1.unconfig_ip6()
1132 # PG1 does not forward IP traffic
1134 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1135 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1138 class TestIP6LoadBalance(VppTestCase):
1139 """ IPv6 Load-Balancing """
1142 super(TestIP6LoadBalance, self).setUp()
1144 self.create_pg_interfaces(range(5))
1146 for i in self.pg_interfaces:
1153 super(TestIP6LoadBalance, self).tearDown()
1154 for i in self.pg_interfaces:
1159 def send_and_expect_load_balancing(self, input, pkts, outputs):
1160 input.add_stream(pkts)
1161 self.pg_enable_capture(self.pg_interfaces)
1164 rx = oo._get_capture(1)
1165 self.assertNotEqual(0, len(rx))
1167 def send_and_expect_one_itf(self, input, pkts, itf):
1168 input.add_stream(pkts)
1169 self.pg_enable_capture(self.pg_interfaces)
1171 rx = itf.get_capture(len(pkts))
1173 def test_ip6_load_balance(self):
1174 """ IPv6 Load-Balancing """
1177 # An array of packets that differ only in the destination port
1181 # - MPLS non-EOS with an entropy label
1185 port_mpls_neos_pkts = []
1189 # An array of packets that differ only in the source address
1194 for ii in range(65):
1195 port_ip_hdr = (IPv6(dst="3000::1", src="3000:1::1") /
1196 UDP(sport=1234, dport=1234 + ii) /
1198 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1199 dst=self.pg0.local_mac) /
1201 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1202 dst=self.pg0.local_mac) /
1203 MPLS(label=66, ttl=2) /
1205 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1206 dst=self.pg0.local_mac) /
1207 MPLS(label=67, ttl=2) /
1208 MPLS(label=77, ttl=2) /
1210 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1211 dst=self.pg0.local_mac) /
1212 MPLS(label=67, ttl=2) /
1213 MPLS(label=14, ttl=2) /
1214 MPLS(label=999, ttl=2) /
1216 src_ip_hdr = (IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1217 UDP(sport=1234, dport=1234) /
1219 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1220 dst=self.pg0.local_mac) /
1222 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1223 dst=self.pg0.local_mac) /
1224 MPLS(label=66, ttl=2) /
1228 # A route for the IP pacekts
1230 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1231 [VppRoutePath(self.pg1.remote_ip6,
1232 self.pg1.sw_if_index,
1234 VppRoutePath(self.pg2.remote_ip6,
1235 self.pg2.sw_if_index,
1238 route_3000_1.add_vpp_config()
1241 # a local-label for the EOS packets
1243 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1244 binding.add_vpp_config()
1247 # An MPLS route for the non-EOS packets
1249 route_67 = VppMplsRoute(self, 67, 0,
1250 [VppRoutePath(self.pg1.remote_ip6,
1251 self.pg1.sw_if_index,
1254 VppRoutePath(self.pg2.remote_ip6,
1255 self.pg2.sw_if_index,
1258 route_67.add_vpp_config()
1261 # inject the packet on pg0 - expect load-balancing across the 2 paths
1262 # - since the default hash config is to use IP src,dst and port
1264 # We are not going to ensure equal amounts of packets across each link,
1265 # since the hash algorithm is statistical and therefore this can never
1266 # be guaranteed. But wuth 64 different packets we do expect some
1267 # balancing. So instead just ensure there is traffic on each link.
1269 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1270 [self.pg1, self.pg2])
1271 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1272 [self.pg1, self.pg2])
1273 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1274 [self.pg1, self.pg2])
1275 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1276 [self.pg1, self.pg2])
1277 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1278 [self.pg1, self.pg2])
1281 # The packets with Entropy label in should not load-balance,
1282 # since the Entorpy value is fixed.
1284 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1287 # change the flow hash config so it's only IP src,dst
1288 # - now only the stream with differing source address will
1291 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1293 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1294 [self.pg1, self.pg2])
1295 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1296 [self.pg1, self.pg2])
1297 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1300 # change the flow hash config back to defaults
1302 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1305 # Recursive prefixes
1306 # - testing that 2 stages of load-balancing occurs and there is no
1307 # polarisation (i.e. only 2 of 4 paths are used)
1312 for ii in range(257):
1313 port_pkts.append((Ether(src=self.pg0.remote_mac,
1314 dst=self.pg0.local_mac) /
1315 IPv6(dst="4000::1", src="4000:1::1") /
1316 UDP(sport=1234, dport=1234 + ii) /
1318 src_pkts.append((Ether(src=self.pg0.remote_mac,
1319 dst=self.pg0.local_mac) /
1320 IPv6(dst="4000::1", src="4000:1::%d" % ii) /
1321 UDP(sport=1234, dport=1234) /
1324 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1325 [VppRoutePath(self.pg3.remote_ip6,
1326 self.pg3.sw_if_index,
1328 VppRoutePath(self.pg4.remote_ip6,
1329 self.pg4.sw_if_index,
1332 route_3000_2.add_vpp_config()
1334 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1335 [VppRoutePath("3000::1",
1338 VppRoutePath("3000::2",
1342 route_4000_1.add_vpp_config()
1345 # inject the packet on pg0 - expect load-balancing across all 4 paths
1347 self.vapi.cli("clear trace")
1348 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1349 [self.pg1, self.pg2,
1350 self.pg3, self.pg4])
1351 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1352 [self.pg1, self.pg2,
1353 self.pg3, self.pg4])
1356 if __name__ == '__main__':
1357 unittest.main(testRunner=VppTestRunner)