Przeglądaj źródła

Speed up browser fetches with 64 KB TCP windows

Give every client TCP connection a 64 KB receive queue allocated from the frame allocator (32/16 KB fallbacks when it is fragmented), and advertise the queue size in the SYN and ACKs.  The old fixed 4 KB window capped each connection at window/RTT (~133 KB/s at 30 ms); the larger window reaches ~2 MB/s and removes ~48 KB of kernel BSS.

Keep idle browser fetch workers and their TCP/TLS connections alive across navigations instead of killing them when a page finishes.  Only busy workers are cancelled on navigation, helpers exit after ten idle seconds, and losing window focus stops them so raw commands can acquire the NIC.  Reduce helper and browser poll intervals to 10 ms.

Report fetch timings: the browser status bar shows total, header and first-byte milliseconds, curl prints first-byte/total times, and the helper metadata carries them back to the browser.

Update AGENTS.md and the architecture pages, including the corrected gzip helper note and the smaller kernel _end.
Johannes Zellner 1 tydzień temu
rodzic
commit
e6d9b04b1c
8 zmienionych plików z 226 dodań i 57 usunięć
  1. 23 15
      AGENTS.md
  2. 1 1
      Makefile
  3. 83 6
      browser.c
  4. 8 7
      docs/network.html
  5. 1 1
      docs/userland.html
  6. 44 15
      tcp.c
  7. 4 3
      tcp.h
  8. 62 9
      ucmd_curl.c

+ 23 - 15
AGENTS.md

@@ -615,8 +615,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   the duration and is refused with `SYS_NTP_EBUSY` while another raw command
   or a TCP client is active.  `tcp.c/h` — a minimal TCP *client table*
   (connect/send/recv/close, one connection per process, in-order receive with
-  a 4 KB queue per connection so data arriving during a send is never lost,
-  one segment of retransmit; `net_pump()` dispatches frames to every client).
+  a per-connection receive queue (64 KB from the frame allocator when it can
+  find a contiguous run, 32/16 KB fallbacks) so data arriving during a send is
+  never lost, one segment of retransmit; `net_pump()` dispatches frames to
+  every client).
   `tcp_client_close()` sends FIN and frees the slot at once (all sends are
   synchronous, so nothing is unacknowledged); the peer's late FIN matches no
   client and is dropped.
@@ -1012,11 +1014,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   starts jobs, harvests completed ones from its main loop and decodes them,
   so text and scrolling stay responsive while several TCP/TLS connections are
   in flight; page/CSS/script fetches stay ordered.  Each helper keeps its own
-  TCP/TLS connection (TLS stays per-process), and workers are stopped when a
-  page finishes loading so raw commands can acquire the NIC.  The helper asks
-  for identity encoding because inflating multi-MB pages in the browser process
-  stalled the UI; if a server still sends gzip, the browser's inflate has a
-  progress hook that pumps the UI.  Page, CSS and
+  TCP/TLS connection (TLS stays per-process).  Idle helpers stay warm across
+  navigations so a second page on the same origin reuses the TCP/TLS
+  connection instead of paying a spawn and handshake; they exit after ten
+  idle seconds and the browser stops them at once when its window loses
+  focus, so raw commands can acquire the NIC.  The helper sends
+  `Accept-Encoding: gzip`; the browser inflates with a
+  progress hook that pumps the UI.  The status bar reports the page fetch
+  (headers and first-byte milliseconds).  Page, CSS and
   image buffers come from the ring-3 libc heap; the DOM uses fixed static
   arenas (6144 nodes, 512 KB attributes, 4608 rules), and page bodies are
   capped at 2 MB with CSS sheets up to 832 KB (six sheets).  Each image gets
@@ -1138,8 +1143,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   pauses while one owns it, and the reaper force-releases it if that process
   dies (`sys_net_reap`, driven by `netdev_owner()`/`netdev_release_all()`).
   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 4 KB
-  receive queue and source port.  `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` keep
+  `TCP_CLIENT_MAX` (twelve) connections, one per process, each with its own
+  receive queue (64 KB preferred, 32/16 KB fallback) and source port.
+  `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` keep
   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
   `net_pump()` (`netcore.c/h`), which dispatches each frame to the client
