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