[PATCH v7 05/23] nvme-tcp: Add DDP offload control path

Aurelien Aptel
2022-10-25T14:00:57Z

From: Boris Pismenny <borisp@nvidia.com>

This commit introduces direct data placement offload to NVME
TCP. There is a context per queue, which is established after the
handshake using the ulp_ddp_sk_add/del NDOs.

Additionally, a resynchronization routine is used to assist
hardware recovery from TCP OOO, and continue the offload.
Resynchronization operates as follows:

1. TCP OOO causes the NIC HW to stop the offload

2. NIC HW identifies a PDU header at some TCP sequence number,
and asks NVMe-TCP to confirm it.
This request is delivered from the NIC driver to NVMe-TCP by first
finding the socket for the packet that triggered the request, and
then finding the nvme_tcp_queue that is used by this routine.
Finally, the request is recorded in the nvme_tcp_queue.

3. When NVMe-TCP observes the requested TCP sequence, it will compare
it with the PDU header TCP sequence, and report the result to the
NIC driver (ulp_ddp_resync), which will update the HW,
and resume offload when all is successful.

Some HW implementation such as ConnectX-7 assume linear CCID (0...N-1
for queue of size N) where the linux nvme driver uses part of the 16
bit CCID for generation counter. To address that, we use the existing
quirk in the nvme layer when the HW driver advertises if the device is
not supports the full 16 bit CCID range.

Furthermore, we let the offloading driver advertise what is the max hw
sectors/segments via ulp_ddp_limits.

A follow-up patch introduces the data-path changes required for this
offload.

Socket operations need a netdev reference. This reference is
dropped on NETDEV_GOING_DOWN events to allow the device to go down in
a follow-up patch.

Signed-off-by: Boris Pismenny <borisp@nvidia.com>
Signed-off-by: Ben Ben-Ishay <benishay@nvidia.com>
Signed-off-by: Or Gerlitz <ogerlitz@nvidia.com>
Signed-off-by: Yoray Zack <yorayz@nvidia.com>
Signed-off-by: Shai Malin <smalin@nvidia.com>
Signed-off-by: Aurelien Aptel <aaptel@nvidia.com>
Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>
---
drivers/nvme/host/tcp.c | 251 ++++++++++++++++++++++++++++++++++++++--
1 file changed, 242 insertions(+), 9 deletions(-)

diff --git a/drivers/nvme/host/tcp.c b/drivers/nvme/host/tcp.c
index 42b2d86dcfc2..0f065f18dac6 100644
--- a/drivers/nvme/host/tcp.c
+++ b/drivers/nvme/host/tcp.c
@@ -15,6 +15,10 @@
#include <crypto/hash.h>
#include <net/busy_poll.h>

+#ifdef CONFIG_ULP_DDP
+#include <net/ulp_ddp.h>
+#endif
+
#include "nvme.h"
#include "fabrics.h"

@@ -103,6 +107,7 @@ enum nvme_tcp_queue_flags {
NVME_TCP_Q_ALLOCATED = 0,
NVME_TCP_Q_LIVE = 1,
NVME_TCP_Q_POLLING = 2,
+ NVME_TCP_Q_OFF_DDP = 3,
};

