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