Linux user mode and kernel mode communication method --netlink

Linux user space and kernel space communication method: system calls, procfs, ioctl interfaces, netlink

 

netlink is the most common way Linux kernel mode and user mode communication.

netlink: netlink socekt IPC is a special data transmission between kernel mode and user mode process is used. Kernel modules by providing a special set of API, with the way a set of standard socket interface for the user program, the communication link is achieved in a full-duplex. Similar to TCP / IP is used as the address family AF_INET, netlink socket using the address family AF_NETLINK.

Add new features to a new system calls, ioctls or proc file a practice is not very easy thing, because we want to kernel code and risking contamination risks could undermine stability of the system to accomplish this. netlink socket but it is so simple, you just need to add a constant in your file netlink.h to identify the type of agreement, then, kernel modules and user programs can immediately use the socket API to communicate with the style!

Netlink provides an asynchronous communication method , system calls and ioctl are synchronized. Thus, when we use a system call to deliver the message from the user mode to the kernel, process the message if the time is very long, the size of the kernel scheduler will be affected.

Netlink is superior to system calls, ioctls and proc file system, another feature is that it supports multicast . A process can be a netlink message to the group address, and any number of processes that can monitor a group address (and receive messages). This mechanism for the kernel to user mode event distribution provides a near-perfect solution.

Ioctl system call and belong IPC simplex mode, i.e., an initiator which is the IPC session only user mode application . Netlink the perfect solution to this problem by allowing the kernel initialization session the way, we call it the duplex properties of netlink socket .

Using a dedicated interface to the kernel space kernel API, the kernel may be used to end the process context and interrupt context. User space using standard BSD socket interface. Easy to use

Not only can achieve kernel - user-space communication can also be used to communicate two user processes, but rarely used. There are many ways of communication in user space.

 

I need a way can initiate the notification message in kernel mode, and user mode programs can best use a "blocking call" approach waiting for news. Such models can maximize save CPU scheduling, at the same time meet the requirements in time, the final choice of the completion of the communication process netlink.

Because netlink is a datagram oriented service. Similar to the UDP.

Netlink communication model and the socket communication is very similar, the main points are as follows:

 

  • netlink coded using their own independent address, struct sockaddr_nl;
  • Each message must be sent by netlink netlink own message comes with a head, struct nlmsghdr;
  • netlink kernel mode and user mode API operation is completely different, then later described;
  • netlink user mode using socket function operation is completed, is very convenient and simple, TCP / UDP socket-based programming is very easy to use.

Guess you like

Origin blog.csdn.net/weixin_38812277/article/details/92762904