Whamcloud - gitweb
LU-9480 lnet: tune lnet_peer_discovery_disabled with lnetctl
[fs/lustre-release.git] / lnet / utils / lnetctl.c
index f0a6568..430d91f 100644 (file)
@@ -30,9 +30,7 @@
 #include <limits.h>
 #include <libcfs/util/ioctl.h>
 #include <libcfs/util/parser.h>
-#include <lnet/lnetctl.h>
-#include <lnet/nidstr.h>
-#include "cyaml/cyaml.h"
+#include <cyaml.h>
 #include "lnetconfig/liblnetconfig.h"
 
 #define LNET_CONFIGURE         true
@@ -50,14 +48,17 @@ static int jt_show_net(int argc, char **argv);
 static int jt_show_routing(int argc, char **argv);
 static int jt_show_stats(int argc, char **argv);
 static int jt_show_peer(int argc, char **argv);
-static int jt_show_numa(int argc, char **argv);
+static int jt_show_global(int argc, char **argv);
 static int jt_set_tiny(int argc, char **argv);
 static int jt_set_small(int argc, char **argv);
 static int jt_set_large(int argc, char **argv);
 static int jt_set_numa(int argc, char **argv);
 static int jt_add_peer_nid(int argc, char **argv);
 static int jt_del_peer_nid(int argc, char **argv);
+static int jt_set_max_intf(int argc, char **argv);
+static int jt_set_discovery(int argc, char **argv);
 /*static int jt_show_peer(int argc, char **argv);*/
