Skip to content

lkl: add libslirp network backend for user-mode networking#631

Open
xdqi wants to merge 5 commits intolkl:masterfrom
xdqi:lkl-slirp
Open

lkl: add libslirp network backend for user-mode networking#631
xdqi wants to merge 5 commits intolkl:masterfrom
xdqi:lkl-slirp

Conversation

@xdqi
Copy link
Copy Markdown

@xdqi xdqi commented May 4, 2026

Summary

Add a libslirp-based virtio-net backend to LKL, enabling user-mode TCP/IP networking without root privileges or pre-configured TAP interfaces.

Motivation

The existing LKL network backends (TAP, macvtap, DPDK, VDE, wintap) all require either root privileges, pre-configured host interfaces, or specific hardware. libslirp provides user-mode NAT networking — the same stack used by QEMU's -netdev user — allowing LKL guests to access the network without any host configuration.

Changes

File Description
tools/lkl/lib/virtio_net_slirp.c New slirp backend (648 lines), POSIX + MinGW portable
tools/lkl/include/lkl.h Slirp API: lkl_netdev_slirp_create, add_hostfwd, remove_hostfwd
tools/lkl/Makefile.autoconf Auto-detection: header check (POSIX) or pkg-config (NT64)
tools/lkl/lib/Build Build integration for virtio_net_slirp.o
tools/lkl/include/mingw32/sys/uio.h Win32 stub for struct iovec

Architecture

  • Guest network: 10.0.2.0/24, gateway 10.0.2.2, DNS 10.0.2.3
  • Packet ring buffer (256 entries) between slirp's send_packet callback and LKL RX path
  • Dedicated poll thread drives slirp's internal socket I/O
  • Platform abstraction layer supports both POSIX (pthread/poll/pipe) and Win32 (Winsock/WSA/CreateThread)

API

struct lkl_netdev *lkl_netdev_slirp_create(void);
int lkl_netdev_slirp_add_hostfwd(struct lkl_netdev *nd, int is_udp,
    const char *host_addr, int host_port,
    const char *guest_addr, int guest_port);
int lkl_netdev_slirp_remove_hostfwd(struct lkl_netdev *nd, int is_udp,
    const char *host_addr, int host_port);

Build

Requires libslirp-dev (pkg-config: slirp). The backend is auto-detected at build time — no extra flags needed. If slirp is not installed, the backend is silently disabled.

Example usage

// Create slirp netdev — no root required
struct lkl_netdev *nd = lkl_netdev_slirp_create();

// Forward host port 12345 to guest 10.0.2.15:12345
lkl_netdev_slirp_add_hostfwd(nd, 0, "0.0.0.0", 12345, "10.0.2.15", 12345);

// Start kernel, add netdev, configure interface (same as other backends)
lkl_init(&lkl_host_ops);
lkl_start_kernel("mem=32M");
int nd_id = lkl_netdev_add(nd, NULL);
lkl_if_up(lkl_netdev_get_ifindex(nd_id));
lkl_if_set_ipv4(lkl_netdev_get_ifindex(nd_id),
    inet_addr("10.0.2.15"), 24);
lkl_set_ipv4_gateway(inet_addr("10.0.2.2"));

Disclaimer

🤖 Generated with Claude Code but I've audited the code.

Add a virtio-net backend using libslirp that provides full TCP/IP
networking without root privileges or pre-configured TAP interfaces.

Features:
- User-mode NAT (guest 10.0.2.0/24, gateway 10.0.2.2, DNS 10.0.2.3)
- Port forwarding via lkl_netdev_slirp_add_hostfwd/remove_hostfwd
- Auto-detected when slirp/libslirp.h is present (POSIX) or
  pkg-config finds slirp (NT64/MinGW)
- Portable: supports both POSIX (pthread/poll) and Win32 (Winsock)
- Packet ring buffer between slirp send_packet callback and LKL rx
- Dedicated poll thread drives slirp internal socket I/O

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@tavip tavip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks @xdqi ! Overall looks good, I'll take a closer look a bit later.

Please check the windows test failure and the checkpatch errors.

Also, please add tests similar with what we have for the other backends.

Comment thread tools/lkl/include/mingw32/sys/uio.h Outdated
Comment thread tools/lkl/lib/virtio_net_slirp.c
xdqi added 4 commits May 6, 2026 13:47
Add SPDX-License-Identifier, update copyright name, and fix missing
blank lines after declarations throughout the file for checkpatch
compliance.

Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
The stub at tools/lkl/include/mingw32/sys/uio.h intercepts the system
<sys/uio.h> on MSYS2 builds, preventing struct iovec from being
defined. lkl_host.h already provides struct iovec for MinGW in its
#else branch, and MSYS2 uses the system header natively.

Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
Add a test suite for the libslirp-based network backend, similar to
the existing pipe/tap/raw tests. Uses the NAT network (10.0.2.0/24)
with gateway 10.0.2.2. Tests netdev creation, kernel boot, interface
configuration, and ICMP connectivity through the slirp gateway.

Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
Add libslirp-dev for Ubuntu and mingw-w64-x86_64-libslirp for MSYS2
so the slirp network backend is auto-detected and tested in CI.

Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants