-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperf.quiche.h
220 lines (163 loc) · 5.45 KB
/
perf.quiche.h
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
#include "quiche.h"
#pragma once
#define LOCAL_CONN_ID_LEN 16
#define MAX_TOKEN_LEN \
sizeof("quiche") - 1 + \
sizeof(struct sockaddr_storage) + \
LOCAL_CONN_ID_LEN
template <Mode mode>
class Quiche : public QuicLibrary<mode> {
private:
using QuicLibrary<mode>::networkHub;
int64_t bytesInFlight = -1;
int64_t bytesSent = -1;
int64_t lingeringBytes = -1;
quiche_config *config;
quiche_conn *conn;
struct sockaddr_in6 *peerAddress;
bool connected;
uint64_t flushPackets(void)
{
if (unlikely(conn == NULL)) return 0;
MultiUDPContext *packets = networkHub->sendPool.get();
UDPContext *packet;
do
{
packet = &packets->msgs[packets->count];
ssize_t written = quiche_conn_send(conn, packet->buffer(), MAX_IPV6_UDP_PACKET_SIZE);
if (written == QUICHE_ERR_DONE) break;
packet->setLength(written);
packet->copyInAddress((struct sockaddr *)peerAddress);
packets->count++;
if (packets->isFull())
{
networkHub->sendBatch(packets);
// printf("networkHub->sendPool.howManyLeft() = %lu\n", networkHub->sendPool.howManyLeft());
//packets = networkHub->sendPool.get();
//if (unlikely(packets == NULL)) return 10000;
}
} while (true);
if (packets->count > 0) networkHub->sendBatch(packets);
skip:
return quiche_conn_timeout_as_nanos(conn) / 1'000;
}
void advance(int32_t count = 0)
{
do
{
uint64_t usTil = flushPackets();
bool timedout = networkHub->recvmsgWithTimeout(usTil, [&] (UDPContext *msg) -> void {
if constexpr (mode & Mode::server)
{
if (conn == NULL)
{
uint8_t serverscid[8];
RAND_bytes(serverscid, sizeof(serverscid));
conn = quiche_conn_new_with_tls(serverscid, sizeof(serverscid), NULL, 0, config, SSL_new(TLS::getTLSCtx()), true);
peerAddress = (struct sockaddr_in6 *)malloc(sizeof(struct sockaddr_in6));
memcpy(peerAddress, msg->address(), sizeof(struct sockaddr_in6));
}
}
ssize_t result = quiche_conn_recv(conn, msg->buffer(), msg->msg_len);
//printf("quiche_conn_recv result = %lld\n", result);
if (quiche_conn_is_established(conn))
{
//printf("quiche_conn_stream_capacity = %lld\n", quiche_conn_stream_capacity(conn, 0));
if constexpr (mode & Mode::client)
{
connected = true;
}
uint64_t streamID = 0;
quiche_stream_iter *readable = quiche_conn_readable(conn);
while (quiche_stream_iter_next(readable, &streamID))
{
static uint8_t buf[65535];
bool fin = false;
ssize_t recv_len = quiche_conn_stream_recv(conn, streamID, buf, sizeof(buf), &fin);
if constexpr (mode & Mode::client)
{
// throw bytes away
bytesInFlight -= recv_len;
//printf("received %.1f%%\n", 100.0 * (double)(_1GB - bytesInFlight)/(double)_1GB);
}
else
{
// receive the bytes in flight
bytesInFlight = bswap_64(*(uint64_t *)buf);
}
}
quiche_stream_iter_free(readable);
}
});
if (timedout) quiche_conn_on_timeout(conn);
if constexpr (mode & Mode::server)
{
if (conn == NULL) continue;
if (quiche_conn_stream_capacity(conn, 0) > 0)
{
do
{
ssize_t sent = quiche_conn_stream_send(conn, 0, (const uint8_t *)networkHub->junk, bytesInFlight > sizeof(networkHub->junk) ? sizeof(networkHub->junk) : bytesInFlight, false);
if (sent > 0) bytesInFlight -= sent;
else break;
} while (true);
if (bytesInFlight == 0) flushPackets();
}
}
} while (bytesInFlight != 0 && (count == 0 || --count > 0));
}
public:
// static void log(const char *line, void *argp)
// {
// printf("%s\n", line);
// }
void instanceSetup(uint16_t localPort, int argc, char *argv[])
{
networkHub = new NetworkHub<mode>(localPort);
config = quiche_config_new(QUICHE_PROTOCOL_VERSION);
quiche_config_set_max_idle_timeout(config, 5000);
quiche_config_set_max_recv_udp_payload_size(config, MAX_IPV6_UDP_PACKET_SIZE);
quiche_config_set_max_send_udp_payload_size(config, MAX_IPV6_UDP_PACKET_SIZE);
quiche_config_set_initial_max_data(config, 10000000);
quiche_config_set_initial_max_stream_data_bidi_local(config, 1000000);
quiche_config_set_initial_max_stream_data_bidi_remote(config, 1000000);
quiche_config_set_initial_max_stream_data_uni(config, 10000);
quiche_config_set_initial_max_streams_bidi(config, 10);
quiche_config_set_initial_max_streams_uni(config, 10);
// quiche_config_set_ack_delay_exponent(config, 5);
// quiche_config_set_max_ack_delay(config, 50);
// quiche_config_set_disable_active_migration(config, true);
//quiche_config_set_cc_algorithm(config, QUICHE_CC_RENO);
//quiche_config_enable_hystart(config, true);
//quiche_enable_debug_logging(log, NULL);
}
void connectToServer(struct sockaddr *address)
{
peerAddress = (struct sockaddr_in6 *)address;
uint8_t scid[8];
RAND_bytes(scid, sizeof(scid));
conn = quiche_conn_new_with_tls((const uint8_t *)scid, sizeof(scid), NULL, 0, config, SSL_new(TLS::getTLSCtx()), false);
do
{
advance(1);
} while (connected == false);
}
void openStream(void)
{
// just nop this for now
// do
// {
// advance(1);
// } while (ready == false);
}
void startPerfTest(uint64_t nBytes)
{
if constexpr (mode & Mode::client)
{
bytesInFlight = nBytes;
uint64_t swappedBytes = bswap_64(bytesInFlight);
quiche_conn_stream_send(conn, 0, (const uint8_t *)&swappedBytes, 8, false);
}
advance();
}
};