2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2017, Intel Corporation.
6 * This file is part of Lustre, https://wiki.whamcloud.com/
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.
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.
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.
33 #include <sys/ioctl.h>
36 #include <linux/types.h>
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 <lustre/lustreapi.h>
46 unsigned int libcfs_debug;
47 unsigned int libcfs_printk = D_CANTMASK;
49 static bool g_net_interactive;
50 static bool g_net_set;
53 #define IOC_BUF_SIZE 8192
54 static char local_buf[IOC_BUF_SIZE];
55 static char *ioc_buf = local_buf;
57 /* Convert a string boolean to an int; "enable" -> 1 */
59 lnet_parse_bool (int *b, char *str)
61 if (!strcasecmp(str, "no") ||
62 !strcasecmp(str, "n") ||
63 !strcasecmp(str, "off") ||
64 !strcasecmp(str, "down") ||
65 !strcasecmp(str, "disable")) {
71 if (!strcasecmp(str, "yes") ||
72 !strcasecmp(str, "y") ||
73 !strcasecmp(str, "on") ||
74 !strcasecmp(str, "up") ||
75 !strcasecmp(str, "enable")) {
85 lnet_parse_port(int *port, char *str)
89 *port = strtol(str, &end, 0);
91 if (*end == 0 && /* parsed whole string */
92 *port > 0 && *port < 65536) /* minimal sanity check */
98 #ifdef HAVE_GETHOSTBYNAME
99 static struct hostent *
100 ptl_gethostbyname(char *hname)
104 he = gethostbyname(hname);
109 fprintf(stderr, "Unable to resolve hostname: %s\n",
113 fprintf(stderr, "gethostbyname error for %s: %s\n",
114 hname, strerror(h_errno));
124 lnet_parse_ipquad(__u32 *ipaddrp, char *str)
128 if (sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
129 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
130 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
131 *ipaddrp = (a << 24) | (b << 16) | (c << 8) | d;
140 lnet_parse_ipaddr(__u32 *ipaddrp, char *str)
142 #ifdef HAVE_GETHOSTBYNAME
146 if (!strcmp(str, "_all_")) {
151 if (lnet_parse_ipquad(ipaddrp, str) == 0)
154 #ifdef HAVE_GETHOSTBYNAME
155 if ((('a' <= str[0] && str[0] <= 'z') ||
156 ('A' <= str[0] && str[0] <= 'Z')) &&
157 (he = ptl_gethostbyname(str)) != NULL) {
158 __u32 addr = *(__u32 *)he->h_addr;
160 *ipaddrp = ntohl(addr); /* HOST byte order */
169 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
171 #ifdef HAVE_GETHOSTBYNAME
176 net_ip = htonl(ipaddr);
177 he = gethostbyaddr(&net_ip, sizeof(net_ip), AF_INET);
179 snprintf(str, strsize, "%s", he->h_name);
185 sprintf(str, "%d.%d.%d.%d",
186 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
187 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
192 lnet_parse_time(time_t *t, char *str)
198 *t = strtol(str, &end, 0);
199 if (*end == 0) /* parsed whole string */
202 memset(&tm, 0, sizeof(tm));
203 n = sscanf(str, "%d-%d-%d-%d:%d:%d",
204 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
205 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
209 tm.tm_mon--; /* convert to 0 == Jan */
210 tm.tm_year -= 1900; /* y2k quirk */
211 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
214 if (*t == (time_t)-1)
221 lnet_parse_nid(char *nid_str, struct lnet_process_id *id_ptr)
223 id_ptr->pid = LNET_PID_ANY;
224 id_ptr->nid = libcfs_str2nid(nid_str);
225 if (id_ptr->nid == LNET_NID_ANY) {
226 fprintf(stderr, "Can't parse nid \"%s\"\n", nid_str);
233 static int g_net_is_set(char *cmd)
241 if (g_net_interactive)
247 "You must run '%s <network>' command before '%s'\n",
255 static int g_net_is_compatible(char *cmd, ...)
260 if (!g_net_is_set(cmd))
266 nal = va_arg(ap, int);
267 if (nal == LNET_NETTYP(g_net)) {
276 fprintf(stderr, "Command %s not compatible with %s NAL\n",
277 cmd, libcfs_lnd2str(LNET_NETTYP(g_net)));
282 int ptl_initialize(int argc, char **argv)
285 g_net_interactive = true;
287 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
292 int jt_ptl_network(int argc, char **argv)
294 struct libcfs_ioctl_data data;
295 __u32 net = LNET_NET_ANY;
299 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
303 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
304 LIBCFS_IOC_INIT(data);
305 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
308 printf("LNET ready to unload\n");
312 if (errno == ENODEV) {
313 printf("LNET is currently not loaded.");
318 fprintf(stderr, "LNET busy\n");
320 fprintf(stderr, "LNET unconfigure error %d: %s\n",
321 errno, strerror(errno));
323 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
324 LIBCFS_IOC_INIT(data);
325 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
328 printf("LNET configured\n");
332 fprintf(stderr, "LNET configure error %d: %s\n",
333 errno, strerror(errno));
337 net = libcfs_str2net(argv[1]);
338 if (net == LNET_NET_ANY) {
339 fprintf(stderr, "Can't parse net %s\n", argv[1]);
349 jt_ptl_list_nids(int argc, char **argv)
351 struct libcfs_ioctl_data data;
352 int all = 0, return_nid = 0;
356 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
357 /* Hack to pass back value */
358 return_nid = (argc == 2) && (argv[1][0] == 1);
360 if ((argc > 2) && !(all || return_nid)) {
361 fprintf(stderr, "usage: %s [all]\n", argv[0]);
365 for (count = 0;; count++) {
366 LIBCFS_IOC_INIT(data);
367 data.ioc_count = count;
368 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
371 if ((count > 0) && (errno == ENOENT))
372 /* We found them all */
374 fprintf(stderr, "IOC_LIBCFS_GET_NI error %d: %s\n",
375 errno, strerror(errno));
379 if (all || (data.ioc_nid != LNET_NID_LO_0)) {
380 printf("%s\n", libcfs_nid2str(data.ioc_nid));
382 *(__u64 *)(argv[1]) = data.ioc_nid;
392 jt_ptl_which_nid(int argc, char **argv)
394 struct libcfs_ioctl_data data;
397 lnet_nid_t best_nid = LNET_NID_ANY;
406 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
410 for (i = 1; i < argc; i++) {
412 nid = libcfs_str2nid(nidstr);
413 if (nid == LNET_NID_ANY) {
414 fprintf(stderr, "Can't parse NID %s\n", nidstr);
418 LIBCFS_IOC_INIT(data);
421 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
423 fprintf(stderr, "Can't get distance to %s: %s\n",
424 nidstr, strerror(errno));
428 dist = data.ioc_u32[0];
429 order = data.ioc_u32[1];
432 if (dist == -EHOSTUNREACH)
435 fprintf(stderr, "Unexpected distance to %s: %d\n",
440 if (best_nid == LNET_NID_ANY ||
442 (dist == best_dist && order < best_order)) {
449 if (best_nid == LNET_NID_ANY) {
450 fprintf(stderr, "No reachable NID\n");
454 printf("%s\n", libcfs_nid2str(best_nid));
459 jt_ptl_print_interfaces(int argc, char **argv)
461 struct libcfs_ioctl_data data;
462 char buffer[3][HOST_NAME_MAX + 1];
466 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
469 for (index = 0; ; index++) {
470 LIBCFS_IOC_INIT(data);
471 data.ioc_net = g_net;
472 data.ioc_count = index;
474 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
478 printf("%s: (%s/%s) npeer %d nroute %d\n",
479 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
480 sizeof(buffer[2]), 1),
481 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
482 sizeof(buffer[0]), 0),
483 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
484 sizeof(buffer[1]), 0),
485 data.ioc_u32[2], data.ioc_u32[3]);
489 if (errno == ENOENT) {
490 printf("<no interfaces>\n");
493 "Error getting interfaces: %s: check dmesg.\n",
502 jt_ptl_add_interface(int argc, char **argv)
504 struct libcfs_ioctl_data data;
507 __u32 netmask = 0xffffff00;
512 if (argc < 2 || argc > 3) {
513 fprintf(stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
517 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
520 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
521 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
526 count = strtol(argv[2], &end, 0);
527 if (count > 0 && count < 32 && *end == 0) {
529 for (i = count; i > 0; i--)
530 netmask = netmask | (1 << (32 - i));
531 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
532 fprintf(stderr, "Can't parse netmask: %s\n", argv[2]);
537 LIBCFS_IOC_INIT(data);
538 data.ioc_net = g_net;
539 data.ioc_u32[0] = ipaddr;
540 data.ioc_u32[1] = netmask;
542 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
544 fprintf(stderr, "failed to add interface: %s\n",
553 jt_ptl_del_interface(int argc, char **argv)
555 struct libcfs_ioctl_data data;
560 fprintf(stderr, "usage: %s [ipaddr]\n", argv[0]);
564 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
568 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
569 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
573 LIBCFS_IOC_INIT(data);
574 data.ioc_net = g_net;
575 data.ioc_u32[0] = ipaddr;
577 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
579 fprintf(stderr, "failed to delete interface: %s\n",
588 jt_ptl_print_peers(int argc, char **argv)
590 struct libcfs_ioctl_data data;
591 struct lnet_process_id id;
592 char buffer[2][HOST_NAME_MAX + 1];
596 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
600 for (index = 0; ; index++) {
601 LIBCFS_IOC_INIT(data);
602 data.ioc_net = g_net;
603 data.ioc_count = index;
605 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
609 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
610 id.nid = data.ioc_nid;
611 id.pid = data.ioc_u32[4];
612 printf("%-20s [%d]%s->%s:%d #%d\n",
614 data.ioc_count, /* persistence */
616 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
617 sizeof(buffer[0]), 1),
619 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
620 sizeof(buffer[1]), 1),
621 data.ioc_u32[1], /* peer port */
622 data.ioc_u32[3]); /* conn_count */
623 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
624 int disconn = data.ioc_flags >> 16;
630 state = data.ioc_flags & 0xffff ? "C" : "U";
632 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
633 libcfs_nid2str(data.ioc_nid), /* peer nid */
634 data.ioc_net, /* gemini device id */
635 state, /* peer is Connecting, Up, or Down */
636 data.ioc_count, /* peer refcount */
637 (uintmax_t)data.ioc_u64[0], /* peerstamp */
638 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
639 /* fmaq, nfma, nrdma */
640 data.ioc_u32[0], data.ioc_u32[1],
643 printf("%-20s [%d]\n",
644 libcfs_nid2str(data.ioc_nid), data.ioc_count);
649 if (errno == ENOENT) {
650 printf("<no peers>\n");
653 "Error getting peer list: %s: check dmesg.\n",
660 int jt_ptl_add_peer(int argc, char **argv)
662 struct libcfs_ioctl_data data;
668 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
672 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
677 nid = libcfs_str2nid(argv[1]);
678 if (nid == LNET_NID_ANY) {
679 fprintf(stderr, "Can't parse NID: %s\n", argv[1]);
683 if (lnet_parse_ipaddr(&ip, argv[2]) != 0) {
684 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
688 if (lnet_parse_port(&port, argv[3]) != 0) {
689 fprintf(stderr, "Can't parse port: %s\n", argv[3]);
693 LIBCFS_IOC_INIT(data);
694 data.ioc_net = g_net;
696 data.ioc_u32[0] = ip;
697 data.ioc_u32[1] = port;
699 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
701 fprintf(stderr, "failed to add peer: %s\n",
710 jt_ptl_del_peer(int argc, char **argv)
712 struct libcfs_ioctl_data data;
713 lnet_nid_t nid = LNET_NID_ANY;
714 lnet_pid_t pid = LNET_PID_ANY;
718 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
722 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
724 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
727 } else if (argc > 2) {
728 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
732 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
733 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
737 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
738 if (argc > 2 && lnet_parse_ipaddr(&ip, argv[2]) != 0) {
739 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
744 LIBCFS_IOC_INIT(data);
745 data.ioc_net = g_net;
747 data.ioc_u32[0] = ip;
748 data.ioc_u32[1] = pid;
750 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
752 fprintf(stderr, "failed to remove peer: %s\n", strerror(errno));
760 jt_ptl_print_connections(int argc, char **argv)
762 struct libcfs_ioctl_data data;
763 struct lnet_process_id id;
764 char buffer[2][HOST_NAME_MAX + 1];
768 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
771 for (index = 0; ; index++) {
772 LIBCFS_IOC_INIT(data);
773 data.ioc_net = g_net;
774 data.ioc_count = index;
776 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
780 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
781 id.nid = data.ioc_nid;
782 id.pid = data.ioc_u32[6];
783 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
785 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
786 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
787 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
788 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
789 data.ioc_u32[4], /* scheduler */
791 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
792 sizeof(buffer[0]), 1),
794 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
795 sizeof(buffer[1]), 1),
796 data.ioc_u32[1], /* remote port */
797 data.ioc_count, /* tx buffer size */
798 data.ioc_u32[5], /* rx buffer size */
799 data.ioc_flags ? "nagle" : "nonagle");
800 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
801 printf("%s mtu %d\n",
802 libcfs_nid2str(data.ioc_nid),
803 data.ioc_u32[0]); /* path MTU */
804 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
805 printf("%-20s [%d]\n",
806 libcfs_nid2str(data.ioc_nid),
807 data.ioc_u32[0] /* device id */);
809 printf("%s\n", libcfs_nid2str(data.ioc_nid));
814 if (errno == ENOENT) {
815 printf("<no connections>\n");
818 "Error getting connection list: %s: check dmesg.\n",
825 int jt_ptl_disconnect(int argc, char **argv)
827 struct libcfs_ioctl_data data;
828 lnet_nid_t nid = LNET_NID_ANY;
833 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
837 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
840 if (argc >= 2 && !libcfs_str2anynid(&nid, argv[1])) {
841 fprintf(stderr, "Can't parse nid %s\n", argv[1]);
845 if (g_net_is_compatible(NULL, SOCKLND, 0) && argc >= 3 &&
846 lnet_parse_ipaddr(&ipaddr, argv[2]) != 0) {
847 fprintf(stderr, "Can't parse ip addr %s\n", argv[2]);
851 LIBCFS_IOC_INIT(data);
852 data.ioc_net = g_net;
854 data.ioc_u32[0] = ipaddr;
856 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
858 fprintf(stderr, "failed to remove connection: %s\n",
866 int jt_ptl_push_connection(int argc, char **argv)
868 struct libcfs_ioctl_data data;
870 lnet_nid_t nid = LNET_NID_ANY;
873 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
877 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
880 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
881 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
885 LIBCFS_IOC_INIT(data);
886 data.ioc_net = g_net;
889 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
891 fprintf(stderr, "failed to push connection: %s\n",
899 int jt_ptl_ping(int argc, char **argv)
903 struct lnet_process_id id;
904 struct lnet_process_id ids[16];
905 int maxids = sizeof(ids) / sizeof(ids[0]);
906 struct libcfs_ioctl_data data;
911 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
915 sep = strchr(argv[1], '-');
917 rc = lnet_parse_nid(argv[1], &id);
923 if (argv[1][0] == 'u' || argv[1][0] == 'U')
924 id.pid = strtoul(&argv[1][1], &end, 0) |
927 id.pid = strtoul(argv[1], &end, 0);
929 if (end != sep) { /* assuming '-' is part of hostname */
930 rc = lnet_parse_nid(argv[1], &id);
934 id.nid = libcfs_str2nid(sep + 1);
936 if (id.nid == LNET_NID_ANY) {
938 "Can't parse process id \"%s\"\n",
946 timeout = 1000 * atol(argv[2]);
947 if (timeout > 120 * 1000) {
948 fprintf(stderr, "Timeout %s is to large\n",
953 timeout = 1000; /* default 1 second timeout */
956 LIBCFS_IOC_INIT(data);
957 data.ioc_nid = id.nid;
958 data.ioc_u32[0] = id.pid;
959 data.ioc_u32[1] = timeout;
960 data.ioc_plen1 = sizeof(ids);
961 data.ioc_pbuf1 = (char *)ids;
963 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
965 fprintf(stderr, "failed to ping %s: %s\n",
966 id.pid == LNET_PID_ANY ?
967 libcfs_nid2str(id.nid) : libcfs_id2str(id),
972 for (i = 0; i < data.ioc_count && i < maxids; i++)
973 printf("%s\n", libcfs_id2str(ids[i]));
975 if (data.ioc_count > maxids)
976 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
981 int jt_ptl_mynid(int argc, char **argv)
983 struct libcfs_ioctl_data data;
988 fprintf(stderr, "usage: %s NID\n", argv[0]);
992 nid = libcfs_str2nid(argv[1]);
993 if (nid == LNET_NID_ANY) {
994 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
998 LIBCFS_IOC_INIT(data);
999 data.ioc_net = LNET_NIDNET(nid);
1002 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1004 fprintf(stderr, "setting my NID failed: %s\n",
1007 printf("registered my nid %s\n", libcfs_nid2str(nid));
1013 jt_ptl_fail_nid(int argc, char **argv)
1018 struct libcfs_ioctl_data data;
1020 if (argc < 2 || argc > 3) {
1021 fprintf(stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n",
1026 if (!libcfs_str2anynid(&nid, argv[1])) {
1027 fprintf(stderr, "Can't parse nid \"%s\"\n", argv[1]);
1032 threshold = LNET_MD_THRESH_INF;
1033 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1034 fprintf(stderr, "Can't parse count \"%s\"\n", argv[2]);
1038 LIBCFS_IOC_INIT(data);
1040 data.ioc_count = threshold;
1042 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1044 fprintf(stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1048 threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1054 jt_ptl_add_route(int argc, char **argv)
1056 struct lnet_ioctl_config_data data;
1057 lnet_nid_t gateway_nid;
1058 __u32 hops = LNET_UNDEFINED_HOPS;
1059 unsigned int priority = 0;
1063 if (argc < 2 || argc > 4) {
1064 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1069 if (g_net_is_set(argv[0]) == 0)
1072 gateway_nid = libcfs_str2nid(argv[1]);
1073 if (gateway_nid == LNET_NID_ANY) {
1074 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1079 hops = strtol(argv[2], &end, 0);
1080 if (hops == 0 || hops >= 256 ||
1081 (end && *end != 0)) {
1082 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1087 priority = strtoul(argv[3], &end, 0);
1088 if (end && *end != 0) {
1090 "Can't parse priority \"%s\"\n",
1097 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1098 data.cfg_net = g_net;
1099 data.cfg_config_u.cfg_route.rtr_hop = hops;
1100 data.cfg_nid = gateway_nid;
1101 data.cfg_config_u.cfg_route.rtr_priority = priority;
1103 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1105 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1114 jt_ptl_del_route(int argc, char **argv)
1116 struct lnet_ioctl_config_data data;
1121 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1125 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1126 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1130 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1131 data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
1134 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1136 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1137 libcfs_nid2str(nid), strerror(errno));
1145 jt_ptl_notify_router(int argc, char **argv)
1147 struct libcfs_ioctl_data data;
1155 fprintf(stderr, "usage: %s targetNID <up/down> [<time>]\n",
1160 nid = libcfs_str2nid(argv[1]);
1161 if (nid == LNET_NID_ANY) {
1162 fprintf(stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1166 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1167 fprintf(stderr, "Can't parse boolean %s\n", argv[2]);
1171 gettimeofday(&now, NULL);
1175 } else if (lnet_parse_time(&when, argv[3]) != 0) {
1177 "Can't parse time %s\n Please specify either 'YYYY-MM-DD-HH:MM:SS'\n or an absolute unix time in seconds\n",
1180 } else if (when > now.tv_sec) {
1181 fprintf(stderr, "%s specifies a time in the future\n",
1186 LIBCFS_IOC_INIT(data);
1188 data.ioc_flags = enable;
1189 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1190 data.ioc_u64[0] = (__u64)when;
1192 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1194 fprintf(stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1195 libcfs_nid2str(nid), strerror(errno));
1203 jt_ptl_print_routes(int argc, char **argv)
1205 struct lnet_ioctl_config_data data;
1214 for (index = 0; ; index++) {
1215 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1216 data.cfg_count = index;
1218 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1223 hops = data.cfg_config_u.cfg_route.rtr_hop;
1225 alive = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
1226 pri = data.cfg_config_u.cfg_route.rtr_priority;
1228 printf("net %18s hops %u gw %32s %s pri %u\n",
1229 libcfs_net2str(net), hops,
1230 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1233 if (errno != ENOENT)
1234 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1241 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1247 /* NB: can't support range ipaddress except * and *@net */
1248 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1249 net = libcfs_str2net(str + 2);
1250 if (net == LNET_NET_ANY)
1253 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1255 rc = libcfs_str2anynid(&nid, str);
1263 fprintf(stderr, "Invalid NID : %s\n", str);
1268 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1270 if (!strcasecmp(msg_str, "put")) {
1271 *mask_p |= LNET_PUT_BIT;
1274 } else if (!strcasecmp(msg_str, "ack")) {
1275 *mask_p |= LNET_ACK_BIT;
1278 } else if (!strcasecmp(msg_str, "get")) {
1279 *mask_p |= LNET_GET_BIT;
1282 } else if (!strcasecmp(msg_str, "reply")) {
1283 *mask_p |= LNET_REPLY_BIT;
1287 fprintf(stderr, "unknown message type %s\n", msg_str);
1292 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1294 unsigned long rc = strtoul(optarg, NULL, 0);
1297 fprintf(stderr, "invalid portal: %lu\n", rc);
1301 *mask_p |= (1ULL << rc);
1306 fault_attr_health_error_parse(char *error, __u32 *mask)
1308 if (!strcasecmp(error, "local_interrupt")) {
1309 *mask |= HSTATUS_LOCAL_INTERRUPT_BIT;
1312 if (!strcasecmp(error, "local_dropped")) {
1313 *mask |= HSTATUS_LOCAL_DROPPED_BIT;
1316 if (!strcasecmp(error, "local_aborted")) {
1317 *mask |= HSTATUS_LOCAL_ABORTED_BIT;
1320 if (!strcasecmp(error, "local_no_route")) {
1321 *mask |= HSTATUS_LOCAL_NO_ROUTE_BIT;
1324 if (!strcasecmp(error, "local_error")) {
1325 *mask |= HSTATUS_LOCAL_ERROR_BIT;
1328 if (!strcasecmp(error, "local_timeout")) {
1329 *mask |= HSTATUS_LOCAL_TIMEOUT_BIT;
1332 if (!strcasecmp(error, "remote_error")) {
1333 *mask |= HSTATUS_REMOTE_ERROR_BIT;
1336 if (!strcasecmp(error, "remote_dropped")) {
1337 *mask |= HSTATUS_REMOTE_DROPPED_BIT;
1340 if (!strcasecmp(error, "remote_timeout")) {
1341 *mask |= HSTATUS_REMOTE_TIMEOUT_BIT;
1344 if (!strcasecmp(error, "network_timeout")) {
1345 *mask |= HSTATUS_NETWORK_TIMEOUT_BIT;
1348 if (!strcasecmp(error, "random")) {
1349 *mask = HSTATUS_RANDOM;
1357 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1359 struct libcfs_ioctl_data data = { { 0 } };
1360 struct lnet_fault_attr attr;
1364 static const struct option opts[] = {
1365 { .name = "source", .has_arg = required_argument, .val = 's' },
1366 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1367 { .name = "rate", .has_arg = required_argument, .val = 'r' },
1368 { .name = "interval", .has_arg = required_argument, .val = 'i' },
1369 { .name = "random", .has_arg = no_argument, .val = 'n' },
1370 { .name = "latency", .has_arg = required_argument, .val = 'l' },
1371 { .name = "portal", .has_arg = required_argument, .val = 'p' },
1372 { .name = "message", .has_arg = required_argument, .val = 'm' },
1373 { .name = "health_error", .has_arg = required_argument, .val = 'e' },
1374 { .name = "local_nid", .has_arg = required_argument, .val = 'o' },
1375 { .name = "drop_all", .has_arg = no_argument, .val = 'x' },
1380 "Failed, please provide source, destination and rate of rule\n");
1384 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
1385 memset(&attr, 0, sizeof(attr));
1387 int c = getopt_long(argc, argv, optstr, opts, NULL);
1394 rc = fault_attr_nid_parse(optarg, &attr.fa_local_nid);
1398 case 's': /* source NID/NET */
1399 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1404 case 'd': /* dest NID/NET */
1405 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1410 case 'r': /* drop rate */
1411 if (opc == LNET_CTL_DROP_ADD)
1412 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1414 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1418 if (opc == LNET_CTL_DROP_ADD) {
1419 rc = fault_attr_health_error_parse(optarg,
1420 &attr.u.drop.da_health_error_mask);
1427 if (opc == LNET_CTL_DROP_ADD)
1428 attr.u.drop.da_drop_all = true;
1432 if (opc == LNET_CTL_DROP_ADD)
1433 attr.u.drop.da_random = true;
1436 case 'i': /* time interval (# seconds) for message drop */
1437 if (opc == LNET_CTL_DROP_ADD)
1438 attr.u.drop.da_interval = strtoul(optarg,
1441 attr.u.delay.la_interval = strtoul(optarg,
1445 case 'l': /* seconds to wait before activating rule */
1446 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1449 case 'p': /* portal to filter */
1450 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1455 case 'm': /* message types to filter */
1456 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1462 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1463 argv[0], argv[optind - 1]);
1469 if (opc == LNET_CTL_DROP_ADD) {
1470 /* NB: drop rate and interval are exclusive to each other */
1471 if (!((attr.u.drop.da_rate == 0) ^
1472 (attr.u.drop.da_interval == 0))) {
1474 "please provide either drop rate or interval but not both at the same time.\n");
1478 if (attr.u.drop.da_random &&
1479 attr.u.drop.da_interval == 0) {
1481 "please provide an interval to randomize\n");
1484 } else if (opc == LNET_CTL_DELAY_ADD) {
1485 if (!((attr.u.delay.la_rate == 0) ^
1486 (attr.u.delay.la_interval == 0))) {
1488 "please provide either delay rate or interval but not both at the same time.\n");
1492 if (attr.u.delay.la_latency == 0) {
1493 fprintf(stderr, "latency cannot be zero\n");
1498 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1500 "Please provide both source and destination of %s rule\n",
1505 if (attr.fa_local_nid == 0)
1506 attr.fa_local_nid = LNET_NID_ANY;
1508 data.ioc_flags = opc;
1509 data.ioc_inllen1 = sizeof(attr);
1510 data.ioc_inlbuf1 = (char *)&attr;
1511 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1512 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1516 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1518 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1519 name, libcfs_nid2str(attr.fa_src),
1520 libcfs_nid2str(attr.fa_dst), strerror(errno));
1524 printf("Added %s rule %s->%s (1/%d)\n",
1525 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1526 opc == LNET_CTL_DROP_ADD ?
1527 attr.u.drop.da_rate : attr.u.delay.la_rate);
1536 jt_ptl_drop_add(int argc, char **argv)
1538 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1542 jt_ptl_delay_add(int argc, char **argv)
1544 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1548 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1550 struct libcfs_ioctl_data data = { { 0 } };
1551 struct lnet_fault_attr attr;
1555 static const struct option opts[] = {
1556 { .name = "source", .has_arg = required_argument, .val = 's' },
1557 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1558 { .name = "all", .has_arg = no_argument, .val = 'a' },
1563 "Failed, please provide source and destination of rule\n");
1567 memset(&attr, 0, sizeof(attr));
1569 int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1576 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1581 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1592 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
1593 argv[0], argv[optind - 1]);
1599 data.ioc_flags = opc;
1600 data.ioc_inllen1 = sizeof(attr);
1601 data.ioc_inlbuf1 = (char *)&attr;
1602 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1603 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1607 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1609 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1610 all ? "all" : libcfs_nid2str(attr.fa_src),
1611 all ? "all" : libcfs_nid2str(attr.fa_dst),
1616 libcfs_ioctl_unpack(&data, ioc_buf);
1617 printf("Removed %d %s rules\n", data.ioc_count, name);
1626 jt_ptl_drop_del(int argc, char **argv)
1628 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1632 jt_ptl_delay_del(int argc, char **argv)
1634 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1638 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1640 struct libcfs_ioctl_data data = { { 0 } };
1643 LIBCFS_IOC_INIT(data);
1644 data.ioc_flags = opc;
1646 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1648 fprintf(stderr, "failed to reset %s stats: %s\n",
1649 name, strerror(errno));
1656 jt_ptl_drop_reset(int argc, char **argv)
1658 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1662 jt_ptl_delay_reset(int argc, char **argv)
1664 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1669 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1671 struct libcfs_ioctl_data data = { { 0 } };
1672 struct lnet_fault_attr attr;
1673 struct lnet_fault_stat stat;
1676 printf("LNet %s rules:\n", name);
1677 for (pos = 0;; pos++) {
1680 memset(&attr, 0, sizeof(attr));
1681 memset(&stat, 0, sizeof(stat));
1683 data.ioc_count = pos;
1684 data.ioc_flags = opc;
1685 data.ioc_inllen1 = sizeof(attr);
1686 data.ioc_inlbuf1 = (char *)&attr;
1687 data.ioc_inllen2 = sizeof(stat);
1688 data.ioc_inlbuf2 = (char *)&stat;
1689 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1690 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1694 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1698 libcfs_ioctl_unpack(&data, ioc_buf);
1700 if (opc == LNET_CTL_DROP_LIST) {
1701 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
1702 libcfs_nid2str(attr.fa_src),
1703 libcfs_nid2str(attr.fa_dst),
1704 attr.u.drop.da_rate, attr.u.drop.da_interval,
1705 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1706 (uintmax_t)stat.u.drop.ds_dropped,
1707 (uintmax_t)stat.fs_count,
1708 (uintmax_t)stat.fs_put,
1709 (uintmax_t)stat.fs_ack,
1710 (uintmax_t)stat.fs_get,
1711 (uintmax_t)stat.fs_reply);
1713 } else if (opc == LNET_CTL_DELAY_LIST) {
1714 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
1715 libcfs_nid2str(attr.fa_src),
1716 libcfs_nid2str(attr.fa_dst),
1717 attr.u.delay.la_rate, attr.u.delay.la_interval,
1718 attr.u.delay.la_latency,
1719 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1720 (uintmax_t)stat.u.delay.ls_delayed,
1721 (uintmax_t)stat.fs_count,
1722 (uintmax_t)stat.fs_put,
1723 (uintmax_t)stat.fs_ack,
1724 (uintmax_t)stat.fs_get,
1725 (uintmax_t)stat.fs_reply);
1728 printf("found total %d\n", pos);
1734 jt_ptl_drop_list(int argc, char **argv)
1736 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1740 jt_ptl_delay_list(int argc, char **argv)
1742 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1746 get_cycles_per_usec()
1748 FILE *f = fopen("/proc/cpuinfo", "r");
1753 while (fgets(line, sizeof(line), f))
1754 if (sscanf(line, "cpu MHz : %lf", &mhz) == 1) {
1761 fprintf(stderr, "Can't read/parse /proc/cpuinfo\n");
1765 int jt_ptl_testprotocompat(int argc, char **argv)
1767 struct libcfs_ioctl_data data;
1773 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1777 flags = strtol(argv[1], &end, 0);
1778 if (flags < 0 || *end != 0) {
1779 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1783 LIBCFS_IOC_INIT(data);
1784 data.ioc_flags = flags;
1785 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1788 fprintf(stderr, "test proto compat %x failed: %s\n",
1789 flags, strerror(errno));
1793 printf("test proto compat %x OK\n", flags);