Skip to contentSkip to conversation

bpf: xdp: Report bpf_prog ID in IFLA_XDP

PATCH Superseded Martin KaFai Lau 5 participants 19 messages Jun 13, 2017

Subsystems (13)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover bpf: xdp: Report bpf_prog ID in IFLA_XDP

This is the first usage of the new bpf_prog ID. It is for reporting the ID of a xdp_prog through netlink.

It rides on the existing IFLA_XDP. This patch adds IFLA_XDP_PROG_ID for the bpf_prog ID reporting.

It starts with changing the generic_xdp first. After that, the hardware driver is changed one by one. The final patch removes the prog_attached from 'struct netdev_xdp' because prog_id > 0 implies the presence of xdp_prog.

I have tested with generic_xdp, mlx4 and mlx5.

Martin KaFai Lau (10):
net: Add IFLA_XDP_PROG_ID
bpf: mlx4: Report bpf_prog ID during XDP_QUERY_PROG
bpf: mlx5e: Report bpf_prog ID during XDP_QUERY_PROG
bpf: virtio_net: Report bpf_prog ID during XDP_QUERY_PROG
bpf: bnxt: Report bpf_prog ID during XDP_QUERY_PROG
bpf: thunderx: Report bpf_prog ID during XDP_QUERY_PROG
bpf: ixgbe: Report bpf_prog ID during XDP_QUERY_PROG
bpf: nfp: Report bpf_prog ID during XDP_QUERY_PROG
bpf: qede: Report bpf_prog ID during XDP_QUERY_PROG
net: Remove prog_attached from struct netdev_xdp

 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c      | 11 ++++++--
 drivers/net/ethernet/cavium/thunder/nicvf_main.c   | 11 ++++++--
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c      | 11 ++++++--
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c     | 20 ++++++++++++--
 drivers/net/ethernet/mellanox/mlx5/core/en_main.c  | 14 ++++++++--
 .../net/ethernet/netronome/nfp/nfp_net_common.c    | 11 ++++++--
 drivers/net/ethernet/qlogic/qede/qede_filter.c     | 11 ++++++--
 drivers/net/virtio_net.c                           | 12 ++++----
 include/linux/netdevice.h                          |  4 +--
 include/uapi/linux/if_link.h                       |  1 +
 net/core/dev.c                                     | 15 +++++-----
 net/core/rtnetlink.c                               | 32 +++++++++++++++++-----
 12 files changed, 116 insertions(+), 37 deletions(-)
Signature · 1 line
2.9.3
#2 lore raw

PATCH 2/10 bpf: mlx4: Report bpf_prog ID during XDP_QUERY_PROG

Add support to mlx4 to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +17 -3
index c1de75fc399a..ba5233d6e19b 100644
@@ -2821,11 +2821,25 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
 	return err;
 }
 
-static bool mlx4_xdp_attached(struct net_device *dev)
+static u32 mlx4_xdp_query(struct net_device *dev)
 {
+	const struct bpf_prog *xdp_prog;
 	struct mlx4_en_priv *priv = netdev_priv(dev);
+	struct mlx4_en_dev *mdev = priv->mdev;
+	u32 prog_id = 0;
+
+	if (!priv->tx_ring_num[TX_XDP])
+		return prog_id;
+
+	mutex_lock(&mdev->state_lock);
+	xdp_prog = rcu_dereference_protected(
+		priv->rx_ring[0]->xdp_prog,
+		lockdep_is_held(&mdev->state_lock));
+	if (xdp_prog)
+		prog_id = xdp_prog->aux->id;
+	mutex_unlock(&mdev->state_lock);
 
-	return !!priv->tx_ring_num[TX_XDP];
+	return prog_id;
 }
 
 static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
@@ -2834,7 +2848,7 @@ static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	case XDP_SETUP_PROG:
 		return mlx4_xdp_set(dev, xdp->prog);
 	case XDP_QUERY_PROG:
