SchOS/kernel/net/lwip/lwipopts.h
Nexoniarz c0806d1b64 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>
2026-08-15 16:02:48 +02:00

94 lines
4.1 KiB
C

// SPDX-License-Identifier: Apache-2.0
//
// lwIP configuration for SchOS.
//
// The shape of this file is one decision: NO_SYS=1.
//
// lwIP can run either as a set of threads of its own (NO_SYS=0), which needs a
// port layer of semaphores, mailboxes and thread creation and then hands you a
// blocking sockets API for free; or bare, with no threading assumptions
// (NO_SYS=1), where every call must come from one context and the blocking is
// yours to write. This kernel takes the second, for two reasons: the sys_arch
// a threaded lwIP wants is more code than the socket layer it would save, and
// the kernel already has a socket layer with its own shape (kernel/net/unix.c)
// that AF_INET has to fit into rather than replace.
//
// "One context" is enforced by a single lock rather than by literally one
// thread: kernel/net/net.c takes it around the network thread's work, and the
// socket syscalls take it around theirs. That is the entire concurrency model.
#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H
#define NO_SYS 1
#define SYS_LIGHTWEIGHT_PROT 1
#define LWIP_NETCONN 0 // needs NO_SYS=0
#define LWIP_SOCKET 0 // ours lives in kernel/net/inet.c
// Memory. All of it is static: the kernel's allocator hands out whole pages
// and nothing else, so lwIP's own pools are a better fit than wrapping kalloc.
// These numbers are sized for a handful of connections, not for a server.
#define MEM_LIBC_MALLOC 0
#define MEMP_MEM_MALLOC 0
#define MEM_ALIGNMENT 4
#define MEM_SIZE (192 * 1024)
#define MEMP_NUM_PBUF 32
#define MEMP_NUM_UDP_PCB 8
#define MEMP_NUM_TCP_PCB 16
#define MEMP_NUM_TCP_PCB_LISTEN 8
#define MEMP_NUM_TCP_SEG 64
#define MEMP_NUM_ARP_QUEUE 16
#define MEMP_NUM_SYS_TIMEOUT 16
#define PBUF_POOL_SIZE 48
#define PBUF_POOL_BUFSIZE 1536 // one Ethernet frame plus room
// Protocols. IPv4 only: the driver is proven for it, the QEMU test network is
// v4, and every line of v6 here would be untested.
#define LWIP_IPV4 1
#define LWIP_IPV6 0
#define LWIP_ARP 1
#define LWIP_ETHERNET 1
#define LWIP_ICMP 1
#define LWIP_RAW 1
#define LWIP_UDP 1
#define LWIP_TCP 1
#define LWIP_DHCP 1
#define LWIP_DNS 1
#define LWIP_AUTOIP 0
#define LWIP_IGMP 0
#define LWIP_NETIF_HOSTNAME 1
// DHCP needs UDP checksums and a broadcast path; both are on. Requesting an
// address rather than hard-coding one matters because QEMU's user-mode network
// hands out 10.0.2.15 through DHCP and nothing else will route.
#define LWIP_NETIF_STATUS_CALLBACK 1
#define LWIP_NETIF_LINK_CALLBACK 1
#define TCP_MSS 1460
#define TCP_SND_BUF (8 * TCP_MSS)
#define TCP_WND (8 * TCP_MSS)
#define TCP_SND_QUEUELEN ((4 * TCP_SND_BUF) / TCP_MSS)
#define TCP_LISTEN_BACKLOG 1
// Checksums in software. The e1000 can offload them, but the driver does not
// ask it to, and a checksum computed by a device the stack has not been told
// about is the sort of mismatch that shows up only against a real peer.
#define CHECKSUM_GEN_IP 1
#define CHECKSUM_GEN_UDP 1
#define CHECKSUM_GEN_TCP 1
#define CHECKSUM_CHECK_IP 1
#define CHECKSUM_CHECK_UDP 1
#define CHECKSUM_CHECK_TCP 1
// Statistics are what /proc/net reports; they cost a few counters.
#define LWIP_STATS 1
#define LWIP_STATS_DISPLAY 0
// Debug output off by default -- turning LWIP_DEBUG on here makes the stack
// extremely chatty on the console, which is occasionally exactly what you want.
#define LWIP_DEBUG 0
// lwIP would otherwise provide its own errno, which would then disagree with
// the kernel's for every value a syscall returns.
#define LWIP_PROVIDE_ERRNO 1
#endif