Commit 0f7dc423 authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

Merge branch 'bpf: Add hwtstamp field for the sockops prog'



Martin KaFai Lau says:

====================

From: Martin KaFai Lau <martin.lau@kernel.org>

The bpf-tc prog has already been able to access the
skb_hwtstamps(skb)->hwtstamp.  This set extends the same hwtstamp
access to the sockops prog.

v2:
- Fixed the btf_dump selftest which depends on the
  last member of 'struct bpf_sock_ops'.
====================

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
parents e4c9cf0c 8cac7a59
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6445,6 +6445,7 @@ struct bpf_sock_ops {
				 * the outgoing header has not
				 * been written yet.
				 */
	__u64 skb_hwtstamp;
};

/* Definitions for bpf_sock_ops_cb_flags */
+31 −8
Original line number Diff line number Diff line
@@ -8925,6 +8925,10 @@ static bool sock_ops_is_valid_access(int off, int size,
			bpf_ctx_record_field_size(info, size_default);
			return bpf_ctx_narrow_access_ok(off, size,
							size_default);
		case offsetof(struct bpf_sock_ops, skb_hwtstamp):
			if (size != sizeof(__u64))
				return false;
			break;
		default:
			if (size != size_default)
				return false;
@@ -9108,21 +9112,21 @@ static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
	return insn;
}

static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
						  struct bpf_insn *insn)
{
	/* si->dst_reg = skb_shinfo(SKB); */
#ifdef NET_SKBUFF_DATA_USES_OFFSET
	*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
			      BPF_REG_AX, si->src_reg,
			      BPF_REG_AX, skb_reg,
			      offsetof(struct sk_buff, end));
	*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
			      si->dst_reg, si->src_reg,
			      dst_reg, skb_reg,
			      offsetof(struct sk_buff, head));
	*insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
	*insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
#else
	*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
			      si->dst_reg, si->src_reg,
			      dst_reg, skb_reg,
			      offsetof(struct sk_buff, end));
#endif

@@ -9515,7 +9519,7 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
		break;

	case offsetof(struct __sk_buff, gso_segs):
		insn = bpf_convert_shinfo_access(si, insn);
		insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
				      si->dst_reg, si->dst_reg,
				      bpf_target_off(struct skb_shared_info,
@@ -9523,7 +9527,7 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
						     target_size));
		break;
	case offsetof(struct __sk_buff, gso_size):
		insn = bpf_convert_shinfo_access(si, insn);
		insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
				      si->dst_reg, si->dst_reg,
				      bpf_target_off(struct skb_shared_info,
@@ -9550,7 +9554,7 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
		BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
		BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);

		insn = bpf_convert_shinfo_access(si, insn);
		insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
		*insn++ = BPF_LDX_MEM(BPF_DW,
				      si->dst_reg, si->dst_reg,
				      bpf_target_off(struct skb_shared_info,
@@ -10400,6 +10404,25 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
						       tcp_flags),
				      si->dst_reg, si->dst_reg, off);
		break;
	case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
		struct bpf_insn *jmp_on_null_skb;

		*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
						       skb),
				      si->dst_reg, si->src_reg,
				      offsetof(struct bpf_sock_ops_kern,
					       skb));
		/* Reserve one insn to test skb == NULL */
		jmp_on_null_skb = insn++;
		insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
		*insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
				      bpf_target_off(struct skb_shared_info,
						     hwtstamps, 8,
						     target_size));
		*jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
					       insn - jmp_on_null_skb - 1);
		break;
	}
	}
	return insn - insn_buf;
}
+1 −0
Original line number Diff line number Diff line
@@ -6445,6 +6445,7 @@ struct bpf_sock_ops {
				 * the outgoing header has not
				 * been written yet.
				 */
	__u64 skb_hwtstamp;
};

/* Definitions for bpf_sock_ops_cb_flags */
+2 −2
Original line number Diff line number Diff line
@@ -791,11 +791,11 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d,
	TEST_BTF_DUMP_DATA_OVER(btf, d, "struct", str, struct bpf_sock_ops,
				sizeof(struct bpf_sock_ops) - 1,
				"(struct bpf_sock_ops){\n\t.op = (__u32)1,\n",
				{ .op = 1, .skb_tcp_flags = 2});
				{ .op = 1, .skb_hwtstamp = 2});
	TEST_BTF_DUMP_DATA_OVER(btf, d, "struct", str, struct bpf_sock_ops,
				sizeof(struct bpf_sock_ops) - 1,
				"(struct bpf_sock_ops){\n\t.op = (__u32)1,\n",
				{ .op = 1, .skb_tcp_flags = 0});
				{ .op = 1, .skb_hwtstamp = 0});
}

static void test_btf_dump_var_data(struct btf *btf, struct btf_dump *d,
+4 −2
Original line number Diff line number Diff line
@@ -485,7 +485,7 @@ static void misc(void)
			goto check_linum;

		ret = read(sk_fds.passive_fd, recv_msg, sizeof(recv_msg));
		if (ASSERT_EQ(ret, sizeof(send_msg), "read(msg)"))
		if (!ASSERT_EQ(ret, sizeof(send_msg), "read(msg)"))
			goto check_linum;
	}

@@ -505,6 +505,8 @@ static void misc(void)

	ASSERT_EQ(misc_skel->bss->nr_fin, 1, "unexpected nr_fin");

	ASSERT_EQ(misc_skel->bss->nr_hwtstamp, 0, "nr_hwtstamp");

check_linum:
	ASSERT_FALSE(check_error_linum(&sk_fds), "check_error_linum");
	sk_fds_close(&sk_fds);
@@ -539,7 +541,7 @@ void test_tcp_hdr_options(void)
		goto skel_destroy;

	cg_fd = test__join_cgroup(CG_NAME);
	if (ASSERT_GE(cg_fd, 0, "join_cgroup"))
	if (!ASSERT_GE(cg_fd, 0, "join_cgroup"))
		goto skel_destroy;

	for (i = 0; i < ARRAY_SIZE(tests); i++) {
Loading