1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
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.
24 #include <sys/types.h>
28 #include <sys/socket.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
49 #include <libcfs/portals_utils.h>
50 #include <lnet/api-support.h>
51 #include <lnet/lnetctl.h>
52 #include <lnet/socklnd.h>
55 unsigned int libcfs_debug;
56 unsigned int libcfs_printk = D_CANTMASK;
61 /* Convert a string boolean to an int; "enable" -> 1 */
63 lnet_parse_bool (int *b, char *str)
65 if (!strcasecmp (str, "no") ||
66 !strcasecmp (str, "n") ||
67 !strcasecmp (str, "off") ||
68 !strcasecmp (str, "down") ||
69 !strcasecmp (str, "disable"))
75 if (!strcasecmp (str, "yes") ||
76 !strcasecmp (str, "y") ||
77 !strcasecmp (str, "on") ||
78 !strcasecmp (str, "up") ||
79 !strcasecmp (str, "enable"))
89 lnet_parse_port (int *port, char *str)
93 *port = strtol (str, &end, 0);
95 if (*end == 0 && /* parsed whole string */
96 *port > 0 && *port < 65536) /* minimal sanity check */
102 #ifdef HAVE_GETHOSTBYNAME
103 static struct hostent *
104 ptl_gethostbyname(char * hname) {
106 he = gethostbyname(hname);
111 fprintf(stderr, "Unable to resolve hostname: %s\n",
115 fprintf(stderr, "gethostbyname error for %s: %s\n",
116 hname, strerror(h_errno));
126 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
133 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
134 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
135 (c & ~0xff) == 0 && (d & ~0xff) == 0)
137 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
145 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
147 #ifdef HAVE_GETHOSTBYNAME
151 if (!strcmp (str, "_all_")) {
156 if (lnet_parse_ipquad(ipaddrp, str) == 0)
159 #ifdef HAVE_GETHOSTBYNAME
160 if ((('a' <= str[0] && str[0] <= 'z') ||
161 ('A' <= str[0] && str[0] <= 'Z')) &&
162 (he = ptl_gethostbyname (str)) != NULL) {
163 __u32 addr = *(__u32 *)he->h_addr;
165 *ipaddrp = ntohl(addr); /* HOST byte order */
174 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
176 #ifdef HAVE_GETHOSTBYNAME
181 net_ip = htonl (ipaddr);
182 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
184 strcpy(str, he->h_name);
190 sprintf (str, "%d.%d.%d.%d",
191 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
192 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
197 lnet_parse_time (time_t *t, char *str)
203 *t = strtol (str, &end, 0);
204 if (*end == 0) /* parsed whole string */
207 memset (&tm, 0, sizeof (tm));
208 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
209 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
210 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
214 tm.tm_mon--; /* convert to 0 == Jan */
215 tm.tm_year -= 1900; /* y2k quirk */
216 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
219 if (*t == (time_t)-1)
225 int g_net_is_set (char *cmd)
232 "You must run the 'network' command before '%s'.\n",
237 int g_net_is_compatible (char *cmd, ...)
242 if (!g_net_is_set(cmd))
248 nal = va_arg (ap, int);
249 if (nal == LNET_NETTYP(g_net)) {
259 "Command %s not compatible with %s NAL\n",
261 libcfs_lnd2str(LNET_NETTYP(g_net)));
265 int ptl_initialize(int argc, char **argv)
267 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
268 LNET_DEV_MAJOR, LNET_DEV_MINOR);
273 int jt_ptl_network(int argc, char **argv)
275 struct libcfs_ioctl_data data;
276 __u32 net = LNET_NIDNET(LNET_NID_ANY);
280 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
284 if (!strcmp(argv[1], "unconfigure") ||
285 !strcmp(argv[1], "down")) {
286 LIBCFS_IOC_INIT(data);
287 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
290 printf ("LNET ready to unload\n");
295 fprintf(stderr, "LNET busy\n");
297 fprintf(stderr, "LNET unconfigure error %d: %s\n",
298 errno, strerror(errno));
302 if (!strcmp(argv[1], "configure") ||
303 !strcmp(argv[1], "up")) {
304 LIBCFS_IOC_INIT(data);
305 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
308 printf ("LNET configured\n");
312 fprintf(stderr, "LNET configure error %d: %s\n",
313 errno, strerror(errno));
317 net = libcfs_str2net(argv[1]);
318 if (net == LNET_NIDNET(LNET_NID_ANY)) {
319 fprintf(stderr, "Can't parse net %s\n", argv[1]);
329 jt_ptl_list_nids(int argc, char **argv)
331 struct libcfs_ioctl_data data;
332 int all = 0, return_nid = 0;
336 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
337 /* Hack to pass back value */
338 return_nid = (argc == 2) && (argv[1][0] == 1);
340 if ((argc > 2) && !(all || return_nid)) {
341 fprintf(stderr, "usage: %s [all]\n", argv[0]);
345 for (count = 0;; count++) {
346 LIBCFS_IOC_INIT (data);
347 data.ioc_count = count;
348 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
351 if ((count > 0) && (errno == ENOENT))
352 /* We found them all */
354 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
355 errno, strerror(errno));
359 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
360 printf("%s\n", libcfs_nid2str(data.ioc_nid));
362 *(__u64 *)(argv[1]) = data.ioc_nid;
372 jt_ptl_which_nid (int argc, char **argv)
374 struct libcfs_ioctl_data data;
377 lnet_nid_t best_nid = LNET_NID_ANY;
386 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
390 for (i = 1; i < argc; i++) {
392 nid = libcfs_str2nid(nidstr);
393 if (nid == LNET_NID_ANY) {
394 fprintf(stderr, "Can't parse NID %s\n", nidstr);
398 LIBCFS_IOC_INIT(data);
401 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
403 fprintf(stderr, "Can't get distance to %s: %s\n",
404 nidstr, strerror(errno));
408 dist = data.ioc_u32[0];
409 order = data.ioc_u32[1];
412 if (dist == -EHOSTUNREACH)
415 fprintf(stderr, "Unexpected distance to %s: %d\n",
420 if (best_nid == LNET_NID_ANY ||
422 (dist == best_dist && order < best_order)) {
429 if (best_nid == LNET_NID_ANY) {
430 fprintf(stderr, "No reachable NID\n");
434 printf("%s\n", libcfs_nid2str(best_nid));
439 jt_ptl_print_interfaces (int argc, char **argv)
441 struct libcfs_ioctl_data data;
446 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
449 for (index = 0;;index++) {
450 LIBCFS_IOC_INIT(data);
451 data.ioc_net = g_net;
452 data.ioc_count = index;
454 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
458 printf ("%s: (%s/%s) npeer %d nroute %d\n",
459 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2], 1),
460 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0], 0),
461 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1], 0),
462 data.ioc_u32[2], data.ioc_u32[3]);
466 if (errno == ENOENT) {
467 printf ("<no interfaces>\n");
469 fprintf(stderr, "Error getting interfaces: %s: "
479 jt_ptl_add_interface (int argc, char **argv)
481 struct libcfs_ioctl_data data;
484 __u32 netmask = 0xffffff00;
489 if (argc < 2 || argc > 3) {
490 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
494 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
497 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
498 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
503 count = strtol(argv[2], &end, 0);
504 if (count > 0 && count < 32 && *end == 0) {
506 for (i = count; i > 0; i--)
507 netmask = netmask|(1<<(32-i));
508 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
509 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
514 LIBCFS_IOC_INIT(data);
515 data.ioc_net = g_net;
516 data.ioc_u32[0] = ipaddr;
517 data.ioc_u32[1] = netmask;
519 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
521 fprintf (stderr, "failed to add interface: %s\n",
530 jt_ptl_del_interface (int argc, char **argv)
532 struct libcfs_ioctl_data data;
537 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
541 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
545 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
546 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
550 LIBCFS_IOC_INIT(data);
551 data.ioc_net = g_net;
552 data.ioc_u32[0] = ipaddr;
554 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
556 fprintf (stderr, "failed to delete interface: %s\n",
565 jt_ptl_print_peers (int argc, char **argv)
567 struct libcfs_ioctl_data data;
568 lnet_process_id_t id;
573 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
574 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
577 for (index = 0;;index++) {
578 LIBCFS_IOC_INIT(data);
579 data.ioc_net = g_net;
580 data.ioc_count = index;
582 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
586 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
587 id.nid = data.ioc_nid;
588 id.pid = data.ioc_u32[4];
589 printf ("%-20s [%d]%s->%s:%d #%d\n",
591 data.ioc_count, /* persistence */
592 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* my ip */
593 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
594 data.ioc_u32[1], /* peer port */
595 data.ioc_u32[3]); /* conn_count */
596 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
597 id.nid = data.ioc_nid;
598 id.pid = data.ioc_u32[4];
599 printf ("%-20s s %d%s [%d] "LPD64".%06d"
600 " m "LPD64"/"LPD64" q %d/%d c %d/%d\n",
602 data.ioc_net, /* state */
603 data.ioc_flags ? "" : " ~!h", /* sent_hello */
604 data.ioc_count, /* refcount */
605 data.ioc_u64[0]/1000000, /* incarnation secs */
606 (int)(data.ioc_u64[0]%1000000), /* incarnation usecs */
607 (((__u64)data.ioc_u32[1])<<32) |
608 ((__u64)data.ioc_u32[0]), /* next_matchbits */
609 (((__u64)data.ioc_u32[3])<<32) |
610 ((__u64)data.ioc_u32[2]), /* last_matchbits_seen */
611 data.ioc_u32[5] >> 16, /* nsendq */
612 data.ioc_u32[5] & 0xffff, /* nactiveq */
613 data.ioc_u32[6] >> 16, /* credits */
614 data.ioc_u32[6] & 0xffff); /* outstanding_credits */
615 } else if (g_net_is_compatible(NULL, RALND, OPENIBLND, CIBLND, VIBLND, 0)) {
616 printf ("%-20s [%d]@%s:%d\n",
617 libcfs_nid2str(data.ioc_nid), /* peer nid */
618 data.ioc_count, /* peer persistence */
619 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
620 data.ioc_u32[1]); /* peer port */
622 printf ("%-20s [%d]\n",
623 libcfs_nid2str(data.ioc_nid), data.ioc_count);
628 if (errno == ENOENT) {
629 printf ("<no peers>\n");
631 fprintf(stderr, "Error getting peer list: %s: "
640 jt_ptl_add_peer (int argc, char **argv)
642 struct libcfs_ioctl_data data;
648 if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
649 OPENIBLND, CIBLND, IIBLND, VIBLND, 0))
652 if (g_net_is_compatible(NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0)) {
654 fprintf (stderr, "usage(tcp,openib,cib,ra): %s nid ipaddr port\n",
658 } else if (g_net_is_compatible(NULL, VIBLND, 0)) {
660 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
664 } else if (argc != 2) {
665 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
669 nid = libcfs_str2nid(argv[1]);
670 if (nid == LNET_NID_ANY) {
671 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
675 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, VIBLND, RALND, 0) &&
676 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
677 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
681 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0) &&
682 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;
713 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
714 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
717 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
719 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
723 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
725 fprintf (stderr, "usage: %s [nid] [pid]\n",
729 } else if (argc > 2) {
730 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
735 !libcfs_str2anynid(&nid, argv[1])) {
736 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
740 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
742 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
743 fprintf (stderr, "Can't parse ip addr: %s\n",
747 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
749 pid = strtol(argv[2], &end, 0);
750 if (end == argv[2] || *end == 0) {
751 fprintf(stderr, "Can't parse pid %s\n",
758 LIBCFS_IOC_INIT(data);
759 data.ioc_net = g_net;
761 data.ioc_u32[0] = ip;
762 data.ioc_u32[1] = pid;
764 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
766 fprintf (stderr, "failed to remove peer: %s\n",
775 jt_ptl_print_connections (int argc, char **argv)
777 struct libcfs_ioctl_data data;
778 lnet_process_id_t id;
783 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
784 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
787 for (index = 0; ; index++) {
788 LIBCFS_IOC_INIT(data);
789 data.ioc_net = g_net;
790 data.ioc_count = index;
792 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
796 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
797 id.nid = data.ioc_nid;
798 id.pid = data.ioc_u32[6];
799 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
801 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
802 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
803 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
804 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
805 data.ioc_u32[4], /* scheduler */
806 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* local IP addr */
807 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* remote IP addr */
808 data.ioc_u32[1], /* remote port */
809 data.ioc_count, /* tx buffer size */
810 data.ioc_u32[5], /* rx buffer size */
811 data.ioc_flags ? "nagle" : "nonagle");
812 } else if (g_net_is_compatible (NULL, RALND, 0)) {
813 printf ("%-20s [%d]\n",
814 libcfs_nid2str(data.ioc_nid),
815 data.ioc_u32[0] /* device id */);
816 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
817 printf ("%s mtu %d\n",
818 libcfs_nid2str(data.ioc_nid),
819 data.ioc_u32[0]); /* path MTU */
821 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
826 if (errno == ENOENT) {
827 printf ("<no connections>\n");
829 fprintf(stderr, "Error getting connection list: %s: "
837 int jt_ptl_disconnect(int argc, char **argv)
839 struct libcfs_ioctl_data data;
840 lnet_nid_t nid = LNET_NID_ANY;
845 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
849 if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND,
850 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
854 !libcfs_str2anynid(&nid, argv[1])) {
855 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
859 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
861 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
862 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
866 LIBCFS_IOC_INIT(data);
867 data.ioc_net = g_net;
869 data.ioc_u32[0] = ipaddr;
871 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
873 fprintf(stderr, "failed to remove connection: %s\n",
881 int jt_ptl_push_connection (int argc, char **argv)
883 struct libcfs_ioctl_data data;
885 lnet_nid_t nid = LNET_NID_ANY;
888 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
892 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
896 !libcfs_str2anynid(&nid, argv[1])) {
897 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
901 LIBCFS_IOC_INIT(data);
902 data.ioc_net = g_net;
905 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
907 fprintf(stderr, "failed to push connection: %s\n",
916 jt_ptl_print_active_txs (int argc, char **argv)
918 struct libcfs_ioctl_data data;
922 if (!g_net_is_compatible (argv[0], QSWLND, 0))
925 for (index = 0;;index++) {
926 LIBCFS_IOC_INIT(data);
927 data.ioc_net = g_net;
928 data.ioc_count = index;
930 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
934 printf ("type %u payload %6d to %s via %s by pid %6d: "
935 "%s, %s, state %d\n",
938 libcfs_nid2str(data.ioc_nid),
939 libcfs_nid2str(data.ioc_u64[0]),
941 (data.ioc_flags & 1) ? "delayed" : "immediate",
942 (data.ioc_flags & 2) ? "nblk" : "normal",
943 data.ioc_flags >> 2);
947 if (errno == ENOENT) {
948 printf ("<no active descs>\n");
950 fprintf(stderr, "Error getting active transmits list: "
951 "%s: check dmesg.\n",
958 int jt_ptl_ping(int argc, char **argv)
962 lnet_process_id_t id;
963 lnet_process_id_t ids[16];
964 int maxids = sizeof(ids)/sizeof(ids[0]);
965 struct libcfs_ioctl_data data;
970 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
974 sep = strchr(argv[1], '-');
976 id.pid = LNET_PID_ANY;
977 id.nid = libcfs_str2nid(argv[1]);
978 if (id.nid == LNET_NID_ANY) {
979 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
985 if (argv[1][0] == 'u' ||
987 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
989 id.pid = strtoul(argv[1], &end, 0);
991 id.nid = libcfs_str2nid(sep + 1);
994 id.nid == LNET_NID_ANY) {
995 fprintf(stderr, "Can't parse process id \"%s\"\n", argv[1]);
1001 timeout = 1000 * atol(argv[2]);
1003 timeout = 1000; /* default 1 second timeout */
1005 LIBCFS_IOC_INIT (data);
1006 data.ioc_nid = id.nid;
1007 data.ioc_u32[0] = id.pid;
1008 data.ioc_u32[1] = timeout;
1009 data.ioc_plen1 = sizeof(ids);
1010 data.ioc_pbuf1 = (char *)ids;
1012 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
1014 fprintf(stderr, "failed to ping %s: %s\n",
1015 id.pid == LNET_PID_ANY ?
1016 libcfs_nid2str(id.nid) : libcfs_id2str(id),
1021 for (i = 0; i < data.ioc_count && i < maxids; i++)
1022 printf("%s\n", libcfs_id2str(ids[i]));
1024 if (data.ioc_count > maxids)
1025 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1030 int jt_ptl_mynid(int argc, char **argv)
1032 struct libcfs_ioctl_data data;
1037 fprintf(stderr, "usage: %s NID\n", argv[0]);
1041 nid = libcfs_str2nid(argv[1]);
1042 if (nid == LNET_NID_ANY) {
1043 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1047 LIBCFS_IOC_INIT(data);
1048 data.ioc_net = LNET_NIDNET(nid);
1051 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1053 fprintf(stderr, "setting my NID failed: %s\n",
1056 printf("registered my nid %s\n", libcfs_nid2str(nid));
1062 jt_ptl_fail_nid (int argc, char **argv)
1066 unsigned int threshold;
1067 struct libcfs_ioctl_data data;
1069 if (argc < 2 || argc > 3)
1071 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1075 if (!libcfs_str2anynid(&nid, argv[1]))
1077 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1082 threshold = LNET_MD_THRESH_INF;
1083 } else if (sscanf (argv[2], "%i", &threshold) != 1) {
1084 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1088 LIBCFS_IOC_INIT (data);
1090 data.ioc_count = threshold;
1092 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1094 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1097 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1103 jt_ptl_add_route (int argc, char **argv)
1105 struct libcfs_ioctl_data data;
1106 lnet_nid_t gateway_nid;
1107 unsigned int hops = 1;
1111 if (argc < 2 || argc > 3)
1113 fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
1117 if (!g_net_is_set(argv[0]))
1120 gateway_nid = libcfs_str2nid(argv[1]);
1121 if (gateway_nid == LNET_NID_ANY) {
1122 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1127 hops = strtoul(argv[2], &end, 0);
1128 if (hops >= 256 || *end != 0) {
1129 fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
1134 LIBCFS_IOC_INIT(data);
1135 data.ioc_net = g_net;
1136 data.ioc_count = hops;
1137 data.ioc_nid = gateway_nid;
1139 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1141 fprintf (stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n", strerror (errno));
1149 jt_ptl_del_route (int argc, char **argv)
1151 struct libcfs_ioctl_data data;
1156 fprintf (stderr, "usage: %s gatewayNID\n", argv[0]);
1160 if (!libcfs_str2anynid(&nid, argv[1])) {
1161 fprintf (stderr, "Can't parse gateway NID "
1162 "\"%s\"\n", argv[1]);
1166 LIBCFS_IOC_INIT(data);
1167 data.ioc_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1170 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1172 fprintf (stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1173 libcfs_nid2str(nid), strerror (errno));
1181 jt_ptl_notify_router (int argc, char **argv)
1183 struct libcfs_ioctl_data data;
1192 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1197 nid = libcfs_str2nid(argv[1]);
1198 if (nid == LNET_NID_ANY) {
1199 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1203 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1204 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1208 gettimeofday(&now, NULL);
1212 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1213 fprintf(stderr, "Can't parse time %s\n"
1214 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1215 "or an absolute unix time in seconds\n", argv[3]);
1217 } else if (when > now.tv_sec) {
1218 fprintf (stderr, "%s specifies a time in the future\n",
1223 LIBCFS_IOC_INIT(data);
1225 data.ioc_flags = enable;
1226 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1227 data.ioc_u64[0] = (__u64)when;
1229 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1231 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1232 libcfs_nid2str(nid), strerror (errno));
1240 jt_ptl_print_routes (int argc, char **argv)
1242 struct libcfs_ioctl_data data;
1250 for (index = 0;;index++)
1252 LIBCFS_IOC_INIT(data);
1253 data.ioc_count = index;
1255 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1260 hops = data.ioc_count;
1262 alive = data.ioc_flags;
1264 printf ("net %18s hops %u gw %32s %s\n",
1265 libcfs_net2str(net), hops,
1266 libcfs_nid2str(nid), alive ? "up" : "down");
1269 if (errno != ENOENT)
1270 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1277 lwt_control(int enable, int clear)
1279 struct libcfs_ioctl_data data;
1282 LIBCFS_IOC_INIT(data);
1283 data.ioc_flags = (enable ? 1 : 0) | (clear ? 2 : 0);
1285 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_CONTROL, &data);
1289 fprintf(stderr, "IOC_LIBCFS_LWT_CONTROL failed: %s\n",
1295 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1296 lwt_event_t *events, int size)
1298 struct libcfs_ioctl_data data;
1301 LIBCFS_IOC_INIT(data);
1302 data.ioc_pbuf1 = (char *)events;
1303 data.ioc_plen1 = size;
1305 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_SNAPSHOT, &data);
1307 fprintf(stderr, "IOC_LIBCFS_LWT_SNAPSHOT failed: %s\n",
1312 /* crappy overloads */
1313 if (data.ioc_u32[2] != sizeof(lwt_event_t) ||
1314 data.ioc_u32[3] != offsetof(lwt_event_t, lwte_where)) {
1315 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1316 (int)data.ioc_u32[2], (int)sizeof(lwt_event_t),
1317 (int)data.ioc_u32[3],
1318 (int)offsetof(lwt_event_t, lwte_where));
1323 *now = data.ioc_u64[0];
1325 LASSERT (data.ioc_u32[0] != 0);
1327 *ncpu = data.ioc_u32[0];
1329 LASSERT (data.ioc_u32[1] != 0);
1330 if (totalsize != NULL)
1331 *totalsize = data.ioc_u32[1];
1337 lwt_get_string(char *kstr)
1340 struct libcfs_ioctl_data data;
1344 /* FIXME: this could maintain a symbol table since we expect to be
1345 * looking up the same strings all the time... */
1347 LIBCFS_IOC_INIT(data);
1348 data.ioc_pbuf1 = kstr;
1349 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1350 data.ioc_pbuf2 = NULL;
1353 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1355 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1360 size = data.ioc_count;
1361 ustr = (char *)malloc(size);
1363 fprintf(stderr, "Can't allocate string storage of size %d\n",
1368 LIBCFS_IOC_INIT(data);
1369 data.ioc_pbuf1 = kstr;
1370 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1371 data.ioc_pbuf2 = ustr;
1372 data.ioc_plen2 = size;
1374 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1376 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1381 LASSERT(strlen(ustr) == size - 1);
1386 lwt_put_string(char *ustr)
1392 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1395 # error "__WORDSIZE not defined"
1396 #elif __WORDSIZE == 32
1397 # define XFMT "%#010lx"
1398 #elif __WORDSIZE== 64
1399 # define XFMT "%#018lx"
1401 # error "Unexpected __WORDSIZE"
1403 char *where = lwt_get_string(e->lwte_where);
1408 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1409 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1410 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1411 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1414 lwt_put_string(where);
1421 get_cycles_per_usec ()
1423 FILE *f = fopen ("/proc/cpuinfo", "r");
1428 while (fgets (line, sizeof (line), f) != NULL)
1429 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1436 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1441 jt_ptl_lwt(int argc, char **argv)
1443 const int lwt_max_cpus = 32;
1446 int nevents_per_cpu;
1447 lwt_event_t *events;
1448 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1449 lwt_event_t *next_event[lwt_max_cpus];
1450 lwt_event_t *first_event[lwt_max_cpus];
1459 struct timeval tvnow;
1460 int printed_date = 0;
1465 (strcmp(argv[1], "start") &&
1466 strcmp(argv[1], "stop"))) {
1469 " %s stop [fname]\n", argv[0], argv[0]);
1473 if (!strcmp(argv[1], "start")) {
1475 if (lwt_control(0, 0) != 0)
1479 if (lwt_control(0, 1) != 0)
1483 if (lwt_control(1, 0) != 0)
1489 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1492 if (ncpus > lwt_max_cpus) {
1493 fprintf(stderr, "Too many cpus: %d (%d)\n",
1494 ncpus, lwt_max_cpus);
1498 events = (lwt_event_t *)malloc(totalspace);
1499 if (events == NULL) {
1500 fprintf(stderr, "Can't allocate %d\n", totalspace);
1504 if (lwt_control(0, 0) != 0) { /* disable */
1509 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1514 /* we want this time to be sampled at snapshot time */
1515 gettimeofday(&tvnow, NULL);
1518 f = fopen (argv[2], "w");
1520 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1526 mhz = get_cycles_per_usec();
1528 /* carve events into per-cpu slices */
1529 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1530 for (cpu = 0; cpu <= ncpus; cpu++)
1531 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1533 /* find the earliest event on each cpu */
1534 for (cpu = 0; cpu < ncpus; cpu++) {
1535 first_event[cpu] = NULL;
1537 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1539 if (e->lwte_where == NULL) /* not an event */
1542 if (first_event[cpu] == NULL ||
1543 first_event[cpu]->lwte_when > e->lwte_when)
1544 first_event[cpu] = e;
1547 next_event[cpu] = first_event[cpu];
1551 for (cpu = 0; cpu < ncpus; cpu++) {
1552 e = first_event[cpu];
1553 if (e == NULL) /* no events this cpu */
1556 if (e == cpu_event[cpu])
1557 e = cpu_event[cpu + 1] - 1;
1561 /* If there's an event immediately before the first one, this
1562 * cpu wrapped its event buffer */
1563 if (e->lwte_where == NULL)
1566 /* We should only start outputting events from the most recent
1567 * first event in any wrapped cpu. Events before this time on
1568 * other cpus won't have any events from this CPU to interleave
1570 if (t0 < first_event[cpu]->lwte_when)
1571 t0 = first_event[cpu]->lwte_when;
1575 /* find which cpu has the next event */
1577 for (i = 0; i < ncpus; i++) {
1579 if (next_event[i] == NULL) /* this cpu exhausted */
1583 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1587 if (cpu < 0) /* all cpus exhausted */
1591 /* no wrapped cpus and this is he first ever event */
1592 t0 = next_event[cpu]->lwte_when;
1595 if (t0 <= next_event[cpu]->lwte_when) {
1596 /* on or after the first event */
1597 if (!printed_date) {
1598 cycles_t du = (tnow - t0) / mhz;
1599 time_t then = tvnow.tv_sec - du/1000000;
1601 if (du % 1000000 > tvnow.tv_usec)
1604 fprintf(f, "%s", ctime(&then));
1608 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1612 if (++nlines % 10000 == 0 && f != stdout) {
1613 /* show some activity... */
1619 tlast = next_event[cpu]->lwte_when;
1622 if (next_event[cpu] == cpu_event[cpu + 1])
1623 next_event[cpu] = cpu_event[cpu];
1625 if (next_event[cpu]->lwte_where == NULL ||
1626 next_event[cpu] == first_event[cpu])
1627 next_event[cpu] = NULL;
1639 int jt_ptl_memhog(int argc, char **argv)
1641 static int gfp = 0; /* sticky! */
1643 struct libcfs_ioctl_data data;
1649 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1653 count = strtol(argv[1], &end, 0);
1654 if (count < 0 || *end != 0) {
1655 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1660 rc = strtol(argv[2], &end, 0);
1662 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1668 LIBCFS_IOC_INIT(data);
1669 data.ioc_count = count;
1670 data.ioc_flags = gfp;
1671 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1674 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1678 printf("memhog %d OK\n", count);
1682 int jt_ptl_testprotocompat(int argc, char **argv)
1684 struct libcfs_ioctl_data data;
1690 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1694 flags = strtol(argv[1], &end, 0);
1695 if (flags < 0 || *end != 0) {
1696 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1700 LIBCFS_IOC_INIT(data);
1701 data.ioc_flags = flags;
1702 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1705 fprintf(stderr, "test proto compat %x failed: %s\n",
1706 flags, strerror(errno));
1710 printf("test proto compat %x OK\n", flags);