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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/* Parses the interactive commands */
#include <config.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#ifdef HAVE_READLINE_HISTORY_H
#include <readline/history.h>
#endif
#include <corosync/coroapi.h>
#include "vqsim.h"
static void do_usage(void)
{
printf(" All node IDs in the cluster are unique and belong to a numbered 'partition' (default=0)\n");
printf("\n");
printf("up [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
printf(" bring node(s) online in the specified partition(s)\n");
printf("down <nodeid>,[<nodeid>...]\n");
printf(" send nodes offline (shut them down)\n");
printf("move/split [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
printf(" Move nodes from one partition to another (netsplit)\n");
printf(" <partition> here is the partition to move the nodes to\n");
printf("join <partition> <partition> [<partition>] ... \n");
printf(" Join partitions together (reverse of a netsplit)\n");
printf("qdevice on|off [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
printf(" Enable quorum device in specified nodes\n");
printf("autofence on|off\n");
printf(" automatically 'down' nodes on inquorate side on netsplit\n");
printf("timeout <n> (default 250)\n");
printf(" Wait a maximum of <n> milli-seconds for the next command to complete.\n");
printf("sync on|off (default on)\n");
printf(" enable/disable synchronous execution of commands (wait for completion)\n");
printf("assert on|off (default off)\n");
printf(" Abort the simulation run if a timeout expires\n");
printf("show Show current nodes status\n");
printf("exit\n\n");
}
/* Commands return 0 if they return immediately, >1 if we are waiting for replies from nodes */
typedef int (*cmd_routine_t)(int argc, char **argv);
static int run_up_cmd(int argc, char **argv);
static int run_down_cmd(int argc, char **argv);
static int run_join_cmd(int argc, char **argv);
static int run_move_cmd(int argc, char **argv);
static int run_exit_cmd(int argc, char **argv);
static int run_show_cmd(int argc, char **argv);
static int run_timeout_cmd(int argc, char **argv);
static int run_assert_cmd(int argc, char **argv);
static int run_autofence_cmd(int argc, char **argv);
static int run_qdevice_cmd(int argc, char **argv);
static int run_sync_cmd(int argc, char **argv);
static struct cmd_list_struct {
const char *cmd;
int min_args;
cmd_routine_t cmd_runner;
} cmd_list[] = {
{ "up", 1, run_up_cmd},
{ "down", 1, run_down_cmd},
{ "move", 2, run_move_cmd},
{ "split", 2, run_move_cmd},
{ "join", 2, run_join_cmd},
{ "autofence", 1, run_autofence_cmd},
{ "qdevice", 1, run_qdevice_cmd},
{ "show", 0, run_show_cmd},
{ "timeout", 1, run_timeout_cmd},
{ "sync", 1, run_sync_cmd},
{ "assert", 1, run_assert_cmd},
{ "exit", 0, run_exit_cmd},
{ "quit", 0, run_exit_cmd},
{ "q", 0, run_exit_cmd},
};
static int num_cmds = (sizeof(cmd_list)) / sizeof(struct cmd_list_struct);
#define MAX_ARGS 1024
/* Takes a <partition>:[<node>[,<node>]...] list and return it
as a partition and a list of nodes.
Returns 0 if successful, -1 if not
*/
static int parse_partition_nodelist(char *string, int *partition, int *num_nodes, int **retnodes)
{
int i;
int nodecount;
int len;
int last_comma;
char *nodeptr;
int *nodes;
char *colonptr = strchr(string, ':');
if (colonptr) {
*colonptr = '\0';
nodeptr = colonptr+1;
*partition = atoi(string);
}
else {
/* Default to partition 0 */
*partition = 0;
nodeptr = string;
}
/* Count the number of commas and allocate space for the nodes */
nodecount = 0;
for (i=0; i<strlen(nodeptr); i++) {
if (nodeptr[i] == ',') {
nodecount++;
}
}
nodecount++; /* The one between the last comma and the trailing NUL */
if (nodecount < 1 || nodecount > MAX_NODES) {
return -1;
}
nodes = malloc(sizeof(int) * nodecount);
if (!nodes) {
return -1;
}
nodecount = 0;
last_comma = 0;
len = strlen(nodeptr);
for (i=0; i<=len; i++) {
if (nodeptr[i] == ',' || nodeptr[i] == '\0') {
nodeptr[i] = '\0';
nodes[nodecount++] = atoi(&nodeptr[last_comma]);
last_comma = i+1;
}
}
*num_nodes = nodecount;
*retnodes = nodes;
return 0;
}
void parse_input_command(char *rl_cmd)
{
int i;
int argc = 0;
int valid_cmd = 0;
char *argv[MAX_ARGS];
int last_arg_start = 0;
int last_was_space = 0;
int len;
int ret = 0;
char *cmd;
/* ^D quits */
if (rl_cmd == NULL) {
(void)run_exit_cmd(0, NULL);
}
/* '#' starts a comment */
if (rl_cmd[0] == '#') {
return;
}
cmd = strdup(rl_cmd);
/* Split cmd up into args
* destroying the original string mwahahahaha
*/
len = strlen(cmd);
/* Span leading spaces */
for (i=0; cmd[i] == ' '; i++)
;
last_arg_start = i;
for (; i<=len; i++) {
if (cmd[i] == ' ' || cmd[i] == '\0') {
/* Allow multiple spaces */
if (last_was_space) {
continue;
}
cmd[i] = '\0';
last_was_space = 1;
argv[argc] = &cmd[last_arg_start];
argc++;
}
else {
if (last_was_space) {
last_arg_start = i;
}
last_was_space = 0;
}
}
/* Ignore null commands */
if (argc < 1 || strlen(argv[0]) == 0) {
free(cmd);
resume_kb_input(0);
return;
}
#ifdef HAVE_READLINE_HISTORY_H
add_history(rl_cmd);
#endif
/* Dispatch command */
for (i=0; i<num_cmds; i++) {
if (strcasecmp(argv[0], cmd_list[i].cmd) == 0) {
if (argc < cmd_list[i].min_args) {
break;
}
ret = cmd_list[i].cmd_runner(argc, argv);
valid_cmd = 1;
}
}
if (!valid_cmd) {
do_usage();
}
free(cmd);
/* ret==0 means we can return immediately to command-line input */
if (ret == 0) {
resume_kb_input(ret);
}
}
static int run_up_cmd(int argc, char **argv)
{
int partition;
int num_nodes;
int *nodelist;
int i,j;
int succeeded = 0;
if (argc <= 1) {
return 0;
}
cmd_start_sync_command();
for (i=1; i<argc; i++) {
if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
for (j=0; j<num_nodes; j++) {
if (!cmd_start_new_node(nodelist[j], partition)) {
succeeded++;
}
}
free(nodelist);
}
}
return succeeded;
}
static int run_down_cmd(int argc, char **argv)
{
int nodeid;
int i;
int succeeded = 0;
cmd_start_sync_command();
for (i=1; i<argc; i++) {
nodeid = atoi(argv[1]);
if (!cmd_stop_node(nodeid)) {
succeeded++;
}
}
return succeeded;
}
static int run_join_cmd(int argc, char **argv)
{
int i;
if (argc < 2) {
printf("join needs at least two partition numbers\n");
return 0;
}
cmd_start_sync_command();
for (i=2; i<argc; i++) {
cmd_join_partitions(atoi(argv[1]), atoi(argv[i]));
}
cmd_update_all_partitions(1);
return 1;
}
static int run_move_cmd(int argc, char **argv)
{
int i;
int partition;
int num_nodes;
int *nodelist;
cmd_start_sync_command();
for (i=1; i<argc; i++) {
if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
cmd_move_nodes(partition, num_nodes, nodelist);
free(nodelist);
}
}
cmd_update_all_partitions(1);
return 1;
}
static int run_autofence_cmd(int argc, char **argv)
{
int onoff = -1;
if (strcasecmp(argv[1], "on") == 0) {
onoff = 1;
}
if (strcasecmp(argv[1], "off") == 0) {
onoff = 0;
}
if (onoff == -1) {
fprintf(stderr, "ERR: autofence value must be 'on' or 'off'\n");
}
else {
cmd_set_autofence(onoff);
}
return 0;
}
static int run_qdevice_cmd(int argc, char **argv)
{
int i,j;
int partition;
int num_nodes;
int *nodelist;
int onoff = -1;
if (strcasecmp(argv[1], "on") == 0) {
onoff = 1;
}
if (strcasecmp(argv[1], "off") == 0) {
onoff = 0;
}
if (onoff == -1) {
fprintf(stderr, "ERR: qdevice should be 'on' or 'off'\n");
return 0;
}
for (i=2; i<argc; i++) {
if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
for (j=0; j<num_nodes; j++) {
cmd_qdevice_poll(nodelist[j], onoff);
}
free(nodelist);
}
}
cmd_update_all_partitions(0);
return 0;
}
static int run_show_cmd(int argc, char **argv)
{
cmd_show_node_states();
return 0;
}
static int run_timeout_cmd(int argc, char **argv)
{
cmd_set_timeout(atol(argv[1]));
return 0;
}
static int run_sync_cmd(int argc, char **argv)
{
int onoff = -1;
if (strcasecmp(argv[1], "on") == 0) {
onoff = 1;
}
if (strcasecmp(argv[1], "off") == 0) {
onoff = 0;
}
if (onoff == -1) {
fprintf(stderr, "ERR: sync value must be 'on' or 'off'\n");
}
else {
cmd_set_sync(onoff);
}
return 0;
}
static int run_assert_cmd(int argc, char **argv)
{
int onoff = -1;
if (strcasecmp(argv[1], "on") == 0) {
onoff = 1;
}
if (strcasecmp(argv[1], "off") == 0) {
onoff = 0;
}
if (onoff == -1) {
fprintf(stderr, "ERR: assert value must be 'on' or 'off'\n");
}
else {
cmd_set_assert(onoff);
}
return 0;
}
static int run_exit_cmd(int argc, char **argv)
{
cmd_stop_all_nodes();
exit(0);
}
|