I'm trying to change the default socket send buffer size to a small size, in order to see how the UDP throughput gets affected for small UDP datagrams. To do this, I use the setsockopt
function with the option SO_SNDBUF
and I am trying to set the buffer size to 64 bytes. I also use getsockopt
to see the result of the setsockopt
function.
Here is the code I use:
int sock_fd;
struct sockaddr_in server_addr;
static int target_port = PORT;
int curr_snd_buff = 0;
int sk_snd_buff = 64;
socklen_t optlen;
if( (sock_fd=socket(AF_INET,SOCK_DGRAM,0)) == -1)
fatal("in socket (client_udp)");
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr=inet_addr(args->a_ip);
server_addr.sin_port=htons(target_port);
if ( sk_snd_buff > 0 ) >{
optlen = sizeof(sk_snd_buff);
// get the default socketh send buffer size
if (getsockopt(sock_fd, SOL_SOCKET, SO_SNDBUF, &curr_snd_buff, &optlen) == -1) {
fatal("getting the socket send buffer");
}
printf("* default socket send buffer: %d\n", curr_snd_buff);
printf("* attempting to change it to: %d\n", sk_snd_buff);
if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDBUF, &sk_snd_buff, optlen) == -1) {
fatal("changing the socket send buffer");
}
if (getsockopt(sock_fd, SOL_SOCKET, SO_SNDBUF, &curr_snd_buff, &optlen) == -1) {
fatal("getting the socket send buffer");
}
printf("* new socket send buffer: %d\n", curr_snd_buff);
}
But, what I see is that there is a lower limit of the buffer size set to 4608 bytes each time I try to set a small value. Here is the output of the above code:
By trying various sizes, I found that this happens for sizes <= 2304. For larger sizes, eg 2305, I get a greater size:
and for some greater sizes, eg 3000, the doubled of the size that I have requested:
By searching throught the net I found that Linux doubles the value within the kernel when you set it, and returns the doubled value when you query it: Understanding set/getsockopt SO_SNDBUF
Is there a way to reduce the minimum lower limit of the socket send buffer size? Eg, to set it to 64 bytes?
The minimum buffer size is a function of the kernel. The Linux docs for option SO_SNDBUF
have this to say:
Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/wmem_default file and the maximum allowed value is set by the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for this option is 2048.
(socket(7) manpage)
Details will likely vary on different systems, but
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.