1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 5 #include <stdlib.h> 6 7 #include <rte_malloc.h> 8 #include <rte_cycles.h> 9 #include <rte_crypto.h> 10 #include <rte_cryptodev.h> 11 12 #include "cperf_test_throughput.h" 13 #include "cperf_ops.h" 14 #include "cperf_test_common.h" 15 16 struct cperf_throughput_ctx { 17 uint8_t dev_id; 18 uint16_t qp_id; 19 uint8_t lcore_id; 20 21 struct rte_mempool *pool; 22 23 void *sess; 24 uint8_t sess_owner; 25 26 cperf_populate_ops_t populate_ops; 27 28 uint32_t src_buf_offset; 29 uint32_t dst_buf_offset; 30 31 const struct cperf_options *options; 32 const struct cperf_test_vector *test_vector; 33 }; 34 35 static void 36 cperf_throughput_test_free(struct cperf_throughput_ctx *ctx) 37 { 38 if (!ctx) 39 return; 40 if (ctx->sess != NULL && ctx->sess_owner) { 41 if (cperf_is_asym_test(ctx->options)) 42 rte_cryptodev_asym_session_free(ctx->dev_id, 43 (void *)ctx->sess); 44 #ifdef RTE_LIB_SECURITY 45 else if (ctx->options->op_type == CPERF_PDCP || 46 ctx->options->op_type == CPERF_DOCSIS || 47 ctx->options->op_type == CPERF_TLS || 48 ctx->options->op_type == CPERF_IPSEC) { 49 void *sec_ctx = rte_cryptodev_get_sec_ctx(ctx->dev_id); 50 51 rte_security_session_destroy(sec_ctx, (void *)ctx->sess); 52 } 53 #endif 54 else 55 rte_cryptodev_sym_session_free(ctx->dev_id, ctx->sess); 56 } 57 rte_mempool_free(ctx->pool); 58 59 rte_free(ctx); 60 } 61 62 void * 63 cperf_throughput_test_constructor(struct rte_mempool *sess_mp, 64 uint8_t dev_id, uint16_t qp_id, 65 const struct cperf_options *options, 66 const struct cperf_test_vector *test_vector, 67 const struct cperf_op_fns *op_fns, 68 void **sess) 69 { 70 struct cperf_throughput_ctx *ctx = NULL; 71 72 ctx = rte_malloc(NULL, sizeof(struct cperf_throughput_ctx), 0); 73 if (ctx == NULL) 74 goto err; 75 76 ctx->dev_id = dev_id; 77 ctx->qp_id = qp_id; 78 79 ctx->populate_ops = op_fns->populate_ops; 80 ctx->options = options; 81 ctx->test_vector = test_vector; 82 83 /* IV goes at the end of the crypto operation */ 84 uint16_t iv_offset = sizeof(struct rte_crypto_op) + 85 sizeof(struct rte_crypto_sym_op); 86 87 if (*sess != NULL) { 88 ctx->sess = *sess; 89 ctx->sess_owner = false; 90 } else { 91 ctx->sess = op_fns->sess_create(sess_mp, dev_id, options, test_vector, 92 iv_offset); 93 if (ctx->sess == NULL) 94 goto err; 95 *sess = ctx->sess; 96 ctx->sess_owner = true; 97 } 98 99 if (cperf_alloc_common_memory(options, test_vector, dev_id, qp_id, 0, 100 &ctx->src_buf_offset, &ctx->dst_buf_offset, 101 &ctx->pool) < 0) 102 goto err; 103 104 return ctx; 105 err: 106 cperf_throughput_test_free(ctx); 107 108 return NULL; 109 } 110 111 static void 112 cperf_verify_init_ops(struct rte_mempool *mp __rte_unused, 113 void *opaque_arg, 114 void *obj, 115 __rte_unused unsigned int i) 116 { 117 uint16_t iv_offset = sizeof(struct rte_crypto_op) + 118 sizeof(struct rte_crypto_sym_op); 119 uint32_t imix_idx = 0; 120 struct cperf_throughput_ctx *ctx = opaque_arg; 121 struct rte_crypto_op *op = obj; 122 123 (ctx->populate_ops)(&op, ctx->src_buf_offset, 124 ctx->dst_buf_offset, 125 1, ctx->sess, ctx->options, 126 ctx->test_vector, iv_offset, &imix_idx, NULL); 127 128 cperf_mbuf_set(op->sym->m_src, ctx->options, ctx->test_vector); 129 } 130 131 int 132 cperf_throughput_test_runner(void *test_ctx) 133 { 134 struct cperf_throughput_ctx *ctx = test_ctx; 135 uint16_t test_burst_size; 136 uint8_t burst_size_idx = 0; 137 uint32_t imix_idx = 0; 138 139 static RTE_ATOMIC(uint16_t) display_once; 140 141 struct rte_crypto_op *ops[ctx->options->max_burst_size]; 142 struct rte_crypto_op *ops_processed[ctx->options->max_burst_size]; 143 uint64_t i; 144 145 uint32_t lcore = rte_lcore_id(); 146 147 #ifdef CPERF_LINEARIZATION_ENABLE 148 struct rte_cryptodev_info dev_info; 149 int linearize = 0; 150 151 /* Check if source mbufs require coalescing */ 152 if ((ctx->options->op_type != CPERF_ASYM_MODEX) && 153 (ctx->options->segment_sz < ctx->options->max_buffer_size)) { 154 rte_cryptodev_info_get(ctx->dev_id, &dev_info); 155 if ((dev_info.feature_flags & 156 RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER) == 0) 157 linearize = 1; 158 } 159 #endif /* CPERF_LINEARIZATION_ENABLE */ 160 161 ctx->lcore_id = lcore; 162 163 /* Warm up the host CPU before starting the test */ 164 for (i = 0; i < ctx->options->total_ops; i++) 165 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0); 166 167 /* Get first size from range or list */ 168 if (ctx->options->inc_burst_size != 0) 169 test_burst_size = ctx->options->min_burst_size; 170 else 171 test_burst_size = ctx->options->burst_size_list[0]; 172 173 uint16_t iv_offset = sizeof(struct rte_crypto_op) + 174 sizeof(struct rte_crypto_sym_op); 175 176 if (ctx->options->out_of_place) 177 rte_mempool_obj_iter(ctx->pool, cperf_verify_init_ops, (void *)ctx); 178 179 while (test_burst_size <= ctx->options->max_burst_size) { 180 uint64_t ops_enqd = 0, ops_enqd_total = 0, ops_enqd_failed = 0; 181 uint64_t ops_deqd = 0, ops_deqd_total = 0, ops_deqd_failed = 0; 182 183 uint64_t tsc_start, tsc_end, tsc_duration; 184 185 uint16_t ops_unused = 0; 186 187 tsc_start = rte_rdtsc_precise(); 188 189 while (ops_enqd_total < ctx->options->total_ops) { 190 191 uint16_t burst_size = ((ops_enqd_total + test_burst_size) 192 <= ctx->options->total_ops) ? 193 test_burst_size : 194 ctx->options->total_ops - 195 ops_enqd_total; 196 197 uint16_t ops_needed = burst_size - ops_unused; 198 199 /* Allocate objects containing crypto operations and mbufs */ 200 if (rte_mempool_get_bulk(ctx->pool, (void **)ops, 201 ops_needed) != 0) { 202 RTE_LOG(ERR, USER1, 203 "Failed to allocate more crypto operations " 204 "from the crypto operation pool.\n" 205 "Consider increasing the pool size " 206 "with --pool-sz\n"); 207 return -1; 208 } 209 210 /* Setup crypto op, attach mbuf etc */ 211 if (!ctx->options->out_of_place) 212 (ctx->populate_ops)(ops, ctx->src_buf_offset, 213 ctx->dst_buf_offset, 214 ops_needed, ctx->sess, 215 ctx->options, ctx->test_vector, 216 iv_offset, &imix_idx, &tsc_start); 217 218 /** 219 * When ops_needed is smaller than ops_enqd, the 220 * unused ops need to be moved to the front for 221 * next round use. 222 */ 223 if (unlikely(ops_enqd > ops_needed)) { 224 size_t nb_b_to_mov = ops_unused * sizeof( 225 struct rte_crypto_op *); 226 227 memmove(&ops[ops_needed], &ops[ops_enqd], 228 nb_b_to_mov); 229 } 230 231 #ifdef CPERF_LINEARIZATION_ENABLE 232 if (linearize) { 233 /* PMD doesn't support scatter-gather and source buffer 234 * is segmented. 235 * We need to linearize it before enqueuing. 236 */ 237 for (i = 0; i < burst_size; i++) 238 rte_pktmbuf_linearize( 239 ops[i]->sym->m_src); 240 } 241 #endif /* CPERF_LINEARIZATION_ENABLE */ 242 243 /* Enqueue burst of ops on crypto device */ 244 ops_enqd = rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, 245 ops, burst_size); 246 if (ops_enqd < burst_size) 247 ops_enqd_failed++; 248 249 /** 250 * Calculate number of ops not enqueued (mainly for hw 251 * accelerators whose ingress queue can fill up). 252 */ 253 ops_unused = burst_size - ops_enqd; 254 ops_enqd_total += ops_enqd; 255 256 257 /* Dequeue processed burst of ops from crypto device */ 258 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id, 259 ops_processed, test_burst_size); 260 261 if (likely(ops_deqd)) { 262 /* Free crypto ops so they can be reused. */ 263 rte_mempool_put_bulk(ctx->pool, 264 (void **)ops_processed, ops_deqd); 265 266 ops_deqd_total += ops_deqd; 267 } else { 268 /** 269 * Count dequeue polls which didn't return any 270 * processed operations. This statistic is mainly 271 * relevant to hw accelerators. 272 */ 273 ops_deqd_failed++; 274 } 275 276 } 277 278 /* Dequeue any operations still in the crypto device */ 279 280 while (ops_deqd_total < ctx->options->total_ops) { 281 /* Sending 0 length burst to flush sw crypto device */ 282 rte_cryptodev_enqueue_burst(ctx->dev_id, ctx->qp_id, NULL, 0); 283 284 /* dequeue burst */ 285 ops_deqd = rte_cryptodev_dequeue_burst(ctx->dev_id, ctx->qp_id, 286 ops_processed, test_burst_size); 287 if (ops_deqd == 0) 288 ops_deqd_failed++; 289 else { 290 rte_mempool_put_bulk(ctx->pool, 291 (void **)ops_processed, ops_deqd); 292 ops_deqd_total += ops_deqd; 293 } 294 } 295 296 tsc_end = rte_rdtsc_precise(); 297 tsc_duration = (tsc_end - tsc_start); 298 299 /* Calculate average operations processed per second */ 300 double ops_per_second = ((double)ctx->options->total_ops / 301 tsc_duration) * rte_get_tsc_hz(); 302 303 /* Calculate average throughput (Gbps) in bits per second */ 304 double throughput_gbps = ((ops_per_second * 305 ctx->options->test_buffer_size * 8) / 1000000000); 306 307 /* Calculate average cycles per packet */ 308 double cycles_per_packet = ((double)tsc_duration / 309 ctx->options->total_ops); 310 311 uint16_t exp = 0; 312 if (!ctx->options->csv) { 313 if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1, 314 rte_memory_order_relaxed, rte_memory_order_relaxed)) 315 printf("%12s%12s%12s%12s%12s%12s%12s%12s%12s%12s\n\n", 316 "lcore id", "Buf Size", "Burst Size", 317 "Enqueued", "Dequeued", "Failed Enq", 318 "Failed Deq", "MOps", "Gbps", 319 "Cycles/Buf"); 320 321 printf("%12u%12u%12u%12"PRIu64"%12"PRIu64"%12"PRIu64 322 "%12"PRIu64"%12.4f%12.4f%12.2f\n", 323 ctx->lcore_id, 324 ctx->options->test_buffer_size, 325 test_burst_size, 326 ops_enqd_total, 327 ops_deqd_total, 328 ops_enqd_failed, 329 ops_deqd_failed, 330 ops_per_second/1000000, 331 throughput_gbps, 332 cycles_per_packet); 333 } else { 334 if (rte_atomic_compare_exchange_strong_explicit(&display_once, &exp, 1, 335 rte_memory_order_relaxed, rte_memory_order_relaxed)) 336 printf("#lcore id,Buffer Size(B)," 337 "Burst Size,Enqueued,Dequeued,Failed Enq," 338 "Failed Deq,Ops(Millions),Throughput(Gbps)," 339 "Cycles/Buf\n\n"); 340 341 printf("%u,%u,%u,%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"," 342 "%.3f,%.3f,%.3f\n", 343 ctx->lcore_id, 344 ctx->options->test_buffer_size, 345 test_burst_size, 346 ops_enqd_total, 347 ops_deqd_total, 348 ops_enqd_failed, 349 ops_deqd_failed, 350 ops_per_second/1000000, 351 throughput_gbps, 352 cycles_per_packet); 353 } 354 355 /* Get next size from range or list */ 356 if (ctx->options->inc_burst_size != 0) 357 test_burst_size += ctx->options->inc_burst_size; 358 else { 359 if (++burst_size_idx == ctx->options->burst_size_count) 360 break; 361 test_burst_size = ctx->options->burst_size_list[burst_size_idx]; 362 } 363 364 } 365 366 return 0; 367 } 368 369 370 void 371 cperf_throughput_test_destructor(void *arg) 372 { 373 struct cperf_throughput_ctx *ctx = arg; 374 375 if (ctx == NULL) 376 return; 377 378 cperf_throughput_test_free(ctx); 379 } 380