uringmachine 0.3 → 0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +2 -1
  3. data/CHANGELOG.md +23 -0
  4. data/README.md +128 -0
  5. data/TODO.md +14 -0
  6. data/examples/bm_snooze.rb +89 -0
  7. data/examples/bm_write.rb +56 -0
  8. data/examples/dns_client.rb +12 -0
  9. data/examples/echo_server.rb +18 -40
  10. data/examples/http_server.rb +42 -43
  11. data/examples/inout.rb +19 -0
  12. data/examples/nc.rb +36 -0
  13. data/examples/server_client.rb +64 -0
  14. data/examples/snooze.rb +44 -0
  15. data/examples/write_dev_null.rb +16 -0
  16. data/ext/um/extconf.rb +24 -23
  17. data/ext/um/um.c +524 -278
  18. data/ext/um/um.h +146 -44
  19. data/ext/um/um_buffer.c +49 -0
  20. data/ext/um/um_class.c +217 -106
  21. data/ext/um/um_const.c +213 -0
  22. data/ext/um/um_ext.c +4 -0
  23. data/ext/um/um_mutex_class.c +47 -0
  24. data/ext/um/um_op.c +86 -114
  25. data/ext/um/um_queue_class.c +58 -0
  26. data/ext/um/um_sync.c +273 -0
  27. data/ext/um/um_utils.c +49 -4
  28. data/lib/uringmachine/dns_resolver.rb +84 -0
  29. data/lib/uringmachine/version.rb +1 -1
  30. data/lib/uringmachine.rb +28 -0
  31. data/supressions/ruby.supp +71 -0
  32. data/test/helper.rb +8 -0
  33. data/test/test_um.rb +685 -46
  34. data/vendor/liburing/.github/workflows/build.yml +29 -1
  35. data/vendor/liburing/.gitignore +6 -0
  36. data/vendor/liburing/CHANGELOG +16 -0
  37. data/vendor/liburing/CONTRIBUTING.md +165 -0
  38. data/vendor/liburing/configure +64 -0
  39. data/vendor/liburing/examples/Makefile +9 -1
  40. data/vendor/liburing/examples/kdigest.c +405 -0
  41. data/vendor/liburing/examples/proxy.c +75 -8
  42. data/vendor/liburing/examples/reg-wait.c +159 -0
  43. data/vendor/liburing/liburing.pc.in +1 -1
  44. data/vendor/liburing/liburing.spec +1 -1
  45. data/vendor/liburing/src/Makefile +16 -2
  46. data/vendor/liburing/src/include/liburing/io_uring.h +77 -0
  47. data/vendor/liburing/src/include/liburing/sanitize.h +39 -0
  48. data/vendor/liburing/src/include/liburing.h +59 -6
  49. data/vendor/liburing/src/int_flags.h +10 -3
  50. data/vendor/liburing/src/liburing-ffi.map +16 -0
  51. data/vendor/liburing/src/liburing.map +10 -0
  52. data/vendor/liburing/src/queue.c +28 -16
  53. data/vendor/liburing/src/register.c +106 -1
  54. data/vendor/liburing/src/sanitize.c +176 -0
  55. data/vendor/liburing/src/setup.c +47 -19
  56. data/vendor/liburing/src/setup.h +6 -0
  57. data/vendor/liburing/test/35fa71a030ca.c +7 -0
  58. data/vendor/liburing/test/500f9fbadef8.c +2 -0
  59. data/vendor/liburing/test/7ad0e4b2f83c.c +0 -25
  60. data/vendor/liburing/test/917257daa0fe.c +7 -0
  61. data/vendor/liburing/test/Makefile +38 -4
  62. data/vendor/liburing/test/a0908ae19763.c +7 -0
  63. data/vendor/liburing/test/a4c0b3decb33.c +7 -0
  64. data/vendor/liburing/test/accept.c +14 -4
  65. data/vendor/liburing/test/b19062a56726.c +7 -0
  66. data/vendor/liburing/test/bind-listen.c +2 -2
  67. data/vendor/liburing/test/buf-ring-nommap.c +10 -3
  68. data/vendor/liburing/test/buf-ring.c +2 -0
  69. data/vendor/liburing/test/cmd-discard.c +427 -0
  70. data/vendor/liburing/test/coredump.c +7 -0
  71. data/vendor/liburing/test/cq-overflow.c +13 -1
  72. data/vendor/liburing/test/d4ae271dfaae.c +11 -3
  73. data/vendor/liburing/test/defer-taskrun.c +2 -2
  74. data/vendor/liburing/test/defer-tw-timeout.c +4 -1
  75. data/vendor/liburing/test/defer.c +2 -2
  76. data/vendor/liburing/test/double-poll-crash.c +1 -1
  77. data/vendor/liburing/test/eeed8b54e0df.c +2 -0
  78. data/vendor/liburing/test/eventfd.c +0 -1
  79. data/vendor/liburing/test/exit-no-cleanup.c +11 -0
  80. data/vendor/liburing/test/fadvise.c +9 -26
  81. data/vendor/liburing/test/fdinfo.c +9 -1
  82. data/vendor/liburing/test/fifo-nonblock-read.c +69 -0
  83. data/vendor/liburing/test/file-exit-unreg.c +48 -0
  84. data/vendor/liburing/test/file-register.c +14 -2
  85. data/vendor/liburing/test/file-update.c +1 -1
  86. data/vendor/liburing/test/file-verify.c +27 -16
  87. data/vendor/liburing/test/files-exit-hang-timeout.c +1 -2
  88. data/vendor/liburing/test/fixed-buf-iter.c +3 -1
  89. data/vendor/liburing/test/fixed-hugepage.c +12 -1
  90. data/vendor/liburing/test/fsnotify.c +1 -0
  91. data/vendor/liburing/test/futex.c +16 -4
  92. data/vendor/liburing/test/helpers.c +47 -0
  93. data/vendor/liburing/test/helpers.h +6 -0
  94. data/vendor/liburing/test/init-mem.c +5 -3
  95. data/vendor/liburing/test/io-cancel.c +0 -24
  96. data/vendor/liburing/test/io_uring_passthrough.c +4 -0
  97. data/vendor/liburing/test/io_uring_register.c +38 -8
  98. data/vendor/liburing/test/iopoll-leak.c +4 -0
  99. data/vendor/liburing/test/iopoll-overflow.c +1 -1
  100. data/vendor/liburing/test/iopoll.c +3 -3
  101. data/vendor/liburing/test/kallsyms.c +203 -0
  102. data/vendor/liburing/test/link-timeout.c +159 -0
  103. data/vendor/liburing/test/linked-defer-close.c +224 -0
  104. data/vendor/liburing/test/madvise.c +12 -25
  105. data/vendor/liburing/test/min-timeout-wait.c +0 -25
  106. data/vendor/liburing/test/min-timeout.c +0 -25
  107. data/vendor/liburing/test/mkdir.c +6 -0
  108. data/vendor/liburing/test/msg-ring.c +8 -2
  109. data/vendor/liburing/test/napi-test.c +16 -3
  110. data/vendor/liburing/test/no-mmap-inval.c +3 -1
  111. data/vendor/liburing/test/nop.c +44 -0
  112. data/vendor/liburing/test/ooo-file-unreg.c +1 -1
  113. data/vendor/liburing/test/open-close.c +40 -0
  114. data/vendor/liburing/test/openat2.c +37 -14
  115. data/vendor/liburing/test/poll-many.c +13 -7
  116. data/vendor/liburing/test/poll-mshot-update.c +17 -10
  117. data/vendor/liburing/test/poll-v-poll.c +6 -3
  118. data/vendor/liburing/test/pollfree.c +148 -0
  119. data/vendor/liburing/test/read-mshot-empty.c +158 -153
  120. data/vendor/liburing/test/read-mshot-stdin.c +121 -0
  121. data/vendor/liburing/test/read-mshot.c +282 -27
  122. data/vendor/liburing/test/read-write.c +78 -13
  123. data/vendor/liburing/test/recv-msgall-stream.c +3 -0
  124. data/vendor/liburing/test/recv-msgall.c +5 -0
  125. data/vendor/liburing/test/recvsend_bundle-inc.c +680 -0
  126. data/vendor/liburing/test/recvsend_bundle.c +94 -31
  127. data/vendor/liburing/test/reg-fd-only.c +15 -5
  128. data/vendor/liburing/test/reg-wait.c +251 -0
  129. data/vendor/liburing/test/regbuf-clone.c +645 -0
  130. data/vendor/liburing/test/regbuf-merge.c +7 -0
  131. data/vendor/liburing/test/register-restrictions.c +86 -85
  132. data/vendor/liburing/test/rename.c +59 -1
  133. data/vendor/liburing/test/resize-rings.c +643 -0
  134. data/vendor/liburing/test/ringbuf-read.c +5 -0
  135. data/vendor/liburing/test/ringbuf-status.c +5 -1
  136. data/vendor/liburing/test/rsrc_tags.c +1 -1
  137. data/vendor/liburing/test/runtests.sh +16 -1
  138. data/vendor/liburing/test/send-zerocopy.c +59 -0
  139. data/vendor/liburing/test/short-read.c +1 -0
  140. data/vendor/liburing/test/socket.c +43 -0
  141. data/vendor/liburing/test/splice.c +3 -1
  142. data/vendor/liburing/test/sq-poll-dup.c +1 -1
  143. data/vendor/liburing/test/sq-poll-share.c +2 -0
  144. data/vendor/liburing/test/sqpoll-disable-exit.c +8 -0
  145. data/vendor/liburing/test/sqpoll-exit-hang.c +1 -25
  146. data/vendor/liburing/test/sqpoll-sleep.c +40 -33
  147. data/vendor/liburing/test/sqwait.c +136 -0
  148. data/vendor/liburing/test/statx.c +89 -0
  149. data/vendor/liburing/test/stdout.c +2 -0
  150. data/vendor/liburing/test/submit-and-wait.c +1 -25
  151. data/vendor/liburing/test/submit-reuse.c +4 -26
  152. data/vendor/liburing/test/symlink.c +12 -1
  153. data/vendor/liburing/test/sync-cancel.c +56 -22
  154. data/vendor/liburing/test/thread-exit.c +5 -0
  155. data/vendor/liburing/test/timeout-new.c +1 -26
  156. data/vendor/liburing/test/timeout.c +25 -34
  157. data/vendor/liburing/test/unlink.c +94 -1
  158. data/vendor/liburing/test/uring_cmd_ublk.c +1252 -0
  159. data/vendor/liburing/test/waitid.c +62 -8
  160. data/vendor/liburing/test/wq-aff.c +35 -0
  161. data/vendor/liburing/test/xfail_prep_link_timeout_out_of_scope.c +46 -0
  162. data/vendor/liburing/test/xfail_register_buffers_out_of_scope.c +51 -0
  163. metadata +37 -6
  164. data/examples/event_loop.rb +0 -69
  165. data/examples/fibers.rb +0 -105
  166. data/examples/http_server_multishot.rb +0 -57
  167. data/examples/http_server_simpler.rb +0 -34
