qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH 04/12] ebpf/uBPF: Introduce ubpf initialize functions


From: Zhang Chen
Subject: [RFC PATCH 04/12] ebpf/uBPF: Introduce ubpf initialize functions
Date: Fri, 17 Jun 2022 15:36:22 +0800

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]