varnish-cache/bin/varnishd/http1/cache_http1_line.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
 * Write data to fd
31
 * We try to use writev() if possible in order to minimize number of
32
 * syscalls made and packets sent.  It also just might allow the worker
33
 * thread to complete the request without holding stuff locked.
34
 *
35
 * XXX: chunked header (generated in Flush) and Tail (EndChunk)
36
 *      are not accounted by means of the size_t returned. Obvious ideas:
37
 *      - add size_t return value to Flush and EndChunk
38
 *      - base accounting on (struct v1l).cnt
39
 */
40
41
#include "config.h"
42
43
#include <sys/uio.h>
44
#include "cache/cache_varnishd.h"
45
#include "cache/cache_filter.h"
46
47
#include <stdio.h>
48
49
#include "cache_http1.h"
50
#include "vtim.h"
51
52
/*--------------------------------------------------------------------*/
53
54
struct v1l {
55
        unsigned                magic;
56
#define V1L_MAGIC               0x2f2142e5
57
        int                     *wfd;
58
        stream_close_t          werr;   /* valid after V1L_Flush() */
59
        struct iovec            *iov;
60
        unsigned                siov;
61
        unsigned                niov;
62
        ssize_t                 liov;
63
        ssize_t                 cliov;
64
        unsigned                ciov;   /* Chunked header marker */
65
        vtim_real               deadline;
66
        struct vsl_log          *vsl;
67
        ssize_t                 cnt;    /* Flushed byte count */
68
        struct ws               *ws;
69
        uintptr_t               ws_snap;
70
};
71
72
/*--------------------------------------------------------------------
73
 * for niov == 0, reserve the ws for max number of iovs
74
 * otherwise, up to niov
75
 */
76
77
void
78 119948
V1L_Open(struct worker *wrk, struct ws *ws, int *fd, struct vsl_log *vsl,
79
    vtim_real deadline, unsigned niov)
