Received: by 2002:ab2:23c8:0:b0:1f2:fdbc:cb93 with SMTP id a8csp196817lqe; Wed, 27 Mar 2024 03:08:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUeb+Ke/sAjGk5AuYN8pFcVCzPb06QRrzsARWExNj6+8KWW8H6u3adm5UsIXkGBSVwQ3cA7GaDWp/YV4ZMWgw2Q0/8cq+mku8OvNMF0YA== X-Google-Smtp-Source: AGHT+IHYwNuNvTzmfs9a8IbRXvySqfL8hGagd6z+1TBHj4a8dTk/QmsI4oNr6LDEFHb13wYbeFW+ X-Received: by 2002:a05:6a20:12c5:b0:1a3:c637:5902 with SMTP id v5-20020a056a2012c500b001a3c6375902mr2145910pzg.42.1711534128944; Wed, 27 Mar 2024 03:08:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711534128; cv=pass; d=google.com; s=arc-20160816; b=gl8wSrqGXihxJcZYSU/eyBHQwmy/Vup8w2CbTv8TDoYzkKv+tqo8fbxVYBwyV9sbDG 1c3Qvmttg69rviQb4iMiCMkgXqmBI3vDS6BwjrcUuUq5CE+IT0BZ+X+QFNnCTCQ1VM3k Ji+lnYW4KG+HyVq3z8FY0A8El0Fjq1IDp9pii5eglGLqNreZbPDNOmdv3a1321XvrcD5 pB5fYPQlbO1awKkvHER4GbBR++WlXBbqwGWjmd9TN8G+9LYKoeJ626iMr9h2OcTFyKFw ecANTbFz9NCDpp2Fwu4M6bJrK+yDoO1kakD5BPFr7F8QNS0o0AcI6C5pMm0Dtc0Dhm09 UPjw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=kP7WIHOLdzRBQLDrSBJg7izGME+taO/PS6KcXmTaXmA=; fh=qlQv4s5uhVJ1S9MrIWzvpZ/wDHZlbKtV9R9zL/pBhhw=; b=yjQgEan0JgCtwvdM4HYKbXH/V2rwX32IX5eNFlOxL4yTTtBwMAo9efwGOA8OZhMsyO HKLcctZcOIQRL+6XIQkXuAlz5WOMxM13Iqf7GZs6B1BmE6m7Ln1psr/mAepuTKeF+1Gr p4dVDdbXW9nY4H8VX2WJcH38/SYORsITwiXN5ekxnGKPOEb85fAV2Cwo9ls6SUzzfSfK WtsAXLBWgFP3nZ2mJZAYfiX3VD4sqGB3H41TZ4Ewnt9DrHOK6sn1Sog3A3TfXWyI6qZK aaAfoA3O0yBLgQJhCUSXANsWiGDLaAi1GO2ABlorOI0T5zrCZGNP2B8/2OD2nMTRi3na Lcxg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=R829kLYe; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-120158-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120158-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id jo30-20020a056a00909e00b006eaccc36ce2si678485pfb.156.2024.03.27.03.08.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 03:08:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-120158-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=R829kLYe; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-120158-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-120158-linux.lists.archive=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 4B84E2C7A20 for ; Wed, 27 Mar 2024 00:39:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5B3A61CD23; Wed, 27 Mar 2024 00:39:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="R829kLYe" Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 117734A32; Wed, 27 Mar 2024 00:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711499954; cv=none; b=RyNI8OalWyDoyfUjo0kIUXgczQGimrc0oD3whX6gXwOaFcLUDFiA+T9lEPLsqz4eiNDamNq3mJt8Id1MHt2cELDp8Vg0aQh+0jH3z4H0CiAOoLJMhw66d0EThgdlQR+MtTSddaWL2i0efwOKIKAZe3QoNJ1NJ3QdSpTCX94563g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711499954; c=relaxed/simple; bh=lM4O1qCIEDxEUxFxG3e/eyhm3l0cH5kZrKl+PEHoY9Q=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fngoa88N03vZE7ce5DMrjGKw69hgi4Zz0rvaLy8FVUr/agF2UjgLiemiolU8pmaHXRu9cFoX0fh2RpWfmXyZWvStfzLP00IjTB8q1MmXLfoEHeTCCtqLTAPX9FkZ+HzvtAgyaD0f0mvs9eN/GHOIufp9S6JoCuO7CF4zrwBxhS8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=R829kLYe; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Sender:Reply-To:Content-ID:Content-Description; bh=kP7WIHOLdzRBQLDrSBJg7izGME+taO/PS6KcXmTaXmA=; b=R829kLYeWxuVRhP3F0l8AK+syA QEH63rPSjU6cE1tUB9vjg/Zru7XGKlHCkGm8UnDZX0WWN4TnXIdlJ0WCiZCc+zozmDKQaebeG8Otb gyunQb5eOnepRvGutL7gjpRAwHFlsqdcqclVFqMr0vyZtopr7+xwWtqWjgaV+MjqFp3U22BjiB+yN hrpvRXIBaXs6tGlDTkTeW+IsWa74VNkZ1lsNunvW7tDimQkfGk1/En8lmoBA8qAVdcqWFJak4HC1p Ae3ZKaz7OrrcZMT5MuKSGX2sdsKSw7PSfpqxR0fKH0RYWoRot45laQOJq0wpX92gC9AGw0DJ8tZEp qhN43EZQ==; Received: from [50.53.2.121] (helo=[192.168.254.15]) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpHJc-00000006zwt-3DsR; Wed, 27 Mar 2024 00:38:52 +0000 Message-ID: Date: Tue, 26 Mar 2024 17:38:47 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH net-next v7 13/14] net: add devmem TCP documentation Content-Language: en-US To: Mina Almasry , 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-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: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Jonathan Corbet , Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , Andreas Larsson , 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_K=C3=B6nig?= , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi References: <20240326225048.785801-1-almasrymina@google.com> <20240326225048.785801-14-almasrymina@google.com> From: Randy Dunlap In-Reply-To: <20240326225048.785801-14-almasrymina@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, On 3/26/24 15:50, Mina Almasry wrote: > Add documentation outlining the usage and details of devmem TCP. > > Signed-off-by: Mina Almasry > > --- > > v7: > - Applied docs suggestions (Jakub). > > v2: > > - Missing spdx (simon) > - add to index.rst (simon) > > --- > Documentation/networking/devmem.rst | 256 ++++++++++++++++++++++++++++ > Documentation/networking/index.rst | 1 + > 2 files changed, 257 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 000000000000..b0899e8e9e83 > --- /dev/null > +++ b/Documentation/networking/devmem.rst > @@ -0,0 +1,256 @@ > +.. 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. SSDs. Much > + > +Typically the Device-to-Device data transfers the network are implemented as the in the network ? > +following low level operations: Device-to-Host copy, Host-to-Host network low-level > +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 traditional path which sends data through to the > + 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> +RX queue bound to devmem. an RX queue ? > + > +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 > +netlink API:: 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 steering to the dmabuf bound RX queue:: flow steered ? > + > + 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 a server on the machine under test, and you it on a server > +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:: > + > + # On server: > + ncdevmem -s -c -f eth1 -d 3 -n 0000:06:00.0 -l \ > + -p 5201 -v 7 > + > + # On client: > + yes $(echo -e \\x01\\x02\\x03\\x04\\x05\\x06) | \ > + tr \\n \\0 | head -c 5G | nc 5201 -p 5201 -- #Randy