mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/5] net: preparations for integrating a network stack
@ 2024-06-10  7:51 Sascha Hauer
  2024-06-10  7:51 ` [PATCH 1/5] net: virtio-net: allow to set current MAC address Sascha Hauer
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

This series has some preparation patches to better integrate a network
stack in the future.

Sascha Hauer (5):
  net: virtio-net: allow to set current MAC address
  net: ifup: use accessor to set network device ip/netmask
  net: add edev argument to net_set_gateway()
  net: establish single code path for setting edev parameters
  net: host command: move to commands/

 commands/Makefile    |  2 +-
 commands/host.c      | 40 ++++++++++++++++++++++++++++++++++++++++
 drivers/net/virtio.c | 34 ++++++++++++++++++++++------------
 include/net.h        |  2 +-
 net/dhcp.c           |  2 +-
 net/dns.c            | 37 -------------------------------------
 net/eth.c            | 33 ++++++++++++++++++++++++++++++---
 net/ifup.c           |  6 +++---
 net/net.c            |  2 +-
 9 files changed, 99 insertions(+), 59 deletions(-)
 create mode 100644 commands/host.c

-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/5] net: virtio-net: allow to set current MAC address
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
@ 2024-06-10  7:51 ` Sascha Hauer
  2024-06-10  7:51 ` [PATCH 2/5] net: ifup: use accessor to set network device ip/netmask Sascha Hauer
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

We currently can't change the current MAC address when VIRTIO_F_VERSION_1
is set, which is always the case. The barebox ethernet code calls set_ethaddr
with this exact MAC address it has just read from the hardware. Just
return successfully when the core wants to set the MAC address we
already have.

Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com>
---
 drivers/net/virtio.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/drivers/net/virtio.c b/drivers/net/virtio.c
index 8605f67ae2..1bc5e0cd90 100644
--- a/drivers/net/virtio.c
+++ b/drivers/net/virtio.c
@@ -135,29 +135,39 @@ static void virtio_net_stop(struct eth_device *dev)
 	 */
 }
 
+static int virtio_net_read_rom_hwaddr(struct eth_device *edev, unsigned char *adr)
+{
+	struct virtio_net_priv *priv = to_priv(edev);
+
+	virtio_cread_bytes(priv->vdev, offsetof(struct virtio_net_config, mac), adr, 6);
+
+	return 0;
+}
+
 static int virtio_net_write_hwaddr(struct eth_device *edev, const unsigned char *adr)
 {
 	struct virtio_net_priv *priv = to_priv(edev);
-	int i;
+	int i, ret;
 
 	/*
 	 * v1.0 compliant device's MAC address is set through control channel,
 	 * which we don't support for now.
 	 */
-	if (virtio_has_feature(priv->vdev, VIRTIO_F_VERSION_1))
-		return -ENOSYS;
-
-	for (i = 0; i < 6; i++)
-		virtio_cwrite8(priv->vdev, offsetof(struct virtio_net_config, mac) + i, adr[i]);
+	if (virtio_has_feature(priv->vdev, VIRTIO_F_VERSION_1)) {
+		char mac[6];
 
-	return 0;
-}
+		ret = virtio_net_read_rom_hwaddr(edev, mac);
+		if (ret)
+			return ret;
 
-static int virtio_net_read_rom_hwaddr(struct eth_device *edev, unsigned char *adr)
-{
-	struct virtio_net_priv *priv = to_priv(edev);
+		if (!memcmp(mac, adr, 5))
+			return 0;
+		else
+			return -ENOSYS;
+	}
 
-	virtio_cread_bytes(priv->vdev, offsetof(struct virtio_net_config, mac), adr, 6);
+	for (i = 0; i < 6; i++)
+		virtio_cwrite8(priv->vdev, offsetof(struct virtio_net_config, mac) + i, adr[i]);
 
 	return 0;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/5] net: ifup: use accessor to set network device ip/netmask
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
  2024-06-10  7:51 ` [PATCH 1/5] net: virtio-net: allow to set current MAC address Sascha Hauer
@ 2024-06-10  7:51 ` Sascha Hauer
  2024-06-10  7:51 ` [PATCH 3/5] net: add edev argument to net_set_gateway() Sascha Hauer
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

