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>
51 #include <portals/ipmap.h>
54 #include <libcfs/portals_utils.h>
55 #include <portals/api-support.h>
56 #include <portals/ptlctl.h>
57 #include <portals/lib-types.h>
58 #include <portals/socknal.h>
61 unsigned int portal_debug;
62 unsigned int portal_printk;
64 static unsigned int g_nal = 0;
72 static name2num_t nalnames[] = {
78 {"openib", OPENIBNAL},
84 {"cray_kern_nal", CRAY_KERN_NAL},
85 {"cray_user_nal", CRAY_USER_NAL},
86 {"cray_qk_nal", CRAY_QK_NAL},
91 static cfg_record_cb_t g_record_cb;
93 /* Convert a string boolean to an int; "enable" -> 1 */
94 int ptl_parse_bool (int *b, char *str) {
95 if (!strcasecmp (str, "no") ||
96 !strcasecmp (str, "n") ||
97 !strcasecmp (str, "off") ||
98 !strcasecmp (str, "down") ||
99 !strcasecmp (str, "disable"))
105 if (!strcasecmp (str, "yes") ||
106 !strcasecmp (str, "y") ||
107 !strcasecmp (str, "on") ||
108 !strcasecmp (str, "up") ||
109 !strcasecmp (str, "enable"))
118 /* Convert human readable size string to and int; "1k" -> 1000 */
119 int ptl_parse_size (int *sizep, char *str) {
123 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
156 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
163 pcfg_ioctl(struct portals_cfg *pcfg)
167 if (pcfg->pcfg_nal ==0)
168 pcfg->pcfg_nal = g_nal;
171 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
173 struct portal_ioctl_data data;
174 PORTAL_IOC_INIT (data);
175 data.ioc_pbuf1 = (char*)pcfg;
176 data.ioc_plen1 = sizeof(*pcfg);
177 /* XXX liblustre hack XXX */
178 data.ioc_nal_cmd = pcfg->pcfg_command;
179 data.ioc_nid = pcfg->pcfg_nid;
181 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
183 if (rc == 0 && pcfg->pcfg_version != PORTALS_CFG_VERSION)
193 name2num_lookup_name (name2num_t *table, char *str)
195 while (table->name != NULL)
196 if (!strcmp (str, table->name))
204 name2num_lookup_num (name2num_t *table, int num)
206 while (table->name != NULL)
207 if (num == table->num)
215 ptl_name2nal (char *str)
217 name2num_t *e = name2num_lookup_name (nalnames, str);
219 return ((e == NULL) ? -1 : e->num);
225 name2num_t *e = name2num_lookup_num (nalnames, nal);
227 return ((e == NULL) ? "???" : e->name);
230 #ifdef HAVE_GETHOSTBYNAME
231 static struct hostent *
232 ptl_gethostbyname(char * hname) {
234 he = gethostbyname(hname);
239 fprintf(stderr, "Unable to resolve hostname: %s\n",
243 fprintf(stderr, "gethostbyname error for %s: %s\n",
244 hname, strerror(h_errno));
254 ptl_parse_port (int *port, char *str)
258 *port = strtol (str, &end, 0);
260 if (*end == 0 && /* parsed whole string */
261 *port > 0 && *port < 65536) /* minimal sanity check */
268 ptl_parse_time (time_t *t, char *str)
274 *t = strtol (str, &end, 0);
275 if (*end == 0) /* parsed whole string */
278 memset (&tm, 0, sizeof (tm));
279 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
280 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
281 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
285 tm.tm_mon--; /* convert to 0 == Jan */
286 tm.tm_year -= 1900; /* y2k quirk */
287 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
290 if (*t == (time_t)-1)
297 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
304 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
305 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
306 (c & ~0xff) == 0 && (d & ~0xff) == 0)
308 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
316 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
318 #ifdef HAVE_GETHOSTBYNAME
322 if (!strcmp (str, "_all_")) {
327 if (ptl_parse_ipquad(ipaddrp, str) == 0)
330 #ifdef HAVE_GETHOSTBYNAME
331 if ((('a' <= str[0] && str[0] <= 'z') ||
332 ('A' <= str[0] && str[0] <= 'Z')) &&
333 (he = ptl_gethostbyname (str)) != NULL) {
334 __u32 addr = *(__u32 *)he->h_addr;
336 *ipaddrp = ntohl(addr); /* HOST byte order */
345 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
347 #ifdef HAVE_GETHOSTBYNAME
352 net_ip = htonl (ipaddr);
353 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
355 strcpy(str, he->h_name);
361 sprintf (str, "%d.%d.%d.%d",
362 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
363 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
368 ptl_parse_nid (ptl_nid_t *nidp, char *str)
372 unsigned long long ullval;
374 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
376 *nidp = (ptl_nid_t)ipaddr;
378 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
383 ullval = strtoull(str, &end, 0);
384 if (end != str && *end == 0) {
385 /* parsed whole non-empty string */
386 *nidp = (ptl_nid_t)ullval;
394 ptl_parse_anynid (ptl_nid_t *nidp, char *str)
396 if (!strcmp (str, "_all_")) {
401 return ptl_parse_nid(nidp, str);
404 __u64 ptl_nid2u64(ptl_nid_t nid)
406 switch (sizeof (nid)) {
412 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
420 ptl_nid2str (char *buffer, ptl_nid_t nid)
422 __u64 nid64 = ptl_nid2u64(nid);
423 #ifdef HAVE_GETHOSTBYNAME
424 struct hostent *he = 0;
426 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
427 * TCP addresses in the 0.x.x.x subnet are not in use. This can
428 * happen on routers and slows things down a _lot_. Bug 3442. */
429 if (nid & 0xff000000) {
430 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
432 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
436 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
438 #endif /* HAVE_GETHOSTBYNAME */
439 sprintf(buffer, LPX64, nid64);
447 fprintf (stderr, "Error: you must run the 'network' command first.\n");
454 int g_nal_is_compatible (char *cmd, ...)
459 if (!g_nal_is_set ())
465 nal = va_arg (ap, int);
466 } while (nal != 0 && nal != g_nal);
474 /* Don't complain verbosely if we've not been passed a command
475 * name to complain about! */
476 fprintf (stderr, "Command %s not compatible with nal %s\n",
477 cmd, nal2name (g_nal));
483 sock_write (int cfd, void *buffer, int nob)
487 int rc = write (cfd, buffer, nob);
499 fprintf (stderr, "Unexpected zero sock_write\n");
504 buffer = (char *)buffer + nob;
511 sock_read (int cfd, void *buffer, int nob)
515 int rc = read (cfd, buffer, nob);
525 if (rc == 0) /* EOF */
527 errno = ECONNABORTED;
532 buffer = (char *)buffer + nob;
538 int ptl_initialize(int argc, char **argv)
540 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
545 int jt_ptl_network(int argc, char **argv)
551 (nal = ptl_name2nal (argv[1])) >= 0) {
556 fprintf(stderr, "usage: %s \n", argv[0]);
557 for (entry = nalnames; entry->name != NULL; entry++)
558 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
559 fprintf(stderr, ">\n");
564 jt_ptl_print_interfaces (int argc, char **argv)
566 struct portals_cfg pcfg;
571 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
574 for (index = 0;;index++) {
575 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
576 pcfg.pcfg_count = index;
578 rc = pcfg_ioctl (&pcfg);
582 printf ("%s: (%s/%s) npeer %d nroute %d\n",
583 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
584 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
585 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
586 pcfg.pcfg_fd, pcfg.pcfg_count);
590 if (errno == ENOENT) {
591 printf ("<no interfaces>\n");
593 fprintf(stderr, "Error getting interfaces: %s: "
603 jt_ptl_add_interface (int argc, char **argv)
605 struct portals_cfg pcfg;
608 __u32 netmask = 0xffffff00;
613 if (argc < 2 || argc > 3) {
614 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
618 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
621 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
622 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
627 count = strtol(argv[2], &end, 0);
628 if (count > 0 && count < 32 && *end == 0) {
630 for (i = count; i > 0; i--)
631 netmask = netmask|(1<<(32-i));
632 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
633 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
638 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
639 pcfg.pcfg_id = ipaddr;
640 pcfg.pcfg_misc = netmask;
642 rc = pcfg_ioctl (&pcfg);
644 fprintf (stderr, "failed to add interface: %s\n",
653 jt_ptl_del_interface (int argc, char **argv)
655 struct portals_cfg pcfg;
660 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
664 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
668 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
669 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
673 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
674 pcfg.pcfg_id = ipaddr;
676 rc = pcfg_ioctl (&pcfg);
678 fprintf (stderr, "failed to delete interface: %s\n",
687 jt_ptl_print_peers (int argc, char **argv)
689 struct portals_cfg pcfg;
694 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
695 OPENIBNAL, IIBNAL, VIBNAL, 0))
698 for (index = 0;;index++) {
699 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
700 pcfg.pcfg_count = index;
702 rc = pcfg_ioctl (&pcfg);
706 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
707 printf (LPX64"[%d]%s@%s:%d #%d\n",
708 pcfg.pcfg_nid, pcfg.pcfg_wait,
709 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
710 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
711 pcfg.pcfg_misc, pcfg.pcfg_count);
712 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, VIBNAL, 0))
713 printf (LPX64"[%d]@%s:%d\n",
714 pcfg.pcfg_nid, pcfg.pcfg_wait,
715 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
718 printf (LPX64"[%d]\n",
719 pcfg.pcfg_nid, pcfg.pcfg_wait);
723 if (errno == ENOENT) {
724 printf ("<no peers>\n");
726 fprintf(stderr, "Error getting peer list: %s: "
735 jt_ptl_add_peer (int argc, char **argv)
737 struct portals_cfg pcfg;
743 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
744 OPENIBNAL, IIBNAL, VIBNAL, 0))
747 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
749 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
753 } else if (g_nal_is_compatible(NULL, VIBNAL, 0)) {
755 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
759 } else if (argc != 2) {
760 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
764 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
765 nid == PTL_NID_ANY) {
766 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
770 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, VIBNAL, RANAL, 0) &&
771 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
772 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
776 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0) &&
777 ptl_parse_port (&port, argv[3]) != 0) {
778 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
782 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
785 pcfg.pcfg_misc = port;
787 rc = pcfg_ioctl (&pcfg);
789 fprintf (stderr, "failed to add peer: %s\n",
798 jt_ptl_del_peer (int argc, char **argv)
800 struct portals_cfg pcfg;
801 ptl_nid_t nid = PTL_NID_ANY;
803 int single_share = 0;
807 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
808 OPENIBNAL, IIBNAL, VIBNAL, 0))
811 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
813 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
817 } else if (argc > 3) {
818 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
823 ptl_parse_anynid (&nid, argv[1]) != 0) {
824 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
829 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
831 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
832 fprintf (stderr, "Can't parse ip addr: %s\n",
840 if (!strcmp (argv[argidx], "single_share")) {
843 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
848 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
851 pcfg.pcfg_flags = single_share;
853 rc = pcfg_ioctl (&pcfg);
855 fprintf (stderr, "failed to remove peer: %s\n",
864 jt_ptl_print_connections (int argc, char **argv)
866 struct portals_cfg pcfg;
871 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
872 OPENIBNAL, IIBNAL, VIBNAL, 0))
875 for (index = 0; ; index++) {
876 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
877 pcfg.pcfg_count = index;
879 rc = pcfg_ioctl (&pcfg);
883 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
884 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
885 pcfg.pcfg_gw_nal, /* scheduler */
886 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
888 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
889 pcfg.pcfg_misc, /* remote port */
890 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
891 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
892 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
893 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
894 pcfg.pcfg_count, /* tx buffer size */
895 pcfg.pcfg_size, /* rx buffer size */
896 pcfg.pcfg_wait ? "nagle" : "nonagle");
897 else if (g_nal_is_compatible (NULL, RANAL, 0))
898 printf ("[%d]"LPX64"\n",
899 pcfg.pcfg_id, /* device id */
907 if (errno == ENOENT) {
908 printf ("<no connections>\n");
910 fprintf(stderr, "Error getting connection list: %s: "
918 int jt_ptl_connect(int argc, char **argv)
921 /* no connect() support */
923 #else /* HAVE_CONNECT */
924 struct portals_cfg pcfg;
925 struct sockaddr_in srvaddr;
926 struct sockaddr_in locaddr;
930 int type = SOCKNAL_CONN_ANY;
935 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
939 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
942 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
944 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
948 if (ptl_parse_port (&port, argv[2]) != 0) {
949 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
954 for (flag = argv[3]; *flag != 0; flag++)
958 if (type != SOCKNAL_CONN_ANY) {
959 fprintf(stderr, "Can't flag type twice\n");
962 type = SOCKNAL_CONN_BULK_IN;
966 if (type != SOCKNAL_CONN_ANY) {
967 fprintf(stderr, "Can't flag type twice\n");
970 type = SOCKNAL_CONN_BULK_OUT;
974 if (type != SOCKNAL_CONN_ANY) {
975 fprintf(stderr, "Can't flag type twice\n");
978 type = SOCKNAL_CONN_CONTROL;
982 fprintf (stderr, "unrecognised flag '%c'\n",
987 memset(&locaddr, 0, sizeof(locaddr));
988 locaddr.sin_family = AF_INET;
989 locaddr.sin_addr.s_addr = INADDR_ANY;
991 memset(&srvaddr, 0, sizeof(srvaddr));
992 srvaddr.sin_family = AF_INET;
993 srvaddr.sin_port = htons(port);
994 srvaddr.sin_addr.s_addr = htonl(ipaddr);
997 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
998 fd = socket(PF_INET, SOCK_STREAM, 0);
1000 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
1005 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1008 locaddr.sin_port = htons(rport);
1009 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
1010 if (rc == 0 || errno == EACCES) {
1011 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
1014 } else if (errno != EADDRINUSE) {
1015 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
1019 } else if (errno != EADDRINUSE) {
1020 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
1026 if (rport == IPPORT_RESERVED / 2) {
1028 "Warning: all privileged ports are in use.\n");
1032 printf("Connected host: %s type: %s\n",
1034 (type == SOCKNAL_CONN_ANY) ? "A" :
1035 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1036 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1037 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1039 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1040 pcfg.pcfg_nal = g_nal;
1042 pcfg.pcfg_misc = type;
1044 rc = pcfg_ioctl(&pcfg);
1046 fprintf(stderr, "failed to register fd with portals: %s\n",
1052 printf("Connection to %s registered with socknal\n", argv[1]);
1056 fprintf(stderr, "close failed: %d\n", rc);
1059 #endif /* HAVE_CONNECT */
1062 int jt_ptl_disconnect(int argc, char **argv)
1064 struct portals_cfg pcfg;
1065 ptl_nid_t nid = PTL_NID_ANY;
1070 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1074 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1075 OPENIBNAL, IIBNAL, VIBNAL, 0))
1079 ptl_parse_anynid (&nid, argv[1]) != 0) {
1080 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1084 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1086 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1087 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1091 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1092 pcfg.pcfg_nid = nid;
1093 pcfg.pcfg_id = ipaddr;
1095 rc = pcfg_ioctl(&pcfg);
1097 fprintf(stderr, "failed to remove connection: %s\n",
1105 int jt_ptl_push_connection (int argc, char **argv)
1107 struct portals_cfg pcfg;
1109 ptl_nid_t nid = PTL_NID_ANY;
1113 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1117 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1121 ptl_parse_anynid (&nid, argv[1]) != 0) {
1122 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1127 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1128 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1131 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1132 pcfg.pcfg_nid = nid;
1133 pcfg.pcfg_id = ipaddr;
1135 rc = pcfg_ioctl(&pcfg);
1137 fprintf(stderr, "failed to push connection: %s\n",
1146 jt_ptl_print_active_txs (int argc, char **argv)
1148 struct portals_cfg pcfg;
1152 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1155 for (index = 0;;index++) {
1156 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1157 pcfg.pcfg_count = index;
1159 rc = pcfg_ioctl(&pcfg);
1163 printf ("%5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1164 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1165 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1166 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1167 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<weird message>",
1172 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1173 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1174 pcfg.pcfg_flags >> 2);
1178 if (errno == ENOENT) {
1179 printf ("<no active descs>\n");
1181 fprintf(stderr, "Error getting active transmits list: "
1182 "%s: check dmesg.\n",
1189 int jt_ptl_ping(int argc, char **argv)
1196 struct portal_ioctl_data data;
1199 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1203 if (!g_nal_is_set())
1206 if (ptl_parse_nid (&nid, argv[1]) != 0)
1208 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1214 count = atol(argv[2]);
1216 if (count < 0 || count > 20000)
1218 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1224 size= atol(argv[3]);
1227 timeout = atol (argv[4]);
1229 PORTAL_IOC_INIT (data);
1230 data.ioc_count = count;
1231 data.ioc_size = size;
1233 data.ioc_nal = g_nal;
1234 data.ioc_timeout = timeout;
1236 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1238 fprintf(stderr, "failed to start pinger: %s\n",
1245 int jt_ptl_shownid(int argc, char **argv)
1247 struct portal_ioctl_data data;
1251 fprintf(stderr, "usage: %s\n", argv[0]);
1255 if (!g_nal_is_set())
1258 PORTAL_IOC_INIT (data);
1259 data.ioc_nal = g_nal;
1260 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1262 fprintf(stderr, "getting my NID failed: %s\n",
1265 printf(LPX64"\n", data.ioc_nid);
1269 int jt_ptl_mynid(int argc, char **argv)
1272 char hostname[1024];
1274 struct portals_cfg pcfg;
1278 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1279 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1283 if (!g_nal_is_set())
1288 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1289 fprintf(stderr, "gethostname failed: %s\n",
1296 rc = ptl_parse_nid (&mynid, nidstr);
1298 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1302 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1303 pcfg.pcfg_nid = mynid;
1305 rc = pcfg_ioctl(&pcfg);
1307 fprintf(stderr, "setting my NID failed: %s\n",
1310 printf("registered my nid "LPX64" (%s)\n",
1311 ptl_nid2u64(mynid), hostname);
1316 jt_ptl_fail_nid (int argc, char **argv)
1320 unsigned int threshold;
1321 struct portal_ioctl_data data;
1323 if (argc < 2 || argc > 3)
1325 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1329 if (!g_nal_is_set())
1332 if (!strcmp (argv[1], "_all_"))
1334 else if (ptl_parse_anynid (&nid, argv[1]) != 0)
1336 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1341 threshold = PTL_MD_THRESH_INF;
1342 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1343 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1347 PORTAL_IOC_INIT (data);
1348 data.ioc_nal = g_nal;
1350 data.ioc_count = threshold;
1352 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1354 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1357 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1363 jt_ptl_loopback (int argc, char **argv)
1368 struct portal_ioctl_data data;
1372 fprintf (stderr, "usage: %s [on|off]\n", argv[0]);
1376 if (!g_nal_is_set())
1380 if (set && ptl_parse_bool (&enable, argv[1]) != 0) {
1381 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1385 PORTAL_IOC_INIT (data);
1386 data.ioc_nal = g_nal;
1387 data.ioc_flags = enable;
1388 data.ioc_misc = set;
1390 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_LOOPBACK, &data);
1392 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1395 printf ("loopback %s\n", data.ioc_flags ? "enabled" : "disabled");
1401 jt_ptl_add_route (int argc, char **argv)
1403 struct portals_cfg pcfg;
1406 ptl_nid_t gateway_nid;
1411 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1415 if (!g_nal_is_set())
1418 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1420 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1424 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1426 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1432 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1434 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1438 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1439 pcfg.pcfg_nid = gateway_nid;
1440 pcfg.pcfg_nal = ROUTER;
1441 pcfg.pcfg_gw_nal = g_nal;
1442 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1443 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1445 rc = pcfg_ioctl(&pcfg);
1448 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1456 jt_ptl_del_route (int argc, char **argv)
1458 struct portals_cfg pcfg;
1460 ptl_nid_t nid1 = PTL_NID_ANY;
1461 ptl_nid_t nid2 = PTL_NID_ANY;
1466 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1470 if (!g_nal_is_set())
1473 if (ptl_parse_nid (&nid, argv[1]) != 0)
1475 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1480 ptl_parse_nid (&nid1, argv[2]) != 0)
1482 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1489 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1490 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1495 ptl_nid_t tmp = nid1;
1502 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1503 pcfg.pcfg_nal = ROUTER;
1504 pcfg.pcfg_gw_nal = g_nal;
1505 pcfg.pcfg_nid = nid;
1506 pcfg.pcfg_nid2 = nid1;
1507 pcfg.pcfg_nid3 = nid2;
1509 rc = pcfg_ioctl(&pcfg);
1512 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1513 ptl_nid2u64(nid), strerror (errno));
1521 jt_ptl_notify_router (int argc, char **argv)
1523 struct portals_cfg pcfg;
1532 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1537 if (ptl_parse_nid (&nid, argv[1]) != 0)
1539 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1543 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1544 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1548 gettimeofday(&now, NULL);
1552 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1553 fprintf(stderr, "Can't parse time %s\n"
1554 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1555 "or an absolute unix time in seconds\n", argv[3]);
1557 } else if (when > now.tv_sec) {
1558 fprintf (stderr, "%s specifies a time in the future\n",
1563 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1564 pcfg.pcfg_nal = ROUTER;
1565 pcfg.pcfg_gw_nal = g_nal;
1566 pcfg.pcfg_nid = nid;
1567 pcfg.pcfg_flags = enable;
1568 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1569 pcfg.pcfg_nid3 = (__u64)when;
1571 rc = pcfg_ioctl(&pcfg);
1574 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1575 ptl_nid2u64(nid), strerror (errno));
1583 jt_ptl_print_routes (int argc, char **argv)
1585 char buffer[3][128];
1586 struct portals_cfg pcfg;
1590 ptl_nid_t gateway_nid;
1595 for (index = 0;;index++)
1597 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1598 pcfg.pcfg_nal = ROUTER;
1599 pcfg.pcfg_count = index;
1601 rc = pcfg_ioctl(&pcfg);
1605 gateway_nal = pcfg.pcfg_gw_nal;
1606 gateway_nid = pcfg.pcfg_nid;
1607 nid1 = pcfg.pcfg_nid2;
1608 nid2 = pcfg.pcfg_nid3;
1609 alive = pcfg.pcfg_flags;
1611 printf ("%8s %18s : %s - %s, %s\n",
1612 nal2name (gateway_nal),
1613 ptl_nid2str (buffer[0], gateway_nid),
1614 ptl_nid2str (buffer[1], nid1),
1615 ptl_nid2str (buffer[2], nid2),
1616 alive ? "up" : "down");
1619 if (index == 0 && errno != ENOENT) {
1620 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1627 lwt_control(int enable, int clear)
1629 struct portal_ioctl_data data;
1632 PORTAL_IOC_INIT(data);
1633 data.ioc_flags = enable;
1634 data.ioc_misc = clear;
1636 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1640 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1646 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1647 lwt_event_t *events, int size)
1649 struct portal_ioctl_data data;
1652 PORTAL_IOC_INIT(data);
1653 data.ioc_pbuf1 = (char *)events;
1654 data.ioc_plen1 = size;
1656 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1658 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1663 /* crappy overloads */
1664 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1665 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1666 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1667 (int)data.ioc_nid2, sizeof(lwt_event_t),
1669 (int)offsetof(lwt_event_t, lwte_where));
1673 LASSERT (data.ioc_count != 0);
1674 LASSERT (data.ioc_misc != 0);
1677 *now = data.ioc_nid;
1680 *ncpu = data.ioc_count;
1682 if (totalsize != NULL)
1683 *totalsize = data.ioc_misc;
1689 lwt_get_string(char *kstr)
1692 struct portal_ioctl_data data;
1696 /* FIXME: this could maintain a symbol table since we expect to be
1697 * looking up the same strings all the time... */
1699 PORTAL_IOC_INIT(data);
1700 data.ioc_pbuf1 = kstr;
1701 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1702 data.ioc_pbuf2 = NULL;
1705 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1707 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1712 size = data.ioc_count;
1713 ustr = (char *)malloc(size);
1715 fprintf(stderr, "Can't allocate string storage of size %d\n",
1720 PORTAL_IOC_INIT(data);
1721 data.ioc_pbuf1 = kstr;
1722 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1723 data.ioc_pbuf2 = ustr;
1724 data.ioc_plen2 = size;
1726 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1728 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1733 LASSERT(strlen(ustr) == size - 1);
1738 lwt_put_string(char *ustr)
1744 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1747 # error "__WORDSIZE not defined"
1748 #elif __WORDSIZE == 32
1749 # define XFMT "%#010lx"
1750 #elif __WORDSIZE== 64
1751 # define XFMT "%#018lx"
1753 # error "Unexpected __WORDSIZE"
1755 char *where = lwt_get_string(e->lwte_where);
1760 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1761 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1762 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1763 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1766 lwt_put_string(where);
1773 get_cycles_per_usec ()
1775 FILE *f = fopen ("/proc/cpuinfo", "r");
1780 while (fgets (line, sizeof (line), f) != NULL)
1781 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1788 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1793 jt_ptl_lwt(int argc, char **argv)
1795 const int lwt_max_cpus = 32;
1798 int nevents_per_cpu;
1799 lwt_event_t *events;
1800 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1801 lwt_event_t *next_event[lwt_max_cpus];
1802 lwt_event_t *first_event[lwt_max_cpus];
1811 struct timeval tvnow;
1812 int printed_date = 0;
1817 (strcmp(argv[1], "start") &&
1818 strcmp(argv[1], "stop"))) {
1821 " %s stop [fname]\n", argv[0], argv[0]);
1825 if (!strcmp(argv[1], "start")) {
1827 if (lwt_control(0, 0) != 0)
1831 if (lwt_control(0, 1) != 0)
1835 if (lwt_control(1, 0) != 0)
1841 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1844 if (ncpus > lwt_max_cpus) {
1845 fprintf(stderr, "Too many cpus: %d (%d)\n",
1846 ncpus, lwt_max_cpus);
1850 events = (lwt_event_t *)malloc(totalspace);
1851 if (events == NULL) {
1852 fprintf(stderr, "Can't allocate %d\n", totalspace);
1856 if (lwt_control(0, 0) != 0) { /* disable */
1861 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1866 /* we want this time to be sampled at snapshot time */
1867 gettimeofday(&tvnow, NULL);
1870 f = fopen (argv[2], "w");
1872 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1878 mhz = get_cycles_per_usec();
1880 /* carve events into per-cpu slices */
1881 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1882 for (cpu = 0; cpu <= ncpus; cpu++)
1883 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1885 /* find the earliest event on each cpu */
1886 for (cpu = 0; cpu < ncpus; cpu++) {
1887 first_event[cpu] = NULL;
1889 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1891 if (e->lwte_where == NULL) /* not an event */
1894 if (first_event[cpu] == NULL ||
1895 first_event[cpu]->lwte_when > e->lwte_when)
1896 first_event[cpu] = e;
1899 next_event[cpu] = first_event[cpu];
1903 for (cpu = 0; cpu < ncpus; cpu++) {
1904 e = first_event[cpu];
1905 if (e == NULL) /* no events this cpu */
1908 if (e == cpu_event[cpu])
1909 e = cpu_event[cpu + 1] - 1;
1913 /* If there's an event immediately before the first one, this
1914 * cpu wrapped its event buffer */
1915 if (e->lwte_where == NULL)
1918 /* We should only start outputting events from the most recent
1919 * first event in any wrapped cpu. Events before this time on
1920 * other cpus won't have any events from this CPU to interleave
1922 if (t0 < first_event[cpu]->lwte_when)
1923 t0 = first_event[cpu]->lwte_when;
1927 /* find which cpu has the next event */
1929 for (i = 0; i < ncpus; i++) {
1931 if (next_event[i] == NULL) /* this cpu exhausted */
1935 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1939 if (cpu < 0) /* all cpus exhausted */
1943 /* no wrapped cpus and this is he first ever event */
1944 t0 = next_event[cpu]->lwte_when;
1947 if (t0 <= next_event[cpu]->lwte_when) {
1948 /* on or after the first event */
1949 if (!printed_date) {
1950 cycles_t du = (tnow - t0) / mhz;
1951 time_t then = tvnow.tv_sec - du/1000000;
1953 if (du % 1000000 > tvnow.tv_usec)
1956 fprintf(f, "%s", ctime(&then));
1960 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1964 if (++nlines % 10000 == 0 && f != stdout) {
1965 /* show some activity... */
1971 tlast = next_event[cpu]->lwte_when;
1974 if (next_event[cpu] == cpu_event[cpu + 1])
1975 next_event[cpu] = cpu_event[cpu];
1977 if (next_event[cpu]->lwte_where == NULL ||
1978 next_event[cpu] == first_event[cpu])
1979 next_event[cpu] = NULL;
1991 int jt_ptl_memhog(int argc, char **argv)
1993 static int gfp = 0; /* sticky! */
1995 struct portal_ioctl_data data;
2001 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
2005 count = strtol(argv[1], &end, 0);
2006 if (count < 0 || *end != 0) {
2007 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
2012 rc = strtol(argv[2], &end, 0);
2014 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
2020 PORTAL_IOC_INIT(data);
2021 data.ioc_count = count;
2022 data.ioc_flags = gfp;
2023 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
2026 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
2030 printf("memhog %d OK\n", count);