From d1ac323bf9f1fbe3adab53671318e00c18858a58 Mon Sep 17 00:00:00 2001 From: Marcin Osypka Date: Wed, 17 Jan 2024 18:54:44 +0100 Subject: [PATCH 1/2] Implement map support A Verdict enum was moved from a NamedExpression to an Expression, placing it in a NamedExpression was a bug. Accroding to libnftables-json(5) (section VERDICT) a Verdict does not have a "verdict" key, it is "anonymous". e.g.: { "jump": { "target": "my_target"}, and not: { "verdict": { "jump": { "target": "my_target"} } } Additionally a `Verdict::Jump` and a `Verdict::Goto` data was changed to a `stmt::JumpTarget` struct from a `String`. A `Map` and a `Set` use almost the same fields, a map additinally uses a map field of type `String`. --- src/expr.rs | 7 ++++--- src/schema.rs | 21 ++++++++++++++++++++- tests/helper_tests.rs | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 5640e4d..eb26b4d 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashSet; use crate::stmt::Statement; +use crate::stmt::JumpTarget; #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] #[serde(untagged)] @@ -18,6 +19,7 @@ pub enum Expression { Range(Range), Named(NamedExpression), + Verdict(Verdict), } #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] @@ -45,7 +47,6 @@ pub enum NamedExpression { JHash(JHash), SymHash(SymHash), Fib(Fib), - Verdict(Verdict), Elem(Elem), Socket(Socket), Osf(Osf), @@ -346,8 +347,8 @@ pub enum Verdict { Drop, Continue, Return, - Jump(String), - Goto(String), + Jump(JumpTarget), + Goto(JumpTarget), } #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] diff --git a/src/schema.rs b/src/schema.rs index 0bfe949..9bc9573 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -197,7 +197,26 @@ pub struct Set { #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Map { - // TODO + pub family: NfFamily, + pub table: String, + pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub handle: Option, + #[serde(rename = "type")] + pub set_type: SetTypeValue, + pub map: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub policy: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub flags: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub elem: Option>, + #[serde(skip_serializing_if = "Option::is_none")] + pub timeout: Option, + #[serde(rename = "gc-interval", skip_serializing_if = "Option::is_none")] + pub gc_interval: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub size: Option, } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] diff --git a/tests/helper_tests.rs b/tests/helper_tests.rs index 2cb8af8..a7b3b91 100644 --- a/tests/helper_tests.rs +++ b/tests/helper_tests.rs @@ -58,6 +58,22 @@ fn example_ruleset() -> schema::Nftables { gc_interval: None, size: None, })); + let map_name = "test_map".to_string(); + let map_type = "verdict".to_string(); + batch.add(schema::NfListObject::Map(schema::Map { + family: types::NfFamily::IP, + table: table_name.clone(), + name: map_name.clone(), + handle: None, + map: map_type.clone(), + set_type: schema::SetTypeValue::Single(schema::SetType::Ipv4Addr), + policy: None, + flags: None, + elem: None, + timeout: None, + gc_interval: None, + size: None, + })); // add element to set batch.add(schema::NfListObject::Element(schema::Element { family: types::NfFamily::IP, From 3c87c233e9be6b8b73e707bfc902e77b0b84e387 Mon Sep 17 00:00:00 2001 From: Marcin Osypka Date: Thu, 18 Jan 2024 18:50:51 +0100 Subject: [PATCH 2/2] Add the comment field to a map The comments can be added to the map. --- src/schema.rs | 2 ++ tests/helper_tests.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/src/schema.rs b/src/schema.rs index 9bc9573..2875425 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -217,6 +217,8 @@ pub struct Map { pub gc_interval: Option, #[serde(skip_serializing_if = "Option::is_none")] pub size: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub comment: Option, } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] diff --git a/tests/helper_tests.rs b/tests/helper_tests.rs index a7b3b91..193c653 100644 --- a/tests/helper_tests.rs +++ b/tests/helper_tests.rs @@ -73,6 +73,7 @@ fn example_ruleset() -> schema::Nftables { timeout: None, gc_interval: None, size: None, + comment: None, })); // add element to set batch.add(schema::NfListObject::Element(schema::Element {