qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Prashant Bhole <prashantbhole.linux@gmail.com>
To: "Michael S . Tsirkin" <mst@redhat.com>,
	Jason Wang <jasowang@redhat.com>,
	qemu-devel@nongnu.org
Cc: Song Liu <songliubraving@fb.com>,
	Jakub Kicinski <jakub.kicinski@netronome.com>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	netdev@vger.kernel.org, John Fastabend <john.fastabend@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Martin KaFai Lau <kafai@fb.com>,
	Prashant Bhole <prashantbhole.linux@gmail.com>,
	kvm@vger.kernel.org, Yonghong Song <yhs@fb.com>,
	Andrii Nakryiko <andriin@fb.com>,
	"David S . Miller" <davem@davemloft.net>
Subject: [RFC 3/3] virtio-net: add support for offloading an ebpf map
Date: Tue, 26 Nov 2019 19:09:14 +0900	[thread overview]
Message-ID: <20191126100914.5150-4-prashantbhole.linux@gmail.com> (raw)
In-Reply-To: <20191126100914.5150-1-prashantbhole.linux@gmail.com>

From: Jason Wang <jasowang@redhat.com>

This change is a part of XDP offload feature. It handles offloading
of eBPF map from the guest.

A command handler for VIRTIO_NET_CTRL_EBPF now checks for subcommand
VIRTIO_NET_CTRL_EBPF_MAP and. The control buffer consists of struct
virtio_net_ctrl_ebpf_map followed by map key/value or key/key pair.
Map manipulation is done using libbpf APIs.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Co-developed-by: Prashant Bhole <prashantbhole.linux@gmail.com>
Signed-off-by: Prashant Bhole <prashantbhole.linux@gmail.com>
---
 hw/net/Makefile.objs                        |  2 +
 hw/net/virtio-net.c                         | 88 +++++++++++++++++++++
 include/standard-headers/linux/virtio_net.h | 23 ++++++
 3 files changed, 113 insertions(+)

diff --git a/hw/net/Makefile.objs b/hw/net/Makefile.objs
index 7907d2c199..5928497a01 100644
--- a/hw/net/Makefile.objs
+++ b/hw/net/Makefile.objs
@@ -52,3 +52,5 @@ common-obj-$(CONFIG_ROCKER) += rocker/rocker.o rocker/rocker_fp.o \
 obj-$(call lnot,$(CONFIG_ROCKER)) += rocker/qmp-norocker.o
 
 common-obj-$(CONFIG_CAN_BUS) += can/
+
+virtio-net.o-libs := $(LIBBPF_LIBS)
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 7cc1bd1654..3c49273796 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -1011,6 +1011,92 @@ static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
     }
 }
 
