1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
* Description: test IOU_PBUF_RING_MMAP with a ring setup with a ring
* setup without mmap'ing sq/cq arrays
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include "liburing.h"
#include "helpers.h"
static int bgid = 5;
static int bid = 89;
int main(int argc, char *argv[])
{
struct io_uring_buf_ring *br;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
struct io_uring ring;
size_t ring_size;
int ret, ring_mask, fds[2];
struct io_uring_buf_reg reg = {
.ring_entries = 1,
.bgid = bgid,
.flags = IOU_PBUF_RING_MMAP,
};
struct io_uring_params p = { };
void *ring_mem;
char buf[32];
off_t off;
if (argc > 1)
return T_EXIT_SKIP;
if (posix_memalign(&ring_mem, 16384, 16384))
return T_EXIT_FAIL;
memset(ring_mem, 0, 16384);
p.flags = IORING_SETUP_NO_MMAP;
ret = io_uring_queue_init_mem(1, &ring, &p, ring_mem, 16384);
if (ret < 0) {
if (ret == -EINVAL || ret == -ENOMEM) {
free(ring_mem);
return T_EXIT_SKIP;
}
fprintf(stderr, "queue init failed %d\n", ret);
return T_EXIT_FAIL;
}
if (pipe(fds) < 0) {
perror("pipe");
return T_EXIT_FAIL;
}
ring_size = sizeof(struct io_uring_buf);
ring_mask = io_uring_buf_ring_mask(1);
ret = io_uring_register_buf_ring(&ring, ®, 0);
if (ret) {
if (ret == -EINVAL) {
free(ring_mem);
return T_EXIT_SKIP;
}
fprintf(stderr, "reg buf ring: %d\n", ret);
return T_EXIT_FAIL;
}
off = IORING_OFF_PBUF_RING |
(unsigned long long) bgid << IORING_OFF_PBUF_SHIFT;
br = mmap(NULL, ring_size, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_POPULATE, ring.ring_fd, off);
if (br == MAP_FAILED) {
if (errno == ENOMEM) {
free(ring_mem);
return T_EXIT_SKIP;
}
perror("mmap");
return T_EXIT_FAIL;
}
io_uring_buf_ring_add(br, buf, sizeof(buf), bid, ring_mask, 0);
io_uring_buf_ring_advance(br, 1);
sqe = io_uring_get_sqe(&ring);
io_uring_prep_read(sqe, fds[0], NULL, 0, 0);
sqe->flags |= IOSQE_BUFFER_SELECT;
sqe->buf_group = bgid;
io_uring_submit(&ring);
ret = write(fds[1], "Hello", 5);
if (ret < 0) {
perror("write");
return T_EXIT_FAIL;
} else if (ret != 5) {
fprintf(stderr, "short write %d\n", ret);
return T_EXIT_FAIL;
}
ret = io_uring_wait_cqe(&ring, &cqe);
if (ret) {
fprintf(stderr, "wait %d\n", ret);
return T_EXIT_FAIL;
}
if (cqe->res < 0) {
fprintf(stderr, "cqe res %d\n", cqe->res);
return T_EXIT_FAIL;
}
if (!(cqe->flags & IORING_CQE_F_BUFFER)) {
fprintf(stderr, "buffer not selected in cqe\n");
return T_EXIT_FAIL;
}
if ((cqe->flags >> IORING_CQE_BUFFER_SHIFT) != bid) {
fprintf(stderr, "wrong buffer id returned\n");
return T_EXIT_FAIL;
}
io_uring_cqe_seen(&ring, cqe);
io_uring_queue_exit(&ring);
free(ring_mem);
return T_EXIT_PASS;
}
|