prober: support filtering regions by region ID in addition to code

Updates tailscale/corp#25758

Signed-off-by: Percy Wegmann <percy@tailscale.com>
This commit is contained in:
Percy Wegmann 2025-01-10 12:23:51 -06:00 committed by Percy Wegmann
parent a841f9d87b
commit cd795d8a7f
3 changed files with 22 additions and 22 deletions

View File

@ -32,7 +32,7 @@ var (
bwTUNIPv4Address = flag.String("bw-tun-ipv4-addr", "", "if specified, bandwidth probes will be performed over a TUN device at this address in order to exercise TCP-in-TCP in similar fashion to TCP over Tailscale via DERP; we will use a /30 subnet including this IP address") bwTUNIPv4Address = flag.String("bw-tun-ipv4-addr", "", "if specified, bandwidth probes will be performed over a TUN device at this address in order to exercise TCP-in-TCP in similar fashion to TCP over Tailscale via DERP; we will use a /30 subnet including this IP address")
qdPacketsPerSecond = flag.Int("qd-packets-per-second", 0, "if greater than 0, queuing delay will be measured continuously using 260 byte packets (approximate size of a CallMeMaybe packet) sent at this rate per second") qdPacketsPerSecond = flag.Int("qd-packets-per-second", 0, "if greater than 0, queuing delay will be measured continuously using 260 byte packets (approximate size of a CallMeMaybe packet) sent at this rate per second")
qdPacketTimeout = flag.Duration("qd-packet-timeout", 5*time.Second, "queuing delay packets arriving after this period of time from being sent are treated like dropped packets and don't count toward queuing delay timings") qdPacketTimeout = flag.Duration("qd-packet-timeout", 5*time.Second, "queuing delay packets arriving after this period of time from being sent are treated like dropped packets and don't count toward queuing delay timings")
regionCode = flag.String("region-code", "", "probe only this region (e.g. 'lax'); if left blank, all regions will be probed") regionCodeOrID = flag.String("region-code", "", "probe only this region (e.g. 'lax' or '17'); if left blank, all regions will be probed")
) )
func main() { func main() {
@ -52,8 +52,8 @@ func main() {
if *bwInterval > 0 { if *bwInterval > 0 {
opts = append(opts, prober.WithBandwidthProbing(*bwInterval, *bwSize, *bwTUNIPv4Address)) opts = append(opts, prober.WithBandwidthProbing(*bwInterval, *bwSize, *bwTUNIPv4Address))
} }
if *regionCode != "" { if *regionCodeOrID != "" {
opts = append(opts, prober.WithRegion(*regionCode)) opts = append(opts, prober.WithRegionCodeOrID(*regionCodeOrID))
} }
dp, err := prober.DERP(p, *derpMapURL, opts...) dp, err := prober.DERP(p, *derpMapURL, opts...)
if err != nil { if err != nil {

View File

@ -60,8 +60,8 @@ type derpProber struct {
qdPacketsPerSecond int // in packets per second qdPacketsPerSecond int // in packets per second
qdPacketTimeout time.Duration qdPacketTimeout time.Duration
// Optionally restrict probes to a single regionCode. // Optionally restrict probes to a single regionCodeOrID.
regionCode string regionCodeOrID string
// Probe class for fetching & updating the DERP map. // Probe class for fetching & updating the DERP map.
ProbeMap ProbeClass ProbeMap ProbeClass
@ -135,11 +135,11 @@ func WithTLSProbing(interval time.Duration) DERPOpt {
} }
} }
// WithRegion restricts probing to the specified region identified by its code // WithRegionCodeOrID restricts probing to the specified region identified by its code
// (e.g. "lax"). This is case sensitive. // (e.g. "lax") or its id (e.g. "17"). This is case sensitive.
func WithRegion(regionCode string) DERPOpt { func WithRegionCodeOrID(regionCode string) DERPOpt {
return func(d *derpProber) { return func(d *derpProber) {
d.regionCode = regionCode d.regionCodeOrID = regionCode
} }
} }
@ -598,7 +598,7 @@ func (d *derpProber) ProbeUDP(ipaddr string, port int) ProbeClass {
} }
func (d *derpProber) skipRegion(region *tailcfg.DERPRegion) bool { func (d *derpProber) skipRegion(region *tailcfg.DERPRegion) bool {
return d.regionCode != "" && region.RegionCode != d.regionCode return d.regionCodeOrID != "" && region.RegionCode != d.regionCodeOrID && strconv.Itoa(region.RegionID) != d.regionCodeOrID
} }
func derpProbeUDP(ctx context.Context, ipStr string, port int) error { func derpProbeUDP(ctx context.Context, ipStr string, port int) error {

View File

@ -81,7 +81,7 @@ func TestDerpProber(t *testing.T) {
meshProbeFn: func(_, _ string) ProbeClass { return FuncProbe(func(context.Context) error { return nil }) }, meshProbeFn: func(_, _ string) ProbeClass { return FuncProbe(func(context.Context) error { return nil }) },
nodes: make(map[string]*tailcfg.DERPNode), nodes: make(map[string]*tailcfg.DERPNode),
probes: make(map[string]*Probe), probes: make(map[string]*Probe),
regionCode: "zero", regionCodeOrID: "zero",
} }
if err := dp.probeMapFn(context.Background()); err != nil { if err := dp.probeMapFn(context.Background()); err != nil {
t.Errorf("unexpected probeMapFn() error: %s", err) t.Errorf("unexpected probeMapFn() error: %s", err)
@ -129,7 +129,7 @@ func TestDerpProber(t *testing.T) {
} }
// Stop filtering regions. // Stop filtering regions.
dp.regionCode = "" dp.regionCodeOrID = ""
if err := dp.probeMapFn(context.Background()); err != nil { if err := dp.probeMapFn(context.Background()); err != nil {
t.Errorf("unexpected probeMapFn() error: %s", err) t.Errorf("unexpected probeMapFn() error: %s", err)
} }