xdp1_user.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "bpf_load.h"
  18. #include "bpf_util.h"
  19. #include "libbpf.h"
  20. static int ifindex;
  21. static __u32 xdp_flags;
  22. static void int_exit(int sig)
  23. {
  24. set_link_xdp_fd(ifindex, -1, xdp_flags);
  25. exit(0);
  26. }
  27. /* simple per-protocol drop counter
  28. */
  29. static void poll_stats(int interval)
  30. {
  31. unsigned int nr_cpus = bpf_num_possible_cpus();
  32. const unsigned int nr_keys = 256;
  33. __u64 values[nr_cpus], prev[nr_keys][nr_cpus];
  34. __u32 key;
  35. int i;
  36. memset(prev, 0, sizeof(prev));
  37. while (1) {
  38. sleep(interval);
  39. for (key = 0; key < nr_keys; key++) {
  40. __u64 sum = 0;
  41. assert(bpf_map_lookup_elem(map_fd[0], &key, values) == 0);
  42. for (i = 0; i < nr_cpus; i++)
  43. sum += (values[i] - prev[key][i]);
  44. if (sum)
  45. printf("proto %u: %10llu pkt/s\n",
  46. key, sum / interval);
  47. memcpy(prev[key], values, sizeof(values));
  48. }
  49. }
  50. }
  51. static void usage(const char *prog)
  52. {
  53. fprintf(stderr,
  54. "usage: %s [OPTS] IFINDEX\n\n"
  55. "OPTS:\n"
  56. " -S use skb-mode\n"
  57. " -N enforce native mode\n",
  58. prog);
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. const char *optstr = "SN";
  63. char filename[256];
  64. int opt;
  65. while ((opt = getopt(argc, argv, optstr)) != -1) {
  66. switch (opt) {
  67. case 'S':
  68. xdp_flags |= XDP_FLAGS_SKB_MODE;
  69. break;
  70. case 'N':
  71. xdp_flags |= XDP_FLAGS_DRV_MODE;
  72. break;
  73. default:
  74. usage(basename(argv[0]));
  75. return 1;
  76. }
  77. }
  78. if (optind == argc) {
  79. usage(basename(argv[0]));
  80. return 1;
  81. }
  82. ifindex = strtoul(argv[optind], NULL, 0);
  83. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  84. if (load_bpf_file(filename)) {
  85. printf("%s", bpf_log_buf);
  86. return 1;
  87. }
  88. if (!prog_fd[0]) {
  89. printf("load_bpf_file: %s\n", strerror(errno));
  90. return 1;
  91. }
  92. signal(SIGINT, int_exit);
  93. signal(SIGTERM, int_exit);
  94. if (set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
  95. printf("link set xdp fd failed\n");
  96. return 1;
  97. }
  98. poll_stats(2);
  99. return 0;
  100. }