1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
17 #include <rte_common.h>
18 #include <rte_malloc.h>
19 #include <rte_mempool.h>
21 #include <rte_cycles.h>
22 #include <rte_regexdev.h>
24 #define MAX_FILE_NAME 255
25 #define MBUF_CACHE_SIZE 256
26 #define MBUF_SIZE (1 << 8)
27 #define START_BURST_SIZE 32u
35 ARG_NUM_OF_ITERATIONS,
41 struct rte_mbuf *mbuf;
45 uint32_t total_enqueue;
46 uint32_t total_dequeue;
47 uint32_t total_matches;
48 struct rte_regex_ops **ops;
49 struct job_ctx *jobs_ctx;
53 struct qps_per_lcore {
54 unsigned int lcore_id;
63 uint32_t nb_iterations;
65 uint8_t nb_max_matches;
74 usage(const char *prog_name)
76 printf("%s [EAL options] --\n"
77 " --rules NAME: precompiled rules file\n"
78 " --data NAME: data file to use\n"
79 " --nb_jobs: number of jobs to use\n"
80 " --perf N: only outputs the performance data\n"
81 " --nb_iter N: number of iteration to run\n"
82 " --nb_qps N: number of queues to use\n"
83 " --nb_lcores N: number of lcores to use\n",
88 args_parse(int argc, char **argv, char *rules_file, char *data_file,
89 uint32_t *nb_jobs, bool *perf_mode, uint32_t *nb_iterations,
90 uint32_t *nb_qps, uint32_t *nb_lcores)
96 static struct option lgopts[] = {
97 { "help", 0, 0, ARG_HELP},
98 /* Rules database file to load. */
99 { "rules", 1, 0, ARG_RULES_FILE_NAME},
100 /* Data file to load. */
101 { "data", 1, 0, ARG_DATA_FILE_NAME},
102 /* Number of jobs to create. */
103 { "nb_jobs", 1, 0, ARG_NUM_OF_JOBS},
105 { "perf", 0, 0, ARG_PERF_MODE},
106 /* Number of iterations to run with perf test */
107 { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
109 { "nb_qps", 1, 0, ARG_NUM_OF_QPS},
110 /* Number of lcores. */
111 { "nb_lcores", 1, 0, ARG_NUM_OF_LCORES},
117 while ((opt = getopt_long(argc, argvopt, "",
118 lgopts, &opt_idx)) != EOF) {
120 case ARG_RULES_FILE_NAME:
121 len = strnlen(optarg, MAX_FILE_NAME - 1);
122 if (len == MAX_FILE_NAME)
123 rte_exit(EXIT_FAILURE,
124 "Rule file name to long max %d\n",
126 strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
128 case ARG_DATA_FILE_NAME:
129 len = strnlen(optarg, MAX_FILE_NAME - 1);
130 if (len == MAX_FILE_NAME)
131 rte_exit(EXIT_FAILURE,
132 "Data file name to long max %d\n",
134 strncpy(data_file, optarg, MAX_FILE_NAME - 1);
136 case ARG_NUM_OF_JOBS:
137 *nb_jobs = atoi(optarg);
142 case ARG_NUM_OF_ITERATIONS:
143 *nb_iterations = atoi(optarg);
146 *nb_qps = atoi(optarg);
148 case ARG_NUM_OF_LCORES:
149 *nb_lcores = atoi(optarg);
152 usage("RegEx test app");
155 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
156 usage("RegEx test app");
157 rte_exit(EXIT_FAILURE, "Invalid option\n");
167 read_file(char *file, char **buf)
174 fp = fopen(file, "r");
177 if (fseek(fp, 0L, SEEK_END) == 0) {
183 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
188 if (fseek(fp, 0L, SEEK_SET) != 0) {
192 read_len = fread(*buf, sizeof(char), buf_len, fp);
193 if (read_len != (unsigned long)buf_len) {
201 printf("Error, can't open file %s\n, err = %d", file, res);
210 clone_buf(char *data_buf, char **buf, long data_len)
214 rte_malloc(NULL, sizeof(char) * (data_len + 1), 4096);
217 memcpy(dest_buf, data_buf, data_len + 1);
223 init_port(uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches,
231 struct rte_regexdev_info info;
232 struct rte_regexdev_config dev_conf = {
233 .nb_queue_pairs = nb_qps,
236 struct rte_regexdev_qp_conf qp_conf = {
242 num_devs = rte_regexdev_count();
244 printf("Error, no devices detected.\n");
248 rules_len = read_file(rules_file, &rules);
250 printf("Error, can't read rules files.\n");
255 for (id = 0; id < num_devs; id++) {
256 res = rte_regexdev_info_get(id, &info);
258 printf("Error, can't get device info.\n");
261 printf(":: initializing dev: %d\n", id);
262 *nb_max_matches = info.max_matches;
263 *nb_max_payload = info.max_payload_size;
264 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
265 dev_conf.dev_cfg_flags |=
266 RTE_REGEXDEV_CFG_MATCH_AS_END_F;
267 dev_conf.nb_max_matches = info.max_matches;
268 dev_conf.nb_rules_per_group = info.max_rules_per_group;
269 dev_conf.rule_db_len = rules_len;
270 dev_conf.rule_db = rules;
271 res = rte_regexdev_configure(id, &dev_conf);
273 printf("Error, can't configure device %d.\n", id);
276 if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
277 qp_conf.qp_conf_flags |=
278 RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
279 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
280 res = rte_regexdev_queue_pair_setup(id, qp_id,
283 printf("Error, can't setup queue pair %u for "
284 "device %d.\n", qp_id, id);
288 printf(":: initializing device: %d done\n", id);
299 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
304 run_regex(void *args)
306 struct regex_conf *rgxc = args;
307 uint32_t nb_jobs = rgxc->nb_jobs;
308 uint32_t nb_iterations = rgxc->nb_iterations;
309 uint8_t nb_max_matches = rgxc->nb_max_matches;
310 uint32_t nb_qps = rgxc->nb_qps;
311 uint16_t qp_id_base = rgxc->qp_id_base;
312 char *data_buf = rgxc->data_buf;
313 long data_len = rgxc->data_len;
314 long job_len = rgxc->job_len;
317 uint32_t actual_jobs = 0;
322 struct rte_regexdev_match *match;
324 unsigned long d_ind = 0;
325 struct rte_mbuf_ext_shared_info shinfo;
330 struct rte_mempool *mbuf_mp;
331 struct qp_params *qp;
332 struct qp_params *qps = NULL;
334 uint16_t qps_used = 0;
337 shinfo.free_cb = extbuf_free_cb;
340 "mbuf_pool_%2u", qp_id_base);
341 mbuf_mp = rte_pktmbuf_pool_create(mbuf_pool, nb_jobs * nb_qps, 0,
342 0, MBUF_SIZE, rte_socket_id());
343 if (mbuf_mp == NULL) {
344 printf("Error, can't create memory pool\n");
348 qps = rte_malloc(NULL, sizeof(*qps) * nb_qps, 0);
350 printf("Error, can't allocate memory for QPs\n");
355 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
356 struct rte_regex_ops **ops;
357 struct job_ctx *jobs_ctx;
363 qp->ops = ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
365 printf("Error, can't allocate memory for ops.\n");
370 qp->jobs_ctx = jobs_ctx =
371 rte_malloc(NULL, sizeof(*jobs_ctx) * nb_jobs, 0);
373 printf("Error, can't allocate memory for jobs_ctx.\n");
378 /* Allocate the jobs and assign each job with an mbuf. */
379 for (i = 0; i < nb_jobs; i++) {
380 ops[i] = rte_malloc(NULL, sizeof(*ops[0]) +
382 sizeof(struct rte_regexdev_match), 0);
384 printf("Error, can't allocate "
389 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
391 printf("Error, can't attach mbuf.\n");
397 if (clone_buf(data_buf, &buf, data_len)) {
398 printf("Error, can't clone buf.\n");
403 /* Assign each mbuf with the data to handle. */
406 for (i = 0; (pos < data_len) && (i < nb_jobs) ; i++) {
407 long act_job_len = RTE_MIN(job_len, data_len - pos);
408 rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
409 act_job_len, &shinfo);
410 jobs_ctx[i].mbuf = ops[i]->mbuf;
411 ops[i]->mbuf->data_len = job_len;
412 ops[i]->mbuf->pkt_len = act_job_len;
414 ops[i]->group_id0 = 1;
420 qp->total_matches = 0;
424 for (i = 0; i < nb_iterations; i++) {
425 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
427 qp->total_enqueue = 0;
428 qp->total_dequeue = 0;
432 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
434 if (qp->total_dequeue < actual_jobs) {
435 struct rte_regex_ops **
436 cur_ops_to_enqueue = qp->ops +
439 if (actual_jobs - qp->total_enqueue)
441 rte_regexdev_enqueue_burst
449 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
451 if (qp->total_dequeue < actual_jobs) {
452 struct rte_regex_ops **
453 cur_ops_to_dequeue = qp->ops +
457 rte_regexdev_dequeue_burst
469 time = ((double)end - start) / CLOCKS_PER_SEC;
470 printf("Job len = %ld Bytes\n", job_len);
471 printf("Time = %lf sec\n", time);
472 printf("Perf = %lf Gbps\n",
473 (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
478 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
479 printf("\n############ QP id=%u ############\n", qp_id);
481 /* Log results per job. */
482 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
483 nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
484 printf("Job id %"PRIu64" number of matches = %d\n",
485 qp->ops[d_ind]->user_id, nb_matches);
486 qp->total_matches += nb_matches;
487 match = qp->ops[d_ind % actual_jobs]->matches;
488 for (i = 0; i < nb_matches; i++) {
489 printf("match %d, rule = %d, "
490 "start = %d,len = %d\n",
491 i, match->rule_id, match->start_offset,
496 printf("Total matches = %d\n", qp->total_matches);
497 printf("All Matches:\n");
498 /* Log absolute results. */
499 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
500 nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
501 qp->total_matches += nb_matches;
502 match = qp->ops[d_ind % actual_jobs]->matches;
503 for (i = 0; i < nb_matches; i++) {
504 printf("start = %ld, len = %d, rule = %d\n",
505 match->start_offset +
507 match->len, match->rule_id);
513 for (qp_id = 0; qp_id < qps_used; qp_id++) {
515 for (i = 0; i < actual_jobs && qp->ops; i++)
516 rte_free(qp->ops[i]);
519 for (i = 0; i < actual_jobs && qp->jobs_ctx; i++)
520 rte_pktmbuf_free(qp->jobs_ctx[i].mbuf);
521 rte_free(qp->jobs_ctx);
527 rte_mempool_free(mbuf_mp);
533 distribute_qps_to_lcores(uint32_t nb_cores, uint32_t nb_qps,
534 struct qps_per_lcore **qpl)
541 struct qps_per_lcore *qps_per_lcore;
542 uint32_t detected_lcores;
544 if (nb_qps < nb_cores) {
546 printf("Reducing number of cores to number of QPs (%u)\n",
549 /* Allocate qps_per_lcore array */
551 rte_malloc(NULL, sizeof(*qps_per_lcore) * nb_cores, 0);
553 rte_exit(EXIT_FAILURE, "Failed to create qps_per_lcore array\n");
554 *qpl = qps_per_lcore;
558 RTE_LCORE_FOREACH_WORKER(lcore_id) {
559 if (detected_lcores >= nb_cores)
561 qps_per_lcore[detected_lcores].lcore_id = lcore_id;
562 socket = rte_lcore_to_socket_id(lcore_id);
563 if (socket == SOCKET_ID_ANY)
565 qps_per_lcore[detected_lcores].socket = socket;
566 qps_per_lcore[detected_lcores].qp_id_base = min_qp_id;
567 max_qp_id = min_qp_id + nb_qps / nb_cores - 1;
568 if (nb_qps % nb_cores > detected_lcores)
570 qps_per_lcore[detected_lcores].nb_qps = max_qp_id -
572 min_qp_id = max_qp_id + 1;
575 if (detected_lcores != nb_cores)
578 for (i = 0; i < detected_lcores; i++) {
579 printf("===> Core %d: allocated queues: ",
580 qps_per_lcore[i].lcore_id);
581 min_qp_id = qps_per_lcore[i].qp_id_base;
583 qps_per_lcore[i].qp_id_base + qps_per_lcore[i].nb_qps;
584 while (min_qp_id < max_qp_id) {
585 printf("%u ", min_qp_id);
594 main(int argc, char **argv)
596 char rules_file[MAX_FILE_NAME];
597 char data_file[MAX_FILE_NAME];
598 uint32_t nb_jobs = 0;
600 uint32_t nb_iterations = 0;
602 uint16_t nb_max_payload = 0;
603 uint8_t nb_max_matches = 0;
608 uint32_t nb_lcores = 1;
609 struct regex_conf *rgxc;
611 struct qps_per_lcore *qps_per_lcore;
614 ret = rte_eal_init(argc, argv);
616 rte_exit(EXIT_FAILURE, "EAL init failed\n");
620 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
621 &perf_mode, &nb_iterations, &nb_qps,
625 rte_exit(EXIT_FAILURE, "Number of QPs must be greater than 0\n");
627 rte_exit(EXIT_FAILURE, "Number of lcores must be greater than 0\n");
628 if (distribute_qps_to_lcores(nb_lcores, nb_qps, &qps_per_lcore) < 0)
629 rte_exit(EXIT_FAILURE, "Failed to distribute queues to lcores!\n");
630 ret = init_port(&nb_max_payload, rules_file,
631 &nb_max_matches, nb_qps);
633 rte_exit(EXIT_FAILURE, "init port failed\n");
635 data_len = read_file(data_file, &data_buf);
637 rte_exit(EXIT_FAILURE, "Error, can't read file, or file is empty.\n");
639 job_len = data_len / nb_jobs;
641 rte_exit(EXIT_FAILURE, "Error, To many jobs, for the given input.\n");
643 if (job_len > nb_max_payload)
644 rte_exit(EXIT_FAILURE, "Error, not enough jobs to cover input.\n");
646 rgxc = rte_malloc(NULL, sizeof(*rgxc) * nb_lcores, 0);
648 rte_exit(EXIT_FAILURE, "Failed to create Regex Conf\n");
649 for (i = 0; i < nb_lcores; i++) {
650 rgxc[i] = (struct regex_conf){
652 .perf_mode = perf_mode,
653 .nb_iterations = nb_iterations,
654 .nb_max_matches = nb_max_matches,
655 .nb_qps = qps_per_lcore[i].nb_qps,
656 .qp_id_base = qps_per_lcore[i].qp_id_base,
657 .data_buf = data_buf,
658 .data_len = data_len,
661 rte_eal_remote_launch(run_regex, &rgxc[i],
662 qps_per_lcore[i].lcore_id);
664 rte_eal_mp_wait_lcore();
667 rte_free(qps_per_lcore);