net: a frame wakes the stack, instead of waiting for the clock
The network thread waited on the timer and nothing else, so a frame that landed just after a poll sat in the ring for up to 10 ms -- the largest fixed latency in the system. That was deliberate when the stack was new (a receive path woken by an interrupt is much harder to debug than one that is asked) and there is now a working stack to compare against, which is exactly when it becomes safe to do. The frame is still *pulled* by the thread through .receive. It has to be: a call into lwIP can reach the driver, the driver can sleep on the buffer cache, and an interrupt handler may do neither. netdev_rx_signal() delivers nothing and reads no ring -- it only ends the wait early. The wait stays on the timer tick rather than moving to a channel of its own, and that is the design. lwIP's timers must still run when the link is silent, so the clock has to remain a floor under the wait, and sleep() cannot wait on two things at once. Joining the tick's existing wakeup costs nothing and leaves one wait with one predicate. rx_signal lives under tickslock for the same reason it gets no lock of its own: sleep() releases tickslock atomically, so a driver setting the flag and waking under that lock cannot lose the wakeup by arriving between the predicate test and the sleep. A private lock would not be held across the sleep, and that window would be back. Calling it from interrupt context is safe on the same terms the timer handler already relies on -- acquire() disables interrupts, so a CPU holding tickslock cannot be interrupted into it. The e1000 signals on RXT0 (a frame or burst has landed) and RXDMT0 (the ring has fallen below its threshold, which is when waiting for the clock hurts most). Measured rather than asserted: /proc/netif reports wake_rx and wake_tick, and across three pings rx_frames and wake_rx both went 2 -> 6 -- every received frame woke the thread through the interrupt. wake_tick keeps advancing as the idle floor, which is what lwIP's timers need. socktest still passes and ping is still 0% loss. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
parent
e80f7b287b
commit
3034efd300
7 changed files with 138 additions and 17 deletions
|
|
@ -250,7 +250,18 @@ e1000_intr(void *arg)
|
|||
// Reading ICR clears it. Not reading it leaves the cause asserted, and with a
|
||||
// level-triggered PCI interrupt that means the handler is re-entered forever.
|
||||
uint32 cause = rd(e, E1000_ICR);
|
||||
(void)cause;
|
||||
|
||||
// Nothing is read from the ring here. The frame is pulled by the network
|
||||
// thread through .receive, which may sleep on the buffer cache and on the
|
||||
// lwIP lock; an interrupt handler may do neither. All this does is end that
|
||||
// thread's wait, so the frame is taken now instead of up to 10 ms later.
|
||||
//
|
||||
// RXT0 is the receive timer expiring -- a frame, or a burst, has landed.
|
||||
// RXDMT0 is the ring falling below its minimum threshold, which means frames
|
||||
// are arriving faster than they are being taken and is exactly when waiting
|
||||
// for the clock hurts most.
|
||||
if (cause & (E1000_ICR_RXT0 | E1000_ICR_RXDMT0))
|
||||
netdev_rx_signal();
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
|
|||
|
|
@ -49,6 +49,73 @@ wait_tick(void)
|
|||
release(&tickslock);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Waking on a received frame
|
||||
//
|
||||
// The thread used to wait for the clock and nothing else, so a frame arriving
|
||||
// just after a poll sat in the ring for up to 10 ms. That was deliberate at the
|
||||
// time -- a receive path woken by an interrupt is much harder to debug than one
|
||||
// that is asked, and the stack had to be proven first -- and it is now the
|
||||
// largest fixed latency in the system.
|
||||
//
|
||||
// The wait stays on `ticks`, rather than moving to a channel of its own, and
|
||||
// that is the whole design. lwIP's timers must still run when the link is
|
||||
// silent, so the clock has to remain a floor under the wait; a second channel
|
||||
// would mean sleeping on two things at once, which sleep() cannot do. Joining
|
||||
// the tick's existing wakeup costs nothing and keeps one wait, one predicate.
|
||||
//
|
||||
// `rx_signal` lives under tickslock for the same reason it is not its own lock:
|
||||
// sleep() releases tickslock atomically, so a driver that sets the flag and
|
||||
// wakes under that lock cannot lose the wakeup by arriving in the window
|
||||
// between the predicate test and the sleep. A private lock would not be held
|
||||
// across the sleep, and that window would be back.
|
||||
//
|
||||
// Called from interrupt context. acquire() disables interrupts, so a CPU
|
||||
// holding tickslock cannot be interrupted into this; another CPU spins briefly.
|
||||
// That is the same arrangement the timer handler itself uses.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static int rx_signal;
|
||||
|
||||
// Counted so the mechanism is observable rather than assumed: if `rx` stays
|
||||
// zero while traffic flows, the interrupt is not arriving and the thread is
|
||||
// running on the clock exactly as it did before.
|
||||
static uint64 wake_rx, wake_tick;
|
||||
|
||||
void
|
||||
netdev_rx_signal(void)
|
||||
{
|
||||
acquire(&tickslock);
|
||||
rx_signal = 1;
|
||||
wakeup(&ticks);
|
||||
release(&tickslock);
|
||||
}
|
||||
|
||||
void
|
||||
net_wake_counts(uint64 *rx, uint64 *tick)
|
||||
{
|
||||
if (rx) *rx = wake_rx;
|
||||
if (tick) *tick = wake_tick;
|
||||
}
|
||||
|
||||
// Sleep until there is something to do: a frame has been signalled, or the
|
||||
// clock has moved and lwIP's timers are due.
|
||||
static void
|
||||
wait_work(void)
|
||||
{
|
||||
acquire(&tickslock);
|
||||
uint start = ticks;
|
||||
while (ticks == start && !rx_signal)
|
||||
sleep(&ticks, &tickslock);
|
||||
if (rx_signal) {
|
||||
wake_rx++;
|
||||
rx_signal = 0;
|
||||
} else {
|
||||
wake_tick++;
|
||||
}
|
||||
release(&tickslock);
|
||||
}
|
||||
|
||||
static int net_started;
|
||||
|
||||
// Set once DHCP has bound, so `ifconfig`-ish output and /proc/net have
|
||||
|
|
@ -160,7 +227,7 @@ net_thread(void)
|
|||
// Busy while there is traffic, idle otherwise. Sleeping even 1 ms with
|
||||
// frames waiting would cap throughput at one burst per tick.
|
||||
if (moved == 0)
|
||||
wait_tick();
|
||||
wait_work();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -312,6 +379,21 @@ net_info(char *buf, int cap)
|
|||
buf[len++] = '\n';
|
||||
}
|
||||
|
||||
// How the network thread is being woken, for the whole stack rather than
|
||||
// per interface. `wake_rx` staying at zero while frames arrive means the
|
||||
// receive interrupt is not reaching us and the thread is running on the
|
||||
// clock exactly as it did before -- which works, and is 10 ms slower.
|
||||
{
|
||||
uint64 wrx, wtick;
|
||||
net_wake_counts(&wrx, &wtick);
|
||||
len += put_field(buf + len, "wake_rx");
|
||||
len += put_u(buf + len, wrx);
|
||||
buf[len++] = '\n';
|
||||
len += put_field(buf + len, "wake_tick");
|
||||
len += put_u(buf + len, wtick);
|
||||
buf[len++] = '\n';
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue