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

Wireless migration #69

Merged
merged 4 commits into from
Apr 8, 2024
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
1 change: 1 addition & 0 deletions rust/migrate-wicked/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/migrate-wicked/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tokio = { version = "1.33.0", features = ["macros", "rt-multi-thread"] }
serde_ignored = "0.1.9"
uuid = { version = "1.3.4", features = ["v4"] }
async-trait = "0.1.77"
macaddr = "1.0"

[[bin]]
name = "migrate-wicked"
Expand Down
5 changes: 3 additions & 2 deletions rust/migrate-wicked/src/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,15 @@ mod tests {
..Default::default()
};

let connection: model::Connection = bond_interface.to_connection().unwrap().connection;
let connection: &model::Connection =
&bond_interface.to_connection().unwrap().connections[0];
assert!(matches!(
connection.config,
model::ConnectionConfig::Bond(_)
));
assert_eq!(connection.mac_address.to_string(), "02:11:22:33:44:55");

if let model::ConnectionConfig::Bond(bond) = connection.config {
if let model::ConnectionConfig::Bond(bond) = &connection.config {
assert_eq!(bond.mode, AgamaBondMode::LACP);
let s = HashMap::from([
("xmit_hash_policy", String::from("encap34")),
Expand Down
54 changes: 40 additions & 14 deletions rust/migrate-wicked/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::bond::Bond;
use crate::bridge::Bridge;
use crate::vlan::Vlan;
use crate::wireless::Wireless;
use crate::MIGRATION_SETTINGS;
use agama_dbus_server::network::model::{
self, IpConfig, IpRoute, Ipv4Method, Ipv6Method, MacAddress,
Expand Down Expand Up @@ -29,6 +30,8 @@ pub struct Interface {
pub dummy: Option<Dummy>,
pub ethernet: Option<Ethernet>,
pub bond: Option<Bond>,
#[serde(skip_serializing_if = "Option::is_none")]
pub wireless: Option<Wireless>,
#[serde(rename = "@origin")]
pub origin: String,
pub vlan: Option<Vlan>,
Expand Down Expand Up @@ -118,7 +121,7 @@ pub struct Nexthop {
}

pub struct ConnectionResult {
pub connection: model::Connection,
pub connections: Vec<model::Connection>,
pub warnings: Vec<anyhow::Error>,
}

Expand All @@ -129,6 +132,7 @@ pub struct IpConfigResult {

impl Interface {
pub fn to_connection(&self) -> Result<ConnectionResult, anyhow::Error> {
let settings = MIGRATION_SETTINGS.get().unwrap();
let ip_config = self.to_ip_config()?;
let mut connection = model::Connection {
id: self.name.clone(),
Expand All @@ -137,32 +141,52 @@ impl Interface {
status: model::Status::Down,
..Default::default()
};
let mut connections: Vec<model::Connection> = vec![];

if let Some(settings) = MIGRATION_SETTINGS.get() {
if settings.activate_connections {
connection.status = model::Status::Up;
}
if settings.activate_connections {
connection.status = model::Status::Up;
}

if let Some(ethernet) = &self.ethernet {
connection.mac_address = MacAddress::try_from(&ethernet.address)?;
connection.config = model::ConnectionConfig::Ethernet
connection.config = model::ConnectionConfig::Ethernet;
connections.push(connection);
} else if let Some(dummy) = &self.dummy {
connection.mac_address = MacAddress::try_from(&dummy.address)?;
connection.config = model::ConnectionConfig::Dummy
connection.config = model::ConnectionConfig::Dummy;
connections.push(connection);
} else if let Some(bond) = &self.bond {
connection.mac_address = MacAddress::try_from(&bond.address)?;
connection.config = bond.into()
connection.config = bond.into();
connections.push(connection);
} else if let Some(vlan) = &self.vlan {
connection.mac_address = MacAddress::try_from(&vlan.address)?;
connection.config = vlan.into()
connection.config = vlan.into();
connections.push(connection);
} else if let Some(bridge) = &self.bridge {
connection.mac_address = MacAddress::try_from(&bridge.address)?;
connection.config = bridge.into();
connections.push(connection);
} else if let Some(wireless) = &self.wireless {
if let Some(networks) = &wireless.networks {
if networks.len() > 1 {
log::info!("{} has multiple networks defined, these will be split into different connections in NM", connection.id);
}
for (i, network) in networks.iter().enumerate() {
let mut wireless_connection = connection.clone();
if networks.len() > 1 {
wireless_connection.id.push_str(&format!("-{}", i));
}
wireless_connection.config = network.try_into()?;
connections.push(wireless_connection);
}
}
} else {
connections.push(connection);
}

Ok(ConnectionResult {
connection,
connections,
warnings: ip_config.warnings,
})
}
Expand Down Expand Up @@ -337,7 +361,7 @@ mod tests {
};

let static_connection: model::Connection =
static_interface.to_connection().unwrap().connection;
static_interface.to_connection().unwrap().connections[0].to_owned();
assert_eq!(static_connection.ip_config.method4, Ipv4Method::Manual);
assert_eq!(
static_connection.ip_config.addresses[0].to_string(),
Expand Down Expand Up @@ -393,7 +417,7 @@ mod tests {
};

let static_connection: model::Connection =
static_interface.to_connection().unwrap().connection;
static_interface.to_connection().unwrap().connections[0].to_owned();
assert_eq!(static_connection.ip_config.method4, Ipv4Method::Auto);
assert_eq!(static_connection.ip_config.method6, Ipv6Method::Auto);
assert_eq!(static_connection.ip_config.addresses.len(), 0);
Expand All @@ -409,7 +433,8 @@ mod tests {
..Default::default()
};

let connection: model::Connection = dummy_interface.to_connection().unwrap().connection;
let connection: &model::Connection =
&dummy_interface.to_connection().unwrap().connections[0];
assert!(matches!(connection.config, model::ConnectionConfig::Dummy));
assert_eq!(connection.mac_address.to_string(), "12:34:56:78:9A:BC");

Expand All @@ -420,7 +445,8 @@ mod tests {
..Default::default()
};

let connection: model::Connection = dummy_interface.to_connection().unwrap().connection;
let connection: &model::Connection =
&dummy_interface.to_connection().unwrap().connections[0];
assert!(matches!(connection.config, model::ConnectionConfig::Dummy));
assert_eq!(dummy_interface.dummy.unwrap().address, None);
assert!(matches!(connection.mac_address, MacAddress::Unset));
Expand Down
1 change: 1 addition & 0 deletions rust/migrate-wicked/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod interface;
mod migrate;
mod reader;
mod vlan;
mod wireless;

use clap::builder::TypedValueParser;
use clap::{Args, Parser, Subcommand};
Expand Down
18 changes: 10 additions & 8 deletions rust/migrate-wicked/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,23 @@ impl Adapter for WickedAdapter {
if !settings.continue_migration {
return Err(anyhow::anyhow!(
"Migration of {} failed",
connection_result.connection.id
connection_result.connections[0].id
)
.into());
}
}

if let Some(parent) = interface.link.master {
parents.insert(connection_result.connection.id.clone(), parent.clone());
}
if let Some(bridge) = interface.bridge {
for port in bridge.ports {
bridge_ports.insert(port.device.clone(), port.clone());
for connection in connection_result.connections {
if let Some(parent) = &interface.link.master {
parents.insert(connection.id.clone(), parent.clone());
}
state.add_connection(connection)?;
if let Some(bridge) = &interface.bridge {
for port in &bridge.ports {
bridge_ports.insert(port.device.clone(), port.clone());
}
}
}
state.add_connection(connection_result.connection)?;
}

update_parent_connection(&mut state, parents)?;
Expand Down
4 changes: 2 additions & 2 deletions rust/migrate-wicked/src/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ mod tests {
let ifc = vlan_interface.to_connection();

assert!(ifc.is_ok());
let ifc = ifc.unwrap().connection;
let ifc = &ifc.unwrap().connections[0];
assert!(matches!(ifc.config, model::ConnectionConfig::Vlan(_)));
if let model::ConnectionConfig::Vlan(v) = ifc.config {
if let model::ConnectionConfig::Vlan(v) = &ifc.config {
assert_eq!(v.id, 10);
assert_eq!(v.protocol, model::VlanProtocol::IEEE802_1ad);
assert_eq!(v.parent, "en0");
Expand Down
Loading
Loading