Commit 60fb9567 authored by Paolo Abeni's avatar Paolo Abeni Committed by David S. Miller

udp: implement complete book-keeping for encap_needed

The *encap_needed static keys are enabled by UDP tunnels
and several UDP encapsulations type, but they are never
turned off. This can cause unneeded overall performance
degradation for systems where such features are used
transiently.

This patch introduces complete book-keeping for such keys,
decreasing the usage at socket destruction time, if needed,
and avoiding that the same socket could increase the key
usage multiple times.

rfc v3 -> v1:
 - add socket lock around udp_tunnel_encap_enable()

rfc v2 -> rfc v3:
 - use udp_tunnel_encap_enable() in setsockopt()
Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 7e225619
...@@ -49,7 +49,12 @@ struct udp_sock { ...@@ -49,7 +49,12 @@ struct udp_sock {
unsigned int corkflag; /* Cork is required */ unsigned int corkflag; /* Cork is required */
__u8 encap_type; /* Is this an Encapsulation socket? */ __u8 encap_type; /* Is this an Encapsulation socket? */
unsigned char no_check6_tx:1,/* Send zero UDP6 checksums on TX? */ unsigned char no_check6_tx:1,/* Send zero UDP6 checksums on TX? */
no_check6_rx:1;/* Allow zero UDP6 checksums on RX? */ no_check6_rx:1,/* Allow zero UDP6 checksums on RX? */
encap_enabled:1; /* This socket enabled encap
* processing; UDP tunnels and
* different encapsulation layer set
* this
*/
/* /*
* Following member retains the information to create a UDP header * Following member retains the information to create a UDP header
* when the socket is uncorked. * when the socket is uncorked.
......
...@@ -165,6 +165,12 @@ static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum) ...@@ -165,6 +165,12 @@ static inline int udp_tunnel_handle_offloads(struct sk_buff *skb, bool udp_csum)
static inline void udp_tunnel_encap_enable(struct socket *sock) static inline void udp_tunnel_encap_enable(struct socket *sock)
{ {
struct udp_sock *up = udp_sk(sock->sk);
if (up->encap_enabled)
return;
up->encap_enabled = 1;
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
if (sock->sk->sk_family == PF_INET6) if (sock->sk->sk_family == PF_INET6)
ipv6_stub->udpv6_encap_enable(); ipv6_stub->udpv6_encap_enable();
......
...@@ -115,6 +115,7 @@ ...@@ -115,6 +115,7 @@
#include "udp_impl.h" #include "udp_impl.h"
#include <net/sock_reuseport.h> #include <net/sock_reuseport.h>
#include <net/addrconf.h> #include <net/addrconf.h>
#include <net/udp_tunnel.h>
struct udp_table udp_table __read_mostly; struct udp_table udp_table __read_mostly;
EXPORT_SYMBOL(udp_table); EXPORT_SYMBOL(udp_table);
...@@ -2395,11 +2396,15 @@ void udp_destroy_sock(struct sock *sk) ...@@ -2395,11 +2396,15 @@ void udp_destroy_sock(struct sock *sk)
bool slow = lock_sock_fast(sk); bool slow = lock_sock_fast(sk);
udp_flush_pending_frames(sk); udp_flush_pending_frames(sk);
unlock_sock_fast(sk, slow); unlock_sock_fast(sk, slow);
if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) { if (static_branch_unlikely(&udp_encap_needed_key)) {
void (*encap_destroy)(struct sock *sk); if (up->encap_type) {
encap_destroy = READ_ONCE(up->encap_destroy); void (*encap_destroy)(struct sock *sk);
if (encap_destroy) encap_destroy = READ_ONCE(up->encap_destroy);
encap_destroy(sk); if (encap_destroy)
encap_destroy(sk);
}
if (up->encap_enabled)
static_branch_disable(&udp_encap_needed_key);
} }
} }
...@@ -2444,7 +2449,9 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname, ...@@ -2444,7 +2449,9 @@ int udp_lib_setsockopt(struct sock *sk, int level, int optname,
/* FALLTHROUGH */ /* FALLTHROUGH */
case UDP_ENCAP_L2TPINUDP: case UDP_ENCAP_L2TPINUDP:
up->encap_type = val; up->encap_type = val;
udp_encap_enable(); lock_sock(sk);
udp_tunnel_encap_enable(sk->sk_socket);
release_sock(sk);
break; break;
default: default:
err = -ENOPROTOOPT; err = -ENOPROTOOPT;
......
...@@ -1456,11 +1456,15 @@ void udpv6_destroy_sock(struct sock *sk) ...@@ -1456,11 +1456,15 @@ void udpv6_destroy_sock(struct sock *sk)
udp_v6_flush_pending_frames(sk); udp_v6_flush_pending_frames(sk);
release_sock(sk); release_sock(sk);
if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) { if (static_branch_unlikely(&udpv6_encap_needed_key)) {
void (*encap_destroy)(struct sock *sk); if (up->encap_type) {
encap_destroy = READ_ONCE(up->encap_destroy); void (*encap_destroy)(struct sock *sk);
if (encap_destroy) encap_destroy = READ_ONCE(up->encap_destroy);
encap_destroy(sk); if (encap_destroy)
encap_destroy(sk);
}
if (up->encap_enabled)
static_branch_disable(&udpv6_encap_needed_key);
} }
inet6_destroy_sock(sk); inet6_destroy_sock(sk);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment