Use Netlink in Linux kernel versions 2.6.24 and later

Source: Internet
Author: User
How to Use Netlink in Linux kernel 2.6.24 and later versions

Netlink 2.6.24 has been transplanted recently. There are also a lot of online tutorials, some of which are still very systematic, but I have used a test to compile none of them. Later, I checked it carefully and found that the kernel version was still a problem. After some hard work, I finally finished it. The following is a brief introduction of my experience.

I used RedHat 5x64 to upgrade the kernel to 2.6.24. Since 2.6.24, Linux has made great changes to the Netlink implementation mechanism and calling interface, in particular, the netlink_kernel_create () function has as many as six latest parameters. Most tutorials and examples on the InternetProgramThese functions are applicable to kernels earlier than 2.6.12, or even version 2.4. Therefore, the most basic functions cannot be compiled successfully, not to mention the subsequent functions.

In fact, the new version of Netlink simplifies the use logic. Of course, I am mainly talking about kernel space programming. for user space programs, Netlink is still as easy to use as a classic socket program. I didn't take a closer look at other detailed functions. In fact, other changes have little impact. Here I will only introduce the most important CREATE FUNCTION OF the change. The following is a function prototype:

Extern struct sock * netlink_kernel_create (struct net * Net,
Int unit, unsigned int groups,
Void (* input) (struct sk_buff * SKB ),
Struct mutex * cb_mutex,
Struct module * module );

The usage is as follows (nl_sk is a sock pointer ):

Nl_sk = netlink_kernel_create (& init_net, netlink_test, 0, nl_data_ready, null, this_module );

The first parameter is newly introduced. I couldn't figure out the specific functions, but I found the method to use it: Use & init_net. This init_net struct is defined in Linux/NET/CORE/net_namespace.c. In addition, it is also defined in Linux/include/NET/net_namespace.h and can be directly used as a parameter. The second parameter is also available in previous versions, that is, the Netlink type. The system defines 16 items, which can be found in net/Netlink. h. To avoid conflicts with existing system definitions (some may be occupied), we have defined a value: # define netlink_test 22. In the following parameters, nl_data_ready is very important. It is a bit similar to the previously initialized receiving function, but now we do not need to call the scheduling method by ourselves to block the kernel process so that we can wait for the message to arrive. If you declare an nl_data_ready function and assign the address to this parameter, Netlink automatically processes the message arrival event. Once a matching message from the user space is received, the nl_data_ready function is automatically called for processing. Therefore, this is actually a major simplification. The last this_module is defined in module. H, indicating the current module. This is also the latest introduced parameter. It should be added in combination with the automatic call of the Data receiving function nl_data_ready. After all, Netlink needs to know which module the receiving function is located in, otherwise, how can it be called.

okay. For other users, see Code . The function of the sample program I wrote is that a user space program nltimer_u interacts with the kernel module nltimer_k. Start the kernel module first. After the user program is started, the system sends a Hello Message to the kernel module. After the nltimer_k receives the message, it starts to send a message to the user program every one second. After receiving the message, the user adds an echo: prefix and then replies to the kernel. After the specified cycle time, send the Exit message. After receiving the message, both parties end the operation and the user process exits. The kernel module is ready to accept the next data arrival. However, it will not go into the timer loop any more. Instead, it will only receive the first Hello message and then send exit. This function covers kernel timer and kernel module programming, as well as most of the common techniques of the new Netlink version. You may try it.

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.