Skip to content

Commit 264d9cf

Browse files
authored
add helper for Filter.Msg.Info (#209)
Signed-off-by: Florian Lehner <dev@der-flo.net>
1 parent 89c67ca commit 264d9cf

File tree

6 files changed

+55
-4
lines changed

6 files changed

+55
-4
lines changed

core/handle.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ func SplitHandle(handle uint32) (major, minor uint32) {
1717
minor = handle & handleMinMask
1818
return major, minor
1919
}
20+
21+
// FilterInfo is a simple helper to set the priority and protocol of the filter
22+
func FilterInfo(priority uint16, protocol uint16) uint32 {
23+
return BuildHandle(uint32(priority), uint32(endianSwapUint16(protocol)))
24+
}
25+
26+
func endianSwapUint16(in uint16) uint16 {
27+
return (in << 8) | (in >> 8)
28+
}

core/handle_test.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package core
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/florianl/go-tc/internal/unix"
7+
)
48

59
// Tests out the HandleStr function
610
func TestSplitHandle(t *testing.T) {
@@ -43,3 +47,30 @@ func TestBuildHandle(t *testing.T) {
4347
})
4448
}
4549
}
50+
51+
func TestFilterInfo(t *testing.T) {
52+
tests := map[string]struct {
53+
prio uint16
54+
proto uint16
55+
info uint32
56+
}{
57+
"protocol ip prio 73": {
58+
prio: 73,
59+
proto: unix.ETH_P_IP,
60+
info: 0x490008,
61+
},
62+
"protocol all prio 0": {
63+
prio: 0,
64+
proto: unix.ETH_P_ALL,
65+
info: 768,
66+
},
67+
}
68+
for name, tt := range tests {
69+
t.Run(name, func(t *testing.T) {
70+
info := FilterInfo(tt.prio, tt.proto)
71+
if info != tt.info {
72+
t.Errorf("Expected 0x%x but got 0x%x", tt.info, info)
73+
}
74+
})
75+
}
76+
}

example_flower_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func ExampleFlower() {
8989
Ifindex: uint32(devID.Index),
9090
Handle: 0,
9191
Parent: tc.HandleIngress + 1,
92-
Info: 768,
92+
Info: core.FilterInfo(0, unix.ETH_P_ALL),
9393
},
9494
tc.Attribute{
9595
Kind: "flower",

example_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func Example_cBPF() {
164164
Ifindex: uint32(devID.Index),
165165
Handle: 0,
166166
Parent: tc.HandleIngress,
167-
Info: 0x300,
167+
Info: core.FilterInfo(0, unix.ETH_P_ALL),
168168
},
169169
tc.Attribute{
170170
Kind: "bpf",
@@ -270,7 +270,7 @@ func ExampleU32() {
270270
Ifindex: uint32(devID.Index),
271271
Handle: 0,
272272
Parent: tc.HandleIngress,
273-
Info: 0x300,
273+
Info: core.FilterInfo(0, unix.ETH_P_ALL),
274274
},
275275
tc.Attribute{
276276
Kind: "bpf",

internal/unix/types_linux.go

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ const (
5151
RTM_DELACTION = linux.RTM_DELACTION
5252
RTM_GETACTION = linux.RTM_GETACTION
5353
)
54+
55+
// For tests:
56+
const (
57+
ETH_P_IP = linux.ETH_P_IP
58+
ETH_P_ALL = linux.ETH_P_ALL
59+
)

internal/unix/types_other.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ const (
4949
RTM_DELACTION = 49
5050
RTM_GETACTION = 50
5151
)
52+
53+
const (
54+
ETH_P_IP = 0x800
55+
ETH_P_ALL = 0x3
56+
)

0 commit comments

Comments
 (0)