Whamcloud - gitweb
LU-6210 utils: Use C99 initializers in lfs_setquota_times()
[fs/lustre-release.git] / lnet / utils / lnetctl.c
index e0aea3a..70c986a 100644 (file)
@@ -32,7 +32,7 @@
 #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
@@ -58,6 +58,7 @@ 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_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"
@@ -135,17 +136,18 @@ command_t set_cmds[] = {
 
 command_t peer_cmds[] = {
        {"add", jt_add_peer_nid, 0, "add a peer NID\n"
-        "\t--key_nid: NID to identify peer. If not provided then the first\n"
-        "\t           NID in the list becomes the key NID of a newly created\n"
-        "\t           peer. \n"
+        "\t--prim_nid: Primary NID of the peer. If not provided then the first\n"
+        "\t            NID in the list becomes the Primary NID of a newly created\n"
+        "\t            peer. \n"
         "\t--nid: one or more peer NIDs\n"
         "\t--non_mr: create this peer as not Multi-Rail capable\n"},
        {"del", jt_del_peer_nid, 0, "delete a peer NID\n"
-        "\t--key_nid: NID to identify peer.\n"
+        "\t--prim_nid: Primary NID of the peer.\n"
         "\t--nid: list of NIDs to remove. If none provided,\n"
         "\t       peer is deleted\n"},
-       {"show", jt_show_peer, 0, "show peer credits\n"
-        "\t--primary_nid: NID of peer to filter on.\n"},
+       {"show", jt_show_peer, 0, "show peer information\n"
+        "\t--nid: NID of peer to filter on.\n"
+        "\t--verbose: Include  extended  statistics\n"},
        { 0, 0, 0, NULL }
 };
 
@@ -187,9 +189,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,
@@ -228,7 +230,7 @@ static int jt_set_numa(int argc, char **argv)
                return -1;
        }
 
-       rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
+       rc = lustre_lnet_config_numa_range(value, -1, &err_rc);
        if (rc != LUSTRE_CFG_RC_NO_ERR)
                cYAML_print_tree2file(stderr, err_rc);
 
@@ -355,10 +357,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,
@@ -411,14 +413,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) {
@@ -468,7 +469,7 @@ static int jt_add_ni(int argc, char **argv)
        char *ip2net = NULL;
        long int pto = -1, pc = -1, pbc = -1, cre = -1;
        struct cYAML *err_rc = NULL;
-       int rc, opt, num_global_cpts = 0;
+       int rc, opt, cpt_rc = -1;
        struct lnet_dlc_network_descr nw_descr;
        struct cfs_expr_list *global_cpts = NULL;
        struct lnet_ioctl_config_lnd_tunables tunables;
@@ -478,18 +479,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) {
@@ -542,10 +543,9 @@ static int jt_add_ni(int argc, char **argv)
                        }
                        break;
                case 's':
-                       num_global_cpts =
-                               cfs_expr_list_parse(optarg,
-                                                strlen(optarg),
-                                                0, UINT_MAX, &global_cpts);
+                       cpt_rc = cfs_expr_list_parse(optarg,
+                                                    strlen(optarg), 0,
+                                                    UINT_MAX, &global_cpts);
                        break;
                case 'h':
                        print_help(net_cmds, "net", "add");
@@ -564,7 +564,7 @@ static int jt_add_ni(int argc, char **argv)
        }
 
        rc = lustre_lnet_config_ni(&nw_descr,
-                                  (num_global_cpts > 0) ? global_cpts: NULL,
+                                  (cpt_rc == 0) ? global_cpts: NULL,
                                   ip2net, (found) ? &tunables : NULL,
                                   -1, &err_rc);
 
@@ -587,12 +587,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 +629,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 +677,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 +743,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) {
@@ -951,13 +947,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) {
@@ -1003,8 +998,7 @@ static int jt_import(int argc, char **argv)
                break;
        }
 
-       if (rc != LUSTRE_CFG_RC_NO_ERR)
-               cYAML_print_tree2file(stderr, err_rc);
+       cYAML_print_tree2file(stderr, err_rc);
 
        cYAML_free_tree(err_rc);
 