-		xdp->prog_attached = mlx4_xdp_attached(dev);
+		xdp->prog_id = mlx4_xdp_query(dev);
 		return 0;
 	default:
 		return -EINVAL;
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/10 net: Add IFLA_XDP_PROG_ID

In reply to Martin KaFai Lau

Expose prog_id through IFLA_XDP_PROG_ID. This patch makes modification to generic_xdp. The later patches will modify other xdp-supported drivers.

prog_id is added to struct net_dev_xdp. It is redundant to prog_attached because prog_id > 0 implies prog_attached. Hence, prog_attached will be removed after other xdp-supported drivers can report prog_id during XDP_QUERY_PROG.

iproute2 patch will be followed. Here is how the 'ip link' will look like:

Quoted 1 line

ip link show eth0

3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 xdp(prog_id:1) qdisc fq_codel state UP mode DEFAULT group default qlen 1000

---
 include/linux/netdevice.h    |  3 ++-
 include/uapi/linux/if_link.h |  1 +
 net/core/dev.c               | 23 +++++++++++++++--------
 net/core/rtnetlink.c         | 27 +++++++++++++++++++++------
 4 files changed, 39 insertions(+), 15 deletions(-)
include/linux/netdevice.h +2 -1
index 524c7776ce96..dec4a2f25c2e 100644
@@ -826,6 +826,7 @@ struct netdev_xdp {
 		/* XDP_QUERY_PROG */
 		bool prog_attached;
 	};
+	u32 prog_id;
 };
 
 #ifdef CONFIG_XFRM_OFFLOAD
@@ -3302,7 +3303,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 typedef int (*xdp_op_t)(struct net_device *dev, struct netdev_xdp *xdp);
 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		      int fd, u32 flags);
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
+bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op, u32 *prog_id);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
include/uapi/linux/if_link.h +1 -0
index 8ed679fe603f..dd88375a6580 100644
@@ -907,6 +907,7 @@ enum {
 	IFLA_XDP_FD,
 	IFLA_XDP_ATTACHED,
 	IFLA_XDP_FLAGS,
+	IFLA_XDP_PROG_ID,
 	__IFLA_XDP_MAX,
 };
 
net/core/dev.c +15 -8
index 8f72f4a9c6ac..aa7c6eba5af6 100644
@@ -4341,13 +4341,12 @@ static struct static_key generic_xdp_needed __read_mostly;
 
 static int generic_xdp_install(struct net_device *dev, struct netdev_xdp *xdp)
 {
+	struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
 	struct bpf_prog *new = xdp->prog;
 	int ret = 0;
 
 	switch (xdp->command) {
-	case XDP_SETUP_PROG: {
-		struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
-
+	case XDP_SETUP_PROG:
 		rcu_assign_pointer(dev->xdp_prog, new);
 		if (old)
 			bpf_prog_put(old);
@@ -4359,10 +4358,12 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_xdp *xdp)
 			dev_disable_lro(dev);
 		}
 		break;
-	}
 
 	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!rcu_access_pointer(dev->xdp_prog);
+		if (old)
+			xdp->prog_id = old->aux->id;
+		else
+			xdp->prog_id = 0;
 		break;
 
 	default:
@@ -6930,7 +6931,8 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
 }
 EXPORT_SYMBOL(dev_change_proto_down);
 
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
+bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op,
+			u32 *prog_id)
 {
 	struct netdev_xdp xdp;
 
@@ -6939,6 +6941,11 @@ bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
 
 	/* Query must always succeed. */
 	WARN_ON(xdp_op(dev, &xdp) < 0);
+	if (xdp.prog_id)
+		xdp.prog_attached = true;
+	if (prog_id)
+		*prog_id = xdp.prog_id;
+
 	return xdp.prog_attached;
 }
 
