examples/ip_pipeline: add mempool object

Add mempool object implementation to the application.

Signed-off-by: Cristian Dumitrescu <cristian.dumitrescu@intel.com>
Signed-off-by: Jasvinder Singh <jasvinder.singh@intel.com>
Signed-off-by: Fan Zhang <roy.fan.zhang@intel.com>
This commit is contained in:
Jasvinder Singh 2018-03-29 19:31:41 +01:00 committed by Cristian Dumitrescu
parent 4bbf8e30aa
commit 6bfe74f8c9
7 changed files with 252 additions and 2 deletions

View File

@ -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

View File

@ -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;
}
void
cli_process(char *in, char *out __rte_unused, size_t out_size __rte_unused)
/**
* 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, 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

View File

@ -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_ */

View File

@ -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,

View File

@ -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;
}

View File

@ -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_ */

View File

@ -11,5 +11,6 @@ sources = files(
'cli.c',
'conn.c',
'main.c',
'mempool.c',
'parser.c',
)