Просмотр исходного кода

Move the network commands to ring 3 (net service syscalls)

The NIC and the stack stay in the kernel; ring-3 commands get the
configuration, the DNS/DHCP clients and the raw frame paths, so the
packet-level commands port over their existing frame code unchanged.

- sys_net.c/h: SYS_NET_INFO/_PRINT_INFO (addresses, MAC, driver status
  line, mirror config), the get/set calls for ip/mask/gw/dns, the raw
  paths (NET_TX/RX/DECODE/REPLY/LOOPBACK/REGS/RESET), DHCP_RUN/INFO,
  DNS_RESOLVE and the netlog send/config calls.
- The receiver is owned by one process at a time (netdev_acquire): the
  background kexec watcher already pauses while a command owns it.  A
  killed command used to leave it owned forever (the watcher stayed
  paused); netdev.c now remembers the owner and the scheduler's reaper
  force-releases it (netdev_owner/netdev_release_all via sys_net_reap).
- Moved: netinfo, netip, netdns, host, dhcp, ping, pong, netsend,
  netrecv, netlog, netconsole, netregs, netloop, netreset.  netcmd.c is
  gone and dns.c keeps only the resolver.  ping kept its frame
  building/parsing and the verbose ARP trail (now with a local checksum
  and the raw syscalls).
- 24 of the shell commands are ring-3 now; the kernel keeps the
  privileged ones plus curl (needs a TCP API), display and lsusb.

Verified on QEMU (slirp + ne2k_pci, QMP keyboard + the netconsole
mirror): netinfo shows ne2000/RTL8029AS, 10.0.2.15/24, gw 10.0.2.2,
dns 10.0.2.3 and the DHCP lease; netip/netdns show and set; dhcp
re-runs; ping 10.0.2.2 prints the ARP trail, "is-at" and 4/4 replies;
netsend/netrecv/netloop/netregs/netreset/netlog/netconsole run; pong
starts and is killed by Ctrl+C (and releases the NIC).

AGENTS.md: the command lists, the net service and the updated known
issues.
Johannes Zellner 1 месяц назад
Родитель
Сommit
7af58a6291
27 измененных файлов с 1369 добавлено и 904 удалено
  1. 27 11
      AGENTS.md
  2. 9 6
      Makefile
  3. 0 49
      dns.c
  4. 0 808
      netcmd.c
  5. 20 2
      netdev.c
  6. 5 0
      netdev.h
  7. 2 0
      process.c
  8. 206 0
      sys_net.c
  9. 35 0
      sys_net.h
  10. 55 0
      syscall.c
  11. 40 0
      syscall.h
  12. 57 28
      terminal.c
  13. 16 0
      ucmd_dhcp.c
  14. 18 0
      ucmd_host.c
  15. 59 0
      ucmd_lib.h
  16. 67 0
      ucmd_netconsole.c
  17. 38 0
      ucmd_netdns.c
  18. 49 0
      ucmd_netinfo.c
  19. 27 0
      ucmd_netip.c
  20. 28 0
      ucmd_netlog.c
  21. 67 0
      ucmd_netloop.c
  22. 51 0
      ucmd_netrecv.c
  23. 21 0
      ucmd_netregs.c
  24. 25 0
      ucmd_netreset.c
  25. 40 0
      ucmd_netsend.c
  26. 353 0
      ucmd_ping.c
  27. 54 0
      ucmd_pong.c

+ 27 - 11
AGENTS.md

@@ -185,15 +185,24 @@ below the run area), so adding code no longer moves any address.
   in the kernel image (`*_embed.o`) and listed as `CMD_USER` rows in the
   terminal's command registry (`cmds[]` + `ucmds[]`).  Moved so far:
   `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
-  `mouseinfo`, `storage`.  They talk to the kernel through `int 0x80`
+  `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`,
+  `ping`, `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`,
+  `netloop`, `netreset`.  They talk to the kernel through `int 0x80`
   (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
-  `sys_fs.c` and the small state snapshots in `sys_info.c`) and inherit the
-  terminal's screen, so their output lands in the owning window.  New
-  commands: add `ucmd_<name>.c`, a `CMD_USER` row in `cmds[]`, and a blob in
-  `ucmds[]` (the Makefile picks the file up via its `ucmd_%` pattern rules).
-  The one argument is passed as a C string on the top of the user stack
-  (`process_create_user(..., arg)`), so `_start(const char *arg)` needs no
-  crt0.
+  `sys_fs.c`, the state snapshots in `sys_info.c` and the network service in
+  `sys_net.c`) and inherit the terminal's screen, so their output lands in
+  the owning window.  New commands: add `ucmd_<name>.c`, a `CMD_USER` row in
+  `cmds[]`, and a blob in `ucmds[]` (the Makefile picks the file up via its
+  `ucmd_%` pattern rules).  The one argument is passed as a C string on the
+  top of the user stack (`process_create_user(..., arg)`), so
+  `_start(const char *arg)` needs no crt0.
+- **Network service**: `sys_net.c/h` — the NIC and the stack stay in the
+  kernel; ring-3 commands get `SYS_NET_INFO`/config, the DNS and DHCP clients
+  and the raw frame paths (`NET_TX/RX/DECODE/REPLY/LOOPBACK/REGS/RESET`), so
+  the packet-level commands ported unchanged.  The receiver is owned by one
+  process at a time (`netdev_acquire`): the background kexec watcher pauses
+  while a command owns it, and the reaper force-releases it if that process
+  dies (`sys_net_reap`, driven by `netdev_owner()`/`netdev_release_all()`).
 
 ## Shell commands
 
@@ -229,9 +238,11 @@ nested). `/` is the read-only VFS root listing the mounted filesystems (the
 filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
-`mouseinfo` and `storage` run as ring-3 processes (embedded `ucmd_*.c`); the
-rest are kernel commands spawned as processes, or shell builtins (`cd`,
-`clear`, `help`).
+`mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
+`pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`
+and `netreset` run as ring-3 processes (embedded `ucmd_*.c`); the rest are
+kernel commands spawned as processes, or shell builtins (`cd`, `clear`,
+`help`).
 
 ## Serial hot-reload (kexec) workflow
 
@@ -571,3 +582,8 @@ Caveats:
 7. Killing/reaping a ring-3 process frees its kernel stack and page directory
    but not the user pages mapped into it (a few pages leak per command run;
    needs a page-table walk/unmap on reap).
+8. `curl` (needs a TCP socket API), `display` (needs a GPU snapshot) and
+   `lsusb` (a 45-byte wrapper around the USB tree printer) are the last
+   commands still living in the kernel, plus the privileged ones (`peek`,
+   `poke`, `dbg`, `serial`, `kexec`, `reboot`, `poweroff`, `format`,
+   `run`/`prog`/`space`, `ps`/`kill`).

+ 9 - 6
Makefile

@@ -143,9 +143,6 @@ ne2000.o: ne2000.c ne2000.h pci.h timer.h io.h
 r8168.o: r8168.c r8168.h netdev.h io.h kdbg.h mm.h paging.h pci.h process.h serial.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-netcmd.o: netcmd.c io.h timer.h netdev.h net.h
-	$(CC) $(CFLAGS) -o $@ $<
-
 netkexec.o: netkexec.c netkexec.h io.h kexec.h net.h netdev.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -248,10 +245,13 @@ sys_fs.o: sys_fs.c sys_fs.h syscall.h fat.h vfs.h
 sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h mm.h mouse.h usbhid.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o ucmd_netinfo_embed.o ucmd_netip_embed.o ucmd_netdns_embed.o ucmd_host_embed.o ucmd_dhcp_embed.o ucmd_netsend_embed.o ucmd_netrecv_embed.o ucmd_ping_embed.o ucmd_pong_embed.o ucmd_netlog_embed.o ucmd_netconsole_embed.o ucmd_netregs_embed.o ucmd_netloop_embed.o ucmd_netreset_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -337,7 +337,10 @@ prog: prog.bin
 # deliver them too.  New command: ucmd_<name>.c + a CMD_USER row in cmds[] and a
 # blob in ucmds[].
 UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
-        ucmd_meminfo ucmd_mouseinfo ucmd_storage
+        ucmd_meminfo ucmd_mouseinfo ucmd_storage \
+        ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
+        ucmd_netsend ucmd_netrecv ucmd_ping ucmd_pong ucmd_netlog \
+        ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -523,4 +526,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 0 - 49
dns.c

@@ -297,52 +297,3 @@ int dns_resolve(const char *name, unsigned char ip[4])
 	return -1;
 }
 
-/* ---- commands ---- */
-
-void program_netdns_main(const char *arg)
-{
-	unsigned char dns[4], gw[4];
-
-	if (arg[0] == '\0') {
-		net_get_dns(dns);
-		if (dns[0] || dns[1] || dns[2] || dns[3]) {
-			io_putstr("dns=");
-			put_ip(dns);
-			io_putchar('\n');
-			return;
-		}
-		net_get_gateway(gw);
-		io_putstr("dns: unset");
-		if (gw[0] || gw[1] || gw[2] || gw[3]) {
-			io_putstr(" (fallback: gateway ");
-			put_ip(gw);
-			io_putchar(')');
-		}
-		io_putchar('\n');
-		return;
-	}
-
-	if (net_parse_ip(arg, dns) != 0) {
-		io_putstr("netdns: usage netdns [a.b.c.d]\n");
-		return;
-	}
-
-	net_set_dns(dns);
-	io_putstr("dns set to ");
-	put_ip(dns);
-	io_putchar('\n');
-}
-
-void program_host_main(const char *arg)
-{
-	unsigned char ip[4];
-
-	if (arg[0] == '\0') {
-		io_putstr("host: usage host <name>\n");
-		return;
-	}
-
-	netdev_acquire();
-	dns_resolve(arg, ip);
-	netdev_release();
-}

+ 0 - 808
netcmd.c

