cc05b8656352e8bcc0000c9abfc29c597e959533
[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 char *logfilename = NULL;
49 bool do_detach = false;
50 struct timeval now;
51
52 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
53         (void)type;
54         int fd = *(int *)handle;
55         send(fd, data, len, 0);
56         return true;
57 }
58
59 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
60         (void)handle;
61         (void)type;
62         (void)data;
63         (void)len;
64         return true;
65 }
66
67 static void receive_data(sptps_t *sptps) {
68         uint8_t buf[4096], *bufp = buf;
69         int fd = *(int *)sptps->handle;
70         size_t len = recv(fd, buf, sizeof(buf), 0);
71
72         while(len) {
73                 size_t done = sptps_receive_data(sptps, bufp, len);
74
75                 if(!done) {
76                         abort();
77                 }
78
79                 bufp += done;
80                 len -= done;
81         }
82 }
83
84 struct timespec start;
85 struct timespec end;
86 double elapsed;
87 double rate;
88 unsigned int count;
89
90 static void clock_start(void) {
91         count = 0;
92         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
93 }
94
95 static bool clock_countto(double seconds) {
96         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
97         elapsed = (double) end.tv_sec + (double) end.tv_nsec * 1e-9
98                   - (double) start.tv_sec - (double) start.tv_nsec * 1e-9;
99
100         if(elapsed < seconds) {
101                 return ++count;
102         }
103
104         rate = count / elapsed;
105         return false;
106 }
107
108 int main(int argc, char *argv[]) {
109         ecdsa_t *key1, *key2;
110         ecdh_t *ecdh1, *ecdh2;
111         sptps_t sptps1, sptps2;
112         uint8_t buf1[4096], buf2[4096], buf3[4096];
113         double duration = argc > 1 ? atof(argv[1]) : 10;
114
115         crypto_init();
116
117         randomize(buf1, sizeof(buf1));
118         randomize(buf2, sizeof(buf2));
119         randomize(buf3, sizeof(buf3));
120
121         // Key generation
122
123         fprintf(stderr, "Generating keys for %lg seconds: ", duration);
124
125         for(clock_start(); clock_countto(duration);) {
126                 ecdsa_free(ecdsa_generate());
127         }
128
129         fprintf(stderr, "%17.2lf op/s\n", rate);
130
131         key1 = ecdsa_generate();
132         key2 = ecdsa_generate();
133
134         // Ed25519 signatures
135
136         fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
137
138         for(clock_start(); clock_countto(duration);)
139                 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
140                         return 1;
141                 }
142
143         fprintf(stderr, "%20.2lf op/s\n", rate);
144
145         fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
146
147         for(clock_start(); clock_countto(duration);)
148                 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
149                         fprintf(stderr, "Signature verification failed\n");
150                         return 1;
151                 }
152
153         fprintf(stderr, "%18.2lf op/s\n", rate);
154
155         ecdh1 = ecdh_generate_public(buf1);
156         fprintf(stderr, "ECDH for %lg seconds: ", duration);
157
158         for(clock_start(); clock_countto(duration);) {
159                 ecdh2 = ecdh_generate_public(buf2);
160
161                 if(!ecdh2) {
162                         return 1;
163                 }
164
165                 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
166                         return 1;
167                 }
168         }
169
170         fprintf(stderr, "%28.2lf op/s\n", rate);
171         ecdh_free(ecdh1);
172
173         // SPTPS authentication phase
174
175         int fd[2];
176
177         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
178                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
179                 return 1;
180         }
181
182         struct pollfd pfd[2] = {{fd[0], POLLIN, 0}, {fd[1], POLLIN, 0}};
183
184         fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
185
186         for(clock_start(); clock_countto(duration);) {
187                 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
188                 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
189
190                 while(poll(pfd, 2, 0)) {
191                         if(pfd[0].revents) {
192                                 receive_data(&sptps1);
193                         }
194
195                         if(pfd[1].revents) {
196                                 receive_data(&sptps2);
197                         }
198                 }
199
200                 sptps_stop(&sptps1);
201                 sptps_stop(&sptps2);
202         }
203
204         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
205
206         // SPTPS data
207
208         sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
209         sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
210
211         while(poll(pfd, 2, 0)) {
212                 if(pfd[0].revents) {
213                         receive_data(&sptps1);
214                 }
215
216                 if(pfd[1].revents) {
217                         receive_data(&sptps2);
218                 }
219         }
220
221         fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
222
223         for(clock_start(); clock_countto(duration);) {
224                 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
225                         abort();
226                 }
227
228                 receive_data(&sptps2);
229         }
230
231         rate *= 2 * 1451 * 8;
232
233         if(rate > 1e9) {
234                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
235         } else if(rate > 1e6) {
236                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
237         } else if(rate > 1e3) {
238                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
239         }
240
241         sptps_stop(&sptps1);
242         sptps_stop(&sptps2);
243
244         // SPTPS datagram authentication phase
245
246         close(fd[0]);
247         close(fd[1]);
248
249         if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
250                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
251                 return 1;
252         }
253
254         fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
255
256         for(clock_start(); clock_countto(duration);) {
257                 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
258                 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
259
260                 while(poll(pfd, 2, 0)) {
261                         if(pfd[0].revents) {
262                                 receive_data(&sptps1);
263                         }
264
265                         if(pfd[1].revents) {
266                                 receive_data(&sptps2);
267                         }
268                 }
269
270                 sptps_stop(&sptps1);
271                 sptps_stop(&sptps2);
272         }
273
274         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
275
276         // SPTPS datagram data
277
278         sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
279         sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
280
281         while(poll(pfd, 2, 0)) {
282                 if(pfd[0].revents) {
283                         receive_data(&sptps1);
284                 }
285
286                 if(pfd[1].revents) {
287                         receive_data(&sptps2);
288                 }
289         }
290
291         fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
292
293         for(clock_start(); clock_countto(duration);) {
294                 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
295                         abort();
296                 }
297
298                 receive_data(&sptps2);
299         }
300
301         rate *= 2 * 1451 * 8;
302
303         if(rate > 1e9) {
304                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
305         } else if(rate > 1e6) {
306                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
307         } else if(rate > 1e3) {
308                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
309         }
310
311         sptps_stop(&sptps1);
312         sptps_stop(&sptps2);
313
314         // Clean up
315
316         close(fd[0]);
317         close(fd[1]);
318         ecdsa_free(key1);
319         ecdsa_free(key2);
320         crypto_exit();
321
322         return 0;
323 }