Whamcloud - gitweb
LU-16605 lfs: Add -n option to fid2path
[fs/lustre-release.git] / lustre / utils / portals.c
index e859609..aac8b78 100644 (file)
@@ -41,7 +41,9 @@
 #include <linux/lnet/lnetctl.h>
 #include <linux/lnet/nidstr.h>
 #include <linux/lnet/socklnd.h>
+#include <lnetconfig/liblnetconfig.h>
 #include <lustre/lustreapi.h>
+#include <lustre_ioctl_old.h>
 
 unsigned int libcfs_debug;
 unsigned int libcfs_printk = D_CANTMASK;
@@ -292,7 +294,7 @@ int ptl_initialize(int argc, char **argv)
 int jt_ptl_network(int argc, char **argv)
 {
        struct libcfs_ioctl_data data;
-       __u32 net = LNET_NIDNET(LNET_NID_ANY);
+       __u32 net = LNET_NET_ANY;
        int rc;
 
        if (argc != 2) {
@@ -335,7 +337,7 @@ int jt_ptl_network(int argc, char **argv)
        }
 
        net = libcfs_str2net(argv[1]);
-       if (net == LNET_NIDNET(LNET_NID_ANY)) {
+       if (net == LNET_NET_ANY) {
                fprintf(stderr, "Can't parse net %s\n", argv[1]);
                return -1;
        }
@@ -345,13 +347,20 @@ int jt_ptl_network(int argc, char **argv)
        return 0;
 }
 
+#ifndef IOC_LIBCFS_GET_NI
+#define IOC_LIBCFS_GET_NI      _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
+#endif
+
 int
 jt_ptl_list_nids(int argc, char **argv)
 {
-       struct libcfs_ioctl_data data;
        int all = 0, return_nid = 0;
-       int count;
-       int rc;
+       yaml_emitter_t request;
+       yaml_parser_t reply;
+       yaml_event_t event;
+       struct nl_sock *sk;
+       bool done = false;
+       int rc = 0;
 
        all = (argc == 2) && (strcmp(argv[1], "all") == 0);
        /* Hack to pass back value */
@@ -362,7 +371,180 @@ jt_ptl_list_nids(int argc, char **argv)
                return 0;
        }
 
+       sk = nl_socket_alloc();
+       if (!sk)
+               goto old_api;
+
+       /* Setup parser to receive Netlink packets */
+       rc = yaml_parser_initialize(&reply);
+       if (rc == 0) {
+               yaml_parser_log_error(&reply, stderr, NULL);
+               goto old_api;
+       }
+
+       rc = yaml_parser_set_input_netlink(&reply, sk, false);
+       if (rc == 0) {
+               yaml_parser_log_error(&reply, stderr, NULL);
+               yaml_parser_delete(&reply);
+               goto old_api;
+       }
+
+       /* Create Netlink emitter to send request to kernel */
+       rc = yaml_emitter_initialize(&request);
+       if (rc == 0) {
+               yaml_parser_log_error(&reply, stderr, NULL);
+               yaml_parser_delete(&reply);
+               goto old_api;
+       }
+
+       rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME, 1,
+                                            LNET_CMD_NETS, NLM_F_DUMP);
+       if (rc == 0) {
+               yaml_emitter_log_error(&request, stderr);
+               yaml_emitter_delete(&request);
+               yaml_parser_delete(&reply);
+               goto old_api;
+       }
+
+       yaml_emitter_open(&request);
+       yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_mapping_start_event_initialize(&event, NULL,
+                                           (yaml_char_t *)YAML_MAP_TAG,
+                                           1, YAML_ANY_MAPPING_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_scalar_event_initialize(&event, NULL,
+                                    (yaml_char_t *)YAML_STR_TAG,
+                                    (yaml_char_t *)"net",
+                                    strlen("net"), 1, 0,
+                                    YAML_PLAIN_SCALAR_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       /* no net_id */
+       if (!g_net_set || g_net == LNET_NET_ANY) {
+               yaml_scalar_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_STR_TAG,
+                                            (yaml_char_t *)"",
+                                            strlen(""), 1, 0,
+                                            YAML_PLAIN_SCALAR_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+       } else {
+               char *net_id = libcfs_net2str(g_net);
+
+               yaml_sequence_start_event_initialize(&event, NULL,
+                                                    (yaml_char_t *)YAML_SEQ_TAG,
+                                                    1, YAML_ANY_SEQUENCE_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_mapping_start_event_initialize(&event, NULL,
+                                                   (yaml_char_t *)YAML_MAP_TAG,
+                                                   1, YAML_ANY_MAPPING_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_scalar_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_STR_TAG,
+                                            (yaml_char_t *)"net type",
+                                            strlen("net type"),
+                                            1, 0, YAML_PLAIN_SCALAR_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_scalar_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_STR_TAG,
+                                            (yaml_char_t *)net_id,
+                                            strlen(net_id), 1, 0,
+                                            YAML_PLAIN_SCALAR_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_mapping_end_event_initialize(&event);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_sequence_end_event_initialize(&event);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+       }
+       yaml_mapping_end_event_initialize(&event);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_document_end_event_initialize(&event, 0);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       rc = yaml_emitter_close(&request);
+emitter_error:
+       if (rc == 0) {
+               yaml_emitter_log_error(&request, stderr);
+               rc = -EINVAL;
+       }
+       yaml_emitter_delete(&request);
+
+       while (!done) {
+               rc = yaml_parser_parse(&reply, &event);
+               if (rc == 0)
+                       break;
+
+               if (event.type == YAML_SCALAR_EVENT &&
+                   strcmp((char *)event.data.scalar.value, "nid") == 0) {
+                       char *tmp;
+
+                       yaml_event_delete(&event);
+                       rc = yaml_parser_parse(&reply, &event);
+                       if (rc == 0) {
+                               yaml_event_delete(&event);
+                               break;
+                       }
+
+                       tmp = (char *)event.data.scalar.value;
+                       if (all || strcmp(tmp, "0@lo") != 0) {
+                               printf("%s\n", tmp);
+                               if (return_nid) {
+                                       *(__u64 *)(argv[1]) = libcfs_str2nid(tmp);
+                                       return_nid--;
+                               }
+                       }
+               }
+               done = (event.type == YAML_STREAM_END_EVENT);
+               yaml_event_delete(&event);
+       }
+
+       if (rc == 0)
+               yaml_parser_log_error(&reply, stderr, NULL);
+       yaml_parser_delete(&reply);
+old_api: {
+#ifdef IOC_LIBCFS_GET_NI
+       int count;
+
+       if (sk)
+               nl_socket_free(sk);
+       if (rc == 1)
+               return 0;
+
        for (count = 0;; count++) {
+               struct libcfs_ioctl_data data;
+
                LIBCFS_IOC_INIT(data);
                data.ioc_count = count;
                rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NI, &data);
@@ -385,7 +567,11 @@ jt_ptl_list_nids(int argc, char **argv)
                }
        }
 
-       return 0;
+#else
+       rc = -1;
+#endif
+       }
+       return rc;
 }
 
 int
@@ -896,27 +1082,32 @@ int jt_ptl_push_connection(int argc, char **argv)
        return 0;
 }
 