@@ -1,808 +0,0 @@
-/* Network commands: netinfo, netrecv, netsend, netip, pong (responder) and
- * ping (active ARP + ICMP echo), over the netdev NIC layer. */
-
-#include "io.h"
-#include "timer.h"
-#include "netdev.h"
-#include "net.h"
-#include "dhcp.h"
-#include "dns.h"
-#include "netlog.h"
-
-/* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
-   hold a full-size frame, and keeping them out of BSS-as-much-as-possible
-   avoids bloating the kernel image). Max Ethernet frame is
-   1518 bytes (14 header + 1500 payload + 4 VLAN). */
-#define NET_FRAME_MAX 1518
-static unsigned char net_rx_buf[NET_FRAME_MAX];
-static unsigned char net_reply_buf[NET_FRAME_MAX];
-
-static void put_hex2(unsigned char v)
-{
-	static const char hex[] = "0123456789ABCDEF";
-	io_putchar(hex[(v >> 4) & 0xF]);
-	io_putchar(hex[v & 0xF]);
-}
-
-static void put_dec(unsigned int v)
-{
-	char b[12];
-	int i = 0;
-
-	if (v == 0) {
-		io_putchar('0');
-		return;
-	}
-	while (v) {
-		b[i++] = (char)('0' + (v % 10));
-		v /= 10;
-	}
-	while (i)
-		io_putchar(b[--i]);
-}
-
-static void put_mac(const unsigned char *m)
-{
-	unsigned int i;
-	for (i = 0; i < 6; i++) {
-		if (i)
-			io_putchar(':');
-		put_hex2(m[i]);
-	}
-}
-
-static void put_ip(const unsigned char *ip)
-{
-	unsigned int i;
-	for (i = 0; i < 4; i++) {
-		if (i)
-			io_putchar('.');
-		put_dec(ip[i]);
-	}
-}
-
-static int ip_eq(const unsigned char *a, const unsigned char *b)
-{
-	return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
-}
-
-/* ---- netinfo ---- */
-
-void program_netinfo_main(void)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned char ip[4], mask[4], gw[4], server[4];
-	unsigned int lease = 0;
-
-	if (!nd) {
-		io_putstr("netinfo: no NIC found\n");
-		return;
-	}
-	io_putstr("netdev: ");
-	io_putstr(nd->name);
-	io_putstr(" (");
-	io_putstr(nd->chip);
-	io_putstr(")\n");
-	nd->print_info();
-
-	net_get_ip(ip);
-	net_get_mask(mask);
-	net_get_gateway(gw);
-	io_putstr("ip=");
-	put_ip(ip);
-	if (mask[0] || mask[1] || mask[2] || mask[3]) {
-		io_putstr("/");
-		put_ip(mask);
-	}
-	io_putchar('\n');
-	if (gw[0] || gw[1] || gw[2] || gw[3]) {
-		io_putstr("gw=");
-		put_ip(gw);
-		io_putchar('\n');
-	}
-	{
-		unsigned char dns[4];
-
-		net_get_dns(dns);
-		if (dns[0] || dns[1] || dns[2] || dns[3]) {
-			io_putstr("dns=");
-			put_ip(dns);
-			io_putchar('\n');
-		}
-	}
-	if (dhcp_last_info(server, &lease)) {
-		io_putstr("dhcp: server ");
-		put_ip(server);
-		io_putstr(" lease ");
-		put_dec(lease);
-		io_putstr("s\n");
-	}
-}
-
-/* ---- dhcp ---- */
-
-void program_dhcp_main(void)
-{
-	switch (dhcp_run(4000)) {
-	case 0:
-		break;                  /* dhcp_run() printed the lease */
-	case -1:
-		/* message already printed */
-		break;
-	default:
-		io_putstr("dhcp: no NIC\n");
-		break;
-	}
-}
-
-/* ---- netregs: driver register dump ---- */
-
-void program_netregs_main(void)
-{
-	const struct netdev *nd = netdev_active();
-
-	if (!nd) {
-		io_putstr("netregs: no NIC found\n");
-		return;
-	}
-	if (!nd->dump_regs) {
-		io_putstr("netregs: no register dump for ");
-		io_putstr(nd->name);
-		io_putchar('\n');
-		return;
-	}
-	nd->dump_regs();
-}
-
-/* ---- netrecv ---- */
-
-void program_netrecv_main(void)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned int deadline, count = 0;
-
-	if (!nd) {
-		io_putstr("netrecv: no NIC found\n");
-		return;
-	}
-
-	io_putstr("netrecv: listening (8s)...\n");
-	netdev_acquire();
-	deadline = get_ticks_ms() + 8000;
-
-	while (get_ticks_ms() < deadline) {
-		int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
-		unsigned int i;
-
-		if (len < 0)
-			continue;
-
-		net_decode_frame(net_rx_buf, (unsigned int)len);
-		io_putchar('\n');
-
-		/* Raw hex dump for closer inspection. */
-		for (i = 0; i < (unsigned int)len; i++) {
-			put_hex2(net_rx_buf[i]);
-			io_putchar(' ');
-			if ((i & 15) == 15)
-				io_putchar('\n');
-		}
-		if (((unsigned int)len & 15) != 0)
-			io_putchar('\n');
-
-		if (++count >= 32)
-			break;
-	}
-
-	io_putstr("netrecv: ");
-	put_dec(count);
-	io_putstr(" frame(s)\n");
-	netdev_release();
-}
-
-/* ---- netsend ---- */
-
-void program_netsend_main(void)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned char frame[64];
-	unsigned char mac[6];
-	unsigned int i;
-
-	if (!nd) {
-		io_putstr("netsend: no NIC found\n");
-		return;
-	}
-
-	nd->get_mac(mac);
-
-	/* Broadcast test frame: dst=ff:ff:ff:ff:ff:ff, src=our MAC,
-	   ethertype 0x88B5 (local experimental), payload "NEBS". */
-	for (i = 0; i < 6; i++)
-		frame[i] = 0xFF;
-	for (i = 0; i < 6; i++)
-		frame[6 + i] = mac[i];
-	frame[12] = 0x88;
-	frame[13] = 0xB5;
-	frame[14] = 'N';
-	frame[15] = 'E';
-	frame[16] = 'B';
-	frame[17] = 'S';
-	for (i = 18; i < 64; i++)
-		frame[i] = 0x00;
-
-	if (nd->tx(frame, sizeof(frame)) != 0) {
-		io_putstr("netsend: transmit failed\n");
-		return;
-	}
-	io_putstr("netsend: sent 64-byte broadcast frame\n");
-}
-
-/* ---- netip ---- */
-
-void program_netip_main(const char *arg)
-{
-	unsigned char ip[4];
-
-	if (arg[0] == '\0') {
-		net_print_ip();
-		io_putchar('\n');
-		return;
-	}
-
-	if (net_parse_ip(arg, ip) != 0) {
-		io_putstr("netip: usage netip <a.b.c.d>\n");
-		return;
-	}
-
-	net_set_ip(ip);
-	io_putstr("ip set to ");
-	net_print_ip();
-	io_putchar('\n');
-}
-
-/* ---- pong: answer ARP/ICMP for our IP (responder mode) ---- */
-
-void program_pong_main(void)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned int deadline, count = 0;
-
-	if (!nd) {
-		io_putstr("pong: no NIC found\n");
-		return;
-	}
-
-	io_putstr("pong: answering ARP/ICMP for 30s (ip=");
-	net_print_ip();
-	io_putstr(")...\n");
-	netdev_acquire();
-
-	deadline = get_ticks_ms() + 30000;
-	while (get_ticks_ms() < deadline) {
-		int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
-		int rlen;
-
-		if (len < 0)
-			continue;
-
-		rlen = net_build_reply(net_rx_buf, (unsigned int)len,
-				       net_reply_buf, NET_FRAME_MAX);
-		if (rlen > 0) {
-			if (nd->tx(net_reply_buf, (unsigned int)rlen) == 0) {
-				io_putstr("reply: ");
-				net_decode_frame(net_reply_buf, (unsigned int)rlen);
-				io_putchar('\n');
-				count++;
-			}
-		}
-	}
-
-	io_putstr("pong: done (");
-	put_dec(count);
-	io_putstr(" replies)\n");
-	netdev_release();
-}
-
-/* ---- netlog: send a text line to the LAN as a UDP datagram ---- */
-
-void program_netlog_main(const char *arg)
-{
-	static const unsigned char bcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
-	unsigned char dst[4];
-	unsigned int len = 0;
-
-	if (arg[0] == '\0') {
-		io_putstr("netlog: usage netlog <text>\n");
-		return;
-	}
-	while (arg[len] && len < NET_UDP_MAX)
-		len++;
-
-	netlog_get_host(dst);
-	if (net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT,
-			 arg, len) != 0) {
-		io_putstr("netlog: send failed\n");
-		return;
-	}
-	io_putstr("netlog: sent ");
-	put_dec(len);
-	io_putstr(" bytes to UDP port ");
-	put_dec(NETLOG_PORT);
-	io_putchar('\n');
-}
-
-/* ---- netconsole: the debug-output mirror (see netlog.c) ---- */
-
-static int arg_is(const char *arg, const char *word)
-{
-	unsigned int i = 0;
-
-	while (arg[i] && word[i] && arg[i] == word[i])
-		i++;
-	return arg[i] == '\0' && word[i] == '\0';
-}
-
-void program_netconsole_main(const char *arg)
-{
-	unsigned char ip[4];
-
-	if (arg[0] == '\0') {
-		io_putstr("netconsole: mirror ");
-		io_putstr(netlog_get_enabled() ? "on" : "off");
-		io_putstr(", host ");
-		netlog_get_host(ip);
-		put_ip(ip);
-		io_putstr(", udp port ");
-		put_dec(NETLOG_PORT);
-		io_putchar('\n');
-		io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
-		return;
-	}
-
-	if (arg_is(arg, "on")) {
-		netlog_set_enabled(1);
-		io_putstr("netconsole: mirror on\n");
-		return;
-	}
-	if (arg_is(arg, "off")) {
-		netlog_set_enabled(0);
-		io_putstr("netconsole: mirror off\n");
-		return;
-	}
-	if (arg[0] == 'h' && arg[1] == 'o' && arg[2] == 's' && arg[3] == 't') {
-		const char *p = arg + 4;
-
-		while (*p == ' ')
-			p++;
-		if (*p == '\0' || net_parse_ip(p, ip) != 0) {
-			io_putstr("netconsole: usage netconsole host <a.b.c.d>\n");
-			return;
-		}
-		netlog_set_host(ip);
-		io_putstr("netconsole: host ");
-		put_ip(ip);
-		io_putchar('\n');
-		return;
-	}
-
-	io_putstr("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
-}
-
-/* ---- netreset: ask the driver for a full NIC re-init ---- */
-
-void program_netreset_main(void)
-{
-	const struct netdev *nd = netdev_active();
-
-	if (!nd) {
-		io_putstr("netreset: no NIC found\n");
-		return;
-	}
-	if (!nd->reset) {
-		io_putstr("netreset: not supported by ");
-		io_putstr(nd->name);
-		io_putchar('\n');
-		return;
-	}
-	io_putstr("netreset: re-initializing ");
-	io_putstr(nd->name);
-	io_putstr("...\n");
-	nd->reset();
-	nd->print_info();
-}
-
-/* ---- netloop: PHY loopback self-test (no cable/peer needed) ---- */
-
-static void netloop_run(void)
-{
-	const struct netdev *nd = netdev_active();
-	unsigned char frame[64], mac[6];
-	unsigned int i, deadline;
-	int got = 0;
-
-	if (!nd) {
-		io_putstr("netloop: no NIC found\n");
-		return;
-	}
-	if (!nd->set_loopback) {
-		io_putstr("netloop: not supported by ");
-		io_putstr(nd->name);
-		io_putchar('\n');
-		return;
-	}
-
-	nd->get_mac(mac);
-	for (i = 0; i < 6; i++)
-		frame[i] = 0xFF;                      /* broadcast */
-	for (i = 0; i < 6; i++)
-		frame[6 + i] = mac[i];
-	frame[12] = 0x88;
-	frame[13] = 0xB6;                             /* local experimental */
-	frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
-	frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
-	frame[20] = 'O'; frame[21] = 'O';
-	for (i = 22; i < 64; i++)
-		frame[i] = 0x00;
-
-	io_putstr("netloop: PHY loopback on, sending...\n");
-	nd->set_loopback(1);
-
-	if (nd->tx(frame, sizeof(frame)) != 0) {
-		io_putstr("netloop: transmit failed\n");
-		nd->set_loopback(0);
-		return;
-	}
-
-	deadline = get_ticks_ms() + 1000;
-	while (get_ticks_ms() < deadline) {
-		int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
-
-		if (len < 0)
-			continue;
-		if (len >= 22 &&
-		    net_rx_buf[12] == 0x88 && net_rx_buf[13] == 0xB6 &&
-		    net_rx_buf[14] == 'N' && net_rx_buf[15] == 'E' &&
-		    net_rx_buf[16] == 'B' && net_rx_buf[17] == 'O' &&
-		    net_rx_buf[18] == 'S') {
-			got = 1;
-			break;
-		}
-	}
-
-	nd->set_loopback(0);
-
-	io_putstr(got ? "netloop: PASS (frame came back)\n"
-		      : "netloop: FAIL (no frame came back)\n");
-}
-
-void program_netloop_main(void)
-{
-	netdev_acquire();
-	netloop_run();
-	netdev_release();
-}
-
-/* ---- ping: active ARP + ICMP echo ---- */
-
-#define PING_COUNT  4
-#define PING_ID     0x4E42   /* "NB" */
-#define PING_WAIT_MS 1000
-
-static unsigned int ping_sent_ms[PING_COUNT];
-
-/* ARP request "who has target? tell my_ip" -> 42-byte frame. */
-static unsigned int build_arp_request(const unsigned char *my_mac,
-				      const unsigned char *my_ip,
-				      const unsigned char *target_ip,
-				      unsigned char *out)
-{
-	unsigned char *a = out + 14;
-	unsigned int i;
-
-	for (i = 0; i < 6; i++)
-		out[i] = 0xFF;                        /* broadcast */
-	for (i = 0; i < 6; i++)
-		out[6 + i] = my_mac[i];
-	out[12] = 0x08;
-	out[13] = 0x06;
-	a[0] = 0; a[1] = 1;                           /* htype Ethernet */
-	a[2] = 0x08; a[3] = 0;                        /* ptype IPv4 */
-	a[4] = 6; a[5] = 4;
-	a[6] = 0; a[7] = 1;                           /* opcode request */
-	for (i = 0; i < 6; i++)                       /* sha */
-		a[8 + i] = my_mac[i];
-	for (i = 0; i < 4; i++)                       /* spa */
-		a[14 + i] = my_ip[i];
-	for (i = 0; i < 6; i++)                       /* tha */
-		a[18 + i] = 0;
-	for (i = 0; i < 4; i++)                       /* tpa */
-		a[24 + i] = target_ip[i];
-	return 42;
-}
-
-/* 1 + peer MAC if frame is an ARP reply from target_ip. */
-static int parse_arp_reply(const unsigned char *f, unsigned int len,
-			   const unsigned char *target_ip, unsigned char *mac)
-{
-	const unsigned char *a = f + 14;
-	unsigned int i;
-
-	if (len < 14 + 28)
-		return 0;
-	if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
-		return 0;
-	if ((((unsigned int)a[6] << 8) | a[7]) != 2)   /* opcode reply */
-		return 0;
-	if (a[4] != 6 || a[5] != 4)
-		return 0;
-	if (!ip_eq(a + 14, target_ip))                 /* sender IP == target */
-		return 0;
-	for (i = 0; i < 6; i++)
-		mac[i] = a[8 + i];
-	return 1;
-}
-
-/* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
-static unsigned int build_icmp_echo(const unsigned char *src_mac,
-				    const unsigned char *dst_mac,
-				    const unsigned char *src_ip,
-				    const unsigned char *dst_ip,
-				    unsigned short seq, unsigned char *out)
-{
-	unsigned char *ip = out + 14;
-	unsigned char *ic = ip + 20;
-	unsigned char *pl = ic + 8;
-	unsigned short cs;
-	unsigned int i;
-
-	for (i = 0; i < 6; i++)
-		out[i] = dst_mac[i];
-	for (i = 0; i < 6; i++)
-		out[6 + i] = src_mac[i];
-	out[12] = 0x08;
-	out[13] = 0x00;
-
-	for (i = 0; i < 20; i++)
-		ip[i] = 0;
-	ip[0] = 0x45;
-	ip[2] = 0; ip[3] = 36;                        /* total length */
-	ip[8] = 64;                                   /* TTL */
-	ip[9] = IPPROTO_ICMP;
-	for (i = 0; i < 4; i++)
-		ip[12 + i] = src_ip[i];
-	for (i = 0; i < 4; i++)
-		ip[16 + i] = dst_ip[i];
-	cs = net_checksum(ip, 20);
-	ip[10] = (unsigned char)(cs >> 8);
-	ip[11] = (unsigned char)(cs & 0xFF);
-
-	ic[0] = ICMP_ECHO_REQUEST;
-	ic[1] = 0;
-	ic[2] = 0; ic[3] = 0;
-	ic[4] = (unsigned char)(PING_ID >> 8);
-	ic[5] = (unsigned char)(PING_ID & 0xFF);
-	ic[6] = (unsigned char)(seq >> 8);
-	ic[7] = (unsigned char)(seq & 0xFF);
-	pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
-	pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
-	cs = net_checksum(ic, 16);
-	ic[2] = (unsigned char)(cs >> 8);
-	ic[3] = (unsigned char)(cs & 0xFF);
-
-	return 14 + 20 + 16;
-}
-
-/* 1 + ICMP echo-reply seq for our IP from peer_ip. */
-static int parse_icmp_reply(const unsigned char *f, unsigned int len,
-			    const unsigned char *my_ip,
-			    const unsigned char *peer_ip,
-			    unsigned short *seq)
-{
-	const unsigned char *ip = f + 14;
-	const unsigned char *ic;
-	unsigned int ihl;
-	unsigned short ip_len, id;
-
-	if (len < 14 + 20)
-		return 0;
-	if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
-		return 0;
-	if ((ip[0] >> 4) != 4)
-		return 0;
-	ihl = (unsigned int)(ip[0] & 0x0F) * 4;
-	if (ihl < 20 || ip[9] != IPPROTO_ICMP)
-		return 0;
-	ip_len = ((unsigned short)ip[2] << 8) | ip[3];
-	if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
-		return 0;
-	if (!ip_eq(ip + 12, peer_ip))                 /* src == peer */
-		return 0;
-	if (!ip_eq(ip + 16, my_ip))                   /* dst == us */
-		return 0;
-	ic = ip + ihl;
-	if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
-		return 0;
-	id = ((unsigned short)ic[4] << 8) | ic[5];
-	if (id != PING_ID)
-		return 0;
-	*seq = ((unsigned short)ic[6] << 8) | ic[7];
-	if (*seq < 1 || *seq > PING_COUNT)
-		return 0;
-	return 1;
-}
-
-/* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
- * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
- * Prints the frames seen while waiting (capped) so a silent peer, a filtered
- * reply and a reply we refuse to parse can be told apart on screen. */
-static int ping_arp(const struct netdev *nd, const unsigned char *my_mac,
-		       const unsigned char *my_ip, const unsigned char *target_ip,
-		       unsigned char *target_mac)
-{
-	unsigned char frame[64];
-	unsigned int tries, seen = 0;
-
-	for (tries = 0; tries < 3; tries++) {
-		unsigned int deadline = get_ticks_ms() + PING_WAIT_MS;
-		unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
-
-		if (nd->tx(frame, flen) != 0)
-			return -2;
-		io_putstr("ping: ARP request sent (try ");
-		put_dec(tries + 1);
-		io_putstr("/3)\n");
-		while (get_ticks_ms() < deadline) {
-			int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
-
-			if (len < 0)
-				continue;
-			if (seen < 8) {
-				io_putstr("ping: rx ");
-				net_decode_frame(net_rx_buf, (unsigned int)len);
-				io_putchar('\n');
-				seen++;
-			}
-			if (parse_arp_reply(net_rx_buf, (unsigned int)len,
-					    target_ip, target_mac))
-				return 0;
-		}
-	}
-	io_putstr("ping: saw ");
-	put_dec(seen);
-	io_putstr(" frame(s) while waiting\n");
-	return -1;
-}
-
-static void ping_run(const char *arg)
-{
-	const struct netdev *nd;
-	unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
-	unsigned int i, replies = 0;
-
-	if (arg[0] == '\0') {
-		io_putstr("ping: usage ping <host|a.b.c.d>\n");
-		return;
-	}
-
-	nd = netdev_active();
-	if (!nd) {
-		io_putstr("ping: no NIC found\n");
-		return;
-	}
-
-	/* Dotted IP, or a name to resolve first. */
-	if (net_parse_ip(arg, target) != 0) {
-		if (dns_resolve(arg, target) != 0)
-			return;
-	}
-
-	nd->get_mac(my_mac);
-	net_get_ip(my_ip);
-
-	/* With a mask and gateway known, off-subnet targets go via the router;
-	 * we can only ARP for the next hop. */
-	{
-		unsigned char mask[4], gw[4], hop[4];
-		unsigned int j, off = 0;
-
-		net_get_mask(mask);
-		net_get_gateway(gw);
-		for (j = 0; j < 4; j++)
-			hop[j] = target[j];
-		if ((mask[0] || mask[1] || mask[2] || mask[3]) &&
-		    (gw[0] || gw[1] || gw[2] || gw[3])) {
-			for (j = 0; j < 4; j++) {
-				if ((target[j] & mask[j]) != (my_ip[j] & mask[j]))
-					off = 1;
-			}
-		}
-		if (off) {
-			for (j = 0; j < 4; j++)
-				hop[j] = gw[j];
-			io_putstr("ping: ");
-			put_ip(target);
-			io_putstr(" via ");
-			put_ip(hop);
-			io_putchar('\n');
-		}
-
-		{
-			int r = ping_arp(nd, my_mac, my_ip, hop, target_mac);
-
-			if (r == -2) {
-				io_putstr("ping: transmit failed (see netregs)\n");
-				return;
-			}
-			if (r != 0) {
-				io_putstr("ping: no ARP reply from ");
-				put_ip(hop);
-				io_putchar('\n');
-				return;
-			}
-		}
-	}
-	io_putstr("ping: ");
-	put_ip(target);
-	io_putstr(" is-at ");
-	put_mac(target_mac);
-	io_putchar('\n');
-
-	for (i = 0; i < PING_COUNT; i++) {
-		unsigned char frame[64];
-		unsigned int flen, deadline;
-		int got = 0;
-
-		flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
-				       (unsigned short)(i + 1), frame);
-		ping_sent_ms[i] = get_ticks_ms();
-		if (nd->tx(frame, flen) != 0) {
-			io_putstr("ping: transmit failed\n");
-			return;
-		}
-
-		deadline = get_ticks_ms() + PING_WAIT_MS;
-		while (get_ticks_ms() < deadline && !got) {
-			unsigned short seq;
-			int len = nd->rx(net_rx_buf, NET_FRAME_MAX);
-			unsigned int rtt;
-
-			if (len < 0)
-				continue;
-			if (!parse_icmp_reply(net_rx_buf, (unsigned int)len,
-					      my_ip, target, &seq))
-				continue;
-			rtt = get_ticks_ms() - ping_sent_ms[seq - 1];
-			io_putstr("reply: seq=");
-			put_dec(seq);
-			io_putstr(" rtt=");
-			if (rtt == 0)
-				io_putstr("<1");
-			else
-				put_dec(rtt);
-			io_putstr("ms\n");
-			got = 1;
-			replies++;
-		}
-		if (!got) {
-			io_putstr("ping: timeout, seq=");
-			put_dec(i + 1);
-			io_putchar('\n');
-		}
-	}
-
-	io_putstr("ping: ");
-	put_dec(replies);
-	io_putstr("/");
-	put_dec(PING_COUNT);
-	io_putstr(" replies from ");
-	put_ip(target);
-	io_putchar('\n');
-}
-
-void program_ping_main(const char *arg)
-{
-	netdev_acquire();
-	ping_run(arg);
-	netdev_release();
-}

