Skip to contentSkip to conversation

net: sock: adapt SOCK_MIN_RCVBUF and SOCK_MIN_SNDBUF

PATCH Superseded Daniel Borkmann 3 participants 5 messages Jun 19, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: sock: adapt SOCK_MIN_RCVBUF and SOCK_MIN_SNDBUF

The current situation is that SOCK_MIN_RCVBUF is 2048 + sizeof(struct sk_buff)) while SOCK_MIN_SNDBUF is 2048. Since in both cases, skb->truesize is used for sk_{r,w}mem_alloc accounting, we should have both sizes equal and adjusted through the macro SKB_TRUESIZE(), which is also used elsewhere to adjust sk buffer sizes. The minor adaption in sk_stream_moderate_sndbuf() is to silence a warning by using a typed max macro, as similarly done in SOCK_MIN_RCVBUF occurences, that would appear otherwise.

---
 include/net/sock.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
include/net/sock.h +6 -5
index ac8e181..189ef98 100644
@@ -2045,18 +2045,19 @@ static inline void sk_wake_async(struct sock *sk, int how, int band)
 		sock_wake_async(sk->sk_socket, how, band);
 }
 
-#define SOCK_MIN_SNDBUF 2048
 /*
- * Since sk_rmem_alloc sums skb->truesize, even a small frame might need
- * sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak
+ * Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
+ * need sizeof(sk_buff) + sizeof(skb_shared_info) + MTU + padding, unless
+ * net driver perform copybreak.
  */
-#define SOCK_MIN_RCVBUF (2048 + sizeof(struct sk_buff))
+#define SOCK_MIN_RCVBUF		SKB_TRUESIZE(2048)
+#define SOCK_MIN_SNDBUF		SKB_TRUESIZE(2048)
 
 static inline void sk_stream_moderate_sndbuf(struct sock *sk)
 {
 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
 		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
-		sk->sk_sndbuf = max(sk->sk_sndbuf, SOCK_MIN_SNDBUF);
+		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
 	}
 }
 
Signature · 1 line
1.7.11.7
#1 lore raw
Quoted 33 lines · Daniel Borkmann

The current situation is that SOCK_MIN_RCVBUF is 2048 + sizeof(struct sk_buff)) while SOCK_MIN_SNDBUF is 2048. Since in both cases, skb->truesize is used for sk_{r,w}mem_alloc accounting, we should have both sizes equal and adjusted through the macro SKB_TRUESIZE(), which is also used elsewhere to adjust sk buffer sizes. The minor adaption in sk_stream_moderate_sndbuf() is to silence a warning by using a typed max macro, as similarly done in SOCK_MIN_RCVBUF occurences, that would appear otherwise.

---
 include/net/sock.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
include/net/sock.h +5 -4
index ac8e181..189ef98 100644
@@ -2045,18 +2045,19 @@ static inline void sk_wake_async(struct sock *sk, int how, int band)
 		sock_wake_async(sk->sk_socket, how, band);
 }
 
-#define SOCK_MIN_SNDBUF 2048
 /*
- * Since sk_rmem_alloc sums skb->truesize, even a small frame might need
- * sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak
+ * Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
+ * need sizeof(sk_buff) + sizeof(skb_shared_info) + MTU + padding, unless
+ * net driver perform copybreak.
  */
-#define SOCK_MIN_RCVBUF (2048 + sizeof(struct sk_buff))
+#define SOCK_MIN_RCVBUF		SKB_TRUESIZE(2048)
+#define SOCK_MIN_SNDBUF		SKB_TRUESIZE(2048)

include/net/sock.h static inline void sk_wake_async(struct sock *sk, int how, int band) · line 2063

Quoted 10 lines
include/net/sock.h +1 -1
 
 
 static inline void sk_stream_moderate_sndbuf(struct sock *sk)
 {
 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
 		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
-		sk->sk_sndbuf = max(sk->sk_sndbuf, SOCK_MIN_SNDBUF);
+		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
 	}
 }

Funny you send this patch, because I prepared a similar patch yesterday ;)

My motivation was a bit different, because we hit a (small) regression here in Google for some applications setting low SO_SNDBUF/SO_RCVBUF values, because of new TCP needs :

Minimal skb truesize in transmit path is indeed SKB_TRUESIZE(2048) after commit f07d960df33c5aef ("tcp: avoid frag allocation for small frames")

And tcp sendmsg() tries to limit skb size to half the congestion window, meaning we try to build two skbs at minimum.

So I believe that we need :

/* TCP works better if we can build two skbs at minimum */
#define SOCK_MIN_SNDBUF (2 * SKB_TRUESIZE(2048))

#2 lore raw
Quoted 35 lines · Eric Dumazet
Quoted 34 lines · Daniel Borkmann

The current situation is that SOCK_MIN_RCVBUF is 2048 + sizeof(struct sk_buff)) while SOCK_MIN_SNDBUF is 2048. Since in both cases, skb->truesize is used for sk_{r,w}mem_alloc accounting, we should have both sizes equal and adjusted through the macro SKB_TRUESIZE(), which is also used elsewhere to adjust sk buffer sizes. The minor adaption in sk_stream_moderate_sndbuf() is to silence a warning by using a typed max macro, as similarly done in SOCK_MIN_RCVBUF occurences, that would appear otherwise.

---
  include/net/sock.h | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)
include/net/sock.h +5 -4
index ac8e181..189ef98 100644
@@ -2045,18 +2045,19 @@ static inline void sk_wake_async(struct sock *sk, int how, int band)
  		sock_wake_async(sk->sk_socket, how, band);
  }
 
