xdp_redirect_user.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Copyright (c) 2016 John Fastabend <john.r.fastabend@intel.com>
  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. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/bpf.h>
  13. #include <linux/if_link.h>
  14. #include <assert.h>
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <libgen.h>
  23. #include <sys/resource.h>
  24. #include "bpf_load.h"
  25. #include "bpf_util.h"
  26. #include <bpf/bpf.h>
  27. static int ifindex_in;
  28. static int ifindex_out;
  29. static bool ifindex_out_xdp_dummy_attached = true;
  30. static __u32 xdp_flags;
  31. static void int_exit(int sig)
  32. {
  33. bpf_set_link_xdp_fd(ifindex_in, -1, xdp_flags);
  34. if (ifindex_out_xdp_dummy_attached)
  35. bpf_set_link_xdp_fd(ifindex_out, -1, xdp_flags);
  36. exit(0);
  37. }
  38. static void poll_stats(int interval, int ifindex)
  39. {
  40. unsigned int nr_cpus = bpf_num_possible_cpus();
  41. __u64 values[nr_cpus], prev[nr_cpus];
  42. memset(prev, 0, sizeof(prev));
  43. while (1) {
  44. __u64 sum = 0;
  45. __u32 key = 0;
  46. int i;
  47. sleep(interval);
  48. assert(bpf_map_lookup_elem(map_fd[1], &key, values) == 0);
  49. for (i = 0; i < nr_cpus; i++)
  50. sum += (values[i] - prev[i]);
  51. if (sum)
  52. printf("ifindex %i: %10llu pkt/s\n",
  53. ifindex, sum / interval);
  54. memcpy(prev, values, sizeof(values));
  55. }
  56. }
  57. static void usage(const char *prog)
  58. {
  59. fprintf(stderr,
  60. "usage: %s [OPTS] IFINDEX_IN IFINDEX_OUT\n\n"
  61. "OPTS:\n"
  62. " -S use skb-mode\n"
  63. " -N enforce native mode\n",
  64. prog);
  65. }
  66. int main(int argc, char **argv)
  67. {
  68. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  69. const char *optstr = "SN";
  70. char filename[256];
  71. int ret, opt, key = 0;
  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. printf("usage: %s IFINDEX_IN IFINDEX_OUT\n", argv[0]);
  87. return 1;
  88. }
  89. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  90. perror("setrlimit(RLIMIT_MEMLOCK)");
  91. return 1;
  92. }
  93. ifindex_in = strtoul(argv[optind], NULL, 0);
  94. ifindex_out = strtoul(argv[optind + 1], NULL, 0);
  95. printf("input: %d output: %d\n", ifindex_in, ifindex_out);
  96. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  97. if (load_bpf_file(filename)) {
  98. printf("%s", bpf_log_buf);
  99. return 1;
  100. }
  101. if (!prog_fd[0]) {
  102. printf("load_bpf_file: %s\n", strerror(errno));
  103. return 1;
  104. }
  105. if (bpf_set_link_xdp_fd(ifindex_in, prog_fd[0], xdp_flags) < 0) {
  106. printf("ERROR: link set xdp fd failed on %d\n", ifindex_in);
  107. return 1;
  108. }
  109. /* Loading dummy XDP prog on out-device */
  110. if (bpf_set_link_xdp_fd(ifindex_out, prog_fd[1],
  111. (xdp_flags | XDP_FLAGS_UPDATE_IF_NOEXIST)) < 0) {
  112. printf("WARN: link set xdp fd failed on %d\n", ifindex_out);
  113. ifindex_out_xdp_dummy_attached = false;
  114. }
  115. signal(SIGINT, int_exit);
  116. signal(SIGTERM, int_exit);
  117. /* bpf redirect port */
  118. ret = bpf_map_update_elem(map_fd[0], &key, &ifindex_out, 0);
  119. if (ret) {
  120. perror("bpf_update_elem");
  121. goto out;
  122. }
  123. poll_stats(2, ifindex_out);
  124. out:
  125. return 0;
  126. }