libbpf.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. void bpf_object__close(struct bpf_object *object);
  28. /* Load/unload object into/from kernel */
  29. int bpf_object__load(struct bpf_object *obj);
  30. int bpf_object__unload(struct bpf_object *obj);
  31. /* Accessors of bpf_program. */
  32. struct bpf_program;
  33. struct bpf_program *bpf_program__next(struct bpf_program *prog,
  34. struct bpf_object *obj);
  35. #define bpf_object__for_each_program(pos, obj) \
  36. for ((pos) = bpf_program__next(NULL, (obj)); \
  37. (pos) != NULL; \
  38. (pos) = bpf_program__next((pos), (obj)))
  39. typedef void (*bpf_program_clear_priv_t)(struct bpf_program *,
  40. void *);
  41. int bpf_program__set_private(struct bpf_program *prog, void *priv,
  42. bpf_program_clear_priv_t clear_priv);
  43. int bpf_program__get_private(struct bpf_program *prog,
  44. void **ppriv);
  45. const char *bpf_program__title(struct bpf_program *prog, bool dup);
  46. int bpf_program__fd(struct bpf_program *prog);
  47. /*
  48. * We don't need __attribute__((packed)) now since it is
  49. * unnecessary for 'bpf_map_def' because they are all aligned.
  50. * In addition, using it will trigger -Wpacked warning message,
  51. * and will be treated as an error due to -Werror.
  52. */
  53. struct bpf_map_def {
  54. unsigned int type;
  55. unsigned int key_size;
  56. unsigned int value_size;
  57. unsigned int max_entries;
  58. };
  59. #endif