Skip to content

Commit

Permalink
Formatting and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-noland committed May 14, 2024
1 parent 9cee739 commit 3b2721e
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 63 deletions.
12 changes: 6 additions & 6 deletions src/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl EncKeyId {
/// Create a new `EncKeyId` without checking the validity of the ID value.
///
/// # Safety
///
///
/// Failure to ensure the ID is within the valid range for the tunnel in
/// question may lead to semantically invalid netlink messages.
///
Expand All @@ -37,7 +37,7 @@ impl EncKeyId {
/// of geneve vni values.
///
/// # Errors
///
///
/// Returns an error if the ID is greater than or equal to 2^24.
pub fn new_geneve_vni(id: u32) -> Result<Self, DecodeError> {
match Self::new_nbit::<24>(id) {
Expand All @@ -49,9 +49,9 @@ impl EncKeyId {
}

/// Create a new `EncKeyId` in the space of valid GRE keys.
///
///
/// # Safety
///
///
/// Since GRE keys are 32 bits and all values are legal, this method is not
/// failable.
#[must_use]
Expand All @@ -63,7 +63,7 @@ impl EncKeyId {
/// of gtp tunnel key values.
///
/// # Errors
///
///
/// Returns an error if the ID is zero.
pub fn new_gtp_key(id: u32) -> Result<Self, DecodeError> {
if id == 0 {
Expand All @@ -78,7 +78,7 @@ impl EncKeyId {
/// of N bit values.
///
/// # Errors
///
///
/// Returns an error if the ID is greater than or equal to 2^N.
const fn new_nbit<const N: usize>(id: u32) -> Result<Self, KeyTooLarge> {
if id >= (1 << N) {
Expand Down
27 changes: 13 additions & 14 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,16 @@ impl<'a, T: AsRef<[u8]> + ?Sized>
let msg = match RouteMessageBuffer::new_checked(&buf.inner()) {
Ok(buf) => RouteMessage::parse(&buf)
.context("invalid route message")?,
// HACK: iproute2 sends invalid RTM_GETROUTE message, where
// the header is limited to the
// interface family (1 byte) and 3 bytes of padding.
// HACK: iproute2 sends an invalid RTM_GETROUTE message,
// where the header is limited to the interface family
// (1 byte) and 3 bytes of padding.
Err(e) => {
// Not only does iproute2 sends invalid messages, it's
// also inconsistent in
// doing so: for link and address messages, the length
// advertised in the
// netlink header includes the 3 bytes of padding but it
// does not seem to be the case
// for the route message, hence the buf.length() == 1
// check.
// Not only does iproute2 send invalid messages, it's
// also inconsistent in doing so: for link and address
// messages, the length advertised in the netlink header
// includes the 3 bytes of padding, but it does not seem
// to be the case for the route message, hence the
// `buf.length() == 1` check.
if (buf.inner().len() == 4 || buf.inner().len() == 1)
&& message_type == RTM_GETROUTE
{
Expand Down Expand Up @@ -253,6 +251,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized>
_ => unreachable!(),
}
}

// TC Messages
RTM_NEWQDISC | RTM_DELQDISC | RTM_GETQDISC | RTM_NEWTCLASS
| RTM_DELTCLASS | RTM_GETTCLASS | RTM_NEWTFILTER
Expand Down Expand Up @@ -292,13 +291,13 @@ impl<'a, T: AsRef<[u8]> + ?Sized>
}
}

// TC action messages
RTM_NEWACTION | RTM_DELACTION | RTM_GETACTION => {
let err = "invalid tc action message";
let msg = TcActionMessage::parse(
&TcActionMessageBuffer::new_checked(&buf.inner())
.context(err)?,
.context("invalid tc action message buffer")?,
)
.context(err)?;
.context("invalid tc action message")?;
match message_type {
RTM_NEWACTION => RouteNetlinkMessage::NewTrafficAction(msg),
RTM_DELACTION => RouteNetlinkMessage::DelTrafficAction(msg),
Expand Down
4 changes: 2 additions & 2 deletions src/net/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const OP_EXP1: u8 = 24;
const OP_EXP2: u8 = 25;

/// Enum of ARP operation codes.
///
///
/// List from [iana.org][1]
///
///
/// [1]: https://www.iana.org/assignments/arp-parameters/arp-parameters.xhtml
#[derive(Debug, PartialEq, Eq, Clone, Copy, Ord, PartialOrd, Hash)]
#[non_exhaustive]
Expand Down
10 changes: 1 addition & 9 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
// General purpose networking abstractions.
#![forbid(unsafe_code)]
#![deny(
clippy::all,
clippy::pedantic,
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
)]
//! General purpose networking abstractions.
pub mod arp;
pub mod ethernet;
Expand Down
13 changes: 6 additions & 7 deletions src/net/vxlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Vni {
pub fn new_unchecked(vni: u32) -> Self {
Self(vni)
}

pub const MAX: u32 = (1 << 24) - 1;
pub const MIN: u32 = 1;
}
Expand Down Expand Up @@ -75,26 +75,25 @@ impl AsRef<u32> for Vni {
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn zero_vni_is_invalid() {
assert!(Vni::new(0).is_err());
}

#[test]
fn max_vni_is_valid() {
assert!(Vni::new(Vni::MAX).is_ok());
}

#[test]
fn vni_greater_than_max_is_invalid() {
assert!(Vni::new(Vni::MAX + 1).is_err());
}

#[test]
fn vni_is_converted_to_u32() {
let vni = Vni::new_unchecked(42);
assert_eq!(u32::from(vni), 42);
}

}
}
19 changes: 12 additions & 7 deletions src/tc/actions/tests/nat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ use crate::tc::{
};
use crate::AddressFamily;

/// # Setup
///
/// Capture of request for
///
/// ```bash
/// ```shell
/// tc actions add action nat ingress 1.2.3.4/32 5.6.7.0 index 1
/// ```
const TC_ACTION_NAT_EXAMPLE1: &[u8] = &[
Expand Down Expand Up @@ -77,9 +79,11 @@ fn emit_tc_action_nat_example1() {
assert_eq!(buf.as_slice(), TC_ACTION_NAT_EXAMPLE1);
}

/// # Setup
///
/// Capture of request for
///
/// ```bash
/// ```shell
/// tc actions add action nat ingress 1.2.3.0/24 5.6.7.9 index 2
/// ```
const TC_ACTION_NAT_EXAMPLE2: &[u8] = &[
Expand Down Expand Up @@ -138,7 +142,7 @@ fn emit_tc_action_nat_example2() {

/// Capture of request for
///
/// ```bash
/// ```shell
/// tc actions add action nat egress 2.3.4.0/24 5.6.7.9 index 3
/// ```
const TC_ACTION_NAT_EXAMPLE3: &[u8] = &[
Expand Down Expand Up @@ -281,20 +285,21 @@ fn tc_action_nat_option_emit_tm_uses_whole_buffer() {
}
}

/// Setup:
/// # Setup
///
/// ```bash
/// ```shell
/// tc actions flush action nat
/// tc actions add action nat ingress 192.0.2.1/32 203.0.113.1 index 1
/// ```
///
/// Then capture netlink response message of this command:
///
/// ```bash
/// ```shell
/// tc -statistics actions get action nat index 1
/// ```
///
/// Raw packet modification:
/// # Packet Modifications
///
/// * cooked header removed (16 bytes).
/// * rtnetlink header removed (16 bytes).
#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/tc/filters/cls_flower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ impl TcFilterFlower {
/// [2]: https://www.ieee802.org/1/pages/802.1ag.html
pub type CfmOpCode = u8;


bitflags! {
// TcpFlags _ARE_ exactly 8 bits.
// Why flower uses a 16-bit field is a mystery, but we deal with it.
Expand Down Expand Up @@ -705,8 +704,9 @@ impl Nla for TcFilterFlowerOption {
(flags.bits() as u16).to_be_bytes().as_slice(),
),
Self::KeyTcpFlagsMask(flags) => {
buffer
.copy_from_slice(u16::from(*flags).to_be_bytes().as_slice());
buffer.copy_from_slice(
u16::from(*flags).to_be_bytes().as_slice(),
);
}
Self::KeyIpTos(tos) => {
buffer.copy_from_slice(tos.to_be_bytes().as_slice());
Expand Down
8 changes: 2 additions & 6 deletions src/tc/filters/flower/encap/erspan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ impl Nla for Direction {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for Direction
{
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Direction {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
if buf.value().len() != 1 {
return Err(DecodeError::from(format!(
Expand Down Expand Up @@ -261,9 +259,7 @@ impl Nla for Options {
}
}

impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
for Options
{
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Options {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
Ok(match buf.kind() {
TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER => {
Expand Down
4 changes: 1 addition & 3 deletions src/tc/filters/flower/encap/gtp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Options {
TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE => {
Options::PduType(parse_u8(payload)?)
}
TCA_FLOWER_KEY_ENC_OPT_GTP_QFI => {
Options::Qfi(parse_u8(payload)?)
}
TCA_FLOWER_KEY_ENC_OPT_GTP_QFI => Options::Qfi(parse_u8(payload)?),
_ => Options::Other(
DefaultNla::parse(buf).context("failed to parse gtp nla")?,
),
Expand Down
3 changes: 1 addition & 2 deletions src/tc/filters/flower/encap/vxlan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use byteorder::{ByteOrder, NativeEndian};
use netlink_packet_utils::nla::{DefaultNla, Nla, NlaBuffer};
use netlink_packet_utils::{DecodeError, Parseable};
use netlink_packet_utils::parsers::parse_u32;
use netlink_packet_utils::{DecodeError, Parseable};

const TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GPB: u16 = 1; /* u32 */

Expand All @@ -12,7 +12,6 @@ pub enum Options {
Other(DefaultNla),
}


impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for Options {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
Ok(match buf.kind() {
Expand Down
9 changes: 5 additions & 4 deletions src/tc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ pub use self::actions::{
};
pub use self::attribute::TcAttribute;
pub use self::filters::{
CfmAttribute, ConnectionTrackingFlags, L2Miss, TcFilterFlower,
TcFilterFlowerOption, TcFilterMatchAll, TcFilterMatchAllOption,
TcFilterU32, TcFilterU32Option, TcFlowerOptionFlags, TcU32Key,
TcU32OptionFlags, TcU32Selector, TcU32SelectorFlags, TcpFlags,
CfmAttribute, ConnectionTrackingFlags, L2Miss, MaintenanceDomainLevel,
TcFilterFlower, TcFilterFlowerOption, TcFilterMatchAll,
TcFilterMatchAllOption, TcFilterU32, TcFilterU32Option,
TcFlowerOptionFlags, TcU32Key, TcU32OptionFlags, TcU32Selector,
TcU32SelectorFlags, TcpFlags,
};
pub use self::header::{TcHandle, TcHeader, TcMessageBuffer};
pub use self::message::TcMessage;
Expand Down

0 comments on commit 3b2721e

Please sign in to comment.