2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2014, Intel Corporation.
6 * This file is part of 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;
51 #define IOC_BUF_SIZE 8192
52 static char local_buf[IOC_BUF_SIZE];
53 static char *ioc_buf = local_buf;
55 /* Convert a string boolean to an int; "enable" -> 1 */
57 lnet_parse_bool (int *b, char *str)
59 if (!strcasecmp (str, "no") ||
60 !strcasecmp (str, "n") ||
61 !strcasecmp (str, "off") ||
62 !strcasecmp (str, "down") ||
63 !strcasecmp (str, "disable"))
69 if (!strcasecmp (str, "yes") ||
70 !strcasecmp (str, "y") ||
71 !strcasecmp (str, "on") ||
72 !strcasecmp (str, "up") ||
73 !strcasecmp (str, "enable"))
83 lnet_parse_port (int *port, char *str)
87 *port = strtol (str, &end, 0);
89 if (*end == 0 && /* parsed whole string */
90 *port > 0 && *port < 65536) /* minimal sanity check */
96 #ifdef HAVE_GETHOSTBYNAME
97 static struct hostent *
98 ptl_gethostbyname(char * hname) {
100 he = gethostbyname(hname);
105 fprintf(stderr, "Unable to resolve hostname: %s\n",
109 fprintf(stderr, "gethostbyname error for %s: %s\n",
110 hname, strerror(h_errno));
120 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
127 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
128 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
129 (c & ~0xff) == 0 && (d & ~0xff) == 0)
131 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
139 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
141 #ifdef HAVE_GETHOSTBYNAME
145 if (!strcmp (str, "_all_")) {
150 if (lnet_parse_ipquad(ipaddrp, str) == 0)
153 #ifdef HAVE_GETHOSTBYNAME
154 if ((('a' <= str[0] && str[0] <= 'z') ||
155 ('A' <= str[0] && str[0] <= 'Z')) &&
156 (he = ptl_gethostbyname (str)) != NULL) {
157 __u32 addr = *(__u32 *)he->h_addr;
159 *ipaddrp = ntohl(addr); /* HOST byte order */
168 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
170 #ifdef HAVE_GETHOSTBYNAME
175 net_ip = htonl (ipaddr);
176 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
178 strlcpy(str, he->h_name, strsize);
184 sprintf (str, "%d.%d.%d.%d",
185 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
186 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
191 lnet_parse_time (time_t *t, char *str)
197 *t = strtol (str, &end, 0);
198 if (*end == 0) /* parsed whole string */
201 memset (&tm, 0, sizeof (tm));
202 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
203 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
204 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
208 tm.tm_mon--; /* convert to 0 == Jan */
209 tm.tm_year -= 1900; /* y2k quirk */
210 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
213 if (*t == (time_t)-1)
220 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
222 id_ptr->pid = LNET_PID_ANY;
223 id_ptr->nid = libcfs_str2nid(nid_str);
224 if (id_ptr->nid == LNET_NID_ANY) {
225 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
232 int g_net_is_set (char *cmd)
239 "You must run the 'network' command before '%s'.\n",
244 int g_net_is_compatible (char *cmd, ...)
249 if (!g_net_is_set(cmd))
255 nal = va_arg (ap, int);
256 if (nal == LNET_NETTYP(g_net)) {
266 "Command %s not compatible with %s NAL\n",
268 libcfs_lnd2str(LNET_NETTYP(g_net)));
272 int ptl_initialize(int argc, char **argv)
274 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
275 LNET_DEV_MAJOR, LNET_DEV_MINOR);
280 int jt_ptl_network(int argc, char **argv)
282 struct libcfs_ioctl_data data;
283 __u32 net = LNET_NIDNET(LNET_NID_ANY);
287 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
291 if (!strcmp(argv[1], "unconfigure") ||
292 !strcmp(argv[1], "down")) {
293 LIBCFS_IOC_INIT(data);
294 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
297 printf ("LNET ready to unload\n");
302 fprintf(stderr, "LNET busy\n");
304 fprintf(stderr, "LNET unconfigure error %d: %s\n",
305 errno, strerror(errno));
309 if (!strcmp(argv[1], "configure") ||
310 !strcmp(argv[1], "up")) {
311 LIBCFS_IOC_INIT(data);
312 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
315 printf ("LNET configured\n");
319 fprintf(stderr, "LNET configure error %d: %s\n",
320 errno, strerror(errno));
324 net = libcfs_str2net(argv[1]);
325 if (net == LNET_NIDNET(LNET_NID_ANY)) {
326 fprintf(stderr, "Can't parse net %s\n", argv[1]);
330 if (LNET_NETTYP(net) == CIBLND ||
331 LNET_NETTYP(net) == OPENIBLND ||
332 LNET_NETTYP(net) == IIBLND ||
333 LNET_NETTYP(net) == VIBLND) {
334 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
344 jt_ptl_list_nids(int argc, char **argv)
346 struct libcfs_ioctl_data data;
347 int all = 0, return_nid = 0;
351 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
352 /* Hack to pass back value */
353 return_nid = (argc == 2) && (argv[1][0] == 1);
355 if ((argc > 2) && !(all || return_nid)) {
356 fprintf(stderr, "usage: %s [all]\n", argv[0]);
360 for (count = 0;; count++) {
361 LIBCFS_IOC_INIT (data);
362 data.ioc_count = count;
363 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
366 if ((count > 0) && (errno == ENOENT))
367 /* We found them all */
369 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
370 errno, strerror(errno));
374 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
375 printf("%s\n", libcfs_nid2str(data.ioc_nid));
377 *(__u64 *)(argv[1]) = data.ioc_nid;
387 jt_ptl_which_nid (int argc, char **argv)
389 struct libcfs_ioctl_data data;
392 lnet_nid_t best_nid = LNET_NID_ANY;
401 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
405 for (i = 1; i < argc; i++) {
407 nid = libcfs_str2nid(nidstr);
408 if (nid == LNET_NID_ANY) {
409 fprintf(stderr, "Can't parse NID %s\n", nidstr);
413 LIBCFS_IOC_INIT(data);
416 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
418 fprintf(stderr, "Can't get distance to %s: %s\n",
419 nidstr, strerror(errno));
423 dist = data.ioc_u32[0];
424 order = data.ioc_u32[1];
427 if (dist == -EHOSTUNREACH)
430 fprintf(stderr, "Unexpected distance to %s: %d\n",
435 if (best_nid == LNET_NID_ANY ||
437 (dist == best_dist && order < best_order)) {
444 if (best_nid == LNET_NID_ANY) {
445 fprintf(stderr, "No reachable NID\n");
449 printf("%s\n", libcfs_nid2str(best_nid));
454 jt_ptl_print_interfaces (int argc, char **argv)
456 struct libcfs_ioctl_data data;
457 char buffer[3][HOST_NAME_MAX + 1];
461 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
464 for (index = 0;;index++) {
465 LIBCFS_IOC_INIT(data);
466 data.ioc_net = g_net;
467 data.ioc_count = index;
469 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
473 printf ("%s: (%s/%s) npeer %d nroute %d\n",
474 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
475 sizeof(buffer[2]), 1),
476 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
477 sizeof(buffer[0]), 0),
478 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
479 sizeof(buffer[1]), 0),
480 data.ioc_u32[2], data.ioc_u32[3]);
484 if (errno == ENOENT) {
485 printf ("<no interfaces>\n");
487 fprintf(stderr, "Error getting interfaces: %s: "
497 jt_ptl_add_interface (int argc, char **argv)
499 struct libcfs_ioctl_data data;
502 __u32 netmask = 0xffffff00;
507 if (argc < 2 || argc > 3) {
508 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
512 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
515 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
516 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
521 count = strtol(argv[2], &end, 0);
522 if (count > 0 && count < 32 && *end == 0) {
524 for (i = count; i > 0; i--)
525 netmask = netmask|(1<<(32-i));
526 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
527 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
532 LIBCFS_IOC_INIT(data);
533 data.ioc_net = g_net;
534 data.ioc_u32[0] = ipaddr;
535 data.ioc_u32[1] = netmask;
537 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
539 fprintf (stderr, "failed to add interface: %s\n",
548 jt_ptl_del_interface (int argc, char **argv)
550 struct libcfs_ioctl_data data;
555 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
559 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
563 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
564 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
568 LIBCFS_IOC_INIT(data);
569 data.ioc_net = g_net;
570 data.ioc_u32[0] = ipaddr;
572 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
574 fprintf (stderr, "failed to delete interface: %s\n",
583 jt_ptl_print_peers (int argc, char **argv)
585 struct libcfs_ioctl_data data;
586 lnet_process_id_t id;
587 char buffer[2][HOST_NAME_MAX + 1];
591 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
594 for (index = 0;;index++) {
595 LIBCFS_IOC_INIT(data);
596 data.ioc_net = g_net;
597 data.ioc_count = index;
599 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
603 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
604 id.nid = data.ioc_nid;
605 id.pid = data.ioc_u32[4];
606 printf ("%-20s [%d]%s->%s:%d #%d\n",
608 data.ioc_count, /* persistence */
610 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
611 sizeof(buffer[0]), 1),
613 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
614 sizeof(buffer[1]), 1),
615 data.ioc_u32[1], /* peer port */
616 data.ioc_u32[3]); /* conn_count */
617 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
618 int disconn = data.ioc_flags >> 16;
624 state = data.ioc_flags & 0xffff ? "C" : "U";
626 printf ("%-20s (%d) %s [%d] "LPU64" "
627 "sq %d/%d tx %d/%d/%d\n",
628 libcfs_nid2str(data.ioc_nid), /* peer nid */
629 data.ioc_net, /* gemini device id */
630 state, /* peer is Connecting, Up, or Down */
631 data.ioc_count, /* peer refcount */
632 data.ioc_u64[0], /* peerstamp */
633 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
634 /* fmaq, nfma, nrdma */
635 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
638 printf ("%-20s [%d]\n",
639 libcfs_nid2str(data.ioc_nid), data.ioc_count);
644 if (errno == ENOENT) {
645 printf ("<no peers>\n");
647 fprintf(stderr, "Error getting peer list: %s: "
656 jt_ptl_add_peer (int argc, char **argv)
658 struct libcfs_ioctl_data data;
664 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
668 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
673 nid = libcfs_str2nid(argv[1]);
674 if (nid == LNET_NID_ANY) {
675 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
679 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
680 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
684 if (lnet_parse_port (&port, argv[3]) != 0) {
685 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
689 LIBCFS_IOC_INIT(data);
690 data.ioc_net = g_net;
692 data.ioc_u32[0] = ip;
693 data.ioc_u32[1] = port;
695 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
697 fprintf (stderr, "failed to add peer: %s\n",
706 jt_ptl_del_peer (int argc, char **argv)
708 struct libcfs_ioctl_data data;
709 lnet_nid_t nid = LNET_NID_ANY;
710 lnet_pid_t pid = LNET_PID_ANY;
714 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
717 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
719 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
723 } else if (argc > 2) {
724 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
729 !libcfs_str2anynid(&nid, argv[1])) {
730 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
734 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
736 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
737 fprintf (stderr, "Can't parse ip addr: %s\n",
743 LIBCFS_IOC_INIT(data);
744 data.ioc_net = g_net;
746 data.ioc_u32[0] = ip;
747 data.ioc_u32[1] = pid;
749 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
751 fprintf (stderr, "failed to remove peer: %s\n",
760 jt_ptl_print_connections (int argc, char **argv)
762 struct libcfs_ioctl_data data;
763 lnet_process_id_t id;
764 char buffer[2][HOST_NAME_MAX + 1];
768 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
771 for (index = 0; ; index++) {
772 LIBCFS_IOC_INIT(data);
773 data.ioc_net = g_net;
774 data.ioc_count = index;
776 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
780 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
781 id.nid = data.ioc_nid;
782 id.pid = data.ioc_u32[6];
783 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
785 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
786 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
787 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
788 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
789 data.ioc_u32[4], /* scheduler */
791 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
792 sizeof(buffer[0]), 1),
794 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
795 sizeof(buffer[1]), 1),
796 data.ioc_u32[1], /* remote port */
797 data.ioc_count, /* tx buffer size */
798 data.ioc_u32[5], /* rx buffer size */
799 data.ioc_flags ? "nagle" : "nonagle");
800 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
801 printf ("%s mtu %d\n",
802 libcfs_nid2str(data.ioc_nid),
803 data.ioc_u32[0]); /* path MTU */
804 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
805 printf ("%-20s [%d]\n",
806 libcfs_nid2str(data.ioc_nid),
807 data.ioc_u32[0] /* device id */);
809 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
814 if (errno == ENOENT) {
815 printf ("<no connections>\n");
817 fprintf(stderr, "Error getting connection list: %s: "
825 int jt_ptl_disconnect(int argc, char **argv)
827 struct libcfs_ioctl_data data;
828 lnet_nid_t nid = LNET_NID_ANY;
833 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
837 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
841 !libcfs_str2anynid(&nid, argv[1])) {
842 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
846 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
848 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
849 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
853 LIBCFS_IOC_INIT(data);
854 data.ioc_net = g_net;
856 data.ioc_u32[0] = ipaddr;
858 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
860 fprintf(stderr, "failed to remove connection: %s\n",
868 int jt_ptl_push_connection (int argc, char **argv)
870 struct libcfs_ioctl_data data;
872 lnet_nid_t nid = LNET_NID_ANY;
875 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
879 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
883 !libcfs_str2anynid(&nid, argv[1])) {
884 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
888 LIBCFS_IOC_INIT(data);
889 data.ioc_net = g_net;
892 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
894 fprintf(stderr, "failed to push connection: %s\n",
902 int jt_ptl_ping(int argc, char **argv)
906 lnet_process_id_t id;
907 lnet_process_id_t ids[16];
908 int maxids = sizeof(ids)/sizeof(ids[0]);
909 struct libcfs_ioctl_data data;
914 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
918 sep = strchr(argv[1], '-');
920 rc = lnet_parse_nid(argv[1], &id);
926 if (argv[1][0] == 'u' ||
928 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
930 id.pid = strtoul(argv[1], &end, 0);
932 if (end != sep) { /* assuming '-' is part of hostname */
933 rc = lnet_parse_nid(argv[1], &id);
937 id.nid = libcfs_str2nid(sep + 1);
939 if (id.nid == LNET_NID_ANY) {
941 "Can't parse process id \"%s\"\n",
949 timeout = 1000 * atol(argv[2]);
951 timeout = 1000; /* default 1 second timeout */
953 LIBCFS_IOC_INIT (data);
954 data.ioc_nid = id.nid;
955 data.ioc_u32[0] = id.pid;
956 data.ioc_u32[1] = timeout;
957 data.ioc_plen1 = sizeof(ids);
958 data.ioc_pbuf1 = (char *)ids;
960 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
962 fprintf(stderr, "failed to ping %s: %s\n",
963 id.pid == LNET_PID_ANY ?
964 libcfs_nid2str(id.nid) : libcfs_id2str(id),
969 for (i = 0; i < data.ioc_count && i < maxids; i++)
970 printf("%s\n", libcfs_id2str(ids[i]));
972 if (data.ioc_count > maxids)
973 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
978 int jt_ptl_mynid(int argc, char **argv)
980 struct libcfs_ioctl_data data;
985 fprintf(stderr, "usage: %s NID\n", argv[0]);
989 nid = libcfs_str2nid(argv[1]);
990 if (nid == LNET_NID_ANY) {
991 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
995 LIBCFS_IOC_INIT(data);
996 data.ioc_net = LNET_NIDNET(nid);
999 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1001 fprintf(stderr, "setting my NID failed: %s\n",
1004 printf("registered my nid %s\n", libcfs_nid2str(nid));
1010 jt_ptl_fail_nid (int argc, char **argv)
1015 struct libcfs_ioctl_data data;
1017 if (argc < 2 || argc > 3)
1019 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1023 if (!libcfs_str2anynid(&nid, argv[1]))
1025 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1030 threshold = LNET_MD_THRESH_INF;
1031 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1032 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1036 LIBCFS_IOC_INIT (data);
1038 data.ioc_count = threshold;
1040 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1042 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1045 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1051 jt_ptl_add_route (int argc, char **argv)
1053 struct lnet_ioctl_config_data data;
1054 lnet_nid_t gateway_nid;
1055 unsigned int hops = 1;
1056 unsigned int priority = 0;
1060 if (argc < 2 || argc > 4) {
1061 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1066 if (g_net_is_set(argv[0]) == 0)
1069 gateway_nid = libcfs_str2nid(argv[1]);
1070 if (gateway_nid == LNET_NID_ANY) {
1071 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1076 hops = strtoul(argv[2], &end, 0);
1077 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1078 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1083 priority = strtoul(argv[3], &end, 0);
1084 if (end != NULL && *end != 0) {
1086 "Can't parse priority \"%s\"\n",
1093 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1094 data.cfg_net = g_net;
1095 data.cfg_config_u.cfg_route.rtr_hop = hops;
1096 data.cfg_nid = gateway_nid;
1097 data.cfg_config_u.cfg_route.rtr_priority = priority;
1099 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1101 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1110 jt_ptl_del_route (int argc, char **argv)
1112 struct lnet_ioctl_config_data data;
1117 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1121 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1122 fprintf(stderr, "Can't parse gateway NID "
1123 "\"%s\"\n", argv[1]);
1127 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1128 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1131 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1133 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1134 libcfs_nid2str(nid), strerror(errno));
1142 jt_ptl_notify_router (int argc, char **argv)
1144 struct libcfs_ioctl_data data;
1153 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1158 nid = libcfs_str2nid(argv[1]);
1159 if (nid == LNET_NID_ANY) {
1160 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1164 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1165 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1169 gettimeofday(&now, NULL);
1173 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1174 fprintf(stderr, "Can't parse time %s\n"
1175 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1176 "or an absolute unix time in seconds\n", argv[3]);
1178 } else if (when > now.tv_sec) {
1179 fprintf (stderr, "%s specifies a time in the future\n",
1184 LIBCFS_IOC_INIT(data);
1186 data.ioc_flags = enable;
1187 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1188 data.ioc_u64[0] = (__u64)when;
1190 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1192 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1193 libcfs_nid2str(nid), strerror (errno));
1201 jt_ptl_print_routes (int argc, char **argv)
1203 struct lnet_ioctl_config_data data;
1212 for (index = 0; ; index++) {
1213 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1214 data.cfg_count = index;
1216 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1221 hops = data.cfg_config_u.cfg_route.rtr_hop;
1223 alive = data.cfg_config_u.cfg_route.rtr_flags;
1224 pri = data.cfg_config_u.cfg_route.rtr_priority;
1226 printf("net %18s hops %u gw %32s %s pri %u\n",
1227 libcfs_net2str(net), hops,
1228 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1231 if (errno != ENOENT)
1232 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1239 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1245 /* NB: can't support range ipaddress except * and *@net */
1246 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1247 net = libcfs_str2net(str + 2);
1248 if (net == LNET_NIDNET(LNET_NID_ANY))
1251 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1253 rc = libcfs_str2anynid(&nid, str);
1261 fprintf(stderr, "Invalid NID : %s\n", str);
1266 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1268 if (!strcasecmp(msg_str, "put")) {
1269 *mask_p |= LNET_PUT_BIT;
1272 } else if (!strcasecmp(msg_str, "ack")) {
1273 *mask_p |= LNET_ACK_BIT;
1276 } else if (!strcasecmp(msg_str, "get")) {
1277 *mask_p |= LNET_GET_BIT;
1280 } else if (!strcasecmp(msg_str, "reply")) {
1281 *mask_p |= LNET_REPLY_BIT;
1285 fprintf(stderr, "unknown message type %s\n", msg_str);
1290 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1292 unsigned long rc = strtoul(optarg, NULL, 0);
1295 fprintf(stderr, "invalid portal: %lu\n", rc);
1299 *mask_p |= (1ULL << rc);
1304 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1306 struct libcfs_ioctl_data data = {{0}};
1307 struct lnet_fault_attr attr;
1311 static struct option opts[] = {
1312 {"source", required_argument, 0, 's'},
1313 {"dest", required_argument, 0, 'd'},
1314 {"rate", required_argument, 0, 'r'},
1315 {"interval", required_argument, 0, 'i'},
1316 {"latency", required_argument, 0, 'l'},
1317 {"portal", required_argument, 0, 'p'},
1318 {"message", required_argument, 0, 'm'},
1323 fprintf(stderr, "Failed, please provide source, destination "
1324 "and rate of rule\n");
1328 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1329 memset(&attr, 0, sizeof(attr));
1331 char c = getopt_long(argc, argv, optstr, opts, NULL);
1337 case 's': /* source NID/NET */
1338 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1343 case 'd': /* dest NID/NET */
1344 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1349 case 'r': /* drop rate */
1350 if (opc == LNET_CTL_DROP_ADD)
1351 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1353 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1356 case 'i': /* time interval (# seconds) for message drop */
1357 if (opc == LNET_CTL_DROP_ADD)
1358 attr.u.drop.da_interval = strtoul(optarg,
1361 attr.u.delay.la_interval = strtoul(optarg,
1365 case 'l': /* seconds to wait before activating rule */
1366 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1369 case 'p': /* portal to filter */
1370 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1375 case 'm': /* message types to filter */
1376 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1382 fprintf(stderr, "error: %s: option '%s' "
1383 "unrecognized\n", argv[0], argv[optind - 1]);
1389 if (opc == LNET_CTL_DROP_ADD) {
1390 /* NB: drop rate and interval are exclusive to each other */
1391 if (!((attr.u.drop.da_rate == 0) ^
1392 (attr.u.drop.da_interval == 0))) {
1394 "please provide either drop rate or interval "
1395 "but not both at the same time.\n");
1398 } else if (opc == LNET_CTL_DELAY_ADD) {
1399 if (!((attr.u.delay.la_rate == 0) ^
1400 (attr.u.delay.la_interval == 0))) {
1402 "please provide either delay rate or interval "
1403 "but not both at the same time.\n");
1407 if (attr.u.delay.la_latency == 0) {
1408 fprintf(stderr, "latency cannot be zero\n");
1413 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1414 fprintf(stderr, "Please provide both source and destination "
1415 "of %s rule\n", name);
1419 data.ioc_flags = opc;
1420 data.ioc_inllen1 = sizeof(attr);
1421 data.ioc_inlbuf1 = (char *)&attr;
1422 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1423 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1427 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1429 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1430 name, libcfs_nid2str(attr.fa_src),
1431 libcfs_nid2str(attr.fa_dst), strerror(errno));
1435 printf("Added %s rule %s->%s (1/%d)\n",
1436 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1437 opc == LNET_CTL_DROP_ADD ?
1438 attr.u.drop.da_rate : attr.u.delay.la_rate);
1447 jt_ptl_drop_add(int argc, char **argv)
1449 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1453 jt_ptl_delay_add(int argc, char **argv)
1455 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1459 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1461 struct libcfs_ioctl_data data = {{0}};
1462 struct lnet_fault_attr attr;
1466 static struct option opts[] = {
1467 {"source", required_argument, 0, 's'},
1468 {"dest", required_argument, 0, 'd'},
1469 {"all", no_argument, 0, 'a'},
1474 fprintf(stderr, "Failed, please provide source and "
1475 "destination of rule\n");
1479 memset(&attr, 0, sizeof(attr));
1481 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1488 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1493 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1498 attr.fa_src = attr.fa_dst = 0;
1502 fprintf(stderr, "error: %s: option '%s' "
1503 "unrecognized\n", argv[0], argv[optind - 1]);
1509 data.ioc_flags = opc;
1510 data.ioc_inllen1 = sizeof(attr);
1511 data.ioc_inlbuf1 = (char *)&attr;
1512 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1513 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1517 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1519 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1520 all ? "all" : libcfs_nid2str(attr.fa_src),
1521 all ? "all" : libcfs_nid2str(attr.fa_dst),
1526 libcfs_ioctl_unpack(&data, ioc_buf);
1527 printf("Removed %d %s rules\n", data.ioc_count, name);
1536 jt_ptl_drop_del(int argc, char **argv)
1538 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1542 jt_ptl_delay_del(int argc, char **argv)
1544 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1548 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1550 struct libcfs_ioctl_data data = {{0}};
1553 LIBCFS_IOC_INIT(data);
1554 data.ioc_flags = opc;
1556 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1558 fprintf(stderr, "failed to reset %s stats: %s\n",
1559 name, strerror(errno));
1566 jt_ptl_drop_reset(int argc, char **argv)
1568 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1572 jt_ptl_delay_reset(int argc, char **argv)
1574 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1579 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1581 struct libcfs_ioctl_data data = {{0}};
1582 struct lnet_fault_attr attr;
1583 struct lnet_fault_stat stat;
1586 printf("LNet %s rules:\n", name);
1587 for (pos = 0;; pos++) {
1590 memset(&attr, 0, sizeof(attr));
1591 memset(&stat, 0, sizeof(stat));
1593 data.ioc_count = pos;
1594 data.ioc_flags = opc;
1595 data.ioc_inllen1 = sizeof(attr);
1596 data.ioc_inlbuf1 = (char *)&attr;
1597 data.ioc_inllen2 = sizeof(stat);
1598 data.ioc_inlbuf2 = (char *)&stat;
1599 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1600 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1604 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1608 libcfs_ioctl_unpack(&data, ioc_buf);
1610 if (opc == LNET_CTL_DROP_LIST) {
1611 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1612 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1613 LPU64", REP "LPU64"\n",
1614 libcfs_nid2str(attr.fa_src),
1615 libcfs_nid2str(attr.fa_dst),
1616 attr.u.drop.da_rate, attr.u.drop.da_interval,
1617 attr.fa_ptl_mask, attr.fa_msg_mask,
1618 stat.u.drop.ds_dropped, stat.fs_count,
1619 stat.fs_put, stat.fs_ack,
1620 stat.fs_get, stat.fs_reply);
1622 } else if (opc == LNET_CTL_DELAY_LIST) {
1623 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1624 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1625 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1626 libcfs_nid2str(attr.fa_src),
1627 libcfs_nid2str(attr.fa_dst),
1628 attr.u.delay.la_rate, attr.u.delay.la_interval,
1629 attr.u.delay.la_latency,
1630 attr.fa_ptl_mask, attr.fa_msg_mask,
1631 stat.u.delay.ls_delayed, stat.fs_count,
1632 stat.fs_put, stat.fs_ack, stat.fs_get,
1636 printf("found total %d\n", pos);
1642 jt_ptl_drop_list(int argc, char **argv)
1644 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1648 jt_ptl_delay_list(int argc, char **argv)
1650 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1654 get_cycles_per_usec ()
1656 FILE *f = fopen ("/proc/cpuinfo", "r");
1661 while (fgets (line, sizeof (line), f) != NULL)
1662 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1669 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1673 int jt_ptl_memhog(int argc, char **argv)
1675 static int gfp = 0; /* sticky! */
1677 struct libcfs_ioctl_data data;
1683 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1687 count = strtol(argv[1], &end, 0);
1688 if (count < 0 || *end != 0) {
1689 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1694 rc = strtol(argv[2], &end, 0);
1696 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1702 LIBCFS_IOC_INIT(data);
1703 data.ioc_count = count;
1704 data.ioc_flags = gfp;
1705 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1708 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1712 printf("memhog %d OK\n", count);
1716 int jt_ptl_testprotocompat(int argc, char **argv)
1718 struct libcfs_ioctl_data data;
1724 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1728 flags = strtol(argv[1], &end, 0);
1729 if (flags < 0 || *end != 0) {
1730 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1734 LIBCFS_IOC_INIT(data);
1735 data.ioc_flags = flags;
1736 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1739 fprintf(stderr, "test proto compat %x failed: %s\n",
1740 flags, strerror(errno));
1744 printf("test proto compat %x OK\n", flags);