2 sptps_speed.c -- SPTPS benchmark
3 Copyright (C) 2013-2014 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.
31 // Symbols necessary to link with logger.o
32 bool send_request(void *c, const char *msg, ...) {
35 struct list_t *connection_list = NULL;
36 bool send_meta(void *c, const char *msg, int len) {
39 char *logfilename = NULL;
40 bool do_detach = false;
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);
49 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
53 static void receive_data(sptps_t *sptps) {
54 char buf[4096], *bufp = buf;
55 int fd = *(int *)sptps->handle;
56 size_t len = recv(fd, buf, sizeof(buf), 0);
59 size_t done = sptps_receive_data(sptps, bufp, len);
70 struct timespec start;
76 static void clock_start() {
78 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
81 static bool clock_countto(double seconds) {
82 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
83 elapsed = end.tv_sec + end.tv_nsec * 1e-9 - start.tv_sec - start.tv_nsec * 1e-9;
85 if(elapsed < seconds) {
89 rate = count / elapsed;
93 int main(int argc, char *argv[]) {
95 ecdh_t *ecdh1, *ecdh2;
96 sptps_t sptps1, sptps2;
97 char buf1[4096], buf2[4096], buf3[4096];
98 double duration = argc > 1 ? atof(argv[1]) : 10;
102 randomize(buf1, sizeof(buf1));
103 randomize(buf2, sizeof(buf2));
104 randomize(buf3, sizeof(buf3));
108 fprintf(stderr, "Generating keys for %lg seconds: ", duration);
110 for(clock_start(); clock_countto(duration);) {
111 ecdsa_free(ecdsa_generate());
114 fprintf(stderr, "%17.2lf op/s\n", rate);
116 key1 = ecdsa_generate();
117 key2 = ecdsa_generate();
119 // Ed25519 signatures
121 fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
123 for(clock_start(); clock_countto(duration);)
124 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
128 fprintf(stderr, "%20.2lf op/s\n", rate);
130 fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
132 for(clock_start(); clock_countto(duration);)
133 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
134 fprintf(stderr, "Signature verification failed\n");
138 fprintf(stderr, "%18.2lf op/s\n", rate);
140 ecdh1 = ecdh_generate_public(buf1);
141 fprintf(stderr, "ECDH for %lg seconds: ", duration);
143 for(clock_start(); clock_countto(duration);) {
144 ecdh2 = ecdh_generate_public(buf2);
150 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
155 fprintf(stderr, "%28.2lf op/s\n", rate);
158 // SPTPS authentication phase
162 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
163 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
167 struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
169 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
171 for(clock_start(); clock_countto(duration);) {
172 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
173 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
175 while(poll(pfd, 2, 0)) {
177 receive_data(&sptps1);
181 receive_data(&sptps2);
189 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
193 sptps_start(&sptps1, fd + 0, true, false, key1, key2, "sptps_speed", 11, send_data, receive_record);
194 sptps_start(&sptps2, fd + 1, false, false, key2, key1, "sptps_speed", 11, send_data, receive_record);
196 while(poll(pfd, 2, 0)) {
198 receive_data(&sptps1);
202 receive_data(&sptps2);
206 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
208 for(clock_start(); clock_countto(duration);) {
209 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
213 receive_data(&sptps2);
216 rate *= 2 * 1451 * 8;
219 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
220 } else if(rate > 1e6) {
221 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
222 } else if(rate > 1e3) {
223 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
229 // SPTPS datagram authentication phase
234 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
235 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
239 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
241 for(clock_start(); clock_countto(duration);) {
242 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
243 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
245 while(poll(pfd, 2, 0)) {
247 receive_data(&sptps1);
251 receive_data(&sptps2);
259 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
261 // SPTPS datagram data
263 sptps_start(&sptps1, fd + 0, true, true, key1, key2, "sptps_speed", 11, send_data, receive_record);
264 sptps_start(&sptps2, fd + 1, false, true, key2, key1, "sptps_speed", 11, send_data, receive_record);
266 while(poll(pfd, 2, 0)) {
268 receive_data(&sptps1);
272 receive_data(&sptps2);
276 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
278 for(clock_start(); clock_countto(duration);) {
279 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
283 receive_data(&sptps2);
286 rate *= 2 * 1451 * 8;
289 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
290 } else if(rate > 1e6) {
291 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
292 } else if(rate > 1e3) {
293 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);