Skip to content

Commit be9f4a4

Browse files
edumazetdavem330
authored andcommitted
ipv4: tcp: remove per net tcp_sock
tcp_v4_send_reset() and tcp_v4_send_ack() use a single socket per network namespace. This leads to bad behavior on multiqueue NICS, because many cpus contend for the socket lock and once socket lock is acquired, extra false sharing on various socket fields slow down the operations. To better resist to attacks, we use a percpu socket. Each cpu can run without contention, using appropriate memory (local node) Additional features : 1) We also mirror the queue_mapping of the incoming skb, so that answers use the same queue if possible. 2) Setting SOCK_USE_WRITE_QUEUE socket flag speedup sock_wfree() 3) We now limit the number of in-flight RST/ACK [1] packets per cpu, instead of per namespace, and we honor the sysctl_wmem_default limit dynamically. (Prior to this patch, sysctl_wmem_default value was copied at boot time, so any further change would not affect tcp_sock limit) [1] These packets are only generated when no socket was matched for the incoming packet. Reported-by: Bill Sommerfeld <wsommerfeld@google.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Tom Herbert <therbert@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent aee06da commit be9f4a4

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

‎include/net/ip.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
158158
return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
159159
}
160160

161-
void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, __be32 daddr,
161+
void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr,
162162
__be32 saddr, const struct ip_reply_arg *arg,
163163
unsigned int len);
164164

‎include/net/netns/ipv4.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ struct netns_ipv4 {
3838
struct sock *fibnl;
3939

4040
struct sock **icmp_sk;
41-
struct sock *tcp_sock;
4241
struct inet_peer_base *peers;
4342
struct tcpm_hash_bucket *tcp_metrics_hash;
4443
unsigned int tcp_metrics_hash_mask;

‎net/ipv4/ip_output.c‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,20 +1463,33 @@ static int ip_reply_glue_bits(void *dptr, char *to, int offset,
14631463

14641464
/*
14651465
* Generic function to send a packet as reply to another packet.
1466-
* Used to send TCP resets so far.
1466+
* Used to send some TCP resets/acks so far.
14671467
*
1468-
* Should run single threaded per socket because it uses the sock
1469-
* structure to pass arguments.
1468+
* Use a fake percpu inet socket to avoid false sharing and contention.
14701469
*/
1471-
void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, __be32 daddr,
1470+
static DEFINE_PER_CPU(struct inet_sock, unicast_sock) = {
1471+
.sk = {
1472+
.__sk_common = {
1473+
.skc_refcnt = ATOMIC_INIT(1),
1474+
},
1475+
.sk_wmem_alloc = ATOMIC_INIT(1),
1476+
.sk_allocation = GFP_ATOMIC,
1477+
.sk_flags = (1UL << SOCK_USE_WRITE_QUEUE),
1478+
},
1479+
.pmtudisc = IP_PMTUDISC_WANT,
1480+
};
1481+
1482+
void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr,
14721483
__be32 saddr, const struct ip_reply_arg *arg,
14731484
unsigned int len)
14741485
{
1475-
struct inet_sock *inet = inet_sk(sk);
14761486
struct ip_options_data replyopts;
14771487
struct ipcm_cookie ipc;
14781488
struct flowi4 fl4;
14791489
struct rtable *rt = skb_rtable(skb);
1490+
struct sk_buff *nskb;
1491+
struct sock *sk;
1492+
struct inet_sock *inet;
14801493

14811494
if (ip_options_echo(&replyopts.opt.opt, skb))
14821495
return;
@@ -1494,38 +1507,39 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb, __be32 daddr,
14941507

14951508
flowi4_init_output(&fl4, arg->bound_dev_if, 0,
14961509
RT_TOS(arg->tos),
1497-
RT_SCOPE_UNIVERSE, sk->sk_protocol,
1510+
RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
14981511
ip_reply_arg_flowi_flags(arg),
14991512
daddr, saddr,
15001513
tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
15011514
security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1502-
rt = ip_route_output_key(sock_net(sk), &fl4);
1515+
rt = ip_route_output_key(net, &fl4);
15031516
if (IS_ERR(rt))
15041517
return;
15051518

1506-
/* And let IP do all the hard work.
1519+
inet = &get_cpu_var(unicast_sock);
15071520

1508-
This chunk is not reenterable, hence spinlock.
1509-
Note that it uses the fact, that this function is called
1510-
with locally disabled BH and that sk cannot be already spinlocked.
1511-
*/
1512-
bh_lock_sock(sk);
15131521
inet->tos = arg->tos;
1522+
sk = &inet->sk;
15141523
sk->sk_priority = skb->priority;
15151524
sk->sk_protocol = ip_hdr(skb)->protocol;
15161525
sk->sk_bound_dev_if = arg->bound_dev_if;
1526+
sock_net_set(sk, net);
1527+
__skb_queue_head_init(&sk->sk_write_queue);
1528+
sk->sk_sndbuf = sysctl_wmem_default;
15171529
ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base, len, 0,
15181530
&ipc, &rt, MSG_DONTWAIT);
1519-
if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
1531+
nskb = skb_peek(&sk->sk_write_queue);
1532+
if (nskb) {
15201533
if (arg->csumoffset >= 0)
1521-
*((__sum16 *)skb_transport_header(skb) +
1522-
arg->csumoffset) = csum_fold(csum_add(skb->csum,
1534+
*((__sum16 *)skb_transport_header(nskb) +
1535+
arg->csumoffset) = csum_fold(csum_add(nskb->csum,
15231536
arg->csum));
1524-
skb->ip_summed = CHECKSUM_NONE;
1537+
nskb->ip_summed = CHECKSUM_NONE;
1538+
skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
15251539
ip_push_pending_frames(sk, &fl4);
15261540
}
15271541

1528-
bh_unlock_sock(sk);
1542+
put_cpu_var(unicast_sock);
15291543

15301544
ip_rt_put(rt);
15311545
}

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
688688

689689
net = dev_net(skb_dst(skb)->dev);
690690
arg.tos = ip_hdr(skb)->tos;
691-
ip_send_unicast_reply(net->ipv4.tcp_sock, skb, ip_hdr(skb)->saddr,
691+
ip_send_unicast_reply(net, skb, ip_hdr(skb)->saddr,
692692
ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
693693

694694
TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
@@ -771,7 +771,7 @@ static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
771771
if (oif)
772772
arg.bound_dev_if = oif;
773773
arg.tos = tos;
774-
ip_send_unicast_reply(net->ipv4.tcp_sock, skb, ip_hdr(skb)->saddr,
774+
ip_send_unicast_reply(net, skb, ip_hdr(skb)->saddr,
775775
ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
776776

777777
TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
@@ -2624,13 +2624,11 @@ EXPORT_SYMBOL(tcp_prot);
26242624

26252625
static int __net_init tcp_sk_init(struct net *net)
26262626
{
2627-
return inet_ctl_sock_create(&net->ipv4.tcp_sock,
2628-
PF_INET, SOCK_RAW, IPPROTO_TCP, net);
2627+
return 0;
26292628
}
26302629

26312630
static void __net_exit tcp_sk_exit(struct net *net)
26322631
{
2633-
inet_ctl_sock_destroy(net->ipv4.tcp_sock);
26342632
}
26352633

26362634
static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)

0 commit comments

Comments
 (0)