forked from nmstate/nmstate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ipvlan: add IPVLAN interface support
Example: ``` --- interfaces: - name: ipvlan0 type: ipvlan state: up ipvlan: base-iface: eth1 mode: l2 ``` Integration tests added. Signed-off-by: Fernando Fernandez Mancera <[email protected]>
- Loading branch information
Showing
33 changed files
with
533 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
interfaces: | ||
- name: ipvlan0 | ||
type: ipvlan | ||
state: absent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
interfaces: | ||
- name: ipvlan0 | ||
type: ipvlan | ||
state: up | ||
ipvlan: | ||
base-iface: eth1 | ||
mode: l2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{BaseInterface, ErrorKind, InterfaceType, NmstateError}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[non_exhaustive] | ||
/// Linux kernel IPVLAN interface. The example YAML output of | ||
/// [crate::NetworkState] with an IPVLAN interface would be: | ||
/// ```yaml | ||
/// --- | ||
/// interfaces: | ||
/// - name: ipvlan0 | ||
/// type: ipvlan | ||
/// state: up | ||
/// ipvlan: | ||
/// base-iface: eth1 | ||
/// mode: l3 | ||
/// private: true | ||
/// ``` | ||
pub struct IpVlanInterface { | ||
#[serde(flatten)] | ||
pub base: BaseInterface, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub ipvlan: Option<IpVlanConfig>, | ||
} | ||
|
||
impl Default for IpVlanInterface { | ||
fn default() -> Self { | ||
Self { | ||
base: BaseInterface { | ||
iface_type: InterfaceType::IpVlan, | ||
..Default::default() | ||
}, | ||
ipvlan: None, | ||
} | ||
} | ||
} | ||
|
||
impl IpVlanInterface { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
pub(crate) fn sanitize( | ||
&self, | ||
is_desired: bool, | ||
) -> Result<(), NmstateError> { | ||
if is_desired { | ||
if let Some(conf) = &self.ipvlan { | ||
if conf.private == Some(true) && conf.vepa == Some(true) { | ||
let e = NmstateError::new( | ||
ErrorKind::InvalidArgument, | ||
"Both private and VEPA flags cannot \ | ||
be enabled at the same time" | ||
.to_string(), | ||
); | ||
log::error!("{}", e); | ||
return Err(e); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] | ||
#[serde(rename_all = "kebab-case", deny_unknown_fields)] | ||
#[non_exhaustive] | ||
pub struct IpVlanConfig { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub base_iface: Option<String>, | ||
#[serde(skip_serializing_if = "Option::is_none", default)] | ||
pub mode: Option<IpVlanMode>, | ||
#[serde( | ||
skip_serializing_if = "Option::is_none", | ||
default, | ||
deserialize_with = "crate::deserializer::option_bool_or_string" | ||
)] | ||
pub private: Option<bool>, | ||
#[serde( | ||
skip_serializing_if = "Option::is_none", | ||
default, | ||
deserialize_with = "crate::deserializer::option_bool_or_string" | ||
)] | ||
pub vepa: Option<bool>, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "kebab-case")] | ||
#[non_exhaustive] | ||
pub enum IpVlanMode { | ||
/// Deserialize and serialize from/to `l2`. | ||
L2, | ||
/// Deserialize and serialize from/to `l3`. | ||
L3, | ||
#[serde(rename = "l3s")] | ||
/// Deserialize and serialize from/to `l3s`. | ||
L3S, | ||
} | ||
|
||
impl From<IpVlanMode> for u32 { | ||
fn from(v: IpVlanMode) -> u32 { | ||
match v { | ||
IpVlanMode::L2 => 1, | ||
IpVlanMode::L3 => 2, | ||
IpVlanMode::L3S => 3, | ||
} | ||
} | ||
} | ||
|
||
impl Default for IpVlanMode { | ||
fn default() -> Self { | ||
Self::L3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{BaseInterface, IpVlanConfig, IpVlanInterface, IpVlanMode}; | ||
|
||
pub(crate) fn np_ipvlan_to_nmstate( | ||
np_iface: &nispor::Iface, | ||
base_iface: BaseInterface, | ||
) -> IpVlanInterface { | ||
let ipvlan_conf = | ||
np_iface | ||
.ip_vlan | ||
.as_ref() | ||
.map(|np_ipvlan_info| IpVlanConfig { | ||
mode: match &np_ipvlan_info.mode { | ||
nispor::IpVlanMode::L2 => Some(IpVlanMode::L2), | ||
nispor::IpVlanMode::L3 => Some(IpVlanMode::L3), | ||
nispor::IpVlanMode::L3S => Some(IpVlanMode::L3S), | ||
_ => { | ||
log::warn!( | ||
"Unknown supported IPVLAN mode {:?}", | ||
np_ipvlan_info.mode | ||
); | ||
Some(IpVlanMode::L3) | ||
} | ||
}, | ||
private: Some( | ||
np_ipvlan_info.flags.contains(&nispor::IpVlanFlag::Private), | ||
), | ||
vepa: Some( | ||
np_ipvlan_info.flags.contains(&nispor::IpVlanFlag::Vepa), | ||
), | ||
base_iface: Some(np_ipvlan_info.base_iface.clone()), | ||
}); | ||
|
||
IpVlanInterface { | ||
base: base_iface, | ||
ipvlan: ipvlan_conf, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.