2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2013, 2017, 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 <linux/lnet/libcfs_debug.h>
41 #include <linux/lnet/lnet-dlc.h>
42 #include <linux/lnet/lnetctl.h>
43 #include <linux/lnet/nidstr.h>
44 #include <linux/lnet/socklnd.h>
46 unsigned int libcfs_debug;
47 unsigned int libcfs_printk = D_CANTMASK;
49 static bool g_net_interactive;
50 static bool g_net_set;
53 #define IOC_BUF_SIZE 8192
54 static char local_buf[IOC_BUF_SIZE];
55 static char *ioc_buf = local_buf;
57 /* Convert a string boolean to an int; "enable" -> 1 */
59 lnet_parse_bool (int *b, char *str)
61 if (!strcasecmp (str, "no") ||
62 !strcasecmp (str, "n") ||
63 !strcasecmp (str, "off") ||
64 !strcasecmp (str, "down") ||
65 !strcasecmp (str, "disable"))
71 if (!strcasecmp (str, "yes") ||
72 !strcasecmp (str, "y") ||
73 !strcasecmp (str, "on") ||
74 !strcasecmp (str, "up") ||
75 !strcasecmp (str, "enable"))
85 lnet_parse_port (int *port, char *str)
89 *port = strtol (str, &end, 0);
91 if (*end == 0 && /* parsed whole string */
92 *port > 0 && *port < 65536) /* minimal sanity check */
98 #ifdef HAVE_GETHOSTBYNAME
99 static struct hostent *
100 ptl_gethostbyname(char * hname) {
102 he = gethostbyname(hname);
107 fprintf(stderr, "Unable to resolve hostname: %s\n",
111 fprintf(stderr, "gethostbyname error for %s: %s\n",
112 hname, strerror(h_errno));
122 lnet_parse_ipquad (__u32 *ipaddrp, char *str)
129 if (sscanf (str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
130 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
131 (c & ~0xff) == 0 && (d & ~0xff) == 0)
133 *ipaddrp = (a<<24)|(b<<16)|(c<<8)|d;
141 lnet_parse_ipaddr (__u32 *ipaddrp, char *str)
143 #ifdef HAVE_GETHOSTBYNAME
147 if (!strcmp (str, "_all_")) {
152 if (lnet_parse_ipquad(ipaddrp, str) == 0)
155 #ifdef HAVE_GETHOSTBYNAME
156 if ((('a' <= str[0] && str[0] <= 'z') ||
157 ('A' <= str[0] && str[0] <= 'Z')) &&
158 (he = ptl_gethostbyname (str)) != NULL) {
159 __u32 addr = *(__u32 *)he->h_addr;
161 *ipaddrp = ntohl(addr); /* HOST byte order */
170 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
172 #ifdef HAVE_GETHOSTBYNAME
177 net_ip = htonl (ipaddr);
178 he = gethostbyaddr (&net_ip, sizeof (net_ip), AF_INET);
180 strlcpy(str, he->h_name, strsize);
186 sprintf (str, "%d.%d.%d.%d",
187 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
188 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
193 lnet_parse_time (time_t *t, char *str)
199 *t = strtol (str, &end, 0);
200 if (*end == 0) /* parsed whole string */
203 memset (&tm, 0, sizeof (tm));
204 n = sscanf (str, "%d-%d-%d-%d:%d:%d",
205 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
206 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
210 tm.tm_mon--; /* convert to 0 == Jan */
211 tm.tm_year -= 1900; /* y2k quirk */
212 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
215 if (*t == (time_t)-1)
222 lnet_parse_nid(char *nid_str, struct lnet_process_id *id_ptr)
224 id_ptr->pid = LNET_PID_ANY;
225 id_ptr->nid = libcfs_str2nid(nid_str);
226 if (id_ptr->nid == LNET_NID_ANY) {
227 fprintf (stderr, "Can't parse nid \"%s\"\n", nid_str);
234 static int g_net_is_set(char *cmd)
242 if (g_net_interactive)
248 "You must run '%s <network>' command before '%s'\n",
256 static int g_net_is_compatible(char *cmd, ...)
261 if (!g_net_is_set(cmd))
267 nal = va_arg(ap, int);
268 if (nal == LNET_NETTYP(g_net)) {
278 "Command %s not compatible with %s NAL\n",
280 libcfs_lnd2str(LNET_NETTYP(g_net)));
285 int ptl_initialize(int argc, char **argv)
288 g_net_interactive = true;
290 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
296 int jt_ptl_network(int argc, char **argv)
298 struct libcfs_ioctl_data data;
299 __u32 net = LNET_NIDNET(LNET_NID_ANY);
302 fprintf(stderr, "This command has been deprecated. Plesae use 'lnetctl net add'\n");
305 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
309 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
310 LIBCFS_IOC_INIT(data);
311 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
314 printf("LNET ready to unload\n");
318 if (errno == ENODEV) {
319 printf("LNET is currently not loaded.");
324 fprintf(stderr, "LNET busy\n");
326 fprintf(stderr, "LNET unconfigure error %d: %s\n",
327 errno, strerror(errno));
329 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
330 LIBCFS_IOC_INIT(data);
331 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
334 printf("LNET configured\n");
338 fprintf(stderr, "LNET configure error %d: %s\n",
339 errno, strerror(errno));
343 net = libcfs_str2net(argv[1]);
344 if (net == LNET_NIDNET(LNET_NID_ANY)) {
345 fprintf(stderr, "Can't parse net %s\n", argv[1]);
355 jt_ptl_list_nids(int argc, char **argv)
357 struct libcfs_ioctl_data data;
358 int all = 0, return_nid = 0;
362 fprintf(stderr, "This command has been deprecated. Plesae use 'lnetctl net show'\n");
364 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
365 /* Hack to pass back value */
366 return_nid = (argc == 2) && (argv[1][0] == 1);
368 if ((argc > 2) && !(all || return_nid)) {
369 fprintf(stderr, "usage: %s [all]\n", argv[0]);
373 for (count = 0;; count++) {
374 LIBCFS_IOC_INIT (data);
375 data.ioc_count = count;
376 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
379 if ((count > 0) && (errno == ENOENT))
380 /* We found them all */
382 fprintf(stderr,"IOC_LIBCFS_GET_NI error %d: %s\n",
383 errno, strerror(errno));
387 if (all || (LNET_NETTYP(LNET_NIDNET(data.ioc_nid)) != LOLND)) {
388 printf("%s\n", libcfs_nid2str(data.ioc_nid));
390 *(__u64 *)(argv[1]) = data.ioc_nid;
400 jt_ptl_which_nid (int argc, char **argv)
402 struct libcfs_ioctl_data data;
405 lnet_nid_t best_nid = LNET_NID_ANY;
414 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
418 for (i = 1; i < argc; i++) {
420 nid = libcfs_str2nid(nidstr);
421 if (nid == LNET_NID_ANY) {
422 fprintf(stderr, "Can't parse NID %s\n", nidstr);
426 LIBCFS_IOC_INIT(data);
429 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
431 fprintf(stderr, "Can't get distance to %s: %s\n",
432 nidstr, strerror(errno));
436 dist = data.ioc_u32[0];
437 order = data.ioc_u32[1];
440 if (dist == -EHOSTUNREACH)
443 fprintf(stderr, "Unexpected distance to %s: %d\n",
448 if (best_nid == LNET_NID_ANY ||
450 (dist == best_dist && order < best_order)) {
457 if (best_nid == LNET_NID_ANY) {
458 fprintf(stderr, "No reachable NID\n");
462 printf("%s\n", libcfs_nid2str(best_nid));
467 jt_ptl_print_interfaces (int argc, char **argv)
469 struct libcfs_ioctl_data data;
470 char buffer[3][HOST_NAME_MAX + 1];
474 if (!g_net_is_compatible (argv[0], SOCKLND, 0))
477 for (index = 0;;index++) {
478 LIBCFS_IOC_INIT(data);
479 data.ioc_net = g_net;
480 data.ioc_count = index;
482 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
486 printf ("%s: (%s/%s) npeer %d nroute %d\n",
487 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
488 sizeof(buffer[2]), 1),
489 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
490 sizeof(buffer[0]), 0),
491 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
492 sizeof(buffer[1]), 0),
493 data.ioc_u32[2], data.ioc_u32[3]);
497 if (errno == ENOENT) {
498 printf ("<no interfaces>\n");
500 fprintf(stderr, "Error getting interfaces: %s: "
510 jt_ptl_add_interface (int argc, char **argv)
512 struct libcfs_ioctl_data data;
515 __u32 netmask = 0xffffff00;
520 if (argc < 2 || argc > 3) {
521 fprintf (stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
525 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
528 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
529 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
534 count = strtol(argv[2], &end, 0);
535 if (count > 0 && count < 32 && *end == 0) {
537 for (i = count; i > 0; i--)
538 netmask = netmask|(1<<(32-i));
539 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
540 fprintf (stderr, "Can't parse netmask: %s\n", argv[2]);
545 LIBCFS_IOC_INIT(data);
546 data.ioc_net = g_net;
547 data.ioc_u32[0] = ipaddr;
548 data.ioc_u32[1] = netmask;
550 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
552 fprintf (stderr, "failed to add interface: %s\n",
561 jt_ptl_del_interface (int argc, char **argv)
563 struct libcfs_ioctl_data data;
568 fprintf (stderr, "usage: %s [ipaddr]\n", argv[0]);
572 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
576 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
577 fprintf (stderr, "Can't parse ip: %s\n", argv[1]);
581 LIBCFS_IOC_INIT(data);
582 data.ioc_net = g_net;
583 data.ioc_u32[0] = ipaddr;
585 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
587 fprintf (stderr, "failed to delete interface: %s\n",
596 jt_ptl_print_peers (int argc, char **argv)
598 struct libcfs_ioctl_data data;
599 struct lnet_process_id id;
600 char buffer[2][HOST_NAME_MAX + 1];
604 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
608 for (index = 0;;index++) {
609 LIBCFS_IOC_INIT(data);
610 data.ioc_net = g_net;
611 data.ioc_count = index;
613 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
617 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
618 id.nid = data.ioc_nid;
619 id.pid = data.ioc_u32[4];
620 printf ("%-20s [%d]%s->%s:%d #%d\n",
622 data.ioc_count, /* persistence */
624 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
625 sizeof(buffer[0]), 1),
627 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
628 sizeof(buffer[1]), 1),
629 data.ioc_u32[1], /* peer port */
630 data.ioc_u32[3]); /* conn_count */
631 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
632 int disconn = data.ioc_flags >> 16;
638 state = data.ioc_flags & 0xffff ? "C" : "U";
640 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
641 libcfs_nid2str(data.ioc_nid), /* peer nid */
642 data.ioc_net, /* gemini device id */
643 state, /* peer is Connecting, Up, or Down */
644 data.ioc_count, /* peer refcount */
645 (uintmax_t)data.ioc_u64[0], /* peerstamp */
646 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
647 /* fmaq, nfma, nrdma */
648 data.ioc_u32[0], data.ioc_u32[1], data.ioc_u32[4]);
650 printf ("%-20s [%d]\n",
651 libcfs_nid2str(data.ioc_nid), data.ioc_count);
656 if (errno == ENOENT) {
657 printf ("<no peers>\n");
659 fprintf(stderr, "Error getting peer list: %s: "
667 int jt_ptl_add_peer(int argc, char **argv)
669 struct libcfs_ioctl_data data;
675 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
679 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
684 nid = libcfs_str2nid(argv[1]);
685 if (nid == LNET_NID_ANY) {
686 fprintf (stderr, "Can't parse NID: %s\n", argv[1]);
690 if (lnet_parse_ipaddr (&ip, argv[2]) != 0) {
691 fprintf (stderr, "Can't parse ip addr: %s\n", argv[2]);
695 if (lnet_parse_port (&port, argv[3]) != 0) {
696 fprintf (stderr, "Can't parse port: %s\n", argv[3]);
700 LIBCFS_IOC_INIT(data);
701 data.ioc_net = g_net;
703 data.ioc_u32[0] = ip;
704 data.ioc_u32[1] = port;
706 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
708 fprintf (stderr, "failed to add peer: %s\n",
717 jt_ptl_del_peer (int argc, char **argv)
719 struct libcfs_ioctl_data data;
720 lnet_nid_t nid = LNET_NID_ANY;
721 lnet_pid_t pid = LNET_PID_ANY;
725 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
729 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
731 fprintf (stderr, "usage: %s [nid] [ipaddr]\n",
735 } else if (argc > 2) {
736 fprintf (stderr, "usage: %s [nid]\n", argv[0]);
741 !libcfs_str2anynid(&nid, argv[1])) {
742 fprintf (stderr, "Can't parse nid: %s\n", argv[1]);
746 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
748 lnet_parse_ipaddr (&ip, argv[2]) != 0) {
749 fprintf (stderr, "Can't parse ip addr: %s\n",
755 LIBCFS_IOC_INIT(data);
756 data.ioc_net = g_net;
758 data.ioc_u32[0] = ip;
759 data.ioc_u32[1] = pid;
761 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
763 fprintf (stderr, "failed to remove peer: %s\n",
772 jt_ptl_print_connections (int argc, char **argv)
774 struct libcfs_ioctl_data data;
775 struct lnet_process_id id;
776 char buffer[2][HOST_NAME_MAX + 1];
780 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
783 for (index = 0; ; index++) {
784 LIBCFS_IOC_INIT(data);
785 data.ioc_net = g_net;
786 data.ioc_count = index;
788 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
792 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
793 id.nid = data.ioc_nid;
794 id.pid = data.ioc_u32[6];
795 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
797 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
798 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
799 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
800 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
801 data.ioc_u32[4], /* scheduler */
803 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
804 sizeof(buffer[0]), 1),
806 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
807 sizeof(buffer[1]), 1),
808 data.ioc_u32[1], /* remote port */
809 data.ioc_count, /* tx buffer size */
810 data.ioc_u32[5], /* rx buffer size */
811 data.ioc_flags ? "nagle" : "nonagle");
812 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
813 printf("%s mtu %d\n",
814 libcfs_nid2str(data.ioc_nid),
815 data.ioc_u32[0]); /* path MTU */
816 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
817 printf("%-20s [%d]\n",
818 libcfs_nid2str(data.ioc_nid),
819 data.ioc_u32[0] /* device id */);
821 printf("%s\n", libcfs_nid2str(data.ioc_nid));
826 if (errno == ENOENT) {
827 printf ("<no connections>\n");
829 fprintf(stderr, "Error getting connection list: %s: "
837 int jt_ptl_disconnect(int argc, char **argv)
839 struct libcfs_ioctl_data data;
840 lnet_nid_t nid = LNET_NID_ANY;
845 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
849 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
853 !libcfs_str2anynid(&nid, argv[1])) {
854 fprintf (stderr, "Can't parse nid %s\n", argv[1]);
858 if (g_net_is_compatible (NULL, SOCKLND, 0) &&
860 lnet_parse_ipaddr (&ipaddr, argv[2]) != 0) {
861 fprintf (stderr, "Can't parse ip addr %s\n", argv[2]);
865 LIBCFS_IOC_INIT(data);
866 data.ioc_net = g_net;
868 data.ioc_u32[0] = ipaddr;
870 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
872 fprintf(stderr, "failed to remove connection: %s\n",
880 int jt_ptl_push_connection (int argc, char **argv)
882 struct libcfs_ioctl_data data;
884 lnet_nid_t nid = LNET_NID_ANY;
887 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
891 if (!g_net_is_compatible (argv[0], SOCKLND, GNILND, 0))
895 !libcfs_str2anynid(&nid, argv[1])) {
896 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
900 LIBCFS_IOC_INIT(data);
901 data.ioc_net = g_net;
904 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
906 fprintf(stderr, "failed to push connection: %s\n",
914 int jt_ptl_ping(int argc, char **argv)
918 struct lnet_process_id id;
919 struct lnet_process_id ids[16];
920 int maxids = sizeof(ids)/sizeof(ids[0]);
921 struct libcfs_ioctl_data data;
925 fprintf(stderr, "This command has been deprecated. Plesae use 'lnetctl ping'\n");
928 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
932 sep = strchr(argv[1], '-');
934 rc = lnet_parse_nid(argv[1], &id);
940 if (argv[1][0] == 'u' ||
942 id.pid = strtoul(&argv[1][1], &end, 0) | LNET_PID_USERFLAG;
944 id.pid = strtoul(argv[1], &end, 0);
946 if (end != sep) { /* assuming '-' is part of hostname */
947 rc = lnet_parse_nid(argv[1], &id);
951 id.nid = libcfs_str2nid(sep + 1);
953 if (id.nid == LNET_NID_ANY) {
955 "Can't parse process id \"%s\"\n",
963 timeout = 1000 * atol(argv[2]);
964 if (timeout > 120 * 1000) {
965 fprintf(stderr, "Timeout %s is to large\n",
970 timeout = 1000; /* default 1 second timeout */
972 LIBCFS_IOC_INIT (data);
973 data.ioc_nid = id.nid;
974 data.ioc_u32[0] = id.pid;
975 data.ioc_u32[1] = timeout;
976 data.ioc_plen1 = sizeof(ids);
977 data.ioc_pbuf1 = (char *)ids;
979 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
981 fprintf(stderr, "failed to ping %s: %s\n",
982 id.pid == LNET_PID_ANY ?
983 libcfs_nid2str(id.nid) : libcfs_id2str(id),
988 for (i = 0; i < data.ioc_count && i < maxids; i++)
989 printf("%s\n", libcfs_id2str(ids[i]));
991 if (data.ioc_count > maxids)
992 printf("%d out of %d ids listed\n", maxids, data.ioc_count);
997 int jt_ptl_mynid(int argc, char **argv)
999 struct libcfs_ioctl_data data;
1004 fprintf(stderr, "usage: %s NID\n", argv[0]);
1008 nid = libcfs_str2nid(argv[1]);
1009 if (nid == LNET_NID_ANY) {
1010 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1014 LIBCFS_IOC_INIT(data);
1015 data.ioc_net = LNET_NIDNET(nid);
1018 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1020 fprintf(stderr, "setting my NID failed: %s\n",
1023 printf("registered my nid %s\n", libcfs_nid2str(nid));
1029 jt_ptl_fail_nid (int argc, char **argv)
1034 struct libcfs_ioctl_data data;
1036 if (argc < 2 || argc > 3)
1038 fprintf (stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n", argv[0]);
1042 if (!libcfs_str2anynid(&nid, argv[1]))
1044 fprintf (stderr, "Can't parse nid \"%s\"\n", argv[1]);
1049 threshold = LNET_MD_THRESH_INF;
1050 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1051 fprintf (stderr, "Can't parse count \"%s\"\n", argv[2]);
1055 LIBCFS_IOC_INIT (data);
1057 data.ioc_count = threshold;
1059 rc = l_ioctl (LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1061 fprintf (stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1064 printf ("%s %s\n", threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1070 jt_ptl_add_route (int argc, char **argv)
1072 struct lnet_ioctl_config_data data;
1073 lnet_nid_t gateway_nid;
1074 __u32 hops = LNET_UNDEFINED_HOPS;
1075 unsigned int priority = 0;
1079 if (argc < 2 || argc > 4) {
1080 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1085 if (g_net_is_set(argv[0]) == 0)
1088 gateway_nid = libcfs_str2nid(argv[1]);
1089 if (gateway_nid == LNET_NID_ANY) {
1090 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1095 hops = strtol(argv[2], &end, 0);
1096 if (hops == 0 || hops >= 256 ||
1097 (end != NULL && *end != 0)) {
1098 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1103 priority = strtoul(argv[3], &end, 0);
1104 if (end != NULL && *end != 0) {
1106 "Can't parse priority \"%s\"\n",
1113 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1114 data.cfg_net = g_net;
1115 data.cfg_config_u.cfg_route.rtr_hop = hops;
1116 data.cfg_nid = gateway_nid;
1117 data.cfg_config_u.cfg_route.rtr_priority = priority;
1119 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1121 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1130 jt_ptl_del_route (int argc, char **argv)
1132 struct lnet_ioctl_config_data data;
1137 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1141 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1142 fprintf(stderr, "Can't parse gateway NID "
1143 "\"%s\"\n", argv[1]);
1147 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1148 data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
1151 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1153 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1154 libcfs_nid2str(nid), strerror(errno));
1162 jt_ptl_notify_router (int argc, char **argv)
1164 struct libcfs_ioctl_data data;
1173 fprintf (stderr, "usage: %s targetNID <up/down> [<time>]\n",
1178 nid = libcfs_str2nid(argv[1]);
1179 if (nid == LNET_NID_ANY) {
1180 fprintf (stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1184 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1185 fprintf (stderr, "Can't parse boolean %s\n", argv[2]);
1189 gettimeofday(&now, NULL);
1193 } else if (lnet_parse_time (&when, argv[3]) != 0) {
1194 fprintf(stderr, "Can't parse time %s\n"
1195 "Please specify either 'YYYY-MM-DD-HH:MM:SS'\n"
1196 "or an absolute unix time in seconds\n", argv[3]);
1198 } else if (when > now.tv_sec) {
1199 fprintf (stderr, "%s specifies a time in the future\n",
1204 LIBCFS_IOC_INIT(data);
1206 data.ioc_flags = enable;
1207 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1208 data.ioc_u64[0] = (__u64)when;
1210 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1212 fprintf (stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1213 libcfs_nid2str(nid), strerror (errno));
1221 jt_ptl_print_routes (int argc, char **argv)
1223 struct lnet_ioctl_config_data data;
1232 fprintf(stderr, "This command has been deprecated. Plesae use 'lnetctl route show'\n");
1234 for (index = 0; ; index++) {
1235 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1236 data.cfg_count = index;
1238 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1243 hops = data.cfg_config_u.cfg_route.rtr_hop;
1245 alive = data.cfg_config_u.cfg_route.rtr_flags;
1246 pri = data.cfg_config_u.cfg_route.rtr_priority;
1248 printf("net %18s hops %u gw %32s %s pri %u\n",
1249 libcfs_net2str(net), hops,
1250 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1253 if (errno != ENOENT)
1254 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1261 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
1267 /* NB: can't support range ipaddress except * and *@net */
1268 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
1269 net = libcfs_str2net(str + 2);
1270 if (net == LNET_NIDNET(LNET_NID_ANY))
1273 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
1275 rc = libcfs_str2anynid(&nid, str);
1283 fprintf(stderr, "Invalid NID : %s\n", str);
1288 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
1290 if (!strcasecmp(msg_str, "put")) {
1291 *mask_p |= LNET_PUT_BIT;
1294 } else if (!strcasecmp(msg_str, "ack")) {
1295 *mask_p |= LNET_ACK_BIT;
1298 } else if (!strcasecmp(msg_str, "get")) {
1299 *mask_p |= LNET_GET_BIT;
1302 } else if (!strcasecmp(msg_str, "reply")) {
1303 *mask_p |= LNET_REPLY_BIT;
1307 fprintf(stderr, "unknown message type %s\n", msg_str);
1312 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
1314 unsigned long rc = strtoul(optarg, NULL, 0);
1317 fprintf(stderr, "invalid portal: %lu\n", rc);
1321 *mask_p |= (1ULL << rc);
1326 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
1328 struct libcfs_ioctl_data data = { { 0 } };
1329 struct lnet_fault_attr attr;
1333 static const struct option opts[] = {
1334 { .name = "source", .has_arg = required_argument, .val = 's' },
1335 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1336 { .name = "rate", .has_arg = required_argument, .val = 'r' },
1337 { .name = "interval", .has_arg = required_argument, .val = 'i' },
1338 { .name = "latency", .has_arg = required_argument, .val = 'l' },
1339 { .name = "portal", .has_arg = required_argument, .val = 'p' },
1340 { .name = "message", .has_arg = required_argument, .val = 'm' },
1344 fprintf(stderr, "Failed, please provide source, destination "
1345 "and rate of rule\n");
1349 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:r:i:p:m:" : "s:d:r:l:p:m:";
1350 memset(&attr, 0, sizeof(attr));
1352 char c = getopt_long(argc, argv, optstr, opts, NULL);
1358 case 's': /* source NID/NET */
1359 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1364 case 'd': /* dest NID/NET */
1365 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1370 case 'r': /* drop rate */
1371 if (opc == LNET_CTL_DROP_ADD)
1372 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
1374 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
1377 case 'i': /* time interval (# seconds) for message drop */
1378 if (opc == LNET_CTL_DROP_ADD)
1379 attr.u.drop.da_interval = strtoul(optarg,
1382 attr.u.delay.la_interval = strtoul(optarg,
1386 case 'l': /* seconds to wait before activating rule */
1387 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
1390 case 'p': /* portal to filter */
1391 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
1396 case 'm': /* message types to filter */
1397 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
1403 fprintf(stderr, "error: %s: option '%s' "
1404 "unrecognized\n", argv[0], argv[optind - 1]);
1410 if (opc == LNET_CTL_DROP_ADD) {
1411 /* NB: drop rate and interval are exclusive to each other */
1412 if (!((attr.u.drop.da_rate == 0) ^
1413 (attr.u.drop.da_interval == 0))) {
1415 "please provide either drop rate or interval "
1416 "but not both at the same time.\n");
1419 } else if (opc == LNET_CTL_DELAY_ADD) {
1420 if (!((attr.u.delay.la_rate == 0) ^
1421 (attr.u.delay.la_interval == 0))) {
1423 "please provide either delay rate or interval "
1424 "but not both at the same time.\n");
1428 if (attr.u.delay.la_latency == 0) {
1429 fprintf(stderr, "latency cannot be zero\n");
1434 if (attr.fa_src == 0 || attr.fa_dst == 0) {
1435 fprintf(stderr, "Please provide both source and destination "
1436 "of %s rule\n", name);
1440 data.ioc_flags = opc;
1441 data.ioc_inllen1 = sizeof(attr);
1442 data.ioc_inlbuf1 = (char *)&attr;
1443 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1444 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1448 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1450 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
1451 name, libcfs_nid2str(attr.fa_src),
1452 libcfs_nid2str(attr.fa_dst), strerror(errno));
1456 printf("Added %s rule %s->%s (1/%d)\n",
1457 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
1458 opc == LNET_CTL_DROP_ADD ?
1459 attr.u.drop.da_rate : attr.u.delay.la_rate);
1468 jt_ptl_drop_add(int argc, char **argv)
1470 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
1474 jt_ptl_delay_add(int argc, char **argv)
1476 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
1480 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
1482 struct libcfs_ioctl_data data = { { 0 } };
1483 struct lnet_fault_attr attr;
1487 static const struct option opts[] = {
1488 { .name = "source", .has_arg = required_argument, .val = 's' },
1489 { .name = "dest", .has_arg = required_argument, .val = 'd' },
1490 { .name = "all", .has_arg = no_argument, .val = 'a' },
1494 fprintf(stderr, "Failed, please provide source and "
1495 "destination of rule\n");
1499 memset(&attr, 0, sizeof(attr));
1501 char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
1508 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
1513 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
1518 attr.fa_src = attr.fa_dst = 0;
1522 fprintf(stderr, "error: %s: option '%s' "
1523 "unrecognized\n", argv[0], argv[optind - 1]);
1529 data.ioc_flags = opc;
1530 data.ioc_inllen1 = sizeof(attr);
1531 data.ioc_inlbuf1 = (char *)&attr;
1532 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1533 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1537 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1539 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
1540 all ? "all" : libcfs_nid2str(attr.fa_src),
1541 all ? "all" : libcfs_nid2str(attr.fa_dst),
1546 libcfs_ioctl_unpack(&data, ioc_buf);
1547 printf("Removed %d %s rules\n", data.ioc_count, name);
1556 jt_ptl_drop_del(int argc, char **argv)
1558 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
1562 jt_ptl_delay_del(int argc, char **argv)
1564 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
1568 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
1570 struct libcfs_ioctl_data data = { { 0 } };
1573 LIBCFS_IOC_INIT(data);
1574 data.ioc_flags = opc;
1576 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
1578 fprintf(stderr, "failed to reset %s stats: %s\n",
1579 name, strerror(errno));
1586 jt_ptl_drop_reset(int argc, char **argv)
1588 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
1592 jt_ptl_delay_reset(int argc, char **argv)
1594 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
1599 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
1601 struct libcfs_ioctl_data data = { { 0 } };
1602 struct lnet_fault_attr attr;
1603 struct lnet_fault_stat stat;
1606 printf("LNet %s rules:\n", name);
1607 for (pos = 0;; pos++) {
1610 memset(&attr, 0, sizeof(attr));
1611 memset(&stat, 0, sizeof(stat));
1613 data.ioc_count = pos;
1614 data.ioc_flags = opc;
1615 data.ioc_inllen1 = sizeof(attr);
1616 data.ioc_inlbuf1 = (char *)&attr;
1617 data.ioc_inllen2 = sizeof(stat);
1618 data.ioc_inlbuf2 = (char *)&stat;
1619 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
1620 fprintf(stderr, "libcfs_ioctl_pack failed\n");
1624 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
1628 libcfs_ioctl_unpack(&data, ioc_buf);
1630 if (opc == LNET_CTL_DROP_LIST) {
1631 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, "
1632 "%ju/%ju, PUT %ju, ACK %ju, GET "
1634 libcfs_nid2str(attr.fa_src),
1635 libcfs_nid2str(attr.fa_dst),
1636 attr.u.drop.da_rate, attr.u.drop.da_interval,
1637 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1638 (uintmax_t)stat.u.drop.ds_dropped,
1639 (uintmax_t)stat.fs_count,
1640 (uintmax_t)stat.fs_put,
1641 (uintmax_t)stat.fs_ack,
1642 (uintmax_t)stat.fs_get,
1643 (uintmax_t)stat.fs_reply);
1645 } else if (opc == LNET_CTL_DELAY_LIST) {
1646 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx"
1647 ", msg %x, %ju/%ju, PUT %ju"
1648 ", ACK %ju, GET %ju, REP %ju\n",
1649 libcfs_nid2str(attr.fa_src),
1650 libcfs_nid2str(attr.fa_dst),
1651 attr.u.delay.la_rate, attr.u.delay.la_interval,
1652 attr.u.delay.la_latency,
1653 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
1654 (uintmax_t)stat.u.delay.ls_delayed,
1655 (uintmax_t)stat.fs_count,
1656 (uintmax_t)stat.fs_put,
1657 (uintmax_t)stat.fs_ack,
1658 (uintmax_t)stat.fs_get,
1659 (uintmax_t)stat.fs_reply);
1662 printf("found total %d\n", pos);
1668 jt_ptl_drop_list(int argc, char **argv)
1670 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
1674 jt_ptl_delay_list(int argc, char **argv)
1676 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
1680 get_cycles_per_usec ()
1682 FILE *f = fopen ("/proc/cpuinfo", "r");
1687 while (fgets (line, sizeof (line), f) != NULL)
1688 if (sscanf (line, "cpu MHz : %lf", &mhz) == 1) {
1695 fprintf (stderr, "Can't read/parse /proc/cpuinfo\n");
1699 int jt_ptl_testprotocompat(int argc, char **argv)
1701 struct libcfs_ioctl_data data;
1707 fprintf(stderr, "usage: %s <number>\n", argv[0]);
1711 flags = strtol(argv[1], &end, 0);
1712 if (flags < 0 || *end != 0) {
1713 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
1717 LIBCFS_IOC_INIT(data);
1718 data.ioc_flags = flags;
1719 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
1722 fprintf(stderr, "test proto compat %x failed: %s\n",
1723 flags, strerror(errno));
1727 printf("test proto compat %x OK\n", flags);