• Martin KaFai Lau's avatar
    bpf: Add struct bpf_tcp_sock and BPF_FUNC_tcp_sock · 655a51e5
    Martin KaFai Lau authored
    This patch adds a helper function BPF_FUNC_tcp_sock and it
    is currently available for cg_skb and sched_(cls|act):
    
    struct bpf_tcp_sock *bpf_tcp_sock(struct bpf_sock *sk);
    
    int cg_skb_foo(struct __sk_buff *skb) {
    	struct bpf_tcp_sock *tp;
    	struct bpf_sock *sk;
    	__u32 snd_cwnd;
    
    	sk = skb->sk;
    	if (!sk)
    		return 1;
    
    	tp = bpf_tcp_sock(sk);
    	if (!tp)
    		return 1;
    
    	snd_cwnd = tp->snd_cwnd;
    	/* ... */
    
    	return 1;
    }
    
    A 'struct bpf_tcp_sock' is also added to the uapi bpf.h to provide
    read-only access.  bpf_tcp_sock has all the existing tcp_sock's fields
    that has already been exposed by the bpf_sock_ops.
    i.e. no new tcp_sock's fields are exposed in bpf.h.
    
    This helper returns a pointer to the tcp_sock.  If it is not a tcp_sock
    or it cannot be traced back to a tcp_sock by sk_to_full_sk(), it
    returns NULL.  Hence, the caller needs to check for NULL before
    accessing it.
    
    The current use case is to expose members from tcp_sock
    to allow a cg_skb_bpf_prog to provide per cgroup traffic
    policing/shaping.
    Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    655a51e5
verifier.c 227 KB