2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2014, Intel Corporation.
6 * This file is part of Lustre, https://wiki.hpdd.intel.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.
32 #include <sys/ioctl.h>
35 #include <linux/types.h>
36 #include <libcfs/libcfs.h>
37 #include <libcfs/util/string.h>
38 #include <libcfs/util/ioctl.h>
39 #include <lnet/lnetctl.h>
40 #include <lnet/socklnd.h>
41 #include <lnet/lnet.h>
43 unsigned int libcfs_debug;
44 unsigned int libcfs_printk = D_CANTMASK;
49 #define IOC_BUF_SIZE 8192
50 static char local_buf[IOC_BUF_SIZE];
51 static char *ioc_buf = local_buf;
53 /* Convert a string boolean to an int; "enable" -> 1 */
55 lnet_parse_bool (int *b, char *str)
57 if (!strcasecmp (str, "no") ||
58 !strcasecmp (str, "n") ||
59 !strcasecmp (str, "off") ||
60 !strcasecmp (str, "down") ||
61 !strcasecmp (str, "disable"))
67 if (!strcasecmp (str, "yes") ||
68 !strcasecmp (str, "y") ||
69 !strcasecmp (str, "on") ||
70 !strcasecmp (str, "up") ||
71 !strcasecmp (str, "enable"))
81 lnet_parse_port (int *port, char *str)
85 *port = strtol (str, &end, 0);
87 if (*end == 0 && /* parsed whole string */
88 *port > 0 && *port < 65536) /* minimal sanity check */
94 #ifdef HAVE_GETHOSTBYNAME
95 static struct hostent *
96 ptl_gethostbyname(char * hname) {
98 he = gethostbyname(hname);
103 fprintf(stderr, "Unable to resolve hostname: %s\n",
107 fprintf(stderr, "gethostbyname error for %s: %s\n",
108 hname, strerror(h_errno));
118 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
125 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
126 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
127 (c & ~0xff) == 0 && (d & ~0xff) == 0)
129 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
137 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
139 #ifdef HAVE_GETHOSTBYNAME
143 if (!strcmp (str, "_all_")) {
148 if (lnet_parse_ipquad(ipaddrp, str) == 0)
151 #ifdef HAVE_GETHOSTBYNAME
152 if ((('a' <= str[0] && str[0] <= 'z') ||
153 ('A' <= str[0] && str[0] <= 'Z')) &&
154 (he = ptl_gethostbyname (str)) != NULL) {
155 __u32 addr = *(__u32 *)he->h_addr;
157 *ipaddrp = ntohl(addr); /* HOST byte order */
166 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
168 #ifdef HAVE_GETHOSTBYNAME
173 net_ip = htonl (ipaddr);
174 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
176 strlcpy(str, he->h_name, strsize);
182 sprintf (str, "%d.%d.%d.%d",
183 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
184 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
189 lnet_parse_time (time_t *t, char *str)
195 *t = strtol (str, &end, 0);
196 if (*end == 0) /* parsed whole string */
199 memset (&tm, 0, sizeof (tm));
200 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
201 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
202 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
206 tm.tm_mon--; /* convert to 0 == Jan */
207 tm.tm_year -= 1900; /* y2k quirk */
208 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
211 if (*t == (time_t)-1)
218 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
220 id_ptr->pid = LNET_PID_ANY;
221 id_ptr->nid = libcfs_str2nid(nid_str);
222 if (id_ptr->nid == LNET_NID_ANY) {
223 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
230 int g_net_is_set (char *cmd)
237 "You must run the 'network' command before '%s'.\n",
242 int g_net_is_compatible (char *cmd, ...)
247 if (!g_net_is_set(cmd))
253 nal = va_arg (ap, int);
254 if (nal == LNET_NETTYP(g_net)) {
264 "Command %s not compatible with %s NAL\n",
266 libcfs_lnd2str(LNET_NETTYP(g_net)));
270 int ptl_initialize(int argc, char **argv)
272 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
273 LNET_DEV_MAJOR, LNET_DEV_MINOR);
278 int jt_ptl_network(int argc, char **argv)
280 struct libcfs_ioctl_data data;
281 __u32 net = LNET_NIDNET(LNET_NID_ANY);
285 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
289 if (!strcmp(argv[1], "unconfigure") ||
290 !strcmp(argv[1], "down")) {
291 LIBCFS_IOC_INIT(data);
292 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
295 printf ("LNET ready to unload\n");
300 fprintf(stderr, "LNET busy\n");
302 fprintf(stderr, "LNET unconfigure error %d: %s\n",
303 errno, strerror(errno));
307 if (!strcmp(argv[1], "configure") ||
308 !strcmp(argv[1], "up")) {
309 LIBCFS_IOC_INIT(data);
310 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
313 printf ("LNET configured\n");
317 fprintf(stderr, "LNET configure error %d: %s\n",
318 errno, strerror(errno));
322 net = libcfs_str2net(argv[1]);
323 if (net == LNET_NIDNET(LNET_NID_ANY)) {
324 fprintf(stderr, "Can't parse net %s\n", argv[1]);
328 if (LNET_NETTYP(net) == CIBLND ||
329 LNET_NETTYP(net) == OPENIBLND ||
330 LNET_NETTYP(net) == IIBLND ||
331 LNET_NETTYP(net) == VIBLND) {
332 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
342 jt_ptl_list_nids(int argc, char **argv)
344 struct libcfs_ioctl_data data;
345 int all = 0, return_nid = 0;
349 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
350 /* Hack to pass back value */
351 return_nid = (argc == 2) && (argv[1][0] == 1);
353 if ((argc > 2) && !(all || return_nid)) {
354 fprintf(stderr, "usage: %s [all]\n", argv[0]);
358 for (count = 0;; count++) {
359 LIBCFS_IOC_INIT (data);
360 data.ioc_count = count;
361 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
364 if ((count > 0) && (errno == ENOENT))
365 /* We found them all */
367 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
368 errno, strerror(errno));
372 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
373 printf("%s\n", libcfs_nid2str(data.ioc_nid));
375 *(__u64 *)(argv[1]) = data.ioc_nid;
385 jt_ptl_which_nid (int argc, char **argv)
387 struct libcfs_ioctl_data data;
390 lnet_nid_t best_nid = LNET_NID_ANY;
399 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
403 for (i = 1; i < argc; i++) {
405 nid = libcfs_str2nid(nidstr);
406 if (nid == LNET_NID_ANY) {
407 fprintf(stderr, "Can't parse NID %s\n", nidstr);
411 LIBCFS_IOC_INIT(data);
414 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
416 fprintf(stderr, "Can't get distance to %s: %s\n",
417 nidstr, strerror(errno));
421 dist = data.ioc_u32[0];
422 order = data.ioc_u32[1];
425 if (dist == -EHOSTUNREACH)
428 fprintf(stderr, "Unexpected distance to %s: %d\n",
433 if (best_nid == LNET_NID_ANY ||
435 (dist == best_dist && order < best_order)) {
442 if (best_nid == LNET_NID_ANY) {
443 fprintf(stderr, "No reachable NID\n");
447 printf("%s\n", libcfs_nid2str(best_nid));
452 jt_ptl_print_interfaces (int argc, char **argv)
454 struct libcfs_ioctl_data data;
455 char buffer[3][HOST_NAME_MAX + 1];
459 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
462 for (index = 0;;index++) {
463 LIBCFS_IOC_INIT(data);
464 data.ioc_net = g_net;
465 data.ioc_count = index;
467 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
471 printf ("%s: (%s/%s) npeer %d nroute %d\n",
472 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
473 sizeof(buffer[2]), 1),
474 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
475 sizeof(buffer[0]), 0),
476 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
477 sizeof(buffer[1]), 0),
478 data.ioc_u32[2], data.ioc_u32[3]);
482 if (errno == ENOENT) {
483 printf ("<no interfaces>\n");
485 fprintf(stderr, "Error getting interfaces: %s: "
495 jt_ptl_add_interface (int argc, char **argv)
497 struct libcfs_ioctl_data data;
500 __u32 netmask = 0xffffff00;
505 if (argc < 2 || argc > 3) {
506 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
510 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
513 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
514 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
519 count = strtol(argv[2], &end, 0);
520 if (count > 0 && count < 32 && *end == 0) {
522 for (i = count; i > 0; i--)
523 netmask = netmask|(1<<(32-i));
524 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
525 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
530 LIBCFS_IOC_INIT(data);
531 data.ioc_net = g_net;
532 data.ioc_u32[0] = ipaddr;
533 data.ioc_u32[1] = netmask;
535 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
537 fprintf (stderr, "failed to add interface: %s\n",
546 jt_ptl_del_interface (int argc, char **argv)
548 struct libcfs_ioctl_data data;
553 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
557 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
561 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
562 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
566 LIBCFS_IOC_INIT(data);
567 data.ioc_net = g_net;
568 data.ioc_u32[0] = ipaddr;
570 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
572 fprintf (stderr, "failed to delete interface: %s\n",
581 jt_ptl_print_peers (int argc, char **argv)
583 struct libcfs_ioctl_data data;
584 lnet_process_id_t id;
585 char buffer[2][HOST_NAME_MAX + 1];
589 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
592 for (index = 0;;index++) {
593 LIBCFS_IOC_INIT(data);
594 data.ioc_net = g_net;
595 data.ioc_count = index;
597 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
601 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
602 id.nid = data.ioc_nid;
603 id.pid = data.ioc_u32[4];
604 printf ("%-20s [%d]%s->%s:%d #%d\n",
606 data.ioc_count, /* persistence */
608 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
609 sizeof(buffer[0]), 1),
611 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
612 sizeof(buffer[1]), 1),
613 data.ioc_u32[1], /* peer port */
614 data.ioc_u32[3]); /* conn_count */
615 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
616 int disconn = data.ioc_flags >> 16;
622 state = data.ioc_flags & 0xffff ? "C" : "U";
624 printf ("%-20s (%d) %s [%d] "LPU64" "
625 "sq %d/%d tx %d/%d/%d\n",
626 libcfs_nid2str(data.ioc_nid), /* peer nid */
627 data.ioc_net, /* gemini device id */
628 state, /* peer is Connecting, Up, or Down */
629 data.ioc_count, /* peer refcount */
630 data.ioc_u64[0], /* peerstamp */
631 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
632 /* fmaq, nfma, nrdma */
633 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
636 printf ("%-20s [%d]\n",
637 libcfs_nid2str(data.ioc_nid), data.ioc_count);
642 if (errno == ENOENT) {
643 printf ("<no peers>\n");
645 fprintf(stderr, "Error getting peer list: %s: "
654 jt_ptl_add_peer (int argc, char **argv)
656 struct libcfs_ioctl_data data;
662 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
666 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
671 nid = libcfs_str2nid(argv[1]);
672 if (nid == LNET_NID_ANY) {
673 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
677 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
678 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
682 if (lnet_parse_port (&port, argv[3]) != 0) {
683 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
687 LIBCFS_IOC_INIT(data);
688 data.ioc_net = g_net;
690 data.ioc_u32[0] = ip;
691 data.ioc_u32[1] = port;
693 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
695 fprintf (stderr, "failed to add peer: %s\n",
704 jt_ptl_del_peer (int argc, char **argv)
706 struct libcfs_ioctl_data data;
707 lnet_nid_t nid = LNET_NID_ANY;
708 lnet_pid_t pid = LNET_PID_ANY;
712 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
715 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
717 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
721 } else if (argc > 2) {
722 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
727 !libcfs_str2anynid(&nid, argv[1])) {
728 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
732 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
734 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
735 fprintf (stderr, "Can't parse ip addr: %s\n",
741 LIBCFS_IOC_INIT(data);
742 data.ioc_net = g_net;
744 data.ioc_u32[0] = ip;
745 data.ioc_u32[1] = pid;
747 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
749 fprintf (stderr, "failed to remove peer: %s\n",
758 jt_ptl_print_connections (int argc, char **argv)
760 struct libcfs_ioctl_data data;
761 lnet_process_id_t id;
762 char buffer[2][HOST_NAME_MAX + 1];
766 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
769 for (index = 0; ; index++) {
770 LIBCFS_IOC_INIT(data);
771 data.ioc_net = g_net;
772 data.ioc_count = index;
774 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
778 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
779 id.nid = data.ioc_nid;
780 id.pid = data.ioc_u32[6];
781 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
783 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
784 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
785 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
786 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
787 data.ioc_u32[4], /* scheduler */
789 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
790 sizeof(buffer[0]), 1),
792 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
793 sizeof(buffer[1]), 1),
794 data.ioc_u32[1], /* remote port */
795 data.ioc_count, /* tx buffer size */
796 data.ioc_u32[5], /* rx buffer size */
797 data.ioc_flags ? "nagle" : "nonagle");
798 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
799 printf ("%s mtu %d\n",
800 libcfs_nid2str(data.ioc_nid),
801 data.ioc_u32[0]); /* path MTU */
802 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
803 printf ("%-20s [%d]\n",
804 libcfs_nid2str(data.ioc_nid),
805 data.ioc_u32[0] /* device id */);
807 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
812 if (errno == ENOENT) {
813 printf ("<no connections>\n");
815 fprintf(stderr, "Error getting connection list: %s: "
823 int jt_ptl_disconnect(int argc, char **argv)
825 struct libcfs_ioctl_data data;
826 lnet_nid_t nid = LNET_NID_ANY;
831 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
835 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
839 !libcfs_str2anynid(&nid, argv[1])) {
840 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
844 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
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))
881 !libcfs_str2anynid(&nid, argv[1])) {
882 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
886 LIBCFS_IOC_INIT(data);
887 data.ioc_net = g_net;
890 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
892 fprintf(stderr, "failed to push connection: %s\n",
900 int jt_ptl_ping(int argc, char **argv)
904 lnet_process_id_t id;
905 lnet_process_id_t ids[16];
906 int maxids = sizeof(ids)/sizeof(ids[0]);
907 struct libcfs_ioctl_data data;
912 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
916 sep = strchr(argv[1], '-');
918 rc = lnet_parse_nid(argv[1], &id);
924 if (argv[1][0] == 'u' ||
926 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
928 id.pid = strtoul(argv[1], &end, 0);
930 if (end != sep) { /* assuming '-' is part of hostname */
931 rc = lnet_parse_nid(argv[1], &id);
935 id.nid = libcfs_str2nid(sep + 1);
937 if (id.nid == LNET_NID_ANY) {
939 "Can't parse process id \"%s\"\n",
947 timeout = 1000 * atol(argv[2]);
949 timeout = 1000; /* default 1 second timeout */
951 LIBCFS_IOC_INIT (data);
952 data.ioc_nid = id.nid;
953 data.ioc_u32[0] = id.pid;
954 data.ioc_u32[1] = timeout;
955 data.ioc_plen1 = sizeof(ids);
956 data.ioc_pbuf1 = (char *)ids;
958 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
960 fprintf(stderr, "failed to ping %s: %s\n",
961 id.pid == LNET_PID_ANY ?
962 libcfs_nid2str(id.nid) : libcfs_id2str(id),
967 for (i = 0; i < data.ioc_count && i < maxids; i++)
968 printf("%s\n", libcfs_id2str(ids[i]));
970 if (data.ioc_count > maxids)
971 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
976 int jt_ptl_mynid(int argc, char **argv)
978 struct libcfs_ioctl_data data;
983 fprintf(stderr, "usage: %s NID\n", argv[0]);
987 nid = libcfs_str2nid(argv[1]);
988 if (nid == LNET_NID_ANY) {
989 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
993 LIBCFS_IOC_INIT(data);
994 data.ioc_net = LNET_NIDNET(nid);
997 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
999 fprintf(stderr, "setting my NID failed: %s\n",
1002 printf("registered my nid %s\n", libcfs_nid2str(nid));
1008 jt_ptl_fail_nid (int argc, char **argv)
1013 struct libcfs_ioctl_data data;
1015 if (argc < 2 || argc > 3)
1017 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1021 if (!libcfs_str2anynid(&nid, argv[1]))
1023 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1028 threshold = LNET_MD_THRESH_INF;
1029 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1030 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1034 LIBCFS_IOC_INIT (data);
1036 data.ioc_count = threshold;
1038 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1040 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1043 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1049 jt_ptl_add_route (int argc, char **argv)
1051 struct lnet_ioctl_config_data data;
1052 lnet_nid_t gateway_nid;
1053 unsigned int hops = 1;
1054 unsigned int priority = 0;
1058 if (argc < 2 || argc > 4) {
1059 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1064 if (g_net_is_set(argv[0]) == 0)
1067 gateway_nid = libcfs_str2nid(argv[1]);
1068 if (gateway_nid == LNET_NID_ANY) {
1069 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1074 hops = strtoul(argv[2], &end, 0);
1075 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1076 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1081 priority = strtoul(argv[3], &end, 0);
1082 if (end != NULL && *end != 0) {
1084 "Can't parse priority \"%s\"\n",
1091 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1092 data.cfg_net = g_net;
1093 data.cfg_config_u.cfg_route.rtr_hop = hops;
1094 data.cfg_nid = gateway_nid;
1095 data.cfg_config_u.cfg_route.rtr_priority = priority;
1097 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1099 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1108 jt_ptl_del_route (int argc, char **argv)
1110 struct lnet_ioctl_config_data data;
1115 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1119 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1120 fprintf(stderr, "Can't parse gateway NID "
1121 "\"%s\"\n", argv[1]);
1125 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1126 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1129 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1131 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1132 libcfs_nid2str(nid), strerror(errno));
1140 jt_ptl_notify_router (int argc, char **argv)
1142 struct libcfs_ioctl_data data;
1151 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1156 nid = libcfs_str2nid(argv[1]);
1157 if (nid == LNET_NID_ANY) {
1158 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1162 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1163 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1167 gettimeofday(&now, NULL);
1171 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1172 fprintf(stderr, "Can't parse time %s\n"
1173 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1174 "or an absolute unix time in seconds\n", argv[3]);
1176 } else if (when > now.tv_sec) {
1177 fprintf (stderr, "%s specifies a time in the future\n",
1182 LIBCFS_IOC_INIT(data);
1184 data.ioc_flags = enable;
1185 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1186 data.ioc_u64[0] = (__u64)when;
1188 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1190 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1191 libcfs_nid2str(nid), strerror (errno));
1199 jt_ptl_print_routes (int argc, char **argv)
1201 struct lnet_ioctl_config_data data;
1210 for (index = 0; ; index++) {
1211 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1212 data.cfg_count = index;
1214 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1219 hops = data.cfg_config_u.cfg_route.rtr_hop;
1221 alive = data.cfg_config_u.cfg_route.rtr_flags;
1222 pri = data.cfg_config_u.cfg_route.rtr_priority;
1224 printf("net %18s hops %u gw %32s %s pri %u\n",
1225 libcfs_net2str(net), hops,
1226 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1229 if (errno != ENOENT)
1230 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1237 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1243 /* NB: can't support range ipaddress except * and *@net */
1244 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1245 net = libcfs_str2net(str + 2);
1246 if (net == LNET_NIDNET(LNET_NID_ANY))
1249 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1251 rc = libcfs_str2anynid(&nid, str);
1259 fprintf(stderr, "Invalid NID : %s\n", str);
1264 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1266 if (!strcasecmp(msg_str, "put")) {
1267 *mask_p |= LNET_PUT_BIT;
1270 } else if (!strcasecmp(msg_str, "ack")) {
1271 *mask_p |= LNET_ACK_BIT;
1274 } else if (!strcasecmp(msg_str, "get")) {
1275 *mask_p |= LNET_GET_BIT;
1278 } else if (!strcasecmp(msg_str, "reply")) {
1279 *mask_p |= LNET_REPLY_BIT;
1283 fprintf(stderr, "unknown message type %s\n", msg_str);
1288 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1290 unsigned long rc = strtoul(optarg, NULL, 0);
1293 fprintf(stderr, "invalid portal: %lu\n", rc);
1297 *mask_p |= (1ULL << rc);
1302 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1304 struct libcfs_ioctl_data data = {{0}};
1305 struct lnet_fault_attr attr;
1309 static struct option opts[] = {
1310 {"source", required_argument, 0, 's'},
1311 {"dest", required_argument, 0, 'd'},
1312 {"rate", required_argument, 0, 'r'},
1313 {"interval", required_argument, 0, 'i'},
1314 {"latency", required_argument, 0, 'l'},
1315 {"portal", required_argument, 0, 'p'},
1316 {"message", required_argument, 0, 'm'},
1321 fprintf(stderr, "Failed, please provide source, destination "
1322 "and rate of rule\n");
1326 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1327 memset(&attr, 0, sizeof(attr));
1329 char c = getopt_long(argc, argv, optstr, opts, NULL);
1335 case 's': /* source NID/NET */
1336 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1341 case 'd': /* dest NID/NET */
1342 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1347 case 'r': /* drop rate */
1348 if (opc == LNET_CTL_DROP_ADD)
1349 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1351 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1354 case 'i': /* time interval (# seconds) for message drop */
1355 if (opc == LNET_CTL_DROP_ADD)
1356 attr.u.drop.da_interval = strtoul(optarg,
1359 attr.u.delay.la_interval = strtoul(optarg,
1363 case 'l': /* seconds to wait before activating rule */
1364 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1367 case 'p': /* portal to filter */
1368 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1373 case 'm': /* message types to filter */
1374 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1380 fprintf(stderr, "error: %s: option '%s' "
1381 "unrecognized\n", argv[0], argv[optind - 1]);
1387 if (opc == LNET_CTL_DROP_ADD) {
1388 /* NB: drop rate and interval are exclusive to each other */
1389 if (!((attr.u.drop.da_rate == 0) ^
1390 (attr.u.drop.da_interval == 0))) {
1392 "please provide either drop rate or interval "
1393 "but not both at the same time.\n");
1396 } else if (opc == LNET_CTL_DELAY_ADD) {
1397 if (!((attr.u.delay.la_rate == 0) ^
1398 (attr.u.delay.la_interval == 0))) {
1400 "please provide either delay rate or interval "
1401 "but not both at the same time.\n");
1405 if (attr.u.delay.la_latency == 0) {
1406 fprintf(stderr, "latency cannot be zero\n");
1411 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1412 fprintf(stderr, "Please provide both source and destination "
1413 "of %s rule\n", name);
1417 data.ioc_flags = opc;
1418 data.ioc_inllen1 = sizeof(attr);
1419 data.ioc_inlbuf1 = (char *)&attr;
1420 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1421 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1425 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1427 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1428 name, libcfs_nid2str(attr.fa_src),
1429 libcfs_nid2str(attr.fa_dst), strerror(errno));
1433 printf("Added %s rule %s->%s (1/%d)\n",
1434 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1435 opc == LNET_CTL_DROP_ADD ?
1436 attr.u.drop.da_rate : attr.u.delay.la_rate);
1445 jt_ptl_drop_add(int argc, char **argv)
1447 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1451 jt_ptl_delay_add(int argc, char **argv)
1453 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1457 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1459 struct libcfs_ioctl_data data = {{0}};
1460 struct lnet_fault_attr attr;
1464 static struct option opts[] = {
1465 {"source", required_argument, 0, 's'},
1466 {"dest", required_argument, 0, 'd'},
1467 {"all", no_argument, 0, 'a'},
1472 fprintf(stderr, "Failed, please provide source and "
1473 "destination of rule\n");
1477 memset(&attr, 0, sizeof(attr));
1479 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1486 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1491 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1496 attr.fa_src = attr.fa_dst = 0;
1500 fprintf(stderr, "error: %s: option '%s' "
1501 "unrecognized\n", argv[0], argv[optind - 1]);
1507 data.ioc_flags = opc;
1508 data.ioc_inllen1 = sizeof(attr);
1509 data.ioc_inlbuf1 = (char *)&attr;
1510 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1511 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1515 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1517 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1518 all ? "all" : libcfs_nid2str(attr.fa_src),
1519 all ? "all" : libcfs_nid2str(attr.fa_dst),
1524 libcfs_ioctl_unpack(&data, ioc_buf);
1525 printf("Removed %d %s rules\n", data.ioc_count, name);
1534 jt_ptl_drop_del(int argc, char **argv)
1536 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1540 jt_ptl_delay_del(int argc, char **argv)
1542 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1546 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1548 struct libcfs_ioctl_data data = {{0}};
1551 LIBCFS_IOC_INIT(data);
1552 data.ioc_flags = opc;
1554 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1556 fprintf(stderr, "failed to reset %s stats: %s\n",
1557 name, strerror(errno));
1564 jt_ptl_drop_reset(int argc, char **argv)
1566 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1570 jt_ptl_delay_reset(int argc, char **argv)
1572 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1577 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1579 struct libcfs_ioctl_data data = {{0}};
1580 struct lnet_fault_attr attr;
1581 struct lnet_fault_stat stat;
1584 printf("LNet %s rules:\n", name);
1585 for (pos = 0;; pos++) {
1588 memset(&attr, 0, sizeof(attr));
1589 memset(&stat, 0, sizeof(stat));
1591 data.ioc_count = pos;
1592 data.ioc_flags = opc;
1593 data.ioc_inllen1 = sizeof(attr);
1594 data.ioc_inlbuf1 = (char *)&attr;
1595 data.ioc_inllen2 = sizeof(stat);
1596 data.ioc_inlbuf2 = (char *)&stat;
1597 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1598 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1602 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1606 libcfs_ioctl_unpack(&data, ioc_buf);
1608 if (opc == LNET_CTL_DROP_LIST) {
1609 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1610 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1611 LPU64", REP "LPU64"\n",
1612 libcfs_nid2str(attr.fa_src),
1613 libcfs_nid2str(attr.fa_dst),
1614 attr.u.drop.da_rate, attr.u.drop.da_interval,
1615 attr.fa_ptl_mask, attr.fa_msg_mask,
1616 stat.u.drop.ds_dropped, stat.fs_count,
1617 stat.fs_put, stat.fs_ack,
1618 stat.fs_get, stat.fs_reply);
1620 } else if (opc == LNET_CTL_DELAY_LIST) {
1621 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1622 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1623 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1624 libcfs_nid2str(attr.fa_src),
1625 libcfs_nid2str(attr.fa_dst),
1626 attr.u.delay.la_rate, attr.u.delay.la_interval,
1627 attr.u.delay.la_latency,
1628 attr.fa_ptl_mask, attr.fa_msg_mask,
1629 stat.u.delay.ls_delayed, stat.fs_count,
1630 stat.fs_put, stat.fs_ack, stat.fs_get,
1634 printf("found total %d\n", pos);
1640 jt_ptl_drop_list(int argc, char **argv)
1642 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1646 jt_ptl_delay_list(int argc, char **argv)
1648 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1652 get_cycles_per_usec ()
1654 FILE *f = fopen ("/proc/cpuinfo", "r");
1659 while (fgets (line, sizeof (line), f) != NULL)
1660 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1667 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1671 int jt_ptl_memhog(int argc, char **argv)
1673 static int gfp = 0; /* sticky! */
1675 struct libcfs_ioctl_data data;
1681 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1685 count = strtol(argv[1], &end, 0);
1686 if (count < 0 || *end != 0) {
1687 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1692 rc = strtol(argv[2], &end, 0);
1694 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1700 LIBCFS_IOC_INIT(data);
1701 data.ioc_count = count;
1702 data.ioc_flags = gfp;
1703 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1706 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1710 printf("memhog %d OK\n", count);
1714 int jt_ptl_testprotocompat(int argc, char **argv)
1716 struct libcfs_ioctl_data data;
1722 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1726 flags = strtol(argv[1], &end, 0);
1727 if (flags < 0 || *end != 0) {
1728 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1732 LIBCFS_IOC_INIT(data);
1733 data.ioc_flags = flags;
1734 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1737 fprintf(stderr, "test proto compat %x failed: %s\n",
1738 flags, strerror(errno));
1742 printf("test proto compat %x OK\n", flags);