Tuesday, November 25, 2008

Reading TCP kernel parameters using Python

There are lots of interesting TCP parameters that are kept in kernel but most of them are not sent on the network. One of the values is snd_cwnd, sender size congestion window, which dicates how much data a TCP host is willing to send. They are dynamic values and they change during the lifetime of a socket.
There are total of 31 values (as of Ubuntu 2.6.20-16). I was working on a proof of concept project to validate TCP_DELAYED_ACK problem can be solved by double buffer write, as explained here.
This is an attempt to read TCP Kernel parameters using Python.
I could not get this running on a Windows computer though.



from socket import *
import struct

tcp_info = sock.getsockopt(SOL_TCP, TCP_INFO,
struct.calcsize('BBBBBBBLLLLLLLLLLLLLLLLLLLLL'))
print struct.unpack('BBBBBBBLLLLLLLLLLLLLLLLLLLLL', tcp_info)


Python Code Snipper that read TCP kernel parameters of a socket




struct tcp_info {


__u8 tcpi_state;
__u8 tcpi_ca_state;
__u8 tcpi_retransmits;
__u8 tcpi_probes;
__u8 tcpi_backoff;
__u8 tcpi_options;
__u8 tcpi_snd_wscale : 4, tcpi_rcv_wscale : 4;



__u32 tcpi_rto;
__u32 tcpi_ato;
__u32 tcpi_snd_mss;
__u32 tcpi_rcv_mss;



__u32 tcpi_unacked;
__u32 tcpi_sacked;
__u32 tcpi_lost;
__u32 tcpi_retrans;
__u32 tcpi_fackets;



/* Times. */
__u32 tcpi_last_data_sent;
__u32 tcpi_last_ack_sent; /* Not remembered, sorry. */
__u32 tcpi_last_data_recv;
__u32 tcpi_last_ack_recv;



/* Metrics. */
__u32 tcpi_pmtu;
__u32 tcpi_rcv_ssthresh;
__u32 tcpi_rtt;
__u32 tcpi_rttvar;
__u32 tcpi_snd_ssthresh;
__u32 tcpi_snd_cwnd;
__u32 tcpi_advmss;
__u32 tcpi_reordering;



__u32 tcpi_rcv_rtt;
__u32 tcpi_rcv_space;

__u32 tcpi_total_retrans;
};


tcp.h

Post a Comment