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