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

Revert current Bond approach in favor of upcoming upstream https://github.com/openSUSE/agama/pull/885 #44

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
2 changes: 0 additions & 2 deletions rust/agama-dbus-server/src/network/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub enum NetworkStateError {
InvalidIpMethod(u8),
#[error("Invalid wireless mode: '{0}'")]
InvalidWirelessMode(String),
#[error("Unknown parent kind '{0}'")]
UnknownParentKind(String),
#[error("Connection '{0}' already exists")]
ConnectionExists(Uuid),
#[error("Invalid security wireless protocol: '{0}'")]
Expand Down
109 changes: 0 additions & 109 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,27 +206,6 @@ mod tests {
let conn = Connection::Loopback(LoopbackConnection { base });
assert!(conn.is_loopback());
}

#[test]
fn test_mac_addr() {
assert_eq!(
MacAddr::try_from("11:22:33:44:55:66"),
Ok(MacAddr {
data: [0x11, 0x22, 0x33, 0x44, 0x55, 0x66]
})
);
assert_eq!(
MacAddr::try_from("11-22-33-44-55-66"),
Ok(MacAddr {
data: [0x11, 0x22, 0x33, 0x44, 0x55, 0x66]
})
);
assert_eq!(
MacAddr::try_from("a-b-c-d-e-f").unwrap().to_string(),
String::from("0a:0b:0c:0d:0e:0f")
);
assert!(MacAddr::try_from("invalid-mac-addr").is_err());
}
}

/// Network device
Expand All @@ -242,7 +221,6 @@ pub enum Connection {
Ethernet(EthernetConnection),
Wireless(WirelessConnection),
Loopback(LoopbackConnection),
Bond(BondConnection),
}

impl Connection {
Expand All @@ -258,10 +236,6 @@ impl Connection {
}),
DeviceType::Loopback => Connection::Loopback(LoopbackConnection { base }),
DeviceType::Ethernet => Connection::Ethernet(EthernetConnection { base }),
DeviceType::Bond => Connection::Bond(BondConnection {
base,
..Default::default()
}),
}
}

Expand All @@ -272,7 +246,6 @@ impl Connection {
Connection::Ethernet(conn) => &conn.base,
Connection::Wireless(conn) => &conn.base,
Connection::Loopback(conn) => &conn.base,
Connection::Bond(conn) => &conn.base,
}
}

Expand All @@ -281,7 +254,6 @@ impl Connection {
Connection::Ethernet(conn) => &mut conn.base,
Connection::Wireless(conn) => &mut conn.base,
Connection::Loopback(conn) => &mut conn.base,
Connection::Bond(conn) => &mut conn.base,
}
}

Expand Down Expand Up @@ -336,37 +308,6 @@ impl Connection {
}
}

#[derive(Debug, PartialEq, Clone)]
pub enum ParentKind {
Bond,
}

impl fmt::Display for ParentKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match &self {
ParentKind::Bond => "bond",
};
write!(f, "{}", name)
}
}

impl FromStr for ParentKind {
type Err = NetworkStateError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"bond" => Ok(ParentKind::Bond),
_ => Err(NetworkStateError::UnknownParentKind(s.to_string())),
}
}
}

#[derive(Debug, Clone)]
pub struct Parent {
pub interface: String,
pub kind: ParentKind,
}

