8 from framework import VppTestCase, VppTestRunner
9 from framework import tag_run_solo
10 from vpp_neighbor import VppNeighbor
11 from vpp_ip_route import find_route, VppIpTable
12 from util import mk_ll_addr
14 from scapy.layers.l2 import Ether, getmacbyip, ARP, Dot1Q
15 from scapy.layers.inet import IP, UDP, ICMP
16 from scapy.layers.inet6 import IPv6, in6_getnsmac
17 from scapy.utils6 import in6_mactoifaceid
18 from scapy.layers.dhcp import DHCP, BOOTP, DHCPTypes
19 from scapy.layers.dhcp6 import (
29 DHCP6OptClientLinkLayerAddr,
32 from socket import AF_INET, AF_INET6, inet_pton, inet_ntop
33 from scapy.utils6 import in6_ptop
34 from vpp_papi import mac_pton, VppEnum
35 from vpp_sub_interface import VppDot1QSubint
36 from vpp_qos import VppQosEgressMap, VppQosMark
37 from vpp_dhcp import VppDHCPClient, VppDHCPProxy
40 DHCP4_CLIENT_PORT = 68
41 DHCP4_SERVER_PORT = 67
42 DHCP6_CLIENT_PORT = 547
43 DHCP6_SERVER_PORT = 546
47 class TestDHCP(VppTestCase):
52 super(TestDHCP, cls).setUpClass()
55 def tearDownClass(cls):
56 super(TestDHCP, cls).tearDownClass()
59 super(TestDHCP, self).setUp()
61 # create 6 pg interfaces for pg0 to pg5
62 self.create_pg_interfaces(range(6))
65 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
66 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
68 for table_id in range(1, 4):
69 tbl4 = VppIpTable(self, table_id)
71 self.tables.append(tbl4)
72 tbl6 = VppIpTable(self, table_id, is_ip6=1)
74 self.tables.append(tbl6)
77 for i in self.pg_interfaces[:3]:
79 i.set_table_ip4(table_id)
80 i.set_table_ip6(table_id)
88 for i in self.pg_interfaces[3:]:
90 i.set_table_ip4(table_id)
91 i.set_table_ip6(table_id)
95 for i in self.pg_interfaces[:3]:
99 for i in self.pg_interfaces:
103 super(TestDHCP, self).tearDown()
105 def verify_dhcp_has_option(self, pkt, option, value):
109 for i in dhcp.options:
110 if isinstance(i, tuple):
112 self.assertEqual(i[1], value)
115 self.assertTrue(found)
117 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
123 for i in dhcp.options:
124 if isinstance(i, tuple):
125 if i[0] == "relay_agent_Information":
127 # There are two sb-options present - each of length 6.
131 self.assertEqual(len(data), 24)
132 elif len(vpn_id) > 0:
133 self.assertEqual(len(data), len(vpn_id) + 17)
135 self.assertEqual(len(data), 12)
138 # First sub-option is ID 1, len 4, then encoded
139 # sw_if_index. This test uses low valued indicies
141 # The ID space is VPP internal - so no matching value
144 self.assertEqual(six.byte2int(data[0:1]), 1)
145 self.assertEqual(six.byte2int(data[1:2]), 4)
146 self.assertEqual(six.byte2int(data[2:3]), 0)
147 self.assertEqual(six.byte2int(data[3:4]), 0)
148 self.assertEqual(six.byte2int(data[4:5]), 0)
149 self.assertEqual(six.byte2int(data[5:6]), intf._sw_if_index)
152 # next sub-option is the IP address of the client side
154 # sub-option ID=5, length (of a v4 address)=4
156 claddr = socket.inet_pton(AF_INET, ip_addr)
158 self.assertEqual(six.byte2int(data[6:7]), 5)
159 self.assertEqual(six.byte2int(data[7:8]), 4)
160 self.assertEqual(data[8], claddr[0])
161 self.assertEqual(data[9], claddr[1])
162 self.assertEqual(data[10], claddr[2])
163 self.assertEqual(data[11], claddr[3])
166 # sub-option 151 encodes vss_type 1,
167 # the 3 byte oui and the 4 byte fib_id
168 self.assertEqual(id_len, 0)
169 self.assertEqual(six.byte2int(data[12:13]), 151)
170 self.assertEqual(six.byte2int(data[13:14]), 8)
171 self.assertEqual(six.byte2int(data[14:15]), 1)
172 self.assertEqual(six.byte2int(data[15:16]), 0)
173 self.assertEqual(six.byte2int(data[16:17]), 0)
174 self.assertEqual(six.byte2int(data[17:18]), oui)
175 self.assertEqual(six.byte2int(data[18:19]), 0)
176 self.assertEqual(six.byte2int(data[19:20]), 0)
177 self.assertEqual(six.byte2int(data[20:21]), 0)
178 self.assertEqual(six.byte2int(data[21:22]), fib_id)
180 # VSS control sub-option
181 self.assertEqual(six.byte2int(data[22:23]), 152)
182 self.assertEqual(six.byte2int(data[23:24]), 0)
185 # sub-option 151 encode vss_type of 0
186 # followerd by vpn_id in ascii
187 self.assertEqual(oui, 0)
188 self.assertEqual(six.byte2int(data[12:13]), 151)
189 self.assertEqual(six.byte2int(data[13:14]), id_len + 1)
190 self.assertEqual(six.byte2int(data[14:15]), 0)
191 self.assertEqual(data[15 : 15 + id_len].decode("ascii"), vpn_id)
193 # VSS control sub-option
195 six.byte2int(data[15 + len(vpn_id) : 16 + len(vpn_id)]), 152
198 six.byte2int(data[16 + len(vpn_id) : 17 + len(vpn_id)]), 0
202 self.assertTrue(found)
206 def verify_dhcp_msg_type(self, pkt, name):
209 for o in dhcp.options:
210 if isinstance(o, tuple):
211 if o[0] == "message-type" and DHCPTypes[o[1]] == name:
213 self.assertTrue(found)
215 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
217 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
218 self.assertEqual(ether.src, intf.local_mac)
221 self.assertEqual(ip.dst, "255.255.255.255")
222 self.assertEqual(ip.src, intf.local_ip4)
225 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
226 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
228 self.verify_dhcp_msg_type(pkt, "offer")
229 data = self.validate_relay_options(
230 pkt, intf, intf.local_ip4, vpn_id, fib_id, oui
233 def verify_orig_dhcp_pkt(self, pkt, intf, dscp, l2_bc=True):
236 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
238 self.assertEqual(ether.dst, intf.remote_mac)
239 self.assertEqual(ether.src, intf.local_mac)
244 self.assertEqual(ip.dst, "255.255.255.255")
245 self.assertEqual(ip.src, "0.0.0.0")
247 self.assertEqual(ip.dst, intf.remote_ip4)
248 self.assertEqual(ip.src, intf.local_ip4)
249 self.assertEqual(ip.tos, dscp)
252 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
253 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
255 def verify_orig_dhcp_discover(
256 self, pkt, intf, hostname, client_id=None, broadcast=True, dscp=0
258 self.verify_orig_dhcp_pkt(pkt, intf, dscp)
260 self.verify_dhcp_msg_type(pkt, "discover")
261 self.verify_dhcp_has_option(pkt, "hostname", hostname.encode("ascii"))
263 client_id = "\x00" + client_id
264 self.verify_dhcp_has_option(pkt, "client_id", client_id.encode("ascii"))
266 self.assertEqual(bootp.ciaddr, "0.0.0.0")
267 self.assertEqual(bootp.giaddr, "0.0.0.0")
269 self.assertEqual(bootp.flags, 0x8000)
271 self.assertEqual(bootp.flags, 0x0000)
273 def verify_orig_dhcp_request(
274 self, pkt, intf, hostname, ip, broadcast=True, l2_bc=True, dscp=0
276 self.verify_orig_dhcp_pkt(pkt, intf, dscp, l2_bc=l2_bc)
278 self.verify_dhcp_msg_type(pkt, "request")
279 self.verify_dhcp_has_option(pkt, "hostname", hostname.encode("ascii"))
280 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
284 self.assertEqual(bootp.ciaddr, "0.0.0.0")
286 self.assertEqual(bootp.ciaddr, intf.local_ip4)
287 self.assertEqual(bootp.giaddr, "0.0.0.0")
290 self.assertEqual(bootp.flags, 0x8000)
292 self.assertEqual(bootp.flags, 0x0000)
294 def verify_relayed_dhcp_discover(
306 dst_mac = intf.remote_mac
308 dst_ip = intf.remote_ip4
311 self.assertEqual(ether.dst, dst_mac)
312 self.assertEqual(ether.src, intf.local_mac)
315 self.assertEqual(ip.dst, dst_ip)
316 self.assertEqual(ip.src, intf.local_ip4)
319 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
320 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
325 for o in dhcp.options:
326 if isinstance(o, tuple):
327 if o[0] == "message-type" and DHCPTypes[o[1]] == "discover":
329 self.assertTrue(is_discover)
331 data = self.validate_relay_options(
332 pkt, src_intf, src_intf.local_ip4, vpn_id, fib_id, oui
336 def verify_dhcp6_solicit(
349 dst_mac = intf.remote_mac
351 dst_ip = in6_ptop(intf.remote_ip6)
354 self.assertEqual(ether.dst, dst_mac)
355 self.assertEqual(ether.src, intf.local_mac)
358 self.assertEqual(in6_ptop(ip.dst), dst_ip)
359 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
362 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
363 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
365 relay = pkt[DHCP6_RelayForward]
366 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
367 oid = pkt[DHCP6OptIfaceId]
368 cll = pkt[DHCP6OptClientLinkLayerAddr]
369 self.assertEqual(cll.optlen, 8)
370 self.assertEqual(cll.lltype, 1)
371 self.assertEqual(cll.clladdr, peer_mac)
376 self.assertEqual(id_len, 0)
377 vss = pkt[DHCP6OptVSS]
378 self.assertEqual(vss.optlen, 8)
379 self.assertEqual(vss.type, 1)
380 # the OUI and FIB-id are really 3 and 4 bytes resp.
381 # but the tested range is small
382 self.assertEqual(six.byte2int(vss.data[0:1]), 0)
383 self.assertEqual(six.byte2int(vss.data[1:2]), 0)
384 self.assertEqual(six.byte2int(vss.data[2:3]), oui)
385 self.assertEqual(six.byte2int(vss.data[3:4]), 0)
386 self.assertEqual(six.byte2int(vss.data[4:5]), 0)
387 self.assertEqual(six.byte2int(vss.data[5:6]), 0)
388 self.assertEqual(six.byte2int(vss.data[6:7]), fib_id)
391 self.assertEqual(oui, 0)
392 vss = pkt[DHCP6OptVSS]
393 self.assertEqual(vss.optlen, id_len + 1)
394 self.assertEqual(vss.type, 0)
395 self.assertEqual(vss.data[0:id_len].decode("ascii"), vpn_id)
397 # the relay message should be an encoded Solicit
398 msg = pkt[DHCP6OptRelayMsg]
399 sol = DHCP6_Solicit()
400 self.assertEqual(msg.optlen, len(sol))
401 self.assertEqual(sol, msg[1])
403 def verify_dhcp6_advert(self, pkt, intf, peer):
405 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
406 self.assertEqual(ether.src, intf.local_mac)
409 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
410 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
413 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
414 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
416 # not sure why this is not decoding
417 # adv = pkt[DHCP6_Advertise]
419 def wait_for_no_route(self, address, length, n_tries=50, s_time=1):
421 if not find_route(self, address, length):
423 n_tries = n_tries - 1
428 def test_dhcp_proxy(self):
432 # Verify no response to DHCP request without DHCP config
435 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg3.remote_mac)
436 / IP(src="0.0.0.0", dst="255.255.255.255")
437 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
439 / DHCP(options=[("message-type", "discover"), ("end")])
441 pkts_disc_vrf0 = [p_disc_vrf0]
443 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg4.remote_mac)
444 / IP(src="0.0.0.0", dst="255.255.255.255")
445 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
447 / DHCP(options=[("message-type", "discover"), ("end")])
449 pkts_disc_vrf1 = [p_disc_vrf1]
451 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg5.remote_mac)
452 / IP(src="0.0.0.0", dst="255.255.255.255")
453 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
455 / DHCP(options=[("message-type", "discover"), ("end")])
457 pkts_disc_vrf2 = [p_disc_vrf2]
459 self.send_and_assert_no_replies(
460 self.pg3, pkts_disc_vrf0, "DHCP with no configuration"
462 self.send_and_assert_no_replies(
463 self.pg4, pkts_disc_vrf1, "DHCP with no configuration"
465 self.send_and_assert_no_replies(
466 self.pg5, pkts_disc_vrf2, "DHCP with no configuration"
470 # Enable DHCP proxy in VRF 0
472 server_addr = self.pg0.remote_ip4
473 src_addr = self.pg0.local_ip4
475 Proxy = VppDHCPProxy(self, server_addr, src_addr, rx_vrf_id=0)
476 Proxy.add_vpp_config()
479 # Discover packets from the client are dropped because there is no
480 # IP address configured on the client facing interface
482 self.send_and_assert_no_replies(
483 self.pg3, pkts_disc_vrf0, "Discover DHCP no relay address"
487 # Inject a response from the server
488 # dropped, because there is no IP addrees on the
489 # client interfce to fill in the option.
492 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
493 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
494 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
496 / DHCP(options=[("message-type", "offer"), ("end")])
500 self.send_and_assert_no_replies(self.pg3, pkts, "Offer DHCP no relay address")
503 # configure an IP address on the client facing interface
505 self.pg3.config_ip4()
508 # Try again with a discover packet
509 # Rx'd packet should be to the server address and from the configured
511 # UDP source ports are unchanged
512 # we've no option 82 config so that should be absent
514 self.pg3.add_stream(pkts_disc_vrf0)
515 self.pg_enable_capture(self.pg_interfaces)
518 rx = self.pg0.get_capture(1)
521 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0, src_intf=self.pg3)
524 # Create an DHCP offer reply from the server with a correctly formatted
525 # option 82. i.e. send back what we just captured
526 # The offer, sent mcast to the client, still has option 82.
529 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
530 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
531 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
535 ("message-type", "offer"),
536 ("relay_agent_Information", option_82),
543 self.pg0.add_stream(pkts)
544 self.pg_enable_capture(self.pg_interfaces)
547 rx = self.pg3.get_capture(1)
550 self.verify_dhcp_offer(rx, self.pg3)
555 # 1. not our IP address = not checked by VPP? so offer is replayed
557 bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
560 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
561 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
562 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
566 ("message-type", "offer"),
567 ("relay_agent_Information", bad_ip),
573 self.send_and_assert_no_replies(
574 self.pg0, pkts, "DHCP offer option 82 bad address"
577 # 2. Not a sw_if_index VPP knows
578 bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
581 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
582 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
583 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
587 ("message-type", "offer"),
588 ("relay_agent_Information", bad_if_index),
594 self.send_and_assert_no_replies(
595 self.pg0, pkts, "DHCP offer option 82 bad if index"
599 # Send a DHCP request in VRF 1. should be dropped.
601 self.send_and_assert_no_replies(
602 self.pg4, pkts_disc_vrf1, "DHCP with no configuration VRF 1"
606 # Delete the DHCP config in VRF 0
607 # Should now drop requests.
609 Proxy.remove_vpp_config()
611 self.send_and_assert_no_replies(
612 self.pg3, pkts_disc_vrf0, "DHCP config removed VRF 0"
614 self.send_and_assert_no_replies(
615 self.pg4, pkts_disc_vrf1, "DHCP config removed VRF 1"
619 # Add DHCP config for VRF 1 & 2
621 server_addr1 = self.pg1.remote_ip4
622 src_addr1 = self.pg1.local_ip4
623 Proxy1 = VppDHCPProxy(
624 self, server_addr1, src_addr1, rx_vrf_id=1, server_vrf_id=1
626 Proxy1.add_vpp_config()
628 server_addr2 = self.pg2.remote_ip4
629 src_addr2 = self.pg2.local_ip4
630 Proxy2 = VppDHCPProxy(
631 self, server_addr2, src_addr2, rx_vrf_id=2, server_vrf_id=2
633 Proxy2.add_vpp_config()
636 # Confim DHCP requests ok in VRF 1 & 2.
637 # - dropped on IP config on client interface
639 self.send_and_assert_no_replies(
640 self.pg4, pkts_disc_vrf1, "DHCP config removed VRF 1"
642 self.send_and_assert_no_replies(
643 self.pg5, pkts_disc_vrf2, "DHCP config removed VRF 2"
647 # configure an IP address on the client facing interface
649 self.pg4.config_ip4()
650 self.pg4.add_stream(pkts_disc_vrf1)
651 self.pg_enable_capture(self.pg_interfaces)
653 rx = self.pg1.get_capture(1)
655 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
657 self.pg5.config_ip4()
658 self.pg5.add_stream(pkts_disc_vrf2)
659 self.pg_enable_capture(self.pg_interfaces)
661 rx = self.pg2.get_capture(1)
663 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
667 # table=1, vss_type=1, vpn_index=1, oui=4
668 # table=2, vss_type=0, vpn_id = "ip4-table-2"
669 self.vapi.dhcp_proxy_set_vss(tbl_id=1, vss_type=1, vpn_index=1, oui=4, is_add=1)
670 self.vapi.dhcp_proxy_set_vss(
671 tbl_id=2, vss_type=0, vpn_ascii_id="ip4-table-2", is_add=1
674 self.pg4.add_stream(pkts_disc_vrf1)
675 self.pg_enable_capture(self.pg_interfaces)
678 rx = self.pg1.get_capture(1)
680 self.verify_relayed_dhcp_discover(
681 rx, self.pg1, src_intf=self.pg4, fib_id=1, oui=4
684 self.pg5.add_stream(pkts_disc_vrf2)
685 self.pg_enable_capture(self.pg_interfaces)
688 rx = self.pg2.get_capture(1)
690 self.verify_relayed_dhcp_discover(
691 rx, self.pg2, src_intf=self.pg5, vpn_id="ip4-table-2"
695 # Add a second DHCP server in VRF 1
696 # expect clients messages to be relay to both configured servers
698 self.pg1.generate_remote_hosts(2)
699 server_addr12 = self.pg1.remote_hosts[1].ip4
701 Proxy12 = VppDHCPProxy(
702 self, server_addr12, src_addr, rx_vrf_id=1, server_vrf_id=1
704 Proxy12.add_vpp_config()
707 # We'll need an ARP entry for the server to send it packets
709 arp_entry = VppNeighbor(
711 self.pg1.sw_if_index,
712 self.pg1.remote_hosts[1].mac,
713 self.pg1.remote_hosts[1].ip4,
715 arp_entry.add_vpp_config()
718 # Send a discover from the client. expect two relayed messages
719 # The frist packet is sent to the second server
720 # We're not enforcing that here, it's just the way it is.
722 self.pg4.add_stream(pkts_disc_vrf1)
723 self.pg_enable_capture(self.pg_interfaces)
726 rx = self.pg1.get_capture(2)
728 option_82 = self.verify_relayed_dhcp_discover(
732 dst_mac=self.pg1.remote_hosts[1].mac,
733 dst_ip=self.pg1.remote_hosts[1].ip4,
737 self.verify_relayed_dhcp_discover(
738 rx[1], self.pg1, src_intf=self.pg4, fib_id=1, oui=4
742 # Send both packets back. Client gets both.
745 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
746 / IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4)
747 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
751 ("message-type", "offer"),
752 ("relay_agent_Information", option_82),
758 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
759 / IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4)
760 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
764 ("message-type", "offer"),
765 ("relay_agent_Information", option_82),
772 self.pg1.add_stream(pkts)
773 self.pg_enable_capture(self.pg_interfaces)
776 rx = self.pg4.get_capture(2)
778 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
779 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
782 # Ensure offers from non-servers are dropeed
785 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
786 / IP(src="8.8.8.8", dst=self.pg1.local_ip4)
787 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT)
791 ("message-type", "offer"),
792 ("relay_agent_Information", option_82),
797 self.send_and_assert_no_replies(self.pg1, p2, "DHCP offer from non-server")
800 # Ensure only the discover is sent to multiple servers
803 Ether(dst="ff:ff:ff:ff:ff:ff", src=self.pg4.remote_mac)
804 / IP(src="0.0.0.0", dst="255.255.255.255")
805 / UDP(sport=DHCP4_CLIENT_PORT, dport=DHCP4_SERVER_PORT)
807 / DHCP(options=[("message-type", "request"), ("end")])
810 self.pg4.add_stream(p_req_vrf1)
811 self.pg_enable_capture(self.pg_interfaces)
814 rx = self.pg1.get_capture(1)
817 # Remove the second DHCP server
819 Proxy12.remove_vpp_config()
822 # Test we can still relay with the first
824 self.pg4.add_stream(pkts_disc_vrf1)
825 self.pg_enable_capture(self.pg_interfaces)
828 rx = self.pg1.get_capture(1)
830 self.verify_relayed_dhcp_discover(
831 rx, self.pg1, src_intf=self.pg4, fib_id=1, oui=4
835 # Remove the VSS config
836 # relayed DHCP has default vlaues in the option.
838 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_add=0)
839 self.vapi.dhcp_proxy_set_vss(tbl_id=2, is_add=0)
841 self.pg4.add_stream(pkts_disc_vrf1)
842 self.pg_enable_capture(self.pg_interfaces)
845 rx = self.pg1.get_capture(1)
847 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
850 # remove DHCP config to cleanup
852 Proxy1.remove_vpp_config()
853 Proxy2.remove_vpp_config()
855 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0, "DHCP cleanup VRF 0")
856 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1, "DHCP cleanup VRF 1")
857 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2, "DHCP cleanup VRF 2")
859 self.pg3.unconfig_ip4()
860 self.pg4.unconfig_ip4()
861 self.pg5.unconfig_ip4()
863 def test_dhcp6_proxy(self):
866 # Verify no response to DHCP request without DHCP config
868 dhcp_solicit_dst = "ff02::1:2"
869 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
870 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
871 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
872 server_addr_vrf0 = self.pg0.remote_ip6
873 src_addr_vrf0 = self.pg0.local_ip6
874 server_addr_vrf1 = self.pg1.remote_ip6
875 src_addr_vrf1 = self.pg1.local_ip6
876 server_addr_vrf2 = self.pg2.remote_ip6
877 src_addr_vrf2 = self.pg2.local_ip6
879 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
881 Ether(dst=dmac, src=self.pg3.remote_mac)
882 / IPv6(src=dhcp_solicit_src_vrf0, dst=dhcp_solicit_dst)
883 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
887 Ether(dst=dmac, src=self.pg4.remote_mac)
888 / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst)
889 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
893 Ether(dst=dmac, src=self.pg5.remote_mac)
894 / IPv6(src=dhcp_solicit_src_vrf2, dst=dhcp_solicit_dst)
895 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
899 self.send_and_assert_no_replies(
900 self.pg3, p_solicit_vrf0, "DHCP with no configuration"
902 self.send_and_assert_no_replies(
903 self.pg4, p_solicit_vrf1, "DHCP with no configuration"
905 self.send_and_assert_no_replies(
906 self.pg5, p_solicit_vrf2, "DHCP with no configuration"
910 # DHCPv6 config in VRF 0.
911 # Packets still dropped because the client facing interface has no
914 Proxy = VppDHCPProxy(
915 self, server_addr_vrf0, src_addr_vrf0, rx_vrf_id=0, server_vrf_id=0
917 Proxy.add_vpp_config()
919 self.send_and_assert_no_replies(
920 self.pg3, p_solicit_vrf0, "DHCP with no configuration"
922 self.send_and_assert_no_replies(
923 self.pg4, p_solicit_vrf1, "DHCP with no configuration"
927 # configure an IP address on the client facing interface
929 self.pg3.config_ip6()
932 # Now the DHCP requests are relayed to the server
934 self.pg3.add_stream(p_solicit_vrf0)
935 self.pg_enable_capture(self.pg_interfaces)
938 rx = self.pg0.get_capture(1)
940 self.verify_dhcp6_solicit(
941 rx[0], self.pg0, dhcp_solicit_src_vrf0, self.pg3.remote_mac
945 # Exception cases for rejected relay responses
948 # 1 - not a relay reply
950 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
951 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
952 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
955 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 not a relay reply")
957 # 2 - no relay message option
959 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
960 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
961 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
965 self.send_and_assert_no_replies(
966 self.pg3, p_adv_vrf0, "DHCP not a relay message"
971 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
972 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
973 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
975 / DHCP6OptRelayMsg(optlen=0)
978 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 no circuit ID")
979 # 4 - wrong circuit ID
981 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
982 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
983 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
985 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
986 / DHCP6OptRelayMsg(optlen=0)
989 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0, "DHCP6 wrong circuit ID")
992 # Send the relay response (the advertisement)
995 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
996 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
997 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
999 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x04")
1000 / DHCP6OptRelayMsg(optlen=0)
1001 / DHCP6_Advertise(trid=1)
1002 / DHCP6OptStatusCode(statuscode=0)
1004 pkts_adv_vrf0 = [p_adv_vrf0]
1006 self.pg0.add_stream(pkts_adv_vrf0)
1007 self.pg_enable_capture(self.pg_interfaces)
1010 rx = self.pg3.get_capture(1)
1012 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
1015 # Send the relay response (the advertisement)
1016 # - with peer address
1018 Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac)
1019 / IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6)
1020 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1021 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0)
1022 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x04")
1023 / DHCP6OptRelayMsg(optlen=0)
1024 / DHCP6_Advertise(trid=1)
1025 / DHCP6OptStatusCode(statuscode=0)
1027 pkts_adv_vrf0 = [p_adv_vrf0]
1029 self.pg0.add_stream(pkts_adv_vrf0)
1030 self.pg_enable_capture(self.pg_interfaces)
1033 rx = self.pg3.get_capture(1)
1035 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
1038 # Add all the config for VRF 1 & 2
1040 Proxy1 = VppDHCPProxy(
1041 self, server_addr_vrf1, src_addr_vrf1, rx_vrf_id=1, server_vrf_id=1
1043 Proxy1.add_vpp_config()
1044 self.pg4.config_ip6()
1046 Proxy2 = VppDHCPProxy(
1047 self, server_addr_vrf2, src_addr_vrf2, rx_vrf_id=2, server_vrf_id=2
1049 Proxy2.add_vpp_config()
1050 self.pg5.config_ip6()
1055 self.pg4.add_stream(p_solicit_vrf1)
1056 self.pg_enable_capture(self.pg_interfaces)
1059 rx = self.pg1.get_capture(1)
1061 self.verify_dhcp6_solicit(
1062 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1068 self.pg5.add_stream(p_solicit_vrf2)
1069 self.pg_enable_capture(self.pg_interfaces)
1072 rx = self.pg2.get_capture(1)
1074 self.verify_dhcp6_solicit(
1075 rx[0], self.pg2, dhcp_solicit_src_vrf2, self.pg5.remote_mac
1082 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1083 / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6)
1084 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1085 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1086 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1087 / DHCP6OptRelayMsg(optlen=0)
1088 / DHCP6_Advertise(trid=1)
1089 / DHCP6OptStatusCode(statuscode=0)
1091 pkts_adv_vrf1 = [p_adv_vrf1]
1093 self.pg1.add_stream(pkts_adv_vrf1)
1094 self.pg_enable_capture(self.pg_interfaces)
1097 rx = self.pg4.get_capture(1)
1099 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1104 self.vapi.dhcp_proxy_set_vss(
1105 tbl_id=1, vss_type=1, oui=4, vpn_index=1, is_ipv6=1, is_add=1
1107 self.vapi.dhcp_proxy_set_vss(
1108 tbl_id=2, vss_type=0, vpn_ascii_id="IPv6-table-2", is_ipv6=1, is_add=1
1111 self.pg4.add_stream(p_solicit_vrf1)
1112 self.pg_enable_capture(self.pg_interfaces)
1115 rx = self.pg1.get_capture(1)
1117 self.verify_dhcp6_solicit(
1118 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac, fib_id=1, oui=4
1121 self.pg5.add_stream(p_solicit_vrf2)
1122 self.pg_enable_capture(self.pg_interfaces)
1125 rx = self.pg2.get_capture(1)
1127 self.verify_dhcp6_solicit(
1130 dhcp_solicit_src_vrf2,
1131 self.pg5.remote_mac,
1132 vpn_id="IPv6-table-2",
1136 # Remove the VSS config
1137 # relayed DHCP has default vlaues in the option.
1139 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_ipv6=1, is_add=0)
1141 self.pg4.add_stream(p_solicit_vrf1)
1142 self.pg_enable_capture(self.pg_interfaces)
1145 rx = self.pg1.get_capture(1)
1147 self.verify_dhcp6_solicit(
1148 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1152 # Add a second DHCP server in VRF 1
1153 # expect clients messages to be relay to both configured servers
1155 self.pg1.generate_remote_hosts(2)
1156 server_addr12 = self.pg1.remote_hosts[1].ip6
1158 Proxy12 = VppDHCPProxy(
1159 self, server_addr12, src_addr_vrf1, rx_vrf_id=1, server_vrf_id=1
1161 Proxy12.add_vpp_config()
1164 # We'll need an ND entry for the server to send it packets
1166 nd_entry = VppNeighbor(
1168 self.pg1.sw_if_index,
1169 self.pg1.remote_hosts[1].mac,
1170 self.pg1.remote_hosts[1].ip6,
1172 nd_entry.add_vpp_config()
1175 # Send a discover from the client. expect two relayed messages
1176 # The frist packet is sent to the second server
1177 # We're not enforcing that here, it's just the way it is.
1179 self.pg4.add_stream(p_solicit_vrf1)
1180 self.pg_enable_capture(self.pg_interfaces)
1183 rx = self.pg1.get_capture(2)
1185 self.verify_dhcp6_solicit(
1186 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1188 self.verify_dhcp6_solicit(
1191 dhcp_solicit_src_vrf1,
1192 self.pg4.remote_mac,
1193 dst_mac=self.pg1.remote_hosts[1].mac,
1194 dst_ip=self.pg1.remote_hosts[1].ip6,
1198 # Send both packets back. Client gets both.
1201 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac)
1202 / IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6)
1203 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1204 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1205 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1206 / DHCP6OptRelayMsg(optlen=0)
1207 / DHCP6_Advertise(trid=1)
1208 / DHCP6OptStatusCode(statuscode=0)
1211 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac)
1212 / IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6)
1213 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1214 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1215 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1216 / DHCP6OptRelayMsg(optlen=0)
1217 / DHCP6_Advertise(trid=1)
1218 / DHCP6OptStatusCode(statuscode=0)
1223 self.pg1.add_stream(pkts)
1224 self.pg_enable_capture(self.pg_interfaces)
1227 rx = self.pg4.get_capture(2)
1229 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1230 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
1233 # Ensure only solicit messages are duplicated
1236 Ether(dst=dmac, src=self.pg4.remote_mac)
1237 / IPv6(src=dhcp_solicit_src_vrf1, dst=dhcp_solicit_dst)
1238 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_CLIENT_PORT)
1242 self.pg4.add_stream(p_request_vrf1)
1243 self.pg_enable_capture(self.pg_interfaces)
1246 rx = self.pg1.get_capture(1)
1249 # Test we drop DHCP packets from addresses that are not configured as
1253 Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac)
1254 / IPv6(dst=self.pg1.local_ip6, src="3001::1")
1255 / UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT)
1256 / DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1)
1257 / DHCP6OptIfaceId(optlen=4, ifaceid="\x00\x00\x00\x05")
1258 / DHCP6OptRelayMsg(optlen=0)
1259 / DHCP6_Advertise(trid=1)
1260 / DHCP6OptStatusCode(statuscode=0)
1262 self.send_and_assert_no_replies(self.pg1, p2, "DHCP6 not from server")
1265 # Remove the second DHCP server
1267 Proxy12.remove_vpp_config()
1270 # Test we can still relay with the first
1272 self.pg4.add_stream(p_solicit_vrf1)
1273 self.pg_enable_capture(self.pg_interfaces)
1276 rx = self.pg1.get_capture(1)
1278 self.verify_dhcp6_solicit(
1279 rx[0], self.pg1, dhcp_solicit_src_vrf1, self.pg4.remote_mac
1285 Proxy.remove_vpp_config()
1286 Proxy1.remove_vpp_config()
1287 Proxy2.remove_vpp_config()
1289 self.pg3.unconfig_ip6()
1290 self.pg4.unconfig_ip6()
1291 self.pg5.unconfig_ip6()
1293 def test_dhcp_client(self):
1296 vdscp = VppEnum.vl_api_ip_dscp_t
1297 hostname = "universal-dp"
1299 self.pg_enable_capture(self.pg_interfaces)
1302 # Configure DHCP client on PG3 and capture the discover sent
1304 Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
1305 Client.add_vpp_config()
1306 self.assertTrue(Client.query_vpp_config())
1308 rx = self.pg3.get_capture(1)
1310 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1313 # Send back on offer, expect the request
1316 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1317 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1318 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1320 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1324 ("message-type", "offer"),
1325 ("server_id", self.pg3.remote_ip4),
1331 self.pg3.add_stream(p_offer)
1332 self.pg_enable_capture(self.pg_interfaces)
1335 rx = self.pg3.get_capture(1)
1336 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
1339 # Send an acknowledgment
1342 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1343 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1344 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1346 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1350 ("message-type", "ack"),
1351 ("subnet_mask", "255.255.255.0"),
1352 ("router", self.pg3.remote_ip4),
1353 ("server_id", self.pg3.remote_ip4),
1354 ("lease_time", 43200),
1360 self.pg3.add_stream(p_ack)
1361 self.pg_enable_capture(self.pg_interfaces)
1365 # We'll get an ARP request for the router address
1367 rx = self.pg3.get_capture(1)
1369 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1370 self.pg_enable_capture(self.pg_interfaces)
1373 # At the end of this procedure there should be a connected route
1376 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1377 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1380 # remove the DHCP config
1382 Client.remove_vpp_config()
1385 # and now the route should be gone
1387 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1388 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1391 # Start the procedure again. this time have VPP send the client-ID
1392 # and set the DSCP value
1394 self.pg3.admin_down()
1398 self.pg3.sw_if_index,
1400 id=self.pg3.local_mac,
1401 dscp=vdscp.IP_API_DSCP_EF,
1403 Client.add_vpp_config()
1405 rx = self.pg3.get_capture(1)
1407 self.verify_orig_dhcp_discover(
1408 rx[0], self.pg3, hostname, self.pg3.local_mac, dscp=vdscp.IP_API_DSCP_EF
1411 # TODO: VPP DHCP client should not accept DHCP OFFER message with
1412 # the XID (Transaction ID) not matching the XID of the most recent
1413 # DHCP DISCOVERY message.
1414 # Such DHCP OFFER message must be silently discarded - RFC2131.
1415 # Reported in Jira ticket: VPP-99
1416 self.pg3.add_stream(p_offer)
1417 self.pg_enable_capture(self.pg_interfaces)
1420 rx = self.pg3.get_capture(1)
1421 self.verify_orig_dhcp_request(
1422 rx[0], self.pg3, hostname, self.pg3.local_ip4, dscp=vdscp.IP_API_DSCP_EF
1426 # unicast the ack to the offered address
1429 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1430 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1431 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1433 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1437 ("message-type", "ack"),
1438 ("subnet_mask", "255.255.255.0"),
1439 ("router", self.pg3.remote_ip4),
1440 ("server_id", self.pg3.remote_ip4),
1441 ("lease_time", 43200),
1447 self.pg3.add_stream(p_ack)
1448 self.pg_enable_capture(self.pg_interfaces)
1452 # We'll get an ARP request for the router address
1454 rx = self.pg3.get_capture(1)
1456 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1457 self.pg_enable_capture(self.pg_interfaces)
1460 # At the end of this procedure there should be a connected route
1463 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1464 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1467 # remove the DHCP config
1469 Client.remove_vpp_config()
1471 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1472 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1475 # Rince and repeat, this time with VPP configured not to set
1476 # the braodcast flag in the discover and request messages,
1477 # and for the server to unicast the responses.
1479 # Configure DHCP client on PG3 and capture the discover sent
1481 Client.set_client(self.pg3.sw_if_index, hostname, set_broadcast_flag=False)
1482 Client.add_vpp_config()
1484 rx = self.pg3.get_capture(1)
1486 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname, broadcast=False)
1489 # Send back on offer, unicasted to the offered address.
1490 # Expect the request.
1493 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1494 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1495 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1497 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1501 ("message-type", "offer"),
1502 ("server_id", self.pg3.remote_ip4),
1508 self.pg3.add_stream(p_offer)
1509 self.pg_enable_capture(self.pg_interfaces)
1512 rx = self.pg3.get_capture(1)
1513 self.verify_orig_dhcp_request(
1514 rx[0], self.pg3, hostname, self.pg3.local_ip4, broadcast=False
1518 # Send an acknowledgment, the lease renewal time is 2 seconds
1519 # so we should expect the renew straight after
1522 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1523 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1524 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1526 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1530 ("message-type", "ack"),
1531 ("subnet_mask", "255.255.255.0"),
1532 ("router", self.pg3.remote_ip4),
1533 ("server_id", self.pg3.remote_ip4),
1534 ("lease_time", 43200),
1535 ("renewal_time", 2),
1541 self.pg3.add_stream(p_ack)
1542 self.pg_enable_capture(self.pg_interfaces)
1546 # We'll get an ARP request for the router address
1548 rx = self.pg3.get_capture(1)
1550 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1551 self.pg_enable_capture(self.pg_interfaces)
1554 # At the end of this procedure there should be a connected route
1557 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1558 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1561 # read the DHCP client details from a dump
1563 clients = self.vapi.dhcp_client_dump()
1565 self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
1566 self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
1567 self.assertEqual(clients[0].client.hostname, hostname)
1568 self.assertEqual(clients[0].lease.hostname, hostname)
1569 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1570 self.assertEqual(clients[0].lease.state, 2)
1571 self.assertEqual(clients[0].lease.mask_width, 24)
1572 self.assertEqual(str(clients[0].lease.router_address), self.pg3.remote_ip4)
1573 self.assertEqual(str(clients[0].lease.host_address), self.pg3.local_ip4)
1576 # wait for the unicasted renewal
1577 # the first attempt will be an ARP packet, since we have not yet
1578 # responded to VPP's request
1580 self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
1581 rx = self.pg3.get_capture(1, timeout=10)
1583 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1585 # respond to the arp
1586 p_arp = Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) / ARP(
1588 hwdst=self.pg3.local_mac,
1589 hwsrc=self.pg3.remote_mac,
1590 pdst=self.pg3.local_ip4,
1591 psrc=self.pg3.remote_ip4,
1593 self.pg3.add_stream(p_arp)
1594 self.pg_enable_capture(self.pg_interfaces)
1597 # the next packet is the unicasted renewal
1598 rx = self.pg3.get_capture(1, timeout=10)
1599 self.verify_orig_dhcp_request(
1600 rx[0], self.pg3, hostname, self.pg3.local_ip4, l2_bc=False, broadcast=False
1603 # send an ACK with different data from the original offer *
1604 self.pg3.generate_remote_hosts(4)
1606 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1607 / IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4)
1608 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1611 yiaddr=self.pg3.remote_hosts[3].ip4,
1612 chaddr=mac_pton(self.pg3.local_mac),
1616 ("message-type", "ack"),
1617 ("subnet_mask", "255.255.255.0"),
1618 ("router", self.pg3.remote_hosts[1].ip4),
1619 ("server_id", self.pg3.remote_hosts[2].ip4),
1620 ("lease_time", 43200),
1621 ("renewal_time", 2),
1627 self.pg3.add_stream(p_ack)
1628 self.pg_enable_capture(self.pg_interfaces)
1632 # read the DHCP client details from a dump
1634 clients = self.vapi.dhcp_client_dump()
1636 self.assertEqual(clients[0].client.sw_if_index, self.pg3.sw_if_index)
1637 self.assertEqual(clients[0].lease.sw_if_index, self.pg3.sw_if_index)
1638 self.assertEqual(clients[0].client.hostname, hostname)
1639 self.assertEqual(clients[0].lease.hostname, hostname)
1640 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1641 self.assertEqual(clients[0].lease.state, 2)
1642 self.assertEqual(clients[0].lease.mask_width, 24)
1644 str(clients[0].lease.router_address), self.pg3.remote_hosts[1].ip4
1647 str(clients[0].lease.host_address), self.pg3.remote_hosts[3].ip4
1651 # remove the DHCP config
1653 Client.remove_vpp_config()
1656 # and now the route should be gone
1658 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1659 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1662 # Start the procedure again. Use requested lease time option.
1663 # this time wait for the lease to expire and the client to
1667 self.pg3.admin_down()
1670 self.pg_enable_capture(self.pg_interfaces)
1671 Client.set_client(self.pg3.sw_if_index, hostname)
1672 Client.add_vpp_config()
1674 rx = self.pg3.get_capture(1)
1676 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1679 # Send back on offer with requested lease time, expect the request
1683 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1684 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1685 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1687 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1691 ("message-type", "offer"),
1692 ("server_id", self.pg3.remote_ip4),
1693 ("lease_time", lease_time),
1699 self.pg3.add_stream(p_offer)
1700 self.pg_enable_capture(self.pg_interfaces)
1703 rx = self.pg3.get_capture(1)
1704 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname, self.pg3.local_ip4)
1707 # Send an acknowledgment
1710 Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac)
1711 / IP(src=self.pg3.remote_ip4, dst="255.255.255.255")
1712 / UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT)
1714 op=1, yiaddr=self.pg3.local_ip4, chaddr=mac_pton(self.pg3.local_mac)
1718 ("message-type", "ack"),
1719 ("subnet_mask", "255.255.255.0"),
1720 ("router", self.pg3.remote_ip4),
1721 ("server_id", self.pg3.remote_ip4),
1722 ("lease_time", lease_time),
1728 self.pg3.add_stream(p_ack)
1729 self.pg_enable_capture(self.pg_interfaces)
1733 # We'll get an ARP request for the router address
1735 rx = self.pg3.get_capture(1)
1737 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1740 # At the end of this procedure there should be a connected route
1743 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1744 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1747 # the route should be gone after the lease expires
1749 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1750 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
1753 # remove the DHCP config
1755 Client.remove_vpp_config()
1757 def test_dhcp_client_vlan(self):
1758 """DHCP Client w/ VLAN"""
1760 vdscp = VppEnum.vl_api_ip_dscp_t
1761 vqos = VppEnum.vl_api_qos_source_t
1762 hostname = "universal-dp"
1764 self.pg_enable_capture(self.pg_interfaces)
1766 vlan_100 = VppDot1QSubint(self, self.pg3, 100)
1769 output = [scapy.compat.chb(4)] * 256
1770 os = b"".join(output)
1771 rows = [{"outputs": os}, {"outputs": os}, {"outputs": os}, {"outputs": os}]
1773 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
1775 self, vlan_100, qem1, vqos.QOS_API_SOURCE_VLAN
1779 # Configure DHCP client on PG3 and capture the discover sent
1781 Client = VppDHCPClient(
1782 self, vlan_100.sw_if_index, hostname, dscp=vdscp.IP_API_DSCP_EF
1784 Client.add_vpp_config()
1786 rx = self.pg3.get_capture(1)
1788 self.assertEqual(rx[0][Dot1Q].vlan, 100)
1789 self.assertEqual(rx[0][Dot1Q].prio, 2)
1791 self.verify_orig_dhcp_discover(
1792 rx[0], self.pg3, hostname, dscp=vdscp.IP_API_DSCP_EF
1796 if __name__ == "__main__":
1797 unittest.main(testRunner=VppTestRunner)