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>
30 #include <lnet/lnetctl.h>
31 #include <libcfs/libcfsutil.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;
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;
342 if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
345 rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
347 if (rc != LUSTRE_CFG_RC_NO_ERR)
348 cYAML_print_tree2file(stderr, err_rc);
350 cYAML_free_tree(err_rc);
354 static int jt_add_route(int argc, char **argv)
356 char *network = NULL, *gateway = NULL;
357 long int hop = -1, prio = -1;
358 struct cYAML *err_rc = NULL;
362 const char *const short_options = "n:g:c:p:h";
363 const struct option long_options[] = {
364 { "net", 1, NULL, 'n' },
365 { "gateway", 1, NULL, 'g' },
366 { "hop-count", 1, NULL, 'c' },
367 { "priority", 1, NULL, 'p' },
368 { "help", 0, NULL, 'h' },
369 { NULL, 0, NULL, 0 },
372 while ((opt = getopt_long(argc, argv, short_options,
373 long_options, NULL)) != -1) {
382 rc = parse_long(optarg, &hop);
390 rc = parse_long(optarg, &prio);
398 print_help(route_cmds, "route", "add");
405 rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
407 if (rc != LUSTRE_CFG_RC_NO_ERR)
408 cYAML_print_tree2file(stderr, err_rc);
410 cYAML_free_tree(err_rc);
415 static int jt_add_net(int argc, char **argv)
417 char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
418 long int pto = -1, pc = -1, pbc = -1, cre = -1;
419 struct cYAML *err_rc = NULL;
423 const char *const short_options = "n:i:p:t:c:b:r:s:h";
424 const struct option long_options[] = {
425 { "net", 1, NULL, 'n' },
426 { "if", 1, NULL, 'i' },
427 { "ip2net", 1, NULL, 'p' },
428 { "peer-timeout", 1, NULL, 't' },
429 { "peer-credits", 1, NULL, 'c' },
430 { "peer-buffer-credits", 1, NULL, 'b' },
431 { "credits", 1, NULL, 'r' },
432 { "cpt", 1, NULL, 's' },
433 { "help", 0, NULL, 'h' },
434 { NULL, 0, NULL, 0 },
437 while ((opt = getopt_long(argc, argv, short_options,
438 long_options, NULL)) != -1) {
450 rc = parse_long(optarg, &pto);
458 rc = parse_long(optarg, &pc);
466 rc = parse_long(optarg, &pbc);
474 rc = parse_long(optarg, &cre);
485 print_help(net_cmds, "net", "add");
492 rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
493 cre, cpt, -1, &err_rc);
495 if (rc != LUSTRE_CFG_RC_NO_ERR)
496 cYAML_print_tree2file(stderr, err_rc);
498 cYAML_free_tree(err_rc);
503 static int jt_del_route(int argc, char **argv)
505 char *network = NULL, *gateway = NULL;
506 struct cYAML *err_rc = NULL;
510 const char *const short_options = "n:g:h";
511 const struct option long_options[] = {
512 { "net", 1, NULL, 'n' },
513 { "gateway", 1, NULL, 'g' },
514 { "help", 0, NULL, 'h' },
515 { NULL, 0, NULL, 0 },
518 while ((opt = getopt_long(argc, argv, short_options,
519 long_options, NULL)) != -1) {
528 print_help(route_cmds, "route", "del");
535 rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
537 if (rc != LUSTRE_CFG_RC_NO_ERR)
538 cYAML_print_tree2file(stderr, err_rc);
540 cYAML_free_tree(err_rc);
545 static int jt_del_net(int argc, char **argv)
547 char *network = NULL;
548 struct cYAML *err_rc = NULL;
552 const char *const short_options = "n:h";
553 const struct option long_options[] = {
554 { "net", 1, NULL, 'n' },
555 { "help", 0, NULL, 'h' },
556 { NULL, 0, NULL, 0 },
559 while ((opt = getopt_long(argc, argv, short_options,
560 long_options, NULL)) != -1) {
566 print_help(net_cmds, "net", "del");
573 rc = lustre_lnet_del_net(network, -1, &err_rc);
575 if (rc != LUSTRE_CFG_RC_NO_ERR)
576 cYAML_print_tree2file(stderr, err_rc);
578 cYAML_free_tree(err_rc);
583 static int jt_show_route(int argc, char **argv)
585 char *network = NULL, *gateway = NULL;
586 long int hop = -1, prio = -1;
587 int detail = 0, rc, opt;
588 struct cYAML *err_rc = NULL, *show_rc = NULL;
591 const char *const short_options = "n:g:h:p:vh";
592 const struct option long_options[] = {
593 { "net", 1, NULL, 'n' },
594 { "gateway", 1, NULL, 'g' },
595 { "hop-count", 1, NULL, 'c' },
596 { "priority", 1, NULL, 'p' },
597 { "verbose", 0, NULL, 'v' },
598 { "help", 0, NULL, 'h' },
599 { NULL, 0, NULL, 0 },
602 while ((opt = getopt_long(argc, argv, short_options,
603 long_options, NULL)) != -1) {
612 rc = parse_long(optarg, &hop);
620 rc = parse_long(optarg, &prio);
631 print_help(route_cmds, "route", "show");
638 rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
641 if (rc != LUSTRE_CFG_RC_NO_ERR)
642 cYAML_print_tree2file(stderr, err_rc);
644 cYAML_print_tree(show_rc);
646 cYAML_free_tree(err_rc);
647 cYAML_free_tree(show_rc);
652 static int jt_show_net(int argc, char **argv)
654 char *network = NULL;
655 int detail = 0, rc, opt;
656 struct cYAML *err_rc = NULL, *show_rc = NULL;
659 const char *const short_options = "n:vh";
660 const struct option long_options[] = {
661 { "net", 1, NULL, 'n' },
662 { "verbose", 0, NULL, 'v' },
663 { "help", 0, NULL, 'h' },
664 { NULL, 0, NULL, 0 },
667 while ((opt = getopt_long(argc, argv, short_options,
668 long_options, NULL)) != -1) {
677 print_help(net_cmds, "net", "show");
684 rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
686 if (rc != LUSTRE_CFG_RC_NO_ERR)
687 cYAML_print_tree2file(stderr, err_rc);
689 cYAML_print_tree(show_rc);
691 cYAML_free_tree(err_rc);
692 cYAML_free_tree(show_rc);
697 static int jt_show_routing(int argc, char **argv)
699 struct cYAML *err_rc = NULL, *show_rc = NULL;
702 if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
705 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
707 if (rc != LUSTRE_CFG_RC_NO_ERR)
708 cYAML_print_tree2file(stderr, err_rc);
710 cYAML_print_tree(show_rc);
712 cYAML_free_tree(err_rc);
713 cYAML_free_tree(show_rc);
718 static int jt_show_stats(int argc, char **argv)
721 struct cYAML *show_rc = NULL, *err_rc = NULL;
723 if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
726 rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
728 if (rc != LUSTRE_CFG_RC_NO_ERR)
729 cYAML_print_tree2file(stderr, err_rc);
731 cYAML_print_tree(show_rc);
733 cYAML_free_tree(err_rc);
734 cYAML_free_tree(show_rc);
739 static int jt_show_peer_credits(int argc, char **argv)
742 struct cYAML *show_rc = NULL, *err_rc = NULL;
745 if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
748 rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
750 if (rc != LUSTRE_CFG_RC_NO_ERR)
751 cYAML_print_tree2file(stderr, err_rc);
753 cYAML_print_tree(show_rc);
755 cYAML_free_tree(err_rc);
756 cYAML_free_tree(show_rc);
761 static inline int jt_lnet(int argc, char **argv)
767 handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
770 return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
773 static inline int jt_route(int argc, char **argv)
779 handle_help(route_cmds, "route", NULL, argc, argv) == 0)
782 return Parser_execarg(argc - 1, &argv[1], route_cmds);
785 static inline int jt_net(int argc, char **argv)
791 handle_help(net_cmds, "net", NULL, argc, argv) == 0)
794 return Parser_execarg(argc - 1, &argv[1], net_cmds);
797 static inline int jt_routing(int argc, char **argv)
803 handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
806 return Parser_execarg(argc - 1, &argv[1], routing_cmds);
809 static inline int jt_stats(int argc, char **argv)
815 handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
818 return Parser_execarg(argc - 1, &argv[1], stats_cmds);
821 static inline int jt_peer_credits(int argc, char **argv)
827 handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
830 return Parser_execarg(argc - 1, &argv[1], credits_cmds);
833 static inline int jt_set(int argc, char **argv)
839 handle_help(set_cmds, "set", NULL, argc, argv) == 0)
842 return Parser_execarg(argc - 1, &argv[1], set_cmds);
845 static int jt_import(int argc, char **argv)
848 struct cYAML *err_rc = NULL;
849 struct cYAML *show_rc = NULL;
850 int rc = 0, opt, opt_found = 0;
854 const char *const short_options = "adsh";
855 const struct option long_options[] = {
856 { "add", 0, NULL, 'a' },
857 { "del", 0, NULL, 'd' },
858 { "show", 0, NULL, 's' },
859 { "help", 0, NULL, 'h' },
860 { NULL, 0, NULL, 0 },
863 while ((opt = getopt_long(argc, argv, short_options,
864 long_options, NULL)) != -1) {
873 printf("import FILE\n"
874 "import < FILE : import a file\n"
875 "\t--add: add configuration\n"
876 "\t--del: delete configuration\n"
877 "\t--show: show configuration\n"
878 "\t--help: display this help\n"
879 "If no command option is given then --add"
880 " is assumed by default\n");
887 /* grab the file name if one exists */
888 if (opt_found && argc == 3)
890 else if (!opt_found && argc == 2)
895 rc = lustre_yaml_config(file, &err_rc);
898 rc = lustre_yaml_del(file, &err_rc);
901 rc = lustre_yaml_show(file, &show_rc, &err_rc);
902 cYAML_print_tree(show_rc);
903 cYAML_free_tree(show_rc);
907 if (rc != LUSTRE_CFG_RC_NO_ERR)
908 cYAML_print_tree2file(stderr, err_rc);
910 cYAML_free_tree(err_rc);
915 static int jt_export(int argc, char **argv)
917 struct cYAML *show_rc = NULL;
918 struct cYAML *err_rc = NULL;
923 const char *const short_options = "h";
924 const struct option long_options[] = {
925 { "help", 0, NULL, 'h' },
926 { NULL, 0, NULL, 0 },
929 while ((opt = getopt_long(argc, argv, short_options,
930 long_options, NULL)) != -1) {
933 printf("export FILE\n"
934 "export > FILE : export configuration\n"
935 "\t--help: display this help\n");
943 f = fopen(argv[1], "w");
949 rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
950 if (rc != LUSTRE_CFG_RC_NO_ERR) {
951 cYAML_print_tree2file(stderr, err_rc);
952 cYAML_free_tree(err_rc);
955 rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
957 if (rc != LUSTRE_CFG_RC_NO_ERR) {
958 cYAML_print_tree2file(stderr, err_rc);
959 cYAML_free_tree(err_rc);
962 rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
963 if (rc != LUSTRE_CFG_RC_NO_ERR) {
964 cYAML_print_tree2file(stderr, err_rc);
965 cYAML_free_tree(err_rc);
968 if (show_rc != NULL) {
969 cYAML_print_tree2file(f, show_rc);
970 cYAML_free_tree(show_rc);
980 {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
981 {"route", jt_route, 0, "route {add | del | show | help}"},
982 {"net", jt_net, 0, "net {add | del | show | help}"},
983 {"routing", jt_routing, 0, "routing {show | help}"},
984 {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
986 {"import", jt_import, 0, "import {--add | --del | --show | "
987 "--help} FILE.yaml"},
988 {"export", jt_export, 0, "export {--help} FILE.yaml"},
989 {"stats", jt_stats, 0, "stats {show | help}"},
990 {"peer_credits", jt_peer_credits, 0, "stats {show | help}"},
991 {"help", Parser_help, 0, "help"},
992 {"exit", Parser_quit, 0, "quit"},
993 {"quit", Parser_quit, 0, "quit"},
997 int main(int argc, char **argv)
1000 struct cYAML *err_rc = NULL;
1002 rc = lustre_lnet_config_lib_init();
1004 cYAML_build_error(-1, -1, "lnetctl", "startup",
1005 "cannot register LNet device", &err_rc);
1006 cYAML_print_tree2file(stderr, err_rc);
1010 rc = libcfs_arch_init();
1012 cYAML_build_error(-1, -1, "lnetctl", "startup",
1013 "cannot initialize libcfs", &err_rc);
1014 cYAML_print_tree2file(stderr, err_rc);
1018 Parser_init("lnetctl > ", list);
1020 rc = Parser_execarg(argc - 1, &argv[1], list);
1027 libcfs_arch_cleanup();