libbpf.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /*
  13. * In include/linux/compiler-gcc.h, __printf is defined. However
  14. * it should be better if libbpf.h doesn't depend on Linux header file.
  15. * So instead of __printf, here we use gcc attribute directly.
  16. */
  17. typedef int (*libbpf_print_fn_t)(const char *, ...)
  18. __attribute__((format(printf, 1, 2)));
  19. void libbpf_set_print(libbpf_print_fn_t warn,
  20. libbpf_print_fn_t info,
  21. libbpf_print_fn_t debug);
  22. /* Hide internal to user */
  23. struct bpf_object;
  24. struct bpf_object *bpf_object__open(const char *path);
  25. struct bpf_object *bpf_object__open_buffer(void *obj_buf,
  26. size_t obj_buf_sz,
  27. const char *name);
  28. void bpf_object__close(struct bpf_object *object);
  29. /* Load/unload object into/from kernel */
  30. int bpf_object__load(struct bpf_object *obj);
  31. int bpf_object__unload(struct bpf_object *obj);
  32. const char *bpf_object__get_name(struct bpf_object *obj);
  33. struct bpf_object *bpf_object__next(struct bpf_object *prev);
  34. #define bpf_object__for_each_safe(pos, tmp) \
  35. for ((pos) = bpf_object__next(NULL), \
  36. (tmp) = bpf_object__next(pos); \
  37. (pos) != NULL; \
  38. (pos) = (tmp), (tmp) = bpf_object__next(tmp))
  39. /* Accessors of bpf_program. */
  40. struct bpf_program;
  41. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  42. struct bpf_object *obj);
  43. #define bpf_object__for_each_program(pos, obj) \
  44. for ((pos) = bpf_program__next(NULL, (obj)); \
  45. (pos) != NULL; \
  46. (pos) = bpf_program__next((pos), (obj)))
  47. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  48. void *);
  49. int bpf_program__set_private(struct bpf_program *prog, void *priv,
  50. bpf_program_clear_priv_t clear_priv);
  51. int bpf_program__get_private(struct bpf_program *prog,
  52. void **ppriv);
  53. const char *bpf_program__title(struct bpf_program *prog, bool dup);
  54. int bpf_program__fd(struct bpf_program *prog);
  55. /*
  56. * We don't need __attribute__((packed)) now since it is
  57. * unnecessary for 'bpf_map_def' because they are all aligned.
  58. * In addition, using it will trigger -Wpacked warning message,
  59. * and will be treated as an error due to -Werror.
  60. */
  61. struct bpf_map_def {
  62. unsigned int type;
  63. unsigned int key_size;
  64. unsigned int value_size;
  65. unsigned int max_entries;
  66. };
  67. #endif