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,
39 usage(const char *prog_name)
41 printf("%s [EAL options] --\n"
42 " --rules NAME: precompiled rules file\n"
43 " --data NAME: data file to use\n"
44 " --nb_jobs: number of jobs to use\n"
45 " --perf N: only outputs the performance data\n"
46 " --nb_iter N: number of iteration to run\n",
51 args_parse(int argc, char **argv, char *rules_file, char *data_file,
52 uint32_t *nb_jobs, bool *perf_mode, uint32_t *nb_iterations)
58 static struct option lgopts[] = {
59 { "help", 0, 0, ARG_HELP},
60 /* Rules database file to load. */
61 { "rules", 1, 0, ARG_RULES_FILE_NAME},
62 /* Data file to load. */
63 { "data", 1, 0, ARG_DATA_FILE_NAME},
64 /* Number of jobs to create. */
65 { "nb_jobs", 1, 0, ARG_NUM_OF_JOBS},
67 { "perf", 0, 0, ARG_PERF_MODE},
68 /* Number of iterations to run with perf test */
69 { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
75 while ((opt = getopt_long(argc, argvopt, "",
76 lgopts, &opt_idx)) != EOF) {
78 case ARG_RULES_FILE_NAME:
79 len = strnlen(optarg, MAX_FILE_NAME - 1);
80 if (len == MAX_FILE_NAME)
81 rte_exit(EXIT_FAILURE,
82 "Rule file name to long max %d\n",
84 strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
86 case ARG_DATA_FILE_NAME:
87 len = strnlen(optarg, MAX_FILE_NAME - 1);
88 if (len == MAX_FILE_NAME)
89 rte_exit(EXIT_FAILURE,
90 "Data file name to long max %d\n",
92 strncpy(data_file, optarg, MAX_FILE_NAME - 1);
95 *nb_jobs = atoi(optarg);
100 case ARG_NUM_OF_ITERATIONS:
101 *nb_iterations = atoi(optarg);
104 usage("RegEx test app");
107 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
108 usage("RegEx test app");
109 rte_exit(EXIT_FAILURE, "Invalid option\n");
119 read_file(char *file, char **buf)
126 fp = fopen(file, "r");
129 if (fseek(fp, 0L, SEEK_END) == 0) {
135 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
140 if (fseek(fp, 0L, SEEK_SET) != 0) {
144 read_len = fread(*buf, sizeof(char), buf_len, fp);
145 if (read_len != (unsigned long)buf_len) {
153 printf("Error, can't open file %s\n, err = %d", file, res);
162 init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
163 uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
169 struct rte_regexdev_info info;
170 struct rte_regexdev_config dev_conf = {
174 struct rte_regexdev_qp_conf qp_conf = {
176 .qp_conf_flags = RTE_REGEX_QUEUE_PAIR_CFG_OOS_F,
180 num_devs = rte_regexdev_count();
182 printf("Error, no devices detected.\n");
186 *mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
187 0, MBUF_SIZE, rte_socket_id());
188 if (*mbuf_mp == NULL) {
189 printf("Error, can't create memory pool\n");
194 rules_len = read_file(rules_file, &rules);
196 printf("Error, can't read rules files.\n");
201 for (id = 0; id < num_devs; id++) {
202 res = rte_regexdev_info_get(id, &info);
204 printf("Error, can't get device info.\n");
207 printf(":: initializing dev: %d\n", id);
208 *nb_max_matches = info.max_matches;
209 *nb_max_payload = info.max_payload_size;
210 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
211 dev_conf.dev_cfg_flags |= RTE_REGEXDEV_CFG_MATCH_AS_END_F;
212 dev_conf.nb_max_matches = info.max_matches;
213 dev_conf.nb_rules_per_group = info.max_rules_per_group;
214 dev_conf.rule_db_len = rules_len;
215 dev_conf.rule_db = rules;
216 res = rte_regexdev_configure(id, &dev_conf);
218 printf("Error, can't configure device %d.\n", id);
221 res = rte_regexdev_queue_pair_setup(id, 0, &qp_conf);
223 printf("Error, can't setup queue pair for device %d.\n",
227 printf(":: initializing device: %d done\n", id);
235 rte_mempool_free(*mbuf_mp);
240 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
245 run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
246 uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
247 char *data_file, uint8_t nb_max_matches)
252 uint32_t actual_jobs = 0;
254 struct rte_regex_ops **ops;
258 struct rte_regexdev_match *match;
260 unsigned long d_ind = 0;
261 struct rte_mbuf_ext_shared_info shinfo;
262 uint32_t total_enqueue = 0;
263 uint32_t total_dequeue = 0;
264 uint32_t total_matches = 0;
270 shinfo.free_cb = extbuf_free_cb;
272 ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
274 printf("Error, can't allocate memory for ops.\n");
278 /* Allocate the jobs and assign each job with an mbuf. */
279 for (i = 0; i < nb_jobs; i++) {
280 ops[i] = rte_malloc(NULL, sizeof(*ops[0]) + nb_max_matches *
281 sizeof(struct rte_regexdev_match), 0);
283 printf("Error, can't allocate memory for op.\n");
287 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
289 printf("Error, can't attach mbuf.\n");
295 buf_len = read_file(data_file, &buf);
297 printf("Error, can't read file, or file is empty.\n");
302 job_len = buf_len / nb_jobs;
304 printf("Error, To many jobs, for the given input.\n");
309 if (job_len > nb_max_payload) {
310 printf("Error, not enough jobs to cover input.\n");
315 /* Assign each mbuf with the data to handle. */
316 for (i = 0; (pos < buf_len) && (i < nb_jobs) ; i++) {
317 long act_job_len = RTE_MIN(job_len, buf_len - pos);
318 rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
319 act_job_len, &shinfo);
320 ops[i]->mbuf->data_len = job_len;
321 ops[i]->mbuf->pkt_len = act_job_len;
323 ops[i]->group_id0 = 1;
329 for (i = 0; i < nb_iterations; i++) {
332 while (total_dequeue < actual_jobs) {
333 struct rte_regex_ops **cur_ops_to_enqueue = ops +
335 struct rte_regex_ops **cur_ops_to_dequeue = ops +
338 if (actual_jobs - total_enqueue)
339 total_enqueue += rte_regexdev_enqueue_burst
340 (dev_id, qp_id, cur_ops_to_enqueue,
341 actual_jobs - total_enqueue);
343 total_dequeue += rte_regexdev_dequeue_burst
344 (dev_id, qp_id, cur_ops_to_dequeue,
345 total_enqueue - total_dequeue);
349 time = ((double)end - start) / CLOCKS_PER_SEC;
350 printf("Job len = %ld Bytes\n", job_len);
351 printf("Time = %lf sec\n", time);
352 printf("Perf = %lf Gbps\n",
353 (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
357 /* Log results per job. */
358 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
359 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
360 printf("Job id %"PRIu64" number of matches = %d\n",
361 ops[d_ind]->user_id, nb_matches);
362 total_matches += nb_matches;
363 match = ops[d_ind % actual_jobs]->matches;
364 for (i = 0; i < nb_matches; i++) {
365 printf("match %d, rule = %d, start = %d,len = %d\n",
366 i, match->rule_id, match->start_offset,
371 printf("Total matches = %d\n", total_matches);
372 printf("All Matches:\n");
374 /* Log absolute results. */
375 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
376 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
377 total_matches += nb_matches;
378 match = ops[d_ind % actual_jobs]->matches;
379 for (i = 0; i < nb_matches; i++) {
380 printf("start = %ld, len = %d, rule = %d\n",
381 match->start_offset + d_ind * job_len,
382 match->len, match->rule_id);
388 for (i = 0; i < actual_jobs; i++) {
391 rte_pktmbuf_free(ops[i]->mbuf);
402 main(int argc, char **argv)
404 char rules_file[MAX_FILE_NAME];
405 char data_file[MAX_FILE_NAME];
406 struct rte_mempool *mbuf_mp = NULL;
407 uint32_t nb_jobs = 0;
408 uint16_t nb_max_payload = 0;
410 uint32_t nb_iterations = 0;
411 uint8_t nb_max_matches = 0;
414 ret = rte_eal_init(argc, argv);
416 rte_exit(EXIT_FAILURE, "EAL init failed\n");
420 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
421 &perf_mode, &nb_iterations);
423 ret = init_port(&mbuf_mp, nb_jobs, &nb_max_payload, rules_file,
426 rte_exit(EXIT_FAILURE, "init port failed\n");
427 ret = run_regex(mbuf_mp, nb_jobs, nb_max_payload, perf_mode,
428 nb_iterations, data_file, nb_max_matches);
430 rte_mempool_free(mbuf_mp);
431 rte_exit(EXIT_FAILURE, "RegEx function failed\n");
433 rte_mempool_free(mbuf_mp);