Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various memory optimizations #94

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,26 @@ Check out the `tests/` directory for more usage examples.
This example applies a ruleset that creates and deletes a table to nftables.

```rust
use nft::{batch::Batch, helper, schema, types};
use nftables::{batch::Batch, helper, schema, types};

/// Applies a ruleset to nftables.
fn test_apply_ruleset() {
let ruleset = example_ruleset();
nft::helper::apply_ruleset(&ruleset, None, None).unwrap();
helper::apply_ruleset(&ruleset, None, None).unwrap();
}

fn example_ruleset() -> schema::Nftables {
fn example_ruleset() -> schema::Nftables<'static> {
let mut batch = Batch::new();
batch.add(schema::NfListObject::Table(schema::Table::new(
types::NfFamily::IP,
"test-table-01".to_string(),
)));
batch.delete(schema::NfListObject::Table(schema::Table::new(
types::NfFamily::IP,
"test-table-01".to_string(),
)));
batch.add(schema::NfListObject::Table(schema::Table {
family: types::NfFamily::IP,
name: "test-table-01".into(),
..Default::default()
}));
batch.delete(schema::NfListObject::Table(schema::Table {
family: types::NfFamily::IP,
name: "test-table-01".into(),
..Default::default()
}));
batch.to_nftables()
}
```
Expand All @@ -90,16 +92,16 @@ fn test_chain_table_rule_inet() {
// nft add table inet some_inet_table
// nft add chain inet some_inet_table some_inet_chain '{ type filter hook forward priority 0; policy accept; }'
let expected: Nftables = Nftables {
objects: vec![
objects: Cow::Borrowed(&[
NfObject::CmdObject(NfCmd::Add(NfListObject::Table(Table {
family: NfFamily::INet,
name: "some_inet_table".to_string(),
name: Cow::Borrowed("some_inet_table"),
handle: None,
}))),
NfObject::CmdObject(NfCmd::Add(NfListObject::Chain(Chain {
family: NfFamily::INet,
table: "some_inet_table".to_string(),
name: "some_inet_chain".to_string(),
table: Cow::Borrowed("some_inet_table"),
name: Cow::Borrowed("some_inet_chain"),
newname: None,
handle: None,
_type: Some(NfChainType::Filter),
Expand All @@ -108,7 +110,7 @@ fn test_chain_table_rule_inet() {
dev: None,
policy: Some(NfChainPolicy::Accept),
}))),
],
]),
};
let json = json!({"nftables":[{"add":{"table":{"family":"inet","name":"some_inet_table"}}},{"add":{"chain":{"family":"inet","table":"some_inet_table","name":"some_inet_chain","type":"filter","hook":"forward","policy":"accept"}}}]});
println!("{}", &json);
Expand Down
28 changes: 15 additions & 13 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,52 @@ use crate::schema::{NfCmd, NfListObject, NfObject, Nftables};

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
/// Batch manages nftables objects and is used to prepare an nftables payload.
pub struct Batch {
data: Vec<NfObject>,
pub struct Batch<'a> {
data: Vec<NfObject<'a>>,
}

impl Default for Batch {
impl Default for Batch<'_> {
fn default() -> Self {
Self::new()
}
}

impl Batch {
impl<'a> Batch<'a> {
/// Creates an empty Batch instance.
pub fn new() -> Batch {
pub fn new() -> Batch<'a> {
Batch { data: Vec::new() }
}

/// Adds object with `add` command to Batch.
pub fn add(&mut self, obj: NfListObject) {
pub fn add(&mut self, obj: NfListObject<'a>) {
self.data.push(NfObject::CmdObject(NfCmd::Add(obj)))
}

/// Adds object with `delete` command to Batch.
pub fn delete(&mut self, obj: NfListObject) {
pub fn delete(&mut self, obj: NfListObject<'a>) {
self.data.push(NfObject::CmdObject(NfCmd::Delete(obj)))
}

/// Adds a command to Batch.
pub fn add_cmd(&mut self, cmd: NfCmd) {
pub fn add_cmd(&mut self, cmd: NfCmd<'a>) {
self.data.push(NfObject::CmdObject(cmd))
}

/// Adds a list object (without a command) directly to Batch.
/// This corresponds to the descriptive output format of `nft -j list ruleset`.
pub fn add_obj(&mut self, obj: NfListObject) {
self.data.push(NfObject::ListObject(Box::new(obj)))
pub fn add_obj(&mut self, obj: NfListObject<'a>) {
self.data.push(NfObject::ListObject(obj))
}

/// Adds all given objects to the batch.
pub fn add_all(&mut self, objs: Vec<NfObject>) {
pub fn add_all<I: IntoIterator<Item = NfObject<'a>>>(&mut self, objs: I) {
self.data.extend(objs)
}

/// Wraps Batch in nftables object.
pub fn to_nftables(self) -> Nftables {
Nftables { objects: self.data }
pub fn to_nftables(self) -> Nftables<'a> {
Nftables {
objects: self.data.into(),
}
}
}
Loading