forked from rbruenig/qperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
157 lines (134 loc) · 4.3 KB
/
common.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
#include "common.h"
#include <sys/socket.h>
#include <netinet/udp.h>
#include <netdb.h>
#include <memory.h>
#include <picotls/openssl.h>
#include <errno.h>
ptls_context_t *get_tlsctx()
{
static ptls_context_t tlsctx = {.random_bytes = ptls_openssl_random_bytes,
.get_time = &ptls_get_time,
.key_exchanges = ptls_openssl_key_exchanges,
.cipher_suites = ptls_openssl_cipher_suites,
.require_dhe_on_psk = 1};
return &tlsctx;
}
struct addrinfo *get_address(const char *host, const char *port)
{
struct addrinfo hints;
struct addrinfo *result;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV | AI_PASSIVE;
hints.ai_protocol = IPPROTO_UDP;
if(getaddrinfo(host, port, &hints, &result) != 0) {
return NULL;
} else {
return result;
}
}
bool send_dgrams_default(int fd, struct sockaddr *dest, struct iovec *dgrams, size_t num_dgrams)
{
for(size_t i = 0; i < num_dgrams; ++i) {
struct msghdr mess = {
.msg_name = dest,
.msg_namelen = quicly_get_socklen(dest),
.msg_iov = &dgrams[i], .msg_iovlen = 1
};
ssize_t bytes_sent;
while ((bytes_sent = sendmsg(fd, &mess, 0)) == -1 && errno == EINTR);
if (bytes_sent == -1) {
perror("sendmsg failed");
return false;
}
}
return true;
}
#ifdef __linux__
/* UDP GSO is only supported on linux */
#ifndef UDP_SEGMENT
#define UDP_SEGMENT 103 /* Set GSO segmentation size */
#endif
bool send_dgrams_gso(int fd, struct sockaddr *dest, struct iovec *dgrams, size_t num_dgrams)
{
struct iovec vec = {
.iov_base = (void *)dgrams[0].iov_base,
.iov_len = dgrams[num_dgrams - 1].iov_base + dgrams[num_dgrams - 1].iov_len - dgrams[0].iov_base
};
struct msghdr mess = {
.msg_name = dest,
.msg_namelen = quicly_get_socklen(dest),
.msg_iov = &vec,
.msg_iovlen = 1
};
union {
struct cmsghdr hdr;
char buf[CMSG_SPACE(sizeof(uint16_t))];
} cmsg;
if (num_dgrams != 1) {
cmsg.hdr.cmsg_level = SOL_UDP;
cmsg.hdr.cmsg_type = UDP_SEGMENT;
cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(uint16_t));
*(uint16_t *)CMSG_DATA(&cmsg.hdr) = dgrams[0].iov_len;
mess.msg_control = &cmsg;
mess.msg_controllen = (socklen_t)CMSG_SPACE(sizeof(uint16_t));
}
ssize_t bytes_sent;
while ((bytes_sent = sendmsg(fd, &mess, 0)) == -1 && errno == EINTR);
if (bytes_sent == -1) {
perror("sendmsg failed");
return false;
}
return true;
}
#endif
bool (*send_dgrams)(int fd, struct sockaddr *dest, struct iovec *dgrams, size_t num_dgrams) = send_dgrams_default;
void enable_gso()
{
send_dgrams = send_dgrams_gso;
}
bool send_pending(quicly_context_t *ctx, int fd, quicly_conn_t *conn)
{
#define SEND_BATCH_SIZE 16
quicly_address_t dest, src;
struct iovec dgrams[SEND_BATCH_SIZE];
uint8_t dgrams_buf[SEND_BATCH_SIZE * ctx->transport_params.max_udp_payload_size];
size_t num_dgrams = SEND_BATCH_SIZE;
while(true) {
int quicly_res = quicly_send(conn, &dest, &src, dgrams, &num_dgrams, &dgrams_buf, sizeof(dgrams_buf));
if(quicly_res != 0) {
if(quicly_res != QUICLY_ERROR_FREE_CONNECTION) {
printf("quicly_send failed with code %i\n", quicly_res);
} else {
printf("connection closed\n");
}
return false;
} else if(num_dgrams == 0) {
return true;
}
if (!send_dgrams(fd, &dest.sa, dgrams, num_dgrams)) {
return false;
}
};
}
void print_escaped(const char *src, size_t len)
{
for(size_t i = 0; i < len; ++i) {
switch (src[i]) {
case '\n':
putchar('\\');
putchar('n');
break;
case '\r':
putchar('\\');
putchar('r');
break;
default:
putchar(src[i]);
}
}
putchar('\n');
fflush(stdout);
}