@@ -1768,8 +1774,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 
 21. The TCP backlog's early-request buffers cost 8 KB of kernel BSS
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
-    pack is out of the kernel now; after the WiFi/netmenu/install code `_end`
-    is ~0x8c000, ~16 KB below the `0x90000` wall, and the remaining limit is
+    pack is out of the kernel now; after the WiFi/netmenu/install code and the
+    per-connection TCP queues moved to the frame allocator, `_end` is
+    ~0x80180, ~64 KB below the `0x90000` wall, and the remaining limit is
     kernel code/buffers only.
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
@@ -1843,9 +1850,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     `[image]` until scrolled into range.  The browser keeps up to three
     `curl __serve` workers, each with its own TCP/TLS keep-alive connection
     and private `/share/bf*N` files; images are prefetched in parallel and
-    decoded as jobs complete.  Page/CSS/script fetches stay ordered, and the
-    workers are stopped when a page finishes loading so their sockets do not
-    block raw commands.  The helper deliberately requests identity encoding, so a page
+    decoded as jobs complete.  Page/CSS/script fetches stay ordered.  Idle
+    helpers stay warm across navigations and are stopped when the browser
+    loses focus (or after ten idle seconds) so their sockets do not block raw
+    commands.  The helper requests gzip; a page
     that is only small when compressed can exceed the 2 MB page/832 KB CSS
     caps.  Video/audio elements are placeholders.  Bare `&` characters are
     consumed by the entity decoder instead of looping forever.  A page with
@@ -2029,7 +2037,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     throughput is CPU-bound.  The driver was written against the Linux iwlwifi
     sources without hardware in the loop; expect bring-up debugging on the
     Chromebook.  The kernel budget is tight: WiFi added ~20 KB, `_end` is
-    ~0x8c000 (~16 KB below the `0x90000` wall).
+    ~0x80180 (~64 KB below the `0x90000` wall).
 
 36. Network menu limits (`ucmd_netmenu.c`): building the device list probes
     every driver, so opening the menu on a machine with an unconfigured USB

+ 1 - 1
Makefile

@@ -272,7 +272,7 @@ dns.o: dns.c dns.h io.h timer.h netcore.h netdev.h net.h process.h
 ntp.o: ntp.c ntp.h dns.h net.h netdev.h timer.h syscall.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-tcp.o: tcp.c tcp.h io.h timer.h netcore.h netdev.h net.h process.h
+tcp.o: tcp.c tcp.h io.h timer.h mm.h netcore.h netdev.h net.h process.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 netdev.o: netdev.c netdev.h ne2000.h r8168.h e1000e.h tcp.h

+ 83 - 6
browser.c

@@ -1779,6 +1779,9 @@ struct fetch_meta {
 	int error;
 	int trunc;
 	unsigned int len;
+	unsigned int open_ms;
+	unsigned int first_ms;
+	unsigned int total_ms;
 	char encoding[16];
 	char ctype[64];
 	char final[BR_URL_MAX];
@@ -1944,6 +1947,33 @@ static void fetch_workers_stop_all(void)
 		fetch_worker_stop(&fetch_workers[i]);
 }
 