@@ -0,0 +1,405 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Proof-of-concept for doing file digests using the kernel's AF_ALG API.
4
+ * Needs a bit of error handling.
5
+ */
6
+ #include <stdio.h>
7
+ #include <fcntl.h>
8
+ #include <string.h>
9
+ #include <stdlib.h>
10
+ #include <unistd.h>
11
+ #include <assert.h>
12
+ #include <errno.h>
13
+ #include <inttypes.h>
14
+ #include <sys/types.h>
15
+ #include <sys/stat.h>
16
+ #include <sys/ioctl.h>
17
+ #include <linux/if_alg.h>
18
+ #include "liburing.h"
19
+
20
+ #define QD 64
21
+ #define WAIT_BATCH (QD / 8)
22
+ #define BS (64*1024)
23
+
24
+ #define BGID 1
25
+ #define BID_MASK (QD - 1)
26
+
27
+ enum req_state {
28
+ IO_INIT = 0,
29
+ IO_READ,
30
+ IO_READ_COMPLETE,
31
+ IO_WRITE,
32
+ IO_WRITE_COMPLETE,
33
+ };
34
+
35
+ struct req {
36
+ off_t offset;
37
+ enum req_state state;
38
+ struct iovec iov;
39
+ };
40
+
41
+ struct kdigest {
42
+ struct io_uring ring;
43
+ struct io_uring_buf_ring *br;
44
+ struct req reqs[QD];
45
+ /* heap allocated, aligned QD*BS buffer */
46
+ uint8_t *bufs;
47
+ };
48
+
49
+ static int infd, outfd;
50
+
51
+ static int get_file_size(int fd, size_t *size)
52
+ {
53
+ struct stat st;
54
+
55
+ if (fstat(fd, &st) < 0)
56
+ return -1;
57
+ if (S_ISREG(st.st_mode)) {
58
+ *size = st.st_size;
59
+ } else if (S_ISBLK(st.st_mode)) {
60
+ unsigned long long bytes;
61
+
62
+ if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
63
+ return -1;
64
+
65
+ *size = bytes;
66
+ } else {
67
+ return -1;
68
+ }
69
+
70
+ return 0;
71
+ }
72
+
73
+ static int reap_completions(struct io_uring *ring, int *inflight,
74
+ size_t *outsize)
75
+ {
76
+ struct io_uring_cqe *cqe;
77
+ unsigned head;
78
+ int ret = 0, nr;
79
+
80
+ nr = 0;
81
+ io_uring_for_each_cqe(ring, head, cqe) {
82
+ struct req *req;
83
+
84
+ req = io_uring_cqe_get_data(cqe);
85
+ assert(req->state == IO_READ || req->state == IO_WRITE);
86
+ if (cqe->res < 0) {
87
+ fprintf(stderr, "%s: cqe error %d\n",
88
+ req->state == IO_WRITE ? "send" : "read",
89
+ cqe->res);
90
+ *outsize = 0;
91
+ ret = 1;
92
+ break;
93
+ }
94
+
95
+ (*inflight)--;
96
+ req->state++;
97
+ if (req->state == IO_WRITE_COMPLETE)
98
+ *outsize -= cqe->res;
99
+ nr++;
100
+ }
101
+
102
+ io_uring_cq_advance(ring, nr);
103
+ return ret;
104
+ }
105
+
106
+ /*
107
+ * Add buffers to the outgoing ring, and submit a single bundle send that
108
+ * will finish when all of them have completed.
109
+ */
110
+ static void submit_sends_br(struct kdigest *kdigest, int *write_idx,
111
+ int *inflight)
112
+ {
113
+ struct io_uring_buf_ring *br = kdigest->br;
114
+ struct req *req, *first_req = NULL;
115
+ struct io_uring_sqe *sqe;
116
+ int nr = 0;
117
+
118
+ /*
119
+ * Find any completed reads, and add the buffers to the outgoing
120
+ * send ring. That will serialize the data sent.
121
+ */
122
+ while (kdigest->reqs[*write_idx].state == IO_READ_COMPLETE) {
123
+ req = &kdigest->reqs[*write_idx];
124
+ io_uring_buf_ring_add(br, req->iov.iov_base, req->iov.iov_len,
125
+ *write_idx, BID_MASK, nr++);
126
+ /*
127
+ * Mark as a write/send if it's the first one, that serve
128
+ * as the "barrier" in the array. The rest can be marked
129
+ * complete upfront, if there's more in this bundle, as
130
+ * the first will serve a the stopping point.
131
+ */
132
+ if (!first_req) {
133
+ req->state = IO_WRITE;
134
+ first_req = req;
135
+ } else {
136
+ req->state = IO_WRITE_COMPLETE;
137
+ }
138
+ *write_idx = (*write_idx + 1) % QD;
139
+ }
140
+
141
+ /*
142
+ * If any completed reads were found and we added buffers, advance
143
+ * the buffer ring and prepare a single bundle send for all of them.
144
+ */
145
+ if (first_req) {
146
+ io_uring_buf_ring_advance(br, nr);
147
+
148
+ sqe = io_uring_get_sqe(&kdigest->ring);
149
+ io_uring_prep_send_bundle(sqe, outfd, 0, MSG_MORE);
150
+ sqe->flags |= IOSQE_BUFFER_SELECT;
151
+ sqe->buf_group = BGID;
152
+ io_uring_sqe_set_data(sqe, first_req);
153
+ (*inflight)++;
154
+ }
155
+ }
156
+
157
+ /*
158
+ * Serialize multiple writes with IOSQE_IO_LINK. Not the most efficient
159
+ * way, as it's both more expensive on the kernel side to handle link, and
160
+ * if there's bundle support, all of the below can be done with a single
161
+ * send rather than multiple ones.
162
+ */
163
+ static void submit_sends_linked(struct kdigest *kdigest, int *write_idx,
164
+ int *inflight)
165
+ {
166
+ struct io_uring_sqe *sqe;
167
+ struct req *req;
168
+
169
+ /* Queue up any possible writes. Link flag ensures ordering. */
170
+ sqe = NULL;
171
+ while (kdigest->reqs[*write_idx].state == IO_READ_COMPLETE) {
172
+ if (sqe)
173
+ sqe->flags |= IOSQE_IO_LINK;
174
+
175
+ req = &kdigest->reqs[*write_idx];
176
+ req->state = IO_WRITE;
177
+ sqe = io_uring_get_sqe(&kdigest->ring);
178
+ io_uring_prep_send(sqe, outfd, req->iov.iov_base,
179
+ req->iov.iov_len, MSG_MORE);
180
+ io_uring_sqe_set_data(sqe, req);
181
+ (*inflight)++;
182
+
183
+ *write_idx = (*write_idx + 1) % QD;
184
+ }
185
+ }
186
+
187
+ static void submit_sends(struct kdigest *kdigest, int *write_idx, int *inflight)
188
+ {
189
+ if (kdigest->br)
190
+ submit_sends_br(kdigest, write_idx, inflight);
191
+ else
192
+ submit_sends_linked(kdigest, write_idx, inflight);
193
+ }
194
+
195
+ static int digest_file(struct kdigest *kdigest, size_t insize)
196
+ {
197
+ struct io_uring *ring = &kdigest->ring;
198
+ off_t read_off = 0;
199
+ size_t outsize = insize;
200
+ int read_idx = 0, write_idx = 0, inflight = 0;
201
+
202
+ while (outsize) {
203
+ struct io_uring_sqe *sqe;
204
+ struct req *req;
205
+ int to_wait;
206
+
207
+ submit_sends(kdigest, &write_idx, &inflight);
208
+
209
+ /* Queue up any reads. Completions may arrive out of order. */
210
+ while (insize && (kdigest->reqs[read_idx].state == IO_INIT
211
+ || kdigest->reqs[read_idx].state == IO_WRITE_COMPLETE)) {
212
+ size_t this_size = (insize < BS ? insize : BS);
213
+
214
+ req = &kdigest->reqs[read_idx];
215
+ req->state = IO_READ;
216
+ req->offset = read_off;
217
+ req->iov.iov_base = &kdigest->bufs[read_idx * BS];
218
+ req->iov.iov_len = this_size;
219
+
220
+ sqe = io_uring_get_sqe(ring);
221
+ io_uring_prep_read(sqe, infd, req->iov.iov_base,
222
+ req->iov.iov_len, read_off);
223
+ io_uring_sqe_set_data(sqe, req);
224
+
225
+ read_off += this_size;
226
+ insize -= this_size;
227
+ inflight++;
228
+
229
+ read_idx = (read_idx + 1) % QD;
230
+ }
231
+
232
+ /* wait for about half queue completion before resubmit */
233
+ for (to_wait = (inflight >> 1) | 1; to_wait; to_wait--) {
234
+ int ret, wait_nr;
235
+
236
+ wait_nr = inflight;
237
+ if (wait_nr > WAIT_BATCH)
238
+ wait_nr = WAIT_BATCH;
239
+
240
+ ret = io_uring_submit_and_wait(ring, wait_nr);
241
+ if (ret < 0) {
242
+ fprintf(stderr, "wait cqe: %s\n",
243
+ strerror(-ret));
244
+ return 1;
245
+ }
246
+
247
+ if (reap_completions(ring, &inflight, &outsize))
248
+ return 1;
249
+ }
250
+ }
251
+ assert(!inflight);
252
+
253
+ return 0;
254
+ }
255
+
256
+ static int get_result(struct kdigest *kdigest, const char *alg, const char *file)
257
+ {
258
+ struct io_uring *ring = &kdigest->ring;
259
+ struct io_uring_sqe *sqe;
260
+ struct io_uring_cqe *cqe;
261
+ int i, ret;
262
+ /* reuse I/O buf block to stash hash result */
263
+
264
+ sqe = io_uring_get_sqe(ring);
265
+ io_uring_prep_recv(sqe, outfd, kdigest->bufs, BS, 0);
266
+
267
+ if (io_uring_submit_and_wait(ring, 1) < 0)
268
+ return 1;
269
+
270
+ ret = io_uring_peek_cqe(ring, &cqe);
271
+ if (ret < 0) {
272
+ fprintf(stderr, "peek cqe: %s\n", strerror(-ret));
273
+ return 1;
274
+ }
275
+
276
+ if (cqe->res < 0) {
277
+ fprintf(stderr, "cqe error: %s\n", strerror(-cqe->res));
278
+ goto err;
279
+ }
280
+
281
+ fprintf(stdout, "uring %s%s(%s) returned(len=%u): ",
282
+ kdigest->br ? "bundled " : "", alg, file, cqe->res);
283
+ for (i = 0; i < cqe->res; i++)
284
+ fprintf(stdout, "%02x", kdigest->bufs[i]);
285
+ putc('\n', stdout);
286
+ ret = 0;
287
+ err:
288
+ io_uring_cqe_seen(ring, cqe);
289
+ return ret;
290
+ }
291
+
292
+ int main(int argc, char *argv[])
293
+ {
294
+ const char *alg;
295
+ const char *infile;
296
+ size_t alg_len, insize;
297
+ struct sockaddr_alg sa = {
298
+ .salg_family = AF_ALG,
299
+ .salg_type = "hash",
300
+ };
301
+ struct kdigest kdigest = { };
302
+ struct io_uring_params p = { };
303
+ int sfd, ret;
304
+
305
+ if (argc < 3) {
306
+ fprintf(stderr, "%s: algorithm infile\n", argv[0]);
307
+ return 1;
308
+ }
309
+
310
+ alg = argv[1];
311
+ infile = argv[2];
312
+ alg_len = strlen(alg);
313
+ if (alg_len >= sizeof(sa.salg_name)) {
314
+ fprintf(stderr, "algorithm name too long\n");
315
+ return 1;
316
+ }
317
+ /* +1 for null terminator */
318
+ memcpy(sa.salg_name, alg, alg_len + 1);
319
+
320
+ infd = open(infile, O_RDONLY);
321
+ if (infd < 0) {
322
+ perror("open infile");
323
+ return 1;
324
+ }
325
+
326
+ sfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
327
+ if (sfd < 0) {
328
+ if (errno == EAFNOSUPPORT)
329
+ fprintf(stderr, "kernel AF_ALG support not available. "
330
+ "CONFIG_CRYPTO_USER_API_HASH required.\n");
331
+ else
332
+ perror("AF_ALG socket");
333
+ return 1;
334
+ }
335
+
336
+ if (bind(sfd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
337
+ if (errno == ENOENT)
338
+ fprintf(stderr, "AF_ALG bind(%s): hash not available. "
339
+ "See /proc/crypto hash algorithm list.\n",
340
+ alg);
341
+ else
342
+ fprintf(stderr, "AF_ALG bind(%s): %s\n",
343
+ alg, strerror(errno));
344
+ return 1;
345
+ }
346
+
347
+ outfd = accept(sfd, NULL, 0);
348
+ if (outfd < 0) {
349
+ perror("AF_ALG accept");
350
+ return 1;
351
+ }
352
+
353
+ if (posix_memalign((void **)&kdigest.bufs, 4096, QD * BS)) {
354
+ fprintf(stderr, "failed to alloc I/O bufs\n");
355
+ return 1;
356
+ }
357
+
358
+ p.flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
359
+ do {
360
+ ret = io_uring_queue_init_params(QD, &kdigest.ring, &p);
361
+ if (!ret)
362
+ break;
363
+ if (!p.flags) {
364
+ fprintf(stderr, "queue_init: %s\n", strerror(-ret));
365
+ return 1;
366
+ }
367
+ p.flags = 0;
368
+ } while (1);
369
+
370
+ /* use send bundles, if available */
371
+ if (p.features & IORING_FEAT_RECVSEND_BUNDLE) {
372
+ kdigest.br = io_uring_setup_buf_ring(&kdigest.ring, QD, BGID, 0, &ret);
373
+ if (!kdigest.br) {
374
+ fprintf(stderr, "Failed setting up bundle buffer ring: %d\n", ret);
375
+ return 1;
376
+ }
377
+ }
378
+
379
+ if (get_file_size(infd, &insize))
380
+ return 1;
381
+
382
+ ret = digest_file(&kdigest, insize);
383
+ if (ret) {
384
+ fprintf(stderr, "%s digest failed\n", alg);
385
+ return 1;
386
+ }
387
+
388
+ ret = get_result(&kdigest, alg, infile);
389
+ if (ret) {
390
+ fprintf(stderr, "failed to retrieve %s digest result\n", alg);
391
+ return 1;
392
+ }
393
+
394
+ if (kdigest.br)
395
+ io_uring_free_buf_ring(&kdigest.ring, kdigest.br, QD, BGID);
396
+ io_uring_queue_exit(&kdigest.ring);
397
+ free(kdigest.bufs);
398
+ if (close(infd) < 0)
399
+ ret |= 1;
400
+ if (close(sfd) < 0)
401
+ ret |= 1;
402
+ if (close(outfd) < 0)
403
+ ret |= 1;
404
+ return ret;
405
+ }
@@ -82,6 +82,7 @@ static char *host = "192.168.3.2";
82
82
  static int send_port = 4445;
83
83
  static int receive_port = 4444;
84
84
  static int buf_size = 32;
85
+ static int buf_ring_inc;
85
86
  static int bidi;
86
87
  static int ipv6;
87
88
  static int napi;
@@ -356,6 +357,7 @@ static void free_buffer_rings(struct io_uring *ring, struct conn *c)
356
357
  static int setup_recv_ring(struct io_uring *ring, struct conn *c)
357
358
  {
358
359
  struct conn_buf_ring *cbr = &c->in_br;
360
+ int br_flags = 0;
359
361
  int ret, i;
360
362
  size_t len;
361
363
  void *ptr;
@@ -375,7 +377,9 @@ static int setup_recv_ring(struct io_uring *ring, struct conn *c)
375
377
  return 1;
376
378
  }
377
379
  }
