Skip to content

Commit 16cc7c2

Browse files
edumazetJiri Slaby
authored andcommitted
inetpeer: get rid of ip_id_count
[ Upstream commit 73f156a ] Ideally, we would need to generate IP ID using a per destination IP generator. linux kernels used inet_peer cache for this purpose, but this had a huge cost on servers disabling MTU discovery. 1) each inet_peer struct consumes 192 bytes 2) inetpeer cache uses a binary tree of inet_peer structs, with a nominal size of ~66000 elements under load. 3) lookups in this tree are hitting a lot of cache lines, as tree depth is about 20. 4) If server deals with many tcp flows, we have a high probability of not finding the inet_peer, allocating a fresh one, inserting it in the tree with same initial ip_id_count, (cf secure_ip_id()) 5) We garbage collect inet_peer aggressively. IP ID generation do not have to be 'perfect' Goal is trying to avoid duplicates in a short period of time, so that reassembly units have a chance to complete reassembly of fragments belonging to one message before receiving other fragments with a recycled ID. We simply use an array of generators, and a Jenkin hash using the dst IP as a key. ipv6_select_ident() is put back into net/ipv6/ip6_output.c where it belongs (it is only used from this file) secure_ip_id() and secure_ipv6_id() no longer are needed. Rename ip_select_ident_more() to ip_select_ident_segs() to avoid unnecessary decrement/increment of the number of segments. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
1 parent 9ac1f1f commit 16cc7c2

17 files changed

Lines changed: 78 additions & 142 deletions

File tree

‎drivers/net/ppp/pptp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static int pptp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
281281
nf_reset(skb);
282282

283283
skb->ip_summed = CHECKSUM_NONE;
284-
ip_select_ident(skb, &rt->dst, NULL);
284+
ip_select_ident(skb, NULL);
285285
ip_send_check(iph);
286286

287287
ip_local_out(skb);

‎include/net/inetpeer.h‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ struct inet_peer {
4141
struct rcu_head gc_rcu;
4242
};
4343
/*
44-
* Once inet_peer is queued for deletion (refcnt == -1), following fields
45-
* are not available: rid, ip_id_count
44+
* Once inet_peer is queued for deletion (refcnt == -1), following field
45+
* is not available: rid
4646
* We can share memory with rcu_head to help keep inet_peer small.
4747
*/
4848
union {
4949
struct {
5050
atomic_t rid; /* Frag reception counter */
51-
atomic_t ip_id_count; /* IP ID for the next packet */
5251
};
5352
struct rcu_head rcu;
5453
struct inet_peer *gc_next;
@@ -166,21 +165,12 @@ extern void inetpeer_invalidate_tree(struct inet_peer_base *);
166165
extern void inetpeer_invalidate_family(int family);
167166

168167
/*
169-
* temporary check to make sure we dont access rid, ip_id_count, tcp_ts,
168+
* temporary check to make sure we dont access rid, tcp_ts,
170169
* tcp_ts_stamp if no refcount is taken on inet_peer
171170
*/
172171
static inline void inet_peer_refcheck(const struct inet_peer *p)
173172
{
174173
WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
175174
}
176175

177-
178-
/* can be called with or without local BH being disabled */
179-
static inline int inet_getid(struct inet_peer *p, int more)
180-
{
181-
more++;
182-
inet_peer_refcheck(p);
183-
return atomic_add_return(more, &p->ip_id_count) - more;
184-
}
185-
186176
#endif /* _NET_INETPEER_H */

‎include/net/ip.h‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,19 @@ int ip_dont_fragment(struct sock *sk, struct dst_entry *dst)
262262
!(dst_metric_locked(dst, RTAX_MTU)));
263263
}
264264

265-
extern void __ip_select_ident(struct iphdr *iph, struct dst_entry *dst, int more);
265+
#define IP_IDENTS_SZ 2048u
266+
extern atomic_t *ip_idents;
266267

