forked from rbruenig/qperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
executable file
·246 lines (203 loc) · 7.32 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "client.h"
#include "client_stream.h"
#include "common.h"
#include <ev.h>
#include <stdio.h>
#include <quicly.h>
#include <quicly/defaults.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <float.h>
#include <stdbool.h>
#include <quicly/streambuf.h>
#include <picotls/../../t/util.h>
static int client_socket = -1;
static quicly_conn_t *conn = NULL;
static ev_timer client_timeout;
static quicly_context_t client_ctx;
static quicly_cid_plaintext_t next_cid;
static int64_t start_time = 0;
static int64_t connect_time = 0;
static bool quit_after_first_byte = false;
static ptls_iovec_t resumption_token;
void client_timeout_cb(EV_P_ ev_timer *w, int revents);
void client_refresh_timeout()
{
int64_t timeout = clamp_int64(quicly_get_first_timeout(conn) - client_ctx.now->cb(client_ctx.now),
1, 200);
client_timeout.repeat = timeout / 1000.;
ev_timer_again(EV_DEFAULT, &client_timeout);
}
void client_timeout_cb(EV_P_ ev_timer *w, int revents)
{
if(!send_pending(&client_ctx, client_socket, conn)) {
quicly_free(conn);
exit(0);
}
client_refresh_timeout();
}
void client_read_cb(EV_P_ ev_io *w, int revents)
{
// retrieve data
uint8_t buf[4096];
struct sockaddr sa;
socklen_t salen = sizeof(sa);
quicly_decoded_packet_t packet;
ssize_t bytes_received;
while((bytes_received = recvfrom(w->fd, buf, sizeof(buf), MSG_DONTWAIT, &sa, &salen)) != -1) {
for(ssize_t offset = 0; offset < bytes_received; ) {
size_t packet_len = quicly_decode_packet(&client_ctx, &packet, buf, bytes_received, &offset);
if(packet_len == SIZE_MAX) {
break;
}
// handle packet --------------------------------------------------
int ret = quicly_receive(conn, NULL, &sa, &packet);
if(ret != 0 && ret != QUICLY_ERROR_PACKET_IGNORED) {
fprintf(stderr, "quicly_receive returned %i\n", ret);
exit(1);
}
// check if connection ready --------------------------------------
if(connect_time == 0 && quicly_connection_is_ready(conn)) {
connect_time = client_ctx.now->cb(client_ctx.now);
int64_t establish_time = connect_time - start_time;
printf("connection establishment time: %lums\n", establish_time);
}
}
}
if(errno != EWOULDBLOCK && errno != 0) {
perror("recvfrom failed");
}
if(!send_pending(&client_ctx, client_socket, conn)) {
quicly_free(conn);
exit(0);
}
client_refresh_timeout();
}
void enqueue_request(quicly_conn_t *conn)
{
quicly_stream_t *stream;
int ret = quicly_open_stream(conn, &stream, 0);
assert(ret == 0);
const char *req = "qperf start sending";
quicly_streambuf_egress_write(stream, req, strlen(req));
quicly_streambuf_egress_shutdown(stream);
}
static void client_on_conn_close(quicly_closed_by_remote_t *self, quicly_conn_t *conn, int err,
uint64_t frame_type, const char *reason, size_t reason_len)
{
if (QUICLY_ERROR_IS_QUIC_TRANSPORT(err)) {
fprintf(stderr, "transport close:code=0x%" PRIx16 ";frame=%" PRIu64 ";reason=%.*s\n", QUICLY_ERROR_GET_ERROR_CODE(err),
frame_type, (int)reason_len, reason);
} else if (QUICLY_ERROR_IS_QUIC_APPLICATION(err)) {
fprintf(stderr, "application close:code=0x%" PRIx16 ";reason=%.*s\n", QUICLY_ERROR_GET_ERROR_CODE(err), (int)reason_len,
reason);
} else if (err == QUICLY_ERROR_RECEIVED_STATELESS_RESET) {
fprintf(stderr, "stateless reset\n");
} else {
fprintf(stderr, "unexpected close:code=%d\n", err);
}
}
static quicly_stream_open_t stream_open = {&client_on_stream_open};
static quicly_closed_by_remote_t closed_by_remote = {&client_on_conn_close};
int run_client(const char *port, bool gso, const char *logfile, const char *cc, int iw, quicly_ss_type_t *ss, int mw, const char *cmdg, const char *host, int runtime_s, uint64_t max_bytes, bool ttfb_only)
{
setup_session_cache(get_tlsctx());
quicly_amend_ptls_context(get_tlsctx());
client_ctx = quicly_spec_context;
client_ctx.tls = get_tlsctx();
client_ctx.stream_open = &stream_open;
client_ctx.closed_by_remote = &closed_by_remote;
client_ctx.transport_params.max_stream_data.uni = UINT32_MAX;
client_ctx.transport_params.max_stream_data.bidi_local = UINT32_MAX;
client_ctx.transport_params.max_stream_data.bidi_remote = UINT32_MAX;
client_ctx.transport_params.max_data = (mw * 1024 * 1024);
client_ctx.initcwnd_packets = iw;
client_ctx.cc_slowstart = ss;
if(strcmp(cc, "reno") == 0) {
client_ctx.init_cc = &quicly_cc_reno_init;
} else if(strcmp(cc, "cubic") == 0) {
client_ctx.init_cc = &quicly_cc_cubic_init;
}
if(strcmp(cmdg, "none") == 0) {
client_ctx.growth_mode = QUICLY_MAX_DATA_GROWTH_NONE;
printf("cmdg mode is none\n");
} else if(strcmp(cmdg, "hybla") == 0) {
client_ctx.growth_mode = QUICLY_MAX_DATA_GROWTH_HYBLA;
printf("cmdg mode is hybla\n");
}
if (gso) {
enable_gso();
}
struct ev_loop *loop = EV_DEFAULT;
struct sockaddr_storage sas;
socklen_t salen;
if(resolve_address((void*)&sas, &salen, host, port, AF_INET, SOCK_DGRAM, IPPROTO_UDP) != 0) {
exit(-1);
}
struct sockaddr *sa = (void*)&sas;
client_socket = socket(sa->sa_family, SOCK_DGRAM, IPPROTO_UDP);
if(client_socket == -1) {
perror("socket(2) failed");
return 1;
}
struct sockaddr_in local = {0};
local.sin_family = AF_INET;
if (bind(client_socket, (void *)&local, sizeof(local)) != 0) {
perror("bind(2) failed");
return 1;
}
if (logfile)
{
setup_log_event(client_ctx.tls, logfile);
}
printf("starting client with host %s, port %s, runtime %is, cc %s, iw %i\n", host, port, runtime_s, cc, iw);
quit_after_first_byte = ttfb_only;
// start time
start_time = client_ctx.now->cb(client_ctx.now);
int ret = quicly_connect(&conn, &client_ctx, host, sa, NULL, &next_cid, resumption_token, 0, 0, NULL);
assert(ret == 0);
++next_cid.master_id;
enqueue_request(conn);
if(!send_pending(&client_ctx, client_socket, conn)) {
printf("failed to connect: send_pending failed\n");
exit(1);
}
if(conn == NULL) {
fprintf(stderr, "connection == NULL\n");
exit(1);
}
ev_io socket_watcher;
ev_io_init(&socket_watcher, &client_read_cb, client_socket, EV_READ);
ev_io_start(loop, &socket_watcher);
ev_init(&client_timeout, &client_timeout_cb);
client_refresh_timeout();
if (max_bytes == 0) {
client_set_quit_after(runtime_s);
}
else {
client_set_quit_after_bytes(max_bytes);
}
ev_run(loop, 0);
return 0;
}
void quit_client()
{
if(conn == NULL) {
return;
}
quicly_close(conn, 0, "");
if(!send_pending(&client_ctx, client_socket, conn)) {
printf("send_pending failed during connection close");
quicly_free(conn);
exit(0);
}
client_refresh_timeout();
}
void on_first_byte()
{
printf("time to first byte: %lums\n", client_ctx.now->cb(client_ctx.now) - start_time);
if(quit_after_first_byte) {
quit_client();
}
}