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.
30 #include <sys/ioctl.h>
33 #include <linux/types.h>
34 #include <libcfs/libcfs.h>
35 #include <libcfs/util/string.h>
36 #include <libcfs/util/ioctl.h>
37 #include <lnet/lnetctl.h>
38 #include <lnet/socklnd.h>
39 #include <lnet/lnet.h>
41 unsigned int libcfs_debug;
42 unsigned int libcfs_printk = D_CANTMASK;
47 #define IOC_BUF_SIZE 8192
48 static char local_buf[IOC_BUF_SIZE];
49 static char *ioc_buf = local_buf;
51 /* Convert a string boolean to an int; "enable" -> 1 */
53 lnet_parse_bool (int *b, char *str)
55 if (!strcasecmp (str, "no") ||
56 !strcasecmp (str, "n") ||
57 !strcasecmp (str, "off") ||
58 !strcasecmp (str, "down") ||
59 !strcasecmp (str, "disable"))
65 if (!strcasecmp (str, "yes") ||
66 !strcasecmp (str, "y") ||
67 !strcasecmp (str, "on") ||
68 !strcasecmp (str, "up") ||
69 !strcasecmp (str, "enable"))
79 lnet_parse_port (int *port, char *str)
83 *port = strtol (str, &end, 0);
85 if (*end == 0 && /* parsed whole string */
86 *port > 0 && *port < 65536) /* minimal sanity check */
92 #ifdef HAVE_GETHOSTBYNAME
93 static struct hostent *
94 ptl_gethostbyname(char * hname) {
96 he = gethostbyname(hname);
101 fprintf(stderr, "Unable to resolve hostname: %s\n",
105 fprintf(stderr, "gethostbyname error for %s: %s\n",
106 hname, strerror(h_errno));
116 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
123 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
124 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
125 (c & ~0xff) == 0 && (d & ~0xff) == 0)
127 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
135 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
137 #ifdef HAVE_GETHOSTBYNAME
141 if (!strcmp (str, "_all_")) {
146 if (lnet_parse_ipquad(ipaddrp, str) == 0)
149 #ifdef HAVE_GETHOSTBYNAME
150 if ((('a' <= str[0] && str[0] <= 'z') ||
151 ('A' <= str[0] && str[0] <= 'Z')) &&
152 (he = ptl_gethostbyname (str)) != NULL) {
153 __u32 addr = *(__u32 *)he->h_addr;
155 *ipaddrp = ntohl(addr); /* HOST byte order */
164 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
166 #ifdef HAVE_GETHOSTBYNAME
171 net_ip = htonl (ipaddr);
172 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
174 strlcpy(str, he->h_name, strsize);
180 sprintf (str, "%d.%d.%d.%d",
181 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
182 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
187 lnet_parse_time (time_t *t, char *str)
193 *t = strtol (str, &end, 0);
194 if (*end == 0) /* parsed whole string */
197 memset (&tm, 0, sizeof (tm));
198 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
199 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
200 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
204 tm.tm_mon--; /* convert to 0 == Jan */
205 tm.tm_year -= 1900; /* y2k quirk */
206 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
209 if (*t == (time_t)-1)
216 lnet_parse_nid(char *nid_str, lnet_process_id_t *id_ptr)
218 id_ptr->pid = LNET_PID_ANY;
219 id_ptr->nid = libcfs_str2nid(nid_str);
220 if (id_ptr->nid == LNET_NID_ANY) {
221 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
228 int g_net_is_set (char *cmd)
235 "You must run the 'network' command before '%s'.\n",
240 int g_net_is_compatible (char *cmd, ...)
245 if (!g_net_is_set(cmd))
251 nal = va_arg (ap, int);
252 if (nal == LNET_NETTYP(g_net)) {
262 "Command %s not compatible with %s NAL\n",
264 libcfs_lnd2str(LNET_NETTYP(g_net)));
268 int ptl_initialize(int argc, char **argv)
270 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
271 LNET_DEV_MAJOR, LNET_DEV_MINOR);
276 int jt_ptl_network(int argc, char **argv)
278 struct libcfs_ioctl_data data;
279 __u32 net = LNET_NIDNET(LNET_NID_ANY);
283 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
287 if (!strcmp(argv[1], "unconfigure") ||
288 !strcmp(argv[1], "down")) {
289 LIBCFS_IOC_INIT(data);
290 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
293 printf ("LNET ready to unload\n");
298 fprintf(stderr, "LNET busy\n");
300 fprintf(stderr, "LNET unconfigure error %d: %s\n",
301 errno, strerror(errno));
305 if (!strcmp(argv[1], "configure") ||
306 !strcmp(argv[1], "up")) {
307 LIBCFS_IOC_INIT(data);
308 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
311 printf ("LNET configured\n");
315 fprintf(stderr, "LNET configure error %d: %s\n",
316 errno, strerror(errno));
320 net = libcfs_str2net(argv[1]);
321 if (net == LNET_NIDNET(LNET_NID_ANY)) {
322 fprintf(stderr, "Can't parse net %s\n", argv[1]);
326 if (LNET_NETTYP(net) == CIBLND ||
327 LNET_NETTYP(net) == OPENIBLND ||
328 LNET_NETTYP(net) == IIBLND ||
329 LNET_NETTYP(net) == VIBLND) {
330 fprintf(stderr, "Net %s obsoleted\n", libcfs_lnd2str(net));
340 jt_ptl_list_nids(int argc, char **argv)
342 struct libcfs_ioctl_data data;
343 int all = 0, return_nid = 0;
347 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
348 /* Hack to pass back value */
349 return_nid = (argc == 2) && (argv[1][0] == 1);
351 if ((argc > 2) && !(all || return_nid)) {
352 fprintf(stderr, "usage: %s [all]\n", argv[0]);
356 for (count = 0;; count++) {
357 LIBCFS_IOC_INIT (data);
358 data.ioc_count = count;
359 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
362 if ((count > 0) && (errno == ENOENT))
363 /* We found them all */
365 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
366 errno, strerror(errno));
370 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
371 printf("%s\n", libcfs_nid2str(data.ioc_nid));
373 *(__u64 *)(argv[1]) = data.ioc_nid;
383 jt_ptl_which_nid (int argc, char **argv)
385 struct libcfs_ioctl_data data;
388 lnet_nid_t best_nid = LNET_NID_ANY;
397 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
401 for (i = 1; i < argc; i++) {
403 nid = libcfs_str2nid(nidstr);
404 if (nid == LNET_NID_ANY) {
405 fprintf(stderr, "Can't parse NID %s\n", nidstr);
409 LIBCFS_IOC_INIT(data);
412 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
414 fprintf(stderr, "Can't get distance to %s: %s\n",
415 nidstr, strerror(errno));
419 dist = data.ioc_u32[0];
420 order = data.ioc_u32[1];
423 if (dist == -EHOSTUNREACH)
426 fprintf(stderr, "Unexpected distance to %s: %d\n",
431 if (best_nid == LNET_NID_ANY ||
433 (dist == best_dist && order < best_order)) {
440 if (best_nid == LNET_NID_ANY) {
441 fprintf(stderr, "No reachable NID\n");
445 printf("%s\n", libcfs_nid2str(best_nid));
450 jt_ptl_print_interfaces (int argc, char **argv)
452 struct libcfs_ioctl_data data;
453 char buffer[3][HOST_NAME_MAX + 1];
457 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
460 for (index = 0;;index++) {
461 LIBCFS_IOC_INIT(data);
462 data.ioc_net = g_net;
463 data.ioc_count = index;
465 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
469 printf ("%s: (%s/%s) npeer %d nroute %d\n",
470 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
471 sizeof(buffer[2]), 1),
472 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
473 sizeof(buffer[0]), 0),
474 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
475 sizeof(buffer[1]), 0),
476 data.ioc_u32[2], data.ioc_u32[3]);
480 if (errno == ENOENT) {
481 printf ("<no interfaces>\n");
483 fprintf(stderr, "Error getting interfaces: %s: "
493 jt_ptl_add_interface (int argc, char **argv)
495 struct libcfs_ioctl_data data;
498 __u32 netmask = 0xffffff00;
503 if (argc < 2 || argc > 3) {
504 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
508 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
511 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
512 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
517 count = strtol(argv[2], &end, 0);
518 if (count > 0 && count < 32 && *end == 0) {
520 for (i = count; i > 0; i--)
521 netmask = netmask|(1<<(32-i));
522 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
523 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
528 LIBCFS_IOC_INIT(data);
529 data.ioc_net = g_net;
530 data.ioc_u32[0] = ipaddr;
531 data.ioc_u32[1] = netmask;
533 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
535 fprintf (stderr, "failed to add interface: %s\n",
544 jt_ptl_del_interface (int argc, char **argv)
546 struct libcfs_ioctl_data data;
551 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
555 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
559 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
560 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
564 LIBCFS_IOC_INIT(data);
565 data.ioc_net = g_net;
566 data.ioc_u32[0] = ipaddr;
568 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
570 fprintf (stderr, "failed to delete interface: %s\n",
579 jt_ptl_print_peers (int argc, char **argv)
581 struct libcfs_ioctl_data data;
582 lnet_process_id_t id;
583 char buffer[2][HOST_NAME_MAX + 1];
587 if (!g_net_is_compatible (argv[0], SOCKLND, O2IBLND, GNILND, 0))
590 for (index = 0;;index++) {
591 LIBCFS_IOC_INIT(data);
592 data.ioc_net = g_net;
593 data.ioc_count = index;
595 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
599 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
600 id.nid = data.ioc_nid;
601 id.pid = data.ioc_u32[4];
602 printf ("%-20s [%d]%s->%s:%d #%d\n",
604 data.ioc_count, /* persistence */
606 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
607 sizeof(buffer[0]), 1),
609 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
610 sizeof(buffer[1]), 1),
611 data.ioc_u32[1], /* peer port */
612 data.ioc_u32[3]); /* conn_count */
613 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
614 int disconn = data.ioc_flags >> 16;
620 state = data.ioc_flags & 0xffff ? "C" : "U";
622 printf ("%-20s (%d) %s [%d] "LPU64" "
623 "sq %d/%d tx %d/%d/%d\n",
624 libcfs_nid2str(data.ioc_nid), /* peer nid */
625 data.ioc_net, /* gemini device id */
626 state, /* peer is Connecting, Up, or Down */
627 data.ioc_count, /* peer refcount */
628 data.ioc_u64[0], /* peerstamp */
629 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
630 /* fmaq, nfma, nrdma */
631 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]
634 printf ("%-20s [%d]\n",
635 libcfs_nid2str(data.ioc_nid), data.ioc_count);
640 if (errno == ENOENT) {
641 printf ("<no peers>\n");
643 fprintf(stderr, "Error getting peer list: %s: "
652 jt_ptl_add_peer (int argc, char **argv)
654 struct libcfs_ioctl_data data;
660 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
664 fprintf (stderr, "usage(tcp,ra,gni): %s nid ipaddr port\n",
669 nid = libcfs_str2nid(argv[1]);
670 if (nid == LNET_NID_ANY) {
671 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
675 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
676 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
680 if (lnet_parse_port (&port, argv[3]) != 0) {
681 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
685 LIBCFS_IOC_INIT(data);
686 data.ioc_net = g_net;
688 data.ioc_u32[0] = ip;
689 data.ioc_u32[1] = port;
691 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
693 fprintf (stderr, "failed to add peer: %s\n",
702 jt_ptl_del_peer (int argc, char **argv)
704 struct libcfs_ioctl_data data;
705 lnet_nid_t nid = LNET_NID_ANY;
706 lnet_pid_t pid = LNET_PID_ANY;
710 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
713 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
715 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
719 } else if (argc > 2) {
720 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
725 !libcfs_str2anynid(&nid, argv[1])) {
726 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
730 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
732 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
733 fprintf (stderr, "Can't parse ip addr: %s\n",
739 LIBCFS_IOC_INIT(data);
740 data.ioc_net = g_net;
742 data.ioc_u32[0] = ip;
743 data.ioc_u32[1] = pid;
745 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
747 fprintf (stderr, "failed to remove peer: %s\n",
756 jt_ptl_print_connections (int argc, char **argv)
758 struct libcfs_ioctl_data data;
759 lnet_process_id_t id;
760 char buffer[2][HOST_NAME_MAX + 1];
764 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
767 for (index = 0; ; index++) {
768 LIBCFS_IOC_INIT(data);
769 data.ioc_net = g_net;
770 data.ioc_count = index;
772 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
776 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
777 id.nid = data.ioc_nid;
778 id.pid = data.ioc_u32[6];
779 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
781 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
782 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
783 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
784 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
785 data.ioc_u32[4], /* scheduler */
787 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
788 sizeof(buffer[0]), 1),
790 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
791 sizeof(buffer[1]), 1),
792 data.ioc_u32[1], /* remote port */
793 data.ioc_count, /* tx buffer size */
794 data.ioc_u32[5], /* rx buffer size */
795 data.ioc_flags ? "nagle" : "nonagle");
796 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
797 printf ("%s mtu %d\n",
798 libcfs_nid2str(data.ioc_nid),
799 data.ioc_u32[0]); /* path MTU */
800 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
801 printf ("%-20s [%d]\n",
802 libcfs_nid2str(data.ioc_nid),
803 data.ioc_u32[0] /* device id */);
805 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
810 if (errno == ENOENT) {
811 printf ("<no connections>\n");
813 fprintf(stderr, "Error getting connection list: %s: "
821 int jt_ptl_disconnect(int argc, char **argv)
823 struct libcfs_ioctl_data data;
824 lnet_nid_t nid = LNET_NID_ANY;
829 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
833 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
837 !libcfs_str2anynid(&nid, argv[1])) {
838 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
842 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
844 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
845 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
849 LIBCFS_IOC_INIT(data);
850 data.ioc_net = g_net;
852 data.ioc_u32[0] = ipaddr;
854 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
856 fprintf(stderr, "failed to remove connection: %s\n",
864 int jt_ptl_push_connection (int argc, char **argv)
866 struct libcfs_ioctl_data data;
868 lnet_nid_t nid = LNET_NID_ANY;
871 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
875 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
879 !libcfs_str2anynid(&nid, argv[1])) {
880 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
884 LIBCFS_IOC_INIT(data);
885 data.ioc_net = g_net;
888 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
890 fprintf(stderr, "failed to push connection: %s\n",
898 int jt_ptl_ping(int argc, char **argv)
902 lnet_process_id_t id;
903 lnet_process_id_t ids[16];
904 int maxids = sizeof(ids)/sizeof(ids[0]);
905 struct libcfs_ioctl_data data;
910 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
914 sep = strchr(argv[1], '-');
916 rc = lnet_parse_nid(argv[1], &id);
922 if (argv[1][0] == 'u' ||
924 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
926 id.pid = strtoul(argv[1], &end, 0);
928 if (end != sep) { /* assuming '-' is part of hostname */
929 rc = lnet_parse_nid(argv[1], &id);
933 id.nid = libcfs_str2nid(sep + 1);
935 if (id.nid == LNET_NID_ANY) {
937 "Can't parse process id \"%s\"\n",
945 timeout = 1000 * atol(argv[2]);
947 timeout = 1000; /* default 1 second timeout */
949 LIBCFS_IOC_INIT (data);
950 data.ioc_nid = id.nid;
951 data.ioc_u32[0] = id.pid;
952 data.ioc_u32[1] = timeout;
953 data.ioc_plen1 = sizeof(ids);
954 data.ioc_pbuf1 = (char *)ids;
956 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
958 fprintf(stderr, "failed to ping %s: %s\n",
959 id.pid == LNET_PID_ANY ?
960 libcfs_nid2str(id.nid) : libcfs_id2str(id),
965 for (i = 0; i < data.ioc_count && i < maxids; i++)
966 printf("%s\n", libcfs_id2str(ids[i]));
968 if (data.ioc_count > maxids)
969 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
974 int jt_ptl_mynid(int argc, char **argv)
976 struct libcfs_ioctl_data data;
981 fprintf(stderr, "usage: %s NID\n", argv[0]);
985 nid = libcfs_str2nid(argv[1]);
986 if (nid == LNET_NID_ANY) {
987 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
991 LIBCFS_IOC_INIT(data);
992 data.ioc_net = LNET_NIDNET(nid);
995 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
997 fprintf(stderr, "setting my NID failed: %s\n",
1000 printf("registered my nid %s\n", libcfs_nid2str(nid));
1006 jt_ptl_fail_nid (int argc, char **argv)
1011 struct libcfs_ioctl_data data;
1013 if (argc < 2 || argc > 3)
1015 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1019 if (!libcfs_str2anynid(&nid, argv[1]))
1021 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1026 threshold = LNET_MD_THRESH_INF;
1027 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1028 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1032 LIBCFS_IOC_INIT (data);
1034 data.ioc_count = threshold;
1036 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1038 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1041 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1047 jt_ptl_add_route (int argc, char **argv)
1049 struct lnet_ioctl_config_data data;
1050 lnet_nid_t gateway_nid;
1051 unsigned int hops = 1;
1052 unsigned int priority = 0;
1056 if (argc < 2 || argc > 4) {
1057 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1062 if (g_net_is_set(argv[0]) == 0)
1065 gateway_nid = libcfs_str2nid(argv[1]);
1066 if (gateway_nid == LNET_NID_ANY) {
1067 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1072 hops = strtoul(argv[2], &end, 0);
1073 if (hops == 0 || hops >= 256 || (end != NULL && *end != 0)) {
1074 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1079 priority = strtoul(argv[3], &end, 0);
1080 if (end != NULL && *end != 0) {
1082 "Can't parse priority \"%s\"\n",
1089 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1090 data.cfg_net = g_net;
1091 data.cfg_config_u.cfg_route.rtr_hop = hops;
1092 data.cfg_nid = gateway_nid;
1093 data.cfg_config_u.cfg_route.rtr_priority = priority;
1095 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1097 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1106 jt_ptl_del_route (int argc, char **argv)
1108 struct lnet_ioctl_config_data data;
1113 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1117 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1118 fprintf(stderr, "Can't parse gateway NID "
1119 "\"%s\"\n", argv[1]);
1123 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1124 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1127 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1129 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1130 libcfs_nid2str(nid), strerror(errno));
1138 jt_ptl_notify_router (int argc, char **argv)
1140 struct libcfs_ioctl_data data;
1149 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1154 nid = libcfs_str2nid(argv[1]);
1155 if (nid == LNET_NID_ANY) {
1156 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1160 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1161 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1165 gettimeofday(&now, NULL);
1169 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1170 fprintf(stderr, "Can't parse time %s\n"
1171 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1172 "or an absolute unix time in seconds\n", argv[3]);
1174 } else if (when > now.tv_sec) {
1175 fprintf (stderr, "%s specifies a time in the future\n",
1180 LIBCFS_IOC_INIT(data);
1182 data.ioc_flags = enable;
1183 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1184 data.ioc_u64[0] = (__u64)when;
1186 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1188 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1189 libcfs_nid2str(nid), strerror (errno));
1197 jt_ptl_print_routes (int argc, char **argv)
1199 struct lnet_ioctl_config_data data;
1208 for (index = 0; ; index++) {
1209 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1210 data.cfg_count = index;
1212 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1217 hops = data.cfg_config_u.cfg_route.rtr_hop;
1219 alive = data.cfg_config_u.cfg_route.rtr_flags;
1220 pri = data.cfg_config_u.cfg_route.rtr_priority;
1222 printf("net %18s hops %u gw %32s %s pri %u\n",
1223 libcfs_net2str(net), hops,
1224 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1227 if (errno != ENOENT)
1228 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1235 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1241 /* NB: can't support range ipaddress except * and *@net */
1242 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1243 net = libcfs_str2net(str + 2);
1244 if (net == LNET_NIDNET(LNET_NID_ANY))
1247 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1249 rc = libcfs_str2anynid(&nid, str);
1257 fprintf(stderr, "Invalid NID : %s\n", str);
1262 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1264 if (!strcasecmp(msg_str, "put")) {
1265 *mask_p |= LNET_PUT_BIT;
1268 } else if (!strcasecmp(msg_str, "ack")) {
1269 *mask_p |= LNET_ACK_BIT;
1272 } else if (!strcasecmp(msg_str, "get")) {
1273 *mask_p |= LNET_GET_BIT;
1276 } else if (!strcasecmp(msg_str, "reply")) {
1277 *mask_p |= LNET_REPLY_BIT;
1281 fprintf(stderr, "unknown message type %s\n", msg_str);
1286 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1288 unsigned long rc = strtoul(optarg, NULL, 0);
1291 fprintf(stderr, "invalid portal: %lu\n", rc);
1295 *mask_p |= (1ULL << rc);
1300 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1302 struct libcfs_ioctl_data data = {{0}};
1303 struct lnet_fault_attr attr;
1307 static struct option opts[] = {
1308 {"source", required_argument, 0, 's'},
1309 {"dest", required_argument, 0, 'd'},
1310 {"rate", required_argument, 0, 'r'},
1311 {"interval", required_argument, 0, 'i'},
1312 {"latency", required_argument, 0, 'l'},
1313 {"portal", required_argument, 0, 'p'},
1314 {"message", required_argument, 0, 'm'},
1319 fprintf(stderr, "Failed, please provide source, destination "
1320 "and rate of rule\n");
1324 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1325 memset(&attr, 0, sizeof(attr));
1327 char c = getopt_long(argc, argv, optstr, opts, NULL);
1333 case 's': /* source NID/NET */
1334 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1339 case 'd': /* dest NID/NET */
1340 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1345 case 'r': /* drop rate */
1346 if (opc == LNET_CTL_DROP_ADD)
1347 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1349 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1352 case 'i': /* time interval (# seconds) for message drop */
1353 if (opc == LNET_CTL_DROP_ADD)
1354 attr.u.drop.da_interval = strtoul(optarg,
1357 attr.u.delay.la_interval = strtoul(optarg,
1361 case 'l': /* seconds to wait before activating rule */
1362 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1365 case 'p': /* portal to filter */
1366 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1371 case 'm': /* message types to filter */
1372 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1378 fprintf(stderr, "error: %s: option '%s' "
1379 "unrecognized\n", argv[0], argv[optind - 1]);
1385 if (opc == LNET_CTL_DROP_ADD) {
1386 /* NB: drop rate and interval are exclusive to each other */
1387 if (!((attr.u.drop.da_rate == 0) ^
1388 (attr.u.drop.da_interval == 0))) {
1390 "please provide either drop rate or interval "
1391 "but not both at the same time.\n");
1394 } else if (opc == LNET_CTL_DELAY_ADD) {
1395 if (!((attr.u.delay.la_rate == 0) ^
1396 (attr.u.delay.la_interval == 0))) {
1398 "please provide either delay rate or interval "
1399 "but not both at the same time.\n");
1403 if (attr.u.delay.la_latency == 0) {
1404 fprintf(stderr, "latency cannot be zero\n");
1409 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1410 fprintf(stderr, "Please provide both source and destination "
1411 "of %s rule\n", name);
1415 data.ioc_flags = opc;
1416 data.ioc_inllen1 = sizeof(attr);
1417 data.ioc_inlbuf1 = (char *)&attr;
1418 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1419 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1423 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1425 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1426 name, libcfs_nid2str(attr.fa_src),
1427 libcfs_nid2str(attr.fa_dst), strerror(errno));
1431 printf("Added %s rule %s->%s (1/%d)\n",
1432 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1433 opc == LNET_CTL_DROP_ADD ?
1434 attr.u.drop.da_rate : attr.u.delay.la_rate);
1443 jt_ptl_drop_add(int argc, char **argv)
1445 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1449 jt_ptl_delay_add(int argc, char **argv)
1451 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1455 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1457 struct libcfs_ioctl_data data = {{0}};
1458 struct lnet_fault_attr attr;
1462 static struct option opts[] = {
1463 {"source", required_argument, 0, 's'},
1464 {"dest", required_argument, 0, 'd'},
1465 {"all", no_argument, 0, 'a'},
1470 fprintf(stderr, "Failed, please provide source and "
1471 "destination of rule\n");
1475 memset(&attr, 0, sizeof(attr));
1477 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1484 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1489 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1494 attr.fa_src = attr.fa_dst = 0;
1498 fprintf(stderr, "error: %s: option '%s' "
1499 "unrecognized\n", argv[0], argv[optind - 1]);
1505 data.ioc_flags = opc;
1506 data.ioc_inllen1 = sizeof(attr);
1507 data.ioc_inlbuf1 = (char *)&attr;
1508 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1509 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1513 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1515 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1516 all ? "all" : libcfs_nid2str(attr.fa_src),
1517 all ? "all" : libcfs_nid2str(attr.fa_dst),
1522 libcfs_ioctl_unpack(&data, ioc_buf);
1523 printf("Removed %d %s rules\n", data.ioc_count, name);
1532 jt_ptl_drop_del(int argc, char **argv)
1534 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1538 jt_ptl_delay_del(int argc, char **argv)
1540 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1544 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1546 struct libcfs_ioctl_data data = {{0}};
1549 LIBCFS_IOC_INIT(data);
1550 data.ioc_flags = opc;
1552 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1554 fprintf(stderr, "failed to reset %s stats: %s\n",
1555 name, strerror(errno));
1562 jt_ptl_drop_reset(int argc, char **argv)
1564 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1568 jt_ptl_delay_reset(int argc, char **argv)
1570 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1575 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1577 struct libcfs_ioctl_data data = {{0}};
1578 struct lnet_fault_attr attr;
1579 struct lnet_fault_stat stat;
1582 printf("LNet %s rules:\n", name);
1583 for (pos = 0;; pos++) {
1586 memset(&attr, 0, sizeof(attr));
1587 memset(&stat, 0, sizeof(stat));
1589 data.ioc_count = pos;
1590 data.ioc_flags = opc;
1591 data.ioc_inllen1 = sizeof(attr);
1592 data.ioc_inlbuf1 = (char *)&attr;
1593 data.ioc_inllen2 = sizeof(stat);
1594 data.ioc_inlbuf2 = (char *)&stat;
1595 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1596 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1600 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1604 libcfs_ioctl_unpack(&data, ioc_buf);
1606 if (opc == LNET_CTL_DROP_LIST) {
1607 printf("%s->%s (1/%d | %d) ptl "LPX64", msg %x, "
1608 LPU64"/"LPU64", PUT "LPU64", ACK "LPU64", GET "
1609 LPU64", REP "LPU64"\n",
1610 libcfs_nid2str(attr.fa_src),
1611 libcfs_nid2str(attr.fa_dst),
1612 attr.u.drop.da_rate, attr.u.drop.da_interval,
1613 attr.fa_ptl_mask, attr.fa_msg_mask,
1614 stat.u.drop.ds_dropped, stat.fs_count,
1615 stat.fs_put, stat.fs_ack,
1616 stat.fs_get, stat.fs_reply);
1618 } else if (opc == LNET_CTL_DELAY_LIST) {
1619 printf("%s->%s (1/%d | %d, latency %d) ptl "LPX64
1620 ", msg %x, "LPU64"/"LPU64", PUT "LPU64
1621 ", ACK "LPU64", GET "LPU64", REP "LPU64"\n",
1622 libcfs_nid2str(attr.fa_src),
1623 libcfs_nid2str(attr.fa_dst),
1624 attr.u.delay.la_rate, attr.u.delay.la_interval,
1625 attr.u.delay.la_latency,
1626 attr.fa_ptl_mask, attr.fa_msg_mask,
1627 stat.u.delay.ls_delayed, stat.fs_count,
1628 stat.fs_put, stat.fs_ack, stat.fs_get,
1632 printf("found total %d\n", pos);
1638 jt_ptl_drop_list(int argc, char **argv)
1640 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1644 jt_ptl_delay_list(int argc, char **argv)
1646 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1650 get_cycles_per_usec ()
1652 FILE *f = fopen ("/proc/cpuinfo", "r");
1657 while (fgets (line, sizeof (line), f) != NULL)
1658 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1665 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1669 int jt_ptl_memhog(int argc, char **argv)
1671 static int gfp = 0; /* sticky! */
1673 struct libcfs_ioctl_data data;
1679 fprintf(stderr, "usage: %s <npages> [<GFP flags>]\n", argv[0]);
1683 count = strtol(argv[1], &end, 0);
1684 if (count < 0 || *end != 0) {
1685 fprintf(stderr, "Can't parse page count '%s'\n", argv[1]);
1690 rc = strtol(argv[2], &end, 0);
1692 fprintf(stderr, "Can't parse gfp flags '%s'\n", argv[2]);
1698 LIBCFS_IOC_INIT(data);
1699 data.ioc_count = count;
1700 data.ioc_flags = gfp;
1701 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_MEMHOG, &data);
1704 fprintf(stderr, "memhog %d failed: %s\n", count, strerror(errno));
1708 printf("memhog %d OK\n", count);
1712 int jt_ptl_testprotocompat(int argc, char **argv)
1714 struct libcfs_ioctl_data data;
1720 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1724 flags = strtol(argv[1], &end, 0);
1725 if (flags < 0 || *end != 0) {
1726 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1730 LIBCFS_IOC_INIT(data);
1731 data.ioc_flags = flags;
1732 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1735 fprintf(stderr, "test proto compat %x failed: %s\n",
1736 flags, strerror(errno));
1740 printf("test proto compat %x OK\n", flags);