bpf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * common eBPF ELF 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. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License (not later!)
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, see <http://www.gnu.org/licenses>
  20. */
  21. #include <stdlib.h>
  22. #include <memory.h>
  23. #include <unistd.h>
  24. #include <asm/unistd.h>
  25. #include <linux/bpf.h>
  26. #include "bpf.h"
  27. /*
  28. * When building perf, unistd.h is overridden. __NR_bpf is
  29. * required to be defined explicitly.
  30. */
  31. #ifndef __NR_bpf
  32. # if defined(__i386__)
  33. # define __NR_bpf 357
  34. # elif defined(__x86_64__)
  35. # define __NR_bpf 321
  36. # elif defined(__aarch64__)
  37. # define __NR_bpf 280
  38. # elif defined(__sparc__)
  39. # define __NR_bpf 349
  40. # else
  41. # error __NR_bpf not defined. libbpf does not support your arch.
  42. # endif
  43. #endif
  44. static inline __u64 ptr_to_u64(const void *ptr)
  45. {
  46. return (__u64) (unsigned long) ptr;
  47. }
  48. static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
  49. unsigned int size)
  50. {
  51. return syscall(__NR_bpf, cmd, attr, size);
  52. }
  53. int bpf_create_map(enum bpf_map_type map_type, int key_size,
  54. int value_size, int max_entries, __u32 map_flags)
  55. {
  56. union bpf_attr attr;
  57. memset(&attr, '\0', sizeof(attr));
  58. attr.map_type = map_type;
  59. attr.key_size = key_size;
  60. attr.value_size = value_size;
  61. attr.max_entries = max_entries;
  62. attr.map_flags = map_flags;
  63. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  64. }
  65. int bpf_create_map_in_map(enum bpf_map_type map_type, int key_size,
  66. int inner_map_fd, int max_entries, __u32 map_flags)
  67. {
  68. union bpf_attr attr;
  69. memset(&attr, '\0', sizeof(attr));
  70. attr.map_type = map_type;
  71. attr.key_size = key_size;
  72. attr.value_size = 4;
  73. attr.inner_map_fd = inner_map_fd;
  74. attr.max_entries = max_entries;
  75. attr.map_flags = map_flags;
  76. return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
  77. }
  78. int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
  79. size_t insns_cnt, const char *license,
  80. __u32 kern_version, char *log_buf, size_t log_buf_sz)
  81. {
  82. int fd;
  83. union bpf_attr attr;
  84. bzero(&attr, sizeof(attr));
  85. attr.prog_type = type;
  86. attr.insn_cnt = (__u32)insns_cnt;
  87. attr.insns = ptr_to_u64(insns);
  88. attr.license = ptr_to_u64(license);
  89. attr.log_buf = ptr_to_u64(NULL);
  90. attr.log_size = 0;
  91. attr.log_level = 0;
  92. attr.kern_version = kern_version;
  93. fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  94. if (fd >= 0 || !log_buf || !log_buf_sz)
  95. return fd;
  96. /* Try again with log */
  97. attr.log_buf = ptr_to_u64(log_buf);
  98. attr.log_size = log_buf_sz;
  99. attr.log_level = 1;
  100. log_buf[0] = 0;
  101. return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  102. }
  103. int bpf_map_update_elem(int fd, const void *key, const void *value,
  104. __u64 flags)
  105. {
  106. union bpf_attr attr;
  107. bzero(&attr, sizeof(attr));
  108. attr.map_fd = fd;
  109. attr.key = ptr_to_u64(key);
  110. attr.value = ptr_to_u64(value);
  111. attr.flags = flags;
  112. return sys_bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
  113. }
  114. int bpf_map_lookup_elem(int fd, const void *key, void *value)
  115. {
  116. union bpf_attr attr;
  117. bzero(&attr, sizeof(attr));
  118. attr.map_fd = fd;
  119. attr.key = ptr_to_u64(key);
  120. attr.value = ptr_to_u64(value);
  121. return sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
  122. }
  123. int bpf_map_delete_elem(int fd, const void *key)
  124. {
  125. union bpf_attr attr;
  126. bzero(&attr, sizeof(attr));
  127. attr.map_fd = fd;
  128. attr.key = ptr_to_u64(key);
  129. return sys_bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
  130. }
  131. int bpf_map_get_next_key(int fd, const void *key, void *next_key)
  132. {
  133. union bpf_attr attr;
  134. bzero(&attr, sizeof(attr));
  135. attr.map_fd = fd;
  136. attr.key = ptr_to_u64(key);
  137. attr.next_key = ptr_to_u64(next_key);
  138. return sys_bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
  139. }
  140. int bpf_obj_pin(int fd, const char *pathname)
  141. {
  142. union bpf_attr attr;
  143. bzero(&attr, sizeof(attr));
  144. attr.pathname = ptr_to_u64((void *)pathname);
  145. attr.bpf_fd = fd;
  146. return sys_bpf(BPF_OBJ_PIN, &attr, sizeof(attr));
  147. }
  148. int bpf_obj_get(const char *pathname)
  149. {
  150. union bpf_attr attr;
  151. bzero(&attr, sizeof(attr));
  152. attr.pathname = ptr_to_u64((void *)pathname);
  153. return sys_bpf(BPF_OBJ_GET, &attr, sizeof(attr));
  154. }
  155. int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
  156. unsigned int flags)
  157. {
  158. union bpf_attr attr;
  159. bzero(&attr, sizeof(attr));
  160. attr.target_fd = target_fd;
  161. attr.attach_bpf_fd = prog_fd;
  162. attr.attach_type = type;
  163. attr.attach_flags = flags;
  164. return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
  165. }
  166. int bpf_prog_detach(int target_fd, enum bpf_attach_type type)
  167. {
  168. union bpf_attr attr;
  169. bzero(&attr, sizeof(attr));
  170. attr.target_fd = target_fd;
  171. attr.attach_type = type;
  172. return sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
  173. }
  174. int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
  175. void *data_out, __u32 *size_out, __u32 *retval,
  176. __u32 *duration)
  177. {
  178. union bpf_attr attr;
  179. int ret;
  180. bzero(&attr, sizeof(attr));
  181. attr.test.prog_fd = prog_fd;
  182. attr.test.data_in = ptr_to_u64(data);
  183. attr.test.data_out = ptr_to_u64(data_out);
  184. attr.test.data_size_in = size;
  185. attr.test.repeat = repeat;
  186. ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  187. if (size_out)
  188. *size_out = attr.test.data_size_out;
  189. if (retval)
  190. *retval = attr.test.retval;
  191. if (duration)
  192. *duration = attr.test.duration;
  193. return ret;
  194. }