Skip to contentSkip to conversation

Add driver bpf hook for early packet drop and forwarding

PATCH Superseded Brenden Blanco 5 participants 25 messages Jul 11, 2016 – Jul 13, 2016

Subsystems (10)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v7 cover Add driver bpf hook for early packet drop and forwarding

This patch set introduces new infrastructure for programmatically processing packets in the earliest stages of rx, as part of an effort others are calling eXpress Data Path (XDP) [1]. Start this effort by introducing a new bpf program type for early packet filtering, before even an skb has been allocated.

Extend on this with the ability to modify packet data and send back out on the same port.

Patch 1 introduces the new prog type and helpers for validating the bpf
program. A new userspace struct is defined containing only data and data_end as fields, with others to follow in the future.
In patch 2, create a new ndo to pass the fd to supported drivers. In patch 3, expose a new rtnl option to userspace.
In patch 4, enable support in mlx4 driver.
In patch 5, create a sample drop and count program. With single core,
achieved ~20 Mpps drop rate on a 40G ConnectX3-Pro. This includes packet data access, bpf array lookup, and increment.
In patch 6, add a page recycle facility to mlx4 rx, enabled when xdp is
active.
In patch 7, add the XDP_TX type to bpf.h
In patch 8, add helper in tx patch for writing tx_desc
In patch 9, add support in mlx4 for packet data write and forwarding In patch 10, turn on packet write support in the bpf verifier In patch 11, add a sample program for packet write and forwarding. With
single core, achieved ~10 Mpps rewrite and forwarding.

[1] https://github.com/iovisor/bpf-docs/blob/master/Express_Data_Path.pdf

v7:
Addressing two of the major discussion points: return codes and ndo. The rest will be taken as todo items for separate patches.

Add an XDP_ABORTED type, which explicitly falls through to DROP. The same result must be taken for the default case as well, as it is now well-defined API behavior.

Merge ndo_xdp_* into a single ndo. The style is similar to ndo_setup_tc, but with less unidirectional naming convention. The IFLA parameter names are unchanged.

TODOs:
Add ethtool per-ring stats for aborted, default cases, maybe even drop and tx as well.
Avoid duplicate dma sync operation in XDP_PASS case as mentioned by Saeed.

1/12: Add XDP_ABORTED enum, reword API comment, and update commit
message.
2/12: Rewrite ndo_xdp_*() into single ndo_xdp() with type/union style
calling convention.
3/12: Switch to ndo_xdp callback.
4/12: Add XDP_ABORTED case as a fall-through to XDP_DROP. Implement
ndo_xdp.
12/12: Dropped, this will need some more work.

v6:
2/12: drop unnecessary netif_device_present check
4/12, 6/12, 9/12: Reorder default case statement above drop case to
remove some copy/paste.

v5:
0/12: Rebase and remove previous 1/13 patch
1/12: Fix nits from Daniel. Left the (void *) cast as-is, to be fixed
in future. Add bpf_warn_invalid_xdp_action() helper, to be used when out of bounds action is returned by the program. Add a comment to bpf.h denoting the undefined nature of out of bounds returns.
2/12: Switch to using bpf_prog_get_type(). Rename ndo_xdp_get() to

    ndo_xdp_attached().

3/12: Add IFLA_XDP as a nested type, and add the associated nla_policy
for the new subtypes IFLA_XDP_FD and IFLA_XDP_ATTACHED.
4/12: Fixup the use of READ_ONCE in the ndos. Add a user of
bpf_warn_invalid_xdp_action helper.
5/12: Adjust to using the nested netlink options.
6/12: kbuild was complaining about overflow of u16 on tile
architecture...bump frag_stride to u32. The page_offset member that is computed from this was already u32.

v4:
2/12: Add inline helper for calling xdp bpf prog under rcu 3/12: Add detail to ndo comments
5/12: Remove mlx4_call_xdp and use inline helper instead. 6/12: Fix checkpatch complaints
9/12: Introduce new patch 9/12 with common helper for tx_desc write
Refactor to use common tx_desc write helper
11/12: Fix checkpatch complaints

v3:
Rewrite from v2 trying to incorporate feedback from multiple sources. Specifically, add ability to forward packets out the same port and
allow packet modification.
For packet forwarding, the driver reserves a dedicated set of tx rings
for exclusive use by xdp. Upon completion, the pages on this ring are recycled directly back to a small per-rx-ring page cache without being dma unmapped.
Use of the percpu skb is dropped in favor of a lightweight struct
xdp_buff. The direct packet access feature is leveraged to remove dependence on the skb.
The mlx4 driver implementation allocates a page-per-packet and maps it
in PCI_DMA_BIDIRECTIONAL mode when the bpf program is activated.
Naming is converted to use "xdp" instead of "phys_dev".

v2:
1/5: Drop xdp from types, instead consistently use bpf_phys_dev_
Introduce enum for return values from phys_dev hook
2/5: Move prog->type check to just before invoking ndo
Change ndo to take a bpf_prog * instead of fd
Add ndo_bpf_get rather than keeping a bool in the netdev struct
3/5: Use ndo_bpf_get to fetch bool
4/5: Enforce that only 1 frag is ever given to bpf prog by disallowing
mtu to increase beyond FRAG_SZ0 when bpf prog is running, or conversely to set a bpf prog when priv->num_frags > 1
Rename pseudo_skb to bpf_phys_dev_md
Implement ndo_bpf_get
Add dma sync just before invoking prog
Check for explicit bpf return code rather than nonzero Remove increment of rx_dropped
5/5: Use explicit bpf return code in example
Update commit log with higher pps numbers

Brenden Blanco (11):
bpf: add XDP prog type for early driver filter
net: add ndo to setup/query xdp prog in adapter rx
rtnl: add option for setting link xdp prog net/mlx4_en: add support for fast rx drop bpf program Add sample for adding simple drop program to link net/mlx4_en: add page recycle to prepare rx ring for tx support
bpf: add XDP_TX xdp_action for direct forwarding net/mlx4_en: break out tx_desc write into separate function net/mlx4_en: add xdp forwarding and data write support
bpf: enable direct packet data write for xdp progs
bpf: add sample for xdp forwarding and rewrite

 drivers/infiniband/hw/mlx4/qp.c                 |  11 +-
 drivers/net/ethernet/mellanox/mlx4/en_ethtool.c |  17 +-
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c  | 106 +++++++++-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c      | 120 +++++++++--
 drivers/net/ethernet/mellanox/mlx4/en_tx.c      | 253 +++++++++++++++++++-----
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h    |  31 ++-
 include/linux/filter.h                          |  18 ++
 include/linux/mlx4/qp.h                         |  18 +-
 include/linux/netdevice.h                       |  32 +++
 include/uapi/linux/bpf.h                        |  21 ++
 include/uapi/linux/if_link.h                    |  12 ++
 kernel/bpf/verifier.c                           |  18 +-
 net/core/dev.c                                  |  33 ++++
 net/core/filter.c                               |  91 +++++++++
 net/core/rtnetlink.c                            |  64 ++++++
 samples/bpf/Makefile                            |   9 +
 samples/bpf/bpf_load.c                          |   8 +
 samples/bpf/xdp1_kern.c                         |  93 +++++++++
 samples/bpf/xdp1_user.c                         | 181 +++++++++++++++++
 samples/bpf/xdp2_kern.c                         | 114 +++++++++++
 20 files changed, 1162 insertions(+), 88 deletions(-)
 create mode 100644 samples/bpf/xdp1_kern.c
 create mode 100644 samples/bpf/xdp1_user.c
 create mode 100644 samples/bpf/xdp2_kern.c
Signature · 1 line
2.8.2
#1 lore raw

PATCH v7 1/11 bpf: add XDP prog type for early driver filter

Add a new bpf prog type that is intended to run in early stages of the packet rx path. Only minimal packet metadata will be available, hence a new context type, struct xdp_md, is exposed to userspace. So far only expose the packet start and end pointers, and only in read mode.

An XDP program must return one of the well known enum values, all other return codes are reserved for future use. Unfortunately, this restriction is hard to enforce at verification time, so take the approach of warning at runtime when such programs are encountered. Out of bounds return codes should alias to XDP_ABORTED.

---
 include/linux/filter.h   | 18 ++++++++++
 include/uapi/linux/bpf.h | 20 +++++++++++
 kernel/bpf/verifier.c    |  1 +
 net/core/filter.c        | 91 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 130 insertions(+)
include/linux/filter.h +18 -0
index 6fc31ef..522dbc9 100644
@@ -368,6 +368,11 @@ struct bpf_skb_data_end {
 	void *data_end;
 };
 
+struct xdp_buff {
+	void *data;
+	void *data_end;
+};
+
 /* compute the linear packet data range [data, data_end) which
  * will be accessed by cls_bpf and act_bpf programs
  */
@@ -429,6 +434,18 @@ static inline u32 bpf_prog_run_clear_cb(const struct bpf_prog *prog,
 	return BPF_PROG_RUN(prog, skb);
 }
 
