]> git.meshlink.io Git - utcp/blob - test.c
Add a script to generate graphs of the data recorded by the benchmark script.
[utcp] / test.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <time.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <netdb.h>
12 #include <poll.h>
13
14
15 #include "utcp.h"
16
17 #define DIR_READ 1
18 #define DIR_WRITE 2
19
20 struct utcp_connection *c;
21 int dir = DIR_READ | DIR_WRITE;
22 bool running = true;
23 long inpktno;
24 long outpktno;
25 long dropfrom;
26 long dropto;
27 double reorder;
28 long reorder_dist = 10;
29 double dropin;
30 double dropout;
31 long total_out;
32 long total_in;
33 FILE *reference;
34 long mtu = 0;
35
36 char *reorder_data;
37 size_t reorder_len;
38 int reorder_countdown;
39
40 #if UTCP_DEBUG
41 static void debug(const char *format, ...) {
42         struct timespec tv;
43         char buf[1024];
44         int len;
45
46         clock_gettime(CLOCK_REALTIME, &tv);
47         len = snprintf(buf, sizeof(buf), "%ld.%06lu ", (long)tv.tv_sec, tv.tv_nsec / 1000);
48         va_list ap;
49         va_start(ap, format);
50         len += vsnprintf(buf + len, sizeof(buf) - len, format, ap);
51         va_end(ap);
52
53         if(len > 0 && (size_t)len < sizeof(buf)) {
54                 fwrite(buf, len, 1, stderr);
55         }
56 }
57 #else
58 #define debug(...) do {} while(0)
59 #endif
60
61 ssize_t do_recv(struct utcp_connection *c, const void *data, size_t len) {
62         (void)c;
63
64         if(!data || !len) {
65                 if(errno) {
66                         debug("Error: %s\n", strerror(errno));
67                         dir = 0;
68                 } else {
69                         dir &= ~DIR_WRITE;
70                         debug("Connection closed by peer\n");
71                 }
72
73                 return -1;
74         }
75
76         if(reference) {
77                 char buf[len];
78
79                 if(fread(buf, len, 1, reference) != 1) {
80                         debug("Error reading reference\n");
81                         abort();
82                 }
83
84                 if(memcmp(buf, data, len)) {
85                         debug("Received data differs from reference\n");
86                         abort();
87                 }
88         }
89
90         return write(1, data, len);
91 }
92
93 void do_accept(struct utcp_connection *nc, uint16_t port) {
94         (void)port;
95         utcp_accept(nc, do_recv, NULL);
96         c = nc;
97         utcp_set_accept_cb(c->utcp, NULL, NULL);
98 }
99
100 ssize_t do_send(struct utcp *utcp, const void *data, size_t len) {
101         int s = *(int *)utcp->priv;
102         outpktno++;
103
104         if(outpktno >= dropfrom && outpktno < dropto) {
105                 if(drand48() < dropout) {
106                         debug("Dropped outgoing packet\n");
107                         return len;
108                 }
109
110                 if(!reorder_data && drand48() < reorder) {
111                         reorder_data = malloc(len);
112
113                         if(!reorder_data) {
114                                 debug("Out of memory\n");
115                                 return len;
116                         }
117
118                         reorder_len = len;
119                         memcpy(reorder_data, data, len);
120                         reorder_countdown = 1 + drand48() * reorder_dist;
121                         return len;
122                 }
123         }
124
125         if(reorder_data) {
126                 if(--reorder_countdown < 0) {
127                         total_out += reorder_len;
128                         send(s, reorder_data, reorder_len, MSG_DONTWAIT);
129                         free(reorder_data);
130                         reorder_data = NULL;
131                 }
132         }
133
134         total_out += len;
135         ssize_t result = send(s, data, len, MSG_DONTWAIT);
136
137         if(result <= 0) {
138                 debug("Error sending UDP packet: %s\n", strerror(errno));
139         }
140
141         return result;
142 }
143
144 static void set_mtu(struct utcp *u, int s) {
145         if(!mtu) {
146                 socklen_t optlen = sizeof(mtu);
147                 getsockopt(s, IPPROTO_IP, IP_MTU, &mtu, &optlen);
148         }
149
150         if(!mtu || mtu == 65535) {
151                 mtu = 1500;
152         }
153
154         debug("Using MTU %lu\n", mtu);
155
156         utcp_set_mtu(u, mtu ? mtu - 48 : 1300);
157 }
158
159 int main(int argc, char *argv[]) {
160         srand(time(NULL));
161         srand48(time(NULL));
162
163         if(argc < 2 || argc > 3) {
164                 return 1;
165         }
166
167         bool server = argc == 2;
168         bool connected = false;
169         uint32_t flags = UTCP_TCP;
170         size_t read_size = 102400;
171
172         if(getenv("DROPIN")) {
173                 dropin = atof(getenv("DROPIN"));
174         }
175
176         if(getenv("DROPOUT")) {
177                 dropout = atof(getenv("DROPOUT"));
178         }
179
180         if(getenv("DROPFROM")) {
181                 dropfrom = atoi(getenv("DROPFROM"));
182         }
183
184         if(getenv("DROPTO")) {
185                 dropto = atoi(getenv("DROPTO"));
186         }
187
188         if(getenv("REORDER")) {
189                 reorder = atof(getenv("REORDER"));
190         }
191
192         if(getenv("REORDER_DIST")) {
193                 reorder_dist = atoi(getenv("REORDER_DIST"));
194         }
195
196         if(getenv("FLAGS")) {
197                 flags = atoi(getenv("FLAGS"));
198         }
199
200         if(getenv("READ_SIZE")) {
201                 read_size = atoi(getenv("READ_SIZE"));
202         }
203
204         if(getenv("MTU")) {
205                 mtu = atoi(getenv("MTU"));
206         }
207
208         char *reference_filename = getenv("REFERENCE");
209
210         if(reference_filename) {
211                 reference = fopen(reference_filename, "r");
212         }
213
214         if(dropto < dropfrom) {
215                 dropto = 1 << 30;
216         }
217
218         struct addrinfo *ai;
219
220         struct addrinfo hint = {
221                 .ai_flags = server ? AI_PASSIVE : 0,
222                 .ai_socktype = SOCK_DGRAM,
223         };
224
225         getaddrinfo(server ? NULL : argv[1], server ? argv[1] : argv[2], &hint, &ai);
226
227         if(!ai) {
228                 debug("Could not lookup address: %s\n", strerror(errno));
229                 return 1;
230         }
231
232         int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
233
234         if(s == -1) {
235                 debug("Could not create socket: %s\n", strerror(errno));
236                 return 1;
237         }
238
239         static const int one = 1;
240         setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
241
242         if(server) {
243                 if(bind(s, ai->ai_addr, ai->ai_addrlen)) {
244                         debug("Could not bind: %s\n", strerror(errno));
245                         return 1;
246                 }
247         } else {
248                 if(connect(s, ai->ai_addr, ai->ai_addrlen)) {
249                         debug("Could not connect: %s\n", strerror(errno));
250                         return 1;
251                 }
252
253                 connected = true;
254         }
255
256         freeaddrinfo(ai);
257
258         struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s);
259
260         if(!u) {
261                 debug("Could not initialize UTCP\n");
262                 return 1;
263         }
264
265         utcp_set_user_timeout(u, 10);
266
267         if(!server) {
268                 set_mtu(u, s);
269                 c = utcp_connect_ex(u, 1, do_recv, NULL, flags);
270         }
271
272         struct pollfd fds[2] = {
273                 {.fd = 0, .events = POLLIN | POLLERR | POLLHUP},
274                 {.fd = s, .events = POLLIN | POLLERR | POLLHUP},
275         };
276
277         char buf[102400];
278
279         struct timeval timeout = utcp_timeout(u);
280
281         while(!connected || utcp_is_active(u)) {
282                 size_t max = c ? utcp_get_sndbuf_free(c) : 0;
283
284                 if(max > sizeof(buf)) {
285                         max = sizeof(buf);
286                 }
287
288                 if(max > read_size) {
289                         max = read_size;
290                 }
291
292                 int timeout_ms = timeout.tv_sec * 1000 + timeout.tv_usec / 1000 + 1;
293
294                 debug("polling, dir = %d, timeout = %d\n", dir, timeout_ms);
295
296                 if((dir & DIR_READ) && max) {
297                         poll(fds, 2, timeout_ms);
298                 } else {
299                         poll(fds + 1, 1, timeout_ms);
300                 }
301
302                 if(fds[0].revents) {
303                         fds[0].revents = 0;
304                         ssize_t len = read(0, buf, max);
305                         debug("stdin %zd\n", len);
306
307                         if(len <= 0) {
308                                 fds[0].fd = -1;
309                                 dir &= ~DIR_READ;
310
311                                 if(c) {
312                                         utcp_shutdown(c, SHUT_WR);
313                                 }
314
315                                 if(len == -1) {
316                                         break;
317                                 } else {
318                                         continue;
319                                 }
320                         }
321
322                         if(c) {
323                                 ssize_t sent = utcp_send(c, buf, len);
324
325                                 if(sent != len) {
326                                         debug("Short send: %zd != %zd\n", sent, len);
327                                 }
328                         }
329                 }
330
331                 if(fds[1].revents) {
332                         fds[1].revents = 0;
333                         struct sockaddr_storage ss;
334                         socklen_t sl = sizeof(ss);
335                         int len = recvfrom(s, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr *)&ss, &sl);
336                         debug("netin %zu\n", len);
337
338                         if(len <= 0) {
339                                 debug("Error receiving UDP packet: %s\n", strerror(errno));
340                                 break;
341                         }
342
343                         if(!connected) {
344                                 if(!connect(s, (struct sockaddr *)&ss, sl)) {
345                                         connected = true;
346                                         set_mtu(u, s);
347                                 }
348                         }
349
350                         inpktno++;
351
352                         if(inpktno >= dropto || inpktno < dropfrom || drand48() >= dropin) {
353                                 total_in += len;
354
355                                 if(utcp_recv(u, buf, len) == -1) {
356                                         debug("Error receiving UTCP packet: %s\n", strerror(errno));
357                                 }
358                         } else {
359                                 debug("Dropped incoming packet\n");
360                         }
361                 }
362
363                 timeout = utcp_timeout(u);
364         };
365
366         utcp_close(c);
367
368         utcp_exit(u);
369
370         free(reorder_data);
371
372         debug("Total bytes in: %ld, out: %ld\n", total_in, total_out);
373
374         return 0;
375 }