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},
84 {"cray_kern_nal", CRAY_KERN_NAL},
85 {"cray_user_nal", CRAY_USER_NAL},
86 {"cray_qk_nal", CRAY_QK_NAL},
91 static cfg_record_cb_t g_record_cb;
93 /* Convert a string boolean to an int; "enable" -> 1 */
94 int ptl_parse_bool (int *b, char *str) {
95 if (!strcasecmp (str, "no") ||
96 !strcasecmp (str, "n") ||
97 !strcasecmp (str, "off") ||
98 !strcasecmp (str, "down") ||
99 !strcasecmp (str, "disable"))
105 if (!strcasecmp (str, "yes") ||
106 !strcasecmp (str, "y") ||
107 !strcasecmp (str, "on") ||
108 !strcasecmp (str, "up") ||
109 !strcasecmp (str, "enable"))
118 /* Convert human readable size string to and int; "1k" -> 1000 */
119 int ptl_parse_size (int *sizep, char *str) {
123 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
156 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
163 pcfg_ioctl(struct portals_cfg *pcfg)
167 if (pcfg->pcfg_nal ==0)
168 pcfg->pcfg_nal = g_nal;
171 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
173 struct portal_ioctl_data data;
174 PORTAL_IOC_INIT (data);
175 data.ioc_pbuf1 = (char*)pcfg;
176 data.ioc_plen1 = sizeof(*pcfg);
177 /* XXX liblustre hack XXX */
178 data.ioc_nal_cmd = pcfg->pcfg_command;
179 data.ioc_nid = pcfg->pcfg_nid;
181 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
190 name2num_lookup_name (name2num_t *table, char *str)
192 while (table->name != NULL)
193 if (!strcmp (str, table->name))
201 name2num_lookup_num (name2num_t *table, int num)
203 while (table->name != NULL)
204 if (num == table->num)
212 ptl_name2nal (char *str)
214 name2num_t *e = name2num_lookup_name (nalnames, str);
216 return ((e == NULL) ? -1 : e->num);
222 name2num_t *e = name2num_lookup_num (nalnames, nal);
224 return ((e == NULL) ? "???" : e->name);
227 #ifdef HAVE_GETHOSTBYNAME
228 static struct hostent *
229 ptl_gethostbyname(char * hname) {
231 he = gethostbyname(hname);
236 fprintf(stderr, "Unable to resolve hostname: %s\n",
240 fprintf(stderr, "gethostbyname error: %s\n",
251 ptl_parse_port (int *port, char *str)
255 *port = strtol (str, &end, 0);
257 if (*end == 0 && /* parsed whole string */
258 *port > 0 && *port < 65536) /* minimal sanity check */
265 ptl_parse_time (time_t *t, char *str)
271 *t = strtol (str, &end, 0);
272 if (*end == 0) /* parsed whole string */
275 memset (&tm, 0, sizeof (tm));
276 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
277 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
278 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
282 tm.tm_mon--; /* convert to 0 == Jan */
283 tm.tm_year -= 1900; /* y2k quirk */
284 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
287 if (*t == (time_t)-1)
294 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
301 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
302 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
303 (c & ~0xff) == 0 && (d & ~0xff) == 0)
305 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
313 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
315 #ifdef HAVE_GETHOSTBYNAME
319 if (!strcmp (str, "_all_"))
325 if (ptl_parse_ipquad(ipaddrp, str) == 0)
328 #if HAVE_GETHOSTBYNAME
329 if ((('a' <= str[0] && str[0] <= 'z') ||
330 ('A' <= str[0] && str[0] <= 'Z')) &&
331 (he = ptl_gethostbyname (str)) != NULL)
333 __u32 addr = *(__u32 *)he->h_addr;
335 *ipaddrp = ntohl(addr); /* HOST byte order */
344 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
346 #ifdef HAVE_GETHOSTBYNAME
351 net_ip = htonl (ipaddr);
352 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
354 strcpy(str, he->h_name);
360 sprintf (str, "%d.%d.%d.%d",
361 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
362 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
367 ptl_parse_nid (ptl_nid_t *nidp, char *str)
371 unsigned long long ullval;
373 if (!strcmp (str, "_all_")) {
378 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
380 *nidp = (ptl_nid_t)ipaddr;
382 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
387 ullval = strtoull(str, &end, 0);
389 /* parsed whole string */
390 *nidp = (ptl_nid_t)ullval;
397 __u64 ptl_nid2u64(ptl_nid_t nid)
399 switch (sizeof (nid)) {
405 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
413 ptl_nid2str (char *buffer, ptl_nid_t nid)
415 __u64 nid64 = ptl_nid2u64(nid);
416 #ifdef HAVE_GETHOSTBYNAME
417 struct hostent *he = 0;
419 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
420 * TCP addresses in the 0.x.x.x subnet are not in use. This can
421 * happen on routers and slows things down a _lot_. Bug 3442. */
422 if (nid & 0xff000000) {
423 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
425 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
429 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
431 #endif /* HAVE_GETHOSTBYNAME */
432 sprintf(buffer, LPX64, nid64);
440 fprintf (stderr, "Error: you must run the 'network' command first.\n");
447 int g_nal_is_compatible (char *cmd, ...)
452 if (!g_nal_is_set ())
458 nal = va_arg (ap, int);
459 } while (nal != 0 && nal != g_nal);
467 /* Don't complain verbosely if we've not been passed a command
468 * name to complain about! */
469 fprintf (stderr, "Command %s not compatible with nal %s\n",
470 cmd, nal2name (g_nal));
476 sock_write (int cfd, void *buffer, int nob)
480 int rc = write (cfd, buffer, nob);
492 fprintf (stderr, "Unexpected zero sock_write\n");
497 buffer = (char *)buffer + nob;
504 sock_read (int cfd, void *buffer, int nob)
508 int rc = read (cfd, buffer, nob);
518 if (rc == 0) /* EOF */
520 errno = ECONNABORTED;
525 buffer = (char *)buffer + nob;
531 int ptl_initialize(int argc, char **argv)
533 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
538 int jt_ptl_network(int argc, char **argv)
544 (nal = ptl_name2nal (argv[1])) >= 0) {
549 fprintf(stderr, "usage: %s \n", argv[0]);
550 for (entry = nalnames; entry->name != NULL; entry++)
551 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
552 fprintf(stderr, ">\n");
557 jt_ptl_print_interfaces (int argc, char **argv)
559 struct portals_cfg pcfg;
564 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
567 for (index = 0;;index++) {
568 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
569 pcfg.pcfg_count = index;
571 rc = pcfg_ioctl (&pcfg);
575 printf ("%s: (%s/%s) npeer %d nroute %d\n",
576 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
577 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
578 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
579 pcfg.pcfg_fd, pcfg.pcfg_count);
583 printf ("<no interfaces>\n");
588 jt_ptl_add_interface (int argc, char **argv)
590 struct portals_cfg pcfg;
593 __u32 netmask = 0xffffff00;
598 if (argc < 2 || argc > 3) {
599 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
603 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
606 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
607 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
612 count = strtol(argv[2], &end, 0);
613 if (count > 0 && count < 32 && *end == 0) {
615 for (i = count; i > 0; i--)
616 netmask = netmask|(1<<(32-i));
617 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
618 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
623 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
624 pcfg.pcfg_id = ipaddr;
625 pcfg.pcfg_misc = netmask;
627 rc = pcfg_ioctl (&pcfg);
629 fprintf (stderr, "failed to add interface: %s\n",
638 jt_ptl_del_interface (int argc, char **argv)
640 struct portals_cfg pcfg;
645 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
649 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
653 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
654 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
658 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
659 pcfg.pcfg_id = ipaddr;
661 rc = pcfg_ioctl (&pcfg);
663 fprintf (stderr, "failed to delete interface: %s\n",
672 jt_ptl_print_peers (int argc, char **argv)
674 struct portals_cfg pcfg;
679 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 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);
697 printf (LPX64"[%d]\n",
698 pcfg.pcfg_nid, pcfg.pcfg_wait);
702 printf ("<no peers>\n");
707 jt_ptl_add_peer (int argc, char **argv)
709 struct portals_cfg pcfg;
715 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, RANAL, 0))
718 if (g_nal_is_compatible(NULL, SOCKNAL, RANAL, 0)) {
720 fprintf (stderr, "usage(tcp): %s nid ipaddr port\n",
724 } else if (argc != 2) {
725 fprintf (stderr, "usage(openib,iib): %s nid\n", argv[0]);
729 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
730 nid == PTL_NID_ANY) {
731 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
735 if (g_nal_is_compatible (NULL, SOCKNAL, RANAL, 0)) {
736 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
737 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
741 if (ptl_parse_port (&port, argv[3]) != 0) {
742 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
747 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
750 pcfg.pcfg_misc = port;
752 rc = pcfg_ioctl (&pcfg);
754 fprintf (stderr, "failed to add peer: %s\n",
763 jt_ptl_del_peer (int argc, char **argv)
765 struct portals_cfg pcfg;
766 ptl_nid_t nid = PTL_NID_ANY;
768 int single_share = 0;
772 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, RANAL, 0))
775 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
777 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
781 } else if (argc > 3) {
782 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
787 ptl_parse_nid (&nid, argv[1]) != 0) {
788 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
793 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
795 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
796 fprintf (stderr, "Can't parse ip addr: %s\n",
804 if (!strcmp (argv[argidx], "single_share")) {
807 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
812 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
815 pcfg.pcfg_flags = single_share;
817 rc = pcfg_ioctl (&pcfg);
819 fprintf (stderr, "failed to remove peer: %s\n",
828 jt_ptl_print_connections (int argc, char **argv)
830 struct portals_cfg pcfg;
835 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
838 for (index = 0;;index++) {
839 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
840 pcfg.pcfg_count = index;
842 rc = pcfg_ioctl (&pcfg);
846 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
847 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
848 pcfg.pcfg_gw_nal, /* scheduler */
849 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
851 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
852 pcfg.pcfg_misc, /* remote port */
853 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
854 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
855 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
856 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
857 pcfg.pcfg_count, /* tx buffer size */
858 pcfg.pcfg_size, /* rx buffer size */
859 pcfg.pcfg_wait ? "nagle" : "nonagle");
866 printf ("<no connections>\n");
870 int jt_ptl_connect(int argc, char **argv)
873 /* no connect() support */
875 #else /* HAVE_CONNECT */
876 struct portals_cfg pcfg;
877 struct sockaddr_in srvaddr;
878 struct sockaddr_in locaddr;
882 int type = SOCKNAL_CONN_ANY;
887 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
891 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
894 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
896 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
900 if (ptl_parse_port (&port, argv[2]) != 0) {
901 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
906 for (flag = argv[3]; *flag != 0; flag++)
910 if (type != SOCKNAL_CONN_ANY) {
911 fprintf(stderr, "Can't flag type twice\n");
914 type = SOCKNAL_CONN_BULK_IN;
918 if (type != SOCKNAL_CONN_ANY) {
919 fprintf(stderr, "Can't flag type twice\n");
922 type = SOCKNAL_CONN_BULK_OUT;
926 if (type != SOCKNAL_CONN_ANY) {
927 fprintf(stderr, "Can't flag type twice\n");
930 type = SOCKNAL_CONN_CONTROL;
934 fprintf (stderr, "unrecognised flag '%c'\n",
939 memset(&locaddr, 0, sizeof(locaddr));
940 locaddr.sin_family = AF_INET;
941 locaddr.sin_addr.s_addr = INADDR_ANY;
943 memset(&srvaddr, 0, sizeof(srvaddr));
944 srvaddr.sin_family = AF_INET;
945 srvaddr.sin_port = htons(port);
946 srvaddr.sin_addr.s_addr = htonl(ipaddr);
949 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
950 fd = socket(PF_INET, SOCK_STREAM, 0);
952 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
957 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
960 locaddr.sin_port = htons(rport);
961 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
962 if (rc == 0 || errno == EACCES) {
963 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
966 } else if (errno != EADDRINUSE) {
967 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
971 } else if (errno != EADDRINUSE) {
972 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
978 if (rport == IPPORT_RESERVED / 2) {
980 "Warning: all privileged ports are in use.\n");
984 printf("Connected host: %s type: %s\n",
986 (type == SOCKNAL_CONN_ANY) ? "A" :
987 (type == SOCKNAL_CONN_CONTROL) ? "C" :
988 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
989 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
991 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
992 pcfg.pcfg_nal = g_nal;
994 pcfg.pcfg_misc = type;
996 rc = pcfg_ioctl(&pcfg);
998 fprintf(stderr, "failed to register fd with portals: %s\n",
1004 printf("Connection to %s registered with socknal\n", argv[1]);
1008 fprintf(stderr, "close failed: %d\n", rc);
1011 #endif /* HAVE_CONNECT */
1014 int jt_ptl_disconnect(int argc, char **argv)
1016 struct portals_cfg pcfg;
1017 ptl_nid_t nid = PTL_NID_ANY;
1022 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1026 if (!g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, IIBNAL, 0))
1030 ptl_parse_nid (&nid, argv[1]) != 0) {
1031 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1035 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1037 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1038 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1042 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1043 pcfg.pcfg_nid = nid;
1044 pcfg.pcfg_id = ipaddr;
1046 rc = pcfg_ioctl(&pcfg);
1048 fprintf(stderr, "failed to remove connection: %s\n",
1056 int jt_ptl_push_connection (int argc, char **argv)
1058 struct portals_cfg pcfg;
1060 ptl_nid_t nid = PTL_NID_ANY;
1064 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1068 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1072 ptl_parse_nid (&nid, argv[1]) != 0) {
1073 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1078 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1079 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1082 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1083 pcfg.pcfg_nid = nid;
1084 pcfg.pcfg_id = ipaddr;
1086 rc = pcfg_ioctl(&pcfg);
1088 fprintf(stderr, "failed to push connection: %s\n",
1097 jt_ptl_print_active_txs (int argc, char **argv)
1099 struct portals_cfg pcfg;
1103 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1106 for (index = 0;;index++) {
1107 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1108 pcfg.pcfg_count = index;
1110 rc = pcfg_ioctl(&pcfg);
1114 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1116 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1117 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1118 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1119 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1124 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1125 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1126 pcfg.pcfg_flags >> 2);
1130 printf ("<no active descs>\n");
1134 int jt_ptl_ping(int argc, char **argv)
1141 struct portal_ioctl_data data;
1144 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1148 if (!g_nal_is_set())
1151 if (ptl_parse_nid (&nid, argv[1]) != 0)
1153 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1159 count = atol(argv[2]);
1161 if (count < 0 || count > 20000)
1163 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1169 size= atol(argv[3]);
1172 timeout = atol (argv[4]);
1174 PORTAL_IOC_INIT (data);
1175 data.ioc_count = count;
1176 data.ioc_size = size;
1178 data.ioc_nal = g_nal;
1179 data.ioc_timeout = timeout;
1181 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1183 fprintf(stderr, "failed to start pinger: %s\n",
1190 int jt_ptl_shownid(int argc, char **argv)
1192 struct portal_ioctl_data data;
1196 fprintf(stderr, "usage: %s\n", argv[0]);
1200 if (!g_nal_is_set())
1203 PORTAL_IOC_INIT (data);
1204 data.ioc_nal = g_nal;
1205 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1207 fprintf(stderr, "getting my NID failed: %s\n",
1210 printf(LPX64"\n", data.ioc_nid);
1214 int jt_ptl_mynid(int argc, char **argv)
1217 char hostname[1024];
1219 struct portals_cfg pcfg;
1223 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1224 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1228 if (!g_nal_is_set())
1233 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1234 fprintf(stderr, "gethostname failed: %s\n",
1241 rc = ptl_parse_nid (&mynid, nidstr);
1243 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1247 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1248 pcfg.pcfg_nid = mynid;
1250 rc = pcfg_ioctl(&pcfg);
1252 fprintf(stderr, "setting my NID failed: %s\n",
1255 printf("registered my nid "LPX64" (%s)\n",
1256 ptl_nid2u64(mynid), hostname);
1261 jt_ptl_fail_nid (int argc, char **argv)
1265 unsigned int threshold;
1266 struct portal_ioctl_data data;
1268 if (argc < 2 || argc > 3)
1270 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1274 if (!g_nal_is_set())
1277 if (!strcmp (argv[1], "_all_"))
1279 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1281 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1286 threshold = PTL_MD_THRESH_INF;
1287 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1288 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1292 PORTAL_IOC_INIT (data);
1293 data.ioc_nal = g_nal;
1295 data.ioc_count = threshold;
1297 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1299 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1302 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1308 jt_ptl_add_route (int argc, char **argv)
1310 struct portals_cfg pcfg;
1313 ptl_nid_t gateway_nid;
1318 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1322 if (!g_nal_is_set())
1325 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1327 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1331 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1333 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1339 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1341 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1345 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1346 pcfg.pcfg_nid = gateway_nid;
1347 pcfg.pcfg_nal = ROUTER;
1348 pcfg.pcfg_gw_nal = g_nal;
1349 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1350 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1352 rc = pcfg_ioctl(&pcfg);
1355 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1363 jt_ptl_del_route (int argc, char **argv)
1365 struct portals_cfg pcfg;
1367 ptl_nid_t nid1 = PTL_NID_ANY;
1368 ptl_nid_t nid2 = PTL_NID_ANY;
1373 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1377 if (!g_nal_is_set())
1380 if (ptl_parse_nid (&nid, argv[1]) != 0)
1382 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1387 ptl_parse_nid (&nid1, argv[2]) != 0)
1389 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1396 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1397 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1402 ptl_nid_t tmp = nid1;
1409 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1410 pcfg.pcfg_nal = ROUTER;
1411 pcfg.pcfg_gw_nal = g_nal;
1412 pcfg.pcfg_nid = nid;
1413 pcfg.pcfg_nid2 = nid1;
1414 pcfg.pcfg_nid3 = nid2;
1416 rc = pcfg_ioctl(&pcfg);
1419 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1420 ptl_nid2u64(nid), strerror (errno));
1428 jt_ptl_notify_router (int argc, char **argv)
1430 struct portals_cfg pcfg;
1439 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1444 if (ptl_parse_nid (&nid, argv[1]) != 0)
1446 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1450 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1451 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1455 gettimeofday(&now, NULL);
1459 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1460 fprintf(stderr, "Can't parse time %s\n"
1461 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1462 "or an absolute unix time in seconds\n", argv[3]);
1464 } else if (when > now.tv_sec) {
1465 fprintf (stderr, "%s specifies a time in the future\n",
1470 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1471 pcfg.pcfg_nal = ROUTER;
1472 pcfg.pcfg_gw_nal = g_nal;
1473 pcfg.pcfg_nid = nid;
1474 pcfg.pcfg_flags = enable;
1475 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1476 pcfg.pcfg_nid3 = (__u64)when;
1478 rc = pcfg_ioctl(&pcfg);
1481 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1482 ptl_nid2u64(nid), strerror (errno));
1490 jt_ptl_print_routes (int argc, char **argv)
1492 char buffer[3][128];
1493 struct portals_cfg pcfg;
1497 ptl_nid_t gateway_nid;
1502 for (index = 0;;index++)
1504 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1505 pcfg.pcfg_nal = ROUTER;
1506 pcfg.pcfg_count = index;
1508 rc = pcfg_ioctl(&pcfg);
1512 gateway_nal = pcfg.pcfg_gw_nal;
1513 gateway_nid = pcfg.pcfg_nid;
1514 nid1 = pcfg.pcfg_nid2;
1515 nid2 = pcfg.pcfg_nid3;
1516 alive = pcfg.pcfg_flags;
1518 printf ("%8s %18s : %s - %s, %s\n",
1519 nal2name (gateway_nal),
1520 ptl_nid2str (buffer[0], gateway_nid),
1521 ptl_nid2str (buffer[1], nid1),
1522 ptl_nid2str (buffer[2], nid2),
1523 alive ? "up" : "down");
1529 lwt_control(int enable, int clear)
1531 struct portal_ioctl_data data;
1534 PORTAL_IOC_INIT(data);
1535 data.ioc_flags = enable;
1536 data.ioc_misc = clear;
1538 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1542 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1548 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1549 lwt_event_t *events, int size)
1551 struct portal_ioctl_data data;
1554 PORTAL_IOC_INIT(data);
1555 data.ioc_pbuf1 = (char *)events;
1556 data.ioc_plen1 = size;
1558 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1560 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1565 /* crappy overloads */
1566 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1567 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1568 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1569 (int)data.ioc_nid2, sizeof(lwt_event_t),
1571 (int)offsetof(lwt_event_t, lwte_where));
1575 LASSERT (data.ioc_count != 0);
1576 LASSERT (data.ioc_misc != 0);
1579 *now = data.ioc_nid;
1582 *ncpu = data.ioc_count;
1584 if (totalsize != NULL)
1585 *totalsize = data.ioc_misc;
1591 lwt_get_string(char *kstr)
1594 struct portal_ioctl_data data;
1598 /* FIXME: this could maintain a symbol table since we expect to be
1599 * looking up the same strings all the time... */
1601 PORTAL_IOC_INIT(data);
1602 data.ioc_pbuf1 = kstr;
1603 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1604 data.ioc_pbuf2 = NULL;
1607 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1609 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1614 size = data.ioc_count;
1615 ustr = (char *)malloc(size);
1617 fprintf(stderr, "Can't allocate string storage of size %d\n",
1622 PORTAL_IOC_INIT(data);
1623 data.ioc_pbuf1 = kstr;
1624 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1625 data.ioc_pbuf2 = ustr;
1626 data.ioc_plen2 = size;
1628 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1630 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1635 LASSERT(strlen(ustr) == size - 1);
1640 lwt_put_string(char *ustr)
1646 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1649 # error "__WORDSIZE not defined"
1650 #elif __WORDSIZE == 32
1651 # define XFMT "%#010lx"
1652 #elif __WORDSIZE== 64
1653 # define XFMT "%#018lx"
1655 # error "Unexpected __WORDSIZE"
1657 char *where = lwt_get_string(e->lwte_where);
1662 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1663 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1664 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1665 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1668 lwt_put_string(where);
1675 get_cycles_per_usec ()
1677 FILE *f = fopen ("/proc/cpuinfo", "r");
1682 while (fgets (line, sizeof (line), f) != NULL)
1683 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1690 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1695 jt_ptl_lwt(int argc, char **argv)
1697 const int lwt_max_cpus = 32;
1700 int nevents_per_cpu;
1701 lwt_event_t *events;
1702 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1703 lwt_event_t *next_event[lwt_max_cpus];
1704 lwt_event_t *first_event[lwt_max_cpus];
1713 struct timeval tvnow;
1714 int printed_date = 0;
1719 (strcmp(argv[1], "start") &&
1720 strcmp(argv[1], "stop"))) {
1723 " %s stop [fname]\n", argv[0], argv[0]);
1727 if (!strcmp(argv[1], "start")) {
1729 if (lwt_control(0, 0) != 0)
1733 if (lwt_control(0, 1) != 0)
1737 if (lwt_control(1, 0) != 0)
1743 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1746 if (ncpus > lwt_max_cpus) {
1747 fprintf(stderr, "Too many cpus: %d (%d)\n",
1748 ncpus, lwt_max_cpus);
1752 events = (lwt_event_t *)malloc(totalspace);
1753 if (events == NULL) {
1754 fprintf(stderr, "Can't allocate %d\n", totalspace);
1758 if (lwt_control(0, 0) != 0) { /* disable */
1763 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1768 /* we want this time to be sampled at snapshot time */
1769 gettimeofday(&tvnow, NULL);
1772 f = fopen (argv[2], "w");
1774 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1780 mhz = get_cycles_per_usec();
1782 /* carve events into per-cpu slices */
1783 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1784 for (cpu = 0; cpu <= ncpus; cpu++)
1785 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1787 /* find the earliest event on each cpu */
1788 for (cpu = 0; cpu < ncpus; cpu++) {
1789 first_event[cpu] = NULL;
1791 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1793 if (e->lwte_where == NULL) /* not an event */
1796 if (first_event[cpu] == NULL ||
1797 first_event[cpu]->lwte_when > e->lwte_when)
1798 first_event[cpu] = e;
1801 next_event[cpu] = first_event[cpu];
1805 for (cpu = 0; cpu < ncpus; cpu++) {
1806 e = first_event[cpu];
1807 if (e == NULL) /* no events this cpu */
1810 if (e == cpu_event[cpu])
1811 e = cpu_event[cpu + 1] - 1;
1815 /* If there's an event immediately before the first one, this
1816 * cpu wrapped its event buffer */
1817 if (e->lwte_where == NULL)
1820 /* We should only start outputting events from the most recent
1821 * first event in any wrapped cpu. Events before this time on
1822 * other cpus won't have any events from this CPU to interleave
1824 if (t0 < first_event[cpu]->lwte_when)
1825 t0 = first_event[cpu]->lwte_when;
1829 /* find which cpu has the next event */
1831 for (i = 0; i < ncpus; i++) {
1833 if (next_event[i] == NULL) /* this cpu exhausted */
1837 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1841 if (cpu < 0) /* all cpus exhausted */
1845 /* no wrapped cpus and this is he first ever event */
1846 t0 = next_event[cpu]->lwte_when;
1849 if (t0 <= next_event[cpu]->lwte_when) {
1850 /* on or after the first event */
1851 if (!printed_date) {
1852 cycles_t du = (tnow - t0) / mhz;
1853 time_t then = tvnow.tv_sec - du/1000000;
1855 if (du % 1000000 > tvnow.tv_usec)
1858 fprintf(f, "%s", ctime(&then));
1862 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1866 if (++nlines % 10000 == 0 && f != stdout) {
1867 /* show some activity... */
1873 tlast = next_event[cpu]->lwte_when;
1876 if (next_event[cpu] == cpu_event[cpu + 1])
1877 next_event[cpu] = cpu_event[cpu];
1879 if (next_event[cpu]->lwte_where == NULL ||
1880 next_event[cpu] == first_event[cpu])
1881 next_event[cpu] = NULL;
1893 int jt_ptl_memhog(int argc, char **argv)
1895 static int gfp = 0; /* sticky! */
1897 struct portal_ioctl_data data;
1903 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1907 count = strtol(argv[1], &end, 0);
1908 if (count < 0 || *end != 0) {
1909 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1914 rc = strtol(argv[2], &end, 0);
1916 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1922 PORTAL_IOC_INIT(data);
1923 data.ioc_count = count;
1924 data.ioc_flags = gfp;
1925 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1928 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1932 printf("memhog %d OK\n", count);