Lines Matching +full:diff +full:- +full:channels

1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Client connection-specific management code.
13 * (2) DONT_REUSE - The connection should be discarded as soon as possible and
31 #include "ar-internal.h"
39 atomic_inc(&bundle->active); in rxrpc_activate_bundle()
48 idr_remove(&local->conn_ids, conn->proto.cid >> RXRPC_CIDSHIFT); in rxrpc_put_client_connection_id()
59 if (!idr_is_empty(&local->conn_ids)) { in rxrpc_destroy_client_conn_ids()
60 idr_for_each_entry(&local->conn_ids, conn, id) { in rxrpc_destroy_client_conn_ids()
62 conn, refcount_read(&conn->ref)); in rxrpc_destroy_client_conn_ids()
67 idr_destroy(&local->conn_ids); in rxrpc_destroy_client_conn_ids()
81 bundle->local = call->local; in rxrpc_alloc_bundle()
82 bundle->peer = rxrpc_get_peer(call->peer, rxrpc_peer_get_bundle); in rxrpc_alloc_bundle()
83 bundle->key = key_get(call->key); in rxrpc_alloc_bundle()
84 bundle->security = call->security; in rxrpc_alloc_bundle()
85 bundle->exclusive = test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags); in rxrpc_alloc_bundle()
86 bundle->upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags); in rxrpc_alloc_bundle()
87 bundle->service_id = call->dest_srx.srx_service; in rxrpc_alloc_bundle()
88 bundle->security_level = call->security_level; in rxrpc_alloc_bundle()
89 bundle->debug_id = atomic_inc_return(&rxrpc_bundle_id); in rxrpc_alloc_bundle()
90 refcount_set(&bundle->ref, 1); in rxrpc_alloc_bundle()
91 atomic_set(&bundle->active, 1); in rxrpc_alloc_bundle()
92 INIT_LIST_HEAD(&bundle->waiting_calls); in rxrpc_alloc_bundle()
93 trace_rxrpc_bundle(bundle->debug_id, 1, rxrpc_bundle_new); in rxrpc_alloc_bundle()
95 write_lock(&bundle->local->rxnet->conn_lock); in rxrpc_alloc_bundle()
96 list_add_tail(&bundle->proc_link, &bundle->local->rxnet->bundle_proc_list); in rxrpc_alloc_bundle()
97 write_unlock(&bundle->local->rxnet->conn_lock); in rxrpc_alloc_bundle()
107 __refcount_inc(&bundle->ref, &r); in rxrpc_get_bundle()
108 trace_rxrpc_bundle(bundle->debug_id, r + 1, why); in rxrpc_get_bundle()
114 trace_rxrpc_bundle(bundle->debug_id, refcount_read(&bundle->ref), in rxrpc_free_bundle()
116 write_lock(&bundle->local->rxnet->conn_lock); in rxrpc_free_bundle()
117 list_del(&bundle->proc_link); in rxrpc_free_bundle()
118 write_unlock(&bundle->local->rxnet->conn_lock); in rxrpc_free_bundle()
119 rxrpc_put_peer(bundle->peer, rxrpc_peer_put_bundle); in rxrpc_free_bundle()
120 key_put(bundle->key); in rxrpc_free_bundle()
131 id = bundle->debug_id; in rxrpc_put_bundle()
132 dead = __refcount_dec_and_test(&bundle->ref, &r); in rxrpc_put_bundle()
133 trace_rxrpc_bundle(id, r - 1, why); in rxrpc_put_bundle()
155 struct rxrpc_local *local = bundle->local; in rxrpc_alloc_client_connection()
156 struct rxrpc_net *rxnet = local->rxnet; in rxrpc_alloc_client_connection()
163 return ERR_PTR(-ENOMEM); in rxrpc_alloc_client_connection()
165 id = idr_alloc_cyclic(&local->conn_ids, conn, 1, 0x40000000, in rxrpc_alloc_client_connection()
172 refcount_set(&conn->ref, 1); in rxrpc_alloc_client_connection()
173 conn->proto.cid = id << RXRPC_CIDSHIFT; in rxrpc_alloc_client_connection()
174 conn->proto.epoch = local->rxnet->epoch; in rxrpc_alloc_client_connection()
175 conn->out_clientflag = RXRPC_CLIENT_INITIATED; in rxrpc_alloc_client_connection()
176 conn->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_conn); in rxrpc_alloc_client_connection()
177 conn->local = rxrpc_get_local(bundle->local, rxrpc_local_get_client_conn); in rxrpc_alloc_client_connection()
178 conn->peer = rxrpc_get_peer(bundle->peer, rxrpc_peer_get_client_conn); in rxrpc_alloc_client_connection()
179 conn->key = key_get(bundle->key); in rxrpc_alloc_client_connection()
180 conn->security = bundle->security; in rxrpc_alloc_client_connection()
181 conn->exclusive = bundle->exclusive; in rxrpc_alloc_client_connection()
182 conn->upgrade = bundle->upgrade; in rxrpc_alloc_client_connection()
183 conn->orig_service_id = bundle->service_id; in rxrpc_alloc_client_connection()
184 conn->security_level = bundle->security_level; in rxrpc_alloc_client_connection()
185 conn->state = RXRPC_CONN_CLIENT_UNSECURED; in rxrpc_alloc_client_connection()
186 conn->service_id = conn->orig_service_id; in rxrpc_alloc_client_connection()
188 if (conn->security == &rxrpc_no_security) in rxrpc_alloc_client_connection()
189 conn->state = RXRPC_CONN_CLIENT; in rxrpc_alloc_client_connection()
191 atomic_inc(&rxnet->nr_conns); in rxrpc_alloc_client_connection()
192 write_lock(&rxnet->conn_lock); in rxrpc_alloc_client_connection()
193 list_add_tail(&conn->proc_link, &rxnet->conn_proc_list); in rxrpc_alloc_client_connection()
194 write_unlock(&rxnet->conn_lock); in rxrpc_alloc_client_connection()
198 atomic_inc(&rxnet->nr_client_conns); in rxrpc_alloc_client_connection()
199 trace_rxrpc_client(conn, -1, rxrpc_client_alloc); in rxrpc_alloc_client_connection()
214 rxnet = conn->rxnet; in rxrpc_may_reuse_conn()
215 if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags)) in rxrpc_may_reuse_conn()
218 if ((conn->state != RXRPC_CONN_CLIENT_UNSECURED && in rxrpc_may_reuse_conn()
219 conn->state != RXRPC_CONN_CLIENT) || in rxrpc_may_reuse_conn()
220 conn->proto.epoch != rxnet->epoch) in rxrpc_may_reuse_conn()
229 id_cursor = idr_get_cursor(&conn->local->conn_ids); in rxrpc_may_reuse_conn()
230 id = conn->proto.cid >> RXRPC_CIDSHIFT; in rxrpc_may_reuse_conn()
231 distance = id - id_cursor; in rxrpc_may_reuse_conn()
233 distance = -distance; in rxrpc_may_reuse_conn()
234 limit = max_t(unsigned long, atomic_read(&rxnet->nr_conns) * 4, 1024); in rxrpc_may_reuse_conn()
241 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); in rxrpc_may_reuse_conn()
253 struct rxrpc_local *local = call->local; in rxrpc_look_up_bundle()
255 long diff; in rxrpc_look_up_bundle() local
256 bool upgrade = test_bit(RXRPC_CALL_UPGRADE, &call->flags); in rxrpc_look_up_bundle()
259 call->peer, key_serial(call->key), call->security_level, in rxrpc_look_up_bundle()
262 if (test_bit(RXRPC_CALL_EXCLUSIVE, &call->flags)) { in rxrpc_look_up_bundle()
263 call->bundle = rxrpc_alloc_bundle(call, gfp); in rxrpc_look_up_bundle()
264 return call->bundle ? 0 : -ENOMEM; in rxrpc_look_up_bundle()
269 spin_lock(&local->client_bundles_lock); in rxrpc_look_up_bundle()
270 p = local->client_bundles.rb_node; in rxrpc_look_up_bundle()
274 #define cmp(X, Y) ((long)(X) - (long)(Y)) in rxrpc_look_up_bundle()
275 diff = (cmp(bundle->peer, call->peer) ?: in rxrpc_look_up_bundle()
276 cmp(bundle->key, call->key) ?: in rxrpc_look_up_bundle()
277 cmp(bundle->security_level, call->security_level) ?: in rxrpc_look_up_bundle()
278 cmp(bundle->upgrade, upgrade)); in rxrpc_look_up_bundle()
280 if (diff < 0) in rxrpc_look_up_bundle()
281 p = p->rb_left; in rxrpc_look_up_bundle()
282 else if (diff > 0) in rxrpc_look_up_bundle()
283 p = p->rb_right; in rxrpc_look_up_bundle()
287 spin_unlock(&local->client_bundles_lock); in rxrpc_look_up_bundle()
293 return -ENOMEM; in rxrpc_look_up_bundle()
296 spin_lock(&local->client_bundles_lock); in rxrpc_look_up_bundle()
297 pp = &local->client_bundles.rb_node; in rxrpc_look_up_bundle()
303 #define cmp(X, Y) ((long)(X) - (long)(Y)) in rxrpc_look_up_bundle()
304 diff = (cmp(bundle->peer, call->peer) ?: in rxrpc_look_up_bundle()
305 cmp(bundle->key, call->key) ?: in rxrpc_look_up_bundle()
306 cmp(bundle->security_level, call->security_level) ?: in rxrpc_look_up_bundle()
307 cmp(bundle->upgrade, upgrade)); in rxrpc_look_up_bundle()
309 if (diff < 0) in rxrpc_look_up_bundle()
310 pp = &(*pp)->rb_left; in rxrpc_look_up_bundle()
311 else if (diff > 0) in rxrpc_look_up_bundle()
312 pp = &(*pp)->rb_right; in rxrpc_look_up_bundle()
318 rb_link_node(&candidate->local_node, parent, pp); in rxrpc_look_up_bundle()
319 rb_insert_color(&candidate->local_node, &local->client_bundles); in rxrpc_look_up_bundle()
320 call->bundle = rxrpc_get_bundle(candidate, rxrpc_bundle_get_client_call); in rxrpc_look_up_bundle()
321 spin_unlock(&local->client_bundles_lock); in rxrpc_look_up_bundle()
322 _leave(" = B=%u [new]", call->bundle->debug_id); in rxrpc_look_up_bundle()
328 call->bundle = rxrpc_get_bundle(bundle, rxrpc_bundle_get_client_call); in rxrpc_look_up_bundle()
330 spin_unlock(&local->client_bundles_lock); in rxrpc_look_up_bundle()
331 _leave(" = B=%u [found]", call->bundle->debug_id); in rxrpc_look_up_bundle()
345 old = bundle->conns[slot]; in rxrpc_add_conn_to_bundle()
347 bundle->conns[slot] = NULL; in rxrpc_add_conn_to_bundle()
348 bundle->conn_ids[slot] = 0; in rxrpc_add_conn_to_bundle()
349 trace_rxrpc_client(old, -1, rxrpc_client_replace); in rxrpc_add_conn_to_bundle()
355 bundle->alloc_error = PTR_ERR(conn); in rxrpc_add_conn_to_bundle()
360 conn->bundle_shift = shift; in rxrpc_add_conn_to_bundle()
361 bundle->conns[slot] = conn; in rxrpc_add_conn_to_bundle()
362 bundle->conn_ids[slot] = conn->debug_id; in rxrpc_add_conn_to_bundle()
364 set_bit(shift + i, &bundle->avail_chans); in rxrpc_add_conn_to_bundle()
374 int slot = -1, i, usable; in rxrpc_bundle_has_space()
378 bundle->alloc_error = 0; in rxrpc_bundle_has_space()
382 for (i = 0; i < ARRAY_SIZE(bundle->conns); i++) { in rxrpc_bundle_has_space()
383 if (rxrpc_may_reuse_conn(bundle->conns[i])) in rxrpc_bundle_has_space()
385 else if (slot == -1) in rxrpc_bundle_has_space()
389 if (!usable && bundle->upgrade) in rxrpc_bundle_has_space()
390 bundle->try_upgrade = true; in rxrpc_bundle_has_space()
395 if (!bundle->avail_chans && in rxrpc_bundle_has_space()
396 !bundle->try_upgrade && in rxrpc_bundle_has_space()
397 usable < ARRAY_SIZE(bundle->conns)) in rxrpc_bundle_has_space()
415 struct rxrpc_channel *chan = &conn->channels[channel]; in rxrpc_activate_one_channel()
416 struct rxrpc_bundle *bundle = conn->bundle; in rxrpc_activate_one_channel()
417 struct rxrpc_call *call = list_entry(bundle->waiting_calls.next, in rxrpc_activate_one_channel()
419 u32 call_id = chan->call_counter + 1; in rxrpc_activate_one_channel()
421 _enter("C=%x,%u", conn->debug_id, channel); in rxrpc_activate_one_channel()
423 list_del_init(&call->wait_link); in rxrpc_activate_one_channel()
430 clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); in rxrpc_activate_one_channel()
431 clear_bit(conn->bundle_shift + channel, &bundle->avail_chans); in rxrpc_activate_one_channel()
434 call->conn = rxrpc_get_connection(conn, rxrpc_conn_get_activate_call); in rxrpc_activate_one_channel()
435 call->cid = conn->proto.cid | channel; in rxrpc_activate_one_channel()
436 call->call_id = call_id; in rxrpc_activate_one_channel()
437 call->dest_srx.srx_service = conn->service_id; in rxrpc_activate_one_channel()
438 call->cong_ssthresh = call->peer->cong_ssthresh; in rxrpc_activate_one_channel()
439 if (call->cong_cwnd >= call->cong_ssthresh) in rxrpc_activate_one_channel()
440 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE; in rxrpc_activate_one_channel()
442 call->cong_mode = RXRPC_CALL_SLOW_START; in rxrpc_activate_one_channel()
444 chan->call_id = call_id; in rxrpc_activate_one_channel()
445 chan->call_debug_id = call->debug_id; in rxrpc_activate_one_channel()
446 chan->call = call; in rxrpc_activate_one_channel()
450 call->tx_last_sent = ktime_get_real(); in rxrpc_activate_one_channel()
453 wake_up(&call->waitq); in rxrpc_activate_one_channel()
461 if (!list_empty(&conn->cache_link)) { in rxrpc_unidle_conn()
462 list_del_init(&conn->cache_link); in rxrpc_unidle_conn()
468 * Assign channels and callNumbers to waiting calls.
476 trace_rxrpc_client(NULL, -1, rxrpc_client_activate_chans); in rxrpc_activate_channels()
478 if (bundle->try_upgrade) in rxrpc_activate_channels()
483 while (!list_empty(&bundle->waiting_calls)) { in rxrpc_activate_channels()
484 avail = bundle->avail_chans & mask; in rxrpc_activate_channels()
488 clear_bit(channel, &bundle->avail_chans); in rxrpc_activate_channels()
491 conn = bundle->conns[slot]; in rxrpc_activate_channels()
495 if (bundle->try_upgrade) in rxrpc_activate_channels()
496 set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags); in rxrpc_activate_channels()
499 channel &= (RXRPC_MAXCALLS - 1); in rxrpc_activate_channels()
500 conn->act_chans |= 1 << channel; in rxrpc_activate_channels()
506 * Connect waiting channels (called from the I/O thread).
512 while ((call = list_first_entry_or_null(&local->new_client_calls, in rxrpc_connect_client_calls()
515 struct rxrpc_bundle *bundle = call->bundle; in rxrpc_connect_client_calls()
517 spin_lock(&local->client_call_lock); in rxrpc_connect_client_calls()
518 list_move_tail(&call->wait_link, &bundle->waiting_calls); in rxrpc_connect_client_calls()
520 spin_unlock(&local->client_call_lock); in rxrpc_connect_client_calls()
533 unsigned int channel = call->cid & RXRPC_CHANNELMASK; in rxrpc_expose_client_call()
534 struct rxrpc_connection *conn = call->conn; in rxrpc_expose_client_call()
535 struct rxrpc_channel *chan = &conn->channels[channel]; in rxrpc_expose_client_call()
537 if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) { in rxrpc_expose_client_call()
543 chan->call_counter++; in rxrpc_expose_client_call()
544 if (chan->call_counter >= INT_MAX) in rxrpc_expose_client_call()
545 set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags); in rxrpc_expose_client_call()
548 spin_lock(&call->peer->lock); in rxrpc_expose_client_call()
549 hlist_add_head(&call->error_link, &call->peer->error_targets); in rxrpc_expose_client_call()
550 spin_unlock(&call->peer->lock); in rxrpc_expose_client_call()
559 if (!local->kill_all_client_conns) { in rxrpc_set_client_reap_timer()
563 if (local->rxnet->live) in rxrpc_set_client_reap_timer()
564 timer_reduce(&local->client_conn_reap_timer, reap_at); in rxrpc_set_client_reap_timer()
575 struct rxrpc_local *local = bundle->local; in rxrpc_disconnect_client_call()
580 _enter("c=%x", call->debug_id); in rxrpc_disconnect_client_call()
585 conn = call->conn; in rxrpc_disconnect_client_call()
588 ASSERTCMP(call->call_id, ==, 0); in rxrpc_disconnect_client_call()
589 ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags)); in rxrpc_disconnect_client_call()
590 /* May still be on ->new_client_calls. */ in rxrpc_disconnect_client_call()
591 spin_lock(&local->client_call_lock); in rxrpc_disconnect_client_call()
592 list_del_init(&call->wait_link); in rxrpc_disconnect_client_call()
593 spin_unlock(&local->client_call_lock); in rxrpc_disconnect_client_call()
597 cid = call->cid; in rxrpc_disconnect_client_call()
599 chan = &conn->channels[channel]; in rxrpc_disconnect_client_call()
602 if (WARN_ON(chan->call != call)) in rxrpc_disconnect_client_call()
616 if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { in rxrpc_disconnect_client_call()
617 _debug("exposed %u,%u", call->call_id, call->abort_code); in rxrpc_disconnect_client_call()
620 if (test_and_clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) { in rxrpc_disconnect_client_call()
622 bundle->try_upgrade = false; in rxrpc_disconnect_client_call()
629 if (may_reuse && !list_empty(&bundle->waiting_calls)) { in rxrpc_disconnect_client_call()
636 * can be skipped if we find a follow-on call. The first DATA packet in rxrpc_disconnect_client_call()
639 if (call->completion == RXRPC_CALL_SUCCEEDED && in rxrpc_disconnect_client_call()
640 test_bit(RXRPC_CALL_EXPOSED, &call->flags)) { in rxrpc_disconnect_client_call()
643 chan->final_ack_at = final_ack_at; in rxrpc_disconnect_client_call()
645 set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags); in rxrpc_disconnect_client_call()
650 chan->call = NULL; in rxrpc_disconnect_client_call()
651 set_bit(conn->bundle_shift + channel, &conn->bundle->avail_chans); in rxrpc_disconnect_client_call()
652 conn->act_chans &= ~(1 << channel); in rxrpc_disconnect_client_call()
654 /* If no channels remain active, then put the connection on the idle in rxrpc_disconnect_client_call()
658 if (!conn->act_chans) { in rxrpc_disconnect_client_call()
660 conn->idle_timestamp = jiffies; in rxrpc_disconnect_client_call()
663 list_move_tail(&conn->cache_link, &local->idle_client_conns); in rxrpc_disconnect_client_call()
674 struct rxrpc_bundle *bundle = conn->bundle; in rxrpc_unbundle_conn()
678 _enter("C=%x", conn->debug_id); in rxrpc_unbundle_conn()
680 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK) in rxrpc_unbundle_conn()
683 bindex = conn->bundle_shift / RXRPC_MAXCALLS; in rxrpc_unbundle_conn()
684 if (bundle->conns[bindex] == conn) { in rxrpc_unbundle_conn()
686 bundle->conns[bindex] = NULL; in rxrpc_unbundle_conn()
687 bundle->conn_ids[bindex] = 0; in rxrpc_unbundle_conn()
689 clear_bit(conn->bundle_shift + i, &bundle->avail_chans); in rxrpc_unbundle_conn()
690 rxrpc_put_client_connection_id(bundle->local, conn); in rxrpc_unbundle_conn()
707 local = bundle->local; in rxrpc_deactivate_bundle()
708 if (atomic_dec_and_lock(&bundle->active, &local->client_bundles_lock)) { in rxrpc_deactivate_bundle()
709 if (!bundle->exclusive) { in rxrpc_deactivate_bundle()
711 rb_erase(&bundle->local_node, &local->client_bundles); in rxrpc_deactivate_bundle()
715 spin_unlock(&local->client_bundles_lock); in rxrpc_deactivate_bundle()
726 struct rxrpc_local *local = conn->local; in rxrpc_kill_client_conn()
727 struct rxrpc_net *rxnet = local->rxnet; in rxrpc_kill_client_conn()
729 _enter("C=%x", conn->debug_id); in rxrpc_kill_client_conn()
731 trace_rxrpc_client(conn, -1, rxrpc_client_cleanup); in rxrpc_kill_client_conn()
732 atomic_dec(&rxnet->nr_client_conns); in rxrpc_kill_client_conn()
742 * considered non-reentrant.
755 nr_conns = atomic_read(&local->rxnet->nr_client_conns); in rxrpc_discard_expired_client_conns()
758 conn = list_first_entry_or_null(&local->idle_client_conns, in rxrpc_discard_expired_client_conns()
763 if (!local->kill_all_client_conns) { in rxrpc_discard_expired_client_conns()
767 * final-ACK or ABORT retransmission. in rxrpc_discard_expired_client_conns()
772 if (conn->local->service_closed) in rxrpc_discard_expired_client_conns()
775 conn_expires_at = conn->idle_timestamp + expiry; in rxrpc_discard_expired_client_conns()
782 atomic_dec(&conn->active); in rxrpc_discard_expired_client_conns()
783 trace_rxrpc_client(conn, -1, rxrpc_client_discard); in rxrpc_discard_expired_client_conns()
784 list_del_init(&conn->cache_link); in rxrpc_discard_expired_client_conns()
787 /* Drop the ->cache_link ref */ in rxrpc_discard_expired_client_conns()
790 nr_conns--; in rxrpc_discard_expired_client_conns()
797 * We don't worry if the work item is already scheduled - it can look in rxrpc_discard_expired_client_conns()
802 if (!local->kill_all_client_conns) in rxrpc_discard_expired_client_conns()
803 timer_reduce(&local->client_conn_reap_timer, conn_expires_at); in rxrpc_discard_expired_client_conns()
817 local->kill_all_client_conns = true; in rxrpc_clean_up_local_conns()
819 del_timer_sync(&local->client_conn_reap_timer); in rxrpc_clean_up_local_conns()
821 while ((conn = list_first_entry_or_null(&local->idle_client_conns, in rxrpc_clean_up_local_conns()
823 list_del_init(&conn->cache_link); in rxrpc_clean_up_local_conns()
824 atomic_dec(&conn->active); in rxrpc_clean_up_local_conns()
825 trace_rxrpc_client(conn, -1, rxrpc_client_discard); in rxrpc_clean_up_local_conns()