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