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