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