summaryrefslogtreecommitdiffhomepage
path: root/pkg/tcpip/socketops.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/tcpip/socketops.go')
-rw-r--r--pkg/tcpip/socketops.go59
1 files changed, 28 insertions, 31 deletions
diff --git a/pkg/tcpip/socketops.go b/pkg/tcpip/socketops.go
index 5642c86f8..34ac62444 100644
--- a/pkg/tcpip/socketops.go
+++ b/pkg/tcpip/socketops.go
@@ -17,6 +17,7 @@ package tcpip
import (
"sync/atomic"
+ "gvisor.dev/gvisor/pkg/atomicbitops"
"gvisor.dev/gvisor/pkg/sync"
)
@@ -56,6 +57,11 @@ type SocketOptionsHandler interface {
// OnSetReceiveBufferSize is invoked by SO_RCVBUF and SO_RCVBUFFORCE.
OnSetReceiveBufferSize(v, oldSz int64) (newSz int64)
+
+ // WakeupWriters is invoked when the send buffer size for an endpoint is
+ // changed. The handler notifies the writers if the send buffer size is
+ // increased with setsockopt(2) for TCP endpoints.
+ WakeupWriters()
}
// DefaultSocketOptionsHandler is an embeddable type that implements no-op
@@ -97,6 +103,9 @@ func (*DefaultSocketOptionsHandler) OnSetSendBufferSize(v int64) (newSz int64) {
return v
}
+// WakeupWriters implements SocketOptionsHandler.WakeupWriters.
+func (*DefaultSocketOptionsHandler) WakeupWriters() {}
+
// OnSetReceiveBufferSize implements SocketOptionsHandler.OnSetReceiveBufferSize.
func (*DefaultSocketOptionsHandler) OnSetReceiveBufferSize(v, oldSz int64) (newSz int64) {
return v
@@ -207,24 +216,16 @@ type SocketOptions struct {
// will not change.
getSendBufferLimits GetSendBufferLimits `state:"manual"`
- // sendBufSizeMu protects sendBufferSize and calls to
- // handler.OnSetSendBufferSize.
- sendBufSizeMu sync.Mutex `state:"nosave"`
-
// sendBufferSize determines the send buffer size for this socket.
- sendBufferSize int64
+ sendBufferSize atomicbitops.AlignedAtomicInt64
// getReceiveBufferLimits provides the handler to get the min, default and
// max size for receive buffer. It is initialized at the creation time and
// will not change.
getReceiveBufferLimits GetReceiveBufferLimits `state:"manual"`
- // receiveBufSizeMu protects receiveBufferSize and calls to
- // handler.OnSetReceiveBufferSize.
- receiveBufSizeMu sync.Mutex `state:"nosave"`
-
// receiveBufferSize determines the receive buffer size for this socket.
- receiveBufferSize int64
+ receiveBufferSize atomicbitops.AlignedAtomicInt64
// mu protects the access to the below fields.
mu sync.Mutex `state:"nosave"`
@@ -614,6 +615,11 @@ func (so *SocketOptions) SetBindToDevice(bindToDevice int32) Error {
return nil
}
+// GetSendBufferSize gets value for SO_SNDBUF option.
+func (so *SocketOptions) GetSendBufferSize() int64 {
+ return so.sendBufferSize.Load()
+}
+
// SendBufferLimits returns the [min, max) range of allowable send buffer
// sizes.
func (so *SocketOptions) SendBufferLimits() (min, max int64) {
@@ -621,22 +627,21 @@ func (so *SocketOptions) SendBufferLimits() (min, max int64) {
return int64(limits.Min), int64(limits.Max)
}
-// GetSendBufferSize gets value for SO_SNDBUF option.
-func (so *SocketOptions) GetSendBufferSize() int64 {
- so.sendBufSizeMu.Lock()
- defer so.sendBufSizeMu.Unlock()
- return so.sendBufferSize
-}
-
// SetSendBufferSize sets value for SO_SNDBUF option. notify indicates if the
// stack handler should be invoked to set the send buffer size.
func (so *SocketOptions) SetSendBufferSize(sendBufferSize int64, notify bool) {
- so.sendBufSizeMu.Lock()
- defer so.sendBufSizeMu.Unlock()
if notify {
sendBufferSize = so.handler.OnSetSendBufferSize(sendBufferSize)
}
- so.sendBufferSize = sendBufferSize
+ so.sendBufferSize.Store(sendBufferSize)
+ if notify {
+ so.handler.WakeupWriters()
+ }
+}
+
+// GetReceiveBufferSize gets value for SO_RCVBUF option.
+func (so *SocketOptions) GetReceiveBufferSize() int64 {
+ return so.receiveBufferSize.Load()
}
// ReceiveBufferLimits returns the [min, max) range of allowable receive buffer
@@ -646,20 +651,12 @@ func (so *SocketOptions) ReceiveBufferLimits() (min, max int64) {
return int64(limits.Min), int64(limits.Max)
}
-// GetReceiveBufferSize gets value for SO_RCVBUF option.
-func (so *SocketOptions) GetReceiveBufferSize() int64 {
- so.receiveBufSizeMu.Lock()
- defer so.receiveBufSizeMu.Unlock()
- return so.receiveBufferSize
-}
-
// SetReceiveBufferSize sets the value of the SO_RCVBUF option, optionally
// notifying the owning endpoint.
func (so *SocketOptions) SetReceiveBufferSize(receiveBufferSize int64, notify bool) {
- so.receiveBufSizeMu.Lock()
- defer so.receiveBufSizeMu.Unlock()
if notify {
- receiveBufferSize = so.handler.OnSetReceiveBufferSize(receiveBufferSize, so.receiveBufferSize)
+ oldSz := so.receiveBufferSize.Load()
+ receiveBufferSize = so.handler.OnSetReceiveBufferSize(receiveBufferSize, oldSz)
}
- so.receiveBufferSize = receiveBufferSize
+ so.receiveBufferSize.Store(receiveBufferSize)
}