Linux内核态之间进程通信,内核态和用户态通信(二)--实现
本文主要使用netlink套接字實(shí)現(xiàn)中斷環(huán)境與用戶態(tài)進(jìn)程通信。
系統(tǒng)環(huán)境:基于linux 2.6.32.27 和 linux 3.16.36
Linux內(nèi)核態(tài)和用戶態(tài)進(jìn)程通信方法的提出和實(shí)現(xiàn)
用戶上下文環(huán)境
運(yùn)行在用戶上下文環(huán)境中的代碼是可以阻塞的,這樣,便可以使用消息隊(duì)列和Unix域套接字來(lái)實(shí)現(xiàn)內(nèi)核態(tài)和用戶態(tài)的通信。但這些的數(shù)據(jù)傳輸效率較低,linux內(nèi)核提供copy_from_user() 和 copy_to_user() 函數(shù)來(lái)實(shí)現(xiàn)內(nèi)核態(tài)與用戶態(tài)數(shù)據(jù)的拷貝,但這兩個(gè)函數(shù)會(huì)引發(fā)阻塞,所以不能用在硬、軟中斷中。一般將這兩個(gè)特殊拷貝函數(shù)用在類似系統(tǒng)調(diào)用一類的函數(shù)中,如圖,
其中相關(guān)的系統(tǒng)調(diào)用是需要用戶自行編寫并載入內(nèi)核。
硬、軟中斷環(huán)境
硬中斷和軟中斷環(huán)境與用戶態(tài)進(jìn)程無(wú)絲毫關(guān)系,而且運(yùn)行過(guò)程不能阻塞。
軟中斷、硬中斷有一套同步機(jī)制 — 自旋鎖(spinlock),可以通過(guò)自旋鎖來(lái)實(shí)現(xiàn)中斷環(huán)境和中斷環(huán)境,中斷環(huán)境與內(nèi)核線程的同步,而內(nèi)核線程是運(yùn)行在有進(jìn)程上下文環(huán)境中的,這樣便可以在內(nèi)核線程中使用套接字或消息隊(duì)列來(lái)取得用戶空間的數(shù)據(jù),然后再將數(shù)據(jù)通過(guò)臨界區(qū)傳遞給中斷過(guò)程,如圖
因?yàn)橹袛噙^(guò)程不可能無(wú)休止地等待用戶態(tài)進(jìn)程發(fā)送數(shù)據(jù),所以要通過(guò)一個(gè)內(nèi)核線程來(lái)接收用戶空間的數(shù)據(jù),再通過(guò)臨界區(qū)傳給中斷過(guò)程。中斷過(guò)程向用戶空間的數(shù)據(jù)發(fā)送必須是無(wú)阻塞的。這樣的通信模型并不令人滿意,因?yàn)閮?nèi)核線程是和其他用戶態(tài)進(jìn)程競(jìng)爭(zhēng)cpu接收數(shù)據(jù)的,效率很低,這樣中斷過(guò)程便不能實(shí)時(shí)地接收來(lái)自用戶空間的數(shù)據(jù)。
netlink套接字的通信依據(jù)是一個(gè)對(duì)應(yīng)于進(jìn)程的標(biāo)識(shí),一般定義為該進(jìn)程的ID。當(dāng)通信的一端處于中斷過(guò)程時(shí),該標(biāo)識(shí)為0。當(dāng)使用 netlink 套接字進(jìn)行通信,通信的雙方都是用戶態(tài)進(jìn)程,則使用方法類似于消息隊(duì)列。但通信雙方有一端是中斷過(guò)程,使用方法則不同。netlink 套接字的最大特點(diǎn)是對(duì)中斷過(guò)程的支持,它在內(nèi)核空間接收用戶空間數(shù)據(jù)時(shí)不再需要用戶自行啟動(dòng)一個(gè)內(nèi)核線程,而是通過(guò)另一個(gè)軟中斷調(diào)用用戶事先指定的接收函數(shù)。工作原理如圖
很明顯,這里使用了軟中斷而不是內(nèi)核線程來(lái)接收數(shù)據(jù),這樣就可以保證數(shù)據(jù)接收的實(shí)時(shí)性。
當(dāng) netlink 套接字用于內(nèi)核空間與用戶空間的通信時(shí),在用戶空間的創(chuàng)建方法和一般套接字使用類似,但內(nèi)核空間的創(chuàng)建方法則不同。如圖
/**
*imp2.h
*/
#ifndef __IMP2_H__
#define __IMP2_H__
#define IMP2_OPS_BASIC 128
#define IMP2_SET IMP2_OPS_BASIC
#define IMP2_GET IMP2_OPS_BASIC
#define IMP2_MAX (IMP2_OPS_BASIC + 1)
#define IMP2_U_PID 0
#define IMP2_K_MSG 1
#define IMP2_CLOSE 2
#define NL_IMP2 31
struct packet_info
{
__u32 src;
__u32 dest;
};
#endif
/**
*imp2_u.c
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "imp2.h"
struct msg_to_kernel
{
struct nlmsghdr hdr;
};
struct u_packet_info
{
struct nlmsghdr hdr;
struct packet_info icmp_info;
};
static int skfd;
static void sig_int(int signo)
{
struct sockaddr_nl kpeer;
struct msg_to_kernel message;
memset(&kpeer,0,sizeof(kpeer));
kpeer.nl_family = AF_NETLINK;
kpeer.nl_pid = 0;
kpeer.nl_groups = 0;
memset(&message,0,sizeof(message));
message.hdr.nlmsg_len = NLMSG_LENGTH(0);
message.hdr.nlmsg_flags = 0;
message.hdr.nlmsg_type = IMP2_CLOSE;
message.hdr.nlmsg_pid = getpid();
sendto(skfd,&message,message.hdr.nlmsg_len,0,(struct sockaddr *)(&kpeer),sizeof(kpeer));
close(skfd);
exit(0);
}
int main(void)
{
/* 本地的 */
struct sockaddr_nl local;
/* 連線kernel的 */
struct sockaddr_nl kpeer;
int kpeerlen;
struct msg_to_kernel message;
struct u_packet_info info;
int sendlen = 0;
int rcvlen = 0;
struct in_addr addr;
skfd = socket(AF_NETLINK,SOCK_RAW,NL_IMP2);
if(skfd < 0) {
printf("cannot create a netlink socket\n");
exit(0);
}
memset(&local,0,sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_pid = getpid();
local.nl_groups = 0;
if(bind(skfd,(struct sockaddr *)&local,sizeof(local)) != 0) {
printf("bind() error\n");
return -1;
}
signal(SIGINT,sig_int);
memset(&kpeer,0,sizeof(kpeer));
kpeer.nl_family = AF_NETLINK;
kpeer.nl_pid = 0;
kpeer.nl_groups = 0;
memset(&message,0,sizeof(message));
message.hdr.nlmsg_len = NLMSG_LENGTH(0);
message.hdr.nlmsg_flags = 0;
message.hdr.nlmsg_type = IMP2_U_PID;
message.hdr.nlmsg_pid = local.nl_pid;
sendto(skfd,&message,message.hdr.nlmsg_len,0,(struct sockaddr *)&kpeer,sizeof(kpeer));
while(1) {
kpeerlen = sizeof(struct sockaddr_nl);
rcvlen = recvfrom(skfd,&info,sizeof(struct u_packet_info),0,(struct sockaddr *)&kpeer,&kpeerlen);
addr.s_addr = info.icmp_info.src;
printf("src:%s,",inet_ntoa(addr));
addr.s_addr = info.icmp_info.dest;
printf("dest:%s\n",inet_ntoa(addr));
}
return 0;
}
/**
* imp2_k.c //兼容linux 2.6.32 和 linux 3.16.36
*/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "imp2.h"
#define NF_IP_PRE_ROUTING 0
/* 實(shí)現(xiàn)從netfilter的NF_IP_PRE_ROUTING點(diǎn)截獲的ICMP數(shù)據(jù)包,
再將數(shù)據(jù)包的相關(guān)信息傳遞到一個(gè)用戶態(tài)進(jìn)程 */
static struct sock *nlfd;
//是競(jìng)爭(zhēng)的資源
struct {
__u32 pid;
rwlock_t lock;
}user_proc;
//相當(dāng)于np_log
static int send_to_user(struct packet_info *info)
{
int ret = 0;
int size = 0;
unsigned char *old_tail = NULL;
struct sk_buff *skb = NULL;
struct nlmsghdr *nlh = NULL;
struct packet_info *packet = NULL;
printk("%s,begin ....\n",__FUNCTION__);
size = NLMSG_SPACE(sizeof(*info));
//分配一塊skb(數(shù)據(jù)緩存區(qū)和skb描述符),大小,GFP自動(dòng)分配
skb = alloc_skb(size,GFP_ATOMIC);
old_tail = skb->tail;
nlh = nlmsg_put(skb,0,0,IMP2_K_MSG,(size-sizeof(*nlh)),0);
packet = NLMSG_DATA(nlh);
memset(packet , 0 ,sizeof(struct packet_info));
packet->src = info->src;
packet->dest = info->dest;
nlh->nlmsg_len = skb->tail - old_tail;
NETLINK_CB(skb).dst_group = 0;
read_lock_bh(&user_proc.lock);
ret = netlink_unicast(nlfd,skb,user_proc.pid,MSG_DONTWAIT);
read_unlock_bh(&user_proc.lock);
printk("%s,end....\n",__FUNCTION__);
return ret;
nlmsg_failure:
if(skb) {
kfree_skb(skb);
}
return -1;
}
static unsigned int get_icmp(unsigned int hooknum,struct sk_buff *skb,
const struct net_device *in,const struct net_device *out,
int(*okfn)(struct sk_buff *))
{
//struct iphdr *iph = (*pskb)->nh.iph;
struct iphdr *iph = ip_hdr(skb); //2.6.24開(kāi)始使用,因?yàn)閟truct sk_buff
struct packet_info info;
if(iph->protocol == IPPROTO_ICMP) {
read_lock_bh(&user_proc.lock);
if(user_proc.pid != 0) {
info.src = iph->saddr;
info.dest= iph->daddr;
//printk("%s,src = %u.%u,%u.%u,dst = %u,%u,%u,%u\n",__FUNCTION__,NIPQUAD(info.src), NIPQUAD(info.dest));
read_unlock_bh(&user_proc.lock);
send_to_user(&info);
} else {
//printk("%s, no user process runing....\n",__FUNCTION__);
read_unlock_bh(&user_proc.lock);
}
}
return NF_ACCEPT;
}
static struct nf_hook_ops imp2_ops =
{
.hook = get_icmp,
.pf = PF_INET,
.hooknum = NF_IP_PRE_ROUTING,
.priority = NF_IP_PRI_FILTER - 1,
.owner = THIS_MODULE,
};
static void kernel_receive(struct sk_buff *skb)
{
struct nlmsghdr *nlh = NULL;
int len = 0;
nlh = nlmsg_hdr(skb);
len = skb->len;
while(NLMSG_OK(nlh,len)) {
write_lock_bh(&user_proc.lock);
if(nlh->nlmsg_type == IMP2_U_PID) {
user_proc.pid = nlh->nlmsg_pid;
}
else if(nlh->nlmsg_type == IMP2_CLOSE && nlh->nlmsg_pid == user_proc.pid) {
user_proc.pid = 0;
}
write_unlock_bh(&user_proc.lock);
netlink_ack(skb,nlh,0);
nlh = NLMSG_NEXT(nlh,len);
}
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
struct netlink_kernel_cfg cfg =
{
.input = kernel_receive,
};
#endif
static int __init init(void)
{
rwlock_init(&user_proc.lock);
//這里的版本問(wèn)題需要解決
/*在內(nèi)核創(chuàng)建一個(gè)netlink socket ,
協(xié)議 NL_IMP2是自定義的,并指示由 kernel_receive接收數(shù)據(jù)*/
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
nlfd = netlink_kernel_create(&init_net,NL_IMP2,&cfg);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
nlfd = netlink_kernel_create(&init_net ,NL_IMP2 , 0, kernel_receive, NULL, THIS_MODULE);
#else
nlfd = NULL;
#endif
}
if(!nlfd) {
printk("cannot create a netlink socket\n");
return -1;
}
return nf_register_hook(&imp2_ops);
}
static void __exit fini(void)
{
if(nlfd) {
netlink_kernel_release(nlfd);
}
nf_unregister_hook(&imp2_ops);
}
module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");
#Makefile
MODULE_NAME := imp2_k
obj-m := $(MODULE_NAME).o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
clean:
rm -fr *.ko
rm -fr *.o
rm -fr *.cmd sender $(MODULE_NAME).mod.c
.PHONY:clean aLL
netlink具體結(jié)構(gòu)分析,可參考其他博文
http://blog.csdn.net/luckyapple1028/article/details/50839395
http://blog.csdn.net/luckyapple1028/article/details/50936563
總結(jié)
以上是生活随笔為你收集整理的Linux内核态之间进程通信,内核态和用户态通信(二)--实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Altium Designer(3)-绘
- 下一篇: p5js可以在linux上运行吗,在li