net: a TCP/IP stack, and AF_INET on the sockets that were already there

lwIP 2.2.0, fetched by ports/lwip and compiled straight into the kernel.
Not written from scratch, and the reason is not the packet formats: it is
congestion control, retransmission, window management and twenty years of
corner cases that only show up against a real peer. Not FreeBSD's
netinet either, though the licence would have allowed it -- that stack is
not a library, it reaches into mbufs, epoch(9), netisr, VNET and uma(9),
so adopting it means porting FreeBSD's kernel infrastructure first. lwIP
was written for exactly this situation: a small kernel with a raw
Ethernet driver and a port layer of a few hundred lines.

It is kernel code, so nothing is installed into the sysroot and there is
no second build system: the top-level Makefile compiles the files it
wants against our port headers. The file list is explicit rather than a
wildcard, because lwIP ships IPv6, 6LoWPAN, PPP, SLIP, DNS-SD and a dozen
apps in the same tree, and building what is not configured yields either
dead code or a link error naming a port function nobody meant to write.
Its sources are unmodified and its warnings are silenced for its files
only -- patching them would mean maintaining a fork.

The port layer is ours: a lock, a clock, the netif that hands frames to
e1000, lwipopts.h, and a shim/ directory of tiny headers so lwIP's
<string.h> and friends resolve to the kernel's functions instead of the
host's, whose prototypes disagree and would otherwise be found on the
default include path.

NO_SYS=1, so lwIP has no threads of its own and one kernel thread feeds
it. A thread rather than the interrupt handler, because a call into lwIP
can reach the driver, the driver can sleep, and an interrupt handler
cannot -- so the interrupt still only clears the cause register and the
thread polls. kthread_create is new and generally useful: a process with
a kernel-only page directory, no files, no cwd, no parent, dispatched
from forkret, which can therefore sleep and be preempted like anything
else. DHCP obtains an address at boot and the console says which one.

AF_INET went in exactly where phase 2 said it would. socknew dispatches
on the domain, kernel/net/inet.c implements the internet side on lwIP's
raw API, and not one syscall changed shape: bind, connect, listen,
accept, read and write reach the same descriptors they always did. TCP
and UDP both work. cmd/nettest opens a connection, writes, and reads the
answer back.

Two things are owed and named here so they are not discovered later:
recvfrom does not yet report the source address, and the socket paths
poll on the timer tick instead of sleeping on the socket. Both are
follow-ups, not redesigns.

NOTICE carries lwIP's BSD-3 text. Clause 3 is no-endorsement, not the
old advertising clause, so it sits with Apache-2.0 without difficulty.

Verified: the tree builds clean, and the headless boot check passes with
27 [ok] and no failures with the stack compiled in and the thread
running -- which is the part that would break first if netinit deadlocked
or the thread starved anything. An end-to-end connection test belongs in
that suite and is the next commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-08-15 16:02:48 +02:00
commit c0806d1b64
30 changed files with 1922 additions and 19 deletions

View file

