Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


result = sock_sendmsg(sock, msg, size);
...
return result;
}

12.11.2 The Netlink Mechanism


Netlinkis a networking-based mechanism that allows for communication within the kernel as well as
between kernel and userland. The formal definition can be found in RFC 3549. The idea to use the net-
working framework to communicate between kernel and userland stems from BSD’s networking sockets.
Netlink sockets, however, extend the possible uses much further. The mechanism is not only used for
networking purposes. By now, one of the most important users is the generic object model, which uses
netlink sockets to pass all kinds of status information about what is going on inside the kernel to user-
land. This includes registration and removal of new devices, special events that have happened on the
hardware side, and much more. While netlink used to be compilable as a module in former kernel ver-
sions, it is nowadays automatically integrated if the kernel has support for networking. This emphasizes
the importance of the mechanism.

There are some alternative methods in the kernel that implement similar functionality — just think of
files in procfs or sysfs. However, the netlink mechanism provides some distinct advantages compared to
these approaches:

❑ No polling is required on any side. If status information were passed via a file, then the userland
side would constantly need to check if any new messages have arrived.
❑ System calls and ioctls that also allow passing information from userland to the kernel are harder
to implement than a simple netlink connection. Besides, there is no problem with modules using
netlink services, while modules and system calls clearly do not fit together very well.
❑ The kernel can initiate sending information to userland without being requested to do so from
there. This is also possible with files, but impossible with system calls or ioctls.
❑ Userspace applications do not need to use anything else than standard sockets to interact with
the kernel.

Netlink supports only datagram messages, but provides bidirectional communication. Additionally, not
only unicast but also multicast messages are possible. Like any other socket-based mechanism, netlink
works asynchronously.

Two manual pages document the netlink mechanism:netlink(3)contains information about in-kernel
macros that can be used to manipulate, access, and create netlink datagrams. The manual page
netlink(7)contains generic information about netlink sockets and documents the data structures used
in this context. Also note that/proc/net/netlinkcontains some information about the currently active
netlink connections.

On the userspace side, two libraries simplify the creation of applications employing netlink sockets:

❑ libnetlinkis bundled with theiproute2packages. The library has specifically been written
with routing sockets in mind. Additionally, is does not come as standalone code, but must be
extracted from the package if it is to be used separately.
Free download pdf