From patchwork Tue Jan 23 22:36:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13528231 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 3E4FF537E9; Tue, 23 Jan 2024 22:36:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049380; cv=none; b=sWF9wiFK5a3VhicNF/v1loe91MRTYy3DMVgb/aJiubpFjLj8NlNDAmYuPxukDvksrYLCOjnV4aOQwRspycNYp7G3Q/lSRa6Etj6JXwolV63wjJSb8LCYMXuyho5CL74aSegkK1h4w76JPI1+VQThbNX8x7Hax3So4KEWvnZENKU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049380; c=relaxed/simple; bh=QSdmgz24hsXghzwu1g2Hb9eTf72nhtTudAlKinSjZrQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tv8MRAdfMg01B7YxhpaXHK/bJ4I4bNj0J7gNFuKeCrl6uxodcS6l+NzYEUXuMgXlQVoxvltKjLN7UiC6Kblz669z7KoBFr64YkLJfA+cM3XPjwB7oIQVjMN0I8gB1ArTJ/i+ah/WObtEKaz/4beAx9oLpJS+z2qtiVX5xmmsRNc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=C3qtSRE8; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="C3qtSRE8" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d76943baafso14165205ad.2; Tue, 23 Jan 2024 14:36:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706049377; x=1706654177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gqDQo5sbKkbIdJoD5mE0BCooUO2QiAeTYNwUjxSUraw=; b=C3qtSRE8zCQRE44zfpU+XUa422PGMU13oJ0OOWU25CiotE8W/SX3irefPeDsJ1M3e9 hOpP0Cv5ZKrhgVVIxTEhIsBu4mHiX8kno1mAEEudUj4EcuSeFJ/q16T32aCjv+10g+cJ t0DpaI3FWhaAUrcJAWDP6FSEeTfHGgFU9FHsghsxDUpB2gUGXYU6pWbkv0QkmadPpbJc ek9Ypmk2o5aRJzjQ68YzT6l89UeZU1X9I8ewwQi+LLZ0zbwY8FVDDnoFevTOjy7HlkoR HdoLKpLMmKVtYWmhdpy5PCYusy8Zk0Tajpqx80cjZnHhmOO0V/uFMuWwsG0xi3hZE/z7 L9dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049377; x=1706654177; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gqDQo5sbKkbIdJoD5mE0BCooUO2QiAeTYNwUjxSUraw=; b=bdkMhflAQHPCqCCTxy3JnKV70xCsExw93X8UidEZR2DoWm+0sPfvkQG1Axj7bAG0/H RQYJqZrR8iyto1gCOuRZCtgo3l206jsc2Y24viItZbBGe62Oc/6vVOOgD7A5MXHFdZbo XzJEa21TKfcAKeMs2H+dwdlPXgYFwQlEbxvlTkrXvdaPxysSPHXeaGaq6tBrGGFRo60H aDzbOYnm/dM/zqbJAotLujb4A8+ww5+t9/lnaLzfqhKMask+rq2M5/emRF+LZa9EQntd E+m5/akc2uWBIXa4GiOuFUC4LpXPGI4zQmpKNw43OHmqIo5DGRitM+DRMpp+GWXYSHek wHZQ== X-Gm-Message-State: AOJu0Yz1HkG5Hun7ZV0EaWQDS4q7/WR+fRCJkLOSzW1IfHX/g6M9BDCc WYq0cbYoki/eeY3G/KmqUmHhKH8frN6zX4cnRMUhRjd4w3GDanU3+rrCJYv1 X-Google-Smtp-Source: AGHT+IEy/TuZ4wT4xCd3W+KWN4YBN96pqN2h8k7BskkiybeE/0CQvsx4oLPrhr2WVvaOYdv4OAWXIg== X-Received: by 2002:a17:902:f551:b0:1d7:4670:ffb0 with SMTP id h17-20020a170902f55100b001d74670ffb0mr4050741plf.9.1706049377262; Tue, 23 Jan 2024 14:36:17 -0800 (PST) Received: from john.. ([98.97.113.214]) by smtp.gmail.com with ESMTPSA id x9-20020a170902e04900b001d73f1fbdd9sm4875241plx.154.2024.01.23.14.36.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 14:36:15 -0800 (PST) From: John Fastabend To: netdev@vger.kernel.org, jakub@cloudflare.com Cc: john.fastabend@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next 1/4] bpf: sockmap, add test for sk_msg prog pop msg helper Date: Tue, 23 Jan 2024 14:36:09 -0800 Message-Id: <20240123223612.1015788-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240123223612.1015788-1-john.fastabend@gmail.com> References: <20240123223612.1015788-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net For msg_pop sk_msg helpers we only have older tests in test_sockmap, but these are showing their age. They don't use any of the newer style BPF and also require running test_sockmap. Lets use the prog_test framework and add a test for msg_pop. This is a much nicer test env using newer style BPF. We can extend this to support all the other helpers shortly. The bpf program is a template that lets us run through all the helpers so we can cover not just pop, but all the other helpers as well. Signed-off-by: John Fastabend --- .../bpf/prog_tests/sockmap_helpers.h | 10 + .../bpf/prog_tests/sockmap_msg_helpers.c | 210 ++++++++++++++++++ .../bpf/progs/test_sockmap_msg_helpers.c | 52 +++++ 3 files changed, 272 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c create mode 100644 tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h index e880f97bc44d..781cbdf01d7b 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h @@ -112,6 +112,16 @@ __ret; \ }) +#define xrecv_nonblock(fd, buf, len, flags) \ + ({ \ + ssize_t __ret = recv_timeout((fd), (buf), (len), (flags), \ + IO_TIMEOUT_SEC); \ + if (__ret == -1) \ + FAIL_ERRNO("recv"); \ + __ret; \ + }) + + #define xsocket(family, sotype, flags) \ ({ \ int __ret = socket(family, sotype, flags); \ diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c new file mode 100644 index 000000000000..0fe3172a6c43 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Cloudflare +#include +#include +#include + +#include "test_progs.h" +#include "test_sockmap_msg_helpers.skel.h" +#include "sockmap_helpers.h" + +#define TCP_REPAIR 19 /* TCP sock is under repair right now */ + +#define TCP_REPAIR_ON 1 +#define TCP_REPAIR_OFF_NO_WP -1 /* Turn off without window probes */ + +struct msg_test_opts { + struct test_sockmap_msg_helpers *skel; + int server; + int client; +}; + +#define POP_END -1 + +static void pop_simple_send(struct msg_test_opts *opts, int start, int len) +{ + struct test_sockmap_msg_helpers *skel = opts->skel; + char buf[] = "abcdefghijklmnopqrstuvwxyz"; + char recvbuf[sizeof(buf)]; + size_t sent, recv, cmp; + + skel->bss->pop = true; + + if (start == -1) + start = sizeof(buf) - len - 1; + + skel->bss->pop_start = start; + skel->bss->pop_len = len; + + sent = xsend(opts->client, buf, sizeof(buf), 0); + if (sent < sizeof(buf)) + FAIL("xsend failed"); + + ASSERT_OK(skel->bss->err, "pop error"); + + recv = xrecv_nonblock(opts->server, recvbuf, sizeof(buf), 0); + if (recv != sent - skel->bss->pop_len) + FAIL("Received incorrect number number of bytes after pop"); + + cmp = memcmp(&buf[0], &recvbuf[0], start); + ASSERT_OK(cmp, "pop cmp start bytes failed"); + cmp = memcmp(&buf[start+len], &recvbuf[start], sizeof(buf) - start - len); + ASSERT_OK(cmp, "pop cmp end bytes failed"); +} + +static void test_sockmap_pop(void) +{ + struct msg_test_opts opts; + struct test_sockmap_msg_helpers *skel; + int s, client, server; + int err, map, prog; + + skel = test_sockmap_msg_helpers__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map = bpf_map__fd(skel->maps.sock_map); + prog = bpf_program__fd(skel->progs.msg_helpers); + err = bpf_prog_attach(prog, map, BPF_SK_MSG_VERDICT, 0); + if (!ASSERT_OK(err, "bpf_prog_attach")) + goto out; + + s = socket_loopback(AF_INET, SOCK_STREAM); + if (s < 0) + goto close_sockets; + + err = create_pair(s, AF_INET, SOCK_STREAM, &client, &server); + if (err < 0) + goto close_loopback; + + err = add_to_sockmap(map, client, server); + if (err < 0) + FAIL("add to sockmap"); + + opts.client = client; + opts.server = server; + opts.skel = skel; + + /* Pop from start */ + pop_simple_send(&opts, 0, 5); + /* Pop from the middle */ + pop_simple_send(&opts, 10, 5); + /* Pop from end */ + pop_simple_send(&opts, POP_END, 5); + +close_sockets: + close(client); + close(server); +close_loopback: + close(s); +out: + test_sockmap_msg_helpers__destroy(skel); +} + +static void test_sockmap_pop_errors(void) +{ + char buf[] = "abcdefghijklmnopqrstuvwxyz"; + struct test_sockmap_msg_helpers *skel; + int i, recv, err, map, prog; + char recvbuf[sizeof(buf)]; + int s, client, server; + + skel = test_sockmap_msg_helpers__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map = bpf_map__fd(skel->maps.sock_map); + prog = bpf_program__fd(skel->progs.msg_helpers); + err = bpf_prog_attach(prog, map, BPF_SK_MSG_VERDICT, 0); + if (!ASSERT_OK(err, "bpf_prog_attach")) + goto out; + + s = socket_loopback(AF_INET, SOCK_STREAM); + if (s < 0) + goto close_sockets; + + err = create_pair(s, AF_INET, SOCK_STREAM, &client, &server); + if (err < 0) + goto close_loopback; + + err = add_to_sockmap(map, client, server); + if (err < 0) + FAIL("add to sockmap"); + + skel->bss->pop = true; + + /* Pop larger than buffer */ + skel->bss->pop_start = 0; + skel->bss->pop_len = sizeof(buf) + 1; + xsend(client, buf, sizeof(buf), 0); + ASSERT_ERR(skel->bss->err, "popping more bytes than msg did not throw an error"); + xrecv_nonblock(server, recvbuf, sizeof(recvbuf), 0); + + /* Pop past end of buffer */ + skel->bss->pop_start = sizeof(buf) - 5; + skel->bss->pop_len = 10; + xsend(client, buf, sizeof(buf), 0); + ASSERT_ERR(skel->bss->err, "popping past end of msg did not throw an error"); + xrecv_nonblock(server, recvbuf, sizeof(recvbuf), 0); + + /* Pop larger than buffer on complex send */ + skel->bss->pop_start = 0; + skel->bss->pop_len = 0; + for (i = 0; i < 14; i++) + xsend(client, buf, sizeof(buf), MSG_MORE); + skel->bss->pop_start = 0; + skel->bss->pop_len = sizeof(buf) * 32; + xsend(client, buf, sizeof(buf), MSG_MORE); + ASSERT_ERR(skel->bss->err, "popping more bytes than sg msg did not throw an error"); + i = 0; + do { + i++; + recv = xrecv_nonblock(server, recvbuf, sizeof(recvbuf), 0); + } while (recv > 0 && i < 15); + + /* Pop past end of complex send */ + skel->bss->pop_start = 0; + skel->bss->pop_len = 0; + for (i = 0; i < 14; i++) + xsend(client, buf, sizeof(buf), MSG_MORE); + skel->bss->pop_start = sizeof(buf) * 14; + skel->bss->pop_len = sizeof(buf) + 1; + xsend(client, buf, sizeof(buf), MSG_MORE); + ASSERT_ERR(skel->bss->err, "popping past end of sg msg did not throw an error"); + i = 0; + do { + i++; + recv = xrecv_nonblock(server, recvbuf, sizeof(recvbuf), 0); + } while (recv > 0 && i < 15); + + /* Pop past end of complex send starting in middle of last sg */ + skel->bss->pop_start = 0; + skel->bss->pop_len = 0; + for (i = 0; i < 14; i++) + xsend(client, buf, sizeof(buf), MSG_MORE); + skel->bss->pop_start = (sizeof(buf) * 14) + sizeof(buf) - 5; + skel->bss->pop_len = 10; + xsend(client, buf, sizeof(buf), MSG_MORE); + ASSERT_ERR(skel->bss->err, "popping past end from offset of sg msg did not throw an error"); + i = 0; + do { + i++; + recv = xrecv_nonblock(server, recvbuf, sizeof(recvbuf), 0); + } while (recv > 0 && i < 15); + +close_sockets: + close(client); + close(server); +close_loopback: + close(s); +out: + test_sockmap_msg_helpers__destroy(skel); +} + +void test_sockmap_msg_helpers(void) +{ + if (test__start_subtest("sockmap pop")) + test_sockmap_pop(); + if (test__start_subtest("sockmap pop errors")) + test_sockmap_pop_errors(); +} diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c new file mode 100644 index 000000000000..c721a00b6001 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2020 Cloudflare + +#include +#include +#include + +#include + +struct { + __uint(type, BPF_MAP_TYPE_SOCKMAP); + __uint(max_entries, 2); + __type(key, __u32); + __type(value, __u64); +} sock_map SEC(".maps"); + +int cork = 0; + +bool pull = false; +bool push = false; +bool pop = false; + +int pull_start = 0; +int pull_end = 0; + +int push_start = 0; +int push_end = 0; + +int pop_start = 0; +int pop_len = 0; + +int err; + +SEC("sk_msg") +int msg_helpers(struct sk_msg_md *msg) +{ + if (cork) + err = bpf_msg_cork_bytes(msg, cork); + + if (pull) + err = bpf_msg_pull_data(msg, pull_start, pull_end, 0); + + if (push) + err = bpf_msg_push_data(msg, push_start, push_end, 0); + + if (pop) + err = bpf_msg_pop_data(msg, pop_start, pop_len, 0); + + return SK_PASS; +} + +char _license[] SEC("license") = "GPL"; From patchwork Tue Jan 23 22:36:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13528232 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 6277753E0C; Tue, 23 Jan 2024 22:36:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049382; cv=none; b=a8nszyH/HRVyFWnwGzMngYUzRZ3ja5PxV68CNSkmklZHXegsbiVReEY6vLBfpgneLux7LpMOrvVg/WwCH4enaC3AW7Ih5RLyXEjl6TV1tS4N6dEwbaUd7E5TwcIpxgwvo+/pyysOc5vaSM3ye0NtFeWvv2C0ZB7F2E0WwAhzRK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049382; c=relaxed/simple; bh=Zxjr/wFZHNPfZI7hjGnd8zzvcRjQMMPjId/saWss+eI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GmoMX9xgC65JZ4AtojRDf87HCVlSAAg/YEzsOE/8HSdyudI6EHcDd1XoNeWeor628jufHt+vFbE6u1wDAifu3acKhT7C2vGFDPFbyjfZS+GztcWD7Rqc+f30Wy1Ztlgdk7pRSVBcj8UgJppKYIHXBsAC3q3jk7thty85x9db1hQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DGKg/qkA; arc=none smtp.client-ip=209.85.215.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DGKg/qkA" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5ce2aada130so2641392a12.1; Tue, 23 Jan 2024 14:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706049379; x=1706654179; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9eSy+rPOCovSpewSX72LD7wMXTxiQZ2pIn9/KLzvYuE=; b=DGKg/qkA8t8zRgUKai4ms1YeCCL34yiYXWhuumpM7O3w+bFro1mDKOAKWNjA/bGS4N 7vnGtUu7kvQr0RaW63zQLhT/lhLbbaU94m5d/w1RslpELcbnhhzvFZc5ffiFigzjp/XF 8m62kIuurVLaNZ5CPAn9Kx9oJvJzNwURYh4p9OH9EQYrOxfmznyUj2IMztyv5ZTfexDI 4kS/wYXskoY7BRtP1c+JcsdI1W4nCzc0+vdcVBiro48s1+dPcoeKVgRjP2AvZ2qLAvdN KAn1t3NI+o3M43RXRyCvh0BvzD9wnZRRO/75AeUshk90wHElO+++lhg15XTt0k8Mi1U2 qV7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049379; x=1706654179; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9eSy+rPOCovSpewSX72LD7wMXTxiQZ2pIn9/KLzvYuE=; b=IQ4rqjAIAn0WDAT5BxjAwCxdT0OjEOn15IwYG0sHc/ZGH3z53ss+OLEA0jKS0zg2Od 6q+bFBgw6pYwZyAlo7qcYdrEA5KLK/XKSJgwcLrnGAT35zM7EHvCCygINSFaw6nbe6Zb 9estRGWwN1ARwgi1O4gARhBX0BO1oVdDf3KkdyNDwnkaKZjdw51hVAEddSXi2uaM2Put kXc2XKKpW3XGqZUTE1sLXYh44LNan73vDYpCJJfWWZgzB/u1JpDQF1E3D/qulRIioOSo Jpz5kag05lQxJ2LyX9obTx8LSqhO6am4fj0ylmeDgL/GHoE1ExRSRQNpeAtTO5eDjY1+ CbsA== X-Gm-Message-State: AOJu0YwkgV6S1LRMr5llefvPhzxINPZ557FkZUJZx8hoE2WFoPN8GcnK WIYTjgJS0HFfKM2v745By3BcLb9ruiFMlSIO8Z4fD4ld+8q+QJIgvotTj0ww X-Google-Smtp-Source: AGHT+IGdImkWoLsGuZ/kQxMZ3kMYwtNqMgfIxwGrxn5Qi95t6kZkQAREB2iGciIzlnQfJinhIM0TDQ== X-Received: by 2002:a05:6a20:7352:b0:19b:a07a:344d with SMTP id v18-20020a056a20735200b0019ba07a344dmr4390121pzc.7.1706049379577; Tue, 23 Jan 2024 14:36:19 -0800 (PST) Received: from john.. ([98.97.113.214]) by smtp.gmail.com with ESMTPSA id x9-20020a170902e04900b001d73f1fbdd9sm4875241plx.154.2024.01.23.14.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 14:36:18 -0800 (PST) From: John Fastabend To: netdev@vger.kernel.org, jakub@cloudflare.com Cc: john.fastabend@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next 2/4] bpf: sockmap, add a sendmsg test so we can check that path Date: Tue, 23 Jan 2024 14:36:10 -0800 Message-Id: <20240123223612.1015788-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240123223612.1015788-1-john.fastabend@gmail.com> References: <20240123223612.1015788-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Sendmsg path with multiple buffers is slightly different from a single send in how we have to handle and walk the sg when doing pops. Lets ensure this walk is correct. Signed-off-by: John Fastabend --- .../bpf/prog_tests/sockmap_helpers.h | 8 +++ .../bpf/prog_tests/sockmap_msg_helpers.c | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h index 781cbdf01d7b..4d8d24482032 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h @@ -103,6 +103,14 @@ __ret; \ }) +#define xsendmsg(fd, msg, flags) \ + ({ \ + ssize_t __ret = sendmsg((fd), (msg), (flags)); \ + if (__ret == -1) \ + FAIL_ERRNO("sendmsg"); \ + __ret; \ + }) + #define xrecv_nonblock(fd, buf, len, flags) \ ({ \ ssize_t __ret = recv_timeout((fd), (buf), (len), (flags), \ diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c index 0fe3172a6c43..7eeba3a35242 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c @@ -52,6 +52,50 @@ static void pop_simple_send(struct msg_test_opts *opts, int start, int len) ASSERT_OK(cmp, "pop cmp end bytes failed"); } +static void pop_complex_send(struct msg_test_opts *opts, int start, int len) +{ + struct test_sockmap_msg_helpers *skel = opts->skel; + char buf[] = "abcdefghijklmnopqrstuvwxyz"; + size_t sent, recv, total = 0; + struct msghdr msg = {0}; + struct iovec iov[15]; + char *recvbuf; + int i; + + for (i = 0; i < 15; i++) { + iov[i].iov_base = buf; + iov[i].iov_len = sizeof(buf); + total += sizeof(buf); + } + + recvbuf = malloc(total); + if (!recvbuf) + FAIL("pop complex send malloc failure\n"); + + msg.msg_iov = iov; + msg.msg_iovlen = 15; + + skel->bss->pop = true; + + if (start == -1) + start = sizeof(buf) - len - 1; + + skel->bss->pop_start = start; + skel->bss->pop_len = len; + + sent = xsendmsg(opts->client, &msg, 0); + if (sent != total) + FAIL("xsend failed"); + + ASSERT_OK(skel->bss->err, "pop error"); + + recv = xrecv_nonblock(opts->server, recvbuf, total, 0); + if (recv != sent - skel->bss->pop_len) + FAIL("Received incorrect number number of bytes after pop"); + + free(recvbuf); +} + static void test_sockmap_pop(void) { struct msg_test_opts opts; @@ -92,6 +136,15 @@ static void test_sockmap_pop(void) /* Pop from end */ pop_simple_send(&opts, POP_END, 5); + /* Empty pop from start of sendmsg */ + pop_complex_send(&opts, 0, 0); + /* Pop from start of sendmsg */ + pop_complex_send(&opts, 0, 10); + /* Pop from middle of sendmsg */ + pop_complex_send(&opts, 100, 10); + /* Pop from end of sendmsg */ + pop_complex_send(&opts, 394, 10); + close_sockets: close(client); close(server); From patchwork Tue Jan 23 22:36:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13528233 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 802A053E2A; Tue, 23 Jan 2024 22:36:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049384; cv=none; b=KGS98o/PZ9XDp4iTZEoqqKPtbGGCe8o+nBqTIHhYyiuWUJsEFlkrIOzspaWM2EAxCQDQMvLZwMpMxCePlsZE86Dzs4sFoqIB2NeWLWUVHL7M7NUnn5M521xDqtlFj5mymhpHEnwp+CQJTlgC8bi1vNnopWnUBOZmYO3CQeJ4VUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049384; c=relaxed/simple; bh=FvTPq8r3psPA/b4XyP6dgSK1rKEQuJBwTYolAtZNHYo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S+CQnyoMk2ig7qRdhjQFC1MP1ZEnSV50UOGjPFOEs5xiQOyUJvtH7ejYREkLtUc99DO7VJlaMFfrO0SItGOVGWR0byvrrwRagN86h+j1NjreiKpXa1lvQrAvfMfE+gOVpSv0r6eaIAeilq0ke57dQSL1IC0STQU4Gq6hJ5kLoAA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HQqQBXpZ; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HQqQBXpZ" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d75c97ea6aso17189315ad.1; Tue, 23 Jan 2024 14:36:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706049382; x=1706654182; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gXT9I2pywE63wO1/MqrFBdHXmKrk7hgom4ddV7IFy0o=; b=HQqQBXpZ6yWstOXTobkTiwgZKMzK9kqsp8GafLhi2nGbVQcUVANU/nMjKBTUAEsA+b nscZ8Egxxb4Jt6HuAehb1Nd7VFOBfShIWgyBm7+AaVrvxd/vm0/JX3lGcDQkNoCIZmNI ET2z7WI4jadSafycXonfMP5rZZqwI5SYESPqWPIdhN3s1d8Xr0Ro9ovVbmaw5MmIG3HB U4zuIuo8tE8aoHb7rdzVDF1HUzUIPjZPJtdgvv+LEQCyv0QgWGef0wvJfZgx0EsEMfaz BiYZoe6ydKEdKA24VqUqkGCk0c9gl+fDdYk/AwQnUPzSTJ/03SC4EgGPUVyn8uUhh6p4 DacQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049382; x=1706654182; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gXT9I2pywE63wO1/MqrFBdHXmKrk7hgom4ddV7IFy0o=; b=BBc14j0yPy5RwPrHRBDMT/lzfqWjCPFSvqs9lockET5196908VGvbgBcq+HInWRIe9 2uFQFpTgVnP65ENR4JC4Auzuycl3HJX4NzGnRp/525Cfb3+B8eLIk95v8ixHfa88+x0V 2UrT86TpXBuG1IXwiTbRLHM/Ttu6cnuML25JCnrbEhFzEbqubLedqaf81t9DinCQCkbw 6goA8cdfHWO62wKFHziEFBGi8+rkM3/4LbsI17a6j3B5evHZn8ntWCHxnK/+Ky1fkH7B dBFSt2/bncVGiRYiEA1YzySNlwEUD/D4EYvwDDpzIVNL92XwRFUPHP5BD7/fCYT+xGzx D+Sw== X-Gm-Message-State: AOJu0YxtTj5G1/3igqY/NKUNr4DpzM6TxQudPVMCAptdbKYhqssWfZlw CvsgA5fe5Gc+YQDerSNyHLcB6obmMbwl5fjqN9QtT6FxGMfUdT1yVEO73JiW X-Google-Smtp-Source: AGHT+IHku+fSLL8hrCGp40sjLm/uwwPKwEk8IL+NuCflLaf7MPVPzkoQWcQU9fNcBiXB6iETUvPe7Q== X-Received: by 2002:a17:902:64d0:b0:1d5:ecfe:4d69 with SMTP id y16-20020a17090264d000b001d5ecfe4d69mr292993pli.49.1706049381747; Tue, 23 Jan 2024 14:36:21 -0800 (PST) Received: from john.. ([98.97.113.214]) by smtp.gmail.com with ESMTPSA id x9-20020a170902e04900b001d73f1fbdd9sm4875241plx.154.2024.01.23.14.36.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 14:36:20 -0800 (PST) From: John Fastabend To: netdev@vger.kernel.org, jakub@cloudflare.com Cc: john.fastabend@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next 3/4] bpf: sockmap, add a cork to force buffering of the scatterlist Date: Tue, 23 Jan 2024 14:36:11 -0800 Message-Id: <20240123223612.1015788-4-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240123223612.1015788-1-john.fastabend@gmail.com> References: <20240123223612.1015788-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net By using cork we can force multiple sends into a single scatterlist and test that first the cork gives us the correct number of bytes, but then also test the pop over the corked data. Signed-off-by: John Fastabend --- .../bpf/prog_tests/sockmap_msg_helpers.c | 81 +++++++++++++++++++ .../bpf/progs/test_sockmap_msg_helpers.c | 3 + 2 files changed, 84 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c index 7eeba3a35242..a05000b07891 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c @@ -21,6 +21,85 @@ struct msg_test_opts { #define POP_END -1 +static void cork_send(struct msg_test_opts *opts, int cork) +{ + struct test_sockmap_msg_helpers *skel = opts->skel; + char buf[] = "abcdefghijklmnopqrstuvwxyz"; + size_t sent, total = 0, recv; + char *recvbuf; + int i; + + skel->bss->pop = false; + skel->bss->cork = cork; + + /* Send N bytes in 27B chunks */ + for (i = 0; i < cork / sizeof(buf); i++) { + sent = xsend(opts->client, buf, sizeof(buf), 0); + if (sent < sizeof(buf)) + FAIL("xsend failed"); + total += sent; + } + + recvbuf = malloc(total); + if (!recvbuf) + FAIL("cork send malloc failure\n"); + + ASSERT_OK(skel->bss->err, "cork error"); + ASSERT_EQ(skel->bss->size, cork, "cork did not receive all bytes"); + + recv = xrecv_nonblock(opts->server, recvbuf, total, 0); + if (recv != total) + FAIL("Received incorrect number of bytes"); + + free(recvbuf); +} + +static void test_sockmap_cork() +{ + struct test_sockmap_msg_helpers *skel; + struct msg_test_opts opts; + int s, client, server; + int err, map, prog; + + skel = test_sockmap_msg_helpers__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + map = bpf_map__fd(skel->maps.sock_map); + prog = bpf_program__fd(skel->progs.msg_helpers); + err = bpf_prog_attach(prog, map, BPF_SK_MSG_VERDICT, 0); + if (!ASSERT_OK(err, "bpf_prog_attach")) + goto out; + + s = socket_loopback(AF_INET, SOCK_STREAM); + if (s < 0) + goto close_sockets; + + err = create_pair(s, AF_INET, SOCK_STREAM, &client, &server); + if (err < 0) + goto close_loopback; + + err = add_to_sockmap(map, client, server); + if (err < 0) + FAIL("add to sockmap"); + + opts.client = client; + opts.server = server; + opts.skel = skel; + + /* Small cork */ + cork_send(&opts, 54); + /* Full cork */ + cork_send(&opts, 270); +close_sockets: + close(client); + close(server); +close_loopback: + close(s); +out: + test_sockmap_msg_helpers__destroy(skel); +} + static void pop_simple_send(struct msg_test_opts *opts, int start, int len) { struct test_sockmap_msg_helpers *skel = opts->skel; @@ -260,4 +339,6 @@ void test_sockmap_msg_helpers(void) test_sockmap_pop(); if (test__start_subtest("sockmap pop errors")) test_sockmap_pop_errors(); + if (test__start_subtest("sockmap cork")) + test_sockmap_cork(); } diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c index c721a00b6001..9622f154d016 100644 --- a/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c +++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c @@ -30,10 +30,13 @@ int pop_start = 0; int pop_len = 0; int err; +int size; SEC("sk_msg") int msg_helpers(struct sk_msg_md *msg) { + size = msg->size; + if (cork) err = bpf_msg_cork_bytes(msg, cork); From patchwork Tue Jan 23 22:36:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13528234 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 9245A54673; Tue, 23 Jan 2024 22:36:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049387; cv=none; b=RuA++h0MF/ogokt1BtWhbXaO2IFipG6hwz4g/Ctoqkk0RlyGJ8ytDBv/OmNfEg/7/Hk5PHTQX5YSCDFqW1Xjk+aQHghLHLVBvzR8BTmvbQnXSBIJxtb7bXlDhQh0XSHekqwqNCiLfZQdv5H4//sgr0ocKQVbg4JME3DmiGNZcp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706049387; c=relaxed/simple; bh=On0QlHaF09Uyj7/AoUsVsIOpy1PzAJx4etnXBVkU+oo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f0zSt5Vx/xoMoiEbJ8gvzGQ8FdrBU0mr5D4IPd3k0mJ92LWvcjZhsfLTGDIPvHHF6oc5NpH8IBfFuYXObRL8XjBqj1+zlMBhI9lxDZ5HMeMFJjynmQpEUvDU1/4AR/U6aVn90i1xVobj9h1BgL8s5UF/0hMxCZWifDeNC3EqnU0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mARSm+/X; arc=none smtp.client-ip=209.85.214.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mARSm+/X" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1d6ff29293dso32426785ad.0; Tue, 23 Jan 2024 14:36:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706049384; x=1706654184; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3/Yjct9w9OwNW7EA63W+TU6+iZOg/M5pr/hq6Il+TEo=; b=mARSm+/X7uZ2sdKdj2UajPqrY8i2pfmn2EOq/RZAOEESsPiprUkWe3kJuZIctBvGLy 0wCMVmINkvkO/HQ9Tub0sG1dkahabneVfKGzHP4ACLrGAhPGM0HiSvMZ4XUTLdRMvNCl XNrpYF1kFn6F4IR/B9ruWYAeXZKAGjEVegYYjECcbwUAV6aDbfecro0vf7PvJ65Q39iO 3hs6KUT9KlFV6tJchOtBBF8NtzjSmzNTHCOMK7PLRg/5TCZ3AB5ZsOllK1UflMGkO2eY ySs99/GjqAoxz+sCREyP11wbl9ed21Mi8NRxNgtGXs3JMxkxiqXyOxCeI/TrpFfepNGW kQFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706049384; x=1706654184; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3/Yjct9w9OwNW7EA63W+TU6+iZOg/M5pr/hq6Il+TEo=; b=Q01zZ55CtDfabfTxXp8dxcptIwhwpGIH4iESNGC68giTec7SBGIqoIo3J397W1HznZ Vw0AlSd4UJFWdM2QlKqhDOb3Jw5G25TVHnF0K1vIn7Ev+2+Sa0sEzZIjxwEZQl2gOHeJ 3jTUVjPPtcLMtv7ObYBEt+lEov3Own63LeRp2INGRD4mzNrWw6bJBwTjWiUtPV4o9kxN tCmm9Ucg4wBclxjxTyAzI4mBp3AETf0x6KAHEQcX9d4iA189CwK23SvqMFmUYV+gQbcK WacupESWNjEblB9sibrWGULfvONKkgpA24Ep+CLWHJyd/u8FDzN3YGrXjavzk9yOiUtm +DtQ== X-Gm-Message-State: AOJu0Ywqj/zrODSf5MMQz8ncod/QdUwIi0Dy82TZc2Yu5nGUeSCP/GRO ARwa8wkF9P1VrgczMVh4VKMa5pnQaExod+9Fhkbz8QWZXB++Kr9xCun++dut X-Google-Smtp-Source: AGHT+IFM/1ZZ032mmP+pOj9N4TP8cIEGf04lpwTBJwL0JUQZ9XlYtgTCPSkhRRy6cXANuW7R+ORXFg== X-Received: by 2002:a17:902:b58c:b0:1d7:a2:3332 with SMTP id a12-20020a170902b58c00b001d700a23332mr3600780pls.114.1706049383849; Tue, 23 Jan 2024 14:36:23 -0800 (PST) Received: from john.. ([98.97.113.214]) by smtp.gmail.com with ESMTPSA id x9-20020a170902e04900b001d73f1fbdd9sm4875241plx.154.2024.01.23.14.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 14:36:22 -0800 (PST) From: John Fastabend To: netdev@vger.kernel.org, jakub@cloudflare.com Cc: john.fastabend@gmail.com, bpf@vger.kernel.org Subject: [PATCH bpf-next 4/4] bpf: sockmap test cork and pop combined Date: Tue, 23 Jan 2024 14:36:12 -0800 Message-Id: <20240123223612.1015788-5-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240123223612.1015788-1-john.fastabend@gmail.com> References: <20240123223612.1015788-1-john.fastabend@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Its possible to cork data for some N bytes and then pop a some bytes off that scatterlist. Test combining cork and pop here. Signed-off-by: John Fastabend --- .../bpf/prog_tests/sockmap_msg_helpers.c | 19 ++++++++++++++----- .../bpf/progs/test_sockmap_msg_helpers.c | 14 +++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c index a05000b07891..cf38d6bb3f94 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_msg_helpers.c @@ -21,7 +21,7 @@ struct msg_test_opts { #define POP_END -1 -static void cork_send(struct msg_test_opts *opts, int cork) +static void cork_send(struct msg_test_opts *opts, int cork, int start, int len) { struct test_sockmap_msg_helpers *skel = opts->skel; char buf[] = "abcdefghijklmnopqrstuvwxyz"; @@ -29,9 +29,12 @@ static void cork_send(struct msg_test_opts *opts, int cork) char *recvbuf; int i; - skel->bss->pop = false; + skel->bss->pop = !!len; skel->bss->cork = cork; + skel->bss->pop_start = start; + skel->bss->pop_len = len; + /* Send N bytes in 27B chunks */ for (i = 0; i < cork / sizeof(buf); i++) { sent = xsend(opts->client, buf, sizeof(buf), 0); @@ -48,7 +51,7 @@ static void cork_send(struct msg_test_opts *opts, int cork) ASSERT_EQ(skel->bss->size, cork, "cork did not receive all bytes"); recv = xrecv_nonblock(opts->server, recvbuf, total, 0); - if (recv != total) + if (recv != total - len) FAIL("Received incorrect number of bytes"); free(recvbuf); @@ -88,9 +91,15 @@ static void test_sockmap_cork() opts.skel = skel; /* Small cork */ - cork_send(&opts, 54); + cork_send(&opts, 54, 0, 0); /* Full cork */ - cork_send(&opts, 270); + cork_send(&opts, 270, 0, 0); + + /* Combine cork and pop small */ + cork_send(&opts, 54, 0, 10); + /* Full cork and pop */ + cork_send(&opts, 270, 200, 50); + close_sockets: close(client); close(server); diff --git a/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c index 9622f154d016..4c7e70367e35 100644 --- a/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c +++ b/tools/testing/selftests/bpf/progs/test_sockmap_msg_helpers.c @@ -37,8 +37,19 @@ int msg_helpers(struct sk_msg_md *msg) { size = msg->size; - if (cork) + /* If message is not yet fully cork'ed skip push, pull, pop */ + if (cork && cork > msg->size) { err = bpf_msg_cork_bytes(msg, cork); + goto out; + } else if (cork) { + /* If we previously corked the msg we need to clear the cork + * otherwise next pop would cause datapath to wait for the + * popped bytes to actually do the send. + */ + err = bpf_msg_cork_bytes(msg, 0); + if (err) + goto out; + } if (pull) err = bpf_msg_pull_data(msg, pull_start, pull_end, 0); @@ -49,6 +60,7 @@ int msg_helpers(struct sk_msg_md *msg) if (pop) err = bpf_msg_pop_data(msg, pop_start, pop_len, 0); +out: return SK_PASS; }