uringmachine 0.10 → 0.11

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 (80) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/examples/bm_side_running.rb +83 -0
  4. data/examples/bm_sqlite.rb +1 -1
  5. data/ext/um/um.c +17 -1
  6. data/ext/um/um.h +29 -0
  7. data/ext/um/um_ext.c +2 -0
  8. data/ext/um/um_stream.c +344 -0
  9. data/ext/um/um_stream_class.c +140 -0
  10. data/lib/uringmachine/version.rb +1 -1
  11. data/lib/uringmachine.rb +20 -16
  12. data/test/test_stream.rb +133 -0
  13. data/test/test_um.rb +63 -0
  14. data/vendor/liburing/.github/workflows/{build.yml → ci.yml} +107 -42
  15. data/vendor/liburing/.gitignore +1 -0
  16. data/vendor/liburing/CHANGELOG +10 -0
  17. data/vendor/liburing/README +5 -0
  18. data/vendor/liburing/configure +1 -1
  19. data/vendor/liburing/examples/Makefile +1 -0
  20. data/vendor/liburing/examples/helpers.c +25 -0
  21. data/vendor/liburing/examples/helpers.h +13 -0
  22. data/vendor/liburing/examples/io_uring-test.c +3 -0
  23. data/vendor/liburing/examples/proxy.c +1 -1
  24. data/vendor/liburing/examples/reg-wait.c +41 -6
  25. data/vendor/liburing/examples/send-zerocopy.c +79 -32
  26. data/vendor/liburing/examples/zcrx.c +436 -0
  27. data/vendor/liburing/liburing.spec +1 -1
  28. data/vendor/liburing/src/Makefile +0 -1
  29. data/vendor/liburing/src/arch/generic/syscall.h +2 -2
  30. data/vendor/liburing/src/arch/syscall-defs.h +2 -2
  31. data/vendor/liburing/src/include/liburing/io_uring.h +101 -17
  32. data/vendor/liburing/src/include/liburing.h +179 -59
  33. data/vendor/liburing/src/int_flags.h +4 -1
  34. data/vendor/liburing/src/liburing-ffi.map +14 -2
  35. data/vendor/liburing/src/liburing.map +9 -2
  36. data/vendor/liburing/src/queue.c +35 -30
  37. data/vendor/liburing/src/register.c +46 -15
  38. data/vendor/liburing/src/sanitize.c +6 -9
  39. data/vendor/liburing/src/setup.c +37 -71
  40. data/vendor/liburing/src/syscall.c +2 -2
  41. data/vendor/liburing/test/232c93d07b74.c +1 -0
  42. data/vendor/liburing/test/Makefile +9 -0
  43. data/vendor/liburing/test/accept-test.c +1 -0
  44. data/vendor/liburing/test/cmd-discard.c +16 -8
  45. data/vendor/liburing/test/connect.c +11 -7
  46. data/vendor/liburing/test/epwait.c +420 -0
  47. data/vendor/liburing/test/eventfd-ring.c +30 -5
  48. data/vendor/liburing/test/fallocate.c +1 -1
  49. data/vendor/liburing/test/fixed-hugepage.c +10 -7
  50. data/vendor/liburing/test/fixed-seg.c +187 -0
  51. data/vendor/liburing/test/helpers.c +121 -0
  52. data/vendor/liburing/test/helpers.h +13 -0
  53. data/vendor/liburing/test/init-mem.c +2 -0
  54. data/vendor/liburing/test/io_uring_passthrough.c +78 -62
  55. data/vendor/liburing/test/iopoll-overflow.c +5 -4
  56. data/vendor/liburing/test/iopoll.c +20 -10
  57. data/vendor/liburing/test/iowait.c +141 -0
  58. data/vendor/liburing/test/nvme.h +2 -0
  59. data/vendor/liburing/test/pipe-bug.c +11 -5
  60. data/vendor/liburing/test/pipe-eof.c +11 -1
  61. data/vendor/liburing/test/read-inc-file.c +150 -0
  62. data/vendor/liburing/test/read-write.c +21 -14
  63. data/vendor/liburing/test/recv-bundle-short-ooo.c +435 -0
  64. data/vendor/liburing/test/recv-multishot.c +2 -2
  65. data/vendor/liburing/test/reg-wait.c +449 -120
  66. data/vendor/liburing/test/regbuf-clone.c +53 -0
  67. data/vendor/liburing/test/resize-rings.c +25 -2
  68. data/vendor/liburing/test/rsrc_tags.c +67 -14
  69. data/vendor/liburing/test/send-zerocopy.c +52 -130
  70. data/vendor/liburing/test/sendmsg_iov_clean.c +216 -0
  71. data/vendor/liburing/test/socket-nb.c +158 -0
  72. data/vendor/liburing/test/sqwait.c +9 -11
  73. data/vendor/liburing/test/timeout.c +198 -0
  74. data/vendor/liburing/test/vec-regbuf.c +609 -0
  75. data/vendor/liburing/test/wait-timeout.c +1 -1
  76. data/vendor/liburing/test/wq-aff.c +5 -1
  77. data/vendor/liburing/test/zcrx.c +928 -0
  78. metadata +16 -4
  79. data/vendor/liburing/.github/workflows/codespell.yml +0 -25
  80. data/vendor/liburing/.github/workflows/shellcheck.yml +0 -20
