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;
90 /* Convert a string boolean to an int; "enable" -> 1 */
91 int ptl_parse_bool (int *b, char *str) {
92 if (!strcasecmp (str, "no") ||
93 !strcasecmp (str, "n") ||
94 !strcasecmp (str, "off") ||
95 !strcasecmp (str, "down") ||
96 !strcasecmp (str, "disable"))
102 if (!strcasecmp (str, "yes") ||
103 !strcasecmp (str, "y") ||
104 !strcasecmp (str, "on") ||
105 !strcasecmp (str, "up") ||
106 !strcasecmp (str, "enable"))
115 /* Convert human readable size string to and int; "1k" -> 1000 */
116 int ptl_parse_size (int *sizep, char *str) {
120 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
153 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
160 pcfg_ioctl(struct portals_cfg *pcfg)
164 if (pcfg->pcfg_nal ==0)
165 pcfg->pcfg_nal = g_nal;
168 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
170 struct portal_ioctl_data data;
171 PORTAL_IOC_INIT (data);
172 data.ioc_pbuf1 = (char*)pcfg;
173 data.ioc_plen1 = sizeof(*pcfg);
174 /* XXX liblustre hack XXX */
175 data.ioc_nal_cmd = pcfg->pcfg_command;
176 data.ioc_nid = pcfg->pcfg_nid;
178 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
187 name2num_lookup_name (name2num_t *table, char *str)
189 while (table->name != NULL)
190 if (!strcmp (str, table->name))
198 name2num_lookup_num (name2num_t *table, int num)
200 while (table->name != NULL)
201 if (num == table->num)
209 ptl_name2nal (char *str)
211 name2num_t *e = name2num_lookup_name (nalnames, str);
213 return ((e == NULL) ? -1 : e->num);
219 name2num_t *e = name2num_lookup_num (nalnames, nal);
221 return ((e == NULL) ? "???" : e->name);
224 static struct hostent *
225 ptl_gethostbyname(char * hname) {
227 he = gethostbyname(hname);
232 fprintf(stderr, "Unable to resolve hostname: %s\n",
236 fprintf(stderr, "gethostbyname error: %s\n",
246 ptl_parse_port (int *port, char *str)
250 *port = strtol (str, &end, 0);
252 if (*end == 0 && /* parsed whole string */
253 *port > 0 && *port < 65536) /* minimal sanity check */
260 ptl_parse_time (time_t *t, char *str)
266 *t = strtol (str, &end, 0);
267 if (*end == 0) /* parsed whole string */
270 memset (&tm, 0, sizeof (tm));
271 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
272 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
273 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
277 tm.tm_mon--; /* convert to 0 == Jan */
278 tm.tm_year -= 1900; /* y2k quirk */
279 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
282 if (*t == (time_t)-1)
289 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
297 if (!strcmp (str, "_all_"))
303 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
304 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
305 (c & ~0xff) == 0 && (d & ~0xff) == 0)
307 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
311 if ((('a' <= str[0] && str[0] <= 'z') ||
312 ('A' <= str[0] && str[0] <= 'Z')) &&
313 (he = ptl_gethostbyname (str)) != NULL)
315 __u32 addr = *(__u32 *)he->h_addr;
317 *ipaddrp = ntohl(addr); /* HOST byte order */
325 ptl_ipaddr_2_str (__u32 ipaddr, char *str)
330 net_ip = htonl (ipaddr);
331 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
335 sprintf (str, "%d.%d.%d.%d",
336 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
337 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
342 ptl_parse_nid (ptl_nid_t *nidp, char *str)
346 unsigned long long ullval;
348 if (!strcmp (str, "_all_")) {
353 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
354 *nidp = (ptl_nid_t)ipaddr;
358 ullval = strtoull(str, &end, 0);
360 /* parsed whole string */
361 *nidp = (ptl_nid_t)ullval;
368 __u64 ptl_nid2u64(ptl_nid_t nid)
370 switch (sizeof (nid)) {
376 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %d\n", sizeof(nid));
384 ptl_nid2str (char *buffer, ptl_nid_t nid)
386 __u64 nid64 = ptl_nid2u64(nid);
389 if ((nid64 & ~((__u64)((__u32)-1))) != 0) {
393 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
395 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
399 strcpy (buffer, he->h_name);
401 sprintf (buffer, LPX64, nid64);
409 fprintf (stderr, "Error: you must run the 'network' command first.\n");
416 int g_nal_is_compatible (char *cmd, ...)
421 if (!g_nal_is_set ())
427 nal = va_arg (ap, int);
428 } while (nal != 0 && nal != g_nal);
436 /* Don't complain verbosely if we've not been passed a command
437 * name to complain about! */
438 fprintf (stderr, "Command %s not compatible with nal %s\n",
439 cmd, nal2name (g_nal));
445 sock_write (int cfd, void *buffer, int nob)
449 int rc = write (cfd, buffer, nob);
461 fprintf (stderr, "Unexpected zero sock_write\n");
466 buffer = (char *)buffer + nob;
473 sock_read (int cfd, void *buffer, int nob)
477 int rc = read (cfd, buffer, nob);
487 if (rc == 0) /* EOF */
489 errno = ECONNABORTED;
494 buffer = (char *)buffer + nob;
500 int ptl_initialize(int argc, char **argv)
502 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
507 int jt_ptl_network(int argc, char **argv)
513 (nal = ptl_name2nal (argv[1])) >= 0) {
518 fprintf(stderr, "usage: %s \n", argv[0]);
519 for (entry = nalnames; entry->name != NULL; entry++)
520 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
521 fprintf(stderr, ">\n");
526 jt_ptl_print_autoconnects (int argc, char **argv)
528 struct portals_cfg pcfg;
533 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
536 for (index = 0;;index++) {
537 PCFG_INIT (pcfg, NAL_CMD_GET_AUTOCONN);
538 pcfg.pcfg_count = index;
540 rc = pcfg_ioctl (&pcfg);
544 printf (LPX64"@%s:%d #%d buffer %d "
545 "nonagle %s affinity %s eager %s share %d\n",
546 pcfg.pcfg_nid, ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
547 pcfg.pcfg_misc, pcfg.pcfg_count, pcfg.pcfg_size,
548 (pcfg.pcfg_flags & 1) ? "on" : "off",
549 (pcfg.pcfg_flags & 2) ? "on" : "off",
550 (pcfg.pcfg_flags & 4) ? "on" : "off",
555 printf ("<no autoconnect routes>\n");
560 jt_ptl_add_autoconnect (int argc, char **argv)
562 struct portals_cfg pcfg;
566 int irq_affinity = 0;
571 if (argc < 4 || argc > 5) {
572 fprintf (stderr, "usage: %s nid ipaddr port [ise]\n", argv[0]);
576 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
579 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
580 nid == PTL_NID_ANY) {
581 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
585 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
586 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
590 if (ptl_parse_port (&port, argv[3]) != 0) {
591 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
596 char *opts = argv[4];
610 fprintf (stderr, "Can't parse options: %s\n",
616 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
619 pcfg.pcfg_misc = port;
620 /* only passing one buffer size! */
621 pcfg.pcfg_size = MAX (g_socket_rxmem, g_socket_txmem);
622 pcfg.pcfg_flags = (g_socket_nonagle ? 0x01 : 0) |
623 (irq_affinity ? 0x02 : 0) |
627 rc = pcfg_ioctl (&pcfg);
629 fprintf (stderr, "failed to enable autoconnect: %s\n",
638 jt_ptl_del_autoconnect (int argc, char **argv)
640 struct portals_cfg pcfg;
641 ptl_nid_t nid = PTL_NID_ANY;
648 fprintf (stderr, "usage: %s [nid] [ipaddr] [sk]\n",
653 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
657 ptl_parse_nid (&nid, argv[1]) != 0) {
658 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
663 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
664 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
669 char *opts = argv[3];
680 fprintf (stderr, "Can't parse flags: %s\n",
686 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
689 pcfg.pcfg_flags = (share ? 1 : 0) |
692 rc = pcfg_ioctl (&pcfg);
694 fprintf (stderr, "failed to remove autoconnect route: %s\n",
703 jt_ptl_print_connections (int argc, char **argv)
705 struct portals_cfg pcfg;
710 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
713 for (index = 0;;index++) {
714 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
715 pcfg.pcfg_count = index;
717 rc = pcfg_ioctl (&pcfg);
721 printf (LPX64"@%s:%d:%s\n",
723 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
725 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
726 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
727 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
728 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
732 printf ("<no connections>\n");
736 int jt_ptl_connect(int argc, char **argv)
738 struct portals_cfg pcfg;
739 struct sockaddr_in srvaddr;
747 int type = SOCKNAL_CONN_ANY;
753 fprintf(stderr, "usage: %s ip port [xibctr]\n", argv[0]);
757 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
760 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
762 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
766 if (ptl_parse_port (&port, argv[2]) != 0) {
767 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
772 for (flag = argv[3]; *flag != 0; flag++)
780 if (type != SOCKNAL_CONN_ANY) {
781 fprintf(stderr, "Can't flag type twice\n");
784 type = SOCKNAL_CONN_BULK_IN;
788 if (type != SOCKNAL_CONN_ANY) {
789 fprintf(stderr, "Can't flag type twice\n");
792 type = SOCKNAL_CONN_BULK_OUT;
796 if (type != SOCKNAL_CONN_ANY) {
797 fprintf(stderr, "Can't flag type twice\n");
800 type = SOCKNAL_CONN_CONTROL;
804 fprintf (stderr, "unrecognised flag '%c'\n",
809 memset(&srvaddr, 0, sizeof(srvaddr));
810 srvaddr.sin_family = AF_INET;
811 srvaddr.sin_port = htons(port);
812 srvaddr.sin_addr.s_addr = htonl(ipaddr);
814 fd = socket(PF_INET, SOCK_STREAM, 0);
816 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
820 if (g_socket_nonagle)
823 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &o, sizeof (o)) != 0) {
824 fprintf(stderr, "cannot disable nagle: %s\n", strerror(errno));
829 if (g_socket_rxmem != 0) {
831 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &o, sizeof (o)) != 0) {
832 fprintf(stderr, "cannot set receive buffer size: %s\n", strerror(errno));
837 if (g_socket_txmem != 0) {
839 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &o, sizeof (o)) != 0) {
840 fprintf(stderr, "cannot set send buffer size: %s\n", strerror(errno));
845 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
847 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
851 olen = sizeof (txmem);
852 if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, &txmem, &olen) != 0)
853 fprintf (stderr, "Can't get send buffer size: %s\n", strerror (errno));
854 olen = sizeof (rxmem);
855 if (getsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rxmem, &olen) != 0)
856 fprintf (stderr, "Can't get receive buffer size: %s\n", strerror (errno));
857 olen = sizeof (nonagle);
858 if (getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, &olen) != 0)
859 fprintf (stderr, "Can't get nagle: %s\n", strerror (errno));
861 printf("Connected host: %s snd: %d rcv: %d nagle: %s type: %s\n",
862 argv[1], txmem, rxmem, nonagle ? "Disabled" : "Enabled",
863 (type == SOCKNAL_CONN_ANY) ? "A" :
864 (type == SOCKNAL_CONN_CONTROL) ? "C" :
865 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
866 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
868 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
869 pcfg.pcfg_nal = g_nal;
871 pcfg.pcfg_flags = bind_irq;
872 pcfg.pcfg_misc = type;
874 rc = pcfg_ioctl(&pcfg);
876 fprintf(stderr, "failed to register fd with portals: %s\n",
882 printf("Connection to %s registered with socknal\n", argv[1]);
886 fprintf(stderr, "close failed: %d\n", rc);
891 int jt_ptl_disconnect(int argc, char **argv)
893 struct portals_cfg pcfg;
894 ptl_nid_t nid = PTL_NID_ANY;
899 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
903 if (!g_nal_is_compatible (NULL, SOCKNAL, 0))
907 ptl_parse_nid (&nid, argv[1]) != 0) {
908 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
913 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
914 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
918 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
920 pcfg.pcfg_id = ipaddr;
922 rc = pcfg_ioctl(&pcfg);
924 fprintf(stderr, "failed to remove connection: %s\n",
932 int jt_ptl_push_connection (int argc, char **argv)
934 struct portals_cfg pcfg;
936 ptl_nid_t nid = PTL_NID_ANY;
940 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
944 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
948 ptl_parse_nid (&nid, argv[1]) != 0) {
949 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
954 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
955 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
958 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
960 pcfg.pcfg_id = ipaddr;
962 rc = pcfg_ioctl(&pcfg);
964 fprintf(stderr, "failed to push connection: %s\n",
973 jt_ptl_print_active_txs (int argc, char **argv)
975 struct portals_cfg pcfg;
979 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
982 for (index = 0;;index++) {
983 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
984 pcfg.pcfg_count = index;
986 rc = pcfg_ioctl(&pcfg);
990 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
992 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
993 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
994 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
995 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1000 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1001 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1002 pcfg.pcfg_flags >> 2);
1006 printf ("<no active descs>\n");
1010 int jt_ptl_ping(int argc, char **argv)
1017 struct portal_ioctl_data data;
1020 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1024 if (!g_nal_is_set())
1027 if (ptl_parse_nid (&nid, argv[1]) != 0)
1029 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1035 count = atol(argv[2]);
1037 if (count < 0 || count > 20000)
1039 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1045 size= atol(argv[3]);
1048 timeout = atol (argv[4]);
1050 PORTAL_IOC_INIT (data);
1051 data.ioc_count = count;
1052 data.ioc_size = size;
1054 data.ioc_nal = g_nal;
1055 data.ioc_timeout = timeout;
1057 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1059 fprintf(stderr, "failed to start pinger: %s\n",
1066 int jt_ptl_shownid(int argc, char **argv)
1068 struct portal_ioctl_data data;
1072 fprintf(stderr, "usage: %s\n", argv[0]);
1076 if (!g_nal_is_set())
1079 PORTAL_IOC_INIT (data);
1080 data.ioc_nal = g_nal;
1081 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1083 fprintf(stderr, "getting my NID failed: %s\n",
1086 printf(LPX64"\n", data.ioc_nid);
1090 int jt_ptl_mynid(int argc, char **argv)
1093 char hostname[1024];
1095 struct portals_cfg pcfg;
1099 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1100 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1104 if (!g_nal_is_set())
1109 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1110 fprintf(stderr, "gethostname failed: %s\n",
1117 rc = ptl_parse_nid (&mynid, nidstr);
1119 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1123 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1124 pcfg.pcfg_nid = mynid;
1126 rc = pcfg_ioctl(&pcfg);
1128 fprintf(stderr, "setting my NID failed: %s\n",
1131 printf("registered my nid "LPX64" (%s)\n",
1132 ptl_nid2u64(mynid), hostname);
1137 jt_ptl_fail_nid (int argc, char **argv)
1141 unsigned int threshold;
1142 struct portal_ioctl_data data;
1144 if (argc < 2 || argc > 3)
1146 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1150 if (!g_nal_is_set())
1153 if (!strcmp (argv[1], "_all_"))
1155 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1157 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1162 threshold = PTL_MD_THRESH_INF;
1163 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1164 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1168 PORTAL_IOC_INIT (data);
1169 data.ioc_nal = g_nal;
1171 data.ioc_count = threshold;
1173 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1175 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1178 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1184 jt_ptl_rxmem (int argc, char **argv)
1190 if (ptl_parse_size (&size, argv[1]) != 0 || size < 0)
1192 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1196 g_socket_rxmem = size;
1198 printf ("Socket rmem = %d\n", g_socket_rxmem);
1203 jt_ptl_txmem (int argc, char **argv)
1209 if (ptl_parse_size (&size, argv[1]) != 0 || size < 0)
1211 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1214 g_socket_txmem = size;
1216 printf ("Socket txmem = %d\n", g_socket_txmem);
1221 jt_ptl_nagle (int argc, char **argv)
1227 if (ptl_parse_bool (&enable, argv[1]) != 0)
1229 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1232 g_socket_nonagle = !enable;
1234 printf ("Nagle %s\n", g_socket_nonagle ? "disabled" : "enabled");
1239 jt_ptl_add_route (int argc, char **argv)
1241 struct portals_cfg pcfg;
1244 ptl_nid_t gateway_nid;
1249 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1253 if (!g_nal_is_set())
1256 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1258 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1262 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1264 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1270 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1272 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1276 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1277 pcfg.pcfg_nid = gateway_nid;
1278 pcfg.pcfg_nal = ROUTER;
1279 pcfg.pcfg_gw_nal = g_nal;
1280 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1281 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1283 rc = pcfg_ioctl(&pcfg);
1286 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1294 jt_ptl_del_route (int argc, char **argv)
1296 struct portals_cfg pcfg;
1298 ptl_nid_t nid1 = PTL_NID_ANY;
1299 ptl_nid_t nid2 = PTL_NID_ANY;
1304 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1308 if (!g_nal_is_set())
1311 if (ptl_parse_nid (&nid, argv[1]) != 0)
1313 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1318 ptl_parse_nid (&nid1, argv[2]) != 0)
1320 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1327 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1328 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1333 ptl_nid_t tmp = nid1;
1340 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1341 pcfg.pcfg_nal = ROUTER;
1342 pcfg.pcfg_gw_nal = g_nal;
1343 pcfg.pcfg_nid = nid;
1344 pcfg.pcfg_nid2 = nid1;
1345 pcfg.pcfg_nid3 = nid2;
1347 rc = pcfg_ioctl(&pcfg);
1350 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1351 ptl_nid2u64(nid), strerror (errno));
1359 jt_ptl_notify_router (int argc, char **argv)
1361 struct portals_cfg pcfg;
1370 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1375 if (ptl_parse_nid (&nid, argv[1]) != 0)
1377 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1381 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1382 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1386 gettimeofday(&now, NULL);
1390 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1391 fprintf(stderr, "Can't parse time %s\n"
1392 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1393 "or an absolute unix time in seconds\n", argv[3]);
1395 } else if (when > now.tv_sec) {
1396 fprintf (stderr, "%s specifies a time in the future\n",
1401 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1402 pcfg.pcfg_nal = ROUTER;
1403 pcfg.pcfg_gw_nal = g_nal;
1404 pcfg.pcfg_nid = nid;
1405 pcfg.pcfg_flags = enable;
1406 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1407 pcfg.pcfg_nid3 = (__u64)when;
1409 rc = pcfg_ioctl(&pcfg);
1412 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1413 ptl_nid2u64(nid), strerror (errno));
1421 jt_ptl_print_routes (int argc, char **argv)
1423 char buffer[3][128];
1424 struct portals_cfg pcfg;
1428 ptl_nid_t gateway_nid;
1433 for (index = 0;;index++)
1435 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1436 pcfg.pcfg_nal = ROUTER;
1437 pcfg.pcfg_count = index;
1439 rc = pcfg_ioctl(&pcfg);
1443 gateway_nal = pcfg.pcfg_gw_nal;
1444 gateway_nid = pcfg.pcfg_nid;
1445 nid1 = pcfg.pcfg_nid2;
1446 nid2 = pcfg.pcfg_nid3;
1447 alive = pcfg.pcfg_flags;
1449 printf ("%8s %18s : %s - %s, %s\n",
1450 nal2name (gateway_nal),
1451 ptl_nid2str (buffer[0], gateway_nid),
1452 ptl_nid2str (buffer[1], nid1),
1453 ptl_nid2str (buffer[2], nid2),
1454 alive ? "up" : "down");
1460 lwt_control(int enable, int clear)
1462 struct portal_ioctl_data data;
1465 PORTAL_IOC_INIT(data);
1466 data.ioc_flags = enable;
1467 data.ioc_misc = clear;
1469 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1473 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1479 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1480 lwt_event_t *events, int size)
1482 struct portal_ioctl_data data;
1485 PORTAL_IOC_INIT(data);
1486 data.ioc_pbuf1 = (char *)events;
1487 data.ioc_plen1 = size;
1489 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1491 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1496 LASSERT (data.ioc_count != 0);
1497 LASSERT (data.ioc_misc != 0);
1500 *now = data.ioc_nid;
1503 *ncpu = data.ioc_count;
1505 if (totalsize != NULL)
1506 *totalsize = data.ioc_misc;
1512 lwt_get_string(char *kstr)
1515 struct portal_ioctl_data data;
1519 /* FIXME: this could maintain a symbol table since we expect to be
1520 * looking up the same strings all the time... */
1522 PORTAL_IOC_INIT(data);
1523 data.ioc_pbuf1 = kstr;
1524 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1525 data.ioc_pbuf2 = NULL;
1528 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1530 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1535 size = data.ioc_count;
1536 ustr = (char *)malloc(size);
1538 fprintf(stderr, "Can't allocate string storage of size %d\n",
1543 PORTAL_IOC_INIT(data);
1544 data.ioc_pbuf1 = kstr;
1545 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1546 data.ioc_pbuf2 = ustr;
1547 data.ioc_plen2 = size;
1549 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1551 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1556 LASSERT(strlen(ustr) == size - 1);
1561 lwt_put_string(char *ustr)
1567 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1570 char *where = lwt_get_string(e->lwte_where);
1575 sprintf(whenstr, LPD64, e->lwte_when - t0);
1577 fprintf(f, "%#010lx %#010lx %#010lx %#010lx: %#010lx %1d %10.6f %10.2f %s\n",
1578 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1579 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1580 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1583 lwt_put_string(where);
1589 get_cycles_per_usec ()
1591 FILE *f = fopen ("/proc/cpuinfo", "r");
1596 while (fgets (line, sizeof (line), f) != NULL)
1597 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1604 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1609 jt_ptl_lwt(int argc, char **argv)
1613 int nevents_per_cpu;
1614 lwt_event_t *events;
1615 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1616 lwt_event_t *next_event[LWT_MAX_CPUS];
1617 lwt_event_t *first_event[LWT_MAX_CPUS];
1626 struct timeval tvnow;
1627 int printed_date = 0;
1631 (strcmp(argv[1], "start") &&
1632 strcmp(argv[1], "stop"))) {
1635 " %s stop [fname]\n", argv[0], argv[0]);
1639 if (!strcmp(argv[1], "start")) {
1641 if (lwt_control(0, 0) != 0)
1645 if (lwt_control(0, 1) != 0)
1649 if (lwt_control(1, 0) != 0)
1655 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1658 if (ncpus > LWT_MAX_CPUS) {
1659 fprintf(stderr, "Too many cpus: %d (%d)\n",
1660 ncpus, LWT_MAX_CPUS);
1664 events = (lwt_event_t *)malloc(totalspace);
1665 if (events == NULL) {
1666 fprintf(stderr, "Can't allocate %d\n", totalspace);
1670 if (lwt_control(0, 0) != 0) { /* disable */
1675 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1680 /* we want this time to be sampled at snapshot time */
1681 gettimeofday(&tvnow, NULL);
1684 f = fopen (argv[2], "w");
1686 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1692 mhz = get_cycles_per_usec();
1694 /* carve events into per-cpu slices */
1695 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1696 for (cpu = 0; cpu <= ncpus; cpu++)
1697 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1699 /* find the earliest event on each cpu */
1700 for (cpu = 0; cpu < ncpus; cpu++) {
1701 first_event[cpu] = NULL;
1703 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1705 if (e->lwte_where == NULL) /* not an event */
1708 if (first_event[cpu] == NULL ||
1709 first_event[cpu]->lwte_when > e->lwte_when)
1710 first_event[cpu] = e;
1713 next_event[cpu] = first_event[cpu];
1717 for (cpu = 0; cpu < ncpus; cpu++) {
1718 e = first_event[cpu];
1719 if (e == NULL) /* no events this cpu */
1722 if (e == cpu_event[cpu])
1723 e = cpu_event[cpu + 1] - 1;
1727 /* If there's an event immediately before the first one, this
1728 * cpu wrapped its event buffer */
1729 if (e->lwte_where == NULL)
1732 /* We should only start outputting events from the most recent
1733 * first event in any wrapped cpu. Events before this time on
1734 * other cpus won't have any events from this CPU to interleave
1736 if (t0 < first_event[cpu]->lwte_when)
1737 t0 = first_event[cpu]->lwte_when;
1741 /* find which cpu has the next event */
1743 for (i = 0; i < ncpus; i++) {
1745 if (next_event[i] == NULL) /* this cpu exhausted */
1749 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1753 if (cpu < 0) /* all cpus exhausted */
1757 /* no wrapped cpus and this is he first ever event */
1758 t0 = next_event[cpu]->lwte_when;
1761 if (t0 <= next_event[cpu]->lwte_when) {
1762 /* on or after the first event */
1763 if (!printed_date) {
1764 cycles_t du = (tnow - t0) / mhz;
1765 time_t then = tvnow.tv_sec - du/1000000;
1767 if (du % 1000000 > tvnow.tv_usec)
1770 fprintf(f, "%s", ctime(&then));
1774 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1779 tlast = next_event[cpu]->lwte_when;
1782 if (next_event[cpu] == cpu_event[cpu + 1])
1783 next_event[cpu] = cpu_event[cpu];
1785 if (next_event[cpu]->lwte_where == NULL ||
1786 next_event[cpu] == first_event[cpu])
1787 next_event[cpu] = NULL;
1797 int jt_ptl_memhog(int argc, char **argv)
1799 static int gfp = 0; /* sticky! */
1801 struct portal_ioctl_data data;
1807 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1811 count = strtol(argv[1], &end, 0);
1812 if (count < 0 || *end != 0) {
1813 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1818 rc = strtol(argv[2], &end, 0);
1820 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1826 PORTAL_IOC_INIT(data);
1827 data.ioc_count = count;
1828 data.ioc_flags = gfp;
1829 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1832 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1836 printf("memhog %d OK\n", count);