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