+/* Abandon the transfers of the current page but keep idle helpers and their
+ * TCP/TLS connections warm for the next navigation.  A busy helper is killed
+ * because its response would otherwise land in a reused /share slot. */
+static void fetch_workers_cancel_busy(void)
+{
+	int i;
+
+	for (i = 0; i < FETCH_WORKERS; i++)
+		if (fetch_workers[i].busy)
+			fetch_worker_stop(&fetch_workers[i]);
+}
+
+/* The browser is no longer the active window and no fetch is running: give
+ * the NIC back so raw commands (ping/netrecv/websrv) can acquire it. */
+static void fetch_workers_stop_if_unfocused(void)
+{
+	int i;
+
+	if (ui.focused || page_loading || preload_active)
+		return;
+	for (i = 0; i < FETCH_WORKERS; i++)
+		if (fetch_workers[i].pid > 0) {
+			fetch_workers_stop_all();
+			return;
+		}
+}
+
 static int fetch_worker_start(struct fetch_worker *w, char *err,
 			      unsigned int errcap)
 {
@@ -2060,6 +2090,14 @@ static int fetch_worker_read_meta(struct fetch_worker *w, char *err,
 	meta_str(&p, w->m.ctype, sizeof(w->m.ctype));
 	meta_str(&p, w->m.final, sizeof(w->m.final));
 	w->m.len = meta_uint(&p);
+	w->m.open_ms = 0;
+	w->m.first_ms = 0;
+	w->m.total_ms = 0;
+	if (*p) {
+		w->m.open_ms = meta_uint(&p);
+		w->m.first_ms = meta_uint(&p);
+		w->m.total_ms = meta_uint(&p);
+	}
 	if (w->m.error) {
 		s_copy(err, uhttp_error_string(w->m.error), errcap);
 		return -1;
@@ -2182,13 +2220,13 @@ static int fetch_worker_wait(struct fetch_worker *w, char *err,
 
 		worker_wait_ui();
 		if (cancel_fetch || pending_action) {
-			fetch_workers_stop_all();
+			fetch_workers_cancel_busy();
 			return FETCH_ABORT;
 		}
 		rv = fetch_worker_poll(w, err, errcap);
 		if (rv != 0)
 			return rv == 1 ? 0 : -1;
-		u_sleep_ms(20);
+		u_sleep_ms(10);
 	}
 }
 
@@ -2716,7 +2754,7 @@ static void show_error(const char *title, const char *detail)
 	preload_active = 0;
 	preload_pending = 0;
 	preload_wait = 0;
-	fetch_workers_stop_all();
+	fetch_workers_cancel_busy();
 	s_copy(page_title, title, sizeof(page_title));
 	page_loading = 0;
 	set_status(title, 1, 0);
@@ -3098,7 +3136,7 @@ static void preload_step(void)
 	if (cancel_fetch || pending_action) {
 		preload_active = 0;
 		preload_pending = 0;
-		fetch_workers_stop_all();
+		fetch_workers_cancel_busy();
 		return;
 	}
 
@@ -3129,7 +3167,9 @@ static void preload_step(void)
 			preload_scroll = page_scroll.offset;
 		else
 			preload_pending = 0;
-		fetch_workers_stop_all();
+		/* Keep the now idle helpers and their connections; a later
+		 * navigation reuses them.  They exit after ten idle seconds,
+		 * and losing window focus stops them at once. */
 		set_status(post_status_text, post_status_error, post_status_ttl);
 		ngui_invalidate(&ui);
 	}
@@ -3182,6 +3222,7 @@ static void load_page(const char *url, int local, int push)
 	const char *detail = 0;
 	const char *post = pending_post_len ? pending_post : 0;
 	unsigned int post_len = pending_post_len;
+	unsigned int page_open_ms = 0, page_first_ms = 0, page_total_ms = 0;
 	int upgraded = nav_https_upgraded;
 	int rv;
 
@@ -3191,7 +3232,7 @@ static void load_page(const char *url, int local, int push)
 	cancel_fetch = 0;
 	preload_active = 0;
 	preload_pending = 0;
-	fetch_workers_stop_all();
+	fetch_workers_cancel_busy();
 
 	js_browser_stop();
 	js_last_status[0] = '\0';
@@ -3232,6 +3273,13 @@ static void load_page(const char *url, int local, int push)
 		     &truncated, post, post_len);
 	if (rv == FETCH_ABORT)
 		return;
