-
Notifications
You must be signed in to change notification settings - Fork 140
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] nf2go: convert nftables rules to golang code #298
base: main
Are you sure you want to change the base?
Conversation
cc: @grosskur |
@stapelberg feel free to suggest directions, right now is very hacky but is a really useful tools to be able to use this library more efficiently |
5dae564
to
444f1f1
Compare
One of the biggest barriers to adopt the netlink format for nftables is the complexity of writing bytecode. This commits adds a tool that allows to take an nftables dump and generate the corresponding golang code and validating that the generated code produces the exact same output. Change-Id: I491b35e0d8062de33c67091dd4126d843b231838 Signed-off-by: Antonio Ojea <[email protected]>
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.
Seems fine to me overall. My main concerns are not introducing new dependencies for this helper tool and not having to extend the public API of the package.
I left two small pointers but will only review in detail once you say it’s ready.
) | ||
|
||
func main() { | ||
args := os.Args[1:] |
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.
use flag.Parse :)
|
||
// Format the generated code | ||
log.Printf("formating file: %s", tempGoFile) | ||
cmd := exec.Command("gofmt", "-w", "-s", tempGoFile) |
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.
use https://pkg.go.dev/go/format#Source instead of shelling out to gofmt
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.
That has a tradeoff in respecting the current GOTOOLCHAIN at the time of execution vs at the time of building this binary?
One of the biggest barriers to adopt the netlink format for nftables is the complexity of writing bytecode.
This commits adds a tool that allows to take an nftables dump and generate the corresponding golang code and validating that the generated code produces the exact same output.
How to use it
pass as parameter the dump obtained via
nft list ruleset
and it will provide the generated go code in stdout and also report the differences , since there may be bugs or misinterpratations.go run main.go rules_simple.txt > generated_nft.go
Note
There are some mismatches that are caused by different order or name interpretation, per example