Deep understanding of Linux Network Technology Insider--interaction between user space and kernel space

Source: Internet
Author: User
Tags network function

Overview:kernel space and user space often need to interact. For example: When user space uses some configuration commands such as Ifconfig or route, the kernel handler responds to these processing requests.
user space and the kernel have a variety of interactive methods, the most commonly used in the following four kinds: through the/proc virtual file system, through the/sys virtual file system, through the IOCTL system calls through the netlink socket. The IOCTL is most commonly used when writing programs, and two of the four ways are through the virtual file system.

Procfs and SysctlPROCFS Mount/proc sysctl mounted on/proc/sys (different from the/sys described later). (note that the Linux kernel used in the understanding Linux Network Internal is obsolete, the kernel interaction mechanisms such as PROCFS and SYSFS have changed considerably, although the code for some new kernels is given in this article.) But the main function of the introduction is still derived from description of the old kernel by the understanding Linux Network Internal. ) procfs     Procfs is a virtual file system, mounted in the/proc directory, when compiling the kernel, you can configure the kernel option make menuconfig->Filesystems Pseudo Filesystems/proc File system support to enable/disable it. It cannot be loaded as a module. PROCFS is a virtual file system, and there is no real presence on disk. But we can read, write, redirect, or even Change access rights (PROCFS mostly read-only content, the writable data is mainly in/proc/sys, see Sysctl Introduction).when the Network function module registers the initialization, it registers some files under PROCFS (Network related registration in/proc/net), which is used for the data interaction between the kernel and the user space.
the directory under/proc is created by Proc_mkdir,The file under/proc/net is passedProc_net_fops_create andProc_net_remove is created and deleted. These two functions are encapsulated asCreate_proc_entry andRemove_proc_entry. (The linux2.x version of the Linux kernel is used in the book, and I can't find these functions in the new version < The linux3.12.32> I'm using). The example of ARP is given in the book, and the new kernel is also changed, and the following gives the implementation of the new kernel Linux in/proc to create the arp file:
static const struct File_operations arp_seq_fops = {                                . Owner      = this_module,    . Open           = Arp_seq_open,    . Read           = Seq_read,    . Llseek         = Seq_lseek,    . Release    = Seq_release_net,};

static int __net_init arp_net_init (struct net *net) {    if (!proc_create ("ARP", S_irugo, Net->proc_net, &arp_ seq_fops))        Return-enomem;    

Static inline struct Proc_dir_entry *proc_create (const char *name, umode_t mode, struct proc_dir_entry *parent, const STRU CT file_operations *proc_fops) {    return Proc_create_data (name, mode, parent, Proc_fops, NULL);}


    proc_create Three parameters indicate that the file to be created is named ARP and the permission isS_irugo (read-only), the parent directory interface is NET, and the file operation handle collection isArp_seq_fops. Arp_seq_fopthe initialization is going to do another initializationArp_seq_open It defines a structureArp_seq_ops, the structure allows data from multiple routines to be returned to the user when the user requests the data and can return only one path.
static int Arp_seq_open (struct inode *inode, struct file *file) {    return seq_open_net (inode, file, &arp_seq_ops, S izeof (struct neigh_seq_state));}

static const struct Seq_operations arp_seq_ops = {    . Start  = Arp_seq_start,    . Next   = Neigh_seq_next,    . Stop   = Neigh_seq_stop,    . Show   = Arp_seq_show,};



sysctl:/proc/sys Directory     /proc/sys contains some kernel variables. These variables are readable and writable. For any of these variables, the kernel can decide to store variables, variable names, and variable permissions in the/proc/sys directory. stored inThe files and directories in the/proc/sys are created by the ctl_table structure. Ctl_table instances are registered and deleted through register_sysctl_table and unregister_sysctl_table.
struct Ctl_table {    const char *procname;       /* Text ID For/proc/sys, or zero */    void *data;    int maxlen;    umode_t mode;    struct ctl_table *child;    /* Deprecated */    Proc_handler *proc_handler;/* Callback for text formatting */    struct ctl_table_poll *poll;    void *extra1;    void *extra2;};


SysfsDue to the misuse of the /proc directory and the/proc/sys directory, there was a SYSFS to replace its file functionality.
details are also provided.

IOCTLthe IOCTL typically communicates with the kernel through the socket. InIfconfig eth0 MTU 1250as an example
struct Ifreq DATA;FD = socket (pf_inet, SOCK_DGRAM, 0);< ... initialize "data" ... >err = IOCTL (FD, SIOCSIFMTU, &d ATA);


Ifconfig first initializes the data to be passed into the kernel locally and then interacts with the kernel via the IOCTL.in the network, the IOCTL generally uses SOCK_IOCTL to search for the correct kernel handler to process the incoming kernel data.
for an example of the IOCTL see my other blog posthttp://blog.csdn.net/windeal3203/article/details/39320605

NetLink
    NetLink communication with the kernel using a standard socket
int socket (int domain, int type, int protocol)
    Where NetLink uses the new protocol family Pf_netlink (domain), and the type can only be sock_dgram. With multiple protocol, each protocol represents one or more components of a TCP/IP protocol stack. For example, Netlink_route represents the routing function of the network protocol stack and the Neighbor Discovery Protocol feature. Netlink_firewall for Firewall (netfilter).
#define Netlink_route       0   */routing/device Hook              */          #define netlink_unused      1/   * UNUSED number                */#define NETLINK_USERSOCK    2/   * Reserved for user mode socket protocols  */#define Netlink_firewall    3/   * Unused number, formerly Ip_queue     */.....

The advantage of NetLink's interaction with the kernel relative to other (IOCTL, etc.) user spaces is that with NetLink, the kernel can proactively transmit kernel data to the user, not just as a response to a user request.

Deep understanding of Linux Network Technology Insider--interaction between user space and kernel space

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.