[WIP] nf2go: convert nftables rules to golang code#298
Draft
aojea wants to merge 1 commit intogoogle:mainfrom
Draft
[WIP] nf2go: convert nftables rules to golang code#298aojea wants to merge 1 commit intogoogle:mainfrom
aojea wants to merge 1 commit intogoogle:mainfrom
Conversation
Contributor
Author
|
cc: @grosskur |
Contributor
Author
|
@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 <aojea@google.com>
stapelberg
reviewed
Feb 3, 2025
Collaborator
stapelberg
left a comment
There was a problem hiding this comment.
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:] |
|
|
||
| // Format the generated code | ||
| log.Printf("formating file: %s", tempGoFile) | ||
| cmd := exec.Command("gofmt", "-w", "-s", tempGoFile) |
Collaborator
There was a problem hiding this comment.
use https://pkg.go.dev/go/format#Source instead of shelling out to gofmt
There was a problem hiding this comment.
That has a tradeoff in respecting the current GOTOOLCHAIN at the time of execution vs at the time of building this binary?
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 rulesetand 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.goNote
There are some mismatches that are caused by different order or name interpretation, per example