xdp_tx_iptunnel_user.c 7.4 KB

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