nish 0.41.0 → 0.44.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.
Files changed (47) hide show
  1. package/README.md +16 -4
  2. package/bin/nish-skills.js +233 -0
  3. package/docs/AI.md +282 -126
  4. package/llms.txt +4 -0
  5. package/package.json +14 -11
  6. package/runtime/core.d.ts +16 -0
  7. package/runtime/core.mjs +5 -0
  8. package/runtime/nish.d.ts +204 -33
  9. package/runtime/nish.h +19 -3
  10. package/runtime/nish.mjs +5 -1
  11. package/runtime/runtime-cross.c +15 -0
  12. package/runtime/runtime-net.c +876 -10
  13. package/runtime/runtime-region.c +21 -0
  14. package/runtime/runtime-sandbox.c +112 -15
  15. package/runtime/runtime-text.c +11 -0
  16. package/runtime/runtime-uring.c +533 -77
  17. package/runtime/runtime-wasm.c +3 -0
  18. package/runtime/runtime.c +474 -5
  19. package/runtime/sqlite/README.md +16 -3
  20. package/runtime/sqlite/nish-sqlite.c +940 -29
  21. package/scripts/build.sh +125 -29
  22. package/scripts/link-skills.mjs +1039 -0
  23. package/scripts/postinstall.mjs +43 -0
  24. package/skills/writing-nish/SKILL.md +144 -0
  25. package/std/README.md +3 -3
  26. package/std/crypto/base64url.ts +102 -1
  27. package/std/crypto/scram.ts +142 -0
  28. package/std/crypto/sha1.ts +99 -41
  29. package/std/host/ring.ts +1 -1
  30. package/std/host/text.ts +1 -1
  31. package/std/net/fetch.ts +9 -1
  32. package/std/net/handler.ts +79 -15
  33. package/std/net/http-server.ts +2 -3
  34. package/std/net/http1-server.ts +294 -51
  35. package/std/net/http3-server.ts +26 -15
  36. package/std/net/http3.ts +2 -2
  37. package/std/net/oidc.ts +1107 -0
  38. package/std/net/postgres-async.ts +64 -33
  39. package/std/net/postgres.ts +65 -76
  40. package/std/net/quic-conn-ack.ts +57 -7
  41. package/std/net/quic-frame.ts +104 -2
  42. package/std/net/quic-listener.ts +9 -2
  43. package/std/net/quic.ts +16 -1
  44. package/std/net/serve.ts +10 -8
  45. package/std/net/websocket.ts +188 -49
  46. package/std/sqlite.ts +325 -0
  47. package/std/text.ts +80 -11
@@ -95,7 +95,8 @@ enum {
95
95
  NISH_URING_DATA = 2, /* `res` bytes at `data` from (ring, slot) */
96
96
  NISH_URING_HUP = 3, /* the peer closed (`res` 0) or failed (`res` < 0) */
97
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 */
98
+ NISH_URING_ACCEPT_FAILED = 5, /* the listener failed with `res`; accepting stopped */
99
+ NISH_URING_ACCEPT_PAUSED = 6 /* an accept found memory short (`res`); accepting resumes after a back-off */
99
100
  };
100
101
 
101
102
  struct nish_uring_event {
@@ -130,6 +131,7 @@ struct nish_uring_stats {
130
131
  int64_t nobufs_rearms;
131
132
  int64_t send_requeues;
132
133
  int64_t accept_errors;
134
+ int64_t accept_pauses;
133
135
  int64_t bound_violations;
134
136
  int64_t max_accepting_rings;
135
137
  };
@@ -137,22 +139,29 @@ struct nish_uring_stats {
137
139
  struct nish_uring_set;
138
140
 
139
141
  int32_t nish_uring_probe(void);
142
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
143
+ int32_t nish_uring_pooled(void);
140
144
  int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts);
141
145
  int32_t nish_uring_fd(const struct nish_uring_set *s);
142
146
  int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
143
147
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
144
148
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
145
149
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
150
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
151
+ int32_t nish_uring_full(const struct nish_uring_set *s);
152
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
146
153
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st);
147
154
  void nish_uring_destroy(struct nish_uring_set *s);
148
155
 
149
156
  #if defined(__linux__)
157
+ #include <fcntl.h>
150
158
  #include <sys/epoll.h>
151
159
  #include <sys/eventfd.h>
152
160
  #include <sys/mman.h>
153
161
  #include <sys/resource.h>
154
162
  #include <sys/socket.h>
155
163
  #include <sys/syscall.h>
164
+ #include <time.h>
156
165
  #include <unistd.h>
157
166
 
158
167
  /* The kernel's io_uring ABI (include/uapi/linux/io_uring.h), declared here so
@@ -170,6 +179,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
170
179
  #endif
171
180
 
172
181
  #define UR_SETUP_CQSIZE (1u << 3)
182
+ #define UR_SETUP_R_DISABLED (1u << 6)
173
183
  #define UR_SETUP_SUBMIT_ALL (1u << 7)
174
184
  #define UR_SETUP_COOP_TASKRUN (1u << 8)
175
185
  #define UR_SETUP_TASKRUN_FLAG (1u << 9)
@@ -188,11 +198,13 @@ void nish_uring_destroy(struct nish_uring_set *s);
188
198
  #define UR_CQE_F_BUFFER (1u << 0)
189
199
  #define UR_CQE_F_MORE (1u << 1)
190
200
  #define UR_CQE_BUFFER_SHIFT 16
201
+ #define UR_OP_NOP 0
191
202
  #define UR_OP_ACCEPT 13
192
203
  #define UR_OP_ASYNC_CANCEL 14
193
204
  #define UR_OP_CLOSE 19
194
205
  #define UR_OP_SEND 26
195
206
  #define UR_OP_RECV 27
207
+ #define UR_OP_SHUTDOWN 34
196
208
  #define UR_RECV_MULTISHOT (1u << 1)
197
209
  #define UR_CANCEL_ALL (1u << 0)
198
210
  #define UR_CANCEL_FD (1u << 1)
@@ -200,7 +212,11 @@ void nish_uring_destroy(struct nish_uring_set *s);
200
212
  #define UR_REGISTER_EVENTFD 4
201
213
  #define UR_REGISTER_PROBE 8
202
214
  #define UR_REGISTER_FILES2 13
215
+ #define UR_REGISTER_RESTRICTIONS 11
216
+ #define UR_REGISTER_ENABLE_RINGS 12
203
217
  #define UR_REGISTER_PBUF_RING 22
218
+ #define UR_RESTRICTION_SQE_OP 1
219
+ #define UR_RESTRICTION_SQE_FLAGS_ALLOWED 2
204
220
  #define UR_RSRC_REGISTER_SPARSE (1u << 0)
205
221
  #define UR_FILE_INDEX_ALLOC (~0u)
206
222
  #define UR_OP_SUPPORTED (1u << 0)
@@ -271,6 +287,14 @@ struct ur_probe {
271
287
  uint32_t resv2[3];
272
288
  struct ur_probe_op ops[64];
273
289
  };
290
+ /* One entry of IORING_REGISTER_RESTRICTIONS: `arg` is the union of
291
+ * register_op, sqe_op and sqe_flags. */
292
+ struct ur_restriction {
293
+ uint16_t opcode;
294
+ uint8_t arg;
295
+ uint8_t resv;
296
+ uint32_t resv2[3];
297
+ };
274
298
 
