-
Notifications
You must be signed in to change notification settings - Fork 138
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
WIP: rule: add String() method #108
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nftables.test |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,15 @@ import ( | |
"golang.org/x/sys/unix" | ||
) | ||
|
||
const ( | ||
NFT_DROP = 0 | ||
NFT_ACCEPT = 1 | ||
NFT_STOLEN = 2 | ||
NFT_QUEUE = 3 | ||
NFT_REPEAT = 4 | ||
NFT_STOP = 5 | ||
) | ||
|
||
// This code assembles the verdict structure, as expected by the | ||
// nftables netlink API. | ||
// For further information, consult: | ||
|
@@ -126,3 +135,37 @@ func (e *Verdict) unmarshal(data []byte) error { | |
} | ||
return ad.Err() | ||
} | ||
|
||
func (e *Verdict) String() string { | ||
var v string | ||
switch e.Kind { | ||
case unix.NFT_RETURN: | ||
v = "return" // -0x5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What did you want to express with these comments? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@stapelberg , just a note as to what the id is for the return. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you follow my other suggestion above regarding using the |
||
case unix.NFT_GOTO: | ||
v = "goto" // -0x4 | ||
case unix.NFT_JUMP: | ||
v = "jump" // NFT_JUMP = -0x3 | ||
case unix.NFT_BREAK: | ||
v = "break" // NFT_BREAK = -0x2 | ||
case unix.NFT_CONTINUE: | ||
v = "continue" // NFT_CONTINUE = -0x1 | ||
case NFT_DROP: | ||
v = "drop" | ||
case NFT_ACCEPT: | ||
v = "accept" | ||
case NFT_STOLEN: | ||
v = "stolen" | ||
case NFT_QUEUE: | ||
v = "queue" | ||
case NFT_REPEAT: | ||
v = "repeat" | ||
case NFT_STOP: | ||
v = "stop" | ||
default: | ||
v = fmt.Sprintf("verdict %v", e.Kind) | ||
} | ||
if e.Chain != "" { | ||
return v + " " + e.Chain | ||
} | ||
return v | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -252,12 +252,27 @@ func TestRuleOperations(t *testing.T) { | |
expr.VerdictDrop, | ||
} | ||
|
||
wantStrings := []string{ | ||
"queue", | ||
"accept", | ||
"queue", | ||
"accept", | ||
"drop", | ||
"drop", | ||
} | ||
|
||
for i, r := range rules { | ||
rr, _ := r.Exprs[0].(*expr.Verdict) | ||
|
||
if rr.Kind != want[i] { | ||
t.Fatalf("bad verdict kind at %d", i) | ||
} | ||
|
||
if rr.String() != wantStrings[i] { | ||
t.Fatalf("bad verdict string at %d: %s (received) vs. %s (expected)", i, rr.String(), wantStrings[i]) | ||
} | ||
|
||
t.Logf("%s", rr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove debugging left-over? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@stapelberg , 👍 will do prior to removing WIP. |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go test ./... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this file from the commit? Making testing a little easier is a good idea in general, but I’d like to avoid establishing precedent of shell scripts :) Maybe a “make test” target in a Makefile in a separate PR would be the best way forward? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@stapelberg , 👍 will do prior to removing WIP.
@stapelberg , that would be nice! I agree. |
||
go test -c github.com/google/nftables | ||
sudo ./nftables.test -test.v -run_system_tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did we not have these declared before? Where are they canonically defined in the upstream sources? Can you add a link please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no definition. They are defined with the following:
I am looking in https://github.com/torvalds/linux/blob/47ec5303d73ea344e84f46660fff693c57641386/include/uapi/linux/netfilter/nf_tables.h#L64-L70 ... yet to discover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another reference: https://github.com/torvalds/linux/blob/b71acb0e372160167bf6d5500b88b30b52ccef6e/include/uapi/linux/netfilter.h#L10-L17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stapelberg , in retrospect, after finding https://git.netfilter.org/libnftnl/tree/src/utils.c#n182, I say was mimicking this function 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stapelberg , also the NF_STOLEN comes from https://git.netfilter.org/libnftnl/tree/include/linux/netfilter.h#n9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After having another look, I think this duplicates the verdict codes we already have defined here, no?
nftables/expr/verdict.go
Lines 39 to 54 in c25e4f6
Why not just use e.g. VerdictReturn in the switch statement below in String()?