Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


structure are designed to communicate with system calls. In other words, they form the link between
user-side and kernel-side sockets.

12.10.2 Sockets and Files


Userspace processes access sockets using normal file operations once a connection has been established.
How is this implemented in the kernel? Owing to theopen structure of the VFS layer (as discussed in
Chapter 8), very few actions are needed.

VFS inodes of the virtual filesystem are discussed in Chapter 8. Each socket is assigned an inode of this
type, which is, in turn, linked with the other structures associated with normal files. The functions for
manipulating files are stored in a separate pointer table:

<fs.h>
struct inode {
...
struct file_operations *i_fop; /* former ->i_op->default_file_ops */
...
}

As a result, file access to the file descriptor of a socket can be redirected transparently to the code of the
network layer. Sockets use the following file operations:

net/socket.c
struct file_operations socket_file_ops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.aio_read = sock_aio_read,
.aio_write = sock_aio_write,
.poll = sock_poll,
.unlocked_ioctl = sock_ioctl,
.compat_ioctl = compat_sock_ioctl,
.mmap = sock_mmap,
.open = sock_no_open, / special open code to disallow open via /proc /
.release = sock_close,
.fasync = sock_fasync,
.sendpage = sock_sendpage,
.splice_write = generic_splice_sendpage,
};


Thesock_functions are simple wrapper routines that invoke asock_operationsroutine as shown in the
following example ofsock_mmap:

net/socket.c
static int sock_mmap(struct file * file, struct vm_area_struct * vma)
{
struct socket *sock = file->private_data;

return sock->ops->mmap(file, sock, vma);
}
Free download pdf