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