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