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:
parent
03cc316001
commit
c0806d1b64
30 changed files with 1922 additions and 19 deletions
11
kernel/net/lwip/shim/ctype.h
Normal file
11
kernel/net/lwip/shim/ctype.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <ctype.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_ctype_H
|
||||
#define LWIP_SHIM_ctype_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/errno.h
Normal file
11
kernel/net/lwip/shim/errno.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <errno.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_errno_H
|
||||
#define LWIP_SHIM_errno_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/inttypes.h
Normal file
11
kernel/net/lwip/shim/inttypes.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <inttypes.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_inttypes_H
|
||||
#define LWIP_SHIM_inttypes_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/limits.h
Normal file
11
kernel/net/lwip/shim/limits.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <limits.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_limits_H
|
||||
#define LWIP_SHIM_limits_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/stddef.h
Normal file
11
kernel/net/lwip/shim/stddef.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <stddef.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_stddef_H
|
||||
#define LWIP_SHIM_stddef_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/stdint.h
Normal file
11
kernel/net/lwip/shim/stdint.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <stdint.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_stdint_H
|
||||
#define LWIP_SHIM_stdint_H
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/stdio.h
Normal file
11
kernel/net/lwip/shim/stdio.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <stdio.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_stdio_H
|
||||
#define LWIP_SHIM_stdio_H
|
||||
#endif
|
||||
28
kernel/net/lwip/shim/stdlib.h
Normal file
28
kernel/net/lwip/shim/stdlib.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// <stdlib.h> for lwIP, shadowing the host's. See shim/string.h for why these
|
||||
// files exist at all.
|
||||
//
|
||||
// lwIP does not need an allocator from here: MEM_LIBC_MALLOC is off, so it uses
|
||||
// its own pools out of a static heap. That is not a workaround -- the kernel's
|
||||
// allocator hands out whole pages and lwIP wants byte granularity, so its pools
|
||||
// are the better fit either way.
|
||||
//
|
||||
// atoi is the one thing it genuinely calls, in netif_name_to_index, to turn the
|
||||
// digits at the end of an interface name into a number.
|
||||
#ifndef LWIP_SHIM_STDLIB_H
|
||||
#define LWIP_SHIM_STDLIB_H
|
||||
|
||||
// Deliberately not a general atoi: no sign, no whitespace, no overflow
|
||||
// handling. It is fed the tail of an interface name ("en0" -> "0"), and
|
||||
// anything cleverer would be untested code answering a question nobody asks.
|
||||
static inline int
|
||||
atoi(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (*s >= '0' && *s <= '9')
|
||||
n = n * 10 + (*s++ - '0');
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif
|
||||
37
kernel/net/lwip/shim/string.h
Normal file
37
kernel/net/lwip/shim/string.h
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// <string.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// lwIP includes <string.h> in thirty-odd files. The kernel is built
|
||||
// -ffreestanding and has its own string functions, but the compiler's default
|
||||
// include path still finds /usr/include/string.h -- which declares strlen
|
||||
// returning size_t against a kernel that returns int, and drags sigset_t and
|
||||
// __float128 in behind it. So the lwIP include path puts this directory first.
|
||||
//
|
||||
// The prototypes are repeated here rather than taken from defs.h on purpose.
|
||||
// lwIP includes <string.h> at the very top of a translation unit, before any
|
||||
// kernel header has been seen, so pulling defs.h in from here would drop the
|
||||
// whole kernel's declarations into a context where its own types are not
|
||||
// defined yet -- which shows up as errors in unrelated headers, like file.h
|
||||
// not knowing what NDIRECT is.
|
||||
//
|
||||
// They must match kernel/core/string.c exactly. Only what lwIP calls is here;
|
||||
// anything else should be added deliberately rather than by growing this into
|
||||
// a libc.
|
||||
#ifndef LWIP_SHIM_STRING_H
|
||||
#define LWIP_SHIM_STRING_H
|
||||
|
||||
int memcmp(const void *, const void *, unsigned long);
|
||||
void *memmove(void *, const void *, unsigned long);
|
||||
void *memset(void *, int, unsigned long);
|
||||
int strlen(const char *);
|
||||
int strncmp(const char *, const char *, unsigned long);
|
||||
char *strncpy(char *, const char *, int);
|
||||
|
||||
// The kernel deliberately provides only memmove, on the grounds that a copy
|
||||
// which happens to overlap should not be undefined behaviour. lwIP calls
|
||||
// memcpy in a handful of places, always on buffers it has just allocated, so
|
||||
// mapping it onto memmove is correct and costs nothing.
|
||||
#define memcpy(d, s, n) memmove((d), (s), (n))
|
||||
|
||||
#endif
|
||||
11
kernel/net/lwip/shim/unistd.h
Normal file
11
kernel/net/lwip/shim/unistd.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
// SPDX-License-Identifier: Apache-2.0
|
||||
// <unistd.h> for lwIP, shadowing the host's.
|
||||
//
|
||||
// Empty on purpose. Everything lwIP would take from this header is supplied by
|
||||
// arch/cc.h instead -- see the LWIP_NO_*_H switches there -- and the file exists
|
||||
// only so the #include resolves to something harmless rather than to the host's
|
||||
// copy, which declares types the kernel already has and pulls half of glibc in
|
||||
// behind them. See shim/string.h for the full reasoning.
|
||||
#ifndef LWIP_SHIM_unistd_H
|
||||
#define LWIP_SHIM_unistd_H
|
||||
#endif
|
||||
Loading…
Reference in a new issue