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 05/12] ebpf/uBPF: Add qemu_prepare_ubpf to load ebpf binary
Date: Fri, 17 Jun 2022 15:36:23 +0800	[thread overview]
Message-ID: <20220617073630.535914-6-chen.zhang@intel.com> (raw)
In-Reply-To: <20220617073630.535914-1-chen.zhang@intel.com>

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 <chen.zhang@intel.com>
---
 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 */
-- 
2.25.1



  parent reply	other threads:[~2022-06-17  8:05 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 ` Zhang Chen [this message]
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 ` [RFC PATCH 12/12] test/qtest: Add ubpf basic test case Zhang Chen
2022-06-17  9:34   ` 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-6-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).