2016-04-04 21:00:40 +08:00
|
|
|
/* Service connection management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* 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 Licence
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the Licence, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include "ar-internal.h"
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
/*
|
|
|
|
* Find a service connection under RCU conditions.
|
|
|
|
*
|
|
|
|
* We could use a hash table, but that is subject to bucket stuffing by an
|
|
|
|
* attacker as the client gets to pick the epoch and cid values and would know
|
|
|
|
* the hash function. So, instead, we use a hash table for the peer and from
|
|
|
|
* that an rbtree to find the service connection. Under ordinary circumstances
|
|
|
|
* it might be slower than a large hash table, but it is at least limited in
|
|
|
|
* depth.
|
|
|
|
*/
|
|
|
|
struct rxrpc_connection *rxrpc_find_service_conn_rcu(struct rxrpc_peer *peer,
|
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
struct rxrpc_connection *conn = NULL;
|
|
|
|
struct rxrpc_conn_proto k;
|
|
|
|
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
|
|
|
|
struct rb_node *p;
|
|
|
|
unsigned int seq = 0;
|
|
|
|
|
|
|
|
k.epoch = sp->hdr.epoch;
|
|
|
|
k.cid = sp->hdr.cid & RXRPC_CIDMASK;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* Unfortunately, rbtree walking doesn't give reliable results
|
|
|
|
* under just the RCU read lock, so we have to check for
|
|
|
|
* changes.
|
|
|
|
*/
|
|
|
|
read_seqbegin_or_lock(&peer->service_conn_lock, &seq);
|
|
|
|
|
|
|
|
p = rcu_dereference_raw(peer->service_conns.rb_node);
|
|
|
|
while (p) {
|
|
|
|
conn = rb_entry(p, struct rxrpc_connection, service_node);
|
|
|
|
|
|
|
|
if (conn->proto.index_key < k.index_key)
|
|
|
|
p = rcu_dereference_raw(p->rb_left);
|
|
|
|
else if (conn->proto.index_key > k.index_key)
|
|
|
|
p = rcu_dereference_raw(p->rb_right);
|
|
|
|
else
|
|
|
|
goto done;
|
|
|
|
conn = NULL;
|
|
|
|
}
|
|
|
|
} while (need_seqretry(&peer->service_conn_lock, seq));
|
|
|
|
|
|
|
|
done:
|
|
|
|
done_seqretry(&peer->service_conn_lock, seq);
|
|
|
|
_leave(" = %d", conn ? conn->debug_id : -1);
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a service connection into a peer's tree, thereby making it a target
|
|
|
|
* for incoming packets.
|
|
|
|
*/
|
|
|
|
static struct rxrpc_connection *
|
|
|
|
rxrpc_publish_service_conn(struct rxrpc_peer *peer,
|
|
|
|
struct rxrpc_connection *conn)
|
|
|
|
{
|
|
|
|
struct rxrpc_connection *cursor = NULL;
|
|
|
|
struct rxrpc_conn_proto k = conn->proto;
|
|
|
|
struct rb_node **pp, *parent;
|
|
|
|
|
|
|
|
write_seqlock_bh(&peer->service_conn_lock);
|
|
|
|
|
|
|
|
pp = &peer->service_conns.rb_node;
|
|
|
|
parent = NULL;
|
|
|
|
while (*pp) {
|
|
|
|
parent = *pp;
|
|
|
|
cursor = rb_entry(parent,
|
|
|
|
struct rxrpc_connection, service_node);
|
|
|
|
|
|
|
|
if (cursor->proto.index_key < k.index_key)
|
|
|
|
pp = &(*pp)->rb_left;
|
|
|
|
else if (cursor->proto.index_key > k.index_key)
|
|
|
|
pp = &(*pp)->rb_right;
|
|
|
|
else
|
|
|
|
goto found_extant_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_link_node_rcu(&conn->service_node, parent, pp);
|
|
|
|
rb_insert_color(&conn->service_node, &peer->service_conns);
|
|
|
|
conn_published:
|
|
|
|
set_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags);
|
|
|
|
write_sequnlock_bh(&peer->service_conn_lock);
|
|
|
|
_leave(" = %d [new]", conn->debug_id);
|
|
|
|
return conn;
|
|
|
|
|
|
|
|
found_extant_conn:
|
|
|
|
if (atomic_read(&cursor->usage) == 0)
|
|
|
|
goto replace_old_connection;
|
|
|
|
write_sequnlock_bh(&peer->service_conn_lock);
|
|
|
|
/* We should not be able to get here. rxrpc_incoming_connection() is
|
|
|
|
* called in a non-reentrant context, so there can't be a race to
|
|
|
|
* insert a new connection.
|
|
|
|
*/
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
replace_old_connection:
|
|
|
|
/* The old connection is from an outdated epoch. */
|
|
|
|
_debug("replace conn");
|
|
|
|
rb_replace_node_rcu(&cursor->service_node,
|
|
|
|
&conn->service_node,
|
|
|
|
&peer->service_conns);
|
|
|
|
clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &cursor->flags);
|
|
|
|
goto conn_published;
|
|
|
|
}
|
|
|
|
|
2016-09-08 18:10:12 +08:00
|
|
|
/*
|
|
|
|
* Preallocate a service connection. The connection is placed on the proc and
|
|
|
|
* reap lists so that we don't have to get the lock from BH context.
|
|
|
|
*/
|
|
|
|
struct rxrpc_connection *rxrpc_prealloc_service_connection(gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct rxrpc_connection *conn = rxrpc_alloc_connection(gfp);
|
|
|
|
|
|
|
|
if (conn) {
|
|
|
|
/* We maintain an extra ref on the connection whilst it is on
|
|
|
|
* the rxrpc_connections list.
|
|
|
|
*/
|
|
|
|
conn->state = RXRPC_CONN_SERVICE_PREALLOC;
|
|
|
|
atomic_set(&conn->usage, 2);
|
|
|
|
|
|
|
|
write_lock(&rxrpc_connection_lock);
|
|
|
|
list_add_tail(&conn->link, &rxrpc_connections);
|
|
|
|
list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list);
|
|
|
|
write_unlock(&rxrpc_connection_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:00:40 +08:00
|
|
|
/*
|
|
|
|
* get a record of an incoming connection
|
|
|
|
*/
|
|
|
|
struct rxrpc_connection *rxrpc_incoming_connection(struct rxrpc_local *local,
|
2016-06-29 21:40:39 +08:00
|
|
|
struct sockaddr_rxrpc *srx,
|
2016-04-04 21:00:40 +08:00
|
|
|
struct sk_buff *skb)
|
|
|
|
{
|
2016-07-01 14:51:50 +08:00
|
|
|
struct rxrpc_connection *conn;
|
2016-04-04 21:00:40 +08:00
|
|
|
struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
|
2016-06-29 21:40:39 +08:00
|
|
|
struct rxrpc_peer *peer;
|
2016-04-04 21:00:40 +08:00
|
|
|
const char *new = "old";
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
2016-06-29 21:40:39 +08:00
|
|
|
peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
|
|
|
|
if (!peer) {
|
|
|
|
_debug("no peer");
|
|
|
|
return ERR_PTR(-EBUSY);
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:00:40 +08:00
|
|
|
ASSERT(sp->hdr.flags & RXRPC_CLIENT_INITIATED);
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
peer = rxrpc_lookup_peer_rcu(local, srx);
|
|
|
|
if (peer) {
|
|
|
|
conn = rxrpc_find_service_conn_rcu(peer, skb);
|
|
|
|
if (conn) {
|
|
|
|
if (sp->hdr.securityIndex != conn->security_ix)
|
|
|
|
goto security_mismatch_rcu;
|
|
|
|
if (rxrpc_get_connection_maybe(conn))
|
|
|
|
goto found_extant_connection_rcu;
|
|
|
|
|
|
|
|
/* The conn has expired but we can't remove it without
|
|
|
|
* the appropriate lock, so we attempt to replace it
|
|
|
|
* when we have a new candidate.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rxrpc_get_peer_maybe(peer))
|
|
|
|
peer = NULL;
|
2016-04-04 21:00:40 +08:00
|
|
|
}
|
2016-07-01 14:51:50 +08:00
|
|
|
rcu_read_unlock();
|
2016-04-04 21:00:40 +08:00
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
if (!peer) {
|
|
|
|
peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
|
2016-07-14 22:47:01 +08:00
|
|
|
if (!peer)
|
2016-07-01 14:51:50 +08:00
|
|
|
goto enomem;
|
2016-04-04 21:00:40 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
/* We don't have a matching record yet. */
|
|
|
|
conn = rxrpc_alloc_connection(GFP_NOIO);
|
|
|
|
if (!conn)
|
|
|
|
goto enomem_peer;
|
|
|
|
|
|
|
|
conn->proto.epoch = sp->hdr.epoch;
|
|
|
|
conn->proto.cid = sp->hdr.cid & RXRPC_CIDMASK;
|
|
|
|
conn->params.local = local;
|
|
|
|
conn->params.peer = peer;
|
|
|
|
conn->params.service_id = sp->hdr.serviceId;
|
|
|
|
conn->security_ix = sp->hdr.securityIndex;
|
|
|
|
conn->out_clientflag = 0;
|
|
|
|
conn->state = RXRPC_CONN_SERVICE;
|
|
|
|
if (conn->params.service_id)
|
|
|
|
conn->state = RXRPC_CONN_SERVICE_UNSECURED;
|
2016-04-04 21:00:40 +08:00
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
rxrpc_get_local(local);
|
2016-04-04 21:00:40 +08:00
|
|
|
|
rxrpc: Improve management and caching of client connection objects
Improve the management and caching of client rxrpc connection objects.
From this point, client connections will be managed separately from service
connections because AF_RXRPC controls the creation and re-use of client
connections but doesn't have that luxury with service connections.
Further, there will be limits on the numbers of client connections that may
be live on a machine. No direct restriction will be placed on the number
of client calls, excepting that each client connection can support a
maximum of four concurrent calls.
Note that, for a number of reasons, we don't want to simply discard a
client connection as soon as the last call is apparently finished:
(1) Security is negotiated per-connection and the context is then shared
between all calls on that connection. The context can be negotiated
again if the connection lapses, but that involves holding up calls
whilst at least two packets are exchanged and various crypto bits are
performed - so we'd ideally like to cache it for a little while at
least.
(2) If a packet goes astray, we will need to retransmit a final ACK or
ABORT packet. To make this work, we need to keep around the
connection details for a little while.
(3) The locally held structures represent some amount of setup time, to be
weighed against their occupation of memory when idle.
To this end, the client connection cache is managed by a state machine on
each connection. There are five states:
(1) INACTIVE - The connection is not held in any list and may not have
been exposed to the world. If it has been previously exposed, it was
discarded from the idle list after expiring.
(2) WAITING - The connection is waiting for the number of client conns to
drop below the maximum capacity. Calls may be in progress upon it
from when it was active and got culled.
The connection is on the rxrpc_waiting_client_conns list which is kept
in to-be-granted order. Culled conns with waiters go to the back of
the queue just like new conns.
(3) ACTIVE - The connection has at least one call in progress upon it, it
may freely grant available channels to new calls and calls may be
waiting on it for channels to become available.
The connection is on the rxrpc_active_client_conns list which is kept
in activation order for culling purposes.
(4) CULLED - The connection got summarily culled to try and free up
capacity. Calls currently in progress on the connection are allowed
to continue, but new calls will have to wait. There can be no waiters
in this state - the conn would have to go to the WAITING state
instead.
(5) IDLE - The connection has no calls in progress upon it and must have
been exposed to the world (ie. the EXPOSED flag must be set). When it
expires, the EXPOSED flag is cleared and the connection transitions to
the INACTIVE state.
The connection is on the rxrpc_idle_client_conns list which is kept in
order of how soon they'll expire.
A connection in the ACTIVE or CULLED state must have at least one active
call upon it; if in the WAITING state it may have active calls upon it;
other states may not have active calls.
As long as a connection remains active and doesn't get culled, it may
continue to process calls - even if there are connections on the wait
queue. This simplifies things a bit and reduces the amount of checking we
need do.
There are a couple flags of relevance to the cache:
(1) EXPOSED - The connection ID got exposed to the world. If this flag is
set, an extra ref is added to the connection preventing it from being
reaped when it has no calls outstanding. This flag is cleared and the
ref dropped when a conn is discarded from the idle list.
(2) DONT_REUSE - The connection should be discarded as soon as possible and
should not be reused.
This commit also provides a number of new settings:
(*) /proc/net/rxrpc/max_client_conns
The maximum number of live client connections. Above this number, new
connections get added to the wait list and must wait for an active
conn to be culled. Culled connections can be reused, but they will go
to the back of the wait list and have to wait.
(*) /proc/net/rxrpc/reap_client_conns
If the number of desired connections exceeds the maximum above, the
active connection list will be culled until there are only this many
left in it.
(*) /proc/net/rxrpc/idle_conn_expiry
The normal expiry time for a client connection, provided there are
fewer than reap_client_conns of them around.
(*) /proc/net/rxrpc/idle_conn_fast_expiry
The expedited expiry time, used when there are more than
reap_client_conns of them around.
Note that I combined the Tx wait queue with the channel grant wait queue to
save space as only one of these should be in use at once.
Note also that, for the moment, the service connection cache still uses the
old connection management code.
Signed-off-by: David Howells <dhowells@redhat.com>
2016-08-24 14:30:52 +08:00
|
|
|
/* We maintain an extra ref on the connection whilst it is on
|
|
|
|
* the rxrpc_connections list.
|
|
|
|
*/
|
|
|
|
atomic_set(&conn->usage, 2);
|
|
|
|
|
2016-04-04 21:00:40 +08:00
|
|
|
write_lock(&rxrpc_connection_lock);
|
|
|
|
list_add_tail(&conn->link, &rxrpc_connections);
|
2016-08-24 14:30:52 +08:00
|
|
|
list_add_tail(&conn->proc_link, &rxrpc_connection_proc_list);
|
2016-04-04 21:00:40 +08:00
|
|
|
write_unlock(&rxrpc_connection_lock);
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
/* Make the connection a target for incoming packets. */
|
|
|
|
rxrpc_publish_service_conn(peer, conn);
|
|
|
|
|
2016-04-04 21:00:40 +08:00
|
|
|
new = "new";
|
|
|
|
|
|
|
|
success:
|
|
|
|
_net("CONNECTION %s %d {%x}", new, conn->debug_id, conn->proto.cid);
|
|
|
|
_leave(" = %p {u=%d}", conn, atomic_read(&conn->usage));
|
|
|
|
return conn;
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
found_extant_connection_rcu:
|
|
|
|
rcu_read_unlock();
|
2016-04-04 21:00:40 +08:00
|
|
|
goto success;
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
security_mismatch_rcu:
|
|
|
|
rcu_read_unlock();
|
2016-04-04 21:00:40 +08:00
|
|
|
_leave(" = -EKEYREJECTED");
|
|
|
|
return ERR_PTR(-EKEYREJECTED);
|
2016-07-01 14:51:50 +08:00
|
|
|
|
|
|
|
enomem_peer:
|
|
|
|
rxrpc_put_peer(peer);
|
|
|
|
enomem:
|
|
|
|
_leave(" = -ENOMEM");
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2016-04-04 21:00:40 +08:00
|
|
|
}
|
2016-06-30 17:45:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the service connection from the peer's tree, thereby removing it as a
|
|
|
|
* target for incoming packets.
|
|
|
|
*/
|
|
|
|
void rxrpc_unpublish_service_conn(struct rxrpc_connection *conn)
|
|
|
|
{
|
|
|
|
struct rxrpc_peer *peer = conn->params.peer;
|
|
|
|
|
2016-07-01 14:51:50 +08:00
|
|
|
write_seqlock_bh(&peer->service_conn_lock);
|
2016-06-30 17:45:22 +08:00
|
|
|
if (test_and_clear_bit(RXRPC_CONN_IN_SERVICE_CONNS, &conn->flags))
|
|
|
|
rb_erase(&conn->service_node, &peer->service_conns);
|
2016-07-01 14:51:50 +08:00
|
|
|
write_sequnlock_bh(&peer->service_conn_lock);
|
2016-06-30 17:45:22 +08:00
|
|
|
}
|