2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2015, Intel Corporation.
6 * This file is part of Lustre, https://wiki.hpdd.intel.com/
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.
33 #include <sys/ioctl.h>
36 #include <linux/types.h>
38 #include <libcfs/util/string.h>
39 #include <libcfs/util/ioctl.h>
40 #include <libcfs/user-time.h>
41 #include <libcfs/libcfs_debug.h>
42 #include <lnet/lnetctl.h>
43 #include <lnet/socklnd.h>
44 #include <lnet/lnet.h>
46 unsigned int libcfs_debug;
47 unsigned int libcfs_printk = D_CANTMASK;
49 static bool g_net_interactive;
50 static bool g_net_set;
53 #define IOC_BUF_SIZE 8192
54 static char local_buf[IOC_BUF_SIZE];
55 static char *ioc_buf = local_buf;
57 /* Convert a string boolean to an int; "enable" -> 1 */
59 lnet_parse_bool (int *b, char *str)
61 if (!strcasecmp (str, "no") ||
62 !strcasecmp (str, "n") ||
63 !strcasecmp (str, "off") ||
64 !strcasecmp (str, "down") ||
65 !strcasecmp (str, "disable"))
71 if (!strcasecmp (str, "yes") ||
72 !strcasecmp (str, "y") ||
73 !strcasecmp (str, "on") ||
74 !strcasecmp (str, "up") ||
75 !strcasecmp (str, "enable"))
85 lnet_parse_port (int *port, char *str)
89 *port = strtol (str, &end, 0);
91 if (*end == 0 && /* parsed whole string */
92 *port > 0 && *port < 65536) /* minimal sanity check */
98 #ifdef HAVE_GETHOSTBYNAME
99 static struct hostent *
100 ptl_gethostbyname(char * hname) {
102 he = gethostbyname(hname);
107 fprintf(stderr, "Unable to resolve hostname: %s\n",
111 fprintf(stderr, "gethostbyname error for %s: %s\n",
112 hname, strerror(h_errno));
122 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
129 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
130 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
131 (c & ~0xff) == 0 && (d & ~0xff) == 0)
133 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
141 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
143 #ifdef HAVE_GETHOSTBYNAME
147 if (!strcmp (str, "_all_")) {
152 if (lnet_parse_ipquad(ipaddrp, str) == 0)
155 #ifdef HAVE_GETHOSTBYNAME
156 if ((('a' <= str[0] && str[0] <= 'z') ||
157 ('A' <= str[0] && str[0] <= 'Z')) &&
158 (he = ptl_gethostbyname (str)) != NULL) {
159 __u32 addr = *(__u32 *)he->h_addr;
161 *ipaddrp = ntohl(addr); /* HOST byte order */
170 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
172 #ifdef HAVE_GETHOSTBYNAME
177 net_ip = htonl (ipaddr);
178 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
180 strlcpy(str, he->h_name, strsize);
186 sprintf (str, "%d.%d.%d.%d",
187 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
188 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
193 lnet_parse_time (time_t *t, char *str)
199 *t = strtol (str, &end, 0);
200 if (*end == 0) /* parsed whole string */
203 memset (&tm, 0, sizeof (tm));
204 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
205 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
206 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
210 tm.tm_mon--; /* convert to 0 == Jan */
211 tm.tm_year -= 1900; /* y2k quirk */
212 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
215 if (*t == (time_t)-1)
222 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
224 id_ptr->pid = LNET_PID_ANY;
225 id_ptr->nid = libcfs_str2nid(nid_str);
226 if (id_ptr->nid == LNET_NID_ANY) {
227 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
234 static int g_net_is_set(char *cmd)
242 if (g_net_interactive)
248 "You must run '%s <network>' command before '%s'\n",
256 static int g_net_is_compatible(char *cmd, ...)
261 if (!g_net_is_set(cmd))
267 nal = va_arg(ap, int);
268 if (nal == LNET_NETTYP(g_net)) {
278 "Command %s not compatible with %s NAL\n",
280 libcfs_lnd2str(LNET_NETTYP(g_net)));
285 int ptl_initialize(int argc, char **argv)
288 g_net_interactive = true;
290 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
291 LNET_DEV_MAJOR, LNET_DEV_MINOR);
296 int jt_ptl_network(int argc, char **argv)
298 struct libcfs_ioctl_data data;
299 __u32 net = LNET_NIDNET(LNET_NID_ANY);
303 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
307 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
308 LIBCFS_IOC_INIT(data);
309 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
312 printf("LNET ready to unload\n");
317 fprintf(stderr, "LNET busy\n");
319 fprintf(stderr, "LNET unconfigure error %d: %s\n",
320 errno, strerror(errno));
322 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
323 LIBCFS_IOC_INIT(data);
324 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
327 printf("LNET configured\n");
331 fprintf(stderr, "LNET configure error %d: %s\n",
332 errno, strerror(errno));
336 net = libcfs_str2net(argv[1]);
337 if (net == LNET_NIDNET(LNET_NID_ANY)) {
338 fprintf(stderr, "Can't parse net %s\n", argv[1]);
342 if (LNET_NETTYP(net) == QSWLND || LNET_NETTYP(net) == GMLND ||
343 LNET_NETTYP(net) == PTLLND || LNET_NETTYP(net) == CIBLND ||
344 LNET_NETTYP(net) == OPENIBLND || LNET_NETTYP(net) == IIBLND ||
345 LNET_NETTYP(net) == RALND || LNET_NETTYP(net) == VIBLND ||
346 LNET_NETTYP(net) == MXLND) {
347 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
357 jt_ptl_list_nids(int argc, char **argv)
359 struct libcfs_ioctl_data data;
360 int all = 0, return_nid = 0;
364 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
365 /* Hack to pass back value */
366 return_nid = (argc == 2) && (argv[1][0] == 1);
368 if ((argc > 2) && !(all || return_nid)) {
369 fprintf(stderr, "usage: %s [all]\n", argv[0]);
373 for (count = 0;; count++) {
374 LIBCFS_IOC_INIT (data);
375 data.ioc_count = count;
376 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
379 if ((count > 0) && (errno == ENOENT))
380 /* We found them all */
382 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
383 errno, strerror(errno));
387 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
388 printf("%s\n", libcfs_nid2str(data.ioc_nid));
390 *(__u64 *)(argv[1]) = data.ioc_nid;
400 jt_ptl_which_nid (int argc, char **argv)
402 struct libcfs_ioctl_data data;
405 lnet_nid_t best_nid = LNET_NID_ANY;
414 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
418 for (i = 1; i < argc; i++) {
420 nid = libcfs_str2nid(nidstr);
421 if (nid == LNET_NID_ANY) {
422 fprintf(stderr, "Can't parse NID %s\n", nidstr);
426 LIBCFS_IOC_INIT(data);
429 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
431 fprintf(stderr, "Can't get distance to %s: %s\n",
432 nidstr, strerror(errno));
436 dist = data.ioc_u32[0];
437 order = data.ioc_u32[1];
440 if (dist == -EHOSTUNREACH)
443 fprintf(stderr, "Unexpected distance to %s: %d\n",
448 if (best_nid == LNET_NID_ANY ||
450 (dist == best_dist && order < best_order)) {
457 if (best_nid == LNET_NID_ANY) {
458 fprintf(stderr, "No reachable NID\n");
462 printf("%s\n", libcfs_nid2str(best_nid));
467 jt_ptl_print_interfaces (int argc, char **argv)
469 struct libcfs_ioctl_data data;
470 char buffer[3][HOST_NAME_MAX + 1];
474 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
477 for (index = 0;;index++) {
478 LIBCFS_IOC_INIT(data);
479 data.ioc_net = g_net;
480 data.ioc_count = index;
482 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
486 printf ("%s: (%s/%s) npeer %d nroute %d\n",
487 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
488 sizeof(buffer[2]), 1),
489 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
490 sizeof(buffer[0]), 0),
491 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
492 sizeof(buffer[1]), 0),
493 data.ioc_u32[2], data.ioc_u32[3]);
497 if (errno == ENOENT) {
498 printf ("<no interfaces>\n");
500 fprintf(stderr, "Error getting interfaces: %s: "
510 jt_ptl_add_interface (int argc, char **argv)
512 struct libcfs_ioctl_data data;
515 __u32 netmask = 0xffffff00;
520 if (argc < 2 || argc > 3) {
521 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
525 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
528 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
529 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
534 count = strtol(argv[2], &end, 0);
535 if (count > 0 && count < 32 && *end == 0) {
537 for (i = count; i > 0; i--)
538 netmask = netmask|(1<<(32-i));
539 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
540 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
545 LIBCFS_IOC_INIT(data);
546 data.ioc_net = g_net;
547 data.ioc_u32[0] = ipaddr;
548 data.ioc_u32[1] = netmask;
550 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
552 fprintf (stderr, "failed to add interface: %s\n",
561 jt_ptl_del_interface (int argc, char **argv)
563 struct libcfs_ioctl_data data;
568 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
572 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
576 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
577 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
581 LIBCFS_IOC_INIT(data);
582 data.ioc_net = g_net;
583 data.ioc_u32[0] = ipaddr;
585 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
587 fprintf (stderr, "failed to delete interface: %s\n",
596 jt_ptl_print_peers (int argc, char **argv)
598 struct libcfs_ioctl_data data;
599 lnet_process_id_t id;
600 char buffer[2][HOST_NAME_MAX + 1];
604 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
607 for (index = 0;;index++) {
608 LIBCFS_IOC_INIT(data);
609 data.ioc_net = g_net;
610 data.ioc_count = index;
612 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
616 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
617 id.nid = data.ioc_nid;
618 id.pid = data.ioc_u32[4];
619 printf ("%-20s [%d]%s->%s:%d #%d\n",
621 data.ioc_count, /* persistence */
623 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
624 sizeof(buffer[0]), 1),
626 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
627 sizeof(buffer[1]), 1),
628 data.ioc_u32[1], /* peer port */
629 data.ioc_u32[3]); /* conn_count */
630 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
631 int disconn = data.ioc_flags >> 16;
637 state = data.ioc_flags & 0xffff ? "C" : "U";
639 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
640 libcfs_nid2str(data.ioc_nid), /* peer nid */
641 data.ioc_net, /* gemini device id */
642 state, /* peer is Connecting, Up, or Down */
643 data.ioc_count, /* peer refcount */
644 (uintmax_t)data.ioc_u64[0], /* peerstamp */
645 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
646 /* fmaq, nfma, nrdma */
647 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
649 printf ("%-20s [%d]\n",
650 libcfs_nid2str(data.ioc_nid), data.ioc_count);
655 if (errno == ENOENT) {
656 printf ("<no peers>\n");
658 fprintf(stderr, "Error getting peer list: %s: "
666 int jt_ptl_add_peer(int argc, char **argv)
668 struct libcfs_ioctl_data data;
674 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
678 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
683 nid = libcfs_str2nid(argv[1]);
684 if (nid == LNET_NID_ANY) {
685 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
689 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
690 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
694 if (lnet_parse_port (&port, argv[3]) != 0) {
695 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
699 LIBCFS_IOC_INIT(data);
700 data.ioc_net = g_net;
702 data.ioc_u32[0] = ip;
703 data.ioc_u32[1] = port;
705 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
707 fprintf (stderr, "failed to add peer: %s\n",
716 jt_ptl_del_peer (int argc, char **argv)
718 struct libcfs_ioctl_data data;
719 lnet_nid_t nid = LNET_NID_ANY;
720 lnet_pid_t pid = LNET_PID_ANY;
724 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
727 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
729 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
733 } else if (argc > 2) {
734 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
739 !libcfs_str2anynid(&nid, argv[1])) {
740 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
744 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
746 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
747 fprintf (stderr, "Can't parse ip addr: %s\n",
753 LIBCFS_IOC_INIT(data);
754 data.ioc_net = g_net;
756 data.ioc_u32[0] = ip;
757 data.ioc_u32[1] = pid;
759 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
761 fprintf (stderr, "failed to remove peer: %s\n",
770 jt_ptl_print_connections (int argc, char **argv)
772 struct libcfs_ioctl_data data;
773 lnet_process_id_t id;
774 char buffer[2][HOST_NAME_MAX + 1];
778 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
781 for (index = 0; ; index++) {
782 LIBCFS_IOC_INIT(data);
783 data.ioc_net = g_net;
784 data.ioc_count = index;
786 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
790 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
791 id.nid = data.ioc_nid;
792 id.pid = data.ioc_u32[6];
793 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
795 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
796 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
797 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
798 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
799 data.ioc_u32[4], /* scheduler */
801 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
802 sizeof(buffer[0]), 1),
804 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
805 sizeof(buffer[1]), 1),
806 data.ioc_u32[1], /* remote port */
807 data.ioc_count, /* tx buffer size */
808 data.ioc_u32[5], /* rx buffer size */
809 data.ioc_flags ? "nagle" : "nonagle");
810 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
811 printf ("%s mtu %d\n",
812 libcfs_nid2str(data.ioc_nid),
813 data.ioc_u32[0]); /* path MTU */
814 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
815 printf ("%-20s [%d]\n",
816 libcfs_nid2str(data.ioc_nid),
817 data.ioc_u32[0] /* device id */);
819 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
824 if (errno == ENOENT) {
825 printf ("<no connections>\n");
827 fprintf(stderr, "Error getting connection list: %s: "
835 int jt_ptl_disconnect(int argc, char **argv)
837 struct libcfs_ioctl_data data;
838 lnet_nid_t nid = LNET_NID_ANY;
843 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
847 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
851 !libcfs_str2anynid(&nid, argv[1])) {
852 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
856 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
858 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
859 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
863 LIBCFS_IOC_INIT(data);
864 data.ioc_net = g_net;
866 data.ioc_u32[0] = ipaddr;
868 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
870 fprintf(stderr, "failed to remove connection: %s\n",
878 int jt_ptl_push_connection (int argc, char **argv)
880 struct libcfs_ioctl_data data;
882 lnet_nid_t nid = LNET_NID_ANY;
885 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
889 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
893 !libcfs_str2anynid(&nid, argv[1])) {
894 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
898 LIBCFS_IOC_INIT(data);
899 data.ioc_net = g_net;
902 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
904 fprintf(stderr, "failed to push connection: %s\n",
912 int jt_ptl_ping(int argc, char **argv)
916 lnet_process_id_t id;
917 lnet_process_id_t ids[16];
918 int maxids = sizeof(ids)/sizeof(ids[0]);
919 struct libcfs_ioctl_data data;
924 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
928 sep = strchr(argv[1], '-');
930 rc = lnet_parse_nid(argv[1], &id);
936 if (argv[1][0] == 'u' ||
938 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
940 id.pid = strtoul(argv[1], &end, 0);
942 if (end != sep) { /* assuming '-' is part of hostname */
943 rc = lnet_parse_nid(argv[1], &id);
947 id.nid = libcfs_str2nid(sep + 1);
949 if (id.nid == LNET_NID_ANY) {
951 "Can't parse process id \"%s\"\n",
959 timeout = 1000 * atol(argv[2]);
961 timeout = 1000; /* default 1 second timeout */
963 LIBCFS_IOC_INIT (data);
964 data.ioc_nid = id.nid;
965 data.ioc_u32[0] = id.pid;
966 data.ioc_u32[1] = timeout;
967 data.ioc_plen1 = sizeof(ids);
968 data.ioc_pbuf1 = (char *)ids;
970 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
972 fprintf(stderr, "failed to ping %s: %s\n",
973 id.pid == LNET_PID_ANY ?
974 libcfs_nid2str(id.nid) : libcfs_id2str(id),
979 for (i = 0; i < data.ioc_count && i < maxids; i++)
980 printf("%s\n", libcfs_id2str(ids[i]));
982 if (data.ioc_count > maxids)
983 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
988 int jt_ptl_mynid(int argc, char **argv)
990 struct libcfs_ioctl_data data;
995 fprintf(stderr, "usage: %s NID\n", argv[0]);
999 nid = libcfs_str2nid(argv[1]);
1000 if (nid == LNET_NID_ANY) {
1001 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1005 LIBCFS_IOC_INIT(data);
1006 data.ioc_net = LNET_NIDNET(nid);
1009 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1011 fprintf(stderr, "setting my NID failed: %s\n",
1014 printf("registered my nid %s\n", libcfs_nid2str(nid));
1020 jt_ptl_fail_nid (int argc, char **argv)
1025 struct libcfs_ioctl_data data;
1027 if (argc < 2 || argc > 3)
1029 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1033 if (!libcfs_str2anynid(&nid, argv[1]))
1035 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1040 threshold = LNET_MD_THRESH_INF;
1041 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1042 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1046 LIBCFS_IOC_INIT (data);
1048 data.ioc_count = threshold;
1050 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1052 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1055 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1061 jt_ptl_add_route (int argc, char **argv)
1063 struct lnet_ioctl_config_data data;
1064 lnet_nid_t gateway_nid;
1065 __u32 hops = LNET_UNDEFINED_HOPS;
1066 unsigned int priority = 0;
1070 if (argc < 2 || argc > 4) {
1071 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1076 if (g_net_is_set(argv[0]) == 0)
1079 gateway_nid = libcfs_str2nid(argv[1]);
1080 if (gateway_nid == LNET_NID_ANY) {
1081 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1086 hops = strtol(argv[2], &end, 0);
1087 if (hops == 0 || hops >= 256 ||
1088 (end != NULL && *end != 0)) {
1089 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1094 priority = strtoul(argv[3], &end, 0);
1095 if (end != NULL && *end != 0) {
1097 "Can't parse priority \"%s\"\n",
1104 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1105 data.cfg_net = g_net;
1106 data.cfg_config_u.cfg_route.rtr_hop = hops;
1107 data.cfg_nid = gateway_nid;
1108 data.cfg_config_u.cfg_route.rtr_priority = priority;
1110 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1112 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1121 jt_ptl_del_route (int argc, char **argv)
1123 struct lnet_ioctl_config_data data;
1128 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1132 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1133 fprintf(stderr, "Can't parse gateway NID "
1134 "\"%s\"\n", argv[1]);
1138 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1139 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1142 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1144 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1145 libcfs_nid2str(nid), strerror(errno));
1153 jt_ptl_notify_router (int argc, char **argv)
1155 struct libcfs_ioctl_data data;
1164 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1169 nid = libcfs_str2nid(argv[1]);
1170 if (nid == LNET_NID_ANY) {
1171 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1175 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1176 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1180 gettimeofday(&now, NULL);
1184 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1185 fprintf(stderr, "Can't parse time %s\n"
1186 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1187 "or an absolute unix time in seconds\n", argv[3]);
1189 } else if (when > now.tv_sec) {
1190 fprintf (stderr, "%s specifies a time in the future\n",
1195 LIBCFS_IOC_INIT(data);
1197 data.ioc_flags = enable;
1198 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1199 data.ioc_u64[0] = (__u64)when;
1201 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1203 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1204 libcfs_nid2str(nid), strerror (errno));
1212 jt_ptl_print_routes (int argc, char **argv)
1214 struct lnet_ioctl_config_data data;
1223 for (index = 0; ; index++) {
1224 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1225 data.cfg_count = index;
1227 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1232 hops = data.cfg_config_u.cfg_route.rtr_hop;
1234 alive = data.cfg_config_u.cfg_route.rtr_flags;
1235 pri = data.cfg_config_u.cfg_route.rtr_priority;
1237 printf("net %18s hops %u gw %32s %s pri %u\n",
1238 libcfs_net2str(net), hops,
1239 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1242 if (errno != ENOENT)
1243 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1250 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1256 /* NB: can't support range ipaddress except * and *@net */
1257 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1258 net = libcfs_str2net(str + 2);
1259 if (net == LNET_NIDNET(LNET_NID_ANY))
1262 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1264 rc = libcfs_str2anynid(&nid, str);
1272 fprintf(stderr, "Invalid NID : %s\n", str);
1277 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1279 if (!strcasecmp(msg_str, "put")) {
1280 *mask_p |= LNET_PUT_BIT;
1283 } else if (!strcasecmp(msg_str, "ack")) {
1284 *mask_p |= LNET_ACK_BIT;
1287 } else if (!strcasecmp(msg_str, "get")) {
1288 *mask_p |= LNET_GET_BIT;
1291 } else if (!strcasecmp(msg_str, "reply")) {
1292 *mask_p |= LNET_REPLY_BIT;
1296 fprintf(stderr, "unknown message type %s\n", msg_str);
1301 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1303 unsigned long rc = strtoul(optarg, NULL, 0);
1306 fprintf(stderr, "invalid portal: %lu\n", rc);
1310 *mask_p |= (1ULL << rc);
1315 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1317 struct libcfs_ioctl_data data = {{0}};
1318 struct lnet_fault_attr attr;
1322 static struct option opts[] = {
1323 {"source", required_argument, 0, 's'},
1324 {"dest", required_argument, 0, 'd'},
1325 {"rate", required_argument, 0, 'r'},
1326 {"interval", required_argument, 0, 'i'},
1327 {"latency", required_argument, 0, 'l'},
1328 {"portal", required_argument, 0, 'p'},
1329 {"message", required_argument, 0, 'm'},
1334 fprintf(stderr, "Failed, please provide source, destination "
1335 "and rate of rule\n");
1339 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1340 memset(&attr, 0, sizeof(attr));
1342 char c = getopt_long(argc, argv, optstr, opts, NULL);
1348 case 's': /* source NID/NET */
1349 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1354 case 'd': /* dest NID/NET */
1355 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1360 case 'r': /* drop rate */
1361 if (opc == LNET_CTL_DROP_ADD)
1362 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1364 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1367 case 'i': /* time interval (# seconds) for message drop */
1368 if (opc == LNET_CTL_DROP_ADD)
1369 attr.u.drop.da_interval = strtoul(optarg,
1372 attr.u.delay.la_interval = strtoul(optarg,
1376 case 'l': /* seconds to wait before activating rule */
1377 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1380 case 'p': /* portal to filter */
1381 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1386 case 'm': /* message types to filter */
1387 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1393 fprintf(stderr, "error: %s: option '%s' "
1394 "unrecognized\n", argv[0], argv[optind - 1]);
1400 if (opc == LNET_CTL_DROP_ADD) {
1401 /* NB: drop rate and interval are exclusive to each other */
1402 if (!((attr.u.drop.da_rate == 0) ^
1403 (attr.u.drop.da_interval == 0))) {
1405 "please provide either drop rate or interval "
1406 "but not both at the same time.\n");
1409 } else if (opc == LNET_CTL_DELAY_ADD) {
1410 if (!((attr.u.delay.la_rate == 0) ^
1411 (attr.u.delay.la_interval == 0))) {
1413 "please provide either delay rate or interval "
1414 "but not both at the same time.\n");
1418 if (attr.u.delay.la_latency == 0) {
1419 fprintf(stderr, "latency cannot be zero\n");
1424 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1425 fprintf(stderr, "Please provide both source and destination "
1426 "of %s rule\n", name);
1430 data.ioc_flags = opc;
1431 data.ioc_inllen1 = sizeof(attr);
1432 data.ioc_inlbuf1 = (char *)&attr;
1433 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1434 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1438 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1440 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1441 name, libcfs_nid2str(attr.fa_src),
1442 libcfs_nid2str(attr.fa_dst), strerror(errno));
1446 printf("Added %s rule %s->%s (1/%d)\n",
1447 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1448 opc == LNET_CTL_DROP_ADD ?
1449 attr.u.drop.da_rate : attr.u.delay.la_rate);
1458 jt_ptl_drop_add(int argc, char **argv)
1460 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1464 jt_ptl_delay_add(int argc, char **argv)
1466 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1470 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1472 struct libcfs_ioctl_data data = {{0}};
1473 struct lnet_fault_attr attr;
1477 static struct option opts[] = {
1478 {"source", required_argument, 0, 's'},
1479 {"dest", required_argument, 0, 'd'},
1480 {"all", no_argument, 0, 'a'},
1485 fprintf(stderr, "Failed, please provide source and "
1486 "destination of rule\n");
1490 memset(&attr, 0, sizeof(attr));
1492 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1499 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1504 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1509 attr.fa_src = attr.fa_dst = 0;
1513 fprintf(stderr, "error: %s: option '%s' "
1514 "unrecognized\n", argv[0], argv[optind - 1]);
1520 data.ioc_flags = opc;
1521 data.ioc_inllen1 = sizeof(attr);
1522 data.ioc_inlbuf1 = (char *)&attr;
1523 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1524 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1528 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1530 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1531 all ? "all" : libcfs_nid2str(attr.fa_src),
1532 all ? "all" : libcfs_nid2str(attr.fa_dst),
1537 libcfs_ioctl_unpack(&data, ioc_buf);
1538 printf("Removed %d %s rules\n", data.ioc_count, name);
1547 jt_ptl_drop_del(int argc, char **argv)
1549 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1553 jt_ptl_delay_del(int argc, char **argv)
1555 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1559 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1561 struct libcfs_ioctl_data data = {{0}};
1564 LIBCFS_IOC_INIT(data);
1565 data.ioc_flags = opc;
1567 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1569 fprintf(stderr, "failed to reset %s stats: %s\n",
1570 name, strerror(errno));
1577 jt_ptl_drop_reset(int argc, char **argv)
1579 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1583 jt_ptl_delay_reset(int argc, char **argv)
1585 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1590 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1592 struct libcfs_ioctl_data data = {{0}};
1593 struct lnet_fault_attr attr;
1594 struct lnet_fault_stat stat;
1597 printf("LNet %s rules:\n", name);
1598 for (pos = 0;; pos++) {
1601 memset(&attr, 0, sizeof(attr));
1602 memset(&stat, 0, sizeof(stat));
1604 data.ioc_count = pos;
1605 data.ioc_flags = opc;
1606 data.ioc_inllen1 = sizeof(attr);
1607 data.ioc_inlbuf1 = (char *)&attr;
1608 data.ioc_inllen2 = sizeof(stat);
1609 data.ioc_inlbuf2 = (char *)&stat;
1610 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1611 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1615 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1619 libcfs_ioctl_unpack(&data, ioc_buf);
1621 if (opc == LNET_CTL_DROP_LIST) {
1622 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1623 "%ju/%ju, PUT %ju, ACK %ju, GET "
1625 libcfs_nid2str(attr.fa_src),
1626 libcfs_nid2str(attr.fa_dst),
1627 attr.u.drop.da_rate, attr.u.drop.da_interval,
1628 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1629 (uintmax_t)stat.u.drop.ds_dropped,
1630 (uintmax_t)stat.fs_count,
1631 (uintmax_t)stat.fs_put,
1632 (uintmax_t)stat.fs_ack,
1633 (uintmax_t)stat.fs_get,
1634 (uintmax_t)stat.fs_reply);
1636 } else if (opc == LNET_CTL_DELAY_LIST) {
1637 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1638 ", msg %x, %ju/%ju, PUT %ju"
1639 ", ACK %ju, GET %ju, REP %ju\n",
1640 libcfs_nid2str(attr.fa_src),
1641 libcfs_nid2str(attr.fa_dst),
1642 attr.u.delay.la_rate, attr.u.delay.la_interval,
1643 attr.u.delay.la_latency,
1644 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1645 (uintmax_t)stat.u.delay.ls_delayed,
1646 (uintmax_t)stat.fs_count,
1647 (uintmax_t)stat.fs_put,
1648 (uintmax_t)stat.fs_ack,
1649 (uintmax_t)stat.fs_get,
1650 (uintmax_t)stat.fs_reply);
1653 printf("found total %d\n", pos);
1659 jt_ptl_drop_list(int argc, char **argv)
1661 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1665 jt_ptl_delay_list(int argc, char **argv)
1667 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1671 get_cycles_per_usec ()
1673 FILE *f = fopen ("/proc/cpuinfo", "r");
1678 while (fgets (line, sizeof (line), f) != NULL)
1679 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1686 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1690 int jt_ptl_testprotocompat(int argc, char **argv)
1692 struct libcfs_ioctl_data data;
1698 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1702 flags = strtol(argv[1], &end, 0);
1703 if (flags < 0 || *end != 0) {
1704 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1708 LIBCFS_IOC_INIT(data);
1709 data.ioc_flags = flags;
1710 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1713 fprintf(stderr, "test proto compat %x failed: %s\n",
1714 flags, strerror(errno));
1718 printf("test proto compat %x OK\n", flags);