xdp_tx_iptunnel_user.c 7.5 KB

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