378
- cbr->br = io_uring_setup_buf_ring(ring, nr_bufs, cbr->bgid, 0, &ret);
380
+ if (buf_ring_inc)
381
+ br_flags = IOU_PBUF_RING_INC;
382
+ cbr->br = io_uring_setup_buf_ring(ring, nr_bufs, cbr->bgid, br_flags, &ret);
379
383
  if (!cbr->br) {
380
384
  fprintf(stderr, "Buffer ring register failed %d\n", ret);
381
385
  return 1;
@@ -401,9 +405,12 @@ static int setup_recv_ring(struct io_uring *ring, struct conn *c)
401
405
  static int setup_send_ring(struct io_uring *ring, struct conn *c)
402
406
  {
403
407
  struct conn_buf_ring *cbr = &c->out_br;
408
+ int br_flags = 0;
404
409
  int ret;
405
410
 
406
- cbr->br = io_uring_setup_buf_ring(ring, nr_bufs, cbr->bgid, 0, &ret);
411
+ if (buf_ring_inc)
412
+ br_flags = IOU_PBUF_RING_INC;
413
+ cbr->br = io_uring_setup_buf_ring(ring, nr_bufs, cbr->bgid, br_flags, &ret);
407
414
  if (!cbr->br) {
408
415
  fprintf(stderr, "Buffer ring register failed %d\n", ret);
409
416
  return 1;
@@ -1148,6 +1155,32 @@ static int recv_done_res(int res)
1148
1155
  return 0;
1149
1156
  }
1150
1157
 
1158
+ static int recv_inc(struct conn *c, struct conn_dir *cd, int *bid,
1159
+ struct io_uring_cqe *cqe)
1160
+ {
1161
+ struct conn_buf_ring *cbr = &c->out_br;
1162
+ struct conn_buf_ring *in_cbr = &c->in_br;
1163
+ void *data;
1164
+
1165
+ if (!cqe->res)
1166
+ return 0;
1167
+ if (cqe->flags & IORING_CQE_F_BUF_MORE)
1168
+ return 0;
1169
+
1170
+ data = in_cbr->buf + *bid * buf_size;
1171
+ if (is_sink) {
1172
+ io_uring_buf_ring_add(in_cbr->br, data, buf_size, *bid, br_mask, 0);
1173
+ io_uring_buf_ring_advance(in_cbr->br, 1);
1174
+ } else if (send_ring) {
1175
+ io_uring_buf_ring_add(cbr->br, data, buf_size, *bid, br_mask, 0);
1176
+ io_uring_buf_ring_advance(cbr->br, 1);
1177
+ } else {
1178
+ send_append(c, cd, data, *bid, buf_size);
1179
+ }
1180
+ *bid = (*bid + 1) & (nr_bufs - 1);
1181
+ return 1;
1182
+ }
1183
+
1151
1184
  /*
1152
1185
  * Any receive that isn't recvmsg with multishot can be handled the same way.
1153
1186
  * Iterate from '*bid' and 'in_bytes' in total, and append the data to the
@@ -1291,7 +1324,9 @@ start_close:
1291
1324
  * end and the buffer will be replenished once the send is done with
1292
1325
  * it.
1293
1326
  */
1294
- if (is_sink)
1327
+ if (buf_ring_inc)
1328
+ nr_packets = recv_inc(c, ocd, &bid, cqe);
1329
+ else if (is_sink)
1295
1330
  nr_packets = replenish_buffers(c, &bid, cqe->res);
1296
1331
  else if (rcv_msg && recv_mshot)
1297
1332
  nr_packets = recv_mshot_msg(c, ocd, &bid, cqe->res);
@@ -1318,7 +1353,7 @@ start_close:
1318
1353
  cd->pending_recv = 0;
1319
1354
  if (recv_done_res(cqe->res))
1320
1355
  goto start_close;
1321
- if (is_sink)
1356
+ if (is_sink || !ocd->pending_send)
1322
1357
  __submit_receive(ring, c, &c->cd[0], c->in_fd);
1323
1358
  }
1324
1359
 
@@ -1480,12 +1515,39 @@ static int prep_next_send(struct io_uring *ring, struct conn *c,
1480
1515
  }
1481
1516
  }
