| | varnish-cache/bin/varnishd/cache/cache_conn_pool.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2015 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
* |
| 29 |
|
* (TCP|UDS) connection pools. |
| 30 |
|
* |
| 31 |
|
*/ |
| 32 |
|
|
| 33 |
|
#include "config.h" |
| 34 |
|
|
| 35 |
|
#include <stdlib.h> |
| 36 |
|
|
| 37 |
|
#include "cache_varnishd.h" |
| 38 |
|
|
| 39 |
|
#include "vsa.h" |
| 40 |
|
#include "vsha256.h" |
| 41 |
|
#include "vtcp.h" |
| 42 |
|
#include "vus.h" |
| 43 |
|
#include "vtim.h" |
| 44 |
|
#include "waiter/waiter.h" |
| 45 |
|
|
| 46 |
|
#include "cache_conn_pool.h" |
| 47 |
|
#include "cache_pool.h" |
| 48 |
|
|
| 49 |
|
#include "VSC_vcp.h" |
| 50 |
|
|
| 51 |
|
struct conn_pool; |
| 52 |
|
static inline int vcp_cmp(const struct conn_pool *a, const struct conn_pool *b); |
| 53 |
|
|
| 54 |
|
/*-------------------------------------------------------------------- |
| 55 |
|
*/ |
| 56 |
|
|
| 57 |
|
struct pfd { |
| 58 |
|
unsigned magic; |
| 59 |
|
#define PFD_MAGIC 0x0c5e6593 |
| 60 |
|
int fd; |
| 61 |
|
VTAILQ_ENTRY(pfd) list; |
| 62 |
|
VCL_IP addr; |
| 63 |
|
uint8_t state; |
| 64 |
|
struct waited waited[1]; |
| 65 |
|
struct conn_pool *conn_pool; |
| 66 |
|
|
| 67 |
|
pthread_cond_t *cond; |
| 68 |
|
}; |
| 69 |
|
|
| 70 |
|
/*-------------------------------------------------------------------- |
| 71 |
|
*/ |
| 72 |
|
|
| 73 |
|
typedef int cp_open_f(const struct conn_pool *, vtim_dur tmo, VCL_IP *ap); |
| 74 |
|
typedef void cp_close_f(struct pfd *); |
| 75 |
|
typedef void cp_name_f(const struct pfd *, char *, unsigned, char *, unsigned); |
| 76 |
|
|
| 77 |
|
struct cp_methods { |
| 78 |
|
cp_open_f *open; |
| 79 |
|
cp_close_f *close; |
| 80 |
|
cp_name_f *local_name; |
| 81 |
|
cp_name_f *remote_name; |
| 82 |
|
}; |
| 83 |
|
|
| 84 |
|
struct conn_pool { |
| 85 |
|
unsigned magic; |
| 86 |
|
#define CONN_POOL_MAGIC 0x85099bc3 |
| 87 |
|
|
| 88 |
|
const struct cp_methods *methods; |
| 89 |
|
|
| 90 |
|
struct vrt_endpoint *endpoint; |
| 91 |
|
char ident[VSHA256_DIGEST_LENGTH]; |
| 92 |
|
|
| 93 |
|
VRBT_ENTRY(conn_pool) entry; |
| 94 |
|
int refcnt; |
| 95 |
|
struct lock mtx; |
| 96 |
|
|
| 97 |
|
VTAILQ_HEAD(, pfd) connlist; |
| 98 |
|
int n_conn; |
| 99 |
|
|
| 100 |
|
int n_kill; |
| 101 |
|
|
| 102 |
|
int n_used; |
| 103 |
|
|
| 104 |
|
vtim_mono holddown; |
| 105 |
|
int holddown_errno; |
| 106 |
|
}; |
| 107 |
|
|
| 108 |
|
static struct lock conn_pools_mtx; |
| 109 |
|
static struct lock dead_pools_mtx; |
| 110 |
|
static struct VSC_vcp *vsc; |
| 111 |
|
|
| 112 |
|
VRBT_HEAD(vrb, conn_pool); |
| 113 |
657 |
VRBT_GENERATE_REMOVE_COLOR(vrb, conn_pool, entry, static) |
| 114 |
1682 |
VRBT_GENERATE_REMOVE(vrb, conn_pool, entry, static) |
| 115 |
8770 |
VRBT_GENERATE_INSERT_COLOR(vrb, conn_pool, entry, static) |
| 116 |
41578 |
VRBT_GENERATE_INSERT_FINISH(vrb, conn_pool, entry, static) |
| 117 |
63152 |
VRBT_GENERATE_INSERT(vrb, conn_pool, entry, vcp_cmp, static) |
| 118 |
98 |
VRBT_GENERATE_NEXT(vrb, conn_pool, entry, static) |
| 119 |
196 |
VRBT_GENERATE_MINMAX(vrb, conn_pool, entry, static) |
| 120 |
|
|
| 121 |
|
static struct vrb conn_pools = VRBT_INITIALIZER(&conn_pools); |
| 122 |
|
static struct vrb dead_pools = VRBT_INITIALIZER(&dying_cps); |
| 123 |
|
|
| 124 |
|
/*-------------------------------------------------------------------- |
| 125 |
|
*/ |
| 126 |
|
|
| 127 |
|
unsigned |
| 128 |
411862 |
PFD_State(const struct pfd *p) |
| 129 |
|
{ |
| 130 |
411862 |
CHECK_OBJ_NOTNULL(p, PFD_MAGIC); |
| 131 |
411862 |
return (p->state); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
int * |
| 135 |
174069 |
PFD_Fd(struct pfd *p) |
| 136 |
|
{ |
| 137 |
174069 |
CHECK_OBJ_NOTNULL(p, PFD_MAGIC); |
| 138 |
174069 |
return (&(p->fd)); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
void |
| 142 |
86599 |
PFD_LocalName(const struct pfd *p, char *abuf, unsigned alen, char *pbuf, |
| 143 |
|
unsigned plen) |
| 144 |
|
{ |
| 145 |
86599 |
CHECK_OBJ_NOTNULL(p, PFD_MAGIC); |
| 146 |
86599 |
CHECK_OBJ_NOTNULL(p->conn_pool, CONN_POOL_MAGIC); |
| 147 |
86599 |
p->conn_pool->methods->local_name(p, abuf, alen, pbuf, plen); |
| 148 |
86599 |
} |
| 149 |
|
|
| 150 |
|
void |
| 151 |
86599 |
PFD_RemoteName(const struct pfd *p, char *abuf, unsigned alen, char *pbuf, |
| 152 |
|
unsigned plen) |
| 153 |
|
{ |
| 154 |
86599 |
CHECK_OBJ_NOTNULL(p, PFD_MAGIC); |
| 155 |
86599 |
CHECK_OBJ_NOTNULL(p->conn_pool, CONN_POOL_MAGIC); |
| 156 |
86599 |
p->conn_pool->methods->remote_name(p, abuf, alen, pbuf, plen); |
| 157 |
86599 |
} |
| 158 |
|
|
| 159 |
|
/*-------------------------------------------------------------------- |
| 160 |
|
*/ |
| 161 |
|
|
| 162 |
|
static inline int |
| 163 |
21574 |
vcp_cmp(const struct conn_pool *a, const struct conn_pool *b) |
| 164 |
|
{ |
| 165 |
21574 |
return (memcmp(a->ident, b->ident, sizeof b->ident)); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
/*-------------------------------------------------------------------- |
| 169 |
|
* Waiter-handler |
| 170 |
|
*/ |
| 171 |
|
|
| 172 |
|
static void v_matchproto_(waiter_handle_f) |
| 173 |
70087 |
vcp_handle(struct waited *w, enum wait_event ev, vtim_real now) |
| 174 |
|
{ |
| 175 |
|
struct pfd *pfd; |
| 176 |
|
struct conn_pool *cp; |
| 177 |
|
|
| 178 |
70087 |
CHECK_OBJ_NOTNULL(w, WAITED_MAGIC); |
| 179 |
70087 |
CAST_OBJ_NOTNULL(pfd, w->priv1, PFD_MAGIC); |
| 180 |
70087 |
(void)ev; |
| 181 |
70087 |
(void)now; |
| 182 |
70087 |
CHECK_OBJ_NOTNULL(pfd->conn_pool, CONN_POOL_MAGIC); |
| 183 |
70087 |
cp = pfd->conn_pool; |
| 184 |
|
|
| 185 |
70087 |
Lck_Lock(&cp->mtx); |
| 186 |
|
|
| 187 |
70087 |
switch (pfd->state) { |
| 188 |
|
case PFD_STATE_STOLEN: |
| 189 |
32278 |
pfd->state = PFD_STATE_USED; |
| 190 |
32278 |
VTAILQ_REMOVE(&cp->connlist, pfd, list); |
| 191 |
32278 |
AN(pfd->cond); |
| 192 |
32278 |
PTOK(pthread_cond_signal(pfd->cond)); |
| 193 |
32278 |
break; |
| 194 |
|
case PFD_STATE_AVAIL: |
| 195 |
37649 |
cp->methods->close(pfd); |
| 196 |
37649 |
VTAILQ_REMOVE(&cp->connlist, pfd, list); |
| 197 |
37649 |
cp->n_conn--; |
| 198 |
37649 |
FREE_OBJ(pfd); |
| 199 |
37649 |
break; |
| 200 |
|
case PFD_STATE_CLEANUP: |
| 201 |
160 |
cp->methods->close(pfd); |
| 202 |
160 |
cp->n_kill--; |
| 203 |
160 |
memset(pfd, 0x11, sizeof *pfd); |
| 204 |
160 |
free(pfd); |
| 205 |
160 |
break; |
| 206 |
|
default: |
| 207 |
0 |
WRONG("Wrong pfd state"); |
| 208 |
0 |
} |
| 209 |
70087 |
Lck_Unlock(&cp->mtx); |
| 210 |
70087 |
} |
| 211 |
|
|
| 212 |
|
|
| 213 |
|
/*-------------------------------------------------------------------- |
| 214 |
|
*/ |
| 215 |
|
|
| 216 |
|
void |
| 217 |
1480 |
VCP_AddRef(struct conn_pool *cp) |
| 218 |
|
{ |
| 219 |
1480 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 220 |
|
|
| 221 |
1480 |
Lck_Lock(&conn_pools_mtx); |
| 222 |
1480 |
assert(cp->refcnt > 0); |
| 223 |
1480 |
cp->refcnt++; |
| 224 |
1480 |
Lck_Unlock(&conn_pools_mtx); |
| 225 |
1480 |
} |
| 226 |
|
|
| 227 |
|
/*-------------------------------------------------------------------- |
| 228 |
|
*/ |
| 229 |
|
|
| 230 |
|
static void |
| 231 |
13240 |
vcp_destroy(struct conn_pool **cpp) |
| 232 |
|
{ |
| 233 |
|
struct conn_pool *cp; |
| 234 |
|
|
| 235 |
13240 |
TAKE_OBJ_NOTNULL(cp, cpp, CONN_POOL_MAGIC); |
| 236 |
13240 |
AZ(cp->n_conn); |
| 237 |
13240 |
AZ(cp->n_kill); |
| 238 |
13240 |
Lck_Delete(&cp->mtx); |
| 239 |
13240 |
FREE_OBJ(cp->endpoint); |
| 240 |
13240 |
FREE_OBJ(cp); |
| 241 |
13240 |
} |
| 242 |
|
|
| 243 |
|
/*-------------------------------------------------------------------- |
| 244 |
|
* Release Conn pool, destroy or stash for future destruction if last |
| 245 |
|
* reference. |
| 246 |
|
*/ |
| 247 |
|
|
| 248 |
|
void |
| 249 |
3467 |
VCP_Rel(struct conn_pool **cpp) |
| 250 |
|
{ |
| 251 |
|
struct conn_pool *cp; |
| 252 |
|
struct pfd *pfd, *pfd2; |
| 253 |
|
int n_kill; |
| 254 |
|
|
| 255 |
3467 |
TAKE_OBJ_NOTNULL(cp, cpp, CONN_POOL_MAGIC); |
| 256 |
|
|
| 257 |
3467 |
Lck_Lock(&conn_pools_mtx); |
| 258 |
3467 |
assert(cp->refcnt > 0); |
| 259 |
3467 |
if (--cp->refcnt > 0) { |
| 260 |
2387 |
Lck_Unlock(&conn_pools_mtx); |
| 261 |
2387 |
return; |
| 262 |
|
} |
| 263 |
1080 |
AZ(cp->n_used); |
| 264 |
1080 |
VRBT_REMOVE(vrb, &conn_pools, cp); |
| 265 |
1080 |
Lck_Unlock(&conn_pools_mtx); |
| 266 |
|
|
| 267 |
1080 |
Lck_Lock(&cp->mtx); |
| 268 |
1160 |
VTAILQ_FOREACH_SAFE(pfd, &cp->connlist, list, pfd2) { |
| 269 |
80 |
VTAILQ_REMOVE(&cp->connlist, pfd, list); |
| 270 |
80 |
cp->n_conn--; |
| 271 |
80 |
assert(pfd->state == PFD_STATE_AVAIL); |
| 272 |
80 |
pfd->state = PFD_STATE_CLEANUP; |
| 273 |
80 |
(void)shutdown(pfd->fd, SHUT_RDWR); |
| 274 |
80 |
cp->n_kill++; |
| 275 |
80 |
} |
| 276 |
1080 |
n_kill = cp->n_kill; |
| 277 |
1080 |
Lck_Unlock(&cp->mtx); |
| 278 |
1080 |
if (n_kill == 0) { |
| 279 |
1000 |
vcp_destroy(&cp); |
| 280 |
1000 |
return; |
| 281 |
|
} |
| 282 |
80 |
Lck_Lock(&dead_pools_mtx); |
| 283 |
|
/* |
| 284 |
|
* Here we reuse cp's entry but it will probably not be correctly |
| 285 |
|
* indexed because of the hack in VCP_RelPoll |
| 286 |
|
*/ |
| 287 |
80 |
VRBT_INSERT(vrb, &dead_pools, cp); |
| 288 |
80 |
Lck_Unlock(&dead_pools_mtx); |
| 289 |
3467 |
} |
| 290 |
|
|
| 291 |
|
void |
| 292 |
309755 |
VCP_RelPoll(void) |
| 293 |
|
{ |
| 294 |
|
struct vrb dead; |
| 295 |
|
struct conn_pool *cp, *cp2; |
| 296 |
|
int n_kill; |
| 297 |
|
|
| 298 |
309755 |
ASSERT_CLI(); |
| 299 |
|
|
| 300 |
309755 |
Lck_Lock(&dead_pools_mtx); |
| 301 |
309755 |
if (VRBT_EMPTY(&dead_pools)) { |
| 302 |
309657 |
Lck_Unlock(&dead_pools_mtx); |
| 303 |
309657 |
return; |
| 304 |
|
} |
| 305 |
98 |
dead = dead_pools; |
| 306 |
98 |
VRBT_INIT(&dead_pools); |
| 307 |
98 |
Lck_Unlock(&dead_pools_mtx); |
| 308 |
|
|
| 309 |
196 |
VRBT_FOREACH_SAFE(cp, vrb, &dead, cp2) { |
| 310 |
98 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 311 |
98 |
Lck_Lock(&cp->mtx); |
| 312 |
98 |
n_kill = cp->n_kill; |
| 313 |
98 |
Lck_Unlock(&cp->mtx); |
| 314 |
98 |
if (n_kill > 0) |
| 315 |
18 |
continue; |
| 316 |
80 |
VRBT_REMOVE(vrb, &dead, cp); |
| 317 |
80 |
vcp_destroy(&cp); |
| 318 |
80 |
} |
| 319 |
|
|
| 320 |
98 |
if (VRBT_EMPTY(&dead)) |
| 321 |
80 |
return; |
| 322 |
|
|
| 323 |
18 |
Lck_Lock(&dead_pools_mtx); |
| 324 |
|
/* |
| 325 |
|
* The following insertion will most likely result in an |
| 326 |
|
* unordered tree, but in this case it does not matter |
| 327 |
|
* as we just want to iterate over all the elements |
| 328 |
|
* in the tree in order to delete them. |
| 329 |
|
*/ |
| 330 |
18 |
VRBT_INSERT(vrb, &dead_pools, dead.rbh_root); |
| 331 |
18 |
Lck_Unlock(&dead_pools_mtx); |
| 332 |
309755 |
} |
| 333 |
|
|
| 334 |
|
/*-------------------------------------------------------------------- |
| 335 |
|
* Recycle a connection. |
| 336 |
|
*/ |
| 337 |
|
|
| 338 |
|
void |
| 339 |
70924 |
VCP_Recycle(const struct worker *wrk, struct pfd **pfdp) |
| 340 |
|
{ |
| 341 |
|
struct pfd *pfd; |
| 342 |
|
struct conn_pool *cp; |
| 343 |
70924 |
int i = 0; |
| 344 |
|
|
| 345 |
70924 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 346 |
70924 |
TAKE_OBJ_NOTNULL(pfd, pfdp, PFD_MAGIC); |
| 347 |
70924 |
cp = pfd->conn_pool; |
| 348 |
70924 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 349 |
|
|
| 350 |
70924 |
assert(pfd->state == PFD_STATE_USED); |
| 351 |
70924 |
assert(pfd->fd > 0); |
| 352 |
|
|
| 353 |
70924 |
Lck_Lock(&cp->mtx); |
| 354 |
70924 |
cp->n_used--; |
| 355 |
|
|
| 356 |
70924 |
pfd->waited->priv1 = pfd; |
| 357 |
70924 |
pfd->waited->fd = pfd->fd; |
| 358 |
70924 |
pfd->waited->idle = VTIM_real(); |
| 359 |
70924 |
pfd->state = PFD_STATE_AVAIL; |
| 360 |
70924 |
pfd->waited->func = vcp_handle; |
| 361 |
70924 |
pfd->waited->tmo = cache_param->backend_idle_timeout; |
| 362 |
70924 |
if (Wait_Enter(wrk->pool->waiter, pfd->waited)) { |
| 363 |
0 |
cp->methods->close(pfd); |
| 364 |
0 |
memset(pfd, 0x33, sizeof *pfd); |
| 365 |
0 |
free(pfd); |
| 366 |
|
// XXX: stats |
| 367 |
0 |
pfd = NULL; |
| 368 |
0 |
} else { |
| 369 |
70924 |
VTAILQ_INSERT_HEAD(&cp->connlist, pfd, list); |
| 370 |
70924 |
i++; |
| 371 |
|
} |
| 372 |
|
|
| 373 |
70924 |
if (pfd != NULL) |
| 374 |
70924 |
cp->n_conn++; |
| 375 |
70924 |
Lck_Unlock(&cp->mtx); |
| 376 |
|
|
| 377 |
70924 |
if (i && DO_DEBUG(DBG_VTC_MODE)) { |
| 378 |
|
/* |
| 379 |
|
* In varnishtest we do not have the luxury of using |
| 380 |
|
* multiple backend connections, so whenever we end up |
| 381 |
|
* in the "pending" case, take a short nap to let the |
| 382 |
|
* waiter catch up and put the pfd back into circulations. |
| 383 |
|
* |
| 384 |
|
* In particular ESI:include related tests suffer random |
| 385 |
|
* failures without this. |
| 386 |
|
* |
| 387 |
|
* In normal operation, the only effect is that we will |
| 388 |
|
* have N+1 backend connections rather than N, which is |
| 389 |
|
* entirely harmless. |
| 390 |
|
*/ |
| 391 |
70924 |
VTIM_sleep(0.01); |
| 392 |
70924 |
} |
| 393 |
70924 |
} |
| 394 |
|
|
| 395 |
|
/*-------------------------------------------------------------------- |
| 396 |
|
* Open a new connection from pool. |
| 397 |
|
*/ |
| 398 |
|
|
| 399 |
|
int |
| 400 |
63427 |
VCP_Open(struct conn_pool *cp, vtim_dur tmo, VCL_IP *ap, int *err) |
| 401 |
|
{ |
| 402 |
|
int r; |
| 403 |
|
vtim_mono h; |
| 404 |
|
|
| 405 |
63427 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 406 |
63427 |
AN(err); |
| 407 |
|
|
| 408 |
63427 |
while (cp->holddown > 0) { |
| 409 |
889 |
Lck_Lock(&cp->mtx); |
| 410 |
889 |
if (cp->holddown == 0) { |
| 411 |
0 |
Lck_Unlock(&cp->mtx); |
| 412 |
0 |
break; |
| 413 |
|
} |
| 414 |
|
|
| 415 |
889 |
if (VTIM_mono() >= cp->holddown) { |
| 416 |
108 |
cp->holddown = 0; |
| 417 |
108 |
Lck_Unlock(&cp->mtx); |
| 418 |
108 |
break; |
| 419 |
|
} |
| 420 |
|
|
| 421 |
781 |
*err = 0; |
| 422 |
781 |
errno = cp->holddown_errno; |
| 423 |
781 |
Lck_Unlock(&cp->mtx); |
| 424 |
781 |
return (-1); |
| 425 |
|
} |
| 426 |
|
|
| 427 |
62646 |
*err = errno = 0; |
| 428 |
62646 |
r = cp->methods->open(cp, tmo, ap); |
| 429 |
|
|
| 430 |
62646 |
if (r >= 0 && errno == 0 && cp->endpoint->preamble != NULL && |
| 431 |
360 |
cp->endpoint->preamble->len > 0) { |
| 432 |
360 |
CHECK_OBJ(cp->endpoint->preamble, VRT_BLOB_MAGIC); |
| 433 |
1080 |
if (write(r, cp->endpoint->preamble->blob, |
| 434 |
720 |
cp->endpoint->preamble->len) != |
| 435 |
360 |
cp->endpoint->preamble->len) { |
| 436 |
0 |
*err = errno; |
| 437 |
0 |
closefd(&r); |
| 438 |
0 |
} |
| 439 |
360 |
} else { |
| 440 |
62286 |
*err = errno; |
| 441 |
|
} |
| 442 |
|
|
| 443 |
62646 |
if (r >= 0) |
| 444 |
61576 |
return (r); |
| 445 |
|
|
| 446 |
1070 |
h = 0; |
| 447 |
|
|
| 448 |
1070 |
switch (errno) { |
| 449 |
|
case EACCES: |
| 450 |
|
case EPERM: |
| 451 |
0 |
h = cache_param->backend_local_error_holddown; |
| 452 |
0 |
break; |
| 453 |
|
case EADDRNOTAVAIL: |
| 454 |
0 |
h = cache_param->backend_local_error_holddown; |
| 455 |
0 |
break; |
| 456 |
|
case ECONNREFUSED: |
| 457 |
1070 |
h = cache_param->backend_remote_error_holddown; |
| 458 |
1070 |
break; |
| 459 |
|
case ENETUNREACH: |
| 460 |
0 |
h = cache_param->backend_remote_error_holddown; |
| 461 |
0 |
break; |
| 462 |
|
default: |
| 463 |
0 |
break; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
1070 |
if (h == 0) |
| 467 |
0 |
return (r); |
| 468 |
|
|
| 469 |
1070 |
Lck_Lock(&cp->mtx); |
| 470 |
1070 |
h += VTIM_mono(); |
| 471 |
1070 |
if (cp->holddown == 0 || h < cp->holddown) { |
| 472 |
963 |
cp->holddown = h; |
| 473 |
963 |
cp->holddown_errno = errno; |
| 474 |
963 |
} |
| 475 |
|
|
| 476 |
1070 |
Lck_Unlock(&cp->mtx); |
| 477 |
|
|
| 478 |
1070 |
return (r); |
| 479 |
63427 |
} |
| 480 |
|
|
| 481 |
|
/*-------------------------------------------------------------------- |
| 482 |
|
* Close a connection. |
| 483 |
|
*/ |
| 484 |
|
|
| 485 |
|
void |
| 486 |
15636 |
VCP_Close(struct pfd **pfdp) |
| 487 |
|
{ |
| 488 |
|
struct pfd *pfd; |
| 489 |
|
struct conn_pool *cp; |
| 490 |
|
|
| 491 |
15636 |
TAKE_OBJ_NOTNULL(pfd, pfdp, PFD_MAGIC); |
| 492 |
15636 |
cp = pfd->conn_pool; |
| 493 |
15636 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 494 |
|
|
| 495 |
15636 |
assert(pfd->fd > 0); |
| 496 |
|
|
| 497 |
15636 |
Lck_Lock(&cp->mtx); |
| 498 |
15636 |
assert(pfd->state == PFD_STATE_USED || pfd->state == PFD_STATE_STOLEN); |
| 499 |
15636 |
cp->n_used--; |
| 500 |
15636 |
if (pfd->state == PFD_STATE_STOLEN) { |
| 501 |
80 |
(void)shutdown(pfd->fd, SHUT_RDWR); |
| 502 |
80 |
VTAILQ_REMOVE(&cp->connlist, pfd, list); |
| 503 |
80 |
pfd->state = PFD_STATE_CLEANUP; |
| 504 |
80 |
cp->n_kill++; |
| 505 |
80 |
} else { |
| 506 |
15556 |
assert(pfd->state == PFD_STATE_USED); |
| 507 |
15556 |
cp->methods->close(pfd); |
| 508 |
15556 |
memset(pfd, 0x44, sizeof *pfd); |
| 509 |
15556 |
free(pfd); |
| 510 |
|
} |
| 511 |
15636 |
Lck_Unlock(&cp->mtx); |
| 512 |
15636 |
} |
| 513 |
|
|
| 514 |
|
/*-------------------------------------------------------------------- |
| 515 |
|
* Get a connection, possibly recycled |
| 516 |
|
*/ |
| 517 |
|
|
| 518 |
|
struct pfd * |
| 519 |
87840 |
VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk, |
| 520 |
|
unsigned force_fresh, int *err) |
| 521 |
|
{ |
| 522 |
|
struct pfd *pfd; |
| 523 |
|
|
| 524 |
87840 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 525 |
87840 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 526 |
87840 |
AN(err); |
| 527 |
|
|
| 528 |
87840 |
*err = 0; |
| 529 |
87840 |
Lck_Lock(&cp->mtx); |
| 530 |
87840 |
pfd = VTAILQ_FIRST(&cp->connlist); |
| 531 |
87840 |
CHECK_OBJ_ORNULL(pfd, PFD_MAGIC); |
| 532 |
87840 |
if (force_fresh || pfd == NULL || pfd->state == PFD_STATE_STOLEN) { |
| 533 |
55482 |
pfd = NULL; |
| 534 |
55482 |
} else { |
| 535 |
32358 |
assert(pfd->conn_pool == cp); |
| 536 |
32358 |
assert(pfd->state == PFD_STATE_AVAIL); |
| 537 |
32358 |
VTAILQ_REMOVE(&cp->connlist, pfd, list); |
| 538 |
32358 |
VTAILQ_INSERT_TAIL(&cp->connlist, pfd, list); |
| 539 |
32358 |
cp->n_conn--; |
| 540 |
32358 |
VSC_C_main->backend_reuse++; |
| 541 |
32358 |
pfd->state = PFD_STATE_STOLEN; |
| 542 |
32358 |
pfd->cond = &wrk->cond; |
| 543 |
|
} |
| 544 |
87840 |
cp->n_used++; // Opening mostly works |
| 545 |
87840 |
Lck_Unlock(&cp->mtx); |
| 546 |
|
|
| 547 |
87840 |
if (pfd != NULL) |
| 548 |
32358 |
return (pfd); |
| 549 |
|
|
| 550 |
55482 |
ALLOC_OBJ(pfd, PFD_MAGIC); |
| 551 |
55482 |
AN(pfd); |
| 552 |
55482 |
INIT_OBJ(pfd->waited, WAITED_MAGIC); |
| 553 |
55482 |
pfd->state = PFD_STATE_USED; |
| 554 |
55482 |
pfd->conn_pool = cp; |
| 555 |
55482 |
pfd->fd = VCP_Open(cp, tmo, &pfd->addr, err); |
| 556 |
55482 |
if (pfd->fd < 0) { |
| 557 |
1240 |
FREE_OBJ(pfd); |
| 558 |
1240 |
Lck_Lock(&cp->mtx); |
| 559 |
1240 |
cp->n_used--; // Nope, didn't work after all. |
| 560 |
1240 |
Lck_Unlock(&cp->mtx); |
| 561 |
1240 |
} else |
| 562 |
54242 |
VSC_C_main->backend_conn++; |
| 563 |
|
|
| 564 |
55482 |
return (pfd); |
| 565 |
87840 |
} |
| 566 |
|
|
| 567 |
|
/*-------------------------------------------------------------------- |
| 568 |
|
*/ |
| 569 |
|
|
| 570 |
|
int |
| 571 |
32238 |
VCP_Wait(struct worker *wrk, struct pfd *pfd, vtim_real when) |
| 572 |
|
{ |
| 573 |
|
struct conn_pool *cp; |
| 574 |
|
int r; |
| 575 |
|
|
| 576 |
32238 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 577 |
32238 |
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC); |
| 578 |
32238 |
cp = pfd->conn_pool; |
| 579 |
32238 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 580 |
32238 |
assert(pfd->cond == &wrk->cond); |
| 581 |
32238 |
Lck_Lock(&cp->mtx); |
| 582 |
64436 |
while (pfd->state == PFD_STATE_STOLEN) { |
| 583 |
32238 |
r = Lck_CondWaitUntil(&wrk->cond, &cp->mtx, when); |
| 584 |
32238 |
if (r != 0) { |
| 585 |
40 |
if (r == EINTR) |
| 586 |
0 |
continue; |
| 587 |
40 |
assert(r == ETIMEDOUT); |
| 588 |
40 |
Lck_Unlock(&cp->mtx); |
| 589 |
40 |
return (1); |
| 590 |
|
} |
| 591 |
|
} |
| 592 |
32198 |
assert(pfd->state == PFD_STATE_USED); |
| 593 |
32198 |
pfd->cond = NULL; |
| 594 |
32198 |
Lck_Unlock(&cp->mtx); |
| 595 |
|
|
| 596 |
32198 |
return (0); |
| 597 |
32238 |
} |
| 598 |
|
|
| 599 |
|
/*-------------------------------------------------------------------- |
| 600 |
|
*/ |
| 601 |
|
|
| 602 |
|
VCL_IP |
| 603 |
40 |
VCP_GetIp(struct pfd *pfd) |
| 604 |
|
{ |
| 605 |
|
|
| 606 |
40 |
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC); |
| 607 |
40 |
return (pfd->addr); |
| 608 |
|
} |
| 609 |
|
|
| 610 |
|
/*--------------------------------------------------------------------*/ |
| 611 |
|
|
| 612 |
|
static void |
| 613 |
240 |
vcp_panic_endpoint(struct vsb *vsb, const struct vrt_endpoint *vep) |
| 614 |
|
{ |
| 615 |
|
char h[VTCP_ADDRBUFSIZE]; |
| 616 |
|
char p[VTCP_PORTBUFSIZE]; |
| 617 |
|
|
| 618 |
240 |
if (PAN_dump_struct(vsb, vep, VRT_ENDPOINT_MAGIC, "vrt_endpoint")) |
| 619 |
0 |
return; |
| 620 |
240 |
if (vep->uds_path) |
| 621 |
0 |
VSB_printf(vsb, "uds_path = %s,\n", vep->uds_path); |
| 622 |
240 |
if (vep->ipv4 && VSA_Sane(vep->ipv4)) { |
| 623 |
240 |
VTCP_name(vep->ipv4, h, sizeof h, p, sizeof p); |
| 624 |
240 |
VSB_printf(vsb, "ipv4 = %s, ", h); |
| 625 |
240 |
VSB_printf(vsb, "port = %s,\n", p); |
| 626 |
240 |
} |
| 627 |
240 |
if (vep->ipv6 && VSA_Sane(vep->ipv6)) { |
| 628 |
0 |
VTCP_name(vep->ipv6, h, sizeof h, p, sizeof p); |
| 629 |
0 |
VSB_printf(vsb, "ipv6 = %s, ", h); |
| 630 |
0 |
VSB_printf(vsb, "port = %s,\n", p); |
| 631 |
0 |
} |
| 632 |
240 |
VSB_indent(vsb, -2); |
| 633 |
240 |
VSB_cat(vsb, "},\n"); |
| 634 |
240 |
} |
| 635 |
|
|
| 636 |
|
void |
| 637 |
240 |
VCP_Panic(struct vsb *vsb, struct conn_pool *cp) |
| 638 |
|
{ |
| 639 |
|
|
| 640 |
240 |
if (PAN_dump_struct(vsb, cp, CONN_POOL_MAGIC, "conn_pool")) |
| 641 |
0 |
return; |
| 642 |
240 |
VSB_cat(vsb, "ident = "); |
| 643 |
240 |
VSB_quote(vsb, cp->ident, VSHA256_DIGEST_LENGTH, VSB_QUOTE_HEX); |
| 644 |
240 |
VSB_cat(vsb, ",\n"); |
| 645 |
240 |
vcp_panic_endpoint(vsb, cp->endpoint); |
| 646 |
240 |
VSB_indent(vsb, -2); |
| 647 |
240 |
VSB_cat(vsb, "},\n"); |
| 648 |
240 |
} |
| 649 |
|
|
| 650 |
|
/*--------------------------------------------------------------------*/ |
| 651 |
|
|
| 652 |
|
void |
| 653 |
38028 |
VCP_Init(void) |
| 654 |
|
{ |
| 655 |
38028 |
Lck_New(&conn_pools_mtx, lck_conn_pool); |
| 656 |
38028 |
Lck_New(&dead_pools_mtx, lck_dead_pool); |
| 657 |
|
|
| 658 |
38028 |
AZ(vsc); |
| 659 |
38028 |
vsc = VSC_vcp_New(NULL, NULL, ""); |
| 660 |
38028 |
AN(vsc); |
| 661 |
38028 |
} |
| 662 |
|
|
| 663 |
|
/**********************************************************************/ |
| 664 |
|
|
| 665 |
|
static inline int |
| 666 |
62685 |
tmo2msec(vtim_dur tmo) |
| 667 |
|
{ |
| 668 |
62685 |
return ((int)floor(tmo * 1000.0)); |
| 669 |
|
} |
| 670 |
|
|
| 671 |
|
static int v_matchproto_(cp_open_f) |
| 672 |
57827 |
vtp_open(const struct conn_pool *cp, vtim_dur tmo, VCL_IP *ap) |
| 673 |
|
{ |
| 674 |
|
int s; |
| 675 |
|
int msec; |
| 676 |
|
|
| 677 |
57827 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 678 |
|
|
| 679 |
57827 |
msec = tmo2msec(tmo); |
| 680 |
57827 |
if (cache_param->prefer_ipv6) { |
| 681 |
0 |
*ap = cp->endpoint->ipv6; |
| 682 |
0 |
s = VTCP_connect(*ap, msec); |
| 683 |
0 |
if (s >= 0) |
| 684 |
0 |
return (s); |
| 685 |
0 |
} |
| 686 |
57827 |
*ap = cp->endpoint->ipv4; |
| 687 |
57827 |
s = VTCP_connect(*ap, msec); |
| 688 |
57827 |
if (s >= 0) |
| 689 |
56699 |
return (s); |
| 690 |
1128 |
if (!cache_param->prefer_ipv6) { |
| 691 |
1128 |
*ap = cp->endpoint->ipv6; |
| 692 |
1128 |
s = VTCP_connect(*ap, msec); |
| 693 |
1128 |
} |
| 694 |
1128 |
return (s); |
| 695 |
57827 |
} |
| 696 |
|
|
| 697 |
|
|
| 698 |
|
/*--------------------------------------------------------------------*/ |
| 699 |
|
|
| 700 |
|
static void v_matchproto_(cp_close_f) |
| 701 |
53362 |
vtp_close(struct pfd *pfd) |
| 702 |
|
{ |
| 703 |
|
|
| 704 |
53362 |
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC); |
| 705 |
53362 |
VTCP_close(&pfd->fd); |
| 706 |
53362 |
} |
| 707 |
|
|
| 708 |
|
static void v_matchproto_(cp_name_f) |
| 709 |
82160 |
vtp_local_name(const struct pfd *pfd, char *addr, unsigned alen, char *pbuf, |
| 710 |
|
unsigned plen) |
| 711 |
|
{ |
| 712 |
82160 |
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC); |
| 713 |
82160 |
VTCP_myname(pfd->fd, addr, alen, pbuf, plen); |
| 714 |
82160 |
} |
| 715 |
|
|
| 716 |
|
static void v_matchproto_(cp_name_f) |
| 717 |
82160 |
vtp_remote_name(const struct pfd *pfd, char *addr, unsigned alen, char *pbuf, |
| 718 |
|
unsigned plen) |
| 719 |
|
{ |
| 720 |
82160 |
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC); |
| 721 |
82160 |
VTCP_hisname(pfd->fd, addr, alen, pbuf, plen); |
| 722 |
82160 |
} |
| 723 |
|
|
| 724 |
|
static const struct cp_methods vtp_methods = { |
| 725 |
|
.open = vtp_open, |
| 726 |
|
.close = vtp_close, |
| 727 |
|
.local_name = vtp_local_name, |
| 728 |
|
.remote_name = vtp_remote_name, |
| 729 |
|
}; |
| 730 |
|
|
| 731 |
|
/*-------------------------------------------------------------------- |
| 732 |
|
*/ |
| 733 |
|
|
| 734 |
|
static int v_matchproto_(cp_open_f) |
| 735 |
4818 |
vus_open(const struct conn_pool *cp, vtim_dur tmo, VCL_IP *ap) |
| 736 |
|
{ |
| 737 |
|
int s; |
| 738 |
|
int msec; |
| 739 |
|
|
| 740 |
4818 |
CHECK_OBJ_NOTNULL(cp, CONN_POOL_MAGIC); |
| 741 |
4818 |
AN(cp->endpoint->uds_path); |
| 742 |
|
|
| 743 |
4818 |
msec = tmo2msec(tmo); |
| 744 |
4818 |
*ap = bogo_ip; |
| 745 |
4818 |
s = VUS_connect(cp->endpoint->uds_path, msec); |
| 746 |
4818 |
return (s); |
| 747 |
|
} |
| 748 |
|
|
| 749 |
|
static void v_matchproto_(cp_name_f) |
| 750 |
8878 |
vus_name(const struct pfd *pfd, char *addr, unsigned alen, char *pbuf, |
| 751 |
|
unsigned plen) |
| 752 |
|
{ |
| 753 |
8878 |
(void) pfd; |
| 754 |
8878 |
assert(alen > strlen("0.0.0.0")); |
| 755 |
8878 |
assert(plen > 1); |
| 756 |
8878 |
strcpy(addr, "0.0.0.0"); |
| 757 |
8878 |
strcpy(pbuf, "0"); |
| 758 |
8878 |
} |
| 759 |
|
|
| 760 |
|
static const struct cp_methods vus_methods = { |
| 761 |
|
.open = vus_open, |
| 762 |
|
.close = vtp_close, |
| 763 |
|
.local_name = vus_name, |
| 764 |
|
.remote_name = vus_name, |
| 765 |
|
}; |
| 766 |
|
|
| 767 |
|
/*-------------------------------------------------------------------- |
| 768 |
|
* Reference a TCP pool given by {ip4, ip6} pair or a UDS. Create if |
| 769 |
|
* it doesn't exist already. |
| 770 |
|
*/ |
| 771 |
|
|
| 772 |
|
struct conn_pool * |
| 773 |
53640 |
VCP_Ref(const struct vrt_endpoint *vep, const char *ident) |
| 774 |
|
{ |
| 775 |
|
struct conn_pool *cp, *cp2; |
| 776 |
|
struct VSHA256Context cx[1]; |
| 777 |
|
unsigned char digest[VSHA256_DIGEST_LENGTH]; |
| 778 |
|
|
| 779 |
53640 |
CHECK_OBJ_NOTNULL(vep, VRT_ENDPOINT_MAGIC); |
| 780 |
53640 |
AN(ident); |
| 781 |
53640 |
AN(vsc); |
| 782 |
|
|
| 783 |
53640 |
VSHA256_Init(cx); |
| 784 |
53640 |
VSHA256_Update(cx, ident, strlen(ident) + 1); // include \0 |
| 785 |
53640 |
if (vep->uds_path != NULL) { |
| 786 |
1840 |
AZ(vep->ipv4); |
| 787 |
1840 |
AZ(vep->ipv6); |
| 788 |
1840 |
VSHA256_Update(cx, "UDS", 4); // include \0 |
| 789 |
1840 |
VSHA256_Update(cx, vep->uds_path, strlen(vep->uds_path)); |
| 790 |
1840 |
} else { |
| 791 |
51800 |
assert(vep->ipv4 != NULL || vep->ipv6 != NULL); |
| 792 |
51800 |
if (vep->ipv4 != NULL) { |
| 793 |
51680 |
assert(VSA_Sane(vep->ipv4)); |
| 794 |
51680 |
VSHA256_Update(cx, "IP4", 4); // include \0 |
| 795 |
51680 |
VSHA256_Update(cx, vep->ipv4, vsa_suckaddr_len); |
| 796 |
51680 |
} |
| 797 |
51800 |
if (vep->ipv6 != NULL) { |
| 798 |
160 |
assert(VSA_Sane(vep->ipv6)); |
| 799 |
160 |
VSHA256_Update(cx, "IP6", 4); // include \0 |
| 800 |
160 |
VSHA256_Update(cx, vep->ipv6, vsa_suckaddr_len); |
| 801 |
160 |
} |
| 802 |
|
} |
| 803 |
53640 |
CHECK_OBJ_ORNULL(vep->preamble, VRT_BLOB_MAGIC); |
| 804 |
53640 |
if (vep->preamble != NULL && vep->preamble->len > 0) { |
| 805 |
360 |
VSHA256_Update(cx, "PRE", 4); // include \0 |
| 806 |
360 |
VSHA256_Update(cx, vep->preamble->blob, vep->preamble->len); |
| 807 |
360 |
} |
| 808 |
53640 |
VSHA256_Final(digest, cx); |
| 809 |
|
|
| 810 |
53640 |
ALLOC_OBJ(cp, CONN_POOL_MAGIC); |
| 811 |
53640 |
AN(cp); |
| 812 |
53640 |
cp->refcnt = 1; |
| 813 |
53640 |
cp->holddown = 0; |
| 814 |
53640 |
cp->endpoint = VRT_Endpoint_Clone(vep); |
| 815 |
53640 |
CHECK_OBJ_NOTNULL(cp->endpoint, VRT_ENDPOINT_MAGIC); |
| 816 |
53640 |
memcpy(cp->ident, digest, sizeof cp->ident); |
| 817 |
53640 |
if (vep->uds_path != NULL) |
| 818 |
1840 |
cp->methods = &vus_methods; |
| 819 |
|
else |
| 820 |
51800 |
cp->methods = &vtp_methods; |
| 821 |
53640 |
Lck_New(&cp->mtx, lck_conn_pool); |
| 822 |
53640 |
VTAILQ_INIT(&cp->connlist); |
| 823 |
|
|
| 824 |
53640 |
Lck_Lock(&conn_pools_mtx); |
| 825 |
53640 |
cp2 = VRBT_INSERT(vrb, &conn_pools, cp); |
| 826 |
53640 |
if (cp2 == NULL) { |
| 827 |
41480 |
vsc->ref_miss++; |
| 828 |
41480 |
Lck_Unlock(&conn_pools_mtx); |
| 829 |
41480 |
return (cp); |
| 830 |
|
} |
| 831 |
|
|
| 832 |
12160 |
CHECK_OBJ(cp2, CONN_POOL_MAGIC); |
| 833 |
12160 |
assert(cp2->refcnt > 0); |
| 834 |
12160 |
cp2->refcnt++; |
| 835 |
12160 |
vsc->ref_hit++; |
| 836 |
12160 |
Lck_Unlock(&conn_pools_mtx); |
| 837 |
|
|
| 838 |
12160 |
vcp_destroy(&cp); |
| 839 |
12160 |
return (cp2); |
| 840 |
53640 |
} |