Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mmynk committed Oct 20, 2023
1 parent 3bf5bc8 commit 09d9a62
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 55 deletions.
7 changes: 4 additions & 3 deletions src/link.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use netlink_packet_route::nlas::link::Nla;

use crate::{netlink, Link, errors::LinkError};
use crate::{errors::LinkError, netlink, Link};

/// `links` parses `LinkMessage`s into `Link`s and returns them.
pub fn links<T: netlink::NetlinkConnection>() -> Result<Vec<Link>, LinkError> {
Expand All @@ -21,10 +21,11 @@ pub fn links<T: netlink::NetlinkConnection>() -> Result<Vec<Link>, LinkError> {
}

if name.is_none() {
return Err(LinkError::MissingAttribute("Interface name not found".to_string()));
return Err(LinkError::MissingAttribute(
"Interface name not found".to_string(),
));
}


links.push(Link {
index,
name: name.unwrap(),
Expand Down
5 changes: 3 additions & 2 deletions src/netlink.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use netlink_packet_core::{
NetlinkHeader, NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
};
use netlink_packet_route::{RtnlMessage, TcHeader, TcMessage, LinkMessage};
use netlink_packet_route::{LinkMessage, RtnlMessage, TcHeader, TcMessage};
use netlink_sys::{protocols::NETLINK_ROUTE, Socket, SocketAddr};

use crate::errors::NetlinkError;
Expand Down Expand Up @@ -36,7 +36,8 @@ impl NetlinkConnection for Netlink {
where
Self: Sized,
{
let socket = Socket::new(NETLINK_ROUTE).map_err(|err| NetlinkError::Socket(Box::new(err)))?;
let socket =
Socket::new(NETLINK_ROUTE).map_err(|err| NetlinkError::Socket(Box::new(err)))?;
socket
.connect(&SocketAddr::new(0, 0))
.map_err(|err| NetlinkError::Socket(Box::new(err)))?;
Expand Down
8 changes: 6 additions & 2 deletions src/qdiscs/fq_codel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ fn unmarshal_fq_codel(nlas: Vec<&nla::DefaultNla>) -> FqCodel {

fn unmarshal_fq_codel_xstats(bytes: &[u8]) -> Result<FqCodelXStats, TcError> {
if bytes.len() < 40 {
return Err(TcError::InvalidAttribute("FqCodel XStats requires 40 bytes".to_string()));
return Err(TcError::InvalidAttribute(
"FqCodel XStats requires 40 bytes".to_string(),
));
}
let buf: [u8; 4] = bytes[..4].try_into().map_err(|_| TcError::Decode("Failed to extract FqCodel XStats kind".to_string()))?;
let buf: [u8; 4] = bytes[..4]
.try_into()
.map_err(|_| TcError::Decode("Failed to extract FqCodel XStats kind".to_string()))?;
let kind = u32::from_ne_bytes(buf);
if kind == 0 {
bincode::deserialize(bytes).map_err(|e| TcError::UnmarshalStruct(e))
Expand Down
13 changes: 6 additions & 7 deletions src/tc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use crate::{
class::htb::Htb,
constants::*,
errors::TcError,
netlink,
links, netlink,
qdiscs::{
clsact::Clsact,
fq_codel::{FqCodel, FqCodelXStats},
},
types::*, HtbXstats, links,
types::*,
HtbXstats,
};

fn parse_stats(attr: &mut Attribute, tc_stats: &netlink_tc::Stats) {
Expand Down Expand Up @@ -92,11 +93,9 @@ fn parse_xstats(attr: &mut Attribute, bytes: Vec<u8>) -> Result<(), TcError> {
let kind = kind.as_str();
attr.xstats = match kind {
FQ_CODEL => FqCodelXStats::new(&bytes)
.and_then(|x| Ok(XStats::FqCodel(x)))
.ok(),
HTB => HtbXstats::new(&bytes)
.and_then(|x| Ok(XStats::Htb(x)))
.ok(),
.and_then(|x| Ok(XStats::FqCodel(x)))
.ok(),
HTB => HtbXstats::new(&bytes).and_then(|x| Ok(XStats::Htb(x))).ok(),
_ => None,
}
}
Expand Down
86 changes: 46 additions & 40 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use netlink_packet_route::{tc, TcHeader, TcMessage, LinkMessage};
use netlink_packet_route::{tc, LinkMessage, TcHeader, TcMessage};
use netlink_packet_utils::{nla, Parseable};

use crate::{errors::NetlinkError, netlink::NetlinkConnection};
Expand Down Expand Up @@ -155,8 +155,8 @@ impl NetlinkConnection for MockNetlink {
tc::nlas::Nla::HwOffload(0),
tc::nlas::Nla::Stats2(vec![
tc::nlas::Stats2::StatsApp(vec![
0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]),
tc::nlas::Stats2::StatsBasic(vec![
76, 222, 96, 2, 0, 0, 0, 0, // bytes
Expand Down Expand Up @@ -186,8 +186,8 @@ impl NetlinkConnection for MockNetlink {
tc::nlas::Stats::parse(&stats_buf).unwrap()
}),
tc::nlas::Nla::XStats(vec![
0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]),
];
messages.push(TcMessage::from_parts(header, nlas));
Expand Down Expand Up @@ -272,7 +272,10 @@ impl NetlinkConnection for MockNetlink {
tc::nlas::Nla::Kind("htb".to_string()),
tc::nlas::Nla::Options(vec![tc::nlas::TcOpt::Other(nla::DefaultNla::new(
1,
vec![0, 1, 0, 0, 0, 0, 0, 0, 72, 232, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 72, 232, 1, 0, 64, 13, 3, 0, 64, 13, 3, 0, 212, 48, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0]
vec![
0, 1, 0, 0, 0, 0, 0, 0, 72, 232, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 72, 232, 1, 0,
64, 13, 3, 0, 64, 13, 3, 0, 212, 48, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0,
],
))]),
tc::nlas::Nla::Stats2(vec![
tc::nlas::Stats2::StatsBasic(vec![
Expand All @@ -288,8 +291,8 @@ impl NetlinkConnection for MockNetlink {
0, 0, 0, 0, // overlimits
]),
tc::nlas::Stats2::StatsApp(vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 13, 3, 0, 64, 13, 3, 0
])
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 13, 3, 0, 64, 13, 3, 0,
]),
]),
tc::nlas::Nla::Stats({
let buf = [
Expand All @@ -310,7 +313,7 @@ impl NetlinkConnection for MockNetlink {
0, 0, 0, 0, // borrows
0, 0, 0, 0, // giants
64, 13, 3, 0, // tokens
64, 13, 3, 0 // ctokens
64, 13, 3, 0, // ctokens
]),
];
messages.push(TcMessage::from_parts(header, nlas));
Expand Down Expand Up @@ -461,37 +464,40 @@ fn test_htb() {

// class
let htb = tc.attr.class.as_ref().unwrap();
assert_eq!(htb, &Class::Htb(Htb {
parms: Some(HtbOpt {
rate: RateSpec {
cell_log: 0,
linklayer: 1,
overhead: 0,
cell_align: 0,
mpu: 0,
rate: 125000,
},
ceil: RateSpec {
cell_log: 0,
linklayer: 1,
overhead: 0,
cell_align: 0,
mpu: 0,
rate: 125000,
},
buffer: 200000,
cbuffer: 200000,
quantum: 12500,
level: 7,
prio: 0,
}),
init: None,
ctab: vec![],
rtab: vec![],
direct_qlen: 0,
rate64: 0,
ceil64: 0,
}));
assert_eq!(
htb,
&Class::Htb(Htb {
parms: Some(HtbOpt {
rate: RateSpec {
cell_log: 0,
linklayer: 1,
overhead: 0,
cell_align: 0,
mpu: 0,
rate: 125000,
},
ceil: RateSpec {
cell_log: 0,
linklayer: 1,
overhead: 0,
cell_align: 0,
mpu: 0,
rate: 125000,
},
buffer: 200000,
cbuffer: 200000,
quantum: 12500,
level: 7,
prio: 0,
}),
init: None,
ctab: vec![],
rtab: vec![],
direct_qlen: 0,
rate64: 0,
ceil64: 0,
})
);
// xstats
let xstats = tc.attr.xstats.as_ref().unwrap();
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tc::{classes, qdiscs, links, Netlink};
use tc::{classes, links, qdiscs, Netlink};

#[test]
fn test_get_qdiscs() {
Expand Down

0 comments on commit 09d9a62

Please sign in to comment.