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;
57 } __rte_cache_aligned;
59 /** scheduling operation variables' wrapping */
60 struct psd_schedule_op {
66 schedule_enqueue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
68 struct psd_scheduler_qp_ctx *qp_ctx =
69 ((struct scheduler_qp_ctx *)qp)->private_qp_ctx;
70 struct rte_crypto_op *sched_ops[NB_PKT_SIZE_SLAVES][nb_ops];
71 struct scheduler_session *sess;
72 uint32_t in_flight_ops[NB_PKT_SIZE_SLAVES] = {
73 qp_ctx->primary_slave.nb_inflight_cops,
74 qp_ctx->secondary_slave.nb_inflight_cops
76 struct psd_schedule_op enq_ops[NB_PKT_SIZE_SLAVES] = {
77 {PRIMARY_SLAVE_IDX, 0}, {SECONDARY_SLAVE_IDX, 0}
79 struct psd_schedule_op *p_enq_op;
80 uint16_t i, processed_ops_pri = 0, processed_ops_sec = 0;
83 if (unlikely(nb_ops == 0))
86 for (i = 0; i < nb_ops && i < 4; i++) {
87 rte_prefetch0(ops[i]->sym);
88 rte_prefetch0(ops[i]->sym->session);
91 for (i = 0; (i < (nb_ops - 8)) && (nb_ops > 8); i += 4) {
92 rte_prefetch0(ops[i + 4]->sym);
93 rte_prefetch0(ops[i + 4]->sym->session);
94 rte_prefetch0(ops[i + 5]->sym);
95 rte_prefetch0(ops[i + 5]->sym->session);
96 rte_prefetch0(ops[i + 6]->sym);
97 rte_prefetch0(ops[i + 6]->sym->session);
98 rte_prefetch0(ops[i + 7]->sym);
99 rte_prefetch0(ops[i + 7]->sym->session);
101 sess = (struct scheduler_session *)
102 ops[i]->sym->session->_private;
103 /* job_len is initialized as cipher data length, once
104 * it is 0, equals to auth data length
106 job_len = ops[i]->sym->cipher.data.length;
107 job_len += (ops[i]->sym->cipher.data.length == 0) *
108 ops[i]->sym->auth.data.length;
109 /* decide the target op based on the job length */
110 p_enq_op = &enq_ops[!(job_len & qp_ctx->threshold)];
112 /* stop schedule cops before the queue is full, this shall
113 * prevent the failed enqueue
115 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
116 qp_ctx->max_nb_objs) {
121 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i];
122 ops[i]->sym->session = sess->sessions[p_enq_op->slave_idx];
125 sess = (struct scheduler_session *)
126 ops[i+1]->sym->session->_private;
127 job_len = ops[i+1]->sym->cipher.data.length;
128 job_len += (ops[i+1]->sym->cipher.data.length == 0) *
129 ops[i+1]->sym->auth.data.length;
130 p_enq_op = &enq_ops[!(job_len & qp_ctx->threshold)];
132 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
133 qp_ctx->max_nb_objs) {
138 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+1];
139 ops[i+1]->sym->session = sess->sessions[p_enq_op->slave_idx];
142 sess = (struct scheduler_session *)
143 ops[i+2]->sym->session->_private;
144 job_len = ops[i+2]->sym->cipher.data.length;
145 job_len += (ops[i+2]->sym->cipher.data.length == 0) *
146 ops[i+2]->sym->auth.data.length;
147 p_enq_op = &enq_ops[!(job_len & qp_ctx->threshold)];
149 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
150 qp_ctx->max_nb_objs) {
155 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+2];
156 ops[i+2]->sym->session = sess->sessions[p_enq_op->slave_idx];
159 sess = (struct scheduler_session *)
160 ops[i+3]->sym->session->_private;
162 job_len = ops[i+3]->sym->cipher.data.length;
163 job_len += (ops[i+3]->sym->cipher.data.length == 0) *
164 ops[i+3]->sym->auth.data.length;
165 p_enq_op = &enq_ops[!(job_len & qp_ctx->threshold)];
167 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
168 qp_ctx->max_nb_objs) {
173 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i+3];
174 ops[i+3]->sym->session = sess->sessions[p_enq_op->slave_idx];
178 for (; i < nb_ops; i++) {
179 sess = (struct scheduler_session *)
180 ops[i]->sym->session->_private;
182 job_len = ops[i]->sym->cipher.data.length;
183 job_len += (ops[i]->sym->cipher.data.length == 0) *
184 ops[i]->sym->auth.data.length;
185 p_enq_op = &enq_ops[!(job_len & qp_ctx->threshold)];
187 if (p_enq_op->pos + in_flight_ops[p_enq_op->slave_idx] ==
188 qp_ctx->max_nb_objs) {
193 sched_ops[p_enq_op->slave_idx][p_enq_op->pos] = ops[i];
194 ops[i]->sym->session = sess->sessions[p_enq_op->slave_idx];
198 processed_ops_pri = rte_cryptodev_enqueue_burst(
199 qp_ctx->primary_slave.dev_id,
200 qp_ctx->primary_slave.qp_id,
201 sched_ops[PRIMARY_SLAVE_IDX],
202 enq_ops[PRIMARY_SLAVE_IDX].pos);
203 /* enqueue shall not fail as the slave queue is monitored */
204 RTE_ASSERT(processed_ops_pri == enq_ops[PRIMARY_SLAVE_IDX].pos);
206 qp_ctx->primary_slave.nb_inflight_cops += processed_ops_pri;
208 processed_ops_sec = rte_cryptodev_enqueue_burst(
209 qp_ctx->secondary_slave.dev_id,
210 qp_ctx->secondary_slave.qp_id,
211 sched_ops[SECONDARY_SLAVE_IDX],
212 enq_ops[SECONDARY_SLAVE_IDX].pos);
213 RTE_ASSERT(processed_ops_sec == enq_ops[SECONDARY_SLAVE_IDX].pos);
215 qp_ctx->secondary_slave.nb_inflight_cops += processed_ops_sec;
217 return processed_ops_pri + processed_ops_sec;
221 schedule_enqueue_ordering(void *qp, struct rte_crypto_op **ops,
224 struct rte_ring *order_ring =
225 ((struct scheduler_qp_ctx *)qp)->order_ring;
226 uint16_t nb_ops_to_enq = get_max_enqueue_order_count(order_ring,
228 uint16_t nb_ops_enqd = schedule_enqueue(qp, ops,
231 scheduler_order_insert(order_ring, ops, nb_ops_enqd);
237 schedule_dequeue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
239 struct psd_scheduler_qp_ctx *qp_ctx =
240 ((struct scheduler_qp_ctx *)qp)->private_qp_ctx;
241 struct scheduler_slave *slaves[NB_PKT_SIZE_SLAVES] = {
242 &qp_ctx->primary_slave, &qp_ctx->secondary_slave};
243 struct scheduler_slave *slave = slaves[qp_ctx->deq_idx];
244 uint16_t nb_deq_ops_pri = 0, nb_deq_ops_sec = 0;
246 if (slave->nb_inflight_cops) {
247 nb_deq_ops_pri = rte_cryptodev_dequeue_burst(slave->dev_id,
248 slave->qp_id, ops, nb_ops);
249 slave->nb_inflight_cops -= nb_deq_ops_pri;
252 qp_ctx->deq_idx = (~qp_ctx->deq_idx) & SLAVE_IDX_SWITCH_MASK;
254 if (nb_deq_ops_pri == nb_ops)
255 return nb_deq_ops_pri;
257 slave = slaves[qp_ctx->deq_idx];
259 if (slave->nb_inflight_cops) {
260 nb_deq_ops_sec = rte_cryptodev_dequeue_burst(slave->dev_id,
261 slave->qp_id, &ops[nb_deq_ops_pri],
262 nb_ops - nb_deq_ops_pri);
263 slave->nb_inflight_cops -= nb_deq_ops_sec;
265 if (!slave->nb_inflight_cops)
266 qp_ctx->deq_idx = (~qp_ctx->deq_idx) &
267 SLAVE_IDX_SWITCH_MASK;
270 return nb_deq_ops_pri + nb_deq_ops_sec;
274 schedule_dequeue_ordering(void *qp, struct rte_crypto_op **ops,
277 struct rte_ring *order_ring =
278 ((struct scheduler_qp_ctx *)qp)->order_ring;
280 schedule_dequeue(qp, ops, nb_ops);
282 return scheduler_order_drain(order_ring, ops, nb_ops);
286 slave_attach(__rte_unused struct rte_cryptodev *dev,
287 __rte_unused uint8_t slave_id)
293 slave_detach(__rte_unused struct rte_cryptodev *dev,
294 __rte_unused uint8_t slave_id)
300 scheduler_start(struct rte_cryptodev *dev)
302 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
303 struct psd_scheduler_ctx *psd_ctx = sched_ctx->private_ctx;
306 /* for packet size based scheduler, nb_slaves have to >= 2 */
307 if (sched_ctx->nb_slaves < NB_PKT_SIZE_SLAVES) {
308 CS_LOG_ERR("not enough slaves to start");
312 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
313 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
314 struct psd_scheduler_qp_ctx *ps_qp_ctx =
315 qp_ctx->private_qp_ctx;
317 ps_qp_ctx->primary_slave.dev_id =
318 sched_ctx->slaves[PRIMARY_SLAVE_IDX].dev_id;
319 ps_qp_ctx->primary_slave.qp_id = i;
320 ps_qp_ctx->primary_slave.nb_inflight_cops = 0;
322 ps_qp_ctx->secondary_slave.dev_id =
323 sched_ctx->slaves[SECONDARY_SLAVE_IDX].dev_id;
324 ps_qp_ctx->secondary_slave.qp_id = i;
325 ps_qp_ctx->secondary_slave.nb_inflight_cops = 0;
327 ps_qp_ctx->threshold = psd_ctx->threshold;
329 ps_qp_ctx->max_nb_objs = sched_ctx->qp_conf.nb_descriptors;
332 if (sched_ctx->reordering_enabled) {
333 dev->enqueue_burst = &schedule_enqueue_ordering;
334 dev->dequeue_burst = &schedule_dequeue_ordering;
336 dev->enqueue_burst = &schedule_enqueue;
337 dev->dequeue_burst = &schedule_dequeue;
344 scheduler_stop(struct rte_cryptodev *dev)
348 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
349 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
350 struct psd_scheduler_qp_ctx *ps_qp_ctx = qp_ctx->private_qp_ctx;
352 if (ps_qp_ctx->primary_slave.nb_inflight_cops +
353 ps_qp_ctx->secondary_slave.nb_inflight_cops) {
354 CS_LOG_ERR("Some crypto ops left in slave queue");
363 scheduler_config_qp(struct rte_cryptodev *dev, uint16_t qp_id)
365 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
366 struct psd_scheduler_qp_ctx *ps_qp_ctx;
368 ps_qp_ctx = rte_zmalloc_socket(NULL, sizeof(*ps_qp_ctx), 0,
371 CS_LOG_ERR("failed allocate memory for private queue pair");
375 qp_ctx->private_qp_ctx = (void *)ps_qp_ctx;
381 scheduler_create_private_ctx(struct rte_cryptodev *dev)
383 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
384 struct psd_scheduler_ctx *psd_ctx;
386 if (sched_ctx->private_ctx)
387 rte_free(sched_ctx->private_ctx);
389 psd_ctx = rte_zmalloc_socket(NULL, sizeof(struct psd_scheduler_ctx), 0,
392 CS_LOG_ERR("failed allocate memory");
396 psd_ctx->threshold = DEF_PKT_SIZE_THRESHOLD;
398 sched_ctx->private_ctx = (void *)psd_ctx;
403 scheduler_option_set(struct rte_cryptodev *dev, uint32_t option_type,
406 struct psd_scheduler_ctx *psd_ctx = ((struct scheduler_ctx *)
407 dev->data->dev_private)->private_ctx;
410 if ((enum rte_cryptodev_schedule_option_type)option_type !=
411 CDEV_SCHED_OPTION_THRESHOLD) {
412 CS_LOG_ERR("Option not supported");
416 threshold = ((struct rte_cryptodev_scheduler_threshold_option *)
418 if (!rte_is_power_of_2(threshold)) {
419 CS_LOG_ERR("Threshold is not power of 2");
423 psd_ctx->threshold = ~(threshold - 1);
429 scheduler_option_get(struct rte_cryptodev *dev, uint32_t option_type,
432 struct psd_scheduler_ctx *psd_ctx = ((struct scheduler_ctx *)
433 dev->data->dev_private)->private_ctx;
434 struct rte_cryptodev_scheduler_threshold_option *threshold_option;
436 if ((enum rte_cryptodev_schedule_option_type)option_type !=
437 CDEV_SCHED_OPTION_THRESHOLD) {
438 CS_LOG_ERR("Option not supported");
442 threshold_option = option;
443 threshold_option->threshold = (~psd_ctx->threshold) + 1;
448 struct rte_cryptodev_scheduler_ops scheduler_ps_ops = {
454 scheduler_create_private_ctx,
455 scheduler_option_set,
459 struct rte_cryptodev_scheduler psd_scheduler = {
460 .name = "packet-size-based-scheduler",
461 .description = "scheduler which will distribute crypto op "
462 "burst based on the packet size",
463 .mode = CDEV_SCHED_MODE_PKT_SIZE_DISTR,
464 .ops = &scheduler_ps_ops
467 struct rte_cryptodev_scheduler *pkt_size_based_distr_scheduler = &psd_scheduler;