@amritk/nish-x86_64-linux 0.44.0 → 0.46.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 (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -134,6 +134,11 @@ struct nish_uring_stats {
134
134
  int64_t accept_pauses;
135
135
  int64_t bound_violations;
136
136
  int64_t max_accepting_rings;
137
+ int64_t direct_waits; /* sleeps in io_uring_enter on the ring itself, not epoll_wait */
138
+ int64_t sends; /* SENDs written to an SQ, each a send's first or a requeue */
139
+ int64_t pool_new; /* send buffers the pools ever allocated */
140
+ int64_t pool_held; /* pool buffers that queued bytes hold now */
141
+ int64_t pool_cached; /* pool buffers on the rings' free lists now */
137
142
  };
138
143
 
139
144
  struct nish_uring_set;
@@ -145,6 +150,8 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
145
150
  int32_t nish_uring_fd(const struct nish_uring_set *s);
146
151
  int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms);
147
152
  int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nish_uring_event *ev, int32_t cap);
153
+ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms);
154
+ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take);
148
155
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n);
149
156
  int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot);
150
157
  int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot);
@@ -155,6 +162,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
155
162
 
156
163
  #if defined(__linux__)
157
164
  #include <fcntl.h>
165
+ #include <poll.h>
158
166
  #include <sys/epoll.h>
159
167
  #include <sys/eventfd.h>
160
168
  #include <sys/mman.h>
@@ -187,11 +195,13 @@ void nish_uring_destroy(struct nish_uring_set *s);
187
195
  #define UR_SETUP_DEFER_TASKRUN (1u << 13)
188
196
  #define UR_FEAT_SINGLE_MMAP (1u << 0)
189
197
  #define UR_FEAT_NODROP (1u << 1)
198
+ #define UR_FEAT_EXT_ARG (1u << 8)
190
199
  #define UR_OFF_SQ_RING 0ull
191
200
  #define UR_OFF_SQES 0x10000000ull
192
201
  #define UR_SQ_CQ_OVERFLOW (1u << 1)
193
202
  #define UR_SQ_TASKRUN (1u << 2)
194
203
  #define UR_ENTER_GETEVENTS (1u << 0)
204
+ #define UR_ENTER_EXT_ARG (1u << 3)
195
205
  #define UR_SQE_FIXED_FILE (1u << 0)
196
206
  #define UR_SQE_BUFFER_SELECT (1u << 5)
197
207
  #define UR_SQE_CQE_SKIP_SUCCESS (1u << 6)
@@ -199,6 +209,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
199
209
  #define UR_CQE_F_MORE (1u << 1)
200
210
  #define UR_CQE_BUFFER_SHIFT 16
201
211
  #define UR_OP_NOP 0
212
+ #define UR_OP_POLL_ADD 6
202
213
  #define UR_OP_ACCEPT 13
203
214
  #define UR_OP_ASYNC_CANCEL 14
204
215
  #define UR_OP_CLOSE 19
@@ -307,7 +318,7 @@ _Static_assert(sizeof(struct ur_restriction) == 16, "io_uring_restriction is 16
307
318
  /* What a completion's user_data says. A send's is its record's address, which
308
319
  * malloc aligns to 8, with UR_SEND in the low three bits; every other op's is
309
320
  * the op, the slot's generation and the slot, and none of those ops is 3. */
310
- enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
321
+ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5, UR_POLL = 6 };
311
322
 
312
323
  /* A slot's state word: open, recv armed, recv parked on -ENOBUFS, recv held
313
324
  * by its caller (`nish_uring_hold`), hung up (its recv ended for good, so it
@@ -318,6 +329,8 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
318
329
  #define ST_PARKED 4u
319
330
  #define ST_HELD 8u
320
331
  #define ST_HUP 16u
332
+ #define ST_MARKED 32u /* on its ring's `mlist`; a close keeps it (NURING-40) */
333
+ #define ST_SHUT(how) (((uint32_t)(how) + 1) << 6) /* the sides a shutdown queued (NURING-43) */
321
334
  #define ST_GEN(st) ((st) >> 8)
