Whamcloud - gitweb
1f314c904c3aa7163fc761a256a274e441235709
[fs/lustre-release.git] / lnet / utils / portals.c
1 /*
2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Copyright (c) 2013, 2014, Intel Corporation.
5  *
6  *   This file is part of Portals, http://www.sf.net/projects/lustre/
7  *
8  *   Portals is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Portals is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Portals; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/lnetctl.h>
25 #include <lnet/socklnd.h>
26 #include <lnet/lnet.h>
27 #include <getopt.h>
28 #include <netdb.h>
29
30 unsigned int libcfs_debug;
31 unsigned int libcfs_printk = D_CANTMASK;
32
33 static int   g_net_set;
34 static __u32 g_net;
35
36 #define IOC_BUF_SIZE    8192
37 static char local_buf[IOC_BUF_SIZE];
38 static char *ioc_buf = local_buf;
39
40 /* Convert a string boolean to an int; "enable" -> 1 */
41 int
42 lnet_parse_bool (int *b, char *str)
43 {
44         if (!strcasecmp (str, "no") ||
45             !strcasecmp (str, "n") ||
46             !strcasecmp (str, "off") ||
47             !strcasecmp (str, "down") ||
48             !strcasecmp (str, "disable"))
49         {
50                 *b = 0;
51                 return (0);
52         }
53
54         if (!strcasecmp (str, "yes") ||
55             !strcasecmp (str, "y") ||
56             !strcasecmp (str, "on") ||
57             !strcasecmp (str, "up") ||
58             !strcasecmp (str, "enable"))
59         {
60                 *b = 1;
61                 return (0);
62         }
63
64         return (-1);
65 }
66
67 int
68 lnet_parse_port (int *port, char *str)
69 {
70         char      *end;
71
72         *port = strtol (str, &end, 0);
73
74         if (*end == 0 &&                        /* parsed whole string */
75             *port > 0 && *port < 65536)         /* minimal sanity check */
76                 return (0);
77
78         return (-1);
79 }
80
81 #ifdef HAVE_GETHOSTBYNAME
82 static struct hostent *
83 ptl_gethostbyname(char * hname) {
84         struct hostent *he;
85         he = gethostbyname(hname);
86         if (!he) {
87                 switch(h_errno) {
88                 case HOST_NOT_FOUND:
89                 case NO_ADDRESS:
90                         fprintf(stderr, "Unable to resolve hostname: %s\n",
91                                 hname);
92                         break;
93                 default:
94                         fprintf(stderr, "gethostbyname error for %s: %s\n",
95                                 hname, strerror(h_errno));
96                         break;
97                 }
98                 return NULL;
99         }
100         return he;
101 }
102 #endif
103
104 int
105 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
106 {
107         int             a;
108         int             b;
109         int             c;
110         int             d;
111
112         if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
113             (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
114             (c & ~0xff) == 0 && (d & ~0xff) == 0)
115         {
116                 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
117                 return (0);
118         }
119
120         return (-1);
121 }
122
123 int
124 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
125 {
126 #ifdef HAVE_GETHOSTBYNAME
127         struct hostent *he;
128 #endif
129
130         if (!strcmp (str, "_all_")) {
131                 *ipaddrp = 0;
132                 return (0);
133         }
134
135         if (lnet_parse_ipquad(ipaddrp, str) == 0)
136                 return (0);
137
138 #ifdef HAVE_GETHOSTBYNAME
139         if ((('a' <= str[0] && str[0] <= 'z') ||
140              ('A' <= str[0] && str[0] <= 'Z')) &&
141              (he = ptl_gethostbyname (str)) != NULL) {
142                 __u32 addr = *(__u32 *)he->h_addr;
143
144                 *ipaddrp = ntohl(addr);         /* HOST byte order */
145                 return (0);
146         }
147 #endif
148
149         return (-1);
150 }
151
152 char *
153 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
154 {
155 #ifdef HAVE_GETHOSTBYNAME
156         __u32           net_ip;
157         struct hostent *he;
158
159         if (lookup) {
160                 net_ip = htonl (ipaddr);
161                 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
162                 if (he != NULL) {
163                         strlcpy(str, he->h_name, strsize);
164                         return (str);
165                 }
166         }
167 #endif
168
169         sprintf (str, "%d.%d.%d.%d",
170                  (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
171                  (ipaddr >> 8) & 0xff, ipaddr & 0xff);
172         return (str);
173 }
174
175 int
176 lnet_parse_time (time_t *t, char *str)
177 {
178         char          *end;
179         int            n;
180         struct tm      tm;
181
182         *t = strtol (str, &end, 0);
183         if (*end == 0) /* parsed whole string */
184                 return (0);
185
186         memset (&tm, 0, sizeof (tm));
187         n = sscanf (str, "%d-%d-%d-%d:%d:%d",
188                     &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
189                     &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
190         if (n != 6)
191                 return (-1);
192
193         tm.tm_mon--;                    /* convert to 0 == Jan */
194         tm.tm_year -= 1900;             /* y2k quirk */
195         tm.tm_isdst = -1;               /* dunno if it's daylight savings... */
196
197         *t = mktime (&tm);
198         if (*t == (time_t)-1)
199                 return (-1);
200
201         return (0);
202 }
203
204 int
205 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
206 {
207         id_ptr->pid = LNET_PID_ANY;
208         id_ptr->nid = libcfs_str2nid(nid_str);
209         if (id_ptr->nid == LNET_NID_ANY) {
210                 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
211                 return -1;
212         }
213
214         return 0;
215 }
216
217 int g_net_is_set (char *cmd)
218 {
219         if (g_net_set)
220                 return 1;
221
222         if (cmd != NULL)
223                 fprintf(stderr,
224                         "You must run the 'network' command before '%s'.\n",
225                         cmd);
226         return 0;
227 }
228
229 int g_net_is_compatible (char *cmd, ...)
230 {
231         va_list       ap;
232         int           nal;
233
234         if (!g_net_is_set(cmd))
235                 return 0;
236
237         va_start(ap, cmd);
238
239         do {
240                 nal = va_arg (ap, int);
241                 if (nal == LNET_NETTYP(g_net)) {
242                         va_end (ap);
243                         return 1;
244                 }
245         } while (nal != 0);
246
247         va_end (ap);
248
249         if (cmd != NULL)
250                 fprintf (stderr,
251                          "Command %s not compatible with %s NAL\n",
252                          cmd,
253                          libcfs_lnd2str(LNET_NETTYP(g_net)));
254         return 0;
255 }
256
257 int ptl_initialize(int argc, char **argv)
258 {
259         register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
260                          LNET_DEV_MAJOR, LNET_DEV_MINOR);
261         return 0;
262 }
263
264
265 int jt_ptl_network(int argc, char **argv)
266 {
267         struct libcfs_ioctl_data data;
268         __u32                    net = LNET_NIDNET(LNET_NID_ANY);
269         int                      rc;
270
271         if (argc < 2) {
272                 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
273                 return 0;
274         }
275
276         if (!strcmp(argv[1], "unconfigure") ||
277             !strcmp(argv[1], "down")) {
278                 LIBCFS_IOC_INIT(data);
279                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
280
281                 if (rc == 0) {
282                         printf ("LNET ready to unload\n");
283                         return 0;
284                 }
285
286                 if (errno == EBUSY)
287                         fprintf(stderr, "LNET busy\n");
288                 else
289                         fprintf(stderr, "LNET unconfigure error %d: %s\n",
290                                 errno, strerror(errno));
291                 return -1;
292         }
293
294         if (!strcmp(argv[1], "configure") ||
295             !strcmp(argv[1], "up")) {
296                 LIBCFS_IOC_INIT(data);
297                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
298
299                 if (rc == 0) {
300                         printf ("LNET configured\n");
301                         return 0;
302                 }
303
304                 fprintf(stderr, "LNET configure error %d: %s\n",
305                         errno, strerror(errno));
306                 return -1;
307         }
308
309         net = libcfs_str2net(argv[1]);
310         if (net == LNET_NIDNET(LNET_NID_ANY)) {
311                 fprintf(stderr, "Can't parse net %s\n", argv[1]);
312                 return -1;
313         }
314
315         if (LNET_NETTYP(net) == CIBLND    ||
316             LNET_NETTYP(net) == OPENIBLND ||
317             LNET_NETTYP(net) == IIBLND    ||
318             LNET_NETTYP(net) == VIBLND) {
319                 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
320                 return -1;
321         }
322
323         g_net_set = 1;
324         g_net = net;
325         return 0;
326 }
327
328 int
329 jt_ptl_list_nids(int argc, char **argv)
330 {
331         struct libcfs_ioctl_data data;
332         int                      all = 0, return_nid = 0;
333         int                      count;
334         int                      rc;
335
336         all = (argc == 2) && (strcmp(argv[1], "all") == 0);
337         /* Hack to pass back value */
338         return_nid = (argc == 2) && (argv[1][0] == 1);
339
340         if ((argc > 2) && !(all || return_nid)) {
341                 fprintf(stderr, "usage: %s [all]\n", argv[0]);
342                 return 0;
343         }
344
345         for (count = 0;; count++) {
346                 LIBCFS_IOC_INIT (data);
347                 data.ioc_count = count;
348                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
349
350                 if (rc < 0) {
351                         if ((count > 0) && (errno == ENOENT))
352                                 /* We found them all */
353                                 break;
354                         fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
355                                 errno, strerror(errno));
356                         return -1;
357                 }
358
359                 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
360                         printf("%s\n", libcfs_nid2str(data.ioc_nid));
361                         if (return_nid) {
362                                 *(__u64 *)(argv[1]) = data.ioc_nid;
363                                 return_nid--;
364                         }
365                 }
366         }
367
368         return 0;
369 }
370
371 int
372 jt_ptl_which_nid (int argc, char **argv)
373 {
374         struct libcfs_ioctl_data data;
375         int          best_dist = 0;
376         int          best_order = 0;
377         lnet_nid_t   best_nid = LNET_NID_ANY;
378         int          dist;
379         int          order;
380         lnet_nid_t   nid;
381         char        *nidstr;
382         int          rc;
383         int          i;
384
385         if (argc < 2) {
386                 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
387                 return 0;
388         }
389
390         for (i = 1; i < argc; i++) {
391                 nidstr = argv[i];
392                 nid = libcfs_str2nid(nidstr);
393                 if (nid == LNET_NID_ANY) {
394                         fprintf(stderr, "Can't parse NID %s\n", nidstr);
395                         return -1;
396                 }
397
398                 LIBCFS_IOC_INIT(data);
399                 data.ioc_nid = nid;
400
401                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
402                 if (rc != 0) {
403                         fprintf(stderr, "Can't get distance to %s: %s\n",
404                                 nidstr, strerror(errno));
405                         return -1;
406                 }
407
408                 dist = data.ioc_u32[0];
409                 order = data.ioc_u32[1];
410
411                 if (dist < 0) {
412                         if (dist == -EHOSTUNREACH)
413                                 continue;
414
415                         fprintf(stderr, "Unexpected distance to %s: %d\n",
416                                 nidstr, dist);
417                         return -1;
418                 }
419
420                 if (best_nid == LNET_NID_ANY ||
421                     dist < best_dist ||
422                     (dist == best_dist && order < best_order)) {
423                         best_dist = dist;
424                         best_order = order;
425                         best_nid = nid;
426                 }
427         }
428
429         if (best_nid == LNET_NID_ANY) {
430                 fprintf(stderr, "No reachable NID\n");
431                 return -1;
432         }
433
434         printf("%s\n", libcfs_nid2str(best_nid));
435         return 0;
436 }
437
438 int
439 jt_ptl_print_interfaces (int argc, char **argv)
440 {
441         struct libcfs_ioctl_data data;
442         char                     buffer[3][HOST_NAME_MAX + 1];
443         int                      index;
444         int                      rc;
445
446         if (!g_net_is_compatible (argv[0], SOCKLND, 0))
447                 return -1;
448
449         for (index = 0;;index++) {
450                 LIBCFS_IOC_INIT(data);
451                 data.ioc_net   = g_net;
452                 data.ioc_count = index;
453
454                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
455                 if (rc != 0)
456                         break;
457
458                 printf ("%s: (%s/%s) npeer %d nroute %d\n",
459                         ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
460                                          sizeof(buffer[2]), 1),
461                         ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
462                                          sizeof(buffer[0]), 0),
463                         ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
464                                          sizeof(buffer[1]), 0),
465                         data.ioc_u32[2], data.ioc_u32[3]);
466         }
467
468         if (index == 0) {
469                 if (errno == ENOENT) {
470                         printf ("<no interfaces>\n");
471                 } else {
472                         fprintf(stderr, "Error getting interfaces: %s: "
473                                 "check dmesg.\n",
474                                 strerror(errno));
475                 }
476         }
477
478         return 0;
479 }
480
481 int
482 jt_ptl_add_interface (int argc, char **argv)
483 {
484         struct libcfs_ioctl_data data;
485         __u32                    ipaddr;
486         int                      rc;
487         __u32                    netmask = 0xffffff00;
488         int                      i;
489         int                      count;
490         char                    *end;
491
492         if (argc < 2 || argc > 3) {
493                 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
494                 return 0;
495         }
496
497         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
498                 return -1;
499
500         if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
501                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
502                 return -1;
503         }
504
505         if (argc > 2 ) {
506                 count = strtol(argv[2], &end, 0);
507                 if (count > 0 && count < 32 && *end == 0) {
508                         netmask = 0;
509                         for (i = count; i > 0; i--)
510                                 netmask = netmask|(1<<(32-i));
511                 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
512                         fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
513                         return -1;
514                 }
515         }
516
517         LIBCFS_IOC_INIT(data);
518         data.ioc_net    = g_net;
519         data.ioc_u32[0] = ipaddr;
520         data.ioc_u32[1] = netmask;
521
522         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
523         if (rc != 0) {
524                 fprintf (stderr, "failed to add interface: %s\n",
525                          strerror (errno));
526                 return -1;
527         }
528
529         return 0;
530 }
531
532 int
533 jt_ptl_del_interface (int argc, char **argv)
534 {
535         struct libcfs_ioctl_data data;
536         int                      rc;
537         __u32                    ipaddr = 0;
538
539         if (argc > 2) {
540                 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
541                 return 0;
542         }
543
544         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
545                 return -1;
546
547         if (argc == 2 &&
548             lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
549                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
550                 return -1;
551         }
552
553         LIBCFS_IOC_INIT(data);
554         data.ioc_net    = g_net;
555         data.ioc_u32[0] = ipaddr;
556
557         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
558         if (rc != 0) {
559                 fprintf (stderr, "failed to delete interface: %s\n",
560                          strerror (errno));
561                 return -1;
562         }
563
564         return 0;
565 }
566
567 int
568 jt_ptl_print_peers (int argc, char **argv)
569 {
570         struct libcfs_ioctl_data data;
571         lnet_process_id_t        id;
572         char                     buffer[2][HOST_NAME_MAX + 1];
573         int                      index;
574         int                      rc;
575
576         if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
577                 return -1;
578
579         for (index = 0;;index++) {
580                 LIBCFS_IOC_INIT(data);
581                 data.ioc_net     = g_net;
582                 data.ioc_count   = index;
583
584                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
585                 if (rc != 0)
586                         break;
587
588                 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
589                         id.nid = data.ioc_nid;
590                         id.pid = data.ioc_u32[4];
591                         printf ("%-20s [%d]%s->%s:%d #%d\n",
592                                 libcfs_id2str(id),
593                                 data.ioc_count, /* persistence */
594                                 /* my ip */
595                                 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
596                                                  sizeof(buffer[0]), 1),
597                                 /* peer ip */
598                                 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
599                                                  sizeof(buffer[1]), 1),
600                                 data.ioc_u32[1], /* peer port */
601                                 data.ioc_u32[3]); /* conn_count */
602                 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
603                         int disconn = data.ioc_flags >> 16;
604                         char *state;
605
606                         if (disconn)
607                                 state = "D";
608                         else
609                                 state = data.ioc_flags & 0xffff ? "C" : "U";
610
611                         printf ("%-20s (%d) %s [%d] "LPU64" "
612                                 "sq %d/%d tx %d/%d/%d\n",
613                                 libcfs_nid2str(data.ioc_nid), /* peer nid */
614                                 data.ioc_net, /* gemini device id */
615                                 state, /* peer is Connecting, Up, or Down */
616                                 data.ioc_count,   /* peer refcount */
617                                 data.ioc_u64[0], /* peerstamp */
618                                 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
619                                 /* fmaq, nfma, nrdma */
620                                 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
621                                 );
622                 } else {
623                         printf ("%-20s [%d]\n",
624                                 libcfs_nid2str(data.ioc_nid), data.ioc_count);
625                 }
626         }
627
628         if (index == 0) {
629                 if (errno == ENOENT) {
630                         printf ("<no peers>\n");
631                 } else {
632                         fprintf(stderr, "Error getting peer list: %s: "
633                                 "check dmesg.\n",
634                                 strerror(errno));
635                 }
636         }
637         return 0;
638 }
639
640 int
641 jt_ptl_add_peer (int argc, char **argv)
642 {
643         struct libcfs_ioctl_data data;
644         lnet_nid_t               nid;
645         __u32                    ip = 0;
646         int                      port = 0;
647         int                      rc;
648
649         if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
650                 return -1;
651
652         if (argc != 4) {
653                 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
654                          argv[0]);
655                 return 0;
656         }
657
658         nid = libcfs_str2nid(argv[1]);
659         if (nid == LNET_NID_ANY) {
660                 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
661                 return -1;
662         }
663
664         if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
665                 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
666                 return -1;
667         }
668
669         if (lnet_parse_port (&port, argv[3]) != 0) {
670                 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
671                 return -1;
672         }
673
674         LIBCFS_IOC_INIT(data);
675         data.ioc_net    = g_net;
676         data.ioc_nid    = nid;
677         data.ioc_u32[0] = ip;
678         data.ioc_u32[1] = port;
679
680         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
681         if (rc != 0) {
682                 fprintf (stderr, "failed to add peer: %s\n",
683                          strerror (errno));
684                 return -1;
685         }
686
687         return 0;
688 }
689
690 int
691 jt_ptl_del_peer (int argc, char **argv)
692 {
693         struct libcfs_ioctl_data data;
694         lnet_nid_t               nid = LNET_NID_ANY;
695         lnet_pid_t               pid = LNET_PID_ANY;
696         __u32                    ip = 0;
697         int                      rc;
698
699         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
700                 return -1;
701
702         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
703                 if (argc > 3) {
704                         fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
705                                  argv[0]);
706                         return 0;
707                 }
708         } else if (argc > 2) {
709                 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
710                 return 0;
711         }
712
713         if (argc > 1 &&
714             !libcfs_str2anynid(&nid, argv[1])) {
715                 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
716                 return -1;
717         }
718
719         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
720                 if (argc > 2 &&
721                     lnet_parse_ipaddr (&ip, argv[2]) != 0) {
722                         fprintf (stderr, "Can't parse ip addr: %s\n",
723                                  argv[2]);
724                         return -1;
725                 }
726         }
727
728         LIBCFS_IOC_INIT(data);
729         data.ioc_net    = g_net;
730         data.ioc_nid    = nid;
731         data.ioc_u32[0] = ip;
732         data.ioc_u32[1] = pid;
733
734         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
735         if (rc != 0) {
736                 fprintf (stderr, "failed to remove peer: %s\n",
737                          strerror (errno));
738                 return -1;
739         }
740
741         return 0;
742 }
743
744 int
745 jt_ptl_print_connections (int argc, char **argv)
746 {
747         struct libcfs_ioctl_data data;
748         lnet_process_id_t        id;
749         char                     buffer[2][HOST_NAME_MAX + 1];
750         int                      index;
751         int                      rc;
752
753         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
754                 return -1;
755
756         for (index = 0; ; index++) {
757                 LIBCFS_IOC_INIT(data);
758                 data.ioc_net     = g_net;
759                 data.ioc_count   = index;
760
761                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
762                 if (rc != 0)
763                         break;
764
765                 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
766                         id.nid = data.ioc_nid;
767                         id.pid = data.ioc_u32[6];
768                         printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
769                                 libcfs_id2str(id),
770                                 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
771                                 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
772                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
773                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
774                                 data.ioc_u32[4], /* scheduler */
775                                 /* local IP addr */
776                                 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
777                                                  sizeof(buffer[0]), 1),
778                                 /* remote IP addr */
779                                 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
780                                                  sizeof(buffer[1]), 1),
781                                 data.ioc_u32[1],         /* remote port */
782                                 data.ioc_count, /* tx buffer size */
783                                 data.ioc_u32[5], /* rx buffer size */
784                                 data.ioc_flags ? "nagle" : "nonagle");
785                 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
786                         printf ("%s mtu %d\n",
787                                 libcfs_nid2str(data.ioc_nid),
788                                 data.ioc_u32[0]); /* path MTU */
789                 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
790                         printf ("%-20s [%d]\n",
791                                 libcfs_nid2str(data.ioc_nid),
792                                 data.ioc_u32[0] /* device id */);
793                 } else {
794                         printf ("%s\n", libcfs_nid2str(data.ioc_nid));
795                 }
796         }
797
798         if (index == 0) {
799                 if (errno == ENOENT) {
800                         printf ("<no connections>\n");
801                 } else {
802                         fprintf(stderr, "Error getting connection list: %s: "
803                                 "check dmesg.\n",
804                                 strerror(errno));
805                 }
806         }
807         return 0;
808 }
809
810 int jt_ptl_disconnect(int argc, char **argv)
811 {
812         struct libcfs_ioctl_data data;
813         lnet_nid_t               nid = LNET_NID_ANY;
814         __u32                    ipaddr = 0;
815         int                      rc;
816
817         if (argc > 3) {
818                 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
819                 return 0;
820         }
821
822         if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
823                 return 0;
824
825         if (argc >= 2 &&
826             !libcfs_str2anynid(&nid, argv[1])) {
827                 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
828                 return -1;
829         }
830
831         if (g_net_is_compatible (NULL, SOCKLND, 0) &&
832             argc >= 3 &&
833             lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
834                 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
835                 return -1;
836         }
837
838         LIBCFS_IOC_INIT(data);
839         data.ioc_net     = g_net;
840         data.ioc_nid     = nid;
841         data.ioc_u32[0]  = ipaddr;
842
843         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
844         if (rc != 0) {
845                 fprintf(stderr, "failed to remove connection: %s\n",
846                         strerror(errno));
847                 return -1;
848         }
849
850         return 0;
851 }
852
853 int jt_ptl_push_connection (int argc, char **argv)
854 {
855         struct libcfs_ioctl_data data;
856         int                      rc;
857         lnet_nid_t               nid = LNET_NID_ANY;
858
859         if (argc > 2) {
860                 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
861                 return 0;
862         }
863
864         if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
865                 return -1;
866
867         if (argc > 1 &&
868             !libcfs_str2anynid(&nid, argv[1])) {
869                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
870                 return -1;
871         }
872
873         LIBCFS_IOC_INIT(data);
874         data.ioc_net     = g_net;
875         data.ioc_nid     = nid;
876
877         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
878         if (rc != 0) {
879                 fprintf(stderr, "failed to push connection: %s\n",
880                         strerror(errno));
881                 return -1;
882         }
883
884         return 0;
885 }
886
887 int jt_ptl_ping(int argc, char **argv)
888 {
889         int                      rc;
890         int                      timeout;
891         lnet_process_id_t        id;
892         lnet_process_id_t        ids[16];
893         int                      maxids = sizeof(ids)/sizeof(ids[0]);
894         struct libcfs_ioctl_data data;
895         char                    *sep;
896         int                      i;
897
898         if (argc < 2) {
899                 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
900                 return 0;
901         }
902
903         sep = strchr(argv[1], '-');
904         if (sep == NULL) {
905                 rc = lnet_parse_nid(argv[1], &id);
906                 if (rc != 0)
907                         return -1;
908         } else {
909                 char   *end;
910
911                 if (argv[1][0] == 'u' ||
912                     argv[1][0] == 'U')
913                         id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
914                 else
915                         id.pid = strtoul(argv[1], &end, 0);
916
917                 if (end != sep) { /* assuming '-' is part of hostname */
918                         rc = lnet_parse_nid(argv[1], &id);
919                         if (rc != 0)
920                                 return -1;
921                 } else {
922                         id.nid = libcfs_str2nid(sep + 1);
923
924                         if (id.nid == LNET_NID_ANY) {
925                                 fprintf(stderr,
926                                         "Can't parse process id \"%s\"\n",
927                                         argv[1]);
928                                 return -1;
929                         }
930                 }
931         }
932
933         if (argc > 2)
934                 timeout = 1000 * atol(argv[2]);
935         else
936                 timeout = 1000;                 /* default 1 second timeout */
937
938         LIBCFS_IOC_INIT (data);
939         data.ioc_nid     = id.nid;
940         data.ioc_u32[0]  = id.pid;
941         data.ioc_u32[1]  = timeout;
942         data.ioc_plen1   = sizeof(ids);
943         data.ioc_pbuf1   = (char *)ids;
944
945         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
946         if (rc != 0) {
947                 fprintf(stderr, "failed to ping %s: %s\n",
948                         id.pid == LNET_PID_ANY ?
949                         libcfs_nid2str(id.nid) : libcfs_id2str(id),
950                         strerror(errno));
951                 return -1;
952         }
953
954         for (i = 0; i < data.ioc_count && i < maxids; i++)
955                 printf("%s\n", libcfs_id2str(ids[i]));
956
957         if (data.ioc_count > maxids)
958                 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
959
960         return 0;
961 }
962
963 int jt_ptl_mynid(int argc, char **argv)
964 {
965         struct libcfs_ioctl_data data;
966         lnet_nid_t               nid;
967         int rc;
968
969         if (argc != 2) {
970                 fprintf(stderr, "usage: %s NID\n", argv[0]);
971                 return 0;
972         }
973
974         nid = libcfs_str2nid(argv[1]);
975         if (nid == LNET_NID_ANY) {
976                 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
977                 return -1;
978         }
979
980         LIBCFS_IOC_INIT(data);
981         data.ioc_net = LNET_NIDNET(nid);
982         data.ioc_nid = nid;
983
984         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
985         if (rc < 0)
986                 fprintf(stderr, "setting my NID failed: %s\n",
987                        strerror(errno));
988         else
989                 printf("registered my nid %s\n", libcfs_nid2str(nid));
990
991         return 0;
992 }
993
994 int
995 jt_ptl_fail_nid (int argc, char **argv)
996 {
997         int                      rc;
998         lnet_nid_t               nid;
999         int                      threshold;
1000         struct libcfs_ioctl_data data;
1001
1002         if (argc < 2 || argc > 3)
1003         {
1004                 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1005                 return (0);
1006         }
1007
1008         if (!libcfs_str2anynid(&nid, argv[1]))
1009         {
1010                 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1011                 return (-1);
1012         }
1013
1014         if (argc < 3) {
1015                 threshold = LNET_MD_THRESH_INF;
1016         } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1017                 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1018                 return (-1);
1019         }
1020
1021         LIBCFS_IOC_INIT (data);
1022         data.ioc_nid = nid;
1023         data.ioc_count = threshold;
1024
1025         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1026         if (rc < 0)
1027                 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1028                          strerror (errno));
1029         else
1030                 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1031
1032         return (0);
1033 }
1034
1035 int
1036 jt_ptl_add_route (int argc, char **argv)
1037 {
1038         struct lnet_ioctl_config_data data;
1039         lnet_nid_t               gateway_nid;
1040         unsigned int             hops = 1;
1041         unsigned int             priority = 0;
1042         char                    *end;
1043         int                      rc;
1044
1045         if (argc < 2 || argc > 4) {
1046                 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1047                         argv[0]);
1048                 return -1;
1049         }
1050
1051         if (g_net_is_set(argv[0]) == 0)
1052                 return -1;
1053
1054         gateway_nid = libcfs_str2nid(argv[1]);
1055         if (gateway_nid == LNET_NID_ANY) {
1056                 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1057                 return -1;
1058         }
1059
1060         if (argc > 2) {
1061                 hops = strtoul(argv[2], &end, 0);
1062                 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1063                         fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1064                                 argv[2]);
1065                         return -1;
1066                 }
1067                 if (argc == 4) {
1068                         priority = strtoul(argv[3], &end, 0);
1069                         if (end != NULL && *end != 0) {
1070                                 fprintf(stderr,
1071                                         "Can't parse priority \"%s\"\n",
1072                                         argv[3]);
1073                                 return -1;
1074                         }
1075                 }
1076         }
1077
1078         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1079         data.cfg_net = g_net;
1080         data.cfg_config_u.cfg_route.rtr_hop = hops;
1081         data.cfg_nid = gateway_nid;
1082         data.cfg_config_u.cfg_route.rtr_priority = priority;
1083
1084         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1085         if (rc != 0) {
1086                 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1087                         strerror(errno));
1088                 return -1;
1089         }
1090
1091         return 0;
1092 }
1093
1094 int
1095 jt_ptl_del_route (int argc, char **argv)
1096 {
1097         struct lnet_ioctl_config_data data;
1098         lnet_nid_t               nid;
1099         int                      rc;
1100
1101         if (argc != 2) {
1102                 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1103                 return 0;
1104         }
1105
1106         if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1107                 fprintf(stderr, "Can't parse gateway NID "
1108                         "\"%s\"\n", argv[1]);
1109                 return -1;
1110         }
1111
1112         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1113         data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1114         data.cfg_nid = nid;
1115
1116         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1117         if (rc != 0) {
1118                 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1119                         libcfs_nid2str(nid), strerror(errno));
1120                 return -1;
1121         }
1122
1123         return 0;
1124 }
1125
1126 int
1127 jt_ptl_notify_router (int argc, char **argv)
1128 {
1129         struct libcfs_ioctl_data data;
1130         int                      enable;
1131         lnet_nid_t               nid;
1132         int                      rc;
1133         struct timeval           now;
1134         time_t                   when;
1135
1136         if (argc < 3)
1137         {
1138                 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n", 
1139                          argv[0]);
1140                 return (0);
1141         }
1142
1143         nid = libcfs_str2nid(argv[1]);
1144         if (nid == LNET_NID_ANY) {
1145                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1146                 return (-1);
1147         }
1148
1149         if (lnet_parse_bool (&enable, argv[2]) != 0) {
1150                 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1151                 return (-1);
1152         }
1153
1154         gettimeofday(&now, NULL);
1155
1156         if (argc < 4) {
1157                 when = now.tv_sec;
1158         } else if (lnet_parse_time (&when, argv[3]) != 0) {
1159                 fprintf(stderr, "Can't parse time %s\n"
1160                         "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1161                         "or an absolute unix time in seconds\n", argv[3]);
1162                 return (-1);
1163         } else if (when > now.tv_sec) {
1164                 fprintf (stderr, "%s specifies a time in the future\n",
1165                          argv[3]);
1166                 return (-1);
1167         }
1168
1169         LIBCFS_IOC_INIT(data);
1170         data.ioc_nid = nid;
1171         data.ioc_flags = enable;
1172         /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1173         data.ioc_u64[0] = (__u64)when;
1174
1175         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1176         if (rc != 0) {
1177                 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1178                          libcfs_nid2str(nid), strerror (errno));
1179                 return (-1);
1180         }
1181
1182         return (0);
1183 }
1184
1185 int
1186 jt_ptl_print_routes (int argc, char **argv)
1187 {
1188         struct lnet_ioctl_config_data  data;
1189         int                       rc;
1190         int                       index;
1191         __u32                     net;
1192         lnet_nid_t                nid;
1193         unsigned int              hops;
1194         int                       alive;
1195         unsigned int              pri;
1196
1197         for (index = 0; ; index++) {
1198                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1199                 data.cfg_count = index;
1200
1201                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1202                 if (rc != 0)
1203                         break;
1204
1205                 net     = data.cfg_net;
1206                 hops    = data.cfg_config_u.cfg_route.rtr_hop;
1207                 nid     = data.cfg_nid;
1208                 alive   = data.cfg_config_u.cfg_route.rtr_flags;
1209                 pri     = data.cfg_config_u.cfg_route.rtr_priority;
1210
1211                 printf("net %18s hops %u gw %32s %s pri %u\n",
1212                        libcfs_net2str(net), hops,
1213                        libcfs_nid2str(nid), alive ? "up" : "down", pri);
1214         }
1215
1216         if (errno != ENOENT)
1217                 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1218                         strerror(errno));
1219
1220         return 0;
1221 }
1222
1223 static int
1224 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1225 {
1226         lnet_nid_t nid;
1227         __u32      net;
1228         int        rc = 0;
1229
1230         /* NB: can't support range ipaddress except * and *@net */
1231         if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1232                 net = libcfs_str2net(str + 2);
1233                 if (net == LNET_NIDNET(LNET_NID_ANY))
1234                         goto failed;
1235
1236                 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1237         } else {
1238                 rc = libcfs_str2anynid(&nid, str);
1239                 if (!rc)
1240                         goto failed;
1241         }
1242
1243         *nid_p = nid;
1244         return 0;
1245 failed:
1246         fprintf(stderr, "Invalid NID : %s\n", str);
1247         return -1;
1248 }
1249
1250 static int
1251 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1252 {
1253         if (!strcasecmp(msg_str, "put")) {
1254                 *mask_p |= LNET_PUT_BIT;
1255                 return 0;
1256
1257         } else if (!strcasecmp(msg_str, "ack")) {
1258                 *mask_p |= LNET_ACK_BIT;
1259                 return 0;
1260
1261         } else if (!strcasecmp(msg_str, "get")) {
1262                 *mask_p |= LNET_GET_BIT;
1263                 return 0;
1264
1265         } else if (!strcasecmp(msg_str, "reply")) {
1266                 *mask_p |= LNET_REPLY_BIT;
1267                 return 0;
1268         }
1269
1270         fprintf(stderr, "unknown message type %s\n", msg_str);
1271         return -1;
1272 }
1273
1274 static int
1275 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1276 {
1277         unsigned long rc = strtoul(optarg, NULL, 0);
1278
1279         if (rc >= 64) {
1280                 fprintf(stderr, "invalid portal: %lu\n", rc);
1281                 return -1;
1282         }
1283
1284         *mask_p |= (1ULL << rc);
1285         return 0;
1286 }
1287
1288 static int
1289 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1290 {
1291         struct libcfs_ioctl_data  data = {{0}};
1292         struct lnet_fault_attr    attr;
1293         char                     *optstr;
1294         int                       rc;
1295
1296         static struct option opts[] = {
1297                 {"source",      required_argument,      0,      's'},
1298                 {"dest",        required_argument,      0,      'd'},
1299                 {"rate",        required_argument,      0,      'r'},
1300                 {"interval",    required_argument,      0,      'i'},
1301                 {"latency",     required_argument,      0,      'l'},
1302                 {"portal",      required_argument,      0,      'p'},
1303                 {"message",     required_argument,      0,      'm'},
1304                 {0, 0, 0, 0}
1305         };
1306
1307         if (argc == 1) {
1308                 fprintf(stderr, "Failed, please provide source, destination "
1309                                 "and rate of rule\n");
1310                 return -1;
1311         }
1312
1313         optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1314         memset(&attr, 0, sizeof(attr));
1315         while (1) {
1316                 char c = getopt_long(argc, argv, optstr, opts, NULL);
1317
1318                 if (c == -1)
1319                         break;
1320
1321                 switch (c) {
1322                 case 's': /* source NID/NET */
1323                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1324                         if (rc != 0)
1325                                 goto getopt_failed;
1326                         break;
1327
1328                 case 'd': /* dest NID/NET */
1329                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1330                         if (rc != 0)
1331                                 goto getopt_failed;
1332                         break;
1333
1334                 case 'r': /* drop rate */
1335                         if (opc == LNET_CTL_DROP_ADD)
1336                                 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1337                         else
1338                                 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1339                         break;
1340
1341                 case 'i': /* time interval (# seconds) for message drop */
1342                         if (opc == LNET_CTL_DROP_ADD)
1343                                 attr.u.drop.da_interval = strtoul(optarg,
1344                                                                   NULL, 0);
1345                         else
1346                                 attr.u.delay.la_interval = strtoul(optarg,
1347                                                                    NULL, 0);
1348                         break;
1349
1350                 case 'l': /* seconds to wait before activating rule */
1351                         attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1352                         break;
1353
1354                 case 'p': /* portal to filter */
1355                         rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1356                         if (rc != 0)
1357                                 goto getopt_failed;
1358                         break;
1359
1360                 case 'm': /* message types to filter */
1361                         rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1362                         if (rc != 0)
1363                                 goto getopt_failed;
1364                         break;
1365
1366                 default:
1367                         fprintf(stderr, "error: %s: option '%s' "
1368                                 "unrecognized\n", argv[0], argv[optind - 1]);
1369                         goto getopt_failed;
1370                 }
1371         }
1372         optind = 1;
1373
1374         if (opc == LNET_CTL_DROP_ADD) {
1375                 /* NB: drop rate and interval are exclusive to each other */
1376                 if (!((attr.u.drop.da_rate == 0) ^
1377                       (attr.u.drop.da_interval == 0))) {
1378                         fprintf(stderr,
1379                                 "please provide either drop rate or interval "
1380                                 "but not both at the same time.\n");
1381                         return -1;
1382                 }
1383         } else if (opc == LNET_CTL_DELAY_ADD) {
1384                 if (!((attr.u.delay.la_rate == 0) ^
1385                       (attr.u.delay.la_interval == 0))) {
1386                         fprintf(stderr,
1387                                 "please provide either delay rate or interval "
1388                                 "but not both at the same time.\n");
1389                         return -1;
1390                 }
1391
1392                 if (attr.u.delay.la_latency == 0) {
1393                         fprintf(stderr, "latency cannot be zero\n");
1394                         return -1;
1395                 }
1396         }
1397
1398         if (attr.fa_src == 0 || attr.fa_dst == 0) {
1399                 fprintf(stderr, "Please provide both source and destination "
1400                                 "of %s rule\n", name);
1401                 return -1;
1402         }
1403
1404         data.ioc_flags = opc;
1405         data.ioc_inllen1 = sizeof(attr);
1406         data.ioc_inlbuf1 = (char *)&attr;
1407         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1408                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1409                 return -1;
1410         }
1411
1412         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1413         if (rc != 0) {
1414                 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1415                         name, libcfs_nid2str(attr.fa_src),
1416                         libcfs_nid2str(attr.fa_dst), strerror(errno));
1417                 return -1;
1418         }
1419
1420         printf("Added %s rule %s->%s (1/%d)\n",
1421                name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1422                opc == LNET_CTL_DROP_ADD ?
1423                attr.u.drop.da_rate : attr.u.delay.la_rate);
1424         return 0;
1425
1426 getopt_failed:
1427         optind = 1;
1428         return -1;
1429 }
1430
1431 int
1432 jt_ptl_drop_add(int argc, char **argv)
1433 {
1434         return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1435 }
1436
1437 int
1438 jt_ptl_delay_add(int argc, char **argv)
1439 {
1440         return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1441 }
1442
1443 static int
1444 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1445 {
1446         struct libcfs_ioctl_data data = {{0}};
1447         struct lnet_fault_attr   attr;
1448         bool                     all = false;
1449         int                      rc;
1450
1451         static struct option opts[] = {
1452                 {"source",      required_argument,      0,      's'},
1453                 {"dest",        required_argument,      0,      'd'},
1454                 {"all",         no_argument,            0,      'a'},
1455                 {0, 0, 0, 0}
1456         };
1457
1458         if (argc == 1) {
1459                 fprintf(stderr, "Failed, please provide source and "
1460                                 "destination of rule\n");
1461                 return -1;
1462         }
1463
1464         memset(&attr, 0, sizeof(attr));
1465         while (1) {
1466                 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1467
1468                 if (c == -1 || all)
1469                         break;
1470
1471                 switch (c) {
1472                 case 's':
1473                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1474                         if (rc != 0)
1475                                 goto getopt_failed;
1476                         break;
1477                 case 'd':
1478                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1479                         if (rc != 0)
1480                                 goto getopt_failed;
1481                         break;
1482                 case 'a':
1483                         attr.fa_src = attr.fa_dst = 0;
1484                         all = true;
1485                         break;
1486                 default:
1487                         fprintf(stderr, "error: %s: option '%s' "
1488                                 "unrecognized\n", argv[0], argv[optind - 1]);
1489                         goto getopt_failed;
1490                 }
1491         }
1492         optind = 1;
1493
1494         data.ioc_flags = opc;
1495         data.ioc_inllen1 = sizeof(attr);
1496         data.ioc_inlbuf1 = (char *)&attr;
1497         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1498                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1499                 return -1;
1500         }
1501
1502         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1503         if (rc != 0) {
1504                 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1505                         all ? "all" : libcfs_nid2str(attr.fa_src),
1506                         all ? "all" : libcfs_nid2str(attr.fa_dst),
1507                         strerror(errno));
1508                 return -1;
1509         }
1510
1511         libcfs_ioctl_unpack(&data, ioc_buf);
1512         printf("Removed %d %s rules\n", data.ioc_count, name);
1513         return 0;
1514
1515 getopt_failed:
1516         optind = 1;
1517         return -1;
1518 }
1519
1520 int
1521 jt_ptl_drop_del(int argc, char **argv)
1522 {
1523         return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1524 }
1525
1526 int
1527 jt_ptl_delay_del(int argc, char **argv)
1528 {
1529         return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1530 }
1531
1532 static int
1533 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1534 {
1535         struct libcfs_ioctl_data   data = {{0}};
1536         int                        rc;
1537
1538         LIBCFS_IOC_INIT(data);
1539         data.ioc_flags = opc;
1540
1541         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1542         if (rc != 0) {
1543                 fprintf(stderr, "failed to reset %s stats: %s\n",
1544                         name, strerror(errno));
1545                 return -1;
1546         }
1547         return 0;
1548 }
1549
1550 int
1551 jt_ptl_drop_reset(int argc, char **argv)
1552 {
1553         return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1554 }
1555
1556 int
1557 jt_ptl_delay_reset(int argc, char **argv)
1558 {
1559         return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1560                                       argc, argv);
1561 }
1562
1563 static int
1564 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1565 {
1566         struct libcfs_ioctl_data data = {{0}};
1567         struct lnet_fault_attr   attr;
1568         struct lnet_fault_stat   stat;
1569         int                      pos;
1570
1571         printf("LNet %s rules:\n", name);
1572         for (pos = 0;; pos++) {
1573                 int             rc;
1574
1575                 memset(&attr, 0, sizeof(attr));
1576                 memset(&stat, 0, sizeof(stat));
1577
1578                 data.ioc_count = pos;
1579                 data.ioc_flags = opc;
1580                 data.ioc_inllen1 = sizeof(attr);
1581                 data.ioc_inlbuf1 = (char *)&attr;
1582                 data.ioc_inllen2 = sizeof(stat);
1583                 data.ioc_inlbuf2 = (char *)&stat;
1584                 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1585                         fprintf(stderr, "libcfs_ioctl_pack failed\n");
1586                         return -1;
1587                 }
1588
1589                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1590                 if (rc != 0)
1591                         break;
1592
1593                 libcfs_ioctl_unpack(&data, ioc_buf);
1594
1595                 if (opc == LNET_CTL_DROP_LIST) {
1596                         printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1597                                LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1598                                LPU64", REP "LPU64"\n",
1599                                libcfs_nid2str(attr.fa_src),
1600                                libcfs_nid2str(attr.fa_dst),
1601                                attr.u.drop.da_rate, attr.u.drop.da_interval,
1602                                attr.fa_ptl_mask, attr.fa_msg_mask,
1603                                stat.u.drop.ds_dropped, stat.fs_count,
1604                                stat.fs_put, stat.fs_ack,
1605                                stat.fs_get, stat.fs_reply);
1606
1607                 } else if (opc == LNET_CTL_DELAY_LIST) {
1608                         printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1609                                ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1610                                ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1611                                libcfs_nid2str(attr.fa_src),
1612                                libcfs_nid2str(attr.fa_dst),
1613                                attr.u.delay.la_rate, attr.u.delay.la_interval,
1614                                attr.u.delay.la_latency,
1615                                attr.fa_ptl_mask, attr.fa_msg_mask,
1616                                stat.u.delay.ls_delayed, stat.fs_count,
1617                                stat.fs_put, stat.fs_ack, stat.fs_get,
1618                                stat.fs_reply);
1619                 }
1620         }
1621         printf("found total %d\n", pos);
1622
1623         return 0;
1624 }
1625
1626 int
1627 jt_ptl_drop_list(int argc, char **argv)
1628 {
1629         return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1630 }
1631
1632 int
1633 jt_ptl_delay_list(int argc, char **argv)
1634 {
1635         return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1636 }
1637
1638 double
1639 get_cycles_per_usec ()
1640 {
1641         FILE      *f = fopen ("/proc/cpuinfo", "r");
1642         double     mhz;
1643         char      line[64];
1644
1645         if (f != NULL) {
1646                 while (fgets (line, sizeof (line), f) != NULL)
1647                         if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1648                                 fclose (f);
1649                                 return (mhz);
1650                         }
1651                 fclose (f);
1652         }
1653
1654         fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1655         return (1000.0);
1656 }
1657
1658 int jt_ptl_memhog(int argc, char **argv)
1659 {
1660         static int                gfp = 0;        /* sticky! */
1661
1662         struct libcfs_ioctl_data  data;
1663         int                       rc;
1664         int                       count;
1665         char                     *end;
1666
1667         if (argc < 2)  {
1668                 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1669                 return 0;
1670         }
1671
1672         count = strtol(argv[1], &end, 0);
1673         if (count < 0 || *end != 0) {
1674                 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1675                 return -1;
1676         }
1677
1678         if (argc >= 3) {
1679                 rc = strtol(argv[2], &end, 0);
1680                 if (*end != 0) {
1681                         fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1682                         return -1;
1683                 }
1684                 gfp = rc;
1685         }
1686
1687         LIBCFS_IOC_INIT(data);
1688         data.ioc_count = count;
1689         data.ioc_flags = gfp;
1690         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1691
1692         if (rc != 0) {
1693                 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1694                 return -1;
1695         }
1696
1697         printf("memhog %d OK\n", count);
1698         return 0;
1699 }
1700
1701 int jt_ptl_testprotocompat(int argc, char **argv)
1702 {
1703         struct libcfs_ioctl_data  data;
1704         int                       rc;
1705         int                       flags;
1706         char                     *end;
1707
1708         if (argc < 2)  {
1709                 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1710                 return 0;
1711         }
1712
1713         flags = strtol(argv[1], &end, 0);
1714         if (flags < 0 || *end != 0) {
1715                 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1716                 return -1;
1717         }
1718
1719         LIBCFS_IOC_INIT(data);
1720         data.ioc_flags = flags;
1721         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1722
1723         if (rc != 0) {
1724                 fprintf(stderr, "test proto compat %x failed: %s\n",
1725                         flags, strerror(errno));
1726                 return -1;
1727         }
1728
1729         printf("test proto compat %x OK\n", flags);
1730         return 0;
1731 }
1732
1733