rawsocket sendto() some of the packet are dropped

2019-05-12 18:17发布

问题:

socketFd_ = socket(AF_INET, SOCK_RAW, protoType);    
sentBytes = sendto(socketFd_, buf, len, 0, 
                  (struct sockaddr   *)&sa,sizeof(structsockaddr_in));
protoType = GRE

I am sending the 1000 packets in the network. If my tx packet rate is 40, i am able to see all the packet in wireshark. however when i will try to send at the rate of 100 some of the packet(3-4) will not reach in the network however sendto did not return any error. i know sendto will just put the txpacket into the queue and will not guarantee the delivery of packet in the network however from where i can get the drop packet statistics and reason for packet drop in the kernel. i have tried increasing the txqueuelen of interface to 65000 but it did not helped. how can i debug this issue?.

回答1:

You are correct that sendto will just put the txpacket in the queue and not guarantee delivery.

From the iEEE documentation on sendto:

Successful completion of a call to sendto() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.

You have to perform some of your own throttling via ioctl() and getsockopt() as you are almost doing. I don't think you can expect the network stack to do it all for you.

Something like:

sendMsgThrottled( msg Msg ) {
    ioctl(socketFd_, SIOCOUTQ, &outstandingBytes);  
    if ( outStandingBytes < limit )
          sendto(socketFd_, ..)
    else
          queueMsg();  /* Or delay */
}

I would test this with by having the send and receiver on the same machine, packet loss can come from other places besides the network layer of the operating system.