Provide bpf_sk_storage data in INET_DIAG
Subsystems (6)
v2 cover Provide bpf_sk_storage data in INET_DIAG
The bpf_prog can store specific info to a sk by using bpf_sk_storage. In other words, a sk can be extended by a bpf_prog.
This series is to support providing bpf_sk_storage data during inet_diag's dump. The primary target is the usage like iproute2's "ss".
The first two patches are refactoring works in inet_diag to make adding bpf_sk_storage support easier. The next two patches do the actual work.
Please see individual patch for details.
v2:
- Add commit message for u16 to u32 change in min_dump_alloc in Patch 4 (Song)
- Add comment to explain the !skb->len check in __inet_diag_dump in Patch 4.
- Do the map->map_type check earlier in Patch 3 for readability.
Martin KaFai Lau (4):
inet_diag: Refactor inet_sk_diag_fill(), dump(), and dump_one()
inet_diag: Move the INET_DIAG_REQ_BYTECODE nlattr to cb->data
bpf: INET_DIAG support in bpf_sk_storage
bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
include/linux/bpf.h | 1 + include/linux/inet_diag.h | 27 +-- include/linux/netlink.h | 4 +- include/net/bpf_sk_storage.h | 27 +++ include/uapi/linux/inet_diag.h | 5 +- include/uapi/linux/sock_diag.h | 26 +++ kernel/bpf/syscall.c | 15 ++ net/core/bpf_sk_storage.c | 283 +++++++++++++++++++++++++++++- net/dccp/diag.c | 9 +- net/ipv4/inet_diag.c | 307 ++++++++++++++++++++------------- net/ipv4/raw_diag.c | 24 ++- net/ipv4/tcp_diag.c | 8 +- net/ipv4/udp_diag.c | 41 +++-- net/sctp/diag.c | 7 +- 14 files changed, 599 insertions(+), 185 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/4 inet_diag: Refactor inet_sk_diag_fill(), dump(), and dump_one()
In a latter patch, there is a need to update "cb->min_dump_alloc" in inet_sk_diag_fill() as it learns the diffierent bpf_sk_storages stored in a sk while dumping all sk(s) (e.g. tcp_hashinfo).
The inet_sk_diag_fill() currently does not take the "cb" as an argument. One of the reason is inet_sk_diag_fill() is used by both dump_one() and dump() (which belong to the "struct inet_diag_handler". The dump_one() interface does not pass the "cb" along.
This patch is to make dump_one() pass a "cb". The "cb" is created in inet_diag_cmd_exact(). The "nlh" and "in_skb" are stored in "cb" as the dump() interface does. The total number of args in inet_sk_diag_fill() is also cut from 10 to 7 and
that helps many callers to pass fewer args.
In particular,
"struct user_namespace *user_ns", "u32 pid", and "u32 seq" can be replaced by accessing "cb->nlh" and "cb->skb".
A similar argument reduction is also made toinet_twsk_diag_fill() and inet_req_diag_fill().
inet_csk_diag_dump() and inet_csk_diag_fill() are also removed. They are mostly equivalent to inet_sk_diag_fill(). Their repeated usages are very limited. Thus, inet_sk_diag_fill() is directly used in those occasions.
--- include/linux/inet_diag.h | 12 ++-- net/dccp/diag.c | 5 +- net/ipv4/inet_diag.c | 116 +++++++++++++++----------------------- net/ipv4/raw_diag.c | 18 ++---- net/ipv4/tcp_diag.c | 4 +- net/ipv4/udp_diag.c | 26 ++++----- net/sctp/diag.c | 5 +- 7 files changed, 73 insertions(+), 113 deletions(-)
include/linux/inet_diag.h +5 -7
@@ -18,8 +18,7 @@ struct inet_diag_handler {const struct inet_diag_req_v2 *r, struct nlattr *bc); -int (*dump_one)(struct sk_buff *in_skb, -const struct nlmsghdr *nlh, +int (*dump_one)(struct netlink_callback *cb,const struct inet_diag_req_v2 *req); void (*idiag_get_info)(struct sock *sk,
@@ -42,16 +41,15 @@ struct inet_diag_handler {struct inet_connection_sock ;int inet_sk_diag_fill (struct sock *sk,struct inet_connection_sock *icsk, -struct sk_buff *skb,const struct inet_diag_req_v2 *req, -struct user_namespace *user_ns, -u32 pid,u32 seq,u16 nlmsg_flags, -const struct nlmsghdr *unlh,bool net_admin); +struct sk_buff *skb,struct netlink_callback *cb, +const struct inet_diag_req_v2 *req, +u16 nlmsg_flags,bool net_admin);void inet_diag_dump_icsk (struct inet_hashinfo *h,struct sk_buff *skb,struct netlink_callback *cb,const struct inet_diag_req_v2 *r,struct nlattr *bc);int inet_diag_dump_one_icsk (struct inet_hashinfo *hashinfo, -struct sk_buff *in_skb,const struct nlmsghdr *nlh, +struct netlink_callback *cb,const struct inet_diag_req_v2 *req); struct sock *inet_diag_find_one_icsk(struct net *net,
net/dccp/diag.c +2 -3
@@ -51,11 +51,10 @@ static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,inet_diag_dump_icsk (&dccp_hashinfo, skb, cb, r, bc); } -static int dccp_diag_dump_one (struct sk_buff *in_skb, -const struct nlmsghdr *nlh, +static int dccp_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { -return inet_diag_dump_one_icsk (&dccp_hashinfo, in_skb, nlh, req); +return inet_diag_dump_one_icsk (&dccp_hashinfo, cb, req); }static const struct inet_diag_handler dccp_diag_handler = {
net/ipv4/inet_diag.c +44 -72
213 lines not on this page · raw message
net/ipv4/raw_diag.c +6 -12
@@ -87,15 +87,16 @@ static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2return sk ? sk :ERR_PTR (-ENOENT ); } -static int raw_diag_dump_one (struct sk_buff *in_skb, -const struct nlmsghdr *nlh, +static int raw_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *r) { -struct net *net =sock_net (in_skb->sk); +struct sk_buff *in_skb = cb->skb;struct sk_buff *rep;struct sock *sk; +struct net *net;int err; + net =sock_net (in_skb->sk); sk =raw_sock_get (net, r);if (IS_ERR (sk))return PTR_ERR (sk);
@@ -108,10 +109,7 @@ static int raw_diag_dump_one(struct sk_buff *in_skb,return -ENOMEM ; } - err =inet_sk_diag_fill (sk,NULL , rep, r, -sk_user_ns (NETLINK_CB (in_skb).sk), -NETLINK_CB (in_skb).portid, - nlh->nlmsg_seq,0 , nlh, + err =inet_sk_diag_fill (sk,NULL , rep, cb, r,0 ,netlink_net_capable (in_skb,CAP_NET_ADMIN ));sock_put (sk);
@@ -136,11 +134,7 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,if (!inet_diag_bc_sk (bc, sk))return 0 ; -return inet_sk_diag_fill (sk,NULL , skb, r, -sk_user_ns (NETLINK_CB (cb->skb).sk), -NETLINK_CB (cb->skb).portid, - cb->nlh->nlmsg_seq,NLM_F_MULTI , - cb->nlh, net_admin); +return inet_sk_diag_fill (sk,NULL , skb, cb, r,NLM_F_MULTI , net_admin); } static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
net/ipv4/tcp_diag.c +2 -2
@@ -184,10 +184,10 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,inet_diag_dump_icsk (&tcp_hashinfo, skb, cb, r, bc); } -static int tcp_diag_dump_one (struct sk_buff *in_skb,const struct nlmsghdr *nlh, +static int tcp_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { -return inet_diag_dump_one_icsk (&tcp_hashinfo, in_skb, nlh, req); +return inet_diag_dump_one_icsk (&tcp_hashinfo, cb, req); }#ifdef CONFIG_INET_DIAG_DESTROY
net/ipv4/udp_diag.c +11 -15
@@ -21,16 +21,15 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,if (!inet_diag_bc_sk (bc, sk))return 0 ; -return inet_sk_diag_fill (sk,NULL , skb, req, -sk_user_ns (NETLINK_CB (cb->skb).sk), -NETLINK_CB (cb->skb).portid, - cb->nlh->nlmsg_seq,NLM_F_MULTI , cb->nlh, net_admin); +return inet_sk_diag_fill (sk,NULL , skb, cb, req,NLM_F_MULTI , + net_admin); } -static int udp_dump_one (struct udp_table *tbl,struct sk_buff *in_skb, -const struct nlmsghdr *nlh, +static int udp_dump_one (struct udp_table *tbl, +struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { +struct sk_buff *in_skb = cb->skb;int err = -EINVAL ;struct sock *sk =NULL ;struct sk_buff *rep;
@@ -70,11 +69,8 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,if (!rep) goto out; - err =inet_sk_diag_fill (sk,NULL , rep, req, -sk_user_ns (NETLINK_CB (in_skb).sk), -NETLINK_CB (in_skb).portid, - nlh->nlmsg_seq,0 , nlh, -netlink_net_capable (in_skb,CAP_NET_ADMIN )); + err =inet_sk_diag_fill (sk,NULL , rep, cb, req,0 , +netlink_net_capable (in_skb,CAP_NET_ADMIN ));if (err <0 ) {WARN_ON (err == -EMSGSIZE );kfree_skb (rep);
@@ -151,10 +147,10 @@ static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,udp_dump (&udp_table, skb, cb, r, bc); } -static int udp_diag_dump_one (struct sk_buff *in_skb,const struct nlmsghdr *nlh, +static int udp_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { -return udp_dump_one (&udp_table, in_skb, nlh, req); +return udp_dump_one (&udp_table, cb, req); } static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
@@ -255,10 +251,10 @@ static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,udp_dump (&udplite_table, skb, cb, r, bc); } -static int udplite_diag_dump_one (struct sk_buff *in_skb,const struct nlmsghdr *nlh, +static int udplite_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { -return udp_dump_one (&udplite_table, in_skb, nlh, req); +return udp_dump_one (&udplite_table, cb, req); }static const struct inet_diag_handler udplite_diag_handler = {
net/sctp/diag.c +3 -2
@@ -432,11 +432,12 @@ static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,sctp_get_sctp_info (sk, infox->asoc, infox->sctpinfo); } -static int sctp_diag_dump_one (struct sk_buff *in_skb, -const struct nlmsghdr *nlh, +static int sctp_diag_dump_one (struct netlink_callback *cb,const struct inet_diag_req_v2 *req) { +struct sk_buff *in_skb = cb->skb;struct net *net =sock_net (in_skb->sk); +const struct nlmsghdr *nlh = cb->nlh;union sctp_addr laddr, paddr;struct sctp_comm_param commp = { .skb = in_skb,
Signature · 1 line
2.17.1
PATCH v2 2/4 inet_diag: Move the INET_DIAG_REQ_BYTECODE nlattr to cb->data
In reply to Martin KaFai LauThe INET_DIAG_REQ_BYTECODE nlattr is currently re-found every time when the "dump()" is re-started.
In a latter patch, it will also need to parse the new INET_DIAG_REQ_SK_BPF_STORAGES nlattr to learn the map_fds. Thus, this patch takes this chance to store the parsed nlattr in cb->data during the "start" time of a dump.
By doing this, the "bc" argument also becomes unnecessary and is removed. Also, the two copies of the INET_DIAG_REQ_BYTECODE parsing-audit logic between compat/current version can be consolidated to one.
--- include/linux/inet_diag.h | 11 ++-- include/uapi/linux/inet_diag.h | 3 +- net/dccp/diag.c | 4 +- net/ipv4/inet_diag.c | 117 ++++++++++++++++++++------------- net/ipv4/raw_diag.c | 6 +- net/ipv4/tcp_diag.c | 4 +- net/ipv4/udp_diag.c | 15 +++-- net/sctp/diag.c | 2 +- 8 files changed, 98 insertions(+), 64 deletions(-)
include/linux/inet_diag.h +7 -4
@@ -15,8 +15,7 @@ struct netlink_callback;struct inet_diag_handler {void (*dump)(struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r, -struct nlattr *bc); +const struct inet_diag_req_v2 *r);int (*dump_one)(struct netlink_callback *cb,const struct inet_diag_req_v2 *req);
@@ -39,6 +38,11 @@ struct inet_diag_handler {__u16 idiag_info_size; }; +struct inet_diag_dump_data { +struct nlattr *req_nlas[__INET_DIAG_REQ_MAX]; +#define inet_diag_nla_bc req_nlas[INET_DIAG_REQ_BYTECODE] +}; +struct inet_connection_sock ; int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, struct sk_buff *skb, struct netlink_callback *cb,
@@ -46,8 +50,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, u16 nlmsg_flags, bool net_admin);void inet_diag_dump_icsk (struct inet_hashinfo *h,struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r, -struct nlattr *bc); +const struct inet_diag_req_v2 *r);int inet_diag_dump_one_icsk (struct inet_hashinfo *hashinfo,struct netlink_callback *cb,const struct inet_diag_req_v2 *req);
include/uapi/linux/inet_diag.h +2 -1
@@ -64,9 +64,10 @@ struct inet_diag_req_raw {enum {INET_DIAG_REQ_NONE ,INET_DIAG_REQ_BYTECODE , + __INET_DIAG_REQ_MAX, }; -#define INET_DIAG_REQ_MAX INET_DIAG_REQ_BYTECODE +#define INET_DIAG_REQ_MAX (__INET_DIAG_REQ_MAX - 1)/* Bytecode is sequence of 4 byte commands followed by variable arguments. * All the commands identified by "code" are conditional jumps forward:
net/dccp/diag.c +2 -2
@@ -46,9 +46,9 @@ static void dccp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, }static void dccp_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) { -inet_diag_dump_icsk (&dccp_hashinfo, skb, cb, r, bc); +inet_diag_dump_icsk (&dccp_hashinfo, skb, cb, r); } static int dccp_diag_dump_one(struct netlink_callback *cb,
net/ipv4/inet_diag.c +70 -47
184 lines not on this page · raw message
net/ipv4/raw_diag.c +5 -1
@@ -138,17 +138,21 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, }static void raw_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) {bool net_admin =netlink_net_capable (cb->skb,CAP_NET_ADMIN );struct raw_hashinfo *hashinfo =raw_get_hashinfo (r);struct net *net =sock_net (skb->sk); +struct inet_diag_dump_data *cb_data;int num, s_num, slot, s_slot;struct sock *sk =NULL ; +struct nlattr *bc;if (IS_ERR (hashinfo))return ; + cb_data = cb->data; + bc = cb_data->inet_diag_nla_bc; s_slot = cb->args[0 ]; num = s_num = cb->args[1 ];
net/ipv4/tcp_diag.c +2 -2
@@ -179,9 +179,9 @@ static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin) }static void tcp_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) { -inet_diag_dump_icsk (&tcp_hashinfo, skb, cb, r, bc); +inet_diag_dump_icsk (&tcp_hashinfo, skb, cb, r); } static int tcp_diag_dump_one(struct netlink_callback *cb,
net/ipv4/udp_diag.c +9 -6
@@ -89,12 +89,16 @@ static int udp_dump_one(struct udp_table *tbl,static void udp_dump (struct udp_table *table,struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) {bool net_admin =netlink_net_capable (cb->skb,CAP_NET_ADMIN );struct net *net =sock_net (skb->sk); +struct inet_diag_dump_data *cb_data;int num, s_num, slot, s_slot; +struct nlattr *bc; + cb_data = cb->data; + bc = cb_data->inet_diag_nla_bc; s_slot = cb->args[0 ]; num = s_num = cb->args[1 ];
@@ -142,9 +146,9 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb, }static void udp_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) { -udp_dump (&udp_table, skb, cb, r, bc); +udp_dump (&udp_table, skb, cb, r); } static int udp_diag_dump_one(struct netlink_callback *cb,
@@ -245,10 +249,9 @@ static const struct inet_diag_handler udp_diag_handler = { };static void udplite_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r, -struct nlattr *bc) +const struct inet_diag_req_v2 *r) { -udp_dump (&udplite_table, skb, cb, r, bc); +udp_dump (&udplite_table, skb, cb, r); } static int udplite_diag_dump_one(struct netlink_callback *cb,
net/sctp/diag.c +1 -1
@@ -471,7 +471,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb, }static void sctp_diag_dump (struct sk_buff *skb,struct netlink_callback *cb, -const struct inet_diag_req_v2 *r,struct nlattr *bc) +const struct inet_diag_req_v2 *r) {u32 idiag_states = r->idiag_states;struct net *net =sock_net (skb->sk);
Signature · 1 line
2.17.1
PATCH v2 3/4 bpf: INET_DIAG support in bpf_sk_storage
In reply to Martin KaFai LauThis patch adds INET_DIAG support to bpf_sk_storage.
1. Although this series adds bpf_sk_storage diag capability to inet sk, bpf_sk_storage is in general applicable to all fullsock. Hence, the bpf_sk_storage logic will operate on SK_DIAG_* nlattr. The caller will pass in its specific nesting nlattr (e.g. INET_DIAG_*) as the argument.
2. The request will be like:
INET_DIAG_REQ_SK_BPF_STORAGES (nla_nest) (defined in latter patch) SK_DIAG_BPF_STORAGE_REQ_MAP_FD (nla_put_u32) SK_DIAG_BPF_STORAGE_REQ_MAP_FD (nla_put_u32) ......
Considering there could have multiple bpf_sk_storages in a sk, instead of reusing INET_DIAG_INFO ("ss -i"), the user can select some specific bpf_sk_storage to dump by specifying an array of SK_DIAG_BPF_STORAGE_REQ_MAP_FD.
If no SK_DIAG_BPF_STORAGE_REQ_MAP_FD is specified (i.e. an empty INET_DIAG_REQ_SK_BPF_STORAGES), it will dump all bpf_sk_storages of a sk.
3. The reply will be like:
INET_DIAG_BPF_SK_STORAGES (nla_nest) (defined in latter patch) SK_DIAG_BPF_STORAGE (nla_nest) SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32) SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit) SK_DIAG_BPF_STORAGE (nla_nest) SK_DIAG_BPF_STORAGE_MAP_ID (nla_put_u32) SK_DIAG_BPF_STORAGE_MAP_VALUE (nla_reserve_64bit) ......
4. Unlike other INET_DIAG info of a sk which is pretty static, the size required to dump the bpf_sk_storage(s) of a sk is dynamic as the system adding more bpf_sk_storage_map. It is hard to set a static min_dump_alloc size.
Hence, this series learns it at the runtime and adjust the cb->min_dump_alloc as it iterates all sk(s) of a system. The "unsigned int *res_diag_size" in bpf_sk_storage_diag_put() is for this purpose.
The next patch will update the cb->min_dump_alloc as it iterates the sk(s).
--- include/linux/bpf.h | 1 + include/net/bpf_sk_storage.h | 27 ++++ include/uapi/linux/sock_diag.h | 26 +++ kernel/bpf/syscall.c | 15 ++ net/core/bpf_sk_storage.c | 283 ++++++++++++++++++++++++++++++++- 5 files changed, 346 insertions(+), 6 deletions(-)
include/linux/bpf.h +1 -0
@@ -1023,6 +1023,7 @@ void __bpf_free_used_maps(struct bpf_prog_aux *aux,void bpf_prog_free_id (struct bpf_prog *prog,bool do_idr_lock);void bpf_map_free_id (struct bpf_map *map,bool do_idr_lock); +struct bpf_map *bpf_map_get (u32 ufd);struct bpf_map *bpf_map_get_with_uref (u32 ufd);struct bpf_map *__bpf_map_get (struct fd f);void bpf_map_inc (struct bpf_map *map);
include/net/bpf_sk_storage.h +27 -0
@@ -10,14 +10,41 @@ void bpf_sk_storage_free(struct sock *sk);extern const struct bpf_func_proto bpf_sk_storage_get_proto;extern const struct bpf_func_proto bpf_sk_storage_delete_proto; +struct bpf_sk_storage_diag ; +struct sk_buff ; +struct nlattr ; +struct sock ; +#ifdef CONFIG_BPF_SYSCALL int bpf_sk_storage_clone (const struct sock *sk,struct sock *newsk); +struct bpf_sk_storage_diag * +bpf_sk_storage_diag_alloc (const struct nlattr *nla_stgs); +void bpf_sk_storage_diag_free (struct bpf_sk_storage_diag *diag); +int bpf_sk_storage_diag_put (struct bpf_sk_storage_diag *diag, +struct sock *sk,struct sk_buff *skb, +int stg_array_type, +unsigned int *res_diag_size);#else static inline int bpf_sk_storage_clone (const struct sock *sk,struct sock *newsk) {return 0 ; } +static inline struct bpf_sk_storage_diag * +bpf_sk_storage_diag_alloc (const struct nlattr *nla) +{ +return NULL ; +} +static inline void bpf_sk_storage_diag_free (struct bpf_sk_storage_diag *diag) +{ +} +static inline int bpf_sk_storage_diag_put (struct bpf_sk_storage_diag *diag, +struct sock *sk,struct sk_buff *skb, +int stg_array_type, +unsigned int *res_diag_size) +{ +return 0 ; +}#endif #endif /* _BPF_SK_STORAGE_H */
include/uapi/linux/sock_diag.h +26 -0
@@ -36,4 +36,30 @@ enum sknetlink_groups { };#define SKNLGRP_MAX (__SKNLGRP_MAX - 1) +enum { +SK_DIAG_BPF_STORAGE_REQ_NONE , +SK_DIAG_BPF_STORAGE_REQ_MAP_FD , + __SK_DIAG_BPF_STORAGE_REQ_MAX, +}; + +#define SK_DIAG_BPF_STORAGE_REQ_MAX (__SK_DIAG_BPF_STORAGE_REQ_MAX - 1) + +enum { +SK_DIAG_BPF_STORAGE_REP_NONE , +SK_DIAG_BPF_STORAGE , + __SK_DIAG_BPF_STORAGE_REP_MAX, +}; + +#define SK_DIAB_BPF_STORAGE_REP_MAX (__SK_DIAG_BPF_STORAGE_REP_MAX - 1) + +enum { +SK_DIAG_BPF_STORAGE_NONE , +SK_DIAG_BPF_STORAGE_PAD , +SK_DIAG_BPF_STORAGE_MAP_ID , +SK_DIAG_BPF_STORAGE_MAP_VALUE , + __SK_DIAG_BPF_STORAGE_MAX, +}; + +#define SK_DIAG_BPF_STORAGE_MAX (__SK_DIAG_BPF_STORAGE_MAX - 1) +#endif /* _UAPI__SOCK_DIAG_H__ */
kernel/bpf/syscall.c +15 -0
@@ -902,6 +902,21 @@ void bpf_map_inc_with_uref(struct bpf_map *map) }EXPORT_SYMBOL_GPL (bpf_map_inc_with_uref); +struct bpf_map *bpf_map_get (u32 ufd) +{ +struct fd f =fdget (ufd); +struct bpf_map *map; + + map =__bpf_map_get (f); +if (IS_ERR (map)) +return map; + +bpf_map_inc (map); +fdput (f); + +return map; +} +struct bpf_map *bpf_map_get_with_uref (u32 ufd) {struct fd f =fdget (ufd);
net/core/bpf_sk_storage.c +277 -6
304 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v2 4/4 bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
In reply to Martin KaFai LauThis patch will dump out the bpf_sk_storages of a sk if the request has the INET_DIAG_REQ_SK_BPF_STORAGES nlattr.
An array of SK_DIAG_BPF_STORAGE_REQ_MAP_FD can be specified in INET_DIAG_REQ_SK_BPF_STORAGES to select which bpf_sk_storage to dump. If no map_fd is specified, all bpf_sk_storages of a sk will be dumped.
bpf_sk_storages can be added to the system at runtime. It is difficult to find a proper static value for cb->min_dump_alloc.
This patch learns the nlattr size required to dump the bpf_sk_storages of a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the needed bpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".
Instead of expanding it in inet_sk_diag_fill(), it is expanded at a sleepable context in __inet_diag_dump() so __GFP_DIRECT_RECLAIM can be used. In __inet_diag_dump(), it will retry as long as the skb is empty and the cb->min_dump_alloc becomes larger than before. cb->min_dump_alloc is bounded by KMALLOC_MAX_SIZE. The min_dump_alloc is also changed from 'u16' to 'u32' to accommodate a sk that may have a few large bpf_sk_storages.
The updated cb->min_dump_alloc will also be used to allocate the skb in the next dump. This logic already exists in netlink_dump().
Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989
bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ]
[root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]
--- include/linux/inet_diag.h | 4 ++ include/linux/netlink.h | 4 +- include/uapi/linux/inet_diag.h | 2 + net/ipv4/inet_diag.c | 74 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-)
include/linux/inet_diag.h +4 -0
@@ -38,9 +38,13 @@ struct inet_diag_handler {__u16 idiag_info_size; }; +struct bpf_sk_storage_diag ;struct inet_diag_dump_data {struct nlattr *req_nlas[__INET_DIAG_REQ_MAX];#define inet_diag_nla_bc req_nlas[INET_DIAG_REQ_BYTECODE] +#define inet_diag_nla_bpf_stgs req_nlas[INET_DIAG_REQ_SK_BPF_STORAGES] + +struct bpf_sk_storage_diag *bpf_stg_diag; };struct inet_connection_sock ;
include/linux/netlink.h +2 -2
@@ -188,10 +188,10 @@ struct netlink_callback {struct module *module;struct netlink_ext_ack *extack;u16 family; -u16 min_dump_alloc; -bool strict_check;u16 answer_flags; +u32 min_dump_alloc;unsigned int prev_seq, seq; +bool strict_check;union {u8 ctx[48 ];
include/uapi/linux/inet_diag.h +2 -0
@@ -64,6 +64,7 @@ struct inet_diag_req_raw {enum {INET_DIAG_REQ_NONE ,INET_DIAG_REQ_BYTECODE , +INET_DIAG_REQ_SK_BPF_STORAGES , __INET_DIAG_REQ_MAX, };
@@ -155,6 +156,7 @@ enum {INET_DIAG_CLASS_ID ,/* request as INET_DIAG_TCLASS */ INET_DIAG_MD5SIG ,INET_DIAG_ULP_INFO , +INET_DIAG_SK_BPF_STORAGES , __INET_DIAG_MAX, };
net/ipv4/inet_diag.c +74 -0
130 lines not on this page · raw message
Signature · 1 line
2.17.1
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 4/4] bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
Quoted 48 lines · Martin KaFai Lau
This patch will dump out the
bpf_sk_storagesof a sk if the request has theINET_DIAG_REQ_SK_BPF_STORAGESnlattr.An array of
SK_DIAG_BPF_STORAGE_REQ_MAP_FDcan be specified inINET_DIAG_REQ_SK_BPF_STORAGESto select whichbpf_sk_storageto dump. If nomap_fdis specified, allbpf_sk_storagesof a sk will be dumped.
bpf_sk_storagescan be added to the system at runtime. It is difficult to find a proper static value forcb->min_dump_alloc.This patch learns the nlattr size required to dump the
bpf_sk_storagesof a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the neededbpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".Instead of expanding it in
inet_sk_diag_fill(), it is expanded at a sleepable context in__inet_diag_dump()so__GFP_DIRECT_RECLAIMcan be used. In__inet_diag_dump(), it will retry as long as the skb is empty and thecb->min_dump_allocbecomes larger than before.cb->min_dump_allocis bounded byKMALLOC_MAX_SIZE. Themin_dump_allocis also changed from 'u16' to 'u32' to accommodate a sk that may have a few largebpf_sk_storages.The updated
cb->min_dump_allocwill also be used to allocate the skb in the next dump. This logic already exists innetlink_dump().Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ][root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]
Quoted 2 lines
Acked-by Song Liu <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>
Hmm, the whole approach is not too pleasant to be honest. I can see why you need it since the regular sk_storage lookup only takes sock fd as a key and you don't have it otherwise available from outside, but then dumping up to KMALLOC_MAX_SIZE via netlink skb is not a great experience either. :( Also, are we planning to add the BTF dump there in addition to bpftool? Thus resulting in two different lookup APIs and several tools needed for introspection instead of one? :/ Also, how do we dump task local storage maps in future? Does it need a third lookup interface?
In your commit logs I haven't read on other approaches and why they won't work; I was wondering, given sockets are backed by inodes, couldn't we have a variant of iget_locked() (minus the alloc_inode() part from there) where you pass in ino number to eventually get to the socket and then dump the map value associated with it the regular way from bpf() syscall?
Thanks,
Daniel
Re: [PATCH bpf-next v2 4/4] bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
Quoted 60 lines · Daniel Borkmann
Quoted 48 lines · Martin KaFai Lau
This patch will dump out the
bpf_sk_storagesof a sk if the request has theINET_DIAG_REQ_SK_BPF_STORAGESnlattr.An array of
SK_DIAG_BPF_STORAGE_REQ_MAP_FDcan be specified inINET_DIAG_REQ_SK_BPF_STORAGESto select whichbpf_sk_storageto dump. If nomap_fdis specified, allbpf_sk_storagesof a sk will be dumped.
bpf_sk_storagescan be added to the system at runtime. It is difficult to find a proper static value forcb->min_dump_alloc.This patch learns the nlattr size required to dump the
bpf_sk_storagesof a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the neededbpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".Instead of expanding it in
inet_sk_diag_fill(), it is expanded at a sleepable context in__inet_diag_dump()so__GFP_DIRECT_RECLAIMcan be used. In__inet_diag_dump(), it will retry as long as the skb is empty and thecb->min_dump_allocbecomes larger than before.cb->min_dump_allocis bounded byKMALLOC_MAX_SIZE. Themin_dump_allocis also changed from 'u16' to 'u32' to accommodate a sk that may have a few largebpf_sk_storages.The updated
cb->min_dump_allocwill also be used to allocate the skb in the next dump. This logic already exists innetlink_dump().Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ][root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]Quoted 2 lines
Acked-by Song Liu <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>Hmm, the whole approach is not too pleasant to be honest. I can see why you need it since the regular
sk_storagelookup only takes sock fd as a key and you don't have it otherwise available from outside, but then dumping up toKMALLOC_MAX_SIZEvia netlink skb is not a great experience either. :( Also, are we planning to add the BTF dump there in addition to bpftool? Thus resulting in two different lookup APIs and several tools needed for introspection instead of one? :/ Also, how do we dump task local storage maps in future? Does it need a third lookup interface?
Quoted 5 lines
In your commit logs I haven't read on other approaches and why they won't work; I was wondering, given sockets are backed by inodes, couldn't we have a variant of
iget_locked()(minus thealloc_inode()part from there) where you pass in ino number to eventually get to the socket and then dump the map value associated with it the regular way frombpf()syscall?
Thanks for the feedback!
I think (1) dumping all sk(s) in a system is different from
(2) dumping all sk of a bpf_sk_storage_map or lookup a particular sk from a bpf_sk_storage_map.
This patch is doing (1). I believe it is useful to make the commonly used tools like "ss" (which already shows many useful information of a sk) to be able to introspect a kernel struct extended by bpf instead of limiting to only the bpftool can show the bpf extended data. The plan is to move the bpftool/btf_dumper.c to libbpf. The libbpf's btf_dumper print out format is still TBD and the current target is the drgn like format instead of the current semi-json like plain-txt printout. As more kernel struct may be extensible by bpf, having it in libbpf will be useful going forward.
Re: future kernel struct extended by bpf
For doing (1), I think we can ride on the existing API to iterate them also. bpf can extend a kernel struct but should not stop the current iteration API from working or seeing them. That includes the current seq_file API. The mid-term goal is to extend the seq_file by attaching a bpf_prog to (e.g. /proc/net/tcp) to do filtering
and printing. Yonghong is looking into that.
Re: lookup a bpf_sk_storage_map by socket fd and KMALLOC_MAX_SIZE In my config,sizeof(tcp_sock) is 2076
1 MAX sk_storage is 65407 (31x of a tcp_sock)KMALLOC_MAX_SIZE is 4194304 (2000x of a tcp_sock)
It is a lot of data to extend on a tcp_sock.
The total bpf_sk_storages that a sk could have is further bounded by net.core.optmem_max. It is a very unusual setup to have a sk that has so many bpf_sk_storage data that a skb cannot accommodate.
That said, the current sk_storage_update() does not limit the total bpf_sk_storages of a sk to KMALLOC_MAX_SIZE.
I think this limit could be added to the update side now and the chance of breaking is very minimal.
or the netlink can return map_id only when the max-sized skb cannot fit all the bpf_sk_storages. The userspace then do another syscall to lookup the data from each individual bpf_sk_storage_map and that requires to lookup side support with another key (non-fd). IMO, it is weird and a bit opposite of what bpf_sk_storage should be (fast bpf_sk_storage lookup while holding a sk). The iteration API already holds the sk but instead it is asking the usespace to go back to find out the sk again in order to get the bpf_sk_storages. I think that should be avoided if possible.
Regarding i_ino, after looking at sock_alloc() and get_next_ino(), hmmm...is it unique?
If it is, what is the different usecase between i_ino and sk->sk_cookie?
Re: [PATCH bpf-next v2 4/4] bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
Quoted 82 lines · Martin KaFai Lau
Quoted 60 lines · Daniel Borkmann
Quoted 48 lines · Martin KaFai Lau
This patch will dump out the
bpf_sk_storagesof a sk if the request has theINET_DIAG_REQ_SK_BPF_STORAGESnlattr.An array of
SK_DIAG_BPF_STORAGE_REQ_MAP_FDcan be specified inINET_DIAG_REQ_SK_BPF_STORAGESto select whichbpf_sk_storageto dump. If nomap_fdis specified, allbpf_sk_storagesof a sk will be dumped.
bpf_sk_storagescan be added to the system at runtime. It is difficult to find a proper static value forcb->min_dump_alloc.This patch learns the nlattr size required to dump the
bpf_sk_storagesof a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the neededbpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".Instead of expanding it in
inet_sk_diag_fill(), it is expanded at a sleepable context in__inet_diag_dump()so__GFP_DIRECT_RECLAIMcan be used. In__inet_diag_dump(), it will retry as long as the skb is empty and thecb->min_dump_allocbecomes larger than before.cb->min_dump_allocis bounded byKMALLOC_MAX_SIZE. Themin_dump_allocis also changed from 'u16' to 'u32' to accommodate a sk that may have a few largebpf_sk_storages.The updated
cb->min_dump_allocwill also be used to allocate the skb in the next dump. This logic already exists innetlink_dump().Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ][root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]Quoted 2 lines
Acked-by Song Liu <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>Hmm, the whole approach is not too pleasant to be honest. I can see why you need it since the regular
sk_storagelookup only takes sock fd as a key and you don't have it otherwise available from outside, but then dumping up toKMALLOC_MAX_SIZEvia netlink skb is not a great experience either. :( Also, are we planning to add the BTF dump there in addition to bpftool? Thus resulting in two different lookup APIs and several tools needed for introspection instead of one? :/ Also, how do we dump task local storage maps in future? Does it need a third lookup interface?Quoted 5 lines
In your commit logs I haven't read on other approaches and why they won't work; I was wondering, given sockets are backed by inodes, couldn't we have a variant of
iget_locked()(minus thealloc_inode()part from there) where you pass in ino number to eventually get to the socket and then dump the map value associated with it the regular way frombpf()syscall?Thanks for the feedback!
I think (1) dumping all sk(s) in a system is different from
(2) dumping all sk of abpf_sk_storage_mapor lookup a particular sk from abpf_sk_storage_map.This patch is doing (1). I believe it is useful to make the commonly used tools like "ss" (which already shows many useful information of a sk) to be able to introspect a kernel struct extended by bpf instead of limiting to only the bpftool can show the bpf extended data. The plan is to move the
bpftool/btf_dumper.cto libbpf. The libbpf'sbtf_dumperprint out format is still TBD and the current target is the drgn like format instead of the current semi-json like plain-txt printout. As more kernel struct may be extensible by bpf, having it in libbpf will be useful going forward.
Quoted 7 lines
Re: future kernel struct extended by bpf
For doing (1), I think we can ride on the existing API to iterate them also. bpf can extend a kernel struct but should not stop the current iteration API from working or seeing them. That includes the currentseq_fileAPI. The mid-term goal is to extend theseq_fileby attaching abpf_progto (e.g./proc/net/tcp) to do filtering
and printing. Yonghong is looking into that.
Right. I am working on a design and prototype to use bpf programs to dump certain kernel internal data structures. The high level idea is to create some /sys path to encode "what" to dump, e.g., /sys/kernel/bpfdump/tcp6_sockets/ for tcp6_sockets and "add" bpf program(s) to that path to specify "how" to dump.
I hope to share the design and prototype soon.
Daniel Borkmann
maintainerRe: [PATCH bpf-next v2 4/4] bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
In reply to Martin KaFai LauQuoted 66 lines · Martin KaFai Lau
Quoted 60 lines · Daniel Borkmann
Quoted 48 lines · Martin KaFai Lau
This patch will dump out the
bpf_sk_storagesof a sk if the request has theINET_DIAG_REQ_SK_BPF_STORAGESnlattr.An array of
SK_DIAG_BPF_STORAGE_REQ_MAP_FDcan be specified inINET_DIAG_REQ_SK_BPF_STORAGESto select whichbpf_sk_storageto dump. If nomap_fdis specified, allbpf_sk_storagesof a sk will be dumped.
bpf_sk_storagescan be added to the system at runtime. It is difficult to find a proper static value forcb->min_dump_alloc.This patch learns the nlattr size required to dump the
bpf_sk_storagesof a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the neededbpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".Instead of expanding it in
inet_sk_diag_fill(), it is expanded at a sleepable context in__inet_diag_dump()so__GFP_DIRECT_RECLAIMcan be used. In__inet_diag_dump(), it will retry as long as the skb is empty and thecb->min_dump_allocbecomes larger than before.cb->min_dump_allocis bounded byKMALLOC_MAX_SIZE. Themin_dump_allocis also changed from 'u16' to 'u32' to accommodate a sk that may have a few largebpf_sk_storages.The updated
cb->min_dump_allocwill also be used to allocate the skb in the next dump. This logic already exists innetlink_dump().Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ][root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]Quoted 2 lines
Acked-by Song Liu <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>Hmm, the whole approach is not too pleasant to be honest. I can see why you need it since the regular
sk_storagelookup only takes sock fd as a key and you don't have it otherwise available from outside, but then dumping up toKMALLOC_MAX_SIZEvia netlink skb is not a great experience either. :( Also, are we planning to add the BTF dump there in addition to bpftool? Thus resulting in two different lookup APIs and several tools needed for introspection instead of one? :/ Also, how do we dump task local storage maps in future? Does it need a third lookup interface?Quoted 5 lines
In your commit logs I haven't read on other approaches and why they won't work; I was wondering, given sockets are backed by inodes, couldn't we have a variant of
iget_locked()(minus thealloc_inode()part from there) where you pass in ino number to eventually get to the socket and then dump the map value associated with it the regular way frombpf()syscall?
Quoted 5 lines
Thanks for the feedback!
I think (1) dumping all sk(s) in a system is different from
(2) dumping all sk of abpf_sk_storage_mapor lookup a particular sk from abpf_sk_storage_map.
Yeah, it is; I was mostly brain-storming if there is a cleaner way for (1) by having (2)b resolved as an intermediate step (1) can then build on, but seems it's tricky w/o much extra infra.
[...]
Quoted 11 lines
or the netlink can return
map_idonly when the max-sized skb cannot fit all thebpf_sk_storages. The userspace then do another syscall to lookup the data from each individualbpf_sk_storage_mapand that requires to lookup side support with another key (non-fd). IMO, it is weird and a bit opposite of whatbpf_sk_storageshould be (fastbpf_sk_storagelookup while holding a sk). The iteration API already holds the sk but instead it is asking the usespace to go back to find out the sk again in order to get thebpf_sk_storages. I think that should be avoided if possible.
Quoted 2 lines
Regarding
i_ino, after looking atsock_alloc()andget_next_ino(), hmmm...is it unique?
It would wrap around after 2^32 allocations, so scratch that thought, iget_locked()/find_inode_fast()-based lookup usage must ensure it's unique.
Quoted 2 lines
If it is, what is the different usecase between
i_inoandsk->sk_cookie?
Agree that advantage of reusing diag is that you already hold the sk and are able to iterate through all of them, the ugly part is having to place the value data into netlink as an API along with all other socket data as a, for better or worse, bpf sk map lookup/introspection interface given there is no other way to have a fast and global (non-fd based) id->socket lookup interface that we could reuse atm.
I was wondering about sk->sk_cookie as well, but it wouldn't make sense to do a ss dump, get (sk cookie, map_id) from the dump and use that for a bpf() lookup if we need to reiterate the diag tables once again in the background just to get the storage data. And I presume it won't make sense either to reuse the diag's walk as a stand-alone for a bpf sk storage dump interface API via bpf() ... at least from ss tool side it would require a correlation based on sk cookie. Not nice either ... so current approach might indeed be the tradeoff. :/
Thanks,
Daniel
Re: [PATCH bpf-next v2 4/4] bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump()
Quoted 106 lines · Daniel Borkmann
Quoted 66 lines · Martin KaFai Lau
Quoted 60 lines · Daniel Borkmann
Quoted 48 lines · Martin KaFai Lau
This patch will dump out the
bpf_sk_storagesof a sk if the request has theINET_DIAG_REQ_SK_BPF_STORAGESnlattr.An array of
SK_DIAG_BPF_STORAGE_REQ_MAP_FDcan be specified inINET_DIAG_REQ_SK_BPF_STORAGESto select whichbpf_sk_storageto dump. If nomap_fdis specified, allbpf_sk_storagesof a sk will be dumped.
bpf_sk_storagescan be added to the system at runtime. It is difficult to find a proper static value forcb->min_dump_alloc.This patch learns the nlattr size required to dump the
bpf_sk_storagesof a sk. If it happens to be the very first nlmsg of a dump and it cannot fit the neededbpf_sk_storages, it will try to expand the skb by "pskb_expand_head()".Instead of expanding it in
inet_sk_diag_fill(), it is expanded at a sleepable context in__inet_diag_dump()so__GFP_DIRECT_RECLAIMcan be used. In__inet_diag_dump(), it will retry as long as the skb is empty and thecb->min_dump_allocbecomes larger than before.cb->min_dump_allocis bounded byKMALLOC_MAX_SIZE. Themin_dump_allocis also changed from 'u16' to 'u32' to accommodate a sk that may have a few largebpf_sk_storages.The updated
cb->min_dump_allocwill also be used to allocate the skb in the next dump. This logic already exists innetlink_dump().Here is the sample output of a locally modified 'ss' and it could be made more readable by using BTF later:
[root@arch-fb-vm1 ~]# ss --bpf-map-id 14 --bpf-map-id 13 -t6an 'dst [::1]:8989' State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
ESTAB 0 0 [::1]:51072 [::1]:8989bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ][root@arch-fb-vm1 ~]# ~/devshare/github/iproute2/misc/ss --bpf-maps -t6an 'dst [::1]:8989'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process ESTAB 0 0 [::1]:51072 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ] ESTAB 0 0 [::1]:51070 [::1]:8989 bpf_map_id:14 value:[ 3feb ] bpf_map_id:13 value:[ 3f ] bpf_map_id:12 value:[ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000... total:65407 ]Quoted 2 lines
Acked-by Song Liu <[email protected]>Signed-off-by Martin KaFai Lau <[email protected]>Hmm, the whole approach is not too pleasant to be honest. I can see why you need it since the regular
sk_storagelookup only takes sock fd as a key and you don't have it otherwise available from outside, but then dumping up toKMALLOC_MAX_SIZEvia netlink skb is not a great experience either. :( Also, are we planning to add the BTF dump there in addition to bpftool? Thus resulting in two different lookup APIs and several tools needed for introspection instead of one? :/ Also, how do we dump task local storage maps in future? Does it need a third lookup interface?Quoted 5 lines
In your commit logs I haven't read on other approaches and why they won't work; I was wondering, given sockets are backed by inodes, couldn't we have a variant of
iget_locked()(minus thealloc_inode()part from there) where you pass in ino number to eventually get to the socket and then dump the map value associated with it the regular way frombpf()syscall?Quoted 5 lines
Thanks for the feedback!
I think (1) dumping all sk(s) in a system is different from
(2) dumping all sk of abpf_sk_storage_mapor lookup a particular sk from abpf_sk_storage_map.Yeah, it is; I was mostly brain-storming if there is a cleaner way for (1) by having (2)b resolved as an intermediate step (1) can then build on, but seems it's tricky w/o much extra infra.
[...]
Quoted 11 lines
or the netlink can return
map_idonly when the max-sized skb cannot fit all thebpf_sk_storages. The userspace then do another syscall to lookup the data from each individualbpf_sk_storage_mapand that requires to lookup side support with another key (non-fd). IMO, it is weird and a bit opposite of whatbpf_sk_storageshould be (fastbpf_sk_storagelookup while holding a sk). The iteration API already holds the sk but instead it is asking the usespace to go back to find out the sk again in order to get thebpf_sk_storages. I think that should be avoided if possible.Quoted 2 lines
Regarding
i_ino, after looking atsock_alloc()andget_next_ino(), hmmm...is it unique?It would wrap around after 2^32 allocations, so scratch that thought, iget_locked()/find_inode_fast()-based lookup usage must ensure it's unique.
Quoted 2 lines
If it is, what is the different usecase between
i_inoandsk->sk_cookie?Agree that advantage of reusing diag is that you already hold the sk and are able to iterate through all of them, the ugly part is having to place the value data into netlink as an API along with all other socket data as a, for better or worse, bpf sk map lookup/introspection interface given there is no other way to have a fast and global (non-fd based)
id->socketlookup interface that we could reuse atm.
Quoted 8 lines
I was wondering about
sk->sk_cookieas well, but it wouldn't make sense to do a ss dump, get (sk cookie,map_id) from the dump and use that for abpf()lookup if we need to reiterate the diag tables once again in the background just to get the storage data. And I presume it won't make sense either to reuse the diag's walk as a stand-alone for a bpf sk storage dump interface API viabpf()... at least from ss tool side it would require a correlation based on sk cookie. Not nice either ... so current approach might indeed be the tradeoff. :/
I agree with all the points above.
I've applied this set. I think making ss show it via standard way is necessary. We should continue thinking on other options.