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/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/crypto.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
20 static void rxrpc_connection_reaper(struct work_struct
*work
);
22 LIST_HEAD(rxrpc_connections
);
23 DEFINE_RWLOCK(rxrpc_connection_lock
);
24 static unsigned long rxrpc_connection_timeout
= 10 * 60;
25 static DECLARE_DELAYED_WORK(rxrpc_connection_reap
, rxrpc_connection_reaper
);
28 * allocate a new client connection bundle
30 static struct rxrpc_conn_bundle
*rxrpc_alloc_bundle(gfp_t gfp
)
32 struct rxrpc_conn_bundle
*bundle
;
36 bundle
= kzalloc(sizeof(struct rxrpc_conn_bundle
), gfp
);
38 INIT_LIST_HEAD(&bundle
->unused_conns
);
39 INIT_LIST_HEAD(&bundle
->avail_conns
);
40 INIT_LIST_HEAD(&bundle
->busy_conns
);
41 init_waitqueue_head(&bundle
->chanwait
);
42 atomic_set(&bundle
->usage
, 1);
45 _leave(" = %p", bundle
);
50 * compare bundle parameters with what we're looking for
51 * - return -ve, 0 or +ve
54 int rxrpc_cmp_bundle(const struct rxrpc_conn_bundle
*bundle
,
55 struct key
*key
, __be16 service_id
)
57 return (bundle
->service_id
- service_id
) ?:
58 ((unsigned long) bundle
->key
- (unsigned long) key
);
62 * get bundle of client connections that a client socket can make use of
64 struct rxrpc_conn_bundle
*rxrpc_get_bundle(struct rxrpc_sock
*rx
,
65 struct rxrpc_transport
*trans
,
70 struct rxrpc_conn_bundle
*bundle
, *candidate
;
71 struct rb_node
*p
, *parent
, **pp
;
73 _enter("%p{%x},%x,%hx,",
74 rx
, key_serial(key
), trans
->debug_id
, ntohl(service_id
));
76 if (rx
->trans
== trans
&& rx
->bundle
) {
77 atomic_inc(&rx
->bundle
->usage
);
81 /* search the extant bundles first for one that matches the specified
83 spin_lock(&trans
->client_lock
);
85 p
= trans
->bundles
.rb_node
;
87 bundle
= rb_entry(p
, struct rxrpc_conn_bundle
, node
);
89 if (rxrpc_cmp_bundle(bundle
, key
, service_id
) < 0)
91 else if (rxrpc_cmp_bundle(bundle
, key
, service_id
) > 0)
94 goto found_extant_bundle
;
97 spin_unlock(&trans
->client_lock
);
99 /* not yet present - create a candidate for a new record and then
101 candidate
= rxrpc_alloc_bundle(gfp
);
103 _leave(" = -ENOMEM");
104 return ERR_PTR(-ENOMEM
);
107 candidate
->key
= key_get(key
);
108 candidate
->service_id
= service_id
;
110 spin_lock(&trans
->client_lock
);
112 pp
= &trans
->bundles
.rb_node
;
116 bundle
= rb_entry(parent
, struct rxrpc_conn_bundle
, node
);
118 if (rxrpc_cmp_bundle(bundle
, key
, service_id
) < 0)
119 pp
= &(*pp
)->rb_left
;
120 else if (rxrpc_cmp_bundle(bundle
, key
, service_id
) > 0)
121 pp
= &(*pp
)->rb_right
;
123 goto found_extant_second
;
126 /* second search also failed; add the new bundle */
130 rb_link_node(&bundle
->node
, parent
, pp
);
131 rb_insert_color(&bundle
->node
, &trans
->bundles
);
132 spin_unlock(&trans
->client_lock
);
133 _net("BUNDLE new on trans %d", trans
->debug_id
);
134 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
135 atomic_inc(&bundle
->usage
);
138 _leave(" = %p [new]", bundle
);
141 /* we found the bundle in the list immediately */
143 atomic_inc(&bundle
->usage
);
144 spin_unlock(&trans
->client_lock
);
145 _net("BUNDLE old on trans %d", trans
->debug_id
);
146 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
147 atomic_inc(&bundle
->usage
);
150 _leave(" = %p [extant %d]", bundle
, atomic_read(&bundle
->usage
));
153 /* we found the bundle on the second time through the list */
155 atomic_inc(&bundle
->usage
);
156 spin_unlock(&trans
->client_lock
);
158 _net("BUNDLE old2 on trans %d", trans
->debug_id
);
159 if (!rx
->bundle
&& rx
->sk
.sk_state
== RXRPC_CLIENT_CONNECTED
) {
160 atomic_inc(&bundle
->usage
);
163 _leave(" = %p [second %d]", bundle
, atomic_read(&bundle
->usage
));
170 void rxrpc_put_bundle(struct rxrpc_transport
*trans
,
171 struct rxrpc_conn_bundle
*bundle
)
173 _enter("%p,%p{%d}",trans
, bundle
, atomic_read(&bundle
->usage
));
175 if (atomic_dec_and_lock(&bundle
->usage
, &trans
->client_lock
)) {
176 _debug("Destroy bundle");
177 rb_erase(&bundle
->node
, &trans
->bundles
);
178 spin_unlock(&trans
->client_lock
);
179 ASSERT(list_empty(&bundle
->unused_conns
));
180 ASSERT(list_empty(&bundle
->avail_conns
));
181 ASSERT(list_empty(&bundle
->busy_conns
));
182 ASSERTCMP(bundle
->num_conns
, ==, 0);
183 key_put(bundle
->key
);
191 * allocate a new connection
193 static struct rxrpc_connection
*rxrpc_alloc_connection(gfp_t gfp
)
195 struct rxrpc_connection
*conn
;
199 conn
= kzalloc(sizeof(struct rxrpc_connection
), gfp
);
201 INIT_WORK(&conn
->processor
, &rxrpc_process_connection
);
202 INIT_LIST_HEAD(&conn
->bundle_link
);
203 conn
->calls
= RB_ROOT
;
204 skb_queue_head_init(&conn
->rx_queue
);
205 rwlock_init(&conn
->lock
);
206 spin_lock_init(&conn
->state_lock
);
207 atomic_set(&conn
->usage
, 1);
208 conn
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
209 conn
->avail_calls
= RXRPC_MAXCALLS
;
210 conn
->size_align
= 4;
211 conn
->header_size
= sizeof(struct rxrpc_header
);
214 _leave(" = %p{%d}", conn
, conn
? conn
->debug_id
: 0);
219 * assign a connection ID to a connection and add it to the transport's
220 * connection lookup tree
221 * - called with transport client lock held
223 static void rxrpc_assign_connection_id(struct rxrpc_connection
*conn
)
225 struct rxrpc_connection
*xconn
;
226 struct rb_node
*parent
, **p
;
234 write_lock_bh(&conn
->trans
->conn_lock
);
236 conn
->trans
->conn_idcounter
+= RXRPC_CID_INC
;
237 if (conn
->trans
->conn_idcounter
< RXRPC_CID_INC
)
238 conn
->trans
->conn_idcounter
= RXRPC_CID_INC
;
239 real_conn_id
= conn
->trans
->conn_idcounter
;
243 p
= &conn
->trans
->client_conns
.rb_node
;
247 xconn
= rb_entry(parent
, struct rxrpc_connection
, node
);
249 if (epoch
< xconn
->epoch
)
251 else if (epoch
> xconn
->epoch
)
253 else if (real_conn_id
< xconn
->real_conn_id
)
255 else if (real_conn_id
> xconn
->real_conn_id
)
261 /* we've found a suitable hole - arrange for this connection to occupy
263 rb_link_node(&conn
->node
, parent
, p
);
264 rb_insert_color(&conn
->node
, &conn
->trans
->client_conns
);
266 conn
->real_conn_id
= real_conn_id
;
267 conn
->cid
= htonl(real_conn_id
);
268 write_unlock_bh(&conn
->trans
->conn_lock
);
269 _leave(" [CONNID %x CID %x]", real_conn_id
, ntohl(conn
->cid
));
272 /* we found a connection with the proposed ID - walk the tree from that
273 * point looking for the next unused ID */
276 real_conn_id
+= RXRPC_CID_INC
;
277 if (real_conn_id
< RXRPC_CID_INC
) {
278 real_conn_id
= RXRPC_CID_INC
;
279 conn
->trans
->conn_idcounter
= real_conn_id
;
280 goto attempt_insertion
;
283 parent
= rb_next(parent
);
285 goto attempt_insertion
;
287 xconn
= rb_entry(parent
, struct rxrpc_connection
, node
);
288 if (epoch
< xconn
->epoch
||
289 real_conn_id
< xconn
->real_conn_id
)
290 goto attempt_insertion
;
295 * add a call to a connection's call-by-ID tree
297 static void rxrpc_add_call_ID_to_conn(struct rxrpc_connection
*conn
,
298 struct rxrpc_call
*call
)
300 struct rxrpc_call
*xcall
;
301 struct rb_node
*parent
, **p
;
304 write_lock_bh(&conn
->lock
);
306 call_id
= call
->call_id
;
307 p
= &conn
->calls
.rb_node
;
311 xcall
= rb_entry(parent
, struct rxrpc_call
, conn_node
);
313 if (call_id
< xcall
->call_id
)
315 else if (call_id
> xcall
->call_id
)
321 rb_link_node(&call
->conn_node
, parent
, p
);
322 rb_insert_color(&call
->conn_node
, &conn
->calls
);
324 write_unlock_bh(&conn
->lock
);
328 * connect a call on an exclusive connection
330 static int rxrpc_connect_exclusive(struct rxrpc_sock
*rx
,
331 struct rxrpc_transport
*trans
,
333 struct rxrpc_call
*call
,
336 struct rxrpc_connection
*conn
;
343 /* not yet present - create a candidate for a new connection
344 * and then redo the check */
345 conn
= rxrpc_alloc_connection(gfp
);
347 _leave(" = %ld", PTR_ERR(conn
));
348 return PTR_ERR(conn
);
353 conn
->service_id
= service_id
;
354 conn
->epoch
= rxrpc_epoch
;
355 conn
->in_clientflag
= 0;
356 conn
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
358 conn
->state
= RXRPC_CONN_CLIENT
;
359 conn
->avail_calls
= RXRPC_MAXCALLS
- 1;
360 conn
->security_level
= rx
->min_sec_level
;
361 conn
->key
= key_get(rx
->key
);
363 ret
= rxrpc_init_client_conn_security(conn
);
367 _leave(" = %d [key]", ret
);
371 write_lock_bh(&rxrpc_connection_lock
);
372 list_add_tail(&conn
->link
, &rxrpc_connections
);
373 write_unlock_bh(&rxrpc_connection_lock
);
375 spin_lock(&trans
->client_lock
);
376 atomic_inc(&trans
->usage
);
378 _net("CONNECT EXCL new %d on TRANS %d",
379 conn
->debug_id
, conn
->trans
->debug_id
);
381 rxrpc_assign_connection_id(conn
);
385 /* we've got a connection with a free channel and we can now attach the
387 * - we're holding the transport's client lock
388 * - we're holding a reference on the connection
390 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
391 if (!conn
->channels
[chan
])
393 goto no_free_channels
;
396 atomic_inc(&conn
->usage
);
397 conn
->channels
[chan
] = call
;
399 call
->channel
= chan
;
400 call
->cid
= conn
->cid
| htonl(chan
);
401 call
->call_id
= htonl(++conn
->call_counter
);
403 _net("CONNECT client on conn %d chan %d as call %x",
404 conn
->debug_id
, chan
, ntohl(call
->call_id
));
406 spin_unlock(&trans
->client_lock
);
408 rxrpc_add_call_ID_to_conn(conn
, call
);
413 spin_unlock(&trans
->client_lock
);
419 * find a connection for a call
420 * - called in process context with IRQs enabled
422 int rxrpc_connect_call(struct rxrpc_sock
*rx
,
423 struct rxrpc_transport
*trans
,
424 struct rxrpc_conn_bundle
*bundle
,
425 struct rxrpc_call
*call
,
428 struct rxrpc_connection
*conn
, *candidate
;
431 DECLARE_WAITQUEUE(myself
, current
);
433 _enter("%p,%lx,", rx
, call
->user_call_ID
);
435 if (test_bit(RXRPC_SOCK_EXCLUSIVE_CONN
, &rx
->flags
))
436 return rxrpc_connect_exclusive(rx
, trans
, bundle
->service_id
,
439 spin_lock(&trans
->client_lock
);
441 /* see if the bundle has a call slot available */
442 if (!list_empty(&bundle
->avail_conns
)) {
444 conn
= list_entry(bundle
->avail_conns
.next
,
445 struct rxrpc_connection
,
447 if (--conn
->avail_calls
== 0)
448 list_move(&conn
->bundle_link
,
449 &bundle
->busy_conns
);
450 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
451 ASSERT(conn
->channels
[0] == NULL
||
452 conn
->channels
[1] == NULL
||
453 conn
->channels
[2] == NULL
||
454 conn
->channels
[3] == NULL
);
455 atomic_inc(&conn
->usage
);
459 if (!list_empty(&bundle
->unused_conns
)) {
461 conn
= list_entry(bundle
->unused_conns
.next
,
462 struct rxrpc_connection
,
464 ASSERTCMP(conn
->avail_calls
, ==, RXRPC_MAXCALLS
);
465 conn
->avail_calls
= RXRPC_MAXCALLS
- 1;
466 ASSERT(conn
->channels
[0] == NULL
&&
467 conn
->channels
[1] == NULL
&&
468 conn
->channels
[2] == NULL
&&
469 conn
->channels
[3] == NULL
);
470 atomic_inc(&conn
->usage
);
471 list_move(&conn
->bundle_link
, &bundle
->avail_conns
);
475 /* need to allocate a new connection */
476 _debug("get new conn [%d]", bundle
->num_conns
);
478 spin_unlock(&trans
->client_lock
);
480 if (signal_pending(current
))
483 if (bundle
->num_conns
>= 20) {
484 _debug("too many conns");
486 if (!(gfp
& __GFP_WAIT
)) {
487 _leave(" = -EAGAIN");
491 add_wait_queue(&bundle
->chanwait
, &myself
);
493 set_current_state(TASK_INTERRUPTIBLE
);
494 if (bundle
->num_conns
< 20 ||
495 !list_empty(&bundle
->unused_conns
) ||
496 !list_empty(&bundle
->avail_conns
))
498 if (signal_pending(current
))
499 goto interrupted_dequeue
;
502 remove_wait_queue(&bundle
->chanwait
, &myself
);
503 __set_current_state(TASK_RUNNING
);
504 spin_lock(&trans
->client_lock
);
508 /* not yet present - create a candidate for a new connection and then
510 candidate
= rxrpc_alloc_connection(gfp
);
511 if (IS_ERR(candidate
)) {
512 _leave(" = %ld", PTR_ERR(candidate
));
513 return PTR_ERR(candidate
);
516 candidate
->trans
= trans
;
517 candidate
->bundle
= bundle
;
518 candidate
->service_id
= bundle
->service_id
;
519 candidate
->epoch
= rxrpc_epoch
;
520 candidate
->in_clientflag
= 0;
521 candidate
->out_clientflag
= RXRPC_CLIENT_INITIATED
;
523 candidate
->state
= RXRPC_CONN_CLIENT
;
524 candidate
->avail_calls
= RXRPC_MAXCALLS
;
525 candidate
->security_level
= rx
->min_sec_level
;
526 candidate
->key
= key_get(bundle
->key
);
528 ret
= rxrpc_init_client_conn_security(candidate
);
530 key_put(candidate
->key
);
532 _leave(" = %d [key]", ret
);
536 write_lock_bh(&rxrpc_connection_lock
);
537 list_add_tail(&candidate
->link
, &rxrpc_connections
);
538 write_unlock_bh(&rxrpc_connection_lock
);
540 spin_lock(&trans
->client_lock
);
542 list_add(&candidate
->bundle_link
, &bundle
->unused_conns
);
544 atomic_inc(&bundle
->usage
);
545 atomic_inc(&trans
->usage
);
547 _net("CONNECT new %d on TRANS %d",
548 candidate
->debug_id
, candidate
->trans
->debug_id
);
550 rxrpc_assign_connection_id(candidate
);
551 if (candidate
->security
)
552 candidate
->security
->prime_packet_security(candidate
);
554 /* leave the candidate lurking in zombie mode attached to the
555 * bundle until we're ready for it */
556 rxrpc_put_connection(candidate
);
560 /* we've got a connection with a free channel and we can now attach the
562 * - we're holding the transport's client lock
563 * - we're holding a reference on the connection
564 * - we're holding a reference on the bundle
566 for (chan
= 0; chan
< RXRPC_MAXCALLS
; chan
++)
567 if (!conn
->channels
[chan
])
569 ASSERT(conn
->channels
[0] == NULL
||
570 conn
->channels
[1] == NULL
||
571 conn
->channels
[2] == NULL
||
572 conn
->channels
[3] == NULL
);
576 conn
->channels
[chan
] = call
;
578 call
->channel
= chan
;
579 call
->cid
= conn
->cid
| htonl(chan
);
580 call
->call_id
= htonl(++conn
->call_counter
);
582 _net("CONNECT client on conn %d chan %d as call %x",
583 conn
->debug_id
, chan
, ntohl(call
->call_id
));
585 ASSERTCMP(conn
->avail_calls
, <, RXRPC_MAXCALLS
);
586 spin_unlock(&trans
->client_lock
);
588 rxrpc_add_call_ID_to_conn(conn
, call
);
594 remove_wait_queue(&bundle
->chanwait
, &myself
);
595 __set_current_state(TASK_RUNNING
);
597 _leave(" = -ERESTARTSYS");
602 * get a record of an incoming connection
604 struct rxrpc_connection
*
605 rxrpc_incoming_connection(struct rxrpc_transport
*trans
,
606 struct rxrpc_header
*hdr
,
609 struct rxrpc_connection
*conn
, *candidate
= NULL
;
610 struct rb_node
*p
, **pp
;
611 const char *new = "old";
617 ASSERT(hdr
->flags
& RXRPC_CLIENT_INITIATED
);
620 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
622 /* search the connection list first */
623 read_lock_bh(&trans
->conn_lock
);
625 p
= trans
->server_conns
.rb_node
;
627 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
629 _debug("maybe %x", conn
->real_conn_id
);
631 if (epoch
< conn
->epoch
)
633 else if (epoch
> conn
->epoch
)
635 else if (conn_id
< conn
->real_conn_id
)
637 else if (conn_id
> conn
->real_conn_id
)
640 goto found_extant_connection
;
642 read_unlock_bh(&trans
->conn_lock
);
644 /* not yet present - create a candidate for a new record and then
646 candidate
= rxrpc_alloc_connection(gfp
);
648 _leave(" = -ENOMEM");
649 return ERR_PTR(-ENOMEM
);
652 candidate
->trans
= trans
;
653 candidate
->epoch
= hdr
->epoch
;
654 candidate
->cid
= hdr
->cid
& __constant_cpu_to_be32(RXRPC_CIDMASK
);
655 candidate
->service_id
= hdr
->serviceId
;
656 candidate
->security_ix
= hdr
->securityIndex
;
657 candidate
->in_clientflag
= RXRPC_CLIENT_INITIATED
;
658 candidate
->out_clientflag
= 0;
659 candidate
->real_conn_id
= conn_id
;
660 candidate
->state
= RXRPC_CONN_SERVER
;
661 if (candidate
->service_id
)
662 candidate
->state
= RXRPC_CONN_SERVER_UNSECURED
;
664 write_lock_bh(&trans
->conn_lock
);
666 pp
= &trans
->server_conns
.rb_node
;
670 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
672 if (epoch
< conn
->epoch
)
673 pp
= &(*pp
)->rb_left
;
674 else if (epoch
> conn
->epoch
)
675 pp
= &(*pp
)->rb_right
;
676 else if (conn_id
< conn
->real_conn_id
)
677 pp
= &(*pp
)->rb_left
;
678 else if (conn_id
> conn
->real_conn_id
)
679 pp
= &(*pp
)->rb_right
;
681 goto found_extant_second
;
684 /* we can now add the new candidate to the list */
687 rb_link_node(&conn
->node
, p
, pp
);
688 rb_insert_color(&conn
->node
, &trans
->server_conns
);
689 atomic_inc(&conn
->trans
->usage
);
691 write_unlock_bh(&trans
->conn_lock
);
693 write_lock_bh(&rxrpc_connection_lock
);
694 list_add_tail(&conn
->link
, &rxrpc_connections
);
695 write_unlock_bh(&rxrpc_connection_lock
);
700 _net("CONNECTION %s %d {%x}", new, conn
->debug_id
, conn
->real_conn_id
);
702 _leave(" = %p {u=%d}", conn
, atomic_read(&conn
->usage
));
705 /* we found the connection in the list immediately */
706 found_extant_connection
:
707 if (hdr
->securityIndex
!= conn
->security_ix
) {
708 read_unlock_bh(&trans
->conn_lock
);
709 goto security_mismatch
;
711 atomic_inc(&conn
->usage
);
712 read_unlock_bh(&trans
->conn_lock
);
715 /* we found the connection on the second time through the list */
717 if (hdr
->securityIndex
!= conn
->security_ix
) {
718 write_unlock_bh(&trans
->conn_lock
);
719 goto security_mismatch
;
721 atomic_inc(&conn
->usage
);
722 write_unlock_bh(&trans
->conn_lock
);
728 _leave(" = -EKEYREJECTED");
729 return ERR_PTR(-EKEYREJECTED
);
733 * find a connection based on transport and RxRPC connection ID for an incoming
736 struct rxrpc_connection
*rxrpc_find_connection(struct rxrpc_transport
*trans
,
737 struct rxrpc_header
*hdr
)
739 struct rxrpc_connection
*conn
;
744 _enter(",{%x,%x}", ntohl(hdr
->cid
), hdr
->flags
);
746 read_lock_bh(&trans
->conn_lock
);
748 conn_id
= ntohl(hdr
->cid
) & RXRPC_CIDMASK
;
751 if (hdr
->flags
& RXRPC_CLIENT_INITIATED
)
752 p
= trans
->server_conns
.rb_node
;
754 p
= trans
->client_conns
.rb_node
;
757 conn
= rb_entry(p
, struct rxrpc_connection
, node
);
759 _debug("maybe %x", conn
->real_conn_id
);
761 if (epoch
< conn
->epoch
)
763 else if (epoch
> conn
->epoch
)
765 else if (conn_id
< conn
->real_conn_id
)
767 else if (conn_id
> conn
->real_conn_id
)
773 read_unlock_bh(&trans
->conn_lock
);
778 atomic_inc(&conn
->usage
);
779 read_unlock_bh(&trans
->conn_lock
);
780 _leave(" = %p", conn
);
785 * release a virtual connection
787 void rxrpc_put_connection(struct rxrpc_connection
*conn
)
789 _enter("%p{u=%d,d=%d}",
790 conn
, atomic_read(&conn
->usage
), conn
->debug_id
);
792 ASSERTCMP(atomic_read(&conn
->usage
), >, 0);
794 conn
->put_time
= xtime
.tv_sec
;
795 if (atomic_dec_and_test(&conn
->usage
)) {
797 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);
804 * destroy a virtual connection
806 static void rxrpc_destroy_connection(struct rxrpc_connection
*conn
)
808 _enter("%p{%d}", conn
, atomic_read(&conn
->usage
));
810 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
812 _net("DESTROY CONN %d", conn
->debug_id
);
815 rxrpc_put_bundle(conn
->trans
, conn
->bundle
);
817 ASSERT(RB_EMPTY_ROOT(&conn
->calls
));
818 rxrpc_purge_queue(&conn
->rx_queue
);
820 rxrpc_clear_conn_security(conn
);
821 rxrpc_put_transport(conn
->trans
);
827 * reap dead connections
829 void rxrpc_connection_reaper(struct work_struct
*work
)
831 struct rxrpc_connection
*conn
, *_p
;
832 unsigned long now
, earliest
, reap_time
;
834 LIST_HEAD(graveyard
);
839 earliest
= ULONG_MAX
;
841 write_lock_bh(&rxrpc_connection_lock
);
842 list_for_each_entry_safe(conn
, _p
, &rxrpc_connections
, link
) {
843 _debug("reap CONN %d { u=%d,t=%ld }",
844 conn
->debug_id
, atomic_read(&conn
->usage
),
845 (long) now
- (long) conn
->put_time
);
847 if (likely(atomic_read(&conn
->usage
) > 0))
850 spin_lock(&conn
->trans
->client_lock
);
851 write_lock(&conn
->trans
->conn_lock
);
852 reap_time
= conn
->put_time
+ rxrpc_connection_timeout
;
854 if (atomic_read(&conn
->usage
) > 0) {
856 } else if (reap_time
<= now
) {
857 list_move_tail(&conn
->link
, &graveyard
);
858 if (conn
->out_clientflag
)
859 rb_erase(&conn
->node
,
860 &conn
->trans
->client_conns
);
862 rb_erase(&conn
->node
,
863 &conn
->trans
->server_conns
);
865 list_del_init(&conn
->bundle_link
);
866 conn
->bundle
->num_conns
--;
869 } else if (reap_time
< earliest
) {
870 earliest
= reap_time
;
873 write_unlock(&conn
->trans
->conn_lock
);
874 spin_unlock(&conn
->trans
->client_lock
);
876 write_unlock_bh(&rxrpc_connection_lock
);
878 if (earliest
!= ULONG_MAX
) {
879 _debug("reschedule reaper %ld", (long) earliest
- now
);
880 ASSERTCMP(earliest
, >, now
);
881 rxrpc_queue_delayed_work(&rxrpc_connection_reap
,
882 (earliest
- now
) * HZ
);
885 /* then destroy all those pulled out */
886 while (!list_empty(&graveyard
)) {
887 conn
= list_entry(graveyard
.next
, struct rxrpc_connection
,
889 list_del_init(&conn
->link
);
891 ASSERTCMP(atomic_read(&conn
->usage
), ==, 0);
892 rxrpc_destroy_connection(conn
);
899 * preemptively destroy all the connection records rather than waiting for them
902 void __exit
rxrpc_destroy_all_connections(void)
906 rxrpc_connection_timeout
= 0;
907 cancel_delayed_work(&rxrpc_connection_reap
);
908 rxrpc_queue_delayed_work(&rxrpc_connection_reap
, 0);