net: a network *class*, so the stack stops knowing what an e1000 is

The TCP/IP stack reached straight into the first driver anyone wrote.
net.c called e1000_present(), the lwIP glue was named netif_e1000.c and
called e1000_transmit(), and include/defs.h declared the driver's
internals to the whole kernel. The second NIC driver would therefore
have arrived with a near-identical copy of the glue file and a patch to
the protocol stack, which is the arrangement include/device.h was
written to prevent -- the e1000 even said so, with `.ops = 0, // no
class vtable yet; there is no net class`.

There is now. A driver publishes a struct netdev_ops -- transmit,
receive, mac, link_up -- and is finished: it never mentions lwIP, and
nothing above it learns its name. The display class was already this
shape; this is the same shape for networking, and the two now read the
same way in kernel/dev/device.c.

What that made possible immediately:

  - One netif per bound device instead of one per driver. lwIP's glue is
    netif_eth.c, it takes a struct device, and it holds up to NETIF_MAX
    interfaces. net.c attaches every network device it finds and starts
    DHCP on each, because which interface the network answers on is not
    ours to decide.
  - /proc/netif lists all of them, one block of fields each, with the
    interface name and the driver that claimed it.
  - The ARP self-test moved from the e1000 to the class. Nothing in it
    was chip-specific, and it is the first thing anyone will want to run
    against the next driver. Still `echo n > /proc/sysrq`.
  - Private headers live with their driver: e1000.h was in include/,
    where exactly one file could use it. The driver build now adds the
    driver's own directory to the include path.

The e1000 also drives more than one card. Its probe() refused the second
outright, because there was a single static instance -- so a machine
with two of them drove one and silently ignored the other. Instances are
an array now, and the instance is hung on dev->priv by a new optional
`attached` hook that the bus calls once the device exists. probe() runs
before device_add(), so there was previously nowhere to put it; that is
the whole reason the hook exists rather than a line inside probe.

QEMU gets an explicit NIC on every target (QEMU_NET). The default device
happens to be an e1000 on this machine type today, which is exactly the
sort of thing that stays true until a QEMU release changes it and the
network stops being exercised without anyone noticing.

Verified:
  - bootcheck -Net passes, 48 [ok], no failures -- including the moved
    self-test and two new checks that /proc/netif names both the
    interface and the driver behind it, which together say the stack
    went through the class rather than through a driver it was compiled
    to know.
  - Two NICs attached by hand (-device e1000 twice, second with its own
    MAC): eth0 and eth1 both attach, each reports its own MAC in
    /proc/netif, and each gets its own DHCP lease. Before this commit
    the same machine produced one interface.
  - make schfs-check still passes in full.

One trap worth recording: moving a header broke the build for anyone
with stale .d files -- make stopped with "No rule to make target
'include/e1000.h'". CFLAGS gains -MP, which emits a phony target per
header so a moved or deleted one can never wedge the build again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-08-15 16:42:34 +02:00
commit 20cc673a41
16 changed files with 729 additions and 367 deletions

View file

