1.. zephyr:code-sample:: dhcpv4-client
2 :name: DHCPv4 client
3 :relevant-api: dhcpv4 net_mgmt
4
5 Start a DHCPv4 client to obtain an IPv4 address from a DHCPv4 server.
6
7Overview
8********
9
10This application starts a DHCPv4 client, gets an IPv4 address from the
11DHCPv4 server, and prints address, lease time, netmask and router
12information to a serial console.
13
14Requirements
15************
16
17- :ref:`networking_with_host`
18
19Building and Running
20********************
21
22Running DHCPv4 client in Linux Host
23===================================
24
25These are instructions for how to use this sample application using
26QEMU on a Linux host to negotiate IP address from DHCPv4 server (kea) running
27on Linux host.
28
29To use QEMU for testing, follow the :ref:`networking_with_qemu` guide.
30
31Here's a sample server configuration file '/etc/kea/kea-dhcp4.conf'
32used to configure the DHCPv4 server:
33
34.. code-block:: console
35
36 {
37 "Dhcp4": {
38 "interfaces-config": {
39 "interfaces": [ "tap0" ],
40 "dhcp-socket-type": "raw"
41 },
42
43 "valid-lifetime": 7200,
44
45 "subnet4": [
46 {
47 "id": 1,
48 "subnet": "192.0.2.0/24",
49 "pools": [ { "pool": "192.0.2.10 - 192.0.2.100" } ],
50 "option-data": [
51 {
52 "name": "routers",
53 "data": "192.0.2.2"
54 },
55 {
56 "name": "domain-name-servers",
57 "data": "8.8.8.8"
58 }
59 ]
60 }
61 ]
62 }
63 }
64
65Use another terminal window to start up a DHCPv4 server on the Linux host,
66using this conf file:
67
68.. code-block:: console
69
70 $ sudo kea-dhcp4 -c /etc/kea/kea-dhcp4.conf
71
72Run Zephyr samples/net/dhcpv4_client application in QEMU:
73
74.. zephyr-app-commands::
75 :zephyr-app: samples/net/dhcpv4_client
76 :host-os: unix
77 :board: qemu_x86
78 :goals: run
79 :compact:
80
81Once DHCPv4 client address negotiation completed with server, details
82are shown like this:
83
84.. code-block:: console
85
86 [00:00:00.000,000] <inf> net_dhcpv4_client_sample: Run dhcpv4 client
87 [00:00:00.000,000] <inf> net_dhcpv4_client_sample: Start on slip: index=1
88 [00:00:07.080,000] <inf> net_dhcpv4: Received: 192.0.2.10
89 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Address[1]: 192.0.2.10
90 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Subnet[1]: 255.255.255.0
91 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Router[1]: 192.0.2.2
92 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Lease time[1]: 7200 seconds
93
94To verify the Zephyr application client is running and has received
95an ip address by typing:
96
97.. code-block:: console
98
99 $ ping -I tap0 192.0.2.10
100
101
102FRDM_K64F
103=========
104
105These are instructions for how to use this sample application running on
106:zephyr:board:`frdm_k64f` board to negotiate IP address from DHCPv4 server (kea) running
107on Linux host.
108
109Connect ethernet cable from :zephyr:board:`Freedom-K64F board <frdm_k64f>` to Linux host
110machine and check for new interfaces:
111
112.. code-block:: console
113
114 $ ifconfig
115
116Add ip address and routing information to interface:
117
118.. code-block:: console
119
120 $ sudo ip addr add 192.0.2.2 dev eth1
121 $ sudo ip route add 192.0.2.0/24 dev eth1
122
123Here's a sample server configuration file '/etc/kea/kea-dhcp4.conf'
124used to configure the DHCPv4 server:
125
126.. code-block:: console
127
128 {
129 "Dhcp4": {
130 "interfaces-config": {
131 "interfaces": [ "eth1" ],
132 "dhcp-socket-type": "raw"
133 },
134
135 "valid-lifetime": 7200,
136
137 "subnet4": [
138 {
139 "id": 1,
140 "subnet": "192.0.2.0/24",
141 "pools": [ { "pool": "192.0.2.10 - 192.0.2.100" } ],
142 "option-data": [
143 {
144 "name": "routers",
145 "data": "192.0.2.2"
146 },
147 {
148 "name": "domain-name-servers",
149 "data": "8.8.8.8"
150 }
151 ]
152 }
153 ]
154 }
155 }
156
157Use another terminal window to start up a DHCPv4 server on the Linux host,
158using this conf file:
159
160.. code-block:: console
161
162 $ sudo kea-dhcp4 -c /etc/kea/kea-dhcp4.conf
163
164Build Zephyr samples/net/dhcpv4_client application:
165
166.. zephyr-app-commands::
167 :zephyr-app: samples/net/dhcpv4_client
168 :host-os: unix
169 :board: frdm_k64f
170 :goals: build flash
171 :compact:
172
173Once DHCPv4 client address negotiation completed with server, details
174are shown like this:
175
176.. code-block:: console
177
178 $ sudo screen /dev/ttyACM0 115200
179 [00:00:00.000,000] <inf> net_dhcpv4_client_sample: Run dhcpv4 client
180 [00:00:00.000,000] <inf> net_dhcpv4_client_sample: Start on ethernet: index=1
181 [00:00:07.080,000] <inf> net_dhcpv4: Received: 192.0.2.10
182 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Address[1]: 192.0.2.10
183 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Subnet[1]: 255.255.255.0
184 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Router[1]: 192.0.2.2
185 [00:00:07.080,000] <inf> net_dhcpv4_client_sample: Lease time[1]: 7200 seconds
186
187To verify the Zephyr application client is running and has received
188an ip address by typing:
189
190.. code-block:: console
191
192 $ ping -I eth1 192.0.2.10
193
194
195Arm FVP
196========
197
198* :ref:`fvp_baser_aemv8r`
199* :ref:`fvp_base_revc_2xaemv8a`
200
201This sample application running on Arm FVP board can negotiate IP
202address from DHCPv4 server running on Arm FVP, so there is no extra
203configuration that needed to do. It can be built and run directly.
204
205Build Zephyr samples/net/dhcpv4_client application:
206
207.. zephyr-app-commands::
208 :zephyr-app: samples/net/dhcpv4_client
209 :host-os: unix
210 :board: fvp_baser_aemv8r
211 :goals: build run
212 :compact:
213
214Once DHCPv4 client address negotiation completed with server, details
215are shown like this:
216
217.. code-block:: console
218
219 uart:~$
220 [00:00:00.060,000] <inf> phy_mii: PHY (0) ID 16F840
221
222 [00:00:00.170,000] <inf> phy_mii: PHY (0) Link speed 10 Mb, half duplex
223
224 [00:00:00.170,000] <inf> eth_smsc91x: MAC 00:02:f7:ef:37:16
225 *** Booting Zephyr OS build zephyr-v3.2.0-4300-g3e6505dba29e ***
226 [00:00:00.170,000] <inf> net_dhcpv4_client_sample: Run dhcpv4 client
227 [00:00:00.180,000] <inf> net_dhcpv4_client_sample: Start on ethernet@9a000000: index=1
228 [00:00:07.180,000] <inf> net_dhcpv4: Received: 172.20.51.1
229 [00:00:07.180,000] <inf> net_dhcpv4_client_sample: Address[1]: 172.20.51.1
230 [00:00:07.180,000] <inf> net_dhcpv4_client_sample: Subnet[1]: 255.255.255.0
231 [00:00:07.180,000] <inf> net_dhcpv4_client_sample: Router[1]: 172.20.51.254
232 [00:00:07.180,000] <inf> net_dhcpv4_client_sample: Lease time[1]: 86400 seconds
233