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