tcp_metrics.c 30.4 KB
Newer Older
1 2 3
#include <linux/rcupdate.h>
#include <linux/spinlock.h>
#include <linux/jiffies.h>
4
#include <linux/module.h>
5
#include <linux/cache.h>
6 7
#include <linux/slab.h>
#include <linux/init.h>
8
#include <linux/tcp.h>
E
Eric Dumazet 已提交
9
#include <linux/hash.h>
10
#include <linux/tcp_metrics.h>
11
#include <linux/vmalloc.h>
12 13

#include <net/inet_connection_sock.h>
14
#include <net/net_namespace.h>
15
#include <net/request_sock.h>
16
#include <net/inetpeer.h>
17
#include <net/sock.h>
18
#include <net/ipv6.h>
19 20
#include <net/dst.h>
#include <net/tcp.h>
21
#include <net/genetlink.h>
22 23 24

int sysctl_tcp_nometrics_save __read_mostly;

25 26
static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
						   const struct inetpeer_addr *daddr,
27 28
						   struct net *net, unsigned int hash);

29 30
struct tcp_fastopen_metrics {
	u16	mss;
31 32
	u16	syn_loss:10,		/* Recurring Fast Open SYN losses */
		try_exp:2;		/* Request w/ exp. option (once) */
33
	unsigned long	last_syn_loss;	/* Last Fast Open SYN loss */
34 35 36
	struct	tcp_fastopen_cookie	cookie;
};

37 38 39 40 41
/* TCP_METRIC_MAX includes 2 extra fields for userspace compatibility
 * Kernel only stores RTT and RTTVAR in usec resolution
 */
#define TCP_METRIC_MAX_KERNEL (TCP_METRIC_MAX - 2)

42 43
struct tcp_metrics_block {
	struct tcp_metrics_block __rcu	*tcpm_next;
44
	possible_net_t			tcpm_net;
45
	struct inetpeer_addr		tcpm_saddr;
46
	struct inetpeer_addr		tcpm_daddr;
47
	unsigned long			tcpm_stamp;
48 49
	u32				tcpm_ts;
	u32				tcpm_ts_stamp;
50
	u32				tcpm_lock;
51
	u32				tcpm_vals[TCP_METRIC_MAX_KERNEL + 1];
52
	struct tcp_fastopen_metrics	tcpm_fastopen;
53 54

	struct rcu_head			rcu_head;
55 56
};

57 58 59 60 61
static inline struct net *tm_net(struct tcp_metrics_block *tm)
{
	return read_pnet(&tm->tcpm_net);
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static bool tcp_metric_locked(struct tcp_metrics_block *tm,
			      enum tcp_metric_index idx)
{
	return tm->tcpm_lock & (1 << idx);
}

static u32 tcp_metric_get(struct tcp_metrics_block *tm,
			  enum tcp_metric_index idx)
{
	return tm->tcpm_vals[idx];
}

static void tcp_metric_set(struct tcp_metrics_block *tm,
			   enum tcp_metric_index idx,
			   u32 val)
{
	tm->tcpm_vals[idx] = val;
}

static bool addr_same(const struct inetpeer_addr *a,
		      const struct inetpeer_addr *b)
{
84
	return inetpeer_addr_cmp(a, b) == 0;
85 86 87 88 89 90
}

struct tcpm_hash_bucket {
	struct tcp_metrics_block __rcu	*chain;
};

91 92 93
static struct tcpm_hash_bucket	*tcp_metrics_hash __read_mostly;
static unsigned int		tcp_metrics_hash_log __read_mostly;

94 95
static DEFINE_SPINLOCK(tcp_metrics_lock);

96 97
static void tcpm_suck_dst(struct tcp_metrics_block *tm,
			  const struct dst_entry *dst,
98
			  bool fastopen_clear)
99
{
100
	u32 msval;
101 102
	u32 val;

103 104
	tm->tcpm_stamp = jiffies;

105 106 107 108 109 110 111 112 113 114 115 116 117
	val = 0;
	if (dst_metric_locked(dst, RTAX_RTT))
		val |= 1 << TCP_METRIC_RTT;
	if (dst_metric_locked(dst, RTAX_RTTVAR))
		val |= 1 << TCP_METRIC_RTTVAR;
	if (dst_metric_locked(dst, RTAX_SSTHRESH))
		val |= 1 << TCP_METRIC_SSTHRESH;
	if (dst_metric_locked(dst, RTAX_CWND))
		val |= 1 << TCP_METRIC_CWND;
	if (dst_metric_locked(dst, RTAX_REORDERING))
		val |= 1 << TCP_METRIC_REORDERING;
	tm->tcpm_lock = val;

118 119 120 121 122
	msval = dst_metric_raw(dst, RTAX_RTT);
	tm->tcpm_vals[TCP_METRIC_RTT] = msval * USEC_PER_MSEC;

	msval = dst_metric_raw(dst, RTAX_RTTVAR);
	tm->tcpm_vals[TCP_METRIC_RTTVAR] = msval * USEC_PER_MSEC;
123 124 125
	tm->tcpm_vals[TCP_METRIC_SSTHRESH] = dst_metric_raw(dst, RTAX_SSTHRESH);
	tm->tcpm_vals[TCP_METRIC_CWND] = dst_metric_raw(dst, RTAX_CWND);
	tm->tcpm_vals[TCP_METRIC_REORDERING] = dst_metric_raw(dst, RTAX_REORDERING);
126 127
	tm->tcpm_ts = 0;
	tm->tcpm_ts_stamp = 0;
128 129 130
	if (fastopen_clear) {
		tm->tcpm_fastopen.mss = 0;
		tm->tcpm_fastopen.syn_loss = 0;
131 132
		tm->tcpm_fastopen.try_exp = 0;
		tm->tcpm_fastopen.cookie.exp = false;
133 134
		tm->tcpm_fastopen.cookie.len = 0;
	}
135 136
}

137 138 139 140 141 142 143 144 145 146 147
#define TCP_METRICS_TIMEOUT		(60 * 60 * HZ)

static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
{
	if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
		tcpm_suck_dst(tm, dst, false);
}

#define TCP_METRICS_RECLAIM_DEPTH	5
#define TCP_METRICS_RECLAIM_PTR		(struct tcp_metrics_block *) 0x1UL

148 149 150
#define deref_locked(p)	\
	rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))

