Whamcloud - gitweb
LU-10003 lnet: use Netlink to support LNet ping commands
[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.whamcloud.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 <lnetconfig/liblnetconfig.h>
45 #include <lustre/lustreapi.h>
46
47 unsigned int libcfs_debug;
48 unsigned int libcfs_printk = D_CANTMASK;
49
50 static bool  g_net_interactive;
51 static bool  g_net_set;
52 static __u32 g_net;
53
54 #define IOC_BUF_SIZE    8192
55 static char local_buf[IOC_BUF_SIZE];
56 static char *ioc_buf = local_buf;
57
58 /* Convert a string boolean to an int; "enable" -> 1 */
59 int
60 lnet_parse_bool (int *b, char *str)
61 {
62         if (!strcasecmp(str, "no") ||
63             !strcasecmp(str, "n") ||
64             !strcasecmp(str, "off") ||
65             !strcasecmp(str, "down") ||
66             !strcasecmp(str, "disable")) {
67                 *b = 0;
68
69                 return 0;
70         }
71
72         if (!strcasecmp(str, "yes") ||
73             !strcasecmp(str, "y") ||
74             !strcasecmp(str, "on") ||
75             !strcasecmp(str, "up") ||
76             !strcasecmp(str, "enable")) {
77                 *b = 1;
78
79                 return 0;
80         }
81
82         return -1;
83 }
84
85 int
86 lnet_parse_port(int *port, char *str)
87 {
88         char *end;
89
90         *port = strtol(str, &end, 0);
91
92         if (*end == 0 &&                        /* parsed whole string */
93             *port > 0 && *port < 65536)         /* minimal sanity check */
94                 return 0;
95
96         return -1;
97 }
98
99 #ifdef HAVE_GETHOSTBYNAME
100 static struct hostent *
101 ptl_gethostbyname(char *hname)
102 {
103         struct hostent *he;
104
105         he = gethostbyname(hname);
106         if (!he) {
107                 switch (h_errno) {
108                 case HOST_NOT_FOUND:
109                 case NO_ADDRESS:
110                         fprintf(stderr, "Unable to resolve hostname: %s\n",
111                                 hname);
112                         break;
113                 default:
114                         fprintf(stderr, "gethostbyname error for %s: %s\n",
115                                 hname, strerror(h_errno));
116                         break;
117                 }
118                 return NULL;
119         }
120         return he;
121 }
122 #endif
123
124 int
125 lnet_parse_ipquad(__u32 *ipaddrp, char *str)
126 {
127         int a, b, c, 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                 *ipaddrp = (a << 24) | (b << 16) | (c << 8) | d;
133
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) {
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) {
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)
277                 fprintf(stderr, "Command %s not compatible with %s NAL\n",
278                         cmd, libcfs_lnd2str(LNET_NETTYP(g_net)));
279
280         return 0;
281 }
282
283 int ptl_initialize(int argc, char **argv)
284 {
285         if (argc > 1)
286                 g_net_interactive = true;
287
288         register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
289
290         return 0;
291 }
292
293 int jt_ptl_network(int argc, char **argv)
294 {
295         struct libcfs_ioctl_data data;
296         __u32 net = LNET_NET_ANY;
297         int rc;
298
299         if (argc != 2) {
300                 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
301                 return -1;
302         }
303
304         if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
305                 LIBCFS_IOC_INIT(data);
306                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
307
308                 if (rc == 0) {
309                         printf("LNET ready to unload\n");
310                         return 0;
311                 }
312
313                 if (errno == ENODEV) {
314                         printf("LNET is currently not loaded.");
315                         return 0;
316                 }
317
318                 if (errno == EBUSY)
319                         fprintf(stderr, "LNET busy\n");
320                 else
321                         fprintf(stderr, "LNET unconfigure error %d: %s\n",
322                                 errno, strerror(errno));
323                 return -1;
324         } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
325                 LIBCFS_IOC_INIT(data);
326                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
327
328                 if (rc == 0) {
329                         printf("LNET configured\n");
330                         return 0;
331                 }
332
333                 fprintf(stderr, "LNET configure error %d: %s\n",
334                         errno, strerror(errno));
335                 return -1;
336         }
337
338         net = libcfs_str2net(argv[1]);
339         if (net == LNET_NET_ANY) {
340                 fprintf(stderr, "Can't parse net %s\n", argv[1]);
341                 return -1;
342         }
343
344         g_net_set = true;
345         g_net = net;
346         return 0;
347 }
348
349 #define IOC_LIBCFS_GET_NI       _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
350
351 int
352 jt_ptl_list_nids(int argc, char **argv)
353 {
354         struct libcfs_ioctl_data data;
355         int all = 0, return_nid = 0;
356         yaml_emitter_t request;
357         yaml_parser_t reply;
358         yaml_event_t event;
359         struct nl_sock *sk;
360         bool done = false;
361         int rc = 0;
362         int count;
363
364         all = (argc == 2) && (strcmp(argv[1], "all") == 0);
365         /* Hack to pass back value */
366         return_nid = (argc == 2) && (argv[1][0] == 1);
367
368         if ((argc > 2) && !(all || return_nid)) {
369                 fprintf(stderr, "usage: %s [all]\n", argv[0]);
370                 return 0;
371         }
372
373         sk = nl_socket_alloc();
374         if (!sk)
375                 goto old_api;
376
377         /* Setup parser to receive Netlink packets */
378         rc = yaml_parser_initialize(&reply);
379         if (rc == 0) {
380                 yaml_parser_log_error(&reply, stderr, NULL);
381                 goto old_api;
382         }
383
384         rc = yaml_parser_set_input_netlink(&reply, sk, false);
385         if (rc == 0) {
386                 yaml_parser_log_error(&reply, stderr, NULL);
387                 yaml_parser_delete(&reply);
388                 goto old_api;
389         }
390
391         /* Create Netlink emitter to send request to kernel */
392         rc = yaml_emitter_initialize(&request);
393         if (rc == 0) {
394                 yaml_parser_log_error(&reply, stderr, NULL);
395                 yaml_parser_delete(&reply);
396                 goto old_api;
397         }
398
399         rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME, 1,
400                                              LNET_CMD_NETS, NLM_F_DUMP);
401         if (rc == 0) {
402                 yaml_emitter_log_error(&request, stderr);
403                 yaml_emitter_delete(&request);
404                 yaml_parser_delete(&reply);
405                 goto old_api;
406         }
407
408         yaml_emitter_open(&request);
409         yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
410         rc = yaml_emitter_emit(&request, &event);
411         if (rc == 0)
412                 goto emitter_error;
413
414         yaml_mapping_start_event_initialize(&event, NULL,
415                                             (yaml_char_t *)YAML_MAP_TAG,
416                                             1, YAML_ANY_MAPPING_STYLE);
417         rc = yaml_emitter_emit(&request, &event);
418         if (rc == 0)
419                 goto emitter_error;
420
421         yaml_scalar_event_initialize(&event, NULL,
422                                      (yaml_char_t *)YAML_STR_TAG,
423                                      (yaml_char_t *)"net",
424                                      strlen("net"), 1, 0,
425                                      YAML_PLAIN_SCALAR_STYLE);
426         rc = yaml_emitter_emit(&request, &event);
427         if (rc == 0)
428                 goto emitter_error;
429
430         /* no net_id */
431         if (!g_net_set || g_net == LNET_NET_ANY) {
432                 yaml_scalar_event_initialize(&event, NULL,
433                                              (yaml_char_t *)YAML_STR_TAG,
434                                              (yaml_char_t *)"",
435                                              strlen(""), 1, 0,
436                                              YAML_PLAIN_SCALAR_STYLE);
437                 rc = yaml_emitter_emit(&request, &event);
438                 if (rc == 0)
439                         goto emitter_error;
440         } else {
441                 char *net_id = libcfs_net2str(g_net);
442
443                 yaml_sequence_start_event_initialize(&event, NULL,
444                                                      (yaml_char_t *)YAML_SEQ_TAG,
445                                                      1, YAML_ANY_SEQUENCE_STYLE);
446                 rc = yaml_emitter_emit(&request, &event);
447                 if (rc == 0)
448                         goto emitter_error;
449
450                 yaml_mapping_start_event_initialize(&event, NULL,
451                                                     (yaml_char_t *)YAML_MAP_TAG,
452                                                     1, YAML_ANY_MAPPING_STYLE);
453                 rc = yaml_emitter_emit(&request, &event);
454                 if (rc == 0)
455                         goto emitter_error;
456
457                 yaml_scalar_event_initialize(&event, NULL,
458                                              (yaml_char_t *)YAML_STR_TAG,
459                                              (yaml_char_t *)"net type",
460                                              strlen("net type"),
461                                              1, 0, YAML_PLAIN_SCALAR_STYLE);
462                 rc = yaml_emitter_emit(&request, &event);
463                 if (rc == 0)
464                         goto emitter_error;
465
466                 yaml_scalar_event_initialize(&event, NULL,
467                                              (yaml_char_t *)YAML_STR_TAG,
468                                              (yaml_char_t *)net_id,
469                                              strlen(net_id), 1, 0,
470                                              YAML_PLAIN_SCALAR_STYLE);
471                 rc = yaml_emitter_emit(&request, &event);
472                 if (rc == 0)
473                         goto emitter_error;
474
475                 yaml_mapping_end_event_initialize(&event);
476                 rc = yaml_emitter_emit(&request, &event);
477                 if (rc == 0)
478                         goto emitter_error;
479
480                 yaml_sequence_end_event_initialize(&event);
481                 rc = yaml_emitter_emit(&request, &event);
482                 if (rc == 0)
483                         goto emitter_error;
484         }
485         yaml_mapping_end_event_initialize(&event);
486         rc = yaml_emitter_emit(&request, &event);
487         if (rc == 0)
488                 goto emitter_error;
489
490         yaml_document_end_event_initialize(&event, 0);
491         rc = yaml_emitter_emit(&request, &event);
492         if (rc == 0)
493                 goto emitter_error;
494
495         rc = yaml_emitter_close(&request);
496 emitter_error:
497         if (rc == 0) {
498                 yaml_emitter_log_error(&request, stderr);
499                 rc = -EINVAL;
500         }
501         yaml_emitter_delete(&request);
502
503         while (!done) {
504                 rc = yaml_parser_parse(&reply, &event);
505                 if (rc == 0)
506                         break;
507
508                 if (event.type == YAML_SCALAR_EVENT &&
509                     strcmp((char *)event.data.scalar.value, "nid") == 0) {
510                         char *tmp;
511
512                         yaml_event_delete(&event);
513                         rc = yaml_parser_parse(&reply, &event);
514                         if (rc == 0) {
515                                 yaml_event_delete(&event);
516                                 break;
517                         }
518
519                         tmp = (char *)event.data.scalar.value;
520                         if (all || strcmp(tmp, "0@lo") != 0) {
521                                 printf("%s\n", tmp);
522                                 if (return_nid) {
523                                         *(__u64 *)(argv[1]) = libcfs_str2nid(tmp);
524                                         return_nid--;
525                                 }
526                         }
527                 }
528                 done = (event.type == YAML_STREAM_END_EVENT);
529                 yaml_event_delete(&event);
530         }
531
532         if (rc == 0)
533                 yaml_parser_log_error(&reply, stderr, NULL);
534         yaml_parser_delete(&reply);
535 old_api:
536         if (sk)
537                 nl_socket_free(sk);
538         if (rc == 1)
539                 return 0;
540
541         for (count = 0;; count++) {
542                 LIBCFS_IOC_INIT(data);
543                 data.ioc_count = count;
544                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
545
546                 if (rc < 0) {
547                         if ((count > 0) && (errno == ENOENT))
548                                 /* We found them all */
549                                 break;
550                         fprintf(stderr, "IOC_LIBCFS_GET_NI error %d: %s\n",
551                                 errno, strerror(errno));
552                         return -1;
553                 }
554
555                 if (all || (data.ioc_nid != LNET_NID_LO_0)) {
556                         printf("%s\n", libcfs_nid2str(data.ioc_nid));
557                         if (return_nid) {
558                                 *(__u64 *)(argv[1]) = data.ioc_nid;
559                                 return_nid--;
560                         }
561                 }
562         }
563
564         return 0;
565 }
566
567 int
568 jt_ptl_which_nid(int argc, char **argv)
569 {
570         struct libcfs_ioctl_data data;
571         int best_dist = 0;
572         int best_order = 0;
573         lnet_nid_t   best_nid = LNET_NID_ANY;
574         int dist;
575         int order;
576         lnet_nid_t nid;
577         char *nidstr;
578         int rc;
579         int i;
580
581         if (argc < 2) {
582                 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
583                 return 0;
584         }
585
586         for (i = 1; i < argc; i++) {
587                 nidstr = argv[i];
588                 nid = libcfs_str2nid(nidstr);
589                 if (nid == LNET_NID_ANY) {
590                         fprintf(stderr, "Can't parse NID %s\n", nidstr);
591                         return -1;
592                 }
593
594                 LIBCFS_IOC_INIT(data);
595                 data.ioc_nid = nid;
596
597                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
598                 if (rc != 0) {
599                         fprintf(stderr, "Can't get distance to %s: %s\n",
600                                 nidstr, strerror(errno));
601                         return -1;
602                 }
603
604                 dist = data.ioc_u32[0];
605                 order = data.ioc_u32[1];
606
607                 if (dist < 0) {
608                         if (dist == -EHOSTUNREACH)
609                                 continue;
610
611                         fprintf(stderr, "Unexpected distance to %s: %d\n",
612                                 nidstr, dist);
613                         return -1;
614                 }
615
616                 if (best_nid == LNET_NID_ANY ||
617                     dist < best_dist ||
618                     (dist == best_dist && order < best_order)) {
619                         best_dist = dist;
620                         best_order = order;
621                         best_nid = nid;
622                 }
623         }
624
625         if (best_nid == LNET_NID_ANY) {
626                 fprintf(stderr, "No reachable NID\n");
627                 return -1;
628         }
629
630         printf("%s\n", libcfs_nid2str(best_nid));
631         return 0;
632 }
633
634 int
635 jt_ptl_print_interfaces(int argc, char **argv)
636 {
637         struct libcfs_ioctl_data data;
638         char buffer[3][HOST_NAME_MAX + 1];
639         int index;
640         int rc;
641
642         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
643                 return -1;
644
645         for (index = 0; ; index++) {
646                 LIBCFS_IOC_INIT(data);
647                 data.ioc_net   = g_net;
648                 data.ioc_count = index;
649
650                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
651                 if (rc != 0)
652                         break;
653
654                 printf("%s: (%s/%s) npeer %d nroute %d\n",
655                        ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
656                                         sizeof(buffer[2]), 1),
657                        ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
658                                         sizeof(buffer[0]), 0),
659                        ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
660                                         sizeof(buffer[1]), 0),
661                        data.ioc_u32[2], data.ioc_u32[3]);
662         }
663
664         if (index == 0) {
665                 if (errno == ENOENT) {
666                         printf("<no interfaces>\n");
667                 } else {
668                         fprintf(stderr,
669                                 "Error getting interfaces: %s: check dmesg.\n",
670                                 strerror(errno));
671                 }
672         }
673
674         return 0;
675 }
676
677 int
678 jt_ptl_add_interface(int argc, char **argv)
679 {
680         struct libcfs_ioctl_data data;
681         __u32 ipaddr;
682         int rc;
683         __u32 netmask = 0xffffff00;
684         int i;
685         int count;
686         char *end;
687
688         if (argc < 2 || argc > 3) {
689                 fprintf(stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
690                 return 0;
691         }
692
693         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
694                 return -1;
695
696         if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
697                 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
698                 return -1;
699         }
700
701         if (argc > 2) {
702                 count = strtol(argv[2], &end, 0);
703                 if (count > 0 && count < 32 && *end == 0) {
704                         netmask = 0;
705                         for (i = count; i > 0; i--)
706                                 netmask = netmask | (1 << (32 - i));
707                 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
708                         fprintf(stderr, "Can't parse netmask: %s\n", argv[2]);
709                         return -1;
710                 }
711         }
712
713         LIBCFS_IOC_INIT(data);
714         data.ioc_net    = g_net;
715         data.ioc_u32[0] = ipaddr;
716         data.ioc_u32[1] = netmask;
717
718         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
719         if (rc != 0) {
720                 fprintf(stderr, "failed to add interface: %s\n",
721                         strerror(errno));
722                 return -1;
723         }
724
725         return 0;
726 }
727
728 int
729 jt_ptl_del_interface(int argc, char **argv)
730 {
731         struct libcfs_ioctl_data data;
732         int rc;
733         __u32 ipaddr = 0;
734
735         if (argc > 2) {
736                 fprintf(stderr, "usage: %s [ipaddr]\n", argv[0]);
737                 return 0;
738         }
739
740         if (!g_net_is_compatible(argv[0], SOCKLND, 0))
741                 return -1;
742
743         if (argc == 2 &&
744             lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
745                 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
746                 return -1;
747         }
748
749         LIBCFS_IOC_INIT(data);
750         data.ioc_net    = g_net;
751         data.ioc_u32[0] = ipaddr;
752
753         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
754         if (rc != 0) {
755                 fprintf(stderr, "failed to delete interface: %s\n",
756                         strerror(errno));
757                 return -1;
758         }
759
760         return 0;
761 }
762
763 int
764 jt_ptl_print_peers(int argc, char **argv)
765 {
766         struct libcfs_ioctl_data data;
767         struct lnet_process_id id;
768         char buffer[2][HOST_NAME_MAX + 1];
769         int index;
770         int rc;
771
772         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
773                                  PTL4LND, 0))
774                 return -1;
775
776         for (index = 0; ; index++) {
777                 LIBCFS_IOC_INIT(data);
778                 data.ioc_net     = g_net;
779                 data.ioc_count   = index;
780
781                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
782                 if (rc != 0)
783                         break;
784
785                 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
786                         id.nid = data.ioc_nid;
787                         id.pid = data.ioc_u32[4];
788                         printf("%-20s [%d]%s->%s:%d #%d\n",
789                                libcfs_id2str(id),
790                                data.ioc_count, /* persistence */
791                                /* my ip */
792                                ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
793                                                 sizeof(buffer[0]), 1),
794                                /* peer ip */
795                                ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
796                                                 sizeof(buffer[1]), 1),
797                                data.ioc_u32[1], /* peer port */
798                                data.ioc_u32[3]); /* conn_count */
799                 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
800                         int disconn = data.ioc_flags >> 16;
801                         char *state;
802
803                         if (disconn)
804                                 state = "D";
805                         else
806                                 state = data.ioc_flags & 0xffff ? "C" : "U";
807
808                         printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
809                                libcfs_nid2str(data.ioc_nid), /* peer nid */
810                                data.ioc_net, /* gemini device id */
811                                state, /* peer is Connecting, Up, or Down */
812                                data.ioc_count,   /* peer refcount */
813                                (uintmax_t)data.ioc_u64[0], /* peerstamp */
814                                data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
815                                /* fmaq, nfma, nrdma */
816                                data.ioc_u32[0], data.ioc_u32[1],
817                                data.ioc_u32[4]);
818                 } else {
819                         printf("%-20s [%d]\n",
820                                libcfs_nid2str(data.ioc_nid), data.ioc_count);
821                 }
822         }
823
824         if (index == 0) {
825                 if (errno == ENOENT) {
826                         printf("<no peers>\n");
827                 } else {
828                         fprintf(stderr,
829                                 "Error getting peer list: %s: check dmesg.\n",
830                                 strerror(errno));
831                 }
832         }
833         return 0;
834 }
835
836 int jt_ptl_add_peer(int argc, char **argv)
837 {
838         struct libcfs_ioctl_data data;
839         lnet_nid_t nid;
840         __u32 ip = 0;
841         int port = 0;
842         int rc;
843
844         if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
845                 return -1;
846
847         if (argc != 4) {
848                 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
849                         argv[0]);
850                 return 0;
851         }
852
853         nid = libcfs_str2nid(argv[1]);
854         if (nid == LNET_NID_ANY) {
855                 fprintf(stderr, "Can't parse NID: %s\n", argv[1]);
856                 return -1;
857         }
858
859         if (lnet_parse_ipaddr(&ip, argv[2]) != 0) {
860                 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
861                 return -1;
862         }
863
864         if (lnet_parse_port(&port, argv[3]) != 0) {
865                 fprintf(stderr, "Can't parse port: %s\n", argv[3]);
866                 return -1;
867         }
868
869         LIBCFS_IOC_INIT(data);
870         data.ioc_net    = g_net;
871         data.ioc_nid    = nid;
872         data.ioc_u32[0] = ip;
873         data.ioc_u32[1] = port;
874
875         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
876         if (rc != 0) {
877                 fprintf(stderr, "failed to add peer: %s\n",
878                         strerror(errno));
879                 return -1;
880         }
881
882         return 0;
883 }
884
885 int
886 jt_ptl_del_peer(int argc, char **argv)
887 {
888         struct libcfs_ioctl_data data;
889         lnet_nid_t nid = LNET_NID_ANY;
890         lnet_pid_t pid = LNET_PID_ANY;
891         __u32 ip = 0;
892         int rc;
893
894         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
895                                  PTL4LND, 0))
896                 return -1;
897
898         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
899                 if (argc > 3) {
900                         fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
901                         return 0;
902                 }
903         } else if (argc > 2) {
904                 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
905                 return 0;
906         }
907
908         if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
909                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
910                 return -1;
911         }
912
913         if (g_net_is_compatible(NULL, SOCKLND, 0)) {
914                 if (argc > 2 && lnet_parse_ipaddr(&ip, argv[2]) != 0) {
915                         fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
916                         return -1;
917                 }
918         }
919
920         LIBCFS_IOC_INIT(data);
921         data.ioc_net    = g_net;
922         data.ioc_nid    = nid;
923         data.ioc_u32[0] = ip;
924         data.ioc_u32[1] = pid;
925
926         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
927         if (rc != 0) {
928                 fprintf(stderr, "failed to remove peer: %s\n", strerror(errno));
929                 return -1;
930         }
931
932         return 0;
933 }
934
935 int
936 jt_ptl_print_connections(int argc, char **argv)
937 {
938         struct libcfs_ioctl_data data;
939         struct lnet_process_id id;
940         char buffer[2][HOST_NAME_MAX + 1];
941         int index;
942         int rc;
943
944         if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
945                 return -1;
946
947         for (index = 0; ; index++) {
948                 LIBCFS_IOC_INIT(data);
949                 data.ioc_net     = g_net;
950                 data.ioc_count   = index;
951
952                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
953                 if (rc != 0)
954                         break;
955
956                 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
957                         id.nid = data.ioc_nid;
958                         id.pid = data.ioc_u32[6];
959                         printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
960                                libcfs_id2str(id),
961                                (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
962                                (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
963                                (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
964                          (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
965                                data.ioc_u32[4], /* scheduler */
966                                /* local IP addr */
967                                ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
968                                                 sizeof(buffer[0]), 1),
969                                /* remote IP addr */
970                                ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
971                                                 sizeof(buffer[1]), 1),
972                                data.ioc_u32[1],         /* remote port */
973                                data.ioc_count, /* tx buffer size */
974                                data.ioc_u32[5], /* rx buffer size */
975                                data.ioc_flags ? "nagle" : "nonagle");
976                 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
977                         printf("%s mtu %d\n",
978                                libcfs_nid2str(data.ioc_nid),
979                                data.ioc_u32[0]); /* path MTU */
980                 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
981                         printf("%-20s [%d]\n",
982                                libcfs_nid2str(data.ioc_nid),
983                                data.ioc_u32[0] /* device id */);
984                 } else {
985                         printf("%s\n", libcfs_nid2str(data.ioc_nid));
986                 }
987         }
988
989         if (index == 0) {
990                 if (errno == ENOENT) {
991                         printf("<no connections>\n");
992                 } else {
993                         fprintf(stderr,
994                                 "Error getting connection list: %s: check dmesg.\n",
995                                 strerror(errno));
996                 }
997         }
998         return 0;
999 }
1000
1001 int jt_ptl_disconnect(int argc, char **argv)
1002 {
1003         struct libcfs_ioctl_data data;
1004         lnet_nid_t nid = LNET_NID_ANY;
1005         __u32 ipaddr = 0;
1006         int rc;
1007
1008         if (argc > 3) {
1009                 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1010                 return 0;
1011         }
1012
1013         if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
1014                 return 0;
1015
1016         if (argc >= 2 && !libcfs_str2anynid(&nid, argv[1])) {
1017                 fprintf(stderr, "Can't parse nid %s\n", argv[1]);
1018                 return -1;
1019         }
1020
1021         if (g_net_is_compatible(NULL, SOCKLND, 0) && argc >= 3 &&
1022             lnet_parse_ipaddr(&ipaddr, argv[2]) != 0) {
1023                 fprintf(stderr, "Can't parse ip addr %s\n", argv[2]);
1024                 return -1;
1025         }
1026
1027         LIBCFS_IOC_INIT(data);
1028         data.ioc_net     = g_net;
1029         data.ioc_nid     = nid;
1030         data.ioc_u32[0]  = ipaddr;
1031
1032         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
1033         if (rc != 0) {
1034                 fprintf(stderr, "failed to remove connection: %s\n",
1035                         strerror(errno));
1036                 return -1;
1037         }
1038
1039         return 0;
1040 }
1041
1042 int jt_ptl_push_connection(int argc, char **argv)
1043 {
1044         struct libcfs_ioctl_data data;
1045         int rc;
1046         lnet_nid_t nid = LNET_NID_ANY;
1047
1048         if (argc > 2) {
1049                 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
1050                 return 0;
1051         }
1052
1053         if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
1054                 return -1;
1055
1056         if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
1057                 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1058                 return -1;
1059         }
1060
1061         LIBCFS_IOC_INIT(data);
1062         data.ioc_net     = g_net;
1063         data.ioc_nid     = nid;
1064
1065         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
1066         if (rc != 0) {
1067                 fprintf(stderr, "failed to push connection: %s\n",
1068                         strerror(errno));
1069                 return -1;
1070         }
1071
1072         return 0;
1073 }
1074
1075 #define IOC_LIBCFS_PING _IOWR('e', 61, IOCTL_LIBCFS_TYPE)
1076
1077 int jt_ptl_ping(int argc, char **argv)
1078 {
1079         bool done = false, print = true;
1080         int rc;
1081         int timeout;
1082         struct lnet_process_id id;
1083         struct lnet_process_id ids[16];
1084         int maxids = sizeof(ids) / sizeof(ids[0]);
1085         struct libcfs_ioctl_data data;
1086         yaml_emitter_t request;
1087         yaml_parser_t reply;
1088         yaml_event_t event;
1089         struct nl_sock *sk;
1090         char *sep;
1091         int i;
1092
1093         if (argc < 2) {
1094                 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
1095                 return 0;
1096         }
1097
1098         sep = strchr(argv[1], '-');
1099         if (!sep) {
1100                 rc = lnet_parse_nid(argv[1], &id);
1101                 if (rc != 0)
1102                         return -1;
1103         } else {
1104                 char   *end;
1105
1106                 if (argv[1][0] == 'u' || argv[1][0] == 'U')
1107                         id.pid = strtoul(&argv[1][1], &end, 0) |
1108                                 LNET_PID_USERFLAG;
1109                 else
1110                         id.pid = strtoul(argv[1], &end, 0);
1111
1112                 if (end != sep) { /* assuming '-' is part of hostname */
1113                         rc = lnet_parse_nid(argv[1], &id);
1114                         if (rc != 0)
1115                                 return -1;
1116                 } else {
1117                         id.nid = libcfs_str2nid(sep + 1);
1118
1119                         if (id.nid == LNET_NID_ANY) {
1120                                 fprintf(stderr,
1121                                         "Can't parse process id \"%s\"\n",
1122                                         argv[1]);
1123                                 return -1;
1124                         }
1125                 }
1126         }
1127
1128         if (argc > 2) {
1129                 timeout = 1000 * atol(argv[2]);
1130                 if (timeout > 120 * 1000) {
1131                         fprintf(stderr, "Timeout %s is to large\n",
1132                                 argv[2]);
1133                         return -1;
1134                 }
1135         } else {
1136                 timeout = 1000; /* default 1 second timeout */
1137         }
1138
1139         /* Create Netlink emitter to send request to kernel */
1140         sk = nl_socket_alloc();
1141         if (!sk)
1142                 goto old_api;
1143
1144         /* Setup parser to recieve Netlink packets */
1145         rc = yaml_parser_initialize(&reply);
1146         if (rc == 0) {
1147                 nl_socket_free(sk);
1148                 goto old_api;
1149         }
1150
1151         rc = yaml_parser_set_input_netlink(&reply, sk, false);
1152         if (rc == 0)
1153                 goto free_reply;
1154
1155         /* Create Netlink emitter to send request to kernel */
1156         rc = yaml_emitter_initialize(&request);
1157         if (rc == 0)
1158                 goto free_reply;
1159
1160         rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
1161                                              LNET_GENL_VERSION, LNET_CMD_PING,
1162                                              NLM_F_DUMP);
1163         if (rc == 0)
1164                 goto emitter_error;
1165
1166         yaml_emitter_open(&request);
1167         yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1168         rc = yaml_emitter_emit(&request, &event);
1169         if (rc == 0)
1170                 goto emitter_error;
1171
1172         yaml_mapping_start_event_initialize(&event, NULL,
1173                                             (yaml_char_t *)YAML_MAP_TAG,
1174                                             1, YAML_ANY_MAPPING_STYLE);
1175         rc = yaml_emitter_emit(&request, &event);
1176         if (rc == 0)
1177                 goto emitter_error;
1178
1179         yaml_scalar_event_initialize(&event, NULL,
1180                                      (yaml_char_t *)YAML_STR_TAG,
1181                                      (yaml_char_t *)"ping",
1182                                      strlen("ping"), 1, 0,
1183                                      YAML_PLAIN_SCALAR_STYLE);
1184         rc = yaml_emitter_emit(&request, &event);
1185         if (rc == 0)
1186                 goto emitter_error;
1187
1188         yaml_mapping_start_event_initialize(&event, NULL,
1189                                             (yaml_char_t *)YAML_MAP_TAG,
1190                                             1, YAML_ANY_MAPPING_STYLE);
1191         rc = yaml_emitter_emit(&request, &event);
1192         if (rc == 0)
1193                 goto emitter_error;
1194
1195         if (timeout != 1000) {
1196                 yaml_scalar_event_initialize(&event, NULL,
1197                                              (yaml_char_t *)YAML_STR_TAG,
1198                                              (yaml_char_t *)"timeout",
1199                                              strlen("timeout"), 1, 0,
1200                                              YAML_PLAIN_SCALAR_STYLE);
1201                 rc = yaml_emitter_emit(&request, &event);
1202                 if (rc == 0)
1203                         goto emitter_error;
1204
1205                 yaml_scalar_event_initialize(&event, NULL,
1206                                              (yaml_char_t *)YAML_INT_TAG,
1207                                              (yaml_char_t *)argv[2],
1208                                              strlen(argv[2]), 1, 0,
1209                                              YAML_PLAIN_SCALAR_STYLE);
1210                 rc = yaml_emitter_emit(&request, &event);
1211                 if (rc == 0)
1212                         goto emitter_error;
1213         }
1214
1215         yaml_scalar_event_initialize(&event, NULL,
1216                                      (yaml_char_t *)YAML_STR_TAG,
1217                                      (yaml_char_t *)"nids",
1218                                      strlen("nids"), 1, 0,
1219                                      YAML_PLAIN_SCALAR_STYLE);
1220         rc = yaml_emitter_emit(&request, &event);
1221         if (rc == 0)
1222                 goto emitter_error;
1223
1224         yaml_sequence_start_event_initialize(&event, NULL,
1225                                              (yaml_char_t *)YAML_SEQ_TAG,
1226                                              1, YAML_FLOW_SEQUENCE_STYLE);
1227         rc = yaml_emitter_emit(&request, &event);
1228         if (rc == 0)
1229                 goto emitter_error;
1230
1231         yaml_scalar_event_initialize(&event, NULL,
1232                                      (yaml_char_t *)YAML_STR_TAG,
1233                                      (yaml_char_t *)argv[1],
1234                                      strlen(argv[1]), 1, 0,
1235                                      YAML_PLAIN_SCALAR_STYLE);
1236         rc = yaml_emitter_emit(&request, &event);
1237         if (rc == 0)
1238                 goto emitter_error;
1239
1240         yaml_sequence_end_event_initialize(&event);
1241         rc = yaml_emitter_emit(&request, &event);
1242         if (rc == 0)
1243                 goto emitter_error;
1244
1245         yaml_mapping_end_event_initialize(&event);
1246         rc = yaml_emitter_emit(&request, &event);
1247         if (rc == 0)
1248                 goto emitter_error;
1249
1250         yaml_mapping_end_event_initialize(&event);
1251         rc = yaml_emitter_emit(&request, &event);
1252         if (rc == 0)
1253                 goto emitter_error;
1254
1255         yaml_document_end_event_initialize(&event, 0);
1256         rc = yaml_emitter_emit(&request, &event);
1257         if (rc == 0)
1258                 goto emitter_error;
1259
1260         rc = yaml_emitter_close(&request);
1261 emitter_error:
1262         if (rc == 0) {
1263                 yaml_emitter_log_error(&request, stderr);
1264                 rc = -EINVAL;
1265         }
1266         yaml_emitter_delete(&request);
1267
1268         /* Now parse the reply results */
1269         while (!done) {
1270                 rc = yaml_parser_parse(&reply, &event);
1271                 if (rc == 0)
1272                         break;
1273
1274                 if (event.type != YAML_SCALAR_EVENT)
1275                         goto skip;
1276
1277                 if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
1278                         yaml_event_delete(&event);
1279                         rc = yaml_parser_parse(&reply, &event);
1280                         if (rc == 0) {
1281                                 yaml_event_delete(&event);
1282                                 goto free_reply;
1283                         }
1284                         if (print) {
1285                                 /* Print 0@lo. Its not sent */
1286                                 printf("12345-0@lo\n");
1287                                 print = false;
1288                         }
1289                         printf("%s\n", (char *)event.data.scalar.value);
1290                 } else if (strcmp((char *)event.data.scalar.value,
1291                                   "errno") == 0) {
1292                         yaml_event_delete(&event);
1293                         rc = yaml_parser_parse(&reply, &event);
1294                         if (rc == 0) {
1295                                 yaml_event_delete(&event);
1296                                 goto free_reply;
1297                         }
1298                         rc = strtol((char *)event.data.scalar.value, NULL, 10);
1299                         fprintf(stdout, "failed to ping %s: %s\n",
1300                                 argv[1], strerror(-rc));
1301                 }
1302 skip:
1303                 done = (event.type == YAML_STREAM_END_EVENT);
1304                 yaml_event_delete(&event);
1305         }
1306 free_reply:
1307         if (rc == 0) {
1308                 const char *msg = yaml_parser_get_reader_error(&reply);
1309
1310                 rc = -errno;
1311                 if (strcmp(msg, "Unspecific failure") != 0) {
1312                         fprintf(stdout, "failed to ping %s: %s\n",
1313                                 argv[1], msg);
1314                 } else {
1315                         fprintf(stdout, "failed to ping %s: %s\n",
1316                                 argv[1], strerror(errno));
1317                 }
1318         }
1319         yaml_parser_delete(&reply);
1320         nl_socket_free(sk);
1321         return rc;
1322 old_api:
1323         LIBCFS_IOC_INIT(data);
1324         data.ioc_nid     = id.nid;
1325         data.ioc_u32[0]  = id.pid;
1326         data.ioc_u32[1]  = timeout;
1327         data.ioc_plen1   = sizeof(ids);
1328         data.ioc_pbuf1   = (char *)ids;
1329
1330         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
1331         if (rc != 0) {
1332                 fprintf(stderr, "failed to ping %s: %s\n",
1333                         id.pid == LNET_PID_ANY ?
1334                         libcfs_nid2str(id.nid) : libcfs_id2str(id),
1335                         strerror(errno));
1336                 return -1;
1337         }
1338
1339         for (i = 0; i < data.ioc_count && i < maxids; i++)
1340                 printf("%s\n", libcfs_id2str(ids[i]));
1341
1342         if (data.ioc_count > maxids)
1343                 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1344
1345         return 0;
1346 }
1347
1348 int jt_ptl_mynid(int argc, char **argv)
1349 {
1350         struct libcfs_ioctl_data data;
1351         lnet_nid_t nid;
1352         int rc;
1353
1354         if (argc != 2) {
1355                 fprintf(stderr, "usage: %s NID\n", argv[0]);
1356                 return 0;
1357         }
1358
1359         nid = libcfs_str2nid(argv[1]);
1360         if (nid == LNET_NID_ANY) {
1361                 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1362                 return -1;
1363         }
1364
1365         LIBCFS_IOC_INIT(data);
1366         data.ioc_net = LNET_NIDNET(nid);
1367         data.ioc_nid = nid;
1368
1369         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1370         if (rc < 0)
1371                 fprintf(stderr, "setting my NID failed: %s\n",
1372                         strerror(errno));
1373         else
1374                 printf("registered my nid %s\n", libcfs_nid2str(nid));
1375
1376         return 0;
1377 }
1378
1379 int
1380 jt_ptl_fail_nid(int argc, char **argv)
1381 {
1382         int rc;
1383         lnet_nid_t nid;
1384         int threshold;
1385         struct libcfs_ioctl_data data;
1386
1387         if (argc < 2 || argc > 3) {
1388                 fprintf(stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n",
1389                         argv[0]);
1390                 return 0;
1391         }
1392
1393         if (!libcfs_str2anynid(&nid, argv[1])) {
1394                 fprintf(stderr, "Can't parse nid \"%s\"\n", argv[1]);
1395                 return -1;
1396         }
1397
1398         if (argc < 3) {
1399                 threshold = LNET_MD_THRESH_INF;
1400         } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1401                 fprintf(stderr, "Can't parse count \"%s\"\n", argv[2]);
1402                 return -1;
1403         }
1404
1405         LIBCFS_IOC_INIT(data);
1406         data.ioc_nid = nid;
1407         data.ioc_count = threshold;
1408
1409         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1410         if (rc < 0)
1411                 fprintf(stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1412                         strerror(errno));
1413         else
1414                 printf("%s %s\n",
1415                        threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1416
1417         return 0;
1418 }
1419
1420 int
1421 jt_ptl_add_route(int argc, char **argv)
1422 {
1423         struct lnet_ioctl_config_data data;
1424         lnet_nid_t gateway_nid;
1425         __u32 hops = LNET_UNDEFINED_HOPS;
1426         unsigned int priority = 0;
1427         char *end;
1428         int rc;
1429
1430         if (argc < 2 || argc > 4) {
1431                 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1432                         argv[0]);
1433                 return -1;
1434         }
1435
1436         if (g_net_is_set(argv[0]) == 0)
1437                 return -1;
1438
1439         gateway_nid = libcfs_str2nid(argv[1]);
1440         if (gateway_nid == LNET_NID_ANY) {
1441                 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1442                 return -1;
1443         }
1444
1445         if (argc > 2) {
1446                 hops = strtol(argv[2], &end, 0);
1447                 if (hops == 0 || hops >= 256 ||
1448                     (end && *end != 0)) {
1449                         fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1450                                 argv[2]);
1451                         return -1;
1452                 }
1453                 if (argc == 4) {
1454                         priority = strtoul(argv[3], &end, 0);
1455                         if (end && *end != 0) {
1456                                 fprintf(stderr,
1457                                         "Can't parse priority \"%s\"\n",
1458                                         argv[3]);
1459                                 return -1;
1460                         }
1461                 }
1462         }
1463
1464         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1465         data.cfg_net = g_net;
1466         data.cfg_config_u.cfg_route.rtr_hop = hops;
1467         data.cfg_nid = gateway_nid;
1468         data.cfg_config_u.cfg_route.rtr_priority = priority;
1469
1470         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1471         if (rc != 0) {
1472                 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1473                         strerror(errno));
1474                 return -1;
1475         }
1476
1477         return 0;
1478 }
1479
1480 int
1481 jt_ptl_del_route(int argc, char **argv)
1482 {
1483         struct lnet_ioctl_config_data data;
1484         lnet_nid_t nid;
1485         int rc;
1486
1487         if (argc != 2) {
1488                 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1489                 return 0;
1490         }
1491
1492         if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1493                 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1494                 return -1;
1495         }
1496
1497         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1498         data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
1499         data.cfg_nid = nid;
1500
1501         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1502         if (rc != 0) {
1503                 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1504                         libcfs_nid2str(nid), strerror(errno));
1505                 return -1;
1506         }
1507
1508         return 0;
1509 }
1510
1511 int
1512 jt_ptl_notify_router(int argc, char **argv)
1513 {
1514         struct libcfs_ioctl_data data;
1515         int enable;
1516         lnet_nid_t nid;
1517         int rc;
1518         struct timeval now;
1519         time_t when;
1520
1521         if (argc < 3) {
1522                 fprintf(stderr, "usage: %s targetNID <up/down> [<time>]\n",
1523                         argv[0]);
1524                 return 0;
1525         }
1526
1527         nid = libcfs_str2nid(argv[1]);
1528         if (nid == LNET_NID_ANY) {
1529                 fprintf(stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1530                 return -1;
1531         }
1532
1533         if (lnet_parse_bool (&enable, argv[2]) != 0) {
1534                 fprintf(stderr, "Can't parse boolean %s\n", argv[2]);
1535                 return -1;
1536         }
1537
1538         gettimeofday(&now, NULL);
1539
1540         if (argc < 4) {
1541                 when = now.tv_sec;
1542         } else if (lnet_parse_time(&when, argv[3]) != 0) {
1543                 fprintf(stderr,
1544                         "Can't parse time %s\n Please specify either 'YYYY-MM-DD-HH:MM:SS'\n or an absolute unix time in seconds\n",
1545                         argv[3]);
1546                 return -1;
1547         } else if (when > now.tv_sec) {
1548                 fprintf(stderr, "%s specifies a time in the future\n",
1549                         argv[3]);
1550                 return -1;
1551         }
1552
1553         LIBCFS_IOC_INIT(data);
1554         data.ioc_nid = nid;
1555         data.ioc_flags = enable;
1556         /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1557         data.ioc_u64[0] = (__u64)when;
1558
1559         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1560         if (rc != 0) {
1561                 fprintf(stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1562                         libcfs_nid2str(nid), strerror(errno));
1563                 return -1;
1564         }
1565
1566         return 0;
1567 }
1568
1569 int
1570 jt_ptl_print_routes(int argc, char **argv)
1571 {
1572         struct lnet_ioctl_config_data  data;
1573         int rc;
1574         int index;
1575         __u32 net;
1576         lnet_nid_t nid;
1577         unsigned int hops;
1578         int alive;
1579         unsigned int pri;
1580
1581         for (index = 0; ; index++) {
1582                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1583                 data.cfg_count = index;
1584
1585                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1586                 if (rc != 0)
1587                         break;
1588
1589                 net     = data.cfg_net;
1590                 hops    = data.cfg_config_u.cfg_route.rtr_hop;
1591                 nid     = data.cfg_nid;
1592                 alive   = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
1593                 pri     = data.cfg_config_u.cfg_route.rtr_priority;
1594
1595                 printf("net %18s hops %u gw %32s %s pri %u\n",
1596                        libcfs_net2str(net), hops,
1597                        libcfs_nid2str(nid), alive ? "up" : "down", pri);
1598         }
1599
1600         if (errno != ENOENT)
1601                 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1602                         strerror(errno));
1603
1604         return 0;
1605 }
1606
1607 static int
1608 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1609 {
1610         lnet_nid_t nid;
1611         __u32 net;
1612         int rc = 0;
1613
1614         /* NB: can't support range ipaddress except * and *@net */
1615         if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1616                 net = libcfs_str2net(str + 2);
1617                 if (net == LNET_NET_ANY)
1618                         goto failed;
1619
1620                 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1621         } else {
1622                 rc = libcfs_str2anynid(&nid, str);
1623                 if (!rc)
1624                         goto failed;
1625         }
1626
1627         *nid_p = nid;
1628         return 0;
1629 failed:
1630         fprintf(stderr, "Invalid NID : %s\n", str);
1631         return -1;
1632 }
1633
1634 static int
1635 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1636 {
1637         if (!strcasecmp(msg_str, "put")) {
1638                 *mask_p |= LNET_PUT_BIT;
1639                 return 0;
1640
1641         } else if (!strcasecmp(msg_str, "ack")) {
1642                 *mask_p |= LNET_ACK_BIT;
1643                 return 0;
1644
1645         } else if (!strcasecmp(msg_str, "get")) {
1646                 *mask_p |= LNET_GET_BIT;
1647                 return 0;
1648
1649         } else if (!strcasecmp(msg_str, "reply")) {
1650                 *mask_p |= LNET_REPLY_BIT;
1651                 return 0;
1652         }
1653
1654         fprintf(stderr, "unknown message type %s\n", msg_str);
1655         return -1;
1656 }
1657
1658 static int
1659 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1660 {
1661         unsigned long rc = strtoul(optarg, NULL, 0);
1662
1663         if (rc >= 64) {
1664                 fprintf(stderr, "invalid portal: %lu\n", rc);
1665                 return -1;
1666         }
1667
1668         *mask_p |= (1ULL << rc);
1669         return 0;
1670 }
1671
1672 static int
1673 fault_attr_health_error_parse(char *error, __u32 *mask)
1674 {
1675         if (!strcasecmp(error, "local_interrupt")) {
1676                 *mask |= HSTATUS_LOCAL_INTERRUPT_BIT;
1677                 return 0;
1678         }
1679         if (!strcasecmp(error, "local_dropped")) {
1680                 *mask |= HSTATUS_LOCAL_DROPPED_BIT;
1681                 return 0;
1682         }
1683         if (!strcasecmp(error, "local_aborted")) {
1684                 *mask |= HSTATUS_LOCAL_ABORTED_BIT;
1685                 return 0;
1686         }
1687         if (!strcasecmp(error, "local_no_route")) {
1688                 *mask |= HSTATUS_LOCAL_NO_ROUTE_BIT;
1689                 return 0;
1690         }
1691         if (!strcasecmp(error, "local_error")) {
1692                 *mask |= HSTATUS_LOCAL_ERROR_BIT;
1693                 return 0;
1694         }
1695         if (!strcasecmp(error, "local_timeout")) {
1696                 *mask |= HSTATUS_LOCAL_TIMEOUT_BIT;
1697                 return 0;
1698         }
1699         if (!strcasecmp(error, "remote_error")) {
1700                 *mask |= HSTATUS_REMOTE_ERROR_BIT;
1701                 return 0;
1702         }
1703         if (!strcasecmp(error, "remote_dropped")) {
1704                 *mask |= HSTATUS_REMOTE_DROPPED_BIT;
1705                 return 0;
1706         }
1707         if (!strcasecmp(error, "remote_timeout")) {
1708                 *mask |= HSTATUS_REMOTE_TIMEOUT_BIT;
1709                 return 0;
1710         }
1711         if (!strcasecmp(error, "network_timeout")) {
1712                 *mask |= HSTATUS_NETWORK_TIMEOUT_BIT;
1713                 return 0;
1714         }
1715         if (!strcasecmp(error, "random")) {
1716                 *mask = HSTATUS_RANDOM;
1717                 return 0;
1718         }
1719
1720         return -1;
1721 }
1722
1723 static int
1724 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1725 {
1726         struct libcfs_ioctl_data  data = { { 0 } };
1727         struct lnet_fault_attr    attr;
1728         char *optstr;
1729         int rc;
1730
1731         static const struct option opts[] = {
1732         { .name = "source",   .has_arg = required_argument, .val = 's' },
1733         { .name = "dest",     .has_arg = required_argument, .val = 'd' },
1734         { .name = "rate",     .has_arg = required_argument, .val = 'r' },
1735         { .name = "interval", .has_arg = required_argument, .val = 'i' },
1736         { .name = "random",   .has_arg = no_argument,       .val = 'n' },
1737         { .name = "latency",  .has_arg = required_argument, .val = 'l' },
1738         { .name = "portal",   .has_arg = required_argument, .val = 'p' },
1739         { .name = "message",  .has_arg = required_argument, .val = 'm' },
1740         { .name = "health_error",  .has_arg = required_argument, .val = 'e' },
1741         { .name = "local_nid",  .has_arg = required_argument, .val = 'o' },
1742         { .name = "drop_all",  .has_arg = no_argument, .val = 'x' },
1743         { .name = NULL } };
1744
1745         if (argc == 1) {
1746                 fprintf(stderr,
1747                         "Failed, please provide source, destination and rate of rule\n");
1748                 return -1;
1749         }
1750
1751         optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
1752         memset(&attr, 0, sizeof(attr));
1753         while (1) {
1754                 int c = getopt_long(argc, argv, optstr, opts, NULL);
1755
1756                 if (c == -1)
1757                         break;
1758
1759                 switch (c) {
1760                 case 'o':
1761                         rc = fault_attr_nid_parse(optarg, &attr.fa_local_nid);
1762                         if (rc != 0)
1763                                 goto getopt_failed;
1764                         break;
1765                 case 's': /* source NID/NET */
1766                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1767                         if (rc != 0)
1768                                 goto getopt_failed;
1769                         break;
1770
1771                 case 'd': /* dest NID/NET */
1772                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1773                         if (rc != 0)
1774                                 goto getopt_failed;
1775                         break;
1776
1777                 case 'r': /* drop rate */
1778                         if (opc == LNET_CTL_DROP_ADD)
1779                                 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1780                         else
1781                                 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1782                         break;
1783
1784                 case 'e':
1785                         if (opc == LNET_CTL_DROP_ADD) {
1786                                 rc = fault_attr_health_error_parse(optarg,
1787                                                                    &attr.u.drop.da_health_error_mask);
1788                                 if (rc)
1789                                         goto getopt_failed;
1790                         }
1791                         break;
1792
1793                 case 'x':
1794                         if (opc == LNET_CTL_DROP_ADD)
1795                                 attr.u.drop.da_drop_all = true;
1796                         break;
1797
1798                 case 'n':
1799                         if (opc == LNET_CTL_DROP_ADD)
1800                                 attr.u.drop.da_random = true;
1801                         break;
1802
1803                 case 'i': /* time interval (# seconds) for message drop */
1804                         if (opc == LNET_CTL_DROP_ADD)
1805                                 attr.u.drop.da_interval = strtoul(optarg,
1806                                                                   NULL, 0);
1807                         else
1808                                 attr.u.delay.la_interval = strtoul(optarg,
1809                                                                    NULL, 0);
1810                         break;
1811
1812                 case 'l': /* seconds to wait before activating rule */
1813                         attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1814                         break;
1815
1816                 case 'p': /* portal to filter */
1817                         rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1818                         if (rc != 0)
1819                                 goto getopt_failed;
1820                         break;
1821
1822                 case 'm': /* message types to filter */
1823                         rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1824                         if (rc != 0)
1825                                 goto getopt_failed;
1826                         break;
1827
1828                 default:
1829                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1830                                 argv[0], argv[optind - 1]);
1831                         goto getopt_failed;
1832                 }
1833         }
1834         optind = 1;
1835
1836         if (opc == LNET_CTL_DROP_ADD) {
1837                 /* NB: drop rate and interval are exclusive to each other */
1838                 if (!((attr.u.drop.da_rate == 0) ^
1839                       (attr.u.drop.da_interval == 0))) {
1840                         fprintf(stderr,
1841                                 "please provide either drop rate or interval but not both at the same time.\n");
1842                         return -1;
1843                 }
1844
1845                 if (attr.u.drop.da_random &&
1846                     attr.u.drop.da_interval == 0) {
1847                         fprintf(stderr,
1848                                 "please provide an interval to randomize\n");
1849                         return -1;
1850                 }
1851         } else if (opc == LNET_CTL_DELAY_ADD) {
1852                 if (!((attr.u.delay.la_rate == 0) ^
1853                       (attr.u.delay.la_interval == 0))) {
1854                         fprintf(stderr,
1855                                 "please provide either delay rate or interval but not both at the same time.\n");
1856                         return -1;
1857                 }
1858
1859                 if (attr.u.delay.la_latency == 0) {
1860                         fprintf(stderr, "latency cannot be zero\n");
1861                         return -1;
1862                 }
1863         }
1864
1865         if (attr.fa_src == 0 || attr.fa_dst == 0) {
1866                 fprintf(stderr,
1867                         "Please provide both source and destination of %s rule\n",
1868                         name);
1869                 return -1;
1870         }
1871
1872         if (attr.fa_local_nid == 0)
1873                 attr.fa_local_nid = LNET_NID_ANY;
1874
1875         data.ioc_flags = opc;
1876         data.ioc_inllen1 = sizeof(attr);
1877         data.ioc_inlbuf1 = (char *)&attr;
1878         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1879                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1880                 return -1;
1881         }
1882
1883         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1884         if (rc != 0) {
1885                 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1886                         name, libcfs_nid2str(attr.fa_src),
1887                         libcfs_nid2str(attr.fa_dst), strerror(errno));
1888                 return -1;
1889         }
1890
1891         printf("Added %s rule %s->%s (1/%d)\n",
1892                name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1893                opc == LNET_CTL_DROP_ADD ?
1894                attr.u.drop.da_rate : attr.u.delay.la_rate);
1895         return 0;
1896
1897 getopt_failed:
1898         optind = 1;
1899         return -1;
1900 }
1901
1902 int
1903 jt_ptl_drop_add(int argc, char **argv)
1904 {
1905         return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1906 }
1907
1908 int
1909 jt_ptl_delay_add(int argc, char **argv)
1910 {
1911         return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1912 }
1913
1914 static int
1915 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1916 {
1917         struct libcfs_ioctl_data data = { { 0 } };
1918         struct lnet_fault_attr   attr;
1919         bool all = false;
1920         int rc;
1921
1922         static const struct option opts[] = {
1923                 { .name = "source", .has_arg = required_argument, .val = 's' },
1924                 { .name = "dest",   .has_arg = required_argument, .val = 'd' },
1925                 { .name = "all",    .has_arg = no_argument,       .val = 'a' },
1926                 { .name = NULL } };
1927
1928         if (argc == 1) {
1929                 fprintf(stderr,
1930                         "Failed, please provide source and destination of rule\n");
1931                 return -1;
1932         }
1933
1934         memset(&attr, 0, sizeof(attr));
1935         while (1) {
1936                 int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1937
1938                 if (c == -1 || all)
1939                         break;
1940
1941                 switch (c) {
1942                 case 's':
1943                         rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1944                         if (rc != 0)
1945                                 goto getopt_failed;
1946                         break;
1947                 case 'd':
1948                         rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1949                         if (rc != 0)
1950                                 goto getopt_failed;
1951                         break;
1952                 case 'a':
1953                         attr.fa_src = 0;
1954                         attr.fa_dst = 0;
1955                         all = true;
1956
1957                         break;
1958                 default:
1959                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1960                                 argv[0], argv[optind - 1]);
1961                         goto getopt_failed;
1962                 }
1963         }
1964         optind = 1;
1965
1966         data.ioc_flags = opc;
1967         data.ioc_inllen1 = sizeof(attr);
1968         data.ioc_inlbuf1 = (char *)&attr;
1969         if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1970                 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1971                 return -1;
1972         }
1973
1974         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1975         if (rc != 0) {
1976                 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1977                         all ? "all" : libcfs_nid2str(attr.fa_src),
1978                         all ? "all" : libcfs_nid2str(attr.fa_dst),
1979                         strerror(errno));
1980                 return -1;
1981         }
1982
1983         libcfs_ioctl_unpack(&data, ioc_buf);
1984         printf("Removed %d %s rules\n", data.ioc_count, name);
1985         return 0;
1986
1987 getopt_failed:
1988         optind = 1;
1989         return -1;
1990 }
1991
1992 int
1993 jt_ptl_drop_del(int argc, char **argv)
1994 {
1995         return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1996 }
1997
1998 int
1999 jt_ptl_delay_del(int argc, char **argv)
2000 {
2001         return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
2002 }
2003
2004 static int
2005 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
2006 {
2007         struct libcfs_ioctl_data   data = { { 0 } };
2008         int                        rc;
2009
2010         LIBCFS_IOC_INIT(data);
2011         data.ioc_flags = opc;
2012
2013         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
2014         if (rc != 0) {
2015                 fprintf(stderr, "failed to reset %s stats: %s\n",
2016                         name, strerror(errno));
2017                 return -1;
2018         }
2019         return 0;
2020 }
2021
2022 int
2023 jt_ptl_drop_reset(int argc, char **argv)
2024 {
2025         return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
2026 }
2027
2028 int
2029 jt_ptl_delay_reset(int argc, char **argv)
2030 {
2031         return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
2032                                       argc, argv);
2033 }
2034
2035 static int
2036 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
2037 {
2038         struct libcfs_ioctl_data data = { { 0 } };
2039         struct lnet_fault_attr   attr;
2040         struct lnet_fault_stat   stat;
2041         int pos;
2042
2043         printf("LNet %s rules:\n", name);
2044         for (pos = 0;; pos++) {
2045                 int rc;
2046
2047                 memset(&attr, 0, sizeof(attr));
2048                 memset(&stat, 0, sizeof(stat));
2049
2050                 data.ioc_count = pos;
2051                 data.ioc_flags = opc;
2052                 data.ioc_inllen1 = sizeof(attr);
2053                 data.ioc_inlbuf1 = (char *)&attr;
2054                 data.ioc_inllen2 = sizeof(stat);
2055                 data.ioc_inlbuf2 = (char *)&stat;
2056                 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2057                         fprintf(stderr, "libcfs_ioctl_pack failed\n");
2058                         return -1;
2059                 }
2060
2061                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2062                 if (rc != 0)
2063                         break;
2064
2065                 libcfs_ioctl_unpack(&data, ioc_buf);
2066
2067                 if (opc == LNET_CTL_DROP_LIST) {
2068                         printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2069                                libcfs_nid2str(attr.fa_src),
2070                                libcfs_nid2str(attr.fa_dst),
2071                                attr.u.drop.da_rate, attr.u.drop.da_interval,
2072                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2073                                (uintmax_t)stat.u.drop.ds_dropped,
2074                                (uintmax_t)stat.fs_count,
2075                                (uintmax_t)stat.fs_put,
2076                                (uintmax_t)stat.fs_ack,
2077                                (uintmax_t)stat.fs_get,
2078                                (uintmax_t)stat.fs_reply);
2079
2080                 } else if (opc == LNET_CTL_DELAY_LIST) {
2081                         printf("%s->%s (1/%d | %d, latency %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2082                                libcfs_nid2str(attr.fa_src),
2083                                libcfs_nid2str(attr.fa_dst),
2084                                attr.u.delay.la_rate, attr.u.delay.la_interval,
2085                                attr.u.delay.la_latency,
2086                                (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2087                                (uintmax_t)stat.u.delay.ls_delayed,
2088                                (uintmax_t)stat.fs_count,
2089                                (uintmax_t)stat.fs_put,
2090                                (uintmax_t)stat.fs_ack,
2091                                (uintmax_t)stat.fs_get,
2092                                (uintmax_t)stat.fs_reply);
2093                 }
2094         }
2095         printf("found total %d\n", pos);
2096
2097         return 0;
2098 }
2099
2100 int
2101 jt_ptl_drop_list(int argc, char **argv)
2102 {
2103         return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
2104 }
2105
2106 int
2107 jt_ptl_delay_list(int argc, char **argv)
2108 {
2109         return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
2110 }
2111
2112 double
2113 get_cycles_per_usec()
2114 {
2115         FILE *f = fopen("/proc/cpuinfo", "r");
2116         double mhz;
2117         char line[64];
2118
2119         if (f) {
2120                 while (fgets(line, sizeof(line), f))
2121                         if (sscanf(line, "cpu MHz : %lf", &mhz) == 1) {
2122                                 fclose(f);
2123                                 return mhz;
2124                         }
2125                 fclose(f);
2126         }
2127
2128         fprintf(stderr, "Can't read/parse /proc/cpuinfo\n");
2129         return 1000.0;
2130 }
2131
2132 int jt_ptl_testprotocompat(int argc, char **argv)
2133 {
2134         struct libcfs_ioctl_data  data;
2135         int rc;
2136         int flags;
2137         char *end;
2138
2139         if (argc < 2)  {
2140                 fprintf(stderr, "usage: %s <number>\n", argv[0]);
2141                 return 0;
2142         }
2143
2144         flags = strtol(argv[1], &end, 0);
2145         if (flags < 0 || *end != 0) {
2146                 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
2147                 return -1;
2148         }
2149
2150         LIBCFS_IOC_INIT(data);
2151         data.ioc_flags = flags;
2152         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
2153
2154         if (rc != 0) {
2155                 fprintf(stderr, "test proto compat %x failed: %s\n",
2156                         flags, strerror(errno));
2157                 return -1;
2158         }
2159
2160         printf("test proto compat %x OK\n", flags);
2161         return 0;
2162 }