+#ifndef IOC_LIBCFS_PING_PEER
+#define IOC_LIBCFS_PING_PEER           _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
+#endif
+
 int jt_ptl_ping(int argc, char **argv)
 {
+       bool done = false, print = true;
        int rc;
        int timeout;
        struct lnet_process_id id;
-       struct lnet_process_id ids[16];
-       int maxids = sizeof(ids) / sizeof(ids[0]);
-       struct libcfs_ioctl_data data;
+       yaml_emitter_t request;
+       yaml_parser_t reply;
+       yaml_event_t event;
+       struct nl_sock *sk;
        char *sep;
-       int i;
 
        if (argc < 2) {
                fprintf(stderr, "usage: %s id [timeout (secs)]\n", argv[0]);
-               return 0;
+               return -EINVAL;
        }
 
        sep = strchr(argv[1], '-');
        if (!sep) {
                rc = lnet_parse_nid(argv[1], &id);
                if (rc != 0)
-                       return -1;
+                       return -EINVAL;
        } else {
                char   *end;
 
@@ -929,7 +1120,7 @@ int jt_ptl_ping(int argc, char **argv)
                if (end != sep) { /* assuming '-' is part of hostname */
                        rc = lnet_parse_nid(argv[1], &id);
                        if (rc != 0)
-                               return -1;
+                               return -EINVAL;
                } else {
                        id.nid = libcfs_str2nid(sep + 1);
 
@@ -937,7 +1128,7 @@ int jt_ptl_ping(int argc, char **argv)
                                fprintf(stderr,
                                        "Can't parse process id \"%s\"\n",
                                        argv[1]);
-                               return -1;
+                               return -EINVAL;
                        }
                }
        }
