Skip to content

Commit f64c4ac

Browse files
vvfedorenkoborkmann
authored andcommitted
bpf: Add hardware timestamp field to __sk_buff
BPF programs may want to know hardware timestamps if NIC supports such timestamping. Expose this data as hwtstamp field of __sk_buff the same way as gso_segs/gso_size. This field could be accessed from the same programs as tstamp field, but it's read-only field. Explicit test to deny access to padding data is added to bpf_skb_is_valid_access. Also update BPF_PROG_TEST_RUN tests of the feature. Signed-off-by: Vadim Fedorenko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e876a03 commit f64c4ac

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,6 +5284,8 @@ struct __sk_buff {
52845284
__u32 gso_segs;
52855285
__bpf_md_ptr(struct bpf_sock *, sk);
52865286
__u32 gso_size;
5287+
__u32 :32; /* Padding, future use. */
5288+
__u64 hwtstamp;
52875289
};
52885290

52895291
struct bpf_tunnel_key {

net/core/filter.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7765,6 +7765,10 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type
77657765
break;
77667766
case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
77677767
return false;
7768+
case bpf_ctx_range(struct __sk_buff, hwtstamp):
7769+
if (type == BPF_WRITE || size != sizeof(__u64))
7770+
return false;
7771+
break;
77687772
case bpf_ctx_range(struct __sk_buff, tstamp):
77697773
if (size != sizeof(__u64))
77707774
return false;
@@ -7774,6 +7778,9 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type
77747778
return false;
77757779
info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
77767780
break;
7781+
case offsetofend(struct __sk_buff, gso_size) ... offsetof(struct __sk_buff, hwtstamp) - 1:
7782+
/* Explicitly prohibit access to padding in __sk_buff. */
7783+
return false;
77777784
default:
77787785
/* Only narrow read access allowed for now. */
77797786
if (type == BPF_WRITE) {
@@ -7802,6 +7809,7 @@ static bool sk_filter_is_valid_access(int off, int size,
78027809
case bpf_ctx_range_till(struct __sk_buff, family, local_port):
78037810
case bpf_ctx_range(struct __sk_buff, tstamp):
78047811
case bpf_ctx_range(struct __sk_buff, wire_len):
7812+
case bpf_ctx_range(struct __sk_buff, hwtstamp):
78057813
return false;
78067814
}
78077815

@@ -7872,6 +7880,7 @@ static bool lwt_is_valid_access(int off, int size,
78727880
case bpf_ctx_range(struct __sk_buff, data_meta):
78737881
case bpf_ctx_range(struct __sk_buff, tstamp):
78747882
case bpf_ctx_range(struct __sk_buff, wire_len):
7883+
case bpf_ctx_range(struct __sk_buff, hwtstamp):
78757884
return false;
78767885
}
78777886

@@ -8373,6 +8382,7 @@ static bool sk_skb_is_valid_access(int off, int size,
83738382
case bpf_ctx_range(struct __sk_buff, data_meta):
83748383
case bpf_ctx_range(struct __sk_buff, tstamp):
83758384
case bpf_ctx_range(struct __sk_buff, wire_len):
8385+
case bpf_ctx_range(struct __sk_buff, hwtstamp):
83768386
return false;
83778387
}
83788388

@@ -8884,6 +8894,17 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type,
88848894
si->dst_reg, si->src_reg,
88858895
offsetof(struct sk_buff, sk));
88868896
break;
8897+
case offsetof(struct __sk_buff, hwtstamp):
8898+
BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
8899+
BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
8900+
8901+
insn = bpf_convert_shinfo_access(si, insn);
8902+
*insn++ = BPF_LDX_MEM(BPF_DW,
8903+
si->dst_reg, si->dst_reg,
8904+
bpf_target_off(struct skb_shared_info,
8905+
hwtstamps, 8,
8906+
target_size));
8907+
break;
88878908
}
88888909

88898910
return insn - insn_buf;

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5284,6 +5284,8 @@ struct __sk_buff {
52845284
__u32 gso_segs;
52855285
__bpf_md_ptr(struct bpf_sock *, sk);
52865286
__u32 gso_size;
5287+
__u32 :32; /* Padding, future use. */
5288+
__u64 hwtstamp;
52875289
};
52885290

52895291
struct bpf_tunnel_key {

0 commit comments

Comments
 (0)