Whamcloud - gitweb
LU-13501 lnet: Skip health and resends for single rail configs
[fs/lustre-release.git] / lnet / utils / lst.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/selftest/conctl.c
33  *
34  * Author: Liang Zhen <liangzhen@clusterfs.com>
35  */
36 #include <errno.h>
37 #include <getopt.h>
38 #include <inttypes.h>
39 #include <pwd.h>
40 #include <unistd.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/ioctl.h>
46 #include <time.h>
47 #include <linux/types.h>
48
49 #include <libcfs/util/list.h>
50 #include <libcfs/util/ioctl.h>
51 #include <libcfs/util/parser.h>
52 #include <linux/lnet/lnetctl.h>
53 #include <linux/lnet/lnetst.h>
54 #include <linux/lnet/nidstr.h>
55 #include "lnetconfig/liblnetconfig.h"
56
57 struct lst_sid LST_INVALID_SID = { .ses_nid = LNET_NID_ANY, .ses_stamp = -1 };
58 static struct lst_sid session_id;
59 static int                 session_key;
60 static int lst_list_commands(int argc, char **argv);
61
62 /* All nodes running 2.6.50 or later understand feature LST_FEAT_BULK_LEN */
63 static unsigned         session_features = LST_FEATS_MASK;
64 static struct lstcon_trans_stat trans_stat;
65
66 typedef struct list_string {
67         struct list_string *lstr_next;
68         int                 lstr_sz;
69         char                lstr_str[0];
70 } lstr_t;
71
72 #ifndef offsetof
73 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
74 #endif
75
76 static int alloc_count = 0;
77 static int alloc_nob   = 0;
78
79 lstr_t *
80 alloc_lstr(int sz)
81 {
82         lstr_t  *lstr = malloc(offsetof(lstr_t, lstr_str[sz]));
83
84         if (lstr == NULL) {
85                 fprintf(stderr, "Can't allocate lstr\n");
86                 abort();
87         }
88
89         alloc_nob += sz;
90         alloc_count++;
91
92         lstr->lstr_str[0] = 0;
93         lstr->lstr_sz = sz;
94         return lstr;
95 }
96
97 void
98 free_lstr(lstr_t *lstr)
99 {
100         alloc_count--;
101         alloc_nob -= lstr->lstr_sz;
102         free(lstr);
103 }
104
105 void
106 free_lstrs(lstr_t **list)
107 {
108         lstr_t   *lstr;
109
110         while ((lstr = *list) != NULL) {
111                 *list = lstr->lstr_next;
112                 free_lstr(lstr);
113         }
114 }
115
116 void
117 new_lstrs(lstr_t **list, char *prefix, char *postfix,
118           int lo, int hi, int stride)
119 {
120         int    n1 = strlen(prefix);
121         int    n2 = strlen(postfix);
122         int    sz = n1 + 20 + n2 + 1;
123
124         do {
125                 lstr_t *n = alloc_lstr(sz);
126
127                 snprintf(n->lstr_str, sz - 1, "%s%u%s",
128                          prefix, lo, postfix);
129
130                 n->lstr_next = *list;
131                 *list = n;
132
133                 lo += stride;
134         } while (lo <= hi);
135 }
136
137 int
138 expand_lstr(lstr_t **list, lstr_t *l)
139 {
140         int          nob = strlen(l->lstr_str);
141         char        *b1;
142         char        *b2;
143         char        *expr;
144         char        *sep;
145         int          x;
146         int          y;
147         int          z;
148         int          n;
149
150         b1 = strchr(l->lstr_str, '[');
151         if (b1 == NULL) {
152                 l->lstr_next = *list;
153                 *list = l;
154                 return 0;
155         }
156
157         b2 = strchr(b1, ']');
158         if (b2 == NULL || b2 == b1 + 1)
159                 return -1;
160
161         *b1++ = 0;
162         *b2++ = 0;
163         expr = b1;
164         do {
165
166                 sep = strchr(expr, ',');
167                 if (sep != NULL)
168                         *sep++ = 0;
169
170                 nob = strlen(expr);
171                 n = nob;
172                 if (sscanf(expr, "%u%n", &x, &n) >= 1 && n == nob) {
173                         /* simple number */
174                         new_lstrs(list, l->lstr_str, b2, x, x, 1);
175                         continue;
176                 }
177
178                 n = nob;
179                 if (sscanf(expr, "%u-%u%n", &x, &y, &n) >= 2 && n == nob &&
180                     x < y) {
181                         /* simple range */
182                         new_lstrs(list, l->lstr_str, b2, x, y, 1);
183                         continue;
184                 }
185
186                 n = nob;
187                 if (sscanf(expr, "%u-%u/%u%n", &x, &y, &z, &n) >= 3 && n == nob &&
188                     x < y) {
189                         /* strided range */
190                         new_lstrs(list, l->lstr_str, b2, x, y, z);
191                         continue;
192                 }
193
194                 /* syntax error */
195                 return -1;
196         } while ((expr = sep) != NULL);
197
198         free_lstr(l);
199
200         return 1;
201 }
202
203 int
204 expand_strs(char *str, lstr_t **head)
205 {
206         lstr_t  *list = NULL;
207         lstr_t  *nlist;
208         lstr_t  *l;
209         int      rc = 0;
210         int      expanded;
211
212         l = alloc_lstr(strlen(str) + 1);
213         memcpy(l->lstr_str, str, strlen(str) + 1);
214         l->lstr_next = NULL;
215         list = l;
216
217         do {
218                 expanded = 0;
219                 nlist = NULL;
220
221                 while ((l = list) != NULL) {
222                         list = l->lstr_next;
223
224                         rc = expand_lstr(&nlist, l);
225                         if (rc < 0) {
226                                 fprintf(stderr, "Syntax error in \"%s\"\n", str);
227                                 free_lstr(l);
228                                 break;
229                         }
230
231                         expanded |= rc > 0;
232                 }
233
234                 /* re-order onto 'list' */
235                 while ((l = nlist) != NULL) {
236                         nlist = l->lstr_next;
237                         l->lstr_next = list;
238                         list = l;
239                 }
240
241         } while (expanded && rc > 0);
242
243         if (rc >= 0) {
244                 *head = list;
245                 return 0;
246         }
247
248         while ((l = list) != NULL) {
249                 list = l->lstr_next;
250
251                 free_lstr(l);
252         }
253         return rc;
254 }
255
256 int
257 lst_parse_nids(char *str, int *countp, struct lnet_process_id **idspp)
258 {
259         lstr_t  *head = NULL;
260         lstr_t  *l;
261         int      c = 0;
262         int      i;
263         int      rc;
264
265         rc = expand_strs(str, &head);
266         if (rc != 0)
267                 goto out;
268
269         l = head;
270         while (l != NULL) {
271                 l = l->lstr_next;
272                 c++;
273         }
274
275         *idspp = malloc(c * sizeof(struct lnet_process_id));
276         if (*idspp == NULL) {
277                 fprintf(stderr, "Out of memory\n");
278                 rc = -1;
279         }
280
281         *countp = c;
282 out:
283         i = 0;
284         while ((l = head) != NULL) {
285                 head = l->lstr_next;
286
287                 if (rc == 0) {
288                         (*idspp)[i].nid = libcfs_str2nid(l->lstr_str);
289                         if ((*idspp)[i].nid == LNET_NID_ANY) {
290                                 fprintf(stderr, "Invalid nid: %s\n",
291                                         l->lstr_str);
292                                 rc = -1;
293                         }
294
295                         (*idspp)[i].pid = LNET_PID_LUSTRE;
296                         i++;
297                 }
298
299                 free_lstr(l);
300         }
301
302         if (rc == 0)
303                 return 0;
304
305         free(*idspp);
306         *idspp = NULL;
307
308         return rc;
309 }
310
311 char *
312 lst_node_state2str(int state)
313 {
314         if (state == LST_NODE_ACTIVE)
315                 return "Active";
316         if (state == LST_NODE_BUSY)
317                 return "Busy";
318         if (state == LST_NODE_DOWN)
319                 return "Down";
320
321         return "Unknown";
322 }
323
324 int
325 lst_node_str2state(char *str)
326 {
327         if (strcasecmp(str, "active") == 0)
328                 return LST_NODE_ACTIVE;
329         if (strcasecmp(str, "busy") == 0)
330                 return LST_NODE_BUSY;
331         if (strcasecmp(str, "down") == 0)
332                 return LST_NODE_DOWN;
333         if (strcasecmp(str, "unknown") == 0)
334                 return LST_NODE_UNKNOWN;
335         if (strcasecmp(str, "invalid") == 0)
336                 return (LST_NODE_UNKNOWN | LST_NODE_DOWN | LST_NODE_BUSY);
337
338         return -1;
339 }
340
341 char *
342 lst_test_type2name(int type)
343 {
344         if (type == LST_TEST_PING)
345                 return "ping";
346         if (type == LST_TEST_BULK)
347                 return "brw";
348
349         return "unknown";
350 }
351
352 int
353 lst_test_name2type(char *name)
354 {
355         if (strcasecmp(name, "ping") == 0)
356                 return LST_TEST_PING;
357         if (strcasecmp(name, "brw") == 0)
358                 return LST_TEST_BULK;
359
360         return -1;
361 }
362
363 void
364 lst_print_usage(char *cmd)
365 {
366         Parser_printhelp(cmd);
367 }
368
369 void
370 lst_print_error(char *sub, const char *def_format, ...)
371 {
372         va_list ap;
373
374         /* local error returned from kernel */
375         switch (errno) {
376         case ESRCH:
377                 fprintf(stderr, "No session exists\n");
378                 return;
379         case ESHUTDOWN:
380                 fprintf(stderr, "Session is shutting down\n");
381                 return;
382         case EACCES:
383                 fprintf(stderr, "Unmatched session key or not root\n");
384                 return;
385         case ENOENT:
386                 fprintf(stderr, "Can't find %s in current session\n", sub);
387                 return;
388         case EINVAL:
389                 fprintf(stderr, "Invalid parameters list in command line\n");
390                 return;
391         case EFAULT:
392                 fprintf(stderr, "Bad parameter address\n");
393                 return;
394         case EEXIST:
395                 fprintf(stderr, "%s already exists\n", sub);
396                 return;
397         default:
398                 va_start(ap, def_format);
399                 vfprintf(stderr, def_format, ap);
400                 va_end(ap);
401
402                 return;
403         }
404 }
405
406 void
407 lst_free_rpcent(struct list_head *head)
408 {
409         struct lstcon_rpc_ent *ent;
410
411         while (!list_empty(head)) {
412                 ent = list_entry(head->next, struct lstcon_rpc_ent, rpe_link);
413
414                 list_del(&ent->rpe_link);
415                 free(ent);
416         }
417 }
418
419 void
420 lst_reset_rpcent(struct list_head *head)
421 {
422         struct lstcon_rpc_ent *ent;
423
424         list_for_each_entry(ent, head, rpe_link) {
425                 ent->rpe_sid       = LST_INVALID_SID;
426                 ent->rpe_peer.nid  = LNET_NID_ANY;
427                 ent->rpe_peer.pid  = LNET_PID_ANY;
428                 ent->rpe_rpc_errno = ent->rpe_fwk_errno = 0;
429         }
430 }
431
432 int
433 lst_alloc_rpcent(struct list_head *head, int count, int offset)
434 {
435         struct lstcon_rpc_ent *ent;
436         int               i;
437
438         for (i = 0; i < count; i++) {
439                 ent = malloc(offsetof(struct lstcon_rpc_ent, rpe_payload[offset]));
440                 if (ent == NULL) {
441                         lst_free_rpcent(head);
442                         return -1;
443                 }
444
445                 memset(ent, 0, offsetof(struct lstcon_rpc_ent, rpe_payload[offset]));
446
447                 ent->rpe_sid      = LST_INVALID_SID;
448                 ent->rpe_peer.nid = LNET_NID_ANY;
449                 ent->rpe_peer.pid = LNET_PID_ANY;
450                 list_add(&ent->rpe_link, head);
451         }
452
453         return 0;
454 }
455
456 void
457 lst_print_transerr(struct list_head *head, char *optstr)
458 {
459         struct lstcon_rpc_ent *ent;
460
461         list_for_each_entry(ent, head, rpe_link) {
462                 if (ent->rpe_rpc_errno == 0 && ent->rpe_fwk_errno == 0)
463                         continue;
464
465                 if (ent->rpe_rpc_errno != 0) {
466                         fprintf(stderr, "%s RPC failed on %s: %s\n",
467                                 optstr, libcfs_id2str(ent->rpe_peer),
468                                 strerror(ent->rpe_rpc_errno));
469                         continue;
470                 }
471
472                 fprintf(stderr, "operation %s failed on %s: %s\n",
473                         optstr, libcfs_id2str(ent->rpe_peer),
474                         strerror(ent->rpe_fwk_errno));
475         }
476 }
477
478 int lst_info_batch_ioctl(char *batch, int test, int server,
479                         struct lstcon_test_batch_ent *entp, int *idxp,
480                         int *ndentp, struct lstcon_node_ent *dentsp);
481
482 int lst_info_group_ioctl(char *name, struct lstcon_ndlist_ent *gent,
483                          int *idx, int *count, struct lstcon_node_ent *dents);
484
485 int lst_query_batch_ioctl(char *batch, int test, int server,
486                           int timeout, struct list_head *head);
487
488 int
489 lst_ioctl(unsigned int opc, void *buf, int len)
490 {
491         struct libcfs_ioctl_data data;
492         int    rc;
493
494         LIBCFS_IOC_INIT (data);
495         data.ioc_u32[0]  = opc;
496         data.ioc_plen1   = len;
497         data.ioc_pbuf1   = (char *)buf;
498         data.ioc_plen2   = sizeof(trans_stat);
499         data.ioc_pbuf2   = (char *)&trans_stat;
500
501         memset(&trans_stat, 0, sizeof(trans_stat));
502
503         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNETST, &data);
504
505         /* local error, no valid RPC result */
506         if (rc != 0)
507                 return -1;
508
509         /* RPC error */
510         if (trans_stat.trs_rpc_errno != 0)
511                 return -2;
512
513         /* Framework error */
514         if (trans_stat.trs_fwk_errno != 0)
515                 return -3;
516
517         return 0;
518 }
519
520 int
521 lst_new_session_ioctl(char *name, int timeout, int force, struct lst_sid *sid)
522 {
523         struct lstio_session_new_args args = { 0 };
524
525         args.lstio_ses_key     = session_key;
526         args.lstio_ses_timeout = timeout;
527         args.lstio_ses_force   = force;
528         args.lstio_ses_idp     = sid;
529         args.lstio_ses_feats   = session_features;
530         args.lstio_ses_nmlen   = strlen(name);
531         args.lstio_ses_namep   = name;
532
533         return lst_ioctl (LSTIO_SESSION_NEW, &args, sizeof(args));
534 }
535
536 int
537 jt_lst_new_session(int argc, char **argv)
538 {
539         char  buf[LST_NAME_SIZE * 2 + 1];
540         char *name;
541         int   optidx = 0;
542         int   timeout = 300;
543         int   force = 0;
544         int   c;
545         int   rc;
546
547         static const struct option session_opts[] = {
548                 { .name = "timeout", .has_arg = required_argument, .val = 't' },
549                 { .name = "force",   .has_arg = no_argument,       .val = 'f' },
550                 { .name = NULL } };
551
552         if (session_key == 0) {
553                 fprintf(stderr,
554                         "Can't find env LST_SESSION or value is not valid\n");
555                 return -1;
556         }
557
558         while (1) {
559
560                 c = getopt_long(argc, argv, "ft:",
561                                 session_opts, &optidx);
562
563                 if (c == -1)
564                         break;
565
566                 switch (c) {
567                 case 'f':
568                         force = 1;
569                         break;
570                 case 't':
571                         timeout = atoi(optarg);
572                         break;
573                 default:
574                         lst_print_usage(argv[0]);
575                         return -1;
576                 }
577         }
578
579         if (timeout <= 0) {
580                 fprintf(stderr, "Invalid timeout value\n");
581                 return -1;
582         }
583
584         if (optind == argc - 1) {
585                 name = argv[optind ++];
586                 if (strlen(name) >= LST_NAME_SIZE) {
587                         fprintf(stderr, "Name size is limited to %d\n",
588                                 LST_NAME_SIZE - 1);
589                         return -1;
590                 }
591
592         } else if (optind == argc) {
593                 char           user[LST_NAME_SIZE];
594                 char           host[LST_NAME_SIZE];
595                 struct passwd *pw = getpwuid(getuid());
596
597                 if (pw == NULL)
598                         snprintf(user, sizeof(user), "%d", (int)getuid());
599                 else
600                         snprintf(user, sizeof(user), "%s", pw->pw_name);
601
602                 rc = gethostname(host, sizeof(host));
603                 if (rc != 0)
604                         snprintf(host, sizeof(host), "unknown_host");
605
606                 snprintf(buf, sizeof(buf), "%s@%s", user, host);
607                 name = buf;
608
609         } else {
610                 lst_print_usage(argv[0]);
611                 return -1;
612         }
613
614         rc = lst_new_session_ioctl(name, timeout, force, &session_id);
615         if (rc != 0) {
616                 lst_print_error("session", "Failed to create session: %s\n",
617                                 strerror(errno));
618                 return rc;
619         }
620
621         fprintf(stdout, "SESSION: %s FEATURES: %x TIMEOUT: %d FORCE: %s\n",
622                 name, session_features, timeout, force ? "Yes" : "No");
623         return 0;
624 }
625
626 int
627 lst_session_info_ioctl(char *name, int len, int *key, unsigned *featp,
628                        struct lst_sid *sid, struct lstcon_ndlist_ent *ndinfo)
629 {
630         struct lstio_session_info_args args = { 0 };
631
632         args.lstio_ses_idp     = sid;
633         args.lstio_ses_keyp    = key;
634         args.lstio_ses_featp   = featp;
635         args.lstio_ses_ndinfo  = ndinfo;
636         args.lstio_ses_nmlen   = len;
637         args.lstio_ses_namep   = name;
638
639         return lst_ioctl(LSTIO_SESSION_INFO, &args, sizeof(args));
640 }
641
642 int
643 jt_lst_show_session(int argc, char **argv)
644 {
645         struct lstcon_ndlist_ent ndinfo;
646         struct lst_sid sid;
647         char                name[LST_NAME_SIZE];
648         unsigned            feats;
649         int                 key;
650         int                 rc;
651
652         rc = lst_session_info_ioctl(name, sizeof(name), &key,
653                                     &feats, &sid, &ndinfo);
654
655         if (rc != 0) {
656                 lst_print_error("session", "Failed to show session: %s\n",
657                                 strerror(errno));
658                 return -1;
659         }
660
661         fprintf(stdout, "%s ID: %ju@%s, KEY: %d FEATURES: %x NODES: %d\n",
662                 name, (uintmax_t)sid.ses_stamp, libcfs_nid2str(sid.ses_nid),
663                 key, feats, ndinfo.nle_nnode);
664
665         return 0;
666 }
667
668 int
669 lst_end_session_ioctl(void)
670 {
671         struct lstio_session_end_args args = { 0 };
672
673         args.lstio_ses_key = session_key;
674         return lst_ioctl(LSTIO_SESSION_END, &args, sizeof(args));
675 }
676
677 int
678 jt_lst_end_session(int argc, char **argv)
679 {
680         int             rc;
681
682         if (session_key == 0) {
683                 fprintf(stderr,
684                         "Can't find env LST_SESSION or value is not valid\n");
685                 return -1;
686         }
687
688         rc = lst_end_session_ioctl();
689
690         if (rc == 0) {
691                 fprintf(stdout, "session is ended\n");
692                 return 0;
693         }
694
695         if (rc == -1) {
696                 lst_print_error("session", "Failed to end session: %s\n",
697                                 strerror(errno));
698                 return rc;
699         }
700
701         if (trans_stat.trs_rpc_errno != 0) {
702                 fprintf(stderr,
703                         "[RPC] Failed to send %d session RPCs: %s\n",
704                         lstcon_rpc_stat_failure(&trans_stat, 0),
705                         strerror(trans_stat.trs_rpc_errno));
706         }
707
708         if (trans_stat.trs_fwk_errno != 0) {
709                 fprintf(stderr,
710                         "[FWK] Failed to end session on %d nodes: %s\n",
711                         lstcon_sesop_stat_failure(&trans_stat, 0),
712                         strerror(trans_stat.trs_fwk_errno));
713         }
714
715         return rc;
716 }
717
718 int
719 lst_ping_ioctl(char *str, int type, int timeout,
720                int count, struct lnet_process_id *ids, struct list_head *head)
721 {
722         struct lstio_debug_args args = { 0 };
723
724         args.lstio_dbg_key     = session_key;
725         args.lstio_dbg_type    = type;
726         args.lstio_dbg_flags   = 0;
727         args.lstio_dbg_timeout = timeout;
728         args.lstio_dbg_nmlen   = (str == NULL) ? 0: strlen(str);
729         args.lstio_dbg_namep   = str;
730         args.lstio_dbg_count   = count;
731         args.lstio_dbg_idsp    = ids;
732         args.lstio_dbg_resultp = head;
733
734         return lst_ioctl (LSTIO_DEBUG, &args, sizeof(args));
735 }
736
737 int
738 lst_get_node_count(int type, char *str, int *countp,
739                    struct lnet_process_id **idspp)
740 {
741         char                    buf[LST_NAME_SIZE];
742         struct lstcon_test_batch_ent ent;
743         struct lstcon_ndlist_ent    *entp = &ent.tbe_cli_nle;
744         struct lst_sid sid;
745         unsigned                feats;
746         int                     key;
747         int                     rc;
748
749         switch (type) {
750         case LST_OPC_SESSION:
751                 rc = lst_session_info_ioctl(buf, LST_NAME_SIZE,
752                                             &key, &feats, &sid, entp);
753                 break;
754
755         case LST_OPC_BATCHSRV:
756                 entp = &ent.tbe_srv_nle;
757         case LST_OPC_BATCHCLI:
758                 rc = lst_info_batch_ioctl(str, 0, 0, &ent, NULL, NULL, NULL);
759                 break;
760
761         case LST_OPC_GROUP:
762                 rc = lst_info_group_ioctl(str, entp, NULL, NULL, NULL);
763                 break;
764
765         case LST_OPC_NODES:
766                 rc = lst_parse_nids(str, &entp->nle_nnode, idspp) < 0 ? -1 : 0;
767                 break;
768
769         default:
770                 rc = -1;
771                 break;
772         }
773
774         if (rc == 0)
775                 *countp = entp->nle_nnode;
776
777         return rc;
778 }
779
780 int
781 jt_lst_ping(int argc,  char **argv)
782 {
783         struct list_head   head;
784         struct lnet_process_id *ids = NULL;
785         struct lstcon_rpc_ent  *ent = NULL;
786         char              *str = NULL;
787         int                optidx  = 0;
788         int                server  = 0;
789         int                timeout = 5;
790         int                count   = 0;
791         int                type    = 0;
792         int                rc      = 0;
793         int                c;
794
795         static const struct option ping_opts[] = {
796                 { .name = "session", .has_arg = no_argument,       .val = 's' },
797                 { .name = "server",  .has_arg = no_argument,       .val = 'v' },
798                 { .name = "batch",   .has_arg = required_argument, .val = 'b' },
799                 { .name = "group",   .has_arg = required_argument, .val = 'g' },
800                 { .name = "nodes",   .has_arg = required_argument, .val = 'n' },
801                 { .name = "timeout", .has_arg = required_argument, .val = 't' },
802                 { .name = NULL, } };
803
804         if (session_key == 0) {
805                 fprintf(stderr,
806                         "Can't find env LST_SESSION or value is not valid\n");
807                 return -1;
808         }
809
810         while (1) {
811
812                 c = getopt_long(argc, argv, "g:b:n:t:sv",
813                                 ping_opts, &optidx);
814
815                 if (c == -1)
816                         break;
817
818                 switch (c) {
819                 case 's':
820                         type = LST_OPC_SESSION;
821                         break;
822
823                 case 'g':
824                         type = LST_OPC_GROUP;
825                         str = optarg;
826                         break;
827
828                 case 'b':
829                         type = LST_OPC_BATCHCLI;
830                         str = optarg;
831                         break;
832
833                 case 'n':
834                         type = LST_OPC_NODES;
835                         str = optarg;
836                         break;
837
838                 case 't':
839                         timeout = atoi(optarg);
840                         break;
841
842                 case 'v':
843                         server = 1;
844                         break;
845
846                 default:
847                         lst_print_usage(argv[0]);
848                         return -1;
849                 }
850         }
851
852         if (type == 0 || timeout <= 0 || optind != argc) {
853                 lst_print_usage(argv[0]);
854                 return -1;
855         }
856
857         if (type == LST_OPC_BATCHCLI && server)
858                 type = LST_OPC_BATCHSRV;
859
860         rc = lst_get_node_count(type, str, &count, &ids);
861         if (rc < 0) {
862                 fprintf(stderr, "Failed to get count of nodes from %s: %s\n",
863                         (str == NULL) ? "session" : str, strerror(errno));
864                 return -1;
865         }
866
867         INIT_LIST_HEAD(&head);
868
869         rc = lst_alloc_rpcent(&head, count, LST_NAME_SIZE);
870         if (rc != 0) {
871                 fprintf(stderr, "Out of memory\n");
872                 goto out;
873         }
874
875         if (count == 0) {
876                 fprintf(stdout, "Target %s is empty\n",
877                         (str == NULL) ? "session" : str);
878                 goto out;
879         }
880
881         rc = lst_ping_ioctl(str, type, timeout, count, ids, &head);
882         if (rc == -1) { /* local failure */
883                 lst_print_error("debug", "Failed to ping %s: %s\n",
884                                 (str == NULL) ? "session" : str,
885                                 strerror(errno));
886                 rc = -1;
887                 goto out;
888         }
889
890         /* ignore RPC errors and framwork errors */
891         list_for_each_entry(ent, &head, rpe_link) {
892                 fprintf(stdout, "\t%s: %s [session: %s id: %s]\n",
893                         libcfs_id2str(ent->rpe_peer),
894                         lst_node_state2str(ent->rpe_state),
895                         (ent->rpe_state == LST_NODE_ACTIVE ||
896                          ent->rpe_state == LST_NODE_BUSY) ?
897                                 (ent->rpe_rpc_errno == 0 ?
898                                         &ent->rpe_payload[0] : "Unknown") :
899                                 "<NULL>", libcfs_nid2str(ent->rpe_sid.ses_nid));
900         }
901
902 out:
903         lst_free_rpcent(&head);
904
905         if (ids != NULL)
906                 free(ids);
907
908         return rc;
909
910 }
911
912 int
913 lst_add_nodes_ioctl(char *name, int count, struct lnet_process_id *ids,
914                     unsigned *featp, struct list_head *resultp)
915 {
916         struct lstio_group_nodes_args args = { 0 };
917
918         args.lstio_grp_key     = session_key;
919         args.lstio_grp_nmlen   = strlen(name);
920         args.lstio_grp_namep   = name;
921         args.lstio_grp_count   = count;
922         args.lstio_grp_featp   = featp;
923         args.lstio_grp_idsp    = ids;
924         args.lstio_grp_resultp = resultp;
925
926         return lst_ioctl(LSTIO_NODES_ADD, &args, sizeof(args));
927 }
928
929 int
930 lst_del_group_ioctl(char *name)
931 {
932         struct lstio_group_del_args args = { 0 };
933
934         args.lstio_grp_key   = session_key;
935         args.lstio_grp_nmlen = strlen(name);
936         args.lstio_grp_namep = name;
937
938         return lst_ioctl(LSTIO_GROUP_DEL, &args, sizeof(args));
939 }
940
941 int
942 lst_del_group(char *grp_name)
943 {
944         int     rc;
945
946         rc = lst_del_group_ioctl(grp_name);
947         if (rc == 0) {
948                 fprintf(stdout, "Group is deleted\n");
949                 return 0;
950         }
951
952         if (rc == -1) {
953                 lst_print_error("group", "Failed to delete group: %s\n",
954                                 strerror(errno));
955                 return rc;
956         }
957
958         fprintf(stderr, "Group is deleted with some errors\n");
959
960         if (trans_stat.trs_rpc_errno != 0) {
961                 fprintf(stderr,
962                         "[RPC] Failed to send %d end session RPCs: %s\n",
963                         lstcon_rpc_stat_failure(&trans_stat, 0),
964                         strerror(trans_stat.trs_rpc_errno));
965         }
966
967         if (trans_stat.trs_fwk_errno != 0) {
968                 fprintf(stderr,
969                         "[FWK] Failed to end session on %d nodes: %s\n",
970                 lstcon_sesop_stat_failure(&trans_stat, 0),
971                 strerror(trans_stat.trs_fwk_errno));
972         }
973
974         return -1;
975 }
976
977 int
978 lst_add_group_ioctl(char *name)
979 {
980         struct lstio_group_add_args args = { 0 };
981
982         args.lstio_grp_key     =  session_key;
983         args.lstio_grp_nmlen   =  strlen(name);
984         args.lstio_grp_namep   =  name;
985
986         return lst_ioctl(LSTIO_GROUP_ADD, &args, sizeof(args));
987 }
988
989 int
990 jt_lst_add_group(int argc, char **argv)
991 {
992         struct list_head   head;
993         struct lnet_process_id *ids;
994         char              *name;
995         unsigned           feats = session_features;
996         int                count;
997         int                rc;
998         int                i;
999         bool               nodes_added = false;
1000
1001         if (session_key == 0) {
1002                 fprintf(stderr,
1003                         "Can't find env LST_SESSION or value is not valid\n");
1004                 return -1;
1005         }
1006
1007         if (argc < 3) {
1008                 lst_print_usage(argv[0]);
1009                 return -1;
1010         }
1011
1012         name = argv[1];
1013         if (strlen(name) >= LST_NAME_SIZE) {
1014                 fprintf(stderr, "Name length is limited to %d\n",
1015                         LST_NAME_SIZE - 1);
1016                 return -1;
1017         }
1018
1019         rc = lst_add_group_ioctl(name);
1020         if (rc != 0) {
1021                 lst_print_error("group", "Failed to add group %s: %s\n",
1022                                 name, strerror(errno));
1023                 return -1;
1024         }
1025
1026         INIT_LIST_HEAD(&head);
1027
1028         for (i = 2; i < argc; i++) {
1029                 /* parse address list */
1030                 rc = lst_parse_nids(argv[i], &count, &ids);
1031                 if (rc < 0) {
1032                         fprintf(stderr, "Ignore invalid id list %s\n",
1033                                 argv[i]);
1034                         continue;
1035                 }
1036
1037                 if (count == 0)
1038                         continue;
1039
1040                 rc = lst_alloc_rpcent(&head, count, 0);
1041                 if (rc != 0) {
1042                         fprintf(stderr, "Out of memory\n");
1043                         free(ids);
1044                         rc = -1;
1045                         goto failed;
1046                 }
1047
1048                 rc = lst_add_nodes_ioctl(name, count, ids, &feats, &head);
1049
1050                 free(ids);
1051
1052                 if (rc != 0)
1053                         goto failed;
1054
1055                 fprintf(stdout, "%s are added to session\n", argv[i]);
1056
1057                 nodes_added = true;
1058
1059                 if ((feats & session_features) != session_features) {
1060                         fprintf(stdout,
1061                                 "Warning, this session will run with "
1062                                 "compatible mode because some test nodes "
1063                                 "might not understand these features: %x\n",
1064                                 (~feats & session_features));
1065                 }
1066
1067                 lst_free_rpcent(&head);
1068         }
1069
1070         if (!nodes_added) {
1071                 /*
1072                  * The selftest kernel module expects that a group should
1073                  * have at least one node, since it doesn't make sense for
1074                  * an empty group to be added to a test.
1075                  */
1076                 fprintf(stderr,
1077                         "No nodes added successfully, deleting group %s\n",
1078                         name);
1079                 rc = lst_del_group(name);
1080                 if (rc != 0) {
1081                         fprintf(stderr,
1082                                 "Failed to delete group %s."
1083                                 "  Group is empty.\n", name);
1084                 }
1085         }
1086
1087         return rc;
1088
1089 failed:
1090         if (rc == -1) {
1091                 lst_print_error("group", "Failed to add nodes %s: %s\n",
1092                                 argv[i], strerror(errno));
1093
1094         } else {
1095                 if (trans_stat.trs_fwk_errno == EPROTO) {
1096                         fprintf(stderr,
1097                                 "test nodes might have different LST "
1098                                 "features, please disable some features by "
1099                                 "setting LST_FEATURES\n");
1100                 }
1101
1102                 lst_print_transerr(&head, "create session");
1103         }
1104
1105         lst_free_rpcent(&head);
1106
1107         if (!nodes_added) {
1108                 fprintf(stderr,
1109                         "No nodes added successfully, deleting group %s\n",
1110                         name);
1111                 if (lst_del_group(name) != 0) {
1112                         fprintf(stderr,
1113                                 "Failed to delete group %s."
1114                                 "  Group is empty.\n", name);
1115                 }
1116         }
1117
1118         return rc;
1119 }
1120
1121 int
1122 jt_lst_del_group(int argc, char **argv)
1123 {
1124         int     rc;
1125
1126         if (session_key == 0) {
1127                 fprintf(stderr,
1128                         "Can't find env LST_SESSION or value is not valid\n");
1129                 return -1;
1130         }
1131
1132         if (argc != 2) {
1133                 lst_print_usage(argv[0]);
1134                 return -1;
1135         }
1136
1137         rc = lst_del_group(argv[1]);
1138
1139         return rc;
1140 }
1141
1142 int
1143 lst_update_group_ioctl(int opc, char *name, int clean, int count,
1144                        struct lnet_process_id *ids, struct list_head *resultp)
1145 {
1146         struct lstio_group_update_args args = { 0 };
1147
1148         args.lstio_grp_key      = session_key;
1149         args.lstio_grp_opc      = opc;
1150         args.lstio_grp_args     = clean;
1151         args.lstio_grp_nmlen    = strlen(name);
1152         args.lstio_grp_namep    = name;
1153         args.lstio_grp_count    = count;
1154         args.lstio_grp_idsp     = ids;
1155         args.lstio_grp_resultp  = resultp;
1156
1157         return lst_ioctl(LSTIO_GROUP_UPDATE, &args, sizeof(args));
1158 }
1159
1160 int
1161 jt_lst_update_group(int argc, char **argv)
1162 {
1163         struct list_head   head;
1164         struct lnet_process_id *ids = NULL;
1165         char              *str = NULL;
1166         char              *grp = NULL;
1167         int                optidx = 0;
1168         int                count = 0;
1169         int                clean = 0;
1170         int                opc = 0;
1171         int                rc;
1172         int                c;
1173
1174         static const struct option update_group_opts[] = {
1175                 { .name = "refresh", .has_arg = no_argument,       .val = 'f' },
1176                 { .name = "clean",   .has_arg = required_argument, .val = 'c' },
1177                 { .name = "remove",  .has_arg = required_argument, .val = 'r' },
1178                 { .name = NULL } };
1179
1180         if (session_key == 0) {
1181                 fprintf(stderr,
1182                         "Can't find env LST_SESSION or value is not valid\n");
1183                 return -1;
1184         }
1185
1186         while (1) {
1187                 c = getopt_long(argc, argv, "fc:r:",
1188                                 update_group_opts, &optidx);
1189
1190                 /* Detect the end of the options. */
1191                 if (c == -1)
1192                         break;
1193
1194                 switch (c) {
1195                 case 'f':
1196                         if (opc != 0) {
1197                                 lst_print_usage(argv[0]);
1198                                 return -1;
1199                         }
1200                         opc = LST_GROUP_REFRESH;
1201                         break;
1202
1203                 case 'r':
1204                         if (opc != 0) {
1205                                 lst_print_usage(argv[0]);
1206                                 return -1;
1207                         }
1208                         opc = LST_GROUP_RMND;
1209                         str = optarg;
1210                         break;
1211
1212                 case 'c':
1213                         clean = lst_node_str2state(optarg);
1214                         if (opc != 0 || clean <= 0) {
1215                                 lst_print_usage(argv[0]);
1216                                 return -1;
1217                         }
1218                         opc = LST_GROUP_CLEAN;
1219                         break;
1220
1221                 default:
1222                         lst_print_usage(argv[0]);
1223                         return -1;
1224                 }
1225         }
1226
1227         /* no OPC or group is specified */
1228         if (opc == 0 || optind != argc - 1) {
1229                 lst_print_usage(argv[0]);
1230                 return -1;
1231         }
1232
1233         grp = argv[optind];
1234
1235         INIT_LIST_HEAD(&head);
1236
1237         if (opc == LST_GROUP_RMND || opc == LST_GROUP_REFRESH) {
1238                 rc = lst_get_node_count(opc == LST_GROUP_RMND ? LST_OPC_NODES :
1239                                                                 LST_OPC_GROUP,
1240                                         opc == LST_GROUP_RMND ? str : grp,
1241                                         &count, &ids);
1242
1243                 if (rc != 0) {
1244                         fprintf(stderr, "Can't get count of nodes from %s: %s\n",
1245                                 opc == LST_GROUP_RMND ? str : grp,
1246                                 strerror(errno));
1247                         return -1;
1248                 }
1249
1250                 rc = lst_alloc_rpcent(&head, count, 0);
1251                 if (rc != 0) {
1252                         fprintf(stderr, "Out of memory\n");
1253                         free(ids);
1254                         return -1;
1255                 }
1256
1257         }
1258
1259         rc = lst_update_group_ioctl(opc, grp, clean, count, ids, &head);
1260
1261         if (ids != NULL)
1262                 free(ids);
1263
1264         if (rc == 0) {
1265                 lst_free_rpcent(&head);
1266                 return 0;
1267         }
1268
1269         if (rc == -1) {
1270                 lst_free_rpcent(&head);
1271                 lst_print_error("group", "Failed to update group: %s\n",
1272                                 strerror(errno));
1273                 return rc;
1274         }
1275
1276         lst_print_transerr(&head, "Updating group");
1277
1278         lst_free_rpcent(&head);
1279
1280         return rc;
1281 }
1282
1283 int
1284 lst_list_group_ioctl(int len, char *name, int idx)
1285 {
1286         struct lstio_group_list_args args = { 0 };
1287
1288         args.lstio_grp_key   = session_key;
1289         args.lstio_grp_idx   = idx;
1290         args.lstio_grp_nmlen = len;
1291         args.lstio_grp_namep = name;
1292
1293         return lst_ioctl(LSTIO_GROUP_LIST, &args, sizeof(args));
1294 }
1295
1296 int
1297 lst_info_group_ioctl(char *name, struct lstcon_ndlist_ent *gent,
1298                      int *idx, int *count, struct lstcon_node_ent *dents)
1299 {
1300         struct lstio_group_info_args args = { 0 };
1301
1302         args.lstio_grp_key    = session_key;
1303         args.lstio_grp_nmlen  = strlen(name);
1304         args.lstio_grp_namep  = name;
1305         args.lstio_grp_entp   = gent;
1306         args.lstio_grp_idxp   = idx;
1307         args.lstio_grp_ndentp = count;
1308         args.lstio_grp_dentsp = dents;
1309
1310         return lst_ioctl(LSTIO_GROUP_INFO, &args, sizeof(args));
1311 }
1312
1313 int
1314 lst_list_group_all(void)
1315 {
1316         char  name[LST_NAME_SIZE];
1317         int   rc;
1318         int   i;
1319
1320         /* no group is specified, list name of all groups */
1321         for (i = 0; ; i++) {
1322                 rc = lst_list_group_ioctl(LST_NAME_SIZE, name, i);
1323                 if (rc == 0) {
1324                         fprintf(stdout, "%d) %s\n", i + 1, name);
1325                         continue;
1326                 }
1327
1328                 if (errno == ENOENT)
1329                         break;
1330
1331                 lst_print_error("group", "Failed to list group: %s\n",
1332                                 strerror(errno));
1333                 return -1;
1334         }
1335
1336         fprintf(stdout, "Total %d groups\n", i);
1337
1338         return 0;
1339 }
1340
1341 #define LST_NODES_TITLE "\tACTIVE\tBUSY\tDOWN\tUNKNOWN\tTOTAL\n"
1342
1343 int
1344 jt_lst_list_group(int argc, char **argv)
1345 {
1346         struct lstcon_ndlist_ent gent;
1347         struct lstcon_node_ent   *dents;
1348         int optidx  = 0;
1349         int verbose = 0;
1350         int active  = 0;
1351         int busy    = 0;
1352         int down    = 0;
1353         int unknown = 0;
1354         int all     = 0;
1355         int count;
1356         int index;
1357         int i;
1358         int j;
1359         int c;
1360         int rc      = 0;
1361
1362         static const struct option list_group_opts[] = {
1363                 { .name = "active",  .has_arg = no_argument, .val = 'a' },
1364                 { .name = "busy",    .has_arg = no_argument, .val = 'b' },
1365                 { .name = "down",    .has_arg = no_argument, .val = 'd' },
1366                 { .name = "unknown", .has_arg = no_argument, .val = 'u' },
1367                 { .name = "all",     .has_arg = no_argument, .val = 'l' },
1368                 { .name = NULL, } };
1369
1370         if (session_key == 0) {
1371                 fprintf(stderr,
1372                         "Can't find env LST_SESSION or value is not valid\n");
1373                 return -1;
1374         }
1375
1376         while (1) {
1377                 c = getopt_long(argc, argv, "abdul",
1378                                 list_group_opts, &optidx);
1379
1380                 if (c == -1)
1381                         break;
1382
1383                 switch (c) {
1384                 case 'a':
1385                         verbose = active = 1;
1386                         all = 0;
1387                         break;
1388                 case 'b':
1389                         verbose = busy = 1;
1390                         all = 0;
1391                         break;
1392                 case 'd':
1393                         verbose = down = 1;
1394                         all = 0;
1395                         break;
1396                 case 'u':
1397                         verbose = unknown = 1;
1398                         all = 0;
1399                         break;
1400                 case 'l':
1401                         verbose = all = 1;
1402                         break;
1403                 default:
1404                         lst_print_usage(argv[0]);
1405                         return -1;
1406                 }
1407         }
1408
1409         if (optind == argc) {
1410                 /* no group is specified, list name of all groups */
1411                 rc = lst_list_group_all();
1412
1413                 return rc;
1414         }
1415
1416         if (!verbose)
1417                 fprintf(stdout, LST_NODES_TITLE);
1418
1419         /* list nodes in specified groups */
1420         for (i = optind; i < argc; i++) {
1421                 rc = lst_info_group_ioctl(argv[i], &gent, NULL, NULL, NULL);
1422                 if (rc != 0) {
1423                         if (errno == ENOENT) {
1424                                 rc = 0;
1425                                 break;
1426                         }
1427
1428                         lst_print_error("group", "Failed to list group\n",
1429                                         strerror(errno));
1430                         break;
1431                 }
1432
1433                 if (!verbose) {
1434                         fprintf(stdout, "\t%d\t%d\t%d\t%d\t%d\t%s\n",
1435                                 gent.nle_nactive, gent.nle_nbusy,
1436                                 gent.nle_ndown, gent.nle_nunknown,
1437                                 gent.nle_nnode, argv[i]);
1438                         continue;
1439                 }
1440
1441                 fprintf(stdout, "Group [ %s ]\n", argv[i]);
1442
1443                 if (gent.nle_nnode == 0) {
1444                         fprintf(stdout, "No nodes found [ %s ]\n", argv[i]);
1445                         continue;
1446                 }
1447
1448                 count = gent.nle_nnode;
1449
1450                 dents = malloc(count * sizeof(struct lstcon_node_ent));
1451                 if (dents == NULL) {
1452                         fprintf(stderr, "Failed to malloc: %s\n",
1453                                 strerror(errno));
1454                         return -1;
1455                 }
1456
1457                 index = 0;
1458                 rc = lst_info_group_ioctl(argv[i], &gent, &index, &count, dents);
1459                 if (rc != 0) {
1460                         lst_print_error("group", "Failed to list group: %s\n",
1461                                         strerror(errno));
1462                         free(dents);
1463                         return -1;
1464                 }
1465
1466                 for (j = 0, c = 0; j < count; j++) {
1467                         if (all ||
1468                             ((active  &&  dents[j].nde_state == LST_NODE_ACTIVE) ||
1469                              (busy    &&  dents[j].nde_state == LST_NODE_BUSY)   ||
1470                              (down    &&  dents[j].nde_state == LST_NODE_DOWN)   ||
1471                              (unknown &&  dents[j].nde_state == LST_NODE_UNKNOWN))) {
1472
1473                                 fprintf(stdout, "\t%s: %s\n",
1474                                         libcfs_id2str(dents[j].nde_id),
1475                                         lst_node_state2str(dents[j].nde_state));
1476                                 c++;
1477                         }
1478                 }
1479
1480                 fprintf(stdout, "Total %d nodes [ %s ]\n", c, argv[i]);
1481
1482                 free(dents);
1483         }
1484
1485         return rc;
1486 }
1487
1488 int
1489 lst_stat_ioctl(char *name, int count, struct lnet_process_id *idsp,
1490                int timeout, struct list_head *resultp)
1491 {
1492         struct lstio_stat_args args = { 0 };
1493
1494         args.lstio_sta_key     = session_key;
1495         args.lstio_sta_timeout = timeout;
1496         args.lstio_sta_nmlen   = strlen(name);
1497         args.lstio_sta_namep   = name;
1498         args.lstio_sta_count   = count;
1499         args.lstio_sta_idsp    = idsp;
1500         args.lstio_sta_resultp = resultp;
1501
1502         return lst_ioctl(LSTIO_STAT_QUERY, &args, sizeof(args));
1503 }
1504
1505 typedef struct {
1506         struct list_head              srp_link;
1507         int                     srp_count;
1508         char                   *srp_name;
1509         struct lnet_process_id      *srp_ids;
1510         struct list_head              srp_result[2];
1511 } lst_stat_req_param_t;
1512
1513 static void
1514 lst_stat_req_param_free(lst_stat_req_param_t *srp)
1515 {
1516         int     i;
1517
1518         for (i = 0; i < 2; i++)
1519                 lst_free_rpcent(&srp->srp_result[i]);
1520
1521         if (srp->srp_ids != NULL)
1522                 free(srp->srp_ids);
1523
1524         free(srp);
1525 }
1526
1527 static int
1528 lst_stat_req_param_alloc(char *name, lst_stat_req_param_t **srpp, int save_old)
1529 {
1530         lst_stat_req_param_t *srp = NULL;
1531         int                   count = save_old ? 2 : 1;
1532         int                   rc;
1533         int                   i;
1534
1535         srp = malloc(sizeof(*srp));
1536         if (srp == NULL)
1537                 return -ENOMEM;
1538
1539         memset(srp, 0, sizeof(*srp));
1540         INIT_LIST_HEAD(&srp->srp_result[0]);
1541         INIT_LIST_HEAD(&srp->srp_result[1]);
1542
1543         rc = lst_get_node_count(LST_OPC_GROUP, name,
1544                                 &srp->srp_count, NULL);
1545         if (rc != 0 && errno == ENOENT) {
1546                 rc = lst_get_node_count(LST_OPC_NODES, name,
1547                                         &srp->srp_count, &srp->srp_ids);
1548         }
1549
1550         if (rc != 0) {
1551                 fprintf(stderr,
1552                         "Failed to get count of nodes from %s: %s\n",
1553                         name, strerror(errno));
1554                 lst_stat_req_param_free(srp);
1555
1556                 return rc;
1557         }
1558
1559         srp->srp_name = name;
1560
1561         for (i = 0; i < count; i++) {
1562                 rc = lst_alloc_rpcent(&srp->srp_result[i], srp->srp_count,
1563                                       sizeof(struct sfw_counters)  +
1564                                       sizeof(struct srpc_counters) +
1565                                       sizeof(struct lnet_counters_common));
1566                 if (rc != 0) {
1567                         fprintf(stderr, "Out of memory\n");
1568                         break;
1569                 }
1570         }
1571
1572         if (rc == 0) {
1573                 *srpp = srp;
1574                 return 0;
1575         }
1576
1577         lst_stat_req_param_free(srp);
1578
1579         return rc;
1580 }
1581
1582 typedef struct {
1583         /* TODO */
1584         int foo;
1585 } lst_srpc_stat_result;
1586
1587 #define LST_LNET_AVG    0
1588 #define LST_LNET_MIN    1
1589 #define LST_LNET_MAX    2
1590
1591 typedef struct {
1592         float           lnet_avg_sndrate;
1593         float           lnet_min_sndrate;
1594         float           lnet_max_sndrate;
1595         float           lnet_total_sndrate;
1596
1597         float           lnet_avg_rcvrate;
1598         float           lnet_min_rcvrate;
1599         float           lnet_max_rcvrate;
1600         float           lnet_total_rcvrate;
1601
1602         float           lnet_avg_sndperf;
1603         float           lnet_min_sndperf;
1604         float           lnet_max_sndperf;
1605         float           lnet_total_sndperf;
1606
1607         float           lnet_avg_rcvperf;
1608         float           lnet_min_rcvperf;
1609         float           lnet_max_rcvperf;
1610         float           lnet_total_rcvperf;
1611
1612         int             lnet_stat_count;
1613 } lst_lnet_stat_result_t;
1614
1615 lst_lnet_stat_result_t lnet_stat_result;
1616
1617 static float
1618 lst_lnet_stat_value(int bw, int send, int off)
1619 {
1620         float  *p;
1621
1622         p = bw ? &lnet_stat_result.lnet_avg_sndperf :
1623                  &lnet_stat_result.lnet_avg_sndrate;
1624
1625         if (!send)
1626                 p += 4;
1627
1628         p += off;
1629
1630         return *p;
1631 }
1632
1633 static void
1634 lst_cal_lnet_stat(float delta, struct lnet_counters_common *lnet_new,
1635                   struct lnet_counters_common *lnet_old, int mbs)
1636 {
1637         float perf;
1638         float rate;
1639         unsigned int unit_divisor;
1640
1641         unit_divisor = (mbs) ? (1000 * 1000) : (1024 * 1024);
1642         perf = (float)(lnet_new->lcc_send_length -
1643                        lnet_old->lcc_send_length) / unit_divisor / delta;
1644         lnet_stat_result.lnet_total_sndperf += perf;
1645
1646         if (lnet_stat_result.lnet_min_sndperf > perf ||
1647             lnet_stat_result.lnet_min_sndperf == 0)
1648                 lnet_stat_result.lnet_min_sndperf = perf;
1649
1650         if (lnet_stat_result.lnet_max_sndperf < perf)
1651                 lnet_stat_result.lnet_max_sndperf = perf;
1652
1653         perf = (float)(lnet_new->lcc_recv_length -
1654                        lnet_old->lcc_recv_length) / unit_divisor / delta;
1655         lnet_stat_result.lnet_total_rcvperf += perf;
1656
1657         if (lnet_stat_result.lnet_min_rcvperf > perf ||
1658             lnet_stat_result.lnet_min_rcvperf == 0)
1659                 lnet_stat_result.lnet_min_rcvperf = perf;
1660
1661         if (lnet_stat_result.lnet_max_rcvperf < perf)
1662                 lnet_stat_result.lnet_max_rcvperf = perf;
1663
1664         rate = (lnet_new->lcc_send_count - lnet_old->lcc_send_count) / delta;
1665         lnet_stat_result.lnet_total_sndrate += rate;
1666
1667         if (lnet_stat_result.lnet_min_sndrate > rate ||
1668             lnet_stat_result.lnet_min_sndrate == 0)
1669                 lnet_stat_result.lnet_min_sndrate = rate;
1670
1671         if (lnet_stat_result.lnet_max_sndrate < rate)
1672                 lnet_stat_result.lnet_max_sndrate = rate;
1673
1674         rate = (lnet_new->lcc_recv_count - lnet_old->lcc_recv_count) / delta;
1675         lnet_stat_result.lnet_total_rcvrate += rate;
1676
1677         if (lnet_stat_result.lnet_min_rcvrate > rate ||
1678             lnet_stat_result.lnet_min_rcvrate == 0)
1679                 lnet_stat_result.lnet_min_rcvrate = rate;
1680
1681         if (lnet_stat_result.lnet_max_rcvrate < rate)
1682                 lnet_stat_result.lnet_max_rcvrate = rate;
1683
1684         lnet_stat_result.lnet_stat_count++;
1685
1686         lnet_stat_result.lnet_avg_sndrate = lnet_stat_result.lnet_total_sndrate /
1687                                             lnet_stat_result.lnet_stat_count;
1688         lnet_stat_result.lnet_avg_rcvrate = lnet_stat_result.lnet_total_rcvrate /
1689                                             lnet_stat_result.lnet_stat_count;
1690
1691         lnet_stat_result.lnet_avg_sndperf = lnet_stat_result.lnet_total_sndperf /
1692                                             lnet_stat_result.lnet_stat_count;
1693         lnet_stat_result.lnet_avg_rcvperf = lnet_stat_result.lnet_total_rcvperf /
1694                                             lnet_stat_result.lnet_stat_count;
1695 }
1696
1697 static void
1698 lst_print_lnet_stat(char *name, int bwrt, int rdwr, int type, int mbs)
1699 {
1700         int     start1 = 0;
1701         int     end1   = 1;
1702         int     start2 = 0;
1703         int     end2   = 1;
1704         int     i;
1705         int     j;
1706         char   *units;
1707
1708         if (lnet_stat_result.lnet_stat_count == 0)
1709                 return;
1710
1711         units = (mbs) ? "MB/s  " : "MiB/s ";
1712
1713         if (bwrt == 1) /* bw only */
1714                 start1 = 1;
1715
1716         if (bwrt == 2) /* rates only */
1717                 end1 = 0;
1718
1719         if (rdwr == 1) /* recv only */
1720                 start2 = 1;
1721
1722         if (rdwr == 2) /* send only */
1723                 end2 = 0;
1724
1725         for (i = start1; i <= end1; i++) {
1726                 fprintf(stdout, "[LNet %s of %s]\n",
1727                         i == 0 ? "Rates" : "Bandwidth", name);
1728
1729                 for (j = start2; j <= end2; j++) {
1730                         fprintf(stdout, "[%c] ", j == 0 ? 'R' : 'W');
1731
1732                         if ((type & 1) != 0) {
1733                                 fprintf(stdout, i == 0 ? "Avg: %-8.0f RPC/s " :
1734                                                          "Avg: %-8.2f %s",
1735                                         lst_lnet_stat_value(i, j, 0), units);
1736                         }
1737
1738                         if ((type & 2) != 0) {
1739                                 fprintf(stdout, i == 0 ? "Min: %-8.0f RPC/s " :
1740                                                          "Min: %-8.2f %s",
1741                                         lst_lnet_stat_value(i, j, 1), units);
1742                         }
1743
1744                         if ((type & 4) != 0) {
1745                                 fprintf(stdout, i == 0 ? "Max: %-8.0f RPC/s" :
1746                                                          "Max: %-8.2f %s",
1747                                         lst_lnet_stat_value(i, j, 2), units);
1748                         }
1749
1750                         fprintf(stdout, "\n");
1751                 }
1752         }
1753 }
1754
1755 static void
1756 lst_print_stat(char *name, struct list_head *resultp,
1757                int idx, int lnet, int bwrt, int rdwr, int type,
1758                int mbs)
1759 {
1760         struct list_head tmp[2];
1761         struct lstcon_rpc_ent *new;
1762         struct lstcon_rpc_ent *old;
1763         struct sfw_counters *sfwk_new;
1764         struct sfw_counters *sfwk_old;
1765         struct srpc_counters *srpc_new;
1766         struct srpc_counters *srpc_old;
1767         struct lnet_counters_common *lnet_new;
1768         struct lnet_counters_common *lnet_old;
1769         float delta;
1770         int errcount = 0;
1771
1772         INIT_LIST_HEAD(&tmp[0]);
1773         INIT_LIST_HEAD(&tmp[1]);
1774
1775         memset(&lnet_stat_result, 0, sizeof(lnet_stat_result));
1776
1777         while (!list_empty(&resultp[idx])) {
1778                 if (list_empty(&resultp[1 - idx])) {
1779                         fprintf(stderr, "Group is changed, re-run stat\n");
1780                         break;
1781                 }
1782
1783                 new = list_entry(resultp[idx].next, struct lstcon_rpc_ent,
1784                                      rpe_link);
1785                 old = list_entry(resultp[1 - idx].next, struct lstcon_rpc_ent,
1786                                      rpe_link);
1787
1788                 /* first time get stats result, can't calculate diff */
1789                 if (new->rpe_peer.nid == LNET_NID_ANY)
1790                         break;
1791
1792                 if (new->rpe_peer.nid != old->rpe_peer.nid ||
1793                     new->rpe_peer.pid != old->rpe_peer.pid) {
1794                         /* Something wrong. i.e, somebody change the group */
1795                         break;
1796                 }
1797
1798                 list_move_tail(&new->rpe_link, &tmp[idx]);
1799
1800                 list_move_tail(&old->rpe_link, &tmp[1 - idx]);
1801
1802                 if (new->rpe_rpc_errno != 0 || new->rpe_fwk_errno != 0 ||
1803                     old->rpe_rpc_errno != 0 || old->rpe_fwk_errno != 0) {
1804                         errcount ++;
1805                         continue;
1806                 }
1807
1808                 sfwk_new = (struct sfw_counters *)&new->rpe_payload[0];
1809                 sfwk_old = (struct sfw_counters *)&old->rpe_payload[0];
1810
1811                 srpc_new = (struct srpc_counters *)((char *)sfwk_new +
1812                                                     sizeof(*sfwk_new));
1813                 srpc_old = (struct srpc_counters *)((char *)sfwk_old +
1814                                                     sizeof(*sfwk_old));
1815
1816                 lnet_new = (struct lnet_counters_common *)((char *)srpc_new +
1817                                                            sizeof(*srpc_new));
1818                 lnet_old = (struct lnet_counters_common *)((char *)srpc_old +
1819                                                            sizeof(*srpc_old));
1820
1821                 /* Prior to version 2.3, the running_ms was a counter for
1822                  * the number of running tests. Since 2.3, running_ms is
1823                  * changed to hold the millisecond since the start of
1824                  * the work item. The rpe_stamp field was formerly used,
1825                  * but is no longer. In 2.12 rpe_stamp was changed to
1826                  * struct timespec64 and has nanosecond resolution, in
1827                  * case it is needed in the future.
1828                  */
1829                 delta = (float)(sfwk_new->running_ms -
1830                                 sfwk_old->running_ms) / 1000;
1831
1832                 if (!lnet) /* TODO */
1833                         continue;
1834
1835                 lst_cal_lnet_stat(delta, lnet_new, lnet_old, mbs);
1836         }
1837
1838         list_splice(&tmp[idx], &resultp[idx]);
1839         list_splice(&tmp[1 - idx], &resultp[1 - idx]);
1840
1841         if (errcount > 0)
1842                 fprintf(stdout, "Failed to stat on %d nodes\n", errcount);
1843
1844         if (!lnet)  /* TODO */
1845                 return;
1846
1847         lst_print_lnet_stat(name, bwrt, rdwr, type, mbs);
1848 }
1849
1850 int
1851 jt_lst_stat(int argc, char **argv)
1852 {
1853         struct list_head        head;
1854         lst_stat_req_param_t *srp;
1855         time_t                last    = 0;
1856         int                   optidx  = 0;
1857         int                   timeout = 5; /* default timeout, 5 sec */
1858         int                   delay   = 5; /* default delay, 5 sec */
1859         int                   count   = -1; /* run forever */
1860         int                   lnet    = 1; /* lnet stat by default */
1861         int                   bwrt    = 0;
1862         int                   rdwr    = 0;
1863         int                   type    = -1;
1864         int                   idx     = 0;
1865         int                   rc;
1866         int                   c;
1867         int                   mbs     = 0; /* report as MB/s */
1868
1869         static const struct option stat_opts[] = {
1870                 { .name = "timeout", .has_arg = required_argument, .val = 't' },
1871                 { .name = "delay",   .has_arg = required_argument, .val = 'd' },
1872                 { .name = "count",   .has_arg = required_argument, .val = 'o' },
1873                 { .name = "lnet",    .has_arg = no_argument,       .val = 'l' },
1874                 { .name = "rpc",     .has_arg = no_argument,       .val = 'c' },
1875                 { .name = "bw",      .has_arg = no_argument,       .val = 'b' },
1876                 { .name = "rate",    .has_arg = no_argument,       .val = 'a' },
1877                 { .name = "read",    .has_arg = no_argument,       .val = 'r' },
1878                 { .name = "write",   .has_arg = no_argument,       .val = 'w' },
1879                 { .name = "avg",     .has_arg = no_argument,       .val = 'g' },
1880                 { .name = "min",     .has_arg = no_argument,       .val = 'n' },
1881                 { .name = "max",     .has_arg = no_argument,       .val = 'x' },
1882                 { .name = "mbs",     .has_arg = no_argument,       .val = 'm' },
1883                 { .name = NULL } };
1884
1885         if (session_key == 0) {
1886                 fprintf(stderr,
1887                         "Can't find env LST_SESSION or value is not valid\n");
1888                 return -1;
1889         }
1890
1891         while (1) {
1892                 c = getopt_long(argc, argv, "t:d:lcbarwgnxm", stat_opts,
1893                                 &optidx);
1894
1895                 if (c == -1)
1896                         break;
1897
1898                 switch (c) {
1899                 case 't':
1900                         timeout = atoi(optarg);
1901                         break;
1902                 case 'd':
1903                         delay = atoi(optarg);
1904                         break;
1905                 case 'o':
1906                         count = atoi(optarg);
1907                         break;
1908                 case 'l':
1909                         lnet = 1;
1910                         break;
1911                 case 'c':
1912                         lnet = 0;
1913                         break;
1914                 case 'b':
1915                         bwrt |= 1;
1916                         break;
1917                 case 'a':
1918                         bwrt |= 2;
1919                         break;
1920                 case 'r':
1921                         rdwr |= 1;
1922                         break;
1923                 case 'w':
1924                         rdwr |= 2;
1925                         break;
1926                 case 'g':
1927                         if (type == -1) {
1928                                 type = 1;
1929                                 break;
1930                         }
1931                         type |= 1;
1932                         break;
1933                 case 'n':
1934                         if (type == -1) {
1935                                 type = 2;
1936                                 break;
1937                         }
1938                         type |= 2;
1939                         break;
1940                 case 'x':
1941                         if (type == -1) {
1942                                 type = 4;
1943                                 break;
1944                         }
1945                         type |= 4;
1946                         break;
1947                 case 'm':
1948                         mbs = 1;
1949                         break;
1950
1951                 default:
1952                         lst_print_usage(argv[0]);
1953                         return -1;
1954                 }
1955         }
1956
1957         if (optind == argc) {
1958                 lst_print_usage(argv[0]);
1959                 return -1;
1960         }
1961
1962         if (timeout <= 0 || delay <= 0) {
1963                 fprintf(stderr, "Invalid timeout or delay value\n");
1964                 return -1;
1965         }
1966
1967         if (count < -1) {
1968             fprintf(stderr, "Invalid count value\n");
1969             return -1;
1970         }
1971
1972         /* extra count to get first data point */
1973         if (count != -1)
1974             count++;
1975
1976         INIT_LIST_HEAD(&head);
1977
1978         while (optind < argc) {
1979                 rc = lst_stat_req_param_alloc(argv[optind++], &srp, 1);
1980                 if (rc != 0)
1981                         goto out;
1982
1983                 list_add_tail(&srp->srp_link, &head);
1984         }
1985
1986         do {
1987                 time_t  now = time(NULL);
1988
1989                 if (now - last < delay) {
1990                         sleep(delay - now + last);
1991                         time(&now);
1992                 }
1993                 last = now;
1994
1995                 list_for_each_entry(srp, &head, srp_link) {
1996                         rc = lst_stat_ioctl(srp->srp_name,
1997                                             srp->srp_count, srp->srp_ids,
1998                                             timeout, &srp->srp_result[idx]);
1999                         if (rc == -1) {
2000                                 lst_print_error("stat", "Failed to stat %s: %s\n",
2001                                                 srp->srp_name, strerror(errno));
2002                                 goto out;
2003                         }
2004
2005                         lst_print_stat(srp->srp_name, srp->srp_result,
2006                                        idx, lnet, bwrt, rdwr, type, mbs);
2007
2008                         lst_reset_rpcent(&srp->srp_result[1 - idx]);
2009                 }
2010
2011                 idx = 1 - idx;
2012
2013                 if (count > 0)
2014                         count--;
2015         } while (count == -1 || count > 0);
2016
2017 out:
2018         while (!list_empty(&head)) {
2019                 srp = list_entry(head.next, lst_stat_req_param_t, srp_link);
2020
2021                 list_del(&srp->srp_link);
2022                 lst_stat_req_param_free(srp);
2023         }
2024
2025         return rc;
2026 }
2027
2028 int
2029 jt_lst_show_error(int argc, char **argv)
2030 {
2031         struct list_head       head;
2032         lst_stat_req_param_t  *srp;
2033         struct lstcon_rpc_ent *ent;
2034         struct sfw_counters   *sfwk;
2035         struct srpc_counters  *srpc;
2036         int                    show_rpc = 1;
2037         int                    optidx = 0;
2038         int                    rc = 0;
2039         int                    ecount;
2040         int                    c;
2041
2042         static const struct option show_error_opts[] = {
2043                 { .name = "session", .has_arg = no_argument, .val = 's' },
2044                 { .name = NULL, } };
2045
2046         if (session_key == 0) {
2047                 fprintf(stderr,
2048                         "Can't find env LST_SESSION or value is not valid\n");
2049                 return -1;
2050         }
2051
2052         while (1) {
2053                 c = getopt_long(argc, argv, "s", show_error_opts, &optidx);
2054
2055                 if (c == -1)
2056                         break;
2057
2058                 switch (c) {
2059                 case 's':
2060                         show_rpc  = 0;
2061                         break;
2062
2063                 default:
2064                         lst_print_usage(argv[0]);
2065                         return -1;
2066                 }
2067         }
2068
2069         if (optind == argc) {
2070                 lst_print_usage(argv[0]);
2071                 return -1;
2072         }
2073
2074         INIT_LIST_HEAD(&head);
2075
2076         while (optind < argc) {
2077                 rc = lst_stat_req_param_alloc(argv[optind++], &srp, 0);
2078                 if (rc != 0)
2079                         goto out;
2080
2081                 list_add_tail(&srp->srp_link, &head);
2082         }
2083
2084         list_for_each_entry(srp, &head, srp_link) {
2085                 rc = lst_stat_ioctl(srp->srp_name, srp->srp_count,
2086                                     srp->srp_ids, 10, &srp->srp_result[0]);
2087
2088                 if (rc == -1) {
2089                         lst_print_error(srp->srp_name, "Failed to show errors of %s: %s\n",
2090                                         srp->srp_name, strerror(errno));
2091                         goto out;
2092                 }
2093
2094                 fprintf(stdout, "%s:\n", srp->srp_name);
2095
2096                 ecount = 0;
2097
2098                 list_for_each_entry(ent, &srp->srp_result[0], rpe_link) {
2099                         if (ent->rpe_rpc_errno != 0) {
2100                                 ecount ++;
2101                                 fprintf(stderr, "RPC failure, can't show error on %s\n",
2102                                         libcfs_id2str(ent->rpe_peer));
2103                                 continue;
2104                         }
2105
2106                         if (ent->rpe_fwk_errno != 0) {
2107                                 ecount ++;
2108                                 fprintf(stderr, "Framework failure, can't show error on %s\n",
2109                                         libcfs_id2str(ent->rpe_peer));
2110                                 continue;
2111                         }
2112
2113                         sfwk = (struct sfw_counters *)&ent->rpe_payload[0];
2114                         srpc = (struct srpc_counters *)((char *)sfwk + sizeof(*sfwk));
2115
2116                         if (srpc->errors == 0 &&
2117                             sfwk->brw_errors == 0 && sfwk->ping_errors == 0)
2118                                 continue;
2119
2120                         if (!show_rpc  &&
2121                             sfwk->brw_errors == 0 && sfwk->ping_errors == 0)
2122                                 continue;
2123
2124                         ecount ++;
2125
2126                         fprintf(stderr, "%s: [Session %d brw errors, %d ping errors]%c",
2127                                 libcfs_id2str(ent->rpe_peer),
2128                                 sfwk->brw_errors, sfwk->ping_errors,
2129                                 show_rpc  ? ' ' : '\n');
2130
2131                         if (!show_rpc)
2132                                 continue;
2133
2134                         fprintf(stderr, "[RPC: %d errors, %d dropped, %d expired]\n",
2135                                 srpc->errors, srpc->rpcs_dropped, srpc->rpcs_expired);
2136                 }
2137
2138                 fprintf(stdout, "Total %d error nodes in %s\n", ecount, srp->srp_name);
2139         }
2140 out:
2141         while (!list_empty(&head)) {
2142                 srp = list_entry(head.next, lst_stat_req_param_t, srp_link);
2143
2144                 list_del(&srp->srp_link);
2145                 lst_stat_req_param_free(srp);
2146         }
2147
2148         return rc;
2149 }
2150
2151 int
2152 lst_add_batch_ioctl(char *name)
2153 {
2154         struct lstio_batch_add_args args = { 0 };
2155
2156         args.lstio_bat_key   = session_key;
2157         args.lstio_bat_nmlen = strlen(name);
2158         args.lstio_bat_namep = name;
2159
2160         return lst_ioctl (LSTIO_BATCH_ADD, &args, sizeof(args));
2161 }
2162
2163 int
2164 jt_lst_add_batch(int argc, char **argv)
2165 {
2166         char   *name;
2167         int     rc;
2168
2169         if (session_key == 0) {
2170                 fprintf(stderr,
2171                         "Can't find env LST_SESSION or value is not valid\n");
2172                 return -1;
2173         }
2174
2175         if (argc != 2) {
2176                 lst_print_usage(argv[0]);
2177                 return -1;
2178         }
2179
2180         name = argv[1];
2181         if (strlen(name) >= LST_NAME_SIZE) {
2182                 fprintf(stderr, "Name length is limited to %d\n",
2183                         LST_NAME_SIZE - 1);
2184                 return -1;
2185         }
2186
2187         rc = lst_add_batch_ioctl(name);
2188         if (rc == 0)
2189                 return 0;
2190
2191         lst_print_error("batch", "Failed to create batch: %s\n",
2192                         strerror(errno));
2193
2194         return -1;
2195 }
2196
2197 int
2198 lst_start_batch_ioctl(char *name, int timeout, struct list_head *resultp)
2199 {
2200         struct lstio_batch_run_args args = { 0 };
2201
2202         args.lstio_bat_key     = session_key;
2203         args.lstio_bat_timeout = timeout;
2204         args.lstio_bat_nmlen   = strlen(name);
2205         args.lstio_bat_namep   = name;
2206         args.lstio_bat_resultp = resultp;
2207
2208         return lst_ioctl(LSTIO_BATCH_START, &args, sizeof(args));
2209 }
2210
2211 int
2212 jt_lst_start_batch(int argc, char **argv)
2213 {
2214         struct list_head  head;
2215         char             *batch;
2216         int               optidx = 0;
2217         int               timeout = 0;
2218         int               count = 0;
2219         int               rc;
2220         int               c;
2221
2222         static const struct option start_batch_opts[] = {
2223                 { .name = "timeout", .has_arg = required_argument, .val = 't' },
2224                 { .name = NULL } };
2225
2226         if (session_key == 0) {
2227                 fprintf(stderr,
2228                         "Can't find env LST_SESSION or value is not valid\n");
2229                 return -1;
2230         }
2231
2232         while (1) {
2233                 c = getopt_long(argc, argv, "t:",
2234                                 start_batch_opts, &optidx);
2235
2236                 /* Detect the end of the options. */
2237                 if (c == -1)
2238                         break;
2239
2240                 switch (c) {
2241                 case 't':
2242                         timeout = atoi(optarg);
2243                         break;
2244                 default:
2245                         lst_print_usage(argv[0]);
2246                         return -1;
2247                 }
2248         }
2249
2250         if (optind == argc) {
2251                 batch = LST_DEFAULT_BATCH;
2252
2253         } else if (optind == argc - 1) {
2254                 batch = argv[optind];
2255
2256         } else {
2257                 lst_print_usage(argv[0]);
2258                 return -1;
2259         }
2260
2261         rc = lst_get_node_count(LST_OPC_BATCHCLI, batch, &count, NULL);
2262         if (rc != 0) {
2263                 fprintf(stderr, "Failed to get count of nodes from %s: %s\n",
2264                         batch, strerror(errno));
2265                 return -1;
2266         }
2267
2268         INIT_LIST_HEAD(&head);
2269
2270         rc = lst_alloc_rpcent(&head, count, 0);
2271         if (rc != 0) {
2272                 fprintf(stderr, "Out of memory\n");
2273                 return -1;
2274         }
2275
2276         rc = lst_start_batch_ioctl(batch, timeout, &head);
2277
2278         if (rc == 0) {
2279                 fprintf(stdout, "%s is running now\n", batch);
2280                 lst_free_rpcent(&head);
2281                 return 0;
2282         }
2283
2284         if (rc == -1) {
2285                 lst_print_error("batch", "Failed to start batch: %s\n",
2286                                 strerror(errno));
2287                 lst_free_rpcent(&head);
2288                 return rc;
2289         }
2290
2291         lst_print_transerr(&head, "Run batch");
2292
2293         lst_free_rpcent(&head);
2294
2295         return rc;
2296 }
2297
2298 int
2299 lst_stop_batch_ioctl(char *name, int force, struct list_head *resultp)
2300 {
2301         struct lstio_batch_stop_args args = { 0 };
2302
2303         args.lstio_bat_key     = session_key;
2304         args.lstio_bat_force   = force;
2305         args.lstio_bat_nmlen   = strlen(name);
2306         args.lstio_bat_namep   = name;
2307         args.lstio_bat_resultp = resultp;
2308
2309         return lst_ioctl(LSTIO_BATCH_STOP, &args, sizeof(args));
2310 }
2311
2312 int
2313 jt_lst_stop_batch(int argc, char **argv)
2314 {
2315         struct list_head  head;
2316         char             *batch;
2317         int               force = 0;
2318         int               optidx;
2319         int               count;
2320         int               rc;
2321         int               c;
2322
2323         static const struct option stop_batch_opts[] = {
2324                 { .name = "force", .has_arg = no_argument, .val = 'f' },
2325                 { .name = NULL } };
2326
2327         if (session_key == 0) {
2328                 fprintf(stderr,
2329                         "Can't find env LST_SESSION or value is not valid\n");
2330                 return -1;
2331         }
2332
2333         while (1) {
2334                 c = getopt_long(argc, argv, "f",
2335                                 stop_batch_opts, &optidx);
2336
2337                 /* Detect the end of the options. */
2338                 if (c == -1)
2339                         break;
2340
2341                 switch (c) {
2342                 case 'f':
2343                         force = 1;
2344                         break;
2345                 default:
2346                         lst_print_usage(argv[0]);
2347                         return -1;
2348                 }
2349         }
2350
2351         if (optind == argc) {
2352                 batch = LST_DEFAULT_BATCH;
2353
2354         } else if (optind == argc - 1) {
2355                 batch = argv[optind];
2356
2357         } else {
2358                 lst_print_usage(argv[0]);
2359                 return -1;
2360         }
2361
2362         rc = lst_get_node_count(LST_OPC_BATCHCLI, batch, &count, NULL);
2363         if (rc != 0) {
2364                 fprintf(stderr, "Failed to get count of nodes from %s: %s\n",
2365                         batch, strerror(errno));
2366                 return -1;
2367         }
2368
2369         INIT_LIST_HEAD(&head);
2370
2371         rc = lst_alloc_rpcent(&head, count, 0);
2372         if (rc != 0) {
2373                 fprintf(stderr, "Out of memory\n");
2374                 return -1;
2375         }
2376
2377         rc = lst_stop_batch_ioctl(batch, force, &head);
2378         if (rc != 0)
2379                 goto out;
2380
2381         while (1) {
2382                 lst_reset_rpcent(&head);
2383
2384                 rc = lst_query_batch_ioctl(batch, 0, 0, 30, &head);
2385                 if (rc != 0)
2386                         goto out;
2387
2388                 if (lstcon_tsbqry_stat_run(&trans_stat, 0)  == 0 &&
2389                     lstcon_tsbqry_stat_failure(&trans_stat, 0) == 0)
2390                         break;
2391
2392                 fprintf(stdout, "%d batch in stopping\n",
2393                         lstcon_tsbqry_stat_run(&trans_stat, 0));
2394                 sleep(1);
2395         }
2396
2397         fprintf(stdout, "Batch is stopped\n");
2398         lst_free_rpcent(&head);
2399
2400         return 0;
2401 out:
2402         if (rc == -1) {
2403                 lst_print_error("batch", "Failed to stop batch: %s\n",
2404                                 strerror(errno));
2405                 lst_free_rpcent(&head);
2406                 return -1;
2407         }
2408
2409         lst_print_transerr(&head, "stop batch");
2410
2411         lst_free_rpcent(&head);
2412
2413         return rc;
2414 }
2415
2416 int
2417 lst_list_batch_ioctl(int len, char *name, int index)
2418 {
2419         struct lstio_batch_list_args args = { 0 };
2420
2421         args.lstio_bat_key   = session_key;
2422         args.lstio_bat_idx   = index;
2423         args.lstio_bat_nmlen = len;
2424         args.lstio_bat_namep = name;
2425
2426         return lst_ioctl(LSTIO_BATCH_LIST, &args, sizeof(args));
2427 }
2428
2429 int
2430 lst_info_batch_ioctl(char *batch, int test, int server,
2431                      struct lstcon_test_batch_ent *entp, int *idxp,
2432                      int *ndentp, struct lstcon_node_ent *dentsp)
2433 {
2434         struct lstio_batch_info_args args = { 0 };
2435
2436         args.lstio_bat_key     = session_key;
2437         args.lstio_bat_nmlen   = strlen(batch);
2438         args.lstio_bat_namep   = batch;
2439         args.lstio_bat_server  = server;
2440         args.lstio_bat_testidx = test;
2441         args.lstio_bat_entp    = entp;
2442         args.lstio_bat_idxp    = idxp;
2443         args.lstio_bat_ndentp  = ndentp;
2444         args.lstio_bat_dentsp  = dentsp;
2445
2446         return lst_ioctl(LSTIO_BATCH_INFO, &args, sizeof(args));
2447 }
2448
2449 int
2450 lst_list_batch_all(void)
2451 {
2452         char name[LST_NAME_SIZE];
2453         int  rc;
2454         int  i;
2455
2456         for (i = 0; ; i++) {
2457                 rc = lst_list_batch_ioctl(LST_NAME_SIZE, name, i);
2458                 if (rc == 0) {
2459                         fprintf(stdout, "%d) %s\n", i + 1, name);
2460                         continue;
2461                 }
2462
2463                 if (errno == ENOENT)
2464                         break;
2465
2466                 lst_print_error("batch", "Failed to list batch: %s\n",
2467                                 strerror(errno));
2468                 return rc;
2469         }
2470
2471         fprintf(stdout, "Total %d batches\n", i);
2472
2473         return 0;
2474 }
2475
2476 int
2477 lst_list_tsb_nodes(char *batch, int test, int server,
2478                    int count, int active, int invalid)
2479 {
2480         struct lstcon_node_ent *dents;
2481         int                index = 0;
2482         int                rc;
2483         int                c;
2484         int                i;
2485
2486         if (count == 0)
2487                 return 0;
2488
2489         /* verbose list, show nodes in batch or test */
2490         dents = malloc(count * sizeof(struct lstcon_node_ent));
2491         if (dents == NULL) {
2492                 fprintf(stdout, "Can't allocate memory\n");
2493                 return -1;
2494         }
2495
2496         rc = lst_info_batch_ioctl(batch, test, server,
2497                                   NULL, &index, &count, dents);
2498         if (rc != 0) {
2499                 free(dents);
2500                 lst_print_error((test > 0) ? "test" : "batch",
2501                                 (test > 0) ? "Failed to query test: %s\n" :
2502                                              "Failed to query batch: %s\n",
2503                                 strerror(errno));
2504                 return -1;
2505         }
2506
2507         for (i = 0, c = 0; i < count; i++) {
2508                 if ((!active  && dents[i].nde_state == LST_NODE_ACTIVE) ||
2509                     (!invalid && (dents[i].nde_state == LST_NODE_BUSY  ||
2510                                   dents[i].nde_state == LST_NODE_DOWN  ||
2511                                   dents[i].nde_state == LST_NODE_UNKNOWN)))
2512                         continue;
2513
2514                 fprintf(stdout, "\t%s: %s\n",
2515                         libcfs_id2str(dents[i].nde_id),
2516                         lst_node_state2str(dents[i].nde_state));
2517                 c++;
2518         }
2519
2520         fprintf(stdout, "Total %d nodes\n", c);
2521         free(dents);
2522
2523         return 0;
2524 }
2525
2526 int
2527 jt_lst_list_batch(int argc, char **argv)
2528 {
2529         struct lstcon_test_batch_ent ent;
2530         char *batch   = NULL;
2531         int   optidx  = 0;
2532         int   verbose = 0; /* list nodes in batch or test */
2533         int   invalid = 0;
2534         int   active  = 0;
2535         int   server  = 0;
2536         int   ntest   = 0;
2537         int   test    = 0;
2538         int   c       = 0;
2539         int   rc;
2540
2541         static const struct option list_batch_opts[] = {
2542                 { .name = "test",    .has_arg = required_argument, .val = 't' },
2543                 { .name = "invalid", .has_arg = no_argument,       .val = 'i' },
2544                 { .name = "active",  .has_arg = no_argument,       .val = 'a' },
2545                 { .name = "all",     .has_arg = no_argument,       .val = 'l' },
2546                 { .name = "server",  .has_arg = no_argument,       .val = 's' },
2547                 { .name = NULL, } };
2548
2549         if (session_key == 0) {
2550                 fprintf(stderr,
2551                         "Can't find env LST_SESSION or value is not valid\n");
2552                 return -1;
2553         }
2554
2555         while (1) {
2556                 c = getopt_long(argc, argv, "ailst:",
2557                                 list_batch_opts, &optidx);
2558
2559                 if (c == -1)
2560                         break;
2561
2562                 switch (c) {
2563                 case 'a':
2564                         verbose = active = 1;
2565                         break;
2566                 case 'i':
2567                         verbose = invalid = 1;
2568                         break;
2569                 case 'l':
2570                         verbose = active = invalid = 1;
2571                         break;
2572                 case 's':
2573                         server = 1;
2574                         break;
2575                 case 't':
2576                         test = atoi(optarg);
2577                         ntest = 1;
2578                         break;
2579                 default:
2580                         lst_print_usage(argv[0]);
2581                         return -1;
2582                 }
2583         }
2584
2585         if (optind == argc) {
2586                 /* list all batches */
2587                 rc = lst_list_batch_all();
2588                 return rc;
2589         }
2590
2591         if (ntest == 1 && test <= 0) {
2592                 fprintf(stderr, "Invalid test id, test id starts from 1\n");
2593                 return -1;
2594         }
2595
2596         if (optind != argc - 1) {
2597                 lst_print_usage(argv[0]);
2598                 return -1;
2599         }
2600
2601         batch = argv[optind];
2602
2603 loop:
2604         /* show detail of specified batch or test */
2605         rc = lst_info_batch_ioctl(batch, test, server,
2606                                   &ent, NULL, NULL, NULL);
2607         if (rc != 0) {
2608                 lst_print_error((test > 0) ? "test" : "batch",
2609                                 (test > 0) ? "Failed to query test: %s\n" :
2610                                              "Failed to query batch: %s\n",
2611                                 strerror(errno));
2612                 return -1;
2613         }
2614
2615         if (verbose) {
2616                 /* list nodes in test or batch */
2617                 rc = lst_list_tsb_nodes(batch, test, server,
2618                                         server ? ent.tbe_srv_nle.nle_nnode :
2619                                                  ent.tbe_cli_nle.nle_nnode,
2620                                         active, invalid);
2621                 return rc;
2622         }
2623
2624         /* only show number of hosts in batch or test */
2625         if (test == 0) {
2626                 fprintf(stdout, "Batch: %s Tests: %d State: %d\n",
2627                         batch, ent.u.tbe_batch.bae_ntest,
2628                         ent.u.tbe_batch.bae_state);
2629                 ntest = ent.u.tbe_batch.bae_ntest;
2630                 test = 1; /* starting from test 1 */
2631
2632         } else {
2633                 fprintf(stdout,
2634                         "\tTest %d(%s) (loop: %d, concurrency: %d)\n",
2635                         test, lst_test_type2name(ent.u.tbe_test.tse_type),
2636                         ent.u.tbe_test.tse_loop,
2637                         ent.u.tbe_test.tse_concur);
2638                 ntest --;
2639                 test ++;
2640         }
2641
2642         fprintf(stdout, LST_NODES_TITLE);
2643         fprintf(stdout, "client\t%d\t%d\t%d\t%d\t%d\n"
2644                         "server\t%d\t%d\t%d\t%d\t%d\n",
2645                 ent.tbe_cli_nle.nle_nactive,
2646                 ent.tbe_cli_nle.nle_nbusy,
2647                 ent.tbe_cli_nle.nle_ndown,
2648                 ent.tbe_cli_nle.nle_nunknown,
2649                 ent.tbe_cli_nle.nle_nnode,
2650                 ent.tbe_srv_nle.nle_nactive,
2651                 ent.tbe_srv_nle.nle_nbusy,
2652                 ent.tbe_srv_nle.nle_ndown,
2653                 ent.tbe_srv_nle.nle_nunknown,
2654                 ent.tbe_srv_nle.nle_nnode);
2655
2656         if (ntest != 0)
2657                 goto loop;
2658
2659         return 0;
2660 }
2661
2662 int
2663 lst_query_batch_ioctl(char *batch, int test, int server,
2664                       int timeout, struct list_head *head)
2665 {
2666         struct lstio_batch_query_args args = { 0 };
2667
2668         args.lstio_bat_key     = session_key;
2669         args.lstio_bat_testidx = test;
2670         args.lstio_bat_client  = !(server);
2671         args.lstio_bat_timeout = timeout;
2672         args.lstio_bat_nmlen   = strlen(batch);
2673         args.lstio_bat_namep   = batch;
2674         args.lstio_bat_resultp = head;
2675
2676         return lst_ioctl(LSTIO_BATCH_QUERY, &args, sizeof(args));
2677 }
2678
2679 void
2680 lst_print_tsb_verbose(struct list_head *head,
2681                       int active, int idle, int error)
2682 {
2683         struct lstcon_rpc_ent *ent;
2684
2685         list_for_each_entry(ent, head, rpe_link) {
2686                 if (ent->rpe_priv[0] == 0 && active)
2687                         continue;
2688
2689                 if (ent->rpe_priv[0] != 0 && idle)
2690                         continue;
2691
2692                 if (ent->rpe_fwk_errno == 0 && error)
2693                         continue;
2694
2695                 fprintf(stdout, "%s [%s]: %s\n",
2696                         libcfs_id2str(ent->rpe_peer),
2697                         lst_node_state2str(ent->rpe_state),
2698                         ent->rpe_rpc_errno != 0 ?
2699                                 strerror(ent->rpe_rpc_errno) :
2700                                 (ent->rpe_priv[0] > 0 ? "Running" : "Idle"));
2701         }
2702 }
2703
2704 int
2705 jt_lst_query_batch(int argc, char **argv)
2706 {
2707         struct lstcon_test_batch_ent ent;
2708         struct list_head head;
2709         char   *batch   = NULL;
2710         time_t  last    = 0;
2711         int     optidx  = 0;
2712         int     verbose = 0;
2713         int     server  = 0;
2714         int     timeout = 5; /* default 5 seconds */
2715         int     delay   = 5; /* default 5 seconds */
2716         int     loop    = 1; /* default 1 loop */
2717         int     active  = 0;
2718         int     error   = 0;
2719         int     idle    = 0;
2720         int     count   = 0;
2721         int     test    = 0;
2722         int     rc      = 0;
2723         int     c       = 0;
2724         int     i;
2725
2726         static const struct option query_batch_opts[] = {
2727                 { .name = "timeout", .has_arg = required_argument, .val = 'o' },
2728                 { .name = "delay",   .has_arg = required_argument, .val = 'd' },
2729                 { .name = "loop",    .has_arg = required_argument, .val = 'c' },
2730                 { .name = "test",    .has_arg = required_argument, .val = 't' },
2731                 { .name = "server",  .has_arg = no_argument,       .val = 's' },
2732                 { .name = "active",  .has_arg = no_argument,       .val = 'a' },
2733                 { .name = "idle",    .has_arg = no_argument,       .val = 'i' },
2734                 { .name = "error",   .has_arg = no_argument,       .val = 'e' },
2735                 { .name = "all",     .has_arg = no_argument,       .val = 'l' },
2736                 { .name = NULL, } };
2737
2738         if (session_key == 0) {
2739                 fprintf(stderr,
2740                         "Can't find env LST_SESSION or value is not valid\n");
2741                 return -1;
2742         }
2743
2744         while (1) {
2745                 c = getopt_long(argc, argv, "o:d:c:t:saiel",
2746                                 query_batch_opts, &optidx);
2747
2748                 /* Detect the end of the options. */
2749                 if (c == -1)
2750                         break;
2751
2752                 switch (c) {
2753                 case 'o':
2754                         timeout = atoi(optarg);
2755                         break;
2756                 case 'd':
2757                         delay = atoi(optarg);
2758                         break;
2759                 case 'c':
2760                         loop = atoi(optarg);
2761                         break;
2762                 case 't':
2763                         test = atoi(optarg);
2764                         break;
2765                 case 's':
2766                         server = 1;
2767                         break;
2768                 case 'a':
2769                         active = verbose = 1;
2770                         break;
2771                 case 'i':
2772                         idle = verbose = 1;
2773                         break;
2774                 case 'e':
2775                         error = verbose = 1;
2776                         break;
2777                 case 'l':
2778                         verbose = 1;
2779                         break;
2780                 default:
2781                         lst_print_usage(argv[0]);
2782                         return -1;
2783                 }
2784         }
2785
2786         if (test < 0 || timeout <= 0 || delay <= 0 || loop <= 0) {
2787                 lst_print_usage(argv[0]);
2788                 return -1;
2789         }
2790
2791         if (optind == argc) {
2792                 batch = LST_DEFAULT_BATCH;
2793
2794         } else if (optind == argc - 1) {
2795                 batch = argv[optind];
2796
2797         } else {
2798                 lst_print_usage(argv[0]);
2799                 return -1;
2800         }
2801
2802
2803         INIT_LIST_HEAD(&head);
2804
2805         if (verbose) {
2806                 rc = lst_info_batch_ioctl(batch, test, server,
2807                                           &ent, NULL, NULL, NULL);
2808                 if (rc != 0) {
2809                         fprintf(stderr, "Failed to query %s [%d]: %s\n",
2810                                 batch, test, strerror(errno));
2811                         return -1;
2812                 }
2813
2814                 count = server ? ent.tbe_srv_nle.nle_nnode :
2815                                  ent.tbe_cli_nle.nle_nnode;
2816                 if (count == 0) {
2817                         fprintf(stdout, "Batch or test is empty\n");
2818                         return 0;
2819                 }
2820         }
2821
2822         rc = lst_alloc_rpcent(&head, count, 0);
2823         if (rc != 0) {
2824                 fprintf(stderr, "Out of memory\n");
2825                 return rc;
2826         }
2827
2828         for (i = 0; i < loop; i++) {
2829                 time_t  now = time(NULL);
2830
2831                 if (now - last < delay) {
2832                         sleep(delay - now + last);
2833                         time(&now);
2834                 }
2835
2836                 last = now;
2837
2838                 rc = lst_query_batch_ioctl(batch, test,
2839                                            server, timeout, &head);
2840                 if (rc == -1) {
2841                         fprintf(stderr, "Failed to query batch: %s\n",
2842                                 strerror(errno));
2843                         break;
2844                 }
2845
2846                 if (verbose) {
2847                         /* Verbose mode */
2848                         lst_print_tsb_verbose(&head, active, idle, error);
2849                         continue;
2850                 }
2851
2852                 fprintf(stdout, "%s [%d] ", batch, test);
2853
2854                 if (lstcon_rpc_stat_failure(&trans_stat, 0) != 0) {
2855                         fprintf(stdout, "%d of %d nodes are unknown, ",
2856                                 lstcon_rpc_stat_failure(&trans_stat, 0),
2857                                 lstcon_rpc_stat_total(&trans_stat, 0));
2858                 }
2859
2860                 if (lstcon_rpc_stat_failure(&trans_stat, 0) == 0 &&
2861                     lstcon_tsbqry_stat_run(&trans_stat, 0)  == 0  &&
2862                     lstcon_tsbqry_stat_failure(&trans_stat, 0) == 0) {
2863                         fprintf(stdout, "is stopped\n");
2864                         continue;
2865                 }
2866
2867                 if (lstcon_rpc_stat_failure(&trans_stat, 0) == 0 &&
2868                     lstcon_tsbqry_stat_idle(&trans_stat, 0) == 0 &&
2869                     lstcon_tsbqry_stat_failure(&trans_stat, 0) == 0) {
2870                         fprintf(stdout, "is running\n");
2871                         continue;
2872                 }
2873
2874                 fprintf(stdout, "stopped: %d , running: %d, failed: %d\n",
2875                                 lstcon_tsbqry_stat_idle(&trans_stat, 0),
2876                                 lstcon_tsbqry_stat_run(&trans_stat, 0),
2877                                 lstcon_tsbqry_stat_failure(&trans_stat, 0));
2878         }
2879
2880         lst_free_rpcent(&head);
2881
2882         return rc;
2883 }
2884
2885 int
2886 lst_parse_distribute(char *dstr, int *dist, int *span)
2887 {
2888         *dist = atoi(dstr);
2889         if (*dist <= 0)
2890                 return -1;
2891
2892         dstr = strchr(dstr, ':');
2893         if (dstr == NULL)
2894                 return -1;
2895
2896         *span = atoi(dstr + 1);
2897         if (*span <= 0)
2898                 return -1;
2899
2900         return 0;
2901 }
2902
2903 int
2904 lst_get_bulk_param(int argc, char **argv, struct lst_test_bulk_param *bulk)
2905 {
2906         char   *tok = NULL;
2907         char   *end = NULL;
2908         int     rc  = 0;
2909         int     i   = 0;
2910
2911         bulk->blk_size  = 4096;
2912         bulk->blk_opc   = LST_BRW_READ;
2913         bulk->blk_flags = LST_BRW_CHECK_NONE;
2914         bulk->blk_srv_off = bulk->blk_cli_off = 0;
2915
2916         while (i < argc) {
2917                 if (strcasestr(argv[i], "check=") == argv[i] ||
2918                     strcasestr(argv[i], "c=") == argv[i]) {
2919                         tok = strchr(argv[i], '=') + 1;
2920
2921                         if (strcasecmp(tok, "full") == 0) {
2922                                 bulk->blk_flags = LST_BRW_CHECK_FULL;
2923                         } else if (strcasecmp(tok, "simple") == 0) {
2924                                 bulk->blk_flags = LST_BRW_CHECK_SIMPLE;
2925                         } else {
2926                                 fprintf(stderr, "Unknow flag %s\n", tok);
2927                                 return -1;
2928                         }
2929
2930                 } else if (strcasestr(argv[i], "size=") == argv[i] ||
2931                            strcasestr(argv[i], "s=") == argv[i]) {
2932                         tok = strchr(argv[i], '=') + 1;
2933
2934                         bulk->blk_size = strtol(tok, &end, 0);
2935                         if (bulk->blk_size <= 0) {
2936                                 fprintf(stderr, "Invalid size %s\n", tok);
2937                                 return -1;
2938                         }
2939
2940                         if (end == NULL)
2941                                 return 0;
2942
2943                         if (*end == 'k' || *end == 'K')
2944                                 bulk->blk_size *= 1024;
2945                         else if (*end == 'm' || *end == 'M')
2946                                 bulk->blk_size *= 1024 * 1024;
2947
2948                         if (bulk->blk_size > LNET_MTU) {
2949                                 fprintf(stderr, "Size exceed limitation: %d bytes\n",
2950                                         bulk->blk_size);
2951                                 return -1;
2952                         }
2953
2954                 } else if (strcasestr(argv[i], "off=") == argv[i]) {
2955                         int     off;
2956
2957                         tok = strchr(argv[i], '=') + 1;
2958
2959                         off = strtol(tok, &end, 0);
2960                         /* NB: align with sizeof(__u64) to simplify page
2961                          * checking implementation */
2962                         if (off < 0 || off % sizeof(__u64) != 0) {
2963                                 fprintf(stderr,
2964                                         "Invalid offset %s/%d, it should be "
2965                                         "postive value and multiple of %d\n",
2966                                         tok, off, (int)sizeof(__u64));
2967                                 return -1;
2968                         }
2969
2970                         /* NB: blk_srv_off is reserved so far */
2971                         bulk->blk_cli_off = bulk->blk_srv_off = off;
2972                         if (end == NULL)
2973                                 return 0;
2974
2975                 } else if (strcasecmp(argv[i], "read") == 0 ||
2976                            strcasecmp(argv[i], "r") == 0) {
2977                         bulk->blk_opc = LST_BRW_READ;
2978
2979                 } else if (strcasecmp(argv[i], "write") == 0 ||
2980                            strcasecmp(argv[i], "w") == 0) {
2981                         bulk->blk_opc = LST_BRW_WRITE;
2982
2983                 } else {
2984                         fprintf(stderr, "Unknow parameter: %s\n", argv[i]);
2985                         return -1;
2986                 }
2987
2988                 i++;
2989         }
2990
2991         return rc;
2992 }
2993
2994 int
2995 lst_get_test_param(char *test, int argc, char **argv, void **param, int *plen)
2996 {
2997         struct lst_test_bulk_param *bulk = NULL;
2998         int                    type;
2999
3000         type = lst_test_name2type(test);
3001         if (type < 0) {
3002                 fprintf(stderr, "Unknow test name %s\n", test);
3003                 return -1;
3004         }
3005
3006         switch (type) {
3007         case LST_TEST_PING:
3008                 break;
3009
3010         case LST_TEST_BULK:
3011                 bulk = malloc(sizeof(*bulk));
3012                 if (bulk == NULL) {
3013                         fprintf(stderr, "Out of memory\n");
3014                         return -1;
3015                 }
3016
3017                 memset(bulk, 0, sizeof(*bulk));
3018
3019                 if (lst_get_bulk_param(argc, argv, bulk) != 0) {
3020                         free(bulk);
3021                         return -1;
3022                 }
3023
3024                 *param = bulk;
3025                 *plen  = sizeof(*bulk);
3026
3027                 break;
3028
3029         default:
3030                 break;
3031         }
3032
3033         /* TODO: parse more parameter */
3034         return type;
3035 }
3036
3037 int
3038 lst_add_test_ioctl(char *batch, int type, int loop, int concur,
3039                    int dist, int span, char *sgrp, char *dgrp,
3040                    void *param, int plen, int *retp, struct list_head *resultp)
3041 {
3042         struct lstio_test_args args = { 0 };
3043
3044         args.lstio_tes_key        = session_key;
3045         args.lstio_tes_bat_nmlen  = strlen(batch);
3046         args.lstio_tes_bat_name   = batch;
3047         args.lstio_tes_type       = type;
3048         args.lstio_tes_oneside    = 0;
3049         args.lstio_tes_loop       = loop;
3050         args.lstio_tes_concur     = concur;
3051         args.lstio_tes_dist       = dist;
3052         args.lstio_tes_span       = span;
3053         args.lstio_tes_sgrp_nmlen = strlen(sgrp);
3054         args.lstio_tes_sgrp_name  = sgrp;
3055         args.lstio_tes_dgrp_nmlen = strlen(dgrp);
3056         args.lstio_tes_dgrp_name  = dgrp;
3057         args.lstio_tes_param_len  = plen;
3058         args.lstio_tes_param      = param;
3059         args.lstio_tes_retp       = retp;
3060         args.lstio_tes_resultp    = resultp;
3061
3062         return lst_ioctl(LSTIO_TEST_ADD, &args, sizeof(args));
3063 }
3064
3065 int
3066 jt_lst_add_test(int argc, char **argv)
3067 {
3068         struct list_head head;
3069         char *batch  = NULL;
3070         char *test   = NULL;
3071         char *dstr   = NULL;
3072         char *from   = NULL;
3073         char *to     = NULL;
3074         void *param  = NULL;
3075         int   optidx = 0;
3076         int   concur = 1;
3077         int   loop   = -1;
3078         int   dist   = 1;
3079         int   span   = 1;
3080         int   plen   = 0;
3081         int   fcount = 0;
3082         int   tcount = 0;
3083         int   ret    = 0;
3084         int   type;
3085         int   rc;
3086         int   c;
3087
3088         static const struct option add_test_opts[] = {
3089         { .name = "batch",       .has_arg = required_argument, .val = 'b' },
3090         { .name = "concurrency", .has_arg = required_argument, .val = 'c' },
3091         { .name = "distribute",  .has_arg = required_argument, .val = 'd' },
3092         { .name = "from",        .has_arg = required_argument, .val = 'f' },
3093         { .name = "to",          .has_arg = required_argument, .val = 't' },
3094         { .name = "loop",        .has_arg = required_argument, .val = 'l' },
3095         { .name = NULL } };
3096
3097         if (session_key == 0) {
3098                 fprintf(stderr,
3099                         "Can't find env LST_SESSION or value is not valid\n");
3100                 return -1;
3101         }
3102
3103         while (1) {
3104                 c = getopt_long(argc, argv, "b:c:d:f:l:t:",
3105                                 add_test_opts, &optidx);
3106
3107                 /* Detect the end of the options. */
3108                 if (c == -1)
3109                         break;
3110
3111                 switch (c) {
3112                 case 'b':
3113                         batch = optarg;
3114                         break;
3115                 case 'c':
3116                         concur = atoi(optarg);
3117                         break;
3118                 case 'd':
3119                         dstr = optarg;
3120                         break;
3121                 case 'f':
3122                         from = optarg;
3123                         break;
3124                 case 'l':
3125                         loop = atoi(optarg);
3126                         break;
3127                 case 't':
3128                         to = optarg;
3129                         break;
3130                 default:
3131                         lst_print_usage(argv[0]);
3132                         return -1;
3133                 }
3134         }
3135
3136         if (optind == argc || from == NULL || to == NULL) {
3137                 lst_print_usage(argv[0]);
3138                 return -1;
3139         }
3140
3141         if (concur <= 0 || concur > LST_MAX_CONCUR) {
3142                 fprintf(stderr, "Invalid concurrency of test: %d\n", concur);
3143                 return -1;
3144         }
3145
3146         if (batch == NULL)
3147                 batch = LST_DEFAULT_BATCH;
3148
3149         if (dstr != NULL) {
3150                 rc = lst_parse_distribute(dstr, &dist, &span);
3151                 if (rc != 0) {
3152                         fprintf(stderr, "Invalid distribution: %s\n", dstr);
3153                         return -1;
3154                 }
3155         }
3156
3157         test = argv[optind++];
3158
3159         argc -= optind;
3160         argv += optind;
3161
3162         type = lst_get_test_param(test, argc, argv, &param, &plen);
3163         if (type < 0) {
3164                 fprintf(stderr, "Failed to add test (%s)\n", test);
3165                 return -1;
3166         }
3167
3168         INIT_LIST_HEAD(&head);
3169
3170         rc = lst_get_node_count(LST_OPC_GROUP, from, &fcount, NULL);
3171         if (rc != 0) {
3172                 fprintf(stderr, "Can't get count of nodes from %s: %s\n",
3173                         from, strerror(errno));
3174                 goto out;
3175         }
3176
3177         rc = lst_get_node_count(LST_OPC_GROUP, to, &tcount, NULL);
3178         if (rc != 0) {
3179                 fprintf(stderr, "Can't get count of nodes from %s: %s\n",
3180                         to, strerror(errno));
3181                 goto out;
3182         }
3183
3184         rc = lst_alloc_rpcent(&head, fcount > tcount ? fcount : tcount, 0);
3185         if (rc != 0) {
3186                 fprintf(stderr, "Out of memory\n");
3187                 goto out;
3188         }
3189
3190         rc = lst_add_test_ioctl(batch, type, loop, concur,
3191                                 dist, span, from, to, param, plen, &ret, &head);
3192
3193         if (rc == 0) {
3194                 fprintf(stdout, "Test was added successfully\n");
3195                 if (ret != 0) {
3196                         fprintf(stdout, "Server group contains userland test "
3197                                 "nodes, old version of tcplnd can't accept "
3198                                 "connection request\n");
3199                 }
3200
3201                 goto out;
3202         }
3203
3204         if (rc == -1) {
3205                 lst_print_error("test", "Failed to add test: %s\n",
3206                                 strerror(errno));
3207                 goto out;
3208         }
3209
3210         lst_print_transerr(&head, "add test");
3211 out:
3212         lst_free_rpcent(&head);
3213
3214         if (param != NULL)
3215                 free(param);
3216
3217         return rc;
3218 }
3219
3220 static command_t lst_cmdlist[] = {
3221         {"new_session",         jt_lst_new_session,     NULL,
3222          "Usage: lst new_session [--timeout TIME] [--force] [NAME]"                     },
3223         {"end_session",         jt_lst_end_session,     NULL,
3224          "Usage: lst end_session"                                                       },
3225         {"show_session",        jt_lst_show_session,    NULL,
3226          "Usage: lst show_session"                                                      },
3227         {"ping",                jt_lst_ping ,           NULL,
3228          "Usage: lst ping  [--group NAME] [--batch NAME] [--session] [--nodes IDS]"     },
3229         {"add_group",           jt_lst_add_group,       NULL,
3230          "Usage: lst group NAME IDs [IDs]..."                                           },
3231         {"del_group",           jt_lst_del_group,       NULL,
3232          "Usage: lst del_group NAME"                                                    },
3233         {"update_group",        jt_lst_update_group,    NULL,
3234          "Usage: lst update_group NAME [--clean] [--refresh] [--remove IDs]"            },
3235         {"list_group",          jt_lst_list_group,      NULL,
3236           "Usage: lst list_group [--active] [--busy] [--down] [--unknown] GROUP ..."    },
3237         {"stat",                jt_lst_stat,            NULL,
3238          "Usage: lst stat [--bw] [--rate] [--read] [--write] [--max] [--min] [--avg] "
3239          " [--mbs] [--timeout #] [--delay #] [--count #] GROUP [GROUP]"                 },
3240         {"show_error",          jt_lst_show_error,      NULL,
3241          "Usage: lst show_error NAME | IDS ..."                                         },
3242         {"add_batch",           jt_lst_add_batch,       NULL,
3243          "Usage: lst add_batch NAME"                                                    },
3244         {"run",                 jt_lst_start_batch,     NULL,
3245          "Usage: lst run [--timeout TIME] [NAME]"                                       },
3246         {"stop",                jt_lst_stop_batch,      NULL,
3247          "Usage: lst stop [--force] BATCH_NAME"                                         },
3248         {"list_batch",          jt_lst_list_batch,      NULL,
3249          "Usage: lst list_batch NAME [--test ID] [--server]"                            },
3250         {"query",               jt_lst_query_batch,     NULL,
3251          "Usage: lst query [--test ID] [--server] [--timeout TIME] NAME"                },
3252         {"add_test",            jt_lst_add_test,        NULL,
3253          "Usage: lst add_test [--batch BATCH] [--loop #] [--concurrency #] "
3254          " [--distribute #:#] [--from GROUP] [--to GROUP] TEST..."                      },
3255         {"help",                Parser_help,            0,     "help"                   },
3256         {"--list-commands",     lst_list_commands,      0,     "list commands"          },
3257         {0,                     0,                      0,      NULL                    }
3258 };
3259
3260 int
3261 lst_initialize(void)
3262 {
3263         char   *key;
3264         char   *feats;
3265
3266         feats = getenv("LST_FEATURES");
3267         if (feats != NULL)
3268                 session_features = strtol(feats, NULL, 16);
3269
3270         if ((session_features & ~LST_FEATS_MASK) != 0) {
3271                 fprintf(stderr,
3272                         "Unsupported session features %x, "
3273                         "only support these features so far: %x\n",
3274                         (session_features & ~LST_FEATS_MASK), LST_FEATS_MASK);
3275                 return -1;
3276         }
3277
3278         key = getenv("LST_SESSION");
3279
3280         if (key == NULL) {
3281                 session_key = 0;
3282                 return 0;
3283         }
3284
3285         session_key = atoi(key);
3286
3287         return 0;
3288 }
3289
3290 static int lst_list_commands(int argc, char **argv)
3291 {
3292         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
3293
3294         Parser_list_commands(lst_cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
3295
3296         return 0;
3297 }
3298
3299 int
3300 main(int argc, char **argv)
3301 {
3302         int rc = 0;
3303
3304         setlinebuf(stdout);
3305
3306         rc = lst_initialize();
3307         if (rc < 0)
3308                 goto errorout;
3309
3310         rc = lustre_lnet_config_lib_init();
3311         if (rc < 0)
3312                 goto errorout;
3313
3314         Parser_init("lst > ", lst_cmdlist);
3315
3316         if (argc != 1)  {
3317                 rc = Parser_execarg(argc - 1, argv + 1, lst_cmdlist);
3318                 goto errorout;
3319         }
3320
3321         Parser_commands();
3322
3323 errorout:
3324         return rc;
3325 }