Skip to content

Commit 73b5380

Browse files
committed
Experimental refactor
1 parent 76962a4 commit 73b5380

File tree

11 files changed

+112
-122
lines changed

11 files changed

+112
-122
lines changed

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub use self::address_family_freebsd::AddressFamily;
3535
target_os = "freebsd",
3636
)))]
3737
mod address_family_fallback;
38+
mod net;
39+
3840
#[cfg(not(any(
3941
target_os = "linux",
4042
target_os = "fuchsia",

Diff for: src/tc/filters/arp.rs renamed to src/net/arp.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// List from [iana.org][1]
22
///
33
/// [1]: https://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
4-
use crate::tc::filters::ethernet::Mac;
54
65
const RESERVED: u8 = 0;
76
const REQUEST: u8 = 1;
@@ -136,8 +135,3 @@ impl From<Operation> for u8 {
136135
*value.as_ref()
137136
}
138137
}
139-
140-
pub type Sha = Mac;
141-
pub type ShaMask = Mac;
142-
pub type Tha = Mac;
143-
pub type ThaMask = Mac;

Diff for: src/tc/filters/ethernet.rs renamed to src/net/ethernet.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use anyhow::Error;
2-
31
#[derive(Debug, PartialEq, Eq, Clone)]
42
pub struct Mac([u8; 6]);
53
pub type MacMask = Mac;
@@ -310,7 +308,7 @@ impl From<Ethertype> for u16 {
310308
pub struct VlanId(u16);
311309

312310
impl VlanId {
313-
pub fn try_new(id: u16) -> Result<Self, Error> {
311+
pub fn try_new(id: u16) -> Result<Self, anyhow::Error> {
314312
if id >= 4096 {
315313
return Err(anyhow::anyhow!("VLAN ID must be less than 4096"));
316314
}
@@ -319,7 +317,7 @@ impl VlanId {
319317
}
320318

321319
impl TryFrom<u16> for VlanId {
322-
type Error = Error;
320+
type Error = anyhow::Error;
323321

324322
fn try_from(id: u16) -> Result<Self, Self::Error> {
325323
Self::try_new(id)
@@ -342,7 +340,7 @@ impl From<VlanId> for u16 {
342340
pub struct VlanPrio(u8);
343341

344342
impl VlanPrio {
345-
pub fn try_new(prio: u8) -> Result<Self, Error> {
343+
pub fn try_new(prio: u8) -> Result<Self, anyhow::Error> {
346344
if prio > Self::HIGHEST.into() {
347345
return Err(anyhow::anyhow!("VLAN priority must be less than 8"));
348346
}
@@ -354,7 +352,7 @@ impl VlanPrio {
354352
}
355353

356354
impl TryFrom<u8> for VlanPrio {
357-
type Error = Error;
355+
type Error = anyhow::Error;
358356

359357
fn try_from(prio: u8) -> Result<Self, Self::Error> {
360358
Self::try_new(prio)

Diff for: src/tc/filters/icmpv4.rs renamed to src/net/icmpv4.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
#![deny(
2-
clippy::all,
3-
clippy::pedantic,
4-
clippy::unwrap_used,
5-
clippy::expect_used,
6-
clippy::panic
7-
)]
8-
91
/// Lists sourced from [iana.org][1]
102
///
113
/// [1]: https://www.iana.org/assignments/icmp-parameters/icmp-parameters.xhtml
@@ -508,16 +500,16 @@ impl From<RouterSolicitation> for u8 {
508500
#[non_exhaustive]
509501
#[repr(u8)]
510502
pub enum TimeExceeded {
511-
TimeToLiveExceededInTransit = 0,
512-
FragmentReassemblyTimeExceeded = 1,
503+
TtlExceededInTransit = 0,
504+
FragmentReassembly = 1,
513505
Other(u8),
514506
}
515507

516508
impl AsRef<u8> for TimeExceeded {
517509
fn as_ref(&self) -> &u8 {
518510
match self {
519-
TimeExceeded::TimeToLiveExceededInTransit => &0,
520-
TimeExceeded::FragmentReassemblyTimeExceeded => &1,
511+
TimeExceeded::TtlExceededInTransit => &0,
512+
TimeExceeded::FragmentReassembly => &1,
521513
TimeExceeded::Other(x) => x,
522514
}
523515
}
@@ -526,8 +518,8 @@ impl AsRef<u8> for TimeExceeded {
526518
impl From<u8> for TimeExceeded {
527519
fn from(value: u8) -> Self {
528520
match value {
529-
0 => TimeExceeded::TimeToLiveExceededInTransit,
530-
1 => TimeExceeded::FragmentReassemblyTimeExceeded,
521+
0 => TimeExceeded::TtlExceededInTransit,
522+
1 => TimeExceeded::FragmentReassembly,
531523
x => TimeExceeded::Other(x),
532524
}
533525
}
@@ -1268,6 +1260,3 @@ impl AsRef<u8> for Code {
12681260
}
12691261
}
12701262
}
1271-
1272-
pub type TypeMask = u8;
1273-
pub type CodeMask = u8;

Diff for: src/tc/filters/icmpv6.rs renamed to src/net/icmpv6.rs

-2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,4 @@ impl From<Type> for u8 {
232232
}
233233
}
234234

235-
pub type TypeMask = u8;
236235
pub type Code = u8;
237-
pub type CodeMask = u8;

Diff for: src/net/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// General purpose networking abstractions.
2+
#![forbid(unsafe_code)]
3+
#![deny(
4+
clippy::all,
5+
clippy::pedantic,
6+
clippy::unwrap_used,
7+
clippy::expect_used,
8+
clippy::panic
9+
)]
10+
11+
pub mod arp;
12+
pub mod ethernet;
13+
pub mod icmpv4;
14+
pub mod icmpv6;
15+
pub mod mpls;

Diff for: src/net/mpls.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use std::convert::TryFrom;
2+
use anyhow::Error;
3+
4+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)]
5+
#[repr(transparent)]
6+
pub struct Label(u32);
7+
8+
/// Should we add handling for reserved labels as per [RFC 3032][1] (see page
9+
/// 4)?
10+
///
11+
/// [1]: https://www.iana.org/assignments/mpls-label-values/mpls-label-values.xhtml
12+
impl Label {
13+
pub fn try_new(label: u32) -> Result<Self, Error> {
14+
if label > 0xFFFFF {
15+
Err(Error::msg("MPLS label must be less than 0xFFFFF"))?;
16+
}
17+
Ok(Self(label))
18+
}
19+
}
20+
21+
impl TryFrom<u32> for Label {
22+
type Error = Error;
23+
24+
fn try_from(label: u32) -> Result<Self, Self::Error> {
25+
Self::try_new(label)
26+
}
27+
}
28+
29+
impl From<Label> for u32 {
30+
fn from(label: Label) -> u32 {
31+
label.0
32+
}
33+
}
34+
35+
/// Bottom of stack flag.
36+
///
37+
/// The "bottom of stack" flag is only a single bit wide.
38+
/// For this reason, we can get away without marking this as non-exhaustive.
39+
/// It is represented as `u8` in the netlink message.
40+
/// I take this to mean that it functions like a c boolean and that any non-zero
41+
/// value is `Set`.
42+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)]
43+
#[repr(u8)]
44+
pub enum BottomOfStack {
45+
Unset = 0,
46+
Set = 1,
47+
}
48+
49+
impl From<u8> for BottomOfStack {
50+
fn from(bos: u8) -> Self {
51+
match bos {
52+
0 => BottomOfStack::Unset,
53+
1 => BottomOfStack::Set,
54+
_ => {
55+
log::warn!(
56+
"Invalid BottomOfStack value: {}, interpreting as Set",
57+
bos
58+
);
59+
BottomOfStack::Set
60+
}
61+
}
62+
}
63+
}
64+
65+
impl From<BottomOfStack> for u8 {
66+
fn from(value: BottomOfStack) -> Self {
67+
value as u8
68+
}
69+
}

Diff for: src/tc/filters/cls_flower.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ use netlink_packet_utils::{
1717
DecodeError, Emitable,
1818
};
1919

20-
use crate::tc::filters::ethernet;
20+
use crate::net::mpls;
21+
use crate::net::{ethernet, icmpv4, icmpv6};
2122
use crate::tc::filters::flower::encap;
22-
use crate::tc::filters::flower::mpls;
23-
use crate::tc::{arp, icmpv4, icmpv6, TcAction, TcFlowerOptionFlags, TcHandle};
23+
use crate::net::arp;
24+
use crate::tc::{TcAction, TcFlowerOptionFlags, TcHandle};
2425
use crate::{EncKeyId, IpProtocol};
26+
use crate::tc::filters::flower;
2527

2628
pub(crate) const TCA_FLOWER_CLASSID: u16 = 1;
2729
pub(crate) const TCA_FLOWER_INDEV: u16 = 2;
@@ -287,7 +289,7 @@ pub enum TcFilterFlowerOption {
287289
KeyCtMarkMask(u32),
288290
KeyCtLabels(u128),
289291
KeyCtLabelsMask(u128),
290-
KeyMplsOpts(mpls::Options),
292+
KeyMplsOpts(flower::mpls::Options),
291293
KeyHash(u32),
292294
KeyHashMask(u32),
293295
KeyNumOfVlans(u8),
@@ -1556,7 +1558,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
15561558
Self::KeyCtLabels(parse_u128_be(payload)?)
15571559
}
15581560
TCA_FLOWER_KEY_MPLS_OPTS => {
1559-
Self::KeyMplsOpts(mpls::Options::parse(buf)?)
1561+
Self::KeyMplsOpts(flower::mpls::Options::parse(buf)?)
15601562
}
15611563
TCA_FLOWER_KEY_HASH => Self::KeyHash(parse_u32(payload)?),
15621564
TCA_FLOWER_KEY_HASH_MASK => Self::KeyHash(parse_u32(payload)?),

Diff for: src/tc/filters/flower/mpls.rs

+5-77
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,11 @@
11
use crate::tc::filters::cls_flower::TCA_FLOWER_KEY_MPLS_OPTS;
2-
use anyhow::Error;
32
use netlink_packet_utils::nla::{
43
DefaultNla, Nla, NlaBuffer, NlasIterator, NLA_F_NESTED,
54
};
65
use netlink_packet_utils::parsers::{parse_u32, parse_u8};
76
use netlink_packet_utils::{DecodeError, Emitable, Parseable};
87

9-
// The "bottom of stack" flag is only a single bit wide.
10-
// For this reason, we can get away without marking this as non-exhaustive.
11-
// It is represented as `u8` in the netlink message.
12-
// I take this to mean that it functions like a c boolean and that any non-zero
13-
// value is `Set`. I
14-
/// Bottom of stack flag.
15-
///
16-
/// The "bottom of stack" flag is only a single bit wide.
17-
/// For this reason, we can get away without marking this as non-exhaustive.
18-
/// It is represented as `u8` in the netlink message.
19-
/// I take this to mean that it functions like a c boolean and that any non-zero
20-
/// value is `Set`.
21-
#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)]
22-
#[repr(u8)]
23-
pub enum BottomOfStack {
24-
Unset = 0,
25-
Set = 1,
26-
}
27-
28-
impl From<u8> for BottomOfStack {
29-
fn from(bos: u8) -> Self {
30-
match bos {
31-
0 => BottomOfStack::Unset,
32-
1 => BottomOfStack::Set,
33-
_ => {
34-
log::warn!(
35-
"Invalid BottomOfStack value: {}, interpreting as Set",
36-
bos
37-
);
38-
BottomOfStack::Set
39-
}
40-
}
41-
}
42-
}
43-
44-
impl From<BottomOfStack> for u8 {
45-
fn from(value: BottomOfStack) -> Self {
46-
value as u8
47-
}
48-
}
49-
50-
51-
#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)]
52-
#[repr(transparent)]
53-
pub struct Label(u32);
54-
55-
/// TODO: should we add handling for reserved labels as per [RFC 3032][1] (see
56-
/// page 4)?
57-
///
58-
/// [1]: https://www.iana.org/assignments/mpls-label-values/mpls-label-values.xhtml
59-
impl Label {
60-
pub fn try_new(label: u32) -> Result<Self, Error> {
61-
if label > 0xFFFFF {
62-
Err(Error::msg("MPLS label must be less than 0xFFFFF"))?
63-
}
64-
Ok(Self(label))
65-
}
66-
}
67-
68-
impl TryFrom<u32> for Label {
69-
type Error = Error;
70-
71-
fn try_from(label: u32) -> Result<Self, Self::Error> {
72-
Self::try_new(label)
73-
}
74-
}
75-
76-
impl From<Label> for u32 {
77-
fn from(label: Label) -> u32 {
78-
label.0
79-
}
80-
}
8+
use crate::net::mpls;
819

8210
#[derive(Debug, PartialEq, Eq, Clone)]
8311
#[non_exhaustive]
@@ -90,9 +18,9 @@ pub enum Options {
9018
#[non_exhaustive]
9119
pub enum LseOptions {
9220
Depth(u8),
93-
Label(Label),
21+
Label(mpls::Label),
9422
TrafficClass(u8),
95-
BottomOfStack(BottomOfStack),
23+
BottomOfStack(mpls::BottomOfStack),
9624
Ttl(u8),
9725
}
9826

@@ -200,13 +128,13 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for LseOptions {
200128
}
201129
TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL => Self::Ttl(parse_u8(payload)?),
202130
TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS => {
203-
Self::BottomOfStack(BottomOfStack::from(parse_u8(payload)?))
131+
Self::BottomOfStack(mpls::BottomOfStack::from(parse_u8(payload)?))
204132
}
205133
TCA_FLOWER_KEY_MPLS_OPT_LSE_TC => {
206134
Self::TrafficClass(parse_u8(payload)?)
207135
}
208136
TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL => {
209-
Self::Label(Label::try_from(parse_u32(payload)?)?)
137+
Self::Label(mpls::Label::try_from(parse_u32(payload)?)?)
210138
}
211139
_ => Err(DecodeError::from("invalid mpls option kind"))?,
212140
})

Diff for: src/tc/filters/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ pub use self::matchall::{TcFilterMatchAll, TcFilterMatchAllOption};
1414

1515
pub mod flower;
1616

17-
pub mod arp;
1817
mod cls_flags;
1918
mod cls_flower;
2019
mod cls_u32;
21-
pub mod ethernet;
2220
mod flower_flags;
23-
pub mod icmpv4;
24-
pub mod icmpv6;
2521
mod matchall;
2622
mod u32_flags;

Diff for: src/tc/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ pub use self::actions::{
1414
};
1515
pub use self::attribute::TcAttribute;
1616
pub use self::filters::{
17-
arp, icmpv4, icmpv6, CfmAttribute, ConnectionTrackingFlags, L2Miss,
18-
TcFilterFlower, TcFilterFlowerOption, TcFilterMatchAll,
19-
TcFilterMatchAllOption, TcFilterU32, TcFilterU32Option,
20-
TcFlowerOptionFlags, TcU32Key, TcU32OptionFlags, TcU32Selector,
21-
TcU32SelectorFlags, TcpFlags,
17+
CfmAttribute, ConnectionTrackingFlags, L2Miss, TcFilterFlower,
18+
TcFilterFlowerOption, TcFilterMatchAll, TcFilterMatchAllOption,
19+
TcFilterU32, TcFilterU32Option, TcFlowerOptionFlags, TcU32Key,
20+
TcU32OptionFlags, TcU32Selector, TcU32SelectorFlags, TcpFlags,
2221
};
2322

2423
pub use self::header::{TcHandle, TcHeader, TcMessageBuffer};

0 commit comments

Comments
 (0)