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