feature/linkspeed: move cosmetic tstun netlink code out to modular feature

Part of making all netlink monitoring code optional.

Updates #17311 (how I got started down this path)
Updates #12614

Change-Id: Ic80d8a7a44dc261c4b8678b3c2241c3b3778370d
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2025-10-06 22:08:38 -07:00
committed by Brad Fitzpatrick
parent 63f7a400a8
commit 232b928974
15 changed files with 72 additions and 30 deletions
-65
View File
@@ -1,65 +0,0 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build !android
package tstun
import (
"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
"github.com/tailscale/wireguard-go/tun"
"golang.org/x/sys/unix"
)
// setLinkSpeed sets the advertised link speed of the TUN interface.
func setLinkSpeed(iface tun.Device, mbps int) error {
name, err := iface.Name()
if err != nil {
return err
}
conn, err := genetlink.Dial(&netlink.Config{Strict: true})
if err != nil {
return err
}
defer conn.Close()
f, err := conn.GetFamily(unix.ETHTOOL_GENL_NAME)
if err != nil {
return err
}
ae := netlink.NewAttributeEncoder()
ae.Nested(unix.ETHTOOL_A_LINKMODES_HEADER, func(nae *netlink.AttributeEncoder) error {
nae.String(unix.ETHTOOL_A_HEADER_DEV_NAME, name)
return nil
})
ae.Uint32(unix.ETHTOOL_A_LINKMODES_SPEED, uint32(mbps))
b, err := ae.Encode()
if err != nil {
return err
}
_, err = conn.Execute(
genetlink.Message{
Header: genetlink.Header{
Command: unix.ETHTOOL_MSG_LINKMODES_SET,
Version: unix.ETHTOOL_GENL_VERSION,
},
Data: b,
},
f.ID,
netlink.Request|netlink.Acknowledge,
)
return err
}
// setLinkAttrs sets up link attributes that can be queried by external tools.
// Its failure is non-fatal to interface bringup.
func setLinkAttrs(iface tun.Device) error {
// By default the link speed is 10Mbps, which is easily exceeded and causes monitoring tools to complain (#3933).
return setLinkSpeed(iface, unix.SPEED_UNKNOWN)
}
-12
View File
@@ -1,12 +0,0 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build !linux || android
package tstun
import "github.com/tailscale/wireguard-go/tun"
func setLinkAttrs(iface tun.Device) error {
return nil
}
+11 -3
View File
@@ -18,12 +18,16 @@ import (
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/feature"
"tailscale.com/feature/buildfeatures"
"tailscale.com/types/logger"
)
// CreateTAP is the hook set by feature/tap.
// CreateTAP is the hook maybe set by feature/tap.
var CreateTAP feature.Hook[func(logf logger.Logf, tapName, bridgeName string) (tun.Device, error)]
// HookSetLinkAttrs is the hook maybe set by feature/linkspeed.
var HookSetLinkAttrs feature.Hook[func(tun.Device) error]
// modprobeTunHook is a Linux-specific hook to run "/sbin/modprobe tun".
var modprobeTunHook feature.Hook[func() error]
@@ -78,8 +82,12 @@ func New(logf logger.Logf, tunName string) (tun.Device, string, error) {
dev.Close()
return nil, "", err
}
if err := setLinkAttrs(dev); err != nil {
logf("setting link attributes: %v", err)
if buildfeatures.HasLinkSpeed {
if f, ok := HookSetLinkAttrs.GetOk(); ok {
if err := f(dev); err != nil {
logf("setting link attributes: %v", err)
}
}
}
name, err := interfaceName(dev)
if err != nil {