Parcourir la source

net: keep parallel downloads from overrunning a polled RX ring

The browser fetches a Vite app's modules over four TLS connections at
once.  Each advertised its full 64 KB receive queue, so a fast server
could have ~256 KB in flight while the ION's RTL8112L ring holds 64
frames (~93 KB).  Out-of-order segments are not kept, so every drop cost
a retransmission timeout and the page load stalled for minutes.

- tcp.c: the advertised window is capped by a budget shared by the open
  client connections (TCP_RX_BUDGET, 32 full segments split evenly, at
  least TCP_RX_FLOOR = 4 segments each).
- r8168.c: an RX overflow / descriptors-unavailable event only flags
  rx_restart_pending; the ring is re-armed once the frames already in it
  were read.  Restarting at once threw away up to 64 good frames.
Johannes Zellner il y a 2 jours
Parent
commit
03b547935b
4 fichiers modifiés avec 58 ajouts et 7 suppressions
  1. 13 2
      AGENTS.md
  2. 2 2
      docs/network.html
  3. 16 3
      kernel/r8168.c
  4. 27 0
      kernel/tcp.c

+ 13 - 2
AGENTS.md

@@ -867,7 +867,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   reads back set while the device still writes legacy descriptors),
   reads back set while the device still writes legacy descriptors),
   `r8168.c`
   `r8168.c`
   RTL8111/8168 family, including the RTL8111E on the Gigabyte 970A-DS3P
   RTL8111/8168 family, including the RTL8111E on the Gigabyte 970A-DS3P
