• Martin KaFai Lau's avatar
    bpf: tcp: Bpf iter batching and lock_sock · 04c7820b
    Martin KaFai Lau authored
    This patch does batching and lock_sock for the bpf tcp iter.
    It does not affect the proc fs iteration.
    
    With bpf-tcp-cc, new algo rollout happens more often.  Instead of
    restarting the application to pick up the new tcp-cc, the next patch
    will allow bpf iter to do setsockopt(TCP_CONGESTION).  This requires
    locking the sock.
    
    Also, unlike the proc iteration (cat /proc/net/tcp[6]), the bpf iter
    can inspect all fields of a tcp_sock.  It will be useful to have a
    consistent view on some of the fields (e.g. the ones reported in
    tcp_get_info() that also acquires the sock lock).
    
    Double lock: locking the bucket first and then locking the sock could
    lead to deadlock.  This patch takes a batching approach similar to
    inet_diag.  While holding the bucket lock, it batch a number of sockets
    into an array first and then unlock the bucket.  Before doing show(),
    it then calls lock_sock_fast().
    
    In a machine with ~400k connections, the maximum number of
    sk in a bucket of the established hashtable is 7.  0.02% of
    the established connections fall into this bucket size.
    
    For listen hash (port+addr lhash2), the bucket is usually very
    small also except for the SO_REUSEPORT use case which the
    userspace could have one SO_REUSEPORT socket per thread.
    
    While batching is used, it can also minimize the chance of missing
    sock in the setsockopt use case if the whole bucket is batched.
    This patch will start with a batch array with INIT_BATCH_SZ (16)
    which will be enough for the most common cases.  bpf_iter_tcp_batch()
    will try to realloc to a larger array to handle exception case (e.g.
    the SO_REUSEPORT case in the lhash2).
    Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
    Acked-by: default avatarKuniyuki Iwashima <kuniyu@amazon.co.jp>
    Acked-by: default avatarYonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/bpf/20210701200613.1036157-1-kafai@fb.com
    04c7820b
tcp_ipv4.c 85.1 KB