io_uring_zerocopy_tx.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* SPDX-License-Identifier: MIT */
  2. /* based on linux-kernel/tools/testing/selftests/net/msg_zerocopy.c */
  3. #include <assert.h>
  4. #include <errno.h>
  5. #include <error.h>
  6. #include <fcntl.h>
  7. #include <limits.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <arpa/inet.h>
  15. #include <linux/errqueue.h>
  16. #include <linux/if_packet.h>
  17. #include <linux/io_uring.h>
  18. #include <linux/ipv6.h>
  19. #include <linux/socket.h>
  20. #include <linux/sockios.h>
  21. #include <net/ethernet.h>
  22. #include <net/if.h>
  23. #include <netinet/in.h>
  24. #include <netinet/ip.h>
  25. #include <netinet/ip6.h>
  26. #include <netinet/tcp.h>
  27. #include <netinet/udp.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/mman.h>
  30. #include <sys/resource.h>
  31. #include <sys/socket.h>
  32. #include <sys/stat.h>
  33. #include <sys/time.h>
  34. #include <sys/types.h>
  35. #include <sys/un.h>
  36. #include <sys/wait.h>
  37. #include <io_uring/mini_liburing.h>
  38. #define NOTIF_TAG 0xfffffffULL
  39. #define NONZC_TAG 0
  40. #define ZC_TAG 1
  41. enum {
  42. MODE_NONZC = 0,
  43. MODE_ZC = 1,
  44. MODE_ZC_FIXED = 2,
  45. MODE_MIXED = 3,
  46. };
  47. static bool cfg_cork = false;
  48. static int cfg_mode = MODE_ZC_FIXED;
  49. static int cfg_nr_reqs = 8;
  50. static int cfg_family = PF_UNSPEC;
  51. static int cfg_payload_len;
  52. static int cfg_port = 8000;
  53. static int cfg_runtime_ms = 4200;
  54. static socklen_t cfg_alen;
  55. static struct sockaddr_storage cfg_dst_addr;
  56. static char payload[IP_MAXPACKET] __attribute__((aligned(4096)));
  57. static unsigned long gettimeofday_ms(void)
  58. {
  59. struct timeval tv;
  60. gettimeofday(&tv, NULL);
  61. return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
  62. }
  63. static void do_setsockopt(int fd, int level, int optname, int val)
  64. {
  65. if (setsockopt(fd, level, optname, &val, sizeof(val)))
  66. error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
  67. }
  68. static int do_setup_tx(int domain, int type, int protocol)
  69. {
  70. int fd;
  71. fd = socket(domain, type, protocol);
  72. if (fd == -1)
  73. error(1, errno, "socket t");
  74. do_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 1 << 21);
  75. if (connect(fd, (void *) &cfg_dst_addr, cfg_alen))
  76. error(1, errno, "connect");
  77. return fd;
  78. }
  79. static void do_tx(int domain, int type, int protocol)
  80. {
  81. struct io_uring_sqe *sqe;
  82. struct io_uring_cqe *cqe;
  83. unsigned long packets = 0, bytes = 0;
  84. struct io_uring ring;
  85. struct iovec iov;
  86. uint64_t tstop;
  87. int i, fd, ret;
  88. int compl_cqes = 0;
  89. fd = do_setup_tx(domain, type, protocol);
  90. ret = io_uring_queue_init(512, &ring, 0);
  91. if (ret)
  92. error(1, -ret, "io_uring: queue init");
  93. iov.iov_base = payload;
  94. iov.iov_len = cfg_payload_len;
  95. ret = io_uring_register_buffers(&ring, &iov, 1);
  96. if (ret)
  97. error(1, -ret, "io_uring: buffer registration");
  98. tstop = gettimeofday_ms() + cfg_runtime_ms;
  99. do {
  100. if (cfg_cork)
  101. do_setsockopt(fd, IPPROTO_UDP, UDP_CORK, 1);
  102. for (i = 0; i < cfg_nr_reqs; i++) {
  103. unsigned zc_flags = 0;
  104. unsigned buf_idx = 0;
  105. unsigned mode = cfg_mode;
  106. unsigned msg_flags = MSG_WAITALL;
  107. if (cfg_mode == MODE_MIXED)
  108. mode = rand() % 3;
  109. sqe = io_uring_get_sqe(&ring);
  110. if (mode == MODE_NONZC) {
  111. io_uring_prep_send(sqe, fd, payload,
  112. cfg_payload_len, msg_flags);
  113. sqe->user_data = NONZC_TAG;
  114. } else {
  115. io_uring_prep_sendzc(sqe, fd, payload,
  116. cfg_payload_len,
  117. msg_flags, zc_flags);
  118. if (mode == MODE_ZC_FIXED) {
  119. sqe->ioprio |= IORING_RECVSEND_FIXED_BUF;
  120. sqe->buf_index = buf_idx;
  121. }
  122. sqe->user_data = ZC_TAG;
  123. }
  124. }
  125. ret = io_uring_submit(&ring);
  126. if (ret != cfg_nr_reqs)
  127. error(1, -ret, "submit");
  128. if (cfg_cork)
  129. do_setsockopt(fd, IPPROTO_UDP, UDP_CORK, 0);
  130. for (i = 0; i < cfg_nr_reqs; i++) {
  131. ret = io_uring_wait_cqe(&ring, &cqe);
  132. if (ret)
  133. error(1, -ret, "wait cqe");
  134. if (cqe->user_data != NONZC_TAG &&
  135. cqe->user_data != ZC_TAG)
  136. error(1, EINVAL, "invalid cqe->user_data");
  137. if (cqe->flags & IORING_CQE_F_NOTIF) {
  138. if (cqe->flags & IORING_CQE_F_MORE)
  139. error(1, EINVAL, "invalid notif flags");
  140. if (compl_cqes <= 0)
  141. error(1, EINVAL, "notification mismatch");
  142. compl_cqes--;
  143. i--;
  144. io_uring_cqe_seen(&ring);
  145. continue;
  146. }
  147. if (cqe->flags & IORING_CQE_F_MORE) {
  148. if (cqe->user_data != ZC_TAG)
  149. error(1, -cqe->res, "unexpected F_MORE");
  150. compl_cqes++;
  151. }
  152. if (cqe->res >= 0) {
  153. packets++;
  154. bytes += cqe->res;
  155. } else if (cqe->res != -EAGAIN) {
  156. error(1, -cqe->res, "send failed");
  157. }
  158. io_uring_cqe_seen(&ring);
  159. }
  160. } while (gettimeofday_ms() < tstop);
  161. while (compl_cqes) {
  162. ret = io_uring_wait_cqe(&ring, &cqe);
  163. if (ret)
  164. error(1, -ret, "wait cqe");
  165. if (cqe->flags & IORING_CQE_F_MORE)
  166. error(1, EINVAL, "invalid notif flags");
  167. if (!(cqe->flags & IORING_CQE_F_NOTIF))
  168. error(1, EINVAL, "missing notif flag");
  169. io_uring_cqe_seen(&ring);
  170. compl_cqes--;
  171. }
  172. fprintf(stderr, "tx=%lu (MB=%lu), tx/s=%lu (MB/s=%lu)\n",
  173. packets, bytes >> 20,
  174. packets / (cfg_runtime_ms / 1000),
  175. (bytes >> 20) / (cfg_runtime_ms / 1000));
  176. if (close(fd))
  177. error(1, errno, "close");
  178. }
  179. static void do_test(int domain, int type, int protocol)
  180. {
  181. int i;
  182. for (i = 0; i < IP_MAXPACKET; i++)
  183. payload[i] = 'a' + (i % 26);
  184. do_tx(domain, type, protocol);
  185. }
  186. static void usage(const char *filepath)
  187. {
  188. error(1, 0, "Usage: %s (-4|-6) (udp|tcp) -D<dst_ip> [-s<payload size>] "
  189. "[-t<time s>] [-n<batch>] [-p<port>] [-m<mode>]", filepath);
  190. }
  191. static void parse_opts(int argc, char **argv)
  192. {
  193. const int max_payload_len = sizeof(payload) -
  194. sizeof(struct ipv6hdr) -
  195. sizeof(struct tcphdr) -
  196. 40 /* max tcp options */;
  197. struct sockaddr_in6 *addr6 = (void *) &cfg_dst_addr;
  198. struct sockaddr_in *addr4 = (void *) &cfg_dst_addr;
  199. char *daddr = NULL;
  200. int c;
  201. if (argc <= 1)
  202. usage(argv[0]);
  203. cfg_payload_len = max_payload_len;
  204. while ((c = getopt(argc, argv, "46D:p:s:t:n:c:m:")) != -1) {
  205. switch (c) {
  206. case '4':
  207. if (cfg_family != PF_UNSPEC)
  208. error(1, 0, "Pass one of -4 or -6");
  209. cfg_family = PF_INET;
  210. cfg_alen = sizeof(struct sockaddr_in);
  211. break;
  212. case '6':
  213. if (cfg_family != PF_UNSPEC)
  214. error(1, 0, "Pass one of -4 or -6");
  215. cfg_family = PF_INET6;
  216. cfg_alen = sizeof(struct sockaddr_in6);
  217. break;
  218. case 'D':
  219. daddr = optarg;
  220. break;
  221. case 'p':
  222. cfg_port = strtoul(optarg, NULL, 0);
  223. break;
  224. case 's':
  225. cfg_payload_len = strtoul(optarg, NULL, 0);
  226. break;
  227. case 't':
  228. cfg_runtime_ms = 200 + strtoul(optarg, NULL, 10) * 1000;
  229. break;
  230. case 'n':
  231. cfg_nr_reqs = strtoul(optarg, NULL, 0);
  232. break;
  233. case 'c':
  234. cfg_cork = strtol(optarg, NULL, 0);
  235. break;
  236. case 'm':
  237. cfg_mode = strtol(optarg, NULL, 0);
  238. break;
  239. }
  240. }
  241. switch (cfg_family) {
  242. case PF_INET:
  243. memset(addr4, 0, sizeof(*addr4));
  244. addr4->sin_family = AF_INET;
  245. addr4->sin_port = htons(cfg_port);
  246. if (daddr &&
  247. inet_pton(AF_INET, daddr, &(addr4->sin_addr)) != 1)
  248. error(1, 0, "ipv4 parse error: %s", daddr);
  249. break;
  250. case PF_INET6:
  251. memset(addr6, 0, sizeof(*addr6));
  252. addr6->sin6_family = AF_INET6;
  253. addr6->sin6_port = htons(cfg_port);
  254. if (daddr &&
  255. inet_pton(AF_INET6, daddr, &(addr6->sin6_addr)) != 1)
  256. error(1, 0, "ipv6 parse error: %s", daddr);
  257. break;
  258. default:
  259. error(1, 0, "illegal domain");
  260. }
  261. if (cfg_payload_len > max_payload_len)
  262. error(1, 0, "-s: payload exceeds max (%d)", max_payload_len);
  263. if (optind != argc - 1)
  264. usage(argv[0]);
  265. }
  266. int main(int argc, char **argv)
  267. {
  268. const char *cfg_test = argv[argc - 1];
  269. parse_opts(argc, argv);
  270. if (!strcmp(cfg_test, "tcp"))
  271. do_test(cfg_family, SOCK_STREAM, 0);
  272. else if (!strcmp(cfg_test, "udp"))
  273. do_test(cfg_family, SOCK_DGRAM, 0);
  274. else
  275. error(1, 0, "unknown cfg_test %s", cfg_test);
  276. return 0;
  277. }