@@ -6984,10 +6991,10 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		xdp_chk = generic_xdp_install;
 
 	if (fd >= 0) {
-		if (xdp_chk && __dev_xdp_attached(dev, xdp_chk))
+		if (xdp_chk && __dev_xdp_attached(dev, xdp_chk, NULL))
 			return -EEXIST;
 		if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
-		    __dev_xdp_attached(dev, xdp_op))
+		    __dev_xdp_attached(dev, xdp_op, NULL))
 			return -EBUSY;
 
 		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
net/core/rtnetlink.c +21 -6
index 7084f1db2446..f25a53b1fb92 100644
@@ -39,6 +39,7 @@
 #include <linux/if_vlan.h>
 #include <linux/pci.h>
 #include <linux/etherdevice.h>
+#include <linux/bpf.h>
 
 #include <linux/uaccess.h>
 
@@ -899,7 +900,8 @@ static size_t rtnl_port_size(const struct net_device *dev,
 static size_t rtnl_xdp_size(void)
 {
 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
-			  nla_total_size(1);	/* XDP_ATTACHED */
+			  nla_total_size(1) +	/* XDP_ATTACHED */
+			  nla_total_size(4);	/* XDP_PROG_ID */
 
 	return xdp_size;
 }
@@ -1247,15 +1249,20 @@ static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
 	return 0;
 }
 
-static u8 rtnl_xdp_attached_mode(struct net_device *dev)
+static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
 {
 	const struct net_device_ops *ops = dev->netdev_ops;
+	const struct bpf_prog *generic_xdp_prog;
 
 	ASSERT_RTNL();
 
-	if (rcu_access_pointer(dev->xdp_prog))
+	*prog_id = 0;
+	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
+	if (generic_xdp_prog) {
+		*prog_id = generic_xdp_prog->aux->id;
 		return XDP_ATTACHED_SKB;
-	if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp))
+	}
+	if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp, prog_id))
 		return XDP_ATTACHED_DRV;
 
 	return XDP_ATTACHED_NONE;
@@ -1264,6 +1271,7 @@ static u8 rtnl_xdp_attached_mode(struct net_device *dev)
 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
 {
 	struct nlattr *xdp;
+	u32 prog_id;
 	int err;
 
 	xdp = nla_nest_start(skb, IFLA_XDP);
@@ -1271,10 +1279,16 @@ static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
 		return -EMSGSIZE;
 
 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED,
-			 rtnl_xdp_attached_mode(dev));
+			 rtnl_xdp_attached_mode(dev, &prog_id));
 	if (err)
 		goto err_cancel;
 
+	if (prog_id) {
+		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
+		if (err)
+			goto err_cancel;
+	}
+
 	nla_nest_end(skb, xdp);
 	return 0;
 
@@ -1552,6 +1566,7 @@ static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
+	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
 };
 
 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
@@ -2224,7 +2239,7 @@ static int do_setlink(const struct sk_buff *skb,
 		if (err < 0)
 			goto errout;
 
-		if (xdp[IFLA_XDP_ATTACHED]) {
+		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
 			err = -EINVAL;
 			goto errout;
 		}
Signature · 1 line
2.9.3
#7 lore raw

PATCH 4/10 bpf: virtio_net: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to virtio_net to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/virtio_net.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/virtio_net.c +7 -5
index 1f8c15cb63b0..8e2d9e32dcda 100644
@@ -1955,16 +1955,18 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
 	return err;
 }
 
-static bool virtnet_xdp_query(struct net_device *dev)
+static u32 virtnet_xdp_query(struct net_device *dev)
 {
+	const struct bpf_prog *xdp_prog;
 	struct virtnet_info *vi = netdev_priv(dev);
 	int i;
 
 	for (i = 0; i < vi->max_queue_pairs; i++) {
-		if (vi->rq[i].xdp_prog)
-			return true;
+		xdp_prog = rtnl_dereference(vi->rq[i].xdp_prog);
+		if (xdp_prog)
+			return xdp_prog->aux->id;
 	}
-	return false;
+	return 0;
 }
 
 static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
