A Linux system has a default socket buffer size for receiving and sending data. These values can be modified on most Linux systems, provided your process is running as root. Modifying these values can help increase network processing performance on both send and receive of packets.
Linux has a set of default values that are viewable via the /proc filesystem:
/proc/sys/net/core/
/rmem_default: The default setting of the socket receive buffer in bytes.
/rmem_max: The maximum receive socket buffer size in bytes.
/wmem_default: The default setting (in bytes) of the socket send buffer.
/wmem_max: The maximum send socket buffer size in bytes.
If you output the rmem_max you can view the size in bytes:
$ cat /proc/sys/net/core/rmem_max
131071
These values can be modified by echoing a value to these variables. If you plan on modifying the socket receive buffer size via a C command then the largest value you can set will be less than or equal to what is specified in the rmem_max variable. So if you require a very large value, you must modify the kernel maximum value rmem_max, then modify the value of your socket.
Read Current Socket Receive Buffer Size
To read your current socket receive buffer size is quite easy in C, its just a matter of using the correct function with the correct set of arguments. The function used to get socket information is known as getsockopt, which takes the arguments:
int getsockopt(int sockfd, int level, int optname, void *optval,
socklen_t *optlen);
The first argument is your socket, the second argument is the API level, optname is the socket option (SO_RCVBUF), optval is value read back from the socket stating the receive buffer size, and optlen is the is the size of the buffer point to by optval. So lets put this into practice:
unsigned int m;
int n;
m = sizeof(n);
getsockopt(socket, SOL_SOCKET, SO_RCVBUF, (void *)&n, &m);
fprintf(stdout, "The socket receive buffer size is: %u\n", n);
The integer n contains the returned buffer size. You will notice the option name used SO_RCVBUF. This is outlined in the socket manual page, among other read/set values available. The manual page output for SO_RCVBUF is:
SO_RCVBUF
Sets or gets the maximum socket receive 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/rmem_default file, and the maximum allowed value is set by the /proc/sys/net/core/rmem_max file.
Now that we can read the value, lets increase the maximum size and set a new value.
Set Socket Receive Buffer Size
To increase the socket buffer size for receives we utilize the setsockopt function.
echo 2097152 > /proc/sys/net/core/rmem_max
Then in C we can now set our socket to a value half that size…lets not break the bank.
int setsockopt(int sockfd, int level, int optname, cont void *optval,
socklen_t optlen);
The arguments for the function are nearly identical. The optval is now a const void, and the socklen_t now takes the actual variable rather than address of. To set a size:
int buff_size;
// In bytes
buff_size = 1048576;
setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buff_size, (int)sizeof(buff_size));
As mentioned earlier this function will set the value to less than or equal to the maximum size set within the kernel variable (rmem_max). There are also other values that can be set or modified in kernel. Have fun tweaking these values to get better performance.
Comments