当前位置: 移动技术网 > 网络运营>服务器>Linux > linux下用户程序同内核通信详解(netlink机制)

linux下用户程序同内核通信详解(netlink机制)

2019年04月21日  | 移动技术网网络运营  | 我要评论

简介

linux下用户程序同内核通信的方式一般有ioctl, proc文件系统,剩下一个就是netlink套接字了。 这里先介绍下netlink。

netlink 是一种在内核与用户应用间进行双向数据传输的非常好的方式,用户态应用使用标准的 socket api 就可以使用 netlink 提供的强大功能,内核态需要使用专门的内核 api 来使用 netlink。

netlink 相对于系统调用,ioctl 以及 /proc 文件系统而言具有以下优点:

1,为了使用 netlink,用户仅需要在 include/linux/netlink.h 中增加一个新类型的 netlink 协议定义即可, 如 #define netlink_mytest 17 然后,内核和用户态应用就可以立即通过 socket api 使用该 netlink 协议类型进行数据交换。但系统调用需要增加新的系统调用,ioctl 则需要增加设备或文件, 那需要不少代码,proc 文件系统则需要在 /proc 下添加新的文件或目录,那将使本来就混乱的 /proc 更加混乱。

2. netlink是一种异步通信机制,在内核与用户态应用之间传递的消息保存在socket缓存队列中,发送消息只是把消息保存在接收者的socket的接 收队列,而不需要等待接收者收到消息,但系统调用与 ioctl 则是同步通信机制,如果传递的数据太长,将影响调度粒度。

3.使用 netlink 的内核部分可以采用模块的方式实现,使用 netlink 的应用部分和内核部分没有编译时依赖,但系统调用就有依赖,而且新的系统调用的实现必须静态地连接到内核中,它无法在模块中实现,使用新系统调用的应用在编译时需要依赖内核。

4.netlink 支持多播,内核模块或应用可以把消息多播给一个netlink组,属于该neilink 组的任何内核模块或应用都能接收到该消息,内核事件向用户态的通知机制就使用了这一特性,任何对内核事件感兴趣的应用都能收到该子系统发送的内核事件,在 后面的文章中将介绍这一机制的使用。

5.内核可以使用 netlink 首先发起会话,但系统调用和 ioctl 只能由用户应用发起调用。

6.netlink 使用标准的 socket api,因此很容易使用,但系统调用和 ioctl则需要专门的培训才能使用。

下面这两部分代码主要的目的是用netlink机制实现用户程序和内核的通信。 具体就是用户程序执行./netlink -s [我是参数] 或./netlink -g 时,内核会返回"s know you!" 和“i know you!” 这两种字符串, 然后输出。 内核和用户程序均加有打印。

内核模块

1. makefile依赖的编译规则 ruler.dir

pwd := $(shell pwd) 
 
all: modules romfs 
 
modules: 
 $(make) -c $(kdir) m=$(pwd) modules 
 @echo $(obj) 
 
modules_install: 
 $(make) -c $(kdir) m=$(pwd) modules_install 
 
romfs: 
 cp -rf *.ko $(modules_build_dir) 
 
clean: 
 rm *.o *.ko *.mod.* module.* modules.* 
 rm -rf $(modules_build_dir) 
 
obj-m := $(mod_name).o 

2.makefile

kernel_modules := netlink 
 
export modules_root_dir := $(shell pwd) 
export modules_build_dir := $(modules_root_dir)/build 
export kdir := /lib/modules/$(shell uname -r)/build #这行是为了取出系统下内核的目录(ubuntu) 
 
all: init modules romfs 
 
init: 
 mkdir -p $(modules_build_dir) 
 
modules:$(patsubst %, _dir_%, $(kernel_modules)) 
 
$(patsubst %, _dir_%, $(kernel_modules)): 
 @echo 
 @echo building $(patsubst _dir_%, %, $@) 
 $(make) -c $(patsubst _dir_%, %, $@) all 
 
 
