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/parser.h>
31 #include <lnet/lnetctl.h>
32 #include "cyaml/cyaml.h"
33 #include "lnetconfig/liblnetconfig.h"
35 #define LNET_CONFIGURE true
36 #define LNET_UNCONFIGURE false
38 static int jt_config_lnet(int argc, char **argv);
39 static int jt_unconfig_lnet(int argc, char **argv);
40 static int jt_add_route(int argc, char **argv);
41 static int jt_add_net(int argc, char **argv);
42 static int jt_set_routing(int argc, char **argv);
43 static int jt_del_route(int argc, char **argv);
44 static int jt_del_net(int argc, char **argv);
45 static int jt_show_route(int argc, char **argv);
46 static int jt_show_net(int argc, char **argv);
47 static int jt_show_routing(int argc, char **argv);
48 static int jt_show_stats(int argc, char **argv);
49 static int jt_show_peer_credits(int argc, char **argv);
50 static int jt_set_tiny(int argc, char **argv);
51 static int jt_set_small(int argc, char **argv);
52 static int jt_set_large(int argc, char **argv);
54 command_t lnet_cmds[] = {
55 {"configure", jt_config_lnet, 0, "configure lnet\n"
56 "\t--all: load NI configuration from module parameters\n"},
57 {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
61 command_t route_cmds[] = {
62 {"add", jt_add_route, 0, "add a route\n"
63 "\t--net: net name (e.g. tcp0)\n"
64 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
65 "\t--hop: number to final destination (1 < hops < 255)\n"
66 "\t--priority: priority of route (0 - highest prio\n"},
67 {"del", jt_del_route, 0, "delete a route\n"
68 "\t--net: net name (e.g. tcp0)\n"
69 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
70 {"show", jt_show_route, 0, "show routes\n"
71 "\t--net: net name (e.g. tcp0) to filter on\n"
72 "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
73 "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
74 "\t--priority: priority of route (0 - highest prio to filter on\n"
75 "\t--verbose: display detailed output per route\n"},
79 command_t net_cmds[] = {
80 {"add", jt_add_net, 0, "add a network\n"
81 "\t--net: net name (e.g. tcp0)\n"
82 "\t--if: physical interface (e.g. eth0)\n"
83 "\t--ip2net: specify networks based on IP address patterns\n"
84 "\t--peer-timeout: time to wait before declaring a peer dead\n"
85 "\t--peer-credits: define the max number of inflight messages\n"
86 "\t--peer-buffer-credits: the number of buffer credits per peer\n"
87 "\t--credits: Network Interface credits\n"
88 "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
89 {"del", jt_del_net, 0, "delete a network\n"
90 "\t--net: net name (e.g. tcp0)\n"},
91 {"show", jt_show_net, 0, "show networks\n"
92 "\t--net: net name (e.g. tcp0) to filter on\n"
93 "\t--verbose: display detailed output per network\n"},
97 command_t routing_cmds[] = {
98 {"show", jt_show_routing, 0, "show routing information\n"},
102 command_t stats_cmds[] = {
103 {"show", jt_show_stats, 0, "show LNET statistics\n"},
107 command_t credits_cmds[] = {
108 {"show", jt_show_peer_credits, 0, "show peer credits\n"},
112 command_t set_cmds[] = {
113 {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
114 "\tVALUE must be greater than 0\n"},
115 {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
116 "\tVALUE must be greater than 0\n"},
117 {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
118 "\tVALUE must be greater than 0\n"},
119 {"routing", jt_set_routing, 0, "enable/disable routing\n"
120 "\t0 - disable routing\n"
121 "\t1 - enable routing\n"},
125 static inline void print_help(const command_t cmds[], const char *cmd_type,
128 const command_t *cmd;
130 for (cmd = cmds; cmd->pc_name; cmd++) {
131 if (pc_name != NULL &&
132 strcmp(cmd->pc_name, pc_name) == 0) {
133 printf("%s %s: %s\n", cmd_type, cmd->pc_name,
136 } else if (pc_name != NULL) {
139 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
143 static int parse_long(const char *number, long int *value)
147 *value = strtol(number, &end, 0);
148 if (end != NULL && *end != 0)
154 static int handle_help(const command_t *cmd_list, const char *cmd,
155 const char *sub_cmd, int argc, char **argv)
162 const char *const short_options = "h";
163 const struct option long_options[] = {
164 { "help", 0, NULL, 'h' },
165 { NULL, 0, NULL, 0 },
168 while ((opt = getopt_long(argc, argv, short_options,
169 long_options, NULL)) != -1) {
172 print_help(cmd_list, cmd, sub_cmd);
186 static int jt_set_tiny(int argc, char **argv)
190 struct cYAML *err_rc = NULL;
192 if (handle_help(set_cmds, "set", "tiny_buffers", argc, argv) == 0)
195 rc = parse_long(argv[1], &value);
197 cYAML_build_error(-1, -1, "parser", "set",
198 "cannot parse tiny_buffers value", &err_rc);
199 cYAML_print_tree2file(stderr, err_rc);
200 cYAML_free_tree(err_rc);
204 rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
205 if (rc != LUSTRE_CFG_RC_NO_ERR)
206 cYAML_print_tree2file(stderr, err_rc);
208 cYAML_free_tree(err_rc);
213 static int jt_set_small(int argc, char **argv)
217 struct cYAML *err_rc = NULL;
219 if (handle_help(set_cmds, "set", "small_buffers", argc, argv) == 0)
222 rc = parse_long(argv[1], &value);
224 cYAML_build_error(-1, -1, "parser", "set",
225 "cannot parse small_buffers value", &err_rc);
226 cYAML_print_tree2file(stderr, err_rc);
227 cYAML_free_tree(err_rc);
231 rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
232 if (rc != LUSTRE_CFG_RC_NO_ERR)
233 cYAML_print_tree2file(stderr, err_rc);
235 cYAML_free_tree(err_rc);
240 static int jt_set_large(int argc, char **argv)
244 struct cYAML *err_rc = NULL;
246 if (handle_help(set_cmds, "set", "large_buffers", argc, argv) == 0)
249 rc = parse_long(argv[1], &value);
251 cYAML_build_error(-1, -1, "parser", "set",
252 "cannot parse large_buffers value", &err_rc);
253 cYAML_print_tree2file(stderr, err_rc);
254 cYAML_free_tree(err_rc);
258 rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
259 if (rc != LUSTRE_CFG_RC_NO_ERR)
260 cYAML_print_tree2file(stderr, err_rc);
262 cYAML_free_tree(err_rc);
267 static int jt_set_routing(int argc, char **argv)
270 struct cYAML *err_rc = NULL;
273 if (handle_help(set_cmds, "set", "routing", argc, argv) == 0)
276 rc = parse_long(argv[1], &value);
277 if (rc != 0 || (value != 0 && value != 1)) {
278 cYAML_build_error(-1, -1, "parser", "set",
279 "cannot parse routing value.\n"
280 "must be 0 for disable or 1 for enable",
282 cYAML_print_tree2file(stderr, err_rc);
283 cYAML_free_tree(err_rc);
287 rc = lustre_lnet_enable_routing(value, -1, &err_rc);
289 if (rc != LUSTRE_CFG_RC_NO_ERR)
290 cYAML_print_tree2file(stderr, err_rc);
292 cYAML_free_tree(err_rc);
297 static int jt_config_lnet(int argc, char **argv)
299 struct cYAML *err_rc = NULL;
300 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;
340 if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
343 rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
345 if (rc != LUSTRE_CFG_RC_NO_ERR)
346 cYAML_print_tree2file(stderr, err_rc);
348 cYAML_free_tree(err_rc);
352 static int jt_add_route(int argc, char **argv)
354 char *network = NULL, *gateway = NULL;
355 long int hop = -1, prio = -1;
356 struct cYAML *err_rc = NULL;
359 const char *const short_options = "n:g:c:p:h";
360 const struct option long_options[] = {
361 { "net", 1, NULL, 'n' },
362 { "gateway", 1, NULL, 'g' },
363 { "hop-count", 1, NULL, 'c' },
364 { "priority", 1, NULL, 'p' },
365 { "help", 0, NULL, 'h' },
366 { NULL, 0, NULL, 0 },
369 while ((opt = getopt_long(argc, argv, short_options,
370 long_options, NULL)) != -1) {
379 rc = parse_long(optarg, &hop);
387 rc = parse_long(optarg, &prio);
395 print_help(route_cmds, "route", "add");
402 rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
404 if (rc != LUSTRE_CFG_RC_NO_ERR)
405 cYAML_print_tree2file(stderr, err_rc);
407 cYAML_free_tree(err_rc);
412 static int jt_add_net(int argc, char **argv)
414 char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
415 long int pto = -1, pc = -1, pbc = -1, cre = -1;
416 struct cYAML *err_rc = NULL;
419 const char *const short_options = "n:i:p:t:c:b:r:s:h";
420 const struct option long_options[] = {
421 { "net", 1, NULL, 'n' },
422 { "if", 1, NULL, 'i' },
423 { "ip2net", 1, NULL, 'p' },
424 { "peer-timeout", 1, NULL, 't' },
425 { "peer-credits", 1, NULL, 'c' },
426 { "peer-buffer-credits", 1, NULL, 'b' },
427 { "credits", 1, NULL, 'r' },
428 { "cpt", 1, NULL, 's' },
429 { "help", 0, NULL, 'h' },
430 { NULL, 0, NULL, 0 },
433 while ((opt = getopt_long(argc, argv, short_options,
434 long_options, NULL)) != -1) {
446 rc = parse_long(optarg, &pto);
454 rc = parse_long(optarg, &pc);
462 rc = parse_long(optarg, &pbc);
470 rc = parse_long(optarg, &cre);
481 print_help(net_cmds, "net", "add");
488 rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
489 cre, cpt, -1, &err_rc);
491 if (rc != LUSTRE_CFG_RC_NO_ERR)
492 cYAML_print_tree2file(stderr, err_rc);
494 cYAML_free_tree(err_rc);
499 static int jt_del_route(int argc, char **argv)
501 char *network = NULL, *gateway = NULL;
502 struct cYAML *err_rc = NULL;
505 const char *const short_options = "n:g:h";
506 const struct option long_options[] = {
507 { "net", 1, NULL, 'n' },
508 { "gateway", 1, NULL, 'g' },
509 { "help", 0, NULL, 'h' },
510 { NULL, 0, NULL, 0 },
513 while ((opt = getopt_long(argc, argv, short_options,
514 long_options, NULL)) != -1) {
523 print_help(route_cmds, "route", "del");
530 rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
532 if (rc != LUSTRE_CFG_RC_NO_ERR)
533 cYAML_print_tree2file(stderr, err_rc);
535 cYAML_free_tree(err_rc);
540 static int jt_del_net(int argc, char **argv)
542 char *network = NULL;
543 struct cYAML *err_rc = NULL;
546 const char *const short_options = "n:h";
547 const struct option long_options[] = {
548 { "net", 1, NULL, 'n' },
549 { "help", 0, NULL, 'h' },
550 { NULL, 0, NULL, 0 },
553 while ((opt = getopt_long(argc, argv, short_options,
554 long_options, NULL)) != -1) {
560 print_help(net_cmds, "net", "del");
567 rc = lustre_lnet_del_net(network, -1, &err_rc);
569 if (rc != LUSTRE_CFG_RC_NO_ERR)
570 cYAML_print_tree2file(stderr, err_rc);
572 cYAML_free_tree(err_rc);
577 static int jt_show_route(int argc, char **argv)
579 char *network = NULL, *gateway = NULL;
580 long int hop = -1, prio = -1;
581 int detail = 0, rc, opt;
582 struct cYAML *err_rc = NULL, *show_rc = NULL;
584 const char *const short_options = "n:g:h:p:vh";
585 const struct option long_options[] = {
586 { "net", 1, NULL, 'n' },
587 { "gateway", 1, NULL, 'g' },
588 { "hop-count", 1, NULL, 'c' },
589 { "priority", 1, NULL, 'p' },
590 { "verbose", 0, NULL, 'v' },
591 { "help", 0, NULL, 'h' },
592 { NULL, 0, NULL, 0 },
595 while ((opt = getopt_long(argc, argv, short_options,
596 long_options, NULL)) != -1) {
605 rc = parse_long(optarg, &hop);
613 rc = parse_long(optarg, &prio);
624 print_help(route_cmds, "route", "show");
631 rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
634 if (rc != LUSTRE_CFG_RC_NO_ERR)
635 cYAML_print_tree2file(stderr, err_rc);
637 cYAML_print_tree(show_rc);
639 cYAML_free_tree(err_rc);
640 cYAML_free_tree(show_rc);
645 static int jt_show_net(int argc, char **argv)
647 char *network = NULL;
648 int detail = 0, rc, opt;
649 struct cYAML *err_rc = NULL, *show_rc = NULL;
651 const char *const short_options = "n:vh";
652 const struct option long_options[] = {
653 { "net", 1, NULL, 'n' },
654 { "verbose", 0, NULL, 'v' },
655 { "help", 0, NULL, 'h' },
656 { NULL, 0, NULL, 0 },
659 while ((opt = getopt_long(argc, argv, short_options,
660 long_options, NULL)) != -1) {
669 print_help(net_cmds, "net", "show");
676 rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
678 if (rc != LUSTRE_CFG_RC_NO_ERR)
679 cYAML_print_tree2file(stderr, err_rc);
681 cYAML_print_tree(show_rc);
683 cYAML_free_tree(err_rc);
684 cYAML_free_tree(show_rc);
689 static int jt_show_routing(int argc, char **argv)
691 struct cYAML *err_rc = NULL, *show_rc = NULL;
694 if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
697 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
699 if (rc != LUSTRE_CFG_RC_NO_ERR)
700 cYAML_print_tree2file(stderr, err_rc);
702 cYAML_print_tree(show_rc);
704 cYAML_free_tree(err_rc);
705 cYAML_free_tree(show_rc);
710 static int jt_show_stats(int argc, char **argv)
713 struct cYAML *show_rc = NULL, *err_rc = NULL;
715 if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
718 rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
720 if (rc != LUSTRE_CFG_RC_NO_ERR)
721 cYAML_print_tree2file(stderr, err_rc);
723 cYAML_print_tree(show_rc);
725 cYAML_free_tree(err_rc);
726 cYAML_free_tree(show_rc);
731 static int jt_show_peer_credits(int argc, char **argv)
734 struct cYAML *show_rc = NULL, *err_rc = NULL;
736 if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
739 rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
741 if (rc != LUSTRE_CFG_RC_NO_ERR)
742 cYAML_print_tree2file(stderr, err_rc);
744 cYAML_print_tree(show_rc);
746 cYAML_free_tree(err_rc);
747 cYAML_free_tree(show_rc);
752 static inline int jt_lnet(int argc, char **argv)
758 handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
761 return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
764 static inline int jt_route(int argc, char **argv)
770 handle_help(route_cmds, "route", NULL, argc, argv) == 0)
773 return Parser_execarg(argc - 1, &argv[1], route_cmds);
776 static inline int jt_net(int argc, char **argv)
782 handle_help(net_cmds, "net", NULL, argc, argv) == 0)
785 return Parser_execarg(argc - 1, &argv[1], net_cmds);
788 static inline int jt_routing(int argc, char **argv)
794 handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
797 return Parser_execarg(argc - 1, &argv[1], routing_cmds);
800 static inline int jt_stats(int argc, char **argv)
806 handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
809 return Parser_execarg(argc - 1, &argv[1], stats_cmds);
812 static inline int jt_peer_credits(int argc, char **argv)
818 handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
821 return Parser_execarg(argc - 1, &argv[1], credits_cmds);
824 static inline int jt_set(int argc, char **argv)
830 handle_help(set_cmds, "set", NULL, argc, argv) == 0)
833 return Parser_execarg(argc - 1, &argv[1], set_cmds);
836 static int jt_import(int argc, char **argv)
839 struct cYAML *err_rc = NULL;
840 struct cYAML *show_rc = NULL;
841 int rc = 0, opt, opt_found = 0;
844 const char *const short_options = "adsh";
845 const struct option long_options[] = {
846 { "add", 0, NULL, 'a' },
847 { "del", 0, NULL, 'd' },
848 { "show", 0, NULL, 's' },
849 { "help", 0, NULL, 'h' },
850 { NULL, 0, NULL, 0 },
853 while ((opt = getopt_long(argc, argv, short_options,
854 long_options, NULL)) != -1) {
863 printf("import FILE\n"
864 "import < FILE : import a file\n"
865 "\t--add: add configuration\n"
866 "\t--del: delete configuration\n"
867 "\t--show: show configuration\n"
868 "\t--help: display this help\n"
869 "If no command option is given then --add"
870 " is assumed by default\n");
877 /* grab the file name if one exists */
878 if (opt_found && argc == 3)
880 else if (!opt_found && argc == 2)
885 rc = lustre_yaml_config(file, &err_rc);
888 rc = lustre_yaml_del(file, &err_rc);
891 rc = lustre_yaml_show(file, &show_rc, &err_rc);
892 cYAML_print_tree(show_rc);
893 cYAML_free_tree(show_rc);
897 if (rc != LUSTRE_CFG_RC_NO_ERR)
898 cYAML_print_tree2file(stderr, err_rc);
900 cYAML_free_tree(err_rc);
905 static int jt_export(int argc, char **argv)
907 struct cYAML *show_rc = NULL;
908 struct cYAML *err_rc = NULL;
912 const char *const short_options = "h";
913 const struct option long_options[] = {
914 { "help", 0, NULL, 'h' },
915 { NULL, 0, NULL, 0 },
918 while ((opt = getopt_long(argc, argv, short_options,
919 long_options, NULL)) != -1) {
922 printf("export FILE\n"
923 "export > FILE : export configuration\n"
924 "\t--help: display this help\n");
932 f = fopen(argv[1], "w");
938 rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
939 if (rc != LUSTRE_CFG_RC_NO_ERR) {
940 cYAML_print_tree2file(stderr, err_rc);
941 cYAML_free_tree(err_rc);
944 rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
946 if (rc != LUSTRE_CFG_RC_NO_ERR) {
947 cYAML_print_tree2file(stderr, err_rc);
948 cYAML_free_tree(err_rc);
951 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
952 if (rc != LUSTRE_CFG_RC_NO_ERR) {
953 cYAML_print_tree2file(stderr, err_rc);
954 cYAML_free_tree(err_rc);
957 if (show_rc != NULL) {
958 cYAML_print_tree2file(f, show_rc);
959 cYAML_free_tree(show_rc);
969 {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
970 {"route", jt_route, 0, "route {add | del | show | help}"},
971 {"net", jt_net, 0, "net {add | del | show | help}"},
972 {"routing", jt_routing, 0, "routing {show | help}"},
973 {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
975 {"import", jt_import, 0, "import {--add | --del | --show | "
976 "--help} FILE.yaml"},
977 {"export", jt_export, 0, "export {--help} FILE.yaml"},
978 {"stats", jt_stats, 0, "stats {show | help}"},
979 {"peer_credits", jt_peer_credits, 0, "peer_credits {show | help}"},
980 {"help", Parser_help, 0, "help"},
981 {"exit", Parser_quit, 0, "quit"},
982 {"quit", Parser_quit, 0, "quit"},
986 int main(int argc, char **argv)
989 struct cYAML *err_rc = NULL;
991 rc = lustre_lnet_config_lib_init();
993 cYAML_build_error(-1, -1, "lnetctl", "startup",
994 "cannot register LNet device", &err_rc);
995 cYAML_print_tree2file(stderr, err_rc);
999 Parser_init("lnetctl > ", list);
1001 rc = Parser_execarg(argc - 1, &argv[1], list);