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