@amritk/nish-x86_64-linux 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 (42) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +282 -126
  3. package/package.json +2 -2
  4. package/runtime/core.d.ts +16 -0
  5. package/runtime/core.mjs +5 -0
  6. package/runtime/nish.d.ts +204 -33
  7. package/runtime/nish.h +19 -3
  8. package/runtime/nish.mjs +5 -1
  9. package/runtime/runtime-cross.c +15 -0
  10. package/runtime/runtime-net.c +876 -10
  11. package/runtime/runtime-region.c +21 -0
  12. package/runtime/runtime-sandbox.c +112 -15
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +533 -77
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +474 -5
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +125 -29
  20. package/std/README.md +3 -3
  21. package/std/crypto/base64url.ts +102 -1
  22. package/std/crypto/scram.ts +142 -0
  23. package/std/crypto/sha1.ts +99 -41
  24. package/std/host/ring.ts +1 -1
  25. package/std/host/text.ts +1 -1
  26. package/std/net/fetch.ts +9 -1
  27. package/std/net/handler.ts +79 -15
  28. package/std/net/http-server.ts +2 -3
  29. package/std/net/http1-server.ts +294 -51
  30. package/std/net/http3-server.ts +26 -15
  31. package/std/net/http3.ts +2 -2
  32. package/std/net/oidc.ts +1107 -0
  33. package/std/net/postgres-async.ts +64 -33
  34. package/std/net/postgres.ts +65 -76
  35. package/std/net/quic-conn-ack.ts +57 -7
  36. package/std/net/quic-frame.ts +104 -2
  37. package/std/net/quic-listener.ts +9 -2
  38. package/std/net/quic.ts +16 -1
  39. package/std/net/serve.ts +10 -8
  40. package/std/net/websocket.ts +188 -49
  41. package/std/sqlite.ts +325 -0
  42. package/std/text.ts +80 -11
@@ -66,7 +66,7 @@ typedef int nish_net_unused;
66
66
  #include <netinet/tcp.h>
67
67
  #include <stddef.h>
68
68
  #include <stdint.h>
69
- #include <stddef.h>
69
+ #include <stdlib.h>
70
70
  #include <string.h>
71
71
  #include <sys/resource.h>
72
72
  #include <sys/socket.h>
@@ -77,6 +77,7 @@ typedef int nish_net_unused;
77
77
  #include <linux/filter.h>
78
78
  #include <netinet/udp.h>
79
79
  #include <sys/epoll.h>
80
+ #include <time.h>
80
81
  /* glibc before 2.29 names neither; the numbers are the kernel's ABI. */
81
82
  #ifndef UDP_SEGMENT
82
83
  #define UDP_SEGMENT 103
