From patchwork Fri Jun 17 07:36:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885229 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3D6AAC433EF for ; Fri, 17 Jun 2022 07:54:14 +0000 (UTC) Received: from localhost ([::1]:50626 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26o1-0007ta-1H for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:54:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26l8-00056e-TJ for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:15 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28791 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26l5-00074u-Md for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452271; x=1686988271; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PVPHvP/TAkXQitDanIQ6TmgG/MRsZgnG2oQCkqbIFzg=; b=OisuWcHtp9QEgkzJdR6VP3NleHZ7h5/UxtzuSLPuI/55/zHtYkNJm0iO sfpICEehLHo8MwAYk53c3Okpi/zCbjSqnXyhJenwtHxuH+tnwx11aoFba uSVaeiM1nCjve0wwSTa/H5Xwt1B66dvemeBSuN/+/3gh83fAgaq+QfWcI jKHQYdk2Af5G9419NXu3AMxA2mqY6hKNaaiWGJxNHrZ+99sdIDTxfLuMD K90mQ3zYAfBlhlji3gJ+OM/0CkMhf5LUnUxM+pcNcpgj1ZJitSjzfleZc JphdWve+fSTsn4Ftq/uOuspQ+IuTvzJdvDjW0RDavl4tM41Gvdr/eHabJ w==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102100" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102100" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:06 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936626" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:04 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 01/12] configure: Add iovisor/ubpf project as a submodule for QEMU Date: Fri, 17 Jun 2022 15:36:19 +0800 Message-Id: <20220617073630.535914-2-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Make iovisor/ubpf project be a git submodule for QEMU. It will auto clone ubpf project when configure QEMU. Signed-off-by: Zhang Chen --- .gitmodules | 3 +++ configure | 20 ++++++++++++++++++++ ubpf | 1 + 3 files changed, 24 insertions(+) create mode 160000 ubpf diff --git a/.gitmodules b/.gitmodules index b8bff47df8..30fb082f39 100644 --- a/.gitmodules +++ b/.gitmodules @@ -64,3 +64,6 @@ [submodule "tests/lcitool/libvirt-ci"] path = tests/lcitool/libvirt-ci url = https://gitlab.com/libvirt/libvirt-ci.git +[submodule "ubpf"] + path = ubpf + url = https://github.com/iovisor/ubpf.git diff --git a/configure b/configure index e69537c756..7dde1429dc 100755 --- a/configure +++ b/configure @@ -326,6 +326,7 @@ else slirp="auto" fi fdt="auto" +ubpf="auto" # 2. Automatically enable/disable other options tcg="enabled" @@ -820,6 +821,14 @@ for opt do ;; --enable-slirp=*) slirp="$optarg" ;; + --disable-ubpf) ubpf="disabled" + ;; + --enable-ubpf) ubpf="enabled" + ;; + --enable-ubpf=git) ubpf="internal" + ;; + --enable-ubpf=*) ubpf="$optarg" + ;; --disable-tcg) tcg="disabled" plugins="no" ;; @@ -2176,6 +2185,16 @@ if test "$have_ubsan" = "yes"; then QEMU_LDFLAGS="-fsanitize=undefined $QEMU_LDFLAGS" fi +########################################## +# check for ubpf + +case "$ubpf" in + auto | enabled | internal) + # Simpler to always update submodule, even if not needed. + git_submodules="${git_submodules} ubpf" + ;; +esac + ########################################## # Exclude --warn-common with TSan to suppress warnings from the TSan libraries. @@ -2664,6 +2683,7 @@ if test "$skip_meson" = no; then # QEMU options test "$cfi" != false && meson_option_add "-Dcfi=$cfi" test "$fdt" != auto && meson_option_add "-Dfdt=$fdt" + test "$ubpf" != auto && meson_option_add "-Dubpf=$ubpf" test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE" test "$qemu_suffix" != qemu && meson_option_add "-Dqemu_suffix=$qemu_suffix" test "$slirp" != auto && meson_option_add "-Dslirp=$slirp" diff --git a/ubpf b/ubpf new file mode 160000 index 0000000000..0dd334daf4 --- /dev/null +++ b/ubpf @@ -0,0 +1 @@ +Subproject commit 0dd334daf4849137fa40d2b7676d2bf920d5c81d From patchwork Fri Jun 17 07:36:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885230 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A5DAFC43334 for ; Fri, 17 Jun 2022 07:54:19 +0000 (UTC) Received: from localhost ([::1]:51052 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26o6-0008A9-IA for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:54:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39336) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26l9-00056f-2Y for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:15 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26l5-000779-NF for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452271; x=1686988271; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JVcVA2OckO5nc/tQNrzCeTGJe2wl+Hute7/BhRzsybY=; b=mPdmMETtoG361485L27LApaZAu/pnlgdlqam7zGV1uDEXTt1UHO3xu6G f86LIImLT/tkUJyc64QJM7dtFz4hT6/9Vt5GMkuhmLqFN2zIcBvsh8Shz Zd39P0XlSZymW2kt4OM9+gNltJhClakzJRjB7OVxj1GTek3jQiqnZx6z8 e8rO7bcTESLoJgBa5oPzxcUpXa27gmoXpRqY+V+Ro89tRH71yoF1Y2WFE KxkFOdjE9fM9BQ6BR6o6DzEtwIBmGxDK+aKkIhAGSLouSucYvdAVhGA5v sSnZL01jta+eKBdvMKi2NJbzO4kGR7euVmPIgIZgPfCQ+vmGMTRGCo/NC Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102114" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102114" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:10 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936646" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:07 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 02/12] meson: Add ubpf build config and misc Date: Fri, 17 Jun 2022 15:36:20 +0800 Message-Id: <20220617073630.535914-3-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Make meson to build iovisor/ubpf code in Qemu. Signed-off-by: Zhang Chen --- meson.build | 47 +++++++++++++++++++++++++++++ meson_options.txt | 3 ++ scripts/coverity-scan/COMPONENTS.md | 3 ++ scripts/meson-buildoptions.sh | 5 +++ 4 files changed, 58 insertions(+) diff --git a/meson.build b/meson.build index 21cd949082..f370c1aba7 100644 --- a/meson.build +++ b/meson.build @@ -2717,9 +2717,53 @@ if not fdt.found() and fdt_required.length() > 0 error('fdt not available but required by targets ' + ', '.join(fdt_required)) endif +ubpf = not_found +ubpf_opt = 'disabled' +if have_system + ubpf_opt = get_option('ubpf') + if ubpf_opt in ['enabled', 'auto', 'system'] + have_internal = fs.exists(meson.current_source_dir() / 'ubpf/vm/Makefile') + ubpf = dependency('ubpf', kwargs: static_kwargs, + method: 'pkg-config', + required: ubpf_opt == 'system' or + ubpf_opt == 'enabled' and not have_internal) + if ubpf.found() + ubpf_opt = 'system' + elif have_internal + ubpf_opt = 'internal' + else + ubpf_opt = 'disabled' + endif + endif + if ubpf_opt == 'internal' + ubpf_data = configuration_data() + + ubpf_files = files( + 'ubpf/vm/ubpf_jit_x86_64.c', + 'ubpf/vm/ubpf_vm.c', + 'ubpf/vm/ubpf_loader.c', + ) + + ubpf_cargs = [ + '-Wno-error', '-w', + '-include', 'ubpf-defs.h' + ] + + configure_file(output: 'ubpf-defs.h', configuration: ubpf_data) + ubpf_inc = include_directories('ubpf/vm', 'ubpf/vm/inc') + libubpf = static_library('ubpf', + sources: ubpf_files, + c_args: ubpf_cargs, + include_directories: ubpf_inc) + ubpf = declare_dependency(link_with: libubpf, + include_directories: ubpf_inc) + endif +endif + config_host_data.set('CONFIG_CAPSTONE', capstone.found()) config_host_data.set('CONFIG_FDT', fdt.found()) config_host_data.set('CONFIG_SLIRP', slirp.found()) +config_host_data.set('CONFIG_UBPF', ubpf.found()) ##################### # Generated sources # @@ -3046,6 +3090,8 @@ subdir('softmmu') common_ss.add(capstone) specific_ss.add(files('cpu.c', 'disas.c', 'gdbstub.c'), capstone) +common_ss.add(ubpf) + # Work around a gcc bug/misfeature wherein constant propagation looks # through an alias: # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99696 @@ -3911,6 +3957,7 @@ summary_info += {'libudev': libudev} # Dummy dependency, keep .found() summary_info += {'FUSE lseek': fuse_lseek.found()} summary_info += {'selinux': selinux} +summary_info += {'ubpf support': ubpf_opt == 'internal' ? ubpf_opt : ubpf} summary(summary_info, bool_yn: true, section: 'Dependencies') if not supported_cpus.contains(cpu) diff --git a/meson_options.txt b/meson_options.txt index 2de94af037..1eb9164857 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -262,6 +262,9 @@ option('slirp', type: 'combo', value: 'auto', option('fdt', type: 'combo', value: 'auto', choices: ['disabled', 'enabled', 'auto', 'system', 'internal'], description: 'Whether and how to find the libfdt library') +option('ubpf', type: 'combo', value: 'auto', + choices: ['disabled', 'enabled', 'auto', 'system', 'internal'], + description: 'Whether and how to find the ubpf library') option('selinux', type: 'feature', value: 'auto', description: 'SELinux support in qemu-nbd') diff --git a/scripts/coverity-scan/COMPONENTS.md b/scripts/coverity-scan/COMPONENTS.md index 183f26a32c..dd28116674 100644 --- a/scripts/coverity-scan/COMPONENTS.md +++ b/scripts/coverity-scan/COMPONENTS.md @@ -72,6 +72,9 @@ char capstone ~ (/qemu)?(/capstone/.*) +ubpf + ~ (/qemu)?(/ubpf/vm/.*) + crypto ~ (/qemu)?((/include)?/crypto/.*|/hw/.*/crypto.*) diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 00ea4d8cd1..044dde1cff 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -37,6 +37,8 @@ meson_options_help() { printf "%s\n" ' getrandom()' printf "%s\n" ' --enable-slirp[=CHOICE] Whether and how to find the slirp library' printf "%s\n" ' (choices: auto/disabled/enabled/internal/system)' + printf "%s\n" ' --enable-ubpf[=CHOICE] Whether and how to find the ubpf library' + printf "%s\n" ' (choices: auto/disabled/enabled/internal/system)' printf "%s\n" ' --enable-strip Strip targets on install' printf "%s\n" ' --enable-tcg-interpreter TCG with bytecode interpreter (slow)' printf "%s\n" ' --enable-trace-backends=CHOICES' @@ -379,6 +381,9 @@ _meson_option_parse() { --enable-slirp=*) quote_sh "-Dslirp=$2" ;; --enable-slirp-smbd) printf "%s" -Dslirp_smbd=enabled ;; --disable-slirp-smbd) printf "%s" -Dslirp_smbd=disabled ;; + --enable-ubpf) printf "%s" -Dubpf=enabled ;; + --disable-ubpf) printf "%s" -Dubpf=disabled ;; + --enable-ubpf=*) quote_sh "-Dubpf=$2" ;; --enable-smartcard) printf "%s" -Dsmartcard=enabled ;; --disable-smartcard) printf "%s" -Dsmartcard=disabled ;; --enable-snappy) printf "%s" -Dsnappy=enabled ;; From patchwork Fri Jun 17 07:36:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885228 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D6E0CC433EF for ; Fri, 17 Jun 2022 07:54:10 +0000 (UTC) Received: from localhost ([::1]:50460 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26nx-0007m2-AB for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:54:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lB-000575-Hg for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:17 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28791 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26l9-00074u-6l for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452275; x=1686988275; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=whdQNEMqdbB7synrgxp+I8MwnlGsyMBCOizN8yk/qDo=; b=HWhZq7vsVbjRs/DNOFuN5lRdS+XwdkYL769pEi9B58MHfF5/raMWxlr5 XUVQb1VK+1iKVZqJmmWrBRBcy28Caq3mWx59XQ6VPhXR/QMm6CExQO6Vj xDyfw8NQ8W6NArvqAOxMqcBV8zAOfnnebmvY2SgOJoxIr4CosoYHqr6qo aw8MdM+mzjKlCZjMVGF9bK3ontaNWiHefGs2suK7Z2AyWO7SfMVHoc5kV VXeXTxfR+qYCjVYCk42VsyHTNulYcKz8myGQVC85FsH7GI3tmRtxsgAhL JlLXuFVndHlIvDCXW8vkZyi66CHA63vUzYm2Qx3ioF6t5Q5HhCXEDIqI5 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102125" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102125" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:12 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936669" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:10 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 03/12] ebpf/uBPF: Introduce userspace ebpf data structure Date: Fri, 17 Jun 2022 15:36:21 +0800 Message-Id: <20220617073630.535914-4-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Add ebpf/ubpf.h for the UbpfState. Signed-off-by: Zhang Chen --- ebpf/ubpf.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ebpf/ubpf.h diff --git a/ebpf/ubpf.h b/ebpf/ubpf.h new file mode 100644 index 0000000000..2562fff503 --- /dev/null +++ b/ebpf/ubpf.h @@ -0,0 +1,37 @@ +/* + * QEMU Userspace eBPF Header + * + * Copyright(C) 2022 Intel Corporation. + * + * Author: + * Zhang Chen + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#ifndef QEMU_UBPF_H +#define QEMU_UBPF_H + +#include +#include +#include + +#define MAX_LEN (1024 * 1024) + +typedef struct UbpfState { + bool jit; + char *code_path; + void *code; + size_t code_len; + char *target_path; + void *target; + size_t target_len; + struct ubpf_vm *vm; + ubpf_jit_fn fn; + int type; + char *func; +} UbpfState; + +#endif /* QEMU_UBPF_H */ From patchwork Fri Jun 17 07:36:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885232 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 81068C433EF for ; Fri, 17 Jun 2022 07:58:33 +0000 (UTC) Received: from localhost ([::1]:59084 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26sC-0005Be-Fn for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:58:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39386) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lD-00057i-51 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:19 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lA-000779-L3 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452276; x=1686988276; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=itrH3lwJEdenqHNk/rzk/jU1d9vLWcacw9Xlg0BpOgI=; b=h+XJc/VzrCw0A9dK2372tWxKznQHiCGNPjohTSECuhnGATMXJ2voE/k3 aWnflP2IQkHIj/fvIRh47mCPHvOzxAFhDc/C7jHcgFlVDOP8/CWquKJD/ 90Ha9/h2C1RYHk8Ak5r2kZTNtSSL+Q8eNbhAOraXKwILnX0kFe/TVrxKT qRaUF9btKMGQ6hwH8W1FXufNm4Fnpg8w413VSXAjZyW/Pejw2QiO1q5mR S/8sc3miti5QyMnkQPx4PMPc+dCFeOVbkw6n4WZdRC5fdriseQ3S3+F98 YYmkyDd7SpmL5jQDNtltjeR2r+JniBHgstA6dgMYMCibX29MqfpRlSy6G w==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102138" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102138" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:15 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936688" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:13 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 04/12] ebpf/uBPF: Introduce ubpf initialize functions Date: Fri, 17 Jun 2022 15:36:22 +0800 Message-Id: <20220617073630.535914-5-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Introduce ubpf.c/ubpf-stub.c with basic read and init_jit functions. Add ubpf related .c files to meson.build. Signed-off-by: Zhang Chen --- ebpf/meson.build | 1 + ebpf/ubpf-stub.c | 24 +++++++++++ ebpf/ubpf.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ ebpf/ubpf.h | 4 ++ 4 files changed, 130 insertions(+) create mode 100644 ebpf/ubpf-stub.c create mode 100644 ebpf/ubpf.c diff --git a/ebpf/meson.build b/ebpf/meson.build index 2dd0fd8948..f4457fbd28 100644 --- a/ebpf/meson.build +++ b/ebpf/meson.build @@ -1 +1,2 @@ softmmu_ss.add(when: libbpf, if_true: files('ebpf_rss.c'), if_false: files('ebpf_rss-stub.c')) +softmmu_ss.add(when: ubpf, if_true: files('ubpf.c'), if_false: files('ubpf-stub.c')) diff --git a/ebpf/ubpf-stub.c b/ebpf/ubpf-stub.c new file mode 100644 index 0000000000..2e8bf15b91 --- /dev/null +++ b/ebpf/ubpf-stub.c @@ -0,0 +1,24 @@ +/* + * QEMU Userspace eBPF Stub File + * + * Copyright(C) 2022 Intel Corporation. + * + * Author: + * Zhang Chen + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path) +{ + return 0; +} + +bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path) +{ + return 0; +} + +void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit) {} diff --git a/ebpf/ubpf.c b/ebpf/ubpf.c new file mode 100644 index 0000000000..38a6530903 --- /dev/null +++ b/ebpf/ubpf.c @@ -0,0 +1,101 @@ +/* + * QEMU Userspace eBPF Support + * + * Copyright(C) 2022 Intel Corporation. + * + * Author: + * Zhang Chen + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qemu/error-report.h" +#include "ebpf/ubpf.h" + +static void *qemu_ubpf_read(const char *path, size_t maxlen, size_t *len) +{ + FILE *file; + size_t offset = 0, rv; + void *data; + + if (!strcmp(path, "-")) { + file = fdopen(STDIN_FILENO, "r"); + } else { + file = fopen(path, "r"); + } + + if (file == NULL) { + error_report("Failed to open %s: %s", path, strerror(errno)); + return NULL; + } + + data = g_malloc0(maxlen); + + while ((rv = fread(data + offset, 1, maxlen - offset, file)) > 0) { + offset += rv; + } + + if (ferror(file)) { + error_report("Failed to read %s: %s", path, strerror(errno)); + goto err; + } + + if (!feof(file)) { + error_report("Failed to read %s because it is too large" + " (max %u bytes)", path, (unsigned)maxlen); + goto err; + } + + fclose(file); + if (len) { + *len = offset; + } + return data; + +err: + fclose(file); + free(data); + return false; +} + +/* Read Userspace eBPF binary file to QEMU */ +bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path) +{ + if (!path) { + return false; + } + u_ebpf->code_path = path; + + u_ebpf->code = qemu_ubpf_read(u_ebpf->code_path, MAX_LEN, + &u_ebpf->code_len); + if (u_ebpf->code) { + return true; + } else { + return false; + } +} + +/* Read Userspace eBPF target */ +bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path) +{ + if (!path) { + return false; + } + u_ebpf->target_path = path; + + u_ebpf->target = qemu_ubpf_read(u_ebpf->target_path, MAX_LEN, + &u_ebpf->target_len); + if (u_ebpf->target) { + return true; + } else { + return false; + } +} + +void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit) +{ + u_ebpf->jit = jit; +} diff --git a/ebpf/ubpf.h b/ebpf/ubpf.h index 2562fff503..808c02565c 100644 --- a/ebpf/ubpf.h +++ b/ebpf/ubpf.h @@ -34,4 +34,8 @@ typedef struct UbpfState { char *func; } UbpfState; +bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path); +bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path); +void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit); + #endif /* QEMU_UBPF_H */ From patchwork Fri Jun 17 07:36:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885245 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C9291C43334 for ; Fri, 17 Jun 2022 08:05:40 +0000 (UTC) Received: from localhost ([::1]:38460 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26z5-0002KT-Bl for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 04:05:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39418) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lF-00059X-IN for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:21 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lD-000779-Hj for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452279; x=1686988279; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=urlV7c37WJynsUdaqFjV/mT7ZEVNkHueLzfaaSMF5sM=; b=NYPYphGRjgs0lBtf+upaDKQfmj6gdissxHWsXACZ0XT+Zd7tC1ifq4T4 kRRPiRr94VN4YaN7aXfcm8vpXm4IVT1Jrp2hdBMzLGn4ShW94NnOu/1Sq 7UmQIUhknCL/TtOcNNqkn01uu6OMzhsLWE2cupR8npkyxiqQq/cVULz4j xE/MjlecxioyDZmiMNFb/dHOol4m8BwuIzHOI8YZjKRbLzkyW5GFvxEAX OqZ/Aux/eYDw9jBFgLmsOG991RJ6K8E7G6uOQnlxc1c4iGQw9h605oJYK D2Yme1o+yVICRLvlHV+/fah9uaYddSEDpr2tIDRbZyym5kFGLSCISVduy g==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102147" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102147" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:18 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936707" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:16 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 05/12] ebpf/uBPF: Add qemu_prepare_ubpf to load ebpf binary Date: Fri, 17 Jun 2022 15:36:23 +0800 Message-Id: <20220617073630.535914-6-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The qemu_prepare_ubpf() can load user defined userspace ebpf binary file to Qemu userspace ebpf VM but not run it. The ebpf program will triggered in the hook point. Signed-off-by: Zhang Chen --- ebpf/ubpf-stub.c | 5 +++ ebpf/ubpf.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++ ebpf/ubpf.h | 1 + 3 files changed, 106 insertions(+) diff --git a/ebpf/ubpf-stub.c b/ebpf/ubpf-stub.c index 2e8bf15b91..885bd954b7 100644 --- a/ebpf/ubpf-stub.c +++ b/ebpf/ubpf-stub.c @@ -22,3 +22,8 @@ bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path) } void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit) {} + +int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path) +{ + return 0; +} diff --git a/ebpf/ubpf.c b/ebpf/ubpf.c index 38a6530903..d65fffeda3 100644 --- a/ebpf/ubpf.c +++ b/ebpf/ubpf.c @@ -99,3 +99,103 @@ void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit) { u_ebpf->jit = jit; } + +static uint64_t gather_bytes(uint8_t a, uint8_t b, uint8_t c, + uint8_t d, uint8_t e) +{ + return ((uint64_t)a << 32) | + ((uint32_t)b << 24) | + ((uint32_t)c << 16) | + ((uint16_t)d << 8) | + e; +} + +static void trash_registers(void) +{ + /* Overwrite all caller-save registers */ + asm( + "mov $0xf0, %rax;" + "mov $0xf1, %rcx;" + "mov $0xf2, %rdx;" + "mov $0xf3, %rsi;" + "mov $0xf4, %rdi;" + "mov $0xf5, %r8;" + "mov $0xf6, %r9;" + "mov $0xf7, %r10;" + "mov $0xf8, %r11;" + ); +} + +static uint32_t sqrti(uint32_t x) +{ + return sqrt(x); +} + +static uint64_t unwind(uint64_t i) +{ + return i; +} + +static void register_functions(struct ubpf_vm *vm) +{ + ubpf_register(vm, 0, "gather_bytes", gather_bytes); + ubpf_register(vm, 1, "memfrob", memfrob); + ubpf_register(vm, 2, "trash_registers", trash_registers); + ubpf_register(vm, 3, "sqrti", sqrti); + ubpf_register(vm, 4, "strcmp_ext", strcmp); + ubpf_register(vm, 5, "unwind", unwind); + ubpf_set_unwind_function_index(vm, 5); +} + +int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path) +{ + bool is_elf; + char *errmsg; + int ret; + + if (!qemu_ubpf_read_code(u_ebpf, code_path)) { + error_report("Ubpf failed to read code"); + return -1; + } + + u_ebpf->vm = ubpf_create(); + if (!u_ebpf->vm) { + error_report("Failed to create ubpf VM"); + return -1; + } + + register_functions(u_ebpf->vm); + + /* + * The ELF magic corresponds to an RSH instruction with an offset, + * which is invalid. + */ + is_elf = u_ebpf->code_len >= SELFMAG && !memcmp(u_ebpf->code, + ELFMAG, SELFMAG); + + if (is_elf) { + ret = ubpf_load_elf(u_ebpf->vm, u_ebpf->code, + u_ebpf->code_len, &errmsg); + } else { + ret = ubpf_load(u_ebpf->vm, u_ebpf->code, + u_ebpf->code_len, &errmsg); + } + + if (ret < 0) { + error_report("Failed to load ubpf code: %s ", errmsg); + free(errmsg); + ubpf_destroy(u_ebpf->vm); + return -1; + } + + if (u_ebpf->jit) { + u_ebpf->fn = ubpf_compile(u_ebpf->vm, &errmsg); + if (u_ebpf->fn == NULL) { + error_report("Failed to ubpf compile: %s", errmsg); + free(errmsg); + return -1; + } + } + + return 0; +} diff --git a/ebpf/ubpf.h b/ebpf/ubpf.h index 808c02565c..9a35efbeb6 100644 --- a/ebpf/ubpf.h +++ b/ebpf/ubpf.h @@ -37,5 +37,6 @@ typedef struct UbpfState { bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path); bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path); void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit); +int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path); #endif /* QEMU_UBPF_H */ From patchwork Fri Jun 17 07:36:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885234 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E39C5C433EF for ; Fri, 17 Jun 2022 07:58:59 +0000 (UTC) Received: from localhost ([::1]:59702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26sc-0005c8-Vl for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:58:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39438) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lI-0005EB-9o for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:24 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lG-000779-LG for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452282; x=1686988282; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MfsDDohzwVJoLUtP3NNLreffjaePujHfV6sS1Be7igQ=; b=ArmUmNj/6jC+7ETTDQ4P1QCsB3xFyKnEDUpnEQJ9Gy5uKwB5hEyxCI7b 8hTPCUxD1Zjjzc71nltdsPEYexxHeEkO9gYvDwOh0j+G+4Mb8+oWZmkZY BFLh5enm84jWwwlcjyhM8bfza3ASzlKic9/1dCOLMX/H3P6R/T0ChJBtN osLpmnGxturqL7UCOMOkfZY2A1gWVjnIFJ/ziV0FQ+pVmvXZEx8GQJTQo OqgJ4mfgfwH81/ckBa9/+TOuT8K4R5rvB4WxEtxsait1/manmMRgfWLhB ySeMiI0U4v7nEei4SsYtELmErcLUG6H2vizYr6bHU0Kt2qDkZhxl9CJck A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102155" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102155" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:21 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936736" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:19 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 06/12] ebpf/uBPF: Add qemu_ubpf_run_once excute real ebpf program Date: Fri, 17 Jun 2022 15:36:24 +0800 Message-Id: <20220617073630.535914-7-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Before running this function, we need to ensure that the userspace ebpf program has been loaded correctly. Signed-off-by: Zhang Chen --- ebpf/ubpf-stub.c | 6 ++++++ ebpf/ubpf.c | 16 ++++++++++++++++ ebpf/ubpf.h | 2 ++ 3 files changed, 24 insertions(+) diff --git a/ebpf/ubpf-stub.c b/ebpf/ubpf-stub.c index 885bd954b7..70da421629 100644 --- a/ebpf/ubpf-stub.c +++ b/ebpf/ubpf-stub.c @@ -27,3 +27,9 @@ int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path) { return 0; } + +uint64_t qemu_ubpf_run_once(UbpfState *u_ebpf, void *target, + size_t target_len) +{ + return 0; +} diff --git a/ebpf/ubpf.c b/ebpf/ubpf.c index d65fffeda3..8ac513c7ed 100644 --- a/ebpf/ubpf.c +++ b/ebpf/ubpf.c @@ -199,3 +199,19 @@ int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path) return 0; } + +uint64_t qemu_ubpf_run_once(UbpfState *u_ebpf, void *target, + size_t target_len) +{ + uint64_t result; + + if (u_ebpf->jit) { + result = u_ebpf->fn(target, target_len); + } else { + if (ubpf_exec(u_ebpf->vm, target, target_len, &result) < 0) { + result = UINT64_MAX; + } + } + + return result; +} diff --git a/ebpf/ubpf.h b/ebpf/ubpf.h index 9a35efbeb6..fc40e84e51 100644 --- a/ebpf/ubpf.h +++ b/ebpf/ubpf.h @@ -38,5 +38,7 @@ bool qemu_ubpf_read_code(UbpfState *u_ebpf, char *path); bool qemu_ubpf_read_target(UbpfState *u_ebpf, char *path); void qemu_ubpf_init_jit(UbpfState *u_ebpf, bool jit); int qemu_ubpf_prepare(UbpfState *u_ebpf, char *code_path); +uint64_t qemu_ubpf_run_once(UbpfState *u_ebpf, void *target, + size_t target_len); #endif /* QEMU_UBPF_H */ From patchwork Fri Jun 17 07:36:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885247 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87FA6C433EF for ; Fri, 17 Jun 2022 08:12:49 +0000 (UTC) Received: from localhost ([::1]:42802 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2760-0005cu-1N for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 04:12:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39474) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lN-0005MO-9I for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:29 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lK-000779-B6 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:27 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452286; x=1686988286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TmmymS2zMdJqwA2QJe8CWQ23Yt3VkbkPJtURlcK+Es4=; b=VNXoPkGsWWRDBhhp+G/MD4w5JjyATfu/qPuRMcZfBtDvve6fuV5hgx/D PFDrc2YhoWBwFO95uxbU2MbbE8IxJfeJjeSpFFoBrKSaG5+u/oHSIY3aY Z3tp9mUBbxlYVwxcez6cu/ndEwk89X5sDdDZWJnISQCNTEyPJD+yRbu4W 0RxXSnIK/Lp35rEBWdSI/jC+o2kIVZorteEhLHenkT5qXqMrinTh60sq0 Q/B+6Cd8bCBX4VI8T7sLVe0ef0iZNDN0I1dmPAZcvtxQ00d6ifsQ7H7c2 i9Q9S+XjRrQL0SbZdIbjxoKfeeDxvgBgT9rJffXxktETeEpK1x9soCajL g==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102159" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102159" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:25 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936751" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:22 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 07/12] net/filter: Introduce filter-ubpf module Date: Fri, 17 Jun 2022 15:36:25 +0800 Message-Id: <20220617073630.535914-8-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The filter-ubpf module able to load user defined ebpf program to handle network packet based on filter framework. Signed-off-by: Zhang Chen --- net/filter-ubpf.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++ net/meson.build | 1 + 2 files changed, 150 insertions(+) create mode 100644 net/filter-ubpf.c diff --git a/net/filter-ubpf.c b/net/filter-ubpf.c new file mode 100644 index 0000000000..c63a021759 --- /dev/null +++ b/net/filter-ubpf.c @@ -0,0 +1,149 @@ +/* + * QEMU Userspace eBPF Support + * + * Copyright(C) 2022 Intel Corporation. + * + * Author: + * Zhang Chen + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "net/filter.h" +#include "net/net.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "qemu/main-loop.h" +#include "qemu/error-report.h" +#include "trace.h" +#include "ebpf/ubpf.h" + +#define TYPE_FILTER_UBPF "filter-ubpf" +OBJECT_DECLARE_SIMPLE_TYPE(FiliterUbpfState, FILTER_UBPF) + +struct FiliterUbpfState { + NetFilterState parent_obj; + bool ip_mode; + char *handler; + UbpfState ubpf; +}; + +static ssize_t filter_ubpf_receive_iov(NetFilterState *nf, + NetClientState *sender, + unsigned flags, + const struct iovec *iov, + int iovcnt, + NetPacketSent *sent_cb) +{ + /* TODO: handle packet by loaded userspace ebpf program */ + + return 0; +} + +static void filter_ubpf_cleanup(NetFilterState *nf) +{ + /* cleanup */ +} + +static void filter_ubpf_setup(NetFilterState *nf, Error **errp) +{ + FiliterUbpfState *s = FILTER_UBPF(nf); + + if (s->handler == NULL) { + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "filter-ubpf parameter"\ + " 'ubpf-handler' cannot be empty"); + return; + } + + qemu_ubpf_init_jit(&s->ubpf, true); + + if (qemu_ubpf_prepare(&s->ubpf, s->handler)) { + error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, "filter-ubpf parameter"\ + " 'ubpf-handler' cannot be load"); + return; + } +} + +static char *filter_ubpf_get_handler(Object *obj, Error **errp) +{ + FiliterUbpfState *s = FILTER_UBPF(obj); + + return g_strdup(s->handler); +} + +static void filter_ubpf_set_handler(Object *obj, + const char *value, + Error **errp) +{ + FiliterUbpfState *s = FILTER_UBPF(obj); + + g_free(s->handler); + s->handler = g_strdup(value); + if (!s->handler) { + error_setg(errp, "filter ubpf needs 'ubpf-handler' " + "property set"); + return; + } +} + +static bool filter_ubpf_get_mode(Object *obj, Error **errp) +{ + FiliterUbpfState *s = FILTER_UBPF(obj); + + return s->ip_mode; +} + +static void filter_ubpf_set_mode(Object *obj, bool value, Error **errp) +{ + FiliterUbpfState *s = FILTER_UBPF(obj); + + s->ip_mode = value; +} + +static void filter_ubpf_class_init(ObjectClass *oc, void *data) +{ + NetFilterClass *nfc = NETFILTER_CLASS(oc); + + object_class_property_add_str(oc, "ubpf-handler", + filter_ubpf_get_handler, + filter_ubpf_set_handler); + object_class_property_add_bool(oc, "ip-mode", + filter_ubpf_get_mode, + filter_ubpf_set_mode); + + nfc->setup = filter_ubpf_setup; + nfc->cleanup = filter_ubpf_cleanup; + nfc->receive_iov = filter_ubpf_receive_iov; +} + +static void filter_ubpf_init(Object *obj) +{ + FiliterUbpfState *s = FILTER_UBPF(obj); + + /* Filter-ubpf default is ip_mode */ + s->ip_mode = true; +} + +static void filter_ubpf_fini(Object *obj) +{ + /* do some thing */ +} + +static const TypeInfo filter_ubpf_info = { + .name = TYPE_FILTER_UBPF, + .parent = TYPE_NETFILTER, + .class_init = filter_ubpf_class_init, + .instance_init = filter_ubpf_init, + .instance_finalize = filter_ubpf_fini, + .instance_size = sizeof(FiliterUbpfState), +}; + +static void register_types(void) +{ + type_register_static(&filter_ubpf_info); +} + +type_init(register_types); diff --git a/net/meson.build b/net/meson.build index 754e2d1d40..177078fa7a 100644 --- a/net/meson.build +++ b/net/meson.build @@ -14,6 +14,7 @@ softmmu_ss.add(files( 'queue.c', 'socket.c', 'util.c', + 'filter-ubpf.c', )) softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('filter-replay.c')) From patchwork Fri Jun 17 07:36:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885246 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 90683C43334 for ; Fri, 17 Jun 2022 08:06:10 +0000 (UTC) Received: from localhost ([::1]:38580 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26zZ-0002Pb-59 for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 04:06:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39544) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lU-0005at-3s for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:36 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28798 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lN-000779-QT for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452289; x=1686988289; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o9+NNdSQwQKKLbfESMJlGiAkUXXjAOfy7EXgOwiurck=; b=kNOQXtyPIE5XzXB3lCZONneNiMWjpeh00AaNhJmxqBN2WmOs04OOlZ1K gU9jFjqjirOFyYe5BFO1fr7KyI6+C5ZCQopN+2boTP+qcAxO0M7bdDRRr 718JSfNBmO5oflKeEgR16ellBmjg4+6UJSO9QgM9Dyadz6ZuHM85wcGiX FtcnZ9osnrfx1sQugUL7j8AL5zzYTIoJjYaNu8r05ETJYhkxPqEanW2VT my1sqdW6ck/V/nEly9HDVVNU77GU3zQSI89mxmtwt+e3Xkrjav/wO41qC 50OZIN44lXUGmLKUzKn0wY+f92WRGr7FncZS4LavZ4aAwvSwegDK9lE2u g==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102165" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102165" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:28 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936759" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:25 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 08/12] qapi: Add FilterUbpfProperties and qemu-options Date: Fri, 17 Jun 2022 15:36:26 +0800 Message-Id: <20220617073630.535914-9-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Add filter-ubpf related QOM and qemu-options. Signed-off-by: Zhang Chen --- qapi/qom.json | 18 ++++++++++++++++++ qemu-options.hx | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/qapi/qom.json b/qapi/qom.json index 6a653c6636..820a5218e8 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -444,6 +444,22 @@ 'base': 'NetfilterProperties', 'data': { '*vnet_hdr_support': 'bool' } } +## +# @FilterUbpfProperties: +# +# Properties for filter-ubpf objects. +# +# @ip-mode: if true, IP packet handle mode is enabled(default: true). +# +# @ubpf-handler: The filename where the userspace ebpf packets handler. +# +# Since: 7.1 +## +{ 'struct': 'FilterUbpfProperties', + 'base': 'NetfilterProperties', + 'data': { '*ip-mode': 'bool', + '*ubpf-handler': 'str' } } + ## # @InputBarrierProperties: # @@ -845,6 +861,7 @@ 'filter-redirector', 'filter-replay', 'filter-rewriter', + 'filter-ubpf', 'input-barrier', { 'name': 'input-linux', 'if': 'CONFIG_LINUX' }, @@ -911,6 +928,7 @@ 'filter-redirector': 'FilterRedirectorProperties', 'filter-replay': 'NetfilterProperties', 'filter-rewriter': 'FilterRewriterProperties', + 'filter-ubpf': 'FilterUbpfProperties', 'input-barrier': 'InputBarrierProperties', 'input-linux': { 'type': 'InputLinuxProperties', 'if': 'CONFIG_LINUX' }, diff --git a/qemu-options.hx b/qemu-options.hx index 60cf188da4..3dfb858867 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -5080,6 +5080,12 @@ SRST stored. The file format is libpcap, so it can be analyzed with tools such as tcpdump or Wireshark. + ``-object filter-ubpf,id=id,netdev=dev,ubpf-handler=filename[,ip-mode][,position=head|tail|id=][,insert=behind|before]`` + filter-ubpf is the userspace ebpf network traffic handler on netdev dev + from the userspace ebpf handler file specified by filename. + If disable ip_mode, the loaded ebpf program will handle raw + network packet. + ``-object colo-compare,id=id,primary_in=chardevid,secondary_in=chardevid,outdev=chardevid,iothread=id[,vnet_hdr_support][,notify_dev=id][,compare_timeout=@var{ms}][,expired_scan_cycle=@var{ms}][,max_queue_size=@var{size}]`` Colo-compare gets packet from primary\_in chardevid and secondary\_in, then compare whether the payload of primary packet From patchwork Fri Jun 17 07:36:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885231 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BC3B7C433EF for ; Fri, 17 Jun 2022 07:54:26 +0000 (UTC) Received: from localhost ([::1]:51450 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26oD-0008QV-E6 for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:54:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39530) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lS-0005Xq-Rs for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:35 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28828 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lR-0007CE-BG for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452293; x=1686988293; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SEZhIf8jMNQNhciEdWpMu+aT2ALSvfXugGxJqxuImrg=; b=ODUr3e2RAbhecC8etg6pDLnilOhAXtP4/32KpLbp1lTPIAS7T58ZpoFG eVT4mdlynXZX9FSiLGY1Teq1FsN6x7ILPt5Zh3yR5LGgFgL/JOtviAGMr VlUK1UBWF1EBxXh8lYNRBL52lkrqRw8vMoPo7tmC0s58rGpGsesgi73O1 2eugCWUc1YJVOV4XQW5lncBcl4H89ZYZq48WWsqLDA1EG+rK7cCpOi86T X7+UHad7XNx/ZsBgRMj/SrtBeW34l8nh1zh4f9yjBQiRctvr4u1yoWDgc EZLKeJFk2UDthIUtBOEkvJQ3sH3bd2duj5IE0Nq0ftS8t+YSWybwT3OWm A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102171" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102171" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:31 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936768" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:28 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 09/12] softmmu/vl.c: Add filter-ubpf for netdev as other netfilters Date: Fri, 17 Jun 2022 15:36:27 +0800 Message-Id: <20220617073630.535914-10-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Zhang Chen --- softmmu/vl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/softmmu/vl.c b/softmmu/vl.c index 4c1e94b00e..d924fb1c71 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -1822,7 +1822,8 @@ static bool object_create_early(const char *type) g_str_equal(type, "filter-redirector") || g_str_equal(type, "colo-compare") || g_str_equal(type, "filter-rewriter") || - g_str_equal(type, "filter-replay")) { + g_str_equal(type, "filter-replay") || + g_str_equal(type, "filter-ubpf")) { return false; } From patchwork Fri Jun 17 07:36:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885235 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D6918C433EF for ; Fri, 17 Jun 2022 07:59:49 +0000 (UTC) Received: from localhost ([::1]:34368 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o26tQ-0007YJ-QZ for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 03:59:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39548) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lU-0005cP-P5 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:36 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28828 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lT-0007CE-6U for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452295; x=1686988295; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gTGywrzpCzMNpBHyDMvcuxeMX8+gcZ5UiuUYs94rVR4=; b=IjhRyD3YdEYGM6YWAkXA59olvNBBUHV6+nFx2QuB2qOL1VvhtEy+3W/Q uGpOyCGG/rlST2GSh6xTz7pAIVS7uWruQ2JwmZruSXjDsA2NcHaWIvG+k tQJOzn8ZaqmU4/b7KaFQo7aCFJ5qVMXeF4MyCzb30xlZN5avO533vMki7 hOJ3EetIh/byQmc6jy6RpH7go1X1ICL4RJVmwMnBLrpCz8jC5J24oFwpG 6ClVBTH2/PBPI2DQKy47rIs8KnL651FuO/VGwRQp9m8OuOvcCd4jDreb1 Br1ZriTr+BD8q7PKOpOnh6NgBo1HUFSo63e3H9Cyd/FVnYKVocomKP4ZL A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102176" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102176" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:33 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936773" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:31 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 10/12] net/filter-ubpf.c: run the ubpf program to handle network packet Date: Fri, 17 Jun 2022 15:36:28 +0800 Message-Id: <20220617073630.535914-11-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Run the loaded userspace ebpf program with the packet. Signed-off-by: Zhang Chen --- net/filter-ubpf.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/net/filter-ubpf.c b/net/filter-ubpf.c index c63a021759..554cc24d8f 100644 --- a/net/filter-ubpf.c +++ b/net/filter-ubpf.c @@ -20,6 +20,8 @@ #include "qemu/error-report.h" #include "trace.h" #include "ebpf/ubpf.h" +#include "colo.h" +#include "util.h" #define TYPE_FILTER_UBPF "filter-ubpf" OBJECT_DECLARE_SIMPLE_TYPE(FiliterUbpfState, FILTER_UBPF) @@ -38,9 +40,43 @@ static ssize_t filter_ubpf_receive_iov(NetFilterState *nf, int iovcnt, NetPacketSent *sent_cb) { - /* TODO: handle packet by loaded userspace ebpf program */ + FiliterUbpfState *s = FILTER_UBPF(nf); + size_t size; + char *buf; + Packet *pkt = NULL; + uint64_t result; + + size = iov_size(iov, iovcnt); + if (!size) { + return 0; + } + + buf = g_malloc(size); + if (unlikely(iov_to_buf(iov, iovcnt, 0, buf, size) != size)) { + g_free(buf); + return 0; + } + + pkt = packet_new_nocopy(buf, size, 0); - return 0; + if (parse_packet_early(pkt)) { + packet_destroy(pkt, NULL); + pkt = NULL; + return 0; + } + + if (s->ip_mode) { + result = qemu_ubpf_run_once(&s->ubpf, pkt->ip, sizeof(struct ip)); + } else { + result = qemu_ubpf_run_once(&s->ubpf, pkt->data, pkt->size); + } + + /* If result == 1, means trigger the ebpf program rules */ + if (result) { + return -1; + } else { + return 0; + } } static void filter_ubpf_cleanup(NetFilterState *nf) From patchwork Fri Jun 17 07:36:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885248 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1A2D8C43334 for ; Fri, 17 Jun 2022 08:12:57 +0000 (UTC) Received: from localhost ([::1]:42926 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o2768-0005hZ-0E for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 04:12:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39570) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lX-0005iR-Ps for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:39 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28828 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lV-0007CE-M7 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452297; x=1686988297; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9BQbAYhTHcp9nvYnIPNZuPTdIll34iEZ3zx7wpIT7GI=; b=hk1ZDfPRfgXXxFE2aAhcXtR0C6vRmBu+2m7fhd52vFI/swetuDAdX79u GTTIYZfJvbIB4i0PUWMOrDqGyoYDoJEZofo5avC55AUNcjzJ+NXtSlwoQ 7vzo/KXzhMb4lKRFyws9U96sUCFaUe9U9cic7Ey65YL1PmYJzVL5SlIsn 2kDoPJjj6u1idJF+ZTCYoBx55+BQ66I7CuDHisZOH4QLH831+fxKtJafW Ef5Pbfg9ueQ31VHd/wDc3qDSILc1egcLf1mmXDWnoDEqVOsmhaJ6QVHAY z16YQHgPN6D0MMnxUq1a3lH/d6sLtRgdmWry2vuCvdVLiEdZRnm2V1JDN w==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102182" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102182" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:36 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936779" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:34 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 11/12] docs/devel: Add userspace-ebpf.rst Date: Fri, 17 Jun 2022 15:36:29 +0800 Message-Id: <20220617073630.535914-12-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Introduce userspace ebpf basic knowledge. Signed-off-by: Zhang Chen --- docs/devel/userspace-ebpf.rst | 106 ++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 docs/devel/userspace-ebpf.rst diff --git a/docs/devel/userspace-ebpf.rst b/docs/devel/userspace-ebpf.rst new file mode 100644 index 0000000000..41eb9b04d6 --- /dev/null +++ b/docs/devel/userspace-ebpf.rst @@ -0,0 +1,106 @@ +=========================== +Userspace eBPF support +=========================== + +eBPF is a revolutionary technology with origins in the Linux kernel that +can run sandboxed programs in an operating system kernel. It is used to +safely and efficiently extend the capabilities of the kernel without +requiring to change kernel source code or load kernel +modules.(from https://ebpf.io/) + +Recently, I worked on QEMU net filter related jobs, like netfilter/iptables +in kernel. We noticed kernel extend the netfilter original cBPF to eBPF, + +It make Linux kernel have the ability to load code dynamically. Why not +enable user space eBPF in QEMU? It can load binary eBPF program even +when VM running. Add some hooks in QEMU as the user space eBPF load point. +Do the things on different layers. The original idea from Jason Wang. + + +That’s the advantages of kernel eBPF. Most of the functions can be +implemented in QEMU. The Power of Programmability. + + 1). Safety: + + Building on the foundation of seeing and understanding all system + calls and combining that with a packet and socket-level view of all + networking operations allows for revolutionary new approaches to + securing systems. + + 2). Tracing & Profiling: + + The ability to attach eBPF programs to trace points as well as kernel + and user application probe points allows unprecedented visibility into + the runtime behavior of applications and the system itself. + + 3). Networking: + + The combination of programmability and efficiency makes eBPF a natural + fit for all packet processing requirements of networking solutions. + + 4). Observability & Monitoring: + + Instead of relying on static counters and gauges exposed by the + perating system, eBPF enables the collection & in-kernel aggregation + of custom metrics and generation of visibility events based on a wide + range of possible sources. + + Qemu userspace ebpf design based on ubpf project (https://github.com/iovisor/ubpf). + The most mature userspace ebpf implementation. This project officially + support by iovisor(Like BCC and bpftrace). Qemu userspace ebpf make + the ubpf project as the git submodule. + + Current implementation support load ebpf program and run it in + filter-ubpf module, developer can easy reuse the ubpf function in + Qemu's other modules from the function in /ebpf/ubpf.c, And it support JIT. + For the uBPF License is Apache License 2.0, It's OK to compatible + with QEMU’s GPLv2 LICENSE same as mason. + + How to use it: + 1. Write your ebpf C program. For example filter dst IP: + + bpf_filter.c + +#include +#include + +#define ONE_ONE_ONE_ONE 0x01010101 + +struct ipv4_header { + uint8_t ver_ihl; + uint8_t tos; + uint16_t total_length; + uint16_t id; + uint16_t frag; + uint8_t ttl; + uint8_t proto; + uint16_t csum; + uint32_t src; + uint32_t dst; +}; + +int is_dst_one_one_one_one(void *opaque) { + struct ipv4_header *ipv4_header = (struct ipv4_header*)opaque; + + if (ntohl(ipv4_header->dst) == ONE_ONE_ONE_ONE) { + return 1; + } + + return 0; +} + + 2. Build it with clang: + clang -O2 -target bpf -c bpf_filter.c -o ip_dst.o + + 3. Load it with Qemu filter-ubpf: + -object filter-ubpf,netdev=hn0,id=ubpf1,queue=tx,ip-mode=on, + ubpf-handler=ip_dst.o + + 4. Boot the VM and it will filt IP dst 1.1.1.1 packet. + + +TODO: Need to add more comments and test-case for ubpf, current + implementation not include ebpf verifier. Qemu is a userspace + program, not like kernel ebpf run code in kernel space, I think + if the someone want to hack Qemu code no need to load a malicious + ubpf program, he can hack Qemu code directly. From patchwork Fri Jun 17 07:36:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 12885250 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B87D3C433EF for ; Fri, 17 Jun 2022 08:16:37 +0000 (UTC) Received: from localhost ([::1]:47148 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o279g-00009w-NQ for qemu-devel@archiver.kernel.org; Fri, 17 Jun 2022 04:16:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39584) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26la-0005qN-UT for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:43 -0400 Received: from mga06b.intel.com ([134.134.136.31]:28828 helo=mga06.intel.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o26lY-0007CE-N5 for qemu-devel@nongnu.org; Fri, 17 Jun 2022 03:51:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655452300; x=1686988300; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ORE+TXo+6a7hc6uGzt4dLpzWhvEAhI8+MLcS8zWXwFA=; b=YOvsPuWIDgADVCVOyZR4lGPw7KRVv5Y5mLzsH5+Yj2XdUCN6TcoPcbyH /MJlE0xxobqlvXsSYAmAHU/UMae+I2uAFAhFdDK/pyDcT4iMEogCZQKM4 c2Lj5OUldZP4xZlao6QDQJuWAMM9msDAanyOeeDGglHCwA7Q/BKutcnWe Kzo7IOr4gybzQabmSfDfNPNwN88LU51VYdUWeZh7vWy3bcGJqAlO8y/li qlALwathExJ166mHyaMWADgVzFiltcuqJuHBWQ6dwxkMYvtGM812q1EmC 2/3zIKhoFtMZvuIdyyzgwMMCGxkFJjS3GvG4g4SniLIFfimuv0Z3a47B4 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="341102192" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="341102192" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:39 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="641936783" Received: from unknown (HELO localhost.localdomain) ([10.239.13.19]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 00:51:37 -0700 From: Zhang Chen To: Jason Wang , qemu-dev , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Eric Blake , Markus Armbruster Cc: Zhang Chen , Peter Maydell , Thomas Huth , Laurent Vivier , Yuri Benditovich , Andrew Melnychenko Subject: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case Date: Fri, 17 Jun 2022 15:36:30 +0800 Message-Id: <20220617073630.535914-13-chen.zhang@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com> References: <20220617073630.535914-1-chen.zhang@intel.com> MIME-Version: 1.0 Received-SPF: pass client-ip=134.134.136.31; envelope-from=chen.zhang@intel.com; helo=mga06.intel.com X-Spam_score_int: -44 X-Spam_score: -4.5 X-Spam_bar: ---- X-Spam_report: (-4.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" TODO: This test case does not work. Need add ubpf.h header in qtest compile "-I ../ubpf/vm -I ../ubpf/vm/inc". I'm not sure if we need it in qtest. Because normal tests/qtest not including external module test case like fdt. Or we just need a qtest case for filter-ubpf module. This test will load pre-compiled ebpf binary and run it in QEMU. Signed-off-by: Zhang Chen --- tests/qtest/demo_ubpf.o | Bin 0 -> 544 bytes tests/qtest/integer_5.mem | Bin 0 -> 4 bytes tests/qtest/meson.build | 3 +- tests/qtest/ubpf-test.c | 64 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/qtest/demo_ubpf.o create mode 100644 tests/qtest/integer_5.mem create mode 100644 tests/qtest/ubpf-test.c diff --git a/tests/qtest/demo_ubpf.o b/tests/qtest/demo_ubpf.o new file mode 100644 index 0000000000000000000000000000000000000000..960a411c224348548db42d9ae2716ae3ef4ea249 GIT binary patch literal 544 zcmb<-^>JfjWMqH=MuzVU2p&w7f#Csy$>0EHJ20>URVE5RB+`KtNZ(Wl7bhtPlwo1` z_#a&XJ5WG~fe9`w0b}Wvq*jzLBo(B^7Zl~EGw9{yl;y@Jrlb@VXQnfh0>yPpQj1IU zk{R@hONvSolYn$(E{LWM&;lC6jK!!0P%$esIrOjt@j;jkO`QXj5BDdO&w-{66uitn o|MP)V1G3ZtWDbzc0_CIIZv+%agepQ)1eEE4qz|MHW + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "ebpf/ubpf.h" + +/* + * Demo userspace ebpf program + * The test binary use clang to build this source code: + * demo_ubpf.c + * + * #include + * + * static uint32_t double_it(uint32_t a) + * { + * return (a * 2); + * } + * + * uint32_t bpf_prog(int32_t *arg) { + * uint32_t result = 0; + * result = double_it(*arg); + * + * return result; + * } + * + * Build the userspace ebpf program binary file: + * clang -O2 -target bpf -c demo_ubpf.c -o demo_ubpf.o + * + * The external terget source: + * printf "%b" '\x05\x00\x00\x00' > integer_5.mem + * + */ + +int main(int argc, char **argc) +{ + UbpfState u_ebpf; + char program_path[] = "demo_ubpf.o"; + /* uBPF can read target from internal source or external source*/ + char target_path[] = "integer_5.mem"; + + qemu_ubpf_init_jit(&u_ebpf, true); + + g_assert_cmpuint(qemu_ubpf_prepare(&u_ebpf, program_path), ==, 0); + + g_assert_true(qemu_ubpf_read_target(&u_ebpf, target_path)); + + g_assert_cmpuint(qemu_run_ubpf_once(&u_ebpf, u_ebpf.target, + u_ebpf.target_len), ==, 10); + + ubpf_destroy(u_ebpf.vm); + + return 0; +}