2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2017, 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/ioctl.h>
39 #include <linux/lnet/libcfs_debug.h>
40 #include <linux/lnet/lnet-dlc.h>
41 #include <linux/lnet/lnetctl.h>
42 #include <linux/lnet/nidstr.h>
43 #include <linux/lnet/socklnd.h>
44 #include <lustre/lustreapi.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 snprintf(str, strsize, "%s", he->h_name);
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, struct lnet_process_id *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);
296 int jt_ptl_network(int argc, char **argv)
298 struct libcfs_ioctl_data data;
299 __u32 net = LNET_NIDNET(LNET_NID_ANY);
302 fprintf(stderr, "lctl %s: has been deprecated. Please use "
303 "'lnetctl net'\n", argv[0]);
306 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
310 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
311 LIBCFS_IOC_INIT(data);
312 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
315 printf("LNET ready to unload\n");
319 if (errno == ENODEV) {
320 printf("LNET is currently not loaded.");
325 fprintf(stderr, "LNET busy\n");
327 fprintf(stderr, "LNET unconfigure error %d: %s\n",
328 errno, strerror(errno));
330 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
331 LIBCFS_IOC_INIT(data);
332 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
335 printf("LNET configured\n");
339 fprintf(stderr, "LNET configure error %d: %s\n",
340 errno, strerror(errno));
344 net = libcfs_str2net(argv[1]);
345 if (net == LNET_NIDNET(LNET_NID_ANY)) {
346 fprintf(stderr, "Can't parse net %s\n", argv[1]);
356 jt_ptl_list_nids(int argc, char **argv)
358 struct libcfs_ioctl_data data;
359 int all = 0, return_nid = 0;
363 fprintf(stderr, "lctl %s: has been deprecated. Please use "
364 "'lnetctl net show'\n", argv[0]);
366 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
367 /* Hack to pass back value */
368 return_nid = (argc == 2) && (argv[1][0] == 1);
370 if ((argc > 2) && !(all || return_nid)) {
371 fprintf(stderr, "usage: %s [all]\n", argv[0]);
375 for (count = 0;; count++) {
376 LIBCFS_IOC_INIT (data);
377 data.ioc_count = count;
378 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
381 if ((count > 0) && (errno == ENOENT))
382 /* We found them all */
384 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
385 errno, strerror(errno));
389 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
390 printf("%s\n", libcfs_nid2str(data.ioc_nid));
392 *(__u64 *)(argv[1]) = data.ioc_nid;
402 jt_ptl_which_nid (int argc, char **argv)
404 struct libcfs_ioctl_data data;
407 lnet_nid_t best_nid = LNET_NID_ANY;
416 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
420 for (i = 1; i < argc; i++) {
422 nid = libcfs_str2nid(nidstr);
423 if (nid == LNET_NID_ANY) {
424 fprintf(stderr, "Can't parse NID %s\n", nidstr);
428 LIBCFS_IOC_INIT(data);
431 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
433 fprintf(stderr, "Can't get distance to %s: %s\n",
434 nidstr, strerror(errno));
438 dist = data.ioc_u32[0];
439 order = data.ioc_u32[1];
442 if (dist == -EHOSTUNREACH)
445 fprintf(stderr, "Unexpected distance to %s: %d\n",
450 if (best_nid == LNET_NID_ANY ||
452 (dist == best_dist && order < best_order)) {
459 if (best_nid == LNET_NID_ANY) {
460 fprintf(stderr, "No reachable NID\n");
464 printf("%s\n", libcfs_nid2str(best_nid));
469 jt_ptl_print_interfaces (int argc, char **argv)
471 struct libcfs_ioctl_data data;
472 char buffer[3][HOST_NAME_MAX + 1];
476 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
479 for (index = 0;;index++) {
480 LIBCFS_IOC_INIT(data);
481 data.ioc_net = g_net;
482 data.ioc_count = index;
484 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
488 printf ("%s: (%s/%s) npeer %d nroute %d\n",
489 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
490 sizeof(buffer[2]), 1),
491 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
492 sizeof(buffer[0]), 0),
493 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
494 sizeof(buffer[1]), 0),
495 data.ioc_u32[2], data.ioc_u32[3]);
499 if (errno == ENOENT) {
500 printf ("<no interfaces>\n");
502 fprintf(stderr, "Error getting interfaces: %s: "
512 jt_ptl_add_interface (int argc, char **argv)
514 struct libcfs_ioctl_data data;
517 __u32 netmask = 0xffffff00;
522 if (argc < 2 || argc > 3) {
523 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
527 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
530 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
531 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
536 count = strtol(argv[2], &end, 0);
537 if (count > 0 && count < 32 && *end == 0) {
539 for (i = count; i > 0; i--)
540 netmask = netmask|(1<<(32-i));
541 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
542 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
547 LIBCFS_IOC_INIT(data);
548 data.ioc_net = g_net;
549 data.ioc_u32[0] = ipaddr;
550 data.ioc_u32[1] = netmask;
552 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
554 fprintf (stderr, "failed to add interface: %s\n",
563 jt_ptl_del_interface (int argc, char **argv)
565 struct libcfs_ioctl_data data;
570 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
574 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
578 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
579 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
583 LIBCFS_IOC_INIT(data);
584 data.ioc_net = g_net;
585 data.ioc_u32[0] = ipaddr;
587 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
589 fprintf (stderr, "failed to delete interface: %s\n",
598 jt_ptl_print_peers (int argc, char **argv)
600 struct libcfs_ioctl_data data;
601 struct lnet_process_id id;
602 char buffer[2][HOST_NAME_MAX + 1];
606 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
610 for (index = 0;;index++) {
611 LIBCFS_IOC_INIT(data);
612 data.ioc_net = g_net;
613 data.ioc_count = index;
615 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
619 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
620 id.nid = data.ioc_nid;
621 id.pid = data.ioc_u32[4];
622 printf ("%-20s [%d]%s->%s:%d #%d\n",
624 data.ioc_count, /* persistence */
626 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
627 sizeof(buffer[0]), 1),
629 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
630 sizeof(buffer[1]), 1),
631 data.ioc_u32[1], /* peer port */
632 data.ioc_u32[3]); /* conn_count */
633 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
634 int disconn = data.ioc_flags >> 16;
640 state = data.ioc_flags & 0xffff ? "C" : "U";
642 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
643 libcfs_nid2str(data.ioc_nid), /* peer nid */
644 data.ioc_net, /* gemini device id */
645 state, /* peer is Connecting, Up, or Down */
646 data.ioc_count, /* peer refcount */
647 (uintmax_t)data.ioc_u64[0], /* peerstamp */
648 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
649 /* fmaq, nfma, nrdma */
650 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
652 printf ("%-20s [%d]\n",
653 libcfs_nid2str(data.ioc_nid), data.ioc_count);
658 if (errno == ENOENT) {
659 printf ("<no peers>\n");
661 fprintf(stderr, "Error getting peer list: %s: "
669 int jt_ptl_add_peer(int argc, char **argv)
671 struct libcfs_ioctl_data data;
677 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
681 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
686 nid = libcfs_str2nid(argv[1]);
687 if (nid == LNET_NID_ANY) {
688 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
692 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
693 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
697 if (lnet_parse_port (&port, argv[3]) != 0) {
698 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
702 LIBCFS_IOC_INIT(data);
703 data.ioc_net = g_net;
705 data.ioc_u32[0] = ip;
706 data.ioc_u32[1] = port;
708 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
710 fprintf (stderr, "failed to add peer: %s\n",
719 jt_ptl_del_peer (int argc, char **argv)
721 struct libcfs_ioctl_data data;
722 lnet_nid_t nid = LNET_NID_ANY;
723 lnet_pid_t pid = LNET_PID_ANY;
727 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
731 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
733 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
737 } else if (argc > 2) {
738 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
743 !libcfs_str2anynid(&nid, argv[1])) {
744 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
748 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
750 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
751 fprintf (stderr, "Can't parse ip addr: %s\n",
757 LIBCFS_IOC_INIT(data);
758 data.ioc_net = g_net;
760 data.ioc_u32[0] = ip;
761 data.ioc_u32[1] = pid;
763 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
765 fprintf (stderr, "failed to remove peer: %s\n",
774 jt_ptl_print_connections (int argc, char **argv)
776 struct libcfs_ioctl_data data;
777 struct lnet_process_id id;
778 char buffer[2][HOST_NAME_MAX + 1];
782 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
785 for (index = 0; ; index++) {
786 LIBCFS_IOC_INIT(data);
787 data.ioc_net = g_net;
788 data.ioc_count = index;
790 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
794 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
795 id.nid = data.ioc_nid;
796 id.pid = data.ioc_u32[6];
797 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
799 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
800 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
801 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
802 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
803 data.ioc_u32[4], /* scheduler */
805 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
806 sizeof(buffer[0]), 1),
808 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
809 sizeof(buffer[1]), 1),
810 data.ioc_u32[1], /* remote port */
811 data.ioc_count, /* tx buffer size */
812 data.ioc_u32[5], /* rx buffer size */
813 data.ioc_flags ? "nagle" : "nonagle");
814 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
815 printf("%s mtu %d\n",
816 libcfs_nid2str(data.ioc_nid),
817 data.ioc_u32[0]); /* path MTU */
818 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
819 printf("%-20s [%d]\n",
820 libcfs_nid2str(data.ioc_nid),
821 data.ioc_u32[0] /* device id */);
823 printf("%s\n", libcfs_nid2str(data.ioc_nid));
828 if (errno == ENOENT) {
829 printf ("<no connections>\n");
831 fprintf(stderr, "Error getting connection list: %s: "
839 int jt_ptl_disconnect(int argc, char **argv)
841 struct libcfs_ioctl_data data;
842 lnet_nid_t nid = LNET_NID_ANY;
847 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
851 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
855 !libcfs_str2anynid(&nid, argv[1])) {
856 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
860 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
862 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
863 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
867 LIBCFS_IOC_INIT(data);
868 data.ioc_net = g_net;
870 data.ioc_u32[0] = ipaddr;
872 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
874 fprintf(stderr, "failed to remove connection: %s\n",
882 int jt_ptl_push_connection (int argc, char **argv)
884 struct libcfs_ioctl_data data;
886 lnet_nid_t nid = LNET_NID_ANY;
889 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
893 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
897 !libcfs_str2anynid(&nid, argv[1])) {
898 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
902 LIBCFS_IOC_INIT(data);
903 data.ioc_net = g_net;
906 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
908 fprintf(stderr, "failed to push connection: %s\n",
916 int jt_ptl_ping(int argc, char **argv)
920 struct lnet_process_id id;
921 struct lnet_process_id ids[16];
922 int maxids = sizeof(ids)/sizeof(ids[0]);
923 struct libcfs_ioctl_data data;
927 fprintf(stderr, "lctl %s: has been deprecated. Please use "
928 "'lnetctl ping'\n", argv[0]);
931 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
935 sep = strchr(argv[1], '-');
937 rc = lnet_parse_nid(argv[1], &id);
943 if (argv[1][0] == 'u' ||
945 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
947 id.pid = strtoul(argv[1], &end, 0);
949 if (end != sep) { /* assuming '-' is part of hostname */
950 rc = lnet_parse_nid(argv[1], &id);
954 id.nid = libcfs_str2nid(sep + 1);
956 if (id.nid == LNET_NID_ANY) {
958 "Can't parse process id \"%s\"\n",
966 timeout = 1000 * atol(argv[2]);
967 if (timeout > 120 * 1000) {
968 fprintf(stderr, "Timeout %s is to large\n",
973 timeout = 1000; /* default 1 second timeout */
975 LIBCFS_IOC_INIT (data);
976 data.ioc_nid = id.nid;
977 data.ioc_u32[0] = id.pid;
978 data.ioc_u32[1] = timeout;
979 data.ioc_plen1 = sizeof(ids);
980 data.ioc_pbuf1 = (char *)ids;
982 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
984 fprintf(stderr, "failed to ping %s: %s\n",
985 id.pid == LNET_PID_ANY ?
986 libcfs_nid2str(id.nid) : libcfs_id2str(id),
991 for (i = 0; i < data.ioc_count && i < maxids; i++)
992 printf("%s\n", libcfs_id2str(ids[i]));
994 if (data.ioc_count > maxids)
995 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
1000 int jt_ptl_mynid(int argc, char **argv)
1002 struct libcfs_ioctl_data data;
1007 fprintf(stderr, "usage: %s NID\n", argv[0]);
1011 nid = libcfs_str2nid(argv[1]);
1012 if (nid == LNET_NID_ANY) {
1013 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1017 LIBCFS_IOC_INIT(data);
1018 data.ioc_net = LNET_NIDNET(nid);
1021 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1023 fprintf(stderr, "setting my NID failed: %s\n",
1026 printf("registered my nid %s\n", libcfs_nid2str(nid));
1032 jt_ptl_fail_nid (int argc, char **argv)
1037 struct libcfs_ioctl_data data;
1039 if (argc < 2 || argc > 3)
1041 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1045 if (!libcfs_str2anynid(&nid, argv[1]))
1047 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1052 threshold = LNET_MD_THRESH_INF;
1053 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1054 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1058 LIBCFS_IOC_INIT (data);
1060 data.ioc_count = threshold;
1062 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1064 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1067 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1073 jt_ptl_add_route (int argc, char **argv)
1075 struct lnet_ioctl_config_data data;
1076 lnet_nid_t gateway_nid;
1077 __u32 hops = LNET_UNDEFINED_HOPS;
1078 unsigned int priority = 0;
1082 if (argc < 2 || argc > 4) {
1083 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1088 if (g_net_is_set(argv[0]) == 0)
1091 gateway_nid = libcfs_str2nid(argv[1]);
1092 if (gateway_nid == LNET_NID_ANY) {
1093 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1098 hops = strtol(argv[2], &end, 0);
1099 if (hops == 0 || hops >= 256 ||
1100 (end != NULL && *end != 0)) {
1101 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1106 priority = strtoul(argv[3], &end, 0);
1107 if (end != NULL && *end != 0) {
1109 "Can't parse priority \"%s\"\n",
1116 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1117 data.cfg_net = g_net;
1118 data.cfg_config_u.cfg_route.rtr_hop = hops;
1119 data.cfg_nid = gateway_nid;
1120 data.cfg_config_u.cfg_route.rtr_priority = priority;
1122 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1124 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1133 jt_ptl_del_route (int argc, char **argv)
1135 struct lnet_ioctl_config_data data;
1140 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1144 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1145 fprintf(stderr, "Can't parse gateway NID "
1146 "\"%s\"\n", argv[1]);
1150 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1151 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1154 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1156 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1157 libcfs_nid2str(nid), strerror(errno));
1165 jt_ptl_notify_router (int argc, char **argv)
1167 struct libcfs_ioctl_data data;
1176 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1181 nid = libcfs_str2nid(argv[1]);
1182 if (nid == LNET_NID_ANY) {
1183 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1187 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1188 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1192 gettimeofday(&now, NULL);
1196 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1197 fprintf(stderr, "Can't parse time %s\n"
1198 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1199 "or an absolute unix time in seconds\n", argv[3]);
1201 } else if (when > now.tv_sec) {
1202 fprintf (stderr, "%s specifies a time in the future\n",
1207 LIBCFS_IOC_INIT(data);
1209 data.ioc_flags = enable;
1210 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1211 data.ioc_u64[0] = (__u64)when;
1213 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1215 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1216 libcfs_nid2str(nid), strerror (errno));
1224 jt_ptl_print_routes (int argc, char **argv)
1226 struct lnet_ioctl_config_data data;
1235 fprintf(stderr, "lctl %s: has been deprecated. Please use "
1236 "'lnetctl route show'\n", argv[0]);
1238 for (index = 0; ; index++) {
1239 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1240 data.cfg_count = index;
1242 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1247 hops = data.cfg_config_u.cfg_route.rtr_hop;
1249 alive = data.cfg_config_u.cfg_route.rtr_flags;
1250 pri = data.cfg_config_u.cfg_route.rtr_priority;
1252 printf("net %18s hops %u gw %32s %s pri %u\n",
1253 libcfs_net2str(net), hops,
1254 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1257 if (errno != ENOENT)
1258 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1265 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1271 /* NB: can't support range ipaddress except * and *@net */
1272 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1273 net = libcfs_str2net(str + 2);
1274 if (net == LNET_NIDNET(LNET_NID_ANY))
1277 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1279 rc = libcfs_str2anynid(&nid, str);
1287 fprintf(stderr, "Invalid NID : %s\n", str);
1292 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1294 if (!strcasecmp(msg_str, "put")) {
1295 *mask_p |= LNET_PUT_BIT;
1298 } else if (!strcasecmp(msg_str, "ack")) {
1299 *mask_p |= LNET_ACK_BIT;
1302 } else if (!strcasecmp(msg_str, "get")) {
1303 *mask_p |= LNET_GET_BIT;
1306 } else if (!strcasecmp(msg_str, "reply")) {
1307 *mask_p |= LNET_REPLY_BIT;
1311 fprintf(stderr, "unknown message type %s\n", msg_str);
1316 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1318 unsigned long rc = strtoul(optarg, NULL, 0);
1321 fprintf(stderr, "invalid portal: %lu\n", rc);
1325 *mask_p |= (1ULL << rc);
1330 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1332 struct libcfs_ioctl_data data = { { 0 } };
1333 struct lnet_fault_attr attr;
1337 static const struct option opts[] = {
1338 { .name = "source", .has_arg = required_argument, .val = 's' },
1339 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1340 { .name = "rate", .has_arg = required_argument, .val = 'r' },
1341 { .name = "interval", .has_arg = required_argument, .val = 'i' },
1342 { .name = "latency", .has_arg = required_argument, .val = 'l' },
1343 { .name = "portal", .has_arg = required_argument, .val = 'p' },
1344 { .name = "message", .has_arg = required_argument, .val = 'm' },
1348 fprintf(stderr, "Failed, please provide source, destination "
1349 "and rate of rule\n");
1353 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1354 memset(&attr, 0, sizeof(attr));
1356 char c = getopt_long(argc, argv, optstr, opts, NULL);
1362 case 's': /* source NID/NET */
1363 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1368 case 'd': /* dest NID/NET */
1369 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1374 case 'r': /* drop rate */
1375 if (opc == LNET_CTL_DROP_ADD)
1376 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1378 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1381 case 'i': /* time interval (# seconds) for message drop */
1382 if (opc == LNET_CTL_DROP_ADD)
1383 attr.u.drop.da_interval = strtoul(optarg,
1386 attr.u.delay.la_interval = strtoul(optarg,
1390 case 'l': /* seconds to wait before activating rule */
1391 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1394 case 'p': /* portal to filter */
1395 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1400 case 'm': /* message types to filter */
1401 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1407 fprintf(stderr, "error: %s: option '%s' "
1408 "unrecognized\n", argv[0], argv[optind - 1]);
1414 if (opc == LNET_CTL_DROP_ADD) {
1415 /* NB: drop rate and interval are exclusive to each other */
1416 if (!((attr.u.drop.da_rate == 0) ^
1417 (attr.u.drop.da_interval == 0))) {
1419 "please provide either drop rate or interval "
1420 "but not both at the same time.\n");
1423 } else if (opc == LNET_CTL_DELAY_ADD) {
1424 if (!((attr.u.delay.la_rate == 0) ^
1425 (attr.u.delay.la_interval == 0))) {
1427 "please provide either delay rate or interval "
1428 "but not both at the same time.\n");
1432 if (attr.u.delay.la_latency == 0) {
1433 fprintf(stderr, "latency cannot be zero\n");
1438 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1439 fprintf(stderr, "Please provide both source and destination "
1440 "of %s rule\n", name);
1444 data.ioc_flags = opc;
1445 data.ioc_inllen1 = sizeof(attr);
1446 data.ioc_inlbuf1 = (char *)&attr;
1447 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1448 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1452 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1454 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1455 name, libcfs_nid2str(attr.fa_src),
1456 libcfs_nid2str(attr.fa_dst), strerror(errno));
1460 printf("Added %s rule %s->%s (1/%d)\n",
1461 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1462 opc == LNET_CTL_DROP_ADD ?
1463 attr.u.drop.da_rate : attr.u.delay.la_rate);
1472 jt_ptl_drop_add(int argc, char **argv)
1474 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1478 jt_ptl_delay_add(int argc, char **argv)
1480 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1484 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1486 struct libcfs_ioctl_data data = { { 0 } };
1487 struct lnet_fault_attr attr;
1491 static const struct option opts[] = {
1492 { .name = "source", .has_arg = required_argument, .val = 's' },
1493 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1494 { .name = "all", .has_arg = no_argument, .val = 'a' },
1498 fprintf(stderr, "Failed, please provide source and "
1499 "destination of rule\n");
1503 memset(&attr, 0, sizeof(attr));
1505 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1512 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1517 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1522 attr.fa_src = attr.fa_dst = 0;
1526 fprintf(stderr, "error: %s: option '%s' "
1527 "unrecognized\n", argv[0], argv[optind - 1]);
1533 data.ioc_flags = opc;
1534 data.ioc_inllen1 = sizeof(attr);
1535 data.ioc_inlbuf1 = (char *)&attr;
1536 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1537 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1541 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1543 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1544 all ? "all" : libcfs_nid2str(attr.fa_src),
1545 all ? "all" : libcfs_nid2str(attr.fa_dst),
1550 libcfs_ioctl_unpack(&data, ioc_buf);
1551 printf("Removed %d %s rules\n", data.ioc_count, name);
1560 jt_ptl_drop_del(int argc, char **argv)
1562 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1566 jt_ptl_delay_del(int argc, char **argv)
1568 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1572 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1574 struct libcfs_ioctl_data data = { { 0 } };
1577 LIBCFS_IOC_INIT(data);
1578 data.ioc_flags = opc;
1580 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1582 fprintf(stderr, "failed to reset %s stats: %s\n",
1583 name, strerror(errno));
1590 jt_ptl_drop_reset(int argc, char **argv)
1592 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1596 jt_ptl_delay_reset(int argc, char **argv)
1598 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1603 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1605 struct libcfs_ioctl_data data = { { 0 } };
1606 struct lnet_fault_attr attr;
1607 struct lnet_fault_stat stat;
1610 printf("LNet %s rules:\n", name);
1611 for (pos = 0;; pos++) {
1614 memset(&attr, 0, sizeof(attr));
1615 memset(&stat, 0, sizeof(stat));
1617 data.ioc_count = pos;
1618 data.ioc_flags = opc;
1619 data.ioc_inllen1 = sizeof(attr);
1620 data.ioc_inlbuf1 = (char *)&attr;
1621 data.ioc_inllen2 = sizeof(stat);
1622 data.ioc_inlbuf2 = (char *)&stat;
1623 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1624 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1628 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1632 libcfs_ioctl_unpack(&data, ioc_buf);
1634 if (opc == LNET_CTL_DROP_LIST) {
1635 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1636 "%ju/%ju, PUT %ju, ACK %ju, GET "
1638 libcfs_nid2str(attr.fa_src),
1639 libcfs_nid2str(attr.fa_dst),
1640 attr.u.drop.da_rate, attr.u.drop.da_interval,
1641 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1642 (uintmax_t)stat.u.drop.ds_dropped,
1643 (uintmax_t)stat.fs_count,
1644 (uintmax_t)stat.fs_put,
1645 (uintmax_t)stat.fs_ack,
1646 (uintmax_t)stat.fs_get,
1647 (uintmax_t)stat.fs_reply);
1649 } else if (opc == LNET_CTL_DELAY_LIST) {
1650 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1651 ", msg %x, %ju/%ju, PUT %ju"
1652 ", ACK %ju, GET %ju, REP %ju\n",
1653 libcfs_nid2str(attr.fa_src),
1654 libcfs_nid2str(attr.fa_dst),
1655 attr.u.delay.la_rate, attr.u.delay.la_interval,
1656 attr.u.delay.la_latency,
1657 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1658 (uintmax_t)stat.u.delay.ls_delayed,
1659 (uintmax_t)stat.fs_count,
1660 (uintmax_t)stat.fs_put,
1661 (uintmax_t)stat.fs_ack,
1662 (uintmax_t)stat.fs_get,
1663 (uintmax_t)stat.fs_reply);
1666 printf("found total %d\n", pos);
1672 jt_ptl_drop_list(int argc, char **argv)
1674 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1678 jt_ptl_delay_list(int argc, char **argv)
1680 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1684 get_cycles_per_usec ()
1686 FILE *f = fopen ("/proc/cpuinfo", "r");
1691 while (fgets (line, sizeof (line), f) != NULL)
1692 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1699 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1703 int jt_ptl_testprotocompat(int argc, char **argv)
1705 struct libcfs_ioctl_data data;
1711 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1715 flags = strtol(argv[1], &end, 0);
1716 if (flags < 0 || *end != 0) {
1717 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1721 LIBCFS_IOC_INIT(data);
1722 data.ioc_flags = flags;
1723 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1726 fprintf(stderr, "test proto compat %x failed: %s\n",
1727 flags, strerror(errno));
1731 printf("test proto compat %x OK\n", flags);