Skip to content

Commit

Permalink
rust: Migrate zbus v1 to V5
Browse files Browse the repository at this point in the history
Fedora 42 is removing support of zbus V1 and we are suggested to use V4
or V5.

The zbus V4 does not have Clone trait for OwnedValue which require
massive changes to our code base. Hence using zbus V5.

The zbus V5 need at least 1.77 to compile as it use a nightly feature of
rust rust-lang/rust#91345 which only become
stable in rust 1.77.

It is impossible to compile this project in Rust 1.66 now, removing
CI test for compiling on Rust 1.66. Downstream release should
considering using Rust 1.80+ to compile this project.

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Dec 13, 2024
1 parent f858dd9 commit c159f85
Show file tree
Hide file tree
Showing 46 changed files with 770 additions and 590 deletions.
21 changes: 0 additions & 21 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,6 @@ jobs:
path: test_artifacts/
retention-days: 5

build_on_rust_1_66:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4

- name: Install Rust 1.66
run: rustup install 1.66

- name: Use rust vendoring
run: |
cd rust
wget -qO- https://github.com/nmstate/nmstate/releases/download/v2.2.36/nmstate-vendor-2.2.36-rust-1.66.tar.xz | tar xJ
mkdir .cargo
echo '[source.crates-io]' > .cargo/config.toml
echo 'replace-with = "vendored-sources"' >> .cargo/config.toml
echo '[source.vendored-sources]' >> .cargo/config.toml
echo 'directory = "vendor"' >> .cargo/config.toml
- name: Build on rust 1.66
run: cd rust && cargo +1.66 build --ignore-rust-version

macos_gen_conf_build:
strategy:
fail-fast: true
Expand Down
7 changes: 5 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ members = [
"src/lib",
]

[workspace.package]
rust-version = "1.80"

[workspace.dependencies]
log = "0.4.17"
serde_yaml = "0.9"
Expand All @@ -15,8 +18,8 @@ nmstate = { path = "src/lib", version = "2.2", default-features = false }
nispor = "1.2.21"
uuid = { version = "1.1 ", default-features = false, features = ["v4"] }
nix = { version = "0.26.2", default-features = false, features = ["feature", "hostname"] }
zbus = { version = "1.9.2", default-features = false}
zvariant = {version = "2.10.0", default-features = false}
zbus = { version = "5.1.0", default-features = false, features = ["tokio"] }
zvariant = {version = "5.1.0", default-features = false}
libc = "0.2.74"
once_cell = "1.12.0"
env_logger = "0.10.0"
Expand Down
2 changes: 1 addition & 1 deletion rust/src/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/nmstate/nmstate"
keywords = ["network", "linux"]
categories = ["network-programming"]
edition = "2021"
rust-version = "1.66"
rust-version.workspace = true

[[bin]]
name = "nmstatectl"
Expand Down
2 changes: 1 addition & 1 deletion rust/src/clib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "2.2.40"
authors = ["Gris Ge <[email protected]>"]
license = "Apache-2.0"
edition = "2021"
rust-version = "1.66"
rust-version.workspace = true
build = "build.rs"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion rust/src/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://nmstate.io"
repository = "https://github.com/nmstate/nmstate"
keywords = ["network", "linux"]
categories = ["network-programming", "os::linux-apis"]
rust-version = "1.66"
rust-version.workspace = true
edition = "2021"

[lib]
Expand Down
24 changes: 15 additions & 9 deletions rust/src/lib/nm/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,50 @@ use crate::{nm::error::nm_error_to_nmstate, NmstateError};
// Wait maximum 60 seconds for rollback
pub(crate) const CHECKPOINT_ROLLBACK_TIMEOUT: u32 = 60;