+ 20 - 2
netdev.c

@@ -2,6 +2,7 @@
 
 #include "netdev.h"
 #include "ne2000.h"
+#include "process.h"
 #include "r8168.h"
 static const struct netdev *const netdev_drivers[] = {
 	/* PCIe Gigabit first: on the AT3IONT-I this is the real NIC, while
@@ -13,6 +14,7 @@ static const struct netdev *const netdev_drivers[] = {
 static const struct netdev *active;
 static int probed;
 static unsigned int busy;
+static unsigned int owner_pid;   /* process that took the NIC (busy > 0) */
 
 /* Shared receive scratch: see netdev.h.  One buffer, reused by every protocol
  * that polls frames (they never run at the same time: the NIC is owned by one
@@ -26,13 +28,29 @@ unsigned char *netdev_rx_scratch(void)
 
 void netdev_acquire(void)
 {
+	if (busy == 0 && procs)
+		owner_pid = procs[current_pid].pid;
 	busy++;
 }
 
 void netdev_release(void)
 {
-	if (busy)
-		busy--;
+	if (busy && --busy == 0)
+		owner_pid = 0;
+}
+
+/* The process currently owning the NIC (0 when free), and a hard reset for
+ * the reaper: a process killed while inside a net operation must not leave
+ * the receiver owned forever (it would pause the kexec watcher). */
+unsigned int netdev_owner(void)
+{
+	return owner_pid;
+}
+
+void netdev_release_all(void)
+{
+	busy = 0;
+	owner_pid = 0;
 }
 
 int netdev_in_use(void)

+ 5 - 0
netdev.h

@@ -30,6 +30,11 @@ void netdev_acquire(void);
 void netdev_release(void);
 int  netdev_in_use(void);
 
+/* Process that currently owns the NIC (0 = free), and a hard reset used by
+ * the reaper so a killed process cannot leave the receiver owned. */
+unsigned int netdev_owner(void);
+void netdev_release_all(void);
+
 /* Fill the active NIC's MAC; -1 (and a zero MAC) when there is no NIC. */
 int netdev_get_mac(unsigned char mac[6]);
 

+ 2 - 0
process.c

@@ -9,6 +9,7 @@
 #include "paging.h"
 #include "serial.h"
 #include "sys_fs.h"
+#include "sys_net.h"
 #include "timer.h"
 
 struct process *procs;               /* dynamically-sized process table */
@@ -436,6 +437,7 @@ void schedule(void)
 	 * so it is reaped on a later schedule() from another process's context. */
 	for (i = 0; i < proc_cap; i++) {
 		if (procs[i].state == PROC_DEAD && i != current_pid) {
+			sys_net_reap(procs[i].pid);   /* release a held NIC */
 			if (procs[i].stack_base)
 				mm_free_page(procs[i].stack_base);
 			if (procs[i].cr3 && procs[i].cr3 != paging_current_cr3())

+ 206 - 0
sys_net.c

@@ -0,0 +1,206 @@
+/* Network service for ring-3 commands (see syscall.h for the ABI).
+ *
+ * The stack (net.c/dns.c/tcp.c) and the NIC drivers stay in the kernel; user
+ * commands get the configuration, the DNS/DHCP clients and the raw frame
+ * paths, so the packet-level commands (ping, pong, netrecv, ...) port over
+ * their existing frame code.  The receiver is owned by one process at a time
+ * (netdev_acquire/release): the background kexec watcher pauses while a
+ * command owns it, and the reaper force-releases it if that process dies
+ * (sys_net_reap). */
+
+#include "syscall.h"
+#include "dhcp.h"
+#include "dns.h"
+#include "io.h"
+#include "net.h"
+#include "netdev.h"
+#include "netlog.h"
+#include "process.h"
+
+static unsigned int caller_pid(void)
+{
+	return procs[current_pid].pid;
+}
+
+/* Called from the scheduler's reap loop for every dead process. */
+void sys_net_reap(unsigned int pid)
+{
+	if (netdev_owner() == pid)
+		netdev_release_all();
+}
+
+int sys_net_info(struct sys_netinfo *out)
+{
+	const struct netdev *nd = netdev_active();
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*out); i++)
+		((unsigned char *)out)[i] = 0;
+	out->mirror_on = netlog_get_enabled() ? 1 : 0;
+	netlog_get_host(out->mirror_host);
+	if (!nd)
+		return 0;
+
+	out->present = 1;
+	if (nd->get_mac)
+		nd->get_mac(out->mac);
+	net_get_ip(out->ip);
+	net_get_mask(out->mask);
+	net_get_gateway(out->gw);
+	net_get_dns(out->dns);
+	for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
+		out->name[i] = nd->name[i];
+	for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
+		out->chip[i] = nd->chip[i];
+	return 1;
+}
+
+int sys_net_print_info(void)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->print_info)
+		return -1;
+	nd->print_info();
+	return 0;
+}
+
+/* The owner must be the caller: the syscall runs in its process context. */
+static int owns_nic(void)
+{
+	return netdev_owner() == caller_pid();
+}
+
+int sys_net_acquire(void)
+{
+	if (owns_nic())
+		return 0;
+	if (netdev_in_use())
+		return -1;
+	netdev_acquire();
+	return 0;
+}
+
+int sys_net_release(void)
+{
+	if (owns_nic())
+		netdev_release();
+	return 0;
+}
+
+int sys_net_mac(unsigned char mac[6])
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->get_mac)
+		return -1;
+	nd->get_mac(mac);
+	return 0;
+}
+
+void sys_net_get_ip(unsigned char ip[4])       { net_get_ip(ip); }
+void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
+void sys_net_get_mask(unsigned char m[4])      { net_get_mask(m); }
+void sys_net_get_gw(unsigned char gw[4])       { net_get_gateway(gw); }
+void sys_net_get_dns(unsigned char d[4])       { net_get_dns(d); }
+void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
+
+int sys_net_tx(const unsigned char *buf, unsigned int len)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !owns_nic())
+		return -1;
+	return nd->tx(buf, len);
+}
+
+int sys_net_rx(unsigned char *buf, unsigned int max)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !owns_nic())
+		return -1;
+	return nd->rx(buf, max);
+}
+
+void sys_net_decode(const unsigned char *buf, unsigned int len)
+{
+	net_decode_frame(buf, len);
+}
+
+/* The reply buffer is always a full frame (the callers carry one). */
+int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
+{
+	if (!owns_nic())
+		return -1;
+	return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
+}
+
+int sys_net_loopback(int on)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->set_loopback)
+		return -1;
+	return nd->set_loopback(on);
+}
+
+int sys_net_regs(void)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->dump_regs)
+		return -1;
+	nd->dump_regs();
+	return 0;
+}
+
+int sys_net_reset(void)
+{
+	const struct netdev *nd = netdev_active();
+
+	if (!nd || !nd->reset)
+		return -1;
+	nd->reset();
+	return 0;
+}
+
+int sys_dhcp_run(unsigned int timeout_ms)
+{
+	return dhcp_run(timeout_ms);
+}
+
+int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
+{
+	return dhcp_last_info(server, lease);
+}
+
+int sys_dns_resolve(const char *name, unsigned char ip[4])
+{
+	if (!owns_nic())
+		return -1;
+	return dns_resolve(name, ip);
+}
+
+int sys_netlog(const char *text)
+{
+	static const unsigned char bcast_mac[6] = {
+		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+	};
+	unsigned char dst[4];
+	unsigned int len = 0;
+
+	while (text[len] && len < NET_UDP_MAX)
+		len++;
+	netlog_get_host(dst);
+	return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
+}
+
+int sys_netlog_conf(int enabled, const unsigned char *ip)
+{
+	if (enabled >= 0)
+		netlog_set_enabled(enabled);
+	if (ip)
+		netlog_set_host(ip);
+	return 0;
+}