151
static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
152
					  struct inetpeer_addr *saddr,
153
					  struct inetpeer_addr *daddr,
154
					  unsigned int hash)
155 156 157
{
	struct tcp_metrics_block *tm;
	struct net *net;
158
	bool reclaim = false;
159 160 161

	spin_lock_bh(&tcp_metrics_lock);
	net = dev_net(dst->dev);
162 163 164 165

	/* While waiting for the spin-lock the cache might have been populated
	 * with this entry and so we have to check again.
	 */
166
	tm = __tcp_get_metrics(saddr, daddr, net, hash);
167 168 169 170 171 172 173 174 175
	if (tm == TCP_METRICS_RECLAIM_PTR) {
		reclaim = true;
		tm = NULL;
	}
	if (tm) {
		tcpm_check_stamp(tm, dst);
		goto out_unlock;
	}

176 177 178
	if (unlikely(reclaim)) {
		struct tcp_metrics_block *oldest;

179 180 181
		oldest = deref_locked(tcp_metrics_hash[hash].chain);
		for (tm = deref_locked(oldest->tcpm_next); tm;
		     tm = deref_locked(tm->tcpm_next)) {
182 183 184 185 186 187 188 189 190
			if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
				oldest = tm;
		}
		tm = oldest;
	} else {
		tm = kmalloc(sizeof(*tm), GFP_ATOMIC);
		if (!tm)
			goto out_unlock;
	}
191
	write_pnet(&tm->tcpm_net, net);
192
	tm->tcpm_saddr = *saddr;
193
	tm->tcpm_daddr = *daddr;
194

195
	tcpm_suck_dst(tm, dst, true);
196 197

	if (likely(!reclaim)) {
198 199
		tm->tcpm_next = tcp_metrics_hash[hash].chain;
		rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	}

out_unlock:
	spin_unlock_bh(&tcp_metrics_lock);
	return tm;
}

static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
{
	if (tm)
		return tm;
	if (depth > TCP_METRICS_RECLAIM_DEPTH)
		return TCP_METRICS_RECLAIM_PTR;
	return NULL;
}

216 217
static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *saddr,
						   const struct inetpeer_addr *daddr,
218 219 220 221 222
						   struct net *net, unsigned int hash)
{
	struct tcp_metrics_block *tm;
	int depth = 0;

223
	for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
224
	     tm = rcu_dereference(tm->tcpm_next)) {
225
		if (addr_same(&tm->tcpm_saddr, saddr) &&
226 227
		    addr_same(&tm->tcpm_daddr, daddr) &&
		    net_eq(tm_net(tm), net))
228 229 230 231 232 233 234 235 236 237
			break;
		depth++;
	}
	return tcp_get_encode(tm, depth);
}

static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
						       struct dst_entry *dst)
{
	struct tcp_metrics_block *tm;
238
	struct inetpeer_addr saddr, daddr;
239 240 241
	unsigned int hash;
	struct net *net;

242
	saddr.family = req->rsk_ops->family;
243 244
	daddr.family = req->rsk_ops->family;
	switch (daddr.family) {
245
	case AF_INET:
246 247
		inetpeer_set_addr_v4(&saddr, inet_rsk(req)->ir_loc_addr);
		inetpeer_set_addr_v4(&daddr, inet_rsk(req)->ir_rmt_addr);
248
		hash = ipv4_addr_hash(inet_rsk(req)->ir_rmt_addr);
249
		break;
250
#if IS_ENABLED(CONFIG_IPV6)
251
	case AF_INET6:
252 253
		inetpeer_set_addr_v6(&saddr, &inet_rsk(req)->ir_v6_loc_addr);
		inetpeer_set_addr_v6(&daddr, &inet_rsk(req)->ir_v6_rmt_addr);
254
		hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
255
		break;
256
#endif
257 258 259 260 261
	default:
		return NULL;
	}

	net = dev_net(dst->dev);
262
	hash ^= net_hash_mix(net);
263
	hash = hash_32(hash, tcp_metrics_hash_log);
264

265
	for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
266
	     tm = rcu_dereference(tm->tcpm_next)) {
267
		if (addr_same(&tm->tcpm_saddr, &saddr) &&
268 269
		    addr_same(&tm->tcpm_daddr, &daddr) &&
		    net_eq(tm_net(tm), net))
270 271 272 273 274 275
			break;
	}
	tcpm_check_stamp(tm, dst);
	return tm;
}

