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