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>
63 lines
3 KiB
C
63 lines
3 KiB
C
#pragma once
|
|
|
|
// The network class: what the TCP/IP stack talks to instead of a driver.
|
|
//
|
|
// The stack should ask for "the network interfaces" and get whatever is bound
|
|
// -- today an e1000, tomorrow a virtio-net or an rtl8139 -- exactly as a
|
|
// compositor asks for "the display" and does not care which driver answers.
|
|
// Without this the first driver's name spreads: kernel/net/net.c called
|
|
// e1000_present(), the lwIP glue was named netif_e1000.c and called
|
|
// e1000_transmit(), and adding a second NIC would have meant editing the
|
|
// protocol stack to add it. That is the mistake include/device.h was written
|
|
// to prevent, made once anyway because the net class did not exist yet --
|
|
// kernel/drivers/net/e1000/e1000.c even said so, with `.ops = 0, // no class
|
|
// vtable yet`.
|
|
//
|
|
// A driver publishes one of these and is then finished with the network stack
|
|
// forever: it never mentions lwIP, and the stack never mentions it.
|
|
//
|
|
// Found with device_find_class(DEVCLASS_NET) or, when there may be more than
|
|
// one, netdev_at(); see include/device.h.
|
|
|
|
#include "device.h"
|
|
|
|
struct netdev_ops {
|
|
// Queue one frame for transmission. Returns 0, or -1 if the ring is full --
|
|
// which is congestion, not failure, and the caller is expected to retry.
|
|
int (*transmit)(struct device *, const void *frame, int len);
|
|
|
|
// Take one received frame if there is one. Returns its length, or 0 when
|
|
// nothing is waiting. Never blocks: the caller decides how to wait.
|
|
int (*receive)(struct device *, void *out, int cap);
|
|
|
|
// The station address, six bytes. Every Ethernet interface has one and the
|
|
// stack needs it before it can send anything, so this is not optional.
|
|
const unsigned char *(*mac)(struct device *);
|
|
|
|
// Whether the link is up. Optional -- a driver that cannot tell leaves this
|
|
// null and is treated as connected, which is the assumption that was made
|
|
// before any driver could report it.
|
|
int (*link_up)(struct device *);
|
|
};
|
|
|
|
// The i-th bound network device, or 0 past the end. Dense over interfaces
|
|
// that have both a driver and a vtable, so the stack can simply count up.
|
|
struct device *netdev_at(int i);
|
|
int netdev_count(void);
|
|
|
|
// Wrappers with the null checks in one place, so no caller is written around
|
|
// a vtable that may be absent or partly filled in.
|
|
int netdev_transmit(struct device *, const void *frame, int len);
|
|
int netdev_receive(struct device *, void *out, int cap);
|
|
const unsigned char *netdev_mac(struct device *);
|
|
int netdev_link_up(struct device *);
|
|
|
|
// Send an ARP request by hand and wait for the reply. `echo n > /proc/sysrq`.
|
|
//
|
|
// It proves the whole driver path at once -- descriptor formats, the DD
|
|
// handshake, tail bookkeeping, DMA addresses, the receive ring hand-back and
|
|
// the MAC filter -- because an ARP reply is addressed to this station and does
|
|
// not arrive if the filter is wrong. Written for the e1000 before there was a
|
|
// stack to test it with; it belongs to the class rather than to that driver,
|
|
// since it is the check every new NIC driver will want first.
|
|
void netdev_selftest(void);
|