romfs: $(patsubst %, _romfs_%, $(kernel_modules)) 
$(patsubst %, _romfs_%, $(kernel_modules)): 
 $(make) -c $(patsubst _romfs_%, %, $@) romfs 
 
 
clean: $(patsubst %, _clean_%, $(kernel_modules)) 
 $(rm) $(build_dir) 
$(patsubst %, _clean_%, $(kernel_modules)): 
 @echo 
 @echo cleaning $(patsubst _dir_%, %, $@) 
 $(make) -c $(patsubst _clean_%, %, $@) clean 
 
.phony: 

3. ./netlink/netlink.c

/* 
 * netlink.c 
 * 
 * created on: 2014 *  author: cr 
 */ 
 
#include <linux/init.h> 
#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/skbuff.h> 
#include <linux/ip.h> 
#include <linux/types.h> 
#include <linux/sched.h> 
#include <linux/netlink.h> 
#include <net/sock.h> 
#include "usrlink.h" 
 
module_license("dual bsd/gpl"); 
module_author("mdaxia"); 
 
struct sock *netlink_fd; 
 
 
static void netlink_to_user(int dest, void *buf, int len) 
{ 
 struct nlmsghdr *nl; 
 struct sk_buff *skb; 
 int size; 
 
 size = nlmsg_space(len); 
 skb = alloc_skb(size, gfp_atomic); 
 if(!skb || !buf) 
 { 
  printk(kern_alert "netlink_to_user skb of buf null!\n"); 
  return; 
 } 
 nl = nlmsg_put(skb, 0, 0, 0, nlmsg_space(len) - sizeof(struct nlmsghdr), 0); 
 netlink_cb(skb).pid = 0; 
 netlink_cb(skb).dst_group = 0; 
 
 memcpy(nlmsg_data(nl), buf, len); 
 nl->nlmsg_len = (len > 2) ? (len - 2):len; 
 
 netlink_unicast(netlink_fd, skb, dest, msg_dontwait); 
 printk(kern_alert "k send packet success\n"); 
} 
 
static int process_hello_get(int dest, void *buf, int len) 
{ 
 printk(kern_alert "in process_hello get!\n"); 
 memcpy(buf, "i known you !", 13); 
 netlink_to_user(dest, buf, 13); 
 return net_ok; 
} 
 
static int process_hello_set(int dest, void *buf, int len) 
{ 
 printk(kern_alert "in process_hello set! %s\n", (char *)buf); 
 memcpy(buf, "s known you !", 13); 
 netlink_to_user(dest, buf, 13); 
 return net_ok; 
} 
 
 
static void netlink_process_packet(struct nlmsghdr *nl) 
{ 
 int ret; 
 
 switch(nl->nlmsg_type) 
 { 
 case hello_get: 
  ret = process_hello_get(nl->nlmsg_pid, nlmsg_data(nl), nl->nlmsg_len); 
  break; 
 case hello_set: 
  ret = process_hello_set(nl->nlmsg_pid, nlmsg_data(nl), nl->nlmsg_len); 
  break; 
 default:break; 
 } 
} 
 
static void netlink_recv_packet(struct sk_buff *__skb) 
{ 
 struct sk_buff *skb; 
 struct nlmsghdr *nlhdr; 
 
 skb = skb_get(__skb); 
 if(skb->len >= sizeof(struct nlmsghdr)) 
 { 
  nlhdr = (struct nlmsghdr *)skb->data; 
  if(nlhdr->nlmsg_len >= sizeof(struct nlmsghdr) && 
    __skb->len >= nlhdr->nlmsg_len) 
  { 
   netlink_process_packet(nlhdr); 
  } 
 } 
 else 
  printk(kern_alert "kernel receive msg length error!\n"); 
} 
 
static int __init netlink_init(void) 
{ 
 netlink_fd = netlink_kernel_create(&init_net, user_netlink_cmd, 0, netlink_recv_packet, null, this_module); 
 if(null == netlink_fd) 
 { 
  printk(kern_alert "init netlink!\n"); 
  return -1; 
 } 
 printk(kern_alert "init netlink success!\n"); 
 return 0; 
} 
 
