diff options
author | Tamir Duberstein <tamird@google.com> | 2021-02-26 11:16:14 -0800 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2021-02-26 11:18:30 -0800 |
commit | da2505df94770301e7907cfaff5e5c7fdd531b75 (patch) | |
tree | 203767a21084cb8831bfaaf71750d199ffe7e09e /pkg/tcpip/stack/transport_demuxer.go | |
parent | 8e78d0eda6ac3b72ec6f9db1f559035410a3a212 (diff) |
Use closure to avoid manual unlocking
Also increase refcount of raw.endpoint.route while in use.
Avoid allocating an array of size zero.
PiperOrigin-RevId: 359797788
Diffstat (limited to 'pkg/tcpip/stack/transport_demuxer.go')
-rw-r--r-- | pkg/tcpip/stack/transport_demuxer.go | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/pkg/tcpip/stack/transport_demuxer.go b/pkg/tcpip/stack/transport_demuxer.go index 61cbaf688..e799f9290 100644 --- a/pkg/tcpip/stack/transport_demuxer.go +++ b/pkg/tcpip/stack/transport_demuxer.go @@ -583,9 +583,14 @@ func (d *transportDemuxer) deliverRawPacket(protocol tcpip.TransportProtocolNumb // raw endpoint first. If there are multiple raw endpoints, they all // receive the packet. eps.mu.RLock() - // Copy the list of raw endpoints so we can release eps.mu earlier. - rawEPs := make([]RawTransportEndpoint, len(eps.rawEndpoints)) - copy(rawEPs, eps.rawEndpoints) + // Copy the list of raw endpoints to avoid packet handling under lock. + var rawEPs []RawTransportEndpoint + if n := len(eps.rawEndpoints); n != 0 { + rawEPs = make([]RawTransportEndpoint, n) + if m := copy(rawEPs, eps.rawEndpoints); m != n { + panic(fmt.Sprintf("unexpected copy = %d, want %d", m, n)) + } + } eps.mu.RUnlock() for _, rawEP := range rawEPs { // Each endpoint gets its own copy of the packet for the sake @@ -593,7 +598,7 @@ func (d *transportDemuxer) deliverRawPacket(protocol tcpip.TransportProtocolNumb rawEP.HandlePacket(pkt.Clone()) } - return len(rawEPs) > 0 + return len(rawEPs) != 0 } // deliverError attempts to deliver the given error to the appropriate transport |