/
sockops.go
368 lines (314 loc) · 9.2 KB
/
sockops.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package sockops
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/cgroups"
"github.com/cilium/cilium/pkg/datapath/loader"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
)
const (
cSockops = "bpf_sockops.c"
oSockops = "bpf_sockops.o"
eSockops = "bpf_sockops"
cIPC = "bpf_redir.c"
oIPC = "bpf_redir.o"
eIPC = "bpf_redir"
sockMap = "cilium_sock_ops"
// Default prefix for map objects
mapPrefix = defaults.DefaultMapPrefix
contextTimeout = 5 * time.Minute
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "sockops")
// BPF programs and sockmaps working on cgroups
func bpftoolMapAttach(progID string, mapID string) error {
prog := "bpftool"
args := []string{"prog", "attach", "id", progID, "msg_verdict", "id", mapID}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("Map Attach BPF Object:")
out, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to attach prog(%s) to map(%s): %s: %s", progID, mapID, err, out)
}
return nil
}
// #bpftool cgroup attach $cgrp sock_ops /sys/fs/bpf/$bpfObject
func bpftoolAttach(bpfObject string) error {
prog := "bpftool"
bpffs := filepath.Join(bpf.GetMapRoot(), bpfObject)
cgrp := cgroups.GetCgroupRoot()
args := []string{"cgroup", "attach", cgrp, "sock_ops", "pinned", bpffs}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("Attach BPF Object:")
out, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to attach %s: %s: %s", bpfObject, err, out)
}
return nil
}
// #bpftool cgroup detach $cgrp sock_ops /sys/fs/bpf/$bpfObject
func bpftoolDetach(bpfObject string) error {
prog := "bpftool"
bpffs := filepath.Join(bpf.GetMapRoot(), bpfObject)
cgrp := cgroups.GetCgroupRoot()
args := []string{"cgroup", "detach", cgrp, "sock_ops", "pinned", bpffs}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("Detach BPF Object:")
out, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to detach %s: %s: %s", bpfObject, err, out)
}
return nil
}
// #bpftool prog load $bpfObject /sys/fs/bpf/sockops
func bpftoolLoad(bpfObject string, bpfFsFile string) error {
sockopsMaps := [...]string{
"cilium_lxc",
"cilium_ipcache",
"cilium_metric",
"cilium_events",
"cilium_sock_ops",
"cilium_ep_to_policy",
"cilium_proxy4", "cilium_proxy6",
"cilium_lb6_reverse_nat", "cilium_lb4_reverse_nat",
"cilium_lb6_services", "cilium_lb4_services",
"cilium_lb6_rr_seq", "cilium_lb4_seq",
"cilium_lb6_rr_seq", "cilium_lb4_seq",
}
prog := "bpftool"
var mapArgList []string
bpffs := filepath.Join(bpf.GetMapRoot(), bpfFsFile)
maps, err := os.ReadDir(filepath.Join(bpf.GetMapRoot(), "/tc/globals/"))
if err != nil {
return err
}
for _, f := range maps {
// Ignore all backing files
if strings.HasPrefix(f.Name(), "..") {
continue
}
use := func() bool {
for _, n := range sockopsMaps {
if f.Name() == n {
return true
}
}
return false
}()
if !use {
continue
}
mapString := []string{"map", "name", f.Name(), "pinned", filepath.Join(bpf.GetMapRoot(), "/tc/globals/", f.Name())}
mapArgList = append(mapArgList, mapString...)
}
args := []string{"-m", "prog", "load", bpfObject, bpffs}
args = append(args, mapArgList...)
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("Load BPF Object:")
out, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to load %s: %s: %s", bpfObject, err, out)
}
return nil
}
// #rm $bpfObject
func bpftoolUnload(bpfObject string) {
bpffs := filepath.Join(bpf.GetMapRoot(), bpfObject)
os.Remove(bpffs)
}
// #bpftool prog show pinned /sys/fs/bpf/
func bpftoolGetProgID(progName string) (string, error) {
bpffs := filepath.Join(bpf.GetMapRoot(), progName)
prog := "bpftool"
args := []string{"prog", "show", "pinned", bpffs}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("GetProgID:")
output, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return "", fmt.Errorf("Failed to load %s: %s: %s", progName, err, output)
}
// Scrap the prog_id out of the bpftool output after libbpf is dual licensed
// we will use programatic API.
s := strings.Fields(string(output))
if s[0] == "" {
return "", fmt.Errorf("Failed to find prog %s: %s", progName, err)
}
progID := strings.Split(s[0], ":")
return progID[0], nil
}
// #bpftool prog show pinned /sys/fs/bpf/bpf_sockops
// #bpftool map show id 21
func bpftoolGetMapID(progName string, mapName string) (int, error) {
bpffs := filepath.Join(bpf.GetMapRoot(), progName)
prog := "bpftool"
args := []string{"prog", "show", "pinned", bpffs}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("GetMapID:")
output, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return 0, fmt.Errorf("Failed to load %s: %s: %s", progName, err, output)
}
// Find the mapID out of the bpftool output
s := strings.Fields(string(output))
for i := range s {
if s[i] == "map_ids" {
id := strings.Split(s[i+1], ",")
for j := range id {
args := []string{"map", "show", "id", id[j]}
output, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return 0, err
}
if strings.Contains(string(output), mapName) {
mapID, _ := strconv.Atoi(id[j])
return mapID, nil
}
}
break
}
}
return 0, nil
}
// #bpftool map pin id map_id /sys/fs/bpf/tc/globals
func bpftoolPinMapID(mapName string, mapID int) error {
mapFile := filepath.Join(bpf.GetMapRoot(), mapPrefix, mapName)
prog := "bpftool"
args := []string{"map", "pin", "id", strconv.Itoa(mapID), mapFile}
log.WithFields(logrus.Fields{
"bpftool": prog,
"args": args,
}).Debug("Map pin:")
out, err := exec.Command(prog, args...).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to pin map %d(%s): %s: %s", mapID, mapName, err, out)
}
return nil
}
// #clang ... | llc ...
func bpfCompileProg(src string, dst string) error {
ctx, cancel := context.WithTimeout(context.Background(), contextTimeout)
defer cancel()
srcpath := filepath.Join("sockops", src)
outpath := filepath.Join(dst)
err := loader.CompileWithOptions(ctx, srcpath, outpath, option.Config.CompilerFlags)
if err != nil {
return fmt.Errorf("failed compile %s: %s", srcpath, err)
}
return nil
}
func bpfLoadMapProg(object string, load string) error {
sockops := object
sockopsObj := filepath.Join(option.Config.StateDir, sockops)
sockopsLoad := load
err := bpftoolLoad(sockopsObj, sockopsLoad)
if err != nil {
return err
}
progID, err := bpftoolGetProgID(load)
if err != nil {
return err
}
_mapID, err := bpftoolGetMapID(eSockops, sockMap)
mapID := strconv.Itoa(_mapID)
if err != nil {
return err
}
err = bpftoolMapAttach(progID, mapID)
if err != nil {
return err
}
return nil
}
// SkmsgEnable will compile and attach the SK_MSG programs to the
// sockmap. After this all sockets added to the cilium_sock_ops will
// have sendmsg/sendfile calls running through BPF program.
func SkmsgEnable() error {
err := bpfCompileProg(cIPC, oIPC)
if err != nil {
return err
}
err = bpfLoadMapProg(oIPC, eIPC)
if err != nil {
return err
}
log.Info("Sockmsg Enabled, bpf_redir loaded")
return nil
}
// SkmsgDisable "unloads" the SK_MSG program. This simply deletes
// the file associated with the program.
func SkmsgDisable() {
bpftoolUnload(eIPC)
}
// First user of sockops root is sockops load programs so we ensure the sockops
// root path no longer changes.
func bpfLoadAttachProg(object string, load string, mapName string) (int, int, error) {
sockopsObj := filepath.Join(option.Config.StateDir, object)
mapID := 0
err := bpftoolLoad(sockopsObj, load)
if err != nil {
return 0, 0, err
}
err = bpftoolAttach(load)
if err != nil {
return 0, 0, err
}
if mapName != "" {
mapID, err = bpftoolGetMapID(load, mapName)
if err != nil {
return 0, mapID, err
}
err = bpftoolPinMapID(mapName, mapID)
if err != nil {
return 0, mapID, err
}
}
return 0, mapID, nil
}
// SockmapEnable will compile sockops programs and attach the sockops programs
// to the cgroup. After this all TCP connect events will be filtered by a BPF
// sockops program.
func SockmapEnable() error {
err := bpfCompileProg(cSockops, oSockops)
if err != nil {
return err
}
progID, mapID, err := bpfLoadAttachProg(oSockops, eSockops, sockMap)
if err != nil {
return err
}
log.Infof("Sockmap Enabled: bpf_sockops prog_id %d and map_id %d loaded", progID, mapID)
return nil
}
// SockmapDisable will detach any sockmap programs from cgroups then "unload"
// all the programs and maps associated with it. Here "unload" just means
// deleting the file associated with the map.
func SockmapDisable() {
mapName := filepath.Join(mapPrefix, sockMap)
bpftoolDetach(eSockops)
bpftoolUnload(eSockops)
bpftoolUnload(mapName)
}