xdping.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. */
  3. #include <linux/bpf.h>
  4. #include <linux/if_link.h>
  5. #include <arpa/inet.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 <unistd.h>
  13. #include <libgen.h>
  14. #include <net/if.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netdb.h>
  18. #include "bpf/bpf.h"
  19. #include "bpf/libbpf.h"
  20. #include "xdping.h"
  21. #include "testing_helpers.h"
  22. static int ifindex;
  23. static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
  24. static void cleanup(int sig)
  25. {
  26. bpf_xdp_detach(ifindex, xdp_flags, NULL);
  27. if (sig)
  28. exit(1);
  29. }
  30. static int get_stats(int fd, __u16 count, __u32 raddr)
  31. {
  32. struct pinginfo pinginfo = { 0 };
  33. char inaddrbuf[INET_ADDRSTRLEN];
  34. struct in_addr inaddr;
  35. __u16 i;
  36. inaddr.s_addr = raddr;
  37. printf("\nXDP RTT data:\n");
  38. if (bpf_map_lookup_elem(fd, &raddr, &pinginfo)) {
  39. perror("bpf_map_lookup elem");
  40. return 1;
  41. }
  42. for (i = 0; i < count; i++) {
  43. if (pinginfo.times[i] == 0)
  44. break;
  45. printf("64 bytes from %s: icmp_seq=%d ttl=64 time=%#.5f ms\n",
  46. inet_ntop(AF_INET, &inaddr, inaddrbuf,
  47. sizeof(inaddrbuf)),
  48. count + i + 1,
  49. (double)pinginfo.times[i]/1000000);
  50. }
  51. if (i < count) {
  52. fprintf(stderr, "Expected %d samples, got %d.\n", count, i);
  53. return 1;
  54. }
  55. bpf_map_delete_elem(fd, &raddr);
  56. return 0;
  57. }
  58. static void show_usage(const char *prog)
  59. {
  60. fprintf(stderr,
  61. "usage: %s [OPTS] -I interface destination\n\n"
  62. "OPTS:\n"
  63. " -c count Stop after sending count requests\n"
  64. " (default %d, max %d)\n"
  65. " -I interface interface name\n"
  66. " -N Run in driver mode\n"
  67. " -s Server mode\n"
  68. " -S Run in skb mode\n",
  69. prog, XDPING_DEFAULT_COUNT, XDPING_MAX_COUNT);
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. __u32 mode_flags = XDP_FLAGS_DRV_MODE | XDP_FLAGS_SKB_MODE;
  74. struct addrinfo *a, hints = { .ai_family = AF_INET };
  75. __u16 count = XDPING_DEFAULT_COUNT;
  76. struct pinginfo pinginfo = { 0 };
  77. const char *optstr = "c:I:NsS";
  78. struct bpf_program *main_prog;
  79. int prog_fd = -1, map_fd = -1;
  80. struct sockaddr_in rin;
  81. struct bpf_object *obj;
  82. struct bpf_map *map;
  83. char *ifname = NULL;
  84. char filename[256];
  85. int opt, ret = 1;
  86. __u32 raddr = 0;
  87. int server = 0;
  88. char cmd[256];
  89. while ((opt = getopt(argc, argv, optstr)) != -1) {
  90. switch (opt) {
  91. case 'c':
  92. count = atoi(optarg);
  93. if (count < 1 || count > XDPING_MAX_COUNT) {
  94. fprintf(stderr,
  95. "min count is 1, max count is %d\n",
  96. XDPING_MAX_COUNT);
  97. return 1;
  98. }
  99. break;
  100. case 'I':
  101. ifname = optarg;
  102. ifindex = if_nametoindex(ifname);
  103. if (!ifindex) {
  104. fprintf(stderr, "Could not get interface %s\n",
  105. ifname);
  106. return 1;
  107. }
  108. break;
  109. case 'N':
  110. xdp_flags |= XDP_FLAGS_DRV_MODE;
  111. break;
  112. case 's':
  113. /* use server program */
  114. server = 1;
  115. break;
  116. case 'S':
  117. xdp_flags |= XDP_FLAGS_SKB_MODE;
  118. break;
  119. default:
  120. show_usage(basename(argv[0]));
  121. return 1;
  122. }
  123. }
  124. if (!ifname) {
  125. show_usage(basename(argv[0]));
  126. return 1;
  127. }
  128. if (!server && optind == argc) {
  129. show_usage(basename(argv[0]));
  130. return 1;
  131. }
  132. if ((xdp_flags & mode_flags) == mode_flags) {
  133. fprintf(stderr, "-N or -S can be specified, not both.\n");
  134. show_usage(basename(argv[0]));
  135. return 1;
  136. }
  137. if (!server) {
  138. /* Only supports IPv4; see hints initiailization above. */
  139. if (getaddrinfo(argv[optind], NULL, &hints, &a) || !a) {
  140. fprintf(stderr, "Could not resolve %s\n", argv[optind]);
  141. return 1;
  142. }
  143. memcpy(&rin, a->ai_addr, sizeof(rin));
  144. raddr = rin.sin_addr.s_addr;
  145. freeaddrinfo(a);
  146. }
  147. /* Use libbpf 1.0 API mode */
  148. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  149. snprintf(filename, sizeof(filename), "%s_kern.bpf.o", argv[0]);
  150. if (bpf_prog_test_load(filename, BPF_PROG_TYPE_XDP, &obj, &prog_fd)) {
  151. fprintf(stderr, "load of %s failed\n", filename);
  152. return 1;
  153. }
  154. main_prog = bpf_object__find_program_by_name(obj,
  155. server ? "xdping_server" : "xdping_client");
  156. if (main_prog)
  157. prog_fd = bpf_program__fd(main_prog);
  158. if (!main_prog || prog_fd < 0) {
  159. fprintf(stderr, "could not find xdping program");
  160. return 1;
  161. }
  162. map = bpf_object__next_map(obj, NULL);
  163. if (map)
  164. map_fd = bpf_map__fd(map);
  165. if (!map || map_fd < 0) {
  166. fprintf(stderr, "Could not find ping map");
  167. goto done;
  168. }
  169. signal(SIGINT, cleanup);
  170. signal(SIGTERM, cleanup);
  171. printf("Setting up XDP for %s, please wait...\n", ifname);
  172. printf("XDP setup disrupts network connectivity, hit Ctrl+C to quit\n");
  173. if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
  174. fprintf(stderr, "Link set xdp fd failed for %s\n", ifname);
  175. goto done;
  176. }
  177. if (server) {
  178. close(prog_fd);
  179. close(map_fd);
  180. printf("Running server on %s; press Ctrl+C to exit...\n",
  181. ifname);
  182. do { } while (1);
  183. }
  184. /* Start xdping-ing from last regular ping reply, e.g. for a count
  185. * of 10 ICMP requests, we start xdping-ing using reply with seq number
  186. * 10. The reason the last "real" ping RTT is much higher is that
  187. * the ping program sees the ICMP reply associated with the last
  188. * XDP-generated packet, so ping doesn't get a reply until XDP is done.
  189. */
  190. pinginfo.seq = htons(count);
  191. pinginfo.count = count;
  192. if (bpf_map_update_elem(map_fd, &raddr, &pinginfo, BPF_ANY)) {
  193. fprintf(stderr, "could not communicate with BPF map: %s\n",
  194. strerror(errno));
  195. cleanup(0);
  196. goto done;
  197. }
  198. /* We need to wait for XDP setup to complete. */
  199. sleep(10);
  200. snprintf(cmd, sizeof(cmd), "ping -c %d -I %s %s",
  201. count, ifname, argv[optind]);
  202. printf("\nNormal ping RTT data\n");
  203. printf("[Ignore final RTT; it is distorted by XDP using the reply]\n");
  204. ret = system(cmd);
  205. if (!ret)
  206. ret = get_stats(map_fd, count, raddr);
  207. cleanup(0);
  208. done:
  209. if (prog_fd > 0)
  210. close(prog_fd);
  211. if (map_fd > 0)
  212. close(map_fd);
  213. return ret;
  214. }