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:
parent
042ca7fb70
commit
20cc673a41
16 changed files with 729 additions and 367 deletions
|
|
@ -13,6 +13,7 @@ struct stat;
|
|||
struct superblock;
|
||||
struct trapframe;
|
||||
struct dirent;
|
||||
struct device;
|
||||
|
||||
// entryother.S / syscall.S
|
||||
void syscall_entry(void);
|
||||
|
|
@ -529,12 +530,10 @@ int fb_ioctl(int request, addr_t arg);
|
|||
// IDE device number. Writes bypass every filesystem above them.
|
||||
int diskread(struct inode*, uint64, char*, int);
|
||||
int diskwrite(struct inode*, uint64, char*, int);
|
||||
// drivers/net/e1000 -- raw Ethernet. No protocol stack above it yet, so these
|
||||
// are the whole interface: queue a frame, take a frame, and the MAC.
|
||||
int e1000_transmit(const void *frame, int len);
|
||||
int e1000_receive(void *out, int cap);
|
||||
const unsigned char *e1000_mac(void);
|
||||
int e1000_present(void);
|
||||
// Network *drivers* are not declared here at all. They are reached through the
|
||||
// netdev class in include/netdev.h -- a driver publishes a vtable and nothing
|
||||
// above it ever learns its name. Declaring e1000_transmit() here was what let
|
||||
// the protocol stack call it directly.
|
||||
|
||||
// net/net.c -- the TCP/IP stack (lwIP). netinit starts the network thread;
|
||||
// net_is_up reports whether an address has been obtained.
|
||||
|
|
@ -542,15 +541,17 @@ void netinit(void);
|
|||
int net_is_up(void);
|
||||
int net_info(char *buf, int cap);
|
||||
|
||||
// net/lwip/ -- the port layer and the interface, called only from net.c.
|
||||
// net/lwip/ -- the port layer and the Ethernet interfaces, called only from
|
||||
// net.c. One netif per bound network device; the index is that interface's.
|
||||
void lwip_sys_init(void);
|
||||
void lwip_sys_lock(void);
|
||||
void lwip_sys_unlock(void);
|
||||
int net_e1000_attach(void);
|
||||
int net_e1000_poll(void);
|
||||
void net_e1000_counts(uint64 *rx, uint64 *tx);
|
||||
struct netif *net_e1000_netif(void);
|
||||
void e1000_selftest(void);
|
||||
int net_eth_attach(struct device *);
|
||||
int net_eth_poll(void);
|
||||
int net_eth_count(void);
|
||||
void net_eth_counts(int i, uint64 *rx, uint64 *tx);
|
||||
struct netif *net_eth_netif(int i);
|
||||
struct device *net_eth_device(int i);
|
||||
|
||||
// registry.c
|
||||
void platform_register_devices(void);
|
||||
|
|
|
|||
165
include/e1000.h
165
include/e1000.h
|
|
@ -1,165 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
// Intel 8254x gigabit Ethernet — the register and descriptor layout.
|
||||
//
|
||||
// From Intel's public "PCI/PCI-X Family of Gigabit Ethernet Controllers
|
||||
// Software Developer's Manual" (317453). A public document with no licence on
|
||||
// what you write from it, which is why this is written rather than ported: see
|
||||
// docs/licensing.md and docs/roadmap.md §1.
|
||||
//
|
||||
// QEMU's `-device e1000` is an 82540EM and implements enough of the manual to
|
||||
// be worth targeting; it is also what the default `-net nic` gives you.
|
||||
|
||||
#define E1000_VENDOR 0x8086
|
||||
|
||||
// The 8254x family: PCI/PCI-X gigabit controllers that share the register map,
|
||||
// descriptor format and EEPROM interface this driver implements.
|
||||
//
|
||||
// The device IDs are transcribed from FreeBSD's `sys/dev/e1000/e1000_hw.h`
|
||||
// (Copyright (c) 2001-2020, Intel Corporation, BSD 3-clause -- verified to
|
||||
// carry no advertising clause; see docs/licensing.md and NOTICE). They are
|
||||
// data, not logic: the driver below is still written from Intel's manual
|
||||
// 317453, and nothing of FreeBSD's implementation is used.
|
||||
//
|
||||
// This matters more than it looks. The driver bound exactly one device -- the
|
||||
// 82540EM QEMU emulates -- which is fine for a virtual machine and useless on
|
||||
// the sort of second-hand hardware a system like this actually gets run on.
|
||||
// These are the cards in every desktop and server of that generation.
|
||||
//
|
||||
// Deliberately *not* included: the PCIe 8257x parts (82571-82574) and anything
|
||||
// newer. They present a similar register map but differ in NVM access and
|
||||
// initialisation, and claiming a device this driver has not been shown to
|
||||
// initialise is worse than not claiming it -- the card binds, reports a MAC of
|
||||
// nonsense, and nothing works for reasons that look nothing like the cause.
|
||||
#define E1000_DEV_82540EM 0x100E
|
||||
#define E1000_DEV_82540EM_LOM 0x1015
|
||||
#define E1000_DEV_82540EP_LOM 0x1016
|
||||
#define E1000_DEV_82540EP 0x1017
|
||||
#define E1000_DEV_82540EP_LP 0x101E
|
||||
#define E1000_DEV_82541EI 0x1013
|
||||
#define E1000_DEV_82541EI_MOBILE 0x1018
|
||||
#define E1000_DEV_82541ER_LOM 0x1014
|
||||
#define E1000_DEV_82541ER 0x1078
|
||||
#define E1000_DEV_82541GI 0x1076
|
||||
#define E1000_DEV_82541GI_MOBILE 0x1077
|
||||
#define E1000_DEV_82541GI_LF 0x107C
|
||||
#define E1000_DEV_82544EI_FIBER 0x1009
|
||||
#define E1000_DEV_82544GC_COPPER 0x100C
|
||||
#define E1000_DEV_82544GC_LOM 0x100D
|
||||
#define E1000_DEV_82545EM_COPPER 0x100F
|
||||
#define E1000_DEV_82545EM_FIBER 0x1011
|
||||
#define E1000_DEV_82545GM_COPPER 0x1026
|
||||
#define E1000_DEV_82545GM_FIBER 0x1027
|
||||
#define E1000_DEV_82545GM_SERDES 0x1028
|
||||
#define E1000_DEV_82546EB_COPPER 0x1010
|
||||
#define E1000_DEV_82546EB_FIBER 0x1012
|
||||
#define E1000_DEV_82546EB_QUAD 0x101D
|
||||
#define E1000_DEV_82546GB_COPPER 0x1079
|
||||
#define E1000_DEV_82546GB_FIBER 0x107A
|
||||
#define E1000_DEV_82546GB_SERDES 0x107B
|
||||
#define E1000_DEV_82547EI 0x1019
|
||||
|
||||
// --- registers, byte offsets into BAR 0 ------------------------------------
|
||||
|
||||
#define E1000_CTRL 0x0000 // device control
|
||||
#define E1000_STATUS 0x0008 // device status
|
||||
#define E1000_EERD 0x0014 // EEPROM read
|
||||
#define E1000_ICR 0x00C0 // interrupt cause read
|
||||
#define E1000_IMS 0x00D0 // interrupt mask set
|
||||
#define E1000_IMC 0x00D8 // interrupt mask clear
|
||||
|
||||
#define E1000_RCTL 0x0100 // receive control
|
||||
#define E1000_TCTL 0x0400 // transmit control
|
||||
#define E1000_TIPG 0x0410 // transmit inter-packet gap
|
||||
|
||||
#define E1000_RDBAL 0x2800 // receive descriptor base, low
|
||||
#define E1000_RDBAH 0x2804 // receive descriptor base, high
|
||||
#define E1000_RDLEN 0x2808 // receive descriptor length, in bytes
|
||||
#define E1000_RDH 0x2810 // receive descriptor head
|
||||
#define E1000_RDT 0x2818 // receive descriptor tail
|
||||
|
||||
#define E1000_TDBAL 0x3800
|
||||
#define E1000_TDBAH 0x3804
|
||||
#define E1000_TDLEN 0x3808
|
||||
#define E1000_TDH 0x3810
|
||||
#define E1000_TDT 0x3818
|
||||
|
||||
#define E1000_MTA 0x5200 // multicast table array, 128 entries
|
||||
#define E1000_RAL 0x5400 // receive address low (the MAC filter)
|
||||
#define E1000_RAH 0x5404 // receive address high
|
||||
|
||||
// CTRL
|
||||
#define E1000_CTRL_RST (1 << 26)
|
||||
#define E1000_CTRL_ASDE (1 << 5) // auto-speed detection
|
||||
#define E1000_CTRL_SLU (1 << 6) // set link up
|
||||
|
||||
// STATUS
|
||||
#define E1000_STATUS_LU (1 << 1) // link up
|
||||
|
||||
// EERD
|
||||
#define E1000_EERD_START (1 << 0)
|
||||
#define E1000_EERD_DONE (1 << 4)
|
||||
#define E1000_EERD_ADDR_SHIFT 8
|
||||
#define E1000_EERD_DATA_SHIFT 16
|
||||
|
||||
// RCTL
|
||||
#define E1000_RCTL_EN (1 << 1) // receiver enable
|
||||
#define E1000_RCTL_SBP (1 << 2) // store bad packets
|
||||
#define E1000_RCTL_UPE (1 << 3) // unicast promiscuous
|
||||
#define E1000_RCTL_MPE (1 << 4) // multicast promiscuous
|
||||
#define E1000_RCTL_LBM_NONE (0 << 6)
|
||||
#define E1000_RCTL_BAM (1 << 15) // accept broadcast
|
||||
#define E1000_RCTL_SZ_2048 (0 << 16) // buffer size, with BSEX clear
|
||||
#define E1000_RCTL_SECRC (1 << 26) // strip the Ethernet CRC
|
||||
|
||||
// TCTL
|
||||
#define E1000_TCTL_EN (1 << 1)
|
||||
#define E1000_TCTL_PSP (1 << 3) // pad short packets
|
||||
#define E1000_TCTL_CT_SHIFT 4 // collision threshold
|
||||
#define E1000_TCTL_COLD_SHIFT 12 // collision distance
|
||||
|
||||
// Transmit descriptor command bits
|
||||
#define E1000_TXD_CMD_EOP (1 << 0) // end of packet
|
||||
#define E1000_TXD_CMD_IFCS (1 << 1) // insert FCS/CRC
|
||||
#define E1000_TXD_CMD_RS (1 << 3) // report status
|
||||
#define E1000_TXD_STAT_DD (1 << 0) // descriptor done
|
||||
|
||||
// Receive descriptor status bits
|
||||
#define E1000_RXD_STAT_DD (1 << 0)
|
||||
#define E1000_RXD_STAT_EOP (1 << 1)
|
||||
|
||||
// Interrupt causes worth unmasking.
|
||||
#define E1000_ICR_TXDW (1 << 0) // transmit descriptor written back
|
||||
#define E1000_ICR_LSC (1 << 2) // link status change
|
||||
#define E1000_ICR_RXDMT0 (1 << 4) // receive descriptor minimum threshold
|
||||
#define E1000_ICR_RXT0 (1 << 7) // receiver timer
|
||||
|
||||
// --- descriptors -----------------------------------------------------------
|
||||
//
|
||||
// Both are 16 bytes and the hardware reads them by physical address, so the
|
||||
// rings must be physically contiguous and 16-byte aligned.
|
||||
|
||||
struct e1000_tx_desc {
|
||||
uint64 addr;
|
||||
uint16 length;
|
||||
uint8 cso; // checksum offset
|
||||
uint8 cmd;
|
||||
uint8 status;
|
||||
uint8 css; // checksum start
|
||||
uint16 special;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct e1000_rx_desc {
|
||||
uint64 addr;
|
||||
uint16 length;
|
||||
uint16 checksum;
|
||||
uint8 status;
|
||||
uint8 errors;
|
||||
uint16 special;
|
||||
} __attribute__((packed));
|
||||
|
||||
// Ring sizes. The manual requires the descriptor ring length to be a multiple
|
||||
// of 128 bytes, i.e. a multiple of 8 descriptors.
|
||||
#define E1000_NTX 16
|
||||
#define E1000_NRX 16
|
||||
#define E1000_BUFSZ 2048
|
||||
63
include/netdev.h
Normal file
63
include/netdev.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#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);
|
||||
|
|
@ -37,3 +37,9 @@
|
|||
// with tens of megabytes; 8 MB would not be.
|
||||
#define NBUF 256
|
||||
#define FSSIZE 150000 // size of file system in blocks
|
||||
|
||||
// Network interfaces the stack will attach. Two is already an ordinary machine
|
||||
// -- a laptop with Ethernet plus a virtual adapter -- and each one costs a
|
||||
// struct netif, so the limit is about keeping a fixed array honest rather than
|
||||
// about memory.
|
||||
#define NETIF_MAX 4
|
||||
|
|
|
|||
|
|
@ -107,4 +107,13 @@ struct pci_driver {
|
|||
// Return 0 to claim the device. A driver that returns non-zero leaves it
|
||||
// unclaimed for anything else that matches.
|
||||
int (*probe)(struct pci_dev *);
|
||||
// Called right after the device is published, for a driver that supports
|
||||
// more than one instance and needs to say which of its own is which.
|
||||
// Optional -- a single-instance driver has nothing to attach.
|
||||
//
|
||||
// It exists because probe() runs *before* device_add(), so a driver has
|
||||
// nowhere to put its per-instance pointer at the point it would want to.
|
||||
// Without this the second card of the same model is either refused or
|
||||
// silently shares the first one's registers.
|
||||
void (*attached)(struct pci_dev *, struct device *);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue