Path: blob/master/tools/testing/selftests/bpf/benchs/bench_bpf_crypto.c
29270 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */23#include <argp.h>4#include "bench.h"5#include "crypto_bench.skel.h"67#define MAX_CIPHER_LEN 328static char *input;9static struct crypto_ctx {10struct crypto_bench *skel;11int pfd;12} ctx;1314static struct crypto_args {15u32 crypto_len;16char *crypto_cipher;17} args = {18.crypto_len = 16,19.crypto_cipher = "ecb(aes)",20};2122enum {23ARG_CRYPTO_LEN = 5000,24ARG_CRYPTO_CIPHER = 5001,25};2627static const struct argp_option opts[] = {28{ "crypto-len", ARG_CRYPTO_LEN, "CRYPTO_LEN", 0,29"Set the length of crypto buffer" },30{ "crypto-cipher", ARG_CRYPTO_CIPHER, "CRYPTO_CIPHER", 0,31"Set the cipher to use (default:ecb(aes))" },32{},33};3435static error_t crypto_parse_arg(int key, char *arg, struct argp_state *state)36{37switch (key) {38case ARG_CRYPTO_LEN:39args.crypto_len = strtoul(arg, NULL, 10);40if (!args.crypto_len ||41args.crypto_len > sizeof(ctx.skel->bss->dst)) {42fprintf(stderr, "Invalid crypto buffer len (limit %zu)\n",43sizeof(ctx.skel->bss->dst));44argp_usage(state);45}46break;47case ARG_CRYPTO_CIPHER:48args.crypto_cipher = strdup(arg);49if (!strlen(args.crypto_cipher) ||50strlen(args.crypto_cipher) > MAX_CIPHER_LEN) {51fprintf(stderr, "Invalid crypto cipher len (limit %d)\n",52MAX_CIPHER_LEN);53argp_usage(state);54}55break;56default:57return ARGP_ERR_UNKNOWN;58}5960return 0;61}6263const struct argp bench_crypto_argp = {64.options = opts,65.parser = crypto_parse_arg,66};6768static void crypto_validate(void)69{70if (env.consumer_cnt != 0) {71fprintf(stderr, "bpf crypto benchmark doesn't support consumer!\n");72exit(1);73}74}7576static void crypto_setup(void)77{78LIBBPF_OPTS(bpf_test_run_opts, opts);7980int err, pfd;81size_t i, sz;8283sz = args.crypto_len;84if (!sz || sz > sizeof(ctx.skel->bss->dst)) {85fprintf(stderr, "invalid encrypt buffer size (source %zu, target %zu)\n",86sz, sizeof(ctx.skel->bss->dst));87exit(1);88}8990setup_libbpf();9192ctx.skel = crypto_bench__open();93if (!ctx.skel) {94fprintf(stderr, "failed to open skeleton\n");95exit(1);96}9798snprintf(ctx.skel->bss->cipher, 128, "%s", args.crypto_cipher);99memcpy(ctx.skel->bss->key, "12345678testtest", 16);100ctx.skel->bss->key_len = 16;101ctx.skel->bss->authsize = 0;102103srandom(time(NULL));104input = malloc(sz);105for (i = 0; i < sz - 1; i++)106input[i] = '1' + random() % 9;107input[sz - 1] = '\0';108109ctx.skel->rodata->len = args.crypto_len;110111err = crypto_bench__load(ctx.skel);112if (err) {113fprintf(stderr, "failed to load skeleton\n");114crypto_bench__destroy(ctx.skel);115exit(1);116}117118pfd = bpf_program__fd(ctx.skel->progs.crypto_setup);119if (pfd < 0) {120fprintf(stderr, "failed to get fd for setup prog\n");121crypto_bench__destroy(ctx.skel);122exit(1);123}124125err = bpf_prog_test_run_opts(pfd, &opts);126if (err || ctx.skel->bss->status) {127fprintf(stderr, "failed to run setup prog: err %d, status %d\n",128err, ctx.skel->bss->status);129crypto_bench__destroy(ctx.skel);130exit(1);131}132}133134static void crypto_encrypt_setup(void)135{136crypto_setup();137ctx.pfd = bpf_program__fd(ctx.skel->progs.crypto_encrypt);138}139140static void crypto_decrypt_setup(void)141{142crypto_setup();143ctx.pfd = bpf_program__fd(ctx.skel->progs.crypto_decrypt);144}145146static void crypto_measure(struct bench_res *res)147{148res->hits = atomic_swap(&ctx.skel->bss->hits, 0);149}150151static void *crypto_producer(void *unused)152{153LIBBPF_OPTS(bpf_test_run_opts, opts,154.repeat = 64,155.data_in = input,156.data_size_in = args.crypto_len,157);158159while (true)160(void)bpf_prog_test_run_opts(ctx.pfd, &opts);161return NULL;162}163164const struct bench bench_crypto_encrypt = {165.name = "crypto-encrypt",166.argp = &bench_crypto_argp,167.validate = crypto_validate,168.setup = crypto_encrypt_setup,169.producer_thread = crypto_producer,170.measure = crypto_measure,171.report_progress = hits_drops_report_progress,172.report_final = hits_drops_report_final,173};174175const struct bench bench_crypto_decrypt = {176.name = "crypto-decrypt",177.argp = &bench_crypto_argp,178.validate = crypto_validate,179.setup = crypto_decrypt_setup,180.producer_thread = crypto_producer,181.measure = crypto_measure,182.report_progress = hits_drops_report_progress,183.report_final = hits_drops_report_final,184};185186187