Skip to contentSkip to conversation

virtio-net: do not start queues that are not enabled by the guest

PATCH In review Yuri Benditovich 3 participants 20 messages Feb 13, 2019 – Feb 28, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-net: do not start queues that are not enabled by the guest

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device, this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
 hw/net/virtio-net.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +5 -2
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(n);
     NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
     if (!get_vhost_net(nc->peer)) {
         return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
         return VIRTIO_NET_ERR;
     }
 
-    n->curr_queues = queues;
     /* stop the backend before changing the number of queues to avoid handling a
      * disabled queue */
+    virtio_net_set_status(vdev, 0);
+
+    n->curr_queues = queues;
+
     virtio_net_set_status(vdev, vdev->status);
     virtio_net_set_queues(n);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 25 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
  hw/net/virtio-net.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  {
      VirtIODevice *vdev = VIRTIO_DEVICE(n);
      NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
      if (!get_vhost_net(nc->peer)) {
          return;

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 7 lines
hw/net/virtio-net.c +1 -1
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
          return VIRTIO_NET_ERR;
      }
 
-    n->curr_queues = queues;
      /* stop the backend before changing the number of queues to avoid handling a
       * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
      virtio_net_set_status(vdev, vdev->status);
      virtio_net_set_queues(n);
 
#2 lore raw
Quoted 44 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
  hw/net/virtio-net.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  {
      VirtIODevice *vdev = VIRTIO_DEVICE(n);
      NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
      if (!get_vhost_net(nc->peer)) {
          return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
          return VIRTIO_NET_ERR;
      }
 
Quoted 7 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
      /* stop the backend before changing the number of queues to avoid handling a
       * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

Quoted 10 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
      virtio_net_set_status(vdev, vdev->status);
      virtio_net_set_queues(n);
 
#3 lore raw
Quoted 53 lines · Yuri Benditovich
Quoted 44 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
  hw/net/virtio-net.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  {
      VirtIODevice *vdev = VIRTIO_DEVICE(n);
      NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
      if (!get_vhost_net(nc->peer)) {
          return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
          return VIRTIO_NET_ERR;
      }
 
Quoted 7 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
      /* stop the backend before changing the number of queues to avoid handling a
       * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Quoted 10 lines
Quoted 10 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
      virtio_net_set_status(vdev, vdev->status);
      virtio_net_set_queues(n);
 
#4 lore raw
Quoted 63 lines · Michael S. Tsirkin
Quoted 53 lines · Yuri Benditovich
Quoted 44 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
  hw/net/virtio-net.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  {
      VirtIODevice *vdev = VIRTIO_DEVICE(n);
      NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
      if (!get_vhost_net(nc->peer)) {
          return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
          return VIRTIO_NET_ERR;
      }
 
Quoted 7 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
      /* stop the backend before changing the number of queues to avoid handling a
       * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

Quoted 8 lines

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
      virtio_net_set_status(vdev, vdev->status);
      virtio_net_set_queues(n);
 
#5 lore raw
Quoted 71 lines · Yuri Benditovich
Quoted 70 lines · Michael S. Tsirkin
Quoted 53 lines · Yuri Benditovich
Quoted 44 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
  hw/net/virtio-net.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  {
      VirtIODevice *vdev = VIRTIO_DEVICE(n);
      NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
      if (!get_vhost_net(nc->peer)) {
          return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
          return VIRTIO_NET_ERR;
      }
 
Quoted 7 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
      /* stop the backend before changing the number of queues to avoid handling a
       * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Quoted 6 lines
Quoted 1 line

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
      virtio_net_set_status(vdev, vdev->status);
      virtio_net_set_queues(n);
 
#6 lore raw
Quoted 66 lines · Michael S. Tsirkin
Quoted 65 lines · Yuri Benditovich
Quoted 64 lines · Michael S. Tsirkin
Quoted 49 lines · Yuri Benditovich
Quoted 42 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
   hw/net/virtio-net.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
   {
       VirtIODevice *vdev = VIRTIO_DEVICE(n);
       NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
       if (!get_vhost_net(nc->peer)) {
           return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
           return VIRTIO_NET_ERR;
       }
 
Quoted 6 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
       /* stop the backend before changing the number of queues to avoid handling a
        * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

Thanks

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
       virtio_net_set_status(vdev, vdev->status);
       virtio_net_set_queues(n);
 
#7 lore raw
Quoted 74 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 70 lines · Yuri Benditovich
Quoted 57 lines · Michael S. Tsirkin
Quoted 49 lines · Yuri Benditovich
Quoted 42 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
   hw/net/virtio-net.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
   {
       VirtIODevice *vdev = VIRTIO_DEVICE(n);
       NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
       if (!get_vhost_net(nc->peer)) {
           return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
           return VIRTIO_NET_ERR;
       }
 
Quoted 6 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
       /* stop the backend before changing the number of queues to avoid handling a
        * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

Quoted 8 lines

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Quoted 8 lines
Quoted 2 lines

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Traditionally devices assumed that queue address 0 implies not enabled.

Quoted 6 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

Thanks

Do you agree it's a compliance issue though?

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
       virtio_net_set_status(vdev, vdev->status);
       virtio_net_set_queues(n);
 
#8 lore raw
Quoted 70 lines · Michael S. Tsirkin
Quoted 69 lines · Jason Wang
Quoted 68 lines · Michael S. Tsirkin
Quoted 67 lines · Yuri Benditovich
Quoted 54 lines · Michael S. Tsirkin
Quoted 46 lines · Yuri Benditovich
Quoted 40 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
    hw/net/virtio-net.c | 7 +++++--
    1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
    {
        VirtIODevice *vdev = VIRTIO_DEVICE(n);
        NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
        if (!get_vhost_net(nc->peer)) {
            return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
            return VIRTIO_NET_ERR;
        }
 
Quoted 5 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
        /* stop the backend before changing the number of queues to avoid handling a
         * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

Quoted 8 lines

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Traditionally devices assumed that queue address 0 implies not enabled.

So device enable the queue through write a non zero value for queue address? Unfortunately this misses the spec.

Quoted 8 lines
Quoted 7 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

Thanks

Do you agree it's a compliance issue though?

Yes, but it needs more work than what this patch did.

Thanks

#9 lore raw
In reply to Jason Wang
Quoted 75 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 70 lines · Yuri Benditovich
Quoted 57 lines · Michael S. Tsirkin
Quoted 49 lines · Yuri Benditovich
Quoted 42 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
   hw/net/virtio-net.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
   {
       VirtIODevice *vdev = VIRTIO_DEVICE(n);
       NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
       if (!get_vhost_net(nc->peer)) {
           return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
           return VIRTIO_NET_ERR;
       }
 
Quoted 6 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
       /* stop the backend before changing the number of queues to avoid handling a
        * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

Quoted 8 lines

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Quoted 8 lines
Quoted 2 lines

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Can you please add several words - what is 0.9 device (probably this is more about driver) and
what is the problem with it?

Quoted 5 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

If I succeed to modify the patch such a way that it will act only in 'target' case,
i.e. only if some of queueus are not initialized (at time of driver_ok), will it be more safe?

Quoted 16 lines

Thanks

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
       virtio_net_set_status(vdev, vdev->status);
       virtio_net_set_queues(n);
 
#10 lore raw
Quoted 74 lines · Yuri Benditovich
Quoted 73 lines · Jason Wang
Quoted 70 lines · Michael S. Tsirkin
Quoted 59 lines · Yuri Benditovich
Quoted 58 lines · Michael S. Tsirkin
Quoted 46 lines · Yuri Benditovich
Quoted 40 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
    hw/net/virtio-net.c | 7 +++++--
    1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
    {
        VirtIODevice *vdev = VIRTIO_DEVICE(n);
        NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
        if (!get_vhost_net(nc->peer)) {
            return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
            return VIRTIO_NET_ERR;
        }
 
Quoted 5 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
        /* stop the backend before changing the number of queues to avoid handling a
         * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

Quoted 8 lines
Quoted 4 lines

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Quoted 7 lines
Quoted 4 lines

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Can you please add several words - what is 0.9 device (probably this is more about driver) and
what is the problem with it?

It's not a net specific issue. 0.9x device is legacy device defined in the spec. We don't have a method to disable and enable a specific queue at that time. Michael said we can assume queue address 0 as disabled, but there's still a question of how to enable it. Spec is unclear and it was too late to add thing for legacy device. For 1.0 device we have queue_enable, but its implementation is incomplete, since it can work with vhost correctly, we probably need to add thing to make it work.

Quoted 9 lines
Quoted 5 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

If I succeed to modify the patch such a way that it will act only in 'target' case,
i.e. only if some of queueus are not initialized (at time of driver_ok), will it be more safe?

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

Thanks

Quoted 14 lines
Quoted 14 lines

Thanks

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
        virtio_net_set_status(vdev, vdev->status);
        virtio_net_set_queues(n);
 
#11 lore raw
Quoted 100 lines · Jason Wang
Quoted 74 lines · Yuri Benditovich
Quoted 73 lines · Jason Wang
Quoted 70 lines · Michael S. Tsirkin
Quoted 59 lines · Yuri Benditovich
Quoted 58 lines · Michael S. Tsirkin
Quoted 46 lines · Yuri Benditovich
Quoted 40 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
    hw/net/virtio-net.c | 7 +++++--
    1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
    {
        VirtIODevice *vdev = VIRTIO_DEVICE(n);
        NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
        if (!get_vhost_net(nc->peer)) {
            return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
            return VIRTIO_NET_ERR;
        }
 
Quoted 5 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
        /* stop the backend before changing the number of queues to avoid handling a
         * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

Quoted 8 lines
Quoted 4 lines

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Quoted 7 lines
Quoted 4 lines

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Can you please add several words - what is 0.9 device (probably this is more about driver) and
what is the problem with it?

It's not a net specific issue. 0.9x device is legacy device defined in the spec. We don't have a method to disable and enable a specific queue at that time. Michael said we can assume queue address 0 as disabled, but there's still a question of how to enable it. Spec is unclear and it was too late to add thing for legacy device. For 1.0 device we have queue_enable, but its implementation is incomplete, since it can work with vhost correctly, we probably need to add thing to make it work.

Quoted 5 lines
Quoted 5 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

Quoted 9 lines
Quoted 4 lines

If I succeed to modify the patch such a way that it will act only in 'target' case,
i.e. only if some of queueus are not initialized (at time of driver_ok), will it be more safe?

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Quoted 4 lines

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Quoted 17 lines

Thanks

Quoted 14 lines
Quoted 14 lines

Thanks

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
        virtio_net_set_status(vdev, vdev->status);
        virtio_net_set_queues(n);
 
#12 lore raw
Quoted 6 lines · Yuri Benditovich
Quoted 3 lines

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue. So driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards. There're tricks to make it work like what is done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

Quoted 11 lines
Quoted 5 lines

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

Quoted 1 line
#13 lore raw
Quoted 26 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

Quoted 5 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

Quoted 28 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#14 lore raw
Quoted 52 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Quoted 6 lines

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

So you meant write to queue_enable is forbidden after DRIVER_OK (though it's not very clear to me from the  spec). And if a driver want to enable new queues, it must reset the device?

Quoted 10 lines
Quoted 6 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 7 lines
Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

If I understand correctly, this is not what is done by current windows drivers.

Thanks

Quoted 29 lines
Quoted 29 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#15 lore raw
Quoted 59 lines · Jason Wang
Quoted 57 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:
Quoted 6 lines
Quoted 1 line

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

So you meant write to queue_enable is forbidden after DRIVER_OK (though it's not very clear to me from the  spec). And if a driver want to enable new queues, it must reset the device?

That's my reading. What do you think?

Quoted 55 lines
Quoted 11 lines
Quoted 7 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 7 lines
Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

If I understand correctly, this is not what is done by current windows drivers.

Thanks

Quoted 29 lines
Quoted 29 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#16 lore raw
Quoted 63 lines · Michael S. Tsirkin
Quoted 62 lines · Jason Wang
Quoted 58 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

Quoted 6 lines
Quoted 3 lines

So you meant write to queue_enable is forbidden after DRIVER_OK (though it's not very clear to me from the  spec). And if a driver want to enable new queues, it must reset the device?

That's my reading. What do you think?

Btw some legacy drivers might violate this by addig buffers before driver_ok.

Quoted 56 lines
Quoted 56 lines
Quoted 12 lines
Quoted 8 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 7 lines
Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

If I understand correctly, this is not what is done by current windows drivers.

Thanks

Quoted 29 lines
Quoted 29 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#17 lore raw
Quoted 59 lines · Michael S. Tsirkin
Quoted 58 lines · Michael S. Tsirkin
Quoted 57 lines · Jason Wang
Quoted 56 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

So you meant write to queue_enable is forbidden after DRIVER_OK (though it's not very clear to me from the  spec). And if a driver want to enable new queues, it must reset the device?

That's my reading. What do you think?

Looks like I can infer this from the spec, maybe it's better to clarify.

Quoted 2 lines

Btw some legacy drivers might violate this by addig buffers before driver_ok.

Yes, but it's probably too late to fix them.

Thanks.

Quoted 50 lines
Quoted 50 lines
Quoted 50 lines
Quoted 9 lines
Quoted 5 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 6 lines
Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

If I understand correctly, this is not what is done by current windows drivers.

Thanks

Quoted 28 lines
Quoted 28 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#18 lore raw
Quoted 73 lines · Jason Wang
Quoted 59 lines · Michael S. Tsirkin
Quoted 58 lines · Michael S. Tsirkin
Quoted 57 lines · Jason Wang
Quoted 56 lines · Michael S. Tsirkin
Quoted 25 lines · Jason Wang

On 2019/2/21 下午4:18, Yuri Benditovich wrote:

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

Let me clarify, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET is used to control the the number of queue pairs used by device for doing transmission and reception. It was not used to enable or disable a virtqueue.

For 1.0 device, we should use queue_enable in pci cfg to enable and disable queue:

We could do:

1) allocate memory and set queue_enable for vq0

2) allocate memory and set queue_enable for vq1

Quoted 7 lines

3) Set vq paris to 1

4) allocate memory and set queue_enable for vq2

5) allocate memory and set queue_enable for vq3

6) set vq pairs to 2

I do not think spec allows this.

The driver MUST follow this sequence to initialize a device:
1. Reset the device.
2. Set the ACKNOWLEDGE status bit: the guest OS has noticed the device.
3. Set the DRIVER status bit: the guest OS knows how to drive the device.
4. Read device feature bits, and write the subset of feature bits understood by the OS and driver to the device. During this step the driver MAY read (but MUST NOT write) the device-specific configuration fields to check that it can support the device before accepting it.
5. Set the FEATURES_OK status bit. The driver MUST NOT accept new feature bits after this step.
6. Re-read device status to ensure the FEATURES_OK bit is still set: otherwise, the device does not support our subset of features and the device is unusable.
7. Perform device-specific setup, including discovery of virtqueues for the device, optional per-bus setup, reading and possibly writing the device’s virtio configuration space, and population of virtqueues.
8. Set the DRIVER_OK status bit. At this point the device is “live”.

Thus vqs are setup at step 7.

# of vq pairs are set up through a command which is a special
buffer, and spec says:
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

The driver MUST NOT send any buffer available notifications to the device before setting DRIVER_OK.

So you meant write to queue_enable is forbidden after DRIVER_OK (though it's not very clear to me from the  spec). And if a driver want to enable new queues, it must reset the device?

That's my reading. What do you think?

Looks like I can infer this from the spec, maybe it's better to clarify.

Quoted 7 lines
Quoted 2 lines

Btw some legacy drivers might violate this by addig buffers before driver_ok.

Yes, but it's probably too late to fix them.

Thanks.

Right. As a work around virtio net also checks rings when it detects driver ok. We can disable that when VIRTIO_1 has been negotiated.

Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 10 lines
Quoted 6 lines

But this requires a proper implementation for queue_enable for vhost which is missed in qemu and probably what you really want to do.

but for 0.9x device, there's no such way to do this. That's the issue.

0.9x there's no queue enable, assumption is PA!=0 means VQ has been enabled.

Quoted 6 lines
Quoted 3 lines

So
driver must allocate all queBes before starting the device, otherwise there's no way to enable it afterwards.

As per spec queues must be allocated before DRIVER_OK.

That is universal.

If I understand correctly, this is not what is done by current windows drivers.

Thanks

Quoted 28 lines
Quoted 28 lines

There're tricks to make it work like what is
done in your patch, but it depends on a specific implementation like qemu which is sub-optimal.

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Well, you just need to allocate some memory for the virtqueue, there's no need to make it visible to the rest until it was enabled.

Thanks

#19 lore raw
In reply to Yuri Benditovich
Quoted 107 lines · Yuri Benditovich
Quoted 106 lines · Jason Wang
Quoted 74 lines · Yuri Benditovich
Quoted 73 lines · Jason Wang
Quoted 70 lines · Michael S. Tsirkin
Quoted 59 lines · Yuri Benditovich
Quoted 58 lines · Michael S. Tsirkin
Quoted 46 lines · Yuri Benditovich
Quoted 40 lines · Jason Wang
Quoted 7 lines · Yuri Benditovich

https://bugzilla.redhat.com/show_bug.cgi?id=1608226
On startup/link-up in multiqueue configuration the virtio-net tries to starts all the queues, including those that the guest will not enable by VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET.
If the guest driver does not allocate queues that it will not use (for example, Windows driver does not) and number of actually used queues is less that maximal number supported by the device,

Is this a requirement of e.g NDIS? If not, could we simply allocate all queues in this case. This is usually what normal Linux driver did.

Quoted 28 lines

this causes vhost_net_start to fail and actually disables vhost for all the queues, reducing the performance.
Current commit fixes this: initially only first queue is started, upon VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET started all the queues requested by the guest.

---
    hw/net/virtio-net.c | 7 +++++--
    1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/virtio-net.c +1 -1
index 3f319ef723..d3b1ac6d3a 100644
@@ -174,7 +174,7 @@ static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
    {
        VirtIODevice *vdev = VIRTIO_DEVICE(n);
        NetClientState *nc = qemu_get_queue(n->nic);
-    int queues = n->multiqueue ? n->max_queues : 1;
+    int queues = n->multiqueue ? n->curr_queues : 1;
 
        if (!get_vhost_net(nc->peer)) {
            return;
@@ -1016,9 +1016,12 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
            return VIRTIO_NET_ERR;
        }
 
Quoted 5 lines

hw/net/virtio-net.c static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, · line 1021

Quoted 4 lines
hw/net/virtio-net.c +1 -1
-    n->curr_queues = queues;
        /* stop the backend before changing the number of queues to avoid handling a
         * disabled queue */
+    virtio_net_set_status(vdev, 0);

Any reason for doing this?

Quoted 7 lines

I think there are 2 reasons:
1. The spec does not require guest SW to allocate unused queues.
2. We spend guest's physical memory to just make vhost happy when it touches queues that it should not use.

Thanks,
Yuri Benditovich

The spec also says:
queue_enable The driver uses this to selectively prevent the device from executing requests from this virtqueue. 1 - enabled; 0 - disabled.

Quoted 8 lines
Quoted 4 lines

While this is not a conformance clause this strongly implies that queues which are not enabled are never accessed by device.

Yuri I am guessing you are not enabling these unused queues right?

Of course, we (Windows driver) do not.
The code of virtio-net passes max_queues to vhost and this causes vhost to try accessing all the queues, fail on unused ones and finally leave vhost disabled at all.

Jason, at least for 1.0 accessing disabled queues looks like a spec violation. What do you think?

Quoted 7 lines
Quoted 4 lines

Yes, but there's some issues:

- How to detect a disabled queue for 0.9x device? Looks like there's no way according to the spec, so device must assume all queues was enabled.

Can you please add several words - what is 0.9 device (probably this is more about driver) and
what is the problem with it?

It's not a net specific issue. 0.9x device is legacy device defined in the spec. We don't have a method to disable and enable a specific queue at that time. Michael said we can assume queue address 0 as disabled, but there's still a question of how to enable it. Spec is unclear and it was too late to add thing for legacy device. For 1.0 device we have queue_enable, but its implementation is incomplete, since it can work with vhost correctly, we probably need to add thing to make it work.

Quoted 9 lines
Quoted 5 lines

- For 1.0, if we depends on queue_enable, we should implement the callback for vhost I think. Otherwise it's still buggy.

So it looks tricky to enable and disable queues through set status

If I succeed to modify the patch such a way that it will act only in 'target' case,
i.e. only if some of queueus are not initialized (at time of driver_ok), will it be more safe?

Quoted 7 lines
Quoted 3 lines

For 1.0 device, we can fix the queue_enable, but for 0.9x device how do you enable one specific queue in this case? (setting status?)

Do I understand correctly that for 0.9 device in some cases the device will receive feature _MQ set, but will not receive VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET? Or the problem is different?

No. For 0.9 it is possible that device adds buffers before DRIVER_OK.

Quoted 29 lines
Quoted 4 lines

A fundamental question is what prevents you from just initialization all queues during driver start? It looks to me this save lots of efforts than allocating queue dynamically.

This is not so trivial in Windows driver, as it does not have objects for queues that it does not use. Linux driver first of all allocates all the queues and then
adds Rx/Tx to those it will use. Windows driver first decides how many queues it will use then allocates objects for them and initializes them from zero to fully functional state.

Quoted 17 lines

Thanks

Quoted 14 lines
Quoted 14 lines

Thanks

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Thanks

Quoted 6 lines
hw/net/virtio-net.c +3 -0
+
+    n->curr_queues = queues;
+
        virtio_net_set_status(vdev, vdev->status);
        virtio_net_set_queues(n);