Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are SO_SNDBUF and SO_RCVBUF

Can you explain me what exactly are SO_SNDBUF and SO_RCVBUF options?

OK, for some reason the OS buffers the outgoing/incomming data but I'd like to clarify this subject.

  • What is their role (generally)?
  • Are they per-socket buffers?
  • Is there a connection between Transport layer's buffers (the TCP buffer, for example) and these buffers?
  • Do they have a different behaviour/role when using stream sockets (TCP) and when using connectionless sockets (UDP)?

A good article will be great too.

I googled it but didn't find any useful information.

like image 587
Hex Avatar asked Nov 23 '10 15:11

Hex


People also ask

What is So_sndbuf?

SO_SNDBUF. public static final SocketOption<Integer> SO_SNDBUF. The size of the socket send buffer. The value of this socket option is an Integer that is the size of the socket send buffer in bytes. The socket send buffer is an output buffer used by the networking implementation.

What is So_reuseport?

Like SO_REUSEADDR, SO_REUSEPORT allows multiple sockets to bind to the same address and port combination. This socket option is fairly recent, with its use beginning from the Linux kernel version 3.9. The rule is that each socket binding to the address and port should also enable the socket option, SO_REUSEPORT.

What is So_priority?

SO_PRIORITY Set the protocol-defined priority for all packets to be sent on this socket. Linux uses this value to order the networking queues: packets with a higher priority may be processed first depending on the selected device queueing discipline.


1 Answers

The "SO_" prefix is for "socket option", so yes, these are per-socket settings for the per-socket buffers. There are usually system-wide defaults and maximum values.

SO_RCVBUF is simpler to understand: it is the size of the buffer the kernel allocates to hold the data arriving into the given socket during the time between it arrives over the network and when it is read by the program that owns this socket. With TCP, if data arrives and you aren't reading it, the buffer will fill up, and the sender will be told to slow down (using TCP window adjustment mechanism). For UDP, once the buffer is full, new packets will just be discarded.

SO_SNDBUF, I think, only matters for TCP (in UDP, whatever you send goes directly out to the network). For TCP, you could fill the buffer either if the remote side isn't reading (so that remote buffer becomes full, then TCP communicates this fact to your kernel, and your kernel stops sending data, instead accumulating it in the local buffer until it fills up). Or it could fill up if there is a network problem, and the kernel isn't getting acknowledgements for the data it sends. It will then slow down sending data on the network until, eventually, the outgoing buffer fills up. If so, future write() calls to this socket by the application will block (or return EAGAIN if you've set the O_NONBLOCK option).

This all is best described in the Unix Network Programming book.

like image 137
DS. Avatar answered Oct 14 '22 10:10

DS.