1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Portals, http://www.sf.net/projects/lustre/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <sys/types.h>
28 #include <sys/socket.h>
29 #ifdef HAVE_NETINET_TCP_H
30 #include <netinet/tcp.h>
35 #ifdef HAVE_SYS_IOCTL_H
36 #include <sys/ioctl.h>
52 #include <portals/ipmap.h>
55 #include <libcfs/portals_utils.h>
56 #include <portals/api-support.h>
57 #include <portals/ptlctl.h>
58 #include <portals/lib-types.h>
59 #include <portals/socknal.h>
62 unsigned int portal_debug;
63 unsigned int portal_printk;
65 static unsigned int g_nal = 0;
73 static name2num_t nalnames[] = {
79 {"openib", OPENIBNAL},
85 {"cray_kern_nal", CRAY_KERN_NAL},
86 {"cray_user_nal", CRAY_USER_NAL},
87 {"cray_qk_nal", CRAY_QK_NAL},
92 static cfg_record_cb_t g_record_cb;
94 /* Convert a string boolean to an int; "enable" -> 1 */
95 int ptl_parse_bool (int *b, char *str) {
96 if (!strcasecmp (str, "no") ||
97 !strcasecmp (str, "n") ||
98 !strcasecmp (str, "off") ||
99 !strcasecmp (str, "down") ||
100 !strcasecmp (str, "disable"))
106 if (!strcasecmp (str, "yes") ||
107 !strcasecmp (str, "y") ||
108 !strcasecmp (str, "on") ||
109 !strcasecmp (str, "up") ||
110 !strcasecmp (str, "enable"))
119 /* Convert human readable size string to and int; "1k" -> 1000 */
120 int ptl_parse_size (int *sizep, char *str) {
124 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
157 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
164 pcfg_ioctl(struct portals_cfg *pcfg)
168 if (pcfg->pcfg_nal ==0)
169 pcfg->pcfg_nal = g_nal;
172 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
174 struct portal_ioctl_data data;
175 PORTAL_IOC_INIT (data);
176 data.ioc_pbuf1 = (char*)pcfg;
177 data.ioc_plen1 = sizeof(*pcfg);
178 /* XXX liblustre hack XXX */
179 data.ioc_nal_cmd = pcfg->pcfg_command;
180 data.ioc_nid = pcfg->pcfg_nid;
182 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
184 if (rc == 0 && pcfg->pcfg_version != PORTALS_CFG_VERSION)
194 name2num_lookup_name (name2num_t *table, char *str)
196 while (table->name != NULL)
197 if (!strcmp (str, table->name))
205 name2num_lookup_num (name2num_t *table, int num)
207 while (table->name != NULL)
208 if (num == table->num)
216 ptl_name2nal (char *str)
218 name2num_t *e = name2num_lookup_name (nalnames, str);
220 return ((e == NULL) ? -1 : e->num);
226 name2num_t *e = name2num_lookup_num (nalnames, nal);
228 return ((e == NULL) ? "???" : e->name);
231 #ifdef HAVE_GETHOSTBYNAME
232 static struct hostent *
233 ptl_gethostbyname(char * hname) {
235 he = gethostbyname(hname);
240 fprintf(stderr, "Unable to resolve hostname: %s\n",
244 fprintf(stderr, "gethostbyname error for %s: %s\n",
245 hname, strerror(h_errno));
255 ptl_parse_port (int *port, char *str)
259 *port = strtol (str, &end, 0);
261 if (*end == 0 && /* parsed whole string */
262 *port > 0 && *port < 65536) /* minimal sanity check */
269 ptl_parse_time (time_t *t, char *str)
275 *t = strtol (str, &end, 0);
276 if (*end == 0) /* parsed whole string */
279 memset (&tm, 0, sizeof (tm));
280 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
281 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
282 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
286 tm.tm_mon--; /* convert to 0 == Jan */
287 tm.tm_year -= 1900; /* y2k quirk */
288 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
291 if (*t == (time_t)-1)
298 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
305 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
306 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
307 (c & ~0xff) == 0 && (d & ~0xff) == 0)
309 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
317 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
319 #ifdef HAVE_GETHOSTBYNAME
323 if (!strcmp (str, "_all_")) {
328 if (ptl_parse_ipquad(ipaddrp, str) == 0)
331 #ifdef HAVE_GETHOSTBYNAME
332 if ((('a' <= str[0] && str[0] <= 'z') ||
333 ('A' <= str[0] && str[0] <= 'Z')) &&
334 (he = ptl_gethostbyname (str)) != NULL) {
335 __u32 addr = *(__u32 *)he->h_addr;
337 *ipaddrp = ntohl(addr); /* HOST byte order */
346 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
348 #ifdef HAVE_GETHOSTBYNAME
353 net_ip = htonl (ipaddr);
354 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
356 strcpy(str, he->h_name);
362 sprintf (str, "%d.%d.%d.%d",
363 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
364 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
369 ptl_parse_nid (ptl_nid_t *nidp, char *str)
373 unsigned long long ullval;
375 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
377 *nidp = (ptl_nid_t)ipaddr;
379 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
384 ullval = strtoull(str, &end, 0);
385 if (end != str && *end == 0) {
386 /* parsed whole non-empty string */
387 *nidp = (ptl_nid_t)ullval;
395 ptl_parse_anynid (ptl_nid_t *nidp, char *str)
397 if (!strcmp (str, "_all_")) {
402 return ptl_parse_nid(nidp, str);
405 __u64 ptl_nid2u64(ptl_nid_t nid)
407 switch (sizeof (nid)) {
413 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
421 ptl_nid2str (char *buffer, ptl_nid_t nid)
423 __u64 nid64 = ptl_nid2u64(nid);
424 #ifdef HAVE_GETHOSTBYNAME
425 struct hostent *he = 0;
427 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
428 * TCP addresses in the 0.x.x.x subnet are not in use. This can
429 * happen on routers and slows things down a _lot_. Bug 3442. */
430 if (nid & 0xff000000) {
431 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
433 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
437 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
439 #endif /* HAVE_GETHOSTBYNAME */
440 sprintf(buffer, LPX64, nid64);
448 fprintf (stderr, "Error: you must run the 'network' command first.\n");
455 int g_nal_is_compatible (char *cmd, ...)
460 if (!g_nal_is_set ())
466 nal = va_arg (ap, int);
467 } while (nal != 0 && nal != g_nal);
475 /* Don't complain verbosely if we've not been passed a command
476 * name to complain about! */
477 fprintf (stderr, "Command %s not compatible with nal %s\n",
478 cmd, nal2name (g_nal));
484 sock_write (int cfd, void *buffer, int nob)
488 int rc = write (cfd, buffer, nob);
500 fprintf (stderr, "Unexpected zero sock_write\n");
505 buffer = (char *)buffer + nob;
512 sock_read (int cfd, void *buffer, int nob)
516 int rc = read (cfd, buffer, nob);
526 if (rc == 0) /* EOF */
528 errno = ECONNABORTED;
533 buffer = (char *)buffer + nob;
539 int ptl_initialize(int argc, char **argv)
541 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
546 int jt_ptl_network(int argc, char **argv)
552 (nal = ptl_name2nal (argv[1])) >= 0) {
557 fprintf(stderr, "usage: %s \n", argv[0]);
558 for (entry = nalnames; entry->name != NULL; entry++)
559 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
560 fprintf(stderr, ">\n");
565 jt_ptl_print_interfaces (int argc, char **argv)
567 struct portals_cfg pcfg;
572 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
575 for (index = 0;;index++) {
576 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
577 pcfg.pcfg_count = index;
579 rc = pcfg_ioctl (&pcfg);
583 printf ("%s: (%s/%s) npeer %d nroute %d\n",
584 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
585 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
586 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
587 pcfg.pcfg_fd, pcfg.pcfg_count);
591 if (errno == ENOENT) {
592 printf ("<no interfaces>\n");
594 fprintf(stderr, "Error getting interfaces: %s: "
604 jt_ptl_add_interface (int argc, char **argv)
606 struct portals_cfg pcfg;
609 __u32 netmask = 0xffffff00;
614 if (argc < 2 || argc > 3) {
615 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
619 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
622 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
623 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
628 count = strtol(argv[2], &end, 0);
629 if (count > 0 && count < 32 && *end == 0) {
631 for (i = count; i > 0; i--)
632 netmask = netmask|(1<<(32-i));
633 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
634 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
639 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
640 pcfg.pcfg_id = ipaddr;
641 pcfg.pcfg_misc = netmask;
643 rc = pcfg_ioctl (&pcfg);
645 fprintf (stderr, "failed to add interface: %s\n",
654 jt_ptl_del_interface (int argc, char **argv)
656 struct portals_cfg pcfg;
661 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
665 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
669 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
670 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
674 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
675 pcfg.pcfg_id = ipaddr;
677 rc = pcfg_ioctl (&pcfg);
679 fprintf (stderr, "failed to delete interface: %s\n",
688 jt_ptl_print_peers (int argc, char **argv)
690 struct portals_cfg pcfg;
695 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
696 OPENIBNAL, IIBNAL, VIBNAL, 0))
699 for (index = 0;;index++) {
700 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
701 pcfg.pcfg_count = index;
703 rc = pcfg_ioctl (&pcfg);
707 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
708 printf (LPX64"[%d]%s@%s:%d #%d\n",
709 pcfg.pcfg_nid, pcfg.pcfg_wait,
710 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
711 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
712 pcfg.pcfg_misc, pcfg.pcfg_count);
713 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, VIBNAL, 0))
714 printf (LPX64"[%d]@%s:%d\n",
715 pcfg.pcfg_nid, pcfg.pcfg_wait,
716 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
719 printf (LPX64"[%d]\n",
720 pcfg.pcfg_nid, pcfg.pcfg_wait);
724 if (errno == ENOENT) {
725 printf ("<no peers>\n");
727 fprintf(stderr, "Error getting peer list: %s: "
736 jt_ptl_add_peer (int argc, char **argv)
738 struct portals_cfg pcfg;
744 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
745 OPENIBNAL, IIBNAL, VIBNAL, 0))
748 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
750 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
754 } else if (g_nal_is_compatible(NULL, VIBNAL, 0)) {
756 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
760 } else if (argc != 2) {
761 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
765 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
766 nid == PTL_NID_ANY) {
767 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
771 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, VIBNAL, RANAL, 0) &&
772 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
773 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
777 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0) &&
778 ptl_parse_port (&port, argv[3]) != 0) {
779 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
783 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
786 pcfg.pcfg_misc = port;
788 rc = pcfg_ioctl (&pcfg);
790 fprintf (stderr, "failed to add peer: %s\n",
799 jt_ptl_del_peer (int argc, char **argv)
801 struct portals_cfg pcfg;
802 ptl_nid_t nid = PTL_NID_ANY;
804 int single_share = 0;
808 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
809 OPENIBNAL, IIBNAL, VIBNAL, 0))
812 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
814 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
818 } else if (argc > 3) {
819 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
824 ptl_parse_anynid (&nid, argv[1]) != 0) {
825 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
830 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
832 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
833 fprintf (stderr, "Can't parse ip addr: %s\n",
841 if (!strcmp (argv[argidx], "single_share")) {
844 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
849 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
852 pcfg.pcfg_flags = single_share;
854 rc = pcfg_ioctl (&pcfg);
856 fprintf (stderr, "failed to remove peer: %s\n",
865 jt_ptl_print_connections (int argc, char **argv)
867 struct portals_cfg pcfg;
872 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
873 OPENIBNAL, IIBNAL, VIBNAL, 0))
876 for (index = 0; ; index++) {
877 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
878 pcfg.pcfg_count = index;
880 rc = pcfg_ioctl (&pcfg);
884 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
885 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
886 pcfg.pcfg_gw_nal, /* scheduler */
887 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
889 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
890 pcfg.pcfg_misc, /* remote port */
891 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
892 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
893 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
894 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
895 pcfg.pcfg_count, /* tx buffer size */
896 pcfg.pcfg_size, /* rx buffer size */
897 pcfg.pcfg_wait ? "nagle" : "nonagle");
898 else if (g_nal_is_compatible (NULL, RANAL, 0))
899 printf ("[%d]"LPX64"\n",
900 pcfg.pcfg_id, /* device id */
908 if (errno == ENOENT) {
909 printf ("<no connections>\n");
911 fprintf(stderr, "Error getting connection list: %s: "
919 int jt_ptl_connect(int argc, char **argv)
922 /* no connect() support */
924 #else /* HAVE_CONNECT */
925 struct portals_cfg pcfg;
926 struct sockaddr_in srvaddr;
927 struct sockaddr_in locaddr;
931 int type = SOCKNAL_CONN_ANY;
936 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
940 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
943 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
945 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
949 if (ptl_parse_port (&port, argv[2]) != 0) {
950 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
955 for (flag = argv[3]; *flag != 0; flag++)
959 if (type != SOCKNAL_CONN_ANY) {
960 fprintf(stderr, "Can't flag type twice\n");
963 type = SOCKNAL_CONN_BULK_IN;
967 if (type != SOCKNAL_CONN_ANY) {
968 fprintf(stderr, "Can't flag type twice\n");
971 type = SOCKNAL_CONN_BULK_OUT;
975 if (type != SOCKNAL_CONN_ANY) {
976 fprintf(stderr, "Can't flag type twice\n");
979 type = SOCKNAL_CONN_CONTROL;
983 fprintf (stderr, "unrecognised flag '%c'\n",
988 memset(&locaddr, 0, sizeof(locaddr));
989 locaddr.sin_family = AF_INET;
990 locaddr.sin_addr.s_addr = INADDR_ANY;
992 memset(&srvaddr, 0, sizeof(srvaddr));
993 srvaddr.sin_family = AF_INET;
994 srvaddr.sin_port = htons(port);
995 srvaddr.sin_addr.s_addr = htonl(ipaddr);
998 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
999 fd = socket(PF_INET, SOCK_STREAM, 0);
1001 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
1006 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1009 locaddr.sin_port = htons(rport);
1010 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
1011 if (rc == 0 || errno == EACCES) {
1012 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
1015 } else if (errno != EADDRINUSE) {
1016 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
1020 } else if (errno != EADDRINUSE) {
1021 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
1027 if (rport == IPPORT_RESERVED / 2) {
1029 "Warning: all privileged ports are in use.\n");
1033 printf("Connected host: %s type: %s\n",
1035 (type == SOCKNAL_CONN_ANY) ? "A" :
1036 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1037 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1038 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1040 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1041 pcfg.pcfg_nal = g_nal;
1043 pcfg.pcfg_misc = type;
1045 rc = pcfg_ioctl(&pcfg);
1047 fprintf(stderr, "failed to register fd with portals: %s\n",
1053 printf("Connection to %s registered with socknal\n", argv[1]);
1057 fprintf(stderr, "close failed: %d\n", rc);
1060 #endif /* HAVE_CONNECT */
1063 int jt_ptl_disconnect(int argc, char **argv)
1065 struct portals_cfg pcfg;
1066 ptl_nid_t nid = PTL_NID_ANY;
1071 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1075 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1076 OPENIBNAL, IIBNAL, VIBNAL, 0))
1080 ptl_parse_anynid (&nid, argv[1]) != 0) {
1081 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1085 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1087 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1088 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1092 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1093 pcfg.pcfg_nid = nid;
1094 pcfg.pcfg_id = ipaddr;
1096 rc = pcfg_ioctl(&pcfg);
1098 fprintf(stderr, "failed to remove connection: %s\n",
1106 int jt_ptl_push_connection (int argc, char **argv)
1108 struct portals_cfg pcfg;
1110 ptl_nid_t nid = PTL_NID_ANY;
1114 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1118 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1122 ptl_parse_anynid (&nid, argv[1]) != 0) {
1123 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1128 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1129 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1132 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1133 pcfg.pcfg_nid = nid;
1134 pcfg.pcfg_id = ipaddr;
1136 rc = pcfg_ioctl(&pcfg);
1138 fprintf(stderr, "failed to push connection: %s\n",
1147 jt_ptl_print_active_txs (int argc, char **argv)
1149 struct portals_cfg pcfg;
1153 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1156 for (index = 0;;index++) {
1157 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1158 pcfg.pcfg_count = index;
1160 rc = pcfg_ioctl(&pcfg);
1164 printf ("%5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1165 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1166 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1167 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1168 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<weird message>",
1173 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1174 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1175 pcfg.pcfg_flags >> 2);
1179 if (errno == ENOENT) {
1180 printf ("<no active descs>\n");
1182 fprintf(stderr, "Error getting active transmits list: "
1183 "%s: check dmesg.\n",
1190 int jt_ptl_ping(int argc, char **argv)
1197 struct portal_ioctl_data data;
1200 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1204 if (!g_nal_is_set())
1207 if (ptl_parse_nid (&nid, argv[1]) != 0)
1209 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1215 count = atol(argv[2]);
1217 if (count < 0 || count > 20000)
1219 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1225 size= atol(argv[3]);
1228 timeout = atol (argv[4]);
1230 PORTAL_IOC_INIT (data);
1231 data.ioc_count = count;
1232 data.ioc_size = size;
1234 data.ioc_nal = g_nal;
1235 data.ioc_timeout = timeout;
1237 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1239 fprintf(stderr, "failed to start pinger: %s\n",
1246 int jt_ptl_shownid(int argc, char **argv)
1248 struct portal_ioctl_data data;
1252 fprintf(stderr, "usage: %s\n", argv[0]);
1256 if (!g_nal_is_set())
1259 PORTAL_IOC_INIT (data);
1260 data.ioc_nal = g_nal;
1261 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1263 fprintf(stderr, "getting my NID failed: %s\n",
1266 printf(LPX64"\n", data.ioc_nid);
1270 int jt_ptl_mynid(int argc, char **argv)
1273 char hostname[1024];
1275 struct portals_cfg pcfg;
1279 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1280 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1284 if (!g_nal_is_set())
1289 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1290 fprintf(stderr, "gethostname failed: %s\n",
1297 rc = ptl_parse_nid (&mynid, nidstr);
1299 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1303 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1304 pcfg.pcfg_nid = mynid;
1306 rc = pcfg_ioctl(&pcfg);
1308 fprintf(stderr, "setting my NID failed: %s\n",
1311 printf("registered my nid "LPX64" (%s)\n",
1312 ptl_nid2u64(mynid), hostname);
1317 jt_ptl_fail_nid (int argc, char **argv)
1321 unsigned int threshold;
1322 struct portal_ioctl_data data;
1324 if (argc < 2 || argc > 3)
1326 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1330 if (!g_nal_is_set())
1333 if (!strcmp (argv[1], "_all_"))
1335 else if (ptl_parse_anynid (&nid, argv[1]) != 0)
1337 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1342 threshold = PTL_MD_THRESH_INF;
1343 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1344 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1348 PORTAL_IOC_INIT (data);
1349 data.ioc_nal = g_nal;
1351 data.ioc_count = threshold;
1353 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1355 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1358 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1364 jt_ptl_loopback (int argc, char **argv)
1369 struct portal_ioctl_data data;
1373 fprintf (stderr, "usage: %s [on|off]\n", argv[0]);
1377 if (!g_nal_is_set())
1381 if (set && ptl_parse_bool (&enable, argv[1]) != 0) {
1382 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1386 PORTAL_IOC_INIT (data);
1387 data.ioc_nal = g_nal;
1388 data.ioc_flags = enable;
1389 data.ioc_misc = set;
1391 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_LOOPBACK, &data);
1393 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1396 printf ("loopback %s\n", data.ioc_flags ? "enabled" : "disabled");
1402 jt_ptl_add_route (int argc, char **argv)
1404 struct portals_cfg pcfg;
1407 ptl_nid_t gateway_nid;
1412 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1416 if (!g_nal_is_set())
1419 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1421 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1425 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1427 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1433 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1435 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1439 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1440 pcfg.pcfg_nid = gateway_nid;
1441 pcfg.pcfg_nal = ROUTER;
1442 pcfg.pcfg_gw_nal = g_nal;
1443 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1444 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1446 rc = pcfg_ioctl(&pcfg);
1449 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1457 jt_ptl_del_route (int argc, char **argv)
1459 struct portals_cfg pcfg;
1461 ptl_nid_t nid1 = PTL_NID_ANY;
1462 ptl_nid_t nid2 = PTL_NID_ANY;
1467 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1471 if (!g_nal_is_set())
1474 if (ptl_parse_nid (&nid, argv[1]) != 0)
1476 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1481 ptl_parse_nid (&nid1, argv[2]) != 0)
1483 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1490 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1491 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1496 ptl_nid_t tmp = nid1;
1503 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1504 pcfg.pcfg_nal = ROUTER;
1505 pcfg.pcfg_gw_nal = g_nal;
1506 pcfg.pcfg_nid = nid;
1507 pcfg.pcfg_nid2 = nid1;
1508 pcfg.pcfg_nid3 = nid2;
1510 rc = pcfg_ioctl(&pcfg);
1513 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1514 ptl_nid2u64(nid), strerror (errno));
1522 jt_ptl_notify_router (int argc, char **argv)
1524 struct portals_cfg pcfg;
1533 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1538 if (ptl_parse_nid (&nid, argv[1]) != 0)
1540 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1544 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1545 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1549 gettimeofday(&now, NULL);
1553 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1554 fprintf(stderr, "Can't parse time %s\n"
1555 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1556 "or an absolute unix time in seconds\n", argv[3]);
1558 } else if (when > now.tv_sec) {
1559 fprintf (stderr, "%s specifies a time in the future\n",
1564 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1565 pcfg.pcfg_nal = ROUTER;
1566 pcfg.pcfg_gw_nal = g_nal;
1567 pcfg.pcfg_nid = nid;
1568 pcfg.pcfg_flags = enable;
1569 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1570 pcfg.pcfg_nid3 = (__u64)when;
1572 rc = pcfg_ioctl(&pcfg);
1575 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1576 ptl_nid2u64(nid), strerror (errno));
1584 jt_ptl_print_routes (int argc, char **argv)
1586 char buffer[3][128];
1587 struct portals_cfg pcfg;
1591 ptl_nid_t gateway_nid;
1596 for (index = 0;;index++)
1598 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1599 pcfg.pcfg_nal = ROUTER;
1600 pcfg.pcfg_count = index;
1602 rc = pcfg_ioctl(&pcfg);
1606 gateway_nal = pcfg.pcfg_gw_nal;
1607 gateway_nid = pcfg.pcfg_nid;
1608 nid1 = pcfg.pcfg_nid2;
1609 nid2 = pcfg.pcfg_nid3;
1610 alive = pcfg.pcfg_flags;
1612 printf ("%8s %18s : %s - %s, %s\n",
1613 nal2name (gateway_nal),
1614 ptl_nid2str (buffer[0], gateway_nid),
1615 ptl_nid2str (buffer[1], nid1),
1616 ptl_nid2str (buffer[2], nid2),
1617 alive ? "up" : "down");
1620 if (index == 0 && errno != ENOENT) {
1621 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1628 lwt_control(int enable, int clear)
1630 struct portal_ioctl_data data;
1633 PORTAL_IOC_INIT(data);
1634 data.ioc_flags = enable;
1635 data.ioc_misc = clear;
1637 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1641 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1647 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1648 lwt_event_t *events, int size)
1650 struct portal_ioctl_data data;
1653 PORTAL_IOC_INIT(data);
1654 data.ioc_pbuf1 = (char *)events;
1655 data.ioc_plen1 = size;
1657 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1659 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1664 /* crappy overloads */
1665 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1666 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1667 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1668 (int)data.ioc_nid2, sizeof(lwt_event_t),
1670 (int)offsetof(lwt_event_t, lwte_where));
1674 LASSERT (data.ioc_count != 0);
1675 LASSERT (data.ioc_misc != 0);
1678 *now = data.ioc_nid;
1681 *ncpu = data.ioc_count;
1683 if (totalsize != NULL)
1684 *totalsize = data.ioc_misc;
1690 lwt_get_string(char *kstr)
1693 struct portal_ioctl_data data;
1697 /* FIXME: this could maintain a symbol table since we expect to be
1698 * looking up the same strings all the time... */
1700 PORTAL_IOC_INIT(data);
1701 data.ioc_pbuf1 = kstr;
1702 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1703 data.ioc_pbuf2 = NULL;
1706 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1708 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1713 size = data.ioc_count;
1714 ustr = (char *)malloc(size);
1716 fprintf(stderr, "Can't allocate string storage of size %d\n",
1721 PORTAL_IOC_INIT(data);
1722 data.ioc_pbuf1 = kstr;
1723 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1724 data.ioc_pbuf2 = ustr;
1725 data.ioc_plen2 = size;
1727 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1729 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1734 LASSERT(strlen(ustr) == size - 1);
1739 lwt_put_string(char *ustr)
1745 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1748 # error "__WORDSIZE not defined"
1749 #elif __WORDSIZE == 32
1750 # define XFMT "%#010lx"
1751 #elif __WORDSIZE== 64
1752 # define XFMT "%#018lx"
1754 # error "Unexpected __WORDSIZE"
1756 char *where = lwt_get_string(e->lwte_where);
1761 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1762 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1763 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1764 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1767 lwt_put_string(where);
1774 get_cycles_per_usec ()
1776 FILE *f = fopen ("/proc/cpuinfo", "r");
1781 while (fgets (line, sizeof (line), f) != NULL)
1782 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1789 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1794 jt_ptl_lwt(int argc, char **argv)
1796 const int lwt_max_cpus = 32;
1799 int nevents_per_cpu;
1800 lwt_event_t *events;
1801 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1802 lwt_event_t *next_event[lwt_max_cpus];
1803 lwt_event_t *first_event[lwt_max_cpus];
1812 struct timeval tvnow;
1813 int printed_date = 0;
1818 (strcmp(argv[1], "start") &&
1819 strcmp(argv[1], "stop"))) {
1822 " %s stop [fname]\n", argv[0], argv[0]);
1826 if (!strcmp(argv[1], "start")) {
1828 if (lwt_control(0, 0) != 0)
1832 if (lwt_control(0, 1) != 0)
1836 if (lwt_control(1, 0) != 0)
1842 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1845 if (ncpus > lwt_max_cpus) {
1846 fprintf(stderr, "Too many cpus: %d (%d)\n",
1847 ncpus, lwt_max_cpus);
1851 events = (lwt_event_t *)malloc(totalspace);
1852 if (events == NULL) {
1853 fprintf(stderr, "Can't allocate %d\n", totalspace);
1857 if (lwt_control(0, 0) != 0) { /* disable */
1862 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1867 /* we want this time to be sampled at snapshot time */
1868 gettimeofday(&tvnow, NULL);
1871 f = fopen (argv[2], "w");
1873 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1879 mhz = get_cycles_per_usec();
1881 /* carve events into per-cpu slices */
1882 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1883 for (cpu = 0; cpu <= ncpus; cpu++)
1884 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1886 /* find the earliest event on each cpu */
1887 for (cpu = 0; cpu < ncpus; cpu++) {
1888 first_event[cpu] = NULL;
1890 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1892 if (e->lwte_where == NULL) /* not an event */
1895 if (first_event[cpu] == NULL ||
1896 first_event[cpu]->lwte_when > e->lwte_when)
1897 first_event[cpu] = e;
1900 next_event[cpu] = first_event[cpu];
1904 for (cpu = 0; cpu < ncpus; cpu++) {
1905 e = first_event[cpu];
1906 if (e == NULL) /* no events this cpu */
1909 if (e == cpu_event[cpu])
1910 e = cpu_event[cpu + 1] - 1;
1914 /* If there's an event immediately before the first one, this
1915 * cpu wrapped its event buffer */
1916 if (e->lwte_where == NULL)
1919 /* We should only start outputting events from the most recent
1920 * first event in any wrapped cpu. Events before this time on
1921 * other cpus won't have any events from this CPU to interleave
1923 if (t0 < first_event[cpu]->lwte_when)
1924 t0 = first_event[cpu]->lwte_when;
1928 /* find which cpu has the next event */
1930 for (i = 0; i < ncpus; i++) {
1932 if (next_event[i] == NULL) /* this cpu exhausted */
1936 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1940 if (cpu < 0) /* all cpus exhausted */
1944 /* no wrapped cpus and this is he first ever event */
1945 t0 = next_event[cpu]->lwte_when;
1948 if (t0 <= next_event[cpu]->lwte_when) {
1949 /* on or after the first event */
1950 if (!printed_date) {
1951 cycles_t du = (tnow - t0) / mhz;
1952 time_t then = tvnow.tv_sec - du/1000000;
1954 if (du % 1000000 > tvnow.tv_usec)
1957 fprintf(f, "%s", ctime(&then));
1961 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1965 if (++nlines % 10000 == 0 && f != stdout) {
1966 /* show some activity... */
1972 tlast = next_event[cpu]->lwte_when;
1975 if (next_event[cpu] == cpu_event[cpu + 1])
1976 next_event[cpu] = cpu_event[cpu];
1978 if (next_event[cpu]->lwte_where == NULL ||
1979 next_event[cpu] == first_event[cpu])
1980 next_event[cpu] = NULL;
1992 int jt_ptl_memhog(int argc, char **argv)
1994 static int gfp = 0; /* sticky! */
1996 struct portal_ioctl_data data;
2002 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
2006 count = strtol(argv[1], &end, 0);
2007 if (count < 0 || *end != 0) {
2008 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
2013 rc = strtol(argv[2], &end, 0);
2015 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
2021 PORTAL_IOC_INIT(data);
2022 data.ioc_count = count;
2023 data.ioc_flags = gfp;
2024 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
2027 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
2031 printf("memhog %d OK\n", count);