Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer size for reading UDP packets in Python

I am trying to find out / adjust the size of network buffers:

import socket

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)
212992

What on earth is this? ~ 0.2 MBytes ..!?

However, if I am looking for the buffer size elsewhere, i.e. on the command line:

sampsa@sampsa-xps13:~/python/sockets$ cat /proc/sys/net/ipv4/tcp_wmem
4096    16384   4194304

.. I get 4096 bytes.

Let's try to set the buffer size and then check its value:

sock.setsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF,1024)

sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF)     
2304

What's going on?

Substituting SOL_SOCKET with SOL_UDP gives "Protocol not available"

How can I adjust the max. size of the UDP packet .. or even find it out?

like image 572
El Sampsa Avatar asked Feb 17 '15 14:02

El Sampsa


People also ask

What is buffer size in UDP?

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

Are UDP packets buffered?

On every UDP socket, there's a “socket send buffer” that you put packets into. The Linux kernel deals with those packets and sends them out as quickly as possible. So if you have a network card that's too slow or something, it's possible that it will not be able to send the packets as fast as you put them in!


1 Answers

I wanted to say, how to find out / adjust the size of the buffer

The way you did with SO_RCVBUF was correct. But note that depending on your setting and on your OS you might get different values back with getsockopt than you set with setsockopt. On Linux socket(7) states:

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.  
The minimum (doubled) value for this option is 256.

And, btw, are the network sockets FIFO ? first in - first discarded when the buffer gets saturated?

As far as I know if the buffer is full receiving will fail. It will not discard already received but not processed data to make room for new data.

like image 169
Steffen Ullrich Avatar answered Oct 17 '22 02:10

Steffen Ullrich