Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why changing value of SO_RCVBUF doesn't work?

I'm making a program which create a RAW socket in order to read all traffic. Between the call of socket() and recvfrom() (last one is in a loop to get out all packets from buffer) I wait 5s.

When I run the program, I send about 200 packets with hping3 command in « faster mode » (to fill in the buffer fastly) to my program. As soon as 5s are elapsed, my program extract about 150 packets from the buffer.

I try to change the size of the receive buffer to get better result:

int a = 65535;
if ( (setsockopt(sockfd, 0, SO_RCVBUF, &a ,sizeof(int)) ) < 0 )
{
    fprintf(stderr, "Error setting sock opts..\n");
}

However, whatever is the value of « a », 1 or 10000000, it seems nothing changes, I still get ~150 packets from the buffer.

What's the problem?

Edit: Value of « a » is verified with a getsockopt call.

like image 513
Flow Avatar asked Apr 08 '12 14:04

Flow


People also ask

What is So_sndbuf?

SO_SNDBUF int Specifies the total per-socket buffer space reserved for sends.

What is Setsockopt?

The setsockopt function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost socket level.


2 Answers

You may also be limited by the OS, if it still doesn't seem to be working. Check the values in:

/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max

If it's TCP as you say in your example, and not actually a raw socket, you can also check the values in:

/proc/sys/net/ipv4/tcp_mem

If you run cat on these files they'll show you the current settings. To change them permanently, use sysctl. It's a good idea to write these settings down before you start changing things. Here's a great tutorial on making those changes: http://fasterdata.es.net/fasterdata/host-tuning/linux/.

like image 112
rutgersmike Avatar answered Oct 31 '22 02:10

rutgersmike


The level argument to setsockopt should be SOL_SOCKET, not 0:

int a = 65535;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &a, sizeof(int)) == -1) {
    fprintf(stderr, "Error setting socket opts: %s\n", strerror(errno));
}
like image 28
Daniel Roethlisberger Avatar answered Oct 31 '22 02:10

Daniel Roethlisberger