From 13cb1f2dcf8865b076a7e8290d8f864d91a2d7c7 Mon Sep 17 00:00:00 2001 From: Cristian Stoica Date: Mon, 24 Oct 2016 16:33:55 +0300 Subject: [PATCH 050/104] add basic optargs support for async_speed test Signed-off-by: Cristian Stoica --- tests/async_speed.c | 302 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 202 insertions(+), 100 deletions(-) diff --git a/tests/async_speed.c b/tests/async_speed.c index 15ab80c..fff3414 100644 --- a/tests/async_speed.c +++ b/tests/async_speed.c @@ -27,9 +27,45 @@ #include #include #include +#include +#include #ifdef ENABLE_ASYNC +struct test_params { + bool tflag; + bool nflag; + int tvalue; + int nvalue; +}; + +const char usage_str[] = "Usage: %s [OPTION]... |\n" + "Run benchmark test for cipher or hash\n\n" + " -t \t" "time to run each test (default 10 secs)\n" + " -n \t" "size of the test buffer\n" + " -h\t\t" "show this help\n" +; + +int run_null(int fdc, struct test_params tp); +int run_aes_cbc(int fdc, struct test_params tp); +int run_aes_xts(int fdc, struct test_params tp); +int run_crc32c(int fdc, struct test_params tp); +int run_sha1(int fdc, struct test_params tp); +int run_sha256(int fdc, struct test_params tp); + +#define ALG_COUNT 6 +struct { + char *name; + int (*func)(int, struct test_params); +} ciphers[ALG_COUNT] = { + {"null", run_null}, + {"aes-cbc", run_aes_cbc}, + {"aes-xts", run_aes_xts}, + {"crc32c", run_crc32c}, + {"sha1", run_sha1}, + {"sha256", run_sha256}, +}; + static double udifftimeval(struct timeval start, struct timeval end) { return (double)(end.tv_usec - start.tv_usec) + @@ -61,7 +97,7 @@ static void value2human(double bytes, double time, double* data, double* speed,c } -int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) +int encrypt_data(int fdc, struct test_params tp, struct session_op *sess) { struct crypt_op cop; char *buffer[64], iv[32]; @@ -72,31 +108,33 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) double secs, ddata, dspeed; char metric[16]; int rc, wqueue = 0, bufidx = 0; + int alignmask; memset(iv, 0x23, 32); - printf("\tEncrypting in chunks of %d bytes: ", chunksize); + printf("\tEncrypting in chunks of %d bytes: ", tp.nvalue); fflush(stdout); + alignmask = get_alignmask(fdc, sess); for (rc = 0; rc < 64; rc++) { if (alignmask) { - if (posix_memalign((void **)(buffer + rc), alignmask + 1, chunksize)) { + if (posix_memalign((void **)(buffer + rc), alignmask + 1, tp.nvalue)) { printf("posix_memalign() failed!\n"); return 1; } } else { - if (!(buffer[rc] = malloc(chunksize))) { + if (!(buffer[rc] = malloc(tp.nvalue))) { perror("malloc()"); return 1; } } - memset(buffer[rc], val++, chunksize); + memset(buffer[rc], val++, tp.nvalue); } pfd.fd = fdc; pfd.events = POLLOUT | POLLIN; must_finish = 0; - alarm(5); + alarm(tp.tvalue); gettimeofday(&start, NULL); do { @@ -111,7 +149,7 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) if (pfd.revents & POLLOUT) { memset(&cop, 0, sizeof(cop)); cop.ses = sess->ses; - cop.len = chunksize; + cop.len = tp.nvalue; cop.iv = (unsigned char *)iv; cop.op = COP_ENCRYPT; cop.src = cop.dst = (unsigned char *)buffer[bufidx]; @@ -146,25 +184,75 @@ int encrypt_data(struct session_op *sess, int fdc, int chunksize, int alignmask) return 0; } -int main(void) +void usage(char *cmd_name) { - int fd, i, fdc = -1, alignmask = 0; - struct session_op sess; -#ifdef CIOCGSESSINFO - struct session_info_op siop; -#endif - char keybuf[32]; + printf(usage_str, cmd_name); +} - signal(SIGALRM, alarm_handler); +int run_test(int id, struct test_params tp) +{ + int fd; + int fdc; - if ((fd = open("/dev/crypto", O_RDWR, 0)) < 0) { + fd = open("/dev/crypto", O_RDWR, 0); + if (fd < 0) { perror("open()"); - return 1; + return fd; } if (ioctl(fd, CRIOGET, &fdc)) { perror("ioctl(CRIOGET)"); - return 1; + return -EINVAL; + } + + ciphers[id].func(fdc, tp); + + close(fdc); + close(fd); +} + +int get_alignmask(int fdc, struct session_op *sess) +{ + int alignmask; + +#ifdef CIOCGSESSINFO + struct session_info_op siop; + + siop.ses = sess->ses; + if (ioctl(fdc, CIOCGSESSINFO, &siop)) { + perror("ioctl(CIOCGSESSINFO)"); + return -EINVAL; + } + alignmask = siop.alignmask; +#else + alignmask = 0; +#endif + + return alignmask; +} + +void do_test_vectors(int fdc, struct test_params tp, struct session_op *sess) +{ + int i; + + if (tp.nflag) { + encrypt_data(fdc, tp, sess); + } else { + for (i = 256; i <= (64 * 1024); i *= 2) { + tp.nvalue = i; + if (encrypt_data(fdc, tp, sess)) { + break; + } + } } +} + + +int run_null(int fdc, struct test_params tp) +{ + struct session_op sess; + char keybuf[32]; + int alignmask; + int i; fprintf(stderr, "Testing NULL cipher: \n"); memset(&sess, 0, sizeof(sess)); @@ -173,21 +261,19 @@ int main(void) sess.key = (unsigned char *)keybuf; if (ioctl(fdc, CIOCGSESSION, &sess)) { perror("ioctl(CIOCGSESSION)"); - return 1; - } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSINFO)"); - return 1; + return -EINVAL; } - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 4096); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) - break; - } + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int run_aes_cbc(int fdc, struct test_params tp) +{ + struct session_op sess; + char keybuf[32]; + int alignmask; + int i; fprintf(stderr, "\nTesting AES-128-CBC cipher: \n"); memset(&sess, 0, sizeof(sess)); @@ -197,21 +283,17 @@ int main(void) sess.key = (unsigned char *)keybuf; if (ioctl(fdc, CIOCGSESSION, &sess)) { perror("ioctl(CIOCGSESSION)"); - return 1; - } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSINFO)"); - return 1; + return -EINVAL; } - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) - break; - } + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int run_aes_xts(int fdc, struct test_params tp) +{ + struct session_op sess; + char keybuf[32]; fprintf(stderr, "\nTesting AES-256-XTS cipher: \n"); memset(&sess, 0, sizeof(sess)); @@ -221,21 +303,16 @@ int main(void) sess.key = (unsigned char *)keybuf; if (ioctl(fdc, CIOCGSESSION, &sess)) { perror("ioctl(CIOCGSESSION)"); - return 1; + return -EINVAL; } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) - break; - } + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int run_crc32c(int fdc, struct test_params tp) +{ + struct session_op sess; fprintf(stderr, "\nTesting CRC32C hash: \n"); memset(&sess, 0, sizeof(sess)); @@ -244,21 +321,14 @@ int main(void) perror("ioctl(CIOCGSESSION)"); return 1; } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - printf("requested hash CRYPTO_CRC32C, got %s with driver %s\n", - siop.hash_info.cra_name, siop.hash_info.cra_driver_name); - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) - break; - } + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int run_sha1(int fdc, struct test_params tp) +{ + struct session_op sess; fprintf(stderr, "\nTesting SHA-1 hash: \n"); memset(&sess, 0, sizeof(sess)); @@ -267,21 +337,14 @@ int main(void) perror("ioctl(CIOCGSESSION)"); return 1; } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - printf("requested hash CRYPTO_SHA1, got %s with driver %s\n", - siop.hash_info.cra_name, siop.hash_info.cra_driver_name); - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) - break; - } + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int run_sha256(int fdc, struct test_params tp) +{ + struct session_op sess; fprintf(stderr, "\nTesting SHA2-256 hash: \n"); memset(&sess, 0, sizeof(sess)); @@ -290,25 +353,64 @@ int main(void) perror("ioctl(CIOCGSESSION)"); return 1; } -#ifdef CIOCGSESSINFO - siop.ses = sess.ses; - if (ioctl(fdc, CIOCGSESSINFO, &siop)) { - perror("ioctl(CIOCGSESSION)"); - return 1; - } - printf("requested hash CRYPTO_SHA2_256, got %s with driver %s\n", - siop.hash_info.cra_name, siop.hash_info.cra_driver_name); - alignmask = siop.alignmask; -#endif - for (i = 256; i <= (64 * 1024); i *= 2) { - if (encrypt_data(&sess, fdc, i, alignmask)) + do_test_vectors(fdc, tp, &sess); + return 0; +} + +int main(int argc, char **argv) +{ + int i; + int c; + int index; + bool alg_flag; + char *alg_name; + struct test_params tp; + + tp.tflag = false; + tp.nflag = false; + alg_flag = false; + opterr = 0; + while ((c = getopt(argc, argv, "hn:t:")) != -1) { + switch (c) { + case 'n': + tp.nvalue = atoi(optarg); + tp.nflag = true; + break; + case 't': + tp.tvalue = atoi(optarg); + tp.tflag = true; break; + case 'h': /* no break */ + default: + usage(argv[0]); + exit(1); + } + } + + /* the name of a specific test asked on the command line */ + if (optind < argc) { + alg_name = argv[optind]; + alg_flag = true; + } + + /* default test time */ + if (!tp.tflag) { + tp.tvalue = 5; + } + + signal(SIGALRM, alarm_handler); + + for (i = 0; i < ALG_COUNT; i++) { + if (alg_flag) { + if (strcmp(alg_name, ciphers[i].name) == 0) { + run_test(i, tp); + } + } else { + run_test(i, tp); + } } -end: - close(fdc); - close(fd); return 0; } -- 2.10.2