Skip to content

Commit d6732b9

Browse files
0x7f454c46davem330
authored andcommitted
net/tcp: Allow asynchronous delete for TCP-AO keys (MKTs)
Delete becomes very, very fast - almost free, but after setsockopt() syscall returns, the key is still alive until next RCU grace period. Which is fine for listen sockets as userspace needs to be aware of setsockopt(TCP_AO) and accept() race and resolve it with verification by getsockopt() after TCP connection was accepted. The benchmark results (on non-loaded box, worse with more RCU work pending): > ok 33 Worst case delete 16384 keys: min=5ms max=10ms mean=6.93904ms stddev=0.263421 > ok 34 Add a new key 16384 keys: min=1ms max=4ms mean=2.17751ms stddev=0.147564 > ok 35 Remove random-search 16384 keys: min=5ms max=10ms mean=6.50243ms stddev=0.254999 > ok 36 Remove async 16384 keys: min=0ms max=0ms mean=0.0296107ms stddev=0.0172078 Co-developed-by: Francesco Ruggeri <[email protected]> Signed-off-by: Francesco Ruggeri <[email protected]> Co-developed-by: Salam Noureddine <[email protected]> Signed-off-by: Salam Noureddine <[email protected]> Signed-off-by: Dmitry Safonov <[email protected]> Acked-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ef84703 commit d6732b9

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

include/uapi/linux/tcp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ struct tcp_ao_del { /* setsockopt(TCP_AO_DEL_KEY) */
396396
__s32 ifindex; /* L3 dev index for VRF */
397397
__u32 set_current :1, /* corresponding ::current_key */
398398
set_rnext :1, /* corresponding ::rnext */
399-
reserved :30; /* must be 0 */
399+
del_async :1, /* only valid for listen sockets */
400+
reserved :29; /* must be 0 */
400401
__u16 reserved2; /* padding, must be 0 */
401402
__u8 prefix; /* peer's address prefix */
402403
__u8 sndid; /* SendID for outgoing segments */

net/ipv4/tcp_ao.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,19 +1628,32 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
16281628
}
16291629

16301630
static int tcp_ao_delete_key(struct sock *sk, struct tcp_ao_info *ao_info,
1631-
struct tcp_ao_key *key,
1631+
bool del_async, struct tcp_ao_key *key,
16321632
struct tcp_ao_key *new_current,
16331633
struct tcp_ao_key *new_rnext)
16341634
{
16351635
int err;
16361636

16371637
hlist_del_rcu(&key->node);
16381638

1639+
/* Support for async delete on listening sockets: as they don't
1640+
* need current_key/rnext_key maintaining, we don't need to check
1641+
* them and we can just free all resources in RCU fashion.
1642+
*/
1643+
if (del_async) {
1644+
atomic_sub(tcp_ao_sizeof_key(key), &sk->sk_omem_alloc);
1645+
call_rcu(&key->rcu, tcp_ao_key_free_rcu);
1646+
return 0;
1647+
}
1648+
16391649
/* At this moment another CPU could have looked this key up
16401650
* while it was unlinked from the list. Wait for RCU grace period,
16411651
* after which the key is off-list and can't be looked up again;
16421652
* the rx path [just before RCU came] might have used it and set it
16431653
* as current_key (very unlikely).
1654+
* Free the key with next RCU grace period (in case it was
1655+
* current_key before tcp_ao_current_rnext() might have
1656+
* changed it in forced-delete).
16441657
*/
16451658
synchronize_rcu();
16461659
if (new_current)
@@ -1711,6 +1724,8 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family,
17111724
if (!new_rnext)
17121725
return -ENOENT;
17131726
}
1727+
if (cmd.del_async && sk->sk_state != TCP_LISTEN)
1728+
return -EINVAL;
17141729

17151730
if (family == AF_INET) {
17161731
struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.addr;
@@ -1758,8 +1773,8 @@ static int tcp_ao_del_cmd(struct sock *sk, unsigned short int family,
17581773
if (key == new_current || key == new_rnext)
17591774
continue;
17601775

1761-
return tcp_ao_delete_key(sk, ao_info, key,
1762-
new_current, new_rnext);
1776+
return tcp_ao_delete_key(sk, ao_info, cmd.del_async, key,
1777+
new_current, new_rnext);
17631778
}
17641779
return -ENOENT;
17651780
}

0 commit comments

Comments
 (0)