+ 35 - 0
sys_net.h

@@ -0,0 +1,35 @@
+/* Network services for ring-3 commands (see sys_net.c). */
+#ifndef SYS_NET_H
+#define SYS_NET_H
+
+#include "syscall.h"
+
+/* Release the receiver if `pid` owned it (called by the reaper). */
+void sys_net_reap(unsigned int pid);
+
+int sys_net_info(struct sys_netinfo *out);
+int sys_net_print_info(void);
+int sys_net_acquire(void);
+int sys_net_release(void);
+int sys_net_mac(unsigned char mac[6]);
+void sys_net_get_ip(unsigned char ip[4]);
+void sys_net_set_ip(const unsigned char ip[4]);
+void sys_net_get_mask(unsigned char mask[4]);
+void sys_net_get_gw(unsigned char gw[4]);
+void sys_net_get_dns(unsigned char dns[4]);
+void sys_net_set_dns(const unsigned char dns[4]);
+int sys_net_tx(const unsigned char *buf, unsigned int len);
+int sys_net_rx(unsigned char *buf, unsigned int max);
+void sys_net_decode(const unsigned char *buf, unsigned int len);
+int sys_net_reply(const unsigned char *in, unsigned int len,
+		  unsigned char *out);
+int sys_net_loopback(int on);
+int sys_net_regs(void);
+int sys_net_reset(void);
+int sys_dhcp_run(unsigned int timeout_ms);
+int sys_dhcp_info(unsigned char server[4], unsigned int *lease);
+int sys_dns_resolve(const char *name, unsigned char ip[4]);
+int sys_netlog(const char *text);
+int sys_netlog_conf(int enabled, const unsigned char *ip);
+
+#endif

+ 55 - 0
syscall.c

@@ -6,6 +6,7 @@
 #include "serial.h"
 #include "sys_fs.h"
 #include "sys_info.h"
+#include "sys_net.h"
 #include "timer.h"
 
 int syscall_dispatch(int num, int a1, int a2, int a3)
@@ -48,6 +49,60 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 		return sys_storage_info((struct sys_storage_info *)a1);
 	case SYS_BLKDEV:
 		return sys_blkdev((unsigned int)a1, (struct sys_blkdev *)a2);
