vscode怎么安装编译器
时间: 2023-10-28 16:07:10 浏览: 157
下面是一个C语言代码示例,可以在xdp程序内核端使用netlink监听来自用户空间的消息,并针对收到的消息调用回调函数:
```
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/pkt_cls.h>
#include <linux/socket.h>
#include <linux/netlink.h>
#define NETLINK_USER 31
#define MAX_PAYLOAD 1024
struct nlmsghdr *nlh = NULL;
struct sk_buff *skb_out;
int user_pid;
static int receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
{
int pid, res;
struct sk_buff *skb_out;
struct nlmsghdr *nlh_out;
char msg[MAX_PAYLOAD];
memset(msg, 0, MAX_PAYLOAD);
nlh_out = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, MAX_PAYLOAD, 0);
NETLINK_CB(skb_out).dst_group = 0;
strncpy(msg, "Message from kernel!", sizeof("Message from kernel!"));
res = nlmsg_unicast(nl_sk, skb_out, pid);
printk(KERN_INFO "Sent message to user!\n");
return 0;
}
static void nl_callback(struct sk_buff *skb)
{
int pid, res;
struct nlmsghdr *nlh;
char msg[MAX_PAYLOAD];
int err;
nlh = (struct nlmsghdr *)skb->data;
printk(KERN_INFO "Received netlink message payload: %s\n", (char*)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */
user_pid = pid;
skb_out = nlmsg_new(MAX_PAYLOAD, 0);
if (!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
receive_msg(skb, nlh);
return;
}
int init_module()
{
struct netlink_kernel_cfg cfg;
cfg.groups = 0;
cfg.input = nl_callback;
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk)
{
printk(KERN_ERR "Failed to create netlink socket\n");
return -10;
}
return 0;
}
void cleanup_module()
{
nlmsg_free(skb_out);
netlink_kernel_release(nl_sk);
}
```
阅读全文