-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
xdp.go
171 lines (150 loc) · 4.79 KB
/
xdp.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package loader
import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/vishvananda/netlink"
"github.com/cilium/ebpf/link"
"github.com/cilium/cilium/pkg/bpf"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/mac"
"github.com/cilium/cilium/pkg/option"
)
func xdpConfigModeToFlag(xdpMode string) link.XDPAttachFlags {
switch xdpMode {
case option.XDPModeNative, option.XDPModeLinkDriver:
return link.XDPDriverMode
case option.XDPModeGeneric, option.XDPModeLinkGeneric:
return link.XDPGenericMode
}
return 0
}
// These constant values are returned by the kernel when querying the XDP program attach mode.
// Important: they differ from constants that are used when attaching an XDP program to a netlink device.
const (
XDPAttachedNone uint32 = iota
XDPAttachedDriver
XDPAttachedGeneric
)
// xdpAttachedModeToFlag maps the attach mode that is returned in the metadata when
// querying netlink devices to the attach flags that were used to configure the
// xdp program attachement.
func xdpAttachedModeToFlag(mode uint32) link.XDPAttachFlags {
switch mode {
case XDPAttachedDriver:
return link.XDPDriverMode
case XDPAttachedGeneric:
return link.XDPGenericMode
}
return 0
}
// maybeUnloadObsoleteXDPPrograms removes bpf_xdp.o from previously used devices.
func maybeUnloadObsoleteXDPPrograms(xdpDevs []string, xdpMode string) {
links, err := netlink.LinkList()
if err != nil {
log.WithError(err).Warn("Failed to list links for XDP unload")
}
for _, link := range links {
linkxdp := link.Attrs().Xdp
if linkxdp == nil || !linkxdp.Attached {
// No XDP program is attached
continue
}
if strings.Contains(link.Attrs().Name, "cilium") {
// Ignore devices created by cilium-agent
continue
}
used := false
for _, xdpDev := range xdpDevs {
if link.Attrs().Name == xdpDev &&
xdpAttachedModeToFlag(linkxdp.AttachMode) == xdpConfigModeToFlag(xdpMode) {
// XDP mode matches; don't unload, otherwise we might introduce
// intermittent connectivity problems
used = true
break
}
}
if !used {
netlink.LinkSetXdpFdWithFlags(link, -1, int(xdpConfigModeToFlag(option.XDPModeLinkGeneric)))
netlink.LinkSetXdpFdWithFlags(link, -1, int(xdpConfigModeToFlag(option.XDPModeLinkDriver)))
}
}
}
// maybeRemoveXDPLinks removes bpf_links for XDP programs.
//
// This is needed for the downgrade path from newer Cilium versions that attach
// XDP using bpf_link. If this is not supported by an old version of Cilium, the
// bpf_link needs to be removed by deleting its pin from bpffs. Then, the old
// version will be able to attach XDP programs using the legacy netlink again.
func maybeRemoveXDPLinks() {
links, err := netlink.LinkList()
if err != nil {
log.WithError(err).Warn("Failed to list links for XDP link removal")
}
for _, link := range links {
bpfLinkPath := filepath.Join(bpffsDeviceLinksDir(bpf.CiliumPath(), link), symbolFromHostNetdevXDP)
if err := os.Remove(bpfLinkPath); err != nil && !errors.Is(err, os.ErrNotExist) {
log.WithError(err).Errorf("Failed to remove link %s", bpfLinkPath)
}
}
}
// xdpCompileArgs derives compile arguments for bpf_xdp.c.
func xdpCompileArgs(xdpDev string, extraCArgs []string) ([]string, error) {
link, err := netlink.LinkByName(xdpDev)
if err != nil {
return nil, err
}
args := []string{
fmt.Sprintf("-DSECLABEL=%d", identity.ReservedIdentityWorld),
fmt.Sprintf("-DNODE_MAC={.addr=%s}", mac.CArrayString(link.Attrs().HardwareAddr)),
"-DCALLS_MAP=cilium_calls_xdp",
}
args = append(args, extraCArgs...)
if option.Config.EnableNodePort {
args = append(args, []string{
fmt.Sprintf("-DTHIS_MTU=%d", link.Attrs().MTU),
fmt.Sprintf("-DNATIVE_DEV_IFINDEX=%d", link.Attrs().Index),
"-DDISABLE_LOOPBACK_LB",
}...)
}
return args, nil
}
// compileAndLoadXDPProg compiles bpf_xdp.c for the given XDP device and loads it.
func compileAndLoadXDPProg(ctx context.Context, xdpDev, xdpMode string, extraCArgs []string) error {
args, err := xdpCompileArgs(xdpDev, extraCArgs)
if err != nil {
return fmt.Errorf("failed to derive XDP compile extra args: %w", err)
}
dirs := &directoryInfo{
Library: option.Config.BpfDir,
Runtime: option.Config.StateDir,
Output: option.Config.StateDir,
State: option.Config.StateDir,
}
prog := &progInfo{
Source: xdpProg,
Output: xdpObj,
OutputType: outputObject,
Options: args,
}
if err := compile(ctx, prog, dirs); err != nil {
return err
}
if err := ctx.Err(); err != nil {
return err
}
objPath := path.Join(dirs.Output, prog.Output)
progs := []progDefinition{{progName: symbolFromHostNetdevXDP, direction: ""}}
finalize, err := replaceDatapath(ctx, xdpDev, objPath, progs, xdpMode)
if err != nil {
return err
}
finalize()
return err
}