Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine TCP payload activity/statistics

I'd like to lookup a counter of the TCP payload activity (total bytes received) either for a given file descriptor or a given interface. Preferably the given file descriptor, but for the interface would be sufficient. Ideally I'd really like to know about any bytes that have been ack-ed, even ones which I have not read into userspace (yet?).

I've seen the TCP_INFO feature of getsockopt() but none of the fields appear to store "Total bytes received" or "total bytes transmitted (acked, e.g.)" so far as I can tell.

I've also seen the netlink IFLA_STATS+RTNL_TC_BYTES and the SIOCETHTOOL+ETHTOOL_GSTATS ioctl() (rx_bytes field) for the interfaces, and those are great, but I don't think they'll be able to discriminate between the overhead/headers of the other layers and the actual payload bytes.

procfs has /proc/net/tcp but this doesn't seem to contain what I'm looking for either.

Is there any way to get this particular data?

EDIT: promiscuous mode has an unbearable impact on throughput, so I can't leverage anything that uses it. Not to mention that implementing large parts of the IP stack to determine which packets are appropriate is beyond my intended scope for this solution.

The goal is to have an overarching/no-trust/second-guess of what values I store from recvmsg().

The Right Thing™ to do is to keep track of those values correctly, but it would be valuable to have a simple "Hey OS? How many bytes have I really received on this socket?"

like image 454
Brian Cain Avatar asked Aug 22 '13 15:08

Brian Cain


1 Answers

One could also use ioctl call with SIOCINQ to get the amount of queued unread data in the receive buffer. Here is usage from the man page: http://man7.org/linux/man-pages/man7/tcp.7.html

int value;
error = ioctl(tcp_socket_fd, SIOCINQ, &value);

For interface TCP stats, we can use " netstat -i -p tcp" to find stats on a per-interface basis.

like image 109
Manoj Pandey Avatar answered Oct 03 '22 22:10

Manoj Pandey