ebpf.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include "qemu/osdep.h"
  12. #include "qemu/queue.h"
  13. #include "qapi/error.h"
  14. #include "qapi/qapi-commands-ebpf.h"
  15. #include "ebpf/ebpf.h"
  16. typedef struct ElfBinaryDataEntry {
  17. int id;
  18. const void *data;
  19. size_t datalen;
  20. QSLIST_ENTRY(ElfBinaryDataEntry) node;
  21. } ElfBinaryDataEntry;
  22. static QSLIST_HEAD(, ElfBinaryDataEntry) ebpf_elf_obj_list =
  23. QSLIST_HEAD_INITIALIZER();
  24. void ebpf_register_binary_data(int id, const void *data, size_t datalen)
  25. {
  26. struct ElfBinaryDataEntry *dataentry = NULL;
  27. dataentry = g_new0(struct ElfBinaryDataEntry, 1);
  28. dataentry->data = data;
  29. dataentry->datalen = datalen;
  30. dataentry->id = id;
  31. QSLIST_INSERT_HEAD(&ebpf_elf_obj_list, dataentry, node);
  32. }
  33. const void *ebpf_find_binary_by_id(int id, size_t *sz, Error **errp)
  34. {
  35. struct ElfBinaryDataEntry *it = NULL;
  36. QSLIST_FOREACH(it, &ebpf_elf_obj_list, node) {
  37. if (id == it->id) {
  38. *sz = it->datalen;
  39. return it->data;
  40. }
  41. }
  42. error_setg(errp, "can't find eBPF object with id: %d", id);
  43. return NULL;
  44. }
  45. EbpfObject *qmp_request_ebpf(EbpfProgramID id, Error **errp)
  46. {
  47. EbpfObject *ret = NULL;
  48. size_t size = 0;
  49. const void *data = ebpf_find_binary_by_id(id, &size, errp);
  50. if (!data) {
  51. return NULL;
  52. }
  53. ret = g_new0(EbpfObject, 1);
  54. ret->object = g_base64_encode(data, size);
  55. return ret;
  56. }