@amritk/nish-x86_64-linux 0.41.0 → 0.42.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.
@@ -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,6 +198,7 @@ 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
@@ -200,7 +211,11 @@ void nish_uring_destroy(struct nish_uring_set *s);
200
211
  #define UR_REGISTER_EVENTFD 4
201
212
  #define UR_REGISTER_PROBE 8
202
213
  #define UR_REGISTER_FILES2 13
214
+ #define UR_REGISTER_RESTRICTIONS 11
215
+ #define UR_REGISTER_ENABLE_RINGS 12
203
216
  #define UR_REGISTER_PBUF_RING 22
217
+ #define UR_RESTRICTION_SQE_OP 1
218
+ #define UR_RESTRICTION_SQE_FLAGS_ALLOWED 2
204
219
  #define UR_RSRC_REGISTER_SPARSE (1u << 0)
205
220
  #define UR_FILE_INDEX_ALLOC (~0u)
206
221
  #define UR_OP_SUPPORTED (1u << 0)
@@ -271,6 +286,14 @@ struct ur_probe {
271
286
  uint32_t resv2[3];
272
287
  struct ur_probe_op ops[64];
273
288
  };
289
+ /* One entry of IORING_REGISTER_RESTRICTIONS: `arg` is the union of
290
+ * register_op, sqe_op and sqe_flags. */
291
+ struct ur_restriction {
292
+ uint16_t opcode;
293
+ uint8_t arg;
294
+ uint8_t resv;
295
+ uint32_t resv2[3];
296
+ };
274
297
 
275
298
  _Static_assert(sizeof(struct ur_params) == 120, "io_uring_params is 120 bytes");
276
299
  _Static_assert(sizeof(struct ur_sqe) == 64, "io_uring_sqe is 64 bytes");
@@ -278,22 +301,32 @@ _Static_assert(sizeof(struct ur_cqe) == 16, "io_uring_cqe is 16 bytes");
278
301
  _Static_assert(sizeof(struct ur_rsrc_register) == 32, "io_uring_rsrc_register is 32 bytes");
279
302
  _Static_assert(sizeof(struct ur_buf) == 16, "io_uring_buf is 16 bytes");
280
303
  _Static_assert(sizeof(struct ur_buf_reg) == 40, "io_uring_buf_reg is 40 bytes");
304
+ _Static_assert(sizeof(struct ur_restriction) == 16, "io_uring_restriction is 16 bytes");
281
305
 
282
306
  /* What a completion's user_data says. A send's is its record's address, which
283
307
  * malloc aligns to 8, with UR_SEND in the low three bits; every other op's is
284
308
  * the op, the slot's generation and the slot, and none of those ops is 3. */
285
309
  enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
286
310
 
287
- /* A slot's state word: open, recv armed, recv parked on -ENOBUFS, and the
288
- * generation in the 24 bits above. */
311
+ /* A slot's state word: open, recv armed, recv parked on -ENOBUFS, recv held
312
+ * by its caller (`nish_uring_hold`), hung up (its recv ended for good, so it
313
+ * is never armed again until the slot is accepted anew), and the generation
314
+ * in the 24 bits above. */
289
315
  #define ST_OPEN 1u
290
316
  #define ST_ARMED 2u
291
317
  #define ST_PARKED 4u
318
+ #define ST_HELD 8u
319
+ #define ST_HUP 16u
292
320
  #define ST_GEN(st) ((st) >> 8)
293
321
  #define GEN_MASK 0xffffffu
294
322
 
295
323
  #define BGID 1
296
324
  #define ACCEPT_DEPTH 32
325
+ /* The back-off after an accept that found memory short: 10 ms, doubling to
326
+ * 640 ms while accepts keep failing. Connections wait in the listener's
327
+ * backlog meanwhile, as they do at a full set. */
328
+ #define ACCEPT_BACKOFF_MIN_MS 10
329
+ #define ACCEPT_BACKOFF_MAX_MS 640
297
330
  #define EVENTS_MAX 256
298
331
 
299
332
  /* One send's bytes, owned by the set until the kernel has taken all of them.
@@ -319,11 +352,13 @@ struct ur_ring {
319
352
  uint32_t sq_entries, sq_local_tail, to_submit;
320
353
  struct ur_buf *br;
321
354
  uint8_t *bufs;
355
+ size_t br_len, bufs_len;
322
356
  uint16_t br_tail;
323
357
  long live, accepting;
324
358
  uint32_t *st; /* per slot */
325
359
  struct ur_send **sq_first; /* per slot: the head of its send queue */
326
360
  struct ur_send **sq_last; /* per slot: the tail */
361
+ uint32_t *qbytes; /* per slot: bytes queued and not yet sent */
327
362
  uint32_t *parked; /* slots whose recv waits for buffers */
328
363
  uint32_t nparked;
329
364
  };
