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

@ -108,6 +108,21 @@ sockclose(struct socket *s)
if(s == 0)
return;
// An AF_INET socket's state lives in inet.c behind the lwIP lock, so its
// teardown happens there. The table entry below is freed either way.
if(s->domain == AF_INET){
acquire(&stab.lock);
int last = (--s->ref <= 0);
if(last)
s->state = SS_FREE;
release(&stab.lock);
if(last && s->inet){
inet_close(s->inet);
s->inet = 0;
}
return;
}
acquire(&stab.lock);
if(--s->ref > 0){
release(&stab.lock);
@ -161,6 +176,8 @@ sockread(struct socket *s, char *addr, int n)
if(n < 0)
return -EINVAL;
if(s->domain == AF_INET)
return inet_read(s->inet, addr, n);
acquire(&stab.lock);
while(s->nread == s->nwrite && !s->peer_closed){
@ -192,6 +209,8 @@ sockwrite(struct socket *s, char *addr, int n)
if(n < 0)
return -EINVAL;
if(s->domain == AF_INET)
return inet_write(s->inet, addr, n);
acquire(&stab.lock);
@ -239,14 +258,30 @@ socknew(int domain, int type)
{
struct socket *s;
if(domain != AF_UNIX)
if(domain != AF_UNIX && domain != AF_INET)
return 0;
if(type != SOCK_STREAM)
// AF_UNIX is stream-only here; AF_INET has datagrams too, and inet_new is
// what decides whether it can serve the type asked for.
if(domain == AF_UNIX && type != SOCK_STREAM)
return 0;
acquire(&stab.lock);
s = alloc_locked(type);
if(s)
s->domain = domain;
release(&stab.lock);
if(s == 0)
return 0;
if(domain == AF_INET){
// Outside the table lock: inet_new takes the lwIP lock, which is a
// sleeplock, and sleeping while holding a spinlock is a panic.
s->inet = inet_new(type);
if(s->inet == 0){
sockclose(s);
return 0;
}
}
return s;
}
@ -305,6 +340,9 @@ sockbind(struct socket *s, const char *path)
int
socklisten(struct socket *s, int backlog)
{
if(s->domain == AF_INET)
return inet_listen(s->inet, backlog);
(void)backlog; // the queue is a fixed NBACKLOG either way
acquire(&stab.lock);
@ -325,6 +363,28 @@ sockaccept(struct socket *s)
struct socket *c;
int i;
if(s->domain == AF_INET){
// The connection lwIP hands back needs a table entry of its own, because
// everything above this layer works in `struct socket` and knows nothing
// about lwIP.
void *h = inet_accept(s->inet);
if(h == 0)
return 0;
acquire(&stab.lock);
c = alloc_locked(s->type);
if(c){
c->domain = AF_INET;
c->state = SS_CONNECTED;
}
release(&stab.lock);
if(c == 0){
inet_close(h);
return 0;
}
c->inet = h;
return c;
}
acquire(&stab.lock);
if(s->state != SS_LISTENING){
release(&stab.lock);
@ -426,6 +486,8 @@ sock_shutdown(struct socket *s, int how)
int
sockreadable(struct socket *s)
{
if(s->domain == AF_INET)
return inet_readable(s->inet);
if(s->state == SS_LISTENING)
return s->nqueue > 0;
return s->nread != s->nwrite || s->peer_closed;
@ -435,7 +497,58 @@ sockreadable(struct socket *s)
int
sock_hup(struct socket *s)
{
if(s->domain == AF_INET)
return inet_hup(s->inet);
if(s->state == SS_LISTENING)
return 0;
return s->peer == 0 && s->nread == s->nwrite;
}
/* ------------------------------------------------------------------ */
/* AF_INET addressing */
/* */
/* Separate entry points rather than an overloaded sockbind, because */
/* the two families do not name the same kind of thing: one takes a */
/* path, the other an address and a port. The syscall layer picks by */
/* the family in the sockaddr the caller passed. */
/* ------------------------------------------------------------------ */
int
sockbind_in(struct socket *s, uint addr, ushort port)
{
if(s->domain != AF_INET)
return -EAFNOSUPPORT;
int r = inet_bind(s->inet, addr, port);
if(r == 0){
acquire(&stab.lock);
s->state = SS_BOUND;
release(&stab.lock);
}
return r;
}
int
sockconnect_in(struct socket *s, uint addr, ushort port)
{
if(s->domain != AF_INET)
return -EAFNOSUPPORT;
int r = inet_connect(s->inet, addr, port);
if(r == 0){
acquire(&stab.lock);
s->state = SS_CONNECTED;
release(&stab.lock);
}
return r;
}
// Which family a socket belongs to. An accessor because kernel/sys/sys_net.c is
// the translation layer and deliberately does not see inside struct socket --
// it works in descriptors and user addresses, not in the socket's guts.
int
sock_domain(struct socket *s)
{
return s ? s->domain : 0;
}