275
299
  _Static_assert(sizeof(struct ur_params) == 120, "io_uring_params is 120 bytes");
276
300
  _Static_assert(sizeof(struct ur_sqe) == 64, "io_uring_sqe is 64 bytes");
@@ -278,33 +302,45 @@ _Static_assert(sizeof(struct ur_cqe) == 16, "io_uring_cqe is 16 bytes");
278
302
  _Static_assert(sizeof(struct ur_rsrc_register) == 32, "io_uring_rsrc_register is 32 bytes");
279
303
  _Static_assert(sizeof(struct ur_buf) == 16, "io_uring_buf is 16 bytes");
280
304
  _Static_assert(sizeof(struct ur_buf_reg) == 40, "io_uring_buf_reg is 40 bytes");
305
+ _Static_assert(sizeof(struct ur_restriction) == 16, "io_uring_restriction is 16 bytes");
281
306
 
282
307
  /* What a completion's user_data says. A send's is its record's address, which
283
308
  * malloc aligns to 8, with UR_SEND in the low three bits; every other op's is
284
309
  * the op, the slot's generation and the slot, and none of those ops is 3. */
285
310
  enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
286
311
 
287
- /* A slot's state word: open, recv armed, recv parked on -ENOBUFS, and the
288
- * generation in the 24 bits above. */
312
+ /* A slot's state word: open, recv armed, recv parked on -ENOBUFS, recv held
313
+ * by its caller (`nish_uring_hold`), hung up (its recv ended for good, so it
314
+ * is never armed again until the slot is accepted anew), and the generation
315
+ * in the 24 bits above. */
289
316
  #define ST_OPEN 1u
290
317
  #define ST_ARMED 2u
291
318
  #define ST_PARKED 4u
319
+ #define ST_HELD 8u
320
+ #define ST_HUP 16u
292
321
  #define ST_GEN(st) ((st) >> 8)
293
322
  #define GEN_MASK 0xffffffu
294
323
 
295
324
  #define BGID 1
296
325
  #define ACCEPT_DEPTH 32
326
+ /* The back-off after an accept that found memory short: 10 ms, doubling to
327
+ * 640 ms while accepts keep failing. Connections wait in the listener's
328
+ * backlog meanwhile, as they do at a full set. */
329
+ #define ACCEPT_BACKOFF_MIN_MS 10
330
+ #define ACCEPT_BACKOFF_MAX_MS 640
297
331
  #define EVENTS_MAX 256
298
332
 
299
333
  /* One send's bytes, owned by the set until the kernel has taken all of them.
300
334
  * `next` queues it behind the slot's earlier sends; `prev_all`/`next_all`
301
335
  * keep every record on one list so `nish_uring_destroy` can free those the
302
- * kernel still held. */
336
+ * kernel still held. `how` is -1 for bytes, or a shutdown's 0 to 2: that
337
+ * record carries none and goes to the kernel as a SHUTDOWN once the sends
338
+ * queued before it are done, so the peer reads them before the FIN. */
303
339
  struct ur_send {
304
340
  struct ur_send *next, *prev_all, *next_all;
305
341
  uint32_t slot, gen;
306
342
  uint32_t off, len;
307
- int in_flight;
343
+ int in_flight, how;
308
344
  uint8_t data[];
309
345
  };
310
346
 
@@ -319,11 +355,13 @@ struct ur_ring {
319
355
  uint32_t sq_entries, sq_local_tail, to_submit;
320
356
  struct ur_buf *br;
321
357
  uint8_t *bufs;
358
+ size_t br_len, bufs_len;
322
359
  uint16_t br_tail;
323
360
  long live, accepting;
324
361
  uint32_t *st; /* per slot */
325
362
  struct ur_send **sq_first; /* per slot: the head of its send queue */
326
363
  struct ur_send **sq_last; /* per slot: the tail */
364
+ uint32_t *qbytes; /* per slot: bytes queued and not yet sent */
327
365
  uint32_t *parked; /* slots whose recv waits for buffers */
328
366
  uint32_t nparked;
329
367
  };
@@ -333,6 +371,12 @@ struct nish_uring_set {
333
371
  uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
334
372
  int nrings, rings_max, acceptor;
335
373
  int accept_stopped;
374
+ /* Accepting waits until `resume_ms` after an accept found memory short; the
375
+ * wait doubles from ACCEPT_BACKOFF_MIN_MS while it keeps failing and starts
376
+ * again from the minimum once an accept succeeds. */
377
+ int accept_paused;
378
+ int32_t backoff_ms;
379
+ int64_t resume_ms;
336
380
  struct ur_ring **rings;
337
381
  struct ur_send *all;
338
382
  long stalled_sends; /* heads that found the SQ full */
@@ -354,6 +398,11 @@ static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
354
398
  #ifndef UR_SYS_REGISTER
355
399
  #define UR_SYS_REGISTER(fd, op, arg, nr) syscall(__NR_io_uring_register, fd, op, arg, nr)
356
400
  #endif
401
+ /* What an accept completion answered; uring-test.c turns one into -ENOMEM,
402
+ * which the kernel gives only when memory is short. */
403
+ #ifndef UR_ACCEPT_RES
404
+ #define UR_ACCEPT_RES(res) (res)
405
+ #endif
357
406
 
358
407
  static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
359
408
 
@@ -386,26 +435,28 @@ static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
386
435
  return fd;
387
436
  }
388
437
 
