1 /* peer.c: Rx RPC peer management
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <rxrpc/rxrpc.h>
16 #include <rxrpc/transport.h>
17 #include <rxrpc/peer.h>
18 #include <rxrpc/connection.h>
19 #include <rxrpc/call.h>
20 #include <rxrpc/message.h>
21 #include <linux/udp.h>
24 #include <asm/uaccess.h>
25 #include <asm/div64.h>
28 __RXACCT_DECL(atomic_t rxrpc_peer_count);
29 LIST_HEAD(rxrpc_peers);
30 DECLARE_RWSEM(rxrpc_peers_sem);
31 unsigned long rxrpc_peer_timeout = 12 * 60 * 60;
33 static void __rxrpc_peer_timeout(rxrpc_timer_t *timer)
35 struct rxrpc_peer *peer =
36 list_entry(timer, struct rxrpc_peer, timeout);
38 _debug("Rx PEER TIMEOUT [%p{u=%d}]", peer, atomic_read(&peer->usage));
40 rxrpc_peer_do_timeout(peer);
43 static const struct rxrpc_timer_ops rxrpc_peer_timer_ops = {
44 .timed_out = __rxrpc_peer_timeout,
47 /*****************************************************************************/
49 * create a peer record
51 static int __rxrpc_create_peer(struct rxrpc_transport *trans, __be32 addr,
52 struct rxrpc_peer **_peer)
54 struct rxrpc_peer *peer;
56 _enter("%p,%08x", trans, ntohl(addr));
58 /* allocate and initialise a peer record */
59 peer = kmalloc(sizeof(struct rxrpc_peer), GFP_KERNEL);
65 memset(peer, 0, sizeof(struct rxrpc_peer));
66 atomic_set(&peer->usage, 1);
68 INIT_LIST_HEAD(&peer->link);
69 INIT_LIST_HEAD(&peer->proc_link);
70 INIT_LIST_HEAD(&peer->conn_idlist);
71 INIT_LIST_HEAD(&peer->conn_active);
72 INIT_LIST_HEAD(&peer->conn_graveyard);
73 spin_lock_init(&peer->conn_gylock);
74 init_waitqueue_head(&peer->conn_gy_waitq);
75 rwlock_init(&peer->conn_idlock);
76 rwlock_init(&peer->conn_lock);
77 atomic_set(&peer->conn_count, 0);
78 spin_lock_init(&peer->lock);
79 rxrpc_timer_init(&peer->timeout, &rxrpc_peer_timer_ops);
81 peer->addr.s_addr = addr;
84 peer->ops = trans->peer_ops;
86 __RXACCT(atomic_inc(&rxrpc_peer_count));
88 _leave(" = 0 (%p)", peer);
91 } /* end __rxrpc_create_peer() */
93 /*****************************************************************************/
95 * find a peer record on the specified transport
96 * - returns (if successful) with peer record usage incremented
97 * - resurrects it from the graveyard if found there
99 int rxrpc_peer_lookup(struct rxrpc_transport *trans, __be32 addr,
100 struct rxrpc_peer **_peer)
102 struct rxrpc_peer *peer, *candidate = NULL;
103 struct list_head *_p;
106 _enter("%p{%hu},%08x", trans, trans->port, ntohl(addr));
108 /* [common case] search the transport's active list first */
109 read_lock(&trans->peer_lock);
110 list_for_each(_p, &trans->peer_active) {
111 peer = list_entry(_p, struct rxrpc_peer, link);
112 if (peer->addr.s_addr == addr)
115 read_unlock(&trans->peer_lock);
117 /* [uncommon case] not active - create a candidate for a new record */
118 ret = __rxrpc_create_peer(trans, addr, &candidate);
120 _leave(" = %d", ret);
124 /* search the active list again, just in case it appeared whilst we
126 write_lock(&trans->peer_lock);
127 list_for_each(_p, &trans->peer_active) {
128 peer = list_entry(_p, struct rxrpc_peer, link);
129 if (peer->addr.s_addr == addr)
130 goto found_active_second_chance;
133 /* search the transport's graveyard list */
134 spin_lock(&trans->peer_gylock);
135 list_for_each(_p, &trans->peer_graveyard) {
136 peer = list_entry(_p, struct rxrpc_peer, link);
137 if (peer->addr.s_addr == addr)
138 goto found_in_graveyard;
140 spin_unlock(&trans->peer_gylock);
142 /* we can now add the new candidate to the list
143 * - tell the application layer that this peer has been added
145 rxrpc_get_transport(trans);
149 if (peer->ops && peer->ops->adding) {
150 ret = peer->ops->adding(peer);
152 write_unlock(&trans->peer_lock);
153 __RXACCT(atomic_dec(&rxrpc_peer_count));
155 rxrpc_put_transport(trans);
156 _leave(" = %d", ret);
161 atomic_inc(&trans->peer_count);
164 list_add_tail(&peer->link, &trans->peer_active);
167 write_unlock(&trans->peer_lock);
170 __RXACCT(atomic_dec(&rxrpc_peer_count));
174 if (list_empty(&peer->proc_link)) {
175 down_write(&rxrpc_peers_sem);
176 list_add_tail(&peer->proc_link, &rxrpc_peers);
177 up_write(&rxrpc_peers_sem);
183 _leave(" = 0 (%p{u=%d cc=%d})",
185 atomic_read(&peer->usage),
186 atomic_read(&peer->conn_count));
189 /* handle the peer being found in the active list straight off */
191 rxrpc_get_peer(peer);
192 read_unlock(&trans->peer_lock);
195 /* handle resurrecting a peer from the graveyard */
197 rxrpc_get_peer(peer);
198 rxrpc_get_transport(peer->trans);
199 rxrpc_krxtimod_del_timer(&peer->timeout);
200 list_del_init(&peer->link);
201 spin_unlock(&trans->peer_gylock);
204 /* handle finding the peer on the second time through the active
206 found_active_second_chance:
207 rxrpc_get_peer(peer);
210 } /* end rxrpc_peer_lookup() */
212 /*****************************************************************************/
214 * finish with a peer record
215 * - it gets sent to the graveyard from where it can be resurrected or timed
218 void rxrpc_put_peer(struct rxrpc_peer *peer)
220 struct rxrpc_transport *trans = peer->trans;
222 _enter("%p{cc=%d a=%08x}",
224 atomic_read(&peer->conn_count),
225 ntohl(peer->addr.s_addr));
228 if (atomic_read(&peer->usage) <= 0)
231 write_lock(&trans->peer_lock);
232 spin_lock(&trans->peer_gylock);
233 if (likely(!atomic_dec_and_test(&peer->usage))) {
234 spin_unlock(&trans->peer_gylock);
235 write_unlock(&trans->peer_lock);
240 /* move to graveyard queue */
241 list_del(&peer->link);
242 write_unlock(&trans->peer_lock);
244 list_add_tail(&peer->link, &trans->peer_graveyard);
246 BUG_ON(!list_empty(&peer->conn_active));
248 rxrpc_krxtimod_add_timer(&peer->timeout, rxrpc_peer_timeout * HZ);
250 spin_unlock(&trans->peer_gylock);
252 rxrpc_put_transport(trans);
255 } /* end rxrpc_put_peer() */
257 /*****************************************************************************/
259 * handle a peer timing out in the graveyard
260 * - called from krxtimod
262 void rxrpc_peer_do_timeout(struct rxrpc_peer *peer)
264 struct rxrpc_transport *trans = peer->trans;
266 _enter("%p{u=%d cc=%d a=%08x}",
268 atomic_read(&peer->usage),
269 atomic_read(&peer->conn_count),
270 ntohl(peer->addr.s_addr));
272 BUG_ON(atomic_read(&peer->usage) < 0);
274 /* remove from graveyard if still dead */
275 spin_lock(&trans->peer_gylock);
276 if (atomic_read(&peer->usage) == 0)
277 list_del_init(&peer->link);
280 spin_unlock(&trans->peer_gylock);
284 return; /* resurrected */
287 /* clear all connections on this peer */
288 rxrpc_conn_clearall(peer);
290 BUG_ON(!list_empty(&peer->conn_active));
291 BUG_ON(!list_empty(&peer->conn_graveyard));
293 /* inform the application layer */
294 if (peer->ops && peer->ops->discarding)
295 peer->ops->discarding(peer);
297 if (!list_empty(&peer->proc_link)) {
298 down_write(&rxrpc_peers_sem);
299 list_del(&peer->proc_link);
300 up_write(&rxrpc_peers_sem);
303 __RXACCT(atomic_dec(&rxrpc_peer_count));
306 /* if the graveyard is now empty, wake up anyone waiting for that */
307 if (atomic_dec_and_test(&trans->peer_count))
308 wake_up(&trans->peer_gy_waitq);
310 _leave(" [destroyed]");
311 } /* end rxrpc_peer_do_timeout() */
313 /*****************************************************************************/
315 * clear all peer records from a transport endpoint
317 void rxrpc_peer_clearall(struct rxrpc_transport *trans)
319 DECLARE_WAITQUEUE(myself,current);
321 struct rxrpc_peer *peer;
326 /* there shouldn't be any active peers remaining */
327 BUG_ON(!list_empty(&trans->peer_active));
329 /* manually timeout all peers in the graveyard */
330 spin_lock(&trans->peer_gylock);
331 while (!list_empty(&trans->peer_graveyard)) {
332 peer = list_entry(trans->peer_graveyard.next,
333 struct rxrpc_peer, link);
334 _debug("Clearing peer %p\n", peer);
335 err = rxrpc_krxtimod_del_timer(&peer->timeout);
336 spin_unlock(&trans->peer_gylock);
339 rxrpc_peer_do_timeout(peer);
341 spin_lock(&trans->peer_gylock);
343 spin_unlock(&trans->peer_gylock);
345 /* wait for the the peer graveyard to be completely cleared */
346 set_current_state(TASK_UNINTERRUPTIBLE);
347 add_wait_queue(&trans->peer_gy_waitq, &myself);
349 while (atomic_read(&trans->peer_count) != 0) {
351 set_current_state(TASK_UNINTERRUPTIBLE);
354 remove_wait_queue(&trans->peer_gy_waitq, &myself);
355 set_current_state(TASK_RUNNING);
358 } /* end rxrpc_peer_clearall() */
360 /*****************************************************************************/
362 * calculate and cache the Round-Trip-Time for a message and its response
364 void rxrpc_peer_calculate_rtt(struct rxrpc_peer *peer,
365 struct rxrpc_message *msg,
366 struct rxrpc_message *resp)
368 unsigned long long rtt;
371 _enter("%p,%p,%p", peer, msg, resp);
373 /* calculate the latest RTT */
374 rtt = resp->stamp.tv_sec - msg->stamp.tv_sec;
376 rtt += resp->stamp.tv_usec - msg->stamp.tv_usec;
379 peer->rtt_cache[peer->rtt_point] = rtt;
381 peer->rtt_point %= RXRPC_RTT_CACHE_SIZE;
383 if (peer->rtt_usage < RXRPC_RTT_CACHE_SIZE)
386 /* recalculate RTT */
388 for (loop = peer->rtt_usage - 1; loop >= 0; loop--)
389 rtt += peer->rtt_cache[loop];
391 do_div(rtt, peer->rtt_usage);
394 _leave(" RTT=%lu.%lums",
395 (long) (peer->rtt / 1000), (long) (peer->rtt % 1000));
397 } /* end rxrpc_peer_calculate_rtt() */