fib: fib api updates
[vpp.git] / test / test_ip6.py
1 #!/usr/bin/env python
2
3 import socket
4 import unittest
5
6 from parameterized import parameterized
7 import scapy.compat
8 import scapy.layers.inet6 as inet6
9 from scapy.contrib.mpls import MPLS
10 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_RS, \
11     ICMPv6ND_RA, ICMPv6NDOptMTU, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptPrefixInfo, \
12     ICMPv6ND_NA, ICMPv6NDOptDstLLAddr, ICMPv6DestUnreach, icmp6types, \
13     ICMPv6TimeExceeded, ICMPv6EchoRequest, ICMPv6EchoReply, IPv6ExtHdrHopByHop
14 from scapy.layers.l2 import Ether, Dot1Q
15 from scapy.packet import Raw
16 from scapy.utils import inet_pton, inet_ntop
17 from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ptop, in6_islladdr, \
18     in6_mactoifaceid
19 from six import moves
20
21 from framework import VppTestCase, VppTestRunner
22 from util import ppp, ip6_normalize, mk_ll_addr
23 from vpp_ip import DpoProto
24 from vpp_ip_route import VppIpRoute, VppRoutePath, find_route, VppIpMRoute, \
25     VppMRoutePath, MRouteItfFlags, MRouteEntryFlags, VppMplsIpBind, \
26     VppMplsRoute, VppMplsTable, VppIpTable, FibPathType
27 from vpp_neighbor import find_nbr, VppNeighbor
28 from vpp_pg_interface import is_ipv6_misc
29 from vpp_sub_interface import VppSubInterface, VppDot1QSubint
30 from ipaddress import IPv6Network, IPv4Network, IPv6Address
31
32 AF_INET6 = socket.AF_INET6
33
34 try:
35     text_type = unicode
36 except NameError:
37     text_type = str
38
39 NUM_PKTS = 67
40
41
42 class TestIPv6ND(VppTestCase):
43     def validate_ra(self, intf, rx, dst_ip=None):
44         if not dst_ip:
45             dst_ip = intf.remote_ip6
46
47         # unicasted packets must come to the unicast mac
48         self.assertEqual(rx[Ether].dst, intf.remote_mac)
49
50         # and from the router's MAC
51         self.assertEqual(rx[Ether].src, intf.local_mac)
52
53         # the rx'd RA should be addressed to the sender's source
54         self.assertTrue(rx.haslayer(ICMPv6ND_RA))
55         self.assertEqual(in6_ptop(rx[IPv6].dst),
56                          in6_ptop(dst_ip))
57
58         # and come from the router's link local
59         self.assertTrue(in6_islladdr(rx[IPv6].src))
60         self.assertEqual(in6_ptop(rx[IPv6].src),
61                          in6_ptop(mk_ll_addr(intf.local_mac)))
62
63     def validate_na(self, intf, rx, dst_ip=None, tgt_ip=None):
64         if not dst_ip:
65             dst_ip = intf.remote_ip6
66         if not tgt_ip:
67             dst_ip = intf.local_ip6
68
69         # unicasted packets must come to the unicast mac
70         self.assertEqual(rx[Ether].dst, intf.remote_mac)
71
72         # and from the router's MAC
73         self.assertEqual(rx[Ether].src, intf.local_mac)
74
75         # the rx'd NA should be addressed to the sender's source
76         self.assertTrue(rx.haslayer(ICMPv6ND_NA))
77         self.assertEqual(in6_ptop(rx[IPv6].dst),
78                          in6_ptop(dst_ip))
79
80         # and come from the target address
81         self.assertEqual(
82             in6_ptop(rx[IPv6].src), in6_ptop(tgt_ip))
83
84         # Dest link-layer options should have the router's MAC
85         dll = rx[ICMPv6NDOptDstLLAddr]
86         self.assertEqual(dll.lladdr, intf.local_mac)
87
88     def validate_ns(self, intf, rx, tgt_ip):
89         nsma = in6_getnsma(inet_pton(AF_INET6, tgt_ip))
90         dst_ip = inet_ntop(AF_INET6, nsma)
91
92         # NS is broadcast
93         self.assertEqual(rx[Ether].dst, in6_getnsmac(nsma))
94
95         # and from the router's MAC
96         self.assertEqual(rx[Ether].src, intf.local_mac)
97
98         # the rx'd NS should be addressed to an mcast address
99         # derived from the target address
100         self.assertEqual(
101             in6_ptop(rx[IPv6].dst), in6_ptop(dst_ip))
102
103         # expect the tgt IP in the NS header
104         ns = rx[ICMPv6ND_NS]
105         self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
106
107         # packet is from the router's local address
108         self.assertEqual(
109             in6_ptop(rx[IPv6].src), intf.local_ip6)
110
111         # Src link-layer options should have the router's MAC
112         sll = rx[ICMPv6NDOptSrcLLAddr]
113         self.assertEqual(sll.lladdr, intf.local_mac)
114
115     def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
116                            filter_out_fn=is_ipv6_misc):
117         intf.add_stream(pkts)
118         self.pg_enable_capture(self.pg_interfaces)
119         self.pg_start()
120         rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
121
122         self.assertEqual(len(rx), 1)
123         rx = rx[0]
124         self.validate_ra(intf, rx, dst_ip)
125
126     def send_and_expect_na(self, intf, pkts, remark, dst_ip=None,
127                            tgt_ip=None,
128                            filter_out_fn=is_ipv6_misc):
129         intf.add_stream(pkts)
130         self.pg_enable_capture(self.pg_interfaces)
131         self.pg_start()
132         rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
133
134         self.assertEqual(len(rx), 1)
135         rx = rx[0]
136         self.validate_na(intf, rx, dst_ip, tgt_ip)
137
138     def send_and_expect_ns(self, tx_intf, rx_intf, pkts, tgt_ip,
139                            filter_out_fn=is_ipv6_misc):
140         tx_intf.add_stream(pkts)
141         self.pg_enable_capture(self.pg_interfaces)
142         self.pg_start()
143         rx = rx_intf.get_capture(1, filter_out_fn=filter_out_fn)
144
145         self.assertEqual(len(rx), 1)
146         rx = rx[0]
147         self.validate_ns(rx_intf, rx, tgt_ip)
148
149     def verify_ip(self, rx, smac, dmac, sip, dip):
150         ether = rx[Ether]
151         self.assertEqual(ether.dst, dmac)
152         self.assertEqual(ether.src, smac)
153
154         ip = rx[IPv6]
155         self.assertEqual(ip.src, sip)
156         self.assertEqual(ip.dst, dip)
157
158
159 class TestIPv6(TestIPv6ND):
160     """ IPv6 Test Case """
161
162     @classmethod
163     def setUpClass(cls):
164         super(TestIPv6, cls).setUpClass()
165
166     @classmethod
167     def tearDownClass(cls):
168         super(TestIPv6, cls).tearDownClass()
169
170     def setUp(self):
171         """
172         Perform test setup before test case.
173
174         **Config:**
175             - create 3 pg interfaces
176                 - untagged pg0 interface
177                 - Dot1Q subinterface on pg1
178                 - Dot1AD subinterface on pg2
179             - setup interfaces:
180                 - put it into UP state
181                 - set IPv6 addresses
182                 - resolve neighbor address using NDP
183             - configure 200 fib entries
184
185         :ivar list interfaces: pg interfaces and subinterfaces.
186         :ivar dict flows: IPv4 packet flows in test.
187
188         *TODO:* Create AD sub interface
189         """
190         super(TestIPv6, self).setUp()
191
192         # create 3 pg interfaces
193         self.create_pg_interfaces(range(3))
194
195         # create 2 subinterfaces for p1 and pg2
196         self.sub_interfaces = [
197             VppDot1QSubint(self, self.pg1, 100),
198             VppDot1QSubint(self, self.pg2, 200)
199             # TODO: VppDot1ADSubint(self, self.pg2, 200, 300, 400)
200         ]
201
202         # packet flows mapping pg0 -> pg1.sub, pg2.sub, etc.
203         self.flows = dict()
204         self.flows[self.pg0] = [self.pg1.sub_if, self.pg2.sub_if]
205         self.flows[self.pg1.sub_if] = [self.pg0, self.pg2.sub_if]
206         self.flows[self.pg2.sub_if] = [self.pg0, self.pg1.sub_if]
207
208         # packet sizes
209         self.pg_if_packet_sizes = [64, 1500, 9020]
210
211         self.interfaces = list(self.pg_interfaces)
212         self.interfaces.extend(self.sub_interfaces)
213
214         # setup all interfaces
215         for i in self.interfaces:
216             i.admin_up()
217             i.config_ip6()
218             i.resolve_ndp()
219
220     def tearDown(self):
221         """Run standard test teardown and log ``show ip6 neighbors``."""
222         for i in self.interfaces:
223             i.unconfig_ip6()
224             i.ip6_disable()
225             i.admin_down()
226         for i in self.sub_interfaces:
227             i.remove_vpp_config()
228
229         super(TestIPv6, self).tearDown()
230         if not self.vpp_dead:
231             self.logger.info(self.vapi.cli("show ip6 neighbors"))
232             # info(self.vapi.cli("show ip6 fib"))  # many entries
233
234     def modify_packet(self, src_if, packet_size, pkt):
235         """Add load, set destination IP and extend packet to required packet
236         size for defined interface.
237
238         :param VppInterface src_if: Interface to create packet for.
239         :param int packet_size: Required packet size.
240         :param Scapy pkt: Packet to be modified.
241         """
242         dst_if_idx = packet_size / 10 % 2
243         dst_if = self.flows[src_if][dst_if_idx]
244         info = self.create_packet_info(src_if, dst_if)
245         payload = self.info_to_payload(info)
246         p = pkt / Raw(payload)
247         p[IPv6].dst = dst_if.remote_ip6
248         info.data = p.copy()
249         if isinstance(src_if, VppSubInterface):
250             p = src_if.add_dot1_layer(p)
251         self.extend_packet(p, packet_size)
252
253         return p
254
255     def create_stream(self, src_if):
256         """Create input packet stream for defined interface.
257
258         :param VppInterface src_if: Interface to create packet stream for.
259         """
260         hdr_ext = 4 if isinstance(src_if, VppSubInterface) else 0
261         pkt_tmpl = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
262                     IPv6(src=src_if.remote_ip6) /
263                     inet6.UDP(sport=1234, dport=1234))
264
265         pkts = [self.modify_packet(src_if, i, pkt_tmpl)
266                 for i in moves.range(self.pg_if_packet_sizes[0],
267                                      self.pg_if_packet_sizes[1], 10)]
268         pkts_b = [self.modify_packet(src_if, i, pkt_tmpl)
269                   for i in moves.range(self.pg_if_packet_sizes[1] + hdr_ext,
270                                        self.pg_if_packet_sizes[2] + hdr_ext,
271                                        50)]
272         pkts.extend(pkts_b)
273
274         return pkts
275
276     def verify_capture(self, dst_if, capture):
277         """Verify captured input packet stream for defined interface.
278
279         :param VppInterface dst_if: Interface to verify captured packet stream
280                                     for.
281         :param list capture: Captured packet stream.
282         """
283         self.logger.info("Verifying capture on interface %s" % dst_if.name)
284         last_info = dict()
285         for i in self.interfaces:
286             last_info[i.sw_if_index] = None
287         is_sub_if = False
288         dst_sw_if_index = dst_if.sw_if_index
289         if hasattr(dst_if, 'parent'):
290             is_sub_if = True
291         for packet in capture:
292             if is_sub_if:
293                 # Check VLAN tags and Ethernet header
294                 packet = dst_if.remove_dot1_layer(packet)
295             self.assertTrue(Dot1Q not in packet)
296             try:
297                 ip = packet[IPv6]
298                 udp = packet[inet6.UDP]
299                 payload_info = self.payload_to_info(packet[Raw])
300                 packet_index = payload_info.index
301                 self.assertEqual(payload_info.dst, dst_sw_if_index)
302                 self.logger.debug(
303                     "Got packet on port %s: src=%u (id=%u)" %
304                     (dst_if.name, payload_info.src, packet_index))
305                 next_info = self.get_next_packet_info_for_interface2(
306                     payload_info.src, dst_sw_if_index,
307                     last_info[payload_info.src])
308                 last_info[payload_info.src] = next_info
309                 self.assertTrue(next_info is not None)
310                 self.assertEqual(packet_index, next_info.index)
311                 saved_packet = next_info.data
312                 # Check standard fields
313                 self.assertEqual(
314                     ip.src, saved_packet[IPv6].src)
315                 self.assertEqual(
316                     ip.dst, saved_packet[IPv6].dst)
317                 self.assertEqual(
318                     udp.sport, saved_packet[inet6.UDP].sport)
319                 self.assertEqual(
320                     udp.dport, saved_packet[inet6.UDP].dport)
321             except:
322                 self.logger.error(ppp("Unexpected or invalid packet:", packet))
323                 raise
324         for i in self.interfaces:
325             remaining_packet = self.get_next_packet_info_for_interface2(
326                 i.sw_if_index, dst_sw_if_index, last_info[i.sw_if_index])
327             self.assertTrue(remaining_packet is None,
328                             "Interface %s: Packet expected from interface %s "
329                             "didn't arrive" % (dst_if.name, i.name))
330
331     def test_next_header_anomaly(self):
332         """ IPv6 next header anomaly test
333
334         Test scenario:
335             - ipv6 next header field = Fragment Header (44)
336             - next header is ICMPv6 Echo Request
337             - wait for reassembly
338         """
339         pkt = (Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) /
340                IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6, nh=44) /
341                ICMPv6EchoRequest())
342
343         self.pg0.add_stream(pkt)
344         self.pg_start()
345
346         # wait for reassembly
347         self.sleep(10)
348
349     def test_fib(self):
350         """ IPv6 FIB test
351
352         Test scenario:
353             - Create IPv6 stream for pg0 interface
354             - Create IPv6 tagged streams for pg1's and pg2's subinterface.
355             - Send and verify received packets on each interface.
356         """
357
358         pkts = self.create_stream(self.pg0)
359         self.pg0.add_stream(pkts)
360
361         for i in self.sub_interfaces:
362             pkts = self.create_stream(i)
363             i.parent.add_stream(pkts)
364
365         self.pg_enable_capture(self.pg_interfaces)
366         self.pg_start()
367
368         pkts = self.pg0.get_capture()
369         self.verify_capture(self.pg0, pkts)
370
371         for i in self.sub_interfaces:
372             pkts = i.parent.get_capture()
373             self.verify_capture(i, pkts)
374
375     def test_ns(self):
376         """ IPv6 Neighbour Solicitation Exceptions
377
378         Test scenario:
379            - Send an NS Sourced from an address not covered by the link sub-net
380            - Send an NS to an mcast address the router has not joined
381            - Send NS for a target address the router does not onn.
382         """
383
384         #
385         # An NS from a non link source address
386         #
387         nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
388         d = inet_ntop(AF_INET6, nsma)
389
390         p = (Ether(dst=in6_getnsmac(nsma)) /
391              IPv6(dst=d, src="2002::2") /
392              ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
393              ICMPv6NDOptSrcLLAddr(
394                  lladdr=self.pg0.remote_mac))
395         pkts = [p]
396
397         self.send_and_assert_no_replies(
398             self.pg0, pkts,
399             "No response to NS source by address not on sub-net")
400
401         #
402         # An NS for sent to a solicited mcast group the router is
403         # not a member of FAILS
404         #
405         if 0:
406             nsma = in6_getnsma(inet_pton(AF_INET6, "fd::ffff"))
407             d = inet_ntop(AF_INET6, nsma)
408
409             p = (Ether(dst=in6_getnsmac(nsma)) /
410                  IPv6(dst=d, src=self.pg0.remote_ip6) /
411                  ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
412                  ICMPv6NDOptSrcLLAddr(
413                      lladdr=self.pg0.remote_mac))
414             pkts = [p]
415
416             self.send_and_assert_no_replies(
417                 self.pg0, pkts,
418                 "No response to NS sent to unjoined mcast address")
419
420         #
421         # An NS whose target address is one the router does not own
422         #
423         nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
424         d = inet_ntop(AF_INET6, nsma)
425
426         p = (Ether(dst=in6_getnsmac(nsma)) /
427              IPv6(dst=d, src=self.pg0.remote_ip6) /
428              ICMPv6ND_NS(tgt="fd::ffff") /
429              ICMPv6NDOptSrcLLAddr(
430                  lladdr=self.pg0.remote_mac))
431         pkts = [p]
432
433         self.send_and_assert_no_replies(self.pg0, pkts,
434                                         "No response to NS for unknown target")
435
436         #
437         # A neighbor entry that has no associated FIB-entry
438         #
439         self.pg0.generate_remote_hosts(4)
440         nd_entry = VppNeighbor(self,
441                                self.pg0.sw_if_index,
442                                self.pg0.remote_hosts[2].mac,
443                                self.pg0.remote_hosts[2].ip6,
444                                is_no_fib_entry=1)
445         nd_entry.add_vpp_config()
446
447         #
448         # check we have the neighbor, but no route
449         #
450         self.assertTrue(find_nbr(self,
451                                  self.pg0.sw_if_index,
452                                  self.pg0._remote_hosts[2].ip6))
453         self.assertFalse(find_route(self,
454                                     self.pg0._remote_hosts[2].ip6,
455                                     128))
456
457         #
458         # send an NS from a link local address to the interface's global
459         # address
460         #
461         p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
462              IPv6(
463                  dst=d, src=self.pg0._remote_hosts[2].ip6_ll) /
464              ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
465              ICMPv6NDOptSrcLLAddr(
466                  lladdr=self.pg0.remote_mac))
467
468         self.send_and_expect_na(self.pg0, p,
469                                 "NS from link-local",
470                                 dst_ip=self.pg0._remote_hosts[2].ip6_ll,
471                                 tgt_ip=self.pg0.local_ip6)
472
473         #
474         # we should have learned an ND entry for the peer's link-local
475         # but not inserted a route to it in the FIB
476         #
477         self.assertTrue(find_nbr(self,
478                                  self.pg0.sw_if_index,
479                                  self.pg0._remote_hosts[2].ip6_ll))
480         self.assertFalse(find_route(self,
481                                     self.pg0._remote_hosts[2].ip6_ll,
482                                     128))
483
484         #
485         # An NS to the router's own Link-local
486         #
487         p = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
488              IPv6(
489                  dst=d, src=self.pg0._remote_hosts[3].ip6_ll) /
490              ICMPv6ND_NS(tgt=self.pg0.local_ip6_ll) /
491              ICMPv6NDOptSrcLLAddr(
492                  lladdr=self.pg0.remote_mac))
493
494         self.send_and_expect_na(self.pg0, p,
495                                 "NS to/from link-local",
496                                 dst_ip=self.pg0._remote_hosts[3].ip6_ll,
497                                 tgt_ip=self.pg0.local_ip6_ll)
498
499         #
500         # we should have learned an ND entry for the peer's link-local
501         # but not inserted a route to it in the FIB
502         #
503         self.assertTrue(find_nbr(self,
504                                  self.pg0.sw_if_index,
505                                  self.pg0._remote_hosts[3].ip6_ll))
506         self.assertFalse(find_route(self,
507                                     self.pg0._remote_hosts[3].ip6_ll,
508                                     128))
509
510     def test_ns_duplicates(self):
511         """ ND Duplicates"""
512
513         #
514         # Generate some hosts on the LAN
515         #
516         self.pg1.generate_remote_hosts(3)
517
518         #
519         # Add host 1 on pg1 and pg2
520         #
521         ns_pg1 = VppNeighbor(self,
522                              self.pg1.sw_if_index,
523                              self.pg1.remote_hosts[1].mac,
524                              self.pg1.remote_hosts[1].ip6)
525         ns_pg1.add_vpp_config()
526         ns_pg2 = VppNeighbor(self,
527                              self.pg2.sw_if_index,
528                              self.pg2.remote_mac,
529                              self.pg1.remote_hosts[1].ip6)
530         ns_pg2.add_vpp_config()
531
532         #
533         # IP packet destined for pg1 remote host arrives on pg1 again.
534         #
535         p = (Ether(dst=self.pg0.local_mac,
536                    src=self.pg0.remote_mac) /
537              IPv6(src=self.pg0.remote_ip6,
538                   dst=self.pg1.remote_hosts[1].ip6) /
539              inet6.UDP(sport=1234, dport=1234) /
540              Raw())
541
542         self.pg0.add_stream(p)
543         self.pg_enable_capture(self.pg_interfaces)
544         self.pg_start()
545
546         rx1 = self.pg1.get_capture(1)
547
548         self.verify_ip(rx1[0],
549                        self.pg1.local_mac,
550                        self.pg1.remote_hosts[1].mac,
551                        self.pg0.remote_ip6,
552                        self.pg1.remote_hosts[1].ip6)
553
554         #
555         # remove the duplicate on pg1
556         # packet stream should generate NSs out of pg1
557         #
558         ns_pg1.remove_vpp_config()
559
560         self.send_and_expect_ns(self.pg0, self.pg1,
561                                 p, self.pg1.remote_hosts[1].ip6)
562
563         #
564         # Add it back
565         #
566         ns_pg1.add_vpp_config()
567
568         self.pg0.add_stream(p)
569         self.pg_enable_capture(self.pg_interfaces)
570         self.pg_start()
571
572         rx1 = self.pg1.get_capture(1)
573
574         self.verify_ip(rx1[0],
575                        self.pg1.local_mac,
576                        self.pg1.remote_hosts[1].mac,
577                        self.pg0.remote_ip6,
578                        self.pg1.remote_hosts[1].ip6)
579
580     def validate_ra(self, intf, rx, dst_ip=None, mtu=9000, pi_opt=None):
581         if not dst_ip:
582             dst_ip = intf.remote_ip6
583
584         # unicasted packets must come to the unicast mac
585         self.assertEqual(rx[Ether].dst, intf.remote_mac)
586
587         # and from the router's MAC
588         self.assertEqual(rx[Ether].src, intf.local_mac)
589
590         # the rx'd RA should be addressed to the sender's source
591         self.assertTrue(rx.haslayer(ICMPv6ND_RA))
592         self.assertEqual(in6_ptop(rx[IPv6].dst),
593                          in6_ptop(dst_ip))
594
595         # and come from the router's link local
596         self.assertTrue(in6_islladdr(rx[IPv6].src))
597         self.assertEqual(in6_ptop(rx[IPv6].src),
598                          in6_ptop(mk_ll_addr(intf.local_mac)))
599
600         # it should contain the links MTU
601         ra = rx[ICMPv6ND_RA]
602         self.assertEqual(ra[ICMPv6NDOptMTU].mtu, mtu)
603
604         # it should contain the source's link layer address option
605         sll = ra[ICMPv6NDOptSrcLLAddr]
606         self.assertEqual(sll.lladdr, intf.local_mac)
607
608         if not pi_opt:
609             # the RA should not contain prefix information
610             self.assertFalse(ra.haslayer(
611                 ICMPv6NDOptPrefixInfo))
612         else:
613             raos = rx.getlayer(ICMPv6NDOptPrefixInfo, 1)
614
615             # the options are nested in the scapy packet in way that i cannot
616             # decipher how to decode. this 1st layer of option always returns
617             # nested classes, so a direct obj1=obj2 comparison always fails.
618             # however, the getlayer(.., 2) does give one instnace.
619             # so we cheat here and construct a new opt instance for comparison
620             rd = ICMPv6NDOptPrefixInfo(
621                 prefixlen=raos.prefixlen,
622                 prefix=raos.prefix,
623                 L=raos.L,
624                 A=raos.A)
625             if type(pi_opt) is list:
626                 for ii in range(len(pi_opt)):
627                     self.assertEqual(pi_opt[ii], rd)
628                     rd = rx.getlayer(
629                         ICMPv6NDOptPrefixInfo, ii + 2)
630             else:
631                 self.assertEqual(pi_opt, raos)
632
633     def send_and_expect_ra(self, intf, pkts, remark, dst_ip=None,
634                            filter_out_fn=is_ipv6_misc,
635                            opt=None):
636         intf.add_stream(pkts)
637         self.pg_enable_capture(self.pg_interfaces)
638         self.pg_start()
639         rx = intf.get_capture(1, filter_out_fn=filter_out_fn)
640
641         self.assertEqual(len(rx), 1)
642         rx = rx[0]
643         self.validate_ra(intf, rx, dst_ip, pi_opt=opt)
644
645     def test_rs(self):
646         """ IPv6 Router Solicitation Exceptions
647
648         Test scenario:
649         """
650
651         #
652         # Before we begin change the IPv6 RA responses to use the unicast
653         # address - that way we will not confuse them with the periodic
654         # RAs which go to the mcast address
655         # Sit and wait for the first periodic RA.
656         #
657         # TODO
658         #
659         self.pg0.ip6_ra_config(send_unicast=1)
660
661         #
662         # An RS from a link source address
663         #  - expect an RA in return
664         #
665         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
666              IPv6(
667                  dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
668              ICMPv6ND_RS())
669         pkts = [p]
670         self.send_and_expect_ra(self.pg0, pkts, "Genuine RS")
671
672         #
673         # For the next RS sent the RA should be rate limited
674         #
675         self.send_and_assert_no_replies(self.pg0, pkts, "RA rate limited")
676
677         #
678         # When we reconfigure the IPv6 RA config,
679         # we reset the RA rate limiting,
680         # so we need to do this before each test below so as not to drop
681         # packets for rate limiting reasons. Test this works here.
682         #
683         self.pg0.ip6_ra_config(send_unicast=1)
684         self.send_and_expect_ra(self.pg0, pkts, "Rate limit reset RS")
685
686         #
687         # An RS sent from a non-link local source
688         #
689         self.pg0.ip6_ra_config(send_unicast=1)
690         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
691              IPv6(dst=self.pg0.local_ip6,
692                   src="2002::ffff") /
693              ICMPv6ND_RS())
694         pkts = [p]
695         self.send_and_assert_no_replies(self.pg0, pkts,
696                                         "RS from non-link source")
697
698         #
699         # Source an RS from a link local address
700         #
701         self.pg0.ip6_ra_config(send_unicast=1)
702         ll = mk_ll_addr(self.pg0.remote_mac)
703         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
704              IPv6(dst=self.pg0.local_ip6, src=ll) /
705              ICMPv6ND_RS())
706         pkts = [p]
707         self.send_and_expect_ra(self.pg0, pkts,
708                                 "RS sourced from link-local",
709                                 dst_ip=ll)
710
711         #
712         # Send the RS multicast
713         #
714         self.pg0.ip6_ra_config(send_unicast=1)
715         dmac = in6_getnsmac(inet_pton(AF_INET6, "ff02::2"))
716         ll = mk_ll_addr(self.pg0.remote_mac)
717         p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
718              IPv6(dst="ff02::2", src=ll) /
719              ICMPv6ND_RS())
720         pkts = [p]
721         self.send_and_expect_ra(self.pg0, pkts,
722                                 "RS sourced from link-local",
723                                 dst_ip=ll)
724
725         #
726         # Source from the unspecified address ::. This happens when the RS
727         # is sent before the host has a configured address/sub-net,
728         # i.e. auto-config. Since the sender has no IP address, the reply
729         # comes back mcast - so the capture needs to not filter this.
730         # If we happen to pick up the periodic RA at this point then so be it,
731         # it's not an error.
732         #
733         self.pg0.ip6_ra_config(send_unicast=1, suppress=1)
734         p = (Ether(dst=dmac, src=self.pg0.remote_mac) /
735              IPv6(dst="ff02::2", src="::") /
736              ICMPv6ND_RS())
737         pkts = [p]
738         self.send_and_expect_ra(self.pg0, pkts,
739                                 "RS sourced from unspecified",
740                                 dst_ip="ff02::1",
741                                 filter_out_fn=None)
742
743         #
744         # Configure The RA to announce the links prefix
745         #
746         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
747                                self.pg0.local_ip6_prefix_len)
748
749         #
750         # RAs should now contain the prefix information option
751         #
752         opt = ICMPv6NDOptPrefixInfo(
753             prefixlen=self.pg0.local_ip6_prefix_len,
754             prefix=self.pg0.local_ip6,
755             L=1,
756             A=1)
757
758         self.pg0.ip6_ra_config(send_unicast=1)
759         ll = mk_ll_addr(self.pg0.remote_mac)
760         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
761              IPv6(dst=self.pg0.local_ip6, src=ll) /
762              ICMPv6ND_RS())
763         self.send_and_expect_ra(self.pg0, p,
764                                 "RA with prefix-info",
765                                 dst_ip=ll,
766                                 opt=opt)
767
768         #
769         # Change the prefix info to not off-link
770         #  L-flag is clear
771         #
772         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
773                                self.pg0.local_ip6_prefix_len,
774                                off_link=1)
775
776         opt = ICMPv6NDOptPrefixInfo(
777             prefixlen=self.pg0.local_ip6_prefix_len,
778             prefix=self.pg0.local_ip6,
779             L=0,
780             A=1)
781
782         self.pg0.ip6_ra_config(send_unicast=1)
783         self.send_and_expect_ra(self.pg0, p,
784                                 "RA with Prefix info with L-flag=0",
785                                 dst_ip=ll,
786                                 opt=opt)
787
788         #
789         # Change the prefix info to not off-link, no-autoconfig
790         #  L and A flag are clear in the advert
791         #
792         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
793                                self.pg0.local_ip6_prefix_len,
794                                off_link=1,
795                                no_autoconfig=1)
796
797         opt = ICMPv6NDOptPrefixInfo(
798             prefixlen=self.pg0.local_ip6_prefix_len,
799             prefix=self.pg0.local_ip6,
800             L=0,
801             A=0)
802
803         self.pg0.ip6_ra_config(send_unicast=1)
804         self.send_and_expect_ra(self.pg0, p,
805                                 "RA with Prefix info with A & L-flag=0",
806                                 dst_ip=ll,
807                                 opt=opt)
808
809         #
810         # Change the flag settings back to the defaults
811         #  L and A flag are set in the advert
812         #
813         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
814                                self.pg0.local_ip6_prefix_len)
815
816         opt = ICMPv6NDOptPrefixInfo(
817             prefixlen=self.pg0.local_ip6_prefix_len,
818             prefix=self.pg0.local_ip6,
819             L=1,
820             A=1)
821
822         self.pg0.ip6_ra_config(send_unicast=1)
823         self.send_and_expect_ra(self.pg0, p,
824                                 "RA with Prefix info",
825                                 dst_ip=ll,
826                                 opt=opt)
827
828         #
829         # Change the prefix info to not off-link, no-autoconfig
830         #  L and A flag are clear in the advert
831         #
832         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
833                                self.pg0.local_ip6_prefix_len,
834                                off_link=1,
835                                no_autoconfig=1)
836
837         opt = ICMPv6NDOptPrefixInfo(
838             prefixlen=self.pg0.local_ip6_prefix_len,
839             prefix=self.pg0.local_ip6,
840             L=0,
841             A=0)
842
843         self.pg0.ip6_ra_config(send_unicast=1)
844         self.send_and_expect_ra(self.pg0, p,
845                                 "RA with Prefix info with A & L-flag=0",
846                                 dst_ip=ll,
847                                 opt=opt)
848
849         #
850         # Use the reset to defaults option to revert to defaults
851         #  L and A flag are clear in the advert
852         #
853         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
854                                self.pg0.local_ip6_prefix_len,
855                                use_default=1)
856
857         opt = ICMPv6NDOptPrefixInfo(
858             prefixlen=self.pg0.local_ip6_prefix_len,
859             prefix=self.pg0.local_ip6,
860             L=1,
861             A=1)
862
863         self.pg0.ip6_ra_config(send_unicast=1)
864         self.send_and_expect_ra(self.pg0, p,
865                                 "RA with Prefix reverted to defaults",
866                                 dst_ip=ll,
867                                 opt=opt)
868
869         #
870         # Advertise Another prefix. With no L-flag/A-flag
871         #
872         self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
873                                self.pg1.local_ip6_prefix_len,
874                                off_link=1,
875                                no_autoconfig=1)
876
877         opt = [ICMPv6NDOptPrefixInfo(
878             prefixlen=self.pg0.local_ip6_prefix_len,
879             prefix=self.pg0.local_ip6,
880             L=1,
881             A=1),
882             ICMPv6NDOptPrefixInfo(
883                 prefixlen=self.pg1.local_ip6_prefix_len,
884                 prefix=self.pg1.local_ip6,
885                 L=0,
886                 A=0)]
887
888         self.pg0.ip6_ra_config(send_unicast=1)
889         ll = mk_ll_addr(self.pg0.remote_mac)
890         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
891              IPv6(dst=self.pg0.local_ip6, src=ll) /
892              ICMPv6ND_RS())
893         self.send_and_expect_ra(self.pg0, p,
894                                 "RA with multiple Prefix infos",
895                                 dst_ip=ll,
896                                 opt=opt)
897
898         #
899         # Remove the first prefix-info - expect the second is still in the
900         # advert
901         #
902         self.pg0.ip6_ra_prefix(self.pg0.local_ip6,
903                                self.pg0.local_ip6_prefix_len,
904                                is_no=1)
905
906         opt = ICMPv6NDOptPrefixInfo(
907             prefixlen=self.pg1.local_ip6_prefix_len,
908             prefix=self.pg1.local_ip6,
909             L=0,
910             A=0)
911
912         self.pg0.ip6_ra_config(send_unicast=1)
913         self.send_and_expect_ra(self.pg0, p,
914                                 "RA with Prefix reverted to defaults",
915                                 dst_ip=ll,
916                                 opt=opt)
917
918         #
919         # Remove the second prefix-info - expect no prefix-info in the adverts
920         #
921         self.pg0.ip6_ra_prefix(self.pg1.local_ip6,
922                                self.pg1.local_ip6_prefix_len,
923                                is_no=1)
924
925         self.pg0.ip6_ra_config(send_unicast=1)
926         self.send_and_expect_ra(self.pg0, p,
927                                 "RA with Prefix reverted to defaults",
928                                 dst_ip=ll)
929
930         #
931         # Reset the periodic advertisements back to default values
932         #
933         self.pg0.ip6_ra_config(no=1, suppress=1, send_unicast=0)
934
935
936 class TestICMPv6Echo(VppTestCase):
937     """ ICMPv6 Echo Test Case """
938
939     @classmethod
940     def setUpClass(cls):
941         super(TestICMPv6Echo, cls).setUpClass()
942
943     @classmethod
944     def tearDownClass(cls):
945         super(TestICMPv6Echo, cls).tearDownClass()
946
947     def setUp(self):
948         super(TestICMPv6Echo, self).setUp()
949
950         # create 1 pg interface
951         self.create_pg_interfaces(range(1))
952
953         for i in self.pg_interfaces:
954             i.admin_up()
955             i.config_ip6()
956             i.resolve_ndp()
957
958     def tearDown(self):
959         super(TestICMPv6Echo, self).tearDown()
960         for i in self.pg_interfaces:
961             i.unconfig_ip6()
962             i.ip6_disable()
963             i.admin_down()
964
965     def test_icmpv6_echo(self):
966         """ VPP replies to ICMPv6 Echo Request
967
968         Test scenario:
969
970             - Receive ICMPv6 Echo Request message on pg0 interface.
971             - Check outgoing ICMPv6 Echo Reply message on pg0 interface.
972         """
973
974         icmpv6_id = 0xb
975         icmpv6_seq = 5
976         icmpv6_data = b'\x0a' * 18
977         p_echo_request = (Ether(src=self.pg0.remote_mac,
978                                 dst=self.pg0.local_mac) /
979                           IPv6(src=self.pg0.remote_ip6,
980                                dst=self.pg0.local_ip6) /
981                           ICMPv6EchoRequest(
982                               id=icmpv6_id,
983                               seq=icmpv6_seq,
984                               data=icmpv6_data))
985
986         self.pg0.add_stream(p_echo_request)
987         self.pg_enable_capture(self.pg_interfaces)
988         self.pg_start()
989
990         rx = self.pg0.get_capture(1)
991         rx = rx[0]
992         ether = rx[Ether]
993         ipv6 = rx[IPv6]
994         icmpv6 = rx[ICMPv6EchoReply]
995
996         self.assertEqual(ether.src, self.pg0.local_mac)
997         self.assertEqual(ether.dst, self.pg0.remote_mac)
998
999         self.assertEqual(ipv6.src, self.pg0.local_ip6)
1000         self.assertEqual(ipv6.dst, self.pg0.remote_ip6)
1001
1002         self.assertEqual(
1003             icmp6types[icmpv6.type], "Echo Reply")
1004         self.assertEqual(icmpv6.id, icmpv6_id)
1005         self.assertEqual(icmpv6.seq, icmpv6_seq)
1006         self.assertEqual(icmpv6.data, icmpv6_data)
1007
1008
1009 class TestIPv6RD(TestIPv6ND):
1010     """ IPv6 Router Discovery Test Case """
1011
1012     @classmethod
1013     def setUpClass(cls):
1014         super(TestIPv6RD, cls).setUpClass()
1015
1016     @classmethod
1017     def tearDownClass(cls):
1018         super(TestIPv6RD, cls).tearDownClass()
1019
1020     def setUp(self):
1021         super(TestIPv6RD, self).setUp()
1022
1023         # create 2 pg interfaces
1024         self.create_pg_interfaces(range(2))
1025
1026         self.interfaces = list(self.pg_interfaces)
1027
1028         # setup all interfaces
1029         for i in self.interfaces:
1030             i.admin_up()
1031             i.config_ip6()
1032
1033     def tearDown(self):
1034         for i in self.interfaces:
1035             i.unconfig_ip6()
1036             i.admin_down()
1037         super(TestIPv6RD, self).tearDown()
1038
1039     def test_rd_send_router_solicitation(self):
1040         """ Verify router solicitation packets """
1041
1042         count = 2
1043         self.pg_enable_capture(self.pg_interfaces)
1044         self.pg_start()
1045         self.vapi.ip6nd_send_router_solicitation(self.pg1.sw_if_index,
1046                                                  mrc=count)
1047         rx_list = self.pg1.get_capture(count, timeout=3)
1048         self.assertEqual(len(rx_list), count)
1049         for packet in rx_list:
1050             self.assertEqual(packet.haslayer(IPv6), 1)
1051             self.assertEqual(packet[IPv6].haslayer(
1052                 ICMPv6ND_RS), 1)
1053             dst = ip6_normalize(packet[IPv6].dst)
1054             dst2 = ip6_normalize("ff02::2")
1055             self.assert_equal(dst, dst2)
1056             src = ip6_normalize(packet[IPv6].src)
1057             src2 = ip6_normalize(self.pg1.local_ip6_ll)
1058             self.assert_equal(src, src2)
1059             self.assertTrue(
1060                 bool(packet[ICMPv6ND_RS].haslayer(
1061                     ICMPv6NDOptSrcLLAddr)))
1062             self.assert_equal(
1063                 packet[ICMPv6NDOptSrcLLAddr].lladdr,
1064                 self.pg1.local_mac)
1065
1066     def verify_prefix_info(self, reported_prefix, prefix_option):
1067         prefix = IPv6Network(
1068             text_type(prefix_option.getfieldval("prefix") +
1069                       "/" +
1070                       text_type(prefix_option.getfieldval("prefixlen"))),
1071             strict=False)
1072         self.assert_equal(reported_prefix.prefix.network_address,
1073                           prefix.network_address)
1074         L = prefix_option.getfieldval("L")
1075         A = prefix_option.getfieldval("A")
1076         option_flags = (L << 7) | (A << 6)
1077         self.assert_equal(reported_prefix.flags, option_flags)
1078         self.assert_equal(reported_prefix.valid_time,
1079                           prefix_option.getfieldval("validlifetime"))
1080         self.assert_equal(reported_prefix.preferred_time,
1081                           prefix_option.getfieldval("preferredlifetime"))
1082
1083     def test_rd_receive_router_advertisement(self):
1084         """ Verify events triggered by received RA packets """
1085
1086         self.vapi.want_ip6_ra_events()
1087
1088         prefix_info_1 = ICMPv6NDOptPrefixInfo(
1089             prefix="1::2",
1090             prefixlen=50,
1091             validlifetime=200,
1092             preferredlifetime=500,
1093             L=1,
1094             A=1,
1095         )
1096
1097         prefix_info_2 = ICMPv6NDOptPrefixInfo(
1098             prefix="7::4",
1099             prefixlen=20,
1100             validlifetime=70,
1101             preferredlifetime=1000,
1102             L=1,
1103             A=0,
1104         )
1105
1106         p = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1107              IPv6(dst=self.pg1.local_ip6_ll,
1108                   src=mk_ll_addr(self.pg1.remote_mac)) /
1109              ICMPv6ND_RA() /
1110              prefix_info_1 /
1111              prefix_info_2)
1112         self.pg1.add_stream([p])
1113         self.pg_start()
1114
1115         ev = self.vapi.wait_for_event(10, "ip6_ra_event")
1116
1117         self.assert_equal(ev.current_hop_limit, 0)
1118         self.assert_equal(ev.flags, 8)
1119         self.assert_equal(ev.router_lifetime_in_sec, 1800)
1120         self.assert_equal(ev.neighbor_reachable_time_in_msec, 0)
1121         self.assert_equal(
1122             ev.time_in_msec_between_retransmitted_neighbor_solicitations, 0)
1123
1124         self.assert_equal(ev.n_prefixes, 2)
1125
1126         self.verify_prefix_info(ev.prefixes[0], prefix_info_1)
1127         self.verify_prefix_info(ev.prefixes[1], prefix_info_2)
1128
1129
1130 class TestIPv6RDControlPlane(TestIPv6ND):
1131     """ IPv6 Router Discovery Control Plane Test Case """
1132
1133     @classmethod
1134     def setUpClass(cls):
1135         super(TestIPv6RDControlPlane, cls).setUpClass()
1136
1137     @classmethod
1138     def tearDownClass(cls):
1139         super(TestIPv6RDControlPlane, cls).tearDownClass()
1140
1141     def setUp(self):
1142         super(TestIPv6RDControlPlane, self).setUp()
1143
1144         # create 1 pg interface
1145         self.create_pg_interfaces(range(1))
1146
1147         self.interfaces = list(self.pg_interfaces)
1148
1149         # setup all interfaces
1150         for i in self.interfaces:
1151             i.admin_up()
1152             i.config_ip6()
1153
1154     def tearDown(self):
1155         super(TestIPv6RDControlPlane, self).tearDown()
1156
1157     @staticmethod
1158     def create_ra_packet(pg, routerlifetime=None):
1159         src_ip = pg.remote_ip6_ll
1160         dst_ip = pg.local_ip6
1161         if routerlifetime is not None:
1162             ra = ICMPv6ND_RA(routerlifetime=routerlifetime)
1163         else:
1164             ra = ICMPv6ND_RA()
1165         p = (Ether(dst=pg.local_mac, src=pg.remote_mac) /
1166              IPv6(dst=dst_ip, src=src_ip) / ra)
1167         return p
1168
1169     @staticmethod
1170     def get_default_routes(fib):
1171         list = []
1172         for entry in fib:
1173             if entry.route.prefix.prefixlen == 0:
1174                 for path in entry.route.paths:
1175                     if path.sw_if_index != 0xFFFFFFFF:
1176                         defaut_route = {}
1177                         defaut_route['sw_if_index'] = path.sw_if_index
1178                         defaut_route['next_hop'] = path.nh.address.ip6
1179                         list.append(defaut_route)
1180         return list
1181
1182     @staticmethod
1183     def get_interface_addresses(fib, pg):
1184         list = []
1185         for entry in fib:
1186             if entry.route.prefix.prefixlen == 128:
1187                 path = entry.route.paths[0]
1188                 if path.sw_if_index == pg.sw_if_index:
1189                     list.append(str(entry.route.prefix.network_address))
1190         return list
1191
1192     def test_all(self):
1193         """ Test handling of SLAAC addresses and default routes """
1194
1195         fib = self.vapi.ip_route_dump(0, True)
1196         default_routes = self.get_default_routes(fib)
1197         initial_addresses = set(self.get_interface_addresses(fib, self.pg0))
1198         self.assertEqual(default_routes, [])
1199         router_address = IPv6Address(text_type(self.pg0.remote_ip6_ll))
1200
1201         self.vapi.ip6_nd_address_autoconfig(self.pg0.sw_if_index, 1, 1)
1202
1203         self.sleep(0.1)
1204
1205         # send RA
1206         packet = (self.create_ra_packet(
1207             self.pg0) / ICMPv6NDOptPrefixInfo(
1208             prefix="1::",
1209             prefixlen=64,
1210             validlifetime=2,
1211             preferredlifetime=2,
1212             L=1,
1213             A=1,
1214         ) / ICMPv6NDOptPrefixInfo(
1215             prefix="7::",
1216             prefixlen=20,
1217             validlifetime=1500,
1218             preferredlifetime=1000,
1219             L=1,
1220             A=0,
1221         ))
1222         self.pg0.add_stream([packet])
1223         self.pg_start()
1224
1225         self.sleep(0.1)
1226
1227         fib = self.vapi.ip_route_dump(0, True)
1228
1229         # check FIB for new address
1230         addresses = set(self.get_interface_addresses(fib, self.pg0))
1231         new_addresses = addresses.difference(initial_addresses)
1232         self.assertEqual(len(new_addresses), 1)
1233         prefix = IPv6Network(text_type("%s/%d" % (list(new_addresses)[0], 20)),
1234                              strict=False)
1235         self.assertEqual(prefix, IPv6Network(text_type('1::/20')))
1236
1237         # check FIB for new default route
1238         default_routes = self.get_default_routes(fib)
1239         self.assertEqual(len(default_routes), 1)
1240         dr = default_routes[0]
1241         self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1242         self.assertEqual(dr['next_hop'], router_address)
1243
1244         # send RA to delete default route
1245         packet = self.create_ra_packet(self.pg0, routerlifetime=0)
1246         self.pg0.add_stream([packet])
1247         self.pg_start()
1248
1249         self.sleep(0.1)
1250
1251         # check that default route is deleted
1252         fib = self.vapi.ip_route_dump(0, True)
1253         default_routes = self.get_default_routes(fib)
1254         self.assertEqual(len(default_routes), 0)
1255
1256         self.sleep(0.1)
1257
1258         # send RA
1259         packet = self.create_ra_packet(self.pg0)
1260         self.pg0.add_stream([packet])
1261         self.pg_start()
1262
1263         self.sleep(0.1)
1264
1265         # check FIB for new default route
1266         fib = self.vapi.ip_route_dump(0, True)
1267         default_routes = self.get_default_routes(fib)
1268         self.assertEqual(len(default_routes), 1)
1269         dr = default_routes[0]
1270         self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1271         self.assertEqual(dr['next_hop'], router_address)
1272
1273         # send RA, updating router lifetime to 1s
1274         packet = self.create_ra_packet(self.pg0, 1)
1275         self.pg0.add_stream([packet])
1276         self.pg_start()
1277
1278         self.sleep(0.1)
1279
1280         # check that default route still exists
1281         fib = self.vapi.ip_route_dump(0, True)
1282         default_routes = self.get_default_routes(fib)
1283         self.assertEqual(len(default_routes), 1)
1284         dr = default_routes[0]
1285         self.assertEqual(dr['sw_if_index'], self.pg0.sw_if_index)
1286         self.assertEqual(dr['next_hop'], router_address)
1287
1288         self.sleep(1)
1289
1290         # check that default route is deleted
1291         fib = self.vapi.ip_route_dump(0, True)
1292         default_routes = self.get_default_routes(fib)
1293         self.assertEqual(len(default_routes), 0)
1294
1295         # check FIB still contains the SLAAC address
1296         addresses = set(self.get_interface_addresses(fib, self.pg0))
1297         new_addresses = addresses.difference(initial_addresses)
1298
1299         self.assertEqual(len(new_addresses), 1)
1300         prefix = IPv6Network(text_type("%s/%d" % (list(new_addresses)[0], 20)),
1301                              strict=False)
1302         self.assertEqual(prefix, IPv6Network(text_type('1::/20')))
1303
1304         self.sleep(1)
1305
1306         # check that SLAAC address is deleted
1307         fib = self.vapi.ip_route_dump(0, True)
1308         addresses = set(self.get_interface_addresses(fib, self.pg0))
1309         new_addresses = addresses.difference(initial_addresses)
1310         self.assertEqual(len(new_addresses), 0)
1311
1312
1313 class IPv6NDProxyTest(TestIPv6ND):
1314     """ IPv6 ND ProxyTest Case """
1315
1316     @classmethod
1317     def setUpClass(cls):
1318         super(IPv6NDProxyTest, cls).setUpClass()
1319
1320     @classmethod
1321     def tearDownClass(cls):
1322         super(IPv6NDProxyTest, cls).tearDownClass()
1323
1324     def setUp(self):
1325         super(IPv6NDProxyTest, self).setUp()
1326
1327         # create 3 pg interfaces
1328         self.create_pg_interfaces(range(3))
1329
1330         # pg0 is the master interface, with the configured subnet
1331         self.pg0.admin_up()
1332         self.pg0.config_ip6()
1333         self.pg0.resolve_ndp()
1334
1335         self.pg1.ip6_enable()
1336         self.pg2.ip6_enable()
1337
1338     def tearDown(self):
1339         super(IPv6NDProxyTest, self).tearDown()
1340
1341     def test_nd_proxy(self):
1342         """ IPv6 Proxy ND """
1343
1344         #
1345         # Generate some hosts in the subnet that we are proxying
1346         #
1347         self.pg0.generate_remote_hosts(8)
1348
1349         nsma = in6_getnsma(inet_pton(AF_INET6, self.pg0.local_ip6))
1350         d = inet_ntop(AF_INET6, nsma)
1351
1352         #
1353         # Send an NS for one of those remote hosts on one of the proxy links
1354         # expect no response since it's from an address that is not
1355         # on the link that has the prefix configured
1356         #
1357         ns_pg1 = (Ether(dst=in6_getnsmac(nsma), src=self.pg1.remote_mac) /
1358                   IPv6(dst=d,
1359                        src=self.pg0._remote_hosts[2].ip6) /
1360                   ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1361                   ICMPv6NDOptSrcLLAddr(
1362                       lladdr=self.pg0._remote_hosts[2].mac))
1363
1364         self.send_and_assert_no_replies(self.pg1, ns_pg1, "Off link NS")
1365
1366         #
1367         # Add proxy support for the host
1368         #
1369         self.vapi.ip6nd_proxy_add_del(
1370             ip=inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1371             sw_if_index=self.pg1.sw_if_index)
1372
1373         #
1374         # try that NS again. this time we expect an NA back
1375         #
1376         self.send_and_expect_na(self.pg1, ns_pg1,
1377                                 "NS to proxy entry",
1378                                 dst_ip=self.pg0._remote_hosts[2].ip6,
1379                                 tgt_ip=self.pg0.local_ip6)
1380
1381         #
1382         # ... and that we have an entry in the ND cache
1383         #
1384         self.assertTrue(find_nbr(self,
1385                                  self.pg1.sw_if_index,
1386                                  self.pg0._remote_hosts[2].ip6))
1387
1388         #
1389         # ... and we can route traffic to it
1390         #
1391         t = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
1392              IPv6(dst=self.pg0._remote_hosts[2].ip6,
1393                   src=self.pg0.remote_ip6) /
1394              inet6.UDP(sport=10000, dport=20000) /
1395              Raw('\xa5' * 100))
1396
1397         self.pg0.add_stream(t)
1398         self.pg_enable_capture(self.pg_interfaces)
1399         self.pg_start()
1400         rx = self.pg1.get_capture(1)
1401         rx = rx[0]
1402
1403         self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1404         self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1405
1406         self.assertEqual(rx[IPv6].src,
1407                          t[IPv6].src)
1408         self.assertEqual(rx[IPv6].dst,
1409                          t[IPv6].dst)
1410
1411         #
1412         # Test we proxy for the host on the main interface
1413         #
1414         ns_pg0 = (Ether(dst=in6_getnsmac(nsma), src=self.pg0.remote_mac) /
1415                   IPv6(dst=d, src=self.pg0.remote_ip6) /
1416                   ICMPv6ND_NS(
1417                       tgt=self.pg0._remote_hosts[2].ip6) /
1418                   ICMPv6NDOptSrcLLAddr(
1419                       lladdr=self.pg0.remote_mac))
1420
1421         self.send_and_expect_na(self.pg0, ns_pg0,
1422                                 "NS to proxy entry on main",
1423                                 tgt_ip=self.pg0._remote_hosts[2].ip6,
1424                                 dst_ip=self.pg0.remote_ip6)
1425
1426         #
1427         # Setup and resolve proxy for another host on another interface
1428         #
1429         ns_pg2 = (Ether(dst=in6_getnsmac(nsma), src=self.pg2.remote_mac) /
1430                   IPv6(dst=d,
1431                        src=self.pg0._remote_hosts[3].ip6) /
1432                   ICMPv6ND_NS(tgt=self.pg0.local_ip6) /
1433                   ICMPv6NDOptSrcLLAddr(
1434                       lladdr=self.pg0._remote_hosts[2].mac))
1435
1436         self.vapi.ip6nd_proxy_add_del(
1437             ip=inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1438             sw_if_index=self.pg2.sw_if_index)
1439
1440         self.send_and_expect_na(self.pg2, ns_pg2,
1441                                 "NS to proxy entry other interface",
1442                                 dst_ip=self.pg0._remote_hosts[3].ip6,
1443                                 tgt_ip=self.pg0.local_ip6)
1444
1445         self.assertTrue(find_nbr(self,
1446                                  self.pg2.sw_if_index,
1447                                  self.pg0._remote_hosts[3].ip6))
1448
1449         #
1450         # hosts can communicate. pg2->pg1
1451         #
1452         t2 = (Ether(dst=self.pg2.local_mac,
1453                     src=self.pg0.remote_hosts[3].mac) /
1454               IPv6(dst=self.pg0._remote_hosts[2].ip6,
1455                    src=self.pg0._remote_hosts[3].ip6) /
1456               inet6.UDP(sport=10000, dport=20000) /
1457               Raw('\xa5' * 100))
1458
1459         self.pg2.add_stream(t2)
1460         self.pg_enable_capture(self.pg_interfaces)
1461         self.pg_start()
1462         rx = self.pg1.get_capture(1)
1463         rx = rx[0]
1464
1465         self.assertEqual(rx[Ether].dst, self.pg0._remote_hosts[2].mac)
1466         self.assertEqual(rx[Ether].src, self.pg1.local_mac)
1467
1468         self.assertEqual(rx[IPv6].src,
1469                          t2[IPv6].src)
1470         self.assertEqual(rx[IPv6].dst,
1471                          t2[IPv6].dst)
1472
1473         #
1474         # remove the proxy configs
1475         #
1476         self.vapi.ip6nd_proxy_add_del(
1477             ip=inet_pton(AF_INET6, self.pg0._remote_hosts[2].ip6),
1478             sw_if_index=self.pg1.sw_if_index, is_del=1)
1479         self.vapi.ip6nd_proxy_add_del(
1480             ip=inet_pton(AF_INET6, self.pg0._remote_hosts[3].ip6),
1481             sw_if_index=self.pg2.sw_if_index, is_del=1)
1482
1483         self.assertFalse(find_nbr(self,
1484                                   self.pg2.sw_if_index,
1485                                   self.pg0._remote_hosts[3].ip6))
1486         self.assertFalse(find_nbr(self,
1487                                   self.pg1.sw_if_index,
1488                                   self.pg0._remote_hosts[2].ip6))
1489
1490         #
1491         # no longer proxy-ing...
1492         #
1493         self.send_and_assert_no_replies(self.pg0, ns_pg0, "Proxy unconfigured")
1494         self.send_and_assert_no_replies(self.pg1, ns_pg1, "Proxy unconfigured")
1495         self.send_and_assert_no_replies(self.pg2, ns_pg2, "Proxy unconfigured")
1496
1497         #
1498         # no longer forwarding. traffic generates NS out of the glean/main
1499         # interface
1500         #
1501         self.pg2.add_stream(t2)
1502         self.pg_enable_capture(self.pg_interfaces)
1503         self.pg_start()
1504
1505         rx = self.pg0.get_capture(1)
1506
1507         self.assertTrue(rx[0].haslayer(ICMPv6ND_NS))
1508
1509
1510 class TestIPNull(VppTestCase):
1511     """ IPv6 routes via NULL """
1512
1513     @classmethod
1514     def setUpClass(cls):
1515         super(TestIPNull, cls).setUpClass()
1516
1517     @classmethod
1518     def tearDownClass(cls):
1519         super(TestIPNull, cls).tearDownClass()
1520
1521     def setUp(self):
1522         super(TestIPNull, self).setUp()
1523
1524         # create 2 pg interfaces
1525         self.create_pg_interfaces(range(1))
1526
1527         for i in self.pg_interfaces:
1528             i.admin_up()
1529             i.config_ip6()
1530             i.resolve_ndp()
1531
1532     def tearDown(self):
1533         super(TestIPNull, self).tearDown()
1534         for i in self.pg_interfaces:
1535             i.unconfig_ip6()
1536             i.admin_down()
1537
1538     def test_ip_null(self):
1539         """ IP NULL route """
1540
1541         p = (Ether(src=self.pg0.remote_mac,
1542                    dst=self.pg0.local_mac) /
1543              IPv6(src=self.pg0.remote_ip6, dst="2001::1") /
1544              inet6.UDP(sport=1234, dport=1234) /
1545              Raw('\xa5' * 100))
1546
1547         #
1548         # A route via IP NULL that will reply with ICMP unreachables
1549         #
1550         ip_unreach = VppIpRoute(
1551             self, "2001::", 64,
1552             [VppRoutePath("::", 0xffffffff,
1553                           type=FibPathType.FIB_PATH_TYPE_ICMP_UNREACH)])
1554         ip_unreach.add_vpp_config()
1555
1556         self.pg0.add_stream(p)
1557         self.pg_enable_capture(self.pg_interfaces)
1558         self.pg_start()
1559
1560         rx = self.pg0.get_capture(1)
1561         rx = rx[0]
1562         icmp = rx[ICMPv6DestUnreach]
1563
1564         # 0 = "No route to destination"
1565         self.assertEqual(icmp.code, 0)
1566
1567         # ICMP is rate limited. pause a bit
1568         self.sleep(1)
1569
1570         #
1571         # A route via IP NULL that will reply with ICMP prohibited
1572         #
1573         ip_prohibit = VppIpRoute(
1574             self, "2001::1", 128,
1575             [VppRoutePath("::", 0xffffffff,
1576                           type=FibPathType.FIB_PATH_TYPE_ICMP_PROHIBIT)])
1577         ip_prohibit.add_vpp_config()
1578
1579         self.pg0.add_stream(p)
1580         self.pg_enable_capture(self.pg_interfaces)
1581         self.pg_start()
1582
1583         rx = self.pg0.get_capture(1)
1584         rx = rx[0]
1585         icmp = rx[ICMPv6DestUnreach]
1586
1587         # 1 = "Communication with destination administratively prohibited"
1588         self.assertEqual(icmp.code, 1)
1589
1590
1591 class TestIPDisabled(VppTestCase):
1592     """ IPv6 disabled """
1593
1594     @classmethod
1595     def setUpClass(cls):
1596         super(TestIPDisabled, cls).setUpClass()
1597
1598     @classmethod
1599     def tearDownClass(cls):
1600         super(TestIPDisabled, cls).tearDownClass()
1601
1602     def setUp(self):
1603         super(TestIPDisabled, self).setUp()
1604
1605         # create 2 pg interfaces
1606         self.create_pg_interfaces(range(2))
1607
1608         # PG0 is IP enabled
1609         self.pg0.admin_up()
1610         self.pg0.config_ip6()
1611         self.pg0.resolve_ndp()
1612
1613         # PG 1 is not IP enabled
1614         self.pg1.admin_up()
1615
1616     def tearDown(self):
1617         super(TestIPDisabled, self).tearDown()
1618         for i in self.pg_interfaces:
1619             i.unconfig_ip4()
1620             i.admin_down()
1621
1622     def test_ip_disabled(self):
1623         """ IP Disabled """
1624
1625         #
1626         # An (S,G).
1627         # one accepting interface, pg0, 2 forwarding interfaces
1628         #
1629         route_ff_01 = VppIpMRoute(
1630             self,
1631             "::",
1632             "ffef::1", 128,
1633             MRouteEntryFlags.MFIB_ENTRY_FLAG_NONE,
1634             [VppMRoutePath(self.pg1.sw_if_index,
1635                            MRouteItfFlags.MFIB_ITF_FLAG_ACCEPT),
1636              VppMRoutePath(self.pg0.sw_if_index,
1637                            MRouteItfFlags.MFIB_ITF_FLAG_FORWARD)])
1638         route_ff_01.add_vpp_config()
1639
1640         pu = (Ether(src=self.pg1.remote_mac,
1641                     dst=self.pg1.local_mac) /
1642               IPv6(src="2001::1", dst=self.pg0.remote_ip6) /
1643               inet6.UDP(sport=1234, dport=1234) /
1644               Raw('\xa5' * 100))
1645         pm = (Ether(src=self.pg1.remote_mac,
1646                     dst=self.pg1.local_mac) /
1647               IPv6(src="2001::1", dst="ffef::1") /
1648               inet6.UDP(sport=1234, dport=1234) /
1649               Raw('\xa5' * 100))
1650
1651         #
1652         # PG1 does not forward IP traffic
1653         #
1654         self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1655         self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1656
1657         #
1658         # IP enable PG1
1659         #
1660         self.pg1.config_ip6()
1661
1662         #
1663         # Now we get packets through
1664         #
1665         self.pg1.add_stream(pu)
1666         self.pg_enable_capture(self.pg_interfaces)
1667         self.pg_start()
1668         rx = self.pg0.get_capture(1)
1669
1670         self.pg1.add_stream(pm)
1671         self.pg_enable_capture(self.pg_interfaces)
1672         self.pg_start()
1673         rx = self.pg0.get_capture(1)
1674
1675         #
1676         # Disable PG1
1677         #
1678         self.pg1.unconfig_ip6()
1679
1680         #
1681         # PG1 does not forward IP traffic
1682         #
1683         self.send_and_assert_no_replies(self.pg1, pu, "IPv6 disabled")
1684         self.send_and_assert_no_replies(self.pg1, pm, "IPv6 disabled")
1685
1686
1687 class TestIP6LoadBalance(VppTestCase):
1688     """ IPv6 Load-Balancing """
1689
1690     @classmethod
1691     def setUpClass(cls):
1692         super(TestIP6LoadBalance, cls).setUpClass()
1693
1694     @classmethod
1695     def tearDownClass(cls):
1696         super(TestIP6LoadBalance, cls).tearDownClass()
1697
1698     def setUp(self):
1699         super(TestIP6LoadBalance, self).setUp()
1700
1701         self.create_pg_interfaces(range(5))
1702
1703         mpls_tbl = VppMplsTable(self, 0)
1704         mpls_tbl.add_vpp_config()
1705
1706         for i in self.pg_interfaces:
1707             i.admin_up()
1708             i.config_ip6()
1709             i.resolve_ndp()
1710             i.enable_mpls()
1711
1712     def tearDown(self):
1713         for i in self.pg_interfaces:
1714             i.unconfig_ip6()
1715             i.admin_down()
1716             i.disable_mpls()
1717         super(TestIP6LoadBalance, self).tearDown()
1718
1719     def pg_send(self, input, pkts):
1720         self.vapi.cli("clear trace")
1721         input.add_stream(pkts)
1722         self.pg_enable_capture(self.pg_interfaces)
1723         self.pg_start()
1724
1725     def send_and_expect_load_balancing(self, input, pkts, outputs):
1726         self.pg_send(input, pkts)
1727         for oo in outputs:
1728             rx = oo._get_capture(1)
1729             self.assertNotEqual(0, len(rx))
1730
1731     def send_and_expect_one_itf(self, input, pkts, itf):
1732         self.pg_send(input, pkts)
1733         rx = itf.get_capture(len(pkts))
1734
1735     def test_ip6_load_balance(self):
1736         """ IPv6 Load-Balancing """
1737
1738         #
1739         # An array of packets that differ only in the destination port
1740         #  - IP only
1741         #  - MPLS EOS
1742         #  - MPLS non-EOS
1743         #  - MPLS non-EOS with an entropy label
1744         #
1745         port_ip_pkts = []
1746         port_mpls_pkts = []
1747         port_mpls_neos_pkts = []
1748         port_ent_pkts = []
1749
1750         #
1751         # An array of packets that differ only in the source address
1752         #
1753         src_ip_pkts = []
1754         src_mpls_pkts = []
1755
1756         for ii in range(NUM_PKTS):
1757             port_ip_hdr = (
1758                 IPv6(dst="3000::1", src="3000:1::1") /
1759                 inet6.UDP(sport=1234, dport=1234 + ii) /
1760                 Raw('\xa5' * 100))
1761             port_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1762                                        dst=self.pg0.local_mac) /
1763                                  port_ip_hdr))
1764             port_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1765                                          dst=self.pg0.local_mac) /
1766                                    MPLS(label=66, ttl=2) /
1767                                    port_ip_hdr))
1768             port_mpls_neos_pkts.append((Ether(src=self.pg0.remote_mac,
1769                                               dst=self.pg0.local_mac) /
1770                                         MPLS(label=67, ttl=2) /
1771                                         MPLS(label=77, ttl=2) /
1772                                         port_ip_hdr))
1773             port_ent_pkts.append((Ether(src=self.pg0.remote_mac,
1774                                         dst=self.pg0.local_mac) /
1775                                   MPLS(label=67, ttl=2) /
1776                                   MPLS(label=14, ttl=2) /
1777                                   MPLS(label=999, ttl=2) /
1778                                   port_ip_hdr))
1779             src_ip_hdr = (
1780                 IPv6(dst="3000::1", src="3000:1::%d" % ii) /
1781                 inet6.UDP(sport=1234, dport=1234) /
1782                 Raw('\xa5' * 100))
1783             src_ip_pkts.append((Ether(src=self.pg0.remote_mac,
1784                                       dst=self.pg0.local_mac) /
1785                                 src_ip_hdr))
1786             src_mpls_pkts.append((Ether(src=self.pg0.remote_mac,
1787                                         dst=self.pg0.local_mac) /
1788                                   MPLS(label=66, ttl=2) /
1789                                   src_ip_hdr))
1790
1791         #
1792         # A route for the IP packets
1793         #
1794         route_3000_1 = VppIpRoute(self, "3000::1", 128,
1795                                   [VppRoutePath(self.pg1.remote_ip6,
1796                                                 self.pg1.sw_if_index),
1797                                    VppRoutePath(self.pg2.remote_ip6,
1798                                                 self.pg2.sw_if_index)])
1799         route_3000_1.add_vpp_config()
1800
1801         #
1802         # a local-label for the EOS packets
1803         #
1804         binding = VppMplsIpBind(self, 66, "3000::1", 128, is_ip6=1)
1805         binding.add_vpp_config()
1806
1807         #
1808         # An MPLS route for the non-EOS packets
1809         #
1810         route_67 = VppMplsRoute(self, 67, 0,
1811                                 [VppRoutePath(self.pg1.remote_ip6,
1812                                               self.pg1.sw_if_index,
1813                                               labels=[67]),
1814                                  VppRoutePath(self.pg2.remote_ip6,
1815                                               self.pg2.sw_if_index,
1816                                               labels=[67])])
1817         route_67.add_vpp_config()
1818
1819         #
1820         # inject the packet on pg0 - expect load-balancing across the 2 paths
1821         #  - since the default hash config is to use IP src,dst and port
1822         #    src,dst
1823         # We are not going to ensure equal amounts of packets across each link,
1824         # since the hash algorithm is statistical and therefore this can never
1825         # be guaranteed. But with 64 different packets we do expect some
1826         # balancing. So instead just ensure there is traffic on each link.
1827         #
1828         self.send_and_expect_load_balancing(self.pg0, port_ip_pkts,
1829                                             [self.pg1, self.pg2])
1830         self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1831                                             [self.pg1, self.pg2])
1832         self.send_and_expect_load_balancing(self.pg0, port_mpls_pkts,
1833                                             [self.pg1, self.pg2])
1834         self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1835                                             [self.pg1, self.pg2])
1836         self.send_and_expect_load_balancing(self.pg0, port_mpls_neos_pkts,
1837                                             [self.pg1, self.pg2])
1838
1839         #
1840         # The packets with Entropy label in should not load-balance,
1841         # since the Entropy value is fixed.
1842         #
1843         self.send_and_expect_one_itf(self.pg0, port_ent_pkts, self.pg1)
1844
1845         #
1846         # change the flow hash config so it's only IP src,dst
1847         #  - now only the stream with differing source address will
1848         #    load-balance
1849         #
1850         self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=0, dport=0,
1851                                    is_ipv6=1)
1852
1853         self.send_and_expect_load_balancing(self.pg0, src_ip_pkts,
1854                                             [self.pg1, self.pg2])
1855         self.send_and_expect_load_balancing(self.pg0, src_mpls_pkts,
1856                                             [self.pg1, self.pg2])
1857         self.send_and_expect_one_itf(self.pg0, port_ip_pkts, self.pg2)
1858
1859         #
1860         # change the flow hash config back to defaults
1861         #
1862         self.vapi.set_ip_flow_hash(vrf_id=0, src=1, dst=1, sport=1, dport=1,
1863                                    is_ipv6=1)
1864
1865         #
1866         # Recursive prefixes
1867         #  - testing that 2 stages of load-balancing occurs and there is no
1868         #    polarisation (i.e. only 2 of 4 paths are used)
1869         #
1870         port_pkts = []
1871         src_pkts = []
1872
1873         for ii in range(257):
1874             port_pkts.append((Ether(src=self.pg0.remote_mac,
1875                                     dst=self.pg0.local_mac) /
1876                               IPv6(dst="4000::1",
1877                                    src="4000:1::1") /
1878                               inet6.UDP(sport=1234,
1879                                         dport=1234 + ii) /
1880                               Raw('\xa5' * 100)))
1881             src_pkts.append((Ether(src=self.pg0.remote_mac,
1882                                    dst=self.pg0.local_mac) /
1883                              IPv6(dst="4000::1",
1884                                   src="4000:1::%d" % ii) /
1885                              inet6.UDP(sport=1234, dport=1234) /
1886                              Raw('\xa5' * 100)))
1887
1888         route_3000_2 = VppIpRoute(self, "3000::2", 128,
1889                                   [VppRoutePath(self.pg3.remote_ip6,
1890                                                 self.pg3.sw_if_index),
1891                                    VppRoutePath(self.pg4.remote_ip6,
1892                                                 self.pg4.sw_if_index)])
1893         route_3000_2.add_vpp_config()
1894
1895         route_4000_1 = VppIpRoute(self, "4000::1", 128,
1896                                   [VppRoutePath("3000::1",
1897                                                 0xffffffff),
1898                                    VppRoutePath("3000::2",
1899                                                 0xffffffff)])
1900         route_4000_1.add_vpp_config()
1901
1902         #
1903         # inject the packet on pg0 - expect load-balancing across all 4 paths
1904         #
1905         self.vapi.cli("clear trace")
1906         self.send_and_expect_load_balancing(self.pg0, port_pkts,
1907                                             [self.pg1, self.pg2,
1908                                              self.pg3, self.pg4])
1909         self.send_and_expect_load_balancing(self.pg0, src_pkts,
1910                                             [self.pg1, self.pg2,
1911                                              self.pg3, self.pg4])
1912
1913         #
1914         # Recursive prefixes
1915         #  - testing that 2 stages of load-balancing no choices
1916         #
1917         port_pkts = []
1918
1919         for ii in range(257):
1920             port_pkts.append((Ether(src=self.pg0.remote_mac,
1921                                     dst=self.pg0.local_mac) /
1922                               IPv6(dst="6000::1",
1923                                    src="6000:1::1") /
1924                               inet6.UDP(sport=1234,
1925                                         dport=1234 + ii) /
1926                               Raw('\xa5' * 100)))
1927
1928         route_5000_2 = VppIpRoute(self, "5000::2", 128,
1929                                   [VppRoutePath(self.pg3.remote_ip6,
1930                                                 self.pg3.sw_if_index)])
1931         route_5000_2.add_vpp_config()
1932
1933         route_6000_1 = VppIpRoute(self, "6000::1", 128,
1934                                   [VppRoutePath("5000::2",
1935                                                 0xffffffff)])
1936         route_6000_1.add_vpp_config()
1937
1938         #
1939         # inject the packet on pg0 - expect load-balancing across all 4 paths
1940         #
1941         self.vapi.cli("clear trace")
1942         self.send_and_expect_one_itf(self.pg0, port_pkts, self.pg3)
1943
1944
1945 class TestIP6Punt(VppTestCase):
1946     """ IPv6 Punt Police/Redirect """
1947
1948     @classmethod
1949     def setUpClass(cls):
1950         super(TestIP6Punt, cls).setUpClass()
1951
1952     @classmethod
1953     def tearDownClass(cls):
1954         super(TestIP6Punt, cls).tearDownClass()
1955
1956     def setUp(self):
1957         super(TestIP6Punt, self).setUp()
1958
1959         self.create_pg_interfaces(range(4))
1960
1961         for i in self.pg_interfaces:
1962             i.admin_up()
1963             i.config_ip6()
1964             i.resolve_ndp()
1965
1966     def tearDown(self):
1967         super(TestIP6Punt, self).tearDown()
1968         for i in self.pg_interfaces:
1969             i.unconfig_ip6()
1970             i.admin_down()
1971
1972     def test_ip_punt(self):
1973         """ IP6 punt police and redirect """
1974
1975         p = (Ether(src=self.pg0.remote_mac,
1976                    dst=self.pg0.local_mac) /
1977              IPv6(src=self.pg0.remote_ip6,
1978                   dst=self.pg0.local_ip6) /
1979              inet6.TCP(sport=1234, dport=1234) /
1980              Raw('\xa5' * 100))
1981
1982         pkts = p * 1025
1983
1984         #
1985         # Configure a punt redirect via pg1.
1986         #
1987         nh_addr = self.pg1.remote_ip6
1988         self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
1989                                    self.pg1.sw_if_index,
1990                                    nh_addr)
1991
1992         self.send_and_expect(self.pg0, pkts, self.pg1)
1993
1994         #
1995         # add a policer
1996         #
1997         policer = self.vapi.policer_add_del(b"ip6-punt", 400, 0, 10, 0,
1998                                             rate_type=1)
1999         self.vapi.ip_punt_police(policer.policer_index, is_ip6=1)
2000
2001         self.vapi.cli("clear trace")
2002         self.pg0.add_stream(pkts)
2003         self.pg_enable_capture(self.pg_interfaces)
2004         self.pg_start()
2005
2006         #
2007         # the number of packet received should be greater than 0,
2008         # but not equal to the number sent, since some were policed
2009         #
2010         rx = self.pg1._get_capture(1)
2011         self.assertGreater(len(rx), 0)
2012         self.assertLess(len(rx), len(pkts))
2013
2014         #
2015         # remove the policer. back to full rx
2016         #
2017         self.vapi.ip_punt_police(policer.policer_index, is_add=0, is_ip6=1)
2018         self.vapi.policer_add_del(b"ip6-punt", 400, 0, 10, 0,
2019                                   rate_type=1, is_add=0)
2020         self.send_and_expect(self.pg0, pkts, self.pg1)
2021
2022         #
2023         # remove the redirect. expect full drop.
2024         #
2025         self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2026                                    self.pg1.sw_if_index,
2027                                    nh_addr,
2028                                    is_add=0)
2029         self.send_and_assert_no_replies(self.pg0, pkts,
2030                                         "IP no punt config")
2031
2032         #
2033         # Add a redirect that is not input port selective
2034         #
2035         self.vapi.ip_punt_redirect(0xffffffff,
2036                                    self.pg1.sw_if_index,
2037                                    nh_addr)
2038         self.send_and_expect(self.pg0, pkts, self.pg1)
2039
2040         self.vapi.ip_punt_redirect(0xffffffff,
2041                                    self.pg1.sw_if_index,
2042                                    nh_addr,
2043                                    is_add=0)
2044
2045     def test_ip_punt_dump(self):
2046         """ IP6 punt redirect dump"""
2047
2048         #
2049         # Configure a punt redirects
2050         #
2051         nh_addr = self.pg3.remote_ip6
2052         self.vapi.ip_punt_redirect(self.pg0.sw_if_index,
2053                                    self.pg3.sw_if_index,
2054                                    nh_addr)
2055         self.vapi.ip_punt_redirect(self.pg1.sw_if_index,
2056                                    self.pg3.sw_if_index,
2057                                    nh_addr)
2058         self.vapi.ip_punt_redirect(self.pg2.sw_if_index,
2059                                    self.pg3.sw_if_index,
2060                                    '0::0')
2061
2062         #
2063         # Dump pg0 punt redirects
2064         #
2065         punts = self.vapi.ip_punt_redirect_dump(self.pg0.sw_if_index,
2066                                                 is_ipv6=1)
2067         for p in punts:
2068             self.assertEqual(p.punt.rx_sw_if_index, self.pg0.sw_if_index)
2069
2070         #
2071         # Dump punt redirects for all interfaces
2072         #
2073         punts = self.vapi.ip_punt_redirect_dump(0xffffffff, is_ipv6=1)
2074         self.assertEqual(len(punts), 3)
2075         for p in punts:
2076             self.assertEqual(p.punt.tx_sw_if_index, self.pg3.sw_if_index)
2077         self.assertNotEqual(punts[1].punt.nh, self.pg3.remote_ip6)
2078         self.assertEqual(str(punts[2].punt.nh), '::')
2079
2080
2081 class TestIPDeag(VppTestCase):
2082     """ IPv6 Deaggregate Routes """
2083
2084     @classmethod
2085     def setUpClass(cls):
2086         super(TestIPDeag, cls).setUpClass()
2087
2088     @classmethod
2089     def tearDownClass(cls):
2090         super(TestIPDeag, cls).tearDownClass()
2091
2092     def setUp(self):
2093         super(TestIPDeag, self).setUp()
2094
2095         self.create_pg_interfaces(range(3))
2096
2097         for i in self.pg_interfaces:
2098             i.admin_up()
2099             i.config_ip6()
2100             i.resolve_ndp()
2101
2102     def tearDown(self):
2103         super(TestIPDeag, self).tearDown()
2104         for i in self.pg_interfaces:
2105             i.unconfig_ip6()
2106             i.admin_down()
2107
2108     def test_ip_deag(self):
2109         """ IP Deag Routes """
2110
2111         #
2112         # Create a table to be used for:
2113         #  1 - another destination address lookup
2114         #  2 - a source address lookup
2115         #
2116         table_dst = VppIpTable(self, 1, is_ip6=1)
2117         table_src = VppIpTable(self, 2, is_ip6=1)
2118         table_dst.add_vpp_config()
2119         table_src.add_vpp_config()
2120
2121         #
2122         # Add a route in the default table to point to a deag/
2123         # second lookup in each of these tables
2124         #
2125         route_to_dst = VppIpRoute(self, "1::1", 128,
2126                                   [VppRoutePath("::",
2127                                                 0xffffffff,
2128                                                 nh_table_id=1)])
2129         route_to_src = VppIpRoute(
2130             self, "1::2", 128,
2131             [VppRoutePath("::",
2132                           0xffffffff,
2133                           nh_table_id=2,
2134                           type=FibPathType.FIB_PATH_TYPE_SOURCE_LOOKUP)])
2135
2136         route_to_dst.add_vpp_config()
2137         route_to_src.add_vpp_config()
2138
2139         #
2140         # packets to these destination are dropped, since they'll
2141         # hit the respective default routes in the second table
2142         #
2143         p_dst = (Ether(src=self.pg0.remote_mac,
2144                        dst=self.pg0.local_mac) /
2145                  IPv6(src="5::5", dst="1::1") /
2146                  inet6.TCP(sport=1234, dport=1234) /
2147                  Raw('\xa5' * 100))
2148         p_src = (Ether(src=self.pg0.remote_mac,
2149                        dst=self.pg0.local_mac) /
2150                  IPv6(src="2::2", dst="1::2") /
2151                  inet6.TCP(sport=1234, dport=1234) /
2152                  Raw('\xa5' * 100))
2153         pkts_dst = p_dst * 257
2154         pkts_src = p_src * 257
2155
2156         self.send_and_assert_no_replies(self.pg0, pkts_dst,
2157                                         "IP in dst table")
2158         self.send_and_assert_no_replies(self.pg0, pkts_src,
2159                                         "IP in src table")
2160
2161         #
2162         # add a route in the dst table to forward via pg1
2163         #
2164         route_in_dst = VppIpRoute(self, "1::1", 128,
2165                                   [VppRoutePath(self.pg1.remote_ip6,
2166                                                 self.pg1.sw_if_index)],
2167                                   table_id=1)
2168         route_in_dst.add_vpp_config()
2169
2170         self.send_and_expect(self.pg0, pkts_dst, self.pg1)
2171
2172         #
2173         # add a route in the src table to forward via pg2
2174         #
2175         route_in_src = VppIpRoute(self, "2::2", 128,
2176                                   [VppRoutePath(self.pg2.remote_ip6,
2177                                                 self.pg2.sw_if_index)],
2178                                   table_id=2)
2179         route_in_src.add_vpp_config()
2180         self.send_and_expect(self.pg0, pkts_src, self.pg2)
2181
2182         #
2183         # loop in the lookup DP
2184         #
2185         route_loop = VppIpRoute(self, "3::3", 128,
2186                                 [VppRoutePath("::",
2187                                               0xffffffff)])
2188         route_loop.add_vpp_config()
2189
2190         p_l = (Ether(src=self.pg0.remote_mac,
2191                      dst=self.pg0.local_mac) /
2192                IPv6(src="3::4", dst="3::3") /
2193                inet6.TCP(sport=1234, dport=1234) /
2194                Raw('\xa5' * 100))
2195
2196         self.send_and_assert_no_replies(self.pg0, p_l * 257,
2197                                         "IP lookup loop")
2198
2199
2200 class TestIP6Input(VppTestCase):
2201     """ IPv6 Input Exception Test Cases """
2202
2203     @classmethod
2204     def setUpClass(cls):
2205         super(TestIP6Input, cls).setUpClass()
2206
2207     @classmethod
2208     def tearDownClass(cls):
2209         super(TestIP6Input, cls).tearDownClass()
2210
2211     def setUp(self):
2212         super(TestIP6Input, self).setUp()
2213
2214         self.create_pg_interfaces(range(2))
2215
2216         for i in self.pg_interfaces:
2217             i.admin_up()
2218             i.config_ip6()
2219             i.resolve_ndp()
2220
2221     def tearDown(self):
2222         super(TestIP6Input, self).tearDown()
2223         for i in self.pg_interfaces:
2224             i.unconfig_ip6()
2225             i.admin_down()
2226
2227     def test_ip_input_icmp_reply(self):
2228         """ IP6 Input Exception - Return ICMP (3,0) """
2229         #
2230         # hop limit - ICMP replies
2231         #
2232         p_version = (Ether(src=self.pg0.remote_mac,
2233                            dst=self.pg0.local_mac) /
2234                      IPv6(src=self.pg0.remote_ip6,
2235                           dst=self.pg1.remote_ip6,
2236                           hlim=1) /
2237                      inet6.UDP(sport=1234, dport=1234) /
2238                      Raw('\xa5' * 100))
2239
2240         rx = self.send_and_expect(self.pg0, p_version * NUM_PKTS, self.pg0)
2241         rx = rx[0]
2242         icmp = rx[ICMPv6TimeExceeded]
2243
2244         # 0: "hop limit exceeded in transit",
2245         self.assertEqual((icmp.type, icmp.code), (3, 0))
2246
2247     icmpv6_data = '\x0a' * 18
2248     all_0s = "::"
2249     all_1s = "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF"
2250
2251     @parameterized.expand([
2252         # Name, src, dst, l4proto, msg, timeout
2253         ("src='iface',   dst='iface'", None, None,
2254          inet6.UDP(sport=1234, dport=1234), "funky version", None),
2255         ("src='All 0's', dst='iface'", all_0s, None,
2256          ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2257         ("src='iface',   dst='All 0's'", None, all_0s,
2258          ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2259         ("src='All 1's', dst='iface'", all_1s, None,
2260          ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2261         ("src='iface',   dst='All 1's'", None, all_1s,
2262          ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2263         ("src='All 1's', dst='All 1's'", all_1s, all_1s,
2264          ICMPv6EchoRequest(id=0xb, seq=5, data=icmpv6_data), None, 0.1),
2265
2266     ])
2267     def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
2268
2269         self._testMethodDoc = 'IPv6 Input Exception - %s' % name
2270
2271         p_version = (Ether(src=self.pg0.remote_mac,
2272                            dst=self.pg0.local_mac) /
2273                      IPv6(src=src or self.pg0.remote_ip6,
2274                           dst=dst or self.pg1.remote_ip6,
2275                           version=3) /
2276                      l4 /
2277                      Raw('\xa5' * 100))
2278
2279         self.send_and_assert_no_replies(self.pg0, p_version * NUM_PKTS,
2280                                         remark=msg or "",
2281                                         timeout=timeout)
2282
2283     def test_hop_by_hop(self):
2284         """ Hop-by-hop header test """
2285
2286         p = (Ether(src=self.pg0.remote_mac,
2287                    dst=self.pg0.local_mac) /
2288              IPv6(src=self.pg0.remote_ip6, dst=self.pg0.local_ip6) /
2289              IPv6ExtHdrHopByHop() /
2290              inet6.UDP(sport=1234, dport=1234) /
2291              Raw('\xa5' * 100))
2292
2293         self.pg0.add_stream(p)
2294         self.pg_enable_capture(self.pg_interfaces)
2295         self.pg_start()
2296
2297 if __name__ == '__main__':
2298     unittest.main(testRunner=VppTestRunner)