Skip to content

Commit 6de834c

Browse files
committed
Refactor network device setup: rename functions, inline setup_network_device, and fix clippy lints
Signed-off-by: nayuta-ai <[email protected]>
1 parent 08e9e98 commit 6de834c

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

crates/libcontainer/src/network/network_device.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn dev_change_net_namespace(
3838
.filter(|d| !d.is_empty())
3939
.map_or(name, |d| d);
4040

41-
let link = link_client.get_by_name(&name)?;
41+
let link = link_client.get_by_name(name)?;
4242

4343
let index = link.header.index;
4444

@@ -51,7 +51,7 @@ pub fn dev_change_net_namespace(
5151
let addrs = addr_client.get_by_index(index)?;
5252

5353
link_client
54-
.set_ns_fd(index, &new_name, netns_file.as_raw_fd())
54+
.set_ns_fd(index, new_name, netns_file.as_raw_fd())
5555
.map_err(|err| {
5656
tracing::error!(?err, "failed to set_ns_fd");
5757
err
@@ -63,32 +63,6 @@ pub fn dev_change_net_namespace(
6363
Ok(serialize_addrs)
6464
}
6565

66-
/// setup_network_device sets up a network device in a new namespace.
67-
/// It moves the device to the new namespace and adds the IP addresses to the device.
68-
/// It also sets the device up.
69-
pub fn setup_network_device(
70-
name: &str,
71-
net_dev: &LinuxNetDevice,
72-
serialize_addrs: Vec<SerializableAddress>,
73-
) -> Result<()> {
74-
let mut link_client = LinkClient::new(create_network_client())?;
75-
let mut addr_client = AddressClient::new(create_network_client())?;
76-
77-
let new_name = net_dev
78-
.name()
79-
.as_ref()
80-
.filter(|d| !d.is_empty())
81-
.map_or(name, |d| d);
82-
83-
let ns_link = link_client.get_by_name(&new_name)?;
84-
let ns_index = ns_link.header.index;
85-
86-
setup_addresses_in_namespace(serialize_addrs, &new_name, ns_index, &mut addr_client)?;
87-
88-
link_client.set_up(ns_index)?;
89-
Ok(())
90-
}
91-
9266
/// Core logic for setting up addresses in the new namespace
9367
/// This function is extracted to make it testable without system calls
9468
pub fn setup_addresses_in_namespace(

crates/libcontainer/src/process/container_main_process.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::PathBuf;
33

44
use nix::sys::wait::{waitpid, WaitStatus};
55
use nix::unistd::Pid;
6-
use oci_spec::runtime::{Linux, LinuxNamespace, LinuxNamespaceType};
6+
use oci_spec::runtime::{Linux, LinuxNamespaceType};
77

88
use crate::network::network_device::dev_change_net_namespace;
99
use crate::network::serialize::SerializableAddress;
@@ -126,7 +126,7 @@ pub fn container_main_process(container_args: &ContainerArgs) -> Result<(Pid, bo
126126
let mut need_to_clean_up_intel_rdt_subdirectory = false;
127127

128128
if let Some(linux) = container_args.spec.linux() {
129-
setup_network_device(linux, init_pid, &mut main_receiver, &mut init_sender)?;
129+
move_network_devices_to_container(linux, init_pid, &mut main_receiver, &mut init_sender)?;
130130
}
131131

132132
if let Some(linux) = container_args.spec.linux() {
@@ -238,25 +238,28 @@ fn setup_mapping(config: &UserNamespaceConfig, pid: Pid) -> Result<()> {
238238
Ok(())
239239
}
240240

241-
/// setup_network_device sets up and initializes any defined network interface inside the container.
242-
fn setup_network_device(
241+
/// Moves configured network devices from the host to the container's network namespace.
242+
/// This function waits for the init process to join its namespace, then transfers each
243+
/// configured device while preserving network addresses. Returns early if the container
244+
/// runs in the host network namespace.
245+
fn move_network_devices_to_container(
243246
linux: &Linux,
244247
init_pid: Pid,
245248
main_receiver: &mut channel::MainReceiver,
246249
init_sender: &mut channel::InitSender,
247250
) -> Result<()> {
248-
// host network pods does not move network devices.
249251
if let Some(namespaces) = linux.namespaces() {
252+
// network devices are not moved for containers running in the host network.
250253
if !namespaces
251254
.iter()
252255
.any(|ns| ns.typ() == LinuxNamespaceType::Network)
253256
{
254257
return Ok(());
255258
}
256259

257-
// get the namespace defined by the config and fall back
258-
// to the one created by youki to run the container process.
259-
let fallback_ns_path = PathBuf::from(format!("/proc/{}/ns/net", init_pid.as_raw()));
260+
// the container init process has already joined the provided net namespace,
261+
// so we can use the process's net ns path directly.
262+
let default_ns_path = PathBuf::from(format!("/proc/{}/ns/net", init_pid.as_raw()));
260263
let ns_path = namespaces
261264
.iter()
262265
.find_map(|ns| {
@@ -266,7 +269,7 @@ fn setup_network_device(
266269
None
267270
}
268271
})
269-
.unwrap_or_else(|| &fallback_ns_path);
272+
.unwrap_or_else(|| &default_ns_path);
270273

271274
// If moving any of the network devices fails, we return an error immediately.
272275
// The runtime spec requires that the kernel handles moving back any devices
@@ -278,7 +281,7 @@ fn setup_network_device(
278281
.iter()
279282
.map(|(name, net_dev)| {
280283
let addrs =
281-
dev_change_net_namespace(name, ns_path, net_dev).map_err(|err| {
284+
dev_change_net_namespace(name, &ns_path, net_dev).map_err(|err| {
282285
tracing::error!("failed to dev_change_net_namespace: {}", err);
283286
err
284287
})?;

crates/libcontainer/src/process/init/process.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use super::error::InitProcessError;
1818
use super::Result;
1919
use crate::error::MissingSpecError;
2020
use crate::namespaces::Namespaces;
21-
use crate::network::network_device::setup_network_device;
21+
use crate::network::address::AddressClient;
22+
use crate::network::link::LinkClient;
23+
use crate::network::network_device::setup_addresses_in_namespace;
24+
use crate::network::wrapper::create_network_client;
2225
use crate::process::args::{ContainerArgs, ContainerType};
2326
use crate::process::channel;
2427
use crate::rootfs::RootFS;
@@ -281,10 +284,12 @@ pub fn container_init_process(
281284
// This is done here before dropping capabilities because we need to be able to add IP addresses to the device
282285
// and set up the device.
283286
if let Some(network_devices) = ctx.linux.net_devices() {
284-
setup_net_devices(network_devices, main_sender, init_receiver).map_err(|err| {
285-
tracing::error!(?err, "failed to setup net_device");
286-
err
287-
})?;
287+
configure_container_network_devices(network_devices, main_sender, init_receiver).map_err(
288+
|err| {
289+
tracing::error!(?err, "failed to setup net_device");
290+
err
291+
},
292+
)?;
288293
}
289294

290295
// Without no new privileges, seccomp is a privileged operation. We have to
@@ -872,7 +877,7 @@ fn sync_seccomp(
872877
Ok(())
873878
}
874879

875-
fn setup_net_devices(
880+
fn configure_container_network_devices(
876881
net_device: &HashMap<String, LinuxNetDevice>,
877882
main_sender: &mut channel::MainSender,
878883
init_receiver: &mut channel::InitReceiver,
@@ -882,8 +887,45 @@ fn setup_net_devices(
882887
let addrs_map = init_receiver.wait_for_move_network_device()?;
883888
for (name, net_dev) in net_device {
884889
if let Some(serialize_addrs) = addrs_map.get(name) {
885-
setup_network_device(name, net_dev, serialize_addrs.clone()).map_err(|err| {
886-
tracing::error!(?err, "failed to setup_network_device");
890+
// Get the device's final name (use configured name if provided, otherwise use original name)
891+
let new_name = net_dev
892+
.name()
893+
.as_ref()
894+
.filter(|d| !d.is_empty())
895+
.map_or(name.as_str(), |d| d);
896+
897+
// Create network clients
898+
let mut link_client = LinkClient::new(create_network_client()).map_err(|err| {
899+
tracing::error!(?err, "failed to create link client");
900+
err
901+
})?;
902+
let mut addr_client = AddressClient::new(create_network_client()).map_err(|err| {
903+
tracing::error!(?err, "failed to create address client");
904+
err
905+
})?;
906+
907+
// Get the device index
908+
let ns_link = link_client.get_by_name(new_name).map_err(|err| {
909+
tracing::error!(?err, "failed to get device by name: {}", new_name);
910+
err
911+
})?;
912+
let ns_index = ns_link.header.index;
913+
914+
// Assign IP addresses to the device
915+
setup_addresses_in_namespace(
916+
serialize_addrs.clone(),
917+
new_name,
918+
ns_index,
919+
&mut addr_client,
920+
)
921+
.map_err(|err| {
922+
tracing::error!(?err, "failed to setup addresses for device: {}", new_name);
923+
err
924+
})?;
925+
926+
// Bring the device up
927+
link_client.set_up(ns_index).map_err(|err| {
928+
tracing::error!(?err, "failed to bring up device: {}", new_name);
887929
err
888930
})?;
889931
}

0 commit comments

Comments
 (0)