Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding set/getsockopt SO_SNDBUF size doubles

Tags:

c

linux

sockets

Hi I have the following program to check the send buffer size for a UDP socket. However, I the return value is a bit confusing to me. I use the following simple app:

#include <sys/socket.h> #include <stdio.h>  int main(int argc, char **argv) {  int sockfd, sendbuff;  socklen_t optlen;   sockfd = socket(AF_INET, SOCK_DGRAM, 0);  if(sockfd == -1)      printf("Error");   int res = 0;   // Get buffer size  optlen = sizeof(sendbuff);  res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);   if(res == -1)      printf("Error getsockopt one");  else      printf("send buffer size = %d\n", sendbuff);   // Set buffer size  sendbuff = 98304;   printf("sets the send buffer to %d\n", sendbuff);  res = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff));   if(res == -1)      printf("Error setsockopt");    // Get buffer size  optlen = sizeof(sendbuff);  res = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sendbuff, &optlen);   if(res == -1)      printf("Error getsockopt two");  else      printf("send buffer size = %d\n", sendbuff);   return 0; } 

The output on my machine is:

send buffer size = 129024

sets the send buffer to 98304

new send buffer size = 196608

Can anybody clarify what I'm doing wrong here or how to interpret the output?

like image 449
mortenvp Avatar asked Jan 08 '10 22:01

mortenvp


People also ask

What is So_sndbuf?

SO_SNDBUF. public static final SocketOption<Integer> SO_SNDBUF. The size of the socket send buffer. The value of this socket option is an Integer that is the size of the socket send buffer in bytes. The socket send buffer is an output buffer used by the networking implementation.

What is Setsockopt?

The setsockopt function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost socket level.

What is socket options in Linux?

In addition to binding a socket to a local address or connecting it to a destination address, application programs need a method to control the socket. For example, when using protocols that use time out and retransmission, the application program may want to obtain or set the time-out parameters.


1 Answers

You're not doing anything wrong. Linux doubles the value (within the kernel) when you set it, and returns the doubled value when you query it. man 7 socket says:

 [...]      SO_SNDBUF               Sets or gets the maximum socket send buffer in bytes.  The  ker-               nel doubles this value (to allow space for bookkeeping overhead)               when it is set using setsockopt(), and  this  doubled  value  is               returned  by  getsockopt().   The  default  value  is set by the               wmem_default sysctl and the maximum allowed value is set by  the               wmem_max sysctl.  The minimum (doubled) value for this option is               2048. [...]  NOTES        Linux assumes that half of the send/receive buffer is used for internal        kernel structures; thus the sysctls are twice what can be  observed  on        the wire. [...] 
like image 159
Matthew Slattery Avatar answered Sep 18 '22 12:09

Matthew Slattery