Netlink 内核实现分析 4
Posted codestack
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Netlink 内核实现分析 4相关的知识,希望对你有一定的参考价值。
netlink 库函数:
http://www.infradead.org/~tgr/libnl/doc/core.html#core_netlink_fundamentals
#define NETLINK_TEST (31) static int s_send_ack_to_test(struct lua_nl_ack *ack) { struct sk_buff *nl_skb; struct nlmsghdr *nlh; int ret; int len = sizeof(*ack); nl_skb = nlmsg_new(len, GFP_ATOMIC); if (!nl_skb) { pr_err("netlink: nlmsg_new fail\n"); return -1; } nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_WNS, len, 0); if(nlh == NULL) { pr_err("netlink: nlmsg_put fail\n"); nlmsg_free(nl_skb); return -1; } memcpy(nlmsg_data(nlh), (void *)ack, len); ret = netlink_unicast(nlsk, nl_skb, NETLINK_WNS_LUA, MSG_DONTWAIT); return ret; } static void test_input_cb(struct sk_buff *skb) { struct nlmsghdr *nlh = NULL; struct lua_nl_req *req = NULL; struct lua_nl_ack ack; nlh = (struct nlmsghdr *)skb->data; if (NLMSG_OK(nlh, skb->len) && skb->len >= NLMSG_HDRLEN + sizeof(struct lua_nl_req)) { req = (struct lua_nl_req *)NLMSG_DATA(nlh); (void)s_get_ip_mac(req, &ack); if (s_send_ack_to_test(&ack) < 0) { pr_err("netlink: s_send_ack_to_lua error\n"); } //TODO: netlink消息失败的情况需要再仔细考虑下,可能导致阻塞 } else { pr_err("netlink: parameters error\n"); } return ; } static struct netlink_kernel_cfg cfg = { .input = test_input_cb, }; static int s_init_netlink() { nlsk = netlink_kernel_create(&init_net, NETLINK_TEST, THIS_MODULE, &cfg); if (!nlsk) { pr_err("netlink: init fail\n"); return -1; } return 0; }
以上是关于Netlink 内核实现分析 4的主要内容,如果未能解决你的问题,请参考以下文章