tools/testing/selftests/bpf/progs/netif_receive_skb.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/netif_receive_skb.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/netif_receive_skb.c
Extension
.c
Size
8011 bytes
Lines
253
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (_ptr.type_id <= 0) {				\
			ret = -EINVAL;					\
			break;						\
		}							\
		ret = bpf_snprintf_btf(_str, STRSIZE,			\
				       &_ptr, sizeof(_ptr), _hflags);	\
		if (ret)						\
			break;						\
		_cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE);	\
		if (_cmp != 0) {					\
			bpf_printk("(%d) got %s", _cmp, _str);		\
			bpf_printk("(%d) expected %s", _cmp,		\
				   _expectedval);			\
			ret = -EBADMSG;					\
			break;						\
		}							\
	} while (0)
#endif

/* Use where expected data string matches its stringified declaration */
#define TEST_BTF_C(_str, _type, _flags, ...)				\
	TEST_BTF(_str, _type, _flags, "(" #_type ")" #__VA_ARGS__,	\
		 __VA_ARGS__)

/* TRACE_EVENT(netif_receive_skb,
 *	TP_PROTO(struct sk_buff *skb),
 */
SEC("tp_btf/netif_receive_skb")
int BPF_PROG(trace_netif_receive_skb, struct sk_buff *skb)
{
	static __u64 flags[] = { 0, BTF_F_COMPACT, BTF_F_ZERO, BTF_F_PTR_RAW,
				 BTF_F_NONAME, BTF_F_COMPACT | BTF_F_ZERO |
				 BTF_F_PTR_RAW | BTF_F_NONAME };
	static struct btf_ptr p = { };
	__u32 key = 0;
	int i, __ret;
	char *str;

#if __has_builtin(__builtin_btf_type_id)
	str = bpf_map_lookup_elem(&strdata, &key);
	if (!str)
		return 0;

	/* Ensure we can write skb string representation */
	p.type_id = bpf_core_type_id_kernel(struct sk_buff);
	p.ptr = skb;
	for (i = 0; i < ARRAY_SIZE(flags); i++) {
		++num_subtests;
		ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0);
		if (ret < 0)
			bpf_printk("returned %d when writing skb", ret);
		++ran_subtests;
	}

	/* Check invalid ptr value */
	p.ptr = BADPTR;
	__ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0);
	if (__ret >= 0) {
		bpf_printk("printing %llx should generate error, got (%d)",
			   (unsigned long long)BADPTR, __ret);
		ret = -ERANGE;
	}

	/* Verify type display for various types. */

	/* simple int */
	TEST_BTF_C(str, int, 0, 1234);
	TEST_BTF(str, int, BTF_F_NONAME, "1234", 1234);
	/* zero value should be printed at toplevel */
	TEST_BTF(str, int, 0, "(int)0", 0);
	TEST_BTF(str, int, BTF_F_NONAME, "0", 0);
	TEST_BTF(str, int, BTF_F_ZERO, "(int)0", 0);
	TEST_BTF(str, int, BTF_F_NONAME | BTF_F_ZERO, "0", 0);
	TEST_BTF_C(str, int, 0, -4567);
	TEST_BTF(str, int, BTF_F_NONAME, "-4567", -4567);

	/* simple char */
	TEST_BTF_C(str, char, 0, 100);
	TEST_BTF(str, char, BTF_F_NONAME, "100", 100);
	/* zero value should be printed at toplevel */
	TEST_BTF(str, char, 0, "(char)0", 0);
	TEST_BTF(str, char, BTF_F_NONAME, "0", 0);
	TEST_BTF(str, char, BTF_F_ZERO, "(char)0", 0);
	TEST_BTF(str, char, BTF_F_NONAME | BTF_F_ZERO, "0", 0);

	/* simple typedef */
	TEST_BTF_C(str, uint64_t, 0, 100);
	TEST_BTF(str, u64, BTF_F_NONAME, "1", 1);
	/* zero value should be printed at toplevel */
	TEST_BTF(str, u64, 0, "(u64)0", 0);

Annotation

Implementation Notes