6 from framework import VppTestCase, VppTestRunner
7 from util import ppp, ip6_normalize
8 from vpp_sub_interface import VppSubInterface, VppDot1QSubint
9 from vpp_pg_interface import is_ipv6_misc
10 from vpp_ip import DpoProto
11 from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
12 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
13 VppMplsRoute, VppMplsTable, VppIpTable
14 from vpp_neighbor import find_nbr, VppNeighbor
16 from scapy.packet import Raw
17 from scapy.layers.l2 import Ether, Dot1Q
18 from scapy.layers.inet6 import IPv6, UDP, TCP, ICMPv6ND_NS, ICMPv6ND_RS, \
19 ICMPv6ND_RA, ICMPv6NDOptSrcLLAddr, getmacbyip6, ICMPv6MRD_Solicitation, \
20 ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
21 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
22 ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply
23 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
24 in6_mactoifaceid, in6_ismaddr
25 from scapy.utils import inet_pton, inet_ntop
26 from scapy.contrib.mpls import MPLS
29 AF_INET6 = socket.AF_INET6
33 euid = in6_mactoifaceid(mac)
34 addr = "fe80::" + euid
38 class TestIPv6ND(VppTestCase):
39 def validate_ra(self, intf, rx, dst_ip=None):
41 dst_ip = intf.remote_ip6
43 # unicasted packets must come to the unicast mac
44 self.assertEqual(rx[Ether].dst, intf.remote_mac)
46 # and from the router's MAC
47 self.assertEqual(rx[Ether].src, intf.local_mac)
49 # the rx'd RA should be addressed to the sender's source
50 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
51 self.assertEqual(in6_ptop(rx[IPv6].dst),
54 # and come from the router's link local
55 self.assertTrue(in6_islladdr(rx[IPv6].src))
56 self.assertEqual(in6_ptop(rx[IPv6].src),
57 in6_ptop(mk_ll_addr(intf.local_mac)))
59 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
61 dst_ip = intf.remote_ip6
63 dst_ip = intf.local_ip6
65 # unicasted packets must come to the unicast mac
66 self.assertEqual(rx[Ether].dst, intf.remote_mac)
68 # and from the router's MAC
69 self.assertEqual(rx[Ether].src, intf.local_mac)
71 # the rx'd NA should be addressed to the sender's source
72 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
73 self.assertEqual(in6_ptop(rx[IPv6].dst),
76 # and come from the target address
77 self.assertEqual(in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
79 # Dest link-layer options should have the router's MAC
80 dll = rx[ICMPv6NDOptDstLLAddr]
81 self.assertEqual(dll.lladdr, intf.local_mac)
83 def validate_ns(self, intf, rx, tgt_ip):
84 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
85 dst_ip = inet_ntop(AF_INET6, nsma)
88 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
90 # and from the router's MAC
91 self.assertEqual(rx[Ether].src, intf.local_mac)
93 # the rx'd NS should be addressed to an mcast address
94 # derived from the target address
95 self.assertEqual(in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
97 # expect the tgt IP in the NS header
99 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
101 # packet is from the router's local address
102 self.assertEqual(in6_ptop(rx[IPv6].src), intf.local_ip6)
104 # Src link-layer options should have the router's MAC
105 sll = rx[ICMPv6NDOptSrcLLAddr]
106 self.assertEqual(sll.lladdr, intf.local_mac)
108 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
109 filter_out_fn=is_ipv6_misc):
110 intf.add_stream(pkts)
111 self.pg_enable_capture(self.pg_interfaces)
113 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
115 self.assertEqual(len(rx), 1)
117 self.validate_ra(intf, rx, dst_ip)
119 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
121 filter_out_fn=is_ipv6_misc):
122 intf.add_stream(pkts)
123 self.pg_enable_capture(self.pg_interfaces)
125 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
127 self.assertEqual(len(rx), 1)
129 self.validate_na(intf, rx, dst_ip, tgt_ip)
131 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
132 filter_out_fn=is_ipv6_misc):
133 tx_intf.add_stream(pkts)
134 self.pg_enable_capture(self.pg_interfaces)
136 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
138 self.assertEqual(len(rx), 1)
140 self.validate_ns(rx_intf, rx, tgt_ip)
142 def verify_ip(self, rx, smac, dmac, sip, dip):
144 self.assertEqual(ether.dst, dmac)
145 self.assertEqual(ether.src, smac)
148 self.assertEqual(ip.src, sip)
149 self.assertEqual(ip.dst, dip)
152 class TestIPv6(TestIPv6ND):
153 """ IPv6 Test Case """
157 super(TestIPv6, cls).setUpClass()
161 Perform test setup before test case.
164 - create 3 pg interfaces
165 - untagged pg0 interface
166 - Dot1Q subinterface on pg1
167 - Dot1AD subinterface on pg2
169 - put it into UP state
171 - resolve neighbor address using NDP
172 - configure 200 fib entries
174 :ivar list interfaces: pg interfaces and subinterfaces.
175 :ivar dict flows: IPv4 packet flows in test.
177 *TODO:* Create AD sub interface
179 super(TestIPv6, self).setUp()
181 # create 3 pg interfaces
182 self.create_pg_interfaces(range(3))
184 # create 2 subinterfaces for p1 and pg2
185 self.sub_interfaces = [
186 VppDot1QSubint(self, self.pg1, 100),
187 VppDot1QSubint(self, self.pg2, 200)
188 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
191 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
193 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
194 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
195 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
198 self.pg_if_packet_sizes = [64, 1500, 9020]
200 self.interfaces = list(self.pg_interfaces)
201 self.interfaces.extend(self.sub_interfaces)
203 # setup all interfaces
204 for i in self.interfaces:
209 # config 2M FIB entries
210 self.config_fib_entries(200)
213 """Run standard test teardown and log ``show ip6 neighbors``."""
214 for i in self.interfaces:
218 for i in self.sub_interfaces:
219 i.remove_vpp_config()
221 super(TestIPv6, self).tearDown()
222 if not self.vpp_dead:
223 self.logger.info(self.vapi.cli("show ip6 neighbors"))
224 # info(self.vapi.cli("show ip6 fib")) # many entries
226 def config_fib_entries(self, count):
227 """For each interface add to the FIB table *count* routes to
228 "fd02::1/128" destination with interface's local address as next-hop
231 :param int count: Number of FIB entries.
233 - *TODO:* check if the next-hop address shouldn't be remote address
234 instead of local address.
236 n_int = len(self.interfaces)
239 dest_addr = inet_pton(AF_INET6, "fd02::1")
241 for i in self.interfaces:
242 next_hop_address = i.local_ip6n
243 for j in range(count / n_int):
244 self.vapi.ip_add_del_route(
245 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
247 if counter / count * 100 > percent:
248 self.logger.info("Configure %d FIB entries .. %d%% done" %
252 def modify_packet(self, src_if, packet_size, pkt):
253 """Add load, set destination IP and extend packet to required packet
254 size for defined interface.
256 :param VppInterface src_if: Interface to create packet for.
257 :param int packet_size: Required packet size.
258 :param Scapy pkt: Packet to be modified.
260 dst_if_idx = packet_size / 10 % 2
261 dst_if = self.flows[src_if][dst_if_idx]
262 info = self.create_packet_info(src_if, dst_if)
263 payload = self.info_to_payload(info)
265 p[IPv6].dst = dst_if.remote_ip6
267 if isinstance(src_if, VppSubInterface):
268 p = src_if.add_dot1_layer(p)
269 self.extend_packet(p, packet_size)
273 def create_stream(self, src_if):
274 """Create input packet stream for defined interface.
276 :param VppInterface src_if: Interface to create packet stream for.
278 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
279 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
280 IPv6(src=src_if.remote_ip6) /
281 UDP(sport=1234, dport=1234))
283 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
284 for i in xrange(self.pg_if_packet_sizes[0],
285 self.pg_if_packet_sizes[1], 10)]
286 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
287 for i in xrange(self.pg_if_packet_sizes[1] + hdr_ext,
288 self.pg_if_packet_sizes[2] + hdr_ext, 50)]
293 def verify_capture(self, dst_if, capture):
294 """Verify captured input packet stream for defined interface.
296 :param VppInterface dst_if: Interface to verify captured packet stream
298 :param list capture: Captured packet stream.
300 self.logger.info("Verifying capture on interface %s" % dst_if.name)
302 for i in self.interfaces:
303 last_info[i.sw_if_index] = None
305 dst_sw_if_index = dst_if.sw_if_index
306 if hasattr(dst_if, 'parent'):
308 for packet in capture:
310 # Check VLAN tags and Ethernet header
311 packet = dst_if.remove_dot1_layer(packet)
312 self.assertTrue(Dot1Q not in packet)
316 payload_info = self.payload_to_info(str(packet[Raw]))
317 packet_index = payload_info.index
318 self.assertEqual(payload_info.dst, dst_sw_if_index)
320 "Got packet on port %s: src=%u (id=%u)" %
321 (dst_if.name, payload_info.src, packet_index))
322 next_info = self.get_next_packet_info_for_interface2(
323 payload_info.src, dst_sw_if_index,
324 last_info[payload_info.src])
325 last_info[payload_info.src] = next_info
326 self.assertTrue(next_info is not None)
327 self.assertEqual(packet_index, next_info.index)
328 saved_packet = next_info.data
329 # Check standard fields
330 self.assertEqual(ip.src, saved_packet[IPv6].src)
331 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
332 self.assertEqual(udp.sport, saved_packet[UDP].sport)
333 self.assertEqual(udp.dport, saved_packet[UDP].dport)
335 self.logger.error(ppp("Unexpected or invalid packet:", packet))
337 for i in self.interfaces:
338 remaining_packet = self.get_next_packet_info_for_interface2(
339 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
340 self.assertTrue(remaining_packet is None,
341 "Interface %s: Packet expected from interface %s "
342 "didn't arrive" % (dst_if.name, i.name))
348 - Create IPv6 stream for pg0 interface
349 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
350 - Send and verify received packets on each interface.
353 pkts = self.create_stream(self.pg0)
354 self.pg0.add_stream(pkts)
356 for i in self.sub_interfaces:
357 pkts = self.create_stream(i)
358 i.parent.add_stream(pkts)
360 self.pg_enable_capture(self.pg_interfaces)
363 pkts = self.pg0.get_capture()
364 self.verify_capture(self.pg0, pkts)
366 for i in self.sub_interfaces:
367 pkts = i.parent.get_capture()
368 self.verify_capture(i, pkts)
371 """ IPv6 Neighbour Solicitation Exceptions
374 - Send an NS Sourced from an address not covered by the link sub-net
375 - Send an NS to an mcast address the router has not joined
376 - Send NS for a target address the router does not onn.
380 # An NS from a non link source address
382 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
383 d = inet_ntop(AF_INET6, nsma)
385 p = (Ether(dst=in6_getnsmac(nsma)) /
386 IPv6(dst=d, src="2002::2") /
387 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
388 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
391 self.send_and_assert_no_replies(
393 "No response to NS source by address not on sub-net")
396 # An NS for sent to a solicited mcast group the router is
397 # not a member of FAILS
400 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
401 d = inet_ntop(AF_INET6, nsma)
403 p = (Ether(dst=in6_getnsmac(nsma)) /
404 IPv6(dst=d, src=self.pg0.remote_ip6) /
405 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
406 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
409 self.send_and_assert_no_replies(
411 "No response to NS sent to unjoined mcast address")
414 # An NS whose target address is one the router does not own
416 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
417 d = inet_ntop(AF_INET6, nsma)
419 p = (Ether(dst=in6_getnsmac(nsma)) /
420 IPv6(dst=d, src=self.pg0.remote_ip6) /
421 ICMPv6ND_NS(tgt="fd::ffff") /
422 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
425 self.send_and_assert_no_replies(self.pg0, pkts,
426 "No response to NS for unknown target")
429 # A neighbor entry that has no associated FIB-entry
431 self.pg0.generate_remote_hosts(4)
432 nd_entry = VppNeighbor(self,
433 self.pg0.sw_if_index,
434 self.pg0.remote_hosts[2].mac,
435 self.pg0.remote_hosts[2].ip6,
438 nd_entry.add_vpp_config()
441 # check we have the neighbor, but no route
443 self.assertTrue(find_nbr(self,
444 self.pg0.sw_if_index,
445 self.pg0._remote_hosts[2].ip6,
447 self.assertFalse(find_route(self,
448 self.pg0._remote_hosts[2].ip6,
453 # send an NS from a link local address to the interface's global
456 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
457 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
458 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
459 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
461 self.send_and_expect_na(self.pg0, p,
462 "NS from link-local",
463 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
464 tgt_ip=self.pg0.local_ip6)
467 # we should have learned an ND entry for the peer's link-local
468 # but not inserted a route to it in the FIB
470 self.assertTrue(find_nbr(self,
471 self.pg0.sw_if_index,
472 self.pg0._remote_hosts[2].ip6_ll,
474 self.assertFalse(find_route(self,
475 self.pg0._remote_hosts[2].ip6_ll,
480 # An NS to the router's own Link-local
482 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
483 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
484 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
485 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
487 self.send_and_expect_na(self.pg0, p,
488 "NS to/from link-local",
489 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
490 tgt_ip=self.pg0.local_ip6_ll)
493 # we should have learned an ND entry for the peer's link-local
494 # but not inserted a route to it in the FIB
496 self.assertTrue(find_nbr(self,
497 self.pg0.sw_if_index,
498 self.pg0._remote_hosts[3].ip6_ll,
500 self.assertFalse(find_route(self,
501 self.pg0._remote_hosts[3].ip6_ll,
505 def test_ns_duplicates(self):
509 # Generate some hosts on the LAN
511 self.pg1.generate_remote_hosts(3)
514 # Add host 1 on pg1 and pg2
516 ns_pg1 = VppNeighbor(self,
517 self.pg1.sw_if_index,
518 self.pg1.remote_hosts[1].mac,
519 self.pg1.remote_hosts[1].ip6,
521 ns_pg1.add_vpp_config()
522 ns_pg2 = VppNeighbor(self,
523 self.pg2.sw_if_index,
525 self.pg1.remote_hosts[1].ip6,
527 ns_pg2.add_vpp_config()
530 # IP packet destined for pg1 remote host arrives on pg1 again.
532 p = (Ether(dst=self.pg0.local_mac,
533 src=self.pg0.remote_mac) /
534 IPv6(src=self.pg0.remote_ip6,
535 dst=self.pg1.remote_hosts[1].ip6) /
536 UDP(sport=1234, dport=1234) /
539 self.pg0.add_stream(p)
540 self.pg_enable_capture(self.pg_interfaces)
543 rx1 = self.pg1.get_capture(1)
545 self.verify_ip(rx1[0],
547 self.pg1.remote_hosts[1].mac,
549 self.pg1.remote_hosts[1].ip6)
552 # remove the duplicate on pg1
553 # packet stream shoud generate NSs out of pg1
555 ns_pg1.remove_vpp_config()
557 self.send_and_expect_ns(self.pg0, self.pg1,
558 p, self.pg1.remote_hosts[1].ip6)
563 ns_pg1.add_vpp_config()
565 self.pg0.add_stream(p)
566 self.pg_enable_capture(self.pg_interfaces)
569 rx1 = self.pg1.get_capture(1)
571 self.verify_ip(rx1[0],
573 self.pg1.remote_hosts[1].mac,
575 self.pg1.remote_hosts[1].ip6)
577 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
579 dst_ip = intf.remote_ip6
581 # unicasted packets must come to the unicast mac
582 self.assertEqual(rx[Ether].dst, intf.remote_mac)
584 # and from the router's MAC
585 self.assertEqual(rx[Ether].src, intf.local_mac)
587 # the rx'd RA should be addressed to the sender's source
588 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
589 self.assertEqual(in6_ptop(rx[IPv6].dst),
592 # and come from the router's link local
593 self.assertTrue(in6_islladdr(rx[IPv6].src))
594 self.assertEqual(in6_ptop(rx[IPv6].src),
595 in6_ptop(mk_ll_addr(intf.local_mac)))
597 # it should contain the links MTU
599 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
601 # it should contain the source's link layer address option
602 sll = ra[ICMPv6NDOptSrcLLAddr]
603 self.assertEqual(sll.lladdr, intf.local_mac)
606 # the RA should not contain prefix information
607 self.assertFalse(ra.haslayer(ICMPv6NDOptPrefixInfo))
609 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
611 # the options are nested in the scapy packet in way that i cannot
612 # decipher how to decode. this 1st layer of option always returns
613 # nested classes, so a direct obj1=obj2 comparison always fails.
614 # however, the getlayer(.., 2) does give one instnace.
615 # so we cheat here and construct a new opt instnace for comparison
616 rd = ICMPv6NDOptPrefixInfo(prefixlen=raos.prefixlen,
620 if type(pi_opt) is list:
621 for ii in range(len(pi_opt)):
622 self.assertEqual(pi_opt[ii], rd)
623 rd = rx.getlayer(ICMPv6NDOptPrefixInfo, ii+2)
625 self.assertEqual(pi_opt, raos)
627 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
628 filter_out_fn=is_ipv6_misc,
630 intf.add_stream(pkts)
631 self.pg_enable_capture(self.pg_interfaces)
633 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
635 self.assertEqual(len(rx), 1)
637 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
640 """ IPv6 Router Solicitation Exceptions
646 # Before we begin change the IPv6 RA responses to use the unicast
647 # address - that way we will not confuse them with the periodic
648 # RAs which go to the mcast address
649 # Sit and wait for the first periodic RA.
653 self.pg0.ip6_ra_config(send_unicast=1)
656 # An RS from a link source address
657 # - expect an RA in return
659 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
660 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
663 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
666 # For the next RS sent the RA should be rate limited
668 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
671 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
672 # so we need to do this before each test below so as not to drop
673 # packets for rate limiting reasons. Test this works here.
675 self.pg0.ip6_ra_config(send_unicast=1)
676 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
679 # An RS sent from a non-link local source
681 self.pg0.ip6_ra_config(send_unicast=1)
682 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
683 IPv6(dst=self.pg0.local_ip6, src="2002::ffff") /
686 self.send_and_assert_no_replies(self.pg0, pkts,
687 "RS from non-link source")
690 # Source an RS from a link local address
692 self.pg0.ip6_ra_config(send_unicast=1)
693 ll = mk_ll_addr(self.pg0.remote_mac)
694 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
695 IPv6(dst=self.pg0.local_ip6, src=ll) /
698 self.send_and_expect_ra(self.pg0, pkts,
699 "RS sourced from link-local",
703 # Send the RS multicast
705 self.pg0.ip6_ra_config(send_unicast=1)
706 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
707 ll = mk_ll_addr(self.pg0.remote_mac)
708 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
709 IPv6(dst="ff02::2", src=ll) /
712 self.send_and_expect_ra(self.pg0, pkts,
713 "RS sourced from link-local",
717 # Source from the unspecified address ::. This happens when the RS
718 # is sent before the host has a configured address/sub-net,
719 # i.e. auto-config. Since the sender has no IP address, the reply
720 # comes back mcast - so the capture needs to not filter this.
721 # If we happen to pick up the periodic RA at this point then so be it,
724 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
725 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
726 IPv6(dst="ff02::2", src="::") /
729 self.send_and_expect_ra(self.pg0, pkts,
730 "RS sourced from unspecified",
735 # Configure The RA to announce the links prefix
737 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
738 self.pg0.local_ip6_prefix_len)
741 # RAs should now contain the prefix information option
743 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
744 prefix=self.pg0.local_ip6,
748 self.pg0.ip6_ra_config(send_unicast=1)
749 ll = mk_ll_addr(self.pg0.remote_mac)
750 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
751 IPv6(dst=self.pg0.local_ip6, src=ll) /
753 self.send_and_expect_ra(self.pg0, p,
754 "RA with prefix-info",
759 # Change the prefix info to not off-link
762 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
763 self.pg0.local_ip6_prefix_len,
766 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
767 prefix=self.pg0.local_ip6,
771 self.pg0.ip6_ra_config(send_unicast=1)
772 self.send_and_expect_ra(self.pg0, p,
773 "RA with Prefix info with L-flag=0",
778 # Change the prefix info to not off-link, no-autoconfig
779 # L and A flag are clear in the advert
781 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
782 self.pg0.local_ip6_prefix_len,
786 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
787 prefix=self.pg0.local_ip6,
791 self.pg0.ip6_ra_config(send_unicast=1)
792 self.send_and_expect_ra(self.pg0, p,
793 "RA with Prefix info with A & L-flag=0",
798 # Change the flag settings back to the defaults
799 # L and A flag are set in the advert
801 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
802 self.pg0.local_ip6_prefix_len)
804 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
805 prefix=self.pg0.local_ip6,
809 self.pg0.ip6_ra_config(send_unicast=1)
810 self.send_and_expect_ra(self.pg0, p,
811 "RA with Prefix info",
816 # Change the prefix info to not off-link, no-autoconfig
817 # L and A flag are clear in the advert
819 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
820 self.pg0.local_ip6_prefix_len,
824 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
825 prefix=self.pg0.local_ip6,
829 self.pg0.ip6_ra_config(send_unicast=1)
830 self.send_and_expect_ra(self.pg0, p,
831 "RA with Prefix info with A & L-flag=0",
836 # Use the reset to defults option to revert to defaults
837 # L and A flag are clear in the advert
839 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
840 self.pg0.local_ip6_prefix_len,
843 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
844 prefix=self.pg0.local_ip6,
848 self.pg0.ip6_ra_config(send_unicast=1)
849 self.send_and_expect_ra(self.pg0, p,
850 "RA with Prefix reverted to defaults",
855 # Advertise Another prefix. With no L-flag/A-flag
857 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
858 self.pg1.local_ip6_prefix_len,
862 opt = [ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
863 prefix=self.pg0.local_ip6,
866 ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
867 prefix=self.pg1.local_ip6,
871 self.pg0.ip6_ra_config(send_unicast=1)
872 ll = mk_ll_addr(self.pg0.remote_mac)
873 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
874 IPv6(dst=self.pg0.local_ip6, src=ll) /
876 self.send_and_expect_ra(self.pg0, p,
877 "RA with multiple Prefix infos",
882 # Remove the first refix-info - expect the second is still in the
885 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
886 self.pg0.local_ip6_prefix_len,
889 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
890 prefix=self.pg1.local_ip6,
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",
901 # Remove the second prefix-info - expect no prefix-info i nthe adverts
903 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
904 self.pg1.local_ip6_prefix_len,
907 self.pg0.ip6_ra_config(send_unicast=1)
908 self.send_and_expect_ra(self.pg0, p,
909 "RA with Prefix reverted to defaults",
913 # Reset the periodic advertisements back to default values
915 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
918 class TestICMPv6Echo(VppTestCase):
919 """ ICMPv6 Echo Test Case """
922 super(TestICMPv6Echo, self).setUp()
924 # create 1 pg interface
925 self.create_pg_interfaces(range(1))
927 for i in self.pg_interfaces:
933 super(TestICMPv6Echo, self).tearDown()
934 for i in self.pg_interfaces:
939 def test_icmpv6_echo(self):
940 """ VPP replies to ICMPv6 Echo Request
944 - Receive ICMPv6 Echo Request message on pg0 interface.
945 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
950 icmpv6_data = '\x0a' * 18
951 p_echo_request = (Ether(src=self.pg0.remote_mac,
952 dst=self.pg0.local_mac) /
953 IPv6(src=self.pg0.remote_ip6,
954 dst=self.pg0.local_ip6) /
955 ICMPv6EchoRequest(id=icmpv6_id, seq=icmpv6_seq,
958 self.pg0.add_stream(p_echo_request)
959 self.pg_enable_capture(self.pg_interfaces)
962 rx = self.pg0.get_capture(1)
966 icmpv6 = rx[ICMPv6EchoReply]
968 self.assertEqual(ether.src, self.pg0.local_mac)
969 self.assertEqual(ether.dst, self.pg0.remote_mac)
971 self.assertEqual(ipv6.src, self.pg0.local_ip6)
972 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
974 self.assertEqual(icmp6types[icmpv6.type], "Echo Reply")
975 self.assertEqual(icmpv6.id, icmpv6_id)
976 self.assertEqual(icmpv6.seq, icmpv6_seq)
977 self.assertEqual(icmpv6.data, icmpv6_data)
980 class TestIPv6RD(TestIPv6ND):
981 """ IPv6 Router Discovery Test Case """
985 super(TestIPv6RD, cls).setUpClass()
988 super(TestIPv6RD, self).setUp()
990 # create 2 pg interfaces
991 self.create_pg_interfaces(range(2))
993 self.interfaces = list(self.pg_interfaces)
995 # setup all interfaces
996 for i in self.interfaces:
1001 for i in self.interfaces:
1004 super(TestIPv6RD, self).tearDown()
1006 def test_rd_send_router_solicitation(self):
1007 """ Verify router solicitation packets """
1010 self.pg_enable_capture(self.pg_interfaces)
1012 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1014 rx_list = self.pg1.get_capture(count, timeout=3)
1015 self.assertEqual(len(rx_list), count)
1016 for packet in rx_list:
1017 self.assertTrue(packet.haslayer(IPv6))
1018 self.assertTrue(packet[IPv6].haslayer(ICMPv6ND_RS))
1019 dst = ip6_normalize(packet[IPv6].dst)
1020 dst2 = ip6_normalize("ff02::2")
1021 self.assert_equal(dst, dst2)
1022 src = ip6_normalize(packet[IPv6].src)
1023 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1024 self.assert_equal(src, src2)
1025 self.assertTrue(packet[ICMPv6ND_RS].haslayer(ICMPv6NDOptSrcLLAddr))
1026 self.assert_equal(packet[ICMPv6NDOptSrcLLAddr].lladdr,
1029 def verify_prefix_info(self, reported_prefix, prefix_option):
1030 prefix = socket.inet_pton(socket.AF_INET6,
1031 prefix_option.getfieldval("prefix"))
1032 self.assert_equal(reported_prefix.dst_address, prefix)
1033 self.assert_equal(reported_prefix.dst_address_length,
1034 prefix_option.getfieldval("prefixlen"))
1035 L = prefix_option.getfieldval("L")
1036 A = prefix_option.getfieldval("A")
1037 option_flags = (L << 7) | (A << 6)
1038 self.assert_equal(reported_prefix.flags, option_flags)
1039 self.assert_equal(reported_prefix.valid_time,
1040 prefix_option.getfieldval("validlifetime"))
1041 self.assert_equal(reported_prefix.preferred_time,
1042 prefix_option.getfieldval("preferredlifetime"))
1044 def test_rd_receive_router_advertisement(self):
1045 """ Verify events triggered by received RA packets """
1047 self.vapi.want_ip6_ra_events()
1049 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1053 preferredlifetime=500,
1058 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1062 preferredlifetime=1000,
1067 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1068 IPv6(dst=self.pg1.local_ip6_ll,
1069 src=mk_ll_addr(self.pg1.remote_mac)) /
1073 self.pg1.add_stream([p])
1076 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1078 self.assert_equal(ev.current_hop_limit, 0)
1079 self.assert_equal(ev.flags, 8)
1080 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1081 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1083 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1085 self.assert_equal(ev.n_prefixes, 2)
1087 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1088 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1091 class TestIPv6RDControlPlane(TestIPv6ND):
1092 """ IPv6 Router Discovery Control Plane Test Case """
1095 def setUpClass(cls):
1096 super(TestIPv6RDControlPlane, cls).setUpClass()
1099 super(TestIPv6RDControlPlane, self).setUp()
1101 # create 1 pg interface
1102 self.create_pg_interfaces(range(1))
1104 self.interfaces = list(self.pg_interfaces)
1106 # setup all interfaces
1107 for i in self.interfaces:
1112 super(TestIPv6RDControlPlane, self).tearDown()
1115 def create_ra_packet(pg, routerlifetime=None):
1116 src_ip = pg.remote_ip6_ll
1117 dst_ip = pg.local_ip6
1118 if routerlifetime is not None:
1119 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1122 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1123 IPv6(dst=dst_ip, src=src_ip) / ra)
1127 def get_default_routes(fib):
1130 if entry.address_length == 0:
1131 for path in entry.path:
1132 if path.sw_if_index != 0xFFFFFFFF:
1134 defaut_route['sw_if_index'] = path.sw_if_index
1135 defaut_route['next_hop'] = path.next_hop
1136 list.append(defaut_route)
1140 def get_interface_addresses(fib, pg):
1143 if entry.address_length == 128:
1144 path = entry.path[0]
1145 if path.sw_if_index == pg.sw_if_index:
1146 list.append(entry.address)
1150 """ Test handling of SLAAC addresses and default routes """
1152 fib = self.vapi.ip6_fib_dump()
1153 default_routes = self.get_default_routes(fib)
1154 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1155 self.assertEqual(default_routes, [])
1156 router_address = self.pg0.remote_ip6n_ll
1158 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1163 packet = (self.create_ra_packet(self.pg0) / ICMPv6NDOptPrefixInfo(
1167 preferredlifetime=2,
1170 ) / ICMPv6NDOptPrefixInfo(
1174 preferredlifetime=1000,
1178 self.pg0.add_stream([packet])
1183 fib = self.vapi.ip6_fib_dump()
1185 # check FIB for new address
1186 addresses = set(self.get_interface_addresses(fib, self.pg0))
1187 new_addresses = addresses.difference(initial_addresses)
1188 self.assertEqual(len(new_addresses), 1)
1189 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1190 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1192 # check FIB for new default route
1193 default_routes = self.get_default_routes(fib)
1194 self.assertEqual(len(default_routes), 1)
1195 dr = default_routes[0]
1196 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1197 self.assertEqual(dr['next_hop'], router_address)
1199 # send RA to delete default route
1200 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1201 self.pg0.add_stream([packet])
1206 # check that default route is deleted
1207 fib = self.vapi.ip6_fib_dump()
1208 default_routes = self.get_default_routes(fib)
1209 self.assertEqual(len(default_routes), 0)
1214 packet = self.create_ra_packet(self.pg0)
1215 self.pg0.add_stream([packet])
1220 # check FIB for new default route
1221 fib = self.vapi.ip6_fib_dump()
1222 default_routes = self.get_default_routes(fib)
1223 self.assertEqual(len(default_routes), 1)
1224 dr = default_routes[0]
1225 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1226 self.assertEqual(dr['next_hop'], router_address)
1228 # send RA, updating router lifetime to 1s
1229 packet = self.create_ra_packet(self.pg0, 1)
1230 self.pg0.add_stream([packet])
1235 # check that default route still exists
1236 fib = self.vapi.ip6_fib_dump()
1237 default_routes = self.get_default_routes(fib)
1238 self.assertEqual(len(default_routes), 1)
1239 dr = default_routes[0]
1240 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1241 self.assertEqual(dr['next_hop'], router_address)
1245 # check that default route is deleted
1246 fib = self.vapi.ip6_fib_dump()
1247 default_routes = self.get_default_routes(fib)
1248 self.assertEqual(len(default_routes), 0)
1250 # check FIB still contains the SLAAC address
1251 addresses = set(self.get_interface_addresses(fib, self.pg0))
1252 new_addresses = addresses.difference(initial_addresses)
1253 self.assertEqual(len(new_addresses), 1)
1254 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1255 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1259 # check that SLAAC address is deleted
1260 fib = self.vapi.ip6_fib_dump()
1261 addresses = set(self.get_interface_addresses(fib, self.pg0))
1262 new_addresses = addresses.difference(initial_addresses)
1263 self.assertEqual(len(new_addresses), 0)
1266 class IPv6NDProxyTest(TestIPv6ND):
1267 """ IPv6 ND ProxyTest Case """
1270 super(IPv6NDProxyTest, self).setUp()
1272 # create 3 pg interfaces
1273 self.create_pg_interfaces(range(3))
1275 # pg0 is the master interface, with the configured subnet
1277 self.pg0.config_ip6()
1278 self.pg0.resolve_ndp()
1280 self.pg1.ip6_enable()
1281 self.pg2.ip6_enable()
1284 super(IPv6NDProxyTest, self).tearDown()
1286 def test_nd_proxy(self):
1287 """ IPv6 Proxy ND """
1290 # Generate some hosts in the subnet that we are proxying
1292 self.pg0.generate_remote_hosts(8)
1294 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1295 d = inet_ntop(AF_INET6, nsma)
1298 # Send an NS for one of those remote hosts on one of the proxy links
1299 # expect no response since it's from an address that is not
1300 # on the link that has the prefix configured
1302 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
1303 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6) /
1304 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1305 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
1307 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1310 # Add proxy support for the host
1312 self.vapi.ip6_nd_proxy(
1313 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1314 self.pg1.sw_if_index)
1317 # try that NS again. this time we expect an NA back
1319 self.send_and_expect_na(self.pg1, ns_pg1,
1320 "NS to proxy entry",
1321 dst_ip=self.pg0._remote_hosts[2].ip6,
1322 tgt_ip=self.pg0.local_ip6)
1325 # ... and that we have an entry in the ND cache
1327 self.assertTrue(find_nbr(self,
1328 self.pg1.sw_if_index,
1329 self.pg0._remote_hosts[2].ip6,
1333 # ... and we can route traffic to it
1335 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1336 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1337 src=self.pg0.remote_ip6) /
1338 UDP(sport=10000, dport=20000) /
1341 self.pg0.add_stream(t)
1342 self.pg_enable_capture(self.pg_interfaces)
1344 rx = self.pg1.get_capture(1)
1347 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1348 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1350 self.assertEqual(rx[IPv6].src, t[IPv6].src)
1351 self.assertEqual(rx[IPv6].dst, t[IPv6].dst)
1354 # Test we proxy for the host on the main interface
1356 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1357 IPv6(dst=d, src=self.pg0.remote_ip6) /
1358 ICMPv6ND_NS(tgt=self.pg0._remote_hosts[2].ip6) /
1359 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
1361 self.send_and_expect_na(self.pg0, ns_pg0,
1362 "NS to proxy entry on main",
1363 tgt_ip=self.pg0._remote_hosts[2].ip6,
1364 dst_ip=self.pg0.remote_ip6)
1367 # Setup and resolve proxy for another host on another interface
1369 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1370 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6) /
1371 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1372 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
1374 self.vapi.ip6_nd_proxy(
1375 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1376 self.pg2.sw_if_index)
1378 self.send_and_expect_na(self.pg2, ns_pg2,
1379 "NS to proxy entry other interface",
1380 dst_ip=self.pg0._remote_hosts[3].ip6,
1381 tgt_ip=self.pg0.local_ip6)
1383 self.assertTrue(find_nbr(self,
1384 self.pg2.sw_if_index,
1385 self.pg0._remote_hosts[3].ip6,
1389 # hosts can communicate. pg2->pg1
1391 t2 = (Ether(dst=self.pg2.local_mac,
1392 src=self.pg0.remote_hosts[3].mac) /
1393 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1394 src=self.pg0._remote_hosts[3].ip6) /
1395 UDP(sport=10000, dport=20000) /
1398 self.pg2.add_stream(t2)
1399 self.pg_enable_capture(self.pg_interfaces)
1401 rx = self.pg1.get_capture(1)
1404 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1405 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1407 self.assertEqual(rx[IPv6].src, t2[IPv6].src)
1408 self.assertEqual(rx[IPv6].dst, t2[IPv6].dst)
1411 # remove the proxy configs
1413 self.vapi.ip6_nd_proxy(
1414 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1415 self.pg1.sw_if_index,
1417 self.vapi.ip6_nd_proxy(
1418 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1419 self.pg2.sw_if_index,
1422 self.assertFalse(find_nbr(self,
1423 self.pg2.sw_if_index,
1424 self.pg0._remote_hosts[3].ip6,
1426 self.assertFalse(find_nbr(self,
1427 self.pg1.sw_if_index,
1428 self.pg0._remote_hosts[2].ip6,
1432 # no longer proxy-ing...
1434 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1435 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1436 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1439 # no longer forwarding. traffic generates NS out of the glean/main
1442 self.pg2.add_stream(t2)
1443 self.pg_enable_capture(self.pg_interfaces)
1446 rx = self.pg0.get_capture(1)
1448 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1451 class TestIPNull(VppTestCase):
1452 """ IPv6 routes via NULL """
1455 super(TestIPNull, self).setUp()
1457 # create 2 pg interfaces
1458 self.create_pg_interfaces(range(1))
1460 for i in self.pg_interfaces:
1466 super(TestIPNull, self).tearDown()
1467 for i in self.pg_interfaces:
1471 def test_ip_null(self):
1472 """ IP NULL route """
1474 p = (Ether(src=self.pg0.remote_mac,
1475 dst=self.pg0.local_mac) /
1476 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1477 UDP(sport=1234, dport=1234) /
1481 # A route via IP NULL that will reply with ICMP unreachables
1483 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1484 ip_unreach.add_vpp_config()
1486 self.pg0.add_stream(p)
1487 self.pg_enable_capture(self.pg_interfaces)
1490 rx = self.pg0.get_capture(1)
1492 icmp = rx[ICMPv6DestUnreach]
1494 # 0 = "No route to destination"
1495 self.assertEqual(icmp.code, 0)
1497 # ICMP is rate limited. pause a bit
1501 # A route via IP NULL that will reply with ICMP prohibited
1503 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1504 is_prohibit=1, is_ip6=1)
1505 ip_prohibit.add_vpp_config()
1507 self.pg0.add_stream(p)
1508 self.pg_enable_capture(self.pg_interfaces)
1511 rx = self.pg0.get_capture(1)
1513 icmp = rx[ICMPv6DestUnreach]
1515 # 1 = "Communication with destination administratively prohibited"
1516 self.assertEqual(icmp.code, 1)
1519 class TestIPDisabled(VppTestCase):
1520 """ IPv6 disabled """
1523 super(TestIPDisabled, self).setUp()
1525 # create 2 pg interfaces
1526 self.create_pg_interfaces(range(2))
1530 self.pg0.config_ip6()
1531 self.pg0.resolve_ndp()
1533 # PG 1 is not IP enabled
1537 super(TestIPDisabled, self).tearDown()
1538 for i in self.pg_interfaces:
1542 def test_ip_disabled(self):
1547 # one accepting interface, pg0, 2 forwarding interfaces
1549 route_ff_01 = VppIpMRoute(
1553 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1554 [VppMRoutePath(self.pg1.sw_if_index,
1555 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1556 VppMRoutePath(self.pg0.sw_if_index,
1557 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1559 route_ff_01.add_vpp_config()
1561 pu = (Ether(src=self.pg1.remote_mac,
1562 dst=self.pg1.local_mac) /
1563 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1564 UDP(sport=1234, dport=1234) /
1566 pm = (Ether(src=self.pg1.remote_mac,
1567 dst=self.pg1.local_mac) /
1568 IPv6(src="2001::1", dst="ffef::1") /
1569 UDP(sport=1234, dport=1234) /
1573 # PG1 does not forward IP traffic
1575 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1576 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1581 self.pg1.config_ip6()
1584 # Now we get packets through
1586 self.pg1.add_stream(pu)
1587 self.pg_enable_capture(self.pg_interfaces)
1589 rx = self.pg0.get_capture(1)
1591 self.pg1.add_stream(pm)
1592 self.pg_enable_capture(self.pg_interfaces)
1594 rx = self.pg0.get_capture(1)
1599 self.pg1.unconfig_ip6()
1602 # PG1 does not forward IP traffic
1604 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1605 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1608 class TestIP6LoadBalance(VppTestCase):
1609 """ IPv6 Load-Balancing """
1612 super(TestIP6LoadBalance, self).setUp()
1614 self.create_pg_interfaces(range(5))
1616 mpls_tbl = VppMplsTable(self, 0)
1617 mpls_tbl.add_vpp_config()
1619 for i in self.pg_interfaces:
1626 for i in self.pg_interfaces:
1630 super(TestIP6LoadBalance, self).tearDown()
1632 def send_and_expect_load_balancing(self, input, pkts, outputs):
1633 self.vapi.cli("clear trace")
1634 input.add_stream(pkts)
1635 self.pg_enable_capture(self.pg_interfaces)
1638 rx = oo._get_capture(1)
1639 self.assertNotEqual(0, len(rx))
1641 def send_and_expect_one_itf(self, input, pkts, itf):
1642 self.vapi.cli("clear trace")
1643 input.add_stream(pkts)
1644 self.pg_enable_capture(self.pg_interfaces)
1646 rx = itf.get_capture(len(pkts))
1648 def test_ip6_load_balance(self):
1649 """ IPv6 Load-Balancing """
1652 # An array of packets that differ only in the destination port
1656 # - MPLS non-EOS with an entropy label
1660 port_mpls_neos_pkts = []
1664 # An array of packets that differ only in the source address
1669 for ii in range(65):
1670 port_ip_hdr = (IPv6(dst="3000::1", src="3000:1::1") /
1671 UDP(sport=1234, dport=1234 + ii) /
1673 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1674 dst=self.pg0.local_mac) /
1676 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1677 dst=self.pg0.local_mac) /
1678 MPLS(label=66, ttl=2) /
1680 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1681 dst=self.pg0.local_mac) /
1682 MPLS(label=67, ttl=2) /
1683 MPLS(label=77, ttl=2) /
1685 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1686 dst=self.pg0.local_mac) /
1687 MPLS(label=67, ttl=2) /
1688 MPLS(label=14, ttl=2) /
1689 MPLS(label=999, ttl=2) /
1691 src_ip_hdr = (IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1692 UDP(sport=1234, dport=1234) /
1694 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1695 dst=self.pg0.local_mac) /
1697 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1698 dst=self.pg0.local_mac) /
1699 MPLS(label=66, ttl=2) /
1703 # A route for the IP pacekts
1705 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1706 [VppRoutePath(self.pg1.remote_ip6,
1707 self.pg1.sw_if_index,
1708 proto=DpoProto.DPO_PROTO_IP6),
1709 VppRoutePath(self.pg2.remote_ip6,
1710 self.pg2.sw_if_index,
1711 proto=DpoProto.DPO_PROTO_IP6)],
1713 route_3000_1.add_vpp_config()
1716 # a local-label for the EOS packets
1718 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1719 binding.add_vpp_config()
1722 # An MPLS route for the non-EOS packets
1724 route_67 = VppMplsRoute(self, 67, 0,
1725 [VppRoutePath(self.pg1.remote_ip6,
1726 self.pg1.sw_if_index,
1728 proto=DpoProto.DPO_PROTO_IP6),
1729 VppRoutePath(self.pg2.remote_ip6,
1730 self.pg2.sw_if_index,
1732 proto=DpoProto.DPO_PROTO_IP6)])
1733 route_67.add_vpp_config()
1736 # inject the packet on pg0 - expect load-balancing across the 2 paths
1737 # - since the default hash config is to use IP src,dst and port
1739 # We are not going to ensure equal amounts of packets across each link,
1740 # since the hash algorithm is statistical and therefore this can never
1741 # be guaranteed. But wuth 64 different packets we do expect some
1742 # balancing. So instead just ensure there is traffic on each link.
1744 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1745 [self.pg1, self.pg2])
1746 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1747 [self.pg1, self.pg2])
1748 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1749 [self.pg1, self.pg2])
1750 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1751 [self.pg1, self.pg2])
1752 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1753 [self.pg1, self.pg2])
1756 # The packets with Entropy label in should not load-balance,
1757 # since the Entorpy value is fixed.
1759 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1762 # change the flow hash config so it's only IP src,dst
1763 # - now only the stream with differing source address will
1766 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1768 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1769 [self.pg1, self.pg2])
1770 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1771 [self.pg1, self.pg2])
1772 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1775 # change the flow hash config back to defaults
1777 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1780 # Recursive prefixes
1781 # - testing that 2 stages of load-balancing occurs and there is no
1782 # polarisation (i.e. only 2 of 4 paths are used)
1787 for ii in range(257):
1788 port_pkts.append((Ether(src=self.pg0.remote_mac,
1789 dst=self.pg0.local_mac) /
1790 IPv6(dst="4000::1", src="4000:1::1") /
1791 UDP(sport=1234, dport=1234 + ii) /
1793 src_pkts.append((Ether(src=self.pg0.remote_mac,
1794 dst=self.pg0.local_mac) /
1795 IPv6(dst="4000::1", src="4000:1::%d" % ii) /
1796 UDP(sport=1234, dport=1234) /
1799 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1800 [VppRoutePath(self.pg3.remote_ip6,
1801 self.pg3.sw_if_index,
1802 proto=DpoProto.DPO_PROTO_IP6),
1803 VppRoutePath(self.pg4.remote_ip6,
1804 self.pg4.sw_if_index,
1805 proto=DpoProto.DPO_PROTO_IP6)],
1807 route_3000_2.add_vpp_config()
1809 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1810 [VppRoutePath("3000::1",
1812 proto=DpoProto.DPO_PROTO_IP6),
1813 VppRoutePath("3000::2",
1815 proto=DpoProto.DPO_PROTO_IP6)],
1817 route_4000_1.add_vpp_config()
1820 # inject the packet on pg0 - expect load-balancing across all 4 paths
1822 self.vapi.cli("clear trace")
1823 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1824 [self.pg1, self.pg2,
1825 self.pg3, self.pg4])
1826 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1827 [self.pg1, self.pg2,
1828 self.pg3, self.pg4])
1831 # Recursive prefixes
1832 # - testing that 2 stages of load-balancing no choices
1836 for ii in range(257):
1837 port_pkts.append((Ether(src=self.pg0.remote_mac,
1838 dst=self.pg0.local_mac) /
1839 IPv6(dst="6000::1", src="6000:1::1") /
1840 UDP(sport=1234, dport=1234 + ii) /
1843 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1844 [VppRoutePath(self.pg3.remote_ip6,
1845 self.pg3.sw_if_index,
1846 proto=DpoProto.DPO_PROTO_IP6)],
1848 route_5000_2.add_vpp_config()
1850 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1851 [VppRoutePath("5000::2",
1853 proto=DpoProto.DPO_PROTO_IP6)],
1855 route_6000_1.add_vpp_config()
1858 # inject the packet on pg0 - expect load-balancing across all 4 paths
1860 self.vapi.cli("clear trace")
1861 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1864 class TestIP6Punt(VppTestCase):
1865 """ IPv6 Punt Police/Redirect """
1868 super(TestIP6Punt, self).setUp()
1870 self.create_pg_interfaces(range(2))
1872 for i in self.pg_interfaces:
1878 super(TestIP6Punt, self).tearDown()
1879 for i in self.pg_interfaces:
1883 def test_ip_punt(self):
1884 """ IP6 punt police and redirect """
1886 p = (Ether(src=self.pg0.remote_mac,
1887 dst=self.pg0.local_mac) /
1888 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1889 TCP(sport=1234, dport=1234) /
1895 # Configure a punt redirect via pg1.
1897 nh_addr = inet_pton(AF_INET6,
1898 self.pg1.remote_ip6)
1899 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1900 self.pg1.sw_if_index,
1904 self.send_and_expect(self.pg0, pkts, self.pg1)
1909 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1911 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1913 self.vapi.cli("clear trace")
1914 self.pg0.add_stream(pkts)
1915 self.pg_enable_capture(self.pg_interfaces)
1919 # the number of packet recieved should be greater than 0,
1920 # but not equal to the number sent, since some were policed
1922 rx = self.pg1._get_capture(1)
1923 self.assertGreater(len(rx), 0)
1924 self.assertLess(len(rx), len(pkts))
1927 # remove the poilcer. back to full rx
1929 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1930 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1931 rate_type=1, is_add=0)
1932 self.send_and_expect(self.pg0, pkts, self.pg1)
1935 # remove the redirect. expect full drop.
1937 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1938 self.pg1.sw_if_index,
1942 self.send_and_assert_no_replies(self.pg0, pkts,
1943 "IP no punt config")
1946 # Add a redirect that is not input port selective
1948 self.vapi.ip_punt_redirect(0xffffffff,
1949 self.pg1.sw_if_index,
1952 self.send_and_expect(self.pg0, pkts, self.pg1)
1954 self.vapi.ip_punt_redirect(0xffffffff,
1955 self.pg1.sw_if_index,
1961 class TestIPDeag(VppTestCase):
1962 """ IPv6 Deaggregate Routes """
1965 super(TestIPDeag, self).setUp()
1967 self.create_pg_interfaces(range(3))
1969 for i in self.pg_interfaces:
1975 super(TestIPDeag, self).tearDown()
1976 for i in self.pg_interfaces:
1980 def test_ip_deag(self):
1981 """ IP Deag Routes """
1984 # Create a table to be used for:
1985 # 1 - another destination address lookup
1986 # 2 - a source address lookup
1988 table_dst = VppIpTable(self, 1, is_ip6=1)
1989 table_src = VppIpTable(self, 2, is_ip6=1)
1990 table_dst.add_vpp_config()
1991 table_src.add_vpp_config()
1994 # Add a route in the default table to point to a deag/
1995 # second lookup in each of these tables
1997 route_to_dst = VppIpRoute(self, "1::1", 128,
2001 proto=DpoProto.DPO_PROTO_IP6)],
2003 route_to_src = VppIpRoute(self, "1::2", 128,
2008 proto=DpoProto.DPO_PROTO_IP6)],
2010 route_to_dst.add_vpp_config()
2011 route_to_src.add_vpp_config()
2014 # packets to these destination are dropped, since they'll
2015 # hit the respective default routes in the second table
2017 p_dst = (Ether(src=self.pg0.remote_mac,
2018 dst=self.pg0.local_mac) /
2019 IPv6(src="5::5", dst="1::1") /
2020 TCP(sport=1234, dport=1234) /
2022 p_src = (Ether(src=self.pg0.remote_mac,
2023 dst=self.pg0.local_mac) /
2024 IPv6(src="2::2", dst="1::2") /
2025 TCP(sport=1234, dport=1234) /
2027 pkts_dst = p_dst * 257
2028 pkts_src = p_src * 257
2030 self.send_and_assert_no_replies(self.pg0, pkts_dst,
2032 self.send_and_assert_no_replies(self.pg0, pkts_src,
2036 # add a route in the dst table to forward via pg1
2038 route_in_dst = VppIpRoute(self, "1::1", 128,
2039 [VppRoutePath(self.pg1.remote_ip6,
2040 self.pg1.sw_if_index,
2041 proto=DpoProto.DPO_PROTO_IP6)],
2044 route_in_dst.add_vpp_config()
2046 self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2049 # add a route in the src table to forward via pg2
2051 route_in_src = VppIpRoute(self, "2::2", 128,
2052 [VppRoutePath(self.pg2.remote_ip6,
2053 self.pg2.sw_if_index,
2054 proto=DpoProto.DPO_PROTO_IP6)],
2057 route_in_src.add_vpp_config()
2058 self.send_and_expect(self.pg0, pkts_src, self.pg2)
2061 # loop in the lookup DP
2063 route_loop = VppIpRoute(self, "3::3", 128,
2066 proto=DpoProto.DPO_PROTO_IP6)],
2068 route_loop.add_vpp_config()
2070 p_l = (Ether(src=self.pg0.remote_mac,
2071 dst=self.pg0.local_mac) /
2072 IPv6(src="3::4", dst="3::3") /
2073 TCP(sport=1234, dport=1234) /
2076 self.send_and_assert_no_replies(self.pg0, p_l * 257,
2080 class TestIP6Input(VppTestCase):
2081 """ IPv6 Input Exceptions """
2084 super(TestIP6Input, self).setUp()
2086 self.create_pg_interfaces(range(2))
2088 for i in self.pg_interfaces:
2094 super(TestIP6Input, self).tearDown()
2095 for i in self.pg_interfaces:
2099 def test_ip_input(self):
2100 """ IP6 Input Exceptions """
2103 # bad version - this is dropped
2105 p_version = (Ether(src=self.pg0.remote_mac,
2106 dst=self.pg0.local_mac) /
2107 IPv6(src=self.pg0.remote_ip6,
2108 dst=self.pg1.remote_ip6,
2110 UDP(sport=1234, dport=1234) /
2113 self.send_and_assert_no_replies(self.pg0, p_version * 65,
2117 # hop limit - IMCP replies
2119 p_version = (Ether(src=self.pg0.remote_mac,
2120 dst=self.pg0.local_mac) /
2121 IPv6(src=self.pg0.remote_ip6,
2122 dst=self.pg1.remote_ip6,
2124 UDP(sport=1234, dport=1234) /
2127 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2129 icmp = rx[ICMPv6TimeExceeded]
2130 self.assertEqual(icmp.type, 3)
2131 # 0: "hop limit exceeded in transit",
2132 self.assertEqual(icmp.code, 0)
2135 if __name__ == '__main__':
2136 unittest.main(testRunner=VppTestRunner)