@@ -947,35 +1138,236 @@ int jt_ptl_ping(int argc, char **argv)
                if (timeout > 120 * 1000) {
                        fprintf(stderr, "Timeout %s is to large\n",
                                argv[2]);
-                       return -1;
+                       return -EINVAL;
                }
        } else {
                timeout = 1000; /* default 1 second timeout */
        }
 
-       LIBCFS_IOC_INIT(data);
-       data.ioc_nid     = id.nid;
-       data.ioc_u32[0]  = id.pid;
-       data.ioc_u32[1]  = timeout;
-       data.ioc_plen1   = sizeof(ids);
-       data.ioc_pbuf1   = (char *)ids;
+       /* Create Netlink emitter to send request to kernel */
+       sk = nl_socket_alloc();
+       if (!sk)
+               goto old_api;
+
+       /* Setup parser to recieve Netlink packets */
+       rc = yaml_parser_initialize(&reply);
+       if (rc == 0)
+               goto old_api;
+
+       rc = yaml_parser_set_input_netlink(&reply, sk, false);
+       if (rc == 0)
+               goto free_reply;
+
+       /* Create Netlink emitter to send request to kernel */
+       rc = yaml_emitter_initialize(&request);
+       if (rc == 0)
+               goto free_reply;
+
+       rc = yaml_emitter_set_output_netlink(&request, sk, LNET_GENL_NAME,
+                                            LNET_GENL_VERSION, LNET_CMD_PING,
+                                            NLM_F_DUMP);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_emitter_open(&request);
+       yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_mapping_start_event_initialize(&event, NULL,
+                                           (yaml_char_t *)YAML_MAP_TAG,
+                                           1, YAML_ANY_MAPPING_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_scalar_event_initialize(&event, NULL,
+                                    (yaml_char_t *)YAML_STR_TAG,
+                                    (yaml_char_t *)"ping",
+                                    strlen("ping"), 1, 0,
+                                    YAML_PLAIN_SCALAR_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_mapping_start_event_initialize(&event, NULL,
+                                           (yaml_char_t *)YAML_MAP_TAG,
+                                           1, YAML_ANY_MAPPING_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       if (timeout != 1000) {
+               yaml_scalar_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_STR_TAG,
+                                            (yaml_char_t *)"timeout",
+                                            strlen("timeout"), 1, 0,
+                                            YAML_PLAIN_SCALAR_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+
+               yaml_scalar_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_INT_TAG,
+                                            (yaml_char_t *)argv[2],
+                                            strlen(argv[2]), 1, 0,
+                                            YAML_PLAIN_SCALAR_STYLE);
+               rc = yaml_emitter_emit(&request, &event);
+               if (rc == 0)
+                       goto emitter_error;
+       }
+
+       yaml_scalar_event_initialize(&event, NULL,
+                                    (yaml_char_t *)YAML_STR_TAG,
+                                    (yaml_char_t *)"nids",
+                                    strlen("nids"), 1, 0,
+                                    YAML_PLAIN_SCALAR_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_sequence_start_event_initialize(&event, NULL,
+                                            (yaml_char_t *)YAML_SEQ_TAG,
+                                            1, YAML_FLOW_SEQUENCE_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       /* convert NID to string, in case libcfs_str2nid() did name lookup */
+       yaml_scalar_event_initialize(&event, NULL,
+                                    (yaml_char_t *)YAML_STR_TAG,
+                                    (yaml_char_t *)libcfs_nid2str(id.nid),
+                                    strlen(libcfs_nid2str(id.nid)), 1, 0,
+                                    YAML_PLAIN_SCALAR_STYLE);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_sequence_end_event_initialize(&event);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_mapping_end_event_initialize(&event);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_mapping_end_event_initialize(&event);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       yaml_document_end_event_initialize(&event, 0);
+       rc = yaml_emitter_emit(&request, &event);
+       if (rc == 0)
+               goto emitter_error;
+
+       rc = yaml_emitter_close(&request);
+emitter_error:
+       if (rc == 0) {
+               yaml_emitter_log_error(&request, stderr);
+               rc = -EINVAL;
+               goto old_api;
+       }
+       yaml_emitter_delete(&request);
+
+       /* Now parse the reply results */
+       while (!done) {
+               rc = yaml_parser_parse(&reply, &event);
+               if (rc == 0)
+                       break;
+
+               if (event.type != YAML_SCALAR_EVENT)
+                       goto skip;
 
-       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING, &data);
+               if (strcmp((char *)event.data.scalar.value, "nid") == 0) {
+                       yaml_event_delete(&event);
+                       rc = yaml_parser_parse(&reply, &event);
+                       if (rc == 0) {
+                               yaml_event_delete(&event);
+                               goto free_reply;
+                       }
+                       if (print) {
+                               /* Print 0@lo. Its not sent */
+                               printf("12345-0@lo\n");
+                               print = false;
+                       }
+                       printf("%s\n", (char *)event.data.scalar.value);
+               } else if (strcmp((char *)event.data.scalar.value,
+                                 "errno") == 0) {
+                       yaml_event_delete(&event);
+                       rc = yaml_parser_parse(&reply, &event);
+                       if (rc == 0) {
+                               yaml_event_delete(&event);
+                               goto free_reply;
+                       }
+                       rc = strtol((char *)event.data.scalar.value, NULL, 10);
+                       fprintf(stdout, "failed to ping %s: %s\n",
+                               argv[1], strerror(-rc));
+                       break; /* "rc" is clobbered if loop is run again */
+               }
+skip:
+               done = (event.type == YAML_STREAM_END_EVENT);
+               yaml_event_delete(&event);
+       }
+free_reply:
+       if (rc == 0) {
+               /* yaml_* functions return 0 for error */
+               const char *msg = yaml_parser_get_reader_error(&reply);
+
+               rc = errno ? -errno : -EHOSTUNREACH;
+               if (strcmp(msg, "Unspecific failure") != 0) {
+                       fprintf(stdout, "failed to ping %s: %s\n",
+                               argv[1], msg);
+               } else {
+                       fprintf(stdout, "failed to ping %s: %s\n",
+                               argv[1], strerror(errno));
+               }
+       } else if (rc == 1) {
+               /* yaml_* functions return 1 for success */
+               rc = 0;
+       }
+       yaml_parser_delete(&reply);
+       nl_socket_free(sk);
+       return rc;
+old_api:
+#ifdef IOC_LIBCFS_PING_PEER
+       {
+       struct lnet_process_id ids[LNET_INTERFACES_MAX_DEFAULT];
+       int maxids = sizeof(ids) / sizeof(ids[0]);
+       struct lnet_ioctl_ping_data ping = { { 0 } };
+       int i;
+
+       if (sk)
+               nl_socket_free(sk);
+
+       LIBCFS_IOC_INIT_V2(ping, ping_hdr);
+       ping.ping_hdr.ioc_len = sizeof(ping);
+       ping.ping_id = id;
+       ping.ping_src = LNET_NID_ANY;
+       ping.op_param = timeout;
+       ping.ping_count = maxids;
+       ping.ping_buf = ids;
+
+       rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_PING_PEER, &ping);
        if (rc != 0) {
-               fprintf(stderr, "failed to ping %s: %s\n",
-                       id.pid == LNET_PID_ANY ?
-                       libcfs_nid2str(id.nid) : libcfs_id2str(id),
+               fprintf(stderr, "failed to ping %s: %s\n", argv[1],
                        strerror(errno));
-               return -1;
+               return rc;
        }
 
-       for (i = 0; i < data.ioc_count && i < maxids; i++)
+       for (i = 0; i < ping.ping_count && i < maxids; i++)
                printf("%s\n", libcfs_id2str(ids[i]));
 
-       if (data.ioc_count > maxids)
-               printf("%d out of %d ids listed\n", maxids, data.ioc_count);
-
-       return 0;
+       if (ping.ping_count > maxids)
+               printf("%d out of %d ids listed\n", maxids, ping.ping_count);
+       }
+#else
+       rc = -ENOTTY;
+#endif
+       return rc;
 }
 
 int jt_ptl_mynid(int argc, char **argv)
@@ -1128,7 +1520,7 @@ jt_ptl_del_route(int argc, char **argv)
        }
 
        LIBCFS_IOC_INIT_V2(data, cfg_hdr);
-       data.cfg_net = g_net_set ? g_net : LNET_NIDNET(LNET_NID_ANY);
+       data.cfg_net = g_net_set ? g_net : LNET_NET_ANY;
        data.cfg_nid = nid;
 
        rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
@@ -1222,7 +1614,7 @@ jt_ptl_print_routes(int argc, char **argv)
                net     = data.cfg_net;
                hops    = data.cfg_config_u.cfg_route.rtr_hop;
                nid     = data.cfg_nid;
-               alive   = data.cfg_config_u.cfg_route.rtr_flags;
+               alive   = data.cfg_config_u.cfg_route.rtr_flags & LNET_RT_ALIVE;
                pri     = data.cfg_config_u.cfg_route.rtr_priority;
 
                printf("net %18s hops %u gw %32s %s pri %u\n",
@@ -1247,7 +1639,7 @@ fault_attr_nid_parse(char *str, lnet_nid_t *nid_p)
        /* NB: can't support range ipaddress except * and *@net */
        if (strlen(str) > 2 && str[0] == '*' && str[1] == '@') {
                net = libcfs_str2net(str + 2);
-               if (net == LNET_NIDNET(LNET_NID_ANY))
+               if (net == LNET_NET_ANY)
                        goto failed;
 
                nid = LNET_MKNID(net, LNET_NIDADDR(LNET_NID_ANY));
@@ -1384,7 +1776,7 @@ fault_simul_rule_add(__u32 opc, char *name, int argc, char **argv)
        optstr = opc == LNET_CTL_DROP_ADD ? "s:d:o:r:i:p:m:e:nx" : "s:d:o:r:l:p:m:";
        memset(&attr, 0, sizeof(attr));
        while (1) {
-               char c = getopt_long(argc, argv, optstr, opts, NULL);
+               int c = getopt_long(argc, argv, optstr, opts, NULL);
 
                if (c == -1)
                        break;
@@ -1566,7 +1958,7 @@ fault_simul_rule_del(__u32 opc, char *name, int argc, char **argv)
 
        memset(&attr, 0, sizeof(attr));
        while (1) {
-               char c = getopt_long(argc, argv, "s:d:a", opts, NULL);
+               int c = getopt_long(argc, argv, "s:d:a", opts, NULL);
 
                if (c == -1 || all)
                        break;