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 85982
Sess_New(struct vtclog *vl, const char *name)
55
{
56
        struct vtc_sess *vsp;
57
58 85982
        ALLOC_OBJ(vsp, VTC_SESS_MAGIC);
59 85982
        AN(vsp);
60 85982
        vsp->vl = vl;
61 85982
        REPLACE(vsp->name, name);
62 85982
        vsp->repeat = 1;
63 85982
        return (vsp);
64
}
65
66
void
67 85982
Sess_Destroy(struct vtc_sess **vspp)
68
{
69
        struct vtc_sess *vsp;
70
71 85982
        TAKE_OBJ_NOTNULL(vsp, vspp, VTC_SESS_MAGIC);
72 85982
        REPLACE(vsp->name, NULL);
73 85982
        FREE_OBJ(vsp);
74 85982
}
75
76
int
77 232280
Sess_GetOpt(struct vtc_sess *vsp, char * const **avp)
78
{
79
        char * const *av;
80 232280
        int rv = 0;
81
82 232280
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
83 232280
        AN(avp);
84 232280
        av = *avp;
85 232280
        AN(*av);
86 232280
        if (!strcmp(*av, "-rcvbuf")) {
87 360
                AN(av[1]);
88 360
                vsp->rcvbuf = atoi(av[1]);
89 360
                av += 1;
90 360
                rv = 1;
91 232280
        } else if (!strcmp(*av, "-repeat")) {
92 3240
                AN(av[1]);
93 3240
                vsp->repeat = atoi(av[1]);
94 3240
                av += 1;
95 3240
                rv = 1;
96 231920
        } else if (!strcmp(*av, "-keepalive")) {
97 520
                vsp->keepalive = 1;
98 520
                rv = 1;
99 520
        }
100 232280
        *avp = av;
101 232280
        return (rv);
102
}
103
104
int
105 150122
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 150122
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
111
112 150122
        rv = http_process(vl, vsp, spec, sock, sfd, addr, vsp->rcvbuf);
113 150122
        return (rv);
114
}
115
116
static void *
117 110600
sess_thread(void *priv)
118
{
119
        struct vtclog *vl;
120
        struct vtc_sess *vsp;
121
        struct thread_arg ta, *tap;
122 110600
        int i, fd = -1;
123
124 110600
        CAST_OBJ_NOTNULL(tap, priv, THREAD_ARG_MAGIC);
125 110600
        ta = *tap;
126 110600
        FREE_OBJ(tap);
127
128 110600
        vsp = ta.vsp;
129 110600
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
130 110600
        vl = vtc_logopen("%s", vsp->name);
131 110600
        pthread_cleanup_push(vtc_logclose, vl);
132
133 110600
        assert(vsp->repeat > 0);
134 221200
        vtc_log(vl, 2, "Started on %s (%u iterations%s)", ta.listen_addr,
135 110600
                vsp->repeat, vsp->keepalive ? " using keepalive" : "");
136 257157
        for (i = 0; i < vsp->repeat; i++) {
137 146557
                if (fd < 0)
138 142128
                        fd = ta.conn_f(ta.priv, vl);
139 293114
                fd = sess_process(vl, ta.vsp, ta.spec, fd,
140 146557
                    ta.asocket, ta.listen_addr);
141 146557
                if (! vsp->keepalive)
142 136557
                        ta.disc_f(ta.priv, vl, &fd);
143 146557
        }
144 110600
        if (vsp->keepalive)
145 640
                ta.disc_f(ta.priv, vl, &fd);
146 110600
        vtc_log(vl, 2, "Ending");
147 110600
        pthread_cleanup_pop(0);
148 110600
        vtc_logclose(vl);
149 110600
        return (NULL);
150
}
151
152
pthread_t
153 110600
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 110600
        AN(priv);
167 110600
        CHECK_OBJ_NOTNULL(vsp, VTC_SESS_MAGIC);
168 110600
        AN(conn);
169 110600
        AN(disc);
170 110600
        AN(listen_addr);
171 110600
        ALLOC_OBJ(ta, THREAD_ARG_MAGIC);
172 110600
        AN(ta);
173 110600
        ta->priv = priv;
174 110600
        ta->vsp = vsp;
175
176 110600
        ta->conn_f = conn;
177 110600
        ta->disc_f = disc;
178 110600
        ta->listen_addr = listen_addr;
179 110600
        ta->asocket = asocket;
180 110600
        ta->spec = spec;
181 110600
        PTOK(pthread_create(&pt, NULL, sess_thread, ta));
182 110600
        return (pt);
183
}