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.
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/api-support.h>
25 #include <lnet/lnetctl.h>
26 #include <lnet/socklnd.h>
28 unsigned int libcfs_debug;
29 unsigned int libcfs_printk = D_CANTMASK;
34 /* Convert a string boolean to an int; "enable" -> 1 */
36 lnet_parse_bool (int *b, char *str)
38 if (!strcasecmp (str, "no") ||
39 !strcasecmp (str, "n") ||
40 !strcasecmp (str, "off") ||
41 !strcasecmp (str, "down") ||
42 !strcasecmp (str, "disable"))
48 if (!strcasecmp (str, "yes") ||
49 !strcasecmp (str, "y") ||
50 !strcasecmp (str, "on") ||
51 !strcasecmp (str, "up") ||
52 !strcasecmp (str, "enable"))
62 lnet_parse_port (int *port, char *str)
66 *port = strtol (str, &end, 0);
68 if (*end == 0 && /* parsed whole string */
69 *port > 0 && *port < 65536) /* minimal sanity check */
75 #ifdef HAVE_GETHOSTBYNAME
76 static struct hostent *
77 ptl_gethostbyname(char * hname) {
79 he = gethostbyname(hname);
84 fprintf(stderr, "Unable to resolve hostname: %s\n",
88 fprintf(stderr, "gethostbyname error for %s: %s\n",
89 hname, strerror(h_errno));
99 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
106 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
107 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
108 (c & ~0xff) == 0 && (d & ~0xff) == 0)
110 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
118 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
120 #ifdef HAVE_GETHOSTBYNAME
124 if (!strcmp (str, "_all_")) {
129 if (lnet_parse_ipquad(ipaddrp, str) == 0)
132 #ifdef HAVE_GETHOSTBYNAME
133 if ((('a' <= str[0] && str[0] <= 'z') ||
134 ('A' <= str[0] && str[0] <= 'Z')) &&
135 (he = ptl_gethostbyname (str)) != NULL) {
136 __u32 addr = *(__u32 *)he->h_addr;
138 *ipaddrp = ntohl(addr); /* HOST byte order */
147 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
149 #ifdef HAVE_GETHOSTBYNAME
154 net_ip = htonl (ipaddr);
155 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
157 strcpy(str, he->h_name);
163 sprintf (str, "%d.%d.%d.%d",
164 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
165 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
170 lnet_parse_time (time_t *t, char *str)
176 *t = strtol (str, &end, 0);
177 if (*end == 0) /* parsed whole string */
180 memset (&tm, 0, sizeof (tm));
181 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
182 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
183 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
187 tm.tm_mon--; /* convert to 0 == Jan */
188 tm.tm_year -= 1900; /* y2k quirk */
189 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
192 if (*t == (time_t)-1)
198 int g_net_is_set (char *cmd)
205 "You must run the 'network' command before '%s'.\n",
210 int g_net_is_compatible (char *cmd, ...)
215 if (!g_net_is_set(cmd))
221 nal = va_arg (ap, int);
222 if (nal == LNET_NETTYP(g_net)) {
232 "Command %s not compatible with %s NAL\n",
234 libcfs_lnd2str(LNET_NETTYP(g_net)));
238 int ptl_initialize(int argc, char **argv)
240 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
241 LNET_DEV_MAJOR, LNET_DEV_MINOR);
246 int jt_ptl_network(int argc, char **argv)
248 struct libcfs_ioctl_data data;
249 __u32 net = LNET_NIDNET(LNET_NID_ANY);
253 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
257 if (!strcmp(argv[1], "unconfigure") ||
258 !strcmp(argv[1], "down")) {
259 LIBCFS_IOC_INIT(data);
260 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
263 printf ("LNET ready to unload\n");
268 fprintf(stderr, "LNET busy\n");
270 fprintf(stderr, "LNET unconfigure error %d: %s\n",
271 errno, strerror(errno));
275 if (!strcmp(argv[1], "configure") ||
276 !strcmp(argv[1], "up")) {
277 LIBCFS_IOC_INIT(data);
278 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
281 printf ("LNET configured\n");
285 fprintf(stderr, "LNET configure error %d: %s\n",
286 errno, strerror(errno));
290 net = libcfs_str2net(argv[1]);
291 if (net == LNET_NIDNET(LNET_NID_ANY)) {
292 fprintf(stderr, "Can't parse net %s\n", argv[1]);
302 jt_ptl_list_nids(int argc, char **argv)
304 struct libcfs_ioctl_data data;
305 int all = 0, return_nid = 0;
309 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
310 /* Hack to pass back value */
311 return_nid = (argc == 2) && (argv[1][0] == 1);
313 if ((argc > 2) && !(all || return_nid)) {
314 fprintf(stderr, "usage: %s [all]\n", argv[0]);
318 for (count = 0;; count++) {
319 LIBCFS_IOC_INIT (data);
320 data.ioc_count = count;
321 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
324 if ((count > 0) && (errno == ENOENT))
325 /* We found them all */
327 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
328 errno, strerror(errno));
332 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
333 printf("%s\n", libcfs_nid2str(data.ioc_nid));
335 *(__u64 *)(argv[1]) = data.ioc_nid;
345 jt_ptl_which_nid (int argc, char **argv)
347 struct libcfs_ioctl_data data;
350 lnet_nid_t best_nid = LNET_NID_ANY;
359 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
363 for (i = 1; i < argc; i++) {
365 nid = libcfs_str2nid(nidstr);
366 if (nid == LNET_NID_ANY) {
367 fprintf(stderr, "Can't parse NID %s\n", nidstr);
371 LIBCFS_IOC_INIT(data);
374 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
376 fprintf(stderr, "Can't get distance to %s: %s\n",
377 nidstr, strerror(errno));
381 dist = data.ioc_u32[0];
382 order = data.ioc_u32[1];
385 if (dist == -EHOSTUNREACH)
388 fprintf(stderr, "Unexpected distance to %s: %d\n",
393 if (best_nid == LNET_NID_ANY ||
395 (dist == best_dist && order < best_order)) {
402 if (best_nid == LNET_NID_ANY) {
403 fprintf(stderr, "No reachable NID\n");
407 printf("%s\n", libcfs_nid2str(best_nid));
412 jt_ptl_print_interfaces (int argc, char **argv)
414 struct libcfs_ioctl_data data;
419 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
422 for (index = 0;;index++) {
423 LIBCFS_IOC_INIT(data);
424 data.ioc_net = g_net;
425 data.ioc_count = index;
427 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
431 printf ("%s: (%s/%s) npeer %d nroute %d\n",
432 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2], 1),
433 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0], 0),
434 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1], 0),
435 data.ioc_u32[2], data.ioc_u32[3]);
439 if (errno == ENOENT) {
440 printf ("<no interfaces>\n");
442 fprintf(stderr, "Error getting interfaces: %s: "
452 jt_ptl_add_interface (int argc, char **argv)
454 struct libcfs_ioctl_data data;
457 __u32 netmask = 0xffffff00;
462 if (argc < 2 || argc > 3) {
463 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
467 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
470 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
471 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
476 count = strtol(argv[2], &end, 0);
477 if (count > 0 && count < 32 && *end == 0) {
479 for (i = count; i > 0; i--)
480 netmask = netmask|(1<<(32-i));
481 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
482 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
487 LIBCFS_IOC_INIT(data);
488 data.ioc_net = g_net;
489 data.ioc_u32[0] = ipaddr;
490 data.ioc_u32[1] = netmask;
492 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
494 fprintf (stderr, "failed to add interface: %s\n",
503 jt_ptl_del_interface (int argc, char **argv)
505 struct libcfs_ioctl_data data;
510 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
514 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
518 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
519 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
523 LIBCFS_IOC_INIT(data);
524 data.ioc_net = g_net;
525 data.ioc_u32[0] = ipaddr;
527 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
529 fprintf (stderr, "failed to delete interface: %s\n",
538 jt_ptl_print_peers (int argc, char **argv)
540 struct libcfs_ioctl_data data;
541 lnet_process_id_t id;
546 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
547 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
550 for (index = 0;;index++) {
551 LIBCFS_IOC_INIT(data);
552 data.ioc_net = g_net;
553 data.ioc_count = index;
555 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
559 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
560 id.nid = data.ioc_nid;
561 id.pid = data.ioc_u32[4];
562 printf ("%-20s [%d]%s->%s:%d #%d\n",
564 data.ioc_count, /* persistence */
565 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* my ip */
566 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
567 data.ioc_u32[1], /* peer port */
568 data.ioc_u32[3]); /* conn_count */
569 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
570 id.nid = data.ioc_nid;
571 id.pid = data.ioc_u32[4];
572 printf ("%-20s s %d%s [%d] "LPD64".%06d"
573 " m "LPD64"/"LPD64" q %d/%d c %d/%d\n",
575 data.ioc_net, /* state */
576 data.ioc_flags ? "" : " ~!h", /* sent_hello */
577 data.ioc_count, /* refcount */
578 data.ioc_u64[0]/1000000, /* incarnation secs */
579 (int)(data.ioc_u64[0]%1000000), /* incarnation usecs */
580 (((__u64)data.ioc_u32[1])<<32) |
581 ((__u64)data.ioc_u32[0]), /* next_matchbits */
582 (((__u64)data.ioc_u32[3])<<32) |
583 ((__u64)data.ioc_u32[2]), /* last_matchbits_seen */
584 data.ioc_u32[5] >> 16, /* nsendq */
585 data.ioc_u32[5] & 0xffff, /* nactiveq */
586 data.ioc_u32[6] >> 16, /* credits */
587 data.ioc_u32[6] & 0xffff); /* outstanding_credits */
588 } else if (g_net_is_compatible(NULL, RALND, OPENIBLND, CIBLND, VIBLND, 0)) {
589 printf ("%-20s [%d]@%s:%d\n",
590 libcfs_nid2str(data.ioc_nid), /* peer nid */
591 data.ioc_count, /* peer persistence */
592 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
593 data.ioc_u32[1]); /* peer port */
595 printf ("%-20s [%d]\n",
596 libcfs_nid2str(data.ioc_nid), data.ioc_count);
601 if (errno == ENOENT) {
602 printf ("<no peers>\n");
604 fprintf(stderr, "Error getting peer list: %s: "
613 jt_ptl_add_peer (int argc, char **argv)
615 struct libcfs_ioctl_data data;
621 if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
622 OPENIBLND, CIBLND, IIBLND, VIBLND, 0))
625 if (g_net_is_compatible(NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0)) {
627 fprintf (stderr, "usage(tcp,openib,cib,ra): %s nid ipaddr port\n",
631 } else if (g_net_is_compatible(NULL, VIBLND, 0)) {
633 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
637 } else if (argc != 2) {
638 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
642 nid = libcfs_str2nid(argv[1]);
643 if (nid == LNET_NID_ANY) {
644 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
648 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, VIBLND, RALND, 0) &&
649 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
650 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
654 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0) &&
655 lnet_parse_port (&port, argv[3]) != 0) {
656 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
660 LIBCFS_IOC_INIT(data);
661 data.ioc_net = g_net;
663 data.ioc_u32[0] = ip;
664 data.ioc_u32[1] = port;
666 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
668 fprintf (stderr, "failed to add peer: %s\n",
677 jt_ptl_del_peer (int argc, char **argv)
679 struct libcfs_ioctl_data data;
680 lnet_nid_t nid = LNET_NID_ANY;
681 lnet_pid_t pid = LNET_PID_ANY;
686 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
687 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
690 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
692 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
696 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
698 fprintf (stderr, "usage: %s [nid] [pid]\n",
702 } else if (argc > 2) {
703 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
708 !libcfs_str2anynid(&nid, argv[1])) {
709 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
713 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
715 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
716 fprintf (stderr, "Can't parse ip addr: %s\n",
720 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
722 pid = strtol(argv[2], &end, 0);
723 if (end == argv[2] || *end == 0) {
724 fprintf(stderr, "Can't parse pid %s\n",
731 LIBCFS_IOC_INIT(data);
732 data.ioc_net = g_net;
734 data.ioc_u32[0] = ip;
735 data.ioc_u32[1] = pid;
737 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
739 fprintf (stderr, "failed to remove peer: %s\n",
748 jt_ptl_print_connections (int argc, char **argv)
750 struct libcfs_ioctl_data data;
751 lnet_process_id_t id;
756 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
757 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
760 for (index = 0; ; index++) {
761 LIBCFS_IOC_INIT(data);
762 data.ioc_net = g_net;
763 data.ioc_count = index;
765 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
769 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
770 id.nid = data.ioc_nid;
771 id.pid = data.ioc_u32[6];
772 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
774 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
775 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
776 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
777 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
778 data.ioc_u32[4], /* scheduler */
779 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* local IP addr */
780 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* remote IP addr */
781 data.ioc_u32[1], /* remote port */
782 data.ioc_count, /* tx buffer size */
783 data.ioc_u32[5], /* rx buffer size */
784 data.ioc_flags ? "nagle" : "nonagle");
785 } else if (g_net_is_compatible (NULL, RALND, 0)) {
786 printf ("%-20s [%d]\n",
787 libcfs_nid2str(data.ioc_nid),
788 data.ioc_u32[0] /* device id */);
790 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
795 if (errno == ENOENT) {
796 printf ("<no connections>\n");
798 fprintf(stderr, "Error getting connection list: %s: "
806 int jt_ptl_disconnect(int argc, char **argv)
808 struct libcfs_ioctl_data data;
809 lnet_nid_t nid = LNET_NID_ANY;
814 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
818 if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND,
819 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
823 !libcfs_str2anynid(&nid, argv[1])) {
824 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
828 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
830 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
831 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
835 LIBCFS_IOC_INIT(data);
836 data.ioc_net = g_net;
838 data.ioc_u32[0] = ipaddr;
840 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
842 fprintf(stderr, "failed to remove connection: %s\n",
850 int jt_ptl_push_connection (int argc, char **argv)
852 struct libcfs_ioctl_data data;
854 lnet_nid_t nid = LNET_NID_ANY;
857 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
861 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
865 !libcfs_str2anynid(&nid, argv[1])) {
866 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
870 LIBCFS_IOC_INIT(data);
871 data.ioc_net = g_net;
874 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
876 fprintf(stderr, "failed to push connection: %s\n",
885 jt_ptl_print_active_txs (int argc, char **argv)
887 struct libcfs_ioctl_data data;
891 if (!g_net_is_compatible (argv[0], QSWLND, 0))
894 for (index = 0;;index++) {
895 LIBCFS_IOC_INIT(data);
896 data.ioc_net = g_net;
897 data.ioc_count = index;
899 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
903 printf ("type %u payload %6d to %s via %s by pid %6d: "
904 "%s, %s, state %d\n",
907 libcfs_nid2str(data.ioc_nid),
908 libcfs_nid2str(data.ioc_u64[0]),
910 (data.ioc_flags & 1) ? "delayed" : "immediate",
911 (data.ioc_flags & 2) ? "nblk" : "normal",
912 data.ioc_flags >> 2);
916 if (errno == ENOENT) {
917 printf ("<no active descs>\n");
919 fprintf(stderr, "Error getting active transmits list: "
920 "%s: check dmesg.\n",
927 int jt_ptl_ping(int argc, char **argv)
931 lnet_process_id_t id;
932 lnet_process_id_t ids[16];
933 int maxids = sizeof(ids)/sizeof(ids[0]);
934 struct libcfs_ioctl_data data;
939 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
943 sep = strchr(argv[1], '-');
945 id.pid = LNET_PID_ANY;
946 id.nid = libcfs_str2nid(argv[1]);
947 if (id.nid == LNET_NID_ANY) {
948 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
954 if (argv[1][0] == 'u' ||
956 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
958 id.pid = strtoul(argv[1], &end, 0);
960 id.nid = libcfs_str2nid(sep + 1);
963 id.nid == LNET_NID_ANY) {
964 fprintf(stderr, "Can't parse process id \"%s\"\n", argv[1]);
970 timeout = 1000 * atol(argv[2]);
972 timeout = 1000; /* default 1 second timeout */
974 LIBCFS_IOC_INIT (data);
975 data.ioc_nid = id.nid;
976 data.ioc_u32[0] = id.pid;
977 data.ioc_u32[1] = timeout;
978 data.ioc_plen1 = sizeof(ids);
979 data.ioc_pbuf1 = (char *)ids;
981 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
983 fprintf(stderr, "failed to ping %s: %s\n",
984 id.pid == LNET_PID_ANY ?
985 libcfs_nid2str(id.nid) : libcfs_id2str(id),
990 for (i = 0; i < data.ioc_count && i < maxids; i++)
991 printf("%s\n", libcfs_id2str(ids[i]));
993 if (data.ioc_count > maxids)
994 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
999 int jt_ptl_mynid(int argc, char **argv)
1001 struct libcfs_ioctl_data data;
1006 fprintf(stderr, "usage: %s NID\n", argv[0]);
1010 nid = libcfs_str2nid(argv[1]);
1011 if (nid == LNET_NID_ANY) {
1012 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1016 LIBCFS_IOC_INIT(data);
1017 data.ioc_net = LNET_NIDNET(nid);
1020 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1022 fprintf(stderr, "setting my NID failed: %s\n",
1025 printf("registered my nid %s\n", libcfs_nid2str(nid));
1031 jt_ptl_fail_nid (int argc, char **argv)
1035 unsigned int threshold;
1036 struct libcfs_ioctl_data data;
1038 if (argc < 2 || argc > 3)
1040 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1044 if (!libcfs_str2anynid(&nid, argv[1]))
1046 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1051 threshold = LNET_MD_THRESH_INF;
1052 } else if (sscanf (argv[2], "%i", &threshold) != 1) {
1053 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1057 LIBCFS_IOC_INIT (data);
1059 data.ioc_count = threshold;
1061 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1063 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1066 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1072 jt_ptl_add_route (int argc, char **argv)
1074 struct libcfs_ioctl_data data;
1075 lnet_nid_t gateway_nid;
1076 unsigned int hops = 1;
1080 if (argc < 2 || argc > 3)
1082 fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
1086 if (!g_net_is_set(argv[0]))
1089 gateway_nid = libcfs_str2nid(argv[1]);
1090 if (gateway_nid == LNET_NID_ANY) {
1091 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1096 hops = strtoul(argv[2], &end, 0);
1097 if (hops >= 256 || *end != 0) {
1098 fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
1103 LIBCFS_IOC_INIT(data);
1104 data.ioc_net = g_net;
1105 data.ioc_count = hops;
1106 data.ioc_nid = gateway_nid;
1108 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1110 fprintf (stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n", strerror (errno));
1118 jt_ptl_del_route (int argc, char **argv)
1120 struct libcfs_ioctl_data data;
1125 fprintf (stderr, "usage: %s gatewayNID\n", argv[0]);
1129 if (!libcfs_str2anynid(&nid, argv[1])) {
1130 fprintf (stderr, "Can't parse gateway NID "
1131 "\"%s\"\n", argv[1]);
1135 LIBCFS_IOC_INIT(data);
1136 data.ioc_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1139 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1141 fprintf (stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1142 libcfs_nid2str(nid), strerror (errno));
1150 jt_ptl_notify_router (int argc, char **argv)
1152 struct libcfs_ioctl_data data;
1161 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1166 nid = libcfs_str2nid(argv[1]);
1167 if (nid == LNET_NID_ANY) {
1168 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1172 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1173 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1177 gettimeofday(&now, NULL);
1181 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1182 fprintf(stderr, "Can't parse time %s\n"
1183 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1184 "or an absolute unix time in seconds\n", argv[3]);
1186 } else if (when > now.tv_sec) {
1187 fprintf (stderr, "%s specifies a time in the future\n",
1192 LIBCFS_IOC_INIT(data);
1194 data.ioc_flags = enable;
1195 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1196 data.ioc_u64[0] = (__u64)when;
1198 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1200 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1201 libcfs_nid2str(nid), strerror (errno));
1209 jt_ptl_print_routes (int argc, char **argv)
1211 struct libcfs_ioctl_data data;
1219 for (index = 0;;index++)
1221 LIBCFS_IOC_INIT(data);
1222 data.ioc_count = index;
1224 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1229 hops = data.ioc_count;
1231 alive = data.ioc_flags;
1233 printf ("net %18s hops %u gw %32s %s\n",
1234 libcfs_net2str(net), hops,
1235 libcfs_nid2str(nid), alive ? "up" : "down");
1238 if (errno != ENOENT)
1239 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1246 lwt_control(int enable, int clear)
1248 struct libcfs_ioctl_data data;
1251 LIBCFS_IOC_INIT(data);
1252 data.ioc_flags = (enable ? 1 : 0) | (clear ? 2 : 0);
1254 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_CONTROL, &data);
1258 fprintf(stderr, "IOC_LIBCFS_LWT_CONTROL failed: %s\n",
1264 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1265 lwt_event_t *events, int size)
1267 struct libcfs_ioctl_data data;
1270 LIBCFS_IOC_INIT(data);
1271 data.ioc_pbuf1 = (char *)events;
1272 data.ioc_plen1 = size;
1274 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_SNAPSHOT, &data);
1276 fprintf(stderr, "IOC_LIBCFS_LWT_SNAPSHOT failed: %s\n",
1281 /* crappy overloads */
1282 if (data.ioc_u32[2] != sizeof(lwt_event_t) ||
1283 data.ioc_u32[3] != offsetof(lwt_event_t, lwte_where)) {
1284 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1285 (int)data.ioc_u32[2], (int)sizeof(lwt_event_t),
1286 (int)data.ioc_u32[3],
1287 (int)offsetof(lwt_event_t, lwte_where));
1292 *now = data.ioc_u64[0];
1294 LASSERT (data.ioc_u32[0] != 0);
1296 *ncpu = data.ioc_u32[0];
1298 LASSERT (data.ioc_u32[1] != 0);
1299 if (totalsize != NULL)
1300 *totalsize = data.ioc_u32[1];
1306 lwt_get_string(char *kstr)
1309 struct libcfs_ioctl_data data;
1313 /* FIXME: this could maintain a symbol table since we expect to be
1314 * looking up the same strings all the time... */
1316 LIBCFS_IOC_INIT(data);
1317 data.ioc_pbuf1 = kstr;
1318 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1319 data.ioc_pbuf2 = NULL;
1322 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1324 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1329 size = data.ioc_count;
1330 ustr = (char *)malloc(size);
1332 fprintf(stderr, "Can't allocate string storage of size %d\n",
1337 LIBCFS_IOC_INIT(data);
1338 data.ioc_pbuf1 = kstr;
1339 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1340 data.ioc_pbuf2 = ustr;
1341 data.ioc_plen2 = size;
1343 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1345 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1350 LASSERT(strlen(ustr) == size - 1);
1355 lwt_put_string(char *ustr)
1361 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1364 # error "__WORDSIZE not defined"
1365 #elif __WORDSIZE == 32
1366 # define XFMT "%#010lx"
1367 #elif __WORDSIZE== 64
1368 # define XFMT "%#018lx"
1370 # error "Unexpected __WORDSIZE"
1372 char *where = lwt_get_string(e->lwte_where);
1377 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1378 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1379 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1380 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1383 lwt_put_string(where);
1390 get_cycles_per_usec ()
1392 FILE *f = fopen ("/proc/cpuinfo", "r");
1397 while (fgets (line, sizeof (line), f) != NULL)
1398 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1405 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1409 #define LWT_MAX_CPUS (32)
1412 jt_ptl_lwt(int argc, char **argv)
1416 int nevents_per_cpu;
1417 lwt_event_t *events;
1418 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1419 lwt_event_t *next_event[LWT_MAX_CPUS];
1420 lwt_event_t *first_event[LWT_MAX_CPUS];
1429 struct timeval tvnow;
1430 int printed_date = 0;
1435 (strcmp(argv[1], "start") &&
1436 strcmp(argv[1], "stop"))) {
1439 " %s stop [fname]\n", argv[0], argv[0]);
1443 if (!strcmp(argv[1], "start")) {
1445 if (lwt_control(0, 0) != 0)
1449 if (lwt_control(0, 1) != 0)
1453 if (lwt_control(1, 0) != 0)
1459 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1462 if (ncpus > LWT_MAX_CPUS) {
1463 fprintf(stderr, "Too many cpus: %d (%d)\n",
1464 ncpus, LWT_MAX_CPUS);
1468 events = (lwt_event_t *)malloc(totalspace);
1469 if (events == NULL) {
1470 fprintf(stderr, "Can't allocate %d\n", totalspace);
1474 if (lwt_control(0, 0) != 0) { /* disable */
1479 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1484 /* we want this time to be sampled at snapshot time */
1485 gettimeofday(&tvnow, NULL);
1488 f = fopen (argv[2], "w");
1490 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1496 mhz = get_cycles_per_usec();
1498 /* carve events into per-cpu slices */
1499 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1500 for (cpu = 0; cpu <= ncpus; cpu++)
1501 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1503 /* find the earliest event on each cpu */
1504 for (cpu = 0; cpu < ncpus; cpu++) {
1505 first_event[cpu] = NULL;
1507 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1509 if (e->lwte_where == NULL) /* not an event */
1512 if (first_event[cpu] == NULL ||
1513 first_event[cpu]->lwte_when > e->lwte_when)
1514 first_event[cpu] = e;
1517 next_event[cpu] = first_event[cpu];
1521 for (cpu = 0; cpu < ncpus; cpu++) {
1522 e = first_event[cpu];
1523 if (e == NULL) /* no events this cpu */
1526 if (e == cpu_event[cpu])
1527 e = cpu_event[cpu + 1] - 1;
1531 /* If there's an event immediately before the first one, this
1532 * cpu wrapped its event buffer */
1533 if (e->lwte_where == NULL)
1536 /* We should only start outputting events from the most recent
1537 * first event in any wrapped cpu. Events before this time on
1538 * other cpus won't have any events from this CPU to interleave
1540 if (t0 < first_event[cpu]->lwte_when)
1541 t0 = first_event[cpu]->lwte_when;
1545 /* find which cpu has the next event */
1547 for (i = 0; i < ncpus; i++) {
1549 if (next_event[i] == NULL) /* this cpu exhausted */
1553 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1557 if (cpu < 0) /* all cpus exhausted */
1561 /* no wrapped cpus and this is he first ever event */
1562 t0 = next_event[cpu]->lwte_when;
1565 if (t0 <= next_event[cpu]->lwte_when) {
1566 /* on or after the first event */
1567 if (!printed_date) {
1568 cycles_t du = (tnow - t0) / mhz;
1569 time_t then = tvnow.tv_sec - du/1000000;
1571 if (du % 1000000 > tvnow.tv_usec)
1574 fprintf(f, "%s", ctime(&then));
1578 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1582 if (++nlines % 10000 == 0 && f != stdout) {
1583 /* show some activity... */
1589 tlast = next_event[cpu]->lwte_when;
1592 if (next_event[cpu] == cpu_event[cpu + 1])
1593 next_event[cpu] = cpu_event[cpu];
1595 if (next_event[cpu]->lwte_where == NULL ||
1596 next_event[cpu] == first_event[cpu])
1597 next_event[cpu] = NULL;
1609 int jt_ptl_memhog(int argc, char **argv)
1611 static int gfp = 0; /* sticky! */
1613 struct libcfs_ioctl_data data;
1619 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1623 count = strtol(argv[1], &end, 0);
1624 if (count < 0 || *end != 0) {
1625 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1630 rc = strtol(argv[2], &end, 0);
1632 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1638 LIBCFS_IOC_INIT(data);
1639 data.ioc_count = count;
1640 data.ioc_flags = gfp;
1641 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1644 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1648 printf("memhog %d OK\n", count);
1652 int jt_ptl_testprotocompat(int argc, char **argv)
1654 struct libcfs_ioctl_data data;
1660 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1664 flags = strtol(argv[1], &end, 0);
1665 if (flags < 0 || *end != 0) {
1666 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1670 LIBCFS_IOC_INIT(data);
1671 data.ioc_flags = flags;
1672 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1675 fprintf(stderr, "test proto compat %x failed: %s\n",
1676 flags, strerror(errno));
1680 printf("test proto compat %x OK\n", flags);