vserver 1.9.3
[linux-2.6.git] / net / rxrpc / peer.c
1 /* peer.c: Rx RPC peer management
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11
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>
22 #include <linux/ip.h>
23 #include <net/sock.h>
24 #include <asm/uaccess.h>
25 #include <asm/div64.h>
26 #include "internal.h"
27
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;
32
33 static void __rxrpc_peer_timeout(rxrpc_timer_t *timer)
34 {
35         struct rxrpc_peer *peer =
36                 list_entry(timer, struct rxrpc_peer, timeout);
37
38         _debug("Rx PEER TIMEOUT [%p{u=%d}]", peer, atomic_read(&peer->usage));
39
40         rxrpc_peer_do_timeout(peer);
41 }
42
43 static const struct rxrpc_timer_ops rxrpc_peer_timer_ops = {
44         .timed_out      = __rxrpc_peer_timeout,
45 };
46
47 /*****************************************************************************/
48 /*
49  * create a peer record
50  */
51 static int __rxrpc_create_peer(struct rxrpc_transport *trans, __be32 addr,
52                                struct rxrpc_peer **_peer)
53 {
54         struct rxrpc_peer *peer;
55
56         _enter("%p,%08x", trans, ntohl(addr));
57
58         /* allocate and initialise a peer record */
59         peer = kmalloc(sizeof(struct rxrpc_peer), GFP_KERNEL);
60         if (!peer) {
61                 _leave(" = -ENOMEM");
62                 return -ENOMEM;
63         }
64
65         memset(peer, 0, sizeof(struct rxrpc_peer));
66         atomic_set(&peer->usage, 1);
67
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);
80
81         peer->addr.s_addr = addr;
82
83         peer->trans = trans;
84         peer->ops = trans->peer_ops;
85
86         __RXACCT(atomic_inc(&rxrpc_peer_count));
87         *_peer = peer;
88         _leave(" = 0 (%p)", peer);
89
90         return 0;
91 } /* end __rxrpc_create_peer() */
92
93 /*****************************************************************************/
94 /*
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
98  */
99 int rxrpc_peer_lookup(struct rxrpc_transport *trans, __be32 addr,
100                       struct rxrpc_peer **_peer)
101 {
102         struct rxrpc_peer *peer, *candidate = NULL;
103         struct list_head *_p;
104         int ret;
105
106         _enter("%p{%hu},%08x", trans, trans->port, ntohl(addr));
107
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)
113                         goto found_active;
114         }
115         read_unlock(&trans->peer_lock);
116
117         /* [uncommon case] not active - create a candidate for a new record */
118         ret = __rxrpc_create_peer(trans, addr, &candidate);
119         if (ret < 0) {
120                 _leave(" = %d", ret);
121                 return ret;
122         }
123
124         /* search the active list again, just in case it appeared whilst we
125          * were busy */
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;
131         }
132
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;
139         }
140         spin_unlock(&trans->peer_gylock);
141
142         /* we can now add the new candidate to the list
143          * - tell the application layer that this peer has been added
144          */
145         rxrpc_get_transport(trans);
146         peer = candidate;
147         candidate = NULL;
148
149         if (peer->ops && peer->ops->adding) {
150                 ret = peer->ops->adding(peer);
151                 if (ret < 0) {
152                         write_unlock(&trans->peer_lock);
153                         __RXACCT(atomic_dec(&rxrpc_peer_count));
154                         kfree(peer);
155                         rxrpc_put_transport(trans);
156                         _leave(" = %d", ret);
157                         return ret;
158                 }
159         }
160
161         atomic_inc(&trans->peer_count);
162
163  make_active:
164         list_add_tail(&peer->link, &trans->peer_active);
165
166  success_uwfree:
167         write_unlock(&trans->peer_lock);
168
169         if (candidate) {
170                 __RXACCT(atomic_dec(&rxrpc_peer_count));
171                 kfree(candidate);
172         }
173
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);
178         }
179
180  success:
181         *_peer = peer;
182
183         _leave(" = 0 (%p{u=%d cc=%d})",
184                peer,
185                atomic_read(&peer->usage),
186                atomic_read(&peer->conn_count));
187         return 0;
188
189         /* handle the peer being found in the active list straight off */
190  found_active:
191         rxrpc_get_peer(peer);
192         read_unlock(&trans->peer_lock);
193         goto success;
194
195         /* handle resurrecting a peer from the graveyard */
196  found_in_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);
202         goto make_active;
203
204         /* handle finding the peer on the second time through the active
205          * list */
206  found_active_second_chance:
207         rxrpc_get_peer(peer);
208         goto success_uwfree;
209
210 } /* end rxrpc_peer_lookup() */
211
212 /*****************************************************************************/
213 /*
214  * finish with a peer record
215  * - it gets sent to the graveyard from where it can be resurrected or timed
216  *   out
217  */
218 void rxrpc_put_peer(struct rxrpc_peer *peer)
219 {
220         struct rxrpc_transport *trans = peer->trans;
221
222         _enter("%p{cc=%d a=%08x}",
223                peer,
224                atomic_read(&peer->conn_count),
225                ntohl(peer->addr.s_addr));
226
227         /* sanity check */
228         if (atomic_read(&peer->usage) <= 0)
229                 BUG();
230
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);
236                 _leave("");
237                 return;
238         }
239
240         /* move to graveyard queue */
241         list_del(&peer->link);
242         write_unlock(&trans->peer_lock);
243
244         list_add_tail(&peer->link, &trans->peer_graveyard);
245
246         BUG_ON(!list_empty(&peer->conn_active));
247
248         rxrpc_krxtimod_add_timer(&peer->timeout, rxrpc_peer_timeout * HZ);
249
250         spin_unlock(&trans->peer_gylock);
251
252         rxrpc_put_transport(trans);
253
254         _leave(" [killed]");
255 } /* end rxrpc_put_peer() */
256
257 /*****************************************************************************/
258 /*
259  * handle a peer timing out in the graveyard
260  * - called from krxtimod
261  */
262 void rxrpc_peer_do_timeout(struct rxrpc_peer *peer)
263 {
264         struct rxrpc_transport *trans = peer->trans;
265
266         _enter("%p{u=%d cc=%d a=%08x}",
267                peer,
268                atomic_read(&peer->usage),
269                atomic_read(&peer->conn_count),
270                ntohl(peer->addr.s_addr));
271
272         BUG_ON(atomic_read(&peer->usage) < 0);
273
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);
278         else
279                 peer = NULL;
280         spin_unlock(&trans->peer_gylock);
281
282         if (!peer) {
283                 _leave("");
284                 return; /* resurrected */
285         }
286
287         /* clear all connections on this peer */
288         rxrpc_conn_clearall(peer);
289
290         BUG_ON(!list_empty(&peer->conn_active));
291         BUG_ON(!list_empty(&peer->conn_graveyard));
292
293         /* inform the application layer */
294         if (peer->ops && peer->ops->discarding)
295                 peer->ops->discarding(peer);
296
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);
301         }
302
303         __RXACCT(atomic_dec(&rxrpc_peer_count));
304         kfree(peer);
305
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);
309
310         _leave(" [destroyed]");
311 } /* end rxrpc_peer_do_timeout() */
312
313 /*****************************************************************************/
314 /*
315  * clear all peer records from a transport endpoint
316  */
317 void rxrpc_peer_clearall(struct rxrpc_transport *trans)
318 {
319         DECLARE_WAITQUEUE(myself,current);
320
321         struct rxrpc_peer *peer;
322         int err;
323
324         _enter("%p",trans);
325
326         /* there shouldn't be any active peers remaining */
327         BUG_ON(!list_empty(&trans->peer_active));
328
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);
337
338                 if (err == 0)
339                         rxrpc_peer_do_timeout(peer);
340
341                 spin_lock(&trans->peer_gylock);
342         }
343         spin_unlock(&trans->peer_gylock);
344
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);
348
349         while (atomic_read(&trans->peer_count) != 0) {
350                 schedule();
351                 set_current_state(TASK_UNINTERRUPTIBLE);
352         }
353
354         remove_wait_queue(&trans->peer_gy_waitq, &myself);
355         set_current_state(TASK_RUNNING);
356
357         _leave("");
358 } /* end rxrpc_peer_clearall() */
359
360 /*****************************************************************************/
361 /*
362  * calculate and cache the Round-Trip-Time for a message and its response
363  */
364 void rxrpc_peer_calculate_rtt(struct rxrpc_peer *peer,
365                               struct rxrpc_message *msg,
366                               struct rxrpc_message *resp)
367 {
368         unsigned long long rtt;
369         int loop;
370
371         _enter("%p,%p,%p", peer, msg, resp);
372
373         /* calculate the latest RTT */
374         rtt = resp->stamp.tv_sec - msg->stamp.tv_sec;
375         rtt *= 1000000UL;
376         rtt += resp->stamp.tv_usec - msg->stamp.tv_usec;
377
378         /* add to cache */
379         peer->rtt_cache[peer->rtt_point] = rtt;
380         peer->rtt_point++;
381         peer->rtt_point %= RXRPC_RTT_CACHE_SIZE;
382
383         if (peer->rtt_usage < RXRPC_RTT_CACHE_SIZE)
384                 peer->rtt_usage++;
385
386         /* recalculate RTT */
387         rtt = 0;
388         for (loop = peer->rtt_usage - 1; loop >= 0; loop--)
389                 rtt += peer->rtt_cache[loop];
390
391         do_div(rtt, peer->rtt_usage);
392         peer->rtt = rtt;
393
394         _leave(" RTT=%lu.%lums",
395                (long) (peer->rtt / 1000), (long) (peer->rtt % 1000));
396
397 } /* end rxrpc_peer_calculate_rtt() */