From: Jasvinder Singh Date: Thu, 29 Mar 2018 18:31:41 +0000 (+0100) Subject: examples/ip_pipeline: add mempool object X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=6bfe74f8c93e9d5ca5906981397aab79a59bb04e;p=dpdk.git examples/ip_pipeline: add mempool object Add mempool object implementation to the application. Signed-off-by: Cristian Dumitrescu Signed-off-by: Jasvinder Singh Signed-off-by: Fan Zhang --- diff --git a/examples/ip_pipeline/Makefile b/examples/ip_pipeline/Makefile index 0c5b6b117e..fca28c5197 100644 --- a/examples/ip_pipeline/Makefile +++ b/examples/ip_pipeline/Makefile @@ -8,6 +8,7 @@ APP = ip_pipeline SRCS-y := cli.c SRCS-y += conn.c SRCS-y += main.c +SRCS-y += mempool.c SRCS-y += parser.c #SRCS-y += thread.c diff --git a/examples/ip_pipeline/cli.c b/examples/ip_pipeline/cli.c index 34ada83baf..b0d8345745 100644 --- a/examples/ip_pipeline/cli.c +++ b/examples/ip_pipeline/cli.c @@ -10,6 +10,23 @@ #include #include "cli.h" +#include "mempool.h" +#include "parser.h" + +#ifndef CMD_MAX_TOKENS +#define CMD_MAX_TOKENS 256 +#endif + +#define MSG_OUT_OF_MEMORY "Not enough memory.\n" +#define MSG_CMD_UNKNOWN "Unknown command \"%s\".\n" +#define MSG_CMD_UNIMPLEM "Command \"%s\" not implemented.\n" +#define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n" +#define MSG_ARG_TOO_MANY "Too many arguments for command \"%s\".\n" +#define MSG_ARG_MISMATCH "Wrong number of arguments for command \"%s\".\n" +#define MSG_ARG_NOT_FOUND "Argument \"%s\" not found.\n" +#define MSG_ARG_INVALID "Invalid value for argument \"%s\".\n" +#define MSG_FILE_ERR "Error in file \"%s\" at line %u.\n" +#define MSG_CMD_FAIL "Command \"%s\" failed.\n" static int is_comment(char *in) @@ -22,12 +39,102 @@ is_comment(char *in) return 0; } +/** + * mempool + * buffer + * pool + * cache + * cpu + */ +static void +cmd_mempool(char **tokens, + uint32_t n_tokens, + char *out, + size_t out_size) +{ + struct mempool_params p; + char *name; + struct mempool *mempool; + + if (n_tokens != 10) { + snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]); + return; + } + + name = tokens[1]; + + if (strcmp(tokens[2], "buffer") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer"); + return; + } + + if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size"); + return; + } + + if (strcmp(tokens[4], "pool") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool"); + return; + } + + if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "pool_size"); + return; + } + + if (strcmp(tokens[6], "cache") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache"); + return; + } + + if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "cache_size"); + return; + } + + if (strcmp(tokens[8], "cpu") != 0) { + snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu"); + return; + } + + if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) { + snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id"); + return; + } + + mempool = mempool_create(name, &p); + if (mempool == NULL) { + snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]); + return; + } +} + void -cli_process(char *in, char *out __rte_unused, size_t out_size __rte_unused) +cli_process(char *in, char *out, size_t out_size) { + char *tokens[CMD_MAX_TOKENS]; + uint32_t n_tokens = RTE_DIM(tokens); + int status; + if (is_comment(in)) return; + status = parse_tokenize_string(in, tokens, &n_tokens); + if (status) { + snprintf(out, out_size, MSG_ARG_TOO_MANY, ""); + return; + } + + if (n_tokens == 0) + return; + + if (strcmp(tokens[0], "mempool") == 0) { + cmd_mempool(tokens, n_tokens, out, out_size); + return; + } + + snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]); } int diff --git a/examples/ip_pipeline/common.h b/examples/ip_pipeline/common.h new file mode 100644 index 0000000000..0886dfbe95 --- /dev/null +++ b/examples/ip_pipeline/common.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation + */ + +#ifndef _INCLUDE_COMMON_H_ +#define _INCLUDE_COMMON_H_ + +#ifndef NAME_SIZE +#define NAME_SIZE 64 +#endif + +#endif /* _INCLUDE_COMMON_H_ */ diff --git a/examples/ip_pipeline/main.c b/examples/ip_pipeline/main.c index 60936f48ed..b53f623a21 100644 --- a/examples/ip_pipeline/main.c +++ b/examples/ip_pipeline/main.c @@ -12,6 +12,7 @@ #include "cli.h" #include "conn.h" +#include "mempool.h" static const char usage[] = "%s EAL_ARGS -- [-h HOST] [-p PORT] [-s SCRIPT]\n"; @@ -159,6 +160,13 @@ main(int argc, char **argv) return status; }; + /* Mempool */ + status = mempool_init(); + if (status) { + printf("Error: Mempool initialization failed (%d)\n", status); + return status; + } + /* Script */ if (app.script_name) cli_script_process(app.script_name, diff --git a/examples/ip_pipeline/mempool.c b/examples/ip_pipeline/mempool.c new file mode 100644 index 0000000000..33b9243397 --- /dev/null +++ b/examples/ip_pipeline/mempool.c @@ -0,0 +1,81 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation + */ + +#include +#include + +#include + +#include "mempool.h" + +#define BUFFER_SIZE_MIN (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM) + +static struct mempool_list mempool_list; + +int +mempool_init(void) +{ + TAILQ_INIT(&mempool_list); + + return 0; +} + +struct mempool * +mempool_find(const char *name) +{ + struct mempool *mempool; + + if (name == NULL) + return NULL; + + TAILQ_FOREACH(mempool, &mempool_list, node) + if (strcmp(mempool->name, name) == 0) + return mempool; + + return NULL; +} + +struct mempool * +mempool_create(const char *name, struct mempool_params *params) +{ + struct mempool *mempool; + struct rte_mempool *m; + + /* Check input params */ + if ((name == NULL) || + mempool_find(name) || + (params == NULL) || + (params->buffer_size < BUFFER_SIZE_MIN) || + (params->pool_size == 0)) + return NULL; + + /* Resource create */ + m = rte_pktmbuf_pool_create( + name, + params->pool_size, + params->cache_size, + 0, + params->buffer_size - sizeof(struct rte_mbuf), + params->cpu_id); + + if (m == NULL) + return NULL; + + /* Node allocation */ + mempool = calloc(1, sizeof(struct mempool)); + if (mempool == NULL) { + rte_mempool_free(m); + return NULL; + } + + /* Node fill in */ + strncpy(mempool->name, name, sizeof(mempool->name)); + mempool->m = m; + mempool->buffer_size = params->buffer_size; + + /* Node add to list */ + TAILQ_INSERT_TAIL(&mempool_list, mempool, node); + + return mempool; +} diff --git a/examples/ip_pipeline/mempool.h b/examples/ip_pipeline/mempool.h new file mode 100644 index 0000000000..bd46a11ca0 --- /dev/null +++ b/examples/ip_pipeline/mempool.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation + */ + +#ifndef _INCLUDE_MEMPOOL_H_ +#define _INCLUDE_MEMPOOL_H_ + +#include +#include + +#include + +#include "common.h" + +struct mempool { + TAILQ_ENTRY(mempool) node; + char name[NAME_SIZE]; + struct rte_mempool *m; + uint32_t buffer_size; +}; + +TAILQ_HEAD(mempool_list, mempool); + +int +mempool_init(void); + +struct mempool * +mempool_find(const char *name); + +struct mempool_params { + uint32_t buffer_size; + uint32_t pool_size; + uint32_t cache_size; + uint32_t cpu_id; +}; + +struct mempool * +mempool_create(const char *name, struct mempool_params *params); + +#endif /* _INCLUDE_MEMPOOL_H_ */ diff --git a/examples/ip_pipeline/meson.build b/examples/ip_pipeline/meson.build index a89cb309d9..f8a450f7a8 100644 --- a/examples/ip_pipeline/meson.build +++ b/examples/ip_pipeline/meson.build @@ -11,5 +11,6 @@ sources = files( 'cli.c', 'conn.c', 'main.c', + 'mempool.c', 'parser.c', )