conn_client.c 32 KB

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