@amritk/nish-x86_64-linux 0.39.0 → 0.41.0
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.
- package/INSTALL.md +79 -133
- package/bin/nish +0 -0
- package/docs/AI.md +112 -33
- package/package.json +1 -1
- package/runtime/nish.d.ts +21 -17
- package/runtime/nish.h +10 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +85 -8
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-net.c +32 -0
- package/runtime/runtime-sandbox.c +55 -28
- package/runtime/runtime-uring.c +1194 -0
- package/runtime/runtime-wasm.c +422 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +22 -6
- package/std/README.md +74 -87
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1388 -2664
- package/std/crypto/pki.ts +116 -4
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x509.ts +7 -0
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +325 -112
- package/std/json-typed.ts +43 -8
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +137 -12
- package/std/net/http1-server.ts +83 -29
- package/std/net/http2.ts +47 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/prefork.ts +49 -13
- package/std/net/quic-recovery.ts +240 -9
- package/std/net/quic-stream.ts +21 -1
- package/std/net/quic.ts +98 -4
- package/std/net/router.ts +378 -31
- package/std/net/serve.ts +873 -65
- package/std/net/tls/verify.ts +19 -1
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +74 -1
- package/std/net/webtransport.ts +7 -1
- package/std/raster.ts +837 -0
- package/std/release.ts +593 -0
- package/std/synth.ts +143 -0
- package/std/temporal.ts +3091 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
|
@@ -0,0 +1,1194 @@
|
|
|
1
|
+
/* Nish runtime, the io_uring half of the network layer (WP63 S1): a set of
|
|
2
|
+
* io_uring rings that holds TCP connections in the rings' registered file
|
|
3
|
+
* tables instead of the process's descriptor table, so one process can hold
|
|
4
|
+
* more connections than RLIMIT_NOFILE allows descriptors. The design and its
|
|
5
|
+
* measurements are docs/wp63-iouring-net.md; the prototype it was measured on
|
|
6
|
+
* is bench/fd-limit/uring-server.c. Nothing in `nish:net` calls this file yet
|
|
7
|
+
* (S2 adds the surface), and section GC means a program pays nothing for it.
|
|
8
|
+
*
|
|
9
|
+
* A translation unit of its own for the reason runtime-net.c is one: it
|
|
10
|
+
* carries its own measured `.text*` ceiling in tests/runtime-budgets.js, and
|
|
11
|
+
* scripts/build.sh pairs it with runtime.c like the other halves.
|
|
12
|
+
*
|
|
13
|
+
* **Four kernel behaviours shape it** (the note's §2, each measured):
|
|
14
|
+
*
|
|
15
|
+
* 1. A ring's file table is capped at RLIMIT_NOFILE slots
|
|
16
|
+
* (io_uring/rsrc.c, io_sqe_files_register answers -EMFILE past it), so a
|
|
17
|
+
* set holds C connections in ceil(C / limit) rings, made as they fill.
|
|
18
|
+
* 2. An accept that finds the table full has already taken the connection
|
|
19
|
+
* off the listener, and the kernel drops it (io_accept: the fixed install
|
|
20
|
+
* fails and puts the file). A multishot accept keeps accepting while the
|
|
21
|
+
* queue is non-empty, so it cannot be bounded. Accepts here are
|
|
22
|
+
* single-shot into IORING_FILE_INDEX_ALLOC, and a ring never has more in
|
|
23
|
+
* flight than it has free slots.
|
|
24
|
+
* 3. Accepts on one listener are exclusive waiters, woken one per arriving
|
|
25
|
+
* connection, so an accept that is only waiting never learns of the ones
|
|
26
|
+
* already queued. Exactly one ring accepts at a time, and moving the job
|
|
27
|
+
* to another ring is a fresh submission there, which is tried at once and
|
|
28
|
+
* drains the backlog.
|
|
29
|
+
* 4. A DEFER_TASKRUN ring runs at most 20 queued completions per
|
|
30
|
+
* io_uring_enter (IO_LOCAL_TW_DEFAULT_MAX), and queuing more work does
|
|
31
|
+
* not signal its eventfd while the rest wait; only the completions the
|
|
32
|
+
* enter posts do. TASKRUN_FLAG says work is left in IORING_SQ_TASKRUN.
|
|
33
|
+
* `ur_service` enters again while that flag is up, so one wait takes
|
|
34
|
+
* all of a ring's queued work rather than 20 per wakeup, and while
|
|
35
|
+
* IORING_SQ_CQ_OVERFLOW says the kernel holds completions the CQ had no
|
|
36
|
+
* room for (FEAT_NODROP keeps them rather than dropping them).
|
|
37
|
+
*
|
|
38
|
+
* **Every ring** is SINGLE_ISSUER | DEFER_TASKRUN | TASKRUN_FLAG |
|
|
39
|
+
* SUBMIT_ALL, with COOP_TASKRUN added where a kernel refuses TASKRUN_FLAG
|
|
40
|
+
* without it; a sparse file table; a provided-buffer ring that every
|
|
41
|
+
* connection's multishot recv draws from; and one eventfd, which the set's
|
|
42
|
+
* one epoll descriptor watches. A DEFER_TASKRUN ring's work runs only when
|
|
43
|
+
* this thread enters the ring, so a set belongs to the thread that opened it
|
|
44
|
+
* (the kernel refuses another thread's submissions under SINGLE_ISSUER). The
|
|
45
|
+
* kernel signals the eventfd when it queues work on an empty list, but on 7.0
|
|
46
|
+
* (and 6.18) that signal can be lost for good (NURING-8: a deferred signal
|
|
47
|
+
* sets a bit nothing clears), and polling the ring's own descriptor is no
|
|
48
|
+
* better, so no sleep on the set is longer than UR_SLEEP_MAX_MS: the timer is
|
|
49
|
+
* the wake the kernel guarantees, and a wait that wakes by it services every
|
|
50
|
+
* flagged ring. A caller that sleeps on `nish_uring_fd` sleeps no longer than
|
|
51
|
+
* `nish_uring_sleep_ms` answers.
|
|
52
|
+
*
|
|
53
|
+
* **A connection is a (ring, slot) pair.** Its recv is multishot; a recv the
|
|
54
|
+
* kernel ends with -ENOBUFS (every buffer lent out) is parked and re-armed on
|
|
55
|
+
* the next `nish_uring_wait`, after the buffers lent by the last one are back,
|
|
56
|
+
* never closed. A send copies its bytes, and a slot keeps one send in flight
|
|
57
|
+
* so that a short send's remainder, which is re-queued, goes out before the
|
|
58
|
+
* bytes queued behind it. A close cancels the slot's armed recv (closing a
|
|
59
|
+
* fixed slot does not, and the recv would keep the socket alive) and then
|
|
60
|
+
* closes the slot. Every completion carries the slot's generation, which the
|
|
61
|
+
* close moves on, so a completion that arrives after the close, or after the
|
|
62
|
+
* slot was reused, is recognised as stale and only gives back its buffer.
|
|
63
|
+
*
|
|
64
|
+
* **Nothing here exits or panics.** A call answers `>= 0` or a negative errno,
|
|
65
|
+
* Linux's number, the shape runtime-net.c uses. `nish_uring_probe` answers
|
|
66
|
+
* whether this kernel and this process may use the design at all: a kernel
|
|
67
|
+
* without io_uring (-ENOSYS), a sandbox or `io_uring_disabled` that refuses
|
|
68
|
+
* it (-EPERM), a kernel too old for the flags (-EINVAL), or one missing a
|
|
69
|
+
* feature or an opcode (-EOPNOTSUPP) all answer negative, and the caller
|
|
70
|
+
* keeps epoll. The calls are raw `syscall()`s against the kernel's ABI, which
|
|
71
|
+
* this file declares itself, so it builds against any libc's headers and
|
|
72
|
+
* needs no liburing.
|
|
73
|
+
*
|
|
74
|
+
* On any other platform, and on WASI, a set is unavailable: Darwin's stub
|
|
75
|
+
* answers -38 (Linux's ENOSYS) from every call, and a WASI build is empty.
|
|
76
|
+
*/
|
|
77
|
+
#if defined(__wasi__) || defined(__wasm__)
|
|
78
|
+
/* ISO C wants at least one declaration in a translation unit. */
|
|
79
|
+
typedef int nish_uring_unused;
|
|
80
|
+
#else
|
|
81
|
+
#if defined(__linux__)
|
|
82
|
+
/* glibc declares `syscall` only with this feature macro. */
|
|
83
|
+
#define _GNU_SOURCE
|
|
84
|
+
#endif
|
|
85
|
+
#include <errno.h>
|
|
86
|
+
#include <stddef.h>
|
|
87
|
+
#include <stdint.h>
|
|
88
|
+
#include <stdlib.h>
|
|
89
|
+
#include <string.h>
|
|
90
|
+
|
|
91
|
+
/* What a set reports to the loop that waits on it. `data` is the set's own
|
|
92
|
+
* buffer and stays valid until the next `nish_uring_wait` on the same set. */
|
|
93
|
+
enum {
|
|
94
|
+
NISH_URING_ACCEPTED = 1, /* a new connection in (ring, slot) */
|
|
95
|
+
NISH_URING_DATA = 2, /* `res` bytes at `data` from (ring, slot) */
|
|
96
|
+
NISH_URING_HUP = 3, /* the peer closed (`res` 0) or failed (`res` < 0) */
|
|
97
|
+
NISH_URING_SEND_FAILED = 4, /* a send on (ring, slot) failed with `res` */
|
|
98
|
+
NISH_URING_ACCEPT_FAILED = 5 /* the listener failed with `res`; accepting stopped */
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
struct nish_uring_event {
|
|
102
|
+
int32_t kind;
|
|
103
|
+
int32_t ring;
|
|
104
|
+
int32_t slot;
|
|
105
|
+
int32_t res;
|
|
106
|
+
const uint8_t *data;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/* Sizes a set is opened with; a field of 0 takes its default. */
|
|
110
|
+
struct nish_uring_opts {
|
|
111
|
+
int32_t slots; /* file-table slots per ring; default RLIMIT_NOFILE's soft limit */
|
|
112
|
+
int32_t rings_max; /* rings a set may grow to; default 64 */
|
|
113
|
+
int32_t sq_entries; /* default 1024 */
|
|
114
|
+
int32_t cq_entries; /* default 8192, at least sq_entries */
|
|
115
|
+
int32_t buf_count; /* provided buffers per ring, a power of two; default 1024 */
|
|
116
|
+
int32_t buf_size; /* bytes per buffer; default 2048 */
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/* Counters the tests read. `max_accepting_rings` is the most rings that ever
|
|
120
|
+
* had an accept in flight at once, which behaviour 3 says must stay 1, and
|
|
121
|
+
* `bound_violations` counts accepts the kernel refused for a full table, which
|
|
122
|
+
* behaviour 2 says must stay 0. */
|
|
123
|
+
struct nish_uring_stats {
|
|
124
|
+
int64_t rings;
|
|
125
|
+
int64_t live;
|
|
126
|
+
int64_t accepting;
|
|
127
|
+
int64_t slots;
|
|
128
|
+
int64_t taskrun_reentries;
|
|
129
|
+
int64_t overflow_drains;
|
|
130
|
+
int64_t nobufs_rearms;
|
|
131
|
+
int64_t send_requeues;
|
|
132
|
+
int64_t accept_errors;
|
|
133
|
+
int64_t bound_violations;
|
|
134
|
+
int64_t max_accepting_rings;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
struct nish_uring_set;
|
|
138
|
+
|
|
139
|
+
int32_t nish_uring_probe(void);
|
|
140
|
+
int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts);
|
|
141
|
+
int32_t nish_uring_fd(const struct nish_uring_set *s);
|
|
142
|
+
int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
|
|
143
|
+
int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
|
|
144
|
+
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
|
|
145
|
+
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
|
|
146
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
|
|
147
|
+
void nish_uring_destroy(struct nish_uring_set *s);
|
|
148
|
+
|
|
149
|
+
#if defined(__linux__)
|
|
150
|
+
#include <sys/epoll.h>
|
|
151
|
+
#include <sys/eventfd.h>
|
|
152
|
+
#include <sys/mman.h>
|
|
153
|
+
#include <sys/resource.h>
|
|
154
|
+
#include <sys/socket.h>
|
|
155
|
+
#include <sys/syscall.h>
|
|
156
|
+
#include <unistd.h>
|
|
157
|
+
|
|
158
|
+
/* The kernel's io_uring ABI (include/uapi/linux/io_uring.h), declared here so
|
|
159
|
+
* the file needs no particular kernel headers: an older sysroot that lacks
|
|
160
|
+
* DEFER_TASKRUN still builds, and the probe finds out at run time. The
|
|
161
|
+
* syscall numbers are the same on every architecture but alpha and mips. */
|
|
162
|
+
#ifndef __NR_io_uring_setup
|
|
163
|
+
#define __NR_io_uring_setup 425
|
|
164
|
+
#endif
|
|
165
|
+
#ifndef __NR_io_uring_enter
|
|
166
|
+
#define __NR_io_uring_enter 426
|
|
167
|
+
#endif
|
|
168
|
+
#ifndef __NR_io_uring_register
|
|
169
|
+
#define __NR_io_uring_register 427
|
|
170
|
+
#endif
|
|
171
|
+
|
|
172
|
+
#define UR_SETUP_CQSIZE (1u << 3)
|
|
173
|
+
#define UR_SETUP_SUBMIT_ALL (1u << 7)
|
|
174
|
+
#define UR_SETUP_COOP_TASKRUN (1u << 8)
|
|
175
|
+
#define UR_SETUP_TASKRUN_FLAG (1u << 9)
|
|
176
|
+
#define UR_SETUP_SINGLE_ISSUER (1u << 12)
|
|
177
|
+
#define UR_SETUP_DEFER_TASKRUN (1u << 13)
|
|
178
|
+
#define UR_FEAT_SINGLE_MMAP (1u << 0)
|
|
179
|
+
#define UR_FEAT_NODROP (1u << 1)
|
|
180
|
+
#define UR_OFF_SQ_RING 0ull
|
|
181
|
+
#define UR_OFF_SQES 0x10000000ull
|
|
182
|
+
#define UR_SQ_CQ_OVERFLOW (1u << 1)
|
|
183
|
+
#define UR_SQ_TASKRUN (1u << 2)
|
|
184
|
+
#define UR_ENTER_GETEVENTS (1u << 0)
|
|
185
|
+
#define UR_SQE_FIXED_FILE (1u << 0)
|
|
186
|
+
#define UR_SQE_BUFFER_SELECT (1u << 5)
|
|
187
|
+
#define UR_SQE_CQE_SKIP_SUCCESS (1u << 6)
|
|
188
|
+
#define UR_CQE_F_BUFFER (1u << 0)
|
|
189
|
+
#define UR_CQE_F_MORE (1u << 1)
|
|
190
|
+
#define UR_CQE_BUFFER_SHIFT 16
|
|
191
|
+
#define UR_OP_ACCEPT 13
|
|
192
|
+
#define UR_OP_ASYNC_CANCEL 14
|
|
193
|
+
#define UR_OP_CLOSE 19
|
|
194
|
+
#define UR_OP_SEND 26
|
|
195
|
+
#define UR_OP_RECV 27
|
|
196
|
+
#define UR_RECV_MULTISHOT (1u << 1)
|
|
197
|
+
#define UR_CANCEL_ALL (1u << 0)
|
|
198
|
+
#define UR_CANCEL_FD (1u << 1)
|
|
199
|
+
#define UR_CANCEL_FD_FIXED (1u << 3)
|
|
200
|
+
#define UR_REGISTER_EVENTFD 4
|
|
201
|
+
#define UR_REGISTER_PROBE 8
|
|
202
|
+
#define UR_REGISTER_FILES2 13
|
|
203
|
+
#define UR_REGISTER_PBUF_RING 22
|
|
204
|
+
#define UR_RSRC_REGISTER_SPARSE (1u << 0)
|
|
205
|
+
#define UR_FILE_INDEX_ALLOC (~0u)
|
|
206
|
+
#define UR_OP_SUPPORTED (1u << 0)
|
|
207
|
+
/* The kernel's own cap on a file table (IORING_MAX_FIXED_FILES). */
|
|
208
|
+
#define UR_MAX_SLOTS (1 << 20)
|
|
209
|
+
|
|
210
|
+
struct ur_sqring_offsets {
|
|
211
|
+
uint32_t head, tail, ring_mask, ring_entries, flags, dropped, array, resv1;
|
|
212
|
+
uint64_t user_addr;
|
|
213
|
+
};
|
|
214
|
+
struct ur_cqring_offsets {
|
|
215
|
+
uint32_t head, tail, ring_mask, ring_entries, overflow, cqes, flags, resv1;
|
|
216
|
+
uint64_t user_addr;
|
|
217
|
+
};
|
|
218
|
+
struct ur_params {
|
|
219
|
+
uint32_t sq_entries, cq_entries, flags, sq_thread_cpu, sq_thread_idle, features, wq_fd, resv[3];
|
|
220
|
+
struct ur_sqring_offsets sq_off;
|
|
221
|
+
struct ur_cqring_offsets cq_off;
|
|
222
|
+
};
|
|
223
|
+
/* One submission. The kernel's struct is a nest of unions; these are the
|
|
224
|
+
* names of the members this file sets, at the kernel's offsets. */
|
|
225
|
+
struct ur_sqe {
|
|
226
|
+
uint8_t opcode;
|
|
227
|
+
uint8_t flags;
|
|
228
|
+
uint16_t ioprio;
|
|
229
|
+
int32_t fd;
|
|
230
|
+
uint64_t off;
|
|
231
|
+
uint64_t addr;
|
|
232
|
+
uint32_t len;
|
|
233
|
+
uint32_t op_flags; /* msg_flags, accept_flags, cancel_flags */
|
|
234
|
+
uint64_t user_data;
|
|
235
|
+
uint16_t buf_group;
|
|
236
|
+
uint16_t personality;
|
|
237
|
+
uint32_t file_index;
|
|
238
|
+
uint64_t addr3;
|
|
239
|
+
uint64_t pad;
|
|
240
|
+
};
|
|
241
|
+
struct ur_cqe {
|
|
242
|
+
uint64_t user_data;
|
|
243
|
+
int32_t res;
|
|
244
|
+
uint32_t flags;
|
|
245
|
+
};
|
|
246
|
+
struct ur_rsrc_register {
|
|
247
|
+
uint32_t nr, flags;
|
|
248
|
+
uint64_t resv2, data, tags;
|
|
249
|
+
};
|
|
250
|
+
struct ur_buf {
|
|
251
|
+
uint64_t addr;
|
|
252
|
+
uint32_t len;
|
|
253
|
+
uint16_t bid;
|
|
254
|
+
uint16_t resv; /* in entry 0, the ring's tail */
|
|
255
|
+
};
|
|
256
|
+
struct ur_buf_reg {
|
|
257
|
+
uint64_t ring_addr;
|
|
258
|
+
uint32_t ring_entries;
|
|
259
|
+
uint16_t bgid;
|
|
260
|
+
uint16_t flags;
|
|
261
|
+
uint64_t resv[3];
|
|
262
|
+
};
|
|
263
|
+
struct ur_probe_op {
|
|
264
|
+
uint8_t op, resv;
|
|
265
|
+
uint16_t flags;
|
|
266
|
+
uint32_t resv2;
|
|
267
|
+
};
|
|
268
|
+
struct ur_probe {
|
|
269
|
+
uint8_t last_op, ops_len;
|
|
270
|
+
uint16_t resv;
|
|
271
|
+
uint32_t resv2[3];
|
|
272
|
+
struct ur_probe_op ops[64];
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
_Static_assert(sizeof(struct ur_params) == 120, "io_uring_params is 120 bytes");
|
|
276
|
+
_Static_assert(sizeof(struct ur_sqe) == 64, "io_uring_sqe is 64 bytes");
|
|
277
|
+
_Static_assert(sizeof(struct ur_cqe) == 16, "io_uring_cqe is 16 bytes");
|
|
278
|
+
_Static_assert(sizeof(struct ur_rsrc_register) == 32, "io_uring_rsrc_register is 32 bytes");
|
|
279
|
+
_Static_assert(sizeof(struct ur_buf) == 16, "io_uring_buf is 16 bytes");
|
|
280
|
+
_Static_assert(sizeof(struct ur_buf_reg) == 40, "io_uring_buf_reg is 40 bytes");
|
|
281
|
+
|
|
282
|
+
/* What a completion's user_data says. A send's is its record's address, which
|
|
283
|
+
* malloc aligns to 8, with UR_SEND in the low three bits; every other op's is
|
|
284
|
+
* the op, the slot's generation and the slot, and none of those ops is 3. */
|
|
285
|
+
enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
|
|
286
|
+
|
|
287
|
+
/* A slot's state word: open, recv armed, recv parked on -ENOBUFS, and the
|
|
288
|
+
* generation in the 24 bits above. */
|
|
289
|
+
#define ST_OPEN 1u
|
|
290
|
+
#define ST_ARMED 2u
|
|
291
|
+
#define ST_PARKED 4u
|
|
292
|
+
#define ST_GEN(st) ((st) >> 8)
|
|
293
|
+
#define GEN_MASK 0xffffffu
|
|
294
|
+
|
|
295
|
+
#define BGID 1
|
|
296
|
+
#define ACCEPT_DEPTH 32
|
|
297
|
+
#define EVENTS_MAX 256
|
|
298
|
+
|
|
299
|
+
/* One send's bytes, owned by the set until the kernel has taken all of them.
|
|
300
|
+
* `next` queues it behind the slot's earlier sends; `prev_all`/`next_all`
|
|
301
|
+
* keep every record on one list so `nish_uring_destroy` can free those the
|
|
302
|
+
* kernel still held. */
|
|
303
|
+
struct ur_send {
|
|
304
|
+
struct ur_send *next, *prev_all, *next_all;
|
|
305
|
+
uint32_t slot, gen;
|
|
306
|
+
uint32_t off, len;
|
|
307
|
+
int in_flight;
|
|
308
|
+
uint8_t data[];
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
struct ur_ring {
|
|
312
|
+
int fd, efd;
|
|
313
|
+
uint32_t *sq_head, *sq_tail, *sq_mask, *sq_array, *sq_flags;
|
|
314
|
+
uint32_t *cq_head, *cq_tail, *cq_mask;
|
|
315
|
+
struct ur_sqe *sqes;
|
|
316
|
+
struct ur_cqe *cqes;
|
|
317
|
+
void *ring_map;
|
|
318
|
+
size_t ring_len, sqes_len;
|
|
319
|
+
uint32_t sq_entries, sq_local_tail, to_submit;
|
|
320
|
+
struct ur_buf *br;
|
|
321
|
+
uint8_t *bufs;
|
|
322
|
+
uint16_t br_tail;
|
|
323
|
+
long live, accepting;
|
|
324
|
+
uint32_t *st; /* per slot */
|
|
325
|
+
struct ur_send **sq_first; /* per slot: the head of its send queue */
|
|
326
|
+
struct ur_send **sq_last; /* per slot: the tail */
|
|
327
|
+
uint32_t *parked; /* slots whose recv waits for buffers */
|
|
328
|
+
uint32_t nparked;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
struct nish_uring_set {
|
|
332
|
+
int listener, ep;
|
|
333
|
+
uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
|
|
334
|
+
int nrings, rings_max, acceptor;
|
|
335
|
+
int accept_stopped;
|
|
336
|
+
struct ur_ring **rings;
|
|
337
|
+
struct ur_send *all;
|
|
338
|
+
long stalled_sends; /* heads that found the SQ full */
|
|
339
|
+
/* Buffers lent to the caller by the last wait, given back by the next. */
|
|
340
|
+
uint32_t lent_ring[EVENTS_MAX];
|
|
341
|
+
uint16_t lent_bid[EVENTS_MAX];
|
|
342
|
+
int nlent;
|
|
343
|
+
struct nish_uring_stats stats;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
|
|
347
|
+
|
|
348
|
+
/* tests/runtime/uring-test.c stands in for setup and register to force the
|
|
349
|
+
* answers a seccomp filter cannot: an -EINVAL that depends on the flags, a
|
|
350
|
+
* missing feature bit or opcode. */
|
|
351
|
+
#ifndef UR_SYS_SETUP
|
|
352
|
+
#define UR_SYS_SETUP(entries, p) syscall(__NR_io_uring_setup, entries, p)
|
|
353
|
+
#endif
|
|
354
|
+
#ifndef UR_SYS_REGISTER
|
|
355
|
+
#define UR_SYS_REGISTER(fd, op, arg, nr) syscall(__NR_io_uring_register, fd, op, arg, nr)
|
|
356
|
+
#endif
|
|
357
|
+
|
|
358
|
+
static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
|
|
359
|
+
|
|
360
|
+
static int ur_enter(struct ur_ring *r, uint32_t submit, uint32_t wait, uint32_t flags) {
|
|
361
|
+
return ur_errno(syscall(__NR_io_uring_enter, r->fd, submit, wait, flags, NULL, 0));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
static int ur_register(int fd, unsigned op, void *arg, unsigned nr) {
|
|
365
|
+
return ur_errno(UR_SYS_REGISTER(fd, op, arg, nr));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
static int ur_setup_try(uint32_t entries, uint32_t cq, struct ur_params *p, uint32_t f) {
|
|
369
|
+
memset(p, 0, sizeof *p);
|
|
370
|
+
p->flags = f;
|
|
371
|
+
p->cq_entries = cq;
|
|
372
|
+
return ur_setup(entries, p);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/* Make a ring fd with the set's flags. A kernel that has TASKRUN_FLAG but
|
|
376
|
+
* wants COOP_TASKRUN beside it, or DEFER_TASKRUN's precondition, answers
|
|
377
|
+
* -EINVAL, so the second try adds COOP_TASKRUN. */
|
|
378
|
+
static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
|
|
379
|
+
uint32_t f = UR_SETUP_SINGLE_ISSUER | UR_SETUP_DEFER_TASKRUN | UR_SETUP_TASKRUN_FLAG | UR_SETUP_SUBMIT_ALL;
|
|
380
|
+
if (cq) f |= UR_SETUP_CQSIZE;
|
|
381
|
+
int fd = ur_setup_try(entries, cq, p, f);
|
|
382
|
+
if (fd == -EINVAL) {
|
|
383
|
+
f |= UR_SETUP_COOP_TASKRUN;
|
|
384
|
+
fd = ur_setup_try(entries, cq, p, f);
|
|
385
|
+
}
|
|
386
|
+
return fd;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
static int32_t ur_probe_once(void) {
|
|
390
|
+
struct ur_params p;
|
|
391
|
+
int fd = ur_setup_flags(4, 0, &p);
|
|
392
|
+
if (fd < 0) return fd;
|
|
393
|
+
int32_t rc = 1;
|
|
394
|
+
if (!(p.features & UR_FEAT_NODROP) || !(p.features & UR_FEAT_SINGLE_MMAP)) rc = -EOPNOTSUPP;
|
|
395
|
+
if (rc > 0) {
|
|
396
|
+
struct ur_probe pr;
|
|
397
|
+
memset(&pr, 0, sizeof pr);
|
|
398
|
+
int e = ur_register(fd, UR_REGISTER_PROBE, &pr, 64);
|
|
399
|
+
if (e < 0) {
|
|
400
|
+
rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
401
|
+
} else {
|
|
402
|
+
static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV};
|
|
403
|
+
for (size_t i = 0; i < sizeof ops; i++) {
|
|
404
|
+
uint8_t op = ops[i];
|
|
405
|
+
if (op > pr.last_op || op >= pr.ops_len || !(pr.ops[op].flags & UR_OP_SUPPORTED)) rc = -EOPNOTSUPP;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (rc > 0) {
|
|
410
|
+
struct ur_rsrc_register files;
|
|
411
|
+
memset(&files, 0, sizeof files);
|
|
412
|
+
files.nr = 1;
|
|
413
|
+
files.flags = UR_RSRC_REGISTER_SPARSE;
|
|
414
|
+
int e = ur_register(fd, UR_REGISTER_FILES2, &files, sizeof files);
|
|
415
|
+
if (e < 0) rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
416
|
+
}
|
|
417
|
+
if (rc > 0) {
|
|
418
|
+
void *br = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
419
|
+
if (br == MAP_FAILED) {
|
|
420
|
+
rc = -ENOMEM;
|
|
421
|
+
} else {
|
|
422
|
+
struct ur_buf_reg reg;
|
|
423
|
+
memset(®, 0, sizeof reg);
|
|
424
|
+
reg.ring_addr = (uint64_t)(uintptr_t)br;
|
|
425
|
+
reg.ring_entries = 8;
|
|
426
|
+
reg.bgid = BGID;
|
|
427
|
+
int e = ur_register(fd, UR_REGISTER_PBUF_RING, ®, 1);
|
|
428
|
+
if (e < 0) rc = e == -EINVAL ? -EOPNOTSUPP : e;
|
|
429
|
+
close(fd);
|
|
430
|
+
fd = -1;
|
|
431
|
+
munmap(br, 4096);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (fd >= 0) close(fd);
|
|
435
|
+
return rc;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/* 1 when this process may use a set, else the negative errno that says why
|
|
439
|
+
* not. A yes or a static refusal is kept for the process: what refuses
|
|
440
|
+
* io_uring (the kernel, a seccomp filter, the sysctl) does not change under
|
|
441
|
+
* a running program in a way it could act on. A refusal for want of a
|
|
442
|
+
* resource (-EMFILE, -ENFILE, -ENOMEM, ...) is not kept, so the next call
|
|
443
|
+
* asks again once descriptors or memory are free. */
|
|
444
|
+
int32_t nish_uring_probe(void) {
|
|
445
|
+
static int32_t cached;
|
|
446
|
+
int32_t c = __atomic_load_n(&cached, __ATOMIC_RELAXED);
|
|
447
|
+
if (c == 0) {
|
|
448
|
+
c = ur_probe_once();
|
|
449
|
+
if (c > 0 || c == -ENOSYS || c == -EPERM || c == -EINVAL || c == -EOPNOTSUPP)
|
|
450
|
+
__atomic_store_n(&cached, c, __ATOMIC_RELAXED);
|
|
451
|
+
}
|
|
452
|
+
return c;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/* Hand the queued submissions to the kernel. -EINTR is tried again; anything
|
|
456
|
+
* else (-EAGAIN for memory, -EBUSY on a kernel that refuses submissions while
|
|
457
|
+
* completions overflow) leaves them queued for the next call to retry. */
|
|
458
|
+
static int ur_flush(struct ur_ring *r) {
|
|
459
|
+
while (r->to_submit) {
|
|
460
|
+
int rc = ur_enter(r, r->to_submit, 0, UR_ENTER_GETEVENTS);
|
|
461
|
+
if (rc == -EINTR) continue;
|
|
462
|
+
if (rc < 0) return rc;
|
|
463
|
+
if (rc == 0) return -EAGAIN;
|
|
464
|
+
r->to_submit -= (uint32_t)rc > r->to_submit ? r->to_submit : (uint32_t)rc;
|
|
465
|
+
}
|
|
466
|
+
return 0;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/* The next free submission entry, zeroed, or NULL when the SQ stays full
|
|
470
|
+
* after a flush. */
|
|
471
|
+
static struct ur_sqe *ur_sqe(struct ur_ring *r) {
|
|
472
|
+
uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
473
|
+
if (r->sq_local_tail - head >= r->sq_entries) {
|
|
474
|
+
(void)ur_flush(r);
|
|
475
|
+
head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
476
|
+
if (r->sq_local_tail - head >= r->sq_entries) return NULL;
|
|
477
|
+
}
|
|
478
|
+
uint32_t i = r->sq_local_tail & *r->sq_mask;
|
|
479
|
+
struct ur_sqe *e = &r->sqes[i];
|
|
480
|
+
memset(e, 0, sizeof *e);
|
|
481
|
+
r->sq_array[i] = i;
|
|
482
|
+
r->sq_local_tail++;
|
|
483
|
+
__atomic_store_n(r->sq_tail, r->sq_local_tail, __ATOMIC_RELEASE);
|
|
484
|
+
r->to_submit++;
|
|
485
|
+
return e;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
static uint64_t ur_tag(unsigned op, uint32_t st, uint32_t slot) {
|
|
489
|
+
return (uint64_t)op | ((uint64_t)(ST_GEN(st) & GEN_MASK) << 8) | ((uint64_t)slot << 32);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid) {
|
|
493
|
+
struct ur_buf *b = &r->br[r->br_tail & (s->buf_count - 1)];
|
|
494
|
+
b->addr = (uint64_t)(uintptr_t)(r->bufs + (size_t)bid * s->buf_size);
|
|
495
|
+
b->len = s->buf_size;
|
|
496
|
+
b->bid = (uint16_t)bid;
|
|
497
|
+
r->br_tail++;
|
|
498
|
+
__atomic_store_n(&r->br[0].resv, r->br_tail, __ATOMIC_RELEASE);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/* Arm a slot's multishot recv, or park it for the next wait when the SQ has
|
|
502
|
+
* no room. */
|
|
503
|
+
static void ur_arm(struct ur_ring *r, uint32_t slot) {
|
|
504
|
+
struct ur_sqe *e = ur_sqe(r);
|
|
505
|
+
if (!e) {
|
|
506
|
+
if (!(r->st[slot] & ST_PARKED)) {
|
|
507
|
+
r->st[slot] |= ST_PARKED;
|
|
508
|
+
r->parked[r->nparked++] = slot;
|
|
509
|
+
}
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
e->opcode = UR_OP_RECV;
|
|
513
|
+
e->fd = (int32_t)slot;
|
|
514
|
+
e->flags = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT;
|
|
515
|
+
e->ioprio = UR_RECV_MULTISHOT;
|
|
516
|
+
e->buf_group = BGID;
|
|
517
|
+
e->user_data = ur_tag(UR_RECV, r->st[slot], slot);
|
|
518
|
+
r->st[slot] |= ST_ARMED;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
static void ur_free_ring(struct nish_uring_set *s, struct ur_ring *r) {
|
|
522
|
+
if (r->efd >= 0) close(r->efd);
|
|
523
|
+
if (r->fd >= 0) close(r->fd);
|
|
524
|
+
if (r->ring_map && r->ring_map != MAP_FAILED) munmap(r->ring_map, r->ring_len);
|
|
525
|
+
if (r->sqes && (void *)r->sqes != MAP_FAILED) munmap(r->sqes, r->sqes_len);
|
|
526
|
+
if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, (size_t)s->buf_count * sizeof(struct ur_buf));
|
|
527
|
+
if (r->bufs && (void *)r->bufs != MAP_FAILED) munmap(r->bufs, (size_t)s->buf_count * s->buf_size);
|
|
528
|
+
free(r->st);
|
|
529
|
+
free(r->sq_first);
|
|
530
|
+
free(r->sq_last);
|
|
531
|
+
free(r->parked);
|
|
532
|
+
free(r);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/* A new ring: the SQ and CQ mapped, a sparse table of `slots`, the buffer
|
|
536
|
+
* ring filled, the eventfd registered and in the set's epoll. Answers the
|
|
537
|
+
* ring's index or a negative errno, and leaves nothing behind on failure. */
|
|
538
|
+
static int ur_add_ring(struct nish_uring_set *s) {
|
|
539
|
+
if (s->nrings >= s->rings_max) return -ENOSPC;
|
|
540
|
+
struct ur_ring *r = calloc(1, sizeof *r);
|
|
541
|
+
if (!r) return -ENOMEM;
|
|
542
|
+
r->fd = -1;
|
|
543
|
+
r->efd = -1;
|
|
544
|
+
int rc = 0;
|
|
545
|
+
struct ur_params p;
|
|
546
|
+
/* The ring itself, and the two features the layer cannot do without. */
|
|
547
|
+
r->fd = ur_setup_flags(s->sq_entries, s->cq_entries, &p);
|
|
548
|
+
if (r->fd < 0) {
|
|
549
|
+
rc = r->fd;
|
|
550
|
+
goto fail;
|
|
551
|
+
}
|
|
552
|
+
if (!(p.features & UR_FEAT_SINGLE_MMAP) || !(p.features & UR_FEAT_NODROP)) {
|
|
553
|
+
rc = -EOPNOTSUPP;
|
|
554
|
+
goto fail;
|
|
555
|
+
}
|
|
556
|
+
/* SQ and CQ share one mapping (FEAT_SINGLE_MMAP); the SQEs are another. */
|
|
557
|
+
r->sq_entries = p.sq_entries;
|
|
558
|
+
size_t sq_len = p.sq_off.array + (size_t)p.sq_entries * sizeof(uint32_t);
|
|
559
|
+
size_t cq_len = p.cq_off.cqes + (size_t)p.cq_entries * sizeof(struct ur_cqe);
|
|
560
|
+
r->ring_len = sq_len > cq_len ? sq_len : cq_len;
|
|
561
|
+
r->ring_map = mmap(NULL, r->ring_len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, r->fd, UR_OFF_SQ_RING);
|
|
562
|
+
if (r->ring_map == MAP_FAILED) {
|
|
563
|
+
rc = -errno;
|
|
564
|
+
goto fail;
|
|
565
|
+
}
|
|
566
|
+
char *m = r->ring_map;
|
|
567
|
+
r->sq_head = (uint32_t *)(void *)(m + p.sq_off.head);
|
|
568
|
+
r->sq_tail = (uint32_t *)(void *)(m + p.sq_off.tail);
|
|
569
|
+
r->sq_mask = (uint32_t *)(void *)(m + p.sq_off.ring_mask);
|
|
570
|
+
r->sq_array = (uint32_t *)(void *)(m + p.sq_off.array);
|
|
571
|
+
r->sq_flags = (uint32_t *)(void *)(m + p.sq_off.flags);
|
|
572
|
+
r->cq_head = (uint32_t *)(void *)(m + p.cq_off.head);
|
|
573
|
+
r->cq_tail = (uint32_t *)(void *)(m + p.cq_off.tail);
|
|
574
|
+
r->cq_mask = (uint32_t *)(void *)(m + p.cq_off.ring_mask);
|
|
575
|
+
r->cqes = (struct ur_cqe *)(void *)(m + p.cq_off.cqes);
|
|
576
|
+
r->sqes_len = (size_t)p.sq_entries * sizeof(struct ur_sqe);
|
|
577
|
+
r->sqes = mmap(NULL, r->sqes_len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, r->fd, UR_OFF_SQES);
|
|
578
|
+
if ((void *)r->sqes == MAP_FAILED) {
|
|
579
|
+
rc = -errno;
|
|
580
|
+
goto fail;
|
|
581
|
+
}
|
|
582
|
+
r->sq_local_tail = *r->sq_tail;
|
|
583
|
+
|
|
584
|
+
/* The sparse file table: one entry per slot, filled by FILE_INDEX_ALLOC. */
|
|
585
|
+
struct ur_rsrc_register files;
|
|
586
|
+
memset(&files, 0, sizeof files);
|
|
587
|
+
files.nr = s->slots;
|
|
588
|
+
files.flags = UR_RSRC_REGISTER_SPARSE;
|
|
589
|
+
rc = ur_register(r->fd, UR_REGISTER_FILES2, &files, sizeof files);
|
|
590
|
+
if (rc < 0) goto fail;
|
|
591
|
+
|
|
592
|
+
/* The provided-buffer ring every multishot recv draws from, full. */
|
|
593
|
+
r->br = mmap(NULL, (size_t)s->buf_count * sizeof(struct ur_buf), PROT_READ | PROT_WRITE,
|
|
594
|
+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
595
|
+
r->bufs = mmap(NULL, (size_t)s->buf_count * s->buf_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
596
|
+
if ((void *)r->br == MAP_FAILED || (void *)r->bufs == MAP_FAILED) {
|
|
597
|
+
rc = -ENOMEM;
|
|
598
|
+
goto fail;
|
|
599
|
+
}
|
|
600
|
+
struct ur_buf_reg reg;
|
|
601
|
+
memset(®, 0, sizeof reg);
|
|
602
|
+
reg.ring_addr = (uint64_t)(uintptr_t)r->br;
|
|
603
|
+
reg.ring_entries = s->buf_count;
|
|
604
|
+
reg.bgid = BGID;
|
|
605
|
+
rc = ur_register(r->fd, UR_REGISTER_PBUF_RING, ®, 1);
|
|
606
|
+
if (rc < 0) goto fail;
|
|
607
|
+
for (uint32_t i = 0; i < s->buf_count; i++) ur_recycle(s, r, i);
|
|
608
|
+
|
|
609
|
+
/* Per-slot state: generation and flags, send queues, parked recvs. */
|
|
610
|
+
r->st = calloc(s->slots, sizeof *r->st);
|
|
611
|
+
r->sq_first = calloc(s->slots, sizeof *r->sq_first);
|
|
612
|
+
r->sq_last = calloc(s->slots, sizeof *r->sq_last);
|
|
613
|
+
r->parked = calloc(s->slots, sizeof *r->parked);
|
|
614
|
+
if (!r->st || !r->sq_first || !r->sq_last || !r->parked) {
|
|
615
|
+
rc = -ENOMEM;
|
|
616
|
+
goto fail;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/* The eventfd the kernel signals for this ring, in the set's epoll with
|
|
620
|
+
* the ring's index as its data. */
|
|
621
|
+
r->efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
|
|
622
|
+
if (r->efd < 0) {
|
|
623
|
+
rc = -errno;
|
|
624
|
+
goto fail;
|
|
625
|
+
}
|
|
626
|
+
rc = ur_register(r->fd, UR_REGISTER_EVENTFD, &r->efd, 1);
|
|
627
|
+
if (rc < 0) goto fail;
|
|
628
|
+
struct epoll_event ev;
|
|
629
|
+
memset(&ev, 0, sizeof ev);
|
|
630
|
+
ev.events = EPOLLIN;
|
|
631
|
+
ev.data.u32 = (uint32_t)s->nrings;
|
|
632
|
+
if (epoll_ctl(s->ep, EPOLL_CTL_ADD, r->efd, &ev) != 0) {
|
|
633
|
+
rc = -errno;
|
|
634
|
+
goto fail;
|
|
635
|
+
}
|
|
636
|
+
s->rings[s->nrings] = r;
|
|
637
|
+
s->stats.rings = ++s->nrings;
|
|
638
|
+
s->stats.slots = (int64_t)s->nrings * s->slots;
|
|
639
|
+
return s->nrings - 1;
|
|
640
|
+
fail:
|
|
641
|
+
ur_free_ring(s, r);
|
|
642
|
+
return rc < 0 ? rc : -EINVAL;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/* Keep min(ACCEPT_DEPTH, free slots) accepts in flight on the accepting ring,
|
|
646
|
+
* and only there (behaviours 2 and 3). A full acceptor with nothing in flight
|
|
647
|
+
* hands the job to the next ring with a free slot, making one when none has
|
|
648
|
+
* and the set may grow; with `rings_max` reached and every ring full,
|
|
649
|
+
* connections wait in the listener's backlog until a close frees a slot. */
|
|
650
|
+
static void ur_top_up(struct nish_uring_set *s) {
|
|
651
|
+
if (s->accept_stopped || s->nrings == 0) return;
|
|
652
|
+
struct ur_ring *r = s->rings[s->acceptor];
|
|
653
|
+
if (r->live + r->accepting >= (long)s->slots && r->accepting == 0) {
|
|
654
|
+
int moved = 0;
|
|
655
|
+
for (int i = 1; i <= s->nrings; i++) {
|
|
656
|
+
int j = (s->acceptor + i) % s->nrings;
|
|
657
|
+
if (s->rings[j]->live < (long)s->slots) {
|
|
658
|
+
s->acceptor = j;
|
|
659
|
+
moved = 1;
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
if (!moved) {
|
|
664
|
+
int j = ur_add_ring(s);
|
|
665
|
+
if (j < 0) return;
|
|
666
|
+
s->acceptor = j;
|
|
667
|
+
}
|
|
668
|
+
r = s->rings[s->acceptor];
|
|
669
|
+
}
|
|
670
|
+
while (r->accepting < ACCEPT_DEPTH && r->live + r->accepting < (long)s->slots) {
|
|
671
|
+
struct ur_sqe *e = ur_sqe(r);
|
|
672
|
+
if (!e) break;
|
|
673
|
+
e->opcode = UR_OP_ACCEPT;
|
|
674
|
+
e->fd = s->listener;
|
|
675
|
+
e->file_index = UR_FILE_INDEX_ALLOC;
|
|
676
|
+
e->user_data = UR_ACCEPT;
|
|
677
|
+
r->accepting++;
|
|
678
|
+
}
|
|
679
|
+
long accepting = 0, rings = 0;
|
|
680
|
+
for (int i = 0; i < s->nrings; i++) {
|
|
681
|
+
accepting += s->rings[i]->accepting;
|
|
682
|
+
rings += s->rings[i]->accepting > 0;
|
|
683
|
+
}
|
|
684
|
+
s->stats.accepting = accepting;
|
|
685
|
+
if (rings > s->stats.max_accepting_rings) s->stats.max_accepting_rings = rings;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/* Submit the slot's first queued send, from where the kernel left off. */
|
|
689
|
+
static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
|
|
690
|
+
struct ur_send *q = r->sq_first[slot];
|
|
691
|
+
if (!q || q->in_flight) return;
|
|
692
|
+
struct ur_sqe *e = ur_sqe(r);
|
|
693
|
+
if (!e) {
|
|
694
|
+
s->stalled_sends++;
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
e->opcode = UR_OP_SEND;
|
|
698
|
+
e->fd = (int32_t)slot;
|
|
699
|
+
e->flags = UR_SQE_FIXED_FILE;
|
|
700
|
+
e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
|
|
701
|
+
e->len = q->len - q->off;
|
|
702
|
+
e->op_flags = MSG_NOSIGNAL;
|
|
703
|
+
e->user_data = (uint64_t)(uintptr_t)q | UR_SEND;
|
|
704
|
+
q->in_flight = 1;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
static void ur_unlink_all(struct nish_uring_set *s, struct ur_send *q) {
|
|
708
|
+
if (q->prev_all) q->prev_all->next_all = q->next_all;
|
|
709
|
+
else s->all = q->next_all;
|
|
710
|
+
if (q->next_all) q->next_all->prev_all = q->prev_all;
|
|
711
|
+
free(q);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/* Drop a slot's queued sends. The one the kernel holds, if any, is left on
|
|
715
|
+
* the all-records list; its completion finds the generation moved on and
|
|
716
|
+
* frees it. */
|
|
717
|
+
static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot) {
|
|
718
|
+
struct ur_send *q = r->sq_first[slot];
|
|
719
|
+
r->sq_first[slot] = r->sq_last[slot] = NULL;
|
|
720
|
+
while (q) {
|
|
721
|
+
struct ur_send *next = q->next;
|
|
722
|
+
q->next = NULL;
|
|
723
|
+
if (!q->in_flight) ur_unlink_all(s, q);
|
|
724
|
+
q = next;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
static int ur_transient_accept(int res) {
|
|
729
|
+
return res == -EAGAIN || res == -EINTR || res == -ECONNABORTED || res == -EPROTO;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
static void ur_event(struct nish_uring_event *ev, int kind, int ring, uint32_t slot, int res, const uint8_t *data) {
|
|
733
|
+
ev->kind = kind;
|
|
734
|
+
ev->ring = ring;
|
|
735
|
+
ev->slot = (int32_t)slot;
|
|
736
|
+
ev->res = res;
|
|
737
|
+
ev->data = data;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/* One completion. Answers 1 when it wrote an event. */
|
|
741
|
+
static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c, struct nish_uring_event *ev) {
|
|
742
|
+
struct ur_ring *r = s->rings[ri];
|
|
743
|
+
uint64_t ud = c->user_data;
|
|
744
|
+
int res = c->res;
|
|
745
|
+
if ((ud & 7) == UR_SEND) {
|
|
746
|
+
struct ur_send *q = (struct ur_send *)(uintptr_t)(ud & ~(uint64_t)7);
|
|
747
|
+
q->in_flight = 0;
|
|
748
|
+
uint32_t slot = q->slot;
|
|
749
|
+
if ((r->st[slot] & ST_OPEN) == 0 || ST_GEN(r->st[slot]) != q->gen || r->sq_first[slot] != q) {
|
|
750
|
+
ur_unlink_all(s, q);
|
|
751
|
+
return 0;
|
|
752
|
+
}
|
|
753
|
+
if (res > 0 && q->off + (uint32_t)res < q->len) {
|
|
754
|
+
q->off += (uint32_t)res;
|
|
755
|
+
s->stats.send_requeues++;
|
|
756
|
+
} else if (res == -EINTR || res == -EAGAIN || res == -ENOBUFS) {
|
|
757
|
+
s->stats.send_requeues++;
|
|
758
|
+
} else if (res < 0) {
|
|
759
|
+
/* q is the head and no longer in flight, so this frees it with the rest. */
|
|
760
|
+
ur_drop_sends(s, r, slot);
|
|
761
|
+
ur_event(ev, NISH_URING_SEND_FAILED, ri, slot, res, NULL);
|
|
762
|
+
return 1;
|
|
763
|
+
} else {
|
|
764
|
+
r->sq_first[slot] = q->next;
|
|
765
|
+
if (!q->next) r->sq_last[slot] = NULL;
|
|
766
|
+
ur_unlink_all(s, q);
|
|
767
|
+
}
|
|
768
|
+
ur_send_head(r, slot, s);
|
|
769
|
+
return 0;
|
|
770
|
+
}
|
|
771
|
+
unsigned op = (unsigned)(ud & 0xff);
|
|
772
|
+
uint32_t gen = (uint32_t)(ud >> 8) & GEN_MASK;
|
|
773
|
+
uint32_t slot = (uint32_t)(ud >> 32);
|
|
774
|
+
if (op == UR_ACCEPT) {
|
|
775
|
+
r->accepting--;
|
|
776
|
+
int wrote = 0;
|
|
777
|
+
if (res >= 0 && (uint32_t)res < s->slots) {
|
|
778
|
+
uint32_t at = (uint32_t)res;
|
|
779
|
+
r->st[at] = (r->st[at] & ~(ST_ARMED | ST_PARKED)) | ST_OPEN;
|
|
780
|
+
r->live++;
|
|
781
|
+
s->stats.live++;
|
|
782
|
+
ur_arm(r, at);
|
|
783
|
+
ur_event(ev, NISH_URING_ACCEPTED, ri, at, 0, NULL);
|
|
784
|
+
wrote = 1;
|
|
785
|
+
} else if (res >= 0) {
|
|
786
|
+
/* A slot past the table the set registered; the kernel never answers
|
|
787
|
+
* one, so it is counted rather than trusted. */
|
|
788
|
+
s->stats.bound_violations++;
|
|
789
|
+
} else if (!ur_transient_accept(res)) {
|
|
790
|
+
s->stats.accept_errors++;
|
|
791
|
+
if (res == -ENFILE) s->stats.bound_violations++;
|
|
792
|
+
if (!s->accept_stopped) {
|
|
793
|
+
s->accept_stopped = 1;
|
|
794
|
+
ur_event(ev, NISH_URING_ACCEPT_FAILED, ri, 0, res, NULL);
|
|
795
|
+
wrote = 1;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
ur_top_up(s);
|
|
799
|
+
return wrote;
|
|
800
|
+
}
|
|
801
|
+
if (op != UR_RECV) return 0; /* a close or cancel; nothing to report */
|
|
802
|
+
int stale = slot >= s->slots || (r->st[slot] & ST_OPEN) == 0 || (ST_GEN(r->st[slot]) & GEN_MASK) != gen;
|
|
803
|
+
int more = (c->flags & UR_CQE_F_MORE) != 0;
|
|
804
|
+
uint32_t bid = c->flags >> UR_CQE_BUFFER_SHIFT;
|
|
805
|
+
int has_buf = (c->flags & UR_CQE_F_BUFFER) != 0 && bid < s->buf_count;
|
|
806
|
+
if (stale) {
|
|
807
|
+
if (has_buf) ur_recycle(s, r, bid);
|
|
808
|
+
return 0;
|
|
809
|
+
}
|
|
810
|
+
if (!more) r->st[slot] &= ~ST_ARMED;
|
|
811
|
+
if (res > 0 && has_buf) {
|
|
812
|
+
s->lent_ring[s->nlent] = (uint32_t)ri;
|
|
813
|
+
s->lent_bid[s->nlent] = (uint16_t)bid;
|
|
814
|
+
s->nlent++;
|
|
815
|
+
if (!more) ur_arm(r, slot);
|
|
816
|
+
ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
|
|
817
|
+
return 1;
|
|
818
|
+
}
|
|
819
|
+
if (has_buf) ur_recycle(s, r, bid);
|
|
820
|
+
if (res == -ENOBUFS) {
|
|
821
|
+
s->stats.nobufs_rearms++;
|
|
822
|
+
if (!more && !(r->st[slot] & ST_PARKED)) {
|
|
823
|
+
r->st[slot] |= ST_PARKED;
|
|
824
|
+
r->parked[r->nparked++] = slot;
|
|
825
|
+
}
|
|
826
|
+
return 0;
|
|
827
|
+
}
|
|
828
|
+
if (more) return 0;
|
|
829
|
+
ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
|
|
830
|
+
return 1;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
/* Take completions until the CQ is empty or `cap` events are written; what
|
|
834
|
+
* does not fit stays in the CQ for the next wait. */
|
|
835
|
+
static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
|
|
836
|
+
struct ur_ring *r = s->rings[ri];
|
|
837
|
+
int n = 0;
|
|
838
|
+
uint32_t head = *r->cq_head;
|
|
839
|
+
for (;;) {
|
|
840
|
+
uint32_t tail = __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE);
|
|
841
|
+
if (head == tail || n >= cap) break;
|
|
842
|
+
struct ur_cqe c = r->cqes[head & *r->cq_mask];
|
|
843
|
+
head++;
|
|
844
|
+
/* Give the entry back before handling it, so a completion's handler that
|
|
845
|
+
* flushes (an accept topping up) frees room in the CQ for what it makes. */
|
|
846
|
+
__atomic_store_n(r->cq_head, head, __ATOMIC_RELEASE);
|
|
847
|
+
n += ur_complete(s, ri, &c, ev + n);
|
|
848
|
+
}
|
|
849
|
+
return n;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/* Reap, submit, and enter again while the kernel says it holds more for this
|
|
853
|
+
* ring (behaviour 4): IORING_SQ_TASKRUN for completions DEFER_TASKRUN has not
|
|
854
|
+
* run, IORING_SQ_CQ_OVERFLOW for ones the CQ had no room for. A flush that
|
|
855
|
+
* answers -EBUSY (a kernel that refuses submissions while completions
|
|
856
|
+
* overflow) is retried at once with no backoff: the reap before it has
|
|
857
|
+
* freed CQ room, so the next pass moves the overflow along. The guard only
|
|
858
|
+
* bounds a kernel that keeps a flag up without progress; the next wait
|
|
859
|
+
* starts again from here. */
|
|
860
|
+
static int ur_service(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
|
|
861
|
+
struct ur_ring *r = s->rings[ri];
|
|
862
|
+
int n = 0;
|
|
863
|
+
for (int guard = 0; guard < 1 << 16; guard++) {
|
|
864
|
+
n += ur_reap(s, ri, ev + n, cap - n);
|
|
865
|
+
int rc = ur_flush(r);
|
|
866
|
+
if (n >= cap) break;
|
|
867
|
+
uint32_t flags = __atomic_load_n(r->sq_flags, __ATOMIC_ACQUIRE);
|
|
868
|
+
if (flags & (UR_SQ_TASKRUN | UR_SQ_CQ_OVERFLOW)) {
|
|
869
|
+
if (flags & UR_SQ_TASKRUN) s->stats.taskrun_reentries++;
|
|
870
|
+
if (flags & UR_SQ_CQ_OVERFLOW) s->stats.overflow_drains++;
|
|
871
|
+
rc = ur_enter(r, 0, 0, UR_ENTER_GETEVENTS);
|
|
872
|
+
if (rc < 0 && rc != -EINTR && rc != -EBUSY && rc != -EAGAIN) break;
|
|
873
|
+
continue;
|
|
874
|
+
}
|
|
875
|
+
if (rc == -EBUSY) continue;
|
|
876
|
+
if (*r->cq_head == __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) break;
|
|
877
|
+
}
|
|
878
|
+
return n;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *o) {
|
|
882
|
+
*out = NULL;
|
|
883
|
+
int32_t ok = nish_uring_probe();
|
|
884
|
+
if (ok < 0) return ok;
|
|
885
|
+
struct nish_uring_opts d;
|
|
886
|
+
memset(&d, 0, sizeof d);
|
|
887
|
+
if (o) d = *o;
|
|
888
|
+
if (listener < 0 || d.slots < 0 || d.rings_max < 0 || d.sq_entries < 0 || d.cq_entries < 0 || d.buf_count < 0 ||
|
|
889
|
+
d.buf_size < 0)
|
|
890
|
+
return -EINVAL;
|
|
891
|
+
if (d.slots == 0) {
|
|
892
|
+
struct rlimit rl;
|
|
893
|
+
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return -errno;
|
|
894
|
+
d.slots = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > UR_MAX_SLOTS ? UR_MAX_SLOTS : (int32_t)rl.rlim_cur;
|
|
895
|
+
}
|
|
896
|
+
if (d.slots > UR_MAX_SLOTS) return -EINVAL;
|
|
897
|
+
if (d.rings_max == 0) d.rings_max = 64;
|
|
898
|
+
if (d.sq_entries == 0) d.sq_entries = 1024;
|
|
899
|
+
if (d.cq_entries == 0) d.cq_entries = 8192;
|
|
900
|
+
if (d.cq_entries < d.sq_entries) d.cq_entries = d.sq_entries;
|
|
901
|
+
if (d.buf_count == 0) d.buf_count = 1024;
|
|
902
|
+
if (d.buf_size == 0) d.buf_size = 2048;
|
|
903
|
+
if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
|
|
904
|
+
return -EINVAL;
|
|
905
|
+
struct nish_uring_set *s = calloc(1, sizeof *s);
|
|
906
|
+
if (!s) return -ENOMEM;
|
|
907
|
+
s->listener = listener;
|
|
908
|
+
s->slots = (uint32_t)d.slots;
|
|
909
|
+
s->rings_max = d.rings_max;
|
|
910
|
+
s->sq_entries = (uint32_t)d.sq_entries;
|
|
911
|
+
s->cq_entries = (uint32_t)d.cq_entries;
|
|
912
|
+
s->buf_count = (uint32_t)d.buf_count;
|
|
913
|
+
s->buf_size = (uint32_t)d.buf_size;
|
|
914
|
+
s->rings = calloc((size_t)d.rings_max, sizeof *s->rings);
|
|
915
|
+
s->ep = epoll_create1(EPOLL_CLOEXEC);
|
|
916
|
+
if (!s->rings || s->ep < 0) {
|
|
917
|
+
int rc = s->ep < 0 ? -errno : -ENOMEM;
|
|
918
|
+
if (s->ep >= 0) close(s->ep);
|
|
919
|
+
free(s->rings);
|
|
920
|
+
free(s);
|
|
921
|
+
return rc;
|
|
922
|
+
}
|
|
923
|
+
int rc = ur_add_ring(s);
|
|
924
|
+
if (rc < 0) {
|
|
925
|
+
close(s->ep);
|
|
926
|
+
free(s->rings);
|
|
927
|
+
free(s);
|
|
928
|
+
return rc;
|
|
929
|
+
}
|
|
930
|
+
ur_top_up(s);
|
|
931
|
+
(void)ur_flush(s->rings[0]);
|
|
932
|
+
*out = s;
|
|
933
|
+
return 0;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/* The epoll descriptor a caller's own loop may wait on beside other sources.
|
|
937
|
+
* It is readable when a ring's eventfd was signalled since that ring was last
|
|
938
|
+
* serviced. It is not readable for events a wait left behind because `cap`
|
|
939
|
+
* was full, so a caller that got `cap` events calls wait again (with a
|
|
940
|
+
* timeout of 0) before it sleeps on this descriptor. Sends and closes are
|
|
941
|
+
* only queued: the next wait hands them to the kernel, so a caller calls
|
|
942
|
+
* wait after them, too, before it sleeps here. A wait returns with nothing
|
|
943
|
+
* left queued unless the kernel refused the submission (-EBUSY while
|
|
944
|
+
* completions overflow, -EAGAIN or -ENOMEM for memory): then the ring has
|
|
945
|
+
* completions to reap or the refusal is transient, and the next wait
|
|
946
|
+
* submits them again.
|
|
947
|
+
*
|
|
948
|
+
* Readable is not the whole answer: the kernel can hold a ring's work with
|
|
949
|
+
* IORING_SQ_TASKRUN up and its eventfd never signalled (NURING-8). A caller
|
|
950
|
+
* that sleeps on this descriptor, alone or beside other sources, asks
|
|
951
|
+
* `nish_uring_sleep_ms` right before it sleeps and sleeps no longer than it
|
|
952
|
+
* answers, then calls wait (with a timeout of 0) whether or not the
|
|
953
|
+
* descriptor became readable. */
|
|
954
|
+
int32_t nish_uring_fd(const struct nish_uring_set *s) { return s->ep; }
|
|
955
|
+
|
|
956
|
+
/* Whether the kernel holds work for one of the ring's completions to post:
|
|
957
|
+
* deferred task work (IORING_SQ_TASKRUN) or completions the CQ had no room
|
|
958
|
+
* for (IORING_SQ_CQ_OVERFLOW). Its eventfd may say nothing of either. */
|
|
959
|
+
static int ur_flagged(const struct ur_ring *r) {
|
|
960
|
+
return (__atomic_load_n(r->sq_flags, __ATOMIC_ACQUIRE) & (UR_SQ_TASKRUN | UR_SQ_CQ_OVERFLOW)) != 0;
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/* The longest sleep on a set: the kernel's eventfd signal can be lost
|
|
964
|
+
* (NURING-8), so the timer is what wakes a sleeper to service a ring the
|
|
965
|
+
* kernel flagged. 10 ms bounds a lost wake's delay; idle, it costs a set 100
|
|
966
|
+
* wakes a second. */
|
|
967
|
+
#define UR_SLEEP_MAX_MS 10
|
|
968
|
+
|
|
969
|
+
/* How long a caller may sleep on `nish_uring_fd` before it calls wait, given
|
|
970
|
+
* the longest it wants (`timeout_ms`, -1 for ever). Answers 0 while a wait has
|
|
971
|
+
* work it can do now: a ring the kernel flagged, completions not yet reaped,
|
|
972
|
+
* buffers the last wait lent, or recvs parked or sends stalled on a full SQ
|
|
973
|
+
* that every ring has room for again. Otherwise answers `timeout_ms` cut to
|
|
974
|
+
* UR_SLEEP_MAX_MS. Submissions the kernel refused, and what waits on them, are
|
|
975
|
+
* retried after that bound, not at once, so a refusal that persists does not
|
|
976
|
+
* spin the caller. */
|
|
977
|
+
int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms) {
|
|
978
|
+
int room = 1, parked = 0;
|
|
979
|
+
for (int ri = 0; ri < s->nrings; ri++) {
|
|
980
|
+
const struct ur_ring *r = s->rings[ri];
|
|
981
|
+
if (ur_flagged(r) || *r->cq_head != __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) return 0;
|
|
982
|
+
if (r->to_submit) room = 0;
|
|
983
|
+
if (r->nparked) parked = 1;
|
|
984
|
+
}
|
|
985
|
+
if (s->nlent || (room && (parked || s->stalled_sends))) return 0;
|
|
986
|
+
return timeout_ms < 0 || timeout_ms > UR_SLEEP_MAX_MS ? UR_SLEEP_MAX_MS : timeout_ms;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/* What the last wait left to do: give back the buffers it lent, re-arm the
|
|
990
|
+
* recvs it parked, and submit the sends that found the SQ full. */
|
|
991
|
+
static void ur_catch_up(struct nish_uring_set *s) {
|
|
992
|
+
for (int i = 0; i < s->nlent; i++) ur_recycle(s, s->rings[s->lent_ring[i]], s->lent_bid[i]);
|
|
993
|
+
s->nlent = 0;
|
|
994
|
+
for (int ri = 0; ri < s->nrings; ri++) {
|
|
995
|
+
struct ur_ring *r = s->rings[ri];
|
|
996
|
+
uint32_t n = r->nparked;
|
|
997
|
+
r->nparked = 0;
|
|
998
|
+
for (uint32_t i = 0; i < n; i++) {
|
|
999
|
+
uint32_t slot = r->parked[i];
|
|
1000
|
+
r->st[slot] &= ~ST_PARKED;
|
|
1001
|
+
if ((r->st[slot] & (ST_OPEN | ST_ARMED)) == ST_OPEN) ur_arm(r, slot);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
/* Scanning every slot of every ring costs rings * slots, but only after a
|
|
1005
|
+
* send found the SQ full, which a queue of `sq_entries` (1,024 by default)
|
|
1006
|
+
* makes rare; keeping a list of the stalled slots would cost every send. */
|
|
1007
|
+
if (s->stalled_sends) {
|
|
1008
|
+
s->stalled_sends = 0;
|
|
1009
|
+
for (int ri = 0; ri < s->nrings; ri++)
|
|
1010
|
+
for (uint32_t slot = 0; slot < s->slots; slot++)
|
|
1011
|
+
if (s->rings[ri]->sq_first[slot]) ur_send_head(s->rings[ri], slot, s);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/* Hand every ring's queued submissions to the kernel before a wait returns
|
|
1016
|
+
* or sleeps. Servicing a ring submits its own, but an accept or a close there
|
|
1017
|
+
* can move the accepting job to a ring the wait already passed or will not
|
|
1018
|
+
* reach (`cap` full, or not ready), and that ring's new accepts would sit in
|
|
1019
|
+
* user space while the caller sleeps on `nish_uring_fd`. A refused flush
|
|
1020
|
+
* leaves them queued for the next wait, as ur_flush says. */
|
|
1021
|
+
static void ur_submit_queued(struct nish_uring_set *s) {
|
|
1022
|
+
for (int ri = 0; ri < s->nrings; ri++)
|
|
1023
|
+
if (s->rings[ri]->to_submit) (void)ur_flush(s->rings[ri]);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/* Wait for events and write at most `cap` of them, at most 256. Answers the
|
|
1027
|
+
* count or a negative errno. A DATA event's bytes are the set's buffer until
|
|
1028
|
+
* the next wait. A `timeout_ms` of -1 waits until there are events (or answers
|
|
1029
|
+
* 0 when a signal interrupts it); 0 does not sleep. Any other timeout is a
|
|
1030
|
+
* most, not a least: the wait sleeps no longer than `nish_uring_sleep_ms`
|
|
1031
|
+
* answers (NURING-8), so it can answer 0 early, while work is pending or
|
|
1032
|
+
* after UR_SLEEP_MAX_MS, and a caller that wants the whole timeout waits again
|
|
1033
|
+
* for what is left of it. */
|
|
1034
|
+
int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap) {
|
|
1035
|
+
if (cap <= 0) return -EINVAL;
|
|
1036
|
+
if (cap > EVENTS_MAX) cap = EVENTS_MAX;
|
|
1037
|
+
again:
|
|
1038
|
+
ur_catch_up(s);
|
|
1039
|
+
int n = 0;
|
|
1040
|
+
for (int ri = 0; ri < s->nrings && n < cap; ri++) n += ur_service(s, ri, ev + n, cap - n);
|
|
1041
|
+
ur_submit_queued(s);
|
|
1042
|
+
if (n > 0) return n;
|
|
1043
|
+
/* No eventfd is trusted to end the sleep (NURING-8): the bound does, and the
|
|
1044
|
+
* flagged rings are serviced below whether an eventfd was read or not. */
|
|
1045
|
+
struct epoll_event ready[64];
|
|
1046
|
+
int k = epoll_wait(s->ep, ready, 64, nish_uring_sleep_ms(s, timeout_ms));
|
|
1047
|
+
if (k < 0) return errno == EINTR ? 0 : -errno;
|
|
1048
|
+
for (int i = 0; i < k && n < cap; i++) {
|
|
1049
|
+
uint32_t ri = ready[i].data.u32;
|
|
1050
|
+
if (ri >= (uint32_t)s->nrings) continue;
|
|
1051
|
+
struct ur_ring *r = s->rings[ri];
|
|
1052
|
+
uint64_t v;
|
|
1053
|
+
(void)!read(r->efd, &v, sizeof v);
|
|
1054
|
+
(void)ur_enter(r, 0, 0, UR_ENTER_GETEVENTS);
|
|
1055
|
+
n += ur_service(s, (int)ri, ev + n, cap - n);
|
|
1056
|
+
}
|
|
1057
|
+
for (int ri = 0; ri < s->nrings && n < cap; ri++)
|
|
1058
|
+
if (ur_flagged(s->rings[ri])) n += ur_service(s, ri, ev + n, cap - n);
|
|
1059
|
+
ur_submit_queued(s);
|
|
1060
|
+
if (n == 0 && timeout_ms < 0) goto again;
|
|
1061
|
+
return n;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1065
|
+
if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return NULL;
|
|
1066
|
+
struct ur_ring *r = s->rings[ring];
|
|
1067
|
+
return (r->st[slot] & ST_OPEN) ? r : NULL;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
|
|
1071
|
+
* is free on return; a failure comes back later as a SEND_FAILED event. */
|
|
1072
|
+
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
|
|
1073
|
+
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1074
|
+
if (!r) return -EBADF;
|
|
1075
|
+
if (n < 0) return -EINVAL;
|
|
1076
|
+
if (n == 0) return 0;
|
|
1077
|
+
struct ur_send *q = malloc(sizeof *q + (size_t)n);
|
|
1078
|
+
if (!q) return -ENOMEM;
|
|
1079
|
+
memcpy(q->data, p, (size_t)n);
|
|
1080
|
+
q->next = NULL;
|
|
1081
|
+
q->prev_all = NULL;
|
|
1082
|
+
q->next_all = s->all;
|
|
1083
|
+
if (s->all) s->all->prev_all = q;
|
|
1084
|
+
s->all = q;
|
|
1085
|
+
q->slot = (uint32_t)slot;
|
|
1086
|
+
q->gen = ST_GEN(r->st[slot]);
|
|
1087
|
+
q->off = 0;
|
|
1088
|
+
q->len = (uint32_t)n;
|
|
1089
|
+
q->in_flight = 0;
|
|
1090
|
+
if (r->sq_last[slot]) {
|
|
1091
|
+
r->sq_last[slot]->next = q;
|
|
1092
|
+
} else {
|
|
1093
|
+
r->sq_first[slot] = q;
|
|
1094
|
+
}
|
|
1095
|
+
r->sq_last[slot] = q;
|
|
1096
|
+
ur_send_head(r, (uint32_t)slot, s);
|
|
1097
|
+
return n;
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/* Cancel the slot's recv, close the slot, and move its generation on, so
|
|
1101
|
+
* every completion still on its way for it is stale. The freed slot goes to
|
|
1102
|
+
* the accepting job. -EAGAIN when the SQ has no room for both entries, with
|
|
1103
|
+
* nothing changed. */
|
|
1104
|
+
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1105
|
+
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1106
|
+
if (!r) return -EBADF;
|
|
1107
|
+
uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
1108
|
+
if (r->sq_local_tail - head + 2 > r->sq_entries) {
|
|
1109
|
+
(void)ur_flush(r);
|
|
1110
|
+
head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
1111
|
+
if (r->sq_local_tail - head + 2 > r->sq_entries) return -EAGAIN;
|
|
1112
|
+
}
|
|
1113
|
+
uint32_t st = r->st[slot];
|
|
1114
|
+
struct ur_sqe *e = ur_sqe(r);
|
|
1115
|
+
e->opcode = UR_OP_ASYNC_CANCEL;
|
|
1116
|
+
e->fd = slot;
|
|
1117
|
+
e->op_flags = UR_CANCEL_FD | UR_CANCEL_FD_FIXED | UR_CANCEL_ALL;
|
|
1118
|
+
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1119
|
+
e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
|
|
1120
|
+
e = ur_sqe(r);
|
|
1121
|
+
e->opcode = UR_OP_CLOSE;
|
|
1122
|
+
e->file_index = (uint32_t)slot + 1;
|
|
1123
|
+
e->flags = UR_SQE_CQE_SKIP_SUCCESS;
|
|
1124
|
+
e->user_data = ur_tag(UR_CLOSE, st, (uint32_t)slot);
|
|
1125
|
+
ur_drop_sends(s, r, (uint32_t)slot);
|
|
1126
|
+
r->st[slot] = ((ST_GEN(st) + 1) & GEN_MASK) << 8;
|
|
1127
|
+
r->live--;
|
|
1128
|
+
s->stats.live--;
|
|
1129
|
+
ur_top_up(s);
|
|
1130
|
+
return 0;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) { *st = s->stats; }
|
|
1134
|
+
|
|
1135
|
+
/* Close every ring, which ends whatever the kernel still held, and free the
|
|
1136
|
+
* sends it held them for. The caller's listener stays open: it is theirs. */
|
|
1137
|
+
void nish_uring_destroy(struct nish_uring_set *s) {
|
|
1138
|
+
if (!s) return;
|
|
1139
|
+
for (int i = 0; i < s->nrings; i++) ur_free_ring(s, s->rings[i]);
|
|
1140
|
+
while (s->all) {
|
|
1141
|
+
struct ur_send *q = s->all;
|
|
1142
|
+
s->all = q->next_all;
|
|
1143
|
+
free(q);
|
|
1144
|
+
}
|
|
1145
|
+
close(s->ep);
|
|
1146
|
+
free(s->rings);
|
|
1147
|
+
free(s);
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
#else /* not Linux: no io_uring, so a set is never available */
|
|
1151
|
+
|
|
1152
|
+
int32_t nish_uring_probe(void) { return -38; }
|
|
1153
|
+
int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts) {
|
|
1154
|
+
(void)listener;
|
|
1155
|
+
(void)opts;
|
|
1156
|
+
*out = NULL;
|
|
1157
|
+
return -38;
|
|
1158
|
+
}
|
|
1159
|
+
int32_t nish_uring_fd(const struct nish_uring_set *s) {
|
|
1160
|
+
(void)s;
|
|
1161
|
+
return -38;
|
|
1162
|
+
}
|
|
1163
|
+
int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms) {
|
|
1164
|
+
(void)s;
|
|
1165
|
+
return timeout_ms;
|
|
1166
|
+
}
|
|
1167
|
+
int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap) {
|
|
1168
|
+
(void)s;
|
|
1169
|
+
(void)timeout_ms;
|
|
1170
|
+
(void)ev;
|
|
1171
|
+
(void)cap;
|
|
1172
|
+
return -38;
|
|
1173
|
+
}
|
|
1174
|
+
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
|
|
1175
|
+
(void)s;
|
|
1176
|
+
(void)ring;
|
|
1177
|
+
(void)slot;
|
|
1178
|
+
(void)p;
|
|
1179
|
+
(void)n;
|
|
1180
|
+
return -38;
|
|
1181
|
+
}
|
|
1182
|
+
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1183
|
+
(void)s;
|
|
1184
|
+
(void)ring;
|
|
1185
|
+
(void)slot;
|
|
1186
|
+
return -38;
|
|
1187
|
+
}
|
|
1188
|
+
void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
|
|
1189
|
+
(void)s;
|
|
1190
|
+
memset(st, 0, sizeof *st);
|
|
1191
|
+
}
|
|
1192
|
+
void nish_uring_destroy(struct nish_uring_set *s) { (void)s; }
|
|
1193
|
+
#endif
|
|
1194
|
+
#endif
|