王张军
王张军

Reputation: 11

ebpf bpf_get_socket_uid error : invalid relo for insn[0].code 0x85

I build a ebpf demo in kernel source. demo code like this:

cgroup_kern.c

#include <uapi/linux/bpf.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/if_packet.h>
#include <uapi/linux/ip.h>
#include "bpf_helpers.h"

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


struct bpf_map_def SEC("maps") my_map = {
        .type = BPF_MAP_TYPE_ARRAY,
        .key_size = sizeof(u32),
        .value_size = sizeof(long),
        .max_entries = 256,
};

SEC("cgroupskb/ingress/stats")
int bpf_cgroup_ingress(struct __sk_buff* skb) {
        uint32_t sock_uid = bpf_get_socket_uid(skb);
        return 0;
}

cgroup_user.c

#include <stdio.h>
#include <assert.h>
#include <linux/bpf.h>
#include "libbpf.h"
#include "bpf_load.h"
#include "sock_example.h"
#include <unistd.h>
#include <arpa/inet.h>

int main(int ac, char **argv)
{
        char filename[256];
        FILE *f;
        int i, sock;

        snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

        if (load_bpf_file(filename)) {
                printf("%s", bpf_log_buf);
                return 1;
        }
        return 0;
}

Then , I add some Makefile for my cgroup ebpf demo. when I make ,got a warning:

/usr/src/linux-source-4.15.0/linux-source-4.15.0/samples/bpf/cgroup_kern.c:20:22: warning: implicit declaration of function 'bpf_get_socket_uid' is invalid in C99 [-Wimplicit-function-declaration]
        uint32_t sock_uid = bpf_get_socket_uid(skb);

when I want to attach my ebpf demo: invalid relo for insn[0].code 0x85

Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 397

Answers (1)

pchaigno
pchaigno

Reputation: 13063

I am guessing you retrieved bpf_helpers.h from the same kernel sources (4.15). Unfortunately, that file is missing several helper declarations in 4.15. This was fixed in 5.1 with commit f2bb538:

commit f2bb53887eb3e8f859ac7cfc09d1a3801492c009
Author: Willem de Bruijn <[email protected]>
Date:   Wed Feb 27 11:08:06 2019 -0500

    bpf: add missing entries to bpf_helpers.h
    
    This header defines the BPF functions enumerated in uapi/linux.bpf.h
    in a callable format. Expand to include all registered functions.
    
    Signed-off-by: Willem de Bruijn <[email protected]>
    Acked-by: Song Liu <[email protected]>
    Signed-off-by: Daniel Borkmann <[email protected]>

You should be able to fix the error simply by adding the proper definition to bpf_helpers.h:

static unsigned int (*bpf_get_socket_uid)(void *ctx) =
    (void *) BPF_FUNC_get_socket_uid;

Upvotes: 1

Related Questions