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/libcfsutil.h>
50 #include <lnet/api-support.h>
51 #include <lnet/lnetctl.h>
52 #include <lnet/socklnd.h>
54 unsigned int libcfs_debug;
55 unsigned int libcfs_printk = D_CANTMASK;
60 /* Convert a string boolean to an int; "enable" -> 1 */
62 lnet_parse_bool (int *b, char *str)
64 if (!strcasecmp (str, "no") ||
65 !strcasecmp (str, "n") ||
66 !strcasecmp (str, "off") ||
67 !strcasecmp (str, "down") ||
68 !strcasecmp (str, "disable"))
74 if (!strcasecmp (str, "yes") ||
75 !strcasecmp (str, "y") ||
76 !strcasecmp (str, "on") ||
77 !strcasecmp (str, "up") ||
78 !strcasecmp (str, "enable"))
88 lnet_parse_port (int *port, char *str)
92 *port = strtol (str, &end, 0);
94 if (*end == 0 && /* parsed whole string */
95 *port > 0 && *port < 65536) /* minimal sanity check */
101 #ifdef HAVE_GETHOSTBYNAME
102 static struct hostent *
103 ptl_gethostbyname(char * hname) {
105 he = gethostbyname(hname);
110 fprintf(stderr, "Unable to resolve hostname: %s\n",
114 fprintf(stderr, "gethostbyname error for %s: %s\n",
115 hname, strerror(h_errno));
125 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
132 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
133 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
134 (c & ~0xff) == 0 && (d & ~0xff) == 0)
136 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
144 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
146 #ifdef HAVE_GETHOSTBYNAME
150 if (!strcmp (str, "_all_")) {
155 if (lnet_parse_ipquad(ipaddrp, str) == 0)
158 #ifdef HAVE_GETHOSTBYNAME
159 if ((('a' <= str[0] && str[0] <= 'z') ||
160 ('A' <= str[0] && str[0] <= 'Z')) &&
161 (he = ptl_gethostbyname (str)) != NULL) {
162 __u32 addr = *(__u32 *)he->h_addr;
164 *ipaddrp = ntohl(addr); /* HOST byte order */
173 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
175 #ifdef HAVE_GETHOSTBYNAME
180 net_ip = htonl (ipaddr);
181 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
183 strcpy(str, he->h_name);
189 sprintf (str, "%d.%d.%d.%d",
190 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
191 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
196 lnet_parse_time (time_t *t, char *str)
202 *t = strtol (str, &end, 0);
203 if (*end == 0) /* parsed whole string */
206 memset (&tm, 0, sizeof (tm));
207 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
208 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
209 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
213 tm.tm_mon--; /* convert to 0 == Jan */
214 tm.tm_year -= 1900; /* y2k quirk */
215 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
218 if (*t == (time_t)-1)
224 int g_net_is_set (char *cmd)
231 "You must run the 'network' command before '%s'.\n",
236 int g_net_is_compatible (char *cmd, ...)
241 if (!g_net_is_set(cmd))
247 nal = va_arg (ap, int);
248 if (nal == LNET_NETTYP(g_net)) {
258 "Command %s not compatible with %s NAL\n",
260 libcfs_lnd2str(LNET_NETTYP(g_net)));
264 int ptl_initialize(int argc, char **argv)
266 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
267 LNET_DEV_MAJOR, LNET_DEV_MINOR);
272 int jt_ptl_network(int argc, char **argv)
274 struct libcfs_ioctl_data data;
275 __u32 net = LNET_NIDNET(LNET_NID_ANY);
279 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
283 if (!strcmp(argv[1], "unconfigure") ||
284 !strcmp(argv[1], "down")) {
285 LIBCFS_IOC_INIT(data);
286 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
289 printf ("LNET ready to unload\n");
294 fprintf(stderr, "LNET busy\n");
296 fprintf(stderr, "LNET unconfigure error %d: %s\n",
297 errno, strerror(errno));
301 if (!strcmp(argv[1], "configure") ||
302 !strcmp(argv[1], "up")) {
303 LIBCFS_IOC_INIT(data);
304 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
307 printf ("LNET configured\n");
311 fprintf(stderr, "LNET configure error %d: %s\n",
312 errno, strerror(errno));
316 net = libcfs_str2net(argv[1]);
317 if (net == LNET_NIDNET(LNET_NID_ANY)) {
318 fprintf(stderr, "Can't parse net %s\n", argv[1]);
328 jt_ptl_list_nids(int argc, char **argv)
330 struct libcfs_ioctl_data data;
331 int all = 0, return_nid = 0;
335 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
336 /* Hack to pass back value */
337 return_nid = (argc == 2) && (argv[1][0] == 1);
339 if ((argc > 2) && !(all || return_nid)) {
340 fprintf(stderr, "usage: %s [all]\n", argv[0]);
344 for (count = 0;; count++) {
345 LIBCFS_IOC_INIT (data);
346 data.ioc_count = count;
347 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
350 if ((count > 0) && (errno == ENOENT))
351 /* We found them all */
353 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
354 errno, strerror(errno));
358 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
359 printf("%s\n", libcfs_nid2str(data.ioc_nid));
361 *(__u64 *)(argv[1]) = data.ioc_nid;
371 jt_ptl_which_nid (int argc, char **argv)
373 struct libcfs_ioctl_data data;
376 lnet_nid_t best_nid = LNET_NID_ANY;
385 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
389 for (i = 1; i < argc; i++) {
391 nid = libcfs_str2nid(nidstr);
392 if (nid == LNET_NID_ANY) {
393 fprintf(stderr, "Can't parse NID %s\n", nidstr);
397 LIBCFS_IOC_INIT(data);
400 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
402 fprintf(stderr, "Can't get distance to %s: %s\n",
403 nidstr, strerror(errno));
407 dist = data.ioc_u32[0];
408 order = data.ioc_u32[1];
411 if (dist == -EHOSTUNREACH)
414 fprintf(stderr, "Unexpected distance to %s: %d\n",
419 if (best_nid == LNET_NID_ANY ||
421 (dist == best_dist && order < best_order)) {
428 if (best_nid == LNET_NID_ANY) {
429 fprintf(stderr, "No reachable NID\n");
433 printf("%s\n", libcfs_nid2str(best_nid));
438 jt_ptl_print_interfaces (int argc, char **argv)
440 struct libcfs_ioctl_data data;
445 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
448 for (index = 0;;index++) {
449 LIBCFS_IOC_INIT(data);
450 data.ioc_net = g_net;
451 data.ioc_count = index;
453 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
457 printf ("%s: (%s/%s) npeer %d nroute %d\n",
458 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2], 1),
459 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0], 0),
460 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1], 0),
461 data.ioc_u32[2], data.ioc_u32[3]);
465 if (errno == ENOENT) {
466 printf ("<no interfaces>\n");
468 fprintf(stderr, "Error getting interfaces: %s: "
478 jt_ptl_add_interface (int argc, char **argv)
480 struct libcfs_ioctl_data data;
483 __u32 netmask = 0xffffff00;
488 if (argc < 2 || argc > 3) {
489 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
493 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
496 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
497 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
502 count = strtol(argv[2], &end, 0);
503 if (count > 0 && count < 32 && *end == 0) {
505 for (i = count; i > 0; i--)
506 netmask = netmask|(1<<(32-i));
507 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
508 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
513 LIBCFS_IOC_INIT(data);
514 data.ioc_net = g_net;
515 data.ioc_u32[0] = ipaddr;
516 data.ioc_u32[1] = netmask;
518 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
520 fprintf (stderr, "failed to add interface: %s\n",
529 jt_ptl_del_interface (int argc, char **argv)
531 struct libcfs_ioctl_data data;
536 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
540 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
544 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
545 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
549 LIBCFS_IOC_INIT(data);
550 data.ioc_net = g_net;
551 data.ioc_u32[0] = ipaddr;
553 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
555 fprintf (stderr, "failed to delete interface: %s\n",
564 jt_ptl_print_peers (int argc, char **argv)
566 struct libcfs_ioctl_data data;
567 lnet_process_id_t id;
572 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
573 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
576 for (index = 0;;index++) {
577 LIBCFS_IOC_INIT(data);
578 data.ioc_net = g_net;
579 data.ioc_count = index;
581 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
585 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
586 id.nid = data.ioc_nid;
587 id.pid = data.ioc_u32[4];
588 printf ("%-20s [%d]%s->%s:%d #%d\n",
590 data.ioc_count, /* persistence */
591 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* my ip */
592 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
593 data.ioc_u32[1], /* peer port */
594 data.ioc_u32[3]); /* conn_count */
595 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
596 id.nid = data.ioc_nid;
597 id.pid = data.ioc_u32[4];
598 printf ("%-20s s %d%s [%d] "LPD64".%06d"
599 " m "LPD64"/"LPD64" q %d/%d c %d/%d\n",
601 data.ioc_net, /* state */
602 data.ioc_flags ? "" : " ~!h", /* sent_hello */
603 data.ioc_count, /* refcount */
604 data.ioc_u64[0]/1000000, /* incarnation secs */
605 (int)(data.ioc_u64[0]%1000000), /* incarnation usecs */
606 (((__u64)data.ioc_u32[1])<<32) |
607 ((__u64)data.ioc_u32[0]), /* next_matchbits */
608 (((__u64)data.ioc_u32[3])<<32) |
609 ((__u64)data.ioc_u32[2]), /* last_matchbits_seen */
610 data.ioc_u32[5] >> 16, /* nsendq */
611 data.ioc_u32[5] & 0xffff, /* nactiveq */
612 data.ioc_u32[6] >> 16, /* credits */
613 data.ioc_u32[6] & 0xffff); /* outstanding_credits */
614 } else if (g_net_is_compatible(NULL, RALND, OPENIBLND, CIBLND, VIBLND, 0)) {
615 printf ("%-20s [%d]@%s:%d\n",
616 libcfs_nid2str(data.ioc_nid), /* peer nid */
617 data.ioc_count, /* peer persistence */
618 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
619 data.ioc_u32[1]); /* peer port */
621 printf ("%-20s [%d]\n",
622 libcfs_nid2str(data.ioc_nid), data.ioc_count);
627 if (errno == ENOENT) {
628 printf ("<no peers>\n");
630 fprintf(stderr, "Error getting peer list: %s: "
639 jt_ptl_add_peer (int argc, char **argv)
641 struct libcfs_ioctl_data data;
647 if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
648 OPENIBLND, CIBLND, IIBLND, VIBLND, 0))
651 if (g_net_is_compatible(NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0)) {
653 fprintf (stderr, "usage(tcp,openib,cib,ra): %s nid ipaddr port\n",
657 } else if (g_net_is_compatible(NULL, VIBLND, 0)) {
659 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
663 } else if (argc != 2) {
664 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
668 nid = libcfs_str2nid(argv[1]);
669 if (nid == LNET_NID_ANY) {
670 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
674 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, VIBLND, RALND, 0) &&
675 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
676 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
680 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0) &&
681 lnet_parse_port (&port, argv[3]) != 0) {
682 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
686 LIBCFS_IOC_INIT(data);
687 data.ioc_net = g_net;
689 data.ioc_u32[0] = ip;
690 data.ioc_u32[1] = port;
692 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
694 fprintf (stderr, "failed to add peer: %s\n",
703 jt_ptl_del_peer (int argc, char **argv)
705 struct libcfs_ioctl_data data;
706 lnet_nid_t nid = LNET_NID_ANY;
707 lnet_pid_t pid = LNET_PID_ANY;
712 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
713 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
716 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
718 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
722 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
724 fprintf (stderr, "usage: %s [nid] [pid]\n",
728 } else if (argc > 2) {
729 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
734 !libcfs_str2anynid(&nid, argv[1])) {
735 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
739 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
741 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
742 fprintf (stderr, "Can't parse ip addr: %s\n",
746 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
748 pid = strtol(argv[2], &end, 0);
749 if (end == argv[2] || *end == 0) {
750 fprintf(stderr, "Can't parse pid %s\n",
757 LIBCFS_IOC_INIT(data);
758 data.ioc_net = g_net;
760 data.ioc_u32[0] = ip;
761 data.ioc_u32[1] = pid;
763 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
765 fprintf (stderr, "failed to remove peer: %s\n",
774 jt_ptl_print_connections (int argc, char **argv)
776 struct libcfs_ioctl_data data;
777 lnet_process_id_t id;
782 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
783 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
786 for (index = 0; ; index++) {
787 LIBCFS_IOC_INIT(data);
788 data.ioc_net = g_net;
789 data.ioc_count = index;
791 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
795 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
796 id.nid = data.ioc_nid;
797 id.pid = data.ioc_u32[6];
798 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
800 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
801 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
802 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
803 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
804 data.ioc_u32[4], /* scheduler */
805 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* local IP addr */
806 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* remote IP addr */
807 data.ioc_u32[1], /* remote port */
808 data.ioc_count, /* tx buffer size */
809 data.ioc_u32[5], /* rx buffer size */
810 data.ioc_flags ? "nagle" : "nonagle");
811 } else if (g_net_is_compatible (NULL, RALND, 0)) {
812 printf ("%-20s [%d]\n",
813 libcfs_nid2str(data.ioc_nid),
814 data.ioc_u32[0] /* device id */);
816 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
821 if (errno == ENOENT) {
822 printf ("<no connections>\n");
824 fprintf(stderr, "Error getting connection list: %s: "
832 int jt_ptl_disconnect(int argc, char **argv)
834 struct libcfs_ioctl_data data;
835 lnet_nid_t nid = LNET_NID_ANY;
840 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
844 if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND,
845 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
849 !libcfs_str2anynid(&nid, argv[1])) {
850 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
854 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
856 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
857 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
861 LIBCFS_IOC_INIT(data);
862 data.ioc_net = g_net;
864 data.ioc_u32[0] = ipaddr;
866 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
868 fprintf(stderr, "failed to remove connection: %s\n",
876 int jt_ptl_push_connection (int argc, char **argv)
878 struct libcfs_ioctl_data data;
880 lnet_nid_t nid = LNET_NID_ANY;
883 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
887 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
891 !libcfs_str2anynid(&nid, argv[1])) {
892 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
896 LIBCFS_IOC_INIT(data);
897 data.ioc_net = g_net;
900 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
902 fprintf(stderr, "failed to push connection: %s\n",
911 jt_ptl_print_active_txs (int argc, char **argv)
913 struct libcfs_ioctl_data data;
917 if (!g_net_is_compatible (argv[0], QSWLND, 0))
920 for (index = 0;;index++) {
921 LIBCFS_IOC_INIT(data);
922 data.ioc_net = g_net;
923 data.ioc_count = index;
925 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
929 printf ("type %u payload %6d to %s via %s by pid %6d: "
930 "%s, %s, state %d\n",
933 libcfs_nid2str(data.ioc_nid),
934 libcfs_nid2str(data.ioc_u64[0]),
936 (data.ioc_flags & 1) ? "delayed" : "immediate",
937 (data.ioc_flags & 2) ? "nblk" : "normal",
938 data.ioc_flags >> 2);
942 if (errno == ENOENT) {
943 printf ("<no active descs>\n");
945 fprintf(stderr, "Error getting active transmits list: "
946 "%s: check dmesg.\n",
953 int jt_ptl_ping(int argc, char **argv)
957 lnet_process_id_t id;
958 lnet_process_id_t ids[16];
959 int maxids = sizeof(ids)/sizeof(ids[0]);
960 struct libcfs_ioctl_data data;
965 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
969 sep = strchr(argv[1], '-');
971 id.pid = LNET_PID_ANY;
972 id.nid = libcfs_str2nid(argv[1]);
973 if (id.nid == LNET_NID_ANY) {
974 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
980 if (argv[1][0] == 'u' ||
982 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
984 id.pid = strtoul(argv[1], &end, 0);
986 id.nid = libcfs_str2nid(sep + 1);
989 id.nid == LNET_NID_ANY) {
990 fprintf(stderr, "Can't parse process id \"%s\"\n", argv[1]);
996 timeout = 1000 * atol(argv[2]);
998 timeout = 1000; /* default 1 second timeout */
1000 LIBCFS_IOC_INIT (data);
1001 data.ioc_nid = id.nid;
1002 data.ioc_u32[0] = id.pid;
1003 data.ioc_u32[1] = timeout;
1004 data.ioc_plen1 = sizeof(ids);
1005 data.ioc_pbuf1 = (char *)ids;
1007 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
1009 fprintf(stderr, "failed to ping %s: %s\n",
1010 id.pid == LNET_PID_ANY ?
1011 libcfs_nid2str(id.nid) : libcfs_id2str(id),
1016 for (i = 0; i < data.ioc_count && i < maxids; i++)
1017 printf("%s\n", libcfs_id2str(ids[i]));
1019 if (data.ioc_count > maxids)
1020 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1025 int jt_ptl_mynid(int argc, char **argv)
1027 struct libcfs_ioctl_data data;
1032 fprintf(stderr, "usage: %s NID\n", argv[0]);
1036 nid = libcfs_str2nid(argv[1]);
1037 if (nid == LNET_NID_ANY) {
1038 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1042 LIBCFS_IOC_INIT(data);
1043 data.ioc_net = LNET_NIDNET(nid);
1046 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1048 fprintf(stderr, "setting my NID failed: %s\n",
1051 printf("registered my nid %s\n", libcfs_nid2str(nid));
1057 jt_ptl_fail_nid (int argc, char **argv)
1061 unsigned int threshold;
1062 struct libcfs_ioctl_data data;
1064 if (argc < 2 || argc > 3)
1066 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1070 if (!libcfs_str2anynid(&nid, argv[1]))
1072 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1077 threshold = LNET_MD_THRESH_INF;
1078 } else if (sscanf (argv[2], "%i", &threshold) != 1) {
1079 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1083 LIBCFS_IOC_INIT (data);
1085 data.ioc_count = threshold;
1087 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1089 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1092 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1098 jt_ptl_add_route (int argc, char **argv)
1100 struct libcfs_ioctl_data data;
1101 lnet_nid_t gateway_nid;
1102 unsigned int hops = 1;
1106 if (argc < 2 || argc > 3)
1108 fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
1112 if (!g_net_is_set(argv[0]))
1115 gateway_nid = libcfs_str2nid(argv[1]);
1116 if (gateway_nid == LNET_NID_ANY) {
1117 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1122 hops = strtoul(argv[2], &end, 0);
1123 if (hops >= 256 || *end != 0) {
1124 fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
1129 LIBCFS_IOC_INIT(data);
1130 data.ioc_net = g_net;
1131 data.ioc_count = hops;
1132 data.ioc_nid = gateway_nid;
1134 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1136 fprintf (stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n", strerror (errno));
1144 jt_ptl_del_route (int argc, char **argv)
1146 struct libcfs_ioctl_data data;
1151 fprintf (stderr, "usage: %s gatewayNID\n", argv[0]);
1155 if (!libcfs_str2anynid(&nid, argv[1])) {
1156 fprintf (stderr, "Can't parse gateway NID "
1157 "\"%s\"\n", argv[1]);
1161 LIBCFS_IOC_INIT(data);
1162 data.ioc_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1165 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1167 fprintf (stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1168 libcfs_nid2str(nid), strerror (errno));
1176 jt_ptl_notify_router (int argc, char **argv)
1178 struct libcfs_ioctl_data data;
1187 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1192 nid = libcfs_str2nid(argv[1]);
1193 if (nid == LNET_NID_ANY) {
1194 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1198 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1199 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1203 gettimeofday(&now, NULL);
1207 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1208 fprintf(stderr, "Can't parse time %s\n"
1209 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1210 "or an absolute unix time in seconds\n", argv[3]);
1212 } else if (when > now.tv_sec) {
1213 fprintf (stderr, "%s specifies a time in the future\n",
1218 LIBCFS_IOC_INIT(data);
1220 data.ioc_flags = enable;
1221 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1222 data.ioc_u64[0] = (__u64)when;
1224 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1226 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1227 libcfs_nid2str(nid), strerror (errno));
1235 jt_ptl_print_routes (int argc, char **argv)
1237 struct libcfs_ioctl_data data;
1245 for (index = 0;;index++)
1247 LIBCFS_IOC_INIT(data);
1248 data.ioc_count = index;
1250 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1255 hops = data.ioc_count;
1257 alive = data.ioc_flags;
1259 printf ("net %18s hops %u gw %32s %s\n",
1260 libcfs_net2str(net), hops,
1261 libcfs_nid2str(nid), alive ? "up" : "down");
1264 if (errno != ENOENT)
1265 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1272 lwt_control(int enable, int clear)
1274 struct libcfs_ioctl_data data;
1277 LIBCFS_IOC_INIT(data);
1278 data.ioc_flags = (enable ? 1 : 0) | (clear ? 2 : 0);
1280 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_CONTROL, &data);
1284 fprintf(stderr, "IOC_LIBCFS_LWT_CONTROL failed: %s\n",
1290 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1291 lwt_event_t *events, int size)
1293 struct libcfs_ioctl_data data;
1296 LIBCFS_IOC_INIT(data);
1297 data.ioc_pbuf1 = (char *)events;
1298 data.ioc_plen1 = size;
1300 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_SNAPSHOT, &data);
1302 fprintf(stderr, "IOC_LIBCFS_LWT_SNAPSHOT failed: %s\n",
1307 /* crappy overloads */
1308 if (data.ioc_u32[2] != sizeof(lwt_event_t) ||
1309 data.ioc_u32[3] != offsetof(lwt_event_t, lwte_where)) {
1310 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1311 (int)data.ioc_u32[2], (int)sizeof(lwt_event_t),
1312 (int)data.ioc_u32[3],
1313 (int)offsetof(lwt_event_t, lwte_where));
1318 *now = data.ioc_u64[0];
1320 LASSERT (data.ioc_u32[0] != 0);
1322 *ncpu = data.ioc_u32[0];
1324 LASSERT (data.ioc_u32[1] != 0);
1325 if (totalsize != NULL)
1326 *totalsize = data.ioc_u32[1];
1332 lwt_get_string(char *kstr)
1335 struct libcfs_ioctl_data data;
1339 /* FIXME: this could maintain a symbol table since we expect to be
1340 * looking up the same strings all the time... */
1342 LIBCFS_IOC_INIT(data);
1343 data.ioc_pbuf1 = kstr;
1344 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1345 data.ioc_pbuf2 = NULL;
1348 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1350 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1355 size = data.ioc_count;
1356 ustr = (char *)malloc(size);
1358 fprintf(stderr, "Can't allocate string storage of size %d\n",
1363 LIBCFS_IOC_INIT(data);
1364 data.ioc_pbuf1 = kstr;
1365 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1366 data.ioc_pbuf2 = ustr;
1367 data.ioc_plen2 = size;
1369 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1371 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1376 LASSERT(strlen(ustr) == size - 1);
1381 lwt_put_string(char *ustr)
1387 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1390 # error "__WORDSIZE not defined"
1391 #elif __WORDSIZE == 32
1392 # define XFMT "%#010lx"
1393 #elif __WORDSIZE== 64
1394 # define XFMT "%#018lx"
1396 # error "Unexpected __WORDSIZE"
1398 char *where = lwt_get_string(e->lwte_where);
1403 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1404 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1405 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1406 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1409 lwt_put_string(where);
1416 get_cycles_per_usec ()
1418 FILE *f = fopen ("/proc/cpuinfo", "r");
1423 while (fgets (line, sizeof (line), f) != NULL)
1424 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1431 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1436 jt_ptl_lwt(int argc, char **argv)
1438 const int lwt_max_cpus = 32;
1441 int nevents_per_cpu;
1442 lwt_event_t *events;
1443 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1444 lwt_event_t *next_event[lwt_max_cpus];
1445 lwt_event_t *first_event[lwt_max_cpus];
1454 struct timeval tvnow;
1455 int printed_date = 0;
1460 (strcmp(argv[1], "start") &&
1461 strcmp(argv[1], "stop"))) {
1464 " %s stop [fname]\n", argv[0], argv[0]);
1468 if (!strcmp(argv[1], "start")) {
1470 if (lwt_control(0, 0) != 0)
1474 if (lwt_control(0, 1) != 0)
1478 if (lwt_control(1, 0) != 0)
1484 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1487 if (ncpus > lwt_max_cpus) {
1488 fprintf(stderr, "Too many cpus: %d (%d)\n",
1489 ncpus, lwt_max_cpus);
1493 events = (lwt_event_t *)malloc(totalspace);
1494 if (events == NULL) {
1495 fprintf(stderr, "Can't allocate %d\n", totalspace);
1499 if (lwt_control(0, 0) != 0) { /* disable */
1504 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1509 /* we want this time to be sampled at snapshot time */
1510 gettimeofday(&tvnow, NULL);
1513 f = fopen (argv[2], "w");
1515 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1521 mhz = get_cycles_per_usec();
1523 /* carve events into per-cpu slices */
1524 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1525 for (cpu = 0; cpu <= ncpus; cpu++)
1526 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1528 /* find the earliest event on each cpu */
1529 for (cpu = 0; cpu < ncpus; cpu++) {
1530 first_event[cpu] = NULL;
1532 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1534 if (e->lwte_where == NULL) /* not an event */
1537 if (first_event[cpu] == NULL ||
1538 first_event[cpu]->lwte_when > e->lwte_when)
1539 first_event[cpu] = e;
1542 next_event[cpu] = first_event[cpu];
1546 for (cpu = 0; cpu < ncpus; cpu++) {
1547 e = first_event[cpu];
1548 if (e == NULL) /* no events this cpu */
1551 if (e == cpu_event[cpu])
1552 e = cpu_event[cpu + 1] - 1;
1556 /* If there's an event immediately before the first one, this
1557 * cpu wrapped its event buffer */
1558 if (e->lwte_where == NULL)
1561 /* We should only start outputting events from the most recent
1562 * first event in any wrapped cpu. Events before this time on
1563 * other cpus won't have any events from this CPU to interleave
1565 if (t0 < first_event[cpu]->lwte_when)
1566 t0 = first_event[cpu]->lwte_when;
1570 /* find which cpu has the next event */
1572 for (i = 0; i < ncpus; i++) {
1574 if (next_event[i] == NULL) /* this cpu exhausted */
1578 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1582 if (cpu < 0) /* all cpus exhausted */
1586 /* no wrapped cpus and this is he first ever event */
1587 t0 = next_event[cpu]->lwte_when;
1590 if (t0 <= next_event[cpu]->lwte_when) {
1591 /* on or after the first event */
1592 if (!printed_date) {
1593 cycles_t du = (tnow - t0) / mhz;
1594 time_t then = tvnow.tv_sec - du/1000000;
1596 if (du % 1000000 > tvnow.tv_usec)
1599 fprintf(f, "%s", ctime(&then));
1603 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1607 if (++nlines % 10000 == 0 && f != stdout) {
1608 /* show some activity... */
1614 tlast = next_event[cpu]->lwte_when;
1617 if (next_event[cpu] == cpu_event[cpu + 1])
1618 next_event[cpu] = cpu_event[cpu];
1620 if (next_event[cpu]->lwte_where == NULL ||
1621 next_event[cpu] == first_event[cpu])
1622 next_event[cpu] = NULL;
1634 int jt_ptl_memhog(int argc, char **argv)
1636 static int gfp = 0; /* sticky! */
1638 struct libcfs_ioctl_data data;
1644 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1648 count = strtol(argv[1], &end, 0);
1649 if (count < 0 || *end != 0) {
1650 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1655 rc = strtol(argv[2], &end, 0);
1657 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1663 LIBCFS_IOC_INIT(data);
1664 data.ioc_count = count;
1665 data.ioc_flags = gfp;
1666 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1669 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1673 printf("memhog %d OK\n", count);
1677 int jt_ptl_testprotocompat(int argc, char **argv)
1679 struct libcfs_ioctl_data data;
1685 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1689 flags = strtol(argv[1], &end, 0);
1690 if (flags < 0 || *end != 0) {
1691 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1695 LIBCFS_IOC_INIT(data);
1696 data.ioc_flags = flags;
1697 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1700 fprintf(stderr, "test proto compat %x failed: %s\n",
1701 flags, strerror(errno));
1705 printf("test proto compat %x OK\n", flags);