forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_flower_test.go
110 lines (97 loc) · 2.31 KB
/
example_flower_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//go:build linux
// +build linux
package tc_test
import (
"fmt"
"net"
"os"
"github.com/xcxinng/go-tc"
"github.com/xcxinng/go-tc/core"
"github.com/xcxinng/go-tc/internal/unix"
"github.com/jsimonetti/rtnetlink"
)
func ExampleFlower() {
tcIface := "ExampleFlower"
rtnl, err := setupDummyInterface(tcIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not setup dummy interface: %v\n", err)
return
}
defer rtnl.Close()
devID, err := net.InterfaceByName(tcIface)
if err != nil {
fmt.Fprintf(os.Stderr, "could not get interface ID: %v\n", err)
return
}
defer func(devID uint32, rtnl *rtnetlink.Conn) {
if err := rtnl.Link.Delete(devID); err != nil {
fmt.Fprintf(os.Stderr, "could not delete interface: %v\n", err)
}
}(uint32(devID.Index), rtnl)
tcnl, err := tc.Open(&tc.Config{})
if err != nil {
fmt.Fprintf(os.Stderr, "could not open rtnetlink socket: %v\n", err)
return
}
defer func() {
if err := tcnl.Close(); err != nil {
fmt.Fprintf(os.Stderr, "could not close rtnetlink socket: %v\n", err)
}
}()
qdisc := tc.Object{
tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: uint32(devID.Index),
Handle: core.BuildHandle(tc.HandleRoot, 0),
Parent: tc.HandleIngress,
Info: 0,
},
tc.Attribute{
Kind: "clsact",
},
}
if err := tcnl.Qdisc().Add(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not assign clsact to lo: %v\n", err)
return
}
defer func() {
if err := tcnl.Qdisc().Delete(&qdisc); err != nil {
fmt.Fprintf(os.Stderr, "could not delete qdisc from iface (%d): %v\n", devID.Index, err)
return
}
}()
srcMac, _ := net.ParseMAC("00:00:5e:00:53:01")
actions := []*tc.Action{
{
Kind: "gact",
Gact: &tc.Gact{
Parms: &tc.GactParms{
Action: 2, // action drop
},
},
},
}
filter := tc.Object{
tc.Msg{
Family: unix.AF_UNSPEC,
Ifindex: uint32(devID.Index),
Handle: 0,
Parent: tc.HandleIngress + 1,
Info: 768,
},
tc.Attribute{
Kind: "flower",
Flower: &tc.Flower{
KeyEthSrc: &srcMac,
Actions: &actions,
},
},
}
// tc filter add dev ExampleFlower ingress protocol all prio 1 \
// flower src_mac 00:00:5e:00:53:01 \
// action gact drop
if err := tcnl.Filter().Add(&filter); err != nil {
fmt.Fprintf(os.Stderr, "could not assign flower filter to iface (%d): %v\n", devID.Index, err)
return
}
}