DHCP Relay Configuration
Since DHCP uses broadcasting to send request messages during the IP address dynamic allocation process, it is suitable only for cases where the DHCP client and server are within the same subnet. When the DHCP server and client are not in the same subnet, the DHCP relay mechanism needs to be employed.
Explanation of Principles
Section titled “Explanation of Principles”In the context of DHCP relay, the message exchange is depicted as shown in the diagram:

- The Client Sends DHCP Discovery Broadcast:
When a DHCP client is located within a subnet and needs to obtain an IP address and other configuration information, it broadcasts a DHCP Discovery message (DHCP Discover) to the broadcast address of the local network.
- Relay Agent Forwards DHCP Message:
The DHCP relay agent forwards the received DHCP Discovery message (along with any other relevant DHCP messages) to the DHCP server, encapsulating the original broadcast message.
- DHCP Server Response:
Upon receiving the forwarded DHCP Discovery message in a different subnet, the DHCP server assigns an IP address to the client and generates a DHCP Offer broadcast message (DHCP Offer) back to the relay agent.
- Relay Agent Forwards DHCP Offer:
The DHCP relay agent forwards the DHCP Offer message back to the original subnet, encapsulating the IP address and configuration information provided by the DHCP server.
- Client Requests and Acknowledges:
The DHCP client, upon receiving the DHCP Offer message, selects a server and sends a DHCP Request message (DHCP Request) to request the allocated IP address. This request message is also forwarded by the relay agent to the DHCP server.
- Server Acknowledgment and Response:
Upon receiving the DHCP Request message, the DHCP server sends a DHCP Acknowledgment message (DHCP Acknowledgment) back to the relay agent. Similarly, the relay agent forwards the acknowledgment message to the subnet where the client is located.
Through this process, DHCP relay allows DHCP clients and servers located in different subnets to communicate without requiring a DHCP server to be set up on each subnet. The relay agent acts as a bridge, forwarding DHCP messages between different subnets, enabling DHCP communication to span physical network boundaries.
Adding Option82
Section titled “Adding Option82”By default, when a device receives a request message from a DHCP client and re-encapsulates it, a field called “option82” is appended to the message to record the client’s location information. The “Relay agent IP address” field is the IP address of the VLAN SVI interface where the client is located. DHCP servers that support parsing the option82 field can use the information carried within it to determine which IP address range to allocate to the client. If the DHCP server does not support parsing the option82 field, the determination can be made based on the “Relay agent IP address.”
However, in scenarios involving cluster networks, access devices are configured as distributed gateways with the same SVI interface address. In such cases, the “dhcp-agent address” command needs to be used to allocate a unique agent IP address to each device. This is done to enable DHCP servers, when sending response messages, to use this unique IP address as the destination address, allowing differentiation among different access devices.
DHCP Relay Configuration
Section titled “DHCP Relay Configuration”| Operation | Command | Description |
|---|---|---|
| Enter the system configuration view | configure terminal | |
| Create a DHCP relay instance and enter the DHCP relay view | dhcp-relay relay-name {v4|v6} | |
| Specify the VLAN to which the DHCP clients belong that require dynamic IP address allocation | down_link interface vlan vlan-id | |
| Specify the interface through which the device is connected to the DHCP server | up_link interface interface-type interface-name | |
| Specify the IP address of the DHCP server | server_ip ip-address | |
| Configure using the loopback interface as the relay agent IP | loopback_interface loopback ID | |
| Enable the option to not encapsulate the option82 field (if needed) | no_option82 | |
| Configure the relay agent IP address (if needed) | dhcp-agent address ip-address |
Configuration Example
Section titled “Configuration Example”Network requirements
- Suppose User 1 and User 3 of a company belong to Department A, User 2 and User 4 belong to Department B, Device A acts as a DHCP relay, and Device B acts as a DHCP server. It is required to assign IP addresses between 10.1.2.2 and 10.1.2.100 to Department A and between 10.1.10.2 and 10.1.10.100 to Department B.
- DNS server, gateway and other information are assigned by DHCP server.
Procedure
- Configuration of Device A
# Create VLAN 100 and configure the IP address
sonic(config)# vlan 100sonic(config)# interface ethernet 1sonic(config-if-1)# switchport access vlan 100sonic(config)# interface ethernet 2sonic(config-if-2)# switchport access vlan 100
sonic(config)# interface vlan 100sonic(config-vlanif-100)# ip address 10.1.2.1/24# Create VLAN 200 and configure the IP address
sonic(config)# vlan 200sonic(config)# interface ethernet 3sonic(config-if-3)# switchport access vlan 200sonic(config)# interface ethernet 4sonic(config-if-4)# switchport access vlan 200sonic(config)# interface vlan 200sonic(config-vlanif-200)# ip address 10.1.10.1/24# Configure the IP address
sonic(config)# interface ethernet 5sonic(config-if-5)# ip address 10.110.1.1/30sonic(config)# interface loopback 0sonic(config-if-lo0)# ip address 10.120.1.1/32Enable DHCP relay function
sonic(config)# dhcp-relay test v4sonic(config-dhcp-relay-test__v4)# down_link interface vlan 100sonic(config-dhcp-relay-test__v4)# down_link interface vlan 200sonic(config-dhcp-relay-test__v4)# up_link interface 5sonic(config-dhcp-relay-test__v4)# server_ip 10.110.1.2sonic(config-dhcp-relay-test__v4)# loopback_interface loopback 0sonic(config-dhcp-relay-test__v4)# exit- Configuration of Device B
sonic(config)# interface ethernet 1sonic(config-if-1)# ip address 10.110.1.2/30sonic(config-if-1)# dhcp select server# Create DHCP address pool
sonic(config)# dhcp pool userAsonic(config-dhcp-pool-userA)# network 10.1.2.1 255.255.255.0sonic(config-dhcp-pool-userA)# address-pool 10.1.2.2 10.1.2.100sonic(config-dhcp-pool-userA)# dns 8.8.8.8sonic(config-dhcp-pool-userA)# exitsonic(config)# dhcp pool userBsonic(config-dhcp-pool-userB)# network 10.1.10.1 255.255.255.0sonic(config-dhcp-pool-userB)# address-pool 10.1.10.2 10.1.10.100sonic(config-dhcp-pool-userB)# dns 8.8.8.8sonic(config-dhcp-pool-userB)# exitVerify configuration
- Check the requested IP address on the PC.
C:\Users\test>ipconfigWindows IP ConfigurationEthernet Adapters Ethernet: Connect to a specific DNS suffix . . . . . . : Local Link IPv6 Address. . . . . . . . . . . : fe80::a495:f96e:6573:c383%12 IPv4 address . . . . . . . . . . . . . . . . : 10.1.10.144 subnet mask . . . . . . . . . . . . . . . . .: 255.255.255.0 Default Gateway. . . . . . . . . . . . . . . : 10.1.10.1- Use the commands show dhcp relay config and show dhcp pool info on Device A and Device B respectively to view the DHCP relay and DHCP server configuration.