xdp_sample_pkts_user.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <linux/perf_event.h>
  6. #include <linux/bpf.h>
  7. #include <net/if.h>
  8. #include <errno.h>
  9. #include <assert.h>
  10. #include <sys/sysinfo.h>
  11. #include <sys/ioctl.h>
  12. #include <signal.h>
  13. #include <bpf/libbpf.h>
  14. #include <bpf/bpf.h>
  15. #include <sys/resource.h>
  16. #include <libgen.h>
  17. #include <linux/if_link.h>
  18. #include "perf-sys.h"
  19. static int if_idx;
  20. static char *if_name;
  21. static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
  22. static __u32 prog_id;
  23. static struct perf_buffer *pb = NULL;
  24. static int do_attach(int idx, int fd, const char *name)
  25. {
  26. struct bpf_prog_info info = {};
  27. __u32 info_len = sizeof(info);
  28. int err;
  29. err = bpf_set_link_xdp_fd(idx, fd, xdp_flags);
  30. if (err < 0) {
  31. printf("ERROR: failed to attach program to %s\n", name);
  32. return err;
  33. }
  34. err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
  35. if (err) {
  36. printf("can't get prog info - %s\n", strerror(errno));
  37. return err;
  38. }
  39. prog_id = info.id;
  40. return err;
  41. }
  42. static int do_detach(int idx, const char *name)
  43. {
  44. __u32 curr_prog_id = 0;
  45. int err = 0;
  46. err = bpf_get_link_xdp_id(idx, &curr_prog_id, xdp_flags);
  47. if (err) {
  48. printf("bpf_get_link_xdp_id failed\n");
  49. return err;
  50. }
  51. if (prog_id == curr_prog_id) {
  52. err = bpf_set_link_xdp_fd(idx, -1, xdp_flags);
  53. if (err < 0)
  54. printf("ERROR: failed to detach prog from %s\n", name);
  55. } else if (!curr_prog_id) {
  56. printf("couldn't find a prog id on a %s\n", name);
  57. } else {
  58. printf("program on interface changed, not removing\n");
  59. }
  60. return err;
  61. }
  62. #define SAMPLE_SIZE 64
  63. static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
  64. {
  65. struct {
  66. __u16 cookie;
  67. __u16 pkt_len;
  68. __u8 pkt_data[SAMPLE_SIZE];
  69. } __packed *e = data;
  70. int i;
  71. if (e->cookie != 0xdead) {
  72. printf("BUG cookie %x sized %d\n", e->cookie, size);
  73. return;
  74. }
  75. printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
  76. for (i = 0; i < 14 && i < e->pkt_len; i++)
  77. printf("%02x ", e->pkt_data[i]);
  78. printf("\n");
  79. }
  80. static void sig_handler(int signo)
  81. {
  82. do_detach(if_idx, if_name);
  83. perf_buffer__free(pb);
  84. exit(0);
  85. }
  86. static void usage(const char *prog)
  87. {
  88. fprintf(stderr,
  89. "%s: %s [OPTS] <ifname|ifindex>\n\n"
  90. "OPTS:\n"
  91. " -F force loading prog\n",
  92. __func__, prog);
  93. }
  94. int main(int argc, char **argv)
  95. {
  96. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  97. struct bpf_prog_load_attr prog_load_attr = {
  98. .prog_type = BPF_PROG_TYPE_XDP,
  99. };
  100. struct perf_buffer_opts pb_opts = {};
  101. const char *optstr = "FS";
  102. int prog_fd, map_fd, opt;
  103. struct bpf_object *obj;
  104. struct bpf_map *map;
  105. char filename[256];
  106. int ret, err;
  107. while ((opt = getopt(argc, argv, optstr)) != -1) {
  108. switch (opt) {
  109. case 'F':
  110. xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
  111. break;
  112. case 'S':
  113. xdp_flags |= XDP_FLAGS_SKB_MODE;
  114. break;
  115. default:
  116. usage(basename(argv[0]));
  117. return 1;
  118. }
  119. }
  120. if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
  121. xdp_flags |= XDP_FLAGS_DRV_MODE;
  122. if (optind == argc) {
  123. usage(basename(argv[0]));
  124. return 1;
  125. }
  126. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  127. perror("setrlimit(RLIMIT_MEMLOCK)");
  128. return 1;
  129. }
  130. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  131. prog_load_attr.file = filename;
  132. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  133. return 1;
  134. if (!prog_fd) {
  135. printf("bpf_prog_load_xattr: %s\n", strerror(errno));
  136. return 1;
  137. }
  138. map = bpf_map__next(NULL, obj);
  139. if (!map) {
  140. printf("finding a map in obj file failed\n");
  141. return 1;
  142. }
  143. map_fd = bpf_map__fd(map);
  144. if_idx = if_nametoindex(argv[optind]);
  145. if (!if_idx)
  146. if_idx = strtoul(argv[optind], NULL, 0);
  147. if (!if_idx) {
  148. fprintf(stderr, "Invalid ifname\n");
  149. return 1;
  150. }
  151. if_name = argv[optind];
  152. err = do_attach(if_idx, prog_fd, if_name);
  153. if (err)
  154. return err;
  155. if (signal(SIGINT, sig_handler) ||
  156. signal(SIGHUP, sig_handler) ||
  157. signal(SIGTERM, sig_handler)) {
  158. perror("signal");
  159. return 1;
  160. }
  161. pb_opts.sample_cb = print_bpf_output;
  162. pb = perf_buffer__new(map_fd, 8, &pb_opts);
  163. err = libbpf_get_error(pb);
  164. if (err) {
  165. perror("perf_buffer setup failed");
  166. return 1;
  167. }
  168. while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
  169. }
  170. kill(0, SIGINT);
  171. return ret;
  172. }