1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #include <rte_malloc.h> 6 #include <rte_cycles.h> 7 #include <rte_crypto.h> 8 #include <rte_cryptodev.h> 9 10 #include "cperf_test_latency.h" 11 #include "cperf_ops.h" 12 #include "cperf_test_common.h" 13 14 struct cperf_op_result { 15 uint64_t tsc_start; 16 uint64_t tsc_end; 17 enum rte_crypto_op_status status; 18 }; 19 20 struct cperf_latency_ctx { 21 uint8_t dev_id; 22 uint16_t qp_id; 23 uint8_t lcore_id; 24 25 struct rte_mempool *pool; 26 27 void *sess; 28 29 cperf_populate_ops_t populate_ops; 30 31 uint32_t src_buf_offset; 32 uint32_t dst_buf_offset; 33 34 const struct cperf_options *options; 35 const struct cperf_test_vector *test_vector; 36 struct cperf_op_result *res; 37 }; 38 39 struct priv_op_data { 40 struct cperf_op_result *result; 41 }; 42 43 static void 44 cperf_latency_test_free(struct cperf_latency_ctx *ctx) 45 { 46 if (ctx == NULL) 47 return; 48 49 if (ctx->sess != NULL) { 50 if (ctx->options->op_type == CPERF_ASYM_MODEX) 51 rte_cryptodev_asym_session_free(ctx->dev_id, ctx->sess); 52 #ifdef RTE_LIB_SECURITY 53 else if (ctx->options->op_type == CPERF_PDCP || 54 ctx->options->op_type == CPERF_DOCSIS || 55 ctx->options->op_type == CPERF_TLS || 56 ctx->options->op_type == CPERF_IPSEC) { 57 void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id); 58 rte_security_session_destroy(sec_ctx, ctx->sess); 59 } 60 #endif 61 else 62 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess); 63 } 64 65 rte_mempool_free(ctx->pool); 66 rte_free(ctx->res); 67 rte_free(ctx); 68 } 69 70 void * 71 cperf_latency_test_constructor(struct rte_mempool *sess_mp, 72 uint8_t dev_id, uint16_t qp_id, 73 const struct cperf_options *options, 74 const struct cperf_test_vector *test_vector, 75 const struct cperf_op_fns *op_fns) 76 { 77 struct cperf_latency_ctx *ctx = NULL; 78 size_t extra_op_priv_size = sizeof(struct priv_op_data); 79 80 ctx = rte_malloc(NULL, sizeof(struct cperf_latency_ctx), 0); 81 if (ctx == NULL) 82 goto err; 83 84 ctx->dev_id = dev_id; 85 ctx->qp_id = qp_id; 86 87 ctx->populate_ops = op_fns->populate_ops; 88 ctx->options = options; 89 ctx->test_vector = test_vector; 90 91 /* IV goes at the end of the crypto operation */ 92 uint16_t iv_offset = sizeof(struct rte_crypto_op) + 93 sizeof(struct rte_crypto_sym_op) + 94 sizeof(struct cperf_op_result *); 95 96 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, 97 test_vector, iv_offset); 98 if (ctx->sess == NULL) 99 goto err; 100 101 if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 102 extra_op_priv_size, 103 &ctx->src_buf_offset, &ctx->dst_buf_offset, 104 &ctx->pool) < 0) 105 goto err; 106 107 ctx->res = rte_malloc(NULL, sizeof(struct cperf_op_result) * 108 ctx->options->total_ops, 0); 109 110 if (ctx->res == NULL) 111 goto err; 112 113 return ctx; 114 err: 115 cperf_latency_test_free(ctx); 116 117 return NULL; 118 } 119 120 static inline void 121 store_timestamp(struct rte_crypto_op *op, uint64_t timestamp) 122 { 123 struct priv_op_data *priv_data; 124 125 priv_data = (struct priv_op_data *) (op->sym + 1); 126 priv_data->result->status = op->status; 127 priv_data->result->tsc_end = timestamp; 128 } 129 130 int 131 cperf_latency_test_runner(void *arg) 132 { 133 struct cperf_latency_ctx *ctx = arg; 134 uint16_t test_burst_size; 135 uint8_t burst_size_idx = 0; 136 uint32_t imix_idx = 0; 137 int ret = 0; 138 139 static uint16_t display_once; 140 141 if (ctx == NULL) 142 return 0; 143 144 struct rte_crypto_op *ops[ctx->options->max_burst_size]; 145 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size]; 146 uint64_t i; 147 struct priv_op_data *priv_data; 148 149 uint32_t lcore = rte_lcore_id(); 150 151 #ifdef CPERF_LINEARIZATION_ENABLE 152 struct rte_cryptodev_info dev_info; 153 int linearize = 0; 154 155 /* Check if source mbufs require coalescing */ 156 if (ctx->options->segment_sz < ctx->options->max_buffer_size) { 157 rte_cryptodev_info_get(ctx->dev_id, &dev_info); 158 if ((dev_info.feature_flags & 159 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0) 160 linearize = 1; 161 } 162 #endif /* CPERF_LINEARIZATION_ENABLE */ 163 164 ctx->lcore_id = lcore; 165 166 /* Warm up the host CPU before starting the test */ 167 for (i = 0; i < ctx->options->total_ops; i++) 168 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0); 169 170 /* Get first size from range or list */ 171 if (ctx->options->inc_burst_size != 0) 172 test_burst_size = ctx->options->min_burst_size; 173 else 174 test_burst_size = ctx->options->burst_size_list[0]; 175 176 uint16_t iv_offset = sizeof(struct rte_crypto_op) + 177 sizeof(struct rte_crypto_sym_op) + 178 sizeof(struct cperf_op_result *); 179 180 while (test_burst_size <= ctx->options->max_burst_size) { 181 uint64_t ops_enqd = 0, ops_deqd = 0; 182 uint64_t b_idx = 0; 183 184 uint64_t tsc_val, tsc_end, tsc_start; 185 uint64_t tsc_max = 0, tsc_min = ~0UL, tsc_tot = 0, tsc_idx = 0; 186 uint64_t enqd_max = 0, enqd_min = ~0UL, enqd_tot = 0; 187 uint64_t deqd_max = 0, deqd_min = ~0UL, deqd_tot = 0; 188 189 while (enqd_tot < ctx->options->total_ops) { 190 191 uint16_t burst_size = ((enqd_tot + test_burst_size) 192 <= ctx->options->total_ops) ? 193 test_burst_size : 194 ctx->options->total_ops - 195 enqd_tot; 196 197 /* Allocate objects containing crypto operations and mbufs */ 198 if (rte_mempool_get_bulk(ctx->pool, (void **)ops, 199 burst_size) != 0) { 200 RTE_LOG(ERR, USER1, 201 "Failed to allocate more crypto operations " 202 "from the crypto operation pool.\n" 203 "Consider increasing the pool size " 204 "with --pool-sz\n"); 205 return -1; 206 } 207 208 /* Setup crypto op, attach mbuf etc */ 209 (ctx->populate_ops)(ops, ctx->src_buf_offset, 210 ctx->dst_buf_offset, 211 burst_size, ctx->sess, ctx->options, 212 ctx->test_vector, iv_offset, 213 &imix_idx, &tsc_start); 214 215 /* Populate the mbuf with the test vector */ 216 for (i = 0; i < burst_size; i++) 217 cperf_mbuf_set(ops[i]->sym->m_src, 218 ctx->options, 219 ctx->test_vector); 220 221 tsc_start = rte_rdtsc_precise(); 222 223 #ifdef CPERF_LINEARIZATION_ENABLE 224 if (linearize) { 225 /* PMD doesn't support scatter-gather and source buffer 226 * is segmented. 227 * We need to linearize it before enqueuing. 228 */ 229 for (i = 0; i < burst_size; i++) 230 rte_pktmbuf_linearize(ops[i]->sym->m_src); 231 } 232 #endif /* CPERF_LINEARIZATION_ENABLE */ 233 234 /* Enqueue burst of ops on crypto device */ 235 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, 236 ops, burst_size); 237 238 /* Dequeue processed burst of ops from crypto device */ 239 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id, 240 ops_processed, test_burst_size); 241 242 tsc_end = rte_rdtsc_precise(); 243 244 /* Free memory for not enqueued operations */ 245 if (ops_enqd != burst_size) 246 rte_mempool_put_bulk(ctx->pool, 247 (void **)&ops[ops_enqd], 248 burst_size - ops_enqd); 249 250 for (i = 0; i < ops_enqd; i++) { 251 ctx->res[tsc_idx].tsc_start = tsc_start; 252 /* 253 * Private data structure starts after the end of the 254 * rte_crypto_sym_op structure. 255 */ 256 priv_data = (struct priv_op_data *) (ops[i]->sym + 1); 257 priv_data->result = (void *)&ctx->res[tsc_idx]; 258 tsc_idx++; 259 } 260 261 if (likely(ops_deqd)) { 262 for (i = 0; i < ops_deqd; i++) { 263 struct rte_crypto_op *op = ops_processed[i]; 264 265 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) 266 ret = -1; 267 268 store_timestamp(ops_processed[i], tsc_end); 269 } 270 271 /* Free crypto ops so they can be reused. */ 272 rte_mempool_put_bulk(ctx->pool, 273 (void **)ops_processed, ops_deqd); 274 275 deqd_tot += ops_deqd; 276 deqd_max = RTE_MAX(ops_deqd, deqd_max); 277 deqd_min = RTE_MIN(ops_deqd, deqd_min); 278 } 279 280 enqd_tot += ops_enqd; 281 enqd_max = RTE_MAX(ops_enqd, enqd_max); 282 enqd_min = RTE_MIN(ops_enqd, enqd_min); 283 284 b_idx++; 285 } 286 287 /* Dequeue any operations still in the crypto device */ 288 while (deqd_tot < ctx->options->total_ops) { 289 /* Sending 0 length burst to flush sw crypto device */ 290 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0); 291 292 /* dequeue burst */ 293 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id, 294 ops_processed, test_burst_size); 295 296 tsc_end = rte_rdtsc_precise(); 297 298 if (ops_deqd != 0) { 299 for (i = 0; i < ops_deqd; i++) { 300 struct rte_crypto_op *op = ops_processed[i]; 301 302 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) 303 ret = -1; 304 305 store_timestamp(ops_processed[i], tsc_end); 306 } 307 308 rte_mempool_put_bulk(ctx->pool, 309 (void **)ops_processed, ops_deqd); 310 311 deqd_tot += ops_deqd; 312 deqd_max = RTE_MAX(ops_deqd, deqd_max); 313 deqd_min = RTE_MIN(ops_deqd, deqd_min); 314 } 315 } 316 317 /* If there was any failure in crypto op, exit */ 318 if (ret) 319 return ret; 320 321 for (i = 0; i < tsc_idx; i++) { 322 tsc_val = ctx->res[i].tsc_end - ctx->res[i].tsc_start; 323 tsc_max = RTE_MAX(tsc_val, tsc_max); 324 tsc_min = RTE_MIN(tsc_val, tsc_min); 325 tsc_tot += tsc_val; 326 } 327 328 double time_tot, time_avg, time_max, time_min; 329 330 const uint64_t tunit = 1000000; /* us */ 331 const uint64_t tsc_hz = rte_get_tsc_hz(); 332 333 uint64_t enqd_avg = enqd_tot / b_idx; 334 uint64_t deqd_avg = deqd_tot / b_idx; 335 uint64_t tsc_avg = tsc_tot / tsc_idx; 336 337 time_tot = tunit*(double)(tsc_tot) / tsc_hz; 338 time_avg = tunit*(double)(tsc_avg) / tsc_hz; 339 time_max = tunit*(double)(tsc_max) / tsc_hz; 340 time_min = tunit*(double)(tsc_min) / tsc_hz; 341 342 uint16_t exp = 0; 343 if (ctx->options->csv) { 344 if (__atomic_compare_exchange_n(&display_once, &exp, 1, 0, 345 __ATOMIC_RELAXED, __ATOMIC_RELAXED)) 346 printf("\n# lcore, Buffer Size, Burst Size, Pakt Seq #, " 347 "cycles, time (us)"); 348 349 for (i = 0; i < ctx->options->total_ops; i++) { 350 351 printf("\n%u,%u,%u,%"PRIu64",%"PRIu64",%.3f", 352 ctx->lcore_id, ctx->options->test_buffer_size, 353 test_burst_size, i + 1, 354 ctx->res[i].tsc_end - ctx->res[i].tsc_start, 355 tunit * (double) (ctx->res[i].tsc_end 356 - ctx->res[i].tsc_start) 357 / tsc_hz); 358 359 } 360 } else { 361 printf("\n# Device %d on lcore %u\n", ctx->dev_id, 362 ctx->lcore_id); 363 printf("\n# total operations: %u", ctx->options->total_ops); 364 printf("\n# Buffer size: %u", ctx->options->test_buffer_size); 365 printf("\n# Burst size: %u", test_burst_size); 366 printf("\n# Number of bursts: %"PRIu64, 367 b_idx); 368 369 printf("\n#"); 370 printf("\n# \t Total\t Average\t " 371 "Maximum\t Minimum"); 372 printf("\n# enqueued\t%12"PRIu64"\t%10"PRIu64"\t" 373 "%10"PRIu64"\t%10"PRIu64, enqd_tot, 374 enqd_avg, enqd_max, enqd_min); 375 printf("\n# dequeued\t%12"PRIu64"\t%10"PRIu64"\t" 376 "%10"PRIu64"\t%10"PRIu64, deqd_tot, 377 deqd_avg, deqd_max, deqd_min); 378 printf("\n# cycles\t%12"PRIu64"\t%10"PRIu64"\t" 379 "%10"PRIu64"\t%10"PRIu64, tsc_tot, 380 tsc_avg, tsc_max, tsc_min); 381 printf("\n# time [us]\t%12.0f\t%10.3f\t%10.3f\t%10.3f", 382 time_tot, time_avg, time_max, time_min); 383 printf("\n\n"); 384 385 } 386 387 /* Get next size from range or list */ 388 if (ctx->options->inc_burst_size != 0) 389 test_burst_size += ctx->options->inc_burst_size; 390 else { 391 if (++burst_size_idx == ctx->options->burst_size_count) 392 break; 393 test_burst_size = 394 ctx->options->burst_size_list[burst_size_idx]; 395 } 396 } 397 398 return 0; 399 } 400 401 void 402 cperf_latency_test_destructor(void *arg) 403 { 404 struct cperf_latency_ctx *ctx = arg; 405 406 if (ctx == NULL) 407 return; 408 409 cperf_latency_test_free(ctx); 410 } 411