1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
4 #include <rte_reorder.h>
5 #include <rte_cryptodev.h>
6 #include <rte_cryptodev_pmd.h>
7 #include <rte_malloc.h>
9 #include "rte_cryptodev_scheduler.h"
10 #include "scheduler_pmd_private.h"
12 int scheduler_logtype_driver;
14 /** update the scheduler pmd's capability with attaching device's
16 * For each device to be attached, the scheduler's capability should be
17 * the common capability set of all slaves
20 sync_caps(struct rte_cryptodev_capabilities *caps,
22 const struct rte_cryptodev_capabilities *slave_caps)
24 uint32_t sync_nb_caps = nb_caps, nb_slave_caps = 0;
27 while (slave_caps[nb_slave_caps].op != RTE_CRYPTO_OP_TYPE_UNDEFINED)
31 rte_memcpy(caps, slave_caps, sizeof(*caps) * nb_slave_caps);
35 for (i = 0; i < sync_nb_caps; i++) {
36 struct rte_cryptodev_capabilities *cap = &caps[i];
39 for (j = 0; j < nb_slave_caps; j++) {
40 const struct rte_cryptodev_capabilities *s_cap =
43 if (s_cap->op != cap->op || s_cap->sym.xform_type !=
47 if (s_cap->sym.xform_type ==
48 RTE_CRYPTO_SYM_XFORM_AUTH) {
49 if (s_cap->sym.auth.algo !=
53 cap->sym.auth.digest_size.min =
54 s_cap->sym.auth.digest_size.min <
55 cap->sym.auth.digest_size.min ?
56 s_cap->sym.auth.digest_size.min :
57 cap->sym.auth.digest_size.min;
58 cap->sym.auth.digest_size.max =
59 s_cap->sym.auth.digest_size.max <
60 cap->sym.auth.digest_size.max ?
61 s_cap->sym.auth.digest_size.max :
62 cap->sym.auth.digest_size.max;
66 if (s_cap->sym.xform_type ==
67 RTE_CRYPTO_SYM_XFORM_CIPHER)
68 if (s_cap->sym.cipher.algo !=
72 /* no common cap found */
76 if (j < nb_slave_caps)
79 /* remove a uncommon cap from the array */
80 for (j = i; j < sync_nb_caps - 1; j++)
81 rte_memcpy(&caps[j], &caps[j+1], sizeof(*cap));
83 memset(&caps[sync_nb_caps - 1], 0, sizeof(*cap));
91 update_scheduler_capability(struct scheduler_ctx *sched_ctx)
93 struct rte_cryptodev_capabilities tmp_caps[256] = { {0} };
94 uint32_t nb_caps = 0, i;
96 if (sched_ctx->capabilities) {
97 rte_free(sched_ctx->capabilities);
98 sched_ctx->capabilities = NULL;
101 for (i = 0; i < sched_ctx->nb_slaves; i++) {
102 struct rte_cryptodev_info dev_info;
104 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
106 nb_caps = sync_caps(tmp_caps, nb_caps, dev_info.capabilities);
111 sched_ctx->capabilities = rte_zmalloc_socket(NULL,
112 sizeof(struct rte_cryptodev_capabilities) *
113 (nb_caps + 1), 0, SOCKET_ID_ANY);
114 if (!sched_ctx->capabilities)
117 rte_memcpy(sched_ctx->capabilities, tmp_caps,
118 sizeof(struct rte_cryptodev_capabilities) * nb_caps);
124 update_scheduler_feature_flag(struct rte_cryptodev *dev)
126 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
129 dev->feature_flags = 0;
131 for (i = 0; i < sched_ctx->nb_slaves; i++) {
132 struct rte_cryptodev_info dev_info;
134 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
136 dev->feature_flags |= dev_info.feature_flags;
141 update_max_nb_qp(struct scheduler_ctx *sched_ctx)
146 if (!sched_ctx->nb_slaves)
149 max_nb_qp = sched_ctx->nb_slaves ? UINT32_MAX : 0;
151 for (i = 0; i < sched_ctx->nb_slaves; i++) {
152 struct rte_cryptodev_info dev_info;
154 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
155 max_nb_qp = dev_info.max_nb_queue_pairs < max_nb_qp ?
156 dev_info.max_nb_queue_pairs : max_nb_qp;
159 sched_ctx->max_nb_queue_pairs = max_nb_qp;
162 /** Attach a device to the scheduler. */
164 rte_cryptodev_scheduler_slave_attach(uint8_t scheduler_id, uint8_t slave_id)
166 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
167 struct scheduler_ctx *sched_ctx;
168 struct scheduler_slave *slave;
169 struct rte_cryptodev_info dev_info;
173 CR_SCHED_LOG(ERR, "Operation not supported");
177 if (dev->driver_id != cryptodev_driver_id) {
178 CR_SCHED_LOG(ERR, "Operation not supported");
182 if (dev->data->dev_started) {
183 CR_SCHED_LOG(ERR, "Illegal operation");
187 sched_ctx = dev->data->dev_private;
188 if (sched_ctx->nb_slaves >=
189 RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES) {
190 CR_SCHED_LOG(ERR, "Too many slaves attached");
194 for (i = 0; i < sched_ctx->nb_slaves; i++)
195 if (sched_ctx->slaves[i].dev_id == slave_id) {
196 CR_SCHED_LOG(ERR, "Slave already added");
200 slave = &sched_ctx->slaves[sched_ctx->nb_slaves];
202 rte_cryptodev_info_get(slave_id, &dev_info);
204 slave->dev_id = slave_id;
205 slave->driver_id = dev_info.driver_id;
206 sched_ctx->nb_slaves++;
208 if (update_scheduler_capability(sched_ctx) < 0) {
210 slave->driver_id = 0;
211 sched_ctx->nb_slaves--;
213 CR_SCHED_LOG(ERR, "capabilities update failed");
217 update_scheduler_feature_flag(dev);
219 update_max_nb_qp(sched_ctx);
225 rte_cryptodev_scheduler_slave_detach(uint8_t scheduler_id, uint8_t slave_id)
227 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
228 struct scheduler_ctx *sched_ctx;
229 uint32_t i, slave_pos;
232 CR_SCHED_LOG(ERR, "Operation not supported");
236 if (dev->driver_id != cryptodev_driver_id) {
237 CR_SCHED_LOG(ERR, "Operation not supported");
241 if (dev->data->dev_started) {
242 CR_SCHED_LOG(ERR, "Illegal operation");
246 sched_ctx = dev->data->dev_private;
248 for (slave_pos = 0; slave_pos < sched_ctx->nb_slaves; slave_pos++)
249 if (sched_ctx->slaves[slave_pos].dev_id == slave_id)
251 if (slave_pos == sched_ctx->nb_slaves) {
252 CR_SCHED_LOG(ERR, "Cannot find slave");
256 if (sched_ctx->ops.slave_detach(dev, slave_id) < 0) {
257 CR_SCHED_LOG(ERR, "Failed to detach slave");
261 for (i = slave_pos; i < sched_ctx->nb_slaves - 1; i++) {
262 memcpy(&sched_ctx->slaves[i], &sched_ctx->slaves[i+1],
263 sizeof(struct scheduler_slave));
265 memset(&sched_ctx->slaves[sched_ctx->nb_slaves - 1], 0,
266 sizeof(struct scheduler_slave));
267 sched_ctx->nb_slaves--;
269 if (update_scheduler_capability(sched_ctx) < 0) {
270 CR_SCHED_LOG(ERR, "capabilities update failed");
274 update_scheduler_feature_flag(dev);
276 update_max_nb_qp(sched_ctx);
282 rte_cryptodev_scheduler_mode_set(uint8_t scheduler_id,
283 enum rte_cryptodev_scheduler_mode mode)
285 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
286 struct scheduler_ctx *sched_ctx;
289 CR_SCHED_LOG(ERR, "Operation not supported");
293 if (dev->driver_id != cryptodev_driver_id) {
294 CR_SCHED_LOG(ERR, "Operation not supported");
298 if (dev->data->dev_started) {
299 CR_SCHED_LOG(ERR, "Illegal operation");
303 sched_ctx = dev->data->dev_private;
305 if (mode == sched_ctx->mode)
309 case CDEV_SCHED_MODE_ROUNDROBIN:
310 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
311 roundrobin_scheduler) < 0) {
312 CR_SCHED_LOG(ERR, "Failed to load scheduler");
316 case CDEV_SCHED_MODE_PKT_SIZE_DISTR:
317 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
318 pkt_size_based_distr_scheduler) < 0) {
319 CR_SCHED_LOG(ERR, "Failed to load scheduler");
323 case CDEV_SCHED_MODE_FAILOVER:
324 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
325 failover_scheduler) < 0) {
326 CR_SCHED_LOG(ERR, "Failed to load scheduler");
330 case CDEV_SCHED_MODE_MULTICORE:
331 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
332 multicore_scheduler) < 0) {
333 CR_SCHED_LOG(ERR, "Failed to load scheduler");
338 CR_SCHED_LOG(ERR, "Not yet supported");
345 enum rte_cryptodev_scheduler_mode
346 rte_cryptodev_scheduler_mode_get(uint8_t scheduler_id)
348 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
349 struct scheduler_ctx *sched_ctx;
352 CR_SCHED_LOG(ERR, "Operation not supported");
356 if (dev->driver_id != cryptodev_driver_id) {
357 CR_SCHED_LOG(ERR, "Operation not supported");
361 sched_ctx = dev->data->dev_private;
363 return sched_ctx->mode;
367 rte_cryptodev_scheduler_ordering_set(uint8_t scheduler_id,
368 uint32_t enable_reorder)
370 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
371 struct scheduler_ctx *sched_ctx;
374 CR_SCHED_LOG(ERR, "Operation not supported");
378 if (dev->driver_id != cryptodev_driver_id) {
379 CR_SCHED_LOG(ERR, "Operation not supported");
383 if (dev->data->dev_started) {
384 CR_SCHED_LOG(ERR, "Illegal operation");
388 sched_ctx = dev->data->dev_private;
390 sched_ctx->reordering_enabled = enable_reorder;
396 rte_cryptodev_scheduler_ordering_get(uint8_t scheduler_id)
398 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
399 struct scheduler_ctx *sched_ctx;
402 CR_SCHED_LOG(ERR, "Operation not supported");
406 if (dev->driver_id != cryptodev_driver_id) {
407 CR_SCHED_LOG(ERR, "Operation not supported");
411 sched_ctx = dev->data->dev_private;
413 return (int)sched_ctx->reordering_enabled;
417 rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id,
418 struct rte_cryptodev_scheduler *scheduler) {
420 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
421 struct scheduler_ctx *sched_ctx;
424 CR_SCHED_LOG(ERR, "Operation not supported");
428 if (dev->driver_id != cryptodev_driver_id) {
429 CR_SCHED_LOG(ERR, "Operation not supported");
433 if (dev->data->dev_started) {
434 CR_SCHED_LOG(ERR, "Illegal operation");
438 sched_ctx = dev->data->dev_private;
440 if (strlen(scheduler->name) > RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
441 CR_SCHED_LOG(ERR, "Invalid name %s, should be less than "
442 "%u bytes.", scheduler->name,
443 RTE_CRYPTODEV_NAME_MAX_LEN);
446 snprintf(sched_ctx->name, sizeof(sched_ctx->name), "%s",
449 if (strlen(scheduler->description) >
450 RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN - 1) {
451 CR_SCHED_LOG(ERR, "Invalid description %s, should be less than "
452 "%u bytes.", scheduler->description,
453 RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN - 1);
456 snprintf(sched_ctx->description, sizeof(sched_ctx->description), "%s",
457 scheduler->description);
459 /* load scheduler instance operations functions */
460 sched_ctx->ops.config_queue_pair = scheduler->ops->config_queue_pair;
461 sched_ctx->ops.create_private_ctx = scheduler->ops->create_private_ctx;
462 sched_ctx->ops.scheduler_start = scheduler->ops->scheduler_start;
463 sched_ctx->ops.scheduler_stop = scheduler->ops->scheduler_stop;
464 sched_ctx->ops.slave_attach = scheduler->ops->slave_attach;
465 sched_ctx->ops.slave_detach = scheduler->ops->slave_detach;
466 sched_ctx->ops.option_set = scheduler->ops->option_set;
467 sched_ctx->ops.option_get = scheduler->ops->option_get;
469 if (sched_ctx->private_ctx) {
470 rte_free(sched_ctx->private_ctx);
471 sched_ctx->private_ctx = NULL;
474 if (sched_ctx->ops.create_private_ctx) {
475 int ret = (*sched_ctx->ops.create_private_ctx)(dev);
478 CR_SCHED_LOG(ERR, "Unable to create scheduler private "
484 sched_ctx->mode = scheduler->mode;
490 rte_cryptodev_scheduler_slaves_get(uint8_t scheduler_id, uint8_t *slaves)
492 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
493 struct scheduler_ctx *sched_ctx;
494 uint32_t nb_slaves = 0;
497 CR_SCHED_LOG(ERR, "Operation not supported");
501 if (dev->driver_id != cryptodev_driver_id) {
502 CR_SCHED_LOG(ERR, "Operation not supported");
506 sched_ctx = dev->data->dev_private;
508 nb_slaves = sched_ctx->nb_slaves;
510 if (slaves && nb_slaves) {
513 for (i = 0; i < nb_slaves; i++)
514 slaves[i] = sched_ctx->slaves[i].dev_id;
517 return (int)nb_slaves;
521 rte_cryptodev_scheduler_option_set(uint8_t scheduler_id,
522 enum rte_cryptodev_schedule_option_type option_type,
525 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
526 struct scheduler_ctx *sched_ctx;
528 if (option_type == CDEV_SCHED_OPTION_NOT_SET ||
529 option_type >= CDEV_SCHED_OPTION_COUNT) {
530 CR_SCHED_LOG(ERR, "Invalid option parameter");
535 CR_SCHED_LOG(ERR, "Invalid option parameter");
539 if (dev->data->dev_started) {
540 CR_SCHED_LOG(ERR, "Illegal operation");
544 sched_ctx = dev->data->dev_private;
546 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_set, -ENOTSUP);
548 return (*sched_ctx->ops.option_set)(dev, option_type, option);
552 rte_cryptodev_scheduler_option_get(uint8_t scheduler_id,
553 enum rte_cryptodev_schedule_option_type option_type,
556 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
557 struct scheduler_ctx *sched_ctx;
560 CR_SCHED_LOG(ERR, "Operation not supported");
565 CR_SCHED_LOG(ERR, "Invalid option parameter");
569 if (dev->driver_id != cryptodev_driver_id) {
570 CR_SCHED_LOG(ERR, "Operation not supported");
574 sched_ctx = dev->data->dev_private;
576 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_get, -ENOTSUP);
578 return (*sched_ctx->ops.option_get)(dev, option_type, option);
581 RTE_INIT(scheduler_init_log)
583 scheduler_logtype_driver = rte_log_register("pmd.crypto.scheduler");