Whamcloud - gitweb
LU-9100 lnet: lctl net down success when lnet not loaded
[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, struct lnet_process_id *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 == ENODEV) {
316                         printf("LNET is currently not loaded.");
317                         return 0;
318                 }
319
320                 if (errno == EBUSY)
321                         fprintf(stderr, "LNET busy\n");
322                 else
323                         fprintf(stderr, "LNET unconfigure error %d: %s\n",
324                                 errno, strerror(errno));
325                 return -1;
326         } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
327                 LIBCFS_IOC_INIT(data);
328                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
329
330                 if (rc == 0) {
331                         printf("LNET configured\n");
332                         return 0;
333                 }
334
335                 fprintf(stderr, "LNET configure error %d: %s\n",
336                         errno, strerror(errno));
337                 return -1;
338         }
339
340         net = libcfs_str2net(argv[1]);
341         if (net == LNET_NIDNET(LNET_NID_ANY)) {
342                 fprintf(stderr, "Can't parse net %s\n", argv[1]);
343                 return -1;
344         }
345
346         g_net_set = true;
347         g_net = net;
348         return 0;
349 }
350
351 int
352 jt_ptl_list_nids(int argc, char **argv)
353 {
354         struct libcfs_ioctl_data data;
355         int                      all = 0, return_nid = 0;
356         int                      count;
357         int                      rc;
358
359         all = (argc == 2) && (strcmp(argv[1], "all") == 0);
360         /* Hack to pass back value */
361         return_nid = (argc == 2) && (argv[1][0] == 1);
362
363         if ((argc > 2) && !(all || return_nid)) {
364                 fprintf(stderr, "usage: %s [all]\n", argv[0]);
365                 return 0;
366         }
367
368         for (count = 0;; count++) {
369                 LIBCFS_IOC_INIT (data);
370                 data.ioc_count = count;
371                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
372
373                 if (rc < 0) {
374                         if ((count > 0) && (errno == ENOENT))
375                                 /* We found them all */
376                                 break;
377                         fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
378                                 errno, strerror(errno));
379                         return -1;
380                 }
381
382                 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
383                         printf("%s\n", libcfs_nid2str(data.ioc_nid));
384                         if (return_nid) {
385                                 *(__u64 *)(argv[1]) = data.ioc_nid;
386                                 return_nid--;
387                         }
388                 }
389         }
390
391         return 0;
392 }
393
394 int
395 jt_ptl_which_nid (int argc, char **argv)
396 {
397         struct libcfs_ioctl_data data;
398         int          best_dist = 0;
399         int          best_order = 0;
400         lnet_nid_t   best_nid = LNET_NID_ANY;
401         int          dist;
402         int          order;
403         lnet_nid_t   nid;
404         char        *nidstr;
405         int          rc;
406         int          i;
407
408         if (argc < 2) {
409                 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
410                 return 0;
411         }
412
413         for (i = 1; i < argc; i++) {
414                 nidstr = argv[i];
415                 nid = libcfs_str2nid(nidstr);
416                 if (nid == LNET_NID_ANY) {
417                         fprintf(stderr, "Can't parse NID %s\n", nidstr);
418                         return -1;
419                 }
420
421                 LIBCFS_IOC_INIT(data);
422                 data.ioc_nid = nid;
423
424                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
425                 if (rc != 0) {
426                         fprintf(stderr, "Can't get distance to %s: %s\n",
427                                 nidstr, strerror(errno));
428                         return -1;
429                 }
430
431                 dist = data.ioc_u32[0];
432                 order = data.ioc_u32[1];
433
434                 if (dist < 0) {
435                         if (dist == -EHOSTUNREACH)
436                                 continue;
437
438                         fprintf(stderr, "Unexpected distance to %s: %d\n",
439                                 nidstr, dist);
440                         return -1;
441                 }
442
443                 if (best_nid == LNET_NID_ANY ||
444                     dist < best_dist ||
445                     (dist == best_dist && order < best_order)) {
446                         best_dist = dist;
447                         best_order = order;
448                         best_nid = nid;
449                 }
450         }
451
452         if (best_nid == LNET_NID_ANY) {
453                 fprintf(stderr, "No reachable NID\n");
454                 return -1;
455         }
456
457         printf("%s\n", libcfs_nid2str(best_nid));
458         return 0;
459 }
460
461 int
462 jt_ptl_print_interfaces (int argc, char **argv)
463 {
464         struct libcfs_ioctl_data data;
465         char                     buffer[3][HOST_NAME_MAX + 1];
466         int                      index;
467         int                      rc;
468
469         if (!g_net_is_compatible (argv[0], SOCKLND, 0))
470                 return -1;
471
472         for (index = 0;;index++) {
473                 LIBCFS_IOC_INIT(data);
474                 data.ioc_net   = g_net;
475                 data.ioc_count = index;
476
477                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
478                 if (rc != 0)
479                         break;
480
481                 printf ("%s: (%s/%s) npeer %d nroute %d\n",
482                         ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
483                                          sizeof(buffer[2]), 1),
484                         ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
485                                          sizeof(buffer[0]), 0),
486                         ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
487                                          sizeof(buffer[1]), 0),
488                         data.ioc_u32[2], data.ioc_u32[3]);
489         }
490
491         if (index == 0) {
492                 if (errno == ENOENT) {
493                         printf ("<no interfaces>\n");
494                 } else {
495                         fprintf(stderr, "Error getting interfaces: %s: "
496                                 "check dmesg.\n",
497                                 strerror(errno));
498                 }
499         }
500
501         return 0;
502 }
503
504 int
505 jt_ptl_add_interface (int argc, char **argv)
506 {
507         struct libcfs_ioctl_data data;
508         __u32                    ipaddr;
509         int                      rc;
510         __u32                    netmask = 0xffffff00;
511         int                      i;
512         int                      count;
513         char                    *end;
514
515         if (argc < 2 || argc > 3) {
516                 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
517                 return 0;
518         }
519
520         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
521                 return -1;
522
523         if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
524                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
525                 return -1;
526         }
527
528         if (argc > 2 ) {
529                 count = strtol(argv[2], &end, 0);
530                 if (count > 0 && count < 32 && *end == 0) {
531                         netmask = 0;
532                         for (i = count; i > 0; i--)
533                                 netmask = netmask|(1<<(32-i));
534                 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
535                         fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
536                         return -1;
537                 }
538         }
539
540         LIBCFS_IOC_INIT(data);
541         data.ioc_net    = g_net;
542         data.ioc_u32[0] = ipaddr;
543         data.ioc_u32[1] = netmask;
544
545         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
546         if (rc != 0) {
547                 fprintf (stderr, "failed to add interface: %s\n",
548                          strerror (errno));
549                 return -1;
550         }
551
552         return 0;
553 }
554
555 int
556 jt_ptl_del_interface (int argc, char **argv)
557 {
558         struct libcfs_ioctl_data data;
559         int                      rc;
560         __u32                    ipaddr = 0;
561
562         if (argc > 2) {
563                 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
564                 return 0;
565         }
566
567         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
568                 return -1;
569
570         if (argc == 2 &&
571             lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
572                 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
573                 return -1;
574         }
575
576         LIBCFS_IOC_INIT(data);
577         data.ioc_net    = g_net;
578         data.ioc_u32[0] = ipaddr;
579
580         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
581         if (rc != 0) {
582                 fprintf (stderr, "failed to delete interface: %s\n",
583                          strerror (errno));
584                 return -1;
585         }
586
587         return 0;
588 }
589
590 int
591 jt_ptl_print_peers (int argc, char **argv)
592 {
593         struct libcfs_ioctl_data data;
594         struct lnet_process_id        id;
595         char                     buffer[2][HOST_NAME_MAX + 1];
596         int                      index;
597         int                      rc;
598
599         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
600                                  PTL4LND, 0))
601                 return -1;
602
603         for (index = 0;;index++) {
604                 LIBCFS_IOC_INIT(data);
605                 data.ioc_net     = g_net;
606                 data.ioc_count   = index;
607
608                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
609                 if (rc != 0)
610                         break;
611
612                 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
613                         id.nid = data.ioc_nid;
614                         id.pid = data.ioc_u32[4];
615                         printf ("%-20s [%d]%s->%s:%d #%d\n",
616                                 libcfs_id2str(id),
617                                 data.ioc_count, /* persistence */
618                                 /* my ip */
619                                 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
620                                                  sizeof(buffer[0]), 1),
621                                 /* peer ip */
622                                 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
623                                                  sizeof(buffer[1]), 1),
624                                 data.ioc_u32[1], /* peer port */
625                                 data.ioc_u32[3]); /* conn_count */
626                 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
627                         int disconn = data.ioc_flags >> 16;
628                         char *state;
629
630                         if (disconn)
631                                 state = "D";
632                         else
633                                 state = data.ioc_flags & 0xffff ? "C" : "U";
634
635                         printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
636                                libcfs_nid2str(data.ioc_nid), /* peer nid */
637                                data.ioc_net, /* gemini device id */
638                                state, /* peer is Connecting, Up, or Down */
639                                data.ioc_count,   /* peer refcount */
640                                (uintmax_t)data.ioc_u64[0], /* peerstamp */
641                                data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
642                                /* fmaq, nfma, nrdma */
643                                data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
644                 } else {
645                         printf ("%-20s [%d]\n",
646                                 libcfs_nid2str(data.ioc_nid), data.ioc_count);
647                 }
648         }
649
650         if (index == 0) {
651                 if (errno == ENOENT) {
652                         printf ("<no peers>\n");
653                 } else {
654                         fprintf(stderr, "Error getting peer list: %s: "
655                                 "check dmesg.\n",
656                                 strerror(errno));
657                 }
658         }
659         return 0;
660 }
661
662 int jt_ptl_add_peer(int argc, char **argv)
663 {
664         struct libcfs_ioctl_data data;
665         lnet_nid_t nid;
666         __u32 ip = 0;
667         int port = 0;
668         int rc;
669
670         if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
671                 return -1;
672
673         if (argc != 4) {
674                 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
675                         argv[0]);
676                 return 0;
677         }
678
679         nid = libcfs_str2nid(argv[1]);
680         if (nid == LNET_NID_ANY) {
681                 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
682                 return -1;
683         }
684
685         if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
686                 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
687                 return -1;
688         }
689
690         if (lnet_parse_port (&port, argv[3]) != 0) {
691                 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
692                 return -1;
693         }
694
695         LIBCFS_IOC_INIT(data);
696         data.ioc_net    = g_net;
697         data.ioc_nid    = nid;
698         data.ioc_u32[0] = ip;
699         data.ioc_u32[1] = port;
700
701         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
702         if (rc != 0) {
703                 fprintf (stderr, "failed to add peer: %s\n",
704                          strerror (errno));
705                 return -1;
706         }
707
708         return 0;
709 }
710
711 int
712 jt_ptl_del_peer (int argc, char **argv)
713 {
714         struct libcfs_ioctl_data data;
715         lnet_nid_t               nid = LNET_NID_ANY;
716         lnet_pid_t               pid = LNET_PID_ANY;
717         __u32                    ip = 0;
718         int                      rc;
719
720         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
721                                  PTL4LND, 0))
722                 return -1;
723
724         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
725                 if (argc > 3) {
726                         fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
727                                  argv[0]);
728                         return 0;
729                 }
730         } else if (argc > 2) {
731                 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
732                 return 0;
733         }
734
735         if (argc > 1 &&
736             !libcfs_str2anynid(&nid, argv[1])) {
737                 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
738                 return -1;
739         }
740
741         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
742                 if (argc > 2 &&
743                     lnet_parse_ipaddr (&ip, argv[2]) != 0) {
744                         fprintf (stderr, "Can't parse ip addr: %s\n",
745                                  argv[2]);
746                         return -1;
747                 }
748         }
749
750         LIBCFS_IOC_INIT(data);
751         data.ioc_net    = g_net;
752         data.ioc_nid    = nid;
753         data.ioc_u32[0] = ip;
754         data.ioc_u32[1] = pid;
755
756         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
757         if (rc != 0) {
758                 fprintf (stderr, "failed to remove peer: %s\n",
759                          strerror (errno));
760                 return -1;
761         }
762
763         return 0;
764 }
765
766 int
767 jt_ptl_print_connections (int argc, char **argv)
768 {
769         struct libcfs_ioctl_data data;
770         struct lnet_process_id        id;
771         char                     buffer[2][HOST_NAME_MAX + 1];
772         int                      index;
773         int                      rc;
774
775         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
776                 return -1;
777
778         for (index = 0; ; index++) {
779                 LIBCFS_IOC_INIT(data);
780                 data.ioc_net     = g_net;
781                 data.ioc_count   = index;
782
783                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
784                 if (rc != 0)
785                         break;
786
787                 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
788                         id.nid = data.ioc_nid;
789                         id.pid = data.ioc_u32[6];
790                         printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
791                                 libcfs_id2str(id),
792                                 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
793                                 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
794                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
795                                 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
796                                 data.ioc_u32[4], /* scheduler */
797                                 /* local IP addr */
798                                 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
799                                                  sizeof(buffer[0]), 1),
800                                 /* remote IP addr */
801                                 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
802                                                  sizeof(buffer[1]), 1),
803                                 data.ioc_u32[1],         /* remote port */
804                                 data.ioc_count, /* tx buffer size */
805                                 data.ioc_u32[5], /* rx buffer size */
806                                 data.ioc_flags ? "nagle" : "nonagle");
807                 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
808                         printf ("%s mtu %d\n",
809                                 libcfs_nid2str(data.ioc_nid),
810                                 data.ioc_u32[0]); /* path MTU */
811                 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
812                         printf ("%-20s [%d]\n",
813                                 libcfs_nid2str(data.ioc_nid),
814                                 data.ioc_u32[0] /* device id */);
815                 } else {
816                         printf ("%s\n", libcfs_nid2str(data.ioc_nid));
817                 }
818         }
819
820         if (index == 0) {
821                 if (errno == ENOENT) {
822                         printf ("<no connections>\n");
823                 } else {
824                         fprintf(stderr, "Error getting connection list: %s: "
825                                 "check dmesg.\n",
826                                 strerror(errno));
827                 }
828         }
829         return 0;
830 }
831
832 int jt_ptl_disconnect(int argc, char **argv)
833 {
834         struct libcfs_ioctl_data data;
835         lnet_nid_t               nid = LNET_NID_ANY;
836         __u32                    ipaddr = 0;
837         int                      rc;
838
839         if (argc > 3) {
840                 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
841                 return 0;
842         }
843
844         if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
845                 return 0;
846
847         if (argc >= 2 &&
848             !libcfs_str2anynid(&nid, argv[1])) {
849                 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
850                 return -1;
851         }
852
853         if (g_net_is_compatible (NULL, SOCKLND, 0) &&
854             argc >= 3 &&
855             lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
856                 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
857                 return -1;
858         }
859
860         LIBCFS_IOC_INIT(data);
861         data.ioc_net     = g_net;
862         data.ioc_nid     = nid;
863         data.ioc_u32[0]  = ipaddr;
864
865         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
866         if (rc != 0) {
867                 fprintf(stderr, "failed to remove connection: %s\n",
868                         strerror(errno));
869                 return -1;
870         }
871
872         return 0;
873 }
874
875 int jt_ptl_push_connection (int argc, char **argv)
876 {
877         struct libcfs_ioctl_data data;
878         int                      rc;
879         lnet_nid_t               nid = LNET_NID_ANY;
880
881         if (argc > 2) {
882                 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
883                 return 0;
884         }
885
886         if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
887                 return -1;
888
889         if (argc > 1 &&
890             !libcfs_str2anynid(&nid, argv[1])) {
891                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
892                 return -1;
893         }
894
895         LIBCFS_IOC_INIT(data);
896         data.ioc_net     = g_net;
897         data.ioc_nid     = nid;
898
899         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
900         if (rc != 0) {
901                 fprintf(stderr, "failed to push connection: %s\n",
902                         strerror(errno));
903                 return -1;
904         }
905
906         return 0;
907 }
908
909 int jt_ptl_ping(int argc, char **argv)
910 {
911         int                      rc;
912         int                      timeout;
913         struct lnet_process_id id;
914         struct lnet_process_id ids[16];
915         int                      maxids = sizeof(ids)/sizeof(ids[0]);
916         struct libcfs_ioctl_data data;
917         char                    *sep;
918         int                      i;
919
920         if (argc < 2) {
921                 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
922                 return 0;
923         }
924
925         sep = strchr(argv[1], '-');
926         if (sep == NULL) {
927                 rc = lnet_parse_nid(argv[1], &id);
928                 if (rc != 0)
929                         return -1;
930         } else {
931                 char   *end;
932
933                 if (argv[1][0] == 'u' ||
934                     argv[1][0] == 'U')
935                         id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
936                 else
937                         id.pid = strtoul(argv[1], &end, 0);
938
939                 if (end != sep) { /* assuming '-' is part of hostname */
940                         rc = lnet_parse_nid(argv[1], &id);
941                         if (rc != 0)
942                                 return -1;
943                 } else {
944                         id.nid = libcfs_str2nid(sep + 1);
945
946                         if (id.nid == LNET_NID_ANY) {
947                                 fprintf(stderr,
948                                         "Can't parse process id \"%s\"\n",
949                                         argv[1]);
950                                 return -1;
951                         }
952                 }
953         }
954
955         if (argc > 2) {
956                 timeout = 1000 * atol(argv[2]);
957                 if (timeout > 120 * 1000) {
958                         fprintf(stderr, "Timeout %s is to large\n",
959                                 argv[2]);
960                         return -1;
961                 }
962         } else
963                 timeout = 1000;                 /* default 1 second timeout */
964
965         LIBCFS_IOC_INIT (data);
966         data.ioc_nid     = id.nid;
967         data.ioc_u32[0]  = id.pid;
968         data.ioc_u32[1]  = timeout;
969         data.ioc_plen1   = sizeof(ids);
970         data.ioc_pbuf1   = (char *)ids;
971
972         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
973         if (rc != 0) {
974                 fprintf(stderr, "failed to ping %s: %s\n",
975                         id.pid == LNET_PID_ANY ?
976                         libcfs_nid2str(id.nid) : libcfs_id2str(id),
977                         strerror(errno));
978                 return -1;
979         }
980
981         for (i = 0; i < data.ioc_count && i < maxids; i++)
982                 printf("%s\n", libcfs_id2str(ids[i]));
983
984         if (data.ioc_count > maxids)
985                 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
986
987         return 0;
988 }
989
990 int jt_ptl_mynid(int argc, char **argv)
991 {
992         struct libcfs_ioctl_data data;
993         lnet_nid_t               nid;
994         int rc;
995
996         if (argc != 2) {
997                 fprintf(stderr, "usage: %s NID\n", argv[0]);
998                 return 0;
999         }
1000
1001         nid = libcfs_str2nid(argv[1]);
1002         if (nid == LNET_NID_ANY) {
1003                 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1004                 return -1;
1005         }
1006
1007         LIBCFS_IOC_INIT(data);
1008         data.ioc_net = LNET_NIDNET(nid);
1009         data.ioc_nid = nid;
1010
1011         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1012         if (rc < 0)
1013                 fprintf(stderr, "setting my NID failed: %s\n",
1014                        strerror(errno));
1015         else
1016                 printf("registered my nid %s\n", libcfs_nid2str(nid));
1017
1018         return 0;
1019 }
1020
1021 int
1022 jt_ptl_fail_nid (int argc, char **argv)
1023 {
1024         int                      rc;
1025         lnet_nid_t               nid;
1026         int                      threshold;
1027         struct libcfs_ioctl_data data;
1028
1029         if (argc < 2 || argc > 3)
1030         {
1031                 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1032                 return (0);
1033         }
1034
1035         if (!libcfs_str2anynid(&nid, argv[1]))
1036         {
1037                 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1038                 return (-1);
1039         }
1040
1041         if (argc < 3) {
1042                 threshold = LNET_MD_THRESH_INF;
1043         } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1044                 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1045                 return (-1);
1046         }
1047
1048         LIBCFS_IOC_INIT (data);
1049         data.ioc_nid = nid;
1050         data.ioc_count = threshold;
1051
1052         rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1053         if (rc < 0)
1054                 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1055                          strerror (errno));
1056         else
1057                 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1058
1059         return (0);
1060 }
1061
1062 int
1063 jt_ptl_add_route (int argc, char **argv)
1064 {
1065         struct lnet_ioctl_config_data data;
1066         lnet_nid_t               gateway_nid;
1067         __u32                    hops = LNET_UNDEFINED_HOPS;
1068         unsigned int             priority = 0;
1069         char                    *end;
1070         int                      rc;
1071
1072         if (argc < 2 || argc > 4) {
1073                 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1074                         argv[0]);
1075                 return -1;
1076         }
1077
1078         if (g_net_is_set(argv[0]) == 0)
1079                 return -1;
1080
1081         gateway_nid = libcfs_str2nid(argv[1]);
1082         if (gateway_nid == LNET_NID_ANY) {
1083                 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1084                 return -1;
1085         }
1086
1087         if (argc > 2) {
1088                 hops = strtol(argv[2], &end, 0);
1089                 if (hops == 0 || hops >= 256 ||
1090                     (end != NULL && *end != 0)) {
1091                         fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1092                                 argv[2]);
1093                         return -1;
1094                 }
1095                 if (argc == 4) {
1096                         priority = strtoul(argv[3], &end, 0);
1097                         if (end != NULL && *end != 0) {
1098                                 fprintf(stderr,
1099                                         "Can't parse priority \"%s\"\n",
1100                                         argv[3]);
1101                                 return -1;
1102                         }
1103                 }
1104         }
1105
1106         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1107         data.cfg_net = g_net;
1108         data.cfg_config_u.cfg_route.rtr_hop = hops;
1109         data.cfg_nid = gateway_nid;
1110         data.cfg_config_u.cfg_route.rtr_priority = priority;
1111
1112         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1113         if (rc != 0) {
1114                 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1115                         strerror(errno));
1116                 return -1;
1117         }
1118
1119         return 0;
1120 }
1121
1122 int
1123 jt_ptl_del_route (int argc, char **argv)
1124 {
1125         struct lnet_ioctl_config_data data;
1126         lnet_nid_t               nid;
1127         int                      rc;
1128
1129         if (argc != 2) {
1130                 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1131                 return 0;
1132         }
1133
1134         if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1135                 fprintf(stderr, "Can't parse gateway NID "
1136                         "\"%s\"\n", argv[1]);
1137                 return -1;
1138         }
1139
1140         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1141         data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1142         data.cfg_nid = nid;
1143
1144         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1145         if (rc != 0) {
1146                 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1147                         libcfs_nid2str(nid), strerror(errno));
1148                 return -1;
1149         }
1150
1151         return 0;
1152 }
1153
1154 int
1155 jt_ptl_notify_router (int argc, char **argv)
1156 {
1157         struct libcfs_ioctl_data data;
1158         int                      enable;
1159         lnet_nid_t               nid;
1160         int                      rc;
1161         struct timeval           now;
1162         time_t                   when;
1163
1164         if (argc < 3)
1165         {
1166                 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n", 
1167                          argv[0]);
1168                 return (0);
1169         }
1170
1171         nid = libcfs_str2nid(argv[1]);
1172         if (nid == LNET_NID_ANY) {
1173                 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1174                 return (-1);
1175         }
1176
1177         if (lnet_parse_bool (&enable, argv[2]) != 0) {
1178                 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1179                 return (-1);
1180         }
1181
1182         gettimeofday(&now, NULL);
1183
1184         if (argc < 4) {
1185                 when = now.tv_sec;
1186         } else if (lnet_parse_time (&when, argv[3]) != 0) {
1187                 fprintf(stderr, "Can't parse time %s\n"
1188                         "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1189                         "or an absolute unix time in seconds\n", argv[3]);
1190                 return (-1);
1191         } else if (when > now.tv_sec) {
1192                 fprintf (stderr, "%s specifies a time in the future\n",
1193                          argv[3]);
1194                 return (-1);
1195         }
1196
1197         LIBCFS_IOC_INIT(data);
1198         data.ioc_nid = nid;
1199         data.ioc_flags = enable;
1200         /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1201         data.ioc_u64[0] = (__u64)when;
1202
1203         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1204         if (rc != 0) {
1205                 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1206                          libcfs_nid2str(nid), strerror (errno));
1207                 return (-1);
1208         }
1209
1210         return (0);
1211 }
1212
1213 int
1214 jt_ptl_print_routes (int argc, char **argv)
1215 {
1216         struct lnet_ioctl_config_data  data;
1217         int                       rc;
1218         int                       index;
1219         __u32                     net;
1220         lnet_nid_t                nid;
1221         unsigned int              hops;
1222         int                       alive;
1223         unsigned int              pri;
1224
1225         for (index = 0; ; index++) {
1226                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1227                 data.cfg_count = index;
1228
1229                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1230                 if (rc != 0)
1231                         break;
1232
1233                 net     = data.cfg_net;
1234                 hops    = data.cfg_config_u.cfg_route.rtr_hop;
1235                 nid     = data.cfg_nid;
1236                 alive   = data.cfg_config_u.cfg_route.rtr_flags;
1237                 pri     = data.cfg_config_u.cfg_route.rtr_priority;
1238
1239                 printf("net %18s hops %u gw %32s %s pri %u\n",
1240                        libcfs_net2str(net), hops,
1241                        libcfs_nid2str(nid), alive ? "up" : "down", pri);
1242         }
1243
1244         if (errno != ENOENT)
1245                 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1246                         strerror(errno));
1247
1248         return 0;
1249 }
1250
1251 static int
1252 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1253 {
1254         lnet_nid_t nid;
1255         __u32      net;
1256         int        rc = 0;
1257
1258         /* NB: can't support range ipaddress except * and *@net */
1259         if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1260                 net = libcfs_str2net(str + 2);
1261                 if (net == LNET_NIDNET(LNET_NID_ANY))
1262                         goto failed;
1263
1264                 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1265         } else {
1266                 rc = libcfs_str2anynid(&nid, str);
1267                 if (!rc)
1268                         goto failed;
1269         }
1270
1271         *nid_p = nid;
1272         return 0;
1273 failed:
1274         fprintf(stderr, "Invalid NID : %s\n", str);
1275         return -1;
1276 }
1277
1278 static int
1279 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1280 {
1281         if (!strcasecmp(msg_str, "put")) {
1282                 *mask_p |= LNET_PUT_BIT;
1283                 return 0;
1284
1285         } else if (!strcasecmp(msg_str, "ack")) {
1286                 *mask_p |= LNET_ACK_BIT;
1287                 return 0;
1288
1289         } else if (!strcasecmp(msg_str, "get")) {
1290                 *mask_p |= LNET_GET_BIT;
1291                 return 0;
1292
1293         } else if (!strcasecmp(msg_str, "reply")) {
1294                 *mask_p |= LNET_REPLY_BIT;
1295                 return 0;
1296         }
1297
1298         fprintf(stderr, "unknown message type %s\n", msg_str);
1299         return -1;
1300 }
1301
1302 static int
1303 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1304 {
1305         unsigned long rc = strtoul(optarg, NULL, 0);
1306
1307         if (rc >= 64) {
1308                 fprintf(stderr, "invalid portal: %lu\n", rc);
1309                 return -1;
1310         }
1311
1312         *mask_p |= (1ULL << rc);
1313         return 0;
1314 }
1315
1316 static int
1317 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1318 {
1319         struct libcfs_ioctl_data  data = { { 0 } };
1320         struct lnet_fault_attr    attr;
1321         char                     *optstr;
1322         int                       rc;
1323
1324         static const struct option opts[] = {
1325         { .name = "source",   .has_arg = required_argument, .val = 's' },
1326         { .name = "dest",     .has_arg = required_argument, .val = 'd' },
1327         { .name = "rate",     .has_arg = required_argument, .val = 'r' },
1328         { .name = "interval", .has_arg = required_argument, .val = 'i' },
1329         { .name = "latency",  .has_arg = required_argument, .val = 'l' },
1330         { .name = "portal",   .has_arg = required_argument, .val = 'p' },
1331         { .name = "message",  .has_arg = required_argument, .val = 'm' },
1332         { .name = NULL } };
1333
1334         if (argc == 1) {
1335                 fprintf(stderr, "Failed, please provide source, destination "
1336                                 "and rate of rule\n");
1337                 return -1;
1338         }
1339
1340         optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1341         memset(&attr, 0, sizeof(attr));
1342         while (1) {
1343                 char c = getopt_long(argc, argv, optstr, opts, NULL);
1344
1345                 if (c == -1)
1346                         break;
1347
1348                 switch (c) {
1349                 case 's': /* source NID/NET */
1350                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1351                         if (rc != 0)
1352                                 goto getopt_failed;
1353                         break;
1354
1355                 case 'd': /* dest NID/NET */
1356                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1357                         if (rc != 0)
1358                                 goto getopt_failed;
1359                         break;
1360
1361                 case 'r': /* drop rate */
1362                         if (opc == LNET_CTL_DROP_ADD)
1363                                 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1364                         else
1365                                 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1366                         break;
1367
1368                 case 'i': /* time interval (# seconds) for message drop */
1369                         if (opc == LNET_CTL_DROP_ADD)
1370                                 attr.u.drop.da_interval = strtoul(optarg,
1371                                                                   NULL, 0);
1372                         else
1373                                 attr.u.delay.la_interval = strtoul(optarg,
1374                                                                    NULL, 0);
1375                         break;
1376
1377                 case 'l': /* seconds to wait before activating rule */
1378                         attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1379                         break;
1380
1381                 case 'p': /* portal to filter */
1382                         rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1383                         if (rc != 0)
1384                                 goto getopt_failed;
1385                         break;
1386
1387                 case 'm': /* message types to filter */
1388                         rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1389                         if (rc != 0)
1390                                 goto getopt_failed;
1391                         break;
1392
1393                 default:
1394                         fprintf(stderr, "error: %s: option '%s' "
1395                                 "unrecognized\n", argv[0], argv[optind - 1]);
1396                         goto getopt_failed;
1397                 }
1398         }
1399         optind = 1;
1400
1401         if (opc == LNET_CTL_DROP_ADD) {
1402                 /* NB: drop rate and interval are exclusive to each other */
1403                 if (!((attr.u.drop.da_rate == 0) ^
1404                       (attr.u.drop.da_interval == 0))) {
1405                         fprintf(stderr,
1406                                 "please provide either drop rate or interval "
1407                                 "but not both at the same time.\n");
1408                         return -1;
1409                 }
1410         } else if (opc == LNET_CTL_DELAY_ADD) {
1411                 if (!((attr.u.delay.la_rate == 0) ^
1412                       (attr.u.delay.la_interval == 0))) {
1413                         fprintf(stderr,
1414                                 "please provide either delay rate or interval "
1415                                 "but not both at the same time.\n");
1416                         return -1;
1417                 }
1418
1419                 if (attr.u.delay.la_latency == 0) {
1420                         fprintf(stderr, "latency cannot be zero\n");
1421                         return -1;
1422                 }
1423         }
1424
1425         if (attr.fa_src == 0 || attr.fa_dst == 0) {
1426                 fprintf(stderr, "Please provide both source and destination "
1427                                 "of %s rule\n", name);
1428                 return -1;
1429         }
1430
1431         data.ioc_flags = opc;
1432         data.ioc_inllen1 = sizeof(attr);
1433         data.ioc_inlbuf1 = (char *)&attr;
1434         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1435                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1436                 return -1;
1437         }
1438
1439         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1440         if (rc != 0) {
1441                 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1442                         name, libcfs_nid2str(attr.fa_src),
1443                         libcfs_nid2str(attr.fa_dst), strerror(errno));
1444                 return -1;
1445         }
1446
1447         printf("Added %s rule %s->%s (1/%d)\n",
1448                name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1449                opc == LNET_CTL_DROP_ADD ?
1450                attr.u.drop.da_rate : attr.u.delay.la_rate);
1451         return 0;
1452
1453 getopt_failed:
1454         optind = 1;
1455         return -1;
1456 }
1457
1458 int
1459 jt_ptl_drop_add(int argc, char **argv)
1460 {
1461         return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1462 }
1463
1464 int
1465 jt_ptl_delay_add(int argc, char **argv)
1466 {
1467         return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1468 }
1469
1470 static int
1471 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1472 {
1473         struct libcfs_ioctl_data data = { { 0 } };
1474         struct lnet_fault_attr   attr;
1475         bool                     all = false;
1476         int                      rc;
1477
1478         static const struct option opts[] = {
1479                 { .name = "source", .has_arg = required_argument, .val = 's' },
1480                 { .name = "dest",   .has_arg = required_argument, .val = 'd' },
1481                 { .name = "all",    .has_arg = no_argument,       .val = 'a' },
1482                 { .name = NULL } };
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 %#jx, msg %x, "
1623                                "%ju/%ju, PUT %ju, ACK %ju, GET "
1624                                "%ju, REP %ju\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                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1629                                (uintmax_t)stat.u.drop.ds_dropped,
1630                                (uintmax_t)stat.fs_count,
1631                                (uintmax_t)stat.fs_put,
1632                                (uintmax_t)stat.fs_ack,
1633                                (uintmax_t)stat.fs_get,
1634                                (uintmax_t)stat.fs_reply);
1635
1636                 } else if (opc == LNET_CTL_DELAY_LIST) {
1637                         printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1638                                ", msg %x, %ju/%ju, PUT %ju"
1639                                ", ACK %ju, GET %ju, REP %ju\n",
1640                                libcfs_nid2str(attr.fa_src),
1641                                libcfs_nid2str(attr.fa_dst),
1642                                attr.u.delay.la_rate, attr.u.delay.la_interval,
1643                                attr.u.delay.la_latency,
1644                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1645                                (uintmax_t)stat.u.delay.ls_delayed,
1646                                (uintmax_t)stat.fs_count,
1647                                (uintmax_t)stat.fs_put,
1648                                (uintmax_t)stat.fs_ack,
1649                                (uintmax_t)stat.fs_get,
1650                                (uintmax_t)stat.fs_reply);
1651                 }
1652         }
1653         printf("found total %d\n", pos);
1654
1655         return 0;
1656 }
1657
1658 int
1659 jt_ptl_drop_list(int argc, char **argv)
1660 {
1661         return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1662 }
1663
1664 int
1665 jt_ptl_delay_list(int argc, char **argv)
1666 {
1667         return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1668 }
1669
1670 double
1671 get_cycles_per_usec ()
1672 {
1673         FILE      *f = fopen ("/proc/cpuinfo", "r");
1674         double     mhz;
1675         char      line[64];
1676
1677         if (f != NULL) {
1678                 while (fgets (line, sizeof (line), f) != NULL)
1679                         if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1680                                 fclose (f);
1681                                 return (mhz);
1682                         }
1683                 fclose (f);
1684         }
1685
1686         fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1687         return (1000.0);
1688 }
1689
1690 int jt_ptl_testprotocompat(int argc, char **argv)
1691 {
1692         struct libcfs_ioctl_data  data;
1693         int                       rc;
1694         int                       flags;
1695         char                     *end;
1696
1697         if (argc < 2)  {
1698                 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1699                 return 0;
1700         }
1701
1702         flags = strtol(argv[1], &end, 0);
1703         if (flags < 0 || *end != 0) {
1704                 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1705                 return -1;
1706         }
1707
1708         LIBCFS_IOC_INIT(data);
1709         data.ioc_flags = flags;
1710         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1711
1712         if (rc != 0) {
1713                 fprintf(stderr, "test proto compat %x failed: %s\n",
1714                         flags, strerror(errno));
1715                 return -1;
1716         }
1717
1718         printf("test proto compat %x OK\n", flags);
1719         return 0;
1720 }
1721
1722