forked from hi-glenn/xdp_acl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdp_rule.go
176 lines (145 loc) · 4.17 KB
/
xdp_rule.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"encoding/binary"
"fmt"
"net/netip"
"runtime"
"unsafe"
"github.com/cilium/ebpf"
"golang.org/x/sync/errgroup"
)
func (x *xdp) doStoreRules(rules *Rules, prevHitcount map[uint32]uint64) error {
var errg errgroup.Group
errg.Go(func() error {
return storePortRules(rules.srcPortPriorities, x.updatableObjs.SportV4, rules.BitmapArraySize)
})
errg.Go(func() error {
return storePortRules(rules.dstPortPriorities, x.updatableObjs.DportV4, rules.BitmapArraySize)
})
errg.Go(func() error {
return storeAddrRules(rules.srcCIDRPriorities, x.updatableObjs.SrcV4, rules.BitmapArraySize)
})
errg.Go(func() error {
return storeAddrRules(rules.dstCIDRPriorities, x.updatableObjs.DstV4, rules.BitmapArraySize)
})
errg.Go(func() error {
return storeProtocolRules(rules.protocolPriorities, x.updatableObjs.ProtoV4, rules.BitmapArraySize)
})
errg.Go(func() error {
return storeActions(rules.priorityActions, rules, x.updatableObjs.RuleActionV4, prevHitcount)
})
err := errg.Wait()
if err != nil {
return fmt.Errorf("failed to store rules: %w", err)
}
return nil
}
func storePortRules(ports [][]uint32, m *ebpf.Map, bitmapArraySize int) error {
for port, priorities := range ports {
// ignore the missing-priorities ports
if len(priorities) == 0 {
continue
}
b := newBitmap(bitmapArraySize)
for _, prio := range priorities {
b.Set(prio)
}
k := htons(uint16(port))
v, _ := b.MarshalBinary()
if err := m.Put(k, v); err != nil {
return fmt.Errorf("failed to update rules of port(%d) to bpf map: %w", port, err)
}
}
return nil
}
func htons(n uint16) uint16 {
b := *(*[2]uint8)(unsafe.Pointer(&n))
return binary.BigEndian.Uint16(b[:])
}
func storeAddrRules(addrs map[netip.Prefix][]uint32, m *ebpf.Map, bitmapArraySize int) error {
type lpmKey struct {
Prefix uint32
Addr [4]uint8
}
for cidr, priorities := range addrs {
b := newBitmap(bitmapArraySize)
for _, prio := range priorities {
b.Set(prio)
}
var k lpmKey
k.Prefix = uint32(cidr.Bits())
k.Addr = cidr.Addr().As4() // network order
v, _ := b.MarshalBinary()
if err := m.Put(k, v); err != nil {
return fmt.Errorf("failed to update rules of CIDR(%s) to bpf map: %w", cidr, err)
}
}
return nil
}
func storeProtocolRules(protos map[uint32][]uint32, m *ebpf.Map, bitmapArraySize int) error {
for proto, priorities := range protos {
b := newBitmap(bitmapArraySize)
for _, prio := range priorities {
b.Set(prio)
}
v, _ := b.MarshalBinary()
if err := m.Put(proto, v); err != nil {
return fmt.Errorf("failed to update rules of protocol(%d) to bpf map: %w", proto, err)
}
}
return nil
}
type actionValue struct {
Action uint64
Count uint64
}
var numCPU = runtime.NumCPU()
func getActionValue(action uint8, count uint64) []actionValue {
val := make([]actionValue, numCPU)
for i := range val {
val[i].Action = uint64(action)
val[i].Count = count
}
return val
}
// storeActions retrieves the old-hit-count-data from oldMap, and updates it to m.
func storeActions(actions []uint8, r *Rules, m *ebpf.Map, prevHitCount map[uint32]uint64) error {
for priority, action := range actions {
key, val := uint32(priority), getActionValue(action, prevHitCount[r.GetRealPriority(uint32(priority))])
if err := m.Put(key, val); err != nil {
return fmt.Errorf("failed to update action of priority(%d) to bpf map: %w", priority, err)
}
}
return nil
}
func getHitCount(m *ebpf.Map, r *Rules) (map[uint32]uint64, error) {
counts, err := retrieveHitCount(m, len(r.rules))
if err != nil {
return nil, err
}
hits := make(map[uint32]uint64, len(counts))
for i, c := range counts {
hits[r.GetRealPriority(uint32(i))] = c
}
return hits, nil
}
func retrieveHitCount(m *ebpf.Map, ruleNum int) ([]uint64, error) {
counts := make([]uint64, ruleNum)
sumOfVal := func(val []actionValue) uint64 {
var sum uint64
for _, val := range val {
sum += val.Count
}
return sum
}
var err error
value := make([]actionValue, numCPU)
for key := uint32(0); key < uint32(ruleNum); key++ {
err = m.Lookup(key, &value)
if err != nil {
return counts, fmt.Errorf("failed to retrieve hit count: %w", err)
}
counts[key] = sumOfVal(value)
}
return counts, nil
}