1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
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>
36 #include <sys/ioctl.h>
45 #include <netinet/in.h>
47 #endif /* __CYGWIN__ */
49 #include <portals/api-support.h>
50 #include <portals/ptlctl.h>
51 #include <portals/list.h>
52 #include <portals/lib-types.h>
53 #include <portals/socknal.h>
56 unsigned int portal_debug;
57 unsigned int portal_printk;
59 static unsigned int g_nal = 0;
67 static name2num_t nalnames[] = {
72 {"openib", OPENIBNAL},
78 static cfg_record_cb_t g_record_cb;
80 /* Convert a string boolean to an int; "enable" -> 1 */
81 int ptl_parse_bool (int *b, char *str) {
82 if (!strcasecmp (str, "no") ||
83 !strcasecmp (str, "n") ||
84 !strcasecmp (str, "off") ||
85 !strcasecmp (str, "down") ||
86 !strcasecmp (str, "disable"))
92 if (!strcasecmp (str, "yes") ||
93 !strcasecmp (str, "y") ||
94 !strcasecmp (str, "on") ||
95 !strcasecmp (str, "up") ||
96 !strcasecmp (str, "enable"))
105 /* Convert human readable size string to and int; "1k" -> 1000 */
106 int ptl_parse_size (int *sizep, char *str) {
110 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
143 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
150 pcfg_ioctl(struct portals_cfg *pcfg)
154 if (pcfg->pcfg_nal ==0)
155 pcfg->pcfg_nal = g_nal;
158 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
160 struct portal_ioctl_data data;
161 PORTAL_IOC_INIT (data);
162 data.ioc_pbuf1 = (char*)pcfg;
163 data.ioc_plen1 = sizeof(*pcfg);
164 /* XXX liblustre hack XXX */
165 data.ioc_nal_cmd = pcfg->pcfg_command;
166 data.ioc_nid = pcfg->pcfg_nid;
168 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
177 name2num_lookup_name (name2num_t *table, char *str)
179 while (table->name != NULL)
180 if (!strcmp (str, table->name))
188 name2num_lookup_num (name2num_t *table, int num)
190 while (table->name != NULL)
191 if (num == table->num)
199 ptl_name2nal (char *str)
201 name2num_t *e = name2num_lookup_name (nalnames, str);
203 return ((e == NULL) ? -1 : e->num);
209 name2num_t *e = name2num_lookup_num (nalnames, nal);
211 return ((e == NULL) ? "???" : e->name);
214 #ifdef HAVE_GETHOSTBYNAME
215 static struct hostent *
216 ptl_gethostbyname(char * hname) {
218 he = gethostbyname(hname);
223 fprintf(stderr, "Unable to resolve hostname: %s\n",
227 fprintf(stderr, "gethostbyname error: %s\n",
238 ptl_parse_port (int *port, char *str)
242 *port = strtol (str, &end, 0);
244 if (*end == 0 && /* parsed whole string */
245 *port > 0 && *port < 65536) /* minimal sanity check */
252 ptl_parse_time (time_t *t, char *str)
258 *t = strtol (str, &end, 0);
259 if (*end == 0) /* parsed whole string */
262 memset (&tm, 0, sizeof (tm));
263 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
264 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
265 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
269 tm.tm_mon--; /* convert to 0 == Jan */
270 tm.tm_year -= 1900; /* y2k quirk */
271 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
274 if (*t == (time_t)-1)
281 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
288 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
289 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
290 (c & ~0xff) == 0 && (d & ~0xff) == 0)
292 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
300 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
302 #ifdef HAVE_GETHOSTBYNAME
306 if (!strcmp (str, "_all_"))
312 if (ptl_parse_ipquad(ipaddrp, str) == 0)
315 #if HAVE_GETHOSTBYNAME
316 if ((('a' <= str[0] && str[0] <= 'z') ||
317 ('A' <= str[0] && str[0] <= 'Z')) &&
318 (he = ptl_gethostbyname (str)) != NULL)
320 __u32 addr = *(__u32 *)he->h_addr;
322 *ipaddrp = ntohl(addr); /* HOST byte order */
331 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
333 #ifdef HAVE_GETHOSTBYNAME
338 net_ip = htonl (ipaddr);
339 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
341 strcpy(str, he->h_name);
347 sprintf (str, "%d.%d.%d.%d",
348 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
349 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
354 ptl_parse_nid (ptl_nid_t *nidp, char *str)
358 unsigned long long ullval;
360 if (!strcmp (str, "_all_")) {
365 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
366 *nidp = (ptl_nid_t)ipaddr;
370 ullval = strtoull(str, &end, 0);
372 /* parsed whole string */
373 *nidp = (ptl_nid_t)ullval;
380 __u64 ptl_nid2u64(ptl_nid_t nid)
382 switch (sizeof (nid)) {
388 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
396 ptl_nid2str (char *buffer, ptl_nid_t nid)
398 __u64 nid64 = ptl_nid2u64(nid);
399 #ifdef HAVE_GETHOSTBYNAME
400 struct hostent *he = 0;
402 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
403 * TCP addresses in the 0.x.x.x subnet are not in use. This can
404 * happen on routers and slows things down a _lot_. Bug 3442. */
405 if (nid & 0xff000000) {
406 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
408 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
412 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
414 #endif /* HAVE_GETHOSTBYNAME */
415 sprintf(buffer, LPX64, nid64);
423 fprintf (stderr, "Error: you must run the 'network' command first.\n");
430 int g_nal_is_compatible (char *cmd, ...)
435 if (!g_nal_is_set ())
441 nal = va_arg (ap, int);
442 } while (nal != 0 && nal != g_nal);
450 /* Don't complain verbosely if we've not been passed a command
451 * name to complain about! */
452 fprintf (stderr, "Command %s not compatible with nal %s\n",
453 cmd, nal2name (g_nal));
459 sock_write (int cfd, void *buffer, int nob)
463 int rc = write (cfd, buffer, nob);
475 fprintf (stderr, "Unexpected zero sock_write\n");
480 buffer = (char *)buffer + nob;
487 sock_read (int cfd, void *buffer, int nob)
491 int rc = read (cfd, buffer, nob);
501 if (rc == 0) /* EOF */
503 errno = ECONNABORTED;
508 buffer = (char *)buffer + nob;
514 int ptl_initialize(int argc, char **argv)
516 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
521 int jt_ptl_network(int argc, char **argv)
527 (nal = ptl_name2nal (argv[1])) >= 0) {
532 fprintf(stderr, "usage: %s \n", argv[0]);
533 for (entry = nalnames; entry->name != NULL; entry++)
534 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
535 fprintf(stderr, ">\n");
540 jt_ptl_print_interfaces (int argc, char **argv)
542 struct portals_cfg pcfg;
547 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
550 for (index = 0;;index++) {
551 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
552 pcfg.pcfg_count = index;
554 rc = pcfg_ioctl (&pcfg);
558 printf ("%s: (%s/%s) npeer %d nroute %d\n",
559 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
560 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
561 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
562 pcfg.pcfg_fd, pcfg.pcfg_count);
566 printf ("<no interfaces>\n");
571 jt_ptl_add_interface (int argc, char **argv)
573 struct portals_cfg pcfg;
576 __u32 netmask = 0xffffff00;
581 if (argc < 2 || argc > 3) {
582 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
586 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
589 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
590 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
595 count = strtol(argv[2], &end, 0);
596 if (count > 0 && count < 32 && *end == 0) {
598 for (i = count; i > 0; i--)
599 netmask = netmask|(1<<(32-i));
600 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
601 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
606 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
607 pcfg.pcfg_id = ipaddr;
608 pcfg.pcfg_misc = netmask;
610 rc = pcfg_ioctl (&pcfg);
612 fprintf (stderr, "failed to add interface: %s\n",
621 jt_ptl_del_interface (int argc, char **argv)
623 struct portals_cfg pcfg;
628 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
632 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
636 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
637 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
641 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
642 pcfg.pcfg_id = ipaddr;
644 rc = pcfg_ioctl (&pcfg);
646 fprintf (stderr, "failed to delete interface: %s\n",
655 jt_ptl_print_peers (int argc, char **argv)
657 struct portals_cfg pcfg;
662 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
665 for (index = 0;;index++) {
666 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
667 pcfg.pcfg_count = index;
669 rc = pcfg_ioctl (&pcfg);
673 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
674 printf (LPX64"[%d]%s@%s:%d #%d\n",
675 pcfg.pcfg_nid, pcfg.pcfg_wait,
676 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
677 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
678 pcfg.pcfg_misc, pcfg.pcfg_count);
680 printf (LPX64"[%d]\n",
681 pcfg.pcfg_nid, pcfg.pcfg_wait);
685 printf ("<no peers>\n");
690 jt_ptl_add_peer (int argc, char **argv)
692 struct portals_cfg pcfg;
698 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
701 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
703 fprintf (stderr, "usage(tcp): %s nid ipaddr port\n",
707 } else if (argc != 2) {
708 fprintf (stderr, "usage(openib,iib): %s nid\n", argv[0]);
712 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
713 nid == PTL_NID_ANY) {
714 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
718 if (g_nal_is_compatible (NULL, SOCKNAL, 0)) {
719 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
720 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
724 if (ptl_parse_port (&port, argv[3]) != 0) {
725 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
730 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
733 pcfg.pcfg_misc = port;
735 rc = pcfg_ioctl (&pcfg);
737 fprintf (stderr, "failed to add peer: %s\n",
746 jt_ptl_del_peer (int argc, char **argv)
748 struct portals_cfg pcfg;
749 ptl_nid_t nid = PTL_NID_ANY;
751 int single_share = 0;
755 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
758 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
760 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
764 } else if (argc > 3) {
765 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
770 ptl_parse_nid (&nid, argv[1]) != 0) {
771 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
776 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
778 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
779 fprintf (stderr, "Can't parse ip addr: %s\n",
787 if (!strcmp (argv[argidx], "single_share")) {
790 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
795 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
798 pcfg.pcfg_flags = single_share;
800 rc = pcfg_ioctl (&pcfg);
802 fprintf (stderr, "failed to remove peer: %s\n",
811 jt_ptl_print_connections (int argc, char **argv)
813 struct portals_cfg pcfg;
818 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
821 for (index = 0;;index++) {
822 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
823 pcfg.pcfg_count = index;
825 rc = pcfg_ioctl (&pcfg);
829 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
830 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
831 pcfg.pcfg_gw_nal, /* scheduler */
832 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
834 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
835 pcfg.pcfg_misc, /* remote port */
836 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
837 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
838 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
839 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
840 pcfg.pcfg_count, /* tx buffer size */
841 pcfg.pcfg_size, /* rx buffer size */
842 pcfg.pcfg_wait ? "nagle" : "nonagle");
849 printf ("<no connections>\n");
853 int jt_ptl_connect(int argc, char **argv)
856 /* no connect() support */
858 #else /* HAVE_CONNECT */
859 struct portals_cfg pcfg;
860 struct sockaddr_in srvaddr;
861 struct sockaddr_in locaddr;
865 int type = SOCKNAL_CONN_ANY;
870 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
874 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
877 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
879 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
883 if (ptl_parse_port (&port, argv[2]) != 0) {
884 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
889 for (flag = argv[3]; *flag != 0; flag++)
893 if (type != SOCKNAL_CONN_ANY) {
894 fprintf(stderr, "Can't flag type twice\n");
897 type = SOCKNAL_CONN_BULK_IN;
901 if (type != SOCKNAL_CONN_ANY) {
902 fprintf(stderr, "Can't flag type twice\n");
905 type = SOCKNAL_CONN_BULK_OUT;
909 if (type != SOCKNAL_CONN_ANY) {
910 fprintf(stderr, "Can't flag type twice\n");
913 type = SOCKNAL_CONN_CONTROL;
917 fprintf (stderr, "unrecognised flag '%c'\n",
922 memset(&locaddr, 0, sizeof(locaddr));
923 locaddr.sin_family = AF_INET;
924 locaddr.sin_addr.s_addr = INADDR_ANY;
926 memset(&srvaddr, 0, sizeof(srvaddr));
927 srvaddr.sin_family = AF_INET;
928 srvaddr.sin_port = htons(port);
929 srvaddr.sin_addr.s_addr = htonl(ipaddr);
932 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
933 fd = socket(PF_INET, SOCK_STREAM, 0);
935 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
940 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
943 locaddr.sin_port = htons(rport);
944 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
945 if (rc == 0 || errno == EACCES) {
946 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
949 } else if (errno != EADDRINUSE) {
950 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
954 } else if (errno != EADDRINUSE) {
955 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
961 if (rport == IPPORT_RESERVED / 2) {
963 "Warning: all privileged ports are in use.\n");
967 printf("Connected host: %s type: %s\n",
969 (type == SOCKNAL_CONN_ANY) ? "A" :
970 (type == SOCKNAL_CONN_CONTROL) ? "C" :
971 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
972 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
974 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
975 pcfg.pcfg_nal = g_nal;
977 pcfg.pcfg_misc = type;
979 rc = pcfg_ioctl(&pcfg);
981 fprintf(stderr, "failed to register fd with portals: %s\n",
987 printf("Connection to %s registered with socknal\n", argv[1]);
991 fprintf(stderr, "close failed: %d\n", rc);
994 #endif /* HAVE_CONNECT */
997 int jt_ptl_disconnect(int argc, char **argv)
999 struct portals_cfg pcfg;
1000 ptl_nid_t nid = PTL_NID_ANY;
1005 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1009 if (!g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, IIBNAL, 0))
1013 ptl_parse_nid (&nid, argv[1]) != 0) {
1014 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1018 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1020 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1021 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1025 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1026 pcfg.pcfg_nid = nid;
1027 pcfg.pcfg_id = ipaddr;
1029 rc = pcfg_ioctl(&pcfg);
1031 fprintf(stderr, "failed to remove connection: %s\n",
1039 int jt_ptl_push_connection (int argc, char **argv)
1041 struct portals_cfg pcfg;
1043 ptl_nid_t nid = PTL_NID_ANY;
1047 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1051 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1055 ptl_parse_nid (&nid, argv[1]) != 0) {
1056 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1061 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1062 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1065 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1066 pcfg.pcfg_nid = nid;
1067 pcfg.pcfg_id = ipaddr;
1069 rc = pcfg_ioctl(&pcfg);
1071 fprintf(stderr, "failed to push connection: %s\n",
1080 jt_ptl_print_active_txs (int argc, char **argv)
1082 struct portals_cfg pcfg;
1086 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1089 for (index = 0;;index++) {
1090 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1091 pcfg.pcfg_count = index;
1093 rc = pcfg_ioctl(&pcfg);
1097 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1099 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1100 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1101 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1102 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1107 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1108 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1109 pcfg.pcfg_flags >> 2);
1113 printf ("<no active descs>\n");
1117 int jt_ptl_ping(int argc, char **argv)
1124 struct portal_ioctl_data data;
1127 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1131 if (!g_nal_is_set())
1134 if (ptl_parse_nid (&nid, argv[1]) != 0)
1136 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1142 count = atol(argv[2]);
1144 if (count < 0 || count > 20000)
1146 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1152 size= atol(argv[3]);
1155 timeout = atol (argv[4]);
1157 PORTAL_IOC_INIT (data);
1158 data.ioc_count = count;
1159 data.ioc_size = size;
1161 data.ioc_nal = g_nal;
1162 data.ioc_timeout = timeout;
1164 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1166 fprintf(stderr, "failed to start pinger: %s\n",
1173 int jt_ptl_shownid(int argc, char **argv)
1175 struct portal_ioctl_data data;
1179 fprintf(stderr, "usage: %s\n", argv[0]);
1183 if (!g_nal_is_set())
1186 PORTAL_IOC_INIT (data);
1187 data.ioc_nal = g_nal;
1188 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1190 fprintf(stderr, "getting my NID failed: %s\n",
1193 printf(LPX64"\n", data.ioc_nid);
1197 int jt_ptl_mynid(int argc, char **argv)
1200 char hostname[1024];
1202 struct portals_cfg pcfg;
1206 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1207 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1211 if (!g_nal_is_set())
1216 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1217 fprintf(stderr, "gethostname failed: %s\n",
1224 rc = ptl_parse_nid (&mynid, nidstr);
1226 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1230 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1231 pcfg.pcfg_nid = mynid;
1233 rc = pcfg_ioctl(&pcfg);
1235 fprintf(stderr, "setting my NID failed: %s\n",
1238 printf("registered my nid "LPX64" (%s)\n",
1239 ptl_nid2u64(mynid), hostname);
1244 jt_ptl_fail_nid (int argc, char **argv)
1248 unsigned int threshold;
1249 struct portal_ioctl_data data;
1251 if (argc < 2 || argc > 3)
1253 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1257 if (!g_nal_is_set())
1260 if (!strcmp (argv[1], "_all_"))
1262 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1264 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1269 threshold = PTL_MD_THRESH_INF;
1270 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1271 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1275 PORTAL_IOC_INIT (data);
1276 data.ioc_nal = g_nal;
1278 data.ioc_count = threshold;
1280 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1282 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1285 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1291 jt_ptl_add_route (int argc, char **argv)
1293 struct portals_cfg pcfg;
1296 ptl_nid_t gateway_nid;
1301 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1305 if (!g_nal_is_set())
1308 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1310 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1314 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1316 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1322 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1324 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1328 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1329 pcfg.pcfg_nid = gateway_nid;
1330 pcfg.pcfg_nal = ROUTER;
1331 pcfg.pcfg_gw_nal = g_nal;
1332 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1333 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1335 rc = pcfg_ioctl(&pcfg);
1338 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1346 jt_ptl_del_route (int argc, char **argv)
1348 struct portals_cfg pcfg;
1350 ptl_nid_t nid1 = PTL_NID_ANY;
1351 ptl_nid_t nid2 = PTL_NID_ANY;
1356 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1360 if (!g_nal_is_set())
1363 if (ptl_parse_nid (&nid, argv[1]) != 0)
1365 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1370 ptl_parse_nid (&nid1, argv[2]) != 0)
1372 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1379 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1380 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1385 ptl_nid_t tmp = nid1;
1392 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1393 pcfg.pcfg_nal = ROUTER;
1394 pcfg.pcfg_gw_nal = g_nal;
1395 pcfg.pcfg_nid = nid;
1396 pcfg.pcfg_nid2 = nid1;
1397 pcfg.pcfg_nid3 = nid2;
1399 rc = pcfg_ioctl(&pcfg);
1402 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1403 ptl_nid2u64(nid), strerror (errno));
1411 jt_ptl_notify_router (int argc, char **argv)
1413 struct portals_cfg pcfg;
1422 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1427 if (ptl_parse_nid (&nid, argv[1]) != 0)
1429 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1433 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1434 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1438 gettimeofday(&now, NULL);
1442 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1443 fprintf(stderr, "Can't parse time %s\n"
1444 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1445 "or an absolute unix time in seconds\n", argv[3]);
1447 } else if (when > now.tv_sec) {
1448 fprintf (stderr, "%s specifies a time in the future\n",
1453 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1454 pcfg.pcfg_nal = ROUTER;
1455 pcfg.pcfg_gw_nal = g_nal;
1456 pcfg.pcfg_nid = nid;
1457 pcfg.pcfg_flags = enable;
1458 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1459 pcfg.pcfg_nid3 = (__u64)when;
1461 rc = pcfg_ioctl(&pcfg);
1464 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1465 ptl_nid2u64(nid), strerror (errno));
1473 jt_ptl_print_routes (int argc, char **argv)
1475 char buffer[3][128];
1476 struct portals_cfg pcfg;
1480 ptl_nid_t gateway_nid;
1485 for (index = 0;;index++)
1487 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1488 pcfg.pcfg_nal = ROUTER;
1489 pcfg.pcfg_count = index;
1491 rc = pcfg_ioctl(&pcfg);
1495 gateway_nal = pcfg.pcfg_gw_nal;
1496 gateway_nid = pcfg.pcfg_nid;
1497 nid1 = pcfg.pcfg_nid2;
1498 nid2 = pcfg.pcfg_nid3;
1499 alive = pcfg.pcfg_flags;
1501 printf ("%8s %18s : %s - %s, %s\n",
1502 nal2name (gateway_nal),
1503 ptl_nid2str (buffer[0], gateway_nid),
1504 ptl_nid2str (buffer[1], nid1),
1505 ptl_nid2str (buffer[2], nid2),
1506 alive ? "up" : "down");
1512 lwt_control(int enable, int clear)
1514 struct portal_ioctl_data data;
1517 PORTAL_IOC_INIT(data);
1518 data.ioc_flags = enable;
1519 data.ioc_misc = clear;
1521 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1525 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1531 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1532 lwt_event_t *events, int size)
1534 struct portal_ioctl_data data;
1537 PORTAL_IOC_INIT(data);
1538 data.ioc_pbuf1 = (char *)events;
1539 data.ioc_plen1 = size;
1541 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1543 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1548 /* crappy overloads */
1549 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1550 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1551 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1552 (int)data.ioc_nid2, sizeof(lwt_event_t),
1554 (int)offsetof(lwt_event_t, lwte_where));
1558 LASSERT (data.ioc_count != 0);
1559 LASSERT (data.ioc_misc != 0);
1562 *now = data.ioc_nid;
1565 *ncpu = data.ioc_count;
1567 if (totalsize != NULL)
1568 *totalsize = data.ioc_misc;
1574 lwt_get_string(char *kstr)
1577 struct portal_ioctl_data data;
1581 /* FIXME: this could maintain a symbol table since we expect to be
1582 * looking up the same strings all the time... */
1584 PORTAL_IOC_INIT(data);
1585 data.ioc_pbuf1 = kstr;
1586 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1587 data.ioc_pbuf2 = NULL;
1590 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1592 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1597 size = data.ioc_count;
1598 ustr = (char *)malloc(size);
1600 fprintf(stderr, "Can't allocate string storage of size %d\n",
1605 PORTAL_IOC_INIT(data);
1606 data.ioc_pbuf1 = kstr;
1607 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1608 data.ioc_pbuf2 = ustr;
1609 data.ioc_plen2 = size;
1611 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1613 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1618 LASSERT(strlen(ustr) == size - 1);
1623 lwt_put_string(char *ustr)
1629 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1632 # error "__WORDSIZE not defined"
1633 #elif __WORDSIZE == 32
1634 # define XFMT "%#010lx"
1635 #elif __WORDSIZE== 64
1636 # define XFMT "%#018lx"
1638 # error "Unexpected __WORDSIZE"
1640 char *where = lwt_get_string(e->lwte_where);
1645 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1646 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1647 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1648 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1651 lwt_put_string(where);
1658 get_cycles_per_usec ()
1660 FILE *f = fopen ("/proc/cpuinfo", "r");
1665 while (fgets (line, sizeof (line), f) != NULL)
1666 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1673 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1678 jt_ptl_lwt(int argc, char **argv)
1680 const int lwt_max_cpus = 32;
1683 int nevents_per_cpu;
1684 lwt_event_t *events;
1685 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1686 lwt_event_t *next_event[lwt_max_cpus];
1687 lwt_event_t *first_event[lwt_max_cpus];
1696 struct timeval tvnow;
1697 int printed_date = 0;
1702 (strcmp(argv[1], "start") &&
1703 strcmp(argv[1], "stop"))) {
1706 " %s stop [fname]\n", argv[0], argv[0]);
1710 if (!strcmp(argv[1], "start")) {
1712 if (lwt_control(0, 0) != 0)
1716 if (lwt_control(0, 1) != 0)
1720 if (lwt_control(1, 0) != 0)
1726 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1729 if (ncpus > lwt_max_cpus) {
1730 fprintf(stderr, "Too many cpus: %d (%d)\n",
1731 ncpus, lwt_max_cpus);
1735 events = (lwt_event_t *)malloc(totalspace);
1736 if (events == NULL) {
1737 fprintf(stderr, "Can't allocate %d\n", totalspace);
1741 if (lwt_control(0, 0) != 0) { /* disable */
1746 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1751 /* we want this time to be sampled at snapshot time */
1752 gettimeofday(&tvnow, NULL);
1755 f = fopen (argv[2], "w");
1757 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1763 mhz = get_cycles_per_usec();
1765 /* carve events into per-cpu slices */
1766 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1767 for (cpu = 0; cpu <= ncpus; cpu++)
1768 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1770 /* find the earliest event on each cpu */
1771 for (cpu = 0; cpu < ncpus; cpu++) {
1772 first_event[cpu] = NULL;
1774 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1776 if (e->lwte_where == NULL) /* not an event */
1779 if (first_event[cpu] == NULL ||
1780 first_event[cpu]->lwte_when > e->lwte_when)
1781 first_event[cpu] = e;
1784 next_event[cpu] = first_event[cpu];
1788 for (cpu = 0; cpu < ncpus; cpu++) {
1789 e = first_event[cpu];
1790 if (e == NULL) /* no events this cpu */
1793 if (e == cpu_event[cpu])
1794 e = cpu_event[cpu + 1] - 1;
1798 /* If there's an event immediately before the first one, this
1799 * cpu wrapped its event buffer */
1800 if (e->lwte_where == NULL)
1803 /* We should only start outputting events from the most recent
1804 * first event in any wrapped cpu. Events before this time on
1805 * other cpus won't have any events from this CPU to interleave
1807 if (t0 < first_event[cpu]->lwte_when)
1808 t0 = first_event[cpu]->lwte_when;
1812 /* find which cpu has the next event */
1814 for (i = 0; i < ncpus; i++) {
1816 if (next_event[i] == NULL) /* this cpu exhausted */
1820 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1824 if (cpu < 0) /* all cpus exhausted */
1828 /* no wrapped cpus and this is he first ever event */
1829 t0 = next_event[cpu]->lwte_when;
1832 if (t0 <= next_event[cpu]->lwte_when) {
1833 /* on or after the first event */
1834 if (!printed_date) {
1835 cycles_t du = (tnow - t0) / mhz;
1836 time_t then = tvnow.tv_sec - du/1000000;
1838 if (du % 1000000 > tvnow.tv_usec)
1841 fprintf(f, "%s", ctime(&then));
1845 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1849 if (++nlines % 10000 == 0 && f != stdout) {
1850 /* show some activity... */
1856 tlast = next_event[cpu]->lwte_when;
1859 if (next_event[cpu] == cpu_event[cpu + 1])
1860 next_event[cpu] = cpu_event[cpu];
1862 if (next_event[cpu]->lwte_where == NULL ||
1863 next_event[cpu] == first_event[cpu])
1864 next_event[cpu] = NULL;
1876 int jt_ptl_memhog(int argc, char **argv)
1878 static int gfp = 0; /* sticky! */
1880 struct portal_ioctl_data data;
1886 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1890 count = strtol(argv[1], &end, 0);
1891 if (count < 0 || *end != 0) {
1892 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1897 rc = strtol(argv[2], &end, 0);
1899 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1905 PORTAL_IOC_INIT(data);
1906 data.ioc_count = count;
1907 data.ioc_flags = gfp;
1908 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1911 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1915 printf("memhog %d OK\n", count);