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:
committed by
Brad Fitzpatrick
parent
63f7a400a8
commit
232b928974
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Code generated by gen.go; DO NOT EDIT.
|
||||
|
||||
//go:build ts_omit_linkspeed
|
||||
|
||||
package buildfeatures
|
||||
|
||||
// HasLinkSpeed is whether the binary was built with support for modular feature "Set link speed on TUN device for better OS integration (Linux only)".
|
||||
// Specifically, it's whether the binary was NOT built with the "ts_omit_linkspeed" build tag.
|
||||
// It's a const so it can be used for dead code elimination.
|
||||
const HasLinkSpeed = false
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Code generated by gen.go; DO NOT EDIT.
|
||||
|
||||
//go:build !ts_omit_linkspeed
|
||||
|
||||
package buildfeatures
|
||||
|
||||
// HasLinkSpeed is whether the binary was built with support for modular feature "Set link speed on TUN device for better OS integration (Linux only)".
|
||||
// Specifically, it's whether the binary was NOT built with the "ts_omit_linkspeed" build tag.
|
||||
// It's a const so it can be used for dead code elimination.
|
||||
const HasLinkSpeed = true
|
||||
@@ -0,0 +1,8 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build linux && !android && !ts_omit_linkspeed
|
||||
|
||||
package condregister
|
||||
|
||||
import _ "tailscale.com/feature/linkspeed"
|
||||
@@ -161,6 +161,10 @@ var Features = map[FeatureTag]FeatureMeta{
|
||||
"kube": {Sym: "Kube", Desc: "Kubernetes integration"},
|
||||
"lazywg": {Sym: "LazyWG", Desc: "Lazy WireGuard configuration for memory-constrained devices with large netmaps"},
|
||||
"linuxdnsfight": {Sym: "LinuxDNSFight", Desc: "Linux support for detecting DNS fights (inotify watching of /etc/resolv.conf)"},
|
||||
"linkspeed": {
|
||||
Sym: "LinkSpeed",
|
||||
Desc: "Set link speed on TUN device for better OS integration (Linux only)",
|
||||
},
|
||||
"listenrawdisco": {
|
||||
Sym: "ListenRawDisco",
|
||||
Desc: "Use raw sockets for more robust disco (NAT traversal) message receiving (Linux only)",
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
// Package linkspeed registers support for setting the TUN link speed on Linux,
|
||||
// to better integrate with system monitoring tools.
|
||||
package linkspeed
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build linux && !android
|
||||
|
||||
package linkspeed
|
||||
|
||||
import (
|
||||
"github.com/mdlayher/genetlink"
|
||||
"github.com/mdlayher/netlink"
|
||||
"github.com/tailscale/wireguard-go/tun"
|
||||
"golang.org/x/sys/unix"
|
||||
"tailscale.com/net/tstun"
|
||||
)
|
||||
|
||||
func init() {
|
||||
tstun.HookSetLinkAttrs.Set(setLinkAttrs)
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
Reference in New Issue
Block a user