267-
static inline void ip_select_ident(struct sk_buff *skb, struct dst_entry *dst, struct sock *sk)
268+
static inline u32 ip_idents_reserve(u32 hash, int segs)
269+
{
270+
atomic_t *id_ptr = ip_idents + hash % IP_IDENTS_SZ;
271+
272+
return atomic_add_return(segs, id_ptr) - segs;
273+
}
274+
275+
void __ip_select_ident(struct iphdr *iph, int segs);
276+
277+
static inline void ip_select_ident_segs(struct sk_buff *skb, struct sock *sk, int segs)
268278
{
269279
struct iphdr *iph = ip_hdr(skb);
270280

@@ -274,24 +284,20 @@ static inline void ip_select_ident(struct sk_buff *skb, struct dst_entry *dst, s
274284
* does not change, they drop every other packet in
275285
* a TCP stream using header compression.
276286
*/
277-
iph->id = (sk && inet_sk(sk)->inet_daddr) ?
278-
htons(inet_sk(sk)->inet_id++) : 0;
279-
} else
280-
__ip_select_ident(iph, dst, 0);
281-
}
282-
283-
static inline void ip_select_ident_more(struct sk_buff *skb, struct dst_entry *dst, struct sock *sk, int more)
284-
{
285-
struct iphdr *iph = ip_hdr(skb);
286-
287-
if ((iph->frag_off & htons(IP_DF)) && !skb->local_df) {
288287
if (sk && inet_sk(sk)->inet_daddr) {
289288
iph->id = htons(inet_sk(sk)->inet_id);
290-
inet_sk(sk)->inet_id += 1 + more;
291-
} else
289+
inet_sk(sk)->inet_id += segs;
290+
} else {
292291
iph->id = 0;
293-
} else
294-
__ip_select_ident(iph, dst, more);
292+
}
293+
} else {
294+
__ip_select_ident(iph, segs);
295+
}
296+
}
297+
298+
static inline void ip_select_ident(struct sk_buff *skb, struct sock *sk)
299+
{
300+
ip_select_ident_segs(skb, sk, 1);
295301
}
296302

297303
/*

‎include/net/ipv6.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,19 @@ static inline u32 ipv6_addr_hash(const struct in6_addr *a)
537537
}
538538

539539
/* more secured version of ipv6_addr_hash() */
540-
static inline u32 ipv6_addr_jhash(const struct in6_addr *a)
540+
static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval)
541541
{
542542
u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1];
543543

544544
return jhash_3words(v,
545545
(__force u32)a->s6_addr32[2],
546546
(__force u32)a->s6_addr32[3],
547-
ipv6_hash_secret);
547+
initval);
548+
}
549+
550+
static inline u32 ipv6_addr_jhash(const struct in6_addr *a)
551+
{
552+
return __ipv6_addr_jhash(a, ipv6_hash_secret);
548553
}
549554

550555
static inline bool ipv6_addr_loopback(const struct in6_addr *a)
@@ -656,8 +661,6 @@ static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_add
656661
return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
657662
}
658663

659-
extern void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt);
660-
661664
extern int ip6_dst_hoplimit(struct dst_entry *dst);
662665

663666
/*

‎include/net/secure_seq.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include <linux/types.h>
55

6-
extern __u32 secure_ip_id(__be32 daddr);
7-
extern __u32 secure_ipv6_id(const __be32 daddr[4]);
86
extern u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport);
97
extern u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
108
__be16 dport);

‎net/core/secure_seq.c‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,6 @@ EXPORT_SYMBOL(secure_ipv6_port_ephemeral);
9595
#endif
9696

9797
#ifdef CONFIG_INET
98-
__u32 secure_ip_id(__be32 daddr)
99-
{
100-
u32 hash[MD5_DIGEST_WORDS];
101-
102-
net_secret_init();
103-
hash[0] = (__force __u32) daddr;
104-
hash[1] = net_secret[13];
105-
hash[2] = net_secret[14];
106-
hash[3] = net_secret[15];
107-
108-
md5_transform(hash, net_secret);
109-
110-
return hash[0];
111-
}
112-
113-
__u32 secure_ipv6_id(const __be32 daddr[4])
114-
{
115-
__u32 hash[4];
116-
117-
net_secret_init();
118-
memcpy(hash, daddr, 16);
119-
md5_transform(hash, net_secret);
120-
121-
return hash[0];
122-
}
12398

12499
__u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
125100
__be16 sport, __be16 dport)

‎net/ipv4/igmp.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
369369
pip->saddr = fl4.saddr;
370370
pip->protocol = IPPROTO_IGMP;
371371
pip->tot_len = 0; /* filled in later */
372-
ip_select_ident(skb, &rt->dst, NULL);
372+
ip_select_ident(skb, NULL);
373373
((u8 *)&pip[1])[0] = IPOPT_RA;
374374
((u8 *)&pip[1])[1] = 4;
375375
((u8 *)&pip[1])[2] = 0;
@@ -714,7 +714,7 @@ static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
714714
iph->daddr = dst;
715715
iph->saddr = fl4.saddr;
716716
iph->protocol = IPPROTO_IGMP;
717-
ip_select_ident(skb, &rt->dst, NULL);
717+
ip_select_ident(skb, NULL);
718718
((u8 *)&iph[1])[0] = IPOPT_RA;
719719
((u8 *)&iph[1])[1] = 4;
720720
((u8 *)&iph[1])[2] = 0;