438
+ /* Whether the ring `fd` has every opcode the layer submits: 1, -EOPNOTSUPP
439
+ * when one is missing or the kernel has no probe, or the register's errno. */
440
+ static int32_t ur_ops_ok(int fd) {
441
+ struct ur_probe pr;
442
+ memset(&pr, 0, sizeof pr);
443
+ int e = ur_register(fd, UR_REGISTER_PROBE, &pr, 64);
444
+ if (e < 0) return e == -EINVAL ? -EOPNOTSUPP : e;
445
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV, UR_OP_SHUTDOWN};
446
+ for (size_t i = 0; i < sizeof ops; i++) {
447
+ uint8_t op = ops[i];
448
+ if (op > pr.last_op || op >= pr.ops_len || !(pr.ops[op].flags & UR_OP_SUPPORTED)) return -EOPNOTSUPP;
449
+ }
450
+ return 1;
451
+ }
452
+
389
453
  static int32_t ur_probe_once(void) {
390
454
  struct ur_params p;
391
455
  int fd = ur_setup_flags(4, 0, &p);
392
456
  if (fd < 0) return fd;
393
457
  int32_t rc = 1;
394
458
  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
- }
459
+ if (rc > 0) rc = ur_ops_ok(fd);
409
460
  if (rc > 0) {
410
461
  struct ur_rsrc_register files;
411
462
  memset(&files, 0, sizeof files);
@@ -435,6 +486,35 @@ static int32_t ur_probe_once(void) {
435
486
  return rc;
436
487
  }
437
488
 
489
+ /* The rings `--sandbox` made before its filter (WP63 S3), which sets take in
490
+ * place of making their own: under that filter `io_uring_setup` and
491
+ * `io_uring_register` kill the process. `made` is set once, before `main`
492
+ * and before any thread, by `nish_uring_premake`; `rc` is the number of rings
493
+ * made or the errno of the step that failed, and `next` the next ring a set
494
+ * takes. `keeper` is one more restricted ring, made after them, which takes
495
+ * only NOP and has no queue mapped: a ring a set frees is dropped by
496
+ * `dup3`ing the keeper over its number, never by closing it, so every number
497
+ * the filter lets `io_uring_enter` take holds a restricted ring for the
498
+ * process's life (NURING-36). -1 when the pool holds no ring. */
499
+ #define UR_POOL_MAX 64
500
+ /* The default sizes, which a pooled ring is made at. */
501
+ #define UR_SQ_DEFAULT 1024
502
+ #define UR_CQ_DEFAULT 8192
503
+ #define UR_BUF_COUNT_DEFAULT 1024
504
+ #define UR_BUF_SIZE_DEFAULT 2048
505
+ struct ur_sizes {
506
+ uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
507
+ };
508
+ static struct {
509
+ int made;
510
+ int32_t rc;
511
+ struct ur_sizes z;
512
+ struct ur_ring *rings[UR_POOL_MAX];
513
+ int32_t fds[UR_POOL_MAX];
514
+ int n, next;
515
+ int keeper;
516
+ } ur_pool = {.keeper = -1};
517
+
438
518
  /* 1 when this process may use a set, else the negative errno that says why
439
519
  * not. A yes or a static refusal is kept for the process: what refuses
440
520
  * io_uring (the kernel, a seccomp filter, the sysctl) does not change under
@@ -442,6 +522,8 @@ static int32_t ur_probe_once(void) {
442
522
  * resource (-EMFILE, -ENFILE, -ENOMEM, ...) is not kept, so the next call
443
523
  * asks again once descriptors or memory are free. */
444
524
  int32_t nish_uring_probe(void) {
525
+ /* A sandboxed process never calls setup: the pool is the answer. */
526
+ if (ur_pool.made) return ur_pool.rc > 0 ? 1 : ur_pool.rc;
445
527
  static int32_t cached;
446
528
  int32_t c = __atomic_load_n(&cached, __ATOMIC_RELAXED);
447
529
  if (c == 0) {
@@ -499,8 +581,11 @@ static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid
499
581
  }
500
582
 
501
583
  /* Arm a slot's multishot recv, or park it for the next wait when the SQ has
502
- * no room. */
584
+ * no room. A slot that has hung up is not armed: a recv on its socket would
585
+ * answer 0 once the kernel has handed over the reset, and a second hangup
586
+ * would turn the reset into a clean end (NURING-27). */
503
587
  static void ur_arm(struct ur_ring *r, uint32_t slot) {
588
+ if (r->st[slot] & ST_HUP) return;
504
589
  struct ur_sqe *e = ur_sqe(r);
505
590
  if (!e) {
506
591
  if (!(r->st[slot] & ST_PARKED)) {
@@ -518,25 +603,80 @@ static void ur_arm(struct ur_ring *r, uint32_t slot) {
518
603
  r->st[slot] |= ST_ARMED;
519
604
  }
520
605
 
521
- static void ur_free_ring(struct nish_uring_set *s, struct ur_ring *r) {
606
+ static void ur_free_ring(struct ur_ring *r) {
522
607
  if (r->efd >= 0) close(r->efd);
523
- if (r->fd >= 0) close(r->fd);
608
+ /* A pooled ring's number stays a restricted ring's: `dup3` drops the ring
609
+ and puts the keeper there in one step, which the filter allows from the
610
+ keeper alone, where it kills `close` on that number (NURING-36). It is
611
+ tried again on EINTR and EBUSY; on any other failure the ring is left at
612
+ its number, still restricted, and its kernel memory is held. */
613
+ if (r->fd >= 0 && ur_pool.keeper >= 0) {
614
+ while (syscall(SYS_dup3, ur_pool.keeper, r->fd, O_CLOEXEC) < 0 && (errno == EINTR || errno == EBUSY)) {
615
+ }
616
+ } else if (r->fd >= 0)
617
+ close(r->fd);
524
618
  if (r->ring_map && r->ring_map != MAP_FAILED) munmap(r->ring_map, r->ring_len);
525
619
  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);
620
+ if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, r->br_len);
621
+ if (r->bufs && (void *)r->bufs != MAP_FAILED) munmap(r->bufs, r->bufs_len);
528
622
  free(r->st);
529
623
  free(r->sq_first);
530
624
  free(r->sq_last);
531
625
  free(r->parked);
626
+ free(r->qbytes);
532
627
  free(r);
533
628
  }
534
629
 
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;
630
+ /* Restrict a ring made with R_DISABLED to what the layer submits (WP63 §6.2):
631
+ * the six opcodes and NOP, the three flags the layer sets, and no register
632
+ * opcode at all once it is enabled. The flags entry allows bits and requires
633
+ * none, so it is the layer, which always sets FIXED_FILE, that confines
634
+ * SHUTDOWN to the ring's own table: an SQE written into the ring by other
635
+ * means could name an ordinary descriptor, as it already could for SEND
636
+ * (NURING-39). */
637
+ static int ur_restrict(int fd) {
638
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV, UR_OP_SEND, UR_OP_CLOSE,
639
+ UR_OP_ASYNC_CANCEL, UR_OP_NOP, UR_OP_SHUTDOWN};
640
+ struct ur_restriction res[sizeof ops + 1];
641
+ memset(res, 0, sizeof res);
642
+ for (size_t i = 0; i < sizeof ops; i++) {
643
+ res[i].opcode = UR_RESTRICTION_SQE_OP;
644
+ res[i].arg = ops[i];
645
+ }
646
+ res[sizeof ops].opcode = UR_RESTRICTION_SQE_FLAGS_ALLOWED;
647
+ res[sizeof ops].arg = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT | UR_SQE_CQE_SKIP_SUCCESS;
648
+ return ur_register(fd, UR_REGISTER_RESTRICTIONS, res, (unsigned)(sizeof res / sizeof res[0]));
649
+ }
650
+
651
+ /* The pool's keeper: a ring of one entry made disabled, restricted to NOP
652
+ * with no submission flag, and enabled, with nothing mapped or registered.
653
+ * Answers its descriptor or the failing step's negative errno. */
654
+ static int ur_make_keeper(void) {
655
+ struct ur_params p;
656
+ int fd = ur_setup_try(1, 0, &p, UR_SETUP_R_DISABLED);
657
+ if (fd < 0) return fd;
658
+ struct ur_restriction res;
659
+ memset(&res, 0, sizeof res);
660
+ res.opcode = UR_RESTRICTION_SQE_OP;
661
+ res.arg = UR_OP_NOP;
662
+ int rc = ur_register(fd, UR_REGISTER_RESTRICTIONS, &res, 1);
663
+ if (rc >= 0) rc = ur_register(fd, UR_REGISTER_ENABLE_RINGS, NULL, 0);
664
+ if (rc < 0) {
665
+ close(fd);
666
+ return rc;
667
+ }
668
+ return fd;
669
+ }
670
+
671
+ /* A new ring of sizes `z`: the SQ and CQ mapped, a sparse table of
672
+ * `z->slots`, the buffer ring registered, the eventfd registered. A
673
+ * `restricted` ring is made disabled, restricted before anything else is
674
+ * registered, and enabled last; it has no SINGLE_ISSUER, which would tie it to
675
+ * the thread that enabled it, the one that runs `.preinit_array`. Answers 0
676
+ * with the ring in `*out`, or the failing step's negative errno with nothing
677
+ * left behind. */
678
+ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring **out) {
679
+ *out = NULL;
540
680
  struct ur_ring *r = calloc(1, sizeof *r);
541
681
  if (!r) return -ENOMEM;
542
682
  r->fd = -1;
@@ -544,7 +684,12 @@ static int ur_add_ring(struct nish_uring_set *s) {
544
684
  int rc = 0;
545
685
  struct ur_params p;
546
686
  /* 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);
687
+ if (restricted)
688
+ r->fd = ur_setup_try(z->sq_entries, z->cq_entries, &p,
689
+ UR_SETUP_R_DISABLED | UR_SETUP_COOP_TASKRUN | UR_SETUP_TASKRUN_FLAG | UR_SETUP_SUBMIT_ALL |
690
+ UR_SETUP_CQSIZE);
691
+ else
692
+ r->fd = ur_setup_flags(z->sq_entries, z->cq_entries, &p);
548
693
  if (r->fd < 0) {
549
694
  rc = r->fd;
550
695
  goto fail;
@@ -553,6 +698,12 @@ static int ur_add_ring(struct nish_uring_set *s) {
553
698
  rc = -EOPNOTSUPP;
554
699
  goto fail;
555
700
  }
701
+ if (restricted) {
702
+ rc = ur_ops_ok(r->fd);
703
+ if (rc < 0) goto fail;
704
+ rc = ur_restrict(r->fd);
705
+ if (rc < 0) goto fail;
706
+ }
556
707
  /* SQ and CQ share one mapping (FEAT_SINGLE_MMAP); the SQEs are another. */
557
708
  r->sq_entries = p.sq_entries;
558
709
  size_t sq_len = p.sq_off.array + (size_t)p.sq_entries * sizeof(uint32_t);
@@ -584,15 +735,16 @@ static int ur_add_ring(struct nish_uring_set *s) {
584
735
  /* The sparse file table: one entry per slot, filled by FILE_INDEX_ALLOC. */
585
736
  struct ur_rsrc_register files;
586
737
  memset(&files, 0, sizeof files);
587
- files.nr = s->slots;
738
+ files.nr = z->slots;
588
739
  files.flags = UR_RSRC_REGISTER_SPARSE;
589
740
  rc = ur_register(r->fd, UR_REGISTER_FILES2, &files, sizeof files);
590
741
  if (rc < 0) goto fail;
591
742
 
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);
743
+ /* The provided-buffer ring every multishot recv draws from; the set fills it. */
744
+ r->br_len = (size_t)z->buf_count * sizeof(struct ur_buf);
745
+ r->bufs_len = (size_t)z->buf_count * z->buf_size;
746
+ r->br = mmap(NULL, r->br_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
747
+ r->bufs = mmap(NULL, r->bufs_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
596
748
  if ((void *)r->br == MAP_FAILED || (void *)r->bufs == MAP_FAILED) {
597
749
  rc = -ENOMEM;
598
750
  goto fail;
@@ -600,24 +752,23 @@ static int ur_add_ring(struct nish_uring_set *s) {
600
752
  struct ur_buf_reg reg;
601
753
  memset(&reg, 0, sizeof reg);
602
754
  reg.ring_addr = (uint64_t)(uintptr_t)r->br;
603
- reg.ring_entries = s->buf_count;
755
+ reg.ring_entries = z->buf_count;
604
756
  reg.bgid = BGID;
605
757
  rc = ur_register(r->fd, UR_REGISTER_PBUF_RING, &reg, 1);
606
758
  if (rc < 0) goto fail;
607
- for (uint32_t i = 0; i < s->buf_count; i++) ur_recycle(s, r, i);
608
759
 
609
760
  /* 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) {
761
+ r->st = calloc(z->slots, sizeof *r->st);
762
+ r->sq_first = calloc(z->slots, sizeof *r->sq_first);
763
+ r->sq_last = calloc(z->slots, sizeof *r->sq_last);
764
+ r->parked = calloc(z->slots, sizeof *r->parked);
765
+ r->qbytes = calloc(z->slots, sizeof *r->qbytes);
766
+ if (!r->st || !r->sq_first || !r->sq_last || !r->parked || !r->qbytes) {
615
767
  rc = -ENOMEM;
616
768
  goto fail;
617
769
  }
618
770
 
619
- /* The eventfd the kernel signals for this ring, in the set's epoll with
620
- * the ring's index as its data. */
771
+ /* The eventfd the kernel signals for this ring. */
621
772
  r->efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
622
773
  if (r->efd < 0) {
623
774
  rc = -errno;
@@ -625,21 +776,150 @@ static int ur_add_ring(struct nish_uring_set *s) {
625
776
  }
626
777
  rc = ur_register(r->fd, UR_REGISTER_EVENTFD, &r->efd, 1);
627
778
  if (rc < 0) goto fail;
779
+ if (restricted) {
780
+ rc = ur_register(r->fd, UR_REGISTER_ENABLE_RINGS, NULL, 0);
781
+ if (rc < 0) goto fail;
782
+ }
783
+ *out = r;
784
+ return 0;
785
+ fail:
786
+ ur_free_ring(r);
787
+ return rc < 0 ? rc : -EINVAL;
788
+ }
789
+
790
+ /* A new ring in the set: made, or taken from the pool when `--sandbox` made
791
+ * one (-ENOSPC when the pool is spent), then its buffer ring filled and its
792
+ * eventfd in the set's epoll with the ring's index as its data. Answers the
793
+ * ring's index or a negative errno, and leaves nothing behind on failure. */
794
+ static int ur_add_ring(struct nish_uring_set *s) {
795
+ if (s->nrings >= s->rings_max) return -ENOSPC;
796
+ struct ur_ring *r;
797
+ if (ur_pool.made) {
798
+ /* A full set asks again on every top-up, so a spent pool answers without
799
+ counting: `next` stays within the threads past `n` and never wraps to
800
+ a negative index (NURING-34). */
801
+ if (__atomic_load_n(&ur_pool.next, __ATOMIC_RELAXED) >= ur_pool.n) return -ENOSPC;
802
+ int i = __atomic_fetch_add(&ur_pool.next, 1, __ATOMIC_RELAXED);
803
+ if (i >= ur_pool.n) return -ENOSPC;
804
+ r = ur_pool.rings[i];
805
+ ur_pool.rings[i] = NULL;
806
+ } else {
807
+ struct ur_sizes z = {s->slots, s->buf_count, s->buf_size, s->sq_entries, s->cq_entries};
808
+ int rc = ur_make_ring(&z, 0, &r);
809
+ if (rc < 0) return rc;
810
+ }
811
+ for (uint32_t i = 0; i < s->buf_count; i++) ur_recycle(s, r, i);
628
812
  struct epoll_event ev;
629
813
  memset(&ev, 0, sizeof ev);
630
814
  ev.events = EPOLLIN;
631
815
  ev.data.u32 = (uint32_t)s->nrings;
632
816
  if (epoll_ctl(s->ep, EPOLL_CTL_ADD, r->efd, &ev) != 0) {
633
- rc = -errno;
634
- goto fail;
817
+ int rc = -errno;
818
+ ur_free_ring(r);
819
+ return rc;
635
820
  }
636
821
  s->rings[s->nrings] = r;
637
822
  s->stats.rings = ++s->nrings;
638
823
  s->stats.slots = (int64_t)s->nrings * s->slots;
639
824
  return s->nrings - 1;
640
- fail:
641
- ur_free_ring(s, r);
642
- return rc < 0 ? rc : -EINVAL;
825
+ }
826
+
827
+ /* Make `n` restricted rings of `slots` slots, at the sizes a set opens with
828
+ * by default, for the sets this process opens later (WP63 S3). `--sandbox`
829
+ * calls it once, before its seccomp filter and before any thread; with `n`
830
+ * of 0 it only records that this process may not make a ring (-EPERM). A
831
+ * kernel below the probe's floor makes none, and a ring that fails any step
832
+ * closes every ring made; either way the pool answers that step's errno from
833
+ * then on: the process falls back to descriptors for good.
834
+ * Answers the number of rings made or that errno, and writes each ring's
835
+ * descriptor to `fds`, which holds 65: the only descriptors the filter lets
836
+ * `io_uring_enter` take, then the keeper's after them, which the filter
837
+ * keeps every one of those numbers on (NURING-36). */
838
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds) {
839
+ if (ur_pool.made) return ur_pool.rc;
840
+ ur_pool.made = 1;
841
+ if (n <= 0) return ur_pool.rc = -EPERM;
842
+ /* A restricted ring drops SINGLE_ISSUER and DEFER_TASKRUN, so it alone
843
+ would take a 5.19 or 6.0 kernel; the probe holds the pool to the floor
844
+ an unsandboxed set has, 6.1 (NURING-33). */
845
+ int32_t probed = ur_probe_once();
846
+ if (probed < 0) return ur_pool.rc = probed;
847
+ if (n > UR_POOL_MAX) n = UR_POOL_MAX;
848
+ if (slots < 1) slots = 1;
849
+ if (slots > UR_MAX_SLOTS) slots = UR_MAX_SLOTS;
850
+ ur_pool.z = (struct ur_sizes){(uint32_t)slots, UR_BUF_COUNT_DEFAULT, UR_BUF_SIZE_DEFAULT, UR_SQ_DEFAULT, UR_CQ_DEFAULT};
851
+ for (int i = 0; i < n; i++) {
852
+ int rc = ur_make_ring(&ur_pool.z, 1, &ur_pool.rings[i]);
853
+ if (rc < 0) {
854
+ for (int j = 0; j < i; j++) ur_free_ring(ur_pool.rings[j]);
855
+ memset(ur_pool.rings, 0, sizeof ur_pool.rings);
856
+ return ur_pool.rc = rc;
857
+ }
858
+ }
859
+ int keeper = ur_make_keeper();
860
+ if (keeper < 0) {
861
+ for (int j = 0; j < n; j++) ur_free_ring(ur_pool.rings[j]);
862
+ memset(ur_pool.rings, 0, sizeof ur_pool.rings);
863
+ return ur_pool.rc = keeper;
864
+ }
865
+ for (int i = 0; i < n; i++) fds[i] = ur_pool.fds[i] = ur_pool.rings[i]->fd;
866
+ fds[n] = ur_pool.keeper = keeper;
867
+ ur_pool.n = n;
868
+ return ur_pool.rc = n;
869
+ }
870
+
871
+ /* The slots of each ring `--sandbox` made, 0 when it made none, or -1 when
872
+ * it ran and made none: a set must open with the pool's sizes. */
873
+ int32_t nish_uring_pooled(void) {
874
+ if (!ur_pool.made) return 0;
875
+ return ur_pool.rc > 0 ? (int32_t)ur_pool.z.slots : -1;
876
+ }
877
+
878
+ #ifdef NISH_URING_TEST_HOOKS
879
+ /* Test-only: tests/run.js's sandbox section builds the runtime with
880
+ * -DNISH_URING_TEST_HOOKS to ask the kernel what a premade ring takes, and
881
+ * scripts/build.sh never passes it. Submits one entry of opcode `op` with
882
+ * submission flags `flags`, on descriptor `fd`, to premade ring `i` before a
883
+ * listener has taken it, and answers its completion's res: -EACCES is the
884
+ * ring's restriction. -ENOENT when no such ring was made or it was taken. */
885
+ int32_t nish_uring_test_submit_fd(int32_t i, int32_t op, int32_t flags, int32_t fd);
886
+ int32_t nish_uring_test_submit_fd(int32_t i, int32_t op, int32_t flags, int32_t fd) {
887
+ if (!ur_pool.made || ur_pool.rc <= 0 || i < 0 || i >= ur_pool.n || ur_pool.rings[i] == NULL) return -ENOENT;
888
+ struct ur_ring *r = ur_pool.rings[i];
889
+ struct ur_sqe *e = ur_sqe(r);
890
+ if (e == NULL) return -EBUSY;
891
+ e->opcode = (uint8_t)op;
892
+ e->flags = (uint8_t)flags;
893
+ e->fd = fd;
894
+ e->user_data = 1;
895
+ int rc;
896
+ do rc = ur_enter(r, r->to_submit, 1, UR_ENTER_GETEVENTS);
897
+ while (rc == -EINTR);
898
+ if (rc < 0) return rc;
899
+ r->to_submit = 0;
900
+ uint32_t head = *r->cq_head;
901
+ if (head == __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) return -EAGAIN;
902
+ int32_t res = r->cqes[head & *r->cq_mask].res;
903
+ __atomic_store_n(r->cq_head, head + 1, __ATOMIC_RELEASE);
904
+ return res;
905
+ }
906
+ /* As `nish_uring_test_submit_fd`, on no file. */
907
+ int32_t nish_uring_test_submit(int32_t i, int32_t op, int32_t flags);
908
+ int32_t nish_uring_test_submit(int32_t i, int32_t op, int32_t flags) { return nish_uring_test_submit_fd(i, op, flags, -1); }
909
+ /* The number premade ring `i` was made at, whether or not a set has taken
910
+ * it since, or the keeper's for `i` equal to the ring count; -ENOENT for
911
+ * any other `i`. */
912
+ int32_t nish_uring_test_fd(int32_t i);
913
+ int32_t nish_uring_test_fd(int32_t i) {
914
+ if (!ur_pool.made || ur_pool.rc <= 0 || i < 0 || i > ur_pool.n) return -ENOENT;
915
+ return i == ur_pool.n ? ur_pool.keeper : ur_pool.fds[i];
916
+ }
917
+ #endif
918
+
919
+ static int64_t ur_now_ms(void) {
920
+ struct timespec t;
921
+ clock_gettime(CLOCK_MONOTONIC, &t);
922
+ return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000;
643
923
  }
644
924
 
645
925
  /* Keep min(ACCEPT_DEPTH, free slots) accepts in flight on the accepting ring,
@@ -649,6 +929,10 @@ fail:
649
929
  * connections wait in the listener's backlog until a close frees a slot. */
650
930
  static void ur_top_up(struct nish_uring_set *s) {
651
931
  if (s->accept_stopped || s->nrings == 0) return;
932
+ if (s->accept_paused) {
933
+ if (ur_now_ms() < s->resume_ms) return;
934
+ s->accept_paused = 0;
935
+ }
652
936
  struct ur_ring *r = s->rings[s->acceptor];
653
937
  if (r->live + r->accepting >= (long)s->slots && r->accepting == 0) {
654
938
  int moved = 0;
@@ -685,7 +969,8 @@ static void ur_top_up(struct nish_uring_set *s) {
685
969
  if (rings > s->stats.max_accepting_rings) s->stats.max_accepting_rings = rings;
686
970
  }
687
971
 
688
- /* Submit the slot's first queued send, from where the kernel left off. */
972
+ /* Submit the slot's first queued send, from where the kernel left off, or
973
+ * its shutdown. */
689
974
  static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
690
975
  struct ur_send *q = r->sq_first[slot];
691
976
  if (!q || q->in_flight) return;
@@ -694,12 +979,17 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
694
979
  s->stalled_sends++;
695
980
  return;
696
981
  }
697
- e->opcode = UR_OP_SEND;
698
982
  e->fd = (int32_t)slot;
699
983
  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;
984
+ if (q->how >= 0) {
985
+ e->opcode = UR_OP_SHUTDOWN;
986
+ e->len = (uint32_t)q->how;
987
+ } else {
988
+ e->opcode = UR_OP_SEND;
989
+ e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
990
+ e->len = q->len - q->off;
991
+ e->op_flags = MSG_NOSIGNAL;
992
+ }
703
993
  e->user_data = (uint64_t)(uintptr_t)q | UR_SEND;
704
994
  q->in_flight = 1;
705
995
  }
@@ -717,6 +1007,7 @@ static void ur_unlink_all(struct nish_uring_set *s, struct ur_send *q) {
717
1007
  static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot) {
718
1008
  struct ur_send *q = r->sq_first[slot];
719
1009
  r->sq_first[slot] = r->sq_last[slot] = NULL;
1010
+ r->qbytes[slot] = 0;
720
1011
  while (q) {
721
1012
  struct ur_send *next = q->next;
722
1013
  q->next = NULL;
@@ -725,10 +1016,16 @@ static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t
725
1016
  }
726
1017
  }
727
1018
 
1019
+ /* An accept that failed for this one peer; the next is submitted at once. */
728
1020
  static int ur_transient_accept(int res) {
729
1021
  return res == -EAGAIN || res == -EINTR || res == -ECONNABORTED || res == -EPROTO;
730
1022
  }
731
1023
 
1024
+ /* An accept that found the kernel short of memory for a socket (NURING-28).
1025
+ * It clears as memory frees, so accepting pauses and resumes; submitting
1026
+ * again at once would spin on the same answer while the shortage lasts. */
1027
+ static int ur_pressure_accept(int res) { return res == -ENOMEM || res == -ENOBUFS; }
1028
+
732
1029
  static void ur_event(struct nish_uring_event *ev, int kind, int ring, uint32_t slot, int res, const uint8_t *data) {
733
1030
  ev->kind = kind;
734
1031
  ev->ring = ring;
@@ -750,6 +1047,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
750
1047
  ur_unlink_all(s, q);
751
1048
  return 0;
752
1049
  }
1050
+ if (q->how >= 0) {
1051
+ /* A shutdown's answer is not a send's failure: -ENOTCONN from a peer
1052
+ * already gone says nothing a recv will not, so it is not reported. */
1053
+ res = 0;
1054
+ }
1055
+ if (res > 0) r->qbytes[slot] -= (uint32_t)res < r->qbytes[slot] ? (uint32_t)res : r->qbytes[slot];
753
1056
  if (res > 0 && q->off + (uint32_t)res < q->len) {
754
1057
  q->off += (uint32_t)res;
755
1058
  s->stats.send_requeues++;
@@ -773,10 +1076,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
773
1076
  uint32_t slot = (uint32_t)(ud >> 32);
774
1077
  if (op == UR_ACCEPT) {
775
1078
  r->accepting--;
1079
+ res = UR_ACCEPT_RES(res);
776
1080
  int wrote = 0;
777
1081
  if (res >= 0 && (uint32_t)res < s->slots) {
778
1082
  uint32_t at = (uint32_t)res;
779
- r->st[at] = (r->st[at] & ~(ST_ARMED | ST_PARKED)) | ST_OPEN;
1083
+ s->backoff_ms = 0;
1084
+ r->st[at] = (r->st[at] & ~(ST_ARMED | ST_PARKED | ST_HELD | ST_HUP)) | ST_OPEN;
780
1085
  r->live++;
781
1086
  s->stats.live++;
782
1087
  ur_arm(r, at);
@@ -786,6 +1091,17 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
786
1091
  /* A slot past the table the set registered; the kernel never answers
787
1092
  * one, so it is counted rather than trusted. */
788
1093
  s->stats.bound_violations++;
1094
+ } else if (ur_pressure_accept(res)) {
1095
+ /* Accepts already in flight may fail the same way; one pause covers them. */
1096
+ if (!s->accept_paused) {
1097
+ s->accept_paused = 1;
1098
+ s->backoff_ms = s->backoff_ms ? s->backoff_ms * 2 : ACCEPT_BACKOFF_MIN_MS;
1099
+ if (s->backoff_ms > ACCEPT_BACKOFF_MAX_MS) s->backoff_ms = ACCEPT_BACKOFF_MAX_MS;
1100
+ s->resume_ms = ur_now_ms() + s->backoff_ms;
1101
+ s->stats.accept_pauses++;
1102
+ ur_event(ev, NISH_URING_ACCEPT_PAUSED, ri, 0, res, NULL);
1103
+ wrote = 1;
1104
+ }
789
1105
  } else if (!ur_transient_accept(res)) {
790
1106
  s->stats.accept_errors++;
791
1107
  if (res == -ENFILE) s->stats.bound_violations++;
@@ -812,11 +1128,19 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
812
1128
  s->lent_ring[s->nlent] = (uint32_t)ri;
813
1129
  s->lent_bid[s->nlent] = (uint16_t)bid;
814
1130
  s->nlent++;
815
- if (!more) ur_arm(r, slot);
1131
+ if (!more && !(r->st[slot] & ST_HELD)) ur_arm(r, slot);
816
1132
  ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
817
1133
  return 1;
818
1134
  }
819
1135
  if (has_buf) ur_recycle(s, r, bid);
1136
+ /* Only a hold cancels a live slot's recv (a close moves the generation on
1137
+ * first). The recv stays down while the slot is held, and one let go before
1138
+ * the cancel landed is armed again; a held recv out of buffers stays down. */
1139
+ if (!more && res == -ECANCELED) {
1140
+ if (!(r->st[slot] & ST_HELD)) ur_arm(r, slot);
1141
+ return 0;
1142
+ }
1143
+ if (!more && res == -ENOBUFS && (r->st[slot] & ST_HELD)) return 0;
820
1144
  if (res == -ENOBUFS) {
821
1145
  s->stats.nobufs_rearms++;
822
1146
  if (!more && !(r->st[slot] & ST_PARKED)) {
@@ -826,6 +1150,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
826
1150
  return 0;
827
1151
  }
828
1152
  if (more) return 0;
1153
+ r->st[slot] |= ST_HUP;
829
1154
  ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
830
1155
  return 1;
831
1156
  }
@@ -895,13 +1220,21 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
895
1220
  }
896
1221
  if (d.slots > UR_MAX_SLOTS) return -EINVAL;
897
1222
  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;
1223
+ if (d.sq_entries == 0) d.sq_entries = UR_SQ_DEFAULT;
1224
+ if (d.cq_entries == 0) d.cq_entries = UR_CQ_DEFAULT;
900
1225
  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;
1226
+ if (d.buf_count == 0) d.buf_count = UR_BUF_COUNT_DEFAULT;
1227
+ if (d.buf_size == 0) d.buf_size = UR_BUF_SIZE_DEFAULT;
903
1228
  if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
904
1229
  return -EINVAL;
1230
+ /* A pooled ring was made at its own sizes, before the program could ask. */
1231
+ if (ur_pool.made) {
1232
+ d.slots = (int32_t)ur_pool.z.slots;
1233
+ d.sq_entries = (int32_t)ur_pool.z.sq_entries;
1234
+ d.cq_entries = (int32_t)ur_pool.z.cq_entries;
1235
+ d.buf_count = (int32_t)ur_pool.z.buf_count;
1236
+ d.buf_size = (int32_t)ur_pool.z.buf_size;
1237
+ }
905
1238
  struct nish_uring_set *s = calloc(1, sizeof *s);
906
1239
  if (!s) return -ENOMEM;
907
1240
  s->listener = listener;
@@ -983,12 +1316,19 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms)
983
1316
  if (r->nparked) parked = 1;
984
1317
  }
985
1318
  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;
1319
+ int32_t bound = UR_SLEEP_MAX_MS;
1320
+ if (s->accept_paused) {
1321
+ int64_t left = s->resume_ms - ur_now_ms();
1322
+ if (left <= 0) return 0;
1323
+ if (left < bound) bound = (int32_t)left;
1324
+ }
1325
+ return timeout_ms < 0 || timeout_ms > bound ? bound : timeout_ms;
987
1326
  }
988
1327
 
989
1328
  /* What the last wait left to do: give back the buffers it lent, re-arm the
990
1329
  * recvs it parked, and submit the sends that found the SQ full. */
991
1330
  static void ur_catch_up(struct nish_uring_set *s) {
1331
+ if (s->accept_paused) ur_top_up(s); /* accepting resumes once the back-off ends */
992
1332
  for (int i = 0; i < s->nlent; i++) ur_recycle(s, s->rings[s->lent_ring[i]], s->lent_bid[i]);
993
1333
  s->nlent = 0;
994
1334
  for (int ri = 0; ri < s->nrings; ri++) {
@@ -998,7 +1338,7 @@ static void ur_catch_up(struct nish_uring_set *s) {
998
1338
  for (uint32_t i = 0; i < n; i++) {
999
1339
  uint32_t slot = r->parked[i];
1000
1340
  r->st[slot] &= ~ST_PARKED;
1001
- if ((r->st[slot] & (ST_OPEN | ST_ARMED)) == ST_OPEN) ur_arm(r, slot);
1341
+ if ((r->st[slot] & (ST_OPEN | ST_ARMED | ST_HELD)) == ST_OPEN) ur_arm(r, slot);
1002
1342
  }
1003
1343
  }
1004
1344
  /* Scanning every slot of every ring costs rings * slots, but only after a
@@ -1067,6 +1407,23 @@ static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int3
1067
1407
  return (r->st[slot] & ST_OPEN) ? r : NULL;
1068
1408
  }
1069
1409
 
1410
+ /* Put `q` on the set's list of records and behind the slot's queue, and
1411
+ * submit it if it is now the head. */
1412
+ static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot, struct ur_send *q) {
1413
+ q->next = NULL;
1414
+ q->prev_all = NULL;
1415
+ q->next_all = s->all;
1416
+ if (s->all) s->all->prev_all = q;
1417
+ s->all = q;
1418
+ if (r->sq_last[slot]) {
1419
+ r->sq_last[slot]->next = q;
1420
+ } else {
1421
+ r->sq_first[slot] = q;
1422
+ }
1423
+ r->sq_last[slot] = q;
1424
+ ur_send_head(r, slot, s);
1425
+ }
1426
+
1070
1427
  /* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
1071
1428
  * is free on return; a failure comes back later as a SEND_FAILED event. */
1072
1429
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
@@ -1077,33 +1434,49 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
1077
1434
  struct ur_send *q = malloc(sizeof *q + (size_t)n);
1078
1435
  if (!q) return -ENOMEM;
1079
1436
  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
1437
  q->slot = (uint32_t)slot;
1086
1438
  q->gen = ST_GEN(r->st[slot]);
1087
1439
  q->off = 0;
1088
1440
  q->len = (uint32_t)n;
1089
1441
  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);
1442
+ q->how = -1;
1443
+ r->qbytes[slot] += (uint32_t)n;
1444
+ ur_enqueue(s, r, (uint32_t)slot, q);
1097
1445
  return n;
1098
1446
  }
1099
1447
 
1448
+ /* Queue a shutdown of (ring, slot)'s `how` side (0 read, 1 write, 2 both)
1449
+ * behind the bytes already queued for it, so they reach the peer first. Its
1450
+ * answer is not reported: a shutdown that fails, as on a peer already gone,
1451
+ * leaves a connection whose next read or write says so. */
1452
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
1453
+ struct ur_ring *r = ur_open_slot(s, ring, slot);
1454
+ if (!r) return -EBADF;
1455
+ if ((uint32_t)how > 2) return -EINVAL;
1456
+ struct ur_send *q = malloc(sizeof *q);
1457
+ if (!q) return -ENOMEM;
1458
+ q->slot = (uint32_t)slot;
1459
+ q->gen = ST_GEN(r->st[slot]);
1460
+ q->off = q->len = 0;
1461
+ q->in_flight = 0;
1462
+ q->how = how;
1463
+ ur_enqueue(s, r, (uint32_t)slot, q);
1464
+ return 0;
1465
+ }
1466
+
1100
1467
  /* Cancel the slot's recv, close the slot, and move its generation on, so
1101
1468
  * 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. */
1469
+ * the accepting job. -EAGAIN, with nothing changed, when the SQ has no room
1470
+ * for both entries or while the slot's shutdown is in flight: a SHUTDOWN runs
1471
+ * on io-wq, which looks the slot's file up only when it runs, and the cancel
1472
+ * below cannot match a request that has no file yet, so a slot freed before
1473
+ * it ran could take a new connection that the shutdown then reaches
1474
+ * (NURING-39). */
1104
1475
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1105
1476
  struct ur_ring *r = ur_open_slot(s, ring, slot);
1106
1477
  if (!r) return -EBADF;
1478
+ const struct ur_send *h = r->sq_first[slot];
1479
+ if (h && h->in_flight && h->how >= 0) return -EAGAIN;
1107
1480
  uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
1108
1481
  if (r->sq_local_tail - head + 2 > r->sq_entries) {
1109
1482
  (void)ur_flush(r);
@@ -1130,13 +1503,65 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1130
1503
  return 0;
1131
1504
  }
1132
1505
 
1506
+ /* The bytes queued for (ring, slot) that the kernel has not yet sent, or -1
1507
+ * for a slot that is not open. runtime-net.c bounds a connection's send area
1508
+ * with it (NURING-2): a successful send makes no event, so the caller asks. */
1509
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
1510
+ if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return -1;
1511
+ const struct ur_ring *r = s->rings[ring];
1512
+ return (r->st[slot] & ST_OPEN) ? (int64_t)r->qbytes[slot] : -1;
1513
+ }
1514
+
1515
+ /* 1 when no accept is in flight and none can be: every ring's table is full
1516
+ * and the set has `rings_max` rings or could not make another. A connection
1517
+ * then waits in the listener's backlog, and tcpAccept answers -23
1518
+ * (NURING-6). A close frees a slot and starts accepting again. */
1519
+ int32_t nish_uring_full(const struct nish_uring_set *s) {
1520
+ if (s->accept_stopped || s->accept_paused) return 0;
1521
+ for (int i = 0; i < s->nrings; i++) {
1522
+ const struct ur_ring *r = s->rings[i];
1523
+ if (r->accepting > 0 || r->live < (long)s->slots) return 0;
1524
+ }
1525
+ return 1;
1526
+ }
1527
+
1528
+ /* Hold (`on` 1) or let go (`on` 0) a slot's receive. A hold cancels the
1529
+ * slot's multishot recv by its tag, not by its descriptor, so the slot's sends
1530
+ * in flight go on, and keeps it from being armed again; the bytes the kernel
1531
+ * received before the cancel takes effect are still reported. runtime-net.c
1532
+ * holds a slot whose receive queue reaches its bound, so a peer the program
1533
+ * does not read cannot grow it (NURING-9). Letting go arms the recv again.
1534
+ * -EBADF for a slot that is not open; -EAGAIN, nothing changed, when the SQ
1535
+ * has no room for the cancel. */
1536
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
1537
+ struct ur_ring *r = ur_open_slot(s, ring, slot);
1538
+ if (!r) return -EBADF;
1539
+ uint32_t st = r->st[slot];
1540
+ if (!on) {
1541
+ r->st[slot] = st & ~ST_HELD;
1542
+ if ((st & (ST_ARMED | ST_PARKED)) == 0) ur_arm(r, (uint32_t)slot);
1543
+ return 0;
1544
+ }
1545
+ if (st & ST_HELD) return 0;
1546
+ if (st & ST_ARMED) {
1547
+ struct ur_sqe *e = ur_sqe(r);
1548
+ if (!e) return -EAGAIN;
1549
+ e->opcode = UR_OP_ASYNC_CANCEL;
1550
+ e->addr = ur_tag(UR_RECV, st, (uint32_t)slot);
1551
+ e->flags = UR_SQE_CQE_SKIP_SUCCESS;
1552
+ e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
1553
+ }
1554
+ r->st[slot] = st | ST_HELD;
1555
+ return 0;
1556
+ }
1557
+
1133
1558
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) { *st = s->stats; }
1134
1559
 
