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: 784648 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 F19A0131E5C for ; Fri, 29 Mar 2024 19:19:56 +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=1711739998; cv=none; b=SwK5IdTMGCecaQ8sF0rZTlp1Y2tYvFTIP+hPZnmtKLqyD1AnCOSeiGrJJPjXBKKbkWP7xejHL7OjbfngsmxC2kkiKYS49ADri0cZRavKIGv07Cf8WkRZRmRPMzCpyX8aBw6nH4/qO/AiDIjkyVLLO21U661sq8sxbcYNg2zF6OI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711739998; c=relaxed/simple; bh=ruM1fSImM8HKEje7JvZtgcNqeHH5/+wlG2CCls1QZT0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tgYLEzMbs/JNhj27XP4bYloYlzo84It+COzDUlydZVKYxxGcK6NeQ1CR1qiPRq2iS+mqvKlzsE0DpV2q7CHoINuH2Te7tNlY02HWm5PjAcYWnK77nbksbwDOiDqEBMNz1LbVdW0624zl5LHZhxDhpy7N6CpMffO/Ae/bstqPXuw= 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.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="TrgfKHO0" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcdc3db67f0so3902243276.1 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=urnveZlaHPtNKk3i5R/LlkVoI/8eeeS7lMJGihOyAsSFgt2M1txboKz22RWM12Qcr+ hKoL4QpaYFe1jIv3XcRIoVjsYVuliRew2yhZnCAdNetFs3B2fKev63WAdX9TpYdfZUd0 Z55HZyHaZHmtEGBuaTdWfQFKP2d9Cu6Kj7TdkEhi930JCvszEaurZyhxKyzwtI6cWEfF 0qDGGHl1ZdEFqS775AADgbGFM3w2xbAab17s9ZkjBNRzQ6WtLfZ48jiClIYcQZW4K70X OrXUoUV+qFmWGNNKk6pFPLkiaCJJ16LqaoD1t4bSnFk55675kDJ0xPh5HQ5wHHiGk9ss VgQw== X-Forwarded-Encrypted: i=1; AJvYcCVSK8eNEWA11d0tLiddg7IzzWJmNR7dd1Td5r8CyVHjdru2N758ttp3DO+9A7NKvDHOeASRLc8lniaCh2RVLXjzWDzfky2Slxytsa/CCfds X-Gm-Message-State: AOJu0YxqrY9X7alehyonAjGvu0V74I+a4Z/GzFoaKIGKObWkY0E0Lx1m HstIFv4znqkH3OGIAGILVuyTsVpsO59UyvAScYKtqHZQKjRCGh4HYyDWdy609G+w4ybyW2tu0Q= = 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: linux-kselftest@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 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: 784281 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 A34A086130 for ; Fri, 29 Mar 2024 19:19:58 +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=1711740000; cv=none; b=muzocmc4jyS93OtflEkEtCJeXRX2PbWRHhXt1itk+Z7fEay3MqnqCz2y9Xkl+jgNIYpV+wSKhd2fNFT0ea7OI7OHoNCgd5ezMp0zzZExQslVmRfwNXXyUxdQYZL1RIpPFzc+t51hzMqcghYBE4jOBSLlJc3WV01M9GMJV8sV77Y= 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=GA6bqvrv; 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="GA6bqvrv" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-613f563def5so35463927b3.1 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=1711739997; x=1712344797; 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=GA6bqvrvlkpPAGnwuc8AHGv7kzB4KukeBwtnrYf4DXNf1IMSqcZkE3Ry6ABidbKQ37 1FgQllKtYaoMM0tMUqkCSY1BtegKF5LPs+Y8QpbB8v59AUa646NDR+DzlXRHYCt60nqb ImGBXmkPQLK5kahMXZ+EHdE9x8nw5VobxUQ96bRFVgmkaagk5ldE1mU/VTJA9jPJ+AK+ y735ofqcNYjJdPc3AfrWKMKZRhCz+Mj7W/Am26Atp7yTeHHF17rKJDuKkU6SC3nlGQ/9 6R4bjjxyqsgEnt00Taq/lQfUoQj7mbBdifZXzERz+fPgQTwERkTMJTH+64A4FyK5lslV stAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711739997; x=1712344797; 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=nCIV4+Ys+tBU4nq0fZBafYaEsQ1sl2dio9QTjjn4u23AMsWVvPipKKYIjoUGbWmBDb I3XbPiGMjn2byZRyWzgPcX7SmW9wOBUHGq0bjymz7pNee03G2iuEA3eSxbEXjQi+NDFD 72Lp1WgJHQt8rNX0bHp143Sp3xjGEu+SsqSmFECanwnYIUwRu9z8TUOD3v2T3LSf/HDv XFTVn2y6UqJH9VOFP8NvhhQzqoFOjio9ncAl5ahviZMAXoujgLAddUMHB6VyUKDmvzmN XE5Z+ZlgmBi5kE8ugRrzl5rtKSAwxcj5uhbaLP59ROX0RThqSKUkq4CZXwP2V14wssRM 4bOg== X-Forwarded-Encrypted: i=1; AJvYcCXlU9iSDm6LqS/yDBw1ejufYfQBtrbUlgo3jFNCo9x7M1uaBszQyT14DeWQqFoguLaNQGYikK4gp+FcxnzFU2sk3hnA1o3DmnoArDT4AwzA X-Gm-Message-State: AOJu0Yzu84SxNQ0SgfrkglVncvT/tCDV5FiswzGGNM6BdOnHhOXaPtLx XDEVVvpbXQ58IMLXvDK32bEaC1djker3zcnMBtcoDUJy5NMOYd1riunlEzdvHoRlaSrr4whlBg= = 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: linux-kselftest@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 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: 784647 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 60176139CFA for ; Fri, 29 Mar 2024 19:20:00 +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=1711740001; cv=none; b=jULs7QiNtZjr61916nrELkaxpdz51+jhka4P2W5yJefqkyIy/suKPyOy8YARwAhyOuir/05W1pXAi85QLbV7L7ObQmNEE6GQpf4yaUe1gAN5mwrfNo2nybHWdqCP73++ExiIHPjr88vywyB622GB8BQsCSFhYWpYb6N+XXTR1CQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740001; c=relaxed/simple; bh=X1TbuMHoleP7CfuoPSPY5DaocW44cFI3B40G/uoR0EE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JBC2p8Kbwx5iEzNok5n+OzFj09Y5+jZ+UWeerJvZky+eu46FsMOEUuaQ1K96Zmuon1VdLWy9SyehYaSg7Wrq4Q4ZPBbeFm8RPmagDQ2Z0ahM9RgAUYtQ1JjaXXm/cGh6WftKlYiNfPXPIGs7MsSlGX+xzkcPrV0KIgbIc6ebQYU= 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.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="fntEcFJt" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dd169dd4183so2703511276.3 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=Tdw7SsnEctU1nXfrPQ5nvNA226rA6hgvZ/HDR8huKuHaGKjHX8hKezASp12NiMlfMA +Xo4phpYjUnEvmIatZrsk/gLklkC4WQqdTEjZFLXwQHdgET04LgrcZF3UYWzkkKIccco es1hSAC1W+CHAk6slzpL49EjPpjWoO6wxWT6VkIow1QhABX23pAFTQglsZNLttdGocr4 NA1/8ZKBa5ZOTF9Y/upbBudqE5Hr1hfUZ8GFo5S2XULBVbJBuNyo+2l7aKCjqLd5xuxY h4jFR3hg5gj3EmYzVb5hm9xYnGvNSewZUi/HbjKER6my4zqFx7k1WnlW493d2/i6wA0Q jcfw== X-Forwarded-Encrypted: i=1; AJvYcCW7Y0hzXSkulai7s74ijw6rYxYWl2LtxPVSikYmiwC8be8XIxmqxlT0b0SrKgoGu7vEY2CEbzTxdozOlTHLRlBL1EUrvl7ew2wVvqamb29x X-Gm-Message-State: AOJu0YzooRQBChasrkiWr1h0HXfI9tTrcU7qmBQBag/qllqTRpGM1QdK AJi2oBXMwaUkeUjUBOdE7BHK3mEORuiGtGvYMhoTAJXzbjnqF5co8LxizWnL5Ye/ZrHqcPUWsA= = 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: linux-kselftest@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 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: 784280 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 16BED13A3FE for ; Fri, 29 Mar 2024 19:20:01 +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=1711740003; cv=none; b=aTF6W8zwTc4HHYunt7vTIwUMJuIuCa80Ouu4o/Qj2FGOqgl3GUSKCpaaMPHHCiT7oBIDAwi4y/o8imqlm7JCe9Vg9uES2EszwDp4hEFkFCl/ZhrWiK48svWD/H3cwFVOTrxg8FaxYIFJhL6GAEdZbEmZXxtXKDfBUPzGNg1+0cY= 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.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="TvvCJknS" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so3954326276.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=JrETTcywF57KNwHcsKStJgXvs1pqxsbqjs5O3cEiIG6TcAyimRqLZDSyOZt+EO03Ji CqFzG+BaeeUMGjKSqe3o4XCWpRtew21fAsxL4Nrm799vsho5qjOY/tIPzcD33ypop3hJ W14m/Pmkwy1SsvYmreR3WZjhdk/U4wY7iQ0c9zCU/bIukbXaMJWrcUkDJIAON1Uk0ODV eIlrXZlKgvUZ3AE52N9Q+WOy1sRlEdNcjHulzRgUa9Y8IuTBGcQuazTcYIPlZ/NOOIuU v3zRO59ig/Z9vpNJPS/nMuiPB7B3dSwdr6JUo/35zgdVDBLujVRDX8vds0lLUumcbevR ygFg== X-Forwarded-Encrypted: i=1; AJvYcCX3LBywFJMJmG6VOV9XjwNkmDfKmGkoJLoiRFEFeKrSC9Y9wA8818ezCqlSsyz90kW/953VL0arGzv01q6GW/TS3oJZhPIt+iTBEu8ATBFi X-Gm-Message-State: AOJu0YzppVm6gjz5Y1Wrlv+0RNqU3DwRrcWlCmy1bcmpwF8ZbKl1L3s7 ghxPKE0RLM2qE9loWLTjk01z+Fpdd3jZtwHgbhu29Rin/6OQBktQRd1sC6LfhwZs7fWwwopdjA= = 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: linux-kselftest@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 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: 784646 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 D89EE13A3FE 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-60ccc3cfa39so40268977b3.2 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=pEvhZqTKm7DbN2QIfKzu8cRt27lTyM7CoGi1k5oV4e+mpFXK1UpZNu7gFPMtONE7qd FQQu6tr3KoV3x1W+lPXUfeQe73wvfp478XriT1/gd0yrLVe11KY1UGqEV0ij7Eo+jlPc 8tuifQirmGbHpX3fdptho0shd6Cy3qcelYTKZJjnzRU/XSg4fOPX474N2ObMbLSoNj2G WDM6WULt589B7lez2UjfCFbr0/godi03F+bQQ5ux1L2duVyEvXRmZutJtfTk6XEt6niP NzvDUYUr5/PlxzesW64S7Epi4IVh2Bdi6Wwtmq33GlqEewqYRdEjH7q3fbfpBnHVLS9k M/Lg== X-Forwarded-Encrypted: i=1; AJvYcCWgFd2I9kdnyiYgPVYdgK2HVQWDV/W4cA0pmA9jHV+FuowtW9e++uDt2NBMJVkyvq31Sy3Rdzck4g+LCBVv3LvdYCdpaxttSfkc5pdzv7TU X-Gm-Message-State: AOJu0Yxb2NEWfluSIN50p4Drru9udJ3PvJ81ZW6pWYAUTExJ5q+/Ok3R RSGBNkwsmh5xUq7DofOBfEEGtibhNk9ZRDxENHQRU3JZLUm7yBKoUTnDJ9HgkcOpnwjza34l2g= = 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: linux-kselftest@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 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: 784279 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 686A213A263 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=1711740006; cv=none; b=B6wVmGr6SYu1wJB5WvEFkWBGxWiViZDbCoZjm/e3ukVZWzpsToZP84EALHDLfSIPGnD/j7OGq7H39XU1fw+kFHlktTIgpSVhkDGeyQIx5nnjCMkWxEAGUHxgEg603NCB9sAeU8o93NSezy5qiARmXOD4BqVaMZm6MrJBRcRb8gs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740006; c=relaxed/simple; bh=FSP1oV2T/usULlM1VYDd08bWBIJsZLOi020rBF4z6Bw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QDCa4nBGRJJENVHImTPv+SX92Ac8sV/7ez/YCb4l+CkKcvlQMT8CPqWGr8CF/wgmHC5KBh5ufwnwUvkP8bE8yXOuCBnDzpVcrd9cR7WVIhZHSKRthldeBAjiBRgUrhlvzSZJyCWJFCIgXJnrpKsXafG0M14JGDU1IpBMEa+4NiQ= 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-dc6b26ce0bbso4024208276.1 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=DrTYgd3Q30yDaczNEZUn0hDeED0zY0lNFz0/fNJhYsbDk6s2bomegxzvIH2ryvHHA5 qPN9ymW40F9DGaBTqX+J3iw+mlgClhk+nEg5NpAyRZ59fg4nhenUhV7ZyQMwKmEHCTOM AD2w9xcevdWFMwzpci43/WQdVsq7YiN7e2KEdQwAoPidUkJFvrcinBQyrk5vdE3gk8hN 6AZm+j14sjWaN+3Gn6uweuFM9cXXVBw7gnwT+mCGZdrmD+yOk1Kx0ifEoHXLYmCCd7AA uU6GmXmh+G6A+rUQHx0IUxh6mqdVcL5sHErA1jWiWnUEOO3FycGyifXfM+kyg9Qu+oI2 4hmA== X-Forwarded-Encrypted: i=1; AJvYcCX1FwT3bPt9BUFOxv6/8Bf8U/oaRC/DPMiBIFuv18JY5FW2lAXsC5SDc6ez8rVtnByi9AQMCBjNJBNfAWsF3smvMpmpSzA0gh71P4+N8E1s X-Gm-Message-State: AOJu0Yw0UmXSdPdaUSFSu150f8GGkMuLtX0sMq8g9GfNNrcGJJT817lS ZbJWj/0Ri5r3yyj+v2hkMsQ9WNEou00elxoI95lovS2H9Wsu/b/dGW2tuu2YBDo5qisWJRydTA= = 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: linux-kselftest@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 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: 784645 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 F11EF13B288 for ; Fri, 29 Mar 2024 19:20:06 +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-610b96c8ca2so41887597b3.2 for ; Fri, 29 Mar 2024 12:20:06 -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=YrtqYH1Q/S7nUNTnJlyrhyFgvXOIFXp7K+7do4BpjMeZzK9IxEmCYoRnAmsSohL6vP 6P1anIRy9TSuIc72lxZSEF88TQN9nmHz2cgcfabknKx+2yLlKXn6LxwMxMf6D5QKExn8 pxQsbUbJtkx9gMp5k1kmz1InFbHqEBFuwZEvEMwwvuKhEcom7xX3xia62ECnVo741+1K 2zAnxuHzlumzXkGUt4Btz5ttkRB5kr/hfS+MLFkezVJIR9ivyyY+joHnGXWK5vIzlVJJ OV9xF7S3raPPz2xsSB6TeSU9QPUcvNacl4OlO3dtp1dmcdWoAL68SOJYmxnGrmhYJXPv dfPg== X-Forwarded-Encrypted: i=1; AJvYcCVo1jdgzTF1Ng/DPZ7lbGucDwVS0C1tW1V18PxwbuFVUz0of8Typ/mcKQsYccST8PQEADBK4ECqnhuVlSN1Mw+e45XtQ/+6w9q1T7eLHTgm X-Gm-Message-State: AOJu0YyTohr3jyV3erSYAd9ebNW6dX5dXd9MPcNO6jKcVge3SyqqhwOr jgDPWR6yGw51vdDdWdC8TYy+fkCUVJPGiK9lQZBYMDF5DpToup0uIYgTK6hO5JGOwOOimB6Lzg= = 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: linux-kselftest@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 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: 784278 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 4690F13B59D 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=1711740009; cv=none; b=GMPe7UkrZgA9UwStsPDZqbE4SCjc27JEkGYbmaIl+bJtad7GAI4eqm6r1yK5xamHdcgNn9hqiMsZaJ9Rq8UySZ2y4MnP3ZhlT2wMBhsc4Na6cghl9MzscufTQc/KujOJjK7EFNkldbJFWomBbj+4ppqaaDU3Bh42AEI92xw3XMk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711740009; c=relaxed/simple; bh=VbGG+XbpZK7oUBxGNtmxZbTCfs8JzgG6wNjJv0uXmO4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oXpzWSD0x7avbsS1tqEexCQD5za7/uavxQM5v3HtWhGitJCoNBdmqS9U0cjbPAEdIB2FKSquQVrxSOmPPg2csFuYsG6J1ysupXDXzRaOIR0U97usZ90iWMpAT37t+hE+huVXkQp2wjHhYnMQixOCIae/Ch7xDyfMaVY12l6r9hI= 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-60a0b18e52dso27493287b3.1 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=fRwdVOYNJUMaVFrRNZMtHjbAsrTZjNNiW9ky3blQAWvG3W/XOLS7A8Pt8oNHXPb76b 5nQb5pOggFjnbPfhzxw3EC4FXu6OpWKLcFJWl35OWUuAD6GSBXPivRyBwRIBZYW5MW7s QSQUEt2s54xcQv0Zw5YieCVJhR6NB9d1yNhJBXO+mWcT+GEcTW0Ilb5aOFiDkpjG4NlF qVyilpUy1TfbQc6WZM6HIM+cg4ag1tYVs3dsuDtEHAPNzeVsByoNDK9u9pvRG2eQKwYw NiYR0/eo2jG7G0QleAyl8qbZTi6pXKuqLVqC20oO5XvVcpnikCdAOaj88wlcd1i8PgTk acfA== X-Forwarded-Encrypted: i=1; AJvYcCXihY6xfDfFc/wdBiq4QWCEFN38XvNE5SNTPTvXz4JdYXJFS4rOulcl6kIup09W7hluKOTHCl6oF3yEoSuB3fHvaUJR0vIXLeIo2rPHuNF6 X-Gm-Message-State: AOJu0Yyckvm4IEyfRDvlEleMcnA3K1xnyphcV5dWLoYLhr5GA1K/teAt JD+9twl2DKgcGGbc1fK2GB2nr9ajUrdyUk8scFW2VfcpfaYTWHU4mQ2oQ0T6zmfeiI8Dgwc5/w= = 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: linux-kselftest@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 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