We have accessors to set a network devices ip/netmask. Use them
instead of accessing the struct members directly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/ifup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/ifup.c b/net/ifup.c
index 5b92ee794d..2e1a5529a8 100644
--- a/net/ifup.c
+++ b/net/ifup.c
@@ -133,8 +133,8 @@ static int source_env_network(struct eth_device *edev)
 		memcpy(edev->ethaddr, ethaddr, 6);
 
 	if (mode == ETH_MODE_STATIC) {
-		edev->ipaddr = ipaddr;
-		edev->netmask = netmask;
+		net_set_ip(edev, ipaddr);
+		net_set_netmask(edev, netmask);
 		if (gateway)
 			net_set_gateway(gateway);
 		if (serverip)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/5] net: add edev argument to net_set_gateway()
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
  2024-06-10  7:51 ` [PATCH 1/5] net: virtio-net: allow to set current MAC address Sascha Hauer
  2024-06-10  7:51 ` [PATCH 2/5] net: ifup: use accessor to set network device ip/netmask Sascha Hauer
@ 2024-06-10  7:51 ` Sascha Hauer
  2024-06-10  7:51 ` [PATCH 4/5] net: establish single code path for setting edev parameters Sascha Hauer
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

Although we currently can't handle multiple gateways, a gateway is
specific to a network device. Pass the struct eth_device * to
net_set_gateway() for better integration of a real network stack later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/net.h | 2 +-
 net/dhcp.c    | 2 +-
 net/ifup.c    | 2 +-
 net/net.c     | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/net.h b/include/net.h
index 5a6dd9ca7b..1933623936 100644
--- a/include/net.h
+++ b/include/net.h
@@ -254,7 +254,7 @@ void net_set_serverip(IPaddr_t ip);
 const char *net_get_server(void);
 void net_set_serverip_empty(IPaddr_t ip);
 void net_set_netmask(struct eth_device *edev, IPaddr_t ip);
-void net_set_gateway(IPaddr_t ip);
+void net_set_gateway(struct eth_device *edev, IPaddr_t ip);
 void net_set_nameserver(IPaddr_t ip);
 void net_set_domainname(const char *name);
 IPaddr_t net_get_ip(struct eth_device *edev);
diff --git a/net/dhcp.c b/net/dhcp.c
index e1025bf91b..045ccc0183 100644
--- a/net/dhcp.c
+++ b/net/dhcp.c
@@ -574,7 +574,7 @@ int dhcp_set_result(struct eth_device *edev, struct dhcp_result *res)
 
 	net_set_ip(edev, res->ip);
 	net_set_netmask(edev, res->netmask);
-	net_set_gateway(res->gateway);
+	net_set_gateway(edev, res->gateway);
 	net_set_nameserver(res->nameserver);
 
 	set_res(&global_dhcp_bootfile, res->bootfile);
diff --git a/net/ifup.c b/net/ifup.c
index 2e1a5529a8..c79aa445f5 100644
--- a/net/ifup.c
+++ b/net/ifup.c
@@ -136,7 +136,7 @@ static int source_env_network(struct eth_device *edev)
 		net_set_ip(edev, ipaddr);
 		net_set_netmask(edev, netmask);
 		if (gateway)
-			net_set_gateway(gateway);
+			net_set_gateway(edev, gateway);
 		if (serverip)
 			net_set_serverip(serverip);
 	}
diff --git a/net/net.c b/net/net.c
index a9d1306354..4703842f43 100644
--- a/net/net.c
+++ b/net/net.c
@@ -353,7 +353,7 @@ void net_set_netmask(struct eth_device *edev, IPaddr_t nm)
 	edev->netmask = nm;
 }
 
-void net_set_gateway(IPaddr_t gw)
+void net_set_gateway(struct eth_device *edev, IPaddr_t gw)
 {
 	net_gateway = gw;
 }
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/5] net: establish single code path for setting edev parameters
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
                   ` (2 preceding siblings ...)
  2024-06-10  7:51 ` [PATCH 3/5] net: add edev argument to net_set_gateway() Sascha Hauer