-#define SOCK_MIN_SNDBUF 2048
  /*
- * Since sk_rmem_alloc sums skb->truesize, even a small frame might need
- * sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak
+ * Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
+ * need sizeof(sk_buff) + sizeof(skb_shared_info) + MTU + padding, unless
+ * net driver perform copybreak.
   */
-#define SOCK_MIN_RCVBUF (2048 + sizeof(struct sk_buff))
+#define SOCK_MIN_RCVBUF		SKB_TRUESIZE(2048)
+#define SOCK_MIN_SNDBUF		SKB_TRUESIZE(2048)
 
 
Quoted 12 lines

include/net/sock.h static inline void sk_wake_async(struct sock *sk, int how, int band) · line 2062

Quoted 9 lines
include/net/sock.h +1 -1
  static inline void sk_stream_moderate_sndbuf(struct sock *sk)
  {
  	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
  		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
-		sk->sk_sndbuf = max(sk->sk_sndbuf, SOCK_MIN_SNDBUF);
+		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
  	}
  }

Funny you send this patch, because I prepared a similar patch yesterday ;)

Hehe, that is indeed funny. :-)

Quoted 7 lines

My motivation was a bit different, because we hit a (small) regression here in Google for some applications setting low SO_SNDBUF/SO_RCVBUF values, because of new TCP needs :

Minimal skb truesize in transmit path is indeed SKB_TRUESIZE(2048) after commit f07d960df33c5aef ("tcp: avoid frag allocation for small frames")

Quoted 7 lines

And tcp sendmsg() tries to limit skb size to half the congestion window, meaning we try to build two skbs at minimum.

So I believe that we need :

/* TCP works better if we can build two skbs at minimum */
#define SOCK_MIN_SNDBUF (2 * SKB_TRUESIZE(2048))

Ok, if you prefer, I can send an update.

Thanks,

Daniel

#3 lore raw
Quoted 2 lines · Daniel Borkmann

Ok, if you prefer, I can send an update.

Yes please.

By the way, I used exactly :

-#define SOCK_MIN_SNDBUF 2048
+/* minimum tcp skb truesize is 2048 + sizeof(struct sk_buff) */ +#define TCP_SKB_MIN_TRUESIZE (2048 + sizeof(struct sk_buff)) +
+#define SOCK_MIN_SNDBUF (2 * TCP_SKB_MIN_TRUESIZE)

because SKB_TRUESIZE(2048) adds
SKB_DATA_ALIGN(sizeof(struct skb_shared_info))

But in case of TCP skbs, the skb_shared_info is part of the 2048 bytes allocation for skb->head

#4 lore raw
In reply to Eric Dumazet
Quoted 55 lines · Eric Dumazet
Quoted 33 lines · Daniel Borkmann

The current situation is that SOCK_MIN_RCVBUF is 2048 + sizeof(struct sk_buff)) while SOCK_MIN_SNDBUF is 2048. Since in both cases, skb->truesize is used for sk_{r,w}mem_alloc accounting, we should have both sizes equal and adjusted through the macro SKB_TRUESIZE(), which is also used elsewhere to adjust sk buffer sizes. The minor adaption in sk_stream_moderate_sndbuf() is to silence a warning by using a typed max macro, as similarly done in SOCK_MIN_RCVBUF occurences, that would appear otherwise.

---
 include/net/sock.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
include/net/sock.h +5 -4
index ac8e181..189ef98 100644
@@ -2045,18 +2045,19 @@ static inline void sk_wake_async(struct sock *sk, int how, int band)
 		sock_wake_async(sk->sk_socket, how, band);
 }
 
-#define SOCK_MIN_SNDBUF 2048
 /*
- * Since sk_rmem_alloc sums skb->truesize, even a small frame might need
- * sizeof(sk_buff) + MTU + padding, unless net driver perform copybreak
+ * Since sk_{r,w}mem_alloc sums skb->truesize, even a small frame might
+ * need sizeof(sk_buff) + sizeof(skb_shared_info) + MTU + padding, unless
+ * net driver perform copybreak.
  */
-#define SOCK_MIN_RCVBUF (2048 + sizeof(struct sk_buff))
+#define SOCK_MIN_RCVBUF		SKB_TRUESIZE(2048)
+#define SOCK_MIN_SNDBUF		SKB_TRUESIZE(2048)

include/net/sock.h static inline void sk_wake_async(struct sock *sk, int how, int band) · line 2063

Quoted 10 lines
include/net/sock.h +1 -1
 
 
 static inline void sk_stream_moderate_sndbuf(struct sock *sk)
 {
 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) {
 		sk->sk_sndbuf = min(sk->sk_sndbuf, sk->sk_wmem_queued >> 1);
-		sk->sk_sndbuf = max(sk->sk_sndbuf, SOCK_MIN_SNDBUF);
+		sk->sk_sndbuf = max_t(u32, sk->sk_sndbuf, SOCK_MIN_SNDBUF);
 	}
 }

Funny you send this patch, because I prepared a similar patch yesterday ;)

My motivation was a bit different, because we hit a (small) regression here in Google for some applications setting low SO_SNDBUF/SO_RCVBUF values, because of new TCP needs :

Minimal skb truesize in transmit path is indeed SKB_TRUESIZE(2048) after commit f07d960df33c5aef ("tcp: avoid frag allocation for small frames")

Quoted 7 lines

And tcp sendmsg() tries to limit skb size to half the congestion window, meaning we try to build two skbs at minimum.

So I believe that we need :

/* TCP works better if we can build two skbs at minimum */
#define SOCK_MIN_SNDBUF (2 * SKB_TRUESIZE(2048))

Or more exactly :