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 /** update the scheduler pmd's capability with attaching device's
15 * For each device to be attached, the scheduler's capability should be
16 * the common capability set of all slaves
19 sync_caps(struct rte_cryptodev_capabilities *caps,
21 const struct rte_cryptodev_capabilities *slave_caps)
23 uint32_t sync_nb_caps = nb_caps, nb_slave_caps = 0;
26 while (slave_caps[nb_slave_caps].op != RTE_CRYPTO_OP_TYPE_UNDEFINED)
30 rte_memcpy(caps, slave_caps, sizeof(*caps) * nb_slave_caps);
34 for (i = 0; i < sync_nb_caps; i++) {
35 struct rte_cryptodev_capabilities *cap = &caps[i];
38 for (j = 0; j < nb_slave_caps; j++) {
39 const struct rte_cryptodev_capabilities *s_cap =
42 if (s_cap->op != cap->op || s_cap->sym.xform_type !=
46 if (s_cap->sym.xform_type ==
47 RTE_CRYPTO_SYM_XFORM_AUTH) {
48 if (s_cap->sym.auth.algo !=
52 cap->sym.auth.digest_size.min =
53 s_cap->sym.auth.digest_size.min <
54 cap->sym.auth.digest_size.min ?
55 s_cap->sym.auth.digest_size.min :
56 cap->sym.auth.digest_size.min;
57 cap->sym.auth.digest_size.max =
58 s_cap->sym.auth.digest_size.max <
59 cap->sym.auth.digest_size.max ?
60 s_cap->sym.auth.digest_size.max :
61 cap->sym.auth.digest_size.max;
65 if (s_cap->sym.xform_type ==
66 RTE_CRYPTO_SYM_XFORM_CIPHER)
67 if (s_cap->sym.cipher.algo !=
71 /* no common cap found */
75 if (j < nb_slave_caps)
78 /* remove a uncommon cap from the array */
79 for (j = i; j < sync_nb_caps - 1; j++)
80 rte_memcpy(&caps[j], &caps[j+1], sizeof(*cap));
82 memset(&caps[sync_nb_caps - 1], 0, sizeof(*cap));
90 update_scheduler_capability(struct scheduler_ctx *sched_ctx)
92 struct rte_cryptodev_capabilities tmp_caps[256] = { {0} };
93 uint32_t nb_caps = 0, i;
95 if (sched_ctx->capabilities) {
96 rte_free(sched_ctx->capabilities);
97 sched_ctx->capabilities = NULL;
100 for (i = 0; i < sched_ctx->nb_slaves; i++) {
101 struct rte_cryptodev_info dev_info;
103 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
105 nb_caps = sync_caps(tmp_caps, nb_caps, dev_info.capabilities);
110 sched_ctx->capabilities = rte_zmalloc_socket(NULL,
111 sizeof(struct rte_cryptodev_capabilities) *
112 (nb_caps + 1), 0, SOCKET_ID_ANY);
113 if (!sched_ctx->capabilities)
116 rte_memcpy(sched_ctx->capabilities, tmp_caps,
117 sizeof(struct rte_cryptodev_capabilities) * nb_caps);
123 update_scheduler_feature_flag(struct rte_cryptodev *dev)
125 struct scheduler_ctx *sched_ctx = dev->data->dev_private;
128 dev->feature_flags = 0;
130 for (i = 0; i < sched_ctx->nb_slaves; i++) {
131 struct rte_cryptodev_info dev_info;
133 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
135 dev->feature_flags |= dev_info.feature_flags;
140 update_max_nb_qp(struct scheduler_ctx *sched_ctx)
145 if (!sched_ctx->nb_slaves)
148 max_nb_qp = sched_ctx->nb_slaves ? UINT32_MAX : 0;
150 for (i = 0; i < sched_ctx->nb_slaves; i++) {
151 struct rte_cryptodev_info dev_info;
153 rte_cryptodev_info_get(sched_ctx->slaves[i].dev_id, &dev_info);
154 max_nb_qp = dev_info.max_nb_queue_pairs < max_nb_qp ?
155 dev_info.max_nb_queue_pairs : max_nb_qp;
158 sched_ctx->max_nb_queue_pairs = max_nb_qp;
161 /** Attach a device to the scheduler. */
163 rte_cryptodev_scheduler_slave_attach(uint8_t scheduler_id, uint8_t slave_id)
165 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
166 struct scheduler_ctx *sched_ctx;
167 struct scheduler_slave *slave;
168 struct rte_cryptodev_info dev_info;
172 CR_SCHED_LOG(ERR, "Operation not supported");
176 if (dev->driver_id != cryptodev_scheduler_driver_id) {
177 CR_SCHED_LOG(ERR, "Operation not supported");
181 if (dev->data->dev_started) {
182 CR_SCHED_LOG(ERR, "Illegal operation");
186 sched_ctx = dev->data->dev_private;
187 if (sched_ctx->nb_slaves >=
188 RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES) {
189 CR_SCHED_LOG(ERR, "Too many slaves attached");
193 for (i = 0; i < sched_ctx->nb_slaves; i++)
194 if (sched_ctx->slaves[i].dev_id == slave_id) {
195 CR_SCHED_LOG(ERR, "Slave already added");
199 slave = &sched_ctx->slaves[sched_ctx->nb_slaves];
201 rte_cryptodev_info_get(slave_id, &dev_info);
203 slave->dev_id = slave_id;
204 slave->driver_id = dev_info.driver_id;
205 sched_ctx->nb_slaves++;
207 if (update_scheduler_capability(sched_ctx) < 0) {
209 slave->driver_id = 0;
210 sched_ctx->nb_slaves--;
212 CR_SCHED_LOG(ERR, "capabilities update failed");
216 update_scheduler_feature_flag(dev);
218 update_max_nb_qp(sched_ctx);
224 rte_cryptodev_scheduler_slave_detach(uint8_t scheduler_id, uint8_t slave_id)
226 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
227 struct scheduler_ctx *sched_ctx;
228 uint32_t i, slave_pos;
231 CR_SCHED_LOG(ERR, "Operation not supported");
235 if (dev->driver_id != cryptodev_scheduler_driver_id) {
236 CR_SCHED_LOG(ERR, "Operation not supported");
240 if (dev->data->dev_started) {
241 CR_SCHED_LOG(ERR, "Illegal operation");
245 sched_ctx = dev->data->dev_private;
247 for (slave_pos = 0; slave_pos < sched_ctx->nb_slaves; slave_pos++)
248 if (sched_ctx->slaves[slave_pos].dev_id == slave_id)
250 if (slave_pos == sched_ctx->nb_slaves) {
251 CR_SCHED_LOG(ERR, "Cannot find slave");
255 if (sched_ctx->ops.slave_detach(dev, slave_id) < 0) {
256 CR_SCHED_LOG(ERR, "Failed to detach slave");
260 for (i = slave_pos; i < sched_ctx->nb_slaves - 1; i++) {
261 memcpy(&sched_ctx->slaves[i], &sched_ctx->slaves[i+1],
262 sizeof(struct scheduler_slave));
264 memset(&sched_ctx->slaves[sched_ctx->nb_slaves - 1], 0,
265 sizeof(struct scheduler_slave));
266 sched_ctx->nb_slaves--;
268 if (update_scheduler_capability(sched_ctx) < 0) {
269 CR_SCHED_LOG(ERR, "capabilities update failed");
273 update_scheduler_feature_flag(dev);
275 update_max_nb_qp(sched_ctx);
281 rte_cryptodev_scheduler_mode_set(uint8_t scheduler_id,
282 enum rte_cryptodev_scheduler_mode mode)
284 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
285 struct scheduler_ctx *sched_ctx;
288 CR_SCHED_LOG(ERR, "Operation not supported");
292 if (dev->driver_id != cryptodev_scheduler_driver_id) {
293 CR_SCHED_LOG(ERR, "Operation not supported");
297 if (dev->data->dev_started) {
298 CR_SCHED_LOG(ERR, "Illegal operation");
302 sched_ctx = dev->data->dev_private;
304 if (mode == sched_ctx->mode)
308 case CDEV_SCHED_MODE_ROUNDROBIN:
309 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
310 crypto_scheduler_roundrobin) < 0) {
311 CR_SCHED_LOG(ERR, "Failed to load scheduler");
315 case CDEV_SCHED_MODE_PKT_SIZE_DISTR:
316 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
317 crypto_scheduler_pkt_size_based_distr) < 0) {
318 CR_SCHED_LOG(ERR, "Failed to load scheduler");
322 case CDEV_SCHED_MODE_FAILOVER:
323 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
324 crypto_scheduler_failover) < 0) {
325 CR_SCHED_LOG(ERR, "Failed to load scheduler");
329 case CDEV_SCHED_MODE_MULTICORE:
330 if (rte_cryptodev_scheduler_load_user_scheduler(scheduler_id,
331 crypto_scheduler_multicore) < 0) {
332 CR_SCHED_LOG(ERR, "Failed to load scheduler");
337 CR_SCHED_LOG(ERR, "Not yet supported");
344 enum rte_cryptodev_scheduler_mode
345 rte_cryptodev_scheduler_mode_get(uint8_t scheduler_id)
347 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
348 struct scheduler_ctx *sched_ctx;
351 CR_SCHED_LOG(ERR, "Operation not supported");
355 if (dev->driver_id != cryptodev_scheduler_driver_id) {
356 CR_SCHED_LOG(ERR, "Operation not supported");
360 sched_ctx = dev->data->dev_private;
362 return sched_ctx->mode;
366 rte_cryptodev_scheduler_ordering_set(uint8_t scheduler_id,
367 uint32_t enable_reorder)
369 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
370 struct scheduler_ctx *sched_ctx;
373 CR_SCHED_LOG(ERR, "Operation not supported");
377 if (dev->driver_id != cryptodev_scheduler_driver_id) {
378 CR_SCHED_LOG(ERR, "Operation not supported");
382 if (dev->data->dev_started) {
383 CR_SCHED_LOG(ERR, "Illegal operation");
387 sched_ctx = dev->data->dev_private;
389 sched_ctx->reordering_enabled = enable_reorder;
395 rte_cryptodev_scheduler_ordering_get(uint8_t scheduler_id)
397 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
398 struct scheduler_ctx *sched_ctx;
401 CR_SCHED_LOG(ERR, "Operation not supported");
405 if (dev->driver_id != cryptodev_scheduler_driver_id) {
406 CR_SCHED_LOG(ERR, "Operation not supported");
410 sched_ctx = dev->data->dev_private;
412 return (int)sched_ctx->reordering_enabled;
416 rte_cryptodev_scheduler_load_user_scheduler(uint8_t scheduler_id,
417 struct rte_cryptodev_scheduler *scheduler) {
419 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
420 struct scheduler_ctx *sched_ctx;
423 CR_SCHED_LOG(ERR, "Operation not supported");
427 if (dev->driver_id != cryptodev_scheduler_driver_id) {
428 CR_SCHED_LOG(ERR, "Operation not supported");
432 if (dev->data->dev_started) {
433 CR_SCHED_LOG(ERR, "Illegal operation");
437 sched_ctx = dev->data->dev_private;
439 if (strlen(scheduler->name) > RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
440 CR_SCHED_LOG(ERR, "Invalid name %s, should be less than "
441 "%u bytes.", scheduler->name,
442 RTE_CRYPTODEV_NAME_MAX_LEN);
445 strlcpy(sched_ctx->name, scheduler->name, sizeof(sched_ctx->name));
447 if (strlen(scheduler->description) >
448 RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN - 1) {
449 CR_SCHED_LOG(ERR, "Invalid description %s, should be less than "
450 "%u bytes.", scheduler->description,
451 RTE_CRYPTODEV_SCHEDULER_DESC_MAX_LEN - 1);
454 strlcpy(sched_ctx->description, scheduler->description,
455 sizeof(sched_ctx->description));
457 /* load scheduler instance operations functions */
458 sched_ctx->ops.config_queue_pair = scheduler->ops->config_queue_pair;
459 sched_ctx->ops.create_private_ctx = scheduler->ops->create_private_ctx;
460 sched_ctx->ops.scheduler_start = scheduler->ops->scheduler_start;
461 sched_ctx->ops.scheduler_stop = scheduler->ops->scheduler_stop;
462 sched_ctx->ops.slave_attach = scheduler->ops->slave_attach;
463 sched_ctx->ops.slave_detach = scheduler->ops->slave_detach;
464 sched_ctx->ops.option_set = scheduler->ops->option_set;
465 sched_ctx->ops.option_get = scheduler->ops->option_get;
467 if (sched_ctx->private_ctx) {
468 rte_free(sched_ctx->private_ctx);
469 sched_ctx->private_ctx = NULL;
472 if (sched_ctx->ops.create_private_ctx) {
473 int ret = (*sched_ctx->ops.create_private_ctx)(dev);
476 CR_SCHED_LOG(ERR, "Unable to create scheduler private "
482 sched_ctx->mode = scheduler->mode;
488 rte_cryptodev_scheduler_slaves_get(uint8_t scheduler_id, uint8_t *slaves)
490 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
491 struct scheduler_ctx *sched_ctx;
492 uint32_t nb_slaves = 0;
495 CR_SCHED_LOG(ERR, "Operation not supported");
499 if (dev->driver_id != cryptodev_scheduler_driver_id) {
500 CR_SCHED_LOG(ERR, "Operation not supported");
504 sched_ctx = dev->data->dev_private;
506 nb_slaves = sched_ctx->nb_slaves;
508 if (slaves && nb_slaves) {
511 for (i = 0; i < nb_slaves; i++)
512 slaves[i] = sched_ctx->slaves[i].dev_id;
515 return (int)nb_slaves;
519 rte_cryptodev_scheduler_option_set(uint8_t scheduler_id,
520 enum rte_cryptodev_schedule_option_type option_type,
523 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
524 struct scheduler_ctx *sched_ctx;
526 if (option_type == CDEV_SCHED_OPTION_NOT_SET ||
527 option_type >= CDEV_SCHED_OPTION_COUNT) {
528 CR_SCHED_LOG(ERR, "Invalid option parameter");
533 CR_SCHED_LOG(ERR, "Invalid option parameter");
537 if (dev->data->dev_started) {
538 CR_SCHED_LOG(ERR, "Illegal operation");
542 sched_ctx = dev->data->dev_private;
544 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_set, -ENOTSUP);
546 return (*sched_ctx->ops.option_set)(dev, option_type, option);
550 rte_cryptodev_scheduler_option_get(uint8_t scheduler_id,
551 enum rte_cryptodev_schedule_option_type option_type,
554 struct rte_cryptodev *dev = rte_cryptodev_pmd_get_dev(scheduler_id);
555 struct scheduler_ctx *sched_ctx;
558 CR_SCHED_LOG(ERR, "Operation not supported");
563 CR_SCHED_LOG(ERR, "Invalid option parameter");
567 if (dev->driver_id != cryptodev_scheduler_driver_id) {
568 CR_SCHED_LOG(ERR, "Operation not supported");
572 sched_ctx = dev->data->dev_private;
574 RTE_FUNC_PTR_OR_ERR_RET(*sched_ctx->ops.option_get, -ENOTSUP);
576 return (*sched_ctx->ops.option_get)(dev, option_type, option);
580 RTE_LOG_REGISTER(scheduler_logtype_driver, pmd.crypto.scheduler, INFO);