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.
32 #include <sys/ioctl.h>
35 #include <linux/types.h>
37 #include <libcfs/util/string.h>
38 #include <libcfs/util/ioctl.h>
39 #include <libcfs/user-time.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]);
341 if (LNET_NETTYP(net) == QSWLND || LNET_NETTYP(net) == GMLND ||
342 LNET_NETTYP(net) == PTLLND || LNET_NETTYP(net) == CIBLND ||
343 LNET_NETTYP(net) == OPENIBLND || LNET_NETTYP(net) == IIBLND ||
344 LNET_NETTYP(net) == RALND || LNET_NETTYP(net) == VIBLND ||
345 LNET_NETTYP(net) == MXLND) {
346 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
356 jt_ptl_list_nids(int argc, char **argv)
358 struct libcfs_ioctl_data data;
359 int all = 0, return_nid = 0;
363 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
364 /* Hack to pass back value */
365 return_nid = (argc == 2) && (argv[1][0] == 1);
367 if ((argc > 2) && !(all || return_nid)) {
368 fprintf(stderr, "usage: %s [all]\n", argv[0]);
372 for (count = 0;; count++) {
373 LIBCFS_IOC_INIT (data);
374 data.ioc_count = count;
375 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
378 if ((count > 0) && (errno == ENOENT))
379 /* We found them all */
381 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
382 errno, strerror(errno));
386 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
387 printf("%s\n", libcfs_nid2str(data.ioc_nid));
389 *(__u64 *)(argv[1]) = data.ioc_nid;
399 jt_ptl_which_nid (int argc, char **argv)
401 struct libcfs_ioctl_data data;
404 lnet_nid_t best_nid = LNET_NID_ANY;
413 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
417 for (i = 1; i < argc; i++) {
419 nid = libcfs_str2nid(nidstr);
420 if (nid == LNET_NID_ANY) {
421 fprintf(stderr, "Can't parse NID %s\n", nidstr);
425 LIBCFS_IOC_INIT(data);
428 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
430 fprintf(stderr, "Can't get distance to %s: %s\n",
431 nidstr, strerror(errno));
435 dist = data.ioc_u32[0];
436 order = data.ioc_u32[1];
439 if (dist == -EHOSTUNREACH)
442 fprintf(stderr, "Unexpected distance to %s: %d\n",
447 if (best_nid == LNET_NID_ANY ||
449 (dist == best_dist && order < best_order)) {
456 if (best_nid == LNET_NID_ANY) {
457 fprintf(stderr, "No reachable NID\n");
461 printf("%s\n", libcfs_nid2str(best_nid));
466 jt_ptl_print_interfaces (int argc, char **argv)
468 struct libcfs_ioctl_data data;
469 char buffer[3][HOST_NAME_MAX + 1];
473 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
476 for (index = 0;;index++) {
477 LIBCFS_IOC_INIT(data);
478 data.ioc_net = g_net;
479 data.ioc_count = index;
481 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
485 printf ("%s: (%s/%s) npeer %d nroute %d\n",
486 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
487 sizeof(buffer[2]), 1),
488 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
489 sizeof(buffer[0]), 0),
490 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
491 sizeof(buffer[1]), 0),
492 data.ioc_u32[2], data.ioc_u32[3]);
496 if (errno == ENOENT) {
497 printf ("<no interfaces>\n");
499 fprintf(stderr, "Error getting interfaces: %s: "
509 jt_ptl_add_interface (int argc, char **argv)
511 struct libcfs_ioctl_data data;
514 __u32 netmask = 0xffffff00;
519 if (argc < 2 || argc > 3) {
520 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
524 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
527 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
528 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
533 count = strtol(argv[2], &end, 0);
534 if (count > 0 && count < 32 && *end == 0) {
536 for (i = count; i > 0; i--)
537 netmask = netmask|(1<<(32-i));
538 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
539 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
544 LIBCFS_IOC_INIT(data);
545 data.ioc_net = g_net;
546 data.ioc_u32[0] = ipaddr;
547 data.ioc_u32[1] = netmask;
549 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
551 fprintf (stderr, "failed to add interface: %s\n",
560 jt_ptl_del_interface (int argc, char **argv)
562 struct libcfs_ioctl_data data;
567 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
571 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
575 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
576 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
580 LIBCFS_IOC_INIT(data);
581 data.ioc_net = g_net;
582 data.ioc_u32[0] = ipaddr;
584 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
586 fprintf (stderr, "failed to delete interface: %s\n",
595 jt_ptl_print_peers (int argc, char **argv)
597 struct libcfs_ioctl_data data;
598 lnet_process_id_t id;
599 char buffer[2][HOST_NAME_MAX + 1];
603 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
606 for (index = 0;;index++) {
607 LIBCFS_IOC_INIT(data);
608 data.ioc_net = g_net;
609 data.ioc_count = index;
611 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
615 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
616 id.nid = data.ioc_nid;
617 id.pid = data.ioc_u32[4];
618 printf ("%-20s [%d]%s->%s:%d #%d\n",
620 data.ioc_count, /* persistence */
622 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
623 sizeof(buffer[0]), 1),
625 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
626 sizeof(buffer[1]), 1),
627 data.ioc_u32[1], /* peer port */
628 data.ioc_u32[3]); /* conn_count */
629 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
630 int disconn = data.ioc_flags >> 16;
636 state = data.ioc_flags & 0xffff ? "C" : "U";
638 printf ("%-20s (%d) %s [%d] "LPU64" "
639 "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 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]
650 printf ("%-20s [%d]\n",
651 libcfs_nid2str(data.ioc_nid), data.ioc_count);
656 if (errno == ENOENT) {
657 printf ("<no peers>\n");
659 fprintf(stderr, "Error getting peer list: %s: "
667 int jt_ptl_add_peer(int argc, char **argv)
669 struct libcfs_ioctl_data data;
675 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
679 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
684 nid = libcfs_str2nid(argv[1]);
685 if (nid == LNET_NID_ANY) {
686 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
690 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
691 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
695 if (lnet_parse_port (&port, argv[3]) != 0) {
696 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
700 LIBCFS_IOC_INIT(data);
701 data.ioc_net = g_net;
703 data.ioc_u32[0] = ip;
704 data.ioc_u32[1] = port;
706 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
708 fprintf (stderr, "failed to add peer: %s\n",
717 jt_ptl_del_peer (int argc, char **argv)
719 struct libcfs_ioctl_data data;
720 lnet_nid_t nid = LNET_NID_ANY;
721 lnet_pid_t pid = LNET_PID_ANY;
725 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
728 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
730 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
734 } else if (argc > 2) {
735 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
740 !libcfs_str2anynid(&nid, argv[1])) {
741 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
745 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
747 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
748 fprintf (stderr, "Can't parse ip addr: %s\n",
754 LIBCFS_IOC_INIT(data);
755 data.ioc_net = g_net;
757 data.ioc_u32[0] = ip;
758 data.ioc_u32[1] = pid;
760 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
762 fprintf (stderr, "failed to remove peer: %s\n",
771 jt_ptl_print_connections (int argc, char **argv)
773 struct libcfs_ioctl_data data;
774 lnet_process_id_t id;
775 char buffer[2][HOST_NAME_MAX + 1];
779 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
782 for (index = 0; ; index++) {
783 LIBCFS_IOC_INIT(data);
784 data.ioc_net = g_net;
785 data.ioc_count = index;
787 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
791 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
792 id.nid = data.ioc_nid;
793 id.pid = data.ioc_u32[6];
794 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
796 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
797 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
798 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
799 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
800 data.ioc_u32[4], /* scheduler */
802 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
803 sizeof(buffer[0]), 1),
805 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
806 sizeof(buffer[1]), 1),
807 data.ioc_u32[1], /* remote port */
808 data.ioc_count, /* tx buffer size */
809 data.ioc_u32[5], /* rx buffer size */
810 data.ioc_flags ? "nagle" : "nonagle");
811 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
812 printf ("%s mtu %d\n",
813 libcfs_nid2str(data.ioc_nid),
814 data.ioc_u32[0]); /* path MTU */
815 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
816 printf ("%-20s [%d]\n",
817 libcfs_nid2str(data.ioc_nid),
818 data.ioc_u32[0] /* device id */);
820 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
825 if (errno == ENOENT) {
826 printf ("<no connections>\n");
828 fprintf(stderr, "Error getting connection list: %s: "
836 int jt_ptl_disconnect(int argc, char **argv)
838 struct libcfs_ioctl_data data;
839 lnet_nid_t nid = LNET_NID_ANY;
844 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
848 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
852 !libcfs_str2anynid(&nid, argv[1])) {
853 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
857 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
859 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
860 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
864 LIBCFS_IOC_INIT(data);
865 data.ioc_net = g_net;
867 data.ioc_u32[0] = ipaddr;
869 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
871 fprintf(stderr, "failed to remove connection: %s\n",
879 int jt_ptl_push_connection (int argc, char **argv)
881 struct libcfs_ioctl_data data;
883 lnet_nid_t nid = LNET_NID_ANY;
886 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
890 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
894 !libcfs_str2anynid(&nid, argv[1])) {
895 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
899 LIBCFS_IOC_INIT(data);
900 data.ioc_net = g_net;
903 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
905 fprintf(stderr, "failed to push connection: %s\n",
913 int jt_ptl_ping(int argc, char **argv)
917 lnet_process_id_t id;
918 lnet_process_id_t ids[16];
919 int maxids = sizeof(ids)/sizeof(ids[0]);
920 struct libcfs_ioctl_data data;
925 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
929 sep = strchr(argv[1], '-');
931 rc = lnet_parse_nid(argv[1], &id);
937 if (argv[1][0] == 'u' ||
939 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
941 id.pid = strtoul(argv[1], &end, 0);
943 if (end != sep) { /* assuming '-' is part of hostname */
944 rc = lnet_parse_nid(argv[1], &id);
948 id.nid = libcfs_str2nid(sep + 1);
950 if (id.nid == LNET_NID_ANY) {
952 "Can't parse process id \"%s\"\n",
960 timeout = 1000 * atol(argv[2]);
962 timeout = 1000; /* default 1 second timeout */
964 LIBCFS_IOC_INIT (data);
965 data.ioc_nid = id.nid;
966 data.ioc_u32[0] = id.pid;
967 data.ioc_u32[1] = timeout;
968 data.ioc_plen1 = sizeof(ids);
969 data.ioc_pbuf1 = (char *)ids;
971 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
973 fprintf(stderr, "failed to ping %s: %s\n",
974 id.pid == LNET_PID_ANY ?
975 libcfs_nid2str(id.nid) : libcfs_id2str(id),
980 for (i = 0; i < data.ioc_count && i < maxids; i++)
981 printf("%s\n", libcfs_id2str(ids[i]));
983 if (data.ioc_count > maxids)
984 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
989 int jt_ptl_mynid(int argc, char **argv)
991 struct libcfs_ioctl_data data;
996 fprintf(stderr, "usage: %s NID\n", argv[0]);
1000 nid = libcfs_str2nid(argv[1]);
1001 if (nid == LNET_NID_ANY) {
1002 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1006 LIBCFS_IOC_INIT(data);
1007 data.ioc_net = LNET_NIDNET(nid);
1010 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1012 fprintf(stderr, "setting my NID failed: %s\n",
1015 printf("registered my nid %s\n", libcfs_nid2str(nid));
1021 jt_ptl_fail_nid (int argc, char **argv)
1026 struct libcfs_ioctl_data data;
1028 if (argc < 2 || argc > 3)
1030 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1034 if (!libcfs_str2anynid(&nid, argv[1]))
1036 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1041 threshold = LNET_MD_THRESH_INF;
1042 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1043 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1047 LIBCFS_IOC_INIT (data);
1049 data.ioc_count = threshold;
1051 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1053 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1056 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1062 jt_ptl_add_route (int argc, char **argv)
1064 struct lnet_ioctl_config_data data;
1065 lnet_nid_t gateway_nid;
1066 __u32 hops = LNET_UNDEFINED_HOPS;
1067 unsigned int priority = 0;
1071 if (argc < 2 || argc > 4) {
1072 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1077 if (g_net_is_set(argv[0]) == 0)
1080 gateway_nid = libcfs_str2nid(argv[1]);
1081 if (gateway_nid == LNET_NID_ANY) {
1082 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1087 hops = strtol(argv[2], &end, 0);
1088 if (hops == 0 || hops >= 256 ||
1089 (end != NULL && *end != 0)) {
1090 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1095 priority = strtoul(argv[3], &end, 0);
1096 if (end != NULL && *end != 0) {
1098 "Can't parse priority \"%s\"\n",
1105 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1106 data.cfg_net = g_net;
1107 data.cfg_config_u.cfg_route.rtr_hop = hops;
1108 data.cfg_nid = gateway_nid;
1109 data.cfg_config_u.cfg_route.rtr_priority = priority;
1111 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1113 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1122 jt_ptl_del_route (int argc, char **argv)
1124 struct lnet_ioctl_config_data data;
1129 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1133 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1134 fprintf(stderr, "Can't parse gateway NID "
1135 "\"%s\"\n", argv[1]);
1139 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1140 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1143 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1145 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1146 libcfs_nid2str(nid), strerror(errno));
1154 jt_ptl_notify_router (int argc, char **argv)
1156 struct libcfs_ioctl_data data;
1165 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1170 nid = libcfs_str2nid(argv[1]);
1171 if (nid == LNET_NID_ANY) {
1172 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1176 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1177 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1181 gettimeofday(&now, NULL);
1185 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1186 fprintf(stderr, "Can't parse time %s\n"
1187 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1188 "or an absolute unix time in seconds\n", argv[3]);
1190 } else if (when > now.tv_sec) {
1191 fprintf (stderr, "%s specifies a time in the future\n",
1196 LIBCFS_IOC_INIT(data);
1198 data.ioc_flags = enable;
1199 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1200 data.ioc_u64[0] = (__u64)when;
1202 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1204 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1205 libcfs_nid2str(nid), strerror (errno));
1213 jt_ptl_print_routes (int argc, char **argv)
1215 struct lnet_ioctl_config_data data;
1224 for (index = 0; ; index++) {
1225 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1226 data.cfg_count = index;
1228 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1233 hops = data.cfg_config_u.cfg_route.rtr_hop;
1235 alive = data.cfg_config_u.cfg_route.rtr_flags;
1236 pri = data.cfg_config_u.cfg_route.rtr_priority;
1238 printf("net %18s hops %u gw %32s %s pri %u\n",
1239 libcfs_net2str(net), hops,
1240 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1243 if (errno != ENOENT)
1244 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1251 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1257 /* NB: can't support range ipaddress except * and *@net */
1258 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1259 net = libcfs_str2net(str + 2);
1260 if (net == LNET_NIDNET(LNET_NID_ANY))
1263 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1265 rc = libcfs_str2anynid(&nid, str);
1273 fprintf(stderr, "Invalid NID : %s\n", str);
1278 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1280 if (!strcasecmp(msg_str, "put")) {
1281 *mask_p |= LNET_PUT_BIT;
1284 } else if (!strcasecmp(msg_str, "ack")) {
1285 *mask_p |= LNET_ACK_BIT;
1288 } else if (!strcasecmp(msg_str, "get")) {
1289 *mask_p |= LNET_GET_BIT;
1292 } else if (!strcasecmp(msg_str, "reply")) {
1293 *mask_p |= LNET_REPLY_BIT;
1297 fprintf(stderr, "unknown message type %s\n", msg_str);
1302 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1304 unsigned long rc = strtoul(optarg, NULL, 0);
1307 fprintf(stderr, "invalid portal: %lu\n", rc);
1311 *mask_p |= (1ULL << rc);
1316 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1318 struct libcfs_ioctl_data data = {{0}};
1319 struct lnet_fault_attr attr;
1323 static struct option opts[] = {
1324 {"source", required_argument, 0, 's'},
1325 {"dest", required_argument, 0, 'd'},
1326 {"rate", required_argument, 0, 'r'},
1327 {"interval", required_argument, 0, 'i'},
1328 {"latency", required_argument, 0, 'l'},
1329 {"portal", required_argument, 0, 'p'},
1330 {"message", required_argument, 0, 'm'},
1335 fprintf(stderr, "Failed, please provide source, destination "
1336 "and rate of rule\n");
1340 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1341 memset(&attr, 0, sizeof(attr));
1343 char c = getopt_long(argc, argv, optstr, opts, NULL);
1349 case 's': /* source NID/NET */
1350 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1355 case 'd': /* dest NID/NET */
1356 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1361 case 'r': /* drop rate */
1362 if (opc == LNET_CTL_DROP_ADD)
1363 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1365 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1368 case 'i': /* time interval (# seconds) for message drop */
1369 if (opc == LNET_CTL_DROP_ADD)
1370 attr.u.drop.da_interval = strtoul(optarg,
1373 attr.u.delay.la_interval = strtoul(optarg,
1377 case 'l': /* seconds to wait before activating rule */
1378 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1381 case 'p': /* portal to filter */
1382 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1387 case 'm': /* message types to filter */
1388 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1394 fprintf(stderr, "error: %s: option '%s' "
1395 "unrecognized\n", argv[0], argv[optind - 1]);
1401 if (opc == LNET_CTL_DROP_ADD) {
1402 /* NB: drop rate and interval are exclusive to each other */
1403 if (!((attr.u.drop.da_rate == 0) ^
1404 (attr.u.drop.da_interval == 0))) {
1406 "please provide either drop rate or interval "
1407 "but not both at the same time.\n");
1410 } else if (opc == LNET_CTL_DELAY_ADD) {
1411 if (!((attr.u.delay.la_rate == 0) ^
1412 (attr.u.delay.la_interval == 0))) {
1414 "please provide either delay rate or interval "
1415 "but not both at the same time.\n");
1419 if (attr.u.delay.la_latency == 0) {
1420 fprintf(stderr, "latency cannot be zero\n");
1425 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1426 fprintf(stderr, "Please provide both source and destination "
1427 "of %s rule\n", name);
1431 data.ioc_flags = opc;
1432 data.ioc_inllen1 = sizeof(attr);
1433 data.ioc_inlbuf1 = (char *)&attr;
1434 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1435 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1439 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1441 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1442 name, libcfs_nid2str(attr.fa_src),
1443 libcfs_nid2str(attr.fa_dst), strerror(errno));
1447 printf("Added %s rule %s->%s (1/%d)\n",
1448 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1449 opc == LNET_CTL_DROP_ADD ?
1450 attr.u.drop.da_rate : attr.u.delay.la_rate);
1459 jt_ptl_drop_add(int argc, char **argv)
1461 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1465 jt_ptl_delay_add(int argc, char **argv)
1467 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1471 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1473 struct libcfs_ioctl_data data = {{0}};
1474 struct lnet_fault_attr attr;
1478 static struct option opts[] = {
1479 {"source", required_argument, 0, 's'},
1480 {"dest", required_argument, 0, 'd'},
1481 {"all", no_argument, 0, 'a'},
1486 fprintf(stderr, "Failed, please provide source and "
1487 "destination of rule\n");
1491 memset(&attr, 0, sizeof(attr));
1493 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1500 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1505 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1510 attr.fa_src = attr.fa_dst = 0;
1514 fprintf(stderr, "error: %s: option '%s' "
1515 "unrecognized\n", argv[0], argv[optind - 1]);
1521 data.ioc_flags = opc;
1522 data.ioc_inllen1 = sizeof(attr);
1523 data.ioc_inlbuf1 = (char *)&attr;
1524 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1525 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1529 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1531 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1532 all ? "all" : libcfs_nid2str(attr.fa_src),
1533 all ? "all" : libcfs_nid2str(attr.fa_dst),
1538 libcfs_ioctl_unpack(&data, ioc_buf);
1539 printf("Removed %d %s rules\n", data.ioc_count, name);
1548 jt_ptl_drop_del(int argc, char **argv)
1550 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1554 jt_ptl_delay_del(int argc, char **argv)
1556 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1560 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1562 struct libcfs_ioctl_data data = {{0}};
1565 LIBCFS_IOC_INIT(data);
1566 data.ioc_flags = opc;
1568 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1570 fprintf(stderr, "failed to reset %s stats: %s\n",
1571 name, strerror(errno));
1578 jt_ptl_drop_reset(int argc, char **argv)
1580 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1584 jt_ptl_delay_reset(int argc, char **argv)
1586 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1591 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1593 struct libcfs_ioctl_data data = {{0}};
1594 struct lnet_fault_attr attr;
1595 struct lnet_fault_stat stat;
1598 printf("LNet %s rules:\n", name);
1599 for (pos = 0;; pos++) {
1602 memset(&attr, 0, sizeof(attr));
1603 memset(&stat, 0, sizeof(stat));
1605 data.ioc_count = pos;
1606 data.ioc_flags = opc;
1607 data.ioc_inllen1 = sizeof(attr);
1608 data.ioc_inlbuf1 = (char *)&attr;
1609 data.ioc_inllen2 = sizeof(stat);
1610 data.ioc_inlbuf2 = (char *)&stat;
1611 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1612 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1616 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1620 libcfs_ioctl_unpack(&data, ioc_buf);
1622 if (opc == LNET_CTL_DROP_LIST) {
1623 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1624 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1625 LPU64", REP "LPU64"\n",
1626 libcfs_nid2str(attr.fa_src),
1627 libcfs_nid2str(attr.fa_dst),
1628 attr.u.drop.da_rate, attr.u.drop.da_interval,
1629 attr.fa_ptl_mask, attr.fa_msg_mask,
1630 stat.u.drop.ds_dropped, stat.fs_count,
1631 stat.fs_put, stat.fs_ack,
1632 stat.fs_get, stat.fs_reply);
1634 } else if (opc == LNET_CTL_DELAY_LIST) {
1635 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1636 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1637 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1638 libcfs_nid2str(attr.fa_src),
1639 libcfs_nid2str(attr.fa_dst),
1640 attr.u.delay.la_rate, attr.u.delay.la_interval,
1641 attr.u.delay.la_latency,
1642 attr.fa_ptl_mask, attr.fa_msg_mask,
1643 stat.u.delay.ls_delayed, stat.fs_count,
1644 stat.fs_put, stat.fs_ack, stat.fs_get,
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_memhog(int argc, char **argv)
1687 static int gfp = 0; /* sticky! */
1689 struct libcfs_ioctl_data data;
1695 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1699 count = strtol(argv[1], &end, 0);
1700 if (count < 0 || *end != 0) {
1701 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1706 rc = strtol(argv[2], &end, 0);
1708 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1714 LIBCFS_IOC_INIT(data);
1715 data.ioc_count = count;
1716 data.ioc_flags = gfp;
1717 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1720 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1724 printf("memhog %d OK\n", count);
1728 int jt_ptl_testprotocompat(int argc, char **argv)
1730 struct libcfs_ioctl_data data;
1736 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1740 flags = strtol(argv[1], &end, 0);
1741 if (flags < 0 || *end != 0) {
1742 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1746 LIBCFS_IOC_INIT(data);
1747 data.ioc_flags = flags;
1748 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1751 fprintf(stderr, "test proto compat %x failed: %s\n",
1752 flags, strerror(errno));
1756 printf("test proto compat %x OK\n", flags);