2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2014, Intel Corporation.
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.
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/nidstr.h>
25 #include <lnet/lnetctl.h>
26 #include <lnet/socklnd.h>
27 #include <lnet/lib-dlc.h>
31 unsigned int libcfs_debug;
32 unsigned int libcfs_printk = D_CANTMASK;
37 #define IOC_BUF_SIZE 8192
38 static char local_buf[IOC_BUF_SIZE];
39 static char *ioc_buf = local_buf;
41 /* Convert a string boolean to an int; "enable" -> 1 */
43 lnet_parse_bool (int *b, char *str)
45 if (!strcasecmp (str, "no") ||
46 !strcasecmp (str, "n") ||
47 !strcasecmp (str, "off") ||
48 !strcasecmp (str, "down") ||
49 !strcasecmp (str, "disable"))
55 if (!strcasecmp (str, "yes") ||
56 !strcasecmp (str, "y") ||
57 !strcasecmp (str, "on") ||
58 !strcasecmp (str, "up") ||
59 !strcasecmp (str, "enable"))
69 lnet_parse_port (int *port, char *str)
73 *port = strtol (str, &end, 0);
75 if (*end == 0 && /* parsed whole string */
76 *port > 0 && *port < 65536) /* minimal sanity check */
82 #ifdef HAVE_GETHOSTBYNAME
83 static struct hostent *
84 ptl_gethostbyname(char * hname) {
86 he = gethostbyname(hname);
91 fprintf(stderr, "Unable to resolve hostname: %s\n",
95 fprintf(stderr, "gethostbyname error for %s: %s\n",
96 hname, strerror(h_errno));
106 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
113 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
114 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
115 (c & ~0xff) == 0 && (d & ~0xff) == 0)
117 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
125 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
127 #ifdef HAVE_GETHOSTBYNAME
131 if (!strcmp (str, "_all_")) {
136 if (lnet_parse_ipquad(ipaddrp, str) == 0)
139 #ifdef HAVE_GETHOSTBYNAME
140 if ((('a' <= str[0] && str[0] <= 'z') ||
141 ('A' <= str[0] && str[0] <= 'Z')) &&
142 (he = ptl_gethostbyname (str)) != NULL) {
143 __u32 addr = *(__u32 *)he->h_addr;
145 *ipaddrp = ntohl(addr); /* HOST byte order */
154 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
156 #ifdef HAVE_GETHOSTBYNAME
161 net_ip = htonl (ipaddr);
162 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
164 strlcpy(str, he->h_name, strsize);
170 sprintf (str, "%d.%d.%d.%d",
171 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
172 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
177 lnet_parse_time (time_t *t, char *str)
183 *t = strtol (str, &end, 0);
184 if (*end == 0) /* parsed whole string */
187 memset (&tm, 0, sizeof (tm));
188 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
189 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
190 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
194 tm.tm_mon--; /* convert to 0 == Jan */
195 tm.tm_year -= 1900; /* y2k quirk */
196 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
199 if (*t == (time_t)-1)
206 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
208 id_ptr->pid = LNET_PID_ANY;
209 id_ptr->nid = libcfs_str2nid(nid_str);
210 if (id_ptr->nid == LNET_NID_ANY) {
211 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
218 int g_net_is_set (char *cmd)
225 "You must run the 'network' command before '%s'.\n",
230 int g_net_is_compatible (char *cmd, ...)
235 if (!g_net_is_set(cmd))
241 nal = va_arg (ap, int);
242 if (nal == LNET_NETTYP(g_net)) {
252 "Command %s not compatible with %s NAL\n",
254 libcfs_lnd2str(LNET_NETTYP(g_net)));
258 int ptl_initialize(int argc, char **argv)
260 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
261 LNET_DEV_MAJOR, LNET_DEV_MINOR);
266 int jt_ptl_network(int argc, char **argv)
268 struct libcfs_ioctl_data data;
269 __u32 net = LNET_NIDNET(LNET_NID_ANY);
273 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
277 if (!strcmp(argv[1], "unconfigure") ||
278 !strcmp(argv[1], "down")) {
279 LIBCFS_IOC_INIT(data);
280 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
283 printf ("LNET ready to unload\n");
288 fprintf(stderr, "LNET busy\n");
290 fprintf(stderr, "LNET unconfigure error %d: %s\n",
291 errno, strerror(errno));
295 if (!strcmp(argv[1], "configure") ||
296 !strcmp(argv[1], "up")) {
297 LIBCFS_IOC_INIT(data);
298 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
301 printf ("LNET configured\n");
305 fprintf(stderr, "LNET configure error %d: %s\n",
306 errno, strerror(errno));
310 net = libcfs_str2net(argv[1]);
311 if (net == LNET_NIDNET(LNET_NID_ANY)) {
312 fprintf(stderr, "Can't parse net %s\n", argv[1]);
316 if (LNET_NETTYP(net) == CIBLND ||
317 LNET_NETTYP(net) == OPENIBLND ||
318 LNET_NETTYP(net) == IIBLND ||
319 LNET_NETTYP(net) == VIBLND) {
320 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
330 jt_ptl_list_nids(int argc, char **argv)
332 struct libcfs_ioctl_data data;
333 int all = 0, return_nid = 0;
337 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
338 /* Hack to pass back value */
339 return_nid = (argc == 2) && (argv[1][0] == 1);
341 if ((argc > 2) && !(all || return_nid)) {
342 fprintf(stderr, "usage: %s [all]\n", argv[0]);
346 for (count = 0;; count++) {
347 LIBCFS_IOC_INIT (data);
348 data.ioc_count = count;
349 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
352 if ((count > 0) && (errno == ENOENT))
353 /* We found them all */
355 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
356 errno, strerror(errno));
360 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
361 printf("%s\n", libcfs_nid2str(data.ioc_nid));
363 *(__u64 *)(argv[1]) = data.ioc_nid;
373 jt_ptl_which_nid (int argc, char **argv)
375 struct libcfs_ioctl_data data;
378 lnet_nid_t best_nid = LNET_NID_ANY;
387 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
391 for (i = 1; i < argc; i++) {
393 nid = libcfs_str2nid(nidstr);
394 if (nid == LNET_NID_ANY) {
395 fprintf(stderr, "Can't parse NID %s\n", nidstr);
399 LIBCFS_IOC_INIT(data);
402 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
404 fprintf(stderr, "Can't get distance to %s: %s\n",
405 nidstr, strerror(errno));
409 dist = data.ioc_u32[0];
410 order = data.ioc_u32[1];
413 if (dist == -EHOSTUNREACH)
416 fprintf(stderr, "Unexpected distance to %s: %d\n",
421 if (best_nid == LNET_NID_ANY ||
423 (dist == best_dist && order < best_order)) {
430 if (best_nid == LNET_NID_ANY) {
431 fprintf(stderr, "No reachable NID\n");
435 printf("%s\n", libcfs_nid2str(best_nid));
440 jt_ptl_print_interfaces (int argc, char **argv)
442 struct libcfs_ioctl_data data;
443 char buffer[3][HOST_NAME_MAX + 1];
447 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
450 for (index = 0;;index++) {
451 LIBCFS_IOC_INIT(data);
452 data.ioc_net = g_net;
453 data.ioc_count = index;
455 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
459 printf ("%s: (%s/%s) npeer %d nroute %d\n",
460 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
461 sizeof(buffer[2]), 1),
462 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
463 sizeof(buffer[0]), 0),
464 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
465 sizeof(buffer[1]), 0),
466 data.ioc_u32[2], data.ioc_u32[3]);
470 if (errno == ENOENT) {
471 printf ("<no interfaces>\n");
473 fprintf(stderr, "Error getting interfaces: %s: "
483 jt_ptl_add_interface (int argc, char **argv)
485 struct libcfs_ioctl_data data;
488 __u32 netmask = 0xffffff00;
493 if (argc < 2 || argc > 3) {
494 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
498 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
501 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
502 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
507 count = strtol(argv[2], &end, 0);
508 if (count > 0 && count < 32 && *end == 0) {
510 for (i = count; i > 0; i--)
511 netmask = netmask|(1<<(32-i));
512 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
513 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
518 LIBCFS_IOC_INIT(data);
519 data.ioc_net = g_net;
520 data.ioc_u32[0] = ipaddr;
521 data.ioc_u32[1] = netmask;
523 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
525 fprintf (stderr, "failed to add interface: %s\n",
534 jt_ptl_del_interface (int argc, char **argv)
536 struct libcfs_ioctl_data data;
541 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
545 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
549 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
550 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
554 LIBCFS_IOC_INIT(data);
555 data.ioc_net = g_net;
556 data.ioc_u32[0] = ipaddr;
558 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
560 fprintf (stderr, "failed to delete interface: %s\n",
569 jt_ptl_print_peers (int argc, char **argv)
571 struct libcfs_ioctl_data data;
572 lnet_process_id_t id;
573 char buffer[2][HOST_NAME_MAX + 1];
577 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
580 for (index = 0;;index++) {
581 LIBCFS_IOC_INIT(data);
582 data.ioc_net = g_net;
583 data.ioc_count = index;
585 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
589 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
590 id.nid = data.ioc_nid;
591 id.pid = data.ioc_u32[4];
592 printf ("%-20s [%d]%s->%s:%d #%d\n",
594 data.ioc_count, /* persistence */
596 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
597 sizeof(buffer[0]), 1),
599 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
600 sizeof(buffer[1]), 1),
601 data.ioc_u32[1], /* peer port */
602 data.ioc_u32[3]); /* conn_count */
603 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
604 int disconn = data.ioc_flags >> 16;
610 state = data.ioc_flags & 0xffff ? "C" : "U";
612 printf ("%-20s (%d) %s [%d] "LPU64" "
613 "sq %d/%d tx %d/%d/%d\n",
614 libcfs_nid2str(data.ioc_nid), /* peer nid */
615 data.ioc_net, /* gemini device id */
616 state, /* peer is Connecting, Up, or Down */
617 data.ioc_count, /* peer refcount */
618 data.ioc_u64[0], /* peerstamp */
619 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
620 /* fmaq, nfma, nrdma */
621 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
624 printf ("%-20s [%d]\n",
625 libcfs_nid2str(data.ioc_nid), data.ioc_count);
630 if (errno == ENOENT) {
631 printf ("<no peers>\n");
633 fprintf(stderr, "Error getting peer list: %s: "
642 jt_ptl_add_peer (int argc, char **argv)
644 struct libcfs_ioctl_data data;
650 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
654 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
659 nid = libcfs_str2nid(argv[1]);
660 if (nid == LNET_NID_ANY) {
661 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
665 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
666 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
670 if (lnet_parse_port (&port, argv[3]) != 0) {
671 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
675 LIBCFS_IOC_INIT(data);
676 data.ioc_net = g_net;
678 data.ioc_u32[0] = ip;
679 data.ioc_u32[1] = port;
681 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
683 fprintf (stderr, "failed to add peer: %s\n",
692 jt_ptl_del_peer (int argc, char **argv)
694 struct libcfs_ioctl_data data;
695 lnet_nid_t nid = LNET_NID_ANY;
696 lnet_pid_t pid = LNET_PID_ANY;
700 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
703 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
705 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
709 } else if (argc > 2) {
710 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
715 !libcfs_str2anynid(&nid, argv[1])) {
716 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
720 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
722 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
723 fprintf (stderr, "Can't parse ip addr: %s\n",
729 LIBCFS_IOC_INIT(data);
730 data.ioc_net = g_net;
732 data.ioc_u32[0] = ip;
733 data.ioc_u32[1] = pid;
735 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
737 fprintf (stderr, "failed to remove peer: %s\n",
746 jt_ptl_print_connections (int argc, char **argv)
748 struct libcfs_ioctl_data data;
749 lnet_process_id_t id;
750 char buffer[2][HOST_NAME_MAX + 1];
754 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
757 for (index = 0; ; index++) {
758 LIBCFS_IOC_INIT(data);
759 data.ioc_net = g_net;
760 data.ioc_count = index;
762 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
766 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
767 id.nid = data.ioc_nid;
768 id.pid = data.ioc_u32[6];
769 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
771 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
772 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
773 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
774 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
775 data.ioc_u32[4], /* scheduler */
777 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
778 sizeof(buffer[0]), 1),
780 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
781 sizeof(buffer[1]), 1),
782 data.ioc_u32[1], /* remote port */
783 data.ioc_count, /* tx buffer size */
784 data.ioc_u32[5], /* rx buffer size */
785 data.ioc_flags ? "nagle" : "nonagle");
786 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
787 printf ("%s mtu %d\n",
788 libcfs_nid2str(data.ioc_nid),
789 data.ioc_u32[0]); /* path MTU */
790 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
791 printf ("%-20s [%d]\n",
792 libcfs_nid2str(data.ioc_nid),
793 data.ioc_u32[0] /* device id */);
795 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
800 if (errno == ENOENT) {
801 printf ("<no connections>\n");
803 fprintf(stderr, "Error getting connection list: %s: "
811 int jt_ptl_disconnect(int argc, char **argv)
813 struct libcfs_ioctl_data data;
814 lnet_nid_t nid = LNET_NID_ANY;
819 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
823 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
827 !libcfs_str2anynid(&nid, argv[1])) {
828 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
832 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
834 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
835 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
839 LIBCFS_IOC_INIT(data);
840 data.ioc_net = g_net;
842 data.ioc_u32[0] = ipaddr;
844 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
846 fprintf(stderr, "failed to remove connection: %s\n",
854 int jt_ptl_push_connection (int argc, char **argv)
856 struct libcfs_ioctl_data data;
858 lnet_nid_t nid = LNET_NID_ANY;
861 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
865 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
869 !libcfs_str2anynid(&nid, argv[1])) {
870 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
874 LIBCFS_IOC_INIT(data);
875 data.ioc_net = g_net;
878 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
880 fprintf(stderr, "failed to push connection: %s\n",
888 int jt_ptl_ping(int argc, char **argv)
892 lnet_process_id_t id;
893 lnet_process_id_t ids[16];
894 int maxids = sizeof(ids)/sizeof(ids[0]);
895 struct libcfs_ioctl_data data;
900 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
904 sep = strchr(argv[1], '-');
906 rc = lnet_parse_nid(argv[1], &id);
912 if (argv[1][0] == 'u' ||
914 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
916 id.pid = strtoul(argv[1], &end, 0);
918 if (end != sep) { /* assuming '-' is part of hostname */
919 rc = lnet_parse_nid(argv[1], &id);
923 id.nid = libcfs_str2nid(sep + 1);
925 if (id.nid == LNET_NID_ANY) {
927 "Can't parse process id \"%s\"\n",
935 timeout = 1000 * atol(argv[2]);
937 timeout = 1000; /* default 1 second timeout */
939 LIBCFS_IOC_INIT (data);
940 data.ioc_nid = id.nid;
941 data.ioc_u32[0] = id.pid;
942 data.ioc_u32[1] = timeout;
943 data.ioc_plen1 = sizeof(ids);
944 data.ioc_pbuf1 = (char *)ids;
946 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
948 fprintf(stderr, "failed to ping %s: %s\n",
949 id.pid == LNET_PID_ANY ?
950 libcfs_nid2str(id.nid) : libcfs_id2str(id),
955 for (i = 0; i < data.ioc_count && i < maxids; i++)
956 printf("%s\n", libcfs_id2str(ids[i]));
958 if (data.ioc_count > maxids)
959 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
964 int jt_ptl_mynid(int argc, char **argv)
966 struct libcfs_ioctl_data data;
971 fprintf(stderr, "usage: %s NID\n", argv[0]);
975 nid = libcfs_str2nid(argv[1]);
976 if (nid == LNET_NID_ANY) {
977 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
981 LIBCFS_IOC_INIT(data);
982 data.ioc_net = LNET_NIDNET(nid);
985 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
987 fprintf(stderr, "setting my NID failed: %s\n",
990 printf("registered my nid %s\n", libcfs_nid2str(nid));
996 jt_ptl_fail_nid (int argc, char **argv)
1001 struct libcfs_ioctl_data data;
1003 if (argc < 2 || argc > 3)
1005 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1009 if (!libcfs_str2anynid(&nid, argv[1]))
1011 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1016 threshold = LNET_MD_THRESH_INF;
1017 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1018 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1022 LIBCFS_IOC_INIT (data);
1024 data.ioc_count = threshold;
1026 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1028 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1031 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1037 jt_ptl_add_route (int argc, char **argv)
1039 struct lnet_ioctl_config_data data;
1040 lnet_nid_t gateway_nid;
1041 unsigned int hops = 1;
1042 unsigned int priority = 0;
1046 if (argc < 2 || argc > 4) {
1047 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1052 if (g_net_is_set(argv[0]) == 0)
1055 gateway_nid = libcfs_str2nid(argv[1]);
1056 if (gateway_nid == LNET_NID_ANY) {
1057 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1062 hops = strtoul(argv[2], &end, 0);
1063 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1064 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1069 priority = strtoul(argv[3], &end, 0);
1070 if (end != NULL && *end != 0) {
1072 "Can't parse priority \"%s\"\n",
1079 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1080 data.cfg_net = g_net;
1081 data.cfg_config_u.cfg_route.rtr_hop = hops;
1082 data.cfg_nid = gateway_nid;
1083 data.cfg_config_u.cfg_route.rtr_priority = priority;
1085 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1087 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1096 jt_ptl_del_route (int argc, char **argv)
1098 struct lnet_ioctl_config_data data;
1103 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1107 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1108 fprintf(stderr, "Can't parse gateway NID "
1109 "\"%s\"\n", argv[1]);
1113 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1114 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1117 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1119 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1120 libcfs_nid2str(nid), strerror(errno));
1128 jt_ptl_notify_router (int argc, char **argv)
1130 struct libcfs_ioctl_data data;
1139 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1144 nid = libcfs_str2nid(argv[1]);
1145 if (nid == LNET_NID_ANY) {
1146 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1150 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1151 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1155 gettimeofday(&now, NULL);
1159 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1160 fprintf(stderr, "Can't parse time %s\n"
1161 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1162 "or an absolute unix time in seconds\n", argv[3]);
1164 } else if (when > now.tv_sec) {
1165 fprintf (stderr, "%s specifies a time in the future\n",
1170 LIBCFS_IOC_INIT(data);
1172 data.ioc_flags = enable;
1173 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1174 data.ioc_u64[0] = (__u64)when;
1176 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1178 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1179 libcfs_nid2str(nid), strerror (errno));
1187 jt_ptl_print_routes (int argc, char **argv)
1189 struct lnet_ioctl_config_data data;
1198 for (index = 0; ; index++) {
1199 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1200 data.cfg_count = index;
1202 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1207 hops = data.cfg_config_u.cfg_route.rtr_hop;
1209 alive = data.cfg_config_u.cfg_route.rtr_flags;
1210 pri = data.cfg_config_u.cfg_route.rtr_priority;
1212 printf("net %18s hops %u gw %32s %s pri %u\n",
1213 libcfs_net2str(net), hops,
1214 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1217 if (errno != ENOENT)
1218 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1225 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1231 /* NB: can't support range ipaddress except * and *@net */
1232 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1233 net = libcfs_str2net(str + 2);
1234 if (net == LNET_NIDNET(LNET_NID_ANY))
1237 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1239 rc = libcfs_str2anynid(&nid, str);
1247 fprintf(stderr, "Invalid NID : %s\n", str);
1252 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1254 if (!strcasecmp(msg_str, "put")) {
1255 *mask_p |= LNET_PUT_BIT;
1258 } else if (!strcasecmp(msg_str, "ack")) {
1259 *mask_p |= LNET_ACK_BIT;
1262 } else if (!strcasecmp(msg_str, "get")) {
1263 *mask_p |= LNET_GET_BIT;
1266 } else if (!strcasecmp(msg_str, "reply")) {
1267 *mask_p |= LNET_REPLY_BIT;
1271 fprintf(stderr, "unknown message type %s\n", msg_str);
1276 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1278 unsigned long rc = strtoul(optarg, NULL, 0);
1281 fprintf(stderr, "invalid portal: %lu\n", rc);
1285 *mask_p |= (1ULL << rc);
1290 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1292 struct libcfs_ioctl_data data = {{0}};
1293 struct lnet_fault_attr attr;
1297 static struct option opts[] = {
1298 {"source", required_argument, 0, 's'},
1299 {"dest", required_argument, 0, 'd'},
1300 {"rate", required_argument, 0, 'r'},
1301 {"interval", required_argument, 0, 'i'},
1302 {"latency", required_argument, 0, 'l'},
1303 {"portal", required_argument, 0, 'p'},
1304 {"message", required_argument, 0, 'm'},
1309 fprintf(stderr, "Failed, please provide source, destination "
1310 "and rate of rule\n");
1314 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1315 memset(&attr, 0, sizeof(attr));
1317 char c = getopt_long(argc, argv, optstr, opts, NULL);
1323 case 's': /* source NID/NET */
1324 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1329 case 'd': /* dest NID/NET */
1330 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1335 case 'r': /* drop rate */
1336 if (opc == LNET_CTL_DROP_ADD)
1337 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1339 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1342 case 'i': /* time interval (# seconds) for message drop */
1343 if (opc == LNET_CTL_DROP_ADD)
1344 attr.u.drop.da_interval = strtoul(optarg,
1347 attr.u.delay.la_interval = strtoul(optarg,
1351 case 'l': /* seconds to wait before activating rule */
1352 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1355 case 'p': /* portal to filter */
1356 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1361 case 'm': /* message types to filter */
1362 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1368 fprintf(stderr, "error: %s: option '%s' "
1369 "unrecognized\n", argv[0], argv[optind - 1]);
1375 if (opc == LNET_CTL_DROP_ADD) {
1376 /* NB: drop rate and interval are exclusive to each other */
1377 if (!((attr.u.drop.da_rate == 0) ^
1378 (attr.u.drop.da_interval == 0))) {
1380 "please provide either drop rate or interval "
1381 "but not both at the same time.\n");
1384 } else if (opc == LNET_CTL_DELAY_ADD) {
1385 if (!((attr.u.delay.la_rate == 0) ^
1386 (attr.u.delay.la_interval == 0))) {
1388 "please provide either delay rate or interval "
1389 "but not both at the same time.\n");
1393 if (attr.u.delay.la_latency == 0) {
1394 fprintf(stderr, "latency cannot be zero\n");
1399 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1400 fprintf(stderr, "Please provide both source and destination "
1401 "of %s rule\n", name);
1405 data.ioc_flags = opc;
1406 data.ioc_inllen1 = sizeof(attr);
1407 data.ioc_inlbuf1 = (char *)&attr;
1408 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1409 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1413 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1415 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1416 name, libcfs_nid2str(attr.fa_src),
1417 libcfs_nid2str(attr.fa_dst), strerror(errno));
1421 printf("Added %s rule %s->%s (1/%d)\n",
1422 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1423 opc == LNET_CTL_DROP_ADD ?
1424 attr.u.drop.da_rate : attr.u.delay.la_rate);
1433 jt_ptl_drop_add(int argc, char **argv)
1435 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1439 jt_ptl_delay_add(int argc, char **argv)
1441 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1445 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1447 struct libcfs_ioctl_data data = {{0}};
1448 struct lnet_fault_attr attr;
1452 static struct option opts[] = {
1453 {"source", required_argument, 0, 's'},
1454 {"dest", required_argument, 0, 'd'},
1455 {"all", no_argument, 0, 'a'},
1460 fprintf(stderr, "Failed, please provide source and "
1461 "destination of rule\n");
1465 memset(&attr, 0, sizeof(attr));
1467 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1474 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1479 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1484 attr.fa_src = attr.fa_dst = 0;
1488 fprintf(stderr, "error: %s: option '%s' "
1489 "unrecognized\n", argv[0], argv[optind - 1]);
1495 data.ioc_flags = opc;
1496 data.ioc_inllen1 = sizeof(attr);
1497 data.ioc_inlbuf1 = (char *)&attr;
1498 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1499 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1503 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1505 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1506 all ? "all" : libcfs_nid2str(attr.fa_src),
1507 all ? "all" : libcfs_nid2str(attr.fa_dst),
1512 libcfs_ioctl_unpack(&data, ioc_buf);
1513 printf("Removed %d %s rules\n", data.ioc_count, name);
1522 jt_ptl_drop_del(int argc, char **argv)
1524 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1528 jt_ptl_delay_del(int argc, char **argv)
1530 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1534 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1536 struct libcfs_ioctl_data data = {{0}};
1539 LIBCFS_IOC_INIT(data);
1540 data.ioc_flags = opc;
1542 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1544 fprintf(stderr, "failed to reset %s stats: %s\n",
1545 name, strerror(errno));
1552 jt_ptl_drop_reset(int argc, char **argv)
1554 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1558 jt_ptl_delay_reset(int argc, char **argv)
1560 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1565 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1567 struct libcfs_ioctl_data data = {{0}};
1568 struct lnet_fault_attr attr;
1569 struct lnet_fault_stat stat;
1572 printf("LNet %s rules:\n", name);
1573 for (pos = 0;; pos++) {
1576 memset(&attr, 0, sizeof(attr));
1577 memset(&stat, 0, sizeof(stat));
1579 data.ioc_count = pos;
1580 data.ioc_flags = opc;
1581 data.ioc_inllen1 = sizeof(attr);
1582 data.ioc_inlbuf1 = (char *)&attr;
1583 data.ioc_inllen2 = sizeof(stat);
1584 data.ioc_inlbuf2 = (char *)&stat;
1585 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1586 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1590 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1594 libcfs_ioctl_unpack(&data, ioc_buf);
1596 if (opc == LNET_CTL_DROP_LIST) {
1597 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1598 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1599 LPU64", REP "LPU64"\n",
1600 libcfs_nid2str(attr.fa_src),
1601 libcfs_nid2str(attr.fa_dst),
1602 attr.u.drop.da_rate, attr.u.drop.da_interval,
1603 attr.fa_ptl_mask, attr.fa_msg_mask,
1604 stat.u.drop.ds_dropped, stat.fs_count,
1605 stat.fs_put, stat.fs_ack,
1606 stat.fs_get, stat.fs_reply);
1608 } else if (opc == LNET_CTL_DELAY_LIST) {
1609 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1610 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1611 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1612 libcfs_nid2str(attr.fa_src),
1613 libcfs_nid2str(attr.fa_dst),
1614 attr.u.delay.la_rate, attr.u.delay.la_interval,
1615 attr.u.delay.la_latency,
1616 attr.fa_ptl_mask, attr.fa_msg_mask,
1617 stat.u.delay.ls_delayed, stat.fs_count,
1618 stat.fs_put, stat.fs_ack, stat.fs_get,
1622 printf("found total %d\n", pos);
1628 jt_ptl_drop_list(int argc, char **argv)
1630 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1634 jt_ptl_delay_list(int argc, char **argv)
1636 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1640 get_cycles_per_usec ()
1642 FILE *f = fopen ("/proc/cpuinfo", "r");
1647 while (fgets (line, sizeof (line), f) != NULL)
1648 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1655 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1659 int jt_ptl_memhog(int argc, char **argv)
1661 static int gfp = 0; /* sticky! */
1663 struct libcfs_ioctl_data data;
1669 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1673 count = strtol(argv[1], &end, 0);
1674 if (count < 0 || *end != 0) {
1675 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1680 rc = strtol(argv[2], &end, 0);
1682 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1688 LIBCFS_IOC_INIT(data);
1689 data.ioc_count = count;
1690 data.ioc_flags = gfp;
1691 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1694 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1698 printf("memhog %d OK\n", count);
1702 int jt_ptl_testprotocompat(int argc, char **argv)
1704 struct libcfs_ioctl_data data;
1710 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1714 flags = strtol(argv[1], &end, 0);
1715 if (flags < 0 || *end != 0) {
1716 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1720 LIBCFS_IOC_INIT(data);
1721 data.ioc_flags = flags;
1722 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1725 fprintf(stderr, "test proto compat %x failed: %s\n",
1726 flags, strerror(errno));
1730 printf("test proto compat %x OK\n", flags);