276 277 278
static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
{
	struct tcp_metrics_block *tm;
279
	struct inetpeer_addr saddr, daddr;
280 281 282
	unsigned int hash;
	struct net *net;

283
	if (tw->tw_family == AF_INET) {
284 285
		inetpeer_set_addr_v4(&saddr, tw->tw_rcv_saddr);
		inetpeer_set_addr_v4(&daddr, tw->tw_daddr);
286
		hash = ipv4_addr_hash(tw->tw_daddr);
287
	}
288
#if IS_ENABLED(CONFIG_IPV6)
289 290
	else if (tw->tw_family == AF_INET6) {
		if (ipv6_addr_v4mapped(&tw->tw_v6_daddr)) {
291 292
			inetpeer_set_addr_v4(&saddr, tw->tw_rcv_saddr);
			inetpeer_set_addr_v4(&daddr, tw->tw_daddr);
293
			hash = ipv4_addr_hash(tw->tw_daddr);
294
		} else {
295 296
			inetpeer_set_addr_v6(&saddr, &tw->tw_v6_rcv_saddr);
			inetpeer_set_addr_v6(&daddr, &tw->tw_v6_daddr);
297 298 299
			hash = ipv6_addr_hash(&tw->tw_v6_daddr);
		}
	}
300
#endif
301
	else
302 303 304
		return NULL;

	net = twsk_net(tw);
305
	hash ^= net_hash_mix(net);
306
	hash = hash_32(hash, tcp_metrics_hash_log);
307

308
	for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
309
	     tm = rcu_dereference(tm->tcpm_next)) {
310
		if (addr_same(&tm->tcpm_saddr, &saddr) &&
311 312
		    addr_same(&tm->tcpm_daddr, &daddr) &&
		    net_eq(tm_net(tm), net))
313 314 315 316 317
			break;
	}
	return tm;
}

318 319 320 321 322
static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
						 struct dst_entry *dst,
						 bool create)
{
	struct tcp_metrics_block *tm;
323
	struct inetpeer_addr saddr, daddr;
324 325 326
	unsigned int hash;
	struct net *net;

327
	if (sk->sk_family == AF_INET) {
328 329
		inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
		inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
330
		hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
331
	}
332
#if IS_ENABLED(CONFIG_IPV6)
333 334
	else if (sk->sk_family == AF_INET6) {
		if (ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
335 336
			inetpeer_set_addr_v4(&saddr, inet_sk(sk)->inet_saddr);
			inetpeer_set_addr_v4(&daddr, inet_sk(sk)->inet_daddr);
337
			hash = ipv4_addr_hash(inet_sk(sk)->inet_daddr);
338
		} else {
339 340
			inetpeer_set_addr_v6(&saddr, &sk->sk_v6_rcv_saddr);
			inetpeer_set_addr_v6(&daddr, &sk->sk_v6_daddr);
341 342 343
			hash = ipv6_addr_hash(&sk->sk_v6_daddr);
		}
	}
344
#endif
345
	else
346 347 348
		return NULL;

	net = dev_net(dst->dev);
349
	hash ^= net_hash_mix(net);
350
	hash = hash_32(hash, tcp_metrics_hash_log);
351

352
	tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
353
	if (tm == TCP_METRICS_RECLAIM_PTR)
354 355
		tm = NULL;
	if (!tm && create)
356
		tm = tcpm_new(dst, &saddr, &daddr, hash);
357 358 359 360 361 362
	else
		tcpm_check_stamp(tm, dst);

	return tm;
}

363 364 365 366 367 368
/* Save metrics learned by this TCP session.  This function is called
 * only, when TCP finishes successfully i.e. when it enters TIME-WAIT
 * or goes from LAST-ACK to CLOSE.
 */
void tcp_update_metrics(struct sock *sk)
{
369
	const struct inet_connection_sock *icsk = inet_csk(sk);
370
	struct dst_entry *dst = __sk_dst_get(sk);
371 372 373 374 375
	struct tcp_sock *tp = tcp_sk(sk);
	struct tcp_metrics_block *tm;
	unsigned long rtt;
	u32 val;
	int m;
376

377
	if (sysctl_tcp_nometrics_save || !dst)
378 379
		return;

380
	if (dst->flags & DST_HOST)
381 382
		dst_confirm(dst);

383
	rcu_read_lock();
384
	if (icsk->icsk_backoff || !tp->srtt_us) {
385 386 387 388 389 390 391 392 393 394
		/* This session failed to estimate rtt. Why?
		 * Probably, no packets returned in time.  Reset our
		 * results.
		 */
		tm = tcp_get_metrics(sk, dst, false);
		if (tm && !tcp_metric_locked(tm, TCP_METRIC_RTT))
			tcp_metric_set(tm, TCP_METRIC_RTT, 0);
		goto out_unlock;
	} else
		tm = tcp_get_metrics(sk, dst, true);
395

396 397
	if (!tm)
		goto out_unlock;
398

399 400
	rtt = tcp_metric_get(tm, TCP_METRIC_RTT);
	m = rtt - tp->srtt_us;
401

402 403 404 405 406 407
	/* If newly calculated rtt larger than stored one, store new
	 * one. Otherwise, use EWMA. Remember, rtt overestimation is
	 * always better than underestimation.
	 */
	if (!tcp_metric_locked(tm, TCP_METRIC_RTT)) {
		if (m <= 0)
408
			rtt = tp->srtt_us;
409 410
		else
			rtt -= (m >> 3);
411
		tcp_metric_set(tm, TCP_METRIC_RTT, rtt);
412
	}
413

414 415
	if (!tcp_metric_locked(tm, TCP_METRIC_RTTVAR)) {
		unsigned long var;
416

417 418
		if (m < 0)
			m = -m;
419

420 421
		/* Scale deviation to rttvar fixed point */
		m >>= 1;
422 423
		if (m < tp->mdev_us)
			m = tp->mdev_us;
424

425
		var = tcp_metric_get(tm, TCP_METRIC_RTTVAR);
426 427 428 429
		if (m >= var)
			var = m;
		else
			var -= (var - m) >> 2;
430

431
		tcp_metric_set(tm, TCP_METRIC_RTTVAR, var);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	}

	if (tcp_in_initial_slowstart(tp)) {
		/* Slow start still did not finish. */
		if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
			val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
			if (val && (tp->snd_cwnd >> 1) > val)
				tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
					       tp->snd_cwnd >> 1);
		}
		if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
			val = tcp_metric_get(tm, TCP_METRIC_CWND);
			if (tp->snd_cwnd > val)
				tcp_metric_set(tm, TCP_METRIC_CWND,
					       tp->snd_cwnd);
		}
