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
159
cmd/nettest/main.c
Normal file
159
cmd/nettest/main.c
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
// nettest -- prove the TCP/IP stack carries real traffic.
|
||||
//
|
||||
// Not a ping and not a status readout: both of those can pass on a stack that
|
||||
// cannot actually move bytes. This opens a TCP connection to a host outside the
|
||||
// guest, sends a request, reads the reply and reports what came back -- which
|
||||
// exercises ARP, IP, the TCP handshake, the send and receive paths, the window
|
||||
// and the close, in one exchange.
|
||||
//
|
||||
// nettest <a.b.c.d> <port>
|
||||
//
|
||||
// Under QEMU's user-mode networking the host is 10.0.2.2, so pointing this at a
|
||||
// listener there is the intended use:
|
||||
//
|
||||
// host$ printf 'HELLO-FROM-HOST\n' | nc -l -p 9000
|
||||
// guest# nettest 10.0.2.2 9000
|
||||
|
||||
#include "types.h"
|
||||
#include "user.h"
|
||||
#include "socket.h"
|
||||
|
||||
static void
|
||||
puts_(const char *s)
|
||||
{
|
||||
int n = 0;
|
||||
while (s[n]) n++;
|
||||
write(1, (void *)s, n);
|
||||
}
|
||||
|
||||
static void
|
||||
putn_(int v)
|
||||
{
|
||||
char buf[12];
|
||||
int i = 0;
|
||||
if (v == 0) { write(1, "0", 1); return; }
|
||||
while (v > 0 && i < (int)sizeof(buf)) { buf[i++] = '0' + (v % 10); v /= 10; }
|
||||
while (i--) write(1, &buf[i], 1);
|
||||
}
|
||||
|
||||
// Host byte order to network. Written out rather than taken from a header
|
||||
// because the userland has no <arpa/inet.h> and this is all of what it would
|
||||
// provide here.
|
||||
static ushort
|
||||
hton16(ushort v)
|
||||
{
|
||||
return (ushort)((v << 8) | (v >> 8));
|
||||
}
|
||||
|
||||
// "10.0.2.2" -> the 32-bit address, already in network order. Returns 0 on
|
||||
// anything malformed, which doubles as "0.0.0.0 is not a destination".
|
||||
static uint
|
||||
parse_ip(const char *s)
|
||||
{
|
||||
uint out = 0;
|
||||
for (int part = 0; part < 4; part++) {
|
||||
int v = 0, digits = 0;
|
||||
while (*s >= '0' && *s <= '9') {
|
||||
v = v * 10 + (*s++ - '0');
|
||||
digits++;
|
||||
}
|
||||
if (digits == 0 || v > 255)
|
||||
return 0;
|
||||
// Network order is most-significant byte first, and the first part of
|
||||
// a dotted quad is the most significant.
|
||||
out |= ((uint)v) << (8 * part);
|
||||
if (part < 3) {
|
||||
if (*s != '.')
|
||||
return 0;
|
||||
s++;
|
||||
}
|
||||
}
|
||||
return *s == '\0' ? out : 0;
|
||||
}
|
||||
|
||||
static int
|
||||
parse_port(const char *s)
|
||||
{
|
||||
int v = 0;
|
||||
while (*s >= '0' && *s <= '9')
|
||||
v = v * 10 + (*s++ - '0');
|
||||
return (*s == '\0' && v > 0 && v < 65536) ? v : -1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
if (argc != 3) {
|
||||
puts_("usage: nettest <a.b.c.d> <port>\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
uint ip = parse_ip(argv[1]);
|
||||
int port = parse_port(argv[2]);
|
||||
if (ip == 0 || port < 0) {
|
||||
puts_("nettest: bad address or port\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (fd < 0) {
|
||||
puts_("nettest: socket failed\n");
|
||||
exit();
|
||||
}
|
||||
|
||||
// Zeroed by hand: this userland has no memset, and sin_zero must be clear
|
||||
// because the kernel compares the whole struct's length against sizeof.
|
||||
struct sockaddr_in sa;
|
||||
for (int i = 0; i < (int)sizeof(sa); i++)
|
||||
((char *)&sa)[i] = 0;
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = hton16((ushort)port);
|
||||
sa.sin_addr = ip;
|
||||
|
||||
puts_("nettest: connecting to "); puts_(argv[1]);
|
||||
puts_(":"); putn_(port); puts_("\n");
|
||||
|
||||
if (connect(fd, (const struct sockaddr *)&sa, sizeof(sa)) < 0) {
|
||||
puts_("nettest: connect failed\n");
|
||||
close(fd);
|
||||
exit();
|
||||
}
|
||||
puts_("nettest: connected\n");
|
||||
|
||||
const char *msg = "HELLO-FROM-SCHOS\n";
|
||||
int len = 0;
|
||||
while (msg[len]) len++;
|
||||
if (write(fd, (void *)msg, len) != len) {
|
||||
puts_("nettest: short write\n");
|
||||
close(fd);
|
||||
exit();
|
||||
}
|
||||
puts_("nettest: sent "); putn_(len); puts_(" bytes\n");
|
||||
|
||||
// Read until the peer closes. One read is not enough to claim the receive
|
||||
// path works: TCP is a stream and may well deliver in pieces.
|
||||
char buf[512];
|
||||
int total = 0;
|
||||
for (;;) {
|
||||
int n = read(fd, buf + total, (int)sizeof(buf) - 1 - total);
|
||||
if (n <= 0)
|
||||
break;
|
||||
total += n;
|
||||
if (total >= (int)sizeof(buf) - 1)
|
||||
break;
|
||||
}
|
||||
buf[total] = '\0';
|
||||
close(fd);
|
||||
|
||||
puts_("nettest: received "); putn_(total); puts_(" bytes: ");
|
||||
puts_(buf);
|
||||
if (buf[0] && buf[total ? total - 1 : 0] != '\n')
|
||||
puts_("\n");
|
||||
|
||||
if (total == 0) {
|
||||
puts_("nettest: FAILED, nothing came back\n");
|
||||
exit();
|
||||
}
|
||||
puts_("nettest: OK\n");
|
||||
exit();
|
||||
}
|
||||
Loading…
Reference in a new issue