From nobody Sat Nov 30 10:30:17 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 7FC611A4E93 for ; Tue, 10 Sep 2024 17:15:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725988531; cv=none; b=MJ4pz4wRVpzoZgweqrBnOab+3DzN/Qa/Tl/e+RyJr04YhKNXZnGx8f7chfoiOUoiZK6HUT6MOMwfFNF3W54uHp8pbItw9+s8w5tSSfa5/5GpdLtZrvYLb9KWw6Hxj85JBfh/+mdl083XIZSz1NOnyIw4VQhFUR+K6rrCOjrepW8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725988531; c=relaxed/simple; bh=+WKjAvj/SJsZ+i2BPYrYgM6EjBBDmMaV/3m9rOmpwfc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EXU/S4BXSnM9BWPQ4ID/ZzMOObtHQfGGoB4O10IMQfhENlgI9xBOm97nU/KMeoR5ExAH09IlaQb/jEctlBOF0YrzYvNkTQL1ezqUAyuE+r0GGM7FvgKIHRdnYBj64zp7EjiyqkWqT/udLYi3+ckVUmldJIrH6858pwYl21JM9Yc= 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=nCMd27Bd; arc=none smtp.client-ip=209.85.128.202 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nCMd27Bd" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6b41e02c293so211896037b3.0 for ; Tue, 10 Sep 2024 10:15:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725988523; x=1726593323; 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=HTq1ahNtP7bfKTiUYMOkvsIp91vuWez2yTTAEimYEQM=; b=nCMd27Bd6a2Z31fJgMm6iaIBVkQpi5eBtt9XhUfZltvgvsVNXSRQ8GephFCedVhvIY BE38Du30Y/xnVT0jNTSK5/28g9oV0PB5hyjDe2N0OmHsOZHYHB7wmfxqik8nzrsHCEDF bRevw4fbsh37ynHPGf457BVw0pbdssLbmQx8ZARPaSlhE49lNVAeBoQ0eEIvbvmj2I9r a4Uu0+ULITPB8zOuMGZPjKjDG8H5hTeh1HPBilDg1UZ3BlAos43/9nvvezjPaNa02ETo 2a7yH2BOCqgdVsmMMmFwCa0M/3W0Eks+x0KYwcvdIw4LjFhxvLw0Bs5zKLuJEZ1fUdue c8hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725988523; x=1726593323; 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=HTq1ahNtP7bfKTiUYMOkvsIp91vuWez2yTTAEimYEQM=; b=hTAWoJwTWuNa35wza+j020MBPyaCVZaZlbitoXZDrtja6PnBROxs0wGeoZMWydB49D hvUeXNTFSLsSUN+gtBKH+Z7dYD1lrU6KLeJ5XoInK05QSLCrXRnwOs5P4YUBfoVqVrhX Jf/+zu7Aon/tMQ1YifXxwegXua55NLBH4c8KBj9hllRf/HBQGtQbicEgiC+54Oyrs8Ig 0h7e8CKUMkUCLT4hkfQ7n4ziLu7funJztqqAKUrN/8R6w5YRUzVWNZCL8Bkdxi66JT3O MEvQJoAKK2D4t943/B97dyGBDZX7rUF+61hzWqpwNWaNuTf6AOXz4waOQLOQU15ahNiG 4CWA== X-Forwarded-Encrypted: i=1; AJvYcCW/ttgI+LuihGe6qxkd8rjnA6a4xaX+YiwGNRJuvHi5LhhTN01l2QIyCoc0wZuD0pNCGNRDlrQJe48sa5A=@vger.kernel.org X-Gm-Message-State: AOJu0YwVmmFApA7Ru/A9UbW2XSz4dQy78oEHqYvF6GlXp6rQunLn4QP/ v2f3j3c5h20dS/V8DKNPl9ztln2060i+2O4JXpGH8/4+Q2T9h4fjQ8I8B6u9E27fcUlLDqzsWrV u2naQEIt6DShWHp+mUr5o7g== X-Google-Smtp-Source: AGHT+IHbKiU5Q8+dHZquZIv0LqCZM0wO2eTwj0zxmzgfdByDL8Dus1u/NP5oVQmANKL45UYWCAHcRkJZDFySudCiyA== X-Received: from almasrymina.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:4bc5]) (user=almasrymina job=sendgmr) by 2002:a05:690c:3601:b0:6d7:37b9:ac93 with SMTP id 00721157ae682-6db45168382mr9520137b3.5.1725988523149; Tue, 10 Sep 2024 10:15:23 -0700 (PDT) Date: Tue, 10 Sep 2024 17:14:56 +0000 In-Reply-To: <20240910171458.219195-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: <20240910171458.219195-1-almasrymina@google.com> X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: <20240910171458.219195-13-almasrymina@google.com> Subject: [PATCH net-next v26 12/13] selftests: add ncdevmem, netcat for devmem TCP 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-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 , Jesper Dangaard Brouer , Ilias Apalodimas , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Arnd Bergmann , Steffen Klassert , Herbert Xu , David Ahern , Willem de Bruijn , "=?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?=" , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , Shuah Khan , Alexei Starovoitov , Daniel Borkmann , John Fastabend , Sumit Semwal , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , Pavel Begunkov , David Wei , Jason Gunthorpe , Yunsheng Lin , Shailend Chand , Harshitha Ramamurthy , Shakeel Butt , Jeroen de Borst , Praveen Kaligineedi , Bagas Sanjaya , Christoph Hellwig , Nikolay Aleksandrov , Taehee Yoo , Stanislav Fomichev Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ncdevmem is a devmem TCP netcat. It works similarly to netcat, but it sends and receives data using the devmem TCP APIs. It uses udmabuf as the dmabuf provider. It is compatible with a regular netcat running on a peer, or a ncdevmem running on a peer. In addition to normal netcat support, ncdevmem has a validation mode, where it sends a specific pattern and validates this pattern on the receiver side to ensure data integrity. Suggested-by: Stanislav Fomichev Signed-off-by: Mina Almasry --- v24: - Add *.d to .gitignore, to stop tracking this file the build generates: ``` git status ... ?? tools/net/ynl/lib/ynl.d ``` v22: - Add run_command helper. It reduces boiler plate and prints the commands it is running. v20: - Remove unnecessary sleep(1) - Add test to ensure dmabuf binding fails if header split is disabled. v19: - Check return code of ethtool commands. - Add test for deactivating mp bound rx queues - Add test for attempting to bind with missing netlink attributes. v16: - Remove outdated -n option (Taehee). - Use 'ifname' instead of accidentally hardcoded 'eth1'. (Taehee) - Remove dead code 'iterations' (Taehee). - Use if_nametoindex() instead of passing device index (Taehee). v15: - Fix linking against libynl. (Jakub) v9: https://lore.kernel.org/netdev/20240403002053.2376017-15-almasrymina@go= ogle.com/ - Remove unused nic_pci_addr entry (Cong). v6: - Updated to bind 8 queues. - Added RSS configuration. - Added some more tests for the netlink API. Changes in v1: - Many more general cleanups (Willem). - Removed driver reset (Jakub). - Removed hardcoded if index (Paolo). RFC v2: - General cleanups (Willem). --- tools/net/ynl/lib/.gitignore | 1 + tools/testing/selftests/net/.gitignore | 1 + tools/testing/selftests/net/Makefile | 9 + tools/testing/selftests/net/ncdevmem.c | 570 +++++++++++++++++++++++++ 4 files changed, 581 insertions(+) create mode 100644 tools/testing/selftests/net/ncdevmem.c diff --git a/tools/net/ynl/lib/.gitignore b/tools/net/ynl/lib/.gitignore index c18dd8d83cee..296c4035dbf2 100644 --- a/tools/net/ynl/lib/.gitignore +++ b/tools/net/ynl/lib/.gitignore @@ -1 +1,2 @@ __pycache__/ +*.d diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftes= ts/net/.gitignore index 923bf098e2eb..1c04c780db66 100644 --- a/tools/testing/selftests/net/.gitignore +++ b/tools/testing/selftests/net/.gitignore @@ -17,6 +17,7 @@ ipv6_flowlabel ipv6_flowlabel_mgr log.txt msg_zerocopy +ncdevmem nettest psock_fanout psock_snd diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests= /net/Makefile index 27362e40eb37..fc1c3a64de2d 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile @@ -97,6 +97,11 @@ TEST_PROGS +=3D fq_band_pktlimit.sh TEST_PROGS +=3D vlan_hw_filter.sh TEST_PROGS +=3D bpf_offload.py =20 +# YNL files, must be before "include ..lib.mk" +EXTRA_CLEAN +=3D $(OUTPUT)/libynl.a +YNL_GEN_FILES :=3D ncdevmem +TEST_GEN_FILES +=3D $(YNL_GEN_FILES) + TEST_FILES :=3D settings TEST_FILES +=3D in_netns.sh lib.sh net_helper.sh setup_loopback.sh setup_v= eth.sh =20 @@ -106,6 +111,10 @@ TEST_INCLUDES :=3D forwarding/lib.sh =20 include ../lib.mk =20 +# YNL build +YNL_GENS :=3D netdev +include ynl.mk + $(OUTPUT)/epoll_busy_poll: LDLIBS +=3D -lcap $(OUTPUT)/reuseport_bpf_numa: LDLIBS +=3D -lnuma $(OUTPUT)/tcp_mmap: LDLIBS +=3D -lpthread -lcrypto diff --git a/tools/testing/selftests/net/ncdevmem.c b/tools/testing/selftes= ts/net/ncdevmem.c new file mode 100644 index 000000000000..64d6805381c5 --- /dev/null +++ b/tools/testing/selftests/net/ncdevmem.c @@ -0,0 +1,570 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#define __EXPORTED_HEADERS__ + +#include +#include +#include +#include +#include +#include +#include +#define __iovec_defined +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "netdev-user.h" +#include + +#define PAGE_SHIFT 12 +#define TEST_PREFIX "ncdevmem" +#define NUM_PAGES 16000 + +#ifndef MSG_SOCK_DEVMEM +#define MSG_SOCK_DEVMEM 0x2000000 +#endif + +/* + * tcpdevmem netcat. Works similarly to netcat but does device memory TCP + * instead of regular TCP. Uses udmabuf to mock a dmabuf provider. + * + * Usage: + * + * On server: + * ncdevmem -s -c -f eth1 -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 + * + * Note this is compatible with regular netcat. i.e. the sender or receive= r can + * be replaced with regular netcat to test the RX or TX path in isolation. + */ + +static char *server_ip =3D "192.168.1.4"; +static char *client_ip =3D "192.168.1.2"; +static char *port =3D "5201"; +static size_t do_validation; +static int start_queue =3D 8; +static int num_queues =3D 8; +static char *ifname =3D "eth1"; +static unsigned int ifindex; +static unsigned int dmabuf_id; + +void print_bytes(void *ptr, size_t size) +{ + unsigned char *p =3D ptr; + int i; + + for (i =3D 0; i < size; i++) + printf("%02hhX ", p[i]); + printf("\n"); +} + +void print_nonzero_bytes(void *ptr, size_t size) +{ + unsigned char *p =3D ptr; + unsigned int i; + + for (i =3D 0; i < size; i++) + putchar(p[i]); + printf("\n"); +} + +void validate_buffer(void *line, size_t size) +{ + static unsigned char seed =3D 1; + unsigned char *ptr =3D line; + int errors =3D 0; + size_t i; + + for (i =3D 0; i < size; i++) { + if (ptr[i] !=3D seed) { + fprintf(stderr, + "Failed validation: expected=3D%u, actual=3D%u, index=3D%lu\n", + seed, ptr[i], i); + errors++; + if (errors > 20) + error(1, 0, "validation failed."); + } + seed++; + if (seed =3D=3D do_validation) + seed =3D 0; + } + + fprintf(stdout, "Validated buffer\n"); +} + +#define run_command(cmd, ...) \ + ({ \ + char command[256]; \ + memset(command, 0, sizeof(command)); \ + snprintf(command, sizeof(command), cmd, ##__VA_ARGS__); \ + printf("Running: %s\n", command); \ + system(command); \ + }) + +static int reset_flow_steering(void) +{ + int ret =3D 0; + + ret =3D run_command("sudo ethtool -K %s ntuple off", ifname); + if (ret) + return ret; + + return run_command("sudo ethtool -K %s ntuple on", ifname); +} + +static int configure_headersplit(bool on) +{ + return run_command("sudo ethtool -G %s tcp-data-split %s", ifname, + on ? "on" : "off"); +} + +static int configure_rss(void) +{ + return run_command("sudo ethtool -X %s equal %d", ifname, start_queue); +} + +static int configure_channels(unsigned int rx, unsigned int tx) +{ + return run_command("sudo ethtool -L %s rx %u tx %u", ifname, rx, tx); +} + +static int configure_flow_steering(void) +{ + return run_command("sudo ethtool -N %s flow-type tcp4 src-ip %s dst-ip %s= src-port %s dst-port %s queue %d", + ifname, client_ip, server_ip, port, port, start_queue); +} + +static int bind_rx_queue(unsigned int ifindex, unsigned int dmabuf_fd, + struct netdev_queue_id *queues, + unsigned int n_queue_index, struct ynl_sock **ys) +{ + struct netdev_bind_rx_req *req =3D NULL; + struct netdev_bind_rx_rsp *rsp =3D NULL; + struct ynl_error yerr; + + *ys =3D ynl_sock_create(&ynl_netdev_family, &yerr); + if (!*ys) { + fprintf(stderr, "YNL: %s\n", yerr.msg); + return -1; + } + + req =3D netdev_bind_rx_req_alloc(); + netdev_bind_rx_req_set_ifindex(req, ifindex); + netdev_bind_rx_req_set_fd(req, dmabuf_fd); + __netdev_bind_rx_req_set_queues(req, queues, n_queue_index); + + rsp =3D netdev_bind_rx(*ys, req); + if (!rsp) { + perror("netdev_bind_rx"); + goto err_close; + } + + if (!rsp->_present.id) { + perror("id not present"); + goto err_close; + } + + printf("got dmabuf id=3D%d\n", rsp->id); + dmabuf_id =3D rsp->id; + + netdev_bind_rx_req_free(req); + netdev_bind_rx_rsp_free(rsp); + + return 0; + +err_close: + fprintf(stderr, "YNL failed: %s\n", (*ys)->err.msg); + netdev_bind_rx_req_free(req); + ynl_sock_destroy(*ys); + return -1; +} + +static void create_udmabuf(int *devfd, int *memfd, int *buf, size_t dmabuf= _size) +{ + struct udmabuf_create create; + int ret; + + *devfd =3D open("/dev/udmabuf", O_RDWR); + if (*devfd < 0) { + error(70, 0, + "%s: [skip,no-udmabuf: Unable to access DMA buffer device file]\n", + TEST_PREFIX); + } + + *memfd =3D memfd_create("udmabuf-test", MFD_ALLOW_SEALING); + if (*memfd < 0) + error(70, 0, "%s: [skip,no-memfd]\n", TEST_PREFIX); + + /* Required for udmabuf */ + ret =3D fcntl(*memfd, F_ADD_SEALS, F_SEAL_SHRINK); + if (ret < 0) + error(73, 0, "%s: [skip,fcntl-add-seals]\n", TEST_PREFIX); + + ret =3D ftruncate(*memfd, dmabuf_size); + if (ret =3D=3D -1) + error(74, 0, "%s: [FAIL,memfd-truncate]\n", TEST_PREFIX); + + memset(&create, 0, sizeof(create)); + + create.memfd =3D *memfd; + create.offset =3D 0; + create.size =3D dmabuf_size; + *buf =3D ioctl(*devfd, UDMABUF_CREATE, &create); + if (*buf < 0) + error(75, 0, "%s: [FAIL, create udmabuf]\n", TEST_PREFIX); +} + +int do_server(void) +{ + char ctrl_data[sizeof(int) * 20000]; + struct netdev_queue_id *queues; + size_t non_page_aligned_frags =3D 0; + struct sockaddr_in client_addr; + struct sockaddr_in server_sin; + size_t page_aligned_frags =3D 0; + int devfd, memfd, buf, ret; + size_t total_received =3D 0; + socklen_t client_addr_len; + bool is_devmem =3D false; + char *buf_mem =3D NULL; + struct ynl_sock *ys; + size_t dmabuf_size; + char iobuf[819200]; + char buffer[256]; + int socket_fd; + int client_fd; + size_t i =3D 0; + int opt =3D 1; + + dmabuf_size =3D getpagesize() * NUM_PAGES; + + create_udmabuf(&devfd, &memfd, &buf, dmabuf_size); + + if (reset_flow_steering()) + error(1, 0, "Failed to reset flow steering\n"); + + /* Configure RSS to divert all traffic from our devmem queues */ + if (configure_rss()) + error(1, 0, "Failed to configure rss\n"); + + /* Flow steer our devmem flows to start_queue */ + if (configure_flow_steering()) + error(1, 0, "Failed to configure flow steering\n"); + + sleep(1); + + queues =3D malloc(sizeof(*queues) * num_queues); + + for (i =3D 0; i < num_queues; i++) { + queues[i]._present.type =3D 1; + queues[i]._present.id =3D 1; + queues[i].type =3D NETDEV_QUEUE_TYPE_RX; + queues[i].id =3D start_queue + i; + } + + if (bind_rx_queue(ifindex, buf, queues, num_queues, &ys)) + error(1, 0, "Failed to bind\n"); + + buf_mem =3D mmap(NULL, dmabuf_size, PROT_READ | PROT_WRITE, MAP_SHARED, + buf, 0); + if (buf_mem =3D=3D MAP_FAILED) + error(1, 0, "mmap()"); + + server_sin.sin_family =3D AF_INET; + server_sin.sin_port =3D htons(atoi(port)); + + ret =3D inet_pton(server_sin.sin_family, server_ip, &server_sin.sin_addr); + if (socket < 0) + error(79, 0, "%s: [FAIL, create socket]\n", TEST_PREFIX); + + socket_fd =3D socket(server_sin.sin_family, SOCK_STREAM, 0); + if (socket < 0) + error(errno, errno, "%s: [FAIL, create socket]\n", TEST_PREFIX); + + ret =3D setsockopt(socket_fd, SOL_SOCKET, SO_REUSEPORT, &opt, + sizeof(opt)); + if (ret) + error(errno, errno, "%s: [FAIL, set sock opt]\n", TEST_PREFIX); + + ret =3D setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, + sizeof(opt)); + if (ret) + error(errno, errno, "%s: [FAIL, set sock opt]\n", TEST_PREFIX); + + printf("binding to address %s:%d\n", server_ip, + ntohs(server_sin.sin_port)); + + ret =3D bind(socket_fd, &server_sin, sizeof(server_sin)); + if (ret) + error(errno, errno, "%s: [FAIL, bind]\n", TEST_PREFIX); + + ret =3D listen(socket_fd, 1); + if (ret) + error(errno, errno, "%s: [FAIL, listen]\n", TEST_PREFIX); + + client_addr_len =3D sizeof(client_addr); + + inet_ntop(server_sin.sin_family, &server_sin.sin_addr, buffer, + sizeof(buffer)); + printf("Waiting or connection on %s:%d\n", buffer, + ntohs(server_sin.sin_port)); + client_fd =3D accept(socket_fd, &client_addr, &client_addr_len); + + inet_ntop(client_addr.sin_family, &client_addr.sin_addr, buffer, + sizeof(buffer)); + printf("Got connection from %s:%d\n", buffer, + ntohs(client_addr.sin_port)); + + while (1) { + struct iovec iov =3D { .iov_base =3D iobuf, + .iov_len =3D sizeof(iobuf) }; + struct dmabuf_cmsg *dmabuf_cmsg =3D NULL; + struct dma_buf_sync sync =3D { 0 }; + struct cmsghdr *cm =3D NULL; + struct msghdr msg =3D { 0 }; + struct dmabuf_token token; + ssize_t ret; + + is_devmem =3D false; + printf("\n\n"); + + msg.msg_iov =3D &iov; + msg.msg_iovlen =3D 1; + msg.msg_control =3D ctrl_data; + msg.msg_controllen =3D sizeof(ctrl_data); + ret =3D recvmsg(client_fd, &msg, MSG_SOCK_DEVMEM); + printf("recvmsg ret=3D%ld\n", ret); + if (ret < 0 && (errno =3D=3D EAGAIN || errno =3D=3D EWOULDBLOCK)) + continue; + if (ret < 0) { + perror("recvmsg"); + continue; + } + if (ret =3D=3D 0) { + printf("client exited\n"); + goto cleanup; + } + + i++; + for (cm =3D CMSG_FIRSTHDR(&msg); cm; cm =3D CMSG_NXTHDR(&msg, cm)) { + if (cm->cmsg_level !=3D SOL_SOCKET || + (cm->cmsg_type !=3D SCM_DEVMEM_DMABUF && + cm->cmsg_type !=3D SCM_DEVMEM_LINEAR)) { + fprintf(stdout, "skipping non-devmem cmsg\n"); + continue; + } + + dmabuf_cmsg =3D (struct dmabuf_cmsg *)CMSG_DATA(cm); + is_devmem =3D true; + + if (cm->cmsg_type =3D=3D SCM_DEVMEM_LINEAR) { + /* TODO: process data copied from skb's linear + * buffer. + */ + fprintf(stdout, + "SCM_DEVMEM_LINEAR. dmabuf_cmsg->frag_size=3D%u\n", + dmabuf_cmsg->frag_size); + + continue; + } + + token.token_start =3D dmabuf_cmsg->frag_token; + token.token_count =3D 1; + + total_received +=3D dmabuf_cmsg->frag_size; + printf("received frag_page=3D%llu, in_page_offset=3D%llu, frag_offset= =3D%llu, frag_size=3D%u, token=3D%u, total_received=3D%lu, dmabuf_id=3D%u\n= ", + dmabuf_cmsg->frag_offset >> PAGE_SHIFT, + dmabuf_cmsg->frag_offset % getpagesize(), + dmabuf_cmsg->frag_offset, dmabuf_cmsg->frag_size, + dmabuf_cmsg->frag_token, total_received, + dmabuf_cmsg->dmabuf_id); + + if (dmabuf_cmsg->dmabuf_id !=3D dmabuf_id) + error(1, 0, + "received on wrong dmabuf_id: flow steering error\n"); + + if (dmabuf_cmsg->frag_size % getpagesize()) + non_page_aligned_frags++; + else + page_aligned_frags++; + + sync.flags =3D DMA_BUF_SYNC_READ | DMA_BUF_SYNC_START; + ioctl(buf, DMA_BUF_IOCTL_SYNC, &sync); + + if (do_validation) + validate_buffer( + ((unsigned char *)buf_mem) + + dmabuf_cmsg->frag_offset, + dmabuf_cmsg->frag_size); + else + print_nonzero_bytes( + ((unsigned char *)buf_mem) + + dmabuf_cmsg->frag_offset, + dmabuf_cmsg->frag_size); + + sync.flags =3D DMA_BUF_SYNC_READ | DMA_BUF_SYNC_END; + ioctl(buf, DMA_BUF_IOCTL_SYNC, &sync); + + ret =3D setsockopt(client_fd, SOL_SOCKET, + SO_DEVMEM_DONTNEED, &token, + sizeof(token)); + if (ret !=3D 1) + error(1, 0, + "SO_DEVMEM_DONTNEED not enough tokens"); + } + if (!is_devmem) + error(1, 0, "flow steering error\n"); + + printf("total_received=3D%lu\n", total_received); + } + + fprintf(stdout, "%s: ok\n", TEST_PREFIX); + + fprintf(stdout, "page_aligned_frags=3D%lu, non_page_aligned_frags=3D%lu\n= ", + page_aligned_frags, non_page_aligned_frags); + + fprintf(stdout, "page_aligned_frags=3D%lu, non_page_aligned_frags=3D%lu\n= ", + page_aligned_frags, non_page_aligned_frags); + +cleanup: + + munmap(buf_mem, dmabuf_size); + close(client_fd); + close(socket_fd); + close(buf); + close(memfd); + close(devfd); + ynl_sock_destroy(ys); + + return 0; +} + +void run_devmem_tests(void) +{ + struct netdev_queue_id *queues; + int devfd, memfd, buf; + struct ynl_sock *ys; + size_t dmabuf_size; + size_t i =3D 0; + + dmabuf_size =3D getpagesize() * NUM_PAGES; + + create_udmabuf(&devfd, &memfd, &buf, dmabuf_size); + + /* Configure RSS to divert all traffic from our devmem queues */ + if (configure_rss()) + error(1, 0, "rss error\n"); + + queues =3D calloc(num_queues, sizeof(*queues)); + + if (configure_headersplit(1)) + error(1, 0, "Failed to configure header split\n"); + + if (!bind_rx_queue(ifindex, buf, queues, num_queues, &ys)) + error(1, 0, "Binding empty queues array should have failed\n"); + + for (i =3D 0; i < num_queues; i++) { + queues[i]._present.type =3D 1; + queues[i]._present.id =3D 1; + queues[i].type =3D NETDEV_QUEUE_TYPE_RX; + queues[i].id =3D start_queue + i; + } + + if (configure_headersplit(0)) + error(1, 0, "Failed to configure header split\n"); + + if (!bind_rx_queue(ifindex, buf, queues, num_queues, &ys)) + error(1, 0, "Configure dmabuf with header split off should have failed\n= "); + + if (configure_headersplit(1)) + error(1, 0, "Failed to configure header split\n"); + + for (i =3D 0; i < num_queues; i++) { + queues[i]._present.type =3D 1; + queues[i]._present.id =3D 1; + queues[i].type =3D NETDEV_QUEUE_TYPE_RX; + queues[i].id =3D start_queue + i; + } + + if (bind_rx_queue(ifindex, buf, queues, num_queues, &ys)) + error(1, 0, "Failed to bind\n"); + + /* Deactivating a bound queue should not be legal */ + if (!configure_channels(num_queues, num_queues - 1)) + error(1, 0, "Deactivating a bound queue should be illegal.\n"); + + /* Closing the netlink socket does an implicit unbind */ + ynl_sock_destroy(ys); +} + +int main(int argc, char *argv[]) +{ + int is_server =3D 0, opt; + + while ((opt =3D getopt(argc, argv, "ls:c:p:v:q:t:f:")) !=3D -1) { + switch (opt) { + case 'l': + is_server =3D 1; + break; + case 's': + server_ip =3D optarg; + break; + case 'c': + client_ip =3D optarg; + break; + case 'p': + port =3D optarg; + break; + case 'v': + do_validation =3D atoll(optarg); + break; + case 'q': + num_queues =3D atoi(optarg); + break; + case 't': + start_queue =3D atoi(optarg); + break; + case 'f': + ifname =3D optarg; + break; + case '?': + printf("unknown option: %c\n", optopt); + break; + } + } + + ifindex =3D if_nametoindex(ifname); + + for (; optind < argc; optind++) + printf("extra arguments: %s\n", argv[optind]); + + run_devmem_tests(); + + if (is_server) + return do_server(); + + return 0; +} --=20 2.46.0.598.g6f2099f65c-goog