448
	} else if (!tcp_in_slow_start(tp) &&
449 450 451 452 453 454 455
		   icsk->icsk_ca_state == TCP_CA_Open) {
		/* Cong. avoidance phase, cwnd is reliable. */
		if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH))
			tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
				       max(tp->snd_cwnd >> 1, tp->snd_ssthresh));
		if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
			val = tcp_metric_get(tm, TCP_METRIC_CWND);
456
			tcp_metric_set(tm, TCP_METRIC_CWND, (val + tp->snd_cwnd) >> 1);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
		}
	} else {
		/* Else slow start did not finish, cwnd is non-sense,
		 * ssthresh may be also invalid.
		 */
		if (!tcp_metric_locked(tm, TCP_METRIC_CWND)) {
			val = tcp_metric_get(tm, TCP_METRIC_CWND);
			tcp_metric_set(tm, TCP_METRIC_CWND,
				       (val + tp->snd_ssthresh) >> 1);
		}
		if (!tcp_metric_locked(tm, TCP_METRIC_SSTHRESH)) {
			val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
			if (val && tp->snd_ssthresh > val)
				tcp_metric_set(tm, TCP_METRIC_SSTHRESH,
					       tp->snd_ssthresh);
		}
		if (!tcp_metric_locked(tm, TCP_METRIC_REORDERING)) {
			val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
			if (val < tp->reordering &&
476
			    tp->reordering != sysctl_tcp_reordering)
477 478
				tcp_metric_set(tm, TCP_METRIC_REORDERING,
					       tp->reordering);
479 480
		}
	}
481 482 483
	tm->tcpm_stamp = jiffies;
out_unlock:
	rcu_read_unlock();
484 485 486 487 488 489 490
}

/* Initialize metrics on socket. */

void tcp_init_metrics(struct sock *sk)
{
	struct dst_entry *dst = __sk_dst_get(sk);
491 492
	struct tcp_sock *tp = tcp_sk(sk);
	struct tcp_metrics_block *tm;
493
	u32 val, crtt = 0; /* cached RTT scaled by 8 */
494

495
	if (!dst)
496 497 498 499
		goto reset;

	dst_confirm(dst);

500 501 502 503 504 505 506 507 508 509 510 511 512
	rcu_read_lock();
	tm = tcp_get_metrics(sk, dst, true);
	if (!tm) {
		rcu_read_unlock();
		goto reset;
	}

	if (tcp_metric_locked(tm, TCP_METRIC_CWND))
		tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);

	val = tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
	if (val) {
		tp->snd_ssthresh = val;
513 514 515 516 517 518 519 520
		if (tp->snd_ssthresh > tp->snd_cwnd_clamp)
			tp->snd_ssthresh = tp->snd_cwnd_clamp;
	} else {
		/* ssthresh may have been reduced unnecessarily during.
		 * 3WHS. Restore it back to its initial default.
		 */
		tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
	}
521 522
	val = tcp_metric_get(tm, TCP_METRIC_REORDERING);
	if (val && tp->reordering != val) {
523 524
		tcp_disable_fack(tp);
		tcp_disable_early_retrans(tp);
525
		tp->reordering = val;
526 527
	}

528
	crtt = tcp_metric_get(tm, TCP_METRIC_RTT);
529
	rcu_read_unlock();
530
reset:
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	/* The initial RTT measurement from the SYN/SYN-ACK is not ideal
	 * to seed the RTO for later data packets because SYN packets are
	 * small. Use the per-dst cached values to seed the RTO but keep
	 * the RTT estimator variables intact (e.g., srtt, mdev, rttvar).
	 * Later the RTO will be updated immediately upon obtaining the first
	 * data RTT sample (tcp_rtt_estimator()). Hence the cached RTT only
	 * influences the first RTO but not later RTT estimation.
	 *
	 * But if RTT is not available from the SYN (due to retransmits or
	 * syn cookies) or the cache, force a conservative 3secs timeout.
	 *
	 * A bit of theory. RTT is time passed after "normal" sized packet
	 * is sent until it is ACKed. In normal circumstances sending small
	 * packets force peer to delay ACKs and calculation is correct too.
	 * The algorithm is adaptive and, provided we follow specs, it
	 * NEVER underestimate RTT. BUT! If peer tries to make some clever
	 * tricks sort of "quick acks" for time long enough to decrease RTT
	 * to low value, and then abruptly stops to do it and starts to delay
	 * ACKs, wait for troubles.
	 */
551
	if (crtt > tp->srtt_us) {
552
		/* Set RTO like tcp_rtt_estimator(), but from cached RTT. */
553
		crtt /= 8 * USEC_PER_MSEC;
554
		inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk));
