1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::convert::TryFrom;
use anyhow::Context;
use crate::{
constants::*,
nlas::{self, DefaultNla, NlaBuffer},
parsers::parse_u16,
traits::Parseable,
DecodeError,
};
use byteorder::{ByteOrder, NativeEndian};
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum AfSpecBridge {
Flags(u16),
VlanInfo(BridgeVlanInfo),
Other(DefaultNla),
}
impl nlas::Nla for AfSpecBridge {
fn value_len(&self) -> usize {
use self::AfSpecBridge::*;
match *self {
VlanInfo(_) => 4,
Flags(_) => 2,
Other(ref nla) => nla.value_len(),
}
}
fn emit_value(&self, buffer: &mut [u8]) {
use self::AfSpecBridge::*;
match *self {
Flags(value) => NativeEndian::write_u16(buffer, value),
VlanInfo(ref info) => {
(&mut buffer[..4]).copy_from_slice(<[u8; 4]>::from(info).as_slice())
}
Other(ref nla) => nla.emit_value(buffer),
}
}
fn kind(&self) -> u16 {
use self::AfSpecBridge::*;
match *self {
Flags(_) => IFLA_BRIDGE_FLAGS,
VlanInfo(_) => IFLA_BRIDGE_VLAN_INFO,
Other(ref nla) => nla.kind(),
}
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for AfSpecBridge {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
use self::AfSpecBridge::*;
let payload = buf.value();
Ok(match buf.kind() {
IFLA_BRIDGE_VLAN_INFO => VlanInfo(
BridgeVlanInfo::try_from(payload).context("Invalid IFLA_BRIDGE_VLAN_INFO value")?,
),
IFLA_BRIDGE_FLAGS => {
Flags(parse_u16(payload).context("invalid IFLA_BRIDGE_FLAGS value")?)
}
kind => Other(DefaultNla::parse(buf).context(format!("Unknown NLA type {}", kind))?),
})
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct BridgeVlanInfo {
pub flags: u16,
pub vid: u16,
}
impl From<&BridgeVlanInfo> for [u8; 4] {
fn from(d: &BridgeVlanInfo) -> Self {
let mut ret = [0u8; 4];
NativeEndian::write_u16(&mut ret[0..2], d.flags);
NativeEndian::write_u16(&mut ret[2..4], d.vid);
ret
}
}
impl TryFrom<&[u8]> for BridgeVlanInfo {
type Error = DecodeError;
fn try_from(raw: &[u8]) -> Result<Self, DecodeError> {
if raw.len() == 4 {
Ok(Self {
flags: parse_u16(&raw[0..2])
.context(format!("Invalid IFLA_BRIDGE_VLAN_INFO value: {:?}", raw))?,
vid: parse_u16(&raw[2..4])
.context(format!("Invalid IFLA_BRIDGE_VLAN_INFO value: {:?}", raw))?,
})
} else {
Err(DecodeError::from(format!(
"Invalid IFLA_BRIDGE_VLAN_INFO value, expecting [u8;4], but got {:?}",
raw
)))
}
}
}