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>
46 #include <portals/ipmap.h>
51 #include <netinet/in.h>
53 #endif /* __CYGWIN__ */
55 #include <portals/api-support.h>
56 #include <portals/ptlctl.h>
57 #include <portals/list.h>
58 #include <portals/lib-types.h>
59 #include <portals/socknal.h>
62 unsigned int portal_debug;
63 unsigned int portal_printk;
65 static unsigned int g_nal = 0;
73 static name2num_t nalnames[] = {
79 {"openib", OPENIBNAL},
85 {"cray_kern_nal", CRAY_KERN_NAL},
86 {"cray_user_nal", CRAY_USER_NAL},
87 {"cray_qk_nal", CRAY_QK_NAL},
92 static cfg_record_cb_t g_record_cb;
94 /* Convert a string boolean to an int; "enable" -> 1 */
95 int ptl_parse_bool (int *b, char *str) {
96 if (!strcasecmp (str, "no") ||
97 !strcasecmp (str, "n") ||
98 !strcasecmp (str, "off") ||
99 !strcasecmp (str, "down") ||
100 !strcasecmp (str, "disable"))
106 if (!strcasecmp (str, "yes") ||
107 !strcasecmp (str, "y") ||
108 !strcasecmp (str, "on") ||
109 !strcasecmp (str, "up") ||
110 !strcasecmp (str, "enable"))
119 /* Convert human readable size string to and int; "1k" -> 1000 */
120 int ptl_parse_size (int *sizep, char *str) {
124 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
157 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
164 pcfg_ioctl(struct portals_cfg *pcfg)
168 if (pcfg->pcfg_nal ==0)
169 pcfg->pcfg_nal = g_nal;
172 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
174 struct portal_ioctl_data data;
175 PORTAL_IOC_INIT (data);
176 data.ioc_pbuf1 = (char*)pcfg;
177 data.ioc_plen1 = sizeof(*pcfg);
178 /* XXX liblustre hack XXX */
179 data.ioc_nal_cmd = pcfg->pcfg_command;
180 data.ioc_nid = pcfg->pcfg_nid;
182 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
191 name2num_lookup_name (name2num_t *table, char *str)
193 while (table->name != NULL)
194 if (!strcmp (str, table->name))
202 name2num_lookup_num (name2num_t *table, int num)
204 while (table->name != NULL)
205 if (num == table->num)
213 ptl_name2nal (char *str)
215 name2num_t *e = name2num_lookup_name (nalnames, str);
217 return ((e == NULL) ? -1 : e->num);
223 name2num_t *e = name2num_lookup_num (nalnames, nal);
225 return ((e == NULL) ? "???" : e->name);
228 #ifdef HAVE_GETHOSTBYNAME
229 static struct hostent *
230 ptl_gethostbyname(char * hname) {
232 he = gethostbyname(hname);
237 fprintf(stderr, "Unable to resolve hostname: %s\n",
241 fprintf(stderr, "gethostbyname error: %s\n",
252 ptl_parse_port (int *port, char *str)
256 *port = strtol (str, &end, 0);
258 if (*end == 0 && /* parsed whole string */
259 *port > 0 && *port < 65536) /* minimal sanity check */
266 ptl_parse_time (time_t *t, char *str)
272 *t = strtol (str, &end, 0);
273 if (*end == 0) /* parsed whole string */
276 memset (&tm, 0, sizeof (tm));
277 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
278 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
279 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
283 tm.tm_mon--; /* convert to 0 == Jan */
284 tm.tm_year -= 1900; /* y2k quirk */
285 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
288 if (*t == (time_t)-1)
295 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
302 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
303 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
304 (c & ~0xff) == 0 && (d & ~0xff) == 0)
306 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
314 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
316 #ifdef HAVE_GETHOSTBYNAME
320 if (!strcmp (str, "_all_"))
326 if (ptl_parse_ipquad(ipaddrp, str) == 0)
329 #if HAVE_GETHOSTBYNAME
330 if ((('a' <= str[0] && str[0] <= 'z') ||
331 ('A' <= str[0] && str[0] <= 'Z')) &&
332 (he = ptl_gethostbyname (str)) != NULL)
334 __u32 addr = *(__u32 *)he->h_addr;
336 *ipaddrp = ntohl(addr); /* HOST byte order */
345 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
347 #ifdef HAVE_GETHOSTBYNAME
352 net_ip = htonl (ipaddr);
353 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
355 strcpy(str, he->h_name);
361 sprintf (str, "%d.%d.%d.%d",
362 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
363 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
368 ptl_parse_nid (ptl_nid_t *nidp, char *str)
372 unsigned long long ullval;
374 if (!strcmp (str, "_all_")) {
379 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
381 *nidp = (ptl_nid_t)ipaddr;
383 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
388 ullval = strtoull(str, &end, 0);
390 /* parsed whole string */
391 *nidp = (ptl_nid_t)ullval;
398 __u64 ptl_nid2u64(ptl_nid_t nid)
400 switch (sizeof (nid)) {
406 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
414 ptl_nid2str (char *buffer, ptl_nid_t nid)
416 __u64 nid64 = ptl_nid2u64(nid);
417 #ifdef HAVE_GETHOSTBYNAME
418 struct hostent *he = 0;
420 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
421 * TCP addresses in the 0.x.x.x subnet are not in use. This can
422 * happen on routers and slows things down a _lot_. Bug 3442. */
423 if (nid & 0xff000000) {
424 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
426 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
430 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
432 #endif /* HAVE_GETHOSTBYNAME */
433 sprintf(buffer, LPX64, nid64);
441 fprintf (stderr, "Error: you must run the 'network' command first.\n");
448 int g_nal_is_compatible (char *cmd, ...)
453 if (!g_nal_is_set ())
459 nal = va_arg (ap, int);
460 } while (nal != 0 && nal != g_nal);
468 /* Don't complain verbosely if we've not been passed a command
469 * name to complain about! */
470 fprintf (stderr, "Command %s not compatible with nal %s\n",
471 cmd, nal2name (g_nal));
477 sock_write (int cfd, void *buffer, int nob)
481 int rc = write (cfd, buffer, nob);
493 fprintf (stderr, "Unexpected zero sock_write\n");
498 buffer = (char *)buffer + nob;
505 sock_read (int cfd, void *buffer, int nob)
509 int rc = read (cfd, buffer, nob);
519 if (rc == 0) /* EOF */
521 errno = ECONNABORTED;
526 buffer = (char *)buffer + nob;
532 int ptl_initialize(int argc, char **argv)
534 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
539 int jt_ptl_network(int argc, char **argv)
545 (nal = ptl_name2nal (argv[1])) >= 0) {
550 fprintf(stderr, "usage: %s \n", argv[0]);
551 for (entry = nalnames; entry->name != NULL; entry++)
552 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
553 fprintf(stderr, ">\n");
558 jt_ptl_print_interfaces (int argc, char **argv)
560 struct portals_cfg pcfg;
565 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
568 for (index = 0;;index++) {
569 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
570 pcfg.pcfg_count = index;
572 rc = pcfg_ioctl (&pcfg);
576 printf ("%s: (%s/%s) npeer %d nroute %d\n",
577 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
578 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
579 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
580 pcfg.pcfg_fd, pcfg.pcfg_count);
584 printf ("<no interfaces>\n");
589 jt_ptl_add_interface (int argc, char **argv)
591 struct portals_cfg pcfg;
594 __u32 netmask = 0xffffff00;
599 if (argc < 2 || argc > 3) {
600 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
604 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
607 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
608 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
613 count = strtol(argv[2], &end, 0);
614 if (count > 0 && count < 32 && *end == 0) {
616 for (i = count; i > 0; i--)
617 netmask = netmask|(1<<(32-i));
618 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
619 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
624 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
625 pcfg.pcfg_id = ipaddr;
626 pcfg.pcfg_misc = netmask;
628 rc = pcfg_ioctl (&pcfg);
630 fprintf (stderr, "failed to add interface: %s\n",
639 jt_ptl_del_interface (int argc, char **argv)
641 struct portals_cfg pcfg;
646 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
650 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
654 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
655 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
659 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
660 pcfg.pcfg_id = ipaddr;
662 rc = pcfg_ioctl (&pcfg);
664 fprintf (stderr, "failed to delete interface: %s\n",
673 jt_ptl_print_peers (int argc, char **argv)
675 struct portals_cfg pcfg;
680 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
681 OPENIBNAL, IIBNAL, VIBNAL, 0))
684 for (index = 0;;index++) {
685 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
686 pcfg.pcfg_count = index;
688 rc = pcfg_ioctl (&pcfg);
692 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
693 printf (LPX64"[%d]%s@%s:%d #%d\n",
694 pcfg.pcfg_nid, pcfg.pcfg_wait,
695 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
696 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
697 pcfg.pcfg_misc, pcfg.pcfg_count);
698 else if (g_nal_is_compatible(NULL, RANAL, 0))
699 printf (LPX64"[%d]@%s:%d\n",
700 pcfg.pcfg_nid, pcfg.pcfg_wait,
701 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
704 printf (LPX64"[%d]\n",
705 pcfg.pcfg_nid, pcfg.pcfg_wait);
709 printf ("<no peers>\n");
714 jt_ptl_add_peer (int argc, char **argv)
716 struct portals_cfg pcfg;
722 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
723 OPENIBNAL, IIBNAL, VIBNAL, 0))
726 if (g_nal_is_compatible(NULL, SOCKNAL, RANAL, 0)) {
728 fprintf (stderr, "usage(tcp,ra): %s nid ipaddr port\n",
732 } else if (argc != 2) {
733 fprintf (stderr, "usage(openib,iib,vib): %s nid\n", argv[0]);
737 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
738 nid == PTL_NID_ANY) {
739 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
743 if (g_nal_is_compatible (NULL, SOCKNAL, RANAL, 0)) {
744 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
745 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
749 if (ptl_parse_port (&port, argv[3]) != 0) {
750 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
755 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
758 pcfg.pcfg_misc = port;
760 rc = pcfg_ioctl (&pcfg);
762 fprintf (stderr, "failed to add peer: %s\n",
771 jt_ptl_del_peer (int argc, char **argv)
773 struct portals_cfg pcfg;
774 ptl_nid_t nid = PTL_NID_ANY;
776 int single_share = 0;
780 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
781 OPENIBNAL, IIBNAL, VIBNAL, 0))
784 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
786 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
790 } else if (argc > 3) {
791 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
796 ptl_parse_nid (&nid, argv[1]) != 0) {
797 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
802 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
804 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
805 fprintf (stderr, "Can't parse ip addr: %s\n",
813 if (!strcmp (argv[argidx], "single_share")) {
816 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
821 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
824 pcfg.pcfg_flags = single_share;
826 rc = pcfg_ioctl (&pcfg);
828 fprintf (stderr, "failed to remove peer: %s\n",
837 jt_ptl_print_connections (int argc, char **argv)
839 struct portals_cfg pcfg;
844 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
845 OPENIBNAL, IIBNAL, VIBNAL, 0))
848 for (index = 0;;index++) {
849 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
850 pcfg.pcfg_count = index;
852 rc = pcfg_ioctl (&pcfg);
856 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
857 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
858 pcfg.pcfg_gw_nal, /* scheduler */
859 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
861 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
862 pcfg.pcfg_misc, /* remote port */
863 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
864 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
865 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
866 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
867 pcfg.pcfg_count, /* tx buffer size */
868 pcfg.pcfg_size, /* rx buffer size */
869 pcfg.pcfg_wait ? "nagle" : "nonagle");
870 else if (g_nal_is_compatible (NULL, RANAL, 0))
871 printf ("[%d]"LPX64"\n",
872 pcfg.pcfg_id, /* device id */
880 printf ("<no connections>\n");
884 int jt_ptl_connect(int argc, char **argv)
887 /* no connect() support */
889 #else /* HAVE_CONNECT */
890 struct portals_cfg pcfg;
891 struct sockaddr_in srvaddr;
892 struct sockaddr_in locaddr;
896 int type = SOCKNAL_CONN_ANY;
901 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
905 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
908 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
910 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
914 if (ptl_parse_port (&port, argv[2]) != 0) {
915 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
920 for (flag = argv[3]; *flag != 0; flag++)
924 if (type != SOCKNAL_CONN_ANY) {
925 fprintf(stderr, "Can't flag type twice\n");
928 type = SOCKNAL_CONN_BULK_IN;
932 if (type != SOCKNAL_CONN_ANY) {
933 fprintf(stderr, "Can't flag type twice\n");
936 type = SOCKNAL_CONN_BULK_OUT;
940 if (type != SOCKNAL_CONN_ANY) {
941 fprintf(stderr, "Can't flag type twice\n");
944 type = SOCKNAL_CONN_CONTROL;
948 fprintf (stderr, "unrecognised flag '%c'\n",
953 memset(&locaddr, 0, sizeof(locaddr));
954 locaddr.sin_family = AF_INET;
955 locaddr.sin_addr.s_addr = INADDR_ANY;
957 memset(&srvaddr, 0, sizeof(srvaddr));
958 srvaddr.sin_family = AF_INET;
959 srvaddr.sin_port = htons(port);
960 srvaddr.sin_addr.s_addr = htonl(ipaddr);
963 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
964 fd = socket(PF_INET, SOCK_STREAM, 0);
966 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
971 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
974 locaddr.sin_port = htons(rport);
975 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
976 if (rc == 0 || errno == EACCES) {
977 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
980 } else if (errno != EADDRINUSE) {
981 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
985 } else if (errno != EADDRINUSE) {
986 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
992 if (rport == IPPORT_RESERVED / 2) {
994 "Warning: all privileged ports are in use.\n");
998 printf("Connected host: %s type: %s\n",
1000 (type == SOCKNAL_CONN_ANY) ? "A" :
1001 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1002 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1003 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1005 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1006 pcfg.pcfg_nal = g_nal;
1008 pcfg.pcfg_misc = type;
1010 rc = pcfg_ioctl(&pcfg);
1012 fprintf(stderr, "failed to register fd with portals: %s\n",
1018 printf("Connection to %s registered with socknal\n", argv[1]);
1022 fprintf(stderr, "close failed: %d\n", rc);
1025 #endif /* HAVE_CONNECT */
1028 int jt_ptl_disconnect(int argc, char **argv)
1030 struct portals_cfg pcfg;
1031 ptl_nid_t nid = PTL_NID_ANY;
1036 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1040 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1041 OPENIBNAL, IIBNAL, VIBNAL, 0))
1045 ptl_parse_nid (&nid, argv[1]) != 0) {
1046 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1050 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1052 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1053 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1057 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1058 pcfg.pcfg_nid = nid;
1059 pcfg.pcfg_id = ipaddr;
1061 rc = pcfg_ioctl(&pcfg);
1063 fprintf(stderr, "failed to remove connection: %s\n",
1071 int jt_ptl_push_connection (int argc, char **argv)
1073 struct portals_cfg pcfg;
1075 ptl_nid_t nid = PTL_NID_ANY;
1079 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1083 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1087 ptl_parse_nid (&nid, argv[1]) != 0) {
1088 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1093 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1094 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1097 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1098 pcfg.pcfg_nid = nid;
1099 pcfg.pcfg_id = ipaddr;
1101 rc = pcfg_ioctl(&pcfg);
1103 fprintf(stderr, "failed to push connection: %s\n",
1112 jt_ptl_print_active_txs (int argc, char **argv)
1114 struct portals_cfg pcfg;
1118 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1121 for (index = 0;;index++) {
1122 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1123 pcfg.pcfg_count = index;
1125 rc = pcfg_ioctl(&pcfg);
1129 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1131 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1132 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1133 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1134 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1139 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1140 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1141 pcfg.pcfg_flags >> 2);
1145 printf ("<no active descs>\n");
1149 int jt_ptl_ping(int argc, char **argv)
1156 struct portal_ioctl_data data;
1159 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1163 if (!g_nal_is_set())
1166 if (ptl_parse_nid (&nid, argv[1]) != 0)
1168 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1174 count = atol(argv[2]);
1176 if (count < 0 || count > 20000)
1178 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1184 size= atol(argv[3]);
1187 timeout = atol (argv[4]);
1189 PORTAL_IOC_INIT (data);
1190 data.ioc_count = count;
1191 data.ioc_size = size;
1193 data.ioc_nal = g_nal;
1194 data.ioc_timeout = timeout;
1196 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1198 fprintf(stderr, "failed to start pinger: %s\n",
1205 int jt_ptl_shownid(int argc, char **argv)
1207 struct portal_ioctl_data data;
1211 fprintf(stderr, "usage: %s\n", argv[0]);
1215 if (!g_nal_is_set())
1218 PORTAL_IOC_INIT (data);
1219 data.ioc_nal = g_nal;
1220 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1222 fprintf(stderr, "getting my NID failed: %s\n",
1225 printf(LPX64"\n", data.ioc_nid);
1229 int jt_ptl_mynid(int argc, char **argv)
1232 char hostname[1024];
1234 struct portals_cfg pcfg;
1238 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1239 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1243 if (!g_nal_is_set())
1248 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1249 fprintf(stderr, "gethostname failed: %s\n",
1256 rc = ptl_parse_nid (&mynid, nidstr);
1258 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1262 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1263 pcfg.pcfg_nid = mynid;
1265 rc = pcfg_ioctl(&pcfg);
1267 fprintf(stderr, "setting my NID failed: %s\n",
1270 printf("registered my nid "LPX64" (%s)\n",
1271 ptl_nid2u64(mynid), hostname);
1276 jt_ptl_fail_nid (int argc, char **argv)
1280 unsigned int threshold;
1281 struct portal_ioctl_data data;
1283 if (argc < 2 || argc > 3)
1285 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1289 if (!g_nal_is_set())
1292 if (!strcmp (argv[1], "_all_"))
1294 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1296 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1301 threshold = PTL_MD_THRESH_INF;
1302 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1303 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1307 PORTAL_IOC_INIT (data);
1308 data.ioc_nal = g_nal;
1310 data.ioc_count = threshold;
1312 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1314 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1317 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1323 jt_ptl_add_route (int argc, char **argv)
1325 struct portals_cfg pcfg;
1328 ptl_nid_t gateway_nid;
1333 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1337 if (!g_nal_is_set())
1340 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1342 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1346 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1348 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1354 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1356 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1360 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1361 pcfg.pcfg_nid = gateway_nid;
1362 pcfg.pcfg_nal = ROUTER;
1363 pcfg.pcfg_gw_nal = g_nal;
1364 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1365 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1367 rc = pcfg_ioctl(&pcfg);
1370 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1378 jt_ptl_del_route (int argc, char **argv)
1380 struct portals_cfg pcfg;
1382 ptl_nid_t nid1 = PTL_NID_ANY;
1383 ptl_nid_t nid2 = PTL_NID_ANY;
1388 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1392 if (!g_nal_is_set())
1395 if (ptl_parse_nid (&nid, argv[1]) != 0)
1397 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1402 ptl_parse_nid (&nid1, argv[2]) != 0)
1404 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1411 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1412 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1417 ptl_nid_t tmp = nid1;
1424 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1425 pcfg.pcfg_nal = ROUTER;
1426 pcfg.pcfg_gw_nal = g_nal;
1427 pcfg.pcfg_nid = nid;
1428 pcfg.pcfg_nid2 = nid1;
1429 pcfg.pcfg_nid3 = nid2;
1431 rc = pcfg_ioctl(&pcfg);
1434 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1435 ptl_nid2u64(nid), strerror (errno));
1443 jt_ptl_notify_router (int argc, char **argv)
1445 struct portals_cfg pcfg;
1454 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1459 if (ptl_parse_nid (&nid, argv[1]) != 0)
1461 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1465 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1466 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1470 gettimeofday(&now, NULL);
1474 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1475 fprintf(stderr, "Can't parse time %s\n"
1476 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1477 "or an absolute unix time in seconds\n", argv[3]);
1479 } else if (when > now.tv_sec) {
1480 fprintf (stderr, "%s specifies a time in the future\n",
1485 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1486 pcfg.pcfg_nal = ROUTER;
1487 pcfg.pcfg_gw_nal = g_nal;
1488 pcfg.pcfg_nid = nid;
1489 pcfg.pcfg_flags = enable;
1490 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1491 pcfg.pcfg_nid3 = (__u64)when;
1493 rc = pcfg_ioctl(&pcfg);
1496 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1497 ptl_nid2u64(nid), strerror (errno));
1505 jt_ptl_print_routes (int argc, char **argv)
1507 char buffer[3][128];
1508 struct portals_cfg pcfg;
1512 ptl_nid_t gateway_nid;
1517 for (index = 0;;index++)
1519 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1520 pcfg.pcfg_nal = ROUTER;
1521 pcfg.pcfg_count = index;
1523 rc = pcfg_ioctl(&pcfg);
1527 gateway_nal = pcfg.pcfg_gw_nal;
1528 gateway_nid = pcfg.pcfg_nid;
1529 nid1 = pcfg.pcfg_nid2;
1530 nid2 = pcfg.pcfg_nid3;
1531 alive = pcfg.pcfg_flags;
1533 printf ("%8s %18s : %s - %s, %s\n",
1534 nal2name (gateway_nal),
1535 ptl_nid2str (buffer[0], gateway_nid),
1536 ptl_nid2str (buffer[1], nid1),
1537 ptl_nid2str (buffer[2], nid2),
1538 alive ? "up" : "down");
1544 lwt_control(int enable, int clear)
1546 struct portal_ioctl_data data;
1549 PORTAL_IOC_INIT(data);
1550 data.ioc_flags = enable;
1551 data.ioc_misc = clear;
1553 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1557 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1563 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1564 lwt_event_t *events, int size)
1566 struct portal_ioctl_data data;
1569 PORTAL_IOC_INIT(data);
1570 data.ioc_pbuf1 = (char *)events;
1571 data.ioc_plen1 = size;
1573 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1575 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1580 /* crappy overloads */
1581 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1582 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1583 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1584 (int)data.ioc_nid2, sizeof(lwt_event_t),
1586 (int)offsetof(lwt_event_t, lwte_where));
1590 LASSERT (data.ioc_count != 0);
1591 LASSERT (data.ioc_misc != 0);
1594 *now = data.ioc_nid;
1597 *ncpu = data.ioc_count;
1599 if (totalsize != NULL)
1600 *totalsize = data.ioc_misc;
1606 lwt_get_string(char *kstr)
1609 struct portal_ioctl_data data;
1613 /* FIXME: this could maintain a symbol table since we expect to be
1614 * looking up the same strings all the time... */
1616 PORTAL_IOC_INIT(data);
1617 data.ioc_pbuf1 = kstr;
1618 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1619 data.ioc_pbuf2 = NULL;
1622 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1624 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1629 size = data.ioc_count;
1630 ustr = (char *)malloc(size);
1632 fprintf(stderr, "Can't allocate string storage of size %d\n",
1637 PORTAL_IOC_INIT(data);
1638 data.ioc_pbuf1 = kstr;
1639 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1640 data.ioc_pbuf2 = ustr;
1641 data.ioc_plen2 = size;
1643 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1645 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1650 LASSERT(strlen(ustr) == size - 1);
1655 lwt_put_string(char *ustr)
1661 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1664 # error "__WORDSIZE not defined"
1665 #elif __WORDSIZE == 32
1666 # define XFMT "%#010lx"
1667 #elif __WORDSIZE== 64
1668 # define XFMT "%#018lx"
1670 # error "Unexpected __WORDSIZE"
1672 char *where = lwt_get_string(e->lwte_where);
1677 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1678 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1679 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1680 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1683 lwt_put_string(where);
1690 get_cycles_per_usec ()
1692 FILE *f = fopen ("/proc/cpuinfo", "r");
1697 while (fgets (line, sizeof (line), f) != NULL)
1698 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1705 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1710 jt_ptl_lwt(int argc, char **argv)
1712 const int lwt_max_cpus = 32;
1715 int nevents_per_cpu;
1716 lwt_event_t *events;
1717 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1718 lwt_event_t *next_event[lwt_max_cpus];
1719 lwt_event_t *first_event[lwt_max_cpus];
1728 struct timeval tvnow;
1729 int printed_date = 0;
1734 (strcmp(argv[1], "start") &&
1735 strcmp(argv[1], "stop"))) {
1738 " %s stop [fname]\n", argv[0], argv[0]);
1742 if (!strcmp(argv[1], "start")) {
1744 if (lwt_control(0, 0) != 0)
1748 if (lwt_control(0, 1) != 0)
1752 if (lwt_control(1, 0) != 0)
1758 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1761 if (ncpus > lwt_max_cpus) {
1762 fprintf(stderr, "Too many cpus: %d (%d)\n",
1763 ncpus, lwt_max_cpus);
1767 events = (lwt_event_t *)malloc(totalspace);
1768 if (events == NULL) {
1769 fprintf(stderr, "Can't allocate %d\n", totalspace);
1773 if (lwt_control(0, 0) != 0) { /* disable */
1778 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1783 /* we want this time to be sampled at snapshot time */
1784 gettimeofday(&tvnow, NULL);
1787 f = fopen (argv[2], "w");
1789 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1795 mhz = get_cycles_per_usec();
1797 /* carve events into per-cpu slices */
1798 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1799 for (cpu = 0; cpu <= ncpus; cpu++)
1800 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1802 /* find the earliest event on each cpu */
1803 for (cpu = 0; cpu < ncpus; cpu++) {
1804 first_event[cpu] = NULL;
1806 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1808 if (e->lwte_where == NULL) /* not an event */
1811 if (first_event[cpu] == NULL ||
1812 first_event[cpu]->lwte_when > e->lwte_when)
1813 first_event[cpu] = e;
1816 next_event[cpu] = first_event[cpu];
1820 for (cpu = 0; cpu < ncpus; cpu++) {
1821 e = first_event[cpu];
1822 if (e == NULL) /* no events this cpu */
1825 if (e == cpu_event[cpu])
1826 e = cpu_event[cpu + 1] - 1;
1830 /* If there's an event immediately before the first one, this
1831 * cpu wrapped its event buffer */
1832 if (e->lwte_where == NULL)
1835 /* We should only start outputting events from the most recent
1836 * first event in any wrapped cpu. Events before this time on
1837 * other cpus won't have any events from this CPU to interleave
1839 if (t0 < first_event[cpu]->lwte_when)
1840 t0 = first_event[cpu]->lwte_when;
1844 /* find which cpu has the next event */
1846 for (i = 0; i < ncpus; i++) {
1848 if (next_event[i] == NULL) /* this cpu exhausted */
1852 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1856 if (cpu < 0) /* all cpus exhausted */
1860 /* no wrapped cpus and this is he first ever event */
1861 t0 = next_event[cpu]->lwte_when;
1864 if (t0 <= next_event[cpu]->lwte_when) {
1865 /* on or after the first event */
1866 if (!printed_date) {
1867 cycles_t du = (tnow - t0) / mhz;
1868 time_t then = tvnow.tv_sec - du/1000000;
1870 if (du % 1000000 > tvnow.tv_usec)
1873 fprintf(f, "%s", ctime(&then));
1877 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1881 if (++nlines % 10000 == 0 && f != stdout) {
1882 /* show some activity... */
1888 tlast = next_event[cpu]->lwte_when;
1891 if (next_event[cpu] == cpu_event[cpu + 1])
1892 next_event[cpu] = cpu_event[cpu];
1894 if (next_event[cpu]->lwte_where == NULL ||
1895 next_event[cpu] == first_event[cpu])
1896 next_event[cpu] = NULL;
1908 int jt_ptl_memhog(int argc, char **argv)
1910 static int gfp = 0; /* sticky! */
1912 struct portal_ioctl_data data;
1918 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1922 count = strtol(argv[1], &end, 0);
1923 if (count < 0 || *end != 0) {
1924 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1929 rc = strtol(argv[2], &end, 0);
1931 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1937 PORTAL_IOC_INIT(data);
1938 data.ioc_count = count;
1939 data.ioc_flags = gfp;
1940 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1943 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1947 printf("memhog %d OK\n", count);