@@ -0,0 +1,435 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Description: Run recv multishot with bundle support, and verify that
4
+ * data is always received in the correct order. A kernel
5
+ * commit sometimes broke this:
6
+ *
7
+ * 7c71a0af81ba ("io_uring/net: improve recv bundles")
8
+ *
9
+ * Test case heavily based on the excellent reproducer posted
10
+ * by royonia in this bug report:
11
+ *
12
+ * https://github.com/axboe/liburing/issues/1409
13
+ *
14
+ */
15
+ #include <stdio.h>
16
+ #include <stdlib.h>
17
+ #include <string.h>
18
+ #include <unistd.h>
19
+ #include <assert.h>
20
+ #include <sys/socket.h>
21
+ #include <sys/types.h>
22
+ #include <sys/mman.h>
23
+ #include <netinet/in.h>
24
+ #include <arpa/inet.h>
25
+
26
+ #include "liburing.h"
27
+ #include "helpers.h"
28
+
29
+ static int no_buf_ring, no_recv_mshot;
30
+
31
+ /* Configuration constants */
32
+ #define ONE_MB (1024 * 1024) /* Size of test data (1MB) */
33
+ #define BUFFER_SIZE 1024 /* Size of each buffer in bytes */
34
+
35
+ #define BUFFER_COUNT 4 /* Number of buffers in the ring */
36
+ #define QUEUE_DEPTH 16 /* io_uring queue depth */
37
+
38
+ #define min(a, b) (((a) < (b)) ? (a) : (b))
39
+
40
+ /* Global state tracking */
41
+ static size_t data_received = 0; /* Tracks total bytes received */
42
+
43
+ /**
44
+ * Buffer data structure
45
+ * Contains information about a buffer from the ring
46
+ */
47
+ struct buf_data {
48
+ void *addr; /* Buffer memory address */
49
+ uint16_t bid; /* Buffer ID within the ring */
50
+ uint32_t len; /* Length of valid data in the buffer */
51
+ };
52
+
53
+ /**
54
+ * Buffer ring data structure
55
+ * Holds information needed to manage a buffer ring
56
+ */
57
+ struct buf_ring_data {
58
+ struct io_uring_buf_ring *buf_ring; /* The io_uring buffer ring */
59
+ void *buffer_memory; /* Memory for all buffers */
60
+ uint16_t ring_entries; /* Number of entries in the ring */
61
+ uint32_t buf_size; /* Size of each buffer */
62
+ };
63
+
64
+ /**
65
+ * Sets up and initializes the buffer ring for io_uring
66
+ *
67
+ * This function allocates memory for the buffer ring and all individual buffers,
68
+ * initializes the buffer ring, registers it with io_uring, and adds all buffers
69
+ * to the ring.
70
+ *
71
+ * @param ring Pointer to the io_uring instance
72
+ * @param entries Number of buffer entries to create
73
+ * @param buf_size Size of each buffer in bytes
74
+ * @param bgid Buffer group ID to use
75
+ *
76
+ */
77
+ static int setup_buf_ring(struct buf_ring_data *data, struct io_uring *ring,
78
+ uint16_t entries, uint32_t buf_size, int bgid)
79
+ {
80
+ data->ring_entries = entries;
81
+ data->buf_size = buf_size;
82
+
83
+ /* Allocate page-aligned memory for all buffers */
84
+ size_t total_size = entries * buf_size;
85
+ int page_size = sysconf(_SC_PAGESIZE);
86
+ size_t aligned_size = (total_size + page_size - 1) & ~(page_size - 1);
87
+
88
+ void *buffer_memory = mmap(NULL, aligned_size, PROT_READ | PROT_WRITE,
89
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
90
+ assert(buffer_memory != MAP_FAILED);
91
+
92
+ /* Verify buffer memory is page-aligned as guaranteed by mmap */
93
+ data->buffer_memory = buffer_memory;
94
+
95
+ /* Allocate and setup buffer ring with page alignment */
96
+ void *mapped;
97
+ struct io_uring_buf_ring *buf_ring;
98
+ int ring_size = entries * sizeof(struct io_uring_buf);
99
+
100
+ /* Round up ring size to page boundary */
101
+ ring_size = (ring_size + page_size - 1) & ~(page_size - 1);
102
+
103
+ mapped = mmap(NULL, ring_size, PROT_READ | PROT_WRITE,
104
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
105
+ assert(mapped != MAP_FAILED);
106
+
107
+ buf_ring = (struct io_uring_buf_ring *)mapped;
108
+
109
+ /* Initialize the buffer ring structure */
110
+ io_uring_buf_ring_init(buf_ring);
111
+ data->buf_ring = buf_ring;
112
+
113
+ /* Prepare registration parameters */
114
+ struct io_uring_buf_reg reg = {
115
+ .ring_addr = (unsigned long)buf_ring,
116
+ .ring_entries = entries,
117
+ .bgid = 0
118
+ };
119
+
120
+ /* Register the buffer ring with io_uring */
121
+ int ret = io_uring_register_buf_ring(ring, &reg, 0);
122
+
123
+ if (ret) {
124
+ if (ret == -EINVAL) {
125
+ no_buf_ring = 1;
126
+ return T_EXIT_SKIP;
127
+ }
128
+ fprintf(stderr, "Buffer ring setup: %d\n", ret);
129
+ return T_EXIT_FAIL;
130
+ }
131
+
132
+ /* Add all individual buffers to the ring */
133
+ for (int i = 0; i < entries; i++) {
134
+ void *buf_addr = buffer_memory + i * buf_size;
135
+
136
+ io_uring_buf_ring_add(buf_ring, buf_addr, buf_size, i,
137
+ io_uring_buf_ring_mask(entries), i);
138
+ }
139
+
140
+ /* Make all buffers available by advancing the tail pointer */
141
+ io_uring_buf_ring_advance(buf_ring, entries);
142
+ return T_EXIT_PASS;
143
+ }
144
+
145
+ /**
146
+ * Verifies that received buffer data matches expected data
147
+ *
148
+ * This function compares each byte of received data against the expected data
149
+ * and asserts if any mismatch is found. It also prints the comparison for debugging.
150
+ *
151
+ * @param buf Pointer to buffer containing received data
152
+ * @param expected_data_start Pointer to start of expected data for comparison
153
+ */
154
+ static int verify_received_buffer(struct buf_data *buf, uint8_t *expected_data_start)
155
+ {
156
+ uint8_t *data = buf->addr;
157
+
158
+ for (uint32_t i = 0; i < buf->len; i++) {
159
+ if (data[i] != expected_data_start[i]) {
160
+ fprintf(stderr, "Recv data ordering mismatch\n");
161
+ return 1;
162
+ }
163
+ }
164
+
165
+ return 0;
166
+ }
167
+
168
+ /**
169
+ * Processes a completed io_uring receive operation
170
+ *
171
+ * This function handles the completion queue entry by:
172
+ * 1. Extracting the buffer ID and received data from the CQE
173
+ * 2. Updating the global data received counter
174
+ * 3. Verifying the data matches the expected pattern
175
+ * 4. Recycling the buffer back to the buffer ring
176
+ *
177
+ * @param cqe Completion queue entry to process
178
+ * @param br_data Buffer ring data structure
179
+ * @param current_expect Pointer to current position in expected data (updated by this function)
180
+ */
181
+ static int process_completion(struct io_uring_cqe *cqe, struct buf_ring_data *br_data,
182
+ uint8_t **current_expect)
183
+ {
184
+ usleep(1);
185
+
186
+ if (cqe->res <= 0) {
187
+ /* Handle error or EOF condition */
188
+ if (cqe->res == 0) {
189
+ fprintf(stderr, "EOF reached\n");
190
+ } else if (cqe->res == -EINVAL) {
191
+ /* no recv mshot support */
192
+ no_recv_mshot = 1;
193
+ } else if (cqe->res != -ENOBUFS) {
194
+ fprintf(stderr, "CQE res %d\n", cqe->res);
195
+ return 1;
196
+ }
197
+ return 0;
198
+ }
199
+
200
+ /* Extract buffer ID and data length from completion */
201
+ uint16_t bid = cqe->flags >> IORING_CQE_BUFFER_SHIFT;
202
+ uint32_t total_len = cqe->res;
203
+
204
+ uint32_t nr_packet = 0;
205
+ while (total_len) {
206
+ uint32_t this_len = min(BUFFER_SIZE, total_len);
207
+ /* should never get a len large then bundled buffer size */
208
+ assert(this_len <= BUFFER_SIZE);
209
+
210
+ void *buffer_addr = (uint8_t*)br_data->buffer_memory + (bid * BUFFER_SIZE);
211
+ /* Prepare buffer data structure for verification */
212
+ struct buf_data buf = {
213
+ .addr = buffer_addr,
214
+ .bid = bid,
215
+ .len = this_len
216
+ };
217
+
218
+ /* Update global counter of total bytes received */
219
+ data_received += this_len;
220
+
221
+ /* Verify received data against expected pattern */
222
+ if (verify_received_buffer(&buf, *current_expect))
223
+ return T_EXIT_FAIL;
224
+ *current_expect += this_len; /* Move expected pointer forward */
225
+
226
+ /* Rearm the buffer */
227
+ io_uring_buf_ring_add(br_data->buf_ring, buffer_addr,
228
+ BUFFER_SIZE, bid,
229
+ io_uring_buf_ring_mask(br_data->ring_entries),
230
+ nr_packet);
231
+ nr_packet++;
232
+
233
+ /* Calculate next buffer id */
234
+ bid = (bid + 1) & (BUFFER_COUNT - 1);
235
+ total_len -= this_len;
236
+ }
237
+ if (nr_packet)
238
+ io_uring_buf_ring_advance(br_data->buf_ring, nr_packet);
239
+ return 0;
240
+ }
241
+
242
+ /**
243
+ * Writes all the data to the specified file descriptor
244
+ *
245
+ * This function ensures that all data is written, handling partial writes
246
+ * by making repeated calls to write() until all bytes are sent.
247
+ *
248
+ * @param fd File descriptor to write to
249
+ * @param data Pointer to the data buffer to write
250
+ * @param size Number of bytes to write
251
+ */
252
+ static void write_all(int fd, const void *data, size_t size)
253
+ {
254
+ const uint8_t *buf = data;
255
+ size_t bytes_sent = 0;
256
+
257
+ /* Continue until all data is sent */
258
+ while (bytes_sent < size) {
259
+ ssize_t sent;
260
+
261
+ sent = write(fd, buf + bytes_sent, size - bytes_sent);
262
+ assert(sent > 0); /* Ensure write succeeded */
263
+ bytes_sent += sent;
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Main test function for io_uring bundle receive mechanism
269
+ *
270
+ * This function demonstrates the complete flow of using io_uring's buffer ring
271
+ * and multishot receive with IORING_RECVSEND_BUNDLE flag. It performs these steps:
272
+ * 1. Set up an io_uring instance and buffer ring
273
+ * 2. Create a TCP socket pair for testing
274
+ * 3. Send 1MB of pattern data over the socket
275
+ * 4. Receive and verify the data using io_uring operations
276
+ *
277
+ */
278
+ static int test_recv_multi_large_packet_isolate_ring(int queue_flags)
279
+ {
280
+ /* Initialize io_uring with parameters */
281
+ struct io_uring ring;
282
+ struct io_uring_params params = { .flags = queue_flags, };
283
+ int ret, eret;
284
+
285
+ ret = t_create_ring_params(QUEUE_DEPTH, &ring, &params);
286
+ if (ret == T_SETUP_SKIP)
287
+ return T_EXIT_SKIP;
288
+ else if (ret != T_SETUP_OK)
289
+ return T_EXIT_FAIL;
290
+
291
+ /* Set up the buffer ring for receiving data */
292
+ struct buf_ring_data br_data;
293
+ ret = setup_buf_ring(&br_data, &ring, BUFFER_COUNT, BUFFER_SIZE, 0);
294
+ if (ret == T_EXIT_SKIP)
295
+ return T_EXIT_SKIP;
296
+ else if (ret != T_EXIT_PASS)
297
+ return T_EXIT_FAIL;
298
+
299
+ /* Create socket pair for local communication testing */
300
+ int socket_fds[2];
301
+ ret = t_create_socket_pair(socket_fds, true);
302
+ assert(ret == 0);
303
+
304
+ int receiver_fd = socket_fds[0];
305
+ int sender_fd = socket_fds[1];
306
+
307
+ /* Allocate and initialize test data with pattern */
308
+ uint8_t *test_data = malloc(ONE_MB);
309
+ for (int i = 0; i < ONE_MB; i++)
310
+ test_data[i] = i % 256; /* Create repeating pattern */
311
+
312
+ /* Send test data through the socket */
313
+ write_all(sender_fd, test_data, ONE_MB);
314
+
315
+ /* Close sender side to signal EOF to receiver */
316
+ close(sender_fd);
317
+
318
+ /* Initialize pointer to track our position in expected data */
319
+ uint8_t *current_expect = test_data;
320
+
321
+ /* Submit initial multishot receive operations with buffer selection */
322
+ for (int i = 0; i < BUFFER_COUNT; i++) {
323
+ struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
324
+
325
+ io_uring_prep_recv_multishot(sqe, receiver_fd, NULL, 0, 0);
326
+ sqe->flags |= IOSQE_BUFFER_SELECT;
327
+ sqe->ioprio |= IORING_RECVSEND_BUNDLE;
328
+ sqe->buf_group = 0;
329
+ }
330
+ io_uring_submit(&ring);
331
+
332
+ /* Process completions from io_uring */
333
+ struct io_uring_cqe *cqe;
334
+ int poll_count = 0;
335
+
336
+ /* Loop until we've received all data or exceed maximum iterations */
337
+ while (data_received < ONE_MB && poll_count < 5000) {
338
+ /* Wait for a completion event */
339
+ ret = io_uring_wait_cqe(&ring, &cqe);
340
+ if (ret) {
341
+ fprintf(stderr, "wait_cqe=%d\n", ret);
342
+ eret = T_EXIT_FAIL;
343
+ goto exit;
344
+ }
345
+ /* Process this completion */
346
+ if (process_completion(cqe, &br_data, &current_expect)) {
347
+ eret = T_EXIT_FAIL;
348
+ goto exit;
349
+ }
350
+ if (no_recv_mshot) {
351
+ eret = T_EXIT_SKIP;
352
+ goto exit;
353
+ }
354
+
355
+ /* Check for EOF (no more data and no more expected) */
356
+ if (!(cqe->flags & IORING_CQE_F_MORE) && !(cqe->res)) {
357
+ io_uring_cq_advance(&ring, 1);
358
+ break; /* Exit loop on EOF */
359
+ }
360
+
361
+ /* Respawn recv request if needed (when this one is done but no EOF) */
362
+ if (!(cqe->flags & IORING_CQE_F_MORE) && cqe->res) {
363
+ /* Get a submission queue entry */
364
+ struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
365
+
366
+ /* Set up another multishot receive with same parameters */
367
+ io_uring_prep_recv_multishot(sqe, receiver_fd, NULL, 0, 0);
368
+ sqe->flags |= IOSQE_BUFFER_SELECT;
369
+ sqe->ioprio |= IORING_RECVSEND_BUNDLE;
370
+ sqe->buf_group = 0;
371
+
372
+ /* Submit the new request */
373
+ io_uring_submit(&ring);
374
+ }
375
+
376
+ /* Mark completion as processed */
377
+ io_uring_cq_advance(&ring, 1);
378
+ poll_count++;
379
+ }
380
+
381
+ /* Verify we received all expected data */
382
+ if (data_received != ONE_MB) {
383
+ fprintf(stderr, "Received %u, wanted %u\n", (int) data_received, ONE_MB);
384
+ return T_EXIT_FAIL;
385
+ }
386
+
387
+ eret = T_EXIT_PASS;
388
+ exit:
389
+ /* Clean up all allocated resources */
390
+ close(receiver_fd); /* Close socket */
391
+ io_uring_queue_exit(&ring); /* Clean up io_uring */
392
+
393
+ /* Free memory resources */
394
+ munmap(br_data.buffer_memory, BUFFER_COUNT * BUFFER_SIZE);
395
+ munmap(br_data.buf_ring, BUFFER_COUNT * sizeof(struct io_uring_buf));
396
+ free(test_data);
397
+
398
+ return eret;
399
+ }
400
+
401
+ int main(int argc, char *argv[])
402
+ {
403
+ int ret;
404
+
405
+ if (argc > 1)
406
+ return T_EXIT_SKIP;
407
+
408
+ ret = test_recv_multi_large_packet_isolate_ring(0);
409
+ if (ret == T_EXIT_FAIL) {
410
+ fprintf(stderr, "test 0 failed\n");
411
+ return ret;
412
+ }
413
+ if (no_buf_ring || no_recv_mshot)
414
+ return T_EXIT_SKIP;
415
+
416
+ ret = test_recv_multi_large_packet_isolate_ring(IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN);
417
+ if (ret == T_EXIT_FAIL) {
418
+ fprintf(stderr, "test defer failed\n");
419
+ return ret;
420
+ }
421
+
422
+ ret = test_recv_multi_large_packet_isolate_ring(IORING_SETUP_SQPOLL);
423
+ if (ret == T_EXIT_FAIL) {
424
+ fprintf(stderr, "test sqpoll failed\n");
425
+ return ret;
426
+ }
427
+
428
+ ret = test_recv_multi_large_packet_isolate_ring(IORING_SETUP_COOP_TASKRUN);
429
+ if (ret == T_EXIT_FAIL) {
430
+ fprintf(stderr, "test coop failed\n");
431
+ return ret;
432
+ }
433
+
434
+ return T_EXIT_PASS;
435
+ }
@@ -165,7 +165,7 @@ static int test(struct args *args)
165
165
  io_uring_prep_recv_multishot(sqe, fds[0], NULL, 0, 0);
166
166
  }
167
167
  sqe->flags |= IOSQE_BUFFER_SELECT;
168
- sqe->buf_group = 7;
168
+ io_uring_sqe_set_buf_group(sqe, 7);
169
169
  io_uring_sqe_set_data64(sqe, 1234);
170
170
  io_uring_submit(&ring);
171
171
 
@@ -503,7 +503,7 @@ static int test_enobuf(void)
503
503
  assert(sqe);
504
504
  io_uring_prep_recv_multishot(sqe, fds[0], NULL, 0, 0);
505
505
  io_uring_sqe_set_data64(sqe, 1);
506
- sqe->buf_group = 0;
506
+ io_uring_sqe_set_buf_group(sqe, 0);
507
507
  sqe->flags |= IOSQE_BUFFER_SELECT;
508
508
 
509
509
  ret = io_uring_submit(&ring);