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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/net_timestamping.c
Extension
.c
Size
5631 bytes
Lines
249
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

struct sk_stg {
	__u64 sendmsg_ns;	/* record ts when sendmsg is called */
};

struct sk_tskey {
	u64 cookie;
	u32 tskey;
};

struct delay_info {
	u64 sendmsg_ns;		/* record ts when sendmsg is called */
	u32 sched_delay;	/* SCHED_CB - sendmsg_ns */
	u32 snd_sw_delay;	/* SND_SW_CB - SCHED_CB */
	u32 ack_delay;		/* ACK_CB - SND_SW_CB */
};

struct {
	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
	__uint(map_flags, BPF_F_NO_PREALLOC);
	__type(key, int);
	__type(value, struct sk_stg);
} sk_stg_map SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, struct sk_tskey);
	__type(value, struct delay_info);
	__uint(max_entries, 1024);
} time_map SEC(".maps");

static u64 delay_tolerance_nsec = 10000000000; /* 10 second as an example */

extern int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, u64 flags) __ksym;

static int bpf_test_sockopt(void *ctx, const struct sock *sk, int expected)
{
	int tmp, new = SK_BPF_CB_TX_TIMESTAMPING;
	int opt = SK_BPF_CB_FLAGS;
	int level = SOL_SOCKET;

	if (bpf_setsockopt(ctx, level, opt, &new, sizeof(new)) != expected)
		return 1;

	if (bpf_getsockopt(ctx, level, opt, &tmp, sizeof(tmp)) != expected ||
	    (!expected && tmp != new))
		return 1;

	return 0;
}

static bool bpf_test_access_sockopt(void *ctx, const struct sock *sk)
{
	if (bpf_test_sockopt(ctx, sk, -EOPNOTSUPP))
		return true;
	return false;
}

static bool bpf_test_access_load_hdr_opt(struct bpf_sock_ops *skops)
{
	u8 opt[3] = {0};
	int load_flags = 0;
	int ret;

	ret = bpf_load_hdr_opt(skops, opt, sizeof(opt), load_flags);
	if (ret != -EOPNOTSUPP)
		return true;

	return false;
}

static bool bpf_test_access_cb_flags_set(struct bpf_sock_ops *skops)
{
	int ret;

	ret = bpf_sock_ops_cb_flags_set(skops, 0);
	if (ret != -EOPNOTSUPP)
		return true;

	return false;
}

/* In the timestamping callbacks, we're not allowed to call the following
 * BPF CALLs for the safety concern. Return false if expected.
 */
static bool bpf_test_access_bpf_calls(struct bpf_sock_ops *skops,
				      const struct sock *sk)
{
	if (bpf_test_access_sockopt(skops, sk))
		return true;

Annotation

Implementation Notes