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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
mod stats;
pub use self::stats::*;
mod stats_queue;
pub use self::stats_queue::*;
mod stats_basic;
pub use self::stats_basic::*;
mod options;
pub use self::options::*;
mod qdisc;
pub use self::qdisc::*;
mod filter;
pub use self::filter::*;
mod action;
pub use self::action::*;
#[cfg(test)]
mod test;
use crate::{
constants::*,
nlas::{self, DefaultNla, NlaBuffer},
traits::{Emitable, Parseable},
DecodeError,
};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Nla {
Unspec(Vec<u8>),
Kind(String),
Options(Vec<TcOpt>),
Stats(Stats),
XStats(Vec<u8>),
Rate(Vec<u8>),
Fcnt(Vec<u8>),
Stats2(Vec<Stats2>),
Stab(Vec<u8>),
Chain(Vec<u8>),
HwOffload(u8),
Other(DefaultNla),
}
impl nlas::Nla for Nla {
#[rustfmt::skip]
fn value_len(&self) -> usize {
use self::Nla::*;
match *self {
Unspec(ref bytes) | XStats(ref bytes) | Rate(ref bytes) | Fcnt(ref bytes)
| Stab(ref bytes) | Chain(ref bytes) => bytes.len(),
HwOffload(_) => 1,
Stats2(ref thing) => thing.as_slice().buffer_len(),
Stats(_) => STATS_LEN,
Kind(ref string) => string.as_bytes().len() + 1,
Options(ref opt) => opt.as_slice().buffer_len(),
Other(ref attr) => attr.value_len(),
}
}
#[cfg_attr(nightly, rustfmt::skip)]
fn emit_value(&self, buffer: &mut [u8]) {
use self::Nla::*;
match *self {
Unspec(ref bytes)
| XStats(ref bytes)
| Rate(ref bytes)
| Fcnt(ref bytes)
| Stab(ref bytes)
| Chain(ref bytes) => buffer.copy_from_slice(bytes.as_slice()),
HwOffload(ref val) => buffer[0] = *val,
Stats2(ref stats) => stats.as_slice().emit(buffer),
Stats(ref stats) => stats.emit(buffer),
Kind(ref string) => {
buffer[..string.as_bytes().len()].copy_from_slice(string.as_bytes());
buffer[string.as_bytes().len()] = 0;
}
Options(ref opt) => opt.as_slice().emit(buffer),
Other(ref attr) => attr.emit_value(buffer),
}
}
fn kind(&self) -> u16 {
use self::Nla::*;
match *self {
Unspec(_) => TCA_UNSPEC,
Kind(_) => TCA_KIND,
Options(_) => TCA_OPTIONS,
Stats(_) => TCA_STATS,
XStats(_) => TCA_XSTATS,
Rate(_) => TCA_RATE,
Fcnt(_) => TCA_FCNT,
Stats2(_) => TCA_STATS2,
Stab(_) => TCA_STAB,
Chain(_) => TCA_CHAIN,
HwOffload(_) => TCA_HW_OFFLOAD,
Other(ref nla) => nla.kind(),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Stats2 {
StatsApp(Vec<u8>),
StatsBasic(Vec<u8>),
StatsQueue(Vec<u8>),
Other(DefaultNla),
}
impl nlas::Nla for Stats2 {
fn value_len(&self) -> usize {
use self::Stats2::*;
match *self {
StatsBasic(ref bytes) | StatsQueue(ref bytes) | StatsApp(ref bytes) => bytes.len(),
Other(ref nla) => nla.value_len(),
}
}
fn emit_value(&self, buffer: &mut [u8]) {
use self::Stats2::*;
match *self {
StatsBasic(ref bytes) | StatsQueue(ref bytes) | StatsApp(ref bytes) => {
buffer.copy_from_slice(bytes.as_slice())
}
Other(ref nla) => nla.emit_value(buffer),
}
}
fn kind(&self) -> u16 {
use self::Stats2::*;
match *self {
StatsApp(_) => TCA_STATS_APP,
StatsBasic(_) => TCA_STATS_BASIC,
StatsQueue(_) => TCA_STATS_QUEUE,
Other(ref nla) => nla.kind(),
}
}
}
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Stats2 {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
let payload = buf.value();
Ok(match buf.kind() {
TCA_STATS_APP => Self::StatsApp(payload.to_vec()),
TCA_STATS_BASIC => Self::StatsBasic(payload.to_vec()),
TCA_STATS_QUEUE => Self::StatsQueue(payload.to_vec()),
_ => Self::Other(DefaultNla::parse(buf)?),
})
}
}