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}
73 while ((opt = getopt_long(argc, argvopt, "",
74 lgopts, &opt_idx)) != EOF) {
76 case ARG_RULES_FILE_NAME:
77 len = strnlen(optarg, MAX_FILE_NAME - 1);
78 if (len == MAX_FILE_NAME)
79 rte_exit(EXIT_FAILURE,
80 "Rule file name to long max %d\n",
82 strncpy(rules_file, optarg, MAX_FILE_NAME - 1);
84 case ARG_DATA_FILE_NAME:
85 len = strnlen(optarg, MAX_FILE_NAME - 1);
86 if (len == MAX_FILE_NAME)
87 rte_exit(EXIT_FAILURE,
88 "Data file name to long max %d\n",
90 strncpy(data_file, optarg, MAX_FILE_NAME - 1);
93 *nb_jobs = atoi(optarg);
98 case ARG_NUM_OF_ITERATIONS:
99 *nb_iterations = atoi(optarg);
102 usage("RegEx test app");
105 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
106 usage("RegEx test app");
107 rte_exit(EXIT_FAILURE, "Invalid option\n");
117 read_file(char *file, char **buf)
124 fp = fopen(file, "r");
127 if (fseek(fp, 0L, SEEK_END) == 0) {
133 *buf = rte_malloc(NULL, sizeof(char) * (buf_len + 1), 4096);
138 if (fseek(fp, 0L, SEEK_SET) != 0) {
142 read_len = fread(*buf, sizeof(char), buf_len, fp);
143 if (read_len != (unsigned long)buf_len) {
151 printf("Error, can't open file %s\n, err = %d", file, res);
160 init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
161 uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
167 struct rte_regexdev_info info;
168 struct rte_regexdev_config dev_conf = {
172 struct rte_regexdev_qp_conf qp_conf = {
174 .qp_conf_flags = RTE_REGEX_QUEUE_PAIR_CFG_OOS_F,
178 num_devs = rte_regexdev_count();
180 printf("Error, no devices detected.\n");
184 *mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
185 0, MBUF_SIZE, rte_socket_id());
186 if (*mbuf_mp == NULL) {
187 printf("Error, can't create memory pool\n");
192 rules_len = read_file(rules_file, &rules);
194 printf("Error, can't read rules files.\n");
199 for (id = 0; id < num_devs; id++) {
200 res = rte_regexdev_info_get(id, &info);
202 printf("Error, can't get device info.\n");
205 printf(":: initializing dev: %d\n", id);
206 *nb_max_matches = info.max_matches;
207 *nb_max_payload = info.max_payload_size;
208 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
209 dev_conf.dev_cfg_flags |= RTE_REGEXDEV_CFG_MATCH_AS_END_F;
210 dev_conf.nb_max_matches = info.max_matches;
211 dev_conf.nb_rules_per_group = info.max_rules_per_group;
212 dev_conf.rule_db_len = rules_len;
213 dev_conf.rule_db = rules;
214 res = rte_regexdev_configure(id, &dev_conf);
216 printf("Error, can't configure device %d.\n", id);
219 res = rte_regexdev_queue_pair_setup(id, 0, &qp_conf);
221 printf("Error, can't setup queue pair for device %d.\n",
225 printf(":: initializing device: %d done\n", id);
233 rte_mempool_free(*mbuf_mp);
238 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
243 run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
244 uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
245 char *data_file, uint8_t nb_max_matches)
250 uint32_t actual_jobs = 0;
252 struct rte_regex_ops **ops;
256 struct rte_regexdev_match *match;
258 unsigned long d_ind = 0;
259 struct rte_mbuf_ext_shared_info shinfo;
260 uint32_t total_enqueue = 0;
261 uint32_t total_dequeue = 0;
262 uint32_t total_matches = 0;
268 shinfo.free_cb = extbuf_free_cb;
270 ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
272 printf("Error, can't allocate memory for ops.\n");
276 /* Allocate the jobs and assign each job with an mbuf. */
277 for (i = 0; i < nb_jobs; i++) {
278 ops[i] = rte_malloc(NULL, sizeof(*ops[0]) + nb_max_matches *
279 sizeof(struct rte_regexdev_match), 0);
281 printf("Error, can't allocate memory for op.\n");
285 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
287 printf("Error, can't attach mbuf.\n");
293 buf_len = read_file(data_file, &buf);
295 printf("Error, can't read file, or file is empty.\n");
300 job_len = buf_len / nb_jobs;
302 printf("Error, To many jobs, for the given input.\n");
307 if (job_len > nb_max_payload) {
308 printf("Error, not enough jobs to cover input.\n");
313 /* Assign each mbuf with the data to handle. */
314 for (i = 0; (pos < buf_len) && (i < nb_jobs) ; i++) {
315 long act_job_len = RTE_MIN(job_len, buf_len - pos);
316 rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
317 act_job_len, &shinfo);
318 ops[i]->mbuf->data_len = job_len;
319 ops[i]->mbuf->pkt_len = act_job_len;
321 ops[i]->group_id0 = 1;
327 for (i = 0; i < nb_iterations; i++) {
330 while (total_dequeue < actual_jobs) {
331 struct rte_regex_ops **cur_ops_to_enqueue = ops +
333 struct rte_regex_ops **cur_ops_to_dequeue = ops +
336 if (actual_jobs - total_enqueue)
337 total_enqueue += rte_regexdev_enqueue_burst
338 (dev_id, qp_id, cur_ops_to_enqueue,
339 actual_jobs - total_enqueue);
341 total_dequeue += rte_regexdev_dequeue_burst
342 (dev_id, qp_id, cur_ops_to_dequeue,
343 total_enqueue - total_dequeue);
347 time = ((double)end - start) / CLOCKS_PER_SEC;
348 printf("Job len = %ld Bytes\n", job_len);
349 printf("Time = %lf sec\n", time);
350 printf("Perf = %lf Gbps\n",
351 (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
355 /* Log results per job. */
356 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
357 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
358 printf("Job id %"PRIu64" number of matches = %d\n",
359 ops[d_ind]->user_id, nb_matches);
360 total_matches += nb_matches;
361 match = ops[d_ind % actual_jobs]->matches;
362 for (i = 0; i < nb_matches; i++) {
363 printf("match %d, rule = %d, start = %d,len = %d\n",
364 i, match->rule_id, match->start_offset,
369 printf("Total matches = %d\n", total_matches);
370 printf("All Matches:\n");
372 /* Log absolute results. */
373 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
374 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
375 total_matches += nb_matches;
376 match = ops[d_ind % actual_jobs]->matches;
377 for (i = 0; i < nb_matches; i++) {
378 printf("start = %ld, len = %d, rule = %d\n",
379 match->start_offset + d_ind * job_len,
380 match->len, match->rule_id);
386 for (i = 0; i < actual_jobs; i++) {
389 rte_pktmbuf_free(ops[i]->mbuf);
400 main(int argc, char **argv)
402 char rules_file[MAX_FILE_NAME];
403 char data_file[MAX_FILE_NAME];
404 struct rte_mempool *mbuf_mp = NULL;
405 uint32_t nb_jobs = 0;
406 uint16_t nb_max_payload = 0;
408 uint32_t nb_iterations = 0;
409 uint8_t nb_max_matches = 0;
412 ret = rte_eal_init(argc, argv);
414 rte_exit(EXIT_FAILURE, "EAL init failed\n");
418 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
419 &perf_mode, &nb_iterations);
421 ret = init_port(&mbuf_mp, nb_jobs, &nb_max_payload, rules_file,
424 rte_exit(EXIT_FAILURE, "init port failed\n");
425 ret = run_regex(mbuf_mp, nb_jobs, nb_max_payload, perf_mode,
426 nb_iterations, data_file, nb_max_matches);
428 rte_mempool_free(mbuf_mp);
429 rte_exit(EXIT_FAILURE, "RegEx function failed\n");
431 rte_mempool_free(mbuf_mp);