2
0

conncache.c 16 KB

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