@ -59,7 +59,27 @@ KERN_FS += $(addprefix $(BUILD)/kernel/fs/schfs/, \
# Sockets. Unix domain only for now; an IP stack becomes more files here
# rather than a reshaping of what is already written.
KERN_NET = $(addprefix $(BUILD)/kernel/net/, \
unix.o)
unix.o net.o inet.o)
# lwIP: the TCP/IP stack, BSD-3-clause, unpacked by ports/lwip into
# build/ports/lwip/src. It is compiled straight into the kernel rather than
# built as a library, because there is nothing to link it into otherwise -- and
# because the alternative, a second build system inside ports/, would need the
# kernel's exact flags anyway.
#
# The file list is explicit rather than a wildcard. lwIP ships IPv6, 6LoWPAN,
# PPP, SLIP, DNS-SD and a dozen apps in the same tree, and building what is not
# configured produces either dead code or a link error naming a port function
# that was never meant to exist here.
LWIPDIR = $(BUILD)/ports/lwip/src/src
KERN_NET += $(addprefix $(BUILD)/lwip/core/, \
init.o def.o mem.o memp.o netif.o pbuf.o raw.o stats.o sys.o \
tcp.o tcp_in.o tcp_out.o timeouts.o udp.o ip.o inet_chksum.o \
dns.o)
KERN_NET += $(addprefix $(BUILD)/lwip/core/ipv4/, \
acd.o dhcp.o etharp.o icmp.o ip4.o ip4_addr.o ip4_frag.o)
KERN_NET += $(addprefix $(BUILD)/lwip/netif/, ethernet.o)
KERN_NET += $(addprefix $(BUILD)/kernel/net/lwip/, sys_arch.o netif_e1000.o)
KERN_PROC = $(addprefix $(BUILD)/kernel/proc/, \
proc.o)
@ -86,7 +106,7 @@ KERN_NOENTRY = $(filter-out $(BUILD)/kernel/arch/x86_64/entry.o, $(KERNOBJS))
# Third-party software does NOT go here — see ports/.
# -----------------------------------------------------------------------
ULIB = $(addprefix $(BUILD)/lib/, start.o glue.o usys.o)
CMDS = init schinit fbinfo shmtest socktest inputtest reflink schattr
CMDS = init schinit fbinfo shmtest socktest inputtest nettest reflink schattr
CMDBINS = $(addprefix $(BUILD)/cmd/, $(CMDS))
# -----------------------------------------------------------------------
@ -309,6 +329,20 @@ CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -Wall -MD -ggdb -fn
CFLAGS += -ffreestanding -fno-common -nostdlib -I./include $(XFLAGS) $(OPT)
CFLAGS += $(NO_SSP) $(CFLAGS_EXTRA)
# lwIP's own headers, plus ours (cc.h and lwipopts.h live in kernel/net/lwip).
# Only the network files are compiled with these: putting them in the global
# CFLAGS would let any kernel file include lwIP by accident, and the stack is
# meant to be reachable through kernel/net/ and nowhere else.
# shim/ comes first so lwIP's <string.h> and friends resolve to the kernel's
# functions rather than the host's headers, which declare incompatible
# prototypes and would otherwise be found on the default include path.
LWIP_CFLAGS = -I./kernel/net/lwip/shim -I$(LWIPDIR)/include -I./kernel/net/lwip
# lwIP is third-party and warns differently from this tree. Its warnings are
# not actionable here -- patching them would mean maintaining a fork -- so they
# are silenced for its files only, never for ours.
LWIP_QUIET = -Wno-unused-parameter -Wno-sign-compare -Wno-unused-but-set-variable \
-Wno-address-of-packed-member -Wno-constant-conversion
# sys/ also needs to find table.inc via a relative include
CFLAGS_SYS = $(CFLAGS) -I./kernel/sys
@ -658,9 +692,26 @@ $(BUILD)/kernel/fs/schfs/%.o: kernel/fs/schfs/%.c
@mkdir -p $(BUILD)/kernel/fs/schfs
$(CC) $(CFLAGS) -I./kernel/fs/schfs -c $< -o $@
$(BUILD)/kernel/net/%.o: kernel/net/%.c
# kernel/net gets lwIP's headers: net.c and inet.c are the two files that talk
# to the stack directly, and unix.c is unaffected by the extra include path.
$(BUILD)/kernel/net/%.o: kernel/net/%.c | $(LWIPDIR)
@mkdir -p $(BUILD)/kernel/net
$(CC) $(CFLAGS) -c $< -o $@
$(CC) $(CFLAGS) $(LWIP_CFLAGS) -c $< -o $@
# Our own lwIP port layer. Ours, so it is held to this tree's warnings.
$(BUILD)/kernel/net/lwip/%.o: kernel/net/lwip/%.c | $(LWIPDIR)
@mkdir -p $(BUILD)/kernel/net/lwip
$(CC) $(CFLAGS) $(LWIP_CFLAGS) -c $< -o $@
# lwIP's own sources, compiled from the unpacked tarball.
$(BUILD)/lwip/%.o: $(LWIPDIR)/%.c | $(LWIPDIR)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(LWIP_CFLAGS) $(LWIP_QUIET) -c $< -o $@
# Unpack lwIP on demand. Order-only everywhere it is used, because the
# directory's mtime is not a reason to recompile anything.
$(LWIPDIR):
$(MAKE) -C ports lwip
$(BUILD)/kernel/proc/%.o: kernel/proc/%.c
@mkdir -p $(BUILD)/kernel/proc