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 struct rte_mbuf *mbuf;
43 usage(const char *prog_name)
45 printf("%s [EAL options] --\n"
46 " --rules NAME: precompiled rules file\n"
47 " --data NAME: data file to use\n"
48 " --nb_jobs: number of jobs to use\n"
49 " --perf N: only outputs the performance data\n"
50 " --nb_iter N: number of iteration to run\n",
55 args_parse(int argc, char **argv, char *rules_file, char *data_file,
56 uint32_t *nb_jobs, bool *perf_mode, uint32_t *nb_iterations)
62 static struct option lgopts[] = {
63 { "help", 0, 0, ARG_HELP},
64 /* Rules database file to load. */
65 { "rules", 1, 0, ARG_RULES_FILE_NAME},
66 /* Data file to load. */
67 { "data", 1, 0, ARG_DATA_FILE_NAME},
68 /* Number of jobs to create. */
69 { "nb_jobs", 1, 0, ARG_NUM_OF_JOBS},
71 { "perf", 0, 0, ARG_PERF_MODE},
72 /* Number of iterations to run with perf test */
73 { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
79 while ((opt = getopt_long(argc, argvopt, "",
80 lgopts, &opt_idx)) != EOF) {
82 case ARG_RULES_FILE_NAME:
83 len = strnlen(optarg, MAX_FILE_NAME - 1);
84 if (len == MAX_FILE_NAME)
85 rte_exit(EXIT_FAILURE,
86 "Rule file name to long max %d\n",
88 strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
90 case ARG_DATA_FILE_NAME:
91 len = strnlen(optarg, MAX_FILE_NAME - 1);
92 if (len == MAX_FILE_NAME)
93 rte_exit(EXIT_FAILURE,
94 "Data file name to long max %d\n",
96 strncpy(data_file, optarg, MAX_FILE_NAME - 1);
99 *nb_jobs = atoi(optarg);
104 case ARG_NUM_OF_ITERATIONS:
105 *nb_iterations = atoi(optarg);
108 usage("RegEx test app");
111 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
112 usage("RegEx test app");
113 rte_exit(EXIT_FAILURE, "Invalid option\n");
123 read_file(char *file, char **buf)
130 fp = fopen(file, "r");
133 if (fseek(fp, 0L, SEEK_END) == 0) {
139 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
144 if (fseek(fp, 0L, SEEK_SET) != 0) {
148 read_len = fread(*buf, sizeof(char), buf_len, fp);
149 if (read_len != (unsigned long)buf_len) {
157 printf("Error, can't open file %s\n, err = %d", file, res);
166 init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
167 uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
173 struct rte_regexdev_info info;
174 struct rte_regexdev_config dev_conf = {
178 struct rte_regexdev_qp_conf qp_conf = {
184 num_devs = rte_regexdev_count();
186 printf("Error, no devices detected.\n");
190 *mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
191 0, MBUF_SIZE, rte_socket_id());
192 if (*mbuf_mp == NULL) {
193 printf("Error, can't create memory pool\n");
198 rules_len = read_file(rules_file, &rules);
200 printf("Error, can't read rules files.\n");
205 for (id = 0; id < num_devs; id++) {
206 res = rte_regexdev_info_get(id, &info);
208 printf("Error, can't get device info.\n");
211 printf(":: initializing dev: %d\n", id);
212 *nb_max_matches = info.max_matches;
213 *nb_max_payload = info.max_payload_size;
214 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
215 dev_conf.dev_cfg_flags |= RTE_REGEXDEV_CFG_MATCH_AS_END_F;
216 dev_conf.nb_max_matches = info.max_matches;
217 dev_conf.nb_rules_per_group = info.max_rules_per_group;
218 dev_conf.rule_db_len = rules_len;
219 dev_conf.rule_db = rules;
220 res = rte_regexdev_configure(id, &dev_conf);
222 printf("Error, can't configure device %d.\n", id);
225 if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
226 qp_conf.qp_conf_flags |= RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
227 res = rte_regexdev_queue_pair_setup(id, 0, &qp_conf);
229 printf("Error, can't setup queue pair for device %d.\n",
233 printf(":: initializing device: %d done\n", id);
241 rte_mempool_free(*mbuf_mp);
246 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
251 run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
252 uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
253 char *data_file, uint8_t nb_max_matches)
258 uint32_t actual_jobs = 0;
260 struct rte_regex_ops **ops;
264 struct rte_regexdev_match *match;
266 unsigned long d_ind = 0;
267 struct rte_mbuf_ext_shared_info shinfo;
268 uint32_t total_enqueue = 0;
269 uint32_t total_dequeue = 0;
270 uint32_t total_matches = 0;
275 struct job_ctx *jobs_ctx;
277 shinfo.free_cb = extbuf_free_cb;
279 ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
281 printf("Error, can't allocate memory for ops.\n");
285 jobs_ctx = rte_malloc(NULL, sizeof(struct job_ctx)*nb_jobs, 0);
287 printf("Error, can't allocate memory for jobs_ctx.\n");
291 /* Allocate the jobs and assign each job with an mbuf. */
292 for (i = 0; i < nb_jobs; i++) {
293 ops[i] = rte_malloc(NULL, sizeof(*ops[0]) + nb_max_matches *
294 sizeof(struct rte_regexdev_match), 0);
296 printf("Error, can't allocate memory for op.\n");
300 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
302 printf("Error, can't attach mbuf.\n");
308 buf_len = read_file(data_file, &buf);
310 printf("Error, can't read file, or file is empty.\n");
315 job_len = buf_len / nb_jobs;
317 printf("Error, To many jobs, for the given input.\n");
322 if (job_len > nb_max_payload) {
323 printf("Error, not enough jobs to cover input.\n");
328 /* Assign each mbuf with the data to handle. */
329 for (i = 0; (pos < buf_len) && (i < nb_jobs) ; i++) {
330 long act_job_len = RTE_MIN(job_len, buf_len - pos);
331 rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
332 act_job_len, &shinfo);
333 jobs_ctx[i].mbuf = ops[i]->mbuf;
334 ops[i]->mbuf->data_len = job_len;
335 ops[i]->mbuf->pkt_len = act_job_len;
337 ops[i]->group_id0 = 1;
343 for (i = 0; i < nb_iterations; i++) {
346 while (total_dequeue < actual_jobs) {
347 struct rte_regex_ops **cur_ops_to_enqueue = ops +
349 struct rte_regex_ops **cur_ops_to_dequeue = ops +
352 if (actual_jobs - total_enqueue)
353 total_enqueue += rte_regexdev_enqueue_burst
354 (dev_id, qp_id, cur_ops_to_enqueue,
355 actual_jobs - total_enqueue);
357 total_dequeue += rte_regexdev_dequeue_burst
358 (dev_id, qp_id, cur_ops_to_dequeue,
359 total_enqueue - total_dequeue);
363 time = ((double)end - start) / CLOCKS_PER_SEC;
364 printf("Job len = %ld Bytes\n", job_len);
365 printf("Time = %lf sec\n", time);
366 printf("Perf = %lf Gbps\n",
367 (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
371 /* Log results per job. */
372 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
373 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
374 printf("Job id %"PRIu64" number of matches = %d\n",
375 ops[d_ind]->user_id, nb_matches);
376 total_matches += nb_matches;
377 match = ops[d_ind % actual_jobs]->matches;
378 for (i = 0; i < nb_matches; i++) {
379 printf("match %d, rule = %d, start = %d,len = %d\n",
380 i, match->rule_id, match->start_offset,
385 printf("Total matches = %d\n", total_matches);
386 printf("All Matches:\n");
388 /* Log absolute results. */
389 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
390 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
391 total_matches += nb_matches;
392 match = ops[d_ind % actual_jobs]->matches;
393 for (i = 0; i < nb_matches; i++) {
394 printf("start = %ld, len = %d, rule = %d\n",
395 match->start_offset + d_ind * job_len,
396 match->len, match->rule_id);
402 for (i = 0; i < actual_jobs; i++) {
405 if (jobs_ctx[i].mbuf)
406 rte_pktmbuf_free(jobs_ctx[i].mbuf);
416 main(int argc, char **argv)
418 char rules_file[MAX_FILE_NAME];
419 char data_file[MAX_FILE_NAME];
420 struct rte_mempool *mbuf_mp = NULL;
421 uint32_t nb_jobs = 0;
422 uint16_t nb_max_payload = 0;
424 uint32_t nb_iterations = 0;
425 uint8_t nb_max_matches = 0;
428 ret = rte_eal_init(argc, argv);
430 rte_exit(EXIT_FAILURE, "EAL init failed\n");
434 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
435 &perf_mode, &nb_iterations);
437 ret = init_port(&mbuf_mp, nb_jobs, &nb_max_payload, rules_file,
440 rte_exit(EXIT_FAILURE, "init port failed\n");
441 ret = run_regex(mbuf_mp, nb_jobs, nb_max_payload, perf_mode,
442 nb_iterations, data_file, nb_max_matches);
444 rte_mempool_free(mbuf_mp);
445 rte_exit(EXIT_FAILURE, "RegEx function failed\n");
447 rte_mempool_free(mbuf_mp);