mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	bpf: sockmap: Add UDP support
Allow adding hashed UDP sockets to sockmaps. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200309111243.6982-9-lmb@cloudflare.com
This commit is contained in:
		
							parent
							
								
									edc6741cc6
								
							
						
					
					
						commit
						7b98cd42b0
					
				
					 1 changed files with 33 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -11,6 +11,7 @@
 | 
			
		|||
#include <linux/list.h>
 | 
			
		||||
#include <linux/jhash.h>
 | 
			
		||||
#include <linux/sock_diag.h>
 | 
			
		||||
#include <net/udp.h>
 | 
			
		||||
 | 
			
		||||
struct bpf_stab {
 | 
			
		||||
	struct bpf_map map;
 | 
			
		||||
| 
						 | 
				
			
			@ -147,7 +148,19 @@ static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
 | 
			
		|||
 | 
			
		||||
	sock_owned_by_me(sk);
 | 
			
		||||
 | 
			
		||||
	prot = tcp_bpf_get_proto(sk, psock);
 | 
			
		||||
	switch (sk->sk_type) {
 | 
			
		||||
	case SOCK_STREAM:
 | 
			
		||||
		prot = tcp_bpf_get_proto(sk, psock);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	case SOCK_DGRAM:
 | 
			
		||||
		prot = udp_bpf_get_proto(sk, psock);
 | 
			
		||||
		break;
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (IS_ERR(prot))
 | 
			
		||||
		return PTR_ERR(prot);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -162,7 +175,7 @@ static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
 | 
			
		|||
	rcu_read_lock();
 | 
			
		||||
	psock = sk_psock(sk);
 | 
			
		||||
	if (psock) {
 | 
			
		||||
		if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
 | 
			
		||||
		if (sk->sk_prot->close != sock_map_close) {
 | 
			
		||||
			psock = ERR_PTR(-EBUSY);
 | 
			
		||||
			goto out;
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -474,15 +487,31 @@ static bool sock_map_op_okay(const struct bpf_sock_ops_kern *ops)
 | 
			
		|||
	       ops->op == BPF_SOCK_OPS_TCP_LISTEN_CB;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool sock_map_sk_is_suitable(const struct sock *sk)
 | 
			
		||||
static bool sk_is_tcp(const struct sock *sk)
 | 
			
		||||
{
 | 
			
		||||
	return sk->sk_type == SOCK_STREAM &&
 | 
			
		||||
	       sk->sk_protocol == IPPROTO_TCP;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool sk_is_udp(const struct sock *sk)
 | 
			
		||||
{
 | 
			
		||||
	return sk->sk_type == SOCK_DGRAM &&
 | 
			
		||||
	       sk->sk_protocol == IPPROTO_UDP;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool sock_map_sk_is_suitable(const struct sock *sk)
 | 
			
		||||
{
 | 
			
		||||
	return sk_is_tcp(sk) || sk_is_udp(sk);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool sock_map_sk_state_allowed(const struct sock *sk)
 | 
			
		||||
{
 | 
			
		||||
	return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
 | 
			
		||||
	if (sk_is_tcp(sk))
 | 
			
		||||
		return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN);
 | 
			
		||||
	else if (sk_is_udp(sk))
 | 
			
		||||
		return sk_hashed(sk);
 | 
			
		||||
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sock_map_update_elem(struct bpf_map *map, void *key,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue