From 4fd07b5f986f0597876436a2cc133c37dea52166 Mon Sep 17 00:00:00 2001 From: Francois Marier Date: Thu, 3 Dec 2026 11:42:45 -0700 Subject: [PATCH] net/portmapper: add metrics for createOrGetMapping Probe counts the replies it gets from PCP, NAT-PMP and UPnP servers. When createOrGetMapping asks for a mapping, only the UPnP path has metrics. We can't tell which protocol created a mapping, how often new mappings and renewals fail, or which errors PCP and NAT-PMP servers return. This adds portmap_create_* metrics for new and renewed mappings by protocol, the UPnP fallback, every PCP and NAT-PMP result code, and the replies we drop (unparseable, PCP nonce mismatch, NAT-PMP reply for another internal port, unknown version). A request is a renewal if the mapping it replaces used the same protocol. We send NAT-PMP requests without knowing whether the gateway supports NAT-PMP, so most timeouts come from gateways that don't. The success rate for new NAT-PMP mappings on gateways that support it is therefore portmap_create_new_pmp_ok / (portmap_create_new_pmp_sent - portmap_create_new_pmp_timeout). portmap_create_no_mapping_v6 stays at 0 for now, since the built-in gateway lookups only return IPv4 gateways. The existing portmap_upnp_select_* metrics move from the UPnP Probe block to the new createOrGetMapping block, since only createOrGetMapping increments them. Their names don't change. Updates tailscale/corp#51202 Change-Id: Iec2051d803f6714aed1b37d63426c1d327e53ff3 Signed-off-by: Francois Marier --- cmd/tailscale/depaware.txt | 2 +- net/portmapper/pcp.go | 31 ++++ net/portmapper/portmapper.go | 309 +++++++++++++++++++++++++++++++---- net/portmapper/upnp.go | 6 + 4 files changed, 317 insertions(+), 31 deletions(-) diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 8b22467e8..4258173fc 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -281,7 +281,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/tsweb/varz from tailscale.com/util/usermetric+ tailscale.com/tsweb/vcstime from tailscale.com/client/web tailscale.com/types/appctype from tailscale.com/client/local+ - tailscale.com/types/bools from tailscale.com/net/netcheck + tailscale.com/types/bools from tailscale.com/net/netcheck+ tailscale.com/types/dnstype from tailscale.com/tailcfg+ tailscale.com/types/empty from tailscale.com/ipn tailscale.com/types/ipproto from tailscale.com/ipn+ diff --git a/net/portmapper/pcp.go b/net/portmapper/pcp.go index 53fb56155..6f7d613d9 100644 --- a/net/portmapper/pcp.go +++ b/net/portmapper/pcp.go @@ -138,16 +138,47 @@ func buildPCPRequestMappingPacket( // In particular, its Client is not populated. func parsePCPMapResponse(resp []byte) (*pcpMapping, error) { if len(resp) < 60 { + metricCreatePCPParseErr.Add(1) return nil, fmt.Errorf("Does not appear to be PCP MAP response") } res, ok := parsePCPResponse(resp[:24]) if !ok { + metricCreatePCPParseErr.Add(1) return nil, fmt.Errorf("Invalid PCP common header") } if res.ResultCode == pcpCodeNotAuthorized { + metricCreatePCPNotAuthorized.Add(1) return nil, fmt.Errorf("PCP is implemented but not enabled in the router") } if res.ResultCode != pcpCodeOK { + switch res.ResultCode { + case pcpCodeUnsupportedVersion: + metricCreatePCPUnsupportedVersion.Add(1) + case pcpCodeMalformedRequest: + metricCreatePCPMalformedRequest.Add(1) + case pcpCodeUnsupportedOpcode: + metricCreatePCPUnsupportedOpcode.Add(1) + case pcpCodeUnsupportedOption: + metricCreatePCPUnsupportedOption.Add(1) + case pcpCodeMalformedOption: + metricCreatePCPMalformedOption.Add(1) + case pcpCodeNetworkFailure: + metricCreatePCPNetworkFailure.Add(1) + case pcpCodeNoResources: + metricCreatePCPNoResources.Add(1) + case pcpCodeUnsupportedProtocol: + metricCreatePCPUnsupportedProtocol.Add(1) + case pcpCodeUserExceededQuota: + metricCreatePCPUserExceededQuota.Add(1) + case pcpCodeCannotProvideExternal: + metricCreatePCPCannotProvideExternal.Add(1) + case pcpCodeAddressMismatch: + metricCreatePCPAddressMismatch.Add(1) + case pcpCodeExcessiveRemotePeers: + metricCreatePCPExcessiveRemotePeers.Add(1) + default: + metricCreatePCPUnhandledResponseCode.Add(1) + } return nil, fmt.Errorf("PCP response not ok, code %d", res.ResultCode) } externalPort := binary.BigEndian.Uint16(resp[42:44]) diff --git a/net/portmapper/portmapper.go b/net/portmapper/portmapper.go index 3c6aebd2c..4ddff2153 100644 --- a/net/portmapper/portmapper.go +++ b/net/portmapper/portmapper.go @@ -29,6 +29,7 @@ import ( "tailscale.com/net/portmapper/portmappertype" "tailscale.com/net/sockstats" "tailscale.com/syncs" + "tailscale.com/types/bools" "tailscale.com/types/logger" "tailscale.com/types/nettype" "tailscale.com/util/clientmetric" @@ -562,6 +563,9 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter return nil, netip.AddrPort{}, NoMappingError{ErrGatewayRange} } if gw.Is6() { + // TODO(fmarier): the built-in gateway lookups only return IPv4 + // gateways, so this can't be reached yet (tailscale/corp#51127). + metricCreateNoMappingV6.Add(1) return nil, netip.AddrPort{}, NoMappingError{ErrGatewayIPv6} } @@ -569,8 +573,10 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter // Log what kind of portmap we obtained reusedExisting := false + prevType := "" // type of the mapping we're replacing, if one exists and hasn't expired defer func() { if err != nil { + metricCreateNoMappingErr.Add(1) return } @@ -586,10 +592,20 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter } if c.mapping == nil { + metricCreateNoMappingUnexpected.Add(1) c.logf("[unexpected] no error but no stored mapping: now=%d external=%v type=%s", now.Unix(), external, portmapType) return } + renew := portmapType == prevType + switch portmapType { // reused mappings have an "existing-" prefix and are not counted + case "pmp": + bools.IfElse(renew, metricCreateRenewPMPOK, metricCreateNewPMPOK).Add(1) + case "pcp": + bools.IfElse(renew, metricCreateRenewPCPOK, metricCreateNewPCPOK).Add(1) + case "upnp": + bools.IfElse(renew, metricCreateRenewUPnPOK, metricCreateNewUPnPOK).Add(1) + } // TODO(creachadair): This is more subtle than it should be. Ideally we // would just return the mapping directly, but there are many different @@ -631,6 +647,10 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter } // The mapping might still be valid, so just try to renew it. prevPort = m.External().Port() + if now.Before(m.GoodUntil()) { + // Only unexpired mappings can be renewed. Expired ones lead to brand-new mappings. + prevType = m.MappingType() + } } if c.debug.DisablePCP() && c.debug.DisablePMP() { @@ -665,7 +685,8 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter if c.lastProbe.After(now.Add(-5*time.Second)) && !haveRecentPMP && !haveRecentPCP { c.mu.Unlock() // fallback to UPnP portmapping - c.vlogf("attempting fallback to UPnP due to no PCP and PMP failed") + metricCreateUPnPFallbackNoPxP.Add(1) + c.vlogf("attempting fallback to UPnP due to no PCP or PMP") if external, ok := c.getUPnPPortMapping(ctx, gw, internalAddr, prevPort); ok { return nil, external, nil } @@ -696,6 +717,16 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter preferPCP := !c.debug.DisablePCP() && (c.debug.DisablePMP() || (!haveRecentPMP && haveRecentPCP)) + sentMetric, timeoutMetric := metricCreateNewPMPSent, metricCreateNewPMPTimeout + switch { + case preferPCP && prevType == "pcp": + sentMetric, timeoutMetric = metricCreateRenewPCPSent, metricCreateRenewPCPTimeout + case preferPCP: + sentMetric, timeoutMetric = metricCreateNewPCPSent, metricCreateNewPCPTimeout + case prevType == "pmp": + sentMetric, timeoutMetric = metricCreateRenewPMPSent, metricCreateRenewPMPTimeout + } + // Create a mapping, defaulting to PMP unless only PCP was seen recently. if preferPCP { // TODO replace wildcardIP here with previous external if known. @@ -707,6 +738,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter } return nil, netip.AddrPort{}, err } + sentMetric.Add(1) } else { // Ask for our external address if needed. if !m.external.Addr().IsValid() { @@ -725,6 +757,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter } return nil, netip.AddrPort{}, err } + sentMetric.Add(1) } res := make([]byte, 1500) @@ -735,6 +768,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter return nil, netip.AddrPort{}, err } // fallback to UPnP portmapping + timeoutMetric.Add(1) c.vlogf("attempting fallback to UPnP due to timeout") if mapping, ok := c.getUPnPPortMapping(ctx, gw, internalAddr, prevPort); ok { return nil, mapping, nil @@ -751,10 +785,32 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter case pmpVersion: pres, ok := parsePMPResponse(res[:n]) if !ok { + // TODO(fmarier): a PMP-only server's 8-byte unsupported version + // reply to our PCP request is counted as a parse error (#21799). + metricCreatePMPParseErr.Add(1) c.logf("unexpected PMP response: % 02x", res[:n]) continue } - if pres.ResultCode != 0 { + if pres.OpCode == pmpOpReply|pmpOpMapUDP && pres.InternalPort != localPort { + // TODO(fmarier): response meant for another client. We should ignore + // these (tailscale/corp#48417). + metricCreatePMPInternalPortMismatch.Add(1) + } + if pres.ResultCode != pmpCodeOK { + switch pres.ResultCode { + case pmpCodeUnsupportedVersion: + metricCreatePMPUnsupportedVersion.Add(1) + case pmpCodeNotAuthorized: + metricCreatePMPNotAuthorized.Add(1) + case pmpCodeNetworkFailure: + metricCreatePMPNetworkFailure.Add(1) + case pmpCodeOutOfResources: + metricCreatePMPOutOfResources.Add(1) + case pmpCodeUnsupportedOpcode: + metricCreatePMPUnsupportedOpcode.Add(1) + default: + metricCreatePMPUnhandledResponseCode.Add(1) + } return nil, netip.AddrPort{}, NoMappingError{fmt.Errorf("PMP response Op=0x%x,Res=0x%x", pres.OpCode, pres.ResultCode)} } if pres.OpCode == pmpOpReply|pmpOpMapPublicAddr { @@ -774,6 +830,9 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter // specific payload. For a MAP operation, the first 12 bytes of // the payload are the client-generated nonce. if n < 36 || !bytes.Equal(res[24:36], pcpNonce[:]) { + // TODO(fmarier): a PCP-only server's unsupported version reply + // to our PMP request is counted as a nonce mismatch (#21799). + metricCreatePCPNonceMismatch.Add(1) c.logf("ignoring PCP response with missing or mismatched nonce") continue } @@ -791,6 +850,7 @@ func (c *Client) createOrGetMapping(ctx context.Context) (mapping mapping, exter c.mapping = pcpMapping return pcpMapping, pcpMapping.external, nil default: + metricCreatePxPUnknownVersion.Add(1) c.logf("unknown PMP/PCP version number: %d %v", version, res[:n]) return nil, netip.AddrPort{}, NoMappingError{ErrNoPortMappingServices} } @@ -1400,34 +1460,6 @@ var ( // received a UPnP response from a port other than the UPnP port. metricUPnPResponseAlternatePort = clientmetric.NewCounter("portmap_upnp_response_alternate_port") - // metricUPnPSelectLegacy counts the number of times that a legacy - // service was found in a UPnP response. - metricUPnPSelectLegacy = clientmetric.NewCounter("portmap_upnp_select_legacy") - - // metricUPnPSelectSingle counts the number of times that only a single - // UPnP device was available in selectBestService. - metricUPnPSelectSingle = clientmetric.NewCounter("portmap_upnp_select_single") - - // metricUPnPSelectMultiple counts the number of times that we need to - // select from among multiple UPnP devices in selectBestService. - metricUPnPSelectMultiple = clientmetric.NewCounter("portmap_upnp_select_multiple") - - // metricUPnPSelectExternalPublic counts the number of times that - // selectBestService picked a UPnP device with an external public IP. - metricUPnPSelectExternalPublic = clientmetric.NewCounter("portmap_upnp_select_external_public") - - // metricUPnPSelectExternalPrivate counts the number of times that - // selectBestService picked a UPnP device with an external private IP. - metricUPnPSelectExternalPrivate = clientmetric.NewCounter("portmap_upnp_select_external_private") - - // metricUPnPSelectUp counts the number of times that selectBestService - // picked a UPnP device that was up but with no external IP. - metricUPnPSelectUp = clientmetric.NewCounter("portmap_upnp_select_up") - - // metricUPnPSelectNone counts the number of times that selectBestService - // picked a UPnP device that is not up. - metricUPnPSelectNone = clientmetric.NewCounter("portmap_upnp_select_none") - // metricUPnPParseErr counts the number of times we failed to parse a UPnP response. metricUPnPParseErr = clientmetric.NewCounter("portmap_upnp_parse_err") @@ -1511,3 +1543,220 @@ func getUPnPErrorsMetric(code int) *clientmetric.Metric { }) return mm } + +// Port mapping create/renew metrics +var ( + // metricCreateNewPMPSent counts the number of times we sent a PMP + // request for a new mapping. + metricCreateNewPMPSent = clientmetric.NewCounter("portmap_create_new_pmp_sent") + + // metricCreateNewPMPOK counts the number of times we created a new PMP + // mapping. + metricCreateNewPMPOK = clientmetric.NewCounter("portmap_create_new_pmp_ok") + + // metricCreateNewPMPTimeout counts the number of times a PMP request for + // a new mapping timed out. + metricCreateNewPMPTimeout = clientmetric.NewCounter("portmap_create_new_pmp_timeout") + + // metricCreateRenewPMPSent counts the number of times we sent a PMP + // request to renew a mapping. + metricCreateRenewPMPSent = clientmetric.NewCounter("portmap_create_renew_pmp_sent") + + // metricCreateRenewPMPOK counts the number of times we renewed a PMP + // mapping. + metricCreateRenewPMPOK = clientmetric.NewCounter("portmap_create_renew_pmp_ok") + + // metricCreateRenewPMPTimeout counts the number of times a PMP request + // to renew a mapping timed out. + metricCreateRenewPMPTimeout = clientmetric.NewCounter("portmap_create_renew_pmp_timeout") + + // metricCreateNewPCPSent counts the number of times we sent a PCP + // request for a new mapping. + metricCreateNewPCPSent = clientmetric.NewCounter("portmap_create_new_pcp_sent") + + // metricCreateNewPCPOK counts the number of times we created a new PCP + // mapping. + metricCreateNewPCPOK = clientmetric.NewCounter("portmap_create_new_pcp_ok") + + // metricCreateNewPCPTimeout counts the number of times a PCP request for + // a new mapping timed out. + metricCreateNewPCPTimeout = clientmetric.NewCounter("portmap_create_new_pcp_timeout") + + // metricCreateRenewPCPSent counts the number of times we sent a PCP + // request to renew a mapping. + metricCreateRenewPCPSent = clientmetric.NewCounter("portmap_create_renew_pcp_sent") + + // metricCreateRenewPCPOK counts the number of times we renewed a PCP + // mapping. + metricCreateRenewPCPOK = clientmetric.NewCounter("portmap_create_renew_pcp_ok") + + // metricCreateRenewPCPTimeout counts the number of times a PCP request + // to renew a mapping timed out. + metricCreateRenewPCPTimeout = clientmetric.NewCounter("portmap_create_renew_pcp_timeout") + + // metricCreateNewUPnPSent counts the number of times we asked a UPnP + // device for a new mapping. + metricCreateNewUPnPSent = clientmetric.NewCounter("portmap_create_new_upnp_sent") + + // metricCreateNewUPnPOK counts the number of times we created a new UPnP + // mapping. + metricCreateNewUPnPOK = clientmetric.NewCounter("portmap_create_new_upnp_ok") + + // metricCreateRenewUPnPSent counts the number of times we asked a UPnP + // device to renew a mapping. + metricCreateRenewUPnPSent = clientmetric.NewCounter("portmap_create_renew_upnp_sent") + + // metricCreateRenewUPnPOK counts the number of times we renewed a UPnP + // mapping. + metricCreateRenewUPnPOK = clientmetric.NewCounter("portmap_create_renew_upnp_ok") + + // UPnP is the last protocol we try. There is no fallback timeout to track. +) + +// Port mapping createOrGetMapping error metrics +var ( + // metricCreateNoMappingV6 counts the number of times we couldn't + // create a mapping because the gateway is an IPv6 address (unsupported). + metricCreateNoMappingV6 = clientmetric.NewCounter("portmap_create_no_mapping_v6") + + // metricCreateNoMappingErr counts the number of times createOrGetMapping + // returned an error. It leaves out the calls where port mapping is + // disabled or we have no usable IPv4 gateway. + metricCreateNoMappingErr = clientmetric.NewCounter("portmap_create_no_mapping_err") + + // metricCreateNoMappingUnexpected counts the number of times + // createOrGetMapping returned no error but couldn't return a mapping. + metricCreateNoMappingUnexpected = clientmetric.NewCounter("portmap_create_no_mapping_unexpected") + + // metricCreatePxPUnknownVersion counts the number of times we + // received a PMP/PCP response with an unknown version number. + metricCreatePxPUnknownVersion = clientmetric.NewCounter("portmap_create_pxp_unknown_version") + + // metricCreatePCPNonceMismatch counts the number of times we ignored + // a PCP response with a missing or mismatched nonce. + metricCreatePCPNonceMismatch = clientmetric.NewCounter("portmap_create_pcp_nonce_mismatch") + + // metricCreatePCPParseErr counts the number of times we failed to + // parse a PCP MAP response. + metricCreatePCPParseErr = clientmetric.NewCounter("portmap_create_pcp_parse_err") + + // metricCreatePCPUnsupportedVersion counts the number of times + // we received a PCP unsupported version result code. + metricCreatePCPUnsupportedVersion = clientmetric.NewCounter("portmap_create_pcp_unsupported_version") + + // metricCreatePCPMalformedRequest counts the number of times + // we received a PCP malformed request result code. + metricCreatePCPMalformedRequest = clientmetric.NewCounter("portmap_create_pcp_malformed_request") + + // metricCreatePCPUnsupportedOpcode counts the number of times + // we received a PCP unsupported opcode result code. + metricCreatePCPUnsupportedOpcode = clientmetric.NewCounter("portmap_create_pcp_unsupported_opcode") + + // metricCreatePCPUnsupportedOption counts the number of times + // we received a PCP unsupported option result code. + metricCreatePCPUnsupportedOption = clientmetric.NewCounter("portmap_create_pcp_unsupported_option") + + // metricCreatePCPMalformedOption counts the number of times + // we received a PCP malformed option result code. + metricCreatePCPMalformedOption = clientmetric.NewCounter("portmap_create_pcp_malformed_option") + + // metricCreatePCPNetworkFailure counts the number of times + // we received a PCP network failure result code. + metricCreatePCPNetworkFailure = clientmetric.NewCounter("portmap_create_pcp_network_failure") + + // metricCreatePCPNoResources counts the number of times + // we received a PCP no resources result code. + metricCreatePCPNoResources = clientmetric.NewCounter("portmap_create_pcp_no_resources") + + // metricCreatePCPUnsupportedProtocol counts the number of times + // we received a PCP unsupported protocol result code. + metricCreatePCPUnsupportedProtocol = clientmetric.NewCounter("portmap_create_pcp_unsupported_protocol") + + // metricCreatePCPUserExceededQuota counts the number of times + // we received a PCP user exceeded quota result code. + metricCreatePCPUserExceededQuota = clientmetric.NewCounter("portmap_create_pcp_user_exceeded_quota") + + // metricCreatePCPCannotProvideExternal counts the number of times + // we received a PCP cannot provide external result code. + metricCreatePCPCannotProvideExternal = clientmetric.NewCounter("portmap_create_pcp_cannot_provide_external") + + // metricCreatePCPAddressMismatch counts the number of times + // we received a PCP address mismatch result code. + metricCreatePCPAddressMismatch = clientmetric.NewCounter("portmap_create_pcp_address_mismatch") + + // metricCreatePCPNotAuthorized counts the number of times + // we received a PCP not authorized result code. + metricCreatePCPNotAuthorized = clientmetric.NewCounter("portmap_create_pcp_not_authorized") + + // metricCreatePCPExcessiveRemotePeers counts the number of times + // we received a PCP excessive remote peers result code. + metricCreatePCPExcessiveRemotePeers = clientmetric.NewCounter("portmap_create_pcp_excessive_remote_peers") + + // metricCreatePCPUnhandledResponseCode counts the number of times + // we received an (as yet) unhandled PCP result code. + metricCreatePCPUnhandledResponseCode = clientmetric.NewCounter("portmap_create_pcp_unhandled_response_code") + + // metricCreatePMPParseErr counts the number of times we failed to + // parse a PMP response. + metricCreatePMPParseErr = clientmetric.NewCounter("portmap_create_pmp_parse_err") + + // metricCreatePMPInternalPortMismatch counts the number of times we + // received a PMP mapping response for an internal port other than ours. + metricCreatePMPInternalPortMismatch = clientmetric.NewCounter("portmap_create_pmp_internal_port_mismatch") + + // metricCreatePMPUnsupportedVersion counts the number of times + // we received a PMP unsupported version result code. + metricCreatePMPUnsupportedVersion = clientmetric.NewCounter("portmap_create_pmp_unsupported_version") + + // metricCreatePMPNotAuthorized counts the number of times + // we received a PMP not authorized result code. + metricCreatePMPNotAuthorized = clientmetric.NewCounter("portmap_create_pmp_not_authorized") + + // metricCreatePMPNetworkFailure counts the number of times + // we received a PMP network failure result code. + metricCreatePMPNetworkFailure = clientmetric.NewCounter("portmap_create_pmp_network_failure") + + // metricCreatePMPOutOfResources counts the number of times + // we received a PMP out of resources result code. + metricCreatePMPOutOfResources = clientmetric.NewCounter("portmap_create_pmp_out_of_resources") + + // metricCreatePMPUnsupportedOpcode counts the number of times + // we received a PMP unsupported opcode result code. + metricCreatePMPUnsupportedOpcode = clientmetric.NewCounter("portmap_create_pmp_unsupported_opcode") + + // metricCreatePMPUnhandledResponseCode counts the number of times + // we received an unhandled PMP result code. + metricCreatePMPUnhandledResponseCode = clientmetric.NewCounter("portmap_create_pmp_unhandled_response_code") + + // metricCreateUPnPFallbackNoPxP counts the number of times we fell + // back to UPnP because a recent probe found neither PCP nor PMP. + metricCreateUPnPFallbackNoPxP = clientmetric.NewCounter("portmap_create_upnp_fallback_no_pxp") + + // metricUPnPSelectLegacy counts the number of times that a legacy + // service was found in a UPnP response. + metricUPnPSelectLegacy = clientmetric.NewCounter("portmap_upnp_select_legacy") + + // metricUPnPSelectSingle counts the number of times that only a single + // UPnP device was available in selectBestService. + metricUPnPSelectSingle = clientmetric.NewCounter("portmap_upnp_select_single") + + // metricUPnPSelectMultiple counts the number of times that we need to + // select from among multiple UPnP devices in selectBestService. + metricUPnPSelectMultiple = clientmetric.NewCounter("portmap_upnp_select_multiple") + + // metricUPnPSelectExternalPublic counts the number of times that + // selectBestService picked a UPnP device with an external public IP. + metricUPnPSelectExternalPublic = clientmetric.NewCounter("portmap_upnp_select_external_public") + + // metricUPnPSelectExternalPrivate counts the number of times that + // selectBestService picked a UPnP device with an external private IP. + metricUPnPSelectExternalPrivate = clientmetric.NewCounter("portmap_upnp_select_external_private") + + // metricUPnPSelectUp counts the number of times that selectBestService + // picked a UPnP device that was up but with no external IP. + metricUPnPSelectUp = clientmetric.NewCounter("portmap_upnp_select_up") + + // metricUPnPSelectNone counts the number of times that selectBestService + // picked a UPnP device that is not up. + metricUPnPSelectNone = clientmetric.NewCounter("portmap_upnp_select_none") +) diff --git a/net/portmapper/upnp.go b/net/portmapper/upnp.go index 7203dead8..8b03ffbeb 100644 --- a/net/portmapper/upnp.go +++ b/net/portmapper/upnp.go @@ -30,6 +30,7 @@ import ( "github.com/huin/goupnp/soap" "tailscale.com/envknob" "tailscale.com/net/netns" + "tailscale.com/types/bools" "tailscale.com/types/logger" "tailscale.com/util/ctxkey" "tailscale.com/util/mak" @@ -514,6 +515,11 @@ func (c *Client) getUPnPPortMapping( steps = append(steps, step{meta: meta}) } + if len(steps) > 0 { + renew := haveOldMapping && now.Before(oldMapping.goodUntil) + bools.IfElse(renew, metricCreateRenewUPnPSent, metricCreateNewUPnPSent).Add(1) + } + // Now, iterate through every meta that we have trying to get an // external IP address. If we succeed, we'll return; if we fail, we // continue this loop.