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
);
386 /* we've got a connection with a free channel and we can now attach the
388 * - we're holding the transport's client lock
389 * - we're holding a reference on the connection
391 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
392 if (!conn
->channels
[chan
])
394 goto no_free_channels
;
397 atomic_inc(&conn
->usage
);
398 conn
->channels
[chan
] = call
;
400 call
->channel
= chan
;
401 call
->cid
= conn
->cid
| htonl(chan
);
402 call
->call_id
= htonl(++conn
->call_counter
);
404 _net("CONNECT client on conn %d chan %d as call %x",
405 conn
->debug_id
, chan
, ntohl(call
->call_id
));
407 spin_unlock(&trans
->client_lock
);
409 rxrpc_add_call_ID_to_conn(conn
, call
);
414 spin_unlock(&trans
->client_lock
);
420 * find a connection for a call
421 * - called in process context with IRQs enabled
423 int rxrpc_connect_call(struct rxrpc_sock
*rx
,
424 struct rxrpc_transport
*trans
,
425 struct rxrpc_conn_bundle
*bundle
,
426 struct rxrpc_call
*call
,
429 struct rxrpc_connection
*conn
, *candidate
;
432 DECLARE_WAITQUEUE(myself
, current
);
434 _enter("%p,%lx,", rx
, call
->user_call_ID
);
436 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN
, &rx
->flags
))
437 return rxrpc_connect_exclusive(rx
, trans
, bundle
->service_id
,
440 spin_lock(&trans
->client_lock
);
442 /* see if the bundle has a call slot available */
443 if (!list_empty(&bundle
->avail_conns
)) {
445 conn
= list_entry(bundle
->avail_conns
.next
,
446 struct rxrpc_connection
,
448 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
449 list_del_init(&conn
->bundle_link
);
453 if (--conn
->avail_calls
== 0)
454 list_move(&conn
->bundle_link
,
455 &bundle
->busy_conns
);
456 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
457 ASSERT(conn
->channels
[0] == NULL
||
458 conn
->channels
[1] == NULL
||
459 conn
->channels
[2] == NULL
||
460 conn
->channels
[3] == NULL
);
461 atomic_inc(&conn
->usage
);
465 if (!list_empty(&bundle
->unused_conns
)) {
467 conn
= list_entry(bundle
->unused_conns
.next
,
468 struct rxrpc_connection
,
470 if (conn
->state
>= RXRPC_CONN_REMOTELY_ABORTED
) {
471 list_del_init(&conn
->bundle_link
);
475 ASSERTCMP(conn
->avail_calls
, ==, RXRPC_MAXCALLS
);
476 conn
->avail_calls
= RXRPC_MAXCALLS
- 1;
477 ASSERT(conn
->channels
[0] == NULL
&&
478 conn
->channels
[1] == NULL
&&
479 conn
->channels
[2] == NULL
&&
480 conn
->channels
[3] == NULL
);
481 atomic_inc(&conn
->usage
);
482 list_move(&conn
->bundle_link
, &bundle
->avail_conns
);
486 /* need to allocate a new connection */
487 _debug("get new conn [%d]", bundle
->num_conns
);
489 spin_unlock(&trans
->client_lock
);
491 if (signal_pending(current
))
494 if (bundle
->num_conns
>= 20) {
495 _debug("too many conns");
497 if (!(gfp
& __GFP_WAIT
)) {
498 _leave(" = -EAGAIN");
502 add_wait_queue(&bundle
->chanwait
, &myself
);
504 set_current_state(TASK_INTERRUPTIBLE
);
505 if (bundle
->num_conns
< 20 ||
506 !list_empty(&bundle
->unused_conns
) ||
507 !list_empty(&bundle
->avail_conns
))
509 if (signal_pending(current
))
510 goto interrupted_dequeue
;
513 remove_wait_queue(&bundle
->chanwait
, &myself
);
514 __set_current_state(TASK_RUNNING
);
515 spin_lock(&trans
->client_lock
);
519 /* not yet present - create a candidate for a new connection and then
521 candidate
= rxrpc_alloc_connection(gfp
);
523 _leave(" = -ENOMEM");
527 candidate
->trans
= trans
;
528 candidate
->bundle
= bundle
;
529 candidate
->service_id
= bundle
->service_id
;
530 candidate
->epoch
= rxrpc_epoch
;
531 candidate
->in_clientflag
= 0;
532 candidate
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
534 candidate
->state
= RXRPC_CONN_CLIENT
;
535 candidate
->avail_calls
= RXRPC_MAXCALLS
;
536 candidate
->security_level
= rx
->min_sec_level
;
537 candidate
->key
= key_get(bundle
->key
);
539 ret
= rxrpc_init_client_conn_security(candidate
);
541 key_put(candidate
->key
);
543 _leave(" = %d [key]", ret
);
547 write_lock_bh(&rxrpc_connection_lock
);
548 list_add_tail(&candidate
->link
, &rxrpc_connections
);
549 write_unlock_bh(&rxrpc_connection_lock
);
551 spin_lock(&trans
->client_lock
);
553 list_add(&candidate
->bundle_link
, &bundle
->unused_conns
);
555 atomic_inc(&bundle
->usage
);
556 atomic_inc(&trans
->usage
);
558 _net("CONNECT new %d on TRANS %d",
559 candidate
->debug_id
, candidate
->trans
->debug_id
);
561 rxrpc_assign_connection_id(candidate
);
562 if (candidate
->security
)
563 candidate
->security
->prime_packet_security(candidate
);
565 /* leave the candidate lurking in zombie mode attached to the
566 * bundle until we're ready for it */
567 rxrpc_put_connection(candidate
);
571 /* we've got a connection with a free channel and we can now attach the
573 * - we're holding the transport's client lock
574 * - we're holding a reference on the connection
575 * - we're holding a reference on the bundle
577 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
578 if (!conn
->channels
[chan
])
580 ASSERT(conn
->channels
[0] == NULL
||
581 conn
->channels
[1] == NULL
||
582 conn
->channels
[2] == NULL
||
583 conn
->channels
[3] == NULL
);
587 conn
->channels
[chan
] = call
;
589 call
->channel
= chan
;
590 call
->cid
= conn
->cid
| htonl(chan
);
591 call
->call_id
= htonl(++conn
->call_counter
);
593 _net("CONNECT client on conn %d chan %d as call %x",
594 conn
->debug_id
, chan
, ntohl(call
->call_id
));
596 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
597 spin_unlock(&trans
->client_lock
);
599 rxrpc_add_call_ID_to_conn(conn
, call
);
605 remove_wait_queue(&bundle
->chanwait
, &myself
);
606 __set_current_state(TASK_RUNNING
);
608 _leave(" = -ERESTARTSYS");
613 * get a record of an incoming connection
615 struct rxrpc_connection
*
616 rxrpc_incoming_connection(struct rxrpc_transport
*trans
,
617 struct rxrpc_header
*hdr
,
620 struct rxrpc_connection
*conn
, *candidate
= NULL
;
621 struct rb_node
*p
, **pp
;
622 const char *new = "old";
628 ASSERT(hdr
->flags
& RXRPC_CLIENT_INITIATED
);
631 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
633 /* search the connection list first */
634 read_lock_bh(&trans
->conn_lock
);
636 p
= trans
->server_conns
.rb_node
;
638 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
640 _debug("maybe %x", conn
->real_conn_id
);
642 if (epoch
< conn
->epoch
)
644 else if (epoch
> conn
->epoch
)
646 else if (conn_id
< conn
->real_conn_id
)
648 else if (conn_id
> conn
->real_conn_id
)
651 goto found_extant_connection
;
653 read_unlock_bh(&trans
->conn_lock
);
655 /* not yet present - create a candidate for a new record and then
657 candidate
= rxrpc_alloc_connection(gfp
);
659 _leave(" = -ENOMEM");
660 return ERR_PTR(-ENOMEM
);
663 candidate
->trans
= trans
;
664 candidate
->epoch
= hdr
->epoch
;
665 candidate
->cid
= hdr
->cid
& cpu_to_be32(RXRPC_CIDMASK
);
666 candidate
->service_id
= hdr
->serviceId
;
667 candidate
->security_ix
= hdr
->securityIndex
;
668 candidate
->in_clientflag
= RXRPC_CLIENT_INITIATED
;
669 candidate
->out_clientflag
= 0;
670 candidate
->real_conn_id
= conn_id
;
671 candidate
->state
= RXRPC_CONN_SERVER
;
672 if (candidate
->service_id
)
673 candidate
->state
= RXRPC_CONN_SERVER_UNSECURED
;
675 write_lock_bh(&trans
->conn_lock
);
677 pp
= &trans
->server_conns
.rb_node
;
681 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
683 if (epoch
< conn
->epoch
)
684 pp
= &(*pp
)->rb_left
;
685 else if (epoch
> conn
->epoch
)
686 pp
= &(*pp
)->rb_right
;
687 else if (conn_id
< conn
->real_conn_id
)
688 pp
= &(*pp
)->rb_left
;
689 else if (conn_id
> conn
->real_conn_id
)
690 pp
= &(*pp
)->rb_right
;
692 goto found_extant_second
;
695 /* we can now add the new candidate to the list */
698 rb_link_node(&conn
->node
, p
, pp
);
699 rb_insert_color(&conn
->node
, &trans
->server_conns
);
700 atomic_inc(&conn
->trans
->usage
);
702 write_unlock_bh(&trans
->conn_lock
);
704 write_lock_bh(&rxrpc_connection_lock
);
705 list_add_tail(&conn
->link
, &rxrpc_connections
);
706 write_unlock_bh(&rxrpc_connection_lock
);
711 _net("CONNECTION %s %d {%x}", new, conn
->debug_id
, conn
->real_conn_id
);
713 _leave(" = %p {u=%d}", conn
, atomic_read(&conn
->usage
));
716 /* we found the connection in the list immediately */
717 found_extant_connection
:
718 if (hdr
->securityIndex
!= conn
->security_ix
) {
719 read_unlock_bh(&trans
->conn_lock
);
720 goto security_mismatch
;
722 atomic_inc(&conn
->usage
);
723 read_unlock_bh(&trans
->conn_lock
);
726 /* we found the connection on the second time through the list */
728 if (hdr
->securityIndex
!= conn
->security_ix
) {
729 write_unlock_bh(&trans
->conn_lock
);
730 goto security_mismatch
;
732 atomic_inc(&conn
->usage
);
733 write_unlock_bh(&trans
->conn_lock
);
739 _leave(" = -EKEYREJECTED");
740 return ERR_PTR(-EKEYREJECTED
);
744 * find a connection based on transport and RxRPC connection ID for an incoming
747 struct rxrpc_connection
*rxrpc_find_connection(struct rxrpc_transport
*trans
,
748 struct rxrpc_header
*hdr
)
750 struct rxrpc_connection
*conn
;
755 _enter(",{%x,%x}", ntohl(hdr
->cid
), hdr
->flags
);
757 read_lock_bh(&trans
->conn_lock
);
759 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
762 if (hdr
->flags
& RXRPC_CLIENT_INITIATED
)
763 p
= trans
->server_conns
.rb_node
;
765 p
= trans
->client_conns
.rb_node
;
768 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
770 _debug("maybe %x", conn
->real_conn_id
);
772 if (epoch
< conn
->epoch
)
774 else if (epoch
> conn
->epoch
)
776 else if (conn_id
< conn
->real_conn_id
)
778 else if (conn_id
> conn
->real_conn_id
)
784 read_unlock_bh(&trans
->conn_lock
);
789 atomic_inc(&conn
->usage
);
790 read_unlock_bh(&trans
->conn_lock
);
791 _leave(" = %p", conn
);
796 * release a virtual connection
798 void rxrpc_put_connection(struct rxrpc_connection
*conn
)
800 _enter("%p{u=%d,d=%d}",
801 conn
, atomic_read(&conn
->usage
), conn
->debug_id
);
803 ASSERTCMP(atomic_read(&conn
->usage
), >, 0);
805 conn
->put_time
= get_seconds();
806 if (atomic_dec_and_test(&conn
->usage
)) {
808 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);
815 * destroy a virtual connection
817 static void rxrpc_destroy_connection(struct rxrpc_connection
*conn
)
819 _enter("%p{%d}", conn
, atomic_read(&conn
->usage
));
821 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
823 _net("DESTROY CONN %d", conn
->debug_id
);
826 rxrpc_put_bundle(conn
->trans
, conn
->bundle
);
828 ASSERT(RB_EMPTY_ROOT(&conn
->calls
));
829 rxrpc_purge_queue(&conn
->rx_queue
);
831 rxrpc_clear_conn_security(conn
);
832 rxrpc_put_transport(conn
->trans
);
838 * reap dead connections
840 static void rxrpc_connection_reaper(struct work_struct
*work
)
842 struct rxrpc_connection
*conn
, *_p
;
843 unsigned long now
, earliest
, reap_time
;
845 LIST_HEAD(graveyard
);
850 earliest
= ULONG_MAX
;
852 write_lock_bh(&rxrpc_connection_lock
);
853 list_for_each_entry_safe(conn
, _p
, &rxrpc_connections
, link
) {
854 _debug("reap CONN %d { u=%d,t=%ld }",
855 conn
->debug_id
, atomic_read(&conn
->usage
),
856 (long) now
- (long) conn
->put_time
);
858 if (likely(atomic_read(&conn
->usage
) > 0))
861 spin_lock(&conn
->trans
->client_lock
);
862 write_lock(&conn
->trans
->conn_lock
);
863 reap_time
= conn
->put_time
+ rxrpc_connection_timeout
;
865 if (atomic_read(&conn
->usage
) > 0) {
867 } else if (reap_time
<= now
) {
868 list_move_tail(&conn
->link
, &graveyard
);
869 if (conn
->out_clientflag
)
870 rb_erase(&conn
->node
,
871 &conn
->trans
->client_conns
);
873 rb_erase(&conn
->node
,
874 &conn
->trans
->server_conns
);
876 list_del_init(&conn
->bundle_link
);
877 conn
->bundle
->num_conns
--;
880 } else if (reap_time
< earliest
) {
881 earliest
= reap_time
;
884 write_unlock(&conn
->trans
->conn_lock
);
885 spin_unlock(&conn
->trans
->client_lock
);
887 write_unlock_bh(&rxrpc_connection_lock
);
889 if (earliest
!= ULONG_MAX
) {
890 _debug("reschedule reaper %ld", (long) earliest
- now
);
891 ASSERTCMP(earliest
, >, now
);
892 rxrpc_queue_delayed_work(&rxrpc_connection_reap
,
893 (earliest
- now
) * HZ
);
896 /* then destroy all those pulled out */
897 while (!list_empty(&graveyard
)) {
898 conn
= list_entry(graveyard
.next
, struct rxrpc_connection
,
900 list_del_init(&conn
->link
);
902 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
903 rxrpc_destroy_connection(conn
);
910 * preemptively destroy all the connection records rather than waiting for them
913 void __exit
rxrpc_destroy_all_connections(void)
917 rxrpc_connection_timeout
= 0;
918 cancel_delayed_work(&rxrpc_connection_reap
);
919 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);