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/libcfs_debug.h>
41 #include <lnet/lnetctl.h>
42 #include <lnet/socklnd.h>
43 #include <lnet/lnet.h>
45 unsigned int libcfs_debug;
46 unsigned int libcfs_printk = D_CANTMASK;
48 static bool g_net_interactive;
49 static bool g_net_set;
52 #define IOC_BUF_SIZE 8192
53 static char local_buf[IOC_BUF_SIZE];
54 static char *ioc_buf = local_buf;
56 /* Convert a string boolean to an int; "enable" -> 1 */
58 lnet_parse_bool (int *b, char *str)
60 if (!strcasecmp (str, "no") ||
61 !strcasecmp (str, "n") ||
62 !strcasecmp (str, "off") ||
63 !strcasecmp (str, "down") ||
64 !strcasecmp (str, "disable"))
70 if (!strcasecmp (str, "yes") ||
71 !strcasecmp (str, "y") ||
72 !strcasecmp (str, "on") ||
73 !strcasecmp (str, "up") ||
74 !strcasecmp (str, "enable"))
84 lnet_parse_port (int *port, char *str)
88 *port = strtol (str, &end, 0);
90 if (*end == 0 && /* parsed whole string */
91 *port > 0 && *port < 65536) /* minimal sanity check */
97 #ifdef HAVE_GETHOSTBYNAME
98 static struct hostent *
99 ptl_gethostbyname(char * hname) {
101 he = gethostbyname(hname);
106 fprintf(stderr, "Unable to resolve hostname: %s\n",
110 fprintf(stderr, "gethostbyname error for %s: %s\n",
111 hname, strerror(h_errno));
121 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
128 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
129 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
130 (c & ~0xff) == 0 && (d & ~0xff) == 0)
132 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
140 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
142 #ifdef HAVE_GETHOSTBYNAME
146 if (!strcmp (str, "_all_")) {
151 if (lnet_parse_ipquad(ipaddrp, str) == 0)
154 #ifdef HAVE_GETHOSTBYNAME
155 if ((('a' <= str[0] && str[0] <= 'z') ||
156 ('A' <= str[0] && str[0] <= 'Z')) &&
157 (he = ptl_gethostbyname (str)) != NULL) {
158 __u32 addr = *(__u32 *)he->h_addr;
160 *ipaddrp = ntohl(addr); /* HOST byte order */
169 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
171 #ifdef HAVE_GETHOSTBYNAME
176 net_ip = htonl (ipaddr);
177 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
179 strlcpy(str, he->h_name, strsize);
185 sprintf (str, "%d.%d.%d.%d",
186 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
187 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
192 lnet_parse_time (time_t *t, char *str)
198 *t = strtol (str, &end, 0);
199 if (*end == 0) /* parsed whole string */
202 memset (&tm, 0, sizeof (tm));
203 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
204 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
205 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
209 tm.tm_mon--; /* convert to 0 == Jan */
210 tm.tm_year -= 1900; /* y2k quirk */
211 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
214 if (*t == (time_t)-1)
221 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
223 id_ptr->pid = LNET_PID_ANY;
224 id_ptr->nid = libcfs_str2nid(nid_str);
225 if (id_ptr->nid == LNET_NID_ANY) {
226 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
233 static int g_net_is_set(char *cmd)
241 if (g_net_interactive)
247 "You must run '%s <network>' command before '%s'\n",
255 static int g_net_is_compatible(char *cmd, ...)
260 if (!g_net_is_set(cmd))
266 nal = va_arg(ap, int);
267 if (nal == LNET_NETTYP(g_net)) {
277 "Command %s not compatible with %s NAL\n",
279 libcfs_lnd2str(LNET_NETTYP(g_net)));
284 int ptl_initialize(int argc, char **argv)
287 g_net_interactive = true;
289 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
290 LNET_DEV_MAJOR, LNET_DEV_MINOR);
295 int jt_ptl_network(int argc, char **argv)
297 struct libcfs_ioctl_data data;
298 __u32 net = LNET_NIDNET(LNET_NID_ANY);
302 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
306 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
307 LIBCFS_IOC_INIT(data);
308 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
311 printf("LNET ready to unload\n");
316 fprintf(stderr, "LNET busy\n");
318 fprintf(stderr, "LNET unconfigure error %d: %s\n",
319 errno, strerror(errno));
321 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
322 LIBCFS_IOC_INIT(data);
323 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
326 printf("LNET configured\n");
330 fprintf(stderr, "LNET configure error %d: %s\n",
331 errno, strerror(errno));
335 net = libcfs_str2net(argv[1]);
336 if (net == LNET_NIDNET(LNET_NID_ANY)) {
337 fprintf(stderr, "Can't parse net %s\n", argv[1]);
347 jt_ptl_list_nids(int argc, char **argv)
349 struct libcfs_ioctl_data data;
350 int all = 0, return_nid = 0;
354 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
355 /* Hack to pass back value */
356 return_nid = (argc == 2) && (argv[1][0] == 1);
358 if ((argc > 2) && !(all || return_nid)) {
359 fprintf(stderr, "usage: %s [all]\n", argv[0]);
363 for (count = 0;; count++) {
364 LIBCFS_IOC_INIT (data);
365 data.ioc_count = count;
366 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
369 if ((count > 0) && (errno == ENOENT))
370 /* We found them all */
372 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
373 errno, strerror(errno));
377 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
378 printf("%s\n", libcfs_nid2str(data.ioc_nid));
380 *(__u64 *)(argv[1]) = data.ioc_nid;
390 jt_ptl_which_nid (int argc, char **argv)
392 struct libcfs_ioctl_data data;
395 lnet_nid_t best_nid = LNET_NID_ANY;
404 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
408 for (i = 1; i < argc; i++) {
410 nid = libcfs_str2nid(nidstr);
411 if (nid == LNET_NID_ANY) {
412 fprintf(stderr, "Can't parse NID %s\n", nidstr);
416 LIBCFS_IOC_INIT(data);
419 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
421 fprintf(stderr, "Can't get distance to %s: %s\n",
422 nidstr, strerror(errno));
426 dist = data.ioc_u32[0];
427 order = data.ioc_u32[1];
430 if (dist == -EHOSTUNREACH)
433 fprintf(stderr, "Unexpected distance to %s: %d\n",
438 if (best_nid == LNET_NID_ANY ||
440 (dist == best_dist && order < best_order)) {
447 if (best_nid == LNET_NID_ANY) {
448 fprintf(stderr, "No reachable NID\n");
452 printf("%s\n", libcfs_nid2str(best_nid));
457 jt_ptl_print_interfaces (int argc, char **argv)
459 struct libcfs_ioctl_data data;
460 char buffer[3][HOST_NAME_MAX + 1];
464 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
467 for (index = 0;;index++) {
468 LIBCFS_IOC_INIT(data);
469 data.ioc_net = g_net;
470 data.ioc_count = index;
472 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
476 printf ("%s: (%s/%s) npeer %d nroute %d\n",
477 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
478 sizeof(buffer[2]), 1),
479 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
480 sizeof(buffer[0]), 0),
481 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
482 sizeof(buffer[1]), 0),
483 data.ioc_u32[2], data.ioc_u32[3]);
487 if (errno == ENOENT) {
488 printf ("<no interfaces>\n");
490 fprintf(stderr, "Error getting interfaces: %s: "
500 jt_ptl_add_interface (int argc, char **argv)
502 struct libcfs_ioctl_data data;
505 __u32 netmask = 0xffffff00;
510 if (argc < 2 || argc > 3) {
511 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
515 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
518 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
519 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
524 count = strtol(argv[2], &end, 0);
525 if (count > 0 && count < 32 && *end == 0) {
527 for (i = count; i > 0; i--)
528 netmask = netmask|(1<<(32-i));
529 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
530 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
535 LIBCFS_IOC_INIT(data);
536 data.ioc_net = g_net;
537 data.ioc_u32[0] = ipaddr;
538 data.ioc_u32[1] = netmask;
540 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
542 fprintf (stderr, "failed to add interface: %s\n",
551 jt_ptl_del_interface (int argc, char **argv)
553 struct libcfs_ioctl_data data;
558 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
562 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
566 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
567 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
571 LIBCFS_IOC_INIT(data);
572 data.ioc_net = g_net;
573 data.ioc_u32[0] = ipaddr;
575 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
577 fprintf (stderr, "failed to delete interface: %s\n",
586 jt_ptl_print_peers (int argc, char **argv)
588 struct libcfs_ioctl_data data;
589 lnet_process_id_t id;
590 char buffer[2][HOST_NAME_MAX + 1];
594 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
598 for (index = 0;;index++) {
599 LIBCFS_IOC_INIT(data);
600 data.ioc_net = g_net;
601 data.ioc_count = index;
603 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
607 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
608 id.nid = data.ioc_nid;
609 id.pid = data.ioc_u32[4];
610 printf ("%-20s [%d]%s->%s:%d #%d\n",
612 data.ioc_count, /* persistence */
614 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
615 sizeof(buffer[0]), 1),
617 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
618 sizeof(buffer[1]), 1),
619 data.ioc_u32[1], /* peer port */
620 data.ioc_u32[3]); /* conn_count */
621 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
622 int disconn = data.ioc_flags >> 16;
628 state = data.ioc_flags & 0xffff ? "C" : "U";
630 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
631 libcfs_nid2str(data.ioc_nid), /* peer nid */
632 data.ioc_net, /* gemini device id */
633 state, /* peer is Connecting, Up, or Down */
634 data.ioc_count, /* peer refcount */
635 (uintmax_t)data.ioc_u64[0], /* peerstamp */
636 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
637 /* fmaq, nfma, nrdma */
638 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
640 printf ("%-20s [%d]\n",
641 libcfs_nid2str(data.ioc_nid), data.ioc_count);
646 if (errno == ENOENT) {
647 printf ("<no peers>\n");
649 fprintf(stderr, "Error getting peer list: %s: "
657 int jt_ptl_add_peer(int argc, char **argv)
659 struct libcfs_ioctl_data data;
665 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
669 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
674 nid = libcfs_str2nid(argv[1]);
675 if (nid == LNET_NID_ANY) {
676 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
680 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
681 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
685 if (lnet_parse_port (&port, argv[3]) != 0) {
686 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
690 LIBCFS_IOC_INIT(data);
691 data.ioc_net = g_net;
693 data.ioc_u32[0] = ip;
694 data.ioc_u32[1] = port;
696 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
698 fprintf (stderr, "failed to add peer: %s\n",
707 jt_ptl_del_peer (int argc, char **argv)
709 struct libcfs_ioctl_data data;
710 lnet_nid_t nid = LNET_NID_ANY;
711 lnet_pid_t pid = LNET_PID_ANY;
715 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
719 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
721 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
725 } else if (argc > 2) {
726 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
731 !libcfs_str2anynid(&nid, argv[1])) {
732 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
736 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
738 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
739 fprintf (stderr, "Can't parse ip addr: %s\n",
745 LIBCFS_IOC_INIT(data);
746 data.ioc_net = g_net;
748 data.ioc_u32[0] = ip;
749 data.ioc_u32[1] = pid;
751 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
753 fprintf (stderr, "failed to remove peer: %s\n",
762 jt_ptl_print_connections (int argc, char **argv)
764 struct libcfs_ioctl_data data;
765 lnet_process_id_t id;
766 char buffer[2][HOST_NAME_MAX + 1];
770 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
773 for (index = 0; ; index++) {
774 LIBCFS_IOC_INIT(data);
775 data.ioc_net = g_net;
776 data.ioc_count = index;
778 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
782 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
783 id.nid = data.ioc_nid;
784 id.pid = data.ioc_u32[6];
785 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
787 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
788 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
789 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
790 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
791 data.ioc_u32[4], /* scheduler */
793 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
794 sizeof(buffer[0]), 1),
796 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
797 sizeof(buffer[1]), 1),
798 data.ioc_u32[1], /* remote port */
799 data.ioc_count, /* tx buffer size */
800 data.ioc_u32[5], /* rx buffer size */
801 data.ioc_flags ? "nagle" : "nonagle");
802 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
803 printf ("%s mtu %d\n",
804 libcfs_nid2str(data.ioc_nid),
805 data.ioc_u32[0]); /* path MTU */
806 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
807 printf ("%-20s [%d]\n",
808 libcfs_nid2str(data.ioc_nid),
809 data.ioc_u32[0] /* device id */);
811 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
816 if (errno == ENOENT) {
817 printf ("<no connections>\n");
819 fprintf(stderr, "Error getting connection list: %s: "
827 int jt_ptl_disconnect(int argc, char **argv)
829 struct libcfs_ioctl_data data;
830 lnet_nid_t nid = LNET_NID_ANY;
835 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
839 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
843 !libcfs_str2anynid(&nid, argv[1])) {
844 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
848 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
850 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
851 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
855 LIBCFS_IOC_INIT(data);
856 data.ioc_net = g_net;
858 data.ioc_u32[0] = ipaddr;
860 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
862 fprintf(stderr, "failed to remove connection: %s\n",
870 int jt_ptl_push_connection (int argc, char **argv)
872 struct libcfs_ioctl_data data;
874 lnet_nid_t nid = LNET_NID_ANY;
877 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
881 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
885 !libcfs_str2anynid(&nid, argv[1])) {
886 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
890 LIBCFS_IOC_INIT(data);
891 data.ioc_net = g_net;
894 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
896 fprintf(stderr, "failed to push connection: %s\n",
904 int jt_ptl_ping(int argc, char **argv)
908 lnet_process_id_t id;
909 lnet_process_id_t ids[16];
910 int maxids = sizeof(ids)/sizeof(ids[0]);
911 struct libcfs_ioctl_data data;
916 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
920 sep = strchr(argv[1], '-');
922 rc = lnet_parse_nid(argv[1], &id);
928 if (argv[1][0] == 'u' ||
930 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
932 id.pid = strtoul(argv[1], &end, 0);
934 if (end != sep) { /* assuming '-' is part of hostname */
935 rc = lnet_parse_nid(argv[1], &id);
939 id.nid = libcfs_str2nid(sep + 1);
941 if (id.nid == LNET_NID_ANY) {
943 "Can't parse process id \"%s\"\n",
951 timeout = 1000 * atol(argv[2]);
952 if (timeout > 120 * 1000) {
953 fprintf(stderr, "Timeout %s is to large\n",
958 timeout = 1000; /* default 1 second timeout */
960 LIBCFS_IOC_INIT (data);
961 data.ioc_nid = id.nid;
962 data.ioc_u32[0] = id.pid;
963 data.ioc_u32[1] = timeout;
964 data.ioc_plen1 = sizeof(ids);
965 data.ioc_pbuf1 = (char *)ids;
967 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
969 fprintf(stderr, "failed to ping %s: %s\n",
970 id.pid == LNET_PID_ANY ?
971 libcfs_nid2str(id.nid) : libcfs_id2str(id),
976 for (i = 0; i < data.ioc_count && i < maxids; i++)
977 printf("%s\n", libcfs_id2str(ids[i]));
979 if (data.ioc_count > maxids)
980 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
985 int jt_ptl_mynid(int argc, char **argv)
987 struct libcfs_ioctl_data data;
992 fprintf(stderr, "usage: %s NID\n", argv[0]);
996 nid = libcfs_str2nid(argv[1]);
997 if (nid == LNET_NID_ANY) {
998 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1002 LIBCFS_IOC_INIT(data);
1003 data.ioc_net = LNET_NIDNET(nid);
1006 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1008 fprintf(stderr, "setting my NID failed: %s\n",
1011 printf("registered my nid %s\n", libcfs_nid2str(nid));
1017 jt_ptl_fail_nid (int argc, char **argv)
1022 struct libcfs_ioctl_data data;
1024 if (argc < 2 || argc > 3)
1026 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1030 if (!libcfs_str2anynid(&nid, argv[1]))
1032 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1037 threshold = LNET_MD_THRESH_INF;
1038 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1039 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1043 LIBCFS_IOC_INIT (data);
1045 data.ioc_count = threshold;
1047 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1049 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1052 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1058 jt_ptl_add_route (int argc, char **argv)
1060 struct lnet_ioctl_config_data data;
1061 lnet_nid_t gateway_nid;
1062 __u32 hops = LNET_UNDEFINED_HOPS;
1063 unsigned int priority = 0;
1067 if (argc < 2 || argc > 4) {
1068 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1073 if (g_net_is_set(argv[0]) == 0)
1076 gateway_nid = libcfs_str2nid(argv[1]);
1077 if (gateway_nid == LNET_NID_ANY) {
1078 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1083 hops = strtol(argv[2], &end, 0);
1084 if (hops == 0 || hops >= 256 ||
1085 (end != NULL && *end != 0)) {
1086 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1091 priority = strtoul(argv[3], &end, 0);
1092 if (end != NULL && *end != 0) {
1094 "Can't parse priority \"%s\"\n",
1101 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1102 data.cfg_net = g_net;
1103 data.cfg_config_u.cfg_route.rtr_hop = hops;
1104 data.cfg_nid = gateway_nid;
1105 data.cfg_config_u.cfg_route.rtr_priority = priority;
1107 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1109 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1118 jt_ptl_del_route (int argc, char **argv)
1120 struct lnet_ioctl_config_data data;
1125 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1129 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1130 fprintf(stderr, "Can't parse gateway NID "
1131 "\"%s\"\n", argv[1]);
1135 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1136 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1139 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1141 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1142 libcfs_nid2str(nid), strerror(errno));
1150 jt_ptl_notify_router (int argc, char **argv)
1152 struct libcfs_ioctl_data data;
1161 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1166 nid = libcfs_str2nid(argv[1]);
1167 if (nid == LNET_NID_ANY) {
1168 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1172 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1173 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1177 gettimeofday(&now, NULL);
1181 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1182 fprintf(stderr, "Can't parse time %s\n"
1183 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1184 "or an absolute unix time in seconds\n", argv[3]);
1186 } else if (when > now.tv_sec) {
1187 fprintf (stderr, "%s specifies a time in the future\n",
1192 LIBCFS_IOC_INIT(data);
1194 data.ioc_flags = enable;
1195 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1196 data.ioc_u64[0] = (__u64)when;
1198 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1200 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1201 libcfs_nid2str(nid), strerror (errno));
1209 jt_ptl_print_routes (int argc, char **argv)
1211 struct lnet_ioctl_config_data data;
1220 for (index = 0; ; index++) {
1221 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1222 data.cfg_count = index;
1224 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1229 hops = data.cfg_config_u.cfg_route.rtr_hop;
1231 alive = data.cfg_config_u.cfg_route.rtr_flags;
1232 pri = data.cfg_config_u.cfg_route.rtr_priority;
1234 printf("net %18s hops %u gw %32s %s pri %u\n",
1235 libcfs_net2str(net), hops,
1236 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1239 if (errno != ENOENT)
1240 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1247 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1253 /* NB: can't support range ipaddress except * and *@net */
1254 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1255 net = libcfs_str2net(str + 2);
1256 if (net == LNET_NIDNET(LNET_NID_ANY))
1259 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1261 rc = libcfs_str2anynid(&nid, str);
1269 fprintf(stderr, "Invalid NID : %s\n", str);
1274 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1276 if (!strcasecmp(msg_str, "put")) {
1277 *mask_p |= LNET_PUT_BIT;
1280 } else if (!strcasecmp(msg_str, "ack")) {
1281 *mask_p |= LNET_ACK_BIT;
1284 } else if (!strcasecmp(msg_str, "get")) {
1285 *mask_p |= LNET_GET_BIT;
1288 } else if (!strcasecmp(msg_str, "reply")) {
1289 *mask_p |= LNET_REPLY_BIT;
1293 fprintf(stderr, "unknown message type %s\n", msg_str);
1298 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1300 unsigned long rc = strtoul(optarg, NULL, 0);
1303 fprintf(stderr, "invalid portal: %lu\n", rc);
1307 *mask_p |= (1ULL << rc);
1312 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1314 struct libcfs_ioctl_data data = { { 0 } };
1315 struct lnet_fault_attr attr;
1319 static const struct option opts[] = {
1320 { .name = "source", .has_arg = required_argument, .val = 's' },
1321 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1322 { .name = "rate", .has_arg = required_argument, .val = 'r' },
1323 { .name = "interval", .has_arg = required_argument, .val = 'i' },
1324 { .name = "latency", .has_arg = required_argument, .val = 'l' },
1325 { .name = "portal", .has_arg = required_argument, .val = 'p' },
1326 { .name = "message", .has_arg = required_argument, .val = 'm' },
1330 fprintf(stderr, "Failed, please provide source, destination "
1331 "and rate of rule\n");
1335 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1336 memset(&attr, 0, sizeof(attr));
1338 char c = getopt_long(argc, argv, optstr, opts, NULL);
1344 case 's': /* source NID/NET */
1345 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1350 case 'd': /* dest NID/NET */
1351 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1356 case 'r': /* drop rate */
1357 if (opc == LNET_CTL_DROP_ADD)
1358 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1360 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1363 case 'i': /* time interval (# seconds) for message drop */
1364 if (opc == LNET_CTL_DROP_ADD)
1365 attr.u.drop.da_interval = strtoul(optarg,
1368 attr.u.delay.la_interval = strtoul(optarg,
1372 case 'l': /* seconds to wait before activating rule */
1373 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1376 case 'p': /* portal to filter */
1377 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1382 case 'm': /* message types to filter */
1383 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1389 fprintf(stderr, "error: %s: option '%s' "
1390 "unrecognized\n", argv[0], argv[optind - 1]);
1396 if (opc == LNET_CTL_DROP_ADD) {
1397 /* NB: drop rate and interval are exclusive to each other */
1398 if (!((attr.u.drop.da_rate == 0) ^
1399 (attr.u.drop.da_interval == 0))) {
1401 "please provide either drop rate or interval "
1402 "but not both at the same time.\n");
1405 } else if (opc == LNET_CTL_DELAY_ADD) {
1406 if (!((attr.u.delay.la_rate == 0) ^
1407 (attr.u.delay.la_interval == 0))) {
1409 "please provide either delay rate or interval "
1410 "but not both at the same time.\n");
1414 if (attr.u.delay.la_latency == 0) {
1415 fprintf(stderr, "latency cannot be zero\n");
1420 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1421 fprintf(stderr, "Please provide both source and destination "
1422 "of %s rule\n", name);
1426 data.ioc_flags = opc;
1427 data.ioc_inllen1 = sizeof(attr);
1428 data.ioc_inlbuf1 = (char *)&attr;
1429 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1430 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1434 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1436 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1437 name, libcfs_nid2str(attr.fa_src),
1438 libcfs_nid2str(attr.fa_dst), strerror(errno));
1442 printf("Added %s rule %s->%s (1/%d)\n",
1443 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1444 opc == LNET_CTL_DROP_ADD ?
1445 attr.u.drop.da_rate : attr.u.delay.la_rate);
1454 jt_ptl_drop_add(int argc, char **argv)
1456 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1460 jt_ptl_delay_add(int argc, char **argv)
1462 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1466 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1468 struct libcfs_ioctl_data data = { { 0 } };
1469 struct lnet_fault_attr attr;
1473 static const struct option opts[] = {
1474 { .name = "source", .has_arg = required_argument, .val = 's' },
1475 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1476 { .name = "all", .has_arg = no_argument, .val = 'a' },
1480 fprintf(stderr, "Failed, please provide source and "
1481 "destination of rule\n");
1485 memset(&attr, 0, sizeof(attr));
1487 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1494 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1499 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1504 attr.fa_src = attr.fa_dst = 0;
1508 fprintf(stderr, "error: %s: option '%s' "
1509 "unrecognized\n", argv[0], argv[optind - 1]);
1515 data.ioc_flags = opc;
1516 data.ioc_inllen1 = sizeof(attr);
1517 data.ioc_inlbuf1 = (char *)&attr;
1518 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1519 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1523 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1525 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1526 all ? "all" : libcfs_nid2str(attr.fa_src),
1527 all ? "all" : libcfs_nid2str(attr.fa_dst),
1532 libcfs_ioctl_unpack(&data, ioc_buf);
1533 printf("Removed %d %s rules\n", data.ioc_count, name);
1542 jt_ptl_drop_del(int argc, char **argv)
1544 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1548 jt_ptl_delay_del(int argc, char **argv)
1550 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1554 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1556 struct libcfs_ioctl_data data = { { 0 } };
1559 LIBCFS_IOC_INIT(data);
1560 data.ioc_flags = opc;
1562 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1564 fprintf(stderr, "failed to reset %s stats: %s\n",
1565 name, strerror(errno));
1572 jt_ptl_drop_reset(int argc, char **argv)
1574 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1578 jt_ptl_delay_reset(int argc, char **argv)
1580 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1585 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1587 struct libcfs_ioctl_data data = { { 0 } };
1588 struct lnet_fault_attr attr;
1589 struct lnet_fault_stat stat;
1592 printf("LNet %s rules:\n", name);
1593 for (pos = 0;; pos++) {
1596 memset(&attr, 0, sizeof(attr));
1597 memset(&stat, 0, sizeof(stat));
1599 data.ioc_count = pos;
1600 data.ioc_flags = opc;
1601 data.ioc_inllen1 = sizeof(attr);
1602 data.ioc_inlbuf1 = (char *)&attr;
1603 data.ioc_inllen2 = sizeof(stat);
1604 data.ioc_inlbuf2 = (char *)&stat;
1605 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1606 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1610 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1614 libcfs_ioctl_unpack(&data, ioc_buf);
1616 if (opc == LNET_CTL_DROP_LIST) {
1617 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1618 "%ju/%ju, PUT %ju, ACK %ju, GET "
1620 libcfs_nid2str(attr.fa_src),
1621 libcfs_nid2str(attr.fa_dst),
1622 attr.u.drop.da_rate, attr.u.drop.da_interval,
1623 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1624 (uintmax_t)stat.u.drop.ds_dropped,
1625 (uintmax_t)stat.fs_count,
1626 (uintmax_t)stat.fs_put,
1627 (uintmax_t)stat.fs_ack,
1628 (uintmax_t)stat.fs_get,
1629 (uintmax_t)stat.fs_reply);
1631 } else if (opc == LNET_CTL_DELAY_LIST) {
1632 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1633 ", msg %x, %ju/%ju, PUT %ju"
1634 ", ACK %ju, GET %ju, REP %ju\n",
1635 libcfs_nid2str(attr.fa_src),
1636 libcfs_nid2str(attr.fa_dst),
1637 attr.u.delay.la_rate, attr.u.delay.la_interval,
1638 attr.u.delay.la_latency,
1639 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1640 (uintmax_t)stat.u.delay.ls_delayed,
1641 (uintmax_t)stat.fs_count,
1642 (uintmax_t)stat.fs_put,
1643 (uintmax_t)stat.fs_ack,
1644 (uintmax_t)stat.fs_get,
1645 (uintmax_t)stat.fs_reply);
1648 printf("found total %d\n", pos);
1654 jt_ptl_drop_list(int argc, char **argv)
1656 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1660 jt_ptl_delay_list(int argc, char **argv)
1662 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1666 get_cycles_per_usec ()
1668 FILE *f = fopen ("/proc/cpuinfo", "r");
1673 while (fgets (line, sizeof (line), f) != NULL)
1674 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1681 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1685 int jt_ptl_testprotocompat(int argc, char **argv)
1687 struct libcfs_ioctl_data data;
1693 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1697 flags = strtol(argv[1], &end, 0);
1698 if (flags < 0 || *end != 0) {
1699 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1703 LIBCFS_IOC_INIT(data);
1704 data.ioc_flags = flags;
1705 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1708 fprintf(stderr, "test proto compat %x failed: %s\n",
1709 flags, strerror(errno));
1713 printf("test proto compat %x OK\n", flags);