peer_object.c 12.0 KB
Newer Older
1
/* RxRPC remote transport endpoint record management
2
 *
3
 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
4 5 6 7 8 9 10 11
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15 16 17 18
#include <linux/module.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/in.h>
D
David Howells 已提交
19
#include <linux/in6.h>
20
#include <linux/slab.h>
21
#include <linux/hashtable.h>
22 23 24
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <net/ip.h>
25
#include <net/route.h>
D
David Howells 已提交
26
#include <net/ip6_route.h>
27 28
#include "ar-internal.h"

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/*
 * Hash a peer key.
 */
static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
					 const struct sockaddr_rxrpc *srx)
{
	const u16 *p;
	unsigned int i, size;
	unsigned long hash_key;

	_enter("");

	hash_key = (unsigned long)local / __alignof__(*local);
	hash_key += srx->transport_type;
	hash_key += srx->transport_len;
	hash_key += srx->transport.family;

	switch (srx->transport.family) {
	case AF_INET:
		hash_key += (u16 __force)srx->transport.sin.sin_port;
		size = sizeof(srx->transport.sin.sin_addr);
		p = (u16 *)&srx->transport.sin.sin_addr;
		break;
52
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
53 54 55 56 57
	case AF_INET6:
		hash_key += (u16 __force)srx->transport.sin.sin_port;
		size = sizeof(srx->transport.sin6.sin6_addr);
		p = (u16 *)&srx->transport.sin6.sin6_addr;
		break;
58
#endif
59 60 61
	default:
		WARN(1, "AF_RXRPC: Unsupported transport address family\n");
		return 0;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	}

	/* Step through the peer address in 16-bit portions for speed */
	for (i = 0; i < size; i += sizeof(*p), p++)
		hash_key += *p;

	_leave(" 0x%lx", hash_key);
	return hash_key;
}

/*
 * Compare a peer to a key.  Return -ve, 0 or +ve to indicate less than, same
 * or greater than.
 *
 * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
 * buckets and mid-bucket insertion, so we don't make full use of this
 * information at this point.
 */
static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
			       struct rxrpc_local *local,
			       const struct sockaddr_rxrpc *srx,
			       unsigned long hash_key)
{
	long diff;

	diff = ((peer->hash_key - hash_key) ?:
		((unsigned long)peer->local - (unsigned long)local) ?:
		(peer->srx.transport_type - srx->transport_type) ?:
		(peer->srx.transport_len - srx->transport_len) ?:
		(peer->srx.transport.family - srx->transport.family));
	if (diff != 0)
		return diff;

	switch (srx->transport.family) {
	case AF_INET:
		return ((u16 __force)peer->srx.transport.sin.sin_port -
			(u16 __force)srx->transport.sin.sin_port) ?:
			memcmp(&peer->srx.transport.sin.sin_addr,
			       &srx->transport.sin.sin_addr,
			       sizeof(struct in_addr));
102
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
103 104 105 106 107 108
	case AF_INET6:
		return ((u16 __force)peer->srx.transport.sin6.sin6_port -
			(u16 __force)srx->transport.sin6.sin6_port) ?:
			memcmp(&peer->srx.transport.sin6.sin6_addr,
			       &srx->transport.sin6.sin6_addr,
			       sizeof(struct in6_addr));
109
#endif
110 111 112 113 114 115 116 117 118 119 120 121 122 123
	default:
		BUG();
	}
}

/*
 * Look up a remote transport endpoint for the specified address using RCU.
 */
static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
	struct rxrpc_local *local,
	const struct sockaddr_rxrpc *srx,
	unsigned long hash_key)
{
	struct rxrpc_peer *peer;
124
	struct rxrpc_net *rxnet = local->rxnet;
125

126
	hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) {
127 128
		if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 &&
		    atomic_read(&peer->usage) > 0)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
			return peer;
	}

	return NULL;
}

/*
 * Look up a remote transport endpoint for the specified address using RCU.
 */
struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
					 const struct sockaddr_rxrpc *srx)
{
	struct rxrpc_peer *peer;
	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);

	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
	if (peer) {
D
David Howells 已提交
146
		_net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
147 148 149 150
		_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
	}
	return peer;
}
151

152 153 154 155 156 157
/*
 * assess the MTU size for the network interface through which this peer is
 * reached
 */
