Skip to contentSkip to conversation

vhost_mdev: add network control vq support

RFC In review Tiwei Bie 2 participants 11 messages Oct 29, 2019 – Oct 31, 2019

Subsystems (5)
lore
Share & tools
lore

Tiwei Bie

author
#0 lore raw

RFC 1/1 vhost_mdev: add network control vq support

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

---
This patch depends on below patch:
https://lkml.org/lkml/2019/10/29/335

 drivers/vhost/mdev.c             | 37 ++++++++++++++++++++++++++++++--
 include/linux/virtio_mdev_ops.h  | 10 +++++++++
 include/uapi/linux/vhost.h       |  7 ++++++
 include/uapi/linux/vhost_types.h |  6 ++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
drivers/vhost/mdev.c +35 -2
index 35b2fb33e686..c9b3eaa77405 100644
@@ -47,6 +47,13 @@ enum {
 		(1ULL << VIRTIO_NET_F_HOST_UFO) |
 		(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
 		(1ULL << VIRTIO_NET_F_STATUS) |
+		(1ULL << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
+		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX) |
+		(1ULL << VIRTIO_NET_F_CTRL_VLAN) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX_EXTRA) |
+		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+		(1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR) |
 		(1ULL << VIRTIO_NET_F_SPEED_DUPLEX),
 };
 
@@ -362,6 +369,29 @@ static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd,
 	return r;
 }
 
+/*
+ * Device specific (e.g. network) ioctls.
+ */
+static long vhost_mdev_dev_ioctl(struct vhost_mdev *m, unsigned int cmd,
+				 void __user *argp)
+{
+	struct mdev_device *mdev = m->mdev;
+	const struct virtio_mdev_device_ops *ops = mdev_get_vhost_ops(mdev);
+
+	switch (m->virtio_id) {
+	case VIRTIO_ID_NET:
+		switch (cmd) {
+		case VHOST_MDEV_NET_CTRL:
+			if (!ops->net.ctrl)
+				return -ENOTSUPP;
+			return ops->net.ctrl(mdev, argp);
+		}
+		break;
+	}
+
+	return -ENOIOCTLCMD;
+}
+
 static int vhost_mdev_open(void *device_data)
 {
 	struct vhost_mdev *m = device_data;
@@ -460,8 +490,11 @@ static long vhost_mdev_unlocked_ioctl(void *device_data,
 		 * VHOST_SET_LOG_FD are not used yet.
 		 */
 		r = vhost_dev_ioctl(&m->dev, cmd, argp);
-		if (r == -ENOIOCTLCMD)
-			r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		if (r == -ENOIOCTLCMD) {
+			r = vhost_mdev_dev_ioctl(m, cmd, argp);
+			if (r == -ENOIOCTLCMD)
+				r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		}
 	}
 
 	mutex_unlock(&m->mutex);
include/linux/virtio_mdev_ops.h +10 -0
index d417b41f2845..622861804ebd 100644
@@ -20,6 +20,8 @@ struct virtio_mdev_callback {
 	void *private;
 };
 
+struct vhost_mdev_net_ctrl;
+
 /**
  * struct vfio_mdev_device_ops - Structure to be registered for each
  * mdev device to register the device for virtio/vhost drivers.
@@ -151,6 +153,14 @@ struct virtio_mdev_device_ops {
 
 	/* Mdev device ops */
 	u64 (*get_mdev_features)(struct mdev_device *mdev);
+
+	/* Vhost-mdev (MDEV_CLASS_ID_VHOST) specific ops */
+	union {
+		struct {
+			int (*ctrl)(struct mdev_device *mdev,
+				    struct vhost_mdev_net_ctrl __user *ctrl);
+		} net;
+	};
 };
 
 void mdev_set_virtio_ops(struct mdev_device *mdev,
include/uapi/linux/vhost.h +7 -0
index 061a2824a1b3..3693b2cba0c4 100644
@@ -134,4 +134,11 @@
 /* Get the max ring size. */
 #define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
 
+/* VHOST_MDEV device specific defines */
+
+/* Send virtio-net commands. The commands follow the same definition
+ * of the virtio-net commands defined in virtio-spec.
+ */
+#define VHOST_MDEV_NET_CTRL		_IOW(VHOST_VIRTIO, 0x77, struct vhost_mdev_net_ctrl *)
+
 #endif
include/uapi/linux/vhost_types.h +6 -0
index 7b105d0b2fb9..e76b4d8e35e5 100644
@@ -127,6 +127,12 @@ struct vhost_mdev_config {
 	__u8 buf[0];
 };
 
+struct vhost_mdev_net_ctrl {
+	__u8 class;
+	__u8 cmd;
+	__u8 cmd_data[0];
+} __attribute__((packed));
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26
Signature · 1 line
2.23.0
#1 lore raw
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Quoted 46 lines

---
This patch depends on below patch:
https://lkml.org/lkml/2019/10/29/335

 drivers/vhost/mdev.c             | 37 ++++++++++++++++++++++++++++++--
 include/linux/virtio_mdev_ops.h  | 10 +++++++++
 include/uapi/linux/vhost.h       |  7 ++++++
 include/uapi/linux/vhost_types.h |  6 ++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
drivers/vhost/mdev.c +19 -0
index 35b2fb33e686..c9b3eaa77405 100644
@@ -47,6 +47,13 @@ enum {
 		(1ULL << VIRTIO_NET_F_HOST_UFO) |
 		(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
 		(1ULL << VIRTIO_NET_F_STATUS) |
+		(1ULL << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
+		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX) |
+		(1ULL << VIRTIO_NET_F_CTRL_VLAN) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX_EXTRA) |
+		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+		(1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR) |
 		(1ULL << VIRTIO_NET_F_SPEED_DUPLEX),
 };
 
@@ -362,6 +369,29 @@ static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd,
 	return r;
 }
 
