dpdk: fix vlan stripping
[vpp.git] / test / test_dhcp.py
index 2efa9a7..fce41a9 100644 (file)
@@ -8,6 +8,7 @@ from framework import VppTestCase, VppTestRunner, running_extended_tests
 from vpp_neighbor import VppNeighbor
 from vpp_ip_route import find_route, VppIpTable
 from util import mk_ll_addr
+import scapy.compat
 from scapy.layers.l2 import Ether, getmacbyip, ARP
 from scapy.layers.inet import IP, UDP, ICMP
 from scapy.layers.inet6 import IPv6, in6_getnsmac
@@ -30,6 +31,14 @@ DHCP6_SERVER_PORT = 546
 class TestDHCP(VppTestCase):
     """ DHCP Test Case """
 
+    @classmethod
+    def setUpClass(cls):
+        super(TestDHCP, cls).setUpClass()
+
+    @classmethod
+    def tearDownClass(cls):
+        super(TestDHCP, cls).tearDownClass()
+
     def setUp(self):
         super(TestDHCP, self).setUp()
 
@@ -201,21 +210,29 @@ class TestDHCP(VppTestCase):
         data = self.validate_relay_options(pkt, intf, intf.local_ip4,
                                            vpn_id, fib_id, oui)
 
-    def verify_orig_dhcp_pkt(self, pkt, intf):
+    def verify_orig_dhcp_pkt(self, pkt, intf, l2_bc=True):
         ether = pkt[Ether]
-        self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
+        if l2_bc:
+            self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
+        else:
+            self.assertEqual(ether.dst, intf.remote_mac)
         self.assertEqual(ether.src, intf.local_mac)
 
         ip = pkt[IP]
-        self.assertEqual(ip.dst, "255.255.255.255")
-        self.assertEqual(ip.src, "0.0.0.0")
+
+        if (l2_bc):
+            self.assertEqual(ip.dst, "255.255.255.255")
+            self.assertEqual(ip.src, "0.0.0.0")
+        else:
+            self.assertEqual(ip.dst, intf.remote_ip4)
+            self.assertEqual(ip.src, intf.local_ip4)
 
         udp = pkt[UDP]
         self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
         self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
 
     def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
-                                  broadcast=1):
+                                  broadcast=True):
         self.verify_orig_dhcp_pkt(pkt, intf)
 
         self.verify_dhcp_msg_type(pkt, "discover")
@@ -231,15 +248,21 @@ class TestDHCP(VppTestCase):
             self.assertEqual(bootp.flags, 0x0000)
 
     def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
-                                 broadcast=1):
-        self.verify_orig_dhcp_pkt(pkt, intf)
+                                 broadcast=True,
+                                 l2_bc=True):
+        self.verify_orig_dhcp_pkt(pkt, intf, l2_bc=l2_bc)
 
         self.verify_dhcp_msg_type(pkt, "request")
         self.verify_dhcp_has_option(pkt, "hostname", hostname)
         self.verify_dhcp_has_option(pkt, "requested_addr", ip)
         bootp = pkt[BOOTP]
-        self.assertEqual(bootp.ciaddr, "0.0.0.0")
+
+        if l2_bc:
+            self.assertEqual(bootp.ciaddr, "0.0.0.0")
+        else:
+            self.assertEqual(bootp.ciaddr, intf.local_ip4)
         self.assertEqual(bootp.giaddr, "0.0.0.0")
+
         if broadcast:
             self.assertEqual(bootp.flags, 0x8000)
         else:
@@ -490,7 +513,7 @@ class TestDHCP(VppTestCase):
         #
         # 1. not our IP address = not checked by VPP? so offer is replayed
         #    to client
-        bad_ip = option_82[0:8] + chr(33) + option_82[9:]
+        bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
 
         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
@@ -504,7 +527,7 @@ class TestDHCP(VppTestCase):
                                         "DHCP offer option 82 bad address")
 
         # 2. Not a sw_if_index VPP knows
-        bad_if_index = option_82[0:2] + chr(33) + option_82[3:]
+        bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
 
         p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
              IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
@@ -1373,7 +1396,7 @@ class TestDHCP(VppTestCase):
         rx = self.pg3.get_capture(1)
 
         self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
-                                       broadcast=0)
+                                       broadcast=False)
 
         #
         # Send back on offer, unicasted to the offered address.
@@ -1395,10 +1418,11 @@ class TestDHCP(VppTestCase):
         rx = self.pg3.get_capture(1)
         self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
                                       self.pg3.local_ip4,
-                                      broadcast=0)
+                                      broadcast=False)
 
         #
-        # Send an acknowledgment
+        # Send an acknowledgment, the lease renewal time is 2 seconds
+        # so we should expect the renew straight after
         #
         p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
                  IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
@@ -1410,6 +1434,7 @@ class TestDHCP(VppTestCase):
                                ('router', self.pg3.remote_ip4),
                                ('server_id', self.pg3.remote_ip4),
                                ('lease_time', 43200),
+                               ('renewal_time', 2),
                                'end']))
 
         self.pg3.add_stream(p_ack)
@@ -1431,11 +1456,33 @@ class TestDHCP(VppTestCase):
         self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
         self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
 
-        # remove the left over ARP entry
-        self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
-                                      self.pg3.remote_mac,
-                                      self.pg3.remote_ip4,
-                                      is_add=0)
+        #
+        # wait for the unicasted renewal
+        #  the first attempt will be an ARP packet, since we have not yet
+        #  responded to VPP's request
+        #
+        self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
+        rx = self.pg3.get_capture(1, timeout=10)
+
+        self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
+
+        # respond to the arp
+        p_arp = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
+                 ARP(op="is-at",
+                     hwdst=self.pg3.local_mac,
+                     hwsrc=self.pg3.remote_mac,
+                     pdst=self.pg3.local_ip4,
+                     psrc=self.pg3.remote_ip4))
+        self.pg3.add_stream(p_arp)
+        self.pg_enable_capture(self.pg_interfaces)
+        self.pg_start()
+
+        # the next packet is the unicasted renewal
+        rx = self.pg3.get_capture(1, timeout=10)
+        self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
+                                      self.pg3.local_ip4,
+                                      l2_bc=False,
+                                      broadcast=False)
 
         #
         # read the DHCP client details from a dump
@@ -1459,6 +1506,12 @@ class TestDHCP(VppTestCase):
         self.assertEqual(clients[0].lease.host_address.rstrip('\0'),
                          self.pg3.local_ip4n)
 
+        # remove the left over ARP entry
+        self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index,
+                                      self.pg3.remote_mac,
+                                      self.pg3.remote_ip4,
+                                      is_add=0)
+
         #
         # remove the DHCP config
         #
@@ -1473,9 +1526,11 @@ class TestDHCP(VppTestCase):
         #
         # Start the procedure again. Use requested lease time option.
         #
+        hostname += "-2"
         self.pg3.admin_down()
         self.sleep(1)
         self.pg3.admin_up()
+        self.pg_enable_capture(self.pg_interfaces)
         self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname)
 
         rx = self.pg3.get_capture(1)