2016-04-04 07:00:32 -06:00
|
|
|
/* RxRPC remote transport endpoint record management
|
2007-04-26 16:48:28 -06:00
|
|
|
*
|
2016-04-04 07:00:32 -06:00
|
|
|
* Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
|
2007-04-26 16:48:28 -06:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-06-02 13:08:52 -06:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2007-04-26 16:48:28 -06:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <linux/in.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2016-04-04 07:00:32 -06:00
|
|
|
#include <linux/hashtable.h>
|
2007-04-26 16:48:28 -06:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/af_rxrpc.h>
|
|
|
|
#include <net/ip.h>
|
2007-05-04 13:41:11 -06:00
|
|
|
#include <net/route.h>
|
2007-04-26 16:48:28 -06:00
|
|
|
#include "ar-internal.h"
|
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
static DEFINE_HASHTABLE(rxrpc_peer_hash, 10);
|
|
|
|
static DEFINE_SPINLOCK(rxrpc_peer_hash_lock);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
/*
|
|
|
|
* 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;
|
2016-06-17 03:55:22 -06:00
|
|
|
default:
|
|
|
|
WARN(1, "AF_RXRPC: Unsupported transport address family\n");
|
|
|
|
return 0;
|
2016-04-04 07:00:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
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;
|
|
|
|
|
|
|
|
hash_for_each_possible_rcu(rxrpc_peer_hash, peer, hash_link, hash_key) {
|
|
|
|
if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0) {
|
|
|
|
if (atomic_read(&peer->usage) == 0)
|
|
|
|
return NULL;
|
|
|
|
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) {
|
|
|
|
switch (srx->transport.family) {
|
|
|
|
case AF_INET:
|
|
|
|
_net("PEER %d {%d,%u,%pI4+%hu}",
|
|
|
|
peer->debug_id,
|
|
|
|
peer->srx.transport_type,
|
|
|
|
peer->srx.transport.family,
|
|
|
|
&peer->srx.transport.sin.sin_addr,
|
|
|
|
ntohs(peer->srx.transport.sin.sin_port));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
|
|
|
|
}
|
|
|
|
return peer;
|
|
|
|
}
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2007-05-04 13:41:11 -06:00
|
|
|
/*
|
|
|
|
* assess the MTU size for the network interface through which this peer is
|
|
|
|
* reached
|
|
|
|
*/
|
|
|
|
static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
|
|
|
|
{
|
|
|
|
struct rtable *rt;
|
2011-05-03 21:25:42 -06:00
|
|
|
struct flowi4 fl4;
|
2007-05-04 13:41:11 -06:00
|
|
|
|
|
|
|
peer->if_mtu = 1500;
|
|
|
|
|
2011-05-03 21:25:42 -06:00
|
|
|
rt = ip_route_output_ports(&init_net, &fl4, NULL,
|
2011-03-11 22:00:52 -07:00
|
|
|
peer->srx.transport.sin.sin_addr.s_addr, 0,
|
|
|
|
htons(7000), htons(7001),
|
|
|
|
IPPROTO_UDP, 0, 0);
|
2011-03-02 15:31:35 -07:00
|
|
|
if (IS_ERR(rt)) {
|
|
|
|
_leave(" [route err %ld]", PTR_ERR(rt));
|
2007-05-04 13:41:11 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-11 00:31:35 -06:00
|
|
|
peer->if_mtu = dst_mtu(&rt->dst);
|
|
|
|
dst_release(&rt->dst);
|
2007-05-04 13:41:11 -06:00
|
|
|
|
2007-05-10 04:15:25 -06:00
|
|
|
_leave(" [if_mtu %u]", peer->if_mtu);
|
2007-05-04 13:41:11 -06:00
|
|
|
}
|
|
|
|
|
2007-04-26 16:48:28 -06:00
|
|
|
/*
|
2016-04-04 07:00:32 -06:00
|
|
|
* Allocate a peer.
|
2007-04-26 16:48:28 -06:00
|
|
|
*/
|
2016-04-04 07:00:32 -06:00
|
|
|
struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
|
2007-04-26 16:48:28 -06:00
|
|
|
{
|
|
|
|
struct rxrpc_peer *peer;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
|
|
|
peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
|
|
|
|
if (peer) {
|
2016-04-04 07:00:32 -06:00
|
|
|
atomic_set(&peer->usage, 1);
|
|
|
|
peer->local = local;
|
2016-04-04 07:00:34 -06:00
|
|
|
INIT_HLIST_HEAD(&peer->error_targets);
|
|
|
|
INIT_WORK(&peer->error_distributor,
|
|
|
|
&rxrpc_peer_error_distributor);
|
2007-04-26 16:48:28 -06:00
|
|
|
spin_lock_init(&peer->lock);
|
|
|
|
peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
|
2016-04-04 07:00:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
_leave(" = %p", peer);
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
peer->hash_key = hash_key;
|
2007-04-26 16:48:28 -06:00
|
|
|
memcpy(&peer->srx, srx, sizeof(*srx));
|
|
|
|
|
2007-05-04 13:41:11 -06:00
|
|
|
rxrpc_assess_MTU_size(peer);
|
|
|
|
peer->mtu = peer->if_mtu;
|
2007-04-26 16:48:28 -06:00
|
|
|
|
|
|
|
if (srx->transport.family == AF_INET) {
|
|
|
|
peer->hdrsize = sizeof(struct iphdr);
|
|
|
|
switch (srx->transport_type) {
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
peer->hdrsize += sizeof(struct udphdr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2016-03-04 08:53:46 -07:00
|
|
|
peer->hdrsize += sizeof(struct rxrpc_wire_header);
|
2007-04-26 16:48:28 -06:00
|
|
|
peer->maxdata = peer->mtu - peer->hdrsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
_leave(" = %p", peer);
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* obtain a remote transport endpoint for the specified address
|
|
|
|
*/
|
2016-04-04 07:00:32 -06:00
|
|
|
struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
|
|
|
|
struct sockaddr_rxrpc *srx, gfp_t gfp)
|
2007-04-26 16:48:28 -06:00
|
|
|
{
|
|
|
|
struct rxrpc_peer *peer, *candidate;
|
2016-04-04 07:00:32 -06:00
|
|
|
unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2008-10-31 01:54:56 -06:00
|
|
|
_enter("{%d,%d,%pI4+%hu}",
|
2007-04-26 16:48:28 -06:00
|
|
|
srx->transport_type,
|
|
|
|
srx->transport_len,
|
2008-10-31 01:54:56 -06:00
|
|
|
&srx->transport.sin.sin_addr,
|
2007-04-26 16:48:28 -06:00
|
|
|
ntohs(srx->transport.sin.sin_port));
|
|
|
|
|
|
|
|
/* search the peer list first */
|
2016-04-04 07:00:32 -06:00
|
|
|
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;
|
|
|
|
}
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
spin_lock(&rxrpc_peer_hash_lock);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
/* 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;
|
|
|
|
if (!peer)
|
|
|
|
hash_add_rcu(rxrpc_peer_hash,
|
|
|
|
&candidate->hash_link, hash_key);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
spin_unlock(&rxrpc_peer_hash_lock);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
if (peer)
|
|
|
|
kfree(candidate);
|
|
|
|
else
|
|
|
|
peer = candidate;
|
|
|
|
}
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
_net("PEER %d {%d,%pI4+%hu}",
|
2007-04-26 16:48:28 -06:00
|
|
|
peer->debug_id,
|
|
|
|
peer->srx.transport_type,
|
2008-10-31 01:54:56 -06:00
|
|
|
&peer->srx.transport.sin.sin_addr,
|
2007-04-26 16:48:28 -06:00
|
|
|
ntohs(peer->srx.transport.sin.sin_port));
|
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
_leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
|
2007-04-26 16:48:28 -06:00
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-04-04 07:00:32 -06:00
|
|
|
* Discard a ref on a remote peer record.
|
2007-04-26 16:48:28 -06:00
|
|
|
*/
|
2016-04-04 07:00:32 -06:00
|
|
|
void __rxrpc_put_peer(struct rxrpc_peer *peer)
|
2007-04-26 16:48:28 -06:00
|
|
|
{
|
2016-04-04 07:00:34 -06:00
|
|
|
ASSERT(hlist_empty(&peer->error_targets));
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
spin_lock(&rxrpc_peer_hash_lock);
|
|
|
|
hash_del_rcu(&peer->hash_link);
|
|
|
|
spin_unlock(&rxrpc_peer_hash_lock);
|
2007-04-26 16:48:28 -06:00
|
|
|
|
2016-04-04 07:00:32 -06:00
|
|
|
kfree_rcu(peer, rcu);
|
2007-04-26 16:48:28 -06:00
|
|
|
}
|