conn_client.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. rcu_assign_pointer(chan->call, call);
  520. wake_up(&call->waitq);
  521. }
  522. /*
  523. * Assign channels and callNumbers to waiting calls with channel_lock
  524. * held by caller.
  525. */
  526. static void rxrpc_activate_channels_locked(struct rxrpc_connection *conn)
  527. {
  528. u8 avail, mask;
  529. switch (conn->cache_state) {
  530. case RXRPC_CONN_CLIENT_ACTIVE:
  531. mask = RXRPC_ACTIVE_CHANS_MASK;
  532. break;
  533. case RXRPC_CONN_CLIENT_UPGRADE:
  534. mask = 0x01;
  535. break;
  536. default:
  537. return;
  538. }
  539. while (!list_empty(&conn->waiting_calls) &&
  540. (avail = ~conn->active_chans,
  541. avail &= mask,
  542. avail != 0))
  543. rxrpc_activate_one_channel(conn, __ffs(avail));
  544. }
  545. /*
  546. * Assign channels and callNumbers to waiting calls.
  547. */
  548. static void rxrpc_activate_channels(struct rxrpc_connection *conn)
  549. {
  550. _enter("%d", conn->debug_id);
  551. trace_rxrpc_client(conn, -1, rxrpc_client_activate_chans);
  552. if (conn->active_chans == RXRPC_ACTIVE_CHANS_MASK)
  553. return;
  554. spin_lock(&conn->channel_lock);
  555. rxrpc_activate_channels_locked(conn);
  556. spin_unlock(&conn->channel_lock);
  557. _leave("");
  558. }
  559. /*
  560. * Wait for a callNumber and a channel to be granted to a call.
  561. */
  562. static int rxrpc_wait_for_channel(struct rxrpc_call *call, gfp_t gfp)
  563. {
  564. int ret = 0;
  565. _enter("%d", call->debug_id);
  566. if (!call->call_id) {
  567. DECLARE_WAITQUEUE(myself, current);
  568. if (!gfpflags_allow_blocking(gfp)) {
  569. ret = -EAGAIN;
  570. goto out;
  571. }
  572. add_wait_queue_exclusive(&call->waitq, &myself);
  573. for (;;) {
  574. set_current_state(TASK_INTERRUPTIBLE);
  575. if (call->call_id)
  576. break;
  577. if (signal_pending(current)) {
  578. ret = -ERESTARTSYS;
  579. break;
  580. }
  581. schedule();
  582. }
  583. remove_wait_queue(&call->waitq, &myself);
  584. __set_current_state(TASK_RUNNING);
  585. }
  586. /* Paired with the write barrier in rxrpc_activate_one_channel(). */
  587. smp_rmb();
  588. out:
  589. _leave(" = %d", ret);
  590. return ret;
  591. }
  592. /*
  593. * find a connection for a call
  594. * - called in process context with IRQs enabled
  595. */
  596. int rxrpc_connect_call(struct rxrpc_call *call,
  597. struct rxrpc_conn_parameters *cp,
  598. struct sockaddr_rxrpc *srx,
  599. gfp_t gfp)
  600. {
  601. struct rxrpc_net *rxnet = cp->local->rxnet;
  602. int ret;
  603. _enter("{%d,%lx},", call->debug_id, call->user_call_ID);
  604. rxrpc_discard_expired_client_conns(&rxnet->client_conn_reaper);
  605. rxrpc_cull_active_client_conns(rxnet);
  606. ret = rxrpc_get_client_conn(call, cp, srx, gfp);
  607. if (ret < 0)
  608. goto out;
  609. rxrpc_animate_client_conn(rxnet, call->conn);
  610. rxrpc_activate_channels(call->conn);
  611. ret = rxrpc_wait_for_channel(call, gfp);
  612. if (ret < 0) {
  613. rxrpc_disconnect_client_call(call);
  614. goto out;
  615. }
  616. spin_lock_bh(&call->conn->params.peer->lock);
  617. hlist_add_head(&call->error_link,
  618. &call->conn->params.peer->error_targets);
  619. spin_unlock_bh(&call->conn->params.peer->lock);
  620. out:
  621. _leave(" = %d", ret);
  622. return ret;
  623. }
  624. /*
  625. * Note that a connection is about to be exposed to the world. Once it is
  626. * exposed, we maintain an extra ref on it that stops it from being summarily
  627. * discarded before it's (a) had a chance to deal with retransmission and (b)
  628. * had a chance at re-use (the per-connection security negotiation is
  629. * expensive).
  630. */
  631. static void rxrpc_expose_client_conn(struct rxrpc_connection *conn,
  632. unsigned int channel)
  633. {
  634. if (!test_and_set_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  635. trace_rxrpc_client(conn, channel, rxrpc_client_exposed);
  636. rxrpc_get_connection(conn);
  637. }
  638. }
  639. /*
  640. * Note that a call, and thus a connection, is about to be exposed to the
  641. * world.
  642. */
  643. void rxrpc_expose_client_call(struct rxrpc_call *call)
  644. {
  645. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  646. struct rxrpc_connection *conn = call->conn;
  647. struct rxrpc_channel *chan = &conn->channels[channel];
  648. if (!test_and_set_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  649. /* Mark the call ID as being used. If the callNumber counter
  650. * exceeds ~2 billion, we kill the connection after its
  651. * outstanding calls have finished so that the counter doesn't
  652. * wrap.
  653. */
  654. chan->call_counter++;
  655. if (chan->call_counter >= INT_MAX)
  656. set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
  657. rxrpc_expose_client_conn(conn, channel);
  658. }
  659. }
  660. /*
  661. * Set the reap timer.
  662. */
  663. static void rxrpc_set_client_reap_timer(struct rxrpc_net *rxnet)
  664. {
  665. unsigned long now = jiffies;
  666. unsigned long reap_at = now + rxrpc_conn_idle_client_expiry;
  667. if (rxnet->live)
  668. timer_reduce(&rxnet->client_conn_reap_timer, reap_at);
  669. }
  670. /*
  671. * Disconnect a client call.
  672. */
  673. void rxrpc_disconnect_client_call(struct rxrpc_call *call)
  674. {
  675. unsigned int channel = call->cid & RXRPC_CHANNELMASK;
  676. struct rxrpc_connection *conn = call->conn;
  677. struct rxrpc_channel *chan = &conn->channels[channel];
  678. struct rxrpc_net *rxnet = conn->params.local->rxnet;
  679. trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
  680. call->conn = NULL;
  681. spin_lock(&conn->channel_lock);
  682. /* Calls that have never actually been assigned a channel can simply be
  683. * discarded. If the conn didn't get used either, it will follow
  684. * immediately unless someone else grabs it in the meantime.
  685. */
  686. if (!list_empty(&call->chan_wait_link)) {
  687. _debug("call is waiting");
  688. ASSERTCMP(call->call_id, ==, 0);
  689. ASSERT(!test_bit(RXRPC_CALL_EXPOSED, &call->flags));
  690. list_del_init(&call->chan_wait_link);
  691. trace_rxrpc_client(conn, channel, rxrpc_client_chan_unstarted);
  692. /* We must deactivate or idle the connection if it's now
  693. * waiting for nothing.
  694. */
  695. spin_lock(&rxnet->client_conn_cache_lock);
  696. if (conn->cache_state == RXRPC_CONN_CLIENT_WAITING &&
  697. list_empty(&conn->waiting_calls) &&
  698. !conn->active_chans)
  699. goto idle_connection;
  700. goto out;
  701. }
  702. ASSERTCMP(rcu_access_pointer(chan->call), ==, call);
  703. /* If a client call was exposed to the world, we save the result for
  704. * retransmission.
  705. *
  706. * We use a barrier here so that the call number and abort code can be
  707. * read without needing to take a lock.
  708. *
  709. * TODO: Make the incoming packet handler check this and handle
  710. * terminal retransmission without requiring access to the call.
  711. */
  712. if (test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  713. _debug("exposed %u,%u", call->call_id, call->abort_code);
  714. __rxrpc_disconnect_call(conn, call);
  715. }
  716. /* See if we can pass the channel directly to another call. */
  717. if (conn->cache_state == RXRPC_CONN_CLIENT_ACTIVE &&
  718. !list_empty(&conn->waiting_calls)) {
  719. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  720. rxrpc_activate_one_channel(conn, channel);
  721. goto out_2;
  722. }
  723. /* Schedule the final ACK to be transmitted in a short while so that it
  724. * can be skipped if we find a follow-on call. The first DATA packet
  725. * of the follow on call will implicitly ACK this call.
  726. */
  727. if (call->completion == RXRPC_CALL_SUCCEEDED &&
  728. test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  729. unsigned long final_ack_at = jiffies + 2;
  730. WRITE_ONCE(chan->final_ack_at, final_ack_at);
  731. smp_wmb(); /* vs rxrpc_process_delayed_final_acks() */
  732. set_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags);
  733. rxrpc_reduce_conn_timer(conn, final_ack_at);
  734. }
  735. /* Things are more complex and we need the cache lock. We might be
  736. * able to simply idle the conn or it might now be lurking on the wait
  737. * list. It might even get moved back to the active list whilst we're
  738. * waiting for the lock.
  739. */
  740. spin_lock(&rxnet->client_conn_cache_lock);
  741. switch (conn->cache_state) {
  742. case RXRPC_CONN_CLIENT_UPGRADE:
  743. /* Deal with termination of a service upgrade probe. */
  744. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  745. clear_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags);
  746. trace_rxrpc_client(conn, channel, rxrpc_client_to_active);
  747. conn->cache_state = RXRPC_CONN_CLIENT_ACTIVE;
  748. rxrpc_activate_channels_locked(conn);
  749. }
  750. /* fall through */
  751. case RXRPC_CONN_CLIENT_ACTIVE:
  752. if (list_empty(&conn->waiting_calls)) {
  753. rxrpc_deactivate_one_channel(conn, channel);
  754. if (!conn->active_chans) {
  755. rxnet->nr_active_client_conns--;
  756. goto idle_connection;
  757. }
  758. goto out;
  759. }
  760. trace_rxrpc_client(conn, channel, rxrpc_client_chan_pass);
  761. rxrpc_activate_one_channel(conn, channel);
  762. goto out;
  763. case RXRPC_CONN_CLIENT_CULLED:
  764. rxrpc_deactivate_one_channel(conn, channel);
  765. ASSERT(list_empty(&conn->waiting_calls));
  766. if (!conn->active_chans)
  767. goto idle_connection;
  768. goto out;
  769. case RXRPC_CONN_CLIENT_WAITING:
  770. rxrpc_deactivate_one_channel(conn, channel);
  771. goto out;
  772. default:
  773. BUG();
  774. }
  775. out:
  776. spin_unlock(&rxnet->client_conn_cache_lock);
  777. out_2:
  778. spin_unlock(&conn->channel_lock);
  779. rxrpc_put_connection(conn);
  780. _leave("");
  781. return;
  782. idle_connection:
  783. /* As no channels remain active, the connection gets deactivated
  784. * immediately or moved to the idle list for a short while.
  785. */
  786. if (test_bit(RXRPC_CONN_EXPOSED, &conn->flags)) {
  787. trace_rxrpc_client(conn, channel, rxrpc_client_to_idle);
  788. conn->idle_timestamp = jiffies;
  789. conn->cache_state = RXRPC_CONN_CLIENT_IDLE;
  790. list_move_tail(&conn->cache_link, &rxnet->idle_client_conns);
  791. if (rxnet->idle_client_conns.next == &conn->cache_link &&
  792. !rxnet->kill_all_client_conns)
  793. rxrpc_set_client_reap_timer(rxnet);
  794. } else {
  795. trace_rxrpc_client(conn, channel, rxrpc_client_to_inactive);
  796. conn->cache_state = RXRPC_CONN_CLIENT_INACTIVE;
  797. list_del_init(&conn->cache_link);
  798. }
  799. goto out;
  800. }
  801. /*
  802. * Clean up a dead client connection.
  803. */
  804. static struct rxrpc_connection *
  805. rxrpc_put_one_client_conn(struct rxrpc_connection *conn)
  806. {
  807. struct rxrpc_connection *next = NULL;
  808. struct rxrpc_local *local = conn->params.local;
  809. struct rxrpc_net *rxnet = local->rxnet;
  810. unsigned int nr_conns;
  811. trace_rxrpc_client(conn, -1, rxrpc_client_cleanup);
  812. if (test_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) {
  813. spin_lock(&local->client_conns_lock);
  814. if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS,
  815. &conn->flags))
  816. rb_erase(&conn->client_node, &local->client_conns);
  817. spin_unlock(&local->client_conns_lock);
  818. }
  819. rxrpc_put_client_connection_id(conn);
  820. ASSERTCMP(conn->cache_state, ==, RXRPC_CONN_CLIENT_INACTIVE);
  821. if (test_bit(RXRPC_CONN_COUNTED, &conn->flags)) {
  822. trace_rxrpc_client(conn, -1, rxrpc_client_uncount);
  823. spin_lock(&rxnet->client_conn_cache_lock);
  824. nr_conns = --rxnet->nr_client_conns;
  825. if (nr_conns < rxrpc_max_client_connections &&
  826. !list_empty(&rxnet->waiting_client_conns)) {
  827. next = list_entry(rxnet->waiting_client_conns.next,
  828. struct rxrpc_connection, cache_link);
  829. rxrpc_get_connection(next);
  830. rxrpc_activate_conn(rxnet, next);
  831. }
  832. spin_unlock(&rxnet->client_conn_cache_lock);
  833. }
  834. rxrpc_kill_connection(conn);
  835. if (next)
  836. rxrpc_activate_channels(next);
  837. /* We need to get rid of the temporary ref we took upon next, but we
  838. * can't call rxrpc_put_connection() recursively.
  839. */
  840. return next;
  841. }
  842. /*
  843. * Clean up a dead client connections.
  844. */
  845. void rxrpc_put_client_conn(struct rxrpc_connection *conn)
  846. {
  847. const void *here = __builtin_return_address(0);
  848. int n;
  849. do {
  850. n = atomic_dec_return(&conn->usage);
  851. trace_rxrpc_conn(conn, rxrpc_conn_put_client, n, here);
  852. if (n > 0)
  853. return;
  854. ASSERTCMP(n, >=, 0);
  855. conn = rxrpc_put_one_client_conn(conn);
  856. } while (conn);
  857. }
  858. /*
  859. * Kill the longest-active client connections to make room for new ones.
  860. */
  861. static void rxrpc_cull_active_client_conns(struct rxrpc_net *rxnet)
  862. {
  863. struct rxrpc_connection *conn;
  864. unsigned int nr_conns = rxnet->nr_client_conns;
  865. unsigned int nr_active, limit;
  866. _enter("");
  867. ASSERTCMP(nr_conns, >=, 0);
  868. if (nr_conns < rxrpc_max_client_connections) {
  869. _leave(" [ok]");
  870. return;
  871. }
  872. limit = rxrpc_reap_client_connections;
  873. spin_lock(&rxnet->client_conn_cache_lock);
  874. nr_active = rxnet->nr_active_client_conns;
  875. while (nr_active > limit) {
  876. ASSERT(!list_empty(&rxnet->active_client_conns));
  877. conn = list_entry(rxnet->active_client_conns.next,
  878. struct rxrpc_connection, cache_link);
  879. ASSERTIFCMP(conn->cache_state != RXRPC_CONN_CLIENT_ACTIVE,
  880. conn->cache_state, ==, RXRPC_CONN_CLIENT_UPGRADE);
  881. if (list_empty(&conn->waiting_calls)) {
  882. trace_rxrpc_client(conn, -1, rxrpc_client_to_culled);
  883. conn->cache_state = RXRPC_CONN_CLIENT_CULLED;
  884. list_del_init(&conn->cache_link);
  885. } else {
  886. trace_rxrpc_client(conn, -1, rxrpc_client_to_waiting);
  887. conn->cache_state = RXRPC_CONN_CLIENT_WAITING;
  888. list_move_tail(&conn->cache_link,
  889. &rxnet->waiting_client_conns);
  890. }
  891. nr_active--;
  892. }
  893. rxnet->nr_active_client_conns = nr_active;
  894. spin_unlock(&rxnet->client_conn_cache_lock);
  895. ASSERTCMP(nr_active, >=, 0);
  896. _leave(" [culled]");
  897. }
  898. /*
  899. * Discard expired client connections from the idle list. Each conn in the
  900. * idle list has been exposed and holds an extra ref because of that.
  901. *
  902. * This may be called from conn setup or from a work item so cannot be
  903. * considered non-reentrant.
  904. */
  905. void rxrpc_discard_expired_client_conns(struct work_struct *work)
  906. {
  907. struct rxrpc_connection *conn;
  908. struct rxrpc_net *rxnet =
  909. container_of(work, struct rxrpc_net, client_conn_reaper);
  910. unsigned long expiry, conn_expires_at, now;
  911. unsigned int nr_conns;
  912. bool did_discard = false;
  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. did_discard = true;
  962. nr_conns--;
  963. goto next;
  964. not_yet_expired:
  965. /* The connection at the front of the queue hasn't yet expired, so
  966. * schedule the work item for that point if we discarded something.
  967. *
  968. * We don't worry if the work item is already scheduled - it can look
  969. * after rescheduling itself at a later time. We could cancel it, but
  970. * then things get messier.
  971. */
  972. _debug("not yet");
  973. if (!rxnet->kill_all_client_conns)
  974. timer_reduce(&rxnet->client_conn_reap_timer,
  975. conn_expires_at);
  976. out:
  977. spin_unlock(&rxnet->client_conn_cache_lock);
  978. spin_unlock(&rxnet->client_conn_discard_lock);
  979. _leave("");
  980. }
  981. /*
  982. * Preemptively destroy all the client connection records rather than waiting
  983. * for them to time out
  984. */
  985. void rxrpc_destroy_all_client_connections(struct rxrpc_net *rxnet)
  986. {
  987. _enter("");
  988. spin_lock(&rxnet->client_conn_cache_lock);
  989. rxnet->kill_all_client_conns = true;
  990. spin_unlock(&rxnet->client_conn_cache_lock);
  991. del_timer_sync(&rxnet->client_conn_reap_timer);
  992. if (!rxrpc_queue_work(&rxnet->client_conn_reaper))
  993. _debug("destroy: queue failed");
  994. _leave("");
  995. }