Repository navigation
Conversation
| /* ---- slirp callbacks ---- */ | ||
|
|
||
| static slirp_ssize_t slirp_send_packet_cb(const void *buf, size_t len, void *opaque) | ||
| { |
There was a problem hiding this comment.
as shown in the build failure, older libslirp versions (4.6.1 on github actions, 4.7.0 in my local environment) don't have this typedef...
we need to have a check for this, or force install newer libslirp, which might be a bit complicated.
lib/virtio_net_slirp.c:218:8: error: unknown type name ‘slirp_ssize_t’
218 | static slirp_ssize_t slirp_send_packet_cb(const void *buf, size_t len, void *opaque)
| ^~~~~~~~~~~~~
lib/virtio_net_slirp.c: In function ‘slirp_send_packet_cb’:
lib/virtio_net_slirp.c:227:25: error: ‘slirp_ssize_t’ undeclared (first use in this function); did you mean ‘lkl_ssize_t’?
227 | return (slirp_ssize_t)len;
| ^~~~~~~~~~~~~
There was a problem hiding this comment.
Fixed: slirp_ssize_t falls back to ssize_t when !SLIRP_CHECK_VERSION(4, 8, 0). Tested with 4.6.1 on Ubuntu 22.04.
| #ifdef __MINGW32__ | ||
| WaitForSingleObject(nd->poll_thread, INFINITE); | ||
| CloseHandle(nd->poll_thread); | ||
| #else | ||
| pthread_join(nd->poll_thread, NULL); |
There was a problem hiding this comment.
can we use lkl_host_ops.thread_join() instead ? (cc: @tavip)
There was a problem hiding this comment.
Done, it uses lkl_host_ops.thread_join() now (and lkl_host_ops.mutex_* for the locks).
| #ifdef __MINGW32__ | ||
| nd->poll_thread = CreateThread(NULL, 0, slirp_poll_thread_fn, nd, 0, NULL); | ||
| if (!nd->poll_thread) { | ||
| fprintf(stderr, "slirp: failed to create poll thread\n"); | ||
| slirp_cleanup(nd->slirp); | ||
| slirp_pipe_close(nd->pipe[0]); | ||
| slirp_pipe_close(nd->pipe[1]); | ||
| slirp_mutex_destroy(&nd->rx_lock); | ||
| free(nd); | ||
| return NULL; | ||
| } | ||
| #else | ||
| if (pthread_create(&nd->poll_thread, NULL, slirp_poll_thread_fn, nd) != 0) { |
There was a problem hiding this comment.
ditto: can we use lkl_host_ops.thread_create() ?
There was a problem hiding this comment.
Done, lkl_host_ops.thread_create().
Add a virtio-net backend using libslirp that provides TCP/IP networking without root privileges or pre-configured TAP interfaces, the same user-mode NAT stack used by QEMU's "-netdev user". - Guest network 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 - Packets from slirp are queued in a ring buffer and handed to the virtio rx path; a dedicated poll thread drives slirp's socket I/O - libslirp is not thread safe, so all libslirp calls (virtio tx path, poll thread, hostfwd API) are serialized by a mutex - Threads and mutexes come from lkl_host_ops; only the wakeup pipe, clock and poll primitives are platform specific (POSIX and Win32) - Works with libslirp 4.x; slirp_ssize_t is only defined since 4.8.0 The backend is enabled automatically when slirp/libslirp.h is found (POSIX) or when $(CROSS_COMPILE)pkg-config finds slirp (NT64). Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
Run net-test with the slirp backend, similar to the existing pipe/tap/ raw tests. The guest uses the slirp NAT network (10.0.2.0/24) and pings the slirp gateway 10.0.2.2. The test is skipped when the slirp backend is not configured. Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
Install libslirp-dev on Ubuntu so the slirp network backend is built and tested in CI. libglib2.0-dev is needed as well: slirp.pc requires glib-2.0, but libslirp-dev does not depend on it on Ubuntu 22.04. Signed-off-by: Sheldon Qi <3365420+xdqi@users.noreply.github.com>
|
Thanks for the reviews @tavip @thehajime, and sorry for the delay. I rebased on the current master and squashed the series into three commits (backend, test, CI). Changes since the last version:
Testing (local; CI will confirm):
|
Test Results111 files + 5 111 suites +5 11m 9s ⏱️ +39s Results for commit 2e083e4. ± Comparison against base commit d0f76a7. This pull request removes 3 and adds 13 tests. Note that renamed tests count towards both. |
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 as QEMU's
-netdev user), so LKL guests can access the network without any host configuration.Changes
tools/lkl/lib/virtio_net_slirp.ctools/lkl/include/lkl.hlkl_netdev_slirp_create,add_hostfwd,remove_hostfwdtools/lkl/Makefile.autoconfpkg-config --exists slirp(POSIX),$(CROSS_COMPILE)pkg-config(NT64)tools/lkl/lib/Buildvirtio_net_slirp.otools/lkl/tests/net.sh -b slirptest (ICMP to the slirp gateway).github/workflows/ci.ymllibslirp-devandlibglib2.0-devArchitecture
10.0.2.0/24, gateway10.0.2.2, DNS10.0.2.3send_packetcallback and the LKL RX pathnotifycallback and on shutdown)lkl_host_ops; only the wakeup pipe, the clock and poll() are platform specific (POSIX and Winsock)API
Build
Requires
libslirp-dev(pkg-config:slirp, which also requires glib-2.0). The backend is auto-detected at build time, with no extra flags needed. If slirp is not installed, the backend is silently disabled.Example usage
Disclaimer
🤖 Generated with Claude Code but I've audited the code.