jbaublitz / neli

Rust type safe netlink library
BSD 3-Clause "New" or "Revised" License
180 stars 35 forks source link

Setting socket buffer sizes #230

Open fpagliughi opened 1 year ago

fpagliughi commented 1 year ago

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.

jbaublitz commented 1 year ago

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?

fpagliughi commented 11 months ago

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.