2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * This file is part of Portals, http://www.sf.net/projects/lustre/
6 * Portals is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * Portals is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Portals; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <libcfs/libcfsutil.h>
22 #include <lnet/api-support.h>
23 #include <lnet/lnetctl.h>
24 #include <lnet/socklnd.h>
26 unsigned int libcfs_debug;
27 unsigned int libcfs_printk = D_CANTMASK;
32 /* Convert a string boolean to an int; "enable" -> 1 */
34 lnet_parse_bool (int *b, char *str)
36 if (!strcasecmp (str, "no") ||
37 !strcasecmp (str, "n") ||
38 !strcasecmp (str, "off") ||
39 !strcasecmp (str, "down") ||
40 !strcasecmp (str, "disable"))
46 if (!strcasecmp (str, "yes") ||
47 !strcasecmp (str, "y") ||
48 !strcasecmp (str, "on") ||
49 !strcasecmp (str, "up") ||
50 !strcasecmp (str, "enable"))
60 lnet_parse_port (int *port, char *str)
64 *port = strtol (str, &end, 0);
66 if (*end == 0 && /* parsed whole string */
67 *port > 0 && *port < 65536) /* minimal sanity check */
73 #ifdef HAVE_GETHOSTBYNAME
74 static struct hostent *
75 ptl_gethostbyname(char * hname) {
77 he = gethostbyname(hname);
82 fprintf(stderr, "Unable to resolve hostname: %s\n",
86 fprintf(stderr, "gethostbyname error for %s: %s\n",
87 hname, strerror(h_errno));
97 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
104 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
105 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
106 (c & ~0xff) == 0 && (d & ~0xff) == 0)
108 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
116 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
118 #ifdef HAVE_GETHOSTBYNAME
122 if (!strcmp (str, "_all_")) {
127 if (lnet_parse_ipquad(ipaddrp, str) == 0)
130 #ifdef HAVE_GETHOSTBYNAME
131 if ((('a' <= str[0] && str[0] <= 'z') ||
132 ('A' <= str[0] && str[0] <= 'Z')) &&
133 (he = ptl_gethostbyname (str)) != NULL) {
134 __u32 addr = *(__u32 *)he->h_addr;
136 *ipaddrp = ntohl(addr); /* HOST byte order */
145 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
147 #ifdef HAVE_GETHOSTBYNAME
152 net_ip = htonl (ipaddr);
153 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
155 strncpy(str, he->h_name, strsize - 1);
156 str[strsize - 1] = '\0';
162 sprintf (str, "%d.%d.%d.%d",
163 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
164 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
169 lnet_parse_time (time_t *t, char *str)
175 *t = strtol (str, &end, 0);
176 if (*end == 0) /* parsed whole string */
179 memset (&tm, 0, sizeof (tm));
180 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
181 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
182 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
186 tm.tm_mon--; /* convert to 0 == Jan */
187 tm.tm_year -= 1900; /* y2k quirk */
188 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
191 if (*t == (time_t)-1)
198 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
200 id_ptr->pid = LNET_PID_ANY;
201 id_ptr->nid = libcfs_str2nid(nid_str);
202 if (id_ptr->nid == LNET_NID_ANY) {
203 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
210 int g_net_is_set (char *cmd)
217 "You must run the 'network' command before '%s'.\n",
222 int g_net_is_compatible (char *cmd, ...)
227 if (!g_net_is_set(cmd))
233 nal = va_arg (ap, int);
234 if (nal == LNET_NETTYP(g_net)) {
244 "Command %s not compatible with %s NAL\n",
246 libcfs_lnd2str(LNET_NETTYP(g_net)));
250 int ptl_initialize(int argc, char **argv)
252 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
253 LNET_DEV_MAJOR, LNET_DEV_MINOR);
258 int jt_ptl_network(int argc, char **argv)
260 struct libcfs_ioctl_data data;
261 __u32 net = LNET_NIDNET(LNET_NID_ANY);
265 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
269 if (!strcmp(argv[1], "unconfigure") ||
270 !strcmp(argv[1], "down")) {
271 LIBCFS_IOC_INIT(data);
272 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
275 printf ("LNET ready to unload\n");
280 fprintf(stderr, "LNET busy\n");
282 fprintf(stderr, "LNET unconfigure error %d: %s\n",
283 errno, strerror(errno));
287 if (!strcmp(argv[1], "configure") ||
288 !strcmp(argv[1], "up")) {
289 LIBCFS_IOC_INIT(data);
290 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
293 printf ("LNET configured\n");
297 fprintf(stderr, "LNET configure error %d: %s\n",
298 errno, strerror(errno));
302 net = libcfs_str2net(argv[1]);
303 if (net == LNET_NIDNET(LNET_NID_ANY)) {
304 fprintf(stderr, "Can't parse net %s\n", argv[1]);
308 if (LNET_NETTYP(net) == CIBLND ||
309 LNET_NETTYP(net) == OPENIBLND ||
310 LNET_NETTYP(net) == IIBLND ||
311 LNET_NETTYP(net) == VIBLND) {
312 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
322 jt_ptl_list_nids(int argc, char **argv)
324 struct libcfs_ioctl_data data;
325 int all = 0, return_nid = 0;
329 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
330 /* Hack to pass back value */
331 return_nid = (argc == 2) && (argv[1][0] == 1);
333 if ((argc > 2) && !(all || return_nid)) {
334 fprintf(stderr, "usage: %s [all]\n", argv[0]);
338 for (count = 0;; count++) {
339 LIBCFS_IOC_INIT (data);
340 data.ioc_count = count;
341 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
344 if ((count > 0) && (errno == ENOENT))
345 /* We found them all */
347 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
348 errno, strerror(errno));
352 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
353 printf("%s\n", libcfs_nid2str(data.ioc_nid));
355 *(__u64 *)(argv[1]) = data.ioc_nid;
365 jt_ptl_which_nid (int argc, char **argv)
367 struct libcfs_ioctl_data data;
370 lnet_nid_t best_nid = LNET_NID_ANY;
379 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
383 for (i = 1; i < argc; i++) {
385 nid = libcfs_str2nid(nidstr);
386 if (nid == LNET_NID_ANY) {
387 fprintf(stderr, "Can't parse NID %s\n", nidstr);
391 LIBCFS_IOC_INIT(data);
394 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
396 fprintf(stderr, "Can't get distance to %s: %s\n",
397 nidstr, strerror(errno));
401 dist = data.ioc_u32[0];
402 order = data.ioc_u32[1];
405 if (dist == -EHOSTUNREACH)
408 fprintf(stderr, "Unexpected distance to %s: %d\n",
413 if (best_nid == LNET_NID_ANY ||
415 (dist == best_dist && order < best_order)) {
422 if (best_nid == LNET_NID_ANY) {
423 fprintf(stderr, "No reachable NID\n");
427 printf("%s\n", libcfs_nid2str(best_nid));
432 jt_ptl_print_interfaces (int argc, char **argv)
434 struct libcfs_ioctl_data data;
435 char buffer[3][HOST_NAME_MAX + 1];
439 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
442 for (index = 0;;index++) {
443 LIBCFS_IOC_INIT(data);
444 data.ioc_net = g_net;
445 data.ioc_count = index;
447 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
451 printf ("%s: (%s/%s) npeer %d nroute %d\n",
452 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
453 sizeof(buffer[2]), 1),
454 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
455 sizeof(buffer[0]), 0),
456 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
457 sizeof(buffer[1]), 0),
458 data.ioc_u32[2], data.ioc_u32[3]);
462 if (errno == ENOENT) {
463 printf ("<no interfaces>\n");
465 fprintf(stderr, "Error getting interfaces: %s: "
475 jt_ptl_add_interface (int argc, char **argv)
477 struct libcfs_ioctl_data data;
480 __u32 netmask = 0xffffff00;
485 if (argc < 2 || argc > 3) {
486 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
490 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
493 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
494 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
499 count = strtol(argv[2], &end, 0);
500 if (count > 0 && count < 32 && *end == 0) {
502 for (i = count; i > 0; i--)
503 netmask = netmask|(1<<(32-i));
504 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
505 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
510 LIBCFS_IOC_INIT(data);
511 data.ioc_net = g_net;
512 data.ioc_u32[0] = ipaddr;
513 data.ioc_u32[1] = netmask;
515 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
517 fprintf (stderr, "failed to add interface: %s\n",
526 jt_ptl_del_interface (int argc, char **argv)
528 struct libcfs_ioctl_data data;
533 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
537 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
541 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
542 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
546 LIBCFS_IOC_INIT(data);
547 data.ioc_net = g_net;
548 data.ioc_u32[0] = ipaddr;
550 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
552 fprintf (stderr, "failed to delete interface: %s\n",
561 jt_ptl_print_peers (int argc, char **argv)
563 struct libcfs_ioctl_data data;
564 lnet_process_id_t id;
565 char buffer[2][HOST_NAME_MAX + 1];
569 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, PTLLND, MXLND,
573 for (index = 0;;index++) {
574 LIBCFS_IOC_INIT(data);
575 data.ioc_net = g_net;
576 data.ioc_count = index;
578 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
582 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
583 id.nid = data.ioc_nid;
584 id.pid = data.ioc_u32[4];
585 printf ("%-20s [%d]%s->%s:%d #%d\n",
587 data.ioc_count, /* persistence */
589 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
590 sizeof(buffer[0]), 1),
592 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
593 sizeof(buffer[1]), 1),
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, 0)) {
616 printf ("%-20s [%d]@%s:%d\n",
617 libcfs_nid2str(data.ioc_nid), /* peer nid */
618 data.ioc_count, /* peer persistence */
620 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
621 sizeof(buffer[1]), 1),
622 data.ioc_u32[1]); /* peer port */
624 printf ("%-20s [%d]\n",
625 libcfs_nid2str(data.ioc_nid), data.ioc_count);
630 if (errno == ENOENT) {
631 printf ("<no peers>\n");
633 fprintf(stderr, "Error getting peer list: %s: "
642 jt_ptl_add_peer (int argc, char **argv)
644 struct libcfs_ioctl_data data;
650 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, 0))
654 fprintf (stderr, "usage(tcp,ra): %s nid ipaddr port\n",
659 nid = libcfs_str2nid(argv[1]);
660 if (nid == LNET_NID_ANY) {
661 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
665 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
666 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
670 if (lnet_parse_port (&port, argv[3]) != 0) {
671 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
675 LIBCFS_IOC_INIT(data);
676 data.ioc_net = g_net;
678 data.ioc_u32[0] = ip;
679 data.ioc_u32[1] = port;
681 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
683 fprintf (stderr, "failed to add peer: %s\n",
692 jt_ptl_del_peer (int argc, char **argv)
694 struct libcfs_ioctl_data data;
695 lnet_nid_t nid = LNET_NID_ANY;
696 lnet_pid_t pid = LNET_PID_ANY;
701 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, PTLLND,
705 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
707 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
711 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
713 fprintf (stderr, "usage: %s [nid] [pid]\n",
717 } else if (argc > 2) {
718 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
723 !libcfs_str2anynid(&nid, argv[1])) {
724 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
728 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
730 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
731 fprintf (stderr, "Can't parse ip addr: %s\n",
735 } else if (g_net_is_compatible(NULL, PTLLND, 0)) {
737 pid = strtol(argv[2], &end, 0);
738 if (end == argv[2] || *end == 0) {
739 fprintf(stderr, "Can't parse pid %s\n",
746 LIBCFS_IOC_INIT(data);
747 data.ioc_net = g_net;
749 data.ioc_u32[0] = ip;
750 data.ioc_u32[1] = pid;
752 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
754 fprintf (stderr, "failed to remove peer: %s\n",
763 jt_ptl_print_connections (int argc, char **argv)
765 struct libcfs_ioctl_data data;
766 lnet_process_id_t id;
767 char buffer[2][HOST_NAME_MAX + 1];
771 if (!g_net_is_compatible (argv[0], SOCKLND, RALND, MXLND, O2IBLND, 0))
774 for (index = 0; ; index++) {
775 LIBCFS_IOC_INIT(data);
776 data.ioc_net = g_net;
777 data.ioc_count = index;
779 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
783 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
784 id.nid = data.ioc_nid;
785 id.pid = data.ioc_u32[6];
786 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
788 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
789 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
790 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
791 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
792 data.ioc_u32[4], /* scheduler */
794 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
795 sizeof(buffer[0]), 1),
797 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
798 sizeof(buffer[1]), 1),
799 data.ioc_u32[1], /* remote port */
800 data.ioc_count, /* tx buffer size */
801 data.ioc_u32[5], /* rx buffer size */
802 data.ioc_flags ? "nagle" : "nonagle");
803 } else if (g_net_is_compatible (NULL, RALND, 0)) {
804 printf ("%-20s [%d]\n",
805 libcfs_nid2str(data.ioc_nid),
806 data.ioc_u32[0] /* device id */);
807 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
808 printf ("%s mtu %d\n",
809 libcfs_nid2str(data.ioc_nid),
810 data.ioc_u32[0]); /* path MTU */
812 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
817 if (errno == ENOENT) {
818 printf ("<no connections>\n");
820 fprintf(stderr, "Error getting connection list: %s: "
828 int jt_ptl_disconnect(int argc, char **argv)
830 struct libcfs_ioctl_data data;
831 lnet_nid_t nid = LNET_NID_ANY;
836 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
840 if (!g_net_is_compatible (NULL, SOCKLND, RALND, MXLND, O2IBLND, 0))
844 !libcfs_str2anynid(&nid, argv[1])) {
845 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
849 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
851 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
852 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
856 LIBCFS_IOC_INIT(data);
857 data.ioc_net = g_net;
859 data.ioc_u32[0] = ipaddr;
861 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
863 fprintf(stderr, "failed to remove connection: %s\n",
871 int jt_ptl_push_connection (int argc, char **argv)
873 struct libcfs_ioctl_data data;
875 lnet_nid_t nid = LNET_NID_ANY;
878 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
882 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
886 !libcfs_str2anynid(&nid, argv[1])) {
887 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
891 LIBCFS_IOC_INIT(data);
892 data.ioc_net = g_net;
895 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
897 fprintf(stderr, "failed to push connection: %s\n",
906 jt_ptl_print_active_txs (int argc, char **argv)
908 struct libcfs_ioctl_data data;
912 if (!g_net_is_compatible (argv[0], QSWLND, 0))
915 for (index = 0;;index++) {
916 LIBCFS_IOC_INIT(data);
917 data.ioc_net = g_net;
918 data.ioc_count = index;
920 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_TXDESC, &data);
924 printf ("type %u payload %6d to %s via %s by pid %6d: "
925 "%s, %s, state %d\n",
928 libcfs_nid2str(data.ioc_nid),
929 libcfs_nid2str(data.ioc_u64[0]),
931 (data.ioc_flags & 1) ? "delayed" : "immediate",
932 (data.ioc_flags & 2) ? "nblk" : "normal",
933 data.ioc_flags >> 2);
937 if (errno == ENOENT) {
938 printf ("<no active descs>\n");
940 fprintf(stderr, "Error getting active transmits list: "
941 "%s: check dmesg.\n",
948 int jt_ptl_ping(int argc, char **argv)
952 lnet_process_id_t id;
953 lnet_process_id_t ids[16];
954 int maxids = sizeof(ids)/sizeof(ids[0]);
955 struct libcfs_ioctl_data data;
960 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
964 sep = strchr(argv[1], '-');
966 rc = lnet_parse_nid(argv[1], &id);
972 if (argv[1][0] == 'u' ||
974 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
976 id.pid = strtoul(argv[1], &end, 0);
978 if (end != sep) { /* assuming '-' is part of hostname */
979 rc = lnet_parse_nid(argv[1], &id);
983 id.nid = libcfs_str2nid(sep + 1);
985 if (id.nid == LNET_NID_ANY) {
987 "Can't parse process id \"%s\"\n",
995 timeout = 1000 * atol(argv[2]);
997 timeout = 1000; /* default 1 second timeout */
999 LIBCFS_IOC_INIT (data);
1000 data.ioc_nid = id.nid;
1001 data.ioc_u32[0] = id.pid;
1002 data.ioc_u32[1] = timeout;
1003 data.ioc_plen1 = sizeof(ids);
1004 data.ioc_pbuf1 = (char *)ids;
1006 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
1008 fprintf(stderr, "failed to ping %s: %s\n",
1009 id.pid == LNET_PID_ANY ?
1010 libcfs_nid2str(id.nid) : libcfs_id2str(id),
1015 for (i = 0; i < data.ioc_count && i < maxids; i++)
1016 printf("%s\n", libcfs_id2str(ids[i]));
1018 if (data.ioc_count > maxids)
1019 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1024 int jt_ptl_mynid(int argc, char **argv)
1026 struct libcfs_ioctl_data data;
1031 fprintf(stderr, "usage: %s NID\n", argv[0]);
1035 nid = libcfs_str2nid(argv[1]);
1036 if (nid == LNET_NID_ANY) {
1037 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1041 LIBCFS_IOC_INIT(data);
1042 data.ioc_net = LNET_NIDNET(nid);
1045 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1047 fprintf(stderr, "setting my NID failed: %s\n",
1050 printf("registered my nid %s\n", libcfs_nid2str(nid));
1056 jt_ptl_fail_nid (int argc, char **argv)
1060 unsigned int threshold;
1061 struct libcfs_ioctl_data data;
1063 if (argc < 2 || argc > 3)
1065 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1069 if (!libcfs_str2anynid(&nid, argv[1]))
1071 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1076 threshold = LNET_MD_THRESH_INF;
1077 } else if (sscanf (argv[2], "%i", &threshold) != 1) {
1078 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1082 LIBCFS_IOC_INIT (data);
1084 data.ioc_count = threshold;
1086 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1088 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1091 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1097 jt_ptl_add_route (int argc, char **argv)
1099 struct libcfs_ioctl_data data;
1100 lnet_nid_t gateway_nid;
1101 unsigned int hops = 1;
1105 if (argc < 2 || argc > 3)
1107 fprintf (stderr, "usage: %s gateway [hopcount]\n", argv[0]);
1111 if (!g_net_is_set(argv[0]))
1114 gateway_nid = libcfs_str2nid(argv[1]);
1115 if (gateway_nid == LNET_NID_ANY) {
1116 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1121 hops = strtoul(argv[2], &end, 0);
1122 if (hops >= 256 || *end != 0) {
1123 fprintf (stderr, "Can't parse hopcount \"%s\"\n", argv[2]);
1128 LIBCFS_IOC_INIT(data);
1129 data.ioc_net = g_net;
1130 data.ioc_count = hops;
1131 data.ioc_nid = gateway_nid;
1133 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1135 fprintf (stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n", strerror (errno));
1143 jt_ptl_del_route (int argc, char **argv)
1145 struct libcfs_ioctl_data data;
1150 fprintf (stderr, "usage: %s gatewayNID\n", argv[0]);
1154 if (!libcfs_str2anynid(&nid, argv[1])) {
1155 fprintf (stderr, "Can't parse gateway NID "
1156 "\"%s\"\n", argv[1]);
1160 LIBCFS_IOC_INIT(data);
1161 data.ioc_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1164 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1166 fprintf (stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1167 libcfs_nid2str(nid), strerror (errno));
1175 jt_ptl_notify_router (int argc, char **argv)
1177 struct libcfs_ioctl_data data;
1186 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1191 nid = libcfs_str2nid(argv[1]);
1192 if (nid == LNET_NID_ANY) {
1193 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1197 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1198 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1202 gettimeofday(&now, NULL);
1206 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1207 fprintf(stderr, "Can't parse time %s\n"
1208 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1209 "or an absolute unix time in seconds\n", argv[3]);
1211 } else if (when > now.tv_sec) {
1212 fprintf (stderr, "%s specifies a time in the future\n",
1217 LIBCFS_IOC_INIT(data);
1219 data.ioc_flags = enable;
1220 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1221 data.ioc_u64[0] = (__u64)when;
1223 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1225 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1226 libcfs_nid2str(nid), strerror (errno));
1234 jt_ptl_print_routes (int argc, char **argv)
1236 struct libcfs_ioctl_data data;
1244 for (index = 0;;index++)
1246 LIBCFS_IOC_INIT(data);
1247 data.ioc_count = index;
1249 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1254 hops = data.ioc_count;
1256 alive = data.ioc_flags;
1258 printf ("net %18s hops %u gw %32s %s\n",
1259 libcfs_net2str(net), hops,
1260 libcfs_nid2str(nid), alive ? "up" : "down");
1263 if (errno != ENOENT)
1264 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1271 lwt_control(int enable, int clear)
1273 struct libcfs_ioctl_data data;
1276 LIBCFS_IOC_INIT(data);
1277 data.ioc_flags = (enable ? 1 : 0) | (clear ? 2 : 0);
1279 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_CONTROL, &data);
1283 fprintf(stderr, "IOC_LIBCFS_LWT_CONTROL failed: %s\n",
1289 lwt_snapshot(cfs_cycles_t *now, int *ncpu, int *totalsize,
1290 lwt_event_t *events, int size)
1292 struct libcfs_ioctl_data data;
1295 LIBCFS_IOC_INIT(data);
1296 data.ioc_pbuf1 = (char *)events;
1297 data.ioc_plen1 = size;
1299 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_SNAPSHOT, &data);
1301 fprintf(stderr, "IOC_LIBCFS_LWT_SNAPSHOT failed: %s\n",
1306 /* crappy overloads */
1307 if (data.ioc_u32[2] != sizeof(lwt_event_t) ||
1308 data.ioc_u32[3] != offsetof(lwt_event_t, lwte_where)) {
1309 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1310 (int)data.ioc_u32[2], (int)sizeof(lwt_event_t),
1311 (int)data.ioc_u32[3],
1312 (int)offsetof(lwt_event_t, lwte_where));
1317 *now = data.ioc_u64[0];
1319 LASSERT (data.ioc_u32[0] != 0);
1321 *ncpu = data.ioc_u32[0];
1323 LASSERT (data.ioc_u32[1] != 0);
1324 if (totalsize != NULL)
1325 *totalsize = data.ioc_u32[1];
1331 lwt_get_string(char *kstr)
1334 struct libcfs_ioctl_data data;
1338 /* FIXME: this could maintain a symbol table since we expect to be
1339 * looking up the same strings all the time... */
1341 LIBCFS_IOC_INIT(data);
1342 data.ioc_pbuf1 = kstr;
1343 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1344 data.ioc_pbuf2 = NULL;
1347 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1349 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1354 size = data.ioc_count;
1355 ustr = (char *)malloc(size);
1357 fprintf(stderr, "Can't allocate string storage of size %d\n",
1362 LIBCFS_IOC_INIT(data);
1363 data.ioc_pbuf1 = kstr;
1364 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1365 data.ioc_pbuf2 = ustr;
1366 data.ioc_plen2 = size;
1368 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LWT_LOOKUP_STRING, &data);
1370 fprintf(stderr, "IOC_LIBCFS_LWT_LOOKUP_STRING failed: %s\n",
1375 LASSERT(strlen(ustr) == size - 1);
1380 lwt_put_string(char *ustr)
1386 lwt_print(FILE *f, cfs_cycles_t t0, cfs_cycles_t tlast, double mhz, int cpu,
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");
1435 #define LWT_MAX_CPUS (32)
1438 jt_ptl_lwt(int argc, char **argv)
1442 int nevents_per_cpu;
1443 lwt_event_t *events;
1444 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1445 lwt_event_t *next_event[LWT_MAX_CPUS];
1446 lwt_event_t *first_event[LWT_MAX_CPUS];
1455 struct timeval tvnow;
1456 int printed_date = 0;
1461 (strcmp(argv[1], "start") &&
1462 strcmp(argv[1], "stop"))) {
1465 " %s stop [fname]\n", argv[0], argv[0]);
1469 if (!strcmp(argv[1], "start")) {
1471 if (lwt_control(0, 0) != 0)
1475 if (lwt_control(0, 1) != 0)
1479 if (lwt_control(1, 0) != 0)
1485 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1488 if (ncpus > LWT_MAX_CPUS) {
1489 fprintf(stderr, "Too many cpus: %d (%d)\n",
1490 ncpus, LWT_MAX_CPUS);
1494 events = (lwt_event_t *)malloc(totalspace);
1495 if (events == NULL) {
1496 fprintf(stderr, "Can't allocate %d\n", totalspace);
1500 if (lwt_control(0, 0) != 0) { /* disable */
1505 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1510 /* we want this time to be sampled at snapshot time */
1511 gettimeofday(&tvnow, NULL);
1514 f = fopen (argv[2], "w");
1516 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1522 mhz = get_cycles_per_usec();
1524 /* carve events into per-cpu slices */
1525 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1526 for (cpu = 0; cpu <= ncpus; cpu++)
1527 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1529 /* find the earliest event on each cpu */
1530 for (cpu = 0; cpu < ncpus; cpu++) {
1531 first_event[cpu] = NULL;
1533 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1535 if (e->lwte_where == NULL) /* not an event */
1538 if (first_event[cpu] == NULL ||
1539 first_event[cpu]->lwte_when > e->lwte_when)
1540 first_event[cpu] = e;
1543 next_event[cpu] = first_event[cpu];
1547 for (cpu = 0; cpu < ncpus; cpu++) {
1548 e = first_event[cpu];
1549 if (e == NULL) /* no events this cpu */
1552 if (e == cpu_event[cpu])
1553 e = cpu_event[cpu + 1] - 1;
1557 /* If there's an event immediately before the first one, this
1558 * cpu wrapped its event buffer */
1559 if (e->lwte_where == NULL)
1562 /* We should only start outputting events from the most recent
1563 * first event in any wrapped cpu. Events before this time on
1564 * other cpus won't have any events from this CPU to interleave
1566 if (t0 < first_event[cpu]->lwte_when)
1567 t0 = first_event[cpu]->lwte_when;
1571 /* find which cpu has the next event */
1573 for (i = 0; i < ncpus; i++) {
1575 if (next_event[i] == NULL) /* this cpu exhausted */
1579 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1583 if (cpu < 0) /* all cpus exhausted */
1587 /* no wrapped cpus and this is he first ever event */
1588 t0 = next_event[cpu]->lwte_when;
1591 if (t0 <= next_event[cpu]->lwte_when) {
1592 /* on or after the first event */
1593 if (!printed_date) {
1594 cfs_cycles_t du = (tnow - t0) / mhz;
1595 time_t then = tvnow.tv_sec - du/1000000;
1597 if (du % 1000000 > tvnow.tv_usec)
1600 fprintf(f, "%s", ctime(&then));
1604 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1608 if (++nlines % 10000 == 0 && f != stdout) {
1609 /* show some activity... */
1615 tlast = next_event[cpu]->lwte_when;
1618 if (next_event[cpu] == cpu_event[cpu + 1])
1619 next_event[cpu] = cpu_event[cpu];
1621 if (next_event[cpu]->lwte_where == NULL ||
1622 next_event[cpu] == first_event[cpu])
1623 next_event[cpu] = NULL;
1635 int jt_ptl_memhog(int argc, char **argv)
1637 static int gfp = 0; /* sticky! */
1639 struct libcfs_ioctl_data data;
1645 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1649 count = strtol(argv[1], &end, 0);
1650 if (count < 0 || *end != 0) {
1651 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1656 rc = strtol(argv[2], &end, 0);
1658 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1664 LIBCFS_IOC_INIT(data);
1665 data.ioc_count = count;
1666 data.ioc_flags = gfp;
1667 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1670 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1674 printf("memhog %d OK\n", count);
1678 int jt_ptl_testprotocompat(int argc, char **argv)
1680 struct libcfs_ioctl_data data;
1686 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1690 flags = strtol(argv[1], &end, 0);
1691 if (flags < 0 || *end != 0) {
1692 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1696 LIBCFS_IOC_INIT(data);
1697 data.ioc_flags = flags;
1698 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1701 fprintf(stderr, "test proto compat %x failed: %s\n",
1702 flags, strerror(errno));
1706 printf("test proto compat %x OK\n", flags);