static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
{
D
David Howells 已提交
158
	struct dst_entry *dst;
159
	struct rtable *rt;
D
David Howells 已提交
160 161
	struct flowi fl;
	struct flowi4 *fl4 = &fl.u.ip4;
162
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
163
	struct flowi6 *fl6 = &fl.u.ip6;
164
#endif
165 166 167

	peer->if_mtu = 1500;

D
David Howells 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181
	memset(&fl, 0, sizeof(fl));
	switch (peer->srx.transport.family) {
	case AF_INET:
		rt = ip_route_output_ports(
			&init_net, fl4, NULL,
			peer->srx.transport.sin.sin_addr.s_addr, 0,
			htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
		if (IS_ERR(rt)) {
			_leave(" [route err %ld]", PTR_ERR(rt));
			return;
		}
		dst = &rt->dst;
		break;

182
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
183 184 185 186 187 188 189 190 191
	case AF_INET6:
		fl6->flowi6_iif = LOOPBACK_IFINDEX;
		fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
		fl6->flowi6_proto = IPPROTO_UDP;
		memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
		       sizeof(struct in6_addr));
		fl6->fl6_dport = htons(7001);
		fl6->fl6_sport = htons(7000);
		dst = ip6_route_output(&init_net, NULL, fl6);
192 193
		if (dst->error) {
			_leave(" [route err %d]", dst->error);
D
David Howells 已提交
194 195 196
			return;
		}
		break;
197
#endif
D
David Howells 已提交
198 199 200

	default:
		BUG();
201 202
	}

D
David Howells 已提交
203 204
	peer->if_mtu = dst_mtu(dst);
	dst_release(dst);
205

D
David Howells 已提交
206
	_leave(" [if_mtu %u]", peer->if_mtu);
207 208
}

209
/*
210
 * Allocate a peer.
211
 */
212
struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
213 214 215 216 217 218 219
{
	struct rxrpc_peer *peer;

	_enter("");

	peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
	if (peer) {
220 221
		atomic_set(&peer->usage, 1);
		peer->local = local;
222 223 224
		INIT_HLIST_HEAD(&peer->error_targets);
		INIT_WORK(&peer->error_distributor,
			  &rxrpc_peer_error_distributor);
225
		peer->service_conns = RB_ROOT;
226
		seqlock_init(&peer->service_conn_lock);
227 228
		spin_lock_init(&peer->lock);
		peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
229 230 231 232 233 234 235

		if (RXRPC_TX_SMSS > 2190)
			peer->cong_cwnd = 2;
		else if (RXRPC_TX_SMSS > 1095)
			peer->cong_cwnd = 3;
		else
			peer->cong_cwnd = 4;
236 237 238 239 240 241
	}

	_leave(" = %p", peer);
	return peer;
}

242 243 244 245 246
/*
 * Initialise peer record.
 */
static void rxrpc_init_peer(struct rxrpc_peer *peer, unsigned long hash_key)
{
247
	peer->hash_key = hash_key;
248 249
	rxrpc_assess_MTU_size(peer);
	peer->mtu = peer->if_mtu;
250
	peer->rtt_last_req = ktime_get_real();
251

D
David Howells 已提交
252 253
	switch (peer->srx.transport.family) {
	case AF_INET:
254
		peer->hdrsize = sizeof(struct iphdr);
D
David Howells 已提交
255
		break;
256
#ifdef CONFIG_AF_RXRPC_IPV6
D
David Howells 已提交
257 258 259
	case AF_INET6:
		peer->hdrsize = sizeof(struct ipv6hdr);
		break;
260
#endif
D
David Howells 已提交
261 262 263 264 265 266 267 268 269
	default:
		BUG();
	}

	switch (peer->srx.transport_type) {
	case SOCK_DGRAM:
		peer->hdrsize += sizeof(struct udphdr);
		break;
	default:
270 271 272 273 274 275 276
		BUG();
	}

	peer->hdrsize += sizeof(struct rxrpc_wire_header);
	peer->maxdata = peer->mtu - peer->hdrsize;
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290
/*
 * Set up a new peer.
 */
static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
					    struct sockaddr_rxrpc *srx,
					    unsigned long hash_key,
					    gfp_t gfp)
{
	struct rxrpc_peer *peer;

	_enter("");

	peer = rxrpc_alloc_peer(local, gfp);
	if (peer) {
291
		memcpy(&peer->srx, srx, sizeof(*srx));
292 293
		rxrpc_init_peer(peer, hash_key);
	}
294

295 296 297
	_leave(" = %p", peer);
	return peer;
}
298

299
/*
300 301 302
 * Set up a new incoming peer.  There shouldn't be any other matching peers
 * since we've already done a search in the list from the non-reentrant context
 * (the data_ready handler) that is the only place we can add new peers.
303
 */
304
void rxrpc_new_incoming_peer(struct rxrpc_local *local, struct rxrpc_peer *peer)
305
{
306
	struct rxrpc_net *rxnet = local->rxnet;
307 308
	unsigned long hash_key;

309 310 311
	hash_key = rxrpc_peer_hash_key(local, &peer->srx);
	peer->local = local;
	rxrpc_init_peer(peer, hash_key);
312

313
	spin_lock(&rxnet->peer_hash_lock);
314 315
	hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
	list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new);
316
	spin_unlock(&rxnet->peer_hash_lock);
317 318 319 320 321
}

/*
 * obtain a remote transport endpoint for the specified address
 */
322 323
struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
				     struct sockaddr_rxrpc *srx, gfp_t gfp)
