8 from framework import VppTestCase, VppTestRunner, running_extended_tests
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 DHCP6, DHCP6_Solicit, DHCP6_RelayForward, \
20 DHCP6_RelayReply, DHCP6_Advertise, DHCP6OptRelayMsg, DHCP6OptIfaceId, \
21 DHCP6OptStatusCode, DHCP6OptVSS, DHCP6OptClientLinkLayerAddr, DHCP6_Request
22 from socket import AF_INET, AF_INET6, inet_pton, inet_ntop
23 from scapy.utils6 import in6_ptop
24 from vpp_papi import mac_pton, VppEnum
25 from vpp_sub_interface import VppDot1QSubint
26 from vpp_qos import VppQosEgressMap, VppQosMark
27 from vpp_dhcp import VppDHCPClient, VppDHCPProxy
30 DHCP4_CLIENT_PORT = 68
31 DHCP4_SERVER_PORT = 67
32 DHCP6_CLIENT_PORT = 547
33 DHCP6_SERVER_PORT = 546
37 class TestDHCP(VppTestCase):
38 """ DHCP Test Case """
42 super(TestDHCP, cls).setUpClass()
45 def tearDownClass(cls):
46 super(TestDHCP, cls).tearDownClass()
49 super(TestDHCP, self).setUp()
51 # create 6 pg interfaces for pg0 to pg5
52 self.create_pg_interfaces(range(6))
55 # pg0 to 2 are IP configured in VRF 0, 1 and 2.
56 # pg3 to 5 are non IP-configured in VRF 0, 1 and 2.
58 for table_id in range(1, 4):
59 tbl4 = VppIpTable(self, table_id)
61 self.tables.append(tbl4)
62 tbl6 = VppIpTable(self, table_id, is_ip6=1)
64 self.tables.append(tbl6)
67 for i in self.pg_interfaces[:3]:
69 i.set_table_ip4(table_id)
70 i.set_table_ip6(table_id)
78 for i in self.pg_interfaces[3:]:
80 i.set_table_ip4(table_id)
81 i.set_table_ip6(table_id)
85 for i in self.pg_interfaces[:3]:
89 for i in self.pg_interfaces:
93 super(TestDHCP, self).tearDown()
95 def verify_dhcp_has_option(self, pkt, option, value):
99 for i in dhcp.options:
100 if isinstance(i, tuple):
102 self.assertEqual(i[1], value)
105 self.assertTrue(found)
107 def validate_relay_options(self, pkt, intf, ip_addr, vpn_id, fib_id, oui):
113 for i in dhcp.options:
114 if isinstance(i, tuple):
115 if i[0] == "relay_agent_Information":
117 # There are two sb-options present - each of length 6.
121 self.assertEqual(len(data), 24)
122 elif len(vpn_id) > 0:
123 self.assertEqual(len(data), len(vpn_id) + 17)
125 self.assertEqual(len(data), 12)
128 # First sub-option is ID 1, len 4, then encoded
129 # sw_if_index. This test uses low valued indicies
131 # The ID space is VPP internal - so no matching value
134 self.assertEqual(six.byte2int(data[0:1]), 1)
135 self.assertEqual(six.byte2int(data[1:2]), 4)
136 self.assertEqual(six.byte2int(data[2:3]), 0)
137 self.assertEqual(six.byte2int(data[3:4]), 0)
138 self.assertEqual(six.byte2int(data[4:5]), 0)
139 self.assertEqual(six.byte2int(data[5:6]),
143 # next sub-option is the IP address of the client side
145 # sub-option ID=5, length (of a v4 address)=4
147 claddr = socket.inet_pton(AF_INET, ip_addr)
149 self.assertEqual(six.byte2int(data[6:7]), 5)
150 self.assertEqual(six.byte2int(data[7:8]), 4)
151 self.assertEqual(data[8], claddr[0])
152 self.assertEqual(data[9], claddr[1])
153 self.assertEqual(data[10], claddr[2])
154 self.assertEqual(data[11], claddr[3])
157 # sub-option 151 encodes vss_type 1,
158 # the 3 byte oui and the 4 byte fib_id
159 self.assertEqual(id_len, 0)
160 self.assertEqual(six.byte2int(data[12:13]), 151)
161 self.assertEqual(six.byte2int(data[13:14]), 8)
162 self.assertEqual(six.byte2int(data[14:15]), 1)
163 self.assertEqual(six.byte2int(data[15:16]), 0)
164 self.assertEqual(six.byte2int(data[16:17]), 0)
165 self.assertEqual(six.byte2int(data[17:18]), oui)
166 self.assertEqual(six.byte2int(data[18:19]), 0)
167 self.assertEqual(six.byte2int(data[19:20]), 0)
168 self.assertEqual(six.byte2int(data[20:21]), 0)
169 self.assertEqual(six.byte2int(data[21:22]), fib_id)
171 # VSS control sub-option
172 self.assertEqual(six.byte2int(data[22:23]), 152)
173 self.assertEqual(six.byte2int(data[23:24]), 0)
176 # sub-option 151 encode vss_type of 0
177 # followerd by vpn_id in ascii
178 self.assertEqual(oui, 0)
179 self.assertEqual(six.byte2int(data[12:13]), 151)
180 self.assertEqual(six.byte2int(data[13:14]), id_len + 1)
181 self.assertEqual(six.byte2int(data[14:15]), 0)
182 self.assertEqual(data[15:15 + id_len].decode('ascii'),
185 # VSS control sub-option
186 self.assertEqual(six.byte2int(data[15 + len(vpn_id):
189 self.assertEqual(six.byte2int(data[16 + len(vpn_id):
194 self.assertTrue(found)
198 def verify_dhcp_msg_type(self, pkt, name):
201 for o in dhcp.options:
202 if isinstance(o, tuple):
203 if o[0] == "message-type" \
204 and DHCPTypes[o[1]] == name:
206 self.assertTrue(found)
208 def verify_dhcp_offer(self, pkt, intf, vpn_id="", fib_id=0, oui=0):
210 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
211 self.assertEqual(ether.src, intf.local_mac)
214 self.assertEqual(ip.dst, "255.255.255.255")
215 self.assertEqual(ip.src, intf.local_ip4)
218 self.assertEqual(udp.dport, DHCP4_CLIENT_PORT)
219 self.assertEqual(udp.sport, DHCP4_SERVER_PORT)
221 self.verify_dhcp_msg_type(pkt, "offer")
222 data = self.validate_relay_options(pkt, intf, intf.local_ip4,
225 def verify_orig_dhcp_pkt(self, pkt, intf, dscp, l2_bc=True):
228 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
230 self.assertEqual(ether.dst, intf.remote_mac)
231 self.assertEqual(ether.src, intf.local_mac)
236 self.assertEqual(ip.dst, "255.255.255.255")
237 self.assertEqual(ip.src, "0.0.0.0")
239 self.assertEqual(ip.dst, intf.remote_ip4)
240 self.assertEqual(ip.src, intf.local_ip4)
241 self.assertEqual(ip.tos, dscp)
244 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
245 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
247 def verify_orig_dhcp_discover(self, pkt, intf, hostname, client_id=None,
248 broadcast=True, dscp=0):
249 self.verify_orig_dhcp_pkt(pkt, intf, dscp)
251 self.verify_dhcp_msg_type(pkt, "discover")
252 self.verify_dhcp_has_option(pkt, "hostname",
253 hostname.encode('ascii'))
255 client_id = '\x00' + client_id
256 self.verify_dhcp_has_option(pkt, "client_id",
257 client_id.encode('ascii'))
259 self.assertEqual(bootp.ciaddr, "0.0.0.0")
260 self.assertEqual(bootp.giaddr, "0.0.0.0")
262 self.assertEqual(bootp.flags, 0x8000)
264 self.assertEqual(bootp.flags, 0x0000)
266 def verify_orig_dhcp_request(self, pkt, intf, hostname, ip,
270 self.verify_orig_dhcp_pkt(pkt, intf, dscp, l2_bc=l2_bc)
272 self.verify_dhcp_msg_type(pkt, "request")
273 self.verify_dhcp_has_option(pkt, "hostname",
274 hostname.encode('ascii'))
275 self.verify_dhcp_has_option(pkt, "requested_addr", ip)
279 self.assertEqual(bootp.ciaddr, "0.0.0.0")
281 self.assertEqual(bootp.ciaddr, intf.local_ip4)
282 self.assertEqual(bootp.giaddr, "0.0.0.0")
285 self.assertEqual(bootp.flags, 0x8000)
287 self.assertEqual(bootp.flags, 0x0000)
289 def verify_relayed_dhcp_discover(self, pkt, intf, src_intf=None,
292 dst_mac=None, dst_ip=None):
294 dst_mac = intf.remote_mac
296 dst_ip = intf.remote_ip4
299 self.assertEqual(ether.dst, dst_mac)
300 self.assertEqual(ether.src, intf.local_mac)
303 self.assertEqual(ip.dst, dst_ip)
304 self.assertEqual(ip.src, intf.local_ip4)
307 self.assertEqual(udp.dport, DHCP4_SERVER_PORT)
308 self.assertEqual(udp.sport, DHCP4_CLIENT_PORT)
313 for o in dhcp.options:
314 if isinstance(o, tuple):
315 if o[0] == "message-type" \
316 and DHCPTypes[o[1]] == "discover":
318 self.assertTrue(is_discover)
320 data = self.validate_relay_options(pkt, src_intf,
326 def verify_dhcp6_solicit(self, pkt, intf,
334 dst_mac = intf.remote_mac
336 dst_ip = in6_ptop(intf.remote_ip6)
339 self.assertEqual(ether.dst, dst_mac)
340 self.assertEqual(ether.src, intf.local_mac)
343 self.assertEqual(in6_ptop(ip.dst), dst_ip)
344 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
347 self.assertEqual(udp.dport, DHCP6_CLIENT_PORT)
348 self.assertEqual(udp.sport, DHCP6_SERVER_PORT)
350 relay = pkt[DHCP6_RelayForward]
351 self.assertEqual(in6_ptop(relay.peeraddr), in6_ptop(peer_ip))
352 oid = pkt[DHCP6OptIfaceId]
353 cll = pkt[DHCP6OptClientLinkLayerAddr]
354 self.assertEqual(cll.optlen, 8)
355 self.assertEqual(cll.lltype, 1)
356 self.assertEqual(cll.clladdr, peer_mac)
361 self.assertEqual(id_len, 0)
362 vss = pkt[DHCP6OptVSS]
363 self.assertEqual(vss.optlen, 8)
364 self.assertEqual(vss.type, 1)
365 # the OUI and FIB-id are really 3 and 4 bytes resp.
366 # but the tested range is small
367 self.assertEqual(six.byte2int(vss.data[0:1]), 0)
368 self.assertEqual(six.byte2int(vss.data[1:2]), 0)
369 self.assertEqual(six.byte2int(vss.data[2:3]), oui)
370 self.assertEqual(six.byte2int(vss.data[3:4]), 0)
371 self.assertEqual(six.byte2int(vss.data[4:5]), 0)
372 self.assertEqual(six.byte2int(vss.data[5:6]), 0)
373 self.assertEqual(six.byte2int(vss.data[6:7]), fib_id)
376 self.assertEqual(oui, 0)
377 vss = pkt[DHCP6OptVSS]
378 self.assertEqual(vss.optlen, id_len + 1)
379 self.assertEqual(vss.type, 0)
380 self.assertEqual(vss.data[0:id_len].decode('ascii'),
383 # the relay message should be an encoded Solicit
384 msg = pkt[DHCP6OptRelayMsg]
385 sol = DHCP6_Solicit()
386 self.assertEqual(msg.optlen, len(sol))
387 self.assertEqual(sol, msg[1])
389 def verify_dhcp6_advert(self, pkt, intf, peer):
391 self.assertEqual(ether.dst, "ff:ff:ff:ff:ff:ff")
392 self.assertEqual(ether.src, intf.local_mac)
395 self.assertEqual(in6_ptop(ip.dst), in6_ptop(peer))
396 self.assertEqual(in6_ptop(ip.src), in6_ptop(intf.local_ip6))
399 self.assertEqual(udp.dport, DHCP6_SERVER_PORT)
400 self.assertEqual(udp.sport, DHCP6_CLIENT_PORT)
402 # not sure why this is not decoding
403 # adv = pkt[DHCP6_Advertise]
405 def wait_for_no_route(self, address, length,
406 n_tries=50, s_time=1):
408 if not find_route(self, address, length):
410 n_tries = n_tries - 1
415 def test_dhcp_proxy(self):
419 # Verify no response to DHCP request without DHCP config
421 p_disc_vrf0 = (Ether(dst="ff:ff:ff:ff:ff:ff",
422 src=self.pg3.remote_mac) /
423 IP(src="0.0.0.0", dst="255.255.255.255") /
424 UDP(sport=DHCP4_CLIENT_PORT,
425 dport=DHCP4_SERVER_PORT) /
427 DHCP(options=[('message-type', 'discover'), ('end')]))
428 pkts_disc_vrf0 = [p_disc_vrf0]
429 p_disc_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
430 src=self.pg4.remote_mac) /
431 IP(src="0.0.0.0", dst="255.255.255.255") /
432 UDP(sport=DHCP4_CLIENT_PORT,
433 dport=DHCP4_SERVER_PORT) /
435 DHCP(options=[('message-type', 'discover'), ('end')]))
436 pkts_disc_vrf1 = [p_disc_vrf1]
437 p_disc_vrf2 = (Ether(dst="ff:ff:ff:ff:ff:ff",
438 src=self.pg5.remote_mac) /
439 IP(src="0.0.0.0", dst="255.255.255.255") /
440 UDP(sport=DHCP4_CLIENT_PORT,
441 dport=DHCP4_SERVER_PORT) /
443 DHCP(options=[('message-type', 'discover'), ('end')]))
444 pkts_disc_vrf2 = [p_disc_vrf2]
446 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
447 "DHCP with no configuration")
448 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
449 "DHCP with no configuration")
450 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
451 "DHCP with no configuration")
454 # Enable DHCP proxy in VRF 0
456 server_addr = self.pg0.remote_ip4
457 src_addr = self.pg0.local_ip4
459 Proxy = VppDHCPProxy(self, server_addr, src_addr, rx_vrf_id=0)
460 Proxy.add_vpp_config()
463 # Discover packets from the client are dropped because there is no
464 # IP address configured on the client facing interface
466 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
467 "Discover DHCP no relay address")
470 # Inject a response from the server
471 # dropped, because there is no IP addrees on the
472 # client interfce to fill in the option.
474 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
475 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
476 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
478 DHCP(options=[('message-type', 'offer'), ('end')]))
481 self.send_and_assert_no_replies(self.pg3, pkts,
482 "Offer DHCP no relay address")
485 # configure an IP address on the client facing interface
487 self.pg3.config_ip4()
490 # Try again with a discover packet
491 # Rx'd packet should be to the server address and from the configured
493 # UDP source ports are unchanged
494 # we've no option 82 config so that should be absent
496 self.pg3.add_stream(pkts_disc_vrf0)
497 self.pg_enable_capture(self.pg_interfaces)
500 rx = self.pg0.get_capture(1)
503 option_82 = self.verify_relayed_dhcp_discover(rx, self.pg0,
507 # Create an DHCP offer reply from the server with a correctly formatted
508 # option 82. i.e. send back what we just captured
509 # The offer, sent mcast to the client, still has option 82.
511 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
512 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
513 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
515 DHCP(options=[('message-type', 'offer'),
516 ('relay_agent_Information', option_82),
520 self.pg0.add_stream(pkts)
521 self.pg_enable_capture(self.pg_interfaces)
524 rx = self.pg3.get_capture(1)
527 self.verify_dhcp_offer(rx, self.pg3)
532 # 1. not our IP address = not checked by VPP? so offer is replayed
534 bad_ip = option_82[0:8] + scapy.compat.chb(33) + option_82[9:]
536 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
537 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
538 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
540 DHCP(options=[('message-type', 'offer'),
541 ('relay_agent_Information', bad_ip),
544 self.send_and_assert_no_replies(self.pg0, pkts,
545 "DHCP offer option 82 bad address")
547 # 2. Not a sw_if_index VPP knows
548 bad_if_index = option_82[0:2] + scapy.compat.chb(33) + option_82[3:]
550 p = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
551 IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4) /
552 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
554 DHCP(options=[('message-type', 'offer'),
555 ('relay_agent_Information', bad_if_index),
558 self.send_and_assert_no_replies(self.pg0, pkts,
559 "DHCP offer option 82 bad if index")
562 # Send a DHCP request in VRF 1. should be dropped.
564 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
565 "DHCP with no configuration VRF 1")
568 # Delete the DHCP config in VRF 0
569 # Should now drop requests.
571 Proxy.remove_vpp_config()
573 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
574 "DHCP config removed VRF 0")
575 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
576 "DHCP config removed VRF 1")
579 # Add DHCP config for VRF 1 & 2
581 server_addr1 = self.pg1.remote_ip4
582 src_addr1 = self.pg1.local_ip4
583 Proxy1 = VppDHCPProxy(
589 Proxy1.add_vpp_config()
591 server_addr2 = self.pg2.remote_ip4
592 src_addr2 = self.pg2.local_ip4
593 Proxy2 = VppDHCPProxy(
599 Proxy2.add_vpp_config()
602 # Confim DHCP requests ok in VRF 1 & 2.
603 # - dropped on IP config on client interface
605 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
606 "DHCP config removed VRF 1")
607 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
608 "DHCP config removed VRF 2")
611 # configure an IP address on the client facing interface
613 self.pg4.config_ip4()
614 self.pg4.add_stream(pkts_disc_vrf1)
615 self.pg_enable_capture(self.pg_interfaces)
617 rx = self.pg1.get_capture(1)
619 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
621 self.pg5.config_ip4()
622 self.pg5.add_stream(pkts_disc_vrf2)
623 self.pg_enable_capture(self.pg_interfaces)
625 rx = self.pg2.get_capture(1)
627 self.verify_relayed_dhcp_discover(rx, self.pg2, src_intf=self.pg5)
631 # table=1, vss_type=1, vpn_index=1, oui=4
632 # table=2, vss_type=0, vpn_id = "ip4-table-2"
633 self.vapi.dhcp_proxy_set_vss(tbl_id=1, vss_type=1,
634 vpn_index=1, oui=4, is_add=1)
635 self.vapi.dhcp_proxy_set_vss(tbl_id=2, vss_type=0,
636 vpn_ascii_id="ip4-table-2", is_add=1)
638 self.pg4.add_stream(pkts_disc_vrf1)
639 self.pg_enable_capture(self.pg_interfaces)
642 rx = self.pg1.get_capture(1)
644 self.verify_relayed_dhcp_discover(rx, self.pg1,
648 self.pg5.add_stream(pkts_disc_vrf2)
649 self.pg_enable_capture(self.pg_interfaces)
652 rx = self.pg2.get_capture(1)
654 self.verify_relayed_dhcp_discover(rx, self.pg2,
656 vpn_id="ip4-table-2")
659 # Add a second DHCP server in VRF 1
660 # expect clients messages to be relay to both configured servers
662 self.pg1.generate_remote_hosts(2)
663 server_addr12 = self.pg1.remote_hosts[1].ip4
665 Proxy12 = VppDHCPProxy(
671 Proxy12.add_vpp_config()
674 # We'll need an ARP entry for the server to send it packets
676 arp_entry = VppNeighbor(self,
677 self.pg1.sw_if_index,
678 self.pg1.remote_hosts[1].mac,
679 self.pg1.remote_hosts[1].ip4)
680 arp_entry.add_vpp_config()
683 # Send a discover from the client. expect two relayed messages
684 # The frist packet is sent to the second server
685 # We're not enforcing that here, it's just the way it is.
687 self.pg4.add_stream(pkts_disc_vrf1)
688 self.pg_enable_capture(self.pg_interfaces)
691 rx = self.pg1.get_capture(2)
693 option_82 = self.verify_relayed_dhcp_discover(
696 dst_mac=self.pg1.remote_hosts[1].mac,
697 dst_ip=self.pg1.remote_hosts[1].ip4,
699 self.verify_relayed_dhcp_discover(rx[1], self.pg1,
704 # Send both packets back. Client gets both.
706 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
707 IP(src=self.pg1.remote_ip4, dst=self.pg1.local_ip4) /
708 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
710 DHCP(options=[('message-type', 'offer'),
711 ('relay_agent_Information', option_82),
713 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
714 IP(src=self.pg1.remote_hosts[1].ip4, dst=self.pg1.local_ip4) /
715 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
717 DHCP(options=[('message-type', 'offer'),
718 ('relay_agent_Information', option_82),
722 self.pg1.add_stream(pkts)
723 self.pg_enable_capture(self.pg_interfaces)
726 rx = self.pg4.get_capture(2)
728 self.verify_dhcp_offer(rx[0], self.pg4, fib_id=1, oui=4)
729 self.verify_dhcp_offer(rx[1], self.pg4, fib_id=1, oui=4)
732 # Ensure offers from non-servers are dropeed
734 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
735 IP(src="8.8.8.8", dst=self.pg1.local_ip4) /
736 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_SERVER_PORT) /
738 DHCP(options=[('message-type', 'offer'),
739 ('relay_agent_Information', option_82),
741 self.send_and_assert_no_replies(self.pg1, p2,
742 "DHCP offer from non-server")
745 # Ensure only the discover is sent to multiple servers
747 p_req_vrf1 = (Ether(dst="ff:ff:ff:ff:ff:ff",
748 src=self.pg4.remote_mac) /
749 IP(src="0.0.0.0", dst="255.255.255.255") /
750 UDP(sport=DHCP4_CLIENT_PORT,
751 dport=DHCP4_SERVER_PORT) /
753 DHCP(options=[('message-type', 'request'),
756 self.pg4.add_stream(p_req_vrf1)
757 self.pg_enable_capture(self.pg_interfaces)
760 rx = self.pg1.get_capture(1)
763 # Remove the second DHCP server
765 Proxy12.remove_vpp_config()
768 # Test we can still relay with the first
770 self.pg4.add_stream(pkts_disc_vrf1)
771 self.pg_enable_capture(self.pg_interfaces)
774 rx = self.pg1.get_capture(1)
776 self.verify_relayed_dhcp_discover(rx, self.pg1,
781 # Remove the VSS config
782 # relayed DHCP has default vlaues in the option.
784 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_add=0)
785 self.vapi.dhcp_proxy_set_vss(tbl_id=2, is_add=0)
787 self.pg4.add_stream(pkts_disc_vrf1)
788 self.pg_enable_capture(self.pg_interfaces)
791 rx = self.pg1.get_capture(1)
793 self.verify_relayed_dhcp_discover(rx, self.pg1, src_intf=self.pg4)
796 # remove DHCP config to cleanup
798 Proxy1.remove_vpp_config()
799 Proxy2.remove_vpp_config()
801 self.send_and_assert_no_replies(self.pg3, pkts_disc_vrf0,
802 "DHCP cleanup VRF 0")
803 self.send_and_assert_no_replies(self.pg4, pkts_disc_vrf1,
804 "DHCP cleanup VRF 1")
805 self.send_and_assert_no_replies(self.pg5, pkts_disc_vrf2,
806 "DHCP cleanup VRF 2")
808 self.pg3.unconfig_ip4()
809 self.pg4.unconfig_ip4()
810 self.pg5.unconfig_ip4()
812 def test_dhcp6_proxy(self):
815 # Verify no response to DHCP request without DHCP config
817 dhcp_solicit_dst = "ff02::1:2"
818 dhcp_solicit_src_vrf0 = mk_ll_addr(self.pg3.remote_mac)
819 dhcp_solicit_src_vrf1 = mk_ll_addr(self.pg4.remote_mac)
820 dhcp_solicit_src_vrf2 = mk_ll_addr(self.pg5.remote_mac)
821 server_addr_vrf0 = self.pg0.remote_ip6
822 src_addr_vrf0 = self.pg0.local_ip6
823 server_addr_vrf1 = self.pg1.remote_ip6
824 src_addr_vrf1 = self.pg1.local_ip6
825 server_addr_vrf2 = self.pg2.remote_ip6
826 src_addr_vrf2 = self.pg2.local_ip6
828 dmac = in6_getnsmac(inet_pton(socket.AF_INET6, dhcp_solicit_dst))
829 p_solicit_vrf0 = (Ether(dst=dmac, src=self.pg3.remote_mac) /
830 IPv6(src=dhcp_solicit_src_vrf0,
831 dst=dhcp_solicit_dst) /
832 UDP(sport=DHCP6_SERVER_PORT,
833 dport=DHCP6_CLIENT_PORT) /
835 p_solicit_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
836 IPv6(src=dhcp_solicit_src_vrf1,
837 dst=dhcp_solicit_dst) /
838 UDP(sport=DHCP6_SERVER_PORT,
839 dport=DHCP6_CLIENT_PORT) /
841 p_solicit_vrf2 = (Ether(dst=dmac, src=self.pg5.remote_mac) /
842 IPv6(src=dhcp_solicit_src_vrf2,
843 dst=dhcp_solicit_dst) /
844 UDP(sport=DHCP6_SERVER_PORT,
845 dport=DHCP6_CLIENT_PORT) /
848 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
849 "DHCP with no configuration")
850 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
851 "DHCP with no configuration")
852 self.send_and_assert_no_replies(self.pg5, p_solicit_vrf2,
853 "DHCP with no configuration")
856 # DHCPv6 config in VRF 0.
857 # Packets still dropped because the client facing interface has no
860 Proxy = VppDHCPProxy(
866 Proxy.add_vpp_config()
868 self.send_and_assert_no_replies(self.pg3, p_solicit_vrf0,
869 "DHCP with no configuration")
870 self.send_and_assert_no_replies(self.pg4, p_solicit_vrf1,
871 "DHCP with no configuration")
874 # configure an IP address on the client facing interface
876 self.pg3.config_ip6()
879 # Now the DHCP requests are relayed to the server
881 self.pg3.add_stream(p_solicit_vrf0)
882 self.pg_enable_capture(self.pg_interfaces)
885 rx = self.pg0.get_capture(1)
887 self.verify_dhcp6_solicit(rx[0], self.pg0,
888 dhcp_solicit_src_vrf0,
892 # Exception cases for rejected relay responses
895 # 1 - not a relay reply
896 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
897 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
898 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
900 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
901 "DHCP6 not a relay reply")
903 # 2 - no relay message option
904 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
905 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
906 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
909 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
910 "DHCP not a relay message")
913 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
914 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
915 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
917 DHCP6OptRelayMsg(optlen=0) /
919 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
920 "DHCP6 no circuit ID")
921 # 4 - wrong circuit ID
922 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
923 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
924 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
926 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
927 DHCP6OptRelayMsg(optlen=0) /
929 self.send_and_assert_no_replies(self.pg3, p_adv_vrf0,
930 "DHCP6 wrong circuit ID")
933 # Send the relay response (the advertisement)
935 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
936 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
937 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
939 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
940 DHCP6OptRelayMsg(optlen=0) /
941 DHCP6_Advertise(trid=1) /
942 DHCP6OptStatusCode(statuscode=0))
943 pkts_adv_vrf0 = [p_adv_vrf0]
945 self.pg0.add_stream(pkts_adv_vrf0)
946 self.pg_enable_capture(self.pg_interfaces)
949 rx = self.pg3.get_capture(1)
951 self.verify_dhcp6_advert(rx[0], self.pg3, "::")
954 # Send the relay response (the advertisement)
955 # - with peer address
956 p_adv_vrf0 = (Ether(dst=self.pg0.local_mac, src=self.pg0.remote_mac) /
957 IPv6(dst=self.pg0.local_ip6, src=self.pg0.remote_ip6) /
958 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
959 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf0) /
960 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x04') /
961 DHCP6OptRelayMsg(optlen=0) /
962 DHCP6_Advertise(trid=1) /
963 DHCP6OptStatusCode(statuscode=0))
964 pkts_adv_vrf0 = [p_adv_vrf0]
966 self.pg0.add_stream(pkts_adv_vrf0)
967 self.pg_enable_capture(self.pg_interfaces)
970 rx = self.pg3.get_capture(1)
972 self.verify_dhcp6_advert(rx[0], self.pg3, dhcp_solicit_src_vrf0)
975 # Add all the config for VRF 1 & 2
977 Proxy1 = VppDHCPProxy(
983 Proxy1.add_vpp_config()
984 self.pg4.config_ip6()
986 Proxy2 = VppDHCPProxy(
992 Proxy2.add_vpp_config()
993 self.pg5.config_ip6()
998 self.pg4.add_stream(p_solicit_vrf1)
999 self.pg_enable_capture(self.pg_interfaces)
1002 rx = self.pg1.get_capture(1)
1004 self.verify_dhcp6_solicit(rx[0], self.pg1,
1005 dhcp_solicit_src_vrf1,
1006 self.pg4.remote_mac)
1011 self.pg5.add_stream(p_solicit_vrf2)
1012 self.pg_enable_capture(self.pg_interfaces)
1015 rx = self.pg2.get_capture(1)
1017 self.verify_dhcp6_solicit(rx[0], self.pg2,
1018 dhcp_solicit_src_vrf2,
1019 self.pg5.remote_mac)
1024 p_adv_vrf1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1025 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1026 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1027 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1028 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1029 DHCP6OptRelayMsg(optlen=0) /
1030 DHCP6_Advertise(trid=1) /
1031 DHCP6OptStatusCode(statuscode=0))
1032 pkts_adv_vrf1 = [p_adv_vrf1]
1034 self.pg1.add_stream(pkts_adv_vrf1)
1035 self.pg_enable_capture(self.pg_interfaces)
1038 rx = self.pg4.get_capture(1)
1040 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1045 self.vapi.dhcp_proxy_set_vss(
1046 tbl_id=1, vss_type=1, oui=4, vpn_index=1, is_ipv6=1, is_add=1)
1047 self.vapi.dhcp_proxy_set_vss(
1050 vpn_ascii_id="IPv6-table-2",
1054 self.pg4.add_stream(p_solicit_vrf1)
1055 self.pg_enable_capture(self.pg_interfaces)
1058 rx = self.pg1.get_capture(1)
1060 self.verify_dhcp6_solicit(rx[0], self.pg1,
1061 dhcp_solicit_src_vrf1,
1062 self.pg4.remote_mac,
1066 self.pg5.add_stream(p_solicit_vrf2)
1067 self.pg_enable_capture(self.pg_interfaces)
1070 rx = self.pg2.get_capture(1)
1072 self.verify_dhcp6_solicit(rx[0], self.pg2,
1073 dhcp_solicit_src_vrf2,
1074 self.pg5.remote_mac,
1075 vpn_id="IPv6-table-2")
1078 # Remove the VSS config
1079 # relayed DHCP has default vlaues in the option.
1081 self.vapi.dhcp_proxy_set_vss(tbl_id=1, is_ipv6=1, is_add=0)
1083 self.pg4.add_stream(p_solicit_vrf1)
1084 self.pg_enable_capture(self.pg_interfaces)
1087 rx = self.pg1.get_capture(1)
1089 self.verify_dhcp6_solicit(rx[0], self.pg1,
1090 dhcp_solicit_src_vrf1,
1091 self.pg4.remote_mac)
1094 # Add a second DHCP server in VRF 1
1095 # expect clients messages to be relay to both configured servers
1097 self.pg1.generate_remote_hosts(2)
1098 server_addr12 = self.pg1.remote_hosts[1].ip6
1100 Proxy12 = VppDHCPProxy(
1106 Proxy12.add_vpp_config()
1109 # We'll need an ND entry for the server to send it packets
1111 nd_entry = VppNeighbor(self,
1112 self.pg1.sw_if_index,
1113 self.pg1.remote_hosts[1].mac,
1114 self.pg1.remote_hosts[1].ip6)
1115 nd_entry.add_vpp_config()
1118 # Send a discover from the client. expect two relayed messages
1119 # The frist packet is sent to the second server
1120 # We're not enforcing that here, it's just the way it is.
1122 self.pg4.add_stream(p_solicit_vrf1)
1123 self.pg_enable_capture(self.pg_interfaces)
1126 rx = self.pg1.get_capture(2)
1128 self.verify_dhcp6_solicit(rx[0], self.pg1,
1129 dhcp_solicit_src_vrf1,
1130 self.pg4.remote_mac)
1131 self.verify_dhcp6_solicit(rx[1], self.pg1,
1132 dhcp_solicit_src_vrf1,
1133 self.pg4.remote_mac,
1134 dst_mac=self.pg1.remote_hosts[1].mac,
1135 dst_ip=self.pg1.remote_hosts[1].ip6)
1138 # Send both packets back. Client gets both.
1140 p1 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_mac) /
1141 IPv6(dst=self.pg1.local_ip6, src=self.pg1.remote_ip6) /
1142 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1143 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1144 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1145 DHCP6OptRelayMsg(optlen=0) /
1146 DHCP6_Advertise(trid=1) /
1147 DHCP6OptStatusCode(statuscode=0))
1148 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1149 IPv6(dst=self.pg1.local_ip6, src=self.pg1._remote_hosts[1].ip6) /
1150 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1151 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1152 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1153 DHCP6OptRelayMsg(optlen=0) /
1154 DHCP6_Advertise(trid=1) /
1155 DHCP6OptStatusCode(statuscode=0))
1159 self.pg1.add_stream(pkts)
1160 self.pg_enable_capture(self.pg_interfaces)
1163 rx = self.pg4.get_capture(2)
1165 self.verify_dhcp6_advert(rx[0], self.pg4, dhcp_solicit_src_vrf1)
1166 self.verify_dhcp6_advert(rx[1], self.pg4, dhcp_solicit_src_vrf1)
1169 # Ensure only solicit messages are duplicated
1171 p_request_vrf1 = (Ether(dst=dmac, src=self.pg4.remote_mac) /
1172 IPv6(src=dhcp_solicit_src_vrf1,
1173 dst=dhcp_solicit_dst) /
1174 UDP(sport=DHCP6_SERVER_PORT,
1175 dport=DHCP6_CLIENT_PORT) /
1178 self.pg4.add_stream(p_request_vrf1)
1179 self.pg_enable_capture(self.pg_interfaces)
1182 rx = self.pg1.get_capture(1)
1185 # Test we drop DHCP packets from addresses that are not configured as
1188 p2 = (Ether(dst=self.pg1.local_mac, src=self.pg1.remote_hosts[1].mac) /
1189 IPv6(dst=self.pg1.local_ip6, src="3001::1") /
1190 UDP(sport=DHCP6_SERVER_PORT, dport=DHCP6_SERVER_PORT) /
1191 DHCP6_RelayReply(peeraddr=dhcp_solicit_src_vrf1) /
1192 DHCP6OptIfaceId(optlen=4, ifaceid='\x00\x00\x00\x05') /
1193 DHCP6OptRelayMsg(optlen=0) /
1194 DHCP6_Advertise(trid=1) /
1195 DHCP6OptStatusCode(statuscode=0))
1196 self.send_and_assert_no_replies(self.pg1, p2,
1197 "DHCP6 not from server")
1200 # Remove the second DHCP server
1202 Proxy12.remove_vpp_config()
1205 # Test we can still relay with the first
1207 self.pg4.add_stream(p_solicit_vrf1)
1208 self.pg_enable_capture(self.pg_interfaces)
1211 rx = self.pg1.get_capture(1)
1213 self.verify_dhcp6_solicit(rx[0], self.pg1,
1214 dhcp_solicit_src_vrf1,
1215 self.pg4.remote_mac)
1220 Proxy.remove_vpp_config()
1221 Proxy1.remove_vpp_config()
1222 Proxy2.remove_vpp_config()
1224 self.pg3.unconfig_ip6()
1225 self.pg4.unconfig_ip6()
1226 self.pg5.unconfig_ip6()
1228 def test_dhcp_client(self):
1231 vdscp = VppEnum.vl_api_ip_dscp_t
1232 hostname = 'universal-dp'
1234 self.pg_enable_capture(self.pg_interfaces)
1237 # Configure DHCP client on PG3 and capture the discover sent
1239 Client = VppDHCPClient(self, self.pg3.sw_if_index, hostname)
1240 Client.add_vpp_config()
1241 self.assertTrue(Client.query_vpp_config())
1243 rx = self.pg3.get_capture(1)
1245 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1248 # Send back on offer, expect the request
1250 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1251 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1252 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1254 yiaddr=self.pg3.local_ip4,
1255 chaddr=mac_pton(self.pg3.local_mac)) /
1256 DHCP(options=[('message-type', 'offer'),
1257 ('server_id', self.pg3.remote_ip4),
1260 self.pg3.add_stream(p_offer)
1261 self.pg_enable_capture(self.pg_interfaces)
1264 rx = self.pg3.get_capture(1)
1265 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1269 # Send an acknowledgment
1271 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1272 IP(src=self.pg3.remote_ip4, dst="255.255.255.255") /
1273 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1274 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1275 chaddr=mac_pton(self.pg3.local_mac)) /
1276 DHCP(options=[('message-type', 'ack'),
1277 ('subnet_mask', "255.255.255.0"),
1278 ('router', self.pg3.remote_ip4),
1279 ('server_id', self.pg3.remote_ip4),
1280 ('lease_time', 43200),
1283 self.pg3.add_stream(p_ack)
1284 self.pg_enable_capture(self.pg_interfaces)
1288 # We'll get an ARP request for the router address
1290 rx = self.pg3.get_capture(1)
1292 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1293 self.pg_enable_capture(self.pg_interfaces)
1296 # At the end of this procedure there should be a connected route
1299 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1300 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1303 # remove the DHCP config
1305 Client.remove_vpp_config()
1308 # and now the route should be gone
1310 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1311 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1314 # Start the procedure again. this time have VPP send the client-ID
1315 # and set the DSCP value
1317 self.pg3.admin_down()
1320 Client.set_client(self.pg3.sw_if_index, hostname,
1321 id=self.pg3.local_mac,
1322 dscp=vdscp.IP_API_DSCP_EF)
1323 Client.add_vpp_config()
1325 rx = self.pg3.get_capture(1)
1327 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1329 dscp=vdscp.IP_API_DSCP_EF)
1331 # TODO: VPP DHCP client should not accept DHCP OFFER message with
1332 # the XID (Transaction ID) not matching the XID of the most recent
1333 # DHCP DISCOVERY message.
1334 # Such DHCP OFFER message must be silently discarded - RFC2131.
1335 # Reported in Jira ticket: VPP-99
1336 self.pg3.add_stream(p_offer)
1337 self.pg_enable_capture(self.pg_interfaces)
1340 rx = self.pg3.get_capture(1)
1341 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1343 dscp=vdscp.IP_API_DSCP_EF)
1346 # unicast the ack to the offered address
1348 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1349 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1350 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1351 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1352 chaddr=mac_pton(self.pg3.local_mac)) /
1353 DHCP(options=[('message-type', 'ack'),
1354 ('subnet_mask', "255.255.255.0"),
1355 ('router', self.pg3.remote_ip4),
1356 ('server_id', self.pg3.remote_ip4),
1357 ('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, 32))
1377 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1380 # remove the DHCP config
1382 Client.remove_vpp_config()
1384 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1385 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1388 # Rince and repeat, this time with VPP configured not to set
1389 # the braodcast flag in the discover and request messages,
1390 # and for the server to unicast the responses.
1392 # Configure DHCP client on PG3 and capture the discover sent
1395 self.pg3.sw_if_index,
1397 set_broadcast_flag=False)
1398 Client.add_vpp_config()
1400 rx = self.pg3.get_capture(1)
1402 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1406 # Send back on offer, unicasted to the offered address.
1407 # Expect the request.
1409 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1410 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1411 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1412 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1413 chaddr=mac_pton(self.pg3.local_mac)) /
1414 DHCP(options=[('message-type', 'offer'),
1415 ('server_id', self.pg3.remote_ip4),
1418 self.pg3.add_stream(p_offer)
1419 self.pg_enable_capture(self.pg_interfaces)
1422 rx = self.pg3.get_capture(1)
1423 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1428 # Send an acknowledgment, the lease renewal time is 2 seconds
1429 # so we should expect the renew straight after
1431 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1432 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1433 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1434 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1435 chaddr=mac_pton(self.pg3.local_mac)) /
1436 DHCP(options=[('message-type', 'ack'),
1437 ('subnet_mask', "255.255.255.0"),
1438 ('router', self.pg3.remote_ip4),
1439 ('server_id', self.pg3.remote_ip4),
1440 ('lease_time', 43200),
1441 ('renewal_time', 2),
1444 self.pg3.add_stream(p_ack)
1445 self.pg_enable_capture(self.pg_interfaces)
1449 # We'll get an ARP request for the router address
1451 rx = self.pg3.get_capture(1)
1453 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1454 self.pg_enable_capture(self.pg_interfaces)
1457 # At the end of this procedure there should be a connected route
1460 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1461 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1464 # read the DHCP client details from a dump
1466 clients = self.vapi.dhcp_client_dump()
1468 self.assertEqual(clients[0].client.sw_if_index,
1469 self.pg3.sw_if_index)
1470 self.assertEqual(clients[0].lease.sw_if_index,
1471 self.pg3.sw_if_index)
1472 self.assertEqual(clients[0].client.hostname, hostname)
1473 self.assertEqual(clients[0].lease.hostname, hostname)
1474 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1475 self.assertEqual(clients[0].lease.state, 2)
1476 self.assertEqual(clients[0].lease.mask_width, 24)
1477 self.assertEqual(str(clients[0].lease.router_address),
1478 self.pg3.remote_ip4)
1479 self.assertEqual(str(clients[0].lease.host_address),
1483 # wait for the unicasted renewal
1484 # the first attempt will be an ARP packet, since we have not yet
1485 # responded to VPP's request
1487 self.logger.info(self.vapi.cli("sh dhcp client intfc pg3 verbose"))
1488 rx = self.pg3.get_capture(1, timeout=10)
1490 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1492 # respond to the arp
1493 p_arp = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1495 hwdst=self.pg3.local_mac,
1496 hwsrc=self.pg3.remote_mac,
1497 pdst=self.pg3.local_ip4,
1498 psrc=self.pg3.remote_ip4))
1499 self.pg3.add_stream(p_arp)
1500 self.pg_enable_capture(self.pg_interfaces)
1503 # the next packet is the unicasted renewal
1504 rx = self.pg3.get_capture(1, timeout=10)
1505 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1510 # send an ACK with different data from the original offer *
1511 self.pg3.generate_remote_hosts(4)
1512 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1513 IP(src=self.pg3.remote_ip4, dst=self.pg3.local_ip4) /
1514 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1515 BOOTP(op=1, yiaddr=self.pg3.remote_hosts[3].ip4,
1516 chaddr=mac_pton(self.pg3.local_mac)) /
1517 DHCP(options=[('message-type', 'ack'),
1518 ('subnet_mask', "255.255.255.0"),
1519 ('router', self.pg3.remote_hosts[1].ip4),
1520 ('server_id', self.pg3.remote_hosts[2].ip4),
1521 ('lease_time', 43200),
1522 ('renewal_time', 2),
1525 self.pg3.add_stream(p_ack)
1526 self.pg_enable_capture(self.pg_interfaces)
1530 # read the DHCP client details from a dump
1532 clients = self.vapi.dhcp_client_dump()
1534 self.assertEqual(clients[0].client.sw_if_index,
1535 self.pg3.sw_if_index)
1536 self.assertEqual(clients[0].lease.sw_if_index,
1537 self.pg3.sw_if_index)
1538 self.assertEqual(clients[0].client.hostname, hostname)
1539 self.assertEqual(clients[0].lease.hostname, hostname)
1540 # 0 = DISCOVER, 1 = REQUEST, 2 = BOUND
1541 self.assertEqual(clients[0].lease.state, 2)
1542 self.assertEqual(clients[0].lease.mask_width, 24)
1543 self.assertEqual(str(clients[0].lease.router_address),
1544 self.pg3.remote_hosts[1].ip4)
1545 self.assertEqual(str(clients[0].lease.host_address),
1546 self.pg3.remote_hosts[3].ip4)
1549 # remove the DHCP config
1551 Client.remove_vpp_config()
1554 # and now the route should be gone
1556 self.assertFalse(find_route(self, self.pg3.local_ip4, 32))
1557 self.assertFalse(find_route(self, self.pg3.local_ip4, 24))
1560 # Start the procedure again. Use requested lease time option.
1561 # this time wait for the lease to expire and the client to
1565 self.pg3.admin_down()
1568 self.pg_enable_capture(self.pg_interfaces)
1569 Client.set_client(self.pg3.sw_if_index, hostname)
1570 Client.add_vpp_config()
1572 rx = self.pg3.get_capture(1)
1574 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname)
1577 # Send back on offer with requested lease time, expect the request
1580 p_offer = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1581 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1582 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1584 yiaddr=self.pg3.local_ip4,
1585 chaddr=mac_pton(self.pg3.local_mac)) /
1586 DHCP(options=[('message-type', 'offer'),
1587 ('server_id', self.pg3.remote_ip4),
1588 ('lease_time', lease_time),
1591 self.pg3.add_stream(p_offer)
1592 self.pg_enable_capture(self.pg_interfaces)
1595 rx = self.pg3.get_capture(1)
1596 self.verify_orig_dhcp_request(rx[0], self.pg3, hostname,
1600 # Send an acknowledgment
1602 p_ack = (Ether(dst=self.pg3.local_mac, src=self.pg3.remote_mac) /
1603 IP(src=self.pg3.remote_ip4, dst='255.255.255.255') /
1604 UDP(sport=DHCP4_SERVER_PORT, dport=DHCP4_CLIENT_PORT) /
1605 BOOTP(op=1, yiaddr=self.pg3.local_ip4,
1606 chaddr=mac_pton(self.pg3.local_mac)) /
1607 DHCP(options=[('message-type', 'ack'),
1608 ('subnet_mask', '255.255.255.0'),
1609 ('router', self.pg3.remote_ip4),
1610 ('server_id', self.pg3.remote_ip4),
1611 ('lease_time', lease_time),
1614 self.pg3.add_stream(p_ack)
1615 self.pg_enable_capture(self.pg_interfaces)
1619 # We'll get an ARP request for the router address
1621 rx = self.pg3.get_capture(1)
1623 self.assertEqual(rx[0][ARP].pdst, self.pg3.remote_ip4)
1626 # At the end of this procedure there should be a connected route
1629 self.assertTrue(find_route(self, self.pg3.local_ip4, 32))
1630 self.assertTrue(find_route(self, self.pg3.local_ip4, 24))
1633 # the route should be gone after the lease expires
1635 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 32))
1636 self.assertTrue(self.wait_for_no_route(self.pg3.local_ip4, 24))
1639 # remove the DHCP config
1641 Client.remove_vpp_config()
1643 def test_dhcp_client_vlan(self):
1644 """ DHCP Client w/ VLAN"""
1646 vdscp = VppEnum.vl_api_ip_dscp_t
1647 vqos = VppEnum.vl_api_qos_source_t
1648 hostname = 'universal-dp'
1650 self.pg_enable_capture(self.pg_interfaces)
1652 vlan_100 = VppDot1QSubint(self, self.pg3, 100)
1655 output = [scapy.compat.chb(4)] * 256
1656 os = b''.join(output)
1657 rows = [{'outputs': os},
1662 qem1 = VppQosEgressMap(self, 1, rows).add_vpp_config()
1663 qm1 = VppQosMark(self, vlan_100, qem1,
1664 vqos.QOS_API_SOURCE_VLAN).add_vpp_config()
1667 # Configure DHCP client on PG3 and capture the discover sent
1669 Client = VppDHCPClient(
1671 vlan_100.sw_if_index,
1673 dscp=vdscp.IP_API_DSCP_EF)
1674 Client.add_vpp_config()
1676 rx = self.pg3.get_capture(1)
1678 self.assertEqual(rx[0][Dot1Q].vlan, 100)
1679 self.assertEqual(rx[0][Dot1Q].prio, 2)
1681 self.verify_orig_dhcp_discover(rx[0], self.pg3, hostname,
1682 dscp=vdscp.IP_API_DSCP_EF)
1685 if __name__ == '__main__':
1686 unittest.main(testRunner=VppTestRunner)