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