Re: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case

2022-06-20 Thread Thomas Huth

On 20/06/2022 11.31, Zhang, Chen wrote:



-Original Message-
From: Thomas Huth 
Sent: Friday, June 17, 2022 5:34 PM
To: Zhang, Chen ; Jason Wang
; qemu-dev ; Paolo
Bonzini ; Daniel P. Berrangé
; Eduardo Habkost ; Eric
Blake ; Markus Armbruster 
Cc: Peter Maydell ; Laurent Vivier
; Yuri Benditovich ;
Andrew Melnychenko 
Subject: Re: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case

On 17/06/2022 09.36, Zhang Chen wrote:

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 
---

[...]

Apart from the #include "libqtest.h" there is nothing related to qtest in
here ... should this maybe rather go into test/unit/ instead?


Rethink about it, I think you are right.
The only issue is can we involve submodule's header file in tests/unit?
I can't find meson/fdt/SLIRP test cases in the tests.


Well, the test should only be enabled in meson.build if the CONFIG_UBPF 
switch is also enabled, then you can be sure that the header files of ubpf 
are also available when it gets compiled.


 Thomas




RE: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case

2022-06-20 Thread Zhang, Chen


> -Original Message-
> From: Thomas Huth 
> Sent: Friday, June 17, 2022 5:34 PM
> To: Zhang, Chen ; Jason Wang
> ; qemu-dev ; Paolo
> Bonzini ; Daniel P. Berrangé
> ; Eduardo Habkost ; Eric
> Blake ; Markus Armbruster 
> Cc: Peter Maydell ; Laurent Vivier
> ; Yuri Benditovich ;
> Andrew Melnychenko 
> Subject: Re: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case
> 
> On 17/06/2022 09.36, Zhang Chen wrote:
> > 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 
> > ---
> [...]
> > diff --git a/tests/qtest/ubpf-test.c b/tests/qtest/ubpf-test.c new
> > file mode 100644 index 00..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 
> > + *
> > + * 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(_ebpf, true);
> > +
> > +g_assert_cmpuint(qemu_ubpf_prepare(_ebpf, program_path), ==,
> > + 0);
> > +
> > +g_assert_true(qemu_ubpf_read_target(_ebpf, target_path));
> > +
> > +g_assert_cmpuint(qemu_run_ubpf_once(_ebpf, u_ebpf.target,
> > +u_ebpf.target_len), ==, 10);
> > +
> > +ubpf_destroy(u_ebpf.vm);
> > +
> > +return 0;
> > +}
> 
> Apart from the #include "libqtest.h" there is nothing related to qtest in
> here ... should this maybe rather go into test/unit/ instead?

Rethink about it, I think you are right.
The only issue is can we involve submodule's header file in tests/unit?
I can't find meson/fdt/SLIRP test cases in the tests.

Thanks
Chen

> 
>   Thomas



Re: [RFC PATCH 12/12] test/qtest: Add ubpf basic test case

2022-06-17 Thread Thomas Huth

On 17/06/2022 09.36, Zhang Chen wrote:

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 
---

[...]

diff --git a/tests/qtest/ubpf-test.c b/tests/qtest/ubpf-test.c
new file mode 100644
index 00..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 
+ *
+ * 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(_ebpf, true);
+
+g_assert_cmpuint(qemu_ubpf_prepare(_ebpf, program_path), ==, 0);
+
+g_assert_true(qemu_ubpf_read_target(_ebpf, target_path));
+
+g_assert_cmpuint(qemu_run_ubpf_once(_ebpf, u_ebpf.target,
+u_ebpf.target_len), ==, 10);
+
+ubpf_destroy(u_ebpf.vm);
+
+return 0;
+}


Apart from the #include "libqtest.h" there is nothing related to qtest in 
here ... should this maybe rather go into test/unit/ instead?


 Thomas