nmstate/nm/settings/
vlan.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use super::super::nm_dbus::{NmConnection, NmSettingVlanFlag};
4
5use crate::{VlanInterface, VlanProtocol, VlanRegistrationProtocol};
6
7const NM_802_1_AD: &str = "802.1ad";
8const NM_802_1_Q: &str = "802.1Q";
9
10pub(crate) fn gen_nm_vlan_setting(
11    iface: &VlanInterface,
12    nm_conn: &mut NmConnection,
13) {
14    if let Some(vlan_conf) = iface.vlan.as_ref() {
15        let mut nm_vlan = nm_conn.vlan.as_ref().cloned().unwrap_or_default();
16        nm_vlan.id = Some(vlan_conf.id.into());
17        nm_vlan.parent = vlan_conf.base_iface.clone();
18        if let Some(protocol) = vlan_conf.protocol {
19            match protocol {
20                VlanProtocol::Ieee8021Ad => {
21                    nm_vlan.protocol = Some(NM_802_1_AD.to_string());
22                }
23                VlanProtocol::Ieee8021Q => {
24                    // To support old NetworkManager 1.41- which VLAN protocol
25                    // is not supported, we only set 802.1q protocol explicitly
26                    // when protocol is already defined in NM connection.
27                    if nm_vlan.protocol.is_some() {
28                        nm_vlan.protocol = Some(NM_802_1_Q.to_string());
29                    }
30                }
31            }
32        }
33
34        if let Some(registration_protocol) = vlan_conf.registration_protocol {
35            match registration_protocol {
36                VlanRegistrationProtocol::Gvrp => {
37                    nm_vlan
38                        .flags
39                        .retain(|x| !matches!(x, NmSettingVlanFlag::Mvrp));
40                    nm_vlan.flags.push(NmSettingVlanFlag::Gvrp);
41                }
42                VlanRegistrationProtocol::Mvrp => {
43                    nm_vlan
44                        .flags
45                        .retain(|x| !matches!(x, NmSettingVlanFlag::Gvrp));
46                    nm_vlan.flags.push(NmSettingVlanFlag::Mvrp);
47                }
48                VlanRegistrationProtocol::None => {
49                    nm_vlan.flags.retain(|x| {
50                        !matches!(
51                            x,
52                            NmSettingVlanFlag::Gvrp | NmSettingVlanFlag::Mvrp,
53                        )
54                    });
55                }
56            }
57        }
58
59        if let Some(reorder_headers) = vlan_conf.reorder_headers {
60            if reorder_headers {
61                nm_vlan.flags.push(NmSettingVlanFlag::ReorderHeaders);
62            } else {
63                nm_vlan.flags.retain(|x| {
64                    !matches!(x, NmSettingVlanFlag::ReorderHeaders)
65                });
66            }
67        }
68
69        if let Some(loose_binding) = vlan_conf.loose_binding {
70            if loose_binding {
71                nm_vlan.flags.push(NmSettingVlanFlag::LooseBinding);
72            } else {
73                nm_vlan
74                    .flags
75                    .retain(|x| !matches!(x, NmSettingVlanFlag::LooseBinding));
76            }
77        }
78        nm_conn.vlan = Some(nm_vlan);
79    }
80}