2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2015, Intel Corporation.
6 * This file is part of Lustre, https://wiki.hpdd.intel.com/
8 * Portals is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Portals is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Portals; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <sys/ioctl.h>
36 #include <linux/types.h>
38 #include <libcfs/util/string.h>
39 #include <libcfs/util/ioctl.h>
40 #include <libcfs/libcfs_debug.h>
41 #include <lnet/lnetctl.h>
42 #include <lnet/socklnd.h>
43 #include <lnet/lnet.h>
45 unsigned int libcfs_debug;
46 unsigned int libcfs_printk = D_CANTMASK;
48 static bool g_net_interactive;
49 static bool g_net_set;
52 #define IOC_BUF_SIZE 8192
53 static char local_buf[IOC_BUF_SIZE];
54 static char *ioc_buf = local_buf;
56 /* Convert a string boolean to an int; "enable" -> 1 */
58 lnet_parse_bool (int *b, char *str)
60 if (!strcasecmp (str, "no") ||
61 !strcasecmp (str, "n") ||
62 !strcasecmp (str, "off") ||
63 !strcasecmp (str, "down") ||
64 !strcasecmp (str, "disable"))
70 if (!strcasecmp (str, "yes") ||
71 !strcasecmp (str, "y") ||
72 !strcasecmp (str, "on") ||
73 !strcasecmp (str, "up") ||
74 !strcasecmp (str, "enable"))
84 lnet_parse_port (int *port, char *str)
88 *port = strtol (str, &end, 0);
90 if (*end == 0 && /* parsed whole string */
91 *port > 0 && *port < 65536) /* minimal sanity check */
97 #ifdef HAVE_GETHOSTBYNAME
98 static struct hostent *
99 ptl_gethostbyname(char * hname) {
101 he = gethostbyname(hname);
106 fprintf(stderr, "Unable to resolve hostname: %s\n",
110 fprintf(stderr, "gethostbyname error for %s: %s\n",
111 hname, strerror(h_errno));
121 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
128 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
129 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
130 (c & ~0xff) == 0 && (d & ~0xff) == 0)
132 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
140 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
142 #ifdef HAVE_GETHOSTBYNAME
146 if (!strcmp (str, "_all_")) {
151 if (lnet_parse_ipquad(ipaddrp, str) == 0)
154 #ifdef HAVE_GETHOSTBYNAME
155 if ((('a' <= str[0] && str[0] <= 'z') ||
156 ('A' <= str[0] && str[0] <= 'Z')) &&
157 (he = ptl_gethostbyname (str)) != NULL) {
158 __u32 addr = *(__u32 *)he->h_addr;
160 *ipaddrp = ntohl(addr); /* HOST byte order */
169 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
171 #ifdef HAVE_GETHOSTBYNAME
176 net_ip = htonl (ipaddr);
177 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
179 strlcpy(str, he->h_name, strsize);
185 sprintf (str, "%d.%d.%d.%d",
186 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
187 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
192 lnet_parse_time (time_t *t, char *str)
198 *t = strtol (str, &end, 0);
199 if (*end == 0) /* parsed whole string */
202 memset (&tm, 0, sizeof (tm));
203 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
204 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
205 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
209 tm.tm_mon--; /* convert to 0 == Jan */
210 tm.tm_year -= 1900; /* y2k quirk */
211 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
214 if (*t == (time_t)-1)
221 lnet_parse_nid(char *nid_str, struct lnet_process_id *id_ptr)
223 id_ptr->pid = LNET_PID_ANY;
224 id_ptr->nid = libcfs_str2nid(nid_str);
225 if (id_ptr->nid == LNET_NID_ANY) {
226 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
233 static int g_net_is_set(char *cmd)
241 if (g_net_interactive)
247 "You must run '%s <network>' command before '%s'\n",
255 static int g_net_is_compatible(char *cmd, ...)
260 if (!g_net_is_set(cmd))
266 nal = va_arg(ap, int);
267 if (nal == LNET_NETTYP(g_net)) {
277 "Command %s not compatible with %s NAL\n",
279 libcfs_lnd2str(LNET_NETTYP(g_net)));
284 int ptl_initialize(int argc, char **argv)
287 g_net_interactive = true;
289 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
290 LNET_DEV_MAJOR, LNET_DEV_MINOR);
295 int jt_ptl_network(int argc, char **argv)
297 struct libcfs_ioctl_data data;
298 __u32 net = LNET_NIDNET(LNET_NID_ANY);
302 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
306 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
307 LIBCFS_IOC_INIT(data);
308 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
311 printf("LNET ready to unload\n");
315 if (errno == ENODEV) {
316 printf("LNET is currently not loaded.");
321 fprintf(stderr, "LNET busy\n");
323 fprintf(stderr, "LNET unconfigure error %d: %s\n",
324 errno, strerror(errno));
326 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
327 LIBCFS_IOC_INIT(data);
328 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
331 printf("LNET configured\n");
335 fprintf(stderr, "LNET configure error %d: %s\n",
336 errno, strerror(errno));
340 net = libcfs_str2net(argv[1]);
341 if (net == LNET_NIDNET(LNET_NID_ANY)) {
342 fprintf(stderr, "Can't parse net %s\n", argv[1]);
352 jt_ptl_list_nids(int argc, char **argv)
354 struct libcfs_ioctl_data data;
355 int all = 0, return_nid = 0;
359 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
360 /* Hack to pass back value */
361 return_nid = (argc == 2) && (argv[1][0] == 1);
363 if ((argc > 2) && !(all || return_nid)) {
364 fprintf(stderr, "usage: %s [all]\n", argv[0]);
368 for (count = 0;; count++) {
369 LIBCFS_IOC_INIT (data);
370 data.ioc_count = count;
371 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
374 if ((count > 0) && (errno == ENOENT))
375 /* We found them all */
377 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
378 errno, strerror(errno));
382 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
383 printf("%s\n", libcfs_nid2str(data.ioc_nid));
385 *(__u64 *)(argv[1]) = data.ioc_nid;
395 jt_ptl_which_nid (int argc, char **argv)
397 struct libcfs_ioctl_data data;
400 lnet_nid_t best_nid = LNET_NID_ANY;
409 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
413 for (i = 1; i < argc; i++) {
415 nid = libcfs_str2nid(nidstr);
416 if (nid == LNET_NID_ANY) {
417 fprintf(stderr, "Can't parse NID %s\n", nidstr);
421 LIBCFS_IOC_INIT(data);
424 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
426 fprintf(stderr, "Can't get distance to %s: %s\n",
427 nidstr, strerror(errno));
431 dist = data.ioc_u32[0];
432 order = data.ioc_u32[1];
435 if (dist == -EHOSTUNREACH)
438 fprintf(stderr, "Unexpected distance to %s: %d\n",
443 if (best_nid == LNET_NID_ANY ||
445 (dist == best_dist && order < best_order)) {
452 if (best_nid == LNET_NID_ANY) {
453 fprintf(stderr, "No reachable NID\n");
457 printf("%s\n", libcfs_nid2str(best_nid));
462 jt_ptl_print_interfaces (int argc, char **argv)
464 struct libcfs_ioctl_data data;
465 char buffer[3][HOST_NAME_MAX + 1];
469 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
472 for (index = 0;;index++) {
473 LIBCFS_IOC_INIT(data);
474 data.ioc_net = g_net;
475 data.ioc_count = index;
477 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
481 printf ("%s: (%s/%s) npeer %d nroute %d\n",
482 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
483 sizeof(buffer[2]), 1),
484 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
485 sizeof(buffer[0]), 0),
486 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
487 sizeof(buffer[1]), 0),
488 data.ioc_u32[2], data.ioc_u32[3]);
492 if (errno == ENOENT) {
493 printf ("<no interfaces>\n");
495 fprintf(stderr, "Error getting interfaces: %s: "
505 jt_ptl_add_interface (int argc, char **argv)
507 struct libcfs_ioctl_data data;
510 __u32 netmask = 0xffffff00;
515 if (argc < 2 || argc > 3) {
516 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
520 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
523 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
524 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
529 count = strtol(argv[2], &end, 0);
530 if (count > 0 && count < 32 && *end == 0) {
532 for (i = count; i > 0; i--)
533 netmask = netmask|(1<<(32-i));
534 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
535 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
540 LIBCFS_IOC_INIT(data);
541 data.ioc_net = g_net;
542 data.ioc_u32[0] = ipaddr;
543 data.ioc_u32[1] = netmask;
545 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
547 fprintf (stderr, "failed to add interface: %s\n",
556 jt_ptl_del_interface (int argc, char **argv)
558 struct libcfs_ioctl_data data;
563 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
567 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
571 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
572 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
576 LIBCFS_IOC_INIT(data);
577 data.ioc_net = g_net;
578 data.ioc_u32[0] = ipaddr;
580 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
582 fprintf (stderr, "failed to delete interface: %s\n",
591 jt_ptl_print_peers (int argc, char **argv)
593 struct libcfs_ioctl_data data;
594 struct lnet_process_id id;
595 char buffer[2][HOST_NAME_MAX + 1];
599 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
603 for (index = 0;;index++) {
604 LIBCFS_IOC_INIT(data);
605 data.ioc_net = g_net;
606 data.ioc_count = index;
608 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
612 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
613 id.nid = data.ioc_nid;
614 id.pid = data.ioc_u32[4];
615 printf ("%-20s [%d]%s->%s:%d #%d\n",
617 data.ioc_count, /* persistence */
619 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
620 sizeof(buffer[0]), 1),
622 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
623 sizeof(buffer[1]), 1),
624 data.ioc_u32[1], /* peer port */
625 data.ioc_u32[3]); /* conn_count */
626 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
627 int disconn = data.ioc_flags >> 16;
633 state = data.ioc_flags & 0xffff ? "C" : "U";
635 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
636 libcfs_nid2str(data.ioc_nid), /* peer nid */
637 data.ioc_net, /* gemini device id */
638 state, /* peer is Connecting, Up, or Down */
639 data.ioc_count, /* peer refcount */
640 (uintmax_t)data.ioc_u64[0], /* peerstamp */
641 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
642 /* fmaq, nfma, nrdma */
643 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
645 printf ("%-20s [%d]\n",
646 libcfs_nid2str(data.ioc_nid), data.ioc_count);
651 if (errno == ENOENT) {
652 printf ("<no peers>\n");
654 fprintf(stderr, "Error getting peer list: %s: "
662 int jt_ptl_add_peer(int argc, char **argv)
664 struct libcfs_ioctl_data data;
670 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
674 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
679 nid = libcfs_str2nid(argv[1]);
680 if (nid == LNET_NID_ANY) {
681 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
685 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
686 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
690 if (lnet_parse_port (&port, argv[3]) != 0) {
691 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
695 LIBCFS_IOC_INIT(data);
696 data.ioc_net = g_net;
698 data.ioc_u32[0] = ip;
699 data.ioc_u32[1] = port;
701 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
703 fprintf (stderr, "failed to add peer: %s\n",
712 jt_ptl_del_peer (int argc, char **argv)
714 struct libcfs_ioctl_data data;
715 lnet_nid_t nid = LNET_NID_ANY;
716 lnet_pid_t pid = LNET_PID_ANY;
720 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
724 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
726 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
730 } else if (argc > 2) {
731 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
736 !libcfs_str2anynid(&nid, argv[1])) {
737 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
741 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
743 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
744 fprintf (stderr, "Can't parse ip addr: %s\n",
750 LIBCFS_IOC_INIT(data);
751 data.ioc_net = g_net;
753 data.ioc_u32[0] = ip;
754 data.ioc_u32[1] = pid;
756 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
758 fprintf (stderr, "failed to remove peer: %s\n",
767 jt_ptl_print_connections (int argc, char **argv)
769 struct libcfs_ioctl_data data;
770 struct lnet_process_id id;
771 char buffer[2][HOST_NAME_MAX + 1];
775 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
778 for (index = 0; ; index++) {
779 LIBCFS_IOC_INIT(data);
780 data.ioc_net = g_net;
781 data.ioc_count = index;
783 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
787 if (g_net_is_compatible (NULL, SOCKLND, 0)) {
788 id.nid = data.ioc_nid;
789 id.pid = data.ioc_u32[6];
790 printf ("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
792 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
793 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
794 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
795 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
796 data.ioc_u32[4], /* scheduler */
798 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
799 sizeof(buffer[0]), 1),
801 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
802 sizeof(buffer[1]), 1),
803 data.ioc_u32[1], /* remote port */
804 data.ioc_count, /* tx buffer size */
805 data.ioc_u32[5], /* rx buffer size */
806 data.ioc_flags ? "nagle" : "nonagle");
807 } else if (g_net_is_compatible (NULL, O2IBLND, 0)) {
808 printf ("%s mtu %d\n",
809 libcfs_nid2str(data.ioc_nid),
810 data.ioc_u32[0]); /* path MTU */
811 } else if (g_net_is_compatible (NULL, GNILND, 0)) {
812 printf ("%-20s [%d]\n",
813 libcfs_nid2str(data.ioc_nid),
814 data.ioc_u32[0] /* device id */);
816 printf ("%s\n", libcfs_nid2str(data.ioc_nid));
821 if (errno == ENOENT) {
822 printf ("<no connections>\n");
824 fprintf(stderr, "Error getting connection list: %s: "
832 int jt_ptl_disconnect(int argc, char **argv)
834 struct libcfs_ioctl_data data;
835 lnet_nid_t nid = LNET_NID_ANY;
840 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
844 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
848 !libcfs_str2anynid(&nid, argv[1])) {
849 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
853 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
855 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
856 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
860 LIBCFS_IOC_INIT(data);
861 data.ioc_net = g_net;
863 data.ioc_u32[0] = ipaddr;
865 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
867 fprintf(stderr, "failed to remove connection: %s\n",
875 int jt_ptl_push_connection (int argc, char **argv)
877 struct libcfs_ioctl_data data;
879 lnet_nid_t nid = LNET_NID_ANY;
882 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
886 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
890 !libcfs_str2anynid(&nid, argv[1])) {
891 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
895 LIBCFS_IOC_INIT(data);
896 data.ioc_net = g_net;
899 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
901 fprintf(stderr, "failed to push connection: %s\n",
909 int jt_ptl_ping(int argc, char **argv)
913 struct lnet_process_id id;
914 struct lnet_process_id ids[16];
915 int maxids = sizeof(ids)/sizeof(ids[0]);
916 struct libcfs_ioctl_data data;
921 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
925 sep = strchr(argv[1], '-');
927 rc = lnet_parse_nid(argv[1], &id);
933 if (argv[1][0] == 'u' ||
935 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
937 id.pid = strtoul(argv[1], &end, 0);
939 if (end != sep) { /* assuming '-' is part of hostname */
940 rc = lnet_parse_nid(argv[1], &id);
944 id.nid = libcfs_str2nid(sep + 1);
946 if (id.nid == LNET_NID_ANY) {
948 "Can't parse process id \"%s\"\n",
956 timeout = 1000 * atol(argv[2]);
957 if (timeout > 120 * 1000) {
958 fprintf(stderr, "Timeout %s is to large\n",
963 timeout = 1000; /* default 1 second timeout */
965 LIBCFS_IOC_INIT (data);
966 data.ioc_nid = id.nid;
967 data.ioc_u32[0] = id.pid;
968 data.ioc_u32[1] = timeout;
969 data.ioc_plen1 = sizeof(ids);
970 data.ioc_pbuf1 = (char *)ids;
972 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
974 fprintf(stderr, "failed to ping %s: %s\n",
975 id.pid == LNET_PID_ANY ?
976 libcfs_nid2str(id.nid) : libcfs_id2str(id),
981 for (i = 0; i < data.ioc_count && i < maxids; i++)
982 printf("%s\n", libcfs_id2str(ids[i]));
984 if (data.ioc_count > maxids)
985 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
990 int jt_ptl_mynid(int argc, char **argv)
992 struct libcfs_ioctl_data data;
997 fprintf(stderr, "usage: %s NID\n", argv[0]);
1001 nid = libcfs_str2nid(argv[1]);
1002 if (nid == LNET_NID_ANY) {
1003 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1007 LIBCFS_IOC_INIT(data);
1008 data.ioc_net = LNET_NIDNET(nid);
1011 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1013 fprintf(stderr, "setting my NID failed: %s\n",
1016 printf("registered my nid %s\n", libcfs_nid2str(nid));
1022 jt_ptl_fail_nid (int argc, char **argv)
1027 struct libcfs_ioctl_data data;
1029 if (argc < 2 || argc > 3)
1031 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1035 if (!libcfs_str2anynid(&nid, argv[1]))
1037 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1042 threshold = LNET_MD_THRESH_INF;
1043 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1044 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1048 LIBCFS_IOC_INIT (data);
1050 data.ioc_count = threshold;
1052 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1054 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1057 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1063 jt_ptl_add_route (int argc, char **argv)
1065 struct lnet_ioctl_config_data data;
1066 lnet_nid_t gateway_nid;
1067 __u32 hops = LNET_UNDEFINED_HOPS;
1068 unsigned int priority = 0;
1072 if (argc < 2 || argc > 4) {
1073 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1078 if (g_net_is_set(argv[0]) == 0)
1081 gateway_nid = libcfs_str2nid(argv[1]);
1082 if (gateway_nid == LNET_NID_ANY) {
1083 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1088 hops = strtol(argv[2], &end, 0);
1089 if (hops == 0 || hops >= 256 ||
1090 (end != NULL && *end != 0)) {
1091 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1096 priority = strtoul(argv[3], &end, 0);
1097 if (end != NULL && *end != 0) {
1099 "Can't parse priority \"%s\"\n",
1106 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1107 data.cfg_net = g_net;
1108 data.cfg_config_u.cfg_route.rtr_hop = hops;
1109 data.cfg_nid = gateway_nid;
1110 data.cfg_config_u.cfg_route.rtr_priority = priority;
1112 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1114 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1123 jt_ptl_del_route (int argc, char **argv)
1125 struct lnet_ioctl_config_data data;
1130 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1134 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1135 fprintf(stderr, "Can't parse gateway NID "
1136 "\"%s\"\n", argv[1]);
1140 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1141 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1144 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1146 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1147 libcfs_nid2str(nid), strerror(errno));
1155 jt_ptl_notify_router (int argc, char **argv)
1157 struct libcfs_ioctl_data data;
1166 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1171 nid = libcfs_str2nid(argv[1]);
1172 if (nid == LNET_NID_ANY) {
1173 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1177 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1178 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1182 gettimeofday(&now, NULL);
1186 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1187 fprintf(stderr, "Can't parse time %s\n"
1188 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1189 "or an absolute unix time in seconds\n", argv[3]);
1191 } else if (when > now.tv_sec) {
1192 fprintf (stderr, "%s specifies a time in the future\n",
1197 LIBCFS_IOC_INIT(data);
1199 data.ioc_flags = enable;
1200 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1201 data.ioc_u64[0] = (__u64)when;
1203 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1205 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1206 libcfs_nid2str(nid), strerror (errno));
1214 jt_ptl_print_routes (int argc, char **argv)
1216 struct lnet_ioctl_config_data data;
1225 for (index = 0; ; index++) {
1226 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1227 data.cfg_count = index;
1229 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1234 hops = data.cfg_config_u.cfg_route.rtr_hop;
1236 alive = data.cfg_config_u.cfg_route.rtr_flags;
1237 pri = data.cfg_config_u.cfg_route.rtr_priority;
1239 printf("net %18s hops %u gw %32s %s pri %u\n",
1240 libcfs_net2str(net), hops,
1241 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1244 if (errno != ENOENT)
1245 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1252 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1258 /* NB: can't support range ipaddress except * and *@net */
1259 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1260 net = libcfs_str2net(str + 2);
1261 if (net == LNET_NIDNET(LNET_NID_ANY))
1264 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1266 rc = libcfs_str2anynid(&nid, str);
1274 fprintf(stderr, "Invalid NID : %s\n", str);
1279 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1281 if (!strcasecmp(msg_str, "put")) {
1282 *mask_p |= LNET_PUT_BIT;
1285 } else if (!strcasecmp(msg_str, "ack")) {
1286 *mask_p |= LNET_ACK_BIT;
1289 } else if (!strcasecmp(msg_str, "get")) {
1290 *mask_p |= LNET_GET_BIT;
1293 } else if (!strcasecmp(msg_str, "reply")) {
1294 *mask_p |= LNET_REPLY_BIT;
1298 fprintf(stderr, "unknown message type %s\n", msg_str);
1303 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1305 unsigned long rc = strtoul(optarg, NULL, 0);
1308 fprintf(stderr, "invalid portal: %lu\n", rc);
1312 *mask_p |= (1ULL << rc);
1317 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1319 struct libcfs_ioctl_data data = { { 0 } };
1320 struct lnet_fault_attr attr;
1324 static const struct option opts[] = {
1325 { .name = "source", .has_arg = required_argument, .val = 's' },
1326 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1327 { .name = "rate", .has_arg = required_argument, .val = 'r' },
1328 { .name = "interval", .has_arg = required_argument, .val = 'i' },
1329 { .name = "latency", .has_arg = required_argument, .val = 'l' },
1330 { .name = "portal", .has_arg = required_argument, .val = 'p' },
1331 { .name = "message", .has_arg = required_argument, .val = 'm' },
1335 fprintf(stderr, "Failed, please provide source, destination "
1336 "and rate of rule\n");
1340 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1341 memset(&attr, 0, sizeof(attr));
1343 char c = getopt_long(argc, argv, optstr, opts, NULL);
1349 case 's': /* source NID/NET */
1350 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1355 case 'd': /* dest NID/NET */
1356 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1361 case 'r': /* drop rate */
1362 if (opc == LNET_CTL_DROP_ADD)
1363 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1365 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1368 case 'i': /* time interval (# seconds) for message drop */
1369 if (opc == LNET_CTL_DROP_ADD)
1370 attr.u.drop.da_interval = strtoul(optarg,
1373 attr.u.delay.la_interval = strtoul(optarg,
1377 case 'l': /* seconds to wait before activating rule */
1378 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1381 case 'p': /* portal to filter */
1382 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1387 case 'm': /* message types to filter */
1388 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1394 fprintf(stderr, "error: %s: option '%s' "
1395 "unrecognized\n", argv[0], argv[optind - 1]);
1401 if (opc == LNET_CTL_DROP_ADD) {
1402 /* NB: drop rate and interval are exclusive to each other */
1403 if (!((attr.u.drop.da_rate == 0) ^
1404 (attr.u.drop.da_interval == 0))) {
1406 "please provide either drop rate or interval "
1407 "but not both at the same time.\n");
1410 } else if (opc == LNET_CTL_DELAY_ADD) {
1411 if (!((attr.u.delay.la_rate == 0) ^
1412 (attr.u.delay.la_interval == 0))) {
1414 "please provide either delay rate or interval "
1415 "but not both at the same time.\n");
1419 if (attr.u.delay.la_latency == 0) {
1420 fprintf(stderr, "latency cannot be zero\n");
1425 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1426 fprintf(stderr, "Please provide both source and destination "
1427 "of %s rule\n", name);
1431 data.ioc_flags = opc;
1432 data.ioc_inllen1 = sizeof(attr);
1433 data.ioc_inlbuf1 = (char *)&attr;
1434 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1435 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1439 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1441 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1442 name, libcfs_nid2str(attr.fa_src),
1443 libcfs_nid2str(attr.fa_dst), strerror(errno));
1447 printf("Added %s rule %s->%s (1/%d)\n",
1448 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1449 opc == LNET_CTL_DROP_ADD ?
1450 attr.u.drop.da_rate : attr.u.delay.la_rate);
1459 jt_ptl_drop_add(int argc, char **argv)
1461 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1465 jt_ptl_delay_add(int argc, char **argv)
1467 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1471 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1473 struct libcfs_ioctl_data data = { { 0 } };
1474 struct lnet_fault_attr attr;
1478 static const struct option opts[] = {
1479 { .name = "source", .has_arg = required_argument, .val = 's' },
1480 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1481 { .name = "all", .has_arg = no_argument, .val = 'a' },
1485 fprintf(stderr, "Failed, please provide source and "
1486 "destination of rule\n");
1490 memset(&attr, 0, sizeof(attr));
1492 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1499 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1504 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1509 attr.fa_src = attr.fa_dst = 0;
1513 fprintf(stderr, "error: %s: option '%s' "
1514 "unrecognized\n", argv[0], argv[optind - 1]);
1520 data.ioc_flags = opc;
1521 data.ioc_inllen1 = sizeof(attr);
1522 data.ioc_inlbuf1 = (char *)&attr;
1523 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1524 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1528 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1530 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1531 all ? "all" : libcfs_nid2str(attr.fa_src),
1532 all ? "all" : libcfs_nid2str(attr.fa_dst),
1537 libcfs_ioctl_unpack(&data, ioc_buf);
1538 printf("Removed %d %s rules\n", data.ioc_count, name);
1547 jt_ptl_drop_del(int argc, char **argv)
1549 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1553 jt_ptl_delay_del(int argc, char **argv)
1555 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1559 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1561 struct libcfs_ioctl_data data = { { 0 } };
1564 LIBCFS_IOC_INIT(data);
1565 data.ioc_flags = opc;
1567 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1569 fprintf(stderr, "failed to reset %s stats: %s\n",
1570 name, strerror(errno));
1577 jt_ptl_drop_reset(int argc, char **argv)
1579 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1583 jt_ptl_delay_reset(int argc, char **argv)
1585 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1590 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1592 struct libcfs_ioctl_data data = { { 0 } };
1593 struct lnet_fault_attr attr;
1594 struct lnet_fault_stat stat;
1597 printf("LNet %s rules:\n", name);
1598 for (pos = 0;; pos++) {
1601 memset(&attr, 0, sizeof(attr));
1602 memset(&stat, 0, sizeof(stat));
1604 data.ioc_count = pos;
1605 data.ioc_flags = opc;
1606 data.ioc_inllen1 = sizeof(attr);
1607 data.ioc_inlbuf1 = (char *)&attr;
1608 data.ioc_inllen2 = sizeof(stat);
1609 data.ioc_inlbuf2 = (char *)&stat;
1610 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1611 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1615 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1619 libcfs_ioctl_unpack(&data, ioc_buf);
1621 if (opc == LNET_CTL_DROP_LIST) {
1622 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1623 "%ju/%ju, PUT %ju, ACK %ju, GET "
1625 libcfs_nid2str(attr.fa_src),
1626 libcfs_nid2str(attr.fa_dst),
1627 attr.u.drop.da_rate, attr.u.drop.da_interval,
1628 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1629 (uintmax_t)stat.u.drop.ds_dropped,
1630 (uintmax_t)stat.fs_count,
1631 (uintmax_t)stat.fs_put,
1632 (uintmax_t)stat.fs_ack,
1633 (uintmax_t)stat.fs_get,
1634 (uintmax_t)stat.fs_reply);
1636 } else if (opc == LNET_CTL_DELAY_LIST) {
1637 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1638 ", msg %x, %ju/%ju, PUT %ju"
1639 ", ACK %ju, GET %ju, REP %ju\n",
1640 libcfs_nid2str(attr.fa_src),
1641 libcfs_nid2str(attr.fa_dst),
1642 attr.u.delay.la_rate, attr.u.delay.la_interval,
1643 attr.u.delay.la_latency,
1644 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1645 (uintmax_t)stat.u.delay.ls_delayed,
1646 (uintmax_t)stat.fs_count,
1647 (uintmax_t)stat.fs_put,
1648 (uintmax_t)stat.fs_ack,
1649 (uintmax_t)stat.fs_get,
1650 (uintmax_t)stat.fs_reply);
1653 printf("found total %d\n", pos);
1659 jt_ptl_drop_list(int argc, char **argv)
1661 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1665 jt_ptl_delay_list(int argc, char **argv)
1667 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1671 get_cycles_per_usec ()
1673 FILE *f = fopen ("/proc/cpuinfo", "r");
1678 while (fgets (line, sizeof (line), f) != NULL)
1679 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1686 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1690 int jt_ptl_testprotocompat(int argc, char **argv)
1692 struct libcfs_ioctl_data data;
1698 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1702 flags = strtol(argv[1], &end, 0);
1703 if (flags < 0 || *end != 0) {
1704 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1708 LIBCFS_IOC_INIT(data);
1709 data.ioc_flags = flags;
1710 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1713 fprintf(stderr, "test proto compat %x failed: %s\n",
1714 flags, strerror(errno));
1718 printf("test proto compat %x OK\n", flags);