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 for %s: %s\n",
242 hname, strerror(h_errno));
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_")) {
325 if (ptl_parse_ipquad(ipaddrp, str) == 0)
328 #ifdef HAVE_GETHOSTBYNAME
329 if ((('a' <= str[0] && str[0] <= 'z') ||
330 ('A' <= str[0] && str[0] <= 'Z')) &&
331 (he = ptl_gethostbyname (str)) != NULL) {
332 __u32 addr = *(__u32 *)he->h_addr;
334 *ipaddrp = ntohl(addr); /* HOST byte order */
343 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
345 #ifdef HAVE_GETHOSTBYNAME
350 net_ip = htonl (ipaddr);
351 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
353 strcpy(str, he->h_name);
359 sprintf (str, "%d.%d.%d.%d",
360 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
361 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
366 ptl_parse_nid (ptl_nid_t *nidp, char *str)
370 unsigned long long ullval;
372 if (!strcmp (str, "_all_")) {
377 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
379 *nidp = (ptl_nid_t)ipaddr;
381 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
386 ullval = strtoull(str, &end, 0);
388 /* parsed whole string */
389 *nidp = (ptl_nid_t)ullval;
396 __u64 ptl_nid2u64(ptl_nid_t nid)
398 switch (sizeof (nid)) {
404 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
412 ptl_nid2str (char *buffer, ptl_nid_t nid)
414 __u64 nid64 = ptl_nid2u64(nid);
415 #ifdef HAVE_GETHOSTBYNAME
416 struct hostent *he = 0;
418 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
419 * TCP addresses in the 0.x.x.x subnet are not in use. This can
420 * happen on routers and slows things down a _lot_. Bug 3442. */
421 if (nid & 0xff000000) {
422 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
424 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
428 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
430 #endif /* HAVE_GETHOSTBYNAME */
431 sprintf(buffer, LPX64, nid64);
439 fprintf (stderr, "Error: you must run the 'network' command first.\n");
446 int g_nal_is_compatible (char *cmd, ...)
451 if (!g_nal_is_set ())
457 nal = va_arg (ap, int);
458 } while (nal != 0 && nal != g_nal);
466 /* Don't complain verbosely if we've not been passed a command
467 * name to complain about! */
468 fprintf (stderr, "Command %s not compatible with nal %s\n",
469 cmd, nal2name (g_nal));
475 sock_write (int cfd, void *buffer, int nob)
479 int rc = write (cfd, buffer, nob);
491 fprintf (stderr, "Unexpected zero sock_write\n");
496 buffer = (char *)buffer + nob;
503 sock_read (int cfd, void *buffer, int nob)
507 int rc = read (cfd, buffer, nob);
517 if (rc == 0) /* EOF */
519 errno = ECONNABORTED;
524 buffer = (char *)buffer + nob;
530 int ptl_initialize(int argc, char **argv)
532 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
537 int jt_ptl_network(int argc, char **argv)
543 (nal = ptl_name2nal (argv[1])) >= 0) {
548 fprintf(stderr, "usage: %s \n", argv[0]);
549 for (entry = nalnames; entry->name != NULL; entry++)
550 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
551 fprintf(stderr, ">\n");
556 jt_ptl_print_interfaces (int argc, char **argv)
558 struct portals_cfg pcfg;
563 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
566 for (index = 0;;index++) {
567 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
568 pcfg.pcfg_count = index;
570 rc = pcfg_ioctl (&pcfg);
574 printf ("%s: (%s/%s) npeer %d nroute %d\n",
575 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
576 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
577 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
578 pcfg.pcfg_fd, pcfg.pcfg_count);
582 printf ("<no interfaces>\n");
587 jt_ptl_add_interface (int argc, char **argv)
589 struct portals_cfg pcfg;
592 __u32 netmask = 0xffffff00;
597 if (argc < 2 || argc > 3) {
598 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
602 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
605 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
606 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
611 count = strtol(argv[2], &end, 0);
612 if (count > 0 && count < 32 && *end == 0) {
614 for (i = count; i > 0; i--)
615 netmask = netmask|(1<<(32-i));
616 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
617 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
622 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
623 pcfg.pcfg_id = ipaddr;
624 pcfg.pcfg_misc = netmask;
626 rc = pcfg_ioctl (&pcfg);
628 fprintf (stderr, "failed to add interface: %s\n",
637 jt_ptl_del_interface (int argc, char **argv)
639 struct portals_cfg pcfg;
644 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
648 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
652 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
653 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
657 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
658 pcfg.pcfg_id = ipaddr;
660 rc = pcfg_ioctl (&pcfg);
662 fprintf (stderr, "failed to delete interface: %s\n",
671 jt_ptl_print_peers (int argc, char **argv)
673 struct portals_cfg pcfg;
678 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
679 OPENIBNAL, IIBNAL, VIBNAL, 0))
682 for (index = 0;;index++) {
683 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
684 pcfg.pcfg_count = index;
686 rc = pcfg_ioctl (&pcfg);
690 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
691 printf (LPX64"[%d]%s@%s:%d #%d\n",
692 pcfg.pcfg_nid, pcfg.pcfg_wait,
693 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
694 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
695 pcfg.pcfg_misc, pcfg.pcfg_count);
696 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, 0))
697 printf (LPX64"[%d]@%s:%d\n",
698 pcfg.pcfg_nid, pcfg.pcfg_wait,
699 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
702 printf (LPX64"[%d]\n",
703 pcfg.pcfg_nid, pcfg.pcfg_wait);
707 printf ("<no peers>\n");
712 jt_ptl_add_peer (int argc, char **argv)
714 struct portals_cfg pcfg;
720 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
721 OPENIBNAL, IIBNAL, VIBNAL, 0))
724 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
726 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
730 } else if (argc != 2) {
731 fprintf (stderr, "usage(iib,vib): %s nid\n", argv[0]);
735 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
736 nid == PTL_NID_ANY) {
737 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
741 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
742 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
743 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
747 if (ptl_parse_port (&port, argv[3]) != 0) {
748 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
753 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
756 pcfg.pcfg_misc = port;
758 rc = pcfg_ioctl (&pcfg);
760 fprintf (stderr, "failed to add peer: %s\n",
769 jt_ptl_del_peer (int argc, char **argv)
771 struct portals_cfg pcfg;
772 ptl_nid_t nid = PTL_NID_ANY;
774 int single_share = 0;
778 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
779 OPENIBNAL, IIBNAL, VIBNAL, 0))
782 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
784 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
788 } else if (argc > 3) {
789 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
794 ptl_parse_nid (&nid, argv[1]) != 0) {
795 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
800 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
802 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
803 fprintf (stderr, "Can't parse ip addr: %s\n",
811 if (!strcmp (argv[argidx], "single_share")) {
814 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
819 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
822 pcfg.pcfg_flags = single_share;
824 rc = pcfg_ioctl (&pcfg);
826 fprintf (stderr, "failed to remove peer: %s\n",
835 jt_ptl_print_connections (int argc, char **argv)
837 struct portals_cfg pcfg;
842 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
843 OPENIBNAL, IIBNAL, VIBNAL, 0))
846 for (index = 0;;index++) {
847 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
848 pcfg.pcfg_count = index;
850 rc = pcfg_ioctl (&pcfg);
854 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
855 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
856 pcfg.pcfg_gw_nal, /* scheduler */
857 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
859 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
860 pcfg.pcfg_misc, /* remote port */
861 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
862 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
863 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
864 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
865 pcfg.pcfg_count, /* tx buffer size */
866 pcfg.pcfg_size, /* rx buffer size */
867 pcfg.pcfg_wait ? "nagle" : "nonagle");
868 else if (g_nal_is_compatible (NULL, RANAL, 0))
869 printf ("[%d]"LPX64"\n",
870 pcfg.pcfg_id, /* device id */
878 printf ("<no connections>\n");
882 int jt_ptl_connect(int argc, char **argv)
885 /* no connect() support */
887 #else /* HAVE_CONNECT */
888 struct portals_cfg pcfg;
889 struct sockaddr_in srvaddr;
890 struct sockaddr_in locaddr;
894 int type = SOCKNAL_CONN_ANY;
899 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
903 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
906 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
908 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
912 if (ptl_parse_port (&port, argv[2]) != 0) {
913 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
918 for (flag = argv[3]; *flag != 0; flag++)
922 if (type != SOCKNAL_CONN_ANY) {
923 fprintf(stderr, "Can't flag type twice\n");
926 type = SOCKNAL_CONN_BULK_IN;
930 if (type != SOCKNAL_CONN_ANY) {
931 fprintf(stderr, "Can't flag type twice\n");
934 type = SOCKNAL_CONN_BULK_OUT;
938 if (type != SOCKNAL_CONN_ANY) {
939 fprintf(stderr, "Can't flag type twice\n");
942 type = SOCKNAL_CONN_CONTROL;
946 fprintf (stderr, "unrecognised flag '%c'\n",
951 memset(&locaddr, 0, sizeof(locaddr));
952 locaddr.sin_family = AF_INET;
953 locaddr.sin_addr.s_addr = INADDR_ANY;
955 memset(&srvaddr, 0, sizeof(srvaddr));
956 srvaddr.sin_family = AF_INET;
957 srvaddr.sin_port = htons(port);
958 srvaddr.sin_addr.s_addr = htonl(ipaddr);
961 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
962 fd = socket(PF_INET, SOCK_STREAM, 0);
964 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
969 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
972 locaddr.sin_port = htons(rport);
973 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
974 if (rc == 0 || errno == EACCES) {
975 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
978 } else if (errno != EADDRINUSE) {
979 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
983 } else if (errno != EADDRINUSE) {
984 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
990 if (rport == IPPORT_RESERVED / 2) {
992 "Warning: all privileged ports are in use.\n");
996 printf("Connected host: %s type: %s\n",
998 (type == SOCKNAL_CONN_ANY) ? "A" :
999 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1000 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1001 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1003 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1004 pcfg.pcfg_nal = g_nal;
1006 pcfg.pcfg_misc = type;
1008 rc = pcfg_ioctl(&pcfg);
1010 fprintf(stderr, "failed to register fd with portals: %s\n",
1016 printf("Connection to %s registered with socknal\n", argv[1]);
1020 fprintf(stderr, "close failed: %d\n", rc);
1023 #endif /* HAVE_CONNECT */
1026 int jt_ptl_disconnect(int argc, char **argv)
1028 struct portals_cfg pcfg;
1029 ptl_nid_t nid = PTL_NID_ANY;
1034 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1038 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1039 OPENIBNAL, IIBNAL, VIBNAL, 0))
1043 ptl_parse_nid (&nid, argv[1]) != 0) {
1044 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1048 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1050 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1051 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1055 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1056 pcfg.pcfg_nid = nid;
1057 pcfg.pcfg_id = ipaddr;
1059 rc = pcfg_ioctl(&pcfg);
1061 fprintf(stderr, "failed to remove connection: %s\n",
1069 int jt_ptl_push_connection (int argc, char **argv)
1071 struct portals_cfg pcfg;
1073 ptl_nid_t nid = PTL_NID_ANY;
1077 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1081 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1085 ptl_parse_nid (&nid, argv[1]) != 0) {
1086 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1091 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1092 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1095 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1096 pcfg.pcfg_nid = nid;
1097 pcfg.pcfg_id = ipaddr;
1099 rc = pcfg_ioctl(&pcfg);
1101 fprintf(stderr, "failed to push connection: %s\n",
1110 jt_ptl_print_active_txs (int argc, char **argv)
1112 struct portals_cfg pcfg;
1116 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1119 for (index = 0;;index++) {
1120 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1121 pcfg.pcfg_count = index;
1123 rc = pcfg_ioctl(&pcfg);
1127 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1129 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1130 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1131 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1132 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1137 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1138 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1139 pcfg.pcfg_flags >> 2);
1143 printf ("<no active descs>\n");
1147 int jt_ptl_ping(int argc, char **argv)
1154 struct portal_ioctl_data data;
1157 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1161 if (!g_nal_is_set())
1164 if (ptl_parse_nid (&nid, argv[1]) != 0)
1166 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1172 count = atol(argv[2]);
1174 if (count < 0 || count > 20000)
1176 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1182 size= atol(argv[3]);
1185 timeout = atol (argv[4]);
1187 PORTAL_IOC_INIT (data);
1188 data.ioc_count = count;
1189 data.ioc_size = size;
1191 data.ioc_nal = g_nal;
1192 data.ioc_timeout = timeout;
1194 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1196 fprintf(stderr, "failed to start pinger: %s\n",
1203 int jt_ptl_shownid(int argc, char **argv)
1205 struct portal_ioctl_data data;
1209 fprintf(stderr, "usage: %s\n", argv[0]);
1213 if (!g_nal_is_set())
1216 PORTAL_IOC_INIT (data);
1217 data.ioc_nal = g_nal;
1218 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1220 fprintf(stderr, "getting my NID failed: %s\n",
1223 printf(LPX64"\n", data.ioc_nid);
1227 int jt_ptl_mynid(int argc, char **argv)
1230 char hostname[1024];
1232 struct portals_cfg pcfg;
1236 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1237 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1241 if (!g_nal_is_set())
1246 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1247 fprintf(stderr, "gethostname failed: %s\n",
1254 rc = ptl_parse_nid (&mynid, nidstr);
1256 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1260 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1261 pcfg.pcfg_nid = mynid;
1263 rc = pcfg_ioctl(&pcfg);
1265 fprintf(stderr, "setting my NID failed: %s\n",
1268 printf("registered my nid "LPX64" (%s)\n",
1269 ptl_nid2u64(mynid), hostname);
1274 jt_ptl_fail_nid (int argc, char **argv)
1278 unsigned int threshold;
1279 struct portal_ioctl_data data;
1281 if (argc < 2 || argc > 3)
1283 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1287 if (!g_nal_is_set())
1290 if (!strcmp (argv[1], "_all_"))
1292 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1294 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1299 threshold = PTL_MD_THRESH_INF;
1300 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1301 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1305 PORTAL_IOC_INIT (data);
1306 data.ioc_nal = g_nal;
1308 data.ioc_count = threshold;
1310 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1312 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1315 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1321 jt_ptl_add_route (int argc, char **argv)
1323 struct portals_cfg pcfg;
1326 ptl_nid_t gateway_nid;
1331 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1335 if (!g_nal_is_set())
1338 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1340 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1344 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1346 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1352 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1354 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1358 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1359 pcfg.pcfg_nid = gateway_nid;
1360 pcfg.pcfg_nal = ROUTER;
1361 pcfg.pcfg_gw_nal = g_nal;
1362 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1363 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1365 rc = pcfg_ioctl(&pcfg);
1368 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1376 jt_ptl_del_route (int argc, char **argv)
1378 struct portals_cfg pcfg;
1380 ptl_nid_t nid1 = PTL_NID_ANY;
1381 ptl_nid_t nid2 = PTL_NID_ANY;
1386 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1390 if (!g_nal_is_set())
1393 if (ptl_parse_nid (&nid, argv[1]) != 0)
1395 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1400 ptl_parse_nid (&nid1, argv[2]) != 0)
1402 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1409 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1410 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1415 ptl_nid_t tmp = nid1;
1422 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1423 pcfg.pcfg_nal = ROUTER;
1424 pcfg.pcfg_gw_nal = g_nal;
1425 pcfg.pcfg_nid = nid;
1426 pcfg.pcfg_nid2 = nid1;
1427 pcfg.pcfg_nid3 = nid2;
1429 rc = pcfg_ioctl(&pcfg);
1432 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1433 ptl_nid2u64(nid), strerror (errno));
1441 jt_ptl_notify_router (int argc, char **argv)
1443 struct portals_cfg pcfg;
1452 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1457 if (ptl_parse_nid (&nid, argv[1]) != 0)
1459 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1463 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1464 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1468 gettimeofday(&now, NULL);
1472 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1473 fprintf(stderr, "Can't parse time %s\n"
1474 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1475 "or an absolute unix time in seconds\n", argv[3]);
1477 } else if (when > now.tv_sec) {
1478 fprintf (stderr, "%s specifies a time in the future\n",
1483 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1484 pcfg.pcfg_nal = ROUTER;
1485 pcfg.pcfg_gw_nal = g_nal;
1486 pcfg.pcfg_nid = nid;
1487 pcfg.pcfg_flags = enable;
1488 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1489 pcfg.pcfg_nid3 = (__u64)when;
1491 rc = pcfg_ioctl(&pcfg);
1494 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1495 ptl_nid2u64(nid), strerror (errno));
1503 jt_ptl_print_routes (int argc, char **argv)
1505 char buffer[3][128];
1506 struct portals_cfg pcfg;
1510 ptl_nid_t gateway_nid;
1515 for (index = 0;;index++)
1517 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1518 pcfg.pcfg_nal = ROUTER;
1519 pcfg.pcfg_count = index;
1521 rc = pcfg_ioctl(&pcfg);
1525 gateway_nal = pcfg.pcfg_gw_nal;
1526 gateway_nid = pcfg.pcfg_nid;
1527 nid1 = pcfg.pcfg_nid2;
1528 nid2 = pcfg.pcfg_nid3;
1529 alive = pcfg.pcfg_flags;
1531 printf ("%8s %18s : %s - %s, %s\n",
1532 nal2name (gateway_nal),
1533 ptl_nid2str (buffer[0], gateway_nid),
1534 ptl_nid2str (buffer[1], nid1),
1535 ptl_nid2str (buffer[2], nid2),
1536 alive ? "up" : "down");
1542 lwt_control(int enable, int clear)
1544 struct portal_ioctl_data data;
1547 PORTAL_IOC_INIT(data);
1548 data.ioc_flags = enable;
1549 data.ioc_misc = clear;
1551 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1555 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1561 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1562 lwt_event_t *events, int size)
1564 struct portal_ioctl_data data;
1567 PORTAL_IOC_INIT(data);
1568 data.ioc_pbuf1 = (char *)events;
1569 data.ioc_plen1 = size;
1571 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1573 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1578 /* crappy overloads */
1579 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1580 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1581 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1582 (int)data.ioc_nid2, sizeof(lwt_event_t),
1584 (int)offsetof(lwt_event_t, lwte_where));
1588 LASSERT (data.ioc_count != 0);
1589 LASSERT (data.ioc_misc != 0);
1592 *now = data.ioc_nid;
1595 *ncpu = data.ioc_count;
1597 if (totalsize != NULL)
1598 *totalsize = data.ioc_misc;
1604 lwt_get_string(char *kstr)
1607 struct portal_ioctl_data data;
1611 /* FIXME: this could maintain a symbol table since we expect to be
1612 * looking up the same strings all the time... */
1614 PORTAL_IOC_INIT(data);
1615 data.ioc_pbuf1 = kstr;
1616 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1617 data.ioc_pbuf2 = NULL;
1620 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1622 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1627 size = data.ioc_count;
1628 ustr = (char *)malloc(size);
1630 fprintf(stderr, "Can't allocate string storage of size %d\n",
1635 PORTAL_IOC_INIT(data);
1636 data.ioc_pbuf1 = kstr;
1637 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1638 data.ioc_pbuf2 = ustr;
1639 data.ioc_plen2 = size;
1641 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1643 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1648 LASSERT(strlen(ustr) == size - 1);
1653 lwt_put_string(char *ustr)
1659 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1662 # error "__WORDSIZE not defined"
1663 #elif __WORDSIZE == 32
1664 # define XFMT "%#010lx"
1665 #elif __WORDSIZE== 64
1666 # define XFMT "%#018lx"
1668 # error "Unexpected __WORDSIZE"
1670 char *where = lwt_get_string(e->lwte_where);
1675 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1676 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1677 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1678 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1681 lwt_put_string(where);
1688 get_cycles_per_usec ()
1690 FILE *f = fopen ("/proc/cpuinfo", "r");
1695 while (fgets (line, sizeof (line), f) != NULL)
1696 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1703 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1708 jt_ptl_lwt(int argc, char **argv)
1710 const int lwt_max_cpus = 32;
1713 int nevents_per_cpu;
1714 lwt_event_t *events;
1715 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1716 lwt_event_t *next_event[lwt_max_cpus];
1717 lwt_event_t *first_event[lwt_max_cpus];
1726 struct timeval tvnow;
1727 int printed_date = 0;
1732 (strcmp(argv[1], "start") &&
1733 strcmp(argv[1], "stop"))) {
1736 " %s stop [fname]\n", argv[0], argv[0]);
1740 if (!strcmp(argv[1], "start")) {
1742 if (lwt_control(0, 0) != 0)
1746 if (lwt_control(0, 1) != 0)
1750 if (lwt_control(1, 0) != 0)
1756 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1759 if (ncpus > lwt_max_cpus) {
1760 fprintf(stderr, "Too many cpus: %d (%d)\n",
1761 ncpus, lwt_max_cpus);
1765 events = (lwt_event_t *)malloc(totalspace);
1766 if (events == NULL) {
1767 fprintf(stderr, "Can't allocate %d\n", totalspace);
1771 if (lwt_control(0, 0) != 0) { /* disable */
1776 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1781 /* we want this time to be sampled at snapshot time */
1782 gettimeofday(&tvnow, NULL);
1785 f = fopen (argv[2], "w");
1787 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1793 mhz = get_cycles_per_usec();
1795 /* carve events into per-cpu slices */
1796 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1797 for (cpu = 0; cpu <= ncpus; cpu++)
1798 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1800 /* find the earliest event on each cpu */
1801 for (cpu = 0; cpu < ncpus; cpu++) {
1802 first_event[cpu] = NULL;
1804 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1806 if (e->lwte_where == NULL) /* not an event */
1809 if (first_event[cpu] == NULL ||
1810 first_event[cpu]->lwte_when > e->lwte_when)
1811 first_event[cpu] = e;
1814 next_event[cpu] = first_event[cpu];
1818 for (cpu = 0; cpu < ncpus; cpu++) {
1819 e = first_event[cpu];
1820 if (e == NULL) /* no events this cpu */
1823 if (e == cpu_event[cpu])
1824 e = cpu_event[cpu + 1] - 1;
1828 /* If there's an event immediately before the first one, this
1829 * cpu wrapped its event buffer */
1830 if (e->lwte_where == NULL)
1833 /* We should only start outputting events from the most recent
1834 * first event in any wrapped cpu. Events before this time on
1835 * other cpus won't have any events from this CPU to interleave
1837 if (t0 < first_event[cpu]->lwte_when)
1838 t0 = first_event[cpu]->lwte_when;
1842 /* find which cpu has the next event */
1844 for (i = 0; i < ncpus; i++) {
1846 if (next_event[i] == NULL) /* this cpu exhausted */
1850 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1854 if (cpu < 0) /* all cpus exhausted */
1858 /* no wrapped cpus and this is he first ever event */
1859 t0 = next_event[cpu]->lwte_when;
1862 if (t0 <= next_event[cpu]->lwte_when) {
1863 /* on or after the first event */
1864 if (!printed_date) {
1865 cycles_t du = (tnow - t0) / mhz;
1866 time_t then = tvnow.tv_sec - du/1000000;
1868 if (du % 1000000 > tvnow.tv_usec)
1871 fprintf(f, "%s", ctime(&then));
1875 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1879 if (++nlines % 10000 == 0 && f != stdout) {
1880 /* show some activity... */
1886 tlast = next_event[cpu]->lwte_when;
1889 if (next_event[cpu] == cpu_event[cpu + 1])
1890 next_event[cpu] = cpu_event[cpu];
1892 if (next_event[cpu]->lwte_where == NULL ||
1893 next_event[cpu] == first_event[cpu])
1894 next_event[cpu] = NULL;
1906 int jt_ptl_memhog(int argc, char **argv)
1908 static int gfp = 0; /* sticky! */
1910 struct portal_ioctl_data data;
1916 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1920 count = strtol(argv[1], &end, 0);
1921 if (count < 0 || *end != 0) {
1922 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1927 rc = strtol(argv[2], &end, 0);
1929 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1935 PORTAL_IOC_INIT(data);
1936 data.ioc_count = count;
1937 data.ioc_flags = gfp;
1938 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1941 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1945 printf("memhog %d OK\n", count);