| | varnish-cache/bin/varnishd/acceptor/cache_acceptor.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
* |
| 30 |
|
* This source file has the various trickery surrounding the accept/listen |
| 31 |
|
* sockets. |
| 32 |
|
* |
| 33 |
|
*/ |
| 34 |
|
|
| 35 |
|
#include "config.h" |
| 36 |
|
|
| 37 |
|
#include <stdlib.h> |
| 38 |
|
#include <netinet/in.h> |
| 39 |
|
#include <netinet/tcp.h> |
| 40 |
|
|
| 41 |
|
#include "cache/cache_varnishd.h" |
| 42 |
|
#include "acceptor/cache_acceptor.h" |
| 43 |
|
#include "acceptor/acceptor_priv.h" |
| 44 |
|
|
| 45 |
|
#include "cache/cache_transport.h" |
| 46 |
|
#include "cache/cache_pool.h" |
| 47 |
|
#include "common/heritage.h" |
| 48 |
|
|
| 49 |
|
#include "vcli_serve.h" |
| 50 |
|
#include "vtim.h" |
| 51 |
|
|
| 52 |
|
unsigned pool_accepting; |
| 53 |
|
|
| 54 |
|
static pthread_t VCA_thread; |
| 55 |
|
static vtim_dur vca_pace = 0.0; |
| 56 |
|
static struct lock pace_mtx; |
| 57 |
|
static struct lock shut_mtx; |
| 58 |
|
|
| 59 |
|
/*-------------------------------------------------------------------- |
| 60 |
|
* lacking a better place, we put some generic periodic updates |
| 61 |
|
* into the vca_acct() loop which we are running anyway |
| 62 |
|
*/ |
| 63 |
|
static void |
| 64 |
100717 |
vca_periodic(vtim_real t0) |
| 65 |
|
{ |
| 66 |
|
vtim_real now; |
| 67 |
|
|
| 68 |
100717 |
now = VTIM_real(); |
| 69 |
100717 |
VSC_C_main->uptime = (uint64_t)(now - t0); |
| 70 |
|
|
| 71 |
100717 |
VTIM_postel = FEATURE(FEATURE_HTTP_DATE_POSTEL); |
| 72 |
100717 |
} |
| 73 |
|
|
| 74 |
|
/*-------------------------------------------------------------------- |
| 75 |
|
* If accept(2)'ing fails, we pace ourselves to relive any resource |
| 76 |
|
* shortage if possible. |
| 77 |
|
*/ |
| 78 |
|
|
| 79 |
|
void |
| 80 |
163634 |
vca_pace_check(void) |
| 81 |
|
{ |
| 82 |
|
vtim_dur p; |
| 83 |
|
|
| 84 |
163634 |
if (vca_pace == 0.0) |
| 85 |
163634 |
return; |
| 86 |
0 |
Lck_Lock(&pace_mtx); |
| 87 |
0 |
p = vca_pace; |
| 88 |
0 |
Lck_Unlock(&pace_mtx); |
| 89 |
0 |
if (p > 0.0) |
| 90 |
0 |
VTIM_sleep(p); |
| 91 |
163634 |
} |
| 92 |
|
|
| 93 |
|
void |
| 94 |
0 |
vca_pace_bad(void) |
| 95 |
|
{ |
| 96 |
|
|
| 97 |
0 |
Lck_Lock(&pace_mtx); |
| 98 |
0 |
vca_pace += cache_param->acceptor_sleep_incr; |
| 99 |
0 |
if (vca_pace > cache_param->acceptor_sleep_max) |
| 100 |
0 |
vca_pace = cache_param->acceptor_sleep_max; |
| 101 |
0 |
Lck_Unlock(&pace_mtx); |
| 102 |
0 |
} |
| 103 |
|
|
| 104 |
|
void |
| 105 |
88493 |
vca_pace_good(void) |
| 106 |
|
{ |
| 107 |
|
|
| 108 |
88493 |
if (vca_pace == 0.0) |
| 109 |
88493 |
return; |
| 110 |
0 |
Lck_Lock(&pace_mtx); |
| 111 |
0 |
vca_pace *= cache_param->acceptor_sleep_decay; |
| 112 |
0 |
if (vca_pace < cache_param->acceptor_sleep_incr) |
| 113 |
0 |
vca_pace = 0.0; |
| 114 |
0 |
Lck_Unlock(&pace_mtx); |
| 115 |
88493 |
} |
| 116 |
|
|
| 117 |
|
/*-------------------------------------------------------------------- |
| 118 |
|
* Called when a worker and attached thread pool is created, to |
| 119 |
|
* allocate the tasks which will listen to sockets for that pool. |
| 120 |
|
*/ |
| 121 |
|
|
| 122 |
|
void |
| 123 |
75189 |
VCA_NewPool(struct pool *pp) |
| 124 |
|
{ |
| 125 |
|
struct acceptor *vca; |
| 126 |
|
|
| 127 |
225567 |
VCA_Foreach(vca) { |
| 128 |
150378 |
CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC); |
| 129 |
150378 |
vca->accept(pp); |
| 130 |
|
} |
| 131 |
75189 |
} |
| 132 |
|
|
| 133 |
|
void |
| 134 |
80 |
VCA_DestroyPool(struct pool *pp) |
| 135 |
|
{ |
| 136 |
|
struct poolsock *ps; |
| 137 |
|
|
| 138 |
160 |
while (!VTAILQ_EMPTY(&pp->poolsocks)) { |
| 139 |
80 |
ps = VTAILQ_FIRST(&pp->poolsocks); |
| 140 |
80 |
VTAILQ_REMOVE(&pp->poolsocks, ps, list); |
| 141 |
|
} |
| 142 |
80 |
} |
| 143 |
|
|
| 144 |
|
/*--------------------------------------------------------------------*/ |
| 145 |
|
|
| 146 |
|
static void * v_matchproto_() |
| 147 |
0 |
vca_acct(void *arg) |
| 148 |
|
{ |
| 149 |
|
struct acceptor *vca; |
| 150 |
|
vtim_real t0; |
| 151 |
|
|
| 152 |
|
// XXX Actually a misnomer now because the accept happens in a pool |
| 153 |
|
// thread. Rename to accept-nanny or so? |
| 154 |
0 |
THR_SetName("cache-acceptor"); |
| 155 |
0 |
THR_Init(); |
| 156 |
0 |
(void)arg; |
| 157 |
|
|
| 158 |
0 |
t0 = VTIM_real(); |
| 159 |
0 |
vca_periodic(t0); |
| 160 |
|
|
| 161 |
0 |
pool_accepting = 1; |
| 162 |
|
|
| 163 |
62877 |
while (1) { |
| 164 |
62877 |
(void)sleep(1); |
| 165 |
|
|
| 166 |
188631 |
VCA_Foreach(vca) { |
| 167 |
125754 |
CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC); |
| 168 |
125754 |
vca->update(&shut_mtx); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
62877 |
vca_periodic(t0); |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
NEEDLESS(return (NULL)); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
/*--------------------------------------------------------------------*/ |
| 178 |
|
|
| 179 |
|
void |
| 180 |
37840 |
VCA_Start(struct cli *cli) |
| 181 |
|
{ |
| 182 |
|
struct acceptor *vca; |
| 183 |
|
|
| 184 |
37840 |
ASSERT_CLI(); |
| 185 |
|
|
| 186 |
113520 |
VCA_Foreach(vca) { |
| 187 |
75680 |
CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC); |
| 188 |
75680 |
vca->start(cli); |
| 189 |
|
} |
| 190 |
|
|
| 191 |
37840 |
PTOK(pthread_create(&VCA_thread, NULL, vca_acct, NULL)); |
| 192 |
37840 |
} |
| 193 |
|
|
| 194 |
|
/*--------------------------------------------------------------------*/ |
| 195 |
|
|
| 196 |
|
static void v_matchproto_(cli_func_t) |
| 197 |
75160 |
ccf_listen_address(struct cli *cli, const char * const *av, void *priv) |
| 198 |
|
{ |
| 199 |
|
struct listen_sock *ls; |
| 200 |
|
|
| 201 |
75160 |
(void)av; |
| 202 |
75160 |
(void)priv; |
| 203 |
|
|
| 204 |
|
/* |
| 205 |
|
* This CLI command is primarily used by varnishtest. Don't |
| 206 |
|
* respond until listen(2) has been called, in order to avoid |
| 207 |
|
* a race where varnishtest::client would attempt to connect(2) |
| 208 |
|
* before listen(2) has been called. |
| 209 |
|
*/ |
| 210 |
75160 |
while (!pool_accepting) |
| 211 |
0 |
VTIM_sleep(.1); |
| 212 |
|
|
| 213 |
75160 |
Lck_Lock(&shut_mtx); |
| 214 |
|
|
| 215 |
|
/* |
| 216 |
|
* Varnishtest expects the list of listen sockets to come out in the |
| 217 |
|
* same order as it is specified on the command line. |
| 218 |
|
*/ |
| 219 |
150800 |
VTAILQ_FOREACH(ls, &heritage.socks, list) { |
| 220 |
75640 |
CHECK_OBJ_NOTNULL(ls, LISTEN_SOCK_MAGIC); |
| 221 |
75640 |
CHECK_OBJ_NOTNULL(ls->vca, ACCEPTOR_MAGIC); |
| 222 |
75640 |
ls->vca->event(cli, ls, VCA_EVENT_LADDR); |
| 223 |
75640 |
} |
| 224 |
|
|
| 225 |
75160 |
Lck_Unlock(&shut_mtx); |
| 226 |
75160 |
} |
| 227 |
|
|
| 228 |
|
/*--------------------------------------------------------------------*/ |
| 229 |
|
|
| 230 |
|
static struct cli_proto vca_cmds[] = { |
| 231 |
|
{ CLICMD_DEBUG_LISTEN_ADDRESS, "d", ccf_listen_address }, |
| 232 |
|
{ NULL } |
| 233 |
|
}; |
| 234 |
|
|
| 235 |
|
void |
| 236 |
38029 |
VCA_Init(void) |
| 237 |
|
{ |
| 238 |
|
struct acceptor *vca; |
| 239 |
|
|
| 240 |
38029 |
CLI_AddFuncs(vca_cmds); |
| 241 |
38029 |
Lck_New(&pace_mtx, lck_vcapace); |
| 242 |
38029 |
Lck_New(&shut_mtx, lck_vcashut); |
| 243 |
|
|
| 244 |
114087 |
VCA_Foreach(vca) { |
| 245 |
76058 |
CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC); |
| 246 |
76058 |
vca->init(); |
| 247 |
|
} |
| 248 |
38029 |
} |
| 249 |
|
|
| 250 |
|
void |
| 251 |
37520 |
VCA_Shutdown(void) |
| 252 |
|
{ |
| 253 |
|
struct acceptor *vca; |
| 254 |
|
|
| 255 |
37520 |
Lck_Lock(&shut_mtx); |
| 256 |
|
|
| 257 |
112560 |
VCA_Foreach(vca) { |
| 258 |
75040 |
CHECK_OBJ_NOTNULL(vca, ACCEPTOR_MAGIC); |
| 259 |
75040 |
vca->shutdown(); |
| 260 |
|
} |
| 261 |
|
|
| 262 |
37520 |
Lck_Unlock(&shut_mtx); |
| 263 |
37520 |
} |
| 264 |
|
|
| 265 |
|
/*-------------------------------------------------------------------- |
| 266 |
|
* Transport protocol registration |
| 267 |
|
* |
| 268 |
|
*/ |
| 269 |
|
|
| 270 |
|
static VTAILQ_HEAD(,transport) transports = |
| 271 |
|
VTAILQ_HEAD_INITIALIZER(transports); |
| 272 |
|
|
| 273 |
|
static uint16_t next_xport; |
| 274 |
|
|
| 275 |
|
void |
| 276 |
123720 |
XPORT_Register(struct transport *xp) |
| 277 |
|
{ |
| 278 |
|
|
| 279 |
123720 |
CHECK_OBJ_NOTNULL(xp, TRANSPORT_MAGIC); |
| 280 |
123720 |
AZ(xp->number); |
| 281 |
|
|
| 282 |
123720 |
xp->number = ++next_xport; |
| 283 |
123720 |
VTAILQ_INSERT_TAIL(&transports, xp, list); |
| 284 |
123720 |
} |
| 285 |
|
|
| 286 |
|
void |
| 287 |
41240 |
XPORT_Init(void) |
| 288 |
|
{ |
| 289 |
|
|
| 290 |
41240 |
ASSERT_MGT(); |
| 291 |
|
|
| 292 |
|
#define TRANSPORT_MACRO(name) XPORT_Register(&name##_transport); |
| 293 |
41240 |
TRANSPORTS |
| 294 |
|
#undef TRANSPORT_MACRO |
| 295 |
41240 |
} |
| 296 |
|
|
| 297 |
|
const struct transport * |
| 298 |
40160 |
XPORT_Find(const char *name) |
| 299 |
|
{ |
| 300 |
|
const struct transport *xp; |
| 301 |
|
|
| 302 |
40160 |
ASSERT_MGT(); |
| 303 |
|
|
| 304 |
79720 |
VTAILQ_FOREACH(xp, &transports, list) |
| 305 |
79640 |
if (xp->proto_ident != NULL && |
| 306 |
79640 |
!strcasecmp(xp->proto_ident, name)) |
| 307 |
40080 |
return (xp); |
| 308 |
80 |
return (NULL); |
| 309 |
40160 |
} |
| 310 |
|
|
| 311 |
|
const struct transport * |
| 312 |
97697 |
XPORT_ByNumber(uint16_t no) |
| 313 |
|
{ |
| 314 |
|
const struct transport *xp; |
| 315 |
|
|
| 316 |
195438 |
VTAILQ_FOREACH(xp, &transports, list) |
| 317 |
195438 |
if (xp->number == no) |
| 318 |
97697 |
return (xp); |
| 319 |
0 |
return (NULL); |
| 320 |
97697 |
} |