forked from intel/istio-tcpip-bypass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (161 loc) · 4.66 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
/* Copyright (C) 2021 Intel Corporation
* SPDX-License-Identifier: Apache-2.0
*/
package main
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -target bpf -cflags "-D__TARGET_ARCH_x86" bpf_redir bpf/bpf_redir.c
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc clang -target bpf -cflags "-D__TARGET_ARCH_x86" bpf_sockops bpf/bpf_sockops.c
import (
"fmt"
"os"
"os/signal"
"path/filepath"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
v2 "github.com/containers/common/pkg/cgroupv2"
"golang.org/x/sys/unix"
)
const (
FilesystemTypeBPFFS = unix.BPF_FS_MAGIC
MapsRoot = "/sys/fs/bpf"
MapsPinpath = "/sys/fs/bpf/tcpip-bypass"
)
type BypassProgram struct {
sockops_Obj bpf_sockopsObjects
redir_Obj bpf_redirObjects
SockopsCgroup link.Link
}
func setLimit() error {
var err error = nil
err = unix.Setrlimit(unix.RLIMIT_MEMLOCK,
&unix.Rlimit{
Cur: unix.RLIM_INFINITY,
Max: unix.RLIM_INFINITY,
})
if err != nil {
fmt.Println("failed to set rlimit:", err)
}
return err
}
func getCgroupPath() (string, error) {
var err error = nil
cgroupPath := "/sys/fs/cgroup"
enabled, err := v2.Enabled()
if !enabled {
cgroupPath = filepath.Join(cgroupPath, "unified")
}
return cgroupPath, err
}
func loadProgram(prog BypassProgram) (BypassProgram, error) {
var err error
var options ebpf.CollectionOptions
err = os.Mkdir(MapsPinpath, os.ModePerm)
if err != nil {
fmt.Println(err)
}
options.Maps.PinPath = MapsPinpath
if err = loadBpf_redirObjects(&prog.redir_Obj, &options); err != nil {
fmt.Println("Error load objects:", err)
}
if err = link.RawAttachProgram(link.RawAttachProgramOptions{
Target: prog.redir_Obj.bpf_redirMaps.MapRedir.FD(),
Program: prog.redir_Obj.bpf_redirPrograms.BpfRedirProxy,
Attach: ebpf.AttachSkMsgVerdict,
}); err != nil {
fmt.Printf("Error attaching to sockmap: %s\n", err)
}
if err = loadBpf_sockopsObjects(&prog.sockops_Obj, &options); err != nil {
fmt.Println("Error load objects:", err)
}
if cgroupPath, err := getCgroupPath(); err == nil {
prog.SockopsCgroup, err = link.AttachCgroup(link.CgroupOptions{
Path: cgroupPath,
Attach: ebpf.AttachCGroupSockOps,
Program: prog.sockops_Obj.bpf_sockopsPrograms.BpfSockmap,
})
if err != nil {
fmt.Printf("Error attaching sockops to cgroup: %s", err)
}
}
return prog, err
}
func closeProgram(prog BypassProgram) {
var err error
if prog.SockopsCgroup != nil {
fmt.Printf("Closing sockops cgroup...\n")
prog.SockopsCgroup.Close()
}
if prog.redir_Obj.bpf_redirPrograms.BpfRedirProxy != nil {
err = link.RawDetachProgram(link.RawDetachProgramOptions{
Target: prog.redir_Obj.bpf_redirMaps.MapRedir.FD(),
Program: prog.redir_Obj.bpf_redirPrograms.BpfRedirProxy,
Attach: ebpf.AttachSkMsgVerdict,
})
if err != nil {
fmt.Printf("Error detaching '%s'\n", err)
}
fmt.Printf("Closing redirect prog...\n")
}
if prog.sockops_Obj.bpf_sockopsMaps.MapActiveEstab != nil {
prog.sockops_Obj.bpf_sockopsMaps.MapActiveEstab.Unpin()
prog.sockops_Obj.bpf_sockopsMaps.MapActiveEstab.Close()
}
if prog.sockops_Obj.bpf_sockopsMaps.MapProxy != nil {
prog.sockops_Obj.bpf_sockopsMaps.MapProxy.Unpin()
prog.sockops_Obj.bpf_sockopsMaps.MapProxy.Close()
}
if prog.sockops_Obj.bpf_sockopsMaps.MapRedir != nil {
prog.sockops_Obj.bpf_sockopsMaps.MapRedir.Unpin()
prog.sockops_Obj.bpf_sockopsMaps.MapRedir.Close()
}
if prog.sockops_Obj.bpf_sockopsMaps.DebugMap != nil {
prog.sockops_Obj.bpf_sockopsMaps.DebugMap.Unpin()
prog.sockops_Obj.bpf_sockopsMaps.DebugMap.Close()
}
}
func checkOrMountBPFFSDefault() error {
var err error
_, err = os.Stat(MapsRoot)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(MapsRoot, 0755); err != nil {
return fmt.Errorf("unable to create bpf mount directory: %s", err)
}
}
}
fst := unix.Statfs_t{}
err = unix.Statfs(MapsRoot, &fst)
if err != nil {
return &os.PathError{Op: "statfs", Path: MapsRoot, Err: err}
} else if fst.Type == FilesystemTypeBPFFS {
return nil
}
err = unix.Mount(MapsRoot, MapsRoot, "bpf", 0, "")
if err != nil {
return fmt.Errorf("failed to mount %s: %s", MapsRoot, err)
}
return nil
}
func main() {
var prog BypassProgram
var err error
err = checkOrMountBPFFSDefault()
if err != nil {
fmt.Println("BPF filesystem mounting on /sys/fs/bpf failed:", err)
return
}
if err := setLimit(); err != nil {
fmt.Println("Setting limit failed:", err)
return
}
prog, err = loadProgram(prog)
if err != nil {
fmt.Println("Loading program failed:", err)
return
}
defer closeProgram(prog)
fmt.Println("Run...")
defer fmt.Println("Exiting...")
c := make(chan os.Signal, 1)
signal.Notify(c)
<-c
}