@@ -201,6 +202,9 @@ static void nish_net_raise_nofile(void) {
201
202
  rlim_t want = r.rlim_max;
202
203
  #if !defined(__linux__)
203
204
  if (want > (rlim_t)OPEN_MAX) want = (rlim_t)OPEN_MAX;
205
+ #else
206
+ /* Every descriptor stays below 2^20, where slot handles begin. */
207
+ if (want > (rlim_t)(1 << 20)) want = (rlim_t)(1 << 20);
204
208
  #endif
205
209
  if (r.rlim_cur >= want) return;
206
210
  r.rlim_cur = want;
@@ -228,9 +232,780 @@ int32_t nish_net_address(nish_array *out, const nish_str *host, int32_t port) {
228
232
  return 0;
229
233
  }
230
234
 
235
+ /* Slot handles (WP63 S2, docs/wp63-iouring-net.md §5). A listener opened
236
+ with `tcpListenFlags` flag 8 on Linux accepts into an io_uring set
237
+ (runtime-uring.c) rather than into descriptors, so a connection costs a
238
+ slot in a ring's file table instead of an entry in the process's: a set of
239
+ N rings holds N times RLIMIT_NOFILE connections in N + 1 descriptors.
240
+
241
+ A handle is still an `i32`. Below 2^20 it is a descriptor, which is why
242
+ `nish_net_raise_nofile` stops the soft limit at 2^20 on Linux. At or above
243
+ it, `2^20 + ring * 2^20 + field` names a slot: `ring` is a global ring id,
244
+ 64 per listener (`NR_RINGS`), and `field` is `slot + slots * (gen % G)`,
245
+ with `G = 2^20 / slots` the generations a slot's handles cycle through. A
246
+ close moves the slot's generation on, so the handle a program closed
247
+ answers -9 at once and does not reach the next connection in that slot
248
+ until the slot has been reused G times (1,024 at a limit of 1,024; never
249
+ fewer than 64). At most `NR_SETS` ring listeners exist in a process, which
250
+ keeps every handle below 2^31; their ids are not reused, so a closed
251
+ listener's handles answer -9 for good.
252
+
253
+ `netShutdown` queues an io_uring SHUTDOWN behind the slot's sends, so the
254
+ peer reads every byte written before it and then the FIN (NURING-39);
255
+ `netMss` answers -1, its "not known", for a slot. Every other call that
256
+ takes a handle and is not one of the slot paths here (`sendFd`,
257
+ `connectResult`, `peerCredentials`, ...) passes it to the kernel as a
258
+ descriptor, which no process holds at 2^20 or above, so it answers -9
259
+ (EBADF) with nothing to check.
260
+
261
+ Readiness keeps the descriptor contract: a read with nothing queued, a write
262
+ to a full send area and an accept with nothing accepted answer -11, and the
263
+ loop reports a slot level-triggered, in the order it became ready. The set
264
+ receives on its own (a multishot recv per slot), so what arrives is copied
265
+ into the slot's receive queue at once and the ring's buffer goes back. A
266
+ write is copied into the set's send queue; `NR_AREA` bounds what one slot
267
+ may hold there (NURING-2), and a full area is -11 until the kernel has sent
268
+ enough. A full set, every ring's table full and no ring left to make, is
269
+ -23 from `tcpAccept` (NURING-6): the connection waits in the backlog until a
270
+ close frees a slot. */
271
+ #if defined(__linux__)
272
+ struct nish_uring_set;
273
+ struct nish_uring_event {
274
+ int32_t kind;
275
+ int32_t ring;
276
+ int32_t slot;
277
+ int32_t res;
278
+ const uint8_t *data;
279
+ };
280
+ struct nish_uring_opts {
281
+ int32_t slots;
282
+ int32_t rings_max;
283
+ int32_t sq_entries;
284
+ int32_t cq_entries;
285
+ int32_t buf_count;
286
+ int32_t buf_size;
287
+ };
288
+ int32_t nish_uring_probe(void);
289
+ int32_t nish_uring_premake(int32_t n, int32_t slots, int32_t *fds);
290
+ int32_t nish_uring_pooled(void);
291
+ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const struct nish_uring_opts *opts);
292
+ int32_t nish_uring_fd(const struct nish_uring_set *s);
293
+ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
294
+ int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
295
+ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
296
+ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
297
+ int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how);
298
+ int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
299
+ int32_t nish_uring_full(const struct nish_uring_set *s);
300
+ int32_t nish_uring_hold(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t on);
301
+ void nish_uring_destroy(struct nish_uring_set *s);
302
+
303
+ #define NR_BASE (1 << 20)
304
+ #define NR_RINGS 64
305
+ #define NR_SETS 31
306
+ /* The most bytes one slot may have queued and not yet sent (NURING-2), and
307
+ the receive queue at which the slot's receive is held (NURING-9). */
308
+ #define NR_AREA (256 * 1024)
309
+ /* What a held slot's queue may still take from the kernel before the cancel
310
+ lands; past it the connection ends with -105 (ENOBUFS). */
311
+ #define NR_RECV_MAX (16 * 1024 * 1024)
312
+ /* A slot's first receive queue, doubled as it fills: small enough for
313
+ glibc's thread cache, and freed once drained (NURING-38). */
314
+ #define NR_RECV_FIRST 1024
315
+ /* The most slots per ring: a table is the kernel's memory, sized up front. */
316
+ #define NR_SLOTS_MAX 16384
317
+ #define NR_EVENTS 256
318
+
319
+ /* A loop's record of one virtual source, a slot or a ring listener: the loop
320
+ it is in (-1 for none), what it watches and its token, and its place on
321
+ the list of sources that are ready, kept in the order they became so. */
322
+ struct nr_vent {
323
+ int32_t loop, events, token;
324
+ uint8_t listed, is_listener;
325
+ struct nr_vent *prev, *next;
326
+ };
327
+
328
+ /* One slot's connection. `gen` counts the slot's closes; `lingering` is a
329
+ close the program made while bytes were still queued, which ends when they
330
+ are sent, the peer goes or a send fails. `wshut` is a shutdown of the
331
+ write side, after which a write answers -32 as send's EPIPE would. */
332
+ struct nr_conn {
333
+ struct nr_vent v;
334
+ uint32_t gen;
335
+ uint8_t open, lingering, hup, wblocked, rheld, wshut;
336
+ int32_t hup_res, werr;
337
+ uint8_t *rbuf;
338
+ uint32_t rlen, roff, rcap;
339
+ };
340
+
341
+ struct nr_set {
342
+ struct nish_uring_set *u;
343
+ struct nr_vent v; /* the listener's own entry */
344
+ int32_t listener, port, slots, gens, dead, accept_err;
345
+ int32_t accept_once; /* an accept that found memory short, answered by the next tcpAccept */
346
+ struct nr_conn *conns[NR_RINGS];
347
+ uint32_t *fifo; /* accepted (ring << 20 | slot), not yet taken */
348
+ uint32_t fhead, flen, fcap;
349
+ uint32_t *pend; /* wblocked or lingering slots, checked after each pump */
350
+ uint32_t npend, pcap;
351
+ };
352
+
353
+ static struct nr_set *nr_sets[NR_SETS];
354
+ static int32_t nr_nsets;
355
+ /* `netRingState()`: 0 until a listener opens a set (1) or meets a refusal. */
356
+ static int32_t nr_state;
357
+ static struct nr_vent *nr_head, *nr_tail;
358
+
359
+ static void nr_unlist(struct nr_vent *v) {
360
+ if (!v->listed) return;
361
+ if (v->prev) v->prev->next = v->next; else nr_head = v->next;
362
+ if (v->next) v->next->prev = v->prev; else nr_tail = v->prev;
363
+ v->prev = v->next = NULL;
364
+ v->listed = 0;
365
+ }
366
+
367
+ static void nr_append(struct nr_vent *v) {
368
+ v->prev = nr_tail;
369
+ v->next = NULL;
370
+ if (nr_tail) nr_tail->next = v; else nr_head = v;
371
+ nr_tail = v;
372
+ v->listed = 1;
373
+ }
374
+
375
+ static struct nr_conn *nr_conn_of(struct nr_vent *v) { return (struct nr_conn *)(void *)v; }
376
+ static struct nr_set *nr_set_of(struct nr_vent *v) {
377
+ return (struct nr_set *)(void *)((char *)v - offsetof(struct nr_set, v));
378
+ }
379
+
380
+ /* Whether the slot's connection has failed: a receive's error or a send's.
381
+ A peer's FIN alone (`hup_res` 0) is not a failure. */
382
+ static int nr_failed(const struct nr_conn *c) {
383
+ return c->werr || (c->hup && c->hup_res < 0);
384
+ }
385
+
386
+ /* The readiness bits a source holds now: 1 readable, 2 writable, 4 an error. */
387
+ static int32_t nr_bits(struct nr_vent *v) {
388
+ if (v->is_listener) {
389
+ struct nr_set *S = nr_set_of(v);
390
+ return (S->flen > 0 || S->accept_err || S->accept_once) ? 1 : 0;
391
+ }
392
+ struct nr_conn *c = nr_conn_of(v);
393
+ int32_t b = 0;
394
+ if (c->rlen > c->roff || c->hup || c->werr) b |= 1;
395
+ if (nr_failed(c)) b |= 4;
396
+ else if (!c->wblocked) b |= 2;
397
+ return b;
398
+ }
399
+
400
+ /* Puts a source that has become ready at the tail of the list. */
401
+ static void nr_touch(struct nr_vent *v) {
402
+ if (v->loop >= 0 && !v->listed && (nr_bits(v) & (v->events | 4))) nr_append(v);
403
+ }
404
+
405
+ static void nr_unregister(struct nr_vent *v) {
406
+ if (v->loop < 0) return;
407
+ nr_unlist(v);
408
+ v->loop = -1;
409
+ }
410
+
411
+ static int nr_push(uint32_t **a, uint32_t *n, uint32_t *cap, uint32_t x) {
412
+ if (*n == *cap) {
413
+ uint32_t c = *cap ? *cap * 2 : 64;
414
+ uint32_t *p = realloc(*a, (size_t)c * sizeof *p);
415
+ if (!p) return -12;
416
+ *a = p;
417
+ *cap = c;
418
+ }
419
+ (*a)[(*n)++] = x;
420
+ return 0;
421
+ }
422
+
423
+ /* The connection a handle names, or NULL for a stale or foreign one. */
424
+ static struct nr_conn *nr_decode(int32_t h, struct nr_set **set, int32_t *ring, int32_t *slot) {
425
+ if (h < NR_BASE) return NULL;
426
+ uint32_t x = (uint32_t)h - NR_BASE;
427
+ uint32_t g = x >> 20, field = x & (NR_BASE - 1);
428
+ uint32_t si = g / NR_RINGS, ri = g % NR_RINGS;
429
+ if (si >= (uint32_t)nr_nsets) return NULL;
430
+ struct nr_set *S = nr_sets[si];
431
+ if (S->dead || !S->conns[ri]) return NULL;
432
+ uint32_t sl = field % (uint32_t)S->slots, gm = field / (uint32_t)S->slots;
433
+ struct nr_conn *c = &S->conns[ri][sl];
434
+ if (gm >= (uint32_t)S->gens || !c->open || c->lingering || c->gen % (uint32_t)S->gens != gm) return NULL;
435
+ *set = S;
436
+ *ring = (int32_t)ri;
437
+ *slot = (int32_t)sl;
438
+ return c;
439
+ }
440
+
441
+ static int32_t nr_encode(const struct nr_set *S, int32_t si, uint32_t ring, uint32_t slot) {
442
+ const struct nr_conn *c = &S->conns[ring][slot];
443
+ uint32_t field = slot + (uint32_t)S->slots * (c->gen % (uint32_t)S->gens);
444
+ return (int32_t)((uint32_t)NR_BASE + ((uint32_t)si * NR_RINGS + ring) * (uint32_t)NR_BASE + field);
445
+ }
446
+
447
+ /* Ends a slot for good: the set closes it and the slot goes back to the
448
+ accepting job. 1 when done, 0 when the set had no room and it is retried. */
449
+ static int nr_finish(struct nr_set *S, uint32_t ring, uint32_t slot) {
450
+ struct nr_conn *c = &S->conns[ring][slot];
451
+ if (nish_uring_close(S->u, (int32_t)ring, (int32_t)slot) == -11) return 0;
452
+ c->open = 0;
453
+ c->lingering = 0;
454
+ return 1;
455
+ }
456
+
457
+ static void nr_dispatch(struct nr_set *S, const struct nish_uring_event *e) {
458
+ if (e->kind == 5) {
459
+ S->accept_err = e->res < 0 ? e->res : -5;
460
+ nr_touch(&S->v);
461
+ return;
462
+ }
463
+ if (e->kind == 6) {
464
+ /* Memory was short for an accept; the set pauses and accepts again. A
465
+ descriptor's accept answers the errno once, and so does tcpAccept. */
466
+ S->accept_once = e->res < 0 ? e->res : -12;
467
+ nr_touch(&S->v);
468
+ return;
469
+ }
470
+ if (e->ring < 0 || e->ring >= NR_RINGS || e->slot < 0 || e->slot >= S->slots) return;
471
+ if (!S->conns[e->ring]) {
472
+ S->conns[e->ring] = calloc((size_t)S->slots, sizeof(struct nr_conn));
473
+ /* With no table to record it in, an accepted slot is closed at once
474
+ rather than left open in the ring for good. */
475
+ if (!S->conns[e->ring]) {
476
+ if (e->kind == 1) (void)nish_uring_close(S->u, e->ring, e->slot);
477
+ return;
478
+ }
479
+ }
480
+ struct nr_conn *c = &S->conns[e->ring][e->slot];
481
+ if (e->kind == 1) {
482
+ /* nr_close freed the queue of a slot reused here (NURING-38). */
483
+ uint32_t gen = c->gen;
484
+ memset(c, 0, sizeof *c);
485
+ c->gen = gen;
486
+ c->open = 1;
487
+ c->v.loop = -1;
488
+ uint32_t i = (S->fhead + S->flen) % (S->fcap ? S->fcap : 1);
489
+ if (S->flen == S->fcap) {
490
+ /* Grow the ring buffer, unrolled into the new array from its head. */
491
+ uint32_t nc = S->fcap ? S->fcap * 2 : 64;
492
+ uint32_t *p = malloc((size_t)nc * sizeof *p);
493
+ if (!p) {
494
+ (void)nr_finish(S, (uint32_t)e->ring, (uint32_t)e->slot);
495
+ return;
496
+ }
497
+ for (uint32_t k = 0; k < S->flen; k++) p[k] = S->fifo[(S->fhead + k) % S->fcap];
498
+ free(S->fifo);
499
+ S->fifo = p;
500
+ S->fcap = nc;
501
+ S->fhead = 0;
502
+ i = S->flen;
503
+ }
504
+ S->fifo[i] = (uint32_t)e->ring << 20 | (uint32_t)e->slot;
505
+ S->flen++;
506
+ nr_touch(&S->v);
507
+ return;
508
+ }
509
+ if (!c->open) return;
510
+ if (e->kind == 2) {
511
+ /* Bytes after the slot has hung up, here or by the kernel, are dropped,
512
+ so a read never runs past the failure it reports. */
513
+ if (c->lingering || c->hup || e->res <= 0) return;
514
+ uint32_t n = (uint32_t)e->res;
515
+ if ((uint64_t)(c->rlen - c->roff) + n > NR_RECV_MAX) {
516
+ c->hup = 1;
517
+ c->hup_res = -105;
518
+ nr_touch(&c->v);
519
+ return;
520
+ }
521
+ if (c->rlen + n > c->rcap) {
522
+ if (c->roff > 0) {
523
+ memmove(c->rbuf, c->rbuf + c->roff, c->rlen - c->roff);
524
+ c->rlen -= c->roff;
525
+ c->roff = 0;
526
+ }
527
+ if (c->rlen + n > c->rcap) {
528
+ uint32_t nc = c->rcap ? c->rcap : NR_RECV_FIRST;
529
+ while (nc < c->rlen + n) nc *= 2;
530
+ uint8_t *p = realloc(c->rbuf, nc);
531
+ if (!p) {
532
+ c->hup = 1;
533
+ c->hup_res = -12;
534
+ nr_touch(&c->v);
535
+ return;
536
+ }
537
+ c->rbuf = p;
538
+ c->rcap = nc;
539
+ }
540
+ }
541
+ memcpy(c->rbuf + c->rlen, e->data, n);
542
+ c->rlen += n;
543
+ if (!c->rheld && c->rlen - c->roff >= NR_AREA && nish_uring_hold(S->u, e->ring, e->slot, 1) == 0) c->rheld = 1;
544
+ } else if (e->kind == 3) {
545
+ /* The first hangup is the one a read reports: a later one, such as the
546
+ kernel's after a -105 here, does not overwrite it (NURING-27). */
547
+ if (!c->hup) {
548
+ c->hup = 1;
549
+ c->hup_res = e->res;
550
+ }
551
+ } else if (e->kind == 4) {
552
+ if (!c->werr) c->werr = e->res < 0 ? e->res : -5;
553
+ }
554
+ if (c->lingering && nr_failed(c)) (void)nr_finish(S, (uint32_t)e->ring, (uint32_t)e->slot);
555
+ else nr_touch(&c->v);
556
+ }
557
+
558
+ /* Takes every event the set holds, and hands the set what it has queued.
559
+ Then the slots waiting on their send areas: a write-blocked one whose area
560
+ has room is ready again, and a lingering close whose bytes are gone ends. */
561
+ static void nr_pump(struct nr_set *S) {
562
+ struct nish_uring_event ev[NR_EVENTS];
563
+ for (int round = 0; round < 64; round++) {
564
+ int n = nish_uring_wait(S->u, 0, ev, NR_EVENTS);
565
+ if (n <= 0) break;
566
+ for (int i = 0; i < n; i++) nr_dispatch(S, &ev[i]);
567
+ }
568
+ int full = S->npend > 0 && nish_uring_full(S->u);
569
+ uint32_t keep = 0;
570
+ for (uint32_t i = 0; i < S->npend; i++) {
571
+ uint32_t x = S->pend[i], ring = x >> 20, slot = x & (NR_BASE - 1);
572
+ struct nr_conn *c = &S->conns[ring][slot];
573
+ int64_t q = nish_uring_queued(S->u, (int32_t)ring, (int32_t)slot);
574
+ if (!c->open) continue;
575
+ if (c->lingering) {
576
+ if ((q <= 0 || nr_failed(c) || full) && nr_finish(S, ring, slot)) continue;
577
+ } else if (c->wblocked) {
578
+ if (q >= NR_AREA) {
579
+ S->pend[keep++] = x;
580
+ continue;
581
+ }
582
+ c->wblocked = 0;
583
+ nr_touch(&c->v);
584
+ continue;
585
+ } else {
586
+ continue;
587
+ }
588
+ S->pend[keep++] = x;
589
+ }
590
+ S->npend = keep;
591
+ }
592
+
593
+ static void nr_pump_all(void) {
594
+ for (int32_t i = 0; i < nr_nsets; i++)
595
+ if (!nr_sets[i]->dead) nr_pump(nr_sets[i]);
596
+ }
597
+
598
+ static struct nr_set *nr_listener(int32_t fd) {
599
+ for (int32_t i = 0; i < nr_nsets; i++)
600
+ if (!nr_sets[i]->dead && nr_sets[i]->listener == fd) return nr_sets[i];
601
+ return NULL;
602
+ }
603
+
604
+ /* Whether `r`, from the probe or the first set's open, refuses io_uring for
605
+ the process: EPERM, ENOSYS, EINVAL or EOPNOTSUPP, the answers
606
+ nish_uring_probe keeps (NURING-20). Any other failure, such as EMFILE or
607
+ ENOMEM, may pass, and a later listen asks again. */
608
+ static int nr_refused(int32_t r) { return r == -1 || r == -38 || r == -22 || r == -95; }
609
+
610
+ /* A ring listener for `fd`: 1 when it has one, 0 when it stays on
611
+ descriptors, or -23 when the process has opened every set it may hold.
612
+ Whatever else fails, the probe or this set's open, the listener falls back
613
+ to descriptors (§5.4); a refusal before any set has opened also decides for
614
+ every later listen, and any other failure, such as -24 or -12, only for
615
+ this one (NURING-20, NURING-22). */
616
+ static int32_t nr_slots(void) {
617
+ struct rlimit rl;
618
+ if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return -1;
619
+ int32_t n = rl.rlim_cur == RLIM_INFINITY || rl.rlim_cur > NR_SLOTS_MAX ? NR_SLOTS_MAX : (int32_t)rl.rlim_cur;
620
+ return n < 1 ? 1 : n;
621
+ }
622
+
623
+ static int32_t nr_open(int32_t fd, int32_t port) {
624
+ if (nr_state < 0) return 0;
625
+ /* Under `--sandbox` every set takes the rings made before the filter, so
626
+ any failure of theirs, a refusal or not, decides for good: no ring can be
627
+ made again (WP63 S3). */
628
+ int32_t pooled = nish_uring_pooled();
629
+ if (nr_state == 0) {
630
+ int32_t p = nish_uring_probe();
631
+ if (p < 0) {
632
+ if (pooled != 0 || nr_refused(p)) nr_state = p;
633
+ return 0;
634
+ }
635
+ }
636
+ if (nr_nsets >= NR_SETS) return -23;
637
+ struct nish_uring_opts o;
638
+ memset(&o, 0, sizeof o);
639
+ o.slots = pooled > 0 ? pooled : nr_slots();
640
+ if (o.slots < 0) return 0;
641
+ o.rings_max = NR_RINGS;
642
+ struct nr_set *S = calloc(1, sizeof *S);
643
+ if (!S) return 0;
644
+ int32_t r = nish_uring_open(&S->u, fd, &o);
645
+ if (r < 0) {
646
+ free(S);
647
+ if (nr_state == 0 && (pooled != 0 || nr_refused(r))) nr_state = r;
648
+ return 0;
649
+ }
650
+ /* Only an open set decides for rings: a probe that passed before a set
651
+ that ran short leaves the state at 0 (NURING-26). */
652
+ nr_state = 1;
653
+ S->listener = fd;
654
+ S->port = port;
655
+ S->slots = o.slots;
656
+ S->gens = NR_BASE / o.slots;
657
+ S->v.loop = -1;
658
+ S->v.is_listener = 1;
659
+ nr_sets[nr_nsets++] = S;
660
+ return 1;
661
+ }
662
+
663
+ /* Closes a ring listener: the set and every slot in it, then the descriptor.
664
+ The record stays, dead, so its handles answer -9. */
665
+ static int32_t nr_close_listener(struct nr_set *S) {
666
+ nr_unregister(&S->v);
667
+ for (int ri = 0; ri < NR_RINGS; ri++) {
668
+ if (!S->conns[ri]) continue;
669
+ for (int32_t sl = 0; sl < S->slots; sl++) {
670
+ nr_unregister(&S->conns[ri][sl].v);
671
+ free(S->conns[ri][sl].rbuf);
672
+ }
673
+ free(S->conns[ri]);
674
+ S->conns[ri] = NULL;
675
+ }
676
+ nish_uring_destroy(S->u);
677
+ S->u = NULL;
678
+ free(S->fifo);
679
+ free(S->pend);
680
+ S->fifo = S->pend = NULL;
681
+ S->dead = 1;
682
+ return close(S->listener) != 0 ? nish_net_fail() : 0;
683
+ }
684
+
685
+ static int32_t nr_accept(struct nr_set *S, nish_array *peer) {
686
+ int32_t si = 0;
687
+ while (nr_sets[si] != S) si++;
688
+ nr_pump(S);
689
+ while (S->flen > 0) {
690
+ uint32_t x = S->fifo[S->fhead];
691
+ S->fhead = (S->fhead + 1) % S->fcap;
692
+ S->flen--;
693
+ uint32_t ring = x >> 20, slot = x & (NR_BASE - 1);
694
+ if (!S->conns[ring][slot].open) continue;
695
+ /* A slot has no descriptor to ask its peer of, so the address is the
696
+ 18 zeros `recvmsg` gives for one it does not know. */
697
+ memset(peer->data, 0, NISH_ADDR_BYTES);
698
+ return nr_encode(S, si, ring, slot);
699
+ }
700
+ if (S->accept_err) return S->accept_err;
701
+ if (S->accept_once) {
702
+ int32_t r = S->accept_once;
703
+ S->accept_once = 0;
704
+ return r;
705
+ }
706
+ return nish_uring_full(S->u) ? -23 : -11;
707
+ }
708
+
709
+ static int32_t nr_read(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, nish_array *buf, int64_t off,
710
+ int64_t len) {
711
+ if (c->rlen == c->roff && !c->hup) nr_pump(S);
712
+ uint32_t have = c->rlen - c->roff;
713
+ if (have > 0) {
714
+ uint32_t n = len < (int64_t)have ? (uint32_t)len : have;
715
+ memcpy(buf->data + off, c->rbuf + c->roff, n);
716
+ c->roff += n;
717
+ /* A held receive goes again once the program has read half the queue. */
718
+ if (c->rheld && c->rlen - c->roff < NR_AREA / 2 && nish_uring_hold(S->u, ring, slot, 0) == 0) c->rheld = 0;
719
+ /* A drained queue is freed and emptied, so an idle slot holds none
720
+ (NURING-38) and a receive never finds roff at rlen past 0. */
721
+ if (c->roff == c->rlen) {
722
+ free(c->rbuf);
723
+ c->rbuf = NULL;
724
+ c->rcap = c->roff = c->rlen = 0;
725
+ }
726
+ return (int32_t)n;
727
+ }
728
+ if (c->hup) return c->hup_res < 0 ? c->hup_res : 0;
729
+ /* A failed send ends the read side too, as recv's error would. */
730
+ if (c->werr) return c->werr;
731
+ return len == 0 ? 0 : -11;
732
+ }
733
+
734
+ /* The errno a failed slot answers a write with: its send's, else its
735
+ receive's, such as -104 for a peer's reset. */
736
+ static int32_t nr_fail_code(const struct nr_conn *c) {
737
+ return c->werr ? c->werr : c->hup_res;
738
+ }
739
+
740
+ /* Copies at most what the slot's send area has room for into the set's send
741
+ queue. -11 when it has none, or the connection's errno once it has failed. */
742
+ static int32_t nr_send(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, const char *p, int64_t len) {
743
+ if (nr_failed(c)) return nr_fail_code(c);
744
+ if (c->wshut) return -32;
745
+ int64_t q = nish_uring_queued(S->u, ring, slot);
746
+ if (q < 0) return -9;
747
+ if (q >= NR_AREA) {
748
+ nr_pump(S);
749
+ if (nr_failed(c)) return nr_fail_code(c);
750
+ q = nish_uring_queued(S->u, ring, slot);
751
+ }
752
+ int64_t room = NR_AREA - q;
753
+ int32_t n = room <= 0 ? 0 : (int32_t)(len < room ? len : room);
754
+ int32_t r = n > 0 ? nish_uring_send(S->u, ring, slot, (const uint8_t *)p, n) : 0;
755
+ if (r < 0) return r;
756
+ if (q + n >= NR_AREA && !c->wblocked) {
757
+ c->wblocked = 1;
758
+ if (nr_push(&S->pend, &S->npend, &S->pcap, (uint32_t)ring << 20 | (uint32_t)slot) != 0) c->wblocked = 0;
759
+ }
760
+ return len > 0 && n == 0 ? -11 : n;
761
+ }
762
+
763
+ /* Half-closes a slot (NURING-39). The SHUTDOWN goes behind the queued sends,
764
+ so the peer reads them and then the FIN. A shut read side reads as the
765
+ peer's end: what is queued, then 0, with later bytes dropped. */
766
+ static int32_t nr_shutdown(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot, int32_t how) {
767
+ if (nr_failed(c)) return -107;
768
+ int32_t r = nish_uring_shutdown(S->u, ring, slot, how);
769
+ if (r < 0) return r;
770
+ if (how != 0) c->wshut = 1;
771
+ if (how != 1 && !c->hup) {
772
+ c->hup = 1;
773
+ c->hup_res = 0;
774
+ nr_touch(&c->v);
775
+ }
776
+ return 0;
777
+ }
778
+
779
+ static int32_t nr_close(struct nr_set *S, struct nr_conn *c, int32_t ring, int32_t slot) {
780
+ nr_unregister(&c->v);
781
+ c->gen++;
782
+ c->roff = c->rlen = 0;
783
+ free(c->rbuf);
784
+ c->rbuf = NULL;
785
+ c->rcap = 0;
786
+ int64_t q = nish_uring_queued(S->u, ring, slot);
787
+ /* A peer that only stopped sending (a FIN) still takes what is queued, so
788
+ only a failure ends the slot before its bytes are sent. */
789
+ if ((q <= 0 || nr_failed(c)) && nr_finish(S, (uint32_t)ring, (uint32_t)slot)) {
790
+ nr_pump(S);
791
+ return 0;
792
+ }
793
+ c->lingering = 1;
794
+ if (!c->wblocked && nr_push(&S->pend, &S->npend, &S->pcap, (uint32_t)ring << 20 | (uint32_t)slot) != 0) {
795
+ while (!nr_finish(S, (uint32_t)ring, (uint32_t)slot)) nr_pump(S);
796
+ return 0;
797
+ }
798
+ nr_pump(S);
799
+ return 0;
800
+ }
801
+
802
+ /* The loops a virtual source has joined, so a close of any other descriptor
803
+ skips the scan below; past NR_LOOPS of them every close scans. */
804
+ #define NR_LOOPS 64
805
+ static int32_t nr_loops[NR_LOOPS];
806
+ static int32_t nr_nloops, nr_loops_spilled;
807
+
808
+ static void nr_note_loop(int32_t loop) {
809
+ for (int32_t i = 0; i < nr_nloops; i++)
810
+ if (nr_loops[i] == loop) return;
811
+ if (nr_nloops < NR_LOOPS) nr_loops[nr_nloops++] = loop;
812
+ else nr_loops_spilled = 1;
813
+ }
814
+
815
+ /* A closed descriptor may have been a loop: every virtual source in it leaves
816
+ it, as epoll drops its registrations, so a later loop that gets the same
817
+ number reports none of them. */
818
+ static void nr_forget_loop(int32_t fd) {
819
+ int32_t at = -1;
820
+ for (int32_t i = 0; i < nr_nloops; i++)
821
+ if (nr_loops[i] == fd) at = i;
822
+ if (at < 0 && !nr_loops_spilled) return;
823
+ if (at >= 0) nr_loops[at] = nr_loops[--nr_nloops];
824
+ for (int32_t si = 0; si < nr_nsets; si++) {
825
+ struct nr_set *S = nr_sets[si];
826
+ if (S->v.loop == fd) nr_unregister(&S->v);
827
+ for (int32_t ring = 0; ring < NR_RINGS; ring++) {
828
+ if (!S->conns[ring]) continue;
829
+ for (int32_t slot = 0; slot < S->slots; slot++)
830
+ if (S->conns[ring][slot].v.loop == fd) nr_unregister(&S->conns[ring][slot].v);
831
+ }
832
+ }
833
+ }
834
+
835
+ /* The virtual source `fd` names in a loop, or NULL for a descriptor. A stale
836
+ slot handle sets `*stale`. */
837
+ static struct nr_vent *nr_vent_for(int32_t fd, int *stale, struct nr_set **set) {
838
+ *stale = 0;
839
+ if (fd >= NR_BASE) {
840
+ int32_t ring, slot;
841
+ struct nr_conn *c = nr_decode(fd, set, &ring, &slot);
842
+ if (!c) *stale = 1;
843
+ return c ? &c->v : NULL;
844
+ }
845
+ struct nr_set *S = nr_listener(fd);
846
+ *set = S;
847
+ return S ? &S->v : NULL;
848
+ }
849
+
850
+ static int32_t nr_ctl(int32_t loop, int op, struct nr_vent *v, struct nr_set *S, int32_t events, int32_t token) {
851
+ if (op == EPOLL_CTL_ADD) {
852
+ if (v->loop >= 0) return -17;
853
+ /* The set's own epoll descriptor joins the loop, marked so a wake on it
854
+ is taken as the set's, never reported as a token. */
855
+ struct epoll_event e;
856
+ memset(&e, 0, sizeof e);
857
+ e.events = EPOLLIN;
858
+ int32_t si = 0;
859
+ while (nr_sets[si] != S) si++;
860
+ e.data.u64 = (uint64_t)1 << 32 | (uint32_t)si;
861
+ if (epoll_ctl(loop, EPOLL_CTL_ADD, nish_uring_fd(S->u), &e) != 0 && errno != EEXIST) return nish_net_fail();
862
+ v->loop = loop;
863
+ nr_note_loop(loop);
864
+ } else if (v->loop != loop) {
865
+ return -2;
866
+ }
867
+ if (op == EPOLL_CTL_DEL) {
868
+ nr_unregister(v);
869
+ return 0;
870
+ }
871
+ v->events = events;
872
+ v->token = token;
873
+ nr_unlist(v);
874
+ nr_touch(v);
875
+ return 0;
876
+ }
877
+
878
+ /* Writes the loop's ready virtual sources into `out`, at most `max`, in the
879
+ order they became ready. One reported and still ready goes to the tail,
880
+ so a busy source does not starve the rest. */
881
+ static int nr_collect(int32_t loop, int32_t *out, int max) {
882
+ int n = 0;
883
+ struct nr_vent *again = NULL, *again_tail = NULL;
884
+ for (struct nr_vent *v = nr_head, *next; v && n < max; v = next) {
885
+ next = v->next;
886
+ if (v->loop != loop) continue;
887
+ int32_t bits = nr_bits(v) & (v->events | 4);
888
+ nr_unlist(v);
889
+ if (bits == 0) continue;
890
+ out[2 * n] = v->token;
891
+ out[2 * n + 1] = bits;
892
+ n++;
893
+ v->next = NULL;
894
+ if (again_tail) again_tail->next = v; else again = v;
895
+ again_tail = v;
896
+ }
897
+ for (struct nr_vent *v = again, *next; v; v = next) {
898
+ next = v->next;
899
+ nr_append(v);
900
+ }
901
+ return n;
902
+ }
903
+
904
+ static int64_t nr_now_ms(void) {
905
+ struct timespec t;
906
+ clock_gettime(CLOCK_MONOTONIC, &t);
907
+ return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000;
908
+ }
909
+
910
+ /* Whose turn a full wait with room for one gives: 1 the epoll set's. */
911
+ static int nr_turn;
912
+
913
+ /* `pollWait` for a loop that may hold virtual sources: pump every set, take
914
+ what is ready, and sleep on the loop's epoll set, which holds each set's
915
+ descriptor, no longer than the sets allow (NURING-8). A wake by the bound,
916
+ or an EINTR, with nothing to report sleeps again until the caller's timeout
917
+ is spent. */
918
+ static int32_t nr_poll_wait(int32_t loop, int32_t *out, int max, int32_t timeout_ms) {
919
+ int64_t deadline = timeout_ms > 0 ? nr_now_ms() + timeout_ms : 0;
920
+ for (;;) {
921
+ nr_pump_all();
922
+ int n = nr_collect(loop, out, max);
923
+ int32_t t = 0;
924
+ if (n == 0) {
925
+ t = timeout_ms < 0 ? -1 : timeout_ms == 0 ? 0 : (int32_t)(deadline - nr_now_ms());
926
+ if (t < -1 || (timeout_ms > 0 && t < 0)) t = 0;
927
+ for (int32_t i = 0; i < nr_nsets; i++)
928
+ if (!nr_sets[i]->dead) t = nish_uring_sleep_ms(nr_sets[i]->u, t);
929
+ }
930
+ /* Ready slots that fill the batch still leave the epoll set a share of
931
+ it, half, or every other wait with room for one, so a descriptor such
932
+ as `signalFd()` is not starved by them (NURING-18). */
933
+ int room = max - n;
934
+ if (room == 0) room = max > 1 ? max / 2 : (nr_turn ^= 1);
935
+ struct epoll_event e[NR_EVENTS];
936
+ int k = room > 0 ? epoll_wait(loop, e, room, t) : 0;
937
+ int woke = 0;
938
+ /* io_uring's task work interrupts the wait with no signal behind it, so
939
+ EINTR is a wake to pump for, not an answer of 0 before the timeout. */
940
+ if (k < 0 && errno != EINTR) return nish_net_fail();
941
+ if (k < 0) {
942
+ k = 0;
943
+ woke = 1;
944
+ }
945
+ int got_fds = 0;
946
+ for (int i = 0; i < k; i++) got_fds += !(e[i].data.u64 >> 32);
947
+ /* The slots this drops stay listed and ready, and a later wait reports them. */
948
+ if (n + got_fds > max) n = max - got_fds;
949
+ for (int i = 0; i < k; i++) {
950
+ if (e[i].data.u64 >> 32) {
951
+ woke = 1;
952
+ continue;
953
+ }
954
+ uint32_t got = e[i].events;
955
+ out[2 * n] = (int32_t)e[i].data.u32;
956
+ out[2 * n + 1] = ((got & EPOLLIN) ? 1 : 0) | ((got & EPOLLOUT) ? 2 : 0) | ((got & (EPOLLHUP | EPOLLERR)) ? 4 : 0);
957
+ n++;
958
+ }
959
+ if (n > 0) return n;
960
+ if (woke) {
961
+ nr_pump_all();
962
+ n = nr_collect(loop, out, max);
963
+ if (n > 0) return n;
964
+ }
965
+ if (timeout_ms == 0 || (timeout_ms > 0 && nr_now_ms() >= deadline)) return 0;
966
+ }
967
+ }
968
+ #endif
969
+
970
+ /* `netRingState()`: whether `tcpListenFlags` flag 8 gets rings. 0 before a
971
+ listener has asked, 1 when it got them, or the negative errno that refused
972
+ them, after which every listener in the process is a descriptor one: -1
973
+ (EPERM) for a filter or `io_uring_disabled`, -38 for a kernel without
974
+ io_uring, -22 or -95 for one too old for the features the set needs. Darwin
975
+ has no io_uring, and answers -95. */
976
+ /* WP63 S3: `--sandbox`, before its filter, makes the `n` restricted rings
977
+ every ring listener of this process takes, at the slots `nr_open` would
978
+ give a set, and writes each ring's descriptor to `fds` (64 of them). With
979
+ `n` of 0 it records that no ring may be made. */
980
+ int32_t nish_net_ring_premake(int32_t n, int32_t *fds) {
981
+ #if defined(__linux__)
982
+ int32_t slots = nr_slots();
983
+ return nish_uring_premake(n, slots < 0 ? 1 : slots, fds);
984
+ #else
985
+ (void)n;
986
+ (void)fds;
987
+ return -95;
988
+ #endif
989
+ }
990
+
991
+ int32_t nish_net_ring_state(void) {
992
+ #if defined(__linux__)
993
+ return nr_state;
994
+ #else
995
+ return -95;
996
+ #endif
997
+ }
998
+
231
999
  /* `netLocalPort(fd)`: the port the socket is bound to, which is how a
232
1000
  program that listened on port 0 learns the one the kernel chose. */
