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