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