d225267b1612758bd5b04898160e87fdb9c19a48
[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 };
38
39 struct job_ctx {
40         struct rte_mbuf *mbuf;
41 };
42
43 struct qp_params {
44         uint32_t total_enqueue;
45         uint32_t total_dequeue;
46         uint32_t total_matches;
47         struct rte_regex_ops **ops;
48         struct job_ctx *jobs_ctx;
49         char *buf;
50 };
51
52 static void
53 usage(const char *prog_name)
54 {
55         printf("%s [EAL options] --\n"
56                 " --rules NAME: precompiled rules file\n"
57                 " --data NAME: data file to use\n"
58                 " --nb_jobs: number of jobs to use\n"
59                 " --perf N: only outputs the performance data\n"
60                 " --nb_iter N: number of iteration to run\n"
61                 " --nb_qps N: number of queues to use\n",
62                 prog_name);
63 }
64
65 static void
66 args_parse(int argc, char **argv, char *rules_file, char *data_file,
67            uint32_t *nb_jobs, bool *perf_mode, uint32_t *nb_iterations,
68            uint32_t *nb_qps)
69 {
70         char **argvopt;
71         int opt;
72         int opt_idx;
73         size_t len;
74         static struct option lgopts[] = {
75                 { "help",  0, 0, ARG_HELP},
76                 /* Rules database file to load. */
77                 { "rules",  1, 0, ARG_RULES_FILE_NAME},
78                 /* Data file to load. */
79                 { "data",  1, 0, ARG_DATA_FILE_NAME},
80                 /* Number of jobs to create. */
81                 { "nb_jobs",  1, 0, ARG_NUM_OF_JOBS},
82                 /* Perf test only */
83                 { "perf", 0, 0, ARG_PERF_MODE},
84                 /* Number of iterations to run with perf test */
85                 { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
86                 /* Number of QPs. */
87                 { "nb_qps", 1, 0, ARG_NUM_OF_QPS},
88                 /* End of options */
89                 { 0, 0, 0, 0 }
90         };
91
92         argvopt = argv;
93         while ((opt = getopt_long(argc, argvopt, "",
94                                 lgopts, &opt_idx)) != EOF) {
95                 switch (opt) {
96                 case ARG_RULES_FILE_NAME:
97                         len = strnlen(optarg, MAX_FILE_NAME - 1);
98                         if (len == MAX_FILE_NAME)
99                                 rte_exit(EXIT_FAILURE,
100                                          "Rule file name to long max %d\n",
101                                          MAX_FILE_NAME - 1);
102                         strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
103                         break;
104                 case ARG_DATA_FILE_NAME:
105                         len = strnlen(optarg, MAX_FILE_NAME - 1);
106                         if (len == MAX_FILE_NAME)
107                                 rte_exit(EXIT_FAILURE,
108                                          "Data file name to long max %d\n",
109                                          MAX_FILE_NAME - 1);
110                         strncpy(data_file, optarg, MAX_FILE_NAME - 1);
111                         break;
112                 case ARG_NUM_OF_JOBS:
113                         *nb_jobs = atoi(optarg);
114                         break;
115                 case ARG_PERF_MODE:
116                         *perf_mode = true;
117                         break;
118                 case ARG_NUM_OF_ITERATIONS:
119                         *nb_iterations = atoi(optarg);
120                         break;
121                 case ARG_NUM_OF_QPS:
122                         *nb_qps = atoi(optarg);
123                         break;
124                 case ARG_HELP:
125                         usage("RegEx test app");
126                         break;
127                 default:
128                         fprintf(stderr, "Invalid option: %s\n", argv[optind]);
129                         usage("RegEx test app");
130                         rte_exit(EXIT_FAILURE, "Invalid option\n");
131                         break;
132                 }
133         }
134
135         if (!perf_mode)
136                 *nb_iterations = 1;
137 }
138
139 static long
140 read_file(char *file, char **buf)
141 {
142         FILE *fp;
143         long buf_len = 0;
144         size_t read_len;
145         int res = 0;
146
147         fp = fopen(file, "r");
148         if (!fp)
149                 return -EIO;
150         if (fseek(fp, 0L, SEEK_END) == 0) {
151                 buf_len = ftell(fp);
152                 if (buf_len == -1) {
153                         res = EIO;
154                         goto error;
155                 }
156                 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
157                 if (!*buf) {
158                         res = ENOMEM;
159                         goto error;
160                 }
161                 if (fseek(fp, 0L, SEEK_SET) != 0) {
162                         res = EIO;
163                         goto error;
164                 }
165                 read_len = fread(*buf, sizeof(char), buf_len, fp);
166                 if (read_len != (unsigned long)buf_len) {
167                         res = EIO;
168                         goto error;
169                 }
170         }
171         fclose(fp);
172         return buf_len;
173 error:
174         printf("Error, can't open file %s\n, err = %d", file, res);
175         if (fp)
176                 fclose(fp);
177         if (*buf)
178                 rte_free(*buf);
179         return -res;
180 }
181
182 static int
183 init_port(uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches,
184           uint32_t nb_qps)
185 {
186         uint16_t id;
187         uint16_t qp_id;
188         uint16_t num_devs;
189         char *rules = NULL;
190         long rules_len;
191         struct rte_regexdev_info info;
192         struct rte_regexdev_config dev_conf = {
193                 .nb_queue_pairs = nb_qps,
194                 .nb_groups = 1,
195         };
196         struct rte_regexdev_qp_conf qp_conf = {
197                 .nb_desc = 1024,
198                 .qp_conf_flags = 0,
199         };
200         int res = 0;
201
202         num_devs = rte_regexdev_count();
203         if (num_devs == 0) {
204                 printf("Error, no devices detected.\n");
205                 return -EINVAL;
206         }
207
208         rules_len = read_file(rules_file, &rules);
209         if (rules_len < 0) {
210                 printf("Error, can't read rules files.\n");
211                 res = -EIO;
212                 goto error;
213         }
214
215         for (id = 0; id < num_devs; id++) {
216                 res = rte_regexdev_info_get(id, &info);
217                 if (res != 0) {
218                         printf("Error, can't get device info.\n");
219                         goto error;
220                 }
221                 printf(":: initializing dev: %d\n", id);
222                 *nb_max_matches = info.max_matches;
223                 *nb_max_payload = info.max_payload_size;
224                 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
225                         dev_conf.dev_cfg_flags |=
226                         RTE_REGEXDEV_CFG_MATCH_AS_END_F;
227                 dev_conf.nb_max_matches = info.max_matches;
228                 dev_conf.nb_rules_per_group = info.max_rules_per_group;
229                 dev_conf.rule_db_len = rules_len;
230                 dev_conf.rule_db = rules;
231                 res = rte_regexdev_configure(id, &dev_conf);
232                 if (res < 0) {
233                         printf("Error, can't configure device %d.\n", id);
234                         goto error;
235                 }
236                 if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
237                         qp_conf.qp_conf_flags |=
238                         RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
239                 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
240                         res = rte_regexdev_queue_pair_setup(id, qp_id,
241                                                             &qp_conf);
242                         if (res < 0) {
243                                 printf("Error, can't setup queue pair %u for "
244                                        "device %d.\n", qp_id, id);
245                                 goto error;
246                         }
247                 }
248                 printf(":: initializing device: %d done\n", id);
249         }
250         rte_free(rules);
251         return 0;
252 error:
253         if (rules)
254                 rte_free(rules);
255         return res;
256 }
257
258 static void
259 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
260 {
261 }
262
263 static int
264 run_regex(uint32_t nb_jobs,
265           uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
266           char *data_file, uint8_t nb_max_matches, uint32_t nb_qps)
267 {
268         char *buf = NULL;
269         long buf_len = 0;
270         long job_len = 0;
271         uint32_t actual_jobs = 0;
272         uint32_t i;
273         uint16_t qp_id;
274         uint16_t dev_id = 0;
275         uint8_t nb_matches;
276         struct rte_regexdev_match *match;
277         long pos;
278         unsigned long d_ind = 0;
279         struct rte_mbuf_ext_shared_info shinfo;
280         int res = 0;
281         time_t start;
282         time_t end;
283         double time;
284         struct rte_mempool *mbuf_mp;
285         struct qp_params *qp;
286         struct qp_params *qps = NULL;
287         bool update;
288         uint16_t qps_used = 0;
289
290         shinfo.free_cb = extbuf_free_cb;
291         mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs * nb_qps, 0,
292                         0, MBUF_SIZE, rte_socket_id());
293         if (mbuf_mp == NULL) {
294                 printf("Error, can't create memory pool\n");
295                 return -ENOMEM;
296         }
297
298         qps = rte_malloc(NULL, sizeof(*qps) * nb_qps, 0);
299         if (!qps) {
300                 printf("Error, can't allocate memory for QPs\n");
301                 res = -ENOMEM;
302                 goto end;
303         }
304
305         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
306                 struct rte_regex_ops **ops;
307                 struct job_ctx *jobs_ctx;
308
309                 qps_used++;
310                 qp = &qps[qp_id];
311                 qp->jobs_ctx = NULL;
312                 qp->buf = NULL;
313                 qp->ops = ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
314                 if (!ops) {
315                         printf("Error, can't allocate memory for ops.\n");
316                         res = -ENOMEM;
317                         goto end;
318                 }
319
320                 qp->jobs_ctx = jobs_ctx =
321                         rte_malloc(NULL, sizeof(*jobs_ctx) * nb_jobs, 0);
322                 if (!jobs_ctx) {
323                         printf("Error, can't allocate memory for jobs_ctx.\n");
324                         res = -ENOMEM;
325                         goto end;
326                 }
327
328                 /* Allocate the jobs and assign each job with an mbuf. */
329                 for (i = 0; i < nb_jobs; i++) {
330                         ops[i] = rte_malloc(NULL, sizeof(*ops[0]) +
331                                         nb_max_matches *
332                                         sizeof(struct rte_regexdev_match), 0);
333                         if (!ops[i]) {
334                                 printf("Error, can't allocate "
335                                        "memory for op.\n");
336                                 res = -ENOMEM;
337                                 goto end;
338                         }
339                         ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
340                         if (!ops[i]->mbuf) {
341                                 printf("Error, can't attach mbuf.\n");
342                                 res = -ENOMEM;
343                                 goto end;
344                         }
345                 }
346
347                 buf_len = read_file(data_file, &buf);
348                 if (buf_len <= 0) {
349                         printf("Error, can't read file, or file is empty.\n");
350                         res = -EXIT_FAILURE;
351                         goto end;
352                 }
353
354                 job_len = buf_len / nb_jobs;
355                 if (job_len == 0) {
356                         printf("Error, To many jobs, for the given input.\n");
357                         res = -EXIT_FAILURE;
358                         goto end;
359                 }
360
361                 if (job_len > nb_max_payload) {
362                         printf("Error, not enough jobs to cover input.\n");
363                         res = -EXIT_FAILURE;
364                         goto end;
365                 }
366
367                 /* Assign each mbuf with the data to handle. */
368                 actual_jobs = 0;
369                 pos = 0;
370                 for (i = 0; (pos < buf_len) && (i < nb_jobs) ; i++) {
371                         long act_job_len = RTE_MIN(job_len, buf_len - pos);
372                         rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
373                                         act_job_len, &shinfo);
374                         jobs_ctx[i].mbuf = ops[i]->mbuf;
375                         ops[i]->mbuf->data_len = job_len;
376                         ops[i]->mbuf->pkt_len = act_job_len;
377                         ops[i]->user_id = i;
378                         ops[i]->group_id0 = 1;
379                         pos += act_job_len;
380                         actual_jobs++;
381                 }
382
383                 qp->buf = buf;
384                 qp->total_matches = 0;
385         }
386
387         start = clock();
388         for (i = 0; i < nb_iterations; i++) {
389                 for (qp_id = 0; qp_id < nb_qps; qp_id++) {
390                         qp = &qps[qp_id];
391                         qp->total_enqueue = 0;
392                         qp->total_dequeue = 0;
393                 }
394                 do {
395                         update = false;
396                         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
397                                 qp = &qps[qp_id];
398                                 if (qp->total_dequeue < actual_jobs) {
399                                         struct rte_regex_ops **
400                                                 cur_ops_to_enqueue = qp->ops +
401                                                 qp->total_enqueue;
402
403                                         if (actual_jobs - qp->total_enqueue)
404                                                 qp->total_enqueue +=
405                                                 rte_regexdev_enqueue_burst
406                                                         (dev_id,
407                                                         qp_id,
408                                                         cur_ops_to_enqueue,
409                                                         actual_jobs -
410                                                         qp->total_enqueue);
411                                 }
412                         }
413                         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
414                                 qp = &qps[qp_id];
415                                 if (qp->total_dequeue < actual_jobs) {
416                                         struct rte_regex_ops **
417                                                 cur_ops_to_dequeue = qp->ops +
418                                                 qp->total_dequeue;
419
420                                         qp->total_dequeue +=
421                                                 rte_regexdev_dequeue_burst
422                                                         (dev_id,
423                                                         qp_id,
424                                                         cur_ops_to_dequeue,
425                                                         qp->total_enqueue -
426                                                         qp->total_dequeue);
427                                         update = true;
428                                 }
429                         }
430                 } while (update);
431         }
432         end = clock();
433         time = ((double)end - start) / CLOCKS_PER_SEC;
434         printf("Job len = %ld Bytes\n",  job_len);
435         printf("Time = %lf sec\n",  time);
436         printf("Perf = %lf Gbps\n",
437                (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
438                 1000000000.0);
439
440         if (perf_mode)
441                 goto end;
442         for (qp_id = 0; qp_id < nb_qps; qp_id++) {
443                 printf("\n############ QP id=%u ############\n", qp_id);
444                 qp = &qps[qp_id];
445                 /* Log results per job. */
446                 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
447                         nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
448                         printf("Job id %"PRIu64" number of matches = %d\n",
449                                         qp->ops[d_ind]->user_id, nb_matches);
450                         qp->total_matches += nb_matches;
451                         match = qp->ops[d_ind % actual_jobs]->matches;
452                         for (i = 0; i < nb_matches; i++) {
453                                 printf("match %d, rule = %d, "
454                                        "start = %d,len = %d\n",
455                                        i, match->rule_id, match->start_offset,
456                                        match->len);
457                                 match++;
458                         }
459                 }
460                 printf("Total matches = %d\n", qp->total_matches);
461                 printf("All Matches:\n");
462                 /* Log absolute results. */
463                 for (d_ind = 0; d_ind < qp->total_dequeue; d_ind++) {
464                         nb_matches = qp->ops[d_ind % actual_jobs]->nb_matches;
465                         qp->total_matches += nb_matches;
466                         match = qp->ops[d_ind % actual_jobs]->matches;
467                         for (i = 0; i < nb_matches; i++) {
468                                 printf("start = %ld, len = %d, rule = %d\n",
469                                                 match->start_offset +
470                                                 d_ind * job_len,
471                                                 match->len, match->rule_id);
472                                 match++;
473                         }
474                 }
475         }
476 end:
477         for (qp_id = 0; qp_id < qps_used; qp_id++) {
478                 qp = &qps[qp_id];
479                 for (i = 0; i < actual_jobs && qp->ops; i++)
480                         rte_free(qp->ops[i]);
481                 rte_free(qp->ops);
482                 qp->ops = NULL;
483                 for (i = 0; i < actual_jobs && qp->jobs_ctx; i++)
484                         rte_pktmbuf_free(qp->jobs_ctx[i].mbuf);
485                 rte_free(qp->jobs_ctx);
486                 qp->jobs_ctx = NULL;
487                 rte_free(qp->buf);
488                 qp->buf = NULL;
489         }
490         if (mbuf_mp)
491                 rte_mempool_free(mbuf_mp);
492         rte_free(qps);
493         return res;
494 }
495
496 int
497 main(int argc, char **argv)
498 {
499         char rules_file[MAX_FILE_NAME];
500         char data_file[MAX_FILE_NAME];
501         uint32_t nb_jobs = 0;
502         bool perf_mode = 0;
503         uint32_t nb_iterations = 0;
504         int ret;
505         uint16_t nb_max_payload = 0;
506         uint8_t nb_max_matches = 0;
507         uint32_t nb_qps = 1;
508
509         /* Init EAL. */
510         ret = rte_eal_init(argc, argv);
511         if (ret < 0)
512                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
513         argc -= ret;
514         argv += ret;
515         if (argc > 1)
516                 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
517                                 &perf_mode, &nb_iterations, &nb_qps);
518
519         if (nb_qps == 0)
520                 rte_exit(EXIT_FAILURE, "Number of QPs must be greater than 0\n");
521         ret = init_port(&nb_max_payload, rules_file,
522                         &nb_max_matches, nb_qps);
523         if (ret < 0)
524                 rte_exit(EXIT_FAILURE, "init port failed\n");
525         ret = run_regex(nb_jobs, nb_max_payload, perf_mode,
526                         nb_iterations, data_file, nb_max_matches, nb_qps);
527         if (ret < 0) {
528                 rte_exit(EXIT_FAILURE, "RegEx function failed\n");
529         }
530         return EXIT_SUCCESS;
531 }