| | varnish-cache/bin/varnishd/http1/cache_http1_pipe.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2011 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 |
|
* XXX: charge bytes to srcaddr |
| 31 |
|
*/ |
| 32 |
|
|
| 33 |
|
#include "config.h" |
| 34 |
|
|
| 35 |
|
#include "cache/cache_varnishd.h" |
| 36 |
|
|
| 37 |
|
#include <poll.h> |
| 38 |
|
#include <stdio.h> |
| 39 |
|
|
| 40 |
|
#include "cache_http1.h" |
| 41 |
|
#include "vtcp.h" |
| 42 |
|
#include "vtim.h" |
| 43 |
|
|
| 44 |
|
#include "VSC_vbe.h" |
| 45 |
|
|
| 46 |
|
static struct lock pipestat_mtx; |
| 47 |
|
|
| 48 |
|
static int |
| 49 |
2926 |
rdf(int fd0, int fd1, uint64_t *pcnt) |
| 50 |
|
{ |
| 51 |
|
ssize_t i, j; |
| 52 |
|
char buf[BUFSIZ], *p; |
| 53 |
|
|
| 54 |
2926 |
i = read(fd0, buf, sizeof buf); |
| 55 |
2926 |
VTCP_Assert(i); |
| 56 |
2926 |
if (i <= 0) |
| 57 |
1520 |
return (1); |
| 58 |
2812 |
for (p = buf; i > 0; i -= j, p += j) { |
| 59 |
1406 |
j = write(fd1, p, i); |
| 60 |
1406 |
VTCP_Assert(j); |
| 61 |
1406 |
if (j <= 0) |
| 62 |
0 |
return (1); |
| 63 |
1406 |
assert(j <= i); |
| 64 |
1406 |
*pcnt += j; |
| 65 |
1406 |
if (i != j) |
| 66 |
0 |
VTIM_sleep(0.1); /* XXX hack */ |
| 67 |
1406 |
} |
| 68 |
1406 |
return (0); |
| 69 |
2926 |
} |
| 70 |
|
|
| 71 |
|
int |
| 72 |
1000 |
V1P_Enter(void) |
| 73 |
|
{ |
| 74 |
1000 |
int retval = 0; |
| 75 |
|
|
| 76 |
1000 |
Lck_Lock(&pipestat_mtx); |
| 77 |
1000 |
if (cache_param->pipe_sess_max == 0 || |
| 78 |
40 |
VSC_C_main->n_pipe < cache_param->pipe_sess_max) |
| 79 |
960 |
VSC_C_main->n_pipe++; |
| 80 |
|
else |
| 81 |
40 |
retval = -1; |
| 82 |
1000 |
Lck_Unlock(&pipestat_mtx); |
| 83 |
1000 |
return (retval); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
void |
| 87 |
960 |
V1P_Leave(void) |
| 88 |
|
{ |
| 89 |
|
|
| 90 |
960 |
Lck_Lock(&pipestat_mtx); |
| 91 |
960 |
assert(VSC_C_main->n_pipe > 0); |
| 92 |
960 |
VSC_C_main->n_pipe--; |
| 93 |
960 |
Lck_Unlock(&pipestat_mtx); |
| 94 |
960 |
} |
| 95 |
|
|
| 96 |
|
void |
| 97 |
960 |
V1P_Charge(struct req *req, const struct v1p_acct *a, struct VSC_vbe *b) |
| 98 |
|
{ |
| 99 |
|
|
| 100 |
960 |
AN(b); |
| 101 |
1920 |
VSLb(req->vsl, SLT_PipeAcct, "%ju %ju %ju %ju", |
| 102 |
960 |
(uintmax_t)a->req, |
| 103 |
960 |
(uintmax_t)a->bereq, |
| 104 |
960 |
(uintmax_t)a->in, |
| 105 |
960 |
(uintmax_t)a->out); |
| 106 |
|
|
| 107 |
960 |
Lck_Lock(&pipestat_mtx); |
| 108 |
960 |
VSC_C_main->s_pipe_hdrbytes += a->req; |
| 109 |
960 |
VSC_C_main->s_pipe_in += a->in; |
| 110 |
960 |
VSC_C_main->s_pipe_out += a->out; |
| 111 |
960 |
b->pipe_hdrbytes += a->bereq; |
| 112 |
960 |
b->pipe_out += a->in; |
| 113 |
960 |
b->pipe_in += a->out; |
| 114 |
960 |
Lck_Unlock(&pipestat_mtx); |
| 115 |
960 |
} |
| 116 |
|
|
| 117 |
|
stream_close_t |
| 118 |
920 |
V1P_Process(const struct req *req, int fd, struct v1p_acct *v1a, |
| 119 |
|
vtim_real deadline) |
| 120 |
|
{ |
| 121 |
|
struct pollfd fds[2]; |
| 122 |
|
vtim_dur tmo, tmo_task; |
| 123 |
|
stream_close_t sc; |
| 124 |
|
int i, j; |
| 125 |
|
|
| 126 |
920 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
| 127 |
920 |
CHECK_OBJ_NOTNULL(req->sp, SESS_MAGIC); |
| 128 |
920 |
assert(fd > 0); |
| 129 |
|
|
| 130 |
920 |
if (req->htc->pipeline_b != NULL) { |
| 131 |
240 |
j = write(fd, req->htc->pipeline_b, |
| 132 |
120 |
req->htc->pipeline_e - req->htc->pipeline_b); |
| 133 |
120 |
VTCP_Assert(j); |
| 134 |
120 |
if (j < 0) |
| 135 |
0 |
return (SC_OVERLOAD); |
| 136 |
120 |
req->htc->pipeline_b = NULL; |
| 137 |
120 |
req->htc->pipeline_e = NULL; |
| 138 |
120 |
v1a->in += j; |
| 139 |
120 |
} |
| 140 |
920 |
memset(fds, 0, sizeof fds); |
| 141 |
920 |
fds[0].fd = fd; |
| 142 |
920 |
fds[0].events = POLLIN; |
| 143 |
920 |
fds[1].fd = req->sp->fd; |
| 144 |
920 |
fds[1].events = POLLIN; |
| 145 |
|
|
| 146 |
920 |
sc = SC_TX_PIPE; |
| 147 |
3086 |
while (fds[0].fd > -1 || fds[1].fd > -1) { |
| 148 |
3086 |
fds[0].revents = 0; |
| 149 |
3086 |
fds[1].revents = 0; |
| 150 |
3086 |
tmo = cache_param->pipe_timeout; |
| 151 |
3086 |
if (tmo == 0.) |
| 152 |
280 |
tmo = INFINITY; |
| 153 |
3086 |
if (deadline > 0.) { |
| 154 |
644 |
tmo_task = deadline - VTIM_real(); |
| 155 |
644 |
tmo = vmin(tmo, tmo_task); |
| 156 |
644 |
} |
| 157 |
3086 |
i = poll(fds, 2, VTIM_poll_tmo(tmo)); |
| 158 |
3086 |
if (i == 0) |
| 159 |
160 |
sc = SC_RX_TIMEOUT; |
| 160 |
3086 |
if (i < 1) |
| 161 |
160 |
break; |
| 162 |
2926 |
if (fds[0].revents && |
| 163 |
2124 |
rdf(fd, req->sp->fd, &v1a->out)) { |
| 164 |
760 |
if (fds[1].fd == -1) |
| 165 |
40 |
break; |
| 166 |
720 |
(void)shutdown(fd, SHUT_RD); |
| 167 |
720 |
(void)shutdown(req->sp->fd, SHUT_WR); |
| 168 |
720 |
fds[0].events = 0; |
| 169 |
720 |
fds[0].fd = -1; |
| 170 |
720 |
} |
| 171 |
2886 |
if (fds[1].revents && |
| 172 |
802 |
rdf(req->sp->fd, fd, &v1a->in)) { |
| 173 |
760 |
if (fds[0].fd == -1) |
| 174 |
720 |
break; |
| 175 |
40 |
(void)shutdown(req->sp->fd, SHUT_RD); |
| 176 |
40 |
(void)shutdown(fd, SHUT_WR); |
| 177 |
40 |
fds[1].events = 0; |
| 178 |
40 |
fds[1].fd = -1; |
| 179 |
40 |
} |
| 180 |
|
} |
| 181 |
|
|
| 182 |
920 |
return (sc); |
| 183 |
920 |
} |
| 184 |
|
|
| 185 |
|
/*--------------------------------------------------------------------*/ |
| 186 |
|
|
| 187 |
|
void |
| 188 |
38027 |
V1P_Init(void) |
| 189 |
|
{ |
| 190 |
|
|
| 191 |
38027 |
Lck_New(&pipestat_mtx, lck_pipestat); |
| 192 |
38027 |
} |