+	case SYS_NET_INFO:
+		return sys_net_info((struct sys_netinfo *)a1);
+	case SYS_NET_PRINT_INFO:
+		return sys_net_print_info();
+	case SYS_NET_ACQUIRE:
+		return sys_net_acquire();
+	case SYS_NET_RELEASE:
+		return sys_net_release();
+	case SYS_NET_MAC:
+		return sys_net_mac((unsigned char *)a1);
+	case SYS_NET_GET_IP:
+		sys_net_get_ip((unsigned char *)a1);
+		return 0;
+	case SYS_NET_SET_IP:
+		sys_net_set_ip((const unsigned char *)a1);
+		return 0;
+	case SYS_NET_GET_MASK:
+		sys_net_get_mask((unsigned char *)a1);
+		return 0;
+	case SYS_NET_GET_GW:
+		sys_net_get_gw((unsigned char *)a1);
+		return 0;
+	case SYS_NET_GET_DNS:
+		sys_net_get_dns((unsigned char *)a1);
+		return 0;
+	case SYS_NET_SET_DNS:
+		sys_net_set_dns((const unsigned char *)a1);
+		return 0;
+	case SYS_NET_TX:
+		return sys_net_tx((const unsigned char *)a1, (unsigned int)a2);
+	case SYS_NET_RX:
+		return sys_net_rx((unsigned char *)a1, (unsigned int)a2);
+	case SYS_NET_DECODE:
+		sys_net_decode((const unsigned char *)a1, (unsigned int)a2);
+		return 0;
+	case SYS_NET_REPLY:
+		return sys_net_reply((const unsigned char *)a1, (unsigned int)a2,
+				     (unsigned char *)a3);
+	case SYS_NET_LOOPBACK:
+		return sys_net_loopback(a1);
+	case SYS_NET_REGS:
+		return sys_net_regs();
+	case SYS_NET_RESET:
+		return sys_net_reset();
+	case SYS_DHCP_RUN:
+		return sys_dhcp_run((unsigned int)a1);
+	case SYS_DHCP_INFO:
+		return sys_dhcp_info((unsigned char *)a1, (unsigned int *)a2);
+	case SYS_DNS_RESOLVE:
+		return sys_dns_resolve((const char *)a1, (unsigned char *)a2);
+	case SYS_NETLOG:
+		return sys_netlog((const char *)a1);
+	case SYS_NETLOG_CONF:
+		return sys_netlog_conf(a1, (const unsigned char *)a2);
 	default:
 		return -1;
 	}

+ 40 - 0
syscall.h

@@ -30,6 +30,46 @@
 				refreshes the mount cache like `storage` did */
 #define SYS_BLKDEV       16  /* (unsigned int index, struct sys_blkdev *out) */
 
+/* Network service (see sys_net.c): the stack and NIC drivers stay in the
+ * kernel; user commands get the configuration, the DNS/DHCP clients and the
+ * raw frame paths (so the packet-level commands port unchanged). */
+#define SYS_NET_INFO      17  /* (struct sys_netinfo *out) -> 1 = NIC present */
+#define SYS_NET_PRINT_INFO 18 /* the driver's status lines */
+#define SYS_NET_ACQUIRE   19  /* take the receiver for this process (0/-1) */
+#define SYS_NET_RELEASE   20
+#define SYS_NET_MAC       21  /* (unsigned char mac[6]) -> 0/-1 */
+#define SYS_NET_GET_IP    22  /* (unsigned char ip[4]) */
+#define SYS_NET_SET_IP    23  /* (const unsigned char ip[4]) */
+#define SYS_NET_GET_MASK  24
+#define SYS_NET_GET_GW    25
+#define SYS_NET_GET_DNS   26
+#define SYS_NET_SET_DNS   27
+#define SYS_NET_TX        28  /* (const void *buf, unsigned len) -> 0/-1 */
+#define SYS_NET_RX        29  /* (void *buf, unsigned max) -> len/-1 */
+#define SYS_NET_DECODE    30  /* (const void *buf, unsigned len): one line */
+#define SYS_NET_REPLY     31  /* (in, len, out, max) -> reply length / 0 */
+#define SYS_NET_LOOPBACK  32  /* (int on) -> 0/-1 */
+#define SYS_NET_REGS      33  /* the driver's register dump */
+#define SYS_NET_RESET     34  /* driver re-init */
+#define SYS_DHCP_RUN      35  /* (timeout_ms) -> dhcp_run() result */
+#define SYS_DHCP_INFO     36  /* (unsigned char ip[4], unsigned *lease) -> 1 */
+#define SYS_DNS_RESOLVE   37  /* (name, unsigned char ip[4]) -> 0; caller owns */
+#define SYS_NETLOG        38  /* (const char *text) -> 0/-1 */
+#define SYS_NETLOG_CONF   39  /* (int enabled_or_-1, const ip[4] or NULL) */
+
+struct sys_netinfo {
+	unsigned int present;
+	unsigned char mac[6];
+	unsigned char ip[4];
+	unsigned char mask[4];
+	unsigned char gw[4];
+	unsigned char dns[4];
+	unsigned int mirror_on;
+	unsigned char mirror_host[4];
+	char name[8];
+	char chip[16];
+};
+
 /* Error codes returned by the file syscalls. */
 #define SYS_EIO     (-1)     /* disk I/O error */
 #define SYS_ENODEV  (-2)     /* no such device / unusable path prefix */

+ 57 - 28
terminal.c

@@ -26,21 +26,7 @@ extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
 extern void program_poke_main(const char *arg);
-extern void program_netinfo_main(void);
-extern void program_netrecv_main(void);
-extern void program_netsend_main(void);
-extern void program_netip_main(const char *arg);
-extern void program_netlog_main(const char *arg);
-extern void program_netconsole_main(const char *arg);
-extern void program_dhcp_main(void);
-extern void program_netregs_main(void);
-extern void program_netloop_main(void);
-extern void program_netreset_main(void);
-extern void program_pong_main(void);
-extern void program_ping_main(const char *arg);
 extern void program_curl_main(const char *arg);
-extern void program_netdns_main(const char *arg);
-extern void program_host_main(const char *arg);
 
 /* Print the prompt (with the current directory) and set line_start. */
 static void term_print_prompt(struct term *t, int first)