@ 2024-06-10  7:51 ` Sascha Hauer
  2024-06-10  7:51 ` [PATCH 5/5] net: host command: move to commands/ Sascha Hauer
  2024-06-13  6:41 ` [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

We have net_set_ip(), net_set_gateway() and net_set_netmask() to set the
IP parameters of a network device. Most code pathes go through these
functions with the exception of setting the parameters through
globalvars on the command line. Explicitly call the accessors for this
code path as well so that all IP parameter settings go through them.
This is done in preparation to integrate a real network stack later.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 net/eth.c | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/net/eth.c b/net/eth.c
index 98567d8d3f..e81018c69b 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -393,6 +393,33 @@ static const char * const eth_mode_names[] = {
 	[ETH_MODE_DISABLED] = "disabled",
 };
 
+static int eth_param_set_ip(struct param_d *p, void *priv)
+{
+	struct eth_device *edev = priv;
+
+	net_set_ip(edev, edev->ipaddr);
+
+	return 0;
+}
+
+static int eth_param_set_gw(struct param_d *p, void *priv)
+{
+	struct eth_device *edev = priv;
+
+	net_set_gateway(edev, net_get_gateway());
+
+	return 0;
+}
+
+static int eth_param_set_nm(struct param_d *p, void *priv)
+{
+	struct eth_device *edev = priv;
+
+	net_set_netmask(edev, edev->netmask);
+
+	return 0;
+}
+
 int eth_register(struct eth_device *edev)
 {
 	struct device *dev = &edev->dev;
@@ -428,10 +455,10 @@ int eth_register(struct eth_device *edev)
 
 	edev->devname = xstrdup(dev_name(&edev->dev));
 
-	dev_add_param_ip(dev, "ipaddr", NULL, NULL, &edev->ipaddr, edev);
+	dev_add_param_ip(dev, "ipaddr", eth_param_set_ip, NULL, &edev->ipaddr, edev);
 	dev_add_param_string(dev, "serverip", NULL, NULL, &net_server, edev);
-	dev_add_param_ip(dev, "gateway", NULL, NULL, &net_gateway, edev);
-	dev_add_param_ip(dev, "netmask", NULL, NULL, &edev->netmask, edev);
+	dev_add_param_ip(dev, "gateway", eth_param_set_gw, NULL, &net_gateway, edev);
+	dev_add_param_ip(dev, "netmask", eth_param_set_nm, NULL, &edev->netmask, edev);
 	dev_add_param_mac(dev, "ethaddr", eth_param_set_ethaddr, NULL,
 			edev->ethaddr, edev);
 	edev->bootarg = xstrdup("");
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 5/5] net: host command: move to commands/
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
                   ` (3 preceding siblings ...)
  2024-06-10  7:51 ` [PATCH 4/5] net: establish single code path for setting edev parameters Sascha Hauer
@ 2024-06-10  7:51 ` Sascha Hauer
  2024-06-13  6:41 ` [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-10  7:51 UTC (permalink / raw)
  To: Barebox List

Commands are usually implemented in the commands/ directory. Move the
host command there as well.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/Makefile |  2 +-
 commands/host.c   | 40 ++++++++++++++++++++++++++++++++++++++++
 net/dns.c         | 37 -------------------------------------
 3 files changed, 41 insertions(+), 38 deletions(-)
 create mode 100644 commands/host.c

diff --git a/commands/Makefile b/commands/Makefile
index 30e1f8403e..19a2dd3112 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -150,5 +150,5 @@ obj-$(CONFIG_CMD_TUTORIAL)	+= tutorial.o
 obj-$(CONFIG_CMD_STACKSMASH)	+= stacksmash.o
 obj-$(CONFIG_CMD_PARTED)	+= parted.o
 obj-$(CONFIG_CMD_EFI_HANDLE_DUMP)	+= efi_handle_dump.o
-
+obj-$(CONFIG_CMD_HOST)		+= host.o
 UBSAN_SANITIZE_ubsan.o := y
diff --git a/commands/host.c b/commands/host.c
new file mode 100644
index 0000000000..b931445056
--- /dev/null
+++ b/commands/host.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <common.h>
+#include <command.h>
+#include <net.h>
+
+static int do_host(int argc, char *argv[])
+{
+	IPaddr_t ip;
+	int ret;
+	char *hostname, *varname = NULL;
+
+	if (argc < 2)
+		return COMMAND_ERROR_USAGE;
+
+	hostname = argv[1];
+
+	if (argc > 2)
+		varname = argv[2];
+
+	ret = resolv(argv[1], &ip);
+	if (ret) {
+		printf("unknown host %s\n", hostname);
+		return 1;
+	}
+
+	if (varname)
+		setenv_ip(varname, ip);
+	else
+		printf("%s is at %pI4\n", hostname, &ip);
+
+	return 0;
+}
+
+BAREBOX_CMD_START(host)
+	.cmd		= do_host,
+	BAREBOX_CMD_DESC("resolve a hostname")
+	BAREBOX_CMD_OPTS("<HOSTNAME> [VARIABLE]")
+	BAREBOX_CMD_GROUP(CMD_GRP_NET)
+BAREBOX_CMD_END
diff --git a/net/dns.c b/net/dns.c
index fdddb3f915..8fbd13cdc3 100644
--- a/net/dns.c
+++ b/net/dns.c
@@ -262,40 +262,3 @@ int resolv(const char *host, IPaddr_t *ip)
 
 	return 0;
 }
-
-#ifdef CONFIG_CMD_HOST
-static int do_host(int argc, char *argv[])
-{
-	IPaddr_t ip;
-	int ret;
-	char *hostname, *varname = NULL;
-
-	if (argc < 2)
-		return COMMAND_ERROR_USAGE;
-
-	hostname = argv[1];
-
-	if (argc > 2)
-		varname = argv[2];
-
-	ret = resolv(argv[1], &ip);
-	if (ret) {
-		printf("unknown host %s\n", hostname);
-		return 1;
-	}
-
-	if (varname)
-		setenv_ip(varname, ip);
-	else
-		printf("%s is at %pI4\n", hostname, &ip);
-
-	return 0;
-}
-
-BAREBOX_CMD_START(host)
-	.cmd		= do_host,
-	BAREBOX_CMD_DESC("resolve a hostname")
-	BAREBOX_CMD_OPTS("<HOSTNAME> [VARIABLE]")
-	BAREBOX_CMD_GROUP(CMD_GRP_NET)
-BAREBOX_CMD_END
-#endif
-- 
2.39.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/5] net: preparations for integrating a network stack
  2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
                   ` (4 preceding siblings ...)
  2024-06-10  7:51 ` [PATCH 5/5] net: host command: move to commands/ Sascha Hauer
@ 2024-06-13  6:41 ` Sascha Hauer
  5 siblings, 0 replies; 7+ messages in thread