+static inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog,
+				   struct xdp_buff *xdp)
+{
+	u32 ret;
+
+	rcu_read_lock();
+	ret = BPF_PROG_RUN(prog, (void *)xdp);
+	rcu_read_unlock();
+
+	return ret;
+}
+
 static inline unsigned int bpf_prog_size(unsigned int proglen)
 {
 	return max(sizeof(struct bpf_prog),
@@ -509,6 +526,7 @@ bool bpf_helper_changes_skb_data(void *func);
 
 struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
 				       const struct bpf_insn *patch, u32 len);
+void bpf_warn_invalid_xdp_action(int act);
 
 #ifdef CONFIG_BPF_JIT
 extern int bpf_jit_enable;
include/uapi/linux/bpf.h +20 -0
index 262a7e8..4282d44 100644
@@ -94,6 +94,7 @@ enum bpf_prog_type {
 	BPF_PROG_TYPE_SCHED_CLS,
 	BPF_PROG_TYPE_SCHED_ACT,
 	BPF_PROG_TYPE_TRACEPOINT,
+	BPF_PROG_TYPE_XDP,
 };
 
 #define BPF_PSEUDO_MAP_FD	1
@@ -437,4 +438,23 @@ struct bpf_tunnel_key {
 	__u32 tunnel_label;
 };
 
+/* User return codes for XDP prog type.
+ * A valid XDP program must return one of these defined values. All other
+ * return codes are reserved for future use. Unknown return codes will result
+ * in packet drop.
+ */
+enum xdp_action {
+	XDP_ABORTED = 0,
+	XDP_DROP,
+	XDP_PASS,
+};
+
+/* user accessible metadata for XDP packet hook
+ * new fields must be added to the end of this structure
+ */
+struct xdp_md {
+	__u32 data;
+	__u32 data_end;
+};
+
 #endif /* _UAPI__LINUX_BPF_H__ */
kernel/bpf/verifier.c +1 -0
index e206c21..a8d67d0 100644
@@ -713,6 +713,7 @@ static int check_ptr_alignment(struct verifier_env *env, struct reg_state *reg,
 	switch (env->prog->type) {
 	case BPF_PROG_TYPE_SCHED_CLS:
 	case BPF_PROG_TYPE_SCHED_ACT:
+	case BPF_PROG_TYPE_XDP:
 		break;
 	default:
 		verbose("verifier is misconfigured\n");
net/core/filter.c +91 -0

126 lines not on this page · raw message

Signature · 1 line
2.8.2
#4 lore raw

PATCH v7 2/11 net: add ndo to setup/query xdp prog in adapter rx

In reply to Brenden Blanco

Add one new netdev op for drivers implementing the BPF_PROG_TYPE_XDP filter. The single op is used for both setup/query of the xdp program, modelled after ndo_setup_tc.

---
 include/linux/netdevice.h | 32 ++++++++++++++++++++++++++++++++
 net/core/dev.c            | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
include/linux/netdevice.h +32 -0
index 49736a3..2f04746 100644
@@ -63,6 +63,7 @@ struct wpan_dev;
 struct mpls_dev;
 /* UDP Tunnel offloads */
 struct udp_tunnel_info;
+struct bpf_prog;
 
 void netdev_set_default_ethtool_ops(struct net_device *dev,
 				    const struct ethtool_ops *ops);
@@ -799,6 +800,31 @@ struct tc_to_netdev {
 	};
 };
 
+/* These structures hold the attributes of xdp state that are being passed
+ * to the netdevice through the xdp op.
+ */
+enum xdp_netdev_command {
+	/* Set or clear a bpf program used in the earliest stages of packet
+	 * rx. The prog will have been loaded as BPF_PROG_TYPE_XDP. The callee
+	 * is responsible for calling bpf_prog_put on any old progs that are
+	 * stored, but not on the passed in prog.
+	 */
+	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.
+	 */
+	XDP_QUERY_PROG,
+};
+
+struct netdev_xdp {
+	enum xdp_netdev_command command;
+	union {
+		/* XDP_SETUP_PROG */
+		struct bpf_prog *prog;
+		/* XDP_QUERY_PROG */
+		bool prog_attached;
+	};
+};
 
 /*
  * This structure defines the management hooks for network devices.
@@ -1087,6 +1113,9 @@ struct tc_to_netdev {
  *	appropriate rx headroom value allows avoiding skb head copy on
  *	forward. Setting a negative value resets the rx headroom to the
  *	default value.
+ * int (*ndo_xdp)(struct net_device *dev, struct netdev_xdp *xdp);
+ *	This function is used to set or query state related to XDP on the
+ *	netdevice. See definition of enum xdp_netdev_command for details.
  *
  */
 struct net_device_ops {
@@ -1271,6 +1300,8 @@ struct net_device_ops {
 						       struct sk_buff *skb);
 	void			(*ndo_set_rx_headroom)(struct net_device *dev,
 						       int needed_headroom);
+	int			(*ndo_xdp)(struct net_device *dev,
+					   struct netdev_xdp *xdp);
 };
 
 /**
@@ -3257,6 +3288,7 @@ int dev_get_phys_port_id(struct net_device *dev,
 int dev_get_phys_port_name(struct net_device *dev,
 			   char *name, size_t len);
 int dev_change_proto_down(struct net_device *dev, bool proto_down);
+int dev_change_xdp_fd(struct net_device *dev, int fd);
 struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev);
 struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
 				    struct netdev_queue *txq, int *ret);
net/core/dev.c +33 -0
index 7894e40..2a9c39f 100644
@@ -94,6 +94,7 @@
 #include <linux/ethtool.h>
 #include <linux/notifier.h>
 #include <linux/skbuff.h>
+#include <linux/bpf.h>
 #include <net/net_namespace.h>
 #include <net/sock.h>
 #include <net/busy_poll.h>
@@ -6615,6 +6616,38 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down)
 EXPORT_SYMBOL(dev_change_proto_down);
 
 /**
+ *	dev_change_xdp_fd - set or clear a bpf program for a device rx path
+ *	@dev: device
+ *	@fd: new program fd or negative value to clear
+ *
+ *	Set or clear a bpf program for a device
+ */
+int dev_change_xdp_fd(struct net_device *dev, int fd)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+	struct bpf_prog *prog = NULL;
+	struct netdev_xdp xdp = {};
+	int err;
+
+	if (!ops->ndo_xdp)
+		return -EOPNOTSUPP;
+	if (fd >= 0) {
+		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
+		if (IS_ERR(prog))
+			return PTR_ERR(prog);
+	}
+
+	xdp.command = XDP_SETUP_PROG;
+	xdp.prog = prog;
+	err = ops->ndo_xdp(dev, &xdp);
+	if (err < 0 && prog)
+		bpf_prog_put(prog);
+
+	return err;
+}
+EXPORT_SYMBOL(dev_change_xdp_fd);
+
+/**
  *	dev_new_index	-	allocate an ifindex
  *	@net: the applicable net namespace
  *
Signature · 1 line
2.8.2
#8 lore raw

PATCH v7 3/11 rtnl: add option for setting link xdp prog

In reply to Brenden Blanco

Sets the bpf program represented by fd as an early filter in the rx path of the netdev. The fd must have been created as BPF_PROG_TYPE_XDP. Providing a negative value as fd clears the program. Getting the fd back via rtnl is not possible, therefore reading of this value merely provides a bool whether the program is valid on the link or not.

---
 include/uapi/linux/if_link.h | 12 +++++++++
 net/core/rtnetlink.c         | 64 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
include/uapi/linux/if_link.h +12 -0
index 4285ac3..a1b5202 100644
@@ -156,6 +156,7 @@ enum {
 	IFLA_GSO_MAX_SEGS,
 	IFLA_GSO_MAX_SIZE,
 	IFLA_PAD,
+	IFLA_XDP,
 	__IFLA_MAX
 };
 
@@ -843,4 +844,15 @@ enum {
 };
 #define LINK_XSTATS_TYPE_MAX (__LINK_XSTATS_TYPE_MAX - 1)
 
+/* XDP section */
+
+enum {
+	IFLA_XDP_UNSPEC,
+	IFLA_XDP_FD,
+	IFLA_XDP_ATTACHED,
+	__IFLA_XDP_MAX,
+};
+
+#define IFLA_XDP_MAX (__IFLA_XDP_MAX - 1)
+
 #endif /* _UAPI_LINUX_IF_LINK_H */
net/core/rtnetlink.c +64 -0

106 lines not on this page · raw message

Signature · 1 line
2.8.2
#9 lore raw

PATCH v7 4/11 net/mlx4_en: add support for fast rx drop bpf program

In reply to Brenden Blanco

Add support for the BPF_PROG_TYPE_XDP hook in mlx4 driver.

In tc/socket bpf programs, helpers linearize skb fragments as needed when the program touches the packet data. However, in the pursuit of speed, XDP programs will not be allowed to use these slower functions, especially if it involves allocating an skb.

Therefore, disallow MTU settings that would produce a multi-fragment packet that XDP programs would fail to access. Future enhancements could be done to increase the allowable MTU.

---
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c | 49 ++++++++++++++++++++++++++
 drivers/net/ethernet/mellanox/mlx4/en_rx.c     | 37 ++++++++++++++++---
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h   |  5 +++
 3 files changed, 87 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +49 -0
index 6083775..31070f9 100644
@@ -31,6 +31,7 @@
  *
  */
 
+#include <linux/bpf.h>
 #include <linux/etherdevice.h>
 #include <linux/tcp.h>
 #include <linux/if_vlan.h>
@@ -2084,6 +2085,9 @@ void mlx4_en_destroy_netdev(struct net_device *dev)
 	if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
 		mlx4_en_remove_timestamp(mdev);
 
+	if (priv->prog)
+		bpf_prog_put(priv->prog);
+
 	/* Detach the netdev so tasks would not attempt to access it */
 	mutex_lock(&mdev->state_lock);
 	mdev->pndev[priv->port] = NULL;
@@ -2112,6 +2116,11 @@ static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
 		en_err(priv, "Bad MTU size:%d.\n", new_mtu);
 		return -EPERM;
 	}
+	if (priv->prog && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
+		en_err(priv, "MTU size:%d requires frags but bpf prog running",
+		       new_mtu);
+		return -EOPNOTSUPP;
+	}
 	dev->mtu = new_mtu;
 
 	if (netif_running(dev)) {
@@ -2520,6 +2529,44 @@ static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 m
 	return err;
 }
 
