mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 15:16:52 +01:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73d26dd87d |
No files matched your search
@@ -14,7 +14,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -40,7 +40,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -83,7 +83,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
# GitHub holds ARNs, not credentials, and ARNs outlive a rotation
|
# GitHub holds ARNs, not credentials, and ARNs outlive a rotation
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: add hashicorp source
|
- name: add hashicorp source
|
||||||
@@ -66,7 +66,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: add hashicorp source
|
- name: add hashicorp source
|
||||||
@@ -92,7 +92,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
# WSL2 + Ubuntu so the smoke can run a real linux peer with its own
|
# WSL2 + Ubuntu so the smoke can run a real linux peer with its own
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Smoke Docker
|
- name: Smoke Docker
|
||||||
@@ -51,7 +51,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: build
|
- name: build
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Install goimports
|
- name: Install goimports
|
||||||
@@ -42,7 +42,7 @@ jobs:
|
|||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v9
|
uses: golangci/golangci-lint-action@v9
|
||||||
with:
|
with:
|
||||||
version: v2.14
|
version: v2.12
|
||||||
|
|
||||||
test:
|
test:
|
||||||
name: Test ${{ matrix.name }}
|
name: Test ${{ matrix.name }}
|
||||||
@@ -87,7 +87,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -132,7 +132,7 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v7
|
- uses: actions/setup-go@v7
|
||||||
with:
|
with:
|
||||||
go-version: '1.27'
|
go-version: '1.26'
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
- name: Build ${{ matrix.name }}
|
- name: Build ${{ matrix.name }}
|
||||||
|
|||||||
@@ -295,6 +295,14 @@ tun:
|
|||||||
# - 2
|
# - 2
|
||||||
# - 4
|
# - 4
|
||||||
|
|
||||||
|
# Linux only. avoid_cpus removes these CPU IDs from the affinity of every thread of the process at startup,
|
||||||
|
# embedding applications included; later threads inherit the remaining set. Applied before cpu_affinity and the
|
||||||
|
# default pin selection, independent of pin_threads. Invalid IDs, or IDs that would leave no CPU, fail
|
||||||
|
# startup. Not reloadable; ignored on other platforms. To keep Nebula off the underlay NIC's receive IRQ CPUs, find
|
||||||
|
# them in /proc/interrupts and /proc/irq/<IRQ>/effective_affinity_list, add their SMT siblings from
|
||||||
|
# /sys/devices/system/cpu/cpu<N>/topology/thread_siblings_list, and revisit the list when IRQ placement changes.
|
||||||
|
#avoid_cpus: []
|
||||||
|
|
||||||
# Route based MTU overrides, you have known vpn ip paths that can support larger MTUs you can increase/decrease them here
|
# Route based MTU overrides, you have known vpn ip paths that can support larger MTUs you can increase/decrease them here
|
||||||
routes:
|
routes:
|
||||||
#- mtu: 8800
|
#- mtu: 8800
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module github.com/slackhq/nebula
|
module github.com/slackhq/nebula
|
||||||
|
|
||||||
go 1.27.2
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.2
|
dario.cat/mergo v1.0.2
|
||||||
@@ -33,7 +33,7 @@ require (
|
|||||||
golang.zx2c4.com/wireguard/windows v1.0.1
|
golang.zx2c4.com/wireguard/windows v1.0.1
|
||||||
google.golang.org/protobuf v1.36.12
|
google.golang.org/protobuf v1.36.12
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -45,6 +45,5 @@ require (
|
|||||||
github.com/prometheus/common v0.70.1 // indirect
|
github.com/prometheus/common v0.70.1 // indirect
|
||||||
github.com/prometheus/procfs v0.21.1 // indirect
|
github.com/prometheus/procfs v0.21.1 // indirect
|
||||||
github.com/vishvananda/netns v0.0.5 // indirect
|
github.com/vishvananda/netns v0.0.5 // indirect
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc // indirect
|
golang.org/x/time v0.5.0 // indirect
|
||||||
golang.org/x/time v0.15.0 // indirect
|
|
||||||
)
|
)
|
||||||
@@ -157,8 +157,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
|
|||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc h1:TS73t7x3KarrNd5qAipmspBDS1rkMcgVG/fS1aRb4Rc=
|
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc=
|
|
||||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
||||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
@@ -205,8 +203,8 @@ golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
|||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||||
@@ -245,5 +243,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980 h1:ribyrTYQ/QYeUIs8x8bVLYwGyepa7RnHLqJzegT5YYk=
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe h1:fre4i6mv4iBuz5lCMOzHD1rH1ljqHWSICFmZRbbgp3g=
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980/go.mod h1:pK/dapF5Bf3KGW7/kd5CI5IC4lYh9FMFY1qETAIGLXk=
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe/go.mod h1:sxc3Uvk/vHcd3tj7/DHVBoR5wvWT/MmRq2pj7HRJnwU=
|
||||||
@@ -2,6 +2,7 @@ package nebula
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
@@ -31,6 +32,10 @@ const maxRoutines = 1 << 16
|
|||||||
const _ = noiseutil.RejectHeadroom - 4*maxRoutines
|
const _ = noiseutil.RejectHeadroom - 4*maxRoutines
|
||||||
|
|
||||||
func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, deviceFactory overlay.DeviceFactory) (retcon *Control, reterr error) {
|
func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, deviceFactory overlay.DeviceFactory) (retcon *Control, reterr error) {
|
||||||
|
// Apply before starting services or selecting per-reader pin CPUs.
|
||||||
|
if err := configureAvoidCPUs(c, l, configTest); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
// The goroutines started below stop only when this context does, and only a caller holding the
|
// The goroutines started below stop only when this context does, and only a caller holding the
|
||||||
// Control can arrange that. Cancel whenever we are not handing one back, which covers an error
|
// Control can arrange that. Cancel whenever we are not handing one back, which covers an error
|
||||||
@@ -416,6 +421,41 @@ func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
|||||||
return cpus
|
return cpus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// configureAvoidCPUs applies tun.avoid_cpus, a list of CPU IDs to keep every thread of the process off. Unlike
|
||||||
|
// tun.cpu_affinity, a bad entry fails startup rather than being ignored. Configuration tests only validate it.
|
||||||
|
func configureAvoidCPUs(c *config.C, l *slog.Logger, configTest bool) error {
|
||||||
|
raw := c.Get("tun.avoid_cpus")
|
||||||
|
if raw == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
values, ok := raw.([]any)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("tun.avoid_cpus must be a list of non-negative integers")
|
||||||
|
}
|
||||||
|
cpus := make([]int, 0, len(values))
|
||||||
|
for _, value := range values {
|
||||||
|
cpu, ok := value.(int)
|
||||||
|
if !ok || cpu < 0 {
|
||||||
|
return fmt.Errorf("tun.avoid_cpus must contain non-negative integers, got %v", value)
|
||||||
|
}
|
||||||
|
cpus = append(cpus, cpu)
|
||||||
|
}
|
||||||
|
if len(cpus) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := util.AvoidCPUs(cpus, !configTest); err != nil {
|
||||||
|
if errors.Is(err, errors.ErrUnsupported) {
|
||||||
|
l.Warn("tun.avoid_cpus is only supported on Linux; ignoring")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("tun.avoid_cpus: %w", err)
|
||||||
|
}
|
||||||
|
if !configTest {
|
||||||
|
l.Info("Excluded CPUs from process threads", "cpus", cpus)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func moduleVersion() string {
|
func moduleVersion() string {
|
||||||
info, ok := debug.ReadBuildInfo()
|
info, ok := debug.ReadBuildInfo()
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -59,6 +59,27 @@ func TestParseCpuAffinity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigureAvoidCPUs(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
for _, tc := range []struct {
|
||||||
|
value any
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{nil, true}, {[]any{}, true}, {"auto", false}, {"1,2", false}, {[]any{-1}, false}, {[]any{1.5}, false},
|
||||||
|
{[]any{"2"}, false},
|
||||||
|
} {
|
||||||
|
c := config.NewC(l)
|
||||||
|
if tc.value != nil {
|
||||||
|
c.Settings["tun"] = map[string]any{"avoid_cpus": tc.value}
|
||||||
|
}
|
||||||
|
if err := configureAvoidCPUs(c, l, true); tc.valid {
|
||||||
|
require.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
require.Error(t, err, "%v", tc.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestMain_ConfigTestReleasesItsGoroutines pins the rule that Main only leaves goroutines running
|
// TestMain_ConfigTestReleasesItsGoroutines pins the rule that Main only leaves goroutines running
|
||||||
// when it hands back a Control to stop them with.
|
// when it hands back a Control to stop them with.
|
||||||
//
|
//
|
||||||
|
|||||||
+3
-96
@@ -552,17 +552,13 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
if cidr.Addr().Is6() {
|
if cidr.Addr().Is6() {
|
||||||
nr.Priority = 256
|
nr.Priority = 256
|
||||||
}
|
}
|
||||||
held, err := t.otherLinkRoutes()
|
err := netlink.RouteReplace(&nr)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = t.replaceRoute(&nr, held)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.l.Warn("Failed to set default route MTU, retrying", "error", err, "cidr", cidr)
|
t.l.Warn("Failed to set default route MTU, retrying", "error", err, "cidr", cidr)
|
||||||
//retry twice more -- on some systems there appears to be a race condition where if we set routes too soon, netlink says `invalid argument`
|
//retry twice more -- on some systems there appears to be a race condition where if we set routes too soon, netlink says `invalid argument`
|
||||||
for range 2 {
|
for range 2 {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
err = t.replaceRoute(&nr, held)
|
err = netlink.RouteReplace(&nr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@@ -582,15 +578,6 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) addRoutes(logErrors bool) error {
|
func (t *tun) addRoutes(logErrors bool) error {
|
||||||
held, err := t.otherLinkRoutes()
|
|
||||||
if err != nil {
|
|
||||||
if logErrors {
|
|
||||||
t.l.Error("Failed to add routes", "error", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path routes
|
// Path routes
|
||||||
routes := *t.Routes.Load()
|
routes := *t.Routes.Load()
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
@@ -615,7 +602,7 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
nr.Priority = r.Metric
|
nr.Priority = r.Metric
|
||||||
}
|
}
|
||||||
|
|
||||||
err := t.replaceRoute(&nr, held)
|
err := netlink.RouteReplace(&nr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
retErr := util.NewContextualError("Failed to add route", map[string]any{"route": r}, err)
|
||||||
if logErrors {
|
if logErrors {
|
||||||
@@ -631,86 +618,6 @@ func (t *tun) addRoutes(logErrors bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ip6RoutePrioUser is the metric the kernel gives an IPv6 route added without one
|
|
||||||
const ip6RoutePrioUser = 1024
|
|
||||||
|
|
||||||
// routeKey is where the main table files a route. Replacing a route replaces the first one with the same key, whatever
|
|
||||||
// device it is on
|
|
||||||
type routeKey struct {
|
|
||||||
dst netip.Prefix
|
|
||||||
priority int
|
|
||||||
}
|
|
||||||
|
|
||||||
func routeKeyOf(dst *net.IPNet, family, priority int) (routeKey, bool) {
|
|
||||||
var p netip.Prefix
|
|
||||||
if dst == nil {
|
|
||||||
// A default route
|
|
||||||
if family == unix.AF_INET6 {
|
|
||||||
p = netip.PrefixFrom(netip.IPv6Unspecified(), 0)
|
|
||||||
} else {
|
|
||||||
p = netip.PrefixFrom(netip.IPv4Unspecified(), 0)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
addr, ok := netip.AddrFromSlice(dst.IP)
|
|
||||||
if !ok {
|
|
||||||
return routeKey{}, false
|
|
||||||
}
|
|
||||||
ones, _ := dst.Mask.Size()
|
|
||||||
p = netip.PrefixFrom(addr.Unmap(), ones).Masked()
|
|
||||||
}
|
|
||||||
if priority == 0 && p.Addr().Is6() {
|
|
||||||
priority = ip6RoutePrioUser
|
|
||||||
}
|
|
||||||
return routeKey{dst: p, priority: priority}, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherLinkRoutes returns the keys of the main table routes that are not on our device
|
|
||||||
func (t *tun) otherLinkRoutes() (map[routeKey]struct{}, error) {
|
|
||||||
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: unix.RT_TABLE_MAIN}, netlink.RT_FILTER_TABLE)
|
|
||||||
// An interrupted dump may miss a route, which then gets replaced like it always used to be
|
|
||||||
if err != nil && !errors.Is(err, netlink.ErrDumpInterrupted) {
|
|
||||||
return nil, fmt.Errorf("failed to list the system routes: %w", err)
|
|
||||||
}
|
|
||||||
held := make(map[routeKey]struct{}, len(routes))
|
|
||||||
for _, r := range routes {
|
|
||||||
if r.LinkIndex == t.deviceIndex {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if k, ok := routeKeyOf(r.Dst, r.Family, r.Priority); ok {
|
|
||||||
held[k] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return held, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// replaceRoute puts nr in the main table in place of our own route with its key. Replacing would take the route of
|
|
||||||
// another device holding that key, so ours goes in behind it instead. Theirs keeps routing and the kernel falls back
|
|
||||||
// to ours once theirs is removed, like when leaving the lan that had it. A different metric decides which one wins.
|
|
||||||
func (t *tun) replaceRoute(nr *netlink.Route, held map[routeKey]struct{}) error {
|
|
||||||
family := unix.AF_INET6
|
|
||||||
if nr.Dst.IP.To4() != nil {
|
|
||||||
family = unix.AF_INET
|
|
||||||
}
|
|
||||||
k, ok := routeKeyOf(nr.Dst, family, nr.Priority)
|
|
||||||
if _, taken := held[k]; !ok || !taken {
|
|
||||||
return netlink.RouteReplace(nr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appending doesn't update a route that is already ours, it adds a second one
|
|
||||||
ours := netlink.Route{
|
|
||||||
LinkIndex: nr.LinkIndex,
|
|
||||||
Dst: nr.Dst,
|
|
||||||
Scope: nr.Scope,
|
|
||||||
Table: nr.Table,
|
|
||||||
Priority: k.priority,
|
|
||||||
}
|
|
||||||
if err := netlink.RouteDel(&ours); err != nil && !errors.Is(err, unix.ESRCH) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.l.Warn("Another interface already routes this destination at the same metric, our route is used once theirs is removed", "destination", k.dst, "metric", k.priority)
|
|
||||||
return netlink.RouteAppend(nr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *tun) removeRoutes(routes []Route) {
|
func (t *tun) removeRoutes(routes []Route) {
|
||||||
for _, r := range routes {
|
for _, r := range routes {
|
||||||
if !r.Install {
|
if !r.Install {
|
||||||
|
|||||||
@@ -1,133 +0,0 @@
|
|||||||
//go:build linux && !android && !e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/DefinedNet/netlink"
|
|
||||||
"github.com/slackhq/nebula/config"
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// An unsafe_route with the same destination and metric as a lan's route must not replace it. Ours waits behind theirs
|
|
||||||
// and takes over when the lan goes away. Run as root, or unprivileged with `unshare -rn`.
|
|
||||||
func TestTunRouteLeavesAnotherDevicesRoute(t *testing.T) {
|
|
||||||
if os.Geteuid() != 0 {
|
|
||||||
t.Skip("creating links and adding routes needs root")
|
|
||||||
}
|
|
||||||
|
|
||||||
lan := &netlink.Veth{LinkAttrs: netlink.LinkAttrs{Name: "nebrt0"}, PeerName: "nebrt1"}
|
|
||||||
require.NoError(t, netlink.LinkAdd(lan))
|
|
||||||
t.Cleanup(func() { _ = netlink.LinkDel(lan) })
|
|
||||||
peer, err := netlink.LinkByName("nebrt1")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, netlink.LinkSetUp(lan))
|
|
||||||
require.NoError(t, netlink.LinkSetUp(peer))
|
|
||||||
|
|
||||||
lanAddrs := []*netlink.Addr{
|
|
||||||
{IPNet: &net.IPNet{IP: net.ParseIP("198.51.100.5"), Mask: net.CIDRMask(24, 32)}},
|
|
||||||
{IPNet: &net.IPNet{IP: net.ParseIP("2001:db8:1::5"), Mask: net.CIDRMask(64, 128)}, Flags: unix.IFA_F_NODAD},
|
|
||||||
}
|
|
||||||
for _, a := range lanAddrs {
|
|
||||||
require.NoError(t, netlink.AddrAdd(lan, a))
|
|
||||||
}
|
|
||||||
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
// The kernel puts a v4 lan route at metric 0, our default, and a v6 one at 256
|
|
||||||
require.NoError(t, c.LoadString(`
|
|
||||||
tun:
|
|
||||||
dev: nebrt2
|
|
||||||
unsafe_routes:
|
|
||||||
- route: 198.51.100.0/24
|
|
||||||
via: 10.250.0.2
|
|
||||||
- route: 2001:db8:1::/64
|
|
||||||
via: fd00:250::2
|
|
||||||
metric: 256
|
|
||||||
`))
|
|
||||||
tn, err := newTun(c, l, []netip.Prefix{netip.MustParsePrefix("10.250.0.1/24"), netip.MustParsePrefix("fd00:250::1/64")}, false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() { _ = tn.Close() })
|
|
||||||
require.NoError(t, tn.Activate())
|
|
||||||
|
|
||||||
via := func(dst string) int {
|
|
||||||
routes, err := netlink.RouteGet(net.ParseIP(dst))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotEmpty(t, routes)
|
|
||||||
return routes[0].LinkIndex
|
|
||||||
}
|
|
||||||
devices := func(dst string) []int {
|
|
||||||
_, n, err := net.ParseCIDR(dst)
|
|
||||||
require.NoError(t, err)
|
|
||||||
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Dst: n}, netlink.RT_FILTER_DST)
|
|
||||||
require.NoError(t, err)
|
|
||||||
var out []int
|
|
||||||
for _, r := range routes {
|
|
||||||
out = append(out, r.LinkIndex)
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dst := range []string{"198.51.100.0/24", "2001:db8:1::/64"} {
|
|
||||||
assert.ElementsMatch(t, []int{lan.Index, tn.deviceIndex}, devices(dst), dst)
|
|
||||||
}
|
|
||||||
assert.Equal(t, lan.Index, via("198.51.100.9"))
|
|
||||||
assert.Equal(t, lan.Index, via("2001:db8:1::9"))
|
|
||||||
|
|
||||||
// Leaving the lan
|
|
||||||
for _, a := range lanAddrs {
|
|
||||||
require.NoError(t, netlink.AddrDel(lan, a))
|
|
||||||
}
|
|
||||||
assert.Equal(t, tn.deviceIndex, via("198.51.100.9"))
|
|
||||||
assert.Equal(t, tn.deviceIndex, via("2001:db8:1::9"))
|
|
||||||
|
|
||||||
// Dropping the unsafe_routes removes only ours
|
|
||||||
for _, a := range lanAddrs {
|
|
||||||
require.NoError(t, netlink.AddrAdd(lan, a))
|
|
||||||
}
|
|
||||||
require.NoError(t, c.ReloadConfigString("tun:\n dev: nebrt2\n"))
|
|
||||||
for _, dst := range []string{"198.51.100.0/24", "2001:db8:1::/64"} {
|
|
||||||
assert.Equal(t, []int{lan.Index}, devices(dst), dst)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The route for our own vpn network is replaced to set its mtu, which must not take a lan's route for the same network
|
|
||||||
func TestTunVpnNetworkLeavesAnotherDevicesRoute(t *testing.T) {
|
|
||||||
if os.Geteuid() != 0 {
|
|
||||||
t.Skip("creating links and adding routes needs root")
|
|
||||||
}
|
|
||||||
|
|
||||||
lan := &netlink.Veth{LinkAttrs: netlink.LinkAttrs{Name: "nebrt0"}, PeerName: "nebrt1"}
|
|
||||||
require.NoError(t, netlink.LinkAdd(lan))
|
|
||||||
t.Cleanup(func() { _ = netlink.LinkDel(lan) })
|
|
||||||
require.NoError(t, netlink.LinkSetUp(lan))
|
|
||||||
require.NoError(t, netlink.AddrAdd(lan, &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP("10.250.0.5"), Mask: net.CIDRMask(24, 32)}}))
|
|
||||||
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("tun:\n dev: nebrt2\n"))
|
|
||||||
tn, err := newTun(c, l, []netip.Prefix{netip.MustParsePrefix("10.250.0.1/24")}, false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() { _ = tn.Close() })
|
|
||||||
require.NoError(t, tn.Activate())
|
|
||||||
|
|
||||||
_, n, err := net.ParseCIDR("10.250.0.0/24")
|
|
||||||
require.NoError(t, err)
|
|
||||||
routes, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &netlink.Route{Dst: n}, netlink.RT_FILTER_DST)
|
|
||||||
require.NoError(t, err)
|
|
||||||
var devices []int
|
|
||||||
for _, r := range routes {
|
|
||||||
devices = append(devices, r.LinkIndex)
|
|
||||||
if r.LinkIndex == tn.deviceIndex {
|
|
||||||
assert.Equal(t, tn.DefaultMTU, r.MTU, "ours still carries the mtu")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert.ElementsMatch(t, []int{lan.Index, tn.deviceIndex}, devices)
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AvoidCPUs removes cpus from the affinity of every thread of the process, for use at startup. New threads inherit
|
||||||
|
// their parent's mask, so it repeats until a pass finds nothing left to change; a thread that exits meanwhile is
|
||||||
|
// skipped. Every mask is checked before any is changed. With apply false it only validates, for configuration tests.
|
||||||
|
// This affects the entire process, including other users of an embedded Nebula.
|
||||||
|
func AvoidCPUs(cpus []int, apply bool) error {
|
||||||
|
const capacity = int(unsafe.Sizeof(unix.CPUSet{})) * 8
|
||||||
|
for _, cpu := range cpus {
|
||||||
|
if cpu < 0 || cpu >= capacity {
|
||||||
|
return fmt.Errorf("CPU %d is out of range", cpu)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(cpus) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
masks, err := avoidingMasks(cpus)
|
||||||
|
if err != nil || !apply || len(masks) == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for tid, mask := range masks {
|
||||||
|
if err := unix.SchedSetaffinity(tid, &mask); err != nil && !errors.Is(err, unix.ESRCH) {
|
||||||
|
return fmt.Errorf("set thread %d affinity: %w", tid, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// avoidingMasks returns the mask without cpus of every thread whose mask has any of them.
|
||||||
|
func avoidingMasks(cpus []int) (map[int]unix.CPUSet, error) {
|
||||||
|
entries, err := os.ReadDir("/proc/self/task")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
masks := make(map[int]unix.CPUSet, len(entries))
|
||||||
|
for _, entry := range entries {
|
||||||
|
tid, err := strconv.Atoi(entry.Name())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var mask unix.CPUSet
|
||||||
|
err = unix.SchedGetaffinity(tid, &mask)
|
||||||
|
if errors.Is(err, unix.ESRCH) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("read thread %d affinity: %w", tid, err)
|
||||||
|
}
|
||||||
|
next := mask
|
||||||
|
for _, cpu := range cpus {
|
||||||
|
next.Clear(cpu)
|
||||||
|
}
|
||||||
|
if next.Count() == 0 {
|
||||||
|
return nil, fmt.Errorf("avoid_cpus leaves thread %d with no allowed CPU", tid)
|
||||||
|
}
|
||||||
|
if next != mask {
|
||||||
|
masks[tid] = next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return masks, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
//go:build linux && !android && !e2e_testing
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAvoidCPUs changes the affinity of the whole process, so it runs in a child, leaving the test runner untouched.
|
||||||
|
func TestAvoidCPUs(t *testing.T) {
|
||||||
|
if os.Getenv("NEBULA_AVOID_CPU_TEST") != "1" {
|
||||||
|
exe, err := os.Executable()
|
||||||
|
require.NoError(t, err)
|
||||||
|
cmd := exec.Command(exe, "-test.run=^TestAvoidCPUs$", "-test.v")
|
||||||
|
cmd.Env = append(os.Environ(), "NEBULA_AVOID_CPU_TEST=1")
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err, "%s", out)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
allowed, err := AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
if len(allowed) < 2 {
|
||||||
|
t.Skip("requires at least two allowed CPUs")
|
||||||
|
}
|
||||||
|
cpu := allowed[0]
|
||||||
|
release := make(chan struct{})
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
// lockThreads occupies 8 OS threads until release, so the runtime has to start more
|
||||||
|
lockThreads := func() {
|
||||||
|
var ready sync.WaitGroup
|
||||||
|
for range 8 {
|
||||||
|
ready.Add(1)
|
||||||
|
wg.Go(func() {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
ready.Done()
|
||||||
|
<-release
|
||||||
|
})
|
||||||
|
}
|
||||||
|
ready.Wait()
|
||||||
|
}
|
||||||
|
lockThreads()
|
||||||
|
|
||||||
|
require.NoError(t, AvoidCPUs([]int{cpu}, false))
|
||||||
|
after, err := AllowedCPUs()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, allowed, after, "configuration check must not mutate affinity")
|
||||||
|
require.Error(t, AvoidCPUs(allowed, true), "must reject an empty mask before applying")
|
||||||
|
require.NoError(t, AvoidCPUs([]int{cpu, cpu}, true))
|
||||||
|
requireAvoided(t, cpu, allowed)
|
||||||
|
// Existing threads stay locked while new ones inherit the restriction
|
||||||
|
lockThreads()
|
||||||
|
requireAvoided(t, cpu, allowed)
|
||||||
|
close(release)
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// requireAvoided checks no thread may run on cpu, and every thread still may on some CPU of allowed only.
|
||||||
|
func requireAvoided(t *testing.T, cpu int, allowed []int) {
|
||||||
|
t.Helper()
|
||||||
|
entries, err := os.ReadDir("/proc/self/task")
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, entry := range entries {
|
||||||
|
tid, err := strconv.Atoi(entry.Name())
|
||||||
|
require.NoError(t, err)
|
||||||
|
var mask unix.CPUSet
|
||||||
|
err = unix.SchedGetaffinity(tid, &mask)
|
||||||
|
if err == unix.ESRCH {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, mask.IsSet(cpu), "thread %d", tid)
|
||||||
|
require.Positive(t, mask.Count())
|
||||||
|
for id := range len(mask) * 64 {
|
||||||
|
if mask.IsSet(id) {
|
||||||
|
require.Contains(t, allowed, id, "must not widen the inherited mask")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAvoidCPUsRange checks the bounds of unix.CPUSet, whose words are 32 bits wide on 32-bit platforms.
|
||||||
|
func TestAvoidCPUsRange(t *testing.T) {
|
||||||
|
capacity := int(unsafe.Sizeof(unix.CPUSet{})) * 8
|
||||||
|
require.NoError(t, AvoidCPUs([]int{capacity - 1}, false))
|
||||||
|
require.EqualError(t, AvoidCPUs([]int{capacity}, false), fmt.Sprintf("CPU %d is out of range", capacity))
|
||||||
|
require.EqualError(t, AvoidCPUs([]int{-1}, false), "CPU -1 is out of range")
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
//go:build !linux || android || e2e_testing
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// AvoidCPUs is unsupported outside Linux: there is no thread affinity API to restrict.
|
||||||
|
func AvoidCPUs(_ []int, _ bool) error { return errors.ErrUnsupported }
|
||||||
Reference in new issue
Block a user