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",
422 ptl_nid2str (char *buffer, ptl_nid_t nid)
424 __u64 nid64 = ptl_nid2u64(nid);
425 #ifdef HAVE_GETHOSTBYNAME
426 struct hostent *he = 0;
428 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
429 * TCP addresses in the 0.x.x.x subnet are not in use. This can
430 * happen on routers and slows things down a _lot_. Bug 3442. */
431 if (nid & 0xff000000) {
432 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
434 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
438 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
440 #endif /* HAVE_GETHOSTBYNAME */
441 sprintf(buffer, LPX64, nid64);
449 fprintf (stderr, "Error: you must run the 'network' command first.\n");
456 int g_nal_is_compatible (char *cmd, ...)
461 if (!g_nal_is_set ())
467 nal = va_arg (ap, int);
468 } while (nal != 0 && nal != g_nal);
476 /* Don't complain verbosely if we've not been passed a command
477 * name to complain about! */
478 fprintf (stderr, "Command %s not compatible with nal %s\n",
479 cmd, nal2name (g_nal));
485 sock_write (int cfd, void *buffer, int nob)
489 int rc = write (cfd, buffer, nob);
501 fprintf (stderr, "Unexpected zero sock_write\n");
506 buffer = (char *)buffer + nob;
513 sock_read (int cfd, void *buffer, int nob)
517 int rc = read (cfd, buffer, nob);
527 if (rc == 0) /* EOF */
529 errno = ECONNABORTED;
534 buffer = (char *)buffer + nob;
540 int ptl_initialize(int argc, char **argv)
542 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
547 int jt_ptl_network(int argc, char **argv)
553 (nal = ptl_name2nal (argv[1])) >= 0) {
558 fprintf(stderr, "usage: %s \n", argv[0]);
559 for (entry = nalnames; entry->name != NULL; entry++)
560 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
561 fprintf(stderr, ">\n");
566 jt_ptl_print_interfaces (int argc, char **argv)
568 struct portals_cfg pcfg;
573 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
576 for (index = 0;;index++) {
577 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
578 pcfg.pcfg_count = index;
580 rc = pcfg_ioctl (&pcfg);
584 printf ("%s: (%s/%s) npeer %d nroute %d\n",
585 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
586 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
587 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
588 pcfg.pcfg_fd, pcfg.pcfg_count);
592 if (errno == ENOENT) {
593 printf ("<no interfaces>\n");
595 fprintf(stderr, "Error getting interfaces: %s: "
605 jt_ptl_add_interface (int argc, char **argv)
607 struct portals_cfg pcfg;
610 __u32 netmask = 0xffffff00;
615 if (argc < 2 || argc > 3) {
616 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
620 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
623 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
624 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
629 count = strtol(argv[2], &end, 0);
630 if (count > 0 && count < 32 && *end == 0) {
632 for (i = count; i > 0; i--)
633 netmask = netmask|(1<<(32-i));
634 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
635 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
640 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
641 pcfg.pcfg_id = ipaddr;
642 pcfg.pcfg_misc = netmask;
644 rc = pcfg_ioctl (&pcfg);
646 fprintf (stderr, "failed to add interface: %s\n",
655 jt_ptl_del_interface (int argc, char **argv)
657 struct portals_cfg pcfg;
662 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
666 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
670 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
671 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
675 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
676 pcfg.pcfg_id = ipaddr;
678 rc = pcfg_ioctl (&pcfg);
680 fprintf (stderr, "failed to delete interface: %s\n",
689 jt_ptl_print_peers (int argc, char **argv)
691 struct portals_cfg pcfg;
696 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
697 OPENIBNAL, IIBNAL, VIBNAL, 0))
700 for (index = 0;;index++) {
701 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
702 pcfg.pcfg_count = index;
704 rc = pcfg_ioctl (&pcfg);
708 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
709 printf (LPX64"[%d]%s@%s:%d #%d\n",
710 pcfg.pcfg_nid, pcfg.pcfg_wait,
711 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
712 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
713 pcfg.pcfg_misc, pcfg.pcfg_count);
714 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, VIBNAL, 0))
715 printf (LPX64"[%d]@%s:%d\n",
716 pcfg.pcfg_nid, pcfg.pcfg_wait,
717 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
720 printf (LPX64"[%d]\n",
721 pcfg.pcfg_nid, pcfg.pcfg_wait);
725 if (errno == ENOENT) {
726 printf ("<no peers>\n");
728 fprintf(stderr, "Error getting peer list: %s: "
737 jt_ptl_add_peer (int argc, char **argv)
739 struct portals_cfg pcfg;
745 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
746 OPENIBNAL, IIBNAL, VIBNAL, 0))
749 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
751 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
755 } else if (g_nal_is_compatible(NULL, VIBNAL, 0)) {
757 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
761 } else if (argc != 2) {
762 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
766 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
767 nid == PTL_NID_ANY) {
768 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
772 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, VIBNAL, RANAL, 0) &&
773 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
774 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
778 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0) &&
779 ptl_parse_port (&port, argv[3]) != 0) {
780 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
784 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
787 pcfg.pcfg_misc = port;
789 rc = pcfg_ioctl (&pcfg);
791 fprintf (stderr, "failed to add peer: %s\n",
800 jt_ptl_del_peer (int argc, char **argv)
802 struct portals_cfg pcfg;
803 ptl_nid_t nid = PTL_NID_ANY;
805 int single_share = 0;
809 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
810 OPENIBNAL, IIBNAL, VIBNAL, 0))
813 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
815 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
819 } else if (argc > 3) {
820 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
825 ptl_parse_anynid (&nid, argv[1]) != 0) {
826 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
831 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
833 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
834 fprintf (stderr, "Can't parse ip addr: %s\n",
842 if (!strcmp (argv[argidx], "single_share")) {
845 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
850 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
853 pcfg.pcfg_flags = single_share;
855 rc = pcfg_ioctl (&pcfg);
857 fprintf (stderr, "failed to remove peer: %s\n",
866 jt_ptl_print_connections (int argc, char **argv)
868 struct portals_cfg pcfg;
873 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
874 OPENIBNAL, IIBNAL, VIBNAL, 0))
877 for (index = 0; ; index++) {
878 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
879 pcfg.pcfg_count = index;
881 rc = pcfg_ioctl (&pcfg);
885 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
886 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
887 pcfg.pcfg_gw_nal, /* scheduler */
888 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
890 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
891 pcfg.pcfg_misc, /* remote port */
892 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
893 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
894 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
895 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
896 pcfg.pcfg_count, /* tx buffer size */
897 pcfg.pcfg_size, /* rx buffer size */
898 pcfg.pcfg_wait ? "nagle" : "nonagle");
899 else if (g_nal_is_compatible (NULL, RANAL, 0))
900 printf ("[%d]"LPX64"\n",
901 pcfg.pcfg_id, /* device id */
909 if (errno == ENOENT) {
910 printf ("<no connections>\n");
912 fprintf(stderr, "Error getting connection list: %s: "
920 int jt_ptl_connect(int argc, char **argv)
923 /* no connect() support */
925 #else /* HAVE_CONNECT */
926 struct portals_cfg pcfg;
927 struct sockaddr_in srvaddr;
928 struct sockaddr_in locaddr;
932 int type = SOCKNAL_CONN_ANY;
937 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
941 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
944 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
946 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
950 if (ptl_parse_port (&port, argv[2]) != 0) {
951 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
956 for (flag = argv[3]; *flag != 0; flag++)
960 if (type != SOCKNAL_CONN_ANY) {
961 fprintf(stderr, "Can't flag type twice\n");
964 type = SOCKNAL_CONN_BULK_IN;
968 if (type != SOCKNAL_CONN_ANY) {
969 fprintf(stderr, "Can't flag type twice\n");
972 type = SOCKNAL_CONN_BULK_OUT;
976 if (type != SOCKNAL_CONN_ANY) {
977 fprintf(stderr, "Can't flag type twice\n");
980 type = SOCKNAL_CONN_CONTROL;
984 fprintf (stderr, "unrecognised flag '%c'\n",
989 memset(&locaddr, 0, sizeof(locaddr));
990 locaddr.sin_family = AF_INET;
991 locaddr.sin_addr.s_addr = INADDR_ANY;
993 memset(&srvaddr, 0, sizeof(srvaddr));
994 srvaddr.sin_family = AF_INET;
995 srvaddr.sin_port = htons(port);
996 srvaddr.sin_addr.s_addr = htonl(ipaddr);
999 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
1000 fd = socket(PF_INET, SOCK_STREAM, 0);
1002 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
1007 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1010 locaddr.sin_port = htons(rport);
1011 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
1012 if (rc == 0 || errno == EACCES) {
1013 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
1016 } else if (errno != EADDRINUSE) {
1017 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
1021 } else if (errno != EADDRINUSE) {
1022 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
1028 if (rport == IPPORT_RESERVED / 2) {
1030 "Warning: all privileged ports are in use.\n");
1034 printf("Connected host: %s type: %s\n",
1036 (type == SOCKNAL_CONN_ANY) ? "A" :
1037 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1038 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1039 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1041 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1042 pcfg.pcfg_nal = g_nal;
1044 pcfg.pcfg_misc = type;
1046 rc = pcfg_ioctl(&pcfg);
1048 fprintf(stderr, "failed to register fd with portals: %s\n",
1054 printf("Connection to %s registered with socknal\n", argv[1]);
1058 fprintf(stderr, "close failed: %d\n", rc);
1061 #endif /* HAVE_CONNECT */
1064 int jt_ptl_disconnect(int argc, char **argv)
1066 struct portals_cfg pcfg;
1067 ptl_nid_t nid = PTL_NID_ANY;
1072 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1076 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1077 OPENIBNAL, IIBNAL, VIBNAL, 0))
1081 ptl_parse_anynid (&nid, argv[1]) != 0) {
1082 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1086 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1088 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1089 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1093 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1094 pcfg.pcfg_nid = nid;
1095 pcfg.pcfg_id = ipaddr;
1097 rc = pcfg_ioctl(&pcfg);
1099 fprintf(stderr, "failed to remove connection: %s\n",
1107 int jt_ptl_push_connection (int argc, char **argv)
1109 struct portals_cfg pcfg;
1111 ptl_nid_t nid = PTL_NID_ANY;
1115 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1119 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1123 ptl_parse_anynid (&nid, argv[1]) != 0) {
1124 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1129 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1130 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1133 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1134 pcfg.pcfg_nid = nid;
1135 pcfg.pcfg_id = ipaddr;
1137 rc = pcfg_ioctl(&pcfg);
1139 fprintf(stderr, "failed to push connection: %s\n",
1148 jt_ptl_print_active_txs (int argc, char **argv)
1150 struct portals_cfg pcfg;
1154 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1157 for (index = 0;;index++) {
1158 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1159 pcfg.pcfg_count = index;
1161 rc = pcfg_ioctl(&pcfg);
1165 printf ("%5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1166 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1167 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1168 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1169 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<weird message>",
1174 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1175 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1176 pcfg.pcfg_flags >> 2);
1180 if (errno == ENOENT) {
1181 printf ("<no active descs>\n");
1183 fprintf(stderr, "Error getting active transmits list: "
1184 "%s: check dmesg.\n",
1191 int jt_ptl_ping(int argc, char **argv)
1198 struct portal_ioctl_data data;
1201 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1205 if (!g_nal_is_set())
1208 if (ptl_parse_nid (&nid, argv[1]) != 0)
1210 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1216 count = atol(argv[2]);
1218 if (count < 0 || count > 20000)
1220 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1226 size= atol(argv[3]);
1229 timeout = atol (argv[4]);
1231 PORTAL_IOC_INIT (data);
1232 data.ioc_count = count;
1233 data.ioc_size = size;
1235 data.ioc_nal = g_nal;
1236 data.ioc_timeout = timeout;
1238 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1240 fprintf(stderr, "failed to start pinger: %s\n",
1247 int jt_ptl_shownid(int argc, char **argv)
1249 struct portal_ioctl_data data;
1253 fprintf(stderr, "usage: %s\n", argv[0]);
1257 if (!g_nal_is_set())
1260 PORTAL_IOC_INIT (data);
1261 data.ioc_nal = g_nal;
1262 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1264 fprintf(stderr, "getting my NID failed: %s\n",
1267 printf(LPX64"\n", data.ioc_nid);
1271 int jt_ptl_mynid(int argc, char **argv)
1274 char hostname[1024];
1276 struct portals_cfg pcfg;
1280 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1281 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1285 if (!g_nal_is_set())
1290 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1291 fprintf(stderr, "gethostname failed: %s\n",
1298 rc = ptl_parse_nid (&mynid, nidstr);
1300 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1304 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1305 pcfg.pcfg_nid = mynid;
1307 rc = pcfg_ioctl(&pcfg);
1309 fprintf(stderr, "setting my NID failed: %s\n",
1312 printf("registered my nid "LPX64" (%s)\n",
1313 ptl_nid2u64(mynid), hostname);
1318 jt_ptl_fail_nid (int argc, char **argv)
1322 unsigned int threshold;
1323 struct portal_ioctl_data data;
1325 if (argc < 2 || argc > 3)
1327 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1331 if (!g_nal_is_set())
1334 if (!strcmp (argv[1], "_all_"))
1336 else if (ptl_parse_anynid (&nid, argv[1]) != 0)
1338 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1343 threshold = PTL_MD_THRESH_INF;
1344 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1345 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1349 PORTAL_IOC_INIT (data);
1350 data.ioc_nal = g_nal;
1352 data.ioc_count = threshold;
1354 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1356 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1359 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1365 jt_ptl_loopback (int argc, char **argv)
1370 struct portal_ioctl_data data;
1374 fprintf (stderr, "usage: %s [on|off]\n", argv[0]);
1378 if (!g_nal_is_set())
1382 if (set && ptl_parse_bool (&enable, argv[1]) != 0) {
1383 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1387 PORTAL_IOC_INIT (data);
1388 data.ioc_nal = g_nal;
1389 data.ioc_flags = enable;
1390 data.ioc_misc = set;
1392 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_LOOPBACK, &data);
1394 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1397 printf ("loopback %s\n", data.ioc_flags ? "enabled" : "disabled");
1403 jt_ptl_add_route (int argc, char **argv)
1405 struct portals_cfg pcfg;
1408 ptl_nid_t gateway_nid;
1413 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1417 if (!g_nal_is_set())
1420 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1422 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1426 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1428 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1434 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1436 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1440 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1441 pcfg.pcfg_nid = gateway_nid;
1442 pcfg.pcfg_nal = ROUTER;
1443 pcfg.pcfg_gw_nal = g_nal;
1444 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1445 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1447 rc = pcfg_ioctl(&pcfg);
1450 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1458 jt_ptl_del_route (int argc, char **argv)
1460 struct portals_cfg pcfg;
1462 ptl_nid_t nid1 = PTL_NID_ANY;
1463 ptl_nid_t nid2 = PTL_NID_ANY;
1468 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1472 if (!g_nal_is_set())
1475 if (ptl_parse_nid (&nid, argv[1]) != 0)
1477 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1482 ptl_parse_nid (&nid1, argv[2]) != 0)
1484 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1491 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1492 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1497 ptl_nid_t tmp = nid1;
1504 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1505 pcfg.pcfg_nal = ROUTER;
1506 pcfg.pcfg_gw_nal = g_nal;
1507 pcfg.pcfg_nid = nid;
1508 pcfg.pcfg_nid2 = nid1;
1509 pcfg.pcfg_nid3 = nid2;
1511 rc = pcfg_ioctl(&pcfg);
1514 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1515 ptl_nid2u64(nid), strerror (errno));
1523 jt_ptl_notify_router (int argc, char **argv)
1525 struct portals_cfg pcfg;
1534 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1539 if (ptl_parse_nid (&nid, argv[1]) != 0)
1541 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1545 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1546 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1550 gettimeofday(&now, NULL);
1554 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1555 fprintf(stderr, "Can't parse time %s\n"
1556 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1557 "or an absolute unix time in seconds\n", argv[3]);
1559 } else if (when > now.tv_sec) {
1560 fprintf (stderr, "%s specifies a time in the future\n",
1565 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1566 pcfg.pcfg_nal = ROUTER;
1567 pcfg.pcfg_gw_nal = g_nal;
1568 pcfg.pcfg_nid = nid;
1569 pcfg.pcfg_flags = enable;
1570 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1571 pcfg.pcfg_nid3 = (__u64)when;
1573 rc = pcfg_ioctl(&pcfg);
1576 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1577 ptl_nid2u64(nid), strerror (errno));
1585 jt_ptl_print_routes (int argc, char **argv)
1587 char buffer[3][128];
1588 struct portals_cfg pcfg;
1592 ptl_nid_t gateway_nid;
1597 for (index = 0;;index++)
1599 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1600 pcfg.pcfg_nal = ROUTER;
1601 pcfg.pcfg_count = index;
1603 rc = pcfg_ioctl(&pcfg);
1607 gateway_nal = pcfg.pcfg_gw_nal;
1608 gateway_nid = pcfg.pcfg_nid;
1609 nid1 = pcfg.pcfg_nid2;
1610 nid2 = pcfg.pcfg_nid3;
1611 alive = pcfg.pcfg_flags;
1613 printf ("%8s %18s : %s - %s, %s\n",
1614 nal2name (gateway_nal),
1615 ptl_nid2str (buffer[0], gateway_nid),
1616 ptl_nid2str (buffer[1], nid1),
1617 ptl_nid2str (buffer[2], nid2),
1618 alive ? "up" : "down");
1621 if (index == 0 && errno != ENOENT) {
1622 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1629 lwt_control(int enable, int clear)
1631 struct portal_ioctl_data data;
1634 PORTAL_IOC_INIT(data);
1635 data.ioc_flags = enable;
1636 data.ioc_misc = clear;
1638 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1642 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1648 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1649 lwt_event_t *events, int size)
1651 struct portal_ioctl_data data;
1654 PORTAL_IOC_INIT(data);
1655 data.ioc_pbuf1 = (char *)events;
1656 data.ioc_plen1 = size;
1658 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1660 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1665 /* crappy overloads */
1666 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1667 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1668 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1669 (int)data.ioc_nid2, (int)sizeof(lwt_event_t),
1671 (int)offsetof(lwt_event_t, lwte_where));
1675 LASSERT (data.ioc_count != 0);
1676 LASSERT (data.ioc_misc != 0);
1679 *now = data.ioc_nid;
1682 *ncpu = data.ioc_count;
1684 if (totalsize != NULL)
1685 *totalsize = data.ioc_misc;
1691 lwt_get_string(char *kstr)
1694 struct portal_ioctl_data data;
1698 /* FIXME: this could maintain a symbol table since we expect to be
1699 * looking up the same strings all the time... */
1701 PORTAL_IOC_INIT(data);
1702 data.ioc_pbuf1 = kstr;
1703 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1704 data.ioc_pbuf2 = NULL;
1707 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1709 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1714 size = data.ioc_count;
1715 ustr = (char *)malloc(size);
1717 fprintf(stderr, "Can't allocate string storage of size %d\n",
1722 PORTAL_IOC_INIT(data);
1723 data.ioc_pbuf1 = kstr;
1724 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1725 data.ioc_pbuf2 = ustr;
1726 data.ioc_plen2 = size;
1728 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1730 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1735 LASSERT(strlen(ustr) == size - 1);
1740 lwt_put_string(char *ustr)
1746 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1749 # error "__WORDSIZE not defined"
1750 #elif __WORDSIZE == 32
1751 # define XFMT "%#010lx"
1752 #elif __WORDSIZE== 64
1753 # define XFMT "%#018lx"
1755 # error "Unexpected __WORDSIZE"
1757 char *where = lwt_get_string(e->lwte_where);
1762 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1763 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1764 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1765 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1768 lwt_put_string(where);
1775 get_cycles_per_usec ()
1777 FILE *f = fopen ("/proc/cpuinfo", "r");
1782 while (fgets (line, sizeof (line), f) != NULL)
1783 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1790 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1795 jt_ptl_lwt(int argc, char **argv)
1797 const int lwt_max_cpus = 32;
1800 int nevents_per_cpu;
1801 lwt_event_t *events;
1802 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1803 lwt_event_t *next_event[lwt_max_cpus];
1804 lwt_event_t *first_event[lwt_max_cpus];
1813 struct timeval tvnow;
1814 int printed_date = 0;
1819 (strcmp(argv[1], "start") &&
1820 strcmp(argv[1], "stop"))) {
1823 " %s stop [fname]\n", argv[0], argv[0]);
1827 if (!strcmp(argv[1], "start")) {
1829 if (lwt_control(0, 0) != 0)
1833 if (lwt_control(0, 1) != 0)
1837 if (lwt_control(1, 0) != 0)
1843 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1846 if (ncpus > lwt_max_cpus) {
1847 fprintf(stderr, "Too many cpus: %d (%d)\n",
1848 ncpus, lwt_max_cpus);
1852 events = (lwt_event_t *)malloc(totalspace);
1853 if (events == NULL) {
1854 fprintf(stderr, "Can't allocate %d\n", totalspace);
1858 if (lwt_control(0, 0) != 0) { /* disable */
1863 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1868 /* we want this time to be sampled at snapshot time */
1869 gettimeofday(&tvnow, NULL);
1872 f = fopen (argv[2], "w");
1874 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1880 mhz = get_cycles_per_usec();
1882 /* carve events into per-cpu slices */
1883 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1884 for (cpu = 0; cpu <= ncpus; cpu++)
1885 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1887 /* find the earliest event on each cpu */
1888 for (cpu = 0; cpu < ncpus; cpu++) {
1889 first_event[cpu] = NULL;
1891 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1893 if (e->lwte_where == NULL) /* not an event */
1896 if (first_event[cpu] == NULL ||
1897 first_event[cpu]->lwte_when > e->lwte_when)
1898 first_event[cpu] = e;
1901 next_event[cpu] = first_event[cpu];
1905 for (cpu = 0; cpu < ncpus; cpu++) {
1906 e = first_event[cpu];
1907 if (e == NULL) /* no events this cpu */
1910 if (e == cpu_event[cpu])
1911 e = cpu_event[cpu + 1] - 1;
1915 /* If there's an event immediately before the first one, this
1916 * cpu wrapped its event buffer */
1917 if (e->lwte_where == NULL)
1920 /* We should only start outputting events from the most recent
1921 * first event in any wrapped cpu. Events before this time on
1922 * other cpus won't have any events from this CPU to interleave
1924 if (t0 < first_event[cpu]->lwte_when)
1925 t0 = first_event[cpu]->lwte_when;
1929 /* find which cpu has the next event */
1931 for (i = 0; i < ncpus; i++) {
1933 if (next_event[i] == NULL) /* this cpu exhausted */
1937 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1941 if (cpu < 0) /* all cpus exhausted */
1945 /* no wrapped cpus and this is he first ever event */
1946 t0 = next_event[cpu]->lwte_when;
1949 if (t0 <= next_event[cpu]->lwte_when) {
1950 /* on or after the first event */
1951 if (!printed_date) {
1952 cycles_t du = (tnow - t0) / mhz;
1953 time_t then = tvnow.tv_sec - du/1000000;
1955 if (du % 1000000 > tvnow.tv_usec)
1958 fprintf(f, "%s", ctime(&then));
1962 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1966 if (++nlines % 10000 == 0 && f != stdout) {
1967 /* show some activity... */
1973 tlast = next_event[cpu]->lwte_when;
1976 if (next_event[cpu] == cpu_event[cpu + 1])
1977 next_event[cpu] = cpu_event[cpu];
1979 if (next_event[cpu]->lwte_where == NULL ||
1980 next_event[cpu] == first_event[cpu])
1981 next_event[cpu] = NULL;
1993 int jt_ptl_memhog(int argc, char **argv)
1995 static int gfp = 0; /* sticky! */
1997 struct portal_ioctl_data data;
2003 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
2007 count = strtol(argv[1], &end, 0);
2008 if (count < 0 || *end != 0) {
2009 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
2014 rc = strtol(argv[2], &end, 0);
2016 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
2022 PORTAL_IOC_INIT(data);
2023 data.ioc_count = count;
2024 data.ioc_flags = gfp;
2025 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
2028 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
2032 printf("memhog %d OK\n", count);