555
	} else if (tp->srtt_us == 0) {
556 557 558 559 560 561
		/* RFC6298: 5.7 We've failed to get a valid RTT sample from
		 * 3WHS. This is most likely due to retransmission,
		 * including spurious one. Reset the RTO back to 3secs
		 * from the more aggressive 1sec to avoid more spurious
		 * retransmission.
		 */
562 563 564
		tp->rttvar_us = jiffies_to_usecs(TCP_TIMEOUT_FALLBACK);
		tp->mdev_us = tp->mdev_max_us = tp->rttvar_us;

565 566 567 568 569 570 571 572 573 574 575 576 577
		inet_csk(sk)->icsk_rto = TCP_TIMEOUT_FALLBACK;
	}
	/* Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
	 * retransmitted. In light of RFC6298 more aggressive 1sec
	 * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
	 * retransmission has occurred.
	 */
	if (tp->total_retrans > 1)
		tp->snd_cwnd = 1;
	else
		tp->snd_cwnd = tcp_init_cwnd(tp, dst);
	tp->snd_cwnd_stamp = tcp_time_stamp;
}
578

579 580
bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
			bool paws_check, bool timestamps)
581
{
582 583 584
	struct tcp_metrics_block *tm;
	bool ret;

585 586
	if (!dst)
		return false;
587 588 589

	rcu_read_lock();
	tm = __tcp_get_metrics_req(req, dst);
590 591 592
	if (paws_check) {
		if (tm &&
		    (u32)get_seconds() - tm->tcpm_ts_stamp < TCP_PAWS_MSL &&
593 594
		    ((s32)(tm->tcpm_ts - req->ts_recent) > TCP_PAWS_WINDOW ||
		     !timestamps))
595 596 597 598 599 600 601 602 603
			ret = false;
		else
			ret = true;
	} else {
		if (tm && tcp_metric_get(tm, TCP_METRIC_RTT) && tm->tcpm_ts_stamp)
			ret = true;
		else
			ret = false;
	}
604 605 606
	rcu_read_unlock();

	return ret;
607 608
}
EXPORT_SYMBOL_GPL(tcp_peer_is_proven);
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst)
{
	struct tcp_metrics_block *tm;

	rcu_read_lock();
	tm = tcp_get_metrics(sk, dst, true);
	if (tm) {
		struct tcp_sock *tp = tcp_sk(sk);

		if ((u32)get_seconds() - tm->tcpm_ts_stamp <= TCP_PAWS_MSL) {
			tp->rx_opt.ts_recent_stamp = tm->tcpm_ts_stamp;
			tp->rx_opt.ts_recent = tm->tcpm_ts;
		}
	}
	rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(tcp_fetch_timewait_stamp);

/* VJ's idea. Save last timestamp seen from this destination and hold
 * it at least for normal timewait interval to use for duplicate
 * segment detection in subsequent connections, before they enter
 * synchronized state.
 */
bool tcp_remember_stamp(struct sock *sk)
{
	struct dst_entry *dst = __sk_dst_get(sk);
	bool ret = false;

	if (dst) {
		struct tcp_metrics_block *tm;

		rcu_read_lock();
		tm = tcp_get_metrics(sk, dst, true);
		if (tm) {
			struct tcp_sock *tp = tcp_sk(sk);

			if ((s32)(tm->tcpm_ts - tp->rx_opt.ts_recent) <= 0 ||
			    ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
			     tm->tcpm_ts_stamp <= (u32)tp->rx_opt.ts_recent_stamp)) {
				tm->tcpm_ts_stamp = (u32)tp->rx_opt.ts_recent_stamp;
				tm->tcpm_ts = tp->rx_opt.ts_recent;
			}
			ret = true;
		}
		rcu_read_unlock();
	}
	return ret;
}

bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw)
{
	struct tcp_metrics_block *tm;
	bool ret = false;

	rcu_read_lock();
	tm = __tcp_get_metrics_tw(tw);
666
	if (tm) {
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		const struct tcp_timewait_sock *tcptw;
		struct sock *sk = (struct sock *) tw;

		tcptw = tcp_twsk(sk);
		if ((s32)(tm->tcpm_ts - tcptw->tw_ts_recent) <= 0 ||
		    ((u32)get_seconds() - tm->tcpm_ts_stamp > TCP_PAWS_MSL &&
		     tm->tcpm_ts_stamp <= (u32)tcptw->tw_ts_recent_stamp)) {
			tm->tcpm_ts_stamp = (u32)tcptw->tw_ts_recent_stamp;
			tm->tcpm_ts	   = tcptw->tw_ts_recent;
		}
		ret = true;
	}
	rcu_read_unlock();

	return ret;
}

684 685 686
static DEFINE_SEQLOCK(fastopen_seqlock);

void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
687 688
			    struct tcp_fastopen_cookie *cookie,
			    int *syn_loss, unsigned long *last_syn_loss)
689 690 691 692 693 694 695 696 697 698 699 700 701 702
{
	struct tcp_metrics_block *tm;

	rcu_read_lock();
	tm = tcp_get_metrics(sk, __sk_dst_get(sk), false);
	if (tm) {
		struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
		unsigned int seq;

		do {
			seq = read_seqbegin(&fastopen_seqlock);
			if (tfom->mss)
				*mss = tfom->mss;
			*cookie = tfom->cookie;
703 704
			if (cookie->len <= 0 && tfom->try_exp == 1)
				cookie->exp = true;
705 706
			*syn_loss = tfom->syn_loss;
			*last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
707 708 709 710 711 712
		} while (read_seqretry(&fastopen_seqlock, seq));
	}
	rcu_read_unlock();
}

void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
713 714
			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
			    u16 try_exp)
