Skip to content

Commit

Permalink
Remove asserts that block re-assignment. Re: #16
Browse files Browse the repository at this point in the history
The asserts removed in this commit all follow the pattern:

assert_eq!(some_struct.member, DEFAULT_VAL);

These asserts seem to only exist for preventing re-assignment of struct
values if they have already been assigned. This doesn't match what the
builder pattern usually does in rust. Rather than implementing an error
for this case,  and giving these methods the signature:

fn setter(self, val) -> Resutl<Self, Error::ReAssignment>

I removed the asserts to retain the standard builder pattern signature:

fn setter(self, val) -> Self

Signed-off-by: Erich Heine <[email protected]>
  • Loading branch information
Erich Heine authored and cathay4t committed Jul 10, 2023
1 parent 697fe74 commit e6bcf3e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 19 deletions.
11 changes: 1 addition & 10 deletions src/traffic_control/add_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use netlink_packet_route::{
constants::{
TCA_ACT_TAB, TCA_EGRESS_REDIR, TC_ACT_STOLEN, TC_H_CLSACT,
TC_H_MAJ_MASK, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS, TC_H_MIN_MASK,
TC_H_ROOT, TC_H_UNSPEC, TC_U32_TERMINAL,
TC_H_ROOT, TC_U32_TERMINAL,
},
},
RtnlMessage, TcMessage, TCM_IFINDEX_MAGIC_BLOCK, TC_H_MAKE,
Expand Down Expand Up @@ -53,15 +53,13 @@ impl TrafficFilterNewRequest {
/// Set interface index.
/// Equivalent to `dev STRING`, dev and block are mutually exlusive.
pub fn index(mut self, index: i32) -> Self {
assert_eq!(self.message.header.index, 0);
self.message.header.index = index;
self
}

/// Set block index.
/// Equivalent to `block BLOCK_INDEX`.
pub fn block(mut self, block_index: u32) -> Self {
assert_eq!(self.message.header.index, 0);
self.message.header.index = TCM_IFINDEX_MAGIC_BLOCK as i32;
self.message.header.parent = block_index;
self
Expand All @@ -71,36 +69,31 @@ impl TrafficFilterNewRequest {
/// Equivalent to `[ root | ingress | egress | parent CLASSID ]`
/// command args. They are mutually exlusive.
pub fn parent(mut self, parent: u32) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = parent;
self
}

/// Set parent to root.
pub fn root(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_ROOT;
self
}

/// Set parent to ingress.
pub fn ingress(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_MAKE!(TC_H_CLSACT, TC_H_MIN_INGRESS);
self
}

/// Set parent to egress.
pub fn egress(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_MAKE!(TC_H_CLSACT, TC_H_MIN_EGRESS);
self
}

/// Set priority.
/// Equivalent to `priority PRIO` or `pref PRIO`.
pub fn priority(mut self, priority: u16) -> Self {
assert_eq!(self.message.header.info & TC_H_MAJ_MASK, 0);
self.message.header.info =
TC_H_MAKE!((priority as u32) << 16, self.message.header.info);
self
Expand All @@ -110,7 +103,6 @@ impl TrafficFilterNewRequest {
/// Equivalent to `protocol PROT`.
/// Default: ETH_P_ALL 0x0003, see llproto_names at iproute2/lib/ll_proto.c.
pub fn protocol(mut self, protocol: u16) -> Self {
assert_eq!(self.message.header.info & TC_H_MIN_MASK, 0);
self.message.header.info =
TC_H_MAKE!(self.message.header.info, protocol as u32);
self
Expand Down Expand Up @@ -139,7 +131,6 @@ impl TrafficFilterNewRequest {
/// 0 action mirred egress redirect dev dest` You need to set the
/// `parent` and `protocol` before call redirect.
pub fn redirect(self, dst_index: u32) -> Self {
assert_eq!(self.message.nlas.len(), 0);
let mut sel_na = tc::u32::Sel::default();
sel_na.flags = TC_U32_TERMINAL;
sel_na.nkeys = 1;
Expand Down
7 changes: 1 addition & 6 deletions src/traffic_control/add_qdisc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
use futures::stream::StreamExt;
use netlink_packet_core::{NetlinkMessage, NLM_F_ACK, NLM_F_REQUEST};
use netlink_packet_route::{
tc::constants::{
TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_MIN_MASK, TC_H_ROOT, TC_H_UNSPEC,
},
tc::constants::{TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_MIN_MASK, TC_H_ROOT},
tc::nlas::Nla,
RtnlMessage, TcMessage, TC_H_MAKE,
};
Expand Down Expand Up @@ -54,21 +52,18 @@ impl QDiscNewRequest {

/// Set parent to root.
pub fn root(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_ROOT;
self
}

/// Set parent
pub fn parent(mut self, parent: u32) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = parent;
self
}

/// New a ingress qdisc
pub fn ingress(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_INGRESS;
self.message.header.handle = 0xffff0000;
self.message.nlas.push(Nla::Kind("ingress".to_string()));
Expand Down
4 changes: 1 addition & 3 deletions src/traffic_control/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::{
};
use netlink_packet_core::{NetlinkMessage, NLM_F_DUMP, NLM_F_REQUEST};
use netlink_packet_route::{
tc::constants::{TC_H_INGRESS, TC_H_ROOT, TC_H_UNSPEC},
tc::constants::{TC_H_INGRESS, TC_H_ROOT},
RtnlMessage, TcMessage,
};

Expand Down Expand Up @@ -54,7 +54,6 @@ impl QDiscGetRequest {

/// Get ingress qdisc
pub fn ingress(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_INGRESS;
self
}
Expand Down Expand Up @@ -129,7 +128,6 @@ impl TrafficFilterGetRequest {

/// Set parent to root.
pub fn root(mut self) -> Self {
assert_eq!(self.message.header.parent, TC_H_UNSPEC);
self.message.header.parent = TC_H_ROOT;
self
}
Expand Down

0 comments on commit e6bcf3e

Please sign in to comment.