fds_example.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <linux/unistd.h>
  2. #include <linux/bpf.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <bpf/bpf.h>
  13. #include <bpf/libbpf.h>
  14. #include "bpf_insn.h"
  15. #include "sock_example.h"
  16. #include "bpf_util.h"
  17. #define BPF_F_PIN (1 << 0)
  18. #define BPF_F_GET (1 << 1)
  19. #define BPF_F_PIN_GET (BPF_F_PIN | BPF_F_GET)
  20. #define BPF_F_KEY (1 << 2)
  21. #define BPF_F_VAL (1 << 3)
  22. #define BPF_F_KEY_VAL (BPF_F_KEY | BPF_F_VAL)
  23. #define BPF_M_UNSPEC 0
  24. #define BPF_M_MAP 1
  25. #define BPF_M_PROG 2
  26. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  27. static void usage(void)
  28. {
  29. printf("Usage: fds_example [...]\n");
  30. printf(" -F <file> File to pin/get object\n");
  31. printf(" -P |- pin object\n");
  32. printf(" -G `- get object\n");
  33. printf(" -m eBPF map mode\n");
  34. printf(" -k <key> |- map key\n");
  35. printf(" -v <value> `- map value\n");
  36. printf(" -p eBPF prog mode\n");
  37. printf(" -o <object> `- object file\n");
  38. printf(" -h Display this help.\n");
  39. }
  40. static int bpf_prog_create(const char *object)
  41. {
  42. static struct bpf_insn insns[] = {
  43. BPF_MOV64_IMM(BPF_REG_0, 1),
  44. BPF_EXIT_INSN(),
  45. };
  46. size_t insns_cnt = ARRAY_SIZE(insns);
  47. struct bpf_object *obj;
  48. int err;
  49. if (object) {
  50. obj = bpf_object__open_file(object, NULL);
  51. assert(!libbpf_get_error(obj));
  52. err = bpf_object__load(obj);
  53. assert(!err);
  54. return bpf_program__fd(bpf_object__next_program(obj, NULL));
  55. } else {
  56. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  57. .log_buf = bpf_log_buf,
  58. .log_size = BPF_LOG_BUF_SIZE,
  59. );
  60. return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
  61. insns, insns_cnt, &opts);
  62. }
  63. }
  64. static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
  65. uint32_t value)
  66. {
  67. int fd, ret;
  68. if (flags & BPF_F_PIN) {
  69. fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(uint32_t),
  70. sizeof(uint32_t), 1024, NULL);
  71. printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
  72. assert(fd > 0);
  73. ret = bpf_obj_pin(fd, file);
  74. printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
  75. assert(ret == 0);
  76. } else {
  77. fd = bpf_obj_get(file);
  78. printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
  79. assert(fd > 0);
  80. }
  81. if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
  82. ret = bpf_map_update_elem(fd, &key, &value, 0);
  83. printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
  84. ret, strerror(errno));
  85. assert(ret == 0);
  86. } else if (flags & BPF_F_KEY) {
  87. ret = bpf_map_lookup_elem(fd, &key, &value);
  88. printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
  89. ret, strerror(errno));
  90. assert(ret == 0);
  91. }
  92. return 0;
  93. }
  94. static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
  95. {
  96. int fd, sock, ret;
  97. if (flags & BPF_F_PIN) {
  98. fd = bpf_prog_create(object);
  99. printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
  100. assert(fd > 0);
  101. ret = bpf_obj_pin(fd, file);
  102. printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
  103. assert(ret == 0);
  104. } else {
  105. fd = bpf_obj_get(file);
  106. printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
  107. assert(fd > 0);
  108. }
  109. sock = open_raw_sock("lo");
  110. assert(sock > 0);
  111. ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
  112. printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
  113. ret, strerror(errno));
  114. assert(ret == 0);
  115. return 0;
  116. }
  117. int main(int argc, char **argv)
  118. {
  119. const char *file = NULL, *object = NULL;
  120. uint32_t key = 0, value = 0, flags = 0;
  121. int opt, mode = BPF_M_UNSPEC;
  122. while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
  123. switch (opt) {
  124. /* General args */
  125. case 'F':
  126. file = optarg;
  127. break;
  128. case 'P':
  129. flags |= BPF_F_PIN;
  130. break;
  131. case 'G':
  132. flags |= BPF_F_GET;
  133. break;
  134. /* Map-related args */
  135. case 'm':
  136. mode = BPF_M_MAP;
  137. break;
  138. case 'k':
  139. key = strtoul(optarg, NULL, 0);
  140. flags |= BPF_F_KEY;
  141. break;
  142. case 'v':
  143. value = strtoul(optarg, NULL, 0);
  144. flags |= BPF_F_VAL;
  145. break;
  146. /* Prog-related args */
  147. case 'p':
  148. mode = BPF_M_PROG;
  149. break;
  150. case 'o':
  151. object = optarg;
  152. break;
  153. default:
  154. goto out;
  155. }
  156. }
  157. if (!(flags & BPF_F_PIN_GET) || !file)
  158. goto out;
  159. switch (mode) {
  160. case BPF_M_MAP:
  161. return bpf_do_map(file, flags, key, value);
  162. case BPF_M_PROG:
  163. return bpf_do_prog(file, flags, object);
  164. }
  165. out:
  166. usage();
  167. return -1;
  168. }