715
{
716
	struct dst_entry *dst = __sk_dst_get(sk);
717 718
	struct tcp_metrics_block *tm;

719 720
	if (!dst)
		return;
721
	rcu_read_lock();
722
	tm = tcp_get_metrics(sk, dst, true);
723 724 725 726
	if (tm) {
		struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;

		write_seqlock_bh(&fastopen_seqlock);
727 728 729
		if (mss)
			tfom->mss = mss;
		if (cookie && cookie->len > 0)
730
			tfom->cookie = *cookie;
731 732 733
		else if (try_exp > tfom->try_exp &&
			 tfom->cookie.len <= 0 && !tfom->cookie.exp)
			tfom->try_exp = try_exp;
734 735 736 737 738
		if (syn_lost) {
			++tfom->syn_loss;
			tfom->last_syn_loss = jiffies;
		} else
			tfom->syn_loss = 0;
739 740 741 742 743
		write_sequnlock_bh(&fastopen_seqlock);
	}
	rcu_read_unlock();
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
static struct genl_family tcp_metrics_nl_family = {
	.id		= GENL_ID_GENERATE,
	.hdrsize	= 0,
	.name		= TCP_METRICS_GENL_NAME,
	.version	= TCP_METRICS_GENL_VERSION,
	.maxattr	= TCP_METRICS_ATTR_MAX,
	.netnsok	= true,
};

static struct nla_policy tcp_metrics_nl_policy[TCP_METRICS_ATTR_MAX + 1] = {
	[TCP_METRICS_ATTR_ADDR_IPV4]	= { .type = NLA_U32, },
	[TCP_METRICS_ATTR_ADDR_IPV6]	= { .type = NLA_BINARY,
					    .len = sizeof(struct in6_addr), },
	/* Following attributes are not received for GET/DEL,
	 * we keep them for reference
	 */
#if 0
	[TCP_METRICS_ATTR_AGE]		= { .type = NLA_MSECS, },
	[TCP_METRICS_ATTR_TW_TSVAL]	= { .type = NLA_U32, },
	[TCP_METRICS_ATTR_TW_TS_STAMP]	= { .type = NLA_S32, },
	[TCP_METRICS_ATTR_VALS]		= { .type = NLA_NESTED, },
	[TCP_METRICS_ATTR_FOPEN_MSS]	= { .type = NLA_U16, },
	[TCP_METRICS_ATTR_FOPEN_SYN_DROPS]	= { .type = NLA_U16, },
	[TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS]	= { .type = NLA_MSECS, },
	[TCP_METRICS_ATTR_FOPEN_COOKIE]	= { .type = NLA_BINARY,
					    .len = TCP_FASTOPEN_COOKIE_MAX, },
#endif
};

/* Add attributes, caller cancels its header on failure */
static int tcp_metrics_fill_info(struct sk_buff *msg,
				 struct tcp_metrics_block *tm)
{
	struct nlattr *nest;
	int i;

780
	switch (tm->tcpm_daddr.family) {
781
	case AF_INET:
782
		if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
783
				    inetpeer_get_addr_v4(&tm->tcpm_daddr)) < 0)
784
			goto nla_put_failure;
785
		if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
786
				    inetpeer_get_addr_v4(&tm->tcpm_saddr)) < 0)
787
			goto nla_put_failure;
788 789
		break;
	case AF_INET6:
790
		if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
791
				     inetpeer_get_addr_v6(&tm->tcpm_daddr)) < 0)
792
			goto nla_put_failure;
793
		if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
794
				     inetpeer_get_addr_v6(&tm->tcpm_saddr)) < 0)
795
			goto nla_put_failure;
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
		break;
	default:
		return -EAFNOSUPPORT;
	}

	if (nla_put_msecs(msg, TCP_METRICS_ATTR_AGE,
			  jiffies - tm->tcpm_stamp) < 0)
		goto nla_put_failure;
	if (tm->tcpm_ts_stamp) {
		if (nla_put_s32(msg, TCP_METRICS_ATTR_TW_TS_STAMP,
				(s32) (get_seconds() - tm->tcpm_ts_stamp)) < 0)
			goto nla_put_failure;
		if (nla_put_u32(msg, TCP_METRICS_ATTR_TW_TSVAL,
				tm->tcpm_ts) < 0)
			goto nla_put_failure;
	}

	{
		int n = 0;

		nest = nla_nest_start(msg, TCP_METRICS_ATTR_VALS);
		if (!nest)
			goto nla_put_failure;
819 820 821 822
		for (i = 0; i < TCP_METRIC_MAX_KERNEL + 1; i++) {
			u32 val = tm->tcpm_vals[i];

			if (!val)
823
				continue;
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
			if (i == TCP_METRIC_RTT) {
				if (nla_put_u32(msg, TCP_METRIC_RTT_US + 1,
						val) < 0)
					goto nla_put_failure;
				n++;
				val = max(val / 1000, 1U);
			}
			if (i == TCP_METRIC_RTTVAR) {
				if (nla_put_u32(msg, TCP_METRIC_RTTVAR_US + 1,
						val) < 0)
					goto nla_put_failure;
				n++;
				val = max(val / 1000, 1U);
			}
			if (nla_put_u32(msg, i + 1, val) < 0)
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
				goto nla_put_failure;
			n++;
		}
		if (n)
			nla_nest_end(msg, nest);
		else
			nla_nest_cancel(msg, nest);
	}

	{
		struct tcp_fastopen_metrics tfom_copy[1], *tfom;
		unsigned int seq;

		do {
			seq = read_seqbegin(&fastopen_seqlock);
			tfom_copy[0] = tm->tcpm_fastopen;
		} while (read_seqretry(&fastopen_seqlock, seq));

		tfom = tfom_copy;
		if (tfom->mss &&
		    nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_MSS,
				tfom->mss) < 0)
			goto nla_put_failure;
		if (tfom->syn_loss &&
		    (nla_put_u16(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROPS,
				tfom->syn_loss) < 0 ||
		     nla_put_msecs(msg, TCP_METRICS_ATTR_FOPEN_SYN_DROP_TS,
				jiffies - tfom->last_syn_loss) < 0))
			goto nla_put_failure;
		if (tfom->cookie.len > 0 &&
		    nla_put(msg, TCP_METRICS_ATTR_FOPEN_COOKIE,
			    tfom->cookie.len, tfom->cookie.val) < 0)
			goto nla_put_failure;
	}

	return 0;

