varnish-cache/bin/varnishtest/vtc_sess.c
0
/*-
1
 * Copyright (c) 2020 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/types.h>
34
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include "vtc.h"
39
#include "vtc_http.h"
40
41
struct thread_arg {
42
        unsigned                magic;
43
#define THREAD_ARG_MAGIC        0xd5dc5f1c
44
        void                    *priv;
45
        sess_conn_f             *conn_f;
46
        sess_disc_f             *disc_f;
47
        const char              *listen_addr;
48
        struct vtc_sess         *vsp;
49
        int                     *asocket;
50
        const char              *spec;
51
};
52
53
struct vtc_sess *
54 52938
Sess_New(struct vtclog *vl, const char *name)
55
{
56
        struct vtc_sess *vsp;
57
58 52938
        ALLOC_OBJ(vsp, VTC_SESS_MAGIC);
59 52938
        AN(vsp);
60 52938
        vsp->vl = vl;
61 52938
        REPLACE(vsp->name, name);
62 52938
        vsp->repeat = 1;
63 52938
        return (vsp);
64
}
65
66
void
67 52938
Sess_Destroy(struct vtc_sess **vspp)
68
{
69
        struct vtc_sess *vsp;
70
71 52938
        TAKE_OBJ_NOTNULL(vsp, vspp, VTC_SESS_MAGIC);
72 52938
        REPLACE(vsp->name, NULL);
73 52938
        FREE_OBJ(vsp);
74 52938
}
75
76
int
77 143175
Sess_GetOpt(struct vtc_sess *vsp, char * const **avp)
78
{
79
        char * const *av;
80 143175
        int rv = 0;
81
82 143175
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
83 143175
        AN(avp);
84 143175
        av = *avp;
85 143175
        AN(*av);
86 143175
        if (!strcmp(*av, "-rcvbuf")) {
87 225
                AN(av[1]);
88 225
                vsp->rcvbuf = atoi(av[1]);
89 225
                av += 1;
90 225
                rv = 1;
91 143175
        } else if (!strcmp(*av, "-repeat")) {
92 2025
                AN(av[1]);
93 2025
                vsp->repeat = atoi(av[1]);
94 2025
                av += 1;
95 2025
                rv = 1;
96 142950
        } else if (!strcmp(*av, "-keepalive")) {
97 325
                vsp->keepalive = 1;
98 325
                rv = 1;
99 325
        }
100 143175
        *avp = av;
101 143175
        return (rv);
102
}
103
104
int
105 92943
sess_process(struct vtclog *vl, struct vtc_sess *vsp,
106
    const char *spec, int sock, int *sfd, const char *addr)
107
{
108
        int rv;
109
110 92943
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
111
112 92943
        rv = http_process(vl, vsp, spec, sock, sfd, addr, vsp->rcvbuf);
113 92943
        return (rv);
114
}
115
116
static void *
117 65195
sess_thread(void *priv)
118
{
119
        struct vtclog *vl;
120
        struct vtc_sess *vsp;
121
        struct thread_arg ta, *tap;
122 65195
        int i, fd = -1;
123
124 65195
        CAST_OBJ_NOTNULL(tap, priv, THREAD_ARG_MAGIC);
125 65195
        ta = *tap;
126 65195
        FREE_OBJ(tap);
127
128 65195
        vsp = ta.vsp;
129 65195
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
130 65195
        vl = vtc_logopen("%s", vsp->name);
131 65195
        pthread_cleanup_push(vtc_logclose, vl);
132
133 65195
        assert(vsp->repeat > 0);
134 130390
        vtc_log(vl, 2, "Started on %s (%u iterations%s)", ta.listen_addr,
135 65195
                vsp->repeat, vsp->keepalive ? " using keepalive" : "");
136 155955
        for (i = 0; i < vsp->repeat; i++) {
137 90760
                if (fd < 0)
138 87914
                        fd = ta.conn_f(ta.priv, vl);
139 181520
                fd = sess_process(vl, ta.vsp, ta.spec, fd,
140 90760
                    ta.asocket, ta.listen_addr);
141 90760
                if (! vsp->keepalive)
142 84510
                        ta.disc_f(ta.priv, vl, &fd);
143 90760
        }
144 65195
        if (vsp->keepalive)
145 400
                ta.disc_f(ta.priv, vl, &fd);
146 65195
        vtc_log(vl, 2, "Ending");
147 65195
        pthread_cleanup_pop(0);
148 65195
        vtc_logclose(vl);
149 65195
        return (NULL);
150
}
151
152
pthread_t
153 68200
Sess_Start_Thread(
154
    void *priv,
155
    struct vtc_sess *vsp,
156
    sess_conn_f *conn,
157
    sess_disc_f *disc,
158
    const char *listen_addr,
159
    int *asocket,
160
    const char *spec
161
)
162
{
163
        struct thread_arg *ta;
164
        pthread_t pt;
165
166 68200
        AN(priv);
167 68200
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
168 68200
        AN(conn);
169 68200
        AN(disc);
170 68200
        AN(listen_addr);
171 68200
        ALLOC_OBJ(ta, THREAD_ARG_MAGIC);
172 68200
        AN(ta);
173 68200
        ta->priv = priv;
174 68200
        ta->vsp = vsp;
175
176 68200
        ta->conn_f = conn;
177 68200
        ta->disc_f = disc;
178 68200
        ta->listen_addr = listen_addr;
179 68200
        ta->asocket = asocket;
180 68200
        ta->spec = spec;
181 68200
        PTOK(pthread_create(&pt, NULL, sess_thread, ta));
182 68200
        return (pt);
183
}