@ -10,9 +10,9 @@ import (
crand "crypto/rand"
crand "crypto/rand"
"encoding/json"
"encoding/json"
"errors"
"errors"
"expvar"
"fmt"
"fmt"
"log"
"log"
"maps"
"net"
"net"
"net/http"
"net/http"
"strconv"
"strconv"
@ -20,6 +20,7 @@ import (
"sync"
"sync"
"time"
"time"
"github.com/prometheus/client_golang/prometheus"
"tailscale.com/derp"
"tailscale.com/derp"
"tailscale.com/derp/derphttp"
"tailscale.com/derp/derphttp"
"tailscale.com/net/stun"
"tailscale.com/net/stun"
@ -42,11 +43,14 @@ type derpProber struct {
bwInterval time . Duration
bwInterval time . Duration
bwProbeSize int64
bwProbeSize int64
// Probe functions that can be overridden for testing.
// Probe class for fetching & updating the DERP map.
tlsProbeFn func ( string ) ProbeFunc
ProbeMap ProbeClass
udpProbeFn func ( string , int ) ProbeFunc
meshProbeFn func ( string , string ) ProbeFunc
// Probe classes for probing individual derpers.
bwProbeFn func ( string , string , int64 ) ProbeFunc
tlsProbeFn func ( string ) ProbeClass
udpProbeFn func ( string , int ) ProbeClass
meshProbeFn func ( string , string ) ProbeClass
bwProbeFn func ( string , string , int64 ) ProbeClass
sync . Mutex
sync . Mutex
lastDERPMap * tailcfg . DERPMap
lastDERPMap * tailcfg . DERPMap
@ -100,6 +104,10 @@ func DERP(p *Prober, derpMapURL string, opts ...DERPOpt) (*derpProber, error) {
nodes : make ( map [ string ] * tailcfg . DERPNode ) ,
nodes : make ( map [ string ] * tailcfg . DERPNode ) ,
probes : make ( map [ string ] * Probe ) ,
probes : make ( map [ string ] * Probe ) ,
}
}
d . ProbeMap = ProbeClass {
Probe : d . probeMapFn ,
Class : "derp_map" ,
}
for _ , o := range opts {
for _ , o := range opts {
o ( d )
o ( d )
}
}
@ -109,10 +117,10 @@ func DERP(p *Prober, derpMapURL string, opts ...DERPOpt) (*derpProber, error) {
return d , nil
return d , nil
}
}
// ProbeMap fetches the DERPMap and creates/destroys probes for each
// probeMapFn fetches the DERPMap and creates/destroys probes for each
// DERP server as necessary. It should get regularly executed as a
// DERP server as necessary. It should get regularly executed as a
// probe function itself.
// probe function itself.
func ( d * derpProber ) ProbeMap ( ctx context . Context ) error {
func ( d * derpProber ) probeMapFn ( ctx context . Context ) error {
if err := d . updateMap ( ctx ) ; err != nil {
if err := d . updateMap ( ctx ) ; err != nil {
return err
return err
}
}
@ -123,7 +131,7 @@ func (d *derpProber) ProbeMap(ctx context.Context) error {
for _ , region := range d . lastDERPMap . Regions {
for _ , region := range d . lastDERPMap . Regions {
for _ , server := range region . Nodes {
for _ , server := range region . Nodes {
labels := map [ string ] string {
labels := Labels {
"region" : region . RegionCode ,
"region" : region . RegionCode ,
"region_id" : strconv . Itoa ( region . RegionID ) ,
"region_id" : strconv . Itoa ( region . RegionID ) ,
"hostname" : server . HostName ,
"hostname" : server . HostName ,
@ -169,18 +177,11 @@ func (d *derpProber) ProbeMap(ctx context.Context) error {
}
}
if d . bwInterval > 0 && d . bwProbeSize > 0 {
if d . bwInterval > 0 && d . bwProbeSize > 0 {
bwLabels := maps . Clone ( labels )
bwLabels [ "probe_size_bytes" ] = fmt . Sprintf ( "%d" , d . bwProbeSize )
if server . Name == to . Name {
bwLabels [ "derp_path" ] = "single"
} else {
bwLabels [ "derp_path" ] = "mesh"
}
n := fmt . Sprintf ( "derp/%s/%s/%s/bw" , region . RegionCode , server . Name , to . Name )
n := fmt . Sprintf ( "derp/%s/%s/%s/bw" , region . RegionCode , server . Name , to . Name )
wantProbes [ n ] = true
wantProbes [ n ] = true
if d . probes [ n ] == nil {
if d . probes [ n ] == nil {
log . Printf ( "adding DERP bandwidth probe for %s->%s (%s) %v bytes every %v" , server . Name , to . Name , region . RegionName , d . bwProbeSize , d . bwInterval )
log . Printf ( "adding DERP bandwidth probe for %s->%s (%s) %v bytes every %v" , server . Name , to . Name , region . RegionName , d . bwProbeSize , d . bwInterval )
d . probes [ n ] = d . p . Run ( n , d . bwInterval , bwL abels, d . bwProbeFn ( server . Name , to . Name , d . bwProbeSize ) )
d . probes [ n ] = d . p . Run ( n , d . bwInterval , labels , d . bwProbeFn ( server . Name , to . Name , d . bwProbeSize ) )
}
}
}
}
}
}
@ -198,32 +199,55 @@ func (d *derpProber) ProbeMap(ctx context.Context) error {
return nil
return nil
}
}
// probeMesh returs a probe fun c that sends a test packet through a pair of DERP
// probeMesh returs a probe class that sends a test packet through a pair of DERP
// servers (or just one server, if 'from' and 'to' are the same). 'from' and 'to'
// servers (or just one server, if 'from' and 'to' are the same). 'from' and 'to'
// are expected to be names (DERPNode.Name) of two DERP servers in the same region.
// are expected to be names (DERPNode.Name) of two DERP servers in the same region.
func ( d * derpProber ) probeMesh ( from , to string ) ProbeFunc {
func ( d * derpProber ) probeMesh ( from , to string ) ProbeClass {
return func ( ctx context . Context ) error {
derpPath := "mesh"
fromN , toN , err := d . getNodePair ( from , to )
if from == to {
if err != nil {
derpPath = "single"
return err
}
}
return ProbeClass {
Probe : func ( ctx context . Context ) error {
fromN , toN , err := d . getNodePair ( from , to )
if err != nil {
return err
}
dm := d . lastDERPMap
dm := d . lastDERPMap
return derpProbeNodePair ( ctx , dm , fromN , toN )
return derpProbeNodePair ( ctx , dm , fromN , toN )
} ,
Class : "derp_mesh" ,
Labels : Labels { "derp_path" : derpPath } ,
}
}
}
}
// probeBandwidth returs a probe fun c that sends a payload of a given size
// probeBandwidth returs a probe class that sends a payload of a given size
// through a pair of DERP servers (or just one server, if 'from' and 'to' are
// through a pair of DERP servers (or just one server, if 'from' and 'to' are
// the same). 'from' and 'to' are expected to be names (DERPNode.Name) of two
// the same). 'from' and 'to' are expected to be names (DERPNode.Name) of two
// DERP servers in the same region.
// DERP servers in the same region.
func ( d * derpProber ) probeBandwidth ( from , to string , size int64 ) ProbeFunc {
func ( d * derpProber ) probeBandwidth ( from , to string , size int64 ) ProbeClass {
return func ( ctx context . Context ) error {
derpPath := "mesh"
fromN , toN , err := d . getNodePair ( from , to )
if from == to {
if err != nil {
derpPath = "single"
return err
}
}
var transferTime expvar . Float
return derpProbeBandwidth ( ctx , d . lastDERPMap , fromN , toN , size )
return ProbeClass {
Probe : func ( ctx context . Context ) error {
fromN , toN , err := d . getNodePair ( from , to )
if err != nil {
return err
}
return derpProbeBandwidth ( ctx , d . lastDERPMap , fromN , toN , size , & transferTime )
} ,
Class : "derp_bw" ,
Labels : Labels { "derp_path" : derpPath } ,
Metrics : func ( l prometheus . Labels ) [ ] prometheus . Metric {
return [ ] prometheus . Metric {
prometheus . MustNewConstMetric ( prometheus . NewDesc ( "derp_bw_probe_size_bytes" , "Payload size of the bandwidth prober" , nil , l ) , prometheus . GaugeValue , float64 ( size ) ) ,
prometheus . MustNewConstMetric ( prometheus . NewDesc ( "derp_bw_transfer_time_seconds_total" , "Time it took to transfer data" , nil , l ) , prometheus . CounterValue , transferTime . Value ( ) ) ,
}
} ,
}
}
}
}
@ -289,9 +313,12 @@ func (d *derpProber) updateMap(ctx context.Context) error {
return nil
return nil
}
}
func ( d * derpProber ) ProbeUDP ( ipaddr string , port int ) ProbeFunc {
func ( d * derpProber ) ProbeUDP ( ipaddr string , port int ) ProbeClass {
return func ( ctx context . Context ) error {
return ProbeClass {
return derpProbeUDP ( ctx , ipaddr , port )
Probe : func ( ctx context . Context ) error {
return derpProbeUDP ( ctx , ipaddr , port )
} ,
Class : "derp_udp" ,
}
}
}
}
@ -347,7 +374,7 @@ func derpProbeUDP(ctx context.Context, ipStr string, port int) error {
// derpProbeBandwidth sends a payload of a given size between two local
// derpProbeBandwidth sends a payload of a given size between two local
// DERP clients connected to two DERP servers.
// DERP clients connected to two DERP servers.
func derpProbeBandwidth ( ctx context . Context , dm * tailcfg . DERPMap , from , to * tailcfg . DERPNode , size int64 ) ( err error ) {
func derpProbeBandwidth ( ctx context . Context , dm * tailcfg . DERPMap , from , to * tailcfg . DERPNode , size int64 , transferTime * expvar . Float ) ( err error ) {
// This probe uses clients with isProber=false to avoid spamming the derper logs with every packet
// This probe uses clients with isProber=false to avoid spamming the derper logs with every packet
// sent by the bandwidth probe.
// sent by the bandwidth probe.
fromc , err := newConn ( ctx , dm , from , false )
fromc , err := newConn ( ctx , dm , from , false )
@ -368,6 +395,9 @@ func derpProbeBandwidth(ctx context.Context, dm *tailcfg.DERPMap, from, to *tail
time . Sleep ( 100 * time . Millisecond ) // pretty arbitrary
time . Sleep ( 100 * time . Millisecond ) // pretty arbitrary
}
}
start := time . Now ( )
defer func ( ) { transferTime . Add ( time . Since ( start ) . Seconds ( ) ) } ( )
if err := runDerpProbeNodePair ( ctx , from , to , fromc , toc , size ) ; err != nil {
if err := runDerpProbeNodePair ( ctx , from , to , fromc , toc , size ) ; err != nil {
// Record pubkeys on failed probes to aid investigation.
// Record pubkeys on failed probes to aid investigation.
return fmt . Errorf ( "%s -> %s: %w" ,
return fmt . Errorf ( "%s -> %s: %w" ,