• Martin KaFai Lau's avatar
    bpf: inet_diag: Dump bpf_sk_storages in inet_diag_dump() · 085c20ca
    Martin KaFai Lau authored
    This 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 ]
    Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Acked-by: default avatarSong Liu <songliubraving@fb.com>
    Link: https://lore.kernel.org/bpf/20200225230427.1976129-1-kafai@fb.com
    085c20ca
inet_diag.c 32.9 KB