-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
97 lines (66 loc) · 1.68 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
package main
import (
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
"golang.org/x/sys/unix"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc=clang XDPACL xdp_acl.c -- -I./include -nostdinc -Wno-unused-value -Wno-compare-distinct-pointer-types -o3
var wgGlobal sync.WaitGroup
func xdpACLIinit() {
// 日志初始化
InitLogger()
NumCPU = runtime.NumCPU()
zlog.Info("cpu core nums: ", NumCPU)
// 加载本地规则文件
preOriginalRules()
zlog.Info("ruleBuffer len: ", len(newOpsBuffer))
// 初始化 webSignal
webSignal = make(chan int)
}
func setResourceLimit() {
if err := unix.Setrlimit(unix.RLIMIT_MEMLOCK, &unix.Rlimit{
Cur: unix.RLIM_INFINITY,
Max: unix.RLIM_INFINITY,
}); err != nil {
zlog.Error(err.Error() + "; Failed to adjust rlimit")
panic(err)
}
}
func holdApp() {
quitSignalChan := make(chan os.Signal, 1)
signal.Notify(quitSignalChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
zlog.Info("XDP program successfully loaded and attached.")
zlog.Info("Press CTRL+C to stop.")
for range quitSignalChan {
// close quitSignalChan
close(quitSignalChan)
webSignal <- 1
// close newOpsBuffer
close(newOpsBuffer)
wgGlobal.Wait()
zlog.Info("stop acl app")
}
}
func main() {
b := time.Now()
// 解析命令行参数
cmdLineInputParamsInit()
xdpACLIinit()
defer zlog.Sync()
zlog.Info("dev: ", opt.dev)
checkNetDevAndGenLink()
setResourceLimit()
fillXdpObjs()
defer objs.Close()
loadOriginalRules()
loadXdpOnLink()
defer unLoadAllXdpFromLink()
go loadImmediateRules("loadImmediateRules")
go webInit(&opt)
zlog.Infof("🍉🍉 name: %s. Cost=%+v.", "app", time.Since(b))
holdApp()
}