examples/ip_pipeline: add mempool object
[dpdk.git] / examples / ip_pipeline / cli.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <rte_common.h>
11
12 #include "cli.h"
13 #include "mempool.h"
14 #include "parser.h"
15
16 #ifndef CMD_MAX_TOKENS
17 #define CMD_MAX_TOKENS     256
18 #endif
19
20 #define MSG_OUT_OF_MEMORY  "Not enough memory.\n"
21 #define MSG_CMD_UNKNOWN    "Unknown command \"%s\".\n"
22 #define MSG_CMD_UNIMPLEM   "Command \"%s\" not implemented.\n"
23 #define MSG_ARG_NOT_ENOUGH "Not enough arguments for command \"%s\".\n"
24 #define MSG_ARG_TOO_MANY   "Too many arguments for command \"%s\".\n"
25 #define MSG_ARG_MISMATCH   "Wrong number of arguments for command \"%s\".\n"
26 #define MSG_ARG_NOT_FOUND  "Argument \"%s\" not found.\n"
27 #define MSG_ARG_INVALID    "Invalid value for argument \"%s\".\n"
28 #define MSG_FILE_ERR       "Error in file \"%s\" at line %u.\n"
29 #define MSG_CMD_FAIL       "Command \"%s\" failed.\n"
30
31 static int
32 is_comment(char *in)
33 {
34         if ((strlen(in) && index("!#%;", in[0])) ||
35                 (strncmp(in, "//", 2) == 0) ||
36                 (strncmp(in, "--", 2) == 0))
37                 return 1;
38
39         return 0;
40 }
41
42 /**
43  * mempool <mempool_name>
44  *  buffer <buffer_size>
45  *  pool <pool_size>
46  *  cache <cache_size>
47  *  cpu <cpu_id>
48  */
49 static void
50 cmd_mempool(char **tokens,
51         uint32_t n_tokens,
52         char *out,
53         size_t out_size)
54 {
55         struct mempool_params p;
56         char *name;
57         struct mempool *mempool;
58
59         if (n_tokens != 10) {
60                 snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]);
61                 return;
62         }
63
64         name = tokens[1];
65
66         if (strcmp(tokens[2], "buffer") != 0) {
67                 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "buffer");
68                 return;
69         }
70
71         if (parser_read_uint32(&p.buffer_size, tokens[3]) != 0) {
72                 snprintf(out, out_size, MSG_ARG_INVALID, "buffer_size");
73                 return;
74         }
75
76         if (strcmp(tokens[4], "pool") != 0) {
77                 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "pool");
78                 return;
79         }
80
81         if (parser_read_uint32(&p.pool_size, tokens[5]) != 0) {
82                 snprintf(out, out_size, MSG_ARG_INVALID, "pool_size");
83                 return;
84         }
85
86         if (strcmp(tokens[6], "cache") != 0) {
87                 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cache");
88                 return;
89         }
90
91         if (parser_read_uint32(&p.cache_size, tokens[7]) != 0) {
92                 snprintf(out, out_size, MSG_ARG_INVALID, "cache_size");
93                 return;
94         }
95
96         if (strcmp(tokens[8], "cpu") != 0) {
97                 snprintf(out, out_size, MSG_ARG_NOT_FOUND, "cpu");
98                 return;
99         }
100
101         if (parser_read_uint32(&p.cpu_id, tokens[9]) != 0) {
102                 snprintf(out, out_size, MSG_ARG_INVALID, "cpu_id");
103                 return;
104         }
105
106         mempool = mempool_create(name, &p);
107         if (mempool == NULL) {
108                 snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]);
109                 return;
110         }
111 }
112
113 void
114 cli_process(char *in, char *out, size_t out_size)
115 {
116         char *tokens[CMD_MAX_TOKENS];
117         uint32_t n_tokens = RTE_DIM(tokens);
118         int status;
119
120         if (is_comment(in))
121                 return;
122
123         status = parse_tokenize_string(in, tokens, &n_tokens);
124         if (status) {
125                 snprintf(out, out_size, MSG_ARG_TOO_MANY, "");
126                 return;
127         }
128
129         if (n_tokens == 0)
130                 return;
131
132         if (strcmp(tokens[0], "mempool") == 0) {
133                 cmd_mempool(tokens, n_tokens, out, out_size);
134                 return;
135         }
136
137         snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]);
138 }
139
140 int
141 cli_script_process(const char *file_name,
142         size_t msg_in_len_max,
143         size_t msg_out_len_max)
144 {
145         char *msg_in = NULL, *msg_out = NULL;
146         FILE *f = NULL;
147
148         /* Check input arguments */
149         if ((file_name == NULL) ||
150                 (strlen(file_name) == 0) ||
151                 (msg_in_len_max == 0) ||
152                 (msg_out_len_max == 0))
153                 return -EINVAL;
154
155         msg_in = malloc(msg_in_len_max + 1);
156         msg_out = malloc(msg_out_len_max + 1);
157         if ((msg_in == NULL) ||
158                 (msg_out == NULL)) {
159                 free(msg_out);
160                 free(msg_in);
161                 return -ENOMEM;
162         }
163
164         /* Open input file */
165         f = fopen(file_name, "r");
166         if (f == NULL) {
167                 free(msg_out);
168                 free(msg_in);
169                 return -EIO;
170         }
171
172         /* Read file */
173         for ( ; ; ) {
174                 if (fgets(msg_in, msg_in_len_max + 1, f) == NULL)
175                         break;
176
177                 printf("%s", msg_in);
178                 msg_out[0] = 0;
179
180                 cli_process(msg_in,
181                         msg_out,
182                         msg_out_len_max);
183
184                 if (strlen(msg_out))
185                         printf("%s", msg_out);
186         }
187
188         /* Close file */
189         fclose(f);
190         free(msg_out);
191         free(msg_in);
192         return 0;
193 }