Upgrade to 1.4.3

Resovles: RHBZ#2179899

Signed-off-by: Gris Ge <fge@redhat.com>
This commit is contained in:
Gris Ge 2023-03-29 21:24:34 +08:00
parent 23dccb3516
commit f0daa6fc98
6 changed files with 11 additions and 915 deletions

3
.gitignore vendored
View File

@ -3,3 +3,6 @@ SOURCES/nmstate-vendor-1.4.2.tar.xz
SOURCES/nmstate.gpg
/nmstate-1.4.2.tar.gz
/nmstate-vendor-1.4.2.tar.xz
/nmstate-1.4.3.tar.gz
/nmstate-1.4.3.tar.gz.asc
/nmstate-vendor-1.4.3.tar.xz

View File

@ -1,66 +0,0 @@
From d7d732332e486cd8969ff4b5ef95a24cb68b5441 Mon Sep 17 00:00:00 2001
From: Gris Ge <fge@redhat.com>
Date: Mon, 27 Feb 2023 12:17:05 +0800
Subject: [PATCH] nm: Ignore error when creating profile if not desired
When a undesired interface holding `autoconf: true` and `dhcp: false`
for IPv6, nmstate will fail with error:
Autoconf without DHCP is not supported yet
This is caused by `nm/connection.py` try to create `NM.SimpleConnection`
for every interface even not desired.
This patch changed to:
* Only create new `NM.SimpleConnection` when desired or changed.
* Use current profile if exists when not desired or changed.
* Ignore error if not desired/changed.
Integration test case included.
Signed-off-by: Gris Ge <fge@redhat.com>
---
libnmstate/nm/profile.py | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/libnmstate/nm/profile.py b/libnmstate/nm/profile.py
index ad1ad19f..1119cd1a 100644
--- a/libnmstate/nm/profile.py
+++ b/libnmstate/nm/profile.py
@@ -24,6 +24,7 @@ from distutils.version import StrictVersion
import logging
import time
+from libnmstate.error import NmstateError
from libnmstate.error import NmstateInternalError
from libnmstate.error import NmstateLibnmError
from libnmstate.error import NmstateNotSupportedError
@@ -321,9 +322,22 @@ class NmProfile:
# TODO: Use applied config as base profile
# Or even better remove the base profile argument as top level
# of nmstate should provide full/merged configure.
- self._nm_simple_conn = create_new_nm_simple_conn(
- self._iface, self._nm_profile
- )
+ if self._iface.is_changed or self._iface.is_desired:
+ self._nm_simple_conn = create_new_nm_simple_conn(
+ self._iface, self._nm_profile
+ )
+ elif self._nm_profile:
+ self._nm_simple_conn = NM.SimpleConnection.new_clone(
+ self._nm_profile
+ )
+ else:
+ try:
+ self._nm_simple_conn = create_new_nm_simple_conn(
+ self._iface, self._nm_profile
+ )
+ # No error for undesired interface
+ except NmstateError:
+ pass
def save_config(self, save_to_disk):
self._check_sriov_support()
--
2.39.2

View File

