linux-can / socketcand

Server to access CAN sockets over ASCII protocol
165 stars 41 forks source link

Reduce latency caused by TCP delayed ACK #28

Closed faisal-shah closed 7 months ago

faisal-shah commented 9 months ago

Default in linux is ~40ms. This can and does introduce latency in the data stream. Protocols sensitive to this type of timing result in timeouts.

Setting TCP_QUICKACK in sock options disables the delayed ACK. The catch is that it resets itself after every send/recv - so it needs to be reenabled upon each call.

Related: https://github.com/hardbyte/python-can/issues/1684

faisal-shah commented 9 months ago

Sorry, just noticed the formatting issues, let me fix and force push

hartkopp commented 8 months ago

https://github.com/hardbyte/python-can/pull/1683#issuecomment-1779773192

faisal-shah commented 8 months ago

Thanks for your comment.

So, how would you like me to approach this. Shall I add a command line option to socketcand? If so, you want something high level like 'low latency', or specific like 'set TCP_QUICKACK on socket' ?

hartkopp commented 8 months ago

Yes, a command line option something like: [-t| --tune-tcp] -t (enable TCP_NODELAY and TCP_QUICKACK socket options)

And maybe you can also create a (global) variable to simplify the sockopt() calls like:

if (tune_tcp)
    setsockopt(client_socket, IPPROTO_TCP, TCP_QUICKACK, &tune_tcp, sizeof(tune_tcp));

Many thanks!

faisal-shah commented 8 months ago

Cool, will do. No delay is set by default currently. Keep it that way? Your description of the option is slightly imprecise. Maybe something like enable quickack in addition to nodelay?

hartkopp commented 8 months ago

Ugh! Sorry. I wasn't aware that TCP_NODELAY is already enabled in socketcand.

Then I would suggest to only add TCP_QUICKACK depending on the command line.

[-q | --quick-ack]
-q (enable TCP_QUICKACK socket option)
faisal-shah commented 8 months ago

I messed up the push, ignore, will fix and notify when ready.

faisal-shah commented 8 months ago

Ok, I believe the patch is ready to go. Please review.

I took the liberty to remove some superfluous tab characters as well.

Tried to insert the option where it made sense, and re-wrapped the usage help. Looks like this on my terminal: image

marckleinebudde commented 8 months ago

That's a lot of code duplication. What about adding a function like this:

void tcp_quickack(int s)
{
    int i = 1;

    if (tcp_quickack_flag)
        setsockopt(s, IPPROTO_TCP, TCP_QUICKACK, &i, sizeof(int));
}
marckleinebudde commented 8 months ago

looks good, please squash both patches and force push

faisal-shah commented 8 months ago

Let me know if anything else needs to be done. Thanks!

faisal-shah commented 7 months ago

Sorry, not familiar with the github review feature. I went ahead and submitted another commit to address the code review changes (unnecessary includes).

marckleinebudde commented 7 months ago

Look good to me. Please squash both patches into one and force push the branch.

faisal-shah commented 7 months ago

Squashed.