Set interface Learning (1) implementation

Source: Internet
Author: User
Tags function prototype

The concept of set interface was first introduced by 4.2bsd (1983). Now it has become a universal network application programming interface, which is supported by all operating systems. The set of interface layer is located between the application and the protocol stack, shielding the application from the specific implementation details related to the Protocol.

Generally, database functions are called in applications, and database functions enter the set interface layer through system calls. The set interface layer of Linux provides a set of special set interface system calls, add the "sys _" prefix to the corresponding library function name. In addition, to reflect the idea that everything is a file, Linux also allows the standard I/O system to call a set of interface file descriptors to read and write network connections on the corresponding set of interfaces, just like accessing an opened normal file through a file descriptor. When a set of interfaces is created, they are bound to a file and a file descriptor. After that, all operations on the set of interfaces are performed through the file descriptor they are bound, this includes a special set of interface system calls. The page includes the operations of standard I/O system calls on the set interface, and the file type bound to the set interface is set interface file.


The call to the interface layer involves the following files:

Include/Linux/net. h defines the structure, Macro, and function prototype related to the interface layer.

Include/NET/sock. h defines the basic transmission control block structure, Macro, and function prototype

Net/socket. C to implement system calls at the Interface Layer

Net/IPv4/af_inet.c network layer and transport layer Interfaces

Socket Structure

struct socket {socket_statestate;kmemcheck_bitfield_begin(type);shorttype;kmemcheck_bitfield_end(type);unsigned longflags;/* * Please keep fasync_list & wait fields in the same cache line */struct fasync_struct*fasync_list;wait_queue_head_twait;struct file*file;struct sock*sk;const struct proto_ops*ops;};
State

Indicates the status flag of the set of interfaces. This flag indicates that some statuses only make sense for TCP interfaces, because only TCP is a connection-oriented protocol.

Typedef Enum {
Ss_free = 0,/* not allocated */
Ss_unconnected,/* unconnected to any socket */
Ss_connecting,/* in process of connecting */
Ss_connected,/* connected to socket */
Ss_disconnecting/* in process of disconnecting */
} Socket_state;

Type

Set Interface Type

Enum sock_type {
Sock_stream = 1, connection-based interface
Sock_dgram = 2, a datagram-based interface
Sock_raw = 3, original Interface
Sock_rdm = 4, reliable packet transmission interface
Sock_seqpacket = 5, sequential grouping Interface
Sock_dccp = 6, Datagram congestion control protocol set Interface
Sock_packet = 10, Mixed Mode Interface
};

Flags

A group of flag Spaces

# Define sock_async_nospace0 whether the sending queue of this interface is full
# Define sock_async_waitdata 1 whether the application is waiting for receiving data when calling the Recv
# Define sock_nospace 2 whether the sending queue of this interface is full in non-asynchronous mode
# Define sock_passcred 3 whether the sock_passcred option is set
# Define sock_passsec 4 whether sock_passsec is set

Struct fasync_struct * fasync_list

Queue with asynchronous notifications

Wait_queue_head_t wait

Process queue waiting for this interface

Struct file * File

Pointer to the file structure bound to this interface

Struct sock * SK;

The transmission control block associated with this interface

Const struct proto_ops * OPS;

It is used to map system calls at the interface layer to the corresponding transport layer protocol implementation.

The pf_inet protocol family defines three prote_ops structure instances.

TCP inet_stream_ops

UDP inet_dgram_ops

Raw inet_sockraw_ops

Proto_ops Structure

The entire proto_ops structure can be seen as a jump table from an interface system call to a function at the transport layer. Some operations will continue to jump to the table through the proto structure, access the specific transport layer or network layer.

Since the proto_ops structure completes the transfer from the Protocol-independent interface layer to the protocol-related transport layer, and the proto structure maps the transport layer to the network layer, you can imagine that each transport layer protocol needs to define a specific proto_ops instance and proto instance. In an IPv4 protocol family, a transport layer protocol corresponds to an inet_protosw structure. The inet_protosw structure includes the proto_ops structure and the proto structure.


Interface File System

Each file has its own file type. For example, a device file includes a character device and a block device file. The file type associated with the set interface is a set interface file.

Interface file system type