@@ -1973,7 +1975,7 @@ static int virtnet_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	case XDP_SETUP_PROG:
 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
 	case XDP_QUERY_PROG:
-		xdp->prog_attached = virtnet_xdp_query(dev);
+		xdp->prog_id = virtnet_xdp_query(dev);
 		return 0;
 	default:
 		return -EINVAL;
Signature · 1 line
2.9.3
#8 lore raw

PATCH 5/10 bpf: bnxt: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to bnxt to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +9 -2
index 8ce793a0d030..5bf1c7ddb378 100644
@@ -216,10 +216,17 @@ int bnxt_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	case XDP_SETUP_PROG:
 		rc = bnxt_xdp_set(bp, xdp->prog);
 		break;
-	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!bp->xdp_prog;
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(bp->xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		rc = 0;
 		break;
+	}
 	default:
 		rc = -EINVAL;
 		break;
Signature · 1 line
2.9.3
#9 lore raw

PATCH 6/10 bpf: thunderx: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to thunderx to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/cavium/thunder/nicvf_main.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/cavium/thunder/nicvf_main.c +9 -2
index d6477af88085..00f8247f2a9a 100644
@@ -1761,9 +1761,16 @@ static int nicvf_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
 	switch (xdp->command) {
 	case XDP_SETUP_PROG:
 		return nicvf_xdp_setup(nic, xdp->prog);
-	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!nic->xdp_prog;
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(nic->xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.9.3
#3 lore raw

PATCH 3/10 bpf: mlx5e: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to mlx5e to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/mellanox/mlx5/core/en_main.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +11 -3
index 5afec0f4a658..c3ed2e122e12 100644
@@ -3599,11 +3599,19 @@ static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog)
 	return err;
 }
 
-static bool mlx5e_xdp_attached(struct net_device *dev)
+static u32 mlx5e_xdp_query(struct net_device *dev)
 {
+	const struct bpf_prog *xdp_prog;
 	struct mlx5e_priv *priv = netdev_priv(dev);
+	u32 prog_id = 0;
 
-	return !!priv->channels.params.xdp_prog;
+	mutex_lock(&priv->state_lock);
+	xdp_prog = READ_ONCE(priv->channels.params.xdp_prog);
+	if (xdp_prog)
+		prog_id = xdp_prog->aux->id;
+	mutex_unlock(&priv->state_lock);
+
+	return prog_id;
 }
 
 static int mlx5e_xdp(struct net_device *dev, struct netdev_xdp *xdp)
@@ -3612,7 +3620,7 @@ static int mlx5e_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	case XDP_SETUP_PROG:
 		return mlx5e_xdp_set(dev, xdp->prog);
 	case XDP_QUERY_PROG:
-		xdp->prog_attached = mlx5e_xdp_attached(dev);
+		xdp->prog_id = mlx5e_xdp_query(dev);
 		return 0;
 	default:
 		return -EINVAL;
Signature · 1 line
2.9.3
#10 lore raw

