conncache.c 17 KB

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