netlink API changes

本文介绍Linux内核中Netlink接口的变化,特别是在2.6.37版本中的更新,并提供了内核模块与用户空间应用通过Netlink进行通信的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Netlink socket is a flexible interface for communication between user-space applications and kernel modules. It provides an easy-to-use socket API to both applications and the kernel. It provides advanced communication features, such as full-duplex, buffered I/O, multicast and asynchronous communication, which are absent in other kernel/user-space IPCs. But the netlink interface in Linux is fast changing,the APIs vary a lot between different kernel versions.Here is the changed interfaces in Linux kernel 2.6.37

netlink_kernel_create

This method is called to create a netlink socket in kernel modules.In ealier version of the kernel this functions is defined as this:

struct sock *

netlink_kernel_create(int unit,

           void (*input)(struct sock *sk, int len));

The parameter unit is, in fact, the netlink protocol type, such as NETLINK_TEST. The function pointer, input, is a callback function invoked 

when a message arrives at this netlink socket. while in 2.6.37 the function is defined as below in /linux/netlink.h

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);

There are some newly-added parameters:

 

struct net *net:the namespace of network.using &init_net is ok.

unsigned int groups:the group number of the socket.pass NULL to this parameter

struct mutex *cb_mutex:NULL is OK

struct module *module:use the THIS_MODULE macro

 

notice that the prototype of the callback func input has also been changed.

sk_buff in handler function

All network-related queues and buffers in the kernel use a common data structure, struct sk_buff. This is a large struct containing all the control information required for the packet (datagram, cell, whatever). The sk_buff elements are organized as a doubly linked list, in such a way that it is very efficient to move an sk_buff element from the beginning/end of a list to the beginning/end of another list. A queue is defined by struct sk_buff_head, which includes a head and a tail pointer to sk_buff elements. 

All the queuing structures include an sk_buff_head representing the queue. For instance, struct sock includes a receive and send queue. Functions to manage the queues (skb_queue_head(), skb_queue_tail(), skb_dequeue(), skb_dequeue_tail()) operate on an sk_buff_head. In reality, however, the sk_buff_head is included in the doubly linked list of sk_buffs (so it actually forms a ring). 

When a sk_buff is allocated, also its data space is allocated from kernel memory. sk_buff allocation is done with alloc_skb() or dev_alloc_skb(); drivers use dev_alloc_skb();. (free by kfree_skb() and dev_kfree_skb(). 

However, sk_buff provides an additional management layer. The data space is divided into a head area and a data area. This allows kernel functions to reserve space for the header, so that the data doesn't need to be copied around. Typically, therefore, after allocating an sk_buff, header space is reserved using skb_reserve(). skb_pull(int len) – removes data from the start of a buffer (skipping over an existing header) by advancing data to data+len and by decreasing len. 

Sample Code

Below is the sample code I wrote. The user send a greeting message to kernel module and the kernel module print the message,then send a greeting message back to the user app.(Linux kernel version 2.6.37,os openSUSE 11.4)

kernel module:

 

User App:

 

 

#include "tst_test.h" #include "tst_safe_macros.h" #include "lapi/sched.h" #define MAX_TRIES 1000 static void child_func(void) { int fd, len, event_found, tries; struct sockaddr_nl sa; char buffer[4096]; struct nlmsghdr *nlh; /* child will listen to a network interface create/delete/up/down events */ memset(&sa, 0, sizeof(sa)); sa.nl_family = AF_NETLINK; sa.nl_groups = RTMGRP_LINK; fd = SAFE_SOCKET(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); SAFE_BIND(fd, (struct sockaddr *) &sa, sizeof(sa)); /* waits for parent to create an interface */ TST_CHECKPOINT_WAKE_AND_WAIT(0); /* * To get rid of "resource temporarily unavailable" errors * when testing with -i option */ tries = 0; event_found = 0; nlh = (struct nlmsghdr *) buffer; while (tries < MAX_TRIES) { len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT); if (len > 0) { /* stop receiving only on interface create/delete event */ if (nlh->nlmsg_type == RTM_NEWLINK || nlh->nlmsg_type == RTM_DELLINK) { event_found++; break; } } usleep(10000); tries++; } SAFE_CLOSE(fd); if (event_found) tst_res(TPASS, "interface changes detected"); else tst_res(TFAIL, "failed to detect interface changes"); exit(0); } static void test_netns_netlink(void) { /* unshares the network namespace */ SAFE_UNSHARE(CLONE_NEWNET); if (SAFE_FORK() == 0) child_func(); /* wait until child opens netlink socket */ TST_CHECKPOINT_WAIT(0); /* creates TAP network interface dummy0 */ if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap"))) tst_brk(TBROK, "adding interface failed"); /* removes previously created dummy0 device */ if (WEXITSTATUS(system("ip tuntap del mode tap dummy0"))) tst_brk(TBROK, "removing interface failed"); /* allow child to continue */ TST_CHECKPOINT_WAKE(0); tst_reap_children(); } static struct tst_test test = { .test_all = test_netns_netlink, .needs_checkpoints = 1, .needs_root = 1, .forks_child = 1, .needs_kconfigs = (const char *[]) { "CONFIG_NET_NS=y", "CONFIG_TUN", NULL }, };
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值