PATCH 7/10 bpf: ixgbe: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to ixgbe to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c +9 -2
index 812319ab77db..62fb564d62a2 100644
@@ -9799,9 +9799,16 @@ static int ixgbe_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	switch (xdp->command) {
 	case XDP_SETUP_PROG:
 		return ixgbe_xdp_setup(dev, xdp->prog);
-	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!(adapter->xdp_prog);
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(adapter->xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.9.3
#11 lore raw

PATCH 8/10 bpf: nfp: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to nfp to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/netronome/nfp/nfp_net_common.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +9 -2
index 49d1756d6a8e..e4d175853dc7 100644
@@ -3254,9 +3254,16 @@ static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp)
 	switch (xdp->command) {
 	case XDP_SETUP_PROG:
 		return nfp_net_xdp_setup(nn, xdp);
-	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!nn->dp.xdp_prog;
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(nn->dp.xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.9.3
#14 lore raw

PATCH 9/10 bpf: qede: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

Add support to qede to report bpf_prog ID during XDP_QUERY_PROG.

---
 drivers/net/ethernet/qlogic/qede/qede_filter.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/net/ethernet/qlogic/qede/qede_filter.c +9 -2
index 13955a3bd3b3..26b652ae5ec1 100644
@@ -1035,9 +1035,16 @@ int qede_xdp(struct net_device *dev, struct netdev_xdp *xdp)
 	switch (xdp->command) {
 	case XDP_SETUP_PROG:
 		return qede_xdp_set(edev, xdp->prog);
-	case XDP_QUERY_PROG:
-		xdp->prog_attached = !!edev->xdp_prog;
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(edev->xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.9.3
#15 lore raw

PATCH 10/10 net: Remove prog_attached from struct netdev_xdp

In reply to Martin KaFai Lau

prog_attached can be implied by prog_id (!!prog_id) and all drivers supporting xdp has been stopped setting prog_attached.

---
 include/linux/netdevice.h |  5 ++---
 net/core/dev.c            | 14 ++++----------
 net/core/rtnetlink.c      |  7 +++++--
 3 files changed, 11 insertions(+), 15 deletions(-)
include/linux/netdevice.h +2 -3
index dec4a2f25c2e..a4ac0821b903 100644
@@ -824,9 +824,8 @@ struct netdev_xdp {
 			struct netlink_ext_ack *extack;
 		};
 		/* XDP_QUERY_PROG */
-		bool prog_attached;
+		u32 prog_id;
 	};
-	u32 prog_id;
 };
 
 #ifdef CONFIG_XFRM_OFFLOAD
@@ -3303,7 +3302,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 typedef int (*xdp_op_t)(struct net_device *dev, struct netdev_xdp *xdp);
 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		      int fd, u32 flags);
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op, u32 *prog_id);
+u32 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
net/core/dev.c +4 -10
index aa7c6eba5af6..a484f946267c 100644
@@ -6931,8 +6931,7 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
 }
 EXPORT_SYMBOL(dev_change_proto_down);
 
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op,
-			u32 *prog_id)
+u32 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
 {
 	struct netdev_xdp xdp;
 
@@ -6941,12 +6940,7 @@ bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op,
 
 	/* Query must always succeed. */
 	WARN_ON(xdp_op(dev, &xdp) < 0);
-	if (xdp.prog_id)
-		xdp.prog_attached = true;
-	if (prog_id)
-		*prog_id = xdp.prog_id;
-
-	return xdp.prog_attached;
+	return xdp.prog_id;
 }
 
 static int dev_xdp_install(struct net_device *dev, xdp_op_t xdp_op,
@@ -6991,10 +6985,10 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		xdp_chk = generic_xdp_install;
 
 	if (fd >= 0) {
-		if (xdp_chk && __dev_xdp_attached(dev, xdp_chk, NULL))
+		if (xdp_chk && __dev_xdp_attached(dev, xdp_chk))
 			return -EEXIST;
 		if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) &&
-		    __dev_xdp_attached(dev, xdp_op, NULL))
+		    __dev_xdp_attached(dev, xdp_op))
 			return -EBUSY;
 
 		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
net/core/rtnetlink.c +5 -2
index f25a53b1fb92..feba0ec757c4 100644
@@ -1262,8 +1262,11 @@ static u8 rtnl_xdp_attached_mode(struct net_device *dev, u32 *prog_id)
 		*prog_id = generic_xdp_prog->aux->id;
 		return XDP_ATTACHED_SKB;
 	}
-	if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp, prog_id))
-		return XDP_ATTACHED_DRV;
+	if (ops->ndo_xdp) {
+		*prog_id = __dev_xdp_attached(dev, ops->ndo_xdp);
+		if (*prog_id)
+			return XDP_ATTACHED_DRV;
+	}
 
 	return XDP_ATTACHED_NONE;
 }
