Commit | Line | Data |
---|---|---|
51c5d0c4 DM |
1 | #include <linux/rcupdate.h> |
2 | #include <linux/spinlock.h> | |
3 | #include <linux/jiffies.h> | |
ab92bb2f | 4 | #include <linux/module.h> |
4aabd8ef | 5 | #include <linux/cache.h> |
51c5d0c4 DM |
6 | #include <linux/slab.h> |
7 | #include <linux/init.h> | |
4aabd8ef | 8 | #include <linux/tcp.h> |
5815d5e7 | 9 | #include <linux/hash.h> |
d23ff701 | 10 | #include <linux/tcp_metrics.h> |
976a702a | 11 | #include <linux/vmalloc.h> |
4aabd8ef DM |
12 | |
13 | #include <net/inet_connection_sock.h> | |
51c5d0c4 | 14 | #include <net/net_namespace.h> |
ab92bb2f | 15 | #include <net/request_sock.h> |
51c5d0c4 | 16 | #include <net/inetpeer.h> |
4aabd8ef | 17 | #include <net/sock.h> |
51c5d0c4 | 18 | #include <net/ipv6.h> |
4aabd8ef DM |
19 | #include <net/dst.h> |
20 | #include <net/tcp.h> | |
d23ff701 | 21 | #include <net/genetlink.h> |
4aabd8ef DM |
22 | |
23 | int sysctl_tcp_nometrics_save __read_mostly; | |
24 | ||
41804420 DM |
25 | static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr, |
26 | const struct inetpeer_addr *daddr, | |
77f99ad1 CP |
27 | struct net *net, unsigned int hash); |
28 | ||
1fe4c481 YC |
29 | struct tcp_fastopen_metrics { |
30 | u16 mss; | |
aab48743 YC |
31 | u16 syn_loss:10; /* Recurring Fast Open SYN losses */ |
32 | unsigned long last_syn_loss; /* Last Fast Open SYN loss */ | |
1fe4c481 YC |
33 | struct tcp_fastopen_cookie cookie; |
34 | }; | |
35 | ||
740b0f18 ED |
36 | /* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility |
37 | * Kernel only stores RTT and RTTVAR in usec resolution | |
38 | */ | |
39 | #define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2) | |
40 | ||
51c5d0c4 DM |
41 | struct tcp_metrics_block { |
42 | struct tcp_metrics_block __rcu *tcpm_next; | |
849e8a0c | 43 | possible_net_t tcpm_net; |
a5443028 | 44 | struct inetpeer_addr tcpm_saddr; |
324fd55a | 45 | struct inetpeer_addr tcpm_daddr; |
51c5d0c4 | 46 | unsigned long tcpm_stamp; |
81166dd6 DM |
47 | u32 tcpm_ts; |
48 | u32 tcpm_ts_stamp; | |
51c5d0c4 | 49 | u32 tcpm_lock; |
740b0f18 | 50 | u32 tcpm_vals[TCP_METRIC_MAX_KERNEL + 1]; |
1fe4c481 | 51 | struct tcp_fastopen_metrics tcpm_fastopen; |
d23ff701 JA |
52 | |
53 | struct rcu_head rcu_head; | |
51c5d0c4 DM |
54 | }; |
55 | ||
849e8a0c EB |
56 | static inline struct net *tm_net(struct tcp_metrics_block *tm) |
57 | { | |
58 | return read_pnet(&tm->tcpm_net); | |
59 | } | |
60 | ||
51c5d0c4 DM |
61 | static bool tcp_metric_locked(struct tcp_metrics_block *tm, |
62 | enum tcp_metric_index idx) | |
63 | { | |
64 | return tm->tcpm_lock & (1 << idx); | |
65 | } | |
66 | ||
67 | static u32 tcp_metric_get(struct tcp_metrics_block *tm, | |
68 | enum tcp_metric_index idx) | |
69 | { | |
70 | return tm->tcpm_vals[idx]; | |
71 | } | |
72 | ||
51c5d0c4 DM |
73 | static void tcp_metric_set(struct tcp_metrics_block *tm, |
74 | enum tcp_metric_index idx, | |
75 | u32 val) | |
76 | { | |
77 | tm->tcpm_vals[idx] = val; | |
78 | } | |
79 | ||
51c5d0c4 DM |
80 | static bool addr_same(const struct inetpeer_addr *a, |
81 | const struct inetpeer_addr *b) | |
82 | { | |
83 | const struct in6_addr *a6, *b6; | |
84 | ||
85 | if (a->family != b->family) | |
86 | return false; | |
87 | if (a->family == AF_INET) | |
88 | return a->addr.a4 == b->addr.a4; | |
89 | ||
90 | a6 = (const struct in6_addr *) &a->addr.a6[0]; | |
91 | b6 = (const struct in6_addr *) &b->addr.a6[0]; | |
92 | ||
93 | return ipv6_addr_equal(a6, b6); | |
94 | } | |
95 | ||
96 | struct tcpm_hash_bucket { | |
97 | struct tcp_metrics_block __rcu *chain; | |
98 | }; | |
99 | ||
098a697b EB |
100 | static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly; |
101 | static unsigned int tcp_metrics_hash_log __read_mostly; | |
102 | ||
51c5d0c4 DM |
103 | static DEFINE_SPINLOCK(tcp_metrics_lock); |
104 | ||
740b0f18 ED |
105 | static void tcpm_suck_dst(struct tcp_metrics_block *tm, |
106 | const struct dst_entry *dst, | |
efeaa555 | 107 | bool fastopen_clear) |
51c5d0c4 | 108 | { |
740b0f18 | 109 | u32 msval; |
51c5d0c4 DM |
110 | u32 val; |
111 | ||
9a0a9502 JA |
112 | tm->tcpm_stamp = jiffies; |
113 | ||
51c5d0c4 DM |
114 | val = 0; |
115 | if (dst_metric_locked(dst, RTAX_RTT)) | |
116 | val |= 1 << TCP_METRIC_RTT; | |
117 | if (dst_metric_locked(dst, RTAX_RTTVAR)) | |
118 | val |= 1 << TCP_METRIC_RTTVAR; | |
119 | if (dst_metric_locked(dst, RTAX_SSTHRESH)) | |
120 | val |= 1 << TCP_METRIC_SSTHRESH; | |
121 | if (dst_metric_locked(dst, RTAX_CWND)) | |
122 | val |= 1 << TCP_METRIC_CWND; | |
123 | if (dst_metric_locked(dst, RTAX_REORDERING)) | |
124 | val |= 1 << TCP_METRIC_REORDERING; | |
125 | tm->tcpm_lock = val; | |
126 | ||
740b0f18 ED |
127 | msval = dst_metric_raw(dst, RTAX_RTT); |
128 | tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC; | |
129 | ||
130 | msval = dst_metric_raw(dst, RTAX_RTTVAR); | |
131 | tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC; | |
51c5d0c4 DM |
132 | tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH); |
133 | tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND); | |
134 | tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING); | |
81166dd6 DM |
135 | tm->tcpm_ts = 0; |
136 | tm->tcpm_ts_stamp = 0; | |
efeaa555 ED |
137 | if (fastopen_clear) { |
138 | tm->tcpm_fastopen.mss = 0; | |
139 | tm->tcpm_fastopen.syn_loss = 0; | |
140 | tm->tcpm_fastopen.cookie.len = 0; | |
141 | } | |
51c5d0c4 DM |
142 | } |
143 | ||
77f99ad1 CP |
144 | #define TCP_METRICS_TIMEOUT (60 * 60 * HZ) |
145 | ||
146 | static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst) | |
147 | { | |
148 | if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT))) | |
149 | tcpm_suck_dst(tm, dst, false); | |
150 | } | |
151 | ||
152 | #define TCP_METRICS_RECLAIM_DEPTH 5 | |
153 | #define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL | |
154 | ||
9f1ab186 ED |
155 | #define deref_locked(p) \ |
156 | rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock)) | |
157 | ||
51c5d0c4 | 158 | static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst, |
a5443028 | 159 | struct inetpeer_addr *saddr, |
324fd55a | 160 | struct inetpeer_addr *daddr, |
77f99ad1 | 161 | unsigned int hash) |
51c5d0c4 DM |
162 | { |
163 | struct tcp_metrics_block *tm; | |
164 | struct net *net; | |
77f99ad1 | 165 | bool reclaim = false; |
51c5d0c4 DM |
166 | |
167 | spin_lock_bh(&tcp_metrics_lock); | |
168 | net = dev_net(dst->dev); | |
77f99ad1 CP |
169 | |
170 | /* While waiting for the spin-lock the cache might have been populated | |
171 | * with this entry and so we have to check again. | |
172 | */ | |
41804420 | 173 | tm = __tcp_get_metrics(saddr, daddr, net, hash); |
77f99ad1 CP |
174 | if (tm == TCP_METRICS_RECLAIM_PTR) { |
175 | reclaim = true; | |
176 | tm = NULL; | |
177 | } | |
178 | if (tm) { | |
179 | tcpm_check_stamp(tm, dst); | |
180 | goto out_unlock; | |
181 | } | |
182 | ||
51c5d0c4 DM |
183 | if (unlikely(reclaim)) { |
184 | struct tcp_metrics_block *oldest; | |
185 | ||
9f1ab186 ED |
186 | oldest = deref_locked(tcp_metrics_hash[hash].chain); |
187 | for (tm = deref_locked(oldest->tcpm_next); tm; | |
188 | tm = deref_locked(tm->tcpm_next)) { | |
51c5d0c4 DM |
189 | if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp)) |
190 | oldest = tm; | |
191 | } | |
192 | tm = oldest; | |
193 | } else { | |
194 | tm = kmalloc(sizeof(*tm), GFP_ATOMIC); | |
195 | if (!tm) | |
196 | goto out_unlock; | |
197 | } | |
849e8a0c | 198 | write_pnet(&tm->tcpm_net, net); |
a5443028 | 199 | tm->tcpm_saddr = *saddr; |
324fd55a | 200 | tm->tcpm_daddr = *daddr; |
51c5d0c4 | 201 | |
efeaa555 | 202 | tcpm_suck_dst(tm, dst, true); |
51c5d0c4 DM |
203 | |
204 | if (likely(!reclaim)) { | |
098a697b EB |
205 | tm->tcpm_next = tcp_metrics_hash[hash].chain; |
206 | rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm); | |
51c5d0c4 DM |
207 | } |
208 | ||
209 | out_unlock: | |
210 | spin_unlock_bh(&tcp_metrics_lock); | |
211 | return tm; | |
212 | } | |
213 | ||
51c5d0c4 DM |
214 | static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth) |
215 | { | |
216 | if (tm) | |
217 | return tm; | |
218 | if (depth > TCP_METRICS_RECLAIM_DEPTH) | |
219 | return TCP_METRICS_RECLAIM_PTR; | |
220 | return NULL; | |
221 | } | |
222 | ||
a5443028 CP |
223 | static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr, |
224 | const struct inetpeer_addr *daddr, | |
51c5d0c4 DM |
225 | struct net *net, unsigned int hash) |
226 | { | |
227 | struct tcp_metrics_block *tm; | |
228 | int depth = 0; | |
229 | ||
098a697b | 230 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
51c5d0c4 | 231 | tm = rcu_dereference(tm->tcpm_next)) { |
a5443028 | 232 | if (addr_same(&tm->tcpm_saddr, saddr) && |
849e8a0c EB |
233 | addr_same(&tm->tcpm_daddr, daddr) && |
234 | net_eq(tm_net(tm), net)) | |
51c5d0c4 DM |
235 | break; |
236 | depth++; | |
237 | } | |
238 | return tcp_get_encode(tm, depth); | |
239 | } | |
240 | ||
241 | static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req, | |
242 | struct dst_entry *dst) | |
243 | { | |
244 | struct tcp_metrics_block *tm; | |
a5443028 | 245 | struct inetpeer_addr saddr, daddr; |
51c5d0c4 DM |
246 | unsigned int hash; |
247 | struct net *net; | |
248 | ||
a5443028 | 249 | saddr.family = req->rsk_ops->family; |
324fd55a CP |
250 | daddr.family = req->rsk_ops->family; |
251 | switch (daddr.family) { | |
51c5d0c4 | 252 | case AF_INET: |
a5443028 | 253 | saddr.addr.a4 = inet_rsk(req)->ir_loc_addr; |
324fd55a CP |
254 | daddr.addr.a4 = inet_rsk(req)->ir_rmt_addr; |
255 | hash = (__force unsigned int) daddr.addr.a4; | |
51c5d0c4 | 256 | break; |
634fb979 | 257 | #if IS_ENABLED(CONFIG_IPV6) |
51c5d0c4 | 258 | case AF_INET6: |
a5443028 | 259 | *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr; |
324fd55a | 260 | *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr; |
634fb979 | 261 | hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr); |
51c5d0c4 | 262 | break; |
634fb979 | 263 | #endif |
51c5d0c4 DM |
264 | default: |
265 | return NULL; | |
266 | } | |
267 | ||
51c5d0c4 | 268 | net = dev_net(dst->dev); |
3e5da62d | 269 | hash ^= net_hash_mix(net); |
098a697b | 270 | hash = hash_32(hash, tcp_metrics_hash_log); |
51c5d0c4 | 271 | |
098a697b | 272 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
51c5d0c4 | 273 | tm = rcu_dereference(tm->tcpm_next)) { |
a5443028 | 274 | if (addr_same(&tm->tcpm_saddr, &saddr) && |
849e8a0c EB |
275 | addr_same(&tm->tcpm_daddr, &daddr) && |
276 | net_eq(tm_net(tm), net)) | |
51c5d0c4 DM |
277 | break; |
278 | } | |
279 | tcpm_check_stamp(tm, dst); | |
280 | return tm; | |
281 | } | |
282 | ||
81166dd6 DM |
283 | static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw) |
284 | { | |
81166dd6 | 285 | struct tcp_metrics_block *tm; |
a5443028 | 286 | struct inetpeer_addr saddr, daddr; |
81166dd6 DM |
287 | unsigned int hash; |
288 | struct net *net; | |
289 | ||
3ad88cf7 CP |
290 | if (tw->tw_family == AF_INET) { |
291 | saddr.family = AF_INET; | |
a5443028 | 292 | saddr.addr.a4 = tw->tw_rcv_saddr; |
3ad88cf7 | 293 | daddr.family = AF_INET; |
324fd55a CP |
294 | daddr.addr.a4 = tw->tw_daddr; |
295 | hash = (__force unsigned int) daddr.addr.a4; | |
3ad88cf7 | 296 | } |
c2bb06db | 297 | #if IS_ENABLED(CONFIG_IPV6) |
3ad88cf7 CP |
298 | else if (tw->tw_family == AF_INET6) { |
299 | if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) { | |
300 | saddr.family = AF_INET; | |
301 | saddr.addr.a4 = tw->tw_rcv_saddr; | |
302 | daddr.family = AF_INET; | |
303 | daddr.addr.a4 = tw->tw_daddr; | |
304 | hash = (__force unsigned int) daddr.addr.a4; | |
305 | } else { | |
306 | saddr.family = AF_INET6; | |
307 | *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr; | |
308 | daddr.family = AF_INET6; | |
309 | *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr; | |
310 | hash = ipv6_addr_hash(&tw->tw_v6_daddr); | |
311 | } | |
312 | } | |
c2bb06db | 313 | #endif |
3ad88cf7 | 314 | else |
81166dd6 | 315 | return NULL; |
81166dd6 | 316 | |
81166dd6 | 317 | net = twsk_net(tw); |
3e5da62d | 318 | hash ^= net_hash_mix(net); |
098a697b | 319 | hash = hash_32(hash, tcp_metrics_hash_log); |
81166dd6 | 320 | |
098a697b | 321 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
81166dd6 | 322 | tm = rcu_dereference(tm->tcpm_next)) { |
a5443028 | 323 | if (addr_same(&tm->tcpm_saddr, &saddr) && |
849e8a0c EB |
324 | addr_same(&tm->tcpm_daddr, &daddr) && |
325 | net_eq(tm_net(tm), net)) | |
81166dd6 DM |
326 | break; |
327 | } | |
328 | return tm; | |
329 | } | |
330 | ||
51c5d0c4 DM |
331 | static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk, |
332 | struct dst_entry *dst, | |
333 | bool create) | |
334 | { | |
335 | struct tcp_metrics_block *tm; | |
a5443028 | 336 | struct inetpeer_addr saddr, daddr; |
51c5d0c4 DM |
337 | unsigned int hash; |
338 | struct net *net; | |
51c5d0c4 | 339 | |
3ad88cf7 CP |
340 | if (sk->sk_family == AF_INET) { |
341 | saddr.family = AF_INET; | |
a5443028 | 342 | saddr.addr.a4 = inet_sk(sk)->inet_saddr; |
3ad88cf7 | 343 | daddr.family = AF_INET; |
324fd55a CP |
344 | daddr.addr.a4 = inet_sk(sk)->inet_daddr; |
345 | hash = (__force unsigned int) daddr.addr.a4; | |
3ad88cf7 | 346 | } |
c2bb06db | 347 | #if IS_ENABLED(CONFIG_IPV6) |
3ad88cf7 CP |
348 | else if (sk->sk_family == AF_INET6) { |
349 | if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { | |
350 | saddr.family = AF_INET; | |
351 | saddr.addr.a4 = inet_sk(sk)->inet_saddr; | |
352 | daddr.family = AF_INET; | |
353 | daddr.addr.a4 = inet_sk(sk)->inet_daddr; | |
354 | hash = (__force unsigned int) daddr.addr.a4; | |
355 | } else { | |
356 | saddr.family = AF_INET6; | |
357 | *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr; | |
358 | daddr.family = AF_INET6; | |
359 | *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr; | |
360 | hash = ipv6_addr_hash(&sk->sk_v6_daddr); | |
361 | } | |
362 | } | |
c2bb06db | 363 | #endif |
3ad88cf7 | 364 | else |
51c5d0c4 | 365 | return NULL; |
51c5d0c4 | 366 | |
51c5d0c4 | 367 | net = dev_net(dst->dev); |
3e5da62d | 368 | hash ^= net_hash_mix(net); |
098a697b | 369 | hash = hash_32(hash, tcp_metrics_hash_log); |
51c5d0c4 | 370 | |
a5443028 | 371 | tm = __tcp_get_metrics(&saddr, &daddr, net, hash); |
77f99ad1 | 372 | if (tm == TCP_METRICS_RECLAIM_PTR) |
51c5d0c4 | 373 | tm = NULL; |
51c5d0c4 | 374 | if (!tm && create) |
41804420 | 375 | tm = tcpm_new(dst, &saddr, &daddr, hash); |
51c5d0c4 DM |
376 | else |
377 | tcpm_check_stamp(tm, dst); | |
378 | ||
379 | return tm; | |
380 | } | |
381 | ||
4aabd8ef DM |
382 | /* Save metrics learned by this TCP session. This function is called |
383 | * only, when TCP finishes successfully i.e. when it enters TIME-WAIT | |
384 | * or goes from LAST-ACK to CLOSE. | |
385 | */ | |
386 | void tcp_update_metrics(struct sock *sk) | |
387 | { | |
51c5d0c4 | 388 | const struct inet_connection_sock *icsk = inet_csk(sk); |
4aabd8ef | 389 | struct dst_entry *dst = __sk_dst_get(sk); |
51c5d0c4 DM |
390 | struct tcp_sock *tp = tcp_sk(sk); |
391 | struct tcp_metrics_block *tm; | |
392 | unsigned long rtt; | |
393 | u32 val; | |
394 | int m; | |
4aabd8ef | 395 | |
51c5d0c4 | 396 | if (sysctl_tcp_nometrics_save || !dst) |
4aabd8ef DM |
397 | return; |
398 | ||
51c5d0c4 | 399 | if (dst->flags & DST_HOST) |
4aabd8ef DM |
400 | dst_confirm(dst); |
401 | ||
51c5d0c4 | 402 | rcu_read_lock(); |
740b0f18 | 403 | if (icsk->icsk_backoff || !tp->srtt_us) { |
51c5d0c4 DM |
404 | /* This session failed to estimate rtt. Why? |
405 | * Probably, no packets returned in time. Reset our | |
406 | * results. | |
407 | */ | |
408 | tm = tcp_get_metrics(sk, dst, false); | |
409 | if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT)) | |
410 | tcp_metric_set(tm, TCP_METRIC_RTT, 0); | |
411 | goto out_unlock; | |
412 | } else | |
413 | tm = tcp_get_metrics(sk, dst, true); | |
4aabd8ef | 414 | |
51c5d0c4 DM |
415 | if (!tm) |
416 | goto out_unlock; | |
4aabd8ef | 417 | |
740b0f18 ED |
418 | rtt = tcp_metric_get(tm, TCP_METRIC_RTT); |
419 | m = rtt - tp->srtt_us; | |
4aabd8ef | 420 | |
51c5d0c4 DM |
421 | /* If newly calculated rtt larger than stored one, store new |
422 | * one. Otherwise, use EWMA. Remember, rtt overestimation is | |
423 | * always better than underestimation. | |
424 | */ | |
425 | if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) { | |
426 | if (m <= 0) | |
740b0f18 | 427 | rtt = tp->srtt_us; |
51c5d0c4 DM |
428 | else |
429 | rtt -= (m >> 3); | |
740b0f18 | 430 | tcp_metric_set(tm, TCP_METRIC_RTT, rtt); |
51c5d0c4 | 431 | } |
4aabd8ef | 432 | |
51c5d0c4 DM |
433 | if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) { |
434 | unsigned long var; | |
4aabd8ef | 435 | |
51c5d0c4 DM |
436 | if (m < 0) |
437 | m = -m; | |
4aabd8ef | 438 | |
51c5d0c4 DM |
439 | /* Scale deviation to rttvar fixed point */ |
440 | m >>= 1; | |
740b0f18 ED |
441 | if (m < tp->mdev_us) |
442 | m = tp->mdev_us; | |
4aabd8ef | 443 | |
740b0f18 | 444 | var = tcp_metric_get(tm, TCP_METRIC_RTTVAR); |
51c5d0c4 DM |
445 | if (m >= var) |
446 | var = m; | |
447 | else | |
448 | var -= (var - m) >> 2; | |
4aabd8ef | 449 | |
740b0f18 | 450 | tcp_metric_set(tm, TCP_METRIC_RTTVAR, var); |
51c5d0c4 DM |
451 | } |
452 | ||
453 | if (tcp_in_initial_slowstart(tp)) { | |
454 | /* Slow start still did not finish. */ | |
455 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) { | |
456 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); | |
457 | if (val && (tp->snd_cwnd >> 1) > val) | |
458 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, | |
459 | tp->snd_cwnd >> 1); | |
460 | } | |
461 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { | |
462 | val = tcp_metric_get(tm, TCP_METRIC_CWND); | |
463 | if (tp->snd_cwnd > val) | |
464 | tcp_metric_set(tm, TCP_METRIC_CWND, | |
465 | tp->snd_cwnd); | |
466 | } | |
467 | } else if (tp->snd_cwnd > tp->snd_ssthresh && | |
468 | icsk->icsk_ca_state == TCP_CA_Open) { | |
469 | /* Cong. avoidance phase, cwnd is reliable. */ | |
470 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) | |
471 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, | |
472 | max(tp->snd_cwnd >> 1, tp->snd_ssthresh)); | |
473 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { | |
474 | val = tcp_metric_get(tm, TCP_METRIC_CWND); | |
2100844c | 475 | tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1); |
51c5d0c4 DM |
476 | } |
477 | } else { | |
478 | /* Else slow start did not finish, cwnd is non-sense, | |
479 | * ssthresh may be also invalid. | |
480 | */ | |
481 | if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) { | |
482 | val = tcp_metric_get(tm, TCP_METRIC_CWND); | |
483 | tcp_metric_set(tm, TCP_METRIC_CWND, | |
484 | (val + tp->snd_ssthresh) >> 1); | |
485 | } | |
486 | if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) { | |
487 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); | |
488 | if (val && tp->snd_ssthresh > val) | |
489 | tcp_metric_set(tm, TCP_METRIC_SSTHRESH, | |
490 | tp->snd_ssthresh); | |
491 | } | |
492 | if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) { | |
493 | val = tcp_metric_get(tm, TCP_METRIC_REORDERING); | |
494 | if (val < tp->reordering && | |
4aabd8ef | 495 | tp->reordering != sysctl_tcp_reordering) |
51c5d0c4 DM |
496 | tcp_metric_set(tm, TCP_METRIC_REORDERING, |
497 | tp->reordering); | |
4aabd8ef DM |
498 | } |
499 | } | |
51c5d0c4 DM |
500 | tm->tcpm_stamp = jiffies; |
501 | out_unlock: | |
502 | rcu_read_unlock(); | |
4aabd8ef DM |
503 | } |
504 | ||
505 | /* Initialize metrics on socket. */ | |
506 | ||
507 | void tcp_init_metrics(struct sock *sk) | |
508 | { | |
4aabd8ef | 509 | struct dst_entry *dst = __sk_dst_get(sk); |
51c5d0c4 DM |
510 | struct tcp_sock *tp = tcp_sk(sk); |
511 | struct tcp_metrics_block *tm; | |
1b7fdd2a | 512 | u32 val, crtt = 0; /* cached RTT scaled by 8 */ |
4aabd8ef DM |
513 | |
514 | if (dst == NULL) | |
515 | goto reset; | |
516 | ||
517 | dst_confirm(dst); | |
518 | ||
51c5d0c4 DM |
519 | rcu_read_lock(); |
520 | tm = tcp_get_metrics(sk, dst, true); | |
521 | if (!tm) { | |
522 | rcu_read_unlock(); | |
523 | goto reset; | |
524 | } | |
525 | ||
526 | if (tcp_metric_locked(tm, TCP_METRIC_CWND)) | |
527 | tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND); | |
528 | ||
529 | val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH); | |
530 | if (val) { | |
531 | tp->snd_ssthresh = val; | |
4aabd8ef DM |
532 | if (tp->snd_ssthresh > tp->snd_cwnd_clamp) |
533 | tp->snd_ssthresh = tp->snd_cwnd_clamp; | |
534 | } else { | |
535 | /* ssthresh may have been reduced unnecessarily during. | |
536 | * 3WHS. Restore it back to its initial default. | |
537 | */ | |
538 | tp->snd_ssthresh = TCP_INFINITE_SSTHRESH; | |
539 | } | |
51c5d0c4 DM |
540 | val = tcp_metric_get(tm, TCP_METRIC_REORDERING); |
541 | if (val && tp->reordering != val) { | |
4aabd8ef DM |
542 | tcp_disable_fack(tp); |
543 | tcp_disable_early_retrans(tp); | |
51c5d0c4 | 544 | tp->reordering = val; |
4aabd8ef DM |
545 | } |
546 | ||
740b0f18 | 547 | crtt = tcp_metric_get(tm, TCP_METRIC_RTT); |
51c5d0c4 | 548 | rcu_read_unlock(); |
4aabd8ef | 549 | reset: |
52f20e65 YC |
550 | /* The initial RTT measurement from the SYN/SYN-ACK is not ideal |
551 | * to seed the RTO for later data packets because SYN packets are | |
552 | * small. Use the per-dst cached values to seed the RTO but keep | |
553 | * the RTT estimator variables intact (e.g., srtt, mdev, rttvar). | |
554 | * Later the RTO will be updated immediately upon obtaining the first | |
555 | * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only | |
556 | * influences the first RTO but not later RTT estimation. | |
557 | * | |
558 | * But if RTT is not available from the SYN (due to retransmits or | |
559 | * syn cookies) or the cache, force a conservative 3secs timeout. | |
560 | * | |
561 | * A bit of theory. RTT is time passed after "normal" sized packet | |
562 | * is sent until it is ACKed. In normal circumstances sending small | |
563 | * packets force peer to delay ACKs and calculation is correct too. | |
564 | * The algorithm is adaptive and, provided we follow specs, it | |
565 | * NEVER underestimate RTT. BUT! If peer tries to make some clever | |
566 | * tricks sort of "quick acks" for time long enough to decrease RTT | |
567 | * to low value, and then abruptly stops to do it and starts to delay | |
568 | * ACKs, wait for troubles. | |
569 | */ | |
740b0f18 | 570 | if (crtt > tp->srtt_us) { |
269aa759 | 571 | /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */ |
740b0f18 | 572 | crtt /= 8 * USEC_PER_MSEC; |
269aa759 | 573 | inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk)); |
740b0f18 | 574 | } else if (tp->srtt_us == 0) { |
4aabd8ef DM |
575 | /* RFC6298: 5.7 We've failed to get a valid RTT sample from |
576 | * 3WHS. This is most likely due to retransmission, | |
577 | * including spurious one. Reset the RTO back to 3secs | |
578 | * from the more aggressive 1sec to avoid more spurious | |
579 | * retransmission. | |
580 | */ | |
740b0f18 ED |
581 | tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK); |
582 | tp->mdev_us = tp->mdev_max_us = tp->rttvar_us; | |
583 | ||
4aabd8ef DM |
584 | inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK; |
585 | } | |
586 | /* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been | |
587 | * retransmitted. In light of RFC6298 more aggressive 1sec | |
588 | * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK | |
589 | * retransmission has occurred. | |
590 | */ | |
591 | if (tp->total_retrans > 1) | |
592 | tp->snd_cwnd = 1; | |
593 | else | |
594 | tp->snd_cwnd = tcp_init_cwnd(tp, dst); | |
595 | tp->snd_cwnd_stamp = tcp_time_stamp; | |
596 | } | |
ab92bb2f | 597 | |
a26552af HFS |
598 | bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, |
599 | bool paws_check, bool timestamps) | |
ab92bb2f | 600 | { |
51c5d0c4 DM |
601 | struct tcp_metrics_block *tm; |
602 | bool ret; | |
603 | ||
ab92bb2f DM |
604 | if (!dst) |
605 | return false; | |
51c5d0c4 DM |
606 | |
607 | rcu_read_lock(); | |
608 | tm = __tcp_get_metrics_req(req, dst); | |
81166dd6 DM |
609 | if (paws_check) { |
610 | if (tm && | |
611 | (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL && | |
a26552af HFS |
612 | ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW || |
613 | !timestamps)) | |
81166dd6 DM |
614 | ret = false; |
615 | else | |
616 | ret = true; | |
617 | } else { | |
618 | if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp) | |
619 | ret = true; | |
620 | else | |
621 | ret = false; | |
622 | } | |
51c5d0c4 DM |
623 | rcu_read_unlock(); |
624 | ||
625 | return ret; | |
ab92bb2f DM |
626 | } |
627 | EXPORT_SYMBOL_GPL(tcp_peer_is_proven); | |
51c5d0c4 | 628 | |
81166dd6 DM |
629 | void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst) |
630 | { | |
631 | struct tcp_metrics_block *tm; | |
632 | ||
633 | rcu_read_lock(); | |
634 | tm = tcp_get_metrics(sk, dst, true); | |
635 | if (tm) { | |
636 | struct tcp_sock *tp = tcp_sk(sk); | |
637 | ||
638 | if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) { | |
639 | tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp; | |
640 | tp->rx_opt.ts_recent = tm->tcpm_ts; | |
641 | } | |
642 | } | |
643 | rcu_read_unlock(); | |
644 | } | |
645 | EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp); | |
646 | ||
647 | /* VJ's idea. Save last timestamp seen from this destination and hold | |
648 | * it at least for normal timewait interval to use for duplicate | |
649 | * segment detection in subsequent connections, before they enter | |
650 | * synchronized state. | |
651 | */ | |
652 | bool tcp_remember_stamp(struct sock *sk) | |
653 | { | |
654 | struct dst_entry *dst = __sk_dst_get(sk); | |
655 | bool ret = false; | |
656 | ||
657 | if (dst) { | |
658 | struct tcp_metrics_block *tm; | |
659 | ||
660 | rcu_read_lock(); | |
661 | tm = tcp_get_metrics(sk, dst, true); | |
662 | if (tm) { | |
663 | struct tcp_sock *tp = tcp_sk(sk); | |
664 | ||
665 | if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 || | |
666 | ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL && | |
667 | tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) { | |
668 | tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp; | |
669 | tm->tcpm_ts = tp->rx_opt.ts_recent; | |
670 | } | |
671 | ret = true; | |
672 | } | |
673 | rcu_read_unlock(); | |
674 | } | |
675 | return ret; | |
676 | } | |
677 | ||
678 | bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw) | |
679 | { | |
680 | struct tcp_metrics_block *tm; | |
681 | bool ret = false; | |
682 | ||
683 | rcu_read_lock(); | |
684 | tm = __tcp_get_metrics_tw(tw); | |
9a0a9502 | 685 | if (tm) { |
81166dd6 DM |
686 | const struct tcp_timewait_sock *tcptw; |
687 | struct sock *sk = (struct sock *) tw; | |
688 | ||
689 | tcptw = tcp_twsk(sk); | |
690 | if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 || | |
691 | ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL && | |
692 | tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) { | |
693 | tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp; | |
694 | tm->tcpm_ts = tcptw->tw_ts_recent; | |
695 | } | |
696 | ret = true; | |
697 | } | |
698 | rcu_read_unlock(); | |
699 | ||
700 | return ret; | |
701 | } | |
702 | ||
1fe4c481 YC |
703 | static DEFINE_SEQLOCK(fastopen_seqlock); |
704 | ||
705 | void tcp_fastopen_cache_get(struct sock *sk, u16 *mss, | |
aab48743 YC |
706 | struct tcp_fastopen_cookie *cookie, |
707 | int *syn_loss, unsigned long *last_syn_loss) | |
1fe4c481 YC |
708 | { |
709 | struct tcp_metrics_block *tm; | |
710 | ||
711 | rcu_read_lock(); | |
712 | tm = tcp_get_metrics(sk, __sk_dst_get(sk), false); | |
713 | if (tm) { | |
714 | struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; | |
715 | unsigned int seq; | |
716 | ||
717 | do { | |
718 | seq = read_seqbegin(&fastopen_seqlock); | |
719 | if (tfom->mss) | |
720 | *mss = tfom->mss; | |
721 | *cookie = tfom->cookie; | |
aab48743 YC |
722 | *syn_loss = tfom->syn_loss; |
723 | *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0; | |
1fe4c481 YC |
724 | } while (read_seqretry(&fastopen_seqlock, seq)); |
725 | } | |
726 | rcu_read_unlock(); | |
727 | } | |
728 | ||
1fe4c481 | 729 | void tcp_fastopen_cache_set(struct sock *sk, u16 mss, |
aab48743 | 730 | struct tcp_fastopen_cookie *cookie, bool syn_lost) |
1fe4c481 | 731 | { |
dccf76ca | 732 | struct dst_entry *dst = __sk_dst_get(sk); |
1fe4c481 YC |
733 | struct tcp_metrics_block *tm; |
734 | ||
dccf76ca ED |
735 | if (!dst) |
736 | return; | |
1fe4c481 | 737 | rcu_read_lock(); |
dccf76ca | 738 | tm = tcp_get_metrics(sk, dst, true); |
1fe4c481 YC |
739 | if (tm) { |
740 | struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; | |
741 | ||
742 | write_seqlock_bh(&fastopen_seqlock); | |
c968601d YC |
743 | if (mss) |
744 | tfom->mss = mss; | |
745 | if (cookie && cookie->len > 0) | |
1fe4c481 | 746 | tfom->cookie = *cookie; |
aab48743 YC |
747 | if (syn_lost) { |
748 | ++tfom->syn_loss; | |
749 | tfom->last_syn_loss = jiffies; | |
750 | } else | |
751 | tfom->syn_loss = 0; | |
1fe4c481 YC |
752 | write_sequnlock_bh(&fastopen_seqlock); |
753 | } | |
754 | rcu_read_unlock(); | |
755 | } | |
756 | ||
d23ff701 JA |
757 | static struct genl_family tcp_metrics_nl_family = { |
758 | .id = GENL_ID_GENERATE, | |
759 | .hdrsize = 0, | |
760 | .name = TCP_METRICS_GENL_NAME, | |
761 | .version = TCP_METRICS_GENL_VERSION, | |
762 | .maxattr = TCP_METRICS_ATTR_MAX, | |
763 | .netnsok = true, | |
764 | }; | |
765 | ||
766 | static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = { | |
767 | [TCP_METRICS_ATTR_ADDR_IPV4] = { .type = NLA_U32, }, | |
768 | [TCP_METRICS_ATTR_ADDR_IPV6] = { .type = NLA_BINARY, | |
769 | .len = sizeof(struct in6_addr), }, | |
770 | /* Following attributes are not received for GET/DEL, | |
771 | * we keep them for reference | |
772 | */ | |
773 | #if 0 | |
774 | [TCP_METRICS_ATTR_AGE] = { .type = NLA_MSECS, }, | |
775 | [TCP_METRICS_ATTR_TW_TSVAL] = { .type = NLA_U32, }, | |
776 | [TCP_METRICS_ATTR_TW_TS_STAMP] = { .type = NLA_S32, }, | |
777 | [TCP_METRICS_ATTR_VALS] = { .type = NLA_NESTED, }, | |
778 | [TCP_METRICS_ATTR_FOPEN_MSS] = { .type = NLA_U16, }, | |
779 | [TCP_METRICS_ATTR_FOPEN_SYN_DROPS] = { .type = NLA_U16, }, | |
780 | [TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS] = { .type = NLA_MSECS, }, | |
781 | [TCP_METRICS_ATTR_FOPEN_COOKIE] = { .type = NLA_BINARY, | |
782 | .len = TCP_FASTOPEN_COOKIE_MAX, }, | |
783 | #endif | |
784 | }; | |
785 | ||
786 | /* Add attributes, caller cancels its header on failure */ | |
787 | static int tcp_metrics_fill_info(struct sk_buff *msg, | |
788 | struct tcp_metrics_block *tm) | |
789 | { | |
790 | struct nlattr *nest; | |
791 | int i; | |
792 | ||
324fd55a | 793 | switch (tm->tcpm_daddr.family) { |
d23ff701 JA |
794 | case AF_INET: |
795 | if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4, | |
324fd55a | 796 | tm->tcpm_daddr.addr.a4) < 0) |
d23ff701 | 797 | goto nla_put_failure; |
8a59359c CP |
798 | if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4, |
799 | tm->tcpm_saddr.addr.a4) < 0) | |
800 | goto nla_put_failure; | |
d23ff701 JA |
801 | break; |
802 | case AF_INET6: | |
803 | if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16, | |
324fd55a | 804 | tm->tcpm_daddr.addr.a6) < 0) |
d23ff701 | 805 | goto nla_put_failure; |
8a59359c CP |
806 | if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16, |
807 | tm->tcpm_saddr.addr.a6) < 0) | |
808 | goto nla_put_failure; | |
d23ff701 JA |
809 | break; |
810 | default: | |
811 | return -EAFNOSUPPORT; | |
812 | } | |
813 | ||
814 | if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE, | |
815 | jiffies - tm->tcpm_stamp) < 0) | |
816 | goto nla_put_failure; | |
817 | if (tm->tcpm_ts_stamp) { | |
818 | if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP, | |
819 | (s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0) | |
820 | goto nla_put_failure; | |
821 | if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL, | |
822 | tm->tcpm_ts) < 0) | |
823 | goto nla_put_failure; | |
824 | } | |
825 | ||
826 | { | |
827 | int n = 0; | |
828 | ||
829 | nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS); | |
830 | if (!nest) | |
831 | goto nla_put_failure; | |
740b0f18 ED |
832 | for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) { |
833 | u32 val = tm->tcpm_vals[i]; | |
834 | ||
835 | if (!val) | |
d23ff701 | 836 | continue; |
740b0f18 ED |
837 | if (i == TCP_METRIC_RTT) { |
838 | if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1, | |
839 | val) < 0) | |
840 | goto nla_put_failure; | |
841 | n++; | |
842 | val = max(val / 1000, 1U); | |
843 | } | |
844 | if (i == TCP_METRIC_RTTVAR) { | |
845 | if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1, | |
846 | val) < 0) | |
847 | goto nla_put_failure; | |
848 | n++; | |
849 | val = max(val / 1000, 1U); | |
850 | } | |
851 | if (nla_put_u32(msg, i + 1, val) < 0) | |
d23ff701 JA |
852 | goto nla_put_failure; |
853 | n++; | |
854 | } | |
855 | if (n) | |
856 | nla_nest_end(msg, nest); | |
857 | else | |
858 | nla_nest_cancel(msg, nest); | |
859 | } | |
860 | ||
861 | { | |
862 | struct tcp_fastopen_metrics tfom_copy[1], *tfom; | |
863 | unsigned int seq; | |
864 | ||
865 | do { | |
866 | seq = read_seqbegin(&fastopen_seqlock); | |
867 | tfom_copy[0] = tm->tcpm_fastopen; | |
868 | } while (read_seqretry(&fastopen_seqlock, seq)); | |
869 | ||
870 | tfom = tfom_copy; | |
871 | if (tfom->mss && | |
872 | nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS, | |
873 | tfom->mss) < 0) | |
874 | goto nla_put_failure; | |
875 | if (tfom->syn_loss && | |
876 | (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS, | |
877 | tfom->syn_loss) < 0 || | |
878 | nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS, | |
879 | jiffies - tfom->last_syn_loss) < 0)) | |
880 | goto nla_put_failure; | |
881 | if (tfom->cookie.len > 0 && | |
882 | nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE, | |
883 | tfom->cookie.len, tfom->cookie.val) < 0) | |
884 | goto nla_put_failure; | |
885 | } | |
886 | ||
887 | return 0; | |
888 | ||
889 | nla_put_failure: | |
890 | return -EMSGSIZE; | |
891 | } | |
892 | ||
893 | static int tcp_metrics_dump_info(struct sk_buff *skb, | |
894 | struct netlink_callback *cb, | |
895 | struct tcp_metrics_block *tm) | |
896 | { | |
897 | void *hdr; | |
898 | ||
15e47304 | 899 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
d23ff701 JA |
900 | &tcp_metrics_nl_family, NLM_F_MULTI, |
901 | TCP_METRICS_CMD_GET); | |
902 | if (!hdr) | |
903 | return -EMSGSIZE; | |
904 | ||
905 | if (tcp_metrics_fill_info(skb, tm) < 0) | |
906 | goto nla_put_failure; | |
907 | ||
053c095a JB |
908 | genlmsg_end(skb, hdr); |
909 | return 0; | |
d23ff701 JA |
910 | |
911 | nla_put_failure: | |
912 | genlmsg_cancel(skb, hdr); | |
913 | return -EMSGSIZE; | |
914 | } | |
915 | ||
916 | static int tcp_metrics_nl_dump(struct sk_buff *skb, | |
917 | struct netlink_callback *cb) | |
918 | { | |
919 | struct net *net = sock_net(skb->sk); | |
098a697b | 920 | unsigned int max_rows = 1U << tcp_metrics_hash_log; |
d23ff701 JA |
921 | unsigned int row, s_row = cb->args[0]; |
922 | int s_col = cb->args[1], col = s_col; | |
923 | ||
924 | for (row = s_row; row < max_rows; row++, s_col = 0) { | |
925 | struct tcp_metrics_block *tm; | |
098a697b | 926 | struct tcpm_hash_bucket *hb = tcp_metrics_hash + row; |
d23ff701 JA |
927 | |
928 | rcu_read_lock(); | |
929 | for (col = 0, tm = rcu_dereference(hb->chain); tm; | |
930 | tm = rcu_dereference(tm->tcpm_next), col++) { | |
849e8a0c EB |
931 | if (!net_eq(tm_net(tm), net)) |
932 | continue; | |
d23ff701 JA |
933 | if (col < s_col) |
934 | continue; | |
935 | if (tcp_metrics_dump_info(skb, cb, tm) < 0) { | |
936 | rcu_read_unlock(); | |
937 | goto done; | |
938 | } | |
939 | } | |
940 | rcu_read_unlock(); | |
941 | } | |
942 | ||
943 | done: | |
944 | cb->args[0] = row; | |
945 | cb->args[1] = col; | |
946 | return skb->len; | |
947 | } | |
948 | ||
3e7013dd CP |
949 | static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr, |
950 | unsigned int *hash, int optional, int v4, int v6) | |
d23ff701 JA |
951 | { |
952 | struct nlattr *a; | |
953 | ||
3e7013dd | 954 | a = info->attrs[v4]; |
d23ff701 JA |
955 | if (a) { |
956 | addr->family = AF_INET; | |
957 | addr->addr.a4 = nla_get_be32(a); | |
3e7013dd CP |
958 | if (hash) |
959 | *hash = (__force unsigned int) addr->addr.a4; | |
d23ff701 JA |
960 | return 0; |
961 | } | |
3e7013dd | 962 | a = info->attrs[v6]; |
d23ff701 | 963 | if (a) { |
2c42a3fb | 964 | if (nla_len(a) != sizeof(struct in6_addr)) |
d23ff701 JA |
965 | return -EINVAL; |
966 | addr->family = AF_INET6; | |
967 | memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6)); | |
3e7013dd CP |
968 | if (hash) |
969 | *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6); | |
d23ff701 JA |
970 | return 0; |
971 | } | |
972 | return optional ? 1 : -EAFNOSUPPORT; | |
973 | } | |
974 | ||
3e7013dd CP |
975 | static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr, |
976 | unsigned int *hash, int optional) | |
977 | { | |
978 | return __parse_nl_addr(info, addr, hash, optional, | |
979 | TCP_METRICS_ATTR_ADDR_IPV4, | |
980 | TCP_METRICS_ATTR_ADDR_IPV6); | |
981 | } | |
982 | ||
983 | static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr) | |
984 | { | |
985 | return __parse_nl_addr(info, addr, NULL, 0, | |
986 | TCP_METRICS_ATTR_SADDR_IPV4, | |
987 | TCP_METRICS_ATTR_SADDR_IPV6); | |
988 | } | |
989 | ||
d23ff701 JA |
990 | static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info) |
991 | { | |
992 | struct tcp_metrics_block *tm; | |
3e7013dd | 993 | struct inetpeer_addr saddr, daddr; |
d23ff701 JA |
994 | unsigned int hash; |
995 | struct sk_buff *msg; | |
996 | struct net *net = genl_info_net(info); | |
997 | void *reply; | |
998 | int ret; | |
3e7013dd | 999 | bool src = true; |
d23ff701 | 1000 | |
324fd55a | 1001 | ret = parse_nl_addr(info, &daddr, &hash, 0); |
d23ff701 JA |
1002 | if (ret < 0) |
1003 | return ret; | |
1004 | ||
3e7013dd CP |
1005 | ret = parse_nl_saddr(info, &saddr); |
1006 | if (ret < 0) | |
1007 | src = false; | |
1008 | ||
d23ff701 JA |
1009 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1010 | if (!msg) | |
1011 | return -ENOMEM; | |
1012 | ||
1013 | reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0, | |
1014 | info->genlhdr->cmd); | |
1015 | if (!reply) | |
1016 | goto nla_put_failure; | |
1017 | ||
3e5da62d | 1018 | hash ^= net_hash_mix(net); |
098a697b | 1019 | hash = hash_32(hash, tcp_metrics_hash_log); |
d23ff701 JA |
1020 | ret = -ESRCH; |
1021 | rcu_read_lock(); | |
098a697b | 1022 | for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm; |
d23ff701 | 1023 | tm = rcu_dereference(tm->tcpm_next)) { |
3e7013dd | 1024 | if (addr_same(&tm->tcpm_daddr, &daddr) && |
849e8a0c EB |
1025 | (!src || addr_same(&tm->tcpm_saddr, &saddr)) && |
1026 | net_eq(tm_net(tm), net)) { | |
d23ff701 JA |
1027 | ret = tcp_metrics_fill_info(msg, tm); |
1028 | break; | |
1029 | } | |
1030 | } | |
1031 | rcu_read_unlock(); | |
1032 | if (ret < 0) | |
1033 | goto out_free; | |
1034 | ||
1035 | genlmsg_end(msg, reply); | |
1036 | return genlmsg_reply(msg, info); | |
1037 | ||
1038 | nla_put_failure: | |
1039 | ret = -EMSGSIZE; | |
1040 | ||
1041 | out_free: | |
1042 | nlmsg_free(msg); | |
1043 | return ret; | |
1044 | } | |
1045 | ||
8a4bff71 | 1046 | static void tcp_metrics_flush_all(struct net *net) |
d23ff701 | 1047 | { |
098a697b EB |
1048 | unsigned int max_rows = 1U << tcp_metrics_hash_log; |
1049 | struct tcpm_hash_bucket *hb = tcp_metrics_hash; | |
d23ff701 JA |
1050 | struct tcp_metrics_block *tm; |
1051 | unsigned int row; | |
1052 | ||
1053 | for (row = 0; row < max_rows; row++, hb++) { | |
04f721c6 | 1054 | struct tcp_metrics_block __rcu **pp; |
d23ff701 | 1055 | spin_lock_bh(&tcp_metrics_lock); |
04f721c6 | 1056 | pp = &hb->chain; |
9f1ab186 | 1057 | for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) { |
04f721c6 EB |
1058 | if (net_eq(tm_net(tm), net)) { |
1059 | *pp = tm->tcpm_next; | |
1060 | kfree_rcu(tm, rcu_head); | |
1061 | } else { | |
1062 | pp = &tm->tcpm_next; | |
1063 | } | |
d23ff701 | 1064 | } |
04f721c6 | 1065 | spin_unlock_bh(&tcp_metrics_lock); |
d23ff701 | 1066 | } |
d23ff701 JA |
1067 | } |
1068 | ||
1069 | static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1070 | { | |
1071 | struct tcpm_hash_bucket *hb; | |
00ca9c5b | 1072 | struct tcp_metrics_block *tm; |
d23ff701 | 1073 | struct tcp_metrics_block __rcu **pp; |
3e7013dd | 1074 | struct inetpeer_addr saddr, daddr; |
d23ff701 JA |
1075 | unsigned int hash; |
1076 | struct net *net = genl_info_net(info); | |
1077 | int ret; | |
00ca9c5b | 1078 | bool src = true, found = false; |
d23ff701 | 1079 | |
324fd55a | 1080 | ret = parse_nl_addr(info, &daddr, &hash, 1); |
d23ff701 JA |
1081 | if (ret < 0) |
1082 | return ret; | |
8a4bff71 EB |
1083 | if (ret > 0) { |
1084 | tcp_metrics_flush_all(net); | |
1085 | return 0; | |
1086 | } | |
3e7013dd CP |
1087 | ret = parse_nl_saddr(info, &saddr); |
1088 | if (ret < 0) | |
1089 | src = false; | |
d23ff701 | 1090 | |
3e5da62d | 1091 | hash ^= net_hash_mix(net); |
098a697b EB |
1092 | hash = hash_32(hash, tcp_metrics_hash_log); |
1093 | hb = tcp_metrics_hash + hash; | |
d23ff701 JA |
1094 | pp = &hb->chain; |
1095 | spin_lock_bh(&tcp_metrics_lock); | |
9f1ab186 | 1096 | for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) { |
3e7013dd | 1097 | if (addr_same(&tm->tcpm_daddr, &daddr) && |
849e8a0c EB |
1098 | (!src || addr_same(&tm->tcpm_saddr, &saddr)) && |
1099 | net_eq(tm_net(tm), net)) { | |
d23ff701 | 1100 | *pp = tm->tcpm_next; |
00ca9c5b CP |
1101 | kfree_rcu(tm, rcu_head); |
1102 | found = true; | |
bbf852b9 CP |
1103 | } else { |
1104 | pp = &tm->tcpm_next; | |
d23ff701 JA |
1105 | } |
1106 | } | |
1107 | spin_unlock_bh(&tcp_metrics_lock); | |
00ca9c5b | 1108 | if (!found) |
d23ff701 | 1109 | return -ESRCH; |
d23ff701 JA |
1110 | return 0; |
1111 | } | |
1112 | ||
4534de83 | 1113 | static const struct genl_ops tcp_metrics_nl_ops[] = { |
d23ff701 JA |
1114 | { |
1115 | .cmd = TCP_METRICS_CMD_GET, | |
1116 | .doit = tcp_metrics_nl_cmd_get, | |
1117 | .dumpit = tcp_metrics_nl_dump, | |
1118 | .policy = tcp_metrics_nl_policy, | |
d23ff701 JA |
1119 | }, |
1120 | { | |
1121 | .cmd = TCP_METRICS_CMD_DEL, | |
1122 | .doit = tcp_metrics_nl_cmd_del, | |
1123 | .policy = tcp_metrics_nl_policy, | |
1124 | .flags = GENL_ADMIN_PERM, | |
1125 | }, | |
1126 | }; | |
1127 | ||
5815d5e7 | 1128 | static unsigned int tcpmhash_entries; |
51c5d0c4 DM |
1129 | static int __init set_tcpmhash_entries(char *str) |
1130 | { | |
1131 | ssize_t ret; | |
1132 | ||
1133 | if (!str) | |
1134 | return 0; | |
1135 | ||
5815d5e7 | 1136 | ret = kstrtouint(str, 0, &tcpmhash_entries); |
51c5d0c4 DM |
1137 | if (ret) |
1138 | return 0; | |
1139 | ||
1140 | return 1; | |
1141 | } | |
1142 | __setup("tcpmhash_entries=", set_tcpmhash_entries); | |
1143 | ||
1144 | static int __net_init tcp_net_metrics_init(struct net *net) | |
1145 | { | |
5815d5e7 ED |
1146 | size_t size; |
1147 | unsigned int slots; | |
51c5d0c4 | 1148 | |
098a697b EB |
1149 | if (!net_eq(net, &init_net)) |
1150 | return 0; | |
1151 | ||
51c5d0c4 DM |
1152 | slots = tcpmhash_entries; |
1153 | if (!slots) { | |
1154 | if (totalram_pages >= 128 * 1024) | |
1155 | slots = 16 * 1024; | |
1156 | else | |
1157 | slots = 8 * 1024; | |
1158 | } | |
1159 | ||
098a697b EB |
1160 | tcp_metrics_hash_log = order_base_2(slots); |
1161 | size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log; | |
51c5d0c4 | 1162 | |
098a697b EB |
1163 | tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
1164 | if (!tcp_metrics_hash) | |
1165 | tcp_metrics_hash = vzalloc(size); | |
976a702a | 1166 | |
098a697b | 1167 | if (!tcp_metrics_hash) |
51c5d0c4 DM |
1168 | return -ENOMEM; |
1169 | ||
51c5d0c4 DM |
1170 | return 0; |
1171 | } | |
1172 | ||
1173 | static void __net_exit tcp_net_metrics_exit(struct net *net) | |
1174 | { | |
098a697b | 1175 | tcp_metrics_flush_all(net); |
51c5d0c4 DM |
1176 | } |
1177 | ||
1178 | static __net_initdata struct pernet_operations tcp_net_metrics_ops = { | |
1179 | .init = tcp_net_metrics_init, | |
1180 | .exit = tcp_net_metrics_exit, | |
1181 | }; | |
1182 | ||
1183 | void __init tcp_metrics_init(void) | |
1184 | { | |
d23ff701 JA |
1185 | int ret; |
1186 | ||
1187 | ret = register_pernet_subsys(&tcp_net_metrics_ops); | |
1188 | if (ret < 0) | |
6493517e EB |
1189 | panic("Could not allocate the tcp_metrics hash table\n"); |
1190 | ||
d23ff701 | 1191 | ret = genl_register_family_with_ops(&tcp_metrics_nl_family, |
c53ed742 | 1192 | tcp_metrics_nl_ops); |
d23ff701 | 1193 | if (ret < 0) |
6493517e | 1194 | panic("Could not register tcp_metrics generic netlink\n"); |
51c5d0c4 | 1195 | } |