Skip to content

Commit f96d31f

Browse files
committed
test/msg-ring: add sync test variants
Use io_uring_register_sync_msg() for the test cases where it makes sense, to test that API as well. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d27f14c commit f96d31f

1 file changed

Lines changed: 123 additions & 9 deletions

File tree

test/msg-ring.c

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,59 @@
1515
#include "helpers.h"
1616

1717
static int no_msg;
18+
static int no_sync_msg;
1819

19-
static int test_own(struct io_uring *ring)
20+
static int test_own_sync(struct io_uring *ring)
2021
{
22+
struct io_uring_sqe sqe = { };
2123
struct io_uring_cqe *cqe;
24+
int ret;
25+
26+
if (no_sync_msg)
27+
return 0;
28+
29+
io_uring_prep_msg_ring(&sqe, ring->ring_fd, 0x10, 0x1234, 0);
30+
sqe.user_data = 1;
31+
32+
ret = io_uring_register_sync_msg(&sqe);
33+
if (ret == -EINVAL) {
34+
no_sync_msg = 1;
35+
return 0;
36+
} else if (ret != 0) {
37+
fprintf(stderr, "register_sync_msg: %d\n", ret);
38+
return 1;
39+
}
40+
41+
ret = io_uring_wait_cqe(ring, &cqe);
42+
if (ret < 0) {
43+
fprintf(stderr, "wait completion %d\n", ret);
44+
return 1;
45+
}
46+
switch (cqe->user_data) {
47+
case 0x1234:
48+
if (cqe->res != 0x10) {
49+
fprintf(stderr, "invalid len %x\n", cqe->res);
50+
return -1;
51+
}
52+
break;
53+
default:
54+
fprintf(stderr, "Invalid user_data\n");
55+
return -1;
56+
}
57+
58+
io_uring_cqe_seen(ring, cqe);
59+
return 0;
60+
}
61+
62+
static int test_own(struct io_uring *ring, int do_sync)
63+
{
2264
struct io_uring_sqe *sqe;
65+
struct io_uring_cqe *cqe;
2366
int ret, i;
2467

68+
if (do_sync)
69+
return test_own_sync(ring);
70+
2571
sqe = io_uring_get_sqe(ring);
2672
if (!sqe) {
2773
fprintf(stderr, "get sqe failed\n");
@@ -116,7 +162,47 @@ static void *wait_cqe_fn(void *__data)
116162
return (void *) (unsigned long) 1;
117163
}
118164

119-
static int test_remote(struct io_uring *ring, unsigned int ring_flags)
165+
static int test_remote_sync(unsigned int ring_flags)
166+
{
167+
struct io_uring *target;
168+
pthread_t thread;
169+
void *tret;
170+
struct io_uring_sqe sqe = { };
171+
struct data d;
172+
int ret;
173+
174+
if (no_sync_msg)
175+
return 0;
176+
177+
d.flags = ring_flags;
178+
pthread_barrier_init(&d.barrier, NULL, 2);
179+
pthread_barrier_init(&d.startup, NULL, 2);
180+
pthread_create(&thread, NULL, wait_cqe_fn, &d);
181+
182+
pthread_barrier_wait(&d.startup);
183+
target = d.ring;
184+
185+
io_uring_prep_msg_ring(&sqe, target->ring_fd, 0x20, 0x5aa5, 0);
186+
sqe.user_data = 1;
187+
188+
pthread_barrier_wait(&d.barrier);
189+
190+
ret = io_uring_register_sync_msg(&sqe);
191+
if (ret == -EINVAL) {
192+
no_sync_msg = 1;
193+
return 0;
194+
} else if (ret != 0) {
195+
fprintf(stderr, "sync_msg: %d\n", ret);
196+
goto err;
197+
}
198+
pthread_join(thread, &tret);
199+
return 0;
200+
err:
201+
return 1;
202+
}
203+
204+
static int test_remote(struct io_uring *ring, unsigned int ring_flags,
205+
int do_sync)
120206
{
121207
struct io_uring *target;
122208
pthread_t thread;
@@ -126,6 +212,9 @@ static int test_remote(struct io_uring *ring, unsigned int ring_flags)
126212
struct data d;
127213
int ret;
128214

215+
if (do_sync)
216+
return test_remote_sync(ring_flags);
217+
129218
d.flags = ring_flags;
130219
pthread_barrier_init(&d.barrier, NULL, 2);
131220
pthread_barrier_init(&d.startup, NULL, 2);
@@ -367,16 +456,29 @@ static int test(int ring_flags)
367456
return T_EXIT_FAIL;
368457
}
369458

370-
ret = test_own(&ring);
459+
ret = test_own(&ring, 0);
371460
if (ret) {
372-
fprintf(stderr, "test_own failed\n");
461+
fprintf(stderr, "test_own sync failed\n");
373462
return T_EXIT_FAIL;
374463
}
375464
if (no_msg)
376465
return T_EXIT_SKIP;
377-
ret = test_own(&pring);
466+
467+
ret = test_own(&ring, 1);
468+
if (ret) {
469+
fprintf(stderr, "test_own async failed\n");
470+
return T_EXIT_FAIL;
471+
}
472+
473+
ret = test_own(&pring, 0);
474+
if (ret) {
475+
fprintf(stderr, "test_own async iopoll failed\n");
476+
return T_EXIT_FAIL;
477+
}
478+
479+
ret = test_own(&pring, 1);
378480
if (ret) {
379-
fprintf(stderr, "test_own iopoll failed\n");
481+
fprintf(stderr, "test_own sync iopoll failed\n");
380482
return T_EXIT_FAIL;
381483
}
382484

@@ -394,12 +496,18 @@ static int test(int ring_flags)
394496
}
395497
}
396498

397-
ret = test_remote(&ring, ring_flags);
499+
ret = test_remote(&ring, ring_flags, 0);
398500
if (ret) {
399501
fprintf(stderr, "test_remote failed\n");
400502
return T_EXIT_FAIL;
401503
}
402504

505+
ret = test_remote(&ring, ring_flags, 1);
506+
if (ret) {
507+
fprintf(stderr, "test_remote sync failed\n");
508+
return T_EXIT_FAIL;
509+
}
510+
403511
io_uring_queue_exit(&ring);
404512
io_uring_queue_exit(&pring);
405513

@@ -411,9 +519,15 @@ static int test(int ring_flags)
411519
return T_EXIT_FAIL;
412520
}
413521

414-
ret = test_own(&ring);
522+
ret = test_own(&ring, 0);
523+
if (ret) {
524+
fprintf(stderr, "test_own async deferred failed\n");
525+
return T_EXIT_FAIL;
526+
}
527+
528+
ret = test_own(&ring, 1);
415529
if (ret) {
416-
fprintf(stderr, "test_own deferred failed\n");
530+
fprintf(stderr, "test_own sync deferred failed\n");
417531
return T_EXIT_FAIL;
418532
}
419533

0 commit comments

Comments
 (0)