233
1001
  int32_t nish_net_local_port(int32_t fd) {
1002
+ #if defined(__linux__)
1003
+ if (fd >= NR_BASE) {
1004
+ struct nr_set *S;
1005
+ int32_t ring, slot;
1006
+ return nr_decode(fd, &S, &ring, &slot) ? S->port : -9;
1007
+ }
1008
+ #endif
234
1009
  nish_sockaddr s;
235
1010
  socklen_t n = sizeof s;
236
1011
  if (getsockname(fd, &s.sa, &n) != 0) return nish_net_fail();
@@ -339,13 +1114,28 @@ int32_t nish_tcp_listen(const nish_str *host, int32_t port, int32_t backlog) {
339
1114
  new connection to the group's listener whose index is the CPU that received
340
1115
  it, the index being the order the group's sockets were bound in, and the
341
1116
  kernel falls back to its hash when there is no such listener. Linux alone
342
- has it; elsewhere flag 4 is -95. Any other bit, or 4 without 1, is -22. */
1117
+ has it; elsewhere flag 4 is -95. Flag 8 asks for io_uring slots: on Linux
1118
+ with io_uring, `tcpAccept` hands out slot handles rather than descriptors,
1119
+ so the listener holds connections past RLIMIT_NOFILE (above). Where rings
1120
+ are refused the listener is a plain one and `netRingState()` says why;
1121
+ Darwin has none, and the flag does nothing there. Any other bit, or 4
1122
+ without 1, is -22. */
343
1123
  int32_t nish_tcp_listen_flags(const nish_str *host, int32_t port, int32_t backlog, int32_t flags) {
344
- if ((uint32_t)flags > 7 || (flags & 5) == 4) return -22;
1124
+ if ((uint32_t)flags > 15 || (flags & 5) == 4) return -22;
345
1125
  #if !defined(__linux__)
346
1126
  if (flags & 4) return -95;
347
1127
  #endif
348
- return nish_net_bound(host, port, SOCK_STREAM, backlog, flags);
1128
+ int32_t fd = nish_net_bound(host, port, SOCK_STREAM, backlog, flags & 7);
1129
+ #if defined(__linux__)
1130
+ if (fd >= 0 && (flags & 8)) {
1131
+ int32_t r = nr_open(fd, nish_net_local_port(fd));
1132
+ if (r < 0) {
1133
+ close(fd);
1134
+ return r;
1135
+ }
1136
+ }
1137
+ #endif
1138
+ return fd;
349
1139
  }
350
1140
 
351
1141
  /* A socket address of `n` bytes into the caller's 18-byte form. An IPv4 peer
@@ -387,6 +1177,10 @@ static void nish_net_put(nish_array *out, const nish_sockaddr *s, socklen_t n) {
387
1177
  socket without it. */
388
1178
  int32_t nish_tcp_accept(int32_t fd, nish_array *peer) {
389
1179
  if (peer->len < NISH_ADDR_BYTES) return -22;
1180
+ #if defined(__linux__)
1181
+ struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1182
+ if (R) return nr_accept(R, peer);
1183
+ #endif
390
1184
  nish_sockaddr s;
391
1185
  socklen_t n = sizeof s;
392
1186
  #if defined(__linux__)
@@ -457,6 +1251,14 @@ int32_t nish_connect_result(int32_t fd) {
457
1251
  The compiled call has already checked `0 <= off <= off + len <=
458
1252
  buf.length`. */
459
1253
  int32_t nish_net_read(int32_t fd, nish_array *buf, int64_t off, int64_t len) {
1254
+ #if defined(__linux__)
1255
+ if (fd >= NR_BASE) {
1256
+ struct nr_set *S;
1257
+ int32_t ring, slot;
1258
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1259
+ return c ? nr_read(S, c, ring, slot, buf, off, len) : -9;
1260
+ }
1261
+ #endif
460
1262
  ssize_t n = recv(fd, buf->data + off, (size_t)len, 0);
461
1263
  return n < 0 ? nish_net_fail() : (int32_t)n;
462
1264
  }
@@ -464,6 +1266,14 @@ int32_t nish_net_read(int32_t fd, nish_array *buf, int64_t off, int64_t len) {
464
1266
  /* `netWrite(fd, buf, off, len)`: at most `len` bytes of `buf` from `off`,
465
1267
  with a peer that has gone answering -32 rather than raising SIGPIPE. */
466
1268
  int32_t nish_net_write(int32_t fd, const nish_array *buf, int64_t off, int64_t len) {
1269
+ #if defined(__linux__)
1270
+ if (fd >= NR_BASE) {
1271
+ struct nr_set *S;
1272
+ int32_t ring, slot;
1273
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1274
+ return c ? nr_send(S, c, ring, slot, buf->data + off, len) : -9;
1275
+ }
1276
+ #endif
467
1277
  ssize_t n = send(fd, buf->data + off, (size_t)len, NISH_NOSIGNAL);
468
1278
  return n < 0 ? nish_net_fail() : (int32_t)n;
469
1279
  }
@@ -479,6 +1289,20 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
479
1289
  struct iovec iov[2];
480
1290
  struct msghdr msg;
481
1291
  if (blen > (int64_t)INT32_MAX - alen) blen = (int64_t)INT32_MAX - alen;
1292
+ #if defined(__linux__)
1293
+ if (fd >= NR_BASE) {
1294
+ struct nr_set *S;
1295
+ int32_t ring, slot;
1296
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1297
+ if (!c) return -9;
1298
+ int32_t n = nr_send(S, c, ring, slot, a->data + aoff, alen);
1299
+ if (n < 0 || n < alen || blen == 0) return n;
1300
+ int32_t m = nr_send(S, c, ring, slot, b->data + boff, blen);
1301
+ /* With nothing of the first range sent, the second's answer is the
1302
+ call's, -11 included, as `sendmsg`'s would be. */
1303
+ return n == 0 ? m : m < 0 ? n : n + m;
1304
+ }
1305
+ #endif
482
1306
  iov[0].iov_base = a->data + aoff;
483
1307
  iov[0].iov_len = (size_t)alen;
484
1308
  iov[1].iov_base = b->data + boff;
@@ -494,12 +1318,30 @@ int32_t nish_net_write2(int32_t fd, const nish_array *a, int64_t aoff, int64_t a
494
1318
  are `SHUT_RD`, `SHUT_WR` and `SHUT_RDWR` on both platforms. */
495
1319
  int32_t nish_net_shutdown(int32_t fd, int32_t how) {
496
1320
  if ((uint32_t)how > 2) return -22;
1321
+ #if defined(__linux__)
1322
+ if (fd >= NR_BASE) { struct nr_set *S; int32_t ring, slot;
1323
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1324
+ return c ? nr_shutdown(S, c, ring, slot, how) : -9; }
1325
+ #endif
497
1326
  return shutdown(fd, how) != 0 ? nish_net_fail() : 0;
498
1327
  }
499
1328
 
500
1329
  /* `netClose(fd)`. The descriptor is gone whatever this answers, as `close`
501
1330
  promises on Linux, so a failure is information rather than a retry. */
502
- int32_t nish_net_close(int32_t fd) { return close(fd) != 0 ? nish_net_fail() : 0; }
1331
+ int32_t nish_net_close(int32_t fd) {
1332
+ #if defined(__linux__)
1333
+ if (fd >= NR_BASE) {
1334
+ struct nr_set *S;
1335
+ int32_t ring, slot;
1336
+ struct nr_conn *c = nr_decode(fd, &S, &ring, &slot);
1337
+ return c ? nr_close(S, c, ring, slot) : -9;
1338
+ }
1339
+ struct nr_set *R = nr_nsets > 0 ? nr_listener(fd) : NULL;
1340
+ if (R) return nr_close_listener(R);
1341
+ if (nr_nsets > 0) nr_forget_loop(fd);
1342
+ #endif
1343
+ return close(fd) != 0 ? nish_net_fail() : 0;
1344
+ }
503
1345
 
504
1346
  /* `netSetOption(fd, option, value)`: one option from a whitelist, named by
505
1347
  the runtime's own id rather than a level and an option number, so a
@@ -509,6 +1351,14 @@ int32_t nish_net_close(int32_t fd) { return close(fd) != 0 ? nish_net_fail() : 0
509
1351
  not keep quick-ACK mode: the kernel leaves it of its own accord, so a
510
1352
  server that wants it on or off sets it again after each response. */
511
1353
  int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
1354
+ #if defined(__linux__)
1355
+ /* A slot's socket is the ring's; no option reaches it from here. */
1356
+ if (fd >= NR_BASE) {
1357
+ struct nr_set *S;
1358
+ int32_t ring, slot;
1359
+ return nr_decode(fd, &S, &ring, &slot) ? -95 : -9;
1360
+ }
1361
+ #endif
512
1362
  int level = SOL_SOCKET;
513
1363
  int name;
514
1364
  switch (option) {
@@ -546,8 +1396,13 @@ int32_t nish_net_set_option(int32_t fd, int32_t option, int32_t value) {
546
1396
  options below. Linux's `TCP_INFO` answers the current `tcpi_snd_mss`; the
547
1397
  kernel copies at most the struct it has and says how much, so an answer
548
1398
  too short to reach the field is not read. `TCP_MAXSEG` is the fallback,
549
- and Darwin's only reading. */
1399
+ and Darwin's only reading. A ring slot is -1 too (NURING-39): its socket
1400
+ is a fixed file with no descriptor to ask, and io_uring's getsockopt
1401
+ command reads only SOL_SOCKET options, not TCP's. */
550
1402
  int32_t nish_net_mss(int32_t fd) {
1403
+ #if defined(__linux__)
1404
+ if (fd >= NR_BASE) return -1;
1405
+ #endif
551
1406
  struct sockaddr_storage peer;
552
1407
  socklen_t len = sizeof peer;
553
1408
  if (getpeername(fd, (struct sockaddr *)&peer, &len) != 0) return -1;
@@ -752,10 +1607,18 @@ static int nish_poll_change(int32_t loop, int32_t fd, int filter, int flags, int
752
1607
  static int32_t nish_poll_ctl(int32_t loop, int op, int32_t fd, int32_t events, int32_t token) {
753
1608
  if ((uint32_t)events > 3) return -22;
754
1609
  #if defined(__linux__)
1610
+ if (nr_nsets > 0) {
1611
+ int stale;
1612
+ struct nr_set *S;
1613
+ struct nr_vent *v = nr_vent_for(fd, &stale, &S);
1614
+ if (stale) return -9;
1615
+ if (v) return nr_ctl(loop, op, v, S, events, token);
1616
+ }
755
1617
  struct epoll_event e;
756
1618
  memset(&e, 0, sizeof e);
757
1619
  e.events = ((events & 1) ? EPOLLIN : 0) | ((events & 2) ? EPOLLOUT : 0);
758
- e.data.u32 = (uint32_t)token;
1620
+ /* The high word is 0 for a descriptor, and 1 for a ring set's own. */
1621
+ e.data.u64 = (uint32_t)token;
759
1622
  return epoll_ctl(loop, op, fd, &e) != 0 ? nish_net_fail() : 0;
760
1623
  #else
761
1624
  int r, w;
@@ -796,9 +1659,11 @@ int32_t nish_poll_remove(int32_t loop, int32_t fd) { return nish_poll_ctl(loop,
796
1659
  `timeout_ms` milliseconds pass, a negative one never, and write a token and
797
1660
  its events into `ready` per ready descriptor, `ready[2k]` and
798
1661
  `ready[2k + 1]`. The count is at most `ready.length / 2` and at most
799
- `NISH_POLL_CHUNK`. A signal that interrupts the wait answers 0, as a
800
- timeout does, rather than waiting again past the deadline; the program's
801
- loop comes round and, if it watches `signalFd()`, finds the signal ready.
1662
+ `NISH_POLL_CHUNK`. Until the process has opened a ring listener, a signal
1663
+ that interrupts the wait answers 0, as a timeout does, rather than waiting
1664
+ again past the deadline; the program's loop comes round and, if it watches
1665
+ `signalFd()`, finds the signal ready. From then on, for the rest of the
1666
+ process's life, an interrupted wait sleeps again (NURING-14).
802
1667
  A `ready` shorter than 2 is -22. */
803
1668
  int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
804
1669
  uint64_t room = ready->len / 2;
@@ -806,6 +1671,7 @@ int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms) {
806
1671
  int max = room < NISH_POLL_CHUNK ? (int)room : NISH_POLL_CHUNK;
807
1672
  int32_t *out = (int32_t *)ready->data;
808
1673
  #if defined(__linux__)
1674
+ if (nr_nsets > 0) return nr_poll_wait(loop, out, max, timeout_ms);
809
1675
  struct epoll_event e[NISH_POLL_CHUNK];
810
1676
  /* epoll waits forever for any negative timeout, as the language does. */
811
1677
  int n = epoll_wait(loop, e, max, timeout_ms);