conn_client.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /* Client connection-specific management code.
  2. *
  3. * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. *
  11. *
  12. * Client connections need to be cached for a little while after they've made a
  13. * call so as to handle retransmitted DATA packets in case the server didn't
  14. * receive the final ACK or terminating ABORT we sent it.
  15. *
  16. * Client connections can be in one of a number of cache states:
  17. *
  18. * (1) INACTIVE - The connection is not held in any list and may not have been
  19. * exposed to the world. If it has been previously exposed, it was
  20. * discarded from the idle list after expiring.
  21. *
  22. * (2) WAITING - The connection is waiting for the number of client conns to
  23. * drop below the maximum capacity. Calls may be in progress upon it from
  24. * when it was active and got culled.
  25. *
  26. * The connection is on the rxrpc_waiting_client_conns list which is kept
  27. * in to-be-granted order. Culled conns with waiters go to the back of
  28. * the queue just like new conns.
  29. *
  30. * (3) ACTIVE - The connection has at least one call in progress upon it, it
  31. * may freely grant available channels to new calls and calls may be
  32. * waiting on it for channels to become available.
  33. *
  34. * The connection is on the rxnet->active_client_conns list which is kept
  35. * in activation order for culling purposes.
  36. *
  37. * rxrpc_nr_active_client_conns is held incremented also.
  38. *
  39. * (4) UPGRADE - As for ACTIVE, but only one call may be in progress and is
  40. * being used to probe for service upgrade.
  41. *
  42. * (5) CULLED - The connection got summarily culled to try and free up
  43. * capacity. Calls currently in progress on the connection are allowed to
  44. * continue, but new calls will have to wait. There can be no waiters in
  45. * this state - the conn would have to go to the WAITING state instead.
  46. *
  47. * (6) IDLE - The connection has no calls in progress upon it and must have
  48. * been exposed to the world (ie. the EXPOSED flag must be set). When it
  49. * expires, the EXPOSED flag is cleared and the connection transitions to
  50. * the INACTIVE state.
  51. *
  52. * The connection is on the rxnet->idle_client_conns list which is kept in
  53. * order of how soon they'll expire.
  54. *
  55. * There are flags of relevance to the cache:
  56. *
  57. * (1) EXPOSED - The connection ID got exposed to the world. If this flag is
  58. * set, an extra ref is added to the connection preventing it from being
  59. * reaped when it has no calls outstanding. This flag is cleared and the
  60. * ref dropped when a conn is discarded from the idle list.
  61. *
  62. * This allows us to move terminal call state retransmission to the
  63. * connection and to discard the call immediately we think it is done
  64. * with. It also give us a chance to reuse the connection.
  65. *
  66. * (2) DONT_REUSE - The connection should be discarded as soon as possible and
  67. * should not be reused. This is set when an exclusive connection is used
  68. * or a call ID counter overflows.
  69. *
  70. * The caching state may only be changed if the cache lock is held.
  71. *
  72. * There are two idle client connection expiry durations. If the total number
  73. * of connections is below the reap threshold, we use the normal duration; if
  74. * it's above, we use the fast duration.
  75. */
  76. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  77. #include <linux/slab.h>
  78. #include <linux/idr.h>
  79. #include <linux/timer.h>
  80. #include <linux/sched/signal.h>
  81. #include "ar-internal.h"
  82. __read_mostly unsigned int rxrpc_max_client_connections = 1000;
  83. __read_mostly unsigned int rxrpc_reap_client_connections = 900;
  84. __read_mostly unsigned long rxrpc_conn_idle_client_expiry = 2 * 60 * HZ;
  85. __read_mostly unsigned long rxrpc_conn_idle_client_fast_expiry = 2 * HZ;
  86. /*
  87. * We use machine-unique IDs for our client connections.
  88. */
  89. DEFINE_IDR(rxrpc_client_conn_ids);
  90. static DEFINE_SPINLOCK(rxrpc_conn_id_lock);
  91. static void rxrpc_cull_active_client_conns(struct rxrpc_net *);
  92. /*
  93. * Get a connection ID and epoch for a client connection from the global pool.
  94. * The connection struct pointer is then recorded in the idr radix tree. The
  95. * epoch doesn't change until the client is rebooted (or, at least, unless the
  96. * module is unloaded).
  97. */
  98. static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn,
  99. gfp_t gfp)
  100. {
  101. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  102. int id;
  103. _enter("");
  104. idr_preload(gfp);
  105. spin_lock(&rxrpc_conn_id_lock);
  106. id = idr_alloc_cyclic(&rxrpc_client_conn_ids, conn,
  107. 1, 0x40000000, GFP_NOWAIT);
  108. if (id < 0)
  109. goto error;
  110. spin_unlock(&rxrpc_conn_id_lock);
  111. idr_preload_end();
  112. conn->proto.epoch = rxnet->epoch;
  113. conn->proto.cid = id << RXRPC_CIDSHIFT;
  114. set_bit(RXRPC_CONN_HAS_IDR, &conn->flags);
  115. _leave(" [CID %x]", conn->proto.cid);
  116. return 0;
  117. error:
  118. spin_unlock(&rxrpc_conn_id_lock);
  119. idr_preload_end();
  120. _leave(" = %d", id);
  121. return id;
  122. }
  123. /*
  124. * Release a connection ID for a client connection from the global pool.
  125. */
  126. static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn)
  127. {
  128. if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) {
  129. spin_lock(&rxrpc_conn_id_lock);
  130. idr_remove(&rxrpc_client_conn_ids,
  131. conn->proto.cid >> RXRPC_CIDSHIFT);
  132. spin_unlock(&rxrpc_conn_id_lock);
  133. }
  134. }
  135. /*
  136. * Destroy the client connection ID tree.
  137. */
  138. void rxrpc_destroy_client_conn_ids(void)
  139. {
  140. struct rxrpc_connection *conn;
  141. int id;
  142. if (!idr_is_empty(&rxrpc_client_conn_ids)) {
  143. idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) {
  144. pr_err("AF_RXRPC: Leaked client conn %p {%d}\n",
  145. conn, atomic_read(&conn->usage));
  146. }
  147. BUG();
  148. }
  149. idr_destroy(&rxrpc_client_conn_ids);
  150. }
  151. /*
  152. * Allocate a client connection.
  153. */
  154. static struct rxrpc_connection *
  155. rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp)
  156. {
  157. struct rxrpc_connection *conn;
  158. struct rxrpc_net *rxnet = cp->local->rxnet;
  159. int ret;
  160. _enter("");
  161. conn = rxrpc_alloc_connection(gfp);
  162. if (!conn) {
  163. _leave(" = -ENOMEM");
  164. return ERR_PTR(-ENOMEM);
  165. }
  166. atomic_set(&conn->usage, 1);
  167. if (cp->exclusive)
  168. __set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  169. if (cp->upgrade)
  170. __set_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  171. conn->params = *cp;
  172. conn->out_clientflag = RXRPC_CLIENT_INITIATED;
  173. conn->state = RXRPC_CONN_CLIENT;
  174. conn->service_id = cp->service_id;
  175. ret = rxrpc_get_client_connection_id(conn, gfp);
  176. if (ret < 0)
  177. goto error_0;
  178. ret = rxrpc_init_client_conn_security(conn);
  179. if (ret < 0)
  180. goto error_1;
  181. ret = conn->security->prime_packet_security(conn);
  182. if (ret < 0)
  183. goto error_2;
  184. atomic_inc(&rxnet->nr_conns);
  185. write_lock(&rxnet->conn_lock);
  186. list_add_tail(&conn->proc_link, &rxnet->conn_proc_list);
  187. write_unlock(&rxnet->conn_lock);
  188. /* We steal the caller's peer ref. */
  189. cp->peer = NULL;
  190. rxrpc_get_local(conn->params.local);
  191. key_get(conn->params.key);
  192. trace_rxrpc_conn(conn, rxrpc_conn_new_client, atomic_read(&conn->usage),
  193. __builtin_return_address(0));
  194. trace_rxrpc_client(conn, -1, rxrpc_client_alloc);
  195. _leave(" = %p", conn);
  196. return conn;
  197. error_2:
  198. conn->security->clear(conn);
  199. error_1:
  200. rxrpc_put_client_connection_id(conn);
  201. error_0:
  202. kfree(conn);
  203. _leave(" = %d", ret);
  204. return ERR_PTR(ret);
  205. }
  206. /*
  207. * Determine if a connection may be reused.
  208. */
  209. static bool rxrpc_may_reuse_conn(struct rxrpc_connection *conn)
  210. {
  211. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  212. int id_cursor, id, distance, limit;
  213. if (test_bit(RXRPC_CONN_DONT_REUSE, &conn->flags))
  214. goto dont_reuse;
  215. if (conn->proto.epoch != rxnet->epoch)
  216. goto mark_dont_reuse;
  217. /* The IDR tree gets very expensive on memory if the connection IDs are
  218. * widely scattered throughout the number space, so we shall want to
  219. * kill off connections that, say, have an ID more than about four
  220. * times the maximum number of client conns away from the current
  221. * allocation point to try and keep the IDs concentrated.
  222. */
  223. id_cursor = idr_get_cursor(&rxrpc_client_conn_ids);
  224. id = conn->proto.cid >> RXRPC_CIDSHIFT;
  225. distance = id - id_cursor;
  226. if (distance < 0)
  227. distance = -distance;
  228. limit = max(rxrpc_max_client_connections * 4, 1024U);
  229. if (distance > limit)
  230. goto mark_dont_reuse;
  231. return true;
  232. mark_dont_reuse:
  233. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  234. dont_reuse:
  235. return false;
  236. }
  237. /*
  238. * Create or find a client connection to use for a call.
  239. *
  240. * If we return with a connection, the call will be on its waiting list. It's
  241. * left to the caller to assign a channel and wake up the call.
  242. */
  243. static int rxrpc_get_client_conn(struct rxrpc_call *call,
  244. struct rxrpc_conn_parameters *cp,
  245. struct sockaddr_rxrpc *srx,
  246. gfp_t gfp)
  247. {
  248. struct rxrpc_connection *conn, *candidate = NULL;
  249. struct rxrpc_local *local = cp->local;
  250. struct rb_node *p, **pp, *parent;
  251. long diff;
  252. int ret = -ENOMEM;
  253. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  254. cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp);
  255. if (!cp->peer)
  256. goto error;
  257. call->cong_cwnd = cp->peer->cong_cwnd;
  258. if (call->cong_cwnd >= call->cong_ssthresh)
  259. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  260. else
  261. call->cong_mode = RXRPC_CALL_SLOW_START;
  262. /* If the connection is not meant to be exclusive, search the available
  263. * connections to see if the connection we want to use already exists.
  264. */
  265. if (!cp->exclusive) {
  266. _debug("search 1");
  267. spin_lock(&local->client_conns_lock);
  268. p = local->client_conns.rb_node;
  269. while (p) {
  270. conn = rb_entry(p, struct rxrpc_connection, client_node);
  271. #define cmp(X) ((long)conn->params.X - (long)cp->X)
  272. diff = (cmp(peer) ?:
  273. cmp(key) ?:
  274. cmp(security_level) ?:
  275. cmp(upgrade));
  276. #undef cmp
  277. if (diff < 0) {
  278. p = p->rb_left;
  279. } else if (diff > 0) {
  280. p = p->rb_right;
  281. } else {
  282. if (rxrpc_may_reuse_conn(conn) &&
  283. rxrpc_get_connection_maybe(conn))
  284. goto found_extant_conn;
  285. /* The connection needs replacing. It's better
  286. * to effect that when we have something to
  287. * replace it with so that we don't have to
  288. * rebalance the tree twice.
  289. */
  290. break;
  291. }
  292. }
  293. spin_unlock(&local->client_conns_lock);
  294. }
  295. /* There wasn't a connection yet or we need an exclusive connection.
  296. * We need to create a candidate and then potentially redo the search
  297. * in case we're racing with another thread also trying to connect on a
  298. * shareable connection.
  299. */
  300. _debug("new conn");
  301. candidate = rxrpc_alloc_client_connection(cp, gfp);
  302. if (IS_ERR(candidate)) {
  303. ret = PTR_ERR(candidate);
  304. goto error_peer;
  305. }
  306. /* Add the call to the new connection's waiting list in case we're
  307. * going to have to wait for the connection to come live. It's our
  308. * connection, so we want first dibs on the channel slots. We would
  309. * normally have to take channel_lock but we do this before anyone else
  310. * can see the connection.
  311. */
  312. list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
  313. if (cp->exclusive) {
  314. call->conn = candidate;
  315. call->security_ix = candidate->security_ix;
  316. call->service_id = candidate->service_id;
  317. _leave(" = 0 [exclusive %d]", candidate->debug_id);
  318. return 0;
  319. }
  320. /* Publish the new connection for userspace to find. We need to redo
  321. * the search before doing this lest we race with someone else adding a
  322. * conflicting instance.
  323. */
  324. _debug("search 2");
  325. spin_lock(&local->client_conns_lock);
  326. pp = &local->client_conns.rb_node;
  327. parent = NULL;
  328. while (*pp) {
  329. parent = *pp;
  330. conn = rb_entry(parent, struct rxrpc_connection, client_node);
  331. #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
  332. diff = (cmp(peer) ?:
  333. cmp(key) ?:
  334. cmp(security_level) ?:
  335. cmp(upgrade));
  336. #undef cmp
  337. if (diff < 0) {
  338. pp = &(*pp)->rb_left;
  339. } else if (diff > 0) {
  340. pp = &(*pp)->rb_right;
  341. } else {
  342. if (rxrpc_may_reuse_conn(conn) &&
  343. rxrpc_get_connection_maybe(conn))
  344. goto found_extant_conn;
  345. /* The old connection is from an outdated epoch. */
  346. _debug("replace conn");
  347. clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
  348. rb_replace_node(&conn->client_node,
  349. &candidate->client_node,
  350. &local->client_conns);
  351. trace_rxrpc_client(conn, -1, rxrpc_client_replace);
  352. goto candidate_published;
  353. }
  354. }
  355. _debug("new conn");
  356. rb_link_node(&candidate->client_node, parent, pp);
  357. rb_insert_color(&candidate->client_node, &local->client_conns);
  358. candidate_published:
  359. set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
  360. call->conn = candidate;
  361. call->security_ix = candidate->security_ix;
  362. call->service_id = candidate->service_id;
  363. spin_unlock(&local->client_conns_lock);
  364. _leave(" = 0 [new %d]", candidate->debug_id);
  365. return 0;
  366. /* We come here if we found a suitable connection already in existence.
  367. * Discard any candidate we may have allocated, and try to get a
  368. * channel on this one.
  369. */
  370. found_extant_conn:
  371. _debug("found conn");
  372. spin_unlock(&local->client_conns_lock);
  373. if (candidate) {
  374. trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
  375. rxrpc_put_connection(candidate);
  376. candidate = NULL;
  377. }
  378. spin_lock(&conn->channel_lock);
  379. call->conn = conn;
  380. call->security_ix = conn->security_ix;
  381. call->service_id = conn->service_id;
  382. list_add(&call->chan_wait_link, &conn->waiting_calls);
  383. spin_unlock(&conn->channel_lock);
  384. _leave(" = 0 [extant %d]", conn->debug_id);
  385. return 0;
  386. error_peer:
  387. rxrpc_put_peer(cp->peer);
  388. cp->peer = NULL;
  389. error:
  390. _leave(" = %d", ret);
  391. return ret;
  392. }
  393. /*
  394. * Activate a connection.
  395. */
  396. static void rxrpc_activate_conn(struct rxrpc_net *rxnet,
  397. struct rxrpc_connection *conn)
  398. {
  399. if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
  400. trace_rxrpc_client(conn, -1, rxrpc_client_to_upgrade);
  401. conn->cache_state = RXRPC_CONN_CLIENT_UPGRADE;
  402. } else {
  403. trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
  404. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  405. }
  406. rxnet->nr_active_client_conns++;
  407. list_move_tail(&conn->cache_link, &rxnet->active_client_conns);
  408. }
  409. /*
  410. * Attempt to animate a connection for a new call.
  411. *
  412. * If it's not exclusive, the connection is in the endpoint tree, and we're in
  413. * the conn's list of those waiting to grab a channel. There is, however, a
  414. * limit on the number of live connections allowed at any one time, so we may
  415. * have to wait for capacity to become available.
  416. *
  417. * Note that a connection on the waiting queue might *also* have active
  418. * channels if it has been culled to make space and then re-requested by a new
  419. * call.
  420. */
  421. static void rxrpc_animate_client_conn(struct rxrpc_net *rxnet,
  422. struct rxrpc_connection *conn)
  423. {
  424. unsigned int nr_conns;
  425. _enter("%d,%d", conn->debug_id, conn->cache_state);
  426. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE ||
  427. conn->cache_state == RXRPC_CONN_CLIENT_UPGRADE)
  428. goto out;
  429. spin_lock(&rxnet->client_conn_cache_lock);
  430. nr_conns = rxnet->nr_client_conns;
  431. if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  432. trace_rxrpc_client(conn, -1, rxrpc_client_count);
  433. rxnet->nr_client_conns = nr_conns + 1;
  434. }
  435. switch (conn->cache_state) {
  436. case RXRPC_CONN_CLIENT_ACTIVE:
  437. case RXRPC_CONN_CLIENT_UPGRADE:
  438. case RXRPC_CONN_CLIENT_WAITING:
  439. break;
  440. case RXRPC_CONN_CLIENT_INACTIVE:
  441. case RXRPC_CONN_CLIENT_CULLED:
  442. case RXRPC_CONN_CLIENT_IDLE:
  443. if (nr_conns >= rxrpc_max_client_connections)
  444. goto wait_for_capacity;
  445. goto activate_conn;
  446. default:
  447. BUG();
  448. }
  449. out_unlock:
  450. spin_unlock(&rxnet->client_conn_cache_lock);
  451. out:
  452. _leave(" [%d]", conn->cache_state);
  453. return;
  454. activate_conn:
  455. _debug("activate");
  456. rxrpc_activate_conn(rxnet, conn);
  457. goto out_unlock;
  458. wait_for_capacity:
  459. _debug("wait");
  460. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  461. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  462. list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns);
  463. goto out_unlock;
  464. }
  465. /*
  466. * Deactivate a channel.
  467. */
  468. static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
  469. unsigned int channel)
  470. {
  471. struct rxrpc_channel *chan = &conn->channels[channel];
  472. rcu_assign_pointer(chan->call, NULL);
  473. conn->active_chans &= ~(1 << channel);
  474. }
  475. /*
  476. * Assign a channel to the call at the front of the queue and wake the call up.
  477. * We don't increment the callNumber counter until this number has been exposed
  478. * to the world.
  479. */
  480. static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
  481. unsigned int channel)
  482. {
  483. struct rxrpc_channel *chan = &conn->channels[channel];
  484. struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
  485. struct rxrpc_call, chan_wait_link);
  486. u32 call_id = chan->call_counter + 1;
  487. trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
  488. /* Cancel the final ACK on the previous call if it hasn't been sent yet
  489. * as the DATA packet will implicitly ACK it.
  490. */
  491. clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  492. write_lock_bh(&call->state_lock);
  493. if (!test_bit(RXRPC_CALL_TX_LASTQ, &call->flags))
  494. call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
  495. else
  496. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  497. write_unlock_bh(&call->state_lock);
  498. rxrpc_see_call(call);
  499. list_del_init(&call->chan_wait_link);
  500. conn->active_chans |= 1 << channel;
  501. call->peer = rxrpc_get_peer(conn->params.peer);
  502. call->cid = conn->proto.cid | channel;
  503. call->call_id = call_id;
  504. trace_rxrpc_connect_call(call);
  505. _net("CONNECT call %08x:%08x as call %d on conn %d",
  506. call->cid, call->call_id, call->debug_id, conn->debug_id);
  507. /* Paired with the read barrier in rxrpc_wait_for_channel(). This
  508. * orders cid and epoch in the connection wrt to call_id without the
  509. * need to take the channel_lock.
  510. *
  511. * We provisionally assign a callNumber at this point, but we don't
  512. * confirm it until the call is about to be exposed.
  513. *
  514. * TODO: Pair with a barrier in the data_ready handler when that looks
  515. * at the call ID through a connection channel.
  516. */
  517. smp_wmb();
  518. chan->call_id = call_id;
  519. chan->call_debug_id = call->debug_id;
  520. rcu_assign_pointer(chan->call, call);
  521. wake_up(&call->waitq);
  522. }
  523. /*
  524. * Assign channels and callNumbers to waiting calls with channel_lock
  525. * held by caller.
  526. */
  527. static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
  528. {
  529. u8 avail, mask;
  530. switch (conn->cache_state) {
  531. case RXRPC_CONN_CLIENT_ACTIVE:
  532. mask = RXRPC_ACTIVE_CHANS_MASK;
  533. break;
  534. case RXRPC_CONN_CLIENT_UPGRADE:
  535. mask = 0x01;
  536. break;
  537. default:
  538. return;
  539. }
  540. while (!list_empty(&conn->waiting_calls) &&
  541. (avail = ~conn->active_chans,
  542. avail &= mask,
  543. avail != 0))
  544. rxrpc_activate_one_channel(conn, __ffs(avail));
  545. }
  546. /*
  547. * Assign channels and callNumbers to waiting calls.
  548. */
  549. static void rxrpc_activate_channels(struct rxrpc_connection *conn)
  550. {
  551. _enter("%d", conn->debug_id);
  552. trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
  553. if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
  554. return;
  555. spin_lock(&conn->channel_lock);
  556. rxrpc_activate_channels_locked(conn);
  557. spin_unlock(&conn->channel_lock);
  558. _leave("");
  559. }
  560. /*
  561. * Wait for a callNumber and a channel to be granted to a call.
  562. */
  563. static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
  564. {
  565. int ret = 0;
  566. _enter("%d", call->debug_id);
  567. if (!call->call_id) {
  568. DECLARE_WAITQUEUE(myself, current);
  569. if (!gfpflags_allow_blocking(gfp)) {
  570. ret = -EAGAIN;
  571. goto out;
  572. }
  573. add_wait_queue_exclusive(&call->waitq, &myself);
  574. for (;;) {
  575. set_current_state(TASK_INTERRUPTIBLE);
  576. if (call->call_id)
  577. break;
  578. if (signal_pending(current)) {
  579. ret = -ERESTARTSYS;
  580. break;
  581. }
  582. schedule();
  583. }
  584. remove_wait_queue(&call->waitq, &myself);
  585. __set_current_state(TASK_RUNNING);
  586. }
  587. /* Paired with the write barrier in rxrpc_activate_one_channel(). */
  588. smp_rmb();
  589. out:
  590. _leave(" = %d", ret);
  591. return ret;
  592. }
  593. /*
  594. * find a connection for a call
  595. * - called in process context with IRQs enabled
  596. */
  597. int rxrpc_connect_call(struct rxrpc_call *call,
  598. struct rxrpc_conn_parameters *cp,
  599. struct sockaddr_rxrpc *srx,
  600. gfp_t gfp)
  601. {
  602. struct rxrpc_net *rxnet = cp->local->rxnet;
  603. int ret;
  604. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  605. rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
  606. rxrpc_cull_active_client_conns(rxnet);
  607. ret = rxrpc_get_client_conn(call, cp, srx, gfp);
  608. if (ret < 0)
  609. goto out;
  610. rxrpc_animate_client_conn(rxnet, call->conn);
  611. rxrpc_activate_channels(call->conn);
  612. ret = rxrpc_wait_for_channel(call, gfp);
  613. if (ret < 0) {
  614. rxrpc_disconnect_client_call(call);
  615. goto out;
  616. }
  617. spin_lock_bh(&call->conn->params.peer->lock);
  618. hlist_add_head(&call->error_link,
  619. &call->conn->params.peer->error_targets);
  620. spin_unlock_bh(&call->conn->params.peer->lock);
  621. out:
  622. _leave(" = %d", ret);
  623. return ret;
  624. }
  625. /*
  626. * Note that a connection is about to be exposed to the world. Once it is
  627. * exposed, we maintain an extra ref on it that stops it from being summarily
  628. * discarded before it's (a) had a chance to deal with retransmission and (b)
  629. * had a chance at re-use (the per-connection security negotiation is
  630. * expensive).
  631. */
  632. static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
  633. unsigned int channel)
  634. {
  635. if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  636. trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
  637. rxrpc_get_connection(conn);
  638. }
  639. }
  640. /*
  641. * Note that a call, and thus a connection, is about to be exposed to the
  642. * world.
  643. */
  644. void rxrpc_expose_client_call(struct rxrpc_call *call)
  645. {
  646. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  647. struct rxrpc_connection *conn = call->conn;
  648. struct rxrpc_channel *chan = &conn->channels[channel];
  649. if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  650. /* Mark the call ID as being used. If the callNumber counter
  651. * exceeds ~2 billion, we kill the connection after its
  652. * outstanding calls have finished so that the counter doesn't
  653. * wrap.
  654. */
  655. chan->call_counter++;
  656. if (chan->call_counter >= INT_MAX)
  657. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  658. rxrpc_expose_client_conn(conn, channel);
  659. }
  660. }
  661. /*
  662. * Set the reap timer.
  663. */
  664. static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
  665. {
  666. unsigned long now = jiffies;
  667. unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
  668. if (rxnet->live)
  669. timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
  670. }
  671. /*
  672. * Disconnect a client call.
  673. */
  674. void rxrpc_disconnect_client_call(struct rxrpc_call *call)
  675. {
  676. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  677. struct rxrpc_connection *conn = call->conn;
  678. struct rxrpc_channel *chan = &conn->channels[channel];
  679. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  680. trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
  681. call->conn = NULL;
  682. spin_lock(&conn->channel_lock);
  683. /* Calls that have never actually been assigned a channel can simply be
  684. * discarded. If the conn didn't get used either, it will follow
  685. * immediately unless someone else grabs it in the meantime.
  686. */
  687. if (!list_empty(&call->chan_wait_link)) {
  688. _debug("call is waiting");
  689. ASSERTCMP(call->call_id, ==, 0);
  690. ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
  691. list_del_init(&call->chan_wait_link);
  692. trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
  693. /* We must deactivate or idle the connection if it's now
  694. * waiting for nothing.
  695. */
  696. spin_lock(&rxnet->client_conn_cache_lock);
  697. if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
  698. list_empty(&conn->waiting_calls) &&
  699. !conn->active_chans)
  700. goto idle_connection;
  701. goto out;
  702. }
  703. ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
  704. /* If a client call was exposed to the world, we save the result for
  705. * retransmission.
  706. *
  707. * We use a barrier here so that the call number and abort code can be
  708. * read without needing to take a lock.
  709. *
  710. * TODO: Make the incoming packet handler check this and handle
  711. * terminal retransmission without requiring access to the call.
  712. */
  713. if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  714. _debug("exposed %u,%u", call->call_id, call->abort_code);
  715. __rxrpc_disconnect_call(conn, call);
  716. }
  717. /* See if we can pass the channel directly to another call. */
  718. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
  719. !list_empty(&conn->waiting_calls)) {
  720. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  721. rxrpc_activate_one_channel(conn, channel);
  722. goto out_2;
  723. }
  724. /* Schedule the final ACK to be transmitted in a short while so that it
  725. * can be skipped if we find a follow-on call. The first DATA packet
  726. * of the follow on call will implicitly ACK this call.
  727. */
  728. if (call->completion == RXRPC_CALL_SUCCEEDED &&
  729. test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  730. unsigned long final_ack_at = jiffies + 2;
  731. WRITE_ONCE(chan->final_ack_at, final_ack_at);
  732. smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
  733. set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  734. rxrpc_reduce_conn_timer(conn, final_ack_at);
  735. }
  736. /* Things are more complex and we need the cache lock. We might be
  737. * able to simply idle the conn or it might now be lurking on the wait
  738. * list. It might even get moved back to the active list whilst we're
  739. * waiting for the lock.
  740. */
  741. spin_lock(&rxnet->client_conn_cache_lock);
  742. switch (conn->cache_state) {
  743. case RXRPC_CONN_CLIENT_UPGRADE:
  744. /* Deal with termination of a service upgrade probe. */
  745. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  746. clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  747. trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
  748. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  749. rxrpc_activate_channels_locked(conn);
  750. }
  751. /* fall through */
  752. case RXRPC_CONN_CLIENT_ACTIVE:
  753. if (list_empty(&conn->waiting_calls)) {
  754. rxrpc_deactivate_one_channel(conn, channel);
  755. if (!conn->active_chans) {
  756. rxnet->nr_active_client_conns--;
  757. goto idle_connection;
  758. }
  759. goto out;
  760. }
  761. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  762. rxrpc_activate_one_channel(conn, channel);
  763. goto out;
  764. case RXRPC_CONN_CLIENT_CULLED:
  765. rxrpc_deactivate_one_channel(conn, channel);
  766. ASSERT(list_empty(&conn->waiting_calls));
  767. if (!conn->active_chans)
  768. goto idle_connection;
  769. goto out;
  770. case RXRPC_CONN_CLIENT_WAITING:
  771. rxrpc_deactivate_one_channel(conn, channel);
  772. goto out;
  773. default:
  774. BUG();
  775. }
  776. out:
  777. spin_unlock(&rxnet->client_conn_cache_lock);
  778. out_2:
  779. spin_unlock(&conn->channel_lock);
  780. rxrpc_put_connection(conn);
  781. _leave("");
  782. return;
  783. idle_connection:
  784. /* As no channels remain active, the connection gets deactivated
  785. * immediately or moved to the idle list for a short while.
  786. */
  787. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  788. trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
  789. conn->idle_timestamp = jiffies;
  790. conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
  791. list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
  792. if (rxnet->idle_client_conns.next == &conn->cache_link &&
  793. !rxnet->kill_all_client_conns)
  794. rxrpc_set_client_reap_timer(rxnet);
  795. } else {
  796. trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
  797. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  798. list_del_init(&conn->cache_link);
  799. }
  800. goto out;
  801. }
  802. /*
  803. * Clean up a dead client connection.
  804. */
  805. static struct rxrpc_connection *
  806. rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
  807. {
  808. struct rxrpc_connection *next = NULL;
  809. struct rxrpc_local *local = conn->params.local;
  810. struct rxrpc_net *rxnet = local->rxnet;
  811. unsigned int nr_conns;
  812. trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
  813. if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
  814. spin_lock(&local->client_conns_lock);
  815. if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
  816. &conn->flags))
  817. rb_erase(&conn->client_node, &local->client_conns);
  818. spin_unlock(&local->client_conns_lock);
  819. }
  820. rxrpc_put_client_connection_id(conn);
  821. ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
  822. if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  823. trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
  824. spin_lock(&rxnet->client_conn_cache_lock);
  825. nr_conns = --rxnet->nr_client_conns;
  826. if (nr_conns < rxrpc_max_client_connections &&
  827. !list_empty(&rxnet->waiting_client_conns)) {
  828. next = list_entry(rxnet->waiting_client_conns.next,
  829. struct rxrpc_connection, cache_link);
  830. rxrpc_get_connection(next);
  831. rxrpc_activate_conn(rxnet, next);
  832. }
  833. spin_unlock(&rxnet->client_conn_cache_lock);
  834. }
  835. rxrpc_kill_connection(conn);
  836. if (next)
  837. rxrpc_activate_channels(next);
  838. /* We need to get rid of the temporary ref we took upon next, but we
  839. * can't call rxrpc_put_connection() recursively.
  840. */
  841. return next;
  842. }
  843. /*
  844. * Clean up a dead client connections.
  845. */
  846. void rxrpc_put_client_conn(struct rxrpc_connection *conn)
  847. {
  848. const void *here = __builtin_return_address(0);
  849. int n;
  850. do {
  851. n = atomic_dec_return(&conn->usage);
  852. trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
  853. if (n > 0)
  854. return;
  855. ASSERTCMP(n, >=, 0);
  856. conn = rxrpc_put_one_client_conn(conn);
  857. } while (conn);
  858. }
  859. /*
  860. * Kill the longest-active client connections to make room for new ones.
  861. */
  862. static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
  863. {
  864. struct rxrpc_connection *conn;
  865. unsigned int nr_conns = rxnet->nr_client_conns;
  866. unsigned int nr_active, limit;
  867. _enter("");
  868. ASSERTCMP(nr_conns, >=, 0);
  869. if (nr_conns < rxrpc_max_client_connections) {
  870. _leave(" [ok]");
  871. return;
  872. }
  873. limit = rxrpc_reap_client_connections;
  874. spin_lock(&rxnet->client_conn_cache_lock);
  875. nr_active = rxnet->nr_active_client_conns;
  876. while (nr_active > limit) {
  877. ASSERT(!list_empty(&rxnet->active_client_conns));
  878. conn = list_entry(rxnet->active_client_conns.next,
  879. struct rxrpc_connection, cache_link);
  880. ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE,
  881. conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE);
  882. if (list_empty(&conn->waiting_calls)) {
  883. trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
  884. conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
  885. list_del_init(&conn->cache_link);
  886. } else {
  887. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  888. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  889. list_move_tail(&conn->cache_link,
  890. &rxnet->waiting_client_conns);
  891. }
  892. nr_active--;
  893. }
  894. rxnet->nr_active_client_conns = nr_active;
  895. spin_unlock(&rxnet->client_conn_cache_lock);
  896. ASSERTCMP(nr_active, >=, 0);
  897. _leave(" [culled]");
  898. }
  899. /*
  900. * Discard expired client connections from the idle list. Each conn in the
  901. * idle list has been exposed and holds an extra ref because of that.
  902. *
  903. * This may be called from conn setup or from a work item so cannot be
  904. * considered non-reentrant.
  905. */
  906. void rxrpc_discard_expired_client_conns(struct work_struct *work)
  907. {
  908. struct rxrpc_connection *conn;
  909. struct rxrpc_net *rxnet =
  910. container_of(work, struct rxrpc_net, client_conn_reaper);
  911. unsigned long expiry, conn_expires_at, now;
  912. unsigned int nr_conns;
  913. _enter("");
  914. if (list_empty(&rxnet->idle_client_conns)) {
  915. _leave(" [empty]");
  916. return;
  917. }
  918. /* Don't double up on the discarding */
  919. if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
  920. _leave(" [already]");
  921. return;
  922. }
  923. /* We keep an estimate of what the number of conns ought to be after
  924. * we've discarded some so that we don't overdo the discarding.
  925. */
  926. nr_conns = rxnet->nr_client_conns;
  927. next:
  928. spin_lock(&rxnet->client_conn_cache_lock);
  929. if (list_empty(&rxnet->idle_client_conns))
  930. goto out;
  931. conn = list_entry(rxnet->idle_client_conns.next,
  932. struct rxrpc_connection, cache_link);
  933. ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
  934. if (!rxnet->kill_all_client_conns) {
  935. /* If the number of connections is over the reap limit, we
  936. * expedite discard by reducing the expiry timeout. We must,
  937. * however, have at least a short grace period to be able to do
  938. * final-ACK or ABORT retransmission.
  939. */
  940. expiry = rxrpc_conn_idle_client_expiry;
  941. if (nr_conns > rxrpc_reap_client_connections)
  942. expiry = rxrpc_conn_idle_client_fast_expiry;
  943. if (conn->params.local->service_closed)
  944. expiry = rxrpc_closed_conn_expiry * HZ;
  945. conn_expires_at = conn->idle_timestamp + expiry;
  946. now = READ_ONCE(jiffies);
  947. if (time_after(conn_expires_at, now))
  948. goto not_yet_expired;
  949. }
  950. trace_rxrpc_client(conn, -1, rxrpc_client_discard);
  951. if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
  952. BUG();
  953. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  954. list_del_init(&conn->cache_link);
  955. spin_unlock(&rxnet->client_conn_cache_lock);
  956. /* When we cleared the EXPOSED flag, we took on responsibility for the
  957. * reference that that had on the usage count. We deal with that here.
  958. * If someone re-sets the flag and re-gets the ref, that's fine.
  959. */
  960. rxrpc_put_connection(conn);
  961. nr_conns--;
  962. goto next;
  963. not_yet_expired:
  964. /* The connection at the front of the queue hasn't yet expired, so
  965. * schedule the work item for that point if we discarded something.
  966. *
  967. * We don't worry if the work item is already scheduled - it can look
  968. * after rescheduling itself at a later time. We could cancel it, but
  969. * then things get messier.
  970. */
  971. _debug("not yet");
  972. if (!rxnet->kill_all_client_conns)
  973. timer_reduce(&rxnet->client_conn_reap_timer,
  974. conn_expires_at);
  975. out:
  976. spin_unlock(&rxnet->client_conn_cache_lock);
  977. spin_unlock(&rxnet->client_conn_discard_lock);
  978. _leave("");
  979. }
  980. /*
  981. * Preemptively destroy all the client connection records rather than waiting
  982. * for them to time out
  983. */
  984. void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
  985. {
  986. _enter("");
  987. spin_lock(&rxnet->client_conn_cache_lock);
  988. rxnet->kill_all_client_conns = true;
  989. spin_unlock(&rxnet->client_conn_cache_lock);
  990. del_timer_sync(&rxnet->client_conn_reap_timer);
  991. if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
  992. _debug("destroy: queue failed");
  993. _leave("");
  994. }