324 325
{
	struct rxrpc_peer *peer, *candidate;
326
	struct rxrpc_net *rxnet = local->rxnet;
327
	unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
328

D
David Howells 已提交
329
	_enter("{%pISp}", &srx->transport);
330 331

	/* search the peer list first */
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	rcu_read_lock();
	peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
	if (peer && !rxrpc_get_peer_maybe(peer))
		peer = NULL;
	rcu_read_unlock();

	if (!peer) {
		/* The peer is not yet present in hash - create a candidate
		 * for a new record and then redo the search.
		 */
		candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
		if (!candidate) {
			_leave(" = NULL [nomem]");
			return NULL;
		}
347

348
		spin_lock_bh(&rxnet->peer_hash_lock);
349

350 351 352 353
		/* Need to check that we aren't racing with someone else */
		peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
		if (peer && !rxrpc_get_peer_maybe(peer))
			peer = NULL;
354
		if (!peer) {
355
			hash_add_rcu(rxnet->peer_hash,
356
				     &candidate->hash_link, hash_key);
357 358
			list_add_tail(&candidate->keepalive_link,
				      &rxnet->peer_keepalive_new);
359
		}
360

361
		spin_unlock_bh(&rxnet->peer_hash_lock);
362

363 364 365 366 367
		if (peer)
			kfree(candidate);
		else
			peer = candidate;
	}
368

D
David Howells 已提交
369
	_net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
370

371
	_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
372 373 374 375
	return peer;
}

/*
376
 * Get a ref on a peer record.
377
 */
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer)
{
	const void *here = __builtin_return_address(0);
	int n;

	n = atomic_inc_return(&peer->usage);
	trace_rxrpc_peer(peer, rxrpc_peer_got, n, here);
	return peer;
}

/*
 * Get a ref on a peer record unless its usage has already reached 0.
 */
struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer)
{
	const void *here = __builtin_return_address(0);

	if (peer) {
396
		int n = atomic_fetch_add_unless(&peer->usage, 1, 0);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
		if (n > 0)
			trace_rxrpc_peer(peer, rxrpc_peer_got, n + 1, here);
		else
			peer = NULL;
	}
	return peer;
}

/*
 * Queue a peer record.  This passes the caller's ref to the workqueue.
 */
void __rxrpc_queue_peer_error(struct rxrpc_peer *peer)
{
	const void *here = __builtin_return_address(0);
	int n;

	n = atomic_read(&peer->usage);
	if (rxrpc_queue_work(&peer->error_distributor))
		trace_rxrpc_peer(peer, rxrpc_peer_queued_error, n, here);
	else
		rxrpc_put_peer(peer);
}

/*
 * Discard a peer record.
 */
static void __rxrpc_put_peer(struct rxrpc_peer *peer)
424
{
425 426
	struct rxrpc_net *rxnet = peer->local->rxnet;

427
	ASSERT(hlist_empty(&peer->error_targets));
428

429
	spin_lock_bh(&rxnet->peer_hash_lock);
430
	hash_del_rcu(&peer->hash_link);
431
	list_del_init(&peer->keepalive_link);
432
	spin_unlock_bh(&rxnet->peer_hash_lock);
433

434
	kfree_rcu(peer, rcu);
435
}
436

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
/*
 * Drop a ref on a peer record.
 */
void rxrpc_put_peer(struct rxrpc_peer *peer)
{
	const void *here = __builtin_return_address(0);
	int n;

	if (peer) {
		n = atomic_dec_return(&peer->usage);
		trace_rxrpc_peer(peer, rxrpc_peer_put, n, here);
		if (n == 0)
			__rxrpc_put_peer(peer);
	}
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
/*
 * Make sure all peer records have been discarded.
 */
void rxrpc_destroy_all_peers(struct rxrpc_net *rxnet)
{
	struct rxrpc_peer *peer;
	int i;

	for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) {
		if (hlist_empty(&rxnet->peer_hash[i]))
			continue;

		hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) {
			pr_err("Leaked peer %u {%u} %pISp\n",
			       peer->debug_id,
			       atomic_read(&peer->usage),
			       &peer->srx.transport);
		}
	}
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487
/**
 * rxrpc_kernel_get_peer - Get the peer address of a call
 * @sock: The socket on which the call is in progress.
 * @call: The call to query
 * @_srx: Where to place the result
 *
 * Get the address of the remote peer in a call.
 */
void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
			   struct sockaddr_rxrpc *_srx)
{
	*_srx = call->peer->srx;
}
EXPORT_SYMBOL(rxrpc_kernel_get_peer);
488 489 490 491 492 493 494 495 496 497 498 499 500

/**
 * rxrpc_kernel_get_rtt - Get a call's peer RTT
 * @sock: The socket on which the call is in progress.
 * @call: The call to query
 *
 * Get the call's peer RTT.
 */
u64 rxrpc_kernel_get_rtt(struct socket *sock, struct rxrpc_call *call)
{
	return call->peer->rtt;
}
EXPORT_SYMBOL(rxrpc_kernel_get_rtt);