1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
4 #include <rte_string_fns.h>
5 #include <rte_reorder.h>
6 #include <rte_cryptodev.h>
7 #include <rte_cryptodev_pmd.h>
8 #include <rte_malloc.h>
10 #include "rte_cryptodev_scheduler.h"
11 #include "scheduler_pmd_private.h"
13 int scheduler_logtype_driver;
15 /** update the scheduler pmd's capability with attaching device's
17 * For each device to be attached, the scheduler's capability should be
18 * the common capability set of all slaves
21 sync_caps(struct rte_cryptodev_capabilities *caps,
23 const struct rte_cryptodev_capabilities *slave_caps)
25 uint32_t sync_nb_caps = nb_caps, nb_slave_caps = 0;
28 while (slave_caps[nb_slave_caps].op != RTE_CRYPTO_OP_TYPE_UNDEFINED)
32 rte_memcpy(caps, slave_caps, sizeof(*caps) * nb_slave_caps);
36 for (i = 0; i < sync_nb_caps; i++) {
37 struct rte_cryptodev_capabilities *cap = &caps[i];
40 for (j = 0; j < nb_slave_caps; j++) {
41 const struct rte_cryptodev_capabilities *s_cap =
44 if (s_cap->op != cap->op || s_cap->sym.xform_type !=
48 if (s_cap->sym.xform_type ==
49 RTE_CRYPTO_SYM_XFORM_AUTH) {
50 if (s_cap->sym.auth.algo !=
54 cap->sym.auth.digest_size.min =
55 s_cap->sym.auth.digest_size.min <
56 cap->sym.auth.digest_size.min ?
57 s_cap->sym.auth.digest_size.min :
58 cap->sym.auth.digest_size.min;
59 cap->sym.auth.digest_size.max =
60 s_cap->sym.auth.digest_size.max <
61 cap->sym.auth.digest_size.max ?
62 s_cap->sym.auth.digest_size.max :
63 cap->sym.auth.digest_size.max;
67 if (s_cap->sym.xform_type ==
68 RTE_CRYPTO_SYM_XFORM_CIPHER)
69 if (s_cap->sym.cipher.algo !=
73 /* no common cap found */
77 if (j < nb_slave_caps)
80 /* remove a uncommon cap from the array */
81 for (j = i; j < sync_nb_caps - 1; j++)
82 rte_memcpy(&caps[j], &caps[j+1], sizeof(*cap));
84 memset(&caps[sync_nb_caps - 1], 0, sizeof(*cap));
92 update_scheduler_capability(struct scheduler_ctx *sched_ctx)
94 struct rte_cryptodev_capabilities tmp_caps[256] = { {0} };
95 uint32_t nb_caps = 0, i;
97 if (sched_ctx->capabilities) {
98 rte_free(sched_ctx->capabilities);
99 sched_ctx->capabilities = NULL;
102 for (i = 0; i < sched_ctx->nb_slaves; i++) {
103 struct rte_cryptodev_info dev_info;
105 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
107 nb_caps = sync_caps(tmp_caps, nb_caps, dev_info.capabilities);
112 sched_ctx->capabilities = rte_zmalloc_socket(NULL,
113 sizeof(struct rte_cryptodev_capabilities) *
114 (nb_caps + 1), 0, SOCKET_ID_ANY);
115 if (!sched_ctx->capabilities)
118 rte_memcpy(sched_ctx->capabilities, tmp_caps,
119 sizeof(struct rte_cryptodev_capabilities) * nb_caps);
125 update_scheduler_feature_flag(struct rte_cryptodev *dev)
127 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
130 dev->feature_flags = 0;
132 for (i = 0; i < sched_ctx->nb_slaves; i++) {
133 struct rte_cryptodev_info dev_info;
135 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
137 dev->feature_flags |= dev_info.feature_flags;
142 update_max_nb_qp(struct scheduler_ctx *sched_ctx)
147 if (!sched_ctx->nb_slaves)
150 max_nb_qp = sched_ctx->nb_slaves ? UINT32_MAX : 0;
152 for (i = 0; i < sched_ctx->nb_slaves; i++) {
153 struct rte_cryptodev_info dev_info;
155 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
156 max_nb_qp = dev_info.max_nb_queue_pairs < max_nb_qp ?
157 dev_info.max_nb_queue_pairs : max_nb_qp;
160 sched_ctx->max_nb_queue_pairs = max_nb_qp;
163 /** Attach a device to the scheduler. */
165 rte_cryptodev_scheduler_slave_attach(uint8_t scheduler_id, uint8_t slave_id)
167 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
168 struct scheduler_ctx *sched_ctx;
169 struct scheduler_slave *slave;
170 struct rte_cryptodev_info dev_info;
174 CR_SCHED_LOG(ERR, "Operation not supported");
178 if (dev->driver_id != cryptodev_scheduler_driver_id) {
179 CR_SCHED_LOG(ERR, "Operation not supported");
183 if (dev->data->dev_started) {
184 CR_SCHED_LOG(ERR, "Illegal operation");
188 sched_ctx = dev->data->dev_private;
189 if (sched_ctx->nb_slaves >=
190 RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES) {
191 CR_SCHED_LOG(ERR, "Too many slaves attached");
195 for (i = 0; i < sched_ctx->nb_slaves; i++)
196 if (sched_ctx->slaves[i].dev_id == slave_id) {
197 CR_SCHED_LOG(ERR, "Slave already added");
201 slave = &sched_ctx->slaves[sched_ctx->nb_slaves];
203 rte_cryptodev_info_get(slave_id, &dev_info);
205 slave->dev_id = slave_id;
206 slave->driver_id = dev_info.driver_id;
207 sched_ctx->nb_slaves++;
209 if (update_scheduler_capability(sched_ctx) < 0) {
211 slave->driver_id = 0;
212 sched_ctx->nb_slaves--;
214 CR_SCHED_LOG(ERR, "capabilities update failed");
218 update_scheduler_feature_flag(dev);
220 update_max_nb_qp(sched_ctx);
226 rte_cryptodev_scheduler_slave_detach(uint8_t scheduler_id, uint8_t slave_id)
228 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
229 struct scheduler_ctx *sched_ctx;
230 uint32_t i, slave_pos;
233 CR_SCHED_LOG(ERR, "Operation not supported");
237 if (dev->driver_id != cryptodev_scheduler_driver_id) {
238 CR_SCHED_LOG(ERR, "Operation not supported");
242 if (dev->data->dev_started) {
243 CR_SCHED_LOG(ERR, "Illegal operation");
247 sched_ctx = dev->data->dev_private;
249 for (slave_pos = 0; slave_pos < sched_ctx->nb_slaves; slave_pos++)
250 if (sched_ctx->slaves[slave_pos].dev_id == slave_id)
252 if (slave_pos == sched_ctx->nb_slaves) {
253 CR_SCHED_LOG(ERR, "Cannot find slave");
257 if (sched_ctx->ops.slave_detach(dev, slave_id) < 0) {
258 CR_SCHED_LOG(ERR, "Failed to detach slave");
262 for (i = slave_pos; i < sched_ctx->nb_slaves - 1; i++) {
263 memcpy(&sched_ctx->slaves[i], &sched_ctx->slaves[i+1],
264 sizeof(struct scheduler_slave));
266 memset(&sched_ctx->slaves[sched_ctx->nb_slaves - 1], 0,
267 sizeof(struct scheduler_slave));
268 sched_ctx->nb_slaves--;
270 if (update_scheduler_capability(sched_ctx) < 0) {
271 CR_SCHED_LOG(ERR, "capabilities update failed");
275 update_scheduler_feature_flag(dev);
277 update_max_nb_qp(sched_ctx);
283 rte_cryptodev_scheduler_mode_set(uint8_t scheduler_id,
284 enum rte_cryptodev_scheduler_mode mode)
286 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
287 struct scheduler_ctx *sched_ctx;
290 CR_SCHED_LOG(ERR, "Operation not supported");
294 if (dev->driver_id != cryptodev_scheduler_driver_id) {
295 CR_SCHED_LOG(ERR, "Operation not supported");
299 if (dev->data->dev_started) {
300 CR_SCHED_LOG(ERR, "Illegal operation");
304 sched_ctx = dev->data->dev_private;
306 if (mode == sched_ctx->mode)
310 case CDEV_SCHED_MODE_ROUNDROBIN:
311 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
312 crypto_scheduler_roundrobin) < 0) {
313 CR_SCHED_LOG(ERR, "Failed to load scheduler");
317 case CDEV_SCHED_MODE_PKT_SIZE_DISTR:
318 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
319 crypto_scheduler_pkt_size_based_distr) < 0) {
320 CR_SCHED_LOG(ERR, "Failed to load scheduler");
324 case CDEV_SCHED_MODE_FAILOVER:
325 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
326 crypto_scheduler_failover) < 0) {
327 CR_SCHED_LOG(ERR, "Failed to load scheduler");
331 case CDEV_SCHED_MODE_MULTICORE:
332 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
333 crypto_scheduler_multicore) < 0) {
334 CR_SCHED_LOG(ERR, "Failed to load scheduler");
339 CR_SCHED_LOG(ERR, "Not yet supported");
346 enum rte_cryptodev_scheduler_mode
347 rte_cryptodev_scheduler_mode_get(uint8_t scheduler_id)
349 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
350 struct scheduler_ctx *sched_ctx;
353 CR_SCHED_LOG(ERR, "Operation not supported");
357 if (dev->driver_id != cryptodev_scheduler_driver_id) {
358 CR_SCHED_LOG(ERR, "Operation not supported");
362 sched_ctx = dev->data->dev_private;
364 return sched_ctx->mode;
368 rte_cryptodev_scheduler_ordering_set(uint8_t scheduler_id,
369 uint32_t enable_reorder)
371 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
372 struct scheduler_ctx *sched_ctx;
375 CR_SCHED_LOG(ERR, "Operation not supported");
379 if (dev->driver_id != cryptodev_scheduler_driver_id) {
380 CR_SCHED_LOG(ERR, "Operation not supported");
384 if (dev->data->dev_started) {
385 CR_SCHED_LOG(ERR, "Illegal operation");
389 sched_ctx = dev->data->dev_private;
391 sched_ctx->reordering_enabled = enable_reorder;
397 rte_cryptodev_scheduler_ordering_get(uint8_t scheduler_id)
399 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
400 struct scheduler_ctx *sched_ctx;
403 CR_SCHED_LOG(ERR, "Operation not supported");
407 if (dev->driver_id != cryptodev_scheduler_driver_id) {
408 CR_SCHED_LOG(ERR, "Operation not supported");
412 sched_ctx = dev->data->dev_private;
414 return (int)sched_ctx->reordering_enabled;
418 rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id,
419 struct rte_cryptodev_scheduler *scheduler) {
421 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
422 struct scheduler_ctx *sched_ctx;
425 CR_SCHED_LOG(ERR, "Operation not supported");
429 if (dev->driver_id != cryptodev_scheduler_driver_id) {
430 CR_SCHED_LOG(ERR, "Operation not supported");
434 if (dev->data->dev_started) {
435 CR_SCHED_LOG(ERR, "Illegal operation");
439 sched_ctx = dev->data->dev_private;
441 if (strlen(scheduler->name) > RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
442 CR_SCHED_LOG(ERR, "Invalid name %s, should be less than "
443 "%u bytes.", scheduler->name,
444 RTE_CRYPTODEV_NAME_MAX_LEN);
447 strlcpy(sched_ctx->name, scheduler->name, sizeof(sched_ctx->name));
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 strlcpy(sched_ctx->description, scheduler->description,
457 sizeof(sched_ctx->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_scheduler_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_scheduler_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");