X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=test%2Ftest_dhcp.py;h=16b0f470b0a93403e4dadfb69a32abbbb9ff7995;hb=038e1dfbd;hp=db3e3f3bab02d57868e89ef0414767001942e40b;hpb=7f99183a20d60cd5c648cc23d7a2f30a594a215b;p=vpp.git diff --git a/test/test_dhcp.py b/test/test_dhcp.py index db3e3f3bab0..16b0f470b0a 100644 --- a/test/test_dhcp.py +++ b/test/test_dhcp.py @@ -8,8 +8,8 @@ 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 -from vpp_mac import mactobinary, binarytomac -from scapy.layers.l2 import Ether, getmacbyip, ARP +import scapy.compat +from scapy.layers.l2 import Ether, getmacbyip, ARP, Dot1Q from scapy.layers.inet import IP, UDP, ICMP from scapy.layers.inet6 import IPv6, in6_getnsmac from scapy.utils6 import in6_mactoifaceid @@ -20,6 +20,10 @@ from scapy.layers.dhcp6 import DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \ from socket import AF_INET, AF_INET6 from scapy.utils import inet_pton, inet_ntop from scapy.utils6 import in6_ptop +from vpp_papi import mac_pton, VppEnum +from vpp_sub_interface import VppDot1QSubint +from vpp_qos import VppQosEgressMap, VppQosMark + DHCP4_CLIENT_PORT = 68 DHCP4_SERVER_PORT = 67 @@ -30,6 +34,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,22 +213,31 @@ 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, dscp, 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) + self.assertEqual(ip.tos, dscp) 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): - self.verify_orig_dhcp_pkt(pkt, intf) + broadcast=True, dscp=0): + self.verify_orig_dhcp_pkt(pkt, intf, dscp) self.verify_dhcp_msg_type(pkt, "discover") self.verify_dhcp_has_option(pkt, "hostname", hostname) @@ -231,15 +252,22 @@ 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, + dscp=0): + self.verify_orig_dhcp_pkt(pkt, intf, dscp, 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: @@ -360,6 +388,16 @@ class TestDHCP(VppTestCase): # not sure why this is not decoding # adv = pkt[DHCP6_Advertise] + def wait_for_no_route(self, address, length, + n_tries=50, s_time=1): + while (n_tries): + if not find_route(self, address, length): + return True + n_tries = n_tries - 1 + self.sleep(s_time) + + return False + def test_dhcp_proxy(self): """ DHCPv4 Proxy """ @@ -480,7 +518,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) / @@ -494,7 +532,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) / @@ -1052,8 +1090,7 @@ class TestDHCP(VppTestCase): nd_entry = VppNeighbor(self, self.pg1.sw_if_index, self.pg1.remote_hosts[1].mac, - self.pg1.remote_hosts[1].ip6, - af=AF_INET6) + self.pg1.remote_hosts[1].ip6) nd_entry.add_vpp_config() # @@ -1197,6 +1234,7 @@ class TestDHCP(VppTestCase): def test_dhcp_client(self): """ DHCP Client""" + vdscp = VppEnum.vl_api_ip_dscp_t hostname = 'universal-dp' self.pg_enable_capture(self.pg_interfaces) @@ -1204,7 +1242,7 @@ class TestDHCP(VppTestCase): # # Configure DHCP client on PG3 and capture the discover sent # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname) rx = self.pg3.get_capture(1) @@ -1218,7 +1256,7 @@ class TestDHCP(VppTestCase): UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'offer'), ('server_id', self.pg3.remote_ip4), 'end'])) @@ -1238,7 +1276,7 @@ class TestDHCP(VppTestCase): IP(src=self.pg3.remote_ip4, dst="255.255.255.255") / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', "255.255.255.0"), ('router', self.pg3.remote_ip4), @@ -1267,13 +1305,14 @@ class TestDHCP(VppTestCase): # remove the left over ARP entry self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index, - mactobinary(self.pg3.remote_mac), + self.pg3.remote_mac, self.pg3.remote_ip4, is_add=0) + # # remove the DHCP config # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0) # # and now the route should be gone @@ -1283,17 +1322,20 @@ class TestDHCP(VppTestCase): # # Start the procedure again. this time have VPP send the client-ID + # and set the DSCP value # self.pg3.admin_down() self.sleep(1) self.pg3.admin_up() - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, - client_id=self.pg3.local_mac) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, + client_id=self.pg3.local_mac, + dscp=vdscp.IP_API_DSCP_EF) rx = self.pg3.get_capture(1) self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname, - self.pg3.local_mac) + self.pg3.local_mac, + dscp=vdscp.IP_API_DSCP_EF) # TODO: VPP DHCP client should not accept DHCP OFFER message with # the XID (Transaction ID) not matching the XID of the most recent @@ -1306,7 +1348,8 @@ class TestDHCP(VppTestCase): rx = self.pg3.get_capture(1) self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, - self.pg3.local_ip4) + self.pg3.local_ip4, + dscp=vdscp.IP_API_DSCP_EF) # # unicast the ack to the offered address @@ -1315,7 +1358,7 @@ class TestDHCP(VppTestCase): IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', "255.255.255.0"), ('router', self.pg3.remote_ip4), @@ -1345,7 +1388,7 @@ class TestDHCP(VppTestCase): # # remove the DHCP config # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0) self.assertFalse(find_route(self, self.pg3.local_ip4, 32)) self.assertFalse(find_route(self, self.pg3.local_ip4, 24)) @@ -1357,13 +1400,13 @@ class TestDHCP(VppTestCase): # # Configure DHCP client on PG3 and capture the discover sent # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, - set_broadcast_flag=0) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, + set_broadcast_flag=0) 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. @@ -1373,7 +1416,7 @@ class TestDHCP(VppTestCase): IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'offer'), ('server_id', self.pg3.remote_ip4), 'end'])) @@ -1385,21 +1428,23 @@ 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) / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', "255.255.255.0"), ('router', self.pg3.remote_ip4), ('server_id', self.pg3.remote_ip4), ('lease_time', 43200), + ('renewal_time', 2), 'end'])) self.pg3.add_stream(p_ack) @@ -1421,11 +1466,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, - mactobinary(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 @@ -1449,10 +1516,16 @@ 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 # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0) # # and now the route should be gone @@ -1463,10 +1536,12 @@ 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.vapi.dhcp_client(self.pg3.sw_if_index, hostname) + self.pg_enable_capture(self.pg_interfaces) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname) rx = self.pg3.get_capture(1) @@ -1481,7 +1556,7 @@ class TestDHCP(VppTestCase): UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'offer'), ('server_id', self.pg3.remote_ip4), ('lease_time', lease_time), @@ -1502,7 +1577,7 @@ class TestDHCP(VppTestCase): IP(src=self.pg3.remote_ip4, dst='255.255.255.255') / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) / BOOTP(op=1, yiaddr=self.pg3.local_ip4, - chaddr=mactobinary(self.pg3.local_mac)) / + chaddr=mac_pton(self.pg3.local_mac)) / DHCP(options=[('message-type', 'ack'), ('subnet_mask', '255.255.255.0'), ('router', self.pg3.remote_ip4), @@ -1530,25 +1605,62 @@ class TestDHCP(VppTestCase): # remove the left over ARP entry self.vapi.ip_neighbor_add_del(self.pg3.sw_if_index, - mactobinary(self.pg3.remote_mac), + self.pg3.remote_mac, self.pg3.remote_ip4, is_add=0) # - # Sleep for the lease time + # the route should be gone after the lease expires # - self.sleep(lease_time+1) + self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32)) + self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24)) # - # And now the route should be gone + # remove the DHCP config # - self.assertFalse(find_route(self, self.pg3.local_ip4, 32)) - self.assertFalse(find_route(self, self.pg3.local_ip4, 24)) + self.vapi.dhcp_client_config(self.pg3.sw_if_index, hostname, is_add=0) + + def test_dhcp_client_vlan(self): + """ DHCP Client w/ VLAN""" + + vdscp = VppEnum.vl_api_ip_dscp_t + vqos = VppEnum.vl_api_qos_source_t + hostname = 'universal-dp' + + self.pg_enable_capture(self.pg_interfaces) + + vlan_100 = VppDot1QSubint(self, self.pg3, 100) + vlan_100.admin_up() + + output = [scapy.compat.chb(4)] * 256 + os = b''.join(output) + rows = [{'outputs': os}, + {'outputs': os}, + {'outputs': os}, + {'outputs': os}] + + qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config() + qm1 = VppQosMark(self, vlan_100, qem1, + vqos.QOS_API_SOURCE_VLAN).add_vpp_config() # - # remove the DHCP config + # Configure DHCP client on PG3 and capture the discover sent # - self.vapi.dhcp_client(self.pg3.sw_if_index, hostname, is_add=0) + self.vapi.dhcp_client_config(vlan_100.sw_if_index, + hostname, + dscp=vdscp.IP_API_DSCP_EF) + + rx = self.pg3.get_capture(1) + + self.assertEqual(rx[0][Dot1Q].vlan, 100) + self.assertEqual(rx[0][Dot1Q].prio, 4) + + self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname, + dscp=vdscp.IP_API_DSCP_EF) + + self.vapi.dhcp_client_config(vlan_100.sw_if_index, + hostname, + is_add=0) if __name__ == '__main__':