Add a benchmark for the SPTPS protocol.
[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         sptps_receive_data(sptps, buf, len);
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(int 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
81         crypto_init();
82
83         // Key generation
84
85         fprintf(stderr, "Generating keys for 10 seconds: ");
86         for(clock_start(); clock_countto(10);)
87                 key1 = ecdsa_generate();
88         fprintf(stderr, "%13.2lf op/s\n", rate);
89
90         // ECDSA signatures
91
92         fprintf(stderr, "ECDSA sign for 10 seconds: ");
93         for(clock_start(); clock_countto(10);)
94                 ecdsa_sign(key1, buf1, 256, buf2);
95         fprintf(stderr, "%18.2lf op/s\n", rate);
96         
97         fprintf(stderr, "ECDSA verify for 10 seconds: ");
98         for(clock_start(); clock_countto(10);)
99                 ecdsa_verify(key1, buf1, 256, buf2);
100         fprintf(stderr, "%16.2lf op/s\n", rate);
101
102         ecdh1 = ecdh_generate_public(buf1);
103         fprintf(stderr, "ECDH for 10 seconds: ");
104         for(clock_start(); clock_countto(10);) {
105                 ecdh2 = ecdh_generate_public(buf2);
106                 ecdh_compute_shared(ecdh2, buf1, buf3);
107         }
108         fprintf(stderr, "%24.2lf op/s\n", rate);
109         ecdh_free(ecdh1);
110
111         // SPTPS authentication phase
112
113         key2 = ecdsa_generate();
114
115         int fd[2];
116         if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
117                 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", strerror(errno));
118                 return 1;
119         }
120
121         struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
122
123         fprintf(stderr, "SPTPS authenticate for 10 seconds: ");
124         for(clock_start(); clock_countto(10);) {
125                 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
126                 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
127                 while(poll(pfd, 2, 0)) {
128                         if(pfd[0].revents)
129                                 receive_data(&sptps1);
130                         if(pfd[1].revents)
131                                 receive_data(&sptps2);
132                 }
133                 sptps_stop(&sptps1);
134                 sptps_stop(&sptps2);
135         } 
136         fprintf(stderr, "%10.2lf op/s\n", rate * 2);    
137
138         // SPTPS data
139
140         sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
141         sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
142         while(poll(pfd, 2, 0)) {
143                 if(pfd[0].revents)
144                         receive_data(&sptps1);
145                 if(pfd[1].revents)
146                         receive_data(&sptps2);
147         }
148         fprintf(stderr, "SPTPS transmit for 10 seconds: ");
149         for(clock_start(); clock_countto(10);) {
150                 sptps_send_record(&sptps1, 0, buf1, 1451);
151                 receive_data(&sptps2);
152         }
153         rate *= 2 * 1451 * 8;
154         if(rate > 1e9)
155                 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
156         else if(rate > 1e6)
157                 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
158         else if(rate > 1e3)
159                 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
160         sptps_stop(&sptps1);
161         sptps_stop(&sptps2);
162
163         // Clean up
164
165         close(fd[0]);
166         close(fd[1]);
167         ecdsa_free(key1);
168         ecdsa_free(key2);
169         crypto_exit();
170
171         return 0;
172 }