Skip to content

Commit

Permalink
agama-server: add tun/tap connection config
Browse files Browse the repository at this point in the history
  • Loading branch information
cfconrad committed Jun 20, 2024
1 parent a5caf80 commit 8b1ed9c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rust/agama-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ pub enum ConnectionConfig {
Vlan(VlanConfig),
Bridge(BridgeConfig),
Infiniband(InfinibandConfig),
Tun(TunConfig),
}

#[derive(Default, Debug, PartialEq, Clone, Serialize)]
Expand Down Expand Up @@ -1313,6 +1314,22 @@ impl fmt::Display for InfinibandTransportMode {
}
}


#[derive(Default, Debug, PartialEq, Clone, Serialize)]
pub enum TunMode {
#[default]
Tun = 1,
Tap = 2,
}

#[derive(Default, Debug, PartialEq, Clone, Serialize)]
pub struct TunConfig {
pub mode: TunMode,
pub group: Option<String>,
pub owner: Option<String>,
}


/// Represents a network change.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down
50 changes: 50 additions & 0 deletions rust/agama-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const VLAN_KEY: &str = "vlan";
const BRIDGE_KEY: &str = "bridge";
const BRIDGE_PORT_KEY: &str = "bridge-port";
const INFINIBAND_KEY: &str = "infiniband";
const TUN_KEY: &str = "tun";

/// Converts a connection struct into a HashMap that can be sent over D-Bus.
///
Expand Down Expand Up @@ -122,6 +123,10 @@ pub fn connection_to_dbus<'a>(
ConnectionConfig::Loopback => {
connection_dbus.insert("type", LOOPBACK_KEY.into());
}
ConnectionConfig::Tun(tun) => {
connection_dbus.insert("type", TUN_KEY.into());
result.insert(TUN_KEY, tun_config_to_dbus(tun));
}
_ => {}
}

Expand Down Expand Up @@ -171,6 +176,11 @@ pub fn connection_from_dbus(conn: OwnedNestedHash) -> Option<Connection> {
return Some(connection);
}

if let Some(tun_config) = tun_config_from_dbus(&conn) {
connection.config = ConnectionConfig::Tun(tun_config);
return Some(connection);
}

if conn.contains_key(DUMMY_KEY) {
connection.config = ConnectionConfig::Dummy;
return Some(connection);
Expand Down Expand Up @@ -532,6 +542,46 @@ fn infiniband_config_from_dbus(conn: &OwnedNestedHash) -> Option<InfinibandConfi
Some(infiniband_config)
}

fn tun_config_to_dbus(config: &TunConfig) -> HashMap<&str, zvariant::Value> {
let mut tun_config: HashMap<&str, zvariant::Value> = HashMap::from([
("mode", Value::new(config.mode.clone() as u32)),
]);

if let Some(group) = &config.group {
tun_config.insert("group", group.into());
}

if let Some(owner) = &config.owner {
tun_config.insert("owner", owner.into());
}

tun_config
}

fn tun_config_from_dbus(conn: &OwnedNestedHash) -> Option<TunConfig> {
let tun = conn.get(TUN_KEY)?;

let mut tun_config = TunConfig::default();


if let Some(mode) = tun.get("mode") {
tun_config.mode = match mode.downcast_ref::<u32>()? {
2 => TunMode::Tap,
_ => TunMode::Tun,
}
}

if let Some(group) = tun.get("group") {
tun_config.group = Some(group.downcast_ref::<str>()?.to_string());
}

if let Some(owner) = tun.get("owner") {
tun_config.owner = Some(owner.downcast_ref::<str>()?.to_string());
}

Some(tun_config)
}

/// Converts a MatchConfig struct into a HashMap that can be sent over D-Bus.
///
/// * `match_config`: MatchConfig to convert.
Expand Down

0 comments on commit 8b1ed9c

Please sign in to comment.