+	/* Capture the page timing before CSS/script/image fetches overwrite the
+	 * shared fetch_meta block. */
+	if (rv == 0 && !local) {
+		page_open_ms = fetch_meta.open_ms;
+		page_first_ms = fetch_meta.first_ms;
+		page_total_ms = fetch_meta.total_ms;
+	}
 	if (rv != 0 && upgraded && !local && starts_with(url, "https://") &&
 	    https_fallback_error(fetch_last_error)) {
 		char http_url[BR_URL_MAX];
@@ -3319,6 +3367,33 @@ static void load_page(const char *url, int local, int push)
 		s_copy(post_status_text, "Page truncated by the parser",
 		       sizeof(post_status_text));
 		post_status_ttl = 6000;
+	} else if (page_total_ms) {
+		unsigned int n = 0;
+
+		if (starts_with(page_url, "https://"))
+			s_append(post_status_text, &n,
+				 "HTTPS (not verified), ",
+				 sizeof(post_status_text));
+		else
+			s_append(post_status_text, &n, "Loaded in ",
+				 sizeof(post_status_text));
+		s_append_dec(post_status_text, &n, page_total_ms,
+			     sizeof(post_status_text));
+		s_append(post_status_text, &n, " ms (headers ",
+			 sizeof(post_status_text));
+		s_append_dec(post_status_text, &n, page_open_ms,
+			     sizeof(post_status_text));
+		s_append(post_status_text, &n, " ms", sizeof(post_status_text));
+		if (page_first_ms) {
+			s_append(post_status_text, &n, ", first byte ",
+				 sizeof(post_status_text));
+			s_append_dec(post_status_text, &n, page_first_ms,
+				     sizeof(post_status_text));
+			s_append(post_status_text, &n, " ms",
+				 sizeof(post_status_text));
+		}
+		s_append(post_status_text, &n, ")", sizeof(post_status_text));
+		post_status_ttl = 6000;
 	} else if (starts_with(page_url, "https://")) {
 		s_copy(post_status_text, "HTTPS: certificate NOT verified",
 		       sizeof(post_status_text));
@@ -8316,6 +8391,7 @@ void _start(const char *arg)
 			 * a slow fetch) must be handled even when this frame
 			 * needs no redraw. */
 			if (!action) {
+				fetch_workers_stop_if_unfocused();
 				u_sleep_ms(10);
 				continue;
 			}
@@ -8359,6 +8435,7 @@ void _start(const char *arg)
 			break;
 		if (pending_action)
 			continue;
+		fetch_workers_stop_if_unfocused();
 		u_sleep_ms(10);
 	}
 	fetch_workers_stop_all();

+ 8 - 7
docs/network.html

@@ -295,14 +295,15 @@
 			<span class="file">tcp.c</span> implements a small client table, not a
 			general socket layer: each process may hold one connect/send/receive/
 			close connection, and up to <code>TCP_CLIENT_MAX</code> (twelve)
-			connections can be active.  Every connection owns a 4 KB receive
-			queue, so one socket's data can never be read or reset by another.
+			connections can be active.  Every connection owns a receive queue
+			(64 KB from the frame allocator, with 32/16 KB fallbacks), so one
+			socket's data can never be read or reset by another.
 			The window update is sent whenever the application drains the queue,
 			so a full queue cannot deadlock until the peer's zero-window probe.
 		</p>
 		<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>MSS / window.</strong> Our MSS is 1460 bytes and the advertised window is 4 KB.</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>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>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>
@@ -389,13 +390,13 @@
 			<li><strong>curl</strong> — <code>http://</code> or <code>https://</code> GET with DNS, ARP, redirects (up to five hops), chunked decoding, status/headers/body dump and a TLS warning.</li>
 			<li><strong>uhttp</strong> — shared ring-3 HTTP/1.1 client used by curl, the browser and chat: URL parsing, redirects (up to eight hops; 1 KB paths and 1.5 KB header lines, enough for OIDC URLs), chunked decoding, timeouts and TLS.  Requests and bodies of any size are split into pieces the transport accepts (1024-byte TLS records, one peer MSS for plain TCP).  Optional per-request fields set the <code>Accept</code> header, extra header lines and a cookie jar; 1xx/204/304 responses are treated as bodyless so keep-alive does not wait for an idle timeout.</li>
 			<li><strong>uhttp_cookie</strong> — the cookie jar (<span class="file">uhttp_cookie.c</span>, 24 cookies of up to 768 bytes).  Every <code>Set-Cookie</code>, including those on redirect responses, is stored; matching cookies are sent on every request and redirect hop.  Cookies are host-only (<code>Domain</code> is ignored), <code>Path</code> is a prefix match, <code>Secure</code> cookies only go over https, and <code>Max-Age&lt;=0</code> or a 1970 <code>Expires</code> deletes a cookie.  The jar outlives the connection, so a cross-origin redirect that reconnects keeps the session.</li>
