2 sptps_speed.c -- SPTPS benchmark
3 Copyright (C) 2013-2022 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
33 // Symbols necessary to link with logger.o
34 bool send_request(struct connection_t *c, const char *msg, ...) {
40 list_t connection_list;
42 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
48 bool do_detach = false;
51 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
53 int fd = *(int *)handle;
54 send(fd, data, len, 0);
58 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
66 static void receive_data(sptps_t *sptps) {
67 uint8_t buf[4096], *bufp = buf;
68 int fd = *(int *)sptps->handle;
69 size_t len = recv(fd, buf, sizeof(buf), 0);
72 size_t done = sptps_receive_data(sptps, bufp, len);
83 struct timespec start;
89 static void clock_start(void) {
91 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
94 static bool clock_countto(double seconds) {
95 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
96 elapsed = (double) end.tv_sec + (double) end.tv_nsec * 1e-9
97 - (double) start.tv_sec - (double) start.tv_nsec * 1e-9;
99 if(elapsed < seconds) {
103 rate = count / elapsed;
107 int main(int argc, char *argv[]) {
108 ecdsa_t *key1, *key2;
109 ecdh_t *ecdh1, *ecdh2;
110 sptps_t sptps1, sptps2;
111 uint8_t buf1[4096], buf2[4096], buf3[4096];
112 double duration = argc > 1 ? atof(argv[1]) : 10;
116 randomize(buf1, sizeof(buf1));
117 randomize(buf2, sizeof(buf2));
118 randomize(buf3, sizeof(buf3));
122 fprintf(stderr, "Generating keys for %lg seconds: ", duration);
124 for(clock_start(); clock_countto(duration);) {
125 ecdsa_free(ecdsa_generate());
128 fprintf(stderr, "%17.2lf op/s\n", rate);
130 key1 = ecdsa_generate();
131 key2 = ecdsa_generate();
133 // Ed25519 signatures
135 fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
137 for(clock_start(); clock_countto(duration);)
138 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
142 fprintf(stderr, "%20.2lf op/s\n", rate);
144 fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
146 for(clock_start(); clock_countto(duration);)
147 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
148 fprintf(stderr, "Signature verification failed\n");
152 fprintf(stderr, "%18.2lf op/s\n", rate);
154 ecdh1 = ecdh_generate_public(buf1);
155 fprintf(stderr, "ECDH for %lg seconds: ", duration);
157 for(clock_start(); clock_countto(duration);) {
158 ecdh2 = ecdh_generate_public(buf2);
164 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
169 fprintf(stderr, "%28.2lf op/s\n", rate);
172 // SPTPS authentication phase
176 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
177 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
181 struct pollfd pfd[2] = {{fd[0], POLLIN, 0}, {fd[1], POLLIN, 0}};
183 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
185 for(clock_start(); clock_countto(duration);) {
186 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
187 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
189 while(poll(pfd, 2, 0)) {
191 receive_data(&sptps1);
195 receive_data(&sptps2);
203 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
207 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
208 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
210 while(poll(pfd, 2, 0)) {
212 receive_data(&sptps1);
216 receive_data(&sptps2);
220 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
222 for(clock_start(); clock_countto(duration);) {
223 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
227 receive_data(&sptps2);
230 rate *= 2 * 1451 * 8;
233 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
234 } else if(rate > 1e6) {
235 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
236 } else if(rate > 1e3) {
237 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
243 // SPTPS datagram authentication phase
248 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
249 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
253 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
255 for(clock_start(); clock_countto(duration);) {
256 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
257 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
259 while(poll(pfd, 2, 0)) {
261 receive_data(&sptps1);
265 receive_data(&sptps2);
273 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
275 // SPTPS datagram data
277 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
278 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
280 while(poll(pfd, 2, 0)) {
282 receive_data(&sptps1);
286 receive_data(&sptps2);
290 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
292 for(clock_start(); clock_countto(duration);) {
293 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
297 receive_data(&sptps2);
300 rate *= 2 * 1451 * 8;
303 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
304 } else if(rate > 1e6) {
305 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
306 } else if(rate > 1e3) {
307 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);