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