<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LinuxEco &#187; TCP</title>
	<atom:link href="http://linuxeco.com/?feed=rss2&#038;tag=tcp" rel="self" type="application/rss+xml" />
	<link>http://linuxeco.com</link>
	<description>Linux Ecosystems, Linux Kernel Training, Kernel Education and Information Exchange, Linux Training</description>
	<lastBuildDate>Fri, 03 Aug 2012 18:36:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Demystifying the Linux Kernel Socket File Systems (Sockfs)</title>
		<link>http://linuxeco.com/?p=1</link>
		<comments>http://linuxeco.com/?p=1#comments</comments>
		<pubDate>Mon, 09 Jan 2012 01:49:57 +0000</pubDate>
		<dc:creator>Anand</dc:creator>
				<category><![CDATA[Linux Networking]]></category>
		<category><![CDATA[Linux Packet Transmission and Reception]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Linux Kernel Sockfs]]></category>
		<category><![CDATA[register_file_system]]></category>
		<category><![CDATA[sockfs]]></category>
		<category><![CDATA[System Call]]></category>
		<category><![CDATA[TCP]]></category>
		<category><![CDATA[Virtual File System VFS]]></category>

		<guid isPermaLink="false">http://linuxeco.com/?p=1</guid>
		<description><![CDATA[All Linux  networking works with System Calls creating network sockets (using the Socket System Call). The Socket System Call returns an integer (socket descriptor). &#8220;Writing&#8221; or &#8220;reading&#8221; to/from that socket descriptor (as though it were a file) using generic System Calls  write / read respectively creates TCP network traffic rather than file-system writes/reads. Note: The [...]]]></description>
			<content:encoded><![CDATA[<p>All Linux  networking works with System Calls creating network sockets (using the <strong>Socket</strong> System Call). The <strong>Socket</strong> System Call returns an integer (<strong>socket descriptor</strong>).</p>
<p>&#8220;Writing&#8221; or &#8220;reading&#8221; to/from that s<strong>ocket descriptor</strong> <strong></strong> (as though it were a file) using generic System Calls  <strong>write</strong> /<strong> read</strong> respectively <strong></strong>creates TCP network traffic rather than file-system writes/reads.</p>
<p><em>Note:</em> The file-system descriptor would have been created by the &#8220;<strong>Open</strong>&#8221; system call IF &#8230; the descriptor were a &#8220;regular&#8221; file-system descriptor, intended for &#8220;regular&#8221; / file-system writes and reads (via System Calls <strong>write</strong>/<strong>read</strong> respectively) to files etc.</p>
<p><em>Further Note:</em> This implies that the network <strong>socket descriptor</strong> created by the &#8220;<strong>socket</strong>&#8221; System Call will be used by systems programmer to write/read , using the same System Calls<strong> write/read</strong> used for &#8220;regular&#8221; file system writes/reads (System Calls that would, under normal and other circumstances, write/read data to/from memory).</p>
<p><em>Further further Note:</em>  A System Call  &#8220;<strong>write</strong>&#8221; (to the descriptor that was created by the <strong>socket</strong> System Call)  must translate &#8220;magically&#8221; into a <strong>TCP transaction that &#8220;writes&#8221; the data across the network </strong>(ostensibly to the client on the other end)<strong>, with the data &#8220;written&#8221; encapsulated within the payload section of a TCP packet</strong>.</p>
<p>This process of adapting  and hijacking the kernel file-system infrastructure to incorporate network operations /socket operations is called SOCKFS (Socket File System).</p>
<p>So how does  the linux kernel accomplish this process, where a file-system write is &#8220;faked&#8221; into a network-system &#8220;write&#8221;, if indeed it can be called that ?</p>
<p>Well&#8230;as is usually the case, the linux kernel&#8217;s methods begins at <strong>System / Kernel Initialization</strong>, when a special socket file-system (statically defined<strong> sock_fs_type</strong>)  for networks is &#8220;registered&#8221; by <strong>register_file_system. </strong>This happens in<strong> sock_init. </strong>File systems are registered so that disk partitions can be mounted for that file system.<strong><br />
</strong></p>
<p style="text-align: left;">The kernel registered file system type <strong>sock_fs_type</strong>  so that it could create a fake mount point  using<strong> kern_mount </strong>(for the file system<strong> sock_fs_type).  </strong>This mount point is necessary if the kernel is to later create a &#8220;fake file&#8221;   *struct file  using  existing/generic mechanisms and infrastructure  made available for the Virtual File System (VFS). These mechanisms  and infrastructure would include a mount point being available.</p>
<p style="text-align: left;">         Note:  No &#8220;actual&#8221; mount point exists, not in the sense an inode etc etc.</p>
<p style="text-align: left;">                       We will blog on file systems later.</p>
<p>Then when the <strong>socket</strong> System Call is initiated (to create the <strong>socket descriptor</strong>),  the kernel executes <strong>sock_create</strong> to create a new descriptor (aka the <strong>socket descriptor</strong>). The kernel also  executes <strong>sock_map_fd</strong>, which creates a   &#8220;fake file&#8221; , and  assigns the &#8220;fake file&#8221; to the <strong>socket descriptor</strong>. The &#8220;fake&#8221; files ops ( <strong>file-&gt;f_op</strong>) are then initialized to be <strong> socket_file_ops</strong>  (statically defined at compile time in source/net/socket.c).</p>
<p>The kernel assigns/maps the <strong>socket descriptor</strong> created earlier to the new &#8220;fake&#8221;  file using <strong>fd_install</strong>.</p>
<p>This <strong>socket descriptor</strong> is returned by the <strong>Socket</strong> System Call<strong></strong> (as required by the MAN page of the <strong>Socket</strong> System Call) to the user program.</p>
<p>I only call it &#8220;fake&#8221; file because a System Call <strong>write</strong> executed against that <strong>socket descriptor</strong> will use the VFS infrastructure created, but  the data will not be written into a disk-file anywhere. It will, instead, be translated into a network operation because of the<strong> f_op</strong>&#8216;s assigned to the &#8220;fake&#8221; file (<strong>socket_file_ops</strong>).<strong><br />
</strong></p>
<p>The kernel is now set up to create network traffic when System Calls <strong>write/read</strong>  are executed to/from to the &#8220;fake&#8221; file descriptor (the <strong>socket descriptor) </strong> which was returned to the user when System Call <strong>socket</strong> was executed.</p>
<p>In point of fact, a System Call <strong>write</strong> to the &#8220;fake&#8221; files <strong>socket descriptor</strong> will then translate into a call to  <strong>__sock_sendmsg </strong>within the kernel, instead of a write into the &#8220;regular&#8221; file system. Because that is how <strong>socket_file_ops</strong> is statically defined before assignment to the &#8220;fake&#8221; file.</p>
<p>And then we are into networking space. And the promised Lan of milk, honey,  TCP traffic,  SOCKFS and File Systems.</p>
<p>No one said understanding the kernel was easy. But extremely gratification awaits those that work on it. And also creates enormous opportunities for innovation.  I  explain Linux Kernel concepts and more in my classes ( Advanced Linux Kernel Programming @UCSC-Extension, and also in other classes that I teach independently).</p>
<p>As always, Feedback, Questions  and Comments are appreciated and will be responded to. I will like to listen to gripes, especially  if you also paypal me some.  Thanks</p>
<p>-Anand</p>
]]></content:encoded>
			<wfw:commentRss>http://linuxeco.com/?feed=rss2&#038;p=1</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
