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>
26 #include <sys/socket.h>
27 #include <netinet/tcp.h>
32 #include <sys/ioctl.h>
41 #include <netinet/in.h>
43 #endif /* __CYGWIN__ */
45 #include <portals/api-support.h>
46 #include <portals/ptlctl.h>
47 #include <portals/list.h>
48 #include <portals/lib-types.h>
49 #include <portals/socknal.h>
52 unsigned int portal_debug;
53 unsigned int portal_printk;
55 static unsigned int g_nal = 0;
57 static int g_socket_txmem = 0;
58 static int g_socket_rxmem = 0;
59 static int g_socket_nonagle = 1;
67 static name2num_t nalnames[] = {
72 {"openib", OPENIBNAL},
76 static cfg_record_cb_t g_record_cb;
78 /* Convert a string boolean to an int; "enable" -> 1 */
79 int ptl_parse_bool (int *b, char *str) {
80 if (!strcasecmp (str, "no") ||
81 !strcasecmp (str, "n") ||
82 !strcasecmp (str, "off") ||
83 !strcasecmp (str, "down") ||
84 !strcasecmp (str, "disable"))
90 if (!strcasecmp (str, "yes") ||
91 !strcasecmp (str, "y") ||
92 !strcasecmp (str, "on") ||
93 !strcasecmp (str, "up") ||
94 !strcasecmp (str, "enable"))
103 /* Convert human readable size string to and int; "1k" -> 1000 */
104 int ptl_parse_size (int *sizep, char *str) {
108 switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
141 ptl_set_cfg_record_cb(cfg_record_cb_t cb)
148 pcfg_ioctl(struct portals_cfg *pcfg)
152 if (pcfg->pcfg_nal ==0)
153 pcfg->pcfg_nal = g_nal;
156 rc = g_record_cb(PORTALS_CFG_TYPE, sizeof(*pcfg), pcfg);
158 struct portal_ioctl_data data;
159 PORTAL_IOC_INIT (data);
160 data.ioc_pbuf1 = (char*)pcfg;
161 data.ioc_plen1 = sizeof(*pcfg);
162 /* XXX liblustre hack XXX */
163 data.ioc_nal_cmd = pcfg->pcfg_command;
164 data.ioc_nid = pcfg->pcfg_nid;
166 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_NAL_CMD, &data);
175 name2num_lookup_name (name2num_t *table, char *str)
177 while (table->name != NULL)
178 if (!strcmp (str, table->name))
186 name2num_lookup_num (name2num_t *table, int num)
188 while (table->name != NULL)
189 if (num == table->num)
197 ptl_name2nal (char *str)
199 name2num_t *e = name2num_lookup_name (nalnames, str);
201 return ((e == NULL) ? -1 : e->num);
207 name2num_t *e = name2num_lookup_num (nalnames, nal);
209 return ((e == NULL) ? "???" : e->name);
212 static struct hostent *
213 ptl_gethostbyname(char * hname) {
215 he = gethostbyname(hname);
220 fprintf(stderr, "Unable to resolve hostname: %s\n",
224 fprintf(stderr, "gethostbyname error: %s\n",
234 ptl_parse_port (int *port, char *str)
238 *port = strtol (str, &end, 0);
240 if (*end == 0 && /* parsed whole string */
241 *port > 0 && *port < 65536) /* minimal sanity check */
248 ptl_parse_time (time_t *t, char *str)
254 *t = strtol (str, &end, 0);
255 if (*end == 0) /* parsed whole string */
258 memset (&tm, 0, sizeof (tm));
259 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
260 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
261 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
265 tm.tm_mon--; /* convert to 0 == Jan */
266 tm.tm_year -= 1900; /* y2k quirk */
267 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
270 if (*t == (time_t)-1)
277 ptl_parse_ipquad (__u32 *ipaddrp, char *str)
284 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
285 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
286 (c & ~0xff) == 0 && (d & ~0xff) == 0)
288 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
296 ptl_parse_ipaddr (__u32 *ipaddrp, char *str)
300 if (!strcmp (str, "_all_"))
306 if (ptl_parse_ipquad(ipaddrp, str) == 0)
309 if ((('a' <= str[0] && str[0] <= 'z') ||
310 ('A' <= str[0] && str[0] <= 'Z')) &&
311 (he = ptl_gethostbyname (str)) != NULL)
313 __u32 addr = *(__u32 *)he->h_addr;
315 *ipaddrp = ntohl(addr); /* HOST byte order */
323 ptl_ipaddr_2_str (__u32 ipaddr, char *str, int lookup)
329 net_ip = htonl (ipaddr);
330 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
332 strcpy(str, he->h_name);
337 sprintf (str, "%d.%d.%d.%d",
338 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
339 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
344 ptl_parse_nid (ptl_nid_t *nidp, char *str)
348 unsigned long long ullval;
350 if (!strcmp (str, "_all_")) {
355 if (ptl_parse_ipaddr (&ipaddr, str) == 0) {
356 *nidp = (ptl_nid_t)ipaddr;
360 ullval = strtoull(str, &end, 0);
362 /* parsed whole string */
363 *nidp = (ptl_nid_t)ullval;
370 __u64 ptl_nid2u64(ptl_nid_t nid)
372 switch (sizeof (nid)) {
378 fprintf(stderr, "Unexpected sizeof(ptl_nid_t) == %u\n", sizeof(nid));
386 ptl_nid2str (char *buffer, ptl_nid_t nid)
388 __u64 nid64 = ptl_nid2u64(nid);
389 struct hostent *he = 0;
391 /* Don't try to resolve NIDs that are e.g. Elan host IDs. Assume
392 * TCP addresses in the 0.x.x.x subnet are not in use. This can
393 * happen on routers and slows things down a _lot_. Bug 3442. */
394 if (nid & 0xff000000) {
395 __u32 addr = htonl((__u32)nid); /* back to NETWORK byte order */
397 he = gethostbyaddr ((const char *)&addr, sizeof (addr), AF_INET);
401 sprintf(buffer, "%#x:%s", (int)(nid64 >> 32), he->h_name);
403 sprintf(buffer, LPX64, nid64);
411 fprintf (stderr, "Error: you must run the 'network' command first.\n");
418 int g_nal_is_compatible (char *cmd, ...)
423 if (!g_nal_is_set ())
429 nal = va_arg (ap, int);
430 } while (nal != 0 && nal != g_nal);
438 /* Don't complain verbosely if we've not been passed a command
439 * name to complain about! */
440 fprintf (stderr, "Command %s not compatible with nal %s\n",
441 cmd, nal2name (g_nal));
447 sock_write (int cfd, void *buffer, int nob)
451 int rc = write (cfd, buffer, nob);
463 fprintf (stderr, "Unexpected zero sock_write\n");
468 buffer = (char *)buffer + nob;
475 sock_read (int cfd, void *buffer, int nob)
479 int rc = read (cfd, buffer, nob);
489 if (rc == 0) /* EOF */
491 errno = ECONNABORTED;
496 buffer = (char *)buffer + nob;
502 int ptl_initialize(int argc, char **argv)
504 register_ioc_dev(PORTALS_DEV_ID, PORTALS_DEV_PATH);
509 int jt_ptl_network(int argc, char **argv)
515 (nal = ptl_name2nal (argv[1])) >= 0) {
520 fprintf(stderr, "usage: %s \n", argv[0]);
521 for (entry = nalnames; entry->name != NULL; entry++)
522 fprintf (stderr, "%s%s", entry == nalnames ? "<" : "|", entry->name);
523 fprintf(stderr, ">\n");
529 jt_ptl_print_interfaces (int argc, char **argv)
531 struct portals_cfg pcfg;
536 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
539 for (index = 0;;index++) {
540 PCFG_INIT (pcfg, NAL_CMD_GET_INTERFACE);
541 pcfg.pcfg_count = index;
543 rc = pcfg_ioctl (&pcfg);
547 printf ("%s: (%s/%s) npeer %d nroute %d\n",
548 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[2], 1),
549 ptl_ipaddr_2_str(pcfg.pcfg_id, buffer[0], 0),
550 ptl_ipaddr_2_str(pcfg.pcfg_misc, buffer[1], 0),
551 pcfg.pcfg_fd, pcfg.pcfg_count);
555 printf ("<no interfaces>\n");
560 jt_ptl_add_interface (int argc, char **argv)
562 struct portals_cfg pcfg;
565 __u32 netmask = 0xffffff00;
567 if (argc < 2 || argc > 3) {
568 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
572 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
575 if (ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
576 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
581 ptl_parse_ipquad(&netmask, argv[2]) != 0) {
582 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
586 PCFG_INIT(pcfg, NAL_CMD_ADD_INTERFACE);
587 pcfg.pcfg_id = ipaddr;
588 pcfg.pcfg_misc = netmask;
590 rc = pcfg_ioctl (&pcfg);
592 fprintf (stderr, "failed to add interface: %s\n",
601 jt_ptl_del_interface (int argc, char **argv)
603 struct portals_cfg pcfg;
608 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
612 if (!g_nal_is_compatible(argv[0], SOCKNAL, 0))
616 ptl_parse_ipaddr(&ipaddr, argv[1]) != 0) {
617 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
621 PCFG_INIT(pcfg, NAL_CMD_DEL_INTERFACE);
622 pcfg.pcfg_id = ipaddr;
624 rc = pcfg_ioctl (&pcfg);
626 fprintf (stderr, "failed to delete interface: %s\n",
635 jt_ptl_print_peers (int argc, char **argv)
637 struct portals_cfg pcfg;
642 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, 0))
645 for (index = 0;;index++) {
646 PCFG_INIT (pcfg, NAL_CMD_GET_PEER);
647 pcfg.pcfg_count = index;
649 rc = pcfg_ioctl (&pcfg);
653 if (g_nal_is_compatible(NULL, SOCKNAL, 0))
654 printf (LPX64"[%d]%s@%s:%d #%d\n",
655 pcfg.pcfg_nid, pcfg.pcfg_wait,
656 ptl_ipaddr_2_str (pcfg.pcfg_size, buffer[0], 1),
657 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1),
658 pcfg.pcfg_misc, pcfg.pcfg_count);
660 printf (LPX64"[%d]\n",
661 pcfg.pcfg_nid, pcfg.pcfg_wait);
665 printf ("<no peers>\n");
670 jt_ptl_add_peer (int argc, char **argv)
672 struct portals_cfg pcfg;
678 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, 0))
681 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
683 fprintf (stderr, "usage(tcp): %s nid ipaddr port\n",
687 } else if (argc != 2) {
688 fprintf (stderr, "usage(openib): %s nid\n", argv[0]);
692 if (ptl_parse_nid (&nid, argv[1]) != 0 ||
693 nid == PTL_NID_ANY) {
694 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
698 if (g_nal_is_compatible (NULL, SOCKNAL, 0)) {
699 if (ptl_parse_ipaddr (&ip, argv[2]) != 0) {
700 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
704 if (ptl_parse_port (&port, argv[3]) != 0) {
705 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
710 PCFG_INIT(pcfg, NAL_CMD_ADD_PEER);
713 pcfg.pcfg_misc = port;
715 rc = pcfg_ioctl (&pcfg);
717 fprintf (stderr, "failed to add peer: %s\n",
726 jt_ptl_del_peer (int argc, char **argv)
728 struct portals_cfg pcfg;
729 ptl_nid_t nid = PTL_NID_ANY;
731 int single_share = 0;
735 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, 0))
738 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
740 fprintf (stderr, "usage: %s [nid] [ipaddr] [single_share]\n",
744 } else if (argc > 3) {
745 fprintf (stderr, "usage: %s [nid] [single_share]\n", argv[0]);
750 ptl_parse_nid (&nid, argv[1]) != 0) {
751 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
756 if (g_nal_is_compatible(NULL, SOCKNAL, 0)) {
758 ptl_parse_ipaddr (&ip, argv[argidx]) != 0) {
759 fprintf (stderr, "Can't parse ip addr: %s\n",
767 if (!strcmp (argv[3], "single_share")) {
770 fprintf (stderr, "Unrecognised arg %s'\n", argv[3]);
775 PCFG_INIT(pcfg, NAL_CMD_DEL_PEER);
778 pcfg.pcfg_flags = single_share;
780 rc = pcfg_ioctl (&pcfg);
782 fprintf (stderr, "failed to remove peer: %s\n",
791 jt_ptl_print_connections (int argc, char **argv)
793 struct portals_cfg pcfg;
798 if (!g_nal_is_compatible (argv[0], SOCKNAL, OPENIBNAL, 0))
801 for (index = 0;;index++) {
802 PCFG_INIT (pcfg, NAL_CMD_GET_CONN);
803 pcfg.pcfg_count = index;
805 rc = pcfg_ioctl (&pcfg);
809 if (g_nal_is_compatible (NULL, SOCKNAL, 0))
810 printf ("[%d]%s:"LPX64"@%s:%d:%s %d/%d %s\n",
811 pcfg.pcfg_gw_nal, /* scheduler */
812 ptl_ipaddr_2_str (pcfg.pcfg_fd, buffer[0], 1), /* local IP addr */
814 ptl_ipaddr_2_str (pcfg.pcfg_id, buffer[1], 1), /* remote IP addr */
815 pcfg.pcfg_misc, /* remote port */
816 (pcfg.pcfg_flags == SOCKNAL_CONN_ANY) ? "A" :
817 (pcfg.pcfg_flags == SOCKNAL_CONN_CONTROL) ? "C" :
818 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_IN) ? "I" :
819 (pcfg.pcfg_flags == SOCKNAL_CONN_BULK_OUT) ? "O" : "?",
820 pcfg.pcfg_count, /* tx buffer size */
821 pcfg.pcfg_size, /* rx buffer size */
822 pcfg.pcfg_wait ? "nagle" : "nonagle");
829 printf ("<no connections>\n");
833 int jt_ptl_connect(int argc, char **argv)
835 struct portals_cfg pcfg;
836 struct sockaddr_in srvaddr;
840 int type = SOCKNAL_CONN_ANY;
844 fprintf(stderr, "usage: %s ip port [type]\n", argv[0]);
848 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
851 rc = ptl_parse_ipaddr (&ipaddr, argv[1]);
853 fprintf(stderr, "Can't parse hostname: %s\n", argv[1]);
857 if (ptl_parse_port (&port, argv[2]) != 0) {
858 fprintf (stderr, "Can't parse port: %s\n", argv[2]);
863 for (flag = argv[3]; *flag != 0; flag++)
867 if (type != SOCKNAL_CONN_ANY) {
868 fprintf(stderr, "Can't flag type twice\n");
871 type = SOCKNAL_CONN_BULK_IN;
875 if (type != SOCKNAL_CONN_ANY) {
876 fprintf(stderr, "Can't flag type twice\n");
879 type = SOCKNAL_CONN_BULK_OUT;
883 if (type != SOCKNAL_CONN_ANY) {
884 fprintf(stderr, "Can't flag type twice\n");
887 type = SOCKNAL_CONN_CONTROL;
891 fprintf (stderr, "unrecognised flag '%c'\n",
896 memset(&srvaddr, 0, sizeof(srvaddr));
897 srvaddr.sin_family = AF_INET;
898 srvaddr.sin_port = htons(port);
899 srvaddr.sin_addr.s_addr = htonl(ipaddr);
901 fd = socket(PF_INET, SOCK_STREAM, 0);
903 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
907 rc = connect(fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr));
909 fprintf(stderr, "connect() failed: %s\n", strerror(errno));
913 printf("Connected host: %s type: %s\n",
915 (type == SOCKNAL_CONN_ANY) ? "A" :
916 (type == SOCKNAL_CONN_CONTROL) ? "C" :
917 (type == SOCKNAL_CONN_BULK_IN) ? "I" :
918 (type == SOCKNAL_CONN_BULK_OUT) ? "O" : "?");
920 PCFG_INIT(pcfg, NAL_CMD_REGISTER_PEER_FD);
921 pcfg.pcfg_nal = g_nal;
923 pcfg.pcfg_misc = type;
925 rc = pcfg_ioctl(&pcfg);
927 fprintf(stderr, "failed to register fd with portals: %s\n",
933 printf("Connection to %s registered with socknal\n", argv[1]);
937 fprintf(stderr, "close failed: %d\n", rc);
942 int jt_ptl_disconnect(int argc, char **argv)
944 struct portals_cfg pcfg;
945 ptl_nid_t nid = PTL_NID_ANY;
950 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
954 if (!g_nal_is_compatible (NULL, SOCKNAL, OPENIBNAL, 0))
958 ptl_parse_nid (&nid, argv[1]) != 0) {
959 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
963 if (g_nal_is_compatible (NULL, SOCKNAL, 0) &&
965 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
966 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
970 PCFG_INIT(pcfg, NAL_CMD_CLOSE_CONNECTION);
972 pcfg.pcfg_id = ipaddr;
974 rc = pcfg_ioctl(&pcfg);
976 fprintf(stderr, "failed to remove connection: %s\n",
984 int jt_ptl_push_connection (int argc, char **argv)
986 struct portals_cfg pcfg;
988 ptl_nid_t nid = PTL_NID_ANY;
992 fprintf(stderr, "usage: %s [nid] [ip]\n", argv[0]);
996 if (!g_nal_is_compatible (argv[0], SOCKNAL, 0))
1000 ptl_parse_nid (&nid, argv[1]) != 0) {
1001 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1006 ptl_parse_ipaddr (&ipaddr, argv[2]) != 0) {
1007 fprintf(stderr, "Can't parse ipaddr: %s\n", argv[2]);
1010 PCFG_INIT(pcfg, NAL_CMD_PUSH_CONNECTION);
1011 pcfg.pcfg_nid = nid;
1012 pcfg.pcfg_id = ipaddr;
1014 rc = pcfg_ioctl(&pcfg);
1016 fprintf(stderr, "failed to push connection: %s\n",
1025 jt_ptl_print_active_txs (int argc, char **argv)
1027 struct portals_cfg pcfg;
1031 if (!g_nal_is_compatible (argv[0], QSWNAL, 0))
1034 for (index = 0;;index++) {
1035 PCFG_INIT(pcfg, NAL_CMD_GET_TXDESC);
1036 pcfg.pcfg_count = index;
1038 rc = pcfg_ioctl(&pcfg);
1042 printf ("%p: %5s payload %6d bytes to "LPX64" via "LPX64" by pid %6d: %s, %s, state %d\n",
1044 pcfg.pcfg_count == PTL_MSG_ACK ? "ACK" :
1045 pcfg.pcfg_count == PTL_MSG_PUT ? "PUT" :
1046 pcfg.pcfg_count == PTL_MSG_GET ? "GET" :
1047 pcfg.pcfg_count == PTL_MSG_REPLY ? "REPLY" : "<wierd message>",
1052 (pcfg.pcfg_flags & 1) ? "delayed" : "immediate",
1053 (pcfg.pcfg_flags & 2) ? "nblk" : "normal",
1054 pcfg.pcfg_flags >> 2);
1058 printf ("<no active descs>\n");
1062 int jt_ptl_ping(int argc, char **argv)
1069 struct portal_ioctl_data data;
1072 fprintf(stderr, "usage: %s nid [count] [size] [timeout (secs)]\n", argv[0]);
1076 if (!g_nal_is_set())
1079 if (ptl_parse_nid (&nid, argv[1]) != 0)
1081 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1087 count = atol(argv[2]);
1089 if (count < 0 || count > 20000)
1091 fprintf(stderr, "are you insane? %ld is a crazy count.\n", count);
1097 size= atol(argv[3]);
1100 timeout = atol (argv[4]);
1102 PORTAL_IOC_INIT (data);
1103 data.ioc_count = count;
1104 data.ioc_size = size;
1106 data.ioc_nal = g_nal;
1107 data.ioc_timeout = timeout;
1109 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_PING, &data);
1111 fprintf(stderr, "failed to start pinger: %s\n",
1118 int jt_ptl_shownid(int argc, char **argv)
1120 struct portal_ioctl_data data;
1124 fprintf(stderr, "usage: %s\n", argv[0]);
1128 if (!g_nal_is_set())
1131 PORTAL_IOC_INIT (data);
1132 data.ioc_nal = g_nal;
1133 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_GET_NID, &data);
1135 fprintf(stderr, "getting my NID failed: %s\n",
1138 printf(LPX64"\n", data.ioc_nid);
1142 int jt_ptl_mynid(int argc, char **argv)
1145 char hostname[1024];
1147 struct portals_cfg pcfg;
1151 fprintf(stderr, "usage: %s [NID]\n", argv[0]);
1152 fprintf(stderr, "NID defaults to the primary IP address of the machine.\n");
1156 if (!g_nal_is_set())
1161 else if (gethostname(hostname, sizeof(hostname)) != 0) {
1162 fprintf(stderr, "gethostname failed: %s\n",
1169 rc = ptl_parse_nid (&mynid, nidstr);
1171 fprintf (stderr, "Can't convert '%s' into a NID\n", nidstr);
1175 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
1176 pcfg.pcfg_nid = mynid;
1178 rc = pcfg_ioctl(&pcfg);
1180 fprintf(stderr, "setting my NID failed: %s\n",
1183 printf("registered my nid "LPX64" (%s)\n",
1184 ptl_nid2u64(mynid), hostname);
1189 jt_ptl_fail_nid (int argc, char **argv)
1193 unsigned int threshold;
1194 struct portal_ioctl_data data;
1196 if (argc < 2 || argc > 3)
1198 fprintf (stderr, "usage: %s nid|\"_all_\" [count (0 == mend)]\n", argv[0]);
1202 if (!g_nal_is_set())
1205 if (!strcmp (argv[1], "_all_"))
1207 else if (ptl_parse_nid (&nid, argv[1]) != 0)
1209 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1214 threshold = PTL_MD_THRESH_INF;
1215 else if (sscanf (argv[2], "%i", &threshold) != 1) {
1216 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1220 PORTAL_IOC_INIT (data);
1221 data.ioc_nal = g_nal;
1223 data.ioc_count = threshold;
1225 rc = l_ioctl (PORTALS_DEV_ID, IOC_PORTAL_FAIL_NID, &data);
1227 fprintf (stderr, "IOC_PORTAL_FAIL_NID failed: %s\n",
1230 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1236 jt_ptl_add_route (int argc, char **argv)
1238 struct portals_cfg pcfg;
1241 ptl_nid_t gateway_nid;
1246 fprintf (stderr, "usage: %s gateway target [target]\n", argv[0]);
1250 if (!g_nal_is_set())
1253 if (ptl_parse_nid (&gateway_nid, argv[1]) != 0)
1255 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1259 if (ptl_parse_nid (&nid1, argv[2]) != 0)
1261 fprintf (stderr, "Can't parse first target NID \"%s\"\n", argv[2]);
1267 else if (ptl_parse_nid (&nid2, argv[3]) != 0)
1269 fprintf (stderr, "Can't parse second target NID \"%s\"\n", argv[4]);
1273 PCFG_INIT(pcfg, NAL_CMD_ADD_ROUTE);
1274 pcfg.pcfg_nid = gateway_nid;
1275 pcfg.pcfg_nal = ROUTER;
1276 pcfg.pcfg_gw_nal = g_nal;
1277 pcfg.pcfg_nid2 = MIN (nid1, nid2);
1278 pcfg.pcfg_nid3 = MAX (nid1, nid2);
1280 rc = pcfg_ioctl(&pcfg);
1283 fprintf (stderr, "NAL_CMD_ADD_ROUTE failed: %s\n", strerror (errno));
1291 jt_ptl_del_route (int argc, char **argv)
1293 struct portals_cfg pcfg;
1295 ptl_nid_t nid1 = PTL_NID_ANY;
1296 ptl_nid_t nid2 = PTL_NID_ANY;
1301 fprintf (stderr, "usage: %s targetNID\n", argv[0]);
1305 if (!g_nal_is_set())
1308 if (ptl_parse_nid (&nid, argv[1]) != 0)
1310 fprintf (stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1315 ptl_parse_nid (&nid1, argv[2]) != 0)
1317 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[2]);
1324 if (ptl_parse_nid (&nid2, argv[3]) != 0) {
1325 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[3]);
1330 ptl_nid_t tmp = nid1;
1337 PCFG_INIT(pcfg, NAL_CMD_DEL_ROUTE);
1338 pcfg.pcfg_nal = ROUTER;
1339 pcfg.pcfg_gw_nal = g_nal;
1340 pcfg.pcfg_nid = nid;
1341 pcfg.pcfg_nid2 = nid1;
1342 pcfg.pcfg_nid3 = nid2;
1344 rc = pcfg_ioctl(&pcfg);
1347 fprintf (stderr, "NAL_CMD_DEL_ROUTE ("LPX64") failed: %s\n",
1348 ptl_nid2u64(nid), strerror (errno));
1356 jt_ptl_notify_router (int argc, char **argv)
1358 struct portals_cfg pcfg;
1367 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1372 if (ptl_parse_nid (&nid, argv[1]) != 0)
1374 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1378 if (ptl_parse_bool (&enable, argv[2]) != 0) {
1379 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1383 gettimeofday(&now, NULL);
1387 } else if (ptl_parse_time (&when, argv[3]) != 0) {
1388 fprintf(stderr, "Can't parse time %s\n"
1389 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1390 "or an absolute unix time in seconds\n", argv[3]);
1392 } else if (when > now.tv_sec) {
1393 fprintf (stderr, "%s specifies a time in the future\n",
1398 PCFG_INIT(pcfg, NAL_CMD_NOTIFY_ROUTER);
1399 pcfg.pcfg_nal = ROUTER;
1400 pcfg.pcfg_gw_nal = g_nal;
1401 pcfg.pcfg_nid = nid;
1402 pcfg.pcfg_flags = enable;
1403 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1404 pcfg.pcfg_nid3 = (__u64)when;
1406 rc = pcfg_ioctl(&pcfg);
1409 fprintf (stderr, "NAL_CMD_NOTIFY_ROUTER ("LPX64") failed: %s\n",
1410 ptl_nid2u64(nid), strerror (errno));
1418 jt_ptl_print_routes (int argc, char **argv)
1420 char buffer[3][128];
1421 struct portals_cfg pcfg;
1425 ptl_nid_t gateway_nid;
1430 for (index = 0;;index++)
1432 PCFG_INIT(pcfg, NAL_CMD_GET_ROUTE);
1433 pcfg.pcfg_nal = ROUTER;
1434 pcfg.pcfg_count = index;
1436 rc = pcfg_ioctl(&pcfg);
1440 gateway_nal = pcfg.pcfg_gw_nal;
1441 gateway_nid = pcfg.pcfg_nid;
1442 nid1 = pcfg.pcfg_nid2;
1443 nid2 = pcfg.pcfg_nid3;
1444 alive = pcfg.pcfg_flags;
1446 printf ("%8s %18s : %s - %s, %s\n",
1447 nal2name (gateway_nal),
1448 ptl_nid2str (buffer[0], gateway_nid),
1449 ptl_nid2str (buffer[1], nid1),
1450 ptl_nid2str (buffer[2], nid2),
1451 alive ? "up" : "down");
1457 lwt_control(int enable, int clear)
1459 struct portal_ioctl_data data;
1462 PORTAL_IOC_INIT(data);
1463 data.ioc_flags = enable;
1464 data.ioc_misc = clear;
1466 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_CONTROL, &data);
1470 fprintf(stderr, "IOC_PORTAL_LWT_CONTROL failed: %s\n",
1476 lwt_snapshot(cycles_t *now, int *ncpu, int *totalsize,
1477 lwt_event_t *events, int size)
1479 struct portal_ioctl_data data;
1482 PORTAL_IOC_INIT(data);
1483 data.ioc_pbuf1 = (char *)events;
1484 data.ioc_plen1 = size;
1486 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_SNAPSHOT, &data);
1488 fprintf(stderr, "IOC_PORTAL_LWT_SNAPSHOT failed: %s\n",
1493 /* crappy overloads */
1494 if (data.ioc_nid != sizeof(lwt_event_t) ||
1495 data.ioc_nid2 != offsetof(lwt_event_t, lwte_where)) {
1496 fprintf(stderr,"kernel/user LWT event mismatch %d(%d),%d(%d)\n",
1497 (int)data.ioc_nid, sizeof(lwt_event_t),
1499 (int)offsetof(lwt_event_t, lwte_where));
1503 LASSERT (data.ioc_count != 0);
1504 LASSERT (data.ioc_misc != 0);
1507 *now = data.ioc_nid;
1510 *ncpu = data.ioc_count;
1512 if (totalsize != NULL)
1513 *totalsize = data.ioc_misc;
1519 lwt_get_string(char *kstr)
1522 struct portal_ioctl_data data;
1526 /* FIXME: this could maintain a symbol table since we expect to be
1527 * looking up the same strings all the time... */
1529 PORTAL_IOC_INIT(data);
1530 data.ioc_pbuf1 = kstr;
1531 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1532 data.ioc_pbuf2 = NULL;
1535 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1537 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1542 size = data.ioc_count;
1543 ustr = (char *)malloc(size);
1545 fprintf(stderr, "Can't allocate string storage of size %d\n",
1550 PORTAL_IOC_INIT(data);
1551 data.ioc_pbuf1 = kstr;
1552 data.ioc_plen1 = 1; /* non-zero just to fool portal_ioctl_is_invalid() */
1553 data.ioc_pbuf2 = ustr;
1554 data.ioc_plen2 = size;
1556 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_LWT_LOOKUP_STRING, &data);
1558 fprintf(stderr, "IOC_PORTAL_LWT_LOOKUP_STRING failed: %s\n",
1563 LASSERT(strlen(ustr) == size - 1);
1568 lwt_put_string(char *ustr)
1574 lwt_print(FILE *f, cycles_t t0, cycles_t tlast, double mhz, int cpu, lwt_event_t *e)
1576 char *where = lwt_get_string(e->lwte_where);
1581 fprintf(f, "%#010lx %#010lx %#010lx %#010lx: %#010lx %1d %10.6f %10.2f %s\n",
1582 e->lwte_p1, e->lwte_p2, e->lwte_p3, e->lwte_p4,
1583 (long)e->lwte_task, cpu, (e->lwte_when - t0) / (mhz * 1000000.0),
1584 (t0 == e->lwte_when) ? 0.0 : (e->lwte_when - tlast) / mhz,
1587 lwt_put_string(where);
1593 get_cycles_per_usec ()
1595 FILE *f = fopen ("/proc/cpuinfo", "r");
1600 while (fgets (line, sizeof (line), f) != NULL)
1601 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1608 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1613 jt_ptl_lwt(int argc, char **argv)
1615 const int lwt_max_cpus = 32;
1618 int nevents_per_cpu;
1619 lwt_event_t *events;
1620 lwt_event_t *cpu_event[lwt_max_cpus + 1];
1621 lwt_event_t *next_event[lwt_max_cpus];
1622 lwt_event_t *first_event[lwt_max_cpus];
1631 struct timeval tvnow;
1632 int printed_date = 0;
1637 (strcmp(argv[1], "start") &&
1638 strcmp(argv[1], "stop"))) {
1641 " %s stop [fname]\n", argv[0], argv[0]);
1645 if (!strcmp(argv[1], "start")) {
1647 if (lwt_control(0, 0) != 0)
1651 if (lwt_control(0, 1) != 0)
1655 if (lwt_control(1, 0) != 0)
1661 if (lwt_snapshot(NULL, &ncpus, &totalspace, NULL, 0) != 0)
1664 if (ncpus > lwt_max_cpus) {
1665 fprintf(stderr, "Too many cpus: %d (%d)\n",
1666 ncpus, lwt_max_cpus);
1670 events = (lwt_event_t *)malloc(totalspace);
1671 if (events == NULL) {
1672 fprintf(stderr, "Can't allocate %d\n", totalspace);
1676 if (lwt_control(0, 0) != 0) { /* disable */
1681 if (lwt_snapshot(&tnow, NULL, NULL, events, totalspace)) {
1686 /* we want this time to be sampled at snapshot time */
1687 gettimeofday(&tvnow, NULL);
1690 f = fopen (argv[2], "w");
1692 fprintf(stderr, "Can't open %s for writing: %s\n", argv[2], strerror (errno));
1698 mhz = get_cycles_per_usec();
1700 /* carve events into per-cpu slices */
1701 nevents_per_cpu = totalspace / (ncpus * sizeof(lwt_event_t));
1702 for (cpu = 0; cpu <= ncpus; cpu++)
1703 cpu_event[cpu] = &events[cpu * nevents_per_cpu];
1705 /* find the earliest event on each cpu */
1706 for (cpu = 0; cpu < ncpus; cpu++) {
1707 first_event[cpu] = NULL;
1709 for (e = cpu_event[cpu]; e < cpu_event[cpu + 1]; e++) {
1711 if (e->lwte_where == NULL) /* not an event */
1714 if (first_event[cpu] == NULL ||
1715 first_event[cpu]->lwte_when > e->lwte_when)
1716 first_event[cpu] = e;
1719 next_event[cpu] = first_event[cpu];
1723 for (cpu = 0; cpu < ncpus; cpu++) {
1724 e = first_event[cpu];
1725 if (e == NULL) /* no events this cpu */
1728 if (e == cpu_event[cpu])
1729 e = cpu_event[cpu + 1] - 1;
1733 /* If there's an event immediately before the first one, this
1734 * cpu wrapped its event buffer */
1735 if (e->lwte_where == NULL)
1738 /* We should only start outputting events from the most recent
1739 * first event in any wrapped cpu. Events before this time on
1740 * other cpus won't have any events from this CPU to interleave
1742 if (t0 < first_event[cpu]->lwte_when)
1743 t0 = first_event[cpu]->lwte_when;
1747 /* find which cpu has the next event */
1749 for (i = 0; i < ncpus; i++) {
1751 if (next_event[i] == NULL) /* this cpu exhausted */
1755 next_event[i]->lwte_when < next_event[cpu]->lwte_when)
1759 if (cpu < 0) /* all cpus exhausted */
1763 /* no wrapped cpus and this is he first ever event */
1764 t0 = next_event[cpu]->lwte_when;
1767 if (t0 <= next_event[cpu]->lwte_when) {
1768 /* on or after the first event */
1769 if (!printed_date) {
1770 cycles_t du = (tnow - t0) / mhz;
1771 time_t then = tvnow.tv_sec - du/1000000;
1773 if (du % 1000000 > tvnow.tv_usec)
1776 fprintf(f, "%s", ctime(&then));
1780 rc = lwt_print(f, t0, tlast, mhz, cpu, next_event[cpu]);
1784 if (++nlines % 10000 == 0 && f != stdout) {
1785 /* show some activity... */
1791 tlast = next_event[cpu]->lwte_when;
1794 if (next_event[cpu] == cpu_event[cpu + 1])
1795 next_event[cpu] = cpu_event[cpu];
1797 if (next_event[cpu]->lwte_where == NULL ||
1798 next_event[cpu] == first_event[cpu])
1799 next_event[cpu] = NULL;
1811 int jt_ptl_memhog(int argc, char **argv)
1813 static int gfp = 0; /* sticky! */
1815 struct portal_ioctl_data data;
1821 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1825 count = strtol(argv[1], &end, 0);
1826 if (count < 0 || *end != 0) {
1827 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1832 rc = strtol(argv[2], &end, 0);
1834 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1840 PORTAL_IOC_INIT(data);
1841 data.ioc_count = count;
1842 data.ioc_flags = gfp;
1843 rc = l_ioctl(PORTALS_DEV_ID, IOC_PORTAL_MEMHOG, &data);
1846 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1850 printf("memhog %d OK\n", count);