Skip to content

Commit

Permalink
sock: allow reading and changing sk_userlocks with setsockopt
Browse files Browse the repository at this point in the history
SOCK_SNDBUF_LOCK and SOCK_RCVBUF_LOCK flags disable automatic socket
buffers adjustment done by kernel (see tcp_fixup_rcvbuf() and
tcp_sndbuf_expand()). If we've just created a new socket this adjustment
is enabled on it, but if one changes the socket buffer size by
setsockopt(SO_{SND,RCV}BUF*) it becomes disabled.

CRIU needs to call setsockopt(SO_{SND,RCV}BUF*) on each socket on
restore as it first needs to increase buffer sizes for packet queues
restore and second it needs to restore back original buffer sizes. So
after CRIU restore all sockets become non-auto-adjustable, which can
decrease network performance of restored applications significantly.

CRIU need to be able to restore sockets with enabled/disabled adjustment
to the same state it was before dump, so let's add special setsockopt
for it.

Let's also export SOCK_SNDBUF_LOCK and SOCK_RCVBUF_LOCK flags to uAPI so
that using these interface one can reenable automatic socket buffer
adjustment on their sockets.

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Pavel Tikhomirov authored and David S. Miller committed Aug 4, 2021
1 parent 625af9f commit 04190bf
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions arch/alpha/include/uapi/asm/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@

#define SO_NETNS_COOKIE 71

#define SO_BUF_LOCK 72

#if !defined(__KERNEL__)

#if __BITS_PER_LONG == 64
Expand Down
2 changes: 2 additions & 0 deletions arch/mips/include/uapi/asm/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@

#define SO_NETNS_COOKIE 71

#define SO_BUF_LOCK 72

#if !defined(__KERNEL__)

#if __BITS_PER_LONG == 64
Expand Down
2 changes: 2 additions & 0 deletions arch/parisc/include/uapi/asm/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@

#define SO_NETNS_COOKIE 0x4045

#define SO_BUF_LOCK 0x4046

#if !defined(__KERNEL__)

#if __BITS_PER_LONG == 64
Expand Down
2 changes: 2 additions & 0 deletions arch/sparc/include/uapi/asm/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

#define SO_NETNS_COOKIE 0x0050

#define SO_BUF_LOCK 0x0051

#if !defined(__KERNEL__)


Expand Down
3 changes: 1 addition & 2 deletions include/net/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include <net/tcp_states.h>
#include <linux/net_tstamp.h>
#include <net/l3mdev.h>
#include <uapi/linux/socket.h>

/*
* This structure really needs to be cleaned up.
Expand Down Expand Up @@ -1438,8 +1439,6 @@ static inline int __sk_prot_rehash(struct sock *sk)
#define RCV_SHUTDOWN 1
#define SEND_SHUTDOWN 2

#define SOCK_SNDBUF_LOCK 1
#define SOCK_RCVBUF_LOCK 2
#define SOCK_BINDADDR_LOCK 4
#define SOCK_BINDPORT_LOCK 8

Expand Down
2 changes: 2 additions & 0 deletions include/uapi/asm-generic/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@

#define SO_NETNS_COOKIE 71

#define SO_BUF_LOCK 72

#if !defined(__KERNEL__)

#if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
Expand Down
5 changes: 5 additions & 0 deletions include/uapi/linux/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ struct __kernel_sockaddr_storage {
};
};

#define SOCK_SNDBUF_LOCK 1
#define SOCK_RCVBUF_LOCK 2

#define SOCK_BUF_LOCK_MASK (SOCK_SNDBUF_LOCK | SOCK_RCVBUF_LOCK)

#endif /* _UAPI_LINUX_SOCKET_H */
13 changes: 13 additions & 0 deletions net/core/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,15 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
ret = sock_bindtoindex_locked(sk, val);
break;

case SO_BUF_LOCK:
if (val & ~SOCK_BUF_LOCK_MASK) {
ret = -EINVAL;
break;
}
sk->sk_userlocks = val | (sk->sk_userlocks &
~SOCK_BUF_LOCK_MASK);
break;

default:
ret = -ENOPROTOOPT;
break;
Expand Down Expand Up @@ -1720,6 +1729,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
v.val64 = sock_net(sk)->net_cookie;
break;

case SO_BUF_LOCK:
v.val = sk->sk_userlocks & SOCK_BUF_LOCK_MASK;
break;

default:
/* We implement the SO_SNDLOWAT etc to not be settable
* (1003.1g 7).
Expand Down

0 comments on commit 04190bf

Please sign in to comment.