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>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 #include <linux/types.h>
41 #include <libcfs/util/ioctl.h>
42 #include <linux/lnet/libcfs_debug.h>
43 #include <linux/lnet/lnet-dlc.h>
44 #include <linux/lnet/lnetctl.h>
45 #include <linux/lnet/nidstr.h>
46 #include <linux/lnet/socklnd.h>
47 #include <lnetconfig/liblnetconfig.h>
48 #include <lustre/lustreapi.h>
49 #include <lustre_ioctl_old.h>
51 unsigned int libcfs_debug;
52 unsigned int libcfs_printk = D_CANTMASK;
54 static bool g_net_interactive;
55 static bool g_net_set;
58 #define IOC_BUF_SIZE 8192
59 static char local_buf[IOC_BUF_SIZE];
60 static char *ioc_buf = local_buf;
62 /* Convert a string boolean to an int; "enable" -> 1 */
64 lnet_parse_bool (int *b, char *str)
66 if (!strcasecmp(str, "no") ||
67 !strcasecmp(str, "n") ||
68 !strcasecmp(str, "off") ||
69 !strcasecmp(str, "down") ||
70 !strcasecmp(str, "disable")) {
76 if (!strcasecmp(str, "yes") ||
77 !strcasecmp(str, "y") ||
78 !strcasecmp(str, "on") ||
79 !strcasecmp(str, "up") ||
80 !strcasecmp(str, "enable")) {
90 lnet_parse_port(int *port, char *str)
94 *port = strtol(str, &end, 0);
96 if (*end == 0 && /* parsed whole string */
97 *port > 0 && *port < 65536) /* minimal sanity check */
104 lnet_parse_ipquad(__u32 *ipaddrp, char *str)
108 if (sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
109 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
110 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
111 *ipaddrp = (a << 24) | (b << 16) | (c << 8) | d;
120 lnet_parse_ipaddr(__u32 *ipaddrp, char *str)
122 struct addrinfo *ai = NULL;
123 struct addrinfo *aip = NULL;
124 struct addrinfo hints;
128 if (!strcmp(str, "_all_")) {
133 if (lnet_parse_ipquad(ipaddrp, str) == 0)
136 memset(&hints, 0, sizeof(struct addrinfo));
137 hints.ai_family = AF_INET;
139 if (('a' <= str[0] && str[0] <= 'z') ||
140 ('A' <= str[0] && str[0] <= 'Z')) {
141 err = getaddrinfo(str, NULL, &hints, &ai);
144 "failed to get addrinfo for %s: %s\n",
145 str, gai_strerror(err));
149 for (aip = ai; aip; aip = aip->ai_next) {
150 if (aip->ai_family == AF_INET && aip->ai_addr) {
151 struct sockaddr_in *sin =
154 __u32 addr = (__u32)sin->sin_addr.s_addr;
155 *ipaddrp = ntohl(addr);
159 /* FIXME: handle AF_INET6 */
162 fprintf(stderr, "failed to get IP address for %s\n",
179 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
181 struct sockaddr_in srcaddr;
184 memset(&srcaddr, 0, sizeof(srcaddr));
185 srcaddr.sin_family = AF_INET;
186 srcaddr.sin_addr.s_addr = (in_addr_t)htonl(ipaddr);
188 if (getnameinfo((struct sockaddr *)&srcaddr, sizeof(srcaddr),
189 str, strsize, NULL, 0, 0) == 0)
193 snprintf(str, strsize, "%d.%d.%d.%d",
194 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
195 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
201 lnet_parse_time(time_t *t, char *str)
207 *t = strtol(str, &end, 0);
208 if (*end == 0) /* parsed whole string */
211 memset(&tm, 0, sizeof(tm));
212 n = sscanf(str, "%d-%d-%d-%d:%d:%d",
213 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
214 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
218 tm.tm_mon--; /* convert to 0 == Jan */
219 tm.tm_year -= 1900; /* y2k quirk */
220 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
223 if (*t == (time_t)-1)
230 lnet_parse_nid(char *nid_str, struct lnet_process_id *id_ptr)
232 id_ptr->pid = LNET_PID_ANY;
233 id_ptr->nid = libcfs_str2nid(nid_str);
234 if (id_ptr->nid == LNET_NID_ANY) {
235 fprintf(stderr, "Can't parse nid \"%s\"\n", nid_str);
242 static int g_net_is_set(char *cmd)
250 if (g_net_interactive)
256 "You must run '%s <network>' command before '%s'\n",
264 static int g_net_is_compatible(char *cmd, ...)
269 if (!g_net_is_set(cmd))
275 nal = va_arg(ap, int);
276 if (nal == LNET_NETTYP(g_net)) {
285 fprintf(stderr, "Command %s not compatible with %s NAL\n",
286 cmd, libcfs_lnd2str(LNET_NETTYP(g_net)));
291 int ptl_initialize(int argc, char **argv)
294 g_net_interactive = true;
296 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
301 int jt_ptl_network(int argc, char **argv)
303 struct libcfs_ioctl_data data;
304 __u32 net = LNET_NET_ANY;
308 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
312 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
313 LIBCFS_IOC_INIT(data);
314 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
317 printf("LNET ready to unload\n");
321 if (errno == ENODEV) {
322 printf("LNET is currently not loaded.");
327 fprintf(stderr, "LNET busy\n");
329 fprintf(stderr, "LNET unconfigure error %d: %s\n",
330 errno, strerror(errno));
332 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
333 LIBCFS_IOC_INIT(data);
334 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
337 printf("LNET configured\n");
341 fprintf(stderr, "LNET configure error %d: %s\n",
342 errno, strerror(errno));
346 net = libcfs_str2net(argv[1]);
347 if (net == LNET_NET_ANY) {
348 fprintf(stderr, "Can't parse net %s\n", argv[1]);
357 #ifndef IOC_LIBCFS_GET_NI
358 #define IOC_LIBCFS_GET_NI _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
362 jt_ptl_list_nids(int argc, char **argv)
364 int all = 0, return_nid = 0;
365 yaml_emitter_t request;
372 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
373 /* Hack to pass back value */
374 return_nid = (argc == 2) && (argv[1][0] == 1);
376 if ((argc > 2) && !(all || return_nid)) {
377 fprintf(stderr, "usage: %s [all]\n", argv[0]);
381 sk = nl_socket_alloc();
385 /* Setup parser to receive Netlink packets */
386 rc = yaml_parser_initialize(&reply);
388 yaml_parser_log_error(&reply, stderr, NULL);
392 rc = yaml_parser_set_input_netlink(&reply, sk, false);
394 yaml_parser_log_error(&reply, stderr, NULL);
395 yaml_parser_delete(&reply);
399 /* Create Netlink emitter to send request to kernel */
400 rc = yaml_emitter_initialize(&request);
402 yaml_parser_log_error(&reply, stderr, NULL);
403 yaml_parser_delete(&reply);
407 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME, 1,
408 LNET_CMD_NETS, NLM_F_DUMP);
410 yaml_emitter_log_error(&request, stderr);
411 yaml_emitter_delete(&request);
412 yaml_parser_delete(&reply);
416 yaml_emitter_open(&request);
417 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
418 rc = yaml_emitter_emit(&request, &event);
422 yaml_mapping_start_event_initialize(&event, NULL,
423 (yaml_char_t *)YAML_MAP_TAG,
424 1, YAML_ANY_MAPPING_STYLE);
425 rc = yaml_emitter_emit(&request, &event);
429 yaml_scalar_event_initialize(&event, NULL,
430 (yaml_char_t *)YAML_STR_TAG,
431 (yaml_char_t *)"net",
433 YAML_PLAIN_SCALAR_STYLE);
434 rc = yaml_emitter_emit(&request, &event);
439 if (!g_net_set || g_net == LNET_NET_ANY) {
440 yaml_scalar_event_initialize(&event, NULL,
441 (yaml_char_t *)YAML_STR_TAG,
444 YAML_PLAIN_SCALAR_STYLE);
445 rc = yaml_emitter_emit(&request, &event);
449 char *net_id = libcfs_net2str(g_net);
451 yaml_sequence_start_event_initialize(&event, NULL,
452 (yaml_char_t *)YAML_SEQ_TAG,
453 1, YAML_ANY_SEQUENCE_STYLE);
454 rc = yaml_emitter_emit(&request, &event);
458 yaml_mapping_start_event_initialize(&event, NULL,
459 (yaml_char_t *)YAML_MAP_TAG,
460 1, YAML_ANY_MAPPING_STYLE);
461 rc = yaml_emitter_emit(&request, &event);
465 yaml_scalar_event_initialize(&event, NULL,
466 (yaml_char_t *)YAML_STR_TAG,
467 (yaml_char_t *)"net type",
469 1, 0, YAML_PLAIN_SCALAR_STYLE);
470 rc = yaml_emitter_emit(&request, &event);
474 yaml_scalar_event_initialize(&event, NULL,
475 (yaml_char_t *)YAML_STR_TAG,
476 (yaml_char_t *)net_id,
477 strlen(net_id), 1, 0,
478 YAML_PLAIN_SCALAR_STYLE);
479 rc = yaml_emitter_emit(&request, &event);
483 yaml_mapping_end_event_initialize(&event);
484 rc = yaml_emitter_emit(&request, &event);
488 yaml_sequence_end_event_initialize(&event);
489 rc = yaml_emitter_emit(&request, &event);
493 yaml_mapping_end_event_initialize(&event);
494 rc = yaml_emitter_emit(&request, &event);
498 yaml_document_end_event_initialize(&event, 0);
499 rc = yaml_emitter_emit(&request, &event);
503 rc = yaml_emitter_close(&request);
506 yaml_emitter_log_error(&request, stderr);
509 yaml_emitter_delete(&request);
512 rc = yaml_parser_parse(&reply, &event);
516 if (event.type == YAML_SCALAR_EVENT &&
517 strcmp((char *)event.data.scalar.value, "nid") == 0) {
520 yaml_event_delete(&event);
521 rc = yaml_parser_parse(&reply, &event);
523 yaml_event_delete(&event);
527 tmp = (char *)event.data.scalar.value;
528 if (all || strcmp(tmp, "0@lo") != 0) {
531 *(__u64 *)(argv[1]) = libcfs_str2nid(tmp);
536 done = (event.type == YAML_STREAM_END_EVENT);
537 yaml_event_delete(&event);
541 yaml_parser_log_error(&reply, stderr, NULL);
542 yaml_parser_delete(&reply);
544 #ifdef IOC_LIBCFS_GET_NI
552 for (count = 0;; count++) {
553 struct libcfs_ioctl_data data;
555 LIBCFS_IOC_INIT(data);
556 data.ioc_count = count;
557 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
560 if ((count > 0) && (errno == ENOENT))
561 /* We found them all */
563 fprintf(stderr, "IOC_LIBCFS_GET_NI error %d: %s\n",
564 errno, strerror(errno));
568 if (all || (data.ioc_nid != LNET_NID_LO_0)) {
569 printf("%s\n", libcfs_nid2str(data.ioc_nid));
571 *(__u64 *)(argv[1]) = data.ioc_nid;
585 jt_ptl_which_nid(int argc, char **argv)
587 struct libcfs_ioctl_data data;
590 lnet_nid_t best_nid = LNET_NID_ANY;
599 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
603 for (i = 1; i < argc; i++) {
605 nid = libcfs_str2nid(nidstr);
606 if (nid == LNET_NID_ANY) {
607 fprintf(stderr, "Can't parse NID %s\n", nidstr);
611 LIBCFS_IOC_INIT(data);
614 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
616 fprintf(stderr, "Can't get distance to %s: %s\n",
617 nidstr, strerror(errno));
621 dist = data.ioc_u32[0];
622 order = data.ioc_u32[1];
625 if (dist == -EHOSTUNREACH)
628 fprintf(stderr, "Unexpected distance to %s: %d\n",
633 if (best_nid == LNET_NID_ANY ||
635 (dist == best_dist && order < best_order)) {
642 if (best_nid == LNET_NID_ANY) {
643 fprintf(stderr, "No reachable NID\n");
647 printf("%s\n", libcfs_nid2str(best_nid));
652 jt_ptl_print_interfaces(int argc, char **argv)
654 struct libcfs_ioctl_data data;
655 char buffer[3][HOST_NAME_MAX + 1];
659 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
662 for (index = 0; ; index++) {
663 LIBCFS_IOC_INIT(data);
664 data.ioc_net = g_net;
665 data.ioc_count = index;
667 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
671 printf("%s: (%s/%s) npeer %d nroute %d\n",
672 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
673 sizeof(buffer[2]), 1),
674 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
675 sizeof(buffer[0]), 0),
676 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
677 sizeof(buffer[1]), 0),
678 data.ioc_u32[2], data.ioc_u32[3]);
682 if (errno == ENOENT) {
683 printf("<no interfaces>\n");
686 "Error getting interfaces: %s: check dmesg.\n",
695 jt_ptl_add_interface(int argc, char **argv)
697 struct libcfs_ioctl_data data;
700 __u32 netmask = 0xffffff00;
705 if (argc < 2 || argc > 3) {
706 fprintf(stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
710 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
713 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
714 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
719 count = strtol(argv[2], &end, 0);
720 if (count > 0 && count < 32 && *end == 0) {
722 for (i = count; i > 0; i--)
723 netmask = netmask | (1 << (32 - i));
724 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
725 fprintf(stderr, "Can't parse netmask: %s\n", argv[2]);
730 LIBCFS_IOC_INIT(data);
731 data.ioc_net = g_net;
732 data.ioc_u32[0] = ipaddr;
733 data.ioc_u32[1] = netmask;
735 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
737 fprintf(stderr, "failed to add interface: %s\n",
746 jt_ptl_del_interface(int argc, char **argv)
748 struct libcfs_ioctl_data data;
753 fprintf(stderr, "usage: %s [ipaddr]\n", argv[0]);
757 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
761 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
762 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
766 LIBCFS_IOC_INIT(data);
767 data.ioc_net = g_net;
768 data.ioc_u32[0] = ipaddr;
770 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
772 fprintf(stderr, "failed to delete interface: %s\n",
781 jt_ptl_print_peers(int argc, char **argv)
783 struct libcfs_ioctl_data data;
784 struct lnet_process_id id;
785 char buffer[2][HOST_NAME_MAX + 1];
789 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
793 for (index = 0; ; index++) {
794 LIBCFS_IOC_INIT(data);
795 data.ioc_net = g_net;
796 data.ioc_count = index;
798 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
802 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
803 id.nid = data.ioc_nid;
804 id.pid = data.ioc_u32[4];
805 printf("%-20s [%d]%s->%s:%d #%d\n",
807 data.ioc_count, /* persistence */
809 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
810 sizeof(buffer[0]), 1),
812 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
813 sizeof(buffer[1]), 1),
814 data.ioc_u32[1], /* peer port */
815 data.ioc_u32[3]); /* conn_count */
816 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
817 int disconn = data.ioc_flags >> 16;
823 state = data.ioc_flags & 0xffff ? "C" : "U";
825 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
826 libcfs_nid2str(data.ioc_nid), /* peer nid */
827 data.ioc_net, /* gemini device id */
828 state, /* peer is Connecting, Up, or Down */
829 data.ioc_count, /* peer refcount */
830 (uintmax_t)data.ioc_u64[0], /* peerstamp */
831 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
832 /* fmaq, nfma, nrdma */
833 data.ioc_u32[0], data.ioc_u32[1],
836 printf("%-20s [%d]\n",
837 libcfs_nid2str(data.ioc_nid), data.ioc_count);
842 if (errno == ENOENT) {
843 printf("<no peers>\n");
846 "Error getting peer list: %s: check dmesg.\n",
853 int jt_ptl_add_peer(int argc, char **argv)
855 struct libcfs_ioctl_data data;
861 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
865 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
870 nid = libcfs_str2nid(argv[1]);
871 if (nid == LNET_NID_ANY) {
872 fprintf(stderr, "Can't parse NID: %s\n", argv[1]);
876 if (lnet_parse_ipaddr(&ip, argv[2]) != 0) {
877 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
881 if (lnet_parse_port(&port, argv[3]) != 0) {
882 fprintf(stderr, "Can't parse port: %s\n", argv[3]);
886 LIBCFS_IOC_INIT(data);
887 data.ioc_net = g_net;
889 data.ioc_u32[0] = ip;
890 data.ioc_u32[1] = port;
892 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
894 fprintf(stderr, "failed to add peer: %s\n",
903 jt_ptl_del_peer(int argc, char **argv)
905 struct libcfs_ioctl_data data;
906 lnet_nid_t nid = LNET_NID_ANY;
907 lnet_pid_t pid = LNET_PID_ANY;
911 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
915 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
917 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
920 } else if (argc > 2) {
921 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
925 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
926 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
930 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
931 if (argc > 2 && lnet_parse_ipaddr(&ip, argv[2]) != 0) {
932 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
937 LIBCFS_IOC_INIT(data);
938 data.ioc_net = g_net;
940 data.ioc_u32[0] = ip;
941 data.ioc_u32[1] = pid;
943 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
945 fprintf(stderr, "failed to remove peer: %s\n", strerror(errno));
953 jt_ptl_print_connections(int argc, char **argv)
955 struct libcfs_ioctl_data data;
956 struct lnet_process_id id;
957 char buffer[2][HOST_NAME_MAX + 1];
961 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
964 for (index = 0; ; index++) {
965 LIBCFS_IOC_INIT(data);
966 data.ioc_net = g_net;
967 data.ioc_count = index;
969 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
973 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
974 id.nid = data.ioc_nid;
975 id.pid = data.ioc_u32[6];
976 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
978 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
979 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
980 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
981 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
982 data.ioc_u32[4], /* scheduler */
984 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
985 sizeof(buffer[0]), 1),
987 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
988 sizeof(buffer[1]), 1),
989 data.ioc_u32[1], /* remote port */
990 data.ioc_count, /* tx buffer size */
991 data.ioc_u32[5], /* rx buffer size */
992 data.ioc_flags ? "nagle" : "nonagle");
993 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
994 printf("%s mtu %d\n",
995 libcfs_nid2str(data.ioc_nid),
996 data.ioc_u32[0]); /* path MTU */
997 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
998 printf("%-20s [%d]\n",
999 libcfs_nid2str(data.ioc_nid),
1000 data.ioc_u32[0] /* device id */);
1002 printf("%s\n", libcfs_nid2str(data.ioc_nid));
1007 if (errno == ENOENT) {
1008 printf("<no connections>\n");
1011 "Error getting connection list: %s: check dmesg.\n",
1018 int jt_ptl_disconnect(int argc, char **argv)
1020 struct libcfs_ioctl_data data;
1021 lnet_nid_t nid = LNET_NID_ANY;
1026 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1030 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
1033 if (argc >= 2 && !libcfs_str2anynid(&nid, argv[1])) {
1034 fprintf(stderr, "Can't parse nid %s\n", argv[1]);
1038 if (g_net_is_compatible(NULL, SOCKLND, 0) && argc >= 3 &&
1039 lnet_parse_ipaddr(&ipaddr, argv[2]) != 0) {
1040 fprintf(stderr, "Can't parse ip addr %s\n", argv[2]);
1044 LIBCFS_IOC_INIT(data);
1045 data.ioc_net = g_net;
1047 data.ioc_u32[0] = ipaddr;
1049 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
1051 fprintf(stderr, "failed to remove connection: %s\n",
1059 int jt_ptl_push_connection(int argc, char **argv)
1061 struct libcfs_ioctl_data data;
1063 lnet_nid_t nid = LNET_NID_ANY;
1066 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
1070 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
1073 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
1074 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1078 LIBCFS_IOC_INIT(data);
1079 data.ioc_net = g_net;
1082 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
1084 fprintf(stderr, "failed to push connection: %s\n",
1092 #ifndef IOC_LIBCFS_PING_PEER
1093 #define IOC_LIBCFS_PING_PEER _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
1096 int jt_ptl_ping(int argc, char **argv)
1098 bool done = false, print = true;
1101 struct lnet_process_id id;
1102 yaml_emitter_t request;
1103 yaml_parser_t reply;
1109 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
1113 sep = strchr(argv[1], '-');
1115 rc = lnet_parse_nid(argv[1], &id);
1121 if (argv[1][0] == 'u' || argv[1][0] == 'U')
1122 id.pid = strtoul(&argv[1][1], &end, 0) |
1125 id.pid = strtoul(argv[1], &end, 0);
1127 if (end != sep) { /* assuming '-' is part of hostname */
1128 rc = lnet_parse_nid(argv[1], &id);
1132 id.nid = libcfs_str2nid(sep + 1);
1134 if (id.nid == LNET_NID_ANY) {
1136 "Can't parse process id \"%s\"\n",
1144 timeout = 1000 * atol(argv[2]);
1145 if (timeout > 120 * 1000) {
1146 fprintf(stderr, "Timeout %s is to large\n",
1151 timeout = 1000; /* default 1 second timeout */
1154 /* Create Netlink emitter to send request to kernel */
1155 sk = nl_socket_alloc();
1159 /* Setup parser to recieve Netlink packets */
1160 rc = yaml_parser_initialize(&reply);
1164 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1168 /* Create Netlink emitter to send request to kernel */
1169 rc = yaml_emitter_initialize(&request);
1173 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
1174 LNET_GENL_VERSION, LNET_CMD_PING,
1179 yaml_emitter_open(&request);
1180 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1181 rc = yaml_emitter_emit(&request, &event);
1185 yaml_mapping_start_event_initialize(&event, NULL,
1186 (yaml_char_t *)YAML_MAP_TAG,
1187 1, YAML_ANY_MAPPING_STYLE);
1188 rc = yaml_emitter_emit(&request, &event);
1192 yaml_scalar_event_initialize(&event, NULL,
1193 (yaml_char_t *)YAML_STR_TAG,
1194 (yaml_char_t *)"ping",
1195 strlen("ping"), 1, 0,
1196 YAML_PLAIN_SCALAR_STYLE);
1197 rc = yaml_emitter_emit(&request, &event);
1201 yaml_mapping_start_event_initialize(&event, NULL,
1202 (yaml_char_t *)YAML_MAP_TAG,
1203 1, YAML_ANY_MAPPING_STYLE);
1204 rc = yaml_emitter_emit(&request, &event);
1208 if (timeout != 1000) {
1209 yaml_scalar_event_initialize(&event, NULL,
1210 (yaml_char_t *)YAML_STR_TAG,
1211 (yaml_char_t *)"timeout",
1212 strlen("timeout"), 1, 0,
1213 YAML_PLAIN_SCALAR_STYLE);
1214 rc = yaml_emitter_emit(&request, &event);
1218 yaml_scalar_event_initialize(&event, NULL,
1219 (yaml_char_t *)YAML_INT_TAG,
1220 (yaml_char_t *)argv[2],
1221 strlen(argv[2]), 1, 0,
1222 YAML_PLAIN_SCALAR_STYLE);
1223 rc = yaml_emitter_emit(&request, &event);
1228 yaml_scalar_event_initialize(&event, NULL,
1229 (yaml_char_t *)YAML_STR_TAG,
1230 (yaml_char_t *)"nids",
1231 strlen("nids"), 1, 0,
1232 YAML_PLAIN_SCALAR_STYLE);
1233 rc = yaml_emitter_emit(&request, &event);
1237 yaml_sequence_start_event_initialize(&event, NULL,
1238 (yaml_char_t *)YAML_SEQ_TAG,
1239 1, YAML_FLOW_SEQUENCE_STYLE);
1240 rc = yaml_emitter_emit(&request, &event);
1244 /* convert NID to string, in case libcfs_str2nid() did name lookup */
1245 yaml_scalar_event_initialize(&event, NULL,
1246 (yaml_char_t *)YAML_STR_TAG,
1247 (yaml_char_t *)libcfs_nid2str(id.nid),
1248 strlen(libcfs_nid2str(id.nid)), 1, 0,
1249 YAML_PLAIN_SCALAR_STYLE);
1250 rc = yaml_emitter_emit(&request, &event);
1254 yaml_sequence_end_event_initialize(&event);
1255 rc = yaml_emitter_emit(&request, &event);
1259 yaml_mapping_end_event_initialize(&event);
1260 rc = yaml_emitter_emit(&request, &event);
1264 yaml_mapping_end_event_initialize(&event);
1265 rc = yaml_emitter_emit(&request, &event);
1269 yaml_document_end_event_initialize(&event, 0);
1270 rc = yaml_emitter_emit(&request, &event);
1274 rc = yaml_emitter_close(&request);
1277 yaml_emitter_log_error(&request, stderr);
1281 yaml_emitter_delete(&request);
1283 /* Now parse the reply results */
1285 rc = yaml_parser_parse(&reply, &event);
1289 if (event.type != YAML_SCALAR_EVENT)
1292 if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
1293 yaml_event_delete(&event);
1294 rc = yaml_parser_parse(&reply, &event);
1296 yaml_event_delete(&event);
1300 /* Print 0@lo. Its not sent */
1301 printf("12345-0@lo\n");
1304 printf("%s\n", (char *)event.data.scalar.value);
1305 } else if (strcmp((char *)event.data.scalar.value,
1307 yaml_event_delete(&event);
1308 rc = yaml_parser_parse(&reply, &event);
1310 yaml_event_delete(&event);
1313 rc = strtol((char *)event.data.scalar.value, NULL, 10);
1314 fprintf(stdout, "failed to ping %s: %s\n",
1315 argv[1], strerror(-rc));
1316 break; /* "rc" is clobbered if loop is run again */
1319 done = (event.type == YAML_STREAM_END_EVENT);
1320 yaml_event_delete(&event);
1324 /* yaml_* functions return 0 for error */
1325 const char *msg = yaml_parser_get_reader_error(&reply);
1327 rc = errno ? -errno : -EHOSTUNREACH;
1328 if (strcmp(msg, "Unspecific failure") != 0) {
1329 fprintf(stdout, "failed to ping %s: %s\n",
1332 fprintf(stdout, "failed to ping %s: %s\n",
1333 argv[1], strerror(errno));
1335 } else if (rc == 1) {
1336 /* yaml_* functions return 1 for success */
1339 yaml_parser_delete(&reply);
1343 #ifdef IOC_LIBCFS_PING_PEER
1345 struct lnet_process_id ids[LNET_INTERFACES_MAX_DEFAULT];
1346 int maxids = sizeof(ids) / sizeof(ids[0]);
1347 struct lnet_ioctl_ping_data ping = { { 0 } };
1353 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
1354 ping.ping_hdr.ioc_len = sizeof(ping);
1356 ping.ping_src = LNET_NID_ANY;
1357 ping.op_param = timeout;
1358 ping.ping_count = maxids;
1359 ping.ping_buf = ids;
1361 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING_PEER, &ping);
1363 fprintf(stderr, "failed to ping %s: %s\n", argv[1],
1368 for (i = 0; i < ping.ping_count && i < maxids; i++)
1369 printf("%s\n", libcfs_id2str(ids[i]));
1371 if (ping.ping_count > maxids)
1372 printf("%d out of %d ids listed\n", maxids, ping.ping_count);
1380 int jt_ptl_mynid(int argc, char **argv)
1382 struct libcfs_ioctl_data data;
1387 fprintf(stderr, "usage: %s NID\n", argv[0]);
1391 nid = libcfs_str2nid(argv[1]);
1392 if (nid == LNET_NID_ANY) {
1393 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1397 LIBCFS_IOC_INIT(data);
1398 data.ioc_net = LNET_NIDNET(nid);
1401 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1403 fprintf(stderr, "setting my NID failed: %s\n",
1406 printf("registered my nid %s\n", libcfs_nid2str(nid));
1412 jt_ptl_fail_nid(int argc, char **argv)
1417 struct libcfs_ioctl_data data;
1419 if (argc < 2 || argc > 3) {
1420 fprintf(stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n",
1425 if (!libcfs_str2anynid(&nid, argv[1])) {
1426 fprintf(stderr, "Can't parse nid \"%s\"\n", argv[1]);
1431 threshold = LNET_MD_THRESH_INF;
1432 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1433 fprintf(stderr, "Can't parse count \"%s\"\n", argv[2]);
1437 LIBCFS_IOC_INIT(data);
1439 data.ioc_count = threshold;
1441 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1443 fprintf(stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1447 threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1452 static int ptl_yaml_route_display(yaml_parser_t *reply)
1454 char gw[LNET_MAX_STR_LEN], net[18];
1455 bool done = false, alive = false;
1456 int hops = -1, prio = -1;
1460 /* Now parse the reply results */
1464 rc = yaml_parser_parse(reply, &event);
1468 if (event.type == YAML_SEQUENCE_END_EVENT) {
1469 printf("net %18s hops %d gw %32.128s %s pri %u\n",
1470 net, hops, gw, alive ? "up" : "down",
1472 memset(net, '\0', sizeof(net));
1473 memset(gw, '\0', sizeof(gw));
1478 if (event.type != YAML_SCALAR_EVENT)
1481 value = (char *)event.data.scalar.value;
1482 if (strcmp(value, "net") == 0) {
1483 yaml_event_delete(&event);
1484 rc = yaml_parser_parse(reply, &event);
1486 yaml_event_delete(&event);
1490 value = (char *)event.data.scalar.value;
1491 strncpy(net, value, sizeof(net) - 1);
1492 } else if (strcmp(value, "gateway") == 0) {
1493 yaml_event_delete(&event);
1494 rc = yaml_parser_parse(reply, &event);
1496 yaml_event_delete(&event);
1500 value = (char *)event.data.scalar.value;
1501 strncpy(gw, value, sizeof(gw) - 1);
1502 } else if (strcmp(value, "state") == 0) {
1503 yaml_event_delete(&event);
1504 rc = yaml_parser_parse(reply, &event);
1506 yaml_event_delete(&event);
1510 value = (char *)event.data.scalar.value;
1511 if (strcmp(value, "up") == 0) {
1513 } else if (strcmp(value, "down") == 0) {
1516 } else if (strcmp(value, "hop") == 0) {
1517 yaml_event_delete(&event);
1518 rc = yaml_parser_parse(reply, &event);
1520 yaml_event_delete(&event);
1524 value = (char *)event.data.scalar.value;
1525 hops = strtol(value, NULL, 10);
1526 } else if (strcmp(value, "priority") == 0) {
1527 yaml_event_delete(&event);
1528 rc = yaml_parser_parse(reply, &event);
1530 yaml_event_delete(&event);
1534 value = (char *)event.data.scalar.value;
1535 prio = strtol(value, NULL, 10);
1538 done = (event.type == YAML_STREAM_END_EVENT);
1539 yaml_event_delete(&event);
1546 static int ptl_yaml_route(char *nw, char *gws, int hops, int prio, bool enable,
1547 time_t notify_time, int flags, int version)
1549 struct nl_sock *sk = NULL;
1550 const char *msg = NULL;
1551 yaml_emitter_t output;
1552 yaml_parser_t reply;
1556 sk = nl_socket_alloc();
1560 /* Setup parser to receive Netlink packets */
1561 rc = yaml_parser_initialize(&reply);
1567 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1569 msg = yaml_parser_get_reader_error(&reply);
1573 /* Create Netlink emitter to send request to kernel */
1574 rc = yaml_emitter_initialize(&output);
1576 msg = "failed to initialize emitter";
1580 rc = yaml_emitter_set_output_netlink(&output, sk, LNET_GENL_NAME,
1581 version, LNET_CMD_ROUTES, flags);
1585 yaml_emitter_open(&output);
1586 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1587 rc = yaml_emitter_emit(&output, &event);
1591 yaml_mapping_start_event_initialize(&event, NULL,
1592 (yaml_char_t *)YAML_MAP_TAG,
1593 1, YAML_ANY_MAPPING_STYLE);
1594 rc = yaml_emitter_emit(&output, &event);
1598 yaml_scalar_event_initialize(&event, NULL,
1599 (yaml_char_t *)YAML_STR_TAG,
1600 (yaml_char_t *)"route",
1601 strlen("route"), 1, 0,
1602 YAML_PLAIN_SCALAR_STYLE);
1603 rc = yaml_emitter_emit(&output, &event);
1608 yaml_sequence_start_event_initialize(&event, NULL,
1609 (yaml_char_t *)YAML_SEQ_TAG,
1611 YAML_BLOCK_SEQUENCE_STYLE);
1612 rc = yaml_emitter_emit(&output, &event);
1616 yaml_mapping_start_event_initialize(&event, NULL,
1617 (yaml_char_t *)YAML_MAP_TAG, 1,
1618 YAML_BLOCK_MAPPING_STYLE);
1619 rc = yaml_emitter_emit(&output, &event);
1624 yaml_scalar_event_initialize(&event, NULL,
1625 (yaml_char_t *)YAML_STR_TAG,
1626 (yaml_char_t *)"net",
1627 strlen("net"), 1, 0,
1628 YAML_PLAIN_SCALAR_STYLE);
1629 rc = yaml_emitter_emit(&output, &event);
1633 yaml_scalar_event_initialize(&event, NULL,
1634 (yaml_char_t *)YAML_STR_TAG,
1637 YAML_PLAIN_SCALAR_STYLE);
1638 rc = yaml_emitter_emit(&output, &event);
1644 yaml_scalar_event_initialize(&event, NULL,
1645 (yaml_char_t *)YAML_STR_TAG,
1646 (yaml_char_t *)"gateway",
1647 strlen("gateway"), 1, 0,
1648 YAML_PLAIN_SCALAR_STYLE);
1649 rc = yaml_emitter_emit(&output, &event);
1653 yaml_scalar_event_initialize(&event, NULL,
1654 (yaml_char_t *)YAML_STR_TAG,
1657 YAML_PLAIN_SCALAR_STYLE);
1658 rc = yaml_emitter_emit(&output, &event);
1664 char when[INT_STRING_LEN];
1666 yaml_scalar_event_initialize(&event, NULL,
1667 (yaml_char_t *)YAML_STR_TAG,
1668 (yaml_char_t *)"notify_time",
1669 strlen("notify_time"), 1, 0,
1670 YAML_PLAIN_SCALAR_STYLE);
1671 rc = yaml_emitter_emit(&output, &event);
1675 snprintf(when, sizeof(when), "%ld", notify_time);
1676 yaml_scalar_event_initialize(&event, NULL,
1677 (yaml_char_t *)YAML_INT_TAG,
1678 (yaml_char_t *)when,
1680 YAML_PLAIN_SCALAR_STYLE);
1681 rc = yaml_emitter_emit(&output, &event);
1686 yaml_scalar_event_initialize(&event, NULL,
1687 (yaml_char_t *)YAML_STR_TAG,
1688 (yaml_char_t *)"state",
1689 strlen("state"), 1, 0,
1690 YAML_PLAIN_SCALAR_STYLE);
1691 rc = yaml_emitter_emit(&output, &event);
1696 yaml_scalar_event_initialize(&event, NULL,
1697 (yaml_char_t *)YAML_STR_TAG,
1698 (yaml_char_t *)"up",
1700 YAML_PLAIN_SCALAR_STYLE);
1702 yaml_scalar_event_initialize(&event, NULL,
1703 (yaml_char_t *)YAML_STR_TAG,
1704 (yaml_char_t *)"down",
1705 strlen("down"), 1, 0,
1706 YAML_PLAIN_SCALAR_STYLE);
1708 rc = yaml_emitter_emit(&output, &event);
1712 yaml_mapping_end_event_initialize(&event);
1713 rc = yaml_emitter_emit(&output, &event);
1717 yaml_sequence_end_event_initialize(&event);
1718 rc = yaml_emitter_emit(&output, &event);
1722 yaml_scalar_event_initialize(&event, NULL,
1723 (yaml_char_t *)YAML_STR_TAG,
1726 YAML_PLAIN_SCALAR_STYLE);
1727 rc = yaml_emitter_emit(&output, &event);
1732 yaml_mapping_end_event_initialize(&event);
1733 rc = yaml_emitter_emit(&output, &event);
1737 yaml_document_end_event_initialize(&event, 0);
1738 rc = yaml_emitter_emit(&output, &event);
1742 rc = yaml_emitter_close(&output);
1745 yaml_emitter_log_error(&output, stderr);
1748 if (flags != NLM_F_DUMP) {
1749 yaml_document_t errmsg;
1751 rc = yaml_parser_load(&reply, &errmsg);
1753 yaml_emitter_t debug;
1755 rc = yaml_emitter_initialize(&debug);
1757 yaml_emitter_set_indent(&debug,
1758 LNET_DEFAULT_INDENT);
1759 yaml_emitter_set_output_file(&debug,
1761 rc = yaml_emitter_dump(&debug,
1763 } else if (rc == 0) {
1764 yaml_emitter_log_error(&debug, stderr);
1767 yaml_emitter_delete(&debug);
1769 yaml_document_delete(&errmsg);
1771 rc = ptl_yaml_route_display(&reply);
1774 msg = yaml_parser_get_reader_error(&reply);
1776 yaml_emitter_delete(&output);
1779 fprintf(stdout, "%s\n", msg);
1780 yaml_parser_delete(&reply);
1783 return rc == 1 ? 0 : rc;
1787 jt_ptl_add_route(int argc, char **argv)
1789 struct lnet_ioctl_config_data data;
1790 lnet_nid_t gateway_nid;
1791 __u32 hops = LNET_UNDEFINED_HOPS;
1792 unsigned int priority = 0;
1796 if (argc < 2 || argc > 4) {
1797 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
1802 if (g_net_is_set(argv[0]) == 0)
1805 gateway_nid = libcfs_str2nid(argv[1]);
1806 if (gateway_nid == LNET_NID_ANY) {
1807 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1812 hops = strtol(argv[2], &end, 0);
1813 if (hops == 0 || hops >= 256 ||
1814 (end && *end != 0)) {
1815 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
1820 priority = strtoul(argv[3], &end, 0);
1821 if (end && *end != 0) {
1823 "Can't parse priority \"%s\"\n",
1830 rc = ptl_yaml_route(libcfs_net2str(g_net), argv[1], hops,
1831 priority, false, 0, NLM_F_CREATE, LNET_GENL_VERSION);
1833 if (rc == -EOPNOTSUPP)
1838 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1839 data.cfg_net = g_net;
1840 data.cfg_config_u.cfg_route.rtr_hop = hops;
1841 data.cfg_nid = gateway_nid;
1842 data.cfg_config_u.cfg_route.rtr_priority = priority;
1844 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1846 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
1855 jt_ptl_del_route(int argc, char **argv)
1857 struct lnet_ioctl_config_data data;
1862 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
1866 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
1867 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
1871 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
1872 -1, -1, false, 0, 0, LNET_GENL_VERSION);
1874 if (rc == -EOPNOTSUPP)
1879 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1880 data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
1883 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1885 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
1886 libcfs_nid2str(nid), strerror(errno));
1894 jt_ptl_notify_router(int argc, char **argv)
1896 struct libcfs_ioctl_data data;
1904 fprintf(stderr, "usage: %s targetNID <up/down> [<time>]\n",
1909 nid = libcfs_str2nid(argv[1]);
1910 if (nid == LNET_NID_ANY) {
1911 fprintf(stderr, "Can't parse target NID \"%s\"\n", argv[1]);
1915 if (lnet_parse_bool (&enable, argv[2]) != 0) {
1916 fprintf(stderr, "Can't parse boolean %s\n", argv[2]);
1920 gettimeofday(&now, NULL);
1924 } else if (lnet_parse_time(&when, argv[3]) != 0) {
1926 "Can't parse time %s\n Please specify either 'YYYY-MM-DD-HH:MM:SS'\n or an absolute unix time in seconds\n",
1929 } else if (when > now.tv_sec) {
1930 fprintf(stderr, "%s specifies a time in the future\n",
1935 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
1936 -1, -1, enable, when, NLM_F_REPLACE, LNET_GENL_VERSION);
1938 if (rc == -EOPNOTSUPP)
1943 LIBCFS_IOC_INIT(data);
1945 data.ioc_flags = enable;
1946 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
1947 data.ioc_u64[0] = (__u64)when;
1949 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
1951 fprintf(stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
1952 libcfs_nid2str(nid), strerror(errno));
1960 jt_ptl_print_routes(int argc, char **argv)
1962 struct lnet_ioctl_config_data data;
1971 rc = ptl_yaml_route(NULL, NULL, -1, -1, false, 0, NLM_F_DUMP,
1974 if (rc == -EOPNOTSUPP)
1979 for (index = 0; ; index++) {
1980 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1981 data.cfg_count = index;
1983 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1988 hops = data.cfg_config_u.cfg_route.rtr_hop;
1990 alive = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
1991 pri = data.cfg_config_u.cfg_route.rtr_priority;
1993 printf("net %18s hops %d gw %32s %s pri %u\n",
1994 libcfs_net2str(net), hops,
1995 libcfs_nid2str(nid), alive ? "up" : "down", pri);
1998 if (errno != ENOENT)
1999 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
2006 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
2012 /* NB: can't support range ipaddress except * and *@net */
2013 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
2014 net = libcfs_str2net(str + 2);
2015 if (net == LNET_NET_ANY)
2018 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
2020 rc = libcfs_str2anynid(&nid, str);
2028 fprintf(stderr, "Invalid NID : %s\n", str);
2033 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
2035 if (!strcasecmp(msg_str, "put")) {
2036 *mask_p |= LNET_PUT_BIT;
2039 } else if (!strcasecmp(msg_str, "ack")) {
2040 *mask_p |= LNET_ACK_BIT;
2043 } else if (!strcasecmp(msg_str, "get")) {
2044 *mask_p |= LNET_GET_BIT;
2047 } else if (!strcasecmp(msg_str, "reply")) {
2048 *mask_p |= LNET_REPLY_BIT;
2052 fprintf(stderr, "unknown message type %s\n", msg_str);
2057 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
2059 unsigned long rc = strtoul(optarg, NULL, 0);
2062 fprintf(stderr, "invalid portal: %lu\n", rc);
2066 *mask_p |= (1ULL << rc);
2071 fault_attr_health_error_parse(char *error, __u32 *mask)
2073 if (!strcasecmp(error, "local_interrupt")) {
2074 *mask |= HSTATUS_LOCAL_INTERRUPT_BIT;
2077 if (!strcasecmp(error, "local_dropped")) {
2078 *mask |= HSTATUS_LOCAL_DROPPED_BIT;
2081 if (!strcasecmp(error, "local_aborted")) {
2082 *mask |= HSTATUS_LOCAL_ABORTED_BIT;
2085 if (!strcasecmp(error, "local_no_route")) {
2086 *mask |= HSTATUS_LOCAL_NO_ROUTE_BIT;
2089 if (!strcasecmp(error, "local_error")) {
2090 *mask |= HSTATUS_LOCAL_ERROR_BIT;
2093 if (!strcasecmp(error, "local_timeout")) {
2094 *mask |= HSTATUS_LOCAL_TIMEOUT_BIT;
2097 if (!strcasecmp(error, "remote_error")) {
2098 *mask |= HSTATUS_REMOTE_ERROR_BIT;
2101 if (!strcasecmp(error, "remote_dropped")) {
2102 *mask |= HSTATUS_REMOTE_DROPPED_BIT;
2105 if (!strcasecmp(error, "remote_timeout")) {
2106 *mask |= HSTATUS_REMOTE_TIMEOUT_BIT;
2109 if (!strcasecmp(error, "network_timeout")) {
2110 *mask |= HSTATUS_NETWORK_TIMEOUT_BIT;
2113 if (!strcasecmp(error, "random")) {
2114 *mask = HSTATUS_RANDOM;
2122 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
2124 struct libcfs_ioctl_data data = { { 0 } };
2125 struct lnet_fault_attr attr;
2129 static const struct option opts[] = {
2130 { .name = "source", .has_arg = required_argument, .val = 's' },
2131 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2132 { .name = "rate", .has_arg = required_argument, .val = 'r' },
2133 { .name = "interval", .has_arg = required_argument, .val = 'i' },
2134 { .name = "random", .has_arg = no_argument, .val = 'n' },
2135 { .name = "latency", .has_arg = required_argument, .val = 'l' },
2136 { .name = "portal", .has_arg = required_argument, .val = 'p' },
2137 { .name = "message", .has_arg = required_argument, .val = 'm' },
2138 { .name = "health_error", .has_arg = required_argument, .val = 'e' },
2139 { .name = "local_nid", .has_arg = required_argument, .val = 'o' },
2140 { .name = "drop_all", .has_arg = no_argument, .val = 'x' },
2145 "Failed, please provide source, destination and rate of rule\n");
2149 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
2150 memset(&attr, 0, sizeof(attr));
2152 int c = getopt_long(argc, argv, optstr, opts, NULL);
2159 rc = fault_attr_nid_parse(optarg, &attr.fa_local_nid);
2163 case 's': /* source NID/NET */
2164 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2169 case 'd': /* dest NID/NET */
2170 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2175 case 'r': /* drop rate */
2176 if (opc == LNET_CTL_DROP_ADD)
2177 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
2179 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
2183 if (opc == LNET_CTL_DROP_ADD) {
2184 rc = fault_attr_health_error_parse(optarg,
2185 &attr.u.drop.da_health_error_mask);
2192 if (opc == LNET_CTL_DROP_ADD)
2193 attr.u.drop.da_drop_all = true;
2197 if (opc == LNET_CTL_DROP_ADD)
2198 attr.u.drop.da_random = true;
2201 case 'i': /* time interval (# seconds) for message drop */
2202 if (opc == LNET_CTL_DROP_ADD)
2203 attr.u.drop.da_interval = strtoul(optarg,
2206 attr.u.delay.la_interval = strtoul(optarg,
2210 case 'l': /* seconds to wait before activating rule */
2211 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
2214 case 'p': /* portal to filter */
2215 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
2220 case 'm': /* message types to filter */
2221 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
2227 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2228 argv[0], argv[optind - 1]);
2234 if (opc == LNET_CTL_DROP_ADD) {
2235 /* NB: drop rate and interval are exclusive to each other */
2236 if (!((attr.u.drop.da_rate == 0) ^
2237 (attr.u.drop.da_interval == 0))) {
2239 "please provide either drop rate or interval but not both at the same time.\n");
2243 if (attr.u.drop.da_random &&
2244 attr.u.drop.da_interval == 0) {
2246 "please provide an interval to randomize\n");
2249 } else if (opc == LNET_CTL_DELAY_ADD) {
2250 if (!((attr.u.delay.la_rate == 0) ^
2251 (attr.u.delay.la_interval == 0))) {
2253 "please provide either delay rate or interval but not both at the same time.\n");
2257 if (attr.u.delay.la_latency == 0) {
2258 fprintf(stderr, "latency cannot be zero\n");
2263 if (attr.fa_src == 0 || attr.fa_dst == 0) {
2265 "Please provide both source and destination of %s rule\n",
2270 if (attr.fa_local_nid == 0)
2271 attr.fa_local_nid = LNET_NID_ANY;
2273 data.ioc_flags = opc;
2274 data.ioc_inllen1 = sizeof(attr);
2275 data.ioc_inlbuf1 = (char *)&attr;
2276 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2277 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2281 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2283 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
2284 name, libcfs_nid2str(attr.fa_src),
2285 libcfs_nid2str(attr.fa_dst), strerror(errno));
2289 printf("Added %s rule %s->%s (1/%d)\n",
2290 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
2291 opc == LNET_CTL_DROP_ADD ?
2292 attr.u.drop.da_rate : attr.u.delay.la_rate);
2301 jt_ptl_drop_add(int argc, char **argv)
2303 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
2307 jt_ptl_delay_add(int argc, char **argv)
2309 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
2313 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
2315 struct libcfs_ioctl_data data = { { 0 } };
2316 struct lnet_fault_attr attr;
2320 static const struct option opts[] = {
2321 { .name = "source", .has_arg = required_argument, .val = 's' },
2322 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2323 { .name = "all", .has_arg = no_argument, .val = 'a' },
2328 "Failed, please provide source and destination of rule\n");
2332 memset(&attr, 0, sizeof(attr));
2334 int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
2341 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2346 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2357 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2358 argv[0], argv[optind - 1]);
2364 data.ioc_flags = opc;
2365 data.ioc_inllen1 = sizeof(attr);
2366 data.ioc_inlbuf1 = (char *)&attr;
2367 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2368 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2372 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2374 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
2375 all ? "all" : libcfs_nid2str(attr.fa_src),
2376 all ? "all" : libcfs_nid2str(attr.fa_dst),
2381 libcfs_ioctl_unpack(&data, ioc_buf);
2382 printf("Removed %d %s rules\n", data.ioc_count, name);
2391 jt_ptl_drop_del(int argc, char **argv)
2393 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
2397 jt_ptl_delay_del(int argc, char **argv)
2399 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
2403 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
2405 struct libcfs_ioctl_data data = { { 0 } };
2408 LIBCFS_IOC_INIT(data);
2409 data.ioc_flags = opc;
2411 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
2413 fprintf(stderr, "failed to reset %s stats: %s\n",
2414 name, strerror(errno));
2421 jt_ptl_drop_reset(int argc, char **argv)
2423 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
2427 jt_ptl_delay_reset(int argc, char **argv)
2429 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
2434 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
2436 struct libcfs_ioctl_data data = { { 0 } };
2437 struct lnet_fault_attr attr;
2438 struct lnet_fault_stat stat;
2441 printf("LNet %s rules:\n", name);
2442 for (pos = 0;; pos++) {
2445 memset(&attr, 0, sizeof(attr));
2446 memset(&stat, 0, sizeof(stat));
2448 data.ioc_count = pos;
2449 data.ioc_flags = opc;
2450 data.ioc_inllen1 = sizeof(attr);
2451 data.ioc_inlbuf1 = (char *)&attr;
2452 data.ioc_inllen2 = sizeof(stat);
2453 data.ioc_inlbuf2 = (char *)&stat;
2454 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2455 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2459 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2463 libcfs_ioctl_unpack(&data, ioc_buf);
2465 if (opc == LNET_CTL_DROP_LIST) {
2466 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2467 libcfs_nid2str(attr.fa_src),
2468 libcfs_nid2str(attr.fa_dst),
2469 attr.u.drop.da_rate, attr.u.drop.da_interval,
2470 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2471 (uintmax_t)stat.u.drop.ds_dropped,
2472 (uintmax_t)stat.fs_count,
2473 (uintmax_t)stat.fs_put,
2474 (uintmax_t)stat.fs_ack,
2475 (uintmax_t)stat.fs_get,
2476 (uintmax_t)stat.fs_reply);
2478 } else if (opc == LNET_CTL_DELAY_LIST) {
2479 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2480 libcfs_nid2str(attr.fa_src),
2481 libcfs_nid2str(attr.fa_dst),
2482 attr.u.delay.la_rate, attr.u.delay.la_interval,
2483 attr.u.delay.la_latency,
2484 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2485 (uintmax_t)stat.u.delay.ls_delayed,
2486 (uintmax_t)stat.fs_count,
2487 (uintmax_t)stat.fs_put,
2488 (uintmax_t)stat.fs_ack,
2489 (uintmax_t)stat.fs_get,
2490 (uintmax_t)stat.fs_reply);
2493 printf("found total %d\n", pos);
2499 jt_ptl_drop_list(int argc, char **argv)
2501 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
2505 jt_ptl_delay_list(int argc, char **argv)
2507 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
2511 get_cycles_per_usec()
2513 FILE *f = fopen("/proc/cpuinfo", "r");
2518 while (fgets(line, sizeof(line), f))
2519 if (sscanf(line, "cpu MHz : %lf", &mhz) == 1) {
2526 fprintf(stderr, "Can't read/parse /proc/cpuinfo\n");
2530 int jt_ptl_testprotocompat(int argc, char **argv)
2532 struct libcfs_ioctl_data data;
2538 fprintf(stderr, "usage: %s <number>\n", argv[0]);
2542 flags = strtol(argv[1], &end, 0);
2543 if (flags < 0 || *end != 0) {
2544 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
2548 LIBCFS_IOC_INIT(data);
2549 data.ioc_flags = flags;
2550 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
2553 fprintf(stderr, "test proto compat %x failed: %s\n",
2554 flags, strerror(errno));
2558 printf("test proto compat %x OK\n", flags);