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 Portals, http://www.sf.net/projects/lustre/
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.
23 #include <libcfs/libcfsutil.h>
24 #include <lnet/lnetctl.h>
25 #include <lnet/socklnd.h>
26 #include <lnet/lnet.h>
30 unsigned int libcfs_debug;
31 unsigned int libcfs_printk = D_CANTMASK;
36 #define IOC_BUF_SIZE 8192
37 static char local_buf[IOC_BUF_SIZE];
38 static char *ioc_buf = local_buf;
40 /* Convert a string boolean to an int; "enable" -> 1 */
42 lnet_parse_bool (int *b, char *str)
44 if (!strcasecmp (str, "no") ||
45 !strcasecmp (str, "n") ||
46 !strcasecmp (str, "off") ||
47 !strcasecmp (str, "down") ||
48 !strcasecmp (str, "disable"))
54 if (!strcasecmp (str, "yes") ||
55 !strcasecmp (str, "y") ||
56 !strcasecmp (str, "on") ||
57 !strcasecmp (str, "up") ||
58 !strcasecmp (str, "enable"))
68 lnet_parse_port (int *port, char *str)
72 *port = strtol (str, &end, 0);
74 if (*end == 0 && /* parsed whole string */
75 *port > 0 && *port < 65536) /* minimal sanity check */
81 #ifdef HAVE_GETHOSTBYNAME
82 static struct hostent *
83 ptl_gethostbyname(char * hname) {
85 he = gethostbyname(hname);
90 fprintf(stderr, "Unable to resolve hostname: %s\n",
94 fprintf(stderr, "gethostbyname error for %s: %s\n",
95 hname, strerror(h_errno));
105 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
112 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
113 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
114 (c & ~0xff) == 0 && (d & ~0xff) == 0)
116 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
124 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
126 #ifdef HAVE_GETHOSTBYNAME
130 if (!strcmp (str, "_all_")) {
135 if (lnet_parse_ipquad(ipaddrp, str) == 0)
138 #ifdef HAVE_GETHOSTBYNAME
139 if ((('a' <= str[0] && str[0] <= 'z') ||
140 ('A' <= str[0] && str[0] <= 'Z')) &&
141 (he = ptl_gethostbyname (str)) != NULL) {
142 __u32 addr = *(__u32 *)he->h_addr;
144 *ipaddrp = ntohl(addr); /* HOST byte order */
153 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
155 #ifdef HAVE_GETHOSTBYNAME
160 net_ip = htonl (ipaddr);
161 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
163 strlcpy(str, he->h_name, strsize);
169 sprintf (str, "%d.%d.%d.%d",
170 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
171 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
176 lnet_parse_time (time_t *t, char *str)
182 *t = strtol (str, &end, 0);
183 if (*end == 0) /* parsed whole string */
186 memset (&tm, 0, sizeof (tm));
187 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
188 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
189 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
193 tm.tm_mon--; /* convert to 0 == Jan */
194 tm.tm_year -= 1900; /* y2k quirk */
195 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
198 if (*t == (time_t)-1)
205 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
207 id_ptr->pid = LNET_PID_ANY;
208 id_ptr->nid = libcfs_str2nid(nid_str);
209 if (id_ptr->nid == LNET_NID_ANY) {
210 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
217 int g_net_is_set (char *cmd)
224 "You must run the 'network' command before '%s'.\n",
229 int g_net_is_compatible (char *cmd, ...)
234 if (!g_net_is_set(cmd))
240 nal = va_arg (ap, int);
241 if (nal == LNET_NETTYP(g_net)) {
251 "Command %s not compatible with %s NAL\n",
253 libcfs_lnd2str(LNET_NETTYP(g_net)));
257 int ptl_initialize(int argc, char **argv)
259 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
260 LNET_DEV_MAJOR, LNET_DEV_MINOR);
265 int jt_ptl_network(int argc, char **argv)
267 struct libcfs_ioctl_data data;
268 __u32 net = LNET_NIDNET(LNET_NID_ANY);
272 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
276 if (!strcmp(argv[1], "unconfigure") ||
277 !strcmp(argv[1], "down")) {
278 LIBCFS_IOC_INIT(data);
279 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
282 printf ("LNET ready to unload\n");
287 fprintf(stderr, "LNET busy\n");
289 fprintf(stderr, "LNET unconfigure error %d: %s\n",
290 errno, strerror(errno));
294 if (!strcmp(argv[1], "configure") ||
295 !strcmp(argv[1], "up")) {
296 LIBCFS_IOC_INIT(data);
297 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
300 printf ("LNET configured\n");
304 fprintf(stderr, "LNET configure error %d: %s\n",
305 errno, strerror(errno));
309 net = libcfs_str2net(argv[1]);
310 if (net == LNET_NIDNET(LNET_NID_ANY)) {
311 fprintf(stderr, "Can't parse net %s\n", argv[1]);
315 if (LNET_NETTYP(net) == CIBLND ||
316 LNET_NETTYP(net) == OPENIBLND ||
317 LNET_NETTYP(net) == IIBLND ||
318 LNET_NETTYP(net) == VIBLND) {
319 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
329 jt_ptl_list_nids(int argc, char **argv)
331 struct libcfs_ioctl_data data;
332 int all = 0, return_nid = 0;
336 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
337 /* Hack to pass back value */
338 return_nid = (argc == 2) && (argv[1][0] == 1);
340 if ((argc > 2) && !(all || return_nid)) {
341 fprintf(stderr, "usage: %s [all]\n", argv[0]);
345 for (count = 0;; count++) {
346 LIBCFS_IOC_INIT (data);
347 data.ioc_count = count;
348 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
351 if ((count > 0) && (errno == ENOENT))
352 /* We found them all */
354 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
355 errno, strerror(errno));
359 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
360 printf("%s\n", libcfs_nid2str(data.ioc_nid));
362 *(__u64 *)(argv[1]) = data.ioc_nid;
372 jt_ptl_which_nid (int argc, char **argv)
374 struct libcfs_ioctl_data data;
377 lnet_nid_t best_nid = LNET_NID_ANY;
386 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
390 for (i = 1; i < argc; i++) {
392 nid = libcfs_str2nid(nidstr);
393 if (nid == LNET_NID_ANY) {
394 fprintf(stderr, "Can't parse NID %s\n", nidstr);
398 LIBCFS_IOC_INIT(data);
401 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
403 fprintf(stderr, "Can't get distance to %s: %s\n",
404 nidstr, strerror(errno));
408 dist = data.ioc_u32[0];
409 order = data.ioc_u32[1];
412 if (dist == -EHOSTUNREACH)
415 fprintf(stderr, "Unexpected distance to %s: %d\n",
420 if (best_nid == LNET_NID_ANY ||
422 (dist == best_dist && order < best_order)) {
429 if (best_nid == LNET_NID_ANY) {
430 fprintf(stderr, "No reachable NID\n");
434 printf("%s\n", libcfs_nid2str(best_nid));
439 jt_ptl_print_interfaces (int argc, char **argv)
441 struct libcfs_ioctl_data data;
442 char buffer[3][HOST_NAME_MAX + 1];
446 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
449 for (index = 0;;index++) {
450 LIBCFS_IOC_INIT(data);
451 data.ioc_net = g_net;
452 data.ioc_count = index;
454 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
458 printf ("%s: (%s/%s) npeer %d nroute %d\n",
459 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
460 sizeof(buffer[2]), 1),
461 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
462 sizeof(buffer[0]), 0),
463 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
464 sizeof(buffer[1]), 0),
465 data.ioc_u32[2], data.ioc_u32[3]);
469 if (errno == ENOENT) {
470 printf ("<no interfaces>\n");
472 fprintf(stderr, "Error getting interfaces: %s: "
482 jt_ptl_add_interface (int argc, char **argv)
484 struct libcfs_ioctl_data data;
487 __u32 netmask = 0xffffff00;
492 if (argc < 2 || argc > 3) {
493 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
497 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
500 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
501 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
506 count = strtol(argv[2], &end, 0);
507 if (count > 0 && count < 32 && *end == 0) {
509 for (i = count; i > 0; i--)
510 netmask = netmask|(1<<(32-i));
511 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
512 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
517 LIBCFS_IOC_INIT(data);
518 data.ioc_net = g_net;
519 data.ioc_u32[0] = ipaddr;
520 data.ioc_u32[1] = netmask;
522 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
524 fprintf (stderr, "failed to add interface: %s\n",
533 jt_ptl_del_interface (int argc, char **argv)
535 struct libcfs_ioctl_data data;
540 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
544 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
548 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
549 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
553 LIBCFS_IOC_INIT(data);
554 data.ioc_net = g_net;
555 data.ioc_u32[0] = ipaddr;
557 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
559 fprintf (stderr, "failed to delete interface: %s\n",
568 jt_ptl_print_peers (int argc, char **argv)
570 struct libcfs_ioctl_data data;
571 lnet_process_id_t id;
572 char buffer[2][HOST_NAME_MAX + 1];
576 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
579 for (index = 0;;index++) {
580 LIBCFS_IOC_INIT(data);
581 data.ioc_net = g_net;
582 data.ioc_count = index;
584 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
588 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
589 id.nid = data.ioc_nid;
590 id.pid = data.ioc_u32[4];
591 printf ("%-20s [%d]%s->%s:%d #%d\n",
593 data.ioc_count, /* persistence */
595 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
596 sizeof(buffer[0]), 1),
598 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
599 sizeof(buffer[1]), 1),
600 data.ioc_u32[1], /* peer port */
601 data.ioc_u32[3]); /* conn_count */
602 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
603 int disconn = data.ioc_flags >> 16;
609 state = data.ioc_flags & 0xffff ? "C" : "U";
611 printf ("%-20s (%d) %s [%d] "LPU64" "
612 "sq %d/%d tx %d/%d/%d\n",
613 libcfs_nid2str(data.ioc_nid), /* peer nid */
614 data.ioc_net, /* gemini device id */
615 state, /* peer is Connecting, Up, or Down */
616 data.ioc_count, /* peer refcount */
617 data.ioc_u64[0], /* peerstamp */
618 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
619 /* fmaq, nfma, nrdma */
620 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
623 printf ("%-20s [%d]\n",
624 libcfs_nid2str(data.ioc_nid), data.ioc_count);
629 if (errno == ENOENT) {
630 printf ("<no peers>\n");
632 fprintf(stderr, "Error getting peer list: %s: "
641 jt_ptl_add_peer (int argc, char **argv)
643 struct libcfs_ioctl_data data;
649 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
653 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
658 nid = libcfs_str2nid(argv[1]);
659 if (nid == LNET_NID_ANY) {
660 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
664 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
665 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
669 if (lnet_parse_port (&port, argv[3]) != 0) {
670 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
674 LIBCFS_IOC_INIT(data);
675 data.ioc_net = g_net;
677 data.ioc_u32[0] = ip;
678 data.ioc_u32[1] = port;
680 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
682 fprintf (stderr, "failed to add peer: %s\n",
691 jt_ptl_del_peer (int argc, char **argv)
693 struct libcfs_ioctl_data data;
694 lnet_nid_t nid = LNET_NID_ANY;
695 lnet_pid_t pid = LNET_PID_ANY;
699 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
702 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
704 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
708 } else if (argc > 2) {
709 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
714 !libcfs_str2anynid(&nid, argv[1])) {
715 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
719 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
721 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
722 fprintf (stderr, "Can't parse ip addr: %s\n",
728 LIBCFS_IOC_INIT(data);
729 data.ioc_net = g_net;
731 data.ioc_u32[0] = ip;
732 data.ioc_u32[1] = pid;
734 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
736 fprintf (stderr, "failed to remove peer: %s\n",
745 jt_ptl_print_connections (int argc, char **argv)
747 struct libcfs_ioctl_data data;
748 lnet_process_id_t id;
749 char buffer[2][HOST_NAME_MAX + 1];
753 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
756 for (index = 0; ; index++) {
757 LIBCFS_IOC_INIT(data);
758 data.ioc_net = g_net;
759 data.ioc_count = index;
761 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
765 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
766 id.nid = data.ioc_nid;
767 id.pid = data.ioc_u32[6];
768 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
770 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
771 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
772 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
773 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
774 data.ioc_u32[4], /* scheduler */
776 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
777 sizeof(buffer[0]), 1),
779 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
780 sizeof(buffer[1]), 1),
781 data.ioc_u32[1], /* remote port */
782 data.ioc_count, /* tx buffer size */
783 data.ioc_u32[5], /* rx buffer size */
784 data.ioc_flags ? "nagle" : "nonagle");
785 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
786 printf ("%s mtu %d\n",
787 libcfs_nid2str(data.ioc_nid),
788 data.ioc_u32[0]); /* path MTU */
789 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
790 printf ("%-20s [%d]\n",
791 libcfs_nid2str(data.ioc_nid),
792 data.ioc_u32[0] /* device id */);
794 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
799 if (errno == ENOENT) {
800 printf ("<no connections>\n");
802 fprintf(stderr, "Error getting connection list: %s: "
810 int jt_ptl_disconnect(int argc, char **argv)
812 struct libcfs_ioctl_data data;
813 lnet_nid_t nid = LNET_NID_ANY;
818 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
822 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
826 !libcfs_str2anynid(&nid, argv[1])) {
827 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
831 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
833 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
834 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
838 LIBCFS_IOC_INIT(data);
839 data.ioc_net = g_net;
841 data.ioc_u32[0] = ipaddr;
843 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
845 fprintf(stderr, "failed to remove connection: %s\n",
853 int jt_ptl_push_connection (int argc, char **argv)
855 struct libcfs_ioctl_data data;
857 lnet_nid_t nid = LNET_NID_ANY;
860 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
864 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
868 !libcfs_str2anynid(&nid, argv[1])) {
869 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
873 LIBCFS_IOC_INIT(data);
874 data.ioc_net = g_net;
877 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
879 fprintf(stderr, "failed to push connection: %s\n",
887 int jt_ptl_ping(int argc, char **argv)
891 lnet_process_id_t id;
892 lnet_process_id_t ids[16];
893 int maxids = sizeof(ids)/sizeof(ids[0]);
894 struct libcfs_ioctl_data data;
899 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
903 sep = strchr(argv[1], '-');
905 rc = lnet_parse_nid(argv[1], &id);
911 if (argv[1][0] == 'u' ||
913 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
915 id.pid = strtoul(argv[1], &end, 0);
917 if (end != sep) { /* assuming '-' is part of hostname */
918 rc = lnet_parse_nid(argv[1], &id);
922 id.nid = libcfs_str2nid(sep + 1);
924 if (id.nid == LNET_NID_ANY) {
926 "Can't parse process id \"%s\"\n",
934 timeout = 1000 * atol(argv[2]);
936 timeout = 1000; /* default 1 second timeout */
938 LIBCFS_IOC_INIT (data);
939 data.ioc_nid = id.nid;
940 data.ioc_u32[0] = id.pid;
941 data.ioc_u32[1] = timeout;
942 data.ioc_plen1 = sizeof(ids);
943 data.ioc_pbuf1 = (char *)ids;
945 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
947 fprintf(stderr, "failed to ping %s: %s\n",
948 id.pid == LNET_PID_ANY ?
949 libcfs_nid2str(id.nid) : libcfs_id2str(id),
954 for (i = 0; i < data.ioc_count && i < maxids; i++)
955 printf("%s\n", libcfs_id2str(ids[i]));
957 if (data.ioc_count > maxids)
958 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
963 int jt_ptl_mynid(int argc, char **argv)
965 struct libcfs_ioctl_data data;
970 fprintf(stderr, "usage: %s NID\n", argv[0]);
974 nid = libcfs_str2nid(argv[1]);
975 if (nid == LNET_NID_ANY) {
976 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
980 LIBCFS_IOC_INIT(data);
981 data.ioc_net = LNET_NIDNET(nid);
984 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
986 fprintf(stderr, "setting my NID failed: %s\n",
989 printf("registered my nid %s\n", libcfs_nid2str(nid));
995 jt_ptl_fail_nid (int argc, char **argv)
1000 struct libcfs_ioctl_data data;
1002 if (argc < 2 || argc > 3)
1004 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1008 if (!libcfs_str2anynid(&nid, argv[1]))
1010 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1015 threshold = LNET_MD_THRESH_INF;
1016 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1017 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1021 LIBCFS_IOC_INIT (data);
1023 data.ioc_count = threshold;
1025 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1027 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1030 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1036 jt_ptl_add_route (int argc, char **argv)
1038 struct lnet_ioctl_config_data data;
1039 lnet_nid_t gateway_nid;
1040 unsigned int hops = 1;
1041 unsigned int priority = 0;
1045 if (argc < 2 || argc > 4) {
1046 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1051 if (g_net_is_set(argv[0]) == 0)
1054 gateway_nid = libcfs_str2nid(argv[1]);
1055 if (gateway_nid == LNET_NID_ANY) {
1056 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1061 hops = strtoul(argv[2], &end, 0);
1062 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1063 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1068 priority = strtoul(argv[3], &end, 0);
1069 if (end != NULL && *end != 0) {
1071 "Can't parse priority \"%s\"\n",
1078 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1079 data.cfg_net = g_net;
1080 data.cfg_config_u.cfg_route.rtr_hop = hops;
1081 data.cfg_nid = gateway_nid;
1082 data.cfg_config_u.cfg_route.rtr_priority = priority;
1084 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1086 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1095 jt_ptl_del_route (int argc, char **argv)
1097 struct lnet_ioctl_config_data data;
1102 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1106 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1107 fprintf(stderr, "Can't parse gateway NID "
1108 "\"%s\"\n", argv[1]);
1112 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1113 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1116 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1118 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1119 libcfs_nid2str(nid), strerror(errno));
1127 jt_ptl_notify_router (int argc, char **argv)
1129 struct libcfs_ioctl_data data;
1138 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1143 nid = libcfs_str2nid(argv[1]);
1144 if (nid == LNET_NID_ANY) {
1145 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1149 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1150 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1154 gettimeofday(&now, NULL);
1158 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1159 fprintf(stderr, "Can't parse time %s\n"
1160 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1161 "or an absolute unix time in seconds\n", argv[3]);
1163 } else if (when > now.tv_sec) {
1164 fprintf (stderr, "%s specifies a time in the future\n",
1169 LIBCFS_IOC_INIT(data);
1171 data.ioc_flags = enable;
1172 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1173 data.ioc_u64[0] = (__u64)when;
1175 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1177 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1178 libcfs_nid2str(nid), strerror (errno));
1186 jt_ptl_print_routes (int argc, char **argv)
1188 struct lnet_ioctl_config_data data;
1197 for (index = 0; ; index++) {
1198 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1199 data.cfg_count = index;
1201 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1206 hops = data.cfg_config_u.cfg_route.rtr_hop;
1208 alive = data.cfg_config_u.cfg_route.rtr_flags;
1209 pri = data.cfg_config_u.cfg_route.rtr_priority;
1211 printf("net %18s hops %u gw %32s %s pri %u\n",
1212 libcfs_net2str(net), hops,
1213 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1216 if (errno != ENOENT)
1217 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1224 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1230 /* NB: can't support range ipaddress except * and *@net */
1231 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1232 net = libcfs_str2net(str + 2);
1233 if (net == LNET_NIDNET(LNET_NID_ANY))
1236 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1238 rc = libcfs_str2anynid(&nid, str);
1246 fprintf(stderr, "Invalid NID : %s\n", str);
1251 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1253 if (!strcasecmp(msg_str, "put")) {
1254 *mask_p |= LNET_PUT_BIT;
1257 } else if (!strcasecmp(msg_str, "ack")) {
1258 *mask_p |= LNET_ACK_BIT;
1261 } else if (!strcasecmp(msg_str, "get")) {
1262 *mask_p |= LNET_GET_BIT;
1265 } else if (!strcasecmp(msg_str, "reply")) {
1266 *mask_p |= LNET_REPLY_BIT;
1270 fprintf(stderr, "unknown message type %s\n", msg_str);
1275 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1277 unsigned long rc = strtoul(optarg, NULL, 0);
1280 fprintf(stderr, "invalid portal: %lu\n", rc);
1284 *mask_p |= (1ULL << rc);
1289 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1291 struct libcfs_ioctl_data data = {{0}};
1292 struct lnet_fault_attr attr;
1296 static struct option opts[] = {
1297 {"source", required_argument, 0, 's'},
1298 {"dest", required_argument, 0, 'd'},
1299 {"rate", required_argument, 0, 'r'},
1300 {"interval", required_argument, 0, 'i'},
1301 {"latency", required_argument, 0, 'l'},
1302 {"portal", required_argument, 0, 'p'},
1303 {"message", required_argument, 0, 'm'},
1308 fprintf(stderr, "Failed, please provide source, destination "
1309 "and rate of rule\n");
1313 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1314 memset(&attr, 0, sizeof(attr));
1316 char c = getopt_long(argc, argv, optstr, opts, NULL);
1322 case 's': /* source NID/NET */
1323 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1328 case 'd': /* dest NID/NET */
1329 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1334 case 'r': /* drop rate */
1335 if (opc == LNET_CTL_DROP_ADD)
1336 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1338 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1341 case 'i': /* time interval (# seconds) for message drop */
1342 if (opc == LNET_CTL_DROP_ADD)
1343 attr.u.drop.da_interval = strtoul(optarg,
1346 attr.u.delay.la_interval = strtoul(optarg,
1350 case 'l': /* seconds to wait before activating rule */
1351 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1354 case 'p': /* portal to filter */
1355 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1360 case 'm': /* message types to filter */
1361 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1367 fprintf(stderr, "error: %s: option '%s' "
1368 "unrecognized\n", argv[0], argv[optind - 1]);
1374 if (opc == LNET_CTL_DROP_ADD) {
1375 /* NB: drop rate and interval are exclusive to each other */
1376 if (!((attr.u.drop.da_rate == 0) ^
1377 (attr.u.drop.da_interval == 0))) {
1379 "please provide either drop rate or interval "
1380 "but not both at the same time.\n");
1383 } else if (opc == LNET_CTL_DELAY_ADD) {
1384 if (!((attr.u.delay.la_rate == 0) ^
1385 (attr.u.delay.la_interval == 0))) {
1387 "please provide either delay rate or interval "
1388 "but not both at the same time.\n");
1392 if (attr.u.delay.la_latency == 0) {
1393 fprintf(stderr, "latency cannot be zero\n");
1398 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1399 fprintf(stderr, "Please provide both source and destination "
1400 "of %s rule\n", name);
1404 data.ioc_flags = opc;
1405 data.ioc_inllen1 = sizeof(attr);
1406 data.ioc_inlbuf1 = (char *)&attr;
1407 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1408 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1412 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1414 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1415 name, libcfs_nid2str(attr.fa_src),
1416 libcfs_nid2str(attr.fa_dst), strerror(errno));
1420 printf("Added %s rule %s->%s (1/%d)\n",
1421 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1422 opc == LNET_CTL_DROP_ADD ?
1423 attr.u.drop.da_rate : attr.u.delay.la_rate);
1432 jt_ptl_drop_add(int argc, char **argv)
1434 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1438 jt_ptl_delay_add(int argc, char **argv)
1440 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1444 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1446 struct libcfs_ioctl_data data = {{0}};
1447 struct lnet_fault_attr attr;
1451 static struct option opts[] = {
1452 {"source", required_argument, 0, 's'},
1453 {"dest", required_argument, 0, 'd'},
1454 {"all", no_argument, 0, 'a'},
1459 fprintf(stderr, "Failed, please provide source and "
1460 "destination of rule\n");
1464 memset(&attr, 0, sizeof(attr));
1466 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1473 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1478 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1483 attr.fa_src = attr.fa_dst = 0;
1487 fprintf(stderr, "error: %s: option '%s' "
1488 "unrecognized\n", argv[0], argv[optind - 1]);
1494 data.ioc_flags = opc;
1495 data.ioc_inllen1 = sizeof(attr);
1496 data.ioc_inlbuf1 = (char *)&attr;
1497 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1498 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1502 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1504 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1505 all ? "all" : libcfs_nid2str(attr.fa_src),
1506 all ? "all" : libcfs_nid2str(attr.fa_dst),
1511 libcfs_ioctl_unpack(&data, ioc_buf);
1512 printf("Removed %d %s rules\n", data.ioc_count, name);
1521 jt_ptl_drop_del(int argc, char **argv)
1523 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1527 jt_ptl_delay_del(int argc, char **argv)
1529 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1533 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1535 struct libcfs_ioctl_data data = {{0}};
1538 LIBCFS_IOC_INIT(data);
1539 data.ioc_flags = opc;
1541 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1543 fprintf(stderr, "failed to reset %s stats: %s\n",
1544 name, strerror(errno));
1551 jt_ptl_drop_reset(int argc, char **argv)
1553 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1557 jt_ptl_delay_reset(int argc, char **argv)
1559 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1564 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1566 struct libcfs_ioctl_data data = {{0}};
1567 struct lnet_fault_attr attr;
1568 struct lnet_fault_stat stat;
1571 printf("LNet %s rules:\n", name);
1572 for (pos = 0;; pos++) {
1575 memset(&attr, 0, sizeof(attr));
1576 memset(&stat, 0, sizeof(stat));
1578 data.ioc_count = pos;
1579 data.ioc_flags = opc;
1580 data.ioc_inllen1 = sizeof(attr);
1581 data.ioc_inlbuf1 = (char *)&attr;
1582 data.ioc_inllen2 = sizeof(stat);
1583 data.ioc_inlbuf2 = (char *)&stat;
1584 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1585 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1589 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1593 libcfs_ioctl_unpack(&data, ioc_buf);
1595 if (opc == LNET_CTL_DROP_LIST) {
1596 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1597 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1598 LPU64", REP "LPU64"\n",
1599 libcfs_nid2str(attr.fa_src),
1600 libcfs_nid2str(attr.fa_dst),
1601 attr.u.drop.da_rate, attr.u.drop.da_interval,
1602 attr.fa_ptl_mask, attr.fa_msg_mask,
1603 stat.u.drop.ds_dropped, stat.fs_count,
1604 stat.fs_put, stat.fs_ack,
1605 stat.fs_get, stat.fs_reply);
1607 } else if (opc == LNET_CTL_DELAY_LIST) {
1608 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1609 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1610 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1611 libcfs_nid2str(attr.fa_src),
1612 libcfs_nid2str(attr.fa_dst),
1613 attr.u.delay.la_rate, attr.u.delay.la_interval,
1614 attr.u.delay.la_latency,
1615 attr.fa_ptl_mask, attr.fa_msg_mask,
1616 stat.u.delay.ls_delayed, stat.fs_count,
1617 stat.fs_put, stat.fs_ack, stat.fs_get,
1621 printf("found total %d\n", pos);
1627 jt_ptl_drop_list(int argc, char **argv)
1629 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1633 jt_ptl_delay_list(int argc, char **argv)
1635 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1639 get_cycles_per_usec ()
1641 FILE *f = fopen ("/proc/cpuinfo", "r");
1646 while (fgets (line, sizeof (line), f) != NULL)
1647 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1654 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1658 int jt_ptl_memhog(int argc, char **argv)
1660 static int gfp = 0; /* sticky! */
1662 struct libcfs_ioctl_data data;
1668 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1672 count = strtol(argv[1], &end, 0);
1673 if (count < 0 || *end != 0) {
1674 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1679 rc = strtol(argv[2], &end, 0);
1681 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1687 LIBCFS_IOC_INIT(data);
1688 data.ioc_count = count;
1689 data.ioc_flags = gfp;
1690 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1693 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1697 printf("memhog %d OK\n", count);
1701 int jt_ptl_testprotocompat(int argc, char **argv)
1703 struct libcfs_ioctl_data data;
1709 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1713 flags = strtol(argv[1], &end, 0);
1714 if (flags < 0 || *end != 0) {
1715 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1719 LIBCFS_IOC_INIT(data);
1720 data.ioc_flags = flags;
1721 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1724 fprintf(stderr, "test proto compat %x failed: %s\n",
1725 flags, strerror(errno));
1729 printf("test proto compat %x OK\n", flags);