From patchwork Thu Sep 14 16:18:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Uvarov X-Patchwork-Id: 722563 Delivered-To: patch@linaro.org Received: by 2002:adf:f0d1:0:b0:31d:da82:a3b4 with SMTP id x17csp485755wro; Thu, 14 Sep 2023 09:21:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEFjsNIATr1/Y/ddCpzUc1r4MdnA6Bwy4eYy//tcVyosXMCbqFTdpsuWLkDzT0HD6Sx4hkh X-Received: by 2002:a1c:4c14:0:b0:3fe:89be:cd3 with SMTP id z20-20020a1c4c14000000b003fe89be0cd3mr5226048wmf.22.1694708504257; Thu, 14 Sep 2023 09:21:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694708504; cv=none; d=google.com; s=arc-20160816; b=EVa2nCzf0RtjI3VmW0/cuN4J3JB1gd1lTKXWZhrLzqUhf1WB4T5IVSilHIoSdGFPN4 C5HqLaKm1Ku9Y6MMoNCCpGW2f9NahhiqlBnRf7yaCFeosy/WTrcGsM2YcyYEceng1QVV nZgrx6sH3CiIx9AOxFUBoTjeFadiAfMvVeBpwW/i2e37mJJQ/enjAlHktVlusBDbxXlU 3qW/gn0Kenm2XY6I9f7jNXI1g2vOZMGoXGfEmxTJcTNwlD5vvfy6mhvYj4jSiSEDn00z hz8ssznAkB/ydIIj6pKiGnHQMEN9UXUIM2vbknFIflmRfqQ6iO0+H5ZZf/Hm0Dbd36up DE7A== 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=Bgxh0nmNV7ezgrPHK4XyUzWpLB3XUz46fliVfKEjy6mUf8NFqP2SMkdtyqeWutzNwO q1LDVSbjqdmy4bSXHq8F589EtC+CI+qLeUgUsB3h53Q/U21i6wMWVuLAc/uXUXaAvaDt h68r9N9OjXKcpEZZRdyYJCae6Z8vEAvFCqgIQVb81Gh4dpmrIL6MpiI3d2ozYRy+F4FW pAL7A4XaM1JGb8uB7h46jzHJVbkVyDT0Y/pXYV35EJD1D/t3PtSDKVu713hTT+QFLKim YSYZ2FAxXX88CPTGW2xAzKaIYaZ1Yg159dvk/UOIWMzMXgmZHI4KFHSGXl9bi7yVs/j8 OC7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ljqCIAvj; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id i21-20020a05600c481500b003fef351f886si2195648wmo.168.2023.09.14.09.21.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 09:21:44 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ljqCIAvj; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 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 F1F5C86A3C; Thu, 14 Sep 2023 18:20:51 +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="ljqCIAvj"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2FC9A86A94; Thu, 14 Sep 2023 18:20:50 +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-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) (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 3A2EA86672 for ; Thu, 14 Sep 2023 18:20:48 +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-x236.google.com with SMTP id 38308e7fff4ca-2bce552508fso17390541fa.1 for ; Thu, 14 Sep 2023 09:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694708447; x=1695313247; 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=ljqCIAvjmfAO877hZFx1wSImW/qMteKp6WVjgaBio8MkYbztLx2WjxFS8FGYHZ0GJf +ce/dQIN8uHBmX28bUgRZkvQtJ9VsU2CaNih+Ayg+NlGEu+EtsiqbvKSH7RBW6Lrc763 Kn/ZUNYYJABo003yH2CBNrE6C9wAnCP5UUXq33kGdDp7e3IluecS3XU+VBDg8Uqk6rhs oJniavE/eBSESgFo0qI8kyZAx+BYtSnLdyJxhqEfR2Pfc1hCFU4+nW8epfjBtT5tlshP f4C1cFfN6OzRdUdEnSeRiHzV+plYw8x50iJbUUZBc3/JqIRVbHBT0zKxfQ1WLU70vcez M+Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694708447; x=1695313247; 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=R4JD/AF8sUp6w6XXW+h5Dp5NP11D5BikiqYBuNq2nwFTMVcLIkRlhqDKsmKW0E6xDL fczia+ET3Y0revJgOtO8DCfUceffBqK3l4G3P64Q7lIvzkLNukGshx2h7KmJ1bSJdNy/ 80BmlMYMyrJlGY+k0fsXD3fJAYpK0llUjuPDouQSXDqHy5Br01XxpYAE4PUjyBoTesGv 612mESI0R4Ev8wxGNjnhwHbWanQIxKQfAde+Ifc81pjHyJDr4i/3vjIjjg99reWbH8X5 lcME+Mjwzc4LDYYb789FeF82FIlSxIuGaMZ1O//gMuhxIF575CVTmXkqJC5bUjoNveWr eiTA== X-Gm-Message-State: AOJu0YyOVAt0gu+X60gKPzvS8TUsBLsbN8XOBR+RysGnn3eB641EZS6S yx6zhPNuqRup480w6nL2aRTFcp6M0MyF6VprKBGlCg== X-Received: by 2002:a2e:9d41:0:b0:2bc:c3ad:f418 with SMTP id y1-20020a2e9d41000000b002bcc3adf418mr5241251ljj.20.1694708447426; Thu, 14 Sep 2023 09:20:47 -0700 (PDT) Received: from localhost.localdomain ([45.82.14.220]) by smtp.gmail.com with ESMTPSA id z15-20020a2e8e8f000000b002ba7ae1f52asm346919ljk.0.2023.09.14.09.20.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 09:20:47 -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: [PATCHv9 05/15] net/lwip: implement dhcp cmd Date: Thu, 14 Sep 2023 22:18:18 +0600 Message-Id: <20230914161828.3662-6-maxim.uvarov@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230914161828.3662-1-maxim.uvarov@linaro.org> References: <20230914161828.3662-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 --- 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; +}