代码之家  ›  专栏  ›  技术社区  ›  dippynark

通过bpf系统调用加载eBPF程序时,参数列表过长

  •  1
  • dippynark  · 技术社区  · 7 年前

    我正在尝试通过加载eBPF程序 bpf Go中的系统调用,但我看到系统调用返回了一个错误。为了限制问题,我正在使用以下最小eBPF程序,该程序不起任何作用:

    struct task_group {};    
    

    Go计划的重要部分如下:

    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 . 为什么会这样?或者更好的是,如何获得更详细的输出以找出问题的根本原因?

    从…起 here 只有三个地方 E2BIG (参数列表过长)从 bpf公司 系统调用,但它们似乎都不适合。

    如果需要的话,我可以提供一个更完整的代码版本,为了简洁起见,我只想去掉不相关的部分。

    为了帮助重新创建此问题,我在下面包含了完整的BPF程序。全部回购为 here :

    #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;
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Qeole    7 年前

    这里的问题是加载BPF字节码的方式。

    b, err := ioutil.ReadFile("bpf/bbf_tty.o")
    

    我从未使用过Go,但据我所知,它读取ELF对象文件中的所有字节,无需任何特定处理,并将它们馈送到 bpf() 稍后在代码中进行syscall。

    问题是,这不是事情的工作方式:当它编译成eBPF时,clang将您的程序放入 特定部分(默认情况下, .text ,但您可以指定其他名称)。此外,如果您使用eBPF映射,会发生一些神奇的事情(映射重新定位),以便您的ELF文件可以嵌入映射信息,并且您的用户空间程序调用 bpf() 可以检索它并将其发送到内核。

    因此,当您加载整个文件并将其发送到 bpf() ,则加载实际字节码,以及所有ELF节和头。内核可能不太喜欢它。我不知道如何在Go中修复它,但以下是一些可能有用的提示:

    • libbpf,一个可以从ELF文件加载eBPF程序的C库:位于 in the kernel tree .
    • Gobpf,将eBPF程序与Go一起使用的一些框架( link ). 我从未使用过它,但他们肯定会有一些代码从目标文件加载程序?
        2
  •  1
  •   pchaigno    7 年前

    有关此错误消息的实际原因,请参阅@Qeole的答案。

    您需要一个非空的BPF程序。否则,您将在中失败以下前提条件 bpf_prog_load :

    if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
        return -E2BIG;
    

    当前编译的BPF程序似乎为空,因为它不包含任何函数。因此 attr->insn_cnt 为空。


    细节 我查过了 属性->insn\U cnt公司 实际上为空:

    $ 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.
    

    对象文件不是空的,而是它的。应包含BPF指令的文本部分为。如果我跑步 readelf -x .text tmp.o 正如所料,在我自己的一个程序中,我得到了一个hextdump。

    推荐文章