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_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
11 VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
12 VppMplsRoute, DpoProto, VppMplsTable
13 from vpp_neighbor import find_nbr, VppNeighbor
15 from scapy.packet import Raw
16 from scapy.layers.l2 import Ether, Dot1Q
17 from scapy.layers.inet6 import IPv6, UDP, TCP, ICMPv6ND_NS, ICMPv6ND_RS, \
18 ICMPv6ND_RA, ICMPv6NDOptSrcLLAddr, getmacbyip6, ICMPv6MRD_Solicitation, \
19 ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
20 ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
21 ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply
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
28 AF_INET6 = socket.AF_INET6
32 euid = in6_mactoifaceid(mac)
33 addr = "fe80::" + euid
37 class TestIPv6ND(VppTestCase):
38 def validate_ra(self, intf, rx, dst_ip=None):
40 dst_ip = intf.remote_ip6
42 # unicasted packets must come to the unicast mac
43 self.assertEqual(rx[Ether].dst, intf.remote_mac)
45 # and from the router's MAC
46 self.assertEqual(rx[Ether].src, intf.local_mac)
48 # the rx'd RA should be addressed to the sender's source
49 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
50 self.assertEqual(in6_ptop(rx[IPv6].dst),
53 # and come from the router's link local
54 self.assertTrue(in6_islladdr(rx[IPv6].src))
55 self.assertEqual(in6_ptop(rx[IPv6].src),
56 in6_ptop(mk_ll_addr(intf.local_mac)))
58 def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
60 dst_ip = intf.remote_ip6
62 dst_ip = intf.local_ip6
64 # unicasted packets must come to the unicast mac
65 self.assertEqual(rx[Ether].dst, intf.remote_mac)
67 # and from the router's MAC
68 self.assertEqual(rx[Ether].src, intf.local_mac)
70 # the rx'd NA should be addressed to the sender's source
71 self.assertTrue(rx.haslayer(ICMPv6ND_NA))
72 self.assertEqual(in6_ptop(rx[IPv6].dst),
75 # and come from the target address
76 self.assertEqual(in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
78 # Dest link-layer options should have the router's MAC
79 dll = rx[ICMPv6NDOptDstLLAddr]
80 self.assertEqual(dll.lladdr, intf.local_mac)
82 def validate_ns(self, intf, rx, tgt_ip):
83 nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
84 dst_ip = inet_ntop(AF_INET6, nsma)
87 self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
89 # and from the router's MAC
90 self.assertEqual(rx[Ether].src, intf.local_mac)
92 # the rx'd NS should be addressed to an mcast address
93 # derived from the target address
94 self.assertEqual(in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
96 # expect the tgt IP in the NS header
98 self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
100 # packet is from the router's local address
101 self.assertEqual(in6_ptop(rx[IPv6].src), intf.local_ip6)
103 # Src link-layer options should have the router's MAC
104 sll = rx[ICMPv6NDOptSrcLLAddr]
105 self.assertEqual(sll.lladdr, intf.local_mac)
107 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
108 filter_out_fn=is_ipv6_misc):
109 intf.add_stream(pkts)
110 self.pg_enable_capture(self.pg_interfaces)
112 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
114 self.assertEqual(len(rx), 1)
116 self.validate_ra(intf, rx, dst_ip)
118 def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
120 filter_out_fn=is_ipv6_misc):
121 intf.add_stream(pkts)
122 self.pg_enable_capture(self.pg_interfaces)
124 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
126 self.assertEqual(len(rx), 1)
128 self.validate_na(intf, rx, dst_ip, tgt_ip)
130 def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
131 filter_out_fn=is_ipv6_misc):
132 tx_intf.add_stream(pkts)
133 self.pg_enable_capture(self.pg_interfaces)
135 rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
137 self.assertEqual(len(rx), 1)
139 self.validate_ns(rx_intf, rx, tgt_ip)
141 def verify_ip(self, rx, smac, dmac, sip, dip):
143 self.assertEqual(ether.dst, dmac)
144 self.assertEqual(ether.src, smac)
147 self.assertEqual(ip.src, sip)
148 self.assertEqual(ip.dst, dip)
151 class TestIPv6(TestIPv6ND):
152 """ IPv6 Test Case """
156 super(TestIPv6, cls).setUpClass()
160 Perform test setup before test case.
163 - create 3 pg interfaces
164 - untagged pg0 interface
165 - Dot1Q subinterface on pg1
166 - Dot1AD subinterface on pg2
168 - put it into UP state
170 - resolve neighbor address using NDP
171 - configure 200 fib entries
173 :ivar list interfaces: pg interfaces and subinterfaces.
174 :ivar dict flows: IPv4 packet flows in test.
176 *TODO:* Create AD sub interface
178 super(TestIPv6, self).setUp()
180 # create 3 pg interfaces
181 self.create_pg_interfaces(range(3))
183 # create 2 subinterfaces for p1 and pg2
184 self.sub_interfaces = [
185 VppDot1QSubint(self, self.pg1, 100),
186 VppDot1QSubint(self, self.pg2, 200)
187 # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
190 # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
192 self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
193 self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
194 self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
197 self.pg_if_packet_sizes = [64, 1500, 9020]
199 self.interfaces = list(self.pg_interfaces)
200 self.interfaces.extend(self.sub_interfaces)
202 # setup all interfaces
203 for i in self.interfaces:
208 # config 2M FIB entries
209 self.config_fib_entries(200)
212 """Run standard test teardown and log ``show ip6 neighbors``."""
213 for i in self.interfaces:
217 for i in self.sub_interfaces:
218 i.remove_vpp_config()
220 super(TestIPv6, self).tearDown()
221 if not self.vpp_dead:
222 self.logger.info(self.vapi.cli("show ip6 neighbors"))
223 # info(self.vapi.cli("show ip6 fib")) # many entries
225 def config_fib_entries(self, count):
226 """For each interface add to the FIB table *count* routes to
227 "fd02::1/128" destination with interface's local address as next-hop
230 :param int count: Number of FIB entries.
232 - *TODO:* check if the next-hop address shouldn't be remote address
233 instead of local address.
235 n_int = len(self.interfaces)
238 dest_addr = inet_pton(AF_INET6, "fd02::1")
240 for i in self.interfaces:
241 next_hop_address = i.local_ip6n
242 for j in range(count / n_int):
243 self.vapi.ip_add_del_route(
244 dest_addr, dest_addr_len, next_hop_address, is_ipv6=1)
246 if counter / count * 100 > percent:
247 self.logger.info("Configure %d FIB entries .. %d%% done" %
251 def modify_packet(self, src_if, packet_size, pkt):
252 """Add load, set destination IP and extend packet to required packet
253 size for defined interface.
255 :param VppInterface src_if: Interface to create packet for.
256 :param int packet_size: Required packet size.
257 :param Scapy pkt: Packet to be modified.
259 dst_if_idx = packet_size / 10 % 2
260 dst_if = self.flows[src_if][dst_if_idx]
261 info = self.create_packet_info(src_if, dst_if)
262 payload = self.info_to_payload(info)
264 p[IPv6].dst = dst_if.remote_ip6
266 if isinstance(src_if, VppSubInterface):
267 p = src_if.add_dot1_layer(p)
268 self.extend_packet(p, packet_size)
272 def create_stream(self, src_if):
273 """Create input packet stream for defined interface.
275 :param VppInterface src_if: Interface to create packet stream for.
277 hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
278 pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
279 IPv6(src=src_if.remote_ip6) /
280 UDP(sport=1234, dport=1234))
282 pkts = [self.modify_packet(src_if, i, pkt_tmpl)
283 for i in xrange(self.pg_if_packet_sizes[0],
284 self.pg_if_packet_sizes[1], 10)]
285 pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
286 for i in xrange(self.pg_if_packet_sizes[1] + hdr_ext,
287 self.pg_if_packet_sizes[2] + hdr_ext, 50)]
292 def verify_capture(self, dst_if, capture):
293 """Verify captured input packet stream for defined interface.
295 :param VppInterface dst_if: Interface to verify captured packet stream
297 :param list capture: Captured packet stream.
299 self.logger.info("Verifying capture on interface %s" % dst_if.name)
301 for i in self.interfaces:
302 last_info[i.sw_if_index] = None
304 dst_sw_if_index = dst_if.sw_if_index
305 if hasattr(dst_if, 'parent'):
307 for packet in capture:
309 # Check VLAN tags and Ethernet header
310 packet = dst_if.remove_dot1_layer(packet)
311 self.assertTrue(Dot1Q not in packet)
315 payload_info = self.payload_to_info(str(packet[Raw]))
316 packet_index = payload_info.index
317 self.assertEqual(payload_info.dst, dst_sw_if_index)
319 "Got packet on port %s: src=%u (id=%u)" %
320 (dst_if.name, payload_info.src, packet_index))
321 next_info = self.get_next_packet_info_for_interface2(
322 payload_info.src, dst_sw_if_index,
323 last_info[payload_info.src])
324 last_info[payload_info.src] = next_info
325 self.assertTrue(next_info is not None)
326 self.assertEqual(packet_index, next_info.index)
327 saved_packet = next_info.data
328 # Check standard fields
329 self.assertEqual(ip.src, saved_packet[IPv6].src)
330 self.assertEqual(ip.dst, saved_packet[IPv6].dst)
331 self.assertEqual(udp.sport, saved_packet[UDP].sport)
332 self.assertEqual(udp.dport, saved_packet[UDP].dport)
334 self.logger.error(ppp("Unexpected or invalid packet:", packet))
336 for i in self.interfaces:
337 remaining_packet = self.get_next_packet_info_for_interface2(
338 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
339 self.assertTrue(remaining_packet is None,
340 "Interface %s: Packet expected from interface %s "
341 "didn't arrive" % (dst_if.name, i.name))
347 - Create IPv6 stream for pg0 interface
348 - Create IPv6 tagged streams for pg1's and pg2's subinterface.
349 - Send and verify received packets on each interface.
352 pkts = self.create_stream(self.pg0)
353 self.pg0.add_stream(pkts)
355 for i in self.sub_interfaces:
356 pkts = self.create_stream(i)
357 i.parent.add_stream(pkts)
359 self.pg_enable_capture(self.pg_interfaces)
362 pkts = self.pg0.get_capture()
363 self.verify_capture(self.pg0, pkts)
365 for i in self.sub_interfaces:
366 pkts = i.parent.get_capture()
367 self.verify_capture(i, pkts)
370 """ IPv6 Neighbour Solicitation Exceptions
373 - Send an NS Sourced from an address not covered by the link sub-net
374 - Send an NS to an mcast address the router has not joined
375 - Send NS for a target address the router does not onn.
379 # An NS from a non link source address
381 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
382 d = inet_ntop(AF_INET6, nsma)
384 p = (Ether(dst=in6_getnsmac(nsma)) /
385 IPv6(dst=d, src="2002::2") /
386 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
387 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
390 self.send_and_assert_no_replies(
392 "No response to NS source by address not on sub-net")
395 # An NS for sent to a solicited mcast group the router is
396 # not a member of FAILS
399 nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
400 d = inet_ntop(AF_INET6, nsma)
402 p = (Ether(dst=in6_getnsmac(nsma)) /
403 IPv6(dst=d, src=self.pg0.remote_ip6) /
404 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
405 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
408 self.send_and_assert_no_replies(
410 "No response to NS sent to unjoined mcast address")
413 # An NS whose target address is one the router does not own
415 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
416 d = inet_ntop(AF_INET6, nsma)
418 p = (Ether(dst=in6_getnsmac(nsma)) /
419 IPv6(dst=d, src=self.pg0.remote_ip6) /
420 ICMPv6ND_NS(tgt="fd::ffff") /
421 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
424 self.send_and_assert_no_replies(self.pg0, pkts,
425 "No response to NS for unknown target")
428 # A neighbor entry that has no associated FIB-entry
430 self.pg0.generate_remote_hosts(4)
431 nd_entry = VppNeighbor(self,
432 self.pg0.sw_if_index,
433 self.pg0.remote_hosts[2].mac,
434 self.pg0.remote_hosts[2].ip6,
437 nd_entry.add_vpp_config()
440 # check we have the neighbor, but no route
442 self.assertTrue(find_nbr(self,
443 self.pg0.sw_if_index,
444 self.pg0._remote_hosts[2].ip6,
446 self.assertFalse(find_route(self,
447 self.pg0._remote_hosts[2].ip6,
452 # send an NS from a link local address to the interface's global
455 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
456 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
457 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
458 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
460 self.send_and_expect_na(self.pg0, p,
461 "NS from link-local",
462 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
463 tgt_ip=self.pg0.local_ip6)
466 # we should have learned an ND entry for the peer's link-local
467 # but not inserted a route to it in the FIB
469 self.assertTrue(find_nbr(self,
470 self.pg0.sw_if_index,
471 self.pg0._remote_hosts[2].ip6_ll,
473 self.assertFalse(find_route(self,
474 self.pg0._remote_hosts[2].ip6_ll,
479 # An NS to the router's own Link-local
481 p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
482 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
483 ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
484 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
486 self.send_and_expect_na(self.pg0, p,
487 "NS to/from link-local",
488 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
489 tgt_ip=self.pg0.local_ip6_ll)
492 # we should have learned an ND entry for the peer's link-local
493 # but not inserted a route to it in the FIB
495 self.assertTrue(find_nbr(self,
496 self.pg0.sw_if_index,
497 self.pg0._remote_hosts[3].ip6_ll,
499 self.assertFalse(find_route(self,
500 self.pg0._remote_hosts[3].ip6_ll,
504 def test_ns_duplicates(self):
508 # Generate some hosts on the LAN
510 self.pg1.generate_remote_hosts(3)
513 # Add host 1 on pg1 and pg2
515 ns_pg1 = VppNeighbor(self,
516 self.pg1.sw_if_index,
517 self.pg1.remote_hosts[1].mac,
518 self.pg1.remote_hosts[1].ip6,
520 ns_pg1.add_vpp_config()
521 ns_pg2 = VppNeighbor(self,
522 self.pg2.sw_if_index,
524 self.pg1.remote_hosts[1].ip6,
526 ns_pg2.add_vpp_config()
529 # IP packet destined for pg1 remote host arrives on pg1 again.
531 p = (Ether(dst=self.pg0.local_mac,
532 src=self.pg0.remote_mac) /
533 IPv6(src=self.pg0.remote_ip6,
534 dst=self.pg1.remote_hosts[1].ip6) /
535 UDP(sport=1234, dport=1234) /
538 self.pg0.add_stream(p)
539 self.pg_enable_capture(self.pg_interfaces)
542 rx1 = self.pg1.get_capture(1)
544 self.verify_ip(rx1[0],
546 self.pg1.remote_hosts[1].mac,
548 self.pg1.remote_hosts[1].ip6)
551 # remove the duplicate on pg1
552 # packet stream shoud generate NSs out of pg1
554 ns_pg1.remove_vpp_config()
556 self.send_and_expect_ns(self.pg0, self.pg1,
557 p, self.pg1.remote_hosts[1].ip6)
562 ns_pg1.add_vpp_config()
564 self.pg0.add_stream(p)
565 self.pg_enable_capture(self.pg_interfaces)
568 rx1 = self.pg1.get_capture(1)
570 self.verify_ip(rx1[0],
572 self.pg1.remote_hosts[1].mac,
574 self.pg1.remote_hosts[1].ip6)
576 def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
578 dst_ip = intf.remote_ip6
580 # unicasted packets must come to the unicast mac
581 self.assertEqual(rx[Ether].dst, intf.remote_mac)
583 # and from the router's MAC
584 self.assertEqual(rx[Ether].src, intf.local_mac)
586 # the rx'd RA should be addressed to the sender's source
587 self.assertTrue(rx.haslayer(ICMPv6ND_RA))
588 self.assertEqual(in6_ptop(rx[IPv6].dst),
591 # and come from the router's link local
592 self.assertTrue(in6_islladdr(rx[IPv6].src))
593 self.assertEqual(in6_ptop(rx[IPv6].src),
594 in6_ptop(mk_ll_addr(intf.local_mac)))
596 # it should contain the links MTU
598 self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
600 # it should contain the source's link layer address option
601 sll = ra[ICMPv6NDOptSrcLLAddr]
602 self.assertEqual(sll.lladdr, intf.local_mac)
605 # the RA should not contain prefix information
606 self.assertFalse(ra.haslayer(ICMPv6NDOptPrefixInfo))
608 raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
610 # the options are nested in the scapy packet in way that i cannot
611 # decipher how to decode. this 1st layer of option always returns
612 # nested classes, so a direct obj1=obj2 comparison always fails.
613 # however, the getlayer(.., 2) does give one instnace.
614 # so we cheat here and construct a new opt instnace for comparison
615 rd = ICMPv6NDOptPrefixInfo(prefixlen=raos.prefixlen,
619 if type(pi_opt) is list:
620 for ii in range(len(pi_opt)):
621 self.assertEqual(pi_opt[ii], rd)
622 rd = rx.getlayer(ICMPv6NDOptPrefixInfo, ii+2)
624 self.assertEqual(pi_opt, raos)
626 def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
627 filter_out_fn=is_ipv6_misc,
629 intf.add_stream(pkts)
630 self.pg_enable_capture(self.pg_interfaces)
632 rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
634 self.assertEqual(len(rx), 1)
636 self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
639 """ IPv6 Router Solicitation Exceptions
645 # Before we begin change the IPv6 RA responses to use the unicast
646 # address - that way we will not confuse them with the periodic
647 # RAs which go to the mcast address
648 # Sit and wait for the first periodic RA.
652 self.pg0.ip6_ra_config(send_unicast=1)
655 # An RS from a link source address
656 # - expect an RA in return
658 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
659 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
662 self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
665 # For the next RS sent the RA should be rate limited
667 self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
670 # When we reconfiure the IPv6 RA config, we reset the RA rate limiting,
671 # so we need to do this before each test below so as not to drop
672 # packets for rate limiting reasons. Test this works here.
674 self.pg0.ip6_ra_config(send_unicast=1)
675 self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
678 # An RS sent from a non-link local source
680 self.pg0.ip6_ra_config(send_unicast=1)
681 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
682 IPv6(dst=self.pg0.local_ip6, src="2002::ffff") /
685 self.send_and_assert_no_replies(self.pg0, pkts,
686 "RS from non-link source")
689 # Source an RS from a link local address
691 self.pg0.ip6_ra_config(send_unicast=1)
692 ll = mk_ll_addr(self.pg0.remote_mac)
693 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
694 IPv6(dst=self.pg0.local_ip6, src=ll) /
697 self.send_and_expect_ra(self.pg0, pkts,
698 "RS sourced from link-local",
702 # Send the RS multicast
704 self.pg0.ip6_ra_config(send_unicast=1)
705 dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
706 ll = mk_ll_addr(self.pg0.remote_mac)
707 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
708 IPv6(dst="ff02::2", src=ll) /
711 self.send_and_expect_ra(self.pg0, pkts,
712 "RS sourced from link-local",
716 # Source from the unspecified address ::. This happens when the RS
717 # is sent before the host has a configured address/sub-net,
718 # i.e. auto-config. Since the sender has no IP address, the reply
719 # comes back mcast - so the capture needs to not filter this.
720 # If we happen to pick up the periodic RA at this point then so be it,
723 self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
724 p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
725 IPv6(dst="ff02::2", src="::") /
728 self.send_and_expect_ra(self.pg0, pkts,
729 "RS sourced from unspecified",
734 # Configure The RA to announce the links prefix
736 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
737 self.pg0.local_ip6_prefix_len)
740 # RAs should now contain the prefix information option
742 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
743 prefix=self.pg0.local_ip6,
747 self.pg0.ip6_ra_config(send_unicast=1)
748 ll = mk_ll_addr(self.pg0.remote_mac)
749 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
750 IPv6(dst=self.pg0.local_ip6, src=ll) /
752 self.send_and_expect_ra(self.pg0, p,
753 "RA with prefix-info",
758 # Change the prefix info to not off-link
761 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
762 self.pg0.local_ip6_prefix_len,
765 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
766 prefix=self.pg0.local_ip6,
770 self.pg0.ip6_ra_config(send_unicast=1)
771 self.send_and_expect_ra(self.pg0, p,
772 "RA with Prefix info with L-flag=0",
777 # Change the prefix info to not off-link, no-autoconfig
778 # L and A flag are clear in the advert
780 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
781 self.pg0.local_ip6_prefix_len,
785 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
786 prefix=self.pg0.local_ip6,
790 self.pg0.ip6_ra_config(send_unicast=1)
791 self.send_and_expect_ra(self.pg0, p,
792 "RA with Prefix info with A & L-flag=0",
797 # Change the flag settings back to the defaults
798 # L and A flag are set in the advert
800 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
801 self.pg0.local_ip6_prefix_len)
803 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
804 prefix=self.pg0.local_ip6,
808 self.pg0.ip6_ra_config(send_unicast=1)
809 self.send_and_expect_ra(self.pg0, p,
810 "RA with Prefix info",
815 # Change the prefix info to not off-link, no-autoconfig
816 # L and A flag are clear in the advert
818 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
819 self.pg0.local_ip6_prefix_len,
823 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
824 prefix=self.pg0.local_ip6,
828 self.pg0.ip6_ra_config(send_unicast=1)
829 self.send_and_expect_ra(self.pg0, p,
830 "RA with Prefix info with A & L-flag=0",
835 # Use the reset to defults option to revert to defaults
836 # L and A flag are clear in the advert
838 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
839 self.pg0.local_ip6_prefix_len,
842 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
843 prefix=self.pg0.local_ip6,
847 self.pg0.ip6_ra_config(send_unicast=1)
848 self.send_and_expect_ra(self.pg0, p,
849 "RA with Prefix reverted to defaults",
854 # Advertise Another prefix. With no L-flag/A-flag
856 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
857 self.pg1.local_ip6_prefix_len,
861 opt = [ICMPv6NDOptPrefixInfo(prefixlen=self.pg0.local_ip6_prefix_len,
862 prefix=self.pg0.local_ip6,
865 ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
866 prefix=self.pg1.local_ip6,
870 self.pg0.ip6_ra_config(send_unicast=1)
871 ll = mk_ll_addr(self.pg0.remote_mac)
872 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
873 IPv6(dst=self.pg0.local_ip6, src=ll) /
875 self.send_and_expect_ra(self.pg0, p,
876 "RA with multiple Prefix infos",
881 # Remove the first refix-info - expect the second is still in the
884 self.pg0.ip6_ra_prefix(self.pg0.local_ip6n,
885 self.pg0.local_ip6_prefix_len,
888 opt = ICMPv6NDOptPrefixInfo(prefixlen=self.pg1.local_ip6_prefix_len,
889 prefix=self.pg1.local_ip6,
893 self.pg0.ip6_ra_config(send_unicast=1)
894 self.send_and_expect_ra(self.pg0, p,
895 "RA with Prefix reverted to defaults",
900 # Remove the second prefix-info - expect no prefix-info i nthe adverts
902 self.pg0.ip6_ra_prefix(self.pg1.local_ip6n,
903 self.pg1.local_ip6_prefix_len,
906 self.pg0.ip6_ra_config(send_unicast=1)
907 self.send_and_expect_ra(self.pg0, p,
908 "RA with Prefix reverted to defaults",
912 # Reset the periodic advertisements back to default values
914 self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
917 class TestICMPv6Echo(VppTestCase):
918 """ ICMPv6 Echo Test Case """
921 super(TestICMPv6Echo, self).setUp()
923 # create 1 pg interface
924 self.create_pg_interfaces(range(1))
926 for i in self.pg_interfaces:
932 super(TestICMPv6Echo, self).tearDown()
933 for i in self.pg_interfaces:
938 def test_icmpv6_echo(self):
939 """ VPP replies to ICMPv6 Echo Request
943 - Receive ICMPv6 Echo Request message on pg0 interface.
944 - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
949 icmpv6_data = '\x0a' * 18
950 p_echo_request = (Ether(src=self.pg0.remote_mac,
951 dst=self.pg0.local_mac) /
952 IPv6(src=self.pg0.remote_ip6,
953 dst=self.pg0.local_ip6) /
954 ICMPv6EchoRequest(id=icmpv6_id, seq=icmpv6_seq,
957 self.pg0.add_stream(p_echo_request)
958 self.pg_enable_capture(self.pg_interfaces)
961 rx = self.pg0.get_capture(1)
965 icmpv6 = rx[ICMPv6EchoReply]
967 self.assertEqual(ether.src, self.pg0.local_mac)
968 self.assertEqual(ether.dst, self.pg0.remote_mac)
970 self.assertEqual(ipv6.src, self.pg0.local_ip6)
971 self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
973 self.assertEqual(icmp6types[icmpv6.type], "Echo Reply")
974 self.assertEqual(icmpv6.id, icmpv6_id)
975 self.assertEqual(icmpv6.seq, icmpv6_seq)
976 self.assertEqual(icmpv6.data, icmpv6_data)
979 class TestIPv6RD(TestIPv6ND):
980 """ IPv6 Router Discovery Test Case """
984 super(TestIPv6RD, cls).setUpClass()
987 super(TestIPv6RD, self).setUp()
989 # create 2 pg interfaces
990 self.create_pg_interfaces(range(2))
992 self.interfaces = list(self.pg_interfaces)
994 # setup all interfaces
995 for i in self.interfaces:
1000 for i in self.interfaces:
1003 super(TestIPv6RD, self).tearDown()
1005 def test_rd_send_router_solicitation(self):
1006 """ Verify router solicitation packets """
1009 self.pg_enable_capture(self.pg_interfaces)
1011 self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1013 rx_list = self.pg1.get_capture(count, timeout=3)
1014 self.assertEqual(len(rx_list), count)
1015 for packet in rx_list:
1016 self.assertTrue(packet.haslayer(IPv6))
1017 self.assertTrue(packet[IPv6].haslayer(ICMPv6ND_RS))
1018 dst = ip6_normalize(packet[IPv6].dst)
1019 dst2 = ip6_normalize("ff02::2")
1020 self.assert_equal(dst, dst2)
1021 src = ip6_normalize(packet[IPv6].src)
1022 src2 = ip6_normalize(self.pg1.local_ip6_ll)
1023 self.assert_equal(src, src2)
1024 self.assertTrue(packet[ICMPv6ND_RS].haslayer(ICMPv6NDOptSrcLLAddr))
1025 self.assert_equal(packet[ICMPv6NDOptSrcLLAddr].lladdr,
1028 def verify_prefix_info(self, reported_prefix, prefix_option):
1029 prefix = socket.inet_pton(socket.AF_INET6,
1030 prefix_option.getfieldval("prefix"))
1031 self.assert_equal(reported_prefix.dst_address, prefix)
1032 self.assert_equal(reported_prefix.dst_address_length,
1033 prefix_option.getfieldval("prefixlen"))
1034 L = prefix_option.getfieldval("L")
1035 A = prefix_option.getfieldval("A")
1036 option_flags = (L << 7) | (A << 6)
1037 self.assert_equal(reported_prefix.flags, option_flags)
1038 self.assert_equal(reported_prefix.valid_time,
1039 prefix_option.getfieldval("validlifetime"))
1040 self.assert_equal(reported_prefix.preferred_time,
1041 prefix_option.getfieldval("preferredlifetime"))
1043 def test_rd_receive_router_advertisement(self):
1044 """ Verify events triggered by received RA packets """
1046 self.vapi.want_ip6_ra_events()
1048 prefix_info_1 = ICMPv6NDOptPrefixInfo(
1052 preferredlifetime=500,
1057 prefix_info_2 = ICMPv6NDOptPrefixInfo(
1061 preferredlifetime=1000,
1066 p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1067 IPv6(dst=self.pg1.local_ip6_ll,
1068 src=mk_ll_addr(self.pg1.remote_mac)) /
1072 self.pg1.add_stream([p])
1075 ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1077 self.assert_equal(ev.current_hop_limit, 0)
1078 self.assert_equal(ev.flags, 8)
1079 self.assert_equal(ev.router_lifetime_in_sec, 1800)
1080 self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1082 ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1084 self.assert_equal(ev.n_prefixes, 2)
1086 self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1087 self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1090 class TestIPv6RDControlPlane(TestIPv6ND):
1091 """ IPv6 Router Discovery Control Plane Test Case """
1094 def setUpClass(cls):
1095 super(TestIPv6RDControlPlane, cls).setUpClass()
1098 super(TestIPv6RDControlPlane, self).setUp()
1100 # create 1 pg interface
1101 self.create_pg_interfaces(range(1))
1103 self.interfaces = list(self.pg_interfaces)
1105 # setup all interfaces
1106 for i in self.interfaces:
1111 super(TestIPv6RDControlPlane, self).tearDown()
1114 def create_ra_packet(pg, routerlifetime=None):
1115 src_ip = pg.remote_ip6_ll
1116 dst_ip = pg.local_ip6
1117 if routerlifetime is not None:
1118 ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1121 p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1122 IPv6(dst=dst_ip, src=src_ip) / ra)
1126 def get_default_routes(fib):
1129 if entry.address_length == 0:
1130 for path in entry.path:
1131 if path.sw_if_index != 0xFFFFFFFF:
1133 defaut_route['sw_if_index'] = path.sw_if_index
1134 defaut_route['next_hop'] = path.next_hop
1135 list.append(defaut_route)
1139 def get_interface_addresses(fib, pg):
1142 if entry.address_length == 128:
1143 path = entry.path[0]
1144 if path.sw_if_index == pg.sw_if_index:
1145 list.append(entry.address)
1149 """ Test handling of SLAAC addresses and default routes """
1151 fib = self.vapi.ip6_fib_dump()
1152 default_routes = self.get_default_routes(fib)
1153 initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1154 self.assertEqual(default_routes, [])
1155 router_address = self.pg0.remote_ip6n_ll
1157 self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1162 packet = (self.create_ra_packet(self.pg0) / ICMPv6NDOptPrefixInfo(
1166 preferredlifetime=2,
1169 ) / ICMPv6NDOptPrefixInfo(
1173 preferredlifetime=1000,
1177 self.pg0.add_stream([packet])
1182 fib = self.vapi.ip6_fib_dump()
1184 # check FIB for new address
1185 addresses = set(self.get_interface_addresses(fib, self.pg0))
1186 new_addresses = addresses.difference(initial_addresses)
1187 self.assertEqual(len(new_addresses), 1)
1188 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1189 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1191 # check FIB for new default route
1192 default_routes = self.get_default_routes(fib)
1193 self.assertEqual(len(default_routes), 1)
1194 dr = default_routes[0]
1195 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1196 self.assertEqual(dr['next_hop'], router_address)
1198 # send RA to delete default route
1199 packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1200 self.pg0.add_stream([packet])
1205 # check that default route is deleted
1206 fib = self.vapi.ip6_fib_dump()
1207 default_routes = self.get_default_routes(fib)
1208 self.assertEqual(len(default_routes), 0)
1213 packet = self.create_ra_packet(self.pg0)
1214 self.pg0.add_stream([packet])
1219 # check FIB for new default route
1220 fib = self.vapi.ip6_fib_dump()
1221 default_routes = self.get_default_routes(fib)
1222 self.assertEqual(len(default_routes), 1)
1223 dr = default_routes[0]
1224 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1225 self.assertEqual(dr['next_hop'], router_address)
1227 # send RA, updating router lifetime to 1s
1228 packet = self.create_ra_packet(self.pg0, 1)
1229 self.pg0.add_stream([packet])
1234 # check that default route still exists
1235 fib = self.vapi.ip6_fib_dump()
1236 default_routes = self.get_default_routes(fib)
1237 self.assertEqual(len(default_routes), 1)
1238 dr = default_routes[0]
1239 self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1240 self.assertEqual(dr['next_hop'], router_address)
1244 # check that default route is deleted
1245 fib = self.vapi.ip6_fib_dump()
1246 default_routes = self.get_default_routes(fib)
1247 self.assertEqual(len(default_routes), 0)
1249 # check FIB still contains the SLAAC address
1250 addresses = set(self.get_interface_addresses(fib, self.pg0))
1251 new_addresses = addresses.difference(initial_addresses)
1252 self.assertEqual(len(new_addresses), 1)
1253 prefix = list(new_addresses)[0][:8] + '\0\0\0\0\0\0\0\0'
1254 self.assertEqual(inet_ntop(AF_INET6, prefix), '1::')
1258 # check that SLAAC address is deleted
1259 fib = self.vapi.ip6_fib_dump()
1260 addresses = set(self.get_interface_addresses(fib, self.pg0))
1261 new_addresses = addresses.difference(initial_addresses)
1262 self.assertEqual(len(new_addresses), 0)
1265 class IPv6NDProxyTest(TestIPv6ND):
1266 """ IPv6 ND ProxyTest Case """
1269 super(IPv6NDProxyTest, self).setUp()
1271 # create 3 pg interfaces
1272 self.create_pg_interfaces(range(3))
1274 # pg0 is the master interface, with the configured subnet
1276 self.pg0.config_ip6()
1277 self.pg0.resolve_ndp()
1279 self.pg1.ip6_enable()
1280 self.pg2.ip6_enable()
1283 super(IPv6NDProxyTest, self).tearDown()
1285 def test_nd_proxy(self):
1286 """ IPv6 Proxy ND """
1289 # Generate some hosts in the subnet that we are proxying
1291 self.pg0.generate_remote_hosts(8)
1293 nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1294 d = inet_ntop(AF_INET6, nsma)
1297 # Send an NS for one of those remote hosts on one of the proxy links
1298 # expect no response since it's from an address that is not
1299 # on the link that has the prefix configured
1301 ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
1302 IPv6(dst=d, src=self.pg0._remote_hosts[2].ip6) /
1303 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1304 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
1306 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1309 # Add proxy support for the host
1311 self.vapi.ip6_nd_proxy(
1312 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1313 self.pg1.sw_if_index)
1316 # try that NS again. this time we expect an NA back
1318 self.send_and_expect_na(self.pg1, ns_pg1,
1319 "NS to proxy entry",
1320 dst_ip=self.pg0._remote_hosts[2].ip6,
1321 tgt_ip=self.pg0.local_ip6)
1324 # ... and that we have an entry in the ND cache
1326 self.assertTrue(find_nbr(self,
1327 self.pg1.sw_if_index,
1328 self.pg0._remote_hosts[2].ip6,
1332 # ... and we can route traffic to it
1334 t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1335 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1336 src=self.pg0.remote_ip6) /
1337 UDP(sport=10000, dport=20000) /
1340 self.pg0.add_stream(t)
1341 self.pg_enable_capture(self.pg_interfaces)
1343 rx = self.pg1.get_capture(1)
1346 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1347 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1349 self.assertEqual(rx[IPv6].src, t[IPv6].src)
1350 self.assertEqual(rx[IPv6].dst, t[IPv6].dst)
1353 # Test we proxy for the host on the main interface
1355 ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1356 IPv6(dst=d, src=self.pg0.remote_ip6) /
1357 ICMPv6ND_NS(tgt=self.pg0._remote_hosts[2].ip6) /
1358 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0.remote_mac))
1360 self.send_and_expect_na(self.pg0, ns_pg0,
1361 "NS to proxy entry on main",
1362 tgt_ip=self.pg0._remote_hosts[2].ip6,
1363 dst_ip=self.pg0.remote_ip6)
1366 # Setup and resolve proxy for another host on another interface
1368 ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1369 IPv6(dst=d, src=self.pg0._remote_hosts[3].ip6) /
1370 ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1371 ICMPv6NDOptSrcLLAddr(lladdr=self.pg0._remote_hosts[2].mac))
1373 self.vapi.ip6_nd_proxy(
1374 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1375 self.pg2.sw_if_index)
1377 self.send_and_expect_na(self.pg2, ns_pg2,
1378 "NS to proxy entry other interface",
1379 dst_ip=self.pg0._remote_hosts[3].ip6,
1380 tgt_ip=self.pg0.local_ip6)
1382 self.assertTrue(find_nbr(self,
1383 self.pg2.sw_if_index,
1384 self.pg0._remote_hosts[3].ip6,
1388 # hosts can communicate. pg2->pg1
1390 t2 = (Ether(dst=self.pg2.local_mac,
1391 src=self.pg0.remote_hosts[3].mac) /
1392 IPv6(dst=self.pg0._remote_hosts[2].ip6,
1393 src=self.pg0._remote_hosts[3].ip6) /
1394 UDP(sport=10000, dport=20000) /
1397 self.pg2.add_stream(t2)
1398 self.pg_enable_capture(self.pg_interfaces)
1400 rx = self.pg1.get_capture(1)
1403 self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1404 self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1406 self.assertEqual(rx[IPv6].src, t2[IPv6].src)
1407 self.assertEqual(rx[IPv6].dst, t2[IPv6].dst)
1410 # remove the proxy configs
1412 self.vapi.ip6_nd_proxy(
1413 inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1414 self.pg1.sw_if_index,
1416 self.vapi.ip6_nd_proxy(
1417 inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1418 self.pg2.sw_if_index,
1421 self.assertFalse(find_nbr(self,
1422 self.pg2.sw_if_index,
1423 self.pg0._remote_hosts[3].ip6,
1425 self.assertFalse(find_nbr(self,
1426 self.pg1.sw_if_index,
1427 self.pg0._remote_hosts[2].ip6,
1431 # no longer proxy-ing...
1433 self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1434 self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1435 self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1438 # no longer forwarding. traffic generates NS out of the glean/main
1441 self.pg2.add_stream(t2)
1442 self.pg_enable_capture(self.pg_interfaces)
1445 rx = self.pg0.get_capture(1)
1447 self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1450 class TestIPNull(VppTestCase):
1451 """ IPv6 routes via NULL """
1454 super(TestIPNull, self).setUp()
1456 # create 2 pg interfaces
1457 self.create_pg_interfaces(range(1))
1459 for i in self.pg_interfaces:
1465 super(TestIPNull, self).tearDown()
1466 for i in self.pg_interfaces:
1470 def test_ip_null(self):
1471 """ IP NULL route """
1473 p = (Ether(src=self.pg0.remote_mac,
1474 dst=self.pg0.local_mac) /
1475 IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1476 UDP(sport=1234, dport=1234) /
1480 # A route via IP NULL that will reply with ICMP unreachables
1482 ip_unreach = VppIpRoute(self, "2001::", 64, [], is_unreach=1, is_ip6=1)
1483 ip_unreach.add_vpp_config()
1485 self.pg0.add_stream(p)
1486 self.pg_enable_capture(self.pg_interfaces)
1489 rx = self.pg0.get_capture(1)
1491 icmp = rx[ICMPv6DestUnreach]
1493 # 0 = "No route to destination"
1494 self.assertEqual(icmp.code, 0)
1496 # ICMP is rate limited. pause a bit
1500 # A route via IP NULL that will reply with ICMP prohibited
1502 ip_prohibit = VppIpRoute(self, "2001::1", 128, [],
1503 is_prohibit=1, is_ip6=1)
1504 ip_prohibit.add_vpp_config()
1506 self.pg0.add_stream(p)
1507 self.pg_enable_capture(self.pg_interfaces)
1510 rx = self.pg0.get_capture(1)
1512 icmp = rx[ICMPv6DestUnreach]
1514 # 1 = "Communication with destination administratively prohibited"
1515 self.assertEqual(icmp.code, 1)
1518 class TestIPDisabled(VppTestCase):
1519 """ IPv6 disabled """
1522 super(TestIPDisabled, self).setUp()
1524 # create 2 pg interfaces
1525 self.create_pg_interfaces(range(2))
1529 self.pg0.config_ip6()
1530 self.pg0.resolve_ndp()
1532 # PG 1 is not IP enabled
1536 super(TestIPDisabled, self).tearDown()
1537 for i in self.pg_interfaces:
1541 def test_ip_disabled(self):
1546 # one accepting interface, pg0, 2 forwarding interfaces
1548 route_ff_01 = VppIpMRoute(
1552 MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1553 [VppMRoutePath(self.pg1.sw_if_index,
1554 MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1555 VppMRoutePath(self.pg0.sw_if_index,
1556 MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)],
1558 route_ff_01.add_vpp_config()
1560 pu = (Ether(src=self.pg1.remote_mac,
1561 dst=self.pg1.local_mac) /
1562 IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1563 UDP(sport=1234, dport=1234) /
1565 pm = (Ether(src=self.pg1.remote_mac,
1566 dst=self.pg1.local_mac) /
1567 IPv6(src="2001::1", dst="ffef::1") /
1568 UDP(sport=1234, dport=1234) /
1572 # PG1 does not forward IP traffic
1574 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1575 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1580 self.pg1.config_ip6()
1583 # Now we get packets through
1585 self.pg1.add_stream(pu)
1586 self.pg_enable_capture(self.pg_interfaces)
1588 rx = self.pg0.get_capture(1)
1590 self.pg1.add_stream(pm)
1591 self.pg_enable_capture(self.pg_interfaces)
1593 rx = self.pg0.get_capture(1)
1598 self.pg1.unconfig_ip6()
1601 # PG1 does not forward IP traffic
1603 self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1604 self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1607 class TestIP6LoadBalance(VppTestCase):
1608 """ IPv6 Load-Balancing """
1611 super(TestIP6LoadBalance, self).setUp()
1613 self.create_pg_interfaces(range(5))
1615 mpls_tbl = VppMplsTable(self, 0)
1616 mpls_tbl.add_vpp_config()
1618 for i in self.pg_interfaces:
1625 for i in self.pg_interfaces:
1629 super(TestIP6LoadBalance, self).tearDown()
1631 def send_and_expect_load_balancing(self, input, pkts, outputs):
1632 self.vapi.cli("clear trace")
1633 input.add_stream(pkts)
1634 self.pg_enable_capture(self.pg_interfaces)
1637 rx = oo._get_capture(1)
1638 self.assertNotEqual(0, len(rx))
1640 def send_and_expect_one_itf(self, input, pkts, itf):
1641 self.vapi.cli("clear trace")
1642 input.add_stream(pkts)
1643 self.pg_enable_capture(self.pg_interfaces)
1645 rx = itf.get_capture(len(pkts))
1647 def test_ip6_load_balance(self):
1648 """ IPv6 Load-Balancing """
1651 # An array of packets that differ only in the destination port
1655 # - MPLS non-EOS with an entropy label
1659 port_mpls_neos_pkts = []
1663 # An array of packets that differ only in the source address
1668 for ii in range(65):
1669 port_ip_hdr = (IPv6(dst="3000::1", src="3000:1::1") /
1670 UDP(sport=1234, dport=1234 + ii) /
1672 port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1673 dst=self.pg0.local_mac) /
1675 port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1676 dst=self.pg0.local_mac) /
1677 MPLS(label=66, ttl=2) /
1679 port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1680 dst=self.pg0.local_mac) /
1681 MPLS(label=67, ttl=2) /
1682 MPLS(label=77, ttl=2) /
1684 port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1685 dst=self.pg0.local_mac) /
1686 MPLS(label=67, ttl=2) /
1687 MPLS(label=14, ttl=2) /
1688 MPLS(label=999, ttl=2) /
1690 src_ip_hdr = (IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1691 UDP(sport=1234, dport=1234) /
1693 src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1694 dst=self.pg0.local_mac) /
1696 src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1697 dst=self.pg0.local_mac) /
1698 MPLS(label=66, ttl=2) /
1702 # A route for the IP pacekts
1704 route_3000_1 = VppIpRoute(self, "3000::1", 128,
1705 [VppRoutePath(self.pg1.remote_ip6,
1706 self.pg1.sw_if_index,
1707 proto=DpoProto.DPO_PROTO_IP6),
1708 VppRoutePath(self.pg2.remote_ip6,
1709 self.pg2.sw_if_index,
1710 proto=DpoProto.DPO_PROTO_IP6)],
1712 route_3000_1.add_vpp_config()
1715 # a local-label for the EOS packets
1717 binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1718 binding.add_vpp_config()
1721 # An MPLS route for the non-EOS packets
1723 route_67 = VppMplsRoute(self, 67, 0,
1724 [VppRoutePath(self.pg1.remote_ip6,
1725 self.pg1.sw_if_index,
1727 proto=DpoProto.DPO_PROTO_IP6),
1728 VppRoutePath(self.pg2.remote_ip6,
1729 self.pg2.sw_if_index,
1731 proto=DpoProto.DPO_PROTO_IP6)])
1732 route_67.add_vpp_config()
1735 # inject the packet on pg0 - expect load-balancing across the 2 paths
1736 # - since the default hash config is to use IP src,dst and port
1738 # We are not going to ensure equal amounts of packets across each link,
1739 # since the hash algorithm is statistical and therefore this can never
1740 # be guaranteed. But wuth 64 different packets we do expect some
1741 # balancing. So instead just ensure there is traffic on each link.
1743 self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1744 [self.pg1, self.pg2])
1745 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1746 [self.pg1, self.pg2])
1747 self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1748 [self.pg1, self.pg2])
1749 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1750 [self.pg1, self.pg2])
1751 self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1752 [self.pg1, self.pg2])
1755 # The packets with Entropy label in should not load-balance,
1756 # since the Entorpy value is fixed.
1758 self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1761 # change the flow hash config so it's only IP src,dst
1762 # - now only the stream with differing source address will
1765 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=0, dport=0)
1767 self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1768 [self.pg1, self.pg2])
1769 self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1770 [self.pg1, self.pg2])
1771 self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1774 # change the flow hash config back to defaults
1776 self.vapi.set_ip_flow_hash(0, is_ip6=1, src=1, dst=1, sport=1, dport=1)
1779 # Recursive prefixes
1780 # - testing that 2 stages of load-balancing occurs and there is no
1781 # polarisation (i.e. only 2 of 4 paths are used)
1786 for ii in range(257):
1787 port_pkts.append((Ether(src=self.pg0.remote_mac,
1788 dst=self.pg0.local_mac) /
1789 IPv6(dst="4000::1", src="4000:1::1") /
1790 UDP(sport=1234, dport=1234 + ii) /
1792 src_pkts.append((Ether(src=self.pg0.remote_mac,
1793 dst=self.pg0.local_mac) /
1794 IPv6(dst="4000::1", src="4000:1::%d" % ii) /
1795 UDP(sport=1234, dport=1234) /
1798 route_3000_2 = VppIpRoute(self, "3000::2", 128,
1799 [VppRoutePath(self.pg3.remote_ip6,
1800 self.pg3.sw_if_index,
1801 proto=DpoProto.DPO_PROTO_IP6),
1802 VppRoutePath(self.pg4.remote_ip6,
1803 self.pg4.sw_if_index,
1804 proto=DpoProto.DPO_PROTO_IP6)],
1806 route_3000_2.add_vpp_config()
1808 route_4000_1 = VppIpRoute(self, "4000::1", 128,
1809 [VppRoutePath("3000::1",
1811 proto=DpoProto.DPO_PROTO_IP6),
1812 VppRoutePath("3000::2",
1814 proto=DpoProto.DPO_PROTO_IP6)],
1816 route_4000_1.add_vpp_config()
1819 # inject the packet on pg0 - expect load-balancing across all 4 paths
1821 self.vapi.cli("clear trace")
1822 self.send_and_expect_load_balancing(self.pg0, port_pkts,
1823 [self.pg1, self.pg2,
1824 self.pg3, self.pg4])
1825 self.send_and_expect_load_balancing(self.pg0, src_pkts,
1826 [self.pg1, self.pg2,
1827 self.pg3, self.pg4])
1830 # Recursive prefixes
1831 # - testing that 2 stages of load-balancing no choices
1835 for ii in range(257):
1836 port_pkts.append((Ether(src=self.pg0.remote_mac,
1837 dst=self.pg0.local_mac) /
1838 IPv6(dst="6000::1", src="6000:1::1") /
1839 UDP(sport=1234, dport=1234 + ii) /
1842 route_5000_2 = VppIpRoute(self, "5000::2", 128,
1843 [VppRoutePath(self.pg3.remote_ip6,
1844 self.pg3.sw_if_index,
1845 proto=DpoProto.DPO_PROTO_IP6)],
1847 route_5000_2.add_vpp_config()
1849 route_6000_1 = VppIpRoute(self, "6000::1", 128,
1850 [VppRoutePath("5000::2",
1852 proto=DpoProto.DPO_PROTO_IP6)],
1854 route_6000_1.add_vpp_config()
1857 # inject the packet on pg0 - expect load-balancing across all 4 paths
1859 self.vapi.cli("clear trace")
1860 self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1863 class TestIP6Punt(VppTestCase):
1864 """ IPv6 Punt Police/Redirect """
1867 super(TestIP6Punt, self).setUp()
1869 self.create_pg_interfaces(range(2))
1871 for i in self.pg_interfaces:
1877 super(TestIP6Punt, self).tearDown()
1878 for i in self.pg_interfaces:
1882 def test_ip_punt(self):
1883 """ IP6 punt police and redirect """
1885 p = (Ether(src=self.pg0.remote_mac,
1886 dst=self.pg0.local_mac) /
1887 IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
1888 TCP(sport=1234, dport=1234) /
1894 # Configure a punt redirect via pg1.
1896 nh_addr = inet_pton(AF_INET6,
1897 self.pg1.remote_ip6)
1898 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1899 self.pg1.sw_if_index,
1903 self.send_and_expect(self.pg0, pkts, self.pg1)
1908 policer = self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1910 self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
1912 self.vapi.cli("clear trace")
1913 self.pg0.add_stream(pkts)
1914 self.pg_enable_capture(self.pg_interfaces)
1918 # the number of packet recieved should be greater than 0,
1919 # but not equal to the number sent, since some were policed
1921 rx = self.pg1._get_capture(1)
1922 self.assertTrue(len(rx) > 0)
1923 self.assertTrue(len(rx) < len(pkts))
1926 # remove the poilcer. back to full rx
1928 self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
1929 self.vapi.policer_add_del("ip6-punt", 400, 0, 10, 0,
1930 rate_type=1, is_add=0)
1931 self.send_and_expect(self.pg0, pkts, self.pg1)
1934 # remove the redirect. expect full drop.
1936 self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1937 self.pg1.sw_if_index,
1941 self.send_and_assert_no_replies(self.pg0, pkts,
1942 "IP no punt config")
1945 # Add a redirect that is not input port selective
1947 self.vapi.ip_punt_redirect(0xffffffff,
1948 self.pg1.sw_if_index,
1951 self.send_and_expect(self.pg0, pkts, self.pg1)
1953 self.vapi.ip_punt_redirect(0xffffffff,
1954 self.pg1.sw_if_index,
1960 class TestIP6Input(VppTestCase):
1961 """ IPv6 Input Exceptions """
1964 super(TestIP6Input, self).setUp()
1966 self.create_pg_interfaces(range(2))
1968 for i in self.pg_interfaces:
1974 super(TestIP6Input, self).tearDown()
1975 for i in self.pg_interfaces:
1979 def test_ip_input(self):
1980 """ IP6 Input Exceptions """
1983 # bad version - this is dropped
1985 p_version = (Ether(src=self.pg0.remote_mac,
1986 dst=self.pg0.local_mac) /
1987 IPv6(src=self.pg0.remote_ip6,
1988 dst=self.pg1.remote_ip6,
1990 UDP(sport=1234, dport=1234) /
1993 self.send_and_assert_no_replies(self.pg0, p_version * 65,
1997 # hop limit - IMCP replies
1999 p_version = (Ether(src=self.pg0.remote_mac,
2000 dst=self.pg0.local_mac) /
2001 IPv6(src=self.pg0.remote_ip6,
2002 dst=self.pg1.remote_ip6,
2004 UDP(sport=1234, dport=1234) /
2007 rx = self.send_and_expect(self.pg0, p_version * 65, self.pg0)
2009 icmp = rx[ICMPv6TimeExceeded]
2010 self.assertEqual(icmp.type, 3)
2011 # 0: "hop limit exceeded in transit",
2012 self.assertEqual(icmp.code, 0)
2015 if __name__ == '__main__':
2016 unittest.main(testRunner=VppTestRunner)