[英]Argument list too long to when loading an eBPF program via the bpf syscall
我試圖通過Go中的bpf
系統調用加載一個eBPF程序,但是我看到系統調用返回了一個錯誤。 為了限制問題,我使用以下最小的eBPF程序,它什么都不做:
struct task_group {};
圍棋計划的重要部分如下:
b, err := ioutil.ReadFile("bpf/bbf_tty.o")
if err != nil {
fmt.Print(err)
}
progType := BPF_PROG_TYPE_KPROBE
insns := unsafe.Pointer(&b)
insnCnt := len(b)
lba := struct {
progType uint32
pad0 [4]byte
insnCnt uint32
pad1 [4]byte
insns uint64
license uint64
logLevel uint32
pad2 [4]byte
logSize uint32
pad3 [4]byte
logBuf uint64
kernVersion uint32
pad4 [4]byte
}{
progType: uint32(progType),
insns: uint64(uintptr(insns)),
insnCnt: uint32(insnCnt),
license: uint64(uintptr(0)),
logBuf: uint64(uintptr(0)),
logSize: uint32(0),
logLevel: uint32(0),
kernVersion: uint32(4),
}
ret, _, err := unix.Syscall(
unix.SYS_BPF,
bpf.BPF_PROG_LOAD,
uintptr(unsafe.Pointer(&lba)),
unsafe.Sizeof(lba),
)
if ret != 0 || err != 0 {
return fmt.Errorf("Unable to load program: %s", err)
}
但是,返回的錯誤是Unable to load program: argument list too long
。 為什么是這樣? 或者更好的是,我如何獲得更詳細的輸出以找出問題的根本原因?
從這里只有三個地方E2BIG
(參數列表太長)從bpf
系統調用返回,但它們似乎都不適合。
如果需要,我可以提供更完整的代碼版本,我只是為了簡潔而試圖刪除不相關的部分。
為了幫助重新創建此問題,我在下面提供了完整的BPF計划。 完整的回購在這里 :
#include <node_config.h>
#include <netdev_config.h>
#include <filter_config.h>
#include <bpf/api.h>
#include <stdint.h>
#include <stdio.h>
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include "lib/utils.h"
#include "lib/common.h"
#include "lib/maps.h"
#include "lib/xdp.h"
#include "lib/eps.h"
#include "lib/events.h"
// define structures
enum pid_type
{
PIDTYPE_PID,
PIDTYPE_PGID,
PIDTYPE_SID,
PIDTYPE_MAX,
// only valid to __task_pid_nr_ns()
__PIDTYPE_TGID
};
struct upid {
int nr;
};
struct pid
{
struct upid numbers[1];
};
struct pid_link
{
struct pid *pid;
};
struct task_group {
};
struct task_struct {
struct task_struct *group_leader;
struct pid_link pids[PIDTYPE_MAX];
};
struct sid_t {
int sid;
};
#define BUFSIZE 256
struct tty_write_t {
int count;
char buf[BUFSIZE];
unsigned int sessionid;
};
// define maps
struct bpf_elf_map __section_maps active_sids = {
.type = BPF_MAP_TYPE_HASH,
.size_key = sizeof(struct sid_t),
.size_value = sizeof(uint64_t),
};
struct bpf_elf_map __section_maps tty_writes = {
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
};
// save_sid saves a sessionid generated from a call
// to setsid to the active_sids map
int save_sid(struct pt_regs *ctx) {
struct sid_t sid_struct = {};
int sid = PT_REGS_RC(ctx);
uint64_t time_ns = bpf_ktime_get_ns();
sid_struct.sid = sid;
bpf_map_update(&sid_struct, &time_ns);
return 0;
}
//int kprobe__tty_write(struct pt_regs *ctx, struct file *file, const char __user *buf, size_t count)
int kprobe__tty_write(struct pt_regs *ctx, struct file *file, const char *buf, size_t count)
{
struct task_struct *task;
struct pid_link pid_link;
struct pid pid;
int sessionid;
// get current sessionid
task = (struct task_struct *)bpf_get_current_task();
bpf_probe_read(&pid_link, sizeof(pid_link), (void *)&task->group_leader->pids[PIDTYPE_SID]);
bpf_probe_read(&pid, sizeof(pid), (void *)pid_link.pid);
sessionid = pid.numbers[0].nr;
// build session struct key
struct sid_t sid_key;
sid_key.sid = sessionid;
// if sid does not exist in our map then return
//u64 *time_ns = active_sids.lookup(&sid_key);
//if (!time_ns) {
// return 0;
//}
// bpf_probe_read() can only use a fixed size, so truncate to count
// in user space:
struct tty_write_t tty_write = {};
bpf_probe_read(&tty_write.buf, BUFSIZE, (void *)buf);
if (count > BUFSIZE) {
tty_write.count = BUFSIZE;
} else {
tty_write.count = count;
}
// add sessionid to tty_write structure and submit
tty_write.sessionid = sessionid;
bpf_perf_event_output(ctx, &tty_write, sizeof(tty_write));
return 0;
}
這里的問題是您嘗試加載BPF字節碼的方式。
b, err := ioutil.ReadFile("bpf/bbf_tty.o")
我從來沒有使用過Go,但據我所知,這將讀取ELF目標文件中的所有字節,無需任何特定處理,並在代碼中稍后將它們提供給bpf()
系統調用。
事情是,這不是事情的工作方式:當它編譯成eBPF時,clang將你的程序放入一個特定的部分(默認情況下, .text
,但你可以指定另一個名字)。 此外,如果您使用eBPF地圖,會發生一些神奇的事情(“地圖重定位”),以便您的ELF文件可以嵌入地圖信息,而調用bpf()
用戶空間程序可以檢索它並將其發送到內核。
因此,當您加載整個文件以將其發送到bpf()
,您將加載實際的字節碼,以及所有ELF部分和標題。 內核可能不喜歡它。 我不知道如何在Go中修復它,但這里有一些可能有用的指針:
有關此錯誤消息的實際原因,請參閱@ Qeole的答案。
您需要一個非空的BPF程序。 否則,您將在bpf_prog_load
失敗以下前提條件:
if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
return -E2BIG;
您當前編譯的BPF程序似乎是空的,因為它不包含任何功能。 因此, attr->insn_cnt
為null。
細節我已經檢查過attr->insn_cnt
實際上是null:
$ cat tmp.c
struct task_group {};
$ clang -O2 -target bpf -c tmp.c -o tmp.o
$ ls -lh tmp.o
-rw-rw-r-- 1 paul paul 368 févr. 7 11:21 tmp.o
$ readelf -x .text tmp.o
Section '.text' has no data to dump.
目標文件不為空,但其.text部分應該包含BPF指令。 如果我在我自己的一個程序上運行readelf -x .text tmp.o
,我會得到一個hexdump,正如預期的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.