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