From patchwork Tue Sep 26 09:41:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 726407 Delivered-To: patch@linaro.org Received: by 2002:adf:ea87:0:b0:31d:da82:a3b4 with SMTP id s7csp2834336wrm; Tue, 26 Sep 2023 02:44:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrGHf6waR3rnRrjqPldXp2qunHY2VYLlpBJZf4xRJkC9thTsaN4cnpiuOSBItm4Xr3ljbB X-Received: by 2002:a19:2d56:0:b0:500:9ab8:b790 with SMTP id t22-20020a192d56000000b005009ab8b790mr6334319lft.60.1695721483755; Tue, 26 Sep 2023 02:44:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695721483; cv=none; d=google.com; s=arc-20160816; b=B0M5vZ9LmuYfi+vpVDk0G8Vmmi5vKWuH5mjh719zdiH7OVkxEq0i9fVnVswA2WYcpr TBsVKwoN3q/c7nwwybihQK4MyEeNeXAdwBp7Sa5iCPYY3haCWXud2R09krdEilLOYyeU tiI9ktmhhVLWnIrQm+/24CWTvMP8gs/scS3/cEB8y48+A1/n7p2moJj0O2eEnLGroKFk najG0siwvUeoewtmePrNINGBZh8AEfHD6t8ik0iX/E5ZwecNelE+5clsy/9QsKou0feV GwFy+m7ot/XbgH0SV8huJR1opHi1zFyXL3oFVgG6o58IivQBCbBBEk4ZzbAd7p0NvNvv gx2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2TpMV1fzgEaRtklvSF1wLKwOvwOJoWTA7xIFAq1N3os=; fh=hYoOR0yg+Fo7S26X8ur5L+RaTmkyxWlAVaBypEC8A0g=; b=cO5H6OZCIfDP9rckwmG+3ClEQDrw7x6qGJbC2djeCl4p/kpSj0cfiXo75nBfDRRzXb +IEeAn3awe5k2rfLdzKL5xQgGdAmuscx1Z6y6cZ5Bsa+E9v1UFQEV3SPGC8giVlBzkV3 IYkU1QqkDyAILz7UsTewKlmFneBrfCZXCuIgrJb/EUajQxga5baE5QgF4QkwHWMF9BqS 523PpU4Qy8Y399jH1NAOmwbAwxm/gwx/AZGUbxl2wes5OjgMUa2QgS++1qYRa2PcSASI UiFnDZmBqBMZfnjY76F3ncjeBwq5MunhZC5bcFfBZl8ZVcP9WZ6GazQubMrHqXrHHQ7D WSjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WLVZxTR4; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id d9-20020adffd89000000b0032327cf4234si2146427wrr.511.2023.09.26.02.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 02:44:43 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WLVZxTR4; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B6C6186DB3; Tue, 26 Sep 2023 11:43:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="WLVZxTR4"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C608D86DB3; Tue, 26 Sep 2023 11:43:56 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BB39086AF7 for ; Tue, 26 Sep 2023 11:43:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=maxim.uvarov@linaro.org Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2c038a1e2e6so130078891fa.2 for ; Tue, 26 Sep 2023 02:43:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1695721434; x=1696326234; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2TpMV1fzgEaRtklvSF1wLKwOvwOJoWTA7xIFAq1N3os=; b=WLVZxTR4RR2rlhbWUdqC9/i+Ld7/bZ4Icoqu61XV4zko88cbZ/ur8Xt6b26PGSWn9X 2VCOwlO+zOGOzzkL/kQfox5PYCrv/Q4Jq+zEkSgPDE1iRT5cJQz7FLYrr044+TjFtws3 FwNzPHbMuXoYiLbrBP1tiimOGARvFdg5BcQrB/nyOipKZWX1b2HR20Prkl9nzNeE9gPf /nT7SNcE2fk9pD3fP/PbmTPOSi/4WkABQQUu4vamvkIyF8G254jqlelpx03Azspihj9G e7Wh4Qd/bVx7VVZH3F1bJqUBCmCUzWjeOi5nn4BBbtPRxmgu4WAXpkmTqQLZ7nrKKmLL I9fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695721434; x=1696326234; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2TpMV1fzgEaRtklvSF1wLKwOvwOJoWTA7xIFAq1N3os=; b=D4k/fzsV1MhGimcgmAyXsOccRSrRk34nRRvOhRLlWyBnFTzEcjzfJxVwjQoJ+19LmM OshMDPU0s5bwFwxB/8Tv6h8w5Jme0jGZJ9g01Or17ZaB5P0EizBVEmgEPD2rd/iKJi/A 4mK1C48N1Hr8LEAjPOsPPfWw+I1l9GjkWljBzHM3PeZO+z8S6oGcyg760pa4jk1rDar/ 5BKCjS73e9Gzga+6L1ACJAbgbGOyzxGT59G981Iasx/5QFfZ6HQ4RTeGs+VviKw3PmAO AwBWY0WvXaj4y78Qek0N1i5/l+717JxFqWfhV1+G3BYwT7lLwifa30VRz1zHiY50GfUt Uwzg== X-Gm-Message-State: AOJu0YwCBiPLE4Q0dr3nE1CFAz+OA1M91dn0tPR1E0H+CskVfFDED3SQ MEywPZ04jzsxv85zq0ZXvTKDCpSxqs34H0ggV4vqHw== X-Received: by 2002:a19:4f16:0:b0:501:b97a:9f50 with SMTP id d22-20020a194f16000000b00501b97a9f50mr6647250lfb.65.1695721433938; Tue, 26 Sep 2023 02:43:53 -0700 (PDT) Received: from localhost.localdomain ([45.82.14.220]) by smtp.gmail.com with ESMTPSA id x6-20020ac25dc6000000b004fe09e6d1e7sm2131131lfq.110.2023.09.26.02.43.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 02:43:53 -0700 (PDT) From: Maxim Uvarov 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 Subject: [PATCHv10 06/15] net/lwip: implement dhcp cmd Date: Tue, 26 Sep 2023 15:41:15 +0600 Message-Id: <20230926094124.7024-7-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230926094124.7024-1-maxim.uvarov@linaro.org> References: <20230926094124.7024-1-maxim.uvarov@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean 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 Reviewed-by: Simon Glass --- 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. + */ + +#include +#include +#include + +#include +#include +#include "lwip/timeouts.h" + +#include +#include + +#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; +}