Преглед изворни кода

Add per-process TCP clients and parallel browser fetches

Share the NIC through a kernel receive pump instead of a process-wide lease, keep packet-level commands on an exclusive raw path, and give each TCP client its own receive queue. The browser now prefetches images through three curl worker processes.
Johannes Zellner пре 1 недеља
родитељ
комит
bc31671ac2
21 измењених фајлова са 1608 додато и 582 уклоњено
  1. 60 38
      AGENTS.md
  2. 12 9
      Makefile
  3. 488 195
      browser.c
  4. 141 30
      dns.c
  5. 9 1
      dns.h
  6. 5 4
      docs/filesystems.html
  7. 40 26
      docs/network.html
  8. 7 6
      docs/userland.html
  9. 129 16
      net.c
  10. 7 2
      net.h
  11. 132 0
      netcore.c
  12. 36 0
      netcore.h
  13. 2 1
      netdev.c
  14. 9 1
      netkexec.c
  15. 4 1
      netlog.c
  16. 48 51
      sys_net.c
  17. 358 144
      tcp.c
  18. 49 22
      tcp.h
  19. 69 26
      ucmd_curl.c
  20. 3 8
      uhttp.c
  21. 0 1
      uhttp.h

+ 60 - 38
AGENTS.md

@@ -224,8 +224,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   by skipping a queued process that is still marked running on another CPU.
   The **big kernel lock** (`bkl_*`) is a sleeping recursive mutex: every
   ring-3 syscall and every kernel thread takes it, voluntary block/sleep
-  points drop it (`sched_yield`), and long serial/network waits release it via
-  `bkl_pause()`/`bkl_resume()`.  A BKL holder is not timer-preempted, so it can
+  points drop it (`sched_yield`), and long serial/raw-network waits release it
+  via `bkl_pause()`/`bkl_resume()`.  A BKL holder is not timer-preempted, so it can
   safely be in the middle of kernel data structures.  Kernel threads are pinned
   to the BSP; user processes may run on any CPU.  The idle fallback uses
   `bkl_trylock()` (never blocks); the kworker and kernel commands take the BKL
@@ -544,11 +544,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   request to UDP port 123, three one-second tries, origin-timestamp
   validation and the reply's transmit timestamp converted from the 1900
   epoch to UTC.  `SYS_NTP_SYNC` exposes it to the Date & Time app; the
-  default server is `pool.ntp.org`, the service takes the NIC receiver for
-  the duration and is refused with `SYS_NTP_EBUSY` while another command
-  owns it.  `tcp.c/h` — a minimal TCP *client* (connect/send/recv/close, in-order
-  receive with an 8 KB receive queue so data arriving during a send is never
-  lost, one segment of retransmit; the caller owns the NIC).
+  default server is `pool.ntp.org`, the service takes the raw NIC receiver for
+  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).
   `curl` — the ring-3 command: `http://` or `https://` GET with DNS, ARP,
   a dumped status/headers/body, chunked decoding (redirects are followed,
   up to 5 hops); TLS 1.3 via `tls.c/h` + `tls_sha256.c`, `tls_chacha20.c`,
@@ -855,15 +856,19 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   so later visits to a bare domain go straight to its canonical scheme/host.
   Remote stylesheets are cached under `/share/bcss-*` for the current boot, so
   a reload or a fresh browser launch does not fetch the same CSS sheets again.
-  Images are fetched one per main-loop iteration (so text and scrolling stay
-  responsive) and only when their measured position is within the viewport
-  plus 160 px; deferred images are loaded as the page scrolls.  Network work
-  runs in a headless `curl __fetch` helper process (`ucmd_curl.c`): it writes
-  the raw body and a small metadata file
-  under `/share`, while the browser keeps drawing and scrolling its own
-  window.  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
+  Images are prefetched up to three at a time and only when their measured
+  position is within the viewport plus 160 px; deferred images are loaded as
+  the page scrolls.  Network work runs in up to `FETCH_WORKERS` (three)
+  headless `curl __serve` helper processes (`ucmd_curl.c`), each with its own
+  `/share/bfreqN`, `bfbodyN`, `bfmetaN` and `bfdoneN` files.  The browser
+  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
   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
@@ -942,17 +947,28 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 - **Network service**: `sys_net.c/h` — the NIC and the stack stay in the
   kernel; ring-3 commands get `SYS_NET_INFO`/config, the DNS, DHCP and NTP
   clients and the raw frame paths (`NET_TX/RX/DECODE/REPLY/LOOPBACK/REGS/RESET`), so
