1 /* RxRPC individual remote procedure call handling
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/slab.h>
13 #include <linux/module.h>
14 #include <linux/circ_buf.h>
15 #include <linux/hashtable.h>
16 #include <linux/spinlock_types.h>
18 #include <net/af_rxrpc.h>
19 #include "ar-internal.h"
22 * Maximum lifetime of a call (in jiffies).
24 unsigned rxrpc_max_call_lifetime
= 60 * HZ
;
27 * Time till dead call expires after last use (in jiffies).
29 unsigned rxrpc_dead_call_expiry
= 2 * HZ
;
31 const char *const rxrpc_call_states
[] = {
32 [RXRPC_CALL_CLIENT_SEND_REQUEST
] = "ClSndReq",
33 [RXRPC_CALL_CLIENT_AWAIT_REPLY
] = "ClAwtRpl",
34 [RXRPC_CALL_CLIENT_RECV_REPLY
] = "ClRcvRpl",
35 [RXRPC_CALL_CLIENT_FINAL_ACK
] = "ClFnlACK",
36 [RXRPC_CALL_SERVER_SECURING
] = "SvSecure",
37 [RXRPC_CALL_SERVER_ACCEPTING
] = "SvAccept",
38 [RXRPC_CALL_SERVER_RECV_REQUEST
] = "SvRcvReq",
39 [RXRPC_CALL_SERVER_ACK_REQUEST
] = "SvAckReq",
40 [RXRPC_CALL_SERVER_SEND_REPLY
] = "SvSndRpl",
41 [RXRPC_CALL_SERVER_AWAIT_ACK
] = "SvAwtACK",
42 [RXRPC_CALL_COMPLETE
] = "Complete",
43 [RXRPC_CALL_SERVER_BUSY
] = "SvBusy ",
44 [RXRPC_CALL_REMOTELY_ABORTED
] = "RmtAbort",
45 [RXRPC_CALL_LOCALLY_ABORTED
] = "LocAbort",
46 [RXRPC_CALL_NETWORK_ERROR
] = "NetError",
47 [RXRPC_CALL_DEAD
] = "Dead ",
50 struct kmem_cache
*rxrpc_call_jar
;
51 LIST_HEAD(rxrpc_calls
);
52 DEFINE_RWLOCK(rxrpc_call_lock
);
54 static void rxrpc_destroy_call(struct work_struct
*work
);
55 static void rxrpc_call_life_expired(unsigned long _call
);
56 static void rxrpc_dead_call_expired(unsigned long _call
);
57 static void rxrpc_ack_time_expired(unsigned long _call
);
58 static void rxrpc_resend_time_expired(unsigned long _call
);
60 static DEFINE_SPINLOCK(rxrpc_call_hash_lock
);
61 static DEFINE_HASHTABLE(rxrpc_call_hash
, 10);
64 * Hash function for rxrpc_call_hash
66 static unsigned long rxrpc_call_hashfunc(
74 unsigned int addr_size
,
80 u32 hcid
= ntohl(cid
);
84 key
= (unsigned long)localptr
;
85 /* We just want to add up the __be32 values, so forcing the
86 * cast should be okay.
88 key
+= (__force u32
)epoch
;
89 key
+= (__force u16
)service_id
;
90 key
+= (__force u32
)call_id
;
91 key
+= (hcid
& RXRPC_CIDMASK
) >> RXRPC_CIDSHIFT
;
92 key
+= hcid
& RXRPC_CHANNELMASK
;
95 /* Step through the peer address in 16-bit portions for speed */
96 for (i
= 0, p
= (const u16
*)peer_addr
; i
< addr_size
>> 1; i
++, p
++)
98 _leave(" key = 0x%lx", key
);
103 * Add a call to the hashtable
105 static void rxrpc_call_hash_add(struct rxrpc_call
*call
)
108 unsigned int addr_size
= 0;
111 switch (call
->proto
) {
113 addr_size
= sizeof(call
->peer_ip
.ipv4_addr
);
116 addr_size
= sizeof(call
->peer_ip
.ipv6_addr
);
121 key
= rxrpc_call_hashfunc(call
->in_clientflag
, call
->cid
,
122 call
->call_id
, call
->epoch
,
123 call
->service_id
, call
->proto
,
124 call
->conn
->trans
->local
, addr_size
,
125 call
->peer_ip
.ipv6_addr
);
126 /* Store the full key in the call */
127 call
->hash_key
= key
;
128 spin_lock(&rxrpc_call_hash_lock
);
129 hash_add_rcu(rxrpc_call_hash
, &call
->hash_node
, key
);
130 spin_unlock(&rxrpc_call_hash_lock
);
135 * Remove a call from the hashtable
137 static void rxrpc_call_hash_del(struct rxrpc_call
*call
)
140 spin_lock(&rxrpc_call_hash_lock
);
141 hash_del_rcu(&call
->hash_node
);
142 spin_unlock(&rxrpc_call_hash_lock
);
147 * Find a call in the hashtable and return it, or NULL if it
150 struct rxrpc_call
*rxrpc_find_call_hash(
161 unsigned int addr_size
= 0;
162 struct rxrpc_call
*call
= NULL
;
163 struct rxrpc_call
*ret
= NULL
;
168 addr_size
= sizeof(call
->peer_ip
.ipv4_addr
);
171 addr_size
= sizeof(call
->peer_ip
.ipv6_addr
);
177 key
= rxrpc_call_hashfunc(clientflag
, cid
, call_id
, epoch
,
178 service_id
, proto
, localptr
, addr_size
,
180 hash_for_each_possible_rcu(rxrpc_call_hash
, call
, hash_node
, key
) {
181 if (call
->hash_key
== key
&&
182 call
->call_id
== call_id
&&
184 call
->in_clientflag
== clientflag
&&
185 call
->service_id
== service_id
&&
186 call
->proto
== proto
&&
187 call
->local
== localptr
&&
188 memcmp(call
->peer_ip
.ipv6_addr
, peer_addr
,
190 call
->epoch
== epoch
) {
195 _leave(" = %p", ret
);
200 * allocate a new call
202 static struct rxrpc_call
*rxrpc_alloc_call(gfp_t gfp
)
204 struct rxrpc_call
*call
;
206 call
= kmem_cache_zalloc(rxrpc_call_jar
, gfp
);
210 call
->acks_winsz
= 16;
211 call
->acks_window
= kmalloc(call
->acks_winsz
* sizeof(unsigned long),
213 if (!call
->acks_window
) {
214 kmem_cache_free(rxrpc_call_jar
, call
);
218 setup_timer(&call
->lifetimer
, &rxrpc_call_life_expired
,
219 (unsigned long) call
);
220 setup_timer(&call
->deadspan
, &rxrpc_dead_call_expired
,
221 (unsigned long) call
);
222 setup_timer(&call
->ack_timer
, &rxrpc_ack_time_expired
,
223 (unsigned long) call
);
224 setup_timer(&call
->resend_timer
, &rxrpc_resend_time_expired
,
225 (unsigned long) call
);
226 INIT_WORK(&call
->destroyer
, &rxrpc_destroy_call
);
227 INIT_WORK(&call
->processor
, &rxrpc_process_call
);
228 INIT_LIST_HEAD(&call
->accept_link
);
229 skb_queue_head_init(&call
->rx_queue
);
230 skb_queue_head_init(&call
->rx_oos_queue
);
231 init_waitqueue_head(&call
->tx_waitq
);
232 spin_lock_init(&call
->lock
);
233 rwlock_init(&call
->state_lock
);
234 atomic_set(&call
->usage
, 1);
235 call
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
236 call
->state
= RXRPC_CALL_CLIENT_SEND_REQUEST
;
238 memset(&call
->sock_node
, 0xed, sizeof(call
->sock_node
));
240 call
->rx_data_expect
= 1;
241 call
->rx_data_eaten
= 0;
242 call
->rx_first_oos
= 0;
243 call
->ackr_win_top
= call
->rx_data_eaten
+ 1 + rxrpc_rx_window_size
;
244 call
->creation_jif
= jiffies
;
249 * allocate a new client call and attempt to get a connection slot for it
251 static struct rxrpc_call
*rxrpc_alloc_client_call(
252 struct rxrpc_sock
*rx
,
253 struct rxrpc_transport
*trans
,
254 struct rxrpc_conn_bundle
*bundle
,
257 struct rxrpc_call
*call
;
263 ASSERT(trans
!= NULL
);
264 ASSERT(bundle
!= NULL
);
266 call
= rxrpc_alloc_call(gfp
);
268 return ERR_PTR(-ENOMEM
);
272 call
->rx_data_post
= 1;
274 ret
= rxrpc_connect_call(rx
, trans
, bundle
, call
, gfp
);
276 kmem_cache_free(rxrpc_call_jar
, call
);
280 /* Record copies of information for hashtable lookup */
281 call
->proto
= rx
->proto
;
282 call
->local
= trans
->local
;
283 switch (call
->proto
) {
285 call
->peer_ip
.ipv4_addr
=
286 trans
->peer
->srx
.transport
.sin
.sin_addr
.s_addr
;
289 memcpy(call
->peer_ip
.ipv6_addr
,
290 trans
->peer
->srx
.transport
.sin6
.sin6_addr
.in6_u
.u6_addr8
,
291 sizeof(call
->peer_ip
.ipv6_addr
));
294 call
->epoch
= call
->conn
->epoch
;
295 call
->service_id
= call
->conn
->service_id
;
296 call
->in_clientflag
= call
->conn
->in_clientflag
;
297 /* Add the new call to the hashtable */
298 rxrpc_call_hash_add(call
);
300 spin_lock(&call
->conn
->trans
->peer
->lock
);
301 list_add(&call
->error_link
, &call
->conn
->trans
->peer
->error_targets
);
302 spin_unlock(&call
->conn
->trans
->peer
->lock
);
304 call
->lifetimer
.expires
= jiffies
+ rxrpc_max_call_lifetime
;
305 add_timer(&call
->lifetimer
);
307 _leave(" = %p", call
);
312 * set up a call for the given data
313 * - called in process context with IRQs enabled
315 struct rxrpc_call
*rxrpc_get_client_call(struct rxrpc_sock
*rx
,
316 struct rxrpc_transport
*trans
,
317 struct rxrpc_conn_bundle
*bundle
,
318 unsigned long user_call_ID
,
322 struct rxrpc_call
*call
, *candidate
;
323 struct rb_node
*p
, *parent
, **pp
;
325 _enter("%p,%d,%d,%lx,%d",
326 rx
, trans
? trans
->debug_id
: -1, bundle
? bundle
->debug_id
: -1,
327 user_call_ID
, create
);
329 /* search the extant calls first for one that matches the specified
331 read_lock(&rx
->call_lock
);
333 p
= rx
->calls
.rb_node
;
335 call
= rb_entry(p
, struct rxrpc_call
, sock_node
);
337 if (user_call_ID
< call
->user_call_ID
)
339 else if (user_call_ID
> call
->user_call_ID
)
342 goto found_extant_call
;
345 read_unlock(&rx
->call_lock
);
347 if (!create
|| !trans
)
348 return ERR_PTR(-EBADSLT
);
350 /* not yet present - create a candidate for a new record and then
352 candidate
= rxrpc_alloc_client_call(rx
, trans
, bundle
, gfp
);
353 if (IS_ERR(candidate
)) {
354 _leave(" = %ld", PTR_ERR(candidate
));
358 candidate
->user_call_ID
= user_call_ID
;
359 __set_bit(RXRPC_CALL_HAS_USERID
, &candidate
->flags
);
361 write_lock(&rx
->call_lock
);
363 pp
= &rx
->calls
.rb_node
;
367 call
= rb_entry(parent
, struct rxrpc_call
, sock_node
);
369 if (user_call_ID
< call
->user_call_ID
)
370 pp
= &(*pp
)->rb_left
;
371 else if (user_call_ID
> call
->user_call_ID
)
372 pp
= &(*pp
)->rb_right
;
374 goto found_extant_second
;
377 /* second search also failed; add the new call */
380 rxrpc_get_call(call
);
382 rb_link_node(&call
->sock_node
, parent
, pp
);
383 rb_insert_color(&call
->sock_node
, &rx
->calls
);
384 write_unlock(&rx
->call_lock
);
386 write_lock_bh(&rxrpc_call_lock
);
387 list_add_tail(&call
->link
, &rxrpc_calls
);
388 write_unlock_bh(&rxrpc_call_lock
);
390 _net("CALL new %d on CONN %d", call
->debug_id
, call
->conn
->debug_id
);
392 _leave(" = %p [new]", call
);
395 /* we found the call in the list immediately */
397 rxrpc_get_call(call
);
398 read_unlock(&rx
->call_lock
);
399 _leave(" = %p [extant %d]", call
, atomic_read(&call
->usage
));
402 /* we found the call on the second time through the list */
404 rxrpc_get_call(call
);
405 write_unlock(&rx
->call_lock
);
406 rxrpc_put_call(candidate
);
407 _leave(" = %p [second %d]", call
, atomic_read(&call
->usage
));
412 * set up an incoming call
413 * - called in process context with IRQs enabled
415 struct rxrpc_call
*rxrpc_incoming_call(struct rxrpc_sock
*rx
,
416 struct rxrpc_connection
*conn
,
417 struct rxrpc_header
*hdr
,
420 struct rxrpc_call
*call
, *candidate
;
421 struct rb_node
**p
, *parent
;
424 _enter(",%d,,%x", conn
->debug_id
, gfp
);
428 candidate
= rxrpc_alloc_call(gfp
);
430 return ERR_PTR(-EBUSY
);
432 candidate
->socket
= rx
;
433 candidate
->conn
= conn
;
434 candidate
->cid
= hdr
->cid
;
435 candidate
->call_id
= hdr
->callNumber
;
436 candidate
->channel
= ntohl(hdr
->cid
) & RXRPC_CHANNELMASK
;
437 candidate
->rx_data_post
= 0;
438 candidate
->state
= RXRPC_CALL_SERVER_ACCEPTING
;
439 if (conn
->security_ix
> 0)
440 candidate
->state
= RXRPC_CALL_SERVER_SECURING
;
442 write_lock_bh(&conn
->lock
);
444 /* set the channel for this call */
445 call
= conn
->channels
[candidate
->channel
];
446 _debug("channel[%u] is %p", candidate
->channel
, call
);
447 if (call
&& call
->call_id
== hdr
->callNumber
) {
448 /* already set; must've been a duplicate packet */
449 _debug("extant call [%d]", call
->state
);
450 ASSERTCMP(call
->conn
, ==, conn
);
452 read_lock(&call
->state_lock
);
453 switch (call
->state
) {
454 case RXRPC_CALL_LOCALLY_ABORTED
:
455 if (!test_and_set_bit(RXRPC_CALL_ABORT
, &call
->events
))
456 rxrpc_queue_call(call
);
457 case RXRPC_CALL_REMOTELY_ABORTED
:
458 read_unlock(&call
->state_lock
);
461 rxrpc_get_call(call
);
462 read_unlock(&call
->state_lock
);
468 /* it seems the channel is still in use from the previous call
469 * - ditch the old binding if its call is now complete */
470 _debug("CALL: %u { %s }",
471 call
->debug_id
, rxrpc_call_states
[call
->state
]);
473 if (call
->state
>= RXRPC_CALL_COMPLETE
) {
474 conn
->channels
[call
->channel
] = NULL
;
476 write_unlock_bh(&conn
->lock
);
477 kmem_cache_free(rxrpc_call_jar
, candidate
);
479 return ERR_PTR(-EBUSY
);
483 /* check the call number isn't duplicate */
485 call_id
= hdr
->callNumber
;
486 p
= &conn
->calls
.rb_node
;
490 call
= rb_entry(parent
, struct rxrpc_call
, conn_node
);
492 /* The tree is sorted in order of the __be32 value without
493 * turning it into host order.
495 if ((__force u32
)call_id
< (__force u32
)call
->call_id
)
497 else if ((__force u32
)call_id
> (__force u32
)call
->call_id
)
503 /* make the call available */
507 rb_link_node(&call
->conn_node
, parent
, p
);
508 rb_insert_color(&call
->conn_node
, &conn
->calls
);
509 conn
->channels
[call
->channel
] = call
;
511 atomic_inc(&conn
->usage
);
512 write_unlock_bh(&conn
->lock
);
514 spin_lock(&conn
->trans
->peer
->lock
);
515 list_add(&call
->error_link
, &conn
->trans
->peer
->error_targets
);
516 spin_unlock(&conn
->trans
->peer
->lock
);
518 write_lock_bh(&rxrpc_call_lock
);
519 list_add_tail(&call
->link
, &rxrpc_calls
);
520 write_unlock_bh(&rxrpc_call_lock
);
522 /* Record copies of information for hashtable lookup */
523 call
->proto
= rx
->proto
;
524 call
->local
= conn
->trans
->local
;
525 switch (call
->proto
) {
527 call
->peer_ip
.ipv4_addr
=
528 conn
->trans
->peer
->srx
.transport
.sin
.sin_addr
.s_addr
;
531 memcpy(call
->peer_ip
.ipv6_addr
,
532 conn
->trans
->peer
->srx
.transport
.sin6
.sin6_addr
.in6_u
.u6_addr8
,
533 sizeof(call
->peer_ip
.ipv6_addr
));
538 call
->epoch
= conn
->epoch
;
539 call
->service_id
= conn
->service_id
;
540 call
->in_clientflag
= conn
->in_clientflag
;
541 /* Add the new call to the hashtable */
542 rxrpc_call_hash_add(call
);
544 _net("CALL incoming %d on CONN %d", call
->debug_id
, call
->conn
->debug_id
);
546 call
->lifetimer
.expires
= jiffies
+ rxrpc_max_call_lifetime
;
547 add_timer(&call
->lifetimer
);
548 _leave(" = %p {%d} [new]", call
, call
->debug_id
);
552 write_unlock_bh(&conn
->lock
);
553 kmem_cache_free(rxrpc_call_jar
, candidate
);
554 _leave(" = %p {%d} [extant]", call
, call
? call
->debug_id
: -1);
558 write_unlock_bh(&conn
->lock
);
559 kmem_cache_free(rxrpc_call_jar
, candidate
);
560 _leave(" = -ECONNABORTED");
561 return ERR_PTR(-ECONNABORTED
);
564 write_unlock_bh(&conn
->lock
);
565 kmem_cache_free(rxrpc_call_jar
, candidate
);
566 _leave(" = -ECONNRESET [old]");
567 return ERR_PTR(-ECONNRESET
);
571 * find an extant server call
572 * - called in process context with IRQs enabled
574 struct rxrpc_call
*rxrpc_find_server_call(struct rxrpc_sock
*rx
,
575 unsigned long user_call_ID
)
577 struct rxrpc_call
*call
;
580 _enter("%p,%lx", rx
, user_call_ID
);
582 /* search the extant calls for one that matches the specified user
584 read_lock(&rx
->call_lock
);
586 p
= rx
->calls
.rb_node
;
588 call
= rb_entry(p
, struct rxrpc_call
, sock_node
);
590 if (user_call_ID
< call
->user_call_ID
)
592 else if (user_call_ID
> call
->user_call_ID
)
595 goto found_extant_call
;
598 read_unlock(&rx
->call_lock
);
602 /* we found the call in the list immediately */
604 rxrpc_get_call(call
);
605 read_unlock(&rx
->call_lock
);
606 _leave(" = %p [%d]", call
, atomic_read(&call
->usage
));
611 * detach a call from a socket and set up for release
613 void rxrpc_release_call(struct rxrpc_call
*call
)
615 struct rxrpc_connection
*conn
= call
->conn
;
616 struct rxrpc_sock
*rx
= call
->socket
;
618 _enter("{%d,%d,%d,%d}",
619 call
->debug_id
, atomic_read(&call
->usage
),
620 atomic_read(&call
->ackr_not_idle
),
623 spin_lock_bh(&call
->lock
);
624 if (test_and_set_bit(RXRPC_CALL_RELEASED
, &call
->flags
))
626 spin_unlock_bh(&call
->lock
);
628 /* dissociate from the socket
629 * - the socket's ref on the call is passed to the death timer
631 _debug("RELEASE CALL %p (%d CONN %p)", call
, call
->debug_id
, conn
);
633 write_lock_bh(&rx
->call_lock
);
634 if (!list_empty(&call
->accept_link
)) {
635 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
636 call
, call
->events
, call
->flags
);
637 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
));
638 list_del_init(&call
->accept_link
);
639 sk_acceptq_removed(&rx
->sk
);
640 } else if (test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
)) {
641 rb_erase(&call
->sock_node
, &rx
->calls
);
642 memset(&call
->sock_node
, 0xdd, sizeof(call
->sock_node
));
643 clear_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
);
645 write_unlock_bh(&rx
->call_lock
);
647 /* free up the channel for reuse */
648 spin_lock(&conn
->trans
->client_lock
);
649 write_lock_bh(&conn
->lock
);
650 write_lock(&call
->state_lock
);
652 if (conn
->channels
[call
->channel
] == call
)
653 conn
->channels
[call
->channel
] = NULL
;
655 if (conn
->out_clientflag
&& conn
->bundle
) {
657 switch (conn
->avail_calls
) {
659 list_move_tail(&conn
->bundle_link
,
660 &conn
->bundle
->avail_conns
);
661 case 2 ... RXRPC_MAXCALLS
- 1:
662 ASSERT(conn
->channels
[0] == NULL
||
663 conn
->channels
[1] == NULL
||
664 conn
->channels
[2] == NULL
||
665 conn
->channels
[3] == NULL
);
668 list_move_tail(&conn
->bundle_link
,
669 &conn
->bundle
->unused_conns
);
670 ASSERT(conn
->channels
[0] == NULL
&&
671 conn
->channels
[1] == NULL
&&
672 conn
->channels
[2] == NULL
&&
673 conn
->channels
[3] == NULL
);
676 printk(KERN_ERR
"RxRPC: conn->avail_calls=%d\n",
682 spin_unlock(&conn
->trans
->client_lock
);
684 if (call
->state
< RXRPC_CALL_COMPLETE
&&
685 call
->state
!= RXRPC_CALL_CLIENT_FINAL_ACK
) {
686 _debug("+++ ABORTING STATE %d +++\n", call
->state
);
687 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
688 call
->abort_code
= RX_CALL_DEAD
;
689 set_bit(RXRPC_CALL_ABORT
, &call
->events
);
690 rxrpc_queue_call(call
);
692 write_unlock(&call
->state_lock
);
693 write_unlock_bh(&conn
->lock
);
695 /* clean up the Rx queue */
696 if (!skb_queue_empty(&call
->rx_queue
) ||
697 !skb_queue_empty(&call
->rx_oos_queue
)) {
698 struct rxrpc_skb_priv
*sp
;
701 _debug("purge Rx queues");
703 spin_lock_bh(&call
->lock
);
704 while ((skb
= skb_dequeue(&call
->rx_queue
)) ||
705 (skb
= skb_dequeue(&call
->rx_oos_queue
))) {
708 ASSERTCMP(sp
->call
, ==, call
);
709 rxrpc_put_call(call
);
712 skb
->destructor
= NULL
;
713 spin_unlock_bh(&call
->lock
);
715 _debug("- zap %s %%%u #%u",
716 rxrpc_pkts
[sp
->hdr
.type
],
717 ntohl(sp
->hdr
.serial
),
720 spin_lock_bh(&call
->lock
);
722 spin_unlock_bh(&call
->lock
);
724 ASSERTCMP(call
->state
, !=, RXRPC_CALL_COMPLETE
);
727 del_timer_sync(&call
->resend_timer
);
728 del_timer_sync(&call
->ack_timer
);
729 del_timer_sync(&call
->lifetimer
);
730 call
->deadspan
.expires
= jiffies
+ rxrpc_dead_call_expiry
;
731 add_timer(&call
->deadspan
);
737 * handle a dead call being ready for reaping
739 static void rxrpc_dead_call_expired(unsigned long _call
)
741 struct rxrpc_call
*call
= (struct rxrpc_call
*) _call
;
743 _enter("{%d}", call
->debug_id
);
745 write_lock_bh(&call
->state_lock
);
746 call
->state
= RXRPC_CALL_DEAD
;
747 write_unlock_bh(&call
->state_lock
);
748 rxrpc_put_call(call
);
752 * mark a call as to be released, aborting it if it's still in progress
753 * - called with softirqs disabled
755 static void rxrpc_mark_call_released(struct rxrpc_call
*call
)
759 write_lock(&call
->state_lock
);
760 if (call
->state
< RXRPC_CALL_DEAD
) {
762 if (call
->state
< RXRPC_CALL_COMPLETE
) {
763 _debug("abort call %p", call
);
764 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
765 call
->abort_code
= RX_CALL_DEAD
;
766 if (!test_and_set_bit(RXRPC_CALL_ABORT
, &call
->events
))
769 if (!test_and_set_bit(RXRPC_CALL_RELEASE
, &call
->events
))
772 rxrpc_queue_call(call
);
774 write_unlock(&call
->state_lock
);
778 * release all the calls associated with a socket
780 void rxrpc_release_calls_on_socket(struct rxrpc_sock
*rx
)
782 struct rxrpc_call
*call
;
787 read_lock_bh(&rx
->call_lock
);
789 /* mark all the calls as no longer wanting incoming packets */
790 for (p
= rb_first(&rx
->calls
); p
; p
= rb_next(p
)) {
791 call
= rb_entry(p
, struct rxrpc_call
, sock_node
);
792 rxrpc_mark_call_released(call
);
795 /* kill the not-yet-accepted incoming calls */
796 list_for_each_entry(call
, &rx
->secureq
, accept_link
) {
797 rxrpc_mark_call_released(call
);
800 list_for_each_entry(call
, &rx
->acceptq
, accept_link
) {
801 rxrpc_mark_call_released(call
);
804 read_unlock_bh(&rx
->call_lock
);
811 void __rxrpc_put_call(struct rxrpc_call
*call
)
813 ASSERT(call
!= NULL
);
815 _enter("%p{u=%d}", call
, atomic_read(&call
->usage
));
817 ASSERTCMP(atomic_read(&call
->usage
), >, 0);
819 if (atomic_dec_and_test(&call
->usage
)) {
820 _debug("call %d dead", call
->debug_id
);
821 ASSERTCMP(call
->state
, ==, RXRPC_CALL_DEAD
);
822 rxrpc_queue_work(&call
->destroyer
);
830 static void rxrpc_cleanup_call(struct rxrpc_call
*call
)
832 _net("DESTROY CALL %d", call
->debug_id
);
834 ASSERT(call
->socket
);
836 memset(&call
->sock_node
, 0xcd, sizeof(call
->sock_node
));
838 del_timer_sync(&call
->lifetimer
);
839 del_timer_sync(&call
->deadspan
);
840 del_timer_sync(&call
->ack_timer
);
841 del_timer_sync(&call
->resend_timer
);
843 ASSERT(test_bit(RXRPC_CALL_RELEASED
, &call
->flags
));
844 ASSERTCMP(call
->events
, ==, 0);
845 if (work_pending(&call
->processor
)) {
846 _debug("defer destroy");
847 rxrpc_queue_work(&call
->destroyer
);
852 spin_lock(&call
->conn
->trans
->peer
->lock
);
853 list_del(&call
->error_link
);
854 spin_unlock(&call
->conn
->trans
->peer
->lock
);
856 write_lock_bh(&call
->conn
->lock
);
857 rb_erase(&call
->conn_node
, &call
->conn
->calls
);
858 write_unlock_bh(&call
->conn
->lock
);
859 rxrpc_put_connection(call
->conn
);
862 /* Remove the call from the hash */
863 rxrpc_call_hash_del(call
);
865 if (call
->acks_window
) {
866 _debug("kill Tx window %d",
867 CIRC_CNT(call
->acks_head
, call
->acks_tail
,
870 while (CIRC_CNT(call
->acks_head
, call
->acks_tail
,
871 call
->acks_winsz
) > 0) {
872 struct rxrpc_skb_priv
*sp
;
875 _skb
= call
->acks_window
[call
->acks_tail
] & ~1;
876 sp
= rxrpc_skb((struct sk_buff
*) _skb
);
877 _debug("+++ clear Tx %u", ntohl(sp
->hdr
.seq
));
878 rxrpc_free_skb((struct sk_buff
*) _skb
);
880 (call
->acks_tail
+ 1) & (call
->acks_winsz
- 1);
883 kfree(call
->acks_window
);
886 rxrpc_free_skb(call
->tx_pending
);
888 rxrpc_purge_queue(&call
->rx_queue
);
889 ASSERT(skb_queue_empty(&call
->rx_oos_queue
));
890 sock_put(&call
->socket
->sk
);
891 kmem_cache_free(rxrpc_call_jar
, call
);
897 static void rxrpc_destroy_call(struct work_struct
*work
)
899 struct rxrpc_call
*call
=
900 container_of(work
, struct rxrpc_call
, destroyer
);
902 _enter("%p{%d,%d,%p}",
903 call
, atomic_read(&call
->usage
), call
->channel
, call
->conn
);
905 ASSERTCMP(call
->state
, ==, RXRPC_CALL_DEAD
);
907 write_lock_bh(&rxrpc_call_lock
);
908 list_del_init(&call
->link
);
909 write_unlock_bh(&rxrpc_call_lock
);
911 rxrpc_cleanup_call(call
);
916 * preemptively destroy all the call records from a transport endpoint rather
917 * than waiting for them to time out
919 void __exit
rxrpc_destroy_all_calls(void)
921 struct rxrpc_call
*call
;
924 write_lock_bh(&rxrpc_call_lock
);
926 while (!list_empty(&rxrpc_calls
)) {
927 call
= list_entry(rxrpc_calls
.next
, struct rxrpc_call
, link
);
928 _debug("Zapping call %p", call
);
930 list_del_init(&call
->link
);
932 switch (atomic_read(&call
->usage
)) {
934 ASSERTCMP(call
->state
, ==, RXRPC_CALL_DEAD
);
937 if (del_timer_sync(&call
->deadspan
) != 0 &&
938 call
->state
!= RXRPC_CALL_DEAD
)
939 rxrpc_dead_call_expired((unsigned long) call
);
940 if (call
->state
!= RXRPC_CALL_DEAD
)
943 printk(KERN_ERR
"RXRPC:"
944 " Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
945 call
, atomic_read(&call
->usage
),
946 atomic_read(&call
->ackr_not_idle
),
947 rxrpc_call_states
[call
->state
],
948 call
->flags
, call
->events
);
949 if (!skb_queue_empty(&call
->rx_queue
))
950 printk(KERN_ERR
"RXRPC: Rx queue occupied\n");
951 if (!skb_queue_empty(&call
->rx_oos_queue
))
952 printk(KERN_ERR
"RXRPC: OOS queue occupied\n");
956 write_unlock_bh(&rxrpc_call_lock
);
958 write_lock_bh(&rxrpc_call_lock
);
961 write_unlock_bh(&rxrpc_call_lock
);
966 * handle call lifetime being exceeded
968 static void rxrpc_call_life_expired(unsigned long _call
)
970 struct rxrpc_call
*call
= (struct rxrpc_call
*) _call
;
972 if (call
->state
>= RXRPC_CALL_COMPLETE
)
975 _enter("{%d}", call
->debug_id
);
976 read_lock_bh(&call
->state_lock
);
977 if (call
->state
< RXRPC_CALL_COMPLETE
) {
978 set_bit(RXRPC_CALL_LIFE_TIMER
, &call
->events
);
979 rxrpc_queue_call(call
);
981 read_unlock_bh(&call
->state_lock
);
985 * handle resend timer expiry
986 * - may not take call->state_lock as this can deadlock against del_timer_sync()
988 static void rxrpc_resend_time_expired(unsigned long _call
)
990 struct rxrpc_call
*call
= (struct rxrpc_call
*) _call
;
992 _enter("{%d}", call
->debug_id
);
994 if (call
->state
>= RXRPC_CALL_COMPLETE
)
997 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
998 if (!test_and_set_bit(RXRPC_CALL_RESEND_TIMER
, &call
->events
))
999 rxrpc_queue_call(call
);
1003 * handle ACK timer expiry
1005 static void rxrpc_ack_time_expired(unsigned long _call
)
1007 struct rxrpc_call
*call
= (struct rxrpc_call
*) _call
;
1009 _enter("{%d}", call
->debug_id
);
1011 if (call
->state
>= RXRPC_CALL_COMPLETE
)
1014 read_lock_bh(&call
->state_lock
);
1015 if (call
->state
< RXRPC_CALL_COMPLETE
&&
1016 !test_and_set_bit(RXRPC_CALL_ACK
, &call
->events
))
1017 rxrpc_queue_call(call
);
1018 read_unlock_bh(&call
->state_lock
);