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, \
11 VppMplsRoute, DpoProto
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 validate_ns(self, intf, rx, tgt_ip):
80 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
81 dst_ip = inet_ntop(AF_INET6, nsma)
84 self.assertEqual(rx[Ether].dst, "ff:ff:ff:ff:ff:ff")
86 # and from the router's MAC
87 self.assertEqual(rx[Ether].src, intf.local_mac)
89 # the rx'd NS should be addressed to an mcast address
90 # derived from the target address
91 self.assertEqual(in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
93 # expect the tgt IP in the NS header
95 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
97 # packet is from the router's local address
98 self.assertEqual(in6_ptop(rx[IPv6].src), intf.local_ip6)
100 # Src link-layer options should have the router's MAC
101 sll = rx[ICMPv6NDOptSrcLLAddr]
102 self.assertEqual(sll.lladdr, intf.local_mac)
104 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
105 filter_out_fn=is_ipv6_misc):
106 intf.add_stream(pkts)
107 self.pg_enable_capture(self.pg_interfaces)
109 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
111 self.assertEqual(len(rx), 1)
113 self.validate_ra(intf, rx, dst_ip)
115 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
117 filter_out_fn=is_ipv6_misc):
118 intf.add_stream(pkts)
119 self.pg_enable_capture(self.pg_interfaces)
121 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
123 self.assertEqual(len(rx), 1)
125 self.validate_na(intf, rx, dst_ip, tgt_ip)
127 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
128 filter_out_fn=is_ipv6_misc):
129 tx_intf.add_stream(pkts)
130 self.pg_enable_capture(self.pg_interfaces)
132 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
134 self.assertEqual(len(rx), 1)
136 self.validate_ns(rx_intf, rx, tgt_ip)
138 def send_and_assert_no_replies(self, intf, pkts, remark):
139 intf.add_stream(pkts)
140 self.pg_enable_capture(self.pg_interfaces)
142 for i in self.pg_interfaces:
144 i.assert_nothing_captured(remark=remark)
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.
180 :ivar list pg_if_packet_sizes: packet sizes in test.
182 *TODO:* Create AD sub interface
184 super(TestIPv6, self).setUp()
186 # create 3 pg interfaces
187 self.create_pg_interfaces(range(3))
189 # create 2 subinterfaces for p1 and pg2
190 self.sub_interfaces = [
191 VppDot1QSubint(self, self.pg1, 100),
192 VppDot1QSubint(self, self.pg2, 200)
193 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
196 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
198 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
199 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
200 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
203 self.pg_if_packet_sizes = [64, 512, 1518, 9018]
204 self.sub_if_packet_sizes = [64, 512, 1518 + 4, 9018 + 4]
206 self.interfaces = list(self.pg_interfaces)
207 self.interfaces.extend(self.sub_interfaces)
209 # setup all interfaces
210 for i in self.interfaces:
215 # config 2M FIB entries
216 self.config_fib_entries(200)
219 """Run standard test teardown and log ``show ip6 neighbors``."""
220 for i in self.sub_interfaces:
224 i.remove_vpp_config()
226 super(TestIPv6, self).tearDown()
227 if not self.vpp_dead:
228 self.logger.info(self.vapi.cli("show ip6 neighbors"))
229 # info(self.vapi.cli("show ip6 fib")) # many entries
231 def config_fib_entries(self, count):
232 """For each interface add to the FIB table *count* routes to
233 "fd02::1/128" destination with interface's local address as next-hop
236 :param int count: Number of FIB entries.
238 - *TODO:* check if the next-hop address shouldn't be remote address
239 instead of local address.
241 n_int = len(self.interfaces)
244 dest_addr = inet_pton(AF_INET6, "fd02::1")
246 for i in self.interfaces:
247 next_hop_address = i.local_ip6n
248 for j in range(count / n_int):
249 self.vapi.ip_add_del_route(
250 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
252 if counter / count * 100 > percent:
253 self.logger.info("Configure %d FIB entries .. %d%% done" %
257 def create_stream(self, src_if, packet_sizes):
258 """Create input packet stream for defined interface.
260 :param VppInterface src_if: Interface to create packet stream for.
261 :param list packet_sizes: Required packet sizes.
264 for i in range(0, 257):
265 dst_if = self.flows[src_if][i % 2]
266 info = self.create_packet_info(src_if, dst_if)
267 payload = self.info_to_payload(info)
268 p = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
269 IPv6(src=src_if.remote_ip6, dst=dst_if.remote_ip6) /
270 UDP(sport=1234, dport=1234) /
273 if isinstance(src_if, VppSubInterface):
274 p = src_if.add_dot1_layer(p)
275 size = packet_sizes[(i // 2) % len(packet_sizes)]
276 self.extend_packet(p, size)
280 def verify_capture(self, dst_if, capture):
281 """Verify captured input packet stream for defined interface.
283 :param VppInterface dst_if: Interface to verify captured packet stream
285 :param list capture: Captured packet stream.
287 self.logger.info("Verifying capture on interface %s" % dst_if.name)
289 for i in self.interfaces:
290 last_info[i.sw_if_index] = None
292 dst_sw_if_index = dst_if.sw_if_index
293 if hasattr(dst_if, 'parent'):
295 for packet in capture:
297 # Check VLAN tags and Ethernet header
298 packet = dst_if.remove_dot1_layer(packet)
299 self.assertTrue(Dot1Q not in packet)
303 payload_info = self.payload_to_info(str(packet[Raw]))
304 packet_index = payload_info.index
305 self.assertEqual(payload_info.dst, dst_sw_if_index)
307 "Got packet on port %s: src=%u (id=%u)" %
308 (dst_if.name, payload_info.src, packet_index))
309 next_info = self.get_next_packet_info_for_interface2(
310 payload_info.src, dst_sw_if_index,
311 last_info[payload_info.src])
312 last_info[payload_info.src] = next_info
313 self.assertTrue(next_info is not None)
314 self.assertEqual(packet_index, next_info.index)
315 saved_packet = next_info.data
316 # Check standard fields
317 self.assertEqual(ip.src, saved_packet[IPv6].src)
318 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
319 self.assertEqual(udp.sport, saved_packet[UDP].sport)
320 self.assertEqual(udp.dport, saved_packet[UDP].dport)
322 self.logger.error(ppp("Unexpected or invalid packet:", packet))
324 for i in self.interfaces:
325 remaining_packet = self.get_next_packet_info_for_interface2(
326 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
327 self.assertTrue(remaining_packet is None,
328 "Interface %s: Packet expected from interface %s "
329 "didn't arrive" % (dst_if.name, i.name))
335 - Create IPv6 stream for pg0 interface
336 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
337 - Send and verify received packets on each interface.
340 pkts = self.create_stream(self.pg0, self.pg_if_packet_sizes)
341 self.pg0.add_stream(pkts)
343 for i in self.sub_interfaces:
344 pkts = self.create_stream(i, self.sub_if_packet_sizes)
345 i.parent.add_stream(pkts)
347 self.pg_enable_capture(self.pg_interfaces)
350 pkts = self.pg0.get_capture()
351 self.verify_capture(self.pg0, pkts)
353 for i in self.sub_interfaces:
354 pkts = i.parent.get_capture()
355 self.verify_capture(i, pkts)
358 """ IPv6 Neighbour Solicitation Exceptions
361 - Send an NS Sourced from an address not covered by the link sub-net
362 - Send an NS to an mcast address the router has not joined
363 - Send NS for a target address the router does not onn.
367 # An NS from a non link source address
369 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
370 d = inet_ntop(AF_INET6, nsma)
372 p = (Ether(dst=in6_getnsmac(nsma)) /
373 IPv6(dst=d, src="2002::2") /
374 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
375 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
378 self.send_and_assert_no_replies(
380 "No response to NS source by address not on sub-net")
383 # An NS for sent to a solicited mcast group the router is
384 # not a member of FAILS
387 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
388 d = inet_ntop(AF_INET6, nsma)
390 p = (Ether(dst=in6_getnsmac(nsma)) /
391 IPv6(dst=d, src=self.pg0.remote_ip6) /
392 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
393 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
396 self.send_and_assert_no_replies(
398 "No response to NS sent to unjoined mcast address")
401 # An NS whose target address is one the router does not own
403 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
404 d = inet_ntop(AF_INET6, nsma)
406 p = (Ether(dst=in6_getnsmac(nsma)) /
407 IPv6(dst=d, src=self.pg0.remote_ip6) /
408 ICMPv6ND_NS(tgt="fd::ffff") /
409 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
412 self.send_and_assert_no_replies(self.pg0, pkts,
413 "No response to NS for unknown target")
416 # A neighbor entry that has no associated FIB-entry
418 self.pg0.generate_remote_hosts(4)
419 nd_entry = VppNeighbor(self,
420 self.pg0.sw_if_index,
421 self.pg0.remote_hosts[2].mac,
422 self.pg0.remote_hosts[2].ip6,
425 nd_entry.add_vpp_config()
428 # check we have the neighbor, but no route
430 self.assertTrue(find_nbr(self,
431 self.pg0.sw_if_index,
432 self.pg0._remote_hosts[2].ip6,
434 self.assertFalse(find_route(self,
435 self.pg0._remote_hosts[2].ip6,
440 # send an NS from a link local address to the interface's global
443 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
444 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
445 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
446 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
448 self.send_and_expect_na(self.pg0, p,
449 "NS from link-local",
450 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
451 tgt_ip=self.pg0.local_ip6)
454 # we should have learned an ND entry for the peer's link-local
455 # but not inserted a route to it in the FIB
457 self.assertTrue(find_nbr(self,
458 self.pg0.sw_if_index,
459 self.pg0._remote_hosts[2].ip6_ll,
461 self.assertFalse(find_route(self,
462 self.pg0._remote_hosts[2].ip6_ll,
467 # An NS to the router's own Link-local
469 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
470 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
471 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
472 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
474 self.send_and_expect_na(self.pg0, p,
475 "NS to/from link-local",
476 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
477 tgt_ip=self.pg0.local_ip6_ll)
480 # we should have learned an ND entry for the peer's link-local
481 # but not inserted a route to it in the FIB
483 self.assertTrue(find_nbr(self,
484 self.pg0.sw_if_index,
485 self.pg0._remote_hosts[3].ip6_ll,
487 self.assertFalse(find_route(self,
488 self.pg0._remote_hosts[3].ip6_ll,
492 def test_ns_duplicates(self):
496 # Generate some hosts on the LAN
498 self.pg1.generate_remote_hosts(3)
501 # Add host 1 on pg1 and pg2
503 ns_pg1 = VppNeighbor(self,
504 self.pg1.sw_if_index,
505 self.pg1.remote_hosts[1].mac,
506 self.pg1.remote_hosts[1].ip6,
508 ns_pg1.add_vpp_config()
509 ns_pg2 = VppNeighbor(self,
510 self.pg2.sw_if_index,
512 self.pg1.remote_hosts[1].ip6,
514 ns_pg2.add_vpp_config()
517 # IP packet destined for pg1 remote host arrives on pg1 again.
519 p = (Ether(dst=self.pg0.local_mac,
520 src=self.pg0.remote_mac) /
521 IPv6(src=self.pg0.remote_ip6,
522 dst=self.pg1.remote_hosts[1].ip6) /
523 UDP(sport=1234, dport=1234) /
526 self.pg0.add_stream(p)
527 self.pg_enable_capture(self.pg_interfaces)
530 rx1 = self.pg1.get_capture(1)
532 self.verify_ip(rx1[0],
534 self.pg1.remote_hosts[1].mac,
536 self.pg1.remote_hosts[1].ip6)
539 # remove the duplicate on pg1
540 # packet stream shoud generate NSs out of pg1
542 ns_pg1.remove_vpp_config()
544 self.send_and_expect_ns(self.pg0, self.pg1,
545 p, self.pg1.remote_hosts[1].ip6)
550 ns_pg1.add_vpp_config()
552 self.pg0.add_stream(p)
553 self.pg_enable_capture(self.pg_interfaces)
556 rx1 = self.pg1.get_capture(1)
558 self.verify_ip(rx1[0],
560 self.pg1.remote_hosts[1].mac,
562 self.pg1.remote_hosts[1].ip6)
564 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
566 dst_ip = intf.remote_ip6
568 # unicasted packets must come to the unicast mac
569 self.assertEqual(rx[Ether].dst, intf.remote_mac)
571 # and from the router's MAC
572 self.assertEqual(rx[Ether].src, intf.local_mac)
574 # the rx'd RA should be addressed to the sender's source
575 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
576 self.assertEqual(in6_ptop(rx[IPv6].dst),
579 # and come from the router's link local
580 self.assertTrue(in6_islladdr(rx[IPv6].src))
581 self.assertEqual(in6_ptop(rx[IPv6].src),
582 in6_ptop(mk_ll_addr(intf.local_mac)))
584 # it should contain the links MTU
586 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
588 # it should contain the source's link layer address option
589 sll = ra[ICMPv6NDOptSrcLLAddr]
590 self.assertEqual(sll.lladdr, intf.local_mac)
593 # the RA should not contain prefix information
594 self.assertFalse(ra.haslayer(ICMPv6NDOptPrefixInfo))
596 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
598 # the options are nested in the scapy packet in way that i cannot
599 # decipher how to decode. this 1st layer of option always returns
600 # nested classes, so a direct obj1=obj2 comparison always fails.
601 # however, the getlayer(.., 2) does give one instnace.
602 # so we cheat here and construct a new opt instnace for comparison
603 rd = ICMPv6NDOptPrefixInfo(prefixlen=raos.prefixlen,
607 if type(pi_opt) is list:
608 for ii in range(len(pi_opt)):
609 self.assertEqual(pi_opt[ii], rd)
610 rd = rx.getlayer(ICMPv6NDOptPrefixInfo, ii+2)
612 self.assertEqual(pi_opt, raos)
614 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
615 filter_out_fn=is_ipv6_misc,
617 intf.add_stream(pkts)
618 self.pg_enable_capture(self.pg_interfaces)
620 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
622 self.assertEqual(len(rx), 1)
624 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
627 """ IPv6 Router Solicitation Exceptions
633 # Before we begin change the IPv6 RA responses to use the unicast
634 # address - that way we will not confuse them with the periodic
635 # RAs which go to the mcast address
636 # Sit and wait for the first periodic RA.
640 self.pg0.ip6_ra_config(send_unicast=1)
643 # An RS from a link source address
644 # - expect an RA in return
646 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
647 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
650 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
653 # For the next RS sent the RA should be rate limited
655 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
658 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
659 # so we need to do this before each test below so as not to drop
660 # packets for rate limiting reasons. Test this works here.
662 self.pg0.ip6_ra_config(send_unicast=1)
663 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
666 # An RS sent from a non-link local source
668 self.pg0.ip6_ra_config(send_unicast=1)
669 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
670 IPv6(dst=self.pg0.local_ip6, src="2002::ffff") /
673 self.send_and_assert_no_replies(self.pg0, pkts,
674 "RS from non-link source")
677 # Source an RS from a link local address
679 self.pg0.ip6_ra_config(send_unicast=1)
680 ll = mk_ll_addr(self.pg0.remote_mac)
681 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
682 IPv6(dst=self.pg0.local_ip6, src=ll) /
685 self.send_and_expect_ra(self.pg0, pkts,
686 "RS sourced from link-local",
690 # Send the RS multicast
692 self.pg0.ip6_ra_config(send_unicast=1)
693 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
694 ll = mk_ll_addr(self.pg0.remote_mac)
695 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
696 IPv6(dst="ff02::2", src=ll) /
699 self.send_and_expect_ra(self.pg0, pkts,
700 "RS sourced from link-local",
704 # Source from the unspecified address ::. This happens when the RS
705 # is sent before the host has a configured address/sub-net,
706 # i.e. auto-config. Since the sender has no IP address, the reply
707 # comes back mcast - so the capture needs to not filter this.
708 # If we happen to pick up the periodic RA at this point then so be it,
711 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
712 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
713 IPv6(dst="ff02::2", src="::") /
716 self.send_and_expect_ra(self.pg0, pkts,
717 "RS sourced from unspecified",
722 # Configure The RA to announce the links prefix
724 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
725 self.pg0.local_ip6_prefix_len)
728 # RAs should now contain the prefix information option
730 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
731 prefix=self.pg0.local_ip6,
735 self.pg0.ip6_ra_config(send_unicast=1)
736 ll = mk_ll_addr(self.pg0.remote_mac)
737 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
738 IPv6(dst=self.pg0.local_ip6, src=ll) /
740 self.send_and_expect_ra(self.pg0, p,
741 "RA with prefix-info",
746 # Change the prefix info to not off-link
749 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
750 self.pg0.local_ip6_prefix_len,
753 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
754 prefix=self.pg0.local_ip6,
758 self.pg0.ip6_ra_config(send_unicast=1)
759 self.send_and_expect_ra(self.pg0, p,
760 "RA with Prefix info with L-flag=0",
765 # Change the prefix info to not off-link, no-autoconfig
766 # L and A flag are clear in the advert
768 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
769 self.pg0.local_ip6_prefix_len,
773 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
774 prefix=self.pg0.local_ip6,
778 self.pg0.ip6_ra_config(send_unicast=1)
779 self.send_and_expect_ra(self.pg0, p,
780 "RA with Prefix info with A & L-flag=0",
785 # Change the flag settings back to the defaults
786 # L and A flag are set in the advert
788 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
789 self.pg0.local_ip6_prefix_len)
791 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
792 prefix=self.pg0.local_ip6,
796 self.pg0.ip6_ra_config(send_unicast=1)
797 self.send_and_expect_ra(self.pg0, p,
798 "RA with Prefix info",
803 # Change the prefix info to not off-link, no-autoconfig
804 # L and A flag are clear in the advert
806 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
807 self.pg0.local_ip6_prefix_len,
811 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
812 prefix=self.pg0.local_ip6,
816 self.pg0.ip6_ra_config(send_unicast=1)
817 self.send_and_expect_ra(self.pg0, p,
818 "RA with Prefix info with A & L-flag=0",
823 # Use the reset to defults option to revert to defaults
824 # L and A flag are clear in the advert
826 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
827 self.pg0.local_ip6_prefix_len,
830 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
831 prefix=self.pg0.local_ip6,
835 self.pg0.ip6_ra_config(send_unicast=1)
836 self.send_and_expect_ra(self.pg0, p,
837 "RA with Prefix reverted to defaults",
842 # Advertise Another prefix. With no L-flag/A-flag
844 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
845 self.pg1.local_ip6_prefix_len,
849 opt = [ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
850 prefix=self.pg0.local_ip6,
853 ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
854 prefix=self.pg1.local_ip6,
858 self.pg0.ip6_ra_config(send_unicast=1)
859 ll = mk_ll_addr(self.pg0.remote_mac)
860 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
861 IPv6(dst=self.pg0.local_ip6, src=ll) /
863 self.send_and_expect_ra(self.pg0, p,
864 "RA with multiple Prefix infos",
869 # Remove the first refix-info - expect the second is still in the
872 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
873 self.pg0.local_ip6_prefix_len,
876 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
877 prefix=self.pg1.local_ip6,
881 self.pg0.ip6_ra_config(send_unicast=1)
882 self.send_and_expect_ra(self.pg0, p,
883 "RA with Prefix reverted to defaults",
888 # Remove the second prefix-info - expect no prefix-info i nthe adverts
890 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
891 self.pg1.local_ip6_prefix_len,
894 self.pg0.ip6_ra_config(send_unicast=1)
895 self.send_and_expect_ra(self.pg0, p,
896 "RA with Prefix reverted to defaults",
900 # Reset the periodic advertisements back to default values
902 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
905 class IPv6NDProxyTest(TestIPv6ND):
906 """ IPv6 ND ProxyTest Case """
909 super(IPv6NDProxyTest, self).setUp()
911 # create 3 pg interfaces
912 self.create_pg_interfaces(range(3))
914 # pg0 is the master interface, with the configured subnet
916 self.pg0.config_ip6()
917 self.pg0.resolve_ndp()
919 self.pg1.ip6_enable()
920 self.pg2.ip6_enable()
923 super(IPv6NDProxyTest, self).tearDown()
925 def test_nd_proxy(self):
926 """ IPv6 Proxy ND """
929 # Generate some hosts in the subnet that we are proxying
931 self.pg0.generate_remote_hosts(8)
933 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
934 d = inet_ntop(AF_INET6, nsma)
937 # Send an NS for one of those remote hosts on one of the proxy links
938 # expect no response since it's from an address that is not
939 # on the link that has the prefix configured
941 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
942 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6) /
943 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
944 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
946 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
949 # Add proxy support for the host
951 self.vapi.ip6_nd_proxy(
952 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
953 self.pg1.sw_if_index)
956 # try that NS again. this time we expect an NA back
958 self.send_and_expect_na(self.pg1, ns_pg1,
960 dst_ip=self.pg0._remote_hosts[2].ip6,
961 tgt_ip=self.pg0.local_ip6)
964 # ... and that we have an entry in the ND cache
966 self.assertTrue(find_nbr(self,
967 self.pg1.sw_if_index,
968 self.pg0._remote_hosts[2].ip6,
972 # ... and we can route traffic to it
974 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
975 IPv6(dst=self.pg0._remote_hosts[2].ip6,
976 src=self.pg0.remote_ip6) /
977 UDP(sport=10000, dport=20000) /
980 self.pg0.add_stream(t)
981 self.pg_enable_capture(self.pg_interfaces)
983 rx = self.pg1.get_capture(1)
986 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
987 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
989 self.assertEqual(rx[IPv6].src, t[IPv6].src)
990 self.assertEqual(rx[IPv6].dst, t[IPv6].dst)
993 # Test we proxy for the host on the main interface
995 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
996 IPv6(dst=d, src=self.pg0.remote_ip6) /
997 ICMPv6ND_NS(tgt=self.pg0._remote_hosts[2].ip6) /
998 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
1000 self.send_and_expect_na(self.pg0, ns_pg0,
1001 "NS to proxy entry on main",
1002 tgt_ip=self.pg0._remote_hosts[2].ip6,
1003 dst_ip=self.pg0.remote_ip6)
1006 # Setup and resolve proxy for another host on another interface
1008 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1009 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6) /
1010 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1011 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
1013 self.vapi.ip6_nd_proxy(
1014 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1015 self.pg2.sw_if_index)
1017 self.send_and_expect_na(self.pg2, ns_pg2,
1018 "NS to proxy entry other interface",
1019 dst_ip=self.pg0._remote_hosts[3].ip6,
1020 tgt_ip=self.pg0.local_ip6)
1022 self.assertTrue(find_nbr(self,
1023 self.pg2.sw_if_index,
1024 self.pg0._remote_hosts[3].ip6,
1028 # hosts can communicate. pg2->pg1
1030 t2 = (Ether(dst=self.pg2.local_mac,
1031 src=self.pg0.remote_hosts[3].mac) /
1032 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1033 src=self.pg0._remote_hosts[3].ip6) /
1034 UDP(sport=10000, dport=20000) /
1037 self.pg2.add_stream(t2)
1038 self.pg_enable_capture(self.pg_interfaces)
1040 rx = self.pg1.get_capture(1)
1043 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1044 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1046 self.assertEqual(rx[IPv6].src, t2[IPv6].src)
1047 self.assertEqual(rx[IPv6].dst, t2[IPv6].dst)
1050 # remove the proxy configs
1052 self.vapi.ip6_nd_proxy(
1053 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1054 self.pg1.sw_if_index,
1056 self.vapi.ip6_nd_proxy(
1057 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1058 self.pg2.sw_if_index,
1061 self.assertFalse(find_nbr(self,
1062 self.pg2.sw_if_index,
1063 self.pg0._remote_hosts[3].ip6,
1065 self.assertFalse(find_nbr(self,
1066 self.pg1.sw_if_index,
1067 self.pg0._remote_hosts[2].ip6,
1071 # no longer proxy-ing...
1073 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1074 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1075 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1078 # no longer forwarding. traffic generates NS out of the glean/main
1081 self.pg2.add_stream(t2)
1082 self.pg_enable_capture(self.pg_interfaces)
1085 rx = self.pg0.get_capture(1)
1087 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1090 class TestIPNull(VppTestCase):
1091 """ IPv6 routes via NULL """
1094 super(TestIPNull, self).setUp()
1096 # create 2 pg interfaces
1097 self.create_pg_interfaces(range(1))
1099 for i in self.pg_interfaces:
1105 super(TestIPNull, self).tearDown()
1106 for i in self.pg_interfaces:
1110 def test_ip_null(self):
1111 """ IP NULL route """
1113 p = (Ether(src=self.pg0.remote_mac,
1114 dst=self.pg0.local_mac) /
1115 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1116 UDP(sport=1234, dport=1234) /
1120 # A route via IP NULL that will reply with ICMP unreachables
1122 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1123 ip_unreach.add_vpp_config()
1125 self.pg0.add_stream(p)
1126 self.pg_enable_capture(self.pg_interfaces)
1129 rx = self.pg0.get_capture(1)
1131 icmp = rx[ICMPv6DestUnreach]
1133 # 0 = "No route to destination"
1134 self.assertEqual(icmp.code, 0)
1136 # ICMP is rate limited. pause a bit
1140 # A route via IP NULL that will reply with ICMP prohibited
1142 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1143 is_prohibit=1, is_ip6=1)
1144 ip_prohibit.add_vpp_config()
1146 self.pg0.add_stream(p)
1147 self.pg_enable_capture(self.pg_interfaces)
1150 rx = self.pg0.get_capture(1)
1152 icmp = rx[ICMPv6DestUnreach]
1154 # 1 = "Communication with destination administratively prohibited"
1155 self.assertEqual(icmp.code, 1)
1158 class TestIPDisabled(VppTestCase):
1159 """ IPv6 disabled """
1162 super(TestIPDisabled, self).setUp()
1164 # create 2 pg interfaces
1165 self.create_pg_interfaces(range(2))
1169 self.pg0.config_ip6()
1170 self.pg0.resolve_ndp()
1172 # PG 1 is not IP enabled
1176 super(TestIPDisabled, self).tearDown()
1177 for i in self.pg_interfaces:
1181 def send_and_assert_no_replies(self, intf, pkts, remark):
1182 intf.add_stream(pkts)
1183 self.pg_enable_capture(self.pg_interfaces)
1185 for i in self.pg_interfaces:
1187 i.assert_nothing_captured(remark=remark)
1189 def test_ip_disabled(self):
1194 # one accepting interface, pg0, 2 forwarding interfaces
1196 route_ff_01 = VppIpMRoute(
1200 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1201 [VppMRoutePath(self.pg1.sw_if_index,
1202 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1203 VppMRoutePath(self.pg0.sw_if_index,
1204 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1206 route_ff_01.add_vpp_config()
1208 pu = (Ether(src=self.pg1.remote_mac,
1209 dst=self.pg1.local_mac) /
1210 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1211 UDP(sport=1234, dport=1234) /
1213 pm = (Ether(src=self.pg1.remote_mac,
1214 dst=self.pg1.local_mac) /
1215 IPv6(src="2001::1", dst="ffef::1") /
1216 UDP(sport=1234, dport=1234) /
1220 # PG1 does not forward IP traffic
1222 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1223 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1228 self.pg1.config_ip6()
1231 # Now we get packets through
1233 self.pg1.add_stream(pu)
1234 self.pg_enable_capture(self.pg_interfaces)
1236 rx = self.pg0.get_capture(1)
1238 self.pg1.add_stream(pm)
1239 self.pg_enable_capture(self.pg_interfaces)
1241 rx = self.pg0.get_capture(1)
1246 self.pg1.unconfig_ip6()
1249 # PG1 does not forward IP traffic
1251 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1252 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1255 class TestIP6LoadBalance(VppTestCase):
1256 """ IPv6 Load-Balancing """
1259 super(TestIP6LoadBalance, self).setUp()
1261 self.create_pg_interfaces(range(5))
1263 for i in self.pg_interfaces:
1270 super(TestIP6LoadBalance, self).tearDown()
1271 for i in self.pg_interfaces:
1276 def send_and_expect_load_balancing(self, input, pkts, outputs):
1277 input.add_stream(pkts)
1278 self.pg_enable_capture(self.pg_interfaces)
1281 rx = oo._get_capture(1)
1282 self.assertNotEqual(0, len(rx))
1284 def send_and_expect_one_itf(self, input, pkts, itf):
1285 input.add_stream(pkts)
1286 self.pg_enable_capture(self.pg_interfaces)
1288 rx = itf.get_capture(len(pkts))
1290 def test_ip6_load_balance(self):
1291 """ IPv6 Load-Balancing """
1294 # An array of packets that differ only in the destination port
1298 # - MPLS non-EOS with an entropy label
1302 port_mpls_neos_pkts = []
1306 # An array of packets that differ only in the source address
1311 for ii in range(65):
1312 port_ip_hdr = (IPv6(dst="3000::1", src="3000:1::1") /
1313 UDP(sport=1234, dport=1234 + ii) /
1315 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1316 dst=self.pg0.local_mac) /
1318 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1319 dst=self.pg0.local_mac) /
1320 MPLS(label=66, ttl=2) /
1322 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1323 dst=self.pg0.local_mac) /
1324 MPLS(label=67, ttl=2) /
1325 MPLS(label=77, ttl=2) /
1327 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1328 dst=self.pg0.local_mac) /
1329 MPLS(label=67, ttl=2) /
1330 MPLS(label=14, ttl=2) /
1331 MPLS(label=999, ttl=2) /
1333 src_ip_hdr = (IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1334 UDP(sport=1234, dport=1234) /
1336 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1337 dst=self.pg0.local_mac) /
1339 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1340 dst=self.pg0.local_mac) /
1341 MPLS(label=66, ttl=2) /
1345 # A route for the IP pacekts
1347 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1348 [VppRoutePath(self.pg1.remote_ip6,
1349 self.pg1.sw_if_index,
1350 proto=DpoProto.DPO_PROTO_IP6),
1351 VppRoutePath(self.pg2.remote_ip6,
1352 self.pg2.sw_if_index,
1353 proto=DpoProto.DPO_PROTO_IP6)],
1355 route_3000_1.add_vpp_config()
1358 # a local-label for the EOS packets
1360 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1361 binding.add_vpp_config()
1364 # An MPLS route for the non-EOS packets
1366 route_67 = VppMplsRoute(self, 67, 0,
1367 [VppRoutePath(self.pg1.remote_ip6,
1368 self.pg1.sw_if_index,
1370 proto=DpoProto.DPO_PROTO_IP6),
1371 VppRoutePath(self.pg2.remote_ip6,
1372 self.pg2.sw_if_index,
1374 proto=DpoProto.DPO_PROTO_IP6)])
1375 route_67.add_vpp_config()
1378 # inject the packet on pg0 - expect load-balancing across the 2 paths
1379 # - since the default hash config is to use IP src,dst and port
1381 # We are not going to ensure equal amounts of packets across each link,
1382 # since the hash algorithm is statistical and therefore this can never
1383 # be guaranteed. But wuth 64 different packets we do expect some
1384 # balancing. So instead just ensure there is traffic on each link.
1386 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1387 [self.pg1, self.pg2])
1388 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1389 [self.pg1, self.pg2])
1390 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1391 [self.pg1, self.pg2])
1392 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1393 [self.pg1, self.pg2])
1394 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1395 [self.pg1, self.pg2])
1398 # The packets with Entropy label in should not load-balance,
1399 # since the Entorpy value is fixed.
1401 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1404 # change the flow hash config so it's only IP src,dst
1405 # - now only the stream with differing source address will
1408 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1410 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1411 [self.pg1, self.pg2])
1412 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1413 [self.pg1, self.pg2])
1414 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1417 # change the flow hash config back to defaults
1419 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1422 # Recursive prefixes
1423 # - testing that 2 stages of load-balancing occurs and there is no
1424 # polarisation (i.e. only 2 of 4 paths are used)
1429 for ii in range(257):
1430 port_pkts.append((Ether(src=self.pg0.remote_mac,
1431 dst=self.pg0.local_mac) /
1432 IPv6(dst="4000::1", src="4000:1::1") /
1433 UDP(sport=1234, dport=1234 + ii) /
1435 src_pkts.append((Ether(src=self.pg0.remote_mac,
1436 dst=self.pg0.local_mac) /
1437 IPv6(dst="4000::1", src="4000:1::%d" % ii) /
1438 UDP(sport=1234, dport=1234) /
1441 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1442 [VppRoutePath(self.pg3.remote_ip6,
1443 self.pg3.sw_if_index,
1444 proto=DpoProto.DPO_PROTO_IP6),
1445 VppRoutePath(self.pg4.remote_ip6,
1446 self.pg4.sw_if_index,
1447 proto=DpoProto.DPO_PROTO_IP6)],
1449 route_3000_2.add_vpp_config()
1451 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1452 [VppRoutePath("3000::1",
1454 proto=DpoProto.DPO_PROTO_IP6),
1455 VppRoutePath("3000::2",
1457 proto=DpoProto.DPO_PROTO_IP6)],
1459 route_4000_1.add_vpp_config()
1462 # inject the packet on pg0 - expect load-balancing across all 4 paths
1464 self.vapi.cli("clear trace")
1465 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1466 [self.pg1, self.pg2,
1467 self.pg3, self.pg4])
1468 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1469 [self.pg1, self.pg2,
1470 self.pg3, self.pg4])
1473 # Recursive prefixes
1474 # - testing that 2 stages of load-balancing no choices
1478 for ii in range(257):
1479 port_pkts.append((Ether(src=self.pg0.remote_mac,
1480 dst=self.pg0.local_mac) /
1481 IPv6(dst="6000::1", src="6000:1::1") /
1482 UDP(sport=1234, dport=1234 + ii) /
1485 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1486 [VppRoutePath(self.pg3.remote_ip6,
1487 self.pg3.sw_if_index,
1488 proto=DpoProto.DPO_PROTO_IP6)],
1490 route_5000_2.add_vpp_config()
1492 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1493 [VppRoutePath("5000::2",
1495 proto=DpoProto.DPO_PROTO_IP6)],
1497 route_6000_1.add_vpp_config()
1500 # inject the packet on pg0 - expect load-balancing across all 4 paths
1502 self.vapi.cli("clear trace")
1503 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1506 if __name__ == '__main__':
1507 unittest.main(testRunner=VppTestRunner)