ldls: ping, over a Linux driver

docs/plan/ldls.md L3d, and with it L3 -- the milestone the plan calls the one
that proves the whole idea and the one that cannot be skipped.

    # ldlsd /lib/modules/8139too.ko /lib/modules/btf &
      kpi: the driver read a MAC off the hardware: 52:54:00🆎cd:ef
      ndo_open returned 0
      carrying frames. the interface is ldls0.

    # cat /proc/netif
    interface ldls0   state up   address 10.9.0.15   gateway 10.9.0.2

    # ping 10.9.0.2 4
    64 bytes from 10.9.0.2: icmp_seq=1 time=30 ms
    64 bytes from 10.9.0.2: icmp_seq=2 time=20 ms
    64 bytes from 10.9.0.2: icmp_seq=3 time=20 ms
    64 bytes from 10.9.0.2: icmp_seq=4 time=20 ms

10.9.0.0/24 is reachable only through ldls0, and ldls0 is 8139too running in a
SchOS process. The address was obtained by DHCP over the same driver first.
lwIP -> netdev_ops -> /dev/netdev -> ldlsd -> ndo_start_xmit -> a BAR mapped by
/dev/pci -> the wire, and the whole way back. Nothing in the kernel knows a
Linux driver is involved; nothing in the driver knows it is not in Linux.

It polls rather than taking interrupts. /dev/pci delivers them by blocking
read(), which needs a thread this program does not have, so the driver's NAPI
poll is called from a loop instead -- the same work in a context LDLS can
provide, at the cost of latency and a spinning process.

Four things Linux's alloc_etherdev does that allocating does not, each found by
the driver faulting on it: dev->dev_addr is a pointer aimed at perm_addr;
dev->mc and dev->uc are list heads and are empty only when they point at
themselves; dev->_tx is an array of netdev_queue, because netif_start_queue
clears a bit in _tx[0].state; and __LINK_STATE_START is set by dev_open before
ndo_open is called.

Two did not announce themselves, and they are the ones worth recording.

netif_running(dev) gates 8139too's receive loop. Without it the chip received
the frame, the driver acknowledged the interrupt and left the loop, and nothing
came out -- while every counter said packets were arriving. Found by reading
the chip's own CBR register through the BAR and seeing the hardware had written
600 bytes the driver never collected.

memcpy was still a logging stub. The driver copied each frame into its sk_buff,
the copy did not happen, and the stack was handed exactly the right length of
zeroes; DHCP never completed and nothing reported an error. A stub that returns
zero is not safe for a function whose whole job is a side effect, and the
memory primitives are the worst case because they fail silently and plausibly.
ldls/kpi/string.c exists because of it.

Still one driver, one kernel version, tried once, against an emulated card --
which is what the plan has always promised and is worth repeating next to a
working ping.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
This commit is contained in:
Nexoniarz 2026-09-14 11:14:18 +02:00
commit 888c48fd96
9 changed files with 809 additions and 14 deletions

120
ldls/kpi/string.c Normal file
View file

@ -0,0 +1,120 @@
// SPDX-License-Identifier: Apache-2.0
//
// The string and memory primitives. See ldls/kpi/kpi.h for the provenance rule.
//
// These are the least interesting functions in the layer and the most
// dangerous to leave out, because a stub for one of them does not crash --
// it silently does nothing, and the caller carries on with a buffer full of
// whatever was there before.
//
// That is not hypothetical. `memcpy` was a logging stub while the receive path
// was being brought up: the driver copied each frame into its skb, the copy
// did not happen, and the stack was handed the right *length* of zeroes. Every
// counter said packets were arriving and DHCP never completed. A crash would
// have been easier to find.
#include "types.h"
#include "kpi.h"
void *
kpi_memcpy(void *d, const void *s, unsigned long n)
{
unsigned char *a = (unsigned char *)d;
const unsigned char *b = (const unsigned char *)s;
while (n--) *a++ = *b++;
return d;
}
void *
kpi_memmove(void *d, const void *s, unsigned long n)
{
unsigned char *a = (unsigned char *)d;
const unsigned char *b = (const unsigned char *)s;
if (a == b || n == 0)
return d;
if (a < b) {
while (n--) *a++ = *b++;
} else {
a += n; b += n;
while (n--) *--a = *--b;
}
return d;
}
void *
kpi_memset(void *d, int c, unsigned long n)
{
unsigned char *a = (unsigned char *)d;
while (n--) *a++ = (unsigned char)c;
return d;
}
int
kpi_memcmp(const void *x, const void *y, unsigned long n)
{
const unsigned char *a = x, *b = y;
while (n--) { if (*a != *b) return (int)*a - (int)*b; a++; b++; }
return 0;
}
unsigned long
kpi_strlen(const char *s)
{
unsigned long n = 0;
while (s[n]) n++;
return n;
}
unsigned long
kpi_strnlen(const char *s, unsigned long max)
{
unsigned long n = 0;
while (n < max && s[n]) n++;
return n;
}
// sized_strscpy(dest, src, count) -- a bounded copy that always terminates and
// reports the length copied, or -E2BIG when it did not fit.
long
kpi_sized_strscpy(char *d, const char *s, unsigned long n)
{
unsigned long i = 0;
if (n == 0)
return -7; /* -E2BIG */
while (i + 1 < n && s[i]) { d[i] = s[i]; i++; }
d[i] = 0;
return s[i] ? -7 : (long)i;
}
// The io variants. On this platform a BAR is ordinary memory once mapped, so
// these differ from their plain counterparts only in that the compiler must
// not reorder or elide them -- which is what the volatile is for.
void
kpi_memcpy_fromio(void *d, const void *src, unsigned long n)
{
unsigned char *a = (unsigned char *)d;
const volatile unsigned char *b = (const volatile unsigned char *)src;
while (n--) *a++ = *b++;
}
void
kpi_memcpy_toio(void *dst, const void *s, unsigned long n)
{
volatile unsigned char *a = (volatile unsigned char *)dst;
const unsigned char *b = (const unsigned char *)s;
while (n--) *a++ = *b++;
}
// __fortify_panic is what a driver's bounds-checked string helpers call when
// they detect an overflow. Reaching it means the driver found a bug in itself,
// and the honest thing is to say so rather than return.
void
kpi_fortify_panic(void)
{
kpi_log("kpi: the driver called __fortify_panic -- it detected an "
"overflow in itself\n");
for (;;)
;
}