[dpdk-dev] [PATCH v3 19/44] ip_pipeline: add mempool object for pipeline

Jasvinder Singh jasvinder.singh at intel.com
Fri Mar 16 18:58:41 CET 2018


Add mempool object implementation to the application.

Signed-off-by: Cristian Dumitrescu <cristian.dumitrescu at intel.com>
Signed-off-by: Jasvinder Singh <jasvinder.singh at intel.com>
Signed-off-by: Fan Zhang <roy.fan.zhang at intel.com>
---
 examples/ip_pipeline/Makefile    |   1 +
 examples/ip_pipeline/cli.c       | 109 ++++++++++++++++++++++++++++++++++++++-
 examples/ip_pipeline/common.h    |  12 +++++
 examples/ip_pipeline/main.c      |   8 +++
 examples/ip_pipeline/mempool.c   |  81 +++++++++++++++++++++++++++++
 examples/ip_pipeline/mempool.h   |  40 ++++++++++++++
 examples/ip_pipeline/meson.build |   1 +
 7 files changed, 251 insertions(+), 1 deletion(-)
 create mode 100644 examples/ip_pipeline/common.h
 create mode 100644 examples/ip_pipeline/mempool.c
 create mode 100644 examples/ip_pipeline/mempool.h

diff --git a/examples/ip_pipeline/Makefile b/examples/ip_pipeline/Makefile
index 0c5b6b1..fca28c5 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 3e97b29..6fe3725 100644
--- a/examples/ip_pipeline/cli.c
+++ b/examples/ip_pipeline/cli.c
@@ -10,6 +10,23 @@
 #include <rte_common.h>
 
 #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 <mempool_name>
+ *  buffer <buffer_size>
+ *  pool <pool_size>
+ *  cache <cache_size>
+ *  cpu <cpu_id>
+ */
+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 0000000..0886dfb
--- /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 60936f4..b53f623 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 0000000..33b9243
--- /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 <stdlib.h>
+#include <string.h>
+
+#include <rte_mbuf.h>
+
+#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 0000000..bd46a11
--- /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 <stdint.h>
+#include <sys/queue.h>
+
+#include <rte_mempool.h>
+
+#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 a89cb30..f8a450f 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',
 )
-- 
2.9.3



More information about the dev mailing list