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