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
149
kernel/net/net.c
149
kernel/net/net.c
|
|
@ -4,11 +4,15 @@
|
|||
//
|
||||
// lwIP with NO_SYS=1 has no threads of its own, so something has to feed it
|
||||
// received frames and run its timers. That something is a kernel thread rather
|
||||
// than the e1000 interrupt handler, and the reason is the lock: a call into
|
||||
// than a driver's interrupt handler, and the reason is the lock: a call into
|
||||
// lwIP can reach the driver, the driver can sleep, and an interrupt handler
|
||||
// cannot. So the interrupt keeps doing what it always did -- clear the cause
|
||||
// register -- and this thread polls.
|
||||
//
|
||||
// Nothing here names a driver. Interfaces come from the netdev class
|
||||
// (include/netdev.h): every bound network device is attached, and a machine
|
||||
// with two NICs gets two interfaces without a line changing in this file.
|
||||
//
|
||||
// Polling costs a wakeup every few milliseconds when the link is idle. The
|
||||
// alternative, waking this thread from the interrupt, is a worthwhile change
|
||||
// later; it is deliberately not the first version, because a receive path that
|
||||
|
|
@ -20,6 +24,8 @@
|
|||
#include "spinlock.h"
|
||||
#include "sleeplock.h"
|
||||
#include "proc.h"
|
||||
#include "device.h"
|
||||
#include "netdev.h"
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/init.h"
|
||||
|
|
@ -66,42 +72,63 @@ net_thread(void)
|
|||
|
||||
lwip_sys_lock();
|
||||
lwip_init();
|
||||
if (net_e1000_attach() != 0) {
|
||||
|
||||
// Every bound network device becomes an interface. DHCP is started on each
|
||||
// of them: which one answers is the network's business, not ours, and an
|
||||
// interface left unconfigured because it happened to be second would be a
|
||||
// rule nobody could see.
|
||||
int attached = 0;
|
||||
for (int i = 0; i < netdev_count(); i++) {
|
||||
struct device *d = netdev_at(i);
|
||||
int idx = net_eth_attach(d);
|
||||
if (idx < 0)
|
||||
continue;
|
||||
dhcp_start(net_eth_netif(idx));
|
||||
attached++;
|
||||
cprintf("net: %s attached as interface %d\n", d->name, idx);
|
||||
}
|
||||
|
||||
if (attached == 0) {
|
||||
lwip_sys_unlock();
|
||||
cprintf("net: no e1000; the network thread has nothing to drive\n");
|
||||
cprintf("net: no network device; the thread has nothing to drive\n");
|
||||
// Not a panic and not an exit: a kernel thread must never return, and
|
||||
// a machine with no NIC is an ordinary machine.
|
||||
for (;;)
|
||||
wait_tick();
|
||||
}
|
||||
struct netif *nif = net_e1000_netif();
|
||||
dhcp_start(nif);
|
||||
lwip_sys_unlock();
|
||||
|
||||
cprintf("net: lwIP %d.%d.%d up, requesting an address\n",
|
||||
LWIP_VERSION_MAJOR, LWIP_VERSION_MINOR, LWIP_VERSION_REVISION);
|
||||
cprintf("net: lwIP %d.%d.%d up on %d interface%s, requesting an address\n",
|
||||
LWIP_VERSION_MAJOR, LWIP_VERSION_MINOR, LWIP_VERSION_REVISION,
|
||||
attached, attached == 1 ? "" : "s");
|
||||
|
||||
int announced = 0;
|
||||
// One bit per interface, so a second one binding later is announced too.
|
||||
int announced[NETIF_MAX] = { 0 };
|
||||
|
||||
for (;;) {
|
||||
int moved;
|
||||
|
||||
lwip_sys_lock();
|
||||
moved = net_e1000_poll();
|
||||
moved = net_eth_poll();
|
||||
sys_check_timeouts();
|
||||
|
||||
// Report the address once, when it first arrives. Anyone who has spent
|
||||
// Report each address once, when it first arrives. Anyone who has spent
|
||||
// an evening wondering whether the guest has an address at all wants
|
||||
// this on the console rather than in a file they have to know about.
|
||||
if (!announced && !ip4_addr_isany_val(*netif_ip4_addr(nif))) {
|
||||
for (int i = 0; i < attached; i++) {
|
||||
struct netif *nif = net_eth_netif(i);
|
||||
if (announced[i] || ip4_addr_isany_val(*netif_ip4_addr(nif)))
|
||||
continue;
|
||||
|
||||
const ip4_addr_t *a = netif_ip4_addr(nif);
|
||||
const ip4_addr_t *m = netif_ip4_netmask(nif);
|
||||
const ip4_addr_t *gw = netif_ip4_gw(nif);
|
||||
cprintf("net: %d.%d.%d.%d/%d.%d.%d.%d via %d.%d.%d.%d\n",
|
||||
cprintf("net: %s %d.%d.%d.%d/%d.%d.%d.%d via %d.%d.%d.%d\n",
|
||||
net_eth_device(i)->name,
|
||||
ip4_addr1(a), ip4_addr2(a), ip4_addr3(a), ip4_addr4(a),
|
||||
ip4_addr1(m), ip4_addr2(m), ip4_addr3(m), ip4_addr4(m),
|
||||
ip4_addr1(gw), ip4_addr2(gw), ip4_addr3(gw), ip4_addr4(gw));
|
||||
announced = 1;
|
||||
announced[i] = 1;
|
||||
net_bound = 1;
|
||||
}
|
||||
lwip_sys_unlock();
|
||||
|
|
@ -183,65 +210,83 @@ put_field(char *out, const char *name)
|
|||
return n;
|
||||
}
|
||||
|
||||
// One block of fields per interface, separated by a blank line. A reader that
|
||||
// can parse one can parse any number, which is the whole reason it is not a
|
||||
// single flat list of values: with two NICs a flat list has no way to say which
|
||||
// address belongs to which.
|
||||
int
|
||||
net_info(char *buf, int cap)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
if (cap < 512)
|
||||
// Room for the largest block, so the loop below never needs a bounds check
|
||||
// in the middle of writing one.
|
||||
if (cap < 256 * (NETIF_MAX + 1))
|
||||
return 0;
|
||||
|
||||
if (!net_started) {
|
||||
if (!net_started || net_eth_count() == 0) {
|
||||
len += put_field(buf + len, "state");
|
||||
len += put_str(buf + len, e1000_present() ? "down\n" : "no interface\n");
|
||||
len += put_str(buf + len, netdev_count() ? "down\n" : "no interface\n");
|
||||
return len;
|
||||
}
|
||||
|
||||
struct netif *nif = net_e1000_netif();
|
||||
for (int i = 0; i < net_eth_count(); i++) {
|
||||
struct netif *nif = net_eth_netif(i);
|
||||
struct device *dev = net_eth_device(i);
|
||||
|
||||
lwip_sys_lock();
|
||||
if (i)
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "interface");
|
||||
buf[len++] = nif->name[0];
|
||||
buf[len++] = nif->name[1];
|
||||
len += put_u(buf + len, nif->num);
|
||||
buf[len++] = '\n';
|
||||
lwip_sys_lock();
|
||||
|
||||
len += put_field(buf + len, "mac");
|
||||
len += put_mac(buf + len, nif->hwaddr);
|
||||
buf[len++] = '\n';
|
||||
len += put_field(buf + len, "interface");
|
||||
len += put_str(buf + len, dev->name);
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "state");
|
||||
len += put_str(buf + len, net_bound ? "up\n" : "no address\n");
|
||||
len += put_field(buf + len, "driver");
|
||||
len += put_str(buf + len, dev->driver ? dev->driver : "-");
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "address");
|
||||
len += put_ip4(buf + len, netif_ip4_addr(nif));
|
||||
buf[len++] = '\n';
|
||||
len += put_field(buf + len, "mac");
|
||||
len += put_mac(buf + len, nif->hwaddr);
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "netmask");
|
||||
len += put_ip4(buf + len, netif_ip4_netmask(nif));
|
||||
buf[len++] = '\n';
|
||||
// Per interface, not per system: with two NICs one can be configured
|
||||
// while the other is still asking.
|
||||
len += put_field(buf + len, "state");
|
||||
len += put_str(buf + len,
|
||||
ip4_addr_isany_val(*netif_ip4_addr(nif)) ? "no address\n"
|
||||
: "up\n");
|
||||
|
||||
len += put_field(buf + len, "gateway");
|
||||
len += put_ip4(buf + len, netif_ip4_gw(nif));
|
||||
buf[len++] = '\n';
|
||||
len += put_field(buf + len, "address");
|
||||
len += put_ip4(buf + len, netif_ip4_addr(nif));
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "mtu");
|
||||
len += put_u(buf + len, nif->mtu);
|
||||
buf[len++] = '\n';
|
||||
len += put_field(buf + len, "netmask");
|
||||
len += put_ip4(buf + len, netif_ip4_netmask(nif));
|
||||
buf[len++] = '\n';
|
||||
|
||||
lwip_sys_unlock();
|
||||
len += put_field(buf + len, "gateway");
|
||||
len += put_ip4(buf + len, netif_ip4_gw(nif));
|
||||
buf[len++] = '\n';
|
||||
|
||||
uint64 rx, tx;
|
||||
net_e1000_counts(&rx, &tx);
|
||||
len += put_field(buf + len, "mtu");
|
||||
len += put_u(buf + len, nif->mtu);
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "rx_frames");
|
||||
len += put_u(buf + len, rx);
|
||||
buf[len++] = '\n';
|
||||
lwip_sys_unlock();
|
||||
|
||||
len += put_field(buf + len, "tx_frames");
|
||||
len += put_u(buf + len, tx);
|
||||
buf[len++] = '\n';
|
||||
uint64 rx, tx;
|
||||
net_eth_counts(i, &rx, &tx);
|
||||
|
||||
len += put_field(buf + len, "rx_frames");
|
||||
len += put_u(buf + len, rx);
|
||||
buf[len++] = '\n';
|
||||
|
||||
len += put_field(buf + len, "tx_frames");
|
||||
len += put_u(buf + len, tx);
|
||||
buf[len++] = '\n';
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
|
@ -258,9 +303,9 @@ netinit(void)
|
|||
// depends on that reasoning holding forever is not worth the two lines
|
||||
// saved.
|
||||
inet_init();
|
||||
if (!e1000_present()) {
|
||||
// Silent: a machine without an e1000 is not misconfigured, and this
|
||||
// runs on every boot.
|
||||
if (netdev_count() == 0) {
|
||||
// Silent: a machine with no network device is not misconfigured, and
|
||||
// this runs on every boot.
|
||||
return;
|
||||
}
|
||||
net_started = 1;
|
||||
|
|
|
|||
Loading…
Reference in a new issue