-  the packet-level commands ported unchanged.  The receiver is owned by one
-  process at a time (`netdev_acquire`): the background kexec watcher pauses
-  while a command owns it, and the reaper force-releases it if that process
+  the packet-level commands ported unchanged.  Packet-level commands take the
+  receiver exclusively through `netdev_acquire`; the background kexec watcher
+  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()`).
-  The TCP client is exposed as `TCP_CONNECT/PEER_MSS/SEND/RECV/CLOSE` (one
-  connection at a time, `tcp.c` keeps the state) plus `NET_NEXT_HOP`/`NET_ARP`
-  for the route to the peer.  Calls that wait inside the stack (ARP, DNS,
-  DHCP, NTP, TCP) re-enable interrupts for the duration: the syscall stub enters
-  with them off, and the deadlines spin on `get_ticks_ms()` -- a frozen tick
-  counter would never satisfy them.  The netdev ownership already serializes
-  net users, so being preempted mid-wait is safe.
+  TCP clients do NOT take that lease: `tcp.c` keeps a table of up to
+  `TCP_CLIENT_MAX` (six) connections, one per process, each with its own 4 KB
+  receive queue 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
+  table, the WebDAV listener, a pending DNS query, the ARP cache and
+  `netkexec_capture_frame()`, and answers ARP/ICMP.  The pump runs under the
+  BKL, so the shared receive scratch and protocol state stay serialized; waits
+  yield with `process_sleep_ms()` and re-enable interrupts around driver TX
+  timeouts (the syscall stub enters with them off).  A raw acquire fails while
+  any TCP socket is open, and `SYS_TCP_CONNECT` fails while a raw owner exists;
+  DNS/ARP keep their direct-poll path when the caller is the raw owner.
+  `SYS_NET_ACQUIRE` is still the gate for `ping`/`netrecv`/`websrv`; DHCP and
+  NTP return busy while TCP clients are active.  The background netkexec
+  watcher and the netlog flush pause while a TCP socket or a stack DNS/ARP
+  operation is active (`net_stack_busy`), so they cannot steal the replies or
+  flood the small RX ring.
 
   The server side is the WebDAV command: `tcp.c` has a small listener (one
   active connection + 3 backlog/closing slots, SYN-ACK/RST, and a fast FIN that keeps
@@ -1119,8 +1135,9 @@ host tool (`netkexec_send.c`) sends the kernel then the gz rootfs as one
 stop-and-wait stream, the board acks every datagram with the next expected
 offset, verifies the 32-bit checksum, writes boot info for the staging tail
 and jumps.  `pack_size == 0` keeps the running rootfs.  ~480 KB takes a
-couple of seconds on a wired LAN.  While a net command owns the receiver
-(`netrecv`/`ping`/`netloop`) or a kexec is in progress, the auto-detect pauses.
+couple of seconds on a wired LAN.  While a raw command owns the receiver
+(`netrecv`/`ping`/`netloop`), a TCP client or stack DNS/ARP wait is active,
+or a kexec is in progress, the auto-detect pauses.
 
 The old `KXNU`/`KXEC` magics are deliberately not understood by this kernel
 (and it uses `KXN2`/`KXE2`), so a board still running a pre-rootfs build must
@@ -1154,7 +1171,9 @@ narrows it to one host (datagrams carry the broadcast MAC, so the board needs
 no ARP entry for it), `netconsole off`/`on` toggles it, and `netlog <text>`
 sends an ad-hoc line.  Nothing is sent before the NIC is up, the producer
 never blocks (a full buffer drops characters and reports it), and the flush
-pauses while a net command owns the receiver.  The producer path is
+pauses while a raw command owns the receiver or a TCP/stack network operation
+is active (its broadcast traffic would otherwise compete with the replies).
+The producer path is
 `netlog_char` (via `screen_putchar`/`serial_putc`) and is non-blocking;
 `SYS_NETLOG` is the direct-send command path — never call it from the WM or
 from any BKL holder, because a blocking NIC TX can wedge the whole board.
@@ -1497,7 +1516,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 11. The BKL serializes all kernel execution: a long FAT/disk operation, a
     non-network driver wait or a big shadow-buffer operation can stall syscalls
     on the other CPUs for its duration.  `bkl_pause()`/`bkl_resume()` is used
-    for serial/network waits, the format raw-sector loop and the installer's
+    for serial/raw-network waits, the format raw-sector loop and the installer's
     target writes; the installer keeps the BKL only for its source-read burst
     so a USB source cannot be re-enumerated mid-read.
 12. Kernel threads (kworker, kexec/payload, kernel commands) are pinned to
@@ -1533,15 +1552,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     with 409 instead of silently truncating (two long names could otherwise
     collide and overwrite each other).  Use `/share` for long names.
 19. The WebDAV server and the packet-level net commands are mutually
-    exclusive: both need the single NIC receiver (`netdev_acquire`).  Starting
-    `websrv` while `ping`/`curl`/`netrecv` owns the NIC fails; kill the other
+    exclusive: both need the raw NIC receiver (`netdev_acquire`).  Starting
+    `websrv` while `ping`/`netrecv`/`host` owns the NIC fails; TCP clients
+    (`curl`, browser workers) also fail while `websrv` owns the raw lease, and
+    `websrv` cannot start while any TCP socket is open.  Kill the other
     command first.
 20. `/share` is RAM only: a reboot/kexec starts empty, and the 16 MB cap is
     charged against the 512 MB identity-mapped window.
 
 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, so `_end` keeps ~170 KB of headroom below
+    pack is out of the kernel now, so `_end` keeps ~100 KB of headroom below
     the `0x90000` wall; the remaining limit is kernel code/buffers only.
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
@@ -1612,11 +1633,12 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     cached bitmap on every repaint.  Lazy
     image loading uses the last measured positions; adding an image can
     reflow the content above it, and images below the viewport stay as
-    `[image]` until scrolled into range.  Each network fetch starts a
-    one-shot helper process and opens a fresh TCP/TLS connection (the
-    `uhttp` keep-alive path exists but the browser does not use it across
-    helper jobs), so many tiny resources have extra process/connection
-    overhead.  The helper deliberately requests identity encoding, so a page
+    `[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
     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

+ 12 - 9
Makefile

@@ -241,28 +241,31 @@ r8168.o: r8168.c r8168.h netdev.h io.h mm.h paging.h pci.h process.h serial.h ti
 e1000e.o: e1000e.c e1000e.h io.h mm.h netdev.h paging.h pci.h process.h serial.h timer.h
 	$(CC) $(CFLAGS) -Oz -o $@ $<
 
-netkexec.o: netkexec.c netkexec.h io.h kexec.h net.h netdev.h timer.h
+netkexec.o: netkexec.c netkexec.h io.h kexec.h net.h netcore.h netdev.h tcp.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-netlog.o: netlog.c netlog.h net.h netdev.h timer.h
+netlog.o: netlog.c netlog.h net.h netcore.h netdev.h tcp.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 dhcp.o: dhcp.c dhcp.h io.h net.h netdev.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-dns.o: dns.c dns.h io.h timer.h netdev.h net.h
+dns.o: dns.c dns.h io.h timer.h netcore.h netdev.h net.h process.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 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 netdev.h net.h
+tcp.o: tcp.c tcp.h io.h timer.h netcore.h netdev.h net.h process.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-netdev.o: netdev.c netdev.h ne2000.h r8168.h e1000e.h
+netdev.o: netdev.c netdev.h ne2000.h r8168.h e1000e.h tcp.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-net.o: net.c net.h io.h netdev.h
+net.o: net.c net.h io.h netcore.h netdev.h process.h timer.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+netcore.o: netcore.c netcore.h dns.h net.h netdev.h netkexec.h spinlock.h tcp.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 blkdev.o: blkdev.c blkdev.h ata.h ahci.h disk.h sdhci.h
@@ -388,13 +391,13 @@ inflate.o: inflate.c inflate.h
 sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h cros_ec.h it87.h mm.h mouse.h power_src.h thermal.h tpec.h usbhid.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h ntp.h process.h sched.h
+sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netcore.h netdev.h netlog.h ntp.h process.h sched.h tcp.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netcore.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -1186,4 +1189,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netcore.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan

+ 488 - 195
browser.c

@@ -1759,20 +1759,15 @@ static int inflate_trunc(const unsigned char *src, unsigned int len,
 	return 0;
 }
 
-/* ---- headless fetch helper ------------------------------------------- *
+/* ---- headless fetch workers ------------------------------------------ *
  *
- * Network I/O runs in a separate ring-3 process (`curl __fetch`): it writes
- * the raw response and a small metadata file under /share.  While it works,
- * the browser keeps drawing and scrolling its own window, so a slow DNS/TLS
- * handshake or a multi-megabyte body never freezes the UI.
+ * Network I/O runs in separate ring-3 processes (`curl __serve <slot>`): each
+ * writes the raw response, a small metadata file and a done marker under
+ * /share.  Up to FETCH_WORKERS jobs run at once, so a slow DNS/TLS handshake
+ * or a multi-megabyte body never freezes the UI and images download in
+ * parallel.
  */
 
-#define FETCH_REQ  "/share/bfreq"
-#define FETCH_BODY "/share/bfbody"
-#define FETCH_META "/share/bfmeta"
-#define FETCH_DONE "/share/bfdone"
-#define FETCH_POST "/share/bfpost"
-
 struct fetch_meta {
 	int status;
 	int error;
@@ -1786,8 +1781,35 @@ struct fetch_meta {
 static void draw(unsigned int *action);
 static void handle_keys(unsigned int *action);
 static void handle_page_click(unsigned int *action);
-static int fetch_helper_tty = -2;
-static int fetch_helper_pid = -1;
+#define FETCH_WORKERS 3
+#define FETCH_PATH_MAX 32
+
+/* One long-lived curl __serve helper per worker.  Each worker has its own
+ * request/body/meta/done files under /share so several downloads can be in
+ * flight at once; the keep-alive connection stays in the helper process, so
+ * tls.c's per-process state remains valid. */
+struct fetch_worker {
+	int pid;
+	int slot;
+	int busy;
+	int ready;
+	unsigned int seq;
+	unsigned int deadline;
+	int retried;
+	char req[FETCH_PATH_MAX];
+	char body[FETCH_PATH_MAX];
+	char meta[FETCH_PATH_MAX];
+	char done[FETCH_PATH_MAX];
+	char post[FETCH_PATH_MAX];
+	/* Optional preload job attached to this worker. */
+	unsigned int node;
+	int bg;
+	char url[BR_URL_MAX];
+	struct fetch_meta m;
+};
+
+static struct fetch_worker fetch_workers[FETCH_WORKERS];
+static int fetch_tty = -2;
 static unsigned int fetch_seq;
 static struct fetch_meta fetch_meta;
 
@@ -1844,62 +1866,122 @@ static void worker_wait_ui(void)
 	ngui_end(&ui);
 }
 
-static void fetch_helper_stop(void)
+static void fetch_path(char *out, unsigned int cap, const char *base, int slot)
 {
+	unsigned int n = 0;
+
+	s_append(out, &n, base, cap);
+	s_append_dec(out, &n, (unsigned int)slot, cap);
+}
+
+static void fetch_pool_init(void)
+{
+	static int ready;
 	int i;
 
-	if (fetch_helper_pid > 0) {
-		u_kill((unsigned int)fetch_helper_pid);
-		for (i = 0; i < 40 && u_alive((unsigned int)fetch_helper_pid); i++)
+	if (ready)
+		return;
+	ready = 1;
+	for (i = 0; i < FETCH_WORKERS; i++) {
+		struct fetch_worker *w = &fetch_workers[i];
+
+		w->pid = -1;
+		w->slot = i;
+		fetch_path(w->req, sizeof(w->req), "/share/bfreq", i);
+		fetch_path(w->body, sizeof(w->body), "/share/bfbody", i);
+		fetch_path(w->meta, sizeof(w->meta), "/share/bfmeta", i);
+		fetch_path(w->done, sizeof(w->done), "/share/bfdone", i);
+		fetch_path(w->post, sizeof(w->post), "/share/bfpost", i);
+	}
+}
+
+static void fetch_worker_stop(struct fetch_worker *w)
+{
+	int i;
+
+	if (w->pid > 0) {
+		u_kill((unsigned int)w->pid);
+		for (i = 0; i < 40 && u_alive((unsigned int)w->pid); i++)
 			u_sleep_ms(5);
-		/* Let the reaper release the NIC before the next request. */
+		/* Let the reaper close the socket before we reuse the slot. */
 		u_sleep_ms(10);
 	}
-	fetch_helper_pid = -1;
+	w->pid = -1;
+	w->busy = 0;
+	w->ready = 0;
+	if (w->req[0]) {
+		u_syscall1(SYS_UNLINK, (int)w->done);
+		u_syscall1(SYS_UNLINK, (int)w->meta);
+		u_syscall1(SYS_UNLINK, (int)w->body);
+		u_syscall1(SYS_UNLINK, (int)w->post);
+		u_syscall1(SYS_UNLINK, (int)w->req);
+	}
+}
+
+static void fetch_workers_stop_all(void)
+{
+	int i;
+
+	for (i = 0; i < FETCH_WORKERS; i++)
+		fetch_worker_stop(&fetch_workers[i]);
 }
 
-static int fetch_helper_start(char *err, unsigned int errcap)
+static int fetch_worker_start(struct fetch_worker *w, char *err,
+			      unsigned int errcap)
 {
-	if (fetch_helper_pid > 0 &&
-	    u_alive((unsigned int)fetch_helper_pid))
+	char arg[24];
+	unsigned int n = 0;
+
+	if (w->pid > 0 && u_alive((unsigned int)w->pid))
 		return 0;
-	fetch_helper_pid = -1;
-	if (fetch_helper_tty == -2) {
+	w->pid = -1;
+	fetch_pool_init();
+	if (fetch_tty == -2) {
 		int t = u_tty_create();
 
-		fetch_helper_tty = t > 0 ? t : 0;
+		fetch_tty = t > 0 ? t : 0;
 	}
-	fetch_helper_pid = u_spawn("/ramfs/bin/curl", "__serve",
-				   fetch_helper_tty, "/");
-	if (fetch_helper_pid <= 0) {
-		fetch_helper_pid = -1;
+	s_append(arg, &n, "__serve ", sizeof(arg));
+	s_append_dec(arg, &n, (unsigned int)w->slot, sizeof(arg));
+	w->pid = u_spawn("/ramfs/bin/curl", arg, fetch_tty, "/");
+	if (w->pid <= 0) {
+		w->pid = -1;
 		s_copy(err, "cannot start network helper", errcap);
 		return -1;
 	}
 	return 0;
 }
 
-static int worker_fetch(const char *url, unsigned int cap,
-			const char *post, unsigned int post_len,
-			struct fetch_meta *m, char *err, unsigned int errcap)
+static struct fetch_worker *fetch_worker_alloc(void)
 {
-	static char req[768], meta[1024], done[24];
-	char *p;
-	unsigned int n = 0, deadline;
-	int rn, mseq, retried = 0;
-	struct sys_stat st;
+	int i;
+
+	fetch_pool_init();
+	for (i = 0; i < FETCH_WORKERS; i++)
+		if (!fetch_workers[i].busy)
+			return &fetch_workers[i];
+	return 0;
+}
+
+static int fetch_worker_submit(struct fetch_worker *w, const char *url,
+			       unsigned int cap, const char *post,
+			       unsigned int post_len, unsigned int node,
+			       int bg, char *err, unsigned int errcap)
+{
+	static char req[768];
+	unsigned int n = 0;
 
-	if (cancel_fetch || pending_action)
-		return FETCH_ABORT;
-	if (u_stat("/share", &st) != 0 || !st.is_dir ||
-	    u_stat("/ramfs/bin/curl", &st) != 0) {
-		s_copy(err, "network helper unavailable", errcap);
-		return -1;
-	}
 	fetch_seq++;
 	if (!fetch_seq)
 		fetch_seq = 1;
-	s_append_dec(req, &n, fetch_seq, sizeof(req));
+	w->seq = fetch_seq;
+	w->ready = 0;
+	w->retried = 0;
+	w->node = node;
+	w->bg = bg;
+	s_copy(w->url, url, sizeof(w->url));
+
+	s_append_dec(req, &n, w->seq, sizeof(req));
 	s_append(req, &n, " ", sizeof(req));
 	s_append_dec(req, &n, cap, sizeof(req));
 	s_append(req, &n, " ", sizeof(req));
@@ -1910,54 +1992,34 @@ static int worker_fetch(const char *url, unsigned int cap,
 	}
 	s_append(req, &n, url, sizeof(req));
 	s_append(req, &n, "\n", sizeof(req));
-	if (post_len && u_syscall3(SYS_WRITE_FILE, (int)FETCH_POST,
+
+	if (post_len && u_syscall3(SYS_WRITE_FILE, (int)w->post,
 				   (int)post, (int)post_len) < 0) {
 		s_copy(err, "cannot queue form body", errcap);
 		return -1;
 	}
-	u_syscall1(SYS_UNLINK, (int)FETCH_DONE);
-	u_syscall1(SYS_UNLINK, (int)FETCH_META);
-	if (u_syscall3(SYS_WRITE_FILE, (int)FETCH_REQ, (int)req, (int)n) < 0) {
+	u_syscall1(SYS_UNLINK, (int)w->done);
+	u_syscall1(SYS_UNLINK, (int)w->meta);
+	if (u_syscall3(SYS_WRITE_FILE, (int)w->req, (int)req, (int)n) < 0) {
 		s_copy(err, "cannot queue network request", errcap);
 		return -1;
 	}
-	if (fetch_helper_start(err, errcap) != 0)
+	if (fetch_worker_start(w, err, errcap) != 0)
 		return -1;
-	deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
-	for (;;) {
-		worker_wait_ui();
-		if (cancel_fetch || pending_action) {
-			fetch_helper_stop();
-			return FETCH_ABORT;
-		}
-		rn = u_syscall3(SYS_READ_FILE, (int)FETCH_DONE, (int)done,
-				(int)(sizeof(done) - 1));
-		if (rn > 0) {
-			done[rn] = '\0';
-			p = done;
-			mseq = (int)meta_uint(&p);
-			if (mseq == (int)fetch_seq)
-				break;
-		}
-		if (!u_alive((unsigned int)fetch_helper_pid)) {
-			fetch_helper_pid = -1;
-			if (!retried && fetch_helper_start(err, errcap) == 0) {
-				retried = 1;
-				continue;
-			}
-			s_copy(err, "network helper stopped", errcap);
-			return -1;
-		}
-		if ((int)(u_ticks_ms() - deadline) >= 0) {
-			fetch_helper_stop();
-			s_copy(err, "network request timed out", errcap);
-			return -1;
-		}
-		u_sleep_ms(20);
-	}
-	if (cancel_fetch || pending_action)
-		return FETCH_ABORT;
-	rn = u_syscall3(SYS_READ_FILE, (int)FETCH_META, (int)meta,
+	w->busy = 1;
+	w->deadline = u_ticks_ms() + UHTTP_TOTAL_TIMEOUT_MS + 5000;
+	return 0;
+}
+
+/* Read and parse w->meta after w->done matched.  Returns 0 when ready. */
+static int fetch_worker_read_meta(struct fetch_worker *w, char *err,
+				  unsigned int errcap)
+{
+	static char meta[1024];
+	char *p;
+	int rn, mseq;
+
+	rn = u_syscall3(SYS_READ_FILE, (int)w->meta, (int)meta,
 			(int)(sizeof(meta) - 1));
 	if (rn <= 0) {
 		s_copy(err, "network helper failed", errcap);
@@ -1966,54 +2028,98 @@ static int worker_fetch(const char *url, unsigned int cap,
 	meta[rn] = '\0';
 	p = meta;
 	mseq = (int)meta_uint(&p);
-	if (mseq != (int)fetch_seq) {
+	if (mseq != (int)w->seq) {
 		s_copy(err, "stale network response", errcap);
 		return -1;
 	}
-	m->status = (int)meta_uint(&p);
-	m->error = (int)meta_uint(&p);
-	m->trunc = (int)meta_uint(&p);
-	meta_str(&p, m->encoding, sizeof(m->encoding));
-	meta_str(&p, m->ctype, sizeof(m->ctype));
-	meta_str(&p, m->final, sizeof(m->final));
-	m->len = meta_uint(&p);
-	if (m->error) {
-		s_copy(err, uhttp_error_string(m->error), errcap);
+	w->m.status = (int)meta_uint(&p);
+	w->m.error = (int)meta_uint(&p);
+	w->m.trunc = (int)meta_uint(&p);
+	meta_str(&p, w->m.encoding, sizeof(w->m.encoding));
+	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);
+	if (w->m.error) {
+		s_copy(err, uhttp_error_string(w->m.error), errcap);
 		return -1;
 	}
+	w->ready = 1;
 	return 0;
 }
 
-static int fetch_remote(const char *url, unsigned int cap,
-			const char *post, unsigned int post_len,
-			unsigned char *plain, unsigned int plaincap,
-			unsigned char *comp, unsigned int compcap,
-			struct fetch_meta *m, int *truncated,
-			char *err, unsigned int errcap)
+/* Non-blocking poll: 1 ready, 0 still running, -1 failed. */
+static int fetch_worker_poll(struct fetch_worker *w, char *err,
+			     unsigned int errcap)
 {
-	int rv, zr, was_trunc;
+	static char done[24];
+	char *p;
+	int rn, mseq;
 
-	rv = worker_fetch(url, cap, post, post_len, m, err, errcap);
-	if (rv == FETCH_ABORT)
-		return FETCH_ABORT;
-	if (rv != 0)
+	if (!w->busy)
+		return -1;
+	if (w->ready)
+		return 1;
+
+	rn = u_syscall3(SYS_READ_FILE, (int)w->done, (int)done,
+			(int)(sizeof(done) - 1));
+	if (rn > 0) {
+		done[rn] = '\0';
+		p = done;
+		mseq = (int)meta_uint(&p);
+		if (mseq == (int)w->seq) {
+			if (fetch_worker_read_meta(w, err, errcap) != 0) {
+				fetch_worker_stop(w);
+				return -1;
+			}
+			return 1;
+		}
+	}
+	if (!u_alive((unsigned int)w->pid)) {
+		w->pid = -1;
+		if (!w->retried && fetch_worker_start(w, err, errcap) == 0) {
+			w->retried = 1;
+			return 0;
+		}
+		s_copy(err, "network helper stopped", errcap);
+		fetch_worker_stop(w);
+		return -1;
+	}
+	if ((int)(u_ticks_ms() - w->deadline) >= 0) {
+		s_copy(err, "network request timed out", errcap);
+		fetch_worker_stop(w);
 		return -1;
-	if (contains_ci(m->encoding, "gzip") ||
-	    contains_ci(m->encoding, "deflate")) {
+	}
+	return 0;
+}
+
+/* Copy the completed body, inflating it if the server ignored our identity
+ * request.  Mirrors the old fetch_remote body path. */
+static int fetch_worker_read_body(struct fetch_worker *w,
+				  unsigned char *plain,
+				  unsigned int plaincap,
+				  unsigned char *comp,
+				  unsigned int compcap,
+				  int *truncated, char *err,
+				  unsigned int errcap)
+{
+	int rv, zr, was_trunc;
+
+	if (contains_ci(w->m.encoding, "gzip") ||
+	    contains_ci(w->m.encoding, "deflate")) {
 		unsigned int comp_len = 0, plen = 0;
 
-		rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)comp,
+		rv = u_syscall3(SYS_READ_FILE, (int)w->body, (int)comp,
 				(int)compcap);
 		if (rv < 0) {
 			s_copy(err, "empty response", errcap);
 			return -1;
 		}
 		comp_len = (unsigned int)rv;
-		if (comp_len < m->len) {
+		if (comp_len < w->m.len) {
 			s_copy(err, "compressed response is too large", errcap);
 			return -1;
 		}
-		if (contains_ci(m->encoding, "deflate"))
+		if (contains_ci(w->m.encoding, "deflate"))
 			zr = inflate_trunc(comp, comp_len, plain, plaincap,
 					   &plen, 0, &was_trunc);
 		else
@@ -2023,18 +2129,83 @@ static int fetch_remote(const char *url, unsigned int cap,
 			s_copy(err, "compressed response is corrupt", errcap);
 			return -1;
 		}
-		*truncated = m->trunc || was_trunc;
-		return (cancel_fetch || pending_action) ? FETCH_ABORT :
-			(int)plen;
+		*truncated = w->m.trunc || was_trunc;
+		return (int)plen;
 	}
-	rv = u_syscall3(SYS_READ_FILE, (int)FETCH_BODY, (int)plain,
-			(int)plaincap);
+	rv = u_syscall3(SYS_READ_FILE, (int)w->body, (int)plain, (int)plaincap);
 	if (rv < 0) {
 		s_copy(err, "empty response", errcap);
 		return -1;
 	}
-	*truncated = m->trunc || ((unsigned int)rv < m->len);
-	return (cancel_fetch || pending_action) ? FETCH_ABORT : rv;
+	*truncated = w->m.trunc || ((unsigned int)rv < w->m.len);
+	return rv;
+}
+
+static void fetch_worker_release(struct fetch_worker *w)
+{
+	u_syscall1(SYS_UNLINK, (int)w->done);
+	u_syscall1(SYS_UNLINK, (int)w->meta);
+	u_syscall1(SYS_UNLINK, (int)w->body);
+	u_syscall1(SYS_UNLINK, (int)w->post);
+	u_syscall1(SYS_UNLINK, (int)w->req);
+	w->busy = 0;
+	w->ready = 0;
+}
+
+/* Blocking wait used by page/CSS/script fetches; keeps the UI alive. */
+static int fetch_worker_wait(struct fetch_worker *w, char *err,
+			     unsigned int errcap)
+{
+	for (;;) {
+		int rv;
+
+		worker_wait_ui();
+		if (cancel_fetch || pending_action) {
+			fetch_workers_stop_all();
+			return FETCH_ABORT;
+		}
+		rv = fetch_worker_poll(w, err, errcap);
+		if (rv != 0)
+			return rv == 1 ? 0 : -1;
+		u_sleep_ms(20);
+	}
+}
+
+static int fetch_remote(const char *url, unsigned int cap,
+			const char *post, unsigned int post_len,
+			unsigned char *plain, unsigned int plaincap,
+			unsigned char *comp, unsigned int compcap,
+			struct fetch_meta *m, int *truncated,
+			char *err, unsigned int errcap)
+{
+	struct fetch_worker *w = fetch_worker_alloc();
+	int rv;
+
+	if (!w) {
+		s_copy(err, "network busy", errcap);
+		return -1;
+	}
+	if (fetch_worker_submit(w, url, cap, post, post_len, 0, 0,
+				err, errcap) != 0) {
+		fetch_worker_release(w);
+		return -1;
+	}
+	rv = fetch_worker_wait(w, err, errcap);
+	if (rv == FETCH_ABORT) {
+		fetch_worker_release(w);
+		return FETCH_ABORT;
+	}
+	if (rv != 0) {
+		fetch_worker_release(w);
+		return -1;
+	}
+	*m = w->m;
+	rv = fetch_worker_read_body(w, plain, plaincap, comp, compcap,
+				    truncated, err, errcap);
+	fetch_worker_release(w);
+	if (rv >= 0 && (cancel_fetch || pending_action))
+		return FETCH_ABORT;
+	return rv;
 }
 
 /* ---- HTTP / file fetch ---- */
@@ -2522,6 +2693,7 @@ static void show_error(const char *title, const char *detail)
 	preload_active = 0;
 	preload_pending = 0;
 	preload_wait = 0;
+	fetch_workers_stop_all();
 	s_copy(page_title, title, sizeof(page_title));
 	page_loading = 0;
 	set_status(title, 1, 0);
@@ -2663,29 +2835,107 @@ static void style_for_node(unsigned int node, struct br_style *out)
 	}
 }
 
-/* Load at most one image per call.  Images below the current viewport are
- * left pending until the page is scrolled; this keeps a long article with
- * many 4K screenshots from stalling on content the user cannot see yet. */
-static void preload_step(void)
+/* ---- parallel image preloading ---- *
+ *
+ * Up to FETCH_WORKERS image downloads run in separate curl helpers at once.
+ * preload_step() only starts jobs and harvests completed ones; the browser
+ * main loop keeps drawing between calls, instead of blocking on one fetch.
+ */
+
+static int preload_active_jobs(void)
 {
-	unsigned int batch = 0;
+	int i, n = 0;
+
+	for (i = 0; i < FETCH_WORKERS; i++)
+		if (fetch_workers[i].busy)
+			n++;
+	return n;
+}
+
+static void preload_job_error(struct fetch_worker *w)
+{
+	if (w->node < doc.nnode) {
+		if (w->bg)
+			doc.nodes[w->node].bg_img_index = -2;
+		else
+			doc.nodes[w->node].img_index = -2;
+		ngui_invalidate(&ui);
+	}
+}
+
+static void preload_decode(unsigned int node, int bg,
+			   const unsigned char *data, unsigned int len)
+{
+	if (image_count >= BR_MAX_IMAGES) {
+		if (bg)
+			doc.nodes[node].bg_img_index = -2;
+		else
+			doc.nodes[node].img_index = -2;
+		return;
+	}
+	if (decode_image_data(data, len, &images[image_count]) == 0) {
+		if (bg)
+			doc.nodes[node].bg_img_index = (short)image_count;
+		else
+			doc.nodes[node].img_index = (short)image_count;
+		image_count++;
+		preload_loaded++;
+		layout_valid = 0;
+	} else {
+		if (bg)
+			doc.nodes[node].bg_img_index = -2;
+		else
+			doc.nodes[node].img_index = -2;
+	}
+	ngui_invalidate(&ui);
+}
+
+/* Harvest one completed worker.  Returns 1 when a job was finished. */
+static int preload_finish_worker(struct fetch_worker *w)
+{
+	char err[64];
+	int rv, tr = 0;
+
+	rv = fetch_worker_poll(w, err, sizeof(err));
+	if (rv < 0) {
+		preload_job_error(w);
+		fetch_worker_release(w);
+		return 1;
+	}
+	if (rv == 0)
+		return 0;
+
+	rv = fetch_worker_read_body(w, image_file_buf, BR_IMG_FILE_MAX,
+				    image_comp_buf, BR_IMG_FILE_MAX,
+				    &tr, err, sizeof(err));
+	if (rv >= 0)
+		preload_decode(w->node, w->bg, image_file_buf,
+			       (unsigned int)rv);
+	else
+		preload_job_error(w);
+	fetch_worker_release(w);
+	return 1;
+}
+
+/* Fill free workers from the DOM, scanning past ineligible nodes.  Returns
+ * the number of jobs started. */
+static int preload_fill_jobs(void)
+{
+	int started = 0;
+	char err[64];
 
 	while (preload_active && preload_node < doc.nnode) {
 		unsigned int i = preload_node;
 		struct br_node *nd = &doc.nodes[i];
 		const char *src;
 		char url[BR_URL_MAX];
-		unsigned char *data;
-		unsigned int len;
-		char err[64];
-		char msg[64];
-		unsigned int n = 0;
-		int rv;
+		struct fetch_worker *w;
 
 		if (nd->type != BR_NODE_ELEM) {
 			preload_node++;
 			continue;
 		}
+
 		if (nd->tag != BR_TAG_IMG) {
 			if (nd->bg_img_index == -1) {
 				struct br_style bs;
@@ -2701,39 +2951,41 @@ static void preload_step(void)
 					    !starts_with(bsrc, "data:") &&
 					    resolve_link(page_url, bsrc, url,
 							 sizeof(url)) == 0) {
-						if (image_count >=
+						if (image_count +
+						    preload_active_jobs() >=
 						    BR_MAX_IMAGES) {
-							preload_node =
-								doc.nnode;
+							preload_node = doc.nnode;
 							break;
 						}
-						preload_node = i + 1;
-						data = 0;
-						len = 0;
-						rv = fetch_image(url,
-								 BR_IMG_FILE_MAX,
-								 &data, &len,
-								 err, sizeof(err));
-						if (rv == FETCH_ABORT) {
-							preload_active = 0;
-							preload_pending = 0;
-							return;
+						if (url[0] == '/') {
+							unsigned char *data = 0;
+							unsigned int len = 0;
+
+							preload_node = i + 1;
+							if (fetch_image(url,
+									BR_IMG_FILE_MAX,
+									&data, &len,
+									err,
+									sizeof(err)) == 0)
+								preload_decode(i, 1, data, len);
+							else
+								nd->bg_img_index = -2;
+							continue;
 						}
-						if (rv == 0 &&
-						    decode_image_data(
-							    data, len,
-							    &images[image_count]) == 0) {
-							nd->bg_img_index =
-								(short)image_count;
-							image_count++;
-							preload_loaded++;
-							layout_valid = 0;
-						} else {
+						w = fetch_worker_alloc();
+						if (!w)
+							return started;
+						preload_node = i + 1;
+						if (fetch_worker_submit(w, url,
+									BR_IMG_FILE_MAX,
+									0, 0, i, 1,
+									err,
+									sizeof(err)) != 0) {
 							nd->bg_img_index = -2;
+							fetch_worker_release(w);
+							continue;
 						}
-						ngui_invalidate(&ui);
-						if (++batch >= 4)
-							return;
+						started++;
 						continue;
 					}
 				}
@@ -2742,6 +2994,7 @@ static void preload_step(void)
 			preload_node++;
 			continue;
 		}
+
 		if (nd->img_index != -1) {
 			preload_node++;
 			continue;
@@ -2760,7 +3013,7 @@ static void preload_step(void)
 			preload_node++;
 			continue;
 		}
-		if (image_count >= BR_MAX_IMAGES) {
+		if (image_count + preload_active_jobs() >= BR_MAX_IMAGES) {
 			preload_node = doc.nnode;
 			break;
 		}
@@ -2773,51 +3026,90 @@ static void preload_step(void)
 			preload_node++;
 			continue;
 		}
-		preload_node = i + 1;
 
-		s_append(msg, &n, "Loading images ", sizeof(msg));
-		s_append_dec(msg, &n, (unsigned int)preload_loaded + 1,
-			     sizeof(msg));
-		s_append(msg, &n, "...", sizeof(msg));
-		set_status(msg, 0, 0);
-		ngui_invalidate(&ui);
+		if (url[0] == '/') {
+			unsigned char *data = 0;
+			unsigned int len = 0;
 
-		data = 0;
-		len = 0;
-		rv = fetch_image(url, BR_IMG_FILE_MAX, &data, &len, err,
-				 sizeof(err));
-		if (rv == FETCH_ABORT) {
-			preload_active = 0;
-			preload_pending = 0;
-			return;
+			preload_node = i + 1;
+			if (fetch_image(url, BR_IMG_FILE_MAX, &data, &len,
+					err, sizeof(err)) == 0)
+				preload_decode(i, 0, data, len);
+			else
+				nd->img_index = -2;
+			continue;
 		}
-		if (rv == 0 &&
-		    decode_image_data(data, len, &images[image_count]) == 0) {
-			nd->img_index = (short)image_count;
-			image_count++;
-			preload_loaded++;
-			layout_valid = 0;
-		} else {
-			/* Remember failures so a scroll does not retry an
-			 * unreachable tracking/ad image with the full network
-			 * timeout on every pass. */
+
+		w = fetch_worker_alloc();
+		if (!w)
+			return started;
+		preload_node = i + 1;
+		{
+			char msg[64];
+			unsigned int n = 0;
+
+			s_append(msg, &n, "Loading images ", sizeof(msg));
+			s_append_dec(msg, &n,
+				     (unsigned int)preload_loaded + 1,
+				     sizeof(msg));
+			s_append(msg, &n, "...", sizeof(msg));
+			set_status(msg, 0, 0);
+		}
+		if (fetch_worker_submit(w, url, BR_IMG_FILE_MAX, 0, 0, i, 0,
+					err, sizeof(err)) != 0) {
 			nd->img_index = -2;
+			fetch_worker_release(w);
+			continue;
 		}
-		ngui_invalidate(&ui);
-		/* A full page layout after every image dominates loading on a
-		 * long page.  Decode a small batch, then let the main loop draw
-		 * once for the whole batch. */
-		if (++batch >= 4)
-			return;
+		started++;
 	}
+	return started;
+}
 
-	preload_active = 0;
-	if (preload_pending)
-		preload_scroll = page_scroll.offset;
-	else
+static void preload_step(void)
+{
+	unsigned int batch = 0;
+
+	if (!preload_active)
+		return;
+	if (cancel_fetch || pending_action) {
+		preload_active = 0;
 		preload_pending = 0;
-	set_status(post_status_text, post_status_error, post_status_ttl);
-	ngui_invalidate(&ui);
+		fetch_workers_stop_all();
+		return;
+	}
+
+	for (;;) {
+		int started = preload_fill_jobs();
+		int done = 0;
+		int i;
+
+		for (i = 0; i < FETCH_WORKERS; i++) {
+			if (!fetch_workers[i].busy)
+				continue;
+			if (preload_finish_worker(&fetch_workers[i]))
+				done++;
+			if (batch + done >= 4)
+				break;
+		}
+		batch += (unsigned int)done;
+		if (batch >= 4)
+			return;
+		if (!started && !done)
+			break;
+	}
+
+	if (preload_active && preload_node >= doc.nnode &&
+	    preload_active_jobs() == 0) {
+		preload_active = 0;
+		if (preload_pending)
+			preload_scroll = page_scroll.offset;
+		else
+			preload_pending = 0;
+		fetch_workers_stop_all();
+		set_status(post_status_text, post_status_error, post_status_ttl);
+		ngui_invalidate(&ui);
+	}
 }
 
 static void history_push(const char *url)
@@ -2861,6 +3153,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();
 
 	js_browser_stop();
 	js_last_status[0] = '\0';
@@ -7875,7 +8168,7 @@ void _start(const char *arg)
 			continue;
 		u_sleep_ms(10);
 	}
-	fetch_helper_stop();
+	fetch_workers_stop_all();
 	u_win_close();
 	u_exit(0);
 }

+ 141 - 30
dns.c

@@ -10,18 +10,33 @@
 
 #include "io.h"
 #include "timer.h"
+#include "netcore.h"
 #include "netdev.h"
 #include "net.h"
+#include "process.h"
 #include "dns.h"
 
 #define DNS_PORT        53
 #define DNS_MAX_ANSWERS 4
 #define DNS_RETRY_MS    1000
 #define DNS_RETRIES     3
+#define DNS_WAIT_MS     5000
 
 /* Query buffer: 12-byte header + a 255-byte name + qtype/qclass. */
 static unsigned char query_buf[12 + 260 + 4];
 
+/* One query may be pending at a time.  Stack-mode resolvers hand received
+ * frames to dns_input() through net_pump(); a raw owner keeps its direct
+ * polling path because it consumes every frame itself. */
+static int dns_busy;
+static unsigned int dns_owner;
+static unsigned short dns_pending_id;
+static unsigned char dns_pending_server[4];
+static unsigned char dns_pending_ip[4];
+static unsigned char dns_pending_extra[DNS_MAX_ANSWERS][4];
+static unsigned int dns_pending_count;
+static int dns_pending_result;      /* 0 none, 1 answer, -1 failure */
+
 static void put_dec(unsigned int v)
 {
 	char b[12];
@@ -198,28 +213,94 @@ static int dns_parse(const unsigned char *frame, unsigned int len,
 	return *count > 0 ? 1 : 0;
 }
 
+/* Feed one frame to a pending stack-mode query.  Returns 1 when it was a
+ * reply to the pending query (consumed), 0 otherwise. */
+int dns_input(const unsigned char *frame, unsigned int len)
+{
+	int r;
+
+	if (!dns_busy || dns_pending_result)
+		return 0;
+	r = dns_parse(frame, len, dns_pending_id, dns_pending_server,
+		      dns_pending_ip, dns_pending_extra,
+		      &dns_pending_count);
+	if (r == 0)
+		return 0;
+	dns_pending_result = r > 0 ? 1 : -1;
+	return 1;
+}
+
+void dns_reap(unsigned int pid)
+{
+	if (dns_busy && dns_owner == pid) {
+		dns_busy = 0;
+		dns_owner = 0;
+		dns_pending_result = 0;
+	}
+}
+
+static void dns_print_answer(const char *name, const unsigned char *ip,
+			     const unsigned char extra[DNS_MAX_ANSWERS][4],
+			     unsigned int count)
+{
+	unsigned int i;
+
+	io_putstr("dns: ");
+	io_putstr(name);
+	io_putstr(" -> ");
+	put_ip(ip);
+	for (i = 1; i < count && i < DNS_MAX_ANSWERS; i++) {
+		io_putstr(", ");
+		put_ip(extra[i - 1]);
+	}
+	io_putchar('\n');
+}
+
 int dns_resolve(const char *name, unsigned char ip[4])
 {
 	unsigned char server[4], hop[4], server_mac[6];
 	unsigned char extra[DNS_MAX_ANSWERS][4];
-	unsigned char *rx_buf = netdev_rx_scratch();
 	const struct netdev *nd = netdev_active();
 	unsigned short id;
-	unsigned int sport, qlen, tries, count, i;
-	int r;
+	unsigned int sport, qlen, tries, count = 0, i;
+	unsigned int pid = current_proc() ? current_proc()->pid : 0;
+	unsigned int wait_start;
+	int raw, r = -1;
 
 	if (!nd) {
 		io_putstr("dns: no NIC found\n");
 		return -1;
 	}
 
+	raw = pid && netdev_owner() == pid;
+	if (!raw)
+		net_stack_enter();
+
+	/* One query in flight.  A second stack-mode resolver waits (and keeps
+	 * pumping TCP frames) until the first one finishes. */
+	wait_start = get_ticks_ms();
+	while (dns_busy && dns_owner != pid) {
+		if (raw ||
+		    (int)(get_ticks_ms() - (wait_start + DNS_WAIT_MS)) >= 0) {
+			if (!raw)
+				net_stack_leave();
+			return -1;
+		}
+		net_pump(8);
+		process_sleep_ms(1);
+	}
+	dns_busy = 1;
+	dns_owner = pid;
+	dns_pending_result = 0;
+	dns_pending_count = 0;
+
 	net_get_dns(server);
 	if (!(server[0] || server[1] || server[2] || server[3])) {
 		/* Home routers usually proxy DNS for their clients. */
 		net_get_gateway(server);
 		if (!(server[0] || server[1] || server[2] || server[3])) {
 			io_putstr("dns: no resolver (run netdns <a.b.c.d>)\n");
-			return -1;
+			goto out;
 		}
 	}
 
@@ -239,7 +320,7 @@ int dns_resolve(const char *name, unsigned char ip[4])
 
 		if (n <= 0) {
 			io_putstr("dns: bad name\n");
-			return -1;
+			goto out;
 		}
 		qlen += (unsigned int)n;
 	}
@@ -249,51 +330,81 @@ int dns_resolve(const char *name, unsigned char ip[4])
 	query_buf[qlen++] = 1;
 
 	sport = 45000 + (id & 0x0FFF);
+	for (i = 0; i < 4; i++)
+		dns_pending_server[i] = server[i];
+	dns_pending_id = id;
 
 	net_next_hop(server, hop);
 	if (net_arp_resolve(hop, server_mac) != 0) {
 		io_putstr("dns: no ARP reply from ");
 		put_ip(hop);
 		io_putchar('\n');
-		return -1;
+		goto out;
 	}
 
 	for (tries = 0; tries < DNS_RETRIES; tries++) {
 		unsigned int deadline = get_ticks_ms() + DNS_RETRY_MS;
-
-		if (net_udp_send(server_mac, server, sport, DNS_PORT,
-				 query_buf, qlen) != 0) {
+		unsigned int flags;
+		int tx;
+
+		flags = net_irq_enter();
+		tx = net_udp_send(server_mac, server, sport, DNS_PORT,
+				  query_buf, qlen);
+		net_irq_exit(flags);
+		if (tx != 0) {
 			io_putstr("dns: send failed\n");
-			return -1;
+			goto out;
 		}
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			int len = nd->rx(rx_buf, NETDEV_FRAME_MAX);
-
-			if (len < 0)
-				continue;
-			r = dns_parse(rx_buf, (unsigned int)len, id, server,
-				      ip, extra, &count);
-			if (r < 0)
-				return -1;
-			if (r == 0)
-				continue;
-			io_putstr("dns: ");
-			io_putstr(name);
-			io_putstr(" -> ");
-			put_ip(ip);
-			for (i = 1; i < count && i < DNS_MAX_ANSWERS; i++) {
-				io_putstr(", ");
-				put_ip(extra[i - 1]);
+			if (raw) {
+				int len = nd->rx(netdev_rx_scratch(),
+						 NETDEV_FRAME_MAX);
+
+				if (len < 0)
+					continue;
+				r = dns_parse(netdev_rx_scratch(),
+					      (unsigned int)len, id, server,
+					      ip, extra, &count);
+				if (r < 0)
+					goto out;
+				if (r == 0)
+					continue;
+				dns_print_answer(name, ip, extra, count);
+				r = 0;
+				goto out;
 			}
-			io_putchar('\n');
-			return 0;
+			net_pump(8);
+			if (dns_pending_result > 0) {
+				for (i = 0; i < 4; i++)
+					ip[i] = dns_pending_ip[i];
+				for (i = 0; i < DNS_MAX_ANSWERS; i++) {
+					unsigned int j;
+
+					for (j = 0; j < 4; j++)
+						extra[i][j] =
+							dns_pending_extra[i][j];
+				}
+				count = dns_pending_count;
+				dns_print_answer(name, ip, extra, count);
+				r = 0;
+				goto out;
+			}
+			if (dns_pending_result < 0)
+				goto out;
+			process_sleep_ms(1);
 		}
 	}
 
 	io_putstr("dns: no reply from ");
 	put_ip(server);
 	io_putchar('\n');
-	return -1;
+out:
+	dns_busy = 0;
+	dns_owner = 0;
+	dns_pending_result = 0;
+	if (!raw)
+		net_stack_leave();
+	return r;
 }
 

+ 9 - 1
dns.h

@@ -4,9 +4,17 @@
 
 /* Resolve `name` to an IPv4 address with a recursive query to the configured
  * resolver (DHCP option 6, else the gateway).  Prints a `dns:` line.
- * Returns 0 on success.  The caller must own the NIC (netdev_acquire). */
+ * Returns 0 on success.  A raw owner polls the NIC directly; stack users are
+ * driven by net_pump(). */
 int dns_resolve(const char *name, unsigned char ip[4]);
 
+/* Feed one received frame to the pending stack-mode query.  Returns 1 when it
+ * was a reply to that query (consumed), 0 otherwise.  Called by net_pump(). */
+int dns_input(const unsigned char *frame, unsigned int len);
+
+/* Called by the reaper when a resolver process dies. */
+void dns_reap(unsigned int pid);
+
 /* `netdns [a.b.c.d]` (show/set the resolver) and `host <name>`. */
 void program_netdns_main(const char *arg);
 void program_host_main(const char *arg);

+ 5 - 4
docs/filesystems.html

@@ -327,10 +327,11 @@
 			<li><strong>No auth.</strong> This is a trusted-LAN service; <code>/share</code> is writable by anyone who can reach it.</li>
 		</ul>
 		<p>
-			The WebDAV server and the packet-level <code>ping</code>/<code>curl</code>/
-			<code>netrecv</code> commands are mutually exclusive: both need the
-			single NIC receiver. Starting <code>websrv</code> while another
-			command owns the receiver fails.
+			The WebDAV server still takes the raw NIC lease, so it is mutually
+			exclusive with packet-level commands and with TCP client connections
+			(<code>curl</code> and the browser workers).  Starting
+			<code>websrv</code> while another raw owner exists fails; TCP clients
+			fail to connect while it owns the receiver.
 		</p>
 	</section>
 

+ 40 - 26
docs/network.html

@@ -33,12 +33,15 @@
 <main id="main" class="page">
 	<div class="page-head">
 		<p class="kicker">Networking</p>
-		<h1>A small stack with one receiver owner.</h1>
+		<h1>A small stack with a shared receive pump.</h1>
 		<p class="lede">
 			The NIC and the protocol stack live in the kernel; ring-3 commands
 			drive them through <code>SYS_NET_*</code> and <code>SYS_TCP_*</code>.
-			Only one process may own the receiver at a time, which keeps the
-			polled drivers and the single TCP connection simple.
+			Packet-level commands still take the receiver exclusively through
+			<code>SYS_NET_ACQUIRE</code>, while up to six processes can hold a
+			TCP client connection at the same time.  Every stack syscall drains
+			the NIC through <code>net_pump()</code>, so downloads overlap without
+			sharing one connection or one receive queue.
 		</p>
 		<div class="meta">
 			<span class="pill">ARP · ICMP · UDP · TCP</span>
@@ -68,10 +71,13 @@
 	<section id="stack">
 		<h2>Protocol stack</h2>
 		<p>
-			NebOS does not expose sockets. A ring-3 program asks the kernel for
-			info and configuration, acquires the receiver, then uses raw frame
-			paths or the single TCP connection. HTTP and TLS are libraries linked
-			into the command, not kernel services.
+			NebOS does not expose a general socket API. A ring-3 program asks the
+			kernel for info and configuration, then either acquires the receiver
+			for raw frame paths or opens one TCP client connection.  Each
+			connection has its own receive queue and is owned by one process;
+			<code>net_pump()</code> dispatches frames to every active client, the
+			WebDAV listener, a pending DNS query and the ARP cache. HTTP and TLS
+			are libraries linked into the command, not kernel services.
 		</p>
 
 		<figure class="figure">
@@ -101,14 +107,14 @@
 
 				<rect class="svg-box green" x="70" y="176" width="780" height="58" rx="9"/>
 				<text class="svg-text" x="92" y="201">SYS_NET_* · SYS_TCP_* · SYS_DNS_RESOLVE · SYS_DHCP_RUN · SYS_NTP_SYNC</text>
-				<text class="svg-sub" x="92" y="221">one receiver owner · interrupt-enabled waits</text>
+				<text class="svg-sub" x="92" y="221">raw owner for packet commands · shared net_pump for TCP/DNS/ARP</text>
 
 				<path class="svg-line green" d="M460 234v20"/>
 				<path class="svg-line green" d="M454 248l6 7 6-7"/>
 
 				<rect class="svg-box" x="70" y="256" width="780" height="58" rx="9"/>
 				<text class="svg-text" x="92" y="281">tcp.c · dns.c · dhcp.c</text>
-				<text class="svg-sub" x="92" y="301">TCP client + listener · one A query · DISCOVER/OFFER/REQUEST/ACK</text>
+				<text class="svg-sub" x="92" y="301">TCP client table + listener · one pending A query · DISCOVER/OFFER/REQUEST/ACK</text>
 
 				<path class="svg-line" d="M460 314v16"/>
 				<path class="svg-line" d="M454 324l6 7 6-7"/>
@@ -118,9 +124,9 @@
 			</svg>
 			<figcaption>
 				<strong>Thin kernel, thick command.</strong> The kernel owns
-				addressing, ARP, routing and the single TCP connection. HTTP, TLS
-				and WebDAV run in ring 3, so a protocol bug kills a command rather
-				than the kernel.
+				addressing, ARP, routing and the shared TCP client table. HTTP,
+				TLS and WebDAV run in ring 3, so a protocol bug kills a command
+				rather than the kernel.
 			</figcaption>
 		</figure>
 	</section>
@@ -224,18 +230,21 @@
 	<section id="tcp">
 		<h2>TCP</h2>
 		<p>
-			<span class="file">tcp.c</span> deliberately implements one client
-			connection, not a general socket layer: connect, send, receive and
-			close. An 8 KB receive queue lets data arriving during a send survive;
-			the window update is sent whenever the application drains the queue,
+			<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> (six)
+			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.
+			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>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>Bursts.</strong> Responses go out in up to four-segment bursts with a cumulative ACK, rather than one segment per ACK.</li>
-			<li><strong>No keep-alive.</strong> With one active connection, an idle persistent connection would block the parallel connections Nautilus opens.</li>
+			<li><strong>No keep-alive.</strong> The WebDAV listener still has one active connection; an idle persistent connection would block the parallel connections Nautilus opens.  Client keep-alive is per helper process and does not hold the raw lease.</li>
 		</ul>
 		<p>
 			New SYNs and data are also handed to <code>tcp_srv_input()</code> from
@@ -274,10 +283,11 @@
 			exposes the DNS, DHCP and NTP clients and the TCP/HTTP helpers.
 		</p>
 		<ul>
-			<li><strong>One owner.</strong> <code>SYS_NET_ACQUIRE</code> takes the receiver for the calling process; the background kexec watcher pauses while a command owns it.</li>
-			<li><strong>Reaper safety.</strong> If the owner dies, <code>sys_net_reap</code> force-releases the receiver through <code>netdev_release_all()</code>.</li>
-			<li><strong>Wait semantics.</strong> ARP, DNS, DHCP, NTP and TCP waits re-enable interrupts for their duration and spin on <code>get_ticks_ms()</code>; the syscall stub enters with interrupts off.</li>
-			<li><strong>No sockets.</strong> The connection state lives in the kernel, one connection at a time; this is why the browser opens a fresh connection per fetch job.</li>
+			<li><strong>Raw owner.</strong> <code>SYS_NET_ACQUIRE</code> takes the receiver for a packet-level command; the background kexec watcher pauses while one owns it.  TCP clients do not take this lease, so they can run alongside each other.</li>
+			<li><strong>Mode gates.</strong> A raw acquire fails while any TCP client socket is open, and <code>SYS_TCP_CONNECT</code> fails while a raw owner exists.  This keeps packet commands and the shared pump from stealing each other's frames.</li>
+			<li><strong>Reaper safety.</strong> If a raw owner dies, <code>sys_net_reap</code> force-releases the receiver through <code>netdev_release_all()</code>; sockets and a pending DNS query owned by the dead process are closed separately.</li>
+			<li><strong>Wait semantics.</strong> Stack waits now yield with <code>process_sleep_ms()</code> and re-enable interrupts for driver TX timeouts; the syscall stub enters with interrupts off.</li>
+			<li><strong>One connection per process.</strong> The connection state lives in the kernel, keyed by the calling pid.  <code>SYS_TCP_CONNECT/SEND/RECV/CLOSE</code> keep their ABI, so uhttp and the TLS layer did not need a socket handle.</li>
 		</ul>
 	</section>
 
@@ -289,7 +299,10 @@
 			broadcasts; <code>netconsole host &lt;laptop-ip&gt;</code> narrows it,
 			<code>netconsole off</code>/<code>on</code> toggles it, and
 			<code>netlog &lt;text&gt;</code> sends an ad-hoc line. The producer
-			never blocks: a full buffer drops characters and reports it.
+			never blocks: a full buffer drops characters and reports it.  The
+			flush pauses while a raw command owns the receiver or a TCP/stack
+			operation is active, so its broadcast traffic cannot compete with
+			DNS/ARP replies.
 		</p>
 		<p>
 			<span class="file">netkexec.c</span> receives a new kernel/rootfs over
@@ -306,7 +319,7 @@
 		<ul>
 			<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 and the browser: URL parsing, redirects, chunked decoding, timeouts and TLS.</li>
-			<li><strong>browser</strong> — runs network fetches in a headless <code>curl __fetch</code> helper that writes the body and metadata under <code>/share</code>, so the UI keeps drawing and scrolling. Each fetch opens a fresh 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</code>.  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>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>
@@ -328,7 +341,7 @@
 					<tr><td>TLS DRBG</td><td>Weak (RDTSC/ticks/MAC)</td><td>Not suitable for high-value secrets.</td></tr>
 					<tr><td>WebDAV</td><td>No authentication</td><td>Anyone on the LAN can read mounts and write <code>/share</code>.</td></tr>
 					<tr><td>NTP</td><td>Unauthenticated SNTP</td><td>Anyone who can answer for the server name can set the clock.</td></tr>
-					<tr><td>NIC ownership</td><td>One process at a time</td><td>Packet-level commands are mutually exclusive; a killed owner is force-released.</td></tr>
+					<tr><td>NIC ownership</td><td>Raw lease for packet commands; shared TCP client table</td><td>Raw commands are mutually exclusive; up to six processes can hold one TCP connection each.  A killed owner is force-released.</td></tr>
 					<tr><td>Kernel/user isolation</td><td>None (identity map)</td><td>A ring-3 bug can reach any mapped kernel page.</td></tr>
 				</tbody>
 			</table>
@@ -340,9 +353,10 @@
 		<ul class="filelist">
 			<li><code>netdev.c</code>, <code>netdev.h</code> — registry</li>
 			<li><code>e1000e.c</code>, <code>r8168.c</code>, <code>ne2000.c</code>, <code>asix.c</code></li>
-			<li><code>net.c</code>, <code>net.h</code> — ARP/ICMP/UDP/IP</li>
+			<li><code>net.c</code>, <code>net.h</code> — ARP cache/ICMP/UDP/IP</li>
+			<li><code>netcore.c</code>, <code>netcore.h</code> — shared receive pump and IRQ helpers</li>
 			<li><code>dhcp.c</code>, <code>dns.c</code>, <code>ntp.c</code></li>
-			<li><code>tcp.c</code>, <code>tcp.h</code> — client + listener</li>
+			<li><code>tcp.c</code>, <code>tcp.h</code> — client table + listener</li>
 			<li><code>tls.c</code>, <code>tls_*.c</code> — TLS 1.3</li>
 			<li><code>sys_net.c</code> — ring-3 service</li>
 			<li><code>netlog.c</code>, <code>netkexec.c</code></li>

+ 7 - 6
docs/userland.html

@@ -231,7 +231,8 @@
 		</div>
 		<p>
 			The file service is whole-file, not stream-oriented. The network
-			service has a single TCP connection and one NIC owner. There is no
+			service has a per-process TCP client table (up to six connections)
+			and a separate raw NIC lease for packet-level commands. There is no
 			<code>fork</code>, <code>exec</code> or shared memory: a command
 			either runs as its own process from an ELF or calls a kernel command.
 		</p>
@@ -360,10 +361,10 @@
 			ngui, the image library, inflate, TLS, uhttp and the vendored MuJS
 			engine. It opens at 1100×700 (960×640 and 780×540 fallbacks on
 			smaller screens), with a URL bar, history, a status bar, inline
-			images and inline/external scripts. Network
-			work runs in a headless <code>curl __fetch</code> helper that writes
-			the body and metadata under <code>/share</code>, so the UI keeps
-			drawing and scrolling.
+			images and inline/external scripts. Network work runs in up to three
+			headless <code>curl __serve</code> workers, each with its own files
+			under <code>/share</code>, so the UI keeps drawing and scrolling
+			while several TCP/TLS connections are in flight.
 		</p>
 		<ul>
 			<li><strong>HTML/CSS subset.</strong> External and inline stylesheets, tag/class/id and descendant/child selectors, colours (including legacy <code>bgcolor</code> attributes), backgrounds (solid and <code>background-image</code> with repeat/position/size and the <code>background</code> shorthand URL), real <code>font-size</code> (8–48 px), the <code>font</code> shorthand, bold/italic faces, <code>font-family: monospace</code> and one regular <code>@font-face</code> face per family (local or HTTP TTF), margins/padding (percentage sizes use the containing width; <code>em</code> uses the element font), per-side borders, alignment, explicit/percentage block widths with <code>min-width</code>/<code>max-width</code>/<code>min-height</code>/<code>max-height</code>, <code>box-sizing: border-box</code>, <code>margin: 0 auto</code>, nested <code>em</code>/<code>rem</code> sizes, basic floats, <code>position: relative</code>/<code>absolute</code>/<code>fixed</code>/<code>sticky</code> (top/bottom, containing-block clamp), <code>right</code>/<code>bottom</code>, <code>z-index</code> (including relative boxes), <code>overflow: hidden</code> clipping, <code>transform: translate</code>/<code>scale</code>/<code>rotate</code> with <code>transform-origin</code> (paint-time box and image transforms), flex rows with grow/wrap/justify (including <code>space-evenly</code>)/align, two-column grids, table rowspan/colspan and percentage columns, <code>calc(percent ± px)</code> in widths, margins/padding, heights and min/max, <code>white-space: nowrap</code> and <code>@media</code> blocks evaluated at the browser canvas width when the page is parsed. Text is Latin-1; there is no shaping, bidi, WOFF/unicode-range or full-Unicode fallback.</li>
@@ -372,7 +373,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> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</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.  Completed images are decoded as jobs finish.  Page, CSS and script fetches stay ordered.</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. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) 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>

+ 129 - 16
net.c

@@ -2,8 +2,10 @@
    Freestanding, no malloc; fixed-size buffers and a single configured IP. */
 
 #include "io.h"
+#include "netcore.h"
 #include "netdev.h"
 #include "net.h"
+#include "process.h"
 #include "timer.h"
 
 static unsigned char my_ip[4] = { 192, 168, 178, 240 };
@@ -421,9 +423,23 @@ static unsigned int arp_build_request(const unsigned char *my_mac,
 	return 42;
 }
 
-/* 1 + peer MAC if frame is an ARP reply from target_ip. */
-static int arp_parse_reply(const unsigned char *f, unsigned int len,
-			   const unsigned char *target_ip, unsigned char *mac)
+/* ---- ARP cache (shared by the stack pump and the raw poll path) ---- */
+
+#define ARP_CACHE_MAX    8
+#define ARP_CACHE_TTL_MS 60000
+
+struct arp_entry {
+	unsigned char ip[4];
+	unsigned char mac[6];
+	unsigned int  age_ms;
+	int           used;
+};
+
+static struct arp_entry arp_cache[ARP_CACHE_MAX];
+
+/* Sender address of an ARP reply.  Returns 1 and fills spa/sha. */
+static int arp_parse_sender(const unsigned char *f, unsigned int len,
+			    unsigned char spa[4], unsigned char sha[6])
 {
 	const unsigned char *a = f + 14;
 	unsigned int i;
@@ -432,14 +448,75 @@ static int arp_parse_reply(const unsigned char *f, unsigned int len,
 		return 0;
 	if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
 		return 0;
-	if ((((unsigned int)a[6] << 8) | a[7]) != 2)   /* opcode reply */
+	if (a[0] != 0 || a[1] != 1)                    /* htype Ethernet */
+		return 0;
+	if (a[2] != 0x08 || a[3] != 0x00)              /* ptype IPv4 */
 		return 0;
 	if (a[4] != 6 || a[5] != 4)
 		return 0;
-	if (!ip_eq(a + 14, target_ip))                 /* sender IP == target */
+	if (a[6] != 0 || a[7] != 2)                    /* opcode reply */
 		return 0;
 	for (i = 0; i < 6; i++)
-		mac[i] = a[8 + i];
+		sha[i] = a[8 + i];
+	for (i = 0; i < 4; i++)
+		spa[i] = a[14 + i];
+	return 1;
+}
+
+static void arp_cache_put(const unsigned char *ip, const unsigned char *mac)
+{
+	unsigned int i, oldest = 0;
+
+	for (i = 0; i < ARP_CACHE_MAX; i++) {
+		if (arp_cache[i].used && ip_eq(arp_cache[i].ip, ip)) {
+			oldest = i;
+			goto store;
+		}
+	}
+	for (i = 0; i < ARP_CACHE_MAX; i++) {
+		if (!arp_cache[i].used) {
+			oldest = i;
+			goto store;
+		}
+		if (arp_cache[i].age_ms < arp_cache[oldest].age_ms)
+			oldest = i;
+	}
+store:
+	for (i = 0; i < 4; i++)
+		arp_cache[oldest].ip[i] = ip[i];
+	for (i = 0; i < 6; i++)
+		arp_cache[oldest].mac[i] = mac[i];
+	arp_cache[oldest].age_ms = get_ticks_ms();
+	arp_cache[oldest].used = 1;
+}
+
+static int arp_cache_lookup(const unsigned char *ip, unsigned char mac[6])
+{
+	unsigned int i, j, now = get_ticks_ms();
+
+	for (i = 0; i < ARP_CACHE_MAX; i++) {
+		if (!arp_cache[i].used || !ip_eq(arp_cache[i].ip, ip))
+			continue;
+		if (now - arp_cache[i].age_ms > ARP_CACHE_TTL_MS) {
+			arp_cache[i].used = 0;
+			return 0;
+		}
+		for (j = 0; j < 6; j++)
+			mac[j] = arp_cache[i].mac[j];
+		return 1;
+	}
+	return 0;
+}
+
+/* Feed one received frame to the cache.  Returns 1 when it was an ARP
+ * reply (consumed), 0 otherwise. */
+int net_arp_input(const unsigned char *f, unsigned int len)
+{
+	unsigned char spa[4], sha[6];
+
+	if (!arp_parse_sender(f, len, spa, sha))
+		return 0;
+	arp_cache_put(spa, sha);
 	return 1;
 }
 
@@ -448,32 +525,68 @@ int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6])
 	const struct netdev *nd = netdev_active();
 	unsigned char my_mac[6], my_ip[4];
 	unsigned char req[64];
-	unsigned char *rxbuf = netdev_rx_scratch();
 	unsigned int tries;
+	int raw;
 
+	if (arp_cache_lookup(dst_ip, mac))
+		return 0;
 	if (!nd || !nd->get_mac)
 		return -2;
+	raw = current_proc() && netdev_owner() == current_proc()->pid;
 	nd->get_mac(my_mac);
 	net_get_ip(my_ip);
+	if (!raw)
+		net_stack_enter();
 
 	for (tries = 0; tries < 3; tries++) {
 		unsigned int deadline = get_ticks_ms() + 1000;
 		unsigned int reqlen = arp_build_request(my_mac, my_ip,
 							dst_ip, req);
-
-		if (nd->tx(req, reqlen) != 0)
+		unsigned int flags = 0;
+		int tx;
+
+		/* A driver TX timeout spins on get_ticks_ms(); keep the PIT
+		 * alive even though the syscall gate entered with IF=0. */
+		if (!raw)
+			flags = net_irq_enter();
+		tx = nd->tx(req, reqlen);
+		if (!raw)
+			net_irq_exit(flags);
+		if (tx != 0) {
+			if (!raw)
+				net_stack_leave();
 			return -2;
+		}
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
-
-			if (len < 0)
-				continue;
-			if (arp_parse_reply(rxbuf, (unsigned int)len,
-					    dst_ip, mac))
-				return 0;
+			if (raw) {
+				unsigned char spa[4], sha[6];
+				int len = nd->rx(netdev_rx_scratch(),
+						 NETDEV_FRAME_MAX);
+
+				if (len < 0)
+					continue;
+				if (arp_parse_sender(netdev_rx_scratch(),
+						     (unsigned int)len,
+						     spa, sha)) {
+					arp_cache_put(spa, sha);
+					if (ip_eq(spa, dst_ip)) {
+						arp_cache_lookup(dst_ip, mac);
+						return 0;
+					}
+				}
+			} else {
+				net_pump(8);
+				if (arp_cache_lookup(dst_ip, mac)) {
+					net_stack_leave();
+					return 0;
+				}
+				process_sleep_ms(1);
+			}
 		}
 	}
+	if (!raw)
+		net_stack_leave();
 	return -1;
 }
 

+ 7 - 2
net.h

@@ -41,11 +41,16 @@ void net_set_dns(const unsigned char dns[4]);
  * known, else dst itself. */
 void net_next_hop(const unsigned char dst[4], unsigned char hop[4]);
 
-/* Resolve dst_ip's MAC by ARP (3 tries, 1 s apart), polling the active NIC.
+/* Resolve dst_ip's MAC by ARP (3 tries, 1 s apart), using a small cache.
  * Returns 0 on success, -1 when no reply arrived, -2 on transmit error.
- * The caller must own the NIC (netdev_acquire). */
+ * A raw owner (netdev_acquire) polls the NIC directly; stack users are driven
+ * by net_pump() and must not hold the raw lease. */
 int net_arp_resolve(const unsigned char *dst_ip, unsigned char mac[6]);
 
+/* Feed one received frame to the ARP cache.  Returns 1 when it was an ARP
+ * reply (consumed), 0 otherwise.  Called by net_pump(). */
+int net_arp_input(const unsigned char *frame, unsigned int len);
+
 const char *net_ethertype_name(unsigned short et);
 
 /* Internet checksum (16-bit one's complement, network byte order). */

+ 132 - 0
netcore.c

@@ -0,0 +1,132 @@
+/* Shared kernel receive pump: see netcore.h. */
+
+#include "netcore.h"
+#include "dns.h"
+#include "net.h"
+#include "netdev.h"
+#include "netkexec.h"
+#include "process.h"
+#include "spinlock.h"
+#include "tcp.h"
+
+/* Reply scratch for ARP/ICMP answered by the pump.  The received frame is
+ * still in netdev_rx_scratch(), so the reply cannot reuse it. */
+static unsigned char reply_buf[NETDEV_FRAME_MAX];
+
+/* Stack operations that may sleep between pumps, tracked per process so a
+ * killed waiter cannot leave the netkexec/netlog gates stuck. */
+#define NET_STACK_MAX 8
+static struct {
+	unsigned int pid;
+	unsigned int depth;
+} stack_ops[NET_STACK_MAX];
+
+static int stack_slot(unsigned int pid)
+{
+	unsigned int i;
+
+	for (i = 0; i < NET_STACK_MAX; i++)
+		if (stack_ops[i].depth && stack_ops[i].pid == pid)
+			return (int)i;
+	for (i = 0; i < NET_STACK_MAX; i++)
+		if (!stack_ops[i].depth) {
+			stack_ops[i].pid = pid;
+			return (int)i;
+		}
+	return -1;
+}
+
+void net_stack_enter(void)
+{
+	struct process *p = current_proc();
+	int slot;
+
+	if (!p)
+		return;
+	slot = stack_slot(p->pid);
+	if (slot >= 0)
+		stack_ops[slot].depth++;
+}
+
+void net_stack_leave(void)
+{
+	struct process *p = current_proc();
+	int slot;
+
+	if (!p)
+		return;
+	slot = stack_slot(p->pid);
+	if (slot >= 0 && stack_ops[slot].depth)
+		stack_ops[slot].depth--;
+}
+
+int net_stack_busy(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < NET_STACK_MAX; i++)
+		if (stack_ops[i].depth)
+			return 1;
+	return 0;
+}
+
+void net_stack_reap(unsigned int pid)
+{
+	unsigned int i;
+
+	for (i = 0; i < NET_STACK_MAX; i++)
+		if (stack_ops[i].pid == pid)
+			stack_ops[i].depth = 0;
+}
+
+unsigned int net_irq_enter(void)
+{
+	unsigned int flags = spin_irq_save_disable();
+
+	__asm__ volatile ("sti");
+	return flags;
+}
+
+void net_irq_exit(unsigned int flags)
+{
+	spin_irq_restore(flags);
+}
+
+int net_pump(unsigned int budget)
+{
+	const struct netdev *nd = netdev_active();
+	unsigned char *rx;
+	unsigned int n, flags;
+
+	if (!nd)
+		return 0;
+	if (netdev_in_use())
+		return 0;
+	rx = netdev_rx_scratch();
+	flags = net_irq_enter();
+
+	for (n = 0; n < budget; n++) {
+		int len = nd->rx(rx, NETDEV_FRAME_MAX);
+
+		if (len < 0)
+			break;
+		if (tcp_client_input(rx, (unsigned int)len))
+			continue;
+		if (tcp_srv_input(rx, (unsigned int)len))
+			continue;
+		if (dns_input(rx, (unsigned int)len))
+			continue;
+		if (netkexec_capture_frame(rx, (unsigned int)len))
+			continue;
+		net_arp_input(rx, (unsigned int)len);
+		{
+			int rlen = net_build_reply(rx, (unsigned int)len,
+						   reply_buf, sizeof(reply_buf));
+
+			if (rlen > 0)
+				nd->tx(reply_buf, (unsigned int)rlen);
+		}
+	}
+	net_irq_exit(flags);
+	return (int)n;
+}

+ 36 - 0
netcore.h

@@ -0,0 +1,36 @@
+/* Shared kernel receive pump for the network stack.
+ *
+ * Packet-level commands still take an exclusive raw lease through
+ * netdev_acquire().  TCP clients no longer take that lease: every socket
+ * syscall drains the active NIC through net_pump(), which dispatches each
+ * frame to the TCP client table, the TCP listener, a pending DNS query, the
+ * ARP cache and the netkexec watcher, and answers ARP/ICMP.
+ *
+ * net_pump() may only be called with the BKL held: it serializes every stack
+ * user against the same NIC rings, the shared receive scratch and the
+ * protocol state.  It temporarily enables interrupts (the driver TX timeout
+ * paths spin on get_ticks_ms()) and restores the caller's IF at the end.
+ */
+#ifndef NETCORE_H
+#define NETCORE_H
+
+/* Drain at most `budget` frames and dispatch them.  Returns the number of
+ * frames consumed.  No-op while a packet-level command owns the NIC. */
+int net_pump(unsigned int budget);
+
+/* Save/disable interrupts, then enable them.  Paired with net_irq_exit().
+ * Used around driver calls in the cooperative stack paths so a stuck TX
+ * descriptor can still time out on the PIT. */
+unsigned int net_irq_enter(void);
+void net_irq_exit(unsigned int flags);
+
+/* Long-running stack operations (DNS/ARP waits, TCP connects) keep the
+ * background netkexec watcher from polling the active NIC and stealing their
+ * frames.  The counter is protected by the BKL. */
+void net_stack_enter(void);
+void net_stack_leave(void);
+int  net_stack_busy(void);
+/* Reaper: forget a dead process's stack-operation counts. */
+void net_stack_reap(unsigned int pid);
+
+#endif

+ 2 - 1
netdev.c

@@ -8,6 +8,7 @@
 #include "net.h"
 #include "process.h"
 #include "r8168.h"
+#include "tcp.h"
 static const struct netdev *const netdev_drivers[] = {
 	/* e1000e is the active test NIC; the ASIX is kept as the management
 	 * link by netdev_mgmt() even while e1000e is active. */
@@ -155,7 +156,7 @@ int netdev_select(const char *name)
 {
 	unsigned int i;
 
-	if (!name || !*name || busy)
+	if (!name || !*name || busy || tcp_client_count() != 0)
 		return -1;
 	while (*name == ' ')
 		name++;

+ 9 - 1
netkexec.c

@@ -23,7 +23,9 @@
 #include "io.h"
 #include "kexec.h"
 #include "net.h"
+#include "netcore.h"
 #include "netdev.h"
+#include "tcp.h"
 #include "process.h"
 #include "serial.h"
 #include "timer.h"
@@ -164,7 +166,8 @@ int netkexec_pending(void)
 {
 	const struct netdev *mgmt, *act;
 
-	if (kexec_busy || netdev_in_use())
+	if (kexec_busy || netdev_in_use() || tcp_client_count() != 0 ||
+	    net_stack_busy())
 		return 0;
 	if (have_header)
 		return 1;
@@ -339,6 +342,11 @@ void program_netkexec_main(void)
 		kexec_busy = 0;
 		return;
 	}
+	if (tcp_client_count() != 0) {
+		io_putstr("netkexec: NIC busy (close network clients first)\n");
+		kexec_busy = 0;
+		return;
+	}
 	have_header = 0;
 	netdev_acquire();
 

+ 4 - 1
netlog.c

@@ -1,7 +1,9 @@
 /* netlog: see netlog.h. */
 
 #include "net.h"
+#include "netcore.h"
 #include "netdev.h"
+#include "tcp.h"
 #include "timer.h"
 #include "netlog.h"
 
@@ -101,7 +103,8 @@ void netlog_flush(void)
 	static const char dropmsg[] = "[netlog: output dropped, buffer full]\n";
 	unsigned int i;
 
-	if (!enabled || netdev_in_use() || !netdev_mgmt())
+	if (!enabled || netdev_in_use() || tcp_client_count() != 0 ||
+	    net_stack_busy() || !netdev_mgmt())
 		return;
 
 	avail = pending();

+ 48 - 51
sys_net.c

@@ -3,16 +3,20 @@
  * The stack (net.c/dns.c/tcp.c) and the NIC drivers stay in the kernel; user
  * commands get the configuration, the DNS/DHCP clients and the raw frame
  * paths, so the packet-level commands (ping, pong, netrecv, ...) port over
- * their existing frame code.  The receiver is owned by one process at a time
- * (netdev_acquire/release): the background kexec watcher pauses while a
- * command owns it, and the reaper force-releases it if that process dies
- * (sys_net_reap). */
+ * their existing frame code.
+ *
+ * Packet-level commands take an exclusive raw lease (netdev_acquire); the
+ * background kexec watcher pauses while one owns the receiver, and the reaper
+ * releases it if that process dies.  TCP clients do NOT take that lease: each
+ * process may hold one connection in tcp.c, and every stack syscall drives
+ * the shared net_pump() so several apps can transfer concurrently. */
 
 #include "syscall.h"
 #include "dhcp.h"
 #include "dns.h"
 #include "io.h"
 #include "net.h"
+#include "netcore.h"
 #include "netdev.h"
 #include "netkexec.h"
 #include "netlog.h"
@@ -21,10 +25,6 @@
 #include "tcp.h"
 #include "timer.h"
 
-/* One TCP connection at a time (the stack is single-connection, and the
- * receiver is owned by the process using it). */
-static struct tcp_conn tcp;
-
 /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
 static struct tcp_conn *srv_conn;
 
@@ -65,6 +65,9 @@ void sys_net_reap(unsigned int pid)
 		tcp_srv_reset();
 		srv_conn = 0;
 	}
+	tcp_client_reap(pid);
+	dns_reap(pid);
+	net_stack_reap(pid);
 }
 
 int sys_net_info(struct sys_netinfo *out)
@@ -97,7 +100,7 @@ int sys_net_print_info(void)
 {
 	const struct netdev *nd = netdev_active();
 
-	if (!nd || !nd->print_info)
+	if (!nd || !nd->print_info || tcp_client_count() != 0)
 		return -1;
 	/* print_info reads the PHY via MDIC and can wait on the PHY/ME
 	 * semaphore; the syscall stub entered with interrupts off. */
@@ -117,7 +120,9 @@ int sys_net_acquire(void)
 {
 	if (owns_nic())
 		return 0;
-	if (netdev_in_use())
+	/* Raw commands consume every frame, so they cannot coexist with a
+	 * shared TCP client (whose data the pump must dispatch). */
+	if (netdev_in_use() || tcp_client_count() != 0)
 		return -1;
 	netdev_acquire();
 	return 0;
@@ -191,7 +196,7 @@ int sys_net_regs(void)
 {
 	const struct netdev *nd = netdev_active();
 
-	if (!nd || !nd->dump_regs)
+	if (!nd || !nd->dump_regs || tcp_client_count() != 0)
 		return -1;
 	net_enter();
 	nd->dump_regs();
@@ -205,6 +210,9 @@ int sys_net_reset(void)
 
 	if (!nd || !nd->reset)
 		return -1;
+	if (tcp_client_count() != 0 ||
+	    (netdev_in_use() && !owns_nic()))
+		return -1;
 	/* Own the receiver for the whole reset, so the netlog kworker cannot
 	 * submit frames while the MAC and rings are being reinitialised. */
 	netdev_acquire();
@@ -253,6 +261,8 @@ int sys_dhcp_run(unsigned int timeout_ms)
 {
 	int r;
 
+	if (netdev_in_use() || tcp_client_count() != 0)
+		return -1;
 	net_enter();
 	r = dhcp_run(timeout_ms);
 	net_exit();
@@ -268,12 +278,17 @@ int sys_dns_resolve(const char *name, unsigned char ip[4])
 {
 	int r;
 
-	if (!owns_nic())
+	/* A raw owner keeps its direct poll path; stack users share the pump.
+	 * Refuse only when another process owns the raw lease. */
+	if (netdev_in_use() && !owns_nic())
 		return -1;
-	net_enter();
-	r = dns_resolve(name, ip);
-	net_exit();
-	return r;
+	if (owns_nic()) {
+		net_enter();
+		r = dns_resolve(name, ip);
+		net_exit();
+		return r;
+	}
+	return dns_resolve(name, ip);
 }
 
 /* SNTP sync for ring-3 callers.  The service owns the receiver for the
@@ -291,6 +306,8 @@ int sys_ntp_sync(const char *server, unsigned int timeout_ms,
 	if (netdev_in_use()) {
 		if (netdev_owner() != pid)
 			return SYS_NTP_EBUSY;
+	} else if (tcp_client_count() != 0) {
+		return SYS_NTP_EBUSY;
 	} else {
 		netdev_acquire();
 		acquired = 1;
@@ -309,52 +326,28 @@ int sys_ntp_sync(const char *server, unsigned int timeout_ms,
 int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
 		    unsigned int port)
 {
-	int r;
-
-	if (!owns_nic())
-		return -3;
-	net_enter();
-	r = tcp_connect(mac, ip, port, &tcp);
-	net_exit();
-	return r;
+	return tcp_client_connect(caller_pid(), mac, ip, port);
 }
 
 int sys_tcp_peer_mss(void)
 {
-	return (int)tcp.peer_mss;
+	return tcp_client_peer_mss(caller_pid());
 }
 
 int sys_tcp_send(const void *buf, unsigned int len)
 {
-	int r;
-
-	if (!owns_nic())
-		return -3;
-	net_enter();
-	r = tcp_send(&tcp, buf, len);
-	net_exit();
-	return r;
+	return tcp_client_send(caller_pid(), buf, len);
 }
 
 int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
 {
-	int r;
-
-	if (!owns_nic())
-		return -2;
-	net_enter();
-	r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
-	net_exit();
-	return r;
+	return tcp_client_recv(caller_pid(), buf, max,
+			       get_ticks_ms() + timeout_ms);
 }
 
 void sys_tcp_close(void)
 {
-	if (!owns_nic())
-		return;
-	net_enter();
-	tcp_close(&tcp);
-	net_exit();
+	tcp_client_close(caller_pid());
 }
 
 /* ---- TCP server ---- */
@@ -470,12 +463,16 @@ int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
 {
 	int r;
 
-	if (!owns_nic())
+	/* Raw owners keep the direct poll path; stack users share the pump. */
+	if (netdev_in_use() && !owns_nic())
 		return -2;
-	net_enter();
-	r = net_arp_resolve(ip, mac);
-	net_exit();
-	return r;
+	if (owns_nic()) {
+		net_enter();
+		r = net_arp_resolve(ip, mac);
+		net_exit();
+		return r;
+	}
+	return net_arp_resolve(ip, mac);
 }
 
 int sys_net_configured(void)

+ 358 - 144
tcp.c

@@ -2,8 +2,10 @@
 
 #include "io.h"
 #include "timer.h"
+#include "netcore.h"
 #include "netdev.h"
 #include "net.h"
+#include "process.h"
 #include "tcp.h"
 
 /* TCP flags. */
@@ -18,6 +20,11 @@
 #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 6
+#define TCP_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. */
 static unsigned int srv_port;
@@ -26,38 +33,43 @@ static unsigned int srv_port;
  * receive side uses the shared frame scratch (netdev_rx_scratch()). */
 static unsigned char tx_frame[14 + 20 + 20 + TCP_MSS_OURS];
 
-/* Receive queue: in-order payload accepted from the peer but not yet handed
- * to the application.  Data can arrive while the app is inside tcp_send()
- * (a TLS server sends session tickets right after the handshake), so it must
- * be buffered, never ACKed and dropped.  A segment that does not fit is left
- * unacknowledged and the peer retransmits it. */
-#define TCP_RXQ_SIZE 8192
-static unsigned char rxq[TCP_RXQ_SIZE];
-static unsigned int rxq_head, rxq_tail;
+/* In-order payload accepted from the peer but not yet handed to the
+ * application.  Every connection owns a queue, so one socket's data can
+ * never be read or reset by another.  Data can arrive while the app is inside
+ * a send (a TLS server sends session tickets right after the handshake), so
+ * it must be buffered, never ACKed and dropped.  A segment that does not fit
+ * is left unacknowledged and the peer retransmits it. */
+void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size)
+{
+	c->rxq = buf;
+	c->rxq_mask = size - 1;
+	c->rxq_head = c->rxq_tail = 0;
+}
 
-static unsigned int rxq_len(void)
+static unsigned int rxq_len(const struct tcp_conn *c)
 {
-	return (rxq_head - rxq_tail) & (TCP_RXQ_SIZE - 1);
+	return (c->rxq_head - c->rxq_tail) & c->rxq_mask;
 }
 
-static unsigned int rxq_space(void)
+static unsigned int rxq_space(const struct tcp_conn *c)
 {
-	return TCP_RXQ_SIZE - 1 - rxq_len();
+	return c->rxq_mask - rxq_len(c);
 }
 
-static void rxq_put(const unsigned char *p, unsigned int n)
+static void rxq_put(struct tcp_conn *c, const unsigned char *p,
+		    unsigned int n)
 {
 	while (n--) {
-		rxq[rxq_head] = *p++;
-		rxq_head = (rxq_head + 1) & (TCP_RXQ_SIZE - 1);
+		c->rxq[c->rxq_head] = *p++;
+		c->rxq_head = (c->rxq_head + 1) & c->rxq_mask;
 	}
 }
 
-static void rxq_get(unsigned char *p, unsigned int n)
+static void rxq_get(struct tcp_conn *c, unsigned char *p, unsigned int n)
 {
 	while (n--) {
-		*p++ = rxq[rxq_tail];
-		rxq_tail = (rxq_tail + 1) & (TCP_RXQ_SIZE - 1);
+		*p++ = c->rxq[c->rxq_tail];
+		c->rxq_tail = (c->rxq_tail + 1) & c->rxq_mask;
 	}
 }
 
@@ -158,7 +170,7 @@ 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();
+	unsigned int wnd = with_mss ? TCP_WINDOW : rxq_space(c);
 
 	return tcp_send_segment_wnd(c, flags, seq, ack, payload, len,
 				    with_mss, wnd);
@@ -266,8 +278,8 @@ static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
 		c->peer_wnd = rd16(s->tcp + 14);
 
 	if (s->len) {
-		if (s->seq == c->rcv_nxt && s->len <= rxq_space()) {
-			rxq_put(s->data, s->len);
+		if (s->seq == c->rcv_nxt && s->len <= rxq_space(c)) {
+			rxq_put(c, s->data, s->len);
 			c->rcv_nxt += s->len;
 			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
 					 0, 0, 0);
@@ -286,132 +298,377 @@ static int tcp_rx_data(struct tcp_conn *c, const struct tcp_seg *s)
 	return 0;
 }
 
-int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
-		unsigned int dport, struct tcp_conn *c)
+/* ---- Client table ---- */
+
+struct tcp_client {
+	struct tcp_conn c;
+	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];
+static unsigned int next_sport = 0xC000;
+
+static struct tcp_client *client_find(unsigned int pid)
+{
+	unsigned int i;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++)
+		if (clients[i].used && clients[i].owner == pid)
+			return &clients[i];
+	return 0;
+}
+
+int tcp_client_count(void)
+{
+	unsigned int i, n = 0;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++)
+		if (clients[i].used)
+			n++;
+	return (int)n;
+}
+
+static int ip4_eq(const unsigned char *a, const unsigned char *b)
+{
+	return a[0] == b[0] && a[1] == b[1] &&
+	       a[2] == b[2] && a[3] == b[3];
+}
+
+static unsigned int alloc_sport(const unsigned char *dst_ip,
+				unsigned int dport)
+{
+	unsigned int i, guard = 0;
+
+	for (;;) {
+		unsigned int sport = next_sport++;
+		int used = 0;
+
+		if (next_sport < 0xC000 || next_sport > 0xFFFE)
+			next_sport = 0xC000;
+		for (i = 0; i < TCP_CLIENT_MAX; i++) {
+			if (!clients[i].used)
+				continue;
+			if (clients[i].c.sport == sport &&
+			    clients[i].c.dport == dport &&
+			    ip4_eq(clients[i].c.dst_ip, dst_ip)) {
+				used = 1;
+				break;
+			}
+		}
+		if (!used || ++guard > 0x3FFF)
+			return sport;
+	}
+}
+
+static struct tcp_client *client_alloc(unsigned int pid)
+{
+	unsigned int i;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++) {
+		if (clients[i].used)
+			continue;
+		clients[i].used = 1;
+		clients[i].owner = pid;
+		clients[i].err = 0;
+		return &clients[i];
+	}
+	return 0;
+}
+
+static void client_free(struct tcp_client *cl)
+{
+	cl->used = 0;
+	cl->owner = 0;
+	cl->err = 0;
+	cl->c.state = TCP_STATE_CLOSED;
+}
+
+/* Feed one frame to the client table.  See tcp.h. */
+int tcp_client_input(const unsigned char *frame, unsigned int len)
+{
+	unsigned int i;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++) {
+		struct tcp_client *cl = &clients[i];
+		struct tcp_conn *c = &cl->c;
+		struct tcp_seg s;
+
+		if (!cl->used)
+			continue;
+		if (!tcp_parse(frame, len, c->dst_ip, c->dport, c->sport, &s))
+			continue;
+
+		if (s.flags & TCP_RST) {
+			c->state = TCP_STATE_CLOSED;
+			cl->err = -2;
+			return 1;
+		}
+
+		if (c->state == TCP_STATE_SYN_SENT) {
+			if ((s.flags & (TCP_SYN | TCP_ACK)) ==
+			    (TCP_SYN | TCP_ACK) &&
+			    s.ack == c->snd_nxt + 1) {
+				unsigned int mss;
+
+				c->rcv_nxt = s.seq + 1;
+				c->snd_nxt++;
+				c->snd_una = c->snd_nxt;
+				c->peer_wnd = rd16(s.tcp + 14);
+				if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
+				    mss >= 64 && mss <= TCP_MSS_OURS)
+					c->peer_mss = mss;
+				tcp_send_segment(c, TCP_ACK, c->snd_nxt,
+						 c->rcv_nxt, 0, 0, 0);
+				c->state = TCP_STATE_ESTABLISHED;
+				cl->err = 0;
+			}
+			return 1;
+		}
+
+		if (c->state != TCP_STATE_ESTABLISHED &&
+		    c->state != TCP_STATE_FIN_WAIT)
+			return 1;
+
+		if (c->need_wnd && (s.flags & TCP_ACK)) {
+			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
+					 0, 0, 0);
+			c->need_wnd = 0;
+		}
+		if ((unsigned)(s.ack - c->snd_una) <=
+		    (unsigned)(c->snd_nxt - c->snd_una))
+			c->snd_una = s.ack;
+		c->peer_wnd = rd16(s.tcp + 14);
+		if (tcp_rx_data(c, &s) == -2) {
+			c->state = TCP_STATE_CLOSED;
+			cl->err = -2;
+		}
+		return 1;
+	}
+	return 0;
+}
+
+int tcp_client_connect(unsigned int pid, const unsigned char mac[6],
+		       const unsigned char ip[4], unsigned int port)
 {
 	const struct netdev *nd = netdev_active();
-	unsigned char *rxbuf = netdev_rx_scratch();
+	struct tcp_client *cl;
+	struct tcp_conn *c;
 	unsigned int tries, i;
 
+	if (client_find(pid))
+		return -4;
+	if (netdev_in_use())
+		return -4;
 	if (!nd || !net_is_configured())
 		return -3;
 
+	cl = client_alloc(pid);
+	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] = dst_mac[i];
+		c->dst_mac[i] = mac[i];
 	for (i = 0; i < 4; i++)
-		c->dst_ip[i] = dst_ip[i];
-	c->dport = dport;
-	c->sport = 0xC000u | (get_ticks_ms() & 0x3FFFu);
-	c->snd_una = c->snd_nxt = (get_ticks_ms() << 16) ^ 0x5EED0000u;
+		c->dst_ip[i] = ip[i];
+	c->dport = port;
+	c->sport = alloc_sport(ip, port);
+	c->snd_una = c->snd_nxt =
+		(get_ticks_ms() << 16) ^ (c->sport << 8) ^ 0x5EED0000u;
 	c->rcv_nxt = 0;
 	c->peer_mss = 536;
 	c->peer_wnd = 4096;
 	c->peer_fin = 0;
 	c->need_wnd = 0;
-	rxq_head = rxq_tail = 0;
+	c->state = TCP_STATE_SYN_SENT;
+	cl->err = 0;
 
 	for (tries = 0; tries < TCP_RETRIES; tries++) {
 		unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
-
-		if (tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1) != 0)
+		unsigned int flags;
+		int r;
+
+		flags = net_irq_enter();
+		r = tcp_send_segment(c, TCP_SYN, c->snd_nxt, 0, 0, 0, 1);
+		net_irq_exit(flags);
+		if (r != 0) {
+			client_free(cl);
 			return -3;
+		}
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			struct tcp_seg s;
-			int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
-
-			if (len < 0)
-				continue;
-			if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
-				       c->dport, c->sport, &s))
-				continue;
-			if (s.flags & TCP_RST)
-				return -2;
-			if ((s.flags & (TCP_SYN | TCP_ACK)) ==
-			    (TCP_SYN | TCP_ACK) && s.ack == c->snd_nxt + 1) {
-				unsigned int mss;
-
-				c->rcv_nxt = s.seq + 1;
-				c->snd_nxt++;
-				c->snd_una = c->snd_nxt;
-				c->peer_wnd = rd16(s.tcp + 14);
-				if (tcp_parse_mss(s.tcp, s.doff, &mss) &&
-				    mss >= 64 && mss <= TCP_MSS_OURS)
-					c->peer_mss = mss;
-				if (tcp_send_segment(c, TCP_ACK, c->snd_nxt,
-						     c->rcv_nxt, 0, 0, 0) != 0)
-					return -3;
+			net_pump(8);
+			if (c->state == TCP_STATE_ESTABLISHED)
 				return 0;
+			if (c->state == TCP_STATE_CLOSED) {
+				r = cl->err ? cl->err : -2;
+				client_free(cl);
+				return r;
 			}
+			process_sleep_ms(1);
 		}
 	}
+	client_free(cl);
 	return -1;
 }
 
-int tcp_send(struct tcp_conn *c, const void *data, unsigned int len)
+int tcp_client_send(unsigned int pid, const void *buf, unsigned int len)
 {
-	const struct netdev *nd = netdev_active();
-	unsigned char *rxbuf = netdev_rx_scratch();
+	struct tcp_client *cl = client_find(pid);
+	struct tcp_conn *c;
 	unsigned int tries;
 
-	if (!nd || len > c->peer_mss)
+	if (!cl)
+		return -4;
+	c = &cl->c;
+	if (c->state != TCP_STATE_ESTABLISHED)
+		return -4;
+	if (len > c->peer_mss)
 		return -3;
 
 	for (tries = 0; tries < TCP_RETRIES; tries++) {
+		unsigned int seq = c->snd_nxt;
 		unsigned int deadline = get_ticks_ms() + TCP_RETRY_MS;
-
-		if (tcp_send_segment(c, TCP_PSH | TCP_ACK, c->snd_nxt,
-				     c->rcv_nxt, (const unsigned char *)data,
-				     len, 0) != 0)
+		unsigned int flags;
+		int r;
+
+		flags = net_irq_enter();
+		r = tcp_send_segment(c, TCP_PSH | TCP_ACK, seq, c->rcv_nxt,
+				     (const unsigned char *)buf, len, 0);
+		net_irq_exit(flags);
+		if (r != 0)
 			return -3;
+		c->snd_nxt += len;
 
 		while ((int)(get_ticks_ms() - deadline) < 0) {
-			struct tcp_seg s;
-			int rlen = nd->rx(rxbuf, NETDEV_FRAME_MAX);
-
-			if (rlen < 0)
-				continue;
-			if (!tcp_parse(rxbuf, (unsigned int)rlen, c->dst_ip,
-				       c->dport, c->sport, &s))
-				continue;
-			if (s.flags & TCP_RST)
-				return -2;
-			if (s.ack == c->snd_nxt + len) {
-				c->snd_una = s.ack;
-				c->snd_nxt = s.ack;
+			net_pump(8);
+			if (c->state == TCP_STATE_CLOSED)
+				return cl->err ? cl->err : -2;
+			if ((int)(c->snd_una - c->snd_nxt) >= 0)
 				return 0;
-			}
-			/* Data arriving while we are sending: buffer it for
-			 * tcp_recv (never ACK and drop). */
-			if (tcp_rx_data(c, &s) == -2)
-				return -2;
+			process_sleep_ms(1);
 		}
+		/* Retransmit the whole segment; a duplicate is harmless. */
 	}
 	return -1;
 }
 
+int tcp_client_recv(unsigned int pid, void *vbuf, unsigned int max,
+		    unsigned int deadline_ms)
+{
+	struct tcp_client *cl = client_find(pid);
+	struct tcp_conn *c;
+	unsigned char *buf = (unsigned char *)vbuf;
+
+	if (!cl)
+		return -4;
+	c = &cl->c;
+
+	for (;;) {
+		if (rxq_len(c)) {
+			unsigned int n = rxq_len(c);
+			unsigned int flags;
+
+			if (n > max)
+				n = max;
+			rxq_get(c, buf, n);
+			flags = net_irq_enter();
+			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
+					 0, 0, 0);
+			net_irq_exit(flags);
+			return (int)n;
+		}
+		if (c->peer_fin)
+			return 0;
+		if (c->state == TCP_STATE_CLOSED)
+			return cl->err ? cl->err : -2;
+		if ((int)(get_ticks_ms() - deadline_ms) >= 0)
+			return -1;
+		net_pump(8);
+		if (!rxq_len(c) && c->state != TCP_STATE_CLOSED)
+			process_sleep_ms(1);
+	}
+}
+
+void tcp_client_close(unsigned int pid)
+{
+	struct tcp_client *cl = client_find(pid);
+	struct tcp_conn *c;
+
+	if (!cl)
+		return;
+	c = &cl->c;
+	if (c->state == TCP_STATE_ESTABLISHED) {
+		unsigned int deadline = get_ticks_ms() + 500;
+		unsigned int flags;
+
+		flags = net_irq_enter();
+		tcp_send_segment(c, TCP_FIN | TCP_ACK, c->snd_nxt,
+				 c->rcv_nxt, 0, 0, 0);
+		net_irq_exit(flags);
+		c->snd_nxt++;
+		c->state = TCP_STATE_FIN_WAIT;
+		while ((int)(get_ticks_ms() - deadline) < 0) {
+			net_pump(4);
+			if (c->peer_fin || c->state == TCP_STATE_CLOSED)
+				break;
+			process_sleep_ms(1);
+		}
+	}
+	client_free(cl);
+}
+
+int tcp_client_peer_mss(unsigned int pid)
+{
+	struct tcp_client *cl = client_find(pid);
+
+	return cl ? (int)cl->c.peer_mss : -1;
+}
+
+void tcp_client_reap(unsigned int pid)
+{
+	unsigned int i;
+
+	for (i = 0; i < TCP_CLIENT_MAX; i++) {
+		struct tcp_client *cl = &clients[i];
+		struct tcp_conn *c = &cl->c;
+
+		if (!cl->used || cl->owner != pid)
+			continue;
+		if (c->state != TCP_STATE_CLOSED) {
+			unsigned int flags = net_irq_enter();
+
+			tcp_send_segment(c, TCP_RST | TCP_ACK, c->snd_nxt,
+					 c->rcv_nxt, 0, 0, 0);
+			net_irq_exit(flags);
+		}
+		client_free(cl);
+	}
+}
+
+/* Generic receive used by the WebDAV server, which owns the raw NIC lease
+ * and therefore polls the device directly instead of net_pump(). */
 int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 	     unsigned int deadline_ms)
 {
 	const struct netdev *nd = netdev_active();
 	unsigned char *rxbuf = netdev_rx_scratch();
 
-	if (!nd)
+	if (!nd || !c || !c->rxq)
 		return -2;
 
-	/* Deliver what is already queued (may span several segments). */
 	for (;;) {
 		struct tcp_seg s;
 
-		if (rxq_len()) {
-			unsigned int n = rxq_len();
+		if (rxq_len(c)) {
+			unsigned int n = rxq_len(c);
 
 			if (n > maxlen)
 				n = maxlen;
-			rxq_get(out, n);
-			/* Window update: ACKs sent while the queue was full may
-			 * have advertised zero, and without this the peer would
-			 * only recover on its next zero-window probe. */
+			rxq_get(c, out, n);
 			tcp_send_segment(c, TCP_ACK, c->snd_nxt, c->rcv_nxt,
 					 0, 0, 0);
 			return (int)n;
@@ -428,16 +685,12 @@ int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 				continue;
 			if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
 				       c->dport, c->sport, &s)) {
-				/* A new connection (or its first data) while
-				 * this one is being served. */
 				if (srv_port)
 					tcp_srv_input(rxbuf, (unsigned int)len);
 				continue;
 			}
 		}
 
-		/* Track our send window (nothing pending here, but keeps
-		 * snd_una meaningful for the ACKs we emit). */
 		if ((unsigned)(s.ack - c->snd_una) <=
 		    (unsigned)(c->snd_nxt - c->snd_una))
 			c->snd_una = s.ack;
@@ -446,55 +699,11 @@ int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
 					 0, 0, 0);
 			c->need_wnd = 0;
 		}
-
 		if (tcp_rx_data(c, &s) == -2)
 			return -2;
 	}
 }
 
-void tcp_close(struct tcp_conn *c)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned char *rxbuf = netdev_rx_scratch();
-	unsigned int fin_seq = c->snd_nxt;
-	unsigned int tries;
-
-	if (!nd)
-		return;
-
-	for (tries = 0; tries < 2; tries++) {
-		unsigned int deadline = get_ticks_ms() + 500;
-		struct tcp_seg s;
-
-		if (tcp_send_segment(c, TCP_FIN | TCP_ACK, fin_seq,
-				     c->rcv_nxt, 0, 0, 0) != 0)
-			return;
-		c->snd_nxt = fin_seq + 1;
-
-		while ((int)(get_ticks_ms() - deadline) < 0) {
-			int len = nd->rx(rxbuf, NETDEV_FRAME_MAX);
-
-			if (len < 0)
-				continue;
-			if (!tcp_parse(rxbuf, (unsigned int)len, c->dst_ip,
-				       c->dport, c->sport, &s))
-				continue;
-			if (s.flags & TCP_RST)
-				return;
-			/* Our FIN acked (or their FIN first). */
-			if ((s.flags & TCP_FIN) && s.seq == c->rcv_nxt) {
-				c->rcv_nxt++;
-				c->peer_fin = 1;
-				tcp_send_segment(c, TCP_ACK, c->snd_nxt,
-						 c->rcv_nxt, 0, 0, 0);
-				return;
-			}
-			if (s.ack == c->snd_nxt)
-				return;
-		}
-	}
-}
-
 /* ---- Server side ----
  *
  * One listening port, one active connection, two slots that hold a
@@ -527,6 +736,9 @@ struct tcp_srv {
 
 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];
 
 struct tcp_rx {
 	unsigned int seq, ack;
@@ -722,7 +934,7 @@ static void srv_send_rst(const struct tcp_rx *s)
 	c.rcv_nxt = ack;
 	c.peer_mss = 536;
 	c.peer_fin = 0;
-	tcp_send_segment(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0);
+	tcp_send_segment_wnd(&c, TCP_RST | TCP_ACK, s->ack, ack, 0, 0, 0, 0);
 }
 
 static void srv_rx_data(struct tcp_conn *c, const struct tcp_rx *r)
@@ -743,8 +955,11 @@ void tcp_srv_init(unsigned int port)
 {
 	int i;
 
-	for (i = 0; i < TCP_SRV_SLOTS; i++)
-		srv[i].used = 0, srv[i].state = SRV_FREE;
+	for (i = 0; i < TCP_SRV_SLOTS; i++) {
+		srv[i].used = 0;
+		srv[i].state = SRV_FREE;
+		tcp_conn_init(&srv[i].c, srv_rxq, sizeof(srv_rxq));
+	}
 	srv_active = -1;
 	srv_port = port;
 }
@@ -752,7 +967,6 @@ void tcp_srv_init(unsigned int port)
 void tcp_srv_reset(void)
 {
 	tcp_srv_init(0);
-	rxq_head = rxq_tail = 0;
 }
 
 int tcp_srv_input(const unsigned char *f, unsigned int len)
@@ -869,9 +1083,9 @@ struct tcp_conn *tcp_srv_accept(void)
 
 	srv[best].state = SRV_ACTIVE;
 	srv_active = best;
-	rxq_head = rxq_tail = 0;
+	srv[best].c.rxq_head = srv[best].c.rxq_tail = 0;
 	if (srv[best].pre_len) {
-		rxq_put(srv[best].pre, srv[best].pre_len);
+		rxq_put(&srv[best].c, srv[best].pre, srv[best].pre_len);
 		srv[best].pre_len = 0;
 	}
 	/* The SYN-ACK advertised a zero window while this connection waited

+ 49 - 22
tcp.h

@@ -1,12 +1,22 @@
-/* Minimal TCP client (RFC 793 subset) for outgoing connections.
+/* Minimal TCP (RFC 793 subset) for outgoing client connections and the
+ * single WebDAV listener.
  *
- * Single connection, in-order receive only, no congestion control, no
- * window scaling, no out-of-order reassembly, no fragmentation.  Everything
- * is polled from the netdev rx() call, matching the rest of the kernel.
- * The caller owns the NIC (netdev_acquire) from connect until close. */
+ * 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.
+ *
+ * Server side: one listening port, one active connection and a small SYN
+ * backlog (see the WebDAV command); the caller owns the raw NIC lease.
+ */
 #ifndef TCP_H
 #define TCP_H
 
