Skip to contentSkip to conversation

net: add virtio network driver

PATCH Applied Ahmad Fatoum 3 participants 3 messages Sep 13, 2021 – Oct 2, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: add virtio network driver

This gives virtio-enabled boards an easy route to network connectivity:

	qemu-system-aarch64 -M virt  -serial mon:stdio -trace file=/dev/null \
	-kernel images/barebox-dt-2nd.img -cpu cortex-a57 -nographic \
	-device virtio-net-device,netdev=network0 -netdev tap,id=network0,ifname=tap0

The tap0 interface created by QEMU can then be bridged/listened on.

---
 Documentation/user/virtio.rst   |   2 +-
 drivers/net/Kconfig             |   7 +
 drivers/net/Makefile            |   1 +
 drivers/net/virtio.c            | 236 +++++++++++++++++++++
 include/linux/virtio_config.h   |   7 +-
 include/uapi/linux/virtio_net.h | 358 ++++++++++++++++++++++++++++++++
 6 files changed, 608 insertions(+), 3 deletions(-)
 create mode 100644 drivers/net/virtio.c
 create mode 100644 include/uapi/linux/virtio_net.h
Documentation/user/virtio.rst +1 -1
index dde47d5f82b1..d944fa4821b1 100644
@@ -35,7 +35,7 @@ queues configuration and buffer transfers are nearly identical. Both MMIO
 and non-legacy PCI are supported in barebox.
 
 The VirtIO spec defines a lots of VirtIO device types, however at present only
-block, console, input and RNG devices are supported.
+block, network, console, input and RNG devices are supported.
 
 Build Instructions
 ------------------
drivers/net/Kconfig +7 -0
index 397164f3f175..4947296f278a 100644
@@ -258,6 +258,13 @@ config DRIVER_NET_EFI_SNP
 	bool "EFI SNP ethernet driver"
 	depends on EFI_BOOTUP
 
+config DRIVER_NET_VIRTIO
+	bool "virtio net driver"
+	depends on VIRTIO
+	help
+	  This is the virtual net driver for virtio. It can be used with
+	  QEMU based targets.
+
 config DRIVER_NET_AG71XX
 	bool "Atheros AG71xx ethernet driver"
 	depends on MACH_MIPS_ATH79
drivers/net/Makefile +1 -0
index b1aa9571fc72..1921d0d9f91c 100644
@@ -36,4 +36,5 @@ obj-$(CONFIG_DRIVER_NET_SMC91111)	+= smc91111.o
 obj-$(CONFIG_DRIVER_NET_TAP)		+= tap.o
 obj-$(CONFIG_DRIVER_NET_TSE)		+= altera_tse.o
 obj-$(CONFIG_DRIVER_NET_EFI_SNP)	+= efi-snp.o
+obj-$(CONFIG_DRIVER_NET_VIRTIO)		+= virtio.o
 obj-$(CONFIG_DRIVER_NET_AG71XX)		+= ag71xx.o
drivers/net/virtio.c new file +236 -0

236 lines not on this page · raw message

include/linux/virtio_config.h +5 -2
index 8160f0952f13..6bb1f768b636 100644
@@ -232,8 +232,11 @@ static inline int virtio_get_config(struct virtio_device *vdev, unsigned int off
  * @len:	the length of the buffer
  * @return 0 if OK, -ve on error
  */
-int virtio_set_config(struct virtio_device *vdev, unsigned int offset,
-		      void *buf, unsigned int len);
+static inline int virtio_set_config(struct virtio_device *vdev, unsigned int offset,
+		      void *buf, unsigned int len)
+{
+	return vdev->config->set_config(vdev, offset, buf, len);
+}
 
 /**
  * virtio_find_vqs() - find virtqueues and instantiate them
include/uapi/linux/virtio_net.h new file +358 -0

358 lines not on this page · raw message

Signature · 7 lines
2.33.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/1 fixup! net: add virtio network driver

We already stop the virtio device. Let's clean up the rest too and unregister the network interface and free the private data.

---
 drivers/net/virtio.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/net/virtio.c +7 -0
index 275588e15c50..ea4d5529037e 100644
@@ -171,6 +171,8 @@ static int virtio_net_probe(struct virtio_device *vdev)
 
 	priv = xzalloc(sizeof(*priv));
 
+	vdev->priv = priv;
+
 	/*
 	 * For v1.0 compliant device, it always assumes the member
 	 * 'num_buffers' exists in the struct virtio_net_hdr while
@@ -205,8 +207,13 @@ static int virtio_net_probe(struct virtio_device *vdev)
 
 static void virtio_net_remove(struct virtio_device *vdev)
 {
+	struct virtio_net_priv *priv = vdev->priv;
+
 	vdev->config->reset(vdev);
+	eth_unregister(&priv->edev);
 	vdev->config->del_vqs(vdev);
+
+	free(priv);
 }
 
 /*
Signature · 7 lines
2.30.2


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Applied
#2 lore raw
In reply to Ahmad Fatoum
Quoted 9 lines · Ahmad Fatoum

This gives virtio-enabled boards an easy route to network connectivity:

	qemu-system-aarch64 -M virt  -serial mon:stdio -trace file=/dev/null \
	-kernel images/barebox-dt-2nd.img -cpu cortex-a57 -nographic \
	-device virtio-net-device,netdev=network0 -netdev tap,id=network0,ifname=tap0

The tap0 interface created by QEMU can then be bridged/listened on.

Quoted 10 lines
---
 Documentation/user/virtio.rst   |   2 +-
 drivers/net/Kconfig             |   7 +
 drivers/net/Makefile            |   1 +
 drivers/net/virtio.c            | 236 +++++++++++++++++++++
 include/linux/virtio_config.h   |   7 +-
 include/uapi/linux/virtio_net.h | 358 ++++++++++++++++++++++++++++++++
 6 files changed, 608 insertions(+), 3 deletions(-)
 create mode 100644 drivers/net/virtio.c
 create mode 100644 include/uapi/linux/virtio_net.h

Applied, thanks

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox