Skip to content

Commit

Permalink
RLock Endpoint in raw.Endpoint.HandlePacket
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 348530530
  • Loading branch information
kevinGC authored and gvisor-bot committed Dec 21, 2020
1 parent 9a72730 commit 981faa2
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/tcpip/transport/raw/endpoint.go
Expand Up @@ -620,6 +620,7 @@ func (e *endpoint) GetSockOptInt(opt tcpip.SockOptInt) (int, *tcpip.Error) {

// HandlePacket implements stack.RawTransportEndpoint.HandlePacket.
func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) {
e.mu.RLock()
e.rcvMu.Lock()

// Drop the packet if our buffer is currently full or if this is an unassociated
Expand All @@ -632,13 +633,15 @@ func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) {
// sockets.
if e.rcvClosed || !e.associated {
e.rcvMu.Unlock()
e.mu.RUnlock()
e.stack.Stats().DroppedPackets.Increment()
e.stats.ReceiveErrors.ClosedReceiver.Increment()
return
}

if e.rcvBufSize >= e.rcvBufSizeMax {
e.rcvMu.Unlock()
e.mu.RUnlock()
e.stack.Stats().DroppedPackets.Increment()
e.stats.ReceiveErrors.ReceiveBufferOverflow.Increment()
return
Expand All @@ -650,11 +653,13 @@ func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) {
// If bound to a NIC, only accept data for that NIC.
if e.BindNICID != 0 && e.BindNICID != pkt.NICID {
e.rcvMu.Unlock()
e.mu.RUnlock()
return
}
// If bound to an address, only accept data for that address.
if e.BindAddr != "" && e.BindAddr != remoteAddr {
e.rcvMu.Unlock()
e.mu.RUnlock()
return
}
}
Expand All @@ -663,6 +668,7 @@ func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) {
// connected to.
if e.connected && e.route.RemoteAddress != remoteAddr {
e.rcvMu.Unlock()
e.mu.RUnlock()
return
}

Expand Down Expand Up @@ -697,6 +703,7 @@ func (e *endpoint) HandlePacket(pkt *stack.PacketBuffer) {
e.rcvList.PushBack(packet)
e.rcvBufSize += packet.data.Size()
e.rcvMu.Unlock()
e.mu.RUnlock()
e.stats.PacketsReceived.Increment()
// Notify waiters that there's data to be read.
if wasEmpty {
Expand Down

0 comments on commit 981faa2

Please sign in to comment.