From nobody Tue May 14 05:40:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1574763473; cv=none; d=zohomail.com; s=zohoarc; b=Z00vhRCJXKFmC8KQ1RtYgNNIWn+xRLu+SapOnkm0hG0wbRIXmyQpE8qpHjSXEmk9EaiRr3CyHR2vhBh5/o1mMdRnmEBwnh73WEmQaV5tWaqa3SdM0dcCMq9/0BNb5aUOIKEPAxsdCPuZcUYRuqX39CxWlCuzqP4vqIPlEkJWGHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574763473; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ecShvr+Zq6STOGx1l0iQ8cCvKv5lb6vZ/WjknM9nD6E=; b=QwMgBreni3zeCFQ0ikTUQtZMwQ1XwBA+MpctEuK+t8MjnHCz6ggm94M6zSHgvFeVVRu/P65ysB7GFl4sqj7M0qsiqujbTepMZ9HUcd1VCuNSuYEiTrgHrelfpNae4Rqusr79Jzd8o1Z6wKxG3kh3koUX3BBbOh77FxnMNRPVqzU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574763473982439.93345744583985; Tue, 26 Nov 2019 02:17:53 -0800 (PST) Received: from localhost ([::1]:52200 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXuq-0007pK-Bl for importer@patchew.org; Tue, 26 Nov 2019 05:17:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57439) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXsF-0005fI-0P for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:15:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iZXnS-0003hD-A7 for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:15 -0500 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]:44708) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iZXnS-0003h1-4R for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:14 -0500 Received: by mail-pf1-x444.google.com with SMTP id d199so4300163pfd.11 for ; Tue, 26 Nov 2019 02:10:14 -0800 (PST) Received: from localhost.localdomain ([222.151.198.97]) by smtp.gmail.com with ESMTPSA id h9sm12059065pgk.84.2019.11.26.02.10.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Nov 2019 02:10:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ecShvr+Zq6STOGx1l0iQ8cCvKv5lb6vZ/WjknM9nD6E=; b=d+YYczk4lJH43WHRgPgABeQW5AYRNcl7DJRbNi1c4p2WWFqkvolmM2XsQ44czI+mWp aVmIt6CW9B+7OpTNN+ps0zReed0nBfjwq9pklcNZKWKoIW7k1EQMTOHSi4d0/BCnYQee jinDCFwA6wNeZ4cBOG9cHPVSHcKoTCX0NlPdmJVevRHmMaf97cZDvXxtiWam3BPVOLr9 RFEZ0ncycDTNPn5Urku5wMT1KOZ8+LKL42IakRmapDH1VNSLE/H9DD99CM6pQ9LMRigI y0T8KiZRlGegkVpLl3zTTtrATMEXAozU32i+jHRlYa3drVFQX+N2oBh6kWr44Z3T2zcB SEaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ecShvr+Zq6STOGx1l0iQ8cCvKv5lb6vZ/WjknM9nD6E=; b=TFf6pK10DpKosKiGxAK8zr2zUVHP+uX5CVQrG7VdJMIvZOtNEp2cZ5eZuHUGCTqyrl p49kFtuYE3vw/pJoS1QNPA1FkGYIRart+L3fzxXzDgy/OLSHwYDM/QQzV0QIaAvEdLY6 1C2eymSssoJYv6AUwv6NXOi6VtiDz4t5QRA0tGsK4rKTmoPFWhaLCRnQFu71OrqXjRZD 13RHvTvVrfKFU65rmmmnGdrA4BeJTAS3uAb0oovo8AG4GkIBT1Yad3XAFsrGErcrxok1 sjehMOnMlok1nwZl9rhEPkv1K1giinvEj6I5vqyUToyUicYjKtWaSKr9Al4mEtJNIjx8 yYdg== X-Gm-Message-State: APjAAAVLZkEz3vWM+4fiu+AeDTA/GckHdJ6Wo3yJ/B/Or6nMuO0WTNca ePA8/XREUZyKZzTUbAnRCLk= X-Google-Smtp-Source: APXvYqxLnUahtpS2ezzELlW12TWhqW19OSB0jtp9PSXtwd5yFTcnaRJ86M926/WsXnNPNpvBgZKE9Q== X-Received: by 2002:a63:e4a:: with SMTP id 10mr35864959pgo.121.1574763013362; Tue, 26 Nov 2019 02:10:13 -0800 (PST) From: Prashant Bhole To: "Michael S . Tsirkin" , Jason Wang , qemu-devel@nongnu.org Subject: [RFC 1/3] configure: add libbpf support Date: Tue, 26 Nov 2019 19:09:12 +0900 Message-Id: <20191126100914.5150-2-prashantbhole.linux@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191126100914.5150-1-prashantbhole.linux@gmail.com> References: <20191126100914.5150-1-prashantbhole.linux@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::444 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Song Liu , Jakub Kicinski , Jesper Dangaard Brouer , Daniel Borkmann , netdev@vger.kernel.org, John Fastabend , Alexei Starovoitov , Martin KaFai Lau , Prashant Bhole , kvm@vger.kernel.org, Yonghong Song , Andrii Nakryiko , "David S . Miller" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This is a preparation to add libbpf support for Qemu. When it is enabled Qemu can load eBPF programs and manipulated eBPF maps libbpf APIs. When configured with --enable-libbpf, availability of libbpf is checked. If it exists then CONFIG_LIBBPF is defined and the qemu binary is linked with libbpf. Signed-off-by: Prashant Bhole --- configure | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/configure b/configure index 6099be1d84..a7e8a8450d 100755 --- a/configure +++ b/configure @@ -504,6 +504,7 @@ debug_mutex=3D"no" libpmem=3D"" default_devices=3D"yes" plugins=3D"no" +libbpf=3D"no" =20 supported_cpu=3D"no" supported_os=3D"no" @@ -1539,6 +1540,8 @@ for opt do ;; --disable-plugins) plugins=3D"no" ;; + --enable-libbpf) libbpf=3D"yes" + ;; *) echo "ERROR: unknown option $opt" echo "Try '$0 --help' for more information" @@ -1825,6 +1828,7 @@ disabled with --disable-FEATURE, default is enabled i= f available: debug-mutex mutex debugging support libpmem libpmem support xkbcommon xkbcommon support + libbpf eBPF program support =20 NOTE: The object files are built at the place where configure is launched EOF @@ -6084,6 +6088,19 @@ case "$slirp" in ;; esac =20 +########################################## +# Do we have libbpf +if test "$libbpf" !=3D "no" ; then + if $pkg_config libbpf; then + libbpf=3D"yes" + libbpf_libs=3D$($pkg_config --libs libbpf) + else + if test "$libbpf" =3D=3D "yes" ; then + feature_not_found "libbpf" "Install libbpf devel" + fi + libbpf=3D"no" + fi +fi =20 ########################################## # End of CC checks @@ -6599,6 +6616,7 @@ echo "libpmem support $libpmem" echo "libudev $libudev" echo "default devices $default_devices" echo "plugin support $plugins" +echo "XDP offload support $libbpf" =20 if test "$supported_cpu" =3D "no"; then echo @@ -7457,6 +7475,11 @@ if test "$plugins" =3D "yes" ; then fi fi =20 +if test "$libbpf" =3D "yes" ; then + echo "CONFIG_LIBBPF=3Dy" >> $config_host_mak + echo "LIBBPF_LIBS=3D$libbpf_libs" >> $config_host_mak +fi + if test "$tcg_interpreter" =3D "yes"; then QEMU_INCLUDES=3D"-iquote \$(SRC_PATH)/tcg/tci $QEMU_INCLUDES" elif test "$ARCH" =3D "sparc64" ; then --=20 2.20.1 From nobody Tue May 14 05:40:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1574763638; cv=none; d=zohomail.com; s=zohoarc; b=QWNGvxmZVKtIBRQIqOxCpVMQM8e35gZ/C9Jca8rfR85h9tNmosXgfV3XOw+v3MOaIv1KfOxTsmaZfGw+uujGpQ7RxUCT7jRkE2y3j7U20GJBkucYgNOdMW9qf/1BPlUgJWGhbnh27WDINHpFN2Za7+r3aA5x37tDRygytQoRJC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574763638; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=izpU3FP4fxkkqeoLGSB6k8qMmojNU/Dikc5DKXQ0xHU=; b=ZKOZ8LbV+gOFf7MSqSfLsFm006C4//5tFMn0G2+g+1GQvbLiEBqB+8TNDy8+TCDqT7Uzt19jpHLxza6hzG8kGeb3Jrn3IHRgTootKjfdJ6qrV0gZ5tEkLg6NK23mndn3vvDbWlRwQDw0IXx/SjTbVBTL0XDdqpa8yPwieY0ggIw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574763638006937.8676680073181; Tue, 26 Nov 2019 02:20:38 -0800 (PST) Received: from localhost ([::1]:52230 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXxT-0002qb-PA for importer@patchew.org; Tue, 26 Nov 2019 05:20:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57422) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXsE-0005e4-V2 for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:15:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iZXnW-0003iu-S3 for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:20 -0500 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]:43286) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iZXnW-0003iV-K9 for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:18 -0500 Received: by mail-pj1-x1032.google.com with SMTP id a10so8056401pju.10 for ; Tue, 26 Nov 2019 02:10:18 -0800 (PST) Received: from localhost.localdomain ([222.151.198.97]) by smtp.gmail.com with ESMTPSA id h9sm12059065pgk.84.2019.11.26.02.10.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Nov 2019 02:10:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=izpU3FP4fxkkqeoLGSB6k8qMmojNU/Dikc5DKXQ0xHU=; b=Uq/ahqOAzTEUkTwsljpwIkyrGPqH/evKDEDersVK+mnaWbVNUkIthOWiLU+OXtLUvU EOw+sFAxx99LvFjbn0xvlUFZ5J3TEpxJkoyjlF2msZFo/LOeVoj6IkxWSzwwvLF/GaO4 LV0VhZdKKH/GmYoyD3t6ep/62dQSmfaGEgWoU9yvfU7Rgusygl34frWANucKte7c05BM V5fH+YZvdKIZB7KcK8MgdI3YFmWqAtKSVQ2oidAApxjtHLUjzcKonWljChWnfE+ng/+x lioML1DyLTJ9oTbXDsRF4zQN7TE/MgG5O/maBRtCNJrC7VQNXshM+DqgYTr9+KMSBVLC NAng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=izpU3FP4fxkkqeoLGSB6k8qMmojNU/Dikc5DKXQ0xHU=; b=jsBGt8wIS6mwHMOGKwxWcjC/irkqKgISJMwd5iH/HRs5ccdJbSe4L37P6dr1ZIY1ej LEdTUuDFMQ57o/pBCvyFAYDABxBNV1K2qoBCUcZZ+KKnG/FW6UZHlpSCcMZ7Zwb7+6i3 PdKO3iHKB9BpU/tZZvG792Vd7SBLF52+hbkGf/n/J8SqeSLLlNaL4HDPxM60jRdnHqv8 5Ew6OVrKIF+3kOS/WisWI5hMqyw56GBOg2I59twpx3NbF4m1ZcIiWS6EKgN18v9f+DXA kxdkFvWist+ajK70iDZXv57hvTi/QOT8ZbHoHgM1+sBMgMLpQLdUL3LKztD/ELe1jW6k O/1A== X-Gm-Message-State: APjAAAWXX2O5rRTIGuUnoOQbx7k8J4CvrMZCYQ4niAb1c+Dv8yQEthIQ CAnzDyIL638OvLWTvs18LTg= X-Google-Smtp-Source: APXvYqytegqONiYQJkp3D/RPNSJspi2nkwoVBXcgZu7C3Ai2QAmscJb++iOInjMbvENLHy/9nfJw7g== X-Received: by 2002:a17:902:fe06:: with SMTP id g6mr16749434plj.52.1574763017372; Tue, 26 Nov 2019 02:10:17 -0800 (PST) From: Prashant Bhole To: "Michael S . Tsirkin" , Jason Wang , qemu-devel@nongnu.org Subject: [RFC 2/3] virtio-net: add support for offloading XDP program Date: Tue, 26 Nov 2019 19:09:13 +0900 Message-Id: <20191126100914.5150-3-prashantbhole.linux@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191126100914.5150-1-prashantbhole.linux@gmail.com> References: <20191126100914.5150-1-prashantbhole.linux@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1032 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Song Liu , Jakub Kicinski , Jesper Dangaard Brouer , Daniel Borkmann , netdev@vger.kernel.org, John Fastabend , Alexei Starovoitov , Martin KaFai Lau , Prashant Bhole , kvm@vger.kernel.org, Yonghong Song , Andrii Nakryiko , "David S . Miller" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Jason Wang This feature involves offloading of XDP program and ebpf map from the guest to the host. This patch takes care of offloadin of program. A handler for VIRTIO_NET_CTRL_EBPF command is added in virtio-net. The control buffer consist of struct virtio_net_ctrl_ebpf_prog and followed by an ebpf program instructions. An array of bpf_insn is prepared and passed to libbpf API bpf_load_program. The program fd is retuned by the API is then attached to tap fd using TUNSETOFFLOADEDXDP ioctl command. Signed-off-by: Jason Wang Co-developed-by: Prashant Bhole Signed-off-by: Prashant Bhole --- hw/net/virtio-net.c | 69 +++++++++++++++++++++ include/net/tap.h | 2 + include/standard-headers/linux/virtio_net.h | 27 ++++++++ net/Makefile.objs | 1 + net/tap-bsd.c | 5 ++ net/tap-linux.c | 48 ++++++++++++++ net/tap-linux.h | 1 + net/tap-solaris.c | 5 ++ net/tap-stub.c | 5 ++ net/tap.c | 7 +++ net/tap_int.h | 1 + 11 files changed, 171 insertions(+) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 97a5113f7e..7cc1bd1654 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -43,6 +43,11 @@ #include "monitor/qdev.h" #include "hw/pci/pci.h" =20 +#ifdef CONFIG_LIBBPF +#include +#include +#endif + #define VIRTIO_NET_VM_VERSION 11 =20 #define MAC_TABLE_ENTRIES 64 @@ -628,6 +633,21 @@ static int peer_attach(VirtIONet *n, int index) return tap_enable(nc->peer); } =20 +static int peer_attach_ebpf(VirtIONet *n, int len, void *insns, uint8_t gp= l) +{ + NetClientState *nc =3D qemu_get_subqueue(n->nic, 0); + + if (!nc->peer) { + return 0; + } + + if (nc->peer->info->type !=3D NET_CLIENT_DRIVER_TAP) { + return 0; + } + + return tap_attach_ebpf(nc->peer, len, insns, gpl); +} + static int peer_detach(VirtIONet *n, int index) { NetClientState *nc =3D qemu_get_subqueue(n->nic, index); @@ -991,6 +1011,53 @@ static int virtio_net_handle_offloads(VirtIONet *n, u= int8_t cmd, } } =20 +static int virtio_net_handle_ebpf_prog(VirtIONet *n, struct iovec *iov, + unsigned int iov_cnt) +{ +#ifdef CONFIG_LIBBPF + struct bpf_insn prog[4096]; + struct virtio_net_ctrl_ebpf_prog ctrl; + size_t s; + int err =3D VIRTIO_NET_ERR; + + s =3D iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); + if (s !=3D sizeof(ctrl)) { + error_report("Invalid ebpf prog control buffer"); + goto err; + } + + if (ctrl.cmd =3D=3D VIRTIO_NET_BPF_CMD_SET_OFFLOAD) { + s =3D iov_to_buf(iov, iov_cnt, sizeof(ctrl), prog, sizeof(prog)); + if (s !=3D ctrl.len) { + error_report("Invalid ebpf prog control buffer"); + goto err; + } + + err =3D peer_attach_ebpf(n, s, prog, ctrl.gpl_compatible); + if (err) { + error_report("Failed to attach XDP program"); + goto err; + } + } else if (ctrl.cmd =3D=3D VIRTIO_NET_BPF_CMD_UNSET_OFFLOAD) { + err =3D peer_attach_ebpf(n, 0, NULL, 0); + } +err: + return err ? VIRTIO_NET_ERR : VIRTIO_NET_OK; +#else + return VIRTIO_NET_ERR; +#endif +} + +static int virtio_net_handle_ebpf(VirtIONet *n, uint8_t cmd, + struct iovec *iov, unsigned int iov_cnt) +{ + if (cmd =3D=3D VIRTIO_NET_CTRL_EBPF_PROG) { + return virtio_net_handle_ebpf_prog(n, iov, iov_cnt); + } + + return VIRTIO_NET_ERR; +} + static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, struct iovec *iov, unsigned int iov_cnt) { @@ -1208,6 +1275,8 @@ static void virtio_net_handle_ctrl(VirtIODevice *vdev= , VirtQueue *vq) status =3D virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); } else if (ctrl.class =3D=3D VIRTIO_NET_CTRL_GUEST_OFFLOADS) { status =3D virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cn= t); + } else if (ctrl.class =3D=3D VIRTIO_NET_CTRL_EBPF) { + status =3D virtio_net_handle_ebpf(n, ctrl.cmd, iov, iov_cnt); } =20 s =3D iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(s= tatus)); diff --git a/include/net/tap.h b/include/net/tap.h index 5d585515f9..19c507a1c2 100644 --- a/include/net/tap.h +++ b/include/net/tap.h @@ -33,6 +33,8 @@ int tap_disable(NetClientState *nc); =20 int tap_get_fd(NetClientState *nc); =20 +int tap_attach_ebpf(NetClientState *nc, int len, void *insns, uint8_t gpl); + struct vhost_net; struct vhost_net *tap_get_vhost_net(NetClientState *nc); =20 diff --git a/include/standard-headers/linux/virtio_net.h b/include/standard= -headers/linux/virtio_net.h index 260c3681d7..83292c81bc 100644 --- a/include/standard-headers/linux/virtio_net.h +++ b/include/standard-headers/linux/virtio_net.h @@ -261,4 +261,31 @@ struct virtio_net_ctrl_mq { #define VIRTIO_NET_CTRL_GUEST_OFFLOADS 5 #define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET 0 =20 +/* + * Control XDP offloads offloads + * + * When guest wants to offload XDP program to tap device, it calls + * VIRTIO_NET_CTRL_EBPF_PROG along with VIRTIO_NET_BPF_CMD_SET_OFFLOAD + * subcommands. When offloading is successful, the tap device run offloaded + * XDP program for each packet before sending it to the guest. + * + * VIRTIO_NET_BPF_CMD_UNSET_OFFLOAD removes the the offloaded program from + * the tap device, if exists. + */ + +struct virtio_net_ctrl_ebpf_prog { + /* program length in bytes */ + __virtio32 len; + __virtio16 cmd; + __virtio16 gpl_compatible; + uint8_t insns[0]; +}; + +#define VIRTIO_NET_CTRL_EBPF 6 + #define VIRTIO_NET_CTRL_EBPF_PROG 1 + +/* Commands for VIRTIO_NET_CTRL_EBPF_PROG */ +#define VIRTIO_NET_BPF_CMD_SET_OFFLOAD 1 +#define VIRTIO_NET_BPF_CMD_UNSET_OFFLOAD 2 + #endif /* _LINUX_VIRTIO_NET_H */ diff --git a/net/Makefile.objs b/net/Makefile.objs index c5d076d19c..e7645225be 100644 --- a/net/Makefile.objs +++ b/net/Makefile.objs @@ -28,5 +28,6 @@ common-obj-$(CONFIG_POSIX) +=3D tap.o $(tap-obj-y) common-obj-$(CONFIG_WIN32) +=3D tap-win32.o =20 vde.o-libs =3D $(VDE_LIBS) +tap-linux.o-libs =3D $(LIBBPF_LIBS) =20 common-obj-$(CONFIG_CAN_BUS) +=3D can/ diff --git a/net/tap-bsd.c b/net/tap-bsd.c index a5c3707f80..e4e2a5c799 100644 --- a/net/tap-bsd.c +++ b/net/tap-bsd.c @@ -259,3 +259,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_attach_ebpf(int fd, int len, void *insns, uint8_t gpl) +{ + return -EINVAL; +} diff --git a/net/tap-linux.c b/net/tap-linux.c index e0dd442ee3..3ff806bf4f 100644 --- a/net/tap-linux.c +++ b/net/tap-linux.c @@ -31,6 +31,8 @@ =20 #include #include +#include +#include =20 #include "qapi/error.h" #include "qemu/error-report.h" @@ -314,3 +316,49 @@ int tap_fd_get_ifname(int fd, char *ifname) pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name); return 0; } + +int tap_fd_attach_ebpf(int fd, int len, void *insns, uint8_t gpl) +{ +#ifdef CONFIG_LIBBPF + struct bpf_insn *prog =3D (struct bpf_insn *)insns; + static char log_buf[65536]; + char license[16] =3D {0}; + int num_insn; + int bpf_fd; + int ret; + + if (!prog) { + bpf_fd =3D -1; + ret =3D ioctl(fd, TUNSETOFFLOADEDXDP, &bpf_fd); + if (ret) { + error_report("Failed to remove offloaded XDP: %s", strerror(er= rno)); + return -EFAULT; + } + return ret; + } + + num_insn =3D len / sizeof(prog[0]); + if (gpl) { + strncpy(license, "GPL", sizeof(license)); + } + + bpf_fd =3D bpf_load_program(BPF_PROG_TYPE_XDP, prog, num_insn, license, + 0, log_buf, sizeof(log_buf)); + if (bpf_fd < 0) { + error_report("Failed to load XDP program: %s", strerror(errno)); + error_report("ebpf verifier log: %s", log_buf); + return -EFAULT; + } + + ret =3D ioctl(fd, TUNSETOFFLOADEDXDP, &bpf_fd); + if (ret) { + error_report("Failed to set offloaded XDP: %s", strerror(errno)); + return -EFAULT; + } + close(bpf_fd); + + return ret; +#else + return -EINVAL; +#endif +} diff --git a/net/tap-linux.h b/net/tap-linux.h index 2f36d100fc..791aeaebc4 100644 --- a/net/tap-linux.h +++ b/net/tap-linux.h @@ -31,6 +31,7 @@ #define TUNSETQUEUE _IOW('T', 217, int) #define TUNSETVNETLE _IOW('T', 220, int) #define TUNSETVNETBE _IOW('T', 222, int) +#define TUNSETOFFLOADEDXDP _IOW('T', 228, int) =20 #endif =20 diff --git a/net/tap-solaris.c b/net/tap-solaris.c index 4725d2314e..38b9136b5f 100644 --- a/net/tap-solaris.c +++ b/net/tap-solaris.c @@ -254,3 +254,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_attach_ebpf(int fd, int len, void *insns, uint8_t gpl) +{ + return -EINVAL; +} diff --git a/net/tap-stub.c b/net/tap-stub.c index a9ab8f8293..5f4161b390 100644 --- a/net/tap-stub.c +++ b/net/tap-stub.c @@ -85,3 +85,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_attach_ebpf(int fd, int len, void *insns, uint8_t gpl) +{ + return -EINVAL; +} diff --git a/net/tap.c b/net/tap.c index 6207f61f84..3dba8eacb1 100644 --- a/net/tap.c +++ b/net/tap.c @@ -971,6 +971,13 @@ int tap_enable(NetClientState *nc) } } =20 +int tap_attach_ebpf(NetClientState *nc, int len, void *insns, uint8_t gpl) +{ + TAPState *s =3D DO_UPCAST(TAPState, nc, nc); + + return tap_fd_attach_ebpf(s->fd, len, insns, gpl); +} + int tap_disable(NetClientState *nc) { TAPState *s =3D DO_UPCAST(TAPState, nc, nc); diff --git a/net/tap_int.h b/net/tap_int.h index e3194b23f4..af641607e2 100644 --- a/net/tap_int.h +++ b/net/tap_int.h @@ -44,5 +44,6 @@ int tap_fd_set_vnet_be(int fd, int vnet_is_be); int tap_fd_enable(int fd); int tap_fd_disable(int fd); int tap_fd_get_ifname(int fd, char *ifname); +int tap_fd_attach_ebpf(int fd, int len, void *insns, uint8_t gpl); =20 #endif /* NET_TAP_INT_H */ --=20 2.20.1 From nobody Tue May 14 05:40:35 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1574763535; cv=none; d=zohomail.com; s=zohoarc; b=Oh7z39ecYjTA281Ni521VJzSfGVFHikdKHmWelzPUR0o5WlsHzIDsKf+RA9nwE94U/7Wbu6gXB9mUXFnKz7SCk4JukY4++v2Ah7T8gNdArs3Iv0Ol0/WUja8X8CuJ73RZePWlh90RBa2Pgb5d2IQQSUeh8RdVXTzK8WgMi7YwR0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1574763535; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=8RYu51EDNv1nof9QqLb6ITvPij8Y0e1oqvT4ZWA3MBk=; b=LBHGKrLULB2h03huszhMf3KhVXdNRIvh5AiMGPDQDiJrEusRi/09mNaQmCwutCVf25Low5VRvCDEN1otcbL8eDnzXdD4G7zPV75+D/JbmrKcue1ijH4z3NImVsHH+OyZwHDvadLnwZ36arou4TcYzzvp0BEJYs6ZxrnOC4SmXcU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1574763535587462.71941447821337; Tue, 26 Nov 2019 02:18:55 -0800 (PST) Received: from localhost ([::1]:52210 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXvq-0000bc-2L for importer@patchew.org; Tue, 26 Nov 2019 05:18:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57458) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iZXsE-0005g9-Sn for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:15:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iZXna-0003jy-9E for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:27 -0500 Received: from mail-pj1-x1043.google.com ([2607:f8b0:4864:20::1043]:40860) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iZXna-0003jo-2y for qemu-devel@nongnu.org; Tue, 26 Nov 2019 05:10:22 -0500 Received: by mail-pj1-x1043.google.com with SMTP id ep1so8070951pjb.7 for ; Tue, 26 Nov 2019 02:10:22 -0800 (PST) Received: from localhost.localdomain ([222.151.198.97]) by smtp.gmail.com with ESMTPSA id h9sm12059065pgk.84.2019.11.26.02.10.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Nov 2019 02:10:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8RYu51EDNv1nof9QqLb6ITvPij8Y0e1oqvT4ZWA3MBk=; b=nRF5Tm8jxrFWVSKwm9O8IM/CbmQqEsu5rCN9Jyr5Imv6QqQfq9VRMYS7YLJ5wUJCxh qIt9XGVC1HAjJFjMFfh2wiMDJ1RsUshx7jiDLkSxPCruk/EpIJA5uWjo33nVwjrwg7KQ iqAIss99ASYM2cGrf4dQrhfnPWjiHbnNJiSrsXNoPQPG99KrN1YdAAsafpM+Mau9iN3X g3ZrKf6tLLLyKSYyX6wrhJ2QDhD4/HkK2AbM4EASZg5bK2CrlgHNsZiMnfc8ye1r2Vti 5CmHfDMSNkWzgKkbp2IsgZJrdCp3p41Vbh3TvrO36cyVwu+Ef5NXYan++ww4AzfpXKdS H29w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8RYu51EDNv1nof9QqLb6ITvPij8Y0e1oqvT4ZWA3MBk=; b=oBWp7l1ht7s+f4IJ8Uf9Hrel56EiShbJ2Ub7A5rA30AhgGGtFCbRvMrb761gOWlJ20 oBihnB9w040b0KcG/nXfgy22DbO28CcGZf9bg0bChXP30Ahu6PUf5Mc0IkZd6+ioNHge wDc50MBYuIY61zuQDmrQvlcbM8PLl3WcKJZmglgwsmSC+KooZa3Uco+xBPNmcJ0eASbk 8MbZPpl0wj5HgIC+S6kcRs0fpPz7wPydsVA2Hbz/qJI/jn8aI7KjjWNpNEpm7jk7A96i V6TAa7uK/uACwOPWhG1xAxAtQiO4BJg8Yd4m2J/C2l/EzMKpuPBIly/5lQLWuEiQcRSr vjUA== X-Gm-Message-State: APjAAAUsZy4HsnwZYCrQKDiszmIgBF+Xv1qso4p9D/nukXEfhPH5kzZf G/NOLvarMV8150ulMXnMqZc= X-Google-Smtp-Source: APXvYqxADjKHatHur+fxT/01AG/vZgD9nSsRNGb2AyFD7GjgVAPzN1abgIppTbaJ/3338My3voR1ig== X-Received: by 2002:a17:90a:650c:: with SMTP id i12mr5948361pjj.28.1574763021275; Tue, 26 Nov 2019 02:10:21 -0800 (PST) From: Prashant Bhole To: "Michael S . Tsirkin" , Jason Wang , qemu-devel@nongnu.org Subject: [RFC 3/3] virtio-net: add support for offloading an ebpf map Date: Tue, 26 Nov 2019 19:09:14 +0900 Message-Id: <20191126100914.5150-4-prashantbhole.linux@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191126100914.5150-1-prashantbhole.linux@gmail.com> References: <20191126100914.5150-1-prashantbhole.linux@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::1043 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Song Liu , Jakub Kicinski , Jesper Dangaard Brouer , Daniel Borkmann , netdev@vger.kernel.org, John Fastabend , Alexei Starovoitov , Martin KaFai Lau , Prashant Bhole , kvm@vger.kernel.org, Yonghong Song , Andrii Nakryiko , "David S . Miller" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Jason Wang 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 Co-developed-by: Prashant Bhole Signed-off-by: Prashant Bhole --- 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) +=3D rocker/rocker.o rocker/r= ocker_fp.o \ obj-$(call lnot,$(CONFIG_ROCKER)) +=3D rocker/qmp-norocker.o =20 common-obj-$(CONFIG_CAN_BUS) +=3D can/ + +virtio-net.o-libs :=3D $(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, } } =20 +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 =3D NULL; + struct bpf_create_map_attr map_attr =3D {}; + uint8_t *key, *val; + uint32_t buf_len; + int fd, err =3D 0; + size_t s; + + s =3D iov_to_buf(iov, iov_cnt, 0, &buf_len, sizeof(buf_len)); + if (s !=3D sizeof(buf_len)) { + goto err; + } + + ctrl =3D malloc(sizeof(*ctrl) + buf_len); + if (!ctrl) { + goto err; + } + + s =3D iov_to_buf(iov, iov_cnt, 0, ctrl, sizeof(*ctrl) + buf_len); + if (s !=3D (sizeof(*ctrl) + buf_len)) { + error_report("Invalid map control buffer"); + goto err; + } + + key =3D ctrl->buf; + val =3D ctrl->buf + ctrl->key_size; + + switch (ctrl->cmd) { + case VIRTIO_NET_BPF_CMD_CREATE_MAP: + map_attr.map_type =3D ctrl->map_type; + map_attr.map_flags =3D ctrl->map_flags; + map_attr.key_size =3D ctrl->key_size; + map_attr.value_size =3D ctrl->value_size; + map_attr.max_entries =3D ctrl->max_entries; + fd =3D bpf_create_map_xattr(&map_attr); + if (fd < 0) { + goto err; + } + ctrl->map_fd =3D fd; + break; + case VIRTIO_NET_BPF_CMD_FREE_MAP: + close(ctrl->map_fd); + break; + case VIRTIO_NET_BPF_CMD_LOOKUP_ELEM: + err =3D bpf_map_lookup_elem(ctrl->map_fd, key, val); + break; + case VIRTIO_NET_BPF_CMD_GET_FIRST: + err =3D bpf_map_get_next_key(ctrl->map_fd, NULL, val); + break; + case VIRTIO_NET_BPF_CMD_GET_NEXT: + err =3D bpf_map_get_next_key(ctrl->map_fd, key, val); + break; + case VIRTIO_NET_BPF_CMD_UPDATE_ELEM: + err =3D bpf_map_update_elem(ctrl->map_fd, key, val, ctrl->flags); + break; + case VIRTIO_NET_BPF_CMD_DELETE_ELEM: + err =3D 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 =3D iov_from_buf(iov, iov_cnt, 0, ctrl, sizeof(*ctrl) + buf_len); + if (s !=3D 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 =3D=3D VIRTIO_NET_CTRL_EBPF_PROG) { return virtio_net_handle_ebpf_prog(n, iov, iov_cnt); + } else if (cmd =3D=3D VIRTIO_NET_CTRL_EBPF_MAP) { + return virtio_net_handle_ebpf_map(n, iov, iov_cnt); } =20 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]; }; =20 +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 =20 /* Commands for VIRTIO_NET_CTRL_EBPF_PROG */ #define VIRTIO_NET_BPF_CMD_SET_OFFLOAD 1 #define VIRTIO_NET_BPF_CMD_UNSET_OFFLOAD 2 =20 +/* 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 */ --=20 2.20.1