-			<li><strong>browser</strong> — runs network fetches in up to three headless <code>curl __serve</code> workers, each with its own request/body/meta files under <code>/share/bf&lt;session&gt;_&lt;slot&gt;*</code>.  The session id comes from the browser pid, so multiple browser windows do not share queue files.  Images are prefetched in parallel and decoded as jobs complete, so the UI keeps drawing and scrolling.  Each worker keeps its own TCP/TLS connection.</li>
+			<li><strong>browser</strong> — runs network fetches in up to three headless <code>curl __serve</code> workers, each with its own request/body/meta files under <code>/share/bf&lt;session&gt;_&lt;slot&gt;*</code>.  The session id comes from the browser pid, so multiple browser windows do not share queue files.  Images are prefetched in parallel and decoded as jobs complete, so the UI keeps drawing and scrolling.  Each worker keeps its own TCP/TLS connection; idle helpers stay warm across navigations and exit after ten idle seconds, and losing window focus stops them so raw commands can acquire the NIC.  The status bar reports the page's total, header and first-byte times.</li>
 			<li><strong>websrv</strong> — the server side, speaking HTTP/1.1 + WebDAV over the TCP listener; see <a href="filesystems.html#webdav">Filesystems → WebDAV</a>.</li>
 		</ul>
 		<p>
-			The browser's helper requests identity encoding because inflating
-			multi-megabyte pages in the UI process stalled. If a server still
-			sends gzip, the browser's inflate hook pumps the UI while decoding.
+			The browser's helper requests gzip; inflating a multi-megabyte
+			page in the UI process uses an inflate progress hook that pumps
+			the UI while decoding.
 		</p>
 	</section>
 

+ 1 - 1
docs/userland.html

@@ -385,7 +385,7 @@
 			<li><strong>Not supported.</strong> Inline event attributes (<code>onclick=</code>), <code>MouseEvent</code>/<code>CustomEvent</code> and <code>once</code>/<code>passive</code> listener options, <code>type="module"</code>, <code>defer</code>/<code>async</code>, <code>MutationObserver</code>/<code>IntersectionObserver</code>, <code>XMLHttpRequest</code>/<code>fetch</code>, <code>DOMParser</code>, canvas/2D contexts and pseudo-class/element selectors. Basic controls render, focus, edit, toggle, select, submit GET queries and POST <code>application/x-www-form-urlencoded</code> bodies (see the compatibility ladder); <code>getComputedStyle</code> covers the engine's properties, and <code>outerHTML</code> only returns the string last assigned to <code>innerHTML</code>.</li>
 			<li><strong>CSS gaps.</strong> Multiple background layers and <code>background-attachment</code> (a non-uniform <code>background-size</code> uses the width scale), 3D/perspective transforms and transformed text/descendants (scale/rotate paint the box and inline images), pseudo-classes/elements, border-radius clipping of descendants, gradients/box-shadow/transitions and <code>color-mix</code>. Flex rows, card grids, tables with rowspan/colspan, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
-			<li><strong>Fetching.</strong> Up to three worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Page, CSS and script fetches stay ordered.</li>
+			<li><strong>Fetching.</strong> Up to three worker processes prefetch images in parallel within the viewport plus 160 px; each worker keeps its own TCP/TLS connection and its own request/body/meta files (keyed by browser pid and worker slot, so multiple windows do not collide).  Completed images are decoded as jobs finish.  Idle helpers stay warm across navigations (they exit after ten idle seconds, and losing window focus stops them), so a second page on the same origin reuses its connection.  Page, CSS and script fetches stay ordered.  The status bar reports the page fetch's header and first-byte times.</li>
 			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. A typed hostname is tried as <code>https://</code> first, skipping the common <code>http://host</code> → <code>https://host</code> hop; if the https connection is refused, reset, times out or fails the TLS handshake, the browser retries once over <code>http://</code> and replaces the history entry. Origin-level redirects (such as <code>https://host</code> → <code>https://www.host</code>) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			</ul>
 		<p>

+ 44 - 15
tcp.c

@@ -2,6 +2,7 @@
 
 #include "io.h"
 #include "timer.h"
+#include "mm.h"
 #include "netcore.h"
 #include "netdev.h"
 #include "net.h"
