xdp_sample_pkts_user.c 3.9 KB

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