diff --git a/cmd/tailscale/cli/routecheck.go b/cmd/tailscale/cli/routecheck.go index c5162f98f..fdc632a54 100644 --- a/cmd/tailscale/cli/routecheck.go +++ b/cmd/tailscale/cli/routecheck.go @@ -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 } diff --git a/cmd/tailscale/tsroutecheckjsonv0/tsroutecheck.go b/cmd/tailscale/tsroutecheckjsonv0/tsroutecheck.go index f9dd89348..24847ea8b 100644 --- a/cmd/tailscale/tsroutecheckjsonv0/tsroutecheck.go +++ b/cmd/tailscale/tsroutecheckjsonv0/tsroutecheck.go @@ -25,4 +25,5 @@ import ( type ReportResponse struct { Done time.Time `json:"done"` Routes routecheck.RoutablePrefixes `json:"routes"` + Apps routecheck.RoutableApps `json:"apps,omitempty"` } diff --git a/feature/routecheck/routertracker.go b/feature/routecheck/routertracker.go index 4f180f1f3..54f23a29d 100644 --- a/feature/routecheck/routertracker.go +++ b/feature/routecheck/routertracker.go @@ -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) } diff --git a/feature/routecheck/routertracker_test.go b/feature/routecheck/routertracker_test.go index 4a1e7dfc1..999190f5e 100644 --- a/feature/routecheck/routertracker_test.go +++ b/feature/routecheck/routertracker_test.go @@ -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 + } +} diff --git a/ipn/ipnstate/ipnstate.go b/ipn/ipnstate/ipnstate.go index ba0447fdd..ab007bd5a 100644 --- a/ipn/ipnstate/ipnstate.go +++ b/ipn/ipnstate/ipnstate.go @@ -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. diff --git a/ipn/ipnstate/ipnstate_test.go b/ipn/ipnstate/ipnstate_test.go index a6ea28b8d..28dd433dd 100644 --- a/ipn/ipnstate/ipnstate_test.go +++ b/ipn/ipnstate/ipnstate_test.go @@ -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) + } + }) + } +} diff --git a/net/routecheck/probe.go b/net/routecheck/probe.go index 16e425848..6f71b23f3 100644 --- a/net/routecheck/probe.go +++ b/net/routecheck/probe.go @@ -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) +} diff --git a/net/routecheck/report.go b/net/routecheck/report.go index afbe126c3..0073e230f 100644 --- a/net/routecheck/report.go +++ b/net/routecheck/report.go @@ -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 + } + } + } +} diff --git a/net/routecheck/routecheck_test.go b/net/routecheck/routecheck_test.go index 72aa69331..645bc4e46 100644 --- a/net/routecheck/routecheck_test.go +++ b/net/routecheck/routecheck_test.go @@ -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() { diff --git a/tailcfg/tailcfg.go b/tailcfg/tailcfg.go index 0cdd9850f..d3559849a 100644 --- a/tailcfg/tailcfg.go +++ b/tailcfg/tailcfg.go @@ -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