forked from iovisor/iomodules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.go
123 lines (116 loc) · 3.27 KB
/
patch.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
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2015 PLUMgrid
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// vim: set ts=8:sts=8:sw=8:noet
package hover
import (
"fmt"
"syscall"
"github.com/vishvananda/netlink"
)
func ensureQdisc(link netlink.Link, qdiscType string, handle, parent uint32) (netlink.Qdisc, error) {
qds, err := netlink.QdiscList(link)
if err != nil {
return nil, err
}
for _, q := range qds {
if q.Attrs().Handle == handle {
//Debug.Printf("Found existing ingress qdisc %x\n", q.Attrs().Handle)
return q, nil
}
}
qdisc := &netlink.GenericQdisc{
QdiscAttrs: netlink.QdiscAttrs{
LinkIndex: link.Attrs().Index,
Handle: handle,
Parent: parent,
},
QdiscType: qdiscType,
}
if err := netlink.QdiscAdd(qdisc); err != nil {
return nil, fmt.Errorf("failed ensuring qdisc: %v", err)
}
return qdisc, nil
}
func ensureIngressFd(link netlink.Link, fd int) error {
q, err := ensureQdisc(link, "ingress", netlink.MakeHandle(0xffff, 0), netlink.HANDLE_INGRESS)
if err != nil {
return err
}
fHandle := netlink.MakeHandle(0, 1)
filter := &netlink.U32{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: q.Attrs().Handle,
Priority: 1,
Protocol: syscall.ETH_P_ALL,
},
Actions: []netlink.Action{
&netlink.BpfAction{Fd: fd, Name: "bpf1"},
},
ClassId: fHandle,
}
filters, err := netlink.FilterList(link, netlink.HANDLE_MIN_INGRESS)
if err != nil {
return fmt.Errorf("failed fetching ingress filter list: %s", err)
}
for _, f := range filters {
if f, ok := f.(*netlink.U32); ok {
if f.ClassId == fHandle {
return nil
}
// remove all previous filters to ensure the
// current one is the only one to be executed
netlink.FilterDel(f)
}
}
if err := netlink.FilterAdd(filter); err != nil {
return fmt.Errorf("failed adding ingress filter: %s", err)
}
//Debug.Printf("ensureIngressFd(%s) success\n", link.Attrs().Name)
return nil
}
func ensureEgressFd(link netlink.Link, fd int) error {
q, err := ensureQdisc(link, "fq_codel", netlink.MakeHandle(1, 0), netlink.HANDLE_ROOT)
if err != nil {
return err
}
fHandle := netlink.MakeHandle(0, 2)
filter := &netlink.U32{
FilterAttrs: netlink.FilterAttrs{
LinkIndex: link.Attrs().Index,
Parent: q.Attrs().Handle,
Priority: 1,
Protocol: syscall.ETH_P_ALL,
},
Actions: []netlink.Action{
&netlink.BpfAction{Fd: fd, Name: "bpf1"},
},
ClassId: fHandle,
}
filters, err := netlink.FilterList(link, netlink.HANDLE_MIN_EGRESS)
if err != nil {
return fmt.Errorf("failed fetching egress filter list: %s", err)
}
for _, f := range filters {
if f, ok := f.(*netlink.U32); ok {
if f.ClassId == fHandle {
return nil
}
}
}
if err := netlink.FilterAdd(filter); err != nil {
return fmt.Errorf("failed adding egress filter: %v", err)
}
return nil
}