<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>TCP on bramp.net</title>
    <link>https://blog.bramp.net/</link>
    <description>Recent content in TCP on bramp.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-GB</language>
    <lastBuildDate>Sat, 05 Apr 2008 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog.bramp.net/tags/tcp/" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Linux kernel patch for TCP timestamping</title>
      <link>https://blog.bramp.net/post/2008/04/05/linux-kernel-patch-for-tcp-timestamping/</link>
      <pubDate>Sat, 05 Apr 2008 00:00:00 +0000</pubDate>
      
      <guid>https://blog.bramp.net/post/2008/04/05/linux-kernel-patch-for-tcp-timestamping/</guid>
      <description><p>Recently I needed to measure the time it takes a packet to travel though the Linux network stack. To begin with I thought I’ll hack the kernel to add my own timing measurements, but then I found this was already built in! I could use the SIOCGSTAMP or SIOCGSTAMPNS ioctl calls to retrieve the timestamp assigned by the network driver when the packet first enters the stack.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-c" data-lang="c"><span class="line"><span class="cl"><span class="n">SOCKET</span> <span class="n">s</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">struct</span> <span class="n">timeval</span> <span class="n">tv</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="k">if</span> <span class="p">(</span> <span class="nf">ioctl</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">SIOCGSTAMP</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">tv</span><span class="p">)</span> <span class="p">)</span>
</span></span><span class="line"><span class="cl">   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// error
</span></span></span><span class="line"><span class="cl"><span class="nf">printf</span><span class="p">(</span> <span class="s">&#34;%ld seconds and %ld nanoseconds&#34;</span><span class="p">,</span>  <span class="n">tv</span><span class="p">.</span><span class="n">tv_sec</span><span class="p">,</span>  <span class="n">tv</span><span class="p">.</span><span class="n">tv_usec</span> <span class="p">);</span>
</span></span></code></pre></div><p>This works great for UDP packets or RAW sockets, but does not work for TCP. There are many good reasons why TCP isn’t supported, mainly because TCP is a stream protocol, whereas UDP and RAW sockets typically operate on discrete packets.</p>
<p>Ignoring this fact I decided to hack the kernel anyway to record the timestamps for TCP. It is a simple patch which hopefully others might find useful if they ever need to profile the TCP stack. It applys cleanly to 2.6.24 kernels.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-diff" data-lang="diff"><span class="line"><span class="cl"><span class="gd">--- a/linux-2.6.24/net/ipv4/tcp.c	2008-01-24 22:58:37.000000000 +0000
</span></span></span><span class="line"><span class="cl"><span class="gi">+++ b/linux-2.6.24/net/ipv4/tcp.c	2008-02-11 19:40:41.000000000 +0000
</span></span></span><span class="line"><span class="cl"><span class="gu">@@ -1180,6 +1180,10 @@
</span></span></span><span class="line"><span class="cl"> 		/* Next get a buffer. */
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl"> 		skb = skb_peek(&amp;sk-&amp;gt;sk_receive_queue);
</span></span><span class="line"><span class="cl"><span class="gi">+		
</span></span></span><span class="line"><span class="cl"><span class="gi">+		if (skb)
</span></span></span><span class="line"><span class="cl"><span class="gi">+			sock_recv_timestamp(msg, sk, skb); /* HACK */
</span></span></span><span class="line"><span class="cl"><span class="gi">+			
</span></span></span><span class="line"><span class="cl"> 		do {
</span></span><span class="line"><span class="cl"> 			if (!skb)
</span></span><span class="line"><span class="cl"> 				break;
</span></span></code></pre></div></description>
    </item>
    
  </channel>
</rss>