pub(crate) fn nm_checkpoint_create(
pub(crate) async fn nm_checkpoint_create(
timeout: u32,
) -> Result<String, NmstateError> {
let mut nm_api = NmApi::new().map_err(nm_error_to_nmstate)?;
let mut nm_api = NmApi::new().await.map_err(nm_error_to_nmstate)?;
nm_api
.checkpoint_create(timeout)
.await
.map_err(nm_error_to_nmstate)
}

pub(crate) fn nm_checkpoint_rollback(
pub(crate) async fn nm_checkpoint_rollback(
checkpoint: &str,
) -> Result<(), NmstateError> {
let mut nm_api = NmApi::new().map_err(nm_error_to_nmstate)?;
let mut nm_api = NmApi::new().await.map_err(nm_error_to_nmstate)?;
nm_api
.checkpoint_rollback(checkpoint)
.await
.map_err(nm_error_to_nmstate)?;
if let Err(e) = nm_api.wait_checkpoint_rollback(CHECKPOINT_ROLLBACK_TIMEOUT)
if let Err(e) = nm_api
.wait_checkpoint_rollback(CHECKPOINT_ROLLBACK_TIMEOUT)
.await
{
warn!("{}", e);
}
Ok(())
}

pub(crate) fn nm_checkpoint_destroy(
pub(crate) async fn nm_checkpoint_destroy(
checkpoint: &str,
) -> Result<(), NmstateError> {
let mut nm_api = NmApi::new().map_err(nm_error_to_nmstate)?;
let mut nm_api = NmApi::new().await.map_err(nm_error_to_nmstate)?;
nm_api
.checkpoint_destroy(checkpoint)
.await
.map_err(nm_error_to_nmstate)
}

pub(crate) fn nm_checkpoint_timeout_extend(
pub(crate) async fn nm_checkpoint_timeout_extend(
checkpoint: &str,
added_time_sec: u32,
) -> Result<(), NmstateError> {
let nm_api = NmApi::new().map_err(nm_error_to_nmstate)?;
let nm_api = NmApi::new().await.map_err(nm_error_to_nmstate)?;
nm_api
.checkpoint_timeout_extend(checkpoint, added_time_sec)
.await
.map_err(nm_error_to_nmstate)
}
36 changes: 22 additions & 14 deletions rust/src/lib/nm/nm_dbus/active_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct NmActiveConnection {
}

#[cfg(feature = "query_apply")]
fn nm_ac_obj_path_state_flags_get(
async fn nm_ac_obj_path_state_flags_get(
dbus_conn: &zbus::Connection,
obj_path: &str,
) -> Result<u32, NmError> {
Expand All @@ -28,8 +28,9 @@ fn nm_ac_obj_path_state_flags_get(
NM_DBUS_INTERFACE_ROOT,
obj_path,
NM_DBUS_INTERFACE_AC,
)?;
match proxy.get_property::<u32>("StateFlags") {
)
.await?;
match proxy.get_property::<u32>("StateFlags").await {
Ok(uuid) => Ok(uuid),
Err(e) => Err(NmError::new(
ErrorKind::Bug,
Expand All @@ -41,7 +42,7 @@ fn nm_ac_obj_path_state_flags_get(
}

#[cfg(feature = "query_apply")]
pub(crate) fn nm_ac_obj_path_uuid_get(
pub(crate) async fn nm_ac_obj_path_uuid_get(
dbus_conn: &zbus::Connection,
obj_path: &str,
) -> Result<String, NmError> {
Expand All @@ -50,8 +51,9 @@ pub(crate) fn nm_ac_obj_path_uuid_get(
NM_DBUS_INTERFACE_ROOT,
obj_path,
NM_DBUS_INTERFACE_AC,
)?;
match proxy.get_property::<String>("Uuid") {
)
.await?;
match proxy.get_property::<String>("Uuid").await {
Ok(uuid) => Ok(uuid),
Err(e) => Err(NmError::new(
ErrorKind::Bug,
Expand All @@ -63,7 +65,7 @@ pub(crate) fn nm_ac_obj_path_uuid_get(
}

#[cfg(feature = "query_apply")]
fn nm_ac_obj_path_nm_con_obj_path_get(
async fn nm_ac_obj_path_nm_con_obj_path_get(
dbus_conn: &zbus::Connection,
obj_path: &str,
) -> Result<String, NmError> {
Expand All @@ -72,8 +74,12 @@ fn nm_ac_obj_path_nm_con_obj_path_get(
NM_DBUS_INTERFACE_ROOT,
obj_path,
NM_DBUS_INTERFACE_AC,
)?;
match proxy.get_property::<zvariant::OwnedObjectPath>("Connection") {
)
.await?;
match proxy
.get_property::<zvariant::OwnedObjectPath>("Connection")
.await
{
Ok(p) => Ok(obj_path_to_string(p)),
// Sometimes the Active Connection is deleting or deactivating which
// does not have connection associated, we return "" in this case
Expand All @@ -82,27 +88,29 @@ fn nm_ac_obj_path_nm_con_obj_path_get(
}

#[cfg(feature = "query_apply")]
pub(crate) fn get_nm_ac_by_obj_path(
pub(crate) async fn get_nm_ac_by_obj_path(
connection: &zbus::Connection,
obj_path: &str,
) -> Result<Option<NmActiveConnection>, NmError> {
// Sometimes the Active Connection is deleting or deactivating which
// does not have connection associated, we return None in this case
let nm_conn_obj_path =
nm_ac_obj_path_nm_con_obj_path_get(connection, obj_path)?;
nm_ac_obj_path_nm_con_obj_path_get(connection, obj_path).await?;

if (!nm_conn_obj_path.is_empty()) && nm_conn_obj_path != "/" {
let nm_conn = nm_con_get_from_obj_path(connection, &nm_conn_obj_path)?;
let nm_conn =
nm_con_get_from_obj_path(connection, &nm_conn_obj_path).await?;
let iface_name = match nm_conn.iface_name() {
Some(i) => i.to_string(),
None => "".to_string(),
};
let iface_type = nm_conn.iface_type().cloned().unwrap_or_default();
Ok(Some(NmActiveConnection {
uuid: nm_ac_obj_path_uuid_get(connection, obj_path)?,
uuid: nm_ac_obj_path_uuid_get(connection, obj_path).await?,
iface_name,
iface_type,
state_flags: nm_ac_obj_path_state_flags_get(connection, obj_path)?,
state_flags: nm_ac_obj_path_state_flags_get(connection, obj_path)
.await?,
}))
} else {
Ok(None)
Expand Down
6 changes: 3 additions & 3 deletions rust/src/lib/nm/nm_dbus/connection/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
use std::collections::HashMap;
use std::convert::TryFrom;

use serde::Deserialize;
use serde::{Deserialize, Serialize};
use zvariant::Value;

use super::super::{connection::DbusDictionary, NmError, ToDbusValue};

#[derive(Debug, Clone, PartialEq, Default, Deserialize)]
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
#[serde(try_from = "DbusDictionary")]
#[non_exhaustive]
pub struct NmSettingBond {
Expand Down Expand Up @@ -44,7 +44,7 @@ impl ToDbusValue for NmSettingBond {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NmSettingBondPort {
pub priority: Option<i32>,
Expand Down
23 changes: 10 additions & 13 deletions rust/src/lib/nm/nm_dbus/connection/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
use std::collections::HashMap;
use std::convert::TryFrom;

use serde::Deserialize;
use serde::{Deserialize, Serialize};

use super::super::{
connection::DbusDictionary,
connection::{DbusDictionary, DBUS_ASV_SIGNATURE},
convert::{
mac_str_to_u8_array, own_value_to_bytes_array, u8_array_to_mac_string,
},
NmError, NmVlanProtocol, ToDbusValue,
};

#[derive(Debug, Clone, PartialEq, Default, Deserialize)]
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
#[serde(try_from = "DbusDictionary")]
#[non_exhaustive]
pub struct NmSettingBridge {
Expand Down Expand Up @@ -273,9 +273,8 @@ impl ToDbusValue for NmSettingBridge {
ret.insert("vlan-stats-enabled", zvariant::Value::new(v));
}
if let Some(vlans) = &self.vlans {
let mut vlan_values = zvariant::Array::new(
zvariant::Signature::from_str_unchecked("a{sv}"),
);
let mut vlan_values = zvariant::Array::new(DBUS_ASV_SIGNATURE);

for vlan in vlans {
vlan_values.append(vlan.to_value()?)?;
}
Expand All @@ -288,7 +287,7 @@ impl ToDbusValue for NmSettingBridge {
}
}

#[derive(Debug, Clone, PartialEq, Default, Deserialize)]
#[derive(Debug, Clone, PartialEq, Default, Deserialize, Serialize)]
#[serde(try_from = "DbusDictionary")]
#[non_exhaustive]
pub struct NmSettingBridgeVlanRange {
Expand Down Expand Up @@ -318,8 +317,8 @@ impl TryFrom<DbusDictionary> for NmSettingBridgeVlanRange {
impl NmSettingBridgeVlanRange {
fn to_value(&self) -> Result<zvariant::Value, NmError> {
let mut ret = zvariant::Dict::new(
zvariant::Signature::from_str_unchecked("s"),
zvariant::Signature::from_str_unchecked("v"),
&zvariant::Signature::Str,
&zvariant::Signature::Variant,
);
ret.append(
zvariant::Value::new("vid-start"),
Expand All @@ -341,7 +340,7 @@ impl NmSettingBridgeVlanRange {
}
}

#[derive(Debug, Clone, PartialEq, Default)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NmSettingBridgePort {
pub hairpin_mode: Option<bool>,
Expand Down Expand Up @@ -376,9 +375,7 @@ impl ToDbusValue for NmSettingBridgePort {
.map(|v| ret.insert("priority", zvariant::Value::new(v)));

if let Some(vlans) = self.vlans.as_ref() {
let mut vlan_values = zvariant::Array::new(
zvariant::Signature::from_str_unchecked("a{sv}"),
);
let mut vlan_values = zvariant::Array::new(DBUS_ASV_SIGNATURE);
for vlan in vlans {
vlan_values.append(vlan.to_value()?)?;
}
Expand Down
Loading

0 comments on commit c159f85

Please sign in to comment.