322
335
  #define GEN_MASK 0xffffffu
323
336
 
@@ -339,11 +352,22 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
339
352
  struct ur_send {
340
353
  struct ur_send *next, *prev_all, *next_all;
341
354
  uint32_t slot, gen;
342
- uint32_t off, len;
355
+ uint32_t off, len; /* `data` holds UR_CHUNK_DATA bytes; [off, len) are unsent */
343
356
  int in_flight, how;
344
357
  uint8_t data[];
345
358
  };
346
359
 
360
+ /* A pool buffer: a record and the bytes it carries. Every record is one,
361
+ * a shutdown's included. 16 KiB holds a response head and a typical body, so
362
+ * a head+body write fits one buffer and leaves as one SEND, and NR_AREA's
363
+ * 256 KiB is 16 of them. Bytes that outgrow it go on in further buffers
364
+ * chained behind it on the slot's queue, each its own SEND: a 64 KiB body
365
+ * behind a head is five (NURING-51). A ring keeps at most UR_SENDPOOL_MAX
366
+ * buffers free, 512 KiB, and frees what comes back past that (NURING-41). */
367
+ #define UR_CHUNK 16384
368
+ #define UR_CHUNK_DATA ((uint32_t)(UR_CHUNK - sizeof(struct ur_send)))
369
+ #define UR_SENDPOOL_MAX 32
370
+
347
371
  struct ur_ring {
348
372
  int fd, efd;
349
373
  uint32_t *sq_head, *sq_tail, *sq_mask, *sq_array, *sq_flags;
@@ -364,6 +388,17 @@ struct ur_ring {
364
388
  uint32_t *qbytes; /* per slot: bytes queued and not yet sent */
365
389
  uint32_t *parked; /* slots whose recv waits for buffers */
366
390
  uint32_t nparked;
391
+ /* IORING_FEAT_EXT_ARG: an enter can carry its own timeout, so a sleeper
392
+ * can wait on the ring itself (nish_uring_wait_direct). A restricted ring
393
+ * may not take POLL_ADD, so it watches no descriptor for its caller. */
394
+ uint8_t ext_arg, restricted;
395
+ /* Slots whose queue gained a record since the ring last submitted: its
396
+ * SEND is written then, so the writes of one pass leave as one (NURING-40).
397
+ * ST_MARKED keeps a slot on `mlist` at most once, across a close too. */
398
+ uint32_t *mlist;
399
+ uint32_t nmarked;
400
+ struct ur_send *pool; /* free send buffers, linked by `next` */
401
+ uint32_t npool;
367
402
  };
368
403
 
369
404
  struct nish_uring_set {
@@ -384,6 +419,12 @@ struct nish_uring_set {
384
419
  uint32_t lent_ring[EVENTS_MAX];
385
420
  uint16_t lent_bid[EVENTS_MAX];
386
421
  int nlent;
422
+ /* The one-shot POLL_ADD a direct wait arms on its caller's descriptor
423
+ * (NURING-45): the descriptor, whether the poll is in the kernel, the
424
+ * events it answered and not yet taken, and whether one failed, after
425
+ * which no descriptor is watched again. */
426
+ int poll_fd, poll_armed, poll_broken;
427
+ int32_t poll_revents;
387
428
  struct nish_uring_stats stats;
388
429
  };
389
430
 
@@ -624,6 +665,12 @@ static void ur_free_ring(struct ur_ring *r) {
624
665
  free(r->sq_last);
625
666
  free(r->parked);
626
667
  free(r->qbytes);
668
+ free(r->mlist);
669
+ while (r->pool) {
670
+ struct ur_send *q = r->pool;
671
+ r->pool = q->next;
672
+ free(q);
673
+ }
627
674
  free(r);
628
675
  }
629
676
 
@@ -698,6 +745,8 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
698
745
  rc = -EOPNOTSUPP;
699
746
  goto fail;
700
747
  }
748
+ r->ext_arg = (p.features & UR_FEAT_EXT_ARG) != 0;
749
+ r->restricted = restricted != 0;
701
750
  if (restricted) {
702
751
  rc = ur_ops_ok(r->fd);
703
752
  if (rc < 0) goto fail;
@@ -763,7 +812,8 @@ static int ur_make_ring(const struct ur_sizes *z, int restricted, struct ur_ring
763
812
  r->sq_last = calloc(z->slots, sizeof *r->sq_last);
764
813
  r->parked = calloc(z->slots, sizeof *r->parked);
765
814
  r->qbytes = calloc(z->slots, sizeof *r->qbytes);
766
- if (!r->st || !r->sq_first || !r->sq_last || !r->parked || !r->qbytes) {
815
+ r->mlist = calloc(z->slots, sizeof *r->mlist);
816
+ if (!r->st || !r->sq_first || !r->sq_last || !r->parked || !r->qbytes || !r->mlist) {
767
817
  rc = -ENOMEM;
768
818
  goto fail;
769
819
  }
@@ -986,6 +1036,7 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
986
1036
  e->len = (uint32_t)q->how;
987
1037
  } else {
988
1038
  e->opcode = UR_OP_SEND;
1039
+ s->stats.sends++;
989
1040
  e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
990
1041
  e->len = q->len - q->off;
991
1042
  e->op_flags = MSG_NOSIGNAL;
@@ -994,11 +1045,65 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
994
1045
  q->in_flight = 1;
995
1046
  }
996
1047
 
997
- static void ur_unlink_all(struct nish_uring_set *s, struct ur_send *q) {
1048
+ /* Note that (r, slot) has a record to submit; the ring writes its SEND when
1049
+ * it next submits. */
1050
+ static void ur_mark(struct ur_ring *r, uint32_t slot) {
1051
+ if (r->st[slot] & ST_MARKED) return;
1052
+ r->st[slot] |= ST_MARKED;
1053
+ r->mlist[r->nmarked++] = slot;
1054
+ }
1055
+
1056
+ /* Write the SEND or SHUTDOWN of every marked slot whose head is not in the
1057
+ * kernel, from the head's whole unsent length. A slot closed since it was
1058
+ * marked has no queue, and is skipped. */
1059
+ static void ur_send_marked(struct nish_uring_set *s, struct ur_ring *r) {
1060
+ uint32_t n = r->nmarked;
1061
+ r->nmarked = 0;
1062
+ for (uint32_t i = 0; i < n; i++) {
1063
+ uint32_t slot = r->mlist[i];
1064
+ r->st[slot] &= ~ST_MARKED;
1065
+ ur_send_head(r, slot, s);
1066
+ }
1067
+ }
1068
+
1069
+ /* A pool buffer, taken from the ring's free list before a new one is
1070
+ * allocated. NULL when memory is short. */
1071
+ static struct ur_send *ur_record(struct nish_uring_set *s, struct ur_ring *r) {
1072
+ struct ur_send *q;
1073
+ if (r->pool) {
1074
+ q = r->pool;
1075
+ r->pool = q->next;
1076
+ r->npool--;
1077
+ s->stats.pool_cached--;
1078
+ } else {
1079
+ q = malloc(UR_CHUNK);
1080
+ if (!q) return NULL;
1081
+ s->stats.pool_new++;
1082
+ }
1083
+ s->stats.pool_held++;
1084
+ return q;
1085
+ }
1086
+
1087
+ /* Give a record's memory back: to its ring's free list while that holds
1088
+ * fewer than UR_SENDPOOL_MAX, else to the allocator. Only a record the kernel
1089
+ * no longer holds comes here (NURING-42). */
1090
+ static void ur_release(struct nish_uring_set *s, struct ur_ring *r, struct ur_send *q) {
1091
+ s->stats.pool_held--;
1092
+ if (r->npool < UR_SENDPOOL_MAX) {
1093
+ q->next = r->pool;
1094
+ r->pool = q;
1095
+ r->npool++;
1096
+ s->stats.pool_cached++;
1097
+ return;
1098
+ }
1099
+ free(q);
1100
+ }
1101
+
1102
+ static void ur_unlink_all(struct nish_uring_set *s, struct ur_ring *r, struct ur_send *q) {
998
1103
  if (q->prev_all) q->prev_all->next_all = q->next_all;
999
1104
  else s->all = q->next_all;
1000
1105
  if (q->next_all) q->next_all->prev_all = q->prev_all;
1001
- free(q);
1106
+ ur_release(s, r, q);
1002
1107
  }
1003
1108
 
1004
1109
  /* Drop a slot's queued sends. The one the kernel holds, if any, is left on
@@ -1011,7 +1116,7 @@ static void ur_drop_sends(struct nish_uring_set *s, struct ur_ring *r, uint32_t
1011
1116
  while (q) {
1012
1117
  struct ur_send *next = q->next;
1013
1118
  q->next = NULL;
1014
- if (!q->in_flight) ur_unlink_all(s, q);
1119
+ if (!q->in_flight) ur_unlink_all(s, r, q);
1015
1120
  q = next;
1016
1121
  }
1017
1122
  }
@@ -1044,7 +1149,7 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
1044
1149
  q->in_flight = 0;
1045
1150
  uint32_t slot = q->slot;
1046
1151
  if ((r->st[slot] & ST_OPEN) == 0 || ST_GEN(r->st[slot]) != q->gen || r->sq_first[slot] != q) {
1047
- ur_unlink_all(s, q);
1152
+ ur_unlink_all(s, r, q);
1048
1153
  return 0;
1049
1154
  }
1050
1155
  if (q->how >= 0) {
@@ -1066,11 +1171,21 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
1066
1171
  } else {
1067
1172
  r->sq_first[slot] = q->next;
1068
1173
  if (!q->next) r->sq_last[slot] = NULL;
1069
- ur_unlink_all(s, q);
1174
+ ur_unlink_all(s, r, q);
1070
1175
  }
1071
1176
  ur_send_head(r, slot, s);
1072
1177
  return 0;
1073
1178
  }
1179
+ if (ud == UR_POLL) {
1180
+ /* The direct wait's poll on its caller's descriptor ended: its events
1181
+ * wait for nish_uring_poll_events. A failure other than a cancel (the
1182
+ * descriptor closed, a kernel that refuses the opcode) stops the watch
1183
+ * for good, so a poll that cannot work is not re-armed every sleep. */
1184
+ s->poll_armed = 0;
1185
+ if (res > 0) s->poll_revents |= res;
1186
+ else if (res < 0 && res != -ECANCELED) s->poll_broken = 1;
1187
+ return 0;
1188
+ }
1074
1189
  unsigned op = (unsigned)(ud & 0xff);
1075
1190
  uint32_t gen = (uint32_t)(ud >> 8) & GEN_MASK;
1076
1191
  uint32_t slot = (uint32_t)(ud >> 32);
@@ -1185,6 +1300,7 @@ static int ur_reap(struct nish_uring_set *s, int ri, struct nish_uring_event *ev
1185
1300
  static int ur_service(struct nish_uring_set *s, int ri, struct nish_uring_event *ev, int cap) {
1186
1301
  struct ur_ring *r = s->rings[ri];
1187
1302
  int n = 0;
1303
+ ur_send_marked(s, r);
1188
1304
  for (int guard = 0; guard < 1 << 16; guard++) {
1189
1305
  n += ur_reap(s, ri, ev + n, cap - n);
1190
1306
  int rc = ur_flush(r);
@@ -1245,6 +1361,7 @@ int32_t nish_uring_open(struct nish_uring_set **out, int32_t listener, const str
1245
1361
  s->buf_count = (uint32_t)d.buf_count;
1246
1362
  s->buf_size = (uint32_t)d.buf_size;
1247
1363
  s->rings = calloc((size_t)d.rings_max, sizeof *s->rings);
1364
+ s->poll_fd = -1;
1248
1365
  s->ep = epoll_create1(EPOLL_CLOEXEC);
1249
1366
  if (!s->rings || s->ep < 0) {
1250
1367
  int rc = s->ep < 0 ? -errno : -ENOMEM;
@@ -1312,6 +1429,7 @@ int32_t nish_uring_sleep_ms(const struct nish_uring_set *s, int32_t timeout_ms)
1312
1429
  for (int ri = 0; ri < s->nrings; ri++) {
1313
1430
  const struct ur_ring *r = s->rings[ri];
1314
1431
  if (ur_flagged(r) || *r->cq_head != __atomic_load_n(r->cq_tail, __ATOMIC_ACQUIRE)) return 0;
1432
+ if (r->nmarked) return 0;
1315
1433
  if (r->to_submit) room = 0;
1316
1434
  if (r->nparked) parked = 1;
1317
1435
  }
@@ -1359,8 +1477,10 @@ static void ur_catch_up(struct nish_uring_set *s) {
1359
1477
  * user space while the caller sleeps on `nish_uring_fd`. A refused flush
1360
1478
  * leaves them queued for the next wait, as ur_flush says. */
1361
1479
  static void ur_submit_queued(struct nish_uring_set *s) {
1362
- for (int ri = 0; ri < s->nrings; ri++)
1480
+ for (int ri = 0; ri < s->nrings; ri++) {
1481
+ ur_send_marked(s, s->rings[ri]);
1363
1482
  if (s->rings[ri]->to_submit) (void)ur_flush(s->rings[ri]);
1483
+ }
1364
1484
  }
1365
1485
 
1366
1486
  /* Wait for events and write at most `cap` of them, at most 256. Answers the
@@ -1401,6 +1521,80 @@ again:
1401
1521
  return n;
1402
1522
  }
1403
1523
 
1524
+ /* Sleep on the set's one ring itself rather than on `nish_uring_fd`: one
1525
+ * io_uring_enter that submits what is queued and waits for `min_complete`
1526
+ * completions (at least 1, at most the CQ's size) or until a timeout passes,
1527
+ * the timeout `nish_uring_sleep_ms` allows for `timeout_ms`, carried in
1528
+ * IORING_ENTER_EXT_ARG. With DEFER_TASKRUN the enter runs the ring's task
1529
+ * work itself, so no eventfd signal is waited for and none can be lost; the
1530
+ * NURING-8 bound is kept all the same, so a ring the kernel flags without a
1531
+ * completion is still serviced within UR_SLEEP_MAX_MS. A `watch_fd` of 0 or
1532
+ * more is watched by a one-shot POLL_ADD in the ring, armed once and taken
1533
+ * with `nish_uring_poll_events` (NURING-45), so a caller that also waits on one
1534
+ * descriptor, `signalFd()`, needs no epoll set beside the ring.
1535
+ *
1536
+ * Answers 0 after a sleep or when there is work to do now, and the caller
1537
+ * then takes events with `nish_uring_wait(s, 0, ...)`; -EOPNOTSUPP when the
1538
+ * set has more than one ring, the kernel lacks IORING_FEAT_EXT_ARG (before
1539
+ * 5.11), or `watch_fd` cannot be watched (a restricted ring, a poll that
1540
+ * failed, or one still armed on another descriptor), and the caller sleeps on
1541
+ * `nish_uring_fd` as before; or the enter's negative errno. A timeout or a
1542
+ * signal ends the sleep with 0, as epoll_wait's would. */
1543
+ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
1544
+ if (s->nrings != 1) return -EOPNOTSUPP;
1545
+ struct ur_ring *r = s->rings[0];
1546
+ if (!r->ext_arg) return -EOPNOTSUPP;
1547
+ if (watch_fd >= 0 && (r->restricted || s->poll_broken || (s->poll_armed && s->poll_fd != watch_fd)))
1548
+ return -EOPNOTSUPP;
1549
+ /* Armed even for a wait that does not sleep, so a loop whose slots are
1550
+ * always ready still hears the descriptor: the caller's pump submits it. */
1551
+ if (watch_fd >= 0 && !s->poll_armed && s->poll_revents == 0) {
1552
+ struct ur_sqe *e = ur_sqe(r);
1553
+ if (!e) return 0; /* a full SQ: the caller pumps, which flushes it */
1554
+ e->opcode = UR_OP_POLL_ADD;
1555
+ e->fd = watch_fd;
1556
+ e->op_flags = POLLIN;
1557
+ e->user_data = UR_POLL;
1558
+ s->poll_armed = 1;
1559
+ s->poll_fd = watch_fd;
1560
+ }
1561
+ /* A poll already answered, by a pump that reaped it before this wait, is
1562
+ * work to do now: sleeping would hold a pending signal for a whole enter. */
1563
+ if (watch_fd >= 0 && s->poll_revents != 0) return 0;
1564
+ /* Coalesced sends wait as marked slots: write their SQEs now, so this
1565
+ * enter submits them and sleeps rather than answering at once for the
1566
+ * caller's pump to send them. */
1567
+ ur_send_marked(s, r);
1568
+ int32_t t = nish_uring_sleep_ms(s, timeout_ms);
1569
+ if (t == 0) return 0;
1570
+ struct {
1571
+ int64_t sec, nsec;
1572
+ } ts = {t / 1000, (int64_t)(t % 1000) * 1000000};
1573
+ struct {
1574
+ uint64_t sigmask;
1575
+ uint32_t sigmask_sz, min_wait_usec;
1576
+ uint64_t ts;
1577
+ } arg = {0, 0, 0, (uint64_t)(uintptr_t)&ts};
1578
+ uint32_t want = min_complete < 1 ? 1 : (uint32_t)min_complete;
1579
+ if (want > s->cq_entries) want = s->cq_entries;
1580
+ s->stats.direct_waits++;
1581
+ int rc = ur_errno(syscall(__NR_io_uring_enter, r->fd, r->to_submit, want, UR_ENTER_GETEVENTS | UR_ENTER_EXT_ARG,
1582
+ &arg, sizeof arg));
1583
+ if (rc > 0) r->to_submit -= (uint32_t)rc > r->to_submit ? r->to_submit : (uint32_t)rc;
1584
+ if (rc >= 0 || rc == -ETIME || rc == -EINTR || rc == -EBUSY || rc == -EAGAIN) return 0;
1585
+ return rc;
1586
+ }
1587
+
1588
+ /* The events the direct wait's poll answered since they were last taken
1589
+ * (POLLIN, POLLHUP and POLLERR as poll(2) gives them), or 0; `take` clears
1590
+ * them, so the next direct wait arms the poll again. They say the descriptor
1591
+ * was ready, not that it still is: the caller asks its own epoll set. */
1592
+ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
1593
+ int32_t v = s->poll_revents;
1594
+ if (take) s->poll_revents = 0;
1595
+ return v;
1596
+ }
1597
+
1404
1598
  static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1405
1599
  if (ring < 0 || ring >= s->nrings || slot < 0 || (uint32_t)slot >= s->slots) return NULL;
1406
1600
  struct ur_ring *r = s->rings[ring];
@@ -1408,7 +1602,7 @@ static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int3
1408
1602
  }
1409
1603
 
1410
1604
  /* 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. */
1605
+ * mark the slot, so the ring submits it once it is the head. */
1412
1606
  static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot, struct ur_send *q) {
1413
1607
  q->next = NULL;
1414
1608
  q->prev_all = NULL;
@@ -1421,53 +1615,102 @@ static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slo
1421
1615
  r->sq_first[slot] = q;
1422
1616
  }
1423
1617
  r->sq_last[slot] = q;
1424
- ur_send_head(r, slot, s);
1618
+ ur_mark(r, slot);
1425
1619
  }
1426
1620
 
1427
1621
  /* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
1428
- * is free on return; a failure comes back later as a SEND_FAILED event. */
1622
+ * is free on return; a failure comes back later as a SEND_FAILED event.
1623
+ * Bytes queued before the ring next submits join the slot's last record
1624
+ * while the kernel does not hold it (NURING-40): in its room, after moving
1625
+ * its unsent bytes to the front if that makes room. What does not fit goes
1626
+ * into pool buffers chained behind it (NURING-51), every one taken before
1627
+ * any byte is copied, so -ENOMEM queues nothing. */
1429
1628
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
1430
1629
  struct ur_ring *r = ur_open_slot(s, ring, slot);
1431
1630
  if (!r) return -EBADF;
1432
1631
  if (n < 0) return -EINVAL;
1433
1632
  if (n == 0) return 0;
1434
- struct ur_send *q = malloc(sizeof *q + (size_t)n);
1435
- if (!q) return -ENOMEM;
1436
- memcpy(q->data, p, (size_t)n);
1437
- q->slot = (uint32_t)slot;
1438
- q->gen = ST_GEN(r->st[slot]);
1439
- q->off = 0;
1440
- q->len = (uint32_t)n;
1441
- q->in_flight = 0;
1442
- q->how = -1;
1633
+ uint32_t left = (uint32_t)n, room = 0;
1634
+ struct ur_send *t = r->sq_last[slot];
1635
+ if (t && !t->in_flight && t->how < 0) {
1636
+ if (UR_CHUNK_DATA - t->len < left && t->off) {
1637
+ memmove(t->data, t->data + t->off, t->len - t->off);
1638
+ t->len -= t->off;
1639
+ t->off = 0;
1640
+ }
1641
+ room = UR_CHUNK_DATA - t->len;
1642
+ }
1643
+ struct ur_send *chain = NULL;
1644
+ for (uint32_t k = left > room ? (left - room + UR_CHUNK_DATA - 1) / UR_CHUNK_DATA : 0; k; k--) {
1645
+ struct ur_send *q = ur_record(s, r);
1646
+ if (!q) {
1647
+ while (chain) {
1648
+ q = chain;
1649
+ chain = q->next;
1650
+ ur_release(s, r, q);
1651
+ }
1652
+ return -ENOMEM;
1653
+ }
1654
+ q->next = chain;
1655
+ chain = q;
1656
+ }
1657
+ if (room) {
1658
+ uint32_t c = left < room ? left : room;
1659
+ memcpy(t->data + t->len, p, c);
1660
+ t->len += c;
1661
+ p += c;
1662
+ left -= c;
1663
+ }
1664
+ while (chain) {
1665
+ struct ur_send *q = chain;
1666
+ uint32_t c = left < UR_CHUNK_DATA ? left : UR_CHUNK_DATA;
1667
+ chain = q->next;
1668
+ memcpy(q->data, p, c);
1669
+ p += c;
1670
+ left -= c;
1671
+ q->slot = (uint32_t)slot;
1672
+ q->gen = ST_GEN(r->st[slot]);
1673
+ q->off = 0;
1674
+ q->len = c;
1675
+ q->in_flight = 0;
1676
+ q->how = -1;
1677
+ ur_enqueue(s, r, (uint32_t)slot, q);
1678
+ }
1443
1679
  r->qbytes[slot] += (uint32_t)n;
1444
- ur_enqueue(s, r, (uint32_t)slot, q);
1680
+ ur_mark(r, (uint32_t)slot);
1445
1681
  return n;
1446
1682
  }
1447
1683
 
1448
1684
  /* Queue a shutdown of (ring, slot)'s `how` side (0 read, 1 write, 2 both)
1449
1685
  * behind the bytes already queued for it, so they reach the peer first. Its
1450
1686
  * 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. */
1687
+ * leaves a connection whose next read or write says so. A side already
1688
+ * queued is not queued again, so a slot holds at most two shutdowns and the
1689
+ * bytes between them at most three partly filled records (NURING-43). */
1452
1690
  int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
1453
1691
  struct ur_ring *r = ur_open_slot(s, ring, slot);
1454
1692
  if (!r) return -EBADF;
1455
1693
  if ((uint32_t)how > 2) return -EINVAL;
1456
- struct ur_send *q = malloc(sizeof *q);
1694
+ uint32_t *st = &r->st[slot];
1695
+ if (!(ST_SHUT(how) & ~*st)) return 0;
1696
+ struct ur_send *q = ur_record(s, r);
1457
1697
  if (!q) return -ENOMEM;
1458
1698
  q->slot = (uint32_t)slot;
1459
- q->gen = ST_GEN(r->st[slot]);
1699
+ q->gen = ST_GEN(*st);
1460
1700
  q->off = q->len = 0;
1461
1701
  q->in_flight = 0;
1462
1702
  q->how = how;
1463
1703
  ur_enqueue(s, r, (uint32_t)slot, q);
1704
+ *st |= ST_SHUT(how);
1464
1705
  return 0;
1465
1706
  }
1466
1707
 
1467
1708
  /* Cancel the slot's recv, close the slot, and move its generation on, so
1468
1709
  * every completion still on its way for it is stale. The freed slot goes to
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
1710
+ * the accepting job. A head send the ring has not submitted yet is written
1711
+ * first (NURING-40). -EAGAIN, with the slot still open, when the SQ has no
1712
+ * room for the entries or while any shutdown is at the slot's queue's head,
1713
+ * which it then submits if it has not: a SHUTDOWN runs
1471
1714
  * on io-wq, which looks the slot's file up only when it runs, and the cancel
1472
1715
  * below cannot match a request that has no file yet, so a slot freed before
1473
1716
  * it ran could take a new connection that the shutdown then reaches
@@ -1476,13 +1719,18 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1476
1719
  struct ur_ring *r = ur_open_slot(s, ring, slot);
1477
1720
  if (!r) return -EBADF;
1478
1721
  const struct ur_send *h = r->sq_first[slot];
1479
- if (h && h->in_flight && h->how >= 0) return -EAGAIN;
1722
+ if (h && h->how >= 0) {
1723
+ ur_send_head(r, (uint32_t)slot, s);
1724
+ return -EAGAIN;
1725
+ }
1726
+ uint32_t need = h && !h->in_flight ? 3 : 2;
1480
1727
  uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
1481
- if (r->sq_local_tail - head + 2 > r->sq_entries) {
1728
+ if (r->sq_local_tail - head + need > r->sq_entries) {
1482
1729
  (void)ur_flush(r);
1483
1730
  head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
1484
- if (r->sq_local_tail - head + 2 > r->sq_entries) return -EAGAIN;
1731
+ if (r->sq_local_tail - head + need > r->sq_entries) return -EAGAIN;
1485
1732
  }
1733
+ ur_send_head(r, (uint32_t)slot, s);
1486
1734
  uint32_t st = r->st[slot];
1487
1735
  struct ur_sqe *e = ur_sqe(r);
1488
1736
  e->opcode = UR_OP_ASYNC_CANCEL;
@@ -1496,7 +1744,7 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
1496
1744
  e->flags = UR_SQE_CQE_SKIP_SUCCESS;
1497
1745
  e->user_data = ur_tag(UR_CLOSE, st, (uint32_t)slot);
1498
1746
  ur_drop_sends(s, r, (uint32_t)slot);
1499
- r->st[slot] = ((ST_GEN(st) + 1) & GEN_MASK) << 8;
1747
+ r->st[slot] = (((ST_GEN(st) + 1) & GEN_MASK) << 8) | (st & ST_MARKED);
1500
1748
  r->live--;
1501
1749
  s->stats.live--;
1502
1750
  ur_top_up(s);
@@ -1603,6 +1851,18 @@ int32_t nish_uring_wait(struct nish_uring_set *s, int32_t timeout_ms, struct nis
1603
1851
  (void)cap;
1604
1852
  return -38;
1605
1853
  }
1854
+ int32_t nish_uring_wait_direct(struct nish_uring_set *s, int32_t watch_fd, int32_t min_complete, int32_t timeout_ms) {
1855
+ (void)s;
1856
+ (void)watch_fd;
1857
+ (void)min_complete;
1858
+ (void)timeout_ms;
1859
+ return -38;
1860
+ }
1861
+ int32_t nish_uring_poll_events(struct nish_uring_set *s, int32_t take) {
1862
+ (void)s;
1863
+ (void)take;
1864
+ return 0;
1865
+ }
1606
1866
  int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
1607
1867
  (void)s;
1608
1868
  (void)ring;