tests: Use errno value rather than a specific int
[vpp.git] / test / test_ip6.py
index 01a6d94..87509f3 100644 (file)
@@ -5,7 +5,6 @@ from socket import inet_pton, inet_ntop
 import unittest
 
 from parameterized import parameterized
-import scapy.compat
 import scapy.layers.inet6 as inet6
 from scapy.layers.inet import UDP, IP
 from scapy.contrib.mpls import MPLS
@@ -26,7 +25,6 @@ from scapy.layers.inet6 import (
     ICMPv6EchoReply,
     IPv6ExtHdrHopByHop,
     ICMPv6MLReport2,
-    ICMPv6MLDMultAddrRec,
 )
 from scapy.layers.l2 import Ether, Dot1Q, GRE
 from scapy.packet import Raw
@@ -35,14 +33,14 @@ from scapy.utils6 import (
     in6_getnsmac,
     in6_ptop,
     in6_islladdr,
-    in6_mactoifaceid,
 )
 from six import moves
 
-from framework import VppTestCase, VppTestRunner, tag_run_solo
+from framework import VppTestCase
+from asfframework import VppTestRunner, tag_run_solo
 from util import ppp, ip6_normalize, mk_ll_addr
 from vpp_papi import VppEnum
-from vpp_ip import DpoProto, VppIpPuntPolicer, VppIpPuntRedirect, VppIpPathMtu
+from vpp_ip import VppIpPuntPolicer, VppIpPuntRedirect, VppIpPathMtu
 from vpp_ip_route import (
     VppIpRoute,
     VppRoutePath,
@@ -141,7 +139,7 @@ class TestIPv6ND(VppTestCase):
         self.assertEqual(in6_ptop(ns.tgt), in6_ptop(tgt_ip))
 
         # packet is from the router's local address
-        self.assertEqual(in6_ptop(rx[IPv6].src), intf.local_ip6)
+        self.assertEqual(in6_ptop(rx[IPv6].src), intf.local_ip6_ll)
 
         # Src link-layer options should have the router's MAC
         sll = rx[ICMPv6NDOptSrcLLAddr]
@@ -274,7 +272,7 @@ class TestIPv6(TestIPv6ND):
 
     def tearDown(self):
         """Run standard test teardown and log ``show ip6 neighbors``."""
-        for i in self.interfaces:
+        for i in reversed(self.interfaces):
             i.unconfig_ip6()
             i.admin_down()
         for i in self.sub_interfaces:
@@ -779,6 +777,85 @@ class TestIPv6(TestIPv6ND):
         rx = rx[0]
         self.validate_ra(intf, rx, dst_ip, src_ip=src_ip, pi_opt=opt)
 
+    def test_ip6_ra_dump(self):
+        """IPv6 RA dump"""
+
+        # Dump IPv6 RA for all interfaces
+        ip6_ra_dump = self.vapi.sw_interface_ip6nd_ra_dump(sw_if_index=0xFFFFFFFF)
+        self.assertEqual(len(ip6_ra_dump), len(self.interfaces))
+
+        for ip6_ra in ip6_ra_dump:
+            self.assertFalse(ip6_ra.send_radv)
+            self.assertEqual(ip6_ra.n_prefixes, 0)
+            self.assertEqual(len(ip6_ra.prefixes), 0)
+            self.assertEqual(ip6_ra.last_radv_time, 0.0)
+            self.assertEqual(ip6_ra.last_multicast_time, 0.0)
+            self.assertEqual(ip6_ra.next_multicast_time, 0.0)
+            self.assertEqual(ip6_ra.n_advertisements_sent, 0)
+            self.assertEqual(ip6_ra.n_solicitations_rcvd, 0)
+            self.assertEqual(ip6_ra.n_solicitations_dropped, 0)
+
+        # Enable sending IPv6 RA for an interface
+        self.pg0.ip6_ra_config(no=1, suppress=1)
+
+        # Add IPv6 RA prefixes for the interface
+        pfx0 = IPv6Network(
+            "%s/%s" % (self.pg0.local_ip6, self.pg0.local_ip6_prefix_len), strict=False
+        )
+        pfx1 = IPv6Network("fafa::/96")
+        self.pg0.ip6_ra_prefix(pfx0, off_link=0, no_autoconfig=0)
+        self.pg0.ip6_ra_prefix(pfx1, off_link=1, no_autoconfig=1)
+
+        # Wait for multicast IPv6 RA
+        self.sleep(1)
+
+        # Dump IPv6 RA for the interface
+        ip6_ra_dump = self.vapi.sw_interface_ip6nd_ra_dump(
+            sw_if_index=self.pg0.sw_if_index
+        )
+        self.assertEqual(len(ip6_ra_dump), 1)
+        ip6_ra = ip6_ra_dump[0]
+
+        self.assertEqual(ip6_ra.sw_if_index, self.pg0.sw_if_index)
+        self.assertTrue(ip6_ra.send_radv)
+        self.assertEqual(ip6_ra.n_prefixes, 2)
+        self.assertEqual(len(ip6_ra.prefixes), 2)
+        self.assertEqual(ip6_ra.last_radv_time, 0.0)
+        self.assertGreater(ip6_ra.last_multicast_time, 0.0)
+        self.assertGreater(ip6_ra.next_multicast_time, 0.0)
+        self.assertGreater(ip6_ra.n_advertisements_sent, 0)
+        self.assertEqual(ip6_ra.n_solicitations_rcvd, 0)
+        self.assertEqual(ip6_ra.n_solicitations_dropped, 0)
+
+        self.assertEqual(ip6_ra.prefixes[0].prefix, pfx0)
+        self.assertTrue(ip6_ra.prefixes[0].onlink_flag)
+        self.assertTrue(ip6_ra.prefixes[0].autonomous_flag)
+        self.assertFalse(ip6_ra.prefixes[0].no_advertise)
+
+        self.assertEqual(ip6_ra.prefixes[1].prefix, pfx1)
+        self.assertFalse(ip6_ra.prefixes[1].onlink_flag)
+        self.assertFalse(ip6_ra.prefixes[1].autonomous_flag)
+        self.assertFalse(ip6_ra.prefixes[1].no_advertise)
+
+        # Reset sending IPv6 RA for the interface
+        self.pg0.ip6_ra_config(suppress=1)
+
+        # Remove IPv6 RA prefixes for the interface
+        self.pg0.ip6_ra_prefix(pfx0, is_no=1)
+        self.pg0.ip6_ra_prefix(pfx1, is_no=1)
+
+        # Dump IPv6 RA for the interface
+        ip6_ra_dump = self.vapi.sw_interface_ip6nd_ra_dump(
+            sw_if_index=self.pg0.sw_if_index
+        )
+        self.assertEqual(len(ip6_ra_dump), 1)
+        ip6_ra = ip6_ra_dump[0]
+
+        self.assertEqual(ip6_ra.sw_if_index, self.pg0.sw_if_index)
+        self.assertFalse(ip6_ra.send_radv)
+        self.assertEqual(ip6_ra.n_prefixes, 0)
+        self.assertEqual(len(ip6_ra.prefixes), 0)
+
     def test_rs(self):
         """IPv6 Router Solicitation Exceptions
 
@@ -1100,6 +1177,8 @@ class TestIPv6(TestIPv6ND):
         self.pg_start()
 
         subitf = VppDot1QSubint(self, self.pg1, 99)
+        self.interfaces.append(subitf)
+        self.sub_interfaces.append(subitf)
 
         subitf.admin_up()
         subitf.config_ip6()
@@ -2894,7 +2973,6 @@ class TestIP6Input(VppTestCase):
         ]
     )
     def test_ip_input_no_replies(self, name, src, dst, l4, msg, timeout):
-
         self._testMethodDoc = "IPv6 Input Exception - %s" % name
 
         p_version = (
@@ -4009,5 +4087,138 @@ class TestIPv6Punt(VppTestCase):
         self.assertEqual(str(punts[2].punt.nh), "::")
 
 
+class TestIP6InterfaceRx(VppTestCase):
+    """IPv6 Interface Receive"""
+
+    @classmethod
+    def setUpClass(cls):
+        super(TestIP6InterfaceRx, cls).setUpClass()
+
+    @classmethod
+    def tearDownClass(cls):
+        super(TestIP6InterfaceRx, cls).tearDownClass()
+
+    def setUp(self):
+        super(TestIP6InterfaceRx, self).setUp()
+
+        self.create_pg_interfaces(range(3))
+
+        table_id = 0
+
+        for i in self.pg_interfaces:
+            i.admin_up()
+
+            if table_id != 0:
+                table = VppIpTable(self, table_id, is_ip6=1)
+                table.add_vpp_config()
+
+            i.set_table_ip6(table_id)
+            i.config_ip6()
+            i.resolve_ndp()
+            table_id += 1
+
+    def tearDown(self):
+        for i in self.pg_interfaces:
+            i.unconfig_ip6()
+            i.admin_down()
+            i.set_table_ip6(0)
+
+        super(TestIP6InterfaceRx, self).tearDown()
+
+    def test_interface_rx(self):
+        """IPv6 Interface Receive"""
+
+        #
+        # add a route in the default table to receive ...
+        #
+        route_to_dst = VppIpRoute(
+            self,
+            "1::",
+            122,
+            [
+                VppRoutePath(
+                    "::",
+                    self.pg1.sw_if_index,
+                    type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
+                )
+            ],
+        )
+        route_to_dst.add_vpp_config()
+
+        #
+        # packets to these destination are dropped, since they'll
+        # hit the respective default routes in table 1
+        #
+        p_dst = (
+            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
+            / IPv6(src="5::5", dst="1::1")
+            / inet6.TCP(sport=1234, dport=1234)
+            / Raw(b"\xa5" * 100)
+        )
+        pkts_dst = p_dst * 10
+
+        self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 1")
+
+        #
+        # add a route in the dst table to forward via pg1
+        #
+        route_in_dst = VppIpRoute(
+            self,
+            "1::1",
+            128,
+            [VppRoutePath(self.pg1.remote_ip6, self.pg1.sw_if_index)],
+            table_id=1,
+        )
+        route_in_dst.add_vpp_config()
+
+        self.send_and_expect(self.pg0, pkts_dst, self.pg1)
+
+        #
+        # add a route in the default table to receive ...
+        #
+        route_to_dst = VppIpRoute(
+            self,
+            "1::",
+            122,
+            [
+                VppRoutePath(
+                    "::",
+                    self.pg2.sw_if_index,
+                    type=FibPathType.FIB_PATH_TYPE_INTERFACE_RX,
+                )
+            ],
+            table_id=1,
+        )
+        route_to_dst.add_vpp_config()
+
+        #
+        # packets to these destination are dropped, since they'll
+        # hit the respective default routes in table 2
+        #
+        p_dst = (
+            Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
+            / IPv6(src="6::6", dst="1::2")
+            / inet6.TCP(sport=1234, dport=1234)
+            / Raw(b"\xa5" * 100)
+        )
+        pkts_dst = p_dst * 10
+
+        self.send_and_assert_no_replies(self.pg0, pkts_dst, "IP in table 2")
+
+        #
+        # add a route in the table 2 to forward via pg2
+        #
+        route_in_dst = VppIpRoute(
+            self,
+            "1::2",
+            128,
+            [VppRoutePath(self.pg2.remote_ip6, self.pg2.sw_if_index)],
+            table_id=2,
+        )
+        route_in_dst.add_vpp_config()
+
+        self.send_and_expect(self.pg0, pkts_dst, self.pg2)
+
+
 if __name__ == "__main__":
     unittest.main(testRunner=VppTestRunner)