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>
98 lines
2.7 KiB
C
98 lines
2.7 KiB
C
#pragma once
|
|
|
|
#include "types.h"
|
|
|
|
// Unix domain sockets.
|
|
//
|
|
// These exist for Schcom. A compositor and its clients have to exchange
|
|
// messages, and a pipe cannot do it: a pipe is one-directional, has no
|
|
// notion of a connection, and gives no way for a server to be *found* by a
|
|
// client that starts later. A socket with a name in the filesystem is
|
|
// exactly the missing piece.
|
|
//
|
|
// AF_INET arrived later, and exactly as planned: underneath the same syscalls
|
|
// rather than reshaping them. A socket carries its domain and every entry point
|
|
// dispatches on it -- kernel/net/unix.c for AF_UNIX, kernel/net/inet.c (lwIP)
|
|
// for AF_INET.
|
|
|
|
#define AF_UNIX 1
|
|
#define AF_LOCAL 1
|
|
#define AF_INET 2
|
|
|
|
#define SOCK_STREAM 1
|
|
#define SOCK_DGRAM 2
|
|
|
|
#define UNIX_PATH_MAX 108
|
|
|
|
#define SHUT_RD 0
|
|
#define SHUT_WR 1
|
|
#define SHUT_RDWR 2
|
|
|
|
struct sockaddr_un {
|
|
uint16 sun_family;
|
|
char sun_path[UNIX_PATH_MAX];
|
|
};
|
|
|
|
// The generic form syscalls take. Same shape as Linux's, so a musl-linked
|
|
// program's struct lines up byte for byte.
|
|
struct sockaddr {
|
|
uint16 sa_family;
|
|
char sa_data[UNIX_PATH_MAX];
|
|
};
|
|
|
|
// The AF_INET address. Same layout as Linux's, so a musl-linked program's
|
|
// struct lines up byte for byte. sin_port and sin_addr are network byte order
|
|
// and stay that way: lwIP wants them so, and converting on the way in and out
|
|
// again would be two swaps that cancel.
|
|
struct sockaddr_in {
|
|
uint16 sin_family;
|
|
uint16 sin_port;
|
|
uint32 sin_addr;
|
|
char sin_zero[8];
|
|
};
|
|
|
|
#ifdef _KERNEL_SOCKET_INTERNALS
|
|
|
|
#define NSOCK 32 // sockets in the system
|
|
#define SOCKBUF 4096 // receive buffer per connected socket
|
|
#define NBACKLOG 8
|
|
|
|
enum sockstate {
|
|
SS_FREE = 0,
|
|
SS_UNBOUND,
|
|
SS_BOUND,
|
|
SS_LISTENING,
|
|
SS_CONNECTED,
|
|
};
|
|
|
|
// Every field here is protected by the socket table's single lock in
|
|
// kernel/net/unix.c -- there is deliberately no per-socket lock; see the
|
|
// comment at the top of that file for why.
|
|
struct socket {
|
|
int ref;
|
|
int type;
|
|
int state;
|
|
int domain; // AF_UNIX or AF_INET
|
|
|
|
// For AF_INET: the lwIP-backed socket in kernel/net/inet.c. Opaque here so
|
|
// that socket.h does not have to drag lwIP's headers into every file that
|
|
// wants a struct socket.
|
|
void *inet;
|
|
|
|
char name[UNIX_PATH_MAX]; // the bound path, for a listener
|
|
|
|
// A connected socket's peer. Writing to a socket appends to the *peer's*
|
|
// buffer, which is what makes one buffer per socket enough for a
|
|
// bidirectional connection.
|
|
struct socket *peer;
|
|
int peer_closed;
|
|
|
|
char data[SOCKBUF];
|
|
uint nread, nwrite;
|
|
|
|
// Connections waiting to be accepted, on a listener.
|
|
struct socket *queue[NBACKLOG];
|
|
int nqueue;
|
|
};
|
|
|
|
#endif
|