xdp_tx_iptunnel_user.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright (c) 2016 Facebook
  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 <assert.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/resource.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/ether.h>
  17. #include <unistd.h>
  18. #include <time.h>
  19. #include "bpf_load.h"
  20. #include "libbpf.h"
  21. #include "bpf_util.h"
  22. #include "xdp_tx_iptunnel_common.h"
  23. #define STATS_INTERVAL_S 2U
  24. static int ifindex = -1;
  25. static void int_exit(int sig)
  26. {
  27. if (ifindex > -1)
  28. set_link_xdp_fd(ifindex, -1);
  29. exit(0);
  30. }
  31. /* simple per-protocol drop counter
  32. */
  33. static void poll_stats(unsigned int kill_after_s)
  34. {
  35. const unsigned int nr_protos = 256;
  36. unsigned int nr_cpus = bpf_num_possible_cpus();
  37. time_t started_at = time(NULL);
  38. __u64 values[nr_cpus], prev[nr_protos][nr_cpus];
  39. __u32 proto;
  40. int i;
  41. memset(prev, 0, sizeof(prev));
  42. while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
  43. sleep(STATS_INTERVAL_S);
  44. for (proto = 0; proto < nr_protos; proto++) {
  45. __u64 sum = 0;
  46. assert(bpf_map_lookup_elem(map_fd[0], &proto, values) == 0);
  47. for (i = 0; i < nr_cpus; i++)
  48. sum += (values[i] - prev[proto][i]);
  49. if (sum)
  50. printf("proto %u: sum:%10llu pkts, rate:%10llu pkts/s\n",
  51. proto, sum, sum / STATS_INTERVAL_S);
  52. memcpy(prev[proto], values, sizeof(values));
  53. }
  54. }
  55. }
  56. static void usage(const char *cmd)
  57. {
  58. printf("Start a XDP prog which encapsulates incoming packets\n"
  59. "in an IPv4/v6 header and XDP_TX it out. The dst <VIP:PORT>\n"
  60. "is used to select packets to encapsulate\n\n");
  61. printf("Usage: %s [...]\n", cmd);
  62. printf(" -i <ifindex> Interface Index\n");
  63. printf(" -a <vip-service-address> IPv4 or IPv6\n");
  64. printf(" -p <vip-service-port> A port range (e.g. 433-444) is also allowed\n");
  65. printf(" -s <source-ip> Used in the IPTunnel header\n");
  66. printf(" -d <dest-ip> Used in the IPTunnel header\n");
  67. printf(" -m <dest-MAC> Used in sending the IP Tunneled pkt\n");
  68. printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
  69. printf(" -P <IP-Protocol> Default is TCP\n");
  70. printf(" -h Display this help\n");
  71. }
  72. static int parse_ipstr(const char *ipstr, unsigned int *addr)
  73. {
  74. if (inet_pton(AF_INET6, ipstr, addr) == 1) {
  75. return AF_INET6;
  76. } else if (inet_pton(AF_INET, ipstr, addr) == 1) {
  77. addr[1] = addr[2] = addr[3] = 0;
  78. return AF_INET;
  79. }
  80. fprintf(stderr, "%s is an invalid IP\n", ipstr);
  81. return AF_UNSPEC;
  82. }
  83. static int parse_ports(const char *port_str, int *min_port, int *max_port)
  84. {
  85. char *end;
  86. long tmp_min_port;
  87. long tmp_max_port;
  88. tmp_min_port = strtol(optarg, &end, 10);
  89. if (tmp_min_port < 1 || tmp_min_port > 65535) {
  90. fprintf(stderr, "Invalid port(s):%s\n", optarg);
  91. return 1;
  92. }
  93. if (*end == '-') {
  94. end++;
  95. tmp_max_port = strtol(end, NULL, 10);
  96. if (tmp_max_port < 1 || tmp_max_port > 65535) {
  97. fprintf(stderr, "Invalid port(s):%s\n", optarg);
  98. return 1;
  99. }
  100. } else {
  101. tmp_max_port = tmp_min_port;
  102. }
  103. if (tmp_min_port > tmp_max_port) {
  104. fprintf(stderr, "Invalid port(s):%s\n", optarg);
  105. return 1;
  106. }
  107. if (tmp_max_port - tmp_min_port + 1 > MAX_IPTNL_ENTRIES) {
  108. fprintf(stderr, "Port range (%s) is larger than %u\n",
  109. port_str, MAX_IPTNL_ENTRIES);
  110. return 1;
  111. }
  112. *min_port = tmp_min_port;
  113. *max_port = tmp_max_port;
  114. return 0;
  115. }
  116. int main(int argc, char **argv)
  117. {
  118. unsigned char opt_flags[256] = {};
  119. unsigned int kill_after_s = 0;
  120. const char *optstr = "i:a:p:s:d:m:T:P:h";
  121. int min_port = 0, max_port = 0;
  122. struct iptnl_info tnl = {};
  123. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  124. struct vip vip = {};
  125. char filename[256];
  126. int opt;
  127. int i;
  128. tnl.family = AF_UNSPEC;
  129. vip.protocol = IPPROTO_TCP;
  130. for (i = 0; i < strlen(optstr); i++)
  131. if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
  132. opt_flags[(unsigned char)optstr[i]] = 1;
  133. while ((opt = getopt(argc, argv, optstr)) != -1) {
  134. unsigned short family;
  135. unsigned int *v6;
  136. switch (opt) {
  137. case 'i':
  138. ifindex = atoi(optarg);
  139. break;
  140. case 'a':
  141. vip.family = parse_ipstr(optarg, vip.daddr.v6);
  142. if (vip.family == AF_UNSPEC)
  143. return 1;
  144. break;
  145. case 'p':
  146. if (parse_ports(optarg, &min_port, &max_port))
  147. return 1;
  148. break;
  149. case 'P':
  150. vip.protocol = atoi(optarg);
  151. break;
  152. case 's':
  153. case 'd':
  154. if (opt == 's')
  155. v6 = tnl.saddr.v6;
  156. else
  157. v6 = tnl.daddr.v6;
  158. family = parse_ipstr(optarg, v6);
  159. if (family == AF_UNSPEC)
  160. return 1;
  161. if (tnl.family == AF_UNSPEC) {
  162. tnl.family = family;
  163. } else if (tnl.family != family) {
  164. fprintf(stderr,
  165. "The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
  166. return 1;
  167. }
  168. break;
  169. case 'm':
  170. if (!ether_aton_r(optarg,
  171. (struct ether_addr *)tnl.dmac)) {
  172. fprintf(stderr, "Invalid mac address:%s\n",
  173. optarg);
  174. return 1;
  175. }
  176. break;
  177. case 'T':
  178. kill_after_s = atoi(optarg);
  179. break;
  180. default:
  181. usage(argv[0]);
  182. return 1;
  183. }
  184. opt_flags[opt] = 0;
  185. }
  186. for (i = 0; i < strlen(optstr); i++) {
  187. if (opt_flags[(unsigned int)optstr[i]]) {
  188. fprintf(stderr, "Missing argument -%c\n", optstr[i]);
  189. usage(argv[0]);
  190. return 1;
  191. }
  192. }
  193. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  194. perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
  195. return 1;
  196. }
  197. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  198. if (load_bpf_file(filename)) {
  199. printf("%s", bpf_log_buf);
  200. return 1;
  201. }
  202. if (!prog_fd[0]) {
  203. printf("load_bpf_file: %s\n", strerror(errno));
  204. return 1;
  205. }
  206. signal(SIGINT, int_exit);
  207. while (min_port <= max_port) {
  208. vip.dport = htons(min_port++);
  209. if (bpf_map_update_elem(map_fd[1], &vip, &tnl, BPF_NOEXIST)) {
  210. perror("bpf_map_update_elem(&vip2tnl)");
  211. return 1;
  212. }
  213. }
  214. if (set_link_xdp_fd(ifindex, prog_fd[0]) < 0) {
  215. printf("link set xdp fd failed\n");
  216. return 1;
  217. }
  218. poll_stats(kill_after_s);
  219. set_link_xdp_fd(ifindex, -1);
  220. return 0;
  221. }