To associate a set of interfaces with file descriptors and support the allocation and release of I nodes on the special set of interface layers, the sockfs File System sock_fs_type is added to the system, the getsb interface of the sockfs file system and alloc_inode and destroy_inode in the super block operation set can be used to allocate and release I nodes related to the interface file. You can view the file systems supported by the operating system in the/proc/filesystems file.

static const struct super_operations sockfs_ops = {.alloc_inode =sock_alloc_inode,.destroy_inode =sock_destroy_inode,.statfs =simple_statfs,};static int sockfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data, struct vfsmount *mnt){return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,     mnt);}static struct vfsmount *sock_mnt __read_mostly;static struct file_system_type sock_fs_type = {.name ="sockfs",.get_sb =sockfs_get_sb,.kill_sb =kill_anon_super,};

Inode of the set interface file

The I nodes of the interface file system correspond to those of the interface. Therefore, the I nodes of the interface file system are allocated in a special way. They are not simply I nodes, it is the combination of the I node and Socket Structure, that is, the socket_alloc structure. This allows the allocation of the Set interface and the allocation of the Set interface file I node bound to it. In the application layer, you must use the file descriptor to quickly locate the bound set interface.

struct socket_alloc {struct socket socket;struct inode vfs_inode;};static struct inode *sock_alloc_inode(struct super_block *sb){struct socket_alloc *ei;ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);if (!ei)return NULL;init_waitqueue_head(&ei->socket.wait);ei->socket.fasync_list = NULL;ei->socket.state = SS_UNCONNECTED;ei->socket.flags = 0;ei->socket.ops = NULL;ei->socket.sk = NULL;ei->socket.file = NULL;return &ei->vfs_inode;}static void sock_destroy_inode(struct inode *inode){kmem_cache_free(sock_inode_cachep,container_of(inode, struct socket_alloc, vfs_inode));}

Set interface file

The set of interfaces has a set of independent system calls, including the establishment of a set of interfaces, connections and IO operations, because after the set of interfaces are created, the file descriptor is returned, therefore, you can also read and write the set interface through standard file IO operations, such as sending data using send (). In fact, the same effect can be achieved through the Write System Call. This is because when you create a set of interface files, the f_op in the file structure directs to socket_file_ops. Through socket_file_ops, you can see that the set of interface files support those system calls.

static const 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,#ifdef CONFIG_COMPAT.compat_ioctl = compat_sock_ioctl,#endif.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,.splice_read =sock_splice_read,};

Bind the set interface file to the set Interface

The Application Layer accesses the set interface through the file descriptor. Therefore, when the socket system is called to create the set interface, sock_map_fd () is called after creation to bind the set interface and file descriptor.

static int sock_attach_fd(struct socket *sock, struct file *file, int flags){struct dentry *dentry;struct qstr name = { .name = "" };dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);if (unlikely(!dentry))return -ENOMEM;dentry->d_op = &sockfs_dentry_operations;/* * We dont want to push this dentry into global dentry hash table. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED * This permits a working /proc/$pid/fd/XXX on sockets */dentry->d_flags &= ~DCACHE_UNHASHED;d_instantiate(dentry, SOCK_INODE(sock));sock->file = file;init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,  &socket_file_ops);SOCK_INODE(sock)->i_fop = &socket_file_ops;file->f_flags = O_RDWR | (flags & O_NONBLOCK);file->f_pos = 0;file->private_data = sock;return 0;}int sock_map_fd(struct socket *sock, int flags){struct file *newfile;int fd = sock_alloc_fd(&newfile, flags);if (likely(fd >= 0)) {int err = sock_attach_fd(sock, newfile, flags);if (unlikely(err < 0)) {put_filp(newfile);put_unused_fd(fd);return err;}fd_install(fd, newfile);}return fd;}

Process, file descriptor, and set Interface

In the task_struct structure, files points to the file_struct structure. The main function of this structure is to manage the descriptor pointed to by the fd_array pointer array. Each file structure describes an open file.

Interface Layer Initialization

static int __init sock_init(void){/* *      Initialize sock SLAB cache. */sk_init();/* *      Initialize skbuff SLAB cache */skb_init();/* *      Initialize the protocols module. */init_inodecache();register_filesystem(&sock_fs_type);sock_mnt = kern_mount(&sock_fs_type);/* The real protocol initialization is performed in later initcalls. */#ifdef CONFIG_NETFILTERnetfilter_init();#endifreturn 0;}

Set interface Learning (1) implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.