thebeanogamer / rpms / qemu-kvm

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