nla_put_failure:
	return -EMSGSIZE;
}

static int tcp_metrics_dump_info(struct sk_buff *skb,
				 struct netlink_callback *cb,
				 struct tcp_metrics_block *tm)
{
	void *hdr;

886
	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
887 888 889 890 891 892 893 894
			  &tcp_metrics_nl_family, NLM_F_MULTI,
			  TCP_METRICS_CMD_GET);
	if (!hdr)
		return -EMSGSIZE;

	if (tcp_metrics_fill_info(skb, tm) < 0)
		goto nla_put_failure;

895 896
	genlmsg_end(skb, hdr);
	return 0;
897 898 899 900 901 902 903 904 905 906

nla_put_failure:
	genlmsg_cancel(skb, hdr);
	return -EMSGSIZE;
}

static int tcp_metrics_nl_dump(struct sk_buff *skb,
			       struct netlink_callback *cb)
{
	struct net *net = sock_net(skb->sk);
907
	unsigned int max_rows = 1U << tcp_metrics_hash_log;
908 909 910 911 912
	unsigned int row, s_row = cb->args[0];
	int s_col = cb->args[1], col = s_col;

	for (row = s_row; row < max_rows; row++, s_col = 0) {
		struct tcp_metrics_block *tm;
913
		struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
914 915 916 917

		rcu_read_lock();
		for (col = 0, tm = rcu_dereference(hb->chain); tm;
		     tm = rcu_dereference(tm->tcpm_next), col++) {
918 919
			if (!net_eq(tm_net(tm), net))
				continue;
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
			if (col < s_col)
				continue;
			if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
				rcu_read_unlock();
				goto done;
			}
		}
		rcu_read_unlock();
	}

done:
	cb->args[0] = row;
	cb->args[1] = col;
	return skb->len;
}

936 937
static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
			   unsigned int *hash, int optional, int v4, int v6)
938 939 940
{
	struct nlattr *a;

941
	a = info->attrs[v4];
942
	if (a) {
943
		inetpeer_set_addr_v4(addr, nla_get_in_addr(a));
944
		if (hash)
945
			*hash = ipv4_addr_hash(inetpeer_get_addr_v4(addr));
946 947
		return 0;
	}
948
	a = info->attrs[v6];
949
	if (a) {
950 951
		struct in6_addr in6;

952
		if (nla_len(a) != sizeof(struct in6_addr))
953
			return -EINVAL;
954 955
		in6 = nla_get_in6_addr(a);
		inetpeer_set_addr_v6(addr, &in6);
956
		if (hash)
957
			*hash = ipv6_addr_hash(inetpeer_get_addr_v6(addr));
958 959 960 961 962
		return 0;
	}
	return optional ? 1 : -EAFNOSUPPORT;
}

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
static int parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
			 unsigned int *hash, int optional)
{
	return __parse_nl_addr(info, addr, hash, optional,
			       TCP_METRICS_ATTR_ADDR_IPV4,
			       TCP_METRICS_ATTR_ADDR_IPV6);
}

static int parse_nl_saddr(struct genl_info *info, struct inetpeer_addr *addr)
{
	return __parse_nl_addr(info, addr, NULL, 0,
			       TCP_METRICS_ATTR_SADDR_IPV4,
			       TCP_METRICS_ATTR_SADDR_IPV6);
}

978 979 980
static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
{
	struct tcp_metrics_block *tm;
981
	struct inetpeer_addr saddr, daddr;
982 983 984 985 986
	unsigned int hash;
	struct sk_buff *msg;
	struct net *net = genl_info_net(info);
	void *reply;
	int ret;
987
	bool src = true;
988

989
	ret = parse_nl_addr(info, &daddr, &hash, 0);
990 991 992
	if (ret < 0)
		return ret;

993 994 995 996
	ret = parse_nl_saddr(info, &saddr);
	if (ret < 0)
		src = false;

997 998 999 1000 1001 1002 1003 1004 1005
	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
	if (!msg)
		return -ENOMEM;

	reply = genlmsg_put_reply(msg, info, &tcp_metrics_nl_family, 0,
				  info->genlhdr->cmd);
	if (!reply)
		goto nla_put_failure;

1006
	hash ^= net_hash_mix(net);
1007
	hash = hash_32(hash, tcp_metrics_hash_log);
1008 1009
	ret = -ESRCH;
	rcu_read_lock();
1010
	for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
1011
	     tm = rcu_dereference(tm->tcpm_next)) {
1012
		if (addr_same(&tm->tcpm_daddr, &daddr) &&
1013 1014
		    (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
		    net_eq(tm_net(tm), net)) {
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
			ret = tcp_metrics_fill_info(msg, tm);
			break;
		}
	}
	rcu_read_unlock();
	if (ret < 0)
		goto out_free;

	genlmsg_end(msg, reply);
	return genlmsg_reply(msg, info);

nla_put_failure:
	ret = -EMSGSIZE;

out_free:
	nlmsg_free(msg);
	return ret;
}

