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.
34 #include <rte_cryptodev.h>
35 #include <rte_malloc.h>
37 #include "rte_cryptodev_scheduler_operations.h"
38 #include "scheduler_pmd_private.h"
40 #define MC_SCHED_ENQ_RING_NAME_PREFIX "MCS_ENQR_"
41 #define MC_SCHED_DEQ_RING_NAME_PREFIX "MCS_DEQR_"
43 #define MC_SCHED_BUFFER_SIZE 32
45 #define CRYPTO_OP_STATUS_BIT_COMPLETE 0x80
47 /** multi-core scheduler context */
48 struct mc_scheduler_ctx {
49 uint32_t num_workers; /**< Number of workers polling */
52 struct rte_ring *sched_enq_ring[RTE_CRYPTODEV_SCHEDULER_MAX_NB_WORKER_CORES];
53 struct rte_ring *sched_deq_ring[RTE_CRYPTODEV_SCHEDULER_MAX_NB_WORKER_CORES];
56 struct mc_scheduler_qp_ctx {
57 struct scheduler_slave slaves[RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES];
60 uint32_t last_enq_worker_idx;
61 uint32_t last_deq_worker_idx;
63 struct mc_scheduler_ctx *mc_private_ctx;
67 schedule_enqueue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
69 struct mc_scheduler_qp_ctx *mc_qp_ctx =
70 ((struct scheduler_qp_ctx *)qp)->private_qp_ctx;
71 struct mc_scheduler_ctx *mc_ctx = mc_qp_ctx->mc_private_ctx;
72 uint32_t worker_idx = mc_qp_ctx->last_enq_worker_idx;
73 uint16_t i, processed_ops = 0;
75 if (unlikely(nb_ops == 0))
78 for (i = 0; i < mc_ctx->num_workers && nb_ops != 0; i++) {
79 struct rte_ring *enq_ring = mc_ctx->sched_enq_ring[worker_idx];
80 uint16_t nb_queue_ops = rte_ring_enqueue_burst(enq_ring,
81 (void *)(&ops[processed_ops]), nb_ops, NULL);
83 nb_ops -= nb_queue_ops;
84 processed_ops += nb_queue_ops;
86 if (++worker_idx == mc_ctx->num_workers)
89 mc_qp_ctx->last_enq_worker_idx = worker_idx;
95 schedule_enqueue_ordering(void *qp, struct rte_crypto_op **ops,
98 struct rte_ring *order_ring =
99 ((struct scheduler_qp_ctx *)qp)->order_ring;
100 uint16_t nb_ops_to_enq = get_max_enqueue_order_count(order_ring,
102 uint16_t nb_ops_enqd = schedule_enqueue(qp, ops,
105 scheduler_order_insert(order_ring, ops, nb_ops_enqd);
112 schedule_dequeue(void *qp, struct rte_crypto_op **ops, uint16_t nb_ops)
114 struct mc_scheduler_qp_ctx *mc_qp_ctx =
115 ((struct scheduler_qp_ctx *)qp)->private_qp_ctx;
116 struct mc_scheduler_ctx *mc_ctx = mc_qp_ctx->mc_private_ctx;
117 uint32_t worker_idx = mc_qp_ctx->last_deq_worker_idx;
118 uint16_t i, processed_ops = 0;
120 for (i = 0; i < mc_ctx->num_workers && nb_ops != 0; i++) {
121 struct rte_ring *deq_ring = mc_ctx->sched_deq_ring[worker_idx];
122 uint16_t nb_deq_ops = rte_ring_dequeue_burst(deq_ring,
123 (void *)(&ops[processed_ops]), nb_ops, NULL);
125 nb_ops -= nb_deq_ops;
126 processed_ops += nb_deq_ops;
127 if (++worker_idx == mc_ctx->num_workers)
131 mc_qp_ctx->last_deq_worker_idx = worker_idx;
133 return processed_ops;
138 schedule_dequeue_ordering(void *qp, struct rte_crypto_op **ops,
141 struct rte_ring *order_ring = ((struct scheduler_qp_ctx *)qp)->order_ring;
142 struct rte_crypto_op *op;
143 uint32_t nb_objs = rte_ring_count(order_ring);
144 uint32_t nb_ops_to_deq = 0;
145 uint32_t nb_ops_deqd = 0;
147 if (nb_objs > nb_ops)
150 while (nb_ops_to_deq < nb_objs) {
151 SCHEDULER_GET_RING_OBJ(order_ring, nb_ops_to_deq, op);
153 if (!(op->status & CRYPTO_OP_STATUS_BIT_COMPLETE))
156 op->status &= ~CRYPTO_OP_STATUS_BIT_COMPLETE;
161 nb_ops_deqd = rte_ring_sc_dequeue_bulk(order_ring,
162 (void **)ops, nb_ops_to_deq, NULL);
169 slave_attach(__rte_unused struct rte_cryptodev *dev,
170 __rte_unused uint8_t slave_id)
176 slave_detach(__rte_unused struct rte_cryptodev *dev,
177 __rte_unused uint8_t slave_id)
183 mc_scheduler_worker(struct rte_cryptodev *dev)
185 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
186 struct mc_scheduler_ctx *mc_ctx = sched_ctx->private_ctx;
187 struct rte_ring *enq_ring;
188 struct rte_ring *deq_ring;
189 uint32_t core_id = rte_lcore_id();
190 int i, worker_idx = -1;
191 struct scheduler_slave *slave;
192 struct rte_crypto_op *enq_ops[MC_SCHED_BUFFER_SIZE];
193 struct rte_crypto_op *deq_ops[MC_SCHED_BUFFER_SIZE];
194 uint16_t processed_ops;
195 uint16_t pending_enq_ops = 0;
196 uint16_t pending_enq_ops_idx = 0;
197 uint16_t pending_deq_ops = 0;
198 uint16_t pending_deq_ops_idx = 0;
199 uint16_t inflight_ops = 0;
200 const uint8_t reordering_enabled = sched_ctx->reordering_enabled;
202 for (i = 0; i < (int)sched_ctx->nb_wc; i++) {
203 if (sched_ctx->wc_pool[i] == core_id) {
208 if (worker_idx == -1) {
209 CS_LOG_ERR("worker on core %u:cannot find worker index!\n", core_id);
213 slave = &sched_ctx->slaves[worker_idx];
214 enq_ring = mc_ctx->sched_enq_ring[worker_idx];
215 deq_ring = mc_ctx->sched_deq_ring[worker_idx];
217 while (!mc_ctx->stop_signal) {
218 if (pending_enq_ops) {
220 rte_cryptodev_enqueue_burst(slave->dev_id,
221 slave->qp_id, &enq_ops[pending_enq_ops_idx],
223 pending_enq_ops -= processed_ops;
224 pending_enq_ops_idx += processed_ops;
225 inflight_ops += processed_ops;
227 processed_ops = rte_ring_dequeue_burst(enq_ring, (void *)enq_ops,
228 MC_SCHED_BUFFER_SIZE, NULL);
230 pending_enq_ops_idx = rte_cryptodev_enqueue_burst(
231 slave->dev_id, slave->qp_id,
232 enq_ops, processed_ops);
233 pending_enq_ops = processed_ops - pending_enq_ops_idx;
234 inflight_ops += pending_enq_ops_idx;
238 if (pending_deq_ops) {
239 processed_ops = rte_ring_enqueue_burst(
240 deq_ring, (void *)&deq_ops[pending_deq_ops_idx],
241 pending_deq_ops, NULL);
242 pending_deq_ops -= processed_ops;
243 pending_deq_ops_idx += processed_ops;
244 } else if (inflight_ops) {
245 processed_ops = rte_cryptodev_dequeue_burst(slave->dev_id,
246 slave->qp_id, deq_ops, MC_SCHED_BUFFER_SIZE);
248 inflight_ops -= processed_ops;
249 if (reordering_enabled) {
252 for (j = 0; j < processed_ops; j++) {
253 deq_ops[j]->status |=
254 CRYPTO_OP_STATUS_BIT_COMPLETE;
257 pending_deq_ops_idx = rte_ring_enqueue_burst(
258 deq_ring, (void *)deq_ops, processed_ops,
260 pending_deq_ops = processed_ops -
273 scheduler_start(struct rte_cryptodev *dev)
275 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
276 struct mc_scheduler_ctx *mc_ctx = sched_ctx->private_ctx;
279 mc_ctx->stop_signal = 0;
281 for (i = 0; i < sched_ctx->nb_wc; i++)
282 rte_eal_remote_launch(
283 (lcore_function_t *)mc_scheduler_worker, dev,
284 sched_ctx->wc_pool[i]);
286 if (sched_ctx->reordering_enabled) {
287 dev->enqueue_burst = &schedule_enqueue_ordering;
288 dev->dequeue_burst = &schedule_dequeue_ordering;
290 dev->enqueue_burst = &schedule_enqueue;
291 dev->dequeue_burst = &schedule_dequeue;
294 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
295 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[i];
296 struct mc_scheduler_qp_ctx *mc_qp_ctx =
297 qp_ctx->private_qp_ctx;
300 memset(mc_qp_ctx->slaves, 0,
301 RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES *
302 sizeof(struct scheduler_slave));
303 for (j = 0; j < sched_ctx->nb_slaves; j++) {
304 mc_qp_ctx->slaves[j].dev_id =
305 sched_ctx->slaves[j].dev_id;
306 mc_qp_ctx->slaves[j].qp_id = i;
309 mc_qp_ctx->nb_slaves = sched_ctx->nb_slaves;
311 mc_qp_ctx->last_enq_worker_idx = 0;
312 mc_qp_ctx->last_deq_worker_idx = 0;
319 scheduler_stop(struct rte_cryptodev *dev)
321 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
322 struct mc_scheduler_ctx *mc_ctx = sched_ctx->private_ctx;
325 mc_ctx->stop_signal = 1;
327 for (i = 0; i < sched_ctx->nb_wc; i++)
328 rte_eal_wait_lcore(sched_ctx->wc_pool[i]);
334 scheduler_config_qp(struct rte_cryptodev *dev, uint16_t qp_id)
336 struct scheduler_qp_ctx *qp_ctx = dev->data->queue_pairs[qp_id];
337 struct mc_scheduler_qp_ctx *mc_qp_ctx;
338 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
339 struct mc_scheduler_ctx *mc_ctx = sched_ctx->private_ctx;
341 mc_qp_ctx = rte_zmalloc_socket(NULL, sizeof(*mc_qp_ctx), 0,
344 CS_LOG_ERR("failed allocate memory for private queue pair");
348 mc_qp_ctx->mc_private_ctx = mc_ctx;
349 qp_ctx->private_qp_ctx = (void *)mc_qp_ctx;
356 scheduler_create_private_ctx(struct rte_cryptodev *dev)
358 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
359 struct mc_scheduler_ctx *mc_ctx;
362 if (sched_ctx->private_ctx)
363 rte_free(sched_ctx->private_ctx);
365 mc_ctx = rte_zmalloc_socket(NULL, sizeof(struct mc_scheduler_ctx), 0,
368 CS_LOG_ERR("failed allocate memory");
372 mc_ctx->num_workers = sched_ctx->nb_wc;
373 for (i = 0; i < sched_ctx->nb_wc; i++) {
376 snprintf(r_name, sizeof(r_name), MC_SCHED_ENQ_RING_NAME_PREFIX "%u", i);
377 mc_ctx->sched_enq_ring[i] = rte_ring_create(r_name, PER_SLAVE_BUFF_SIZE,
378 rte_socket_id(), RING_F_SC_DEQ | RING_F_SP_ENQ);
379 if (!mc_ctx->sched_enq_ring[i]) {
380 CS_LOG_ERR("Cannot create ring for worker %u", i);
383 snprintf(r_name, sizeof(r_name), MC_SCHED_DEQ_RING_NAME_PREFIX "%u", i);
384 mc_ctx->sched_deq_ring[i] = rte_ring_create(r_name, PER_SLAVE_BUFF_SIZE,
385 rte_socket_id(), RING_F_SC_DEQ | RING_F_SP_ENQ);
386 if (!mc_ctx->sched_deq_ring[i]) {
387 CS_LOG_ERR("Cannot create ring for worker %u", i);
392 sched_ctx->private_ctx = (void *)mc_ctx;
397 struct rte_cryptodev_scheduler_ops scheduler_mc_ops = {
403 scheduler_create_private_ctx,
404 NULL, /* option_set */
405 NULL /* option_get */
408 struct rte_cryptodev_scheduler mc_scheduler = {
409 .name = "multicore-scheduler",
410 .description = "scheduler which will run burst across multiple cpu cores",
411 .mode = CDEV_SCHED_MODE_MULTICORE,
412 .ops = &scheduler_mc_ops
415 struct rte_cryptodev_scheduler *multicore_scheduler = &mc_scheduler;