5

Quoting form this online kernel doc

  • SO_TIMESTAMPING Generates timestamps on reception, transmission or both. Supports multiple timestamp sources, including hardware. Supports generating timestamps for stream sockets.

Linux supports TCP timestamping, and I tried to write some demo code to get any timestamp for TCP packet.

The server code as below:

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    perror("bind failed. Error");
    return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
int c = sizeof(struct sockaddr_in);

client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
    perror("accept failed");
    return 1;
}

// Note: I am trying to get software timestamp only here..
int oval = SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_SOFTWARE;
int olen = sizeof( oval );
if ( setsockopt( client_sock, SOL_SOCKET, SO_TIMESTAMPING, &oval, olen ) < 0 )
    { perror( "setsockopt TIMESTAMP"); exit(1); }

puts("Connection accepted");

char    buf[] = "----------------------------------------";
int len = strlen( buf );

struct iovec    myiov[1] = { {buf, len } };

unsigned char   cbuf[ 40 ] = { 0 };
int     clen = sizeof( cbuf ); 

struct msghdr   mymsghdr = { 0 };
mymsghdr.msg_name   = NULL;
mymsghdr.msg_namelen    = 0;
mymsghdr.msg_iov    = myiov;
mymsghdr.msg_iovlen = 1;
mymsghdr.msg_control    = cbuf;
mymsghdr.msg_controllen = clen;
mymsghdr.msg_flags  = 0;

int read_size = recvmsg( client_sock, &mymsghdr, 0);

if(read_size == 0)
{
  puts("Client disconnected");
  fflush(stdout);
}
else if(read_size == -1)
{
  perror("recv failed");
}
else
{
  struct msghdr *msgp = &mymsghdr;
  printf("msg received: %s \n",(char*)msgp->msg_iov[0].iov_base);// This line is successfully hit.
  // Additional info: print msgp->msg_controllen inside gdb is 0.
  struct cmsghdr    *cmsg;
  for ( cmsg = CMSG_FIRSTHDR( msgp );
      cmsg != NULL;
      cmsg = CMSG_NXTHDR( msgp, cmsg ) )
  {
    printf("Time GOT!\n"); // <-- This line is not hit.
    if (( cmsg->cmsg_level == SOL_SOCKET )
        &&( cmsg->cmsg_type == SO_TIMESTAMPING ))
      printf("TIME GOT2\n");// <-- of course , this line is not hit
  } 

}

Any ideas why no timestamping is available here ? Thanks

Solution I am able to get the software timestamp along with hardware timestamp using onload with solarflare NIC. Still no idea how to get software timestamp alone.

FaceBro
  • 787
  • 2
  • 13
  • 29
  • I doubt that `SO_TIMESTAMPING` is the correct value to match `cmsg_type` with. –  May 26 '17 at 12:00
  • Sorry I didn't get your point. could you please be more detailed? – FaceBro May 26 '17 at 12:02
  • Nevermind yet. There are other problems. Compile your code with all warnings on and share build output as well as program's output. –  May 26 '17 at 12:11
  • this is just some pieces of the whole server code, you can make it build able with little efforts though. – FaceBro May 26 '17 at 13:06
  • @Igor ah, I get your point of your first comment. But the issue is that there is no cmsg to match cmsg_type in the first place, no control msg is returned. – FaceBro Jun 12 '17 at 03:33
  • Facebro, did you ever get RX timestamps for TCP via the kernel? – user997112 Feb 19 '23 at 05:36

1 Answers1

2

The link you gave, in the comments at the end, says:

I've discovered why it doesn't work. SIOCGSTAMP only works for UDP
packets or RAW sockets, but does not work for TCP. – Gio Mar 17 '16 at 9:331    

it doesn't make sense to ask for timestamps for TCP, because there's
no direct correlation between arriving packets and data becoming 
available. If you really want timestamps for TCP you'll have to use 
RAW sockets and implement your own TCP stack (or use a userspace TCP
library). – ecatmur Jul 4 '16 at 10:39 
mikep
  • 3,841
  • 8
  • 21
  • I don't find the comments you mentioned at the end of the kernel document. And I actually went through the document several times,TCP timestamp is supported as explained in section 1.4. – FaceBro Jun 03 '17 at 00:04
  • I don't think there is comments at the end of the link above. Could you please tell where find the comment above ? Section 1.4 states clearly that TCP timstamp is supported though . – FaceBro Jun 05 '17 at 01:33
  • 1
    This answer comes from another SO thread https://stackoverflow.com/questions/36041740/obtain-packet-timestamp-through-ioctl-call-on-socket-file-descriptor And I highly doubt the authority of these comments. More authority needed. – FaceBro Jun 12 '17 at 03:26