Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off TCP PSH flag in the POSIX function send()?

I am sending commands to a stepper motor system using Ethernet. The commands get the motor to move, or respond with drive status, or configure the drive, etc... The stepper motor system sometimes hangs, or fails to execute the command, and the manufacturer having looked over everything I supplied has told me to turn off the PSH flag in the TCP layer.

A screenshot of Wireshark showing the use of the PSH flag by my code:

enter image description here

I am using C++11 which is running in Ubuntu (16.04).

My send function looks like so:

int sendStatusCode = send(socket , *&pointerToMessageAsCharArray , sizeOfMessage , 0);

I looked over the writeup for the function, and I understand that the last argument (which I have at 0) is a flag. What I do not understand is the value that I need to plug in to turn off the PSH flag without changing anything else (if that is even possible). Can you help?

Edit The size of the messages is quite small. for example: const char m_ME[5] = {m_HEADER_ONE, m_HEADER_TWO, m_M_HEX, m_E_HEX, m_FOOTER_HEX};

which when sent looks like so: enter image description here

The largest message I am sending is 8 chars which works out to 8 bytes.

EDIT I implemented this answer (How would one disable Nagle's algorithm in Linux?) and it looks to me that TCP_NODELAY does not turn off the PSH flag. The code I used to check:

int noDelayState = 1; // 0 means prefer bandwidth optimization over low latency

int tcpPushOptionOff = setsockopt(m_socketRight
    , IPPROTO_TCP
    , TCP_NODELAY
    ,(char *) &noDelayState
    , sizeof(int));

if (tcpPushOptionOff < 0) { /* do smth */ }
like image 581
Mike Avatar asked Mar 28 '19 17:03

Mike


People also ask

What is the PSH flag in TCP?

The PSH flag in the TCP header informs the receiving host that the data should be pushed up to the receiving application immediately.

How do you change the PSH flag?

The setting of the Push Flag is usually not controlled by the sending application, but by the sending TCP layer. Most modern TCP/IP stacks set the PSH bit at the end of the buffer supplied to send() . The PSH flag is set on the entire outgoing segment, not 'at the end of the buffer'.

What is TCP urgent flag used for?

TCP offers the ability to flag certain bytes of data as “urgent.” This feature allows an application to process and forward any data that must be dealt with immediately, without the data having to sit in the send queue for processing.

What is the difference between push and urgent flag?

The gist of the matter is that once you push data on a connection you have to wait for the receiver to get all of it before it gets to the new data. This is where the URG flag kicks in. When you send urgent data, your TCP creates a special segment in which it sets the URG flag and also the urgent pointer field.


1 Answers

With standard linux TCP stack you don't have direct control over this. Whether or not the PSH flag gets set is controlled by send buffer and copies in the stack. With something like embedded LWIP stack you have a bit more control.

However, try setting setsockopt(SO_SNDBUF) to be a very small value and see if this makes a difference. Smaller than your packet size, i.e. a few bytes Buffer size setting doesn't work, as minimum size larger than a frame is enforced by OS.

like image 121
kert Avatar answered Sep 20 '22 16:09

kert