xdp_tx_iptunnel_user.c 6.3 KB

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