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.
34 // Symbols necessary to link with logger.o
35 bool send_request(struct connection_t *c, const char *msg, ...) {
41 list_t connection_list;
43 bool send_meta(struct connection_t *c, const void *msg, size_t len) {
49 bool do_detach = false;
52 static bool send_data(void *handle, uint8_t type, const void *data, size_t len) {
54 int fd = *(int *)handle;
55 send(fd, data, len, 0);
59 static bool receive_record(void *handle, uint8_t type, const void *data, uint16_t len) {
67 static void receive_data(sptps_t *sptps) {
68 uint8_t buf[4096], *bufp = buf;
69 int fd = *(int *)sptps->handle;
70 size_t len = recv(fd, buf, sizeof(buf), 0);
73 size_t done = sptps_receive_data(sptps, bufp, len);
84 struct timespec start;
90 static void clock_start(void) {
92 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
95 static bool clock_countto(double seconds) {
96 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
97 elapsed = (double) end.tv_sec + (double) end.tv_nsec * 1e-9
98 - (double) start.tv_sec - (double) start.tv_nsec * 1e-9;
100 if(elapsed < seconds) {
104 rate = count / elapsed;
108 static int run_benchmark(int argc, char *argv[]) {
109 ecdsa_t *key1, *key2;
110 ecdh_t *ecdh1, *ecdh2;
111 sptps_t sptps1, sptps2;
112 uint8_t buf1[4096], buf2[4096], buf3[4096];
113 double duration = argc > 1 ? atof(argv[1]) : 10;
115 randomize(buf1, sizeof(buf1));
116 randomize(buf2, sizeof(buf2));
117 randomize(buf3, sizeof(buf3));
121 fprintf(stderr, "Generating keys for %lg seconds: ", duration);
123 for(clock_start(); clock_countto(duration);) {
124 ecdsa_free(ecdsa_generate());
127 fprintf(stderr, "%17.2lf op/s\n", rate);
129 key1 = ecdsa_generate();
130 key2 = ecdsa_generate();
132 // Ed25519 signatures
134 fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
136 for(clock_start(); clock_countto(duration);)
137 if(!ecdsa_sign(key1, buf1, 256, buf2)) {
141 fprintf(stderr, "%20.2lf op/s\n", rate);
143 fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
145 for(clock_start(); clock_countto(duration);)
146 if(!ecdsa_verify(key1, buf1, 256, buf2)) {
147 fprintf(stderr, "Signature verification failed\n");
151 fprintf(stderr, "%18.2lf op/s\n", rate);
153 ecdh1 = ecdh_generate_public(buf1);
154 fprintf(stderr, "ECDH for %lg seconds: ", duration);
156 for(clock_start(); clock_countto(duration);) {
157 ecdh2 = ecdh_generate_public(buf2);
163 if(!ecdh_compute_shared(ecdh2, buf1, buf3)) {
168 fprintf(stderr, "%28.2lf op/s\n", rate);
172 struct pollfd pfd[2] = {{fd[0], POLLIN}, {fd[1], POLLIN}};
174 sptps_params_t params1 = {
180 .label = "sptps_speed",
181 .send_data = send_data,
182 .receive_record = receive_record,
185 sptps_params_t params2 = {
191 .label = "sptps_speed",
192 .send_data = send_data,
193 .receive_record = receive_record,
196 static const char *suite_names[] = {
201 for(uint8_t suite = 0; suite < 2; suite++) {
202 fprintf(stderr, "\nCipher suite %u (%s):\n", suite, suite_names[suite]);
203 params1.preferred_suite = params2.preferred_suite = suite;
205 // SPTPS authentication phase
207 fprintf(stderr, "SPTPS/TCP authenticate for %lg seconds: ", duration);
209 if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
210 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
214 pfd[0].fd = fd[0], pfd[1].fd = fd[1];
215 params1.datagram = params2.datagram = false;
217 for(clock_start(); clock_countto(duration);) {
218 sptps_start(&sptps1, ¶ms1);
219 sptps_start(&sptps2, ¶ms2);
221 while(poll(pfd, 2, 0)) {
223 receive_data(&sptps1);
227 receive_data(&sptps2);
235 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
239 sptps_start(&sptps1, ¶ms1);
240 sptps_start(&sptps2, ¶ms2);
242 while(poll(pfd, 2, 0)) {
244 receive_data(&sptps1);
248 receive_data(&sptps2);
252 fprintf(stderr, "SPTPS/TCP transmit for %lg seconds: ", duration);
254 for(clock_start(); clock_countto(duration);) {
255 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
259 receive_data(&sptps2);
262 rate *= 2 * 1451 * 8;
265 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
266 } else if(rate > 1e6) {
267 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
268 } else if(rate > 1e3) {
269 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
278 // SPTPS datagram authentication phase
280 if(socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)) {
281 fprintf(stderr, "Could not create a UNIX socket pair: %s\n", sockstrerror(sockerrno));
285 pfd[0].fd = fd[0], pfd[1].fd = fd[1];
286 params1.datagram = params2.datagram = true;
288 fprintf(stderr, "SPTPS/UDP authenticate for %lg seconds: ", duration);
290 for(clock_start(); clock_countto(duration);) {
291 sptps_start(&sptps1, ¶ms1);
292 sptps_start(&sptps2, ¶ms2);
294 while(poll(pfd, 2, 0)) {
296 receive_data(&sptps1);
300 receive_data(&sptps2);
308 fprintf(stderr, "%10.2lf op/s\n", rate * 2);
310 // SPTPS datagram data
312 sptps_start(&sptps1, ¶ms1);
313 sptps_start(&sptps2, ¶ms2);
315 while(poll(pfd, 2, 0)) {
317 receive_data(&sptps1);
321 receive_data(&sptps2);
325 fprintf(stderr, "SPTPS/UDP transmit for %lg seconds: ", duration);
327 for(clock_start(); clock_countto(duration);) {
328 if(!sptps_send_record(&sptps1, 0, buf1, 1451)) {
332 receive_data(&sptps2);
335 rate *= 2 * 1451 * 8;
338 fprintf(stderr, "%14.2lf Gbit/s\n", rate / 1e9);
339 } else if(rate > 1e6) {
340 fprintf(stderr, "%14.2lf Mbit/s\n", rate / 1e6);
341 } else if(rate > 1e3) {
342 fprintf(stderr, "%14.2lf kbit/s\n", rate / 1e3);
360 int main(int argc, char *argv[]) {
364 int result = run_benchmark(argc, argv);