From patchwork Fri Mar 29 19:18:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610971 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 4348413280B for ; Fri, 29 Mar 2024 19:19:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711739999; cv=none; b=utJs5fXGolPkzwbGaydsnRw03HfAT4nlQWESI0HGYFgzpDxV92awmn9rDvsj1tq5DR2iOp6b97Bi7/+XK4ykTLXmp1PuL+xMf110EcMnbjN8uOmUeCaWkIgn83KxxiGXY2nJWyTXvq8PISfAln2LEb75ynABFcxLZwlxFzliTWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711739999; c=relaxed/simple; bh=ruM1fSImM8HKEje7JvZtgcNqeHH5/+wlG2CCls1QZT0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mC6hoLbiLUIuk0N7HGtzbgc7SptzBOxWG74ssK0wLbTRkJgiUQEqE0/6lBOFC47wrKJ+SLd9M/GxI8FqZk30WH0iz/gpXoBzrn7Rtu97LjDnkiCE8u281acUo3y4McEY3Cs7N/swetKSAMnjJsI8A2GegjjlUmaEJRXdeSLVq30= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TrgfKHO0; arc=none smtp.client-ip=209.85.219.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--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TrgfKHO0" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dd933a044baso4041763276.0 for ; Fri, 29 Mar 2024 12:19:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711739996; x=1712344796; 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=we19mLACjXRZNttAFYVbtTYtEILOCnooUoGzKYB5j+E=; b=TrgfKHO0oWcGQKZ2HeG6R0BL9PMw0W65Wx6mPgqm8StiLuchuP5tCX7zTFLdN6kO1U gZ/F6KZJ/Qq94UUJ2e8nhXNuqBCLRlh6embBKv5W24dUkHqEHehrjiSbIlLmFANaTW5p j5ekeVyQcAq8ktPXHrwmvzqr66N6mZXfGoVBSDNXwnGjiewrV2FetywDspzk7KEMaKNU 0MqZAWO6QeBiUB/VoRDY1FKzEYCayQQ8J/14PqxokBRbnwITzVn9YwByOuBwTl53BuXg RG/Ta36zYLJ4AO/RLzAo37Ck7O2QFDIbsGRHEag0NrhFhVjH35s3IKpJ8DO4qcuiqEAp 1jhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711739996; x=1712344796; 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=we19mLACjXRZNttAFYVbtTYtEILOCnooUoGzKYB5j+E=; b=cVpZcNqzci8gw87DUExakwqkMUxnDAmXk6dNIMbC95sqs3bqjVqzzOwBuw5aZROjO0 o+WPmO8lZo2glUm4PZya34mL9YT8LCUUrsz/myFmFoB+ENESXomLRM87acFW+2JzI0Rp q6y4Tk/RJOUtMfw5C/c3N4bsmpmUkJSGYYZ3XdkVrbG7UXSBAZd11U70Xu5oVDFnblMq OFOkK+S7DN+R28LX8H8PzthAbCuqDt2G0Qg+exOMokmERnauTYWgGJBHRBYn+aqZK58F 0UofhJQe2GVmaYpPS90PTjvdAyE5sVmUsmU1dSeN1ajskODxZHR55ig//lVQxUGF4v2f 3WMQ== X-Gm-Message-State: AOJu0YxVdMzcV+I/7AzIuq2g35KZqInN9mGclzcGYIS/cWYkcON9R4Wu KjF56GUWuNlO4eKiOZiJ7+73jg3g02qFW85md+prmEFzvcNysg6pi668PLmeb75ZZTVyhcihFG7 TSx2w2RNEM3o4QvDE0NoD7+QLDq1JP5DbASr174cll9AcXTSvNek4MbvgHE81+TMTypWmUhiatO OWu3kGQpcZykpDm/t5E+JFzL0= X-Google-Smtp-Source: AGHT+IGQBKUyRnSY3uGuCn5Yqobb+H58UfZVQoPukhr0QJVO2OFpqedQVhm/FDPsiicigZHl5TFkDhFNSA== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a05:6902:1b8b:b0:dd9:20c1:85b6 with SMTP id ei11-20020a0569021b8b00b00dd920c185b6mr1004032ybb.2.1711739996049; Fri, 29 Mar 2024 12:19:56 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:46 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-2-jrife@google.com> Subject: [PATCH v1 bpf-next 1/8] selftests/bpf: Introduce sock_addr_testmod From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net sock_addr_testmod provides a mechanism for the sock_addr_kern prog_test to drive socket operations in kernel space. On init, one of the following socket operations is performed based on the module parameters: kernel_bind(), kernel_connect(), or sock_sendmsg()/kernel_sendmsg() and results are exposed through debugfs. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/Makefile | 11 +- .../bpf/sock_addr_testmod/.gitignore | 6 + .../selftests/bpf/sock_addr_testmod/Makefile | 20 ++ .../bpf/sock_addr_testmod/sock_addr_testmod.c | 256 ++++++++++++++++++ 4 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 3b9eb40d63436..b5d02ff724957 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -132,7 +132,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ - xdp_features bpf_test_no_cfi.ko + xdp_features bpf_test_no_cfi.ko sock_addr_testmod.ko TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi @@ -160,6 +160,7 @@ override define CLEAN $(Q)$(RM) -r $(TEST_GEN_FILES) $(Q)$(RM) -r $(EXTRA_CLEAN) $(Q)$(MAKE) -C bpf_testmod clean + $(Q)$(MAKE) -C sock_addr_testmod clean $(Q)$(MAKE) docs-clean endef @@ -260,6 +261,12 @@ $(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_te $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_no_cfi $(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@ +$(OUTPUT)/sock_addr_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard sock_addr_testmod/Makefile sock_addr_testmod/*.[ch]) + $(call msg,MOD,,$@) + $(Q)$(RM) sock_addr_testmod/sock_addr_testmod.ko # force re-compilation + $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C sock_addr_testmod + $(Q)cp sock_addr_testmod/sock_addr_testmod.ko $@ + DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool ifneq ($(CROSS_COMPILE),) CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool @@ -638,6 +645,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \ ip_check_defrag_frags.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/bpf_test_no_cfi.ko \ + $(OUTPUT)/sock_addr_testmod.ko \ $(OUTPUT)/liburandom_read.so \ $(OUTPUT)/xdp_synproxy \ $(OUTPUT)/sign-file \ @@ -767,6 +775,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \ no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \ bpf_test_no_cfi.ko \ + sock_addr_testmod.ko \ liburandom_read.so) .PHONY: docs docs-clean diff --git a/tools/testing/selftests/bpf/sock_addr_testmod/.gitignore b/tools/testing/selftests/bpf/sock_addr_testmod/.gitignore new file mode 100644 index 0000000000000..ded5137772813 --- /dev/null +++ b/tools/testing/selftests/bpf/sock_addr_testmod/.gitignore @@ -0,0 +1,6 @@ +*.mod +*.mod.c +*.o +.ko +/Module.symvers +/modules.order diff --git a/tools/testing/selftests/bpf/sock_addr_testmod/Makefile b/tools/testing/selftests/bpf/sock_addr_testmod/Makefile new file mode 100644 index 0000000000000..47c92fb3a7748 --- /dev/null +++ b/tools/testing/selftests/bpf/sock_addr_testmod/Makefile @@ -0,0 +1,20 @@ +SOCK_ADDR_TEST_MOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(SOCK_ADDR_TEST_MOD_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = sock_addr_testmod.ko + +obj-m += sock_addr_testmod.o +CFLAGS_sock_addr_testmod.o = -I$(src) + +all: + +$(Q)make -C $(KDIR) M=$(SOCK_ADDR_TEST_MOD_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(SOCK_ADDR_TEST_MOD_DIR) clean + diff --git a/tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c b/tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c new file mode 100644 index 0000000000000..de0b2007f0829 --- /dev/null +++ b/tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c @@ -0,0 +1,256 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Google LLC. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define BIND 0 +#define CONNECT 1 +#define SENDMSG 2 + +#define CONNECT_TIMEOUT_SEC 1 + +static char ip[256]; +module_param_string(ip, ip, sizeof(ip), 0644); +MODULE_PARM_DESC(ip, "IPv4/IPv6/Unix address to use for socket operation"); +static char port[7]; +module_param_string(port, port, sizeof(port), 0644); +MODULE_PARM_DESC(port, "Port number to use for socket operation"); +static uint af; +module_param(af, uint, 0644); +MODULE_PARM_DESC(af, "Address family (AF_INET, AF_INET6, or AF_UNIX)"); +static int type; +module_param(type, int, 0644); +MODULE_PARM_DESC(type, "Socket type (SOCK_STREAM or SOCK_DGRAM)"); +static uint op; +module_param(op, uint, 0644); +MODULE_PARM_DESC(op, "Socket operation (BIND=0, CONNECT=1, SENDMSG=2)"); + +static struct debugfs_blob_wrapper sock_name_blob; +static struct debugfs_blob_wrapper peer_name_blob; +static struct debugfs_blob_wrapper addr_blob; +static struct dentry *debugfs_dentry; +static struct sockaddr_storage sock_name; +static struct sockaddr_storage peer_name; +static struct sockaddr_storage addr; +static bool success; + +static struct socket *sock; + +static int do_kernel_bind(struct sockaddr *addr, int addrlen) +{ + int err; + + err = kernel_bind(sock, (struct sockaddr *)addr, addrlen); + if (err) { + pr_err("kernel_bind() returned %d\n", err); + goto err; + } + + err = kernel_getsockname(sock, (struct sockaddr *)&sock_name); + if (err < 0) { + pr_err("kernel_getsockname() returned %d\n", err); + goto err; + } + + if (type == SOCK_STREAM) { + err = kernel_listen(sock, 128); + if (err == -1) { + pr_err("kernel_listen() returned %d\n", err); + goto err; + } + } + + err = 0; + goto out; +err: + err = -1; +out: + return err; +} + +static int do_kernel_connect(struct sockaddr *addr, int addrlen) +{ + int err; + + /* Set timeout for call to kernel_connect() to prevent it from hanging, + * and consider the connection attempt failed if it returns + * -EINPROGRESS. + */ + sock->sk->sk_sndtimeo = CONNECT_TIMEOUT_SEC * HZ; + + err = kernel_connect(sock, addr, addrlen, 0); + if (err) { + pr_err("kernel_connect() returned %d\n", err); + goto err; + } + + err = kernel_getsockname(sock, (struct sockaddr *)&sock_name); + if (err < 0) { + pr_err("kernel_getsockname() returned %d\n", err); + goto err; + } + + err = kernel_getpeername(sock, (struct sockaddr *)&peer_name); + if (err < 0) { + pr_err("kernel_getpeername() returned %d\n", err); + goto err; + } + + err = 0; + goto out; +err: + err = -1; +out: + return err; +} + +static int do_kernel_sendmsg(struct sockaddr *addr, int addrlen) +{ + struct msghdr msg = { + .msg_name = addr, + .msg_namelen = addrlen, + }; + struct kvec iov; + int err; + + iov.iov_base = "abc"; + iov.iov_len = sizeof("abc"); + + err = kernel_sendmsg(sock, &msg, &iov, 1, sizeof("abc")); + if (err < 0) { + pr_err("kernel_sendmsg() returned %d\n", err); + goto err; + } + + /* Unix socket sockaddr hooks may transform msg_namelen. Make sure it is + * unmodified after the call to kernel_sendmsg(). + */ + if (msg.msg_namelen != addrlen) { + pr_err("msg_namelen was modified (original=%d,current=%d)\n", addrlen, msg.msg_namelen); + goto err; + } + + /* kernel_sendmsg() and sock_sendmsg() are both used throughout the + * kernel. Neither of these functions should modify msg_name, so call + * both just to make sure. + */ + iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &iov, 1, sizeof("abc")); + err = sock_sendmsg(sock, &msg); + if (err < 0) { + pr_err("sock_sendmsg() returned %d\n", err); + goto err; + } + + /* Unix socket sockaddr hooks may transform msg_namelen. Make sure it is + * unmodified after the call to sock_sendmsg(). + */ + if (msg.msg_namelen != addrlen) { + pr_err("msg_namelen was modified (original=%d,current=%d)\n", addrlen, msg.msg_namelen); + goto err; + } + + err = 0; + goto out; +err: + err = -1; +out: + return err; +} + +static int do_sock_op(int op, struct sockaddr *addr, int addrlen) +{ + switch (op) { + case BIND: + return do_kernel_bind(addr, addrlen); + case CONNECT: + return do_kernel_connect(addr, addrlen); + case SENDMSG: + return do_kernel_sendmsg(addr, addrlen); + default: + return -EINVAL; + } +} + +static int kernel_sock_addr_testmod_init(void) +{ + int addr_len = sizeof(struct sockaddr_storage); + int proto; + int err; + + debugfs_dentry = debugfs_create_dir("sock_addr_testmod", NULL); + + addr_blob.data = &addr; + addr_blob.size = sizeof(addr); + sock_name_blob.data = &sock_name; + sock_name_blob.size = sizeof(sock_name); + peer_name_blob.data = &peer_name; + peer_name_blob.size = sizeof(peer_name); + + debugfs_create_blob("addr", 0444, debugfs_dentry, &addr_blob); + debugfs_create_blob("sock_name", 0444, debugfs_dentry, &sock_name_blob); + debugfs_create_blob("peer_name", 0444, debugfs_dentry, &peer_name_blob); + debugfs_create_bool("success", 0444, debugfs_dentry, &success); + + switch (af) { + case AF_INET: + case AF_INET6: + err = inet_pton_with_scope(&init_net, af, ip, port, &addr); + if (err) { + pr_err("inet_pton_with_scope() returned %d\n", err); + goto err; + } + + proto = type == SOCK_STREAM ? IPPROTO_TCP : IPPROTO_UDP; + break; + case AF_UNIX: + memset(&addr, 0, sizeof(addr)); + ((struct sockaddr_un *)&addr)->sun_family = AF_UNIX; + ((struct sockaddr_un *)&addr)->sun_path[0] = 0; // abstract + strcpy(((struct sockaddr_un *)&addr)->sun_path + 1, ip); + addr_len = offsetof(struct sockaddr_un, sun_path) + 1 + + strlen(ip); + proto = PF_UNIX; + break; + default: + pr_err("invalid address family %d\n", af); + goto err; + } + + err = sock_create_kern(&init_net, af, type, proto, &sock); + if (err) { + pr_err("sock_create_kern() returned %d\n", err); + goto err; + } + + if (do_sock_op(op, (struct sockaddr *)&addr, addr_len)) + goto err; + + success = true; + goto out; +err: + success = false; +out: + return 0; +} + +static void kernel_sock_addr_testmod_exit(void) +{ + if (sock) + sock_release(sock); + + debugfs_remove_recursive(debugfs_dentry); +} + +module_init(kernel_sock_addr_testmod_init); +module_exit(kernel_sock_addr_testmod_exit); + +MODULE_AUTHOR("Jordan Rife"); +MODULE_DESCRIPTION("BPF socket address selftests module"); +MODULE_LICENSE("Dual BSD/GPL"); From patchwork Fri Mar 29 19:18:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610972 X-Patchwork-Delegate: bpf@iogearbox.net 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 D649D130E5D for ; Fri, 29 Mar 2024 19:19:58 +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=1711740000; cv=none; b=P0wIUDff9ZLdzcUCzjqqrNEuVUiCsekZTtfK4BgL/Hc/FnTdl9Jw4KxYcaor7zVXueYTI+8/vMeAY4etV5ygbKAz3ub29B4h6L5L2MZprUKZZfE15FAmWonFPQi/KHaGXwqqBN7W3W+f9dR/Z9CATiSnnAiANftQOUfrTQ/NkAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740000; c=relaxed/simple; bh=11J1oF1EtXV1qtgpaShlOiZNJHDVgqvSLoQIBQbUPeo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cf7CFY+ghNyB6zbK8bzmgIpy1AM5FYHFzyEYH3d9coOEwpln2Nn5Cztv7U7LaRIab57G2q7LaIk6KImVxzKRsJ4If9phgSxS6JlqzhzlIwManLRg9TVeKtf214p+rlDwK+K2ZFrefNGZ8xEOv1bQ5PVKfJmGWfHPGt2UnNcnFUI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dvYW/EB/; 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--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dvYW/EB/" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6144de213f3so13097857b3.3 for ; Fri, 29 Mar 2024 12:19:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711739998; x=1712344798; 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=dRiul2graw1bxh54kq90mvx6bUbLW8gw8FgM2SMnjyw=; b=dvYW/EB/z+3f5U11Uf2GmwCYRX48BdiL4yzEQrxCWRm8e4WmsOZA4JA1wlAvT9GHBR OSCmOEeFkN1j+Jamb1NkmzP5cry4Ll6VOeDKEzB7i8iOjts0wyKLnQL5j0QpM4LxqiFX QR+N47qMiESW9nzJtmv713Smv3VyuuUtHgZZOcPLGzgmkXNWohmitoJM1E75Y1Bn4jAD ucH5kFk7AkM5HDoaMWa8P+df7Htel1CaAU9Hz9/f7zSnulwU2hE8Znf7jkOOOj1O+cGU HoEoeXWbOA7uEQp6Wb/ZqwKUpqFurU5vMfM6xz6pHtPMgqfQFANj2Xs2zpE8QjWeWRsh R83A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711739998; x=1712344798; 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=dRiul2graw1bxh54kq90mvx6bUbLW8gw8FgM2SMnjyw=; b=gpT1zyXzxY2xb+Sp0jPEBPn9oIosd6XvbckO87mtH5JQcRHXcyPHonxW7yyBvKj2dU dwCz+kum2W6p+7lB5ghPo5PM83eRxJLkSXez9zHJ6v/+MdZ2xYSRXM4rQtI4UEskHxXs fadn58WijXFldPx33S/+pk1SP07n2UrnPiOzmJz+ImyreQH1w+Yg6BfRg+lM2oM8FsHs fo4Q/6Gc9YsQZDcAIEhAS0Ul6whsZ4zw/tlQDvWfvgJd5F4selRU22/aczxxM2D9uSZr HJ9oAaGq32YAzru4m8r7awIErzX204jV/0ysoj31GMgNakkkgJ/Y+d7icO8AUmMvPB+j IjJQ== X-Gm-Message-State: AOJu0Yw1yxfPdEE+PVvnGcaKIBDYRpLLCpcDLoDkvZTf5OseOdXJIlqN PhwRDmfq7lX3kHiYjvsTMDrWQmqiedm3J47QuxssR6n5Rj++ponwT2HFZQJqZPm4TTYRrjBQVKd P+otVZHmDKLS6pJlif3P0dPYnCshjAq2IRJeay34Tcd57O/NwqrqfcjZPMhbynvAKKrBGOWiua3 ci/QFHSbyivCLVmQk1wAm0Okk= X-Google-Smtp-Source: AGHT+IHRdLbvyHJnMHKtWdr+cFy6jSU0+diPw3ZFjoZcj2SJx21rEPQB79DhdlcNPoxtA68+qL8xQhArzw== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a0d:d9c8:0:b0:613:eb1a:6407 with SMTP id b191-20020a0dd9c8000000b00613eb1a6407mr706881ywe.9.1711739997755; Fri, 29 Mar 2024 12:19:57 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:47 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-3-jrife@google.com> Subject: [PATCH v1 bpf-next 2/8] selftests/bpf: Add module load helpers From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net This patch introduces a helpers used by the sock_addr_kern test program that allow it to load and unload sock_addr_testmod. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/testing_helpers.c | 44 ++++++++++++++----- tools/testing/selftests/bpf/testing_helpers.h | 2 + 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c index 28b6646662af6..16959a748d4b1 100644 --- a/tools/testing/selftests/bpf/testing_helpers.c +++ b/tools/testing/selftests/bpf/testing_helpers.c @@ -366,48 +366,68 @@ int delete_module(const char *name, int flags) return syscall(__NR_delete_module, name, flags); } -int unload_bpf_testmod(bool verbose) +static int unload_mod(char name[], bool verbose) { if (kern_sync_rcu()) fprintf(stdout, "Failed to trigger kernel-side RCU sync!\n"); - if (delete_module("bpf_testmod", 0)) { + if (delete_module(name, 0)) { if (errno == ENOENT) { if (verbose) - fprintf(stdout, "bpf_testmod.ko is already unloaded.\n"); + fprintf(stdout, "%s is already unloaded.\n", name); return -1; } - fprintf(stdout, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno); + fprintf(stdout, "Failed to unload %so from kernel: %d\n", name, -errno); return -1; } if (verbose) - fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n"); + fprintf(stdout, "Successfully unloaded %s.\n", name); return 0; } -int load_bpf_testmod(bool verbose) +int unload_bpf_testmod(bool verbose) +{ + return unload_mod("bpf_testmod", verbose); +} + +int unload_bpf_sock_addr_testmod(bool verbose) +{ + return unload_mod("sock_addr_testmod", verbose); +} + +static int load_mod(const char *name, const char *param_values, bool verbose) { int fd; if (verbose) - fprintf(stdout, "Loading bpf_testmod.ko...\n"); + fprintf(stdout, "Loading %s...\n", name); - fd = open("bpf_testmod.ko", O_RDONLY); + fd = open(name, O_RDONLY); if (fd < 0) { - fprintf(stdout, "Can't find bpf_testmod.ko kernel module: %d\n", -errno); + fprintf(stdout, "Can't find %s kernel module: %d\n", name, -errno); return -ENOENT; } - if (finit_module(fd, "", 0)) { - fprintf(stdout, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno); + if (finit_module(fd, param_values, 0)) { + fprintf(stdout, "Failed to load %s into the kernel: %d\n", name, -errno); close(fd); return -EINVAL; } close(fd); if (verbose) - fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n"); + fprintf(stdout, "Successfully loaded %s.\n", name); return 0; } +int load_bpf_testmod(bool verbose) +{ + return load_mod("bpf_testmod.ko", "", verbose); +} + +int load_bpf_sock_addr_testmod(const char *param_values, bool verbose) +{ + return load_mod("sock_addr_testmod.ko", param_values, verbose); +} + /* * Trigger synchronize_rcu() in kernel. */ diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h index d55f6ab124338..d553baa01d597 100644 --- a/tools/testing/selftests/bpf/testing_helpers.h +++ b/tools/testing/selftests/bpf/testing_helpers.h @@ -34,7 +34,9 @@ int parse_test_list_file(const char *path, __u64 read_perf_max_sample_freq(void); int load_bpf_testmod(bool verbose); +int load_bpf_sock_addr_testmod(const char *param_values, bool verbose); int unload_bpf_testmod(bool verbose); +int unload_bpf_sock_addr_testmod(bool verbose); int kern_sync_rcu(void); int finit_module(int fd, const char *param_values, int flags); int delete_module(const char *name, int flags); From patchwork Fri Mar 29 19:18:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610973 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DD3D25777 for ; Fri, 29 Mar 2024 19:20:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740002; cv=none; b=jaBUsS1w029yNIUzL+adicdenjMvV0rVl1YUXddnURjWieianhYJfTrw+2AmAgpIunz+fTtcoXLbnT9q10Ksl3bQIg2TQrag7eUOXVv73cba2DkjKeFM9Bm44MmJslv9G9Z4XcJX4yNCN5PBMjvafw270XwSGkWhimRsdJTRZJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740002; c=relaxed/simple; bh=X1TbuMHoleP7CfuoPSPY5DaocW44cFI3B40G/uoR0EE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JxdSajbRxiQhKseHSFn3gH5nZhSXSDB0ts+XQmYh+4bdG5oIsJH2Lu1w4NV9HFQlaEcrSctgld/9nh5cIoRyM5HR6TQnKqSrAM+LSAP9EKydgiHB6oxZS7ea+piuo0X+wBN/mNdSk3kMwG4ROs3IPJxXvYrtKlzGCW2Vuhx2doY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fntEcFJt; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fntEcFJt" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dced704f17cso3902287276.1 for ; Fri, 29 Mar 2024 12:20:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711739999; x=1712344799; 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=GxOJxV7C93f80JjMcGj5DhuREG1NQh5Bl14os2SU3XM=; b=fntEcFJtlvA5XsbBc+IFXpOLVMeQtfuXfKqLdRZ/yc5e3HJiUez4L1vt4E9ql+wZ3Y df5hgjHSfsBLm9NBzidjXyezQ62mzLpplwlwf29E8cQ69q+n7L9wCgVknoixOaoqNg1T zqVK/Uk6NmIj640xSuTlZg8Ef72tKvVk6d1dAiBPy1c/u0rUkhZqu2t7RKxuMywyHZoQ nzZU49nD+6c2ZjQyo2lPuveCs5XOMGkxm/q8BHE1HiBR67INTKsd2UGD4JNxuCiOaq2d ikou/GfWc6kvb9CJQV2Iz+lK3//vbSPqPcS61C2WJTJaDk2P/T5ukCEGfXEIQyk0bzr6 9Tew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711739999; x=1712344799; 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=GxOJxV7C93f80JjMcGj5DhuREG1NQh5Bl14os2SU3XM=; b=fXlSsoIYkNUWaQi6iTENGP5fjMMXjkkTrQOhhct1LAwEr3jx2mK7u+Fct5eBjtCJLV 01PhS1bjStEbqt1xKeZVYYI20KJuSbXgsUeY2VWf0kXl6BTh1K9uETDnCW8LunezlbA+ mBONsGZvcjc8+l1e4nj4OlnVN7r+hX/bS6zpfehY1D2y4JHY7piNr7WMkm4LbT9zjuaV T44gWS5hqoN+5WVk+KRc9iCBMB4OMfXOvDk9sli8Uftrf/8cqBf/nxsW2+nOQT1jAXWj rav09/gnnmi85B8nDEpD+nTkICFQ/bF/oj7Pp2yL5J4/++lMWSMLqL6pPYcyUGxHFUPB +tHA== X-Gm-Message-State: AOJu0YxCY++kSq8S6D9v5URyfWbOQjtybLr3nPEiVX3P1Mbq2FHTT0kz OeANCfd68TsZV+/D9ayikgZcw/0bEjuk3J7IR1fP+sI1ypK8WyTEJZKipNzLcScvTWTzQ+wtDpy 1IvcnOq1BBMCXcg5QlcTrO5ZolYkQ4yQSTSzvCmAFQQ4skCGk5cdKjsDh+EhdPHtpw/pMVWV9K6 fpYhAz4Q0Pd7lP1+DuCzkSuVs= X-Google-Smtp-Source: AGHT+IH2i8Os2O9p1v2f6xqox5g0brFwHzIYIqzJjgFXwfBLBENk4YiWQ9985pU5BPfz5rHyoZOonZ1Q2Q== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a05:6902:1006:b0:dcc:94b7:a7a3 with SMTP id w6-20020a056902100600b00dcc94b7a7a3mr254520ybt.12.1711739999404; Fri, 29 Mar 2024 12:19:59 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:48 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-4-jrife@google.com> Subject: [PATCH v1 bpf-next 3/8] selftests/bpf: Factor out cmp_addr From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net Factor out cmp_addr into network_helpers.c to so that it can be used by the sock_addr_kern test program. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/network_helpers.c | 34 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 4 +++ .../selftests/bpf/prog_tests/sock_addr.c | 34 ------------------- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 6db27a9088e97..8eae4afe3ec3d 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -497,3 +497,37 @@ int get_socket_local_port(int sock_fd) return -1; } + +int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, + const struct sockaddr_storage *addr2, socklen_t addr2_len, + bool cmp_port) +{ + const struct sockaddr_in *four1, *four2; + const struct sockaddr_in6 *six1, *six2; + const struct sockaddr_un *un1, *un2; + + if (addr1->ss_family != addr2->ss_family) + return -1; + + if (addr1_len != addr2_len) + return -1; + + if (addr1->ss_family == AF_INET) { + four1 = (const struct sockaddr_in *)addr1; + four2 = (const struct sockaddr_in *)addr2; + return !((four1->sin_port == four2->sin_port || !cmp_port) && + four1->sin_addr.s_addr == four2->sin_addr.s_addr); + } else if (addr1->ss_family == AF_INET6) { + six1 = (const struct sockaddr_in6 *)addr1; + six2 = (const struct sockaddr_in6 *)addr2; + return !((six1->sin6_port == six2->sin6_port || !cmp_port) && + !memcmp(&six1->sin6_addr, &six2->sin6_addr, + sizeof(struct in6_addr))); + } else if (addr1->ss_family == AF_UNIX) { + un1 = (const struct sockaddr_un *)addr1; + un2 = (const struct sockaddr_un *)addr2; + return memcmp(un1, un2, addr1_len); + } + + return -1; +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 94b9be24e39bc..a4a458c858d59 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -61,6 +61,10 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); int get_socket_local_port(int sock_fd); +int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, + const struct sockaddr_storage *addr2, socklen_t addr2_len, + bool cmp_port); + struct nstoken; /** diff --git a/tools/testing/selftests/bpf/prog_tests/sock_addr.c b/tools/testing/selftests/bpf/prog_tests/sock_addr.c index 5fd6177189915..c1db1d7e08b04 100644 --- a/tools/testing/selftests/bpf/prog_tests/sock_addr.c +++ b/tools/testing/selftests/bpf/prog_tests/sock_addr.c @@ -246,40 +246,6 @@ static struct sock_addr_test tests[] = { typedef int (*info_fn)(int, struct sockaddr *, socklen_t *); -static int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, - const struct sockaddr_storage *addr2, socklen_t addr2_len, - bool cmp_port) -{ - const struct sockaddr_in *four1, *four2; - const struct sockaddr_in6 *six1, *six2; - const struct sockaddr_un *un1, *un2; - - if (addr1->ss_family != addr2->ss_family) - return -1; - - if (addr1_len != addr2_len) - return -1; - - if (addr1->ss_family == AF_INET) { - four1 = (const struct sockaddr_in *)addr1; - four2 = (const struct sockaddr_in *)addr2; - return !((four1->sin_port == four2->sin_port || !cmp_port) && - four1->sin_addr.s_addr == four2->sin_addr.s_addr); - } else if (addr1->ss_family == AF_INET6) { - six1 = (const struct sockaddr_in6 *)addr1; - six2 = (const struct sockaddr_in6 *)addr2; - return !((six1->sin6_port == six2->sin6_port || !cmp_port) && - !memcmp(&six1->sin6_addr, &six2->sin6_addr, - sizeof(struct in6_addr))); - } else if (addr1->ss_family == AF_UNIX) { - un1 = (const struct sockaddr_un *)addr1; - un2 = (const struct sockaddr_un *)addr2; - return memcmp(un1, un2, addr1_len); - } - - return -1; -} - static int cmp_sock_addr(info_fn fn, int sock1, const struct sockaddr_storage *addr2, socklen_t addr2_len, bool cmp_port) From patchwork Fri Mar 29 19:18:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610974 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46F7413A879 for ; Fri, 29 Mar 2024 19:20:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740003; cv=none; b=lHKvMZBWf5TK9PonCPx5n1tMki1cAYsH24JehTvRyZnySH/j90rWFE1REFHS8eltDZZO978M9nGiOXulDRSVAwqV1cnpx1ROOBVYK8ooGEeJTzdT9G0uf6U///wcROjBa/djQH0ZuYLgB6cTomoOyl5gyZWKVrMEFs2ZiJu7WHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740003; c=relaxed/simple; bh=WvFjbe5Gio5p4zohr/Ppr/b/4wbz85m1gtQran9FEU0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lzJ5djwnodh+3oIHR5eEw5YaQv/QbFE/XpowdZ3/clqtQrQ5MJwx/IeREK6IIHKwx7cgpj7lApSnqoapqc+XJf89rFHAkTqqG4h58ShZzQjGgk9nedGFHWeguDWHPV9t7ZvzGMsDiNCIt2yLPuMEPshTQmixvB7J7PORq/EyDM4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=TvvCJknS; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TvvCJknS" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so3954330276.0 for ; Fri, 29 Mar 2024 12:20:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711740001; x=1712344801; 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=SbvV0u8XBY9xDuz2lN6+l2kDTrO5oaEqCBNhNq59mVw=; b=TvvCJknSETV2b5BHoBV+KHircI8RG0r3NWGwFE3P8O2fNVH8UhXhSyYWOs8xHMQwi+ JQcm/rFPV9qi9wcA+Cycz3qFdUQafYOqHgt7tcWjRmadeCspMxquKCarJlP/2Tt0gPyK vWBWsxJQVSYM2mQvfjpCnxIrozNIs1BqOlq0MMzjIL5TMfH1N+yqPtXN0G+CVKEBPePq mkDZI56i3WlEsh/Xdis8+3OyY5ZQE9BCFrRSqwID+0yRUt2ulb95t6T99qGGG9+ehzyQ ggprjrdAJgvVv0YtvPFnyS2ECmQzTXvgAF2wG/UoAlO5rA3wISh+LLYNTrg9KKpWV6Vu VIdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711740001; x=1712344801; 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=SbvV0u8XBY9xDuz2lN6+l2kDTrO5oaEqCBNhNq59mVw=; b=nL2Gdf9jC3CbxtMHnGPuMUSdNnIDH8a7OW2+MC//HyEwTO2oy74jZ0K7ynxZvSPgcm Ca1Y6ABxSnKxF5sjIp8SpYlT4eusJZCdWxYU5+aGZRFXMm5FaBiLJfGEkIdLORVtLpds HxWoxGCU5BO2hkbNGrNrVWGU4+QvoSz6VYYtnY974MbTaNCipG4Gwnz+sbzeoIvjuYHJ sJmLurPYXtJONHM+ASRdIW8O0Szq9cL44j5xko4ChBTfP0IHU2iEVx7jdh/EVKgkJ77y TwOIFa2RqQ9/6bN/4xb1ARxiPjDf2HWsHXqthjn9gWNzO9UluQQ95M1HilTh7ychrqnr zycQ== X-Gm-Message-State: AOJu0Yw6tE4qHgbjg1OjEyNL+ImMtmZFsqdgkKKseERh7FHI+lSv+bvw HV28bGp7Nlm9QFC77iwwDM9WrrNCpIjIkYlNAyhXibz8txCm4nh+avX9r8iYT00EIVXR6IBaGBj u93ZlkTCUjcqvRJeNUS7xr+8JcinfdOd7WjJZVgzraVdurfiN0xgIPAjLFJ1Z4krR4/1JVwfPMe 3Mk20ZakbAR2LjBG7eDrTQM7o= X-Google-Smtp-Source: AGHT+IHV9/yL97+mJdhDWN8bvqgaKmZP0Anot8i5ZOBRqXEQJ86jHkPNBbIG+1NcaUZYkN/9StR9FKyYew== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a05:6902:2404:b0:dcc:5a91:aee9 with SMTP id dr4-20020a056902240400b00dcc5a91aee9mr893141ybb.7.1711740000986; Fri, 29 Mar 2024 12:20:00 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:49 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-5-jrife@google.com> Subject: [PATCH v1 bpf-next 4/8] selftests/bpf: Add recv_msg_from_client to network helpers From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net Copy recv_msg_from_client helper from test_sock_addr self test in order to use it in the sock_addr_kern test program. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/network_helpers.c | 31 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 32 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 8eae4afe3ec3d..ef0a94d9e9d5c 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -498,6 +498,37 @@ int get_socket_local_port(int sock_fd) return -1; } +int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr) +{ + struct timeval tv; + struct msghdr hdr; + struct iovec iov; + char data[64]; + fd_set rfds; + + FD_ZERO(&rfds); + FD_SET(sockfd, &rfds); + + tv.tv_sec = 2; + tv.tv_usec = 0; + + if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 || + !FD_ISSET(sockfd, &rfds)) + return -1; + + memset(&iov, 0, sizeof(iov)); + iov.iov_base = data; + iov.iov_len = sizeof(data); + + memset(&hdr, 0, sizeof(hdr)); + hdr.msg_name = src_addr; + hdr.msg_namelen = sizeof(struct sockaddr_storage); + hdr.msg_iov = &iov; + hdr.msg_iovlen = 1; + + return recvmsg(sockfd, &hdr, 0); +} + int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, const struct sockaddr_storage *addr2, socklen_t addr2_len, bool cmp_port) diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index a4a458c858d59..f88f2470e3fcb 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -61,6 +61,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); int get_socket_local_port(int sock_fd); +int recvmsg_from_client(int sockfd, struct sockaddr_storage *src_addr); int cmp_addr(const struct sockaddr_storage *addr1, socklen_t addr1_len, const struct sockaddr_storage *addr2, socklen_t addr2_len, bool cmp_port); From patchwork Fri Mar 29 19:18:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610975 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29D5A13474E for ; Fri, 29 Mar 2024 19:20:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740005; cv=none; b=NC/OFcuApZjCfX8VVHhsH+faANHMRsPzQR6huF60xIQvVeUqYjj3I0J0nkMS1S7CXhs/lxwssf+CRPusGx5A3B2L2UIlWM5xZCd8UAAkKDFg/GpgvVIf66N3gy+SCFljE5Q99vFwgG67moWvC0pFcVhpmjS876eCFBtG2Ijb9Ms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740005; c=relaxed/simple; bh=BcjYvByBQLUQ9OPrptNdY42qwu+ggzunOSva7kCPrk4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FARNLIpdou488GFz/NWMV5/j8LOx+djQxuW11Y0KjMAmWyL3n83V4uKXk2I7vRgOM0Bqv3PIITTKrRiiTbZ6zNFtfWwGv0uKFhpophbZCsbzqMZOD54cO17YA8Od6SkdAJ0pKusIGIXQKdESaQxcMdAck1/wPmgv/RhWmtl/q/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=z3DNnXr7; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="z3DNnXr7" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60a54004e9fso42016107b3.3 for ; Fri, 29 Mar 2024 12:20:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711740003; x=1712344803; 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=0FAb19c85kXjG8+dndUXZwZa/GQ2ubPPIIKCr0AviKI=; b=z3DNnXr7cca712s3BtGRnKK51EI1x5SzbTZXiii7Bi9xnk/3++QTUeCGCWuEAFaEhU GkaxbLOuvK/E53bXdqO8UGQkhl7MSe2xByTZME5Qa7+pArcsD67DdYpQEI31Kntbpxgf Y5BBV1iFojtr1VSpnhnXT/g2heE6yZ/iwvlaTmRv/v+KWn/pEpB+n6dnNLrK5hpkRHYz +7sxbgrlVkdMGE3fteOmv8v9rUvnG4XnYN0pNMEYYY2tcEfuoa9JoPZ/fS/gPz0/tANb +QJJFmYt8+pElh/xcYdj5YWFdTG6USZN6Kah9LZ2jzFVT0NBzpIS40C2QvlIELMuZkzG +Dng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711740003; x=1712344803; 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=0FAb19c85kXjG8+dndUXZwZa/GQ2ubPPIIKCr0AviKI=; b=TLONhmp4V1UcO1a7P8wzxmKWLXbsyFuVm/7vjT2eJ1oVN9tihuwuzCiFnOn+74wHh8 6eU4hFwYWxQQlfNsqkXKA0AdBVzxC5lq+zHy2l62EvErZGwh5zpLW+6Q4vFcAl4cZDUN daDxTLe2cJnkPh9kGXmRKtNsUdNAbql/aRQs4aNxA3JIPtukPOj45IqGFarXisd8GjYw hFZuCgI8LmOQO34tspdjea7vjnrHdQBAKg81qipSWFdZoY2o0K7dnjNkfFvu4K65yRuD YJ6kKTarvnRNO3RnjR5LZD/AvWFwToijEDM8zhgpACvC5hIwpRC27d4oE1o80rvyrmni tmYw== X-Gm-Message-State: AOJu0YwgXun1uzA4OZsn4lI27GgZnotUv1nydnX8K8E6bRdReKzZDgUn jp94UeeDBo6wY5ag9usfZp17B9Vp5SEvG9ldyY3LRfqtHn43SMrI47WcMwL+P0KfTTziT5lW6RI qdBscxX501sA03M+6+jxynzhwY4b41DwUtOoVSAMeJDgcOsWp+mwFxCvD6kGlR81tngHpN7ghVK 6vHFPOyL1g8h9tKDGrmaxrYkY= X-Google-Smtp-Source: AGHT+IGORRu2axiBp/EZZ3Bg2wSe7V1M05tc8fUgXvO/3fHJ2RgQuUdfelBAFL7UWWGTwpLFBQhVgg96rg== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a81:52d3:0:b0:611:3096:bf60 with SMTP id g202-20020a8152d3000000b006113096bf60mr822271ywb.7.1711740002982; Fri, 29 Mar 2024 12:20:02 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:50 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-6-jrife@google.com> Subject: [PATCH v1 bpf-next 5/8] selftests/bpf: Factor out load_path and defines from test_sock_addr From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net Factor out useful defines and load_path() from the test_sock_addr self test to use them in sock_addr_kern. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/Makefile | 34 +++++---- .../testing/selftests/bpf/sock_addr_helpers.c | 46 +++++++++++ .../testing/selftests/bpf/sock_addr_helpers.h | 44 +++++++++++ tools/testing/selftests/bpf/test_sock_addr.c | 76 +++---------------- 4 files changed, 120 insertions(+), 80 deletions(-) create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.c create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.h diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index b5d02ff724957..ccc1c11559a45 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -291,6 +291,7 @@ TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL) $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ) +SOCK_ADDR_HELPERS := $(OUTPUT)/sock_addr_helpers.o TESTING_HELPERS := $(OUTPUT)/testing_helpers.o CGROUP_HELPERS := $(OUTPUT)/cgroup_helpers.o UNPRIV_HELPERS := $(OUTPUT)/unpriv_helpers.o @@ -301,7 +302,7 @@ CAP_HELPERS := $(OUTPUT)/cap_helpers.o $(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) -$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) +$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(SOCK_ADDR_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) $(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) @@ -628,21 +629,22 @@ endef # Define test_progs test runner. TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs -TRUNNER_EXTRA_SOURCES := test_progs.c \ - cgroup_helpers.c \ - trace_helpers.c \ - network_helpers.c \ - testing_helpers.c \ - btf_helpers.c \ - cap_helpers.c \ - unpriv_helpers.c \ - netlink_helpers.c \ - test_loader.c \ - xsk.c \ - disasm.c \ - json_writer.c \ - flow_dissector_load.h \ - ip_check_defrag_frags.h +TRUNNER_EXTRA_SOURCES := test_progs.c \ + cgroup_helpers.c \ + trace_helpers.c \ + network_helpers.c \ + testing_helpers.c \ + btf_helpers.c \ + cap_helpers.c \ + unpriv_helpers.c \ + netlink_helpers.c \ + test_loader.c \ + xsk.c \ + disasm.c \ + json_writer.c \ + flow_dissector_load.h \ + ip_check_defrag_frags.h \ + sock_addr_helpers.c TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/bpf_test_no_cfi.ko \ $(OUTPUT)/sock_addr_testmod.ko \ diff --git a/tools/testing/selftests/bpf/sock_addr_helpers.c b/tools/testing/selftests/bpf/sock_addr_helpers.c new file mode 100644 index 0000000000000..ff2eb09870f16 --- /dev/null +++ b/tools/testing/selftests/bpf/sock_addr_helpers.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +#include +#include + +#include "cgroup_helpers.h" +#include "sock_addr_helpers.h" +#include "testing_helpers.h" + +int load_path(const char *path, enum bpf_attach_type attach_type, + bool expect_reject) +{ + struct bpf_object *obj; + struct bpf_program *prog; + int err; + + obj = bpf_object__open_file(path, NULL); + err = libbpf_get_error(obj); + if (err) { + log_err(">>> Opening BPF object (%s) error.\n", path); + return -1; + } + + prog = bpf_object__next_program(obj, NULL); + if (!prog) + goto err_out; + + bpf_program__set_type(prog, BPF_PROG_TYPE_CGROUP_SOCK_ADDR); + bpf_program__set_expected_attach_type(prog, attach_type); + bpf_program__set_flags(prog, testing_prog_flags()); + + err = bpf_object__load(obj); + if (err) { + if (!expect_reject) + log_err(">>> Loading program (%s) error.\n", path); + goto err_out; + } + + return bpf_program__fd(prog); +err_out: + bpf_object__close(obj); + return -1; +} diff --git a/tools/testing/selftests/bpf/sock_addr_helpers.h b/tools/testing/selftests/bpf/sock_addr_helpers.h new file mode 100644 index 0000000000000..cb17579075143 --- /dev/null +++ b/tools/testing/selftests/bpf/sock_addr_helpers.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __SOCK_ADDR_HELPERS_H +#define __SOCK_ADDR_HELPERS_H + +#include +#include +#include + +#include +#include + +#define CONNECT4_PROG_PATH "./connect4_prog.bpf.o" +#define CONNECT6_PROG_PATH "./connect6_prog.bpf.o" +#define CONNECTUN_PROG_PATH "./connect_unix_prog.bpf.o" +#define SENDMSG4_PROG_PATH "./sendmsg4_prog.bpf.o" +#define SENDMSG6_PROG_PATH "./sendmsg6_prog.bpf.o" +#define RECVMSG4_PROG_PATH "./recvmsg4_prog.bpf.o" +#define RECVMSG6_PROG_PATH "./recvmsg6_prog.bpf.o" +#define BIND4_PROG_PATH "./bind4_prog.bpf.o" +#define BIND6_PROG_PATH "./bind6_prog.bpf.o" + +#define SERV4_IP "192.168.1.254" +#define SERV4_REWRITE_IP "127.0.0.1" +#define SRC4_IP "172.16.0.1" +#define SRC4_REWRITE_IP "127.0.0.4" +#define SERV4_PORT 4040 +#define SERV4_REWRITE_PORT 4444 + +#define SERV6_IP "face:b00c:1234:5678::abcd" +#define SERV6_REWRITE_IP "::1" +#define SERV6_V4MAPPED_IP "::ffff:192.168.0.4" +#define SRC6_IP "::1" +#define SRC6_REWRITE_IP "::6" +#define WILDCARD6_IP "::" +#define SERV6_PORT 6060 +#define SERV6_REWRITE_PORT 6666 + +#define SERVUN_ADDRESS "bpf_cgroup_unix_test" +#define SERVUN_REWRITE_ADDRESS "bpf_cgroup_unix_test_rewrite" + +int load_path(const char *path, enum bpf_attach_type attach_type, + bool expected_reject); + +#endif diff --git a/tools/testing/selftests/bpf/test_sock_addr.c b/tools/testing/selftests/bpf/test_sock_addr.c index 80c42583f5977..89c106832f9a4 100644 --- a/tools/testing/selftests/bpf/test_sock_addr.c +++ b/tools/testing/selftests/bpf/test_sock_addr.c @@ -21,37 +21,13 @@ #include "cgroup_helpers.h" #include "testing_helpers.h" #include "bpf_util.h" +#include "sock_addr_helpers.h" #ifndef ENOTSUPP # define ENOTSUPP 524 #endif #define CG_PATH "/foo" -#define CONNECT4_PROG_PATH "./connect4_prog.bpf.o" -#define CONNECT6_PROG_PATH "./connect6_prog.bpf.o" -#define SENDMSG4_PROG_PATH "./sendmsg4_prog.bpf.o" -#define SENDMSG6_PROG_PATH "./sendmsg6_prog.bpf.o" -#define RECVMSG4_PROG_PATH "./recvmsg4_prog.bpf.o" -#define RECVMSG6_PROG_PATH "./recvmsg6_prog.bpf.o" -#define BIND4_PROG_PATH "./bind4_prog.bpf.o" -#define BIND6_PROG_PATH "./bind6_prog.bpf.o" - -#define SERV4_IP "192.168.1.254" -#define SERV4_REWRITE_IP "127.0.0.1" -#define SRC4_IP "172.16.0.1" -#define SRC4_REWRITE_IP "127.0.0.4" -#define SERV4_PORT 4040 -#define SERV4_REWRITE_PORT 4444 - -#define SERV6_IP "face:b00c:1234:5678::abcd" -#define SERV6_REWRITE_IP "::1" -#define SERV6_V4MAPPED_IP "::ffff:192.168.0.4" -#define SRC6_IP "::1" -#define SRC6_REWRITE_IP "::6" -#define WILDCARD6_IP "::" -#define SERV6_PORT 6060 -#define SERV6_REWRITE_PORT 6666 - #define INET_NTOP_BUF 40 struct sock_addr_test; @@ -661,58 +637,30 @@ static int load_insns(const struct sock_addr_test *test, return ret; } -static int load_path(const struct sock_addr_test *test, const char *path) +static int ld_path(const struct sock_addr_test *test, const char *path) { - struct bpf_object *obj; - struct bpf_program *prog; - int err; - - obj = bpf_object__open_file(path, NULL); - err = libbpf_get_error(obj); - if (err) { - log_err(">>> Opening BPF object (%s) error.\n", path); - return -1; - } - - prog = bpf_object__next_program(obj, NULL); - if (!prog) - goto err_out; - - bpf_program__set_type(prog, BPF_PROG_TYPE_CGROUP_SOCK_ADDR); - bpf_program__set_expected_attach_type(prog, test->expected_attach_type); - bpf_program__set_flags(prog, testing_prog_flags()); - - err = bpf_object__load(obj); - if (err) { - if (test->expected_result != LOAD_REJECT) - log_err(">>> Loading program (%s) error.\n", path); - goto err_out; - } - - return bpf_program__fd(prog); -err_out: - bpf_object__close(obj); - return -1; + return load_path(path, test->expected_attach_type, + test->expected_result == LOAD_REJECT); } static int bind4_prog_load(const struct sock_addr_test *test) { - return load_path(test, BIND4_PROG_PATH); + return ld_path(test, BIND4_PROG_PATH); } static int bind6_prog_load(const struct sock_addr_test *test) { - return load_path(test, BIND6_PROG_PATH); + return ld_path(test, BIND6_PROG_PATH); } static int connect4_prog_load(const struct sock_addr_test *test) { - return load_path(test, CONNECT4_PROG_PATH); + return ld_path(test, CONNECT4_PROG_PATH); } static int connect6_prog_load(const struct sock_addr_test *test) { - return load_path(test, CONNECT6_PROG_PATH); + return ld_path(test, CONNECT6_PROG_PATH); } static int xmsg_ret_only_prog_load(const struct sock_addr_test *test, @@ -800,12 +748,12 @@ static int sendmsg4_rw_asm_prog_load(const struct sock_addr_test *test) static int recvmsg4_rw_c_prog_load(const struct sock_addr_test *test) { - return load_path(test, RECVMSG4_PROG_PATH); + return ld_path(test, RECVMSG4_PROG_PATH); } static int sendmsg4_rw_c_prog_load(const struct sock_addr_test *test) { - return load_path(test, SENDMSG4_PROG_PATH); + return ld_path(test, SENDMSG4_PROG_PATH); } static int sendmsg6_rw_dst_asm_prog_load(const struct sock_addr_test *test, @@ -868,7 +816,7 @@ static int sendmsg6_rw_asm_prog_load(const struct sock_addr_test *test) static int recvmsg6_rw_c_prog_load(const struct sock_addr_test *test) { - return load_path(test, RECVMSG6_PROG_PATH); + return ld_path(test, RECVMSG6_PROG_PATH); } static int sendmsg6_rw_v4mapped_prog_load(const struct sock_addr_test *test) @@ -883,7 +831,7 @@ static int sendmsg6_rw_wildcard_prog_load(const struct sock_addr_test *test) static int sendmsg6_rw_c_prog_load(const struct sock_addr_test *test) { - return load_path(test, SENDMSG6_PROG_PATH); + return ld_path(test, SENDMSG6_PROG_PATH); } static int cmp_addr(const struct sockaddr_storage *addr1, From patchwork Fri Mar 29 19:18:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610976 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA979136E2F for ; Fri, 29 Mar 2024 19:20:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740007; cv=none; b=jDu0rwaQapfdWuoDe00+J+kkOrpjdvEC/Nzd6eQmGBNhgkdut4JaVZwXX7Kk46IppDsHCBK0kmDT9KeUp2qPVY0HCeBBxUA3g0B9w1i9Mm0ovhLCSmbM+iUD1PvNOMQ9pv5VlWB/rp4QUNqchfj/lYlOGuofgnoQ34IOM8rsbTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740007; c=relaxed/simple; bh=FSP1oV2T/usULlM1VYDd08bWBIJsZLOi020rBF4z6Bw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eGtuNHMojoJOybwQ3dk+1Nce8QuEd/NH4BpqQayrXWnpuV96bMTZo93bekxEbcDe7I1kPgndS14YmnEA96O7CvSPB/0HpQWheYo8OwJFX4UPbaq/nd8cF3iDnfSoXC7pIWu9EyCXuVDOMOsKdmCzIHX1Vaj38+fMCChtcXQyfw8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AdbcJ4bQ; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AdbcJ4bQ" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc64f63d768so3910003276.2 for ; Fri, 29 Mar 2024 12:20:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711740004; x=1712344804; 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=EcI6g4FflKBTSfJKFEIW1tJynz7mTB9AleLJZXHTyLA=; b=AdbcJ4bQH0gqZlhr0WmKydY/gGFJ5IJBwSncVy5cbhKignhCun7QzKW99CTVCNI+qg sTRd3wegMfdO3o1EGbSAcnX/D52gL9lBPcB8On5hwkfuvvXzeTVpTljG5BLvMeSImJHk qwfn03aZx2/S5OVRYvblMPx1JuKKgcw9A0ZGbUi0rLQEtg6n4gCTEdxEJPO6QUM97ATO +92G7aGo2SS5nWgweMQ5kbak4gEVxDK9KhrYDkZtEzjBAwGPYnc71YDQebYJ4jXVdg2Q oG2zXnftogW4eTNIrCxyE8+ozwFpyicYPMX5S9Fn6uNjyzHXDGnWcb/p39ZRcg5oxMFW 88ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711740004; x=1712344804; 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=EcI6g4FflKBTSfJKFEIW1tJynz7mTB9AleLJZXHTyLA=; b=eHmioJhu7QE2rLbW/AzzqWtn7kgXBNHXAYZQmkzXfwyxRJGLlMwPhvw4J8fr0H3jkW fKhkQ6m/OOnxyhKoGaIPGRyxSU9qYs+uNAVj+LhKTU2l4uChgMFIdIFY5P9us3CQnhAO TXcE9HG9lfFj3NeLfh2KlwjFs9LNze10xsfLGg96mqJYBHAPNDshFYxoryNVuBayrS0Z oAM2qUAmeSl6lsyJFhH+7m+ntCYYzZLy2D9r5uVTLl9wNYLgqN7mWIXHBUSkoXowLRWO 99NaTxKLM6RJDmaXm72Dkou9b0ebEebDO08g2SLOYItGa3qwitK/wV4f2coRDlTtyFAk IV3w== X-Gm-Message-State: AOJu0YzxxXaOvJmRMG61FdzcY7zyG49m8a+H9aWXBUteGULCluOpiUm5 R+0HBEoo4J7zSAp47vpGGtulaGVpmi8Z7jGl9lwm6+7ntgW3Ithzcr/Ne86CjqJ6fTS2MJlJCDH u+uVVa7qTwpQA4ek38gaNFkgH1C4xKbL2dq8ZE9Kn0tkF8rzjPdwpgmuwdmw6GUDzKT8s8hjlIw 2jiheiyg/PcWT5vqYZCznTyzU= X-Google-Smtp-Source: AGHT+IGH0KK9182UUL1GAJNaUHzbTv3CkROT6oxKGok86GWqAdDqq2cDBN1yFx4i1s+2vMk+0slVLQNbbA== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a05:6902:2306:b0:dcc:50ca:e153 with SMTP id do6-20020a056902230600b00dcc50cae153mr932390ybb.7.1711740004528; Fri, 29 Mar 2024 12:20:04 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:51 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-7-jrife@google.com> Subject: [PATCH v1 bpf-next 6/8] selftests/bpf: Add setup/cleanup subcommands From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net Add optional setup/cleanup subcommands to test_sock_addr.sh to allow those phases to be driven externally by the sock_addr_kern test program. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/test_sock_addr.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/test_sock_addr.sh b/tools/testing/selftests/bpf/test_sock_addr.sh index 3b9fdb8094aa2..dc0dff612b0d2 100755 --- a/tools/testing/selftests/bpf/test_sock_addr.sh +++ b/tools/testing/selftests/bpf/test_sock_addr.sh @@ -55,4 +55,12 @@ TEST_IPv4="127.0.0.4/8" TEST_IPv6="::6/128" MAX_PING_TRIES=5 -main +if [ $# = 0 ]; then + main +elif [ $1 = "setup" ]; then + setup +elif [ $1 = "cleanup" ]; then + cleanup +else + echo "invalid option: $1" +fi From patchwork Fri Mar 29 19:18:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610977 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56A0513B2B8 for ; Fri, 29 Mar 2024 19:20:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740009; cv=none; b=eLKlgqj5ozWLvB+EbKtkufdSGdpSbJOwy3ospde1Tk+T5/28N2p3MSXmQ/YzdYpQDbFRdcDfET1c4MYzWqrMl47EBY2EWWLBZ9I4hv29xbmhWoYyDnDo7VneeAf+r2qzAEDrAzvrXTg0nlTePtmoWLet3XuTrm/6SzKMXH9BZCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740009; c=relaxed/simple; bh=lq+VHZ/6SliQ0l8D/03cbanG9rZRe9h1jG9pu+GwWnE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Q0whMrcdiK9haqqB8RRMof87Rr0L5GDl2fKBYJdoQ29L+w9Al/iK1kq9MnOFGORZRhYUhf9APf0vPlbIqmh4YmfcmFmfnctPZI4XHmkEaLPWWK/cSywZq32yyY39JRxfItV8tC8h4lYdt+E2laDQepRKmaRSHpVGuGH/hP4euDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QU3NJwRy; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QU3NJwRy" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60cd041665bso41305297b3.0 for ; Fri, 29 Mar 2024 12:20:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711740006; x=1712344806; 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=FkSWwqGbj+QCOVR1P3G4xOB1/rPb4gIDmJKvBOGCrXA=; b=QU3NJwRybjqXzCYezjqWnaN+t0fdxlBMgCpZjJUEuNelQa84pxBvM58beFG7K2WSn0 4854hiO4P04GhtpV5iFfwBhYeHBgmjetpKfQ2sl2LubyR8hkRxZVv1PpgHyL+QTbOXl2 b366+47OrM4Z5i38y11VueaEdrDJqNFPQh2dpx3hWotWEXwUKiuHyXr7bYwJWkEp87cN Iog8FopDeJCfTzGOChmWvczWCmApcvZI5CsX8rrOz8afy9+gZI8bIeqdTnNlq39xvJlP /M7ahqjAZ5vgqWHWp3mbJi/jRHIL0ZkJU/gSqR5TSeEI3N7cLIZ1BcjlxWEKHpCoH5Zl 9jIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711740006; x=1712344806; 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=FkSWwqGbj+QCOVR1P3G4xOB1/rPb4gIDmJKvBOGCrXA=; b=izWAsGM7r0YW5VfNFWbs+jQmM41UAB0DVY67nqXdh+Pz3iBrwptfFq5wUjIJhF/cOT tq2HPl4kQb4YOYUe/IcmoiDThv0/SP2eZbk3c7YEk/EdiJ9Pwcf7Eet1K89LRphYpYt1 v/OLJsqWpTA2cN0Igc/3qmuuRHlhohJKW2xvjFxhwRvCDaXHp9bw20BGXvocxjNq/4Mk Qbf/kQHzGyW3fI7tu4T3lwF2to1MHdJQPFuT5yBzqhdRi6XXME4SGg5/V3tb4obI7JM8 2v3NvZtbZErDEII9e+i7zvG7awg8gGMPY+xrIBQ/kraH6g548Hr9FHBtJMjQkSWA6a5e xSCQ== X-Gm-Message-State: AOJu0Yzwr7AJr9XkS7coG+Ia5dsayEt+VXIL0DqkldNABWewZbWxT93U CTbVPZmpEGOnFNTCineAvwfNoBS32oEHx376hCkO7c7gjk9Y40oZnFubNAo4YFp4pUB3dOC5upE kqYsVxf0cg49KTYTFeKUjPl8ns7vt5SrF9piqC2+AR0cNK5fH2+zsJbDtmhKPkJQ0MuKJclMw0O A1r6MMfZeasmG01pB74ca7M4g= X-Google-Smtp-Source: AGHT+IF/GfMFUr6lTZdhS+GecnaHBHcrL3gjPRHllZpNaVHqr//EV6xxrTUrUCkJIiy3YFWYNB9OV+hYhQ== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a05:690c:6306:b0:614:edb:ac22 with SMTP id ho6-20020a05690c630600b006140edbac22mr866644ywb.2.1711740006076; Fri, 29 Mar 2024 12:20:06 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:52 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-8-jrife@google.com> Subject: [PATCH v1 bpf-next 7/8] selftests/bpf: Add sock_addr_kern prog_test From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net The sock_addr_kern test program leverages the sock_addr_testmod kernel module to test the interaction between kernel socket operations and BPF sockaddr hooks. It focuses on operations that may modify an input address, namely connect, bind, and sendmsg to add regression test coverage for - commit 0bdf399342c5("net: Avoid address overwrite in kernel_connect") - commit 86a7e0b69bd5("net: prevent rewrite of msg_name in sock_sendmsg()") - commit c889a99a21bf("net: prevent address rewrite in kernel_bind()") with some additional sanity checks in place to make sure kernel_getpeername() and kernel_getsockname() work as expected. It provides coverage for IPv4, IPv6, and the recently added Unix sockaddr hooks. Signed-off-by: Jordan Rife --- tools/testing/selftests/bpf/Makefile | 1 + .../selftests/bpf/prog_tests/sock_addr_kern.c | 631 ++++++++++++++++++ 2 files changed, 632 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index ccc1c11559a45..58c9ba6eb4e7f 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -654,6 +654,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/uprobe_multi \ ima_setup.sh \ verify_sig_setup.sh \ + test_sock_addr.sh \ $(wildcard progs/btf_dump_test_case_*.c) \ $(wildcard progs/*.bpf.o) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE diff --git a/tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c b/tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c new file mode 100644 index 0000000000000..50959b142c8eb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c @@ -0,0 +1,631 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Google LLC. */ +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "test_progs.h" + +#include "cgroup_helpers.h" +#include "testing_helpers.h" +#include "bpf_util.h" +#include "network_helpers.h" +#include "sock_addr_helpers.h" + +#define BIND 0 +#define CONNECT 1 +#define SENDMSG 2 + +struct sock_addr_kern_test; + +typedef int (*load_fn)(const struct sock_addr_kern_test *test); + +struct sock_addr_kern_test { + const char *descr; + /* BPF prog properties */ + load_fn loadfn; + enum bpf_attach_type attach_type; + /* Socket properties */ + int socket_family; + int socket_type; + /* IP:port pairs for BPF prog to override */ + const char *requested_ip; + unsigned short requested_port; + const char *expected_ip; + unsigned short expected_port; + const char *expected_src_ip; +}; + +static int ld_path(const struct sock_addr_kern_test *test, const char *path) +{ + return load_path(path, test->attach_type, false); +} + +static int bind4_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, BIND4_PROG_PATH); +} + +static int bind6_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, BIND6_PROG_PATH); +} + +static int connect4_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, CONNECT4_PROG_PATH); +} + +static int connect6_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, CONNECT6_PROG_PATH); +} + +static int connect_unix_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, CONNECTUN_PROG_PATH); +} + +static int sendmsg4_rw_c_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, SENDMSG4_PROG_PATH); +} + +static int sendmsg6_rw_c_prog_load(const struct sock_addr_kern_test *test) +{ + return ld_path(test, SENDMSG6_PROG_PATH); +} + +static struct sock_addr_kern_test tests[] = { + /* bind */ + { + "bind4: ensure that kernel_bind does not overwrite the address (TCP)", + bind4_prog_load, + BPF_CGROUP_INET4_BIND, + AF_INET, + SOCK_STREAM, + SERV4_IP, + SERV4_PORT, + SERV4_REWRITE_IP, + SERV4_REWRITE_PORT, + }, + { + "bind4: ensure that kernel_bind does not overwrite the address (UDP)", + bind4_prog_load, + BPF_CGROUP_INET4_BIND, + AF_INET, + SOCK_DGRAM, + SERV4_IP, + SERV4_PORT, + SERV4_REWRITE_IP, + SERV4_REWRITE_PORT, + }, + { + "bind6: ensure that kernel_bind does not overwrite the address (TCP)", + bind6_prog_load, + BPF_CGROUP_INET6_BIND, + AF_INET6, + SOCK_STREAM, + SERV6_IP, + SERV6_PORT, + SERV6_REWRITE_IP, + SERV6_REWRITE_PORT, + }, + { + "bind6: ensure that kernel_bind does not overwrite the address (UDP)", + bind6_prog_load, + BPF_CGROUP_INET6_BIND, + AF_INET6, + SOCK_DGRAM, + SERV6_IP, + SERV6_PORT, + SERV6_REWRITE_IP, + SERV6_REWRITE_PORT, + }, + + /* connect */ + { + "connect4: ensure that kernel_connect does not overwrite the address (TCP)", + connect4_prog_load, + BPF_CGROUP_INET4_CONNECT, + AF_INET, + SOCK_STREAM, + SERV4_IP, + SERV4_PORT, + SERV4_REWRITE_IP, + SERV4_REWRITE_PORT, + SRC4_REWRITE_IP, + }, + { + "connect4: ensure that kernel_connect does not overwrite the address (UDP)", + connect4_prog_load, + BPF_CGROUP_INET4_CONNECT, + AF_INET, + SOCK_DGRAM, + SERV4_IP, + SERV4_PORT, + SERV4_REWRITE_IP, + SERV4_REWRITE_PORT, + SRC4_REWRITE_IP, + }, + { + "connect6: ensure that kernel_connect does not overwrite the address (TCP)", + connect6_prog_load, + BPF_CGROUP_INET6_CONNECT, + AF_INET6, + SOCK_STREAM, + SERV6_IP, + SERV6_PORT, + SERV6_REWRITE_IP, + SERV6_REWRITE_PORT, + SRC6_REWRITE_IP, + }, + { + "connect6: ensure that kernel_connect does not overwrite the address (UDP)", + connect6_prog_load, + BPF_CGROUP_INET6_CONNECT, + AF_INET6, + SOCK_DGRAM, + SERV6_IP, + SERV6_PORT, + SERV6_REWRITE_IP, + SERV6_REWRITE_PORT, + SRC6_REWRITE_IP, + }, + { + "connect_unix: ensure that kernel_connect does not overwrite the address", + connect_unix_prog_load, + BPF_CGROUP_UNIX_CONNECT, + AF_UNIX, + SOCK_STREAM, + SERVUN_ADDRESS, + 0, + SERVUN_REWRITE_ADDRESS, + 0, + NULL, + }, + + /* sendmsg */ + { + "sendmsg4: ensure that kernel_sendmsg does not overwrite the address (UDP)", + sendmsg4_rw_c_prog_load, + BPF_CGROUP_UDP4_SENDMSG, + AF_INET, + SOCK_DGRAM, + SERV4_IP, + SERV4_PORT, + SERV4_REWRITE_IP, + SERV4_REWRITE_PORT, + SRC4_REWRITE_IP, + }, + { + "sendmsg6: ensure that kernel_sendmsg does not overwrite the address (UDP)", + sendmsg6_rw_c_prog_load, + BPF_CGROUP_UDP6_SENDMSG, + AF_INET6, + SOCK_DGRAM, + SERV6_IP, + SERV6_PORT, + SERV6_REWRITE_IP, + SERV6_REWRITE_PORT, + SRC6_REWRITE_IP, + }, + { + "sendmsg_unix: ensure that kernel_sendmsg does not overwrite the address", + connect_unix_prog_load, + BPF_CGROUP_UNIX_SENDMSG, + AF_UNIX, + SOCK_DGRAM, + SERVUN_ADDRESS, + 0, + SERVUN_REWRITE_ADDRESS, + 0, + NULL, + }, +}; + +struct sock_addr_testmod_results { + bool success; + struct sockaddr_storage addr; + struct sockaddr_storage sock_name; + struct sockaddr_storage peer_name; +}; + +static int load_mod(const struct sock_addr_kern_test *test, int op) +{ + char params_str[512]; + + if (sprintf(params_str, "ip=%s port=%hu af=%d type=%d op=%d", + test->requested_ip, test->requested_port, + test->socket_family, test->socket_type, op) < 0) + return -1; + + if (load_bpf_sock_addr_testmod(params_str, false)) + return -1; + + return 0; +} + +static int unload_mod(void) +{ + return unload_bpf_sock_addr_testmod(false); +} + +static int read_result(const char *path, void *val, size_t len) +{ + FILE *f; + int err; + + f = fopen(path, "r"); + if (!f) + goto err; + + err = fread(val, 1, len, f); + if (err != len) + goto err; + + err = 0; + goto out; + +err: + err = -1; +out: + if (f) + fclose(f); + + return err; +} + +static int read_mod_results(struct sock_addr_testmod_results *results) +{ + char success[2]; + int err; + + if (read_result("/sys/kernel/debug/sock_addr_testmod/success", success, + sizeof(success))) + goto err; + + switch (success[0]) { + case 'N': + results->success = false; + break; + case 'Y': + results->success = true; + break; + default: + goto err; + } + + if (read_result("/sys/kernel/debug/sock_addr_testmod/addr", + &results->addr, sizeof(results->addr))) + goto err; + + if (read_result("/sys/kernel/debug/sock_addr_testmod/sock_name", + &results->sock_name, sizeof(results->sock_name))) + goto err; + + if (read_result("/sys/kernel/debug/sock_addr_testmod/peer_name", + &results->peer_name, sizeof(results->peer_name))) + goto err; + + err = 0; + goto out; +err: + err = -1; +out: + return err; +} + +static int run_mod_test(const struct sock_addr_kern_test *test, int op, + struct sock_addr_testmod_results *results) +{ + int err; + + if (!ASSERT_OK(load_mod(test, op), "load_mod")) + goto err; + + if (!ASSERT_OK(read_mod_results(results), "read_mod_results")) + goto err; + + err = 0; + goto out; +err: + err = -1; +out: + if (!ASSERT_OK(unload_mod(), "unload_mod")) + err = -1; + + return err; +} + +static const char *ntop(int af, const struct sockaddr_storage *addr, char *buf, + size_t buf_len) +{ + char ip_buf[256]; + struct sockaddr_in6 *sin6; + struct sockaddr_in *sin; + unsigned short port; + + switch (af) { + case AF_INET: + sin = (struct sockaddr_in *)addr; + port = ntohs(sin->sin_port); + + if (!inet_ntop(AF_INET, &sin->sin_addr, ip_buf, sizeof(ip_buf))) + goto err; + + break; + case AF_INET6: + sin6 = (struct sockaddr_in6 *)addr; + port = ntohs(sin6->sin6_port); + + if (!inet_ntop(AF_INET6, &sin6->sin6_addr, ip_buf, + sizeof(ip_buf))) + goto err; + + break; + case AF_UNIX: + strcpy(buf, ((struct sockaddr_un *)addr)->sun_path + 1); + goto out; + default: + goto err; + } + + sprintf(buf, "%s:%d", ip_buf, port); + + goto out; +err: + buf = NULL; +out: + return buf; +} + +static void assert_addr_eq(const char *name, int af, + const struct sockaddr_storage *expected, + const struct sockaddr_storage *got, int cmp_port) +{ + int ret = cmp_addr(expected, 0, got, 0, cmp_port); + char expected_buf[100]; + char got_buf[100]; + int duration = 0; + + CHECK(ret, name, "(expected=%s, got=%s)\n", + ntop(af, expected, expected_buf, sizeof(expected_buf)), + ntop(af, got, got_buf, sizeof(got_buf))); +} + +static void test_kernel_bind(const struct sock_addr_kern_test *test) +{ + struct sock_addr_testmod_results results; + struct sockaddr_storage requested_addr; + struct sockaddr_storage expected_addr; + socklen_t addr_len; + int clientfd = -1; + + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->requested_ip, + test->requested_port, + &requested_addr, NULL), + "make_requested_addr")) + goto cleanup; + + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->expected_ip, + test->expected_port, + &expected_addr, &addr_len), + "make_expected_addr")) + goto cleanup; + + if (!ASSERT_OK(load_mod(test, BIND), "load_mod")) + goto cleanup; + + /* Try to connect to server just in case */ + clientfd = connect_to_addr(&expected_addr, addr_len, test->socket_type); + if (!ASSERT_GT(clientfd, 0, "connect_to_addr")) + goto cleanup; + + if (!ASSERT_OK(read_mod_results(&results), "read_mod_results")) + goto cleanup; + + if (!ASSERT_TRUE(results.success, "results_success")) + goto cleanup; + + assert_addr_eq("addr", test->socket_family, &requested_addr, + &results.addr, 1); + assert_addr_eq("sock_name", test->socket_family, &expected_addr, + &results.sock_name, 1); + +cleanup: + ASSERT_OK(unload_mod(), "unload_mod"); +} + +static void test_kernel_connect(const struct sock_addr_kern_test *test) +{ + struct sockaddr_storage expected_src_addr; + struct sock_addr_testmod_results results; + struct sockaddr_storage requested_addr; + struct sockaddr_storage expected_addr; + int servfd = -1; + + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->requested_ip, + test->requested_port, + &requested_addr, NULL), + "make_requested_addr")) + goto cleanup; + + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->expected_ip, + test->expected_port, + &expected_addr, NULL), + "make_expected_addr")) + goto cleanup; + + if (test->expected_src_ip) + if (!ASSERT_OK(make_sockaddr(test->socket_family, + test->expected_src_ip, 0, + &expected_src_addr, NULL), + "make_expected_src_addr")) + goto cleanup; + + /* Prepare server to connect to */ + servfd = start_server(test->socket_family, test->socket_type, + test->expected_ip, test->expected_port, 0); + if (!ASSERT_GT(servfd, 0, "start_server")) + goto cleanup; + + if (!ASSERT_OK(run_mod_test(test, CONNECT, &results), "run_mod_test")) + goto cleanup; + + if (!ASSERT_TRUE(results.success, "results_success")) + goto cleanup; + + assert_addr_eq("addr", test->socket_family, &requested_addr, + &results.addr, 1); + if (test->expected_src_ip) + assert_addr_eq("source_addr", test->socket_family, &expected_src_addr, + &results.sock_name, 0); + assert_addr_eq("peer_name", test->socket_family, &expected_addr, + &results.peer_name, 1); + +cleanup: + if (servfd > 0) + close(servfd); +} + +static void test_kernel_sendmsg(const struct sock_addr_kern_test *test) +{ + struct sock_addr_testmod_results results; + struct sockaddr_storage expected_addr; + struct sockaddr_storage sendmsg_addr; + struct sockaddr_storage recvmsg_addr; + int servfd = -1; + + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->requested_ip, + test->requested_port, + &sendmsg_addr, NULL), + "make_requested_addr")) + goto cleanup; + + if (test->expected_src_ip) + if (!ASSERT_OK(make_sockaddr(test->socket_family, test->expected_src_ip, + 0, &expected_addr, NULL), + "make_expected_src_addr")) + goto cleanup; + + /* Prepare server to sendmsg to */ + servfd = start_server(test->socket_family, test->socket_type, + test->expected_ip, test->expected_port, 0); + if (!ASSERT_GT(servfd, 0, "start_server")) + goto cleanup; + + if (!ASSERT_OK(run_mod_test(test, SENDMSG, &results), "run_mod_test")) + goto cleanup; + + if (!ASSERT_TRUE(results.success, "results_success")) + goto cleanup; + + assert_addr_eq("msg_name", test->socket_family, &sendmsg_addr, + &results.addr, 1); + + if (!ASSERT_GT(recvmsg_from_client(servfd, &recvmsg_addr), 0, + "recvmsg_from_client")) + goto cleanup; + + if (test->expected_src_ip) + assert_addr_eq("source_addr", test->socket_family, &recvmsg_addr, + &expected_addr, 0); + +cleanup: + if (servfd > 0) + close(servfd); +} + +static void run_test_case(int cgfd, const struct sock_addr_kern_test *test) +{ + int progfd = -1; + + progfd = test->loadfn(test); + if (!ASSERT_GE(progfd, 0, "loadfn")) + goto cleanup; + + if (!ASSERT_OK(bpf_prog_attach(progfd, cgfd, test->attach_type, + BPF_F_ALLOW_OVERRIDE), "bpf_prog_attach")) + goto cleanup; + + switch (test->attach_type) { + case BPF_CGROUP_INET4_BIND: + case BPF_CGROUP_INET6_BIND: + test_kernel_bind(test); + break; + case BPF_CGROUP_INET4_CONNECT: + case BPF_CGROUP_INET6_CONNECT: + case BPF_CGROUP_UNIX_CONNECT: + test_kernel_connect(test); + break; + case BPF_CGROUP_UDP4_SENDMSG: + case BPF_CGROUP_UDP6_SENDMSG: + case BPF_CGROUP_UNIX_SENDMSG: + test_kernel_sendmsg(test); + break; + default: + ASSERT_FAIL("attach_type not valid: %d", test->attach_type); + } + +cleanup: + /* Detaching w/o checking return code: best effort attempt. */ + if (progfd != -1) { + bpf_prog_detach(cgfd, test->attach_type); + close(progfd); + } +} + +static void run_tests(int cgfd) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(tests); ++i) { + if (!test__start_subtest(tests[i].descr)) + continue; + + run_test_case(cgfd, &tests[i]); + } +} + +static int setup_test_env(void) +{ + return system("./test_sock_addr.sh setup"); +} + +static int cleanup_test_env(void) +{ + return system("./test_sock_addr.sh cleanup"); +} + +void test_sock_addr_kern(void) +{ + int cgfd = -1; + + if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_environment")) + goto cleanup; + + if (!ASSERT_OK(setup_test_env(), "setup_test_env")) + goto cleanup; + + /* Attach programs to root cgroup so they interact with kernel socket + * operations. + */ + cgfd = get_root_cgroup(); + if (!ASSERT_GE(cgfd, 0, "get_root_cgroup")) + goto cleanup; + + run_tests(cgfd); +cleanup: + if (cgfd >= 0) + close(cgfd); + cleanup_cgroup_environment(); + cleanup_test_env(); +} From patchwork Fri Mar 29 19:18:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jordan Rife X-Patchwork-Id: 13610978 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 96DAC38DD3 for ; Fri, 29 Mar 2024 19:20:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740010; cv=none; b=pau1RSNLQZKaDc7mShumOk74vN2tWPZ3aJiAbxR8o+mEjDgN5lqZmLclkf09ku30/oAyQ4HiqqVaa/yB1lDmT6BTUEIe0qXZLahxGpcLWQEVxdixZKotrPChBB67Vv639vq8Enk8DffuGRzBi6eKpd4DnkDbv0E5Bkwj1VxTvhw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740010; c=relaxed/simple; bh=VbGG+XbpZK7oUBxGNtmxZbTCfs8JzgG6wNjJv0uXmO4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TZpk3Em0fmpUH4wVKYj/bmK6+ZJFvRjxPfJ4XQlN4PvuSiZrtm3RsocpTVWSTUgU9C1Ve6tRgT+H1xN5jjkm7EzvRxK27n4rBPpZpn6kXBLOQZhgn2mUHRTgkbzBgbBp5LvF3gEunWa8YYzjUkdbbVk6u/JnzpN0XMMxGDWnwV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DbaUhS4E; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jrife.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DbaUhS4E" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-60ab69a9e6fso40757997b3.0 for ; Fri, 29 Mar 2024 12:20:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711740007; x=1712344807; 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=Q+6H8V8tRjJIRYzVICNw6xqfrh4PmImpo3mdIUVEfq4=; b=DbaUhS4EE5ixNMLrH50oWLQPzywAOF2/kCRuJGFV9n9ZJFcHnMV3RtGNnsG1+Sz2bZ EWy6vA+Jms/7I2drS36pY/DqR2gRxeECecf9NbJp6sSkxIs6+4NfUsUhnBt68oC8QgxE v1ankrbsNa9qdDw0RBNWvSUGiHJdxwWXy3FD5DeFDDs6C4f8SKUxFexV0X6Qv2lStDW3 OntZlLvIs1kT0uRs4Wk5csfmdReI++dohLNIypnMrDNCdRWiHNGy2hC0Zh9JU1C4WU5z strvYzx0Yaqf3DR9JUawLdGBJmf3et/NnAendnLT3y9PIoPYE/7qLVeP9LNoFD1HGbyw KPDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711740007; x=1712344807; 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=Q+6H8V8tRjJIRYzVICNw6xqfrh4PmImpo3mdIUVEfq4=; b=ASamKKFgn2RJE6latIbiZd2tPB/4Y2M8gYBhxJ5cFi84YnoawjTQbfoGgjeueX42Hb EQIqVRckC2oaShzpbzLHxbbcucCYYAEOH/1SWaGiTWtCyOnPbELVTSDl26empW27WIlq tEGuLvzwccW43g0aKzDzuHzq78A/V4QovTNmGdpTbf1ubW5d3pexbYY1l11ZeXjyIxv8 97KYB+u7ewu5j5TVlfevXCb4s1SLUf4LP5Ngy6JvVyvYU6gknid05HTR58CoaCfcx6Io Do33We7e6J6BhQkCFJcBTlDWRed3LGhHCMGFMR+mFyAn3MjzzGHcyCIET0ZxDz+l96j4 HXRA== X-Gm-Message-State: AOJu0Yx0Wn7BlFXf/qdtGPmhr+LnAyv8dKaDBvAKB6+SZaJZ+manOpMV C84bUrQVvPHPzhLktXVnyyOM4EQQfM8hHJta5Ti58lwj6dcBFtdfncIY2dLVd917QmSjMROXy/R zLpHEoQQSSWUkSTMc4o2JLALERf0BPI1xTBfPvQYSDRd25VP86vKh4sin+0nswGr/Ic5CaeN+eg dKB3w1WWHKI1zDGdqcm4BVKIw= X-Google-Smtp-Source: AGHT+IHwcUgirIdazUL6RxU7j2gI/AR6RRo+4nJUn64Ts9RX9y8sDHkirAedhp2lZjPuqpBRH6ZjDSYGog== X-Received: from jrife.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:9f]) (user=jrife job=sendgmr) by 2002:a0d:ccd3:0:b0:614:6883:43bd with SMTP id o202-20020a0dccd3000000b00614688343bdmr149348ywd.1.1711740007486; Fri, 29 Mar 2024 12:20:07 -0700 (PDT) Date: Fri, 29 Mar 2024 14:18:53 -0500 In-Reply-To: <20240329191907.1808635-1-jrife@google.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240329191907.1808635-1-jrife@google.com> X-Mailer: git-send-email 2.44.0.478.gd926399ef9-goog Message-ID: <20240329191907.1808635-9-jrife@google.com> Subject: [PATCH v1 bpf-next 8/8] selftests/bpf: Fix bind program for big endian systems From: Jordan Rife To: bpf@vger.kernel.org Cc: Jordan Rife , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , Daan De Meyer X-Patchwork-Delegate: bpf@iogearbox.net Without this fix, the bind4 and bind6 programs will reject bind attempts on big endian systems. This patch ensures that CI tests pass for the s390x architecture. Signed-off-by: Jordan Rife --- .../testing/selftests/bpf/progs/bind4_prog.c | 18 ++++++++++-------- .../testing/selftests/bpf/progs/bind6_prog.c | 18 ++++++++++-------- tools/testing/selftests/bpf/progs/bind_prog.h | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bind_prog.h diff --git a/tools/testing/selftests/bpf/progs/bind4_prog.c b/tools/testing/selftests/bpf/progs/bind4_prog.c index a487f60b73ac4..2bc052ecb6eef 100644 --- a/tools/testing/selftests/bpf/progs/bind4_prog.c +++ b/tools/testing/selftests/bpf/progs/bind4_prog.c @@ -12,6 +12,8 @@ #include #include +#include "bind_prog.h" + #define SERV4_IP 0xc0a801feU /* 192.168.1.254 */ #define SERV4_PORT 4040 #define SERV4_REWRITE_IP 0x7f000001U /* 127.0.0.1 */ @@ -118,23 +120,23 @@ int bind_v4_prog(struct bpf_sock_addr *ctx) // u8 narrow loads: user_ip4 = 0; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[0] << 0; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[1] << 8; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[2] << 16; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[3] << 24; + user_ip4 |= load_byte_ntoh(ctx->user_ip4, 0, sizeof(user_ip4)); + user_ip4 |= load_byte_ntoh(ctx->user_ip4, 1, sizeof(user_ip4)); + user_ip4 |= load_byte_ntoh(ctx->user_ip4, 2, sizeof(user_ip4)); + user_ip4 |= load_byte_ntoh(ctx->user_ip4, 3, sizeof(user_ip4)); if (ctx->user_ip4 != user_ip4) return 0; user_port = 0; - user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0; - user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8; + user_port |= load_byte_ntoh(ctx->user_port, 0, sizeof(user_port)); + user_port |= load_byte_ntoh(ctx->user_port, 1, sizeof(user_port)); if (ctx->user_port != user_port) return 0; // u16 narrow loads: user_ip4 = 0; - user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[0] << 0; - user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[1] << 16; + user_ip4 |= load_word_ntoh(ctx->user_ip4, 0, sizeof(user_ip4)); + user_ip4 |= load_word_ntoh(ctx->user_ip4, 1, sizeof(user_ip4)); if (ctx->user_ip4 != user_ip4) return 0; diff --git a/tools/testing/selftests/bpf/progs/bind6_prog.c b/tools/testing/selftests/bpf/progs/bind6_prog.c index d62cd9e9cf0ea..194583e3375bf 100644 --- a/tools/testing/selftests/bpf/progs/bind6_prog.c +++ b/tools/testing/selftests/bpf/progs/bind6_prog.c @@ -12,6 +12,8 @@ #include #include +#include "bind_prog.h" + #define SERV6_IP_0 0xfaceb00c /* face:b00c:1234:5678::abcd */ #define SERV6_IP_1 0x12345678 #define SERV6_IP_2 0x00000000 @@ -129,25 +131,25 @@ int bind_v6_prog(struct bpf_sock_addr *ctx) // u8 narrow loads: for (i = 0; i < 4; i++) { user_ip6 = 0; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0] << 0; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1] << 8; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2] << 16; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3] << 24; + user_ip6 |= load_byte_ntoh(ctx->user_ip6[i], 0, sizeof(user_ip6)); + user_ip6 |= load_byte_ntoh(ctx->user_ip6[i], 1, sizeof(user_ip6)); + user_ip6 |= load_byte_ntoh(ctx->user_ip6[i], 2, sizeof(user_ip6)); + user_ip6 |= load_byte_ntoh(ctx->user_ip6[i], 3, sizeof(user_ip6)); if (ctx->user_ip6[i] != user_ip6) return 0; } user_port = 0; - user_port |= ((volatile __u8 *)&ctx->user_port)[0] << 0; - user_port |= ((volatile __u8 *)&ctx->user_port)[1] << 8; + user_port |= load_byte_ntoh(ctx->user_port, 0, sizeof(user_port)); + user_port |= load_byte_ntoh(ctx->user_port, 1, sizeof(user_port)); if (ctx->user_port != user_port) return 0; // u16 narrow loads: for (i = 0; i < 4; i++) { user_ip6 = 0; - user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0] << 0; - user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1] << 16; + user_ip6 |= load_word_ntoh(ctx->user_ip6[i], 0, sizeof(user_ip6)); + user_ip6 |= load_word_ntoh(ctx->user_ip6[i], 1, sizeof(user_ip6)); if (ctx->user_ip6[i] != user_ip6) return 0; } diff --git a/tools/testing/selftests/bpf/progs/bind_prog.h b/tools/testing/selftests/bpf/progs/bind_prog.h new file mode 100644 index 0000000000000..0fdc466aec346 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bind_prog.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BIND_PROG_H__ +#define __BIND_PROG_H__ + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define load_byte_ntoh(src, b, s) \ + (((volatile __u8 *)&(src))[b] << 8 * b) +#define load_word_ntoh(src, w, s) \ + (((volatile __u16 *)&(src))[w] << 16 * w) +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define load_byte_ntoh(src, b, s) \ + (((volatile __u8 *)&(src))[(b) + (sizeof(src) - (s))] << 8 * ((s) - (b) - 1)) +#define load_word_ntoh(src, w, s) \ + (((volatile __u16 *)&(src))[w] << 16 * (((s) / 2) - (w) - 1)) +#else +# error "Fix your compiler's __BYTE_ORDER__?!" +#endif + +#endif