4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 * Copyright (c) 2014, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
30 #include <libcfs/util/ioctl.h>
31 #include <libcfs/util/parser.h>
32 #include <lnet/lnetctl.h>
33 #include "cyaml/cyaml.h"
34 #include "lnetconfig/liblnetconfig.h"
36 #define LNET_CONFIGURE true
37 #define LNET_UNCONFIGURE false
39 static int jt_config_lnet(int argc, char **argv);
40 static int jt_unconfig_lnet(int argc, char **argv);
41 static int jt_add_route(int argc, char **argv);
42 static int jt_add_net(int argc, char **argv);
43 static int jt_set_routing(int argc, char **argv);
44 static int jt_del_route(int argc, char **argv);
45 static int jt_del_net(int argc, char **argv);
46 static int jt_show_route(int argc, char **argv);
47 static int jt_show_net(int argc, char **argv);
48 static int jt_show_routing(int argc, char **argv);
49 static int jt_show_stats(int argc, char **argv);
50 static int jt_show_peer_credits(int argc, char **argv);
51 static int jt_set_tiny(int argc, char **argv);
52 static int jt_set_small(int argc, char **argv);
53 static int jt_set_large(int argc, char **argv);
55 command_t lnet_cmds[] = {
56 {"configure", jt_config_lnet, 0, "configure lnet\n"
57 "\t--all: load NI configuration from module parameters\n"},
58 {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
62 command_t route_cmds[] = {
63 {"add", jt_add_route, 0, "add a route\n"
64 "\t--net: net name (e.g. tcp0)\n"
65 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
66 "\t--hop: number to final destination (1 < hops < 255)\n"
67 "\t--priority: priority of route (0 - highest prio\n"},
68 {"del", jt_del_route, 0, "delete a route\n"
69 "\t--net: net name (e.g. tcp0)\n"
70 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
71 {"show", jt_show_route, 0, "show routes\n"
72 "\t--net: net name (e.g. tcp0) to filter on\n"
73 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
74 "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
75 "\t--priority: priority of route (0 - highest prio to filter on\n"
76 "\t--verbose: display detailed output per route\n"},
80 command_t net_cmds[] = {
81 {"add", jt_add_net, 0, "add a network\n"
82 "\t--net: net name (e.g. tcp0)\n"
83 "\t--if: physical interface (e.g. eth0)\n"
84 "\t--ip2net: specify networks based on IP address patterns\n"
85 "\t--peer-timeout: time to wait before declaring a peer dead\n"
86 "\t--peer-credits: define the max number of inflight messages\n"
87 "\t--peer-buffer-credits: the number of buffer credits per peer\n"
88 "\t--credits: Network Interface credits\n"
89 "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
90 {"del", jt_del_net, 0, "delete a network\n"
91 "\t--net: net name (e.g. tcp0)\n"},
92 {"show", jt_show_net, 0, "show networks\n"
93 "\t--net: net name (e.g. tcp0) to filter on\n"
94 "\t--verbose: display detailed output per network\n"},
98 command_t routing_cmds[] = {
99 {"show", jt_show_routing, 0, "show routing information\n"},
103 command_t stats_cmds[] = {
104 {"show", jt_show_stats, 0, "show LNET statistics\n"},
108 command_t credits_cmds[] = {
109 {"show", jt_show_peer_credits, 0, "show peer credits\n"},
113 command_t set_cmds[] = {
114 {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
115 "\tVALUE must be greater than 0\n"},
116 {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
117 "\tVALUE must be greater than 0\n"},
118 {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
119 "\tVALUE must be greater than 0\n"},
120 {"routing", jt_set_routing, 0, "enable/disable routing\n"
121 "\t0 - disable routing\n"
122 "\t1 - enable routing\n"},
126 static inline void print_help(const command_t cmds[], const char *cmd_type,
129 const command_t *cmd;
131 for (cmd = cmds; cmd->pc_name; cmd++) {
132 if (pc_name != NULL &&
133 strcmp(cmd->pc_name, pc_name) == 0) {
134 printf("%s %s: %s\n", cmd_type, cmd->pc_name,
137 } else if (pc_name != NULL) {
140 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
144 static int parse_long(const char *number, long int *value)
148 *value = strtol(number, &end, 0);
149 if (end != NULL && *end != 0)
155 static int handle_help(const command_t *cmd_list, const char *cmd,
156 const char *sub_cmd, int argc, char **argv)
163 const char *const short_options = "h";
164 const struct option long_options[] = {
165 { "help", 0, NULL, 'h' },
166 { NULL, 0, NULL, 0 },
169 while ((opt = getopt_long(argc, argv, short_options,
170 long_options, NULL)) != -1) {
173 print_help(cmd_list, cmd, sub_cmd);
187 static int jt_set_tiny(int argc, char **argv)
191 struct cYAML *err_rc = NULL;
193 if (handle_help(set_cmds, "set", "tiny_buffers", argc, argv) == 0)
196 rc = parse_long(argv[1], &value);
198 cYAML_build_error(-1, -1, "parser", "set",
199 "cannot parse tiny_buffers value", &err_rc);
200 cYAML_print_tree2file(stderr, err_rc);
201 cYAML_free_tree(err_rc);
205 rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
206 if (rc != LUSTRE_CFG_RC_NO_ERR)
207 cYAML_print_tree2file(stderr, err_rc);
209 cYAML_free_tree(err_rc);
214 static int jt_set_small(int argc, char **argv)
218 struct cYAML *err_rc = NULL;
220 if (handle_help(set_cmds, "set", "small_buffers", argc, argv) == 0)
223 rc = parse_long(argv[1], &value);
225 cYAML_build_error(-1, -1, "parser", "set",
226 "cannot parse small_buffers value", &err_rc);
227 cYAML_print_tree2file(stderr, err_rc);
228 cYAML_free_tree(err_rc);
232 rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
233 if (rc != LUSTRE_CFG_RC_NO_ERR)
234 cYAML_print_tree2file(stderr, err_rc);
236 cYAML_free_tree(err_rc);
241 static int jt_set_large(int argc, char **argv)
245 struct cYAML *err_rc = NULL;
247 if (handle_help(set_cmds, "set", "large_buffers", argc, argv) == 0)
250 rc = parse_long(argv[1], &value);
252 cYAML_build_error(-1, -1, "parser", "set",
253 "cannot parse large_buffers value", &err_rc);
254 cYAML_print_tree2file(stderr, err_rc);
255 cYAML_free_tree(err_rc);
259 rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
260 if (rc != LUSTRE_CFG_RC_NO_ERR)
261 cYAML_print_tree2file(stderr, err_rc);
263 cYAML_free_tree(err_rc);
268 static int jt_set_routing(int argc, char **argv)
271 struct cYAML *err_rc = NULL;
274 if (handle_help(set_cmds, "set", "routing", argc, argv) == 0)
277 rc = parse_long(argv[1], &value);
278 if (rc != 0 || (value != 0 && value != 1)) {
279 cYAML_build_error(-1, -1, "parser", "set",
280 "cannot parse routing value.\n"
281 "must be 0 for disable or 1 for enable",
283 cYAML_print_tree2file(stderr, err_rc);
284 cYAML_free_tree(err_rc);
288 rc = lustre_lnet_enable_routing(value, -1, &err_rc);
290 if (rc != LUSTRE_CFG_RC_NO_ERR)
291 cYAML_print_tree2file(stderr, err_rc);
293 cYAML_free_tree(err_rc);
298 static int jt_config_lnet(int argc, char **argv)
300 struct cYAML *err_rc = NULL;
301 bool load_mod_params = false;
304 const char *const short_options = "ah";
305 const struct option long_options[] = {
306 { "all", 0, NULL, 'a' },
307 { "help", 0, NULL, 'h' },
308 { NULL, 0, NULL, 0 },
311 while ((opt = getopt_long(argc, argv, short_options,
312 long_options, NULL)) != -1) {
315 load_mod_params = true;
318 print_help(lnet_cmds, "lnet", "configure");
325 rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
328 if (rc != LUSTRE_CFG_RC_NO_ERR)
329 cYAML_print_tree2file(stderr, err_rc);
331 cYAML_free_tree(err_rc);
336 static int jt_unconfig_lnet(int argc, char **argv)
338 struct cYAML *err_rc = NULL;
341 if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
344 rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
346 if (rc != LUSTRE_CFG_RC_NO_ERR)
347 cYAML_print_tree2file(stderr, err_rc);
349 cYAML_free_tree(err_rc);
353 static int jt_add_route(int argc, char **argv)
355 char *network = NULL, *gateway = NULL;
356 long int hop = -1, prio = -1;
357 struct cYAML *err_rc = NULL;
360 const char *const short_options = "n:g:c:p:h";
361 const struct option long_options[] = {
362 { "net", 1, NULL, 'n' },
363 { "gateway", 1, NULL, 'g' },
364 { "hop-count", 1, NULL, 'c' },
365 { "priority", 1, NULL, 'p' },
366 { "help", 0, NULL, 'h' },
367 { NULL, 0, NULL, 0 },
370 while ((opt = getopt_long(argc, argv, short_options,
371 long_options, NULL)) != -1) {
380 rc = parse_long(optarg, &hop);
388 rc = parse_long(optarg, &prio);
396 print_help(route_cmds, "route", "add");
403 rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
405 if (rc != LUSTRE_CFG_RC_NO_ERR)
406 cYAML_print_tree2file(stderr, err_rc);
408 cYAML_free_tree(err_rc);
413 static int jt_add_net(int argc, char **argv)
415 char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
416 long int pto = -1, pc = -1, pbc = -1, cre = -1;
417 struct cYAML *err_rc = NULL;
420 const char *const short_options = "n:i:p:t:c:b:r:s:h";
421 const struct option long_options[] = {
422 { "net", 1, NULL, 'n' },
423 { "if", 1, NULL, 'i' },
424 { "ip2net", 1, NULL, 'p' },
425 { "peer-timeout", 1, NULL, 't' },
426 { "peer-credits", 1, NULL, 'c' },
427 { "peer-buffer-credits", 1, NULL, 'b' },
428 { "credits", 1, NULL, 'r' },
429 { "cpt", 1, NULL, 's' },
430 { "help", 0, NULL, 'h' },
431 { NULL, 0, NULL, 0 },
434 while ((opt = getopt_long(argc, argv, short_options,
435 long_options, NULL)) != -1) {
447 rc = parse_long(optarg, &pto);
455 rc = parse_long(optarg, &pc);
463 rc = parse_long(optarg, &pbc);
471 rc = parse_long(optarg, &cre);
482 print_help(net_cmds, "net", "add");
489 rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
490 cre, cpt, -1, NULL, &err_rc);
492 if (rc != LUSTRE_CFG_RC_NO_ERR)
493 cYAML_print_tree2file(stderr, err_rc);
495 cYAML_free_tree(err_rc);
500 static int jt_del_route(int argc, char **argv)
502 char *network = NULL, *gateway = NULL;
503 struct cYAML *err_rc = NULL;
506 const char *const short_options = "n:g:h";
507 const struct option long_options[] = {
508 { "net", 1, NULL, 'n' },
509 { "gateway", 1, NULL, 'g' },
510 { "help", 0, NULL, 'h' },
511 { NULL, 0, NULL, 0 },
514 while ((opt = getopt_long(argc, argv, short_options,
515 long_options, NULL)) != -1) {
524 print_help(route_cmds, "route", "del");
531 rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
533 if (rc != LUSTRE_CFG_RC_NO_ERR)
534 cYAML_print_tree2file(stderr, err_rc);
536 cYAML_free_tree(err_rc);
541 static int jt_del_net(int argc, char **argv)
543 char *network = NULL;
544 struct cYAML *err_rc = NULL;
547 const char *const short_options = "n:h";
548 const struct option long_options[] = {
549 { "net", 1, NULL, 'n' },
550 { "help", 0, NULL, 'h' },
551 { NULL, 0, NULL, 0 },
554 while ((opt = getopt_long(argc, argv, short_options,
555 long_options, NULL)) != -1) {
561 print_help(net_cmds, "net", "del");
568 rc = lustre_lnet_del_net(network, -1, &err_rc);
570 if (rc != LUSTRE_CFG_RC_NO_ERR)
571 cYAML_print_tree2file(stderr, err_rc);
573 cYAML_free_tree(err_rc);
578 static int jt_show_route(int argc, char **argv)
580 char *network = NULL, *gateway = NULL;
581 long int hop = -1, prio = -1;
582 int detail = 0, rc, opt;
583 struct cYAML *err_rc = NULL, *show_rc = NULL;
585 const char *const short_options = "n:g:h:p:vh";
586 const struct option long_options[] = {
587 { "net", 1, NULL, 'n' },
588 { "gateway", 1, NULL, 'g' },
589 { "hop-count", 1, NULL, 'c' },
590 { "priority", 1, NULL, 'p' },
591 { "verbose", 0, NULL, 'v' },
592 { "help", 0, NULL, 'h' },
593 { NULL, 0, NULL, 0 },
596 while ((opt = getopt_long(argc, argv, short_options,
597 long_options, NULL)) != -1) {
606 rc = parse_long(optarg, &hop);
614 rc = parse_long(optarg, &prio);
625 print_help(route_cmds, "route", "show");
632 rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
635 if (rc != LUSTRE_CFG_RC_NO_ERR)
636 cYAML_print_tree2file(stderr, err_rc);
638 cYAML_print_tree(show_rc);
640 cYAML_free_tree(err_rc);
641 cYAML_free_tree(show_rc);
646 static int jt_show_net(int argc, char **argv)
648 char *network = NULL;
649 int detail = 0, rc, opt;
650 struct cYAML *err_rc = NULL, *show_rc = NULL;
652 const char *const short_options = "n:vh";
653 const struct option long_options[] = {
654 { "net", 1, NULL, 'n' },
655 { "verbose", 0, NULL, 'v' },
656 { "help", 0, NULL, 'h' },
657 { NULL, 0, NULL, 0 },
660 while ((opt = getopt_long(argc, argv, short_options,
661 long_options, NULL)) != -1) {
670 print_help(net_cmds, "net", "show");
677 rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
679 if (rc != LUSTRE_CFG_RC_NO_ERR)
680 cYAML_print_tree2file(stderr, err_rc);
682 cYAML_print_tree(show_rc);
684 cYAML_free_tree(err_rc);
685 cYAML_free_tree(show_rc);
690 static int jt_show_routing(int argc, char **argv)
692 struct cYAML *err_rc = NULL, *show_rc = NULL;
695 if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
698 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
700 if (rc != LUSTRE_CFG_RC_NO_ERR)
701 cYAML_print_tree2file(stderr, err_rc);
703 cYAML_print_tree(show_rc);
705 cYAML_free_tree(err_rc);
706 cYAML_free_tree(show_rc);
711 static int jt_show_stats(int argc, char **argv)
714 struct cYAML *show_rc = NULL, *err_rc = NULL;
716 if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
719 rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
721 if (rc != LUSTRE_CFG_RC_NO_ERR)
722 cYAML_print_tree2file(stderr, err_rc);
724 cYAML_print_tree(show_rc);
726 cYAML_free_tree(err_rc);
727 cYAML_free_tree(show_rc);
732 static int jt_show_peer_credits(int argc, char **argv)
735 struct cYAML *show_rc = NULL, *err_rc = NULL;
737 if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
740 rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
742 if (rc != LUSTRE_CFG_RC_NO_ERR)
743 cYAML_print_tree2file(stderr, err_rc);
745 cYAML_print_tree(show_rc);
747 cYAML_free_tree(err_rc);
748 cYAML_free_tree(show_rc);
753 static inline int jt_lnet(int argc, char **argv)
759 handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
762 return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
765 static inline int jt_route(int argc, char **argv)
771 handle_help(route_cmds, "route", NULL, argc, argv) == 0)
774 return Parser_execarg(argc - 1, &argv[1], route_cmds);
777 static inline int jt_net(int argc, char **argv)
783 handle_help(net_cmds, "net", NULL, argc, argv) == 0)
786 return Parser_execarg(argc - 1, &argv[1], net_cmds);
789 static inline int jt_routing(int argc, char **argv)
795 handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
798 return Parser_execarg(argc - 1, &argv[1], routing_cmds);
801 static inline int jt_stats(int argc, char **argv)
807 handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
810 return Parser_execarg(argc - 1, &argv[1], stats_cmds);
813 static inline int jt_peer_credits(int argc, char **argv)
819 handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
822 return Parser_execarg(argc - 1, &argv[1], credits_cmds);
825 static inline int jt_set(int argc, char **argv)
831 handle_help(set_cmds, "set", NULL, argc, argv) == 0)
834 return Parser_execarg(argc - 1, &argv[1], set_cmds);
837 static int jt_import(int argc, char **argv)
840 struct cYAML *err_rc = NULL;
841 struct cYAML *show_rc = NULL;
842 int rc = 0, opt, opt_found = 0;
845 const char *const short_options = "adsh";
846 const struct option long_options[] = {
847 { "add", 0, NULL, 'a' },
848 { "del", 0, NULL, 'd' },
849 { "show", 0, NULL, 's' },
850 { "help", 0, NULL, 'h' },
851 { NULL, 0, NULL, 0 },
854 while ((opt = getopt_long(argc, argv, short_options,
855 long_options, NULL)) != -1) {
864 printf("import FILE\n"
865 "import < FILE : import a file\n"
866 "\t--add: add configuration\n"
867 "\t--del: delete configuration\n"
868 "\t--show: show configuration\n"
869 "\t--help: display this help\n"
870 "If no command option is given then --add"
871 " is assumed by default\n");
878 /* grab the file name if one exists */
879 if (opt_found && argc == 3)
881 else if (!opt_found && argc == 2)
886 rc = lustre_yaml_config(file, &err_rc);
889 rc = lustre_yaml_del(file, &err_rc);
892 rc = lustre_yaml_show(file, &show_rc, &err_rc);
893 cYAML_print_tree(show_rc);
894 cYAML_free_tree(show_rc);
898 if (rc != LUSTRE_CFG_RC_NO_ERR)
899 cYAML_print_tree2file(stderr, err_rc);
901 cYAML_free_tree(err_rc);
906 static int jt_export(int argc, char **argv)
908 struct cYAML *show_rc = NULL;
909 struct cYAML *err_rc = NULL;
913 const char *const short_options = "h";
914 const struct option long_options[] = {
915 { "help", 0, NULL, 'h' },
916 { NULL, 0, NULL, 0 },
919 while ((opt = getopt_long(argc, argv, short_options,
920 long_options, NULL)) != -1) {
923 printf("export FILE\n"
924 "export > FILE : export configuration\n"
925 "\t--help: display this help\n");
933 f = fopen(argv[1], "w");
939 rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
940 if (rc != LUSTRE_CFG_RC_NO_ERR) {
941 cYAML_print_tree2file(stderr, err_rc);
942 cYAML_free_tree(err_rc);
945 rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
947 if (rc != LUSTRE_CFG_RC_NO_ERR) {
948 cYAML_print_tree2file(stderr, err_rc);
949 cYAML_free_tree(err_rc);
952 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
953 if (rc != LUSTRE_CFG_RC_NO_ERR) {
954 cYAML_print_tree2file(stderr, err_rc);
955 cYAML_free_tree(err_rc);
958 if (show_rc != NULL) {
959 cYAML_print_tree2file(f, show_rc);
960 cYAML_free_tree(show_rc);
970 {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
971 {"route", jt_route, 0, "route {add | del | show | help}"},
972 {"net", jt_net, 0, "net {add | del | show | help}"},
973 {"routing", jt_routing, 0, "routing {show | help}"},
974 {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
976 {"import", jt_import, 0, "import {--add | --del | --show | "
977 "--help} FILE.yaml"},
978 {"export", jt_export, 0, "export {--help} FILE.yaml"},
979 {"stats", jt_stats, 0, "stats {show | help}"},
980 {"peer_credits", jt_peer_credits, 0, "peer_credits {show | help}"},
981 {"help", Parser_help, 0, "help"},
982 {"exit", Parser_quit, 0, "quit"},
983 {"quit", Parser_quit, 0, "quit"},
987 int main(int argc, char **argv)
990 struct cYAML *err_rc = NULL;
992 rc = lustre_lnet_config_lib_init();
994 cYAML_build_error(-1, -1, "lnetctl", "startup",
995 "cannot register LNet device", &err_rc);
996 cYAML_print_tree2file(stderr, err_rc);
1000 Parser_init("lnetctl > ", list);
1002 rc = Parser_execarg(argc - 1, &argv[1], list);