app/regex: move mempool creation to worker routine
[dpdk.git] / app / test-regex / main.c
index 0d35f45..cb2a065 100644 (file)
@@ -35,6 +35,10 @@ enum app_args {
        ARG_NUM_OF_ITERATIONS,
 };
 
+struct job_ctx {
+       struct rte_mbuf *mbuf;
+};
+
 static void
 usage(const char *prog_name)
 {
@@ -66,7 +70,9 @@ args_parse(int argc, char **argv, char *rules_file, char *data_file,
                /* Perf test only */
                { "perf", 0, 0, ARG_PERF_MODE},
                /* Number of iterations to run with perf test */
-               { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS}
+               { "nb_iter", 1, 0, ARG_NUM_OF_ITERATIONS},
+               /* End of options */
+               { 0, 0, 0, 0 }
        };
 
        argvopt = argv;
@@ -157,8 +163,7 @@ error:
 }
 
 static int
-init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
-         uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
+init_port(uint16_t *nb_max_payload, char *rules_file, uint8_t *nb_max_matches)
 {
        uint16_t id;
        uint16_t num_devs;
@@ -171,7 +176,7 @@ init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
        };
        struct rte_regexdev_qp_conf qp_conf = {
                .nb_desc = 1024,
-               .qp_conf_flags = RTE_REGEX_QUEUE_PAIR_CFG_OOS_F,
+               .qp_conf_flags = 0,
        };
        int res = 0;
 
@@ -181,14 +186,6 @@ init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
                return -EINVAL;
        }
 
-       *mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
-                                         0, MBUF_SIZE, rte_socket_id());
-       if (*mbuf_mp == NULL) {
-               printf("Error, can't create memory pool\n");
-               res = -ENOMEM;
-               goto error;
-       }
-
        rules_len = read_file(rules_file, &rules);
        if (rules_len < 0) {
                printf("Error, can't read rules files.\n");
@@ -216,6 +213,8 @@ init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
                        printf("Error, can't configure device %d.\n", id);
                        goto error;
                }
+               if (info.regexdev_capa & RTE_REGEXDEV_CAPA_QUEUE_PAIR_OOS_F)
+                       qp_conf.qp_conf_flags |= RTE_REGEX_QUEUE_PAIR_CFG_OOS_F;
                res = rte_regexdev_queue_pair_setup(id, 0, &qp_conf);
                if (res < 0) {
                        printf("Error, can't setup queue pair for device %d.\n",
@@ -229,8 +228,6 @@ init_port(struct rte_mempool **mbuf_mp, uint32_t nb_jobs,
 error:
        if (rules)
                rte_free(rules);
-       if (*mbuf_mp)
-               rte_mempool_free(*mbuf_mp);
        return res;
 }
 
@@ -240,7 +237,7 @@ extbuf_free_cb(void *addr __rte_unused, void *fcb_opaque __rte_unused)
 }
 
 static int
-run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
+run_regex(uint32_t nb_jobs,
          uint16_t nb_max_payload, bool perf_mode, uint32_t nb_iterations,
          char *data_file, uint8_t nb_max_matches)
 {
@@ -264,15 +261,30 @@ run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
        time_t start;
        time_t end;
        double time;
+       struct job_ctx *jobs_ctx;
+       struct rte_mempool *mbuf_mp;
 
        shinfo.free_cb = extbuf_free_cb;
 
+       mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool", nb_jobs, 0,
+                       0, MBUF_SIZE, rte_socket_id());
+       if (mbuf_mp == NULL) {
+               printf("Error, can't create memory pool\n");
+               return -ENOMEM;
+       }
+
        ops = rte_malloc(NULL, sizeof(*ops) * nb_jobs, 0);
        if (!ops) {
                printf("Error, can't allocate memory for ops.\n");
                return -ENOMEM;
        }
 
+       jobs_ctx = rte_malloc(NULL, sizeof(struct job_ctx)*nb_jobs, 0);
+       if (!jobs_ctx) {
+               printf("Error, can't allocate memory for jobs_ctx.\n");
+               return -ENOMEM;
+       }
+
        /* Allocate the jobs and assign each job with an mbuf. */
        for (i = 0; i < nb_jobs; i++) {
                ops[i] = rte_malloc(NULL, sizeof(*ops[0]) + nb_max_matches *
@@ -315,6 +327,7 @@ run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
                long act_job_len = RTE_MIN(job_len, buf_len - pos);
                rte_pktmbuf_attach_extbuf(ops[i]->mbuf, &buf[pos], 0,
                                          act_job_len, &shinfo);
+               jobs_ctx[i].mbuf = ops[i]->mbuf;
                ops[i]->mbuf->data_len = job_len;
                ops[i]->mbuf->pkt_len = act_job_len;
                ops[i]->user_id = i;
@@ -384,15 +397,18 @@ run_regex(struct rte_mempool *mbuf_mp, uint32_t nb_jobs,
        }
 end:
        for (i = 0; i < actual_jobs; i++) {
-               if (ops[i]) {
-                       if (ops[i]->mbuf)
-                               rte_pktmbuf_free(ops[i]->mbuf);
+               if (ops[i])
                        rte_free(ops[i]);
-               }
+               if (jobs_ctx[i].mbuf)
+                       rte_pktmbuf_free(jobs_ctx[i].mbuf);
        }
        rte_free(ops);
+       rte_free(jobs_ctx);
        if (buf)
                rte_free(buf);
+       if (mbuf_mp)
+               rte_mempool_free(mbuf_mp);
+
        return res;
 }
 
@@ -401,7 +417,6 @@ main(int argc, char **argv)
 {
        char rules_file[MAX_FILE_NAME];
        char data_file[MAX_FILE_NAME];
-       struct rte_mempool *mbuf_mp = NULL;
        uint32_t nb_jobs = 0;
        uint16_t nb_max_payload = 0;
        bool perf_mode = 0;
@@ -418,16 +433,13 @@ main(int argc, char **argv)
                args_parse(argc, argv, rules_file, data_file, &nb_jobs,
                           &perf_mode, &nb_iterations);
 
-       ret = init_port(&mbuf_mp, nb_jobs, &nb_max_payload, rules_file,
-                       &nb_max_matches);
+       ret = init_port(&nb_max_payload, rules_file, &nb_max_matches);
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "init port failed\n");
-       ret = run_regex(mbuf_mp, nb_jobs, nb_max_payload, perf_mode,
+       ret = run_regex(nb_jobs, nb_max_payload, perf_mode,
                        nb_iterations, data_file, nb_max_matches);
        if (ret < 0) {
-               rte_mempool_free(mbuf_mp);
                rte_exit(EXIT_FAILURE, "RegEx function failed\n");
        }
-       rte_mempool_free(mbuf_mp);
        return EXIT_SUCCESS;
 }