From nobody Mon Oct 6 22:53:55 2025 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5F7F21B4248; Thu, 17 Jul 2025 03:29:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752722962; cv=none; b=r5NaniuBpl0p7ubNe4CPvKxwM1m723SXRX2Oak2x+r7hAkW6GFevTM83eTwhsGQ+alybsEYMF0Y5TDJYmBfMJBFgTY62THxixEkULqpAGkLq/GLl2lQayN3raOVm6289ELnIh4Z2KFxJqbZfTVs5422iJJD7LsDa92/Qq1AmcgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752722962; c=relaxed/simple; bh=z7X9KahpL9KLpBuE2Yl0f84xDfCkg84nlg5xmh8tl6M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GIi3xm95hNquNi6Pr/Y6aQk2MDvxYt2UgQ2Gv+SJ7+ylyJHdt2PF1NpaWS4W3uaXti4mgcUUxJnwBtTdV7j9GGephQKMi6B3psobgzbns9d8Yzd9Z2D/yk/fNeQ/gj33V1Yj831u1jNNNKxJ8iGX+j4bzH5PvKxtKbIyuwHzCMc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=d6eI6dCD; arc=none smtp.client-ip=220.197.31.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="d6eI6dCD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Rc 1LVsNKuf8womRfzkt5J5umHNIAJZcid8R1TPWN7LY=; b=d6eI6dCDPWjbEKQvZX oiEBl0d7Upuela/o+ZCqDUw7bBbvpQvCy2KQoOw/S3SeJqyF2je2qWwiu9FUZtv9 pbjpOkNJB/yeymdeges02lVZlqrwr05PLotjn19mkpufzm85prtKcaJ98Yr41C2a fQ6UXrAghtTiytw0q/Pz+rqEM= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wD3_4bdbXhoJsULFQ--.2953S3; Thu, 17 Jul 2025 11:28:32 +0800 (CST) From: Feng Yang To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 bpf-next 1/2] bpf: Fix macro redefined Date: Thu, 17 Jul 2025 11:28:27 +0800 Message-Id: <20250717032828.500146-2-yangfeng59949@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250717032828.500146-1-yangfeng59949@163.com> References: <20250717032828.500146-1-yangfeng59949@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wD3_4bdbXhoJsULFQ--.2953S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxCF1UGr4rtr1UCFy3GF13CFg_yoWrZr1kpr Z5C3WFyr4UXF48Ww1UJw4jvr1Y9w4ku3W5KrnFq34Fkw4Yqrs5XF1vkr1xZ3s3Kr4jg3sx XF43t390y3y8ZrUanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UprWwUUUUU= X-CM-SenderInfo: p1dqww5hqjkmqzuzqiywtou0bp/1tbiYx+NeGh4aJOpngAAsV Content-Type: text/plain; charset="utf-8" From: Feng Yang When compiling a program that include and = , (For example: make samples/bpf) the following warning will be generated: In file included from tcp_dumpstats_kern.c:7: samples/bpf/libbpf/include/bpf/bpf_helpers.h:321:9: warning: 'bpf_stream_pr= intk' macro redefined [-Wmacro-redefined] 321 | #define bpf_stream_printk(stream_id, fmt, args...) = \ | ^ include/linux/bpf.h:3626:9: note: previous definition is here 3626 | #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __V= A_ARGS__) | ^ The main reason is due to below in sample/bpf/Makefile: $(obj)/%.o: $(src)/%.c @echo " CLANG-bpf " $@ $(Q)$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(BPF_EXTRA_CFLAGS)= \ -I$(obj) -I$(srctree)/tools/testing/selftests/bpf/ \ -I$(LIBBPF_INCLUDE) $(CLANG_SYS_INCLUDES) \ -D__KERNEL__ -D__BPF_TRACING__ -Wno-unused-value -Wno-poin= ter-sign \ -D__TARGET_ARCH_$(SRCARCH) -Wno-compare-distinct-pointer-t= ypes \ -Wno-gnu-variable-sized-type-not-at-end \ -Wno-address-of-packed-member -Wno-tautological-compare \ -Wno-unknown-warning-option $(CLANG_ARCH_ARGS) \ -fno-asynchronous-unwind-tables \ -I$(srctree)/samples/bpf/ -include asm_goto_workaround.h \ -O2 -emit-llvm -Xclang -disable-llvm-passes -c $< -o - | \ $(OPT) -O2 -mtriple=3Dbpf-pc-linux | $(LLVM_DIS) | \ $(LLC) -march=3Dbpf $(LLC_FLAGS) -filetype=3Dobj -o $@ Here, some kernel data structure is needed for some particular architecture= so the initial from source to IR is compiled with native arch and after IR opt= imization is done, it is switched to bpf. So remove this line #define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARG= S__) and directly use bpf_stream_stage_printk(&ss, ...) Fixes: 21a3afc76a31 ("libbpf: Add bpf_stream_printk() macro") Signed-off-by: Feng Yang --- include/linux/bpf.h | 1 - kernel/bpf/core.c | 2 +- kernel/bpf/rqspinlock.c | 8 ++++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index bc887831eaa5..d010a0c4e374 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -3623,7 +3623,6 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *= ss, struct bpf_prog *prog, enum bpf_stream_id stream_id); int bpf_stream_stage_dump_stack(struct bpf_stream_stage *ss); =20 -#define bpf_stream_printk(ss, ...) bpf_stream_stage_printk(&ss, __VA_ARGS_= _) #define bpf_stream_dump_stack(ss) bpf_stream_stage_dump_stack(&ss) =20 #define bpf_stream_stage(ss, prog, stream_id, expr) \ diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 61613785bdd0..cc8e076f6d54 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -3178,7 +3178,7 @@ static noinline void bpf_prog_report_may_goto_violati= on(void) if (!prog) return; bpf_stream_stage(ss, prog, BPF_STDERR, ({ - bpf_stream_printk(ss, "ERROR: Timeout detected for may_goto instruction\= n"); + bpf_stream_stage_printk(&ss, "ERROR: Timeout detected for may_goto instr= uction\n"); bpf_stream_dump_stack(ss); })); #endif diff --git a/kernel/bpf/rqspinlock.c b/kernel/bpf/rqspinlock.c index 5ab354d55d82..b176a6f9e431 100644 --- a/kernel/bpf/rqspinlock.c +++ b/kernel/bpf/rqspinlock.c @@ -676,11 +676,11 @@ static void bpf_prog_report_rqspinlock_violation(cons= t char *str, void *lock, bo if (!prog) return; bpf_stream_stage(ss, prog, BPF_STDERR, ({ - bpf_stream_printk(ss, "ERROR: %s for bpf_res_spin_lock%s\n", str, irqsav= e ? "_irqsave" : ""); - bpf_stream_printk(ss, "Attempted lock =3D 0x%px\n", lock); - bpf_stream_printk(ss, "Total held locks =3D %d\n", rqh->cnt); + bpf_stream_stage_printk(&ss, "ERROR: %s for bpf_res_spin_lock%s\n", str,= irqsave ? "_irqsave" : ""); + bpf_stream_stage_printk(&ss, "Attempted lock =3D 0x%px\n", lock); + bpf_stream_stage_printk(&ss, "Total held locks =3D %d\n", rqh->cnt); for (int i =3D 0; i < min(RES_NR_HELD, rqh->cnt); i++) - bpf_stream_printk(ss, "Held lock[%2d] =3D 0x%px\n", i, rqh->locks[i]); + bpf_stream_stage_printk(&ss, "Held lock[%2d] =3D 0x%px\n", i, rqh->lock= s[i]); bpf_stream_dump_stack(ss); })); } --=20 2.43.0 From nobody Mon Oct 6 22:53:55 2025 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2BAFF1FF601; Thu, 17 Jul 2025 03:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752722968; cv=none; b=KW71FXAhcRo71kEmK+zEMgKhFgT0XhbNNNbAYt/u1olxHbhyR/VAO/WdiZXYPEd3AvvVrgdBBLf/4R6PwNpv2XFdnMKdzKTN6kJNPgBw/Z6+OqzhfkQISBI6btywuTn9tDHhVwBJmtchJRwQCkYbQJTTpMI77XK+aSc+APEuQS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752722968; c=relaxed/simple; bh=XldtMI1cI42MaOPtp5XjObeUW/TtuJzMmeGPBkeEP2k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mw7ouim6pyX/wWRy7bGfoGWrBg9N7lfOy8PkewhH8dEoNITaYawUGsVkOKF03Y8Fyu5jdyPl1/HgjMhPQ4rQgAocwk17GL1j0wv1bYmLvJ3Pe9aF9zz5Xe/LNjhCt+ZcBReuLuw/lMPpSnuiNcunneJKOzAHeK3xvasU/c0rL6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=l2mOejzP; arc=none smtp.client-ip=117.135.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="l2mOejzP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=OB w5/BaGROLyPcGHk2TkTPqt5QXfRAUqLf98jtZEi/M=; b=l2mOejzPfGyFtz2LBl WMFahHvP5EbbOaVd2U29Zoz0YQ9yLVcb4C5fV6DM/7v2NinbsHz9ljjcSQcV44KS h2zPQlxfiwlfw0ebGhhxWcVri+GF5qnvSacDy227du4t6oPQt9qqTs1/fJILHDaX GR9r4dnT2Bz4WGk9EDUt46G4o= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wD3_4bdbXhoJsULFQ--.2953S4; Thu, 17 Jul 2025 11:28:33 +0800 (CST) From: Feng Yang To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 bpf-next 2/2] samples/bpf: Delete map_in_map test Date: Thu, 17 Jul 2025 11:28:28 +0800 Message-Id: <20250717032828.500146-3-yangfeng59949@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20250717032828.500146-1-yangfeng59949@163.com> References: <20250717032828.500146-1-yangfeng59949@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wD3_4bdbXhoJsULFQ--.2953S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3KFWfXw17AryrXw1xCFWDXFb_yoWkGr1Dpa y3ArZxCrW8ZF4UGayrtw48tryY9w4UXw1DWrs7K34SyrsF9ryxJr40qFZ29Fn8JrWqvFW5 CF4av34rGFWDXFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UEJmUUUUUU= X-CM-SenderInfo: p1dqww5hqjkmqzuzqiywtou0bp/1tbipQONeGh4ajB03wAAsy Content-Type: text/plain; charset="utf-8" From: Feng Yang Test cases should be placed in selftests, and as selftests already include tests for map_in_map, this one should be d= eleted. Signed-off-by: Feng Yang --- samples/bpf/Makefile | 3 - samples/bpf/test_map_in_map.bpf.c | 172 ----------------------------- samples/bpf/test_map_in_map_user.c | 168 ---------------------------- 3 files changed, 343 deletions(-) delete mode 100644 samples/bpf/test_map_in_map.bpf.c delete mode 100644 samples/bpf/test_map_in_map_user.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 95a4fa1f1e44..b5fb72c94050 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -28,7 +28,6 @@ tprogs-y +=3D sampleip tprogs-y +=3D tc_l2_redirect tprogs-y +=3D lwt_len_hist tprogs-y +=3D xdp_tx_iptunnel -tprogs-y +=3D test_map_in_map tprogs-y +=3D per_socket_stats_example tprogs-y +=3D syscall_tp tprogs-y +=3D cpustat @@ -69,7 +68,6 @@ sampleip-objs :=3D sampleip_user.o $(TRACE_HELPERS) tc_l2_redirect-objs :=3D tc_l2_redirect_user.o lwt_len_hist-objs :=3D lwt_len_hist_user.o xdp_tx_iptunnel-objs :=3D xdp_tx_iptunnel_user.o -test_map_in_map-objs :=3D test_map_in_map_user.o per_socket_stats_example-objs :=3D cookie_uid_helper_example.o syscall_tp-objs :=3D syscall_tp_user.o cpustat-objs :=3D cpustat_user.o @@ -103,7 +101,6 @@ always-y +=3D trace_event_kern.o always-y +=3D sampleip_kern.o always-y +=3D lwt_len_hist.bpf.o always-y +=3D xdp_tx_iptunnel_kern.o -always-y +=3D test_map_in_map.bpf.o always-y +=3D tcp_synrto_kern.o always-y +=3D tcp_rwnd_kern.o always-y +=3D tcp_bufs_kern.o diff --git a/samples/bpf/test_map_in_map.bpf.c b/samples/bpf/test_map_in_ma= p.bpf.c deleted file mode 100644 index 9f030f9c4e1b..000000000000 --- a/samples/bpf/test_map_in_map.bpf.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2017 Facebook - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 2 of the GNU General Public - * License as published by the Free Software Foundation. - */ -#define KBUILD_MODNAME "foo" -#include "vmlinux.h" -#include -#include -#include -#include - -#define MAX_NR_PORTS 65536 - -#define EINVAL 22 -#define ENOENT 2 - -/* map #0 */ -struct inner_a { - __uint(type, BPF_MAP_TYPE_ARRAY); - __type(key, u32); - __type(value, int); - __uint(max_entries, MAX_NR_PORTS); -} port_a SEC(".maps"); - -/* map #1 */ -struct inner_h { - __uint(type, BPF_MAP_TYPE_HASH); - __type(key, u32); - __type(value, int); - __uint(max_entries, 1); -} port_h SEC(".maps"); - -/* map #2 */ -struct { - __uint(type, BPF_MAP_TYPE_HASH); - __type(key, u32); - __type(value, int); - __uint(max_entries, 1); -} reg_result_h SEC(".maps"); - -/* map #3 */ -struct { - __uint(type, BPF_MAP_TYPE_HASH); - __type(key, u32); - __type(value, int); - __uint(max_entries, 1); -} inline_result_h SEC(".maps"); - -/* map #4 */ /* Test case #0 */ -struct { - __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS); - __uint(max_entries, MAX_NR_PORTS); - __uint(key_size, sizeof(u32)); - __array(values, struct inner_a); /* use inner_a as inner map */ -} a_of_port_a SEC(".maps"); - -/* map #5 */ /* Test case #1 */ -struct { - __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); - __uint(max_entries, 1); - __uint(key_size, sizeof(u32)); - __array(values, struct inner_a); /* use inner_a as inner map */ -} h_of_port_a SEC(".maps"); - -/* map #6 */ /* Test case #2 */ -struct { - __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS); - __uint(max_entries, 1); - __uint(key_size, sizeof(u32)); - __array(values, struct inner_h); /* use inner_h as inner map */ -} h_of_port_h SEC(".maps"); - -static __always_inline int do_reg_lookup(void *inner_map, u32 port) -{ - int *result; - - result =3D bpf_map_lookup_elem(inner_map, &port); - return result ? *result : -ENOENT; -} - -static __always_inline int do_inline_array_lookup(void *inner_map, u32 por= t) -{ - int *result; - - if (inner_map !=3D &port_a) - return -EINVAL; - - result =3D bpf_map_lookup_elem(&port_a, &port); - return result ? *result : -ENOENT; -} - -static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port) -{ - int *result; - - if (inner_map !=3D &port_h) - return -EINVAL; - - result =3D bpf_map_lookup_elem(&port_h, &port); - return result ? *result : -ENOENT; -} - -SEC("ksyscall/connect") -int BPF_KSYSCALL(trace_sys_connect, unsigned int fd, struct sockaddr_in6 *= in6, int addrlen) -{ - u16 test_case, port, dst6[8]; - int ret, inline_ret, ret_key =3D 0; - u32 port_key; - void *outer_map, *inner_map; - bool inline_hash =3D false; - - if (addrlen !=3D sizeof(*in6)) - return 0; - - ret =3D bpf_probe_read_user(dst6, sizeof(dst6), &in6->sin6_addr); - if (ret) { - inline_ret =3D ret; - goto done; - } - - if (dst6[0] !=3D 0xdead || dst6[1] !=3D 0xbeef) - return 0; - - test_case =3D dst6[7]; - - ret =3D bpf_probe_read_user(&port, sizeof(port), &in6->sin6_port); - if (ret) { - inline_ret =3D ret; - goto done; - } - - port_key =3D port; - - ret =3D -ENOENT; - if (test_case =3D=3D 0) { - outer_map =3D &a_of_port_a; - } else if (test_case =3D=3D 1) { - outer_map =3D &h_of_port_a; - } else if (test_case =3D=3D 2) { - outer_map =3D &h_of_port_h; - } else { - ret =3D __LINE__; - inline_ret =3D ret; - goto done; - } - - inner_map =3D bpf_map_lookup_elem(outer_map, &port_key); - if (!inner_map) { - ret =3D __LINE__; - inline_ret =3D ret; - goto done; - } - - ret =3D do_reg_lookup(inner_map, port_key); - - if (test_case =3D=3D 0 || test_case =3D=3D 1) - inline_ret =3D do_inline_array_lookup(inner_map, port_key); - else - inline_ret =3D do_inline_hash_lookup(inner_map, port_key); - -done: - bpf_map_update_elem(®_result_h, &ret_key, &ret, BPF_ANY); - bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY); - - return 0; -} - -char _license[] SEC("license") =3D "GPL"; -u32 _version SEC("version") =3D LINUX_VERSION_CODE; diff --git a/samples/bpf/test_map_in_map_user.c b/samples/bpf/test_map_in_m= ap_user.c deleted file mode 100644 index 55dca43f3723..000000000000 --- a/samples/bpf/test_map_in_map_user.c +++ /dev/null @@ -1,168 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * Copyright (c) 2017 Facebook - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "bpf_util.h" - -static int map_fd[7]; - -#define PORT_A (map_fd[0]) -#define PORT_H (map_fd[1]) -#define REG_RESULT_H (map_fd[2]) -#define INLINE_RESULT_H (map_fd[3]) -#define A_OF_PORT_A (map_fd[4]) /* Test case #0 */ -#define H_OF_PORT_A (map_fd[5]) /* Test case #1 */ -#define H_OF_PORT_H (map_fd[6]) /* Test case #2 */ - -static const char * const test_names[] =3D { - "Array of Array", - "Hash of Array", - "Hash of Hash", -}; - -#define NR_TESTS ARRAY_SIZE(test_names) - -static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key) -{ - struct bpf_map_info info =3D {}; - uint32_t info_len =3D sizeof(info); - int ret, id; - - ret =3D bpf_map_get_info_by_fd(inner_map_fd, &info, &info_len); - assert(!ret); - - ret =3D bpf_map_lookup_elem(map_in_map_fd, &key, &id); - assert(!ret); - assert(id =3D=3D info.id); -} - -static void populate_map(uint32_t port_key, int magic_result) -{ - int ret; - - ret =3D bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY); - assert(!ret); - - ret =3D bpf_map_update_elem(PORT_H, &port_key, &magic_result, - BPF_NOEXIST); - assert(!ret); - - ret =3D bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY); - assert(!ret); - check_map_id(PORT_A, A_OF_PORT_A, port_key); - - ret =3D bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST); - assert(!ret); - check_map_id(PORT_A, H_OF_PORT_A, port_key); - - ret =3D bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST); - assert(!ret); - check_map_id(PORT_H, H_OF_PORT_H, port_key); -} - -static void test_map_in_map(void) -{ - struct sockaddr_in6 in6 =3D { .sin6_family =3D AF_INET6 }; - uint32_t result_key =3D 0, port_key; - int result, inline_result; - int magic_result =3D 0xfaceb00c; - int ret; - int i; - - port_key =3D rand() & 0x00FF; - populate_map(port_key, magic_result); - - in6.sin6_addr.s6_addr16[0] =3D 0xdead; - in6.sin6_addr.s6_addr16[1] =3D 0xbeef; - in6.sin6_port =3D port_key; - - for (i =3D 0; i < NR_TESTS; i++) { - printf("%s: ", test_names[i]); - - in6.sin6_addr.s6_addr16[7] =3D i; - ret =3D connect(-1, (struct sockaddr *)&in6, sizeof(in6)); - assert(ret =3D=3D -1 && errno =3D=3D EBADF); - - ret =3D bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result); - assert(!ret); - - ret =3D bpf_map_lookup_elem(INLINE_RESULT_H, &result_key, - &inline_result); - assert(!ret); - - if (result !=3D magic_result || inline_result !=3D magic_result) { - printf("Error. result:%d inline_result:%d\n", - result, inline_result); - exit(1); - } - - bpf_map_delete_elem(REG_RESULT_H, &result_key); - bpf_map_delete_elem(INLINE_RESULT_H, &result_key); - - printf("Pass\n"); - } -} - -int main(int argc, char **argv) -{ - struct bpf_link *link =3D NULL; - struct bpf_program *prog; - struct bpf_object *obj; - char filename[256]; - - snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); - obj =3D bpf_object__open_file(filename, NULL); - if (libbpf_get_error(obj)) { - fprintf(stderr, "ERROR: opening BPF object file failed\n"); - return 0; - } - - prog =3D bpf_object__find_program_by_name(obj, "trace_sys_connect"); - if (!prog) { - printf("finding a prog in obj file failed\n"); - goto cleanup; - } - - /* load BPF program */ - if (bpf_object__load(obj)) { - fprintf(stderr, "ERROR: loading BPF object file failed\n"); - goto cleanup; - } - - map_fd[0] =3D bpf_object__find_map_fd_by_name(obj, "port_a"); - map_fd[1] =3D bpf_object__find_map_fd_by_name(obj, "port_h"); - map_fd[2] =3D bpf_object__find_map_fd_by_name(obj, "reg_result_h"); - map_fd[3] =3D bpf_object__find_map_fd_by_name(obj, "inline_result_h"); - map_fd[4] =3D bpf_object__find_map_fd_by_name(obj, "a_of_port_a"); - map_fd[5] =3D bpf_object__find_map_fd_by_name(obj, "h_of_port_a"); - map_fd[6] =3D bpf_object__find_map_fd_by_name(obj, "h_of_port_h"); - if (map_fd[0] < 0 || map_fd[1] < 0 || map_fd[2] < 0 || - map_fd[3] < 0 || map_fd[4] < 0 || map_fd[5] < 0 || map_fd[6] < 0) { - fprintf(stderr, "ERROR: finding a map in obj file failed\n"); - goto cleanup; - } - - link =3D bpf_program__attach(prog); - if (libbpf_get_error(link)) { - fprintf(stderr, "ERROR: bpf_program__attach failed\n"); - link =3D NULL; - goto cleanup; - } - - test_map_in_map(); - -cleanup: - bpf_link__destroy(link); - bpf_object__close(obj); - return 0; -} --=20 2.43.0