+#define TCP_STATE_CLOSED      0
+#define TCP_STATE_SYN_SENT    1
+#define TCP_STATE_ESTABLISHED 2
+#define TCP_STATE_FIN_WAIT    3
+
 struct tcp_conn {
 	unsigned char dst_mac[6];
 	unsigned char dst_ip[4];
@@ -18,34 +28,46 @@ struct tcp_conn {
 	unsigned int  peer_wnd;   /* peer's advertised receive window (raw) */
 	int           peer_fin;   /* peer FIN consumed (EOF) */
 	int           need_wnd;   /* re-send the window update after the final ACK */
+	int           state;      /* TCP_STATE_* */
+	unsigned char *rxq;       /* receive queue (power-of-two size) */
+	unsigned int  rxq_mask;
+	unsigned int  rxq_head, rxq_tail;
 };
 
-/* Open a connection (SYN -> SYN-ACK -> ACK).  Returns:
- *   0 = established,
- *  -1 = no reply (timeout),
- *  -2 = refused (RST),
- *  -3 = transmit failed / no NIC / no IP. */
-int tcp_connect(const unsigned char *dst_mac, const unsigned char *dst_ip,
-		unsigned int dport, struct tcp_conn *c);
+/* Initialise a connection's receive queue.  `size` must be a power of two. */
+void tcp_conn_init(struct tcp_conn *c, unsigned char *buf, unsigned int size);
 
-/* Send one segment (len must be <= peer_mss), retransmitting until acked.
- * Returns 0, or -1 timeout / -2 reset / -3 transmit failed. */
-int tcp_send(struct tcp_conn *c, const void *data, unsigned int len);
+/* ---- Client table (one connection per process) ----
+ *
+ * `pid` is the calling process.  Returns:
+ *   connect: 0 established, -1 timeout, -2 refused/reset, -3 no NIC/tx,
+ *            -4 busy (raw NIC owner) or no free slot / already connected.
+ *   send:    0 / -1 timeout / -2 reset / -3 transmit or bad length /
+ *            -4 no socket.
+ *   recv:    >0 bytes, 0 EOF, -1 timeout, -2 reset/-4 no socket.
+ */
+int  tcp_client_connect(unsigned int pid, const unsigned char mac[6],
+			const unsigned char ip[4], unsigned int port);
+int  tcp_client_send(unsigned int pid, const void *buf, unsigned int len);
+int  tcp_client_recv(unsigned int pid, void *buf, unsigned int max,
+		     unsigned int deadline_ms);
+void tcp_client_close(unsigned int pid);
+int  tcp_client_peer_mss(unsigned int pid);
 
-/* Read the next in-order payload bytes into out.  Returns >0 = byte count,
- * 0 = peer closed (EOF), -1 = deadline passed, -2 = reset. */
-int tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
-	     unsigned int deadline_ms);
+/* Feed one frame from net_pump() to the client table.  Returns 1 when it
+ * matched a client connection (consumed), 0 otherwise. */
+int  tcp_client_input(const unsigned char *frame, unsigned int len);
 
