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>
36 #include <sys/ioctl.h>
46 #include <portals/ipmap.h>
51 #include <netinet/in.h>
53 #endif /* __CYGWIN__ */
55 #include <portals/api-support.h>
56 #include <portals/ptlctl.h>
57 #include <portals/list.h>
58 #include <portals/lib-types.h>
59 #include <portals/socknal.h>
62 unsigned int portal_debug;
63 unsigned int portal_printk;
65 static unsigned int g_nal = 0;
73 static name2num_t nalnames[] = {
79 {"openib", OPENIBNAL},
85 {"cray_kern_nal", CRAY_KERN_NAL},
86 {"cray_user_nal", CRAY_USER_NAL},
87 {"cray_qk_nal", CRAY_QK_NAL},
92 static cfg_record_cb_t g_record_cb;
94 /* Convert a string boolean to an int; "enable" -> 1 */
95 int ptl_parse_bool (int *b, char *str) {
96 if (!strcasecmp (str, "no") ||
97 !strcasecmp (str, "n") ||
98 !strcasecmp (str, "off") ||
99 !strcasecmp (str, "down") ||
100 !strcasecmp (str, "disable"))
106 if (!strcasecmp (str, "yes") ||
107 !strcasecmp (str, "y") ||
108 !strcasecmp (str, "on") ||
109 !strcasecmp (str, "up") ||
110 !strcasecmp (str, "enable"))
119 /* Convert human readable size string to and int; "1k" -> 1000 */
120 int ptl_parse_size (int *sizep, char *str) {
124 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
157 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
164 pcfg_ioctl(struct portals_cfg *pcfg)
168 if (pcfg->pcfg_nal ==0)
169 pcfg->pcfg_nal = g_nal;
172 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
174 struct portal_ioctl_data data;
175 PORTAL_IOC_INIT (data);
176 data.ioc_pbuf1 = (char*)pcfg;
177 data.ioc_plen1 = sizeof(*pcfg);
178 /* XXX liblustre hack XXX */
179 data.ioc_nal_cmd = pcfg->pcfg_command;
180 data.ioc_nid = pcfg->pcfg_nid;
182 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
191 name2num_lookup_name (name2num_t *table, char *str)
193 while (table->name != NULL)
194 if (!strcmp (str, table->name))
202 name2num_lookup_num (name2num_t *table, int num)
204 while (table->name != NULL)
205 if (num == table->num)
213 ptl_name2nal (char *str)
215 name2num_t *e = name2num_lookup_name (nalnames, str);
217 return ((e == NULL) ? -1 : e->num);
223 name2num_t *e = name2num_lookup_num (nalnames, nal);
225 return ((e == NULL) ? "???" : e->name);
228 #ifdef HAVE_GETHOSTBYNAME
229 static struct hostent *
230 ptl_gethostbyname(char * hname) {
232 he = gethostbyname(hname);
237 fprintf(stderr, "Unable to resolve hostname: %s\n",
241 fprintf(stderr, "gethostbyname error for %s: %s\n",
242 hname, strerror(h_errno));
252 ptl_parse_port (int *port, char *str)
256 *port = strtol (str, &end, 0);
258 if (*end == 0 && /* parsed whole string */
259 *port > 0 && *port < 65536) /* minimal sanity check */
266 ptl_parse_time (time_t *t, char *str)
272 *t = strtol (str, &end, 0);
273 if (*end == 0) /* parsed whole string */
276 memset (&tm, 0, sizeof (tm));
277 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
278 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
279 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
283 tm.tm_mon--; /* convert to 0 == Jan */
284 tm.tm_year -= 1900; /* y2k quirk */
285 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
288 if (*t == (time_t)-1)
295 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
302 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
303 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
304 (c & ~0xff) == 0 && (d & ~0xff) == 0)
306 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
314 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
316 #ifdef HAVE_GETHOSTBYNAME
320 if (!strcmp (str, "_all_")) {
325 if (ptl_parse_ipquad(ipaddrp, str) == 0)
328 #ifdef HAVE_GETHOSTBYNAME
329 if ((('a' <= str[0] && str[0] <= 'z') ||
330 ('A' <= str[0] && str[0] <= 'Z')) &&
331 (he = ptl_gethostbyname (str)) != NULL) {
332 __u32 addr = *(__u32 *)he->h_addr;
334 *ipaddrp = ntohl(addr); /* HOST byte order */
343 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
345 #ifdef HAVE_GETHOSTBYNAME
350 net_ip = htonl (ipaddr);
351 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
353 strcpy(str, he->h_name);
359 sprintf (str, "%d.%d.%d.%d",
360 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
361 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
366 ptl_parse_nid (ptl_nid_t *nidp, char *str)
370 unsigned long long ullval;
372 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
374 *nidp = (ptl_nid_t)ipaddr;
376 *nidp = (((ptl_nid_t)ipaddr & PNAL_HOSTID_MASK) << PNAL_VNODE_SHIFT);
381 ullval = strtoull(str, &end, 0);
382 if (end != str && *end == 0) {
383 /* parsed whole non-empty string */
384 *nidp = (ptl_nid_t)ullval;
392 ptl_parse_anynid (ptl_nid_t *nidp, char *str)
394 if (!strcmp (str, "_all_")) {
399 return ptl_parse_nid(nidp, str);
402 __u64 ptl_nid2u64(ptl_nid_t nid)
404 switch (sizeof (nid)) {
410 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
418 ptl_nid2str (char *buffer, ptl_nid_t nid)
420 __u64 nid64 = ptl_nid2u64(nid);
421 #ifdef HAVE_GETHOSTBYNAME
422 struct hostent *he = 0;
424 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
425 * TCP addresses in the 0.x.x.x subnet are not in use. This can
426 * happen on routers and slows things down a _lot_. Bug 3442. */
427 if (nid & 0xff000000) {
428 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
430 he = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET);
434 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
436 #endif /* HAVE_GETHOSTBYNAME */
437 sprintf(buffer, LPX64, nid64);
445 fprintf (stderr, "Error: you must run the 'network' command first.\n");
452 int g_nal_is_compatible (char *cmd, ...)
457 if (!g_nal_is_set ())
463 nal = va_arg (ap, int);
464 } while (nal != 0 && nal != g_nal);
472 /* Don't complain verbosely if we've not been passed a command
473 * name to complain about! */
474 fprintf (stderr, "Command %s not compatible with nal %s\n",
475 cmd, nal2name (g_nal));
481 sock_write (int cfd, void *buffer, int nob)
485 int rc = write (cfd, buffer, nob);
497 fprintf (stderr, "Unexpected zero sock_write\n");
502 buffer = (char *)buffer + nob;
509 sock_read (int cfd, void *buffer, int nob)
513 int rc = read (cfd, buffer, nob);
523 if (rc == 0) /* EOF */
525 errno = ECONNABORTED;
530 buffer = (char *)buffer + nob;
536 int ptl_initialize(int argc, char **argv)
538 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
543 int jt_ptl_network(int argc, char **argv)
549 (nal = ptl_name2nal (argv[1])) >= 0) {
554 fprintf(stderr, "usage: %s \n", argv[0]);
555 for (entry = nalnames; entry->name != NULL; entry++)
556 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
557 fprintf(stderr, ">\n");
562 jt_ptl_print_interfaces (int argc, char **argv)
564 struct portals_cfg pcfg;
569 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
572 for (index = 0;;index++) {
573 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
574 pcfg.pcfg_count = index;
576 rc = pcfg_ioctl (&pcfg);
580 printf ("%s: (%s/%s) npeer %d nroute %d\n",
581 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
582 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
583 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
584 pcfg.pcfg_fd, pcfg.pcfg_count);
588 printf ("<no interfaces>\n");
593 jt_ptl_add_interface (int argc, char **argv)
595 struct portals_cfg pcfg;
598 __u32 netmask = 0xffffff00;
603 if (argc < 2 || argc > 3) {
604 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
608 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
611 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
612 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
617 count = strtol(argv[2], &end, 0);
618 if (count > 0 && count < 32 && *end == 0) {
620 for (i = count; i > 0; i--)
621 netmask = netmask|(1<<(32-i));
622 } else if (ptl_parse_ipquad(&netmask, argv[2]) != 0) {
623 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
628 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
629 pcfg.pcfg_id = ipaddr;
630 pcfg.pcfg_misc = netmask;
632 rc = pcfg_ioctl (&pcfg);
634 fprintf (stderr, "failed to add interface: %s\n",
643 jt_ptl_del_interface (int argc, char **argv)
645 struct portals_cfg pcfg;
650 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
654 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
658 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
659 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
663 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
664 pcfg.pcfg_id = ipaddr;
666 rc = pcfg_ioctl (&pcfg);
668 fprintf (stderr, "failed to delete interface: %s\n",
677 jt_ptl_print_peers (int argc, char **argv)
679 struct portals_cfg pcfg;
684 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
685 OPENIBNAL, IIBNAL, VIBNAL, 0))
688 for (index = 0;;index++) {
689 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
690 pcfg.pcfg_count = index;
692 rc = pcfg_ioctl (&pcfg);
696 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
697 printf (LPX64"[%d]%s@%s:%d #%d\n",
698 pcfg.pcfg_nid, pcfg.pcfg_wait,
699 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
700 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
701 pcfg.pcfg_misc, pcfg.pcfg_count);
702 else if (g_nal_is_compatible(NULL, RANAL, OPENIBNAL, 0))
703 printf (LPX64"[%d]@%s:%d\n",
704 pcfg.pcfg_nid, pcfg.pcfg_wait,
705 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
708 printf (LPX64"[%d]\n",
709 pcfg.pcfg_nid, pcfg.pcfg_wait);
713 printf ("<no peers>\n");
718 jt_ptl_add_peer (int argc, char **argv)
720 struct portals_cfg pcfg;
726 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
727 OPENIBNAL, IIBNAL, VIBNAL, 0))
730 if (g_nal_is_compatible(NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
732 fprintf (stderr, "usage(tcp,openib,ra): %s nid ipaddr port\n",
736 } else if (argc != 2) {
737 fprintf (stderr, "usage(iib,vib): %s nid\n", argv[0]);
741 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
742 nid == PTL_NID_ANY) {
743 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
747 if (g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, RANAL, 0)) {
748 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
749 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
753 if (ptl_parse_port (&port, argv[3]) != 0) {
754 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
759 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
762 pcfg.pcfg_misc = port;
764 rc = pcfg_ioctl (&pcfg);
766 fprintf (stderr, "failed to add peer: %s\n",
775 jt_ptl_del_peer (int argc, char **argv)
777 struct portals_cfg pcfg;
778 ptl_nid_t nid = PTL_NID_ANY;
780 int single_share = 0;
784 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
785 OPENIBNAL, IIBNAL, VIBNAL, 0))
788 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
790 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
794 } else if (argc > 3) {
795 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
800 ptl_parse_anynid (&nid, argv[1]) != 0) {
801 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
806 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
808 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
809 fprintf (stderr, "Can't parse ip addr: %s\n",
817 if (!strcmp (argv[argidx], "single_share")) {
820 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
825 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
828 pcfg.pcfg_flags = single_share;
830 rc = pcfg_ioctl (&pcfg);
832 fprintf (stderr, "failed to remove peer: %s\n",
841 jt_ptl_print_connections (int argc, char **argv)
843 struct portals_cfg pcfg;
848 if (!g_nal_is_compatible (argv[0], SOCKNAL, RANAL,
849 OPENIBNAL, IIBNAL, VIBNAL, 0))
852 for (index = 0;;index++) {
853 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
854 pcfg.pcfg_count = index;
856 rc = pcfg_ioctl (&pcfg);
860 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
861 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
862 pcfg.pcfg_gw_nal, /* scheduler */
863 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
865 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
866 pcfg.pcfg_misc, /* remote port */
867 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
868 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
869 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
870 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
871 pcfg.pcfg_count, /* tx buffer size */
872 pcfg.pcfg_size, /* rx buffer size */
873 pcfg.pcfg_wait ? "nagle" : "nonagle");
874 else if (g_nal_is_compatible (NULL, RANAL, 0))
875 printf ("[%d]"LPX64"\n",
876 pcfg.pcfg_id, /* device id */
884 printf ("<no connections>\n");
888 int jt_ptl_connect(int argc, char **argv)
891 /* no connect() support */
893 #else /* HAVE_CONNECT */
894 struct portals_cfg pcfg;
895 struct sockaddr_in srvaddr;
896 struct sockaddr_in locaddr;
900 int type = SOCKNAL_CONN_ANY;
905 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
909 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
912 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
914 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
918 if (ptl_parse_port (&port, argv[2]) != 0) {
919 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
924 for (flag = argv[3]; *flag != 0; flag++)
928 if (type != SOCKNAL_CONN_ANY) {
929 fprintf(stderr, "Can't flag type twice\n");
932 type = SOCKNAL_CONN_BULK_IN;
936 if (type != SOCKNAL_CONN_ANY) {
937 fprintf(stderr, "Can't flag type twice\n");
940 type = SOCKNAL_CONN_BULK_OUT;
944 if (type != SOCKNAL_CONN_ANY) {
945 fprintf(stderr, "Can't flag type twice\n");
948 type = SOCKNAL_CONN_CONTROL;
952 fprintf (stderr, "unrecognised flag '%c'\n",
957 memset(&locaddr, 0, sizeof(locaddr));
958 locaddr.sin_family = AF_INET;
959 locaddr.sin_addr.s_addr = INADDR_ANY;
961 memset(&srvaddr, 0, sizeof(srvaddr));
962 srvaddr.sin_family = AF_INET;
963 srvaddr.sin_port = htons(port);
964 srvaddr.sin_addr.s_addr = htonl(ipaddr);
967 for (rport = IPPORT_RESERVED - 1; rport > IPPORT_RESERVED / 2; --rport) {
968 fd = socket(PF_INET, SOCK_STREAM, 0);
970 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
975 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
978 locaddr.sin_port = htons(rport);
979 rc = bind(fd, (struct sockaddr *)&locaddr, sizeof(locaddr));
980 if (rc == 0 || errno == EACCES) {
981 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
984 } else if (errno != EADDRINUSE) {
985 fprintf(stderr, "Error connecting to host: %s\n", strerror(errno));
989 } else if (errno != EADDRINUSE) {
990 fprintf(stderr, "Error binding to port %d: %d: %s\n", port, errno, strerror(errno));
996 if (rport == IPPORT_RESERVED / 2) {
998 "Warning: all privileged ports are in use.\n");
1002 printf("Connected host: %s type: %s\n",
1004 (type == SOCKNAL_CONN_ANY) ? "A" :
1005 (type == SOCKNAL_CONN_CONTROL) ? "C" :
1006 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
1007 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
1009 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
1010 pcfg.pcfg_nal = g_nal;
1012 pcfg.pcfg_misc = type;
1014 rc = pcfg_ioctl(&pcfg);
1016 fprintf(stderr, "failed to register fd with portals: %s\n",
1022 printf("Connection to %s registered with socknal\n", argv[1]);
1026 fprintf(stderr, "close failed: %d\n", rc);
1029 #endif /* HAVE_CONNECT */
1032 int jt_ptl_disconnect(int argc, char **argv)
1034 struct portals_cfg pcfg;
1035 ptl_nid_t nid = PTL_NID_ANY;
1040 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1044 if (!g_nal_is_compatible (NULL, SOCKNAL, RANAL,
1045 OPENIBNAL, IIBNAL, VIBNAL, 0))
1049 ptl_parse_anynid (&nid, argv[1]) != 0) {
1050 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
1054 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
1056 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1057 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
1061 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
1062 pcfg.pcfg_nid = nid;
1063 pcfg.pcfg_id = ipaddr;
1065 rc = pcfg_ioctl(&pcfg);
1067 fprintf(stderr, "failed to remove connection: %s\n",
1075 int jt_ptl_push_connection (int argc, char **argv)
1077 struct portals_cfg pcfg;
1079 ptl_nid_t nid = PTL_NID_ANY;
1083 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
1087 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1091 ptl_parse_anynid (&nid, argv[1]) != 0) {
1092 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1097 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1098 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1101 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1102 pcfg.pcfg_nid = nid;
1103 pcfg.pcfg_id = ipaddr;
1105 rc = pcfg_ioctl(&pcfg);
1107 fprintf(stderr, "failed to push connection: %s\n",
1116 jt_ptl_print_active_txs (int argc, char **argv)
1118 struct portals_cfg pcfg;
1122 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1125 for (index = 0;;index++) {
1126 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1127 pcfg.pcfg_count = index;
1129 rc = pcfg_ioctl(&pcfg);
1133 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1135 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1136 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1137 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1138 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1143 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1144 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1145 pcfg.pcfg_flags >> 2);
1149 printf ("<no active descs>\n");
1153 int jt_ptl_ping(int argc, char **argv)
1160 struct portal_ioctl_data data;
1163 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1167 if (!g_nal_is_set())
1170 if (ptl_parse_nid (&nid, argv[1]) != 0)
1172 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1178 count = atol(argv[2]);
1180 if (count < 0 || count > 20000)
1182 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1188 size= atol(argv[3]);
1191 timeout = atol (argv[4]);
1193 PORTAL_IOC_INIT (data);
1194 data.ioc_count = count;
1195 data.ioc_size = size;
1197 data.ioc_nal = g_nal;
1198 data.ioc_timeout = timeout;
1200 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1202 fprintf(stderr, "failed to start pinger: %s\n",
1209 int jt_ptl_shownid(int argc, char **argv)
1211 struct portal_ioctl_data data;
1215 fprintf(stderr, "usage: %s\n", argv[0]);
1219 if (!g_nal_is_set())
1222 PORTAL_IOC_INIT (data);
1223 data.ioc_nal = g_nal;
1224 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1226 fprintf(stderr, "getting my NID failed: %s\n",
1229 printf(LPX64"\n", data.ioc_nid);
1233 int jt_ptl_mynid(int argc, char **argv)
1236 char hostname[1024];
1238 struct portals_cfg pcfg;
1242 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1243 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1247 if (!g_nal_is_set())
1252 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1253 fprintf(stderr, "gethostname failed: %s\n",
1260 rc = ptl_parse_nid (&mynid, nidstr);
1262 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1266 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1267 pcfg.pcfg_nid = mynid;
1269 rc = pcfg_ioctl(&pcfg);
1271 fprintf(stderr, "setting my NID failed: %s\n",
1274 printf("registered my nid "LPX64" (%s)\n",
1275 ptl_nid2u64(mynid), hostname);
1280 jt_ptl_fail_nid (int argc, char **argv)
1284 unsigned int threshold;
1285 struct portal_ioctl_data data;
1287 if (argc < 2 || argc > 3)
1289 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1293 if (!g_nal_is_set())
1296 if (!strcmp (argv[1], "_all_"))
1298 else if (ptl_parse_anynid (&nid, argv[1]) != 0)
1300 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1305 threshold = PTL_MD_THRESH_INF;
1306 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1307 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1311 PORTAL_IOC_INIT (data);
1312 data.ioc_nal = g_nal;
1314 data.ioc_count = threshold;
1316 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1318 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1321 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1327 jt_ptl_add_route (int argc, char **argv)
1329 struct portals_cfg pcfg;
1332 ptl_nid_t gateway_nid;
1337 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1341 if (!g_nal_is_set())
1344 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1346 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1350 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1352 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1358 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1360 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1364 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1365 pcfg.pcfg_nid = gateway_nid;
1366 pcfg.pcfg_nal = ROUTER;
1367 pcfg.pcfg_gw_nal = g_nal;
1368 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1369 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1371 rc = pcfg_ioctl(&pcfg);
1374 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1382 jt_ptl_del_route (int argc, char **argv)
1384 struct portals_cfg pcfg;
1386 ptl_nid_t nid1 = PTL_NID_ANY;
1387 ptl_nid_t nid2 = PTL_NID_ANY;
1392 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1396 if (!g_nal_is_set())
1399 if (ptl_parse_nid (&nid, argv[1]) != 0)
1401 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1406 ptl_parse_nid (&nid1, argv[2]) != 0)
1408 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1415 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1416 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1421 ptl_nid_t tmp = nid1;
1428 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1429 pcfg.pcfg_nal = ROUTER;
1430 pcfg.pcfg_gw_nal = g_nal;
1431 pcfg.pcfg_nid = nid;
1432 pcfg.pcfg_nid2 = nid1;
1433 pcfg.pcfg_nid3 = nid2;
1435 rc = pcfg_ioctl(&pcfg);
1438 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1439 ptl_nid2u64(nid), strerror (errno));
1447 jt_ptl_notify_router (int argc, char **argv)
1449 struct portals_cfg pcfg;
1458 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1463 if (ptl_parse_nid (&nid, argv[1]) != 0)
1465 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1469 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1470 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1474 gettimeofday(&now, NULL);
1478 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1479 fprintf(stderr, "Can't parse time %s\n"
1480 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1481 "or an absolute unix time in seconds\n", argv[3]);
1483 } else if (when > now.tv_sec) {
1484 fprintf (stderr, "%s specifies a time in the future\n",
1489 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1490 pcfg.pcfg_nal = ROUTER;
1491 pcfg.pcfg_gw_nal = g_nal;
1492 pcfg.pcfg_nid = nid;
1493 pcfg.pcfg_flags = enable;
1494 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1495 pcfg.pcfg_nid3 = (__u64)when;
1497 rc = pcfg_ioctl(&pcfg);
1500 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1501 ptl_nid2u64(nid), strerror (errno));
1509 jt_ptl_print_routes (int argc, char **argv)
1511 char buffer[3][128];
1512 struct portals_cfg pcfg;
1516 ptl_nid_t gateway_nid;
1521 for (index = 0;;index++)
1523 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1524 pcfg.pcfg_nal = ROUTER;
1525 pcfg.pcfg_count = index;
1527 rc = pcfg_ioctl(&pcfg);
1531 gateway_nal = pcfg.pcfg_gw_nal;
1532 gateway_nid = pcfg.pcfg_nid;
1533 nid1 = pcfg.pcfg_nid2;
1534 nid2 = pcfg.pcfg_nid3;
1535 alive = pcfg.pcfg_flags;
1537 printf ("%8s %18s : %s - %s, %s\n",
1538 nal2name (gateway_nal),
1539 ptl_nid2str (buffer[0], gateway_nid),
1540 ptl_nid2str (buffer[1], nid1),
1541 ptl_nid2str (buffer[2], nid2),
1542 alive ? "up" : "down");
1548 lwt_control(int enable, int clear)
1550 struct portal_ioctl_data data;
1553 PORTAL_IOC_INIT(data);
1554 data.ioc_flags = enable;
1555 data.ioc_misc = clear;
1557 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1561 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1567 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1568 lwt_event_t *events, int size)
1570 struct portal_ioctl_data data;
1573 PORTAL_IOC_INIT(data);
1574 data.ioc_pbuf1 = (char *)events;
1575 data.ioc_plen1 = size;
1577 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1579 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1584 /* crappy overloads */
1585 if (data.ioc_nid2 != sizeof(lwt_event_t) ||
1586 data.ioc_nid3 != offsetof(lwt_event_t, lwte_where)) {
1587 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1588 (int)data.ioc_nid2, sizeof(lwt_event_t),
1590 (int)offsetof(lwt_event_t, lwte_where));
1594 LASSERT (data.ioc_count != 0);
1595 LASSERT (data.ioc_misc != 0);
1598 *now = data.ioc_nid;
1601 *ncpu = data.ioc_count;
1603 if (totalsize != NULL)
1604 *totalsize = data.ioc_misc;
1610 lwt_get_string(char *kstr)
1613 struct portal_ioctl_data data;
1617 /* FIXME: this could maintain a symbol table since we expect to be
1618 * looking up the same strings all the time... */
1620 PORTAL_IOC_INIT(data);
1621 data.ioc_pbuf1 = kstr;
1622 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1623 data.ioc_pbuf2 = NULL;
1626 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1628 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1633 size = data.ioc_count;
1634 ustr = (char *)malloc(size);
1636 fprintf(stderr, "Can't allocate string storage of size %d\n",
1641 PORTAL_IOC_INIT(data);
1642 data.ioc_pbuf1 = kstr;
1643 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1644 data.ioc_pbuf2 = ustr;
1645 data.ioc_plen2 = size;
1647 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1649 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1654 LASSERT(strlen(ustr) == size - 1);
1659 lwt_put_string(char *ustr)
1665 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1668 # error "__WORDSIZE not defined"
1669 #elif __WORDSIZE == 32
1670 # define XFMT "%#010lx"
1671 #elif __WORDSIZE== 64
1672 # define XFMT "%#018lx"
1674 # error "Unexpected __WORDSIZE"
1676 char *where = lwt_get_string(e->lwte_where);
1681 fprintf(f, XFMT" "XFMT" "XFMT" "XFMT": "XFMT" %2d %10.6f %10.2f %s\n",
1682 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1683 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1684 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1687 lwt_put_string(where);
1694 get_cycles_per_usec ()
1696 FILE *f = fopen ("/proc/cpuinfo", "r");
1701 while (fgets (line, sizeof (line), f) != NULL)
1702 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1709 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1714 jt_ptl_lwt(int argc, char **argv)
1716 const int lwt_max_cpus = 32;
1719 int nevents_per_cpu;
1720 lwt_event_t *events;
1721 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1722 lwt_event_t *next_event[lwt_max_cpus];
1723 lwt_event_t *first_event[lwt_max_cpus];
1732 struct timeval tvnow;
1733 int printed_date = 0;
1738 (strcmp(argv[1], "start") &&
1739 strcmp(argv[1], "stop"))) {
1742 " %s stop [fname]\n", argv[0], argv[0]);
1746 if (!strcmp(argv[1], "start")) {
1748 if (lwt_control(0, 0) != 0)
1752 if (lwt_control(0, 1) != 0)
1756 if (lwt_control(1, 0) != 0)
1762 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1765 if (ncpus > lwt_max_cpus) {
1766 fprintf(stderr, "Too many cpus: %d (%d)\n",
1767 ncpus, lwt_max_cpus);
1771 events = (lwt_event_t *)malloc(totalspace);
1772 if (events == NULL) {
1773 fprintf(stderr, "Can't allocate %d\n", totalspace);
1777 if (lwt_control(0, 0) != 0) { /* disable */
1782 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1787 /* we want this time to be sampled at snapshot time */
1788 gettimeofday(&tvnow, NULL);
1791 f = fopen (argv[2], "w");
1793 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1799 mhz = get_cycles_per_usec();
1801 /* carve events into per-cpu slices */
1802 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1803 for (cpu = 0; cpu <= ncpus; cpu++)
1804 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1806 /* find the earliest event on each cpu */
1807 for (cpu = 0; cpu < ncpus; cpu++) {
1808 first_event[cpu] = NULL;
1810 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1812 if (e->lwte_where == NULL) /* not an event */
1815 if (first_event[cpu] == NULL ||
1816 first_event[cpu]->lwte_when > e->lwte_when)
1817 first_event[cpu] = e;
1820 next_event[cpu] = first_event[cpu];
1824 for (cpu = 0; cpu < ncpus; cpu++) {
1825 e = first_event[cpu];
1826 if (e == NULL) /* no events this cpu */
1829 if (e == cpu_event[cpu])
1830 e = cpu_event[cpu + 1] - 1;
1834 /* If there's an event immediately before the first one, this
1835 * cpu wrapped its event buffer */
1836 if (e->lwte_where == NULL)
1839 /* We should only start outputting events from the most recent
1840 * first event in any wrapped cpu. Events before this time on
1841 * other cpus won't have any events from this CPU to interleave
1843 if (t0 < first_event[cpu]->lwte_when)
1844 t0 = first_event[cpu]->lwte_when;
1848 /* find which cpu has the next event */
1850 for (i = 0; i < ncpus; i++) {
1852 if (next_event[i] == NULL) /* this cpu exhausted */
1856 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1860 if (cpu < 0) /* all cpus exhausted */
1864 /* no wrapped cpus and this is he first ever event */
1865 t0 = next_event[cpu]->lwte_when;
1868 if (t0 <= next_event[cpu]->lwte_when) {
1869 /* on or after the first event */
1870 if (!printed_date) {
1871 cycles_t du = (tnow - t0) / mhz;
1872 time_t then = tvnow.tv_sec - du/1000000;
1874 if (du % 1000000 > tvnow.tv_usec)
1877 fprintf(f, "%s", ctime(&then));
1881 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1885 if (++nlines % 10000 == 0 && f != stdout) {
1886 /* show some activity... */
1892 tlast = next_event[cpu]->lwte_when;
1895 if (next_event[cpu] == cpu_event[cpu + 1])
1896 next_event[cpu] = cpu_event[cpu];
1898 if (next_event[cpu]->lwte_where == NULL ||
1899 next_event[cpu] == first_event[cpu])
1900 next_event[cpu] = NULL;
1912 int jt_ptl_memhog(int argc, char **argv)
1914 static int gfp = 0; /* sticky! */
1916 struct portal_ioctl_data data;
1922 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1926 count = strtol(argv[1], &end, 0);
1927 if (count < 0 || *end != 0) {
1928 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1933 rc = strtol(argv[2], &end, 0);
1935 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1941 PORTAL_IOC_INIT(data);
1942 data.ioc_count = count;
1943 data.ioc_flags = gfp;
1944 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1947 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1951 printf("memhog %d OK\n", count);