1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
6 #include "crypto_bench.skel.h"
8 #define MAX_CIPHER_LEN 32
10 static struct crypto_ctx {
11 struct crypto_bench *skel;
15 static struct crypto_args {
20 .crypto_cipher = "ecb(aes)",
24 ARG_CRYPTO_LEN = 5000,
25 ARG_CRYPTO_CIPHER = 5001,
28 static const struct argp_option opts[] = {
29 { "crypto-len", ARG_CRYPTO_LEN, "CRYPTO_LEN", 0,
30 "Set the length of crypto buffer" },
31 { "crypto-cipher", ARG_CRYPTO_CIPHER, "CRYPTO_CIPHER", 0,
32 "Set the cipher to use (default:ecb(aes))" },
36 static error_t crypto_parse_arg(int key, char *arg, struct argp_state *state)
40 args.crypto_len = strtoul(arg, NULL, 10);
41 if (!args.crypto_len ||
42 args.crypto_len > sizeof(ctx.skel->bss->dst)) {
43 fprintf(stderr, "Invalid crypto buffer len (limit %zu)\n",
44 sizeof(ctx.skel->bss->dst));
48 case ARG_CRYPTO_CIPHER:
49 args.crypto_cipher = strdup(arg);
50 if (!strlen(args.crypto_cipher) ||
51 strlen(args.crypto_cipher) > MAX_CIPHER_LEN) {
52 fprintf(stderr, "Invalid crypto cipher len (limit %d)\n",
58 return ARGP_ERR_UNKNOWN;
64 const struct argp bench_crypto_argp = {
66 .parser = crypto_parse_arg,
69 static void crypto_validate(void)
71 if (env.consumer_cnt != 0) {
72 fprintf(stderr, "bpf crypto benchmark doesn't support consumer!\n");
77 static void crypto_setup(void)
79 LIBBPF_OPTS(bpf_test_run_opts, opts);
85 if (!sz || sz > sizeof(ctx.skel->bss->dst)) {
86 fprintf(stderr, "invalid encrypt buffer size (source %zu, target %zu)\n",
87 sz, sizeof(ctx.skel->bss->dst));
93 ctx.skel = crypto_bench__open();
95 fprintf(stderr, "failed to open skeleton\n");
99 snprintf(ctx.skel->bss->cipher, 128, "%s", args.crypto_cipher);
100 memcpy(ctx.skel->bss->key, "12345678testtest", 16);
101 ctx.skel->bss->key_len = 16;
102 ctx.skel->bss->authsize = 0;
106 for (i = 0; i < sz - 1; i++)
107 input[i] = '1' + random() % 9;
108 input[sz - 1] = '\0';
110 ctx.skel->rodata->len = args.crypto_len;
112 err = crypto_bench__load(ctx.skel);
114 fprintf(stderr, "failed to load skeleton\n");
115 crypto_bench__destroy(ctx.skel);
119 pfd = bpf_program__fd(ctx.skel->progs.crypto_setup);
121 fprintf(stderr, "failed to get fd for setup prog\n");
122 crypto_bench__destroy(ctx.skel);
126 err = bpf_prog_test_run_opts(pfd, &opts);
127 if (err || ctx.skel->bss->status) {
128 fprintf(stderr, "failed to run setup prog: err %d, status %d\n",
129 err, ctx.skel->bss->status);
130 crypto_bench__destroy(ctx.skel);
135 static void crypto_encrypt_setup(void)
138 ctx.pfd = bpf_program__fd(ctx.skel->progs.crypto_encrypt);
141 static void crypto_decrypt_setup(void)
144 ctx.pfd = bpf_program__fd(ctx.skel->progs.crypto_decrypt);
147 static void crypto_measure(struct bench_res *res)
149 res->hits = atomic_swap(&ctx.skel->bss->hits, 0);
152 static void *crypto_producer(void *unused)
154 LIBBPF_OPTS(bpf_test_run_opts, opts,
157 .data_size_in = args.crypto_len,
161 (void)bpf_prog_test_run_opts(ctx.pfd, &opts);
165 const struct bench bench_crypto_encrypt = {
166 .name = "crypto-encrypt",
167 .argp = &bench_crypto_argp,
168 .validate = crypto_validate,
169 .setup = crypto_encrypt_setup,
170 .producer_thread = crypto_producer,
171 .measure = crypto_measure,
172 .report_progress = hits_drops_report_progress,
173 .report_final = hits_drops_report_final,
176 const struct bench bench_crypto_decrypt = {
177 .name = "crypto-decrypt",
178 .argp = &bench_crypto_argp,
179 .validate = crypto_validate,
180 .setup = crypto_decrypt_setup,
181 .producer_thread = crypto_producer,
182 .measure = crypto_measure,
183 .report_progress = hits_drops_report_progress,
184 .report_final = hits_drops_report_final,