static void __exit netlink_exit(void) 
{ 
 netlink_kernel_release(netlink_fd); 
 printk(kern_alert "exit netlink!\n"); 
} 
 
module_init(netlink_init); 
module_exit(netlink_exit); 

4. ./netlink/usrlink.h

/* 
 * usrlink.h 
 * 
 * created on: 2014骞?鏈?7鏃? *  author: cr 
 */ 
#ifndef usrlink_h_ 
#define usrlink_h_ 
 
#define user_netlink_cmd 25 
#define maxmsglen   1024 
 
typedef enum error_e { 
 net_error, 
 net_ok, 
 net_param, 
 net_mem, 
 net_sock, 
} netlink_err; 
 
typedef enum module_e { 
 hello_cmd = 1, 
} netlink_module; 
 
typedef enum type_e { 
 hello_set, 
 hello_get, 
} netlink_type; 
 
#endif /* usrlink_h_ */ 

5. ./netlink/makefile

mod_name := netlink 
 
$(mod_name)-objs : netlink.o 
 
-include $(modules_root_dir)/rules.dir 
 
.phony: 

6. 编译方式

其中makefile、rulers.dir 在knetlink/下, netlink.c 、netlink.h 、makefile在knetlink/netlink/目录下。 编译时在knetlink目录下执行make即可

用户程序

用户程序的makefile 这里就不放出了。 我是直接在eclipse下建的工程 自动编译的、...

1. netlink.c

/* 
 * usrlink.c 
 * 
 * created on: 2014骞?鏈?7鏃? *  author: cr 
 */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include "usrlink.h" 
 
int netlink_sock_init(netlink_sock *netlink_s, int module, int protocol) 
{ 
 netlink_s->sock = socket(pf_netlink, sock_raw, protocol); 
 if(netlink_s->sock < 0) 
  return net_sock; 
 memset(&netlink_s->src, 0 ,sizeof(netlink_s->src)); 
 netlink_s->src.nl_family = af_netlink; 
 netlink_s->src.nl_pid = module; 
 netlink_s->src.nl_groups = 0; 
 
 if(bind(netlink_s->sock, (struct sockaddr *)&netlink_s->src, sizeof(netlink_s->src)) < 0) 
  return net_sock; 
 
 netlink_s->dest.nl_family = af_netlink; 
 netlink_s->dest.nl_pid = 0; 
 netlink_s->dest.nl_groups = 0; 
 
 return net_ok; 
} 
 
int netlink_send(netlink_sock *netlink_s, int type, char *sbuf, int slen, char *rbuf, int *rlen) 
{ 
 struct msghdr msg; 
 struct nlmsghdr *nlhdr = null; 
 struct iovec iov; 
 int ret; 
 
 nlhdr = (struct nlmsghdr *)malloc(nlmsg_space(maxmsglen)); 
 if(null == nlhdr) 
  return net_mem; 
 
 memcpy(nlmsg_data(nlhdr), sbuf, slen); 
 nlhdr->nlmsg_len = nlmsg_space(slen); 
 nlhdr->nlmsg_pid = netlink_s->src.nl_pid; 
 nlhdr->nlmsg_type = type; 
 nlhdr->nlmsg_flags = 0; 
 
 iov.iov_base = (void *)nlhdr; 
 iov.iov_len = nlhdr->nlmsg_len; 
 
 msg.msg_name = (void *)&(netlink_s->dest); 
 msg.msg_namelen = sizeof(netlink_s->dest); 
 msg.msg_iov = &iov; 
 msg.msg_iovlen = 1; 
 
 ret = sendmsg(netlink_s->sock, &msg, 0); 
 if(ret < 0) 
 { 
  printf("send fail\n"); 
  goto error; 
 } 
 ret = recvmsg(netlink_s->sock, &msg, 0); 
 if(ret < 0) 
 { 
  printf("read fail\n"); 
  goto error; 
 } 
 memcpy(rbuf, nlmsg_data(nlhdr), nlhdr->nlmsg_len); 
 *rlen = nlhdr->nlmsg_len; 
 return net_ok; 
 
error: 
 free(nlhdr); 
 return net_sock; 
} 
 