-/* Send FIN and drain the close handshake briefly (best effort). */
-void tcp_close(struct tcp_conn *c);
+/* Reaper: close every socket owned by `pid` without waiting. */
+void tcp_client_reap(unsigned int pid);
+int  tcp_client_count(void);
 
 /* ---- Server side (WebDAV listener) ----
  *
  * One listening port, one active connection and a small SYN backlog.  The
  * caller owns the NIC.  Incoming frames are fed to tcp_srv_input() (which
  * also answers SYNs and RSTs unknown ports); tcp_srv_accept() promotes a
- * backlog connection for send/recv with the normal tcp_send()/tcp_recv(). */
+ * backlog connection for send/recv with the normal tcp_srv_send/recv path. */
 void tcp_srv_init(unsigned int port);
 void tcp_srv_reset(void);
 /* Handle one frame.  Returns 1 when it was a TCP frame for us (consumed),
@@ -55,6 +77,11 @@ int  tcp_srv_input(const unsigned char *frame, unsigned int len);
 struct tcp_conn *tcp_srv_accept(void);
 /* Send len bytes, split to the peer's MSS.  0 / -1 / -2 / -3. */
 int  tcp_srv_send(struct tcp_conn *c, const void *data, unsigned int len);
+/* Read the next in-order payload bytes from a connection's receive queue
+ * (used by the server side).  Returns >0 = byte count, 0 = peer closed (EOF),
+ * -1 = deadline passed, -2 = reset. */
+int  tcp_recv(struct tcp_conn *c, unsigned char *out, unsigned int maxlen,
+	      unsigned int deadline_ms);
 /* FIN once, then remember the tuple briefly so a late FIN/ACK is absorbed
  * instead of answered with RST. */
 void tcp_srv_close(struct tcp_conn *c);