enum nvme_tcp_recv_state {
@@ -130,6 +135,15 @@ struct nvme_tcp_queue {
size_t ddgst_remaining;
unsigned int nr_cqe;

+ /*
+ * HW can request a tcp seq num to continue
+ * offload in case of resync.
+ * - The 32 high bits store the seq number
+ * - The 32 low bits are used as a flag to know if a request
+ * is pending (ULP_DDP_RESYNC_PENDING).
+ */
+ atomic64_t resync_req;
+
/* send state */
struct nvme_tcp_request *request;

@@ -170,6 +184,9 @@ struct nvme_tcp_ctrl {
struct delayed_work connect_work;
struct nvme_tcp_request async_req;
u32 io_queues[HCTX_MAX_TYPES];
+
+ struct net_device *offloading_netdev;
+ u32 offload_io_threshold;
};

static LIST_HEAD(nvme_tcp_ctrl_list);
@@ -261,6 +278,190 @@ static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
return nvme_tcp_pdu_data_left(req) <= len;
}

+#ifdef CONFIG_ULP_DDP
+
+static bool nvme_tcp_ddp_query_limits(struct net_device *netdev,
+ struct nvme_tcp_ddp_limits *limits)
+{
+ int ret;
+
+ if (!netdev || !(netdev->features & NETIF_F_HW_ULP_DDP) ||
+ !netdev->ulp_ddp_ops || !netdev->ulp_ddp_ops->ulp_ddp_limits)
+ return false;
+
+ limits->lmt.type = ULP_DDP_NVME;
+ ret = netdev->ulp_ddp_ops->ulp_ddp_limits(netdev, &limits->lmt);
+ if (ret == -EOPNOTSUPP) {
+ return false;
+ } else if (ret) {
+ WARN_ONCE(ret, "ddp limits failed (ret=%d)", ret);
+ return false;
+ }
+
+ return true;
+}
+
+static bool nvme_tcp_resync_request(struct sock *sk, u32 seq, u32 flags);
+static const struct ulp_ddp_ulp_ops nvme_tcp_ddp_ulp_ops = {
+ .resync_request = nvme_tcp_resync_request,
+};
+
+static int nvme_tcp_offload_socket(struct nvme_tcp_queue *queue)
+{
+ struct net_device *netdev = queue->ctrl->offloading_netdev;
+ struct nvme_tcp_ddp_config config = {};
+ struct nvme_tcp_ddp_limits limits = {{ULP_DDP_NVME}};
+ int ret;
+
+ /* device has ULP DPP, check NVMe-TCP offload support */
+
+ if (!nvme_tcp_ddp_query_limits(netdev, &limits))
+ return 0;
+
+ if (!(limits.lmt.offload_capabilities & ULP_DDP_C_NVME_TCP))
+ return 0;
+
+ config.cfg.type = ULP_DDP_NVME;
+ config.pfv = NVME_TCP_PFV_1_0;
+ config.cpda = 0;
+ config.dgst = queue->hdr_digest ?
+ NVME_TCP_HDR_DIGEST_ENABLE : 0;
+ config.dgst |= queue->data_digest ?
+ NVME_TCP_DATA_DIGEST_ENABLE : 0;
+ config.queue_size = queue->queue_size;
+ config.queue_id = nvme_tcp_queue_id(queue);
+ config.io_cpu = queue->io_cpu;
+
+ /* Socket ops keep a netdev reference. It is put in
+ * nvme_tcp_unoffload_socket(). This ref is dropped on
+ * NETDEV_GOING_DOWN events to allow the device to go down
+ */
+ dev_hold(netdev);
+ ret = netdev->ulp_ddp_ops->ulp_ddp_sk_add(netdev,
+ queue->sock->sk,
+ &config.cfg);
+ if (ret) {
+ dev_put(netdev);
+ return ret;
+ }
+
+ inet_csk(queue->sock->sk)->icsk_ulp_ddp_ops = &nvme_tcp_ddp_ulp_ops;
+ set_bit(NVME_TCP_Q_OFF_DDP, &queue->flags);
+ return 0;
+}
+
+static void nvme_tcp_unoffload_socket(struct nvme_tcp_queue *queue)
+{
+ struct net_device *netdev = queue->ctrl->offloading_netdev;
+
+ if (!netdev) {
+ dev_info_ratelimited(queue->ctrl->ctrl.device, "netdev not found\n");
+ return;
+ }
+
+ clear_bit(NVME_TCP_Q_OFF_DDP, &queue->flags);
+
+ netdev->ulp_ddp_ops->ulp_ddp_sk_del(netdev, queue->sock->sk);
+
+ inet_csk(queue->sock->sk)->icsk_ulp_ddp_ops = NULL;
+ dev_put(netdev); /* held by offload_socket */
+}
+
+static void nvme_tcp_offload_limits(struct nvme_tcp_queue *queue, struct net_device *netdev)
+{
+ struct nvme_tcp_ddp_limits limits = {{ULP_DDP_NVME}};
+
+ if (!nvme_tcp_ddp_query_limits(netdev, &limits)) {
+ queue->ctrl->offloading_netdev = NULL;
+ return;
+ }
+
+ queue->ctrl->offloading_netdev = netdev;
+ dev_dbg_ratelimited(queue->ctrl->ctrl.device,
+ "netdev %s offload limits: max_ddp_sgl_len %d\n",
+ netdev->name, limits.lmt.max_ddp_sgl_len);
+ queue->ctrl->ctrl.max_segments = limits.lmt.max_ddp_sgl_len;
+ queue->ctrl->ctrl.max_hw_sectors =
+ limits.lmt.max_ddp_sgl_len << (ilog2(SZ_4K) - 9);
+ queue->ctrl->offload_io_threshold = limits.lmt.io_threshold;
+
+ /* offloading HW doesn't support full ccid range, apply the quirk */
+ queue->ctrl->ctrl.quirks |= limits.full_ccid_range ? 0 : NVME_QUIRK_SKIP_CID_GEN;
+}
+
+/* In presence of packet drops or network packet reordering, the device may lose
+ * synchronization between the TCP stream and the L5P framing, and require a
+ * resync with the kernel's TCP stack.
+ *
+ * - NIC HW identifies a PDU header at some TCP sequence number,
+ * and asks NVMe-TCP to confirm it.
+ * - When NVMe-TCP observes the requested TCP sequence, it will compare
+ * it with the PDU header TCP sequence, and report the result to the
+ * NIC driver
+ */
+static void nvme_tcp_resync_response(struct nvme_tcp_queue *queue,
+ struct sk_buff *skb, unsigned int offset)
+{
+ u64 pdu_seq = TCP_SKB_CB(skb)->seq + offset - queue->pdu_offset;
+ struct net_device *netdev = queue->ctrl->offloading_netdev;
+ u64 pdu_val = (pdu_seq << 32) | ULP_DDP_RESYNC_PENDING;
+ u64 resync_val;
+ u32 resync_seq;
+
+ resync_val = atomic64_read(&queue->resync_req);
+ /* Lower 32 bit flags. Check validity of the request */
+ if ((resync_val & ULP_DDP_RESYNC_PENDING) == 0)
+ return;
+
+ /* Obtain and check requested sequence number: is this PDU header before the request? */
+ resync_seq = resync_val >> 32;
+ if (before(pdu_seq, resync_seq))
+ return;
+
+ /*
+ * The atomic operation guarantees that we don't miss any NIC driver
+ * resync requests submitted after the above checks.
+ */
+ if (atomic64_cmpxchg(&queue->resync_req, pdu_val,
+ pdu_val & ~ULP_DDP_RESYNC_PENDING) !=
+ atomic64_read(&queue->resync_req))
+ netdev->ulp_ddp_ops->ulp_ddp_resync(netdev, queue->sock->sk, pdu_seq);
+}
+
+static bool nvme_tcp_resync_request(struct sock *sk, u32 seq, u32 flags)
+{
+ struct nvme_tcp_queue *queue = sk->sk_user_data;
+
+ /*
+ * "seq" (TCP seq number) is what the HW assumes is the
+ * beginning of a PDU. The nvme-tcp layer needs to store the
+ * number along with the "flags" (ULP_DDP_RESYNC_PENDING) to
+ * indicate that a request is pending.
+ */
+ atomic64_set(&queue->resync_req, (((uint64_t)seq << 32) | flags));
+
+ return true;
+}
+
+#else
+
+static int nvme_tcp_offload_socket(struct nvme_tcp_queue *queue)
+{
+ return 0;
+}
+
+static void nvme_tcp_unoffload_socket(struct nvme_tcp_queue *queue)
+{}
+
+static void nvme_tcp_offload_limits(struct nvme_tcp_queue *queue, struct net_device *netdev)
+{}
+
+static void nvme_tcp_resync_response(struct nvme_tcp_queue *queue,
+ struct sk_buff *skb, unsigned int offset)
+{}
+
+#endif
+
static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
unsigned int dir)
{
@@ -703,6 +904,9 @@ static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
int ret;

+ if (test_bit(NVME_TCP_Q_OFF_DDP, &queue->flags))
+ nvme_tcp_resync_response(queue, skb, *offset);
+
ret = skb_copy_bits(skb, *offset,
&pdu[queue->pdu_offset], rcv_len);
if (unlikely(ret))
@@ -1650,6 +1854,8 @@ static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
kernel_sock_shutdown(queue->sock, SHUT_RDWR);
nvme_tcp_restore_sock_calls(queue);
cancel_work_sync(&queue->io_work);
+ if (test_bit(NVME_TCP_Q_OFF_DDP, &queue->flags))
+ nvme_tcp_unoffload_socket(queue);
}

