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