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