7

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?

El Sampsa
  • 1,673
  • 3
  • 17
  • 33
  • 1
    RCVBUF has not much to do with the maximum size of a UDP packet, but mainly how much data get buffered before they get discarded if the application is not fast enough to read them. Thus it is a socket property and not related to UDP. The max size of an UDP packet is 64k. – Steffen Ullrich Feb 17 '15 at 16:20
  • Thanks .. well, uh.. that is exactly what I wanted to state. I wanted to say, how to find out / adjust the size of the buffer – El Sampsa Feb 17 '15 at 19:50
  • And, btw, are the network sockets FIFO ? first in - first discarded when the buffer gets saturated? – El Sampsa Feb 17 '15 at 19:52

1 Answers1

2

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.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • 1
    1024 doubled ain't 2304 – El Sampsa Feb 18 '15 at 11:25
  • Like I said, it does not need to reflect exactly what you set. Maybe it will set itself to a multiple of some kernel specific size. – Steffen Ullrich Feb 18 '15 at 14:03
  • @SteffenUllrich "But note that depending on your setting and on your OS you might get different values back with getsockopt than you set with setsockopt" -- where is the documentation for such effect? Because man page for socket(7) is misleading -- it says nothing about the value "2304". I faced the same behaviour on Linux 3.16.0-30 (64 bit) -- found no way of setting buffer size down to the documented minimum of 256. 2304 is the lowest I have got with `getsockopt`. – Oleg Andriyanov Apr 27 '15 at 14:37
  • I don't know of any specific documentation. And it might differ between OS and maybe between different OS versions, kernel parameters etc. – Steffen Ullrich Apr 27 '15 at 17:16