+ Proxy.remove_vpp_config()
+ Proxy1.remove_vpp_config()
+ Proxy2.remove_vpp_config()
+
+ self.pg3.unconfig_ip6()
+ self.pg4.unconfig_ip6()
+ self.pg5.unconfig_ip6()
+
+ def test_dhcp_client(self):
+ """DHCP Client"""
+
+ vdscp = VppEnum.vl_api_ip_dscp_t
+ hostname = "universal-dp"
+
+ self.pg_enable_capture(self.pg_interfaces)
+
+ #
+ # Configure DHCP client on PG3 and capture the discover sent
+ #
+ Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
+ Client.add_vpp_config()
+ self.assertTrue(Client.query_vpp_config())
+
+ rx = self.pg3.get_capture(1)
+
+ self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
+
+ #
+ # Send back on offer, expect the request
+ #
+ p_offer = (
+ Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
+ / 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=mac_pton(self.pg3.local_mac)
+ )
+ / DHCP(
+ options=[
+ ("message-type", "offer"),
+ ("server_id", self.pg3.remote_ip4),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_offer)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ rx = self.pg3.get_capture(1)
+ self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
+
+ #
+ # Send an acknowledgment
+ #
+ p_ack = (
+ Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
+ / 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=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),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_ack)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ #
+ # We'll get an ARP request for the router address
+ #
+ rx = self.pg3.get_capture(1)
+
+ self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
+ self.pg_enable_capture(self.pg_interfaces)
+
+ #
+ # At the end of this procedure there should be a connected route
+ # in the FIB
+ #
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
+
+ #
+ # remove the DHCP config
+ #
+ Client.remove_vpp_config()
+
+ #
+ # and now the route should be gone
+ #
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
+
+ #
+ # 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()
+ Client.set_client(
+ self.pg3.sw_if_index,
+ hostname,
+ id=self.pg3.local_mac,
+ dscp=vdscp.IP_API_DSCP_EF,
+ )
+ Client.add_vpp_config()
+
+ rx = self.pg3.get_capture(1)
+
+ self.verify_orig_dhcp_discover(
+ rx[0], self.pg3, hostname, 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
+ # DHCP DISCOVERY message.
+ # Such DHCP OFFER message must be silently discarded - RFC2131.
+ # Reported in Jira ticket: VPP-99
+ self.pg3.add_stream(p_offer)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ rx = self.pg3.get_capture(1)
+ self.verify_orig_dhcp_request(
+ rx[0], self.pg3, hostname, self.pg3.local_ip4, dscp=vdscp.IP_API_DSCP_EF
+ )
+
+ #
+ # unicast the ack to the offered address
+ #
+ 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=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),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_ack)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ #
+ # We'll get an ARP request for the router address
+ #
+ rx = self.pg3.get_capture(1)
+
+ self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
+ self.pg_enable_capture(self.pg_interfaces)
+
+ #
+ # At the end of this procedure there should be a connected route
+ # in the FIB
+ #
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
+
+ #
+ # remove the DHCP config
+ #
+ Client.remove_vpp_config()
+
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
+
+ #
+ # Rince and repeat, this time with VPP configured not to set
+ # the braodcast flag in the discover and request messages,
+ # and for the server to unicast the responses.
+ #
+ # Configure DHCP client on PG3 and capture the discover sent
+ #
+ Client.set_client(self.pg3.sw_if_index, hostname, set_broadcast_flag=False)
+ Client.add_vpp_config()
+
+ rx = self.pg3.get_capture(1)
+
+ self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname, broadcast=False)
+
+ #
+ # Send back on offer, unicasted to the offered address.
+ # Expect the request.
+ #
+ p_offer = (
+ 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=mac_pton(self.pg3.local_mac)
+ )
+ / DHCP(
+ options=[
+ ("message-type", "offer"),
+ ("server_id", self.pg3.remote_ip4),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_offer)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ rx = self.pg3.get_capture(1)
+ self.verify_orig_dhcp_request(
+ rx[0], self.pg3, hostname, self.pg3.local_ip4, broadcast=False
+ )
+
+ #
+ # 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=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)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ #
+ # We'll get an ARP request for the router address
+ #
+ rx = self.pg3.get_capture(1)
+
+ self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
+ self.pg_enable_capture(self.pg_interfaces)
+
+ #
+ # At the end of this procedure there should be a connected route
+ # in the FIB
+ #
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
+
+ #
+ # read the DHCP client details from a dump
+ #
+ clients = self.vapi.dhcp_client_dump()
+
+ self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
+ self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
+ self.assertEqual(clients[0].client.hostname, hostname)
+ self.assertEqual(clients[0].lease.hostname, hostname)
+ # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
+ self.assertEqual(clients[0].lease.state, 2)
+ self.assertEqual(clients[0].lease.mask_width, 24)
+ self.assertEqual(str(clients[0].lease.router_address), self.pg3.remote_ip4)
+ self.assertEqual(str(clients[0].lease.host_address), self.pg3.local_ip4)
+
+ #
+ # 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
+ )
+
+ # send an ACK with different data from the original offer *
+ self.pg3.generate_remote_hosts(4)
+ 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.remote_hosts[3].ip4,
+ chaddr=mac_pton(self.pg3.local_mac),
+ )
+ / DHCP(
+ options=[
+ ("message-type", "ack"),
+ ("subnet_mask", "255.255.255.0"),
+ ("router", self.pg3.remote_hosts[1].ip4),
+ ("server_id", self.pg3.remote_hosts[2].ip4),
+ ("lease_time", 43200),
+ ("renewal_time", 2),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_ack)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ #
+ # read the DHCP client details from a dump
+ #
+ clients = self.vapi.dhcp_client_dump()
+
+ self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
+ self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
+ self.assertEqual(clients[0].client.hostname, hostname)
+ self.assertEqual(clients[0].lease.hostname, hostname)
+ # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
+ self.assertEqual(clients[0].lease.state, 2)
+ self.assertEqual(clients[0].lease.mask_width, 24)
+ self.assertEqual(
+ str(clients[0].lease.router_address), self.pg3.remote_hosts[1].ip4
+ )
+ self.assertEqual(
+ str(clients[0].lease.host_address), self.pg3.remote_hosts[3].ip4
+ )
+
+ #
+ # remove the DHCP config
+ #
+ Client.remove_vpp_config()
+
+ #
+ # and now the route should be gone
+ #
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
+ self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
+
+ #
+ # Start the procedure again. Use requested lease time option.
+ # this time wait for the lease to expire and the client to
+ # self-destruct
+ #
+ hostname += "-2"
+ self.pg3.admin_down()
+ self.sleep(1)
+ self.pg3.admin_up()
+ self.pg_enable_capture(self.pg_interfaces)
+ Client.set_client(self.pg3.sw_if_index, hostname)
+ Client.add_vpp_config()
+
+ rx = self.pg3.get_capture(1)
+
+ self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
+
+ #
+ # Send back on offer with requested lease time, expect the request
+ #
+ lease_time = 1
+ p_offer = (
+ Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
+ / 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=mac_pton(self.pg3.local_mac)
+ )
+ / DHCP(
+ options=[
+ ("message-type", "offer"),
+ ("server_id", self.pg3.remote_ip4),
+ ("lease_time", lease_time),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_offer)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ rx = self.pg3.get_capture(1)
+ self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
+
+ #
+ # Send an acknowledgment
+ #
+ p_ack = (
+ Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
+ / 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=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", lease_time),
+ "end",
+ ]
+ )
+ )
+
+ self.pg3.add_stream(p_ack)
+ self.pg_enable_capture(self.pg_interfaces)
+ self.pg_start()
+
+ #
+ # We'll get an ARP request for the router address
+ #
+ rx = self.pg3.get_capture(1)
+
+ self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
+
+ #
+ # At the end of this procedure there should be a connected route
+ # in the FIB
+ #
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
+ self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
+
+ #
+ # the route should be gone after the lease expires
+ #
+ self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
+ self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
+
+ #
+ # remove the DHCP config
+ #
+ Client.remove_vpp_config()
+
+ 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()
+
+ #
+ # Configure DHCP client on PG3 and capture the discover sent
+ #
+ Client = VppDHCPClient(
+ self, vlan_100.sw_if_index, hostname, dscp=vdscp.IP_API_DSCP_EF
+ )
+ Client.add_vpp_config()
+
+ rx = self.pg3.get_capture(1)
+
+ self.assertEqual(rx[0][Dot1Q].vlan, 100)
+ self.assertEqual(rx[0][Dot1Q].prio, 2)
+
+ self.verify_orig_dhcp_discover(
+ rx[0], self.pg3, hostname, dscp=vdscp.IP_API_DSCP_EF
+ )
+
+
+if __name__ == "__main__":