@@ -136,18 +122,18 @@ static const struct kcmd cmds[] = {
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
 	{ "peek",       "peek <addr> [len]",             CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
 	{ "poke",       "poke <addr> <val>",             CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },
-	{ "netinfo",    "netinfo",                       CK_SPAWN, 0, program_netinfo_main, 0, 0, 0 },
-	{ "netrecv",    "netrecv",                       CK_SPAWN, 0, program_netrecv_main, 0, 0, 0 },
-	{ "netsend",    "netsend",                       CK_SPAWN, 0, program_netsend_main, 0, 0, 0 },
-	{ "netip",      "netip [a.b.c.d]",               CK_SPAWN_ARG, 0, 0, program_netip_main, 0, 0 },
-	{ "ping",       "ping <host|a.b.c.d>",           CK_SPAWN_ARG, 0, 0, program_ping_main, 0, 0 },
-	{ "netlog",     "netlog <text>",                 CK_SPAWN_ARG, 0, 0, program_netlog_main, 0, 0 },
-	{ "netconsole", "netconsole [on|off|host <ip>]", CK_SPAWN_ARG, 0, 0, program_netconsole_main, 0, 0 },
-	{ "dhcp",       "dhcp",                          CK_SPAWN, 0, program_dhcp_main, 0, 0, 0 },
-	{ "netregs",    "netregs",                       CK_SPAWN, 0, program_netregs_main, 0, 0, 0 },
-	{ "netloop",    "netloop",                       CK_SPAWN, 0, program_netloop_main, 0, 0, 0 },
-	{ "netreset",   "netreset",                      CK_SPAWN, 0, program_netreset_main, 0, 0, 0 },
-	{ "pong",       "pong",                          CK_SPAWN, 0, program_pong_main, 0, 0, 0 },
+	{ "netinfo",    "netinfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netrecv",    "netrecv",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netsend",    "netsend",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netip",      "netip [a.b.c.d]",               CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
+	{ "ping",       "ping <host|a.b.c.d>",           CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
+	{ "netlog",     "netlog <text>",                 CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
+	{ "netconsole", "netconsole [on|off|host <ip>]", CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
+	{ "dhcp",       "dhcp",                          CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netregs",    "netregs",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netloop",    "netloop",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "netreset",   "netreset",                      CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "pong",       "pong",                          CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "ps",         "ps",                            CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
 	{ "kill",       "kill <pid>",                    CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
 	{ "kexec",      "kexec",                         CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
@@ -155,8 +141,8 @@ static const struct kcmd cmds[] = {
 	{ "serial",     "serial test|recv",              CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
 	{ "clear",      "clear",                         CK_CUSTOM, 0, 0, 0, 0, cmd_clear },
 	{ "curl",       "curl <url>",                    CK_SPAWN_ARG, 0, 0, program_curl_main, 0, 0 },
-	{ "netdns",     "netdns [a.b.c.d]",              CK_SPAWN_ARG, 0, 0, program_netdns_main, 0, 0 },
-	{ "host",       "host <name>",                   CK_SPAWN_ARG, 0, 0, program_host_main, 0, 0 },
+	{ "netdns",     "netdns [a.b.c.d]",              CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
+	{ "host",       "host <name>",                   CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
 	{ "help",       "help",                          CK_CUSTOM, 0, 0, 0, 0, cmd_help },
 };
 
@@ -185,6 +171,34 @@ extern unsigned char _binary_ucmd_mouseinfo_elf_start[];
 extern unsigned char _binary_ucmd_mouseinfo_elf_end[];
 extern unsigned char _binary_ucmd_storage_elf_start[];
 extern unsigned char _binary_ucmd_storage_elf_end[];
+extern unsigned char _binary_ucmd_netinfo_elf_start[];
+extern unsigned char _binary_ucmd_netinfo_elf_end[];
+extern unsigned char _binary_ucmd_netip_elf_start[];
+extern unsigned char _binary_ucmd_netip_elf_end[];
+extern unsigned char _binary_ucmd_netdns_elf_start[];
+extern unsigned char _binary_ucmd_netdns_elf_end[];
+extern unsigned char _binary_ucmd_host_elf_start[];
+extern unsigned char _binary_ucmd_host_elf_end[];
+extern unsigned char _binary_ucmd_dhcp_elf_start[];
+extern unsigned char _binary_ucmd_dhcp_elf_end[];
+extern unsigned char _binary_ucmd_netsend_elf_start[];
+extern unsigned char _binary_ucmd_netsend_elf_end[];
+extern unsigned char _binary_ucmd_netrecv_elf_start[];
+extern unsigned char _binary_ucmd_netrecv_elf_end[];
+extern unsigned char _binary_ucmd_ping_elf_start[];
+extern unsigned char _binary_ucmd_ping_elf_end[];
+extern unsigned char _binary_ucmd_pong_elf_start[];
+extern unsigned char _binary_ucmd_pong_elf_end[];
+extern unsigned char _binary_ucmd_netlog_elf_start[];
+extern unsigned char _binary_ucmd_netlog_elf_end[];
+extern unsigned char _binary_ucmd_netconsole_elf_start[];
+extern unsigned char _binary_ucmd_netconsole_elf_end[];
+extern unsigned char _binary_ucmd_netregs_elf_start[];
+extern unsigned char _binary_ucmd_netregs_elf_end[];
+extern unsigned char _binary_ucmd_netloop_elf_start[];
+extern unsigned char _binary_ucmd_netloop_elf_end[];
+extern unsigned char _binary_ucmd_netreset_elf_start[];
+extern unsigned char _binary_ucmd_netreset_elf_end[];
 
 struct ucmd_blob {
 	const char *name;
@@ -203,8 +217,23 @@ static const struct ucmd_blob ucmds[] = {
 	{ "meminfo", _binary_ucmd_meminfo_elf_start, _binary_ucmd_meminfo_elf_end },
 	{ "mouseinfo", _binary_ucmd_mouseinfo_elf_start, _binary_ucmd_mouseinfo_elf_end },
 	{ "storage", _binary_ucmd_storage_elf_start, _binary_ucmd_storage_elf_end },
+	{ "netinfo", _binary_ucmd_netinfo_elf_start, _binary_ucmd_netinfo_elf_end },
+	{ "netip",   _binary_ucmd_netip_elf_start,   _binary_ucmd_netip_elf_end },
+	{ "netdns",  _binary_ucmd_netdns_elf_start,  _binary_ucmd_netdns_elf_end },
+	{ "host",    _binary_ucmd_host_elf_start,    _binary_ucmd_host_elf_end },
+	{ "dhcp",    _binary_ucmd_dhcp_elf_start,    _binary_ucmd_dhcp_elf_end },
+	{ "netsend",      _binary_ucmd_netsend_elf_start,              _binary_ucmd_netsend_elf_end },
+	{ "netrecv",      _binary_ucmd_netrecv_elf_start,              _binary_ucmd_netrecv_elf_end },
+	{ "ping",         _binary_ucmd_ping_elf_start,              _binary_ucmd_ping_elf_end },
+	{ "pong",         _binary_ucmd_pong_elf_start,              _binary_ucmd_pong_elf_end },
+	{ "netlog",       _binary_ucmd_netlog_elf_start,              _binary_ucmd_netlog_elf_end },
+	{ "netconsole",   _binary_ucmd_netconsole_elf_start,              _binary_ucmd_netconsole_elf_end },
+	{ "netregs",      _binary_ucmd_netregs_elf_start,              _binary_ucmd_netregs_elf_end },
+	{ "netloop",      _binary_ucmd_netloop_elf_start,              _binary_ucmd_netloop_elf_end },
+	{ "netreset",     _binary_ucmd_netreset_elf_start,              _binary_ucmd_netreset_elf_end },
 };
 
+
 static const struct ucmd_blob *ucmd_lookup(const char *name)
 {
 	unsigned int i;

+ 16 - 0
ucmd_dhcp.c

@@ -0,0 +1,16 @@
+/* dhcp: ring-3 command.  Re-run the DHCP client; dhcp.c prints the exchange. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	int r;
+
+	(void)arg;
+	r = u_syscall1(SYS_DHCP_RUN, 4000);
+	if (r == -2)
+		u_puts("dhcp: no NIC\n");
+	/* r == 0: dhcp_run() printed the lease; r == -1: it printed its own
+	 * failure line. */
+	u_exit(r == 0 ? 0 : 1);
+}

+ 18 - 0
ucmd_host.c

@@ -0,0 +1,18 @@
+/* host: ring-3 command.  One DNS A query; the resolver prints its answers. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	unsigned char ip[4];
+
+	if (!arg || !arg[0]) {
+		u_puts("host: usage host <name>\n");
+		u_exit(1);
+	}
+
+	u_syscall0(SYS_NET_ACQUIRE);
+	u_syscall2(SYS_DNS_RESOLVE, (int)arg, (int)ip);
+	u_syscall0(SYS_NET_RELEASE);
+	u_exit(0);
+}

+ 59 - 0
ucmd_lib.h

@@ -88,6 +88,65 @@ static inline void u_puthex(unsigned int n)
 	u_puts(buf);
 }
 
+/* "a.b.c.d" (strict, 4 parts, 0-255) -> 0, else -1. */
+static inline int u_parse_ip(const char *s, unsigned char ip[4])
+{
+	unsigned int i, val = 0, nparts = 0;
+	int have_digit = 0;
+
+	for (i = 0; ; i++) {
+		char c = s[i];
+
+		if (c >= '0' && c <= '9') {
+			val = val * 10 + (unsigned int)(c - '0');
+			if (val > 255)
+				return -1;
+			have_digit = 1;
+		} else if (c == '.' || c == '\0') {
+			if (!have_digit || nparts >= 4)
+				return -1;
+			ip[nparts++] = (unsigned char)val;
+			val = 0;
+			have_digit = 0;
+			if (c == '\0')
+				break;
+		} else {
+			return -1;
+		}
+	}
+	return (nparts == 4) ? 0 : -1;
+}
+
+static inline void u_put_ip(const unsigned char ip[4])
+{
+	unsigned int i;
+
+	for (i = 0; i < 4; i++) {
+		if (i)
+			u_putc('.');
+		u_putdec(ip[i]);
+	}
+}
+
+static inline void u_put_hex2(unsigned int v)
+{
+	static const char hex[] = "0123456789ABCDEF";
+
+	u_putc(hex[(v >> 4) & 0xF]);
+	u_putc(hex[v & 0xF]);
+}
+
+static inline void u_put_mac(const unsigned char mac[6])
+{
+	unsigned int i;
+
+	for (i = 0; i < 6; i++) {
+		if (i)
+			u_putc(':');
+		u_put_hex2(mac[i]);
+	}
+}
+
 static inline unsigned int u_ticks_ms(void)
 {
 	return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);

+ 67 - 0
ucmd_netconsole.c

@@ -0,0 +1,67 @@
+/* netconsole: ring-3 command.  The debug-output mirror's configuration. */
+
+#include "ucmd_lib.h"
+
+#define NETLOG_PORT 19290
+
+static int arg_is(const char *arg, const char *word)
+{
+	unsigned int i = 0;
+
+	while (arg[i] && word[i] && arg[i] == word[i])
+		i++;
+	return arg[i] == '\0' && word[i] == '\0';
+}
+
+static void usage(void)
+{
+	u_puts("netconsole: usage netconsole on|off|host <a.b.c.d>\n");
+}
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+	unsigned char ip[4];
+
+	if (!arg || !arg[0]) {
+		u_syscall1(SYS_NET_INFO, (int)&n);
+		u_puts("netconsole: mirror ");
+		u_puts(n.mirror_on ? "on" : "off");
+		u_puts(", host ");
+		u_put_ip(n.mirror_host);
+		u_puts(", udp port ");
+		u_putdec(NETLOG_PORT);
+		u_putc('\n');
+		usage();
+		u_exit(1);
+	}
+
+	if (arg_is(arg, "on")) {
+		u_syscall2(SYS_NETLOG_CONF, 1, 0);
+		u_puts("netconsole: mirror on\n");
+		u_exit(0);
+	}
+	if (arg_is(arg, "off")) {
+		u_syscall2(SYS_NETLOG_CONF, 0, 0);
+		u_puts("netconsole: mirror off\n");
+		u_exit(0);
+	}
+	if (arg[0] == 'h' && arg[1] == 'o' && arg[2] == 's' && arg[3] == 't') {
+		const char *p = arg + 4;
+
+		while (*p == ' ')
+			p++;
+		if (*p == '\0' || u_parse_ip(p, ip) != 0) {
+			usage();
+			u_exit(1);
+		}
+		u_syscall2(SYS_NETLOG_CONF, -1, (int)ip);
+		u_puts("netconsole: host ");
+		u_put_ip(ip);
+		u_putc('\n');
+		u_exit(0);
+	}
+
+	usage();
+	u_exit(1);
+}

+ 38 - 0
ucmd_netdns.c

@@ -0,0 +1,38 @@
+/* netdns: ring-3 command.  Show or set the DNS resolver. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	unsigned char dns[4], gw[4];
+
+	if (!arg || !arg[0]) {
+		u_syscall1(SYS_NET_GET_DNS, (int)dns);
+		if (dns[0] || dns[1] || dns[2] || dns[3]) {
+			u_puts("dns=");
+			u_put_ip(dns);
+			u_putc('\n');
+			u_exit(0);
+		}
+		u_syscall1(SYS_NET_GET_GW, (int)gw);
+		u_puts("dns: unset");
+		if (gw[0] || gw[1] || gw[2] || gw[3]) {
+			u_puts(" (fallback: gateway ");
+			u_put_ip(gw);
+			u_putc(')');
+		}
+		u_putc('\n');
+		u_exit(0);
+	}
+
+	if (u_parse_ip(arg, dns) != 0) {
+		u_puts("netdns: usage netdns [a.b.c.d]\n");
+		u_exit(1);
+	}
+
+	u_syscall1(SYS_NET_SET_DNS, (int)dns);
+	u_puts("dns set to ");
+	u_put_ip(dns);
+	u_putc('\n');
+	u_exit(0);
+}

+ 49 - 0
ucmd_netinfo.c

@@ -0,0 +1,49 @@
+/* netinfo: ring-3 command.  NIC, addresses and DHCP lease. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+	unsigned char srv[4];
+	unsigned int lease = 0;
+
+	(void)arg;
+	if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
+		u_puts("netinfo: no NIC found\n");
+		u_exit(0);
+	}
+
+	u_puts("netdev: ");
+	u_puts(n.name);
+	u_puts(" (");
+	u_puts(n.chip);
+	u_puts(")\n");
+	u_syscall0(SYS_NET_PRINT_INFO);
+
+	u_puts("ip=");
+	u_put_ip(n.ip);
+	if (n.mask[0] || n.mask[1] || n.mask[2] || n.mask[3]) {
+		u_puts("/");
+		u_put_ip(n.mask);
+	}
+	u_putc('\n');
+	if (n.gw[0] || n.gw[1] || n.gw[2] || n.gw[3]) {
+		u_puts("gw=");
+		u_put_ip(n.gw);
+		u_putc('\n');
+	}
+	if (n.dns[0] || n.dns[1] || n.dns[2] || n.dns[3]) {
+		u_puts("dns=");
+		u_put_ip(n.dns);
+		u_putc('\n');
+	}
+	if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
+		u_puts("dhcp: server ");
+		u_put_ip(srv);
+		u_puts(" lease ");
+		u_putdec(lease);
+		u_puts("s\n");
+	}
+	u_exit(0);
+}

+ 27 - 0
ucmd_netip.c

@@ -0,0 +1,27 @@
+/* netip: ring-3 command.  Show or set the IPv4 address. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	unsigned char ip[4];
+
+	if (!arg || !arg[0]) {
+		u_syscall1(SYS_NET_GET_IP, (int)ip);
+		u_put_ip(ip);
+		u_putc('\n');
+		u_exit(0);
+	}
+
+	if (u_parse_ip(arg, ip) != 0) {
+		u_puts("netip: usage netip <a.b.c.d>\n");
+		u_exit(1);
+	}
+
+	u_syscall1(SYS_NET_SET_IP, (int)ip);
+	u_puts("ip set to ");
+	u_syscall1(SYS_NET_GET_IP, (int)ip);
+	u_put_ip(ip);
+	u_putc('\n');
+	u_exit(0);
+}

+ 28 - 0
ucmd_netlog.c

@@ -0,0 +1,28 @@
+/* netlog: ring-3 command.  Send one text line as a UDP datagram. */
+
+#include "ucmd_lib.h"
+
+#define NETLOG_PORT 19290
+
+void _start(const char *arg)
+{
+	unsigned int len = 0;
+
+	if (!arg || !arg[0]) {
+		u_puts("netlog: usage netlog <text>\n");
+		u_exit(1);
+	}
+	while (arg[len])
+		len++;
+
+	if (u_syscall1(SYS_NETLOG, (int)arg) != 0) {
+		u_puts("netlog: send failed\n");
+		u_exit(1);
+	}
+	u_puts("netlog: sent ");
+	u_putdec(len);
+	u_puts(" bytes to UDP port ");
+	u_putdec(NETLOG_PORT);
+	u_putc('\n');
+	u_exit(0);
+}

+ 67 - 0
ucmd_netloop.c

@@ -0,0 +1,67 @@
+/* netloop: ring-3 command.  PHY loopback self-test (no cable/peer needed). */
+
+#include "ucmd_lib.h"
+
+#define NET_FRAME_MAX 1518
+
+static unsigned char rx_buf[NET_FRAME_MAX];
+
+void _start(const char *arg)
+{
+	unsigned char frame[64], mac[6];
+	unsigned int i, deadline;
+	int got = 0;
+
+	(void)arg;
+	if (u_syscall1(SYS_NET_MAC, (int)mac) != 0) {
+		u_puts("netloop: no NIC found\n");
+		u_exit(1);
+	}
+	if (u_syscall1(SYS_NET_LOOPBACK, 1) != 0) {
+		u_puts("netloop: not supported by this NIC\n");
+		u_exit(1);
+	}
+
+	for (i = 0; i < 6; i++)
+		frame[i] = 0xFF;                      /* broadcast */
+	for (i = 0; i < 6; i++)
+		frame[6 + i] = mac[i];
+	frame[12] = 0x88;
+	frame[13] = 0xB6;                             /* local experimental */
+	frame[14] = 'N'; frame[15] = 'E'; frame[16] = 'B';
+	frame[17] = 'O'; frame[18] = 'S'; frame[19] = 'L';
+	frame[20] = 'O'; frame[21] = 'O';
+	for (i = 22; i < 64; i++)
+		frame[i] = 0x00;
+
+	u_puts("netloop: PHY loopback on, sending...\n");
+	u_syscall0(SYS_NET_ACQUIRE);
+	if (u_syscall2(SYS_NET_TX, (int)frame, sizeof(frame)) != 0) {
+		u_puts("netloop: transmit failed\n");
+		u_syscall0(SYS_NET_RELEASE);
+		u_syscall1(SYS_NET_LOOPBACK, 0);
+		u_exit(1);
+	}
+
+	deadline = u_ticks_ms() + 1000;
+	while ((int)(u_ticks_ms() - deadline) < 0) {
+		int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
+
+		if (len < 0)
+			continue;
+		if (len >= 22 &&
+		    rx_buf[12] == 0x88 && rx_buf[13] == 0xB6 &&
+		    rx_buf[14] == 'N' && rx_buf[15] == 'E' &&
+		    rx_buf[16] == 'B' && rx_buf[17] == 'O' &&
+		    rx_buf[18] == 'S') {
+			got = 1;
+			break;
+		}
+	}
+
+	u_syscall0(SYS_NET_RELEASE);
+	u_syscall1(SYS_NET_LOOPBACK, 0);
+	u_puts(got ? "netloop: PASS (frame came back)\n"
+		   : "netloop: FAIL (no frame came back)\n");
+	u_exit(got ? 0 : 1);
+}

+ 51 - 0
ucmd_netrecv.c

@@ -0,0 +1,51 @@
+/* netrecv: ring-3 command.  Listen for 8 s and dump frames. */
+
+#include "ucmd_lib.h"
+
+#define NET_FRAME_MAX 1518
+
+static unsigned char rx_buf[NET_FRAME_MAX];
+
+void _start(const char *arg)
+{
+	unsigned int deadline, count = 0;
+
+	(void)arg;
+	if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
+		u_puts("netrecv: no NIC found\n");
+		u_exit(1);
+	}
+
+	u_puts("netrecv: listening (8s)...\n");
+	deadline = u_ticks_ms() + 8000;
+
+	while ((int)(u_ticks_ms() - deadline) < 0) {
+		int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
+		unsigned int i;
+
+		if (len < 0)
+			continue;
+
+		u_syscall2(SYS_NET_DECODE, (int)rx_buf, (unsigned int)len);
+		u_putc('\n');
+
+		/* Raw hex dump for closer inspection. */
+		for (i = 0; i < (unsigned int)len; i++) {
+			u_put_hex2(rx_buf[i]);
+			u_putc(' ');
+			if ((i & 15) == 15)
+				u_putc('\n');
+		}
+		if (((unsigned int)len & 15) != 0)
+			u_putc('\n');
+
+		if (++count >= 32)
+			break;
+	}
+
+	u_syscall0(SYS_NET_RELEASE);
+	u_puts("netrecv: ");
+	u_putdec(count);
+	u_puts(" frame(s)\n");
+	u_exit(0);
+}

+ 21 - 0
ucmd_netregs.c

@@ -0,0 +1,21 @@
+/* netregs: ring-3 command.  Driver register dump. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+
+	(void)arg;
+	if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
+		u_puts("netregs: no NIC found\n");
+		u_exit(1);
+	}
+	if (u_syscall0(SYS_NET_REGS) != 0) {
+		u_puts("netregs: no register dump for ");
+		u_puts(n.name);
+		u_putc('\n');
+		u_exit(1);
+	}
+	u_exit(0);
+}

+ 25 - 0
ucmd_netreset.c

@@ -0,0 +1,25 @@
+/* netreset: ring-3 command.  Full NIC re-init. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	struct sys_netinfo n;
+
+	(void)arg;
+	if (u_syscall1(SYS_NET_INFO, (int)&n) != 1) {
+		u_puts("netreset: no NIC found\n");
+		u_exit(1);
+	}
+	if (u_syscall0(SYS_NET_RESET) != 0) {
+		u_puts("netreset: not supported by ");
+		u_puts(n.name);
+		u_putc('\n');
+		u_exit(1);
+	}
+	u_puts("netreset: re-initializing ");
+	u_puts(n.name);
+	u_puts("...\n");
+	u_syscall0(SYS_NET_PRINT_INFO);
+	u_exit(0);
+}

+ 40 - 0
ucmd_netsend.c

@@ -0,0 +1,40 @@
+/* netsend: ring-3 command.  Broadcast a 64-byte test frame. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	unsigned char frame[64], mac[6];
+	unsigned int i;
+
+	(void)arg;
+	if (u_syscall1(SYS_NET_MAC, (int)mac) != 0) {
+		u_puts("netsend: no NIC found\n");
+		u_exit(1);
+	}
+
+	/* dst=ff:ff:ff:ff:ff:ff, src=our MAC, ethertype 0x88B5 (local
+	 * experimental), payload "NEBS". */
+	for (i = 0; i < 6; i++)
+		frame[i] = 0xFF;
+	for (i = 0; i < 6; i++)
+		frame[6 + i] = mac[i];
+	frame[12] = 0x88;
+	frame[13] = 0xB5;
+	frame[14] = 'N';
+	frame[15] = 'E';
+	frame[16] = 'B';
+	frame[17] = 'S';
+	for (i = 18; i < 64; i++)
+		frame[i] = 0x00;
+
+	u_syscall0(SYS_NET_ACQUIRE);
+	if (u_syscall2(SYS_NET_TX, (int)frame, sizeof(frame)) != 0) {
+		u_puts("netsend: transmit failed\n");
+		u_syscall0(SYS_NET_RELEASE);
+		u_exit(1);
+	}
+	u_syscall0(SYS_NET_RELEASE);
+	u_puts("netsend: sent 64-byte broadcast frame\n");
+	u_exit(0);
+}

+ 353 - 0
ucmd_ping.c

@@ -0,0 +1,353 @@
+/* ping: ring-3 command.  Active ARP + ICMP echo (via the raw frame syscalls). */
+
+#include "ucmd_lib.h"
+
+#define ET_IPV4 0x0800
+#define ET_ARP  0x0806
+#define IPPROTO_ICMP 1
+#define ICMP_ECHO_REPLY   0
+#define ICMP_ECHO_REQUEST 8
+
+#define NET_FRAME_MAX 1518
+#define PING_COUNT  4
+#define PING_ID     0x4E42   /* "NB" */
+#define PING_WAIT_MS 1000
+
+static unsigned char rx_buf[NET_FRAME_MAX];
+static unsigned int ping_sent_ms[PING_COUNT];
+
+static unsigned short net_checksum(const void *data, unsigned int len)
+{
+	const unsigned char *p = (const unsigned char *)data;
+	unsigned int sum = 0;
+
+	while (len > 1) {
+		sum += ((unsigned int)p[0] << 8) | p[1];
+		p += 2;
+		len -= 2;
+	}
+	if (len)
+		sum += (unsigned int)p[0] << 8;
+	while (sum >> 16)
+		sum = (sum & 0xFFFFu) + (sum >> 16);
+	return (unsigned short)(~sum);
+}
+
+static int ip_eq(const unsigned char *a, const unsigned char *b)
+{
+	return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3];
+}
+
+/* ARP request "who has target? tell my_ip" -> 42-byte frame. */
+static unsigned int build_arp_request(const unsigned char *my_mac,
+				      const unsigned char *my_ip,
+				      const unsigned char *target_ip,
+				      unsigned char *out)
+{
+	unsigned char *a = out + 14;
+	unsigned int i;
+
+	for (i = 0; i < 6; i++)
+		out[i] = 0xFF;                        /* broadcast */
+	for (i = 0; i < 6; i++)
+		out[6 + i] = my_mac[i];
+	out[12] = 0x08;
+	out[13] = 0x06;
+	a[0] = 0; a[1] = 1;                           /* htype Ethernet */
+	a[2] = 0x08; a[3] = 0;                        /* ptype IPv4 */
+	a[4] = 6; a[5] = 4;
+	a[6] = 0; a[7] = 1;                           /* opcode request */
+	for (i = 0; i < 6; i++)                       /* sha */
+		a[8 + i] = my_mac[i];
+	for (i = 0; i < 4; i++)                       /* spa */
+		a[14 + i] = my_ip[i];
+	for (i = 0; i < 6; i++)                       /* tha */
+		a[18 + i] = 0;
+	for (i = 0; i < 4; i++)                       /* tpa */
+		a[24 + i] = target_ip[i];
+	return 42;
+}
+
+/* 1 + peer MAC if frame is an ARP reply from target_ip. */
+static int parse_arp_reply(const unsigned char *f, unsigned int len,
+			   const unsigned char *target_ip, unsigned char *mac)
+{
+	const unsigned char *a = f + 14;
+	unsigned int i;
+
+	if (len < 14 + 28)
+		return 0;
+	if ((((unsigned int)f[12] << 8) | f[13]) != ET_ARP)
+		return 0;
+	if ((((unsigned int)a[6] << 8) | a[7]) != 2)   /* opcode reply */
+		return 0;
+	if (a[4] != 6 || a[5] != 4)
+		return 0;
+	if (!ip_eq(a + 14, target_ip))                 /* sender IP == target */
+		return 0;
+	for (i = 0; i < 6; i++)
+		mac[i] = a[8 + i];
+	return 1;
+}
+
+/* ICMP echo request (14 + 20 + 16 bytes; the driver pads to 60). */
+static unsigned int build_icmp_echo(const unsigned char *src_mac,
+				    const unsigned char *dst_mac,
+				    const unsigned char *src_ip,
+				    const unsigned char *dst_ip,
+				    unsigned short seq, unsigned char *out)
+{
+	unsigned char *ip = out + 14;
+	unsigned char *ic = ip + 20;
+	unsigned char *pl = ic + 8;
+	unsigned short cs;
+	unsigned int i;
+
+	for (i = 0; i < 6; i++)
+		out[i] = dst_mac[i];
+	for (i = 0; i < 6; i++)
+		out[6 + i] = src_mac[i];
+	out[12] = 0x08;
+	out[13] = 0x00;
+
+	for (i = 0; i < 20; i++)
+		ip[i] = 0;
+	ip[0] = 0x45;
+	ip[2] = 0; ip[3] = 36;                        /* total length */
+	ip[8] = 64;                                   /* TTL */
+	ip[9] = IPPROTO_ICMP;
+	for (i = 0; i < 4; i++)
+		ip[12 + i] = src_ip[i];
+	for (i = 0; i < 4; i++)
+		ip[16 + i] = dst_ip[i];
+	cs = net_checksum(ip, 20);
+	ip[10] = (unsigned char)(cs >> 8);
+	ip[11] = (unsigned char)(cs & 0xFF);
+
+	ic[0] = ICMP_ECHO_REQUEST;
+	ic[1] = 0;
+	ic[2] = 0; ic[3] = 0;
+	ic[4] = (unsigned char)(PING_ID >> 8);
+	ic[5] = (unsigned char)(PING_ID & 0xFF);
+	ic[6] = (unsigned char)(seq >> 8);
+	ic[7] = (unsigned char)(seq & 0xFF);
+	pl[0] = 'n'; pl[1] = 'e'; pl[2] = 'b'; pl[3] = 'o';
+	pl[4] = 's'; pl[5] = 0; pl[6] = 0; pl[7] = 0;
+	cs = net_checksum(ic, 16);
+	ic[2] = (unsigned char)(cs >> 8);
+	ic[3] = (unsigned char)(cs & 0xFF);
+
+	return 14 + 20 + 16;
+}
+
+/* 1 + ICMP echo-reply seq for our IP from peer_ip. */
+static int parse_icmp_reply(const unsigned char *f, unsigned int len,
+			    const unsigned char *my_ip,
+			    const unsigned char *peer_ip,
+			    unsigned short *seq)
+{
+	const unsigned char *ip = f + 14;
+	const unsigned char *ic;
+	unsigned int ihl;
+	unsigned short ip_len, id;
+
+	if (len < 14 + 20)
+		return 0;
+	if ((((unsigned int)f[12] << 8) | f[13]) != ET_IPV4)
+		return 0;
+	if ((ip[0] >> 4) != 4)
+		return 0;
+	ihl = (unsigned int)(ip[0] & 0x0F) * 4;
+	if (ihl < 20 || ip[9] != IPPROTO_ICMP)
+		return 0;
+	ip_len = ((unsigned short)ip[2] << 8) | ip[3];
+	if (ip_len < ihl + 8 || (unsigned int)ip_len > len - 14)
+		return 0;
+	if (!ip_eq(ip + 12, peer_ip))                 /* src == peer */
+		return 0;
+	if (!ip_eq(ip + 16, my_ip))                   /* dst == us */
+		return 0;
+	ic = ip + ihl;
+	if (ic[0] != ICMP_ECHO_REPLY || ic[1] != 0)
+		return 0;
+	id = ((unsigned short)ic[4] << 8) | ic[5];
+	if (id != PING_ID)
+		return 0;
+	*seq = ((unsigned short)ic[6] << 8) | ic[7];
+	if (*seq < 1 || *seq > PING_COUNT)
+		return 0;
+	return 1;
+}
+
+/* Resolve target_ip's MAC by ARP (3 tries, 1 s each).
+ * Returns 0 = resolved, -1 = no reply, -2 = transmit failed.
+ * Prints the frames seen while waiting (capped) so a silent peer, a filtered
+ * reply and a reply we refuse to parse can be told apart on screen. */
+static int ping_arp(const unsigned char *my_mac, const unsigned char *my_ip,
+		    const unsigned char *target_ip, unsigned char *target_mac)
+{
+	unsigned char frame[64];
+	unsigned int tries, seen = 0;
+
+	for (tries = 0; tries < 3; tries++) {
+		unsigned int deadline = u_ticks_ms() + PING_WAIT_MS;
+		unsigned int flen = build_arp_request(my_mac, my_ip, target_ip, frame);
+
+		if (u_syscall2(SYS_NET_TX, (int)frame, flen) != 0)
+			return -2;
+		u_puts("ping: ARP request sent (try ");
+		u_putdec(tries + 1);
+		u_puts("/3)\n");
+		while ((int)(u_ticks_ms() - deadline) < 0) {
+			int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
+
+			if (len < 0)
+				continue;
+			if (seen < 8) {
+				u_puts("ping: rx ");
+				u_syscall2(SYS_NET_DECODE, (int)rx_buf,
+					   (unsigned int)len);
+				u_putc('\n');
+				seen++;
+			}
+			if (parse_arp_reply(rx_buf, (unsigned int)len,
+					    target_ip, target_mac))
+				return 0;
+		}
+	}
+	u_puts("ping: saw ");
+	u_putdec(seen);
+	u_puts(" frame(s) while waiting\n");
+	return -1;
+}
+
+void _start(const char *arg)
+{
+	unsigned char target[4], my_ip[4], my_mac[6], target_mac[6];
+	unsigned char mask[4], gw[4], hop[4];
+	unsigned int i, replies = 0;
+
+	if (!arg || !arg[0]) {
+		u_puts("ping: usage ping <host|a.b.c.d>\n");
+		u_exit(1);
+	}
+
+	if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
+		u_puts("ping: no NIC found\n");
+		u_exit(1);
+	}
+
+	/* Dotted IP, or a name to resolve first. */
+	if (u_parse_ip(arg, target) != 0) {
+		if (u_syscall2(SYS_DNS_RESOLVE, (int)arg, (int)target) != 0) {
+			u_syscall0(SYS_NET_RELEASE);
+			u_exit(1);
+		}
+	}
+
+	u_syscall1(SYS_NET_MAC, (int)my_mac);
+	u_syscall1(SYS_NET_GET_IP, (int)my_ip);
+
+	/* With a mask and gateway known, off-subnet targets go via the router;
+	 * we can only ARP for the next hop. */
+	u_syscall1(SYS_NET_GET_MASK, (int)mask);
+	u_syscall1(SYS_NET_GET_GW, (int)gw);
+	{
+		unsigned int j, off = 0;
+
+		for (j = 0; j < 4; j++)
+			hop[j] = target[j];
+		if ((mask[0] || mask[1] || mask[2] || mask[3]) &&
+		    (gw[0] || gw[1] || gw[2] || gw[3])) {
+			for (j = 0; j < 4; j++) {
+				if ((target[j] & mask[j]) != (my_ip[j] & mask[j]))
+					off = 1;
+			}
+		}
+		if (off) {
+			for (j = 0; j < 4; j++)
+				hop[j] = gw[j];
+			u_puts("ping: ");
+			u_put_ip(target);
+			u_puts(" via ");
+			u_put_ip(hop);
+			u_putc('\n');
+		}
+
+		{
+			int r = ping_arp(my_mac, my_ip, hop, target_mac);
+
+			if (r == -2) {
+				u_puts("ping: transmit failed (see netregs)\n");
+				u_syscall0(SYS_NET_RELEASE);
+				u_exit(1);
+			}
+			if (r != 0) {
+				u_puts("ping: no ARP reply from ");
+				u_put_ip(hop);
+				u_putc('\n');
+				u_syscall0(SYS_NET_RELEASE);
+				u_exit(1);
+			}
+		}
+	}
+	u_puts("ping: ");
+	u_put_ip(target);
+	u_puts(" is-at ");
+	u_put_mac(target_mac);
+	u_putc('\n');
+
+	for (i = 0; i < PING_COUNT; i++) {
+		unsigned char frame[64];
+		unsigned int flen, deadline;
+		int got = 0;
+
+		flen = build_icmp_echo(my_mac, target_mac, my_ip, target,
+				       (unsigned short)(i + 1), frame);
+		ping_sent_ms[i] = u_ticks_ms();
+		if (u_syscall2(SYS_NET_TX, (int)frame, flen) != 0) {
+			u_puts("ping: transmit failed\n");
+			u_syscall0(SYS_NET_RELEASE);
+			u_exit(1);
+		}
+
+		deadline = u_ticks_ms() + PING_WAIT_MS;
+		while ((int)(u_ticks_ms() - deadline) < 0 && !got) {
+			unsigned short seq;
+			int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
+			unsigned int rtt;
+
+			if (len < 0)
+				continue;
+			if (!parse_icmp_reply(rx_buf, (unsigned int)len,
+					      my_ip, target, &seq))
+				continue;
+			rtt = u_ticks_ms() - ping_sent_ms[seq - 1];
+			u_puts("reply: seq=");
+			u_putdec(seq);
+			u_puts(" rtt=");
+			if (rtt == 0)
+				u_puts("<1");
+			else
+				u_putdec(rtt);
+			u_puts("ms\n");
+			got = 1;
+			replies++;
+		}
+		if (!got) {
+			u_puts("ping: timeout, seq=");
+			u_putdec(i + 1);
+			u_putc('\n');
+		}
+	}
+
+	u_puts("ping: ");
+	u_putdec(replies);
+	u_puts("/");
+	u_putdec(PING_COUNT);
+	u_puts(" replies from ");
+	u_put_ip(target);
+	u_putc('\n');
+	u_syscall0(SYS_NET_RELEASE);
+	u_exit(replies ? 0 : 1);
+}

+ 54 - 0
ucmd_pong.c

@@ -0,0 +1,54 @@
+/* pong: ring-3 command.  Answer ARP/ICMP for our IP for 30 s. */
+
+#include "ucmd_lib.h"
+
+#define NET_FRAME_MAX 1518
+#define NETLOG_PORT 19290
+
+static unsigned char rx_buf[NET_FRAME_MAX];
+static unsigned char reply_buf[NET_FRAME_MAX];
+
+void _start(const char *arg)
+{
+	unsigned char ip[4];
+	unsigned int deadline, count = 0;
+
+	(void)arg;
+	if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
+		u_puts("pong: no NIC found\n");
+		u_exit(1);
+	}
+
+	u_syscall1(SYS_NET_GET_IP, (int)ip);
+	u_puts("pong: answering ARP/ICMP for 30s (ip=");
+	u_put_ip(ip);
+	u_puts(")...\n");
+
+	deadline = u_ticks_ms() + 30000;
+	while ((int)(u_ticks_ms() - deadline) < 0) {
+		int len = u_syscall2(SYS_NET_RX, (int)rx_buf, NET_FRAME_MAX);
+		int rlen;
+
+		if (len < 0)
+			continue;
+
+		rlen = u_syscall3(SYS_NET_REPLY, (int)rx_buf, (unsigned int)len,
+				  (int)reply_buf);
+		if (rlen > 0) {
+			if (u_syscall2(SYS_NET_TX, (int)reply_buf,
+				       (unsigned int)rlen) == 0) {
+				u_puts("reply: ");
+				u_syscall2(SYS_NET_DECODE, (int)reply_buf,
+					   (unsigned int)rlen);
+				u_putc('\n');
+				count++;
+			}
+		}
+	}
+
+	u_syscall0(SYS_NET_RELEASE);
+	u_puts("pong: done (");
+	u_putdec(count);
+	u_puts(" replies)\n");
+	u_exit(0);
+}