-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
343 lines (305 loc) · 7.75 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"encoding/binary"
"errors"
"flag"
"fmt"
"net"
"os"
"unsafe"
"github.com/cilium/ebpf"
)
const MAXLEN = 2000
// ifindex,mac address mapping for the interfaces
type entry struct {
ifIdx uint32
mac net.HardwareAddr
ip uint32
}
// cntPkt resembles cntPkt in ebpf kernel code
type cntPkt struct {
drop uint32
pass uint32
}
type statEntry struct {
ifIdx uint32
count cntPkt
}
func Ip2long(ipAddr string) (uint32, error) {
ip := net.ParseIP(ipAddr)
if ip == nil {
return 0, errors.New("wrong ipAddr format")
}
ip = ip.To4()
return binary.LittleEndian.Uint32(ip), nil
}
func Long2ip(ipLong uint32) string {
ipByte := make([]byte, 4)
binary.LittleEndian.PutUint32(ipByte, ipLong)
ip := net.IP(ipByte)
return ip.String()
}
func initializeStatsMap(m *ebpf.Map, entries []uint32) error {
fmt.Printf("initStatsMap : Info: %v keysize: %v valueSize: %v", m.String(), m.KeySize(), m.ValueSize())
for _, entry := range entries {
cntPkt := cntPkt{drop: 0, pass: 0}
err := m.Put(entry, (cntPkt))
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}
return nil
}
func makeEntry(ifIdx uint32, mac net.HardwareAddr, ip uint32) *entry {
var en entry
en.ifIdx = ifIdx
en.mac = mac
en.ip = ip
fmt.Printf("created an entry with id %v, mac %s, ip %v\n", ifIdx, mac, ip)
return &en
}
func getAllMACs() ([]entry, error) {
ifas, err := net.Interfaces()
if err != nil {
return nil, err
}
entries := []entry{}
for _, ifa := range ifas {
a := ifa.HardwareAddr.String()
if a != "" {
fmt.Printf("ifIndex: %v macAddr: %v size_mac: %d\n",
ifa.Index, ifa.HardwareAddr, int(unsafe.Sizeof(ifa.HardwareAddr)))
e := makeEntry(uint32(ifa.Index), ifa.HardwareAddr, 0)
entries = append(entries, *e)
}
}
return entries, nil
}
func getInterface(idx int) (*net.Interface, error) {
ifa, err := net.InterfaceByIndex(idx)
if err != nil {
fmt.Printf("Error: %v", err.Error())
return nil, err
}
return ifa, nil
}
// Returns indices of interfaces
func getAllIfaceIndices() ([]uint32, error) {
ifas, err := net.Interfaces()
if err != nil {
return nil, err
}
entries := []uint32{}
for _, ifa := range ifas {
a := ifa.HardwareAddr.String()
if a != "" {
fmt.Printf("ifIndex: %v macAddr: %v size_mac: %d\n",
ifa.Index, ifa.HardwareAddr, int(unsafe.Sizeof(ifa.HardwareAddr)))
entries = append(entries, uint32(ifa.Index))
}
}
return entries, nil
}
// This will overwrite previous entry if any
func addEntryMacMap(m *ebpf.Map, entries []entry, rand int) error {
for _, ifa := range entries {
err := m.Put(ifa.ifIdx+uint32(rand), []byte(ifa.mac))
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}
return nil
}
// This will overwrite previous entry if any
func addEntryIpMap(m *ebpf.Map, entries []entry, rand int) error {
for _, ifa := range entries {
err := m.Put(ifa.ifIdx+uint32(rand), ifa.ip)
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}
return nil
}
// This will ignore missing entries and always return success
func delEntryMap(m *ebpf.Map, keys []interface{}) error {
for _, ifa := range keys {
var err error
switch ifa.(type) {
case uint32:
err = m.Delete(ifa.(uint32))
case string:
err = m.Delete(ifa.(string))
}
fmt.Printf("[delMap] ifIdx: %v\n", ifa)
if err != nil {
fmt.Printf("[delMap] Warn: %v\n", err)
}
}
return nil
}
func createArray(maxEntries int, keySize int, valueSize int) (*ebpf.Map, error) {
fmt.Printf("KeySize: %d ValueSize: %d MaxEntries: %d\n", keySize, valueSize, maxEntries)
m, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Hash,
KeySize: uint32(keySize),
ValueSize: uint32(valueSize),
MaxEntries: uint32(maxEntries),
})
if err != nil {
return nil, err
}
return m, nil
}
func pinMap(m *ebpf.Map, path string) error {
if err := m.Pin(path); err != nil {
m.Close()
//fmt.Printf("[pinMap] Error! pin map: %s\n", err)
return err
}
return nil
}
func closeMap(m *ebpf.Map) error {
return m.Close()
}
func getMap(path string) (*ebpf.Map, error) {
return ebpf.LoadPinnedMap(path)
}
func pinOrGetMap(path string, m *ebpf.Map) (*ebpf.Map, error) {
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
err = pinMap(m, path)
if err != nil {
//fmt.Printf("Error! PinOrGetMap map: %s\n", err)
return m, err
}
return m, nil
} else {
temp, err := getMap(path)
if err != nil {
//fmt.Printf("Error! PinOrGetMap map: %s\n", err)
return m, err
}
return temp, nil
}
}
// We are not unpinning the map XXX
// We should Freeze() userspace to avoid maniplulation XXX
// Userspace should keep updating interfaces when they come and go down? So dont freeze()?? XXX
func main() {
var mode string
var idx int
var pod_mac string
var pod_ip uint32
var arg_ip string
flag.StringVar(&mode, "mode", "init", "Mode can be init or add")
flag.IntVar(&idx, "idx", 0, "iface index where tc hook is attached")
flag.StringVar(&pod_mac, "pod_mac", "invalid", "MAC address which is allowed to pass through idx")
flag.StringVar(&arg_ip, "pod_ip", "invalid", "IP address of pod which is allowed to pass through idx")
flag.Parse()
fmt.Printf("Arguments - mode: %v idx: %v pod_mac: %v pod_ip: %v\n", mode, idx, pod_mac, arg_ip)
pod_ip, err := Ip2long(arg_ip)
if err != nil {
fmt.Printf("Error while converting %s to ip address", arg_ip)
fmt.Println(err)
return
}
mapPathDir := "/sys/fs/bpf/tc/globals/"
ifaceMacMapPath := "/sys/fs/bpf/tc/globals/iface_map"
ifaceIpMapPath := "/sys/fs/bpf/tc/globals/iface_ip_map"
countMapPath := "/sys/fs/bpf/tc/globals/iface_stat_map"
var ip_map *ebpf.Map
var mac_map *ebpf.Map
var m *ebpf.Map
var stats_map *ebpf.Map
var en entry
var ct cntPkt
err = os.MkdirAll(mapPathDir, os.ModePerm)
if err != nil {
fmt.Printf("Error while creating the directory %s", err)
return
}
mac_map, err = createArray(MAXLEN,
//len(macArr),
int(unsafe.Sizeof(en.ifIdx)),
//int(unsafe.Sizeof(en.mac)))
6)
if err != nil {
fmt.Printf("Create Map returned error %s\n", err)
return
}
mac_map, err = pinOrGetMap(ifaceMacMapPath, mac_map)
if err != nil {
fmt.Printf("Error! create map: %s\n", err)
return
}
ip_map, err = createArray(MAXLEN,
int(unsafe.Sizeof(en.ifIdx)),
4)
if err != nil {
fmt.Printf("Create Map returned error %s\n", err)
return
}
ip_map, err = pinOrGetMap(ifaceIpMapPath, ip_map)
if err != nil {
fmt.Printf("Error! create map: %s\n", err)
return
}
stats_map, err = createArray(MAXLEN, int(unsafe.Sizeof(en.ifIdx)), int(unsafe.Sizeof(ct)))
stats_map, err = pinOrGetMap(countMapPath, stats_map)
if err != nil {
fmt.Printf("Error! create map: %s\n", err)
return
}
switch mode {
case "init":
macArr, err := getAllMACs()
if err != nil || len(macArr) == 0 {
return
}
err = addEntryMacMap(mac_map, macArr, 0)
if err != nil {
fmt.Printf("Error! populating map: %s\n", err)
return
}
ifaceIndices, err := getAllIfaceIndices()
initializeStatsMap(stats_map, ifaceIndices)
case "add":
ifa, err := getInterface(idx)
if err != nil {
fmt.Printf("Could not get interface %v\n", err.Error())
os.Exit(1)
}
entries := []entry{}
hwa, err := net.ParseMAC(pod_mac)
if err != nil {
hwa = ifa.HardwareAddr
}
e := makeEntry(uint32(ifa.Index), hwa, pod_ip)
entries = append(entries, *e)
err = addEntryMacMap(mac_map, entries, 0)
if err != nil {
fmt.Printf("Error! populating map: %s\n", err)
return
}
err = addEntryIpMap(ip_map, entries, 0)
if err != nil {
fmt.Printf("Error! populating map: %s\n", err)
return
}
//Initialize stats maps for idx
cntPkt := cntPkt{drop: 0, pass: 0}
err = stats_map.Put(uint32(ifa.Index), (cntPkt))
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
}
err = closeMap(m)
if err != nil {
fmt.Printf("Error! closing map: %s\n", err)
return
}
return
}