29b115
From 49e91b34b62f5da147fa2fb80d203dd675c48f64 Mon Sep 17 00:00:00 2001
29b115
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
29b115
Date: Thu, 21 Jul 2022 15:38:55 +0200
29b115
Subject: [PATCH 09/32] virtio-net: Expose ctrl virtqueue logic
29b115
MIME-Version: 1.0
29b115
Content-Type: text/plain; charset=UTF-8
29b115
Content-Transfer-Encoding: 8bit
29b115
29b115
RH-Author: Eugenio Pérez <eperezma@redhat.com>
29b115
RH-MergeRequest: 108: Net Control Virtqueue shadow Support
29b115
RH-Commit: [9/27] c4ab1e35f4ca728df82a687763c662369282c513 (eperezmartin/qemu-kvm)
29b115
RH-Bugzilla: 1939363
29b115
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
29b115
RH-Acked-by: Cindy Lu <lulu@redhat.com>
29b115
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
29b115
29b115
Bugzilla: https://bugzilla.redhat.com/1939363
29b115
29b115
Upstream Status: git://git.qemu.org/qemu.git
29b115
29b115
commit 640b8a1c588b56349b3307d88459ea1cd86181fb
29b115
Author: Eugenio Pérez <eperezma@redhat.com>
29b115
Date:   Wed Jul 20 08:59:28 2022 +0200
29b115
29b115
    virtio-net: Expose ctrl virtqueue logic
29b115
29b115
    This allows external vhost-net devices to modify the state of the
29b115
    VirtIO device model once the vhost-vdpa device has acknowledged the
29b115
    control commands.
29b115
29b115
    Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
29b115
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
29b115
    Signed-off-by: Jason Wang <jasowang@redhat.com>
29b115
29b115
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
29b115
---
29b115
 hw/net/virtio-net.c            | 84 ++++++++++++++++++++--------------
29b115
 include/hw/virtio/virtio-net.h |  4 ++
29b115
 2 files changed, 53 insertions(+), 35 deletions(-)
29b115
29b115
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
29b115
index 2a127f0a3b..59bedba681 100644
29b115
--- a/hw/net/virtio-net.c
29b115
+++ b/hw/net/virtio-net.c
29b115
@@ -1433,57 +1433,71 @@ static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
29b115
     return VIRTIO_NET_OK;
29b115
 }
29b115
 
29b115
-static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
29b115
+size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
29b115
+                                  const struct iovec *in_sg, unsigned in_num,
29b115
+                                  const struct iovec *out_sg,
29b115
+                                  unsigned out_num)
29b115
 {
29b115
     VirtIONet *n = VIRTIO_NET(vdev);
29b115
     struct virtio_net_ctrl_hdr ctrl;
29b115
     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
29b115
-    VirtQueueElement *elem;
29b115
     size_t s;
29b115
     struct iovec *iov, *iov2;
29b115
-    unsigned int iov_cnt;
29b115
+
29b115
+    if (iov_size(in_sg, in_num) < sizeof(status) ||
29b115
+        iov_size(out_sg, out_num) < sizeof(ctrl)) {
29b115
+        virtio_error(vdev, "virtio-net ctrl missing headers");
29b115
+        return 0;
29b115
+    }
29b115
+
29b115
+    iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num);
29b115
+    s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl));
29b115
+    iov_discard_front(&iov, &out_num, sizeof(ctrl));
29b115
+    if (s != sizeof(ctrl)) {
29b115
+        status = VIRTIO_NET_ERR;
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
29b115
+        status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num);
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
29b115
+        status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num);
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
29b115
+        status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num);
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
29b115
+        status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num);
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
29b115
+        status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
29b115
+    } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
29b115
+        status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
29b115
+    }
29b115
+
29b115
+    s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
29b115
+    assert(s == sizeof(status));
29b115
+
29b115
+    g_free(iov2);
29b115
+    return sizeof(status);
29b115
+}
29b115
+
29b115
+static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
29b115
+{
29b115
+    VirtQueueElement *elem;
29b115
 
29b115
     for (;;) {
29b115
+        size_t written;
29b115
         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
29b115
         if (!elem) {
29b115
             break;
29b115
         }
29b115
-        if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
29b115
-            iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
29b115
-            virtio_error(vdev, "virtio-net ctrl missing headers");
29b115
+
29b115
+        written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num,
29b115
+                                             elem->out_sg, elem->out_num);
29b115
+        if (written > 0) {
29b115
+            virtqueue_push(vq, elem, written);
29b115
+            virtio_notify(vdev, vq);
29b115
+            g_free(elem);
29b115
+        } else {
29b115
             virtqueue_detach_element(vq, elem, 0);
29b115
             g_free(elem);
29b115
             break;
29b115
         }
29b115
-
29b115
-        iov_cnt = elem->out_num;
29b115
-        iov2 = iov = g_memdup2(elem->out_sg,
29b115
-                               sizeof(struct iovec) * elem->out_num);
29b115
-        s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
29b115
-        iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
29b115
-        if (s != sizeof(ctrl)) {
29b115
-            status = VIRTIO_NET_ERR;
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
29b115
-            status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
29b115
-            status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
29b115
-            status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
29b115
-            status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
29b115
-            status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
29b115
-        } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
29b115
-            status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
29b115
-        }
29b115
-
29b115
-        s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
29b115
-        assert(s == sizeof(status));
29b115
-
29b115
-        virtqueue_push(vq, elem, sizeof(status));
29b115
-        virtio_notify(vdev, vq);
29b115
-        g_free(iov2);
29b115
-        g_free(elem);
29b115
     }
29b115
 }
29b115
 
29b115
diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h
29b115
index cce1c554f7..ef234ffe7e 100644
29b115
--- a/include/hw/virtio/virtio-net.h
29b115
+++ b/include/hw/virtio/virtio-net.h
29b115
@@ -221,6 +221,10 @@ struct VirtIONet {
29b115
     struct EBPFRSSContext ebpf_rss;
29b115
 };
29b115
 
29b115
+size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
29b115
+                                  const struct iovec *in_sg, unsigned in_num,
29b115
+                                  const struct iovec *out_sg,
29b115
+                                  unsigned out_num);
29b115
 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
29b115
                                    const char *type);
29b115
 
29b115
-- 
29b115
2.31.1
29b115