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(uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
172 struct rte_regexdev_info info;
173 struct rte_regexdev_config dev_conf = {
177 struct rte_regexdev_qp_conf qp_conf = {
183 num_devs = rte_regexdev_count();
185 printf("Error, no devices detected.\n");
189 rules_len = read_file(rules_file, &rules);
191 printf("Error, can't read rules files.\n");
196 for (id = 0; id < num_devs; id++) {
197 res = rte_regexdev_info_get(id, &info);
199 printf("Error, can't get device info.\n");
202 printf(":: initializing dev: %d\n", id);
203 *nb_max_matches = info.max_matches;
204 *nb_max_payload = info.max_payload_size;
205 if (info.regexdev_capa & RTE_REGEXDEV_SUPP_MATCH_AS_END_F)
206 dev_conf.dev_cfg_flags |= RTE_REGEXDEV_CFG_MATCH_AS_END_F;
207 dev_conf.nb_max_matches = info.max_matches;
208 dev_conf.nb_rules_per_group = info.max_rules_per_group;
209 dev_conf.rule_db_len = rules_len;
210 dev_conf.rule_db = rules;
211 res = rte_regexdev_configure(id, &dev_conf);
213 printf("Error, can't configure device %d.\n", id);
216 if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
217 qp_conf.qp_conf_flags |= RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
218 res = rte_regexdev_queue_pair_setup(id, 0, &qp_conf);
220 printf("Error, can't setup queue pair for device %d.\n",
224 printf(":: initializing device: %d done\n", id);
235 extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
240 run_regex(uint32_t nb_jobs,
241 uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
242 char *data_file, uint8_t nb_max_matches)
247 uint32_t actual_jobs = 0;
249 struct rte_regex_ops **ops;
253 struct rte_regexdev_match *match;
255 unsigned long d_ind = 0;
256 struct rte_mbuf_ext_shared_info shinfo;
257 uint32_t total_enqueue = 0;
258 uint32_t total_dequeue = 0;
259 uint32_t total_matches = 0;
264 struct job_ctx *jobs_ctx;
265 struct rte_mempool *mbuf_mp;
267 shinfo.free_cb = extbuf_free_cb;
269 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
270 0, MBUF_SIZE, rte_socket_id());
271 if (mbuf_mp == NULL) {
272 printf("Error, can't create memory pool\n");
276 ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
278 printf("Error, can't allocate memory for ops.\n");
282 jobs_ctx = rte_malloc(NULL, sizeof(struct job_ctx)*nb_jobs, 0);
284 printf("Error, can't allocate memory for jobs_ctx.\n");
288 /* Allocate the jobs and assign each job with an mbuf. */
289 for (i = 0; i < nb_jobs; i++) {
290 ops[i] = rte_malloc(NULL, sizeof(*ops[0]) + nb_max_matches *
291 sizeof(struct rte_regexdev_match), 0);
293 printf("Error, can't allocate memory for op.\n");
297 ops[i]->mbuf = rte_pktmbuf_alloc(mbuf_mp);
299 printf("Error, can't attach mbuf.\n");
305 buf_len = read_file(data_file, &buf);
307 printf("Error, can't read file, or file is empty.\n");
312 job_len = buf_len / nb_jobs;
314 printf("Error, To many jobs, for the given input.\n");
319 if (job_len > nb_max_payload) {
320 printf("Error, not enough jobs to cover input.\n");
325 /* Assign each mbuf with the data to handle. */
326 for (i = 0; (pos < buf_len) && (i < nb_jobs) ; i++) {
327 long act_job_len = RTE_MIN(job_len, buf_len - pos);
328 rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
329 act_job_len, &shinfo);
330 jobs_ctx[i].mbuf = ops[i]->mbuf;
331 ops[i]->mbuf->data_len = job_len;
332 ops[i]->mbuf->pkt_len = act_job_len;
334 ops[i]->group_id0 = 1;
340 for (i = 0; i < nb_iterations; i++) {
343 while (total_dequeue < actual_jobs) {
344 struct rte_regex_ops **cur_ops_to_enqueue = ops +
346 struct rte_regex_ops **cur_ops_to_dequeue = ops +
349 if (actual_jobs - total_enqueue)
350 total_enqueue += rte_regexdev_enqueue_burst
351 (dev_id, qp_id, cur_ops_to_enqueue,
352 actual_jobs - total_enqueue);
354 total_dequeue += rte_regexdev_dequeue_burst
355 (dev_id, qp_id, cur_ops_to_dequeue,
356 total_enqueue - total_dequeue);
360 time = ((double)end - start) / CLOCKS_PER_SEC;
361 printf("Job len = %ld Bytes\n", job_len);
362 printf("Time = %lf sec\n", time);
363 printf("Perf = %lf Gbps\n",
364 (((double)actual_jobs * job_len * nb_iterations * 8) / time) /
368 /* Log results per job. */
369 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
370 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
371 printf("Job id %"PRIu64" number of matches = %d\n",
372 ops[d_ind]->user_id, nb_matches);
373 total_matches += nb_matches;
374 match = ops[d_ind % actual_jobs]->matches;
375 for (i = 0; i < nb_matches; i++) {
376 printf("match %d, rule = %d, start = %d,len = %d\n",
377 i, match->rule_id, match->start_offset,
382 printf("Total matches = %d\n", total_matches);
383 printf("All Matches:\n");
385 /* Log absolute results. */
386 for (d_ind = 0; d_ind < total_dequeue; d_ind++) {
387 nb_matches = ops[d_ind % actual_jobs]->nb_matches;
388 total_matches += nb_matches;
389 match = ops[d_ind % actual_jobs]->matches;
390 for (i = 0; i < nb_matches; i++) {
391 printf("start = %ld, len = %d, rule = %d\n",
392 match->start_offset + d_ind * job_len,
393 match->len, match->rule_id);
399 for (i = 0; i < actual_jobs; i++) {
402 if (jobs_ctx[i].mbuf)
403 rte_pktmbuf_free(jobs_ctx[i].mbuf);
410 rte_mempool_free(mbuf_mp);
416 main(int argc, char **argv)
418 char rules_file[MAX_FILE_NAME];
419 char data_file[MAX_FILE_NAME];
420 uint32_t nb_jobs = 0;
421 uint16_t nb_max_payload = 0;
423 uint32_t nb_iterations = 0;
424 uint8_t nb_max_matches = 0;
427 ret = rte_eal_init(argc, argv);
429 rte_exit(EXIT_FAILURE, "EAL init failed\n");
433 args_parse(argc, argv, rules_file, data_file, &nb_jobs,
434 &perf_mode, &nb_iterations);
436 ret = init_port(&nb_max_payload, rules_file, &nb_max_matches);
438 rte_exit(EXIT_FAILURE, "init port failed\n");
439 ret = run_regex(nb_jobs, nb_max_payload, perf_mode,
440 nb_iterations, data_file, nb_max_matches);
442 rte_exit(EXIT_FAILURE, "RegEx function failed\n");