@@ -1019,10 +1013,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) {
@@ -1088,7 +1081,7 @@ static int jt_export(int argc, char **argv)
 
 static int jt_add_peer_nid(int argc, char **argv)
 {
-       char *key_nid = NULL;
+       char *prim_nid = NULL;
        char **nids = NULL, **nids2 = NULL;
        int size = 0;
        struct cYAML *err_rc = NULL;
@@ -1097,7 +1090,7 @@ static int jt_add_peer_nid(int argc, char **argv)
 
        const char *const short_options = "k:n:mh";
        const struct option long_options[] = {
-               { "key_nid", 1, NULL, 'k' },
+               { "prim_nid", 1, NULL, 'k' },
                { "nid", 1, NULL, 'n' },
                { "non_mr", 0, NULL, 'm'},
                { "help", 0, NULL, 'h' },
@@ -1108,7 +1101,7 @@ static int jt_add_peer_nid(int argc, char **argv)
                                  long_options, NULL)) != -1) {
                switch (opt) {
                case 'k':
-                       key_nid = optarg;
+                       prim_nid = optarg;
                        break;
                case 'n':
                        size = lustre_lnet_parse_nids(optarg, nids, size,
@@ -1129,13 +1122,16 @@ static int jt_add_peer_nid(int argc, char **argv)
                }
        }
 
-       rc = lustre_lnet_config_peer_nid(key_nid, nids, size,
+       rc = lustre_lnet_config_peer_nid(prim_nid, nids, size,
                                         !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);
@@ -1147,14 +1143,14 @@ failed:
 
 static int jt_del_peer_nid(int argc, char **argv)
 {
-       char *key_nid = NULL;
+       char *prim_nid = NULL;
        char **nids = NULL, **nids2 = NULL;
        struct cYAML *err_rc = NULL;
        int rc = LUSTRE_CFG_RC_NO_ERR, opt, i, size = 0;
 
        const char *const short_options = "k:n:h";
        const struct option long_options[] = {
-               { "key_nid", 1, NULL, 'k' },
+               { "prim_nid", 1, NULL, 'k' },
                { "nid", 1, NULL, 'n' },
                { "help", 0, NULL, 'h' },
                { NULL, 0, NULL, 0 },
@@ -1164,7 +1160,7 @@ static int jt_del_peer_nid(int argc, char **argv)
                                  long_options, NULL)) != -1) {
                switch (opt) {
                case 'k':
-                       key_nid = optarg;
+                       prim_nid = optarg;
                        break;
                case 'n':
                        size = lustre_lnet_parse_nids(optarg, nids, size,
@@ -1182,12 +1178,14 @@ static int jt_del_peer_nid(int argc, char **argv)
                }
        }
 
-       rc = lustre_lnet_del_peer_nid(key_nid, nids, size, -1, &err_rc);
+       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);
@@ -1199,14 +1197,14 @@ failed:
 
 static int jt_show_peer(int argc, char **argv)
 {
-       char *key_nid = NULL;
+       char *nid = NULL;
        int rc, opt;
        struct cYAML *err_rc = NULL, *show_rc = NULL;
        int detail = 0;
 
-       const char *const short_options = "k:vh";
+       const char *const short_options = "n:vh";
        const struct option long_options[] = {
-               { "key_nid", 1, NULL, 'k' },
+               { "nid", 1, NULL, 'n' },
                { "verbose", 0, NULL, 'v' },
                { "help", 0, NULL, 'h' },
                { NULL, 0, NULL, 0 },
@@ -1215,21 +1213,21 @@ static int jt_show_peer(int argc, char **argv)
        while ((opt = getopt_long(argc, argv, short_options,
                                  long_options, NULL)) != -1) {
                switch (opt) {
-               case 'k':
-                       key_nid = optarg;
+               case 'n':
+                       nid = optarg;
                        break;
                case 'v':
                        detail = 1;
                        break;
                case 'h':
-                       print_help(peer_cmds, "peer", "add");
+                       print_help(peer_cmds, "peer", "show");
                        return 0;
                default:
                        return 0;
                }
        }
 
-       rc = lustre_lnet_show_peer(key_nid, detail, -1, &show_rc, &err_rc);
+       rc = lustre_lnet_show_peer(nid, detail, -1, &show_rc, &err_rc);
 
        if (rc != LUSTRE_CFG_RC_NO_ERR)
                cYAML_print_tree2file(stderr, err_rc);
@@ -1258,9 +1256,19 @@ command_t list[] = {
        {"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;