@ -15,6 +15,7 @@
#include "param.h"
#include "device.h"
#include "display.h"
#include "netdev.h"
#include "theme.h"
#include "text.h"
@ -306,3 +307,161 @@ text_selftest(void)
display_present(0, 0, m.width, m.height);
cprintf("text: drew a specimen, \"%s\" measures %d px\n", msg, w);
}
// ---------------------------------------------------------------------------
// The network class. See include/netdev.h.
//
// Plural where the display class is singular: a machine with two NICs is
// ordinary, and "the network" is not a thing a stack can ask for the way a
// compositor asks for "the display". So this walks, and the caller decides
// what to do with each interface it finds.
struct device *
netdev_at(int i)
{
int seen = 0;
for (int k = 0; k < device_count(); k++) {
struct device *d = device_at(k);
if (!d || d->class != DEVCLASS_NET || !d->driver || !d->ops)
continue;
if (seen++ == i)
return d;
}
return 0;
}
int
netdev_count(void)
{
int n = 0;
while (netdev_at(n))
n++;
return n;
}
int
netdev_transmit(struct device *d, const void *frame, int len)
{
if (!d || !d->ops || !((struct netdev_ops *)d->ops)->transmit)
return -1;
return ((struct netdev_ops *)d->ops)->transmit(d, frame, len);
}
int
netdev_receive(struct device *d, void *out, int cap)
{
if (!d || !d->ops || !((struct netdev_ops *)d->ops)->receive)
return 0;
return ((struct netdev_ops *)d->ops)->receive(d, out, cap);
}
const unsigned char *
netdev_mac(struct device *d)
{
if (!d || !d->ops || !((struct netdev_ops *)d->ops)->mac)
return 0;
return ((struct netdev_ops *)d->ops)->mac(d);
}
int
netdev_link_up(struct device *d)
{
// A driver that cannot tell is treated as up. Reporting "down" for a link
// whose state is simply unknown would stop the stack on hardware that works.
if (!d || !d->ops || !((struct netdev_ops *)d->ops)->link_up)
return 1;
return ((struct netdev_ops *)d->ops)->link_up(d);
}
static void
put_be16(uint8 *p, uint16 v)
{
p[0] = (uint8)(v >> 8);
p[1] = (uint8)v;
}
// The ARP probe. Written for the e1000 when it was the only driver and there
// was no stack to test it with; it belongs here because it is the first thing
// anyone will want to run against the *next* driver, and none of it is
// specific to a chip.
//
// Under QEMU's user-mode networking the guest is 10.0.2.15 and the gateway is
// 10.0.2.2. SLIRP is silent until spoken to, which is why this has to transmit
// before it can expect to receive anything.
void
netdev_selftest(void)
{
struct device *d = netdev_at(0);
uint8 frame[42];
uint8 reply[2048];
if (!d) {
cprintf("netdev: no interface\n");
return;
}
const unsigned char *mac = netdev_mac(d);
if (!mac) {
cprintf("netdev: %s has no station address\n", d->name);
return;
}
static const uint8 me[4] = { 10, 0, 2, 15 }; // QEMU SLIRP guest address
static const uint8 them[4] = { 10, 0, 2, 2 }; // and its gateway
memset(frame, 0, sizeof(frame));
for (int i = 0; i < 6; i++)
frame[i] = 0xFF; // broadcast
for (int i = 0; i < 6; i++)
frame[6 + i] = mac[i];
put_be16(&frame[12], 0x0806); // ARP
put_be16(&frame[14], 1); // Ethernet
put_be16(&frame[16], 0x0800); // IPv4
frame[18] = 6; // hardware address length
frame[19] = 4; // protocol address length
put_be16(&frame[20], 1); // request
for (int i = 0; i < 6; i++)
frame[22 + i] = mac[i]; // sender hardware address
for (int i = 0; i < 4; i++)
frame[28 + i] = me[i]; // sender protocol address
// target hardware address stays zero -- that is what is being asked
for (int i = 0; i < 4; i++)
frame[38 + i] = them[i];
cprintf("netdev: %s asking who has %d.%d.%d.%d\n",
d->name, them[0], them[1], them[2], them[3]);
if (netdev_transmit(d, frame, sizeof(frame)) != 0) {
cprintf("netdev: %s transmit ring full\n", d->name);
return;
}
// Bounded poll. This runs from a sysrq write and there is nothing to sleep
// on that would be woken by a raw frame, so spinning briefly is honest.
//
// It will also swallow whatever else is on the wire, including the stack's
// own traffic if it is running -- which is why a non-ARP frame is reported
// rather than treated as a failure.
for (int spin = 0; spin < 20000000; spin++) {
int n = netdev_receive(d, reply, sizeof(reply));
if (n <= 0)
continue;
cprintf("netdev: %d bytes from %x:%x:%x:%x:%x:%x type %x\n",
n, reply[6], reply[7], reply[8], reply[9], reply[10], reply[11],
((uint32)reply[12] << 8) | reply[13]);
if (n >= 42 && reply[12] == 0x08 && reply[13] == 0x06 &&
reply[20] == 0 && reply[21] == 2) {
cprintf("netdev: ARP reply -- %d.%d.%d.%d is at %x:%x:%x:%x:%x:%x\n",
reply[28], reply[29], reply[30], reply[31],
reply[22], reply[23], reply[24], reply[25],
reply[26], reply[27]);
cprintf("netdev: %s self-test passed\n", d->name);
return;
}
}
cprintf("netdev: %s no reply\n", d->name);
}

View file

@ -386,6 +386,11 @@ pci_bind(const struct pci_driver *drv)
dev->product = d->device;
dev->b = d->bus; dev->s = d->slot; dev->f = d->func;
pci_describe(d, dev->desc, sizeof(dev->desc));
// Now, and only now, can a multi-instance driver say which of its
// own this device is: the object it would hang that on did not
// exist until the line above.
if (drv->attached)
drv->attached(d, dev);
}
}
break;

View file

@ -21,6 +21,7 @@
#include "x86.h"
#include "device.h"
#include "display.h"
#include "netdev.h"
#include "param.h"
#include "memlayout.h"
#include "mmu.h"
@ -444,7 +445,7 @@ proc_sysrq_cmd(char cmd)
display_selftest();
break;
case 'n':
e1000_selftest();
netdev_selftest();
break;
case 't':
text_selftest();