diff --git a/pkg/tcpip/network/ipv6/ipv6.go b/pkg/tcpip/network/ipv6/ipv6.go index ff5b44653..fc8e56871 100644 --- a/pkg/tcpip/network/ipv6/ipv6.go +++ b/pkg/tcpip/network/ipv6/ipv6.go @@ -2196,7 +2196,7 @@ func (e *endpoint) acquireOutgoingPrimaryAddressRLocked(remoteAddr tcpip.Address // Return the most preferred address that can have its reference count // incremented. for _, c := range cs { - if c.addressEndpoint.IncRef() { + if c.addressEndpoint.TryIncRef() { return c.addressEndpoint } } diff --git a/pkg/tcpip/stack/addressable_endpoint_state.go b/pkg/tcpip/stack/addressable_endpoint_state.go index 40be2e6a9..f472bbf0a 100644 --- a/pkg/tcpip/stack/addressable_endpoint_state.go +++ b/pkg/tcpip/stack/addressable_endpoint_state.go @@ -480,7 +480,7 @@ func (a *AddressableEndpointState) acquirePrimaryAddressRLocked(remoteAddr tcpip bestLen = stateLen } } - if best != nil && best.IncRef() { + if best != nil && best.TryIncRef() { return best } } @@ -492,7 +492,7 @@ func (a *AddressableEndpointState) acquirePrimaryAddressRLocked(remoteAddr tcpip } if !ep.Deprecated() { - if ep.IncRef() { + if ep.TryIncRef() { // ep is not deprecated, so return it immediately. // // If we kept track of a deprecated endpoint, decrement its reference @@ -509,7 +509,7 @@ func (a *AddressableEndpointState) acquirePrimaryAddressRLocked(remoteAddr tcpip return ep } - } else if deprecatedEndpoint == nil && ep.IncRef() { + } else if deprecatedEndpoint == nil && ep.TryIncRef() { // We prefer an endpoint that is not deprecated, but we keep track of // ep in case a doesn't have any non-deprecated endpoints. // @@ -541,7 +541,7 @@ func (a *AddressableEndpointState) AcquireAssignedAddressOrMatching(localAddr tc return nil } - if !addrState.IncRef() { + if !addrState.TryIncRef() { panic(fmt.Sprintf("failed to increase the reference count for address = %s", addrState.addr)) } @@ -550,7 +550,7 @@ func (a *AddressableEndpointState) AcquireAssignedAddressOrMatching(localAddr tc if f != nil { for _, addrState := range a.endpoints { - if addrState.IsAssigned(allowTemp) && f(addrState) && addrState.IncRef() { + if addrState.IsAssigned(allowTemp) && f(addrState) && addrState.TryIncRef() { return addrState } } @@ -805,7 +805,7 @@ func (a *addressState) IsAssigned(allowExpired bool) bool { } // IncRef implements AddressEndpoint. -func (a *addressState) IncRef() bool { +func (a *addressState) TryIncRef() bool { return a.refs.TryIncRef() } diff --git a/pkg/tcpip/stack/addressable_endpoint_state_test.go b/pkg/tcpip/stack/addressable_endpoint_state_test.go index 056c6470c..1d320c430 100644 --- a/pkg/tcpip/stack/addressable_endpoint_state_test.go +++ b/pkg/tcpip/stack/addressable_endpoint_state_test.go @@ -80,7 +80,7 @@ func TestAddressDispatcherExpiredToAssigned(t *testing.T) { t.Fatalf("s.AddAndAcquirePermanentAddress(%s, {}): %s", addr, err) } defer ep.DecRef() - if !ep.IncRef() { + if !ep.TryIncRef() { t.Fatalf("failed to increase ref count of address endpoint") } diff --git a/pkg/tcpip/stack/registration.go b/pkg/tcpip/stack/registration.go index 4cd720820..bbfe1a797 100644 --- a/pkg/tcpip/stack/registration.go +++ b/pkg/tcpip/stack/registration.go @@ -534,11 +534,11 @@ type AssignableAddressEndpoint interface { // to its NetworkEndpoint. IsAssigned(allowExpired bool) bool - // IncRef increments this endpoint's reference count. + // TryIncRef tries to increment this endpoint's reference count. // // Returns true if it was successfully incremented. If it returns false, then // the endpoint is considered expired and should no longer be used. - IncRef() bool + TryIncRef() bool // DecRef decrements this endpoint's reference count. DecRef() diff --git a/pkg/tcpip/stack/route.go b/pkg/tcpip/stack/route.go index 9755362af..32ce9a7f0 100644 --- a/pkg/tcpip/stack/route.go +++ b/pkg/tcpip/stack/route.go @@ -537,7 +537,7 @@ func (r *Route) Acquire() { // +checklocksread:r.mu func (r *Route) acquireLocked() { if ep := r.localAddressEndpoint; ep != nil { - if !ep.IncRef() { + if !ep.TryIncRef() { panic(fmt.Sprintf("failed to increment reference count for local address endpoint = %s", r.LocalAddress())) } }