@ -1,206 +0,0 @@
From d410b928c8f2a22d42d1974b62ab5b3164861184 Mon Sep 17 00:00:00 2001
From: Gris Ge <fge@redhat.com>
Date: Thu, 23 Feb 2023 13:06:01 +0800
Subject: [PATCH] nm: Fix error on SR-IOV
When SR-IOV VF naming scheme is like `ens1f0v0`, nmstate will delete
the VF NM connection when applying this state:
```yml
---
interfaces:
- name: ens1f0
type: ethernet
state: up
ethernet:
sr-iov:
total-vfs: 1
- name: ens1f0v0
type: ethernet
state: up
ipv4:
enabled: false
ipv6:
enabled: false
```
This is because `delete_other_profiles()` is checking
`self._nm_profile()` from active NM profile instead of newly created
one. The fix is using newly created profile `self._nm_simple_conn`.
We also have race problem when activating PF along with VF, PF
activation might delete VF NIC which cause VF activation failed. To
workaround that, we activate PF first via `NmProfile.ACTION_SRIOV_PF`
and wait on it before start VF activation.
Also problem found during SR-IOV investigations is we do extra
un-required modification to `NM.SettingOvsExternalIDs` even it is not
mentioned in desired. We skip overriding `NM.SettingOvsExternalIDs` when
not desired.
Existing test case can cover the use cases.
Signed-off-by: Gris Ge <fge@redhat.com>
---
libnmstate/ifaces/ifaces.py | 18 +++++++++++++++++-
libnmstate/netapplier.py | 20 +++++++++++---------
libnmstate/nm/connection.py | 2 +-
libnmstate/nm/profile.py | 12 ++++++++++--
4 files changed, 39 insertions(+), 13 deletions(-)
diff --git a/libnmstate/ifaces/ifaces.py b/libnmstate/ifaces/ifaces.py
index 828ff578..470dc0e6 100644
--- a/libnmstate/ifaces/ifaces.py
+++ b/libnmstate/ifaces/ifaces.py
@@ -157,6 +157,23 @@ class Ifaces:
def has_vf_count_change_and_missing_eth(self):
return self._has_vf_count_change() and self._has_missing_veth()
+ def has_sriov_iface(self):
+ for iface in self.all_kernel_ifaces.values():
+ if (iface.is_desired or iface.is_changed) and iface.is_up:
+ cur_iface = self._cur_kernel_ifaces.get(iface.name)
+ if (
+ cur_iface
+ and cur_iface.raw.get(Ethernet.CONFIG_SUBTREE, {}).get(
+ Ethernet.SRIOV_SUBTREE, {}
+ )
+ ) or iface.original_desire_dict.get(
+ Ethernet.CONFIG_SUBTREE, {}
+ ).get(
+ Ethernet.SRIOV_SUBTREE, {}
+ ):
+ return True
+ return False
+
def _has_vf_count_change(self):
for iface in self.all_kernel_ifaces.values():
cur_iface = self._cur_kernel_ifaces.get(iface.name)
@@ -664,7 +681,6 @@ class Ifaces:
return None
def get_cur_iface(self, iface_name, iface_type):
-
iface = self._cur_kernel_ifaces.get(iface_name)
if iface and iface_type in (None, InterfaceType.UNKNOWN, iface.type):
return iface
diff --git a/libnmstate/netapplier.py b/libnmstate/netapplier.py
index ae909126..50a70a9c 100644
--- a/libnmstate/netapplier.py
+++ b/libnmstate/netapplier.py
@@ -104,7 +104,7 @@ def apply(
pf_net_state,
verify_change,
save_to_disk,
- has_sriov_pf=True,
+ VERIFY_RETRY_COUNT_SRIOV,
)
# Refresh the current state
current_state = show_with_plugins(
@@ -120,8 +120,16 @@ def apply(
current_state,
save_to_disk,
)
+
+ if net_state.ifaces.has_sriov_iface():
+ # If SR-IOV is present, the verification timeout is being increased
+ # to avoid timeouts due to slow drivers like i40e.
+ verify_retry = VERIFY_RETRY_COUNT_SRIOV
+ else:
+ verify_retry = VERIFY_RETRY_COUNT
+
_apply_ifaces_state(
- plugins, net_state, verify_change, save_to_disk, has_sriov_pf=False
+ plugins, net_state, verify_change, save_to_disk, verify_retry
)
if commit:
destroy_checkpoints(plugins, checkpoints)
@@ -154,7 +162,7 @@ def rollback(*, checkpoint=None):
def _apply_ifaces_state(
- plugins, net_state, verify_change, save_to_disk, has_sriov_pf=False
+ plugins, net_state, verify_change, save_to_disk, verify_retry
):
for plugin in plugins:
# Do not allow plugin to modify the net_state for future verification
@@ -163,12 +171,6 @@ def _apply_ifaces_state(
verified = False
if verify_change:
- if has_sriov_pf:
- # If SR-IOV is present, the verification timeout is being increased
- # to avoid timeouts due to slow drivers like i40e.
- verify_retry = VERIFY_RETRY_COUNT_SRIOV
- else:
- verify_retry = VERIFY_RETRY_COUNT
for _ in range(verify_retry):
try:
_verify_change(plugins, net_state)
diff --git a/libnmstate/nm/connection.py b/libnmstate/nm/connection.py
index 1fbb380b..6448e372 100644
--- a/libnmstate/nm/connection.py
+++ b/libnmstate/nm/connection.py
@@ -240,7 +240,7 @@ def create_new_nm_simple_conn(iface, nm_profile):
InterfaceType.OVS_PORT,
)
or iface.type == InterfaceType.OVS_BRIDGE
- ):
+ ) and OvsDB.OVS_DB_SUBTREE in iface.original_desire_dict:
nm_setting = create_ovsdb_external_ids_setting(
iface_info.get(OvsDB.OVS_DB_SUBTREE, {})
)
diff --git a/libnmstate/nm/profile.py b/libnmstate/nm/profile.py
index 53eaebed..ad1ad19f 100644
--- a/libnmstate/nm/profile.py
+++ b/libnmstate/nm/profile.py
@@ -56,6 +56,7 @@ ROUTE_REMOVED = "_route_removed"
class NmProfile:
# For unmanged iface and desired to down
ACTION_ACTIVATE_FIRST = "activate_first"
+ ACTION_SRIOV_PF = "activate_sriov_pf"
ACTION_DEACTIVATE = "deactivate"
ACTION_DEACTIVATE_FIRST = "deactivate_first"
ACTION_DELETE_DEVICE = "delete_device"
@@ -77,6 +78,7 @@ class NmProfile:
ACTION_ACTIVATE_FIRST,
ACTION_DEACTIVATE_FIRST,
ACTION_TOP_CONTROLLER,
+ ACTION_SRIOV_PF,
ACTION_NEW_IFACES,
ACTION_OTHER_CONTROLLER,
ACTION_NEW_OVS_PORT,
@@ -181,6 +183,11 @@ class NmProfile:
else:
self._add_action(NmProfile.ACTION_NEW_IFACES)
else:
+ if (
+ self._nm_dev.props.capabilities
+ & NM.DeviceCapabilities.SRIOV
+ ):
+ self._add_action(NmProfile.ACTION_SRIOV_PF)
if self._iface.type == InterfaceType.OVS_PORT:
self._add_action(NmProfile.ACTION_MODIFIED_OVS_PORT)
if self._iface.type == InterfaceType.OVS_INTERFACE:
@@ -462,6 +469,7 @@ class NmProfile:
def do_action(self, action):
if action in (
+ NmProfile.ACTION_SRIOV_PF,
NmProfile.ACTION_MODIFIED,
NmProfile.ACTION_MODIFIED_OVS_PORT,
NmProfile.ACTION_MODIFIED_OVS_IFACE,
@@ -559,8 +567,8 @@ class NmProfile:
or nm_profile.get_connection_type() == self._nm_iface_type
)
and (
- self._nm_profile is None
- or nm_profile.get_uuid() != self._nm_profile.get_uuid()
+ self._nm_simple_conn is None
+ or nm_profile.get_uuid() != self._nm_simple_conn.get_uuid()
)
):
ProfileDelete(
--
2.39.2

View File

@ -1,636 +0,0 @@
From ad2bfa136290e72cdfd4b7877b49b3fc07203f9c Mon Sep 17 00:00:00 2001
From: Gris Ge <fge@redhat.com>
Date: Tue, 21 Feb 2023 16:26:22 +0800
Subject: [PATCH] clib: Introduce YAML support
Allowing both YAML and JSON input, the output format will matching input
format.
For `nmstate_net_state_retrieve()`, user can use
`NMSTATE_FLAG_YAML_OUTPUT` flag to instruct the output to be YAML
format.
Signed-off-by: Gris Ge <fge@redhat.com>
---
rust/src/clib/Cargo.toml | 1 +
rust/src/clib/apply.rs | 2 +-
rust/src/clib/gen_conf.rs | 52 +++++++-----
rust/src/clib/nmstate.h.in | 55 +++++++------
rust/src/clib/policy.rs | 57 ++++++++-----
rust/src/clib/query.rs | 49 ++++++++----
.../{nmpolicy_test.c => nmpolicy_json_test.c} | 5 ++
rust/src/clib/test/nmpolicy_yaml_test.c | 80 +++++++++++++++++++
.../{nmstate_test.c => nmstate_json_test.c} | 5 ++
rust/src/clib/test/nmstate_yaml_test.c | 34 ++++++++
rust/src/lib/Cargo.toml | 3 +
rust/src/lib/net_state.rs | 14 +++-
12 files changed, 274 insertions(+), 83 deletions(-)
rename rust/src/clib/test/{nmpolicy_test.c => nmpolicy_json_test.c} (96%)
create mode 100644 rust/src/clib/test/nmpolicy_yaml_test.c
rename rust/src/clib/test/{nmstate_test.c => nmstate_json_test.c} (87%)
create mode 100644 rust/src/clib/test/nmstate_yaml_test.c
diff --git a/rust/src/clib/Cargo.toml b/rust/src/clib/Cargo.toml
index 97e4128c..ed391b3a 100644
--- a/rust/src/clib/Cargo.toml
+++ b/rust/src/clib/Cargo.toml
@@ -16,6 +16,7 @@ crate-type = ["cdylib", "staticlib"]
nmstate = { path = "../lib", default-features = false }
libc = "0.2.74"
serde_json = "1.0"
+serde_yaml = "0.9"
log = "0.4.17"
serde = { version = "1.0.137", features = ["derive"] }
once_cell = "1.12.0"
diff --git a/rust/src/clib/apply.rs b/rust/src/clib/apply.rs
index 9a0d6fbc..67d39730 100644
--- a/rust/src/clib/apply.rs
+++ b/rust/src/clib/apply.rs
@@ -74,7 +74,7 @@ pub extern "C" fn nmstate_net_state_apply(
};
let mut net_state =
- match nmstate::NetworkState::new_from_json(net_state_str) {
+ match nmstate::NetworkState::new_from_yaml(net_state_str) {
Ok(n) => n,
Err(e) => {
unsafe {
diff --git a/rust/src/clib/gen_conf.rs b/rust/src/clib/gen_conf.rs
index f63fb7b0..1ad7156b 100644
--- a/rust/src/clib/gen_conf.rs
+++ b/rust/src/clib/gen_conf.rs
@@ -68,7 +68,7 @@ pub extern "C" fn nmstate_generate_configurations(
}
};
- let net_state = match nmstate::NetworkState::new_from_json(net_state_str) {
+ let net_state = match nmstate::NetworkState::new_from_yaml(net_state_str) {
Ok(n) => n,
Err(e) => {
unsafe {
@@ -80,28 +80,44 @@ pub extern "C" fn nmstate_generate_configurations(
}
};
+ let input_is_json =
+ serde_json::from_str::<serde_json::Value>(net_state_str).is_ok();
let result = net_state.gen_conf();
unsafe {
*log = CString::new(logger.drain(now)).unwrap().into_raw();
}
match result {
- Ok(s) => match serde_json::to_string(&s) {
- Ok(cfgs) => unsafe {
- *configs = CString::new(cfgs).unwrap().into_raw();
- NMSTATE_PASS
- },
- Err(e) => unsafe {
- *err_msg =
- CString::new(format!("serde_json::to_string failure: {e}"))
- .unwrap()
- .into_raw();
- *err_kind =
- CString::new(format!("{}", nmstate::ErrorKind::Bug))
- .unwrap()
- .into_raw();
- NMSTATE_FAIL
- },
- },
+ Ok(s) => {
+ let serialize = if input_is_json {
+ serde_json::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to JSON: {e}"),
+ )
+ })
+ } else {
+ serde_yaml::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to YAML: {e}"),
+ )
+ })
+ };
+
+ match serialize {
+ Ok(cfgs) => unsafe {
+ *configs = CString::new(cfgs).unwrap().into_raw();
+ NMSTATE_PASS
+ },
+ Err(e) => unsafe {
+ *err_msg =
+ CString::new(e.msg().to_string()).unwrap().into_raw();
+ *err_kind =
+ CString::new(e.kind().to_string()).unwrap().into_raw();
+ NMSTATE_FAIL
+ },
+ }
+ }
Err(e) => {
unsafe {
*err_msg = CString::new(e.msg()).unwrap().into_raw();
diff --git a/rust/src/clib/nmstate.h.in b/rust/src/clib/nmstate.h.in
index 0879d47e..391477fd 100644
--- a/rust/src/clib/nmstate.h.in
+++ b/rust/src/clib/nmstate.h.in
@@ -1,19 +1,4 @@
-/*
- * Copyright 2021 Red Hat
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+// SPDX-License-Identifier: Apache-2.0
#ifndef _LIBNMSTATE_H_
#define _LIBNMSTATE_H_
@@ -44,6 +29,7 @@ extern "C" {
#define NMSTATE_FLAG_NO_COMMIT 1 << 5
#define NMSTATE_FLAG_MEMORY_ONLY 1 << 6
#define NMSTATE_FLAG_RUNNING_CONFIG_ONLY 1 << 7
+#define NMSTATE_FLAG_YAML_OUTPUT 1 << 8
/**
* nmstate_net_state_retrieve - Retrieve network state
@@ -52,7 +38,7 @@ extern "C" {
* 0.1
*
* Description:
- * Retrieve network state in the format of JSON.
+ * Retrieve network state in the format of JSON or YAML.
*
* @flags:
* Flags for special use cases:
@@ -60,6 +46,13 @@ extern "C" {
* No flag
* * NMSTATE_FLAG_KERNEL_ONLY
* Do not use external plugins, show kernel status only.
+ * * NMSTATE_FLAG_INCLUDE_SECRETS
+ * No not hide sercerts like password.
+ * * NMSTATE_FLAG_RUNNING_CONFIG_ONLY
+ * Only include running config excluding running status like auto
+ * IP addresses and routes, LLDP neighbors.
+ * * NMSTATE_FLAG_YAML_OUTPUT
+ * Show the state in YAML format
* @state:
* Output pointer of char array for network state in json format.
* The memory should be freed by nmstate_net_state_free().
@@ -90,7 +83,7 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
* 0.1
*
* Description:
- * Apply network state in the format of JSON.
+ * Apply network state in the format of JSON or YAML.
*
* @flags:
* Flags for special use cases:
@@ -98,8 +91,12 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
* No flag
* * NMSTATE_FLAG_KERNEL_ONLY
* Do not use external plugins, apply to kernel only.
+ * * NMSTATE_FLAG_NO_VERIFY
+ * Do not verify state after applied
* * NMSTATE_FLAG_NO_COMMIT
* Do not commit new state after verification
+ * * NMSTATE_FLAG_MEMORY_ONLY
+ * No not store network state to persistent.
* @state:
* Pointer of char array for network state in json format.
* @log:
@@ -119,7 +116,8 @@ int nmstate_net_state_retrieve(uint32_t flags, char **state, char **log,
* * NMSTATE_FAIL
* On failure.
*/
-int nmstate_net_state_apply(uint32_t flags, const char *state, uint32_t rollback_timeout, char **log,
+int nmstate_net_state_apply(uint32_t flags, const char *state,
+ uint32_t rollback_timeout, char **log,
char **err_kind, char **err_msg);
/**
@@ -151,8 +149,8 @@ int nmstate_net_state_apply(uint32_t flags, const char *state, uint32_t rollback
* * NMSTATE_FAIL
* On failure.
*/
-int nmstate_checkpoint_commit(const char *checkpoint, char **log, char **err_kind,
- char **err_msg);
+int nmstate_checkpoint_commit(const char *checkpoint, char **log,
+ char **err_kind, char **err_msg);
/**
* nmstate_checkpoint_rollback - Rollback the checkpoint
@@ -183,8 +181,8 @@ int nmstate_checkpoint_commit(const char *checkpoint, char **log, char **err_kin
* * NMSTATE_FAIL
* On failure.
*/
-int nmstate_checkpoint_rollback(const char *checkpoint, char **log, char **err_kind,
- char **err_msg);
+int nmstate_checkpoint_rollback(const char *checkpoint, char **log,
+ char **err_kind, char **err_msg);
/**
* nmstate_generate_configurations - Generate network configurations
@@ -199,9 +197,10 @@ int nmstate_checkpoint_rollback(const char *checkpoint, char **log, char **err_k
* as value.
*
* @state:
- * Pointer of char array for network state in json format.
+ * Pointer of char array for network state in JSON or YAML format.
* @configs:
- * Output pointer of char array for network configures in json format.
+ * Output pointer of char array for network configures in JSON or
+ * YAML(depend on which format you use in @state) format.
* The memory should be freed by nmstate_net_state_free().
* @log:
* Output pointer of char array for logging.
@@ -231,14 +230,14 @@ int nmstate_generate_configurations(const char *state, char **configs,
* 2.2
*
* Description:
- * Generate new network state from policy again specifed state
+ * Generate new network state from policy again specified state
*
* @policy:
- * Pointer of char array for network policy in json format.
+ * Pointer of char array for network policy in JSON/YAML format.
* @current_state:
* Pointer of char array for current network state.
* @state:
- * Output pointer of char array for network state in json format.
+ * Output pointer of char array for network state in JSON/YAML format.
* The memory should be freed by nmstate_net_state_free().
* @log:
* Output pointer of char array for logging.
diff --git a/rust/src/clib/policy.rs b/rust/src/clib/policy.rs
index ec8c46c1..ea7dd036 100644
--- a/rust/src/clib/policy.rs
+++ b/rust/src/clib/policy.rs
@@ -67,6 +67,13 @@ pub extern "C" fn nmstate_net_state_from_policy(
}
};
+ let input_is_json =
+ if let Ok(policy_str) = unsafe { CStr::from_ptr(policy) }.to_str() {
+ serde_json::from_str::<serde_json::Value>(policy_str).is_ok()
+ } else {
+ false
+ };
+
let mut policy = match deserilize_from_c_char::<NetworkPolicy>(
policy, err_kind, err_msg,
) {
@@ -86,23 +93,37 @@ pub extern "C" fn nmstate_net_state_from_policy(
}
match result {
- Ok(s) => match serde_json::to_string(&s) {
- Ok(state_str) => unsafe {
- *state = CString::new(state_str).unwrap().into_raw();
- NMSTATE_PASS
- },
- Err(e) => unsafe {
- *err_msg =
- CString::new(format!("serde_json::to_string failure: {e}"))
- .unwrap()
- .into_raw();
- *err_kind =
- CString::new(format!("{}", nmstate::ErrorKind::Bug))
- .unwrap()
- .into_raw();
- NMSTATE_FAIL
- },
- },
+ Ok(s) => {
+ let serialize = if input_is_json {
+ serde_json::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to JSON: {e}"),
+ )
+ })
+ } else {
+ serde_yaml::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to YAML: {e}"),
+ )
+ })
+ };
+
+ match serialize {
+ Ok(state_str) => unsafe {
+ *state = CString::new(state_str).unwrap().into_raw();
+ NMSTATE_PASS
+ },
+ Err(e) => unsafe {
+ *err_msg =
+ CString::new(e.msg().to_string()).unwrap().into_raw();
+ *err_kind =
+ CString::new(e.kind().to_string()).unwrap().into_raw();
+ NMSTATE_FAIL
+ },
+ }
+ }
Err(e) => {
unsafe {
*err_msg = CString::new(e.msg()).unwrap().into_raw();
@@ -144,7 +165,7 @@ where
}
};
- match serde_json::from_str(content_str) {
+ match serde_yaml::from_str(content_str) {
Ok(n) => Some(n),
Err(e) => {
unsafe {
diff --git a/rust/src/clib/query.rs b/rust/src/clib/query.rs
index a24b9c83..12e44d05 100644
--- a/rust/src/clib/query.rs
+++ b/rust/src/clib/query.rs
@@ -14,6 +14,7 @@ pub(crate) const NMSTATE_FLAG_INCLUDE_SECRETS: u32 = 1 << 4;
pub(crate) const NMSTATE_FLAG_NO_COMMIT: u32 = 1 << 5;
pub(crate) const NMSTATE_FLAG_MEMORY_ONLY: u32 = 1 << 6;
pub(crate) const NMSTATE_FLAG_RUNNING_CONFIG_ONLY: u32 = 1 << 7;
+pub(crate) const NMSTATE_FLAG_YAML_OUTPUT: u32 = 1 << 8;
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle]
@@ -72,23 +73,37 @@ pub extern "C" fn nmstate_net_state_retrieve(
}
match result {
- Ok(s) => match serde_json::to_string(&s) {
- Ok(state_str) => unsafe {
- *state = CString::new(state_str).unwrap().into_raw();
- NMSTATE_PASS
- },
- Err(e) => unsafe {
- *err_msg =
- CString::new(format!("serde_json::to_string failure: {e}"))
- .unwrap()
- .into_raw();
- *err_kind =
- CString::new(format!("{}", nmstate::ErrorKind::Bug))
- .unwrap()
- .into_raw();
- NMSTATE_FAIL
- },
- },
+ Ok(s) => {
+ let serialize = if (flags & NMSTATE_FLAG_YAML_OUTPUT) > 0 {
+ serde_yaml::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to YAML: {e}"),
+ )
+ })
+ } else {
+ serde_json::to_string(&s).map_err(|e| {
+ nmstate::NmstateError::new(
+ nmstate::ErrorKind::Bug,
+ format!("Failed to convert state {s:?} to JSON: {e}"),
+ )
+ })
+ };
+
+ match serialize {
+ Ok(state_str) => unsafe {
+ *state = CString::new(state_str).unwrap().into_raw();
+ NMSTATE_PASS
+ },
+ Err(e) => unsafe {
+ *err_msg =
+ CString::new(e.msg().to_string()).unwrap().into_raw();
+ *err_kind =
+ CString::new(e.kind().to_string()).unwrap().into_raw();
+ NMSTATE_FAIL
+ },
+ }
+ }
Err(e) => {
unsafe {
*err_msg = CString::new(e.msg()).unwrap().into_raw();
diff --git a/rust/src/clib/test/nmpolicy_test.c b/rust/src/clib/test/nmpolicy_json_test.c
similarity index 96%
rename from rust/src/clib/test/nmpolicy_test.c
rename to rust/src/clib/test/nmpolicy_json_test.c
index 7a71a5f5..8a0444d4 100644
--- a/rust/src/clib/test/nmpolicy_test.c
+++ b/rust/src/clib/test/nmpolicy_json_test.c
@@ -1,3 +1,6 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -91,6 +94,8 @@ int main(void) {
rc = EXIT_FAILURE;
}
+ assert(state[0] == '{');
+
nmstate_cstring_free(state);
nmstate_cstring_free(err_kind);
nmstate_cstring_free(err_msg);
diff --git a/rust/src/clib/test/nmpolicy_yaml_test.c b/rust/src/clib/test/nmpolicy_yaml_test.c
new file mode 100644
index 00000000..7984f509
--- /dev/null
+++ b/rust/src/clib/test/nmpolicy_yaml_test.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <nmstate.h>
+
+int main(void) {
+ int rc = EXIT_SUCCESS;
+ const char *policy = "\
+capture:\n\
+ default-gw: override me with the cache\n\
+ base-iface: >\n\
+ interfaces.name == capture.default-gw.routes.running.0.next-hop-interface\n\
+ base-iface-routes: >\n\
+ routes.running.next-hop-interface ==\n\
+ capture.default-gw.routes.running.0.next-hop-interface\n\
+ bridge-routes: >\n\
+ capture.base-iface-routes | routes.running.next-hop-interface:=\"br1\"\n\
+desired:\n\
+ interfaces:\n\
+ - name: br1\n\
+ description: Linux bridge with base interface as a port\n\
+ type: linux-bridge\n\
+ state: up\n\
+ bridge:\n\
+ options:\n\
+ stp:\n\
+ enabled: false\n\
+ port:\n\
+ - name: '{{ capture.base-iface.interfaces.0.name }}'\n\
+ ipv4: '{{ capture.base-iface.interfaces.0.ipv4 }}'\n\
+ routes:\n\
+ config: '{{ capture.bridge-routes.routes.running }}'";
+ const char *current_state = "\
+interfaces:\n\
+- name: eth1\n\
+ type: ethernet\n\
+ state: up\n\
+ mac-address: 1c:c1:0c:32:3b:ff\n\
+ ipv4:\n\
+ address:\n\
+ - ip: 192.0.2.251\n\
+ prefix-length: 24\n\
+ dhcp: false\n\
+ enabled: true\n\
+routes:\n\
+ config:\n\
+ - destination: 0.0.0.0/0\n\
+ next-hop-address: 192.0.2.1\n\
+ next-hop-interface: eth1\n\
+ running:\n\
+ - destination: 0.0.0.0/0\n\
+ next-hop-address: 192.0.2.1\n\
+ next-hop-interface: eth1";
+ char *state = NULL;
+ char *err_kind = NULL;
+ char *err_msg = NULL;
+ char *log = NULL;
+
+ if (nmstate_net_state_from_policy(policy, current_state, &state, &log,
+ &err_kind, &err_msg) == NMSTATE_PASS)
+ {
+ printf("%s\n", state);
+ } else {
+ printf("%s: %s\n", err_kind, err_msg);
+ rc = EXIT_FAILURE;
+ }
+
+ assert(state[0] != '{');
+
+ nmstate_cstring_free(state);
+ nmstate_cstring_free(err_kind);
+ nmstate_cstring_free(err_msg);
+ nmstate_cstring_free(log);
+ exit(rc);
+}
diff --git a/rust/src/clib/test/nmstate_test.c b/rust/src/clib/test/nmstate_json_test.c
similarity index 87%
rename from rust/src/clib/test/nmstate_test.c
rename to rust/src/clib/test/nmstate_json_test.c
index 0e79cb15..1bfbcda7 100644
--- a/rust/src/clib/test/nmstate_test.c
+++ b/rust/src/clib/test/nmstate_json_test.c
@@ -1,3 +1,6 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -21,6 +24,8 @@ int main(void) {
rc = EXIT_FAILURE;
}
+ assert(state[0] == '{');
+
nmstate_cstring_free(state);
nmstate_cstring_free(err_kind);
nmstate_cstring_free(err_msg);
diff --git a/rust/src/clib/test/nmstate_yaml_test.c b/rust/src/clib/test/nmstate_yaml_test.c
new file mode 100644
index 00000000..de0f2486
--- /dev/null
+++ b/rust/src/clib/test/nmstate_yaml_test.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include <assert.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <nmstate.h>
+
+int main(void) {
+ int rc = EXIT_SUCCESS;
+ char *state = NULL;
+ char *err_kind = NULL;
+ char *err_msg = NULL;
+ char *log = NULL;
+ uint32_t flag = NMSTATE_FLAG_KERNEL_ONLY | NMSTATE_FLAG_YAML_OUTPUT;
+
+ if (nmstate_net_state_retrieve(flag, &state, &log, &err_kind, &err_msg)
+ == NMSTATE_PASS) {
+ printf("%s\n", state);
+ } else {
+ printf("%s: %s\n", err_kind, err_msg);
+ rc = EXIT_FAILURE;
+ }
+
+ assert(state[0] != '{');
+
+ nmstate_cstring_free(state);
+ nmstate_cstring_free(err_kind);
+ nmstate_cstring_free(err_msg);
+ nmstate_cstring_free(log);
+ exit(rc);
+}
diff --git a/rust/src/lib/Cargo.toml b/rust/src/lib/Cargo.toml
index a27d0e1a..0142026d 100644
--- a/rust/src/lib/Cargo.toml
+++ b/rust/src/lib/Cargo.toml
@@ -15,6 +15,9 @@ edition = "2018"
[lib]
path = "lib.rs"
+[dependencies]
+serde_yaml = "0.9"
+
[dependencies.nispor]
version = "1.2.9"
optional = true
diff --git a/rust/src/lib/net_state.rs b/rust/src/lib/net_state.rs
index 8ab79642..fe5fea78 100644
--- a/rust/src/lib/net_state.rs
+++ b/rust/src/lib/net_state.rs
@@ -274,7 +274,19 @@ impl NetworkState {
Ok(s) => Ok(s),
Err(e) => Err(NmstateError::new(
ErrorKind::InvalidArgument,
- format!("Invalid json string: {e}"),
+ format!("Invalid JSON string: {e}"),
+ )),
+ }
+ }
+
+ /// Wrapping function of [serde_yaml::from_str()] with error mapped to
+ /// [NmstateError].
+ pub fn new_from_yaml(net_state_yaml: &str) -> Result<Self, NmstateError> {
+ match serde_yaml::from_str(net_state_yaml) {
+ Ok(s) => Ok(s),
+ Err(e) => Err(NmstateError::new(
+ ErrorKind::InvalidArgument,
+ format!("Invalid YAML string: {e}"),
)),
}
}
--
2.39.2

View File

@ -3,8 +3,8 @@
%define libname libnmstate
Name: nmstate
Version: 1.4.2
Release: 4%{?dist}
Version: 1.4.3
Release: 1%{?dist}
Summary: Declarative network manager API
License: LGPLv2+
URL: https://github.com/%{srcname}/%{srcname}
@ -14,9 +14,6 @@ Source2: https://www.nmstate.io/nmstate.gpg
Source3: %{url}/releases/download/v%{version}/%{srcname}-vendor-%{version}.tar.xz
# Patches 0X are reserved to downstream only
Patch0: BZ_2132570-nm-reverse-IPv6-order-before-adding-them-to-setting.patch
Patch11: Enable_clib_yml_api.patch
Patch12: BZ_2160416-fix-SRIOV.patch
Patch13: BZ_2160416-Ignore-error-when-creating-profile-if-not-desired.patch
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: gnupg2
@ -151,6 +148,9 @@ popd
/sbin/ldconfig
%changelog
* Wed Mar 29 2023 Gris Ge <fge@redhat.com> - 1.4.3-1
- Upgrade to nmstate 1.4.3. RHBZ#2179899
* Mon Feb 27 2023 Gris Ge <fge@redhat.com> - 1.4.2-4
- Ignore undesired iface config. RHBZ#2160416

View File

@ -1,2 +1,3 @@
SHA512 (nmstate-1.4.2.tar.gz) = e99d600d2c2921b614e8236d1ce651eee6a94a091a5a60946393bf84c7af2169b39cb8e2577dbfcf54ade9d0e527e73d52be4e356def19c99277870b1b853690
SHA512 (nmstate-vendor-1.4.2.tar.xz) = 1109d4a803313205d2a7e63eaee44f181c013b7dd3e5d698931e2f2a68ea3ef736dd3c16afc0960852c85a1db1918d1f2f6edb6ab917bca447f8f5ea48bc827e
SHA512 (nmstate-1.4.3.tar.gz) = f25341834a2dcb04a64061fcee6bd95181ee9261478e2577d7978ab8040b2e523be7a730b031580fccb46ca58ad03f0eb9b340786a04653f92c61f8594610bb5
SHA512 (nmstate-1.4.3.tar.gz.asc) = cb49622aadbc9a4cf85a5b306523fc810344a68e781477b26491a88ee110b6510a3544e1c0bc09ee21b78dbdf4934e1835379adc3ab10eb778ce685c3ad9557f
SHA512 (nmstate-vendor-1.4.3.tar.xz) = 4ea75195b59534bd86204ec2b9a2f30b8b16dd689d8878953828d84b0a17f81c3ec9238ae81d8b3fd385765db46212da24f02729ba9c97a75439ee8cc0c99fb8