-  (PCIe, polled, C+ mode, 64-entry RX ring), `ne2000.c`
+  (PCIe, polled, C+ mode, 64-entry RX ring; an RX overflow /
+  descriptors-unavailable event only flags `rx_restart_pending`, and the
+  ring is re-armed once the frames already in it were read -- restarting
+  at once discarded up to 64 good frames per overflow), `ne2000.c`
   RTL8029 (QEMU/ISA), `asix.c` AX88772 USB Ethernet (bulk over the USB
   RTL8029 (QEMU/ISA), `asix.c` AX88772 USB Ethernet (bulk over the USB
   core, so an EHCI/xHCI dongle is usable) and `rndis.c` USB RNDIS tethering
   core, so an EHCI/xHCI dongle is usable) and `rndis.c` USB RNDIS tethering
   (Android phones and QEMU `usb-net`; device-level config scan, RNDIS
   (Android phones and QEMU `usb-net`; device-level config scan, RNDIS
@@ -2076,6 +2079,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   TCP clients do NOT take that lease: `tcp.c` keeps a table of up to
   TCP clients do NOT take that lease: `tcp.c` keeps a table of up to
   `TCP_CLIENT_MAX` (twelve) connections, one per process, each with its own
   `TCP_CLIENT_MAX` (twelve) connections, one per process, each with its own
   receive queue (64 KB preferred, 32/16 KB fallback) and source port.
   receive queue (64 KB preferred, 32/16 KB fallback) and source port.
+  The advertised window is capped by a budget shared by the open client
+  connections (`TCP_RX_BUDGET`, 32 full segments split evenly, at least
+  `TCP_RX_FLOOR` = 4 segments each), so parallel downloads (the browser's
+  four module fetches) cannot overrun a small polled RX ring; without it
+  the ION's RTL8112L dropped bursts and the page load stalled on
+  retransmission timeouts.
   `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` keep
   `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` keep
   their ABI and look the connection up by calling pid; `NET_NEXT_HOP`/`NET_ARP`
   their ABI and look the connection up by calling pid; `NET_NEXT_HOP`/`NET_ARP`
   provide the route to the peer.  Every stack syscall drains the NIC through
   provide the route to the peer.  Every stack syscall drains the NIC through
@@ -2812,7 +2821,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     sequential apart from the modulepreload pass.  `crypto` entropy is the
     sequential apart from the modulepreload pass.  `crypto` entropy is the
     weak Math.random/clock mix (fine for PKCE nonces, like the TLS DRBG).
     weak Math.random/clock mix (fine for PKCE nonces, like the TLS DRBG).
     On QEMU's NE2000 the four parallel module downloads can overrun its
     On QEMU's NE2000 the four parallel module downloads can overrun its
-    small RX ring and stall TLS handshakes into a 10 s timeout; use e1000.
+    small RX ring and stall TLS handshakes into a 10 s timeout; use e1000
+    (the shared receive-window budget in `tcp.c` limits, but does not
+    rule out, such overruns).
     `DOMParser` and `document.implementation.createHTMLDocument` return an
     `DOMParser` and `document.implementation.createHTMLDocument` return an
     inert detached document (its nodes live in the page arena; markup goes
     inert detached document (its nodes live in the page arena; markup goes
     through the innerHTML fragment parser, split at `<head>`/`<body>`),
     through the innerHTML fragment parser, split at `<head>`/`<body>`),

+ 2 - 2
docs/network.html

@@ -227,7 +227,7 @@
 				</thead>
 				</thead>
 				<tbody>
 				<tbody>
 					<tr><td><span class="file">e1000e.c</span></td><td>Intel 82540EM, 82577LM, 82578, 82579</td><td>PCI MMIO</td><td>PCI INTx disabled before MMIO; PCH D0; skips the global MAC reset while the link is up; extended RX descriptors only when <code>RFCTL.EXTEN</code> reads back set; the 82540EM (<code>0x100E</code>) is pinned to legacy descriptors, because it has no RFCTL but QEMU stores the register, so EXTEN reads back set.</td></tr>
 					<tr><td><span class="file">e1000e.c</span></td><td>Intel 82540EM, 82577LM, 82578, 82579</td><td>PCI MMIO</td><td>PCI INTx disabled before MMIO; PCH D0; skips the global MAC reset while the link is up; extended RX descriptors only when <code>RFCTL.EXTEN</code> reads back set; the 82540EM (<code>0x100E</code>) is pinned to legacy descriptors, because it has no RFCTL but QEMU stores the register, so EXTEN reads back set.</td></tr>
-					<tr><td><span class="file">r8168.c</span></td><td>RTL8111/8168 family (RTL8111E on the 970A-DS3P)</td><td>PCIe MMIO</td><td>C+ mode, 64-entry RX ring, polled.</td></tr>
+					<tr><td><span class="file">r8168.c</span></td><td>RTL8111/8168 family (RTL8111E on the 970A-DS3P)</td><td>PCIe MMIO</td><td>C+ mode, 64-entry RX ring, polled; an RX overflow re-arms the ring only after the frames in it were read.</td></tr>
 					<tr><td><span class="file">ne2000.c</span></td><td>RTL8029</td><td>ISA / QEMU PCI</td><td>Simple DP8390 path used by <code>make run-net</code>.</td></tr>
 					<tr><td><span class="file">ne2000.c</span></td><td>RTL8029</td><td>ISA / QEMU PCI</td><td>Simple DP8390 path used by <code>make run-net</code>.</td></tr>
 					<tr><td><span class="file">asix.c</span></td><td>AX88772</td><td>USB bulk</td><td>Runs over the USB core; usable on EHCI/xHCI dongles.</td></tr>
 					<tr><td><span class="file">asix.c</span></td><td>AX88772</td><td>USB bulk</td><td>Runs over the USB core; usable on EHCI/xHCI dongles.</td></tr>
 					<tr><td><span class="file">rndis.c</span></td><td>Android USB tethering, QEMU <code>usb-net</code></td><td>USB control + bulk</td><td>RNDIS INIT/QUERY/packet filter; presents the phone's tethering link as an Ethernet netdev.</td></tr>
 					<tr><td><span class="file">rndis.c</span></td><td>Android USB tethering, QEMU <code>usb-net</code></td><td>USB control + bulk</td><td>RNDIS INIT/QUERY/packet filter; presents the phone's tethering link as an Ethernet netdev.</td></tr>
@@ -326,7 +326,7 @@
 		</p>
 		</p>
 		<ul>
 		<ul>
 			<li><strong>Demultiplexing.</strong> <code>net_pump()</code> feeds every received frame to <code>tcp_client_input()</code>, which matches the destination IP and port pair and updates that connection's sequence state.</li>
 			<li><strong>Demultiplexing.</strong> <code>net_pump()</code> feeds every received frame to <code>tcp_client_input()</code>, which matches the destination IP and port pair and updates that connection's sequence state.</li>
-			<li><strong>MSS / window.</strong> Our MSS is 1460 bytes and clients advertise up to a 64 KB window (the connection's receive queue; no window scaling).  At a 30 ms round trip that is ~2 MB/s per connection instead of the old 4 KB window's ~133 KB/s.</li>
+			<li><strong>MSS / window.</strong> Our MSS is 1460 bytes and clients advertise up to a 64 KB window (the connection's receive queue; no window scaling).  At a 30 ms round trip that is ~2 MB/s per connection instead of the old 4 KB window's ~133 KB/s.  The open client connections share a budget of 32 full segments (<code>TCP_RX_BUDGET</code> in <span class="file">tcp.c</span>; at least 4 segments each), so one download is still offered ~45 KB, while four parallel ones cannot overrun a 64-descriptor polled RX ring: the ION's browser used to stall on retransmission timeouts while loading a Vite app's modules.</li>
 			<li><strong>Close.</strong> <code>tcp_client_close()</code> sends FIN and frees the slot immediately instead of waiting for the peer's FIN; every send is synchronous, so no data is outstanding, and a late FIN matches no client and is dropped.  <code>uhttp</code> also skips the TLS close_notify when a redirect leaves the origin, so a redirect hop costs no extra round trips.</li>
 			<li><strong>Close.</strong> <code>tcp_client_close()</code> sends FIN and frees the slot immediately instead of waiting for the peer's FIN; every send is synchronous, so no data is outstanding, and a late FIN matches no client and is dropped.  <code>uhttp</code> also skips the TLS close_notify when a redirect leaves the origin, so a redirect hop costs no extra round trips.</li>
 			<li><strong>Retransmit.</strong> A 500 ms retry timer with a small retry count carries the connection through loss; there is no full congestion-control state machine.</li>
 			<li><strong>Retransmit.</strong> A 500 ms retry timer with a small retry count carries the connection through loss; there is no full congestion-control state machine.</li>
 			<li><strong>Listener.</strong> The server side has one active connection plus three backlog/closing slots. SYN-ACK advertises a zero window while queued; a queued connection may hold 1 KB of early request, and the window opens when <code>tcp_srv_accept()</code> runs.</li>
 			<li><strong>Listener.</strong> The server side has one active connection plus three backlog/closing slots. SYN-ACK advertises a zero window while queued; a queued connection may hold 1 KB of early request, and the window opens when <code>tcp_srv_accept()</code> runs.</li>

+ 16 - 3
kernel/r8168.c

@@ -361,6 +361,13 @@ static void rx_restart(void)
 	rx_restarts++;
 	rx_restarts++;
 }
 }
 
 
+/* Set by an RX overflow / descriptors-unavailable event: restart the RX
+ * engine, but only once the frames already in the ring were handed out.
+ * Restarting at once re-armed all 64 descriptors and threw away up to a
+ * ring of good frames per overflow, so parallel TCP downloads lost bursts,
+ * waited for the peers' retransmission timeouts and stalled. */
+static int rx_restart_pending;
+
 /* Read + acknowledge latched events; recover from an RX stall. */
 /* Read + acknowledge latched events; recover from an RX stall. */
 static void isr_poll(void)
 static void isr_poll(void)
 {
 {
@@ -372,11 +379,11 @@ static void isr_poll(void)
 	isr_last = st;
 	isr_last = st;
 	if (st & ISR_ROVW) {
 	if (st & ISR_ROVW) {
 		isr_rovw++;
 		isr_rovw++;
-		rx_restart();
+		rx_restart_pending = 1;
 	}
 	}
 	if (st & ISR_FOV) {
 	if (st & ISR_FOV) {
 		isr_fov++;
 		isr_fov++;
-		rx_restart();
+		rx_restart_pending = 1;
 	}
 	}
 	if (st & ISR_TDU) {
 	if (st & ISR_TDU) {
 		isr_tdu++;
 		isr_tdu++;
@@ -819,8 +826,14 @@ int r8168_rx(unsigned char *buf, unsigned int maxlen)
 		unsigned int status = d->opts1;
 		unsigned int status = d->opts1;
 		unsigned int len;
 		unsigned int len;
 
 
-		if (status & DescOwn)
+		if (status & DescOwn) {
+			/* Drained: a pending restart loses nothing now. */
+			if (rx_restart_pending) {
+				rx_restart_pending = 0;
+				rx_restart();
+			}
 			return -1;              /* nothing pending */
 			return -1;              /* nothing pending */
+		}
 
 
 		dma_barrier();
 		dma_barrier();
 
 

+ 27 - 0
kernel/tcp.c

@@ -30,6 +30,19 @@
 #define TCP_RXQ_PAGES   16
 #define TCP_RXQ_PAGES   16
 #define TCP_SRV_RXQ_SIZE 4096
 #define TCP_SRV_RXQ_SIZE 4096
 
 
+/* What all client connections together may have in flight towards us.
+ * The NICs are polled, and a small RX ring (64 descriptors on the
+ * RTL8111/8168) overflows when several fast senders each fill a 64 KB
+ * window at once -- the browser's four parallel module downloads on the
+ * ION.  Every overflow costs the peer a retransmission timeout, because
+ * out-of-order segments are not kept, so the loads stalled for minutes.
+ * The budget (~32 full frames) is split evenly between the open client
+ * connections, with a floor of four segments each; a single download
+ * keeps ~45 KB. */
+#define TCP_RX_BUDGET   (32u * TCP_MSS_OURS)
+#define TCP_RX_FLOOR    (4u * TCP_MSS_OURS)
+static unsigned int tcp_rx_share(void);
+
 /* Listening port for the server side (0 = not listening).  Declared here
 /* Listening port for the server side (0 = not listening).  Declared here
  * because the shared receive loop feeds non-matching frames to the listener. */
  * because the shared receive loop feeds non-matching frames to the listener. */
 static unsigned int srv_port;
 static unsigned int srv_port;
@@ -180,7 +193,10 @@ static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
 	 * is at most 64 KB. */
 	 * is at most 64 KB. */
 	unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
 	unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
 				    : rxq_space(c);
 				    : rxq_space(c);
+	unsigned int share = tcp_rx_share();
 
 
+	if (wnd > share)
+		wnd = share;
 	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
 	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
 				    with_mss, wnd);
 				    with_mss, wnd);
 }
 }
@@ -317,6 +333,17 @@ struct tcp_client {
 };
 };
 
 
 static struct tcp_client clients[TCP_CLIENT_MAX];
 static struct tcp_client clients[TCP_CLIENT_MAX];
+
+static unsigned int tcp_rx_share(void)
+{
+	unsigned int i, n = 0, share;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++)
+		if (clients[i].used)
+			n++;
+	share = TCP_RX_BUDGET / (n ? n : 1);
+	return share < TCP_RX_FLOOR ? TCP_RX_FLOOR : share;
+}
 static unsigned int next_sport = 0xC000;
 static unsigned int next_sport = 0xC000;
 
 
 static struct tcp_client *client_find(unsigned int pid)
 static struct tcp_client *client_find(unsigned int pid)