Whamcloud - gitweb
fd86a14090709532fcb30c3a723a7dc7d186279b
[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 <inttypes.h>
25 #include <limits.h>
26 #ifdef HAVE_NETDB_H
27 # include <netdb.h>
28 #endif
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <time.h>
36 #include <linux/types.h>
37
38 #include <libcfs/util/string.h>
39 #include <libcfs/util/ioctl.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] %ju sq %d/%d tx %d/%d/%d\n",
639                                libcfs_nid2str(data.ioc_nid), /* peer nid */
640                                data.ioc_net, /* gemini device id */
641                                state, /* peer is Connecting, Up, or Down */
642                                data.ioc_count,   /* peer refcount */
643                                (uintmax_t)data.ioc_u64[0], /* peerstamp */
644                                data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
645                                /* fmaq, nfma, nrdma */
646                                data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
647                 } else {
648                         printf ("%-20s [%d]\n",
649                                 libcfs_nid2str(data.ioc_nid), data.ioc_count);
650                 }
651         }
652
653         if (index == 0) {
654                 if (errno == ENOENT) {
655                         printf ("<no peers>\n");
656                 } else {
657                         fprintf(stderr, "Error getting peer list: %s: "
658                                 "check dmesg.\n",
659                                 strerror(errno));
660                 }
661         }
662         return 0;
663 }
664
665 int jt_ptl_add_peer(int argc, char **argv)
666 {
667         struct libcfs_ioctl_data data;
668         lnet_nid_t nid;
669         __u32 ip = 0;
670         int port = 0;
671         int rc;
672
673         if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
674                 return -1;
675
676         if (argc != 4) {
677                 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
678                         argv[0]);
679                 return 0;
680         }
681
682         nid = libcfs_str2nid(argv[1]);
683         if (nid == LNET_NID_ANY) {
684                 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
685                 return -1;
686         }
687
688         if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
689                 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
690                 return -1;
691         }
692
693         if (lnet_parse_port (&port, argv[3]) != 0) {
694                 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
695                 return -1;
696         }
697
698         LIBCFS_IOC_INIT(data);
699         data.ioc_net    = g_net;
700         data.ioc_nid    = nid;
701         data.ioc_u32[0] = ip;
702         data.ioc_u32[1] = port;
703
704         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
705         if (rc != 0) {
706                 fprintf (stderr, "failed to add peer: %s\n",
707                          strerror (errno));
708                 return -1;
709         }
710
711         return 0;
712 }
713
714 int
715 jt_ptl_del_peer (int argc, char **argv)
716 {
717         struct libcfs_ioctl_data data;
718         lnet_nid_t               nid = LNET_NID_ANY;
719         lnet_pid_t               pid = LNET_PID_ANY;
720         __u32                    ip = 0;
721         int                      rc;
722
723         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
724                 return -1;
725
726         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
727                 if (argc > 3) {
728                         fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
729                                  argv[0]);
730                         return 0;
731                 }
732         } else if (argc > 2) {
733                 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
734                 return 0;
735         }
736
737         if (argc > 1 &&
738             !libcfs_str2anynid(&nid, argv[1])) {
739                 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
740                 return -1;
741         }
742
743         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
744                 if (argc > 2 &&
745                     lnet_parse_ipaddr (&ip, argv[2]) != 0) {
746                         fprintf (stderr, "Can't parse ip addr: %s\n",
747                                  argv[2]);
748                         return -1;
749                 }
750         }
751
752         LIBCFS_IOC_INIT(data);
753         data.ioc_net    = g_net;
754         data.ioc_nid    = nid;
755         data.ioc_u32[0] = ip;
756         data.ioc_u32[1] = pid;
757
758         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
759         if (rc != 0) {
760                 fprintf (stderr, "failed to remove peer: %s\n",
761                          strerror (errno));
762                 return -1;
763         }
764
765         return 0;
766 }
767
768 int
769 jt_ptl_print_connections (int argc, char **argv)
770 {
771         struct libcfs_ioctl_data data;
772         lnet_process_id_t        id;
773         char                     buffer[2][HOST_NAME_MAX + 1];
774         int                      index;
775         int                      rc;
776
777         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
778                 return -1;
779
780         for (index = 0; ; index++) {
781                 LIBCFS_IOC_INIT(data);
782                 data.ioc_net     = g_net;
783                 data.ioc_count   = index;
784
785                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
786                 if (rc != 0)
787                         break;
788
789                 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
790                         id.nid = data.ioc_nid;
791                         id.pid = data.ioc_u32[6];
792                         printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
793                                 libcfs_id2str(id),
794                                 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
795                                 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
796                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
797                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
798                                 data.ioc_u32[4], /* scheduler */
799                                 /* local IP addr */
800                                 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
801                                                  sizeof(buffer[0]), 1),
802                                 /* remote IP addr */
803                                 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
804                                                  sizeof(buffer[1]), 1),
805                                 data.ioc_u32[1],         /* remote port */
806                                 data.ioc_count, /* tx buffer size */
807                                 data.ioc_u32[5], /* rx buffer size */
808                                 data.ioc_flags ? "nagle" : "nonagle");
809                 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
810                         printf ("%s mtu %d\n",
811                                 libcfs_nid2str(data.ioc_nid),
812                                 data.ioc_u32[0]); /* path MTU */
813                 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
814                         printf ("%-20s [%d]\n",
815                                 libcfs_nid2str(data.ioc_nid),
816                                 data.ioc_u32[0] /* device id */);
817                 } else {
818                         printf ("%s\n", libcfs_nid2str(data.ioc_nid));
819                 }
820         }
821
822         if (index == 0) {
823                 if (errno == ENOENT) {
824                         printf ("<no connections>\n");
825                 } else {
826                         fprintf(stderr, "Error getting connection list: %s: "
827                                 "check dmesg.\n",
828                                 strerror(errno));
829                 }
830         }
831         return 0;
832 }
833
834 int jt_ptl_disconnect(int argc, char **argv)
835 {
836         struct libcfs_ioctl_data data;
837         lnet_nid_t               nid = LNET_NID_ANY;
838         __u32                    ipaddr = 0;
839         int                      rc;
840
841         if (argc > 3) {
842                 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
843                 return 0;
844         }
845
846         if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
847                 return 0;
848
849         if (argc >= 2 &&
850             !libcfs_str2anynid(&nid, argv[1])) {
851                 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
852                 return -1;
853         }
854
855         if (g_net_is_compatible (NULL, SOCKLND, 0) &&
856             argc >= 3 &&
857             lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
858                 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
859                 return -1;
860         }
861
862         LIBCFS_IOC_INIT(data);
863         data.ioc_net     = g_net;
864         data.ioc_nid     = nid;
865         data.ioc_u32[0]  = ipaddr;
866
867         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
868         if (rc != 0) {
869                 fprintf(stderr, "failed to remove connection: %s\n",
870                         strerror(errno));
871                 return -1;
872         }
873
874         return 0;
875 }
876
877 int jt_ptl_push_connection (int argc, char **argv)
878 {
879         struct libcfs_ioctl_data data;
880         int                      rc;
881         lnet_nid_t               nid = LNET_NID_ANY;
882
883         if (argc > 2) {
884                 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
885                 return 0;
886         }
887
888         if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
889                 return -1;
890
891         if (argc > 1 &&
892             !libcfs_str2anynid(&nid, argv[1])) {
893                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
894                 return -1;
895         }
896
897         LIBCFS_IOC_INIT(data);
898         data.ioc_net     = g_net;
899         data.ioc_nid     = nid;
900
901         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
902         if (rc != 0) {
903                 fprintf(stderr, "failed to push connection: %s\n",
904                         strerror(errno));
905                 return -1;
906         }
907
908         return 0;
909 }
910
911 int jt_ptl_ping(int argc, char **argv)
912 {
913         int                      rc;
914         int                      timeout;
915         lnet_process_id_t        id;
916         lnet_process_id_t        ids[16];
917         int                      maxids = sizeof(ids)/sizeof(ids[0]);
918         struct libcfs_ioctl_data data;
919         char                    *sep;
920         int                      i;
921
922         if (argc < 2) {
923                 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
924                 return 0;
925         }
926
927         sep = strchr(argv[1], '-');
928         if (sep == NULL) {
929                 rc = lnet_parse_nid(argv[1], &id);
930                 if (rc != 0)
931                         return -1;
932         } else {
933                 char   *end;
934
935                 if (argv[1][0] == 'u' ||
936                     argv[1][0] == 'U')
937                         id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
938                 else
939                         id.pid = strtoul(argv[1], &end, 0);
940
941                 if (end != sep) { /* assuming '-' is part of hostname */
942                         rc = lnet_parse_nid(argv[1], &id);
943                         if (rc != 0)
944                                 return -1;
945                 } else {
946                         id.nid = libcfs_str2nid(sep + 1);
947
948                         if (id.nid == LNET_NID_ANY) {
949                                 fprintf(stderr,
950                                         "Can't parse process id \"%s\"\n",
951                                         argv[1]);
952                                 return -1;
953                         }
954                 }
955         }
956
957         if (argc > 2) {
958                 timeout = 1000 * atol(argv[2]);
959                 if (timeout > 120 * 1000) {
960                         fprintf(stderr, "Timeout %s is to large\n",
961                                 argv[2]);
962                         return -1;
963                 }
964         } else
965                 timeout = 1000;                 /* default 1 second timeout */
966
967         LIBCFS_IOC_INIT (data);
968         data.ioc_nid     = id.nid;
969         data.ioc_u32[0]  = id.pid;
970         data.ioc_u32[1]  = timeout;
971         data.ioc_plen1   = sizeof(ids);
972         data.ioc_pbuf1   = (char *)ids;
973
974         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
975         if (rc != 0) {
976                 fprintf(stderr, "failed to ping %s: %s\n",
977                         id.pid == LNET_PID_ANY ?
978                         libcfs_nid2str(id.nid) : libcfs_id2str(id),
979                         strerror(errno));
980                 return -1;
981         }
982
983         for (i = 0; i < data.ioc_count && i < maxids; i++)
984                 printf("%s\n", libcfs_id2str(ids[i]));
985
986         if (data.ioc_count > maxids)
987                 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
988
989         return 0;
990 }
991
992 int jt_ptl_mynid(int argc, char **argv)
993 {
994         struct libcfs_ioctl_data data;
995         lnet_nid_t               nid;
996         int rc;
997
998         if (argc != 2) {
999                 fprintf(stderr, "usage: %s NID\n", argv[0]);
1000                 return 0;
1001         }
1002
1003         nid = libcfs_str2nid(argv[1]);
1004         if (nid == LNET_NID_ANY) {
1005                 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1006                 return -1;
1007         }
1008
1009         LIBCFS_IOC_INIT(data);
1010         data.ioc_net = LNET_NIDNET(nid);
1011         data.ioc_nid = nid;
1012
1013         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1014         if (rc < 0)
1015                 fprintf(stderr, "setting my NID failed: %s\n",
1016                        strerror(errno));
1017         else
1018                 printf("registered my nid %s\n", libcfs_nid2str(nid));
1019
1020         return 0;
1021 }
1022
1023 int
1024 jt_ptl_fail_nid (int argc, char **argv)
1025 {
1026         int                      rc;
1027         lnet_nid_t               nid;
1028         int                      threshold;
1029         struct libcfs_ioctl_data data;
1030
1031         if (argc < 2 || argc > 3)
1032         {
1033                 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1034                 return (0);
1035         }
1036
1037         if (!libcfs_str2anynid(&nid, argv[1]))
1038         {
1039                 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1040                 return (-1);
1041         }
1042
1043         if (argc < 3) {
1044                 threshold = LNET_MD_THRESH_INF;
1045         } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1046                 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1047                 return (-1);
1048         }
1049
1050         LIBCFS_IOC_INIT (data);
1051         data.ioc_nid = nid;
1052         data.ioc_count = threshold;
1053
1054         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1055         if (rc < 0)
1056                 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1057                          strerror (errno));
1058         else
1059                 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1060
1061         return (0);
1062 }
1063
1064 int
1065 jt_ptl_add_route (int argc, char **argv)
1066 {
1067         struct lnet_ioctl_config_data data;
1068         lnet_nid_t               gateway_nid;
1069         __u32                    hops = LNET_UNDEFINED_HOPS;
1070         unsigned int             priority = 0;
1071         char                    *end;
1072         int                      rc;
1073
1074         if (argc < 2 || argc > 4) {
1075                 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1076                         argv[0]);
1077                 return -1;
1078         }
1079
1080         if (g_net_is_set(argv[0]) == 0)
1081                 return -1;
1082
1083         gateway_nid = libcfs_str2nid(argv[1]);
1084         if (gateway_nid == LNET_NID_ANY) {
1085                 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1086                 return -1;
1087         }
1088
1089         if (argc > 2) {
1090                 hops = strtol(argv[2], &end, 0);
1091                 if (hops == 0 || hops >= 256 ||
1092                     (end != NULL && *end != 0)) {
1093                         fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1094                                 argv[2]);
1095                         return -1;
1096                 }
1097                 if (argc == 4) {
1098                         priority = strtoul(argv[3], &end, 0);
1099                         if (end != NULL && *end != 0) {
1100                                 fprintf(stderr,
1101                                         "Can't parse priority \"%s\"\n",
1102                                         argv[3]);
1103                                 return -1;
1104                         }
1105                 }
1106         }
1107
1108         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1109         data.cfg_net = g_net;
1110         data.cfg_config_u.cfg_route.rtr_hop = hops;
1111         data.cfg_nid = gateway_nid;
1112         data.cfg_config_u.cfg_route.rtr_priority = priority;
1113
1114         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1115         if (rc != 0) {
1116                 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1117                         strerror(errno));
1118                 return -1;
1119         }
1120
1121         return 0;
1122 }
1123
1124 int
1125 jt_ptl_del_route (int argc, char **argv)
1126 {
1127         struct lnet_ioctl_config_data data;
1128         lnet_nid_t               nid;
1129         int                      rc;
1130
1131         if (argc != 2) {
1132                 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1133                 return 0;
1134         }
1135
1136         if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1137                 fprintf(stderr, "Can't parse gateway NID "
1138                         "\"%s\"\n", argv[1]);
1139                 return -1;
1140         }
1141
1142         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1143         data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1144         data.cfg_nid = nid;
1145
1146         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1147         if (rc != 0) {
1148                 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1149                         libcfs_nid2str(nid), strerror(errno));
1150                 return -1;
1151         }
1152
1153         return 0;
1154 }
1155
1156 int
1157 jt_ptl_notify_router (int argc, char **argv)
1158 {
1159         struct libcfs_ioctl_data data;
1160         int                      enable;
1161         lnet_nid_t               nid;
1162         int                      rc;
1163         struct timeval           now;
1164         time_t                   when;
1165
1166         if (argc < 3)
1167         {
1168                 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n", 
1169                          argv[0]);
1170                 return (0);
1171         }
1172
1173         nid = libcfs_str2nid(argv[1]);
1174         if (nid == LNET_NID_ANY) {
1175                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1176                 return (-1);
1177         }
1178
1179         if (lnet_parse_bool (&enable, argv[2]) != 0) {
1180                 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1181                 return (-1);
1182         }
1183
1184         gettimeofday(&now, NULL);
1185
1186         if (argc < 4) {
1187                 when = now.tv_sec;
1188         } else if (lnet_parse_time (&when, argv[3]) != 0) {
1189                 fprintf(stderr, "Can't parse time %s\n"
1190                         "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1191                         "or an absolute unix time in seconds\n", argv[3]);
1192                 return (-1);
1193         } else if (when > now.tv_sec) {
1194                 fprintf (stderr, "%s specifies a time in the future\n",
1195                          argv[3]);
1196                 return (-1);
1197         }
1198
1199         LIBCFS_IOC_INIT(data);
1200         data.ioc_nid = nid;
1201         data.ioc_flags = enable;
1202         /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1203         data.ioc_u64[0] = (__u64)when;
1204
1205         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1206         if (rc != 0) {
1207                 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1208                          libcfs_nid2str(nid), strerror (errno));
1209                 return (-1);
1210         }
1211
1212         return (0);
1213 }
1214
1215 int
1216 jt_ptl_print_routes (int argc, char **argv)
1217 {
1218         struct lnet_ioctl_config_data  data;
1219         int                       rc;
1220         int                       index;
1221         __u32                     net;
1222         lnet_nid_t                nid;
1223         unsigned int              hops;
1224         int                       alive;
1225         unsigned int              pri;
1226
1227         for (index = 0; ; index++) {
1228                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1229                 data.cfg_count = index;
1230
1231                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1232                 if (rc != 0)
1233                         break;
1234
1235                 net     = data.cfg_net;
1236                 hops    = data.cfg_config_u.cfg_route.rtr_hop;
1237                 nid     = data.cfg_nid;
1238                 alive   = data.cfg_config_u.cfg_route.rtr_flags;
1239                 pri     = data.cfg_config_u.cfg_route.rtr_priority;
1240
1241                 printf("net %18s hops %u gw %32s %s pri %u\n",
1242                        libcfs_net2str(net), hops,
1243                        libcfs_nid2str(nid), alive ? "up" : "down", pri);
1244         }
1245
1246         if (errno != ENOENT)
1247                 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1248                         strerror(errno));
1249
1250         return 0;
1251 }
1252
1253 static int
1254 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1255 {
1256         lnet_nid_t nid;
1257         __u32      net;
1258         int        rc = 0;
1259
1260         /* NB: can't support range ipaddress except * and *@net */
1261         if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1262                 net = libcfs_str2net(str + 2);
1263                 if (net == LNET_NIDNET(LNET_NID_ANY))
1264                         goto failed;
1265
1266                 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1267         } else {
1268                 rc = libcfs_str2anynid(&nid, str);
1269                 if (!rc)
1270                         goto failed;
1271         }
1272
1273         *nid_p = nid;
1274         return 0;
1275 failed:
1276         fprintf(stderr, "Invalid NID : %s\n", str);
1277         return -1;
1278 }
1279
1280 static int
1281 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1282 {
1283         if (!strcasecmp(msg_str, "put")) {
1284                 *mask_p |= LNET_PUT_BIT;
1285                 return 0;
1286
1287         } else if (!strcasecmp(msg_str, "ack")) {
1288                 *mask_p |= LNET_ACK_BIT;
1289                 return 0;
1290
1291         } else if (!strcasecmp(msg_str, "get")) {
1292                 *mask_p |= LNET_GET_BIT;
1293                 return 0;
1294
1295         } else if (!strcasecmp(msg_str, "reply")) {
1296                 *mask_p |= LNET_REPLY_BIT;
1297                 return 0;
1298         }
1299
1300         fprintf(stderr, "unknown message type %s\n", msg_str);
1301         return -1;
1302 }
1303
1304 static int
1305 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1306 {
1307         unsigned long rc = strtoul(optarg, NULL, 0);
1308
1309         if (rc >= 64) {
1310                 fprintf(stderr, "invalid portal: %lu\n", rc);
1311                 return -1;
1312         }
1313
1314         *mask_p |= (1ULL << rc);
1315         return 0;
1316 }
1317
1318 static int
1319 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1320 {
1321         struct libcfs_ioctl_data  data = {{0}};
1322         struct lnet_fault_attr    attr;
1323         char                     *optstr;
1324         int                       rc;
1325
1326         static struct option opts[] = {
1327                 {"source",      required_argument,      0,      's'},
1328                 {"dest",        required_argument,      0,      'd'},
1329                 {"rate",        required_argument,      0,      'r'},
1330                 {"interval",    required_argument,      0,      'i'},
1331                 {"latency",     required_argument,      0,      'l'},
1332                 {"portal",      required_argument,      0,      'p'},
1333                 {"message",     required_argument,      0,      'm'},
1334                 {0, 0, 0, 0}
1335         };
1336
1337         if (argc == 1) {
1338                 fprintf(stderr, "Failed, please provide source, destination "
1339                                 "and rate of rule\n");
1340                 return -1;
1341         }
1342
1343         optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1344         memset(&attr, 0, sizeof(attr));
1345         while (1) {
1346                 char c = getopt_long(argc, argv, optstr, opts, NULL);
1347
1348                 if (c == -1)
1349                         break;
1350
1351                 switch (c) {
1352                 case 's': /* source NID/NET */
1353                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1354                         if (rc != 0)
1355                                 goto getopt_failed;
1356                         break;
1357
1358                 case 'd': /* dest NID/NET */
1359                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1360                         if (rc != 0)
1361                                 goto getopt_failed;
1362                         break;
1363
1364                 case 'r': /* drop rate */
1365                         if (opc == LNET_CTL_DROP_ADD)
1366                                 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1367                         else
1368                                 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1369                         break;
1370
1371                 case 'i': /* time interval (# seconds) for message drop */
1372                         if (opc == LNET_CTL_DROP_ADD)
1373                                 attr.u.drop.da_interval = strtoul(optarg,
1374                                                                   NULL, 0);
1375                         else
1376                                 attr.u.delay.la_interval = strtoul(optarg,
1377                                                                    NULL, 0);
1378                         break;
1379
1380                 case 'l': /* seconds to wait before activating rule */
1381                         attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1382                         break;
1383
1384                 case 'p': /* portal to filter */
1385                         rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1386                         if (rc != 0)
1387                                 goto getopt_failed;
1388                         break;
1389
1390                 case 'm': /* message types to filter */
1391                         rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1392                         if (rc != 0)
1393                                 goto getopt_failed;
1394                         break;
1395
1396                 default:
1397                         fprintf(stderr, "error: %s: option '%s' "
1398                                 "unrecognized\n", argv[0], argv[optind - 1]);
1399                         goto getopt_failed;
1400                 }
1401         }
1402         optind = 1;
1403
1404         if (opc == LNET_CTL_DROP_ADD) {
1405                 /* NB: drop rate and interval are exclusive to each other */
1406                 if (!((attr.u.drop.da_rate == 0) ^
1407                       (attr.u.drop.da_interval == 0))) {
1408                         fprintf(stderr,
1409                                 "please provide either drop rate or interval "
1410                                 "but not both at the same time.\n");
1411                         return -1;
1412                 }
1413         } else if (opc == LNET_CTL_DELAY_ADD) {
1414                 if (!((attr.u.delay.la_rate == 0) ^
1415                       (attr.u.delay.la_interval == 0))) {
1416                         fprintf(stderr,
1417                                 "please provide either delay rate or interval "
1418                                 "but not both at the same time.\n");
1419                         return -1;
1420                 }
1421
1422                 if (attr.u.delay.la_latency == 0) {
1423                         fprintf(stderr, "latency cannot be zero\n");
1424                         return -1;
1425                 }
1426         }
1427
1428         if (attr.fa_src == 0 || attr.fa_dst == 0) {
1429                 fprintf(stderr, "Please provide both source and destination "
1430                                 "of %s rule\n", name);
1431                 return -1;
1432         }
1433
1434         data.ioc_flags = opc;
1435         data.ioc_inllen1 = sizeof(attr);
1436         data.ioc_inlbuf1 = (char *)&attr;
1437         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1438                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1439                 return -1;
1440         }
1441
1442         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1443         if (rc != 0) {
1444                 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1445                         name, libcfs_nid2str(attr.fa_src),
1446                         libcfs_nid2str(attr.fa_dst), strerror(errno));
1447                 return -1;
1448         }
1449
1450         printf("Added %s rule %s->%s (1/%d)\n",
1451                name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1452                opc == LNET_CTL_DROP_ADD ?
1453                attr.u.drop.da_rate : attr.u.delay.la_rate);
1454         return 0;
1455
1456 getopt_failed:
1457         optind = 1;
1458         return -1;
1459 }
1460
1461 int
1462 jt_ptl_drop_add(int argc, char **argv)
1463 {
1464         return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1465 }
1466
1467 int
1468 jt_ptl_delay_add(int argc, char **argv)
1469 {
1470         return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1471 }
1472
1473 static int
1474 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1475 {
1476         struct libcfs_ioctl_data data = {{0}};
1477         struct lnet_fault_attr   attr;
1478         bool                     all = false;
1479         int                      rc;
1480
1481         static struct option opts[] = {
1482                 {"source",      required_argument,      0,      's'},
1483                 {"dest",        required_argument,      0,      'd'},
1484                 {"all",         no_argument,            0,      'a'},
1485                 {0, 0, 0, 0}
1486         };
1487
1488         if (argc == 1) {
1489                 fprintf(stderr, "Failed, please provide source and "
1490                                 "destination of rule\n");
1491                 return -1;
1492         }
1493
1494         memset(&attr, 0, sizeof(attr));
1495         while (1) {
1496                 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1497
1498                 if (c == -1 || all)
1499                         break;
1500
1501                 switch (c) {
1502                 case 's':
1503                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1504                         if (rc != 0)
1505                                 goto getopt_failed;
1506                         break;
1507                 case 'd':
1508                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1509                         if (rc != 0)
1510                                 goto getopt_failed;
1511                         break;
1512                 case 'a':
1513                         attr.fa_src = attr.fa_dst = 0;
1514                         all = true;
1515                         break;
1516                 default:
1517                         fprintf(stderr, "error: %s: option '%s' "
1518                                 "unrecognized\n", argv[0], argv[optind - 1]);
1519                         goto getopt_failed;
1520                 }
1521         }
1522         optind = 1;
1523
1524         data.ioc_flags = opc;
1525         data.ioc_inllen1 = sizeof(attr);
1526         data.ioc_inlbuf1 = (char *)&attr;
1527         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1528                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1529                 return -1;
1530         }
1531
1532         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1533         if (rc != 0) {
1534                 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1535                         all ? "all" : libcfs_nid2str(attr.fa_src),
1536                         all ? "all" : libcfs_nid2str(attr.fa_dst),
1537                         strerror(errno));
1538                 return -1;
1539         }
1540
1541         libcfs_ioctl_unpack(&data, ioc_buf);
1542         printf("Removed %d %s rules\n", data.ioc_count, name);
1543         return 0;
1544
1545 getopt_failed:
1546         optind = 1;
1547         return -1;
1548 }
1549
1550 int
1551 jt_ptl_drop_del(int argc, char **argv)
1552 {
1553         return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1554 }
1555
1556 int
1557 jt_ptl_delay_del(int argc, char **argv)
1558 {
1559         return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1560 }
1561
1562 static int
1563 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1564 {
1565         struct libcfs_ioctl_data   data = {{0}};
1566         int                        rc;
1567
1568         LIBCFS_IOC_INIT(data);
1569         data.ioc_flags = opc;
1570
1571         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1572         if (rc != 0) {
1573                 fprintf(stderr, "failed to reset %s stats: %s\n",
1574                         name, strerror(errno));
1575                 return -1;
1576         }
1577         return 0;
1578 }
1579
1580 int
1581 jt_ptl_drop_reset(int argc, char **argv)
1582 {
1583         return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1584 }
1585
1586 int
1587 jt_ptl_delay_reset(int argc, char **argv)
1588 {
1589         return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1590                                       argc, argv);
1591 }
1592
1593 static int
1594 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1595 {
1596         struct libcfs_ioctl_data data = {{0}};
1597         struct lnet_fault_attr   attr;
1598         struct lnet_fault_stat   stat;
1599         int                      pos;
1600
1601         printf("LNet %s rules:\n", name);
1602         for (pos = 0;; pos++) {
1603                 int             rc;
1604
1605                 memset(&attr, 0, sizeof(attr));
1606                 memset(&stat, 0, sizeof(stat));
1607
1608                 data.ioc_count = pos;
1609                 data.ioc_flags = opc;
1610                 data.ioc_inllen1 = sizeof(attr);
1611                 data.ioc_inlbuf1 = (char *)&attr;
1612                 data.ioc_inllen2 = sizeof(stat);
1613                 data.ioc_inlbuf2 = (char *)&stat;
1614                 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1615                         fprintf(stderr, "libcfs_ioctl_pack failed\n");
1616                         return -1;
1617                 }
1618
1619                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1620                 if (rc != 0)
1621                         break;
1622
1623                 libcfs_ioctl_unpack(&data, ioc_buf);
1624
1625                 if (opc == LNET_CTL_DROP_LIST) {
1626                         printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1627                                "%ju/%ju, PUT %ju, ACK %ju, GET "
1628                                "%ju, REP %ju\n",
1629                                libcfs_nid2str(attr.fa_src),
1630                                libcfs_nid2str(attr.fa_dst),
1631                                attr.u.drop.da_rate, attr.u.drop.da_interval,
1632                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1633                                (uintmax_t)stat.u.drop.ds_dropped,
1634                                (uintmax_t)stat.fs_count,
1635                                (uintmax_t)stat.fs_put,
1636                                (uintmax_t)stat.fs_ack,
1637                                (uintmax_t)stat.fs_get,
1638                                (uintmax_t)stat.fs_reply);
1639
1640                 } else if (opc == LNET_CTL_DELAY_LIST) {
1641                         printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1642                                ", msg %x, %ju/%ju, PUT %ju"
1643                                ", ACK %ju, GET %ju, REP %ju\n",
1644                                libcfs_nid2str(attr.fa_src),
1645                                libcfs_nid2str(attr.fa_dst),
1646                                attr.u.delay.la_rate, attr.u.delay.la_interval,
1647                                attr.u.delay.la_latency,
1648                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1649                                (uintmax_t)stat.u.delay.ls_delayed,
1650                                (uintmax_t)stat.fs_count,
1651                                (uintmax_t)stat.fs_put,
1652                                (uintmax_t)stat.fs_ack,
1653                                (uintmax_t)stat.fs_get,
1654                                (uintmax_t)stat.fs_reply);
1655                 }
1656         }
1657         printf("found total %d\n", pos);
1658
1659         return 0;
1660 }
1661
1662 int
1663 jt_ptl_drop_list(int argc, char **argv)
1664 {
1665         return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1666 }
1667
1668 int
1669 jt_ptl_delay_list(int argc, char **argv)
1670 {
1671         return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1672 }
1673
1674 double
1675 get_cycles_per_usec ()
1676 {
1677         FILE      *f = fopen ("/proc/cpuinfo", "r");
1678         double     mhz;
1679         char      line[64];
1680
1681         if (f != NULL) {
1682                 while (fgets (line, sizeof (line), f) != NULL)
1683                         if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1684                                 fclose (f);
1685                                 return (mhz);
1686                         }
1687                 fclose (f);
1688         }
1689
1690         fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1691         return (1000.0);
1692 }
1693
1694 int jt_ptl_testprotocompat(int argc, char **argv)
1695 {
1696         struct libcfs_ioctl_data  data;
1697         int                       rc;
1698         int                       flags;
1699         char                     *end;
1700
1701         if (argc < 2)  {
1702                 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1703                 return 0;
1704         }
1705
1706         flags = strtol(argv[1], &end, 0);
1707         if (flags < 0 || *end != 0) {
1708                 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1709                 return -1;
1710         }
1711
1712         LIBCFS_IOC_INIT(data);
1713         data.ioc_flags = flags;
1714         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1715
1716         if (rc != 0) {
1717                 fprintf(stderr, "test proto compat %x failed: %s\n",
1718                         flags, strerror(errno));
1719                 return -1;
1720         }
1721
1722         printf("test proto compat %x OK\n", flags);
1723         return 0;
1724 }
1725
1726