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>
37 #include <asm/byteorder.h>
39 #include <portals/api-support.h>
40 #include <portals/ptlctl.h>
41 #include <portals/list.h>
42 #include <portals/lib-types.h>
43 #include <portals/socknal.h>
46 unsigned int portal_debug;
47 unsigned int portal_printk;
48 unsigned int portal_stack;
49 unsigned int portal_cerror;
51 static unsigned int g_nal = 0;
53 static int g_socket_txmem = 0;
54 static int g_socket_rxmem = 0;
55 static int g_socket_nonagle = 1;
63 static name2num_t nalnames[] = {
69 {"scimac", SCIMACNAL},
73 static cfg_record_cb_t g_record_cb;
76 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
83 pcfg_ioctl(struct portals_cfg *pcfg)
87 if (pcfg->pcfg_nal ==0)
88 pcfg->pcfg_nal = g_nal;
91 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
93 struct portal_ioctl_data data;
94 PORTAL_IOC_INIT (data);
95 data.ioc_pbuf1 = (char*)pcfg;
96 data.ioc_plen1 = sizeof(*pcfg);
98 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
107 name2num_lookup_name (name2num_t *table, char *str)
109 while (table->name != NULL)
110 if (!strcmp (str, table->name))
118 name2num_lookup_num (name2num_t *table, int num)
120 while (table->name != NULL)
121 if (num == table->num)
129 ptl_name2nal (char *str)
131 name2num_t *e = name2num_lookup_name (nalnames, str);
133 return ((e == NULL) ? -1 : e->num);
139 name2num_t *e = name2num_lookup_num (nalnames, nal);
141 return ((e == NULL) ? "???" : e->name);
144 static struct hostent *
145 ptl_gethostbyname(char * hname) {
147 he = gethostbyname(hname);
152 fprintf(stderr, "Unable to resolve hostname: %s\n",
156 fprintf(stderr, "gethostbyname error: %s\n",
166 ptl_parse_port (int *port, char *str)
170 *port = strtol (str, &end, 0);
172 if (*end == 0 && /* parsed whole string */
173 *port > 0 && *port < 65536) /* minimal sanity check */
180 ptl_parse_time (time_t *t, char *str)
186 *t = strtol (str, &end, 0);
187 if (*end == 0) /* parsed whole string */
190 memset (&tm, 0, sizeof (tm));
191 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
192 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
193 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
197 tm.tm_mon--; /* convert to 0 == Jan */
198 tm.tm_year -= 1900; /* y2k quirk */
199 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
202 if (*t == (time_t)-1)
209 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
217 if (!strcmp (str, "_all_"))
223 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
224 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
225 (c & ~0xff) == 0 && (d & ~0xff) == 0)
227 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
231 if ((('a' <= str[0] && str[0] <= 'z') ||
232 ('A' <= str[0] && str[0] <= 'Z')) &&
233 (he = ptl_gethostbyname (str)) != NULL)
235 __u32 addr = *(__u32 *)he->h_addr;
237 *ipaddrp = ntohl(addr); /* HOST byte order */
245 ptl_ipaddr_2_str (__u32 ipaddr, char *str)
250 net_ip = htonl (ipaddr);
251 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
255 sprintf (str, "%d.%d.%d.%d",
256 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
257 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
262 ptl_parse_nid (ptl_nid_t *nidp, char *str)
266 unsigned long long ullval;
268 if (!strcmp (str, "_all_")) {
273 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
274 *nidp = (ptl_nid_t)ipaddr;
278 ullval = strtoull(str, &end, 0);
280 /* parsed whole string */
281 *nidp = (ptl_nid_t)ullval;
289 ptl_nid2str (char *buffer, ptl_nid_t nid)
291 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
292 struct hostent *he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
295 strcpy (buffer, he->h_name);
297 sprintf (buffer, LPX64, nid);
305 fprintf (stderr, "Error: you must run the 'network' command first.\n");
312 int g_nal_is_compatible (char *cmd, ...)
317 if (!g_nal_is_set ())
323 nal = va_arg (ap, int);
324 } while (nal != 0 && nal != g_nal);
332 /* Don't complain verbosely if we've not been passed a command
333 * name to complain about! */
334 fprintf (stderr, "Command %s not compatible with nal %s\n",
335 cmd, nal2name (g_nal));
341 sock_write (int cfd, void *buffer, int nob)
345 int rc = write (cfd, buffer, nob);
357 fprintf (stderr, "Unexpected zero sock_write\n");
362 buffer = (char *)buffer + nob;
369 sock_read (int cfd, void *buffer, int nob)
373 int rc = read (cfd, buffer, nob);
383 if (rc == 0) /* EOF */
385 errno = ECONNABORTED;
390 buffer = (char *)buffer + nob;
396 int ptl_initialize(int argc, char **argv)
398 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
403 int jt_ptl_network(int argc, char **argv)
409 (nal = ptl_name2nal (argv[1])) >= 0) {
414 fprintf(stderr, "usage: %s \n", argv[0]);
415 for (entry = nalnames; entry->name != NULL; entry++)
416 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
417 fprintf(stderr, ">\n");
422 jt_ptl_print_autoconnects (int argc, char **argv)
424 struct portals_cfg pcfg;
429 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
432 for (index = 0;;index++) {
433 PCFG_INIT (pcfg, NAL_CMD_GET_AUTOCONN);
434 pcfg.pcfg_count = index;
436 rc = pcfg_ioctl (&pcfg);
440 printf (LPX64"@%s:%d #%d buffer %d "
441 "nonagle %s affinity %s eager %s share %d\n",
442 pcfg.pcfg_nid, ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
443 pcfg.pcfg_misc, pcfg.pcfg_count, pcfg.pcfg_size,
444 (pcfg.pcfg_flags & 1) ? "on" : "off",
445 (pcfg.pcfg_flags & 2) ? "on" : "off",
446 (pcfg.pcfg_flags & 4) ? "on" : "off",
451 printf ("<no autoconnect routes>\n");
456 jt_ptl_add_autoconnect (int argc, char **argv)
458 struct portals_cfg pcfg;
462 int irq_affinity = 0;
467 if (argc < 4 || argc > 5) {
468 fprintf (stderr, "usage: %s nid ipaddr port [ise]\n", argv[0]);
472 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
475 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
476 nid == PTL_NID_ANY) {
477 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
481 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
482 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
486 if (ptl_parse_port (&port, argv[3]) != 0) {
487 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
492 char *opts = argv[4];
506 fprintf (stderr, "Can't parse options: %s\n",
512 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
515 pcfg.pcfg_misc = port;
516 /* only passing one buffer size! */
517 pcfg.pcfg_size = MAX (g_socket_rxmem, g_socket_txmem);
518 pcfg.pcfg_flags = (g_socket_nonagle ? 0x01 : 0) |
519 (irq_affinity ? 0x02 : 0) |
523 rc = pcfg_ioctl (&pcfg);
525 fprintf (stderr, "failed to enable autoconnect: %s\n",
534 jt_ptl_del_autoconnect (int argc, char **argv)
536 struct portals_cfg pcfg;
537 ptl_nid_t nid = PTL_NID_ANY;
544 fprintf (stderr, "usage: %s [nid] [ipaddr] [sk]\n",
549 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
553 ptl_parse_nid (&nid, argv[1]) != 0) {
554 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
559 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
560 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
565 char *opts = argv[3];
576 fprintf (stderr, "Can't parse flags: %s\n",
582 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
585 pcfg.pcfg_flags = (share ? 1 : 0) |
588 rc = pcfg_ioctl (&pcfg);
590 fprintf (stderr, "failed to remove autoconnect route: %s\n",
599 jt_ptl_print_connections (int argc, char **argv)
601 struct portals_cfg pcfg;
606 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
609 for (index = 0;;index++) {
610 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
611 pcfg.pcfg_count = index;
613 rc = pcfg_ioctl (&pcfg);
617 printf (LPX64"@%s:%d:%s\n",
619 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
621 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
622 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
623 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
624 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
628 printf ("<no connections>\n");
632 int jt_ptl_connect(int argc, char **argv)
634 struct portals_cfg pcfg;
635 struct sockaddr_in srvaddr;
643 int type = SOCKNAL_CONN_ANY;
649 fprintf(stderr, "usage: %s ip port [xibctr]\n", argv[0]);
653 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
656 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
658 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
662 if (ptl_parse_port (&port, argv[2]) != 0) {
663 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
668 for (flag = argv[3]; *flag != 0; flag++)
676 if (type != SOCKNAL_CONN_ANY) {
677 fprintf(stderr, "Can't flag type twice\n");
680 type = SOCKNAL_CONN_BULK_IN;
684 if (type != SOCKNAL_CONN_ANY) {
685 fprintf(stderr, "Can't flag type twice\n");
688 type = SOCKNAL_CONN_BULK_OUT;
692 if (type != SOCKNAL_CONN_ANY) {
693 fprintf(stderr, "Can't flag type twice\n");
696 type = SOCKNAL_CONN_CONTROL;
700 fprintf (stderr, "unrecognised flag '%c'\n",
705 memset(&srvaddr, 0, sizeof(srvaddr));
706 srvaddr.sin_family = AF_INET;
707 srvaddr.sin_port = htons(port);
708 srvaddr.sin_addr.s_addr = htonl(ipaddr);
710 fd = socket(PF_INET, SOCK_STREAM, 0);
712 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
716 if (g_socket_nonagle)
719 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &o, sizeof (o)) != 0) {
720 fprintf(stderr, "cannot disable nagle: %s\n", strerror(errno));
725 if (g_socket_rxmem != 0) {
727 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &o, sizeof (o)) != 0) {
728 fprintf(stderr, "cannot set receive buffer size: %s\n", strerror(errno));
733 if (g_socket_txmem != 0) {
735 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &o, sizeof (o)) != 0) {
736 fprintf(stderr, "cannot set send buffer size: %s\n", strerror(errno));
741 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
743 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
747 olen = sizeof (txmem);
748 if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, &txmem, &olen) != 0)
749 fprintf (stderr, "Can't get send buffer size: %s\n", strerror (errno));
750 olen = sizeof (rxmem);
751 if (getsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rxmem, &olen) != 0)
752 fprintf (stderr, "Can't get receive buffer size: %s\n", strerror (errno));
753 olen = sizeof (nonagle);
754 if (getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, &olen) != 0)
755 fprintf (stderr, "Can't get nagle: %s\n", strerror (errno));
757 printf("Connected host: %s snd: %d rcv: %d nagle: %s type: %s\n",
758 argv[1], txmem, rxmem, nonagle ? "Disabled" : "Enabled",
759 (type == SOCKNAL_CONN_ANY) ? "A" :
760 (type == SOCKNAL_CONN_CONTROL) ? "C" :
761 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
762 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
764 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
765 pcfg.pcfg_nal = g_nal;
767 pcfg.pcfg_flags = bind_irq;
768 pcfg.pcfg_misc = type;
770 rc = pcfg_ioctl(&pcfg);
772 fprintf(stderr, "failed to register fd with portals: %s\n",
778 printf("Connection to %s registered with socknal\n", argv[1]);
782 fprintf(stderr, "close failed: %d\n", rc);
787 int jt_ptl_disconnect(int argc, char **argv)
789 struct portals_cfg pcfg;
790 ptl_nid_t nid = PTL_NID_ANY;
795 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
799 if (!g_nal_is_compatible (NULL, SOCKNAL, 0))
803 ptl_parse_nid (&nid, argv[1]) != 0) {
804 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
809 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
810 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
814 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
816 pcfg.pcfg_id = ipaddr;
818 rc = pcfg_ioctl(&pcfg);
820 fprintf(stderr, "failed to remove connection: %s\n",
828 int jt_ptl_push_connection (int argc, char **argv)
830 struct portals_cfg pcfg;
832 ptl_nid_t nid = PTL_NID_ANY;
836 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
840 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
844 ptl_parse_nid (&nid, argv[1]) != 0) {
845 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
850 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
851 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
854 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
856 pcfg.pcfg_id = ipaddr;
858 rc = pcfg_ioctl(&pcfg);
860 fprintf(stderr, "failed to push connection: %s\n",
869 jt_ptl_print_active_txs (int argc, char **argv)
871 struct portals_cfg pcfg;
875 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
878 for (index = 0;;index++) {
879 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
880 pcfg.pcfg_count = index;
882 rc = pcfg_ioctl(&pcfg);
886 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
888 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
889 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
890 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
891 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
896 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
897 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
898 pcfg.pcfg_flags >> 2);
902 printf ("<no active descs>\n");
906 int jt_ptl_ping(int argc, char **argv)
913 struct portal_ioctl_data data;
916 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
923 if (ptl_parse_nid (&nid, argv[1]) != 0)
925 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
931 count = atol(argv[2]);
933 if (count < 0 || count > 20000)
935 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
944 timeout = atol (argv[4]);
946 PORTAL_IOC_INIT (data);
947 data.ioc_count = count;
948 data.ioc_size = size;
950 data.ioc_nal = g_nal;
951 data.ioc_timeout = timeout;
953 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
955 fprintf(stderr, "failed to start pinger: %s\n",
962 int jt_ptl_shownid(int argc, char **argv)
964 struct portal_ioctl_data data;
968 fprintf(stderr, "usage: %s\n", argv[0]);
975 PORTAL_IOC_INIT (data);
976 data.ioc_nal = g_nal;
977 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
979 fprintf(stderr, "getting my NID failed: %s\n",
982 printf(LPX64"\n", data.ioc_nid);
986 int jt_ptl_mynid(int argc, char **argv)
991 struct portals_cfg pcfg;
995 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
996 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1000 if (!g_nal_is_set())
1005 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1006 fprintf(stderr, "gethostname failed: %s\n",
1013 rc = ptl_parse_nid (&mynid, nidstr);
1015 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1019 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1020 pcfg.pcfg_nid = mynid;
1022 rc = pcfg_ioctl(&pcfg);
1024 fprintf(stderr, "setting my NID failed: %s\n",
1027 printf("registered my nid "LPX64" (%s)\n", mynid, hostname);
1032 jt_ptl_fail_nid (int argc, char **argv)
1036 unsigned int threshold;
1037 struct portal_ioctl_data data;
1039 if (argc < 2 || argc > 3)
1041 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1045 if (!g_nal_is_set())
1048 if (!strcmp (argv[1], "_all_"))
1050 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1052 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1057 threshold = PTL_MD_THRESH_INF;
1058 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1059 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1063 PORTAL_IOC_INIT (data);
1064 data.ioc_nal = g_nal;
1066 data.ioc_count = threshold;
1068 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1070 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1073 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1079 jt_ptl_rxmem (int argc, char **argv)
1085 if (Parser_size (&size, argv[1]) != 0 || size < 0)
1087 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1091 g_socket_rxmem = size;
1093 printf ("Socket rmem = %d\n", g_socket_rxmem);
1098 jt_ptl_txmem (int argc, char **argv)
1104 if (Parser_size (&size, argv[1]) != 0 || size < 0)
1106 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1109 g_socket_txmem = size;
1111 printf ("Socket txmem = %d\n", g_socket_txmem);
1116 jt_ptl_nagle (int argc, char **argv)
1122 if (Parser_bool (&enable, argv[1]) != 0)
1124 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1127 g_socket_nonagle = !enable;
1129 printf ("Nagle %s\n", g_socket_nonagle ? "disabled" : "enabled");
1134 jt_ptl_add_route (int argc, char **argv)
1136 struct portals_cfg pcfg;
1139 ptl_nid_t gateway_nid;
1144 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1148 if (!g_nal_is_set())
1151 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1153 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1157 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1159 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1165 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1167 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1171 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1172 pcfg.pcfg_nid = gateway_nid;
1173 pcfg.pcfg_nal = ROUTER;
1174 pcfg.pcfg_gw_nal = g_nal;
1175 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1176 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1178 rc = pcfg_ioctl(&pcfg);
1181 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1189 jt_ptl_del_route (int argc, char **argv)
1191 struct portals_cfg pcfg;
1193 ptl_nid_t nid1 = PTL_NID_ANY;
1194 ptl_nid_t nid2 = PTL_NID_ANY;
1199 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1203 if (!g_nal_is_set())
1206 if (ptl_parse_nid (&nid, argv[1]) != 0)
1208 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1213 ptl_parse_nid (&nid1, argv[2]) != 0)
1215 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1222 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1223 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1228 ptl_nid_t tmp = nid1;
1235 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1236 pcfg.pcfg_nal = ROUTER;
1237 pcfg.pcfg_gw_nal = g_nal;
1238 pcfg.pcfg_nid = nid;
1239 pcfg.pcfg_nid2 = nid1;
1240 pcfg.pcfg_nid3 = nid2;
1242 rc = pcfg_ioctl(&pcfg);
1245 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n", nid, strerror (errno));
1253 jt_ptl_notify_router (int argc, char **argv)
1255 struct portals_cfg pcfg;
1264 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1269 if (ptl_parse_nid (&nid, argv[1]) != 0)
1271 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1275 if (Parser_bool (&enable, argv[2]) != 0) {
1276 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1280 gettimeofday(&now, NULL);
1284 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1285 fprintf(stderr, "Can't parse time %s\n"
1286 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1287 "or an absolute unix time in seconds\n", argv[3]);
1289 } else if (when > now.tv_sec) {
1290 fprintf (stderr, "%s specifies a time in the future\n",
1295 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1296 pcfg.pcfg_nal = ROUTER;
1297 pcfg.pcfg_gw_nal = g_nal;
1298 pcfg.pcfg_nid = nid;
1299 pcfg.pcfg_flags = enable;
1300 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1301 pcfg.pcfg_nid3 = (__u64)when;
1303 rc = pcfg_ioctl(&pcfg);
1306 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1307 nid, strerror (errno));
1315 jt_ptl_print_routes (int argc, char **argv)
1317 char buffer[3][128];
1318 struct portals_cfg pcfg;
1322 ptl_nid_t gateway_nid;
1327 for (index = 0;;index++)
1329 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1330 pcfg.pcfg_nal = ROUTER;
1331 pcfg.pcfg_count = index;
1333 rc = pcfg_ioctl(&pcfg);
1337 gateway_nal = pcfg.pcfg_gw_nal;
1338 gateway_nid = pcfg.pcfg_nid;
1339 nid1 = pcfg.pcfg_nid2;
1340 nid2 = pcfg.pcfg_nid3;
1341 alive = pcfg.pcfg_flags;
1343 printf ("%8s %18s : %s - %s, %s\n",
1344 nal2name (gateway_nal),
1345 ptl_nid2str (buffer[0], gateway_nid),
1346 ptl_nid2str (buffer[1], nid1),
1347 ptl_nid2str (buffer[2], nid2),
1348 alive ? "up" : "down");
1354 lwt_control(int enable, int clear)
1356 struct portal_ioctl_data data;
1359 PORTAL_IOC_INIT(data);
1360 data.ioc_flags = enable;
1361 data.ioc_misc = clear;
1363 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1367 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1373 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1374 lwt_event_t *events, int size)
1376 struct portal_ioctl_data data;
1379 PORTAL_IOC_INIT(data);
1380 data.ioc_pbuf1 = (char *)events;
1381 data.ioc_plen1 = size;
1383 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1385 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1390 LASSERT (data.ioc_count != 0);
1391 LASSERT (data.ioc_misc != 0);
1394 *now = data.ioc_nid;
1397 *ncpu = data.ioc_count;
1399 if (totalsize != NULL)
1400 *totalsize = data.ioc_misc;
1406 lwt_get_string(char *kstr)
1409 struct portal_ioctl_data data;
1413 /* FIXME: this could maintain a symbol table since we expect to be
1414 * looking up the same strings all the time... */
1416 PORTAL_IOC_INIT(data);
1417 data.ioc_pbuf1 = kstr;
1418 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1419 data.ioc_pbuf2 = NULL;
1422 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1424 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1429 size = data.ioc_count;
1430 ustr = (char *)malloc(size);
1432 fprintf(stderr, "Can't allocate string storage of size %d\n",
1437 PORTAL_IOC_INIT(data);
1438 data.ioc_pbuf1 = kstr;
1439 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1440 data.ioc_pbuf2 = ustr;
1441 data.ioc_plen2 = size;
1443 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1445 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1450 LASSERT(strlen(ustr) == size - 1);
1455 lwt_put_string(char *ustr)
1461 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1464 char *where = lwt_get_string(e->lwte_where);
1469 sprintf(whenstr, LPD64, e->lwte_when - t0);
1471 fprintf(f, "%#010lx %#010lx %#010lx %#010lx: %#010lx %1d %10.6f %10.2f %s\n",
1472 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1473 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1474 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1477 lwt_put_string(where);
1483 get_cycles_per_usec ()
1485 FILE *f = fopen ("/proc/cpuinfo", "r");
1490 while (fgets (line, sizeof (line), f) != NULL)
1491 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1498 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1503 jt_ptl_lwt(int argc, char **argv)
1507 int nevents_per_cpu;
1508 lwt_event_t *events;
1509 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1510 lwt_event_t *next_event[LWT_MAX_CPUS];
1511 lwt_event_t *first_event[LWT_MAX_CPUS];
1520 struct timeval tvnow;
1521 int printed_date = 0;
1525 (strcmp(argv[1], "start") &&
1526 strcmp(argv[1], "stop"))) {
1529 " %s stop [fname]\n", argv[0], argv[0]);
1533 if (!strcmp(argv[1], "start")) {
1535 if (lwt_control(0, 0) != 0)
1539 if (lwt_control(0, 1) != 0)
1543 if (lwt_control(1, 0) != 0)
1549 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1552 if (ncpus > LWT_MAX_CPUS) {
1553 fprintf(stderr, "Too many cpus: %d (%d)\n",
1554 ncpus, LWT_MAX_CPUS);
1558 events = (lwt_event_t *)malloc(totalspace);
1559 if (events == NULL) {
1560 fprintf(stderr, "Can't allocate %d\n", totalspace);
1564 if (lwt_control(0, 0) != 0) { /* disable */
1569 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1574 /* we want this time to be sampled at snapshot time */
1575 gettimeofday(&tvnow, NULL);
1578 f = fopen (argv[2], "w");
1580 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1586 mhz = get_cycles_per_usec();
1588 /* carve events into per-cpu slices */
1589 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1590 for (cpu = 0; cpu <= ncpus; cpu++)
1591 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1593 /* find the earliest event on each cpu */
1594 for (cpu = 0; cpu < ncpus; cpu++) {
1595 first_event[cpu] = NULL;
1597 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1599 if (e->lwte_where == NULL) /* not an event */
1602 if (first_event[cpu] == NULL ||
1603 first_event[cpu]->lwte_when > e->lwte_when)
1604 first_event[cpu] = e;
1607 next_event[cpu] = first_event[cpu];
1611 for (cpu = 0; cpu < ncpus; cpu++) {
1612 e = first_event[cpu];
1613 if (e == NULL) /* no events this cpu */
1616 if (e == cpu_event[cpu])
1617 e = cpu_event[cpu + 1] - 1;
1621 /* If there's an event immediately before the first one, this
1622 * cpu wrapped its event buffer */
1623 if (e->lwte_where == NULL)
1626 /* We should only start outputting events from the most recent
1627 * first event in any wrapped cpu. Events before this time on
1628 * other cpus won't have any events from this CPU to interleave
1630 if (t0 < first_event[cpu]->lwte_when)
1631 t0 = first_event[cpu]->lwte_when;
1635 /* find which cpu has the next event */
1637 for (i = 0; i < ncpus; i++) {
1639 if (next_event[i] == NULL) /* this cpu exhausted */
1643 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1647 if (cpu < 0) /* all cpus exhausted */
1651 /* no wrapped cpus and this is he first ever event */
1652 t0 = next_event[cpu]->lwte_when;
1655 if (t0 <= next_event[cpu]->lwte_when) {
1656 /* on or after the first event */
1657 if (!printed_date) {
1658 cycles_t du = (tnow - t0) / mhz;
1659 time_t then = tvnow.tv_sec - du/1000000;
1661 if (du % 1000000 > tvnow.tv_usec)
1664 fprintf(f, "%s", ctime(&then));
1668 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1673 tlast = next_event[cpu]->lwte_when;
1676 if (next_event[cpu] == cpu_event[cpu + 1])
1677 next_event[cpu] = cpu_event[cpu];
1679 if (next_event[cpu]->lwte_where == NULL ||
1680 next_event[cpu] == first_event[cpu])
1681 next_event[cpu] = NULL;
1691 int jt_ptl_memhog(int argc, char **argv)
1693 static int gfp = 0; /* sticky! */
1695 struct portal_ioctl_data data;
1701 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1705 count = strtol(argv[1], &end, 0);
1706 if (count < 0 || *end != 0) {
1707 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1712 rc = strtol(argv[2], &end, 0);
1714 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1720 PORTAL_IOC_INIT(data);
1721 data.ioc_count = count;
1722 data.ioc_flags = gfp;
1723 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1726 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1730 printf("memhog %d OK\n", count);