4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <rte_cryptodev.h>
34 #include <rte_malloc.h>
36 #include "rte_cryptodev_scheduler_operations.h"
37 #include "scheduler_pmd_private.h"
39 #define DEF_PKT_SIZE_THRESHOLD (0xffffff80)
40 #define SLAVE_IDX_SWITCH_MASK (0x01)
41 #define PRIMARY_SLAVE_IDX 0
42 #define SECONDARY_SLAVE_IDX 1
43 #define NB_PKT_SIZE_SLAVES 2
45 /** pkt size based scheduler context */
46 struct psd_scheduler_ctx {
50 /** pkt size based scheduler queue pair context */
51 struct psd_scheduler_qp_ctx {
52 struct scheduler_slave primary_slave;
53 struct scheduler_slave secondary_slave;
56 } __rte_cache_aligned;
58 /** scheduling operation variables' wrapping */
59 struct psd_schedule_op {
65 schedule_enqueue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
67 struct scheduler_qp_ctx *qp_ctx = qp;
68 struct psd_scheduler_qp_ctx *psd_qp_ctx = qp_ctx->private_qp_ctx;
69 struct rte_crypto_op *sched_ops[NB_PKT_SIZE_SLAVES][nb_ops];
70 uint32_t in_flight_ops[NB_PKT_SIZE_SLAVES] = {
71 psd_qp_ctx->primary_slave.nb_inflight_cops,
72 psd_qp_ctx->secondary_slave.nb_inflight_cops
74 struct psd_schedule_op enq_ops[NB_PKT_SIZE_SLAVES] = {
75 {PRIMARY_SLAVE_IDX, 0}, {SECONDARY_SLAVE_IDX, 0}
77 struct psd_schedule_op *p_enq_op;
78 uint16_t i, processed_ops_pri = 0, processed_ops_sec = 0;
81 if (unlikely(nb_ops == 0))
84 for (i = 0; i < nb_ops && i < 4; i++) {
85 rte_prefetch0(ops[i]->sym);
86 rte_prefetch0(ops[i]->sym->session);
89 for (i = 0; (i < (nb_ops - 8)) && (nb_ops > 8); i += 4) {
90 rte_prefetch0(ops[i + 4]->sym);
91 rte_prefetch0(ops[i + 4]->sym->session);
92 rte_prefetch0(ops[i + 5]->sym);
93 rte_prefetch0(ops[i + 5]->sym->session);
94 rte_prefetch0(ops[i + 6]->sym);
95 rte_prefetch0(ops[i + 6]->sym->session);
96 rte_prefetch0(ops[i + 7]->sym);
97 rte_prefetch0(ops[i + 7]->sym->session);
99 /* job_len is initialized as cipher data length, once
100 * it is 0, equals to auth data length
102 job_len = ops[i]->sym->cipher.data.length;
103 job_len += (ops[i]->sym->cipher.data.length == 0) *
104 ops[i]->sym->auth.data.length;
105 /* decide the target op based on the job length */
106 p_enq_op = &enq_ops[!(job_len & psd_qp_ctx->threshold)];
108 /* stop schedule cops before the queue is full, this shall
109 * prevent the failed enqueue
111 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
112 qp_ctx->max_nb_objs) {
117 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i];
120 job_len = ops[i+1]->sym->cipher.data.length;
121 job_len += (ops[i+1]->sym->cipher.data.length == 0) *
122 ops[i+1]->sym->auth.data.length;
123 p_enq_op = &enq_ops[!(job_len & psd_qp_ctx->threshold)];
125 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
126 qp_ctx->max_nb_objs) {
131 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+1];
134 job_len = ops[i+2]->sym->cipher.data.length;
135 job_len += (ops[i+2]->sym->cipher.data.length == 0) *
136 ops[i+2]->sym->auth.data.length;
137 p_enq_op = &enq_ops[!(job_len & psd_qp_ctx->threshold)];
139 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
140 qp_ctx->max_nb_objs) {
145 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+2];
148 job_len = ops[i+3]->sym->cipher.data.length;
149 job_len += (ops[i+3]->sym->cipher.data.length == 0) *
150 ops[i+3]->sym->auth.data.length;
151 p_enq_op = &enq_ops[!(job_len & psd_qp_ctx->threshold)];
153 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
154 qp_ctx->max_nb_objs) {
159 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+3];
163 for (; i < nb_ops; i++) {
164 job_len = ops[i]->sym->cipher.data.length;
165 job_len += (ops[i]->sym->cipher.data.length == 0) *
166 ops[i]->sym->auth.data.length;
167 p_enq_op = &enq_ops[!(job_len & psd_qp_ctx->threshold)];
169 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
170 qp_ctx->max_nb_objs) {
175 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i];
179 processed_ops_pri = rte_cryptodev_enqueue_burst(
180 psd_qp_ctx->primary_slave.dev_id,
181 psd_qp_ctx->primary_slave.qp_id,
182 sched_ops[PRIMARY_SLAVE_IDX],
183 enq_ops[PRIMARY_SLAVE_IDX].pos);
184 /* enqueue shall not fail as the slave queue is monitored */
185 RTE_ASSERT(processed_ops_pri == enq_ops[PRIMARY_SLAVE_IDX].pos);
187 psd_qp_ctx->primary_slave.nb_inflight_cops += processed_ops_pri;
189 processed_ops_sec = rte_cryptodev_enqueue_burst(
190 psd_qp_ctx->secondary_slave.dev_id,
191 psd_qp_ctx->secondary_slave.qp_id,
192 sched_ops[SECONDARY_SLAVE_IDX],
193 enq_ops[SECONDARY_SLAVE_IDX].pos);
194 RTE_ASSERT(processed_ops_sec == enq_ops[SECONDARY_SLAVE_IDX].pos);
196 psd_qp_ctx->secondary_slave.nb_inflight_cops += processed_ops_sec;
198 return processed_ops_pri + processed_ops_sec;
202 schedule_enqueue_ordering(void *qp, struct rte_crypto_op **ops,
205 struct rte_ring *order_ring =
206 ((struct scheduler_qp_ctx *)qp)->order_ring;
207 uint16_t nb_ops_to_enq = get_max_enqueue_order_count(order_ring,
209 uint16_t nb_ops_enqd = schedule_enqueue(qp, ops,
212 scheduler_order_insert(order_ring, ops, nb_ops_enqd);
218 schedule_dequeue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
220 struct psd_scheduler_qp_ctx *qp_ctx =
221 ((struct scheduler_qp_ctx *)qp)->private_qp_ctx;
222 struct scheduler_slave *slaves[NB_PKT_SIZE_SLAVES] = {
223 &qp_ctx->primary_slave, &qp_ctx->secondary_slave};
224 struct scheduler_slave *slave = slaves[qp_ctx->deq_idx];
225 uint16_t nb_deq_ops_pri = 0, nb_deq_ops_sec = 0;
227 if (slave->nb_inflight_cops) {
228 nb_deq_ops_pri = rte_cryptodev_dequeue_burst(slave->dev_id,
229 slave->qp_id, ops, nb_ops);
230 slave->nb_inflight_cops -= nb_deq_ops_pri;
233 qp_ctx->deq_idx = (~qp_ctx->deq_idx) & SLAVE_IDX_SWITCH_MASK;
235 if (nb_deq_ops_pri == nb_ops)
236 return nb_deq_ops_pri;
238 slave = slaves[qp_ctx->deq_idx];
240 if (slave->nb_inflight_cops) {
241 nb_deq_ops_sec = rte_cryptodev_dequeue_burst(slave->dev_id,
242 slave->qp_id, &ops[nb_deq_ops_pri],
243 nb_ops - nb_deq_ops_pri);
244 slave->nb_inflight_cops -= nb_deq_ops_sec;
246 if (!slave->nb_inflight_cops)
247 qp_ctx->deq_idx = (~qp_ctx->deq_idx) &
248 SLAVE_IDX_SWITCH_MASK;
251 return nb_deq_ops_pri + nb_deq_ops_sec;
255 schedule_dequeue_ordering(void *qp, struct rte_crypto_op **ops,
258 struct rte_ring *order_ring =
259 ((struct scheduler_qp_ctx *)qp)->order_ring;
261 schedule_dequeue(qp, ops, nb_ops);
263 return scheduler_order_drain(order_ring, ops, nb_ops);
267 slave_attach(__rte_unused struct rte_cryptodev *dev,
268 __rte_unused uint8_t slave_id)
274 slave_detach(__rte_unused struct rte_cryptodev *dev,
275 __rte_unused uint8_t slave_id)
281 scheduler_start(struct rte_cryptodev *dev)
283 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
284 struct psd_scheduler_ctx *psd_ctx = sched_ctx->private_ctx;
287 /* for packet size based scheduler, nb_slaves have to >= 2 */
288 if (sched_ctx->nb_slaves < NB_PKT_SIZE_SLAVES) {
289 CS_LOG_ERR("not enough slaves to start");
293 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
294 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
295 struct psd_scheduler_qp_ctx *ps_qp_ctx =
296 qp_ctx->private_qp_ctx;
298 ps_qp_ctx->primary_slave.dev_id =
299 sched_ctx->slaves[PRIMARY_SLAVE_IDX].dev_id;
300 ps_qp_ctx->primary_slave.qp_id = i;
301 ps_qp_ctx->primary_slave.nb_inflight_cops = 0;
303 ps_qp_ctx->secondary_slave.dev_id =
304 sched_ctx->slaves[SECONDARY_SLAVE_IDX].dev_id;
305 ps_qp_ctx->secondary_slave.qp_id = i;
306 ps_qp_ctx->secondary_slave.nb_inflight_cops = 0;
308 ps_qp_ctx->threshold = psd_ctx->threshold;
311 if (sched_ctx->reordering_enabled) {
312 dev->enqueue_burst = &schedule_enqueue_ordering;
313 dev->dequeue_burst = &schedule_dequeue_ordering;
315 dev->enqueue_burst = &schedule_enqueue;
316 dev->dequeue_burst = &schedule_dequeue;
323 scheduler_stop(struct rte_cryptodev *dev)
327 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
328 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
329 struct psd_scheduler_qp_ctx *ps_qp_ctx = qp_ctx->private_qp_ctx;
331 if (ps_qp_ctx->primary_slave.nb_inflight_cops +
332 ps_qp_ctx->secondary_slave.nb_inflight_cops) {
333 CS_LOG_ERR("Some crypto ops left in slave queue");
342 scheduler_config_qp(struct rte_cryptodev *dev, uint16_t qp_id)
344 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
345 struct psd_scheduler_qp_ctx *ps_qp_ctx;
347 ps_qp_ctx = rte_zmalloc_socket(NULL, sizeof(*ps_qp_ctx), 0,
350 CS_LOG_ERR("failed allocate memory for private queue pair");
354 qp_ctx->private_qp_ctx = (void *)ps_qp_ctx;
360 scheduler_create_private_ctx(struct rte_cryptodev *dev)
362 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
363 struct psd_scheduler_ctx *psd_ctx;
365 if (sched_ctx->private_ctx)
366 rte_free(sched_ctx->private_ctx);
368 psd_ctx = rte_zmalloc_socket(NULL, sizeof(struct psd_scheduler_ctx), 0,
371 CS_LOG_ERR("failed allocate memory");
375 psd_ctx->threshold = DEF_PKT_SIZE_THRESHOLD;
377 sched_ctx->private_ctx = (void *)psd_ctx;
382 scheduler_option_set(struct rte_cryptodev *dev, uint32_t option_type,
385 struct psd_scheduler_ctx *psd_ctx = ((struct scheduler_ctx *)
386 dev->data->dev_private)->private_ctx;
389 if ((enum rte_cryptodev_schedule_option_type)option_type !=
390 CDEV_SCHED_OPTION_THRESHOLD) {
391 CS_LOG_ERR("Option not supported");
395 threshold = ((struct rte_cryptodev_scheduler_threshold_option *)
397 if (!rte_is_power_of_2(threshold)) {
398 CS_LOG_ERR("Threshold is not power of 2");
402 psd_ctx->threshold = ~(threshold - 1);
408 scheduler_option_get(struct rte_cryptodev *dev, uint32_t option_type,
411 struct psd_scheduler_ctx *psd_ctx = ((struct scheduler_ctx *)
412 dev->data->dev_private)->private_ctx;
413 struct rte_cryptodev_scheduler_threshold_option *threshold_option;
415 if ((enum rte_cryptodev_schedule_option_type)option_type !=
416 CDEV_SCHED_OPTION_THRESHOLD) {
417 CS_LOG_ERR("Option not supported");
421 threshold_option = option;
422 threshold_option->threshold = (~psd_ctx->threshold) + 1;
427 struct rte_cryptodev_scheduler_ops scheduler_ps_ops = {
433 scheduler_create_private_ctx,
434 scheduler_option_set,
438 struct rte_cryptodev_scheduler psd_scheduler = {
439 .name = "packet-size-based-scheduler",
440 .description = "scheduler which will distribute crypto op "
441 "burst based on the packet size",
442 .mode = CDEV_SCHED_MODE_PKT_SIZE_DISTR,
443 .ops = &scheduler_ps_ops
446 struct rte_cryptodev_scheduler *pkt_size_based_distr_scheduler = &psd_scheduler;