libbpf.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Common eBPF ELF object loading operations.
  3. *
  4. * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
  5. * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
  6. * Copyright (C) 2015 Huawei Inc.
  7. */
  8. #ifndef __BPF_LIBBPF_H
  9. #define __BPF_LIBBPF_H
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12. #include <linux/err.h>
  13. enum libbpf_errno {
  14. __LIBBPF_ERRNO__START = 4000,
  15. /* Something wrong in libelf */
  16. LIBBPF_ERRNO__LIBELF = __LIBBPF_ERRNO__START,
  17. LIBBPF_ERRNO__FORMAT, /* BPF object format invalid */
  18. LIBBPF_ERRNO__KVERSION, /* Incorrect or no 'version' section */
  19. LIBBPF_ERRNO__ENDIAN, /* Endian missmatch */
  20. LIBBPF_ERRNO__INTERNAL, /* Internal error in libbpf */
  21. LIBBPF_ERRNO__RELOC, /* Relocation failed */
  22. LIBBPF_ERRNO__LOAD, /* Load program failure for unknown reason */
  23. LIBBPF_ERRNO__VERIFY, /* Kernel verifier blocks program loading */
  24. LIBBPF_ERRNO__PROG2BIG, /* Program too big */
  25. LIBBPF_ERRNO__KVER, /* Incorrect kernel version */
  26. __LIBBPF_ERRNO__END,
  27. };
  28. int libbpf_strerror(int err, char *buf, size_t size);
  29. /*
  30. * In include/linux/compiler-gcc.h, __printf is defined. However
  31. * it should be better if libbpf.h doesn't depend on Linux header file.
  32. * So instead of __printf, here we use gcc attribute directly.
  33. */
  34. typedef int (*libbpf_print_fn_t)(const char *, ...)
  35. __attribute__((format(printf, 1, 2)));
  36. void libbpf_set_print(libbpf_print_fn_t warn,
  37. libbpf_print_fn_t info,
  38. libbpf_print_fn_t debug);
  39. /* Hide internal to user */
  40. struct bpf_object;
  41. struct bpf_object *bpf_object__open(const char *path);
  42. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  43. size_t obj_buf_sz,
  44. const char *name);
  45. void bpf_object__close(struct bpf_object *object);
  46. /* Load/unload object into/from kernel */
  47. int bpf_object__load(struct bpf_object *obj);
  48. int bpf_object__unload(struct bpf_object *obj);
  49. const char *bpf_object__get_name(struct bpf_object *obj);
  50. unsigned int bpf_object__get_kversion(struct bpf_object *obj);
  51. struct bpf_object *bpf_object__next(struct bpf_object *prev);
  52. #define bpf_object__for_each_safe(pos, tmp) \
  53. for ((pos) = bpf_object__next(NULL), \
  54. (tmp) = bpf_object__next(pos); \
  55. (pos) != NULL; \
  56. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  57. /* Accessors of bpf_program. */
  58. struct bpf_program;
  59. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  60. struct bpf_object *obj);
  61. #define bpf_object__for_each_program(pos, obj) \
  62. for ((pos) = bpf_program__next(NULL, (obj)); \
  63. (pos) != NULL; \
  64. (pos) = bpf_program__next((pos), (obj)))
  65. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  66. void *);
  67. int bpf_program__set_private(struct bpf_program *prog, void *priv,
  68. bpf_program_clear_priv_t clear_priv);
  69. int bpf_program__get_private(struct bpf_program *prog,
  70. void **ppriv);
  71. const char *bpf_program__title(struct bpf_program *prog, bool needs_copy);
  72. int bpf_program__fd(struct bpf_program *prog);
  73. /*
  74. * We don't need __attribute__((packed)) now since it is
  75. * unnecessary for 'bpf_map_def' because they are all aligned.
  76. * In addition, using it will trigger -Wpacked warning message,
  77. * and will be treated as an error due to -Werror.
  78. */
  79. struct bpf_map_def {
  80. unsigned int type;
  81. unsigned int key_size;
  82. unsigned int value_size;
  83. unsigned int max_entries;
  84. };
  85. #endif