1.1 使用系统默认的buf
在创建socket的时候,会在inet_create 调用sock_init_data函数,然后对sk_sndbuf和sk_rcvbuf的设置,下面是对这两个buf的设置,但是会被后面的tcp_v4_init_sock重新赋值修改,
sk->sk_rcvbuf = sysctl_rmem_default;
sk->sk_sndbuf = sysctl_wmem_default;
如果用户空间没有使用setsockopt函数的SO_RCVBUF参数设置接收缓存时,其初始化的值在tcp_v4_init_sock函数中确定:
sk->sk_sndbuf = sysctl_tcp_wmem[1];
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1.2 用户空间配置buf
1) 如果用户使用的setsocketopt设置了SO_RCVBUF,则进入下面的逻辑配置sk_rcvbuf:
点击(此处)折叠或打开
-
case SO_RCVBUF:
-
val = min_t(u32, val, sysctl_rmem_max);//用户设置的值不能大于sysctl_rmem_max
-
set_rcvbuf:
-
sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
-
/* #define SOCK_MIN_RCVBUF TCP_SKB_MIN_TRUESIZE
-
#define TCP_SKB_MIN_TRUESIZE (2048 + SKB_DATA_ALIGN(sizeof(struct sk_buff))) */
-
sk->sk_rcvbuf = max_t(u32, val * 2, SOCK_MIN_RCVBUF);
- break;
点击(此处)折叠或打开
-
2)sk_sndbuf
-
case SO_SNDBUF:
-
val = min_t(u32, val, sysctl_wmem_max);
-
set_sndbuf:
-
sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
-
sk->sk_sndbuf = max_t(u32, val * 2, SOCK_MIN_SNDBUF);
-
/* Wake up sending tasks if we upped the value. */
-
sk->sk_write_space(sk);
- break;