2
0

conncache.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2016, Linus Nielsen Feltzing, <linus@haxx.se>
  9. * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #include <curl/curl.h>
  27. #include "urldata.h"
  28. #include "url.h"
  29. #include "progress.h"
  30. #include "multiif.h"
  31. #include "sendf.h"
  32. #include "conncache.h"
  33. #include "share.h"
  34. #include "sigpipe.h"
  35. #include "connect.h"
  36. #include "strcase.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. #define HASHKEY_SIZE 128
  42. static void conn_llist_dtor(void *user, void *element)
  43. {
  44. struct connectdata *conn = element;
  45. (void)user;
  46. conn->bundle = NULL;
  47. }
  48. static CURLcode bundle_create(struct connectbundle **bundlep)
  49. {
  50. DEBUGASSERT(*bundlep == NULL);
  51. *bundlep = malloc(sizeof(struct connectbundle));
  52. if(!*bundlep)
  53. return CURLE_OUT_OF_MEMORY;
  54. (*bundlep)->num_connections = 0;
  55. (*bundlep)->multiuse = BUNDLE_UNKNOWN;
  56. Curl_llist_init(&(*bundlep)->conn_list, (Curl_llist_dtor) conn_llist_dtor);
  57. return CURLE_OK;
  58. }
  59. static void bundle_destroy(struct connectbundle *bundle)
  60. {
  61. if(!bundle)
  62. return;
  63. Curl_llist_destroy(&bundle->conn_list, NULL);
  64. free(bundle);
  65. }
  66. /* Add a connection to a bundle */
  67. static void bundle_add_conn(struct connectbundle *bundle,
  68. struct connectdata *conn)
  69. {
  70. Curl_llist_insert_next(&bundle->conn_list, bundle->conn_list.tail, conn,
  71. &conn->bundle_node);
  72. conn->bundle = bundle;
  73. bundle->num_connections++;
  74. }
  75. /* Remove a connection from a bundle */
  76. static int bundle_remove_conn(struct connectbundle *bundle,
  77. struct connectdata *conn)
  78. {
  79. struct Curl_llist_element *curr;
  80. curr = bundle->conn_list.head;
  81. while(curr) {
  82. if(curr->ptr == conn) {
  83. Curl_llist_remove(&bundle->conn_list, curr, NULL);
  84. bundle->num_connections--;
  85. conn->bundle = NULL;
  86. return 1; /* we removed a handle */
  87. }
  88. curr = curr->next;
  89. }
  90. DEBUGASSERT(0);
  91. return 0;
  92. }
  93. static void free_bundle_hash_entry(void *freethis)
  94. {
  95. struct connectbundle *b = (struct connectbundle *) freethis;
  96. bundle_destroy(b);
  97. }
  98. int Curl_conncache_init(struct conncache *connc, int size)
  99. {
  100. /* allocate a new easy handle to use when closing cached connections */
  101. connc->closure_handle = curl_easy_init();
  102. if(!connc->closure_handle)
  103. return 1; /* bad */
  104. Curl_hash_init(&connc->hash, size, Curl_hash_str,
  105. Curl_str_key_compare, free_bundle_hash_entry);
  106. connc->closure_handle->state.conn_cache = connc;
  107. return 0; /* good */
  108. }
  109. void Curl_conncache_destroy(struct conncache *connc)
  110. {
  111. if(connc)
  112. Curl_hash_destroy(&connc->hash);
  113. }
  114. /* creates a key to find a bundle for this connection */
  115. static void hashkey(struct connectdata *conn, char *buf, size_t len)
  116. {
  117. const char *hostname;
  118. long port = conn->remote_port;
  119. DEBUGASSERT(len >= HASHKEY_SIZE);
  120. #ifndef CURL_DISABLE_PROXY
  121. if(conn->bits.httpproxy && !conn->bits.tunnel_proxy) {
  122. hostname = conn->http_proxy.host.name;
  123. port = conn->port;
  124. }
  125. else
  126. #endif
  127. if(conn->bits.conn_to_host)
  128. hostname = conn->conn_to_host.name;
  129. else
  130. hostname = conn->host.name;
  131. /* put the numbers first so that the hostname gets cut off if too long */
  132. #ifdef ENABLE_IPV6
  133. msnprintf(buf, len, "%u/%ld/%s", conn->scope_id, port, hostname);
  134. #else
  135. msnprintf(buf, len, "%ld/%s", port, hostname);
  136. #endif
  137. Curl_strntolower(buf, buf, len);
  138. }
  139. /* Returns number of connections currently held in the connection cache.
  140. Locks/unlocks the cache itself!
  141. */
  142. size_t Curl_conncache_size(struct Curl_easy *data)
  143. {
  144. size_t num;
  145. CONNCACHE_LOCK(data);
  146. num = data->state.conn_cache->num_conn;
  147. CONNCACHE_UNLOCK(data);
  148. return num;
  149. }
  150. /* Look up the bundle with all the connections to the same host this
  151. connectdata struct is setup to use.
  152. **NOTE**: When it returns, it holds the connection cache lock! */
  153. struct connectbundle *
  154. Curl_conncache_find_bundle(struct Curl_easy *data,
  155. struct connectdata *conn,
  156. struct conncache *connc)
  157. {
  158. struct connectbundle *bundle = NULL;
  159. CONNCACHE_LOCK(data);
  160. if(connc) {
  161. char key[HASHKEY_SIZE];
  162. hashkey(conn, key, sizeof(key));
  163. bundle = Curl_hash_pick(&connc->hash, key, strlen(key));
  164. }
  165. return bundle;
  166. }
  167. static void *conncache_add_bundle(struct conncache *connc,
  168. char *key,
  169. struct connectbundle *bundle)
  170. {
  171. return Curl_hash_add(&connc->hash, key, strlen(key), bundle);
  172. }
  173. static void conncache_remove_bundle(struct conncache *connc,
  174. struct connectbundle *bundle)
  175. {
  176. struct Curl_hash_iterator iter;
  177. struct Curl_hash_element *he;
  178. if(!connc)
  179. return;
  180. Curl_hash_start_iterate(&connc->hash, &iter);
  181. he = Curl_hash_next_element(&iter);
  182. while(he) {
  183. if(he->ptr == bundle) {
  184. /* The bundle is destroyed by the hash destructor function,
  185. free_bundle_hash_entry() */
  186. Curl_hash_delete(&connc->hash, he->key, he->key_len);
  187. return;
  188. }
  189. he = Curl_hash_next_element(&iter);
  190. }
  191. }
  192. CURLcode Curl_conncache_add_conn(struct Curl_easy *data)
  193. {
  194. CURLcode result = CURLE_OK;
  195. struct connectbundle *bundle = NULL;
  196. struct connectdata *conn = data->conn;
  197. struct conncache *connc = data->state.conn_cache;
  198. DEBUGASSERT(conn);
  199. /* *find_bundle() locks the connection cache */
  200. bundle = Curl_conncache_find_bundle(data, conn, data->state.conn_cache);
  201. if(!bundle) {
  202. char key[HASHKEY_SIZE];
  203. result = bundle_create(&bundle);
  204. if(result) {
  205. goto unlock;
  206. }
  207. hashkey(conn, key, sizeof(key));
  208. if(!conncache_add_bundle(data->state.conn_cache, key, bundle)) {
  209. bundle_destroy(bundle);
  210. result = CURLE_OUT_OF_MEMORY;
  211. goto unlock;
  212. }
  213. }
  214. bundle_add_conn(bundle, conn);
  215. conn->connection_id = connc->next_connection_id++;
  216. connc->num_conn++;
  217. DEBUGF(infof(data, "Added connection %ld. "
  218. "The cache now contains %zu members",
  219. conn->connection_id, connc->num_conn));
  220. unlock:
  221. CONNCACHE_UNLOCK(data);
  222. return result;
  223. }
  224. /*
  225. * Removes the connectdata object from the connection cache, but the transfer
  226. * still owns this connection.
  227. *
  228. * Pass TRUE/FALSE in the 'lock' argument depending on if the parent function
  229. * already holds the lock or not.
  230. */
  231. void Curl_conncache_remove_conn(struct Curl_easy *data,
  232. struct connectdata *conn, bool lock)
  233. {
  234. struct connectbundle *bundle = conn->bundle;
  235. struct conncache *connc = data->state.conn_cache;
  236. /* The bundle pointer can be NULL, since this function can be called
  237. due to a failed connection attempt, before being added to a bundle */
  238. if(bundle) {
  239. if(lock) {
  240. CONNCACHE_LOCK(data);
  241. }
  242. bundle_remove_conn(bundle, conn);
  243. if(bundle->num_connections == 0)
  244. conncache_remove_bundle(connc, bundle);
  245. conn->bundle = NULL; /* removed from it */
  246. if(connc) {
  247. connc->num_conn--;
  248. DEBUGF(infof(data, "The cache now contains %zu members",
  249. connc->num_conn));
  250. }
  251. if(lock) {
  252. CONNCACHE_UNLOCK(data);
  253. }
  254. }
  255. }
  256. /* This function iterates the entire connection cache and calls the function
  257. func() with the connection pointer as the first argument and the supplied
  258. 'param' argument as the other.
  259. The conncache lock is still held when the callback is called. It needs it,
  260. so that it can safely continue traversing the lists once the callback
  261. returns.
  262. Returns 1 if the loop was aborted due to the callback's return code.
  263. Return 0 from func() to continue the loop, return 1 to abort it.
  264. */
  265. bool Curl_conncache_foreach(struct Curl_easy *data,
  266. struct conncache *connc,
  267. void *param,
  268. int (*func)(struct Curl_easy *data,
  269. struct connectdata *conn, void *param))
  270. {
  271. struct Curl_hash_iterator iter;
  272. struct Curl_llist_element *curr;
  273. struct Curl_hash_element *he;
  274. if(!connc)
  275. return FALSE;
  276. CONNCACHE_LOCK(data);
  277. Curl_hash_start_iterate(&connc->hash, &iter);
  278. he = Curl_hash_next_element(&iter);
  279. while(he) {
  280. struct connectbundle *bundle;
  281. bundle = he->ptr;
  282. he = Curl_hash_next_element(&iter);
  283. curr = bundle->conn_list.head;
  284. while(curr) {
  285. /* Yes, we need to update curr before calling func(), because func()
  286. might decide to remove the connection */
  287. struct connectdata *conn = curr->ptr;
  288. curr = curr->next;
  289. if(1 == func(data, conn, param)) {
  290. CONNCACHE_UNLOCK(data);
  291. return TRUE;
  292. }
  293. }
  294. }
  295. CONNCACHE_UNLOCK(data);
  296. return FALSE;
  297. }
  298. /* Return the first connection found in the cache. Used when closing all
  299. connections.
  300. NOTE: no locking is done here as this is presumably only done when cleaning
  301. up a cache!
  302. */
  303. static struct connectdata *
  304. conncache_find_first_connection(struct conncache *connc)
  305. {
  306. struct Curl_hash_iterator iter;
  307. struct Curl_hash_element *he;
  308. struct connectbundle *bundle;
  309. Curl_hash_start_iterate(&connc->hash, &iter);
  310. he = Curl_hash_next_element(&iter);
  311. while(he) {
  312. struct Curl_llist_element *curr;
  313. bundle = he->ptr;
  314. curr = bundle->conn_list.head;
  315. if(curr) {
  316. return curr->ptr;
  317. }
  318. he = Curl_hash_next_element(&iter);
  319. }
  320. return NULL;
  321. }
  322. /*
  323. * Give ownership of a connection back to the connection cache. Might
  324. * disconnect the oldest existing in there to make space.
  325. *
  326. * Return TRUE if stored, FALSE if closed.
  327. */
  328. bool Curl_conncache_return_conn(struct Curl_easy *data,
  329. struct connectdata *conn)
  330. {
  331. /* data->multi->maxconnects can be negative, deal with it. */
  332. size_t maxconnects =
  333. (data->multi->maxconnects < 0) ? data->multi->num_easy * 4:
  334. data->multi->maxconnects;
  335. struct connectdata *conn_candidate = NULL;
  336. conn->lastused = Curl_now(); /* it was used up until now */
  337. if(maxconnects > 0 &&
  338. Curl_conncache_size(data) > maxconnects) {
  339. infof(data, "Connection cache is full, closing the oldest one");
  340. conn_candidate = Curl_conncache_extract_oldest(data);
  341. if(conn_candidate) {
  342. /* the winner gets the honour of being disconnected */
  343. Curl_disconnect(data, conn_candidate, /* dead_connection */ FALSE);
  344. }
  345. }
  346. return (conn_candidate == conn) ? FALSE : TRUE;
  347. }
  348. /*
  349. * This function finds the connection in the connection bundle that has been
  350. * unused for the longest time.
  351. *
  352. * Does not lock the connection cache!
  353. *
  354. * Returns the pointer to the oldest idle connection, or NULL if none was
  355. * found.
  356. */
  357. struct connectdata *
  358. Curl_conncache_extract_bundle(struct Curl_easy *data,
  359. struct connectbundle *bundle)
  360. {
  361. struct Curl_llist_element *curr;
  362. timediff_t highscore = -1;
  363. timediff_t score;
  364. struct curltime now;
  365. struct connectdata *conn_candidate = NULL;
  366. struct connectdata *conn;
  367. (void)data;
  368. now = Curl_now();
  369. curr = bundle->conn_list.head;
  370. while(curr) {
  371. conn = curr->ptr;
  372. if(!CONN_INUSE(conn)) {
  373. /* Set higher score for the age passed since the connection was used */
  374. score = Curl_timediff(now, conn->lastused);
  375. if(score > highscore) {
  376. highscore = score;
  377. conn_candidate = conn;
  378. }
  379. }
  380. curr = curr->next;
  381. }
  382. if(conn_candidate) {
  383. /* remove it to prevent another thread from nicking it */
  384. bundle_remove_conn(bundle, conn_candidate);
  385. data->state.conn_cache->num_conn--;
  386. DEBUGF(infof(data, "The cache now contains %zu members",
  387. data->state.conn_cache->num_conn));
  388. }
  389. return conn_candidate;
  390. }
  391. /*
  392. * This function finds the connection in the connection cache that has been
  393. * unused for the longest time and extracts that from the bundle.
  394. *
  395. * Returns the pointer to the connection, or NULL if none was found.
  396. */
  397. struct connectdata *
  398. Curl_conncache_extract_oldest(struct Curl_easy *data)
  399. {
  400. struct conncache *connc = data->state.conn_cache;
  401. struct Curl_hash_iterator iter;
  402. struct Curl_llist_element *curr;
  403. struct Curl_hash_element *he;
  404. timediff_t highscore =- 1;
  405. timediff_t score;
  406. struct curltime now;
  407. struct connectdata *conn_candidate = NULL;
  408. struct connectbundle *bundle;
  409. struct connectbundle *bundle_candidate = NULL;
  410. now = Curl_now();
  411. CONNCACHE_LOCK(data);
  412. Curl_hash_start_iterate(&connc->hash, &iter);
  413. he = Curl_hash_next_element(&iter);
  414. while(he) {
  415. struct connectdata *conn;
  416. bundle = he->ptr;
  417. curr = bundle->conn_list.head;
  418. while(curr) {
  419. conn = curr->ptr;
  420. if(!CONN_INUSE(conn) && !conn->bits.close &&
  421. !conn->connect_only) {
  422. /* Set higher score for the age passed since the connection was used */
  423. score = Curl_timediff(now, conn->lastused);
  424. if(score > highscore) {
  425. highscore = score;
  426. conn_candidate = conn;
  427. bundle_candidate = bundle;
  428. }
  429. }
  430. curr = curr->next;
  431. }
  432. he = Curl_hash_next_element(&iter);
  433. }
  434. if(conn_candidate) {
  435. /* remove it to prevent another thread from nicking it */
  436. bundle_remove_conn(bundle_candidate, conn_candidate);
  437. connc->num_conn--;
  438. DEBUGF(infof(data, "The cache now contains %zu members",
  439. connc->num_conn));
  440. }
  441. CONNCACHE_UNLOCK(data);
  442. return conn_candidate;
  443. }
  444. void Curl_conncache_close_all_connections(struct conncache *connc)
  445. {
  446. struct connectdata *conn;
  447. char buffer[READBUFFER_MIN + 1];
  448. SIGPIPE_VARIABLE(pipe_st);
  449. if(!connc->closure_handle)
  450. return;
  451. connc->closure_handle->state.buffer = buffer;
  452. connc->closure_handle->set.buffer_size = READBUFFER_MIN;
  453. conn = conncache_find_first_connection(connc);
  454. while(conn) {
  455. sigpipe_ignore(connc->closure_handle, &pipe_st);
  456. /* This will remove the connection from the cache */
  457. connclose(conn, "kill all");
  458. Curl_conncache_remove_conn(connc->closure_handle, conn, TRUE);
  459. Curl_disconnect(connc->closure_handle, conn, FALSE);
  460. sigpipe_restore(&pipe_st);
  461. conn = conncache_find_first_connection(connc);
  462. }
  463. connc->closure_handle->state.buffer = NULL;
  464. sigpipe_ignore(connc->closure_handle, &pipe_st);
  465. Curl_hostcache_clean(connc->closure_handle,
  466. connc->closure_handle->dns.hostcache);
  467. Curl_close(&connc->closure_handle);
  468. sigpipe_restore(&pipe_st);
  469. }
  470. #if 0
  471. /* Useful for debugging the connection cache */
  472. void Curl_conncache_print(struct conncache *connc)
  473. {
  474. struct Curl_hash_iterator iter;
  475. struct Curl_llist_element *curr;
  476. struct Curl_hash_element *he;
  477. if(!connc)
  478. return;
  479. fprintf(stderr, "=Bundle cache=\n");
  480. Curl_hash_start_iterate(connc->hash, &iter);
  481. he = Curl_hash_next_element(&iter);
  482. while(he) {
  483. struct connectbundle *bundle;
  484. struct connectdata *conn;
  485. bundle = he->ptr;
  486. fprintf(stderr, "%s -", he->key);
  487. curr = bundle->conn_list->head;
  488. while(curr) {
  489. conn = curr->ptr;
  490. fprintf(stderr, " [%p %d]", (void *)conn, conn->inuse);
  491. curr = curr->next;
  492. }
  493. fprintf(stderr, "\n");
  494. he = Curl_hash_next_element(&iter);
  495. }
  496. }
  497. #endif