forked from DFC-OpenSource/ox-ctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_args.c
273 lines (244 loc) · 9.28 KB
/
cmd_args.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* OX: Open-Channel NVM Express SSD Controller
*
* - OX Controller argument parser
*
* Copyright (C) 2016, IT University of Copenhagen. All rights reserved.
* Written by Ivan Luiz Picoli <[email protected]>
*
* Funding support provided by CAPES Foundation, Ministry of Education
* of Brazil, Brasilia - DF 70040-020, Brazil.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <argp.h>
#include <stdint.h>
#include <string.h>
#include "include/ssd.h"
extern struct core_struct core;
const char *argp_program_version = "ox-ctrl v1.3";
const char *argp_program_bug_address = "Ivan L. Picoli <[email protected]>";
enum cmdtypes {
CMDARG_START = 1,
CMDARG_TEST,
CMDARG_DEBUG,
CMDARG_ADMIN,
CMDARG_NULL,
CMDARG_NULL_DEBUG
};
static char doc_global[] = "\n*** OX Controller v1.3 ***\n"
" \n An OpenChannel SSD Controller\n\n"
" Available commands:\n"
" start Start controller with standard configuration\n"
" debug Start controller and print Admin/IO commands\n"
" null Start controller with Null IOs (NVMe queue tests)\n"
" null-debug Null IOs and print Admin/IO commands\n"
" test Start controller, run tests and close\n"
" admin Execute specific tasks within the controller\n"
" \n Initial release developed by Ivan L. Picoli, <[email protected]>\n\n";
static char doc_test[] =
"\nUse this command to run tests, it will start the controller,"
" run the tests and close the controller.\n"
"\n Examples:"
"\n Show all available set of tests + subtests:"
"\n ox-ctrl test -l\n"
"\n Run all available tests:"
"\n ox-ctrl test -a\n"
"\n Run a specific test set:"
"\n ox-ctrl test -s <set_name>\n"
"\n Run a specific subtest:"
"\n ox-ctrl test -s <set_name> -t <subtest_name>";
static char doc_admin[] =
"\nUse this command to run specific tasks within the controller.\n"
"\n Examples:"
"\n Show all available Admin tasks:"
"\n ox-ctrl admin -l\n"
"\n Run a specific admin task:"
"\n ox-ctrl admin -t <task_name>";
static struct argp_option opt_test[] = {
{"list", 'l', "list", OPTION_ARG_OPTIONAL,"Show available tests."},
{"all", 'a', "run_all", OPTION_ARG_OPTIONAL, "Use to run all tests."},
{"set", 's', "test_set", 0, "Test set name. <char>"},
{"test", 't', "test", 0, "Subtest name. <char>"},
{0}
};
static struct argp_option opt_admin[] = {
{"list", 'l', "list", OPTION_ARG_OPTIONAL,"Show available admin tasks."},
{"task", 't', "admin_task", 0, "Admin task to be executed. <char>"},
{0}
};
static error_t parse_opt_test(int key, char *arg, struct argp_state *state)
{
struct nvm_init_arg *args = state->input;
switch (key) {
case 's':
if (!arg || strlen(arg) == 0 || strlen(arg) > CMDARG_LEN) {
argp_usage(state);
}
strcpy(args->test_setname,arg);
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_S;
break;
case 't':
if (!arg || strlen(arg) == 0 || strlen(arg) > CMDARG_LEN)
argp_usage(state);
strcpy(args->test_subtest,arg);
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_T;
break;
case 'a':
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_A;
break;
case 'l':
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_L;
break;
case ARGP_KEY_END:
if (args->arg_num > 2 || args->arg_num == 0)
argp_usage(state);
if (args->arg_flag & CMDARG_FLAG_A && args->arg_num > 1)
argp_usage(state);
if (args->arg_flag & CMDARG_FLAG_L && args->arg_num > 1)
argp_usage(state);
if (args->arg_flag & CMDARG_FLAG_T && args->arg_num != 2)
argp_usage(state);
break;
case ARGP_KEY_ARG:
case ARGP_KEY_NO_ARGS:
case ARGP_KEY_ERROR:
case ARGP_KEY_SUCCESS:
case ARGP_KEY_FINI:
case ARGP_KEY_INIT:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static error_t parse_opt_admin(int key, char *arg, struct argp_state *state)
{
struct nvm_init_arg *args = state->input;
switch (key) {
case 't':
if (!arg || strlen(arg) == 0 || strlen(arg) > CMDARG_LEN)
argp_usage(state);
strcpy(args->admin_task,arg);
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_T;
break;
case 'l':
args->arg_num++;
args->arg_flag |= CMDARG_FLAG_L;
break;
case ARGP_KEY_END:
if (args->arg_num > 1 || args->arg_num == 0)
argp_usage(state);
break;
case ARGP_KEY_ARG:
case ARGP_KEY_NO_ARGS:
case ARGP_KEY_ERROR:
case ARGP_KEY_SUCCESS:
case ARGP_KEY_FINI:
case ARGP_KEY_INIT:
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static void cmd_prepare(struct argp_state *state, struct nvm_init_arg *args,
char *cmd, struct argp *argp_cmd)
{
/* Remove the first arg from the parser */
int argc = state->argc - state->next + 1;
char** argv = &state->argv[state->next - 1];
char* argv0 = argv[0];
argv[0] = malloc(strlen(state->name) + strlen(cmd) + 2);
if(!argv[0])
argp_failure(state, 1, ENOMEM, 0);
sprintf(argv[0], "%s %s", state->name, cmd);
argp_parse(argp_cmd, argc, argv, ARGP_IN_ORDER, &argc, args);
free(argv[0]);
argv[0] = argv0;
state->next += argc - 1;
}
static struct argp argp_test = {opt_test, parse_opt_test, 0, doc_test};
static struct argp argp_admin = {opt_admin, parse_opt_admin, 0, doc_admin};
error_t parse_opt (int key, char *arg, struct argp_state *state)
{
struct nvm_init_arg *args = state->input;
switch(key)
{
case ARGP_KEY_ARG:
if (strcmp(arg, "start") == 0)
args->cmdtype = CMDARG_START;
else if (strcmp(arg, "debug") == 0)
args->cmdtype = CMDARG_DEBUG;
else if (strcmp(arg, "null") == 0)
args->cmdtype = CMDARG_NULL;
else if (strcmp(arg, "null-debug") == 0)
args->cmdtype = CMDARG_NULL_DEBUG;
else if (strcmp(arg, "test") == 0){
args->cmdtype = CMDARG_TEST;
cmd_prepare(state, args, "test", &argp_test);
} else if (strcmp(arg, "admin") == 0){
args->cmdtype = CMDARG_ADMIN;
cmd_prepare(state, args, "admin", &argp_admin);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp_global={NULL, parse_opt,"ox-ctrl [<cmd> [cmd-options]]",
doc_global};
int cmdarg_init (int argc, char **argv)
{
int ret;
core.args_global = malloc (sizeof (struct nvm_init_arg));
memset (core.args_global, 0, sizeof(struct nvm_init_arg));
argp_parse(&argp_global, argc, argv, ARGP_IN_ORDER, NULL, core.args_global);
switch (core.args_global->cmdtype)
{
case CMDARG_START:
return OX_RUN_MODE;
case CMDARG_DEBUG:
core.debug |= 1 << 0;
return OX_RUN_MODE;
case CMDARG_NULL:
core.null |= 1 << 0;
return OX_RUN_MODE;
case CMDARG_NULL_DEBUG:
core.debug |= 1 << 0;
core.null |= 1 << 0;
return OX_RUN_MODE;
case CMDARG_TEST:
ret = nvm_test_unit(core.args_global);
return (!ret) ? OX_TEST_MODE : -1;
case CMDARG_ADMIN:
ret = nvm_admin_unit(core.args_global);
return (!ret) ? OX_ADMIN_MODE : -1;
default:
printf("Invalid command, please use --help to see more info.\n");
}
return -1;
}