conn_client.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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_sock *rx,
  244. struct rxrpc_call *call,
  245. struct rxrpc_conn_parameters *cp,
  246. struct sockaddr_rxrpc *srx,
  247. gfp_t gfp)
  248. {
  249. struct rxrpc_connection *conn, *candidate = NULL;
  250. struct rxrpc_local *local = cp->local;
  251. struct rb_node *p, **pp, *parent;
  252. long diff;
  253. int ret = -ENOMEM;
  254. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  255. cp->peer = rxrpc_lookup_peer(rx, cp->local, srx, gfp);
  256. if (!cp->peer)
  257. goto error;
  258. call->cong_cwnd = cp->peer->cong_cwnd;
  259. if (call->cong_cwnd >= call->cong_ssthresh)
  260. call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
  261. else
  262. call->cong_mode = RXRPC_CALL_SLOW_START;
  263. /* If the connection is not meant to be exclusive, search the available
  264. * connections to see if the connection we want to use already exists.
  265. */
  266. if (!cp->exclusive) {
  267. _debug("search 1");
  268. spin_lock(&local->client_conns_lock);
  269. p = local->client_conns.rb_node;
  270. while (p) {
  271. conn = rb_entry(p, struct rxrpc_connection, client_node);
  272. #define cmp(X) ((long)conn->params.X - (long)cp->X)
  273. diff = (cmp(peer) ?:
  274. cmp(key) ?:
  275. cmp(security_level) ?:
  276. cmp(upgrade));
  277. #undef cmp
  278. if (diff < 0) {
  279. p = p->rb_left;
  280. } else if (diff > 0) {
  281. p = p->rb_right;
  282. } else {
  283. if (rxrpc_may_reuse_conn(conn) &&
  284. rxrpc_get_connection_maybe(conn))
  285. goto found_extant_conn;
  286. /* The connection needs replacing. It's better
  287. * to effect that when we have something to
  288. * replace it with so that we don't have to
  289. * rebalance the tree twice.
  290. */
  291. break;
  292. }
  293. }
  294. spin_unlock(&local->client_conns_lock);
  295. }
  296. /* There wasn't a connection yet or we need an exclusive connection.
  297. * We need to create a candidate and then potentially redo the search
  298. * in case we're racing with another thread also trying to connect on a
  299. * shareable connection.
  300. */
  301. _debug("new conn");
  302. candidate = rxrpc_alloc_client_connection(cp, gfp);
  303. if (IS_ERR(candidate)) {
  304. ret = PTR_ERR(candidate);
  305. goto error_peer;
  306. }
  307. /* Add the call to the new connection's waiting list in case we're
  308. * going to have to wait for the connection to come live. It's our
  309. * connection, so we want first dibs on the channel slots. We would
  310. * normally have to take channel_lock but we do this before anyone else
  311. * can see the connection.
  312. */
  313. list_add_tail(&call->chan_wait_link, &candidate->waiting_calls);
  314. if (cp->exclusive) {
  315. call->conn = candidate;
  316. call->security_ix = candidate->security_ix;
  317. call->service_id = candidate->service_id;
  318. _leave(" = 0 [exclusive %d]", candidate->debug_id);
  319. return 0;
  320. }
  321. /* Publish the new connection for userspace to find. We need to redo
  322. * the search before doing this lest we race with someone else adding a
  323. * conflicting instance.
  324. */
  325. _debug("search 2");
  326. spin_lock(&local->client_conns_lock);
  327. pp = &local->client_conns.rb_node;
  328. parent = NULL;
  329. while (*pp) {
  330. parent = *pp;
  331. conn = rb_entry(parent, struct rxrpc_connection, client_node);
  332. #define cmp(X) ((long)conn->params.X - (long)candidate->params.X)
  333. diff = (cmp(peer) ?:
  334. cmp(key) ?:
  335. cmp(security_level) ?:
  336. cmp(upgrade));
  337. #undef cmp
  338. if (diff < 0) {
  339. pp = &(*pp)->rb_left;
  340. } else if (diff > 0) {
  341. pp = &(*pp)->rb_right;
  342. } else {
  343. if (rxrpc_may_reuse_conn(conn) &&
  344. rxrpc_get_connection_maybe(conn))
  345. goto found_extant_conn;
  346. /* The old connection is from an outdated epoch. */
  347. _debug("replace conn");
  348. clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags);
  349. rb_replace_node(&conn->client_node,
  350. &candidate->client_node,
  351. &local->client_conns);
  352. trace_rxrpc_client(conn, -1, rxrpc_client_replace);
  353. goto candidate_published;
  354. }
  355. }
  356. _debug("new conn");
  357. rb_link_node(&candidate->client_node, parent, pp);
  358. rb_insert_color(&candidate->client_node, &local->client_conns);
  359. candidate_published:
  360. set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags);
  361. call->conn = candidate;
  362. call->security_ix = candidate->security_ix;
  363. call->service_id = candidate->service_id;
  364. spin_unlock(&local->client_conns_lock);
  365. _leave(" = 0 [new %d]", candidate->debug_id);
  366. return 0;
  367. /* We come here if we found a suitable connection already in existence.
  368. * Discard any candidate we may have allocated, and try to get a
  369. * channel on this one.
  370. */
  371. found_extant_conn:
  372. _debug("found conn");
  373. spin_unlock(&local->client_conns_lock);
  374. if (candidate) {
  375. trace_rxrpc_client(candidate, -1, rxrpc_client_duplicate);
  376. rxrpc_put_connection(candidate);
  377. candidate = NULL;
  378. }
  379. spin_lock(&conn->channel_lock);
  380. call->conn = conn;
  381. call->security_ix = conn->security_ix;
  382. call->service_id = conn->service_id;
  383. list_add(&call->chan_wait_link, &conn->waiting_calls);
  384. spin_unlock(&conn->channel_lock);
  385. _leave(" = 0 [extant %d]", conn->debug_id);
  386. return 0;
  387. error_peer:
  388. rxrpc_put_peer(cp->peer);
  389. cp->peer = NULL;
  390. error:
  391. _leave(" = %d", ret);
  392. return ret;
  393. }
  394. /*
  395. * Activate a connection.
  396. */
  397. static void rxrpc_activate_conn(struct rxrpc_net *rxnet,
  398. struct rxrpc_connection *conn)
  399. {
  400. if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags)) {
  401. trace_rxrpc_client(conn, -1, rxrpc_client_to_upgrade);
  402. conn->cache_state = RXRPC_CONN_CLIENT_UPGRADE;
  403. } else {
  404. trace_rxrpc_client(conn, -1, rxrpc_client_to_active);
  405. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  406. }
  407. rxnet->nr_active_client_conns++;
  408. list_move_tail(&conn->cache_link, &rxnet->active_client_conns);
  409. }
  410. /*
  411. * Attempt to animate a connection for a new call.
  412. *
  413. * If it's not exclusive, the connection is in the endpoint tree, and we're in
  414. * the conn's list of those waiting to grab a channel. There is, however, a
  415. * limit on the number of live connections allowed at any one time, so we may
  416. * have to wait for capacity to become available.
  417. *
  418. * Note that a connection on the waiting queue might *also* have active
  419. * channels if it has been culled to make space and then re-requested by a new
  420. * call.
  421. */
  422. static void rxrpc_animate_client_conn(struct rxrpc_net *rxnet,
  423. struct rxrpc_connection *conn)
  424. {
  425. unsigned int nr_conns;
  426. _enter("%d,%d", conn->debug_id, conn->cache_state);
  427. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE ||
  428. conn->cache_state == RXRPC_CONN_CLIENT_UPGRADE)
  429. goto out;
  430. spin_lock(&rxnet->client_conn_cache_lock);
  431. nr_conns = rxnet->nr_client_conns;
  432. if (!test_and_set_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  433. trace_rxrpc_client(conn, -1, rxrpc_client_count);
  434. rxnet->nr_client_conns = nr_conns + 1;
  435. }
  436. switch (conn->cache_state) {
  437. case RXRPC_CONN_CLIENT_ACTIVE:
  438. case RXRPC_CONN_CLIENT_UPGRADE:
  439. case RXRPC_CONN_CLIENT_WAITING:
  440. break;
  441. case RXRPC_CONN_CLIENT_INACTIVE:
  442. case RXRPC_CONN_CLIENT_CULLED:
  443. case RXRPC_CONN_CLIENT_IDLE:
  444. if (nr_conns >= rxrpc_max_client_connections)
  445. goto wait_for_capacity;
  446. goto activate_conn;
  447. default:
  448. BUG();
  449. }
  450. out_unlock:
  451. spin_unlock(&rxnet->client_conn_cache_lock);
  452. out:
  453. _leave(" [%d]", conn->cache_state);
  454. return;
  455. activate_conn:
  456. _debug("activate");
  457. rxrpc_activate_conn(rxnet, conn);
  458. goto out_unlock;
  459. wait_for_capacity:
  460. _debug("wait");
  461. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  462. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  463. list_move_tail(&conn->cache_link, &rxnet->waiting_client_conns);
  464. goto out_unlock;
  465. }
  466. /*
  467. * Deactivate a channel.
  468. */
  469. static void rxrpc_deactivate_one_channel(struct rxrpc_connection *conn,
  470. unsigned int channel)
  471. {
  472. struct rxrpc_channel *chan = &conn->channels[channel];
  473. rcu_assign_pointer(chan->call, NULL);
  474. conn->active_chans &= ~(1 << channel);
  475. }
  476. /*
  477. * Assign a channel to the call at the front of the queue and wake the call up.
  478. * We don't increment the callNumber counter until this number has been exposed
  479. * to the world.
  480. */
  481. static void rxrpc_activate_one_channel(struct rxrpc_connection *conn,
  482. unsigned int channel)
  483. {
  484. struct rxrpc_channel *chan = &conn->channels[channel];
  485. struct rxrpc_call *call = list_entry(conn->waiting_calls.next,
  486. struct rxrpc_call, chan_wait_link);
  487. u32 call_id = chan->call_counter + 1;
  488. trace_rxrpc_client(conn, channel, rxrpc_client_chan_activate);
  489. /* Cancel the final ACK on the previous call if it hasn't been sent yet
  490. * as the DATA packet will implicitly ACK it.
  491. */
  492. clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  493. write_lock_bh(&call->state_lock);
  494. if (!test_bit(RXRPC_CALL_TX_LASTQ, &call->flags))
  495. call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
  496. else
  497. call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
  498. write_unlock_bh(&call->state_lock);
  499. rxrpc_see_call(call);
  500. list_del_init(&call->chan_wait_link);
  501. conn->active_chans |= 1 << channel;
  502. call->peer = rxrpc_get_peer(conn->params.peer);
  503. call->cid = conn->proto.cid | channel;
  504. call->call_id = call_id;
  505. trace_rxrpc_connect_call(call);
  506. _net("CONNECT call %08x:%08x as call %d on conn %d",
  507. call->cid, call->call_id, call->debug_id, conn->debug_id);
  508. /* Paired with the read barrier in rxrpc_wait_for_channel(). This
  509. * orders cid and epoch in the connection wrt to call_id without the
  510. * need to take the channel_lock.
  511. *
  512. * We provisionally assign a callNumber at this point, but we don't
  513. * confirm it until the call is about to be exposed.
  514. *
  515. * TODO: Pair with a barrier in the data_ready handler when that looks
  516. * at the call ID through a connection channel.
  517. */
  518. smp_wmb();
  519. chan->call_id = call_id;
  520. chan->call_debug_id = call->debug_id;
  521. rcu_assign_pointer(chan->call, call);
  522. wake_up(&call->waitq);
  523. }
  524. /*
  525. * Assign channels and callNumbers to waiting calls with channel_lock
  526. * held by caller.
  527. */
  528. static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
  529. {
  530. u8 avail, mask;
  531. switch (conn->cache_state) {
  532. case RXRPC_CONN_CLIENT_ACTIVE:
  533. mask = RXRPC_ACTIVE_CHANS_MASK;
  534. break;
  535. case RXRPC_CONN_CLIENT_UPGRADE:
  536. mask = 0x01;
  537. break;
  538. default:
  539. return;
  540. }
  541. while (!list_empty(&conn->waiting_calls) &&
  542. (avail = ~conn->active_chans,
  543. avail &= mask,
  544. avail != 0))
  545. rxrpc_activate_one_channel(conn, __ffs(avail));
  546. }
  547. /*
  548. * Assign channels and callNumbers to waiting calls.
  549. */
  550. static void rxrpc_activate_channels(struct rxrpc_connection *conn)
  551. {
  552. _enter("%d", conn->debug_id);
  553. trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
  554. if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
  555. return;
  556. spin_lock(&conn->channel_lock);
  557. rxrpc_activate_channels_locked(conn);
  558. spin_unlock(&conn->channel_lock);
  559. _leave("");
  560. }
  561. /*
  562. * Wait for a callNumber and a channel to be granted to a call.
  563. */
  564. static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
  565. {
  566. int ret = 0;
  567. _enter("%d", call->debug_id);
  568. if (!call->call_id) {
  569. DECLARE_WAITQUEUE(myself, current);
  570. if (!gfpflags_allow_blocking(gfp)) {
  571. ret = -EAGAIN;
  572. goto out;
  573. }
  574. add_wait_queue_exclusive(&call->waitq, &myself);
  575. for (;;) {
  576. set_current_state(TASK_INTERRUPTIBLE);
  577. if (call->call_id)
  578. break;
  579. if (signal_pending(current)) {
  580. ret = -ERESTARTSYS;
  581. break;
  582. }
  583. schedule();
  584. }
  585. remove_wait_queue(&call->waitq, &myself);
  586. __set_current_state(TASK_RUNNING);
  587. }
  588. /* Paired with the write barrier in rxrpc_activate_one_channel(). */
  589. smp_rmb();
  590. out:
  591. _leave(" = %d", ret);
  592. return ret;
  593. }
  594. /*
  595. * find a connection for a call
  596. * - called in process context with IRQs enabled
  597. */
  598. int rxrpc_connect_call(struct rxrpc_sock *rx,
  599. struct rxrpc_call *call,
  600. struct rxrpc_conn_parameters *cp,
  601. struct sockaddr_rxrpc *srx,
  602. gfp_t gfp)
  603. {
  604. struct rxrpc_net *rxnet = cp->local->rxnet;
  605. int ret;
  606. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  607. rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
  608. rxrpc_cull_active_client_conns(rxnet);
  609. ret = rxrpc_get_client_conn(rx, call, cp, srx, gfp);
  610. if (ret < 0)
  611. goto out;
  612. rxrpc_animate_client_conn(rxnet, call->conn);
  613. rxrpc_activate_channels(call->conn);
  614. ret = rxrpc_wait_for_channel(call, gfp);
  615. if (ret < 0) {
  616. rxrpc_disconnect_client_call(call);
  617. goto out;
  618. }
  619. spin_lock_bh(&call->conn->params.peer->lock);
  620. hlist_add_head_rcu(&call->error_link,
  621. &call->conn->params.peer->error_targets);
  622. spin_unlock_bh(&call->conn->params.peer->lock);
  623. out:
  624. _leave(" = %d", ret);
  625. return ret;
  626. }
  627. /*
  628. * Note that a connection is about to be exposed to the world. Once it is
  629. * exposed, we maintain an extra ref on it that stops it from being summarily
  630. * discarded before it's (a) had a chance to deal with retransmission and (b)
  631. * had a chance at re-use (the per-connection security negotiation is
  632. * expensive).
  633. */
  634. static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
  635. unsigned int channel)
  636. {
  637. if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  638. trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
  639. rxrpc_get_connection(conn);
  640. }
  641. }
  642. /*
  643. * Note that a call, and thus a connection, is about to be exposed to the
  644. * world.
  645. */
  646. void rxrpc_expose_client_call(struct rxrpc_call *call)
  647. {
  648. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  649. struct rxrpc_connection *conn = call->conn;
  650. struct rxrpc_channel *chan = &conn->channels[channel];
  651. if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  652. /* Mark the call ID as being used. If the callNumber counter
  653. * exceeds ~2 billion, we kill the connection after its
  654. * outstanding calls have finished so that the counter doesn't
  655. * wrap.
  656. */
  657. chan->call_counter++;
  658. if (chan->call_counter >= INT_MAX)
  659. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  660. rxrpc_expose_client_conn(conn, channel);
  661. }
  662. }
  663. /*
  664. * Set the reap timer.
  665. */
  666. static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
  667. {
  668. unsigned long now = jiffies;
  669. unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
  670. if (rxnet->live)
  671. timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
  672. }
  673. /*
  674. * Disconnect a client call.
  675. */
  676. void rxrpc_disconnect_client_call(struct rxrpc_call *call)
  677. {
  678. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  679. struct rxrpc_connection *conn = call->conn;
  680. struct rxrpc_channel *chan = &conn->channels[channel];
  681. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  682. trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
  683. call->conn = NULL;
  684. spin_lock(&conn->channel_lock);
  685. /* Calls that have never actually been assigned a channel can simply be
  686. * discarded. If the conn didn't get used either, it will follow
  687. * immediately unless someone else grabs it in the meantime.
  688. */
  689. if (!list_empty(&call->chan_wait_link)) {
  690. _debug("call is waiting");
  691. ASSERTCMP(call->call_id, ==, 0);
  692. ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
  693. list_del_init(&call->chan_wait_link);
  694. trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
  695. /* We must deactivate or idle the connection if it's now
  696. * waiting for nothing.
  697. */
  698. spin_lock(&rxnet->client_conn_cache_lock);
  699. if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
  700. list_empty(&conn->waiting_calls) &&
  701. !conn->active_chans)
  702. goto idle_connection;
  703. goto out;
  704. }
  705. ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
  706. /* If a client call was exposed to the world, we save the result for
  707. * retransmission.
  708. *
  709. * We use a barrier here so that the call number and abort code can be
  710. * read without needing to take a lock.
  711. *
  712. * TODO: Make the incoming packet handler check this and handle
  713. * terminal retransmission without requiring access to the call.
  714. */
  715. if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  716. _debug("exposed %u,%u", call->call_id, call->abort_code);
  717. __rxrpc_disconnect_call(conn, call);
  718. }
  719. /* See if we can pass the channel directly to another call. */
  720. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
  721. !list_empty(&conn->waiting_calls)) {
  722. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  723. rxrpc_activate_one_channel(conn, channel);
  724. goto out_2;
  725. }
  726. /* Schedule the final ACK to be transmitted in a short while so that it
  727. * can be skipped if we find a follow-on call. The first DATA packet
  728. * of the follow on call will implicitly ACK this call.
  729. */
  730. if (call->completion == RXRPC_CALL_SUCCEEDED &&
  731. test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  732. unsigned long final_ack_at = jiffies + 2;
  733. WRITE_ONCE(chan->final_ack_at, final_ack_at);
  734. smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
  735. set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  736. rxrpc_reduce_conn_timer(conn, final_ack_at);
  737. }
  738. /* Things are more complex and we need the cache lock. We might be
  739. * able to simply idle the conn or it might now be lurking on the wait
  740. * list. It might even get moved back to the active list whilst we're
  741. * waiting for the lock.
  742. */
  743. spin_lock(&rxnet->client_conn_cache_lock);
  744. switch (conn->cache_state) {
  745. case RXRPC_CONN_CLIENT_UPGRADE:
  746. /* Deal with termination of a service upgrade probe. */
  747. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  748. clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  749. trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
  750. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  751. rxrpc_activate_channels_locked(conn);
  752. }
  753. /* fall through */
  754. case RXRPC_CONN_CLIENT_ACTIVE:
  755. if (list_empty(&conn->waiting_calls)) {
  756. rxrpc_deactivate_one_channel(conn, channel);
  757. if (!conn->active_chans) {
  758. rxnet->nr_active_client_conns--;
  759. goto idle_connection;
  760. }
  761. goto out;
  762. }
  763. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  764. rxrpc_activate_one_channel(conn, channel);
  765. goto out;
  766. case RXRPC_CONN_CLIENT_CULLED:
  767. rxrpc_deactivate_one_channel(conn, channel);
  768. ASSERT(list_empty(&conn->waiting_calls));
  769. if (!conn->active_chans)
  770. goto idle_connection;
  771. goto out;
  772. case RXRPC_CONN_CLIENT_WAITING:
  773. rxrpc_deactivate_one_channel(conn, channel);
  774. goto out;
  775. default:
  776. BUG();
  777. }
  778. out:
  779. spin_unlock(&rxnet->client_conn_cache_lock);
  780. out_2:
  781. spin_unlock(&conn->channel_lock);
  782. rxrpc_put_connection(conn);
  783. _leave("");
  784. return;
  785. idle_connection:
  786. /* As no channels remain active, the connection gets deactivated
  787. * immediately or moved to the idle list for a short while.
  788. */
  789. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  790. trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
  791. conn->idle_timestamp = jiffies;
  792. conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
  793. list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
  794. if (rxnet->idle_client_conns.next == &conn->cache_link &&
  795. !rxnet->kill_all_client_conns)
  796. rxrpc_set_client_reap_timer(rxnet);
  797. } else {
  798. trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
  799. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  800. list_del_init(&conn->cache_link);
  801. }
  802. goto out;
  803. }
  804. /*
  805. * Clean up a dead client connection.
  806. */
  807. static struct rxrpc_connection *
  808. rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
  809. {
  810. struct rxrpc_connection *next = NULL;
  811. struct rxrpc_local *local = conn->params.local;
  812. struct rxrpc_net *rxnet = local->rxnet;
  813. unsigned int nr_conns;
  814. trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
  815. if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
  816. spin_lock(&local->client_conns_lock);
  817. if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
  818. &conn->flags))
  819. rb_erase(&conn->client_node, &local->client_conns);
  820. spin_unlock(&local->client_conns_lock);
  821. }
  822. rxrpc_put_client_connection_id(conn);
  823. ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
  824. if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  825. trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
  826. spin_lock(&rxnet->client_conn_cache_lock);
  827. nr_conns = --rxnet->nr_client_conns;
  828. if (nr_conns < rxrpc_max_client_connections &&
  829. !list_empty(&rxnet->waiting_client_conns)) {
  830. next = list_entry(rxnet->waiting_client_conns.next,
  831. struct rxrpc_connection, cache_link);
  832. rxrpc_get_connection(next);
  833. rxrpc_activate_conn(rxnet, next);
  834. }
  835. spin_unlock(&rxnet->client_conn_cache_lock);
  836. }
  837. rxrpc_kill_connection(conn);
  838. if (next)
  839. rxrpc_activate_channels(next);
  840. /* We need to get rid of the temporary ref we took upon next, but we
  841. * can't call rxrpc_put_connection() recursively.
  842. */
  843. return next;
  844. }
  845. /*
  846. * Clean up a dead client connections.
  847. */
  848. void rxrpc_put_client_conn(struct rxrpc_connection *conn)
  849. {
  850. const void *here = __builtin_return_address(0);
  851. int n;
  852. do {
  853. n = atomic_dec_return(&conn->usage);
  854. trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
  855. if (n > 0)
  856. return;
  857. ASSERTCMP(n, >=, 0);
  858. conn = rxrpc_put_one_client_conn(conn);
  859. } while (conn);
  860. }
  861. /*
  862. * Kill the longest-active client connections to make room for new ones.
  863. */
  864. static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
  865. {
  866. struct rxrpc_connection *conn;
  867. unsigned int nr_conns = rxnet->nr_client_conns;
  868. unsigned int nr_active, limit;
  869. _enter("");
  870. ASSERTCMP(nr_conns, >=, 0);
  871. if (nr_conns < rxrpc_max_client_connections) {
  872. _leave(" [ok]");
  873. return;
  874. }
  875. limit = rxrpc_reap_client_connections;
  876. spin_lock(&rxnet->client_conn_cache_lock);
  877. nr_active = rxnet->nr_active_client_conns;
  878. while (nr_active > limit) {
  879. ASSERT(!list_empty(&rxnet->active_client_conns));
  880. conn = list_entry(rxnet->active_client_conns.next,
  881. struct rxrpc_connection, cache_link);
  882. ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE,
  883. conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE);
  884. if (list_empty(&conn->waiting_calls)) {
  885. trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
  886. conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
  887. list_del_init(&conn->cache_link);
  888. } else {
  889. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  890. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  891. list_move_tail(&conn->cache_link,
  892. &rxnet->waiting_client_conns);
  893. }
  894. nr_active--;
  895. }
  896. rxnet->nr_active_client_conns = nr_active;
  897. spin_unlock(&rxnet->client_conn_cache_lock);
  898. ASSERTCMP(nr_active, >=, 0);
  899. _leave(" [culled]");
  900. }
  901. /*
  902. * Discard expired client connections from the idle list. Each conn in the
  903. * idle list has been exposed and holds an extra ref because of that.
  904. *
  905. * This may be called from conn setup or from a work item so cannot be
  906. * considered non-reentrant.
  907. */
  908. void rxrpc_discard_expired_client_conns(struct work_struct *work)
  909. {
  910. struct rxrpc_connection *conn;
  911. struct rxrpc_net *rxnet =
  912. container_of(work, struct rxrpc_net, client_conn_reaper);
  913. unsigned long expiry, conn_expires_at, now;
  914. unsigned int nr_conns;
  915. _enter("");
  916. if (list_empty(&rxnet->idle_client_conns)) {
  917. _leave(" [empty]");
  918. return;
  919. }
  920. /* Don't double up on the discarding */
  921. if (!spin_trylock(&rxnet->client_conn_discard_lock)) {
  922. _leave(" [already]");
  923. return;
  924. }
  925. /* We keep an estimate of what the number of conns ought to be after
  926. * we've discarded some so that we don't overdo the discarding.
  927. */
  928. nr_conns = rxnet->nr_client_conns;
  929. next:
  930. spin_lock(&rxnet->client_conn_cache_lock);
  931. if (list_empty(&rxnet->idle_client_conns))
  932. goto out;
  933. conn = list_entry(rxnet->idle_client_conns.next,
  934. struct rxrpc_connection, cache_link);
  935. ASSERT(test_bit(RXRPC_CONN_EXPOSED, &conn->flags));
  936. if (!rxnet->kill_all_client_conns) {
  937. /* If the number of connections is over the reap limit, we
  938. * expedite discard by reducing the expiry timeout. We must,
  939. * however, have at least a short grace period to be able to do
  940. * final-ACK or ABORT retransmission.
  941. */
  942. expiry = rxrpc_conn_idle_client_expiry;
  943. if (nr_conns > rxrpc_reap_client_connections)
  944. expiry = rxrpc_conn_idle_client_fast_expiry;
  945. if (conn->params.local->service_closed)
  946. expiry = rxrpc_closed_conn_expiry * HZ;
  947. conn_expires_at = conn->idle_timestamp + expiry;
  948. now = READ_ONCE(jiffies);
  949. if (time_after(conn_expires_at, now))
  950. goto not_yet_expired;
  951. }
  952. trace_rxrpc_client(conn, -1, rxrpc_client_discard);
  953. if (!test_and_clear_bit(RXRPC_CONN_EXPOSED, &conn->flags))
  954. BUG();
  955. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  956. list_del_init(&conn->cache_link);
  957. spin_unlock(&rxnet->client_conn_cache_lock);
  958. /* When we cleared the EXPOSED flag, we took on responsibility for the
  959. * reference that that had on the usage count. We deal with that here.
  960. * If someone re-sets the flag and re-gets the ref, that's fine.
  961. */
  962. rxrpc_put_connection(conn);
  963. nr_conns--;
  964. goto next;
  965. not_yet_expired:
  966. /* The connection at the front of the queue hasn't yet expired, so
  967. * schedule the work item for that point if we discarded something.
  968. *
  969. * We don't worry if the work item is already scheduled - it can look
  970. * after rescheduling itself at a later time. We could cancel it, but
  971. * then things get messier.
  972. */
  973. _debug("not yet");
  974. if (!rxnet->kill_all_client_conns)
  975. timer_reduce(&rxnet->client_conn_reap_timer,
  976. conn_expires_at);
  977. out:
  978. spin_unlock(&rxnet->client_conn_cache_lock);
  979. spin_unlock(&rxnet->client_conn_discard_lock);
  980. _leave("");
  981. }
  982. /*
  983. * Preemptively destroy all the client connection records rather than waiting
  984. * for them to time out
  985. */
  986. void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
  987. {
  988. _enter("");
  989. spin_lock(&rxnet->client_conn_cache_lock);
  990. rxnet->kill_all_client_conns = true;
  991. spin_unlock(&rxnet->client_conn_cache_lock);
  992. del_timer_sync(&rxnet->client_conn_reap_timer);
  993. if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
  994. _debug("destroy: queue failed");
  995. _leave("");
  996. }