@@ -333,6 +368,12 @@ struct nish_uring_set {
333
368
  uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
334
369
  int nrings, rings_max, acceptor;
335
370
  int accept_stopped;
371
+ /* Accepting waits until `resume_ms` after an accept found memory short; the
372
+ * wait doubles from ACCEPT_BACKOFF_MIN_MS while it keeps failing and starts
373
+ * again from the minimum once an accept succeeds. */
374
+ int accept_paused;
375
+ int32_t backoff_ms;
376
+ int64_t resume_ms;
336
377
  struct ur_ring **rings;
337
378
  struct ur_send *all;
338
379
  long stalled_sends; /* heads that found the SQ full */
@@ -354,6 +395,11 @@ static int ur_errno(long rc) { return rc < 0 ? -errno : (int)rc; }
354
395
  #ifndef UR_SYS_REGISTER
355
396
  #define UR_SYS_REGISTER(fd, op, arg, nr) syscall(__NR_io_uring_register, fd, op, arg, nr)
356
397
  #endif
398
+ /* What an accept completion answered; uring-test.c turns one into -ENOMEM,
399
+ * which the kernel gives only when memory is short. */
400
+ #ifndef UR_ACCEPT_RES
401
+ #define UR_ACCEPT_RES(res) (res)
402
+ #endif
357
403
 
358
404
  static int ur_setup(uint32_t entries, struct ur_params *p) { return ur_errno(UR_SYS_SETUP(entries, p)); }
359
405
 
@@ -386,26 +432,28 @@ static int ur_setup_flags(uint32_t entries, uint32_t cq, struct ur_params *p) {
386
432
  return fd;
387
433
  }
388
434
 
435
+ /* Whether the ring `fd` has every opcode the layer submits: 1, -EOPNOTSUPP
436
+ * when one is missing or the kernel has no probe, or the register's errno. */
437
+ static int32_t ur_ops_ok(int fd) {
438
+ struct ur_probe pr;
439
+ memset(&pr, 0, sizeof pr);
440
+ int e = ur_register(fd, UR_REGISTER_PROBE, &pr, 64);
441
+ if (e < 0) return e == -EINVAL ? -EOPNOTSUPP : e;
442
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV};
443
+ for (size_t i = 0; i < sizeof ops; i++) {
444
+ uint8_t op = ops[i];
445
+ if (op > pr.last_op || op >= pr.ops_len || !(pr.ops[op].flags & UR_OP_SUPPORTED)) return -EOPNOTSUPP;
446
+ }
447
+ return 1;
448
+ }
449
+
389
450
  static int32_t ur_probe_once(void) {
390
451
  struct ur_params p;
391
452
  int fd = ur_setup_flags(4, 0, &p);
392
453
  if (fd < 0) return fd;
393
454
  int32_t rc = 1;
394
455
  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
- }
456
+ if (rc > 0) rc = ur_ops_ok(fd);
409
457
  if (rc > 0) {
410
458
  struct ur_rsrc_register files;
411
459
  memset(&files, 0, sizeof files);
@@ -435,6 +483,35 @@ static int32_t ur_probe_once(void) {
435
483
  return rc;
436
484
  }
437
485
 
486
+ /* The rings `--sandbox` made before its filter (WP63 S3), which sets take in
487
+ * place of making their own: under that filter `io_uring_setup` and
488
+ * `io_uring_register` kill the process. `made` is set once, before `main`
489
+ * and before any thread, by `nish_uring_premake`; `rc` is the number of rings
490
+ * made or the errno of the step that failed, and `next` the next ring a set
491
+ * takes. `keeper` is one more restricted ring, made after them, which takes
492
+ * only NOP and has no queue mapped: a ring a set frees is dropped by
493
+ * `dup3`ing the keeper over its number, never by closing it, so every number
494
+ * the filter lets `io_uring_enter` take holds a restricted ring for the
495
+ * process's life (NURING-36). -1 when the pool holds no ring. */
496
+ #define UR_POOL_MAX 64
497
+ /* The default sizes, which a pooled ring is made at. */
498
+ #define UR_SQ_DEFAULT 1024
499
+ #define UR_CQ_DEFAULT 8192
500
+ #define UR_BUF_COUNT_DEFAULT 1024
501
+ #define UR_BUF_SIZE_DEFAULT 2048
502
+ struct ur_sizes {
503
+ uint32_t slots, buf_count, buf_size, sq_entries, cq_entries;
504
+ };
505
+ static struct {
506
+ int made;
507
+ int32_t rc;
508
+ struct ur_sizes z;
509
+ struct ur_ring *rings[UR_POOL_MAX];
510
+ int32_t fds[UR_POOL_MAX];
511
+ int n, next;
512
+ int keeper;
513
+ } ur_pool = {.keeper = -1};
514
+
438
515
  /* 1 when this process may use a set, else the negative errno that says why
439
516
  * not. A yes or a static refusal is kept for the process: what refuses
440
517
  * io_uring (the kernel, a seccomp filter, the sysctl) does not change under
@@ -442,6 +519,8 @@ static int32_t ur_probe_once(void) {
442
519
  * resource (-EMFILE, -ENFILE, -ENOMEM, ...) is not kept, so the next call
443
520
  * asks again once descriptors or memory are free. */
444
521
  int32_t nish_uring_probe(void) {
522
+ /* A sandboxed process never calls setup: the pool is the answer. */
523
+ if (ur_pool.made) return ur_pool.rc > 0 ? 1 : ur_pool.rc;
445
524
  static int32_t cached;
446
525
  int32_t c = __atomic_load_n(&cached, __ATOMIC_RELAXED);
447
526
  if (c == 0) {
@@ -499,8 +578,11 @@ static void ur_recycle(struct nish_uring_set *s, struct ur_ring *r, uint32_t bid
499
578
  }
500
579
 
501
580
  /* Arm a slot's multishot recv, or park it for the next wait when the SQ has
502
- * no room. */
581
+ * no room. A slot that has hung up is not armed: a recv on its socket would
582
+ * answer 0 once the kernel has handed over the reset, and a second hangup
583
+ * would turn the reset into a clean end (NURING-27). */
503
584
  static void ur_arm(struct ur_ring *r, uint32_t slot) {
585
+ if (r->st[slot] & ST_HUP) return;
504
586
  struct ur_sqe *e = ur_sqe(r);
505
587
  if (!e) {
506
588
  if (!(r->st[slot] & ST_PARKED)) {
@@ -518,25 +600,75 @@ static void ur_arm(struct ur_ring *r, uint32_t slot) {
518
600
  r->st[slot] |= ST_ARMED;
519
601
  }
520
602
 
521
- static void ur_free_ring(struct nish_uring_set *s, struct ur_ring *r) {
603
+ static void ur_free_ring(struct ur_ring *r) {
522
604
  if (r->efd >= 0) close(r->efd);
523
- if (r->fd >= 0) close(r->fd);
605
+ /* A pooled ring's number stays a restricted ring's: `dup3` drops the ring
606
+ and puts the keeper there in one step, which the filter allows from the
607
+ keeper alone, where it kills `close` on that number (NURING-36). It is
608
+ tried again on EINTR and EBUSY; on any other failure the ring is left at
609
+ its number, still restricted, and its kernel memory is held. */
610
+ if (r->fd >= 0 && ur_pool.keeper >= 0) {
611
+ while (syscall(SYS_dup3, ur_pool.keeper, r->fd, O_CLOEXEC) < 0 && (errno == EINTR || errno == EBUSY)) {
612
+ }
613
+ } else if (r->fd >= 0)
614
+ close(r->fd);
524
615
  if (r->ring_map && r->ring_map != MAP_FAILED) munmap(r->ring_map, r->ring_len);
525
616
  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);
617
+ if (r->br && (void *)r->br != MAP_FAILED) munmap(r->br, r->br_len);
618
+ if (r->bufs && (void *)r->bufs != MAP_FAILED) munmap(r->bufs, r->bufs_len);
528
619
  free(r->st);
529
620
  free(r->sq_first);
530
621
  free(r->sq_last);
531
622
  free(r->parked);
623
+ free(r->qbytes);
532
624
  free(r);
533
625
  }
534
626
 
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;
627
+ /* Restrict a ring made with R_DISABLED to what the layer submits (WP63 §6.2):
628
+ * the five opcodes and NOP, the three flags the layer sets, and no register
629
+ * opcode at all once it is enabled. */
630
+ static int ur_restrict(int fd) {
631
+ static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV, UR_OP_SEND, UR_OP_CLOSE, UR_OP_ASYNC_CANCEL, UR_OP_NOP};
632
+ struct ur_restriction res[sizeof ops + 1];
633
+ memset(res, 0, sizeof res);
634
+ for (size_t i = 0; i < sizeof ops; i++) {
635
+ res[i].opcode = UR_RESTRICTION_SQE_OP;
636
+ res[i].arg = ops[i];
637
+ }
638
+ res[sizeof ops].opcode = UR_RESTRICTION_SQE_FLAGS_ALLOWED;
639
+ res[sizeof ops].arg = UR_SQE_FIXED_FILE | UR_SQE_BUFFER_SELECT | UR_SQE_CQE_SKIP_SUCCESS;
640
+ return ur_register(fd, UR_REGISTER_RESTRICTIONS, res, (unsigned)(sizeof res / sizeof res[0]));
641
+ }
642
+
643
+ /* The pool's keeper: a ring of one entry made disabled, restricted to NOP
644
+ * with no submission flag, and enabled, with nothing mapped or registered.
645
+ * Answers its descriptor or the failing step's negative errno. */
646
+ static int ur_make_keeper(void) {
647
+ struct ur_params p;
648
+ int fd = ur_setup_try(1, 0, &p, UR_SETUP_R_DISABLED);
649
+ if (fd < 0) return fd;
650
+ struct ur_restriction res;
651
+ memset(&res, 0, sizeof res);
652
+ res.opcode = UR_RESTRICTION_SQE_OP;
653
+ res.arg = UR_OP_NOP;
654
+ int rc = ur_register(fd, UR_REGISTER_RESTRICTIONS, &res, 1);
655
+ if (rc >= 0) rc = ur_register(fd, UR_REGISTER_ENABLE_RINGS, NULL, 0);
656
+ if (rc < 0) {
657
+ close(fd);
658
+ return rc;
659
+ }
660
+ return fd;
661
+ }
662
+
663
+ /* A new ring of sizes `z`: the SQ and CQ mapped, a sparse table of
664
+ * `z->slots`, the buffer ring registered, the eventfd registered. A
665
+ * `restricted` ring is made disabled, restricted before anything else is
666
+ * registered, and enabled last; it has no SINGLE_ISSUER, which would tie it to
667
+ * the thread that enabled it, the one that runs `.preinit_array`. Answers 0
668
+ * with the ring in `*out`, or the failing step's negative errno with nothing
669
+ * left behind. */
670
+ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring **out) {
671
+ *out = NULL;
540
672
  struct ur_ring *r = calloc(1, sizeof *r);
541
673
  if (!r) return -ENOMEM;
542
674
  r->fd = -1;
@@ -544,7 +676,12 @@ static int ur_add_ring(struct nish_uring_set *s) {
544
676
  int rc = 0;
545
677
  struct ur_params p;
546
678
  /* 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);
679
+ if (restricted)
680
+ r->fd = ur_setup_try(z->sq_entries, z->cq_entries, &p,
681
+ UR_SETUP_R_DISABLED | UR_SETUP_COOP_TASKRUN | UR_SETUP_TASKRUN_FLAG | UR_SETUP_SUBMIT_ALL |
682
+ UR_SETUP_CQSIZE);
683
+ else
684
+ r->fd = ur_setup_flags(z->sq_entries, z->cq_entries, &p);
548
685
  if (r->fd < 0) {
549
686
  rc = r->fd;
550
687
  goto fail;
@@ -553,6 +690,12 @@ static int ur_add_ring(struct nish_uring_set *s) {
553
690
  rc = -EOPNOTSUPP;
554
691
  goto fail;
555
692
  }
693
+ if (restricted) {
694
+ rc = ur_ops_ok(r->fd);
695
+ if (rc < 0) goto fail;
696
+ rc = ur_restrict(r->fd);
697
+ if (rc < 0) goto fail;
698
+ }
556
699
  /* SQ and CQ share one mapping (FEAT_SINGLE_MMAP); the SQEs are another. */
557
700
  r->sq_entries = p.sq_entries;
558
701
  size_t sq_len = p.sq_off.array + (size_t)p.sq_entries * sizeof(uint32_t);
@@ -584,15 +727,16 @@ static int ur_add_ring(struct nish_uring_set *s) {
584
727
  /* The sparse file table: one entry per slot, filled by FILE_INDEX_ALLOC. */
585
728
  struct ur_rsrc_register files;
586
729
  memset(&files, 0, sizeof files);
587
- files.nr = s->slots;
730
+ files.nr = z->slots;
588
731
  files.flags = UR_RSRC_REGISTER_SPARSE;
589
732
  rc = ur_register(r->fd, UR_REGISTER_FILES2, &files, sizeof files);
590
733
  if (rc < 0) goto fail;
591
734
 
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);
735
+ /* The provided-buffer ring every multishot recv draws from; the set fills it. */
736
+ r->br_len = (size_t)z->buf_count * sizeof(struct ur_buf);
737
+ r->bufs_len = (size_t)z->buf_count * z->buf_size;
738
+ r->br = mmap(NULL, r->br_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
739
+ r->bufs = mmap(NULL, r->bufs_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
596
740
  if ((void *)r->br == MAP_FAILED || (void *)r->bufs == MAP_FAILED) {
597
741
  rc = -ENOMEM;
598
742
  goto fail;
@@ -600,24 +744,23 @@ static int ur_add_ring(struct nish_uring_set *s) {
600
744
  struct ur_buf_reg reg;
601
745
  memset(&reg, 0, sizeof reg);
602
746
  reg.ring_addr = (uint64_t)(uintptr_t)r->br;
603
- reg.ring_entries = s->buf_count;
747
+ reg.ring_entries = z->buf_count;
604
748
  reg.bgid = BGID;
605
749
  rc = ur_register(r->fd, UR_REGISTER_PBUF_RING, &reg, 1);
606
750
  if (rc < 0) goto fail;
607
- for (uint32_t i = 0; i < s->buf_count; i++) ur_recycle(s, r, i);
608
751
 
609
752
  /* 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) {
753
+ r->st = calloc(z->slots, sizeof *r->st);
754
+ r->sq_first = calloc(z->slots, sizeof *r->sq_first);
755
+ r->sq_last = calloc(z->slots, sizeof *r->sq_last);
756
+ r->parked = calloc(z->slots, sizeof *r->parked);
757
+ r->qbytes = calloc(z->slots, sizeof *r->qbytes);
758
+ if (!r->st || !r->sq_first || !r->sq_last || !r->parked || !r->qbytes) {
615
759
  rc = -ENOMEM;
616
760
  goto fail;
617
761
  }
618
762
 
619
- /* The eventfd the kernel signals for this ring, in the set's epoll with
620
- * the ring's index as its data. */
763
+ /* The eventfd the kernel signals for this ring. */
621
764
  r->efd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
622
765
  if (r->efd < 0) {
623
766
  rc = -errno;
@@ -625,21 +768,150 @@ static int ur_add_ring(struct nish_uring_set *s) {
625
768
  }
626
769
  rc = ur_register(r->fd, UR_REGISTER_EVENTFD, &r->efd, 1);
627
770
  if (rc < 0) goto fail;
771
+ if (restricted) {
772
+ rc = ur_register(r->fd, UR_REGISTER_ENABLE_RINGS, NULL, 0);
773
+ if (rc < 0) goto fail;
774
+ }
775
+ *out = r;
776
+ return 0;
777
+ fail:
778
+ ur_free_ring(r);
779
+ return rc < 0 ? rc : -EINVAL;
780
+ }
781
+
782
+ /* A new ring in the set: made, or taken from the pool when `--sandbox` made
783
+ * one (-ENOSPC when the pool is spent), then its buffer ring filled and its
784
+ * eventfd in the set's epoll with the ring's index as its data. Answers the
785
+ * ring's index or a negative errno, and leaves nothing behind on failure. */
786
+ static int ur_add_ring(struct nish_uring_set *s) {
787
+ if (s->nrings >= s->rings_max) return -ENOSPC;
788
+ struct ur_ring *r;
789
+ if (ur_pool.made) {
790
+ /* A full set asks again on every top-up, so a spent pool answers without
791
+ counting: `next` stays within the threads past `n` and never wraps to
792
+ a negative index (NURING-34). */
793
+ if (__atomic_load_n(&ur_pool.next, __ATOMIC_RELAXED) >= ur_pool.n) return -ENOSPC;
794
+ int i = __atomic_fetch_add(&ur_pool.next, 1, __ATOMIC_RELAXED);
795
+ if (i >= ur_pool.n) return -ENOSPC;
796
+ r = ur_pool.rings[i];
797
+ ur_pool.rings[i] = NULL;
798
+ } else {
799
+ struct ur_sizes z = {s->slots, s->buf_count, s->buf_size, s->sq_entries, s->cq_entries};
800
+ int rc = ur_make_ring(&z, 0, &r);
801
+ if (rc < 0) return rc;
802
+ }
803
+ for (uint32_t i = 0; i < s->buf_count; i++) ur_recycle(s, r, i);
628
804
  struct epoll_event ev;
629
805
  memset(&ev, 0, sizeof ev);
630
806
  ev.events = EPOLLIN;
631
807
  ev.data.u32 = (uint32_t)s->nrings;
632
808
  if (epoll_ctl(s->ep, EPOLL_CTL_ADD, r->efd, &ev) != 0) {
633
- rc = -errno;
634
- goto fail;
809
+ int rc = -errno;
810
+ ur_free_ring(r);
811
+ return rc;
635
812
  }
636
813
  s->rings[s->nrings] = r;
637
814
  s->stats.rings = ++s->nrings;
638
815
  s->stats.slots = (int64_t)s->nrings * s->slots;
639
816
  return s->nrings - 1;
640
- fail:
641
- ur_free_ring(s, r);
642
- return rc < 0 ? rc : -EINVAL;
817
+ }
818
+
819
+ /* Make `n` restricted rings of `slots` slots, at the sizes a set opens with
820
+ * by default, for the sets this process opens later (WP63 S3). `--sandbox`
821
+ * calls it once, before its seccomp filter and before any thread; with `n`
822
+ * of 0 it only records that this process may not make a ring (-EPERM). A
823
+ * kernel below the probe's floor makes none, and a ring that fails any step
824
+ * closes every ring made; either way the pool answers that step's errno from
825
+ * then on: the process falls back to descriptors for good.
826
+ * Answers the number of rings made or that errno, and writes each ring's
827
+ * descriptor to `fds`, which holds 65: the only descriptors the filter lets
828
+ * `io_uring_enter` take, then the keeper's after them, which the filter
829
+ * keeps every one of those numbers on (NURING-36). */
830
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds) {
831
+ if (ur_pool.made) return ur_pool.rc;
832
+ ur_pool.made = 1;
833
+ if (n <= 0) return ur_pool.rc = -EPERM;
834
+ /* A restricted ring drops SINGLE_ISSUER and DEFER_TASKRUN, so it alone
835
+ would take a 5.19 or 6.0 kernel; the probe holds the pool to the floor
836
+ an unsandboxed set has, 6.1 (NURING-33). */
837
+ int32_t probed = ur_probe_once();
838
+ if (probed < 0) return ur_pool.rc = probed;
839
+ if (n > UR_POOL_MAX) n = UR_POOL_MAX;
840
+ if (slots < 1) slots = 1;
841
+ if (slots > UR_MAX_SLOTS) slots = UR_MAX_SLOTS;
842
+ ur_pool.z = (struct ur_sizes){(uint32_t)slots, UR_BUF_COUNT_DEFAULT, UR_BUF_SIZE_DEFAULT, UR_SQ_DEFAULT, UR_CQ_DEFAULT};
843
+ for (int i = 0; i < n; i++) {
844
+ int rc = ur_make_ring(&ur_pool.z, 1, &ur_pool.rings[i]);
845
+ if (rc < 0) {
846
+ for (int j = 0; j < i; j++) ur_free_ring(ur_pool.rings[j]);
847
+ memset(ur_pool.rings, 0, sizeof ur_pool.rings);
848
+ return ur_pool.rc = rc;
849
+ }
850
+ }
851
+ int keeper = ur_make_keeper();
852
+ if (keeper < 0) {
853
+ for (int j = 0; j < n; j++) ur_free_ring(ur_pool.rings[j]);
854
+ memset(ur_pool.rings, 0, sizeof ur_pool.rings);
855
+ return ur_pool.rc = keeper;
856
+ }
857
+ for (int i = 0; i < n; i++) fds[i] = ur_pool.fds[i] = ur_pool.rings[i]->fd;
858
+ fds[n] = ur_pool.keeper = keeper;
859
+ ur_pool.n = n;
860
+ return ur_pool.rc = n;
861
+ }
862
+
863
+ /* The slots of each ring `--sandbox` made, 0 when it made none, or -1 when
864
+ * it ran and made none: a set must open with the pool's sizes. */
865
+ int32_t nish_uring_pooled(void) {
866
+ if (!ur_pool.made) return 0;
867
+ return ur_pool.rc > 0 ? (int32_t)ur_pool.z.slots : -1;
868
+ }
869
+
870
+ #ifdef NISH_URING_TEST_HOOKS
871
+ /* Test-only: tests/run.js's sandbox section builds the runtime with
872
+ * -DNISH_URING_TEST_HOOKS to ask the kernel what a premade ring takes, and
873
+ * scripts/build.sh never passes it. Submits one entry of opcode `op` with
874
+ * submission flags `flags`, on descriptor `fd`, to premade ring `i` before a
875
+ * listener has taken it, and answers its completion's res: -EACCES is the
876
+ * ring's restriction. -ENOENT when no such ring was made or it was taken. */
877
+ int32_t nish_uring_test_submit_fd(int32_t i, int32_t op, int32_t flags, int32_t fd);
878
+ int32_t nish_uring_test_submit_fd(int32_t i, int32_t op, int32_t flags, int32_t fd) {
879
+ if (!ur_pool.made || ur_pool.rc <= 0 || i < 0 || i >= ur_pool.n || ur_pool.rings[i] == NULL) return -ENOENT;
880
+ struct ur_ring *r = ur_pool.rings[i];
881
+ struct ur_sqe *e = ur_sqe(r);
882
+ if (e == NULL) return -EBUSY;
883
+ e->opcode = (uint8_t)op;
884
+ e->flags = (uint8_t)flags;
885
+ e->fd = fd;
886
+ e->user_data = 1;
887
+ int rc;
888
+ do rc = ur_enter(r, r->to_submit, 1, UR_ENTER_GETEVENTS);
889
+ while (rc == -EINTR);
890
+ if (rc < 0) return rc;
891
+ r->to_submit = 0;
892
+ uint32_t head = *r->cq_head;
893
+ if (head == __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) return -EAGAIN;
894
+ int32_t res = r->cqes[head & *r->cq_mask].res;
895
+ __atomic_store_n(r->cq_head, head + 1, __ATOMIC_RELEASE);
896
+ return res;
897
+ }
898
+ /* As `nish_uring_test_submit_fd`, on no file. */
899
+ int32_t nish_uring_test_submit(int32_t i, int32_t op, int32_t flags);
900
+ 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); }
901
+ /* The number premade ring `i` was made at, whether or not a set has taken
902
+ * it since, or the keeper's for `i` equal to the ring count; -ENOENT for
903
+ * any other `i`. */
904
+ int32_t nish_uring_test_fd(int32_t i);
905
+ int32_t nish_uring_test_fd(int32_t i) {
906
+ if (!ur_pool.made || ur_pool.rc <= 0 || i < 0 || i > ur_pool.n) return -ENOENT;
907
+ return i == ur_pool.n ? ur_pool.keeper : ur_pool.fds[i];
908
+ }
909
+ #endif
910
+
911
+ static int64_t ur_now_ms(void) {
912
+ struct timespec t;
913
+ clock_gettime(CLOCK_MONOTONIC, &t);
914
+ return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000;
643
915
  }
644
916
 
645
917
  /* Keep min(ACCEPT_DEPTH, free slots) accepts in flight on the accepting ring,
@@ -649,6 +921,10 @@ fail:
649
921
  * connections wait in the listener's backlog until a close frees a slot. */
650
922
  static void ur_top_up(struct nish_uring_set *s) {
651
923
  if (s->accept_stopped || s->nrings == 0) return;
924
+ if (s->accept_paused) {
925
+ if (ur_now_ms() < s->resume_ms) return;
926
+ s->accept_paused = 0;
927
+ }
652
928
  struct ur_ring *r = s->rings[s->acceptor];
653
929
  if (r->live + r->accepting >= (long)s->slots && r->accepting == 0) {
654
930
  int moved = 0;
@@ -717,6 +993,7 @@ static void ur_unlink_all(struct nish_uring_set *s, struct ur_send *q) {
717
993
  static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot) {
718
994
  struct ur_send *q = r->sq_first[slot];
719
995
  r->sq_first[slot] = r->sq_last[slot] = NULL;
996
+ r->qbytes[slot] = 0;
720
997
  while (q) {
721
998
  struct ur_send *next = q->next;
722
999
  q->next = NULL;
@@ -725,10 +1002,16 @@ static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t
725
1002
  }
726
1003
  }
727
1004
 
1005
+ /* An accept that failed for this one peer; the next is submitted at once. */
728
1006
  static int ur_transient_accept(int res) {
729
1007
  return res == -EAGAIN || res == -EINTR || res == -ECONNABORTED || res == -EPROTO;
730
1008
  }
731
1009
 
1010
+ /* An accept that found the kernel short of memory for a socket (NURING-28).
1011
+ * It clears as memory frees, so accepting pauses and resumes; submitting
1012
+ * again at once would spin on the same answer while the shortage lasts. */
1013
+ static int ur_pressure_accept(int res) { return res == -ENOMEM || res == -ENOBUFS; }
1014
+
732
1015
  static void ur_event(struct nish_uring_event *ev, int kind, int ring, uint32_t slot, int res, const uint8_t *data) {
733
1016
  ev->kind = kind;
734
1017
  ev->ring = ring;
@@ -750,6 +1033,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
750
1033
  ur_unlink_all(s, q);
751
1034
  return 0;
752
1035
  }
1036
+ if (res > 0) r->qbytes[slot] -= (uint32_t)res < r->qbytes[slot] ? (uint32_t)res : r->qbytes[slot];
753
1037
  if (res > 0 && q->off + (uint32_t)res < q->len) {
754
1038
  q->off += (uint32_t)res;
755
1039
  s->stats.send_requeues++;
@@ -773,10 +1057,12 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
773
1057
  uint32_t slot = (uint32_t)(ud >> 32);
774
1058
  if (op == UR_ACCEPT) {
775
1059
  r->accepting--;
1060
+ res = UR_ACCEPT_RES(res);
776
1061
  int wrote = 0;
777
1062
  if (res >= 0 && (uint32_t)res < s->slots) {
778
1063
  uint32_t at = (uint32_t)res;
779
- r->st[at] = (r->st[at] & ~(ST_ARMED | ST_PARKED)) | ST_OPEN;
1064
+ s->backoff_ms = 0;
1065
+ r->st[at] = (r->st[at] & ~(ST_ARMED | ST_PARKED | ST_HELD | ST_HUP)) | ST_OPEN;
780
1066
  r->live++;
781
1067
  s->stats.live++;
782
1068
  ur_arm(r, at);
@@ -786,6 +1072,17 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
786
1072
  /* A slot past the table the set registered; the kernel never answers
787
1073
  * one, so it is counted rather than trusted. */
788
1074
  s->stats.bound_violations++;
1075
+ } else if (ur_pressure_accept(res)) {
1076
+ /* Accepts already in flight may fail the same way; one pause covers them. */
1077
+ if (!s->accept_paused) {
1078
+ s->accept_paused = 1;
1079
+ s->backoff_ms = s->backoff_ms ? s->backoff_ms * 2 : ACCEPT_BACKOFF_MIN_MS;
1080
+ if (s->backoff_ms > ACCEPT_BACKOFF_MAX_MS) s->backoff_ms = ACCEPT_BACKOFF_MAX_MS;
1081
+ s->resume_ms = ur_now_ms() + s->backoff_ms;
1082
+ s->stats.accept_pauses++;
1083
+ ur_event(ev, NISH_URING_ACCEPT_PAUSED, ri, 0, res, NULL);
1084
+ wrote = 1;
1085
+ }
789
1086
  } else if (!ur_transient_accept(res)) {
790
1087
  s->stats.accept_errors++;
791
1088
  if (res == -ENFILE) s->stats.bound_violations++;
@@ -812,11 +1109,19 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
812
1109
  s->lent_ring[s->nlent] = (uint32_t)ri;
813
1110
  s->lent_bid[s->nlent] = (uint16_t)bid;
814
1111
  s->nlent++;
815
- if (!more) ur_arm(r, slot);
1112
+ if (!more && !(r->st[slot] & ST_HELD)) ur_arm(r, slot);
816
1113
  ur_event(ev, NISH_URING_DATA, ri, slot, res, r->bufs + (size_t)bid * s->buf_size);
817
1114
  return 1;
818
1115
  }
819
1116
  if (has_buf) ur_recycle(s, r, bid);
1117
+ /* Only a hold cancels a live slot's recv (a close moves the generation on
1118
+ * first). The recv stays down while the slot is held, and one let go before
1119
+ * the cancel landed is armed again; a held recv out of buffers stays down. */
1120
+ if (!more && res == -ECANCELED) {
1121
+ if (!(r->st[slot] & ST_HELD)) ur_arm(r, slot);
1122
+ return 0;
1123
+ }
1124
+ if (!more && res == -ENOBUFS && (r->st[slot] & ST_HELD)) return 0;
820
1125
  if (res == -ENOBUFS) {
821
1126
  s->stats.nobufs_rearms++;
822
1127
  if (!more && !(r->st[slot] & ST_PARKED)) {
@@ -826,6 +1131,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
826
1131
  return 0;
827
1132
  }
828
1133
  if (more) return 0;
1134
+ r->st[slot] |= ST_HUP;
829
1135
  ur_event(ev, NISH_URING_HUP, ri, slot, res, NULL);
830
1136
  return 1;
831
1137
  }
@@ -895,13 +1201,21 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
895
1201
  }
896
1202
  if (d.slots > UR_MAX_SLOTS) return -EINVAL;
897
1203
  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;
1204
+ if (d.sq_entries == 0) d.sq_entries = UR_SQ_DEFAULT;
1205
+ if (d.cq_entries == 0) d.cq_entries = UR_CQ_DEFAULT;
900
1206
  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;
1207
+ if (d.buf_count == 0) d.buf_count = UR_BUF_COUNT_DEFAULT;
1208
+ if (d.buf_size == 0) d.buf_size = UR_BUF_SIZE_DEFAULT;
903
1209
  if (d.rings_max > 4096 || d.buf_count > 32768 || (d.buf_count & (d.buf_count - 1)) != 0 || d.buf_size > (1 << 20))
904
1210
  return -EINVAL;
1211
+ /* A pooled ring was made at its own sizes, before the program could ask. */
1212
+ if (ur_pool.made) {
1213
+ d.slots = (int32_t)ur_pool.z.slots;
1214
+ d.sq_entries = (int32_t)ur_pool.z.sq_entries;
1215
+ d.cq_entries = (int32_t)ur_pool.z.cq_entries;
1216
+ d.buf_count = (int32_t)ur_pool.z.buf_count;
1217
+ d.buf_size = (int32_t)ur_pool.z.buf_size;
1218
+ }
905
1219
  struct nish_uring_set *s = calloc(1, sizeof *s);
906
1220
  if (!s) return -ENOMEM;
907
1221
  s->listener = listener;
@@ -983,12 +1297,19 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms)
983
1297
  if (r->nparked) parked = 1;
984
1298
  }
985
1299
  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;
1300
+ int32_t bound = UR_SLEEP_MAX_MS;
1301
+ if (s->accept_paused) {
1302
+ int64_t left = s->resume_ms - ur_now_ms();
1303
+ if (left <= 0) return 0;
1304
+ if (left < bound) bound = (int32_t)left;
1305
+ }
1306
+ return timeout_ms < 0 || timeout_ms > bound ? bound : timeout_ms;
987
1307
  }
988
1308
 
989
1309
  /* What the last wait left to do: give back the buffers it lent, re-arm the
990
1310
  * recvs it parked, and submit the sends that found the SQ full. */
991
1311
  static void ur_catch_up(struct nish_uring_set *s) {
1312
+ if (s->accept_paused) ur_top_up(s); /* accepting resumes once the back-off ends */
992
1313
  for (int i = 0; i < s->nlent; i++) ur_recycle(s, s->rings[s->lent_ring[i]], s->lent_bid[i]);
993
1314
  s->nlent = 0;
994
1315
  for (int ri = 0; ri < s->nrings; ri++) {
@@ -998,7 +1319,7 @@ static void ur_catch_up(struct nish_uring_set *s) {
998
1319
  for (uint32_t i = 0; i < n; i++) {
999
1320
  uint32_t slot = r->parked[i];
1000
1321
  r->st[slot] &= ~ST_PARKED;
1001
- if ((r->st[slot] & (ST_OPEN | ST_ARMED)) == ST_OPEN) ur_arm(r, slot);
1322
+ if ((r->st[slot] & (ST_OPEN | ST_ARMED | ST_HELD)) == ST_OPEN) ur_arm(r, slot);
1002
1323
  }
1003
1324
  }
1004
1325
  /* Scanning every slot of every ring costs rings * slots, but only after a
@@ -1087,6 +1408,7 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
1087
1408
  q->off = 0;
1088
1409
  q->len = (uint32_t)n;
1089
1410
  q->in_flight = 0;
1411
+ r->qbytes[slot] += (uint32_t)n;
1090
1412
  if (r->sq_last[slot]) {
1091
1413
  r->sq_last[slot]->next = q;
1092
1414
  } else {
@@ -1130,13 +1452,65 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1130
1452
  return 0;
1131
1453
  }
1132
1454
 
1455
+ /* The bytes queued for (ring, slot) that the kernel has not yet sent, or -1
1456
+ * for a slot that is not open. runtime-net.c bounds a connection's send area
1457
+ * with it (NURING-2): a successful send makes no event, so the caller asks. */
1458
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
1459
+ if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return -1;
1460
+ const struct ur_ring *r = s->rings[ring];
1461
+ return (r->st[slot] & ST_OPEN) ? (int64_t)r->qbytes[slot] : -1;
1462
+ }
1463
+
1464
+ /* 1 when no accept is in flight and none can be: every ring's table is full
1465
+ * and the set has `rings_max` rings or could not make another. A connection
1466
+ * then waits in the listener's backlog, and tcpAccept answers -23
1467
+ * (NURING-6). A close frees a slot and starts accepting again. */
1468
+ int32_t nish_uring_full(const struct nish_uring_set *s) {
1469
+ if (s->accept_stopped || s->accept_paused) return 0;
1470
+ for (int i = 0; i < s->nrings; i++) {
1471
+ const struct ur_ring *r = s->rings[i];
1472
+ if (r->accepting > 0 || r->live < (long)s->slots) return 0;
1473
+ }
1474
+ return 1;
1475
+ }
1476
+
1477
+ /* Hold (`on` 1) or let go (`on` 0) a slot's receive. A hold cancels the
1478
+ * slot's multishot recv by its tag, not by its descriptor, so the slot's sends
1479
+ * in flight go on, and keeps it from being armed again; the bytes the kernel
1480
+ * received before the cancel takes effect are still reported. runtime-net.c
1481
+ * holds a slot whose receive queue reaches its bound, so a peer the program
1482
+ * does not read cannot grow it (NURING-9). Letting go arms the recv again.
1483
+ * -EBADF for a slot that is not open; -EAGAIN, nothing changed, when the SQ
1484
+ * has no room for the cancel. */
1485
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
1486
+ struct ur_ring *r = ur_open_slot(s, ring, slot);
1487
+ if (!r) return -EBADF;
1488
+ uint32_t st = r->st[slot];
1489
+ if (!on) {
1490
+ r->st[slot] = st & ~ST_HELD;
1491
+ if ((st & (ST_ARMED | ST_PARKED)) == 0) ur_arm(r, (uint32_t)slot);
1492
+ return 0;
1493
+ }
1494
+ if (st & ST_HELD) return 0;
1495
+ if (st & ST_ARMED) {
1496
+ struct ur_sqe *e = ur_sqe(r);
1497
+ if (!e) return -EAGAIN;
1498
+ e->opcode = UR_OP_ASYNC_CANCEL;
1499
+ e->addr = ur_tag(UR_RECV, st, (uint32_t)slot);
1500
+ e->flags = UR_SQE_CQE_SKIP_SUCCESS;
1501
+ e->user_data = ur_tag(UR_CANCEL, st, (uint32_t)slot);
1502
+ }
1503
+ r->st[slot] = st | ST_HELD;
1504
+ return 0;
1505
+ }
1506
+
1133
1507
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) { *st = s->stats; }
1134
1508
 
1135
1509
  /* Close every ring, which ends whatever the kernel still held, and free the
1136
1510
  * sends it held them for. The caller's listener stays open: it is theirs. */
1137
1511
  void nish_uring_destroy(struct nish_uring_set *s) {
1138
1512
  if (!s) return;
1139
- for (int i = 0; i < s->nrings; i++) ur_free_ring(s, s->rings[i]);
1513
+ for (int i = 0; i < s->nrings; i++) ur_free_ring(s->rings[i]);
1140
1514
  while (s->all) {
1141
1515
  struct ur_send *q = s->all;
1142
1516
  s->all = q->next_all;
@@ -1150,6 +1524,13 @@ void nish_uring_destroy(struct nish_uring_set *s) {
1150
1524
  #else /* not Linux: no io_uring, so a set is never available */
1151
1525
 
1152
1526
  int32_t nish_uring_probe(void) { return -38; }
1527
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds) {
1528
+ (void)n;
1529
+ (void)slots;
1530
+ (void)fds;
1531
+ return -38;
1532
+ }
1533
+ int32_t nish_uring_pooled(void) { return 0; }
1153
1534
  int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts) {
1154
1535
  (void)listener;
1155
1536
  (void)opts;
@@ -1185,6 +1566,23 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1185
1566
  (void)slot;
1186
1567
  return -38;
1187
1568
  }
1569
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
1570
+ (void)s;
1571
+ (void)ring;
1572
+ (void)slot;
1573
+ return -1;
1574
+ }
1575
+ int32_t nish_uring_full(const struct nish_uring_set *s) {
1576
+ (void)s;
1577
+ return 0;
1578
+ }
1579
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on) {
1580
+ (void)s;
1581
+ (void)ring;
1582
+ (void)slot;
1583
+ (void)on;
1584
+ return -38;
1585
+ }
1188
1586
  void nish_uring_stats(const struct nish_uring_set *s, struct nish_uring_stats *st) {
1189
1587
  (void)s;
1190
1588
  memset(st, 0, sizeof *st);