diff --git a/Documentation/networking/net_cachelines/net_device.rst b/Documentation/networking/net_cachelines/net_device.rst
index 8eceaa80b686..7620d3fcb8dc 100644
--- a/Documentation/networking/net_cachelines/net_device.rst
+++ b/Documentation/networking/net_cachelines/net_device.rst
@@ -169,6 +169,7 @@ unsigned_long:1 see_all_hwtstamp_requests
unsigned_long:1 change_proto_down
unsigned_long:1 netns_immutable
unsigned_long:1 fcoe_mtu
+unsigned_long:1 xdp_from_upper
struct list_head net_notifier_list
struct macsec_ops* macsec_ops
struct udp_tunnel_nic_info* udp_tunnel_nic_info
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index f18bafb4efe2..670fe869a875 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2531,6 +2531,12 @@ struct net_device {
unsigned long change_proto_down:1;
unsigned long netns_immutable:1;
unsigned long fcoe_mtu:1;
+ /**
+ * @xdp_from_upper: the program in @xdp_state was installed by an
+ * upper device with netif_xdp_propagate(); it belongs to the
+ * upper and can't be replaced or removed through this device.
+ */
+ unsigned long xdp_from_upper:1;
struct list_head net_notifier_list;
@@ -4445,6 +4451,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
u8 dev_xdp_prog_count(struct net_device *dev);
+bool dev_xdp_has_own_prog(struct net_device *dev);
int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
u8 dev_xdp_sb_prog_count(struct net_device *dev);
diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index 9249a79a168e..167503d1fd15 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -2327,7 +2327,7 @@ int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
.extack = extack,
};
- if (dev_xdp_prog_count(slave_dev) > 0) {
+ if (dev_xdp_has_own_prog(slave_dev)) {
SLAVE_NL_ERR(bond_dev, slave_dev, extack,
"Slave has XDP program loaded, please unload before enslaving");
res = -EOPNOTSUPP;@@ -5736,7 +5736,7 @@ static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
goto err;
}
- if (dev_xdp_prog_count(slave_dev) > 0) {
+ if (dev_xdp_has_own_prog(slave_dev)) {
SLAVE_NL_ERR(dev, slave_dev, extack,
"Slave has XDP program loaded, please unload before enslaving");
err = -EOPNOTSUPP;diff --git a/drivers/net/hyperv/netvsc_bpf.c b/drivers/net/hyperv/netvsc_bpf.c
index 14bb520b44ed..fa6be9c71c81 100644
--- a/drivers/net/hyperv/netvsc_bpf.c
+++ b/drivers/net/hyperv/netvsc_bpf.c
@@ -228,7 +228,7 @@ int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
/* The VF's own program is not ours: refuse to install over
* it, as bonding does, and leave it be on removal.
*/
- if (vf_netdev && dev_xdp_prog_count(vf_netdev)) {
+ if (vf_netdev && dev_xdp_has_own_prog(vf_netdev)) {
if (bpf->prog) {
NL_SET_ERR_MSG_MOD(extack,
"VF has an XDP program of its own");diff --git a/drivers/net/hyperv/netvsc_drv.c b/drivers/net/hyperv/netvsc_drv.c
index 61109ff9927f..6fb5c21b88dc 100644
--- a/drivers/net/hyperv/netvsc_drv.c
+++ b/drivers/net/hyperv/netvsc_drv.c
@@ -2332,7 +2332,7 @@ static int netvsc_register_vf(struct net_device *vf_netdev, int context)
return NOTIFY_DONE;
prog = netvsc_xdp_get(netvsc_dev);
- if (prog && dev_xdp_prog_count(vf_netdev)) {
+ if (prog && dev_xdp_has_own_prog(vf_netdev)) {
netdev_warn(ndev, "not using VF %s, it has an XDP program attached\n",
vf_netdev->name);
return NOTIFY_DONE;@@ -2485,7 +2485,8 @@ static int netvsc_unregister_vf(struct net_device *vf_netdev)
* reach netvsc, but leave a program of the VF's own be.
*/
nvdev = rtnl_dereference(net_device_ctx->nvdev);
- if (nvdev && netvsc_xdp_get(nvdev) && !dev_xdp_prog_count(vf_netdev)) {
+ if (nvdev && netvsc_xdp_get(nvdev) &&
+ !dev_xdp_has_own_prog(vf_netdev)) {
netdev_lock_ops(vf_netdev);
ret = netvsc_vf_setxdp(vf_netdev, NULL);
netdev_unlock_ops(vf_netdev);diff --git a/net/core/dev.c b/net/core/dev.c
index 69ee4342f43e..3a520c798d4a 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -9326,6 +9326,9 @@ static void __netdev_upper_dev_unlink(struct net_device *dev,
__netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
+ if (changeupper_info.master)
+ dev->xdp_from_upper = 0;
+
call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
&changeupper_info.info);
@@ -10561,6 +10564,15 @@ u8 dev_xdp_prog_count(struct net_device *dev)
}
EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
+/* Does the device have an XDP program, and was it installed directly on the
+ * device rather than propagated down by an upper with netif_xdp_propagate()?
+ */
+bool dev_xdp_has_own_prog(struct net_device *dev)
+{
+ return dev_xdp_prog_count(dev) && !dev->xdp_from_upper;
+}
+EXPORT_SYMBOL_GPL(dev_xdp_has_own_prog);
+
u8 dev_xdp_sb_prog_count(struct net_device *dev)
{
struct bpf_prog *prog;@@ -10591,6 +10603,11 @@ static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
{
+ struct bpf_prog *old_prog;
+ int err;
+
+ netdev_assert_locked_ops_compat(dev);
+
if (!dev->netdev_ops->ndo_bpf)
return -EOPNOTSUPP;
@@ -10598,6 +10615,12 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
if (bpf->command != XDP_SETUP_PROG)
return dev->netdev_ops->ndo_bpf(dev, bpf);
+ if (dev_xdp_has_own_prog(dev)) {
+ NL_SET_ERR_MSG(bpf->extack,
+ "unable to propagate XDP to device with an XDP program of its own");
+ return -EBUSY;
+ }
+
if (bpf->prog) {
if (dev->cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED &&
!bpf->prog->aux->xdp_has_frags) {@@ -10613,7 +10636,26 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
}
}
- return dev->netdev_ops->ndo_bpf(dev, bpf);
+ err = dev->netdev_ops->ndo_bpf(dev, bpf);
+ if (err)
+ return err;
+ if (bpf->prog)
+ bpf_prog_inc(bpf->prog);
+
+ /* Record it like a program of our own, so that everything which asks
+ * whether XDP is running here gets the right answer. @xdp_from_upper
+ * keeps the two apart where it matters. Register it with the
+ * dispatcher too, dev_xdp_uninstall() will drop it from there if this
+ * device goes away first.
+ */
+ old_prog = dev_xdp_prog(dev, XDP_MODE_DRV);
+ bpf_prog_change_xdp(old_prog, bpf->prog);
+ dev_xdp_set_prog(dev, XDP_MODE_DRV, bpf->prog);
+ dev->xdp_from_upper = !!bpf->prog;
+ if (old_prog)
+ bpf_prog_put(old_prog);
+
+ return 0;
}
EXPORT_SYMBOL_GPL(netif_xdp_propagate);
@@ -10731,6 +10773,7 @@ static void dev_xdp_uninstall(struct net_device *dev)
dev_xdp_set_link(dev, mode, NULL);
}
+ dev->xdp_from_upper = 0;
}
static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
@@ -10773,6 +10816,10 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
}
mode = dev_xdp_mode(dev, flags);
+ if (dev->xdp_from_upper) {
+ NL_SET_ERR_MSG(extack, "Can't replace an XDP program installed by an upper device");
+ return -EBUSY;
+ }
/* can't replace attached link */
if (dev_xdp_link(dev, mode)) {
NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");--
2.56.0