Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp101673lqq; Wed, 12 Jun 2024 18:42:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqC6AgwrtwucuTXtJPjsd1It/GZOuVWKDclKT8vpD/t0+XAQM5tCHb9OLcHLKsl5sulN8v0Ir6E7fmaZlnJxdf9sQSTxwgP5O9DV8uMw== X-Google-Smtp-Source: AGHT+IGeNluUiJOA9Pk9IB696H7riO79MkWKTKSuTSfaZhH5eErsvmn59/xxYifprp23mnC7J/4B X-Received: by 2002:a0c:c589:0:b0:6b2:a34c:f7b7 with SMTP id 6a1803df08f44-6b2a34cfafbmr11960916d6.56.1718242922257; Wed, 12 Jun 2024 18:42:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718242922; cv=pass; d=google.com; s=arc-20160816; b=e8VyJbNrxSKABuRGwQ2ykJ8Wceqp6BFA+7H6zrDbPoXevwwVUEk+SmHWUzpxEa6R9B wg4OSEZFPZJMIJd8TV3cESWcdiBBwI7TGFqjSjbjexPMG9x4PyXAgLIkotXkVsgYnKeT JJl2Ts0/eWnOUVv6H2ieRkPLJ8vDd6bRbwkgN2qEHiQf18fWshsYWtqPvaQRcokWpCo9 Gb3FQKnkyeBXGJij3lhy8xINKwLzH5WxWWidGOySerPj3C3TN0GNbPli1rHsvowsw+ir 1TeEcMCBd02ZHvLNNMy0jtW85c6HBN8lmMffgI36aCGsi2erDjHztXV0Xq50kjHH+x31 Vc6g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=DOmuvn8Oj/aVx2P9uVs7PicLin7S83XgYGg6MlGI1pQ=; fh=ESelRI3mzrnxKMhohe5qQqViZfqq0y43fd508dPbwUs=; b=nbxXjTvZUVXHdf/yaxqDG8WZ0F9SrTbfYFYEkmywddodXyRy9DmoCVhyeVb38en/Bs jl6GrN3DRzVt85udDApIOTYfFOUBQl9r7MK1MrDF6ajCQEe9Y8ie45XL24l781V9zDdY kMYoz9k4EJ6+J1toPmRmtDh088sOrI0tcmfAZlQxKcWnui7lwVRxf4fVlO7WHHTdi1O7 eUOR9RxbGx+SNYQC3h7p7MfAWvCmxRQnwEv3mCJhC4gF1561JzIOysk+ZmkUnfVeQrZS BuyabgUyjOThJ5Od7w6iYJixmpJIo3RkiIT3ls7rVXBsfUjwb8d5C8R47CGHkJAZ52hb zoDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xzBcIC9s; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212448-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6b2a5a17245si4499906d6.82.2024.06.12.18.42.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 18:42:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=xzBcIC9s; arc=pass (i=1 spf=pass spfdomain=flex--almasrymina.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-212448-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212448-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E5FC11C212FC for ; Thu, 13 Jun 2024 01:42:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F64113A24E; Thu, 13 Jun 2024 01:36:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xzBcIC9s" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB056130A5C for ; Thu, 13 Jun 2024 01:36:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718242595; cv=none; b=AkhehwKFBsriUwJDdJb+rB4aFoJ+u49Wzfc88OlOnDGSjNQ7c0csMkk8ldlmfDsRH5E6O1RcDiUo0+vlNFf/CZ1rJ6qY8JYBsCyJ1/m5aEYNazgh/ZTL+YffRz8leTL3cAcHIZBoRALgMjTriSDCem/5D2WbZnQgIKkODUbCkkw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718242595; c=relaxed/simple; bh=IpmsKdmAg2dkzE7gg4B1MRUlTQNolccrcZaFLDHfwBA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Li3USIgnhCPot51J11o9kXJIFsC3SZ1sNSVlOIhJZ7wJSfrtckAVk8feo1y7YSiwENbTKzyQ79pO28H8TXIQM+aOtE60mRpkN/fKNRTLgW/uWIRPRAp5nqXxyfvG7UI0rIylxGINeyXKZPkwpLJoIk+DqoFxfUb9jA72Kc9maJ4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xzBcIC9s; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--almasrymina.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dfdfe3d9fc8so880809276.3 for ; Wed, 12 Jun 2024 18:36:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718242584; x=1718847384; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DOmuvn8Oj/aVx2P9uVs7PicLin7S83XgYGg6MlGI1pQ=; b=xzBcIC9s86NLmu5LOqa3Cc0MaXmRvReIXywRKS8HuXbOoH20O4OuQcpwb1V+4QDyGR sp9yBZ8fxbFL9vAXyC1fGh7XduuYrv822KYT+/vqrXj1/jJ0Gjydr4m38GLX4SPi5ykt hNGGYs/cQ7gN64YnW5o/3eh75ASC0m45Zssgpo899j+PGoR3GSeAi8DHyiI16LjSvWJ5 m9Ir4HnD6bOawEq+OXw0aWBGP6x2UT5FRZkkHqPwte6sNeNZS6CZDKHJoNzWdIXtey2k lWxLNkYtrO37K6USc2qYOWdw2UCbVYqvH6CMNZPDF8xA0PorOszAsTZZLp6bW68IXvsV WMkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718242584; x=1718847384; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DOmuvn8Oj/aVx2P9uVs7PicLin7S83XgYGg6MlGI1pQ=; b=nbfz1kHaxWSaT3wP2tyYedHdPepMImJtl60T+RHduEKplyTE0LeXGHlGb/UgdBK7Tl 1Oo8Fdjvgm5rXVLtliGvI2ioAbWBhP0uFmD4p+rTikL2IAx45/LvtBPM/QYEVgP1MlG1 mhw14Z+0GF+XgGzqT2nwsYaznH1FBh6IlBpREu2RLO/J1JRHU2fHExmf5aqsaAH5L4ZT PWhSMvNxqmJ2kZNvHpKe2b30U6cBfkKLRLHEgQ7R6+li43yUWanJ3v2wh3NiUKWnlbax tvFQGJh+qu48hg/J9ewXswYHL3/+TGB4wzHZu06uSDJ4oHe9vykcm6vwlK5JzfX9JQIy kRhQ== X-Forwarded-Encrypted: i=1; AJvYcCX7Pts0gx/q3UB4Yc2+oIgtEFEcoNUSo3O9vNpoDvC1m/BgIryJOeSb82TsHKB+BDFn6swBXuDBsAw2c540+ww5qyo0C4vLuRfTQJZP X-Gm-Message-State: AOJu0Yz2l3T+VCZbRDz8hg8ttg7apIN174zrO+/inoBlqCyAcnK8o+1Z 7Mw8HOLuSBF34Rle9UgmiDE20fYarYyF8Xke6D8cZMqPx6t/elYQbeYTScoGSuHleiC7p7RsRpZ sr5wTT3SJp2s7VjIwjVQEEg== X-Received: from almasrymina.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:4bc5]) (user=almasrymina job=sendgmr) by 2002:a05:6902:f89:b0:dfa:59bc:8857 with SMTP id 3f1490d57ef6-dfe67e3a1e3mr912630276.9.1718242583658; Wed, 12 Jun 2024 18:36:23 -0700 (PDT) Date: Thu, 13 Jun 2024 01:35:49 +0000 In-Reply-To: <20240613013557.1169171-1-almasrymina@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240613013557.1169171-1-almasrymina@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240613013557.1169171-13-almasrymina@google.com> Subject: [PATCH net-next v12 12/13] net: add devmem TCP documentation From: Mina Almasry To: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-arch@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: Mina Almasry , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Donald Hunter , Jonathan Corbet , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Andreas Larsson , Sergey Shtylyov , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Steffen Klassert , Herbert Xu , David Ahern , Willem de Bruijn , Shuah Khan , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Bagas Sanjaya , Christoph Hellwig , Nikolay Aleksandrov , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi Content-Type: text/plain; charset="UTF-8" Add documentation outlining the usage and details of devmem TCP. Signed-off-by: Mina Almasry Reviewed-by: Bagas Sanjaya --- v9: https://lore.kernel.org/netdev/20240403002053.2376017-14-almasrymina@google.com/ - Bagas doc suggestions. v8: - Applied docs suggestions (Randy). Thanks! v7: - Applied docs suggestions (Jakub). v2: - Missing spdx (simon) - add to index.rst (simon) --- Documentation/networking/devmem.rst | 258 ++++++++++++++++++++++++++++ Documentation/networking/index.rst | 1 + 2 files changed, 259 insertions(+) create mode 100644 Documentation/networking/devmem.rst diff --git a/Documentation/networking/devmem.rst b/Documentation/networking/devmem.rst new file mode 100644 index 0000000000000..f32acfd62075d --- /dev/null +++ b/Documentation/networking/devmem.rst @@ -0,0 +1,258 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================= +Device Memory TCP +================= + + +Intro +===== + +Device memory TCP (devmem TCP) enables receiving data directly into device +memory (dmabuf). The feature is currently implemented for TCP sockets. + + +Opportunity +----------- + +A large number of data transfers have device memory as the source and/or +destination. Accelerators drastically increased the prevalence of such +transfers. Some examples include: + +- Distributed training, where ML accelerators, such as GPUs on different hosts, + exchange data. + +- Distributed raw block storage applications transfer large amounts of data with + remote SSDs. Much of this data does not require host processing. + +Typically the Device-to-Device data transfers in the network are implemented as +the following low-level operations: Device-to-Host copy, Host-to-Host network +transfer, and Host-to-Device copy. + +The flow involving host copies is suboptimal, especially for bulk data transfers, +and can put significant strains on system resources such as host memory +bandwidth and PCIe bandwidth. + +Devmem TCP optimizes this use case by implementing socket APIs that enable +the user to receive incoming network packets directly into device memory. + +Packet payloads go directly from the NIC to device memory. + +Packet headers go to host memory and are processed by the TCP/IP stack +normally. The NIC must support header split to achieve this. + +Advantages: + +- Alleviate host memory bandwidth pressure, compared to existing + network-transfer + device-copy semantics. + +- Alleviate PCIe bandwidth pressure, by limiting data transfer to the lowest + level of the PCIe tree, compared to the traditional path which sends data + through the root complex. + + +More Info +--------- + + slides, video + https://netdevconf.org/0x17/sessions/talk/device-memory-tcp.html + + patchset + [RFC PATCH v6 00/12] Device Memory TCP + https://lore.kernel.org/netdev/20240305020153.2787423-1-almasrymina@google.com/ + + +Interface +========= + +Example +------- + +tools/testing/selftests/net/ncdevmem.c:do_server shows an example of setting up +the RX path of this API. + +NIC Setup +--------- + +Header split, flow steering, & RSS are required features for devmem TCP. + +Header split is used to split incoming packets into a header buffer in host +memory, and a payload buffer in device memory. + +Flow steering & RSS are used to ensure that only flows targeting devmem land on +an RX queue bound to devmem. + +Enable header split & flow steering:: + + # enable header split + ethtool -G eth1 tcp-data-split on + + + # enable flow steering + ethtool -K eth1 ntuple on + +Configure RSS to steer all traffic away from the target RX queue (queue 15 in +this example):: + + ethtool --set-rxfh-indir eth1 equal 15 + + +The user must bind a dmabuf to any number of RX queues on a given NIC using +the netlink API:: + + /* Bind dmabuf to NIC RX queue 15 */ + struct netdev_queue *queues; + queues = malloc(sizeof(*queues) * 1); + + queues[0]._present.type = 1; + queues[0]._present.idx = 1; + queues[0].type = NETDEV_RX_QUEUE_TYPE_RX; + queues[0].idx = 15; + + *ys = ynl_sock_create(&ynl_netdev_family, &yerr); + + req = netdev_bind_rx_req_alloc(); + netdev_bind_rx_req_set_ifindex(req, 1 /* ifindex */); + netdev_bind_rx_req_set_dmabuf_fd(req, dmabuf_fd); + __netdev_bind_rx_req_set_queues(req, queues, n_queue_index); + + rsp = netdev_bind_rx(*ys, req); + + dmabuf_id = rsp->dmabuf_id; + + +The netlink API returns a dmabuf_id: a unique ID that refers to this dmabuf +that has been bound. + +Socket Setup +------------ + +The socket must be flow steered to the dmabuf bound RX queue:: + + ethtool -N eth1 flow-type tcp4 ... queue 15, + + +Receiving data +-------------- + +The user application must signal to the kernel that it is capable of receiving +devmem data by passing the MSG_SOCK_DEVMEM flag to recvmsg:: + + ret = recvmsg(fd, &msg, MSG_SOCK_DEVMEM); + +Applications that do not specify the MSG_SOCK_DEVMEM flag will receive an EFAULT +on devmem data. + +Devmem data is received directly into the dmabuf bound to the NIC in 'NIC +Setup', and the kernel signals such to the user via the SCM_DEVMEM_* cmsgs:: + + for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) { + if (cm->cmsg_level != SOL_SOCKET || + (cm->cmsg_type != SCM_DEVMEM_DMABUF && + cm->cmsg_type != SCM_DEVMEM_LINEAR)) + continue; + + dmabuf_cmsg = (struct dmabuf_cmsg *)CMSG_DATA(cm); + + if (cm->cmsg_type == SCM_DEVMEM_DMABUF) { + /* Frag landed in dmabuf. + * + * dmabuf_cmsg->dmabuf_id is the dmabuf the + * frag landed on. + * + * dmabuf_cmsg->frag_offset is the offset into + * the dmabuf where the frag starts. + * + * dmabuf_cmsg->frag_size is the size of the + * frag. + * + * dmabuf_cmsg->frag_token is a token used to + * refer to this frag for later freeing. + */ + + struct dmabuf_token token; + token.token_start = dmabuf_cmsg->frag_token; + token.token_count = 1; + continue; + } + + if (cm->cmsg_type == SCM_DEVMEM_LINEAR) + /* Frag landed in linear buffer. + * + * dmabuf_cmsg->frag_size is the size of the + * frag. + */ + continue; + + } + +Applications may receive 2 cmsgs: + +- SCM_DEVMEM_DMABUF: this indicates the fragment landed in the dmabuf indicated + by dmabuf_id. + +- SCM_DEVMEM_LINEAR: this indicates the fragment landed in the linear buffer. + This typically happens when the NIC is unable to split the packet at the + header boundary, such that part (or all) of the payload landed in host + memory. + +Applications may receive no SO_DEVMEM_* cmsgs. That indicates non-devmem, +regular TCP data that landed on an RX queue not bound to a dmabuf. + + +Freeing frags +------------- + +Frags received via SCM_DEVMEM_DMABUF are pinned by the kernel while the user +processes the frag. The user must return the frag to the kernel via +SO_DEVMEM_DONTNEED:: + + ret = setsockopt(client_fd, SOL_SOCKET, SO_DEVMEM_DONTNEED, &token, + sizeof(token)); + +The user must ensure the tokens are returned to the kernel in a timely manner. +Failure to do so will exhaust the limited dmabuf that is bound to the RX queue +and will lead to packet drops. + + +Implementation & Caveats +======================== + +Unreadable skbs +--------------- + +Devmem payloads are inaccessible to the kernel processing the packets. This +results in a few quirks for payloads of devmem skbs: + +- Loopback is not functional. Loopback relies on copying the payload, which is + not possible with devmem skbs. + +- Software checksum calculation fails. + +- TCP Dump and bpf can't access devmem packet payloads. + + +Testing +======= + +More realistic example code can be found in the kernel source under +tools/testing/selftests/net/ncdevmem.c + +ncdevmem is a devmem TCP netcat. It works very similarly to netcat, but +receives data directly into a udmabuf. + +To run ncdevmem, you need to run it on a server on the machine under test, and +you need to run netcat on a peer to provide the TX data. + +ncdevmem has a validation mode as well that expects a repeating pattern of +incoming data and validates it as such. For example, you can launch +ncdevmem on the server by:: + + ncdevmem -s -c -f eth1 -d 3 -n 0000:06:00.0 -l \ + -p 5201 -v 7 + +On client side, use regular netcat to send TX data to ncdevmem process +on the server:: + + yes $(echo -e \\x01\\x02\\x03\\x04\\x05\\x06) | \ + tr \\n \\0 | head -c 5G | nc 5201 -p 5201 diff --git a/Documentation/networking/index.rst b/Documentation/networking/index.rst index a6443851a1424..f93950a441573 100644 --- a/Documentation/networking/index.rst +++ b/Documentation/networking/index.rst @@ -48,6 +48,7 @@ Contents: cdc_mbim dccp dctcp + devmem dns_resolver driver eql -- 2.45.2.505.gda0bf45e8d-goog