![](/img/trans.png)
[英]When I create a new userspace thead, it will appear on kernel process list?
[英]How do I create a “netlink” between kernel and userspace?
我想使用netlink在應用程序和內核空間之間進行通信。 我的Linux內核版本是2.6.28,以下是我的錯誤代碼:
nf_sock=netlink_kernel_create(NL_PROTO,0,nl_user_skb,THIS_MODULE);
縮寫錯誤消息是:
error: too few arguments to function 'netlink_kernel_create'
在文件<linux/netlink.h>
,函數netlink_kernel_create()
定義為
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)
我不明白第一個參數net
用什么。 誰能解釋一下我應該在這里使用什么?
struct net
包含有關網絡命名空間的信息,網絡命名空間是一組可用於進程的網絡資源。 請注意,可能有多個網絡命名空間(即網絡堆棧的多個實例),但大多數驅動程序使用init_net命名空間。
您的電話應該看起來像下面這樣
nf_sock = netlink_kernel_create(&init_net,
NETLINK_USERSOCK,
0,
nl_rcv_func,
NULL,
THIS_MODULE);
其中nl_rcv_func
是一個以struct sk_buff *skb
為唯一參數並處理收到的netlink消息的函數。
是的,struct net確實用於net命名空間,但是總是使用init_net是不合適的,你應該注冊自己的pernet_operations,如下所示:
static struct pernet_operations fib_net_ops = {
.init = fib_net_init,
.exit = fib_net_exit,
};
static int __net_init fib_net_init(struct net *net)
{
int error;
#ifdef CONFIG_IP_ROUTE_CLASSID
net->ipv4.fib_num_tclassid_users = 0;
#endif
error = ip_fib_net_init(net);
if (error < 0)
goto out;
error = nl_fib_lookup_init(net);
if (error < 0)
goto out_nlfl;
error = fib_proc_init(net);
if (error < 0)
goto out_proc;
out:
return error;
out_proc:
nl_fib_lookup_exit(net);
out_nlfl:
ip_fib_net_exit(net);
goto out;
}
static int __net_init nl_fib_lookup_init(struct net *net)
{
struct sock *sk;
struct netlink_kernel_cfg cfg = {
.input = nl_fib_input,
};
sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
if (sk == NULL)
return -EAFNOSUPPORT;
net->ipv4.fibnl = sk;
return 0;
}
最后:
register_pernet_subsys(&fib_net_ops);
我建議ioctl進行內核/用戶通信。 ioctl接口是標准的,並且內核之間更新的可能性很小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.