Skip to content

Commit

Permalink
mac vlan/vtap: Introduce MacVlanMode and MacVtapMode
Browse files Browse the repository at this point in the history
The `MacVtapMode` is just alias to `MacVlanMode`.

Unit test case updated and also added new unit test case for MAC VTAP.

Signed-off-by: Gris Ge <[email protected]>
  • Loading branch information
cathay4t committed Feb 5, 2024
1 parent de0f47a commit cebeba9
Show file tree
Hide file tree
Showing 6 changed files with 432 additions and 31 deletions.
108 changes: 81 additions & 27 deletions src/link/link_info/mac_vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const IFLA_MACVLAN_BC_CUTOFF: u16 = 9;
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum InfoMacVlan {
Mode(u32),
Mode(MacVlanMode),
Flags(u16),
MacAddrMode(u32),
MacAddr([u8; 6]),
Expand All @@ -37,34 +37,38 @@ pub enum InfoMacVlan {

impl Nla for InfoMacVlan {
fn value_len(&self) -> usize {
use self::InfoMacVlan::*;
match self {
Mode(_) => 4,
Flags(_) => 2,
MacAddrMode(_) => 4,
MacAddr(_) => 6,
MacAddrData(ref nlas) => nlas.as_slice().buffer_len(),
MacAddrCount(_) => 4,
BcQueueLen(_) => 4,
BcQueueLenUsed(_) => 4,
BcCutoff(_) => 4,
Other(nla) => nla.value_len(),
Self::Mode(_) => 4,
Self::Flags(_) => 2,
Self::MacAddrMode(_) => 4,

Check warning on line 43 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L43

Added line #L43 was not covered by tests
Self::MacAddr(_) => 6,
Self::MacAddrData(ref nlas) => nlas.as_slice().buffer_len(),
Self::MacAddrCount(_) => 4,
Self::BcQueueLen(_) => 4,
Self::BcQueueLenUsed(_) => 4,
Self::BcCutoff(_) => 4,
Self::Other(nla) => nla.value_len(),

Check warning on line 50 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L49-L50

Added lines #L49 - L50 were not covered by tests
}
}

fn emit_value(&self, buffer: &mut [u8]) {
use self::InfoMacVlan::*;
match self {
Mode(value) => NativeEndian::write_u32(buffer, *value),
Flags(value) => NativeEndian::write_u16(buffer, *value),
MacAddrMode(value) => NativeEndian::write_u32(buffer, *value),
MacAddr(bytes) => buffer.copy_from_slice(bytes),
MacAddrData(ref nlas) => nlas.as_slice().emit(buffer),
MacAddrCount(value) => NativeEndian::write_u32(buffer, *value),
BcQueueLen(value) => NativeEndian::write_u32(buffer, *value),
BcQueueLenUsed(value) => NativeEndian::write_u32(buffer, *value),
BcCutoff(value) => NativeEndian::write_i32(buffer, *value),
Other(nla) => nla.emit_value(buffer),
Self::Mode(value) => {
NativeEndian::write_u32(buffer, (*value).into())
}
Self::Flags(value) => NativeEndian::write_u16(buffer, *value),
Self::MacAddrMode(value) => NativeEndian::write_u32(buffer, *value),

Check warning on line 60 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L60

Added line #L60 was not covered by tests
Self::MacAddr(bytes) => buffer.copy_from_slice(bytes),
Self::MacAddrData(ref nlas) => nlas.as_slice().emit(buffer),
Self::MacAddrCount(value) => {
NativeEndian::write_u32(buffer, *value)
}
Self::BcQueueLen(value) => NativeEndian::write_u32(buffer, *value),
Self::BcQueueLenUsed(value) => {
NativeEndian::write_u32(buffer, *value)
}
Self::BcCutoff(value) => NativeEndian::write_i32(buffer, *value),
Self::Other(nla) => nla.emit_value(buffer),

Check warning on line 71 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L70-L71

Added lines #L70 - L71 were not covered by tests
}
}

Expand Down Expand Up @@ -92,7 +96,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoMacVlan {
Ok(match buf.kind() {
IFLA_MACVLAN_MODE => Mode(
parse_u32(payload)
.context("invalid IFLA_MACVLAN_MODE value")?,
.context("invalid IFLA_MACVLAN_MODE value")?
.into(),
),
IFLA_MACVLAN_FLAGS => Flags(
parse_u16(payload)
Expand Down Expand Up @@ -142,7 +147,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoMacVlan {
#[derive(Debug, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum InfoMacVtap {
Mode(u32),
Mode(MacVtapMode),
Flags(u16),
MacAddrMode(u32),
MacAddr([u8; 6]),
Expand Down Expand Up @@ -174,7 +179,7 @@ impl Nla for InfoMacVtap {
fn emit_value(&self, buffer: &mut [u8]) {
use self::InfoMacVtap::*;
match self {
Mode(value) => NativeEndian::write_u32(buffer, *value),
Mode(value) => NativeEndian::write_u32(buffer, (*value).into()),
Flags(value) => NativeEndian::write_u16(buffer, *value),
MacAddrMode(value) => NativeEndian::write_u32(buffer, *value),
MacAddr(bytes) => buffer.copy_from_slice(bytes),
Expand Down Expand Up @@ -211,7 +216,8 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoMacVtap {
Ok(match buf.kind() {
IFLA_MACVLAN_MODE => Mode(
parse_u32(payload)
.context("invalid IFLA_MACVLAN_MODE value")?,
.context("invalid IFLA_MACVLAN_MODE value")?
.into(),
),
IFLA_MACVLAN_FLAGS => Flags(
parse_u16(payload)
Expand Down Expand Up @@ -257,3 +263,51 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoMacVtap {
})
}
}

const MACVLAN_MODE_PRIVATE: u32 = 1;
const MACVLAN_MODE_VEPA: u32 = 2;
const MACVLAN_MODE_BRIDGE: u32 = 4;
const MACVLAN_MODE_PASSTHRU: u32 = 8;
const MACVLAN_MODE_SOURCE: u32 = 16;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum MacVlanMode {
Private,
Vepa,
Bridge,
Passthrough,
Source,
Other(u32),
}

pub type MacVtapMode = MacVlanMode;

impl From<u32> for MacVlanMode {
fn from(d: u32) -> Self {
match d {
MACVLAN_MODE_PRIVATE => Self::Private,
MACVLAN_MODE_VEPA => Self::Vepa,
MACVLAN_MODE_BRIDGE => Self::Bridge,
MACVLAN_MODE_PASSTHRU => Self::Passthrough,

Check warning on line 292 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L290-L292

Added lines #L290 - L292 were not covered by tests
MACVLAN_MODE_SOURCE => Self::Source,
_ => {
log::warn!("Unknown MAC VLAN mode {}", d);
Self::Other(d)

Check warning on line 296 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L295-L296

Added lines #L295 - L296 were not covered by tests
}
}
}
}

impl From<MacVlanMode> for u32 {
fn from(v: MacVlanMode) -> u32 {
match v {
MacVlanMode::Private => MACVLAN_MODE_PRIVATE,
MacVlanMode::Vepa => MACVLAN_MODE_VEPA,
MacVlanMode::Bridge => MACVLAN_MODE_BRIDGE,
MacVlanMode::Passthrough => MACVLAN_MODE_PASSTHRU,

Check warning on line 308 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L306-L308

Added lines #L306 - L308 were not covered by tests
MacVlanMode::Source => MACVLAN_MODE_SOURCE,
MacVlanMode::Other(d) => d,

Check warning on line 310 in src/link/link_info/mac_vlan.rs

View check run for this annotation

Codecov / codecov/patch

src/link/link_info/mac_vlan.rs#L310

Added line #L310 was not covered by tests
}
}
}
2 changes: 1 addition & 1 deletion src/link/link_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use self::info_port::{InfoPortData, InfoPortKind};
pub use self::infos::{InfoKind, LinkInfo};
pub use self::ipoib::InfoIpoib;
pub use self::ipvlan::InfoIpVlan;
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap};
pub use self::mac_vlan::{InfoMacVlan, InfoMacVtap, MacVlanMode, MacVtapMode};
pub use self::macsec::{
InfoMacSec, MacSecCipherId, MacSecOffload, MacSecValidate,
};
Expand Down
3 changes: 2 additions & 1 deletion src/link/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub use self::link_info::{
InfoIpVlan, InfoIpoib, InfoKind, InfoMacSec, InfoMacVlan, InfoMacVtap,
InfoPortData, InfoPortKind, InfoSitTun, InfoTun, InfoVeth, InfoVlan,
InfoVrf, InfoVti, InfoVxlan, InfoXfrm, LinkInfo, LinkXstats,
MacSecCipherId, MacSecOffload, MacSecValidate, MiiStatus, VlanQosMapping,
MacSecCipherId, MacSecOffload, MacSecValidate, MacVlanMode, MacVtapMode,
MiiStatus, VlanQosMapping,
};
pub use self::link_layer_type::LinkLayerType;
pub use self::link_state::State;
Expand Down
4 changes: 2 additions & 2 deletions src/link/tests/macvlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use netlink_packet_utils::{Emitable, Parseable};

use crate::link::{
InfoData, InfoKind, InfoMacVlan, LinkAttribute, LinkFlag, LinkHeader,
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer,
LinkInfo, LinkLayerType, LinkMessage, LinkMessageBuffer, MacVlanMode,
};
use crate::AddressFamily;

Expand Down Expand Up @@ -39,7 +39,7 @@ fn test_macvlan_link_info() {
attributes: vec![LinkAttribute::LinkInfo(vec![
LinkInfo::Kind(InfoKind::MacVlan),
LinkInfo::Data(InfoData::MacVlan(vec![
InfoMacVlan::Mode(16),
InfoMacVlan::Mode(MacVlanMode::Source),
InfoMacVlan::Flags(0),
InfoMacVlan::MacAddrCount(2),
InfoMacVlan::MacAddrData(vec![
Expand Down
Loading

0 comments on commit cebeba9

Please sign in to comment.