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

Add bitwise decoder logic #71

Merged
merged 8 commits into from
Nov 7, 2019
Merged

Conversation

sbezverk
Copy link
Contributor

Signed-off-by: Serguei Bezverkhi sbezverk@cisco.com
This PR partially implements: #70

@sbezverk
Copy link
Contributor Author

@stapelberg Please review when you have a chance, thank you.

@sbezverk
Copy link
Contributor Author

I tested this change in my e2e testing.

Initial programmed tables/chains/rules: {"Name":"nftables_ipv4","Use":0,"Flags":0,"Family":2}{"Name":"chain-1","Table":{"Name":"nftables_ipv4","Use":0,"Flags":0,"Family":2},"Hooknum":1,"Priority":0,"Type":"filter","Policy":1}[{"DestRegister":1,"Base":"expr.PayloadBaseNetworkHeader","Len":1,"Offset":9},{"Op":"expr.CmpOpEq","Register":1,"Data":["0x1"]},{"DestRegister":1,"Base":"expr.PayloadBaseNetworkHeader","Len":4,"Offset":16},{"SourceRegister":1,"DestRegister":1,"Len":4,"Mask":["0xff","0xff","0x0","0x0"],"Xor":["0x0","0x0","0x0","0x0"]},{"Op":"expr.CmpOpEq","Register":1,"Data":["0x1","0x1","0x0","0x0"]},{"Kind":"0x0"}]
Learned rules for table: nftables_ipv4, chain: chain-1 rules: [{"DestRegister":1,"Base":"expr.PayloadBaseNetworkHeader","Len":1,"Offset":9},{"Op":"expr.CmpOpEq","Register":1,"Data":["0x1"]},{"DestRegister":1,"Base":"expr.PayloadBaseNetworkHeader","Len":4,"Offset":16},{"SourceRegister":1,"DestRegister":1,"Len":4,"Mask":["0xff","0xff","0x0","0x0"],"Xor":["0x0","0x0","0x0","0x0"]},{"Op":"expr.CmpOpEq","Register":1,"Data":["0x1","0x1","0x0","0x0"]},{"Kind":"0x0"}]

@stapelberg
Copy link
Collaborator

Looks good. Can you also add a unit test please?

@sbezverk
Copy link
Contributor Author

@stapelberg Adding a unit test is not straight forward in this scenario, as to test it, a rule really needs to be programmed and then get it and then parsed. I will see what else can be done.

Copy link
Contributor

@mdlayher mdlayher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
@sbezverk
Copy link
Contributor Author

sbezverk commented Nov 1, 2019

@stapelberg any chance you could merge it please?

@sbezverk
Copy link
Contributor Author

sbezverk commented Nov 4, 2019

@stapelberg could you please merge it?

data, err := tt.bw.marshal()
if err != nil {
t.Errorf("Test \"%s\" failed to marshal Bitwise struct with error: %+v", tt.name, err)
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of t.Errorf+continue and prefixing each error with the tests name, please use subtests with t.Fatalf: http://blog.golang.org/subtests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem to be addressed yet? I don’t see you using a subtest even in the latest changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check diff again. it has all been done. Not sure why you do not see it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t see a subtest (t.Run), even after clearing my browser cache. Looking into your branch, I don’t see it either: https://github.com/sbezverk/nftables/blob/add_bitwise_decoder/expr/bitwise_test.go

Did you forget to push a commit, or misunderstand what I meant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/sbezverk/nftables/blob/add_bitwise_decoder/expr/bitwise_test.go#L36

	for _, tt := range tests {
		nbw := Bitwise{}
		data, err := tt.bw.marshal()
		if err != nil {
			t.Fatalf("Test \"%s\" failed to marshal Bitwise struct with error: %+v", tt.name, err)

		}

I tried force push, but it says no changes. Not sure what else can be done..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your code excerpt, I don’t see t.Run either?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I thought your comment was just about Errorf/continue vs Fatalf.

github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a/go.mod h1:Oz+70psSo5OFh8DBl0Zv2ACw7Esh6pPUphlvZG9x7uw=
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? Can you run go mod tidy please? Alternatively, try reverting your changes to go.sum and rebuild.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run go mod tidy

go mod tidy
go: downloading github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a
go: extracting github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a

"golang.org/x/sys/unix"
)

func TestBitwise(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark this test as parallelizable by calling t.Parallel() as first statement please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

DestRegister: 2,
Len: 4,
Xor: []byte{0x0, 0x0, 0x0, 0x0},
Mask: []byte{0xff, 0xff, 0x0, 0x0},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this how one matches a /16 IPv4 subnet mask? If so, add a comment please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
@sbezverk
Copy link
Contributor Author

sbezverk commented Nov 6, 2019

@stapelberg comments are addressed in the latest revision, PTAL.

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
nbw := Bitwise{}
data, err := tt.bw.marshal()
if err != nil {
t.Fatalf("Test \"%s\" failed to marshal Bitwise struct with error: %+v", tt.name, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below: prefixing the test name manually is now superfluous with subtests, which automatically include the test name.

Please use something like:

t.Fatalf("marshal: %v", err)

Signed-off-by: Serguei Bezverkhi <sbezverk@cisco.com>
@stapelberg stapelberg merged commit e0f4f3f into google:master Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants