Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying UDP receive buffer size at runtime in Linux

In Linux, one can specify the system's default receive buffer size for network packets, say UDP, using the following commands:

sysctl -w net.core.rmem_max=<value> sysctl -w net.core.rmem_default=<value> 

But I wonder, is it possible for an application (say, in c) to override system's defaults by specifying the receive buffer size per UDP socket in runtime?

like image 891
Mαzen Avatar asked Jan 19 '10 03:01

Mαzen


People also ask

What is UDP buffer size?

The default send buffer size for UDP sockets is 65535 bytes. The default receive buffer size for UDP sockets is 2147483647 bytes.

How increase buffer size in Linux?

Change the following set of parameters as needed: /proc/sys/net/core/rmem_default > recv > 124928 > changed to 512000. /proc/sys/net/core/wmem_default > send > 124928 > changed to 512000. /proc/sys/net/core/rmem_max > 124928 > changed to 512000.

How do you check TCP receive buffer size in Linux?

If you want see your buffer size in terminal, you can take a look at: /proc/sys/net/ipv4/tcp_rmem (for read) /proc/sys/net/ipv4/tcp_wmem (for write)


1 Answers

You can increase the value from the default, but you can't increase it beyond the maximum value. Use setsockopt to change the SO_RCVBUF option:

int n = 1024 * 1024; if (setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) == -1) {   // deal with failure, or ignore if you can live with the default size } 

Note that this is the portable solution; it should work on any POSIX platform for increasing the receive buffer size. Linux has had autotuning for a while now (since 2.6.7, and with reasonable maximum buffer sizes since 2.6.17), which automatically adjusts the receive buffer size based on load. On kernels with autotuning, it is recommended that you not set the receive buffer size using setsockopt, as that will disable the kernel's autotuning. Using setsockopt to adjust the buffer size may still be necessary on other platforms, however.

like image 79
Brian Campbell Avatar answered Sep 23 '22 01:09

Brian Campbell