From: Maxim Uvarov <maxim.uvarov@linaro.org>
To: u-boot@lists.denx.de
Cc: pbrobinson@gmail.com, ilias.apalodimas@linaro.org,
joe.hershberger@ni.com, rfried.dev@gmail.com, trini@konsulko.com,
goldsimon@gmx.de, Maxim Uvarov <maxim.uvarov@linaro.org>
Subject: [PATCHv10 06/15] net/lwip: implement dhcp cmd
Date: Tue, 26 Sep 2023 15:41:15 +0600 [thread overview]
Message-ID: <20230926094124.7024-7-maxim.uvarov@linaro.org> (raw)
In-Reply-To: <20230926094124.7024-1-maxim.uvarov@linaro.org>
U-Boot recently got support for an alternative network stack using LWIP.
Replace dhcp command with the LWIP variant while keeping the output and
error messages identical.
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
---
include/net/lwip.h | 12 +++++
net/lwip/Makefile | 1 +
net/lwip/apps/dhcp/lwip-dhcp.c | 85 ++++++++++++++++++++++++++++++++++
3 files changed, 98 insertions(+)
create mode 100644 net/lwip/apps/dhcp/lwip-dhcp.c
diff --git a/include/net/lwip.h b/include/net/lwip.h
index ab3db1a214..6a8fcef392 100644
--- a/include/net/lwip.h
+++ b/include/net/lwip.h
@@ -17,3 +17,15 @@ int do_lwip_dns(struct cmd_tbl *cmdtp, int flag, int argc,
* Other value < 0, if error
*/
int ulwip_dns(char *name, char *varname);
+
+/**
+ * ulwip_dhcp() - create the DHCP request to obtain IP address.
+ *
+ * This function creates the DHCP request to obtain IP address. If DHCP server
+ * returns file name, this file will be downloaded with tftp. After this
+ * function you need to invoke the polling loop to process network communication.
+ *
+ * Returns: 0 if success
+ * Other value < 0, if error
+*/
+int ulwip_dhcp(void);
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 5d8d5527c6..a3a33b7f71 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -63,4 +63,5 @@ obj-$(CONFIG_NET) += lwip-external/src/netif/ethernet.o
obj-$(CONFIG_NET) += port/if.o
obj-$(CONFIG_NET) += port/sys-arch.o
+obj-y += apps/dhcp/lwip-dhcp.o
obj-y += apps/dns/lwip-dns.o
diff --git a/net/lwip/apps/dhcp/lwip-dhcp.c b/net/lwip/apps/dhcp/lwip-dhcp.c
new file mode 100644
index 0000000000..f0b0e26f6e
--- /dev/null
+++ b/net/lwip/apps/dhcp/lwip-dhcp.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * (C) Copyright 2023 Linaro Ltd. <maxim.uvarov@linaro.org>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <console.h>
+
+#include <lwip/dhcp.h>
+#include <lwip/prot/dhcp.h>
+#include "lwip/timeouts.h"
+
+#include <net/eth.h>
+#include <net/ulwip.h>
+
+#define DHCP_TMO_TIME 500 /* poll for DHCP state change */
+#define DHCP_TMO_NUM 10 /* number of tries */
+
+typedef struct dhcp_priv {
+ int num_tries;
+ struct netif *netif;
+} dhcp_priv;
+
+static void dhcp_tmo(void *arg)
+{
+ struct dhcp_priv *dpriv = (struct dhcp_priv *)arg;
+ struct netif *netif = dpriv->netif;
+ struct dhcp *dhcp;
+
+ dhcp = netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
+ if (!dhcp)
+ return;
+
+ if (dhcp->state == DHCP_STATE_BOUND) {
+ int err = 0;
+
+ err -= env_set("bootfile", dhcp->boot_file_name);
+ err -= env_set("ipaddr", ip4addr_ntoa(&dhcp->offered_ip_addr));
+ err -= env_set("netmask", ip4addr_ntoa(&dhcp->offered_sn_mask));
+ err -= env_set("serverip", ip4addr_ntoa(&dhcp->server_ip_addr));
+ if (err)
+ log_err("error update envs\n");
+ log_info("DHCP client bound to address %s\n", ip4addr_ntoa(&dhcp->offered_ip_addr));
+ free(dpriv);
+ ulwip_exit(err);
+ return;
+ }
+
+ dpriv->num_tries--;
+ if (dpriv->num_tries < 0) {
+ log_err("DHCP client timeout\n");
+ free(dpriv);
+ ulwip_exit(-1);
+ return;
+ }
+
+ sys_timeout(DHCP_TMO_TIME, dhcp_tmo, dpriv);
+}
+
+int ulwip_dhcp(void)
+{
+ struct netif *netif;
+ int eth_idx;
+ struct dhcp_priv *dpriv;
+
+ dpriv = malloc(sizeof(struct dhcp_priv));
+ if (!dpriv)
+ return -EPERM;
+
+ eth_idx = eth_get_dev_index();
+ if (eth_idx < 0)
+ return -EPERM;
+
+ netif = netif_get_by_index(eth_idx + 1);
+ if (!netif)
+ return -ENOENT;
+
+ dpriv->num_tries = DHCP_TMO_NUM;
+ dpriv->netif = netif;
+ sys_timeout(DHCP_TMO_TIME, dhcp_tmo, dpriv);
+
+ return dhcp_start(netif) ? 0 : -EPERM;
+}
--
2.30.2
next prev parent reply other threads:[~2023-09-26 9:44 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-26 9:41 [PATCHv10 00/15] net/lwip: add lwip library for the network stack Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 01/15] submodule: add lwIP as git submodule Maxim Uvarov
2023-09-26 11:37 ` Simon Glass
2023-09-26 13:41 ` Tom Rini
2023-09-26 14:16 ` Simon Glass
2023-09-26 14:19 ` Tom Rini
2023-10-02 6:34 ` Maxim Uvarov
2023-10-02 11:23 ` Maxim Uvarov
2023-10-02 14:46 ` Simon Glass
2023-10-04 7:52 ` Maxim Uvarov
2023-10-04 16:02 ` Tom Rini
2023-10-04 16:06 ` Simon Glass
2023-09-26 9:41 ` [PATCHv10 02/15] Makefile: init submodules Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 03/15] net/lwip: add doc/develop/net_lwip.rst Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 04/15] net/lwip: integrate lwIP library Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 05/15] net/lwip: implement dns cmd Maxim Uvarov
2023-09-26 9:41 ` Maxim Uvarov [this message]
2023-10-02 1:17 ` [PATCHv10 06/15] net/lwip: implement dhcp cmd Simon Glass
2023-09-26 9:41 ` [PATCHv10 07/15] net/lwip: implement tftp cmd Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 08/15] net/lwip: implement wget cmd Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
2023-09-26 9:41 ` [PATCHv10 09/15] net/lwip: implement ping cmd Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 10/15] net/lwip: add lwIP configuration Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
2023-09-26 9:41 ` [PATCHv10 11/15] net/lwip: implement lwIP port to U-Boot Maxim Uvarov
2023-09-26 9:41 ` [PATCHv10 12/15] net/lwip: update .gitignore with lwIP Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
2023-09-26 9:41 ` [PATCHv10 13/15] net/lwip: connection between cmd and lwip apps Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
2023-09-26 9:41 ` [PATCHv10 14/15] net/lwip: replace original net commands with lwip Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
2023-10-03 17:58 ` Sean Edmond
2023-10-03 21:58 ` Peter Robinson
2023-10-03 23:44 ` Sean Edmond
2023-10-04 2:11 ` Simon Glass
2023-10-04 8:29 ` Maxim Uvarov
2023-10-04 20:14 ` Simon Goldschmidt
2023-09-26 9:41 ` [PATCHv10 15/15] net/lwip: split net.h to net.h, arp.h and eth.h Maxim Uvarov
2023-10-02 1:17 ` Simon Glass
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230926094124.7024-7-maxim.uvarov@linaro.org \
--to=maxim.uvarov@linaro.org \
--cc=goldsimon@gmx.de \
--cc=ilias.apalodimas@linaro.org \
--cc=joe.hershberger@ni.com \
--cc=pbrobinson@gmail.com \
--cc=rfried.dev@gmail.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).