+static int lnetctl_list_commands(int argc, char **argv);
 
 command_t lnet_cmds[] = {
        {"configure", jt_config_lnet, 0, "configure lnet\n"
@@ -113,8 +114,8 @@ command_t stats_cmds[] = {
        { 0, 0, 0, NULL }
 };
 
-command_t numa_cmds[] = {
-       {"show", jt_show_numa, 0, "show NUMA range\n"},
+command_t global_cmds[] = {
+       {"show", jt_show_global, 0, "show global variables\n"},
        { 0, 0, 0, NULL }
 };
 
@@ -130,6 +131,12 @@ command_t set_cmds[] = {
         "\t1 - enable routing\n"},
        {"numa_range", jt_set_numa, 0, "set NUMA range for NI selection\n"
         "\tVALUE must be at least 0\n"},
+       {"max_interfaces", jt_set_max_intf, 0, "set the default value for "
+               "max interfaces\n"
+        "\tValue must be greater than 16\n"},
+       {"discovery", jt_set_discovery, 0, "enable/disable peer discovery\n"
+        "\t0 - disable peer discovery\n"
+        "\t1 - enable peer discovery (default)\n"},
        { 0, 0, 0, NULL }
 };
 
@@ -188,9 +195,9 @@ static int handle_help(const command_t *cmd_list, const char *cmd,
        opterr = 0;
 
        const char *const short_options = "h";
-       const struct option long_options[] = {
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
+       static const struct option long_options[] = {
+               { .name = "help", .has_arg = no_argument, .val = 'h' },
+               { .name = NULL }
        };
 
        while ((opt = getopt_long(argc, argv, short_options,
@@ -211,6 +218,33 @@ static int handle_help(const command_t *cmd_list, const char *cmd,
        return rc;
 }
 
+static int jt_set_max_intf(int argc, char **argv)
+{
+       long int value;
+       int rc;
+       struct cYAML *err_rc = NULL;
+
+       if (handle_help(set_cmds, "set", "max_interfaces", argc, argv) == 0)
+               return 0;
+
+       rc = parse_long(argv[1], &value);
+       if (rc != 0) {
+               cYAML_build_error(-1, -1, "parser", "set",
+                                 "cannot parse max_interfaces value", &err_rc);
+               cYAML_print_tree2file(stderr, err_rc);
+               cYAML_free_tree(err_rc);
+               return -1;
+       }
+
+       rc = lustre_lnet_config_max_intf(value, -1, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR)
+               cYAML_print_tree2file(stderr, err_rc);
+
+       cYAML_free_tree(err_rc);
+
+       return rc;
+}
+
 static int jt_set_numa(int argc, char **argv)
 {
        long int value;
@@ -238,6 +272,33 @@ static int jt_set_numa(int argc, char **argv)
        return rc;
 }
 
+static int jt_set_discovery(int argc, char **argv)
+{
+       long int value;
+       int rc;
+       struct cYAML *err_rc = NULL;
+
+       if (handle_help(set_cmds, "set", "discovery", argc, argv) == 0)
+               return 0;
+
+       rc = parse_long(argv[1], &value);
+       if (rc != 0) {
+               cYAML_build_error(-1, -1, "parser", "set",
+                                 "cannot parse discovery value", &err_rc);
+               cYAML_print_tree2file(stderr, err_rc);
+               cYAML_free_tree(err_rc);
+               return -1;
+       }
+
+       rc = lustre_lnet_config_discovery(value, -1, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR)
+               cYAML_print_tree2file(stderr, err_rc);
+
+       cYAML_free_tree(err_rc);
+
+       return rc;
+}
+
 static int jt_set_tiny(int argc, char **argv)
 {
        long int value;
@@ -356,10 +417,10 @@ static int jt_config_lnet(int argc, char **argv)
        int rc, opt;
 
        const char *const short_options = "ah";
-       const struct option long_options[] = {
-               { "all", 0, NULL, 'a' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
+       static const struct option long_options[] = {
+               { .name = "all",  .has_arg = no_argument, .val = 'a' },
+               { .name = "help", .has_arg = no_argument, .val = 'h' },
+               { .name = NULL }
        };
 
        while ((opt = getopt_long(argc, argv, short_options,
@@ -412,14 +473,13 @@ static int jt_add_route(int argc, char **argv)
        int rc, opt;
 
        const char *const short_options = "n:g:c:p:h";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "gateway", 1, NULL, 'g' },
-               { "hop-count", 1, NULL, 'c' },
-               { "priority", 1, NULL, 'p' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+       { .name = "net",       .has_arg = required_argument, .val = 'n' },
+       { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
+       { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
+       { .name = "priority",  .has_arg = required_argument, .val = 'p' },
+       { .name = "help",      .has_arg = no_argument,       .val = 'h' },
+       { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -479,18 +539,18 @@ static int jt_add_ni(int argc, char **argv)
        lustre_lnet_init_nw_descr(&nw_descr);
 
        const char *const short_options = "n:i:p:t:c:b:r:s:h";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "if", 1, NULL, 'i' },
-               { "ip2net", 1, NULL, 'p' },
-               { "peer-timeout", 1, NULL, 't' },
-               { "peer-credits", 1, NULL, 'c' },
-               { "peer-buffer-credits", 1, NULL, 'b' },
-               { "credits", 1, NULL, 'r' },
-               { "cpt", 1, NULL, 's' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+       { .name = "net",          .has_arg = required_argument, .val = 'n' },
+       { .name = "if",           .has_arg = required_argument, .val = 'i' },
+       { .name = "ip2net",       .has_arg = required_argument, .val = 'p' },
+       { .name = "peer-timeout", .has_arg = required_argument, .val = 't' },
+       { .name = "peer-credits", .has_arg = required_argument, .val = 'c' },
+       { .name = "peer-buffer-credits",
+                                 .has_arg = required_argument, .val = 'b' },
+       { .name = "credits",      .has_arg = required_argument, .val = 'r' },
+       { .name = "cpt",          .has_arg = required_argument, .val = 's' },
+       { .name = "help",         .has_arg = no_argument,       .val = 'h' },
+       { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -587,12 +647,11 @@ static int jt_del_route(int argc, char **argv)
        int rc, opt;
 
        const char *const short_options = "n:g:h";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "gateway", 1, NULL, 'g' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+               { .name = "net",     .has_arg = required_argument, .val = 'n' },
+               { .name = "gateway", .has_arg = required_argument, .val = 'g' },
+               { .name = "help",    .has_arg = no_argument,       .val = 'h' },
+               { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -630,12 +689,11 @@ static int jt_del_ni(int argc, char **argv)
        lustre_lnet_init_nw_descr(&nw_descr);
 
        const char *const short_options = "n:i:h";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "if", 1, NULL, 'i' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+       { .name = "net",        .has_arg = required_argument,   .val = 'n' },
+       { .name = "if",         .has_arg = required_argument,   .val = 'i' },
+       { .name = "help",       .has_arg = no_argument,         .val = 'h' },
+       { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -679,15 +737,14 @@ static int jt_show_route(int argc, char **argv)
        struct cYAML *err_rc = NULL, *show_rc = NULL;
 
        const char *const short_options = "n:g:h:p:vh";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "gateway", 1, NULL, 'g' },
-               { "hop-count", 1, NULL, 'c' },
-               { "priority", 1, NULL, 'p' },
-               { "verbose", 0, NULL, 'v' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+       { .name = "net",       .has_arg = required_argument, .val = 'n' },
+       { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
+       { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
+       { .name = "priority",  .has_arg = required_argument, .val = 'p' },
+       { .name = "verbose",   .has_arg = no_argument,       .val = 'v' },
+       { .name = "help",      .has_arg = no_argument,       .val = 'h' },
+       { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -746,12 +803,11 @@ static int jt_show_net(int argc, char **argv)
        struct cYAML *err_rc = NULL, *show_rc = NULL;
 
        const char *const short_options = "n:vh";
-       const struct option long_options[] = {
-               { "net", 1, NULL, 'n' },
-               { "verbose", 0, NULL, 'v' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+               { .name = "net",     .has_arg = required_argument, .val = 'n' },
+               { .name = "verbose", .has_arg = no_argument,       .val = 'v' },
+               { .name = "help",    .has_arg = no_argument,       .val = 'h' },
+               { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -825,21 +881,36 @@ static int jt_show_stats(int argc, char **argv)
        return rc;
 }
 
-static int jt_show_numa(int argc, char **argv)
+static int jt_show_global(int argc, char **argv)
 {
        int rc;
        struct cYAML *show_rc = NULL, *err_rc = NULL;
 
-       if (handle_help(numa_cmds, "numa", "show", argc, argv) == 0)
+       if (handle_help(global_cmds, "global", "show", argc, argv) == 0)
                return 0;
 
        rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR) {
+               cYAML_print_tree2file(stderr, err_rc);
+               goto out;
+       }
 
-       if (rc != LUSTRE_CFG_RC_NO_ERR)
+       rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR) {
                cYAML_print_tree2file(stderr, err_rc);
-       else if (show_rc)
+               goto out;
+       }
+
+       rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR) {
+               cYAML_print_tree2file(stderr, err_rc);
+               goto out;
+       }
+
+       if (show_rc)
                cYAML_print_tree(show_rc);
 
+out:
        cYAML_free_tree(err_rc);
        cYAML_free_tree(show_rc);
 
@@ -906,16 +977,16 @@ static inline int jt_stats(int argc, char **argv)
        return Parser_execarg(argc - 1, &argv[1], stats_cmds);
 }
 
-static inline int jt_numa(int argc, char **argv)
+static inline int jt_global(int argc, char **argv)
 {
        if (argc < 2)
                return CMD_HELP;
 
        if (argc == 2 &&
-           handle_help(numa_cmds, "numa", NULL, argc, argv) == 0)
+           handle_help(global_cmds, "global", NULL, argc, argv) == 0)
                return 0;
 
-       return Parser_execarg(argc - 1, &argv[1], numa_cmds);
+       return Parser_execarg(argc - 1, &argv[1], global_cmds);
 }
 
 static inline int jt_peers(int argc, char **argv)
@@ -951,13 +1022,12 @@ static int jt_import(int argc, char **argv)
        char cmd = 'a';
 
        const char *const short_options = "adsh";
-       const struct option long_options[] = {
-               { "add", 0, NULL, 'a' },
-               { "del", 0, NULL, 'd' },
-               { "show", 0, NULL, 's' },
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+               { .name = "add",  .has_arg = no_argument, .val = 'a' },
+               { .name = "del",  .has_arg = no_argument, .val = 'd' },
+               { .name = "show", .has_arg = no_argument, .val = 's' },
+               { .name = "help", .has_arg = no_argument, .val = 'h' },
+               { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -1018,10 +1088,9 @@ static int jt_export(int argc, char **argv)
        FILE *f = NULL;
 
        const char *const short_options = "h";
-       const struct option long_options[] = {
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       static const struct option long_options[] = {
+               { .name = "help", .has_arg = no_argument, .val = 'h' },
+               { .name = NULL } };
 
        while ((opt = getopt_long(argc, argv, short_options,
                                   long_options, NULL)) != -1) {
@@ -1074,6 +1143,12 @@ static int jt_export(int argc, char **argv)
                cYAML_free_tree(err_rc);
        }
 
+       rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
+       if (rc != LUSTRE_CFG_RC_NO_ERR) {
+               cYAML_print_tree2file(stderr, err_rc);
+               cYAML_free_tree(err_rc);
+       }
+
        if (show_rc != NULL) {
                cYAML_print_tree2file(f, show_rc);
                cYAML_free_tree(show_rc);
@@ -1132,9 +1207,12 @@ static int jt_add_peer_nid(int argc, char **argv)
                                         !non_mr, -1, &err_rc);
 
 failed:
-       for (i = 0; i < size; i++)
-               free(nids[i]);
-       free(nids);
+       if (nids) {
+               /* free the array of nids */
+               for (i = 0; i < size; i++)
+                       free(nids[i]);
+               free(nids);
+       }
 
        if (rc != LUSTRE_CFG_RC_NO_ERR)
                cYAML_print_tree2file(stderr, err_rc);
@@ -1184,9 +1262,11 @@ static int jt_del_peer_nid(int argc, char **argv)
        rc = lustre_lnet_del_peer_nid(prim_nid, nids, size, -1, &err_rc);
 
 failed:
-       for (i = 0; i < size; i++)
-               free(nids[i]);
-       free(nids);
+       if (nids) {
+               for (i = 0; i < size; i++)
+                       free(nids[i]);
+               free(nids);
+       }
 
        if (rc != LUSTRE_CFG_RC_NO_ERR)
                cYAML_print_tree2file(stderr, err_rc);
@@ -1247,19 +1327,30 @@ command_t list[] = {
        {"net", jt_net, 0, "net {add | del | show | help}"},
        {"routing", jt_routing, 0, "routing {show | help}"},
        {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
-                          " | routing}"},
+                          " | routing | numa_range | max_interfaces"
+                          " | discovery}"},
        {"import", jt_import, 0, "import {--add | --del | --show | "
                                 "--help} FILE.yaml"},
        {"export", jt_export, 0, "export {--help} FILE.yaml"},
        {"stats", jt_stats, 0, "stats {show | help}"},
-       {"numa", jt_numa, 0, "numa {show | help}"},
+       {"global", jt_global, 0, "global {show | help}"},
        {"peer", jt_peers, 0, "peer {add | del | show | help}"},
        {"help", Parser_help, 0, "help"},
        {"exit", Parser_quit, 0, "quit"},
        {"quit", Parser_quit, 0, "quit"},
+       {"--list-commands", lnetctl_list_commands, 0, "list commands"},
        { 0, 0, 0, NULL }
 };
 
+static int lnetctl_list_commands(int argc, char **argv)
+{
+       char buffer[81] = ""; /* 80 printable chars + terminating NUL */
+
+       Parser_list_commands(list, buffer, sizeof(buffer), NULL, 0, 4);
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        int rc = 0;