qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Zhang Chen <chen.zhang@intel.com>
To: "Jason Wang" <jasowang@redhat.com>,
	qemu-dev <qemu-devel@nongnu.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Eric Blake" <eblake@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>
Cc: Zhang Chen <chen.zhang@intel.com>,
	Peter Maydell <peter.maydell@linaro.org>,
	Thomas Huth <thuth@redhat.com>,
	Laurent Vivier <lvivier@redhat.com>,
	Yuri Benditovich <yuri.benditovich@daynix.com>,
	Andrew Melnychenko <andrew@daynix.com>
Subject: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case
Date: Fri, 17 Jun 2022 15:36:30 +0800	[thread overview]
Message-ID: <20220617073630.535914-13-chen.zhang@intel.com> (raw)
In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com>

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 <chen.zhang@intel.com>
---
 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<Shb0E~Jd)c^nh

literal 0
HcmV?d00001

diff --git a/tests/qtest/integer_5.mem b/tests/qtest/integer_5.mem
new file mode 100644
index 0000000000000000000000000000000000000000..a786e127004dd9e94e88fda7742d248237ad8885
GIT binary patch
literal 4
LcmZQ&U|;|M02lxU

literal 0
HcmV?d00001

diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index 31287a9173..9cc629e22b 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -94,7 +94,8 @@ qtests_i386 = \
    'migration-test',
    'test-x86-cpuid-compat',
    'numa-test',
-   'test-filter-redirector'
+   'test-filter-redirector',
+   'ubpf-test'
   ]
 
 if dbus_display
diff --git a/tests/qtest/ubpf-test.c b/tests/qtest/ubpf-test.c
new file mode 100644
index 0000000000..6e70a99320
--- /dev/null
+++ b/tests/qtest/ubpf-test.c
@@ -0,0 +1,64 @@
+/*
+ * QEMU Userspace eBPF test case
+ *
+ * Copyright(C) 2022 Intel Corporation.
+ *
+ * Author:
+ *  Zhang Chen <chen.zhang@intel.com>
+ *
+ * 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 <stdint.h>
+ *
+ * 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;
+}
-- 
2.25.1



  parent reply	other threads:[~2022-06-17  8:16 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-17  7:36 [RFC PATCH 00/12] Introduce QEMU userspace ebpf support Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 01/12] configure: Add iovisor/ubpf project as a submodule for QEMU Zhang Chen
2022-06-17  8:05   ` Daniel P. Berrangé
2022-06-20  5:59     ` Zhang, Chen
2022-06-20  8:11       ` Daniel P. Berrangé
2022-06-20  8:46         ` Thomas Huth
2022-06-20  9:29           ` Zhang, Chen
2022-06-20  9:43             ` Thomas Huth
2022-06-20 10:29               ` Zhang, Chen
2022-06-20 10:37                 ` Daniel P. Berrangé
2022-06-22  9:21                   ` Zhang, Chen
2022-06-20 10:00             ` Daniel P. Berrangé
2022-06-20 10:22               ` Zhang, Chen
2022-06-17  7:36 ` [RFC PATCH 02/12] meson: Add ubpf build config and misc Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 03/12] ebpf/uBPF: Introduce userspace ebpf data structure Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 04/12] ebpf/uBPF: Introduce ubpf initialize functions Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 05/12] ebpf/uBPF: Add qemu_prepare_ubpf to load ebpf binary Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 06/12] ebpf/uBPF: Add qemu_ubpf_run_once excute real ebpf program Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 07/12] net/filter: Introduce filter-ubpf module Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 08/12] qapi: Add FilterUbpfProperties and qemu-options Zhang Chen
2022-06-20  7:45   ` Markus Armbruster
2022-06-20  9:40     ` Zhang, Chen
2022-06-17  7:36 ` [RFC PATCH 09/12] softmmu/vl.c: Add filter-ubpf for netdev as other netfilters Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 10/12] net/filter-ubpf.c: run the ubpf program to handle network packet Zhang Chen
2022-06-17  7:36 ` [RFC PATCH 11/12] docs/devel: Add userspace-ebpf.rst Zhang Chen
2022-06-17  7:36 ` Zhang Chen [this message]
2022-06-17  9:34   ` [RFC PATCH 12/12] test/qtest: Add ubpf basic test case Thomas Huth
2022-06-20  9:31     ` Zhang, Chen
2022-06-20  9:51       ` Thomas Huth
2022-06-29 10:43 ` [RFC PATCH 00/12] Introduce QEMU userspace ebpf support Andrew Melnichenko
2022-07-01  6:14   ` Zhang, Chen

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=20220617073630.535914-13-chen.zhang@intel.com \
    --to=chen.zhang@intel.com \
    --cc=andrew@daynix.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=jasowang@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    --cc=yuri.benditovich@daynix.com \
    /path/to/YOUR_REPLY

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

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