Skip to content

Commit

Permalink
gc: Fix trunk vlans for ovs port in gen_conf
Browse files Browse the repository at this point in the history
The `nmstatectl gc` will fail on OVS trunk VLAN state like:

```yml
---
interfaces:
  - name: br0
    type: ovs-bridge
    state: up
    bridge:
      port:
        - name: eth1
          vlan:
            mode: trunk
            trunk-tags:
            - id: 500
```

Integration test case included.

Signed-off-by: Koen de Laat <koen.de.laat@philips.com>
  • Loading branch information
koendelaat authored and cathay4t committed Oct 16, 2024
1 parent 23247af commit 614c66d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
11 changes: 11 additions & 0 deletions examples/ovsbridge_vlan_port.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ interfaces:
- name: ovs0
type: ovs-interface
state: up
- name: ovs1
type: ovs-interface
state: up
- name: ovs-br0
type: ovs-bridge
state: up
Expand All @@ -12,3 +15,11 @@ interfaces:
vlan:
mode: access
tag: 2
- name: ovs1
vlan:
mode: trunk
trunk-tags:
- id: 1
- id-range:
min: 10
max: 20
33 changes: 32 additions & 1 deletion rust/src/lib/nm/nm_dbus/gen_conf/ovs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

use crate::nm::nm_dbus::ToDbusValue;
use std::collections::HashMap;

use super::super::{
Expand All @@ -9,11 +10,41 @@ use super::super::{
};

impl ToKeyfile for NmSettingOvsBridge {}
impl ToKeyfile for NmSettingOvsPort {}
impl ToKeyfile for NmSettingOvsIface {}
impl ToKeyfile for NmSettingOvsPatch {}
impl ToKeyfile for NmSettingOvsDpdk {}

impl ToKeyfile for NmSettingOvsPort {
fn to_keyfile(&self) -> Result<HashMap<String, zvariant::Value>, NmError> {
let mut ret = HashMap::new();

for (k, v) in self.to_value()?.drain() {
if k != "trunks" {
ret.insert(k.to_string(), v);
}
}
if let Some(vlans) = self.trunks.as_ref() {
let mut vlans_clone = vlans.clone();
vlans_clone.sort_unstable_by_key(|v| v.start);
let mut vlans_str = Vec::new();
for vlan in vlans_clone {
let ret = if vlan.start == vlan.end {
vlan.start.to_string()
} else {
format!("{}-{}", vlan.start, vlan.end)
};
vlans_str.push(ret);
}
ret.insert(
"trunks".to_string(),
zvariant::Value::new(vlans_str.join(",")),
);
}

Ok(ret)
}
}

impl ToKeyfile for NmSettingOvsExtIds {
fn to_keyfile(&self) -> Result<HashMap<String, zvariant::Value>, NmError> {
let mut ret = HashMap::new();
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/nm/gen_conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from libnmstate.schema import RouteRule
from libnmstate.schema import LLDP

from ..testlib import assertlib
from ..testlib import iprule
from ..testlib.env import is_k8s
from ..testlib.env import nm_minor_version
Expand Down Expand Up @@ -67,6 +68,42 @@ def test_gen_conf_ovs_same_name(eth1_up, cleanup_ovs_same_name):
retry_verify_ovs_ports("br0", sorted(["eth1", "br0"]))


@pytest.mark.tier1
@pytest.mark.skipif(is_k8s(), reason="K8S does not support genconf")
def test_gen_conf_ovs_trunk_vlan():
desired_state = load_yaml(
"""
interfaces:
- name: ovs0
type: ovs-interface
state: up
- name: ovs1
type: ovs-interface
state: up
- name: ovs-br0
type: ovs-bridge
state: up
bridge:
port:
- name: ovs0
vlan:
mode: access
tag: 10
- name: ovs1
vlan:
mode: trunk
trunk-tags:
- id: 1
- id-range:
min: 10
max: 20
"""
)

with gen_conf_apply(desired_state):
assertlib.assert_state_match(desired_state)


@pytest.mark.tier1
def test_gen_conf_routes_rules():
desired_state = load_yaml(
Expand Down

0 comments on commit 614c66d

Please sign in to comment.