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) 2013, Intel Corporation.
24 * Amir Shehata <amir.shehata@intel.com>
29 #include <lnet/lnetctl.h>
30 #include <libcfs/libcfsutil.h>
31 #include "cyaml/cyaml.h"
32 #include "lnetconfig/liblnetconfig.h"
34 #define LNET_CONFIGURE true
35 #define LNET_UNCONFIGURE false
37 static int jt_config_lnet(int argc, char **argv);
38 static int jt_unconfig_lnet(int argc, char **argv);
39 static int jt_add_route(int argc, char **argv);
40 static int jt_add_net(int argc, char **argv);
41 static int jt_set_routing(int argc, char **argv);
42 static int jt_del_route(int argc, char **argv);
43 static int jt_del_net(int argc, char **argv);
44 static int jt_show_route(int argc, char **argv);
45 static int jt_show_net(int argc, char **argv);
46 static int jt_show_routing(int argc, char **argv);
47 static int jt_show_stats(int argc, char **argv);
48 static int jt_show_peer_credits(int argc, char **argv);
49 static int jt_set_tiny(int argc, char **argv);
50 static int jt_set_small(int argc, char **argv);
51 static int jt_set_large(int argc, char **argv);
53 command_t lnet_cmds[] = {
54 {"configure", jt_config_lnet, 0, "configure lnet\n"
55 "\t--all: load NI configuration from module parameters\n"},
56 {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
60 command_t route_cmds[] = {
61 {"add", jt_add_route, 0, "add a route\n"
62 "\t--net: net name (e.g. tcp0)\n"
63 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
64 "\t--hop: number to final destination (1 < hops < 255)\n"
65 "\t--priority: priority of route (0 - highest prio\n"},
66 {"del", jt_del_route, 0, "delete a route\n"
67 "\t--net: net name (e.g. tcp0)\n"
68 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
69 {"show", jt_show_route, 0, "show routes\n"
70 "\t--net: net name (e.g. tcp0) to filter on\n"
71 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
72 "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
73 "\t--priority: priority of route (0 - highest prio to filter on\n"
74 "\t--vebose: display detailed output per route\n"},
78 command_t net_cmds[] = {
79 {"add", jt_add_net, 0, "add a network\n"
80 "\t--net: net name (e.g. tcp0)\n"
81 "\t--if: physical interface (e.g. eth0)\n"
82 "\t--ip2net: specify networks based on IP address patterns\n"
83 "\t--peer-timeout: time to wait before declaring a peer dead\n"
84 "\t--peer-credits: define the max number of inflight messages\n"
85 "\t--peer-buffer-credits: the number of buffer credits per peer\n"
86 "\t--credits: Network Interface credits\n"
87 "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
88 {"del", jt_del_net, 0, "delete a network\n"
89 "\t--net: net name (e.g. tcp0)\n"},
90 {"show", jt_show_net, 0, "show networks\n"
91 "\t--net: net name (e.g. tcp0) to filter on\n"
92 "\t--verbose: display detailed output per network\n"},
96 command_t routing_cmds[] = {
97 {"show", jt_show_routing, 0, "show routing information\n"},
101 command_t stats_cmds[] = {
102 {"show", jt_show_stats, 0, "show LNET statistics\n"},
106 command_t credits_cmds[] = {
107 {"show", jt_show_peer_credits, 0, "show peer credits\n"},
111 command_t set_cmds[] = {
112 {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
113 "\tVALUE must be greater than 0\n"},
114 {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
115 "\tVALUE must be greater than 0\n"},
116 {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
117 "\tVALUE must be greater than 0\n"},
118 {"routing", jt_set_routing, 0, "enable/disable routing\n"
119 "\t0 - disable routing\n"
120 "\t1 - enable routing\n"},
124 static inline void print_help(const command_t cmds[], const char *cmd_type,
127 const command_t *cmd;
129 for (cmd = cmds; cmd->pc_name; cmd++) {
130 if (pc_name != NULL &&
131 strcmp(cmd->pc_name, pc_name) == 0) {
132 printf("%s %s: %s\n", cmd_type, cmd->pc_name,
135 } else if (pc_name != NULL) {
138 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
142 static int parse_long(const char *number, long int *value)
146 *value = strtol(number, &end, 0);
147 if (end != NULL && *end != 0)
153 static int handle_help(const command_t *cmd_list, const char *cmd,
154 const char *sub_cmd, int argc, char **argv)
161 const char *const short_options = "h";
162 const struct option long_options[] = {
163 { "help", 0, NULL, 'h' },
164 { NULL, 0, NULL, 0 },
167 while ((opt = getopt_long(argc, argv, short_options,
168 long_options, NULL)) != -1) {
171 print_help(cmd_list, cmd, sub_cmd);
185 static int jt_set_tiny(int argc, char **argv)
189 struct cYAML *err_rc = NULL;
191 if (handle_help(set_cmds, "set", "tiny_buffers", argc, argv) == 0)
194 rc = parse_long(argv[1], &value);
196 cYAML_build_error(-1, -1, "parser", "set",
197 "cannot parse tiny_buffers value", &err_rc);
198 cYAML_print_tree2file(stderr, err_rc);
199 cYAML_free_tree(err_rc);
203 rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
204 if (rc != LUSTRE_CFG_RC_NO_ERR)
205 cYAML_print_tree2file(stderr, err_rc);
207 cYAML_free_tree(err_rc);
212 static int jt_set_small(int argc, char **argv)
216 struct cYAML *err_rc = NULL;
218 if (handle_help(set_cmds, "set", "small_buffers", argc, argv) == 0)
221 rc = parse_long(argv[1], &value);
223 cYAML_build_error(-1, -1, "parser", "set",
224 "cannot parse small_buffers value", &err_rc);
225 cYAML_print_tree2file(stderr, err_rc);
226 cYAML_free_tree(err_rc);
230 rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
231 if (rc != LUSTRE_CFG_RC_NO_ERR)
232 cYAML_print_tree2file(stderr, err_rc);
234 cYAML_free_tree(err_rc);
239 static int jt_set_large(int argc, char **argv)
243 struct cYAML *err_rc = NULL;
245 if (handle_help(set_cmds, "set", "large_buffers", argc, argv) == 0)
248 rc = parse_long(argv[1], &value);
250 cYAML_build_error(-1, -1, "parser", "set",
251 "cannot parse large_buffers value", &err_rc);
252 cYAML_print_tree2file(stderr, err_rc);
253 cYAML_free_tree(err_rc);
257 rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
258 if (rc != LUSTRE_CFG_RC_NO_ERR)
259 cYAML_print_tree2file(stderr, err_rc);
261 cYAML_free_tree(err_rc);
266 static int jt_set_routing(int argc, char **argv)
269 struct cYAML *err_rc = NULL;
272 if (handle_help(set_cmds, "set", "routing", argc, argv) == 0)
275 rc = parse_long(argv[1], &value);
276 if (rc != 0 || (value != 0 && value != 1)) {
277 cYAML_build_error(-1, -1, "parser", "set",
278 "cannot parse routing value.\n"
279 "must be 0 for disable or 1 for enable",
281 cYAML_print_tree2file(stderr, err_rc);
282 cYAML_free_tree(err_rc);
286 rc = lustre_lnet_enable_routing(value, -1, &err_rc);
288 if (rc != LUSTRE_CFG_RC_NO_ERR)
289 cYAML_print_tree2file(stderr, err_rc);
291 cYAML_free_tree(err_rc);
296 static int jt_config_lnet(int argc, char **argv)
298 struct cYAML *err_rc = NULL;
299 bool load_mod_params = false;
303 const char *const short_options = "ah";
304 const struct option long_options[] = {
305 { "all", 0, NULL, 'a' },
306 { "help", 0, NULL, 'h' },
307 { NULL, 0, NULL, 0 },
310 while ((opt = getopt_long(argc, argv, short_options,
311 long_options, NULL)) != -1) {
314 load_mod_params = true;
317 print_help(lnet_cmds, "lnet", "configure");
324 rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
327 if (rc != LUSTRE_CFG_RC_NO_ERR)
328 cYAML_print_tree2file(stderr, err_rc);
330 cYAML_free_tree(err_rc);
335 static int jt_unconfig_lnet(int argc, char **argv)
337 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;
361 const char *const short_options = "n:g:c:p:h";
362 const struct option long_options[] = {
363 { "net", 1, NULL, 'n' },
364 { "gateway", 1, NULL, 'g' },
365 { "hop-count", 1, NULL, 'c' },
366 { "priority", 1, NULL, 'p' },
367 { "help", 0, NULL, 'h' },
368 { NULL, 0, NULL, 0 },
371 while ((opt = getopt_long(argc, argv, short_options,
372 long_options, NULL)) != -1) {
381 rc = parse_long(optarg, &hop);
389 rc = parse_long(optarg, &prio);
397 print_help(route_cmds, "route", "add");
404 rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
406 if (rc != LUSTRE_CFG_RC_NO_ERR)
407 cYAML_print_tree2file(stderr, err_rc);
409 cYAML_free_tree(err_rc);
414 static int jt_add_net(int argc, char **argv)
416 char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
417 long int pto = -1, pc = -1, pbc = -1, cre = -1;
418 struct cYAML *err_rc = NULL;
422 const char *const short_options = "n:i:p:t:c:b:r:s:h";
423 const struct option long_options[] = {
424 { "net", 1, NULL, 'n' },
425 { "if", 1, NULL, 'i' },
426 { "ip2net", 1, NULL, 'p' },
427 { "peer-timeout", 1, NULL, 't' },
428 { "peer-credits", 1, NULL, 'c' },
429 { "peer-buffer-credits", 1, NULL, 'b' },
430 { "credits", 1, NULL, 'r' },
431 { "cpt", 1, NULL, 's' },
432 { "help", 0, NULL, 'h' },
433 { NULL, 0, NULL, 0 },
436 while ((opt = getopt_long(argc, argv, short_options,
437 long_options, NULL)) != -1) {
449 rc = parse_long(optarg, &pto);
457 rc = parse_long(optarg, &pc);
465 rc = parse_long(optarg, &pbc);
473 rc = parse_long(optarg, &cre);
484 print_help(net_cmds, "net", "add");
491 rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
492 cre, cpt, -1, &err_rc);
494 if (rc != LUSTRE_CFG_RC_NO_ERR)
495 cYAML_print_tree2file(stderr, err_rc);
497 cYAML_free_tree(err_rc);
502 static int jt_del_route(int argc, char **argv)
504 char *network = NULL, *gateway = NULL;
505 struct cYAML *err_rc = NULL;
509 const char *const short_options = "n:g:h";
510 const struct option long_options[] = {
511 { "net", 1, NULL, 'n' },
512 { "gateway", 1, NULL, 'g' },
513 { "help", 0, NULL, 'h' },
514 { NULL, 0, NULL, 0 },
517 while ((opt = getopt_long(argc, argv, short_options,
518 long_options, NULL)) != -1) {
527 print_help(route_cmds, "route", "del");
534 rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
536 if (rc != LUSTRE_CFG_RC_NO_ERR)
537 cYAML_print_tree2file(stderr, err_rc);
539 cYAML_free_tree(err_rc);
544 static int jt_del_net(int argc, char **argv)
546 char *network = NULL;
547 struct cYAML *err_rc = NULL;
551 const char *const short_options = "n:h";
552 const struct option long_options[] = {
553 { "net", 1, NULL, 'n' },
554 { "help", 0, NULL, 'h' },
555 { NULL, 0, NULL, 0 },
558 while ((opt = getopt_long(argc, argv, short_options,
559 long_options, NULL)) != -1) {
565 print_help(net_cmds, "net", "del");
572 rc = lustre_lnet_del_net(network, -1, &err_rc);
574 if (rc != LUSTRE_CFG_RC_NO_ERR)
575 cYAML_print_tree2file(stderr, err_rc);
577 cYAML_free_tree(err_rc);
582 static int jt_show_route(int argc, char **argv)
584 char *network = NULL, *gateway = NULL;
585 long int hop = -1, prio = -1;
586 int detail = 0, rc, opt;
587 struct cYAML *err_rc = NULL, *show_rc = NULL;
590 const char *const short_options = "n:g:h:p:vh";
591 const struct option long_options[] = {
592 { "net", 1, NULL, 'n' },
593 { "gateway", 1, NULL, 'g' },
594 { "hop-count", 1, NULL, 'c' },
595 { "priority", 1, NULL, 'p' },
596 { "verbose", 0, NULL, 'v' },
597 { "help", 0, NULL, 'h' },
598 { NULL, 0, NULL, 0 },
601 while ((opt = getopt_long(argc, argv, short_options,
602 long_options, NULL)) != -1) {
611 rc = parse_long(optarg, &hop);
619 rc = parse_long(optarg, &prio);
630 print_help(route_cmds, "route", "show");
637 rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
640 if (rc != LUSTRE_CFG_RC_NO_ERR)
641 cYAML_print_tree2file(stderr, err_rc);
643 cYAML_print_tree(show_rc);
645 cYAML_free_tree(err_rc);
646 cYAML_free_tree(show_rc);
651 static int jt_show_net(int argc, char **argv)
653 char *network = NULL;
654 int detail = 0, rc, opt;
655 struct cYAML *err_rc = NULL, *show_rc = NULL;
658 const char *const short_options = "n:vh";
659 const struct option long_options[] = {
660 { "net", 1, NULL, 'n' },
661 { "verbose", 0, NULL, 'v' },
662 { "help", 0, NULL, 'h' },
663 { NULL, 0, NULL, 0 },
666 while ((opt = getopt_long(argc, argv, short_options,
667 long_options, NULL)) != -1) {
676 print_help(net_cmds, "net", "show");
683 rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
685 if (rc != LUSTRE_CFG_RC_NO_ERR)
686 cYAML_print_tree2file(stderr, err_rc);
688 cYAML_print_tree(show_rc);
690 cYAML_free_tree(err_rc);
691 cYAML_free_tree(show_rc);
696 static int jt_show_routing(int argc, char **argv)
698 struct cYAML *err_rc = NULL, *show_rc = NULL;
701 if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
704 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
706 if (rc != LUSTRE_CFG_RC_NO_ERR)
707 cYAML_print_tree2file(stderr, err_rc);
709 cYAML_print_tree(show_rc);
711 cYAML_free_tree(err_rc);
712 cYAML_free_tree(show_rc);
717 static int jt_show_stats(int argc, char **argv)
720 struct cYAML *show_rc = NULL, *err_rc = NULL;
722 if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
725 rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
727 if (rc != LUSTRE_CFG_RC_NO_ERR)
728 cYAML_print_tree2file(stderr, err_rc);
730 cYAML_print_tree(show_rc);
732 cYAML_free_tree(err_rc);
733 cYAML_free_tree(show_rc);
738 static int jt_show_peer_credits(int argc, char **argv)
741 struct cYAML *show_rc = NULL, *err_rc = NULL;
744 if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
747 rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
749 if (rc != LUSTRE_CFG_RC_NO_ERR)
750 cYAML_print_tree2file(stderr, err_rc);
752 cYAML_print_tree(show_rc);
754 cYAML_free_tree(err_rc);
755 cYAML_free_tree(show_rc);
760 static inline int jt_lnet(int argc, char **argv)
766 handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
769 return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
772 static inline int jt_route(int argc, char **argv)
778 handle_help(route_cmds, "route", NULL, argc, argv) == 0)
781 return Parser_execarg(argc - 1, &argv[1], route_cmds);
784 static inline int jt_net(int argc, char **argv)
790 handle_help(net_cmds, "net", NULL, argc, argv) == 0)
793 return Parser_execarg(argc - 1, &argv[1], net_cmds);
796 static inline int jt_routing(int argc, char **argv)
802 handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
805 return Parser_execarg(argc - 1, &argv[1], routing_cmds);
808 static inline int jt_stats(int argc, char **argv)
814 handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
817 return Parser_execarg(argc - 1, &argv[1], stats_cmds);
820 static inline int jt_peer_credits(int argc, char **argv)
826 handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
829 return Parser_execarg(argc - 1, &argv[1], credits_cmds);
832 static inline int jt_set(int argc, char **argv)
838 handle_help(set_cmds, "set", NULL, argc, argv) == 0)
841 return Parser_execarg(argc - 1, &argv[1], set_cmds);
844 static int jt_import(int argc, char **argv)
847 struct cYAML *err_rc = NULL;
848 struct cYAML *show_rc = NULL;
849 int rc = 0, opt, opt_found = 0;
853 const char *const short_options = "adsh";
854 const struct option long_options[] = {
855 { "add", 0, NULL, 'a' },
856 { "del", 0, NULL, 'd' },
857 { "show", 0, NULL, 's' },
858 { "help", 0, NULL, 'h' },
859 { NULL, 0, NULL, 0 },
862 while ((opt = getopt_long(argc, argv, short_options,
863 long_options, NULL)) != -1) {
872 printf("import FILE\n"
873 "import < FILE : import a file\n"
874 "\t--add: add configuration\n"
875 "\t--del: delete configuration\n"
876 "\t--show: show configuration\n"
877 "\t--help: display this help\n"
878 "If no command option is given then --add"
879 " is assumed by default\n");
886 /* grab the file name if one exists */
887 if (opt_found && argc == 3)
889 else if (!opt_found && argc == 2)
894 rc = lustre_yaml_config(file, &err_rc);
897 rc = lustre_yaml_del(file, &err_rc);
900 rc = lustre_yaml_show(file, &show_rc, &err_rc);
901 cYAML_print_tree(show_rc);
902 cYAML_free_tree(show_rc);
906 if (rc != LUSTRE_CFG_RC_NO_ERR)
907 cYAML_print_tree2file(stderr, err_rc);
909 cYAML_free_tree(err_rc);
914 static int jt_export(int argc, char **argv)
916 struct cYAML *show_rc = NULL;
917 struct cYAML *err_rc = NULL;
922 const char *const short_options = "h";
923 const struct option long_options[] = {
924 { "help", 0, NULL, 'h' },
925 { NULL, 0, NULL, 0 },
928 while ((opt = getopt_long(argc, argv, short_options,
929 long_options, NULL)) != -1) {
932 printf("export FILE\n"
933 "export > FILE : export configuration\n"
934 "\t--help: display this help\n");
942 f = fopen(argv[1], "w");
948 rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
949 if (rc != LUSTRE_CFG_RC_NO_ERR) {
950 cYAML_print_tree2file(stderr, err_rc);
951 cYAML_free_tree(err_rc);
954 rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
956 if (rc != LUSTRE_CFG_RC_NO_ERR) {
957 cYAML_print_tree2file(stderr, err_rc);
958 cYAML_free_tree(err_rc);
961 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
962 if (rc != LUSTRE_CFG_RC_NO_ERR) {
963 cYAML_print_tree2file(stderr, err_rc);
964 cYAML_free_tree(err_rc);
967 if (show_rc != NULL) {
968 cYAML_print_tree2file(f, show_rc);
969 cYAML_free_tree(show_rc);
979 {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
980 {"route", jt_route, 0, "route {add | del | show | help}"},
981 {"net", jt_net, 0, "net {add | del | show | help}"},
982 {"routing", jt_routing, 0, "routing {show | help}"},
983 {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
985 {"import", jt_import, 0, "import {--add | --del | --show | "
986 "--help} FILE.yaml"},
987 {"export", jt_export, 0, "export {--help} FILE.yaml"},
988 {"stats", jt_stats, 0, "stats {show | help}"},
989 {"peer_credits", jt_peer_credits, 0, "stats {show | help}"},
990 {"help", Parser_help, 0, "help"},
991 {"exit", Parser_quit, 0, "quit"},
992 {"quit", Parser_quit, 0, "quit"},
996 int main(int argc, char **argv)
999 struct cYAML *err_rc = NULL;
1001 rc = lustre_lnet_config_lib_init();
1003 cYAML_build_error(-1, -1, "lnetctl", "startup",
1004 "cannot register LNet device", &err_rc);
1005 cYAML_print_tree2file(stderr, err_rc);
1009 rc = libcfs_arch_init();
1011 cYAML_build_error(-1, -1, "lnetctl", "startup",
1012 "cannot initialize libcfs", &err_rc);
1013 cYAML_print_tree2file(stderr, err_rc);
1017 Parser_init("lnetctl > ", list);
1019 rc = Parser_execarg(argc - 1, &argv[1], list);
1026 libcfs_arch_cleanup();