1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. 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)
199 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
201 id_ptr->pid = LNET_PID_ANY;
202 id_ptr->nid = libcfs_str2nid(nid_str);
203 if (id_ptr->nid == LNET_NID_ANY) {
204 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
211 int g_net_is_set (char *cmd)
218 "You must run the 'network' command before '%s'.\n",
223 int g_net_is_compatible (char *cmd, ...)
228 if (!g_net_is_set(cmd))
234 nal = va_arg (ap, int);
235 if (nal == LNET_NETTYP(g_net)) {
245 "Command %s not compatible with %s NAL\n",
247 libcfs_lnd2str(LNET_NETTYP(g_net)));
251 int ptl_initialize(int argc, char **argv)
253 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
254 LNET_DEV_MAJOR, LNET_DEV_MINOR);
259 int jt_ptl_network(int argc, char **argv)
261 struct libcfs_ioctl_data data;
262 __u32 net = LNET_NIDNET(LNET_NID_ANY);
266 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
270 if (!strcmp(argv[1], "unconfigure") ||
271 !strcmp(argv[1], "down")) {
272 LIBCFS_IOC_INIT(data);
273 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
276 printf ("LNET ready to unload\n");
281 fprintf(stderr, "LNET busy\n");
283 fprintf(stderr, "LNET unconfigure error %d: %s\n",
284 errno, strerror(errno));
288 if (!strcmp(argv[1], "configure") ||
289 !strcmp(argv[1], "up")) {
290 LIBCFS_IOC_INIT(data);
291 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
294 printf ("LNET configured\n");
298 fprintf(stderr, "LNET configure error %d: %s\n",
299 errno, strerror(errno));
303 net = libcfs_str2net(argv[1]);
304 if (net == LNET_NIDNET(LNET_NID_ANY)) {
305 fprintf(stderr, "Can't parse net %s\n", argv[1]);
315 jt_ptl_list_nids(int argc, char **argv)
317 struct libcfs_ioctl_data data;
318 int all = 0, return_nid = 0;
322 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
323 /* Hack to pass back value */
324 return_nid = (argc == 2) && (argv[1][0] == 1);
326 if ((argc > 2) && !(all || return_nid)) {
327 fprintf(stderr, "usage: %s [all]\n", argv[0]);
331 for (count = 0;; count++) {
332 LIBCFS_IOC_INIT (data);
333 data.ioc_count = count;
334 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
337 if ((count > 0) && (errno == ENOENT))
338 /* We found them all */
340 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
341 errno, strerror(errno));
345 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
346 printf("%s\n", libcfs_nid2str(data.ioc_nid));
348 *(__u64 *)(argv[1]) = data.ioc_nid;
358 jt_ptl_which_nid (int argc, char **argv)
360 struct libcfs_ioctl_data data;
363 lnet_nid_t best_nid = LNET_NID_ANY;
372 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
376 for (i = 1; i < argc; i++) {
378 nid = libcfs_str2nid(nidstr);
379 if (nid == LNET_NID_ANY) {
380 fprintf(stderr, "Can't parse NID %s\n", nidstr);
384 LIBCFS_IOC_INIT(data);
387 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
389 fprintf(stderr, "Can't get distance to %s: %s\n",
390 nidstr, strerror(errno));
394 dist = data.ioc_u32[0];
395 order = data.ioc_u32[1];
398 if (dist == -EHOSTUNREACH)
401 fprintf(stderr, "Unexpected distance to %s: %d\n",
406 if (best_nid == LNET_NID_ANY ||
408 (dist == best_dist && order < best_order)) {
415 if (best_nid == LNET_NID_ANY) {
416 fprintf(stderr, "No reachable NID\n");
420 printf("%s\n", libcfs_nid2str(best_nid));
425 jt_ptl_print_interfaces (int argc, char **argv)
427 struct libcfs_ioctl_data data;
432 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
435 for (index = 0;;index++) {
436 LIBCFS_IOC_INIT(data);
437 data.ioc_net = g_net;
438 data.ioc_count = index;
440 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
444 printf ("%s: (%s/%s) npeer %d nroute %d\n",
445 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2], 1),
446 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0], 0),
447 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1], 0),
448 data.ioc_u32[2], data.ioc_u32[3]);
452 if (errno == ENOENT) {
453 printf ("<no interfaces>\n");
455 fprintf(stderr, "Error getting interfaces: %s: "
465 jt_ptl_add_interface (int argc, char **argv)
467 struct libcfs_ioctl_data data;
470 __u32 netmask = 0xffffff00;
475 if (argc < 2 || argc > 3) {
476 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
480 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
483 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
484 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
489 count = strtol(argv[2], &end, 0);
490 if (count > 0 && count < 32 && *end == 0) {
492 for (i = count; i > 0; i--)
493 netmask = netmask|(1<<(32-i));
494 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
495 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
500 LIBCFS_IOC_INIT(data);
501 data.ioc_net = g_net;
502 data.ioc_u32[0] = ipaddr;
503 data.ioc_u32[1] = netmask;
505 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
507 fprintf (stderr, "failed to add interface: %s\n",
516 jt_ptl_del_interface (int argc, char **argv)
518 struct libcfs_ioctl_data data;
523 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
527 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
531 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
532 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
536 LIBCFS_IOC_INIT(data);
537 data.ioc_net = g_net;
538 data.ioc_u32[0] = ipaddr;
540 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
542 fprintf (stderr, "failed to delete interface: %s\n",
551 jt_ptl_print_peers (int argc, char **argv)
553 struct libcfs_ioctl_data data;
554 lnet_process_id_t id;
559 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
560 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
563 for (index = 0;;index++) {
564 LIBCFS_IOC_INIT(data);
565 data.ioc_net = g_net;
566 data.ioc_count = index;
568 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
572 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
573 id.nid = data.ioc_nid;
574 id.pid = data.ioc_u32[4];
575 printf ("%-20s [%d]%s->%s:%d #%d\n",
577 data.ioc_count, /* persistence */
578 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* my ip */
579 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
580 data.ioc_u32[1], /* peer port */
581 data.ioc_u32[3]); /* conn_count */
582 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
583 id.nid = data.ioc_nid;
584 id.pid = data.ioc_u32[4];
585 printf ("%-20s s %d%s [%d] "LPD64".%06d"
586 " m "LPD64"/"LPD64" q %d/%d c %d/%d\n",
588 data.ioc_net, /* state */
589 data.ioc_flags ? "" : " ~!h", /* sent_hello */
590 data.ioc_count, /* refcount */
591 data.ioc_u64[0]/1000000, /* incarnation secs */
592 (int)(data.ioc_u64[0]%1000000), /* incarnation usecs */
593 (((__u64)data.ioc_u32[1])<<32) |
594 ((__u64)data.ioc_u32[0]), /* next_matchbits */
595 (((__u64)data.ioc_u32[3])<<32) |
596 ((__u64)data.ioc_u32[2]), /* last_matchbits_seen */
597 data.ioc_u32[5] >> 16, /* nsendq */
598 data.ioc_u32[5] & 0xffff, /* nactiveq */
599 data.ioc_u32[6] >> 16, /* credits */
600 data.ioc_u32[6] & 0xffff); /* outstanding_credits */
601 } else if (g_net_is_compatible(NULL, RALND, OPENIBLND, CIBLND, VIBLND, 0)) {
602 printf ("%-20s [%d]@%s:%d\n",
603 libcfs_nid2str(data.ioc_nid), /* peer nid */
604 data.ioc_count, /* peer persistence */
605 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* peer ip */
606 data.ioc_u32[1]); /* peer port */
608 printf ("%-20s [%d]\n",
609 libcfs_nid2str(data.ioc_nid), data.ioc_count);
614 if (errno == ENOENT) {
615 printf ("<no peers>\n");
617 fprintf(stderr, "Error getting peer list: %s: "
626 jt_ptl_add_peer (int argc, char **argv)
628 struct libcfs_ioctl_data data;
634 if (!g_net_is_compatible (argv[0], SOCKLND, RALND,
635 OPENIBLND, CIBLND, IIBLND, VIBLND, 0))
638 if (g_net_is_compatible(NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0)) {
640 fprintf (stderr, "usage(tcp,openib,cib,ra): %s nid ipaddr port\n",
644 } else if (g_net_is_compatible(NULL, VIBLND, 0)) {
646 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
650 } else if (argc != 2) {
651 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
655 nid = libcfs_str2nid(argv[1]);
656 if (nid == LNET_NID_ANY) {
657 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
661 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, VIBLND, RALND, 0) &&
662 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
663 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
667 if (g_net_is_compatible (NULL, SOCKLND, OPENIBLND, CIBLND, RALND, 0) &&
668 lnet_parse_port (&port, argv[3]) != 0) {
669 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
673 LIBCFS_IOC_INIT(data);
674 data.ioc_net = g_net;
676 data.ioc_u32[0] = ip;
677 data.ioc_u32[1] = port;
679 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
681 fprintf (stderr, "failed to add peer: %s\n",
690 jt_ptl_del_peer (int argc, char **argv)
692 struct libcfs_ioctl_data data;
693 lnet_nid_t nid = LNET_NID_ANY;
694 lnet_pid_t pid = LNET_PID_ANY;
699 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
700 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
703 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
705 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
709 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
711 fprintf (stderr, "usage: %s [nid] [pid]\n",
715 } else if (argc > 2) {
716 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
721 !libcfs_str2anynid(&nid, argv[1])) {
722 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
726 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
728 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
729 fprintf (stderr, "Can't parse ip addr: %s\n",
733 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
735 pid = strtol(argv[2], &end, 0);
736 if (end == argv[2] || *end == 0) {
737 fprintf(stderr, "Can't parse pid %s\n",
744 LIBCFS_IOC_INIT(data);
745 data.ioc_net = g_net;
747 data.ioc_u32[0] = ip;
748 data.ioc_u32[1] = pid;
750 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
752 fprintf (stderr, "failed to remove peer: %s\n",
761 jt_ptl_print_connections (int argc, char **argv)
763 struct libcfs_ioctl_data data;
764 lnet_process_id_t id;
769 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND,
770 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
773 for (index = 0; ; index++) {
774 LIBCFS_IOC_INIT(data);
775 data.ioc_net = g_net;
776 data.ioc_count = index;
778 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
782 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
783 id.nid = data.ioc_nid;
784 id.pid = data.ioc_u32[6];
785 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
787 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
788 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
789 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
790 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
791 data.ioc_u32[4], /* scheduler */
792 ptl_ipaddr_2_str (data.ioc_u32[2], buffer[0], 1), /* local IP addr */
793 ptl_ipaddr_2_str (data.ioc_u32[0], buffer[1], 1), /* remote IP addr */
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, RALND, 0)) {
799 printf ("%-20s [%d]\n",
800 libcfs_nid2str(data.ioc_nid),
801 data.ioc_u32[0] /* device id */);
802 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
803 printf ("%s mtu %d\n",
804 libcfs_nid2str(data.ioc_nid),
805 data.ioc_u32[0]); /* path MTU */
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, RALND, MXLND,
836 OPENIBLND, CIBLND, IIBLND, VIBLND, O2IBLND, 0))
840 !libcfs_str2anynid(&nid, argv[1])) {
841 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
845 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
847 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
848 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
852 LIBCFS_IOC_INIT(data);
853 data.ioc_net = g_net;
855 data.ioc_u32[0] = ipaddr;
857 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
859 fprintf(stderr, "failed to remove connection: %s\n",
867 int jt_ptl_push_connection (int argc, char **argv)
869 struct libcfs_ioctl_data data;
871 lnet_nid_t nid = LNET_NID_ANY;
874 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
878 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
882 !libcfs_str2anynid(&nid, argv[1])) {
883 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
887 LIBCFS_IOC_INIT(data);
888 data.ioc_net = g_net;
891 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
893 fprintf(stderr, "failed to push connection: %s\n",
902 jt_ptl_print_active_txs (int argc, char **argv)
904 struct libcfs_ioctl_data data;
908 if (!g_net_is_compatible (argv[0], QSWLND, 0))
911 for (index = 0;;index++) {
912 LIBCFS_IOC_INIT(data);
913 data.ioc_net = g_net;
914 data.ioc_count = index;
916 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
920 printf ("type %u payload %6d to %s via %s by pid %6d: "
921 "%s, %s, state %d\n",
924 libcfs_nid2str(data.ioc_nid),
925 libcfs_nid2str(data.ioc_u64[0]),
927 (data.ioc_flags & 1) ? "delayed" : "immediate",
928 (data.ioc_flags & 2) ? "nblk" : "normal",
929 data.ioc_flags >> 2);
933 if (errno == ENOENT) {
934 printf ("<no active descs>\n");
936 fprintf(stderr, "Error getting active transmits list: "
937 "%s: check dmesg.\n",
944 int jt_ptl_ping(int argc, char **argv)
948 lnet_process_id_t id;
949 lnet_process_id_t ids[16];
950 int maxids = sizeof(ids)/sizeof(ids[0]);
951 struct libcfs_ioctl_data data;
956 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
960 sep = strchr(argv[1], '-');
962 rc = lnet_parse_nid(argv[1], &id);
968 if (argv[1][0] == 'u' ||
970 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
972 id.pid = strtoul(argv[1], &end, 0);
974 if (end != sep) { /* assuming '-' is part of hostname */
975 rc = lnet_parse_nid(argv[1], &id);
979 id.nid = libcfs_str2nid(sep + 1);
981 if (id.nid == LNET_NID_ANY) {
983 "Can't parse process id \"%s\"\n",
991 timeout = 1000 * atol(argv[2]);
993 timeout = 1000; /* default 1 second timeout */
995 LIBCFS_IOC_INIT (data);
996 data.ioc_nid = id.nid;
997 data.ioc_u32[0] = id.pid;
998 data.ioc_u32[1] = timeout;
999 data.ioc_plen1 = sizeof(ids);
1000 data.ioc_pbuf1 = (char *)ids;
1002 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
1004 fprintf(stderr, "failed to ping %s: %s\n",
1005 id.pid == LNET_PID_ANY ?
1006 libcfs_nid2str(id.nid) : libcfs_id2str(id),
1011 for (i = 0; i < data.ioc_count && i < maxids; i++)
1012 printf("%s\n", libcfs_id2str(ids[i]));
1014 if (data.ioc_count > maxids)
1015 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1020 int jt_ptl_mynid(int argc, char **argv)
1022 struct libcfs_ioctl_data data;
1027 fprintf(stderr, "usage: %s NID\n", argv[0]);
1031 nid = libcfs_str2nid(argv[1]);
1032 if (nid == LNET_NID_ANY) {
1033 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1037 LIBCFS_IOC_INIT(data);
1038 data.ioc_net = LNET_NIDNET(nid);
1041 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1043 fprintf(stderr, "setting my NID failed: %s\n",
1046 printf("registered my nid %s\n", libcfs_nid2str(nid));
1052 jt_ptl_fail_nid (int argc, char **argv)
1056 unsigned int threshold;
1057 struct libcfs_ioctl_data data;
1059 if (argc < 2 || argc > 3)
1061 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1065 if (!libcfs_str2anynid(&nid, argv[1]))
1067 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1072 threshold = LNET_MD_THRESH_INF;
1073 } else if (sscanf (argv[2], "%i", &threshold) != 1) {
1074 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1078 LIBCFS_IOC_INIT (data);
1080 data.ioc_count = threshold;
1082 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1084 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1087 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1093 jt_ptl_add_route (int argc, char **argv)
1095 struct libcfs_ioctl_data data;
1096 lnet_nid_t gateway_nid;
1097 unsigned int hops = 1;
1101 if (argc < 2 || argc > 3)
1103 fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
1107 if (!g_net_is_set(argv[0]))
1110 gateway_nid = libcfs_str2nid(argv[1]);
1111 if (gateway_nid == LNET_NID_ANY) {
1112 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1117 hops = strtoul(argv[2], &end, 0);
1118 if (hops >= 256 || *end != 0) {
1119 fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
1124 LIBCFS_IOC_INIT(data);
1125 data.ioc_net = g_net;
1126 data.ioc_count = hops;
1127 data.ioc_nid = gateway_nid;
1129 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1131 fprintf (stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n", strerror (errno));
1139 jt_ptl_del_route (int argc, char **argv)
1141 struct libcfs_ioctl_data data;
1146 fprintf (stderr, "usage: %s gatewayNID\n", argv[0]);
1150 if (!libcfs_str2anynid(&nid, argv[1])) {
1151 fprintf (stderr, "Can't parse gateway NID "
1152 "\"%s\"\n", argv[1]);
1156 LIBCFS_IOC_INIT(data);
1157 data.ioc_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1160 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1162 fprintf (stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1163 libcfs_nid2str(nid), strerror (errno));
1171 jt_ptl_notify_router (int argc, char **argv)
1173 struct libcfs_ioctl_data data;
1182 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1187 nid = libcfs_str2nid(argv[1]);
1188 if (nid == LNET_NID_ANY) {
1189 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1193 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1194 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1198 gettimeofday(&now, NULL);
1202 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1203 fprintf(stderr, "Can't parse time %s\n"
1204 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1205 "or an absolute unix time in seconds\n", argv[3]);
1207 } else if (when > now.tv_sec) {
1208 fprintf (stderr, "%s specifies a time in the future\n",
1213 LIBCFS_IOC_INIT(data);
1215 data.ioc_flags = enable;
1216 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1217 data.ioc_u64[0] = (__u64)when;
1219 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1221 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1222 libcfs_nid2str(nid), strerror (errno));
1230 jt_ptl_print_routes (int argc, char **argv)
1232 struct libcfs_ioctl_data data;
1240 for (index = 0;;index++)
1242 LIBCFS_IOC_INIT(data);
1243 data.ioc_count = index;
1245 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1250 hops = data.ioc_count;
1252 alive = data.ioc_flags;
1254 printf ("net %18s hops %u gw %32s %s\n",
1255 libcfs_net2str(net), hops,
1256 libcfs_nid2str(nid), alive ? "up" : "down");
1259 if (errno != ENOENT)
1260 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1267 lwt_control(int enable, int clear)
1269 struct libcfs_ioctl_data data;
1272 LIBCFS_IOC_INIT(data);
1273 data.ioc_flags = (enable ? 1 : 0) | (clear ? 2 : 0);
1275 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_CONTROL, &data);
1279 fprintf(stderr, "IOC_LIBCFS_LWT_CONTROL failed: %s\n",
1285 lwt_snapshot(cfs_cycles_t *now, int *ncpu, int *totalsize,
1286 lwt_event_t *events, int size)
1288 struct libcfs_ioctl_data data;
1291 LIBCFS_IOC_INIT(data);
1292 data.ioc_pbuf1 = (char *)events;
1293 data.ioc_plen1 = size;
1295 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_SNAPSHOT, &data);
1297 fprintf(stderr, "IOC_LIBCFS_LWT_SNAPSHOT failed: %s\n",
1302 /* crappy overloads */
1303 if (data.ioc_u32[2] != sizeof(lwt_event_t) ||
1304 data.ioc_u32[3] != offsetof(lwt_event_t, lwte_where)) {
1305 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1306 (int)data.ioc_u32[2], (int)sizeof(lwt_event_t),
1307 (int)data.ioc_u32[3],
1308 (int)offsetof(lwt_event_t, lwte_where));
1313 *now = data.ioc_u64[0];
1315 LASSERT (data.ioc_u32[0] != 0);
1317 *ncpu = data.ioc_u32[0];
1319 LASSERT (data.ioc_u32[1] != 0);
1320 if (totalsize != NULL)
1321 *totalsize = data.ioc_u32[1];
1327 lwt_get_string(char *kstr)
1330 struct libcfs_ioctl_data data;
1334 /* FIXME: this could maintain a symbol table since we expect to be
1335 * looking up the same strings all the time... */
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 = NULL;
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 size = data.ioc_count;
1351 ustr = (char *)malloc(size);
1353 fprintf(stderr, "Can't allocate string storage of size %d\n",
1358 LIBCFS_IOC_INIT(data);
1359 data.ioc_pbuf1 = kstr;
1360 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1361 data.ioc_pbuf2 = ustr;
1362 data.ioc_plen2 = size;
1364 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1366 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1371 LASSERT(strlen(ustr) == size - 1);
1376 lwt_put_string(char *ustr)
1382 lwt_print(FILE *f, cfs_cycles_t t0, cfs_cycles_t tlast, double mhz, int cpu,
1386 # error "__WORDSIZE not defined"
1387 #elif __WORDSIZE == 32
1388 # define XFMT "%#010lx"
1389 #elif __WORDSIZE== 64
1390 # define XFMT "%#018lx"
1392 # error "Unexpected __WORDSIZE"
1394 char *where = lwt_get_string(e->lwte_where);
1399 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1400 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1401 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1402 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1405 lwt_put_string(where);
1412 get_cycles_per_usec ()
1414 FILE *f = fopen ("/proc/cpuinfo", "r");
1419 while (fgets (line, sizeof (line), f) != NULL)
1420 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1427 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1431 #define LWT_MAX_CPUS (32)
1434 jt_ptl_lwt(int argc, char **argv)
1438 int nevents_per_cpu;
1439 lwt_event_t *events;
1440 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1441 lwt_event_t *next_event[LWT_MAX_CPUS];
1442 lwt_event_t *first_event[LWT_MAX_CPUS];
1451 struct timeval tvnow;
1452 int printed_date = 0;
1457 (strcmp(argv[1], "start") &&
1458 strcmp(argv[1], "stop"))) {
1461 " %s stop [fname]\n", argv[0], argv[0]);
1465 if (!strcmp(argv[1], "start")) {
1467 if (lwt_control(0, 0) != 0)
1471 if (lwt_control(0, 1) != 0)
1475 if (lwt_control(1, 0) != 0)
1481 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1484 if (ncpus > LWT_MAX_CPUS) {
1485 fprintf(stderr, "Too many cpus: %d (%d)\n",
1486 ncpus, LWT_MAX_CPUS);
1490 events = (lwt_event_t *)malloc(totalspace);
1491 if (events == NULL) {
1492 fprintf(stderr, "Can't allocate %d\n", totalspace);
1496 if (lwt_control(0, 0) != 0) { /* disable */
1501 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1506 /* we want this time to be sampled at snapshot time */
1507 gettimeofday(&tvnow, NULL);
1510 f = fopen (argv[2], "w");
1512 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1518 mhz = get_cycles_per_usec();
1520 /* carve events into per-cpu slices */
1521 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1522 for (cpu = 0; cpu <= ncpus; cpu++)
1523 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1525 /* find the earliest event on each cpu */
1526 for (cpu = 0; cpu < ncpus; cpu++) {
1527 first_event[cpu] = NULL;
1529 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1531 if (e->lwte_where == NULL) /* not an event */
1534 if (first_event[cpu] == NULL ||
1535 first_event[cpu]->lwte_when > e->lwte_when)
1536 first_event[cpu] = e;
1539 next_event[cpu] = first_event[cpu];
1543 for (cpu = 0; cpu < ncpus; cpu++) {
1544 e = first_event[cpu];
1545 if (e == NULL) /* no events this cpu */
1548 if (e == cpu_event[cpu])
1549 e = cpu_event[cpu + 1] - 1;
1553 /* If there's an event immediately before the first one, this
1554 * cpu wrapped its event buffer */
1555 if (e->lwte_where == NULL)
1558 /* We should only start outputting events from the most recent
1559 * first event in any wrapped cpu. Events before this time on
1560 * other cpus won't have any events from this CPU to interleave
1562 if (t0 < first_event[cpu]->lwte_when)
1563 t0 = first_event[cpu]->lwte_when;
1567 /* find which cpu has the next event */
1569 for (i = 0; i < ncpus; i++) {
1571 if (next_event[i] == NULL) /* this cpu exhausted */
1575 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1579 if (cpu < 0) /* all cpus exhausted */
1583 /* no wrapped cpus and this is he first ever event */
1584 t0 = next_event[cpu]->lwte_when;
1587 if (t0 <= next_event[cpu]->lwte_when) {
1588 /* on or after the first event */
1589 if (!printed_date) {
1590 cfs_cycles_t du = (tnow - t0) / mhz;
1591 time_t then = tvnow.tv_sec - du/1000000;
1593 if (du % 1000000 > tvnow.tv_usec)
1596 fprintf(f, "%s", ctime(&then));
1600 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1604 if (++nlines % 10000 == 0 && f != stdout) {
1605 /* show some activity... */
1611 tlast = next_event[cpu]->lwte_when;
1614 if (next_event[cpu] == cpu_event[cpu + 1])
1615 next_event[cpu] = cpu_event[cpu];
1617 if (next_event[cpu]->lwte_where == NULL ||
1618 next_event[cpu] == first_event[cpu])
1619 next_event[cpu] = NULL;
1631 int jt_ptl_memhog(int argc, char **argv)
1633 static int gfp = 0; /* sticky! */
1635 struct libcfs_ioctl_data data;
1641 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1645 count = strtol(argv[1], &end, 0);
1646 if (count < 0 || *end != 0) {
1647 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1652 rc = strtol(argv[2], &end, 0);
1654 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1660 LIBCFS_IOC_INIT(data);
1661 data.ioc_count = count;
1662 data.ioc_flags = gfp;
1663 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1666 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1670 printf("memhog %d OK\n", count);
1674 int jt_ptl_testprotocompat(int argc, char **argv)
1676 struct libcfs_ioctl_data data;
1682 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1686 flags = strtol(argv[1], &end, 0);
1687 if (flags < 0 || *end != 0) {
1688 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1692 LIBCFS_IOC_INIT(data);
1693 data.ioc_flags = flags;
1694 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1697 fprintf(stderr, "test proto compat %x failed: %s\n",
1698 flags, strerror(errno));
1702 printf("test proto compat %x OK\n", flags);