@@ -16,14 +17,18 @@
 #define TCP_ACK 0x10
 
 #define TCP_MSS_OURS  1460
-#define TCP_WINDOW    4096
+#define TCP_WINDOW    4096          /* fallback only; clients use rxq_mask */
 #define TCP_RETRY_MS  500
 #define TCP_RETRIES   3
 
-/* Client sockets: one per process, plus a little headroom.  4 KB per queue
- * keeps the kernel BSS cost modest (the old single queue was 8 KB). */
-#define TCP_CLIENT_MAX 12
-#define TCP_RXQ_SIZE   4096
+/* Client sockets: one per process, plus a little headroom.  Each connection
+ * gets a 64 KB receive queue from the frame allocator, which fills the
+ * unscaled 16-bit window.  Less is used when the allocator cannot find a
+ * contiguous run.  64 KB / RTT instead of 4 KB / RTT is the difference
+ * between ~130 KB/s and ~2 MB/s at a 30 ms round trip. */
+#define TCP_CLIENT_MAX  12
+#define TCP_RXQ_PAGES   16
+#define TCP_SRV_RXQ_SIZE 4096
 
 /* Listening port for the server side (0 = not listening).  Declared here
  * because the shared receive loop feeds non-matching frames to the listener. */
@@ -170,7 +175,11 @@ static int tcp_send_segment(struct tcp_conn *c, unsigned char flags,
 			    const unsigned char *payload, unsigned int len,
 			    int with_mss)
 {
-	unsigned int wnd = with_mss ? TCP_WINDOW : rxq_space(c);
+	/* The SYN advertises the receive queue size; ACKs advertise the space
+	 * currently left in it.  Both fit the 16-bit field because the queue
+	 * is at most 64 KB. */
+	unsigned int wnd = with_mss ? (c->rxq_mask ? c->rxq_mask : TCP_WINDOW)
+				    : rxq_space(c);
 
 	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
 				    with_mss, wnd);
@@ -305,7 +314,6 @@ struct tcp_client {
 	unsigned int owner;
 	int used;
 	int err;              /* last reset reason while a connect waits */
-	unsigned char rxq[TCP_RXQ_SIZE];
 };
 
 static struct tcp_client clients[TCP_CLIENT_MAX];
