-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
bigtcp.go
380 lines (332 loc) · 10.9 KB
/
bigtcp.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package bigtcp
import (
"errors"
"github.com/spf13/pflag"
"github.com/vishvananda/netlink"
datapathOption "github.com/cilium/cilium/pkg/datapath/option"
"github.com/cilium/cilium/pkg/datapath/tables"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/math"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/statedb"
)
const (
defaultGROMaxSize = 65536
defaultGSOMaxSize = 65536
bigTCPGROMaxSize = 196608
bigTCPGSOMaxSize = 196608
probeDevice = "lo"
EnableIPv4BIGTCPFlag = "enable-ipv4-big-tcp"
EnableIPv6BIGTCPFlag = "enable-ipv6-big-tcp"
)
// UserConfig are the configuration flags that the user can modify.
type UserConfig struct {
// EnableIPv6BIGTCP enables IPv6 BIG TCP (larger GSO/GRO limits) for the node including pods.
EnableIPv6BIGTCP bool
// EnableIPv4BIGTCP enables IPv4 BIG TCP (larger GSO/GRO limits) for the node including pods.
EnableIPv4BIGTCP bool
}
var defaultUserConfig = UserConfig{
EnableIPv6BIGTCP: false,
EnableIPv4BIGTCP: false,
}
func (def UserConfig) Flags(flags *pflag.FlagSet) {
flags.Bool(EnableIPv4BIGTCPFlag, def.EnableIPv4BIGTCP, "Enable IPv4 BIG TCP option which increases device's maximum GRO/GSO limits for IPv4")
flags.Bool(EnableIPv6BIGTCPFlag, def.EnableIPv6BIGTCP, "Enable IPv6 BIG TCP option which increases device's maximum GRO/GSO limits for IPv6")
}
var Cell = cell.Module(
"bigtcp",
"BIG TCP support",
cell.Config(defaultUserConfig),
cell.Provide(newBIGTCP),
cell.Invoke(func(*Configuration) {}),
)
func newDefaultConfiguration(userConfig UserConfig) *Configuration {
return &Configuration{
UserConfig: userConfig,
groIPv4MaxSize: defaultGROMaxSize,
gsoIPv4MaxSize: defaultGSOMaxSize,
groIPv6MaxSize: defaultGROMaxSize,
gsoIPv6MaxSize: defaultGSOMaxSize,
}
}
// Configuration is the BIG TCP configuration. The values are finalized after
// BIG TCP has started and must not be read before that.
type Configuration struct {
UserConfig
// gsoIPv{4,6}MaxSize is the GSO maximum size used when configuring
// devices.
//
// Note that this is a singleton for the process including this
// package. This means, for instance, that when using this from the
// ``pkg/plugins/*`` sources, it will not respect the settings
// configured inside the ``daemon/``.
gsoIPv4MaxSize int
gsoIPv6MaxSize int
// groIPv{4,6}MaxSize is the GRO maximum size used when configuring
// devices.
//
// Note that this is a singleton for the process including this
// package. This means, for instance, that when using this from the
// ``pkg/plugins/*`` sources, it will not respect the settings
// configured inside the ``daemon/``.
groIPv4MaxSize int
groIPv6MaxSize int
}
func (c *Configuration) GetGROIPv6MaxSize() int {
return c.groIPv6MaxSize
}
func (c *Configuration) GetGSOIPv6MaxSize() int {
return c.gsoIPv6MaxSize
}
func (c *Configuration) GetGROIPv4MaxSize() int {
return c.groIPv4MaxSize
}
func (c *Configuration) GetGSOIPv4MaxSize() int {
return c.gsoIPv4MaxSize
}
// If an error is returned the caller is responsible for rolling back
// any partial changes.
func setGROGSOIPv6MaxSize(userConfig UserConfig, device string, GROMaxSize, GSOMaxSize int) error {
link, err := netlink.LinkByName(device)
if err != nil {
log.WithError(err).WithField("device", device).Warn("Link does not exist")
return nil
}
attrs := link.Attrs()
// The check below is needed to avoid trying to change GSO/GRO max sizes
// when that is not necessary (e.g. BIG TCP was never enabled or current
// size matches the target size we need).
if (int(attrs.GROMaxSize) == GROMaxSize && int(attrs.GSOMaxSize) == GSOMaxSize) ||
(!userConfig.EnableIPv6BIGTCP &&
int(attrs.GROMaxSize) <= GROMaxSize &&
int(attrs.GSOMaxSize) <= GSOMaxSize) {
return nil
}
err = netlink.LinkSetGROMaxSize(link, GROMaxSize)
if err != nil {
return err
}
return netlink.LinkSetGSOMaxSize(link, GSOMaxSize)
}
// If an error is returned the caller is responsible for rolling back
// any partial changes.
func setGROGSOIPv4MaxSize(userConfig UserConfig, device string, GROMaxSize, GSOMaxSize int) error {
link, err := netlink.LinkByName(device)
if err != nil {
log.WithError(err).WithField("device", device).Warn("Link does not exist")
return nil
}
attrs := link.Attrs()
// The check below is needed to avoid trying to change GSO/GRO max sizes
// when that is not necessary (e.g. BIG TCP was never enabled or current
// size matches the target size we need).
if (int(attrs.GROIPv4MaxSize) == GROMaxSize && int(attrs.GSOIPv4MaxSize) == GSOMaxSize) ||
(!userConfig.EnableIPv4BIGTCP &&
int(attrs.GROIPv4MaxSize) <= GROMaxSize &&
int(attrs.GSOIPv4MaxSize) <= GSOMaxSize) {
return nil
}
err = netlink.LinkSetGROIPv4MaxSize(link, GROMaxSize)
if err != nil {
return err
}
return netlink.LinkSetGSOIPv4MaxSize(link, GSOMaxSize)
}
func haveIPv4MaxSize() bool {
link, err := netlink.LinkByName(probeDevice)
if err != nil {
return false
}
if link.Attrs().GROIPv4MaxSize > 0 && link.Attrs().GSOIPv4MaxSize > 0 {
return true
}
return false
}
func haveIPv6MaxSize() bool {
link, err := netlink.LinkByName(probeDevice)
if err != nil {
return false
}
if link.Attrs().TSOMaxSize > 0 {
return true
}
return false
}
func probeTSOMaxSize(devices []string) int {
maxSize := math.IntMin(bigTCPGSOMaxSize, bigTCPGROMaxSize)
for _, device := range devices {
link, err := netlink.LinkByName(device)
if err == nil {
tso := link.Attrs().TSOMaxSize
tsoMax := int(tso)
if tsoMax > defaultGSOMaxSize && tsoMax < maxSize {
log.WithField("device", device).Infof("Lowering GRO/GSO max size from %d to %d", maxSize, tsoMax)
maxSize = tsoMax
}
}
}
return maxSize
}
type params struct {
cell.In
DaemonConfig *option.DaemonConfig
UserConfig UserConfig
DB *statedb.DB
Devices statedb.Table[*tables.Device]
}
func validateConfig(cfg UserConfig, daemonCfg *option.DaemonConfig) error {
if cfg.EnableIPv6BIGTCP || cfg.EnableIPv4BIGTCP {
if daemonCfg.DatapathMode != datapathOption.DatapathModeVeth {
return errors.New("BIG TCP is supported only in veth datapath mode")
}
if daemonCfg.TunnelingEnabled() {
return errors.New("BIG TCP is not supported in tunneling mode")
}
if daemonCfg.EncryptionEnabled() {
return errors.New("BIG TCP is not supported with encryption enabled")
}
if daemonCfg.EnableHostLegacyRouting {
return errors.New("BIG TCP is not supported with legacy host routing")
}
}
return nil
}
func newBIGTCP(lc cell.Lifecycle, p params) (*Configuration, error) {
if err := validateConfig(p.UserConfig, p.DaemonConfig); err != nil {
return nil, err
}
cfg := newDefaultConfiguration(p.UserConfig)
lc.Append(cell.Hook{
OnStart: func(cell.HookContext) error {
return startBIGTCP(p, cfg)
},
})
return cfg, nil
}
func startBIGTCP(p params, cfg *Configuration) error {
var err error
nativeDevices, _ := tables.SelectedDevices(p.Devices, p.DB.ReadTxn())
deviceNames := tables.DeviceNames(nativeDevices)
disableMsg := ""
if len(deviceNames) == 0 {
if p.UserConfig.EnableIPv4BIGTCP || p.UserConfig.EnableIPv6BIGTCP {
log.Warn("BIG TCP could not detect host devices. Disabling the feature.")
}
p.UserConfig.EnableIPv4BIGTCP = false
p.UserConfig.EnableIPv6BIGTCP = false
return nil
}
haveIPv4 := haveIPv4MaxSize()
haveIPv6 := haveIPv6MaxSize()
if !haveIPv4 {
if p.UserConfig.EnableIPv4BIGTCP {
log.Warnf("Cannot enable --%s, needs kernel 6.3 or newer",
EnableIPv4BIGTCPFlag)
}
p.UserConfig.EnableIPv4BIGTCP = false
}
if !haveIPv6 {
if p.UserConfig.EnableIPv6BIGTCP {
log.Warnf("Cannot enable --%s, needs kernel 5.19 or newer",
EnableIPv6BIGTCPFlag)
}
p.UserConfig.EnableIPv6BIGTCP = false
}
if !haveIPv4 && !haveIPv6 {
return nil
}
if haveIPv4 {
cfg.groIPv4MaxSize = defaultGROMaxSize
cfg.gsoIPv4MaxSize = defaultGSOMaxSize
}
if haveIPv6 {
cfg.groIPv6MaxSize = defaultGROMaxSize
cfg.gsoIPv6MaxSize = defaultGSOMaxSize
}
if p.UserConfig.EnableIPv6BIGTCP || p.UserConfig.EnableIPv4BIGTCP {
log.Infof("Setting up BIG TCP")
tsoMax := probeTSOMaxSize(deviceNames)
if p.UserConfig.EnableIPv4BIGTCP && haveIPv4 {
cfg.groIPv4MaxSize = tsoMax
cfg.gsoIPv4MaxSize = tsoMax
}
if p.UserConfig.EnableIPv6BIGTCP && haveIPv6 {
cfg.groIPv6MaxSize = tsoMax
cfg.gsoIPv6MaxSize = tsoMax
}
disableMsg = ", disabling BIG TCP"
}
bigv6 := p.UserConfig.EnableIPv6BIGTCP
bigv4 := p.UserConfig.EnableIPv4BIGTCP
modifiedDevices := []string{}
for _, device := range deviceNames {
// We always add the device because we might do only a partial
// modification and end up with an error, so best to be conservative
// and always reset all on error.
modifiedDevices = append(modifiedDevices, device)
// For compatibility, the kernel will also update the net device's
// {gso,gro}_ipv4_max_size, if the new size of {gso,gro}_max_size
// isn't greater than 64KB. So it needs to set the IPv6 one first
// as otherwise the IPv4 BIG TCP value will be reset.
if haveIPv6 {
err = setGROGSOIPv6MaxSize(p.UserConfig, device,
cfg.groIPv6MaxSize, cfg.gsoIPv6MaxSize)
if err != nil {
log.WithError(err).WithField("device", device).Warnf("Could not modify IPv6 gro_max_size and gso_max_size%s",
disableMsg)
p.UserConfig.EnableIPv4BIGTCP = false
p.UserConfig.EnableIPv6BIGTCP = false
break
}
log.WithField("device", device).Infof("Setting IPv6 gso_max_size to %d and gro_max_size to %d",
cfg.gsoIPv6MaxSize, cfg.groIPv6MaxSize)
}
if haveIPv4 {
err = setGROGSOIPv4MaxSize(p.UserConfig, device,
cfg.groIPv4MaxSize, cfg.gsoIPv4MaxSize)
if err != nil {
log.WithError(err).WithField("device", device).Warnf("Could not modify IPv4 gro_max_size and gso_max_size%s",
disableMsg)
p.UserConfig.EnableIPv4BIGTCP = false
p.UserConfig.EnableIPv6BIGTCP = false
break
}
log.WithField("device", device).Infof("Setting IPv4 gso_max_size to %d and gro_max_size to %d",
cfg.gsoIPv4MaxSize, cfg.groIPv4MaxSize)
}
}
if err != nil {
if haveIPv4 {
cfg.groIPv4MaxSize = defaultGROMaxSize
cfg.gsoIPv4MaxSize = defaultGSOMaxSize
}
if haveIPv6 {
cfg.groIPv6MaxSize = defaultGROMaxSize
cfg.gsoIPv6MaxSize = defaultGSOMaxSize
}
for _, device := range modifiedDevices {
if bigv4 {
err = setGROGSOIPv4MaxSize(p.UserConfig, device,
defaultGROMaxSize, defaultGSOMaxSize)
if err != nil {
log.WithError(err).WithField("device", device).Warn("Could not reset IPv4 gro_max_size and gso_max_size")
continue
}
log.WithField("device", device).Info("Resetting IPv4 gso_max_size and gro_max_size")
}
if bigv6 {
err = setGROGSOIPv6MaxSize(p.UserConfig, device,
defaultGROMaxSize, defaultGSOMaxSize)
if err != nil {
log.WithError(err).WithField("device", device).Warn("Could not reset IPv6 gro_max_size and gso_max_size")
continue
}
log.WithField("device", device).Info("Resetting IPv6 gso_max_size and gro_max_size")
}
}
}
return nil
}