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>
53 unsigned int libcfs_debug;
54 unsigned int libcfs_printk = D_CANTMASK;
56 static bool g_net_interactive;
57 static bool g_net_set;
60 #define IOC_BUF_SIZE 8192
61 static char local_buf[IOC_BUF_SIZE];
62 static char *ioc_buf = local_buf;
64 /* Convert a string boolean to an int; "enable" -> 1 */
66 lnet_parse_bool (int *b, char *str)
68 if (!strcasecmp(str, "no") ||
69 !strcasecmp(str, "n") ||
70 !strcasecmp(str, "off") ||
71 !strcasecmp(str, "down") ||
72 !strcasecmp(str, "disable")) {
78 if (!strcasecmp(str, "yes") ||
79 !strcasecmp(str, "y") ||
80 !strcasecmp(str, "on") ||
81 !strcasecmp(str, "up") ||
82 !strcasecmp(str, "enable")) {
92 lnet_parse_port(int *port, char *str)
96 *port = strtol(str, &end, 0);
98 if (*end == 0 && /* parsed whole string */
99 *port > 0 && *port < 65536) /* minimal sanity check */
106 lnet_parse_ipquad(__u32 *ipaddrp, char *str)
110 if (sscanf(str, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 &&
111 (a & ~0xff) == 0 && (b & ~0xff) == 0 &&
112 (c & ~0xff) == 0 && (d & ~0xff) == 0) {
113 *ipaddrp = (a << 24) | (b << 16) | (c << 8) | d;
122 lnet_parse_ipaddr(__u32 *ipaddrp, char *str)
124 struct addrinfo *ai = NULL;
125 struct addrinfo *aip = NULL;
126 struct addrinfo hints;
130 if (!strcmp(str, "_all_")) {
135 if (lnet_parse_ipquad(ipaddrp, str) == 0)
138 memset(&hints, 0, sizeof(struct addrinfo));
139 hints.ai_family = AF_INET;
141 if (('a' <= str[0] && str[0] <= 'z') ||
142 ('A' <= str[0] && str[0] <= 'Z')) {
143 err = getaddrinfo(str, NULL, &hints, &ai);
146 "failed to get addrinfo for %s: %s\n",
147 str, gai_strerror(err));
151 for (aip = ai; aip; aip = aip->ai_next) {
152 if (aip->ai_family == AF_INET && aip->ai_addr) {
153 struct sockaddr_in *sin =
156 __u32 addr = (__u32)sin->sin_addr.s_addr;
157 *ipaddrp = ntohl(addr);
161 /* FIXME: handle AF_INET6 */
164 fprintf(stderr, "failed to get IP address for %s\n",
181 ptl_ipaddr_2_str(__u32 ipaddr, char *str, size_t strsize, int lookup)
183 struct sockaddr_in srcaddr;
186 memset(&srcaddr, 0, sizeof(srcaddr));
187 srcaddr.sin_family = AF_INET;
188 srcaddr.sin_addr.s_addr = (in_addr_t)htonl(ipaddr);
190 if (getnameinfo((struct sockaddr *)&srcaddr, sizeof(srcaddr),
191 str, strsize, NULL, 0, 0) == 0)
195 snprintf(str, strsize, "%d.%d.%d.%d",
196 (ipaddr >> 24) & 0xff, (ipaddr >> 16) & 0xff,
197 (ipaddr >> 8) & 0xff, ipaddr & 0xff);
203 lnet_parse_time(time_t *t, char *str)
209 *t = strtol(str, &end, 0);
210 if (*end == 0) /* parsed whole string */
213 memset(&tm, 0, sizeof(tm));
214 n = sscanf(str, "%d-%d-%d-%d:%d:%d",
215 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
216 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
220 tm.tm_mon--; /* convert to 0 == Jan */
221 tm.tm_year -= 1900; /* y2k quirk */
222 tm.tm_isdst = -1; /* dunno if it's daylight savings... */
225 if (*t == (time_t)-1)
232 lnet_parse_nid(char *nid_str, struct lnet_processid *id_ptr)
234 id_ptr->pid = LNET_PID_ANY;
235 if (libcfs_strnid(&id_ptr->nid, nid_str) < 0 ||
236 LNET_NID_IS_ANY(&id_ptr->nid)) {
237 fprintf(stderr, "Invalid NID argument \"%s\"\n", nid_str);
244 static int g_net_is_set(char *cmd)
252 if (g_net_interactive)
258 "You must run '%s <network>' command before '%s'\n",
266 static int g_net_is_compatible(char *cmd, ...)
271 if (!g_net_is_set(cmd))
277 nal = va_arg(ap, int);
278 if (nal == LNET_NETTYP(g_net)) {
287 fprintf(stderr, "Command %s not compatible with %s NAL\n",
288 cmd, libcfs_lnd2str(LNET_NETTYP(g_net)));
293 int ptl_initialize(int argc, char **argv)
296 g_net_interactive = true;
298 register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
303 int jt_ptl_network(int argc, char **argv)
305 struct libcfs_ioctl_data data;
306 __u32 net = LNET_NET_ANY;
307 const char *msg = NULL;
311 fprintf(stderr, "usage: %s <net>|up|down\n", argv[0]);
315 if (!strcmp(argv[1], "unconfigure") || !strcmp(argv[1], "down")) {
316 rc = yaml_lnet_configure(0, &msg);
317 if (rc != -EOPNOTSUPP) {
320 printf("LNET ready to unload\n");
327 printf("LNET unconfigure error %u: %s\n",
334 LIBCFS_IOC_INIT(data);
335 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_UNCONFIGURE, &data);
337 printf("LNET ready to unload\n");
341 if (errno == ENODEV) {
342 printf("LNET is currently not loaded.");
347 fprintf(stderr, "LNET busy\n");
349 fprintf(stderr, "LNET unconfigure error %d: %s\n",
350 errno, strerror(errno));
352 } else if (!strcmp(argv[1], "configure") || !strcmp(argv[1], "up")) {
353 rc = yaml_lnet_configure(NLM_F_CREATE, &msg);
354 if (rc != -EOPNOTSUPP) {
357 printf("LNET configured\n");
360 fprintf(stderr, "LNET configure error %u: %s\n",
367 LIBCFS_IOC_INIT(data);
368 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIGURE, &data);
371 printf("LNET configured\n");
375 fprintf(stderr, "LNET configure error %d: %s\n",
376 errno, strerror(errno));
380 net = libcfs_str2net(argv[1]);
381 if (net == LNET_NET_ANY) {
382 fprintf(stderr, "Can't parse net %s\n", argv[1]);
391 #ifndef IOC_LIBCFS_GET_NI
392 #define IOC_LIBCFS_GET_NI _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
396 jt_ptl_list_nids(int argc, char **argv)
398 int all = 0, return_nid = 0;
399 yaml_emitter_t request;
406 all = (argc == 2) && (strcmp(argv[1], "all") == 0);
407 /* Hack to pass back value */
408 return_nid = (argc == 2) && (argv[1][0] == 1);
410 if ((argc > 2) && !(all || return_nid)) {
411 fprintf(stderr, "usage: %s [all]\n", argv[0]);
415 sk = nl_socket_alloc();
419 /* Setup parser to receive Netlink packets */
420 rc = yaml_parser_initialize(&reply);
422 yaml_parser_log_error(&reply, stderr, NULL);
426 rc = yaml_parser_set_input_netlink(&reply, sk, false);
428 yaml_parser_log_error(&reply, stderr, NULL);
429 yaml_parser_delete(&reply);
433 /* Create Netlink emitter to send request to kernel */
434 rc = yaml_emitter_initialize(&request);
436 yaml_parser_log_error(&reply, stderr, NULL);
437 yaml_parser_delete(&reply);
441 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME, 1,
442 LNET_CMD_NETS, NLM_F_DUMP);
444 yaml_emitter_log_error(&request, stderr);
445 yaml_emitter_delete(&request);
446 yaml_parser_delete(&reply);
450 yaml_emitter_open(&request);
451 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
452 rc = yaml_emitter_emit(&request, &event);
456 yaml_mapping_start_event_initialize(&event, NULL,
457 (yaml_char_t *)YAML_MAP_TAG,
458 1, YAML_ANY_MAPPING_STYLE);
459 rc = yaml_emitter_emit(&request, &event);
463 yaml_scalar_event_initialize(&event, NULL,
464 (yaml_char_t *)YAML_STR_TAG,
465 (yaml_char_t *)"net",
467 YAML_PLAIN_SCALAR_STYLE);
468 rc = yaml_emitter_emit(&request, &event);
473 if (!g_net_set || g_net == LNET_NET_ANY) {
474 yaml_scalar_event_initialize(&event, NULL,
475 (yaml_char_t *)YAML_STR_TAG,
478 YAML_PLAIN_SCALAR_STYLE);
479 rc = yaml_emitter_emit(&request, &event);
483 char *net_id = libcfs_net2str(g_net);
485 yaml_sequence_start_event_initialize(&event, NULL,
486 (yaml_char_t *)YAML_SEQ_TAG,
487 1, YAML_ANY_SEQUENCE_STYLE);
488 rc = yaml_emitter_emit(&request, &event);
492 yaml_mapping_start_event_initialize(&event, NULL,
493 (yaml_char_t *)YAML_MAP_TAG,
494 1, YAML_ANY_MAPPING_STYLE);
495 rc = yaml_emitter_emit(&request, &event);
499 yaml_scalar_event_initialize(&event, NULL,
500 (yaml_char_t *)YAML_STR_TAG,
501 (yaml_char_t *)"net type",
503 1, 0, YAML_PLAIN_SCALAR_STYLE);
504 rc = yaml_emitter_emit(&request, &event);
508 yaml_scalar_event_initialize(&event, NULL,
509 (yaml_char_t *)YAML_STR_TAG,
510 (yaml_char_t *)net_id,
511 strlen(net_id), 1, 0,
512 YAML_PLAIN_SCALAR_STYLE);
513 rc = yaml_emitter_emit(&request, &event);
517 yaml_mapping_end_event_initialize(&event);
518 rc = yaml_emitter_emit(&request, &event);
522 yaml_sequence_end_event_initialize(&event);
523 rc = yaml_emitter_emit(&request, &event);
527 yaml_mapping_end_event_initialize(&event);
528 rc = yaml_emitter_emit(&request, &event);
532 yaml_document_end_event_initialize(&event, 0);
533 rc = yaml_emitter_emit(&request, &event);
537 rc = yaml_emitter_close(&request);
540 yaml_emitter_log_error(&request, stderr);
543 yaml_emitter_delete(&request);
546 rc = yaml_parser_parse(&reply, &event);
550 if (event.type == YAML_SCALAR_EVENT &&
551 strcmp((char *)event.data.scalar.value, "nid") == 0) {
554 yaml_event_delete(&event);
555 rc = yaml_parser_parse(&reply, &event);
557 yaml_event_delete(&event);
561 tmp = (char *)event.data.scalar.value;
562 if (all || strcmp(tmp, "0@lo") != 0) {
565 *(__u64 *)(argv[1]) = libcfs_str2nid(tmp);
570 done = (event.type == YAML_STREAM_END_EVENT);
571 yaml_event_delete(&event);
575 yaml_parser_log_error(&reply, stderr, NULL);
576 yaml_parser_delete(&reply);
578 #ifdef IOC_LIBCFS_GET_NI
586 for (count = 0;; count++) {
587 struct libcfs_ioctl_data data;
589 LIBCFS_IOC_INIT(data);
590 data.ioc_count = count;
591 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
594 if ((count > 0) && (errno == ENOENT))
595 /* We found them all */
597 fprintf(stderr, "IOC_LIBCFS_GET_NI error %d: %s\n",
598 errno, strerror(errno));
602 if (all || (data.ioc_nid != LNET_NID_LO_0)) {
603 printf("%s\n", libcfs_nid2str(data.ioc_nid));
605 *(__u64 *)(argv[1]) = data.ioc_nid;
619 jt_ptl_which_nid(int argc, char **argv)
621 struct lnet_nid best_nid = LNET_ANY_NID;
622 yaml_emitter_t request;
636 fprintf(stderr, "usage: %s NID [NID...]\n", argv[0]);
640 /* Create Netlink emitter to send request to kernel */
641 sk = nl_socket_alloc();
645 /* Setup parser to recieve Netlink packets */
646 rc = yaml_parser_initialize(&reply);
650 rc = yaml_parser_set_input_netlink(&reply, sk, false);
654 /* Create Netlink emitter to send request to kernel */
655 rc = yaml_emitter_initialize(&request);
659 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
661 LNET_CMD_PEER_DIST, NLM_F_DUMP);
665 yaml_emitter_open(&request);
666 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
667 rc = yaml_emitter_emit(&request, &event);
671 yaml_mapping_start_event_initialize(&event, NULL,
672 (yaml_char_t *)YAML_MAP_TAG,
673 1, YAML_ANY_MAPPING_STYLE);
674 rc = yaml_emitter_emit(&request, &event);
678 yaml_scalar_event_initialize(&event, NULL,
679 (yaml_char_t *)YAML_STR_TAG,
680 (yaml_char_t *)"peer",
681 strlen("peer"), 1, 0,
682 YAML_PLAIN_SCALAR_STYLE);
683 rc = yaml_emitter_emit(&request, &event);
687 yaml_sequence_start_event_initialize(&event, NULL,
688 (yaml_char_t *)YAML_SEQ_TAG,
689 1, YAML_BLOCK_SEQUENCE_STYLE);
690 rc = yaml_emitter_emit(&request, &event);
694 for (i = 1; i < argc; i++) {
698 if (strcmp(nidstr, "*") == 0)
701 rc = libcfs_strnid(&nid, nidstr);
702 if (rc < 0 || nid_same(&nid, &LNET_ANY_NID)) {
703 fprintf(stderr, "Can't parse NID %s\n", nidstr);
707 yaml_scalar_event_initialize(&event, NULL,
708 (yaml_char_t *)YAML_STR_TAG,
709 (yaml_char_t *)nidstr,
710 strlen(nidstr), 1, 0,
711 YAML_PLAIN_SCALAR_STYLE);
712 rc = yaml_emitter_emit(&request, &event);
717 yaml_sequence_end_event_initialize(&event);
718 rc = yaml_emitter_emit(&request, &event);
722 yaml_mapping_end_event_initialize(&event);
723 rc = yaml_emitter_emit(&request, &event);
727 yaml_document_end_event_initialize(&event, 0);
728 rc = yaml_emitter_emit(&request, &event);
732 rc = yaml_emitter_close(&request);
735 yaml_emitter_log_error(&request, stderr);
738 yaml_emitter_delete(&request);
741 rc = yaml_parser_parse(&reply, &event);
745 if (event.type != YAML_SCALAR_EVENT)
749 if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
750 yaml_event_delete(&event);
751 rc = yaml_parser_parse(&reply, &event);
753 yaml_event_delete(&event);
757 nidstr = (char *)event.data.scalar.value;
759 if (nid_same(&best_nid, &LNET_ANY_NID) ||
761 (dist == best_dist && order < best_order)) {
764 libcfs_strnid(&best_nid, nidstr);
766 } else if (strcmp((char *)event.data.scalar.value,
768 yaml_event_delete(&event);
769 rc = yaml_parser_parse(&reply, &event);
771 yaml_event_delete(&event);
775 dist = strtol((char *)event.data.scalar.value, NULL, 10);
776 } else if (strcmp((char *)event.data.scalar.value,
778 yaml_event_delete(&event);
779 rc = yaml_parser_parse(&reply, &event);
781 yaml_event_delete(&event);
785 order = strtol((char *)event.data.scalar.value, NULL, 10);
788 done = (event.type == YAML_STREAM_END_EVENT);
789 yaml_event_delete(&event);
794 /* yaml_* functions return 0 for error */
795 const char *msg = yaml_parser_get_reader_error(&reply);
797 fprintf(stderr, "Unexpected distance: %s\n", msg);
799 } else if (rc == 1) {
800 /* yaml_* functions return 1 for success */
804 yaml_parser_delete(&reply);
809 for (i = 1; i < argc; i++) {
810 struct libcfs_ioctl_data data;
814 nid4 = libcfs_str2nid(nidstr);
815 if (nid4 == LNET_NID_ANY) {
816 fprintf(stderr, "Can't parse NID %s\n", nidstr);
820 LIBCFS_IOC_INIT(data);
823 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_DIST, &data);
825 fprintf(stderr, "Can't get distance to %s: %s\n",
826 nidstr, strerror(errno));
830 dist = data.ioc_u32[0];
831 order = data.ioc_u32[1];
834 if (dist == -EHOSTUNREACH)
837 fprintf(stderr, "Unexpected distance to %s: %d\n",
842 if (nid_same(&best_nid, &LNET_ANY_NID) ||
844 (dist == best_dist && order < best_order)) {
847 lnet_nid4_to_nid(nid4, &best_nid);
851 if (nid_same(&best_nid, &LNET_ANY_NID)) {
852 fprintf(stderr, "No reachable NID\n");
856 printf("%s\n", libcfs_nidstr(&best_nid));
861 jt_ptl_print_interfaces(int argc, char **argv)
863 struct libcfs_ioctl_data data;
864 char buffer[3][HOST_NAME_MAX + 1];
868 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
871 for (index = 0; ; index++) {
872 LIBCFS_IOC_INIT(data);
873 data.ioc_net = g_net;
874 data.ioc_count = index;
876 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_INTERFACE, &data);
880 printf("%s: (%s/%s) npeer %d nroute %d\n",
881 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[2],
882 sizeof(buffer[2]), 1),
883 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[0],
884 sizeof(buffer[0]), 0),
885 ptl_ipaddr_2_str(data.ioc_u32[1], buffer[1],
886 sizeof(buffer[1]), 0),
887 data.ioc_u32[2], data.ioc_u32[3]);
891 if (errno == ENOENT) {
892 printf("<no interfaces>\n");
895 "Error getting interfaces: %s: check dmesg.\n",
904 jt_ptl_add_interface(int argc, char **argv)
906 struct libcfs_ioctl_data data;
909 __u32 netmask = 0xffffff00;
914 if (argc < 2 || argc > 3) {
915 fprintf(stderr, "usage: %s ipaddr [netmask]\n", argv[0]);
919 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
922 if (lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
923 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
928 count = strtol(argv[2], &end, 0);
929 if (count > 0 && count < 32 && *end == 0) {
931 for (i = count; i > 0; i--)
932 netmask = netmask | (1 << (32 - i));
933 } else if (lnet_parse_ipquad(&netmask, argv[2]) != 0) {
934 fprintf(stderr, "Can't parse netmask: %s\n", argv[2]);
939 LIBCFS_IOC_INIT(data);
940 data.ioc_net = g_net;
941 data.ioc_u32[0] = ipaddr;
942 data.ioc_u32[1] = netmask;
944 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_INTERFACE, &data);
946 fprintf(stderr, "failed to add interface: %s\n",
955 jt_ptl_del_interface(int argc, char **argv)
957 struct libcfs_ioctl_data data;
962 fprintf(stderr, "usage: %s [ipaddr]\n", argv[0]);
966 if (!g_net_is_compatible(argv[0], SOCKLND, 0))
970 lnet_parse_ipaddr(&ipaddr, argv[1]) != 0) {
971 fprintf(stderr, "Can't parse ip: %s\n", argv[1]);
975 LIBCFS_IOC_INIT(data);
976 data.ioc_net = g_net;
977 data.ioc_u32[0] = ipaddr;
979 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_INTERFACE, &data);
981 fprintf(stderr, "failed to delete interface: %s\n",
990 jt_ptl_print_peers(int argc, char **argv)
992 struct libcfs_ioctl_data data;
993 struct lnet_process_id id;
994 char buffer[2][HOST_NAME_MAX + 1];
998 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
1002 for (index = 0; ; index++) {
1003 LIBCFS_IOC_INIT(data);
1004 data.ioc_net = g_net;
1005 data.ioc_count = index;
1007 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER, &data);
1011 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
1012 id.nid = data.ioc_nid;
1013 id.pid = data.ioc_u32[4];
1014 printf("%-20s [%d]%s->%s:%d #%d\n",
1016 data.ioc_count, /* persistence */
1018 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
1019 sizeof(buffer[0]), 1),
1021 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
1022 sizeof(buffer[1]), 1),
1023 data.ioc_u32[1], /* peer port */
1024 data.ioc_u32[3]); /* conn_count */
1025 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
1026 int disconn = data.ioc_flags >> 16;
1032 state = data.ioc_flags & 0xffff ? "C" : "U";
1034 printf("%-20s (%d) %s [%d] %ju sq %d/%d tx %d/%d/%d\n",
1035 libcfs_nid2str(data.ioc_nid), /* peer nid */
1036 data.ioc_net, /* gemini device id */
1037 state, /* peer is Connecting, Up, or Down */
1038 data.ioc_count, /* peer refcount */
1039 (uintmax_t)data.ioc_u64[0], /* peerstamp */
1040 data.ioc_u32[2], data.ioc_u32[3], /* tx and rx seq */
1041 /* fmaq, nfma, nrdma */
1042 data.ioc_u32[0], data.ioc_u32[1],
1045 printf("%-20s [%d]\n",
1046 libcfs_nid2str(data.ioc_nid), data.ioc_count);
1051 if (errno == ENOENT) {
1052 printf("<no peers>\n");
1055 "Error getting peer list: %s: check dmesg.\n",
1062 int jt_ptl_add_peer(int argc, char **argv)
1064 struct libcfs_ioctl_data data;
1070 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
1074 fprintf(stderr, "usage(tcp,gni): %s nid ipaddr port\n",
1079 nid = libcfs_str2nid(argv[1]);
1080 if (nid == LNET_NID_ANY) {
1081 fprintf(stderr, "Can't parse NID: %s\n", argv[1]);
1085 if (lnet_parse_ipaddr(&ip, argv[2]) != 0) {
1086 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
1090 if (lnet_parse_port(&port, argv[3]) != 0) {
1091 fprintf(stderr, "Can't parse port: %s\n", argv[3]);
1095 LIBCFS_IOC_INIT(data);
1096 data.ioc_net = g_net;
1098 data.ioc_u32[0] = ip;
1099 data.ioc_u32[1] = port;
1101 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_PEER, &data);
1103 fprintf(stderr, "failed to add peer: %s\n",
1112 jt_ptl_del_peer(int argc, char **argv)
1114 struct libcfs_ioctl_data data;
1115 lnet_nid_t nid = LNET_NID_ANY;
1116 lnet_pid_t pid = LNET_PID_ANY;
1120 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND,
1124 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
1126 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1129 } else if (argc > 2) {
1130 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
1134 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
1135 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1139 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
1140 if (argc > 2 && lnet_parse_ipaddr(&ip, argv[2]) != 0) {
1141 fprintf(stderr, "Can't parse ip addr: %s\n", argv[2]);
1146 LIBCFS_IOC_INIT(data);
1147 data.ioc_net = g_net;
1149 data.ioc_u32[0] = ip;
1150 data.ioc_u32[1] = pid;
1152 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_PEER, &data);
1154 fprintf(stderr, "failed to remove peer: %s\n", strerror(errno));
1162 jt_ptl_print_connections(int argc, char **argv)
1164 struct libcfs_ioctl_data data;
1165 struct lnet_process_id id;
1166 char buffer[2][HOST_NAME_MAX + 1];
1170 if (!g_net_is_compatible(argv[0], SOCKLND, O2IBLND, GNILND, 0))
1173 for (index = 0; ; index++) {
1174 LIBCFS_IOC_INIT(data);
1175 data.ioc_net = g_net;
1176 data.ioc_count = index;
1178 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_CONN, &data);
1182 if (g_net_is_compatible(NULL, SOCKLND, 0)) {
1183 id.nid = data.ioc_nid;
1184 id.pid = data.ioc_u32[6];
1185 printf("%-20s %s[%d]%s->%s:%d %d/%d %s\n",
1187 (data.ioc_u32[3] == SOCKLND_CONN_ANY) ? "A" :
1188 (data.ioc_u32[3] == SOCKLND_CONN_CONTROL) ? "C" :
1189 (data.ioc_u32[3] == SOCKLND_CONN_BULK_IN) ? "I" :
1190 (data.ioc_u32[3] == SOCKLND_CONN_BULK_OUT) ? "O" : "?",
1191 data.ioc_u32[4], /* scheduler */
1193 ptl_ipaddr_2_str(data.ioc_u32[2], buffer[0],
1194 sizeof(buffer[0]), 1),
1195 /* remote IP addr */
1196 ptl_ipaddr_2_str(data.ioc_u32[0], buffer[1],
1197 sizeof(buffer[1]), 1),
1198 data.ioc_u32[1], /* remote port */
1199 data.ioc_count, /* tx buffer size */
1200 data.ioc_u32[5], /* rx buffer size */
1201 data.ioc_flags ? "nagle" : "nonagle");
1202 } else if (g_net_is_compatible(NULL, O2IBLND, 0)) {
1203 printf("%s mtu %d\n",
1204 libcfs_nid2str(data.ioc_nid),
1205 data.ioc_u32[0]); /* path MTU */
1206 } else if (g_net_is_compatible(NULL, GNILND, 0)) {
1207 printf("%-20s [%d]\n",
1208 libcfs_nid2str(data.ioc_nid),
1209 data.ioc_u32[0] /* device id */);
1211 printf("%s\n", libcfs_nid2str(data.ioc_nid));
1216 if (errno == ENOENT) {
1217 printf("<no connections>\n");
1220 "Error getting connection list: %s: check dmesg.\n",
1227 int jt_ptl_disconnect(int argc, char **argv)
1229 struct libcfs_ioctl_data data;
1230 lnet_nid_t nid = LNET_NID_ANY;
1235 fprintf(stderr, "usage: %s [nid] [ipaddr]\n", argv[0]);
1239 if (!g_net_is_compatible(NULL, SOCKLND, O2IBLND, GNILND, 0))
1242 if (argc >= 2 && !libcfs_str2anynid(&nid, argv[1])) {
1243 fprintf(stderr, "Can't parse nid %s\n", argv[1]);
1247 if (g_net_is_compatible(NULL, SOCKLND, 0) && argc >= 3 &&
1248 lnet_parse_ipaddr(&ipaddr, argv[2]) != 0) {
1249 fprintf(stderr, "Can't parse ip addr %s\n", argv[2]);
1253 LIBCFS_IOC_INIT(data);
1254 data.ioc_net = g_net;
1256 data.ioc_u32[0] = ipaddr;
1258 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CLOSE_CONNECTION, &data);
1260 fprintf(stderr, "failed to remove connection: %s\n",
1268 int jt_ptl_push_connection(int argc, char **argv)
1270 struct libcfs_ioctl_data data;
1272 lnet_nid_t nid = LNET_NID_ANY;
1275 fprintf(stderr, "usage: %s [nid]\n", argv[0]);
1279 if (!g_net_is_compatible(argv[0], SOCKLND, GNILND, 0))
1282 if (argc > 1 && !libcfs_str2anynid(&nid, argv[1])) {
1283 fprintf(stderr, "Can't parse nid: %s\n", argv[1]);
1287 LIBCFS_IOC_INIT(data);
1288 data.ioc_net = g_net;
1291 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PUSH_CONNECTION, &data);
1293 fprintf(stderr, "failed to push connection: %s\n",
1301 #ifndef IOC_LIBCFS_PING_PEER
1302 #define IOC_LIBCFS_PING_PEER _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
1305 int jt_ptl_ping(int argc, char **argv)
1307 bool done = false, print = true;
1310 struct lnet_processid id;
1311 yaml_emitter_t request;
1312 yaml_parser_t reply;
1318 fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
1322 sep = strchr(argv[1], '-');
1324 rc = lnet_parse_nid(argv[1], &id);
1330 if (argv[1][0] == 'u' || argv[1][0] == 'U')
1331 id.pid = strtoul(&argv[1][1], &end, 0) |
1334 id.pid = strtoul(argv[1], &end, 0);
1336 if (end != sep) { /* assuming '-' is part of hostname */
1337 rc = lnet_parse_nid(argv[1], &id);
1341 if (libcfs_strnid(&id.nid, (sep + 1)) < 0 ||
1342 LNET_NID_IS_ANY(&id.nid)) {
1344 "Invalid PID argument \"%s\"\n",
1352 timeout = 1000 * atol(argv[2]);
1353 if (timeout > 120 * 1000) {
1354 fprintf(stderr, "Timeout %s is to large\n",
1359 timeout = 1000; /* default 1 second timeout */
1362 /* Create Netlink emitter to send request to kernel */
1363 sk = nl_socket_alloc();
1367 /* Setup parser to recieve Netlink packets */
1368 rc = yaml_parser_initialize(&reply);
1372 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1376 /* Create Netlink emitter to send request to kernel */
1377 rc = yaml_emitter_initialize(&request);
1381 rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
1382 LNET_GENL_VERSION, LNET_CMD_PING,
1387 yaml_emitter_open(&request);
1388 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1389 rc = yaml_emitter_emit(&request, &event);
1393 yaml_mapping_start_event_initialize(&event, NULL,
1394 (yaml_char_t *)YAML_MAP_TAG,
1395 1, YAML_ANY_MAPPING_STYLE);
1396 rc = yaml_emitter_emit(&request, &event);
1400 yaml_scalar_event_initialize(&event, NULL,
1401 (yaml_char_t *)YAML_STR_TAG,
1402 (yaml_char_t *)"ping",
1403 strlen("ping"), 1, 0,
1404 YAML_PLAIN_SCALAR_STYLE);
1405 rc = yaml_emitter_emit(&request, &event);
1409 yaml_mapping_start_event_initialize(&event, NULL,
1410 (yaml_char_t *)YAML_MAP_TAG,
1411 1, YAML_ANY_MAPPING_STYLE);
1412 rc = yaml_emitter_emit(&request, &event);
1416 if (timeout != 1000) {
1417 yaml_scalar_event_initialize(&event, NULL,
1418 (yaml_char_t *)YAML_STR_TAG,
1419 (yaml_char_t *)"timeout",
1420 strlen("timeout"), 1, 0,
1421 YAML_PLAIN_SCALAR_STYLE);
1422 rc = yaml_emitter_emit(&request, &event);
1426 yaml_scalar_event_initialize(&event, NULL,
1427 (yaml_char_t *)YAML_INT_TAG,
1428 (yaml_char_t *)argv[2],
1429 strlen(argv[2]), 1, 0,
1430 YAML_PLAIN_SCALAR_STYLE);
1431 rc = yaml_emitter_emit(&request, &event);
1436 yaml_scalar_event_initialize(&event, NULL,
1437 (yaml_char_t *)YAML_STR_TAG,
1438 (yaml_char_t *)"nids",
1439 strlen("nids"), 1, 0,
1440 YAML_PLAIN_SCALAR_STYLE);
1441 rc = yaml_emitter_emit(&request, &event);
1445 yaml_sequence_start_event_initialize(&event, NULL,
1446 (yaml_char_t *)YAML_SEQ_TAG,
1447 1, YAML_FLOW_SEQUENCE_STYLE);
1448 rc = yaml_emitter_emit(&request, &event);
1452 /* convert NID to string, in case libcfs_str2nid() did name lookup */
1453 yaml_scalar_event_initialize(&event, NULL,
1454 (yaml_char_t *)YAML_STR_TAG,
1455 (yaml_char_t *)libcfs_nidstr(&id.nid),
1456 strlen(libcfs_nidstr(&id.nid)), 1, 0,
1457 YAML_PLAIN_SCALAR_STYLE);
1458 rc = yaml_emitter_emit(&request, &event);
1462 yaml_sequence_end_event_initialize(&event);
1463 rc = yaml_emitter_emit(&request, &event);
1467 yaml_mapping_end_event_initialize(&event);
1468 rc = yaml_emitter_emit(&request, &event);
1472 yaml_mapping_end_event_initialize(&event);
1473 rc = yaml_emitter_emit(&request, &event);
1477 yaml_document_end_event_initialize(&event, 0);
1478 rc = yaml_emitter_emit(&request, &event);
1482 rc = yaml_emitter_close(&request);
1485 yaml_emitter_log_error(&request, stderr);
1489 yaml_emitter_delete(&request);
1491 /* Now parse the reply results */
1493 rc = yaml_parser_parse(&reply, &event);
1497 if (event.type != YAML_SCALAR_EVENT)
1500 if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
1501 yaml_event_delete(&event);
1502 rc = yaml_parser_parse(&reply, &event);
1504 yaml_event_delete(&event);
1508 /* Print 0@lo. Its not sent */
1509 printf("12345-0@lo\n");
1512 printf("%s\n", (char *)event.data.scalar.value);
1513 } else if (strcmp((char *)event.data.scalar.value,
1515 yaml_event_delete(&event);
1516 rc = yaml_parser_parse(&reply, &event);
1518 yaml_event_delete(&event);
1521 rc = strtol((char *)event.data.scalar.value, NULL, 10);
1522 fprintf(stdout, "failed to ping %s: %s\n",
1523 argv[1], strerror(-rc));
1524 break; /* "rc" is clobbered if loop is run again */
1527 done = (event.type == YAML_STREAM_END_EVENT);
1528 yaml_event_delete(&event);
1532 /* yaml_* functions return 0 for error */
1533 const char *msg = yaml_parser_get_reader_error(&reply);
1535 rc = errno ? -errno : -EHOSTUNREACH;
1536 if (strcmp(msg, "Unspecific failure") != 0) {
1537 fprintf(stdout, "failed to ping %s: %s\n",
1540 fprintf(stdout, "failed to ping %s: %s\n",
1541 argv[1], strerror(errno));
1543 } else if (rc == 1) {
1544 /* yaml_* functions return 1 for success */
1547 yaml_parser_delete(&reply);
1551 #ifdef IOC_LIBCFS_PING_PEER
1553 struct lnet_process_id ids[LNET_INTERFACES_MAX_DEFAULT];
1554 int maxids = sizeof(ids) / sizeof(ids[0]);
1555 struct lnet_ioctl_ping_data ping = { { 0 } };
1561 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
1562 ping.ping_hdr.ioc_len = sizeof(ping);
1563 ping.ping_id = lnet_pid_to_pid4(&id);
1564 ping.ping_src = LNET_NID_ANY;
1565 ping.op_param = timeout;
1566 ping.ping_count = maxids;
1567 ping.ping_buf = ids;
1569 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING_PEER, &ping);
1571 fprintf(stderr, "failed to ping %s: %s\n", argv[1],
1576 for (i = 0; i < ping.ping_count && i < maxids; i++)
1577 printf("%s\n", libcfs_id2str(ids[i]));
1579 if (ping.ping_count > maxids)
1580 printf("%d out of %d ids listed\n", maxids, ping.ping_count);
1588 int jt_ptl_mynid(int argc, char **argv)
1590 struct libcfs_ioctl_data data;
1595 fprintf(stderr, "usage: %s NID\n", argv[0]);
1599 nid = libcfs_str2nid(argv[1]);
1600 if (nid == LNET_NID_ANY) {
1601 fprintf(stderr, "Can't parse NID '%s'\n", argv[1]);
1605 LIBCFS_IOC_INIT(data);
1606 data.ioc_net = LNET_NIDNET(nid);
1609 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_REGISTER_MYNID, &data);
1611 fprintf(stderr, "setting my NID failed: %s\n",
1614 printf("registered my nid %s\n", libcfs_nid2str(nid));
1620 jt_ptl_fail_nid(int argc, char **argv)
1625 struct libcfs_ioctl_data data;
1627 if (argc < 2 || argc > 3) {
1628 fprintf(stderr, "usage: %s nid|\"*\" [count (0 == mend)]\n",
1633 if (!libcfs_str2anynid(&nid, argv[1])) {
1634 fprintf(stderr, "Can't parse nid \"%s\"\n", argv[1]);
1639 threshold = LNET_MD_THRESH_INF;
1640 } else if (sscanf(argv[2], "%i", &threshold) != 1) {
1641 fprintf(stderr, "Can't parse count \"%s\"\n", argv[2]);
1645 LIBCFS_IOC_INIT(data);
1647 data.ioc_count = threshold;
1649 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_FAIL_NID, &data);
1651 fprintf(stderr, "IOC_LIBCFS_FAIL_NID failed: %s\n",
1655 threshold == 0 ? "Unfailing" : "Failing", argv[1]);
1660 static int ptl_yaml_route_display(yaml_parser_t *reply)
1662 char gw[LNET_MAX_STR_LEN], net[18];
1663 bool done = false, alive = false;
1664 int hops = -1, prio = -1;
1668 /* Now parse the reply results */
1672 rc = yaml_parser_parse(reply, &event);
1676 if (event.type == YAML_SEQUENCE_END_EVENT) {
1677 printf("net %18s hops %d gw %32.128s %s pri %u\n",
1678 net, hops, gw, alive ? "up" : "down",
1680 memset(net, '\0', sizeof(net));
1681 memset(gw, '\0', sizeof(gw));
1686 if (event.type != YAML_SCALAR_EVENT)
1689 value = (char *)event.data.scalar.value;
1690 if (strcmp(value, "net") == 0) {
1691 yaml_event_delete(&event);
1692 rc = yaml_parser_parse(reply, &event);
1694 yaml_event_delete(&event);
1698 value = (char *)event.data.scalar.value;
1699 strncpy(net, value, sizeof(net) - 1);
1700 } else if (strcmp(value, "gateway") == 0) {
1701 yaml_event_delete(&event);
1702 rc = yaml_parser_parse(reply, &event);
1704 yaml_event_delete(&event);
1708 value = (char *)event.data.scalar.value;
1709 strncpy(gw, value, sizeof(gw) - 1);
1710 } else if (strcmp(value, "state") == 0) {
1711 yaml_event_delete(&event);
1712 rc = yaml_parser_parse(reply, &event);
1714 yaml_event_delete(&event);
1718 value = (char *)event.data.scalar.value;
1719 if (strcmp(value, "up") == 0) {
1721 } else if (strcmp(value, "down") == 0) {
1724 } else if (strcmp(value, "hop") == 0) {
1725 yaml_event_delete(&event);
1726 rc = yaml_parser_parse(reply, &event);
1728 yaml_event_delete(&event);
1732 value = (char *)event.data.scalar.value;
1733 hops = strtol(value, NULL, 10);
1734 } else if (strcmp(value, "priority") == 0) {
1735 yaml_event_delete(&event);
1736 rc = yaml_parser_parse(reply, &event);
1738 yaml_event_delete(&event);
1742 value = (char *)event.data.scalar.value;
1743 prio = strtol(value, NULL, 10);
1746 done = (event.type == YAML_STREAM_END_EVENT);
1747 yaml_event_delete(&event);
1754 static int ptl_yaml_route(char *nw, char *gws, int hops, int prio, bool enable,
1755 time_t notify_time, int flags, int version)
1757 struct nl_sock *sk = NULL;
1758 const char *msg = NULL;
1759 yaml_emitter_t output;
1760 yaml_parser_t reply;
1764 sk = nl_socket_alloc();
1768 /* Setup parser to receive Netlink packets */
1769 rc = yaml_parser_initialize(&reply);
1775 rc = yaml_parser_set_input_netlink(&reply, sk, false);
1777 msg = yaml_parser_get_reader_error(&reply);
1781 /* Create Netlink emitter to send request to kernel */
1782 rc = yaml_emitter_initialize(&output);
1784 msg = "failed to initialize emitter";
1788 rc = yaml_emitter_set_output_netlink(&output, sk, LNET_GENL_NAME,
1789 version, LNET_CMD_ROUTES, flags);
1793 yaml_emitter_open(&output);
1794 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
1795 rc = yaml_emitter_emit(&output, &event);
1799 yaml_mapping_start_event_initialize(&event, NULL,
1800 (yaml_char_t *)YAML_MAP_TAG,
1801 1, YAML_ANY_MAPPING_STYLE);
1802 rc = yaml_emitter_emit(&output, &event);
1806 yaml_scalar_event_initialize(&event, NULL,
1807 (yaml_char_t *)YAML_STR_TAG,
1808 (yaml_char_t *)"route",
1809 strlen("route"), 1, 0,
1810 YAML_PLAIN_SCALAR_STYLE);
1811 rc = yaml_emitter_emit(&output, &event);
1816 yaml_sequence_start_event_initialize(&event, NULL,
1817 (yaml_char_t *)YAML_SEQ_TAG,
1819 YAML_BLOCK_SEQUENCE_STYLE);
1820 rc = yaml_emitter_emit(&output, &event);
1824 yaml_mapping_start_event_initialize(&event, NULL,
1825 (yaml_char_t *)YAML_MAP_TAG, 1,
1826 YAML_BLOCK_MAPPING_STYLE);
1827 rc = yaml_emitter_emit(&output, &event);
1832 yaml_scalar_event_initialize(&event, NULL,
1833 (yaml_char_t *)YAML_STR_TAG,
1834 (yaml_char_t *)"net",
1835 strlen("net"), 1, 0,
1836 YAML_PLAIN_SCALAR_STYLE);
1837 rc = yaml_emitter_emit(&output, &event);
1841 yaml_scalar_event_initialize(&event, NULL,
1842 (yaml_char_t *)YAML_STR_TAG,
1845 YAML_PLAIN_SCALAR_STYLE);
1846 rc = yaml_emitter_emit(&output, &event);
1852 yaml_scalar_event_initialize(&event, NULL,
1853 (yaml_char_t *)YAML_STR_TAG,
1854 (yaml_char_t *)"gateway",
1855 strlen("gateway"), 1, 0,
1856 YAML_PLAIN_SCALAR_STYLE);
1857 rc = yaml_emitter_emit(&output, &event);
1861 yaml_scalar_event_initialize(&event, NULL,
1862 (yaml_char_t *)YAML_STR_TAG,
1865 YAML_PLAIN_SCALAR_STYLE);
1866 rc = yaml_emitter_emit(&output, &event);
1872 char when[INT_STRING_LEN];
1874 yaml_scalar_event_initialize(&event, NULL,
1875 (yaml_char_t *)YAML_STR_TAG,
1876 (yaml_char_t *)"notify_time",
1877 strlen("notify_time"), 1, 0,
1878 YAML_PLAIN_SCALAR_STYLE);
1879 rc = yaml_emitter_emit(&output, &event);
1883 snprintf(when, sizeof(when), "%ld", notify_time);
1884 yaml_scalar_event_initialize(&event, NULL,
1885 (yaml_char_t *)YAML_INT_TAG,
1886 (yaml_char_t *)when,
1888 YAML_PLAIN_SCALAR_STYLE);
1889 rc = yaml_emitter_emit(&output, &event);
1894 yaml_scalar_event_initialize(&event, NULL,
1895 (yaml_char_t *)YAML_STR_TAG,
1896 (yaml_char_t *)"state",
1897 strlen("state"), 1, 0,
1898 YAML_PLAIN_SCALAR_STYLE);
1899 rc = yaml_emitter_emit(&output, &event);
1904 yaml_scalar_event_initialize(&event, NULL,
1905 (yaml_char_t *)YAML_STR_TAG,
1906 (yaml_char_t *)"up",
1908 YAML_PLAIN_SCALAR_STYLE);
1910 yaml_scalar_event_initialize(&event, NULL,
1911 (yaml_char_t *)YAML_STR_TAG,
1912 (yaml_char_t *)"down",
1913 strlen("down"), 1, 0,
1914 YAML_PLAIN_SCALAR_STYLE);
1916 rc = yaml_emitter_emit(&output, &event);
1920 yaml_mapping_end_event_initialize(&event);
1921 rc = yaml_emitter_emit(&output, &event);
1925 yaml_sequence_end_event_initialize(&event);
1926 rc = yaml_emitter_emit(&output, &event);
1930 yaml_scalar_event_initialize(&event, NULL,
1931 (yaml_char_t *)YAML_STR_TAG,
1934 YAML_PLAIN_SCALAR_STYLE);
1935 rc = yaml_emitter_emit(&output, &event);
1940 yaml_mapping_end_event_initialize(&event);
1941 rc = yaml_emitter_emit(&output, &event);
1945 yaml_document_end_event_initialize(&event, 0);
1946 rc = yaml_emitter_emit(&output, &event);
1950 rc = yaml_emitter_close(&output);
1953 yaml_emitter_log_error(&output, stderr);
1956 if (flags != NLM_F_DUMP) {
1957 yaml_document_t errmsg;
1959 rc = yaml_parser_load(&reply, &errmsg);
1961 yaml_emitter_t debug;
1963 rc = yaml_emitter_initialize(&debug);
1965 yaml_emitter_set_indent(&debug,
1966 LNET_DEFAULT_INDENT);
1967 yaml_emitter_set_output_file(&debug,
1969 rc = yaml_emitter_dump(&debug,
1971 } else if (rc == 0) {
1972 yaml_emitter_log_error(&debug, stderr);
1975 yaml_emitter_delete(&debug);
1977 yaml_document_delete(&errmsg);
1979 rc = ptl_yaml_route_display(&reply);
1982 msg = yaml_parser_get_reader_error(&reply);
1984 yaml_emitter_delete(&output);
1987 fprintf(stdout, "%s\n", msg);
1988 yaml_parser_delete(&reply);
1991 return rc == 1 ? 0 : rc;
1995 jt_ptl_add_route(int argc, char **argv)
1997 struct lnet_ioctl_config_data data;
1998 lnet_nid_t gateway_nid;
1999 __u32 hops = LNET_UNDEFINED_HOPS;
2000 unsigned int priority = 0;
2004 if (argc < 2 || argc > 4) {
2005 fprintf(stderr, "usage: %s gateway [hopcount [priority]]\n",
2010 if (g_net_is_set(argv[0]) == 0)
2013 gateway_nid = libcfs_str2nid(argv[1]);
2014 if (gateway_nid == LNET_NID_ANY) {
2015 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
2020 hops = strtol(argv[2], &end, 0);
2021 if (hops == 0 || hops >= 256 ||
2022 (end && *end != 0)) {
2023 fprintf(stderr, "Can't parse hopcount \"%s\"\n",
2028 priority = strtoul(argv[3], &end, 0);
2029 if (end && *end != 0) {
2031 "Can't parse priority \"%s\"\n",
2038 rc = ptl_yaml_route(libcfs_net2str(g_net), argv[1], hops,
2039 priority, false, 0, NLM_F_CREATE, LNET_GENL_VERSION);
2041 if (rc == -EOPNOTSUPP)
2046 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2047 data.cfg_net = g_net;
2048 data.cfg_config_u.cfg_route.rtr_hop = hops;
2049 data.cfg_nid = gateway_nid;
2050 data.cfg_config_u.cfg_route.rtr_priority = priority;
2052 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
2054 fprintf(stderr, "IOC_LIBCFS_ADD_ROUTE failed: %s\n",
2063 jt_ptl_del_route(int argc, char **argv)
2065 struct lnet_ioctl_config_data data;
2070 fprintf(stderr, "usage: %s gatewayNID\n", argv[0]);
2074 if (libcfs_str2anynid(&nid, argv[1]) == 0) {
2075 fprintf(stderr, "Can't parse gateway NID \"%s\"\n", argv[1]);
2079 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
2080 -1, -1, false, 0, 0, LNET_GENL_VERSION);
2082 if (rc == -EOPNOTSUPP)
2087 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2088 data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
2091 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
2093 fprintf(stderr, "IOC_LIBCFS_DEL_ROUTE (%s) failed: %s\n",
2094 libcfs_nid2str(nid), strerror(errno));
2102 jt_ptl_notify_router(int argc, char **argv)
2104 struct libcfs_ioctl_data data;
2112 fprintf(stderr, "usage: %s targetNID <up/down> [<time>]\n",
2117 nid = libcfs_str2nid(argv[1]);
2118 if (nid == LNET_NID_ANY) {
2119 fprintf(stderr, "Can't parse target NID \"%s\"\n", argv[1]);
2123 if (lnet_parse_bool (&enable, argv[2]) != 0) {
2124 fprintf(stderr, "Can't parse boolean %s\n", argv[2]);
2128 gettimeofday(&now, NULL);
2132 } else if (lnet_parse_time(&when, argv[3]) != 0) {
2134 "Can't parse time %s\n Please specify either 'YYYY-MM-DD-HH:MM:SS'\n or an absolute unix time in seconds\n",
2137 } else if (when > now.tv_sec) {
2138 fprintf(stderr, "%s specifies a time in the future\n",
2143 rc = ptl_yaml_route(g_net_set ? libcfs_net2str(g_net) : NULL, argv[1],
2144 -1, -1, enable, when, NLM_F_REPLACE, LNET_GENL_VERSION);
2146 if (rc == -EOPNOTSUPP)
2151 LIBCFS_IOC_INIT(data);
2153 data.ioc_flags = enable;
2154 /* Yeuch; 'cept I need a __u64 on 64 bit machines... */
2155 data.ioc_u64[0] = (__u64)when;
2157 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_NOTIFY_ROUTER, &data);
2159 fprintf(stderr, "IOC_LIBCFS_NOTIFY_ROUTER (%s) failed: %s\n",
2160 libcfs_nid2str(nid), strerror(errno));
2168 jt_ptl_print_routes(int argc, char **argv)
2170 struct lnet_ioctl_config_data data;
2179 rc = ptl_yaml_route(NULL, NULL, -1, -1, false, 0, NLM_F_DUMP,
2182 if (rc == -EOPNOTSUPP)
2187 for (index = 0; ; index++) {
2188 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2189 data.cfg_count = index;
2191 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
2196 hops = data.cfg_config_u.cfg_route.rtr_hop;
2198 alive = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
2199 pri = data.cfg_config_u.cfg_route.rtr_priority;
2201 printf("net %18s hops %d gw %32s %s pri %u\n",
2202 libcfs_net2str(net), hops,
2203 libcfs_nid2str(nid), alive ? "up" : "down", pri);
2206 if (errno != ENOENT)
2207 fprintf(stderr, "Error getting routes: %s: check dmesg.\n",
2214 fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
2220 /* NB: can't support range ipaddress except * and *@net */
2221 if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
2222 net = libcfs_str2net(str + 2);
2223 if (net == LNET_NET_ANY)
2226 nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
2228 rc = libcfs_str2anynid(&nid, str);
2236 fprintf(stderr, "Invalid NID : %s\n", str);
2241 fault_attr_msg_parse(char *msg_str, __u32 *mask_p)
2243 if (!strcasecmp(msg_str, "put")) {
2244 *mask_p |= LNET_PUT_BIT;
2247 } else if (!strcasecmp(msg_str, "ack")) {
2248 *mask_p |= LNET_ACK_BIT;
2251 } else if (!strcasecmp(msg_str, "get")) {
2252 *mask_p |= LNET_GET_BIT;
2255 } else if (!strcasecmp(msg_str, "reply")) {
2256 *mask_p |= LNET_REPLY_BIT;
2260 fprintf(stderr, "unknown message type %s\n", msg_str);
2265 fault_attr_ptl_parse(char *ptl_str, __u64 *mask_p)
2267 unsigned long rc = strtoul(optarg, NULL, 0);
2270 fprintf(stderr, "invalid portal: %lu\n", rc);
2274 *mask_p |= (1ULL << rc);
2279 fault_attr_health_error_parse(char *error, __u32 *mask)
2281 if (!strcasecmp(error, "local_interrupt")) {
2282 *mask |= HSTATUS_LOCAL_INTERRUPT_BIT;
2285 if (!strcasecmp(error, "local_dropped")) {
2286 *mask |= HSTATUS_LOCAL_DROPPED_BIT;
2289 if (!strcasecmp(error, "local_aborted")) {
2290 *mask |= HSTATUS_LOCAL_ABORTED_BIT;
2293 if (!strcasecmp(error, "local_no_route")) {
2294 *mask |= HSTATUS_LOCAL_NO_ROUTE_BIT;
2297 if (!strcasecmp(error, "local_error")) {
2298 *mask |= HSTATUS_LOCAL_ERROR_BIT;
2301 if (!strcasecmp(error, "local_timeout")) {
2302 *mask |= HSTATUS_LOCAL_TIMEOUT_BIT;
2305 if (!strcasecmp(error, "remote_error")) {
2306 *mask |= HSTATUS_REMOTE_ERROR_BIT;
2309 if (!strcasecmp(error, "remote_dropped")) {
2310 *mask |= HSTATUS_REMOTE_DROPPED_BIT;
2313 if (!strcasecmp(error, "remote_timeout")) {
2314 *mask |= HSTATUS_REMOTE_TIMEOUT_BIT;
2317 if (!strcasecmp(error, "network_timeout")) {
2318 *mask |= HSTATUS_NETWORK_TIMEOUT_BIT;
2321 if (!strcasecmp(error, "random")) {
2322 *mask = HSTATUS_RANDOM;
2330 fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
2332 struct libcfs_ioctl_data data = { { 0 } };
2333 struct lnet_fault_attr attr;
2337 static const struct option opts[] = {
2338 { .name = "source", .has_arg = required_argument, .val = 's' },
2339 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2340 { .name = "rate", .has_arg = required_argument, .val = 'r' },
2341 { .name = "interval", .has_arg = required_argument, .val = 'i' },
2342 { .name = "random", .has_arg = no_argument, .val = 'n' },
2343 { .name = "latency", .has_arg = required_argument, .val = 'l' },
2344 { .name = "portal", .has_arg = required_argument, .val = 'p' },
2345 { .name = "message", .has_arg = required_argument, .val = 'm' },
2346 { .name = "health_error", .has_arg = required_argument, .val = 'e' },
2347 { .name = "local_nid", .has_arg = required_argument, .val = 'o' },
2348 { .name = "drop_all", .has_arg = no_argument, .val = 'x' },
2353 "Failed, please provide source, destination and rate of rule\n");
2357 optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
2358 memset(&attr, 0, sizeof(attr));
2360 int c = getopt_long(argc, argv, optstr, opts, NULL);
2367 rc = fault_attr_nid_parse(optarg, &attr.fa_local_nid);
2371 case 's': /* source NID/NET */
2372 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2377 case 'd': /* dest NID/NET */
2378 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2383 case 'r': /* drop rate */
2384 if (opc == LNET_CTL_DROP_ADD)
2385 attr.u.drop.da_rate = strtoul(optarg, NULL, 0);
2387 attr.u.delay.la_rate = strtoul(optarg, NULL, 0);
2391 if (opc == LNET_CTL_DROP_ADD) {
2392 rc = fault_attr_health_error_parse(optarg,
2393 &attr.u.drop.da_health_error_mask);
2400 if (opc == LNET_CTL_DROP_ADD)
2401 attr.u.drop.da_drop_all = true;
2405 if (opc == LNET_CTL_DROP_ADD)
2406 attr.u.drop.da_random = true;
2409 case 'i': /* time interval (# seconds) for message drop */
2410 if (opc == LNET_CTL_DROP_ADD)
2411 attr.u.drop.da_interval = strtoul(optarg,
2414 attr.u.delay.la_interval = strtoul(optarg,
2418 case 'l': /* seconds to wait before activating rule */
2419 attr.u.delay.la_latency = strtoul(optarg, NULL, 0);
2422 case 'p': /* portal to filter */
2423 rc = fault_attr_ptl_parse(optarg, &attr.fa_ptl_mask);
2428 case 'm': /* message types to filter */
2429 rc = fault_attr_msg_parse(optarg, &attr.fa_msg_mask);
2435 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2436 argv[0], argv[optind - 1]);
2442 if (opc == LNET_CTL_DROP_ADD) {
2443 /* NB: drop rate and interval are exclusive to each other */
2444 if (!((attr.u.drop.da_rate == 0) ^
2445 (attr.u.drop.da_interval == 0))) {
2447 "please provide either drop rate or interval but not both at the same time.\n");
2451 if (attr.u.drop.da_random &&
2452 attr.u.drop.da_interval == 0) {
2454 "please provide an interval to randomize\n");
2457 } else if (opc == LNET_CTL_DELAY_ADD) {
2458 if (!((attr.u.delay.la_rate == 0) ^
2459 (attr.u.delay.la_interval == 0))) {
2461 "please provide either delay rate or interval but not both at the same time.\n");
2465 if (attr.u.delay.la_latency == 0) {
2466 fprintf(stderr, "latency cannot be zero\n");
2471 if (attr.fa_src == 0 || attr.fa_dst == 0) {
2473 "Please provide both source and destination of %s rule\n",
2478 if (attr.fa_local_nid == 0)
2479 attr.fa_local_nid = LNET_NID_ANY;
2481 data.ioc_flags = opc;
2482 data.ioc_inllen1 = sizeof(attr);
2483 data.ioc_inlbuf1 = (char *)&attr;
2484 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2485 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2489 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2491 fprintf(stderr, "add %s rule %s->%s failed: %s\n",
2492 name, libcfs_nid2str(attr.fa_src),
2493 libcfs_nid2str(attr.fa_dst), strerror(errno));
2497 printf("Added %s rule %s->%s (1/%d)\n",
2498 name, libcfs_nid2str(attr.fa_src), libcfs_nid2str(attr.fa_dst),
2499 opc == LNET_CTL_DROP_ADD ?
2500 attr.u.drop.da_rate : attr.u.delay.la_rate);
2509 jt_ptl_drop_add(int argc, char **argv)
2511 return fault_simul_rule_add(LNET_CTL_DROP_ADD, "drop", argc, argv);
2515 jt_ptl_delay_add(int argc, char **argv)
2517 return fault_simul_rule_add(LNET_CTL_DELAY_ADD, "delay", argc, argv);
2521 fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
2523 struct libcfs_ioctl_data data = { { 0 } };
2524 struct lnet_fault_attr attr;
2528 static const struct option opts[] = {
2529 { .name = "source", .has_arg = required_argument, .val = 's' },
2530 { .name = "dest", .has_arg = required_argument, .val = 'd' },
2531 { .name = "all", .has_arg = no_argument, .val = 'a' },
2536 "Failed, please provide source and destination of rule\n");
2540 memset(&attr, 0, sizeof(attr));
2542 int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
2549 rc = fault_attr_nid_parse(optarg, &attr.fa_src);
2554 rc = fault_attr_nid_parse(optarg, &attr.fa_dst);
2565 fprintf(stderr, "error: %s: option '%s' unrecognized\n",
2566 argv[0], argv[optind - 1]);
2572 data.ioc_flags = opc;
2573 data.ioc_inllen1 = sizeof(attr);
2574 data.ioc_inlbuf1 = (char *)&attr;
2575 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2576 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2580 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2582 fprintf(stderr, "remove %s rule %s->%s failed: %s\n", name,
2583 all ? "all" : libcfs_nid2str(attr.fa_src),
2584 all ? "all" : libcfs_nid2str(attr.fa_dst),
2589 libcfs_ioctl_unpack(&data, ioc_buf);
2590 printf("Removed %d %s rules\n", data.ioc_count, name);
2599 jt_ptl_drop_del(int argc, char **argv)
2601 return fault_simul_rule_del(LNET_CTL_DROP_DEL, "drop", argc, argv);
2605 jt_ptl_delay_del(int argc, char **argv)
2607 return fault_simul_rule_del(LNET_CTL_DELAY_DEL, "delay", argc, argv);
2611 fault_simul_rule_reset(__u32 opc, char *name, int argc, char **argv)
2613 struct libcfs_ioctl_data data = { { 0 } };
2616 LIBCFS_IOC_INIT(data);
2617 data.ioc_flags = opc;
2619 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, &data);
2621 fprintf(stderr, "failed to reset %s stats: %s\n",
2622 name, strerror(errno));
2629 jt_ptl_drop_reset(int argc, char **argv)
2631 return fault_simul_rule_reset(LNET_CTL_DROP_RESET, "drop", argc, argv);
2635 jt_ptl_delay_reset(int argc, char **argv)
2637 return fault_simul_rule_reset(LNET_CTL_DELAY_RESET, "delay",
2642 fault_simul_rule_list(__u32 opc, char *name, int argc, char **argv)
2644 struct libcfs_ioctl_data data = { { 0 } };
2645 struct lnet_fault_attr attr;
2646 struct lnet_fault_stat stat;
2649 printf("LNet %s rules:\n", name);
2650 for (pos = 0;; pos++) {
2653 memset(&attr, 0, sizeof(attr));
2654 memset(&stat, 0, sizeof(stat));
2656 data.ioc_count = pos;
2657 data.ioc_flags = opc;
2658 data.ioc_inllen1 = sizeof(attr);
2659 data.ioc_inlbuf1 = (char *)&attr;
2660 data.ioc_inllen2 = sizeof(stat);
2661 data.ioc_inlbuf2 = (char *)&stat;
2662 if (libcfs_ioctl_pack(&data, &ioc_buf, IOC_BUF_SIZE) != 0) {
2663 fprintf(stderr, "libcfs_ioctl_pack failed\n");
2667 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_LNET_FAULT, ioc_buf);
2671 libcfs_ioctl_unpack(&data, ioc_buf);
2673 if (opc == LNET_CTL_DROP_LIST) {
2674 printf("%s->%s (1/%d | %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2675 libcfs_nid2str(attr.fa_src),
2676 libcfs_nid2str(attr.fa_dst),
2677 attr.u.drop.da_rate, attr.u.drop.da_interval,
2678 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2679 (uintmax_t)stat.u.drop.ds_dropped,
2680 (uintmax_t)stat.fs_count,
2681 (uintmax_t)stat.fs_put,
2682 (uintmax_t)stat.fs_ack,
2683 (uintmax_t)stat.fs_get,
2684 (uintmax_t)stat.fs_reply);
2686 } else if (opc == LNET_CTL_DELAY_LIST) {
2687 printf("%s->%s (1/%d | %d, latency %d) ptl %#jx, msg %x, %ju/%ju, PUT %ju, ACK %ju, GET %ju, REP %ju\n",
2688 libcfs_nid2str(attr.fa_src),
2689 libcfs_nid2str(attr.fa_dst),
2690 attr.u.delay.la_rate, attr.u.delay.la_interval,
2691 attr.u.delay.la_latency,
2692 (uintmax_t)attr.fa_ptl_mask, attr.fa_msg_mask,
2693 (uintmax_t)stat.u.delay.ls_delayed,
2694 (uintmax_t)stat.fs_count,
2695 (uintmax_t)stat.fs_put,
2696 (uintmax_t)stat.fs_ack,
2697 (uintmax_t)stat.fs_get,
2698 (uintmax_t)stat.fs_reply);
2701 printf("found total %d\n", pos);
2707 jt_ptl_drop_list(int argc, char **argv)
2709 return fault_simul_rule_list(LNET_CTL_DROP_LIST, "drop", argc, argv);
2713 jt_ptl_delay_list(int argc, char **argv)
2715 return fault_simul_rule_list(LNET_CTL_DELAY_LIST, "delay", argc, argv);
2718 int jt_ptl_testprotocompat(int argc, char **argv)
2720 struct libcfs_ioctl_data data;
2726 fprintf(stderr, "usage: %s <number>\n", argv[0]);
2730 flags = strtol(argv[1], &end, 0);
2731 if (flags < 0 || *end != 0) {
2732 fprintf(stderr, "Can't parse flags '%s'\n", argv[1]);
2736 LIBCFS_IOC_INIT(data);
2737 data.ioc_flags = flags;
2738 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_TESTPROTOCOMPAT, &data);
2741 fprintf(stderr, "test proto compat %x failed: %s\n",
2742 flags, strerror(errno));
2746 printf("test proto compat %x OK\n", flags);