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

Add support to assign an interface or match settings to a connection #723

Merged
merged 12 commits into from
Sep 13, 2023
137 changes: 135 additions & 2 deletions rust/agama-dbus-server/src/network/dbus/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,41 @@ impl Connections {
///
/// It offers an API to query a connection.
pub struct Connection {
actions: Arc<Mutex<Sender<Action>>>,
connection: Arc<Mutex<NetworkConnection>>,
}

impl Connection {
/// Creates a Connection interface object.
///
/// * `actions`: sending-half of a channel to send actions.
/// * `connection`: connection to expose over D-Bus.
pub fn new(connection: Arc<Mutex<NetworkConnection>>) -> Self {
Self { connection }
pub fn new(actions: Sender<Action>, connection: Arc<Mutex<NetworkConnection>>) -> Self {
Self {
actions: Arc::new(Mutex::new(actions)),
connection,
}
}

/// Returns the underlying connection.
async fn get_connection(&self) -> MutexGuard<NetworkConnection> {
self.connection.lock().await
}

/// Updates the connection data in the NetworkSystem.
///
/// * `connection`: Updated connection.
async fn update_connection<'a>(
&self,
connection: MutexGuard<'a, NetworkConnection>,
) -> zbus::fdo::Result<()> {
let actions = self.actions.lock().await;
actions
.send(Action::UpdateConnection(connection.clone()))
.await
.unwrap();
Ok(())
}
}

#[dbus_interface(name = "org.opensuse.Agama1.Network.Connection")]
Expand All @@ -205,6 +225,119 @@ impl Connection {
pub async fn id(&self) -> String {
self.get_connection().await.id().to_string()
}

#[dbus_interface(property)]
pub async fn interface(&self) -> String {
self.get_connection().await.interface().to_string()
}

#[dbus_interface(property)]
pub async fn set_interface(&mut self, name: &str) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
connection.set_interface(name);
self.update_connection(connection).await
}
}

/// D-Bus interface for Match settings
pub struct Match {
actions: Arc<Mutex<Sender<Action>>>,
connection: Arc<Mutex<NetworkConnection>>,
}

impl Match {
/// Creates a Match Settings interface object.
///
/// * `actions`: sending-half of a channel to send actions.
/// * `connection`: connection to expose over D-Bus.
pub fn new(actions: Sender<Action>, connection: Arc<Mutex<NetworkConnection>>) -> Self {
Self {
actions: Arc::new(Mutex::new(actions)),
connection,
}
}

/// Returns the underlying connection.
async fn get_connection(&self) -> MutexGuard<NetworkConnection> {
self.connection.lock().await
}

/// Updates the connection data in the NetworkSystem.
///
/// * `connection`: Updated connection.
async fn update_connection<'a>(
&self,
connection: MutexGuard<'a, NetworkConnection>,
) -> zbus::fdo::Result<()> {
let actions = self.actions.lock().await;
actions
.send(Action::UpdateConnection(connection.clone()))
.await
.unwrap();
Ok(())
}
}

#[dbus_interface(name = "org.opensuse.Agama1.Network.Connection.Match")]
impl Match {
/// List of driver
#[dbus_interface(property)]
pub async fn driver(&self) -> Vec<String> {
let connection = self.get_connection().await;
connection.match_config().driver.clone()
}

#[dbus_interface(property)]
pub async fn set_driver(&mut self, driver: Vec<String>) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
let config = connection.match_config_mut();
config.driver = driver;
self.update_connection(connection).await
}

/// List of paths
#[dbus_interface(property)]
pub async fn path(&self) -> Vec<String> {
let connection = self.get_connection().await;
connection.match_config().path.clone()
}

#[dbus_interface(property)]
pub async fn set_path(&mut self, path: Vec<String>) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
let config = connection.match_config_mut();
config.path = path;
self.update_connection(connection).await
}
/// List of driver
#[dbus_interface(property)]
pub async fn interface(&self) -> Vec<String> {
let connection = self.get_connection().await;
connection.match_config().interface.clone()
}

#[dbus_interface(property)]
pub async fn set_interface(&mut self, interface: Vec<String>) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
let config = connection.match_config_mut();
config.interface = interface;
self.update_connection(connection).await
}

/// List of kernel options
#[dbus_interface(property)]
pub async fn kernel(&self) -> Vec<String> {
let connection = self.get_connection().await;
connection.match_config().kernel.clone()
}

#[dbus_interface(property)]
pub async fn set_kernel(&mut self, kernel: Vec<String>) -> zbus::fdo::Result<()> {
let mut connection = self.get_connection().await;
let config = connection.match_config_mut();
config.kernel = kernel;
self.update_connection(connection).await
}
}

/// D-Bus interface for IPv4 settings
Expand Down
12 changes: 10 additions & 2 deletions rust/agama-dbus-server/src/network/dbus/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,23 @@ impl Tree {
log::info!("Publishing network connection '{}'", id);

let cloned = Arc::new(Mutex::new(conn.clone()));
self.add_interface(&path, interfaces::Connection::new(Arc::clone(&cloned)))
.await?;
self.add_interface(
&path,
interfaces::Connection::new(self.actions.clone(), Arc::clone(&cloned)),
)
.await?;

self.add_interface(
&path,
interfaces::Ipv4::new(self.actions.clone(), Arc::clone(&cloned)),
)
.await?;

self.add_interface(
&path,
interfaces::Match::new(self.actions.clone(), Arc::clone(&cloned)),
)
.await?;
if let Connection::Wireless(_) = conn {
self.add_interface(
&path,
Expand Down
26 changes: 26 additions & 0 deletions rust/agama-dbus-server/src/network/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ impl Connection {
self.base_mut().id = id.to_string()
}

pub fn interface(&self) -> &str {
self.base().interface.as_str()
}

pub fn set_interface(&mut self, interface: &str) {
self.base_mut().interface = interface.to_string()
}

pub fn uuid(&self) -> Uuid {
self.base().uuid
}
Expand All @@ -274,6 +282,14 @@ impl Connection {
&mut self.base_mut().ipv4
}

pub fn match_config(&self) -> &MatchConfig {
&self.base().match_config
}

pub fn match_config_mut(&mut self) -> &mut MatchConfig {
&mut self.base_mut().match_config
}

pub fn remove(&mut self) {
self.base_mut().status = Status::Removed;
}
Expand All @@ -294,6 +310,8 @@ pub struct BaseConnection {
pub uuid: Uuid,
pub ipv4: Ipv4Config,
pub status: Status,
pub interface: String,
pub match_config: MatchConfig,
}

impl PartialEq for BaseConnection {
Expand All @@ -317,6 +335,14 @@ pub struct Ipv4Config {
pub gateway: Option<Ipv4Addr>,
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct MatchConfig {
pub driver: Vec<String>,
pub interface: Vec<String>,
pub path: Vec<String>,
pub kernel: Vec<String>,
}

#[derive(Debug, Error)]
#[error("Unknown IP configuration method name: {0}")]
pub struct UnknownIpMethod(String);
Expand Down
Loading
Loading