Skip to content

Commit

Permalink
bpf: introduce skb map
Browse files Browse the repository at this point in the history
Insert skb into skb map at position @key:
bpf_skb_push(&map, skb, key);

Remove the skb at position @key from skb map:
skb = bpf_skb_pop(&map, key);

Peek an skb by @key:
skb = bpf_map_lookup_elem(&map, &key);

Drop the skb at position @key:
bpf_map_delete_elem(&map, &key);

Iterate all the skbs in the map in order:
bpf_for_each_map_elem(&skb_map, skb_callback, key, skb);

Signed-off-by: Cong Wang <cong.wang@bytedance.com>
  • Loading branch information
congwang authored and Cong Wang committed May 31, 2022
1 parent c0a8f3b commit 03e158b
Show file tree
Hide file tree
Showing 7 changed files with 548 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/linux/bpf.h
Expand Up @@ -2273,6 +2273,10 @@ extern const struct bpf_func_proto bpf_loop_proto;
extern const struct bpf_func_proto bpf_strncmp_proto;
extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
extern const struct bpf_func_proto bpf_kptr_xchg_proto;
extern const struct bpf_func_proto bpf_skb_map_push_proto;
extern const struct bpf_func_proto bpf_skb_map_pop_proto;
extern const struct bpf_func_proto bpf_flow_map_push_proto;
extern const struct bpf_func_proto bpf_flow_map_pop_proto;

const struct bpf_func_proto *tracing_prog_func_proto(
enum bpf_func_id func_id, const struct bpf_prog *prog);
Expand Down
2 changes: 2 additions & 0 deletions include/linux/bpf_types.h
Expand Up @@ -110,6 +110,8 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_SKBMAP, skb_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_FLOWMAP, flow_map_ops)
#if defined(CONFIG_XDP_SOCKETS)
BPF_MAP_TYPE(BPF_MAP_TYPE_XSKMAP, xsk_map_ops)
#endif
Expand Down
4 changes: 3 additions & 1 deletion include/linux/skbuff.h
Expand Up @@ -1017,7 +1017,9 @@ struct sk_buff {
unsigned long dev_scratch;
};
};
struct rb_node rbnode; /* used in netem, ip4 defrag, and tcp stack */
struct rb_node rbnode; /* used in eBPF skb map, netem, ip4 defrag, and tcp
* stack
*/
struct list_head list;
struct llist_node ll_node;
};
Expand Down
6 changes: 6 additions & 0 deletions include/uapi/linux/bpf.h
Expand Up @@ -909,6 +909,8 @@ enum bpf_map_type {
BPF_MAP_TYPE_INODE_STORAGE,
BPF_MAP_TYPE_TASK_STORAGE,
BPF_MAP_TYPE_BLOOM_FILTER,
BPF_MAP_TYPE_SKBMAP,
BPF_MAP_TYPE_FLOWMAP,
};

/* Note that tracing related programs such as
Expand Down Expand Up @@ -5455,6 +5457,10 @@ union bpf_attr {
FN(dynptr_read), \
FN(dynptr_write), \
FN(dynptr_data), \
FN(skb_map_push), \
FN(skb_map_pop), \
FN(flow_map_push), \
FN(flow_map_pop), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down
10 changes: 10 additions & 0 deletions kernel/bpf/verifier.c
Expand Up @@ -6264,6 +6264,16 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,
func_id != BPF_FUNC_map_push_elem)
goto error;
break;
case BPF_MAP_TYPE_SKBMAP:
if (func_id != BPF_FUNC_skb_map_push &&
func_id != BPF_FUNC_skb_map_pop)
goto error;
break;
case BPF_MAP_TYPE_FLOWMAP:
if (func_id != BPF_FUNC_flow_map_push &&
func_id != BPF_FUNC_flow_map_pop)
goto error;
break;
default:
break;
}
Expand Down
1 change: 1 addition & 0 deletions net/core/Makefile
Expand Up @@ -38,4 +38,5 @@ obj-$(CONFIG_FAILOVER) += failover.o
obj-$(CONFIG_NET_SOCK_MSG) += skmsg.o
obj-$(CONFIG_BPF_SYSCALL) += sock_map.o
obj-$(CONFIG_BPF_SYSCALL) += bpf_sk_storage.o
obj-$(CONFIG_BPF_SYSCALL) += skb_map.o
obj-$(CONFIG_OF) += of_net.o

0 comments on commit 03e158b

Please sign in to comment.