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)
1332 unsigned int threshold;
1335 struct portal_ioctl_data data;
1339 fprintf (stderr, "usage: %s [on|off]\n", argv[0]);
1343 if (!g_nal_is_set())
1347 if (set && ptl_parse_bool (&enable, argv[1]) != 0) {
1348 fprintf (stderr, "Can't parse boolean %s\n", argv[1]);
1352 PORTAL_IOC_INIT (data);
1353 data.ioc_nal = g_nal;
1354 data.ioc_flags = enable;
1355 data.ioc_misc = set;
1357 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_LOOPBACK, &data);
1359 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1362 printf ("loopback %s\n", data.ioc_flags ? "enabled" : "disabled");
1368 jt_ptl_add_route (int argc, char **argv)
1370 struct portals_cfg pcfg;
1373 ptl_nid_t gateway_nid;
1378 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1382 if (!g_nal_is_set())
1385 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1387 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1391 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1393 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1399 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1401 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1405 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1406 pcfg.pcfg_nid = gateway_nid;
1407 pcfg.pcfg_nal = ROUTER;
1408 pcfg.pcfg_gw_nal = g_nal;
1409 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1410 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1412 rc = pcfg_ioctl(&pcfg);
1415 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1423 jt_ptl_del_route (int argc, char **argv)
1425 struct portals_cfg pcfg;
1427 ptl_nid_t nid1 = PTL_NID_ANY;
1428 ptl_nid_t nid2 = PTL_NID_ANY;
1433 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1437 if (!g_nal_is_set())
1440 if (ptl_parse_nid (&nid, argv[1]) != 0)
1442 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1447 ptl_parse_nid (&nid1, argv[2]) != 0)
1449 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1456 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1457 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1462 ptl_nid_t tmp = nid1;
1469 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1470 pcfg.pcfg_nal = ROUTER;
1471 pcfg.pcfg_gw_nal = g_nal;
1472 pcfg.pcfg_nid = nid;
1473 pcfg.pcfg_nid2 = nid1;
1474 pcfg.pcfg_nid3 = nid2;
1476 rc = pcfg_ioctl(&pcfg);
1479 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1480 ptl_nid2u64(nid), strerror (errno));
1488 jt_ptl_notify_router (int argc, char **argv)
1490 struct portals_cfg pcfg;
1499 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1504 if (ptl_parse_nid (&nid, argv[1]) != 0)
1506 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1510 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1511 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1515 gettimeofday(&now, NULL);
1519 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1520 fprintf(stderr, "Can't parse time %s\n"
1521 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1522 "or an absolute unix time in seconds\n", argv[3]);
1524 } else if (when > now.tv_sec) {
1525 fprintf (stderr, "%s specifies a time in the future\n",
1530 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1531 pcfg.pcfg_nal = ROUTER;
1532 pcfg.pcfg_gw_nal = g_nal;
1533 pcfg.pcfg_nid = nid;
1534 pcfg.pcfg_flags = enable;
1535 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1536 pcfg.pcfg_nid3 = (__u64)when;
1538 rc = pcfg_ioctl(&pcfg);
1541 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1542 ptl_nid2u64(nid), strerror (errno));
1550 jt_ptl_print_routes (int argc, char **argv)
1552 char buffer[3][128];
1553 struct portals_cfg pcfg;
1557 ptl_nid_t gateway_nid;
1562 for (index = 0;;index++)
1564 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1565 pcfg.pcfg_nal = ROUTER;
1566 pcfg.pcfg_count = index;
1568 rc = pcfg_ioctl(&pcfg);
1572 gateway_nal = pcfg.pcfg_gw_nal;
1573 gateway_nid = pcfg.pcfg_nid;
1574 nid1 = pcfg.pcfg_nid2;
1575 nid2 = pcfg.pcfg_nid3;
1576 alive = pcfg.pcfg_flags;
1578 printf ("%8s %18s : %s - %s, %s\n",
1579 nal2name (gateway_nal),
1580 ptl_nid2str (buffer[0], gateway_nid),
1581 ptl_nid2str (buffer[1], nid1),
1582 ptl_nid2str (buffer[2], nid2),
1583 alive ? "up" : "down");
1589 lwt_control(int enable, int clear)
1591 struct portal_ioctl_data data;
1594 PORTAL_IOC_INIT(data);
1595 data.ioc_flags = enable;
1596 data.ioc_misc = clear;
1598 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1602 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1608 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1609 lwt_event_t *events, int size)
1611 struct portal_ioctl_data data;
1614 PORTAL_IOC_INIT(data);
1615 data.ioc_pbuf1 = (char *)events;
1616 data.ioc_plen1 = size;
1618 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1620 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1625 /* crappy overloads */
1626 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1627 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1628 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1629 (int)data.ioc_nid2, sizeof(lwt_event_t),
1631 (int)offsetof(lwt_event_t, lwte_where));
1635 LASSERT (data.ioc_count != 0);
1636 LASSERT (data.ioc_misc != 0);
1639 *now = data.ioc_nid;
1642 *ncpu = data.ioc_count;
1644 if (totalsize != NULL)
1645 *totalsize = data.ioc_misc;
1651 lwt_get_string(char *kstr)
1654 struct portal_ioctl_data data;
1658 /* FIXME: this could maintain a symbol table since we expect to be
1659 * looking up the same strings all the time... */
1661 PORTAL_IOC_INIT(data);
1662 data.ioc_pbuf1 = kstr;
1663 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1664 data.ioc_pbuf2 = NULL;
1667 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1669 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1674 size = data.ioc_count;
1675 ustr = (char *)malloc(size);
1677 fprintf(stderr, "Can't allocate string storage of size %d\n",
1682 PORTAL_IOC_INIT(data);
1683 data.ioc_pbuf1 = kstr;
1684 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1685 data.ioc_pbuf2 = ustr;
1686 data.ioc_plen2 = size;
1688 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1690 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1695 LASSERT(strlen(ustr) == size - 1);
1700 lwt_put_string(char *ustr)
1706 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1709 # error "__WORDSIZE not defined"
1710 #elif __WORDSIZE == 32
1711 # define XFMT "%#010lx"
1712 #elif __WORDSIZE== 64
1713 # define XFMT "%#018lx"
1715 # error "Unexpected __WORDSIZE"
1717 char *where = lwt_get_string(e->lwte_where);
1722 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1723 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1724 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1725 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1728 lwt_put_string(where);
1735 get_cycles_per_usec ()
1737 FILE *f = fopen ("/proc/cpuinfo", "r");
1742 while (fgets (line, sizeof (line), f) != NULL)
1743 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1750 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1755 jt_ptl_lwt(int argc, char **argv)
1757 const int lwt_max_cpus = 32;
1760 int nevents_per_cpu;
1761 lwt_event_t *events;
1762 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1763 lwt_event_t *next_event[lwt_max_cpus];
1764 lwt_event_t *first_event[lwt_max_cpus];
1773 struct timeval tvnow;
1774 int printed_date = 0;
1779 (strcmp(argv[1], "start") &&
1780 strcmp(argv[1], "stop"))) {
1783 " %s stop [fname]\n", argv[0], argv[0]);
1787 if (!strcmp(argv[1], "start")) {
1789 if (lwt_control(0, 0) != 0)
1793 if (lwt_control(0, 1) != 0)
1797 if (lwt_control(1, 0) != 0)
1803 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1806 if (ncpus > lwt_max_cpus) {
1807 fprintf(stderr, "Too many cpus: %d (%d)\n",
1808 ncpus, lwt_max_cpus);
1812 events = (lwt_event_t *)malloc(totalspace);
1813 if (events == NULL) {
1814 fprintf(stderr, "Can't allocate %d\n", totalspace);
1818 if (lwt_control(0, 0) != 0) { /* disable */
1823 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1828 /* we want this time to be sampled at snapshot time */
1829 gettimeofday(&tvnow, NULL);
1832 f = fopen (argv[2], "w");
1834 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1840 mhz = get_cycles_per_usec();
1842 /* carve events into per-cpu slices */
1843 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1844 for (cpu = 0; cpu <= ncpus; cpu++)
1845 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1847 /* find the earliest event on each cpu */
1848 for (cpu = 0; cpu < ncpus; cpu++) {
1849 first_event[cpu] = NULL;
1851 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1853 if (e->lwte_where == NULL) /* not an event */
1856 if (first_event[cpu] == NULL ||
1857 first_event[cpu]->lwte_when > e->lwte_when)
1858 first_event[cpu] = e;
1861 next_event[cpu] = first_event[cpu];
1865 for (cpu = 0; cpu < ncpus; cpu++) {
1866 e = first_event[cpu];
1867 if (e == NULL) /* no events this cpu */
1870 if (e == cpu_event[cpu])
1871 e = cpu_event[cpu + 1] - 1;
1875 /* If there's an event immediately before the first one, this
1876 * cpu wrapped its event buffer */
1877 if (e->lwte_where == NULL)
1880 /* We should only start outputting events from the most recent
1881 * first event in any wrapped cpu. Events before this time on
1882 * other cpus won't have any events from this CPU to interleave
1884 if (t0 < first_event[cpu]->lwte_when)
1885 t0 = first_event[cpu]->lwte_when;
1889 /* find which cpu has the next event */
1891 for (i = 0; i < ncpus; i++) {
1893 if (next_event[i] == NULL) /* this cpu exhausted */
1897 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1901 if (cpu < 0) /* all cpus exhausted */
1905 /* no wrapped cpus and this is he first ever event */
1906 t0 = next_event[cpu]->lwte_when;
1909 if (t0 <= next_event[cpu]->lwte_when) {
1910 /* on or after the first event */
1911 if (!printed_date) {
1912 cycles_t du = (tnow - t0) / mhz;
1913 time_t then = tvnow.tv_sec - du/1000000;
1915 if (du % 1000000 > tvnow.tv_usec)
1918 fprintf(f, "%s", ctime(&then));
1922 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1926 if (++nlines % 10000 == 0 && f != stdout) {
1927 /* show some activity... */
1933 tlast = next_event[cpu]->lwte_when;
1936 if (next_event[cpu] == cpu_event[cpu + 1])
1937 next_event[cpu] = cpu_event[cpu];
1939 if (next_event[cpu]->lwte_where == NULL ||
1940 next_event[cpu] == first_event[cpu])
1941 next_event[cpu] = NULL;
1953 int jt_ptl_memhog(int argc, char **argv)
1955 static int gfp = 0; /* sticky! */
1957 struct portal_ioctl_data data;
1963 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1967 count = strtol(argv[1], &end, 0);
1968 if (count < 0 || *end != 0) {
1969 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1974 rc = strtol(argv[2], &end, 0);
1976 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1982 PORTAL_IOC_INIT(data);
1983 data.ioc_count = count;
1984 data.ioc_flags = gfp;
1985 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1988 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1992 printf("memhog %d OK\n", count);