Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly support DHCP + add tests+ update libslirp to v4.6.1 #270

Merged
merged 1 commit into from
Jun 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
libslirp_commit: [master, v4.6.0, v4.5.0, v4.1.0]
libslirp_commit: [master, v4.6.1, v4.5.0, v4.1.0]
steps:
- uses: actions/checkout@v2
- run: docker build -t slirp4netns-tests --build-arg LIBSLIRP_COMMIT -f Dockerfile.tests .
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
run: sh ./run-vagrant-tests
env:
LIBSECCOMP_COMMIT: v2.5.0
LIBSLIRP_COMMIT: v4.6.0
LIBSLIRP_COMMIT: v4.6.1
artifact:
runs-on: ubuntu-20.04
steps:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.artifact
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG LIBSLIRP_COMMIT=v4.6.0
ARG LIBSLIRP_COMMIT=v4.6.1
ARG DEBIAN_VERSION=10

FROM --platform=$TARGETPLATFORM debian:${DEBIAN_VERSION} AS build
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.buildtests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG LIBSLIRP_COMMIT=v4.6.0
ARG LIBSLIRP_COMMIT=v4.6.1

# Alpine
FROM alpine:3 AS buildtest-alpine3-static
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG LIBSLIRP_COMMIT=v4.6.0
ARG LIBSLIRP_COMMIT=v4.6.1

FROM ubuntu:20.04 AS build
ENV DEBIAN_FRONTEND=noninteractive
Expand All @@ -16,6 +16,6 @@ RUN ./autogen.sh && ./configure && make -j $(nproc)

FROM build AS test
USER 0
RUN apt update && apt install -y git libtool iproute2 clang clang-format clang-tidy iputils-ping iperf3 ncat jq
RUN apt update && apt install -y git libtool iproute2 clang clang-format clang-tidy iputils-ping iperf3 ncat jq udhcpc
USER 1000:1000
CMD ["make", "ci"]
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AM_CFLAGS = @GLIB_CFLAGS@ @SLIRP_CFLAGS@ @LIBCAP_CFLAGS@ @LIBSECCOMP_CFLAGS@
noinst_LIBRARIES = libparson.a

AM_TESTS_ENVIRONMENT = PATH="$(abs_top_builddir):$(PATH)"
TESTS = tests/test-slirp4netns.sh tests/test-slirp4netns-configure.sh tests/test-slirp4netns-exit-fd.sh tests/test-slirp4netns-ready-fd.sh tests/test-slirp4netns-api-socket.sh tests/test-slirp4netns-disable-host-loopback.sh tests/test-slirp4netns-cidr.sh tests/test-slirp4netns-outbound-addr.sh tests/test-slirp4netns-disable-dns.sh tests/test-slirp4netns-seccomp.sh tests/test-slirp4netns-macaddress.sh
TESTS = tests/test-slirp4netns.sh tests/test-slirp4netns-configure.sh tests/test-slirp4netns-exit-fd.sh tests/test-slirp4netns-ready-fd.sh tests/test-slirp4netns-api-socket.sh tests/test-slirp4netns-disable-host-loopback.sh tests/test-slirp4netns-cidr.sh tests/test-slirp4netns-outbound-addr.sh tests/test-slirp4netns-disable-dns.sh tests/test-slirp4netns-seccomp.sh tests/test-slirp4netns-macaddress.sh tests/test-slirp4netns-dhcp.sh

EXTRA_DIST = \
slirp4netns.1.md \
Expand Down
2 changes: 2 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Vagrant.configure("2") do |config|
glib2-devel libcap-devel \
git-core libtool iproute iputils iperf3 nmap jq

# TODO: install udhcpc (required by test-slirp4netns-dhcp.sh)

cd /src
chown vagrant .

