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[] = {
87 static cfg_record_cb_t g_record_cb;
89 /* Convert a string boolean to an int; "enable" -> 1 */
90 int ptl_parse_bool (int *b, char *str) {
91 if (!strcasecmp (str, "no") ||
92 !strcasecmp (str, "n") ||
93 !strcasecmp (str, "off") ||
94 !strcasecmp (str, "down") ||
95 !strcasecmp (str, "disable"))
101 if (!strcasecmp (str, "yes") ||
102 !strcasecmp (str, "y") ||
103 !strcasecmp (str, "on") ||
104 !strcasecmp (str, "up") ||
105 !strcasecmp (str, "enable"))
114 /* Convert human readable size string to and int; "1k" -> 1000 */
115 int ptl_parse_size (int *sizep, char *str) {
119 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
152 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
159 pcfg_ioctl(struct portals_cfg *pcfg)
163 if (pcfg->pcfg_nal ==0)
164 pcfg->pcfg_nal = g_nal;
167 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
169 struct portal_ioctl_data data;
170 PORTAL_IOC_INIT (data);
171 data.ioc_pbuf1 = (char*)pcfg;
172 data.ioc_plen1 = sizeof(*pcfg);
173 /* XXX liblustre hack XXX */
174 data.ioc_nal_cmd = pcfg->pcfg_command;
175 data.ioc_nid = pcfg->pcfg_nid;
177 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
186 name2num_lookup_name (name2num_t *table, char *str)
188 while (table->name != NULL)
189 if (!strcmp (str, table->name))
197 name2num_lookup_num (name2num_t *table, int num)
199 while (table->name != NULL)
200 if (num == table->num)
208 ptl_name2nal (char *str)
210 name2num_t *e = name2num_lookup_name (nalnames, str);
212 return ((e == NULL) ? -1 : e->num);
218 name2num_t *e = name2num_lookup_num (nalnames, nal);
220 return ((e == NULL) ? "???" : e->name);
223 static struct hostent *
224 ptl_gethostbyname(char * hname) {
226 he = gethostbyname(hname);
231 fprintf(stderr, "Unable to resolve hostname: %s\n",
235 fprintf(stderr, "gethostbyname error: %s\n",
245 ptl_parse_port (int *port, char *str)
249 *port = strtol (str, &end, 0);
251 if (*end == 0 && /* parsed whole string */
252 *port > 0 && *port < 65536) /* minimal sanity check */
259 ptl_parse_time (time_t *t, char *str)
265 *t = strtol (str, &end, 0);
266 if (*end == 0) /* parsed whole string */
269 memset (&tm, 0, sizeof (tm));
270 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
271 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
272 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
276 tm.tm_mon--; /* convert to 0 == Jan */
277 tm.tm_year -= 1900; /* y2k quirk */
278 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
281 if (*t == (time_t)-1)
288 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
296 if (!strcmp (str, "_all_"))
302 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
303 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
304 (c & ~0xff) == 0 && (d & ~0xff) == 0)
306 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
310 if ((('a' <= str[0] && str[0] <= 'z') ||
311 ('A' <= str[0] && str[0] <= 'Z')) &&
312 (he = ptl_gethostbyname (str)) != NULL)
314 __u32 addr = *(__u32 *)he->h_addr;
316 *ipaddrp = ntohl(addr); /* HOST byte order */
324 ptl_ipaddr_2_str (__u32 ipaddr, char *str)
329 net_ip = htonl (ipaddr);
330 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
334 sprintf (str, "%d.%d.%d.%d",
335 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
336 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
341 ptl_parse_nid (ptl_nid_t *nidp, char *str)
345 unsigned long long ullval;
347 if (!strcmp (str, "_all_")) {
352 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
353 *nidp = (ptl_nid_t)ipaddr;
357 ullval = strtoull(str, &end, 0);
359 /* parsed whole string */
360 *nidp = (ptl_nid_t)ullval;
367 __u64 ptl_nid2u64(ptl_nid_t nid)
369 switch (sizeof (nid)) {
375 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
383 ptl_nid2str (char *buffer, ptl_nid_t nid)
385 __u64 nid64 = ptl_nid2u64(nid);
388 if ((nid64 & ~((__u64)((__u32)-1))) != 0) {
392 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
394 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
398 strcpy (buffer, he->h_name);
400 sprintf (buffer, LPX64, nid64);
408 fprintf (stderr, "Error: you must run the 'network' command first.\n");
415 int g_nal_is_compatible (char *cmd, ...)
420 if (!g_nal_is_set ())
426 nal = va_arg (ap, int);
427 } while (nal != 0 && nal != g_nal);
435 /* Don't complain verbosely if we've not been passed a command
436 * name to complain about! */
437 fprintf (stderr, "Command %s not compatible with nal %s\n",
438 cmd, nal2name (g_nal));
444 sock_write (int cfd, void *buffer, int nob)
448 int rc = write (cfd, buffer, nob);
460 fprintf (stderr, "Unexpected zero sock_write\n");
465 buffer = (char *)buffer + nob;
472 sock_read (int cfd, void *buffer, int nob)
476 int rc = read (cfd, buffer, nob);
486 if (rc == 0) /* EOF */
488 errno = ECONNABORTED;
493 buffer = (char *)buffer + nob;
499 int ptl_initialize(int argc, char **argv)
501 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
506 int jt_ptl_network(int argc, char **argv)
512 (nal = ptl_name2nal (argv[1])) >= 0) {
517 fprintf(stderr, "usage: %s \n", argv[0]);
518 for (entry = nalnames; entry->name != NULL; entry++)
519 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
520 fprintf(stderr, ">\n");
525 jt_ptl_print_autoconnects (int argc, char **argv)
527 struct portals_cfg pcfg;
532 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
535 for (index = 0;;index++) {
536 PCFG_INIT (pcfg, NAL_CMD_GET_AUTOCONN);
537 pcfg.pcfg_count = index;
539 rc = pcfg_ioctl (&pcfg);
543 printf (LPX64"@%s:%d #%d buffer %d "
544 "nonagle %s affinity %s eager %s share %d\n",
545 pcfg.pcfg_nid, ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
546 pcfg.pcfg_misc, pcfg.pcfg_count, pcfg.pcfg_size,
547 (pcfg.pcfg_flags & 1) ? "on" : "off",
548 (pcfg.pcfg_flags & 2) ? "on" : "off",
549 (pcfg.pcfg_flags & 4) ? "on" : "off",
554 printf ("<no autoconnect routes>\n");
559 jt_ptl_add_autoconnect (int argc, char **argv)
561 struct portals_cfg pcfg;
565 int irq_affinity = 0;
570 if (argc < 4 || argc > 5) {
571 fprintf (stderr, "usage: %s nid ipaddr port [ise]\n", argv[0]);
575 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
578 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
579 nid == PTL_NID_ANY) {
580 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
584 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
585 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
589 if (ptl_parse_port (&port, argv[3]) != 0) {
590 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
595 char *opts = argv[4];
609 fprintf (stderr, "Can't parse options: %s\n",
615 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
618 pcfg.pcfg_misc = port;
619 /* only passing one buffer size! */
620 pcfg.pcfg_size = MAX (g_socket_rxmem, g_socket_txmem);
621 pcfg.pcfg_flags = (g_socket_nonagle ? 0x01 : 0) |
622 (irq_affinity ? 0x02 : 0) |
626 rc = pcfg_ioctl (&pcfg);
628 fprintf (stderr, "failed to enable autoconnect: %s\n",
637 jt_ptl_del_autoconnect (int argc, char **argv)
639 struct portals_cfg pcfg;
640 ptl_nid_t nid = PTL_NID_ANY;
647 fprintf (stderr, "usage: %s [nid] [ipaddr] [sk]\n",
652 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
656 ptl_parse_nid (&nid, argv[1]) != 0) {
657 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
662 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
663 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
668 char *opts = argv[3];
679 fprintf (stderr, "Can't parse flags: %s\n",
685 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
688 pcfg.pcfg_flags = (share ? 1 : 0) |
691 rc = pcfg_ioctl (&pcfg);
693 fprintf (stderr, "failed to remove autoconnect route: %s\n",
702 jt_ptl_print_connections (int argc, char **argv)
704 struct portals_cfg pcfg;
709 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
712 for (index = 0;;index++) {
713 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
714 pcfg.pcfg_count = index;
716 rc = pcfg_ioctl (&pcfg);
720 printf (LPX64"@%s:%d:%s\n",
722 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer),
724 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
725 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
726 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
727 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
731 printf ("<no connections>\n");
735 int jt_ptl_connect(int argc, char **argv)
737 struct portals_cfg pcfg;
738 struct sockaddr_in srvaddr;
746 int type = SOCKNAL_CONN_ANY;
752 fprintf(stderr, "usage: %s ip port [xibctr]\n", argv[0]);
756 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
759 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
761 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
765 if (ptl_parse_port (&port, argv[2]) != 0) {
766 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
771 for (flag = argv[3]; *flag != 0; flag++)
779 if (type != SOCKNAL_CONN_ANY) {
780 fprintf(stderr, "Can't flag type twice\n");
783 type = SOCKNAL_CONN_BULK_IN;
787 if (type != SOCKNAL_CONN_ANY) {
788 fprintf(stderr, "Can't flag type twice\n");
791 type = SOCKNAL_CONN_BULK_OUT;
795 if (type != SOCKNAL_CONN_ANY) {
796 fprintf(stderr, "Can't flag type twice\n");
799 type = SOCKNAL_CONN_CONTROL;
803 fprintf (stderr, "unrecognised flag '%c'\n",
808 memset(&srvaddr, 0, sizeof(srvaddr));
809 srvaddr.sin_family = AF_INET;
810 srvaddr.sin_port = htons(port);
811 srvaddr.sin_addr.s_addr = htonl(ipaddr);
813 fd = socket(PF_INET, SOCK_STREAM, 0);
815 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
819 if (g_socket_nonagle)
822 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &o, sizeof (o)) != 0) {
823 fprintf(stderr, "cannot disable nagle: %s\n", strerror(errno));
828 if (g_socket_rxmem != 0) {
830 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &o, sizeof (o)) != 0) {
831 fprintf(stderr, "cannot set receive buffer size: %s\n", strerror(errno));
836 if (g_socket_txmem != 0) {
838 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &o, sizeof (o)) != 0) {
839 fprintf(stderr, "cannot set send buffer size: %s\n", strerror(errno));
844 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
846 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
850 olen = sizeof (txmem);
851 if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, &txmem, &olen) != 0)
852 fprintf (stderr, "Can't get send buffer size: %s\n", strerror (errno));
853 olen = sizeof (rxmem);
854 if (getsockopt (fd, SOL_SOCKET, SO_RCVBUF, &rxmem, &olen) != 0)
855 fprintf (stderr, "Can't get receive buffer size: %s\n", strerror (errno));
856 olen = sizeof (nonagle);
857 if (getsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &nonagle, &olen) != 0)
858 fprintf (stderr, "Can't get nagle: %s\n", strerror (errno));
860 printf("Connected host: %s snd: %d rcv: %d nagle: %s type: %s\n",
861 argv[1], txmem, rxmem, nonagle ? "Disabled" : "Enabled",
862 (type == SOCKNAL_CONN_ANY) ? "A" :
863 (type == SOCKNAL_CONN_CONTROL) ? "C" :
864 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
865 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
867 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
868 pcfg.pcfg_nal = g_nal;
870 pcfg.pcfg_flags = bind_irq;
871 pcfg.pcfg_misc = type;
873 rc = pcfg_ioctl(&pcfg);
875 fprintf(stderr, "failed to register fd with portals: %s\n",
881 printf("Connection to %s registered with socknal\n", argv[1]);
885 fprintf(stderr, "close failed: %d\n", rc);
890 int jt_ptl_disconnect(int argc, char **argv)
892 struct portals_cfg pcfg;
893 ptl_nid_t nid = PTL_NID_ANY;
898 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
902 if (!g_nal_is_compatible (NULL, SOCKNAL, 0))
906 ptl_parse_nid (&nid, argv[1]) != 0) {
907 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
912 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
913 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
917 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
919 pcfg.pcfg_id = ipaddr;
921 rc = pcfg_ioctl(&pcfg);
923 fprintf(stderr, "failed to remove connection: %s\n",
931 int jt_ptl_push_connection (int argc, char **argv)
933 struct portals_cfg pcfg;
935 ptl_nid_t nid = PTL_NID_ANY;
939 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
943 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
947 ptl_parse_nid (&nid, argv[1]) != 0) {
948 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
953 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
954 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
957 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
959 pcfg.pcfg_id = ipaddr;
961 rc = pcfg_ioctl(&pcfg);
963 fprintf(stderr, "failed to push connection: %s\n",
972 jt_ptl_print_active_txs (int argc, char **argv)
974 struct portals_cfg pcfg;
978 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
981 for (index = 0;;index++) {
982 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
983 pcfg.pcfg_count = index;
985 rc = pcfg_ioctl(&pcfg);
989 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
991 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
992 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
993 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
994 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
999 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1000 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1001 pcfg.pcfg_flags >> 2);
1005 printf ("<no active descs>\n");
1009 int jt_ptl_ping(int argc, char **argv)
1016 struct portal_ioctl_data data;
1019 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1023 if (!g_nal_is_set())
1026 if (ptl_parse_nid (&nid, argv[1]) != 0)
1028 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1034 count = atol(argv[2]);
1036 if (count < 0 || count > 20000)
1038 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1044 size= atol(argv[3]);
1047 timeout = atol (argv[4]);
1049 PORTAL_IOC_INIT (data);
1050 data.ioc_count = count;
1051 data.ioc_size = size;
1053 data.ioc_nal = g_nal;
1054 data.ioc_timeout = timeout;
1056 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1058 fprintf(stderr, "failed to start pinger: %s\n",
1065 int jt_ptl_shownid(int argc, char **argv)
1067 struct portal_ioctl_data data;
1071 fprintf(stderr, "usage: %s\n", argv[0]);
1075 if (!g_nal_is_set())
1078 PORTAL_IOC_INIT (data);
1079 data.ioc_nal = g_nal;
1080 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1082 fprintf(stderr, "getting my NID failed: %s\n",
1085 printf(LPX64"\n", data.ioc_nid);
1089 int jt_ptl_mynid(int argc, char **argv)
1092 char hostname[1024];
1094 struct portals_cfg pcfg;
1098 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1099 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1103 if (!g_nal_is_set())
1108 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1109 fprintf(stderr, "gethostname failed: %s\n",
1116 rc = ptl_parse_nid (&mynid, nidstr);
1118 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1122 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1123 pcfg.pcfg_nid = mynid;
1125 rc = pcfg_ioctl(&pcfg);
1127 fprintf(stderr, "setting my NID failed: %s\n",
1130 printf("registered my nid "LPX64" (%s)\n",
1131 ptl_nid2u64(mynid), hostname);
1136 jt_ptl_fail_nid (int argc, char **argv)
1140 unsigned int threshold;
1141 struct portal_ioctl_data data;
1143 if (argc < 2 || argc > 3)
1145 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1149 if (!g_nal_is_set())
1152 if (!strcmp (argv[1], "_all_"))
1154 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1156 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1161 threshold = PTL_MD_THRESH_INF;
1162 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1163 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1167 PORTAL_IOC_INIT (data);
1168 data.ioc_nal = g_nal;
1170 data.ioc_count = threshold;
1172 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1174 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1177 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1183 jt_ptl_rxmem (int argc, char **argv)
1189 if (ptl_parse_size (&size, argv[1]) != 0 || size < 0)
1191 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1195 g_socket_rxmem = size;
1197 printf ("Socket rmem = %d\n", g_socket_rxmem);
1202 jt_ptl_txmem (int argc, char **argv)
1208 if (ptl_parse_size (&size, argv[1]) != 0 || size < 0)
1210 fprintf (stderr, "Can't parse size %s\n", argv[1]);
1213 g_socket_txmem = size;
1215 printf ("Socket txmem = %d\n", g_socket_txmem);
1220 jt_ptl_nagle (int argc, char **argv)
1226 if (ptl_parse_bool (&enable, argv[1]) != 0)
1228 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1231 g_socket_nonagle = !enable;
1233 printf ("Nagle %s\n", g_socket_nonagle ? "disabled" : "enabled");
1238 jt_ptl_add_route (int argc, char **argv)
1240 struct portals_cfg pcfg;
1243 ptl_nid_t gateway_nid;
1248 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1252 if (!g_nal_is_set())
1255 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1257 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1261 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1263 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1269 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1271 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1275 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1276 pcfg.pcfg_nid = gateway_nid;
1277 pcfg.pcfg_nal = ROUTER;
1278 pcfg.pcfg_gw_nal = g_nal;
1279 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1280 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1282 rc = pcfg_ioctl(&pcfg);
1285 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1293 jt_ptl_del_route (int argc, char **argv)
1295 struct portals_cfg pcfg;
1297 ptl_nid_t nid1 = PTL_NID_ANY;
1298 ptl_nid_t nid2 = PTL_NID_ANY;
1303 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1307 if (!g_nal_is_set())
1310 if (ptl_parse_nid (&nid, argv[1]) != 0)
1312 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1317 ptl_parse_nid (&nid1, argv[2]) != 0)
1319 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1326 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1327 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1332 ptl_nid_t tmp = nid1;
1339 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1340 pcfg.pcfg_nal = ROUTER;
1341 pcfg.pcfg_gw_nal = g_nal;
1342 pcfg.pcfg_nid = nid;
1343 pcfg.pcfg_nid2 = nid1;
1344 pcfg.pcfg_nid3 = nid2;
1346 rc = pcfg_ioctl(&pcfg);
1349 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1350 ptl_nid2u64(nid), strerror (errno));
1358 jt_ptl_notify_router (int argc, char **argv)
1360 struct portals_cfg pcfg;
1369 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1374 if (ptl_parse_nid (&nid, argv[1]) != 0)
1376 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1380 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1381 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1385 gettimeofday(&now, NULL);
1389 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1390 fprintf(stderr, "Can't parse time %s\n"
1391 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1392 "or an absolute unix time in seconds\n", argv[3]);
1394 } else if (when > now.tv_sec) {
1395 fprintf (stderr, "%s specifies a time in the future\n",
1400 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1401 pcfg.pcfg_nal = ROUTER;
1402 pcfg.pcfg_gw_nal = g_nal;
1403 pcfg.pcfg_nid = nid;
1404 pcfg.pcfg_flags = enable;
1405 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1406 pcfg.pcfg_nid3 = (__u64)when;
1408 rc = pcfg_ioctl(&pcfg);
1411 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1412 ptl_nid2u64(nid), strerror (errno));
1420 jt_ptl_print_routes (int argc, char **argv)
1422 char buffer[3][128];
1423 struct portals_cfg pcfg;
1427 ptl_nid_t gateway_nid;
1432 for (index = 0;;index++)
1434 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1435 pcfg.pcfg_nal = ROUTER;
1436 pcfg.pcfg_count = index;
1438 rc = pcfg_ioctl(&pcfg);
1442 gateway_nal = pcfg.pcfg_gw_nal;
1443 gateway_nid = pcfg.pcfg_nid;
1444 nid1 = pcfg.pcfg_nid2;
1445 nid2 = pcfg.pcfg_nid3;
1446 alive = pcfg.pcfg_flags;
1448 printf ("%8s %18s : %s - %s, %s\n",
1449 nal2name (gateway_nal),
1450 ptl_nid2str (buffer[0], gateway_nid),
1451 ptl_nid2str (buffer[1], nid1),
1452 ptl_nid2str (buffer[2], nid2),
1453 alive ? "up" : "down");
1459 lwt_control(int enable, int clear)
1461 struct portal_ioctl_data data;
1464 PORTAL_IOC_INIT(data);
1465 data.ioc_flags = enable;
1466 data.ioc_misc = clear;
1468 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1472 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1478 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1479 lwt_event_t *events, int size)
1481 struct portal_ioctl_data data;
1484 PORTAL_IOC_INIT(data);
1485 data.ioc_pbuf1 = (char *)events;
1486 data.ioc_plen1 = size;
1488 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1490 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1495 LASSERT (data.ioc_count != 0);
1496 LASSERT (data.ioc_misc != 0);
1499 *now = data.ioc_nid;
1502 *ncpu = data.ioc_count;
1504 if (totalsize != NULL)
1505 *totalsize = data.ioc_misc;
1511 lwt_get_string(char *kstr)
1514 struct portal_ioctl_data data;
1518 /* FIXME: this could maintain a symbol table since we expect to be
1519 * looking up the same strings all the time... */
1521 PORTAL_IOC_INIT(data);
1522 data.ioc_pbuf1 = kstr;
1523 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1524 data.ioc_pbuf2 = NULL;
1527 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1529 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1534 size = data.ioc_count;
1535 ustr = (char *)malloc(size);
1537 fprintf(stderr, "Can't allocate string storage of size %d\n",
1542 PORTAL_IOC_INIT(data);
1543 data.ioc_pbuf1 = kstr;
1544 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1545 data.ioc_pbuf2 = ustr;
1546 data.ioc_plen2 = size;
1548 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1550 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1555 LASSERT(strlen(ustr) == size - 1);
1560 lwt_put_string(char *ustr)
1566 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1568 char *where = lwt_get_string(e->lwte_where);
1573 fprintf(f, "%#010lx %#010lx %#010lx %#010lx: %#010lx %1d %10.6f %10.2f %s\n",
1574 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1575 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1576 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1579 lwt_put_string(where);
1585 get_cycles_per_usec ()
1587 FILE *f = fopen ("/proc/cpuinfo", "r");
1592 while (fgets (line, sizeof (line), f) != NULL)
1593 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1600 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1605 jt_ptl_lwt(int argc, char **argv)
1609 int nevents_per_cpu;
1610 lwt_event_t *events;
1611 lwt_event_t *cpu_event[LWT_MAX_CPUS + 1];
1612 lwt_event_t *next_event[LWT_MAX_CPUS];
1613 lwt_event_t *first_event[LWT_MAX_CPUS];
1622 struct timeval tvnow;
1623 int printed_date = 0;
1628 (strcmp(argv[1], "start") &&
1629 strcmp(argv[1], "stop"))) {
1632 " %s stop [fname]\n", argv[0], argv[0]);
1636 if (!strcmp(argv[1], "start")) {
1638 if (lwt_control(0, 0) != 0)
1642 if (lwt_control(0, 1) != 0)
1646 if (lwt_control(1, 0) != 0)
1652 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1655 if (ncpus > LWT_MAX_CPUS) {
1656 fprintf(stderr, "Too many cpus: %d (%d)\n",
1657 ncpus, LWT_MAX_CPUS);
1661 events = (lwt_event_t *)malloc(totalspace);
1662 if (events == NULL) {
1663 fprintf(stderr, "Can't allocate %d\n", totalspace);
1667 if (lwt_control(0, 0) != 0) { /* disable */
1672 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1677 /* we want this time to be sampled at snapshot time */
1678 gettimeofday(&tvnow, NULL);
1681 f = fopen (argv[2], "w");
1683 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1689 mhz = get_cycles_per_usec();
1691 /* carve events into per-cpu slices */
1692 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1693 for (cpu = 0; cpu <= ncpus; cpu++)
1694 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1696 /* find the earliest event on each cpu */
1697 for (cpu = 0; cpu < ncpus; cpu++) {
1698 first_event[cpu] = NULL;
1700 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1702 if (e->lwte_where == NULL) /* not an event */
1705 if (first_event[cpu] == NULL ||
1706 first_event[cpu]->lwte_when > e->lwte_when)
1707 first_event[cpu] = e;
1710 next_event[cpu] = first_event[cpu];
1714 for (cpu = 0; cpu < ncpus; cpu++) {
1715 e = first_event[cpu];
1716 if (e == NULL) /* no events this cpu */
1719 if (e == cpu_event[cpu])
1720 e = cpu_event[cpu + 1] - 1;
1724 /* If there's an event immediately before the first one, this
1725 * cpu wrapped its event buffer */
1726 if (e->lwte_where == NULL)
1729 /* We should only start outputting events from the most recent
1730 * first event in any wrapped cpu. Events before this time on
1731 * other cpus won't have any events from this CPU to interleave
1733 if (t0 < first_event[cpu]->lwte_when)
1734 t0 = first_event[cpu]->lwte_when;
1738 /* find which cpu has the next event */
1740 for (i = 0; i < ncpus; i++) {
1742 if (next_event[i] == NULL) /* this cpu exhausted */
1746 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1750 if (cpu < 0) /* all cpus exhausted */
1754 /* no wrapped cpus and this is he first ever event */
1755 t0 = next_event[cpu]->lwte_when;
1758 if (t0 <= next_event[cpu]->lwte_when) {
1759 /* on or after the first event */
1760 if (!printed_date) {
1761 cycles_t du = (tnow - t0) / mhz;
1762 time_t then = tvnow.tv_sec - du/1000000;
1764 if (du % 1000000 > tvnow.tv_usec)
1767 fprintf(f, "%s", ctime(&then));
1771 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1775 if (++nlines % 10000 == 0 && f != stdout) {
1776 /* show some activity... */
1782 tlast = next_event[cpu]->lwte_when;
1785 if (next_event[cpu] == cpu_event[cpu + 1])
1786 next_event[cpu] = cpu_event[cpu];
1788 if (next_event[cpu]->lwte_where == NULL ||
1789 next_event[cpu] == first_event[cpu])
1790 next_event[cpu] = NULL;
1802 int jt_ptl_memhog(int argc, char **argv)
1804 static int gfp = 0; /* sticky! */
1806 struct portal_ioctl_data data;
1812 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1816 count = strtol(argv[1], &end, 0);
1817 if (count < 0 || *end != 0) {
1818 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1823 rc = strtol(argv[2], &end, 0);
1825 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1831 PORTAL_IOC_INIT(data);
1832 data.ioc_count = count;
1833 data.ioc_flags = gfp;
1834 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1837 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1841 printf("memhog %d OK\n", count);