Fix warnings about missing return value checks.
[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, ...) { return false; }
33 struct list_t *connection_list = NULL;
34 bool send_meta(void *c, const char *msg , int len) { return false; }
35 char *logfilename = NULL;
36 bool do_detach = false;
37 struct timeval now;
38
39 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
40         int fd = *(int *)handle;
41         send(fd, data, len, 0);
42         return true;
43 }
44
45 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
46         return true;
47 }
48
49 static void receive_data(sptps_t *sptps) {
50         char buf[4096];
51         int fd = *(int *)sptps->handle;
52         size_t len = recv(fd, buf, sizeof buf, 0);
53         if(!sptps_receive_data(sptps, buf, len))
54                 abort();
55 }
56
57 struct timespec start;
58 struct timespec end;
59 double elapsed;
60 double rate;
61 unsigned int count;
62
63 static void clock_start() {
64         count = 0;
65         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
66 }
67
68 static bool clock_countto(double seconds) {
69         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
70         elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
71         if(elapsed < seconds)
72                 return ++count;
73
74         rate = count / elapsed;
75         return false;
76 }
77
78 int main(int argc, char *argv[]) {
79         ecdsa_t *key1, *key2;
80         ecdh_t *ecdh1, *ecdh2;
81         sptps_t sptps1, sptps2;
82         char buf1[4096], buf2[4096], buf3[4096];
83         double duration = argc > 1 ? atof(argv[1]) : 10;
84
85         crypto_init();
86
87         randomize(buf1, sizeof buf1);
88         randomize(buf2, sizeof buf2);
89         randomize(buf3, sizeof buf3);
90
91         // Key generation
92
93         fprintf(stderr, "Generating keys for %lg seconds: ", duration);
94         for(clock_start(); clock_countto(duration);)
95                 ecdsa_free(ecdsa_generate());
96         fprintf(stderr, "%17.2lf op/s\n", rate);
97
98         key1 = ecdsa_generate();
99         key2 = ecdsa_generate();
100
101         // Ed25519 signatures
102
103         fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
104         for(clock_start(); clock_countto(duration);)
105                 if(!ecdsa_sign(key1, buf1, 256, buf2))
106                         return 1;
107         fprintf(stderr, "%22.2lf op/s\n", rate);
108
109         fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
110         for(clock_start(); clock_countto(duration);)
111                 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
112                         fprintf(stderr, "Signature verification failed\n");
113                         return 1;
114                 }
115         fprintf(stderr, "%20.2lf op/s\n", rate);
116
117         ecdh1 = ecdh_generate_public(buf1);
118         fprintf(stderr, "ECDH for %lg seconds: ", duration);
119         for(clock_start(); clock_countto(duration);) {
120                 ecdh2 = ecdh_generate_public(buf2);
121                 if(!ecdh2)
122                         return 1;
123                 if(!ecdh_compute_shared(ecdh2, buf1, buf3))
124                         return 1;
125         }
126         fprintf(stderr, "%28.2lf op/s\n", rate);
127         ecdh_free(ecdh1);
128
129         // SPTPS authentication phase
130
131         int fd[2];
132         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
133                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
134                 return 1;
135         }
136
137         struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
138
139         fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
140         for(clock_start(); clock_countto(duration);) {
141                 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
142                 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
143                 while(poll(pfd, 2, 0)) {
144                         if(pfd[0].revents)
145                                 receive_data(&sptps1);
146                         if(pfd[1].revents)
147                                 receive_data(&sptps2);
148                 }
149                 sptps_stop(&sptps1);
150                 sptps_stop(&sptps2);
151         }
152         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
153
154         // SPTPS data
155
156         sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
157         sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
158         while(poll(pfd, 2, 0)) {
159                 if(pfd[0].revents)
160                         receive_data(&sptps1);
161                 if(pfd[1].revents)
162                         receive_data(&sptps2);
163         }
164         fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
165         for(clock_start(); clock_countto(duration);) {
166                 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
167                         abort();
168                 receive_data(&sptps2);
169         }
170         rate *= 2 * 1451 * 8;
171         if(rate > 1e9)
172                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
173         else if(rate > 1e6)
174                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
175         else if(rate > 1e3)
176                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
177         sptps_stop(&sptps1);
178         sptps_stop(&sptps2);
179
180         // SPTPS datagram authentication phase
181
182         close(fd[0]);
183         close(fd[1]);
184
185         if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
186                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
187                 return 1;
188         }
189
190         fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
191         for(clock_start(); clock_countto(duration);) {
192                 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
193                 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
194                 while(poll(pfd, 2, 0)) {
195                         if(pfd[0].revents)
196                                 receive_data(&sptps1);
197                         if(pfd[1].revents)
198                                 receive_data(&sptps2);
199                 }
200                 sptps_stop(&sptps1);
201                 sptps_stop(&sptps2);
202         }
203         fprintf(stderr, "%10.2lf op/s\n", rate * 2);
204
205         // SPTPS datagram data
206
207         sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
208         sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
209         while(poll(pfd, 2, 0)) {
210                 if(pfd[0].revents)
211                         receive_data(&sptps1);
212                 if(pfd[1].revents)
213                         receive_data(&sptps2);
214         }
215         fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
216         for(clock_start(); clock_countto(duration);) {
217                 if(!sptps_send_record(&sptps1, 0, buf1, 1451))
218                         abort();
219                 receive_data(&sptps2);
220         }
221         rate *= 2 * 1451 * 8;
222         if(rate > 1e9)
223                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
224         else if(rate > 1e6)
225                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
226         else if(rate > 1e3)
227                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
228         sptps_stop(&sptps1);
229         sptps_stop(&sptps2);
230
231         // Clean up
232
233         close(fd[0]);
234         close(fd[1]);
235         ecdsa_free(key1);
236         ecdsa_free(key2);
237         crypto_exit();
238
239         return 0;
240 }