Expand Down
9 changes: 9 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ static int parent(int sock, int ready_fd, int exit_fd, const char *api_socket,
struct slirp4netns_config *cfg, pid_t target_pid)
{
int rc, tapfd;
struct in_addr vdhcp_end = {
#define NB_BOOTP_CLIENTS 16
/* NB_BOOTP_CLIENTS is hard-coded to 16 in libslirp:
https://gitlab.freedesktop.org/slirp/libslirp/-/issues/49 */
.s_addr = htonl(ntohl(cfg->vdhcp_start.s_addr) + NB_BOOTP_CLIENTS - 1),
#undef NB_BOOTP_CLIENTS
};
if ((tapfd = recvfd(sock)) < 0) {
return tapfd;
}
Expand All @@ -277,6 +284,8 @@ static int parent(int sock, int ready_fd, int exit_fd, const char *api_socket,
printf("* Netmask: %s\n", inet_ntoa(cfg->vnetmask));
printf("* Gateway: %s\n", inet_ntoa(cfg->vhost));
printf("* DNS: %s\n", inet_ntoa(cfg->vnameserver));
printf("* DHCP begin: %s\n", inet_ntoa(cfg->vdhcp_start));
printf("* DHCP end: %s\n", inet_ntoa(vdhcp_end));
printf("* Recommended IP: %s\n", inet_ntoa(cfg->recommended_vguest));
if (api_socket != NULL) {
printf("* API Socket: %s\n", api_socket);
Expand Down
6 changes: 5 additions & 1 deletion slirp4netns.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.nh
.TH SLIRP4NETNS 1 "November 2020" "Rootless Containers" "User Commands"
.TH SLIRP4NETNS 1 "June 2021" "Rootless Containers" "User Commands"

.SH NAME
.PP
Expand Down Expand Up @@ -31,6 +31,10 @@ Gateway/Host: 10.0.2.2 (network address + 2)
.IP \(bu 2
DNS: 10.0.2.3 (network address + 3)
.IP \(bu 2
DHCP begin: 10.0.2.15 (network address + 15)
.IP \(bu 2
DHCP end: 10.0.2.30 (network address + 30)
.IP \(bu 2
IPv6 CIDR: fd00::/64
.IP \(bu 2
IPv6 Gateway/Host: fd00::2
Expand Down
4 changes: 3 additions & 1 deletion slirp4netns.1.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SLIRP4NETNS 1 "November 2020" "Rootless Containers" "User Commands"
SLIRP4NETNS 1 "June 2021" "Rootless Containers" "User Commands"
==================================================

# NAME
Expand All @@ -21,6 +21,8 @@ Default configuration:
* CIDR: 10.0.2.0/24
* Gateway/Host: 10.0.2.2 (network address + 2)
* DNS: 10.0.2.3 (network address + 3)
* DHCP begin: 10.0.2.15 (network address + 15)
* DHCP end: 10.0.2.30 (network address + 30)
* IPv6 CIDR: fd00::/64
* IPv6 Gateway/Host: fd00::2
* IPv6 DNS: fd00::3
Expand Down
3 changes: 3 additions & 0 deletions tests/common.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# See https://www.gnu.org/software/automake/manual/html_node/Scripts_002dbased-Testsuites.html#Testsuite-progress-on-console
TEST_EXIT_CODE_SKIP=77

function nsenter_flags {
pid=$1
flags="--target=${pid}"
Expand Down
35 changes: 35 additions & 0 deletions tests/test-slirp4netns-dhcp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# This test should pass with libslirp < 4.6.0 and libslirp >= 4.6.1,
# but should fail with libslirp == 4.6.0 .
# https://gitlab.freedesktop.org/slirp/libslirp/-/issues/48

set -xeuo pipefail

. $(dirname $0)/common.sh

if ! command -v udhcpc 2>&1; then
echo "udhcpc is missing, skipping the test"
exit "$TEST_EXIT_CODE_SKIP"
fi

unshare -r -n sleep infinity &
child=$!

wait_for_network_namespace $child

slirp4netns $child tap0 &
slirp_pid=$!

udhcpc_log=$(mktemp /tmp/slirp4netns-test-udhcpc.XXXXXXXXXX)

function cleanup {
kill -9 $child $slirp_pid
rm -f $udhcpc_log
}
trap cleanup EXIT

nsenter $(nsenter_flags $child) ip link set lo up
nsenter $(nsenter_flags $child) ip link set tap0 up
nsenter $(nsenter_flags $child) timeout 10s udhcpc -q -i tap0 -s /bin/true 2>&1 | tee $udhcpc_log
grep 10.0.2.15 $udhcpc_log