From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 09/21] net: dhcp: Allow to specify network device
Date: Fri, 24 Nov 2017 09:12:25 +0100 [thread overview]
Message-ID: <20171124081237.6830-10-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20171124081237.6830-1-s.hauer@pengutronix.de>
Instead of allowing to DHCP only on the "current" network
device, allow to specify the desired network device. This
is a first step to get rid of the concept of a "current"
network device.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/dhcp.c | 20 +++++++++++++++++---
include/dhcp.h | 4 +++-
include/net.h | 6 +++---
net/dhcp.c | 15 +++++++++------
net/ifup.c | 5 ++++-
net/net.c | 12 +++---------
6 files changed, 39 insertions(+), 23 deletions(-)
diff --git a/commands/dhcp.c b/commands/dhcp.c
index 4f4f5490c5..ce4a78830d 100644
--- a/commands/dhcp.c
+++ b/commands/dhcp.c
@@ -15,12 +15,15 @@
#include <environment.h>
#include <getopt.h>
#include <dhcp.h>
+#include <net.h>
static int do_dhcp(int argc, char *argv[])
{
int ret, opt;
int retries = DHCP_DEFAULT_RETRY;
struct dhcp_req_param dhcp_param;
+ struct eth_device *edev;
+ const char *edevname;
memset(&dhcp_param, 0, sizeof(struct dhcp_req_param));
getenv_uint("global.dhcp.retries", &retries);
@@ -50,12 +53,23 @@ static int do_dhcp(int argc, char *argv[])
}
}
+ if (optind == argc)
+ edevname = "eth0";
+ else
+ edevname = argv[optind];
+
+ edev = eth_get_byname(edevname);
+ if (!edev) {
+ printf("No such network device: %s\n", edevname);
+ return 1;
+ }
+
if (!retries) {
printf("retries is set to zero, set it to %d\n", DHCP_DEFAULT_RETRY);
retries = DHCP_DEFAULT_RETRY;
}
- ret = dhcp(retries, &dhcp_param);
+ ret = dhcp(edev, retries, &dhcp_param);
return ret;
}
@@ -73,8 +87,8 @@ BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(dhcp)
.cmd = do_dhcp,
BAREBOX_CMD_DESC("DHCP client to obtain IP or boot params")
- BAREBOX_CMD_OPTS("[-HvcuUr]")
+ BAREBOX_CMD_OPTS("[-HvcuUr] [device]")
BAREBOX_CMD_GROUP(CMD_GRP_NET)
BAREBOX_CMD_HELP(cmd_dhcp_help)
- BAREBOX_CMD_COMPLETE(empty_complete)
+ BAREBOX_CMD_COMPLETE(eth_complete)
BAREBOX_CMD_END
diff --git a/include/dhcp.h b/include/dhcp.h
index 0796b30cf1..20a523250f 100644
--- a/include/dhcp.h
+++ b/include/dhcp.h
@@ -20,6 +20,8 @@ struct dhcp_req_param {
char *client_uuid;
};
-int dhcp(int retries, struct dhcp_req_param *param);
+struct eth_device;
+
+int dhcp(struct eth_device *edev, int retries, struct dhcp_req_param *param);
#endif
diff --git a/include/net.h b/include/net.h
index 6788f14cb0..416fcde761 100644
--- a/include/net.h
+++ b/include/net.h
@@ -212,13 +212,13 @@ struct icmphdr {
extern unsigned char *NetRxPackets[PKTBUFSRX];/* Receive packets */
-void net_set_ip(IPaddr_t ip);
+void net_set_ip(struct eth_device *edev, IPaddr_t ip);
void net_set_serverip(IPaddr_t ip, bool overwrite);
-void net_set_netmask(IPaddr_t ip);
+void net_set_netmask(struct eth_device *edev, IPaddr_t ip);
void net_set_gateway(IPaddr_t ip);
void net_set_nameserver(IPaddr_t ip);
void net_set_domainname(const char *name);
-IPaddr_t net_get_ip(void);
+IPaddr_t net_get_ip(struct eth_device *edev);
IPaddr_t net_get_serverip(void);
IPaddr_t net_get_gateway(void);
IPaddr_t net_get_nameserver(void);
diff --git a/net/dhcp.c b/net/dhcp.c
index 61abf49272..270fe91ac1 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -79,6 +79,7 @@ static dhcp_state_t dhcp_state;
static uint32_t dhcp_leasetime;
static IPaddr_t net_dhcp_server_ip;
static uint64_t dhcp_start;
+static struct eth_device *dhcp_edev;
static char dhcp_tftpname[256];
static const char* dhcp_get_barebox_global(const char * var)
@@ -126,7 +127,7 @@ static void netmask_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen
IPaddr_t ip;
ip = net_read_ip(popt);
- net_set_netmask(ip);
+ net_set_netmask(dhcp_edev, ip);
}
static void gateway_handle(struct dhcp_opt *opt, unsigned char *popt, int optlen)
@@ -369,7 +370,7 @@ static void bootp_copy_net_params(struct bootp *bp)
IPaddr_t tmp_ip;
tmp_ip = net_read_ip(&bp->bp_yiaddr);
- net_set_ip(tmp_ip);
+ net_set_ip(dhcp_edev, tmp_ip);
tmp_ip = net_read_ip(&bp->bp_siaddr);
if (tmp_ip != 0)
@@ -618,7 +619,7 @@ static void dhcp_handler(void *ctx, char *packet, unsigned int len)
dhcp_options_process((u8 *)&bp->bp_vend[4], bp);
bootp_copy_net_params(bp); /* Store net params from reply */
dhcp_state = BOUND;
- ip = net_get_ip();
+ ip = net_get_ip(dhcp_edev);
printf("DHCP client bound to address %pI4\n", &ip);
return;
}
@@ -646,12 +647,14 @@ static void dhcp_reset_env(void)
}
}
-int dhcp(int retries, struct dhcp_req_param *param)
+int dhcp(struct eth_device *edev, int retries, struct dhcp_req_param *param)
{
int ret = 0;
dhcp_reset_env();
+ dhcp_edev = edev;
+
dhcp_set_param_data(DHCP_HOSTNAME, param->hostname);
dhcp_set_param_data(DHCP_VENDOR_ID, param->vendor_id);
dhcp_set_param_data(DHCP_CLIENT_ID, param->client_id);
@@ -661,7 +664,7 @@ int dhcp(int retries, struct dhcp_req_param *param)
if (!retries)
retries = DHCP_DEFAULT_RETRY;
- dhcp_con = net_udp_new(0xffffffff, PORT_BOOTPS, dhcp_handler, NULL);
+ dhcp_con = net_udp_eth_new(edev, 0xffffffff, PORT_BOOTPS, dhcp_handler, NULL);
if (IS_ERR(dhcp_con)) {
ret = PTR_ERR(dhcp_con);
goto out;
@@ -671,7 +674,7 @@ int dhcp(int retries, struct dhcp_req_param *param)
if (ret)
goto out1;
- net_set_ip(0);
+ net_set_ip(dhcp_edev, 0);
dhcp_start = get_time_ns();
ret = bootp_request(); /* Basically same as BOOTP */
diff --git a/net/ifup.c b/net/ifup.c
index 70ecf9c717..6c298d8501 100644
--- a/net/ifup.c
+++ b/net/ifup.c
@@ -101,12 +101,15 @@ int ifup(const char *name, unsigned flags)
if (!strcmp(ip, "dhcp")) {
IPaddr_t serverip;
+ char *dhcp_cmd;
serverip = getenv_ip("serverip");
if (serverip)
net_set_serverip(serverip, false);
- ret = run_command("dhcp");
+ dhcp_cmd = basprintf("dhcp %s", name);
+ ret = run_command(dhcp_cmd);
+ free(dhcp_cmd);
if (ret)
goto out;
dev_set_param(dev, "linux.bootargs", "ip=dhcp");
diff --git a/net/net.c b/net/net.c
index 1d47bb449a..cf1d0b32fa 100644
--- a/net/net.c
+++ b/net/net.c
@@ -254,24 +254,18 @@ void net_set_serverip(IPaddr_t ip, bool overwrite)
net_serverip = ip;
}
-void net_set_ip(IPaddr_t ip)
+void net_set_ip(struct eth_device *edev, IPaddr_t ip)
{
- struct eth_device *edev = eth_get_current();
-
edev->ipaddr = ip;
}
-IPaddr_t net_get_ip(void)
+IPaddr_t net_get_ip(struct eth_device *edev)
{
- struct eth_device *edev = eth_get_current();
-
return edev->ipaddr;
}
-void net_set_netmask(IPaddr_t nm)
+void net_set_netmask(struct eth_device *edev, IPaddr_t nm)
{
- struct eth_device *edev = eth_get_current();
-
edev->netmask = nm;
}
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2017-11-24 8:13 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-24 8:12 Networking updates Sascha Hauer
2017-11-24 8:12 ` [PATCH 01/21] driver: Add device_detect_all() function Sascha Hauer
2017-11-24 23:34 ` Sam Ravnborg
2017-11-28 7:58 ` Sascha Hauer
2017-11-28 22:52 ` Sam Ravnborg
2017-11-24 8:12 ` [PATCH 02/21] nvvar: when setting a nvvar to NULL just free the content Sascha Hauer
2017-11-24 8:12 ` [PATCH 03/21] net: Make domainname and nameserver globalvars Sascha Hauer
2017-11-24 8:12 ` [PATCH 04/21] net: Add functions to get/set nameserver and domainname Sascha Hauer
2017-11-24 23:47 ` Sam Ravnborg
2017-11-27 15:18 ` Sascha Hauer
2017-11-24 8:12 ` [PATCH 05/21] net: introduce global.net.server Sascha Hauer
2017-11-24 8:12 ` [PATCH 06/21] net: dhcp: Do not overwrite serverip if it is valid Sascha Hauer
2017-11-25 16:36 ` Sam Ravnborg
2017-11-28 7:42 ` Sascha Hauer
2017-11-24 8:12 ` [PATCH 07/21] net: Use a single gateway Sascha Hauer
2017-11-24 8:12 ` [PATCH 08/21] net: allow udp connections on specified network device Sascha Hauer
2017-11-25 16:41 ` Sam Ravnborg
2017-11-27 14:45 ` Sascha Hauer
2017-11-24 8:12 ` Sascha Hauer [this message]
2017-11-25 16:46 ` [PATCH 09/21] net: dhcp: Allow to specify " Sam Ravnborg
2017-11-28 7:50 ` Sascha Hauer
2017-11-24 8:12 ` [PATCH 10/21] net: dhcp: avoid unnecessary casts Sascha Hauer
2017-11-24 8:12 ` [PATCH 11/21] net: dhcp: Coding style fixes Sascha Hauer
2017-11-24 8:12 ` [PATCH 12/21] net: dhcp: rework Sascha Hauer
2017-11-25 17:02 ` Sam Ravnborg
2017-11-28 7:54 ` Sascha Hauer
2017-11-24 8:12 ` [PATCH 13/21] net: Pick network device based on IP settings Sascha Hauer
2017-11-24 8:12 ` [PATCH 14/21] net: remove "current" network device Sascha Hauer
2017-11-24 8:12 ` [PATCH 15/21] net: ifup: Factor out a eth_discover function Sascha Hauer
2017-11-24 8:12 ` [PATCH 16/21] ifup: Use dhcp C API rather than running command Sascha Hauer
2017-11-24 8:12 ` [PATCH 17/21] net: Provide new way to configure network devices Sascha Hauer
2017-11-24 8:12 ` [PATCH 18/21] net: update network docs Sascha Hauer
2017-11-24 8:12 ` [PATCH 19/21] net: environment: remove ethx setup files Sascha Hauer
2017-11-24 8:12 ` [PATCH 20/21] net: environment: update automounts Sascha Hauer
2017-11-24 8:12 ` [PATCH 21/21] defaultenv: Add README for new network config Sascha Hauer
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=20171124081237.6830-10-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/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