1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
28 #include <sys/socket.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
36 #include <sys/ioctl.h>
45 #include <netinet/in.h>
47 #endif /* __CYGWIN__ */
49 #include <portals/api-support.h>
50 #include <portals/ptlctl.h>
51 #include <portals/list.h>
52 #include <portals/lib-types.h>
53 #include <portals/socknal.h>
56 unsigned int portal_debug;
57 unsigned int portal_printk;
59 static unsigned int g_nal = 0;
67 static name2num_t nalnames[] = {
72 {"openib", OPENIBNAL},
77 static cfg_record_cb_t g_record_cb;
79 /* Convert a string boolean to an int; "enable" -> 1 */
80 int ptl_parse_bool (int *b, char *str) {
81 if (!strcasecmp (str, "no") ||
82 !strcasecmp (str, "n") ||
83 !strcasecmp (str, "off") ||
84 !strcasecmp (str, "down") ||
85 !strcasecmp (str, "disable"))
91 if (!strcasecmp (str, "yes") ||
92 !strcasecmp (str, "y") ||
93 !strcasecmp (str, "on") ||
94 !strcasecmp (str, "up") ||
95 !strcasecmp (str, "enable"))
104 /* Convert human readable size string to and int; "1k" -> 1000 */
105 int ptl_parse_size (int *sizep, char *str) {
109 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
142 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
149 pcfg_ioctl(struct portals_cfg *pcfg)
153 if (pcfg->pcfg_nal ==0)
154 pcfg->pcfg_nal = g_nal;
157 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
159 struct portal_ioctl_data data;
160 PORTAL_IOC_INIT (data);
161 data.ioc_pbuf1 = (char*)pcfg;
162 data.ioc_plen1 = sizeof(*pcfg);
163 /* XXX liblustre hack XXX */
164 data.ioc_nal_cmd = pcfg->pcfg_command;
165 data.ioc_nid = pcfg->pcfg_nid;
167 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
176 name2num_lookup_name (name2num_t *table, char *str)
178 while (table->name != NULL)
179 if (!strcmp (str, table->name))
187 name2num_lookup_num (name2num_t *table, int num)
189 while (table->name != NULL)
190 if (num == table->num)
198 ptl_name2nal (char *str)
200 name2num_t *e = name2num_lookup_name (nalnames, str);
202 return ((e == NULL) ? -1 : e->num);
208 name2num_t *e = name2num_lookup_num (nalnames, nal);
210 return ((e == NULL) ? "???" : e->name);
213 #ifdef HAVE_GETHOSTBYNAME
214 static struct hostent *
215 ptl_gethostbyname(char * hname) {
217 he = gethostbyname(hname);
222 fprintf(stderr, "Unable to resolve hostname: %s\n",
226 fprintf(stderr, "gethostbyname error: %s\n",
237 ptl_parse_port (int *port, char *str)
241 *port = strtol (str, &end, 0);
243 if (*end == 0 && /* parsed whole string */
244 *port > 0 && *port < 65536) /* minimal sanity check */
251 ptl_parse_time (time_t *t, char *str)
257 *t = strtol (str, &end, 0);
258 if (*end == 0) /* parsed whole string */
261 memset (&tm, 0, sizeof (tm));
262 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
263 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
264 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
268 tm.tm_mon--; /* convert to 0 == Jan */
269 tm.tm_year -= 1900; /* y2k quirk */
270 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
273 if (*t == (time_t)-1)
280 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
287 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
288 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
289 (c & ~0xff) == 0 && (d & ~0xff) == 0)
291 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
299 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
301 #ifdef HAVE_GETHOSTBYNAME
305 if (!strcmp (str, "_all_"))
311 if (ptl_parse_ipquad(ipaddrp, str) == 0)
314 #if HAVE_GETHOSTBYNAME
315 if ((('a' <= str[0] && str[0] <= 'z') ||
316 ('A' <= str[0] && str[0] <= 'Z')) &&
317 (he = ptl_gethostbyname (str)) != NULL)
319 __u32 addr = *(__u32 *)he->h_addr;
321 *ipaddrp = ntohl(addr); /* HOST byte order */
330 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
332 #ifdef HAVE_GETHOSTBYNAME
337 net_ip = htonl (ipaddr);
338 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
340 strcpy(str, he->h_name);
346 sprintf (str, "%d.%d.%d.%d",
347 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
348 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
353 ptl_parse_nid (ptl_nid_t *nidp, char *str)
357 unsigned long long ullval;
359 if (!strcmp (str, "_all_")) {
364 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
365 *nidp = (ptl_nid_t)ipaddr;
369 ullval = strtoull(str, &end, 0);
371 /* parsed whole string */
372 *nidp = (ptl_nid_t)ullval;
379 __u64 ptl_nid2u64(ptl_nid_t nid)
381 switch (sizeof (nid)) {
387 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
395 ptl_nid2str (char *buffer, ptl_nid_t nid)
397 __u64 nid64 = ptl_nid2u64(nid);
398 #ifdef HAVE_GETHOSTBYNAME
399 struct hostent *he = 0;
401 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
402 * TCP addresses in the 0.x.x.x subnet are not in use. This can
403 * happen on routers and slows things down a _lot_. Bug 3442. */
404 if (nid & 0xff000000) {
405 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
407 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
411 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
413 #endif /* HAVE_GETHOSTBYNAME */
414 sprintf(buffer, LPX64, nid64);
422 fprintf (stderr, "Error: you must run the 'network' command first.\n");
429 int g_nal_is_compatible (char *cmd, ...)
434 if (!g_nal_is_set ())
440 nal = va_arg (ap, int);
441 } while (nal != 0 && nal != g_nal);
449 /* Don't complain verbosely if we've not been passed a command
450 * name to complain about! */
451 fprintf (stderr, "Command %s not compatible with nal %s\n",
452 cmd, nal2name (g_nal));
458 sock_write (int cfd, void *buffer, int nob)
462 int rc = write (cfd, buffer, nob);
474 fprintf (stderr, "Unexpected zero sock_write\n");
479 buffer = (char *)buffer + nob;
486 sock_read (int cfd, void *buffer, int nob)
490 int rc = read (cfd, buffer, nob);
500 if (rc == 0) /* EOF */
502 errno = ECONNABORTED;
507 buffer = (char *)buffer + nob;
513 int ptl_initialize(int argc, char **argv)
515 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
520 int jt_ptl_network(int argc, char **argv)
526 (nal = ptl_name2nal (argv[1])) >= 0) {
531 fprintf(stderr, "usage: %s \n", argv[0]);
532 for (entry = nalnames; entry->name != NULL; entry++)
533 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
534 fprintf(stderr, ">\n");
539 jt_ptl_print_interfaces (int argc, char **argv)
541 struct portals_cfg pcfg;
546 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
549 for (index = 0;;index++) {
550 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
551 pcfg.pcfg_count = index;
553 rc = pcfg_ioctl (&pcfg);
557 printf ("%s: (%s/%s) npeer %d nroute %d\n",
558 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
559 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
560 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
561 pcfg.pcfg_fd, pcfg.pcfg_count);
565 printf ("<no interfaces>\n");
570 jt_ptl_add_interface (int argc, char **argv)
572 struct portals_cfg pcfg;
575 __u32 netmask = 0xffffff00;
580 if (argc < 2 || argc > 3) {
581 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
585 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
588 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
589 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
594 count = strtol(argv[2], &end, 0);
595 if (count > 0 && count < 32 && *end == 0) {
597 for (i = count; i > 0; i--)
598 netmask = netmask|(1<<(32-i));
599 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
600 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
605 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
606 pcfg.pcfg_id = ipaddr;
607 pcfg.pcfg_misc = netmask;
609 rc = pcfg_ioctl (&pcfg);
611 fprintf (stderr, "failed to add interface: %s\n",
620 jt_ptl_del_interface (int argc, char **argv)
622 struct portals_cfg pcfg;
627 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
631 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
635 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
636 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
640 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
641 pcfg.pcfg_id = ipaddr;
643 rc = pcfg_ioctl (&pcfg);
645 fprintf (stderr, "failed to delete interface: %s\n",
654 jt_ptl_print_peers (int argc, char **argv)
656 struct portals_cfg pcfg;
661 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
664 for (index = 0;;index++) {
665 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
666 pcfg.pcfg_count = index;
668 rc = pcfg_ioctl (&pcfg);
672 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
673 printf (LPX64"[%d]%s@%s:%d #%d\n",
674 pcfg.pcfg_nid, pcfg.pcfg_wait,
675 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
676 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
677 pcfg.pcfg_misc, pcfg.pcfg_count);
679 printf (LPX64"[%d]\n",
680 pcfg.pcfg_nid, pcfg.pcfg_wait);
684 printf ("<no peers>\n");
689 jt_ptl_add_peer (int argc, char **argv)
691 struct portals_cfg pcfg;
697 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
700 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
702 fprintf (stderr, "usage(tcp): %s nid ipaddr port\n",
706 } else if (argc != 2) {
707 fprintf (stderr, "usage(openib,iib): %s nid\n", argv[0]);
711 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
712 nid == PTL_NID_ANY) {
713 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
717 if (g_nal_is_compatible (NULL, SOCKNAL, 0)) {
718 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
719 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
723 if (ptl_parse_port (&port, argv[3]) != 0) {
724 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
729 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
732 pcfg.pcfg_misc = port;
734 rc = pcfg_ioctl (&pcfg);
736 fprintf (stderr, "failed to add peer: %s\n",
745 jt_ptl_del_peer (int argc, char **argv)
747 struct portals_cfg pcfg;
748 ptl_nid_t nid = PTL_NID_ANY;
750 int single_share = 0;
754 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
757 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
759 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
763 } else if (argc > 3) {
764 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
769 ptl_parse_nid (&nid, argv[1]) != 0) {
770 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
775 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
777 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
778 fprintf (stderr, "Can't parse ip addr: %s\n",
786 if (!strcmp (argv[argidx], "single_share")) {
789 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
794 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
797 pcfg.pcfg_flags = single_share;
799 rc = pcfg_ioctl (&pcfg);
801 fprintf (stderr, "failed to remove peer: %s\n",
810 jt_ptl_print_connections (int argc, char **argv)
812 struct portals_cfg pcfg;
817 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, IIBNAL, 0))
820 for (index = 0;;index++) {
821 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
822 pcfg.pcfg_count = index;
824 rc = pcfg_ioctl (&pcfg);
828 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
829 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
830 pcfg.pcfg_gw_nal, /* scheduler */
831 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
833 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
834 pcfg.pcfg_misc, /* remote port */
835 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
836 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
837 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
838 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
839 pcfg.pcfg_count, /* tx buffer size */
840 pcfg.pcfg_size, /* rx buffer size */
841 pcfg.pcfg_wait ? "nagle" : "nonagle");
848 printf ("<no connections>\n");
852 int jt_ptl_connect(int argc, char **argv)
855 /* no connect() support */
857 #else /* HAVE_CONNECT */
858 struct portals_cfg pcfg;
859 struct sockaddr_in srvaddr;
860 struct sockaddr_in locaddr;
864 int type = SOCKNAL_CONN_ANY;
869 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
873 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
876 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
878 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
882 if (ptl_parse_port (&port, argv[2]) != 0) {
883 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
888 for (flag = argv[3]; *flag != 0; flag++)
892 if (type != SOCKNAL_CONN_ANY) {
893 fprintf(stderr, "Can't flag type twice\n");
896 type = SOCKNAL_CONN_BULK_IN;
900 if (type != SOCKNAL_CONN_ANY) {
901 fprintf(stderr, "Can't flag type twice\n");
904 type = SOCKNAL_CONN_BULK_OUT;
908 if (type != SOCKNAL_CONN_ANY) {
909 fprintf(stderr, "Can't flag type twice\n");
912 type = SOCKNAL_CONN_CONTROL;
916 fprintf (stderr, "unrecognised flag '%c'\n",
921 memset(&locaddr, 0, sizeof(locaddr));
922 locaddr.sin_family = AF_INET;
923 locaddr.sin_addr.s_addr = INADDR_ANY;
925 memset(&srvaddr, 0, sizeof(srvaddr));
926 srvaddr.sin_family = AF_INET;
927 srvaddr.sin_port = htons(port);
928 srvaddr.sin_addr.s_addr = htonl(ipaddr);
931 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
932 fd = socket(PF_INET, SOCK_STREAM, 0);
934 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
939 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
942 locaddr.sin_port = htons(rport);
943 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
944 if (rc == 0 || errno == EACCES) {
945 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
948 } else if (errno != EADDRINUSE) {
949 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
953 } else if (errno != EADDRINUSE) {
954 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
960 if (rport == IPPORT_RESERVED / 2) {
962 "Warning: all privileged ports are in use.\n");
966 printf("Connected host: %s type: %s\n",
968 (type == SOCKNAL_CONN_ANY) ? "A" :
969 (type == SOCKNAL_CONN_CONTROL) ? "C" :
970 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
971 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
973 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
974 pcfg.pcfg_nal = g_nal;
976 pcfg.pcfg_misc = type;
978 rc = pcfg_ioctl(&pcfg);
980 fprintf(stderr, "failed to register fd with portals: %s\n",
986 printf("Connection to %s registered with socknal\n", argv[1]);
990 fprintf(stderr, "close failed: %d\n", rc);
993 #endif /* HAVE_CONNECT */
996 int jt_ptl_disconnect(int argc, char **argv)
998 struct portals_cfg pcfg;
999 ptl_nid_t nid = PTL_NID_ANY;
1004 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1008 if (!g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, IIBNAL, 0))
1012 ptl_parse_nid (&nid, argv[1]) != 0) {
1013 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1017 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1019 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1020 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1024 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1025 pcfg.pcfg_nid = nid;
1026 pcfg.pcfg_id = ipaddr;
1028 rc = pcfg_ioctl(&pcfg);
1030 fprintf(stderr, "failed to remove connection: %s\n",
1038 int jt_ptl_push_connection (int argc, char **argv)
1040 struct portals_cfg pcfg;
1042 ptl_nid_t nid = PTL_NID_ANY;
1046 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1050 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1054 ptl_parse_nid (&nid, argv[1]) != 0) {
1055 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1060 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1061 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1064 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1065 pcfg.pcfg_nid = nid;
1066 pcfg.pcfg_id = ipaddr;
1068 rc = pcfg_ioctl(&pcfg);
1070 fprintf(stderr, "failed to push connection: %s\n",
1079 jt_ptl_print_active_txs (int argc, char **argv)
1081 struct portals_cfg pcfg;
1085 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1088 for (index = 0;;index++) {
1089 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1090 pcfg.pcfg_count = index;
1092 rc = pcfg_ioctl(&pcfg);
1096 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1098 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1099 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1100 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1101 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1106 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1107 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1108 pcfg.pcfg_flags >> 2);
1112 printf ("<no active descs>\n");
1116 int jt_ptl_ping(int argc, char **argv)
1123 struct portal_ioctl_data data;
1126 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1130 if (!g_nal_is_set())
1133 if (ptl_parse_nid (&nid, argv[1]) != 0)
1135 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1141 count = atol(argv[2]);
1143 if (count < 0 || count > 20000)
1145 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1151 size= atol(argv[3]);
1154 timeout = atol (argv[4]);
1156 PORTAL_IOC_INIT (data);
1157 data.ioc_count = count;
1158 data.ioc_size = size;
1160 data.ioc_nal = g_nal;
1161 data.ioc_timeout = timeout;
1163 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1165 fprintf(stderr, "failed to start pinger: %s\n",
1172 int jt_ptl_shownid(int argc, char **argv)
1174 struct portal_ioctl_data data;
1178 fprintf(stderr, "usage: %s\n", argv[0]);
1182 if (!g_nal_is_set())
1185 PORTAL_IOC_INIT (data);
1186 data.ioc_nal = g_nal;
1187 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1189 fprintf(stderr, "getting my NID failed: %s\n",
1192 printf(LPX64"\n", data.ioc_nid);
1196 int jt_ptl_mynid(int argc, char **argv)
1199 char hostname[1024];
1201 struct portals_cfg pcfg;
1205 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1206 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1210 if (!g_nal_is_set())
1215 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1216 fprintf(stderr, "gethostname failed: %s\n",
1223 rc = ptl_parse_nid (&mynid, nidstr);
1225 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1229 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1230 pcfg.pcfg_nid = mynid;
1232 rc = pcfg_ioctl(&pcfg);
1234 fprintf(stderr, "setting my NID failed: %s\n",
1237 printf("registered my nid "LPX64" (%s)\n",
1238 ptl_nid2u64(mynid), hostname);
1243 jt_ptl_fail_nid (int argc, char **argv)
1247 unsigned int threshold;
1248 struct portal_ioctl_data data;
1250 if (argc < 2 || argc > 3)
1252 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1256 if (!g_nal_is_set())
1259 if (!strcmp (argv[1], "_all_"))
1261 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1263 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1268 threshold = PTL_MD_THRESH_INF;
1269 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1270 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1274 PORTAL_IOC_INIT (data);
1275 data.ioc_nal = g_nal;
1277 data.ioc_count = threshold;
1279 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1281 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1284 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1290 jt_ptl_add_route (int argc, char **argv)
1292 struct portals_cfg pcfg;
1295 ptl_nid_t gateway_nid;
1300 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1304 if (!g_nal_is_set())
1307 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1309 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1313 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1315 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1321 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1323 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1327 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1328 pcfg.pcfg_nid = gateway_nid;
1329 pcfg.pcfg_nal = ROUTER;
1330 pcfg.pcfg_gw_nal = g_nal;
1331 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1332 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1334 rc = pcfg_ioctl(&pcfg);
1337 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1345 jt_ptl_del_route (int argc, char **argv)
1347 struct portals_cfg pcfg;
1349 ptl_nid_t nid1 = PTL_NID_ANY;
1350 ptl_nid_t nid2 = PTL_NID_ANY;
1355 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1359 if (!g_nal_is_set())
1362 if (ptl_parse_nid (&nid, argv[1]) != 0)
1364 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1369 ptl_parse_nid (&nid1, argv[2]) != 0)
1371 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1378 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1379 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1384 ptl_nid_t tmp = nid1;
1391 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1392 pcfg.pcfg_nal = ROUTER;
1393 pcfg.pcfg_gw_nal = g_nal;
1394 pcfg.pcfg_nid = nid;
1395 pcfg.pcfg_nid2 = nid1;
1396 pcfg.pcfg_nid3 = nid2;
1398 rc = pcfg_ioctl(&pcfg);
1401 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1402 ptl_nid2u64(nid), strerror (errno));
1410 jt_ptl_notify_router (int argc, char **argv)
1412 struct portals_cfg pcfg;
1421 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1426 if (ptl_parse_nid (&nid, argv[1]) != 0)
1428 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1432 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1433 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1437 gettimeofday(&now, NULL);
1441 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1442 fprintf(stderr, "Can't parse time %s\n"
1443 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1444 "or an absolute unix time in seconds\n", argv[3]);
1446 } else if (when > now.tv_sec) {
1447 fprintf (stderr, "%s specifies a time in the future\n",
1452 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1453 pcfg.pcfg_nal = ROUTER;
1454 pcfg.pcfg_gw_nal = g_nal;
1455 pcfg.pcfg_nid = nid;
1456 pcfg.pcfg_flags = enable;
1457 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1458 pcfg.pcfg_nid3 = (__u64)when;
1460 rc = pcfg_ioctl(&pcfg);
1463 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1464 ptl_nid2u64(nid), strerror (errno));
1472 jt_ptl_print_routes (int argc, char **argv)
1474 char buffer[3][128];
1475 struct portals_cfg pcfg;
1479 ptl_nid_t gateway_nid;
1484 for (index = 0;;index++)
1486 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1487 pcfg.pcfg_nal = ROUTER;
1488 pcfg.pcfg_count = index;
1490 rc = pcfg_ioctl(&pcfg);
1494 gateway_nal = pcfg.pcfg_gw_nal;
1495 gateway_nid = pcfg.pcfg_nid;
1496 nid1 = pcfg.pcfg_nid2;
1497 nid2 = pcfg.pcfg_nid3;
1498 alive = pcfg.pcfg_flags;
1500 printf ("%8s %18s : %s - %s, %s\n",
1501 nal2name (gateway_nal),
1502 ptl_nid2str (buffer[0], gateway_nid),
1503 ptl_nid2str (buffer[1], nid1),
1504 ptl_nid2str (buffer[2], nid2),
1505 alive ? "up" : "down");
1511 lwt_control(int enable, int clear)
1513 struct portal_ioctl_data data;
1516 PORTAL_IOC_INIT(data);
1517 data.ioc_flags = enable;
1518 data.ioc_misc = clear;
1520 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1524 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1530 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1531 lwt_event_t *events, int size)
1533 struct portal_ioctl_data data;
1536 PORTAL_IOC_INIT(data);
1537 data.ioc_pbuf1 = (char *)events;
1538 data.ioc_plen1 = size;
1540 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1542 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1547 /* crappy overloads */
1548 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1549 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1550 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1551 (int)data.ioc_nid2, sizeof(lwt_event_t),
1553 (int)offsetof(lwt_event_t, lwte_where));
1557 LASSERT (data.ioc_count != 0);
1558 LASSERT (data.ioc_misc != 0);
1561 *now = data.ioc_nid;
1564 *ncpu = data.ioc_count;
1566 if (totalsize != NULL)
1567 *totalsize = data.ioc_misc;
1573 lwt_get_string(char *kstr)
1576 struct portal_ioctl_data data;
1580 /* FIXME: this could maintain a symbol table since we expect to be
1581 * looking up the same strings all the time... */
1583 PORTAL_IOC_INIT(data);
1584 data.ioc_pbuf1 = kstr;
1585 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1586 data.ioc_pbuf2 = NULL;
1589 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1591 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1596 size = data.ioc_count;
1597 ustr = (char *)malloc(size);
1599 fprintf(stderr, "Can't allocate string storage of size %d\n",
1604 PORTAL_IOC_INIT(data);
1605 data.ioc_pbuf1 = kstr;
1606 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1607 data.ioc_pbuf2 = ustr;
1608 data.ioc_plen2 = size;
1610 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1612 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1617 LASSERT(strlen(ustr) == size - 1);
1622 lwt_put_string(char *ustr)
1628 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1631 # error "__WORDSIZE not defined"
1632 #elif __WORDSIZE == 32
1633 # define XFMT "%#010lx"
1634 #elif __WORDSIZE== 64
1635 # define XFMT "%#018lx"
1637 # error "Unexpected __WORDSIZE"
1639 char *where = lwt_get_string(e->lwte_where);
1644 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1645 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1646 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1647 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1650 lwt_put_string(where);
1657 get_cycles_per_usec ()
1659 FILE *f = fopen ("/proc/cpuinfo", "r");
1664 while (fgets (line, sizeof (line), f) != NULL)
1665 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1672 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1677 jt_ptl_lwt(int argc, char **argv)
1679 const int lwt_max_cpus = 32;
1682 int nevents_per_cpu;
1683 lwt_event_t *events;
1684 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1685 lwt_event_t *next_event[lwt_max_cpus];
1686 lwt_event_t *first_event[lwt_max_cpus];
1695 struct timeval tvnow;
1696 int printed_date = 0;
1701 (strcmp(argv[1], "start") &&
1702 strcmp(argv[1], "stop"))) {
1705 " %s stop [fname]\n", argv[0], argv[0]);
1709 if (!strcmp(argv[1], "start")) {
1711 if (lwt_control(0, 0) != 0)
1715 if (lwt_control(0, 1) != 0)
1719 if (lwt_control(1, 0) != 0)
1725 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1728 if (ncpus > lwt_max_cpus) {
1729 fprintf(stderr, "Too many cpus: %d (%d)\n",
1730 ncpus, lwt_max_cpus);
1734 events = (lwt_event_t *)malloc(totalspace);
1735 if (events == NULL) {
1736 fprintf(stderr, "Can't allocate %d\n", totalspace);
1740 if (lwt_control(0, 0) != 0) { /* disable */
1745 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1750 /* we want this time to be sampled at snapshot time */
1751 gettimeofday(&tvnow, NULL);
1754 f = fopen (argv[2], "w");
1756 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1762 mhz = get_cycles_per_usec();
1764 /* carve events into per-cpu slices */
1765 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1766 for (cpu = 0; cpu <= ncpus; cpu++)
1767 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1769 /* find the earliest event on each cpu */
1770 for (cpu = 0; cpu < ncpus; cpu++) {
1771 first_event[cpu] = NULL;
1773 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1775 if (e->lwte_where == NULL) /* not an event */
1778 if (first_event[cpu] == NULL ||
1779 first_event[cpu]->lwte_when > e->lwte_when)
1780 first_event[cpu] = e;
1783 next_event[cpu] = first_event[cpu];
1787 for (cpu = 0; cpu < ncpus; cpu++) {
1788 e = first_event[cpu];
1789 if (e == NULL) /* no events this cpu */
1792 if (e == cpu_event[cpu])
1793 e = cpu_event[cpu + 1] - 1;
1797 /* If there's an event immediately before the first one, this
1798 * cpu wrapped its event buffer */
1799 if (e->lwte_where == NULL)
1802 /* We should only start outputting events from the most recent
1803 * first event in any wrapped cpu. Events before this time on
1804 * other cpus won't have any events from this CPU to interleave
1806 if (t0 < first_event[cpu]->lwte_when)
1807 t0 = first_event[cpu]->lwte_when;
1811 /* find which cpu has the next event */
1813 for (i = 0; i < ncpus; i++) {
1815 if (next_event[i] == NULL) /* this cpu exhausted */
1819 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1823 if (cpu < 0) /* all cpus exhausted */
1827 /* no wrapped cpus and this is he first ever event */
1828 t0 = next_event[cpu]->lwte_when;
1831 if (t0 <= next_event[cpu]->lwte_when) {
1832 /* on or after the first event */
1833 if (!printed_date) {
1834 cycles_t du = (tnow - t0) / mhz;
1835 time_t then = tvnow.tv_sec - du/1000000;
1837 if (du % 1000000 > tvnow.tv_usec)
1840 fprintf(f, "%s", ctime(&then));
1844 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1848 if (++nlines % 10000 == 0 && f != stdout) {
1849 /* show some activity... */
1855 tlast = next_event[cpu]->lwte_when;
1858 if (next_event[cpu] == cpu_event[cpu + 1])
1859 next_event[cpu] = cpu_event[cpu];
1861 if (next_event[cpu]->lwte_where == NULL ||
1862 next_event[cpu] == first_event[cpu])
1863 next_event[cpu] = NULL;
1875 int jt_ptl_memhog(int argc, char **argv)
1877 static int gfp = 0; /* sticky! */
1879 struct portal_ioctl_data data;
1885 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1889 count = strtol(argv[1], &end, 0);
1890 if (count < 0 || *end != 0) {
1891 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1896 rc = strtol(argv[2], &end, 0);
1898 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1904 PORTAL_IOC_INIT(data);
1905 data.ioc_count = count;
1906 data.ioc_flags = gfp;
1907 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1910 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1914 printf("memhog %d OK\n", count);