varnish-cache/bin/varnishd/http2/cache_http2_send.c
0
/*-
1
 * Copyright (c) 2016 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
 */
30
31
#include "config.h"
32
33
#include <sys/uio.h>
34
35
#include "cache/cache_varnishd.h"
36
37
#include "cache/cache_transport.h"
38
#include "http2/cache_http2.h"
39
40
#include "vend.h"
41
#include "vtim.h"
42
43
#define H2_SEND_HELD(h2, r2) (VTAILQ_FIRST(&(h2)->txqueue) == (r2))
44
45
static h2_error
46 1665
h2_errcheck(const struct h2_req *r2, const struct h2_sess *h2)
47
{
48 1665
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
49 1665
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
50
51 1665
        if (r2->error != NULL)
52 241
                return (r2->error);
53 1424
        if (h2->error != NULL && r2->stream > h2->goaway_last_stream)
54 5
                return (h2->error);
55 1419
        return (NULL);
56 1665
}
57
58
static int
59 42
h2_cond_wait(pthread_cond_t *cond, struct h2_sess *h2, struct h2_req *r2)
60
{
61 42
        vtim_dur tmo = 0.;
62
        vtim_real now;
63
        h2_error h2e;
64
        int r;
65
66 42
        AN(cond);
67 42
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
68 42
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
69
70 42
        Lck_AssertHeld(&h2->sess->mtx);
71
72 42
        if (cache_param->h2_window_timeout > 0.)
73 42
                tmo = cache_param->h2_window_timeout;
74
75 42
        r = Lck_CondWaitTimeout(cond, &h2->sess->mtx, tmo);
76 42
        assert(r == 0 || r == ETIMEDOUT);
77
78 42
        now = VTIM_real();
79
80
        /* NB: when we grab h2_window_timeout before acquiring the session
81
         * lock we may time out, but once we wake up both send_timeout and
82
         * h2_window_timeout may have changed meanwhile. For this reason
83
         * h2_stream_tmo() may not log what timed out and we need to call
84
         * again with a magic NAN "now" that indicates to h2_stream_tmo()
85
         * that the stream reached the h2_window_timeout via the lock and
86
         * force it to log it.
87
         */
88 42
        h2e = h2_stream_tmo(h2, r2, now);
89 42
        if (h2e == NULL && r == ETIMEDOUT) {
90 3
                h2e = h2_stream_tmo(h2, r2, NAN);
91 3
                AN(h2e);
92 3
        }
93
94 42
        if (r2->error == NULL)
95 39
                r2->error = h2e;
96
97 42
        return (h2e != NULL ? -1 : 0);
98
}
99
100
static void
101 2308
h2_send_get_locked(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
102
{
103
104 2308
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
105 2308
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
106 2308
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
107
108 2308
        Lck_AssertHeld(&h2->sess->mtx);
109 2308
        if (&wrk->cond == h2->cond)
110 1167
                ASSERT_RXTHR(h2);
111 2308
        r2->wrk = wrk;
112 2308
        VTAILQ_INSERT_TAIL(&h2->txqueue, r2, tx_list);
113 2334
        while (!H2_SEND_HELD(h2, r2))
114 26
                AZ(Lck_CondWait(&wrk->cond, &h2->sess->mtx));
115 2308
        r2->wrk = NULL;
116 2308
}
117
118
void
119 2275
H2_Send_Get(struct worker *wrk, struct h2_sess *h2, struct h2_req *r2)
120
{
121
122 2275
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
123 2275
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
124 2275
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
125
126 2275
        Lck_Lock(&h2->sess->mtx);
127 2275
        h2_send_get_locked(wrk, h2, r2);
128 2275
        Lck_Unlock(&h2->sess->mtx);
129 2275
}
130
131
static void
132 2308
h2_send_rel_locked(struct h2_sess *h2, const struct h2_req *r2)
133
{
134 2308
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
135 2308
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
136
137 2308
        Lck_AssertHeld(&h2->sess->mtx);
138 2308
        AN(H2_SEND_HELD(h2, r2));
139 2308
        VTAILQ_REMOVE(&h2->txqueue, r2, tx_list);
140 2308
        r2 = VTAILQ_FIRST(&h2->txqueue);
141 2308
        if (r2 != NULL) {
142 26
                CHECK_OBJ_NOTNULL(r2->wrk, WORKER_MAGIC);
143 26
                PTOK(pthread_cond_signal(&r2->wrk->cond));
144 26
        }
145 2308
}
146
147
void
148 2275
H2_Send_Rel(struct h2_sess *h2, const struct h2_req *r2)
149
{
150 2275
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
151 2275
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
152
153 2275
        Lck_Lock(&h2->sess->mtx);
154 2275
        h2_send_rel_locked(h2, r2);
155 2275
        Lck_Unlock(&h2->sess->mtx);
156 2275
}
157
158
static void
159 2257
h2_mk_hdr(uint8_t *hdr, h2_frame ftyp, uint8_t flags,
160
    uint32_t len, uint32_t stream)
161
{
162
163 2257
        AN(hdr);
164 2257
        assert(len < (1U << 24));
165 2257
        vbe32enc(hdr, len << 8);
166 2257
        hdr[3] = ftyp->type;
167 2257
        hdr[4] = flags;
168 2257
        vbe32enc(hdr + 5, stream);
169 2257
}
170
171
/*
172
 * This is the "raw" frame sender, all per-stream accounting and
173
 * prioritization must have happened before this is called, and
174
 * the session mtx must be held.
175
 */
176
177
void
178 2257
H2_Send_Frame(struct worker *wrk, struct h2_sess *h2,
179
    h2_frame ftyp, uint8_t flags,
180
    uint32_t len, uint32_t stream, const void *ptr)
181
{
182
        uint8_t hdr[9];
183
        ssize_t s;
184
        struct iovec iov[2];
185
186 2257
        (void)wrk;
187
188 2257
        AN(ftyp);
189 2257
        AZ(flags & ~(ftyp->flags));
190 2257
        if (stream == 0)
191 1461
                AZ(ftyp->act_szero);
192
        else
193 796
                AZ(ftyp->act_snonzero);
194
195 2257
        h2_mk_hdr(hdr, ftyp, flags, len, stream);
196 2257
        Lck_Lock(&h2->sess->mtx);
197 2257
        VSLb_bin(h2->vsl, SLT_H2TxHdr, 9, hdr);
198 2257
        h2->srq->acct.resp_hdrbytes += 9;
199 2257
        if (ftyp->overhead)
200 1693
                h2->srq->acct.resp_bodybytes += len;
201 2257
        Lck_Unlock(&h2->sess->mtx);
202
203 2257
        memset(iov, 0, sizeof iov);
204 2257
        iov[0].iov_base = (void*)hdr;
205 2257
        iov[0].iov_len = sizeof hdr;
206 2257
        iov[1].iov_base = TRUST_ME(ptr);
207 2257
        iov[1].iov_len = len;
208 2257
        s = writev(h2->sess->fd, iov, len == 0 ? 1 : 2);
209 2257
        if (s != sizeof hdr + len) {
210 9
                if (errno == EWOULDBLOCK) {
211 0
                        H2S_Lock_VSLb(h2, SLT_SessError,
212 0
                             "H2: stream %u: Hit idle_send_timeout", stream);
213 0
                }
214
                else {
215 18
                        H2S_Lock_VSLb(h2, SLT_Debug,
216
                            "H2: stream %u: write error s=%zd/%zu errno=%d",
217 9
                            stream, s, sizeof hdr + len, errno);
218
                }
219
                /*
220
                 * There is no point in being nice here, we will be unable
221
                 * to send a GOAWAY once the code unrolls, so go directly
222
                 * to the finale and be done with it.
223
                 */
224 9
                h2->error = H2CE_PROTOCOL_ERROR;
225 2257
        } else if (len > 0) {
226 1726
                Lck_Lock(&h2->sess->mtx);
227 1726
                VSLb_bin(h2->vsl, SLT_H2TxBody, len, ptr);
228 1726
                Lck_Unlock(&h2->sess->mtx);
229 1726
        }
230 2257
}
231
232
static int64_t
233 150
h2_win_limit(const struct h2_req *r2, const struct h2_sess *h2)
234
{
235
236 150
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
237 150
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
238 150
        CHECK_OBJ_NOTNULL(h2->req0, H2_REQ_MAGIC);
239
240 150
        Lck_AssertHeld(&h2->sess->mtx);
241 150
        return (vmin_t(int64_t, r2->t_window, h2->req0->t_window));
242
}
243
244
static void
245 150
h2_win_charge(struct h2_req *r2, const struct h2_sess *h2, uint32_t w)
246
{
247 150
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
248 150
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
249 150
        CHECK_OBJ_NOTNULL(h2->req0, H2_REQ_MAGIC);
250
251 150
        Lck_AssertHeld(&h2->sess->mtx);
252 150
        r2->t_window -= w;
253 150
        h2->req0->t_window -= w;
254 150
}
255
256
static int64_t
257 246
h2_do_window(struct worker *wrk, struct h2_req *r2,
258
    struct h2_sess *h2, int64_t wanted)
259
{
260 246
        int64_t w = 0;
261
262 246
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
263 246
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
264 246
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
265
266 246
        if (wanted == 0)
267 84
                return (0);
268
269 162
        Lck_Lock(&h2->sess->mtx);
270 162
        if (r2->t_window <= 0 || h2->req0->t_window <= 0) {
271 33
                r2->t_winupd = VTIM_real();
272 33
                h2_send_rel_locked(h2, r2);
273
274 33
                assert(h2->winup_streams >= 0);
275 33
                h2->winup_streams++;
276
277 66
                while (r2->t_window <= 0 && h2_errcheck(r2, h2) == NULL) {
278 33
                        r2->cond = &wrk->cond;
279 33
                        (void)h2_cond_wait(r2->cond, h2, r2);
280 33
                        r2->cond = NULL;
281
                }
282
283 42
                while (h2->req0->t_window <= 0 && h2_errcheck(r2, h2) == NULL)
284 9
                        (void)h2_cond_wait(h2->winupd_cond, h2, r2);
285
286 33
                if (h2_errcheck(r2, h2) == NULL) {
287 21
                        w = vmin_t(int64_t, h2_win_limit(r2, h2), wanted);
288 21
                        h2_win_charge(r2, h2, w);
289 21
                        assert (w > 0);
290 21
                }
291
292 33
                if (r2->error == H2SE_BROKE_WINDOW &&
293 6
                    h2->open_streams <= h2->winup_streams) {
294 0
                        VSLb(h2->vsl, SLT_SessError, "H2: window bankrupt");
295 0
                        h2->error = r2->error = H2CE_BANKRUPT;
296 0
                    }
297
298 33
                assert(h2->winup_streams > 0);
299 33
                h2->winup_streams--;
300
301 33
                h2_send_get_locked(wrk, h2, r2);
302 33
        }
303
304 162
        if (w == 0 && h2_errcheck(r2, h2) == NULL) {
305 129
                assert(r2->t_window > 0);
306 129
                assert(h2->req0->t_window > 0);
307 129
                w = h2_win_limit(r2, h2);
308 129
                if (w > wanted)
309 108
                        w = wanted;
310 129
                h2_win_charge(r2, h2, w);
311 129
                assert (w > 0);
312 129
        }
313 162
        r2->t_winupd = 0;
314 162
        Lck_Unlock(&h2->sess->mtx);
315 162
        return (w);
316 246
}
317
318
/*
319
 * This is the per-stream frame sender.
320
 * XXX: priority
321
 */
322
323
static void
324 597
h2_send(struct worker *wrk, struct h2_req *r2, h2_frame ftyp, uint8_t flags,
325
    uint32_t len, const void *ptr, uint64_t *counter)
326
{
327
        struct h2_sess *h2;
328
        uint32_t mfs, tf;
329
        const char *p;
330
        uint8_t final_flags;
331
332 597
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
333 597
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
334 597
        h2 = r2->h2sess;
335 597
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
336 597
        assert(len == 0 || ptr != NULL);
337 597
        AN(counter);
338
339 597
        AN(H2_SEND_HELD(h2, r2));
340
341 597
        if (h2_errcheck(r2, h2) != NULL)
342 93
                return;
343
344 504
        AN(ftyp);
345 504
        AZ(flags & ~(ftyp->flags));
346 504
        if (r2->stream == 0)
347 0
                AZ(ftyp->act_szero);
348
        else
349 504
                AZ(ftyp->act_snonzero);
350
351 504
        Lck_Lock(&h2->sess->mtx);
352 504
        mfs = h2->remote_settings.max_frame_size;
353 504
        if (r2->counted && (
354 504
            (ftyp == H2_F_HEADERS && (flags & H2FF_HEADERS_END_STREAM)) ||
355 192
            (ftyp == H2_F_DATA && (flags & H2FF_DATA_END_STREAM)) ||
356 0
            ftyp == H2_F_RST_STREAM
357
            )) {
358 723
                assert(h2->open_streams > 0);
359 285
                h2->open_streams--;
360 285
                r2->counted = 0;
361 285
        }
362 504
        Lck_Unlock(&h2->sess->mtx);
363
364 504
        if (ftyp->respect_window) {
365 192
                tf = h2_do_window(wrk, r2, h2, (len > mfs) ? mfs : len);
366 192
                if (h2_errcheck(r2, h2) != NULL)
367 0
                        return;
368 192
                AN(H2_SEND_HELD(h2, r2));
369 192
        } else
370 312
                tf = mfs;
371
372 504
        if (len <= tf) {
373 474
                H2_Send_Frame(wrk, h2, ftyp, flags, len, r2->stream, ptr);
374 474
                *counter += len;
375 474
        } else {
376 30
                AN(ptr);
377 30
                p = ptr;
378 30
                final_flags = ftyp->final_flags & flags;
379 30
                flags &= ~ftyp->final_flags;
380 30
                do {
381 102
                        AN(ftyp->continuation);
382 102
                        if (!ftyp->respect_window)
383 24
                                tf = mfs;
384 102
                        if (ftyp->respect_window && p != ptr) {
385 108
                                tf = h2_do_window(wrk, r2, h2,
386 54
                                    (len > mfs) ? mfs : len);
387 54
                                if (h2_errcheck(r2, h2) != NULL)
388 12
                                        return;
389 42
                                AN(H2_SEND_HELD(h2, r2));
390 42
                        }
391 90
                        if (tf < len) {
392 144
                                H2_Send_Frame(wrk, h2, ftyp,
393 72
                                    flags, tf, r2->stream, p);
394 72
                        } else {
395 18
                                if (ftyp->respect_window)
396 12
                                        assert(tf == len);
397 18
                                tf = len;
398 36
                                H2_Send_Frame(wrk, h2, ftyp, final_flags, tf,
399 18
                                    r2->stream, p);
400 18
                                flags = 0;
401
                        }
402 90
                        p += tf;
403 90
                        len -= tf;
404 90
                        *counter += tf;
405 90
                        ftyp = ftyp->continuation;
406 90
                        flags &= ftyp->flags;
407 90
                        final_flags &= ftyp->flags;
408 90
                } while (h2->error == NULL && len > 0);
409
        }
410 597
}
411
412
void
413 172
H2_Send_RST(struct worker *wrk, struct h2_sess *h2, const struct h2_req *r2,
414
    uint32_t stream, h2_error h2e)
415
{
416
        char b[4];
417
418 172
        CHECK_OBJ_NOTNULL(h2, H2_SESS_MAGIC);
419 172
        CHECK_OBJ_NOTNULL(r2, H2_REQ_MAGIC);
420 172
        AN(H2_SEND_HELD(h2, r2));
421 172
        AN(h2e);
422
423 172
        H2S_Lock_VSLb(h2, SLT_Debug, "H2: stream %u: %s", stream, h2e->txt);
424 172
        vbe32enc(b, h2e->val);
425
426 172
        H2_Send_Frame(wrk, h2, H2_F_RST_STREAM, 0, sizeof b, stream, b);
427 172
}
428
429
void
430 597
H2_Send(struct worker *wrk, struct h2_req *r2, h2_frame ftyp, uint8_t flags,
431
    uint32_t len, const void *ptr, uint64_t *counter)
432
{
433 597
        uint64_t dummy_counter = 0;
434
        h2_error h2e;
435
436 597
        if (counter == NULL)
437 162
                counter = &dummy_counter;
438
439 597
        h2_send(wrk, r2, ftyp, flags, len, ptr, counter);
440
441 597
        h2e = h2_errcheck(r2, r2->h2sess);
442 597
        if (H2_ERROR_MATCH(h2e, H2SE_CANCEL))
443 15
                H2_Send_RST(wrk, r2->h2sess, r2, r2->stream, h2e);
444 597
}