+ 69 - 26
ucmd_curl.c

@@ -60,22 +60,20 @@ static const struct uhttp_hooks hooks = {
 
 /* ---- headless fetch helper for the browser ---------------------------- */
 
-#define FETCH_REQ   "/share/bfreq"
-#define FETCH_BODY  "/share/bfbody"
-#define FETCH_META  "/share/bfmeta"
-#define FETCH_DONE  "/share/bfdone"
-#define FETCH_POST  "/share/bfpost"
+#define FETCH_PATH_MAX 32
 #define FETCH_POST_MAX 4096u
 #define FETCH_MAX   (2u * 1024u * 1024u)
 
-static unsigned int c_len(const char *s)
-{
-	unsigned int n = 0;
-
-	while (s && s[n])
-		n++;
-	return n;
-}
+/* Each browser worker gets its own set of /share files; slot -1 keeps the
+ * historical single-helper names for the one-shot __fetch mode. */
+struct fetch_paths {
+	char req[FETCH_PATH_MAX];
+	char body[FETCH_PATH_MAX];
+	char meta[FETCH_PATH_MAX];
+	char done[FETCH_PATH_MAX];
+	char post[FETCH_PATH_MAX];
+};
+static struct fetch_paths fp;
 
 static void app_ch(char *d, unsigned int *n, char c, unsigned int cap)
 {
@@ -103,6 +101,31 @@ static void app_dec(char *d, unsigned int *n, unsigned int v, unsigned int cap)
 		app_ch(d, n, tmp[i - 1 - j], cap);
 }
 
+static void fetch_paths_init(int slot)
+{
+	unsigned int n = 0;
+
+	app_str(fp.req, &n, "/share/bfreq", sizeof(fp.req));
+	if (slot >= 0)
+		app_dec(fp.req, &n, (unsigned int)slot, sizeof(fp.req));
+	n = 0;
+	app_str(fp.body, &n, "/share/bfbody", sizeof(fp.body));
+	if (slot >= 0)
+		app_dec(fp.body, &n, (unsigned int)slot, sizeof(fp.body));
+	n = 0;
+	app_str(fp.meta, &n, "/share/bfmeta", sizeof(fp.meta));
+	if (slot >= 0)
+		app_dec(fp.meta, &n, (unsigned int)slot, sizeof(fp.meta));
+	n = 0;
+	app_str(fp.done, &n, "/share/bfdone", sizeof(fp.done));
+	if (slot >= 0)
+		app_dec(fp.done, &n, (unsigned int)slot, sizeof(fp.done));
+	n = 0;
+	app_str(fp.post, &n, "/share/bfpost", sizeof(fp.post));
+	if (slot >= 0)
+		app_dec(fp.post, &n, (unsigned int)slot, sizeof(fp.post));
+}
+
 static void fetch_fail(unsigned int status, int err, const char *ctype,
 		       const char *enc, const char *final)
 {
@@ -116,7 +139,7 @@ static void fetch_fail(unsigned int status, int err, const char *ctype,
 	app_str(meta, &n, ctype ? ctype : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_str(meta, &n, final ? final : "", sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
 	app_dec(meta, &n, 0, sizeof(meta)); app_ch(meta, &n, '\n', sizeof(meta));
-	u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
+	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
 static void write_meta(unsigned int seq, int with_seq, struct uhttp *h,
@@ -136,7 +159,7 @@ 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));
-	u_syscall3(SYS_WRITE_FILE, (int)FETCH_META, (int)meta, (int)n);
+	u_syscall3(SYS_WRITE_FILE, (int)fp.meta, (int)meta, (int)n);
 }
 
 /* Fetch one URL through a possibly reused keep-alive connection. */
@@ -190,13 +213,15 @@ static void fetch_once(void)
 {
 	static struct uhttp h;
 	char req[768];
+
+	fetch_paths_init(-1);
 	unsigned char *buf;
 	unsigned int cap = 0;
 	unsigned int body_len = 0, body_trunc = 0;
 	unsigned int i;
 	int rn, err = 0;
 
-	rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
+	rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
 			(int)(sizeof(req) - 1));
 	if (rn <= 0)
 		u_exit(1);
@@ -227,7 +252,7 @@ static void fetch_once(void)
 	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);
 	if (body_len)
-		u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)buf,
+		u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 			   (int)body_len);
 	write_meta(0, 0, &h, err, body_len, body_trunc);
 	uhttp_close(&h);