Signature · 1 line
2.9.3
#16 lore raw
In reply to Martin KaFai Lau
Quoted 10 lines · Martin KaFai Lau

This is the first usage of the new bpf_prog ID. It is for reporting the ID of a xdp_prog through netlink.

It rides on the existing IFLA_XDP. This patch adds IFLA_XDP_PROG_ID for the bpf_prog ID reporting.

It starts with changing the generic_xdp first. After that, the hardware driver is changed one by one. The final patch removes the prog_attached from 'struct netdev_xdp' because prog_id > 0 implies the presence of xdp_prog.

Daniel made the XDP_ATTACHED an enum to be able to extend it with other modes. I would appreciate if you didn't remove the member since I have this waiting in my tree:

From: Jakub Kicinski <[email protected]>
Date: Mon, 5 Jun 2017 22:52:01 -0700
Subject: [PATCH net-next] xdp: add reporting of offload mode

Extend the XDP_ATTACHED_* values to include offloaded mode. Let drivers report whether program is installed in the driver or the HW by changing the prog_attached field from bool to u8 (type of the netlink attribute).

---
 include/linux/netdevice.h    | 7 ++++---
 include/uapi/linux/if_link.h | 1 +
 net/core/dev.c               | 2 +-
 net/core/rtnetlink.c         | 6 +++---
 4 files changed, 9 insertions(+), 7 deletions(-)
include/linux/netdevice.h +4 -3
index 524c7776ce96..76165dfd2a1e 100644
@@ -808,7 +808,8 @@ enum xdp_netdev_command {
 	 */
 	XDP_SETUP_PROG,
 	/* Check if a bpf program is set on the device.  The callee should
-	 * return true if a program is currently attached and running.
+	 * set @prog_attached to one of XDP_ATTACHED_* values, note that "true"
+	 * is equivalent to XDP_ATTACHED_DRV.
 	 */
 	XDP_QUERY_PROG,
 };
@@ -824,7 +825,7 @@ struct netdev_xdp {
 			struct netlink_ext_ack *extack;
 		};
 		/* XDP_QUERY_PROG */
-		bool prog_attached;
+		u8 prog_attached;
 	};
 };
 
@@ -3302,7 +3303,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 typedef int (*xdp_op_t)(struct net_device *dev, struct netdev_xdp *xdp);
 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		      int fd, u32 flags);
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
+u8 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
include/uapi/linux/if_link.h +1 -0
index 8ed679fe603f..4dd59b7463a9 100644
@@ -900,6 +900,7 @@ enum {
 	XDP_ATTACHED_NONE = 0,
 	XDP_ATTACHED_DRV,
 	XDP_ATTACHED_SKB,
+	XDP_ATTACHED_HW,
 };
 
 enum {
net/core/dev.c +1 -1
index 8f72f4a9c6ac..8f29b6ba99ac 100644
@@ -6930,7 +6930,7 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
 }
 EXPORT_SYMBOL(dev_change_proto_down);
 
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
+u8 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
 {
 	struct netdev_xdp xdp;
 
net/core/rtnetlink.c +3 -3
index 7084f1db2446..5b16e479ea68 100644
@@ -1255,10 +1255,10 @@ static u8 rtnl_xdp_attached_mode(struct net_device *dev)
 
 	if (rcu_access_pointer(dev->xdp_prog))
 		return XDP_ATTACHED_SKB;
-	if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp))
-		return XDP_ATTACHED_DRV;
+	if (!ops->ndo_xdp)
+		return XDP_ATTACHED_NONE;
 
-	return XDP_ATTACHED_NONE;
+	return __dev_xdp_attached(dev, ops->ndo_xdp);
 }
 
 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
Signature · 1 line
2.11.0
#12 lore raw