80
{
81
        struct v1l *v1l;
82
        unsigned u;
83
        uintptr_t ws_snap;
84
85 119948
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
86 119948
        AZ(wrk->v1l);
87
88 119948
        if (WS_Overflowed(ws))
89 0
                return;
90
91 119948
        if (niov != 0)
92 68800
                assert(niov >= 3);
93
94 119948
        ws_snap = WS_Snapshot(ws);
95
96 119948
        v1l = WS_Alloc(ws, sizeof *v1l);
97 119948
        if (v1l == NULL)
98 0
                return;
99 119948
        INIT_OBJ(v1l, V1L_MAGIC);
100
101 119948
        v1l->ws = ws;
102 119948
        v1l->ws_snap = ws_snap;
103
104 119948
        u = WS_ReserveLumps(ws, sizeof(struct iovec));
105 119948
        if (u < 3) {
106
                /* Must have at least 3 in case of chunked encoding */
107 0
                WS_Release(ws, 0);
108 0
                WS_MarkOverflow(ws);
109 0
                return;
110
        }
111 119948
        if (u > IOV_MAX)
112 0
                u = IOV_MAX;
113 119948
        if (niov != 0 && u > niov)
114 66896
                u = niov;
115 119948
        v1l->iov = WS_Reservation(ws);
116 119948
        v1l->siov = u;
117 119948
        v1l->ciov = u;
118 119948
        v1l->wfd = fd;
119 119948
        v1l->deadline = deadline;
120 119948
        v1l->vsl = vsl;
121 119948
        v1l->werr = SC_NULL;
122
123 119948
        AZ(wrk->v1l);
124 119948
        wrk->v1l = v1l;
125
126 119948
        WS_Release(ws, u * sizeof(struct iovec));
127 119948
}
128
129
stream_close_t
130 119962
V1L_Close(struct worker *wrk, uint64_t *cnt)
131
{
132
        struct v1l *v1l;
133
        struct ws *ws;
134
        uintptr_t ws_snap;
135
        stream_close_t sc;
136
137 119962
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
138 119962
        AN(cnt);
139 119962
        sc = V1L_Flush(wrk);
140 119962
        TAKE_OBJ_NOTNULL(v1l, &wrk->v1l, V1L_MAGIC);
141 119962
        *cnt = v1l->cnt;
142 119962
        ws = v1l->ws;
143 119962
        ws_snap = v1l->ws_snap;
144 119962
        ZERO_OBJ(v1l, sizeof *v1l);
145 119962
        WS_Rollback(ws, ws_snap);
146 119962
        return (sc);
147
}
148
149
static void
150 498
v1l_prune(struct v1l *v1l, size_t bytes)
151
{
152 498
        ssize_t used = 0;
153
        ssize_t j, used_here;
154
155 1789
        for (j = 0; j < v1l->niov; j++) {
156 1789
                if (used + v1l->iov[j].iov_len > bytes) {
157
                        /* Cutoff is in this iov */
158 498
                        used_here = bytes - used;
159 498
                        v1l->iov[j].iov_len -= used_here;
160 498
                        v1l->iov[j].iov_base =
161 498
                            (char*)v1l->iov[j].iov_base + used_here;
162 996
                        memmove(v1l->iov, &v1l->iov[j],
163 498
                            (v1l->niov - j) * sizeof(struct iovec));
164 498
                        v1l->niov -= j;
165 498
                        v1l->liov -= bytes;
166 498
                        return;
167
                }
168 1291
                used += v1l->iov[j].iov_len;
169 1291
        }
170 0
        AZ(v1l->liov);
171 498
}
172
173
stream_close_t
174 242879
V1L_Flush(const struct worker *wrk)
175
{
176
        ssize_t i;
177
        int err;
178
        struct v1l *v1l;
179
        char cbuf[32];
180
181 242879
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
182 242879
        v1l = wrk->v1l;
183 242879
        CHECK_OBJ_NOTNULL(v1l, V1L_MAGIC);
184 242879
        CHECK_OBJ_NOTNULL(v1l->werr, STREAM_CLOSE_MAGIC);
185 242879
        AN(v1l->wfd);
186
187 242879
        assert(v1l->niov <= v1l->siov);
188
189 242879
        if (*v1l->wfd >= 0 && v1l->liov > 0 && v1l->werr == SC_NULL) {
190 164662
                if (v1l->ciov < v1l->siov && v1l->cliov > 0) {
191
                        /* Add chunk head & tail */
192 26149
                        bprintf(cbuf, "00%zx\r\n", v1l->cliov);
193 26149
                        i = strlen(cbuf);
194 26149
                        v1l->iov[v1l->ciov].iov_base = cbuf;
195 26149
                        v1l->iov[v1l->ciov].iov_len = i;
196 26149
                        v1l->liov += i;
197
198
                        /* This is OK, because siov was --'ed */
199 26149
                        v1l->iov[v1l->niov].iov_base = cbuf + i - 2;
200 26149
                        v1l->iov[v1l->niov++].iov_len = 2;
201 26149
                        v1l->liov += 2;
202 164662
                } else if (v1l->ciov < v1l->siov) {
203 1546
                        v1l->iov[v1l->ciov].iov_base = cbuf;
204 1546
                        v1l->iov[v1l->ciov].iov_len = 0;
205 1546
                }
206
207 164662
                i = 0;
208 164662
                err = 0;
209 164662
                do {
210 165840
                        if (VTIM_real() > v1l->deadline) {
211 200
                                VSLb(v1l->vsl, SLT_Debug,
212
                                    "Hit total send timeout, "
213
                                    "wrote = %zd/%zd; not retrying",
214 100
                                    i, v1l->liov);
215 100
                                i = -1;
216 100
                                break;
217
                        }
218
219 165740
                        i = writev(*v1l->wfd, v1l->iov, v1l->niov);
220 165740
                        if (i > 0)
221 164870
                                v1l->cnt += i;
222
223 165740
                        if (i == v1l->liov)
224 164371
                                break;
225
226
                        /* we hit a timeout, and some data may have been sent:
227
                         * Remove sent data from start of I/O vector, then retry
228
                         *
229
                         * XXX: Add a "minimum sent data per timeout counter to
230
                         * prevent slowloris attacks
231
                         */
232
233 1369
                        err = errno;
234
235 1369
                        if (err == EWOULDBLOCK) {
236 1340
                                VSLb(v1l->vsl, SLT_Debug,
237
                                    "Hit idle send timeout, "
238
                                    "wrote = %zd/%zd; retrying",
239 670
                                    i, v1l->liov);
240 670
                        }
241
242 1369
                        if (i > 0)
243 499
                                v1l_prune(v1l, i);
244 1369
                } while (i > 0 || err == EWOULDBLOCK);
245
246 164662
                if (i <= 0) {
247 600
                        VSLb(v1l->vsl, SLT_Debug,
248
                            "Write error, retval = %zd, len = %zd, errno = %s",
249 300
                            i, v1l->liov, VAS_errtxt(err));
250 300
                        assert(v1l->werr == SC_NULL);
251 300
                        if (err == EPIPE)
252 196
                                v1l->werr = SC_REM_CLOSE;
253
                        else
254 104
                                v1l->werr = SC_TX_ERROR;
255 300
                        errno = err;
256 300
                }
257 164662
        }
258 242885
        v1l->liov = 0;
259 242885
        v1l->cliov = 0;
260 242885
        v1l->niov = 0;
261 242885
        if (v1l->ciov < v1l->siov)
262 50859
                v1l->ciov = v1l->niov++;
263 242867
        CHECK_OBJ_NOTNULL(v1l->werr, STREAM_CLOSE_MAGIC);
264 242867
        return (v1l->werr);
265
}
266
267
size_t
268 2745280
V1L_Write(const struct worker *wrk, const void *ptr, ssize_t len)
269
{
270
        struct v1l *v1l;
271
272 2745280
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
273 2745280
        v1l = wrk->v1l;
274 2745280
        CHECK_OBJ_NOTNULL(v1l, V1L_MAGIC);
275 2745280
        AN(v1l->wfd);
276 2745280
        if (len == 0 || *v1l->wfd < 0)
277 182
                return (0);
278 2745112
        if (len == -1)
279 1393386
                len = strlen(ptr);
280 2745112
        assert(v1l->niov < v1l->siov);
281 2745112
        v1l->iov[v1l->niov].iov_base = TRUST_ME(ptr);
282 2745112
        v1l->iov[v1l->niov].iov_len = len;
283 2745112
        v1l->liov += len;
284 2745112
        v1l->niov++;
285 2745112
        v1l->cliov += len;
286 2745112
        if (v1l->niov >= v1l->siov) {
287 2750
                (void)V1L_Flush(wrk);
288 2750
                VSC_C_main->http1_iovs_flush++;
289 2750
        }
290 2745112
        return (len);
291 2745112
}
292
293
void
294 6605
V1L_Chunked(const struct worker *wrk)
295
{
296
        struct v1l *v1l;
297
298 6605
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
299 6605
        v1l = wrk->v1l;
300 6605
        CHECK_OBJ_NOTNULL(v1l, V1L_MAGIC);
301
302 6605
        assert(v1l->ciov == v1l->siov);
303 6605
        assert(v1l->siov >= 3);
304
        /*
305
         * If there is no space for chunked header, a chunk of data and
306
         * a chunk tail, we might as well flush right away.
307
         */
308 6605
        if (v1l->niov + 3 >= v1l->siov) {
309 0
                (void)V1L_Flush(wrk);
310 0
                VSC_C_main->http1_iovs_flush++;
311 0
        }
312 6605
        v1l->siov--;
313 6605
        v1l->ciov = v1l->niov++;
314 6605
        v1l->cliov = 0;
315 6605
        assert(v1l->ciov < v1l->siov);
316 6605
        assert(v1l->niov < v1l->siov);
317 6605
}
318
319
/*
320
 * XXX: It is not worth the complexity to attempt to get the
321
 * XXX: end of chunk into the V1L_Flush(), because most of the time
322
 * XXX: if not always, that is a no-op anyway, because the calling
323
 * XXX: code already called V1L_Flush() to release local storage.
324
 */