1034
static void tcp_metrics_flush_all(struct net *net)
1035
{
1036 1037
	unsigned int max_rows = 1U << tcp_metrics_hash_log;
	struct tcpm_hash_bucket *hb = tcp_metrics_hash;
1038 1039 1040 1041
	struct tcp_metrics_block *tm;
	unsigned int row;

	for (row = 0; row < max_rows; row++, hb++) {
1042
		struct tcp_metrics_block __rcu **pp;
1043
		spin_lock_bh(&tcp_metrics_lock);
1044
		pp = &hb->chain;
1045
		for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1046 1047 1048 1049 1050 1051
			if (net_eq(tm_net(tm), net)) {
				*pp = tm->tcpm_next;
				kfree_rcu(tm, rcu_head);
			} else {
				pp = &tm->tcpm_next;
			}
1052
		}
1053
		spin_unlock_bh(&tcp_metrics_lock);
1054 1055 1056 1057 1058 1059
	}
}

static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
{
	struct tcpm_hash_bucket *hb;
1060
	struct tcp_metrics_block *tm;
1061
	struct tcp_metrics_block __rcu **pp;
1062
	struct inetpeer_addr saddr, daddr;
1063 1064 1065
	unsigned int hash;
	struct net *net = genl_info_net(info);
	int ret;
1066
	bool src = true, found = false;
1067

1068
	ret = parse_nl_addr(info, &daddr, &hash, 1);
1069 1070
	if (ret < 0)
		return ret;
1071 1072 1073 1074
	if (ret > 0) {
		tcp_metrics_flush_all(net);
		return 0;
	}
1075 1076 1077
	ret = parse_nl_saddr(info, &saddr);
	if (ret < 0)
		src = false;
1078

1079
	hash ^= net_hash_mix(net);
1080 1081
	hash = hash_32(hash, tcp_metrics_hash_log);
	hb = tcp_metrics_hash + hash;
1082 1083
	pp = &hb->chain;
	spin_lock_bh(&tcp_metrics_lock);
1084
	for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1085
		if (addr_same(&tm->tcpm_daddr, &daddr) &&
1086 1087
		    (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
		    net_eq(tm_net(tm), net)) {
1088
			*pp = tm->tcpm_next;
1089 1090
			kfree_rcu(tm, rcu_head);
			found = true;
1091 1092
		} else {
			pp = &tm->tcpm_next;
1093 1094 1095
		}
	}
	spin_unlock_bh(&tcp_metrics_lock);
1096
	if (!found)
1097 1098 1099 1100
		return -ESRCH;
	return 0;
}

1101
static const struct genl_ops tcp_metrics_nl_ops[] = {
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	{
		.cmd = TCP_METRICS_CMD_GET,
		.doit = tcp_metrics_nl_cmd_get,
		.dumpit = tcp_metrics_nl_dump,
		.policy = tcp_metrics_nl_policy,
	},
	{
		.cmd = TCP_METRICS_CMD_DEL,
		.doit = tcp_metrics_nl_cmd_del,
		.policy = tcp_metrics_nl_policy,
		.flags = GENL_ADMIN_PERM,
	},
};

E
Eric Dumazet 已提交
1116
static unsigned int tcpmhash_entries;
1117 1118 1119 1120 1121 1122 1123
static int __init set_tcpmhash_entries(char *str)
{
	ssize_t ret;

	if (!str)
		return 0;

E
Eric Dumazet 已提交
1124
	ret = kstrtouint(str, 0, &tcpmhash_entries);
1125 1126 1127 1128 1129 1130 1131 1132 1133
	if (ret)
		return 0;

	return 1;
}
__setup("tcpmhash_entries=", set_tcpmhash_entries);

static int __net_init tcp_net_metrics_init(struct net *net)
{
E
Eric Dumazet 已提交
1134 1135
	size_t size;
	unsigned int slots;
1136

1137 1138 1139
	if (!net_eq(net, &init_net))
		return 0;

1140 1141 1142 1143 1144 1145 1146 1147
	slots = tcpmhash_entries;
	if (!slots) {
		if (totalram_pages >= 128 * 1024)
			slots = 16 * 1024;
		else
			slots = 8 * 1024;
	}

1148 1149
	tcp_metrics_hash_log = order_base_2(slots);
	size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
1150

1151 1152 1153
	tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
	if (!tcp_metrics_hash)
		tcp_metrics_hash = vzalloc(size);
1154

1155
	if (!tcp_metrics_hash)
1156 1157 1158 1159 1160 1161 1162
		return -ENOMEM;

	return 0;
}

static void __net_exit tcp_net_metrics_exit(struct net *net)
{
1163
	tcp_metrics_flush_all(net);
1164 1165 1166 1167 1168 1169 1170 1171 1172
}

static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
	.init	=	tcp_net_metrics_init,
	.exit	=	tcp_net_metrics_exit,
};

void __init tcp_metrics_init(void)
{
1173 1174 1175 1176
	int ret;

	ret = register_pernet_subsys(&tcp_net_metrics_ops);
	if (ret < 0)
1177 1178
		panic("Could not allocate the tcp_metrics hash table\n");

1179
	ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1180
					    tcp_metrics_nl_ops);
1181
	if (ret < 0)
1182
		panic("Could not register tcp_metrics generic netlink\n");
1183
}