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 #include <sys/ioctl.h>
47 #include <portals/ipmap.h>
50 #include <libcfs/portals_utils.h>
51 #include <portals/api-support.h>
52 #include <portals/ptlctl.h>
53 #include <portals/lib-types.h>
54 #include <portals/socknal.h>
57 unsigned int portal_debug;
58 unsigned int portal_printk;
60 static unsigned int g_nal = 0;
68 static name2num_t nalnames[] = {
74 {"openib", OPENIBNAL},
80 {"cray_kern_nal", CRAY_KERN_NAL},
81 {"cray_user_nal", CRAY_USER_NAL},
82 {"cray_qk_nal", CRAY_QK_NAL},
87 static cfg_record_cb_t g_record_cb;
89 /* Convert a string boolean to an int; "enable" -> 1 */
90 int ptl_parse_bool (int *b, char *str) {
91 if (!strcasecmp (str, "no") ||
92 !strcasecmp (str, "n") ||
93 !strcasecmp (str, "off") ||
94 !strcasecmp (str, "down") ||
95 !strcasecmp (str, "disable"))
101 if (!strcasecmp (str, "yes") ||
102 !strcasecmp (str, "y") ||
103 !strcasecmp (str, "on") ||
104 !strcasecmp (str, "up") ||
105 !strcasecmp (str, "enable"))
114 /* Convert human readable size string to and int; "1k" -> 1000 */
115 int ptl_parse_size (int *sizep, char *str) {
119 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
152 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
159 pcfg_ioctl(struct portals_cfg *pcfg)
163 if (pcfg->pcfg_nal ==0)
164 pcfg->pcfg_nal = g_nal;
167 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
169 struct portal_ioctl_data data;
170 PORTAL_IOC_INIT (data);
171 data.ioc_pbuf1 = (char*)pcfg;
172 data.ioc_plen1 = sizeof(*pcfg);
173 /* XXX liblustre hack XXX */
174 data.ioc_nal_cmd = pcfg->pcfg_command;
175 data.ioc_nid = pcfg->pcfg_nid;
177 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
186 name2num_lookup_name (name2num_t *table, char *str)
188 while (table->name != NULL)
189 if (!strcmp (str, table->name))
197 name2num_lookup_num (name2num_t *table, int num)
199 while (table->name != NULL)
200 if (num == table->num)
208 ptl_name2nal (char *str)
210 name2num_t *e = name2num_lookup_name (nalnames, str);
212 return ((e == NULL) ? -1 : e->num);
218 name2num_t *e = name2num_lookup_num (nalnames, nal);
220 return ((e == NULL) ? "???" : e->name);
223 #ifdef HAVE_GETHOSTBYNAME
224 static struct hostent *
225 ptl_gethostbyname(char * hname) {
227 he = gethostbyname(hname);
232 fprintf(stderr, "Unable to resolve hostname: %s\n",
236 fprintf(stderr, "gethostbyname error for %s: %s\n",
237 hname, strerror(h_errno));
247 ptl_parse_port (int *port, char *str)
251 *port = strtol (str, &end, 0);
253 if (*end == 0 && /* parsed whole string */
254 *port > 0 && *port < 65536) /* minimal sanity check */
261 ptl_parse_time (time_t *t, char *str)
267 *t = strtol (str, &end, 0);
268 if (*end == 0) /* parsed whole string */
271 memset (&tm, 0, sizeof (tm));
272 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
273 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
274 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
278 tm.tm_mon--; /* convert to 0 == Jan */
279 tm.tm_year -= 1900; /* y2k quirk */
280 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
283 if (*t == (time_t)-1)
290 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
297 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
298 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
299 (c & ~0xff) == 0 && (d & ~0xff) == 0)
301 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
309 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
311 #ifdef HAVE_GETHOSTBYNAME
315 if (!strcmp (str, "_all_")) {
320 if (ptl_parse_ipquad(ipaddrp, str) == 0)
323 #ifdef HAVE_GETHOSTBYNAME
324 if ((('a' <= str[0] && str[0] <= 'z') ||
325 ('A' <= str[0] && str[0] <= 'Z')) &&
326 (he = ptl_gethostbyname (str)) != NULL) {
327 __u32 addr = *(__u32 *)he->h_addr;
329 *ipaddrp = ntohl(addr); /* HOST byte order */
338 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
340 #ifdef HAVE_GETHOSTBYNAME
345 net_ip = htonl (ipaddr);
346 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
348 strcpy(str, he->h_name);
354 sprintf (str, "%d.%d.%d.%d",
355 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
356 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
361 ptl_parse_nid (ptl_nid_t *nidp, char *str)
365 unsigned long long ullval;
367 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
369 *nidp = (ptl_nid_t)ipaddr;
371 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
376 ullval = strtoull(str, &end, 0);
377 if (end != str && *end == 0) {
378 /* parsed whole non-empty string */
379 *nidp = (ptl_nid_t)ullval;
387 ptl_parse_anynid (ptl_nid_t *nidp, char *str)
389 if (!strcmp (str, "_all_")) {
394 return ptl_parse_nid(nidp, str);
397 __u64 ptl_nid2u64(ptl_nid_t nid)
399 switch (sizeof (nid)) {
405 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
413 ptl_nid2str (char *buffer, ptl_nid_t nid)
415 __u64 nid64 = ptl_nid2u64(nid);
416 #ifdef HAVE_GETHOSTBYNAME
417 struct hostent *he = 0;
419 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
420 * TCP addresses in the 0.x.x.x subnet are not in use. This can
421 * happen on routers and slows things down a _lot_. Bug 3442. */
422 if (nid & 0xff000000) {
423 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
425 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
429 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
431 #endif /* HAVE_GETHOSTBYNAME */
432 sprintf(buffer, LPX64, nid64);
440 fprintf (stderr, "Error: you must run the 'network' command first.\n");
447 int g_nal_is_compatible (char *cmd, ...)
452 if (!g_nal_is_set ())
458 nal = va_arg (ap, int);
459 } while (nal != 0 && nal != g_nal);
467 /* Don't complain verbosely if we've not been passed a command
468 * name to complain about! */
469 fprintf (stderr, "Command %s not compatible with nal %s\n",
470 cmd, nal2name (g_nal));
476 sock_write (int cfd, void *buffer, int nob)
480 int rc = write (cfd, buffer, nob);
492 fprintf (stderr, "Unexpected zero sock_write\n");
497 buffer = (char *)buffer + nob;
504 sock_read (int cfd, void *buffer, int nob)
508 int rc = read (cfd, buffer, nob);
518 if (rc == 0) /* EOF */
520 errno = ECONNABORTED;
525 buffer = (char *)buffer + nob;
531 int ptl_initialize(int argc, char **argv)
533 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
538 int jt_ptl_network(int argc, char **argv)
544 (nal = ptl_name2nal (argv[1])) >= 0) {
549 fprintf(stderr, "usage: %s \n", argv[0]);
550 for (entry = nalnames; entry->name != NULL; entry++)
551 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
552 fprintf(stderr, ">\n");
557 jt_ptl_print_interfaces (int argc, char **argv)
559 struct portals_cfg pcfg;
564 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
567 for (index = 0;;index++) {
568 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
569 pcfg.pcfg_count = index;
571 rc = pcfg_ioctl (&pcfg);
575 printf ("%s: (%s/%s) npeer %d nroute %d\n",
576 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
577 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
578 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
579 pcfg.pcfg_fd, pcfg.pcfg_count);
583 printf ("<no interfaces>\n");
588 jt_ptl_add_interface (int argc, char **argv)
590 struct portals_cfg pcfg;
593 __u32 netmask = 0xffffff00;
598 if (argc < 2 || argc > 3) {
599 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
603 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
606 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
607 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
612 count = strtol(argv[2], &end, 0);
613 if (count > 0 && count < 32 && *end == 0) {
615 for (i = count; i > 0; i--)
616 netmask = netmask|(1<<(32-i));
617 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
618 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
623 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
624 pcfg.pcfg_id = ipaddr;
625 pcfg.pcfg_misc = netmask;
627 rc = pcfg_ioctl (&pcfg);
629 fprintf (stderr, "failed to add interface: %s\n",
638 jt_ptl_del_interface (int argc, char **argv)
640 struct portals_cfg pcfg;
645 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
649 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
653 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
654 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
658 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
659 pcfg.pcfg_id = ipaddr;
661 rc = pcfg_ioctl (&pcfg);
663 fprintf (stderr, "failed to delete interface: %s\n",
672 jt_ptl_print_peers (int argc, char **argv)
674 struct portals_cfg pcfg;
679 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
680 OPENIBNAL, IIBNAL, VIBNAL, 0))
683 for (index = 0;;index++) {
684 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
685 pcfg.pcfg_count = index;
687 rc = pcfg_ioctl (&pcfg);
691 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
692 printf (LPX64"[%d]%s@%s:%d #%d\n",
693 pcfg.pcfg_nid, pcfg.pcfg_wait,
694 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
695 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
696 pcfg.pcfg_misc, pcfg.pcfg_count);
697 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, VIBNAL, 0))
698 printf (LPX64"[%d]@%s:%d\n",
699 pcfg.pcfg_nid, pcfg.pcfg_wait,
700 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
703 printf (LPX64"[%d]\n",
704 pcfg.pcfg_nid, pcfg.pcfg_wait);
708 printf ("<no peers>\n");
713 jt_ptl_add_peer (int argc, char **argv)
715 struct portals_cfg pcfg;
721 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
722 OPENIBNAL, IIBNAL, VIBNAL, 0))
725 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
727 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
731 } else if (g_nal_is_compatible(NULL, VIBNAL, 0)) {
733 fprintf (stderr, "usage(vib): %s nid ipaddr\n",
737 } else if (argc != 2) {
738 fprintf (stderr, "usage(iib): %s nid\n", argv[0]);
742 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
743 nid == PTL_NID_ANY) {
744 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
748 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, VIBNAL, RANAL, 0) &&
749 ptl_parse_ipaddr (&ip, argv[2]) != 0) {
750 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
754 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0) &&
755 ptl_parse_port (&port, argv[3]) != 0) {
756 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
760 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
763 pcfg.pcfg_misc = port;
765 rc = pcfg_ioctl (&pcfg);
767 fprintf (stderr, "failed to add peer: %s\n",
776 jt_ptl_del_peer (int argc, char **argv)
778 struct portals_cfg pcfg;
779 ptl_nid_t nid = PTL_NID_ANY;
781 int single_share = 0;
785 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
786 OPENIBNAL, IIBNAL, VIBNAL, 0))
789 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
791 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
795 } else if (argc > 3) {
796 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
801 ptl_parse_anynid (&nid, argv[1]) != 0) {
802 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
807 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
809 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
810 fprintf (stderr, "Can't parse ip addr: %s\n",
818 if (!strcmp (argv[argidx], "single_share")) {
821 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
826 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
829 pcfg.pcfg_flags = single_share;
831 rc = pcfg_ioctl (&pcfg);
833 fprintf (stderr, "failed to remove peer: %s\n",
842 jt_ptl_print_connections (int argc, char **argv)
844 struct portals_cfg pcfg;
849 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
850 OPENIBNAL, IIBNAL, VIBNAL, 0))
853 for (index = 0;;index++) {
854 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
855 pcfg.pcfg_count = index;
857 rc = pcfg_ioctl (&pcfg);
861 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
862 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
863 pcfg.pcfg_gw_nal, /* scheduler */
864 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
866 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
867 pcfg.pcfg_misc, /* remote port */
868 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
869 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
870 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
871 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
872 pcfg.pcfg_count, /* tx buffer size */
873 pcfg.pcfg_size, /* rx buffer size */
874 pcfg.pcfg_wait ? "nagle" : "nonagle");
875 else if (g_nal_is_compatible (NULL, RANAL, 0))
876 printf ("[%d]"LPX64"\n",
877 pcfg.pcfg_id, /* device id */
885 printf ("<no connections>\n");
889 int jt_ptl_connect(int argc, char **argv)
892 /* no connect() support */
894 #else /* HAVE_CONNECT */
895 struct portals_cfg pcfg;
896 struct sockaddr_in srvaddr;
897 struct sockaddr_in locaddr;
901 int type = SOCKNAL_CONN_ANY;
906 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
910 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
913 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
915 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
919 if (ptl_parse_port (&port, argv[2]) != 0) {
920 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
925 for (flag = argv[3]; *flag != 0; flag++)
929 if (type != SOCKNAL_CONN_ANY) {
930 fprintf(stderr, "Can't flag type twice\n");
933 type = SOCKNAL_CONN_BULK_IN;
937 if (type != SOCKNAL_CONN_ANY) {
938 fprintf(stderr, "Can't flag type twice\n");
941 type = SOCKNAL_CONN_BULK_OUT;
945 if (type != SOCKNAL_CONN_ANY) {
946 fprintf(stderr, "Can't flag type twice\n");
949 type = SOCKNAL_CONN_CONTROL;
953 fprintf (stderr, "unrecognised flag '%c'\n",
958 memset(&locaddr, 0, sizeof(locaddr));
959 locaddr.sin_family = AF_INET;
960 locaddr.sin_addr.s_addr = INADDR_ANY;
962 memset(&srvaddr, 0, sizeof(srvaddr));
963 srvaddr.sin_family = AF_INET;
964 srvaddr.sin_port = htons(port);
965 srvaddr.sin_addr.s_addr = htonl(ipaddr);
968 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
969 fd = socket(PF_INET, SOCK_STREAM, 0);
971 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
976 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
979 locaddr.sin_port = htons(rport);
980 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
981 if (rc == 0 || errno == EACCES) {
982 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
985 } else if (errno != EADDRINUSE) {
986 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
990 } else if (errno != EADDRINUSE) {
991 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
997 if (rport == IPPORT_RESERVED / 2) {
999 "Warning: all privileged ports are in use.\n");
1003 printf("Connected host: %s type: %s\n",
1005 (type == SOCKNAL_CONN_ANY) ? "A" :
1006 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1007 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1008 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1010 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1011 pcfg.pcfg_nal = g_nal;
1013 pcfg.pcfg_misc = type;
1015 rc = pcfg_ioctl(&pcfg);
1017 fprintf(stderr, "failed to register fd with portals: %s\n",
1023 printf("Connection to %s registered with socknal\n", argv[1]);
1027 fprintf(stderr, "close failed: %d\n", rc);
1030 #endif /* HAVE_CONNECT */
1033 int jt_ptl_disconnect(int argc, char **argv)
1035 struct portals_cfg pcfg;
1036 ptl_nid_t nid = PTL_NID_ANY;
1041 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1045 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1046 OPENIBNAL, IIBNAL, VIBNAL, 0))
1050 ptl_parse_anynid (&nid, argv[1]) != 0) {
1051 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1055 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1057 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1058 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1062 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1063 pcfg.pcfg_nid = nid;
1064 pcfg.pcfg_id = ipaddr;
1066 rc = pcfg_ioctl(&pcfg);
1068 fprintf(stderr, "failed to remove connection: %s\n",
1076 int jt_ptl_push_connection (int argc, char **argv)
1078 struct portals_cfg pcfg;
1080 ptl_nid_t nid = PTL_NID_ANY;
1084 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1088 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1092 ptl_parse_anynid (&nid, argv[1]) != 0) {
1093 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1098 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1099 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1102 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1103 pcfg.pcfg_nid = nid;
1104 pcfg.pcfg_id = ipaddr;
1106 rc = pcfg_ioctl(&pcfg);
1108 fprintf(stderr, "failed to push connection: %s\n",
1117 jt_ptl_print_active_txs (int argc, char **argv)
1119 struct portals_cfg pcfg;
1123 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1126 for (index = 0;;index++) {
1127 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1128 pcfg.pcfg_count = index;
1130 rc = pcfg_ioctl(&pcfg);
1134 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1136 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1137 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1138 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1139 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1144 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1145 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1146 pcfg.pcfg_flags >> 2);
1150 printf ("<no active descs>\n");
1154 int jt_ptl_ping(int argc, char **argv)
1161 struct portal_ioctl_data data;
1164 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1168 if (!g_nal_is_set())
1171 if (ptl_parse_nid (&nid, argv[1]) != 0)
1173 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1179 count = atol(argv[2]);
1181 if (count < 0 || count > 20000)
1183 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1189 size= atol(argv[3]);
1192 timeout = atol (argv[4]);
1194 PORTAL_IOC_INIT (data);
1195 data.ioc_count = count;
1196 data.ioc_size = size;
1198 data.ioc_nal = g_nal;
1199 data.ioc_timeout = timeout;
1201 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1203 fprintf(stderr, "failed to start pinger: %s\n",
1210 int jt_ptl_shownid(int argc, char **argv)
1212 struct portal_ioctl_data data;
1216 fprintf(stderr, "usage: %s\n", argv[0]);
1220 if (!g_nal_is_set())
1223 PORTAL_IOC_INIT (data);
1224 data.ioc_nal = g_nal;
1225 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1227 fprintf(stderr, "getting my NID failed: %s\n",
1230 printf(LPX64"\n", data.ioc_nid);
1234 int jt_ptl_mynid(int argc, char **argv)
1237 char hostname[1024];
1239 struct portals_cfg pcfg;
1243 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1244 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1248 if (!g_nal_is_set())
1253 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1254 fprintf(stderr, "gethostname failed: %s\n",
1261 rc = ptl_parse_nid (&mynid, nidstr);
1263 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1267 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1268 pcfg.pcfg_nid = mynid;
1270 rc = pcfg_ioctl(&pcfg);
1272 fprintf(stderr, "setting my NID failed: %s\n",
1275 printf("registered my nid "LPX64" (%s)\n",
1276 ptl_nid2u64(mynid), hostname);
1281 jt_ptl_fail_nid (int argc, char **argv)
1285 unsigned int threshold;
1286 struct portal_ioctl_data data;
1288 if (argc < 2 || argc > 3)
1290 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1294 if (!g_nal_is_set())
1297 if (!strcmp (argv[1], "_all_"))
1299 else if (ptl_parse_anynid (&nid, argv[1]) != 0)
1301 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1306 threshold = PTL_MD_THRESH_INF;
1307 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1308 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1312 PORTAL_IOC_INIT (data);
1313 data.ioc_nal = g_nal;
1315 data.ioc_count = threshold;
1317 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1319 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1322 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1328 jt_ptl_loopback (int argc, char **argv)
1333 struct portal_ioctl_data data;
1337 fprintf (stderr, "usage: %s [on|off]\n", argv[0]);
1341 if (!g_nal_is_set())
1345 if (set && ptl_parse_bool (&enable, argv[1]) != 0) {
1346 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1350 PORTAL_IOC_INIT (data);
1351 data.ioc_nal = g_nal;
1352 data.ioc_flags = enable;
1353 data.ioc_misc = set;
1355 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_LOOPBACK, &data);
1357 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1360 printf ("loopback %s\n", data.ioc_flags ? "enabled" : "disabled");
1366 jt_ptl_add_route (int argc, char **argv)
1368 struct portals_cfg pcfg;
1371 ptl_nid_t gateway_nid;
1376 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1380 if (!g_nal_is_set())
1383 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1385 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1389 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1391 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1397 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1399 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1403 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1404 pcfg.pcfg_nid = gateway_nid;
1405 pcfg.pcfg_nal = ROUTER;
1406 pcfg.pcfg_gw_nal = g_nal;
1407 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1408 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1410 rc = pcfg_ioctl(&pcfg);
1413 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1421 jt_ptl_del_route (int argc, char **argv)
1423 struct portals_cfg pcfg;
1425 ptl_nid_t nid1 = PTL_NID_ANY;
1426 ptl_nid_t nid2 = PTL_NID_ANY;
1431 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1435 if (!g_nal_is_set())
1438 if (ptl_parse_nid (&nid, argv[1]) != 0)
1440 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1445 ptl_parse_nid (&nid1, argv[2]) != 0)
1447 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1454 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1455 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1460 ptl_nid_t tmp = nid1;
1467 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1468 pcfg.pcfg_nal = ROUTER;
1469 pcfg.pcfg_gw_nal = g_nal;
1470 pcfg.pcfg_nid = nid;
1471 pcfg.pcfg_nid2 = nid1;
1472 pcfg.pcfg_nid3 = nid2;
1474 rc = pcfg_ioctl(&pcfg);
1477 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1478 ptl_nid2u64(nid), strerror (errno));
1486 jt_ptl_notify_router (int argc, char **argv)
1488 struct portals_cfg pcfg;
1497 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1502 if (ptl_parse_nid (&nid, argv[1]) != 0)
1504 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1508 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1509 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1513 gettimeofday(&now, NULL);
1517 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1518 fprintf(stderr, "Can't parse time %s\n"
1519 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1520 "or an absolute unix time in seconds\n", argv[3]);
1522 } else if (when > now.tv_sec) {
1523 fprintf (stderr, "%s specifies a time in the future\n",
1528 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1529 pcfg.pcfg_nal = ROUTER;
1530 pcfg.pcfg_gw_nal = g_nal;
1531 pcfg.pcfg_nid = nid;
1532 pcfg.pcfg_flags = enable;
1533 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1534 pcfg.pcfg_nid3 = (__u64)when;
1536 rc = pcfg_ioctl(&pcfg);
1539 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1540 ptl_nid2u64(nid), strerror (errno));
1548 jt_ptl_print_routes (int argc, char **argv)
1550 char buffer[3][128];
1551 struct portals_cfg pcfg;
1555 ptl_nid_t gateway_nid;
1560 for (index = 0;;index++)
1562 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1563 pcfg.pcfg_nal = ROUTER;
1564 pcfg.pcfg_count = index;
1566 rc = pcfg_ioctl(&pcfg);
1570 gateway_nal = pcfg.pcfg_gw_nal;
1571 gateway_nid = pcfg.pcfg_nid;
1572 nid1 = pcfg.pcfg_nid2;
1573 nid2 = pcfg.pcfg_nid3;
1574 alive = pcfg.pcfg_flags;
1576 printf ("%8s %18s : %s - %s, %s\n",
1577 nal2name (gateway_nal),
1578 ptl_nid2str (buffer[0], gateway_nid),
1579 ptl_nid2str (buffer[1], nid1),
1580 ptl_nid2str (buffer[2], nid2),
1581 alive ? "up" : "down");
1587 lwt_control(int enable, int clear)
1589 struct portal_ioctl_data data;
1592 PORTAL_IOC_INIT(data);
1593 data.ioc_flags = enable;
1594 data.ioc_misc = clear;
1596 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1600 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1606 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1607 lwt_event_t *events, int size)
1609 struct portal_ioctl_data data;
1612 PORTAL_IOC_INIT(data);
1613 data.ioc_pbuf1 = (char *)events;
1614 data.ioc_plen1 = size;
1616 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1618 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1623 /* crappy overloads */
1624 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1625 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1626 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1627 (int)data.ioc_nid2, sizeof(lwt_event_t),
1629 (int)offsetof(lwt_event_t, lwte_where));
1633 LASSERT (data.ioc_count != 0);
1634 LASSERT (data.ioc_misc != 0);
1637 *now = data.ioc_nid;
1640 *ncpu = data.ioc_count;
1642 if (totalsize != NULL)
1643 *totalsize = data.ioc_misc;
1649 lwt_get_string(char *kstr)
1652 struct portal_ioctl_data data;
1656 /* FIXME: this could maintain a symbol table since we expect to be
1657 * looking up the same strings all the time... */
1659 PORTAL_IOC_INIT(data);
1660 data.ioc_pbuf1 = kstr;
1661 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1662 data.ioc_pbuf2 = NULL;
1665 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1667 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1672 size = data.ioc_count;
1673 ustr = (char *)malloc(size);
1675 fprintf(stderr, "Can't allocate string storage of size %d\n",
1680 PORTAL_IOC_INIT(data);
1681 data.ioc_pbuf1 = kstr;
1682 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1683 data.ioc_pbuf2 = ustr;
1684 data.ioc_plen2 = size;
1686 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1688 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1693 LASSERT(strlen(ustr) == size - 1);
1698 lwt_put_string(char *ustr)
1704 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1707 # error "__WORDSIZE not defined"
1708 #elif __WORDSIZE == 32
1709 # define XFMT "%#010lx"
1710 #elif __WORDSIZE== 64
1711 # define XFMT "%#018lx"
1713 # error "Unexpected __WORDSIZE"
1715 char *where = lwt_get_string(e->lwte_where);
1720 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1721 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1722 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1723 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1726 lwt_put_string(where);
1733 get_cycles_per_usec ()
1735 FILE *f = fopen ("/proc/cpuinfo", "r");
1740 while (fgets (line, sizeof (line), f) != NULL)
1741 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1748 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1753 jt_ptl_lwt(int argc, char **argv)
1755 const int lwt_max_cpus = 32;
1758 int nevents_per_cpu;
1759 lwt_event_t *events;
1760 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1761 lwt_event_t *next_event[lwt_max_cpus];
1762 lwt_event_t *first_event[lwt_max_cpus];
1771 struct timeval tvnow;
1772 int printed_date = 0;
1777 (strcmp(argv[1], "start") &&
1778 strcmp(argv[1], "stop"))) {
1781 " %s stop [fname]\n", argv[0], argv[0]);
1785 if (!strcmp(argv[1], "start")) {
1787 if (lwt_control(0, 0) != 0)
1791 if (lwt_control(0, 1) != 0)
1795 if (lwt_control(1, 0) != 0)
1801 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1804 if (ncpus > lwt_max_cpus) {
1805 fprintf(stderr, "Too many cpus: %d (%d)\n",
1806 ncpus, lwt_max_cpus);
1810 events = (lwt_event_t *)malloc(totalspace);
1811 if (events == NULL) {
1812 fprintf(stderr, "Can't allocate %d\n", totalspace);
1816 if (lwt_control(0, 0) != 0) { /* disable */
1821 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1826 /* we want this time to be sampled at snapshot time */
1827 gettimeofday(&tvnow, NULL);
1830 f = fopen (argv[2], "w");
1832 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1838 mhz = get_cycles_per_usec();
1840 /* carve events into per-cpu slices */
1841 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1842 for (cpu = 0; cpu <= ncpus; cpu++)
1843 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1845 /* find the earliest event on each cpu */
1846 for (cpu = 0; cpu < ncpus; cpu++) {
1847 first_event[cpu] = NULL;
1849 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1851 if (e->lwte_where == NULL) /* not an event */
1854 if (first_event[cpu] == NULL ||
1855 first_event[cpu]->lwte_when > e->lwte_when)
1856 first_event[cpu] = e;
1859 next_event[cpu] = first_event[cpu];
1863 for (cpu = 0; cpu < ncpus; cpu++) {
1864 e = first_event[cpu];
1865 if (e == NULL) /* no events this cpu */
1868 if (e == cpu_event[cpu])
1869 e = cpu_event[cpu + 1] - 1;
1873 /* If there's an event immediately before the first one, this
1874 * cpu wrapped its event buffer */
1875 if (e->lwte_where == NULL)
1878 /* We should only start outputting events from the most recent
1879 * first event in any wrapped cpu. Events before this time on
1880 * other cpus won't have any events from this CPU to interleave
1882 if (t0 < first_event[cpu]->lwte_when)
1883 t0 = first_event[cpu]->lwte_when;
1887 /* find which cpu has the next event */
1889 for (i = 0; i < ncpus; i++) {
1891 if (next_event[i] == NULL) /* this cpu exhausted */
1895 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1899 if (cpu < 0) /* all cpus exhausted */
1903 /* no wrapped cpus and this is he first ever event */
1904 t0 = next_event[cpu]->lwte_when;
1907 if (t0 <= next_event[cpu]->lwte_when) {
1908 /* on or after the first event */
1909 if (!printed_date) {
1910 cycles_t du = (tnow - t0) / mhz;
1911 time_t then = tvnow.tv_sec - du/1000000;
1913 if (du % 1000000 > tvnow.tv_usec)
1916 fprintf(f, "%s", ctime(&then));
1920 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1924 if (++nlines % 10000 == 0 && f != stdout) {
1925 /* show some activity... */
1931 tlast = next_event[cpu]->lwte_when;
1934 if (next_event[cpu] == cpu_event[cpu + 1])
1935 next_event[cpu] = cpu_event[cpu];
1937 if (next_event[cpu]->lwte_where == NULL ||
1938 next_event[cpu] == first_event[cpu])
1939 next_event[cpu] = NULL;
1951 int jt_ptl_memhog(int argc, char **argv)
1953 static int gfp = 0; /* sticky! */
1955 struct portal_ioctl_data data;
1961 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1965 count = strtol(argv[1], &end, 0);
1966 if (count < 0 || *end != 0) {
1967 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1972 rc = strtol(argv[2], &end, 0);
1974 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1980 PORTAL_IOC_INIT(data);
1981 data.ioc_count = count;
1982 data.ioc_flags = gfp;
1983 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1986 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1990 printf("memhog %d OK\n", count);