/
liburing_provider.c
278 lines (233 loc) · 9.37 KB
/
liburing_provider.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "liburing_provider.h"
#include "liburing_socket_provider.h"
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <liburing.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdint.h>
#define EVENT_TYPE_ACCEPT 0
#define EVENT_TYPE_READ 1
#define EVENT_TYPE_WRITE 2
#define EVENT_TYPE_CONNECT 3
struct request {
int32_t fd;
int8_t event_type;
int64_t buffer_addr;
};
struct accept_request {
int32_t fd;
int8_t event_type;
struct sockaddr_in client_addr;
socklen_t client_addr_len;
};
JNIEXPORT jlong JNICALL
Java_sh_blake_niouring_IoUring_createCqes(JNIEnv *env, jclass cls, jint count) {
struct io_uring_cqe **cqes = malloc(sizeof(struct io_uring_cqe *) * count);
if (!cqes) {
return throw_out_of_memory_error(env);
}
return cqes;
}
JNIEXPORT jlong JNICALL
Java_sh_blake_niouring_IoUring_create(JNIEnv *env, jclass cls, jint maxEvents) {
struct io_uring *ring = malloc(sizeof(struct io_uring));
if (!ring) {
return throw_out_of_memory_error(env);
}
int32_t ret = io_uring_queue_init(maxEvents, ring, 0);
if (ret < 0) {
return throw_exception(env, "io_uring_queue_init", ret);
}
return (uint64_t) ring;
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_IoUring_close(JNIEnv *env, jclass cls, jlong ring_address) {
struct io_uring *ring = (struct io_uring *) ring_address;
io_uring_queue_exit(ring);
}
JNIEXPORT jint JNICALL
Java_sh_blake_niouring_IoUring_submit(JNIEnv *env, jclass cls, jlong ring_address) {
struct io_uring *ring = (struct io_uring *) ring_address;
int32_t ret = io_uring_submit(ring);
if (ret < 0) {
return throw_exception(env, "io_uring_submit", ret);
}
return ret;
}
JNIEXPORT jint JNICALL
Java_sh_blake_niouring_IoUring_submitAndGetCqes(JNIEnv *env, jclass cls, jlong ring_address, jlong cqes_address, jint cqes_size, jboolean should_wait) {
struct io_uring *ring = (struct io_uring *) ring_address;
int32_t ret = io_uring_submit(ring);
if (ret < 0) {
if (ret != -EBUSY) { // if busy, continue handling completions
return throw_exception(env, "io_uring_submit", ret);
}
}
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
ret = io_uring_peek_batch_cqe(ring, cqes, cqes_size);
if (ret == 0 && should_wait) {
ret = io_uring_wait_cqe(ring, cqes);
if (ret < 0) {
return throw_exception(env, "io_uring_wait_cqe", ret);
}
ret = 1;
}
return (int32_t) ret;
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_IoUring_freeCqes(JNIEnv *env, jclass cls, jlong cqes_address) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
free(cqes);
}
JNIEXPORT jbyte JNICALL
Java_sh_blake_niouring_IoUring_getCqeEventType(JNIEnv *env, jclass cls, jlong cqes_address, jint cqe_index) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
struct request *req = (struct request *) cqe->user_data;
return (int32_t) req->event_type;
}
JNIEXPORT jint JNICALL
Java_sh_blake_niouring_IoUring_getCqeFd(JNIEnv *env, jclass cls, jlong cqes_address, jint cqe_index) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
struct request *req = (struct request *) cqe->user_data;
return (int32_t) req->fd;
}
JNIEXPORT jint JNICALL
Java_sh_blake_niouring_IoUring_getCqeResult(JNIEnv *env, jclass cls, jlong cqes_address, jint cqe_index) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
return (int32_t) cqe->res;
}
JNIEXPORT jlong JNICALL
Java_sh_blake_niouring_IoUring_getCqeBufferAddress(JNIEnv *env, jclass cls, jlong cqes_address, jint cqe_index) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
struct request *req = (struct request *) cqe->user_data;
return (int64_t) req->buffer_addr;
}
JNIEXPORT jstring JNICALL
Java_sh_blake_niouring_IoUring_getCqeIpAddress(JNIEnv *env, jclass cls, jlong cqes_address, jint cqe_index) {
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
struct accept_request *req = (struct accept_request *) cqe->user_data;
char *ip_address = inet_ntoa(req->client_addr.sin_addr);
return (*env)->NewStringUTF(env, ip_address);
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_IoUring_markCqeSeen(JNIEnv *env, jclass cls, jlong ring_address, jlong cqes_address, jint cqe_index) {
struct io_uring *ring = (struct io_uring *) ring_address;
struct io_uring_cqe **cqes = (struct io_uring_cqe **) cqes_address;
struct io_uring_cqe *cqe = cqes[cqe_index];
struct request *req = cqe->user_data;
free(req);
io_uring_cqe_seen(ring, cqe);
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_IoUring_queueAccept(JNIEnv *env, jclass cls, jlong ring_address, jint server_socket_fd) {
struct io_uring *ring = (struct io_uring *) ring_address;
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
if (sqe == NULL) {
return throw_exception(env, "io_uring_get_sqe", -EBUSY);
}
struct accept_request *req = malloc(sizeof(*req));
if (!req) {
return throw_out_of_memory_error(env);
}
memset(&req->client_addr, 0, sizeof(req->client_addr));
req->client_addr_len = sizeof(req->client_addr);
req->event_type = EVENT_TYPE_ACCEPT;
req->fd = server_socket_fd;
io_uring_prep_accept(sqe, server_socket_fd, (struct sockaddr *) &req->client_addr, &req->client_addr_len, 0);
io_uring_sqe_set_data(sqe, req);
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_IoUring_queueConnect(JNIEnv *env, jclass cls, jlong ring_address, jint socket_fd, jstring ip_address, jint port) {
struct io_uring *ring = (struct io_uring *) ring_address;
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
if (sqe == NULL) {
return throw_exception(env, "io_uring_get_sqe", -EBUSY);
}
struct accept_request *req = malloc(sizeof(*req));
if (!req) {
return throw_out_of_memory_error(env);
}
char *ip = (*env)->GetStringUTFChars(env, ip_address, NULL);
memset(&req->client_addr, 0, sizeof(req->client_addr));
req->client_addr.sin_addr.s_addr = inet_addr(ip);
req->client_addr.sin_port = htons(port);
req->client_addr.sin_family = AF_INET;
req->client_addr_len = sizeof(req->client_addr);
req->event_type = EVENT_TYPE_CONNECT;
req->fd = socket_fd;
(*env)->ReleaseStringUTFChars(env, ip_address, ip);
io_uring_prep_connect(sqe, socket_fd, (struct sockaddr *) &req->client_addr, req->client_addr_len);
io_uring_sqe_set_data(sqe, req);
}
JNIEXPORT jlong JNICALL
Java_sh_blake_niouring_IoUring_queueRead(JNIEnv *env, jclass cls, jlong ring_address, jint fd, jobject byte_buffer, jint buffer_pos, jint buffer_len) {
void *buffer = (*env)->GetDirectBufferAddress(env, byte_buffer);
if (buffer == NULL) {
return throw_exception(env, "invalid byte buffer (read)", -EINVAL);
}
struct io_uring *ring = (struct io_uring *) ring_address;
if (io_uring_sq_space_left(ring) <= 1) {
return throw_exception(env, "io_uring_sq_space_left", -EBUSY);
}
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
if (sqe == NULL) {
return throw_exception(env, "io_uring_get_sqe", -EBUSY);
}
struct request *req = malloc(sizeof(*req));
if (!req) {
return throw_out_of_memory_error(env);
}
req->event_type = EVENT_TYPE_READ;
req->buffer_addr = buffer;
req->fd = fd;
io_uring_prep_read(sqe, fd, buffer + buffer_pos, buffer_len, 0);
io_uring_sqe_set_data(sqe, req);
return (uint64_t) buffer;
}
JNIEXPORT jlong JNICALL
Java_sh_blake_niouring_IoUring_queueWrite(JNIEnv *env, jclass cls, jlong ring_address, jint fd, jobject byte_buffer, jint buffer_pos, jint buffer_len) {
void *buffer = (*env)->GetDirectBufferAddress(env, byte_buffer);
if (buffer == NULL) {
return throw_exception(env, "invalid byte buffer (write)", -EINVAL);
}
struct io_uring *ring = (struct io_uring *) ring_address;
if (io_uring_sq_space_left(ring) <= 1) {
return throw_exception(env, "io_uring_sq_space_left", -EBUSY);
}
struct io_uring_sqe *sqe = io_uring_get_sqe(ring);
if (sqe == NULL) {
return throw_exception(env, "io_uring_get_sqe", -EBUSY);
}
struct request *req = malloc(sizeof(*req));
if (!req) {
return throw_out_of_memory_error(env);
}
req->event_type = EVENT_TYPE_WRITE;
req->buffer_addr = buffer;
req->fd = fd;
io_uring_prep_write(sqe, fd, buffer + buffer_pos, buffer_len, 0);
io_uring_sqe_set_data(sqe, req);
return (uint64_t) buffer;
}
JNIEXPORT void JNICALL
Java_sh_blake_niouring_AbstractIoUringChannel_close(JNIEnv *env, jclass cls, jint fd) {
close(fd);
}
int32_t throw_exception(JNIEnv *env, char *cause, int32_t ret) {
char error_msg[1024];
snprintf(error_msg, sizeof(error_msg), "%s - %s", cause, strerror(-ret));
return (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/RuntimeException"), &error_msg);
}
int32_t throw_out_of_memory_error(JNIEnv *env) {
return (*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/OutOfMemoryError"), "Out of heap space");
}