Search code examples
cgolinux-kernelebpfxdp-bpf

BPF tail call not called


In the following code BPF program tail_prog is not getting tail called from main_prog:

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

struct bpf_map_def SEC("maps") jump_table = {
   .type = BPF_MAP_TYPE_PROG_ARRAY,
   .key_size = sizeof(__u32),
   .value_size = sizeof(__u32),
   .max_entries = 8,
};

SEC("xdp")
int main_prog(struct xdp_md *ctx) {
    bpf_printk("Making tail call");
    bpf_tail_call(ctx, &jump_table, 0);

    return XDP_PASS;
}

SEC("xdp_1")
int tail_prog(struct xdp_md *ctx) {
    bpf_printk("Inside tail call");

    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";

I observe only the print in main_prog is printed.

I'm loading the BPF programs using Cilium's eBPF Go package. Here's the code for loading the programs and maps:

type BpfObjects struct {
    MainProg *ebpf.Program  `ebpf:"main_prog"`
    TailProg *ebpf.Program  `ebpf:"tail_prog"`
    JumpTable *ebpf.Map     `ebpf:"jump_table"`
}

    var objects BpfObjects

    spec, err := ebpf.LoadCollectionSpec("prog.o")

    if err != nil {
        log.Fatalln("ebpf.LoadCollectionSpec", err)
    }

    if err := spec.LoadAndAssign(&objects, nil); err != nil {
        log.Fatalln("ebpf.LoadAndAssign", err)
    }

    objects.JumpTable.Update(0, objects.TailProg.FD(), ebpf.UpdateAny)

According to this, the jump table has be initialized from user space, which is what I think the last line above is supposed to do. However, I don't see any difference whether that line is there or not.


Solution

  • I wasn't looking at the error returned from the Update function: Update can't marshal key: encoding int: binary.Write: invalid type int. Therefore, the program array map was not updated. I changed to the following:

        err = objects.JumpTable.Update(uint32(0), uint32(objects.CopyHttpHostnameProg.FD()), ebpf.UpdateAny)
    
        if err != nil {
            println("Update", err.Error())
        }
    

    If you pass 0 as the key, the size of the key is 8 bytes, which is why you have to do uint32(0), which matches the map's definition. Now the tail calls succeed.