Skip to content

Commit 604326b

Browse files
borkmannAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf, sockmap: convert to generic sk_msg interface
Add a generic sk_msg layer, and convert current sockmap and later kTLS over to make use of it. While sk_buff handles network packet representation from netdevice up to socket, sk_msg handles data representation from application to socket layer. This means that sk_msg framework spans across ULP users in the kernel, and enables features such as introspection or filtering of data with the help of BPF programs that operate on this data structure. Latter becomes in particular useful for kTLS where data encryption is deferred into the kernel, and as such enabling the kernel to perform L7 introspection and policy based on BPF for TLS connections where the record is being encrypted after BPF has run and came to a verdict. In order to get there, first step is to transform open coding of scatter-gather list handling into a common core framework that subsystems can use. The code itself has been split and refactored into three bigger pieces: i) the generic sk_msg API which deals with managing the scatter gather ring, providing helpers for walking and mangling, transferring application data from user space into it, and preparing it for BPF pre/post-processing, ii) the plain sock map itself where sockets can be attached to or detached from; these bits are independent of i) which can now be used also without sock map, and iii) the integration with plain TCP as one protocol to be used for processing L7 application data (later this could e.g. also be extended to other protocols like UDP). The semantics are the same with the old sock map code and therefore no change of user facing behavior or APIs. While pursuing this work it also helped finding a number of bugs in the old sockmap code that we've fixed already in earlier commits. The test_sockmap kselftest suite passes through fine as well. Joint work with John. Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 1243a51 commit 604326b

File tree

17 files changed

+2925
-2860
lines changed

17 files changed

+2925
-2860
lines changed

include/linux/bpf.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -737,33 +737,18 @@ static inline void bpf_map_offload_map_free(struct bpf_map *map)
737737
}
738738
#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
739739

740-
#if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_INET)
741-
struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key);
742-
struct sock *__sock_hash_lookup_elem(struct bpf_map *map, void *key);
743-
int sock_map_prog(struct bpf_map *map, struct bpf_prog *prog, u32 type);
744-
int sockmap_get_from_fd(const union bpf_attr *attr, int type,
745-
struct bpf_prog *prog);
740+
#if defined(CONFIG_BPF_STREAM_PARSER)
741+
int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, u32 which);
742+
int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
746743
#else
747-
static inline struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)
748-
{
749-
return NULL;
750-
}
751-
752-
static inline struct sock *__sock_hash_lookup_elem(struct bpf_map *map,
753-
void *key)
754-
{
755-
return NULL;
756-
}
757-
758-
static inline int sock_map_prog(struct bpf_map *map,
759-
struct bpf_prog *prog,
760-
u32 type)
744+
static inline int sock_map_prog_update(struct bpf_map *map,
745+
struct bpf_prog *prog, u32 which)
761746
{
762747
return -EOPNOTSUPP;
763748
}
764749

765-
static inline int sockmap_get_from_fd(const union bpf_attr *attr, int type,
766-
struct bpf_prog *prog)
750+
static inline int sock_map_get_from_fd(const union bpf_attr *attr,
751+
struct bpf_prog *prog)
767752
{
768753
return -EINVAL;
769754
}
@@ -839,6 +824,10 @@ extern const struct bpf_func_proto bpf_get_stack_proto;
839824
extern const struct bpf_func_proto bpf_sock_map_update_proto;
840825
extern const struct bpf_func_proto bpf_sock_hash_update_proto;
841826
extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
827+
extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
828+
extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
829+
extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
830+
extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
842831

843832
extern const struct bpf_func_proto bpf_get_local_storage_proto;
844833

include/linux/bpf_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)
5757
BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
5858
#ifdef CONFIG_NET
5959
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
60-
#if defined(CONFIG_STREAM_PARSER) && defined(CONFIG_INET)
60+
#if defined(CONFIG_BPF_STREAM_PARSER)
6161
BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops)
6262
BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)
6363
#endif

include/linux/filter.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -520,24 +520,6 @@ struct bpf_skb_data_end {
520520
void *data_end;
521521
};
522522

523-
struct sk_msg_buff {
524-
void *data;
525-
void *data_end;
526-
__u32 apply_bytes;
527-
__u32 cork_bytes;
528-
int sg_copybreak;
529-
int sg_start;
530-
int sg_curr;
531-
int sg_end;
532-
struct scatterlist sg_data[MAX_SKB_FRAGS];
533-
bool sg_copy[MAX_SKB_FRAGS];
534-
__u32 flags;
535-
struct sock *sk_redir;
536-
struct sock *sk;
537-
struct sk_buff *skb;
538-
struct list_head list;
539-
};
540-
541523
struct bpf_redirect_info {
542524
u32 ifindex;
543525
u32 flags;
@@ -833,9 +815,6 @@ void xdp_do_flush_map(void);
833815

834816
void bpf_warn_invalid_xdp_action(u32 act);
835817

836-
struct sock *do_sk_redirect_map(struct sk_buff *skb);
837-
struct sock *do_msg_redirect_map(struct sk_msg_buff *md);
838-
839818
#ifdef CONFIG_INET
840819
struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
841820
struct bpf_prog *prog, struct sk_buff *skb,

0 commit comments

Comments
 (0)