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.
32 #include <rte_reorder.h>
33 #include <rte_cryptodev.h>
34 #include <rte_cryptodev_pmd.h>
35 #include <rte_malloc.h>
37 #include "rte_cryptodev_scheduler.h"
38 #include "scheduler_pmd_private.h"
40 /** update the scheduler pmd's capability with attaching device's
42 * For each device to be attached, the scheduler's capability should be
43 * the common capability set of all slaves
46 sync_caps(struct rte_cryptodev_capabilities *caps,
48 const struct rte_cryptodev_capabilities *slave_caps)
50 uint32_t sync_nb_caps = nb_caps, nb_slave_caps = 0;
53 while (slave_caps[nb_slave_caps].op != RTE_CRYPTO_OP_TYPE_UNDEFINED)
57 rte_memcpy(caps, slave_caps, sizeof(*caps) * nb_slave_caps);
61 for (i = 0; i < sync_nb_caps; i++) {
62 struct rte_cryptodev_capabilities *cap = &caps[i];
65 for (j = 0; j < nb_slave_caps; j++) {
66 const struct rte_cryptodev_capabilities *s_cap =
69 if (s_cap->op != cap->op || s_cap->sym.xform_type !=
73 if (s_cap->sym.xform_type ==
74 RTE_CRYPTO_SYM_XFORM_AUTH) {
75 if (s_cap->sym.auth.algo !=
79 cap->sym.auth.digest_size.min =
80 s_cap->sym.auth.digest_size.min <
81 cap->sym.auth.digest_size.min ?
82 s_cap->sym.auth.digest_size.min :
83 cap->sym.auth.digest_size.min;
84 cap->sym.auth.digest_size.max =
85 s_cap->sym.auth.digest_size.max <
86 cap->sym.auth.digest_size.max ?
87 s_cap->sym.auth.digest_size.max :
88 cap->sym.auth.digest_size.max;
92 if (s_cap->sym.xform_type ==
93 RTE_CRYPTO_SYM_XFORM_CIPHER)
94 if (s_cap->sym.cipher.algo !=
98 /* no common cap found */
102 if (j < nb_slave_caps)
105 /* remove a uncommon cap from the array */
106 for (j = i; j < sync_nb_caps - 1; j++)
107 rte_memcpy(&caps[j], &caps[j+1], sizeof(*cap));
109 memset(&caps[sync_nb_caps - 1], 0, sizeof(*cap));
117 update_scheduler_capability(struct scheduler_ctx *sched_ctx)
119 struct rte_cryptodev_capabilities tmp_caps[256] = { {0} };
120 uint32_t nb_caps = 0, i;
122 if (sched_ctx->capabilities)
123 rte_free(sched_ctx->capabilities);
125 for (i = 0; i < sched_ctx->nb_slaves; i++) {
126 struct rte_cryptodev_info dev_info;
128 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
130 nb_caps = sync_caps(tmp_caps, nb_caps, dev_info.capabilities);
135 sched_ctx->capabilities = rte_zmalloc_socket(NULL,
136 sizeof(struct rte_cryptodev_capabilities) *
137 (nb_caps + 1), 0, SOCKET_ID_ANY);
138 if (!sched_ctx->capabilities)
141 rte_memcpy(sched_ctx->capabilities, tmp_caps,
142 sizeof(struct rte_cryptodev_capabilities) * nb_caps);
148 update_scheduler_feature_flag(struct rte_cryptodev *dev)
150 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
153 dev->feature_flags = 0;
155 for (i = 0; i < sched_ctx->nb_slaves; i++) {
156 struct rte_cryptodev_info dev_info;
158 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
160 dev->feature_flags |= dev_info.feature_flags;
165 update_max_nb_qp(struct scheduler_ctx *sched_ctx)
170 if (!sched_ctx->nb_slaves)
173 max_nb_qp = sched_ctx->nb_slaves ? UINT32_MAX : 0;
175 for (i = 0; i < sched_ctx->nb_slaves; i++) {
176 struct rte_cryptodev_info dev_info;
178 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
179 max_nb_qp = dev_info.max_nb_queue_pairs < max_nb_qp ?
180 dev_info.max_nb_queue_pairs : max_nb_qp;
183 sched_ctx->max_nb_queue_pairs = max_nb_qp;
186 /** Attach a device to the scheduler. */
188 rte_cryptodev_scheduler_slave_attach(uint8_t scheduler_id, uint8_t slave_id)
190 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
191 struct scheduler_ctx *sched_ctx;
192 struct scheduler_slave *slave;
193 struct rte_cryptodev_info dev_info;
197 CS_LOG_ERR("Operation not supported");
201 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
202 CS_LOG_ERR("Operation not supported");
206 if (dev->data->dev_started) {
207 CS_LOG_ERR("Illegal operation");
211 sched_ctx = dev->data->dev_private;
212 if (sched_ctx->nb_slaves >=
213 RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES) {
214 CS_LOG_ERR("Too many slaves attached");
218 for (i = 0; i < sched_ctx->nb_slaves; i++)
219 if (sched_ctx->slaves[i].dev_id == slave_id) {
220 CS_LOG_ERR("Slave already added");
224 slave = &sched_ctx->slaves[sched_ctx->nb_slaves];
226 rte_cryptodev_info_get(slave_id, &dev_info);
228 slave->dev_id = slave_id;
229 slave->dev_type = dev_info.dev_type;
230 sched_ctx->nb_slaves++;
232 if (update_scheduler_capability(sched_ctx) < 0) {
235 sched_ctx->nb_slaves--;
237 CS_LOG_ERR("capabilities update failed");
241 update_scheduler_feature_flag(dev);
243 update_max_nb_qp(sched_ctx);
249 rte_cryptodev_scheduler_slave_detach(uint8_t scheduler_id, uint8_t slave_id)
251 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
252 struct scheduler_ctx *sched_ctx;
253 uint32_t i, slave_pos;
256 CS_LOG_ERR("Operation not supported");
260 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
261 CS_LOG_ERR("Operation not supported");
265 if (dev->data->dev_started) {
266 CS_LOG_ERR("Illegal operation");
270 sched_ctx = dev->data->dev_private;
272 for (slave_pos = 0; slave_pos < sched_ctx->nb_slaves; slave_pos++)
273 if (sched_ctx->slaves[slave_pos].dev_id == slave_id)
275 if (slave_pos == sched_ctx->nb_slaves) {
276 CS_LOG_ERR("Cannot find slave");
280 if (sched_ctx->ops.slave_detach(dev, slave_id) < 0) {
281 CS_LOG_ERR("Failed to detach slave");
285 for (i = slave_pos; i < sched_ctx->nb_slaves - 1; i++) {
286 memcpy(&sched_ctx->slaves[i], &sched_ctx->slaves[i+1],
287 sizeof(struct scheduler_slave));
289 memset(&sched_ctx->slaves[sched_ctx->nb_slaves - 1], 0,
290 sizeof(struct scheduler_slave));
291 sched_ctx->nb_slaves--;
293 if (update_scheduler_capability(sched_ctx) < 0) {
294 CS_LOG_ERR("capabilities update failed");
298 update_scheduler_feature_flag(dev);
300 update_max_nb_qp(sched_ctx);
306 rte_cryptodev_scheduler_mode_set(uint8_t scheduler_id,
307 enum rte_cryptodev_scheduler_mode mode)
309 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
310 struct scheduler_ctx *sched_ctx;
313 CS_LOG_ERR("Operation not supported");
317 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
318 CS_LOG_ERR("Operation not supported");
322 if (dev->data->dev_started) {
323 CS_LOG_ERR("Illegal operation");
327 sched_ctx = dev->data->dev_private;
329 if (mode == sched_ctx->mode)
333 case CDEV_SCHED_MODE_ROUNDROBIN:
334 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
335 roundrobin_scheduler) < 0) {
336 CS_LOG_ERR("Failed to load scheduler");
340 case CDEV_SCHED_MODE_PKT_SIZE_DISTR:
341 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
342 pkt_size_based_distr_scheduler) < 0) {
343 CS_LOG_ERR("Failed to load scheduler");
347 case CDEV_SCHED_MODE_FAILOVER:
348 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
349 failover_scheduler) < 0) {
350 CS_LOG_ERR("Failed to load scheduler");
355 CS_LOG_ERR("Not yet supported");
363 rte_crpytodev_scheduler_mode_set(uint8_t scheduler_id,
364 enum rte_cryptodev_scheduler_mode mode)
366 return rte_cryptodev_scheduler_mode_set(scheduler_id, mode);
369 enum rte_cryptodev_scheduler_mode
370 rte_cryptodev_scheduler_mode_get(uint8_t scheduler_id)
372 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
373 struct scheduler_ctx *sched_ctx;
376 CS_LOG_ERR("Operation not supported");
380 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
381 CS_LOG_ERR("Operation not supported");
385 sched_ctx = dev->data->dev_private;
387 return sched_ctx->mode;
390 enum rte_cryptodev_scheduler_mode
391 rte_crpytodev_scheduler_mode_get(uint8_t scheduler_id)
393 return rte_cryptodev_scheduler_mode_get(scheduler_id);
397 rte_cryptodev_scheduler_ordering_set(uint8_t scheduler_id,
398 uint32_t enable_reorder)
400 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
401 struct scheduler_ctx *sched_ctx;
404 CS_LOG_ERR("Operation not supported");
408 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
409 CS_LOG_ERR("Operation not supported");
413 if (dev->data->dev_started) {
414 CS_LOG_ERR("Illegal operation");
418 sched_ctx = dev->data->dev_private;
420 sched_ctx->reordering_enabled = enable_reorder;
426 rte_cryptodev_scheduler_ordering_get(uint8_t scheduler_id)
428 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
429 struct scheduler_ctx *sched_ctx;
432 CS_LOG_ERR("Operation not supported");
436 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
437 CS_LOG_ERR("Operation not supported");
441 sched_ctx = dev->data->dev_private;
443 return (int)sched_ctx->reordering_enabled;
447 rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id,
448 struct rte_cryptodev_scheduler *scheduler) {
450 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
451 struct scheduler_ctx *sched_ctx;
454 CS_LOG_ERR("Operation not supported");
458 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
459 CS_LOG_ERR("Operation not supported");
463 if (dev->data->dev_started) {
464 CS_LOG_ERR("Illegal operation");
468 sched_ctx = dev->data->dev_private;
470 strncpy(sched_ctx->name, scheduler->name,
471 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
472 strncpy(sched_ctx->description, scheduler->description,
473 RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN);
475 /* load scheduler instance operations functions */
476 sched_ctx->ops.config_queue_pair = scheduler->ops->config_queue_pair;
477 sched_ctx->ops.create_private_ctx = scheduler->ops->create_private_ctx;
478 sched_ctx->ops.scheduler_start = scheduler->ops->scheduler_start;
479 sched_ctx->ops.scheduler_stop = scheduler->ops->scheduler_stop;
480 sched_ctx->ops.slave_attach = scheduler->ops->slave_attach;
481 sched_ctx->ops.slave_detach = scheduler->ops->slave_detach;
482 sched_ctx->ops.option_set = scheduler->ops->option_set;
483 sched_ctx->ops.option_get = scheduler->ops->option_get;
485 if (sched_ctx->private_ctx)
486 rte_free(sched_ctx->private_ctx);
488 if (sched_ctx->ops.create_private_ctx) {
489 int ret = (*sched_ctx->ops.create_private_ctx)(dev);
492 CS_LOG_ERR("Unable to create scheduler private "
498 sched_ctx->mode = scheduler->mode;
504 rte_cryptodev_scheduler_slaves_get(uint8_t scheduler_id, uint8_t *slaves)
506 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
507 struct scheduler_ctx *sched_ctx;
508 uint32_t nb_slaves = 0;
511 CS_LOG_ERR("Operation not supported");
515 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
516 CS_LOG_ERR("Operation not supported");
520 sched_ctx = dev->data->dev_private;
522 nb_slaves = sched_ctx->nb_slaves;
524 if (slaves && nb_slaves) {
527 for (i = 0; i < nb_slaves; i++)
528 slaves[i] = sched_ctx->slaves[i].dev_id;
531 return (int)nb_slaves;
535 rte_cryptodev_scheduler_option_set(uint8_t scheduler_id,
536 enum rte_cryptodev_schedule_option_type option_type,
539 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
540 struct scheduler_ctx *sched_ctx;
542 if (option_type == CDEV_SCHED_OPTION_NOT_SET ||
543 option_type >= CDEV_SCHED_OPTION_COUNT) {
544 CS_LOG_ERR("Invalid option parameter");
549 CS_LOG_ERR("Invalid option parameter");
553 if (dev->data->dev_started) {
554 CS_LOG_ERR("Illegal operation");
558 sched_ctx = dev->data->dev_private;
560 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_set, -ENOTSUP);
562 return (*sched_ctx->ops.option_set)(dev, option_type, option);
566 rte_cryptodev_scheduler_option_get(uint8_t scheduler_id,
567 enum rte_cryptodev_schedule_option_type option_type,
570 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
571 struct scheduler_ctx *sched_ctx;
574 CS_LOG_ERR("Operation not supported");
579 CS_LOG_ERR("Invalid option parameter");
583 if (dev->dev_type != RTE_CRYPTODEV_SCHEDULER_PMD) {
584 CS_LOG_ERR("Operation not supported");
588 sched_ctx = dev->data->dev_private;
590 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_get, -ENOTSUP);
592 return (*sched_ctx->ops.option_get)(dev, option_type, option);