static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
@@ -1669,21 +1875,48 @@ static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
{
struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
+ struct net_device *netdev;
int ret;

- if (idx)
+ if (idx) {
ret = nvmf_connect_io_queue(nctrl, idx);
- else
+ if (ret)
+ goto err;
+
+ netdev = ctrl->queues[idx].ctrl->offloading_netdev;
+ if (netdev && (netdev->features & NETIF_F_HW_ULP_DDP)) {
+ ret = nvme_tcp_offload_socket(&ctrl->queues[idx]);
+ if (ret) {
+ dev_err(nctrl->device,
+ "failed to setup offload on queue %d ret=%d\n",
+ idx, ret);
+ }
+ }
+ } else {
ret = nvmf_connect_admin_queue(nctrl);
+ if (ret)
+ goto err;

- if (!ret) {
- set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
- } else {
- if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
- __nvme_tcp_stop_queue(&ctrl->queues[idx]);
- dev_err(nctrl->device,
- "failed to connect queue: %d ret=%d\n", idx, ret);
+ netdev = get_netdev_for_sock(ctrl->queues[idx].sock->sk);
+ if (!netdev) {
+ dev_info_ratelimited(ctrl->ctrl.device, "netdev not found\n");
+ ctrl->offloading_netdev = NULL;
+ goto done;
+ }
+ if (netdev->features & NETIF_F_HW_ULP_DDP)
+ nvme_tcp_offload_limits(&ctrl->queues[idx], netdev);
+ /* release the device as no offload context is established yet. */
+ dev_put(netdev);
}
+
+done:
+ set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
+ return 0;
+err:
+ if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
+ __nvme_tcp_stop_queue(&ctrl->queues[idx]);
+ dev_err(nctrl->device,
+ "failed to connect queue: %d ret=%d\n", idx, ret);
return ret;
}

--
2.31.1