xref: /qemu/ebpf/ebpf.c (revision 4a1babe5)
1 /*
2  * QEMU eBPF binary declaration routine.
3  *
4  * Developed by Daynix Computing LTD (http://www.daynix.com)
5  *
6  * Authors:
7  *  Andrew Melnychenko <andrew@daynix.com>
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qemu/queue.h"
14 #include "qapi/error.h"
15 #include "qapi/qapi-commands-ebpf.h"
16 #include "ebpf/ebpf.h"
17 
18 typedef struct ElfBinaryDataEntry {
19     int id;
20     const void *data;
21     size_t datalen;
22 
23     QSLIST_ENTRY(ElfBinaryDataEntry) node;
24 } ElfBinaryDataEntry;
25 
26 static QSLIST_HEAD(, ElfBinaryDataEntry) ebpf_elf_obj_list =
27                                             QSLIST_HEAD_INITIALIZER();
28 
29 void ebpf_register_binary_data(int id, const void *data, size_t datalen)
30 {
31     struct ElfBinaryDataEntry *dataentry = NULL;
32 
33     dataentry = g_new0(struct ElfBinaryDataEntry, 1);
34     dataentry->data = data;
35     dataentry->datalen = datalen;
36     dataentry->id = id;
37 
38     QSLIST_INSERT_HEAD(&ebpf_elf_obj_list, dataentry, node);
39 }
40 
41 const void *ebpf_find_binary_by_id(int id, size_t *sz, Error **errp)
42 {
43     struct ElfBinaryDataEntry *it = NULL;
44     QSLIST_FOREACH(it, &ebpf_elf_obj_list, node) {
45         if (id == it->id) {
46             *sz = it->datalen;
47             return it->data;
48         }
49     }
50 
51     error_setg(errp, "can't find eBPF object with id: %d", id);
52 
53     return NULL;
54 }
55 
56 EbpfObject *qmp_request_ebpf(EbpfProgramID id, Error **errp)
57 {
58     EbpfObject *ret = NULL;
59     size_t size = 0;
60     const void *data = ebpf_find_binary_by_id(id, &size, errp);
61     if (!data) {
62         return NULL;
63     }
64 
65     ret = g_new0(EbpfObject, 1);
66     ret->object = g_base64_encode(data, size);
67 
68     return ret;
69 }
70