‎net/ipv4/inetpeer.c‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,7 @@
2626
* Theory of operations.
2727
* We keep one entry for each peer IP address. The nodes contains long-living
2828
* information about the peer which doesn't depend on routes.
29-
* At this moment this information consists only of ID field for the next
30-
* outgoing IP packet. This field is incremented with each packet as encoded
31-
* in inet_getid() function (include/net/inetpeer.h).
32-
* At the moment of writing this notes identifier of IP packets is generated
33-
* to be unpredictable using this code only for packets subjected
34-
* (actually or potentially) to defragmentation. I.e. DF packets less than
35-
* PMTU in size when local fragmentation is disabled use a constant ID and do
36-
* not use this code (see ip_select_ident() in include/net/ip.h).
3729
*
38-
* Route cache entries hold references to our nodes.
39-
* New cache entries get references via lookup by destination IP address in
40-
* the avl tree. The reference is grabbed only when it's needed i.e. only
41-
* when we try to output IP packet which needs an unpredictable ID (see
42-
* __ip_select_ident() in net/ipv4/route.c).
4330
* Nodes are removed only when reference counter goes to 0.
4431
* When it's happened the node may be removed when a sufficient amount of
4532
* time has been passed since its last use. The less-recently-used entry can
@@ -62,7 +49,6 @@
6249
* refcnt: atomically against modifications on other CPU;
6350
* usually under some other lock to prevent node disappearing
6451
* daddr: unchangeable
65-
* ip_id_count: atomic value (no lock needed)
6652
*/
6753

6854
static struct kmem_cache *peer_cachep __read_mostly;
@@ -504,10 +490,6 @@ struct inet_peer *inet_getpeer(struct inet_peer_base *base,
504490
p->daddr = *daddr;
505491
atomic_set(&p->refcnt, 1);
506492
atomic_set(&p->rid, 0);
507-
atomic_set(&p->ip_id_count,
508-
(daddr->family == AF_INET) ?
509-
secure_ip_id(daddr->addr.a4) :
510-
secure_ipv6_id(daddr->addr.a6));
511493
p->metrics[RTAX_LOCK-1] = INETPEER_METRICS_NEW;
512494
p->rate_tokens = 0;
513495
/* 60*HZ is arbitrary, but chosen enough high so that the first

‎net/ipv4/ip_output.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
148148
iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
149149
iph->saddr = saddr;
150150
iph->protocol = sk->sk_protocol;
151-
ip_select_ident(skb, &rt->dst, sk);
151+
ip_select_ident(skb, sk);
152152

153153
if (opt && opt->opt.optlen) {
154154
iph->ihl += opt->opt.optlen>>2;
@@ -386,8 +386,7 @@ int ip_queue_xmit(struct sk_buff *skb, struct flowi *fl)
386386
ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
387387
}
388388

389-
ip_select_ident_more(skb, &rt->dst, sk,
390-
(skb_shinfo(skb)->gso_segs ?: 1) - 1);
389+
ip_select_ident_segs(skb, sk, skb_shinfo(skb)->gso_segs ?: 1);
391390

392391
skb->priority = sk->sk_priority;
393392
skb->mark = sk->sk_mark;
@@ -1329,7 +1328,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
13291328
iph->ttl = ttl;
13301329
iph->protocol = sk->sk_protocol;
13311330
ip_copy_addrs(iph, fl4);
1332-
ip_select_ident(skb, &rt->dst, sk);
1331+
ip_select_ident(skb, sk);
13331332

13341333
if (opt) {
13351334
iph->ihl += opt->optlen>>2;

‎net/ipv4/ip_tunnel_core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ int iptunnel_xmit(struct rtable *rt, struct sk_buff *skb,
7474
iph->daddr = dst;
7575
iph->saddr = src;
7676
iph->ttl = ttl;
77-
__ip_select_ident(iph, &rt->dst, (skb_shinfo(skb)->gso_segs ?: 1) - 1);
77+
__ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
7878

7979
err = ip_local_out(skb);
8080
if (unlikely(net_xmit_eval(err)))

0 commit comments

Comments
 (0)