mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
feature/routecheck,ipn,net/routecheck,tailcfg: Probe conn25 connectors
Add conn25 connectors to the targets for routecheck probing. Enables conn25 clients to use the routecheck report to help decide which connector to use. Updates tailscale/corp#47251 Signed-off-by: Fran Bull <fran@tailscale.com> Signed-off-by: Guy J Grigsby <guy@tailscale.com>
This commit is contained in:
1 parent
3745416cc6
commit
2d0af3590e
10 files changed
+273
-7
No files matched your search
@@ -91,10 +91,20 @@ func printRouteCheckReport(rp *routecheck.Report) error {
|
||||
}
|
||||
}
|
||||
|
||||
apps := rp.RoutableApps()
|
||||
|
||||
// Don’t render apps that only have one connector:
|
||||
for app, nodes := range apps {
|
||||
if len(nodes) <= 1 {
|
||||
delete(apps, app)
|
||||
}
|
||||
}
|
||||
|
||||
if enc != nil {
|
||||
out := tsroutecheckjsonv0.ReportResponse{
|
||||
Done: rp.Done,
|
||||
Routes: routes,
|
||||
Apps: apps,
|
||||
}
|
||||
if err := jsonv2.MarshalEncode(enc, out); err != nil {
|
||||
return err
|
||||
@@ -118,5 +128,19 @@ func printRouteCheckReport(rp *routecheck.Report) error {
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
|
||||
if len(apps) > 0 {
|
||||
fmt.Fprintf(w, "\nReachable connectors at %s:\n", rp.Done.Local().Format(tstime.DateSpTimeZ))
|
||||
fmt.Fprintf(w, "\n %s\t%s\t%s", "APP", "IP", "HOSTNAME")
|
||||
for app, nodes := range apps.Sorted() {
|
||||
slices.SortFunc(nodes, func(a, b routecheck.Node) int {
|
||||
return cmp.Compare(a.Name, b.Name) // order by hostname
|
||||
})
|
||||
for _, n := range nodes {
|
||||
fmt.Fprintf(w, "\n %s\t%s\t%s", app, n.Addr, strings.TrimSuffix(n.Name, "."))
|
||||
}
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -25,4 +25,5 @@ import (
|
||||
type ReportResponse struct {
|
||||
Done time.Time `json:"done"`
|
||||
Routes routecheck.RoutablePrefixes `json:"routes"`
|
||||
Apps routecheck.RoutableApps `json:"apps,omitempty"`
|
||||
}
|
||||
@@ -185,7 +185,7 @@ func (rt *RouterTracker) watchIPNBus(ctx context.Context, done chan<- struct{},
|
||||
// Bootstrap the router set from the initial Status.
|
||||
// This will trigger the initial probe for all routers.
|
||||
for _, ps := range s.Peer {
|
||||
if ps.IsRouter() {
|
||||
if ps.IsRouter() || ps.IsConn25Connector() {
|
||||
nid := ps.NodeID
|
||||
routers.Add(nid)
|
||||
added = append(added, nid)
|
||||
@@ -194,17 +194,17 @@ func (rt *RouterTracker) watchIPNBus(ctx context.Context, done chan<- struct{},
|
||||
}
|
||||
for _, p := range n.PeersChanged {
|
||||
nid := p.ID
|
||||
wasRouter := routers.Contains(p.ID)
|
||||
isRouter := p.IsRouter()
|
||||
wasTracked := routers.Contains(p.ID)
|
||||
isProbeTarget := p.IsRouter() || p.IsConn25Connector()
|
||||
switch {
|
||||
case !wasRouter && isRouter:
|
||||
case !wasTracked && isProbeTarget:
|
||||
routers.Add(nid)
|
||||
added = append(added, nid)
|
||||
case wasRouter && isRouter:
|
||||
case wasTracked && isProbeTarget:
|
||||
// TODO(sfllaw): Tune this to ignore changes
|
||||
// that don’t affect this node’s status as a router.
|
||||
modified = append(modified, nid)
|
||||
case wasRouter && !isRouter:
|
||||
case wasTracked && !isProbeTarget:
|
||||
routers.Delete(nid)
|
||||
removed = append(removed, nid)
|
||||
}
|
||||
|
||||
@@ -110,6 +110,15 @@ func TestRouterTracker(t *testing.T) {
|
||||
wantModified: nil,
|
||||
wantRemoved: []tailcfg.NodeID{6},
|
||||
},
|
||||
{
|
||||
name: "conn25-connector",
|
||||
bus: ipnBus{
|
||||
{PeersChanged: []*tailcfg.Node{makeNode(7, withConn25Connector("example"))}},
|
||||
},
|
||||
wantAdded: []tailcfg.NodeID{7},
|
||||
wantModified: nil,
|
||||
wantRemoved: nil,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
@@ -266,3 +275,16 @@ func withRoutes(routes ...netip.Prefix) nodeOptFunc {
|
||||
n.AllowedIPs = append(n.AllowedIPs, routes...)
|
||||
}
|
||||
}
|
||||
|
||||
func withConn25Connector(apps ...string) nodeOptFunc {
|
||||
return func(n *tailcfg.Node) {
|
||||
vals := make([]tailcfg.RawMessage, 0, len(apps))
|
||||
for _, app := range apps {
|
||||
vals = append(vals, tailcfg.RawMessage(fmt.Sprintf("%q", app)))
|
||||
}
|
||||
if n.CapMap == nil {
|
||||
n.CapMap = tailcfg.NodeCapMap{}
|
||||
}
|
||||
n.CapMap[nodecap.Conn25Connector] = vals
|
||||
}
|
||||
}
|
||||
@@ -388,6 +388,11 @@ func (ps *PeerStatus) IsTagged() bool {
|
||||
return ps.Tags != nil && ps.Tags.Len() > 0
|
||||
}
|
||||
|
||||
// IsConn25Connector reports whether ps describes a conn25 connector.
|
||||
func (ps *PeerStatus) IsConn25Connector() bool {
|
||||
return ps.HasCap(nodecap.Conn25Connector)
|
||||
}
|
||||
|
||||
// StatusBuilder is a request to construct a Status. A new StatusBuilder is
|
||||
// passed to various subsystems which then call methods on it to populate state.
|
||||
// Call its Status method to return the final constructed Status.
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"tailscale.com/ipn/ipnstate"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tailcfg/nodecap"
|
||||
"tailscale.com/types/views"
|
||||
)
|
||||
|
||||
@@ -162,3 +164,32 @@ func TestPeerStatusIsRouter(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsConn25Connector(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
status *ipnstate.PeerStatus
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "plain",
|
||||
status: &ipnstate.PeerStatus{},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "connector",
|
||||
status: &ipnstate.PeerStatus{
|
||||
CapMap: tailcfg.NodeCapMap{
|
||||
nodecap.Conn25Connector: nil,
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := tc.status.IsConn25Connector(); got != tc.want {
|
||||
t.Errorf("got %t, want %t", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -16,6 +16,7 @@ import (
|
||||
"tailscale.com/net/traffic"
|
||||
"tailscale.com/syncs"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tailcfg/nodecap"
|
||||
"tailscale.com/tsconst"
|
||||
"tailscale.com/util/clientmetric"
|
||||
"tailscale.com/util/mak"
|
||||
@@ -37,6 +38,7 @@ type probed struct {
|
||||
tailcfg.NodeView
|
||||
addr netip.Addr
|
||||
routes []netip.Prefix
|
||||
apps []string
|
||||
}
|
||||
|
||||
func (c *Client) probe(ctx context.Context, nodes iter.Seq[probed], limit int, timeout time.Duration) (*Report, error) {
|
||||
@@ -66,6 +68,7 @@ func (c *Client) probe(ctx context.Context, nodes iter.Seq[probed], limit int, t
|
||||
Name: n.Name(),
|
||||
Addr: n.addr,
|
||||
Routes: n.routes,
|
||||
Apps: n.apps,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -184,10 +187,12 @@ func (c *Client) Probe(ctx context.Context, nodes iter.Seq[tailcfg.NodeView], li
|
||||
if !addr.IsValid() {
|
||||
continue // No valid addresses.
|
||||
}
|
||||
apps, _ := connectorApps(n)
|
||||
if !yield(probed{
|
||||
NodeView: n,
|
||||
addr: addr,
|
||||
routes: routes(n),
|
||||
apps: apps,
|
||||
}) {
|
||||
return
|
||||
}
|
||||
@@ -208,17 +213,43 @@ func (c *Client) ProbeAllHARouters(ctx context.Context, limit int, timeout time.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
peers := c.nb.NodeBackend().Peers()
|
||||
// When a prefix is routed by multiple nodes, we probe those nodes.
|
||||
// There is no point to probing a router when it is the only choice.
|
||||
// These nodes are referred to a High Availability (HA) routers.
|
||||
var nodes []tailcfg.NodeView
|
||||
for _, rs := range c.RoutersByPrefix() {
|
||||
for _, rs := range GroupRoutersByPrefix(peers) {
|
||||
if len(rs) <= 1 {
|
||||
continue
|
||||
}
|
||||
nodes = append(nodes, rs...) // Note: this introduces duplicates.
|
||||
}
|
||||
|
||||
// Group conn25 connectors by the app they serve, then probe only
|
||||
// connectors for apps served by more than one node, since there is
|
||||
// no point probing a connector when it is the only choice for its app.
|
||||
//
|
||||
// It is the control plane's responsibility to only provide app names on
|
||||
// peers that are eligible connectors for this client, so there is no need
|
||||
// to filter the peers here by the apps the self node uses.
|
||||
connectorsByApp := make(map[string][]tailcfg.NodeView)
|
||||
for _, nv := range peers {
|
||||
apps, err := connectorApps(nv)
|
||||
if err != nil {
|
||||
c.logf("routecheck: bad %s cap on node %v: %v", nodecap.Conn25Connector, nv.ID(), err)
|
||||
continue
|
||||
}
|
||||
for _, app := range apps {
|
||||
connectorsByApp[app] = append(connectorsByApp[app], nv)
|
||||
}
|
||||
}
|
||||
for _, cs := range connectorsByApp {
|
||||
if len(cs) <= 1 {
|
||||
continue
|
||||
}
|
||||
nodes = append(nodes, cs...)
|
||||
}
|
||||
|
||||
// Sort by Node.ID and deduplicate to avoid double-probing.
|
||||
slices.SortFunc(nodes, func(a, b tailcfg.NodeView) int {
|
||||
return cmp.Compare(a.ID(), b.ID())
|
||||
@@ -295,3 +326,8 @@ func addrPicker(can4, can6 bool) func(n tailcfg.NodeView) netip.Addr {
|
||||
return zero
|
||||
}
|
||||
}
|
||||
|
||||
// connectorApps returns the conn25 app names that nv serves as a connector.
|
||||
func connectorApps(nv tailcfg.NodeView) ([]string, error) {
|
||||
return tailcfg.UnmarshalNodeCapViewJSON[string](nv.CapMap(), nodecap.Conn25Connector)
|
||||
}
|
||||
@@ -82,6 +82,23 @@ func (rp Report) RoutablePrefixes() RoutablePrefixes {
|
||||
return out
|
||||
}
|
||||
|
||||
// RoutableApps returns a map of conn25 apps associated with each app’s
|
||||
// connectors that were reachable by the current host,
|
||||
// at the time the report was finished.
|
||||
// Each slice of connectors are ordered by their node ID.
|
||||
func (rp Report) RoutableApps() RoutableApps {
|
||||
var out map[string][]Node
|
||||
for _, n := range rp.Reachable {
|
||||
for _, app := range n.Apps {
|
||||
mak.Set(&out, app, append(out[app], n))
|
||||
}
|
||||
}
|
||||
for app := range out {
|
||||
slices.SortFunc(out[app], Node.Compare)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Node represents a node in the reachability report.
|
||||
type Node struct {
|
||||
ID tailcfg.NodeID `json:"id"`
|
||||
@@ -97,6 +114,9 @@ type Node struct {
|
||||
|
||||
// Routes are the subnets that the node will route.
|
||||
Routes []netip.Prefix `json:"routes"`
|
||||
|
||||
// Apps are the conn25 apps that the node is a connector for.
|
||||
Apps []string `json:"apps,omitempty"`
|
||||
}
|
||||
|
||||
// Compare returns an integer comparing two nodes, ordered by their node ID.
|
||||
@@ -164,3 +184,20 @@ func (rt RoutablePrefixes) Sorted() iter.Seq2[netip.Prefix, []Node] {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RoutableApps is a map of conn25 connectors,
|
||||
// keyed by the app for which they are a connector.
|
||||
type RoutableApps map[string][]Node
|
||||
|
||||
// Sorted returns an iterator over the map of connectors,
|
||||
// ordered by the app name.
|
||||
func (ra RoutableApps) Sorted() iter.Seq2[string, []Node] {
|
||||
return func(yield func(string, []Node) bool) {
|
||||
apps := slices.Sorted(maps.Keys(ra))
|
||||
for _, app := range apps {
|
||||
if !yield(app, ra[app]) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"maps"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"tailscale.com/net/routecheck"
|
||||
"tailscale.com/net/tsaddr"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tailcfg/nodecap"
|
||||
"tailscale.com/types/netmap"
|
||||
"tailscale.com/util/mak"
|
||||
"tailscale.com/util/set"
|
||||
@@ -460,6 +462,84 @@ func TestRoutersByPrefix(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestProbeAllHARouters(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
self := makeNode(99, withName("self"))
|
||||
peers := []tailcfg.NodeView{
|
||||
// A plain peer that routes nothing: not probed.
|
||||
makeNode(1, withName("peer1")),
|
||||
// An HA pair sharing a prefix: both probed.
|
||||
makeNode(21, withName("subnet21"), withRoutes(netip.MustParsePrefix("192.168.1.0/24"))),
|
||||
// Node 22 is both an HA router and a conn25 connector for
|
||||
// "example", so it appears in the probe set twice and must be
|
||||
// deduplicated.
|
||||
makeNode(22, withName("subnet22"),
|
||||
withRoutes(netip.MustParsePrefix("192.168.1.0/24")),
|
||||
withConn25Connector("example")),
|
||||
// A lone subnet router: the only choice for its prefix, not probed.
|
||||
makeNode(31, withName("subnet31"), withRoutes(netip.MustParsePrefix("192.168.3.0/24"))),
|
||||
// Another connector for "example": probed, since "example" served by more
|
||||
// than one connector (22, 41, 61).
|
||||
makeNode(41, withName("connector41"), withConn25Connector("example")),
|
||||
// The only connector for "solo": not probed, since it is the only choice for its app.
|
||||
makeNode(51, withName("connector51"), withConn25Connector("solo")),
|
||||
// A connector for a shared app ("example") and a solo app ("lonely"): probed via the shared app.
|
||||
makeNode(61, withName("connector61"), withConn25Connector("example", "lonely")),
|
||||
}
|
||||
b := newStubBackend(self, peers)
|
||||
t.Cleanup(func() { b.Close() })
|
||||
c, err := routecheck.NewClient(t.Context(), t.Logf, b, b, b)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
got, err := c.ProbeAllHARouters(t.Context(), 0, routecheck.DefaultTimeout)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
wantReachable := []tailcfg.NodeID{21, 22, 41, 61}
|
||||
gotReachable := slices.Collect(maps.Keys(got.Reachable))
|
||||
slices.Sort(gotReachable)
|
||||
if !slices.Equal(gotReachable, wantReachable) {
|
||||
t.Errorf("reachable: got %v, want %v", gotReachable, wantReachable)
|
||||
}
|
||||
|
||||
// The report indexes reachable connectors by the app they serve.
|
||||
wantApps := map[string][]tailcfg.NodeID{
|
||||
"example": {22, 41, 61},
|
||||
"lonely": {61},
|
||||
}
|
||||
gotApps := map[string][]tailcfg.NodeID{}
|
||||
for app, nodes := range got.RoutableApps() {
|
||||
for _, n := range nodes {
|
||||
gotApps[app] = append(gotApps[app], n.ID)
|
||||
}
|
||||
slices.Sort(gotApps[app])
|
||||
}
|
||||
if diff := gcmp.Diff(wantApps, gotApps); diff != "" {
|
||||
t.Errorf("routable apps -want +got:\n%s", diff)
|
||||
}
|
||||
|
||||
// Every probed node should be pinged once, confirming that the
|
||||
// dual-role node (22) isn't double-probed, that lone routers and lone
|
||||
// connectors (1, 31, 51) aren't probed, and that a connector serving
|
||||
// both a shared and a solo app (61) is probed.
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
wantPinged := map[netip.Addr]int{
|
||||
netip.MustParseAddr("192.168.0.21"): 1,
|
||||
netip.MustParseAddr("192.168.0.22"): 1,
|
||||
netip.MustParseAddr("192.168.0.41"): 1,
|
||||
netip.MustParseAddr("192.168.0.61"): 1,
|
||||
}
|
||||
if diff := gcmp.Diff(wantPinged, b.pinged,
|
||||
gcmpopts.EquateComparable(netip.Addr{})); diff != "" {
|
||||
t.Errorf("pinged addresses -want +got:\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type nodeOptFunc func(*tailcfg.Node)
|
||||
|
||||
func makeNode(id tailcfg.NodeID, opts ...nodeOptFunc) tailcfg.NodeView {
|
||||
@@ -499,6 +579,19 @@ func withRoutes(routes ...netip.Prefix) nodeOptFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func withConn25Connector(apps ...string) nodeOptFunc {
|
||||
return func(n *tailcfg.Node) {
|
||||
vals := make([]tailcfg.RawMessage, 0, len(apps))
|
||||
for _, app := range apps {
|
||||
vals = append(vals, tailcfg.RawMessage(fmt.Sprintf("%q", app)))
|
||||
}
|
||||
if n.CapMap == nil {
|
||||
n.CapMap = tailcfg.NodeCapMap{}
|
||||
}
|
||||
n.CapMap[nodecap.Conn25Connector] = vals
|
||||
}
|
||||
}
|
||||
|
||||
var _ routecheck.NodeBackender = &stubBackend{}
|
||||
var _ routecheck.NodeBackend = &stubBackend{}
|
||||
var _ routecheck.NetMapper = &stubBackend{}
|
||||
@@ -513,6 +606,9 @@ type stubBackend struct {
|
||||
cancel context.CancelFunc
|
||||
|
||||
donef atomic.Pointer[func()]
|
||||
|
||||
mu sync.Mutex
|
||||
pinged map[netip.Addr]int
|
||||
}
|
||||
|
||||
type backendOptFunc func(*stubBackend)
|
||||
@@ -574,6 +670,10 @@ func (nb *stubBackend) Peers() []tailcfg.NodeView {
|
||||
}
|
||||
|
||||
func (b *stubBackend) Ping(ip netip.Addr, pingType tailcfg.PingType, size int, cb func(*ipnstate.PingResult)) {
|
||||
b.mu.Lock()
|
||||
mak.Set(&b.pinged, ip, b.pinged[ip]+1)
|
||||
b.mu.Unlock()
|
||||
|
||||
// Does the IP address match one of the peers’ addresses?
|
||||
for _, n := range b.peers {
|
||||
for _, a := range n.Addresses().All() {
|
||||
|
||||
@@ -634,6 +634,16 @@ func (n *Node) IsRouter() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsConn25Connector reports whether n is a conn25 connector.
|
||||
func (n *Node) IsConn25Connector() bool {
|
||||
return n.HasCap(nodecap.Conn25Connector)
|
||||
}
|
||||
|
||||
// IsConn25Connector reports whether v is a conn25 connector.
|
||||
func (v NodeView) IsConn25Connector() bool {
|
||||
return v.ж.IsConn25Connector()
|
||||
}
|
||||
|
||||
// IsTagged reports whether the node has any tags.
|
||||
func (n *Node) IsTagged() bool {
|
||||
return len(n.Tags) > 0
|
||||
|
||||
Reference in new issue
Block a user