mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 09:36:52 +01:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f59f895a4 | ||
|
|
df1c4d9364 | ||
|
|
36579bae03 | ||
|
|
73d26dd87d |
No files matched your search
@@ -0,0 +1,75 @@
|
||||
//go:build linux && !android && !e2e_testing
|
||||
|
||||
package cpupick
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Avoid 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. Not wired up yet: tun.avoid_cpus
|
||||
// only steers Default for now.
|
||||
func Avoid(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,98 @@
|
||||
//go:build linux && !android && !e2e_testing
|
||||
|
||||
package cpupick
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/slackhq/nebula/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// TestAvoid changes the affinity of the whole process, so it runs in a child, leaving the test runner untouched.
|
||||
func TestAvoid(t *testing.T) {
|
||||
if os.Getenv("NEBULA_CPUPICK_AVOID_TEST") != "1" {
|
||||
exe, err := os.Executable()
|
||||
require.NoError(t, err)
|
||||
cmd := exec.Command(exe, "-test.run=^TestAvoid$", "-test.v")
|
||||
cmd.Env = append(os.Environ(), "NEBULA_CPUPICK_AVOID_TEST=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
require.NoError(t, err, "%s", out)
|
||||
return
|
||||
}
|
||||
allowed, err := util.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, Avoid([]int{cpu}, false))
|
||||
after, err := util.AllowedCPUs()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, allowed, after, "configuration check must not mutate affinity")
|
||||
require.Error(t, Avoid(allowed, true), "must reject an empty mask before applying")
|
||||
require.NoError(t, Avoid([]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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestAvoidRange checks the bounds of unix.CPUSet, whose words are 32 bits wide on 32-bit platforms.
|
||||
func TestAvoidRange(t *testing.T) {
|
||||
capacity := int(unsafe.Sizeof(unix.CPUSet{})) * 8
|
||||
require.NoError(t, Avoid([]int{capacity - 1}, false))
|
||||
require.EqualError(t, Avoid([]int{capacity}, false), fmt.Sprintf("CPU %d is out of range", capacity))
|
||||
require.EqualError(t, Avoid([]int{-1}, false), "CPU -1 is out of range")
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build !linux || android || e2e_testing
|
||||
|
||||
package cpupick
|
||||
|
||||
import "errors"
|
||||
|
||||
// Avoid is unsupported outside Linux: there is no thread affinity API to restrict.
|
||||
func Avoid(_ []int, _ bool) error { return errors.ErrUnsupported }
|
||||
+17
-6
@@ -16,10 +16,15 @@
|
||||
// across distinct physical cores when the topology permits, CPU 0's physical
|
||||
// core demoted to last resort, and the order rotated by a stable per-instance
|
||||
// key so co-located instances spread instead of stacking.
|
||||
//
|
||||
// CPUs the operator asked to keep readers off (tun.avoid_cpus, typically the
|
||||
// underlay NIC's receive IRQ CPUs) are removed from the allowed set before
|
||||
// any of that, so no rule can bring one back.
|
||||
package cpupick
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"slices"
|
||||
|
||||
"github.com/slackhq/nebula/util"
|
||||
)
|
||||
@@ -57,12 +62,17 @@ func flatTopology(cpus []int) topology {
|
||||
// Default computes the pin order for `routines` tun readers. key is any
|
||||
// stable per-instance value; the bound UDP port is ideal — distinct across
|
||||
// co-located instances, stable across restarts so benchmark runs stay
|
||||
// comparable. Returns nil when there is nothing useful to say (no affinity
|
||||
// support on this platform, lookup failure); callers keep their existing
|
||||
// fallback spread.
|
||||
func Default(routines int, key uint64, l *slog.Logger) []int {
|
||||
// comparable. avoid lists CPUs never to return, matched literally. Returns
|
||||
// nil when there is nothing useful to say (no affinity support on this
|
||||
// platform, lookup failure, avoid covering every allowed CPU); callers keep
|
||||
// their existing fallback spread.
|
||||
func Default(routines int, key uint64, avoid []int, l *slog.Logger) []int {
|
||||
allowed, err := util.AllowedCPUs()
|
||||
if err != nil || len(allowed) == 0 {
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
allowed = slices.DeleteFunc(allowed, func(c int) bool { return slices.Contains(avoid, c) })
|
||||
if len(allowed) == 0 {
|
||||
return nil
|
||||
}
|
||||
perf, signal := perfCPUs(allowed)
|
||||
@@ -77,7 +87,8 @@ func Default(routines int, key uint64, l *slog.Logger) []int {
|
||||
if l != nil {
|
||||
l.Info("chose default pin CPUs for tun readers",
|
||||
"cpus", cpus[:min(routines, len(cpus))],
|
||||
"perfSignal", signal)
|
||||
"perfSignal", signal,
|
||||
"avoided", avoid)
|
||||
}
|
||||
return cpus
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
//go:build linux && !android && !e2e_testing
|
||||
|
||||
package cpupick
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/slackhq/nebula/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDefaultSkipsAvoided(t *testing.T) {
|
||||
allowed, err := util.AllowedCPUs()
|
||||
require.NoError(t, err)
|
||||
if len(allowed) < 2 {
|
||||
t.Skip("requires at least two allowed CPUs")
|
||||
}
|
||||
avoid := allowed[:len(allowed)/2]
|
||||
for key := range uint64(16) {
|
||||
got := Default(len(allowed), key, avoid, nil)
|
||||
require.NotEmpty(t, got)
|
||||
for _, cpu := range got {
|
||||
assert.NotContains(t, avoid, cpu, "key %d", key)
|
||||
assert.Contains(t, allowed, cpu, "key %d", key)
|
||||
}
|
||||
}
|
||||
assert.Nil(t, Default(2, 0, allowed, nil), "avoiding everything leaves nothing to suggest")
|
||||
|
||||
// Without avoid every allowed CPU stays in play
|
||||
got := Default(len(allowed), 0, nil, nil)
|
||||
slices.Sort(got)
|
||||
assert.Equal(t, allowed, got)
|
||||
}
|
||||
@@ -295,6 +295,15 @@ tun:
|
||||
# - 2
|
||||
# - 4
|
||||
|
||||
# Linux only. avoid_cpus lists CPU IDs the tun readers are never pinned to, exactly as listed (SMT siblings are not
|
||||
# added for you). The default pin selection skips them, and a cpu_affinity entry naming one is rejected like any
|
||||
# other invalid entry. Other threads are not restricted, and the setting does nothing with pin_threads: false. An
|
||||
# invalid list, or one covering every allowed CPU, is ignored with a warning. Not reloadable. 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 if you want those kept clear
|
||||
# too, 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
|
||||
routes:
|
||||
#- mtu: 8800
|
||||
|
||||
+9
-1
@@ -60,6 +60,9 @@ type InterfaceConfig struct {
|
||||
// shorter lists than `routines` cycle. Empty list keeps the default
|
||||
// pin-to-(i % NumCPU) behavior. Only consulted when PinThreads is true.
|
||||
CpuAffinity []int
|
||||
// AvoidCPUs (tun.avoid_cpus) lists CPUs the default pin spread skips when
|
||||
// CpuAffinity is empty. Only consulted when PinThreads is true.
|
||||
AvoidCPUs []int
|
||||
// PinThreads controls whether each TUN reader OS thread is pinned to a
|
||||
// single CPU (via tun.pin_threads, default true). Pinning keeps each
|
||||
// goroutine's sendmmsg on one XPS-selected NIC TX ring so per-flow
|
||||
@@ -95,6 +98,8 @@ type Interface struct {
|
||||
// Empty falls back to the default pin-to-(allowed CPU) behavior.
|
||||
// Only consulted when pinThreads is true.
|
||||
cpuAffinity []int
|
||||
// avoidCPUs are removed from that fallback spread, unless they would empty it.
|
||||
avoidCPUs []int
|
||||
// pinThreads controls whether listenIn pins each TUN reader OS thread to
|
||||
// a CPU at all (tun.pin_threads, default true). When false, threads are
|
||||
// left free to migrate as on stock nebula.
|
||||
@@ -247,6 +252,7 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
||||
connectionManager: c.connectionManager,
|
||||
conntrackCacheTimeout: c.ConntrackCacheTimeout,
|
||||
cpuAffinity: c.CpuAffinity,
|
||||
avoidCPUs: c.AvoidCPUs,
|
||||
pinThreads: c.PinThreads,
|
||||
udpTxErrLimiter: udpTxErrLimiter,
|
||||
udpRelayTxErrLimiter: udpRelayTxErrLimiter,
|
||||
@@ -437,12 +443,14 @@ func (f *Interface) pinThisThread(i int) {
|
||||
var cpu int
|
||||
if n := len(f.cpuAffinity); n > 0 {
|
||||
// Explicit tun.cpu_affinity list wins; parseCpuAffinity already
|
||||
// validated the entries against the allowed CPU set.
|
||||
// validated the entries against the allowed CPU set and tun.avoid_cpus.
|
||||
cpu = f.cpuAffinity[i%n]
|
||||
} else if allowed, err := util.AllowedCPUs(); err == nil && len(allowed) > 0 {
|
||||
// Default: spread queues across the CPUs we're actually allowed to
|
||||
// run on. Under a cpuset/taskset mask these aren't 0..NumCPU-1, so
|
||||
// i % NumCPU would pick unrunnable IDs and every pin would fail.
|
||||
// parseAvoidCPUs already dropped an avoid list that covers them all.
|
||||
allowed = slices.DeleteFunc(allowed, func(c int) bool { return slices.Contains(f.avoidCPUs, c) })
|
||||
cpu = allowed[i%len(allowed)]
|
||||
} else {
|
||||
cpu = i % runtime.NumCPU()
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/slackhq/nebula/cert"
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/slackhq/nebula/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -118,3 +119,22 @@ func TestReloadFirewall_NoChange(t *testing.T) {
|
||||
|
||||
assert.Same(t, fw, f.firewall, "firewall should not have been replaced")
|
||||
}
|
||||
|
||||
// TestPinThisThreadFallbackSkipsAvoided covers the single reader, which never goes through cpupick: its fallback
|
||||
// spread over the allowed set must still skip tun.avoid_cpus.
|
||||
func TestPinThisThreadFallbackSkipsAvoided(t *testing.T) {
|
||||
allowed, err := util.AllowedCPUs()
|
||||
require.NoError(t, err)
|
||||
if len(allowed) < 2 {
|
||||
t.Skip("requires at least two allowed CPUs")
|
||||
}
|
||||
f := &Interface{l: test.NewLogger(), pinThreads: true, avoidCPUs: []int{allowed[0]}}
|
||||
got := make(chan []int)
|
||||
go func() {
|
||||
// The pin leaves this goroutine locked to its thread, so the runtime discards the thread when it returns.
|
||||
f.pinThisThread(0)
|
||||
cpus, _ := util.AllowedCPUs()
|
||||
got <- cpus
|
||||
}()
|
||||
assert.Equal(t, []int{allowed[1]}, <-got)
|
||||
}
|
||||
@@ -243,11 +243,19 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
||||
}
|
||||
|
||||
pinThreads := c.GetBool("tun.pin_threads", true)
|
||||
cpuAffinity := parseCpuAffinity(c, l, routines)
|
||||
avoidCPUs := parseAvoidCPUs(c, l)
|
||||
if len(avoidCPUs) > 0 && !pinThreads {
|
||||
l.Warn("tun.avoid_cpus has no effect with tun.pin_threads disabled")
|
||||
}
|
||||
// Keeping every thread of the process off avoidCPUs, not just the pinned readers, is parked for now:
|
||||
//if err := cpupick.Avoid(avoidCPUs, !configTest); err != nil && !errors.Is(err, errors.ErrUnsupported) {
|
||||
// return nil, fmt.Errorf("tun.avoid_cpus: %w", err)
|
||||
//}
|
||||
cpuAffinity := parseCpuAffinity(c, l, routines, avoidCPUs)
|
||||
if pinThreads && routines > 1 && len(cpuAffinity) == 0 && !configTest {
|
||||
// The operator didn't choose pin CPUs, so pick a default set that
|
||||
// prefers performance cores and doesn't stack co-located instances
|
||||
// onto allowed[0].
|
||||
// prefers performance cores, skips tun.avoid_cpus and doesn't stack
|
||||
// co-located instances onto allowed[0].
|
||||
|
||||
// key is used to seed the spreading of routines->cores.
|
||||
// use PID if you want to ensure many different Nebulas in VMs or containers land on different cores
|
||||
@@ -270,7 +278,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
||||
l.Warn("tun.pin_threads_key is invalid, using PID")
|
||||
}
|
||||
|
||||
cpuAffinity = cpupick.Default(routines, key, l)
|
||||
cpuAffinity = cpupick.Default(routines, key, avoidCPUs, l)
|
||||
}
|
||||
|
||||
ifConfig := &InterfaceConfig{
|
||||
@@ -295,6 +303,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
||||
punchy: punchy,
|
||||
ConntrackCacheTimeout: conntrackCacheTimeout,
|
||||
CpuAffinity: cpuAffinity,
|
||||
AvoidCPUs: avoidCPUs,
|
||||
PinThreads: pinThreads,
|
||||
l: l,
|
||||
}
|
||||
@@ -363,8 +372,10 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
||||
// mask (util.AllowedCPUs) rather than 0..NumCPU-1: under a cgroup cpuset or
|
||||
// taskset the runnable IDs are frequently not that contiguous range, and
|
||||
// pinning to an unrunnable ID always fails. If the allowed set can't be
|
||||
// determined we fall back to a plain non-negative check.
|
||||
func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
||||
// determined we fall back to a plain non-negative check. An entry listed in
|
||||
// avoid (tun.avoid_cpus) contradicts the operator's own config and is treated
|
||||
// the same as any other invalid entry.
|
||||
func parseCpuAffinity(c *config.C, l *slog.Logger, routines int, avoid []int) []int {
|
||||
raw := c.Get("tun.cpu_affinity")
|
||||
if raw == nil {
|
||||
return nil
|
||||
@@ -407,6 +418,11 @@ func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
||||
"index", i, "cpu", cpu, "allowed", allowed)
|
||||
return nil
|
||||
}
|
||||
if slices.Contains(avoid, cpu) {
|
||||
l.Warn("tun.cpu_affinity entry is listed in tun.avoid_cpus; ignoring affinity",
|
||||
"index", i, "cpu", cpu)
|
||||
return nil
|
||||
}
|
||||
cpus = append(cpus, cpu)
|
||||
}
|
||||
if len(cpus) != routines {
|
||||
@@ -416,6 +432,36 @@ func parseCpuAffinity(c *config.C, l *slog.Logger, routines int) []int {
|
||||
return cpus
|
||||
}
|
||||
|
||||
// parseAvoidCPUs reads tun.avoid_cpus, a list of CPU IDs the tun readers must not be pinned to. IDs outside the
|
||||
// allowed set are accepted, since one config may serve differently shaped hosts. Like tun.cpu_affinity, an invalid
|
||||
// list is a warning and is ignored entirely, as is one covering every allowed CPU, which would leave nowhere to pin.
|
||||
func parseAvoidCPUs(c *config.C, l *slog.Logger) []int {
|
||||
raw := c.Get("tun.avoid_cpus")
|
||||
if raw == nil {
|
||||
return nil
|
||||
}
|
||||
values, ok := raw.([]any)
|
||||
if !ok {
|
||||
l.Warn("tun.avoid_cpus must be a list of integers; ignoring", "value", raw)
|
||||
return nil
|
||||
}
|
||||
cpus := make([]int, 0, len(values))
|
||||
for i, value := range values {
|
||||
cpu, ok := value.(int)
|
||||
if !ok || cpu < 0 {
|
||||
l.Warn("tun.avoid_cpus entry not a non-negative integer; ignoring avoid_cpus", "index", i, "value", value)
|
||||
return nil
|
||||
}
|
||||
cpus = append(cpus, cpu)
|
||||
}
|
||||
if allowed, err := util.AllowedCPUs(); err == nil && len(allowed) > 0 &&
|
||||
!slices.ContainsFunc(allowed, func(cpu int) bool { return !slices.Contains(cpus, cpu) }) {
|
||||
l.Warn("tun.avoid_cpus covers every allowed CPU; ignoring avoid_cpus", "cpus", cpus, "allowed", allowed)
|
||||
return nil
|
||||
}
|
||||
return cpus
|
||||
}
|
||||
|
||||
func moduleVersion() string {
|
||||
info, ok := debug.ReadBuildInfo()
|
||||
if !ok {
|
||||
|
||||
+36
-5
@@ -32,7 +32,7 @@ func TestParseCpuAffinity(t *testing.T) {
|
||||
}
|
||||
|
||||
// unset -> nil (listenIn falls back to spreading across the allowed set)
|
||||
assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1))
|
||||
assert.Nil(t, parseCpuAffinity(newConfig(nil), l, 1, nil))
|
||||
|
||||
// Pick a CPU we're actually allowed to run on so a valid list survives
|
||||
// validation regardless of the host's affinity mask.
|
||||
@@ -43,19 +43,50 @@ func TestParseCpuAffinity(t *testing.T) {
|
||||
}
|
||||
|
||||
// valid list -> parsed through unchanged
|
||||
assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2))
|
||||
assert.Equal(t, []int{validCPU, validCPU}, parseCpuAffinity(newConfig([]any{validCPU, validCPU}), l, 2, nil))
|
||||
|
||||
// a negative entry is out of range on every platform -> disables the override
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2))
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, -1}), l, 2, nil))
|
||||
|
||||
// a non-integer entry -> disables the override
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2))
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU, "not-a-cpu"}), l, 2, nil))
|
||||
|
||||
// a CPU id outside the allowed set -> disables the override. Only assertable
|
||||
// where we can enumerate the allowed set (e.g. linux); 1<<20 is far beyond
|
||||
// any representable CPU id so it can never be in the mask.
|
||||
if len(allowed) > 0 {
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1))
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1, nil))
|
||||
}
|
||||
|
||||
// an entry also listed in tun.avoid_cpus -> disables the override
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{validCPU}), l, 1, []int{validCPU}))
|
||||
}
|
||||
|
||||
func TestParseAvoidCPUs(t *testing.T) {
|
||||
l := test.NewLogger()
|
||||
for _, tc := range []struct {
|
||||
value any
|
||||
want []int
|
||||
}{
|
||||
{nil, nil}, {[]any{}, []int{}}, {[]any{1, 1 << 20}, []int{1, 1 << 20}},
|
||||
{"auto", nil}, {"1,2", nil}, {[]any{-1}, nil}, {[]any{1.5}, nil}, {[]any{1, "2"}, nil},
|
||||
} {
|
||||
c := config.NewC(l)
|
||||
if tc.value != nil {
|
||||
c.Settings["tun"] = map[string]any{"avoid_cpus": tc.value}
|
||||
}
|
||||
assert.Equal(t, tc.want, parseAvoidCPUs(c, l), "%v", tc.value)
|
||||
}
|
||||
|
||||
// avoiding every allowed CPU leaves nowhere to pin; only checkable where the allowed set is known
|
||||
if allowed, _ := util.AllowedCPUs(); len(allowed) > 0 {
|
||||
all := make([]any, len(allowed))
|
||||
for i, cpu := range allowed {
|
||||
all[i] = cpu
|
||||
}
|
||||
c := config.NewC(l)
|
||||
c.Settings["tun"] = map[string]any{"avoid_cpus": all}
|
||||
assert.Nil(t, parseAvoidCPUs(c, l))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user