libbpf 开发指南:打开一个已经固定在文件系统路径上的 eBPF 程序的链接

news/2024/7/5 4:09:34

目录

函数原型与解释

代码demo

makefile

cmake

期望输出


函数原型与解释

LIBBPF_API struct bpf_link * bpf_link__open(const char *path);

参数说明:

  • path:一个字符串,表示已固定 eBPF 程序的文件系统路径。

返回值:一个指向 bpf_link 结构的指针,表示 eBPF 程序的链接。如果打开链接失败,将返回 NULL。

代码demo

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>

SEC("kprobe/__x64_sys_getpid")
int bpf_prog1(struct pt_regs *ctx) {
    bpf_printk("sys_getpid called\n");
    return 0;
}

char _license[] SEC("license") = "GPL";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/bpf.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>

int main(int argc, char **argv) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <bpf_program.o> <pin_path>\n", argv[0]);
        return 1;
    }

    const char *bpf_program_path = argv[1];
    const char *pin_path = argv[2];
    struct bpf_object *obj = NULL;
    struct bpf_program *prog = NULL;
    struct bpf_link *link = NULL;

    if (bpf_prog_load(bpf_program_path, BPF_PROG_TYPE_KPROBE, &obj, &prog)) {
        fprintf(stderr, "Error loading BPF program: %s\n", strerror(errno));
        return 1;
    }

    link = bpf_program__attach(prog);
    if (libbpf_get_error(link)) {
        fprintf(stderr, "Error attaching BPF program: %s\n", strerror(errno));
        return 1;
    }

    if (bpf_link__pin(link, pin_path)) {
        fprintf(stderr, "Error pinning BPF link: %s\n", strerror(errno));
        return 1;
    }

    printf("BPF link pinned at: %s\n", pin_path);

    // Wait for user input to unpin and close
    printf("Press Enter to unpin and close the BPF link...\n");
    getchar();

    struct bpf_link *opened_link = bpf_link__open(pin_path);
    if (opened_link == NULL) {
        fprintf(stderr, "Error opening pinned BPF link: %s\n", strerror(errno));
        return 1;
    }

    bpf_link__unpin(opened_link, pin_path);
    printf("BPF link unpinned from: %s\n", pin_path);

    bpf_link__disconnect(opened_link);
    bpf_link__destroy(opened_link);
    bpf_link__destroy(link);
    bpf_object__unload(obj);

    return 0;
}

makefile

CC=gcc
CFLAGS=-I/usr/include/bpf -I./include
LDFLAGS=-lbpf

all: test

test: test.c
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

clean:
	rm -f test

cmake

cmake_minimum_required(VERSION 2.8)
project(test)

set(CMAKE_C_FLAGS "-I/usr/include/bpf -I./include")

add_executable(test test.c)
target_link_libraries(test bpf)

期望输出

make

./demo

BPF link pinned at: /sys/fs/bpf/my_bpf_link
Press Enter to unpin and close the BPF link...
BPF link unpinned from: /sys/fs/bpf/my_bpf_link


http://lihuaxi.xjx100.cn/news/1313844.html

相关文章

不同语言的连接符(JavaScript、PHP、Java、C语言、C语言、Python、C#、Ruby、Swift、Go、Rust、C++、MATLAB)

JavaScript JavaScript 使用加号 () 运算符来连接字符串。 var str1 "Hello"; var str2 "World"; var result str1 " " str2; console.log(result); // 输出&#xff1a;Hello WorldPHP PHP 使用点号 (.) 运算符来连接字符串。 $str1 …

宝塔安装Jenkins-图文小白教程

一、Jenkins包下载 大家可以从Jenkins官网&#xff08;https://www.jenkins.io/&#xff09;根据自己的需要下载最新的版本。 但Jenkins官网下载较慢&#xff0c;容易造成下载失败。可以去国内的开源镜像网站下载Jenkins最新版本。目前博主使用的是清华大学的开源镜像网站&…

安装和配置nginx(含https)

文章目录 安装Nginx配置单独的配置&#xff1a;https配置 nginx为什么可以处理高并发 安装Nginx sudo yum update sudo yum install epel-release sudo yum install nginx sudo systemctl start nginx安装好后可以打开自己的域名 看一下默认的页面 配置 具体参考Link 位置 …

【基础算法】贪心算法

贪心算法又称贪婪算法&#xff0c;是一种常见的算法思想。贪心算法的优点是效率高&#xff0c;实现较为简单&#xff0c;缺点是可能得不到最优解。 贪心算法的基本思想 贪心算法就是在求解问题时&#xff0c;总是做出当前看来最好的选择。也就是说贪心算法并不从整体最优上考…

伪类元素content,icon变形倾斜问题

![在这里插入图片描述](https://img-blog.csdnimg.cn/b58d128a80fd4a069a5e47cf2e87e256.png 检查发现原本设置了一个 font-style 为 italic&#xff0c;为倾斜样式 解决办法&#xff1a; font-style: normal;

seatunnel数据同步参数优化

执行引擎: Flink集群 数据同步脚本(tidb到tidb) env {job.name = tidb2tidbexecution.parallelism = 1job.mode = "BATCH"checkpoint.interval = 10000 } source {Jdbc {url = "jdbc:mysql://192.168.21.110:4000/test?useUnicode=true

骨传导耳机音质怎么样,推荐几款音质表现不错的骨传导耳机

最近体验了几款骨传导耳机&#xff0c;分享下我的使用感受。首先说一下为什么要选择骨传导耳机&#xff0c;我之前是使用入耳式耳机&#xff0c;戴久了耳朵会疼&#xff0c;而且晚上睡觉不能戴。于是就考虑骨传导耳机&#xff0c;因为骨传导耳机在传声的过程中不需要经过耳膜&a…

分布式中灰度方案就该这样设计!

一、背景简介 分布式系统中会存在这样的开发场景&#xff0c;不同需求可能涉及到对同一个服务的开发&#xff0c;那么该服务在研发期间就会存在多个版本并行的状态&#xff0c;为了保持不同版本之间的隔离性&#xff0c;验收需要将请求路由到指定版本号的服务上处理&#xff1…