Demystifying the Linux Kernel Socket File Systems (Sockfs)
来源:互联网 发布:淘宝买红木家具可靠吗 编辑:程序博客网 时间:2024/06/05 15:29
All Linux networking works with System Calls creating network sockets (using theSocket System Call). The Socket System Call returns an integer (socket descriptor).
“Writing” or “reading” 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 file-system descriptor would have been created by the “Open” system call IF … the descriptor were a “regular” file-system descriptor, intended for “regular” / file-system writes and reads (via System Callswrite/read respectively) to files etc.
Further Note: This implies that the network socket descriptor created by the “socket” System Call will be used by systems programmer to write/read , using the same System Calls write/read used for “regular” file system writes/reads (System Calls that would, under normal and other circumstances, write/read data to/from memory).
Further further Note: A System Call “write” (to the descriptor that was created by thesocket System Call) must translate “magically” into a TCP transaction that “writes” the data across the network(ostensibly to the client on the other end), with the data “written” encapsulated within the payload section of a TCP packet.
This process of adapting and hijacking the kernel file-system infrastructure to incorporate network operations /socket operations is called SOCKFS (Socket File System).
So how does the linux kernel accomplish this process, where a file-system write is “faked” into a network-system “write”, if indeed it can be called that ?
Well…as is usually the case, the linux kernel’s methods begins at System / Kernel Initialization, when a special socket file-system (statically defined sock_fs_type) for networks is “registered” byregister_file_system. This happens in sock_init. File systems are registered so that disk partitions can be mounted for that file system.
The kernel registered file system type sock_fs_type so that it could create a fake mount point using kern_mount(for the file system sock_fs_type). This mount point is necessary if the kernel is to later create a “fake file” *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.
Note: No “actual” mount point exists, not in the sense an inode etc etc.
We will blog on file systems later.
Then when the socket System Call is initiated (to create the socket descriptor), the kernel executes sock_create to create a new descriptor (aka thesocket descriptor). The kernel also executes sock_map_fd, which creates a “fake file” , and assigns the “fake file” to thesocket descriptor. The “fake” files ops ( file->f_op) are then initialized to besocket_file_ops (statically defined at compile time in source/net/socket.c).
The kernel assigns/maps the socket descriptor created earlier to the new “fake” file usingfd_install.
This socket descriptor is returned by the Socket System Call (as required by the MAN page of theSocket System Call) to the user program.
I only call it “fake” file because a System Call write executed against thatsocket descriptor 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 f_op‘s assigned to the “fake” file (socket_file_ops).
The kernel is now set up to create network traffic when System Calls write/read are executed to/from to the “fake” file descriptor (thesocket descriptor) which was returned to the user when System Callsocket was executed.
In point of fact, a System Call write to the “fake” files socket descriptor will then translate into a call to __sock_sendmsgwithin the kernel, instead of a write into the “regular” file system. Because that is howsocket_file_ops is statically defined before assignment to the “fake” file.
And then we are into networking space. And the promised Lan of milk, honey, TCP traffic, SOCKFS and File Systems.
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).
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
- Demystifying the Linux Kernel Socket File Systems (Sockfs)
- Demystifying the Linux Kernel Socket File Systems (Sockfs)
- sockfs: the most simple file system in linux kernel
- Linux File Systems
- The Linux Programming Interface 14 File Systems 文件系统
- linux内核编译过程及配置说明解释(7)--Firmware Drivers,File systems,Kernel hacking
- Embedded Linux: Using Compressed File Systems
- The Linux Kernel API
- The Linux Kernel HOWTO
- The Linux Kernel HOWTO
- Understanding the Linux Kernel
- The Linux kernel
- The Linux Kernel Makefile
- understanding the linux kernel
- The Linux Kernel API
- The Linux Kernel API
- The Linux Kernel API
- The Linux Kernel API
- python 中文乱码问题
- 对信息熵的再认识
- CSDN 赚积分方法
- php.ini extension”模块
- jBPM4.3 一个请假例子 web
- Demystifying the Linux Kernel Socket File Systems (Sockfs)
- 多个cloudstack环境公用一个NFS服务器
- Mac 基本命令
- Div的宽度与高度的100%设定
- Apache axis2 + Eclipse 开发 WebService
- Berg Soft Next Suite 5 10.2013 for Delphi 7 - XE5 Full Source
- OCP-1Z0-053-V13.02-709题
- Hibernate hbm.xml配置
- C++ typedef struct 和 struct