1135
1560
  /* Close every ring, which ends whatever the kernel still held, and free the
1136
1561
  * sends it held them for. The caller's listener stays open: it is theirs. */
1137
1562
  void nish_uring_destroy(struct nish_uring_set *s) {
1138
1563
  if (!s) return;
1139
- for (int i = 0; i < s->nrings; i++) ur_free_ring(s, s->rings[i]);
1564
+ for (int i = 0; i < s->nrings; i++) ur_free_ring(s->rings[i]);
1140
1565
  while (s->all) {
1141
1566
  struct ur_send *q = s->all;
1142
1567
  s->all = q->next_all;
@@ -1150,6 +1575,13 @@ void nish_uring_destroy(struct nish_uring_set *s) {
1150
1575
  #else /* not Linux: no io_uring, so a set is never available */
1151
1576
 
1152
1577
  int32_t nish_uring_probe(void) { return -38; }
1578
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds) {
1579
+ (void)n;
1580
+ (void)slots;
1581
+ (void)fds;
1582
+ return -38;
1583
+ }
1584
+ int32_t nish_uring_pooled(void) { return 0; }
1153
1585
  int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts) {
1154
1586
  (void)listener;
1155
1587
  (void)opts;
@@ -1185,6 +1617,30 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1185
1617
  (void)slot;
1186
1618
  return -38;
1187
1619
  }
1620
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
1621
+ (void)s;
1622
+ (void)ring;
1623
+ (void)slot;
1624
+ (void)how;
1625
+ return -38;
1626
+ }
1627
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
1628
+ (void)s;
1629
+ (void)ring;
1630
+ (void)slot;
1631
+ return -1;
1632
+ }
1633
+ int32_t nish_uring_full(const struct nish_uring_set *s) {
1634
+ (void)s;
1635
+ return 0;
1636
+ }
1637
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
1638
+ (void)s;
1639
+ (void)ring;
1640
+ (void)slot;
1641
+ (void)on;
1642
+ return -38;
1643
+ }
1188
1644
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
1189
1645
  (void)s;
1190
1646
  memset(st, 0, sizeof *st);