From: Sascha Hauer @ 2024-06-13  6:41 UTC (permalink / raw)
  To: Barebox List, Sascha Hauer


On Mon, 10 Jun 2024 09:51:00 +0200, Sascha Hauer wrote:
> This series has some preparation patches to better integrate a network
> stack in the future.
> 
> Sascha Hauer (5):
>   net: virtio-net: allow to set current MAC address
>   net: ifup: use accessor to set network device ip/netmask
>   net: add edev argument to net_set_gateway()
>   net: establish single code path for setting edev parameters
>   net: host command: move to commands/
> 
> [...]

Applied, thanks!

[1/5] net: virtio-net: allow to set current MAC address
      https://git.pengutronix.de/cgit/barebox/commit/?id=7abc114aeeba (link may not be stable)
[2/5] net: ifup: use accessor to set network device ip/netmask
      https://git.pengutronix.de/cgit/barebox/commit/?id=5ed217fa0faa (link may not be stable)
[3/5] net: add edev argument to net_set_gateway()
      https://git.pengutronix.de/cgit/barebox/commit/?id=f54a1f61ae04 (link may not be stable)
[4/5] net: establish single code path for setting edev parameters
      https://git.pengutronix.de/cgit/barebox/commit/?id=4f7bc1804e24 (link may not be stable)
[5/5] net: host command: move to commands/
      https://git.pengutronix.de/cgit/barebox/commit/?id=bf3c04df757c (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-06-13  6:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-10  7:51 [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer
2024-06-10  7:51 ` [PATCH 1/5] net: virtio-net: allow to set current MAC address Sascha Hauer
2024-06-10  7:51 ` [PATCH 2/5] net: ifup: use accessor to set network device ip/netmask Sascha Hauer
2024-06-10  7:51 ` [PATCH 3/5] net: add edev argument to net_set_gateway() Sascha Hauer
2024-06-10  7:51 ` [PATCH 4/5] net: establish single code path for setting edev parameters Sascha Hauer
2024-06-10  7:51 ` [PATCH 5/5] net: host command: move to commands/ Sascha Hauer
2024-06-13  6:41 ` [PATCH 0/5] net: preparations for integrating a network stack Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox