Skip to content

Commit 3bdbd02

Browse files
jrfastabborkmann
authored andcommitted
bpf: sockmap, metadata support for reporting size of msg
This adds metadata to sk_msg_md for BPF programs to read the sk_msg size. When the SK_MSG program is running under an application that is using sendfile the data is not copied into sk_msg buffers by default. Rather the BPF program uses sk_msg_pull_data to read the bytes in. This avoids doing the costly memcopy instructions when they are not in fact needed. However, if we don't know the size of the sk_msg we have to guess if needed bytes are available by doing a pull request which may fail. By including the size of the sk_msg BPF programs can check the size before issuing sk_msg_pull_data requests. Additionally, the same applies for sendmsg calls when the application provides multiple iovs. Here the BPF program needs to pull in data to update data pointers but its not clear where the data ends without a size parameter. In many cases "guessing" is not easy to do and results in multiple calls to pull and without bounded loops everything gets fairly tricky. Clean this up by including a u32 size field. Note, all writes into sk_msg_md are rejected already from sk_msg_is_valid_access so nothing additional is needed there. Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 0bae2d4 commit 3bdbd02

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

include/linux/skmsg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ struct sk_msg_sg {
3636
struct scatterlist data[MAX_MSG_FRAGS + 1];
3737
};
3838

39+
/* UAPI in filter.c depends on struct sk_msg_sg being first element. If
40+
* this is moved filter.c also must be updated.
41+
*/
3942
struct sk_msg {
4043
struct sk_msg_sg sg;
4144
void *data;

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,7 @@ struct sk_msg_md {
26652665
__u32 local_ip6[4]; /* Stored in network byte order */
26662666
__u32 remote_port; /* Stored in network byte order */
26672667
__u32 local_port; /* stored in host byte order */
2668+
__u32 size; /* Total size of sk_msg */
26682669
};
26692670

26702671
struct sk_reuseport_md {

net/core/filter.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7530,6 +7530,12 @@ static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
75307530
*insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
75317531
offsetof(struct sock_common, skc_num));
75327532
break;
7533+
7534+
case offsetof(struct sk_msg_md, size):
7535+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
7536+
si->dst_reg, si->src_reg,
7537+
offsetof(struct sk_msg_sg, size));
7538+
break;
75337539
}
75347540

75357541
return insn - insn_buf;

0 commit comments

Comments
 (0)