Skip to content

Commit ba0ec5a

Browse files
committed
linux: add support for network devices
opencontainers/runtime-spec#1271 added support for moving existing network devices to the container network namespace. Closes: containers#1712 Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent fe8f327 commit ba0ec5a

File tree

7 files changed

+618
-0
lines changed

7 files changed

+618
-0
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ libcrun_SOURCES = src/libcrun/utils.c \
6969
src/libcrun/seccomp_notify.c \
7070
src/libcrun/signals.c \
7171
src/libcrun/status.c \
72+
src/libcrun/net_device.c \
7273
src/libcrun/terminal.c
7374

7475
if HAVE_EMBEDDED_YAJL
@@ -157,6 +158,7 @@ EXTRA_DIST = COPYING COPYING.libcrun README.md NEWS SECURITY.md rpm/crun.spec au
157158
src/libcrun/linux.h src/libcrun/utils.h src/libcrun/error.h src/libcrun/criu.h \
158159
src/libcrun/scheduler.h src/libcrun/status.h src/libcrun/terminal.h \
159160
src/libcrun/mount_flags.h src/libcrun/intelrdt.h src/libcrun/ring_buffer.h src/libcrun/string_map.h \
161+
src/libcrun/net_device.h \
160162
crun.1.md crun.1 libcrun.lds \
161163
krun.1.md krun.1 \
162164
lua/luacrun.rockspec

src/libcrun/container.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,10 @@ libcrun_container_run_internal (libcrun_container_t *container, libcrun_context_
26332633
if (UNLIKELY (ret < 0))
26342634
goto fail;
26352635

2636+
ret = libcrun_move_network_devices (container, pid, err);
2637+
if (UNLIKELY (ret < 0))
2638+
goto fail;
2639+
26362640
/* sync send own pid. */
26372641
ret = TEMP_FAILURE_RETRY (write (sync_socket, &pid, sizeof (pid)));
26382642
if (UNLIKELY (ret != sizeof (pid)))

src/libcrun/linux.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "scheduler.h"
5353
#include "intelrdt.h"
5454
#include "io_priority.h"
55+
#include "net_device.h"
5556

5657
#include <sys/socket.h>
5758
#include <libgen.h>
@@ -6059,3 +6060,33 @@ libcrun_destroy_runtime_mounts (libcrun_container_t *container arg_unused, libcr
60596060

60606061
return run_in_container_namespace (status, do_umount_in_a_container, &args, err);
60616062
}
6063+
6064+
int
6065+
libcrun_move_network_devices (libcrun_container_t *container, pid_t pid, libcrun_error_t *err)
6066+
{
6067+
runtime_spec_schema_config_schema *def = container->container_def;
6068+
cleanup_close int netns_fd = -1;
6069+
char ns_file[64];
6070+
size_t i;
6071+
int ret;
6072+
6073+
if (def == NULL || def->linux == NULL || def->linux->net_devices == NULL)
6074+
return 0;
6075+
6076+
snprintf (ns_file, sizeof (ns_file), "/proc/%d/ns/net", pid);
6077+
6078+
netns_fd = open (ns_file, O_RDONLY);
6079+
if (UNLIKELY (netns_fd < 0))
6080+
return crun_make_error (err, errno, "open `%s`", ns_file);
6081+
6082+
for (i = 0; i < def->linux->net_devices->len; i++)
6083+
{
6084+
const char *new_name = def->linux->net_devices->values[i]->name ?: def->linux->net_devices->keys[i];
6085+
6086+
ret = move_network_device (def->linux->net_devices->keys[i], new_name, netns_fd, err);
6087+
if (UNLIKELY (ret < 0))
6088+
return ret;
6089+
}
6090+
6091+
return 0;
6092+
}

src/libcrun/linux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ const char *libcrun_get_intelrdt_name (const char *ctr_name, libcrun_container_t
141141

142142
int libcrun_apply_intelrdt (const char *ctr_name, libcrun_container_t *container, pid_t pid, int actions, libcrun_error_t *err);
143143

144+
int libcrun_move_network_devices (libcrun_container_t *container, pid_t pid, libcrun_error_t *err);
145+
144146
int libcrun_destroy_intelrdt (const char *name, libcrun_error_t *err);
145147

146148
int libcrun_update_intel_rdt (const char *ctr_name, libcrun_container_t *container, const char *l3_cache_schema, const char *mem_bw_schema, char *const *schemata, libcrun_error_t *err);

0 commit comments

Comments
 (0)