xdp-project / xdp-tools

Utilities and example programs for use with XDP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: re-attached prog has empty target prog

vincentmli opened this issue · comments

Hi @tohojo

I am learning how libxdp works with kernel and trying to understand kernel internal :)

I have this bpftrace script to trace the attached prog target program and destination trampoline

#!/usr/bin/env bpftrace

/*
 * run bpftrace -l to find the function allowed by kprobe
 * kernel/bpf/syscall.c has 
 * static int bpf_tracing_prog_attach(struct bpf_prog *prog,
 *                                  int tgt_prog_fd,
 *                                  u32 btf_id,
 *                                  u64 bpf_cookie)
 *
 */

kprobe:bpf_tracing_prog_attach
{

$p = (struct bpf_prog *)arg0;

$prog_name = $p->aux->name;
$target_name = $p->aux->dst_prog->aux->name;
$target_id = $p->aux->dst_prog->aux->id;
$prog_dst_tramp_key = $p->aux->dst_trampoline->key;

printf("prog: %s\n target prog: %s\n target prog id: %d\n destination trampoline: %d\n\n",
         $prog_name, $target_name, $target_id, $prog_dst_tramp_key);

I used xdp-loader to load the libxdp test program xdp_pass.o and xdp_drop.o

I noticed if I xdp-loader load xdp_pass.o first, then xdp-loader load xdp_drop.o, re-attached xdp_pass shows empty target prog.

for example:


run xdp-loader load lo -m skb ./lib/testing/xdp_pass.o

bpftrace output:

prog: xdp_pass
 target prog: xdp_pass
 target prog id: 1291
 destination trampoline: 31

prog: xdp_pass
 target prog: xdp_dispatcher <=====
 target prog id: 1287
 destination trampoline: 29

run xdp-loader load lo -m skb ./lib/testing/xdp_pass.o

bpftrace output:

prog: xdp_pass
 target prog: xdp_pass
 target prog id: 1310
 destination trampoline: 31

prog: xdp_pass
 target prog:   <=====no target prog
 target prog id: 0
 destination trampoline: 0

prog: xdp_drop
 target prog: xdp_dispatcher <====
 target prog id: 1306
 destination trampoline: 9

I see the code below that seems match the bpftrace output

        /* Always clear the trampoline and target prog from prog->aux to make
         * sure the original attach destination is not kept alive after a
         * program is (re-)attached to another target.
         */
        if (prog->aux->dst_prog &&
            (tgt_prog_fd || tr != prog->aux->dst_trampoline))
                /* got extra prog ref from syscall, or attaching to different prog */
                bpf_prog_put(prog->aux->dst_prog);
        if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
                /* we allocated a new trampoline, so free the old one */
                bpf_trampoline_put(prog->aux->dst_trampoline);

        prog->aux->dst_prog = NULL;
        prog->aux->dst_trampoline = NULL;

my question is why not store the new target destination (xdp_dispatcher) to the re-attached xdp_pass prog? any problem with that?