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