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