1482
1517
 
1518
+ static int handle_send_inc(struct conn *c, struct conn_dir *cd, int bid,
1519
+ struct io_uring_cqe *cqe)
1520
+ {
1521
+ struct conn_buf_ring *in_cbr = &c->in_br;
1522
+ int ret = 0;
1523
+ void *data;
1524
+
1525
+ if (!cqe->res)
1526
+ goto out;
1527
+ if (cqe->flags & IORING_CQE_F_BUF_MORE)
1528
+ return 0;
1529
+
1530
+ assert(cqe->res <= buf_size);
1531
+ cd->out_bytes += cqe->res;
1532
+
1533
+ data = in_cbr->buf + bid * buf_size;
1534
+ io_uring_buf_ring_add(in_cbr->br, data, buf_size, bid, br_mask, 0);
1535
+ io_uring_buf_ring_advance(in_cbr->br, 1);
1536
+ bid = (bid + 1) & (nr_bufs - 1);
1537
+ ret = 1;
1538
+ out:
1539
+ if (pending_shutdown(c))
1540
+ close_cd(c, cd);
1541
+
1542
+ return ret;
1543
+ }
1544
+
1483
1545
  /*
1484
1546
  * Handling a send with an outgoing send ring. Get the buffers from the
1485
1547
  * receive side, and add them to the ingoing buffer ring again.
1486
1548
  */
