-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
260 lines (215 loc) · 6.45 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <liburing.h>
#include <netdb.h>
#include <argp.h>
#define QUEUE_DEPTH 256
#define READ_SIZE 1024
enum event_type {
ACCEPT,
READ,
WRITE,
CLOSE
};
struct request {
enum event_type type;
int client_socket;
int iovec_count;
struct iovec iov[];
};
struct io_uring ring;
#define DEFAULT_HOST "127.0.0.1";
#define DEFAULT_PORT "3333";
int debug = 0;
int num_ops = 10;
int concurrent_ops = 100;
void signal_handler(int num) {
printf("Exiting\n");
io_uring_queue_exit(&ring);
exit(0);
}
void fatal_error(const char *syscall) {
perror(syscall);
exit(1);
}
void print_addr(struct addrinfo *addr) {
char num_name[255];
char num_serv[255];
int status = getnameinfo(addr->ai_addr, addr->ai_addrlen, num_name, 255, num_serv, 255, NI_NUMERICHOST | NI_NUMERICSERV);
if (status != 0) {
fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
fprintf(stderr, "Trying %s:%s\n", num_name, num_serv);
}
int client_connect(const char* host, const char* service) {
int sock;
struct addrinfo hints;
struct addrinfo *result, *rp;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* TCP socket */
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */
int status = getaddrinfo(host, service, &hints, &result);
if (status != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
exit(EXIT_FAILURE);
}
/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */
for (rp = result; rp != NULL; rp = rp->ai_next) {
if (debug) print_addr(rp);
sock = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sock == -1)
continue;
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */
close(sock);
}
freeaddrinfo(result); /* No longer needed */
if (rp == NULL) { /* No address succeeded */
fprintf(stderr, "Could not connect\n");
exit(EXIT_FAILURE);
}
return sock;
}
int add_read_request(int socket) {
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
struct request *req = malloc(sizeof(*req) + sizeof(struct iovec));
req->iov[0].iov_base = malloc(READ_SIZE);
req->iov[0].iov_len = READ_SIZE;
req->type = READ;
req->client_socket = socket;
memset(req->iov[0].iov_base, 0, READ_SIZE);
io_uring_prep_readv(sqe, socket, &req->iov[0], 1, 0);
io_uring_sqe_set_data(sqe, req);
return 0;
}
int add_write_request(int socket) {
struct request *req = malloc(sizeof(*req) + sizeof(struct iovec));
req->iov[0].iov_base = malloc(READ_SIZE);
req->iov[0].iov_len = READ_SIZE;
req->client_socket = socket;
memset(req->iov[0].iov_base, 0, READ_SIZE);
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
req->type = WRITE;
io_uring_prep_writev(sqe, req->client_socket, req->iov, 1, 0);
io_uring_sqe_set_data(sqe, req);
return 0;
}
void loop(char *host, char *port) {
struct io_uring_cqe *cqe;
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int in_flight = 0;
int remaining = num_ops;
int write_count = 0;
int read_count = 0;
while (read_count < num_ops) {
unsigned space = io_uring_sq_space_left(&ring);
int queued = 0;
while (space > 0 && remaining > 0 && in_flight < concurrent_ops) {
int sock = client_connect(host, port);
add_write_request(sock);
space--;
remaining--;
in_flight++;
queued++;
}
if (queued > 0) {
io_uring_submit(&ring);
}
int ret = io_uring_wait_cqe(&ring, &cqe);
if (ret < 0) {
fatal_error("io_uring_wait_cqe");
}
struct request *req = (struct request*) cqe->user_data;
if (cqe->res < 0) {
fprintf(stderr, "Async request failed: %s, for event: %d on socket %d\n",
strerror(-cqe->res), req->type, req->client_socket);
exit(1);
}
switch (req->type) {
case ACCEPT:
//fprintf(stderr, "Client should never reach accept\n");
exit(1);
break;
case READ:
read_count++;
if (debug) fprintf(stderr, "READ %d\n", cqe->res);
close(req->client_socket);
free(req->iov[0].iov_base);
free(req);
in_flight--;
break;
case WRITE:
write_count++;
if (debug) fprintf(stderr, "WRITE %d\n", cqe->res);
add_read_request(req->client_socket);
io_uring_submit(&ring);
free(req->iov[0].iov_base);
free(req);
break;
}
io_uring_cqe_seen(&ring, cqe);
}
}
const char argp_program_doc[] =
"Echo client\n"
"\n"
"Usage: ./client\n";
static const struct argp_option opts[] = {
{"host", 'h', "address", 0, "Address of host"},
{"port", 'p', "number", 0, "Port number of echo service"},
{"times", 'n', "number", 0, "Repeat number of times"},
{"concurrent", 'c', "number", 0, "Number of concurrent requests"},
{"debug", 'd', 0, 0, "Provide debug output"},
{},
};
char *host = DEFAULT_HOST;
char *port = DEFAULT_PORT;
error_t parse_opts (int key, char *arg, struct argp_state *state)
{
switch (key) {
case 'h':
host = arg;
break;
case 'p':
port = arg;
break;
case 'n':
num_ops = atoi(arg);
break;
case 'c':
concurrent_ops = atoi(arg);
break;
case 'd':
debug = 1;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const struct argp argp = {
.options = opts,
.parser = parse_opts,
.doc = argp_program_doc,
};
int main(int argc, char *argv[]) {
int err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
if (err)
return 0;
signal(SIGINT, signal_handler);
io_uring_queue_init(QUEUE_DEPTH, &ring, 0);
// Client
loop(host, port);
return 0;
}