fds_example.c 4.2 KB

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