1487
- static int handle_send_ring(struct conn *c, struct conn_dir *cd,
1488
- int bid, int bytes)
1549
+ static int handle_send_ring(struct conn *c, struct conn_dir *cd, int bid,
1550
+ int bytes)
1489
1551
  {
1490
1552
  struct conn_buf_ring *in_cbr = &c->in_br;
1491
1553
  struct conn_buf_ring *out_cbr = &c->out_br;
@@ -1605,7 +1667,9 @@ static int __handle_send(struct io_uring *ring, struct conn *c,
1605
1667
 
1606
1668
  vlog("send: got %d, %lu\n", cqe->res, cd->out_bytes);
1607
1669
 
1608
- if (send_ring)
1670
+ if (buf_ring_inc)
1671
+ nr_packets = handle_send_inc(c, cd, bid, cqe);
1672
+ else if (send_ring)
1609
1673
  nr_packets = handle_send_ring(c, cd, bid, cqe->res);
1610
1674
  else
1611
1675
  nr_packets = handle_send_buf(c, cd, bid, cqe->res);
@@ -2305,7 +2369,7 @@ int main(int argc, char *argv[])
2305
2369
 
2306
2370
  pthread_mutex_init(&thread_lock, NULL);
2307
2371
 
2308
- optstring = "m:d:S:s:b:f:H:r:p:n:B:N:T:w:t:M:R:u:c:C:q:a:x:z:6Vh?";
2372
+ optstring = "m:d:S:s:b:f:H:r:p:n:B:N:T:w:t:M:R:u:c:C:q:a:x:z:i:6Vh?";
2309
2373
  while ((opt = getopt(argc, argv, optstring)) != -1) {
2310
2374
  switch (opt) {
2311
2375
  case 'm':
@@ -2377,6 +2441,9 @@ int main(int argc, char *argv[])
2377
2441
  case 'q':
2378
2442
  ring_size = atoi(optarg);
2379
2443
  break;
2444
+ case 'i':
2445
+ buf_ring_inc = !!atoi(optarg);
2446
+ break;
2380
2447
  case 'a':
2381
2448
  use_huge = !!atoi(optarg);
2382
2449
  break;
@@ -0,0 +1,159 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Sample program that shows how to use registered waits.
4
+ *
5
+ * (C) 2024 Jens Axboe <axboe@kernel.dk>
6
+ */
7
+ #include <stdint.h>
8
+ #include <stdio.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #include <unistd.h>
12
+ #include <assert.h>
13
+ #include <sys/time.h>
14
+ #include <liburing.h>
15
+
16
+ static unsigned long long mtime_since(const struct timeval *s,
17
+ const struct timeval *e)
18
+ {
19
+ long long sec, usec;
20
+
21
+ sec = e->tv_sec - s->tv_sec;
22
+ usec = (e->tv_usec - s->tv_usec);
23
+ if (sec > 0 && usec < 0) {
24
+ sec--;
25
+ usec += 1000000;
26
+ }
27
+
28
+ sec *= 1000;
29
+ usec /= 1000;
30
+ return sec + usec;
31
+ }
32
+
33
+ static unsigned long long mtime_since_now(struct timeval *tv)
34
+ {
35
+ struct timeval end;
36
+
37
+ gettimeofday(&end, NULL);
38
+ return mtime_since(tv, &end);
39
+ }
40
+
41
+ int main(int argc, char *argv[])
42
+ {
43
+ struct io_uring_reg_wait *reg;
44
+ struct io_uring_sqe *sqe;
45
+ struct io_uring_cqe *cqe[2];
46
+ struct io_uring ring;
47
+ char b1[8], b2[8];
48
+ unsigned long msec;
49
+ struct timeval tv;
50
+ int ret, fds[2];
51
+
52
+ if (argc > 1) {
53
+ fprintf(stdout, "%s: takes no arguments\n", argv[0]);
54
+ return 0;
55
+ }
56
+
57
+ if (pipe(fds) < 0) {
58
+ perror("pipe");
59
+ return 1;
60
+ }
61
+
62
+ ret = io_uring_queue_init(8, &ring, 0);
63
+ if (ret) {
64
+ fprintf(stderr, "Queue init: %d\n", ret);
65
+ return 1;
66
+ }
67
+
68
+ /*
69
+ * Setup wait region. We'll use 32 here, but 64 is probably a more
70
+ * logical value, as it'll pin a page regardless of size. 64 is the
71
+ * max value on a 4k page size architecture.
72
+ */
73
+ reg = io_uring_setup_reg_wait(&ring, 32, &ret);
74
+ if (!reg) {
75
+ if (ret == -EINVAL) {
76
+ fprintf(stderr, "Kernel doesn't support registered waits\n");
77
+ return 1;
78
+ }
79
+ fprintf(stderr, "Registered wait: %d\n", ret);
80
+ return 1;
81
+ }
82
+
83
+ /*
84
+ * Setup two distinct wait regions. Index 0 will be a 1 second wait,
85
+ * and region 2 is a short wait using min_wait_usec as well. Neither
86
+ * of these use a signal mask, but sigmask/sigmask_sz can be set as
87
+ * well for that.
88
+ */
89
+ reg[0].ts.tv_sec = 1;
90
+ reg[0].ts.tv_nsec = 0;
91
+ reg[0].flags = IORING_REG_WAIT_TS;
92
+
93
+ reg[1].ts.tv_sec = 0;
94
+ reg[1].ts.tv_nsec = 100000000LL;
95
+ reg[1].min_wait_usec = 10000;
96
+ reg[1].flags = IORING_REG_WAIT_TS;
97
+
98
+ /*
99
+ * No pending completions. Wait with region 0, which should time
100
+ * out after 1 second.
101
+ */
102
+ gettimeofday(&tv, NULL);
103
+ ret = io_uring_submit_and_wait_reg(&ring, cqe, 1, 0);
104
+ if (ret == -EINVAL) {
105
+ fprintf(stderr, "Kernel doesn't support registered waits\n");
106
+ return 1;
107
+ } else if (ret != -ETIME) {
108
+ fprintf(stderr, "Wait should've timed out... %d\n", ret);
109
+ return 1;
110
+ }
111
+ msec = mtime_since_now(&tv);
112
+ if (msec < 900 || msec > 1100) {
113
+ fprintf(stderr, "Wait took an unexpected amount of time: %lu\n",
114
+ msec);
115
+ return 1;
116
+ }
117
+
118
+ /*
119
+ * Now prepare two pipe reads. We'll trigger one completion quickly,
120
+ * but the other one will never happen. Use min_wait_usec timeout
121
+ * to abort after 10 msec of time, where the overall timeout is
122
+ * otherwise 100 msec. Since we're waiting on two events, the min
123
+ * timeout ends up aborting us.
124
+ */
125
+ sqe = io_uring_get_sqe(&ring);
126
+ io_uring_prep_read(sqe, fds[0], b1, sizeof(b1), 0);
127
+ sqe = io_uring_get_sqe(&ring);
128
+ io_uring_prep_read(sqe, fds[0], b2, sizeof(b2), 0);
129
+
130
+ /* trigger one read */
131
+ ret = write(fds[1], "Hello", 5);
132
+ if (ret < 0) {
133
+ perror("write");
134
+ return 1;
135
+ }
136
+
137
+ /*
138
+ * This should will wait for 2 entries, where 1 is already available.
139
+ * Since we're using min_wait_usec == 10 msec here with an overall
140
+ * wait of 100 msec, we expect the wait to abort after 10 msec since
141
+ * one or more events are available.
142
+ */
143
+ gettimeofday(&tv, NULL);
144
+ ret = io_uring_submit_and_wait_reg(&ring, cqe, 2, 1);
145
+ msec = mtime_since_now(&tv);
146
+ if (ret != 2) {
147
+ fprintf(stderr, "Should have submitted 2: %d\n", ret);
148
+ return 1;
149
+ }
150
+ if (msec < 8 || msec > 12)
151
+ fprintf(stderr, "min_wait_usec should take ~10 msec: %lu\n", msec);
152
+
153
+ /*
154
+ * Cleanup after ourselves
155
+ */
156
+ io_uring_queue_exit(&ring);
157
+ io_uring_free_reg_wait(reg, 32);
158
+ return 0;
159
+ }