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