Re: [PATCH net-next 08/10] bpf: nfp: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau

drivers/net/ethernet/netronome/nfp/nfp_net_common.c static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp) · line 3269

Quoted 13 lines · Martin KaFai Lau
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +9 -0
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(nn->dp.xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}

Why READ_ONCE? I thought .ndo_xdp is always called under RTNL.

#13 lore raw

Re: [PATCH net-next 08/10] bpf: nfp: Report bpf_prog ID during XDP_QUERY_PROG

Quoted 16 lines · Jakub Kicinski

drivers/net/ethernet/netronome/nfp/nfp_net_common.c static int nfp_net_xdp(struct net_device *netdev, struct netdev_xdp *xdp) · line 3269

Quoted 13 lines · Martin KaFai Lau
drivers/net/ethernet/netronome/nfp/nfp_net_common.c +9 -0
+	case XDP_QUERY_PROG: {
+		const struct bpf_prog *xdp_prog;
+
+		xdp_prog = READ_ONCE(nn->dp.xdp_prog);
+		if (xdp_prog)
+			xdp->prog_id = xdp_prog->aux->id;
+		else
+			xdp->prog_id = 0;
 		return 0;
+	}
 	default:
 		return -EINVAL;
 	}

Why READ_ONCE? I thought .ndo_xdp is always called under RTNL.

will remove in the next spin.

#17 lore raw
In reply to Jakub Kicinski
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · Martin KaFai Lau

This is the first usage of the new bpf_prog ID. It is for reporting the ID of a xdp_prog through netlink.

It rides on the existing IFLA_XDP. This patch adds IFLA_XDP_PROG_ID for the bpf_prog ID reporting.

Quoted 8 lines
Quoted 4 lines

It starts with changing the generic_xdp first. After that, the hardware driver is changed one by one. The final patch removes the prog_attached from 'struct netdev_xdp' because prog_id > 0 implies the presence of xdp_prog.

Daniel made the XDP_ATTACHED an enum to be able to extend it with other modes. I would appreciate if you didn't remove the member since I have this waiting in my tree:

I see. I will keep prog_attached then.

Quoted 5 lines

From: Jakub Kicinski <[email protected]>
Date: Mon, 5 Jun 2017 22:52:01 -0700
Subject: [PATCH net-next] xdp: add reporting of offload mode

Quoted 8 lines

Extend the XDP_ATTACHED_* values to include offloaded mode. Let drivers report whether program is installed in the driver or the HW by changing the prog_attached field from bool to u8 (type of the netlink attribute).

---
 include/linux/netdevice.h    | 7 ++++---

j

Quoted 83 lines
 include/uapi/linux/if_link.h | 1 +
 net/core/dev.c               | 2 +-
 net/core/rtnetlink.c         | 6 +++---
 4 files changed, 9 insertions(+), 7 deletions(-)
include/linux/netdevice.h +4 -3
index 524c7776ce96..76165dfd2a1e 100644
@@ -808,7 +808,8 @@ enum xdp_netdev_command {
 	 */
 	XDP_SETUP_PROG,
 	/* Check if a bpf program is set on the device.  The callee should
-	 * return true if a program is currently attached and running.
+	 * set @prog_attached to one of XDP_ATTACHED_* values, note that "true"
+	 * is equivalent to XDP_ATTACHED_DRV.
 	 */
 	XDP_QUERY_PROG,
 };
@@ -824,7 +825,7 @@ struct netdev_xdp {
 			struct netlink_ext_ack *extack;
 		};
 		/* XDP_QUERY_PROG */
-		bool prog_attached;
+		u8 prog_attached;
 	};
 };
 