@@ -239,12 +264,26 @@ static void fetch_once(void)
  * (reusing the same TCP/TLS connection whenever the host is unchanged) and
  * publish body + meta + a small done marker.  Idle instances exit and
  * release the NIC after ten seconds. */
-static void fetch_serve(void)
+static void fetch_serve(const char *arg)
 {
 	static struct uhttp h;
 	char req[768];
 	unsigned char *buf, *post;
 	unsigned int last_seq = 0, idle = 0;
+	int slot = -1;
+
+	while (arg && *arg == ' ')
+		arg++;
+	if (arg && !strncmp(arg, "__serve", 7))
+		arg += 7;
+	while (arg && *arg == ' ')
+		arg++;
+	if (arg && *arg >= '0' && *arg <= '9') {
+		slot = 0;
+		while (*arg >= '0' && *arg <= '9')
+			slot = slot * 10 + (*arg++ - '0');
+	}
+	fetch_paths_init(slot);
 
 	buf = (unsigned char *)malloc(FETCH_MAX);
 	post = (unsigned char *)malloc(FETCH_POST_MAX);
@@ -264,7 +303,7 @@ static void fetch_serve(void)
 		char *url;
 		int rn, err = 0;
 
-		rn = u_syscall3(SYS_READ_FILE, (int)FETCH_REQ, (int)req,
+		rn = u_syscall3(SYS_READ_FILE, (int)fp.req, (int)req,
 				(int)(sizeof(req) - 1));
 		if (rn <= 0) {
 			url = 0;
@@ -314,7 +353,7 @@ static void fetch_serve(void)
 
 			if (post_len > FETCH_POST_MAX)
 				post_len = FETCH_POST_MAX;
-			pn = u_syscall3(SYS_READ_FILE, (int)FETCH_POST,
+			pn = u_syscall3(SYS_READ_FILE, (int)fp.post,
 					(int)post, (int)FETCH_POST_MAX);
 			if (pn < 0)
 				pn = 0;
@@ -327,11 +366,11 @@ static void fetch_serve(void)
 		helper_fetch(&h, url, cap, is_post ? (char *)post : 0, post_len,
 			     buf, &body_len, &body_trunc, &err);
 		if (body_len)
-			u_syscall3(SYS_WRITE_FILE, (int)FETCH_BODY, (int)buf,
+			u_syscall3(SYS_WRITE_FILE, (int)fp.body, (int)buf,
 				   (int)body_len);
 		write_meta(seq, 1, &h, err, body_len, body_trunc);
 		app_dec(done, &dn, seq, sizeof(done));
-		u_syscall3(SYS_WRITE_FILE, (int)FETCH_DONE, (int)done, (int)dn);
+		u_syscall3(SYS_WRITE_FILE, (int)fp.done, (int)done, (int)dn);
 
 		if (err || body_trunc || h.peer_close || is_post) {
 			h.keepalive = 0;
@@ -359,11 +398,15 @@ void _start(const char *arg)
 	unsigned int total = 0;
 	int rv;
 
-	if (arg && c_len(arg) == 7 && arg[0] == '_' && arg[1] == '_') {
-		if (arg[2] == 'f')
+	if (arg && arg[0] == '_' && arg[1] == '_') {
+		if (!strncmp(arg, "__fetch", 7)) {
 			fetch_once();
-		else if (arg[2] == 's')
-			fetch_serve();
+			u_exit(0);
+		}
+		if (!strncmp(arg, "__serve", 7)) {
+			fetch_serve(arg);
+			u_exit(0);
+		}
 	}
 	if (!arg || !arg[0]) {
 		u_puts("curl: usage curl <http|https://host[:port]/[path]>\n");

+ 3 - 8
uhttp.c

@@ -405,6 +405,8 @@ static int http_connect(struct uhttp *h)
 		return UHTTP_ERR_TIMEOUT;
 	if (rv == -2)
 		return UHTTP_ERR_REFUSED;
+	if (rv == -4)
+		return UHTTP_ERR_BUSY;
 	if (rv != 0)
 		return UHTTP_ERR_CONNECT;
 	h->connected = 1;
@@ -692,16 +694,13 @@ int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
 		reuse = 0;
 		if (h->connected)
 			transport_close(h);
-		if (!h->owns_nic) {
+		{
 			struct sys_netinfo ni;
 
 			if (u_syscall1(SYS_NET_INFO, (int)&ni) != 1)
 				return UHTTP_ERR_NIC;
 			if (u_syscall0(SYS_NET_CONFIGURED) != 1)
 				return UHTTP_ERR_IP;
-			if (u_syscall0(SYS_NET_ACQUIRE) != 0)
-				return UHTTP_ERR_BUSY;
-			h->owns_nic = 1;
 		}
 	}
 
@@ -886,10 +885,6 @@ void uhttp_close(struct uhttp *h)
 	if (h->keepalive && h->connected && !h->peer_close && !h->error)
 		return;
 	transport_close(h);
-	if (h->owns_nic) {
-		u_syscall0(SYS_NET_RELEASE);
-		h->owns_nic = 0;
-	}
 }
 
 const char *uhttp_error_string(int err)

+ 0 - 1
uhttp.h

@@ -86,7 +86,6 @@ struct uhttp {
 	int eof;
 	int error;                      /* UHTTP_ERR_* once a read failed */
 	int connected;
-	int owns_nic;
 	int keepalive;                  /* reuse the TCP/TLS connection */
 	int peer_close;                 /* response asked to close it */