Analysis of netlink sample application

The netlink sample application for the virtual loopback driver that we are developing is provided at this place. We will analyze the code with respect to the various aspects of how a generic netlink connection would work in this case.

The first thing that we achieve is to open a netlink socket and connect to the generic netlink sub-system. As discussed in previous articles, the generic netlink subsystem maintains all the different families that are registered to it. The code snippet achieving this is provided below

Do kindly note that the above APIs used are part of libnl and libnl-genl libraries and these libraries are needed for the code to compile. All the APIs used in the sample application are part of these two libraries and should be present.

Once the socket connection is created, the application needs to query the generic netlink sub-system for the generic netlink identification number for the family “vnet_nl” that we are attempting to send a netlink command. The family_id is sent as part of the netlink message header for the generic netlink subsystem to identify the family and forward the command to the specific registered callback it holds with it for that command. The family identification resolution and the message header creation is shown below.

When the netlink family was registered for the NL_CMD_ECHO echo command as shown in this article, a function which would handle the command was also registered. The code snippet for that relevant section for the loopback driver is placed again here in order to understand the flow on how the generic netlink controller would identify the family via the family_id and later invoke the registered callback for the specific command invoked.

Coming back to the sample application, finally the message to be sent to the kernel module which hosts the generic netlink family “vnet_nl” is formulated and the message sent to via the netlink socket. A callback function “echo_cb” is also registered with the generic netlink sub-system to handle the response from the device driver.

The sample compilation and output is provided below. The message is echoed back from the kernel

The dmesg output from the kernel module hosting the generic netlink family is also provided below

In the next set of articles, we will review what we have currently incorporated and look at the next steps on adding more functionality commonly seen in networking device drivers.

Leave a Reply

Your email address will not be published. Required fields are marked *