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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/circ_buf.h>
17 #include <linux/spinlock_types.h>
19 #include <net/af_rxrpc.h>
20 #include "ar-internal.h"
22 const char *const rxrpc_call_states
[NR__RXRPC_CALL_STATES
] = {
23 [RXRPC_CALL_UNINITIALISED
] = "Uninit ",
24 [RXRPC_CALL_CLIENT_AWAIT_CONN
] = "ClWtConn",
25 [RXRPC_CALL_CLIENT_SEND_REQUEST
] = "ClSndReq",
26 [RXRPC_CALL_CLIENT_AWAIT_REPLY
] = "ClAwtRpl",
27 [RXRPC_CALL_CLIENT_RECV_REPLY
] = "ClRcvRpl",
28 [RXRPC_CALL_SERVER_PREALLOC
] = "SvPrealc",
29 [RXRPC_CALL_SERVER_SECURING
] = "SvSecure",
30 [RXRPC_CALL_SERVER_ACCEPTING
] = "SvAccept",
31 [RXRPC_CALL_SERVER_RECV_REQUEST
] = "SvRcvReq",
32 [RXRPC_CALL_SERVER_ACK_REQUEST
] = "SvAckReq",
33 [RXRPC_CALL_SERVER_SEND_REPLY
] = "SvSndRpl",
34 [RXRPC_CALL_SERVER_AWAIT_ACK
] = "SvAwtACK",
35 [RXRPC_CALL_COMPLETE
] = "Complete",
38 const char *const rxrpc_call_completions
[NR__RXRPC_CALL_COMPLETIONS
] = {
39 [RXRPC_CALL_SUCCEEDED
] = "Complete",
40 [RXRPC_CALL_REMOTELY_ABORTED
] = "RmtAbort",
41 [RXRPC_CALL_LOCALLY_ABORTED
] = "LocAbort",
42 [RXRPC_CALL_LOCAL_ERROR
] = "LocError",
43 [RXRPC_CALL_NETWORK_ERROR
] = "NetError",
46 struct kmem_cache
*rxrpc_call_jar
;
48 static void rxrpc_call_timer_expired(unsigned long _call
)
50 struct rxrpc_call
*call
= (struct rxrpc_call
*)_call
;
52 _enter("%d", call
->debug_id
);
54 if (call
->state
< RXRPC_CALL_COMPLETE
)
55 rxrpc_set_timer(call
, rxrpc_timer_expired
, ktime_get_real());
59 * find an extant server call
60 * - called in process context with IRQs enabled
62 struct rxrpc_call
*rxrpc_find_call_by_user_ID(struct rxrpc_sock
*rx
,
63 unsigned long user_call_ID
)
65 struct rxrpc_call
*call
;
68 _enter("%p,%lx", rx
, user_call_ID
);
70 read_lock(&rx
->call_lock
);
72 p
= rx
->calls
.rb_node
;
74 call
= rb_entry(p
, struct rxrpc_call
, sock_node
);
76 if (user_call_ID
< call
->user_call_ID
)
78 else if (user_call_ID
> call
->user_call_ID
)
81 goto found_extant_call
;
84 read_unlock(&rx
->call_lock
);
89 rxrpc_get_call(call
, rxrpc_call_got
);
90 read_unlock(&rx
->call_lock
);
91 _leave(" = %p [%d]", call
, atomic_read(&call
->usage
));
98 struct rxrpc_call
*rxrpc_alloc_call(gfp_t gfp
)
100 struct rxrpc_call
*call
;
102 call
= kmem_cache_zalloc(rxrpc_call_jar
, gfp
);
106 call
->rxtx_buffer
= kcalloc(RXRPC_RXTX_BUFF_SIZE
,
107 sizeof(struct sk_buff
*),
109 if (!call
->rxtx_buffer
)
112 call
->rxtx_annotations
= kcalloc(RXRPC_RXTX_BUFF_SIZE
, sizeof(u8
), gfp
);
113 if (!call
->rxtx_annotations
)
116 mutex_init(&call
->user_mutex
);
117 setup_timer(&call
->timer
, rxrpc_call_timer_expired
,
118 (unsigned long)call
);
119 INIT_WORK(&call
->processor
, &rxrpc_process_call
);
120 INIT_LIST_HEAD(&call
->link
);
121 INIT_LIST_HEAD(&call
->chan_wait_link
);
122 INIT_LIST_HEAD(&call
->accept_link
);
123 INIT_LIST_HEAD(&call
->recvmsg_link
);
124 INIT_LIST_HEAD(&call
->sock_link
);
125 init_waitqueue_head(&call
->waitq
);
126 spin_lock_init(&call
->lock
);
127 spin_lock_init(&call
->notify_lock
);
128 rwlock_init(&call
->state_lock
);
129 atomic_set(&call
->usage
, 1);
130 call
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
131 call
->tx_total_len
= -1;
133 memset(&call
->sock_node
, 0xed, sizeof(call
->sock_node
));
135 /* Leave space in the ring to handle a maxed-out jumbo packet */
136 call
->rx_winsize
= rxrpc_rx_window_size
;
137 call
->tx_winsize
= 16;
138 call
->rx_expect_next
= 1;
141 call
->cong_ssthresh
= RXRPC_RXTX_BUFF_SIZE
- 1;
145 kfree(call
->rxtx_buffer
);
147 kmem_cache_free(rxrpc_call_jar
, call
);
152 * Allocate a new client call.
154 static struct rxrpc_call
*rxrpc_alloc_client_call(struct sockaddr_rxrpc
*srx
,
157 struct rxrpc_call
*call
;
162 call
= rxrpc_alloc_call(gfp
);
164 return ERR_PTR(-ENOMEM
);
165 call
->state
= RXRPC_CALL_CLIENT_AWAIT_CONN
;
166 call
->service_id
= srx
->srx_service
;
167 call
->tx_phase
= true;
168 now
= ktime_get_real();
169 call
->acks_latest_ts
= now
;
170 call
->cong_tstamp
= now
;
172 _leave(" = %p", call
);
177 * Initiate the call ack/resend/expiry timer.
179 static void rxrpc_start_call_timer(struct rxrpc_call
*call
)
181 ktime_t now
= ktime_get_real(), expire_at
;
183 expire_at
= ktime_add_ms(now
, rxrpc_max_call_lifetime
);
184 call
->expire_at
= expire_at
;
185 call
->ack_at
= expire_at
;
186 call
->ping_at
= expire_at
;
187 call
->resend_at
= expire_at
;
188 call
->timer
.expires
= jiffies
+ LONG_MAX
/ 2;
189 rxrpc_set_timer(call
, rxrpc_timer_begin
, now
);
193 * Set up a call for the given parameters.
194 * - Called with the socket lock held, which it must release.
195 * - If it returns a call, the call's lock will need releasing by the caller.
197 struct rxrpc_call
*rxrpc_new_client_call(struct rxrpc_sock
*rx
,
198 struct rxrpc_conn_parameters
*cp
,
199 struct sockaddr_rxrpc
*srx
,
200 unsigned long user_call_ID
,
203 __releases(&rx
->sk
.sk_lock
.slock
)
205 struct rxrpc_call
*call
, *xcall
;
206 struct rxrpc_net
*rxnet
= rxrpc_net(sock_net(&rx
->sk
));
207 struct rb_node
*parent
, **pp
;
208 const void *here
= __builtin_return_address(0);
211 _enter("%p,%lx", rx
, user_call_ID
);
213 call
= rxrpc_alloc_client_call(srx
, gfp
);
215 release_sock(&rx
->sk
);
216 _leave(" = %ld", PTR_ERR(call
));
220 call
->tx_total_len
= tx_total_len
;
221 trace_rxrpc_call(call
, rxrpc_call_new_client
, atomic_read(&call
->usage
),
222 here
, (const void *)user_call_ID
);
224 /* We need to protect a partially set up call against the user as we
225 * will be acting outside the socket lock.
227 mutex_lock(&call
->user_mutex
);
229 /* Publish the call, even though it is incompletely set up as yet */
230 write_lock(&rx
->call_lock
);
232 pp
= &rx
->calls
.rb_node
;
236 xcall
= rb_entry(parent
, struct rxrpc_call
, sock_node
);
238 if (user_call_ID
< xcall
->user_call_ID
)
239 pp
= &(*pp
)->rb_left
;
240 else if (user_call_ID
> xcall
->user_call_ID
)
241 pp
= &(*pp
)->rb_right
;
243 goto error_dup_user_ID
;
246 rcu_assign_pointer(call
->socket
, rx
);
247 call
->user_call_ID
= user_call_ID
;
248 __set_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
);
249 rxrpc_get_call(call
, rxrpc_call_got_userid
);
250 rb_link_node(&call
->sock_node
, parent
, pp
);
251 rb_insert_color(&call
->sock_node
, &rx
->calls
);
252 list_add(&call
->sock_link
, &rx
->sock_calls
);
254 write_unlock(&rx
->call_lock
);
256 write_lock(&rxnet
->call_lock
);
257 list_add_tail(&call
->link
, &rxnet
->calls
);
258 write_unlock(&rxnet
->call_lock
);
260 /* From this point on, the call is protected by its own lock. */
261 release_sock(&rx
->sk
);
263 /* Set up or get a connection record and set the protocol parameters,
264 * including channel number and call ID.
266 ret
= rxrpc_connect_call(call
, cp
, srx
, gfp
);
270 trace_rxrpc_call(call
, rxrpc_call_connected
, atomic_read(&call
->usage
),
273 rxrpc_start_call_timer(call
);
275 _net("CALL new %d on CONN %d", call
->debug_id
, call
->conn
->debug_id
);
277 _leave(" = %p [new]", call
);
280 /* We unexpectedly found the user ID in the list after taking
281 * the call_lock. This shouldn't happen unless the user races
282 * with itself and tries to add the same user ID twice at the
283 * same time in different threads.
286 write_unlock(&rx
->call_lock
);
287 release_sock(&rx
->sk
);
291 __rxrpc_set_call_completion(call
, RXRPC_CALL_LOCAL_ERROR
,
293 trace_rxrpc_call(call
, rxrpc_call_error
, atomic_read(&call
->usage
),
295 rxrpc_release_call(rx
, call
);
296 mutex_unlock(&call
->user_mutex
);
297 rxrpc_put_call(call
, rxrpc_call_put
);
298 _leave(" = %d", ret
);
303 * Retry a call to a new address. It is expected that the Tx queue of the call
304 * will contain data previously packaged for an old call.
306 int rxrpc_retry_client_call(struct rxrpc_sock
*rx
,
307 struct rxrpc_call
*call
,
308 struct rxrpc_conn_parameters
*cp
,
309 struct sockaddr_rxrpc
*srx
,
312 const void *here
= __builtin_return_address(0);
315 /* Set up or get a connection record and set the protocol parameters,
316 * including channel number and call ID.
318 ret
= rxrpc_connect_call(call
, cp
, srx
, gfp
);
322 trace_rxrpc_call(call
, rxrpc_call_connected
, atomic_read(&call
->usage
),
325 rxrpc_start_call_timer(call
);
327 _net("CALL new %d on CONN %d", call
->debug_id
, call
->conn
->debug_id
);
329 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND
, &call
->events
))
330 rxrpc_queue_call(call
);
336 rxrpc_set_call_completion(call
, RXRPC_CALL_LOCAL_ERROR
,
338 trace_rxrpc_call(call
, rxrpc_call_error
, atomic_read(&call
->usage
),
340 _leave(" = %d", ret
);
345 * Set up an incoming call. call->conn points to the connection.
346 * This is called in BH context and isn't allowed to fail.
348 void rxrpc_incoming_call(struct rxrpc_sock
*rx
,
349 struct rxrpc_call
*call
,
352 struct rxrpc_connection
*conn
= call
->conn
;
353 struct rxrpc_skb_priv
*sp
= rxrpc_skb(skb
);
356 _enter(",%d", call
->conn
->debug_id
);
358 rcu_assign_pointer(call
->socket
, rx
);
359 call
->call_id
= sp
->hdr
.callNumber
;
360 call
->service_id
= sp
->hdr
.serviceId
;
361 call
->cid
= sp
->hdr
.cid
;
362 call
->state
= RXRPC_CALL_SERVER_ACCEPTING
;
363 if (sp
->hdr
.securityIndex
> 0)
364 call
->state
= RXRPC_CALL_SERVER_SECURING
;
365 call
->cong_tstamp
= skb
->tstamp
;
367 /* Set the channel for this call. We don't get channel_lock as we're
368 * only defending against the data_ready handler (which we're called
369 * from) and the RESPONSE packet parser (which is only really
370 * interested in call_counter and can cope with a disagreement with the
373 chan
= sp
->hdr
.cid
& RXRPC_CHANNELMASK
;
374 conn
->channels
[chan
].call_counter
= call
->call_id
;
375 conn
->channels
[chan
].call_id
= call
->call_id
;
376 rcu_assign_pointer(conn
->channels
[chan
].call
, call
);
378 spin_lock(&conn
->params
.peer
->lock
);
379 hlist_add_head(&call
->error_link
, &conn
->params
.peer
->error_targets
);
380 spin_unlock(&conn
->params
.peer
->lock
);
382 _net("CALL incoming %d on CONN %d", call
->debug_id
, call
->conn
->debug_id
);
384 rxrpc_start_call_timer(call
);
389 * Queue a call's work processor, getting a ref to pass to the work queue.
391 bool rxrpc_queue_call(struct rxrpc_call
*call
)
393 const void *here
= __builtin_return_address(0);
394 int n
= __atomic_add_unless(&call
->usage
, 1, 0);
397 if (rxrpc_queue_work(&call
->processor
))
398 trace_rxrpc_call(call
, rxrpc_call_queued
, n
+ 1, here
, NULL
);
400 rxrpc_put_call(call
, rxrpc_call_put_noqueue
);
405 * Queue a call's work processor, passing the callers ref to the work queue.
407 bool __rxrpc_queue_call(struct rxrpc_call
*call
)
409 const void *here
= __builtin_return_address(0);
410 int n
= atomic_read(&call
->usage
);
412 if (rxrpc_queue_work(&call
->processor
))
413 trace_rxrpc_call(call
, rxrpc_call_queued_ref
, n
, here
, NULL
);
415 rxrpc_put_call(call
, rxrpc_call_put_noqueue
);
420 * Note the re-emergence of a call.
422 void rxrpc_see_call(struct rxrpc_call
*call
)
424 const void *here
= __builtin_return_address(0);
426 int n
= atomic_read(&call
->usage
);
428 trace_rxrpc_call(call
, rxrpc_call_seen
, n
, here
, NULL
);
433 * Note the addition of a ref on a call.
435 void rxrpc_get_call(struct rxrpc_call
*call
, enum rxrpc_call_trace op
)
437 const void *here
= __builtin_return_address(0);
438 int n
= atomic_inc_return(&call
->usage
);
440 trace_rxrpc_call(call
, op
, n
, here
, NULL
);
444 * Detach a call from its owning socket.
446 void rxrpc_release_call(struct rxrpc_sock
*rx
, struct rxrpc_call
*call
)
448 const void *here
= __builtin_return_address(0);
449 struct rxrpc_connection
*conn
= call
->conn
;
453 _enter("{%d,%d}", call
->debug_id
, atomic_read(&call
->usage
));
455 trace_rxrpc_call(call
, rxrpc_call_release
, atomic_read(&call
->usage
),
456 here
, (const void *)call
->flags
);
458 ASSERTCMP(call
->state
, ==, RXRPC_CALL_COMPLETE
);
460 spin_lock_bh(&call
->lock
);
461 if (test_and_set_bit(RXRPC_CALL_RELEASED
, &call
->flags
))
463 spin_unlock_bh(&call
->lock
);
465 del_timer_sync(&call
->timer
);
467 /* Make sure we don't get any more notifications */
468 write_lock_bh(&rx
->recvmsg_lock
);
470 if (!list_empty(&call
->recvmsg_link
)) {
471 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
472 call
, call
->events
, call
->flags
);
473 list_del(&call
->recvmsg_link
);
477 /* list_empty() must return false in rxrpc_notify_socket() */
478 call
->recvmsg_link
.next
= NULL
;
479 call
->recvmsg_link
.prev
= NULL
;
481 write_unlock_bh(&rx
->recvmsg_lock
);
483 rxrpc_put_call(call
, rxrpc_call_put
);
485 write_lock(&rx
->call_lock
);
487 if (test_and_clear_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
)) {
488 rb_erase(&call
->sock_node
, &rx
->calls
);
489 memset(&call
->sock_node
, 0xdd, sizeof(call
->sock_node
));
490 rxrpc_put_call(call
, rxrpc_call_put_userid
);
493 list_del(&call
->sock_link
);
494 write_unlock(&rx
->call_lock
);
496 _debug("RELEASE CALL %p (%d CONN %p)", call
, call
->debug_id
, conn
);
499 rxrpc_disconnect_call(call
);
501 for (i
= 0; i
< RXRPC_RXTX_BUFF_SIZE
; i
++) {
502 rxrpc_free_skb(call
->rxtx_buffer
[i
],
503 (call
->tx_phase
? rxrpc_skb_tx_cleaned
:
504 rxrpc_skb_rx_cleaned
));
505 call
->rxtx_buffer
[i
] = NULL
;
512 * Prepare a kernel service call for retry.
514 int rxrpc_prepare_call_for_retry(struct rxrpc_sock
*rx
, struct rxrpc_call
*call
)
516 const void *here
= __builtin_return_address(0);
520 _enter("{%d,%d}", call
->debug_id
, atomic_read(&call
->usage
));
522 trace_rxrpc_call(call
, rxrpc_call_release
, atomic_read(&call
->usage
),
523 here
, (const void *)call
->flags
);
525 ASSERTCMP(call
->state
, ==, RXRPC_CALL_COMPLETE
);
526 ASSERTCMP(call
->completion
, !=, RXRPC_CALL_REMOTELY_ABORTED
);
527 ASSERTCMP(call
->completion
, !=, RXRPC_CALL_LOCALLY_ABORTED
);
528 ASSERT(list_empty(&call
->recvmsg_link
));
530 del_timer_sync(&call
->timer
);
532 _debug("RELEASE CALL %p (%d CONN %p)", call
, call
->debug_id
, call
->conn
);
535 rxrpc_disconnect_call(call
);
537 if (rxrpc_is_service_call(call
) ||
539 call
->tx_hard_ack
!= 0 ||
540 call
->rx_hard_ack
!= 0 ||
544 call
->state
= RXRPC_CALL_UNINITIALISED
;
545 call
->completion
= RXRPC_CALL_SUCCEEDED
;
549 call
->cong_extra
= 0;
550 call
->cong_ssthresh
= 0;
552 call
->cong_dup_acks
= 0;
553 call
->cong_cumul_acks
= 0;
554 call
->acks_lowest_nak
= 0;
556 for (i
= 0; i
< RXRPC_RXTX_BUFF_SIZE
; i
++) {
557 last
|= call
->rxtx_annotations
[i
];
558 call
->rxtx_annotations
[i
] &= RXRPC_TX_ANNO_LAST
;
559 call
->rxtx_annotations
[i
] |= RXRPC_TX_ANNO_RETRANS
;
567 * release all the calls associated with a socket
569 void rxrpc_release_calls_on_socket(struct rxrpc_sock
*rx
)
571 struct rxrpc_call
*call
;
575 while (!list_empty(&rx
->to_be_accepted
)) {
576 call
= list_entry(rx
->to_be_accepted
.next
,
577 struct rxrpc_call
, accept_link
);
578 list_del(&call
->accept_link
);
579 rxrpc_abort_call("SKR", call
, 0, RX_CALL_DEAD
, -ECONNRESET
);
580 rxrpc_put_call(call
, rxrpc_call_put
);
583 while (!list_empty(&rx
->sock_calls
)) {
584 call
= list_entry(rx
->sock_calls
.next
,
585 struct rxrpc_call
, sock_link
);
586 rxrpc_get_call(call
, rxrpc_call_got
);
587 rxrpc_abort_call("SKT", call
, 0, RX_CALL_DEAD
, -ECONNRESET
);
588 rxrpc_send_abort_packet(call
);
589 rxrpc_release_call(rx
, call
);
590 rxrpc_put_call(call
, rxrpc_call_put
);
599 void rxrpc_put_call(struct rxrpc_call
*call
, enum rxrpc_call_trace op
)
601 struct rxrpc_net
*rxnet
;
602 const void *here
= __builtin_return_address(0);
605 ASSERT(call
!= NULL
);
607 n
= atomic_dec_return(&call
->usage
);
608 trace_rxrpc_call(call
, op
, n
, here
, NULL
);
611 _debug("call %d dead", call
->debug_id
);
612 ASSERTCMP(call
->state
, ==, RXRPC_CALL_COMPLETE
);
614 if (!list_empty(&call
->link
)) {
615 rxnet
= rxrpc_net(sock_net(&call
->socket
->sk
));
616 write_lock(&rxnet
->call_lock
);
617 list_del_init(&call
->link
);
618 write_unlock(&rxnet
->call_lock
);
621 rxrpc_cleanup_call(call
);
626 * Final call destruction under RCU.
628 static void rxrpc_rcu_destroy_call(struct rcu_head
*rcu
)
630 struct rxrpc_call
*call
= container_of(rcu
, struct rxrpc_call
, rcu
);
632 rxrpc_put_peer(call
->peer
);
633 kfree(call
->rxtx_buffer
);
634 kfree(call
->rxtx_annotations
);
635 kmem_cache_free(rxrpc_call_jar
, call
);
641 void rxrpc_cleanup_call(struct rxrpc_call
*call
)
645 _net("DESTROY CALL %d", call
->debug_id
);
647 memset(&call
->sock_node
, 0xcd, sizeof(call
->sock_node
));
649 del_timer_sync(&call
->timer
);
651 ASSERTCMP(call
->state
, ==, RXRPC_CALL_COMPLETE
);
652 ASSERT(test_bit(RXRPC_CALL_RELEASED
, &call
->flags
));
653 ASSERTCMP(call
->conn
, ==, NULL
);
655 /* Clean up the Rx/Tx buffer */
656 for (i
= 0; i
< RXRPC_RXTX_BUFF_SIZE
; i
++)
657 rxrpc_free_skb(call
->rxtx_buffer
[i
],
658 (call
->tx_phase
? rxrpc_skb_tx_cleaned
:
659 rxrpc_skb_rx_cleaned
));
661 rxrpc_free_skb(call
->tx_pending
, rxrpc_skb_tx_cleaned
);
663 call_rcu(&call
->rcu
, rxrpc_rcu_destroy_call
);
667 * Make sure that all calls are gone from a network namespace. To reach this
668 * point, any open UDP sockets in that namespace must have been closed, so any
669 * outstanding calls cannot be doing I/O.
671 void rxrpc_destroy_all_calls(struct rxrpc_net
*rxnet
)
673 struct rxrpc_call
*call
;
677 if (list_empty(&rxnet
->calls
))
680 write_lock(&rxnet
->call_lock
);
682 while (!list_empty(&rxnet
->calls
)) {
683 call
= list_entry(rxnet
->calls
.next
, struct rxrpc_call
, link
);
684 _debug("Zapping call %p", call
);
686 rxrpc_see_call(call
);
687 list_del_init(&call
->link
);
689 pr_err("Call %p still in use (%d,%s,%lx,%lx)!\n",
690 call
, atomic_read(&call
->usage
),
691 rxrpc_call_states
[call
->state
],
692 call
->flags
, call
->events
);
694 write_unlock(&rxnet
->call_lock
);
696 write_lock(&rxnet
->call_lock
);
699 write_unlock(&rxnet
->call_lock
);