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