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