neli icon indicating copy to clipboard operation
neli copied to clipboard

Setting socket buffer sizes

Open fpagliughi opened this issue 9 months ago • 2 comments

I've been looking through some C examples for some SocketCAN Netlink communications, and they all seem to set the socket buffer sizes. Like:

int sndbuf = 32768;
int rcvbuf = 1048576;

setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&sndbuf, sizeof(sndbuf));
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&rcvbuf, sizeof(rcvbuf));

Is there a way to do this easily with this library - other than getting the socket file handle and doing it manually with libc functions? If it doesn't already exist, I can give it a try and send over a PR.

fpagliughi avatar Oct 13 '23 14:10 fpagliughi

Hi @fpagliughi, sorry for the delay. I don't support this right now, but this is actually very interesting to me mainly because if we can set the buffer size to a lower value than MAX_NL_LEN, we can simplify some of the parallel buffer code a bit by only allocating buffers that are rcvbuf size. This would also eliminate an environment variable that I thought was a little bit clunky.

Does this mean that the data from .recv() is guaranteed to be less than or equal to rcvbuf?

jbaublitz avatar Nov 03 '23 17:11 jbaublitz

Honestly, I have no idea. I'm still brand new to Netlink as a whole. I was just going through a number of different C libraries and examples for the SocketCAN use of Netlink to figure out the calls, and all of them set the socket buffer length. Like here: https://github.com/lalten/libsocketcan/blob/b464485031b6f2a4e53d3ef1b3d405f9ba159c07/src/libsocketcan.c#L305-L307

My random guess was that it was a performance thing to reduce the trips across the user/kernel barrier if you were transferring large amounts of data.

fpagliughi avatar Nov 29 '23 15:11 fpagliughi