Why does changing the value of SO_RCVBUF not work?

I am creating a program that creates a RAW socket to read all traffic. Between calling socket () and recvfrom () (the last one is in a loop to get all packets out of the buffer). I wait 5 seconds.

When I run the program, I send about 200 packets with the hping3 command to "faster mode" (to quickly fill the buffer) in my program. As soon as 5 seconds have passed, my program will extract about 150 packets from the buffer.

I am trying to resize the receive buffer to get the best result:

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

However, regardless of the value of "a", 1 or 10,000,000, nothing seems to change, I still get ~ 150 packets from the buffer.

What is the problem?

Edit: The value of "a" is checked when getsockopt called.

+6
source share
2 answers

The level argument for setsockopt must 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)); } 
+7
source

You may also be limited by the OS if it still does not work. Check the values ​​in:

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

If this is 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 in these files, they will show you the current settings. To change them forever, use sysctl. It is a good idea to write these settings before you begin to change. Here is a great tutorial on making these changes: http://fasterdata.es.net/fasterdata/host-tuning/linux/ .

+13
source

Source: https://habr.com/ru/post/912689/


All Articles