325
326
void
327 6076
V1L_EndChunk(const struct worker *wrk)
328
{
329
        struct v1l *v1l;
330
331 6076
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
332 6076
        v1l = wrk->v1l;
333 6076
        CHECK_OBJ_NOTNULL(v1l, V1L_MAGIC);
334
335 6076
        assert(v1l->ciov < v1l->siov);
336 6076
        (void)V1L_Flush(wrk);
337 6076
        v1l->siov++;
338 6076
        v1l->ciov = v1l->siov;
339 6076
        v1l->niov = 0;
340 6076
        v1l->cliov = 0;
341 6076
        (void)V1L_Write(wrk, "0\r\n\r\n", -1);
342 6076
}
343
344
/*--------------------------------------------------------------------
345
 * VDP using V1L
346
 */
347
348
static int v_matchproto_(vdp_bytes_f)
349 115498
v1l_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
350
    const void *ptr, ssize_t len)
351
{
352 115498
        ssize_t wl = 0;
353
354 115498
        CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
355 115498
        (void)priv;
356
357 115498
        AZ(vdc->nxt);           /* always at the bottom of the pile */
358
359 115498
        if (len > 0)
360 82331
                wl = V1L_Write(vdc->wrk, ptr, len);
361 115498
        if (act > VDP_NULL && V1L_Flush(vdc->wrk) != SC_NULL)
362 298
                return (-1);
363 115200
        if (len != wl)
364 0
                return (-1);
365 115200
        return (0);
366 115498
}
367
368
const struct vdp * const VDP_v1l = &(struct vdp){
369
        .name =         "V1B",
370
        .bytes =        v1l_bytes,
371
};