@@ -365,21 +373,43 @@ static unsigned int alloc_sport(const unsigned char *dst_ip,
 
 static struct tcp_client *client_alloc(unsigned int pid)
 {
-	unsigned int i;
+	static const unsigned int pages[] = {
+		TCP_RXQ_PAGES, TCP_RXQ_PAGES / 2, TCP_RXQ_PAGES / 4
+	};
+	unsigned int i, j;
 
 	for (i = 0; i < TCP_CLIENT_MAX; i++) {
-		if (clients[i].used)
+		struct tcp_client *cl = &clients[i];
+		unsigned char *q = 0;
+		unsigned int bytes = 0;
+
+		if (cl->used)
 			continue;
-		clients[i].used = 1;
-		clients[i].owner = pid;
-		clients[i].err = 0;
-		return &clients[i];
+		for (j = 0; j < sizeof(pages) / sizeof(pages[0]); j++) {
+			q = (unsigned char *)mm_alloc_pages(pages[j]);
+			if (q) {
+				bytes = pages[j] * PAGE_SIZE;
+				break;
+			}
+		}
+		if (!q)
+			return 0;
+		tcp_conn_init(&cl->c, q, bytes);
+		cl->used = 1;
+		cl->owner = pid;
+		cl->err = 0;
+		return cl;
 	}
 	return 0;
 }
 
 static void client_free(struct tcp_client *cl)
 {
+	if (cl->c.rxq) {
+		mm_free_range((unsigned int)cl->c.rxq, cl->c.rxq_mask + 1);
+		cl->c.rxq = 0;
+		cl->c.rxq_mask = 0;
+	}
 	cl->used = 0;
 	cl->owner = 0;
 	cl->err = 0;
@@ -469,7 +499,6 @@ int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
 	if (!cl)
 		return -4;
 	c = &cl->c;
-	tcp_conn_init(c, cl->rxq, sizeof(cl->rxq));
 	for (i = 0; i < 6; i++)
 		c->dst_mac[i] = mac[i];
 	for (i = 0; i < 4; i++)
@@ -765,7 +794,7 @@ static struct tcp_srv srv[TCP_SRV_SLOTS];
 static int srv_active = -1;
 /* Only one server connection is active at a time, so the listener shares one
  * receive queue instead of embedding a 4 KB buffer in every slot. */
-static unsigned char srv_rxq[TCP_RXQ_SIZE];
+static unsigned char srv_rxq[TCP_SRV_RXQ_SIZE];
 
 struct tcp_rx {
 	unsigned int seq, ack;

+ 4 - 3
tcp.h

@@ -2,9 +2,10 @@
  * single WebDAV listener.
  *
  * Client side: a small table of per-process connections.  Each connection has
- * its own in-order receive queue; every stack entry point drains the NIC
- * through net_pump(), so several processes can download at the same time.
- * There is no out-of-order reassembly, window scaling or congestion control.
+ * its own in-order receive queue (64 KB preferred, 32/16 KB when the frame
+ * allocator is fragmented); every stack entry point drains the NIC through
+ * net_pump(), so several processes can download at the same time.  There is no
+ * out-of-order reassembly, window scaling or congestion control.
  *
  * Server side: one listening port, one active connection and a small SYN
  * backlog (see the WebDAV command); the caller owns the raw NIC lease.

+ 62 - 9
ucmd_curl.c

@@ -140,8 +140,15 @@ static void fetch_fail(unsigned int status, int err, const char *ctype,
 	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
+struct fetch_times {
+	unsigned int open_ms;
+	unsigned int first_ms;
+	unsigned int total_ms;
+};
+
 static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
-		       int err, unsigned int body_len, unsigned int body_trunc)
+		       int err, unsigned int body_len, unsigned int body_trunc,
+		       const struct fetch_times *t)
 {
 	char meta[1024];
 	unsigned int n = 0;
@@ -157,6 +164,9 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
 	app_str(meta, &n, h->resp.content_type, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_str(meta, &n, h->resp.final_url, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_dec(meta, &n, body_len, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, t ? t->open_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, t ? t->first_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
+	app_dec(meta, &n, t ? t->total_ms : 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
@@ -164,14 +174,21 @@ static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
 static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 			 const char *post, unsigned int post_len,
 			 unsigned char *buf, unsigned int *outlen,
-			 unsigned int *outtrunc, int *outerr)
+			 unsigned int *outtrunc, int *outerr,
+			 struct fetch_times *times)
 {
 	unsigned int total = 0;
+	unsigned int t0 = u_ticks_ms(), t1, tfirst = 0;
 	int rv, trunc = 0, err = 0;
 
 	*outlen = 0;
 	*outtrunc = 0;
 	*outerr = 0;
+	if (times) {
+		times->open_ms = 0;
+		times->first_ms = 0;
+		times->total_ms = 0;
+	}
 	uhttp_set_method(h, post_len ? "POST" : 0, post, post_len,
 			 "application/x-www-form-urlencoded");
 	rv = uhttp_open(h, url, UHTTP_GZIP, 0);
@@ -183,6 +200,11 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 			rv = uhttp_open(h, url, UHTTP_GZIP, 0);
 		}
 	}
+	t1 = u_ticks_ms();
+	if (times) {
+		times->open_ms = t1 - t0;
+		times->total_ms = t1 - t0;
+	}
 	if (rv != 0) {
 		*outerr = rv;
 		return;
@@ -191,6 +213,8 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 		rv = uhttp_read(h, buf + total, cap - total,
 				UHTTP_IDLE_TIMEOUT_MS);
 		if (rv > 0) {
+			if (!tfirst)
+				tfirst = u_ticks_ms();
 			total += (unsigned int)rv;
 			if (total >= cap) {
 				trunc = 1;
@@ -205,12 +229,19 @@ static void helper_fetch(struct uhttp *h, const char *url, unsigned int cap,
 	*outlen = total;
 	*outtrunc = (unsigned int)trunc;
 	*outerr = err;
+	if (times) {
+		times->first_ms = tfirst ? tfirst - t0 : 0;
+		times->total_ms = u_ticks_ms() - t0;
+		if (!times->total_ms)
+			times->total_ms = 1;
+	}
 }
 
 static void fetch_once(void)
 {
 	static struct uhttp h;
 	char req[768];
+	struct fetch_times times;
 
 	fetch_paths_init(0, -1);
 	unsigned char *buf;
@@ -248,11 +279,12 @@ static void fetch_once(void)
 
 	uhttp_init(&h);
 	h.user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0 Safari/537.36";
-	helper_fetch(&h, req + i, cap, 0, 0, buf, &body_len, &body_trunc, &err);
+	helper_fetch(&h, req + i, cap, 0, 0, buf, &body_len, &body_trunc, &err,
+		     &times);
 	if (body_len)
 		u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 			   (int)body_len);
-	write_meta(0, 0, &h, err, body_len, body_trunc);
+	write_meta(0, 0, &h, err, body_len, body_trunc, &times);
 	uhttp_close(&h);
 	free(buf);
 	u_exit(err ? 1 : 0);
@@ -266,6 +298,7 @@ static void fetch_serve(const char *arg)
 {
 	static struct uhttp h;
 	char req[768];
+	struct fetch_times times;
 	unsigned char *buf, *post;
 	unsigned int last_seq = 0, idle = 0, session = 0;
 	int slot = -1;
@@ -343,8 +376,11 @@ static void fetch_serve(const char *arg)
 		}
 		if (!url || !url[0] || seq == last_seq) {
 			idle++;
-			u_sleep_ms(50);
-			if (idle >= 200)
+			/* 10 ms keeps the next navigation's first request from
+			 * waiting on this poll; 1000 ticks is still ten idle
+			 * seconds before the helper releases the NIC. */
+			u_sleep_ms(10);
+			if (idle >= 1000)
 				break;
 			continue;
 		}
@@ -368,11 +404,11 @@ static void fetch_serve(const char *arg)
 		}
 
 		helper_fetch(&h, url, cap, is_post ? (char *)post : 0, post_len,
-			     buf, &body_len, &body_trunc, &err);
+			     buf, &body_len, &body_trunc, &err, &times);
 		if (body_len)
 			u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 				   (int)body_len);
-		write_meta(seq, 1, &h, err, body_len, body_trunc);
+		write_meta(seq, 1, &h, err, body_len, body_trunc, &times);
 		app_dec(done, &dn, seq, sizeof(done));
 		u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
 
@@ -399,7 +435,7 @@ void _start(const char *arg)
 {
 	static struct uhttp h;
 	unsigned char buf[512];
-	unsigned int total = 0;
+	unsigned int total = 0, t0, t1 = 0, t2 = 0;
 	int rv;
 
 	if (arg && arg[0] == '_' && arg[1] == '_') {
@@ -424,7 +460,9 @@ void _start(const char *arg)
 	u_puts(arg);
 	u_putc('\n');
 
+	t0 = u_ticks_ms();
 	rv = uhttp_open(&h, arg, 0, &hooks);
+	t1 = u_ticks_ms();
 	if (rv != 0) {
 		u_puts("curl: ");
 		u_puts(uhttp_error_string(rv));
@@ -442,6 +480,8 @@ void _start(const char *arg)
 		if (rv > 0) {
 			int i;
 
+			if (!t2)
+				t2 = u_ticks_ms();
 			for (i = 0; i < rv; i++)
 				u_putc((char)buf[i]);
 			total += (unsigned int)rv;
@@ -459,6 +499,19 @@ void _start(const char *arg)
 	u_putdec(total);
 	u_puts(" bytes received, status ");
 	u_putdec((unsigned int)h.resp.status);
+	if (t2) {
+		u_puts(", ");
+		u_putdec(t2 - t0);
+		u_puts(" ms to first byte");
+	}
+	u_puts(", ");
+	u_putdec(u_ticks_ms() - t0);
+	u_puts(" ms total");
+	if (!t2 && t1) {
+		u_puts(" (headers ");
+		u_putdec(t1 - t0);
+		u_puts(" ms)");
+	}
 	u_putc('\n');
 	uhttp_close(&h);
 	u_exit(0);