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>
26 #include <sys/socket.h>
27 #include <netinet/tcp.h>
32 #include <sys/ioctl.h>
41 #include <netinet/in.h>
43 #warning assuming little endian
45 #define __cpu_to_le64(x) ((__u64)(x))
46 #define __le64_to_cpu(x) ((__u64)(x))
47 #define __cpu_to_le32(x) ((__u32)(x))
48 #define __le32_to_cpu(x) ((__u32)(x))
49 #define __cpu_to_le16(x) ((__u16)(x))
50 #define __le16_to_cpu(x) ((__u16)(x))
52 #endif /* __CYGWIN__ */
54 #include <portals/api-support.h>
55 #include <portals/ptlctl.h>
56 #include <portals/list.h>
57 #include <portals/lib-types.h>
58 #include <portals/socknal.h>
61 unsigned int portal_debug;
62 unsigned int portal_printk;
63 unsigned int portal_stack;
64 unsigned int portal_cerror = 1;
66 static unsigned int g_nal = 0;
68 static int g_socket_txmem = 0;
69 static int g_socket_rxmem = 0;
70 static int g_socket_nonagle = 1;
78 static name2num_t nalnames[] = {
84 {"scimac", SCIMACNAL},
88 static cfg_record_cb_t g_record_cb;
91 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
98 pcfg_ioctl(struct portals_cfg *pcfg)
102 if (pcfg->pcfg_nal ==0)
103 pcfg->pcfg_nal = g_nal;
106 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
108 struct portal_ioctl_data data;
109 PORTAL_IOC_INIT (data);
110 data.ioc_pbuf1 = (char*)pcfg;
111 data.ioc_plen1 = sizeof(*pcfg);
112 /* XXX liblustre hack XXX */
113 data.ioc_nal_cmd = pcfg->pcfg_command;
114 data.ioc_nid = pcfg->pcfg_nid;
116 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
125 name2num_lookup_name (name2num_t *table, char *str)
127 while (table->name != NULL)
128 if (!strcmp (str, table->name))
136 name2num_lookup_num (name2num_t *table, int num)
138 while (table->name != NULL)
139 if (num == table->num)
147 ptl_name2nal (char *str)
149 name2num_t *e = name2num_lookup_name (nalnames, str);
151 return ((e == NULL) ? -1 : e->num);
157 name2num_t *e = name2num_lookup_num (nalnames, nal);
159 return ((e == NULL) ? "???" : e->name);
162 static struct hostent *
163 ptl_gethostbyname(char * hname) {
165 he = gethostbyname(hname);
170 fprintf(stderr, "Unable to resolve hostname: %s\n",
174 fprintf(stderr, "gethostbyname error: %s\n",
184 ptl_parse_port (int *port, char *str)
188 *port = strtol (str, &end, 0);
190 if (*end == 0 && /* parsed whole string */
191 *port > 0 && *port < 65536) /* minimal sanity check */
198 ptl_parse_time (time_t *t, char *str)
204 *t = strtol (str, &end, 0);
205 if (*end == 0) /* parsed whole string */
208 memset (&tm, 0, sizeof (tm));
209 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
210 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
211 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
215 tm.tm_mon--; /* convert to 0 == Jan */
216 tm.tm_year -= 1900; /* y2k quirk */
217 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
220 if (*t == (time_t)-1)
227 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
235 if (!strcmp (str, "_all_"))
241 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
242 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
243 (c & ~0xff) == 0 && (d & ~0xff) == 0)
245 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
249 if ((('a' <= str[0] && str[0] <= 'z') ||
250 ('A' <= str[0] && str[0] <= 'Z')) &&
251 (he = ptl_gethostbyname (str)) != NULL)
253 __u32 addr = *(__u32 *)he->h_addr;
255 *ipaddrp = ntohl(addr); /* HOST byte order */
263 ptl_ipaddr_2_str (__u32 ipaddr, char *str)
268 net_ip = htonl (ipaddr);
269 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
273 sprintf (str, "%d.%d.%d.%d",
274 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
275 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
280 ptl_parse_nid (ptl_nid_t *nidp, char *str)
284 unsigned long long ullval;
286 if (!strcmp (str, "_all_")) {
291 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
292 *nidp = (ptl_nid_t)ipaddr;
296 ullval = strtoull(str, &end, 0);
298 /* parsed whole string */
299 *nidp = (ptl_nid_t)ullval;
307 ptl_nid2str (char *buffer, ptl_nid_t nid)
309 struct hostent *he = NULL;
311 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
312 * TCP addresses in the 0.x.x.x subnet are not in use. This can
313 * happen on routers and slows things down a _lot_. Bug 3442. */
314 if (nid & 0xff000000) {
315 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
316 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
319 sprintf(buffer, "%#x:%s", (int)(nid >> 32), he->h_name);
321 sprintf(buffer, LPX64, nid);
329 fprintf (stderr, "Error: you must run the 'network' command first.\n");
336 int g_nal_is_compatible (char *cmd, ...)
341 if (!g_nal_is_set ())
347 nal = va_arg (ap, int);
348 } while (nal != 0 && nal != g_nal);
356 /* Don't complain verbosely if we've not been passed a command
357 * name to complain about! */
358 fprintf (stderr, "Command %s not compatible with nal %s\n",
359 cmd, nal2name (g_nal));
365 sock_write (int cfd, void *buffer, int nob)
369 int rc = write (cfd, buffer, nob);
381 fprintf (stderr, "Unexpected zero sock_write\n");
386 buffer = (char *)buffer + nob;
393 sock_read (int cfd, void *buffer, int nob)
397 int rc = read (cfd, buffer, nob);
407 if (rc == 0) /* EOF */
409 errno = ECONNABORTED;
414 buffer = (char *)buffer + nob;
420 int ptl_initialize(int argc, char **argv)
422 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
427 int jt_ptl_network(int argc, char **argv)
433 (nal = ptl_name2nal (argv[1])) >= 0) {
438 fprintf(stderr, "usage: %s \n", argv[0]);
439 for (entry = nalnames; entry->name != NULL; entry++)
440 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
441 fprintf(stderr, ">\n");
446 jt_ptl_print_autoconnects (int argc, char **argv)
448 struct portals_cfg pcfg;
453 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
456 for (index = 0;;index++) {
457 PCFG_INIT (pcfg, NAL_CMD_GET_AUTOCONN);
458 pcfg.pcfg_count = index;
460 rc = pcfg_ioctl (&pcfg);
464 printf (LPX64"@%s:%d #%d buffer %d "
465 "nonagle %s affinity %s eager %s share %d\n",
466 pcfg.pcfg_nid, ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
467 pcfg.pcfg_misc, pcfg.pcfg_count, pcfg.pcfg_size,
468 (pcfg.pcfg_flags & 1) ? "on" : "off",
469 (pcfg.pcfg_flags & 2) ? "on" : "off",
470 (pcfg.pcfg_flags & 4) ? "on" : "off",
475 printf ("<no autoconnect routes>\n");
480 jt_ptl_add_autoconnect (int argc, char **argv)
482 struct portals_cfg pcfg;
486 int irq_affinity = 0;
491 if (argc < 4 || argc > 5) {
492 fprintf (stderr, "usage: %s nid ipaddr port [ise]\n", argv[0]);
496 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
499 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
500 nid == PTL_NID_ANY) {
501 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
505 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
506 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
510 if (ptl_parse_port (&port, argv[3]) != 0) {
511 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
516 char *opts = argv[4];
530 fprintf (stderr, "Can't parse options: %s\n",
536 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
539 pcfg.pcfg_misc = port;
540 /* only passing one buffer size! */
541 pcfg.pcfg_size = MAX (g_socket_rxmem, g_socket_txmem);
542 pcfg.pcfg_flags = (g_socket_nonagle ? 0x01 : 0) |
543 (irq_affinity ? 0x02 : 0) |
547 rc = pcfg_ioctl (&pcfg);
549 fprintf (stderr, "failed to enable autoconnect: %s\n",
558 jt_ptl_del_autoconnect (int argc, char **argv)
560 struct portals_cfg pcfg;
561 ptl_nid_t nid = PTL_NID_ANY;
568 fprintf (stderr, "usage: %s [nid] [ipaddr] [sk]\n",
573 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
577 ptl_parse_nid (&nid, argv[1]) != 0) {
578 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
583 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
584 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
589 char *opts = argv[3];
600 fprintf (stderr, "Can't parse flags: %s\n",
606 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
609 pcfg.pcfg_flags = (share ? 1 : 0) |
612 rc = pcfg_ioctl (&pcfg);
614 fprintf (stderr, "failed to remove autoconnect route: %s\n",
623 jt_ptl_print_connections (int argc, char **argv)
625 struct portals_cfg pcfg;
630 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
633 for (index = 0;;index++) {
634 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
635 pcfg.pcfg_count = index;
637 rc = pcfg_ioctl (&pcfg);
641 printf (LPX64"@%s:%d:%s\n",
643 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
645 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
646 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
647 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
648 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
652 printf ("<no connections>\n");
656 int jt_ptl_connect(int argc, char **argv)
658 struct portals_cfg pcfg;
659 struct sockaddr_in srvaddr;
667 int type = SOCKNAL_CONN_ANY;
673 fprintf(stderr, "usage: %s ip port [xibctr]\n", argv[0]);
677 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
680 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
682 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
686 if (ptl_parse_port (&port, argv[2]) != 0) {
687 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
692 for (flag = argv[3]; *flag != 0; flag++)
700 if (type != SOCKNAL_CONN_ANY) {
701 fprintf(stderr, "Can't flag type twice\n");
704 type = SOCKNAL_CONN_BULK_IN;
708 if (type != SOCKNAL_CONN_ANY) {
709 fprintf(stderr, "Can't flag type twice\n");
712 type = SOCKNAL_CONN_BULK_OUT;
716 if (type != SOCKNAL_CONN_ANY) {
717 fprintf(stderr, "Can't flag type twice\n");
720 type = SOCKNAL_CONN_CONTROL;
724 fprintf (stderr, "unrecognised flag '%c'\n",
729 memset(&srvaddr, 0, sizeof(srvaddr));
730 srvaddr.sin_family = AF_INET;
731 srvaddr.sin_port = htons(port);
732 srvaddr.sin_addr.s_addr = htonl(ipaddr);
734 fd = socket(PF_INET, SOCK_STREAM, 0);
736 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
740 if (g_socket_nonagle)
743 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &o, sizeof (o)) != 0) {
744 fprintf(stderr, "cannot disable nagle: %s\n", strerror(errno));
749 if (g_socket_rxmem != 0) {
751 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &o, sizeof (o)) != 0) {
752 fprintf(stderr, "cannot set receive buffer size: %s\n", strerror(errno));
757 if (g_socket_txmem != 0) {
759 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &o, sizeof (o)) != 0) {
760 fprintf(stderr, "cannot set send buffer size: %s\n", strerror(errno));
765 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
767 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
771 olen = sizeof (txmem);
772 if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, &txmem, &olen) != 0)
773 fprintf (stderr, "Can't get send buffer size: %s\n", strerror (errno));
774 olen = sizeof (rxmem);
775 if (getsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rxmem, &olen) != 0)
776 fprintf (stderr, "Can't get receive buffer size: %s\n", strerror (errno));
777 olen = sizeof (nonagle);
778 if (getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, &olen) != 0)
779 fprintf (stderr, "Can't get nagle: %s\n", strerror (errno));
781 printf("Connected host: %s snd: %d rcv: %d nagle: %s type: %s\n",
782 argv[1], txmem, rxmem, nonagle ? "Disabled" : "Enabled",
783 (type == SOCKNAL_CONN_ANY) ? "A" :
784 (type == SOCKNAL_CONN_CONTROL) ? "C" :
785 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
786 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
788 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
789 pcfg.pcfg_nal = g_nal;
791 pcfg.pcfg_flags = bind_irq;
792 pcfg.pcfg_misc = type;
794 rc = pcfg_ioctl(&pcfg);
796 fprintf(stderr, "failed to register fd with portals: %s\n",
802 printf("Connection to %s registered with socknal\n", argv[1]);
806 fprintf(stderr, "close failed: %d\n", rc);
811 int jt_ptl_disconnect(int argc, char **argv)
813 struct portals_cfg pcfg;
814 ptl_nid_t nid = PTL_NID_ANY;
819 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
823 if (!g_nal_is_compatible (NULL, SOCKNAL, 0))
827 ptl_parse_nid (&nid, argv[1]) != 0) {
828 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
833 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
834 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
838 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
840 pcfg.pcfg_id = ipaddr;
842 rc = pcfg_ioctl(&pcfg);
844 fprintf(stderr, "failed to remove connection: %s\n",
852 int jt_ptl_push_connection (int argc, char **argv)
854 struct portals_cfg pcfg;
856 ptl_nid_t nid = PTL_NID_ANY;
860 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
864 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
868 ptl_parse_nid (&nid, argv[1]) != 0) {
869 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
874 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
875 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
878 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
880 pcfg.pcfg_id = ipaddr;
882 rc = pcfg_ioctl(&pcfg);
884 fprintf(stderr, "failed to push connection: %s\n",
893 jt_ptl_print_active_txs (int argc, char **argv)
895 struct portals_cfg pcfg;
899 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
902 for (index = 0;;index++) {
903 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
904 pcfg.pcfg_count = index;
906 rc = pcfg_ioctl(&pcfg);
910 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
912 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
913 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
914 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
915 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
920 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
921 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
922 pcfg.pcfg_flags >> 2);
926 printf ("<no active descs>\n");
930 int jt_ptl_ping(int argc, char **argv)
937 struct portal_ioctl_data data;
940 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
947 if (ptl_parse_nid (&nid, argv[1]) != 0)
949 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
955 count = atol(argv[2]);
957 if (count < 0 || count > 20000)
959 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
968 timeout = atol (argv[4]);
970 PORTAL_IOC_INIT (data);
971 data.ioc_count = count;
972 data.ioc_size = size;
974 data.ioc_nal = g_nal;
975 data.ioc_timeout = timeout;
977 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
979 fprintf(stderr, "failed to start pinger: %s\n",
986 int jt_ptl_shownid(int argc, char **argv)
988 struct portal_ioctl_data data;
992 fprintf(stderr, "usage: %s\n", argv[0]);
999 PORTAL_IOC_INIT (data);
1000 data.ioc_nal = g_nal;
1001 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1003 fprintf(stderr, "getting my NID failed: %s\n",
1006 printf(LPX64"\n", data.ioc_nid);
1010 int jt_ptl_mynid(int argc, char **argv)
1013 char hostname[1024];
1015 struct portals_cfg pcfg;
1019 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1020 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1024 if (!g_nal_is_set())
1029 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1030 fprintf(stderr, "gethostname failed: %s\n",
1037 rc = ptl_parse_nid (&mynid, nidstr);
1039 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1043 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1044 pcfg.pcfg_nid = mynid;
1046 rc = pcfg_ioctl(&pcfg);
1048 fprintf(stderr, "setting my NID failed: %s\n",
1051 printf("registered my nid "LPX64" (%s)\n", mynid, hostname);
1056 jt_ptl_fail_nid (int argc, char **argv)
1060 unsigned int threshold;
1061 struct portal_ioctl_data data;
1063 if (argc < 2 || argc > 3)
1065 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1069 if (!g_nal_is_set())
1072 if (!strcmp (argv[1], "_all_"))
1074 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1076 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1081 threshold = PTL_MD_THRESH_INF;
1082 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1083 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1087 PORTAL_IOC_INIT (data);
1088 data.ioc_nal = g_nal;
1090 data.ioc_count = threshold;
1092 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1094 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1097 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1103 jt_ptl_rxmem (int argc, char **argv)
1109 if (Parser_size (&size, argv[1]) != 0 || size < 0)
1111 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1115 g_socket_rxmem = size;
1117 printf ("Socket rmem = %d\n", g_socket_rxmem);
1122 jt_ptl_txmem (int argc, char **argv)
1128 if (Parser_size (&size, argv[1]) != 0 || size < 0)
1130 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1133 g_socket_txmem = size;
1135 printf ("Socket txmem = %d\n", g_socket_txmem);
1140 jt_ptl_nagle (int argc, char **argv)
1146 if (Parser_bool (&enable, argv[1]) != 0)
1148 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1151 g_socket_nonagle = !enable;
1153 printf ("Nagle %s\n", g_socket_nonagle ? "disabled" : "enabled");
1158 jt_ptl_add_route (int argc, char **argv)
1160 struct portals_cfg pcfg;
1163 ptl_nid_t gateway_nid;
1168 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1172 if (!g_nal_is_set())
1175 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1177 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1181 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1183 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1189 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1191 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1195 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1196 pcfg.pcfg_nid = gateway_nid;
1197 pcfg.pcfg_nal = ROUTER;
1198 pcfg.pcfg_gw_nal = g_nal;
1199 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1200 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1202 rc = pcfg_ioctl(&pcfg);
1205 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1213 jt_ptl_del_route (int argc, char **argv)
1215 struct portals_cfg pcfg;
1217 ptl_nid_t nid1 = PTL_NID_ANY;
1218 ptl_nid_t nid2 = PTL_NID_ANY;
1223 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1227 if (!g_nal_is_set())
1230 if (ptl_parse_nid (&nid, argv[1]) != 0)
1232 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1237 ptl_parse_nid (&nid1, argv[2]) != 0)
1239 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1246 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1247 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1252 ptl_nid_t tmp = nid1;
1259 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1260 pcfg.pcfg_nal = ROUTER;
1261 pcfg.pcfg_gw_nal = g_nal;
1262 pcfg.pcfg_nid = nid;
1263 pcfg.pcfg_nid2 = nid1;
1264 pcfg.pcfg_nid3 = nid2;
1266 rc = pcfg_ioctl(&pcfg);
1269 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n", nid, strerror (errno));
1277 jt_ptl_notify_router (int argc, char **argv)
1279 struct portals_cfg pcfg;
1288 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1293 if (ptl_parse_nid (&nid, argv[1]) != 0)
1295 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1299 if (Parser_bool (&enable, argv[2]) != 0) {
1300 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1304 gettimeofday(&now, NULL);
1308 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1309 fprintf(stderr, "Can't parse time %s\n"
1310 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1311 "or an absolute unix time in seconds\n", argv[3]);
1313 } else if (when > now.tv_sec) {
1314 fprintf (stderr, "%s specifies a time in the future\n",
1319 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1320 pcfg.pcfg_nal = ROUTER;
1321 pcfg.pcfg_gw_nal = g_nal;
1322 pcfg.pcfg_nid = nid;
1323 pcfg.pcfg_flags = enable;
1324 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1325 pcfg.pcfg_nid3 = (__u64)when;
1327 rc = pcfg_ioctl(&pcfg);
1330 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1331 nid, strerror (errno));
1339 jt_ptl_print_routes (int argc, char **argv)
1341 char buffer[3][128];
1342 struct portals_cfg pcfg;
1346 ptl_nid_t gateway_nid;
1351 for (index = 0;;index++)
1353 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1354 pcfg.pcfg_nal = ROUTER;
1355 pcfg.pcfg_count = index;
1357 rc = pcfg_ioctl(&pcfg);
1361 gateway_nal = pcfg.pcfg_gw_nal;
1362 gateway_nid = pcfg.pcfg_nid;
1363 nid1 = pcfg.pcfg_nid2;
1364 nid2 = pcfg.pcfg_nid3;
1365 alive = pcfg.pcfg_flags;
1367 printf ("%8s %18s : %s - %s, %s\n",
1368 nal2name (gateway_nal),
1369 ptl_nid2str (buffer[0], gateway_nid),
1370 ptl_nid2str (buffer[1], nid1),
1371 ptl_nid2str (buffer[2], nid2),
1372 alive ? "up" : "down");
1378 lwt_control(int enable, int clear)
1380 struct portal_ioctl_data data;
1383 PORTAL_IOC_INIT(data);
1384 data.ioc_flags = enable;
1385 data.ioc_misc = clear;
1387 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1391 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1397 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1398 lwt_event_t *events, int size)
1400 struct portal_ioctl_data data;
1403 PORTAL_IOC_INIT(data);
1404 data.ioc_pbuf1 = (char *)events;
1405 data.ioc_plen1 = size;
1407 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1409 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1414 LASSERT (data.ioc_count != 0);
1415 LASSERT (data.ioc_misc != 0);
1418 *now = data.ioc_nid;
1421 *ncpu = data.ioc_count;
1423 if (totalsize != NULL)
1424 *totalsize = data.ioc_misc;
1430 lwt_get_string(char *kstr)
1433 struct portal_ioctl_data data;
1437 /* FIXME: this could maintain a symbol table since we expect to be
1438 * looking up the same strings all the time... */
1440 PORTAL_IOC_INIT(data);
1441 data.ioc_pbuf1 = kstr;
1442 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1443 data.ioc_pbuf2 = NULL;
1446 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1448 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1453 size = data.ioc_count;
1454 ustr = (char *)malloc(size);
1456 fprintf(stderr, "Can't allocate string storage of size %d\n",
1461 PORTAL_IOC_INIT(data);
1462 data.ioc_pbuf1 = kstr;
1463 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1464 data.ioc_pbuf2 = ustr;
1465 data.ioc_plen2 = size;
1467 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1469 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1474 LASSERT(strlen(ustr) == size - 1);
1479 lwt_put_string(char *ustr)
1485 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1488 char *where = lwt_get_string(e->lwte_where);
1493 sprintf(whenstr, LPD64, e->lwte_when - t0);
1495 fprintf(f, "%#010lx %#010lx %#010lx %#010lx: %#010lx %1d %10.6f %10.2f %s\n",
1496 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1497 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1498 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1501 lwt_put_string(where);
1507 get_cycles_per_usec ()
1509 FILE *f = fopen ("/proc/cpuinfo", "r");
1514 while (fgets (line, sizeof (line), f) != NULL)
1515 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1522 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1527 jt_ptl_lwt(int argc, char **argv)
1531 int nevents_per_cpu;
1532 lwt_event_t *events;
1533 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1534 lwt_event_t *next_event[LWT_MAX_CPUS];
1535 lwt_event_t *first_event[LWT_MAX_CPUS];
1544 struct timeval tvnow;
1545 int printed_date = 0;
1549 (strcmp(argv[1], "start") &&
1550 strcmp(argv[1], "stop"))) {
1553 " %s stop [fname]\n", argv[0], argv[0]);
1557 if (!strcmp(argv[1], "start")) {
1559 if (lwt_control(0, 0) != 0)
1563 if (lwt_control(0, 1) != 0)
1567 if (lwt_control(1, 0) != 0)
1573 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1576 if (ncpus > LWT_MAX_CPUS) {
1577 fprintf(stderr, "Too many cpus: %d (%d)\n",
1578 ncpus, LWT_MAX_CPUS);
1582 events = (lwt_event_t *)malloc(totalspace);
1583 if (events == NULL) {
1584 fprintf(stderr, "Can't allocate %d\n", totalspace);
1588 if (lwt_control(0, 0) != 0) { /* disable */
1593 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1598 /* we want this time to be sampled at snapshot time */
1599 gettimeofday(&tvnow, NULL);
1602 f = fopen (argv[2], "w");
1604 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1610 mhz = get_cycles_per_usec();
1612 /* carve events into per-cpu slices */
1613 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1614 for (cpu = 0; cpu <= ncpus; cpu++)
1615 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1617 /* find the earliest event on each cpu */
1618 for (cpu = 0; cpu < ncpus; cpu++) {
1619 first_event[cpu] = NULL;
1621 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1623 if (e->lwte_where == NULL) /* not an event */
1626 if (first_event[cpu] == NULL ||
1627 first_event[cpu]->lwte_when > e->lwte_when)
1628 first_event[cpu] = e;
1631 next_event[cpu] = first_event[cpu];
1635 for (cpu = 0; cpu < ncpus; cpu++) {
1636 e = first_event[cpu];
1637 if (e == NULL) /* no events this cpu */
1640 if (e == cpu_event[cpu])
1641 e = cpu_event[cpu + 1] - 1;
1645 /* If there's an event immediately before the first one, this
1646 * cpu wrapped its event buffer */
1647 if (e->lwte_where == NULL)
1650 /* We should only start outputting events from the most recent
1651 * first event in any wrapped cpu. Events before this time on
1652 * other cpus won't have any events from this CPU to interleave
1654 if (t0 < first_event[cpu]->lwte_when)
1655 t0 = first_event[cpu]->lwte_when;
1659 /* find which cpu has the next event */
1661 for (i = 0; i < ncpus; i++) {
1663 if (next_event[i] == NULL) /* this cpu exhausted */
1667 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1671 if (cpu < 0) /* all cpus exhausted */
1675 /* no wrapped cpus and this is he first ever event */
1676 t0 = next_event[cpu]->lwte_when;
1679 if (t0 <= next_event[cpu]->lwte_when) {
1680 /* on or after the first event */
1681 if (!printed_date) {
1682 cycles_t du = (tnow - t0) / mhz;
1683 time_t then = tvnow.tv_sec - du/1000000;
1685 if (du % 1000000 > tvnow.tv_usec)
1688 fprintf(f, "%s", ctime(&then));
1692 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1697 tlast = next_event[cpu]->lwte_when;
1700 if (next_event[cpu] == cpu_event[cpu + 1])
1701 next_event[cpu] = cpu_event[cpu];
1703 if (next_event[cpu]->lwte_where == NULL ||
1704 next_event[cpu] == first_event[cpu])
1705 next_event[cpu] = NULL;
1715 int jt_ptl_memhog(int argc, char **argv)
1717 static int gfp = 0; /* sticky! */
1719 struct portal_ioctl_data data;
1725 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1729 count = strtol(argv[1], &end, 0);
1730 if (count < 0 || *end != 0) {
1731 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1736 rc = strtol(argv[2], &end, 0);
1738 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1744 PORTAL_IOC_INIT(data);
1745 data.ioc_count = count;
1746 data.ioc_flags = gfp;
1747 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1750 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1754 printf("memhog %d OK\n", count);