82cffaacfa1d13319ebd2f0fb0dd63e16ab37825
[dpdk.git] / app / test-regex / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <stdarg.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <getopt.h>
14 #include <signal.h>
15
16 #include <rte_eal.h>
17 #include <rte_common.h>
18 #include <rte_malloc.h>
19 #include <rte_mempool.h>
20 #include <rte_mbuf.h>
21 #include <rte_cycles.h>
22 #include <rte_regexdev.h>
23
24 #define MAX_FILE_NAME 255
25 #define MBUF_CACHE_SIZE 256
26 #define MBUF_SIZE (1 << 8)
27 #define START_BURST_SIZE 32u
28
29 enum app_args {
30         ARG_HELP,
31         ARG_RULES_FILE_NAME,
32         ARG_DATA_FILE_NAME,
33         ARG_NUM_OF_JOBS,
34         ARG_PERF_MODE,
35         ARG_NUM_OF_ITERATIONS,
36         ARG_NUM_OF_QPS,
37         ARG_NUM_OF_LCORES,
38         ARG_NUM_OF_MBUF_SEGS,
39 };
40
41 struct job_ctx {
42         struct rte_mbuf *mbuf;
43 };
44
45 struct qp_params {
46         uint32_t total_enqueue;
47         uint32_t total_dequeue;
48         uint32_t total_matches;
49         struct rte_regex_ops **ops;
50         struct job_ctx *jobs_ctx;
51         char *buf;
52         uint64_t start;
53         uint64_t cycles;
54 };
55
56 struct qps_per_lcore {
57         unsigned int lcore_id;
58         int socket;
59         uint16_t qp_id_base;
60         uint16_t nb_qps;
61 };
62
63 struct regex_conf {
64         uint32_t nb_jobs;
65         bool perf_mode;
66         uint32_t nb_iterations;
67         char *data_file;
68         uint8_t nb_max_matches;
69         uint32_t nb_qps;
70         uint16_t qp_id_base;
71         char *data_buf;
72         long data_len;
73         long job_len;
74         uint32_t nb_segs;
75 };
76
77 static void
78 usage(const char *prog_name)
79 {
80         printf("%s [EAL options] --\n"
81                 " --rules NAME: precompiled rules file\n"
82                 " --data NAME: data file to use\n"
83                 " --nb_jobs: number of jobs to use\n"
84                 " --perf N: only outputs the performance data\n"
85                 " --nb_iter N: number of iteration to run\n"
86                 " --nb_qps N: number of queues to use\n"
87                 " --nb_lcores N: number of lcores to use\n"
88                 " --nb_segs N: number of mbuf segments\n",
89                 prog_name);
90 }
91
92 static void
93 args_parse(int argc, char **argv, char *rules_file, char *data_file,
94            uint32_t *nb_jobs, bool *perf_mode, uint32_t *nb_iterations,
95            uint32_t *nb_qps, uint32_t *nb_lcores, uint32_t *nb_segs)
96 {
97         char **argvopt;
98         int opt;
99         int opt_idx;
100         size_t len;
101         static struct option lgopts[] = {
102                 { "help",  0, 0, ARG_HELP},
103                 /* Rules database file to load. */
104                 { "rules",  1, 0, ARG_RULES_FILE_NAME},
105                 /* Data file to load. */
106                 { "data",  1, 0, ARG_DATA_FILE_NAME},
107                 /* Number of jobs to create. */
108                 { "nb_jobs",  1, 0, ARG_NUM_OF_JOBS},
109                 /* Perf test only */
110                 { "perf", 0, 0, ARG_PERF_MODE},
111                 /* Number of iterations to run with perf test */
112                 { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
113                 /* Number of QPs. */
114                 { "nb_qps", 1, 0, ARG_NUM_OF_QPS},
115                 /* Number of lcores. */
116                 { "nb_lcores", 1, 0, ARG_NUM_OF_LCORES},
117                 /* Number of mbuf segments. */
118                 { "nb_segs", 1, 0, ARG_NUM_OF_MBUF_SEGS},
119                 /* End of options */
120                 { 0, 0, 0, 0 }
121         };
122
123         argvopt = argv;
124         while ((opt = getopt_long(argc, argvopt, "",
125                                 lgopts, &opt_idx)) != EOF) {
126                 switch (opt) {
127                 case ARG_RULES_FILE_NAME:
128                         len = strnlen(optarg, MAX_FILE_NAME - 1);
129                         if (len == MAX_FILE_NAME)
130                                 rte_exit(EXIT_FAILURE,
131                                          "Rule file name to long max %d\n",
132                                          MAX_FILE_NAME - 1);
133                         strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
134                         break;
135                 case ARG_DATA_FILE_NAME:
136                         len = strnlen(optarg, MAX_FILE_NAME - 1);
137                         if (len == MAX_FILE_NAME)
138                                 rte_exit(EXIT_FAILURE,
139                                          "Data file name to long max %d\n",
140                                          MAX_FILE_NAME - 1);
141                         strncpy(data_file, optarg, MAX_FILE_NAME - 1);
142                         break;
143                 case ARG_NUM_OF_JOBS:
144                         *nb_jobs = atoi(optarg);
145                         break;
146                 case ARG_PERF_MODE:
147                         *perf_mode = true;
148                         break;
149                 case ARG_NUM_OF_ITERATIONS:
150                         *nb_iterations = atoi(optarg);
151                         break;
152                 case ARG_NUM_OF_QPS:
153                         *nb_qps = atoi(optarg);
154                         break;
155                 case ARG_NUM_OF_LCORES:
156                         *nb_lcores = atoi(optarg);
157                         break;
158                 case ARG_NUM_OF_MBUF_SEGS:
159                         *nb_segs = atoi(optarg);
160                         break;
161                 case ARG_HELP:
162                         usage("RegEx test app");
163                         break;
164                 default:
165                         fprintf(stderr, "Invalid option: %s\n", argv[optind]);
166                         usage("RegEx test app");
167                         rte_exit(EXIT_FAILURE, "Invalid option\n");
168                         break;
169                 }
170         }
171
172         if (!perf_mode)
173                 *nb_iterations = 1;
174 }
175
176 static long
177 read_file(char *file, char **buf)
178 {
179         FILE *fp;
180         long buf_len = 0;
181         size_t read_len;
182         int res = 0;
183
184         fp = fopen(file, "r");
185         if (!fp)
186                 return -EIO;
187         if (fseek(fp, 0L, SEEK_END) == 0) {
188                 buf_len = ftell(fp);
189                 if (buf_len == -1) {
190                         res = EIO;
191                         goto error;
192                 }
193                 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
194                 if (!*buf) {
195                         res = ENOMEM;
196                         goto error;
197                 }
198                 if (fseek(fp, 0L, SEEK_SET) != 0) {
199                         res = EIO;
200                         goto error;
201                 }
202                 read_len = fread(*buf, sizeof(char), buf_len, fp);
203                 if (read_len != (unsigned long)buf_len) {
204                         res = EIO;
205                         goto error;
206                 }
207         }
208         fclose(fp);
209         return buf_len;
210 error:
211         printf("Error, can't open file %s\n, err = %d", file, res);
212         if (fp)
213                 fclose(fp);
214         if (*buf)
215                 rte_free(*buf);
216         return -res;
217 }
218
219 static int
220 clone_buf(char *data_buf, char **buf, long data_len)
221 {
222         char *dest_buf;
223         dest_buf =
224                 rte_malloc(NULL, sizeof(char) * (data_len + 1), 4096);
225         if (!dest_buf)
226                 return -ENOMEM;
227         memcpy(dest_buf, data_buf, data_len + 1);
228         *buf = dest_buf;
229         return 0;
230 }
231
232 static int
233 init_port(uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches,
234           uint32_t nb_qps)
235 {
236         uint16_t id;
237         uint16_t qp_id;
238         uint16_t num_devs;
239         char *rules = NULL;
240         long rules_len;
241         struct rte_regexdev_info info;
242         struct rte_regexdev_config dev_conf = {
243                 .nb_queue_pairs = nb_qps,
244                 .nb_groups = 1,
245         };
246         struct rte_regexdev_qp_conf qp_conf = {
247                 .nb_desc = 1024,
248                 .qp_conf_flags = 0,
249         };
250         int res = 0;
251
252         num_devs = rte_regexdev_count();
253         if (num_devs == 0) {
254                 printf("Error, no devices detected.\n");
255                 return -EINVAL;
256         }
257
258         rules_len = read_file(rules_file, &rules);
259         if (rules_len < 0) {
260                 printf("Error, can't read rules files.\n");
261                 res = -EIO;
262                 goto error;
263         }
264
265         for (id = 0; id < num_devs; id++) {
266                 res = rte_regexdev_info_get(id, &info);
267                 if (res != 0) {
268                         printf("Error, can't get device info.\n");
269                         goto error;
270                 }
271                 printf(":: initializing dev: %d\n", id);
272                 *nb_max_matches = info.max_matches;
273                 *nb_max_payload = info.max_payload_size;
274                 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
275                         dev_conf.dev_cfg_flags |=
276                         RTE_REGEXDEV_CFG_MATCH_AS_END_F;
277                 dev_conf.nb_max_matches = info.max_matches;
278                 dev_conf.nb_rules_per_group = info.max_rules_per_group;
279                 dev_conf.rule_db_len = rules_len;
280                 dev_conf.rule_db = rules;
281                 res = rte_regexdev_configure(id, &dev_conf);
282                 if (res < 0) {
283                         printf("Error, can't configure device %d.\n", id);
284                         goto error;
285                 }
286                 if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
287                         qp_conf.qp_conf_flags |=
288                         RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
289                 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
290                         res = rte_regexdev_queue_pair_setup(id, qp_id,
291                                                             &qp_conf);
292                         if (res < 0) {
293                                 printf("Error, can't setup queue pair %u for "
294                                        "device %d.\n", qp_id, id);
295                                 goto error;
296                         }
297                 }
298                 printf(":: initializing device: %d done\n", id);
299         }
300         rte_free(rules);
301         return 0;
302 error:
303         if (rules)
304                 rte_free(rules);
305         return res;
306 }
307
308 static void
309 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
310 {
311 }
312
313 static inline struct rte_mbuf *
314 regex_create_segmented_mbuf(struct rte_mempool *mbuf_pool, int pkt_len,
315                 int nb_segs, void *buf) {
316
317         struct rte_mbuf *m = NULL, *mbuf = NULL;
318         uint8_t *dst;
319         char *src = buf;
320         int data_len = 0;
321         int i, size;
322         int t_len;
323
324         if (pkt_len < 1) {
325                 printf("Packet size must be 1 or more (is %d)\n", pkt_len);
326                 return NULL;
327         }
328
329         if (nb_segs < 1) {
330                 printf("Number of segments must be 1 or more (is %d)\n",
331                                 nb_segs);
332                 return NULL;
333         }
334
335         t_len = pkt_len >= nb_segs ? (pkt_len / nb_segs +
336                                      !!(pkt_len % nb_segs)) : 1;
337         size = pkt_len;
338
339         /* Create chained mbuf_src and fill it with buf data */
340         for (i = 0; size > 0; i++) {
341
342                 m = rte_pktmbuf_alloc(mbuf_pool);
343                 if (i == 0)
344                         mbuf = m;
345
346                 if (m == NULL) {
347                         printf("Cannot create segment for source mbuf");
348                         goto fail;
349                 }
350
351                 data_len = size > t_len ? t_len : size;
352                 memset(rte_pktmbuf_mtod(m, uint8_t *), 0,
353                                 rte_pktmbuf_tailroom(m));
354                 memcpy(rte_pktmbuf_mtod(m, uint8_t *), src, data_len);
355                 dst = (uint8_t *)rte_pktmbuf_append(m, data_len);
356                 if (dst == NULL) {
357                         printf("Cannot append %d bytes to the mbuf\n",
358                                         data_len);
359                         goto fail;
360                 }
361
362                 if (mbuf != m)
363                         rte_pktmbuf_chain(mbuf, m);
364                 src += data_len;
365                 size -= data_len;
366
367         }
368         return mbuf;
369
370 fail:
371         if (mbuf)
372                 rte_pktmbuf_free(mbuf);
373         return NULL;
374 }
375
376 static int
377 run_regex(void *args)
378 {
379         struct regex_conf *rgxc = args;
380         uint32_t nb_jobs = rgxc->nb_jobs;
381         uint32_t nb_segs = rgxc->nb_segs;
382         uint32_t nb_iterations = rgxc->nb_iterations;
383         uint8_t nb_max_matches = rgxc->nb_max_matches;
384         uint32_t nb_qps = rgxc->nb_qps;
385         uint16_t qp_id_base  = rgxc->qp_id_base;
386         char *data_buf = rgxc->data_buf;
387         long data_len = rgxc->data_len;
388         long job_len = rgxc->job_len;
389
390         char *buf = NULL;
391         uint32_t actual_jobs = 0;
392         uint32_t i;
393         uint16_t qp_id;
394         uint16_t dev_id = 0;
395         uint8_t nb_matches;
396         struct rte_regexdev_match *match;
397         long pos;
398         unsigned long d_ind = 0;
399         struct rte_mbuf_ext_shared_info shinfo;
400         int res = 0;
401         long double time;
402         struct rte_mempool *mbuf_mp;
403         struct qp_params *qp;
404         struct qp_params *qps = NULL;
405         bool update;
406         uint16_t qps_used = 0;
407         char mbuf_pool[16];
408
409         shinfo.free_cb = extbuf_free_cb;
410         snprintf(mbuf_pool,
411                  sizeof(mbuf_pool),
412                  "mbuf_pool_%2u", qp_id_base);
413         mbuf_mp = rte_pktmbuf_pool_create(mbuf_pool,
414                         rte_align32pow2(nb_jobs * nb_qps * nb_segs),
415                         0, 0, (nb_segs == 1) ? MBUF_SIZE :
416                         (rte_align32pow2(job_len) / nb_segs +
417                         RTE_PKTMBUF_HEADROOM),
418                         rte_socket_id());
419         if (mbuf_mp == NULL) {
420                 printf("Error, can't create memory pool\n");
421                 return -ENOMEM;
422         }
423
424         qps = rte_malloc(NULL, sizeof(*qps) * nb_qps, 0);
425         if (!qps) {
426                 printf("Error, can't allocate memory for QPs\n");
427                 res = -ENOMEM;
428                 goto end;
429         }
430
431         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
432                 struct rte_regex_ops **ops;
433                 struct job_ctx *jobs_ctx;
434
435                 qps_used++;
436                 qp = &qps[qp_id];
437                 qp->jobs_ctx = NULL;
438                 qp->buf = NULL;
439                 qp->ops = ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
440                 if (!ops) {
441                         printf("Error, can't allocate memory for ops.\n");
442                         res = -ENOMEM;
443                         goto end;
444                 }
445
446                 qp->jobs_ctx = jobs_ctx =
447                         rte_malloc(NULL, sizeof(*jobs_ctx) * nb_jobs, 0);
448                 if (!jobs_ctx) {
449                         printf("Error, can't allocate memory for jobs_ctx.\n");
450                         res = -ENOMEM;
451                         goto end;
452                 }
453
454                 if (clone_buf(data_buf, &buf, data_len)) {
455                         printf("Error, can't clone buf.\n");
456                         res = -EXIT_FAILURE;
457                         goto end;
458                 }
459
460                 /* Assign each mbuf with the data to handle. */
461                 actual_jobs = 0;
462                 pos = 0;
463                 /* Allocate the jobs and assign each job with an mbuf. */
464                 for (i = 0; (pos < data_len) && (i < nb_jobs) ; i++) {
465                         long act_job_len = RTE_MIN(job_len, data_len - pos);
466
467                         ops[i] = rte_malloc(NULL, sizeof(*ops[0]) +
468                                         nb_max_matches *
469                                         sizeof(struct rte_regexdev_match), 0);
470                         if (!ops[i]) {
471                                 printf("Error, can't allocate "
472                                        "memory for op.\n");
473                                 res = -ENOMEM;
474                                 goto end;
475                         }
476                         if (nb_segs > 1) {
477                                 ops[i]->mbuf = regex_create_segmented_mbuf
478                                                         (mbuf_mp, act_job_len,
479                                                          nb_segs, &buf[pos]);
480                         } else {
481                                 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
482                                 if (ops[i]->mbuf) {
483                                         rte_pktmbuf_attach_extbuf(ops[i]->mbuf,
484                                         &buf[pos], 0, act_job_len, &shinfo);
485                                         ops[i]->mbuf->data_len = job_len;
486                                         ops[i]->mbuf->pkt_len = act_job_len;
487                                 }
488                         }
489                         if (!ops[i]->mbuf) {
490                                 printf("Error, can't add mbuf.\n");
491                                 res = -ENOMEM;
492                                 goto end;
493                         }
494
495                         jobs_ctx[i].mbuf = ops[i]->mbuf;
496                         ops[i]->user_id = i;
497                         ops[i]->group_id0 = 1;
498                         pos += act_job_len;
499                         actual_jobs++;
500                 }
501
502                 qp->buf = buf;
503                 qp->total_matches = 0;
504                 qp->start = 0;
505                 qp->cycles = 0;
506         }
507
508         for (i = 0; i < nb_iterations; i++) {
509                 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
510                         qp = &qps[qp_id];
511                         qp->total_enqueue = 0;
512                         qp->total_dequeue = 0;
513                 }
514                 do {
515                         update = false;
516                         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
517                                 qp = &qps[qp_id];
518                                 if (qp->total_dequeue < actual_jobs) {
519                                         qp->start = rte_rdtsc_precise();
520                                         struct rte_regex_ops **
521                                                 cur_ops_to_enqueue = qp->ops +
522                                                 qp->total_enqueue;
523
524                                         if (actual_jobs - qp->total_enqueue)
525                                                 qp->total_enqueue +=
526                                                 rte_regexdev_enqueue_burst
527                                                         (dev_id,
528                                                         qp_id_base + qp_id,
529                                                         cur_ops_to_enqueue,
530                                                         actual_jobs -
531                                                         qp->total_enqueue);
532                                 }
533                         }
534                         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
535                                 qp = &qps[qp_id];
536                                 if (qp->total_dequeue < actual_jobs) {
537                                         struct rte_regex_ops **
538                                                 cur_ops_to_dequeue = qp->ops +
539                                                 qp->total_dequeue;
540
541                                         qp->total_dequeue +=
542                                                 rte_regexdev_dequeue_burst
543                                                         (dev_id,
544                                                         qp_id_base + qp_id,
545                                                         cur_ops_to_dequeue,
546                                                         qp->total_enqueue -
547                                                         qp->total_dequeue);
548                                         qp->cycles +=
549                                              (rte_rdtsc_precise() - qp->start);
550                                         update = true;
551                                 }
552                         }
553                 } while (update);
554         }
555         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
556                 qp = &qps[qp_id];
557                 time = (long double)qp->cycles / rte_get_timer_hz();
558                 printf("Core=%u QP=%u Job=%ld Bytes Time=%Lf sec Perf=%Lf "
559                        "Gbps\n", rte_lcore_id(), qp_id + qp_id_base,
560                        job_len, time,
561                        (((double)actual_jobs * job_len * nb_iterations * 8)
562                        / time) / 1000000000.0);
563         }
564
565         if (rgxc->perf_mode)
566                 goto end;
567         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
568                 printf("\n############ Core=%u QP=%u ############\n",
569                        rte_lcore_id(), qp_id + qp_id_base);
570                 qp = &qps[qp_id];
571                 /* Log results per job. */
572                 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
573                         nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
574                         printf("Job id %"PRIu64" number of matches = %d\n",
575                                         qp->ops[d_ind]->user_id, nb_matches);
576                         qp->total_matches += nb_matches;
577                         match = qp->ops[d_ind % actual_jobs]->matches;
578                         for (i = 0; i < nb_matches; i++) {
579                                 printf("match %d, rule = %d, "
580                                        "start = %d,len = %d\n",
581                                        i, match->rule_id, match->start_offset,
582                                        match->len);
583                                 match++;
584                         }
585                 }
586                 printf("Total matches = %d\n", qp->total_matches);
587                 printf("All Matches:\n");
588                 /* Log absolute results. */
589                 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
590                         nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
591                         qp->total_matches += nb_matches;
592                         match = qp->ops[d_ind % actual_jobs]->matches;
593                         for (i = 0; i < nb_matches; i++) {
594                                 printf("start = %ld, len = %d, rule = %d\n",
595                                                 match->start_offset +
596                                                 d_ind * job_len,
597                                                 match->len, match->rule_id);
598                                 match++;
599                         }
600                 }
601         }
602 end:
603         for (qp_id = 0; qp_id < qps_used; qp_id++) {
604                 qp = &qps[qp_id];
605                 for (i = 0; i < actual_jobs && qp->ops; i++)
606                         rte_free(qp->ops[i]);
607                 rte_free(qp->ops);
608                 qp->ops = NULL;
609                 for (i = 0; i < actual_jobs && qp->jobs_ctx; i++)
610                         rte_pktmbuf_free(qp->jobs_ctx[i].mbuf);
611                 rte_free(qp->jobs_ctx);
612                 qp->jobs_ctx = NULL;
613                 rte_free(qp->buf);
614                 qp->buf = NULL;
615         }
616         if (mbuf_mp)
617                 rte_mempool_free(mbuf_mp);
618         rte_free(qps);
619         return res;
620 }
621
622 static int
623 distribute_qps_to_lcores(uint32_t nb_cores, uint32_t nb_qps,
624                          struct qps_per_lcore **qpl)
625 {
626         int socket;
627         unsigned lcore_id;
628         uint32_t i;
629         uint16_t min_qp_id;
630         uint16_t max_qp_id;
631         struct qps_per_lcore *qps_per_lcore;
632         uint32_t detected_lcores;
633
634         if (nb_qps < nb_cores) {
635                 nb_cores = nb_qps;
636                 printf("Reducing number of cores to number of QPs (%u)\n",
637                        nb_cores);
638         }
639         /* Allocate qps_per_lcore array */
640         qps_per_lcore =
641                 rte_malloc(NULL, sizeof(*qps_per_lcore) * nb_cores, 0);
642         if (!qps_per_lcore)
643                 rte_exit(EXIT_FAILURE, "Failed to create qps_per_lcore array\n");
644         *qpl = qps_per_lcore;
645         detected_lcores = 0;
646         min_qp_id = 0;
647
648         RTE_LCORE_FOREACH_WORKER(lcore_id) {
649                 if (detected_lcores >= nb_cores)
650                         break;
651                 qps_per_lcore[detected_lcores].lcore_id = lcore_id;
652                 socket = rte_lcore_to_socket_id(lcore_id);
653                 if (socket == SOCKET_ID_ANY)
654                         socket = 0;
655                 qps_per_lcore[detected_lcores].socket = socket;
656                 qps_per_lcore[detected_lcores].qp_id_base = min_qp_id;
657                 max_qp_id = min_qp_id + nb_qps / nb_cores - 1;
658                 if (nb_qps % nb_cores > detected_lcores)
659                         max_qp_id++;
660                 qps_per_lcore[detected_lcores].nb_qps = max_qp_id -
661                                                         min_qp_id + 1;
662                 min_qp_id = max_qp_id + 1;
663                 detected_lcores++;
664         }
665         if (detected_lcores != nb_cores)
666                 return -1;
667
668         for (i = 0; i < detected_lcores; i++) {
669                 printf("===> Core %d: allocated queues: ",
670                        qps_per_lcore[i].lcore_id);
671                 min_qp_id = qps_per_lcore[i].qp_id_base;
672                 max_qp_id =
673                         qps_per_lcore[i].qp_id_base + qps_per_lcore[i].nb_qps;
674                 while (min_qp_id < max_qp_id) {
675                         printf("%u ", min_qp_id);
676                         min_qp_id++;
677                 }
678                 printf("\n");
679         }
680         return 0;
681 }
682
683 int
684 main(int argc, char **argv)
685 {
686         char rules_file[MAX_FILE_NAME];
687         char data_file[MAX_FILE_NAME];
688         uint32_t nb_jobs = 0;
689         bool perf_mode = 0;
690         uint32_t nb_iterations = 0;
691         int ret;
692         uint16_t nb_max_payload = 0;
693         uint8_t nb_max_matches = 0;
694         uint32_t nb_qps = 1;
695         char *data_buf;
696         long data_len;
697         long job_len;
698         uint32_t nb_lcores = 1, nb_segs = 1;
699         struct regex_conf *rgxc;
700         uint32_t i;
701         struct qps_per_lcore *qps_per_lcore;
702
703         /* Init EAL. */
704         ret = rte_eal_init(argc, argv);
705         if (ret < 0)
706                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
707         argc -= ret;
708         argv += ret;
709         if (argc > 1)
710                 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
711                                 &perf_mode, &nb_iterations, &nb_qps,
712                                 &nb_lcores, &nb_segs);
713
714         if (nb_qps == 0)
715                 rte_exit(EXIT_FAILURE, "Number of QPs must be greater than 0\n");
716         if (nb_lcores == 0)
717                 rte_exit(EXIT_FAILURE, "Number of lcores must be greater than 0\n");
718         if (distribute_qps_to_lcores(nb_lcores, nb_qps, &qps_per_lcore) < 0)
719                 rte_exit(EXIT_FAILURE, "Failed to distribute queues to lcores!\n");
720         ret = init_port(&nb_max_payload, rules_file,
721                         &nb_max_matches, nb_qps);
722         if (ret < 0)
723                 rte_exit(EXIT_FAILURE, "init port failed\n");
724
725         data_len = read_file(data_file, &data_buf);
726         if (data_len <= 0)
727                 rte_exit(EXIT_FAILURE, "Error, can't read file, or file is empty.\n");
728
729         job_len = data_len / nb_jobs;
730         if (job_len == 0)
731                 rte_exit(EXIT_FAILURE, "Error, To many jobs, for the given input.\n");
732
733         if (job_len > nb_max_payload)
734                 rte_exit(EXIT_FAILURE, "Error, not enough jobs to cover input.\n");
735
736         rgxc = rte_malloc(NULL, sizeof(*rgxc) * nb_lcores, 0);
737         if (!rgxc)
738                 rte_exit(EXIT_FAILURE, "Failed to create Regex Conf\n");
739         for (i = 0; i < nb_lcores; i++) {
740                 rgxc[i] = (struct regex_conf){
741                         .nb_jobs = nb_jobs,
742                         .nb_segs = nb_segs,
743                         .perf_mode = perf_mode,
744                         .nb_iterations = nb_iterations,
745                         .nb_max_matches = nb_max_matches,
746                         .nb_qps = qps_per_lcore[i].nb_qps,
747                         .qp_id_base = qps_per_lcore[i].qp_id_base,
748                         .data_buf = data_buf,
749                         .data_len = data_len,
750                         .job_len = job_len,
751                 };
752                 rte_eal_remote_launch(run_regex, &rgxc[i],
753                                       qps_per_lcore[i].lcore_id);
754         }
755         rte_eal_mp_wait_lcore();
756         rte_free(data_buf);
757         rte_free(rgxc);
758         rte_free(qps_per_lcore);
759         return EXIT_SUCCESS;
760 }