1 /* RxRPC virtual connection handler
3 * Copyright (C) 2007 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/module.h>
13 #include <linux/slab.h>
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/crypto.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
21 static void rxrpc_connection_reaper(struct work_struct
*work
);
23 LIST_HEAD(rxrpc_connections
);
24 DEFINE_RWLOCK(rxrpc_connection_lock
);
25 static unsigned long rxrpc_connection_timeout
= 10 * 60;
26 static DECLARE_DELAYED_WORK(rxrpc_connection_reap
, rxrpc_connection_reaper
);
29 * allocate a new client connection bundle
31 static struct rxrpc_conn_bundle
*rxrpc_alloc_bundle(gfp_t gfp
)
33 struct rxrpc_conn_bundle
*bundle
;
37 bundle
= kzalloc(sizeof(struct rxrpc_conn_bundle
), gfp
);
39 INIT_LIST_HEAD(&bundle
->unused_conns
);
40 INIT_LIST_HEAD(&bundle
->avail_conns
);
41 INIT_LIST_HEAD(&bundle
->busy_conns
);
42 init_waitqueue_head(&bundle
->chanwait
);
43 atomic_set(&bundle
->usage
, 1);
46 _leave(" = %p", bundle
);
51 * compare bundle parameters with what we're looking for
52 * - return -ve, 0 or +ve
55 int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle
*bundle
,
56 struct key
*key
, __be16 service_id
)
58 return (bundle
->service_id
- service_id
) ?:
59 ((unsigned long) bundle
->key
- (unsigned long) key
);
63 * get bundle of client connections that a client socket can make use of
65 struct rxrpc_conn_bundle
*rxrpc_get_bundle(struct rxrpc_sock
*rx
,
66 struct rxrpc_transport
*trans
,
71 struct rxrpc_conn_bundle
*bundle
, *candidate
;
72 struct rb_node
*p
, *parent
, **pp
;
74 _enter("%p{%x},%x,%hx,",
75 rx
, key_serial(key
), trans
->debug_id
, ntohs(service_id
));
77 if (rx
->trans
== trans
&& rx
->bundle
) {
78 atomic_inc(&rx
->bundle
->usage
);
82 /* search the extant bundles first for one that matches the specified
84 spin_lock(&trans
->client_lock
);
86 p
= trans
->bundles
.rb_node
;
88 bundle
= rb_entry(p
, struct rxrpc_conn_bundle
, node
);
90 if (rxrpc_cmp_bundle(bundle
, key
, service_id
) < 0)
92 else if (rxrpc_cmp_bundle(bundle
, key
, service_id
) > 0)
95 goto found_extant_bundle
;
98 spin_unlock(&trans
->client_lock
);
100 /* not yet present - create a candidate for a new record and then
102 candidate
= rxrpc_alloc_bundle(gfp
);
104 _leave(" = -ENOMEM");
105 return ERR_PTR(-ENOMEM
);
108 candidate
->key
= key_get(key
);
109 candidate
->service_id
= service_id
;
111 spin_lock(&trans
->client_lock
);
113 pp
= &trans
->bundles
.rb_node
;
117 bundle
= rb_entry(parent
, struct rxrpc_conn_bundle
, node
);
119 if (rxrpc_cmp_bundle(bundle
, key
, service_id
) < 0)
120 pp
= &(*pp
)->rb_left
;
121 else if (rxrpc_cmp_bundle(bundle
, key
, service_id
) > 0)
122 pp
= &(*pp
)->rb_right
;
124 goto found_extant_second
;
127 /* second search also failed; add the new bundle */
131 rb_link_node(&bundle
->node
, parent
, pp
);
132 rb_insert_color(&bundle
->node
, &trans
->bundles
);
133 spin_unlock(&trans
->client_lock
);
134 _net("BUNDLE new on trans %d", trans
->debug_id
);
135 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
136 atomic_inc(&bundle
->usage
);
139 _leave(" = %p [new]", bundle
);
142 /* we found the bundle in the list immediately */
144 atomic_inc(&bundle
->usage
);
145 spin_unlock(&trans
->client_lock
);
146 _net("BUNDLE old on trans %d", trans
->debug_id
);
147 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
148 atomic_inc(&bundle
->usage
);
151 _leave(" = %p [extant %d]", bundle
, atomic_read(&bundle
->usage
));
154 /* we found the bundle on the second time through the list */
156 atomic_inc(&bundle
->usage
);
157 spin_unlock(&trans
->client_lock
);
159 _net("BUNDLE old2 on trans %d", trans
->debug_id
);
160 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
161 atomic_inc(&bundle
->usage
);
164 _leave(" = %p [second %d]", bundle
, atomic_read(&bundle
->usage
));
171 void rxrpc_put_bundle(struct rxrpc_transport
*trans
,
172 struct rxrpc_conn_bundle
*bundle
)
174 _enter("%p,%p{%d}",trans
, bundle
, atomic_read(&bundle
->usage
));
176 if (atomic_dec_and_lock(&bundle
->usage
, &trans
->client_lock
)) {
177 _debug("Destroy bundle");
178 rb_erase(&bundle
->node
, &trans
->bundles
);
179 spin_unlock(&trans
->client_lock
);
180 ASSERT(list_empty(&bundle
->unused_conns
));
181 ASSERT(list_empty(&bundle
->avail_conns
));
182 ASSERT(list_empty(&bundle
->busy_conns
));
183 ASSERTCMP(bundle
->num_conns
, ==, 0);
184 key_put(bundle
->key
);
192 * allocate a new connection
194 static struct rxrpc_connection
*rxrpc_alloc_connection(gfp_t gfp
)
196 struct rxrpc_connection
*conn
;
200 conn
= kzalloc(sizeof(struct rxrpc_connection
), gfp
);
202 INIT_WORK(&conn
->processor
, &rxrpc_process_connection
);
203 INIT_LIST_HEAD(&conn
->bundle_link
);
204 conn
->calls
= RB_ROOT
;
205 skb_queue_head_init(&conn
->rx_queue
);
206 rwlock_init(&conn
->lock
);
207 spin_lock_init(&conn
->state_lock
);
208 atomic_set(&conn
->usage
, 1);
209 conn
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
210 conn
->avail_calls
= RXRPC_MAXCALLS
;
211 conn
->size_align
= 4;
212 conn
->header_size
= sizeof(struct rxrpc_header
);
215 _leave(" = %p{%d}", conn
, conn
? conn
->debug_id
: 0);
220 * assign a connection ID to a connection and add it to the transport's
221 * connection lookup tree
222 * - called with transport client lock held
224 static void rxrpc_assign_connection_id(struct rxrpc_connection
*conn
)
226 struct rxrpc_connection
*xconn
;
227 struct rb_node
*parent
, **p
;
235 write_lock_bh(&conn
->trans
->conn_lock
);
237 conn
->trans
->conn_idcounter
+= RXRPC_CID_INC
;
238 if (conn
->trans
->conn_idcounter
< RXRPC_CID_INC
)
239 conn
->trans
->conn_idcounter
= RXRPC_CID_INC
;
240 real_conn_id
= conn
->trans
->conn_idcounter
;
244 p
= &conn
->trans
->client_conns
.rb_node
;
248 xconn
= rb_entry(parent
, struct rxrpc_connection
, node
);
250 if (epoch
< xconn
->epoch
)
252 else if (epoch
> xconn
->epoch
)
254 else if (real_conn_id
< xconn
->real_conn_id
)
256 else if (real_conn_id
> xconn
->real_conn_id
)
262 /* we've found a suitable hole - arrange for this connection to occupy
264 rb_link_node(&conn
->node
, parent
, p
);
265 rb_insert_color(&conn
->node
, &conn
->trans
->client_conns
);
267 conn
->real_conn_id
= real_conn_id
;
268 conn
->cid
= htonl(real_conn_id
);
269 write_unlock_bh(&conn
->trans
->conn_lock
);
270 _leave(" [CONNID %x CID %x]", real_conn_id
, ntohl(conn
->cid
));
273 /* we found a connection with the proposed ID - walk the tree from that
274 * point looking for the next unused ID */
277 real_conn_id
+= RXRPC_CID_INC
;
278 if (real_conn_id
< RXRPC_CID_INC
) {
279 real_conn_id
= RXRPC_CID_INC
;
280 conn
->trans
->conn_idcounter
= real_conn_id
;
281 goto attempt_insertion
;
284 parent
= rb_next(parent
);
286 goto attempt_insertion
;
288 xconn
= rb_entry(parent
, struct rxrpc_connection
, node
);
289 if (epoch
< xconn
->epoch
||
290 real_conn_id
< xconn
->real_conn_id
)
291 goto attempt_insertion
;
296 * add a call to a connection's call-by-ID tree
298 static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection
*conn
,
299 struct rxrpc_call
*call
)
301 struct rxrpc_call
*xcall
;
302 struct rb_node
*parent
, **p
;
305 write_lock_bh(&conn
->lock
);
307 call_id
= call
->call_id
;
308 p
= &conn
->calls
.rb_node
;
312 xcall
= rb_entry(parent
, struct rxrpc_call
, conn_node
);
314 if (call_id
< xcall
->call_id
)
316 else if (call_id
> xcall
->call_id
)
322 rb_link_node(&call
->conn_node
, parent
, p
);
323 rb_insert_color(&call
->conn_node
, &conn
->calls
);
325 write_unlock_bh(&conn
->lock
);
329 * connect a call on an exclusive connection
331 static int rxrpc_connect_exclusive(struct rxrpc_sock
*rx
,
332 struct rxrpc_transport
*trans
,
334 struct rxrpc_call
*call
,
337 struct rxrpc_connection
*conn
;
344 /* not yet present - create a candidate for a new connection
345 * and then redo the check */
346 conn
= rxrpc_alloc_connection(gfp
);
348 _leave(" = -ENOMEM");
354 conn
->service_id
= service_id
;
355 conn
->epoch
= rxrpc_epoch
;
356 conn
->in_clientflag
= 0;
357 conn
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
359 conn
->state
= RXRPC_CONN_CLIENT
;
360 conn
->avail_calls
= RXRPC_MAXCALLS
- 1;
361 conn
->security_level
= rx
->min_sec_level
;
362 conn
->key
= key_get(rx
->key
);
364 ret
= rxrpc_init_client_conn_security(conn
);
368 _leave(" = %d [key]", ret
);
372 write_lock_bh(&rxrpc_connection_lock
);
373 list_add_tail(&conn
->link
, &rxrpc_connections
);
374 write_unlock_bh(&rxrpc_connection_lock
);
376 spin_lock(&trans
->client_lock
);
377 atomic_inc(&trans
->usage
);
379 _net("CONNECT EXCL new %d on TRANS %d",
380 conn
->debug_id
, conn
->trans
->debug_id
);
382 rxrpc_assign_connection_id(conn
);
385 spin_lock(&trans
->client_lock
);
388 /* we've got a connection with a free channel and we can now attach the
390 * - we're holding the transport's client lock
391 * - we're holding a reference on the connection
393 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
394 if (!conn
->channels
[chan
])
396 goto no_free_channels
;
399 atomic_inc(&conn
->usage
);
400 conn
->channels
[chan
] = call
;
402 call
->channel
= chan
;
403 call
->cid
= conn
->cid
| htonl(chan
);
404 call
->call_id
= htonl(++conn
->call_counter
);
406 _net("CONNECT client on conn %d chan %d as call %x",
407 conn
->debug_id
, chan
, ntohl(call
->call_id
));
409 spin_unlock(&trans
->client_lock
);
411 rxrpc_add_call_ID_to_conn(conn
, call
);
416 spin_unlock(&trans
->client_lock
);
422 * find a connection for a call
423 * - called in process context with IRQs enabled
425 int rxrpc_connect_call(struct rxrpc_sock
*rx
,
426 struct rxrpc_transport
*trans
,
427 struct rxrpc_conn_bundle
*bundle
,
428 struct rxrpc_call
*call
,
431 struct rxrpc_connection
*conn
, *candidate
;
434 DECLARE_WAITQUEUE(myself
, current
);
436 _enter("%p,%lx,", rx
, call
->user_call_ID
);
438 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN
, &rx
->flags
))
439 return rxrpc_connect_exclusive(rx
, trans
, bundle
->service_id
,
442 spin_lock(&trans
->client_lock
);
444 /* see if the bundle has a call slot available */
445 if (!list_empty(&bundle
->avail_conns
)) {
447 conn
= list_entry(bundle
->avail_conns
.next
,
448 struct rxrpc_connection
,
450 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
451 list_del_init(&conn
->bundle_link
);
455 if (--conn
->avail_calls
== 0)
456 list_move(&conn
->bundle_link
,
457 &bundle
->busy_conns
);
458 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
459 ASSERT(conn
->channels
[0] == NULL
||
460 conn
->channels
[1] == NULL
||
461 conn
->channels
[2] == NULL
||
462 conn
->channels
[3] == NULL
);
463 atomic_inc(&conn
->usage
);
467 if (!list_empty(&bundle
->unused_conns
)) {
469 conn
= list_entry(bundle
->unused_conns
.next
,
470 struct rxrpc_connection
,
472 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
473 list_del_init(&conn
->bundle_link
);
477 ASSERTCMP(conn
->avail_calls
, ==, RXRPC_MAXCALLS
);
478 conn
->avail_calls
= RXRPC_MAXCALLS
- 1;
479 ASSERT(conn
->channels
[0] == NULL
&&
480 conn
->channels
[1] == NULL
&&
481 conn
->channels
[2] == NULL
&&
482 conn
->channels
[3] == NULL
);
483 atomic_inc(&conn
->usage
);
484 list_move(&conn
->bundle_link
, &bundle
->avail_conns
);
488 /* need to allocate a new connection */
489 _debug("get new conn [%d]", bundle
->num_conns
);
491 spin_unlock(&trans
->client_lock
);
493 if (signal_pending(current
))
496 if (bundle
->num_conns
>= 20) {
497 _debug("too many conns");
499 if (!(gfp
& __GFP_WAIT
)) {
500 _leave(" = -EAGAIN");
504 add_wait_queue(&bundle
->chanwait
, &myself
);
506 set_current_state(TASK_INTERRUPTIBLE
);
507 if (bundle
->num_conns
< 20 ||
508 !list_empty(&bundle
->unused_conns
) ||
509 !list_empty(&bundle
->avail_conns
))
511 if (signal_pending(current
))
512 goto interrupted_dequeue
;
515 remove_wait_queue(&bundle
->chanwait
, &myself
);
516 __set_current_state(TASK_RUNNING
);
517 spin_lock(&trans
->client_lock
);
521 /* not yet present - create a candidate for a new connection and then
523 candidate
= rxrpc_alloc_connection(gfp
);
525 _leave(" = -ENOMEM");
529 candidate
->trans
= trans
;
530 candidate
->bundle
= bundle
;
531 candidate
->service_id
= bundle
->service_id
;
532 candidate
->epoch
= rxrpc_epoch
;
533 candidate
->in_clientflag
= 0;
534 candidate
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
536 candidate
->state
= RXRPC_CONN_CLIENT
;
537 candidate
->avail_calls
= RXRPC_MAXCALLS
;
538 candidate
->security_level
= rx
->min_sec_level
;
539 candidate
->key
= key_get(bundle
->key
);
541 ret
= rxrpc_init_client_conn_security(candidate
);
543 key_put(candidate
->key
);
545 _leave(" = %d [key]", ret
);
549 write_lock_bh(&rxrpc_connection_lock
);
550 list_add_tail(&candidate
->link
, &rxrpc_connections
);
551 write_unlock_bh(&rxrpc_connection_lock
);
553 spin_lock(&trans
->client_lock
);
555 list_add(&candidate
->bundle_link
, &bundle
->unused_conns
);
557 atomic_inc(&bundle
->usage
);
558 atomic_inc(&trans
->usage
);
560 _net("CONNECT new %d on TRANS %d",
561 candidate
->debug_id
, candidate
->trans
->debug_id
);
563 rxrpc_assign_connection_id(candidate
);
564 if (candidate
->security
)
565 candidate
->security
->prime_packet_security(candidate
);
567 /* leave the candidate lurking in zombie mode attached to the
568 * bundle until we're ready for it */
569 rxrpc_put_connection(candidate
);
573 /* we've got a connection with a free channel and we can now attach the
575 * - we're holding the transport's client lock
576 * - we're holding a reference on the connection
577 * - we're holding a reference on the bundle
579 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
580 if (!conn
->channels
[chan
])
582 ASSERT(conn
->channels
[0] == NULL
||
583 conn
->channels
[1] == NULL
||
584 conn
->channels
[2] == NULL
||
585 conn
->channels
[3] == NULL
);
589 conn
->channels
[chan
] = call
;
591 call
->channel
= chan
;
592 call
->cid
= conn
->cid
| htonl(chan
);
593 call
->call_id
= htonl(++conn
->call_counter
);
595 _net("CONNECT client on conn %d chan %d as call %x",
596 conn
->debug_id
, chan
, ntohl(call
->call_id
));
598 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
599 spin_unlock(&trans
->client_lock
);
601 rxrpc_add_call_ID_to_conn(conn
, call
);
607 remove_wait_queue(&bundle
->chanwait
, &myself
);
608 __set_current_state(TASK_RUNNING
);
610 _leave(" = -ERESTARTSYS");
615 * get a record of an incoming connection
617 struct rxrpc_connection
*
618 rxrpc_incoming_connection(struct rxrpc_transport
*trans
,
619 struct rxrpc_header
*hdr
,
622 struct rxrpc_connection
*conn
, *candidate
= NULL
;
623 struct rb_node
*p
, **pp
;
624 const char *new = "old";
630 ASSERT(hdr
->flags
& RXRPC_CLIENT_INITIATED
);
633 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
635 /* search the connection list first */
636 read_lock_bh(&trans
->conn_lock
);
638 p
= trans
->server_conns
.rb_node
;
640 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
642 _debug("maybe %x", conn
->real_conn_id
);
644 if (epoch
< conn
->epoch
)
646 else if (epoch
> conn
->epoch
)
648 else if (conn_id
< conn
->real_conn_id
)
650 else if (conn_id
> conn
->real_conn_id
)
653 goto found_extant_connection
;
655 read_unlock_bh(&trans
->conn_lock
);
657 /* not yet present - create a candidate for a new record and then
659 candidate
= rxrpc_alloc_connection(gfp
);
661 _leave(" = -ENOMEM");
662 return ERR_PTR(-ENOMEM
);
665 candidate
->trans
= trans
;
666 candidate
->epoch
= hdr
->epoch
;
667 candidate
->cid
= hdr
->cid
& cpu_to_be32(RXRPC_CIDMASK
);
668 candidate
->service_id
= hdr
->serviceId
;
669 candidate
->security_ix
= hdr
->securityIndex
;
670 candidate
->in_clientflag
= RXRPC_CLIENT_INITIATED
;
671 candidate
->out_clientflag
= 0;
672 candidate
->real_conn_id
= conn_id
;
673 candidate
->state
= RXRPC_CONN_SERVER
;
674 if (candidate
->service_id
)
675 candidate
->state
= RXRPC_CONN_SERVER_UNSECURED
;
677 write_lock_bh(&trans
->conn_lock
);
679 pp
= &trans
->server_conns
.rb_node
;
683 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
685 if (epoch
< conn
->epoch
)
686 pp
= &(*pp
)->rb_left
;
687 else if (epoch
> conn
->epoch
)
688 pp
= &(*pp
)->rb_right
;
689 else if (conn_id
< conn
->real_conn_id
)
690 pp
= &(*pp
)->rb_left
;
691 else if (conn_id
> conn
->real_conn_id
)
692 pp
= &(*pp
)->rb_right
;
694 goto found_extant_second
;
697 /* we can now add the new candidate to the list */
700 rb_link_node(&conn
->node
, p
, pp
);
701 rb_insert_color(&conn
->node
, &trans
->server_conns
);
702 atomic_inc(&conn
->trans
->usage
);
704 write_unlock_bh(&trans
->conn_lock
);
706 write_lock_bh(&rxrpc_connection_lock
);
707 list_add_tail(&conn
->link
, &rxrpc_connections
);
708 write_unlock_bh(&rxrpc_connection_lock
);
713 _net("CONNECTION %s %d {%x}", new, conn
->debug_id
, conn
->real_conn_id
);
715 _leave(" = %p {u=%d}", conn
, atomic_read(&conn
->usage
));
718 /* we found the connection in the list immediately */
719 found_extant_connection
:
720 if (hdr
->securityIndex
!= conn
->security_ix
) {
721 read_unlock_bh(&trans
->conn_lock
);
722 goto security_mismatch
;
724 atomic_inc(&conn
->usage
);
725 read_unlock_bh(&trans
->conn_lock
);
728 /* we found the connection on the second time through the list */
730 if (hdr
->securityIndex
!= conn
->security_ix
) {
731 write_unlock_bh(&trans
->conn_lock
);
732 goto security_mismatch
;
734 atomic_inc(&conn
->usage
);
735 write_unlock_bh(&trans
->conn_lock
);
741 _leave(" = -EKEYREJECTED");
742 return ERR_PTR(-EKEYREJECTED
);
746 * find a connection based on transport and RxRPC connection ID for an incoming
749 struct rxrpc_connection
*rxrpc_find_connection(struct rxrpc_transport
*trans
,
750 struct rxrpc_header
*hdr
)
752 struct rxrpc_connection
*conn
;
757 _enter(",{%x,%x}", ntohl(hdr
->cid
), hdr
->flags
);
759 read_lock_bh(&trans
->conn_lock
);
761 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
764 if (hdr
->flags
& RXRPC_CLIENT_INITIATED
)
765 p
= trans
->server_conns
.rb_node
;
767 p
= trans
->client_conns
.rb_node
;
770 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
772 _debug("maybe %x", conn
->real_conn_id
);
774 if (epoch
< conn
->epoch
)
776 else if (epoch
> conn
->epoch
)
778 else if (conn_id
< conn
->real_conn_id
)
780 else if (conn_id
> conn
->real_conn_id
)
786 read_unlock_bh(&trans
->conn_lock
);
791 atomic_inc(&conn
->usage
);
792 read_unlock_bh(&trans
->conn_lock
);
793 _leave(" = %p", conn
);
798 * release a virtual connection
800 void rxrpc_put_connection(struct rxrpc_connection
*conn
)
802 _enter("%p{u=%d,d=%d}",
803 conn
, atomic_read(&conn
->usage
), conn
->debug_id
);
805 ASSERTCMP(atomic_read(&conn
->usage
), >, 0);
807 conn
->put_time
= get_seconds();
808 if (atomic_dec_and_test(&conn
->usage
)) {
810 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);
817 * destroy a virtual connection
819 static void rxrpc_destroy_connection(struct rxrpc_connection
*conn
)
821 _enter("%p{%d}", conn
, atomic_read(&conn
->usage
));
823 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
825 _net("DESTROY CONN %d", conn
->debug_id
);
828 rxrpc_put_bundle(conn
->trans
, conn
->bundle
);
830 ASSERT(RB_EMPTY_ROOT(&conn
->calls
));
831 rxrpc_purge_queue(&conn
->rx_queue
);
833 rxrpc_clear_conn_security(conn
);
834 rxrpc_put_transport(conn
->trans
);
840 * reap dead connections
842 static void rxrpc_connection_reaper(struct work_struct
*work
)
844 struct rxrpc_connection
*conn
, *_p
;
845 unsigned long now
, earliest
, reap_time
;
847 LIST_HEAD(graveyard
);
852 earliest
= ULONG_MAX
;
854 write_lock_bh(&rxrpc_connection_lock
);
855 list_for_each_entry_safe(conn
, _p
, &rxrpc_connections
, link
) {
856 _debug("reap CONN %d { u=%d,t=%ld }",
857 conn
->debug_id
, atomic_read(&conn
->usage
),
858 (long) now
- (long) conn
->put_time
);
860 if (likely(atomic_read(&conn
->usage
) > 0))
863 spin_lock(&conn
->trans
->client_lock
);
864 write_lock(&conn
->trans
->conn_lock
);
865 reap_time
= conn
->put_time
+ rxrpc_connection_timeout
;
867 if (atomic_read(&conn
->usage
) > 0) {
869 } else if (reap_time
<= now
) {
870 list_move_tail(&conn
->link
, &graveyard
);
871 if (conn
->out_clientflag
)
872 rb_erase(&conn
->node
,
873 &conn
->trans
->client_conns
);
875 rb_erase(&conn
->node
,
876 &conn
->trans
->server_conns
);
878 list_del_init(&conn
->bundle_link
);
879 conn
->bundle
->num_conns
--;
882 } else if (reap_time
< earliest
) {
883 earliest
= reap_time
;
886 write_unlock(&conn
->trans
->conn_lock
);
887 spin_unlock(&conn
->trans
->client_lock
);
889 write_unlock_bh(&rxrpc_connection_lock
);
891 if (earliest
!= ULONG_MAX
) {
892 _debug("reschedule reaper %ld", (long) earliest
- now
);
893 ASSERTCMP(earliest
, >, now
);
894 rxrpc_queue_delayed_work(&rxrpc_connection_reap
,
895 (earliest
- now
) * HZ
);
898 /* then destroy all those pulled out */
899 while (!list_empty(&graveyard
)) {
900 conn
= list_entry(graveyard
.next
, struct rxrpc_connection
,
902 list_del_init(&conn
->link
);
904 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
905 rxrpc_destroy_connection(conn
);
912 * preemptively destroy all the connection records rather than waiting for them
915 void __exit
rxrpc_destroy_all_connections(void)
919 rxrpc_connection_timeout
= 0;
920 cancel_delayed_work(&rxrpc_connection_reap
);
921 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);