#[derive(Debug, Default, Clone)]
pub struct BaseConnection {
pub id: String,
Expand All @@ -375,7 +316,6 @@ pub struct BaseConnection {
pub status: Status,
pub interface: String,
pub match_config: MatchConfig,
pub parent: Option<Parent>,
}

impl PartialEq for BaseConnection {
Expand Down Expand Up @@ -540,55 +480,6 @@ pub struct LoopbackConnection {
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct BondConnection {
pub base: BaseConnection,
pub bond: BondConfig,
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct MacAddr {
pub data: [u8; 6],
}

impl TryFrom<&str> for MacAddr {
type Error = &'static str;

fn try_from(str: &str) -> Result<Self, Self::Error> {
let mut ret: [u8; 6] = [0; 6];

let split = str.split([':', '-']);

for (i, s) in split.into_iter().enumerate() {
if i >= ret.len() {
return Err("The given string doesn't match xx:xx:xx:xx:xx:xx");
}
match u8::from_str_radix(s, 16) {
Ok(v) => ret[i] = v,
_ => return Err("Unable to parse hex number"),
};
}

Ok(MacAddr { data: ret })
}
}

impl fmt::Display for MacAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.data[0], self.data[1], self.data[2], self.data[3], self.data[4], self.data[5]
)
}
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct BondConfig {
pub options: HashMap<String, String>,
pub hwaddr: Option<MacAddr>,
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct WirelessConfig {
pub mode: WirelessMode,
pub ssid: SSID,
Expand Down
65 changes: 1 addition & 64 deletions rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use uuid::Uuid;
use zbus::zvariant::{self, OwnedValue, Value};

const ETHERNET_KEY: &str = "802-3-ethernet";
const BOND_KEY: &str = "bond";
const WIRELESS_KEY: &str = "802-11-wireless";
const WIRELESS_SECURITY_KEY: &str = "802-11-wireless-security";
const LOOPBACK_KEY: &str = "loopback";
Expand All @@ -29,34 +28,18 @@ pub fn connection_to_dbus(conn: &Connection) -> NestedHash {
("type", ETHERNET_KEY.into()),
("interface-name", conn.interface().into()),
]);
if let Some(parent) = &conn.base().parent {
connection_dbus.insert("master", parent.interface.clone().into());
connection_dbus.insert("slave-type", parent.kind.to_string().into());
}
result.insert("ipv4", ip_config_to_ipv4_dbus(conn.ip_config()));
result.insert("ipv6", ip_config_to_ipv6_dbus(conn.ip_config()));
result.insert("match", match_config_to_dbus(conn.match_config()));

if let Connection::Wireless(wireless) = conn {
connection_dbus.insert("type", WIRELESS_KEY.into());
connection_dbus.insert("type", "802-11-wireless".into());
let wireless_dbus = wireless_config_to_dbus(wireless);
for (k, v) in wireless_dbus {
result.insert(k, v);
}
}

if let Connection::Bond(bond) = conn {
connection_dbus.insert("type", BOND_KEY.into());
let bond_dbus = bond_config_to_dbus(bond);
for (k, v) in bond_dbus {
result.insert(k, v);
}
if let Some(mac) = &bond.bond.hwaddr {
let h = result.entry("802-3-ethernet").or_insert(HashMap::new());
h.insert("assigned-mac-address", mac.to_string().into());
}
}

result.insert("connection", connection_dbus);
result
}
Expand All @@ -74,13 +57,6 @@ pub fn connection_from_dbus(conn: OwnedNestedHash) -> Option<Connection> {
}));
}

if let Some(bond_config) = bond_config_from_dbus(&conn) {
return Some(Connection::Bond(BondConnection {
base,
bond: bond_config,
}));
}

if conn.get(LOOPBACK_KEY).is_some() {
return Some(Connection::Loopback(LoopbackConnection { base }));
};
Expand Down Expand Up @@ -257,14 +233,6 @@ fn wireless_config_to_dbus(conn: &WirelessConnection) -> NestedHash {
])
}

fn bond_config_to_dbus(conn: &BondConnection) -> NestedHash {
let config = &conn.bond;
let bond: HashMap<&str, zvariant::Value> =
HashMap::from([("options", Value::new(config.options.clone()))]);

NestedHash::from([("bond", bond)])
}

/// Converts a MatchConfig struct into a HashMap that can be sent over D-Bus.
///
/// * `match_config`: MatchConfig to convert.
Expand Down Expand Up @@ -502,37 +470,6 @@ fn wireless_config_from_dbus(conn: &OwnedNestedHash) -> Option<WirelessConfig> {
Some(wireless_config)
}

fn bond_hwaddr_from_dbus(conn: &OwnedNestedHash) -> Option<MacAddr> {
let Some(eth) = conn.get(ETHERNET_KEY) else {
return None;
};

let Some(mac) = eth.get("assigned-mac-address") else {
return None;
};

let mac: &str = mac.downcast_ref()?;
MacAddr::try_from(mac).ok()
}

fn bond_config_from_dbus(conn: &OwnedNestedHash) -> Option<BondConfig> {
let Some(bond) = conn.get(BOND_KEY) else {
return None;
};

if let Some(dict) = bond.get("options") {
let dict: zvariant::Dict = dict.downcast_ref::<Value>()?.try_into().unwrap();
if dict.full_signature() == "a{aa}" {
let options: HashMap<String, String> = dict.try_into().unwrap();
return Some(BondConfig {
options,
hwaddr: bond_hwaddr_from_dbus(conn),
});
}
}
None
}

/// Determines whether a value is empty.
///
/// TODO: Generalize for other kind of values, like dicts or arrays.
Expand Down
2 changes: 0 additions & 2 deletions rust/agama-lib/src/network/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub enum DeviceType {
Loopback = 0,
Ethernet = 1,
Wireless = 2,
Bond = 3,
}

#[derive(Debug, Error, PartialEq)]
Expand All @@ -44,7 +43,6 @@ impl TryFrom<u8> for DeviceType {
0 => Ok(DeviceType::Loopback),
1 => Ok(DeviceType::Ethernet),
2 => Ok(DeviceType::Wireless),
3 => Ok(DeviceType::Bond),
_ => Err(InvalidDeviceType(value)),
}
}
Expand Down
Loading