Add support for meson build system
[tinc] / src / sptps_speed.c
1 /*
2     sptps_speed.c -- SPTPS benchmark
3     Copyright (C) 2013-2022 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21 #include "utils.h"
22
23 #include <poll.h>
24
25 #include "crypto.h"
26 #include "ecdh.h"
27 #include "ecdsa.h"
28 #include "ecdsagen.h"
29 #include "meta.h"
30 #include "protocol.h"
31 #include "sptps.h"
32
33 // Symbols necessary to link with logger.o
34 bool send_request(struct connection_t *c, const char *msg, ...) {
35         (void)c;
36         (void)msg;
37         return false;
38 }
39
40 list_t connection_list;
41
42 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
43         (void)c;
44         (void)msg;
45         (void)len;
46         return false;
47 }
48 bool do_detach = false;
49 struct timeval now;
50
51 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
52         (void)type;
53         int fd = *(int *)handle;
54         send(fd, data, len, 0);
55         return true;
56 }
57
58 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
59         (void)handle;
60         (void)type;
61         (void)data;
62         (void)len;
63         return true;
64 }
65
66 static void receive_data(sptps_t *sptps) {
67         uint8_t buf[4096], *bufp = buf;
68         int fd = *(int *)sptps->handle;
69         size_t len = recv(fd, buf, sizeof(buf), 0);
70
71         while(len) {
72                 size_t done = sptps_receive_data(sptps, bufp, len);
73
74                 if(!done) {
75                         abort();
76                 }
77
78                 bufp += done;
79                 len -= done;
80         }
81 }
82
83 struct timespec start;
84 struct timespec end;
85 double elapsed;
86 double rate;
87 unsigned int count;
88
89 static void clock_start(void) {
90         count = 0;
91         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
92 }
93
94 static bool clock_countto(double seconds) {
95         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
96         elapsed = (double) end.tv_sec + (double) end.tv_nsec * 1e-9
97                   - (double) start.tv_sec - (double) start.tv_nsec * 1e-9;
98
99         if(elapsed < seconds) {
100                 return ++count;
101         }
102
103         rate = count / elapsed;
104         return false;
105 }
106
107 int main(int argc, char *argv[]) {
108         ecdsa_t *key1, *key2;
109         ecdh_t *ecdh1, *ecdh2;
110         sptps_t sptps1, sptps2;
111         uint8_t buf1[4096], buf2[4096], buf3[4096];
112         double duration = argc > 1 ? atof(argv[1]) : 10;
113
114         crypto_init();
115
116         randomize(buf1, sizeof(buf1));
117         randomize(buf2, sizeof(buf2));
118         randomize(buf3, sizeof(buf3));
119
120         // Key generation
121
122         fprintf(stderr, "Generating keys for %lg seconds: ", duration);
123
124         for(clock_start(); clock_countto(duration);) {
125                 ecdsa_free(ecdsa_generate());
126         }
127
128         fprintf(stderr, "%17.2lf op/s\n", rate);
129
130         key1 = ecdsa_generate();
131         key2 = ecdsa_generate();
132
133         // Ed25519 signatures
134
135         fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
136
137         for(clock_start(); clock_countto(duration);)
138                 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
139                         return 1;
140                 }
141
142         fprintf(stderr, "%20.2lf op/s\n", rate);
143
144         fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
145
146         for(clock_start(); clock_countto(duration);)
147                 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
148                         fprintf(stderr, "Signature verification failed\n");
149                         return 1;
150                 }
151
152         fprintf(stderr, "%18.2lf op/s\n", rate);
153
154         ecdh1 = ecdh_generate_public(buf1);
155         fprintf(stderr, "ECDH for %lg seconds: ", duration);
156
157         for(clock_start(); clock_countto(duration);) {
158                 ecdh2 = ecdh_generate_public(buf2);
159
160                 if(!ecdh2) {
161                         return 1;
162                 }
163
164                 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
165                         return 1;
166                 }
167         }
168
169         fprintf(stderr, "%28.2lf op/s\n", rate);
170         ecdh_free(ecdh1);
171
172         // SPTPS authentication phase
173
174         int fd[2];
175
176         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
177                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
178                 return 1;
179         }
180
181         struct pollfd pfd[2] = {{fd[0], POLLIN, 0}, {fd[1], POLLIN, 0}};
182
183         fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
184
185         for(clock_start(); clock_countto(duration);) {
186                 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
187                 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
188
189                 while(poll(pfd, 2, 0)) {
190                         if(pfd[0].revents) {
191                                 receive_data(&sptps1);
192                         }
193
194                         if(pfd[1].revents) {
195                                 receive_data(&sptps2);
196                         }
197                 }
198
199                 sptps_stop(&sptps1);
200                 sptps_stop(&sptps2);
201         }
202
203         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
204
205         // SPTPS data
206
207         sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
208         sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
209
210         while(poll(pfd, 2, 0)) {
211                 if(pfd[0].revents) {
212                         receive_data(&sptps1);
213                 }
214
215                 if(pfd[1].revents) {
216                         receive_data(&sptps2);
217                 }
218         }
219
220         fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
221
222         for(clock_start(); clock_countto(duration);) {
223                 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
224                         abort();
225                 }
226
227                 receive_data(&sptps2);
228         }
229
230         rate *= 2 * 1451 * 8;
231
232         if(rate > 1e9) {
233                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
234         } else if(rate > 1e6) {
235                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
236         } else if(rate > 1e3) {
237                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
238         }
239
240         sptps_stop(&sptps1);
241         sptps_stop(&sptps2);
242
243         // SPTPS datagram authentication phase
244
245         close(fd[0]);
246         close(fd[1]);
247
248         if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
249                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
250                 return 1;
251         }
252
253         fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
254
255         for(clock_start(); clock_countto(duration);) {
256                 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
257                 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
258
259                 while(poll(pfd, 2, 0)) {
260                         if(pfd[0].revents) {
261                                 receive_data(&sptps1);
262                         }
263
264                         if(pfd[1].revents) {
265                                 receive_data(&sptps2);
266                         }
267                 }
268
269                 sptps_stop(&sptps1);
270                 sptps_stop(&sptps2);
271         }
272
273         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
274
275         // SPTPS datagram data
276
277         sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
278         sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
279
280         while(poll(pfd, 2, 0)) {
281                 if(pfd[0].revents) {
282                         receive_data(&sptps1);
283                 }
284
285                 if(pfd[1].revents) {
286                         receive_data(&sptps2);
287                 }
288         }
289
290         fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
291
292         for(clock_start(); clock_countto(duration);) {
293                 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
294                         abort();
295                 }
296
297                 receive_data(&sptps2);
298         }
299
300         rate *= 2 * 1451 * 8;
301
302         if(rate > 1e9) {
303                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
304         } else if(rate > 1e6) {
305                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
306         } else if(rate > 1e3) {
307                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
308         }
309
310         sptps_stop(&sptps1);
311         sptps_stop(&sptps2);
312
313         // Clean up
314
315         close(fd[0]);
316         close(fd[1]);
317         ecdsa_free(key1);
318         ecdsa_free(key2);
319         crypto_exit();
320
321         return 0;
322 }