xdp1_user.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Copyright (c) 2016 PLUMgrid
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/bpf.h>
  8. #include <linux/if_link.h>
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <signal.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <libgen.h>
  17. #include <sys/resource.h>
  18. #include "bpf_util.h"
  19. #include "bpf/bpf.h"
  20. #include "bpf/libbpf.h"
  21. static int ifindex;
  22. static __u32 xdp_flags;
  23. static void int_exit(int sig)
  24. {
  25. bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
  26. exit(0);
  27. }
  28. /* simple per-protocol drop counter
  29. */
  30. static void poll_stats(int map_fd, int interval)
  31. {
  32. unsigned int nr_cpus = bpf_num_possible_cpus();
  33. const unsigned int nr_keys = 256;
  34. __u64 values[nr_cpus], prev[nr_keys][nr_cpus];
  35. __u32 key;
  36. int i;
  37. memset(prev, 0, sizeof(prev));
  38. while (1) {
  39. sleep(interval);
  40. for (key = 0; key < nr_keys; key++) {
  41. __u64 sum = 0;
  42. assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
  43. for (i = 0; i < nr_cpus; i++)
  44. sum += (values[i] - prev[key][i]);
  45. if (sum)
  46. printf("proto %u: %10llu pkt/s\n",
  47. key, sum / interval);
  48. memcpy(prev[key], values, sizeof(values));
  49. }
  50. }
  51. }
  52. static void usage(const char *prog)
  53. {
  54. fprintf(stderr,
  55. "usage: %s [OPTS] IFINDEX\n\n"
  56. "OPTS:\n"
  57. " -S use skb-mode\n"
  58. " -N enforce native mode\n",
  59. prog);
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  64. struct bpf_prog_load_attr prog_load_attr = {
  65. .prog_type = BPF_PROG_TYPE_XDP,
  66. };
  67. const char *optstr = "SN";
  68. int prog_fd, map_fd, opt;
  69. struct bpf_object *obj;
  70. struct bpf_map *map;
  71. char filename[256];
  72. while ((opt = getopt(argc, argv, optstr)) != -1) {
  73. switch (opt) {
  74. case 'S':
  75. xdp_flags |= XDP_FLAGS_SKB_MODE;
  76. break;
  77. case 'N':
  78. xdp_flags |= XDP_FLAGS_DRV_MODE;
  79. break;
  80. default:
  81. usage(basename(argv[0]));
  82. return 1;
  83. }
  84. }
  85. if (optind == argc) {
  86. usage(basename(argv[0]));
  87. return 1;
  88. }
  89. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  90. perror("setrlimit(RLIMIT_MEMLOCK)");
  91. return 1;
  92. }
  93. ifindex = strtoul(argv[optind], NULL, 0);
  94. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  95. prog_load_attr.file = filename;
  96. if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
  97. return 1;
  98. map = bpf_map__next(NULL, obj);
  99. if (!map) {
  100. printf("finding a map in obj file failed\n");
  101. return 1;
  102. }
  103. map_fd = bpf_map__fd(map);
  104. if (!prog_fd) {
  105. printf("load_bpf_file: %s\n", strerror(errno));
  106. return 1;
  107. }
  108. signal(SIGINT, int_exit);
  109. signal(SIGTERM, int_exit);
  110. if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
  111. printf("link set xdp fd failed\n");
  112. return 1;
  113. }
  114. poll_stats(map_fd, 2);
  115. return 0;
  116. }