+static int virtio_net_handle_ebpf_map(VirtIONet *n, struct iovec *iov,
+                                      unsigned int iov_cnt)
+{
+#ifdef CONFIG_LIBBPF
+    struct virtio_net_ctrl_ebpf_map *ctrl = NULL;
+    struct bpf_create_map_attr map_attr = {};
+    uint8_t *key, *val;
+    uint32_t buf_len;
+    int fd, err = 0;
+    size_t s;
+
+    s = iov_to_buf(iov, iov_cnt, 0, &buf_len, sizeof(buf_len));
+    if (s != sizeof(buf_len)) {
+        goto err;
+    }
+
+    ctrl = malloc(sizeof(*ctrl) + buf_len);
+    if (!ctrl) {
+        goto err;
+    }
+
+    s = iov_to_buf(iov, iov_cnt, 0, ctrl, sizeof(*ctrl) + buf_len);
+    if (s != (sizeof(*ctrl) + buf_len)) {
+        error_report("Invalid map control buffer");
+        goto err;
+    }
+
+    key = ctrl->buf;
+    val = ctrl->buf + ctrl->key_size;
+
+    switch (ctrl->cmd) {
+    case VIRTIO_NET_BPF_CMD_CREATE_MAP:
+        map_attr.map_type = ctrl->map_type;
+        map_attr.map_flags = ctrl->map_flags;
+        map_attr.key_size = ctrl->key_size;
+        map_attr.value_size = ctrl->value_size;
+        map_attr.max_entries = ctrl->max_entries;
+        fd = bpf_create_map_xattr(&map_attr);
+        if (fd < 0) {
+            goto err;
+        }
+        ctrl->map_fd = fd;
+        break;
+    case VIRTIO_NET_BPF_CMD_FREE_MAP:
+        close(ctrl->map_fd);
+        break;
+    case VIRTIO_NET_BPF_CMD_LOOKUP_ELEM:
+        err = bpf_map_lookup_elem(ctrl->map_fd, key, val);
+        break;
+    case VIRTIO_NET_BPF_CMD_GET_FIRST:
+        err = bpf_map_get_next_key(ctrl->map_fd, NULL, val);
+        break;
+    case VIRTIO_NET_BPF_CMD_GET_NEXT:
+        err = bpf_map_get_next_key(ctrl->map_fd, key, val);
+        break;
+    case VIRTIO_NET_BPF_CMD_UPDATE_ELEM:
+        err = bpf_map_update_elem(ctrl->map_fd, key, val, ctrl->flags);
+        break;
+    case VIRTIO_NET_BPF_CMD_DELETE_ELEM:
+        err = bpf_map_delete_elem(ctrl->map_fd, key);
+    default:
+        error_report("map operation not implemented %d", ctrl->cmd);
+        goto err;
+    }
+
+    if (err) {
+        goto err;
+    }
+
+    s = iov_from_buf(iov, iov_cnt, 0, ctrl, sizeof(*ctrl) + buf_len);
+    if (s != sizeof(*ctrl) + buf_len) {
+        error_report("failed to write map operation result");
+        goto err;
+    }
+
+    free(ctrl);
+    return VIRTIO_NET_OK;
+
+err:
+    if (ctrl) {
+        free(ctrl);
+    }
+#endif
+    return VIRTIO_NET_ERR;
+}
+
 static int virtio_net_handle_ebpf_prog(VirtIONet *n, struct iovec *iov,
                                        unsigned int iov_cnt)
 {
@@ -1053,6 +1139,8 @@ static int virtio_net_handle_ebpf(VirtIONet *n, uint8_t cmd,
 {
     if (cmd == VIRTIO_NET_CTRL_EBPF_PROG) {
         return virtio_net_handle_ebpf_prog(n, iov, iov_cnt);
+    } else if (cmd == VIRTIO_NET_CTRL_EBPF_MAP) {
+        return virtio_net_handle_ebpf_map(n, iov, iov_cnt);
     }
 
     return VIRTIO_NET_ERR;
diff --git a/include/standard-headers/linux/virtio_net.h b/include/standard-headers/linux/virtio_net.h
index 83292c81bc..cca234e0e8 100644
--- a/include/standard-headers/linux/virtio_net.h
+++ b/include/standard-headers/linux/virtio_net.h
@@ -281,11 +281,34 @@ struct virtio_net_ctrl_ebpf_prog {
 	uint8_t insns[0];
 };
 
+struct virtio_net_ctrl_ebpf_map {
+	__virtio32 buf_len;
+	__virtio32 cmd;
+	__virtio32 map_type;
+	__virtio32 key_size;
+	__virtio32 value_size;
+	__virtio32 max_entries;
+	__virtio32 map_flags;
+	__virtio32 map_fd;
+	__virtio64 flags;
+	uint8_t buf[0];
+};
+
 #define VIRTIO_NET_CTRL_EBPF 	6
  #define VIRTIO_NET_CTRL_EBPF_PROG 1
+ #define VIRTIO_NET_CTRL_EBPF_MAP 2
 
 /* Commands for VIRTIO_NET_CTRL_EBPF_PROG */
 #define VIRTIO_NET_BPF_CMD_SET_OFFLOAD 1
 #define VIRTIO_NET_BPF_CMD_UNSET_OFFLOAD 2
 
+/* Commands for VIRTIO_NET_CTRL_EBPF_MAP */
+#define VIRTIO_NET_BPF_CMD_CREATE_MAP 1
+#define VIRTIO_NET_BPF_CMD_FREE_MAP 2
+#define VIRTIO_NET_BPF_CMD_UPDATE_ELEM 3
+#define VIRTIO_NET_BPF_CMD_LOOKUP_ELEM 4
+#define VIRTIO_NET_BPF_CMD_DELETE_ELEM 5
+#define VIRTIO_NET_BPF_CMD_GET_FIRST 6
+#define VIRTIO_NET_BPF_CMD_GET_NEXT 7
+
 #endif /* _LINUX_VIRTIO_NET_H */
-- 
2.20.1



  parent reply	other threads:[~2019-11-26 10:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-26 10:09 [RFC 0/3] Qemu: virtio-net XDP offload Prashant Bhole
2019-11-26 10:09 ` [RFC 1/3] configure: add libbpf support Prashant Bhole
2019-11-26 10:09 ` [RFC 2/3] virtio-net: add support for offloading XDP program Prashant Bhole
2019-11-26 10:09 ` Prashant Bhole [this message]
2019-11-26 10:33 ` [RFC 0/3] Qemu: virtio-net XDP offload no-reply
2019-11-27  1:27   ` Prashant Bhole
2019-11-26 10:36 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191126100914.5150-4-prashantbhole.linux@gmail.com \
    --to=prashantbhole.linux@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=hawk@kernel.org \
    --cc=jakub.kicinski@netronome.com \
    --cc=jasowang@redhat.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).