-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
loader.go
500 lines (443 loc) · 16.2 KB
/
loader.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package loader
import (
"context"
"fmt"
"net"
"os"
"path"
"sync"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/datapath"
"github.com/cilium/cilium/pkg/datapath/link"
"github.com/cilium/cilium/pkg/datapath/linux/route"
"github.com/cilium/cilium/pkg/datapath/loader/metrics"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/elf"
iputil "github.com/cilium/cilium/pkg/ip"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/callsmap"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
)
const (
Subsystem = "datapath-loader"
symbolFromEndpoint = "cil_from_container"
symbolToEndpoint = "cil_to_container"
symbolFromNetwork = "cil_from_network"
symbolFromHostNetdevEp = "cil_from_netdev"
symbolToHostNetdevEp = "cil_to_netdev"
symbolFromHostEp = "cil_from_host"
symbolToHostEp = "cil_to_host"
symbolFromHostNetdevXDP = "cil_xdp_entry"
dirIngress = "ingress"
dirEgress = "egress"
)
const (
SecctxFromIpcacheDisabled = iota + 1
SecctxFromIpcacheEnabled
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, Subsystem)
// Loader is a wrapper structure around operations related to compiling,
// loading, and reloading datapath programs.
type Loader struct {
once sync.Once
// templateCache is the cache of pre-compiled datapaths.
templateCache *objectCache
}
// NewLoader returns a new loader.
func NewLoader() *Loader {
return &Loader{}
}
// Init initializes the datapath cache with base program hashes derived from
// the LocalNodeConfiguration.
func (l *Loader) init(dp datapath.ConfigWriter, nodeCfg *datapath.LocalNodeConfiguration) {
l.once.Do(func() {
l.templateCache = NewObjectCache(dp, nodeCfg)
ignorePrefixes := ignoredELFPrefixes
if !option.Config.EnableIPv4 {
ignorePrefixes = append(ignorePrefixes, "LXC_IPV4")
}
elf.IgnoreSymbolPrefixes(ignorePrefixes)
})
l.templateCache.Update(nodeCfg)
}
func upsertEndpointRoute(ep datapath.Endpoint, ip net.IPNet) error {
endpointRoute := route.Route{
Prefix: ip,
Device: ep.InterfaceName(),
Scope: netlink.SCOPE_LINK,
}
return route.Upsert(endpointRoute)
}
func removeEndpointRoute(ep datapath.Endpoint, ip net.IPNet) error {
return route.Delete(route.Route{
Prefix: ip,
Device: ep.InterfaceName(),
Scope: netlink.SCOPE_LINK,
})
}
// We need this function when patching an object file for which symbols were
// already substituted. During the first symbol substitutions, string symbols
// were replaced such that:
//
// template_string -> string_for_endpoint
//
// Since we only want to replace one int symbol, we can nullify string
// substitutions with:
//
// string_for_endpoint -> string_for_endpoint
//
// We cannot simply pass an empty map as the agent would complain that some
// symbol had no corresponding values.
func nullifyStringSubstitutions(strings map[string]string) map[string]string {
nullStrings := make(map[string]string)
for _, v := range strings {
nullStrings[v] = v
}
return nullStrings
}
// Since we attach the host endpoint datapath to two different interfaces, we
// need two different NODE_MAC values. patchHostNetdevDatapath creates a new
// object file for the native device, from the object file for the host device
// (cilium_host).
// Since the two object files should only differ by the values of their
// NODE_MAC symbols, we can avoid a full compilation.
func patchHostNetdevDatapath(ep datapath.Endpoint, objPath, dstPath, ifName string,
bpfMasqIPv4Addrs map[string]net.IP) error {
hostObj, err := elf.Open(objPath)
if err != nil {
return err
}
defer hostObj.Close()
opts, strings := ELFSubstitutions(ep)
// The NODE_MAC value is specific to each attachment interface.
mac, err := link.GetHardwareAddr(ifName)
if err != nil {
return err
}
if mac == nil {
// L2-less device
mac = make([]byte, 6)
opts["ETH_HLEN"] = uint32(0)
}
opts["NODE_MAC_1"] = sliceToBe32(mac[0:4])
opts["NODE_MAC_2"] = uint32(sliceToBe16(mac[4:6]))
ifIndex, err := link.GetIfIndex(ifName)
if err != nil {
return err
}
if !option.Config.EnableHostLegacyRouting {
opts["SECCTX_FROM_IPCACHE"] = uint32(SecctxFromIpcacheEnabled)
} else {
opts["SECCTX_FROM_IPCACHE"] = uint32(SecctxFromIpcacheDisabled)
}
if option.Config.EnableNodePort {
opts["NATIVE_DEV_IFINDEX"] = ifIndex
}
if option.Config.EnableIPv4Masquerade && option.Config.EnableBPFMasquerade && bpfMasqIPv4Addrs != nil {
if option.Config.EnableIPv4 {
ipv4 := bpfMasqIPv4Addrs[ifName]
opts["IPV4_MASQUERADE"] = byteorder.NetIPv4ToHost32(ipv4)
}
}
// Among string substitutions, only the calls map name is specific to each
// attachment interface.
strings = nullifyStringSubstitutions(strings)
callsMapHostDevice := bpf.LocalMapName(callsmap.HostMapName, uint16(ep.GetID()))
strings[callsMapHostDevice] = bpf.LocalMapName(callsmap.NetdevMapName, uint16(ifIndex))
return hostObj.Write(dstPath, opts, strings)
}
// reloadHostDatapath loads bpf_host programs attached to the host device
// (usually cilium_host) and the native devices if any. To that end, it
// uses a single object file, pointed to by objPath, compiled for the host
// device and patches it with values for native devices if needed.
// Symbols in objPath have already been substituted with the appropriate values
// for the host device. Thus, when packing the object file again for the native
// devices, we don't need to substitute most values (see
// nullifyStringSubstitutions above).
// reloadHostDatapath skips native devices that do not exist just before
// loading. If loading+attaching fails later on however, reloadHostDatapath
// will return with an error. Failing to load or to attach the host device
// always results in reloadHostDatapath returning with an error.
func (l *Loader) reloadHostDatapath(ctx context.Context, ep datapath.Endpoint, objPath string) error {
nbInterfaces := len(option.Config.GetDevices()) + 2
symbols := make([]string, 2, nbInterfaces)
directions := make([]string, 2, nbInterfaces)
objPaths := make([]string, 2, nbInterfaces)
interfaceNames := make([]string, 2, nbInterfaces)
symbols[0], symbols[1] = symbolToHostEp, symbolFromHostEp
directions[0], directions[1] = dirIngress, dirEgress
objPaths[0], objPaths[1] = objPath, objPath
interfaceNames[0], interfaceNames[1] = ep.InterfaceName(), ep.InterfaceName()
if _, err := netlink.LinkByName(defaults.SecondHostDevice); err != nil {
log.WithError(err).WithField("device", defaults.SecondHostDevice).Error("Link does not exist")
return err
} else {
interfaceNames = append(interfaceNames, defaults.SecondHostDevice)
symbols = append(symbols, symbolToHostEp)
directions = append(directions, dirIngress)
secondDevObjPath := path.Join(ep.StateDir(), hostEndpointPrefix+"_"+defaults.SecondHostDevice+".o")
if err := patchHostNetdevDatapath(ep, objPath, secondDevObjPath, defaults.SecondHostDevice, nil); err != nil {
return err
}
objPaths = append(objPaths, secondDevObjPath)
}
bpfMasqIPv4Addrs := node.GetMasqIPv4AddrsWithDevices()
for _, device := range option.Config.GetDevices() {
if _, err := netlink.LinkByName(device); err != nil {
log.WithError(err).WithField("device", device).Warn("Link does not exist")
continue
}
netdevObjPath := path.Join(ep.StateDir(), hostEndpointNetdevPrefix+device+".o")
if err := patchHostNetdevDatapath(ep, objPath, netdevObjPath, device, bpfMasqIPv4Addrs); err != nil {
return err
}
objPaths = append(objPaths, netdevObjPath)
interfaceNames = append(interfaceNames, device)
symbols = append(symbols, symbolFromHostNetdevEp)
directions = append(directions, dirIngress)
if option.Config.AreDevicesRequired() {
interfaceNames = append(interfaceNames, device)
symbols = append(symbols, symbolToHostNetdevEp)
directions = append(directions, dirEgress)
objPaths = append(objPaths, netdevObjPath)
} else {
// Remove any previously attached device from egress path if BPF
// NodePort and host firewall are disabled.
err := RemoveTCFilters(device, netlink.HANDLE_MIN_EGRESS)
if err != nil {
log.WithField("device", device).Error(err)
}
}
}
for i, interfaceName := range interfaceNames {
symbol := symbols[i]
finalize, err := replaceDatapath(ctx, interfaceName, objPaths[i], symbol, directions[i], "")
if err != nil {
scopedLog := ep.Logger(Subsystem).WithFields(logrus.Fields{
logfields.Path: objPath,
logfields.Veth: interfaceName,
})
// Don't log an error here if the context was canceled or timed out;
// this log message should only represent failures with respect to
// loading the program.
if ctx.Err() == nil {
scopedLog.WithError(err).Warningf("JoinEP: Failed to load program for host endpoint (%s)", symbol)
}
return err
}
// Defer map removal until all interfaces' progs have been replaced.
defer finalize()
}
return nil
}
func (l *Loader) reloadDatapath(ctx context.Context, ep datapath.Endpoint, dirs *directoryInfo) error {
// Replace the current program
objPath := path.Join(dirs.Output, endpointObj)
if ep.IsHost() {
objPath = path.Join(dirs.Output, hostEndpointObj)
if err := l.reloadHostDatapath(ctx, ep, objPath); err != nil {
return err
}
} else {
finalize, err := replaceDatapath(ctx, ep.InterfaceName(), objPath, symbolFromEndpoint, dirIngress, "")
if err != nil {
scopedLog := ep.Logger(Subsystem).WithFields(logrus.Fields{
logfields.Path: objPath,
logfields.Veth: ep.InterfaceName(),
})
// Don't log an error here if the context was canceled or timed out;
// this log message should only represent failures with respect to
// loading the program.
if ctx.Err() == nil {
scopedLog.WithError(err).Warn("JoinEP: Failed to attach ingress program")
}
return err
}
defer finalize()
if ep.RequireEgressProg() {
finalize, err := replaceDatapath(ctx, ep.InterfaceName(), objPath, symbolToEndpoint, dirEgress, "")
if err != nil {
scopedLog := ep.Logger(Subsystem).WithFields(logrus.Fields{
logfields.Path: objPath,
logfields.Veth: ep.InterfaceName(),
})
// Don't log an error here if the context was canceled or timed out;
// this log message should only represent failures with respect to
// loading the program.
if ctx.Err() == nil {
scopedLog.WithError(err).Warn("JoinEP: Failed to attach egress program")
}
return err
}
defer finalize()
} else {
err := RemoveTCFilters(ep.InterfaceName(), netlink.HANDLE_MIN_EGRESS)
if err != nil {
log.WithField("device", ep.InterfaceName()).Error(err)
}
}
}
if ep.RequireEndpointRoute() {
scopedLog := ep.Logger(Subsystem).WithFields(logrus.Fields{
logfields.Veth: ep.InterfaceName(),
})
if ip := ep.IPv4Address(); ip.IsValid() {
if err := upsertEndpointRoute(ep, *iputil.AddrToIPNet(ip)); err != nil {
scopedLog.WithError(err).Warn("Failed to upsert route")
}
}
if ip := ep.IPv6Address(); ip.IsValid() {
if err := upsertEndpointRoute(ep, *iputil.AddrToIPNet(ip)); err != nil {
scopedLog.WithError(err).Warn("Failed to upsert route")
}
}
}
return nil
}
func (l *Loader) replaceNetworkDatapath(ctx context.Context, interfaces []string) error {
if err := compileNetwork(ctx); err != nil {
log.WithError(err).Fatal("failed to compile encryption programs")
}
for _, iface := range option.Config.EncryptInterface {
finalize, err := replaceDatapath(ctx, iface, networkObj, symbolFromNetwork, dirIngress, "")
if err != nil {
log.WithField(logfields.Interface, iface).WithError(err).Fatal("Load encryption network failed")
}
// Defer map removal until all interfaces' progs have been replaced.
defer finalize()
}
return nil
}
func (l *Loader) compileAndLoad(ctx context.Context, ep datapath.Endpoint, dirs *directoryInfo, stats *metrics.SpanStat) error {
stats.BpfCompilation.Start()
err := compileDatapath(ctx, dirs, ep.IsHost(), ep.Logger(Subsystem))
stats.BpfCompilation.End(err == nil)
if err != nil {
return err
}
stats.BpfLoadProg.Start()
err = l.reloadDatapath(ctx, ep, dirs)
stats.BpfLoadProg.End(err == nil)
return err
}
// CompileAndLoad compiles the BPF datapath programs for the specified endpoint
// and loads it onto the interface associated with the endpoint.
//
// Expects the caller to have created the directory at the path ep.StateDir().
func (l *Loader) CompileAndLoad(ctx context.Context, ep datapath.Endpoint, stats *metrics.SpanStat) error {
if ep == nil {
log.Fatalf("LoadBPF() doesn't support non-endpoint load")
}
dirs := directoryInfo{
Library: option.Config.BpfDir,
Runtime: option.Config.StateDir,
State: ep.StateDir(),
Output: ep.StateDir(),
}
return l.compileAndLoad(ctx, ep, &dirs, stats)
}
// CompileOrLoad loads the BPF datapath programs for the specified endpoint.
//
// In contrast with CompileAndLoad(), it attempts to find a pre-compiled
// template datapath object to use, to avoid a costly compile operation.
// Only if there is no existing template that has the same configuration
// parameters as the specified endpoint, this function will compile a new
// template for this configuration.
//
// This function will block if the cache does not contain an entry for the
// same EndpointConfiguration and multiple goroutines attempt to concurrently
// CompileOrLoad with the same configuration parameters. When the first
// goroutine completes compilation of the template, all other CompileOrLoad
// invocations will be released.
func (l *Loader) CompileOrLoad(ctx context.Context, ep datapath.Endpoint, stats *metrics.SpanStat) error {
templatePath, _, err := l.templateCache.fetchOrCompile(ctx, ep, stats)
if err != nil {
return err
}
template, err := elf.Open(templatePath)
if err != nil {
return err
}
defer template.Close()
symPath := path.Join(ep.StateDir(), defaults.TemplatePath)
if _, err := os.Stat(symPath); err == nil {
if err = os.RemoveAll(symPath); err != nil {
return &os.PathError{
Op: "Failed to remove old symlink",
Path: symPath,
Err: err,
}
}
} else if !os.IsNotExist(err) {
return &os.PathError{
Op: "Failed to locate symlink",
Path: symPath,
Err: err,
}
}
if err := os.Symlink(templatePath, symPath); err != nil {
return &os.PathError{
Op: fmt.Sprintf("Failed to create symlink to %s", templatePath),
Path: symPath,
Err: err,
}
}
stats.BpfWriteELF.Start()
epObj := endpointObj
if ep.IsHost() {
epObj = hostEndpointObj
}
dstPath := path.Join(ep.StateDir(), epObj)
opts, strings := ELFSubstitutions(ep)
if err = template.Write(dstPath, opts, strings); err != nil {
stats.BpfWriteELF.End(err == nil)
return err
}
stats.BpfWriteELF.End(err == nil)
return l.ReloadDatapath(ctx, ep, stats)
}
// ReloadDatapath reloads the BPF datapath programs for the specified endpoint.
func (l *Loader) ReloadDatapath(ctx context.Context, ep datapath.Endpoint, stats *metrics.SpanStat) (err error) {
dirs := directoryInfo{
Library: option.Config.BpfDir,
Runtime: option.Config.StateDir,
State: ep.StateDir(),
Output: ep.StateDir(),
}
stats.BpfLoadProg.Start()
err = l.reloadDatapath(ctx, ep, &dirs)
stats.BpfLoadProg.End(err == nil)
return err
}
// Unload removes the datapath specific program aspects
func (l *Loader) Unload(ep datapath.Endpoint) {
if ep.RequireEndpointRoute() {
if ip := ep.IPv4Address(); ip.IsValid() {
removeEndpointRoute(ep, *iputil.AddrToIPNet(ip))
}
if ip := ep.IPv6Address(); ip.IsValid() {
removeEndpointRoute(ep, *iputil.AddrToIPNet(ip))
}
}
}
// EndpointHash hashes the specified endpoint configuration with the current
// datapath hash cache and returns the hash as string.
func (l *Loader) EndpointHash(cfg datapath.EndpointConfiguration) (string, error) {
return l.templateCache.baseHash.sumEndpoint(l.templateCache, cfg, true)
}
// CallsMapPath gets the BPF Calls Map for the endpoint with the specified ID.
func (l *Loader) CallsMapPath(id uint16) string {
return bpf.LocalMapPath(callsmap.MapName, id)
}
// CustomCallsMapPath gets the BPF Custom Calls Map for the endpoint with the
// specified ID.
func (l *Loader) CustomCallsMapPath(id uint16) string {
return bpf.LocalMapPath(callsmap.CustomCallsMapName, id)
}