Skip to content

Commit c8a36f1

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: xdp: Fix XDP mode when no mode flags specified
7f0a838 ("bpf, xdp: Maintain info on attached XDP BPF programs in net_device") inadvertently changed which XDP mode is assumed when no mode flags are specified explicitly. Previously, driver mode was preferred, if driver supported it. If not, generic SKB mode was chosen. That commit changed default to SKB mode always. This patch fixes the issue and restores the original logic. Fixes: 7f0a838 ("bpf, xdp: Maintain info on attached XDP BPF programs in net_device") Reported-by: Lorenzo Bianconi <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Tested-by: Lorenzo Bianconi <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5597432 commit c8a36f1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

net/core/dev.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8742,13 +8742,15 @@ struct bpf_xdp_link {
87428742
int flags;
87438743
};
87448744

8745-
static enum bpf_xdp_mode dev_xdp_mode(u32 flags)
8745+
static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
87468746
{
87478747
if (flags & XDP_FLAGS_HW_MODE)
87488748
return XDP_MODE_HW;
87498749
if (flags & XDP_FLAGS_DRV_MODE)
87508750
return XDP_MODE_DRV;
8751-
return XDP_MODE_SKB;
8751+
if (flags & XDP_FLAGS_SKB_MODE)
8752+
return XDP_MODE_SKB;
8753+
return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
87528754
}
87538755

87548756
static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
@@ -8896,7 +8898,7 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
88968898
return -EINVAL;
88978899
}
88988900

8899-
mode = dev_xdp_mode(flags);
8901+
mode = dev_xdp_mode(dev, flags);
89008902
/* can't replace attached link */
89018903
if (dev_xdp_link(dev, mode)) {
89028904
NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
@@ -8984,7 +8986,7 @@ static int dev_xdp_detach_link(struct net_device *dev,
89848986

89858987
ASSERT_RTNL();
89868988

8987-
mode = dev_xdp_mode(link->flags);
8989+
mode = dev_xdp_mode(dev, link->flags);
89888990
if (dev_xdp_link(dev, mode) != link)
89898991
return -EINVAL;
89908992

@@ -9080,7 +9082,7 @@ static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
90809082
goto out_unlock;
90819083
}
90829084

9083-
mode = dev_xdp_mode(xdp_link->flags);
9085+
mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
90849086
bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
90859087
err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
90869088
xdp_link->flags, new_prog);
@@ -9164,7 +9166,7 @@ int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
91649166
int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
91659167
int fd, int expected_fd, u32 flags)
91669168
{
9167-
enum bpf_xdp_mode mode = dev_xdp_mode(flags);
9169+
enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
91689170
struct bpf_prog *new_prog = NULL, *old_prog = NULL;
91699171
int err;
91709172

0 commit comments

Comments
 (0)