+/*
+ * Device specific (e.g. network) ioctls.
+ */
+static long vhost_mdev_dev_ioctl(struct vhost_mdev *m, unsigned int cmd,
+				 void __user *argp)
+{
+	struct mdev_device *mdev = m->mdev;
+	const struct virtio_mdev_device_ops *ops = mdev_get_vhost_ops(mdev);
+
+	switch (m->virtio_id) {
+	case VIRTIO_ID_NET:
+		switch (cmd) {

drivers/vhost/mdev.c static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd, · line 393

Quoted 10 lines
drivers/vhost/mdev.c +10 -0
+		case VHOST_MDEV_NET_CTRL:
+			if (!ops->net.ctrl)
+				return -ENOTSUPP;
+			return ops->net.ctrl(mdev, argp);
+		}
+		break;
+	}
+
+	return -ENOIOCTLCMD;
+}

As you comment above, then vhost-mdev need device specific stuffs.

Quoted 33 lines
drivers/vhost/mdev.c +6 -2
+
 static int vhost_mdev_open(void *device_data)
 {
 	struct vhost_mdev *m = device_data;
@@ -460,8 +490,11 @@ static long vhost_mdev_unlocked_ioctl(void *device_data,
 		 * VHOST_SET_LOG_FD are not used yet.
 		 */
 		r = vhost_dev_ioctl(&m->dev, cmd, argp);
-		if (r == -ENOIOCTLCMD)
-			r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		if (r == -ENOIOCTLCMD) {
+			r = vhost_mdev_dev_ioctl(m, cmd, argp);
+			if (r == -ENOIOCTLCMD)
+				r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		}
 	}
 
 	mutex_unlock(&m->mutex);
include/linux/virtio_mdev_ops.h +2 -0
index d417b41f2845..622861804ebd 100644
@@ -20,6 +20,8 @@ struct virtio_mdev_callback {
 	void *private;
 };
 
+struct vhost_mdev_net_ctrl;
+
 /**
  * struct vfio_mdev_device_ops - Structure to be registered for each
  * mdev device to register the device for virtio/vhost drivers.
@@ -151,6 +153,14 @@ struct virtio_mdev_device_ops {
 

include/linux/virtio_mdev_ops.h struct virtio_mdev_device_ops { · line 163

Quoted 10 lines
include/linux/virtio_mdev_ops.h +8 -0
 	/* Mdev device ops */
 	u64 (*get_mdev_features)(struct mdev_device *mdev);
+
+	/* Vhost-mdev (MDEV_CLASS_ID_VHOST) specific ops */
+	union {
+		struct {
+			int (*ctrl)(struct mdev_device *mdev,
+				    struct vhost_mdev_net_ctrl __user *ctrl);
+		} net;
+	};

And so did device_ops. I still think we'd try out best to avoid such thing.

Thanks

Quoted 26 lines
include/linux/virtio_mdev_ops.h +0 -0
 };
 
 void mdev_set_virtio_ops(struct mdev_device *mdev,
include/uapi/linux/vhost.h +7 -0
index 061a2824a1b3..3693b2cba0c4 100644
@@ -134,4 +134,11 @@
 /* Get the max ring size. */
 #define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
 
+/* VHOST_MDEV device specific defines */
+
+/* Send virtio-net commands. The commands follow the same definition
+ * of the virtio-net commands defined in virtio-spec.
+ */
+#define VHOST_MDEV_NET_CTRL		_IOW(VHOST_VIRTIO, 0x77, struct vhost_mdev_net_ctrl *)
+
 #endif
include/uapi/linux/vhost_types.h +0 -0
index 7b105d0b2fb9..e76b4d8e35e5 100644
@@ -127,6 +127,12 @@ struct vhost_mdev_config {
 	__u8 buf[0];
 };

include/uapi/linux/vhost_types.h struct vhost_mdev_config { · line 138

Quoted 10 lines
include/uapi/linux/vhost_types.h +6 -0
 
+struct vhost_mdev_net_ctrl {
+	__u8 class;
+	__u8 cmd;
+	__u8 cmd_data[0];
+} __attribute__((packed));
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to Tiwei Bie
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Quoted 46 lines

---
This patch depends on below patch:
https://lkml.org/lkml/2019/10/29/335

 drivers/vhost/mdev.c             | 37 ++++++++++++++++++++++++++++++--
 include/linux/virtio_mdev_ops.h  | 10 +++++++++
 include/uapi/linux/vhost.h       |  7 ++++++
 include/uapi/linux/vhost_types.h |  6 ++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
drivers/vhost/mdev.c +19 -0
index 35b2fb33e686..c9b3eaa77405 100644
@@ -47,6 +47,13 @@ enum {
 		(1ULL << VIRTIO_NET_F_HOST_UFO) |
 		(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
 		(1ULL << VIRTIO_NET_F_STATUS) |
+		(1ULL << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
+		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX) |
+		(1ULL << VIRTIO_NET_F_CTRL_VLAN) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX_EXTRA) |
+		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+		(1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR) |
 		(1ULL << VIRTIO_NET_F_SPEED_DUPLEX),
 };
 
@@ -362,6 +369,29 @@ static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd,
 	return r;
 }
 
+/*
+ * Device specific (e.g. network) ioctls.
+ */
+static long vhost_mdev_dev_ioctl(struct vhost_mdev *m, unsigned int cmd,
+				 void __user *argp)
+{
+	struct mdev_device *mdev = m->mdev;
+	const struct virtio_mdev_device_ops *ops = mdev_get_vhost_ops(mdev);
+
+	switch (m->virtio_id) {
+	case VIRTIO_ID_NET:
+		switch (cmd) {

drivers/vhost/mdev.c static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd, · line 393

Quoted 10 lines
drivers/vhost/mdev.c +10 -0
+		case VHOST_MDEV_NET_CTRL:
+			if (!ops->net.ctrl)
+				return -ENOTSUPP;
+			return ops->net.ctrl(mdev, argp);
+		}
+		break;
+	}
+
+	return -ENOIOCTLCMD;
+}

As you comment above, then vhost-mdev need device specific stuffs.

Quoted 33 lines
drivers/vhost/mdev.c +6 -2
+
 static int vhost_mdev_open(void *device_data)
 {
 	struct vhost_mdev *m = device_data;
@@ -460,8 +490,11 @@ static long vhost_mdev_unlocked_ioctl(void *device_data,
 		 * VHOST_SET_LOG_FD are not used yet.
 		 */
 		r = vhost_dev_ioctl(&m->dev, cmd, argp);
-		if (r == -ENOIOCTLCMD)
-			r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		if (r == -ENOIOCTLCMD) {
+			r = vhost_mdev_dev_ioctl(m, cmd, argp);
+			if (r == -ENOIOCTLCMD)
+				r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		}
 	}
 
 	mutex_unlock(&m->mutex);
include/linux/virtio_mdev_ops.h +2 -0
index d417b41f2845..622861804ebd 100644
@@ -20,6 +20,8 @@ struct virtio_mdev_callback {
 	void *private;
 };
 
+struct vhost_mdev_net_ctrl;
+
 /**
  * struct vfio_mdev_device_ops - Structure to be registered for each
  * mdev device to register the device for virtio/vhost drivers.
@@ -151,6 +153,14 @@ struct virtio_mdev_device_ops {
 

include/linux/virtio_mdev_ops.h struct virtio_mdev_device_ops { · line 163

Quoted 10 lines
include/linux/virtio_mdev_ops.h +8 -0
 	/* Mdev device ops */
 	u64 (*get_mdev_features)(struct mdev_device *mdev);
+
+	/* Vhost-mdev (MDEV_CLASS_ID_VHOST) specific ops */
+	union {
+		struct {
+			int (*ctrl)(struct mdev_device *mdev,
+				    struct vhost_mdev_net_ctrl __user *ctrl);
+		} net;
+	};

And so did device_ops. I still think we'd try out best to avoid such thing.

Thanks

Quoted 36 lines
include/linux/virtio_mdev_ops.h +0 -0
 };
 
 void mdev_set_virtio_ops(struct mdev_device *mdev,
include/uapi/linux/vhost.h +7 -0
index 061a2824a1b3..3693b2cba0c4 100644
@@ -134,4 +134,11 @@
 /* Get the max ring size. */
 #define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
 
+/* VHOST_MDEV device specific defines */
+
+/* Send virtio-net commands. The commands follow the same definition
+ * of the virtio-net commands defined in virtio-spec.
+ */
+#define VHOST_MDEV_NET_CTRL		_IOW(VHOST_VIRTIO, 0x77, struct vhost_mdev_net_ctrl *)
+
 #endif
include/uapi/linux/vhost_types.h +6 -0
index 7b105d0b2fb9..e76b4d8e35e5 100644
@@ -127,6 +127,12 @@ struct vhost_mdev_config {
 	__u8 buf[0];
 };
 
+struct vhost_mdev_net_ctrl {
+	__u8 class;
+	__u8 cmd;
+	__u8 cmd_data[0];
+} __attribute__((packed));
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26
author
#3 lore raw
Quoted 6 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Quoted 52 lines
Quoted 52 lines

---
This patch depends on below patch:
https://lkml.org/lkml/2019/10/29/335

 drivers/vhost/mdev.c             | 37 ++++++++++++++++++++++++++++++--
 include/linux/virtio_mdev_ops.h  | 10 +++++++++
 include/uapi/linux/vhost.h       |  7 ++++++
 include/uapi/linux/vhost_types.h |  6 ++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
drivers/vhost/mdev.c +23 -0
index 35b2fb33e686..c9b3eaa77405 100644
@@ -47,6 +47,13 @@ enum {
 		(1ULL << VIRTIO_NET_F_HOST_UFO) |
 		(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
 		(1ULL << VIRTIO_NET_F_STATUS) |
+		(1ULL << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
+		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX) |
+		(1ULL << VIRTIO_NET_F_CTRL_VLAN) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX_EXTRA) |
+		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+		(1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR) |
 		(1ULL << VIRTIO_NET_F_SPEED_DUPLEX),
 };
 
@@ -362,6 +369,29 @@ static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd,
 	return r;
 }
 
+/*
+ * Device specific (e.g. network) ioctls.
+ */
+static long vhost_mdev_dev_ioctl(struct vhost_mdev *m, unsigned int cmd,
+				 void __user *argp)
+{
+	struct mdev_device *mdev = m->mdev;
+	const struct virtio_mdev_device_ops *ops = mdev_get_vhost_ops(mdev);
+
+	switch (m->virtio_id) {
+	case VIRTIO_ID_NET:
+		switch (cmd) {
+		case VHOST_MDEV_NET_CTRL:
+			if (!ops->net.ctrl)
+				return -ENOTSUPP;
+			return ops->net.ctrl(mdev, argp);
Quoted 8 lines

drivers/vhost/mdev.c static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd, · line 393

Quoted 6 lines
drivers/vhost/mdev.c +6 -0
+		}
+		break;
+	}
+
+	return -ENOIOCTLCMD;
+}

As you comment above, then vhost-mdev need device specific stuffs.

Yeah. But this device specific stuff is quite small and simple. It's just to forward the settings between parent and userspace. But I totally agree it would be really great if we could avoid it in an elegant way.

Quoted 35 lines
Quoted 35 lines
drivers/vhost/mdev.c +6 -2
+
 static int vhost_mdev_open(void *device_data)
 {
 	struct vhost_mdev *m = device_data;
@@ -460,8 +490,11 @@ static long vhost_mdev_unlocked_ioctl(void *device_data,
 		 * VHOST_SET_LOG_FD are not used yet.
 		 */
 		r = vhost_dev_ioctl(&m->dev, cmd, argp);
-		if (r == -ENOIOCTLCMD)
-			r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		if (r == -ENOIOCTLCMD) {
+			r = vhost_mdev_dev_ioctl(m, cmd, argp);
+			if (r == -ENOIOCTLCMD)
+				r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		}
 	}
 
 	mutex_unlock(&m->mutex);
include/linux/virtio_mdev_ops.h +2 -0
index d417b41f2845..622861804ebd 100644
@@ -20,6 +20,8 @@ struct virtio_mdev_callback {
 	void *private;
 };
 
+struct vhost_mdev_net_ctrl;
+
 /**
  * struct vfio_mdev_device_ops - Structure to be registered for each
  * mdev device to register the device for virtio/vhost drivers.
@@ -151,6 +153,14 @@ struct virtio_mdev_device_ops {
 
Quoted 12 lines

include/linux/virtio_mdev_ops.h struct virtio_mdev_device_ops { · line 163

Quoted 10 lines
include/linux/virtio_mdev_ops.h +8 -0
 	/* Mdev device ops */
 	u64 (*get_mdev_features)(struct mdev_device *mdev);
+
+	/* Vhost-mdev (MDEV_CLASS_ID_VHOST) specific ops */
+	union {
+		struct {
+			int (*ctrl)(struct mdev_device *mdev,
+				    struct vhost_mdev_net_ctrl __user *ctrl);
+		} net;
+	};

And so did device_ops. I still think we'd try out best to avoid such thing.

I totally agree we should try our best to avoid it. But in this case, I think it may be worth considering as it does simplify the userspace API and the parent driver implementation a lot..

Quoted 41 lines

Thanks

Quoted 36 lines
include/linux/virtio_mdev_ops.h +0 -0
 };
 
 void mdev_set_virtio_ops(struct mdev_device *mdev,
include/uapi/linux/vhost.h +7 -0
index 061a2824a1b3..3693b2cba0c4 100644
@@ -134,4 +134,11 @@
 /* Get the max ring size. */
 #define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
 
+/* VHOST_MDEV device specific defines */
+
+/* Send virtio-net commands. The commands follow the same definition
+ * of the virtio-net commands defined in virtio-spec.
+ */
+#define VHOST_MDEV_NET_CTRL		_IOW(VHOST_VIRTIO, 0x77, struct vhost_mdev_net_ctrl *)
+
 #endif
include/uapi/linux/vhost_types.h +6 -0
index 7b105d0b2fb9..e76b4d8e35e5 100644
@@ -127,6 +127,12 @@ struct vhost_mdev_config {
 	__u8 buf[0];
 };
 
+struct vhost_mdev_net_ctrl {
+	__u8 class;
+	__u8 cmd;
+	__u8 cmd_data[0];
+} __attribute__((packed));
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26
#4 lore raw
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
In reply to Tiwei Bie
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks

author
#6 lore raw
Quoted 21 lines · Jason Wang
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

Quoted 7 lines

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks for the list! It looks dirty to define specific commands and types in vhost UAPI for each of them in the future. It's definitely much better to find an approach to solve it once for all if possible..

Just a quick thought, considering all vhost-mdev does is just to forward settings between parent and userspace, I'm wondering whether it's possible to make the argp opaque in vhost-mdev UAPI and just introduce one generic ioctl command to deliver these device specific commands (which are opaque in vhost-mdev as vhost-mdev just pass the pointer -- argp) defined by spec.

I'm also fine with exposing ctrlq to userspace directly. PS. It's interesting that some devices have more than one ctrlq. I need to take a close look first..

Quoted 3 lines

Thanks

#7 lore raw
Quoted 33 lines · Tiwei Bie
Quoted 20 lines · Jason Wang
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

Quoted 7 lines

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks for the list! It looks dirty to define specific commands and types in vhost UAPI for each of them in the future. It's definitely much better to find an approach to solve it once for all if possible..

Quoted 7 lines

Just a quick thought, considering all vhost-mdev does is just to forward settings between parent and userspace, I'm wondering whether it's possible to make the argp opaque in vhost-mdev UAPI and just introduce one generic ioctl command to deliver these device specific commands (which are opaque in vhost-mdev as vhost-mdev just pass the pointer -- argp) defined by spec.

It looks that using opaque pointer is probably not good for UAPI. And we need also invent API for eventq.

Quoted 4 lines

I'm also fine with exposing ctrlq to userspace directly. PS. It's interesting that some devices have more than one ctrlq. I need to take a close look first..

Thanks.

Quoted 4 lines
Quoted 4 lines

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#8 lore raw
In reply to Tiwei Bie
Quoted 33 lines · Tiwei Bie
Quoted 20 lines · Jason Wang
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

Quoted 7 lines

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks for the list! It looks dirty to define specific commands and types in vhost UAPI for each of them in the future. It's definitely much better to find an approach to solve it once for all if possible..

Quoted 7 lines

Just a quick thought, considering all vhost-mdev does is just to forward settings between parent and userspace, I'm wondering whether it's possible to make the argp opaque in vhost-mdev UAPI and just introduce one generic ioctl command to deliver these device specific commands (which are opaque in vhost-mdev as vhost-mdev just pass the pointer -- argp) defined by spec.

It looks that using opaque pointer is probably not good for UAPI. And we need also invent API for eventq.

Quoted 4 lines

I'm also fine with exposing ctrlq to userspace directly. PS. It's interesting that some devices have more than one ctrlq. I need to take a close look first..

Thanks.

Quoted 4 lines
Quoted 4 lines

Thanks

author
#9 lore raw
In reply to Jason Wang
Quoted 21 lines · Jason Wang
Quoted 11 lines · Tiwei Bie
Quoted 5 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Then maybe it's better to introduce vhost-mdev-net on top?

Looking at the other type of virtio device:

- console have two control virtqueues when multiqueue port is enabled

- SCSI has controlq + eventq

Quoted 7 lines

- GPU has controlq

- Crypto device has one controlq

- Socket has eventq

...

Thanks for the list! It looks dirty to define specific commands and types in vhost UAPI for each of them in the future. It's definitely much better to find an approach to solve it once for all if possible..

Just a quick thought, considering all vhost-mdev does is just to forward settings between parent and userspace, I'm wondering whether it's possible to make the argp opaque in vhost-mdev UAPI and just introduce one generic ioctl command to deliver these device specific commands (which are opaque in vhost-mdev as vhost-mdev just pass the pointer -- argp) defined by spec.

I'm also fine with exposing ctrlq to userspace directly. PS. It's interesting that some devices have more than one ctrlq. I need to take a close look first..

Quoted 3 lines

Thanks

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#10 lore raw
In reply to Jason Wang
Quoted 6 lines · Jason Wang
Quoted 3 lines · Tiwei Bie

This patch adds the network control vq support in vhost-mdev. A vhost-mdev specific op is introduced to allow parent drivers to handle the network control commands come from userspace.

Probably work for userspace driver but not kernel driver.

Exactly. This is only for userspace.

I got your point now. In virtio-mdev kernel driver case, the ctrl-vq can be special as well.

Quoted 52 lines
Quoted 52 lines

---
This patch depends on below patch:
https://lkml.org/lkml/2019/10/29/335

 drivers/vhost/mdev.c             | 37 ++++++++++++++++++++++++++++++--
 include/linux/virtio_mdev_ops.h  | 10 +++++++++
 include/uapi/linux/vhost.h       |  7 ++++++
 include/uapi/linux/vhost_types.h |  6 ++++++
 4 files changed, 58 insertions(+), 2 deletions(-)
drivers/vhost/mdev.c +23 -0
index 35b2fb33e686..c9b3eaa77405 100644
@@ -47,6 +47,13 @@ enum {
 		(1ULL << VIRTIO_NET_F_HOST_UFO) |
 		(1ULL << VIRTIO_NET_F_MRG_RXBUF) |
 		(1ULL << VIRTIO_NET_F_STATUS) |
+		(1ULL << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
+		(1ULL << VIRTIO_NET_F_CTRL_VQ) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX) |
+		(1ULL << VIRTIO_NET_F_CTRL_VLAN) |
+		(1ULL << VIRTIO_NET_F_CTRL_RX_EXTRA) |
+		(1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) |
+		(1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR) |
 		(1ULL << VIRTIO_NET_F_SPEED_DUPLEX),
 };
 
@@ -362,6 +369,29 @@ static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd,
 	return r;
 }
 
+/*
+ * Device specific (e.g. network) ioctls.
+ */
+static long vhost_mdev_dev_ioctl(struct vhost_mdev *m, unsigned int cmd,
+				 void __user *argp)
+{
+	struct mdev_device *mdev = m->mdev;
+	const struct virtio_mdev_device_ops *ops = mdev_get_vhost_ops(mdev);
+
+	switch (m->virtio_id) {
+	case VIRTIO_ID_NET:
+		switch (cmd) {
+		case VHOST_MDEV_NET_CTRL:
+			if (!ops->net.ctrl)
+				return -ENOTSUPP;
+			return ops->net.ctrl(mdev, argp);
Quoted 8 lines

drivers/vhost/mdev.c static long vhost_mdev_vring_ioctl(struct vhost_mdev *m, unsigned int cmd, · line 393

Quoted 6 lines
drivers/vhost/mdev.c +6 -0
+		}
+		break;
+	}
+
+	return -ENOIOCTLCMD;
+}

As you comment above, then vhost-mdev need device specific stuffs.

Yeah. But this device specific stuff is quite small and simple. It's just to forward the settings between parent and userspace. But I totally agree it would be really great if we could avoid it in an elegant way.

Quoted 35 lines
Quoted 35 lines
drivers/vhost/mdev.c +6 -2
+
 static int vhost_mdev_open(void *device_data)
 {
 	struct vhost_mdev *m = device_data;
@@ -460,8 +490,11 @@ static long vhost_mdev_unlocked_ioctl(void *device_data,
 		 * VHOST_SET_LOG_FD are not used yet.
 		 */
 		r = vhost_dev_ioctl(&m->dev, cmd, argp);
-		if (r == -ENOIOCTLCMD)
-			r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		if (r == -ENOIOCTLCMD) {
+			r = vhost_mdev_dev_ioctl(m, cmd, argp);
+			if (r == -ENOIOCTLCMD)
+				r = vhost_mdev_vring_ioctl(m, cmd, argp);
+		}
 	}
 
 	mutex_unlock(&m->mutex);
include/linux/virtio_mdev_ops.h +2 -0
index d417b41f2845..622861804ebd 100644
@@ -20,6 +20,8 @@ struct virtio_mdev_callback {
 	void *private;
 };
 
+struct vhost_mdev_net_ctrl;
+
 /**
  * struct vfio_mdev_device_ops - Structure to be registered for each
  * mdev device to register the device for virtio/vhost drivers.
@@ -151,6 +153,14 @@ struct virtio_mdev_device_ops {
 
Quoted 12 lines

include/linux/virtio_mdev_ops.h struct virtio_mdev_device_ops { · line 163

Quoted 10 lines
include/linux/virtio_mdev_ops.h +8 -0
 	/* Mdev device ops */
 	u64 (*get_mdev_features)(struct mdev_device *mdev);
+
+	/* Vhost-mdev (MDEV_CLASS_ID_VHOST) specific ops */
+	union {
+		struct {
+			int (*ctrl)(struct mdev_device *mdev,
+				    struct vhost_mdev_net_ctrl __user *ctrl);
+		} net;
+	};

And so did device_ops. I still think we'd try out best to avoid such thing.

I totally agree we should try our best to avoid it. But in this case, I think it may be worth considering as it does simplify the userspace API and the parent driver implementation a lot..

Quoted 31 lines

Thanks

Quoted 27 lines
include/linux/virtio_mdev_ops.h +0 -0
 };
 
 void mdev_set_virtio_ops(struct mdev_device *mdev,
include/uapi/linux/vhost.h +7 -0
index 061a2824a1b3..3693b2cba0c4 100644
@@ -134,4 +134,11 @@
 /* Get the max ring size. */
 #define VHOST_MDEV_GET_VRING_NUM	_IOR(VHOST_VIRTIO, 0x76, __u16)
 
+/* VHOST_MDEV device specific defines */
+
+/* Send virtio-net commands. The commands follow the same definition
+ * of the virtio-net commands defined in virtio-spec.
+ */
+#define VHOST_MDEV_NET_CTRL		_IOW(VHOST_VIRTIO, 0x77, struct vhost_mdev_net_ctrl *)
+
 #endif
include/uapi/linux/vhost_types.h +0 -0
index 7b105d0b2fb9..e76b4d8e35e5 100644
@@ -127,6 +127,12 @@ struct vhost_mdev_config {
 	__u8 buf[0];
 };
 
Quoted 10 lines

include/uapi/linux/vhost_types.h struct vhost_mdev_config { · line 138

Quoted 9 lines
include/uapi/linux/vhost_types.h +6 -0
+struct vhost_mdev_net_ctrl {
+	__u8 class;
+	__u8 cmd;
+	__u8 cmd_data[0];
+} __attribute__((packed));
+
 /* Feature bits */
 /* Log all write descriptors. Can be changed while device is active. */
 #define VHOST_F_LOG_ALL 26

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization