xdp_sample_pkts_user.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <libbpf.h>
  14. #include <bpf/bpf.h>
  15. #include "perf-sys.h"
  16. #include "trace_helpers.h"
  17. #define MAX_CPUS 128
  18. static int pmu_fds[MAX_CPUS], if_idx;
  19. static struct perf_event_mmap_page *headers[MAX_CPUS];
  20. static char *if_name;
  21. static int do_attach(int idx, int fd, const char *name)
  22. {
  23. int err;
  24. err = bpf_set_link_xdp_fd(idx, fd, 0);
  25. if (err < 0)
  26. printf("ERROR: failed to attach program to %s\n", name);
  27. return err;
  28. }
  29. static int do_detach(int idx, const char *name)
  30. {
  31. int err;
  32. err = bpf_set_link_xdp_fd(idx, -1, 0);
  33. if (err < 0)
  34. printf("ERROR: failed to detach program from %s\n", name);
  35. return err;
  36. }
  37. #define SAMPLE_SIZE 64
  38. static int print_bpf_output(void *data, int size)
  39. {
  40. struct {
  41. __u16 cookie;
  42. __u16 pkt_len;
  43. __u8 pkt_data[SAMPLE_SIZE];
  44. } __packed *e = data;
  45. int i;
  46. if (e->cookie != 0xdead) {
  47. printf("BUG cookie %x sized %d\n",
  48. e->cookie, size);
  49. return LIBBPF_PERF_EVENT_ERROR;
  50. }
  51. printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
  52. for (i = 0; i < 14 && i < e->pkt_len; i++)
  53. printf("%02x ", e->pkt_data[i]);
  54. printf("\n");
  55. return LIBBPF_PERF_EVENT_CONT;
  56. }
  57. static void test_bpf_perf_event(int map_fd, int num)
  58. {
  59. struct perf_event_attr attr = {
  60. .sample_type = PERF_SAMPLE_RAW,
  61. .type = PERF_TYPE_SOFTWARE,
  62. .config = PERF_COUNT_SW_BPF_OUTPUT,
  63. .wakeup_events = 1, /* get an fd notification for every event */
  64. };
  65. int i;
  66. for (i = 0; i < num; i++) {
  67. int key = i;
  68. pmu_fds[i] = sys_perf_event_open(&attr, -1/*pid*/, i/*cpu*/,
  69. -1/*group_fd*/, 0);
  70. assert(pmu_fds[i] >= 0);
  71. assert(bpf_map_update_elem(map_fd, &key,
  72. &pmu_fds[i], BPF_ANY) == 0);
  73. ioctl(pmu_fds[i], PERF_EVENT_IOC_ENABLE, 0);
  74. }
  75. }
  76. static void sig_handler(int signo)
  77. {
  78. do_detach(if_idx, if_name);
  79. exit(0);
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. struct bpf_prog_load_attr prog_load_attr = {
  84. .prog_type = BPF_PROG_TYPE_XDP,
  85. };
  86. struct bpf_object *obj;
  87. struct bpf_map *map;
  88. int prog_fd, map_fd;
  89. char filename[256];
  90. int ret, err, i;
  91. int numcpus;
  92. if (argc < 2) {
  93. printf("Usage: %s <ifname>\n", argv[0]);
  94. return 1;
  95. }
  96. numcpus = get_nprocs();
  97. if (numcpus > MAX_CPUS)
  98. numcpus = MAX_CPUS;
  99. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  100. prog_load_attr.file = filename;
  101. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  102. return 1;
  103. if (!prog_fd) {
  104. printf("load_bpf_file: %s\n", strerror(errno));
  105. return 1;
  106. }
  107. map = bpf_map__next(NULL, obj);
  108. if (!map) {
  109. printf("finding a map in obj file failed\n");
  110. return 1;
  111. }
  112. map_fd = bpf_map__fd(map);
  113. if_idx = if_nametoindex(argv[1]);
  114. if (!if_idx)
  115. if_idx = strtoul(argv[1], NULL, 0);
  116. if (!if_idx) {
  117. fprintf(stderr, "Invalid ifname\n");
  118. return 1;
  119. }
  120. if_name = argv[1];
  121. err = do_attach(if_idx, prog_fd, argv[1]);
  122. if (err)
  123. return err;
  124. if (signal(SIGINT, sig_handler) ||
  125. signal(SIGHUP, sig_handler) ||
  126. signal(SIGTERM, sig_handler)) {
  127. perror("signal");
  128. return 1;
  129. }
  130. test_bpf_perf_event(map_fd, numcpus);
  131. for (i = 0; i < numcpus; i++)
  132. if (perf_event_mmap_header(pmu_fds[i], &headers[i]) < 0)
  133. return 1;
  134. ret = perf_event_poller_multi(pmu_fds, headers, numcpus,
  135. print_bpf_output);
  136. kill(0, SIGINT);
  137. return ret;
  138. }