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