1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
4 #include <rte_common.h>
5 #include <rte_hexdump.h>
6 #include <rte_cryptodev.h>
7 #include <rte_cryptodev_pmd.h>
8 #include <rte_bus_vdev.h>
9 #include <rte_malloc.h>
10 #include <rte_cpuflags.h>
11 #include <rte_reorder.h>
12 #include <rte_string_fns.h>
14 #include "rte_cryptodev_scheduler.h"
15 #include "scheduler_pmd_private.h"
17 uint8_t cryptodev_scheduler_driver_id;
19 struct scheduler_init_params {
20 struct rte_cryptodev_pmd_init_params def_p;
22 enum rte_cryptodev_scheduler_mode mode;
23 char mode_param_str[RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN];
24 uint32_t enable_ordering;
25 uint16_t wc_pool[RTE_MAX_LCORE];
27 char slave_names[RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES]
28 [RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN];
31 #define RTE_CRYPTODEV_VDEV_NAME ("name")
32 #define RTE_CRYPTODEV_VDEV_SLAVE ("slave")
33 #define RTE_CRYPTODEV_VDEV_MODE ("mode")
34 #define RTE_CRYPTODEV_VDEV_MODE_PARAM ("mode_param")
35 #define RTE_CRYPTODEV_VDEV_ORDERING ("ordering")
36 #define RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG ("max_nb_queue_pairs")
37 #define RTE_CRYPTODEV_VDEV_SOCKET_ID ("socket_id")
38 #define RTE_CRYPTODEV_VDEV_COREMASK ("coremask")
39 #define RTE_CRYPTODEV_VDEV_CORELIST ("corelist")
41 static const char * const scheduler_valid_params[] = {
42 RTE_CRYPTODEV_VDEV_NAME,
43 RTE_CRYPTODEV_VDEV_SLAVE,
44 RTE_CRYPTODEV_VDEV_MODE,
45 RTE_CRYPTODEV_VDEV_MODE_PARAM,
46 RTE_CRYPTODEV_VDEV_ORDERING,
47 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
48 RTE_CRYPTODEV_VDEV_SOCKET_ID,
49 RTE_CRYPTODEV_VDEV_COREMASK,
50 RTE_CRYPTODEV_VDEV_CORELIST
53 struct scheduler_parse_map {
58 const struct scheduler_parse_map scheduler_mode_map[] = {
59 {RTE_STR(SCHEDULER_MODE_NAME_ROUND_ROBIN),
60 CDEV_SCHED_MODE_ROUNDROBIN},
61 {RTE_STR(SCHEDULER_MODE_NAME_PKT_SIZE_DISTR),
62 CDEV_SCHED_MODE_PKT_SIZE_DISTR},
63 {RTE_STR(SCHEDULER_MODE_NAME_FAIL_OVER),
64 CDEV_SCHED_MODE_FAILOVER},
65 {RTE_STR(SCHEDULER_MODE_NAME_MULTI_CORE),
66 CDEV_SCHED_MODE_MULTICORE}
69 const struct scheduler_parse_map scheduler_ordering_map[] = {
74 #define CDEV_SCHED_MODE_PARAM_SEP_CHAR ':'
77 cryptodev_scheduler_create(const char *name,
78 struct rte_vdev_device *vdev,
79 struct scheduler_init_params *init_params)
81 struct rte_cryptodev *dev;
82 struct scheduler_ctx *sched_ctx;
86 dev = rte_cryptodev_pmd_create(name, &vdev->device,
89 CR_SCHED_LOG(ERR, "driver %s: failed to create cryptodev vdev",
94 dev->driver_id = cryptodev_scheduler_driver_id;
95 dev->dev_ops = rte_crypto_scheduler_pmd_ops;
97 sched_ctx = dev->data->dev_private;
98 sched_ctx->max_nb_queue_pairs =
99 init_params->def_p.max_nb_queue_pairs;
101 if (init_params->mode == CDEV_SCHED_MODE_MULTICORE) {
104 sched_ctx->nb_wc = init_params->nb_wc;
106 for (i = 0; i < sched_ctx->nb_wc; i++) {
107 sched_ctx->wc_pool[i] = init_params->wc_pool[i];
108 CR_SCHED_LOG(INFO, " Worker core[%u]=%u added",
109 i, sched_ctx->wc_pool[i]);
113 if (init_params->mode > CDEV_SCHED_MODE_USERDEFINED &&
114 init_params->mode < CDEV_SCHED_MODE_COUNT) {
116 struct rte_cryptodev_scheduler_threshold_option
119 enum rte_cryptodev_schedule_option_type option_type;
120 char param_name[RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN] = {0};
121 char param_val[RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN] = {0};
124 ret = rte_cryptodev_scheduler_mode_set(dev->data->dev_id,
127 rte_cryptodev_pmd_release_device(dev);
131 for (i = 0; i < RTE_DIM(scheduler_mode_map); i++) {
132 if (scheduler_mode_map[i].val != sched_ctx->mode)
135 CR_SCHED_LOG(INFO, " Scheduling mode = %s",
136 scheduler_mode_map[i].name);
140 if (strlen(init_params->mode_param_str) > 0) {
141 s = strchr(init_params->mode_param_str,
142 CDEV_SCHED_MODE_PARAM_SEP_CHAR);
144 CR_SCHED_LOG(ERR, "Invalid mode param");
148 strlcpy(param_name, init_params->mode_param_str,
149 s - init_params->mode_param_str + 1);
151 strlcpy(param_val, s,
152 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
154 switch (init_params->mode) {
155 case CDEV_SCHED_MODE_PKT_SIZE_DISTR:
156 if (strcmp(param_name,
157 RTE_CRYPTODEV_SCHEDULER_PARAM_THRES)
159 CR_SCHED_LOG(ERR, "Invalid mode param");
162 option_type = CDEV_SCHED_OPTION_THRESHOLD;
164 option.threshold_option.threshold =
165 strtoul(param_val, &end, 0);
168 CR_SCHED_LOG(ERR, "Invalid mode param");
172 if (sched_ctx->ops.option_set(dev, option_type,
173 (void *)&option) < 0) {
174 CR_SCHED_LOG(ERR, "Invalid mode param");
178 RTE_LOG(INFO, PMD, " Sched mode param (%s = %s)\n",
179 param_name, param_val);
183 sched_ctx->reordering_enabled = init_params->enable_ordering;
185 for (i = 0; i < RTE_DIM(scheduler_ordering_map); i++) {
186 if (scheduler_ordering_map[i].val !=
187 sched_ctx->reordering_enabled)
190 CR_SCHED_LOG(INFO, " Packet ordering = %s",
191 scheduler_ordering_map[i].name);
196 for (i = 0; i < init_params->nb_slaves; i++) {
197 sched_ctx->init_slave_names[sched_ctx->nb_init_slaves] =
200 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN, 0,
203 if (!sched_ctx->init_slave_names[
204 sched_ctx->nb_init_slaves]) {
205 CR_SCHED_LOG(ERR, "driver %s: Insufficient memory",
210 strncpy(sched_ctx->init_slave_names[
211 sched_ctx->nb_init_slaves],
212 init_params->slave_names[i],
213 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
215 sched_ctx->nb_init_slaves++;
219 * Initialize capabilities structure as an empty structure,
220 * in case device information is requested when no slaves are attached
222 sched_ctx->capabilities = rte_zmalloc_socket(NULL,
223 sizeof(struct rte_cryptodev_capabilities),
226 if (!sched_ctx->capabilities) {
227 CR_SCHED_LOG(ERR, "Not enough memory for capability "
236 cryptodev_scheduler_remove(struct rte_vdev_device *vdev)
239 struct rte_cryptodev *dev;
240 struct scheduler_ctx *sched_ctx;
245 name = rte_vdev_device_name(vdev);
246 dev = rte_cryptodev_pmd_get_named_dev(name);
250 sched_ctx = dev->data->dev_private;
252 if (sched_ctx->nb_slaves) {
255 for (i = 0; i < sched_ctx->nb_slaves; i++)
256 rte_cryptodev_scheduler_slave_detach(dev->data->dev_id,
257 sched_ctx->slaves[i].dev_id);
260 return rte_cryptodev_pmd_destroy(dev);
263 /** Parse integer from integer argument */
265 parse_integer_arg(const char *key __rte_unused,
266 const char *value, void *extra_args)
268 int *i = (int *) extra_args;
272 CR_SCHED_LOG(ERR, "Argument has to be positive.");
279 /** Parse integer from hexadecimal integer argument */
281 parse_coremask_arg(const char *key __rte_unused,
282 const char *value, void *extra_args)
287 struct scheduler_init_params *params = extra_args;
293 /* Remove all blank characters ahead and after .
294 * Remove 0x/0X if exists.
296 while (isblank(*value))
298 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X')))
301 while ((i > 0) && isblank(value[i - 1]))
307 for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
309 if (isxdigit(c) == 0) {
310 /* invalid characters */
320 for (j = 0; j < 4 && idx < RTE_MAX_LCORE; j++, idx++) {
322 params->wc_pool[params->nb_wc++] = idx;
329 /** Parse integer from list of integers argument */
331 parse_corelist_arg(const char *key __rte_unused,
332 const char *value, void *extra_args)
334 struct scheduler_init_params *params = extra_args;
338 const char *token = value;
340 while (isdigit(token[0])) {
342 unsigned int core = strtoul(token, &rval, 10);
344 if (core >= RTE_MAX_LCORE) {
345 CR_SCHED_LOG(ERR, "Invalid worker core %u, should be smaller "
346 "than %u.", core, RTE_MAX_LCORE);
348 params->wc_pool[params->nb_wc++] = (uint16_t)core;
349 token = (const char *)rval;
350 if (token[0] == '\0')
360 parse_name_arg(const char *key __rte_unused,
361 const char *value, void *extra_args)
363 struct rte_cryptodev_pmd_init_params *params = extra_args;
365 if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
366 CR_SCHED_LOG(ERR, "Invalid name %s, should be less than "
368 RTE_CRYPTODEV_NAME_MAX_LEN - 1);
372 strlcpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
379 parse_slave_arg(const char *key __rte_unused,
380 const char *value, void *extra_args)
382 struct scheduler_init_params *param = extra_args;
384 if (param->nb_slaves >= RTE_CRYPTODEV_SCHEDULER_MAX_NB_SLAVES) {
385 CR_SCHED_LOG(ERR, "Too many slaves.");
389 strncpy(param->slave_names[param->nb_slaves++], value,
390 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN - 1);
396 parse_mode_arg(const char *key __rte_unused,
397 const char *value, void *extra_args)
399 struct scheduler_init_params *param = extra_args;
402 for (i = 0; i < RTE_DIM(scheduler_mode_map); i++) {
403 if (strcmp(value, scheduler_mode_map[i].name) == 0) {
404 param->mode = (enum rte_cryptodev_scheduler_mode)
405 scheduler_mode_map[i].val;
411 if (i == RTE_DIM(scheduler_mode_map)) {
412 CR_SCHED_LOG(ERR, "Unrecognized input.");
420 parse_mode_param_arg(const char *key __rte_unused,
421 const char *value, void *extra_args)
423 struct scheduler_init_params *param = extra_args;
425 strlcpy(param->mode_param_str, value,
426 RTE_CRYPTODEV_SCHEDULER_NAME_MAX_LEN);
432 parse_ordering_arg(const char *key __rte_unused,
433 const char *value, void *extra_args)
435 struct scheduler_init_params *param = extra_args;
438 for (i = 0; i < RTE_DIM(scheduler_ordering_map); i++) {
439 if (strcmp(value, scheduler_ordering_map[i].name) == 0) {
440 param->enable_ordering =
441 scheduler_ordering_map[i].val;
446 if (i == RTE_DIM(scheduler_ordering_map)) {
447 CR_SCHED_LOG(ERR, "Unrecognized input.");
455 scheduler_parse_init_params(struct scheduler_init_params *params,
456 const char *input_args)
458 struct rte_kvargs *kvlist = NULL;
465 kvlist = rte_kvargs_parse(input_args,
466 scheduler_valid_params);
470 ret = rte_kvargs_process(kvlist,
471 RTE_CRYPTODEV_VDEV_MAX_NB_QP_ARG,
473 ¶ms->def_p.max_nb_queue_pairs);
477 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SOCKET_ID,
479 ¶ms->def_p.socket_id);
483 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_COREMASK,
489 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_CORELIST,
495 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_NAME,
501 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_SLAVE,
502 &parse_slave_arg, params);
506 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_MODE,
507 &parse_mode_arg, params);
511 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_MODE_PARAM,
512 &parse_mode_param_arg, params);
516 ret = rte_kvargs_process(kvlist, RTE_CRYPTODEV_VDEV_ORDERING,
517 &parse_ordering_arg, params);
523 rte_kvargs_free(kvlist);
528 cryptodev_scheduler_probe(struct rte_vdev_device *vdev)
530 struct scheduler_init_params init_params = {
533 sizeof(struct scheduler_ctx),
535 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
538 .mode = CDEV_SCHED_MODE_NOT_SET,
539 .enable_ordering = 0,
540 .slave_names = { {0} }
544 name = rte_vdev_device_name(vdev);
548 scheduler_parse_init_params(&init_params,
549 rte_vdev_device_args(vdev));
552 return cryptodev_scheduler_create(name,
557 static struct rte_vdev_driver cryptodev_scheduler_pmd_drv = {
558 .probe = cryptodev_scheduler_probe,
559 .remove = cryptodev_scheduler_remove
562 static struct cryptodev_driver scheduler_crypto_drv;
564 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_SCHEDULER_PMD,
565 cryptodev_scheduler_pmd_drv);
566 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_SCHEDULER_PMD,
567 "max_nb_queue_pairs=<int> "
570 RTE_PMD_REGISTER_CRYPTO_DRIVER(scheduler_crypto_drv,
571 cryptodev_scheduler_pmd_drv.driver,
572 cryptodev_scheduler_driver_id);