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