4

When calling setsockopt with SO_RECVBUF, then turning around and calling getsockopt with SO_RECVBUF, it appears to be telling me that it sets the buffer size to twice what I requested it to be set to. Anybody know why that may be?

code in question:

https://gist.github.com/rdp/8443238

output:

setting it as 2222
[udp @ 0x1a72ec0] end receive buffer size reported is 4444

Only in linux, on other OS's seem to report it as the value I set it to. Thank you.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • Possible duplicate of [Understanding set/getsockopt SO\_SNDBUF](https://stackoverflow.com/questions/2031109/understanding-set-getsockopt-so-sndbuf) – rogerdpack Oct 17 '17 at 17:55

2 Answers2

8

Excerpt from Linux man page for socket

SO_SNDBUF

Sets or gets the maximum socket send 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/wmem_default file and the maximum allowed value is set by the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for this option is 2048.

So, kernel doubles the value you set for it's internal purpose.

Khaled
  • 670
  • 6
  • 18
0

The platform can adjust the value you supply up or down. See the man page. You were below the platform minimum.

2222 is far too low. It should be more like 32k.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • If I set it to 32K, it reports it as 64K. If it set it as 64K, it reports it as 128K...so I'm thinking it may not be that the value is too low here...thanks! – rogerdpack Jan 15 '14 at 21:21
  • So it is doubling whatever you set it to. The platform can adjust the value you supply up or down. See the 'man' page. – user207421 Jan 16 '14 at 10:12
  • Yes, so my question still remains as to "why" Linux doubles it, when other OS's don't. What's the rationale behind it preferring to double the requested value, basically. – rogerdpack Jan 16 '14 at 17:48