int netlink_sock_deinit(netlink_sock *netlink_s) 
{ 
 close(netlink_s->sock); 
 memset(netlink_s, 0, sizeof(netlink_sock)); 
 return net_ok; 
} 

2. netlink.h

/* 
 * usrlink.h 
 * 
 * created on: 2014 *  author: cr 
 */ 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <asm/types.h> 
#include <linux/socket.h> 
#include <linux/netlink.h> 
#ifndef usrlink_h_ 
#define usrlink_h_ 
 
#define user_netlink_cmd 25 
#define maxmsglen   1024 
 
typedef enum error_e { 
 net_error, 
 net_ok, 
 net_param, 
 net_mem, 
 net_sock, 
} netlink_err; 
 
typedef enum module_e { 
 hello_cmd = 1, 
} netlink_module; 
 
typedef enum type_e { 
 hello_set, 
 hello_get, 
} netlink_type; 
 
typedef struct usr_sock_h { 
 int sock; 
 struct sockaddr_nl dest; 
 struct sockaddr_nl src; 
} netlink_sock; 
 
int netlink_sock_init(netlink_sock *netlink_s, int module, int protocol); 
int netlink_sock_deinit(netlink_sock *netlink_s); 
int netlink_send(netlink_sock *netlink_s, int type, char *sbuf, int slen, char *rbuf, int *rlen); 
 
#endif /* usrlink_h_ */ 

3. main.c

/* 
 * main.c 
 * 
 * created on: 2014骞?鏈?7鏃? *  author: cr 
 */ 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "usrlink.h" 
 
int parse_ret(int ret) 
{ 
 switch(ret) 
 { 
 case net_ok: 
  return ret; 
 case net_error: 
  printf("error\n"); 
  goto exit_p; 
 case net_mem: 
  printf("memory error\n"); 
  goto exit_p; 
 case net_param: 
  printf("param error\n"); 
  goto exit_p; 
 case net_sock: 
  printf("socket error\n"); 
  goto exit_p; 
 default:break; 
 } 
exit_p: 
 return net_error; 
} 
 
void usage(void) 
{ 
 printf("usage: netlink -g <param>\n\t-s <param>\n"); 
} 
 
int main(int argc, char **argv) 
{ 
 netlink_sock h_sock; 
 char rbuf[1024]; 
 char sbuf[1024]; 
 int ret, type, slen = 0, rlen = 0; 
 
 ret = netlink_sock_init(&h_sock, hello_cmd, user_netlink_cmd); 
 if(net_ok != parse_ret(ret)) 
  goto exit_p; 
 
 bzero(&rbuf, sizeof(rbuf)); 
 bzero(&sbuf, sizeof(sbuf)); 
 if(argc < 3) 
 { 
  usage(); 
  goto exit_p; 
 } 
 if(!strncmp("-g", argv[1], 2)) 
  type = hello_get; 
 else if(!strncmp("-s", argv[1], 2)) 
  type = hello_set; 
 
 strcpy(sbuf, argv[2]); 
 slen = strlen(sbuf); 
 ret = netlink_send(&h_sock, type, sbuf, slen, rbuf, &rlen); 
 if(net_ok != parse_ret(ret)) 
  goto exit_p; 
 
 if(rlen > 0) 
 { 
  rbuf[rlen] = '\0'; 
  printf("k rep [len = %d]:%s\n", rlen, rbuf); 
 } 
 printf("k[len = %d]: %s\n", rlen, rbuf); 
 
exit_p: 
 netlink_sock_deinit(&h_sock); 
 return 0; 
} 

总结

以上就是本文关于linux下用户程序同内核通信详解(netlink机制)的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网