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.whamcloud.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/ioctl.h>
39 #include <linux/lnet/libcfs_debug.h>
40 #include <linux/lnet/lnet-dlc.h>
41 #include <linux/lnet/lnetctl.h>
42 #include <linux/lnet/nidstr.h>
43 #include <linux/lnet/socklnd.h>
44 #include <lnetconfig/liblnetconfig.h>
45 #include <lustre/lustreapi.h>
46 #include <lustre_ioctl_old.h>
48 unsigned int libcfs_debug;
49 unsigned int libcfs_printk = D_CANTMASK;
51 static bool g_net_interactive;
52 static bool g_net_set;
55 #define IOC_BUF_SIZE 8192
56 static char local_buf[IOC_BUF_SIZE];
57 static char *ioc_buf = local_buf;
59 /* Convert a string boolean to an int; "enable" -> 1 */
61 lnet_parse_bool (int *b, char *str)
63 if (!strcasecmp(str, "no") ||
64 !strcasecmp(str, "n") ||
65 !strcasecmp(str, "off") ||
66 !strcasecmp(str, "down") ||
67 !strcasecmp(str, "disable")) {
73 if (!strcasecmp(str, "yes") ||
74 !strcasecmp(str, "y") ||
75 !strcasecmp(str, "on") ||
76 !strcasecmp(str, "up") ||
77 !strcasecmp(str, "enable")) {
87 lnet_parse_port(int *port, char *str)
91 *port = strtol(str, &end, 0);
93 if (*end == 0 && /* parsed whole string */
94 *port > 0 && *port < 65536) /* minimal sanity check */
100 #ifdef HAVE_GETHOSTBYNAME
101 static struct hostent *
102 ptl_gethostbyname(char *hname)
106 he = gethostbyname(hname);
111 fprintf(stderr, "Unable to resolve hostname: %s\n",
115 fprintf(stderr, "gethostbyname error for %s: %s\n",
116 hname, strerror(h_errno));
126 lnet_parse_ipquad(__u32 *ipaddrp, char *str)
130 if (sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
131 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
132 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
133 *ipaddrp = (a << 24) | (b << 16) | (c << 8) | d;
142 lnet_parse_ipaddr(__u32 *ipaddrp, char *str)
144 #ifdef HAVE_GETHOSTBYNAME
148 if (!strcmp(str, "_all_")) {
153 if (lnet_parse_ipquad(ipaddrp, str) == 0)
156 #ifdef HAVE_GETHOSTBYNAME
157 if ((('a' <= str[0] && str[0] <= 'z') ||
158 ('A' <= str[0] && str[0] <= 'Z')) &&
159 (he = ptl_gethostbyname(str)) != NULL) {
160 __u32 addr = *(__u32 *)he->h_addr;
162 *ipaddrp = ntohl(addr); /* HOST byte order */
171 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
173 #ifdef HAVE_GETHOSTBYNAME
178 net_ip = htonl(ipaddr);
179 he = gethostbyaddr(&net_ip, sizeof(net_ip), AF_INET);
181 snprintf(str, strsize, "%s", he->h_name);
187 sprintf(str, "%d.%d.%d.%d",
188 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
189 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
194 lnet_parse_time(time_t *t, char *str)
200 *t = strtol(str, &end, 0);
201 if (*end == 0) /* parsed whole string */
204 memset(&tm, 0, sizeof(tm));
205 n = sscanf(str, "%d-%d-%d-%d:%d:%d",
206 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
207 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
211 tm.tm_mon--; /* convert to 0 == Jan */
212 tm.tm_year -= 1900; /* y2k quirk */
213 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
216 if (*t == (time_t)-1)
223 lnet_parse_nid(char *nid_str, struct lnet_process_id *id_ptr)
225 id_ptr->pid = LNET_PID_ANY;
226 id_ptr->nid = libcfs_str2nid(nid_str);
227 if (id_ptr->nid == LNET_NID_ANY) {
228 fprintf(stderr, "Can't parse nid \"%s\"\n", nid_str);
235 static int g_net_is_set(char *cmd)
243 if (g_net_interactive)
249 "You must run '%s <network>' command before '%s'\n",
257 static int g_net_is_compatible(char *cmd, ...)
262 if (!g_net_is_set(cmd))
268 nal = va_arg(ap, int);
269 if (nal == LNET_NETTYP(g_net)) {
278 fprintf(stderr, "Command %s not compatible with %s NAL\n",
279 cmd, 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);
294 int jt_ptl_network(int argc, char **argv)
296 struct libcfs_ioctl_data data;
297 __u32 net = LNET_NET_ANY;
301 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
305 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
306 LIBCFS_IOC_INIT(data);
307 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
310 printf("LNET ready to unload\n");
314 if (errno == ENODEV) {
315 printf("LNET is currently not loaded.");
320 fprintf(stderr, "LNET busy\n");
322 fprintf(stderr, "LNET unconfigure error %d: %s\n",
323 errno, strerror(errno));
325 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
326 LIBCFS_IOC_INIT(data);
327 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
330 printf("LNET configured\n");
334 fprintf(stderr, "LNET configure error %d: %s\n",
335 errno, strerror(errno));
339 net = libcfs_str2net(argv[1]);
340 if (net == LNET_NET_ANY) {
341 fprintf(stderr, "Can't parse net %s\n", argv[1]);
350 #ifndef IOC_LIBCFS_GET_NI
351 #define IOC_LIBCFS_GET_NI _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
355 jt_ptl_list_nids(int argc, char **argv)
357 int all = 0, return_nid = 0;
358 yaml_emitter_t request;
365 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
366 /* Hack to pass back value */
367 return_nid = (argc == 2) && (argv[1][0] == 1);
369 if ((argc > 2) && !(all || return_nid)) {
370 fprintf(stderr, "usage: %s [all]\n", argv[0]);
374 sk = nl_socket_alloc();
378 /* Setup parser to receive Netlink packets */
379 rc = yaml_parser_initialize(&reply);
381 yaml_parser_log_error(&reply, stderr, NULL);
385 rc = yaml_parser_set_input_netlink(&reply, sk, false);
387 yaml_parser_log_error(&reply, stderr, NULL);
388 yaml_parser_delete(&reply);
392 /* Create Netlink emitter to send request to kernel */
393 rc = yaml_emitter_initialize(&request);
395 yaml_parser_log_error(&reply, stderr, NULL);
396 yaml_parser_delete(&reply);
400 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME, 1,
401 LNET_CMD_NETS, NLM_F_DUMP);
403 yaml_emitter_log_error(&request, stderr);
404 yaml_emitter_delete(&request);
405 yaml_parser_delete(&reply);
409 yaml_emitter_open(&request);
410 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
411 rc = yaml_emitter_emit(&request, &event);
415 yaml_mapping_start_event_initialize(&event, NULL,
416 (yaml_char_t *)YAML_MAP_TAG,
417 1, YAML_ANY_MAPPING_STYLE);
418 rc = yaml_emitter_emit(&request, &event);
422 yaml_scalar_event_initialize(&event, NULL,
423 (yaml_char_t *)YAML_STR_TAG,
424 (yaml_char_t *)"net",
426 YAML_PLAIN_SCALAR_STYLE);
427 rc = yaml_emitter_emit(&request, &event);
432 if (!g_net_set || g_net == LNET_NET_ANY) {
433 yaml_scalar_event_initialize(&event, NULL,
434 (yaml_char_t *)YAML_STR_TAG,
437 YAML_PLAIN_SCALAR_STYLE);
438 rc = yaml_emitter_emit(&request, &event);
442 char *net_id = libcfs_net2str(g_net);
444 yaml_sequence_start_event_initialize(&event, NULL,
445 (yaml_char_t *)YAML_SEQ_TAG,
446 1, YAML_ANY_SEQUENCE_STYLE);
447 rc = yaml_emitter_emit(&request, &event);
451 yaml_mapping_start_event_initialize(&event, NULL,
452 (yaml_char_t *)YAML_MAP_TAG,
453 1, YAML_ANY_MAPPING_STYLE);
454 rc = yaml_emitter_emit(&request, &event);
458 yaml_scalar_event_initialize(&event, NULL,
459 (yaml_char_t *)YAML_STR_TAG,
460 (yaml_char_t *)"net type",
462 1, 0, YAML_PLAIN_SCALAR_STYLE);
463 rc = yaml_emitter_emit(&request, &event);
467 yaml_scalar_event_initialize(&event, NULL,
468 (yaml_char_t *)YAML_STR_TAG,
469 (yaml_char_t *)net_id,
470 strlen(net_id), 1, 0,
471 YAML_PLAIN_SCALAR_STYLE);
472 rc = yaml_emitter_emit(&request, &event);
476 yaml_mapping_end_event_initialize(&event);
477 rc = yaml_emitter_emit(&request, &event);
481 yaml_sequence_end_event_initialize(&event);
482 rc = yaml_emitter_emit(&request, &event);
486 yaml_mapping_end_event_initialize(&event);
487 rc = yaml_emitter_emit(&request, &event);
491 yaml_document_end_event_initialize(&event, 0);
492 rc = yaml_emitter_emit(&request, &event);
496 rc = yaml_emitter_close(&request);
499 yaml_emitter_log_error(&request, stderr);
502 yaml_emitter_delete(&request);
505 rc = yaml_parser_parse(&reply, &event);
509 if (event.type == YAML_SCALAR_EVENT &&
510 strcmp((char *)event.data.scalar.value, "nid") == 0) {
513 yaml_event_delete(&event);
514 rc = yaml_parser_parse(&reply, &event);
516 yaml_event_delete(&event);
520 tmp = (char *)event.data.scalar.value;
521 if (all || strcmp(tmp, "0@lo") != 0) {
524 *(__u64 *)(argv[1]) = libcfs_str2nid(tmp);
529 done = (event.type == YAML_STREAM_END_EVENT);
530 yaml_event_delete(&event);
534 yaml_parser_log_error(&reply, stderr, NULL);
535 yaml_parser_delete(&reply);
537 #ifdef IOC_LIBCFS_GET_NI
545 for (count = 0;; count++) {
546 struct libcfs_ioctl_data data;
548 LIBCFS_IOC_INIT(data);
549 data.ioc_count = count;
550 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
553 if ((count > 0) && (errno == ENOENT))
554 /* We found them all */
556 fprintf(stderr, "IOC_LIBCFS_GET_NI error %d: %s\n",
557 errno, strerror(errno));
561 if (all || (data.ioc_nid != LNET_NID_LO_0)) {
562 printf("%s\n", libcfs_nid2str(data.ioc_nid));
564 *(__u64 *)(argv[1]) = data.ioc_nid;
578 jt_ptl_which_nid(int argc, char **argv)
580 struct libcfs_ioctl_data data;
583 lnet_nid_t best_nid = LNET_NID_ANY;
592 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
596 for (i = 1; i < argc; i++) {
598 nid = libcfs_str2nid(nidstr);
599 if (nid == LNET_NID_ANY) {
600 fprintf(stderr, "Can't parse NID %s\n", nidstr);
604 LIBCFS_IOC_INIT(data);
607 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
609 fprintf(stderr, "Can't get distance to %s: %s\n",
610 nidstr, strerror(errno));
614 dist = data.ioc_u32[0];
615 order = data.ioc_u32[1];
618 if (dist == -EHOSTUNREACH)
621 fprintf(stderr, "Unexpected distance to %s: %d\n",
626 if (best_nid == LNET_NID_ANY ||
628 (dist == best_dist && order < best_order)) {
635 if (best_nid == LNET_NID_ANY) {
636 fprintf(stderr, "No reachable NID\n");
640 printf("%s\n", libcfs_nid2str(best_nid));
645 jt_ptl_print_interfaces(int argc, char **argv)
647 struct libcfs_ioctl_data data;
648 char buffer[3][HOST_NAME_MAX + 1];
652 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
655 for (index = 0; ; index++) {
656 LIBCFS_IOC_INIT(data);
657 data.ioc_net = g_net;
658 data.ioc_count = index;
660 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
664 printf("%s: (%s/%s) npeer %d nroute %d\n",
665 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
666 sizeof(buffer[2]), 1),
667 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
668 sizeof(buffer[0]), 0),
669 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
670 sizeof(buffer[1]), 0),
671 data.ioc_u32[2], data.ioc_u32[3]);
675 if (errno == ENOENT) {
676 printf("<no interfaces>\n");
679 "Error getting interfaces: %s: check dmesg.\n",
688 jt_ptl_add_interface(int argc, char **argv)
690 struct libcfs_ioctl_data data;
693 __u32 netmask = 0xffffff00;
698 if (argc < 2 || argc > 3) {
699 fprintf(stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
703 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
706 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
707 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
712 count = strtol(argv[2], &end, 0);
713 if (count > 0 && count < 32 && *end == 0) {
715 for (i = count; i > 0; i--)
716 netmask = netmask | (1 << (32 - i));
717 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
718 fprintf(stderr, "Can't parse netmask: %s\n", argv[2]);
723 LIBCFS_IOC_INIT(data);
724 data.ioc_net = g_net;
725 data.ioc_u32[0] = ipaddr;
726 data.ioc_u32[1] = netmask;
728 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
730 fprintf(stderr, "failed to add interface: %s\n",
739 jt_ptl_del_interface(int argc, char **argv)
741 struct libcfs_ioctl_data data;
746 fprintf(stderr, "usage: %s [ipaddr]\n", argv[0]);
750 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
754 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
755 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
759 LIBCFS_IOC_INIT(data);
760 data.ioc_net = g_net;
761 data.ioc_u32[0] = ipaddr;
763 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
765 fprintf(stderr, "failed to delete interface: %s\n",
774 jt_ptl_print_peers(int argc, char **argv)
776 struct libcfs_ioctl_data data;
777 struct lnet_process_id id;
778 char buffer[2][HOST_NAME_MAX + 1];
782 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
786 for (index = 0; ; index++) {
787 LIBCFS_IOC_INIT(data);
788 data.ioc_net = g_net;
789 data.ioc_count = index;
791 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
795 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
796 id.nid = data.ioc_nid;
797 id.pid = data.ioc_u32[4];
798 printf("%-20s [%d]%s->%s:%d #%d\n",
800 data.ioc_count, /* persistence */
802 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
803 sizeof(buffer[0]), 1),
805 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
806 sizeof(buffer[1]), 1),
807 data.ioc_u32[1], /* peer port */
808 data.ioc_u32[3]); /* conn_count */
809 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
810 int disconn = data.ioc_flags >> 16;
816 state = data.ioc_flags & 0xffff ? "C" : "U";
818 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
819 libcfs_nid2str(data.ioc_nid), /* peer nid */
820 data.ioc_net, /* gemini device id */
821 state, /* peer is Connecting, Up, or Down */
822 data.ioc_count, /* peer refcount */
823 (uintmax_t)data.ioc_u64[0], /* peerstamp */
824 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
825 /* fmaq, nfma, nrdma */
826 data.ioc_u32[0], data.ioc_u32[1],
829 printf("%-20s [%d]\n",
830 libcfs_nid2str(data.ioc_nid), data.ioc_count);
835 if (errno == ENOENT) {
836 printf("<no peers>\n");
839 "Error getting peer list: %s: check dmesg.\n",
846 int jt_ptl_add_peer(int argc, char **argv)
848 struct libcfs_ioctl_data data;
854 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
858 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
863 nid = libcfs_str2nid(argv[1]);
864 if (nid == LNET_NID_ANY) {
865 fprintf(stderr, "Can't parse NID: %s\n", argv[1]);
869 if (lnet_parse_ipaddr(&ip, argv[2]) != 0) {
870 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
874 if (lnet_parse_port(&port, argv[3]) != 0) {
875 fprintf(stderr, "Can't parse port: %s\n", argv[3]);
879 LIBCFS_IOC_INIT(data);
880 data.ioc_net = g_net;
882 data.ioc_u32[0] = ip;
883 data.ioc_u32[1] = port;
885 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
887 fprintf(stderr, "failed to add peer: %s\n",
896 jt_ptl_del_peer(int argc, char **argv)
898 struct libcfs_ioctl_data data;
899 lnet_nid_t nid = LNET_NID_ANY;
900 lnet_pid_t pid = LNET_PID_ANY;
904 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
908 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
910 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
913 } else if (argc > 2) {
914 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
918 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
919 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
923 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
924 if (argc > 2 && lnet_parse_ipaddr(&ip, argv[2]) != 0) {
925 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
930 LIBCFS_IOC_INIT(data);
931 data.ioc_net = g_net;
933 data.ioc_u32[0] = ip;
934 data.ioc_u32[1] = pid;
936 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
938 fprintf(stderr, "failed to remove peer: %s\n", strerror(errno));
946 jt_ptl_print_connections(int argc, char **argv)
948 struct libcfs_ioctl_data data;
949 struct lnet_process_id id;
950 char buffer[2][HOST_NAME_MAX + 1];
954 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
957 for (index = 0; ; index++) {
958 LIBCFS_IOC_INIT(data);
959 data.ioc_net = g_net;
960 data.ioc_count = index;
962 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
966 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
967 id.nid = data.ioc_nid;
968 id.pid = data.ioc_u32[6];
969 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
971 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
972 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
973 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
974 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
975 data.ioc_u32[4], /* scheduler */
977 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
978 sizeof(buffer[0]), 1),
980 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
981 sizeof(buffer[1]), 1),
982 data.ioc_u32[1], /* remote port */
983 data.ioc_count, /* tx buffer size */
984 data.ioc_u32[5], /* rx buffer size */
985 data.ioc_flags ? "nagle" : "nonagle");
986 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
987 printf("%s mtu %d\n",
988 libcfs_nid2str(data.ioc_nid),
989 data.ioc_u32[0]); /* path MTU */
990 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
991 printf("%-20s [%d]\n",
992 libcfs_nid2str(data.ioc_nid),
993 data.ioc_u32[0] /* device id */);
995 printf("%s\n", libcfs_nid2str(data.ioc_nid));
1000 if (errno == ENOENT) {
1001 printf("<no connections>\n");
1004 "Error getting connection list: %s: check dmesg.\n",
1011 int jt_ptl_disconnect(int argc, char **argv)
1013 struct libcfs_ioctl_data data;
1014 lnet_nid_t nid = LNET_NID_ANY;
1019 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1023 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
1026 if (argc >= 2 && !libcfs_str2anynid(&nid, argv[1])) {
1027 fprintf(stderr, "Can't parse nid %s\n", argv[1]);
1031 if (g_net_is_compatible(NULL, SOCKLND, 0) && argc >= 3 &&
1032 lnet_parse_ipaddr(&ipaddr, argv[2]) != 0) {
1033 fprintf(stderr, "Can't parse ip addr %s\n", argv[2]);
1037 LIBCFS_IOC_INIT(data);
1038 data.ioc_net = g_net;
1040 data.ioc_u32[0] = ipaddr;
1042 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
1044 fprintf(stderr, "failed to remove connection: %s\n",
1052 int jt_ptl_push_connection(int argc, char **argv)
1054 struct libcfs_ioctl_data data;
1056 lnet_nid_t nid = LNET_NID_ANY;
1059 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
1063 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
1066 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
1067 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1071 LIBCFS_IOC_INIT(data);
1072 data.ioc_net = g_net;
1075 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
1077 fprintf(stderr, "failed to push connection: %s\n",
1085 #ifndef IOC_LIBCFS_PING_PEER
1086 #define IOC_LIBCFS_PING_PEER _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
1089 int jt_ptl_ping(int argc, char **argv)
1091 bool done = false, print = true;
1094 struct lnet_process_id id;
1095 yaml_emitter_t request;
1096 yaml_parser_t reply;
1102 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
1106 sep = strchr(argv[1], '-');
1108 rc = lnet_parse_nid(argv[1], &id);
1114 if (argv[1][0] == 'u' || argv[1][0] == 'U')
1115 id.pid = strtoul(&argv[1][1], &end, 0) |
1118 id.pid = strtoul(argv[1], &end, 0);
1120 if (end != sep) { /* assuming '-' is part of hostname */
1121 rc = lnet_parse_nid(argv[1], &id);
1125 id.nid = libcfs_str2nid(sep + 1);
1127 if (id.nid == LNET_NID_ANY) {
1129 "Can't parse process id \"%s\"\n",
1137 timeout = 1000 * atol(argv[2]);
1138 if (timeout > 120 * 1000) {
1139 fprintf(stderr, "Timeout %s is to large\n",
1144 timeout = 1000; /* default 1 second timeout */
1147 /* Create Netlink emitter to send request to kernel */
1148 sk = nl_socket_alloc();
1152 /* Setup parser to recieve Netlink packets */
1153 rc = yaml_parser_initialize(&reply);
1157 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1161 /* Create Netlink emitter to send request to kernel */
1162 rc = yaml_emitter_initialize(&request);
1166 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
1167 LNET_GENL_VERSION, LNET_CMD_PING,
1172 yaml_emitter_open(&request);
1173 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1174 rc = yaml_emitter_emit(&request, &event);
1178 yaml_mapping_start_event_initialize(&event, NULL,
1179 (yaml_char_t *)YAML_MAP_TAG,
1180 1, YAML_ANY_MAPPING_STYLE);
1181 rc = yaml_emitter_emit(&request, &event);
1185 yaml_scalar_event_initialize(&event, NULL,
1186 (yaml_char_t *)YAML_STR_TAG,
1187 (yaml_char_t *)"ping",
1188 strlen("ping"), 1, 0,
1189 YAML_PLAIN_SCALAR_STYLE);
1190 rc = yaml_emitter_emit(&request, &event);
1194 yaml_mapping_start_event_initialize(&event, NULL,
1195 (yaml_char_t *)YAML_MAP_TAG,
1196 1, YAML_ANY_MAPPING_STYLE);
1197 rc = yaml_emitter_emit(&request, &event);
1201 if (timeout != 1000) {
1202 yaml_scalar_event_initialize(&event, NULL,
1203 (yaml_char_t *)YAML_STR_TAG,
1204 (yaml_char_t *)"timeout",
1205 strlen("timeout"), 1, 0,
1206 YAML_PLAIN_SCALAR_STYLE);
1207 rc = yaml_emitter_emit(&request, &event);
1211 yaml_scalar_event_initialize(&event, NULL,
1212 (yaml_char_t *)YAML_INT_TAG,
1213 (yaml_char_t *)argv[2],
1214 strlen(argv[2]), 1, 0,
1215 YAML_PLAIN_SCALAR_STYLE);
1216 rc = yaml_emitter_emit(&request, &event);
1221 yaml_scalar_event_initialize(&event, NULL,
1222 (yaml_char_t *)YAML_STR_TAG,
1223 (yaml_char_t *)"nids",
1224 strlen("nids"), 1, 0,
1225 YAML_PLAIN_SCALAR_STYLE);
1226 rc = yaml_emitter_emit(&request, &event);
1230 yaml_sequence_start_event_initialize(&event, NULL,
1231 (yaml_char_t *)YAML_SEQ_TAG,
1232 1, YAML_FLOW_SEQUENCE_STYLE);
1233 rc = yaml_emitter_emit(&request, &event);
1237 /* convert NID to string, in case libcfs_str2nid() did name lookup */
1238 yaml_scalar_event_initialize(&event, NULL,
1239 (yaml_char_t *)YAML_STR_TAG,
1240 (yaml_char_t *)libcfs_nid2str(id.nid),
1241 strlen(libcfs_nid2str(id.nid)), 1, 0,
1242 YAML_PLAIN_SCALAR_STYLE);
1243 rc = yaml_emitter_emit(&request, &event);
1247 yaml_sequence_end_event_initialize(&event);
1248 rc = yaml_emitter_emit(&request, &event);
1252 yaml_mapping_end_event_initialize(&event);
1253 rc = yaml_emitter_emit(&request, &event);
1257 yaml_mapping_end_event_initialize(&event);
1258 rc = yaml_emitter_emit(&request, &event);
1262 yaml_document_end_event_initialize(&event, 0);
1263 rc = yaml_emitter_emit(&request, &event);
1267 rc = yaml_emitter_close(&request);
1270 yaml_emitter_log_error(&request, stderr);
1274 yaml_emitter_delete(&request);
1276 /* Now parse the reply results */
1278 rc = yaml_parser_parse(&reply, &event);
1282 if (event.type != YAML_SCALAR_EVENT)
1285 if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
1286 yaml_event_delete(&event);
1287 rc = yaml_parser_parse(&reply, &event);
1289 yaml_event_delete(&event);
1293 /* Print 0@lo. Its not sent */
1294 printf("12345-0@lo\n");
1297 printf("%s\n", (char *)event.data.scalar.value);
1298 } else if (strcmp((char *)event.data.scalar.value,
1300 yaml_event_delete(&event);
1301 rc = yaml_parser_parse(&reply, &event);
1303 yaml_event_delete(&event);
1306 rc = strtol((char *)event.data.scalar.value, NULL, 10);
1307 fprintf(stdout, "failed to ping %s: %s\n",
1308 argv[1], strerror(-rc));
1309 break; /* "rc" is clobbered if loop is run again */
1312 done = (event.type == YAML_STREAM_END_EVENT);
1313 yaml_event_delete(&event);
1317 /* yaml_* functions return 0 for error */
1318 const char *msg = yaml_parser_get_reader_error(&reply);
1320 rc = errno ? -errno : -EHOSTUNREACH;
1321 if (strcmp(msg, "Unspecific failure") != 0) {
1322 fprintf(stdout, "failed to ping %s: %s\n",
1325 fprintf(stdout, "failed to ping %s: %s\n",
1326 argv[1], strerror(errno));
1328 } else if (rc == 1) {
1329 /* yaml_* functions return 1 for success */
1332 yaml_parser_delete(&reply);
1336 #ifdef IOC_LIBCFS_PING_PEER
1338 struct lnet_process_id ids[LNET_INTERFACES_MAX_DEFAULT];
1339 int maxids = sizeof(ids) / sizeof(ids[0]);
1340 struct lnet_ioctl_ping_data ping = { { 0 } };
1346 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
1347 ping.ping_hdr.ioc_len = sizeof(ping);
1349 ping.ping_src = LNET_NID_ANY;
1350 ping.op_param = timeout;
1351 ping.ping_count = maxids;
1352 ping.ping_buf = ids;
1354 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING_PEER, &ping);
1356 fprintf(stderr, "failed to ping %s: %s\n", argv[1],
1361 for (i = 0; i < ping.ping_count && i < maxids; i++)
1362 printf("%s\n", libcfs_id2str(ids[i]));
1364 if (ping.ping_count > maxids)
1365 printf("%d out of %d ids listed\n", maxids, ping.ping_count);
1373 int jt_ptl_mynid(int argc, char **argv)
1375 struct libcfs_ioctl_data data;
1380 fprintf(stderr, "usage: %s NID\n", argv[0]);
1384 nid = libcfs_str2nid(argv[1]);
1385 if (nid == LNET_NID_ANY) {
1386 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1390 LIBCFS_IOC_INIT(data);
1391 data.ioc_net = LNET_NIDNET(nid);
1394 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1396 fprintf(stderr, "setting my NID failed: %s\n",
1399 printf("registered my nid %s\n", libcfs_nid2str(nid));
1405 jt_ptl_fail_nid(int argc, char **argv)
1410 struct libcfs_ioctl_data data;
1412 if (argc < 2 || argc > 3) {
1413 fprintf(stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n",
1418 if (!libcfs_str2anynid(&nid, argv[1])) {
1419 fprintf(stderr, "Can't parse nid \"%s\"\n", argv[1]);
1424 threshold = LNET_MD_THRESH_INF;
1425 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1426 fprintf(stderr, "Can't parse count \"%s\"\n", argv[2]);
1430 LIBCFS_IOC_INIT(data);
1432 data.ioc_count = threshold;
1434 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1436 fprintf(stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1440 threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1445 static int ptl_yaml_route_display(yaml_parser_t *reply)
1447 char gw[LNET_MAX_STR_LEN], net[18];
1448 bool done = false, alive = false;
1449 int hops = -1, prio = -1;
1453 /* Now parse the reply results */
1457 rc = yaml_parser_parse(reply, &event);
1461 if (event.type == YAML_SEQUENCE_END_EVENT) {
1462 printf("net %18s hops %d gw %32.128s %s pri %u\n",
1463 net, hops, gw, alive ? "up" : "down",
1465 memset(net, '\0', sizeof(net));
1466 memset(gw, '\0', sizeof(gw));
1471 if (event.type != YAML_SCALAR_EVENT)
1474 value = (char *)event.data.scalar.value;
1475 if (strcmp(value, "net") == 0) {
1476 yaml_event_delete(&event);
1477 rc = yaml_parser_parse(reply, &event);
1479 yaml_event_delete(&event);
1483 value = (char *)event.data.scalar.value;
1484 strncpy(net, value, sizeof(net) - 1);
1485 } else if (strcmp(value, "gateway") == 0) {
1486 yaml_event_delete(&event);
1487 rc = yaml_parser_parse(reply, &event);
1489 yaml_event_delete(&event);
1493 value = (char *)event.data.scalar.value;
1494 strncpy(gw, value, sizeof(gw) - 1);
1495 } else if (strcmp(value, "state") == 0) {
1496 yaml_event_delete(&event);
1497 rc = yaml_parser_parse(reply, &event);
1499 yaml_event_delete(&event);
1503 value = (char *)event.data.scalar.value;
1504 if (strcmp(value, "up") == 0) {
1506 } else if (strcmp(value, "down") == 0) {
1509 } else if (strcmp(value, "hop") == 0) {
1510 yaml_event_delete(&event);
1511 rc = yaml_parser_parse(reply, &event);
1513 yaml_event_delete(&event);
1517 value = (char *)event.data.scalar.value;
1518 hops = strtol(value, NULL, 10);
1519 } else if (strcmp(value, "priority") == 0) {
1520 yaml_event_delete(&event);
1521 rc = yaml_parser_parse(reply, &event);
1523 yaml_event_delete(&event);
1527 value = (char *)event.data.scalar.value;
1528 prio = strtol(value, NULL, 10);
1531 done = (event.type == YAML_STREAM_END_EVENT);
1532 yaml_event_delete(&event);
1539 static int ptl_yaml_route(char *nw, char *gws, int hops, int prio, bool enable,
1540 time_t notify_time, int flags, int version)
1542 struct nl_sock *sk = NULL;
1543 const char *msg = NULL;
1544 yaml_emitter_t output;
1545 yaml_parser_t reply;
1549 sk = nl_socket_alloc();
1553 /* Setup parser to receive Netlink packets */
1554 rc = yaml_parser_initialize(&reply);
1560 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1562 msg = yaml_parser_get_reader_error(&reply);
1566 /* Create Netlink emitter to send request to kernel */
1567 rc = yaml_emitter_initialize(&output);
1569 msg = "failed to initialize emitter";
1573 rc = yaml_emitter_set_output_netlink(&output, sk, LNET_GENL_NAME,
1574 version, LNET_CMD_ROUTES, flags);
1578 yaml_emitter_open(&output);
1579 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1580 rc = yaml_emitter_emit(&output, &event);
1584 yaml_mapping_start_event_initialize(&event, NULL,
1585 (yaml_char_t *)YAML_MAP_TAG,
1586 1, YAML_ANY_MAPPING_STYLE);
1587 rc = yaml_emitter_emit(&output, &event);
1591 yaml_scalar_event_initialize(&event, NULL,
1592 (yaml_char_t *)YAML_STR_TAG,
1593 (yaml_char_t *)"route",
1594 strlen("route"), 1, 0,
1595 YAML_PLAIN_SCALAR_STYLE);
1596 rc = yaml_emitter_emit(&output, &event);
1601 yaml_sequence_start_event_initialize(&event, NULL,
1602 (yaml_char_t *)YAML_SEQ_TAG,
1604 YAML_BLOCK_SEQUENCE_STYLE);
1605 rc = yaml_emitter_emit(&output, &event);
1609 yaml_mapping_start_event_initialize(&event, NULL,
1610 (yaml_char_t *)YAML_MAP_TAG, 1,
1611 YAML_BLOCK_MAPPING_STYLE);
1612 rc = yaml_emitter_emit(&output, &event);
1617 yaml_scalar_event_initialize(&event, NULL,
1618 (yaml_char_t *)YAML_STR_TAG,
1619 (yaml_char_t *)"net",
1620 strlen("net"), 1, 0,
1621 YAML_PLAIN_SCALAR_STYLE);
1622 rc = yaml_emitter_emit(&output, &event);
1626 yaml_scalar_event_initialize(&event, NULL,
1627 (yaml_char_t *)YAML_STR_TAG,
1630 YAML_PLAIN_SCALAR_STYLE);
1631 rc = yaml_emitter_emit(&output, &event);
1637 yaml_scalar_event_initialize(&event, NULL,
1638 (yaml_char_t *)YAML_STR_TAG,
1639 (yaml_char_t *)"gateway",
1640 strlen("gateway"), 1, 0,
1641 YAML_PLAIN_SCALAR_STYLE);
1642 rc = yaml_emitter_emit(&output, &event);
1646 yaml_scalar_event_initialize(&event, NULL,
1647 (yaml_char_t *)YAML_STR_TAG,
1650 YAML_PLAIN_SCALAR_STYLE);
1651 rc = yaml_emitter_emit(&output, &event);
1657 char when[INT_STRING_LEN];
1659 yaml_scalar_event_initialize(&event, NULL,
1660 (yaml_char_t *)YAML_STR_TAG,
1661 (yaml_char_t *)"notify_time",
1662 strlen("notify_time"), 1, 0,
1663 YAML_PLAIN_SCALAR_STYLE);
1664 rc = yaml_emitter_emit(&output, &event);
1668 snprintf(when, sizeof(when), "%ld", notify_time);
1669 yaml_scalar_event_initialize(&event, NULL,
1670 (yaml_char_t *)YAML_INT_TAG,
1671 (yaml_char_t *)when,
1673 YAML_PLAIN_SCALAR_STYLE);
1674 rc = yaml_emitter_emit(&output, &event);
1679 yaml_scalar_event_initialize(&event, NULL,
1680 (yaml_char_t *)YAML_STR_TAG,
1681 (yaml_char_t *)"state",
1682 strlen("state"), 1, 0,
1683 YAML_PLAIN_SCALAR_STYLE);
1684 rc = yaml_emitter_emit(&output, &event);
1689 yaml_scalar_event_initialize(&event, NULL,
1690 (yaml_char_t *)YAML_STR_TAG,
1691 (yaml_char_t *)"up",
1693 YAML_PLAIN_SCALAR_STYLE);
1695 yaml_scalar_event_initialize(&event, NULL,
1696 (yaml_char_t *)YAML_STR_TAG,
1697 (yaml_char_t *)"down",
1698 strlen("down"), 1, 0,
1699 YAML_PLAIN_SCALAR_STYLE);
1701 rc = yaml_emitter_emit(&output, &event);
1705 yaml_mapping_end_event_initialize(&event);
1706 rc = yaml_emitter_emit(&output, &event);
1710 yaml_sequence_end_event_initialize(&event);
1711 rc = yaml_emitter_emit(&output, &event);
1715 yaml_scalar_event_initialize(&event, NULL,
1716 (yaml_char_t *)YAML_STR_TAG,
1719 YAML_PLAIN_SCALAR_STYLE);
1720 rc = yaml_emitter_emit(&output, &event);
1725 yaml_mapping_end_event_initialize(&event);
1726 rc = yaml_emitter_emit(&output, &event);
1730 yaml_document_end_event_initialize(&event, 0);
1731 rc = yaml_emitter_emit(&output, &event);
1735 rc = yaml_emitter_close(&output);
1738 yaml_emitter_log_error(&output, stderr);
1741 if (flags != NLM_F_DUMP) {
1742 yaml_document_t errmsg;
1744 rc = yaml_parser_load(&reply, &errmsg);
1746 yaml_emitter_t debug;
1748 rc = yaml_emitter_initialize(&debug);
1750 yaml_emitter_set_indent(&debug,
1751 LNET_DEFAULT_INDENT);
1752 yaml_emitter_set_output_file(&debug,
1754 rc = yaml_emitter_dump(&debug,
1756 } else if (rc == 0) {
1757 yaml_emitter_log_error(&debug, stderr);
1760 yaml_emitter_delete(&debug);
1762 yaml_document_delete(&errmsg);
1764 rc = ptl_yaml_route_display(&reply);
1767 msg = yaml_parser_get_reader_error(&reply);
1769 yaml_emitter_delete(&output);
1772 fprintf(stdout, "%s\n", msg);
1773 yaml_parser_delete(&reply);
1776 return rc == 1 ? 0 : rc;
1780 jt_ptl_add_route(int argc, char **argv)
1782 struct lnet_ioctl_config_data data;
1783 lnet_nid_t gateway_nid;
1784 __u32 hops = LNET_UNDEFINED_HOPS;
1785 unsigned int priority = 0;
1789 if (argc < 2 || argc > 4) {
1790 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1795 if (g_net_is_set(argv[0]) == 0)
1798 gateway_nid = libcfs_str2nid(argv[1]);
1799 if (gateway_nid == LNET_NID_ANY) {
1800 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1805 hops = strtol(argv[2], &end, 0);
1806 if (hops == 0 || hops >= 256 ||
1807 (end && *end != 0)) {
1808 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1813 priority = strtoul(argv[3], &end, 0);
1814 if (end && *end != 0) {
1816 "Can't parse priority \"%s\"\n",
1823 rc = ptl_yaml_route(libcfs_net2str(g_net), argv[1], hops,
1824 priority, false, 0, NLM_F_CREATE, LNET_GENL_VERSION);
1826 if (rc == -EOPNOTSUPP)
1831 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1832 data.cfg_net = g_net;
1833 data.cfg_config_u.cfg_route.rtr_hop = hops;
1834 data.cfg_nid = gateway_nid;
1835 data.cfg_config_u.cfg_route.rtr_priority = priority;
1837 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1839 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1848 jt_ptl_del_route(int argc, char **argv)
1850 struct lnet_ioctl_config_data data;
1855 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1859 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1860 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1864 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
1865 -1, -1, false, 0, 0, LNET_GENL_VERSION);
1867 if (rc == -EOPNOTSUPP)
1872 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1873 data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
1876 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1878 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1879 libcfs_nid2str(nid), strerror(errno));
1887 jt_ptl_notify_router(int argc, char **argv)
1889 struct libcfs_ioctl_data data;
1897 fprintf(stderr, "usage: %s targetNID <up/down> [<time>]\n",
1902 nid = libcfs_str2nid(argv[1]);
1903 if (nid == LNET_NID_ANY) {
1904 fprintf(stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1908 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1909 fprintf(stderr, "Can't parse boolean %s\n", argv[2]);
1913 gettimeofday(&now, NULL);
1917 } else if (lnet_parse_time(&when, argv[3]) != 0) {
1919 "Can't parse time %s\n Please specify either 'YYYY-MM-DD-HH:MM:SS'\n or an absolute unix time in seconds\n",
1922 } else if (when > now.tv_sec) {
1923 fprintf(stderr, "%s specifies a time in the future\n",
1928 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
1929 -1, -1, enable, when, NLM_F_REPLACE, LNET_GENL_VERSION);
1931 if (rc == -EOPNOTSUPP)
1936 LIBCFS_IOC_INIT(data);
1938 data.ioc_flags = enable;
1939 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1940 data.ioc_u64[0] = (__u64)when;
1942 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1944 fprintf(stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1945 libcfs_nid2str(nid), strerror(errno));
1953 jt_ptl_print_routes(int argc, char **argv)
1955 struct lnet_ioctl_config_data data;
1964 rc = ptl_yaml_route(NULL, NULL, -1, -1, false, 0, NLM_F_DUMP,
1967 if (rc == -EOPNOTSUPP)
1972 for (index = 0; ; index++) {
1973 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1974 data.cfg_count = index;
1976 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1981 hops = data.cfg_config_u.cfg_route.rtr_hop;
1983 alive = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
1984 pri = data.cfg_config_u.cfg_route.rtr_priority;
1986 printf("net %18s hops %d gw %32s %s pri %u\n",
1987 libcfs_net2str(net), hops,
1988 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1991 if (errno != ENOENT)
1992 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
1999 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
2005 /* NB: can't support range ipaddress except * and *@net */
2006 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
2007 net = libcfs_str2net(str + 2);
2008 if (net == LNET_NET_ANY)
2011 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
2013 rc = libcfs_str2anynid(&nid, str);
2021 fprintf(stderr, "Invalid NID : %s\n", str);
2026 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
2028 if (!strcasecmp(msg_str, "put")) {
2029 *mask_p |= LNET_PUT_BIT;
2032 } else if (!strcasecmp(msg_str, "ack")) {
2033 *mask_p |= LNET_ACK_BIT;
2036 } else if (!strcasecmp(msg_str, "get")) {
2037 *mask_p |= LNET_GET_BIT;
2040 } else if (!strcasecmp(msg_str, "reply")) {
2041 *mask_p |= LNET_REPLY_BIT;
2045 fprintf(stderr, "unknown message type %s\n", msg_str);
2050 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
2052 unsigned long rc = strtoul(optarg, NULL, 0);
2055 fprintf(stderr, "invalid portal: %lu\n", rc);
2059 *mask_p |= (1ULL << rc);
2064 fault_attr_health_error_parse(char *error, __u32 *mask)
2066 if (!strcasecmp(error, "local_interrupt")) {
2067 *mask |= HSTATUS_LOCAL_INTERRUPT_BIT;
2070 if (!strcasecmp(error, "local_dropped")) {
2071 *mask |= HSTATUS_LOCAL_DROPPED_BIT;
2074 if (!strcasecmp(error, "local_aborted")) {
2075 *mask |= HSTATUS_LOCAL_ABORTED_BIT;
2078 if (!strcasecmp(error, "local_no_route")) {
2079 *mask |= HSTATUS_LOCAL_NO_ROUTE_BIT;
2082 if (!strcasecmp(error, "local_error")) {
2083 *mask |= HSTATUS_LOCAL_ERROR_BIT;
2086 if (!strcasecmp(error, "local_timeout")) {
2087 *mask |= HSTATUS_LOCAL_TIMEOUT_BIT;
2090 if (!strcasecmp(error, "remote_error")) {
2091 *mask |= HSTATUS_REMOTE_ERROR_BIT;
2094 if (!strcasecmp(error, "remote_dropped")) {
2095 *mask |= HSTATUS_REMOTE_DROPPED_BIT;
2098 if (!strcasecmp(error, "remote_timeout")) {
2099 *mask |= HSTATUS_REMOTE_TIMEOUT_BIT;
2102 if (!strcasecmp(error, "network_timeout")) {
2103 *mask |= HSTATUS_NETWORK_TIMEOUT_BIT;
2106 if (!strcasecmp(error, "random")) {
2107 *mask = HSTATUS_RANDOM;
2115 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
2117 struct libcfs_ioctl_data data = { { 0 } };
2118 struct lnet_fault_attr attr;
2122 static const struct option opts[] = {
2123 { .name = "source", .has_arg = required_argument, .val = 's' },
2124 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2125 { .name = "rate", .has_arg = required_argument, .val = 'r' },
2126 { .name = "interval", .has_arg = required_argument, .val = 'i' },
2127 { .name = "random", .has_arg = no_argument, .val = 'n' },
2128 { .name = "latency", .has_arg = required_argument, .val = 'l' },
2129 { .name = "portal", .has_arg = required_argument, .val = 'p' },
2130 { .name = "message", .has_arg = required_argument, .val = 'm' },
2131 { .name = "health_error", .has_arg = required_argument, .val = 'e' },
2132 { .name = "local_nid", .has_arg = required_argument, .val = 'o' },
2133 { .name = "drop_all", .has_arg = no_argument, .val = 'x' },
2138 "Failed, please provide source, destination and rate of rule\n");
2142 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
2143 memset(&attr, 0, sizeof(attr));
2145 int c = getopt_long(argc, argv, optstr, opts, NULL);
2152 rc = fault_attr_nid_parse(optarg, &attr.fa_local_nid);
2156 case 's': /* source NID/NET */
2157 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2162 case 'd': /* dest NID/NET */
2163 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2168 case 'r': /* drop rate */
2169 if (opc == LNET_CTL_DROP_ADD)
2170 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
2172 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
2176 if (opc == LNET_CTL_DROP_ADD) {
2177 rc = fault_attr_health_error_parse(optarg,
2178 &attr.u.drop.da_health_error_mask);
2185 if (opc == LNET_CTL_DROP_ADD)
2186 attr.u.drop.da_drop_all = true;
2190 if (opc == LNET_CTL_DROP_ADD)
2191 attr.u.drop.da_random = true;
2194 case 'i': /* time interval (# seconds) for message drop */
2195 if (opc == LNET_CTL_DROP_ADD)
2196 attr.u.drop.da_interval = strtoul(optarg,
2199 attr.u.delay.la_interval = strtoul(optarg,
2203 case 'l': /* seconds to wait before activating rule */
2204 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
2207 case 'p': /* portal to filter */
2208 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
2213 case 'm': /* message types to filter */
2214 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
2220 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2221 argv[0], argv[optind - 1]);
2227 if (opc == LNET_CTL_DROP_ADD) {
2228 /* NB: drop rate and interval are exclusive to each other */
2229 if (!((attr.u.drop.da_rate == 0) ^
2230 (attr.u.drop.da_interval == 0))) {
2232 "please provide either drop rate or interval but not both at the same time.\n");
2236 if (attr.u.drop.da_random &&
2237 attr.u.drop.da_interval == 0) {
2239 "please provide an interval to randomize\n");
2242 } else if (opc == LNET_CTL_DELAY_ADD) {
2243 if (!((attr.u.delay.la_rate == 0) ^
2244 (attr.u.delay.la_interval == 0))) {
2246 "please provide either delay rate or interval but not both at the same time.\n");
2250 if (attr.u.delay.la_latency == 0) {
2251 fprintf(stderr, "latency cannot be zero\n");
2256 if (attr.fa_src == 0 || attr.fa_dst == 0) {
2258 "Please provide both source and destination of %s rule\n",
2263 if (attr.fa_local_nid == 0)
2264 attr.fa_local_nid = LNET_NID_ANY;
2266 data.ioc_flags = opc;
2267 data.ioc_inllen1 = sizeof(attr);
2268 data.ioc_inlbuf1 = (char *)&attr;
2269 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2270 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2274 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2276 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
2277 name, libcfs_nid2str(attr.fa_src),
2278 libcfs_nid2str(attr.fa_dst), strerror(errno));
2282 printf("Added %s rule %s->%s (1/%d)\n",
2283 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
2284 opc == LNET_CTL_DROP_ADD ?
2285 attr.u.drop.da_rate : attr.u.delay.la_rate);
2294 jt_ptl_drop_add(int argc, char **argv)
2296 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
2300 jt_ptl_delay_add(int argc, char **argv)
2302 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
2306 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
2308 struct libcfs_ioctl_data data = { { 0 } };
2309 struct lnet_fault_attr attr;
2313 static const struct option opts[] = {
2314 { .name = "source", .has_arg = required_argument, .val = 's' },
2315 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2316 { .name = "all", .has_arg = no_argument, .val = 'a' },
2321 "Failed, please provide source and destination of rule\n");
2325 memset(&attr, 0, sizeof(attr));
2327 int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
2334 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2339 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2350 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2351 argv[0], argv[optind - 1]);
2357 data.ioc_flags = opc;
2358 data.ioc_inllen1 = sizeof(attr);
2359 data.ioc_inlbuf1 = (char *)&attr;
2360 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2361 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2365 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2367 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
2368 all ? "all" : libcfs_nid2str(attr.fa_src),
2369 all ? "all" : libcfs_nid2str(attr.fa_dst),
2374 libcfs_ioctl_unpack(&data, ioc_buf);
2375 printf("Removed %d %s rules\n", data.ioc_count, name);
2384 jt_ptl_drop_del(int argc, char **argv)
2386 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
2390 jt_ptl_delay_del(int argc, char **argv)
2392 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
2396 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
2398 struct libcfs_ioctl_data data = { { 0 } };
2401 LIBCFS_IOC_INIT(data);
2402 data.ioc_flags = opc;
2404 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
2406 fprintf(stderr, "failed to reset %s stats: %s\n",
2407 name, strerror(errno));
2414 jt_ptl_drop_reset(int argc, char **argv)
2416 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
2420 jt_ptl_delay_reset(int argc, char **argv)
2422 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
2427 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
2429 struct libcfs_ioctl_data data = { { 0 } };
2430 struct lnet_fault_attr attr;
2431 struct lnet_fault_stat stat;
2434 printf("LNet %s rules:\n", name);
2435 for (pos = 0;; pos++) {
2438 memset(&attr, 0, sizeof(attr));
2439 memset(&stat, 0, sizeof(stat));
2441 data.ioc_count = pos;
2442 data.ioc_flags = opc;
2443 data.ioc_inllen1 = sizeof(attr);
2444 data.ioc_inlbuf1 = (char *)&attr;
2445 data.ioc_inllen2 = sizeof(stat);
2446 data.ioc_inlbuf2 = (char *)&stat;
2447 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2448 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2452 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2456 libcfs_ioctl_unpack(&data, ioc_buf);
2458 if (opc == LNET_CTL_DROP_LIST) {
2459 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2460 libcfs_nid2str(attr.fa_src),
2461 libcfs_nid2str(attr.fa_dst),
2462 attr.u.drop.da_rate, attr.u.drop.da_interval,
2463 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2464 (uintmax_t)stat.u.drop.ds_dropped,
2465 (uintmax_t)stat.fs_count,
2466 (uintmax_t)stat.fs_put,
2467 (uintmax_t)stat.fs_ack,
2468 (uintmax_t)stat.fs_get,
2469 (uintmax_t)stat.fs_reply);
2471 } else if (opc == LNET_CTL_DELAY_LIST) {
2472 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2473 libcfs_nid2str(attr.fa_src),
2474 libcfs_nid2str(attr.fa_dst),
2475 attr.u.delay.la_rate, attr.u.delay.la_interval,
2476 attr.u.delay.la_latency,
2477 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2478 (uintmax_t)stat.u.delay.ls_delayed,
2479 (uintmax_t)stat.fs_count,
2480 (uintmax_t)stat.fs_put,
2481 (uintmax_t)stat.fs_ack,
2482 (uintmax_t)stat.fs_get,
2483 (uintmax_t)stat.fs_reply);
2486 printf("found total %d\n", pos);
2492 jt_ptl_drop_list(int argc, char **argv)
2494 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
2498 jt_ptl_delay_list(int argc, char **argv)
2500 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
2504 get_cycles_per_usec()
2506 FILE *f = fopen("/proc/cpuinfo", "r");
2511 while (fgets(line, sizeof(line), f))
2512 if (sscanf(line, "cpu MHz : %lf", &mhz) == 1) {
2519 fprintf(stderr, "Can't read/parse /proc/cpuinfo\n");
2523 int jt_ptl_testprotocompat(int argc, char **argv)
2525 struct libcfs_ioctl_data data;
2531 fprintf(stderr, "usage: %s <number>\n", argv[0]);
2535 flags = strtol(argv[1], &end, 0);
2536 if (flags < 0 || *end != 0) {
2537 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
2541 LIBCFS_IOC_INIT(data);
2542 data.ioc_flags = flags;
2543 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
2546 fprintf(stderr, "test proto compat %x failed: %s\n",
2547 flags, strerror(errno));
2551 printf("test proto compat %x OK\n", flags);