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