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