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