移除BPF尾部程序以读取环形缓冲区
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《移除BPF尾部程序以读取环形缓冲区》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
我观察到,如果我写入尾部程序中的环形缓冲区并从用户空间读取环形缓冲区,则尾部程序最终会被删除。 tail 程序不再显示在 bpftool prog 中。 bpftool 地图转储名称 jump_table 表示 found 0 elements;它最初有 1 个元素,即尾部程序。
该 bpf 程序由 main_prog 调用尾部程序组成。 tail 程序将 0 写入环形缓冲区。
#include#include struct bpf_map_def sec("maps") flow_ring_buf = { .type = bpf_map_type_ringbuf, .max_entries = 1<<12 }; struct bpf_map_def sec("maps") jump_table = { .type = bpf_map_type_prog_array, .key_size = sizeof(__u32), .value_size = sizeof(__u32), .max_entries = 1, }; sec("xdp") int main_prog(struct xdp_md *ctx) { bpf_tail_call(ctx, &jump_table, 0); bpf_printk("tail call failed"); return xdp_pass; } sec("xdp_2") int tail_prog(struct xdp_md *ctx) { __u32 num = 0; bpf_ringbuf_output(&flow_ring_buf, &num, sizeof(__u32), 0); return xdp_pass; } char _license[] sec("license") = "gpl";
这个 go 程序加载程序和映射并从环形缓冲区读取:
package main
import "c"
import (
"errors"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/ringbuf"
"github.com/vishvananda/netlink"
"log"
)
type bpfobjects struct {
mainprog *ebpf.program `ebpf:"main_prog"`
tailprog *ebpf.program `ebpf:"tail_prog"`
jumptable *ebpf.map `ebpf:"jump_table"`
flowringbuf *ebpf.map `ebpf:"flow_ring_buf"`
}
func main() {
var objects bpfobjects
spec, err := ebpf.loadcollectionspec("test.o")
if err != nil {
log.fatalln("ebpf.loadcollectionspec", err)
}
if err := spec.loadandassign(&objects, nil); err != nil {
log.fatalln("ebpf.loadandassign", err)
}
// update the jump table with the tail prog
if err = objects.jumptable.update(uint32(0), uint32(objects.tailprog.fd()), ebpf.updateany); err != nil {
log.fatalln("update prog_array", err)
}
link, err := netlink.linkbyname("enp0s8")
if err != nil {
log.fatalln("netlink.linkbyname", err)
}
// load the program onto the interface
if err = netlink.linksetxdpfdwithflags(link, objects.mainprog.fd(), 0x2); err != nil {
log.fatalln("netlink.linksetxdpfdwithflags:", err)
}
// a
// problem doesn't happen if you comment out code below and replace with select {}
reader, err := ringbuf.newreader(objects.flowringbuf)
for {
_, err := reader.read()
if err != nil {
if errors.is(err, ringbuf.errclosed) {
log.println("received signal, exiting..")
return
}
log.printf("reading from reader: %s", err)
continue
}
}
}
当我向接口发送流量时遇到了问题。 reader.read() 永远不会返回错误,并且返回的 record 对象具有 0。由于跳转表为空,尾部调用失败,我在内核日志中看到 bpf_printk 输出。
如果注释掉 a 下面的代码并将其替换为无限等待,例如 select {},我就不会遇到问题。
作为参考,等效的 c 程序可以正常运行。我正在使用 libbpf 0.7:
#include#include #include #include // Handles each insert into ring buffer static int flow_buf_sample(void *ctx, void *data, size_t len) { return 0; } int main(int argc, char *argv[]) { struct bpf_object *obj; struct bpf_map *jump_table; struct bpf_map *flow_ring_buf; struct bpf_program *tail_prog; struct bpf_program *main_prog; int err; if ((obj = bpf_object__open_file("test.o", NULL)) == NULL) { fprintf(stderr, "Could not open ELF"); return 1; } if ((err = bpf_object__load(obj)) < 0) { fprintf(stderr, "Could not load BPF"); return 1; } if ((jump_table = bpf_object__find_map_by_name(obj, "jump_table")) == NULL) { fprintf(stderr, "Could not find jump_table map"); return 1; } if ((flow_ring_buf = bpf_object__find_map_by_name(obj, "flow_ring_buf")) == NULL) { fprintf(stderr, "Could not find flow_ring_buf_map map"); return 1; } if ((main_prog = bpf_object__find_program_by_name(obj, "main_prog")) == NULL) { fprintf(stderr, "Could not find main_prog"); return 1; } if ((tail_prog = bpf_object__find_program_by_name(obj, "tail_prog")) == NULL) { fprintf(stderr, "Could not find tail_prog"); return 1; } struct ring_buffer *ring_buffer = ring_buffer__new(bpf_map__fd(flow_ring_buf), flow_buf_sample, NULL, NULL); if (ring_buffer == NULL) { fprintf(stderr, "failed to create ring buffer\n"); return 1; } int index0 = 0; int tail_prog_fd = bpf_program__fd(tail_prog); if ((err = bpf_map_update_elem(bpf_map__fd(jump_table), &index0, &tail_prog_fd, 0)) < 0) { fprintf(stderr, "failed update jump_table: %d", err); } int if_index = if_nametoindex(argv[1]); if (!if_index) { printf("get if_index from interface name failed\n"); return 1; } if ((err = bpf_xdp_attach(if_index, bpf_program__fd(main_prog), 0x2, NULL)) != 0) { fprintf(stderr, "bpf_xdp_attach failed: %d", err); return 1; } while(1) { ring_buffer__poll(ring_buffer, -1); } return 0; }
正确答案
发生这种情况是因为 objects.jumptable 在主程序循环运行时正在收集垃圾。删除引用后,地图 jump_table 将取消固定。解决方案是在调用 loadandassign() 后执行 defer objects.jumptable.close() 。或者添加此代码
func (o *bpfObjects) Close() {
o.MainProg.Close()
o.TailProg.Close()
o.JumpTable.Close()
o.FlowRingBuf.Close()
}
并在调用loadandassign()后调用defer对象.close()。这在 c 版本中不会发生,因为没有垃圾回收。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
使用Python开启创造力之旅:掌握这门语言,释放无限可能
- 上一篇
- 使用Python开启创造力之旅:掌握这门语言,释放无限可能
- 下一篇
- jQuery教程:如何一次性为元素设置多个属性值
-
- Golang · Go问答 | 11分钟前 |
- Go billy Chroot 之后绝对路径该怎样解释
- 131浏览 收藏
-
- Golang · Go问答 | 19分钟前 |
- Go afero WriteFile 为什么不会自动创建父目录
- 239浏览 收藏
-
- Golang · Go问答 | 23分钟前 | Go教程 · Go问答 · 文件系统 · Go afero CopyOnWriteFs CopyOnWriteFs 写入 base layer overlay 文件系统
- Go afero CopyOnWriteFs 为什么能读到基础层却无法改原文件
- 417浏览 收藏
-
- Golang · Go问答 | 24分钟前 |
- Go afero MemMapFs 与 OsFs 测试时有什么区别
- 272浏览 收藏
-
- Golang · Go问答 | 1小时前 | Go问答 · 输入校验 · Go Base64解码 CorruptInputError base64.Encoding.Strict
- Go base64.Encoding.Strict 遇到非零填充位时的解码排查
- 480浏览 收藏
-
- Golang · Go问答 | 1小时前 | Go问答 · Go encoding/binary binary.Uvarint 截断输入 变长整数
- Go binary.Uvarint 返回 n=0 时怎样处理截断输入
- 346浏览 收藏
-
- Golang · Go问答 | 1小时前 | 标准库 · 接口设计 · Go问答 · encoding/json · Go encoding/json json.RawMessage JSON字段 部分更新
- Go json.RawMessage 如何区分字段缺失和显式 null
- 445浏览 收藏
-
- Golang · Go问答 | 1小时前 | Go问答 · 持续集成 · Go 测试产物 testing.T.ArtifactDir go test -artifacts CI排障
- Go testing.T.ArtifactDir 怎样配合 -artifacts 保存测试产物
- 226浏览 收藏
-
- Golang · Go问答 | 8小时前 | go · https · net/http · GetCertificate TLS配置 Go ServeTLS TLSConfig.Certificates GetConfigForClient 内存证书
- Go http.Server.ServeTLS 证书文件为空时如何使用 TLS 配置
- 168浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 458次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 521次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 536次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 485次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 306次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go crypto/rand.Text 的长度为什么不是固定字符数
- 2026-10-04 501浏览
-
- Go strings.ToValidUTF8 清洗日志内容的边界
- 2026-10-03 501浏览
-
- Go tls.GetCertificate 为什么收不到空 ServerName 请求
- 2026-09-27 501浏览

