From patchwork Wed Jan 24 18:54:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13529563 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 9F31C13175D; Wed, 24 Jan 2024 18:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122450; cv=none; b=tfizPKpcyrsTlC8eK24nK9kl9Dajph4tGyU8YkzHxh7zxl6jKfHdhsau5y9N9BmAGf7sprTeRK5/UDd/1gCt07uR5FKlyREJ8bl06r+FsPjLTa7N0RxhtNHbKU+WdmFQgdvg40Mr8i+TowWhuPmpzUX6IsejBLnIrcHVURkHvHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122450; c=relaxed/simple; bh=L3igyJHYbCDhLOOOZeQY+/7fItzANL3OcV+d19x9sKw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=K4yns4JFF//GIzmbC9qxdJMzmR8AYiG+JhuBgFkzBiVFToWjFAcD8BuVZXZg6BcjvUk6u2pulCKWCs49TNcoAs3F71KcOrOc4WZUGfYif6DoAh+/oTboMmjsw3E6QUr6lUIqXxx0tY+ur9NiK9YdjcmDaTpvR0g4Mwkhh+eKRBY= 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=KvHtgRlL; arc=none smtp.client-ip=209.85.210.180 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="KvHtgRlL" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6dc6f47302bso2615866b3a.1; Wed, 24 Jan 2024 10:54:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706122448; x=1706727248; 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=XIy/qDrfZpGQr03QOGQZTj3QWnPvTmsJgQQT7l8hmHg=; b=KvHtgRlLFqa9OK+8vYkoQTeEvNP5FIXL95UHIR8LX5NGlTykAlNTb4dqwaZ4j4RQdE bsR7AjEKLCUesiJfNiNXzPzYtrdl52hKetpUMBE3Ni7r3B9NevQMq2yiyBRUHgPg6jeN fhCeE7r8y2VnhIoEr41TS6Lg6AVYJyeeZX5dsVw0qewbA8LNGQ+urmgab8MDIrRwc4QS f79iqLOeniN3KyFBORvG8haDLjKGwqbUMu1Qckp99lQnRwbIs8nGO7z4yrb96z2NAjBm HEXHXv+JgDQ9rxfvQyg162XEMjM0ZoEO0KlqcaktXWaW9+Et6Xm138gNqvwwyZIK3tHG CIfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706122448; x=1706727248; 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=XIy/qDrfZpGQr03QOGQZTj3QWnPvTmsJgQQT7l8hmHg=; b=l4Qaa/BfUb46DFby/6LZm+FZfEFzTxDObMcJcImvrXpmeeH70VcCb5OstaS1cn5NQA ynSGCF6W2CFa9VagK32DFiKXgNO6pUSFUjO78syeiDME79EF8sCHe2Dh8xLQnrZWwCKI CnoMox3msmNNlxKQ3Q1/7QoMxYzXmPYkYl1c6ZBFS4RZbULDNeQWXlR0gVUfaURWvtm3 wqMR0oySUkr9xCs3tREjQAG4ZqItOJFVQz63D2Fvv6D1StfmIHKbVgsmeqviwFif4IH1 r7r7W9xY0LXjphBigisKev4SwTeY6Dq7ISjgralubF4P/exBdFIG8Ger1rW4W7A3EJoc 5L1A== X-Gm-Message-State: AOJu0YzaCQeDmj0aN+4WyQrBUEYKxOATI67TT/63JKK3ARyE0EA2YddQ WfwCznU+UoXgm6pd5SI0pU6qNPhe8/Lx8YeEvbJdnWS0RxInDNlk X-Google-Smtp-Source: AGHT+IFLAsqXlNEwdbanfo/V3kjDEX+RvB/Z1dIJwIrExAfZD60qZ7cZZOXk1B3SUWemq6fcH9f9Qg== X-Received: by 2002:a05:6a00:3a09:b0:6db:a0e5:7ec3 with SMTP id fj9-20020a056a003a0900b006dba0e57ec3mr10551981pfb.22.1706122447888; Wed, 24 Jan 2024 10:54:07 -0800 (PST) Received: from john.. ([98.97.116.78]) by smtp.gmail.com with ESMTPSA id ko18-20020a056a00461200b006dab0d72cd0sm14113696pfb.214.2024.01.24.10.54.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 10:54:06 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, bpf@vger.kernel.org Cc: netdev@vger.kernel.org, john.fastabend@gmail.com, andrii@kernel.org Subject: [PATCH bpf-next v2 1/4] bpf: sockmap, add test for sk_msg prog pop msg helper Date: Wed, 24 Jan 2024 10:54:00 -0800 Message-Id: <20240124185403.1104141-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240124185403.1104141-1-john.fastabend@gmail.com> References: <20240124185403.1104141-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..9ffe02f45808 --- /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 out; + + 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) + goto close_sockets; + + 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 out; + + 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) + goto close_sockets; + + 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 Wed Jan 24 18:54:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13529564 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 D74D2131E3B; Wed, 24 Jan 2024 18:54:10 +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=1706122452; cv=none; b=MZMAZLyLch0ivpw70UICp3dtKOUtUjHpxt0R3fH5Non65aoK+kdjQQ2BeAlBkBZHjaLJeEFqshxdfDjzyKpojoHPfB1LNnAPL7yOgbyKv6NZhoUzahTD5PL+ay414k09WsGyECRe5i3OIIwOi3puh+pSTcnocim+T1FCqu9U+Wo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122452; c=relaxed/simple; bh=Uu/SwqAnEEfj6bz3iaYVr9X49+WecBYMiwA+zR/KRas=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QOrnU8nQslIIWMyEnW166uXkS+yIpQF4devetzeK886Fbc+RwRIa/ETXhH0xCV4AZD0HHCnC3elYI1zgIfx2teakwf4m5kf5dsWquKzqum+7aHFTVfQ9sHfbtRybOXhup5pjOdhClWe411W9IEAgnyuhzUNcXyx6/WRkfJ/guLE= 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=e2HnZ3n1; 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="e2HnZ3n1" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5cfb81124ecso4241915a12.2; Wed, 24 Jan 2024 10:54:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706122450; x=1706727250; 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=P0NtAqbHyngCcUSOczUVS6y+pvGJR3Y9mdYKUB6jL1U=; b=e2HnZ3n1CxEGhjHs8LsuXMlHjqzeJ+j1ceaA6spzVEVPt2ElfMax+9NgmwfaXBfbH3 Df1ZZoRpabToEJXN04vW3cADZJojmOp+tuXTljc5VaUcA/pdnZBjfU3b/OeZZPzduysK Uw99cWga5/5jYMo7E1HoSRj8BQ2YoXUF/zdKikpDIX5X+qtVXXwWwvVUPVfId4uxk9gn 2AMyoEPQ402D1HSbGlbUxT9v0oyBDHnXwYernjnQubsyjkWvErxeEJBDcqCWv5Y1Ix1W EylCPRQrk5FXl+xdHQHGTngqpmo9V0H94FPUZB6ahKBXmqBQU0vLJ38GZUioVyrFi7JV ReDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706122450; x=1706727250; 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=P0NtAqbHyngCcUSOczUVS6y+pvGJR3Y9mdYKUB6jL1U=; b=PRDMioN2nDsLKbDzkC7bgg6+3iczhXylv7a6wp+Bqjaq1JXQn7olmn/33mO+OqlnKl CAc/dGHx3Q3LeyuTmsUOwD9tfES+COVFtXi61Hd9OuZs6wOiXcLia9mG70h+1oThVsx0 rXNQeFUvsVDp1mYbP7oHbEPOh+8aOFhGu2qxJZMeayfylky0DXqBh6n9janSr1jy2rpF 1yZrlpbGIMw0Vsc0kVCM+24rTIGZsMwYNnxxgcZGdqoca4dDwLQ3g/YBqQ5CEGKyOgYC P+wEsbJ77p1Bfngdh6fMdXiQhsF3EzIEfpw4/xMuTPs+cwYfwMGSx5XQnFjPfm7rJ2ny vWow== X-Gm-Message-State: AOJu0YzJ457y8uYi9YbXGN4vY5BEpJWw0I/DAu072FeaDbOSr2cs15Sp tXM5ENZ3SZb1gLyomYt3uDHaAsHHkXBWeHsYd55Lv5hERz+DpjSy X-Google-Smtp-Source: AGHT+IHjuj+RLVRB1tMQnZIX6HZYdS4KBcxKAkpgH24vbv399tyOzUNXgw8p7QTTPer4QIBh1aZNmw== X-Received: by 2002:a05:6a20:6043:b0:19a:39a1:f05b with SMTP id s3-20020a056a20604300b0019a39a1f05bmr1349720pza.37.1706122450105; Wed, 24 Jan 2024 10:54:10 -0800 (PST) Received: from john.. ([98.97.116.78]) by smtp.gmail.com with ESMTPSA id ko18-20020a056a00461200b006dab0d72cd0sm14113696pfb.214.2024.01.24.10.54.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 10:54:08 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, bpf@vger.kernel.org Cc: netdev@vger.kernel.org, john.fastabend@gmail.com, andrii@kernel.org Subject: [PATCH bpf-next v2 2/4] bpf: sockmap, add a sendmsg test so we can check that path Date: Wed, 24 Jan 2024 10:54:01 -0800 Message-Id: <20240124185403.1104141-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240124185403.1104141-1-john.fastabend@gmail.com> References: <20240124185403.1104141-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 9ffe02f45808..e5e618e84950 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 Wed Jan 24 18:54:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13529565 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 7BBE2131E4A; Wed, 24 Jan 2024 18:54:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122453; cv=none; b=GbfKuVn1qcEWDFP2+ba2IBj2mPmud45MZEHiuBx+AAhv8gnH5BT8olN22Y7x/Co3F699+/Uz7wg0yMWdFst+j1P53KE0tRcxwVb5p0HqkTRyKJgiQZuMPQ3qs+QXnCe9+kdCuSYKawMjlJrnKSqX/0F2dSDJzMoJYY3wqphpDSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122453; c=relaxed/simple; bh=Jq3Or8NJ0GJ9VdEOqtczXgyaZGRJ1hVkHCU1N/bpPNU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jNP2UVJLjZgHoHZejTXlhijwhYA+r0O+XmABn6DFvIkimkz5mit45vB37y+JXl1olnRUyLP2XNn71MjNzlts/jVDUO0w8w6pwiQ2fvAa+EnHKt8lLgsqoR/HcA3xtl5g8nxP/o6LVAI8qXGfJs9hsojev4M6CCxa2HwTL51yXGs= 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=S3I6p56J; arc=none smtp.client-ip=209.85.210.180 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="S3I6p56J" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6dd7a44d51bso1932272b3a.3; Wed, 24 Jan 2024 10:54:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706122451; x=1706727251; 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=FLfrQKp0BI/MS/C+9kJL3wZUi8/Fej4YlyCO2sBCq+o=; b=S3I6p56JassA9zl7AvhsNvllFNHF0jUco4x2nnOe8kbYvxAZf1zLmxO0HCf0fGyabP Tj71na7hhvjUCSYswL06gHYGoHC21EzXY2lcIliH1CAp8fnTUo0progZ7B5rEMgVpEnU 0JtY2t8Z3JYqaao2KR3eC5vYUanJWZXF9dDsOck7MSH7HGrSHeYsLWIYP6RuLBP4gVjI CtmaPR0uARvqRzhsZhuR8ur63oqoj7DYZWTwGhCj6sQkKhjZUdU8DoARs7wTIEGlVJ4Q PvWZmmloLE2cmAgjmYDC5uozTJUJVk6/bVZKQMVBtxOV/wQIQzrvn65jPcwVN5sYsG1L apRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706122451; x=1706727251; 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=FLfrQKp0BI/MS/C+9kJL3wZUi8/Fej4YlyCO2sBCq+o=; b=twkKHmN2/OrRGrPVwfxksS3m2jlnIPjo3Zbg5AvrEWE/G1CYdQDrVFPH0wMJns9hj1 h3qE1MgHD0HwA9oD4FU8U/0G/ewpZ5Ju/Qmd8T3GEj2nIUcw94amopwlCbAZnDxA7JCd USzouH1LR0ZVcYwlAuLjymrngqGj/asEaboZO89Cj8euc7gveKKJl2DgxVudbdsd7xHR M364afnWwS7X8A2DR8Z5ulSBCTc8/z72LBx3CqwNDbMwZv741VdsrkU9vY/RnJFWRA9d TWyWlo83/59cNw1xD/mXJBdWOXzlUbEqChTH0Raa41KY0qaeJQmeAmpJzfQ7Fjw1QjKA BcFw== X-Gm-Message-State: AOJu0YwZi89D+mQd+O7c1Lp365bMXEPP1IjsY6WX2oEOftCCJoBgztAG V/fgygVlztkpGh/i5LnVBd3h2EmlsyIMnvdH0OkgzAFcE6uQ7IDn X-Google-Smtp-Source: AGHT+IGTC7RqOQ1STyMt0AUcPhq8Q4hfexFYFkK6H5MvhUmuVspZe05sQOnQ2pWtiGZFuiT3lRtdmQ== X-Received: by 2002:a05:6a00:a26:b0:6d9:bf50:1c71 with SMTP id p38-20020a056a000a2600b006d9bf501c71mr6562643pfh.48.1706122451622; Wed, 24 Jan 2024 10:54:11 -0800 (PST) Received: from john.. ([98.97.116.78]) by smtp.gmail.com with ESMTPSA id ko18-20020a056a00461200b006dab0d72cd0sm14113696pfb.214.2024.01.24.10.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 10:54:10 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, bpf@vger.kernel.org Cc: netdev@vger.kernel.org, john.fastabend@gmail.com, andrii@kernel.org Subject: [PATCH bpf-next v2 3/4] bpf: sockmap, add a cork to force buffering of the scatterlist Date: Wed, 24 Jan 2024 10:54:02 -0800 Message-Id: <20240124185403.1104141-4-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240124185403.1104141-1-john.fastabend@gmail.com> References: <20240124185403.1104141-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 e5e618e84950..8ced54fe1a0b 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 out; + + 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) + goto close_sockets; + + 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 Wed Jan 24 18:54:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13529566 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (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 A8D4E132C06; Wed, 24 Jan 2024 18:54:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122456; cv=none; b=d5QX4Sb8KuLtBFfqZ1bjlfr/ZGQoYQfzoiVAsjEfgZjYIfbG2j19o+F11XGxgW/UJ63aFDTq0dwOx+K2HOwgd43kWvqnZ5lXMM/osJu6mAzuFHA2CjfHfhQe9tX218J0E2mkQP5WQFVtdkJfpYvK6DUKj9kM9sDJlB4uufayMbs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706122456; c=relaxed/simple; bh=/iI4CuBcskIT5fz8Z3/ta8Kdw8odzS46OI8RUnpyst4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bfoSADr6r/mCzLPbHOrsXh0eDTnG3LfmANrwVFNHOdz0Mo2o/js+Wgsuw47fRRl9YM5YpggLRQZLolhu1Z5FqIGD7/QrJlXTrDAiKmM0AVouVhqsgVpqV/7Vvqs0377Ugaly+0brXJ/gLM/u4lxkn3G3xYVDM0JFHUSDVWdE50E= 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=JzANqe5X; arc=none smtp.client-ip=209.85.210.169 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="JzANqe5X" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6dd7c194bb2so1577928b3a.0; Wed, 24 Jan 2024 10:54:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706122454; x=1706727254; 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=CkrNgdmMknR9/DA9BuNR/VhwDO3Guj9g1A1NTv+VIf0=; b=JzANqe5X5lTsxyXMKKOcJmP/b6J/qmouOjlSzhLVHXds/DWE6y0SDLe6Q23o+YC2XQ x4pdf9KGDIsvhhID9c5/lKKU88SX9CheiP4oiLXe4c/LGTak2LK0Yhwjq0aub5w4qL8S a4vaN2FUqvel0M9nU9g3KjDvYgC+7zDXCwyBW0YQ9IuBIY9reDNE0k+LZB2P/0YscYeT SaZkZDTmN31FDjTVfTbsCv+JyJ17cYJliizEJ0kjgCqRJ7mk/15UzVuoro81JlqeoqGX mr9mZ9JsHcinHQ3qtORL61Qtz/C25sxM9XNtWOOAsaNApo6cQpn1TiexwqnQxGTPDqKg IHhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706122454; x=1706727254; 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=CkrNgdmMknR9/DA9BuNR/VhwDO3Guj9g1A1NTv+VIf0=; b=Qjsnnb9Ae+d1m5iAsNYO7keiPSsfpH5L3LyXpmFBgG1eB0T3GEvQyqj4dpZsSNPyen IvCQCQwrEzgEgwZqid7i8yjoNM7FVk9UmBqgcslHzAmdpkuDr254NYTdSa72x+JEie/V pAYElDrJKWYerkl838aFo7fdwM+TPcQJFTofJ652LsE8n78XvxeUEbmyshBVPeF9l8Ry YNAPULLM038dcERl9uKKfOuc2WzEcg1MBrmNg2Rb+Fj5Pv1y3lTH61I/VVC913fem1lq 1tIEeCfMSiMJFsatfdi1XiYjz2Z1gCPbgFYPwme9qqoYEjAlCjAUcibF3WOcyESWsHs1 UQfA== X-Gm-Message-State: AOJu0Yw/DnNfoCmJcchNjGVB4XTYiBGdSkCx43vz3PKErLFhfeqLNlB9 yGc9BfX/Zp/Tmyvo+lh+5gRTTCdmtXcvtMbDysuqRX+oG8QAGwYmY3p0v7/t X-Google-Smtp-Source: AGHT+IHPfWW121AaVvKLP7rcHI+7Wlqt/jJ8THFP0dtYQ4QzzblEMRJpRciE6ykQJGNqgTeU4L1iQA== X-Received: by 2002:a62:d14a:0:b0:6da:bcea:4cd4 with SMTP id t10-20020a62d14a000000b006dabcea4cd4mr99088pfl.16.1706122453772; Wed, 24 Jan 2024 10:54:13 -0800 (PST) Received: from john.. ([98.97.116.78]) by smtp.gmail.com with ESMTPSA id ko18-20020a056a00461200b006dab0d72cd0sm14113696pfb.214.2024.01.24.10.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 10:54:12 -0800 (PST) From: John Fastabend To: jakub@cloudflare.com, bpf@vger.kernel.org Cc: netdev@vger.kernel.org, john.fastabend@gmail.com, andrii@kernel.org Subject: [PATCH bpf-next v2 4/4] bpf: sockmap test cork and pop combined Date: Wed, 24 Jan 2024 10:54:03 -0800 Message-Id: <20240124185403.1104141-5-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20240124185403.1104141-1-john.fastabend@gmail.com> References: <20240124185403.1104141-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 8ced54fe1a0b..cfb965f6832f 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; }