@@ -3302,7 +3303,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 typedef int (*xdp_op_t)(struct net_device *dev, struct netdev_xdp *xdp);
 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 		      int fd, u32 flags);
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
+u8 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
include/uapi/linux/if_link.h +1 -0
index 8ed679fe603f..4dd59b7463a9 100644
@@ -900,6 +900,7 @@ enum {
 	XDP_ATTACHED_NONE = 0,
 	XDP_ATTACHED_DRV,
 	XDP_ATTACHED_SKB,
+	XDP_ATTACHED_HW,
 };
 
 enum {
net/core/dev.c +1 -1
index 8f72f4a9c6ac..8f29b6ba99ac 100644
@@ -6930,7 +6930,7 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
 }
 EXPORT_SYMBOL(dev_change_proto_down);
 
-bool __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
+u8 __dev_xdp_attached(struct net_device *dev, xdp_op_t xdp_op)
 {
 	struct netdev_xdp xdp;
 
net/core/rtnetlink.c +3 -4
index 7084f1db2446..5b16e479ea68 100644
@@ -1255,10 +1255,10 @@ static u8 rtnl_xdp_attached_mode(struct net_device *dev)
 
 	if (rcu_access_pointer(dev->xdp_prog))
 		return XDP_ATTACHED_SKB;
-	if (ops->ndo_xdp && __dev_xdp_attached(dev, ops->ndo_xdp))
-		return XDP_ATTACHED_DRV;
+	if (!ops->ndo_xdp)
+		return XDP_ATTACHED_NONE;
 
-	return XDP_ATTACHED_NONE;
+	return __dev_xdp_attached(dev, ops->ndo_xdp);
 }
 
 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
--

2.11.0

#18 lore raw
Quoted 13 lines · Martin KaFai Lau
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · Martin KaFai Lau

This is the first usage of the new bpf_prog ID. It is for reporting the ID of a xdp_prog through netlink.

It rides on the existing IFLA_XDP. This patch adds IFLA_XDP_PROG_ID for the bpf_prog ID reporting.

It starts with changing the generic_xdp first. After that, the hardware driver is changed one by one. The final patch removes the prog_attached from 'struct netdev_xdp' because prog_id > 0 implies the presence of xdp_prog.

Quoted 4 lines
Quoted 3 lines

Daniel made the XDP_ATTACHED an enum to be able to extend it with other modes. I would appreciate if you didn't remove the member since I have this waiting in my tree:

I see. I will keep prog_attached then.

Thanks!

Acked-by on v1 3/10
#4 lore raw

Re: [PATCH net-next 03/10] bpf: mlx5e: Report bpf_prog ID during XDP_QUERY_PROG

In reply to Martin KaFai Lau
Quoted 7 lines · Martin KaFai Lau

Add support to mlx5e to report bpf_prog ID during XDP_QUERY_PROG.

Thanks you Martin.

#5 lore raw

Re: [PATCH net-next 03/10] bpf: mlx5e: Report bpf_prog ID during XDP_QUERY_PROG

Quoted 10 lines · Saeed Mahameed
Quoted 7 lines · Martin KaFai Lau

Add support to mlx5e to report bpf_prog ID during XDP_QUERY_PROG.

Thanks for reviewing, Saeed.

I will respin v2 with READ_ONCE removed since it is already under rtnl lock per Jakub Kicinski's suggestion in nfp. The situation should be similar in mlx5e.

maintainer
#6 lore raw

Re: [PATCH net-next 03/10] bpf: mlx5e: Report bpf_prog ID during XDP_QUERY_PROG

Quoted 10 lines · Martin KaFai Lau
Quoted 9 lines · Saeed Mahameed
Quoted 7 lines · Martin KaFai Lau

Add support to mlx5e to report bpf_prog ID during XDP_QUERY_PROG.

Quoted 7 lines
Quoted 1 line

Thanks for reviewing, Saeed.

I will respin v2 with READ_ONCE removed since it is already under rtnl lock per Jakub Kicinski's suggestion in nfp. The situation should be similar in mlx5e.

Sounds good, yeah, only needed in data path wrt program updates.