+static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
+{
+	struct mlx4_en_priv *priv = netdev_priv(dev);
+	struct bpf_prog *old_prog;
+
+	if (priv->num_frags > 1)
+		return -EOPNOTSUPP;
+
+	/* This xchg is paired with READ_ONCE in the fast path, but is
+	 * also protected from itself via rtnl lock
+	 */
+	old_prog = xchg(&priv->prog, prog);
+	if (old_prog)
+		bpf_prog_put(old_prog);
+
+	return 0;
+}
+
+static bool mlx4_xdp_attached(struct net_device *dev)
+{
+	struct mlx4_en_priv *priv = netdev_priv(dev);
+
+	return !!READ_ONCE(priv->prog);
+}
+
+static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
+{
+	switch (xdp->command) {
+	case XDP_SETUP_PROG:
+		return mlx4_xdp_set(dev, xdp->prog);
+	case XDP_QUERY_PROG:
+		xdp->prog_attached = mlx4_xdp_attached(dev);
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
 static const struct net_device_ops mlx4_netdev_ops = {
 	.ndo_open		= mlx4_en_open,
 	.ndo_stop		= mlx4_en_close,
@@ -2548,6 +2595,7 @@ static const struct net_device_ops mlx4_netdev_ops = {
 	.ndo_udp_tunnel_del	= mlx4_en_del_vxlan_port,
 	.ndo_features_check	= mlx4_en_features_check,
 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
+	.ndo_xdp		= mlx4_xdp,
 };
 
 static const struct net_device_ops mlx4_netdev_ops_master = {
@@ -2584,6 +2632,7 @@ static const struct net_device_ops mlx4_netdev_ops_master = {
 	.ndo_udp_tunnel_del	= mlx4_en_del_vxlan_port,
 	.ndo_features_check	= mlx4_en_features_check,
 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
+	.ndo_xdp		= mlx4_xdp,
 };
 
 struct mlx4_en_bond {
drivers/net/ethernet/mellanox/mlx4/en_rx.c +33 -4
index c1b3a9c..adfa123 100644
@@ -743,6 +743,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 	struct mlx4_en_rx_ring *ring = priv->rx_ring[cq->ring];
 	struct mlx4_en_rx_alloc *frags;
 	struct mlx4_en_rx_desc *rx_desc;
+	struct bpf_prog *prog;
 	struct sk_buff *skb;
 	int index;
 	int nr;
@@ -759,6 +760,8 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 	if (budget <= 0)
 		return polled;
 
+	prog = READ_ONCE(priv->prog);
+
 	/* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
 	 * descriptor offset can be deduced from the CQE index instead of
 	 * reading 'cqe->index' */
@@ -835,6 +838,35 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 		l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
 			(cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
 
+		/* A bpf program gets first chance to drop the packet. It may
+		 * read bytes but not past the end of the frag.
+		 */
+		if (prog) {
+			struct xdp_buff xdp;
+			dma_addr_t dma;
+			u32 act;
+
+			dma = be64_to_cpu(rx_desc->data[0].addr);
+			dma_sync_single_for_cpu(priv->ddev, dma,
+						priv->frag_info[0].frag_size,
+						DMA_FROM_DEVICE);
+
+			xdp.data = page_address(frags[0].page) +
+							frags[0].page_offset;
+			xdp.data_end = xdp.data + length;
+
+			act = bpf_prog_run_xdp(prog, &xdp);
+			switch (act) {
+			case XDP_PASS:
+				break;
+			default:
+				bpf_warn_invalid_xdp_action(act);
+			case XDP_ABORTED:
+			case XDP_DROP:
+				goto next;
+			}
+		}
+
 		if (likely(dev->features & NETIF_F_RXCSUM)) {
 			if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
 						      MLX4_CQE_STATUS_UDP)) {
@@ -1062,10 +1094,7 @@ static const int frag_sizes[] = {
 void mlx4_en_calc_rx_buf(struct net_device *dev)
 {
 	struct mlx4_en_priv *priv = netdev_priv(dev);
-	/* VLAN_HLEN is added twice,to support skb vlan tagged with multiple
-	 * headers. (For example: ETH_P_8021Q and ETH_P_8021AD).
-	 */
-	int eff_mtu = dev->mtu + ETH_HLEN + (2 * VLAN_HLEN);
+	int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
 	int buf_size = 0;
 	int i = 0;
 
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +5 -0
index d39bf59..35ecfa2 100644
@@ -164,6 +164,10 @@ enum {
 #define MLX4_LOOPBACK_TEST_PAYLOAD (HEADER_COPY_SIZE - ETH_HLEN)
 
 #define MLX4_EN_MIN_MTU		46
+/* VLAN_HLEN is added twice,to support skb vlan tagged with multiple
+ * headers. (For example: ETH_P_8021Q and ETH_P_8021AD).
+ */
+#define MLX4_EN_EFF_MTU(mtu)	((mtu) + ETH_HLEN + (2 * VLAN_HLEN))
 #define ETH_BCAST		0xffffffffffffULL
 
 #define MLX4_EN_LOOPBACK_RETRIES	5
@@ -590,6 +594,7 @@ struct mlx4_en_priv {
 	struct hlist_head mac_hash[MLX4_EN_MAC_HASH_SIZE];
 	struct hwtstamp_config hwtstamp_config;
 	u32 counter_index;
+	struct bpf_prog *prog;
 
 #ifdef CONFIG_MLX4_EN_DCB
 #define MLX4_EN_DCB_ENABLED	0x3
Signature · 1 line
2.8.2
#12 lore raw

PATCH v7 5/11 Add sample for adding simple drop program to link

In reply to Brenden Blanco

Add a sample program that only drops packets at the BPF_PROG_TYPE_XDP_RX hook of a link. With the drop-only program, observed single core rate is ~20Mpps.

Other tests were run, for instance without the dropcnt increment or without reading from the packet header, the packet rate was mostly unchanged.

$ perf record -a samples/bpf/xdp1 $(</sys/class/net/eth0/ifindex)
proto 17:   20403027 drops/s

./pktgen_sample03_burst_single_flow.sh -i $DEV -d $IP -m $MAC -t 4 Running... ctrl^C to stop
Device: eth4@0
Result: OK: 11791017(c11788327+d2689) usec, 59622913 (60byte,0frags)
5056638pps 2427Mb/sec (2427186240bps) errors: 0
Device: eth4@1
Result: OK: 11791012(c11787906+d3106) usec, 60526944 (60byte,0frags)
5133311pps 2463Mb/sec (2463989280bps) errors: 0
Device: eth4@2
Result: OK: 11791019(c11788249+d2769) usec, 59868091 (60byte,0frags)
5077431pps 2437Mb/sec (2437166880bps) errors: 0
Device: eth4@3
Result: OK: 11795039(c11792403+d2636) usec, 59483181 (60byte,0frags)
5043067pps 2420Mb/sec (2420672160bps) errors: 0

perf report --no-children:

 26.05%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_process_rx_cq
 17.84%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_alloc_frags
  5.52%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_free_frag
  4.90%  swapper      [kernel.vmlinux]  [k] poll_idle
  4.14%  ksoftirqd/0  [kernel.vmlinux]  [k] get_page_from_freelist
  2.78%  ksoftirqd/0  [kernel.vmlinux]  [k] __free_pages_ok
  2.57%  ksoftirqd/0  [kernel.vmlinux]  [k] bpf_map_lookup_elem
  2.51%  swapper      [mlx4_en]         [k] mlx4_en_process_rx_cq
  1.94%  ksoftirqd/0  [kernel.vmlinux]  [k] percpu_array_map_lookup_elem
  1.45%  swapper      [mlx4_en]         [k] mlx4_en_alloc_frags
  1.35%  ksoftirqd/0  [kernel.vmlinux]  [k] free_one_page
  1.33%  swapper      [kernel.vmlinux]  [k] intel_idle
  1.04%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c5c5
  0.96%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c58d
  0.93%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c6ee
  0.92%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c6b9
  0.89%  ksoftirqd/0  [kernel.vmlinux]  [k] __alloc_pages_nodemask
  0.83%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c686
  0.83%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c5d5
  0.78%  ksoftirqd/0  [mlx4_en]         [k] mlx4_alloc_pages.isra.23
  0.77%  ksoftirqd/0  [mlx4_en]         [k] 0x000000000001c5b4
  0.77%  ksoftirqd/0  [kernel.vmlinux]  [k] net_rx_action

machine specs:
receiver - Intel E5-1630 v3 @ 3.70GHz
sender - Intel E5645 @ 2.40GHz
Mellanox ConnectX-3 @40G

---
 samples/bpf/Makefile    |   4 ++
 samples/bpf/bpf_load.c  |   8 +++
 samples/bpf/xdp1_kern.c |  93 +++++++++++++++++++++++++
 samples/bpf/xdp1_user.c | 181 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 286 insertions(+)
 create mode 100644 samples/bpf/xdp1_kern.c
 create mode 100644 samples/bpf/xdp1_user.c
samples/bpf/Makefile +4 -0
index a98b780..0e4ab3a 100644
@@ -21,6 +21,7 @@ hostprogs-y += spintest
 hostprogs-y += map_perf_test
 hostprogs-y += test_overhead
 hostprogs-y += test_cgrp2_array_pin
+hostprogs-y += xdp1
 
 test_verifier-objs := test_verifier.o libbpf.o
 test_maps-objs := test_maps.o libbpf.o
@@ -42,6 +43,7 @@ spintest-objs := bpf_load.o libbpf.o spintest_user.o
 map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o
 test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o
 test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o
+xdp1-objs := bpf_load.o libbpf.o xdp1_user.o
 
 # Tell kbuild to always build the programs
 always := $(hostprogs-y)
@@ -64,6 +66,7 @@ always += test_overhead_tp_kern.o
 always += test_overhead_kprobe_kern.o
 always += parse_varlen.o parse_simple.o parse_ldabs.o
 always += test_cgrp2_tc_kern.o
+always += xdp1_kern.o
 
 HOSTCFLAGS += -I$(objtree)/usr/include
 
@@ -84,6 +87,7 @@ HOSTLOADLIBES_offwaketime += -lelf
 HOSTLOADLIBES_spintest += -lelf
 HOSTLOADLIBES_map_perf_test += -lelf -lrt
 HOSTLOADLIBES_test_overhead += -lelf -lrt
+HOSTLOADLIBES_xdp1 += -lelf
 
 # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
 #  make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/bpf_load.c +8 -0
index 022af71..0cfda23 100644
@@ -50,6 +50,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
 	bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
 	bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
 	bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
+	bool is_xdp = strncmp(event, "xdp", 3) == 0;
 	enum bpf_prog_type prog_type;
 	char buf[256];
 	int fd, efd, err, id;
@@ -66,6 +67,8 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
 		prog_type = BPF_PROG_TYPE_KPROBE;
 	} else if (is_tracepoint) {
 		prog_type = BPF_PROG_TYPE_TRACEPOINT;
+	} else if (is_xdp) {
+		prog_type = BPF_PROG_TYPE_XDP;
 	} else {
 		printf("Unknown event '%s'\n", event);
 		return -1;
@@ -79,6 +82,9 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
 
 	prog_fd[prog_cnt++] = fd;
 
+	if (is_xdp)
+		return 0;
+
 	if (is_socket) {
 		event += 6;
 		if (*event != '/')
@@ -319,6 +325,7 @@ int load_bpf_file(char *path)
 			if (memcmp(shname_prog, "kprobe/", 7) == 0 ||
 			    memcmp(shname_prog, "kretprobe/", 10) == 0 ||
 			    memcmp(shname_prog, "tracepoint/", 11) == 0 ||
+			    memcmp(shname_prog, "xdp", 3) == 0 ||
 			    memcmp(shname_prog, "socket", 6) == 0)
 				load_and_attach(shname_prog, insns, data_prog->d_size);
 		}
@@ -336,6 +343,7 @@ int load_bpf_file(char *path)
 		if (memcmp(shname, "kprobe/", 7) == 0 ||
 		    memcmp(shname, "kretprobe/", 10) == 0 ||
 		    memcmp(shname, "tracepoint/", 11) == 0 ||
+		    memcmp(shname, "xdp", 3) == 0 ||
 		    memcmp(shname, "socket", 6) == 0)
 			load_and_attach(shname, data->d_buf, data->d_size);
 	}
samples/bpf/xdp1_kern.c new file +93 -0

93 lines not on this page · raw message

samples/bpf/xdp1_user.c new file +181 -0

181 lines not on this page · raw message

Signature · 1 line
2.8.2
#13 lore raw

PATCH v7 6/11 net/mlx4_en: add page recycle to prepare rx ring for tx support

In reply to Brenden Blanco

The mlx4 driver by default allocates order-3 pages for the ring to consume in multiple fragments. When the device has an xdp program, this behavior will prevent tx actions since the page must be re-mapped in TODEVICE mode, which cannot be done if the page is still shared.

Start by making the allocator configurable based on whether xdp is running, such that order-0 pages are always used and never shared.

Since this will stress the page allocator, add a simple page cache to each rx ring. Pages in the cache are left dma-mapped, and in drop-only stress tests the page allocator is eliminated from the perf report.

Note that setting an xdp program will now require the rings to be reconfigured.

Before:

 26.91%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_process_rx_cq
 17.88%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_alloc_frags
  6.00%  ksoftirqd/0  [mlx4_en]         [k] mlx4_en_free_frag
  4.49%  ksoftirqd/0  [kernel.vmlinux]  [k] get_page_from_freelist
  3.21%  swapper      [kernel.vmlinux]  [k] intel_idle
  2.73%  ksoftirqd/0  [kernel.vmlinux]  [k] bpf_map_lookup_elem
  2.57%  swapper      [mlx4_en]         [k] mlx4_en_process_rx_cq

After:

 31.72%  swapper      [kernel.vmlinux]       [k] intel_idle
  8.79%  swapper      [mlx4_en]              [k] mlx4_en_process_rx_cq
  7.54%  swapper      [kernel.vmlinux]       [k] poll_idle
  6.36%  swapper      [mlx4_core]            [k] mlx4_eq_int
  4.21%  swapper      [kernel.vmlinux]       [k] tasklet_action
  4.03%  swapper      [kernel.vmlinux]       [k] cpuidle_enter_state
  3.43%  swapper      [mlx4_en]              [k] mlx4_en_prepare_rx_desc
  2.18%  swapper      [kernel.vmlinux]       [k] native_irq_return_iret
  1.37%  swapper      [kernel.vmlinux]       [k] menu_select
  1.09%  swapper      [kernel.vmlinux]       [k] bpf_map_lookup_elem
---
 drivers/net/ethernet/mellanox/mlx4/en_ethtool.c |  2 +-
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c  | 46 +++++++++++++++--
 drivers/net/ethernet/mellanox/mlx4/en_rx.c      | 69 ++++++++++++++++++++++---
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h    | 12 ++++-
 4 files changed, 115 insertions(+), 14 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +1 -1
index 51a2e82..d3d51fa 100644
@@ -47,7 +47,7 @@
 #define EN_ETHTOOL_SHORT_MASK cpu_to_be16(0xffff)
 #define EN_ETHTOOL_WORD_MASK  cpu_to_be32(0xffffffff)
 
-static int mlx4_en_moderation_update(struct mlx4_en_priv *priv)
+int mlx4_en_moderation_update(struct mlx4_en_priv *priv)
 {
 	int i;
 	int err = 0;
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +42 -4

61 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx4/en_rx.c +61 -8

152 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +11 -1
index 35ecfa2..0e0ecd1 100644
@@ -259,6 +259,12 @@ struct mlx4_en_rx_alloc {
 	u32		page_size;
 };
 
+#define MLX4_EN_CACHE_SIZE (2 * NAPI_POLL_WEIGHT)
+struct mlx4_en_page_cache {
+	u32 index;
+	struct mlx4_en_rx_alloc buf[MLX4_EN_CACHE_SIZE];
+};
+
 struct mlx4_en_tx_ring {
 	/* cache line used and dirtied in tx completion
 	 * (mlx4_en_free_tx_buf())
@@ -323,6 +329,7 @@ struct mlx4_en_rx_ring {
 	u8  fcs_del;
 	void *buf;
 	void *rx_info;
+	struct mlx4_en_page_cache page_cache;
 	unsigned long bytes;
 	unsigned long packets;
 	unsigned long csum_ok;
@@ -442,7 +449,9 @@ struct mlx4_en_mc_list {
 struct mlx4_en_frag_info {
 	u16 frag_size;
 	u16 frag_prefix_size;
-	u16 frag_stride;
+	u32 frag_stride;
+	enum dma_data_direction dma_dir;
+	int order;
 };
 
 #ifdef CONFIG_MLX4_EN_DCB
@@ -654,6 +663,7 @@ void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
 
 void mlx4_en_free_resources(struct mlx4_en_priv *priv);
 int mlx4_en_alloc_resources(struct mlx4_en_priv *priv);
+int mlx4_en_moderation_update(struct mlx4_en_priv *priv);
 
 int mlx4_en_create_cq(struct mlx4_en_priv *priv, struct mlx4_en_cq **pcq,
 		      int entries, int ring, enum cq_type mode, int node);
Signature · 1 line
2.8.2
#14 lore raw

PATCH v7 7/11 bpf: add XDP_TX xdp_action for direct forwarding

In reply to Brenden Blanco

XDP enabled drivers must transmit received packets back out on the same port they were received on when a program returns this action.

---
 include/uapi/linux/bpf.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/bpf.h +1 -0
index 4282d44..a8f1ea1 100644
@@ -447,6 +447,7 @@ enum xdp_action {
 	XDP_ABORTED = 0,
 	XDP_DROP,
 	XDP_PASS,
+	XDP_TX,
 };
 
 /* user accessible metadata for XDP packet hook
Signature · 1 line
2.8.2
#15 lore raw

PATCH v7 8/11 net/mlx4_en: break out tx_desc write into separate function

In reply to Brenden Blanco

In preparation for writing the tx descriptor from multiple functions, create a helper for both normal and blueflame access.

---
 drivers/infiniband/hw/mlx4/qp.c            |  11 +--
 drivers/net/ethernet/mellanox/mlx4/en_tx.c | 127 +++++++++++++++++------------
 include/linux/mlx4/qp.h                    |  18 ++--
 3 files changed, 90 insertions(+), 66 deletions(-)
drivers/infiniband/hw/mlx4/qp.c +6 -5
index 8db8405..768085f 100644
@@ -232,7 +232,7 @@ static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
 		}
 	} else {
 		ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
-		s = (ctrl->fence_size & 0x3f) << 4;
+		s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
 		for (i = 64; i < s; i += 64) {
 			wqe = buf + i;
 			*wqe = cpu_to_be32(0xffffffff);
@@ -264,7 +264,7 @@ static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
 		inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
 	}
 	ctrl->srcrb_flags = 0;
-	ctrl->fence_size = size / 16;
+	ctrl->qpn_vlan.fence_size = size / 16;
 	/*
 	 * Make sure descriptor is fully written before setting ownership bit
 	 * (because HW can start executing as soon as we do).
@@ -1992,7 +1992,8 @@ static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
 			ctrl = get_send_wqe(qp, i);
 			ctrl->owner_opcode = cpu_to_be32(1 << 31);
 			if (qp->sq_max_wqes_per_wr == 1)
-				ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
+				ctrl->qpn_vlan.fence_size =
+						1 << (qp->sq.wqe_shift - 4);
 
 			stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
 		}
@@ -3169,8 +3170,8 @@ int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
 		wmb();
 		*lso_wqe = lso_hdr_sz;
 
-		ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
-				    MLX4_WQE_CTRL_FENCE : 0) | size;
+		ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
+					     MLX4_WQE_CTRL_FENCE : 0) | size;
 
 		/*
 		 * Make sure descriptor is fully written before
drivers/net/ethernet/mellanox/mlx4/en_tx.c +74 -53

165 lines not on this page · raw message

include/linux/mlx4/qp.h +10 -8
index 587cdf9..deaa221 100644
@@ -291,16 +291,18 @@ enum {
 	MLX4_WQE_CTRL_FORCE_LOOPBACK	= 1 << 0,
 };
 
+union mlx4_wqe_qpn_vlan {
+	struct {
+		__be16	vlan_tag;
+		u8	ins_vlan;
+		u8	fence_size;
+	};
+	__be32		bf_qpn;
+};
+
 struct mlx4_wqe_ctrl_seg {
 	__be32			owner_opcode;
-	union {
-		struct {
-			__be16			vlan_tag;
-			u8			ins_vlan;
-			u8			fence_size;
-		};
-		__be32			bf_qpn;
-	};
+	union mlx4_wqe_qpn_vlan	qpn_vlan;
 	/*
 	 * High 24 bits are SRC remote buffer; low 8 bits are flags:
 	 * [7]   SO (strong ordering)
Signature · 1 line
2.8.2
#16 lore raw

PATCH v7 9/11 net/mlx4_en: add xdp forwarding and data write support

In reply to Brenden Blanco

A user will now be able to loop packets back out of the same port using a bpf program attached to xdp hook. Updates to the packet contents from the bpf program is also supported.

For the packet write feature to work, the rx buffers are now mapped as bidirectional when the page is allocated. This occurs only when the xdp hook is active.

When the program returns a TX action, enqueue the packet directly to a dedicated tx ring, so as to avoid completely any locking. This requires the tx ring to be allocated 1:1 for each rx ring, as well as the tx completion running in the same softirq.

Upon tx completion, this dedicated tx ring recycles pages without unmapping directly back to the original rx ring. In steady state tx/drop workload, effectively 0 page allocs/frees will occur.

---
 drivers/net/ethernet/mellanox/mlx4/en_ethtool.c |  15 ++-
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c  |  19 +++-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c      |  14 +++
 drivers/net/ethernet/mellanox/mlx4/en_tx.c      | 126 +++++++++++++++++++++++-
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h    |  14 ++-
 5 files changed, 181 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +11 -4
index d3d51fa..10642b1 100644
@@ -1694,6 +1694,11 @@ static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
 	return err;
 }
 
+static int mlx4_en_max_tx_channels(struct mlx4_en_priv *priv)
+{
+	return (MAX_TX_RINGS - priv->rsv_tx_rings) / MLX4_EN_NUM_UP;
+}
+
 static void mlx4_en_get_channels(struct net_device *dev,
 				 struct ethtool_channels *channel)
 {
@@ -1705,7 +1710,7 @@ static void mlx4_en_get_channels(struct net_device *dev,
 	channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
 
 	channel->rx_count = priv->rx_ring_num;
-	channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
+	channel->tx_count = priv->num_tx_rings_p_up;
 }
 
 static int mlx4_en_set_channels(struct net_device *dev,
@@ -1717,7 +1722,7 @@ static int mlx4_en_set_channels(struct net_device *dev,
 	int err = 0;
 
 	if (channel->other_count || channel->combined_count ||
-	    channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
+	    channel->tx_count > mlx4_en_max_tx_channels(priv) ||
 	    channel->rx_count > MAX_RX_RINGS ||
 	    !channel->tx_count || !channel->rx_count)
 		return -EINVAL;
@@ -1731,7 +1736,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
 	mlx4_en_free_resources(priv);
 
 	priv->num_tx_rings_p_up = channel->tx_count;
-	priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
+	priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP +
+							priv->rsv_tx_rings;
 	priv->rx_ring_num = channel->rx_count;
 
 	err = mlx4_en_alloc_resources(priv);
@@ -1740,7 +1746,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
 		goto out;
 	}
 
-	netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
+	netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
+							priv->rsv_tx_rings);
 	netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
 
 	if (dev->num_tc)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +18 -1
index 0417023..3257db7 100644
@@ -1636,7 +1636,7 @@ int mlx4_en_start_port(struct net_device *dev)
 		/* Configure ring */
 		tx_ring = priv->tx_ring[i];
 		err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
-			i / priv->num_tx_rings_p_up);
+			i / (priv->tx_ring_num / MLX4_EN_NUM_UP));
 		if (err) {
 			en_err(priv, "Failed allocating Tx ring\n");
 			mlx4_en_deactivate_cq(priv, cq);
@@ -2022,6 +2022,16 @@ int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
 			goto err;
 	}
 
+	/* When rsv_tx_rings is non-zero, each rx ring will have a
+	 * corresponding tx ring, with the tx completion event for that ring
+	 * recycling buffers into the cache.
+	 */
+	for (i = 0; i < priv->rsv_tx_rings; i++) {
+		int j = (priv->tx_ring_num - priv->rsv_tx_rings) + i;
+
+		priv->tx_ring[j]->recycle_ring = priv->rx_ring[i];
+	}
+
 #ifdef CONFIG_RFS_ACCEL
 	priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
 #endif
@@ -2534,9 +2544,12 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
 	struct mlx4_en_priv *priv = netdev_priv(dev);
 	struct mlx4_en_dev *mdev = priv->mdev;
 	struct bpf_prog *old_prog;
+	int rsv_tx_rings;
 	int port_up = 0;
 	int err;
 
+	rsv_tx_rings = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
+
 	/* No need to reconfigure buffers when simply swapping the
 	 * program for a new one.
 	 */
@@ -2561,6 +2574,10 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
 
 	mlx4_en_free_resources(priv);
 
+	priv->rsv_tx_rings = rsv_tx_rings;
+	priv->tx_ring_num = priv->num_tx_rings_p_up * MLX4_EN_NUM_UP +
+							priv->rsv_tx_rings;
+
 	old_prog = xchg(&priv->prog, prog);
 	if (old_prog)
 		bpf_prog_put(old_prog);
drivers/net/ethernet/mellanox/mlx4/en_rx.c +14 -0
index f26306c..9215940 100644
@@ -779,7 +779,9 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 	struct mlx4_en_rx_alloc *frags;
 	struct mlx4_en_rx_desc *rx_desc;
 	struct bpf_prog *prog;
+	int doorbell_pending;
 	struct sk_buff *skb;
+	int tx_index;
 	int index;
 	int nr;
 	unsigned int length;
@@ -796,6 +798,8 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 		return polled;
 
 	prog = READ_ONCE(priv->prog);
+	doorbell_pending = 0;
+	tx_index = (priv->tx_ring_num - priv->rsv_tx_rings) + cq->ring;
 
 	/* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
 	 * descriptor offset can be deduced from the CQE index instead of
@@ -894,6 +898,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 			switch (act) {
 			case XDP_PASS:
 				break;
+			case XDP_TX:
+				if (!mlx4_en_xmit_frame(frags, dev,
+							length, tx_index,
+							&doorbell_pending))
+					goto consumed;
+				break;
 			default:
 				bpf_warn_invalid_xdp_action(act);
 			case XDP_ABORTED:
@@ -1064,6 +1074,9 @@ consumed:
 	}
 
 out:
+	if (doorbell_pending)
+		mlx4_en_xmit_doorbell(priv->tx_ring[tx_index]);
+
 	AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
 	mlx4_cq_set_ci(&cq->mcq);
 	wmb(); /* ensure HW sees CQ consumer before we post new buffers */
@@ -1143,6 +1156,7 @@ void mlx4_en_calc_rx_buf(struct net_device *dev)
 	 * This only works when num_frags == 1.
 	 */
 	if (priv->prog) {
+		dma_dir = PCI_DMA_BIDIRECTIONAL;
 		/* This will gain efficient xdp frame recycling at the expense
 		 * of more costly truesize accounting
 		 */
drivers/net/ethernet/mellanox/mlx4/en_tx.c +125 -1

144 lines not on this page · raw message

drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +13 -1
index 0e0ecd1..7309308 100644
@@ -132,6 +132,7 @@ enum {
 					 MLX4_EN_NUM_UP)
 
 #define MLX4_EN_DEFAULT_TX_WORK		256
+#define MLX4_EN_DOORBELL_BUDGET		8
 
 /* Target number of packets to coalesce with interrupt moderation */
 #define MLX4_EN_RX_COAL_TARGET	44
@@ -219,7 +220,10 @@ enum cq_type {
 
 
 struct mlx4_en_tx_info {
-	struct sk_buff *skb;
+	union {
+		struct sk_buff *skb;
+		struct page *page;
+	};
 	dma_addr_t	map0_dma;
 	u32		map0_byte_count;
 	u32		nr_txbb;
@@ -298,6 +302,7 @@ struct mlx4_en_tx_ring {
 	__be32			mr_key;
 	void			*buf;
 	struct mlx4_en_tx_info	*tx_info;
+	struct mlx4_en_rx_ring	*recycle_ring;
 	u8			*bounce_buf;
 	struct mlx4_qp_context	context;
 	int			qpn;
@@ -604,6 +609,7 @@ struct mlx4_en_priv {
 	struct hwtstamp_config hwtstamp_config;
 	u32 counter_index;
 	struct bpf_prog *prog;
+	int rsv_tx_rings;
 
 #ifdef CONFIG_MLX4_EN_DCB
 #define MLX4_EN_DCB_ENABLED	0x3
@@ -678,6 +684,12 @@ void mlx4_en_tx_irq(struct mlx4_cq *mcq);
 u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
 			 void *accel_priv, select_queue_fallback_t fallback);
 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev);
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+			       struct net_device *dev, unsigned int length,
+			       int tx_ind, int *doorbell_pending);
+void mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring *ring);
+bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
+			struct mlx4_en_rx_alloc *frame);
 
 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
 			   struct mlx4_en_tx_ring **pring,
Signature · 1 line
2.8.2
#20 lore raw

PATCH v7 10/11 bpf: enable direct packet data write for xdp progs

In reply to Brenden Blanco

For forwarding to be effective, XDP programs should be allowed to rewrite packet data.

This requires that the drivers supporting XDP must all map the packet memory as TODEVICE or BIDIRECTIONAL before invoking the program.

---
 kernel/bpf/verifier.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +16 -1
index a8d67d0..f72f23b 100644
@@ -653,6 +653,16 @@ static int check_map_access(struct verifier_env *env, u32 regno, int off,
 
 #define MAX_PACKET_OFF 0xffff
 
+static bool may_write_pkt_data(enum bpf_prog_type type)
+{
+	switch (type) {
+	case BPF_PROG_TYPE_XDP:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int check_packet_access(struct verifier_env *env, u32 regno, int off,
 			       int size)
 {
@@ -806,10 +816,15 @@ static int check_mem_access(struct verifier_env *env, u32 regno, int off,
 			err = check_stack_read(state, off, size, value_regno);
 		}
 	} else if (state->regs[regno].type == PTR_TO_PACKET) {
-		if (t == BPF_WRITE) {
+		if (t == BPF_WRITE && !may_write_pkt_data(env->prog->type)) {
 			verbose("cannot write into packet\n");
 			return -EACCES;
 		}
+		if (t == BPF_WRITE && value_regno >= 0 &&
+		    is_pointer_value(env, value_regno)) {
+			verbose("R%d leaks addr into packet\n", value_regno);
+			return -EACCES;
+		}
 		err = check_packet_access(env, regno, off, size);
 		if (!err && t == BPF_READ && value_regno >= 0)
 			mark_reg_unknown_value(state->regs, value_regno);
Signature · 1 line
2.8.2
#21 lore raw

PATCH v7 11/11 bpf: add sample for xdp forwarding and rewrite

In reply to Brenden Blanco

Add a sample that rewrites and forwards packets out on the same interface. Observed single core forwarding performance of ~10Mpps.

Since the mlx4 driver under test recycles every single packet page, the perf output shows almost exclusively just the ring management and bpf program work. Slowdowns are likely occurring due to cache misses.

---
 samples/bpf/Makefile    |   5 +++
 samples/bpf/xdp2_kern.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)
 create mode 100644 samples/bpf/xdp2_kern.c
samples/bpf/Makefile +5 -0
index 0e4ab3a..d2d2b35 100644
@@ -22,6 +22,7 @@ hostprogs-y += map_perf_test
 hostprogs-y += test_overhead
 hostprogs-y += test_cgrp2_array_pin
 hostprogs-y += xdp1
+hostprogs-y += xdp2
 
 test_verifier-objs := test_verifier.o libbpf.o
 test_maps-objs := test_maps.o libbpf.o
@@ -44,6 +45,8 @@ map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o
 test_overhead-objs := bpf_load.o libbpf.o test_overhead_user.o
 test_cgrp2_array_pin-objs := libbpf.o test_cgrp2_array_pin.o
 xdp1-objs := bpf_load.o libbpf.o xdp1_user.o
+# reuse xdp1 source intentionally
+xdp2-objs := bpf_load.o libbpf.o xdp1_user.o
 
 # Tell kbuild to always build the programs
 always := $(hostprogs-y)
@@ -67,6 +70,7 @@ always += test_overhead_kprobe_kern.o
 always += parse_varlen.o parse_simple.o parse_ldabs.o
 always += test_cgrp2_tc_kern.o
 always += xdp1_kern.o
+always += xdp2_kern.o
 
 HOSTCFLAGS += -I$(objtree)/usr/include
 
@@ -88,6 +92,7 @@ HOSTLOADLIBES_spintest += -lelf
 HOSTLOADLIBES_map_perf_test += -lelf -lrt
 HOSTLOADLIBES_test_overhead += -lelf -lrt
 HOSTLOADLIBES_xdp1 += -lelf
+HOSTLOADLIBES_xdp2 += -lelf
 
 # Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline:
 #  make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang
samples/bpf/xdp2_kern.c new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.8.2
#22 lore raw
In reply to Brenden Blanco
Quoted 1 line · Brenden Blanco

v7:

[...]

Quoted 3 lines

TODOs:
Add ethtool per-ring stats for aborted, default cases, maybe even drop and tx as well.

please no... lets stop and think if we can have something better vs every XDP enabled driver to have bunch of new ethtool based stats, was this somehow discussed over the threads so far?

#23 lore raw
Quoted 11 lines · Or Gerlitz
Quoted 1 line · Brenden Blanco

v7:

[...]

Quoted 3 lines

TODOs:
Add ethtool per-ring stats for aborted, default cases, maybe even drop and tx as well.

please no... lets stop and think if we can have something better vs every XDP enabled driver to have bunch of new ethtool based stats, was this somehow discussed over the threads so far?

This was in the context of the discussion in [01/12] relating to the return codes and debuggability.

#24 lore raw
In reply to Or Gerlitz
Quoted 11 lines · Or Gerlitz
Quoted 1 line · Brenden Blanco

v7:

[...]

Quoted 3 lines

TODOs:
Add ethtool per-ring stats for aborted, default cases, maybe even drop and tx as well.

please no... lets stop and think if we can have something better vs every XDP enabled driver to have bunch of new ethtool based stats, was this somehow discussed over the threads so far?

We'll also need to consider statistics what statistics a device keeps and how they are exposed when the device offloads XDP, and also the possibility that the BPF program itself might keep its own stats.

Tom

maintainer
#2 lore raw

Re: [PATCH v7 01/11] bpf: add XDP prog type for early driver filter

In reply to Brenden Blanco

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:48 -0700

net/core/filter.c static bool tc_cls_act_is_valid_access(int off, int size, · line 2490

Quoted 12 lines
net/core/filter.c +12 -0
+	WARN_ONCE(1, "\n"
+		     "*****************************************************\n"
+		     "**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n"
+		     "**                                               **\n"
+		     "** XDP program returned unknown value %-10u **\n"
+		     "**                                               **\n"
+		     "** XDP programs must return a well-known return  **\n"
+		     "** value. Invalid return values will result in   **\n"
+		     "** packet loss.                                  **\n"
+		     "**                                               **\n"
+		     "**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n"
+		     "*****************************************************\n",

This is rediculous.

The log message should be concise, one line, and not scream things like "NOTICE". That's what the log level is for, to indicate the severity of the issue.

maintainer
#5 lore raw

Re: [PATCH v7 02/11] net: add ndo to setup/query xdp prog in adapter rx

In reply to Brenden Blanco

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:49 -0700

net/core/dev.c int dev_change_proto_down(struct net_device *dev, bool proto_down) · line 6644

Quoted 11 lines
net/core/dev.c +11 -0
+	if (fd >= 0) {
+		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
+		if (IS_ERR(prog))
+			return PTR_ERR(prog);
+	}
+
+	xdp.command = XDP_SETUP_PROG;
+	xdp.prog = prog;
+	err = ops->ndo_xdp(dev, &xdp);
+	if (err < 0 && prog)
+		bpf_prog_put(prog);

I don't understand the reference counting on 'prog' here.

The enumeration documentation states that no matter what, the passed in prog doesn't need to be mangaged by the ->ndo_xdp() method.

Therefore, if that is true, we must always put the 'prog' here if it is non-NULL. Regardless of the 'err' value.

maintainer
#10 lore raw

Re: [PATCH v7 04/11] net/mlx4_en: add support for fast rx drop bpf program

In reply to Brenden Blanco

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:51 -0700

drivers/net/ethernet/mellanox/mlx4/en_netdev.c static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 m · line 2538

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +2 -0
+	if (priv->num_frags > 1)
+		return -EOPNOTSUPP;

I hate to be the user who has to debug why his XDP program won't load just because he set a jumbo MTU beforehand.

#3 lore raw

Re: [PATCH v7 01/11] bpf: add XDP prog type for early driver filter

In reply to David Miller
Quoted 18 lines · David Miller

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:48 -0700

net/core/filter.c static bool tc_cls_act_is_valid_access(int off, int size, · line 2490

Quoted 12 lines
net/core/filter.c +12 -0
+	WARN_ONCE(1, "\n"
+		     "*****************************************************\n"
+		     "**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n"
+		     "**                                               **\n"
+		     "** XDP program returned unknown value %-10u **\n"
+		     "**                                               **\n"
+		     "** XDP programs must return a well-known return  **\n"
+		     "** value. Invalid return values will result in   **\n"
+		     "** packet loss.                                  **\n"
+		     "**                                               **\n"
+		     "**   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **\n"
+		     "*****************************************************\n",

This is rediculous.

Quoted 3 lines

The log message should be concise, one line, and not scream things like "NOTICE". That's what the log level is for, to indicate the severity of the issue.

Makes sense, and I won't try this approach again. The caller of this code continues to be much debated as well.

#6 lore raw

Re: [PATCH v7 02/11] net: add ndo to setup/query xdp prog in adapter rx

In reply to David Miller
Quoted 17 lines · David Miller

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:49 -0700

net/core/dev.c int dev_change_proto_down(struct net_device *dev, bool proto_down) · line 6644

Quoted 11 lines
net/core/dev.c +11 -0
+	if (fd >= 0) {
+		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
+		if (IS_ERR(prog))
+			return PTR_ERR(prog);
+	}
+
+	xdp.command = XDP_SETUP_PROG;
+	xdp.prog = prog;
+	err = ops->ndo_xdp(dev, &xdp);
+	if (err < 0 && prog)
+		bpf_prog_put(prog);

I don't understand the reference counting on 'prog' here.

Quoted 6 lines

The enumeration documentation states that no matter what, the passed in prog doesn't need to be mangaged by the ->ndo_xdp() method.

Therefore, if that is true, we must always put the 'prog' here if it is non-NULL. Regardless of the 'err' value.

The documentation is unclear then. What I _meant_ to say is that the callee is not responsible for putting the program on error, but on success it takes ownership of the reference. In context of that, does the code make sense? Is there a more conventional way of handling this?

Thanks for your review.

maintainer
#7 lore raw

Re: [PATCH v7 02/11] net: add ndo to setup/query xdp prog in adapter rx

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 23:35:00 -0700

Quoted 21 lines · David Miller
Quoted 20 lines

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:49 -0700

net/core/dev.c int dev_change_proto_down(struct net_device *dev, bool proto_down) · line 6644

Quoted 11 lines
net/core/dev.c +11 -0
+	if (fd >= 0) {
+		prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
+		if (IS_ERR(prog))
+			return PTR_ERR(prog);
+	}
+
+	xdp.command = XDP_SETUP_PROG;
+	xdp.prog = prog;
+	err = ops->ndo_xdp(dev, &xdp);
+	if (err < 0 && prog)
+		bpf_prog_put(prog);

I don't understand the reference counting on 'prog' here.

The enumeration documentation states that no matter what, the passed in prog doesn't need to be mangaged by the ->ndo_xdp() method.

Quoted 7 lines
Quoted 3 lines

Therefore, if that is true, we must always put the 'prog' here if it is non-NULL. Regardless of the 'err' value.

The documentation is unclear then. What I _meant_ to say is that the callee is not responsible for putting the program on error, but on success it takes ownership of the reference. In context of that, does the code make sense? Is there a more conventional way of handling this?

Then please document it more clearly, thanks.

#11 lore raw

Re: [PATCH v7 04/11] net/mlx4_en: add support for fast rx drop bpf program

In reply to David Miller
Quoted 8 lines · David Miller

From: Brenden Blanco <[email protected]>
Date: Mon, 11 Jul 2016 14:29:51 -0700

drivers/net/ethernet/mellanox/mlx4/en_netdev.c static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 m · line 2538

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +2 -0
+	if (priv->num_frags > 1)
+		return -EOPNOTSUPP;

I hate to be the user who has to debug why his XDP program won't load just because he set a jumbo MTU beforehand.

Me too. Patch v8 now has an en_err message, and double checked that a message is printed when MTU is attempted to be increased when XDP program has already been loaded.

Thanks.

#17 lore raw

Re: [PATCH v7 09/11] net/mlx4_en: add xdp forwarding and data write support

In reply to Brenden Blanco
Quoted 31 lines · Brenden Blanco

A user will now be able to loop packets back out of the same port using a bpf program attached to xdp hook. Updates to the packet contents from the bpf program is also supported.

For the packet write feature to work, the rx buffers are now mapped as bidirectional when the page is allocated. This occurs only when the xdp hook is active.

When the program returns a TX action, enqueue the packet directly to a dedicated tx ring, so as to avoid completely any locking. This requires the tx ring to be allocated 1:1 for each rx ring, as well as the tx completion running in the same softirq.

Upon tx completion, this dedicated tx ring recycles pages without unmapping directly back to the original rx ring. In steady state tx/drop workload, effectively 0 page allocs/frees will occur.

---
 drivers/net/ethernet/mellanox/mlx4/en_ethtool.c |  15 ++-
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c  |  19 +++-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c      |  14 +++
 drivers/net/ethernet/mellanox/mlx4/en_tx.c      | 126 +++++++++++++++++++++++-
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h    |  14 ++-
 5 files changed, 181 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +0 -0
index d3d51fa..10642b1 100644

drivers/net/ethernet/mellanox/mlx4/en_ethtool.c static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) · line 1701

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +5 -0
@@ -1694,6 +1694,11 @@ static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
        return err;
 }
 
+static int mlx4_en_max_tx_channels(struct mlx4_en_priv *priv)
+{
+       return (MAX_TX_RINGS - priv->rsv_tx_rings) / MLX4_EN_NUM_UP;
+}
+

MAX_TX_RING is a software limitation made to limit netdev real_num_tx queues for CX3 internal cache utilization,
in your case the netdev doesn't care about xdp_tx rings, the accounting you added in this patch adds a lot of complications and it would be better to have clear separation between the two types of tx_rings, in terms of the holding/managing data structure.

I suggest here to leave priv->tx_ring untouched. i.e, don't store the xdp_tx rings at the end of it, i.e priv->tx_ring should only reflect the
netdev real tx queues.

In case of priv->porg is active, we can allocate and store xdp tx ring per rx ring, this tx ring will be allocated and activated once the rx ring is created and activated, and store this dedicated tx ring in the rx_ring it self.

i.e :
struct mlx4_en_rx_ring {
[...]
struct mlx4_en_tx_ring *xdp_tx;
struct mlx4_en_cq *xdp_tx_cq;
[...]
}

for this the following changes are required.

@ mlx4_en_create_rx_ring
[...] // Create the RX ring

/* create CQ for xdp tx ring */
node = cpu_to_node(i % num_online_cpus());

mlx4_en_create_cq(priv, &rx_ring->xdp_tx_cq, prof->tx_ring_size, i, TX, node)

/* create xdp tx ring */
mlx4_en_create_tx_ring(priv, &rx_ring->xdp_tx, prof->tx_ring_size, TXBB_SIZE, node, -1)

@mlx4_en_start/stop_port
/* Configure tx cq's and rings */
// You will need to configure xdp tx rings same as priv->rx_ring_num rings

@mlx4_en_poll_tx_cq
This Also will require a new NAPI handler for xdp rings to replace the following line @mlx4_en_poll_tx_cq
- struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring]; with
+ struct mlx4_en_tx_ring *ring = priv->rx_ring[cq->ring].xdp_tx;

Or just change cq->ring from ring index to the actual ring pointer.

Bottom line, my suggestion also started to look complicated :).. but still it would look cleaner to separate between netdev rings and xdp rings.

Quoted 132 lines
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +6 -4
 static void mlx4_en_get_channels(struct net_device *dev,
                                 struct ethtool_channels *channel)
 {
@@ -1705,7 +1710,7 @@ static void mlx4_en_get_channels(struct net_device *dev,
        channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
 
        channel->rx_count = priv->rx_ring_num;
-       channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
+       channel->tx_count = priv->num_tx_rings_p_up;
 }
 
 static int mlx4_en_set_channels(struct net_device *dev,
@@ -1717,7 +1722,7 @@ static int mlx4_en_set_channels(struct net_device *dev,
        int err = 0;
 
        if (channel->other_count || channel->combined_count ||
-           channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
+           channel->tx_count > mlx4_en_max_tx_channels(priv) ||
            channel->rx_count > MAX_RX_RINGS ||
            !channel->tx_count || !channel->rx_count)
                return -EINVAL;
@@ -1731,7 +1736,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
        mlx4_en_free_resources(priv);
 
        priv->num_tx_rings_p_up = channel->tx_count;
-       priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
+       priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP +
+                                                       priv->rsv_tx_rings;
        priv->rx_ring_num = channel->rx_count;
 
        err = mlx4_en_alloc_resources(priv);
@@ -1740,7 +1746,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
                goto out;
        }
 
-       netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
+       netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
+                                                       priv->rsv_tx_rings);
        netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
 
        if (dev->num_tc)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +18 -1
index 0417023..3257db7 100644
@@ -1636,7 +1636,7 @@ int mlx4_en_start_port(struct net_device *dev)
                /* Configure ring */
                tx_ring = priv->tx_ring[i];
                err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
-                       i / priv->num_tx_rings_p_up);
+                       i / (priv->tx_ring_num / MLX4_EN_NUM_UP));
                if (err) {
                        en_err(priv, "Failed allocating Tx ring\n");
                        mlx4_en_deactivate_cq(priv, cq);
@@ -2022,6 +2022,16 @@ int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
                        goto err;
        }
 
+       /* When rsv_tx_rings is non-zero, each rx ring will have a
+        * corresponding tx ring, with the tx completion event for that ring
+        * recycling buffers into the cache.
+        */
+       for (i = 0; i < priv->rsv_tx_rings; i++) {
+               int j = (priv->tx_ring_num - priv->rsv_tx_rings) + i;
+
+               priv->tx_ring[j]->recycle_ring = priv->rx_ring[i];
+       }
+
 #ifdef CONFIG_RFS_ACCEL
        priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
 #endif
@@ -2534,9 +2544,12 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
        struct mlx4_en_priv *priv = netdev_priv(dev);
        struct mlx4_en_dev *mdev = priv->mdev;
        struct bpf_prog *old_prog;
+       int rsv_tx_rings;
        int port_up = 0;
        int err;
 
+       rsv_tx_rings = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
+
        /* No need to reconfigure buffers when simply swapping the
         * program for a new one.
         */
@@ -2561,6 +2574,10 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
 
        mlx4_en_free_resources(priv);
 
+       priv->rsv_tx_rings = rsv_tx_rings;
+       priv->tx_ring_num = priv->num_tx_rings_p_up * MLX4_EN_NUM_UP +
+                                                       priv->rsv_tx_rings;
+
        old_prog = xchg(&priv->prog, prog);
        if (old_prog)
                bpf_prog_put(old_prog);
drivers/net/ethernet/mellanox/mlx4/en_rx.c +10 -0
index f26306c..9215940 100644
@@ -779,7 +779,9 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
        struct mlx4_en_rx_alloc *frags;
        struct mlx4_en_rx_desc *rx_desc;
        struct bpf_prog *prog;
+       int doorbell_pending;
        struct sk_buff *skb;
+       int tx_index;
        int index;
        int nr;
        unsigned int length;
@@ -796,6 +798,8 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                return polled;
 
        prog = READ_ONCE(priv->prog);
+       doorbell_pending = 0;
+       tx_index = (priv->tx_ring_num - priv->rsv_tx_rings) + cq->ring;
 
        /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
         * descriptor offset can be deduced from the CQE index instead of
@@ -894,6 +898,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                        switch (act) {
                        case XDP_PASS:
                                break;
+                       case XDP_TX:
+                               if (!mlx4_en_xmit_frame(frags, dev,
+                                                       length, tx_index,
+                                                       &doorbell_pending))
+                                       goto consumed;
+                               break;
                        default:
                                bpf_warn_invalid_xdp_action(act);
                        case XDP_ABORTED:

drivers/net/ethernet/mellanox/mlx4/en_rx.c consumed: · line 1079

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +3 -0
@@ -1064,6 +1074,9 @@ consumed:
        }
 
 out:
+       if (doorbell_pending)
+               mlx4_en_xmit_doorbell(priv->tx_ring[tx_index]);
+

If in this napi cycle we had at least one packet that went through XDP_PASS (mlx4_en_xmit_frame) you must hit doorbell here, otherwise if no packet will be forwarded later, this packet will never be really sent and it will wait in HW forever.

The idea is correct to hit the doorbell only at the end of mlx4_en_process_rx_cq cycle to batch tx descriptors and send them in one batch,
but you must hit doorbell at the end of the cycle. you can't just assume more RX packets will come in the future to hit the doorbell for you.

Quoted 29 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +1 -0
        AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
        mlx4_cq_set_ci(&cq->mcq);
        wmb(); /* ensure HW sees CQ consumer before we post new buffers */
@@ -1143,6 +1156,7 @@ void mlx4_en_calc_rx_buf(struct net_device *dev)
         * This only works when num_frags == 1.
         */
        if (priv->prog) {
+               dma_dir = PCI_DMA_BIDIRECTIONAL;
                /* This will gain efficient xdp frame recycling at the expense
                 * of more costly truesize accounting
                 */
drivers/net/ethernet/mellanox/mlx4/en_tx.c +6 -1
index c29191e..ba7b5eb 100644
@@ -274,10 +274,28 @@ static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
        struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
        struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
        void *end = ring->buf + ring->buf_size;
-       struct sk_buff *skb = tx_info->skb;
        int nr_maps = tx_info->nr_maps;
+       struct sk_buff *skb;
        int i;
 
+       if (ring->recycle_ring) {
+               struct mlx4_en_rx_alloc frame = {
+                       .page = tx_info->page,
+                       .dma = tx_info->map0_dma,
+                       .page_offset = 0,

drivers/net/ethernet/mellanox/mlx4/en_tx.c static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv, · line 295

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -0
+                       .page_size = PAGE_SIZE,
+               };
+
+               if (!mlx4_en_rx_recycle(ring->recycle_ring, &frame)) {
+                       dma_unmap_page(priv->ddev, tx_info->map0_dma,
+                                      PAGE_SIZE, priv->frag_info[0].dma_dir);
+                       put_page(tx_info->page);
+               }
+               return tx_info->nr_txbb;
+       }

This condition will be checked always even for non XDP rings and when XDP is not enabled.
can't we just figure a way not to have this for non XDP rings ? other than having a separate napi handler i don't see a way to do this. on the other hand, new NAPI handler would introduce a lot of code duplication.

Quoted 95 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +81 -0
+
+       skb = tx_info->skb;
+
        /* We do not touch skb here, so prefetch skb->users location
         * to speedup consume_skb()
         */
@@ -476,6 +494,9 @@ static bool mlx4_en_process_tx_cq(struct net_device *dev,
        ACCESS_ONCE(ring->last_nr_txbb) = last_nr_txbb;
        ACCESS_ONCE(ring->cons) = ring_cons + txbbs_skipped;
 
+       if (ring->recycle_ring)
+               return done < budget;
+
        netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
 
        /* Wakeup Tx queue if this stopped, and ring is not full.
@@ -1055,3 +1076,106 @@ tx_drop:
        return NETDEV_TX_OK;
 }
 
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+                              struct net_device *dev, unsigned int length,
+                              int tx_ind, int *doorbell_pending)
+{
+       struct mlx4_en_priv *priv = netdev_priv(dev);
+       union mlx4_wqe_qpn_vlan qpn_vlan = {};
+       struct mlx4_en_tx_ring *ring;
+       struct mlx4_en_tx_desc *tx_desc;
+       struct mlx4_wqe_data_seg *data;
+       struct mlx4_en_tx_info *tx_info;
+       int index, bf_index;
+       bool send_doorbell;
+       int nr_txbb = 1;
+       bool stop_queue;
+       dma_addr_t dma;
+       int real_size;
+       __be32 op_own;
+       u32 ring_cons;
+       bool bf_ok;
+
+       BUILD_BUG_ON_MSG(ALIGN(CTRL_SIZE + DS_SIZE, TXBB_SIZE) != TXBB_SIZE,
+                        "mlx4_en_xmit_frame requires minimum size tx desc");
+
+       ring = priv->tx_ring[tx_ind];
+
+       if (!priv->port_up)
+               goto tx_drop;
+
+       if (mlx4_en_is_tx_ring_full(ring))
+               goto tx_drop;
+
+       /* fetch ring->cons far ahead before needing it to avoid stall */
+       ring_cons = READ_ONCE(ring->cons);
+
+       index = ring->prod & ring->size_mask;
+       tx_info = &ring->tx_info[index];
+
+       bf_ok = ring->bf_enabled;
+
+       /* Track current inflight packets for performance analysis */
+       AVG_PERF_COUNTER(priv->pstats.inflight_avg,
+                        (u32)(ring->prod - ring_cons - 1));
+
+       bf_index = ring->prod;
+       tx_desc = ring->buf + index * TXBB_SIZE;
+       data = &tx_desc->data;
+
+       dma = frame->dma;
+
+       tx_info->page = frame->page;
+       frame->page = NULL;
+       tx_info->map0_dma = dma;
+       tx_info->map0_byte_count = length;
+       tx_info->nr_txbb = nr_txbb;
+       tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);
+       tx_info->data_offset = (void *)data - (void *)tx_desc;
+       tx_info->ts_requested = 0;
+       tx_info->nr_maps = 1;
+       tx_info->linear = 1;
+       tx_info->inl = 0;
+
+       dma_sync_single_for_device(priv->ddev, dma, length, PCI_DMA_TODEVICE);
+
+       data->addr = cpu_to_be64(dma);
+       data->lkey = ring->mr_key;
+       dma_wmb();
+       data->byte_count = cpu_to_be32(length);
+
+       /* tx completion can avoid cache line miss for common cases */
+       tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
+
+       op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
+               ((ring->prod & ring->size) ?
+                cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
+

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1163

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -0
+       ring->packets++;
+       ring->bytes += tx_info->nr_bytes;
+       AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, length);
+
+       ring->prod += nr_txbb;
+
+       stop_queue = mlx4_en_is_tx_ring_full(ring);
+       send_doorbell = stop_queue ||
+                               *doorbell_pending > MLX4_EN_DOORBELL_BUDGET;
+       bf_ok &= send_doorbell;

you missed here one important condition for blue flame, there is max size for sending a BF request.

bf_ok &= desc_size <= MAX_BF;

The doorbell accounting here is redundant, and you should always request for doorbell.

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1174

Quoted 11 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +11 -0
+
+       real_size = ((CTRL_SIZE + nr_txbb * DS_SIZE) / 16) & 0x3f;
+
+       if (bf_ok)
+               qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
+       else
+               qpn_vlan.fence_size = real_size;
+
+       mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, TXBB_SIZE, bf_index,
+                             op_own, bf_ok, send_doorbell);
+       *doorbell_pending = send_doorbell ? 0 : *doorbell_pending + 1;

Why not to set doorbell to 1 here ? how do you know more packets will be forwarded ?

Quoted 63 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +7 -0
+
+       return NETDEV_TX_OK;
+
+tx_drop:
+       ring->tx_dropped++;
+       return NETDEV_TX_BUSY;
+}
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +13 -2
index 0e0ecd1..7309308 100644
@@ -132,6 +132,7 @@ enum {
                                         MLX4_EN_NUM_UP)
 
 #define MLX4_EN_DEFAULT_TX_WORK                256
+#define MLX4_EN_DOORBELL_BUDGET                8
 
 /* Target number of packets to coalesce with interrupt moderation */
 #define MLX4_EN_RX_COAL_TARGET 44
@@ -219,7 +220,10 @@ enum cq_type {
 
 
 struct mlx4_en_tx_info {
-       struct sk_buff *skb;
+       union {
+               struct sk_buff *skb;
+               struct page *page;
+       };
        dma_addr_t      map0_dma;
        u32             map0_byte_count;
        u32             nr_txbb;
@@ -298,6 +302,7 @@ struct mlx4_en_tx_ring {
        __be32                  mr_key;
        void                    *buf;
        struct mlx4_en_tx_info  *tx_info;
+       struct mlx4_en_rx_ring  *recycle_ring;
        u8                      *bounce_buf;
        struct mlx4_qp_context  context;
        int                     qpn;
@@ -604,6 +609,7 @@ struct mlx4_en_priv {
        struct hwtstamp_config hwtstamp_config;
        u32 counter_index;
        struct bpf_prog *prog;
+       int rsv_tx_rings;
 
 #ifdef CONFIG_MLX4_EN_DCB
 #define MLX4_EN_DCB_ENABLED    0x3
@@ -678,6 +684,12 @@ void mlx4_en_tx_irq(struct mlx4_cq *mcq);
 u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
                         void *accel_priv, select_queue_fallback_t fallback);
 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev);
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+                              struct net_device *dev, unsigned int length,
+                              int tx_ind, int *doorbell_pending);
+void mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring *ring);
+bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
+                       struct mlx4_en_rx_alloc *frame);
 
 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
                           struct mlx4_en_tx_ring **pring,
--

2.8.2

#18 lore raw

Re: [PATCH v7 09/11] net/mlx4_en: add xdp forwarding and data write support

Quoted 92 lines · Saeed Mahameed
Quoted 31 lines · Brenden Blanco

A user will now be able to loop packets back out of the same port using a bpf program attached to xdp hook. Updates to the packet contents from the bpf program is also supported.

For the packet write feature to work, the rx buffers are now mapped as bidirectional when the page is allocated. This occurs only when the xdp hook is active.

When the program returns a TX action, enqueue the packet directly to a dedicated tx ring, so as to avoid completely any locking. This requires the tx ring to be allocated 1:1 for each rx ring, as well as the tx completion running in the same softirq.

Upon tx completion, this dedicated tx ring recycles pages without unmapping directly back to the original rx ring. In steady state tx/drop workload, effectively 0 page allocs/frees will occur.

---
 drivers/net/ethernet/mellanox/mlx4/en_ethtool.c |  15 ++-
 drivers/net/ethernet/mellanox/mlx4/en_netdev.c  |  19 +++-
 drivers/net/ethernet/mellanox/mlx4/en_rx.c      |  14 +++
 drivers/net/ethernet/mellanox/mlx4/en_tx.c      | 126 +++++++++++++++++++++++-
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h    |  14 ++-
 5 files changed, 181 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +0 -0
index d3d51fa..10642b1 100644

drivers/net/ethernet/mellanox/mlx4/en_ethtool.c static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd) · line 1701

Quoted 8 lines
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +5 -0
@@ -1694,6 +1694,11 @@ static int mlx4_en_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
        return err;
 }
 
+static int mlx4_en_max_tx_channels(struct mlx4_en_priv *priv)
+{
+       return (MAX_TX_RINGS - priv->rsv_tx_rings) / MLX4_EN_NUM_UP;
+}
+

MAX_TX_RING is a software limitation made to limit netdev real_num_tx queues for CX3 internal cache utilization,
in your case the netdev doesn't care about xdp_tx rings, the accounting you added in this patch adds a lot of complications and it would be better to have clear separation between the two types of tx_rings, in terms of the holding/managing data structure.

I suggest here to leave priv->tx_ring untouched. i.e, don't store the xdp_tx rings at the end of it, i.e priv->tx_ring should only reflect the
netdev real tx queues.

In case of priv->porg is active, we can allocate and store xdp tx ring per rx ring, this tx ring will be allocated and activated once the rx ring is created and activated, and store this dedicated tx ring in the rx_ring it self.

i.e :
struct mlx4_en_rx_ring {
[...]
struct mlx4_en_tx_ring *xdp_tx;
struct mlx4_en_cq *xdp_tx_cq;
[...]
}

for this the following changes are required.

@ mlx4_en_create_rx_ring
[...] // Create the RX ring

/* create CQ for xdp tx ring */
node = cpu_to_node(i % num_online_cpus());

mlx4_en_create_cq(priv, &rx_ring->xdp_tx_cq, prof->tx_ring_size, i, TX, node)

/* create xdp tx ring */
mlx4_en_create_tx_ring(priv, &rx_ring->xdp_tx, prof->tx_ring_size, TXBB_SIZE, node, -1)

@mlx4_en_start/stop_port
/* Configure tx cq's and rings */
// You will need to configure xdp tx rings same as priv->rx_ring_num rings

@mlx4_en_poll_tx_cq
This Also will require a new NAPI handler for xdp rings to replace the following line @mlx4_en_poll_tx_cq
- struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring]; with
+ struct mlx4_en_tx_ring *ring = priv->rx_ring[cq->ring].xdp_tx;

Quoted 6 lines

Or just change cq->ring from ring index to the actual ring pointer.

Bottom line, my suggestion also started to look complicated :).. but still it would look cleaner to separate between netdev rings and xdp rings.

I considered this at first too, but it seemed the worse option to me at the time. There would be a lot of copy/paste as well as new code to review.

Quoted 135 lines
Quoted 135 lines
drivers/net/ethernet/mellanox/mlx4/en_ethtool.c +6 -4
 static void mlx4_en_get_channels(struct net_device *dev,
                                 struct ethtool_channels *channel)
 {
@@ -1705,7 +1710,7 @@ static void mlx4_en_get_channels(struct net_device *dev,
        channel->max_tx = MLX4_EN_MAX_TX_RING_P_UP;
 
        channel->rx_count = priv->rx_ring_num;
-       channel->tx_count = priv->tx_ring_num / MLX4_EN_NUM_UP;
+       channel->tx_count = priv->num_tx_rings_p_up;
 }
 
 static int mlx4_en_set_channels(struct net_device *dev,
@@ -1717,7 +1722,7 @@ static int mlx4_en_set_channels(struct net_device *dev,
        int err = 0;
 
        if (channel->other_count || channel->combined_count ||
-           channel->tx_count > MLX4_EN_MAX_TX_RING_P_UP ||
+           channel->tx_count > mlx4_en_max_tx_channels(priv) ||
            channel->rx_count > MAX_RX_RINGS ||
            !channel->tx_count || !channel->rx_count)
                return -EINVAL;
@@ -1731,7 +1736,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
        mlx4_en_free_resources(priv);
 
        priv->num_tx_rings_p_up = channel->tx_count;
-       priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP;
+       priv->tx_ring_num = channel->tx_count * MLX4_EN_NUM_UP +
+                                                       priv->rsv_tx_rings;
        priv->rx_ring_num = channel->rx_count;
 
        err = mlx4_en_alloc_resources(priv);
@@ -1740,7 +1746,8 @@ static int mlx4_en_set_channels(struct net_device *dev,
                goto out;
        }
 
-       netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
+       netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
+                                                       priv->rsv_tx_rings);
        netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
 
        if (dev->num_tc)
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +18 -1
index 0417023..3257db7 100644
@@ -1636,7 +1636,7 @@ int mlx4_en_start_port(struct net_device *dev)
                /* Configure ring */
                tx_ring = priv->tx_ring[i];
                err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
-                       i / priv->num_tx_rings_p_up);
+                       i / (priv->tx_ring_num / MLX4_EN_NUM_UP));
                if (err) {
                        en_err(priv, "Failed allocating Tx ring\n");
                        mlx4_en_deactivate_cq(priv, cq);
@@ -2022,6 +2022,16 @@ int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
                        goto err;
        }
 
+       /* When rsv_tx_rings is non-zero, each rx ring will have a
+        * corresponding tx ring, with the tx completion event for that ring
+        * recycling buffers into the cache.
+        */
+       for (i = 0; i < priv->rsv_tx_rings; i++) {
+               int j = (priv->tx_ring_num - priv->rsv_tx_rings) + i;
+
+               priv->tx_ring[j]->recycle_ring = priv->rx_ring[i];
+       }
+
 #ifdef CONFIG_RFS_ACCEL
        priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
 #endif
@@ -2534,9 +2544,12 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
        struct mlx4_en_priv *priv = netdev_priv(dev);
        struct mlx4_en_dev *mdev = priv->mdev;
        struct bpf_prog *old_prog;
+       int rsv_tx_rings;
        int port_up = 0;
        int err;
 
+       rsv_tx_rings = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
+
        /* No need to reconfigure buffers when simply swapping the
         * program for a new one.
         */
@@ -2561,6 +2574,10 @@ static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
 
        mlx4_en_free_resources(priv);
 
+       priv->rsv_tx_rings = rsv_tx_rings;
+       priv->tx_ring_num = priv->num_tx_rings_p_up * MLX4_EN_NUM_UP +
+                                                       priv->rsv_tx_rings;
+
        old_prog = xchg(&priv->prog, prog);
        if (old_prog)
                bpf_prog_put(old_prog);
drivers/net/ethernet/mellanox/mlx4/en_rx.c +10 -0
index f26306c..9215940 100644
@@ -779,7 +779,9 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
        struct mlx4_en_rx_alloc *frags;
        struct mlx4_en_rx_desc *rx_desc;
        struct bpf_prog *prog;
+       int doorbell_pending;
        struct sk_buff *skb;
+       int tx_index;
        int index;
        int nr;
        unsigned int length;
@@ -796,6 +798,8 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                return polled;
 
        prog = READ_ONCE(priv->prog);
+       doorbell_pending = 0;
+       tx_index = (priv->tx_ring_num - priv->rsv_tx_rings) + cq->ring;
 
        /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
         * descriptor offset can be deduced from the CQE index instead of
@@ -894,6 +898,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                        switch (act) {
                        case XDP_PASS:
                                break;
+                       case XDP_TX:
+                               if (!mlx4_en_xmit_frame(frags, dev,
+                                                       length, tx_index,
+                                                       &doorbell_pending))
+                                       goto consumed;
+                               break;
                        default:
                                bpf_warn_invalid_xdp_action(act);
                        case XDP_ABORTED:
@@ -1064,6 +1074,9 @@ consumed:
        }
 
Quoted 7 lines

drivers/net/ethernet/mellanox/mlx4/en_rx.c consumed: · line 1079

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +3 -0
 out:
+       if (doorbell_pending)
+               mlx4_en_xmit_doorbell(priv->tx_ring[tx_index]);
+

If in this napi cycle we had at least one packet that went through XDP_PASS (mlx4_en_xmit_frame) you must hit doorbell here,

You mean XDP_TX?

Quoted 6 lines

otherwise if no packet will be forwarded later, this packet will never be really sent and it will wait in HW forever.

The idea is correct to hit the doorbell only at the end of mlx4_en_process_rx_cq cycle to batch tx descriptors and send them in one batch,

Up to a budget of 8

Quoted 3 lines

but you must hit doorbell at the end of the cycle. you can't just assume more RX packets will come in the future to hit the doorbell for you.

I don't assume that. If you look at the code, either:
xmit_frame rings the doorbell, in which case doorbell_pending <- 0 or
xmit_frame doesn't ring the doorbell, in which case doorbell_pending++ So how is a packet left in the ring unannounced?

Quoted 38 lines
Quoted 38 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +1 -0
        AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
        mlx4_cq_set_ci(&cq->mcq);
        wmb(); /* ensure HW sees CQ consumer before we post new buffers */
@@ -1143,6 +1156,7 @@ void mlx4_en_calc_rx_buf(struct net_device *dev)
         * This only works when num_frags == 1.
         */
        if (priv->prog) {
+               dma_dir = PCI_DMA_BIDIRECTIONAL;
                /* This will gain efficient xdp frame recycling at the expense
                 * of more costly truesize accounting
                 */
drivers/net/ethernet/mellanox/mlx4/en_tx.c +14 -1
index c29191e..ba7b5eb 100644
@@ -274,10 +274,28 @@ static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv,
        struct mlx4_en_tx_desc *tx_desc = ring->buf + index * TXBB_SIZE;
        struct mlx4_wqe_data_seg *data = (void *) tx_desc + tx_info->data_offset;
        void *end = ring->buf + ring->buf_size;
-       struct sk_buff *skb = tx_info->skb;
        int nr_maps = tx_info->nr_maps;
+       struct sk_buff *skb;
        int i;
 
+       if (ring->recycle_ring) {
+               struct mlx4_en_rx_alloc frame = {
+                       .page = tx_info->page,
+                       .dma = tx_info->map0_dma,
+                       .page_offset = 0,
+                       .page_size = PAGE_SIZE,
+               };
+
+               if (!mlx4_en_rx_recycle(ring->recycle_ring, &frame)) {
+                       dma_unmap_page(priv->ddev, tx_info->map0_dma,
+                                      PAGE_SIZE, priv->frag_info[0].dma_dir);
+                       put_page(tx_info->page);
+               }
Quoted 8 lines

drivers/net/ethernet/mellanox/mlx4/en_tx.c static u32 mlx4_en_free_tx_desc(struct mlx4_en_priv *priv, · line 295

Quoted 2 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +2 -0
+               return tx_info->nr_txbb;
+       }

This condition will be checked always even for non XDP rings and when XDP is not enabled.
can't we just figure a way not to have this for non XDP rings ? other than having a separate napi handler i don't see a way to do this. on the other hand, new NAPI handler would introduce a lot of code duplication.

Yes I considered a separate napi handler, but again that would be more code duplication than it's worth, IMO.

Quoted 110 lines
Quoted 96 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +81 -0
+
+       skb = tx_info->skb;
+
        /* We do not touch skb here, so prefetch skb->users location
         * to speedup consume_skb()
         */
@@ -476,6 +494,9 @@ static bool mlx4_en_process_tx_cq(struct net_device *dev,
        ACCESS_ONCE(ring->last_nr_txbb) = last_nr_txbb;
        ACCESS_ONCE(ring->cons) = ring_cons + txbbs_skipped;
 
+       if (ring->recycle_ring)
+               return done < budget;
+
        netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
 
        /* Wakeup Tx queue if this stopped, and ring is not full.
@@ -1055,3 +1076,106 @@ tx_drop:
        return NETDEV_TX_OK;
 }
 
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+                              struct net_device *dev, unsigned int length,
+                              int tx_ind, int *doorbell_pending)
+{
+       struct mlx4_en_priv *priv = netdev_priv(dev);
+       union mlx4_wqe_qpn_vlan qpn_vlan = {};
+       struct mlx4_en_tx_ring *ring;
+       struct mlx4_en_tx_desc *tx_desc;
+       struct mlx4_wqe_data_seg *data;
+       struct mlx4_en_tx_info *tx_info;
+       int index, bf_index;
+       bool send_doorbell;
+       int nr_txbb = 1;
+       bool stop_queue;
+       dma_addr_t dma;
+       int real_size;
+       __be32 op_own;
+       u32 ring_cons;
+       bool bf_ok;
+
+       BUILD_BUG_ON_MSG(ALIGN(CTRL_SIZE + DS_SIZE, TXBB_SIZE) != TXBB_SIZE,
+                        "mlx4_en_xmit_frame requires minimum size tx desc");
+
+       ring = priv->tx_ring[tx_ind];
+
+       if (!priv->port_up)
+               goto tx_drop;
+
+       if (mlx4_en_is_tx_ring_full(ring))
+               goto tx_drop;
+
+       /* fetch ring->cons far ahead before needing it to avoid stall */
+       ring_cons = READ_ONCE(ring->cons);
+
+       index = ring->prod & ring->size_mask;
+       tx_info = &ring->tx_info[index];
+
+       bf_ok = ring->bf_enabled;
+
+       /* Track current inflight packets for performance analysis */
+       AVG_PERF_COUNTER(priv->pstats.inflight_avg,
+                        (u32)(ring->prod - ring_cons - 1));
+
+       bf_index = ring->prod;
+       tx_desc = ring->buf + index * TXBB_SIZE;
+       data = &tx_desc->data;
+
+       dma = frame->dma;
+
+       tx_info->page = frame->page;
+       frame->page = NULL;
+       tx_info->map0_dma = dma;
+       tx_info->map0_byte_count = length;
+       tx_info->nr_txbb = nr_txbb;
+       tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);
+       tx_info->data_offset = (void *)data - (void *)tx_desc;
+       tx_info->ts_requested = 0;
+       tx_info->nr_maps = 1;
+       tx_info->linear = 1;
+       tx_info->inl = 0;
+
+       dma_sync_single_for_device(priv->ddev, dma, length, PCI_DMA_TODEVICE);
+
+       data->addr = cpu_to_be64(dma);
+       data->lkey = ring->mr_key;
+       dma_wmb();
+       data->byte_count = cpu_to_be32(length);
+
+       /* tx completion can avoid cache line miss for common cases */
+       tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
+
+       op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
+               ((ring->prod & ring->size) ?
+                cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
+

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1163

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -0
+       ring->packets++;
+       ring->bytes += tx_info->nr_bytes;
+       AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, length);
+
+       ring->prod += nr_txbb;
+
+       stop_queue = mlx4_en_is_tx_ring_full(ring);
+       send_doorbell = stop_queue ||
+                               *doorbell_pending > MLX4_EN_DOORBELL_BUDGET;
+       bf_ok &= send_doorbell;

you missed here one important condition for blue flame, there is max size for sending a BF request.

Quoted 1 line

bf_ok &= desc_size <= MAX_BF;

We have single frag only, so this would be redundant, since it is always true.

Quoted 3 lines

The doorbell accounting here is redundant, and you should always request for doorbell.

If we have a doorbell on every packet, performance will suck. The variable is not if _this_ packet needs a doorbell, its the number of outstanding packets/doorbells. The loop in mlx4_en_process_rx_cq will always catch the remainder if doorbell_pending is non-zero.

Quoted 15 lines

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1174

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +11 -0
+
+       real_size = ((CTRL_SIZE + nr_txbb * DS_SIZE) / 16) & 0x3f;
+
+       if (bf_ok)
+               qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
+       else
+               qpn_vlan.fence_size = real_size;
+
+       mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, TXBB_SIZE, bf_index,
+                             op_own, bf_ok, send_doorbell);
+       *doorbell_pending = send_doorbell ? 0 : *doorbell_pending + 1;

Why not to set doorbell to 1 here ? how do you know more packets will be forwarded ?

I don't know, but that's not how the logic works.

Quoted 64 lines
Quoted 64 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +7 -0
+
+       return NETDEV_TX_OK;
+
+tx_drop:
+       ring->tx_dropped++;
+       return NETDEV_TX_BUSY;
+}
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +13 -2
index 0e0ecd1..7309308 100644
@@ -132,6 +132,7 @@ enum {
                                         MLX4_EN_NUM_UP)
 
 #define MLX4_EN_DEFAULT_TX_WORK                256
+#define MLX4_EN_DOORBELL_BUDGET                8
 
 /* Target number of packets to coalesce with interrupt moderation */
 #define MLX4_EN_RX_COAL_TARGET 44
@@ -219,7 +220,10 @@ enum cq_type {
 
 
 struct mlx4_en_tx_info {
-       struct sk_buff *skb;
+       union {
+               struct sk_buff *skb;
+               struct page *page;
+       };
        dma_addr_t      map0_dma;
        u32             map0_byte_count;
        u32             nr_txbb;
@@ -298,6 +302,7 @@ struct mlx4_en_tx_ring {
        __be32                  mr_key;
        void                    *buf;
        struct mlx4_en_tx_info  *tx_info;
+       struct mlx4_en_rx_ring  *recycle_ring;
        u8                      *bounce_buf;
        struct mlx4_qp_context  context;
        int                     qpn;
@@ -604,6 +609,7 @@ struct mlx4_en_priv {
        struct hwtstamp_config hwtstamp_config;
        u32 counter_index;
        struct bpf_prog *prog;
+       int rsv_tx_rings;
 
 #ifdef CONFIG_MLX4_EN_DCB
 #define MLX4_EN_DCB_ENABLED    0x3
@@ -678,6 +684,12 @@ void mlx4_en_tx_irq(struct mlx4_cq *mcq);
 u16 mlx4_en_select_queue(struct net_device *dev, struct sk_buff *skb,
                         void *accel_priv, select_queue_fallback_t fallback);
 netdev_tx_t mlx4_en_xmit(struct sk_buff *skb, struct net_device *dev);
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+                              struct net_device *dev, unsigned int length,
+                              int tx_ind, int *doorbell_pending);
+void mlx4_en_xmit_doorbell(struct mlx4_en_tx_ring *ring);
+bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
+                       struct mlx4_en_rx_alloc *frame);
 
 int mlx4_en_create_tx_ring(struct mlx4_en_priv *priv,
                           struct mlx4_en_tx_ring **pring,
--

2.8.2

#19 lore raw

Re: [PATCH v7 09/11] net/mlx4_en: add xdp forwarding and data write support

Quoted 2 lines · Brenden Blanco
Quoted 1 line · Saeed Mahameed

On Tue, Jul 12, 2016 at 12:29 AM, Brenden Blanco <[email protected]> wrote:

[...]

Quoted 54 lines
Quoted 54 lines

MAX_TX_RING is a software limitation made to limit netdev real_num_tx queues for CX3 internal cache utilization,
in your case the netdev doesn't care about xdp_tx rings, the accounting you added in this patch adds a lot of complications and it would be better to have clear separation between the two types of tx_rings, in terms of the holding/managing data structure.

I suggest here to leave priv->tx_ring untouched. i.e, don't store the xdp_tx rings at the end of it, i.e priv->tx_ring should only reflect the
netdev real tx queues.

In case of priv->porg is active, we can allocate and store xdp tx ring per rx ring, this tx ring will be allocated and activated once the rx ring is created and activated, and store this dedicated tx ring in the rx_ring it self.

i.e :
struct mlx4_en_rx_ring {
[...]
struct mlx4_en_tx_ring *xdp_tx;
struct mlx4_en_cq *xdp_tx_cq;
[...]
}

for this the following changes are required.

@ mlx4_en_create_rx_ring
[...] // Create the RX ring

/* create CQ for xdp tx ring */
node = cpu_to_node(i % num_online_cpus());

mlx4_en_create_cq(priv, &rx_ring->xdp_tx_cq, prof->tx_ring_size, i, TX, node)

/* create xdp tx ring */
mlx4_en_create_tx_ring(priv, &rx_ring->xdp_tx, prof->tx_ring_size, TXBB_SIZE, node, -1)

@mlx4_en_start/stop_port
/* Configure tx cq's and rings */
// You will need to configure xdp tx rings same as priv->rx_ring_num rings

@mlx4_en_poll_tx_cq
This Also will require a new NAPI handler for xdp rings to replace the following line @mlx4_en_poll_tx_cq
- struct mlx4_en_tx_ring *ring = priv->tx_ring[cq->ring]; with
+ struct mlx4_en_tx_ring *ring = priv->rx_ring[cq->ring].xdp_tx;

Or just change cq->ring from ring index to the actual ring pointer.

Quoted 7 lines
Quoted 4 lines

Bottom line, my suggestion also started to look complicated :).. but still it would look cleaner to separate between netdev rings and xdp rings.

I considered this at first too, but it seemed the worse option to me at the time. There would be a lot of copy/paste as well as new code to review.

We can start from a small refactoring patch that moves code around and extracts the needed helper functions. But it is up to you and Tariq.

it is really non trivial to follow the logic of rsv_tx_rings and tx_ring_num accounting.

Quoted 4 lines
Quoted 3 lines

If in this napi cycle we had at least one packet that went through XDP_PASS (mlx4_en_xmit_frame) you must hit doorbell here,

You mean XDP_TX?

yes

Quoted 7 lines
Quoted 6 lines

otherwise if no packet will be forwarded later, this packet will never be really sent and it will wait in HW forever.

The idea is correct to hit the doorbell only at the end of mlx4_en_process_rx_cq cycle to batch tx descriptors and send them in one batch,

Up to a budget of 8

Quoted 8 lines
Quoted 3 lines

but you must hit doorbell at the end of the cycle. you can't just assume more RX packets will come in the future to hit the doorbell for you.

I don't assume that. If you look at the code, either:
xmit_frame rings the doorbell, in which case doorbell_pending <- 0 or
xmit_frame doesn't ring the doorbell, in which case doorbell_pending++ So how is a packet left in the ring unannounced?

Ooh, now i see, yeap the logic is good.

Quoted 8 lines
Quoted 6 lines

This condition will be checked always even for non XDP rings and when XDP is not enabled.
can't we just figure a way not to have this for non XDP rings ? other than having a separate napi handler i don't see a way to do this. on the other hand, new NAPI handler would introduce a lot of code duplication.

Yes I considered a separate napi handler, but again that would be more code duplication than it's worth, IMO.

Yeah, I agree.

Quoted 110 lines
Quoted 110 lines
Quoted 96 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +81 -0
+
+       skb = tx_info->skb;
+
        /* We do not touch skb here, so prefetch skb->users location
         * to speedup consume_skb()
         */
@@ -476,6 +494,9 @@ static bool mlx4_en_process_tx_cq(struct net_device *dev,
        ACCESS_ONCE(ring->last_nr_txbb) = last_nr_txbb;
        ACCESS_ONCE(ring->cons) = ring_cons + txbbs_skipped;
 
+       if (ring->recycle_ring)
+               return done < budget;
+
        netdev_tx_completed_queue(ring->tx_queue, packets, bytes);
 
        /* Wakeup Tx queue if this stopped, and ring is not full.
@@ -1055,3 +1076,106 @@ tx_drop:
        return NETDEV_TX_OK;
 }
 
+netdev_tx_t mlx4_en_xmit_frame(struct mlx4_en_rx_alloc *frame,
+                              struct net_device *dev, unsigned int length,
+                              int tx_ind, int *doorbell_pending)
+{
+       struct mlx4_en_priv *priv = netdev_priv(dev);
+       union mlx4_wqe_qpn_vlan qpn_vlan = {};
+       struct mlx4_en_tx_ring *ring;
+       struct mlx4_en_tx_desc *tx_desc;
+       struct mlx4_wqe_data_seg *data;
+       struct mlx4_en_tx_info *tx_info;
+       int index, bf_index;
+       bool send_doorbell;
+       int nr_txbb = 1;
+       bool stop_queue;
+       dma_addr_t dma;
+       int real_size;
+       __be32 op_own;
+       u32 ring_cons;
+       bool bf_ok;
+
+       BUILD_BUG_ON_MSG(ALIGN(CTRL_SIZE + DS_SIZE, TXBB_SIZE) != TXBB_SIZE,
+                        "mlx4_en_xmit_frame requires minimum size tx desc");
+
+       ring = priv->tx_ring[tx_ind];
+
+       if (!priv->port_up)
+               goto tx_drop;
+
+       if (mlx4_en_is_tx_ring_full(ring))
+               goto tx_drop;
+
+       /* fetch ring->cons far ahead before needing it to avoid stall */
+       ring_cons = READ_ONCE(ring->cons);
+
+       index = ring->prod & ring->size_mask;
+       tx_info = &ring->tx_info[index];
+
+       bf_ok = ring->bf_enabled;
+
+       /* Track current inflight packets for performance analysis */
+       AVG_PERF_COUNTER(priv->pstats.inflight_avg,
+                        (u32)(ring->prod - ring_cons - 1));
+
+       bf_index = ring->prod;
+       tx_desc = ring->buf + index * TXBB_SIZE;
+       data = &tx_desc->data;
+
+       dma = frame->dma;
+
+       tx_info->page = frame->page;
+       frame->page = NULL;
+       tx_info->map0_dma = dma;
+       tx_info->map0_byte_count = length;
+       tx_info->nr_txbb = nr_txbb;
+       tx_info->nr_bytes = max_t(unsigned int, length, ETH_ZLEN);
+       tx_info->data_offset = (void *)data - (void *)tx_desc;
+       tx_info->ts_requested = 0;
+       tx_info->nr_maps = 1;
+       tx_info->linear = 1;
+       tx_info->inl = 0;
+
+       dma_sync_single_for_device(priv->ddev, dma, length, PCI_DMA_TODEVICE);
+
+       data->addr = cpu_to_be64(dma);
+       data->lkey = ring->mr_key;
+       dma_wmb();
+       data->byte_count = cpu_to_be32(length);
+
+       /* tx completion can avoid cache line miss for common cases */
+       tx_desc->ctrl.srcrb_flags = priv->ctrl_flags;
+
+       op_own = cpu_to_be32(MLX4_OPCODE_SEND) |
+               ((ring->prod & ring->size) ?
+                cpu_to_be32(MLX4_EN_BIT_DESC_OWN) : 0);
+

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1163

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +10 -0
+       ring->packets++;
+       ring->bytes += tx_info->nr_bytes;
+       AVG_PERF_COUNTER(priv->pstats.tx_pktsz_avg, length);
+
+       ring->prod += nr_txbb;
+
+       stop_queue = mlx4_en_is_tx_ring_full(ring);
+       send_doorbell = stop_queue ||
+                               *doorbell_pending > MLX4_EN_DOORBELL_BUDGET;
+       bf_ok &= send_doorbell;

you missed here one important condition for blue flame, there is max size for sending a BF request.

Quoted 3 lines
Quoted 1 line

bf_ok &= desc_size <= MAX_BF;

We have single frag only, so this would be redundant, since it is always true.

We have this one frag assumption all over the mlx4 XDP code, it will take a series reverse engineering work once we decide to support larger MTU (more frags). anyway I agree with you.

Quoted 22 lines
Quoted 3 lines

The doorbell accounting here is redundant, and you should always request for doorbell.

If we have a doorbell on every packet, performance will suck. The variable is not if _this_ packet needs a doorbell, its the number of outstanding packets/doorbells. The loop in mlx4_en_process_rx_cq will always catch the remainder if doorbell_pending is non-zero.

Quoted 15 lines

drivers/net/ethernet/mellanox/mlx4/en_tx.c tx_drop: · line 1174

Quoted 12 lines
drivers/net/ethernet/mellanox/mlx4/en_tx.c +11 -0
+
+       real_size = ((CTRL_SIZE + nr_txbb * DS_SIZE) / 16) & 0x3f;
+
+       if (bf_ok)
+               qpn_vlan.bf_qpn = ring->doorbell_qpn | cpu_to_be32(real_size);
+       else
+               qpn_vlan.fence_size = real_size;
+
+       mlx4_en_tx_write_desc(ring, tx_desc, qpn_vlan, TXBB_SIZE, bf_index,
+                             op_own, bf_ok, send_doorbell);
+       *doorbell_pending = send_doorbell ? 0 : *doorbell_pending + 1;

Why not to set doorbell to 1 here ? how do you know more packets will be forwarded ?

Quoted 1 line

I don't know, but that's not how the logic works.

I see.

Other than the rings separation, this series looks good to me. If you and Tariq decide to keep it this way, I am good with it.

Thanks Brenden, superb work.