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