Skip to content

Commit 026a4c2

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf, xdp: Implement LINK_UPDATE for BPF XDP link
Add support for LINK_UPDATE command for BPF XDP link to enable reliable replacement of underlying BPF program. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200722064603.3350758-6-andriin@fb.com
1 parent aa8d3a7 commit 026a4c2

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎net/core/dev.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8996,9 +8996,52 @@ static void bpf_xdp_link_dealloc(struct bpf_link *link)
89968996
kfree(xdp_link);
89978997
}
89988998

8999+
static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9000+
struct bpf_prog *old_prog)
9001+
{
9002+
struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9003+
enum bpf_xdp_mode mode;
9004+
bpf_op_t bpf_op;
9005+
int err = 0;
9006+
9007+
rtnl_lock();
9008+
9009+
/* link might have been auto-released already, so fail */
9010+
if (!xdp_link->dev) {
9011+
err = -ENOLINK;
9012+
goto out_unlock;
9013+
}
9014+
9015+
if (old_prog && link->prog != old_prog) {
9016+
err = -EPERM;
9017+
goto out_unlock;
9018+
}
9019+
old_prog = link->prog;
9020+
if (old_prog == new_prog) {
9021+
/* no-op, don't disturb drivers */
9022+
bpf_prog_put(new_prog);
9023+
goto out_unlock;
9024+
}
9025+
9026+
mode = dev_xdp_mode(xdp_link->flags);
9027+
bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9028+
err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9029+
xdp_link->flags, new_prog);
9030+
if (err)
9031+
goto out_unlock;
9032+
9033+
old_prog = xchg(&link->prog, new_prog);
9034+
bpf_prog_put(old_prog);
9035+
9036+
out_unlock:
9037+
rtnl_unlock();
9038+
return err;
9039+
}
9040+
89999041
static const struct bpf_link_ops bpf_xdp_link_lops = {
90009042
.release = bpf_xdp_link_release,
90019043
.dealloc = bpf_xdp_link_dealloc,
9044+
.update_prog = bpf_xdp_link_update,
90029045
};
90039046

90049047
int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)

0 commit comments

Comments
 (0)