Whamcloud - gitweb
LU-12410 lnet: Convert lnetctl peer add and del
[fs/lustre-release.git] / lnet / utils / lnetctl.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * LGPL HEADER END
20  *
21  * Copyright (c) 2014, 2017, Intel Corporation.
22  *
23  * Author:
24  *   Amir Shehata <amir.shehata@intel.com>
25  */
26 #include <getopt.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <libcfs/util/ioctl.h>
32 #include <libcfs/util/parser.h>
33 #include <cyaml.h>
34 #include "lnetconfig/liblnetconfig.h"
35
36 #define LNET_CONFIGURE          true
37 #define LNET_UNCONFIGURE        false
38
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_ni(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_ni(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(int argc, char **argv);
51 static int jt_show_recovery(int argc, char **argv);
52 static int jt_show_global(int argc, char **argv);
53 static int jt_set_tiny(int argc, char **argv);
54 static int jt_set_small(int argc, char **argv);
55 static int jt_set_large(int argc, char **argv);
56 static int jt_set_numa(int argc, char **argv);
57 static int jt_set_retry_count(int argc, char **argv);
58 static int jt_set_transaction_to(int argc, char **argv);
59 static int jt_set_recov_intrv(int argc, char **argv);
60 static int jt_set_rtr_sensitivity(int argc, char **argv);
61 static int jt_set_hsensitivity(int argc, char **argv);
62 static int jt_add_peer_nid(int argc, char **argv);
63 static int jt_del_peer_nid(int argc, char **argv);
64 static int jt_set_max_intf(int argc, char **argv);
65 static int jt_set_discovery(int argc, char **argv);
66 static int jt_set_drop_asym_route(int argc, char **argv);
67 static int jt_list_peer(int argc, char **argv);
68 /*static int jt_show_peer(int argc, char **argv);*/
69 static int lnetctl_list_commands(int argc, char **argv);
70 static int jt_import(int argc, char **argv);
71 static int jt_export(int argc, char **argv);
72 static int jt_ping(int argc, char **argv);
73 static int jt_discover(int argc, char **argv);
74 static int jt_lnet(int argc, char **argv);
75 static int jt_route(int argc, char **argv);
76 static int jt_net(int argc, char **argv);
77 static int jt_routing(int argc, char **argv);
78 static int jt_set(int argc, char **argv);
79 static int jt_debug(int argc, char **argv);
80 static int jt_stats(int argc, char **argv);
81 static int jt_global(int argc, char **argv);
82 static int jt_peers(int argc, char **argv);
83 static int jt_set_ni_value(int argc, char **argv);
84 static int jt_set_peer_ni_value(int argc, char **argv);
85
86 command_t cmd_list[] = {
87         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
88         {"route", jt_route, 0, "route {add | del | show | help}"},
89         {"net", jt_net, 0, "net {add | del | show | help}"},
90         {"routing", jt_routing, 0, "routing {show | help}"},
91         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
92                            " | routing | numa_range | max_interfaces"
93                            " | discovery}"},
94         {"import", jt_import, 0, "import FILE.yaml"},
95         {"export", jt_export, 0, "export FILE.yaml"},
96         {"stats", jt_stats, 0, "stats {show | help}"},
97         {"debug", jt_debug, 0, "debug recovery {local | peer}"},
98         {"global", jt_global, 0, "global {show | help}"},
99         {"peer", jt_peers, 0, "peer {add | del | show | help}"},
100         {"ping", jt_ping, 0, "ping nid,[nid,...]"},
101         {"discover", jt_discover, 0, "discover nid[,nid,...]"},
102         {"help", Parser_help, 0, "help"},
103         {"exit", Parser_quit, 0, "quit"},
104         {"quit", Parser_quit, 0, "quit"},
105         {"--list-commands", lnetctl_list_commands, 0, "list commands"},
106         { 0, 0, 0, NULL }
107 };
108
109 command_t lnet_cmds[] = {
110         {"configure", jt_config_lnet, 0, "configure lnet\n"
111          "\t--all: load NI configuration from module parameters\n"},
112         {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
113         { 0, 0, 0, NULL }
114 };
115
116 command_t route_cmds[] = {
117         {"add", jt_add_route, 0, "add a route\n"
118          "\t--net: net name (e.g. tcp0)\n"
119          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
120          "\t--hop: number to final destination (1 < hops < 255)\n"
121          "\t--priority: priority of route (0 - highest prio\n"
122          "\t--health_sensitivity: gateway health sensitivity (>= 1)\n"},
123         {"del", jt_del_route, 0, "delete a route\n"
124          "\t--net: net name (e.g. tcp0)\n"
125          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
126         {"show", jt_show_route, 0, "show routes\n"
127          "\t--net: net name (e.g. tcp0) to filter on\n"
128          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
129          "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
130          "\t--priority: priority of route (0 - highest prio to filter on\n"
131          "\t--health_sensitivity: gateway health sensitivity (>= 1)\n"
132          "\t--verbose: display detailed output per route\n"},
133         { 0, 0, 0, NULL }
134 };
135
136 command_t net_cmds[] = {
137         {"add", jt_add_ni, 0, "add a network\n"
138          "\t--net: net name (e.g. tcp0)\n"
139          "\t--if: physical interface (e.g. eth0)\n"
140          "\t--ip2net: specify networks based on IP address patterns\n"
141          "\t--peer-timeout: time to wait before declaring a peer dead\n"
142          "\t--peer-credits: define the max number of inflight messages\n"
143          "\t--peer-buffer-credits: the number of buffer credits per peer\n"
144          "\t--credits: Network Interface credits\n"
145          "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
146         {"del", jt_del_ni, 0, "delete a network\n"
147          "\t--net: net name (e.g. tcp0)\n"
148          "\t--if: physical interface (e.g. eth0)\n"},
149         {"show", jt_show_net, 0, "show networks\n"
150          "\t--net: net name (e.g. tcp0) to filter on\n"
151          "\t--verbose: display detailed output per network."
152                        " Optional argument of '2' outputs more stats\n"},
153         {"set", jt_set_ni_value, 0, "set local NI specific parameter\n"
154          "\t--nid: NI NID to set the\n"
155          "\t--health: specify health value to set\n"
156          "\t--all: set all NIs value to the one specified\n"},
157         { 0, 0, 0, NULL }
158 };
159
160 command_t routing_cmds[] = {
161         {"show", jt_show_routing, 0, "show routing information\n"},
162         { 0, 0, 0, NULL }
163 };
164
165 command_t stats_cmds[] = {
166         {"show", jt_show_stats, 0, "show LNET statistics\n"},
167         { 0, 0, 0, NULL }
168 };
169
170 command_t debug_cmds[] = {
171         {"recovery", jt_show_recovery, 0, "list recovery queues\n"
172                 "\t--local : list local recovery queue\n"
173                 "\t--peer : list peer recovery queue\n"},
174         { 0, 0, 0, NULL }
175 };
176
177 command_t global_cmds[] = {
178         {"show", jt_show_global, 0, "show global variables\n"},
179         { 0, 0, 0, NULL }
180 };
181
182 command_t set_cmds[] = {
183         {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
184          "\tVALUE must be greater than 0\n"},
185         {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
186          "\tVALUE must be greater than 0\n"},
187         {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
188          "\tVALUE must be greater than 0\n"},
189         {"routing", jt_set_routing, 0, "enable/disable routing\n"
190          "\t0 - disable routing\n"
191          "\t1 - enable routing\n"},
192         {"numa_range", jt_set_numa, 0, "set NUMA range for NI selection\n"
193          "\tVALUE must be at least 0\n"},
194         {"max_interfaces", jt_set_max_intf, 0, "set the default value for "
195                 "max interfaces\n"
196          "\tValue must be greater than 16\n"},
197         {"discovery", jt_set_discovery, 0, "enable/disable peer discovery\n"
198          "\t0 - disable peer discovery\n"
199          "\t1 - enable peer discovery (default)\n"},
200         {"drop_asym_route", jt_set_drop_asym_route, 0,
201          "drop/accept asymmetrical route messages\n"
202          "\t0 - accept asymmetrical route messages (default)\n"
203          "\t1 - drop asymmetrical route messages\n"},
204         {"retry_count", jt_set_retry_count, 0, "number of retries\n"
205          "\t0 - turn of retries\n"
206          "\t>0 - number of retries\n"},
207         {"transaction_timeout", jt_set_transaction_to, 0, "Message/Response timeout\n"
208          "\t>0 - timeout in seconds\n"},
209         {"health_sensitivity", jt_set_hsensitivity, 0, "sensitivity to failure\n"
210          "\t0 - turn off health evaluation\n"
211          "\t>0 - sensitivity value not more than 1000\n"},
212         {"recovery_interval", jt_set_recov_intrv, 0, "interval to ping in seconds (at least 1)\n"
213          "\t>0 - time in seconds between pings\n"},
214         {"router_sensitivity", jt_set_rtr_sensitivity, 0, "router sensitivity %\n"
215          "\t100 - router interfaces need to be fully healthy to be used\n"
216          "\t<100 - router interfaces can be used even if not healthy\n"},
217         { 0, 0, 0, NULL }
218 };
219
220 command_t peer_cmds[] = {
221         {"add", jt_add_peer_nid, 0, "add a peer NID\n"
222          "\t--prim_nid: Primary NID of the peer. If not provided then the first\n"
223          "\t            NID in the list becomes the Primary NID of a newly created\n"
224          "\t            peer. \n"
225          "\t--nid: one or more peer NIDs\n"
226          "\t--non_mr: create this peer as not Multi-Rail capable\n"
227          "\t--ip2nets: specify a range of nids per peer"},
228         {"del", jt_del_peer_nid, 0, "delete a peer NID\n"
229          "\t--prim_nid: Primary NID of the peer.\n"
230          "\t--nid: list of NIDs to remove. If none provided,\n"
231          "\t       peer is deleted\n"
232          "\t--ip2nets: specify a range of nids per peer"},
233         {"show", jt_show_peer, 0, "show peer information\n"
234          "\t--nid: NID of peer to filter on.\n"
235          "\t--verbose: display detailed output per peer."
236                        " Optional argument of '2' outputs more stats\n"},
237         {"list", jt_list_peer, 0, "list all peers\n"},
238         {"set", jt_set_peer_ni_value, 0, "set peer ni specific parameter\n"
239          "\t--nid: Peer NI NID to set the\n"
240          "\t--health: specify health value to set\n"
241          "\t--all: set all peer_nis values to the one specified\n"},
242         { 0, 0, 0, NULL }
243 };
244
245 static inline void print_help(const command_t cmds[], const char *cmd_type,
246                               const char *pc_name)
247 {
248         const command_t *cmd;
249
250         for (cmd = cmds; cmd->pc_name; cmd++) {
251                 if (pc_name != NULL &&
252                     strcmp(cmd->pc_name, pc_name) == 0) {
253                         printf("%s %s: %s\n", cmd_type, cmd->pc_name,
254                                cmd->pc_help);
255                         return;
256                 } else if (pc_name != NULL) {
257                         continue;
258                 }
259                 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
260         }
261 }
262
263 static int parse_long(const char *number, long int *value)
264 {
265         char *end;
266
267         if (!number)
268                 return -1;
269
270         *value = strtol(number,  &end, 0);
271         if (end != NULL && *end != 0)
272                 return -1;
273
274         return 0;
275 }
276
277 static int check_cmd(const command_t *cmd_list, const char *cmd,
278                      const char *sub_cmd, const int min_args,
279                      int argc, char **argv)
280 {
281         int opt;
282         int rc = 0;
283         optind = 0;
284         opterr = 0;
285
286         const char *const short_options = "h";
287         static const struct option long_options[] = {
288                 { .name = "help", .has_arg = no_argument, .val = 'h' },
289                 { .name = NULL }
290         };
291
292         if (argc < min_args) {
293                 print_help(cmd_list, cmd, sub_cmd);
294                 rc = -1;
295                 goto out;
296         } else if (argc > 2) {
297                 return 0;
298         }
299
300         while ((opt = getopt_long(argc, argv, short_options,
301                                   long_options, NULL)) != -1) {
302                 switch (opt) {
303                 case 'h':
304                         print_help(cmd_list, cmd, sub_cmd);
305                         rc = 1;
306                         break;
307                 default:
308                         rc = 0;
309                         break;
310                 }
311         }
312
313 out:
314         opterr = 1;
315         optind = 0;
316         return rc;
317 }
318
319 static int jt_set_max_intf(int argc, char **argv)
320 {
321         long int value;
322         int rc;
323         struct cYAML *err_rc = NULL;
324
325         rc = check_cmd(set_cmds, "set", "max_interfaces", 2, argc, argv);
326         if (rc)
327                 return rc;
328
329         rc = parse_long(argv[1], &value);
330         if (rc != 0) {
331                 cYAML_build_error(-1, -1, "parser", "set",
332                                   "cannot parse max_interfaces value", &err_rc);
333                 cYAML_print_tree2file(stderr, err_rc);
334                 cYAML_free_tree(err_rc);
335                 return -1;
336         }
337
338         rc = lustre_lnet_config_max_intf(value, -1, &err_rc);
339         if (rc != LUSTRE_CFG_RC_NO_ERR)
340                 cYAML_print_tree2file(stderr, err_rc);
341
342         cYAML_free_tree(err_rc);
343
344         return rc;
345 }
346
347 static int jt_set_numa(int argc, char **argv)
348 {
349         long int value;
350         int rc;
351         struct cYAML *err_rc = NULL;
352
353         rc = check_cmd(set_cmds, "set", "numa_range", 2, argc, argv);
354         if (rc)
355                 return rc;
356
357         rc = parse_long(argv[1], &value);
358         if (rc != 0) {
359                 cYAML_build_error(-1, -1, "parser", "set",
360                                   "cannot parse numa_range value", &err_rc);
361                 cYAML_print_tree2file(stderr, err_rc);
362                 cYAML_free_tree(err_rc);
363                 return -1;
364         }
365
366         rc = lustre_lnet_config_numa_range(value, -1, &err_rc);
367         if (rc != LUSTRE_CFG_RC_NO_ERR)
368                 cYAML_print_tree2file(stderr, err_rc);
369
370         cYAML_free_tree(err_rc);
371
372         return rc;
373 }
374
375 static int jt_set_recov_intrv(int argc, char **argv)
376 {
377         long int value;
378         int rc;
379         struct cYAML *err_rc = NULL;
380
381         rc = check_cmd(set_cmds, "set", "recovery_interval", 2, argc, argv);
382         if (rc)
383                 return rc;
384
385         rc = parse_long(argv[1], &value);
386         if (rc != 0) {
387                 cYAML_build_error(-1, -1, "parser", "set",
388                                   "cannot parse recovery interval value", &err_rc);
389                 cYAML_print_tree2file(stderr, err_rc);
390                 cYAML_free_tree(err_rc);
391                 return -1;
392         }
393
394         rc = lustre_lnet_config_recov_intrv(value, -1, &err_rc);
395         if (rc != LUSTRE_CFG_RC_NO_ERR)
396                 cYAML_print_tree2file(stderr, err_rc);
397
398         cYAML_free_tree(err_rc);
399
400         return rc;
401 }
402
403 static int jt_set_rtr_sensitivity(int argc, char **argv)
404 {
405         long int value;
406         int rc;
407         struct cYAML *err_rc = NULL;
408
409         rc = check_cmd(set_cmds, "set", "router_sensitivity", 2, argc, argv);
410         if (rc)
411                 return rc;
412
413         rc = parse_long(argv[1], &value);
414         if (rc != 0) {
415                 cYAML_build_error(-1, -1, "parser", "set",
416                                   "cannot parse router sensitivity value", &err_rc);
417                 cYAML_print_tree2file(stderr, err_rc);
418                 cYAML_free_tree(err_rc);
419                 return -1;
420         }
421
422         rc = lustre_lnet_config_rtr_sensitivity(value, -1, &err_rc);
423         if (rc != LUSTRE_CFG_RC_NO_ERR)
424                 cYAML_print_tree2file(stderr, err_rc);
425
426         cYAML_free_tree(err_rc);
427
428         return rc;
429 }
430
431 static int jt_set_hsensitivity(int argc, char **argv)
432 {
433         long int value;
434         int rc;
435         struct cYAML *err_rc = NULL;
436
437         rc = check_cmd(set_cmds, "set", "health_sensitivity", 2, argc, argv);
438         if (rc)
439                 return rc;
440
441         rc = parse_long(argv[1], &value);
442         if (rc != 0) {
443                 cYAML_build_error(-1, -1, "parser", "set",
444                                   "cannot parse health sensitivity value", &err_rc);
445                 cYAML_print_tree2file(stderr, err_rc);
446                 cYAML_free_tree(err_rc);
447                 return -1;
448         }
449
450         rc = lustre_lnet_config_hsensitivity(value, -1, &err_rc);
451         if (rc != LUSTRE_CFG_RC_NO_ERR)
452                 cYAML_print_tree2file(stderr, err_rc);
453
454         cYAML_free_tree(err_rc);
455
456         return rc;
457 }
458
459 static int jt_set_transaction_to(int argc, char **argv)
460 {
461         long int value;
462         int rc;
463         struct cYAML *err_rc = NULL;
464
465         rc = check_cmd(set_cmds, "set", "transaction_timeout", 2, argc, argv);
466         if (rc)
467                 return rc;
468
469         rc = parse_long(argv[1], &value);
470         if (rc != 0) {
471                 cYAML_build_error(-1, -1, "parser", "set",
472                                   "cannot parse transaction timeout value", &err_rc);
473                 cYAML_print_tree2file(stderr, err_rc);
474                 cYAML_free_tree(err_rc);
475                 return -1;
476         }
477
478         rc = lustre_lnet_config_transaction_to(value, -1, &err_rc);
479         if (rc != LUSTRE_CFG_RC_NO_ERR)
480                 cYAML_print_tree2file(stderr, err_rc);
481
482         cYAML_free_tree(err_rc);
483
484         return rc;
485 }
486
487 static int jt_set_retry_count(int argc, char **argv)
488 {
489         long int value;
490         int rc;
491         struct cYAML *err_rc = NULL;
492
493         rc = check_cmd(set_cmds, "set", "retry_count", 2, argc, argv);
494         if (rc)
495                 return rc;
496
497         rc = parse_long(argv[1], &value);
498         if (rc != 0) {
499                 cYAML_build_error(-1, -1, "parser", "set",
500                                   "cannot parse retry_count value", &err_rc);
501                 cYAML_print_tree2file(stderr, err_rc);
502                 cYAML_free_tree(err_rc);
503                 return -1;
504         }
505
506         rc = lustre_lnet_config_retry_count(value, -1, &err_rc);
507         if (rc != LUSTRE_CFG_RC_NO_ERR)
508                 cYAML_print_tree2file(stderr, err_rc);
509
510         cYAML_free_tree(err_rc);
511
512         return rc;
513 }
514
515 static int jt_set_discovery(int argc, char **argv)
516 {
517         long int value;
518         int rc;
519         struct cYAML *err_rc = NULL;
520
521         rc = check_cmd(set_cmds, "set", "discovery", 2, argc, argv);
522         if (rc)
523                 return rc;
524
525         rc = parse_long(argv[1], &value);
526         if (rc != 0) {
527                 cYAML_build_error(-1, -1, "parser", "set",
528                                   "cannot parse discovery value", &err_rc);
529                 cYAML_print_tree2file(stderr, err_rc);
530                 cYAML_free_tree(err_rc);
531                 return -1;
532         }
533
534         rc = lustre_lnet_config_discovery(value, -1, &err_rc);
535         if (rc != LUSTRE_CFG_RC_NO_ERR)
536                 cYAML_print_tree2file(stderr, err_rc);
537
538         cYAML_free_tree(err_rc);
539
540         return rc;
541 }
542
543 static int jt_set_drop_asym_route(int argc, char **argv)
544 {
545         long int value;
546         int rc;
547         struct cYAML *err_rc = NULL;
548
549         rc = check_cmd(set_cmds, "set", "drop_asym_route", 2, argc, argv);
550         if (rc)
551                 return rc;
552
553         rc = parse_long(argv[1], &value);
554         if (rc != 0) {
555                 cYAML_build_error(-1, -1, "parser", "set",
556                                   "cannot parse drop_asym_route value",
557                                   &err_rc);
558                 cYAML_print_tree2file(stderr, err_rc);
559                 cYAML_free_tree(err_rc);
560                 return -1;
561         }
562
563         rc = lustre_lnet_config_drop_asym_route(value, -1, &err_rc);
564         if (rc != LUSTRE_CFG_RC_NO_ERR)
565                 cYAML_print_tree2file(stderr, err_rc);
566
567         cYAML_free_tree(err_rc);
568
569         return rc;
570 }
571
572 static int jt_set_tiny(int argc, char **argv)
573 {
574         long int value;
575         int rc;
576         struct cYAML *err_rc = NULL;
577
578         rc = check_cmd(set_cmds, "set", "tiny_buffers", 2, argc, argv);
579         if (rc)
580                 return rc;
581
582         rc = parse_long(argv[1], &value);
583         if (rc != 0) {
584                 cYAML_build_error(-1, -1, "parser", "set",
585                                   "cannot parse tiny_buffers value", &err_rc);
586                 cYAML_print_tree2file(stderr, err_rc);
587                 cYAML_free_tree(err_rc);
588                 return -1;
589         }
590
591         rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
592         if (rc != LUSTRE_CFG_RC_NO_ERR)
593                 cYAML_print_tree2file(stderr, err_rc);
594
595         cYAML_free_tree(err_rc);
596
597         return rc;
598 }
599
600 static int jt_set_small(int argc, char **argv)
601 {
602         long int value;
603         int rc;
604         struct cYAML *err_rc = NULL;
605
606         rc = check_cmd(set_cmds, "set", "small_buffers", 2, argc, argv);
607         if (rc)
608                 return rc;
609
610         rc = parse_long(argv[1], &value);
611         if (rc != 0) {
612                 cYAML_build_error(-1, -1, "parser", "set",
613                                   "cannot parse small_buffers value", &err_rc);
614                 cYAML_print_tree2file(stderr, err_rc);
615                 cYAML_free_tree(err_rc);
616                 return -1;
617         }
618
619         rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
620         if (rc != LUSTRE_CFG_RC_NO_ERR)
621                 cYAML_print_tree2file(stderr, err_rc);
622
623         cYAML_free_tree(err_rc);
624
625         return rc;
626 }
627
628 static int jt_set_large(int argc, char **argv)
629 {
630         long int value;
631         int rc;
632         struct cYAML *err_rc = NULL;
633
634         rc = check_cmd(set_cmds, "set", "large_buffers", 2, argc, argv);
635         if (rc)
636                 return rc;
637
638         rc = parse_long(argv[1], &value);
639         if (rc != 0) {
640                 cYAML_build_error(-1, -1, "parser", "set",
641                                   "cannot parse large_buffers value", &err_rc);
642                 cYAML_print_tree2file(stderr, err_rc);
643                 cYAML_free_tree(err_rc);
644                 return -1;
645         }
646
647         rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
648         if (rc != LUSTRE_CFG_RC_NO_ERR)
649                 cYAML_print_tree2file(stderr, err_rc);
650
651         cYAML_free_tree(err_rc);
652
653         return rc;
654 }
655
656 static int jt_set_routing(int argc, char **argv)
657 {
658         long int value;
659         struct cYAML *err_rc = NULL;
660         int rc;
661
662         rc = check_cmd(set_cmds, "set", "routing", 2, argc, argv);
663         if (rc)
664                 return rc;
665
666         rc = parse_long(argv[1], &value);
667         if (rc != 0 || (value != 0 && value != 1)) {
668                 cYAML_build_error(-1, -1, "parser", "set",
669                                   "cannot parse routing value.\n"
670                                   "must be 0 for disable or 1 for enable",
671                                   &err_rc);
672                 cYAML_print_tree2file(stderr, err_rc);
673                 cYAML_free_tree(err_rc);
674                 return -1;
675         }
676
677         rc = lustre_lnet_enable_routing(value, -1, &err_rc);
678
679         if (rc != LUSTRE_CFG_RC_NO_ERR)
680                 cYAML_print_tree2file(stderr, err_rc);
681
682         cYAML_free_tree(err_rc);
683
684         return rc;
685 }
686
687 static int jt_config_lnet(int argc, char **argv)
688 {
689         struct cYAML *err_rc = NULL;
690         bool load_mod_params = false;
691         int rc, opt;
692
693         const char *const short_options = "a";
694         static const struct option long_options[] = {
695                 { .name = "all",  .has_arg = no_argument, .val = 'a' },
696                 { .name = NULL }
697         };
698
699         rc = check_cmd(lnet_cmds, "lnet", "configure", 0, argc, argv);
700         if (rc)
701                 return rc;
702
703         while ((opt = getopt_long(argc, argv, short_options,
704                                    long_options, NULL)) != -1) {
705                 switch (opt) {
706                 case 'a':
707                         load_mod_params = true;
708                         break;
709                 default:
710                         return 0;
711                 }
712         }
713
714         rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
715                                           -1, &err_rc);
716
717         if (rc != LUSTRE_CFG_RC_NO_ERR)
718                 cYAML_print_tree2file(stderr, err_rc);
719
720         cYAML_free_tree(err_rc);
721
722         return rc;
723 }
724
725 static int jt_unconfig_lnet(int argc, char **argv)
726 {
727         struct cYAML *err_rc = NULL;
728         int rc;
729
730         rc = check_cmd(lnet_cmds, "lnet", "unconfigure", 0, argc, argv);
731         if (rc)
732                 return rc;
733
734         rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
735
736         if (rc != LUSTRE_CFG_RC_NO_ERR)
737                 cYAML_print_tree2file(stderr, err_rc);
738
739         cYAML_free_tree(err_rc);
740
741         return rc;
742 }
743 static int jt_add_route(int argc, char **argv)
744 {
745         char *network = NULL, *gateway = NULL;
746         long int hop = -1, prio = -1, sen = -1;
747         struct cYAML *err_rc = NULL;
748         int rc, opt;
749
750         const char *const short_options = "n:g:c:p:";
751         static const struct option long_options[] = {
752         { .name = "net",       .has_arg = required_argument, .val = 'n' },
753         { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
754         { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
755         { .name = "priority",  .has_arg = required_argument, .val = 'p' },
756         { .name = "health_sensitivity",  .has_arg = required_argument, .val = 's' },
757         { .name = NULL } };
758
759         rc = check_cmd(route_cmds, "route", "add", 0, argc, argv);
760         if (rc)
761                 return rc;
762
763         while ((opt = getopt_long(argc, argv, short_options,
764                                    long_options, NULL)) != -1) {
765                 switch (opt) {
766                 case 'n':
767                         network = optarg;
768                         break;
769                 case 'g':
770                         gateway = optarg;
771                         break;
772                 case 'c':
773                         rc = parse_long(optarg, &hop);
774                         if (rc != 0) {
775                                 /* ignore option */
776                                 hop = -1;
777                                 continue;
778                         }
779                         break;
780                 case 'p':
781                         rc = parse_long(optarg, &prio);
782                         if (rc != 0) {
783                                 /* ingore option */
784                                 prio = -1;
785                                 continue;
786                         }
787                         break;
788                 case 's':
789                         rc = parse_long(optarg, &sen);
790                         if (rc != 0) {
791                                 /* ingore option */
792                                 sen = -1;
793                                 continue;
794                         }
795                         break;
796
797                 case '?':
798                         print_help(route_cmds, "route", "add");
799                 default:
800                         return 0;
801                 }
802         }
803
804         rc = lustre_lnet_config_route(network, gateway, hop, prio, sen, -1,
805                                       &err_rc);
806
807         if (rc != LUSTRE_CFG_RC_NO_ERR)
808                 cYAML_print_tree2file(stderr, err_rc);
809
810         cYAML_free_tree(err_rc);
811
812         return rc;
813 }
814
815 static int jt_add_ni(int argc, char **argv)
816 {
817         char *ip2net = NULL;
818         long int pto = -1, pc = -1, pbc = -1, cre = -1;
819         struct cYAML *err_rc = NULL;
820         int rc, opt, cpt_rc = -1;
821         struct lnet_dlc_network_descr nw_descr;
822         struct cfs_expr_list *global_cpts = NULL;
823         struct lnet_ioctl_config_lnd_tunables tunables;
824         bool found = false;
825
826         memset(&tunables, 0, sizeof(tunables));
827         lustre_lnet_init_nw_descr(&nw_descr);
828
829         const char *const short_options = "n:i:p:t:c:b:r:s:";
830         static const struct option long_options[] = {
831         { .name = "net",          .has_arg = required_argument, .val = 'n' },
832         { .name = "if",           .has_arg = required_argument, .val = 'i' },
833         { .name = "ip2net",       .has_arg = required_argument, .val = 'p' },
834         { .name = "peer-timeout", .has_arg = required_argument, .val = 't' },
835         { .name = "peer-credits", .has_arg = required_argument, .val = 'c' },
836         { .name = "peer-buffer-credits",
837                                   .has_arg = required_argument, .val = 'b' },
838         { .name = "credits",      .has_arg = required_argument, .val = 'r' },
839         { .name = "cpt",          .has_arg = required_argument, .val = 's' },
840         { .name = NULL } };
841
842         rc = check_cmd(net_cmds, "net", "add", 0, argc, argv);
843         if (rc)
844                 return rc;
845
846         while ((opt = getopt_long(argc, argv, short_options,
847                                    long_options, NULL)) != -1) {
848                 switch (opt) {
849                 case 'n':
850                         nw_descr.nw_id = libcfs_str2net(optarg);
851                         break;
852                 case 'i':
853                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
854                         if (rc != 0) {
855                                 cYAML_build_error(-1, -1, "ni", "add",
856                                                 "bad interface list",
857                                                 &err_rc);
858                                 goto failed;
859                         }
860                         break;
861                 case 'p':
862                         ip2net = optarg;
863                         break;
864                 case 't':
865                         rc = parse_long(optarg, &pto);
866                         if (rc != 0) {
867                                 /* ignore option */
868                                 pto = -1;
869                                 continue;
870                         }
871                         break;
872                 case 'c':
873                         rc = parse_long(optarg, &pc);
874                         if (rc != 0) {
875                                 /* ignore option */
876                                 pc = -1;
877                                 continue;
878                         }
879                         break;
880                 case 'b':
881                         rc = parse_long(optarg, &pbc);
882                         if (rc != 0) {
883                                 /* ignore option */
884                                 pbc = -1;
885                                 continue;
886                         }
887                         break;
888                 case 'r':
889                         rc = parse_long(optarg, &cre);
890                         if (rc != 0) {
891                                 /* ignore option */
892                                 cre = -1;
893                                 continue;
894                         }
895                         break;
896                 case 's':
897                         cpt_rc = cfs_expr_list_parse(optarg,
898                                                      strlen(optarg), 0,
899                                                      UINT_MAX, &global_cpts);
900                         break;
901                 case '?':
902                         print_help(net_cmds, "net", "add");
903                 default:
904                         return 0;
905                 }
906         }
907
908         if (pto > 0 || pc > 0 || pbc > 0 || cre > 0) {
909                 tunables.lt_cmn.lct_peer_timeout = pto;
910                 tunables.lt_cmn.lct_peer_tx_credits = pc;
911                 tunables.lt_cmn.lct_peer_rtr_credits = pbc;
912                 tunables.lt_cmn.lct_max_tx_credits = cre;
913                 found = true;
914         }
915
916         rc = lustre_lnet_config_ni(&nw_descr,
917                                    (cpt_rc == 0) ? global_cpts: NULL,
918                                    ip2net, (found) ? &tunables : NULL,
919                                    -1, &err_rc);
920
921         if (global_cpts != NULL)
922                 cfs_expr_list_free(global_cpts);
923
924 failed:
925         if (rc != LUSTRE_CFG_RC_NO_ERR)
926                 cYAML_print_tree2file(stderr, err_rc);
927
928         cYAML_free_tree(err_rc);
929
930         return rc;
931 }
932
933 static int jt_del_route(int argc, char **argv)
934 {
935         char *network = NULL, *gateway = NULL;
936         struct cYAML *err_rc = NULL;
937         int rc, opt;
938
939         const char *const short_options = "n:g:";
940         static const struct option long_options[] = {
941                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
942                 { .name = "gateway", .has_arg = required_argument, .val = 'g' },
943                 { .name = NULL } };
944
945         rc = check_cmd(route_cmds, "route", "del", 0, argc, argv);
946         if (rc)
947                 return rc;
948
949         while ((opt = getopt_long(argc, argv, short_options,
950                                    long_options, NULL)) != -1) {
951                 switch (opt) {
952                 case 'n':
953                         network = optarg;
954                         break;
955                 case 'g':
956                         gateway = optarg;
957                         break;
958                 case '?':
959                         print_help(route_cmds, "route", "del");
960                 default:
961                         return 0;
962                 }
963         }
964
965         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
966
967         if (rc != LUSTRE_CFG_RC_NO_ERR)
968                 cYAML_print_tree2file(stderr, err_rc);
969
970         cYAML_free_tree(err_rc);
971
972         return rc;
973 }
974
975 static int jt_del_ni(int argc, char **argv)
976 {
977         struct cYAML *err_rc = NULL;
978         int rc, opt;
979         struct lnet_dlc_network_descr nw_descr;
980
981         lustre_lnet_init_nw_descr(&nw_descr);
982
983         const char *const short_options = "n:i:";
984         static const struct option long_options[] = {
985         { .name = "net",        .has_arg = required_argument,   .val = 'n' },
986         { .name = "if",         .has_arg = required_argument,   .val = 'i' },
987         { .name = NULL } };
988
989         rc = check_cmd(net_cmds, "net", "del", 0, argc, argv);
990         if (rc)
991                 return rc;
992
993         while ((opt = getopt_long(argc, argv, short_options,
994                                    long_options, NULL)) != -1) {
995                 switch (opt) {
996                 case 'n':
997                         nw_descr.nw_id = libcfs_str2net(optarg);
998                         break;
999                 case 'i':
1000                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
1001                         if (rc != 0) {
1002                                 cYAML_build_error(-1, -1, "ni", "add",
1003                                                 "bad interface list",
1004                                                 &err_rc);
1005                                 goto out;
1006                         }
1007                         break;
1008                 case '?':
1009                         print_help(net_cmds, "net", "del");
1010                 default:
1011                         return 0;
1012                 }
1013         }
1014
1015         rc = lustre_lnet_del_ni(&nw_descr, -1, &err_rc);
1016
1017 out:
1018         if (rc != LUSTRE_CFG_RC_NO_ERR)
1019                 cYAML_print_tree2file(stderr, err_rc);
1020
1021         cYAML_free_tree(err_rc);
1022
1023         return rc;
1024 }
1025
1026 static int jt_show_route(int argc, char **argv)
1027 {
1028         char *network = NULL, *gateway = NULL;
1029         long int hop = -1, prio = -1;
1030         int detail = 0, rc, opt;
1031         struct cYAML *err_rc = NULL, *show_rc = NULL;
1032
1033         const char *const short_options = "n:g:h:p:v";
1034         static const struct option long_options[] = {
1035         { .name = "net",       .has_arg = required_argument, .val = 'n' },
1036         { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
1037         { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
1038         { .name = "priority",  .has_arg = required_argument, .val = 'p' },
1039         { .name = "verbose",   .has_arg = no_argument,       .val = 'v' },
1040         { .name = NULL } };
1041
1042         rc = check_cmd(route_cmds, "route", "show", 0, argc, argv);
1043         if (rc)
1044                 return rc;
1045
1046         while ((opt = getopt_long(argc, argv, short_options,
1047                                    long_options, NULL)) != -1) {
1048                 switch (opt) {
1049                 case 'n':
1050                         network = optarg;
1051                         break;
1052                 case 'g':
1053                         gateway = optarg;
1054                         break;
1055                 case 'c':
1056                         rc = parse_long(optarg, &hop);
1057                         if (rc != 0) {
1058                                 /* ignore option */
1059                                 hop = -1;
1060                                 continue;
1061                         }
1062                         break;
1063                 case 'p':
1064                         rc = parse_long(optarg, &prio);
1065                         if (rc != 0) {
1066                                 /* ignore option */
1067                                 prio = -1;
1068                                 continue;
1069                         }
1070                         break;
1071                 case 'v':
1072                         detail = 1;
1073                         break;
1074                 case '?':
1075                         print_help(route_cmds, "route", "show");
1076                 default:
1077                         return 0;
1078                 }
1079         }
1080
1081         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
1082                                     &show_rc, &err_rc, false);
1083
1084         if (rc != LUSTRE_CFG_RC_NO_ERR)
1085                 cYAML_print_tree2file(stderr, err_rc);
1086         else if (show_rc)
1087                 cYAML_print_tree(show_rc);
1088
1089         cYAML_free_tree(err_rc);
1090         cYAML_free_tree(show_rc);
1091
1092         return rc;
1093 }
1094
1095 static int set_value_helper(int argc, char **argv,
1096                             int (*cb)(int, bool, char*, int, struct cYAML**))
1097 {
1098         char *nid = NULL;
1099         long int healthv = -1;
1100         bool all = false;
1101         int rc, opt;
1102         struct cYAML *err_rc = NULL;
1103
1104         const char *const short_options = "t:n:a";
1105         static const struct option long_options[] = {
1106                 { .name = "nid", .has_arg = required_argument, .val = 'n' },
1107                 { .name = "health", .has_arg = required_argument, .val = 't' },
1108                 { .name = "all", .has_arg = no_argument, .val = 'a' },
1109                 { .name = NULL } };
1110
1111         rc = check_cmd(net_cmds, "net", "set", 0, argc, argv);
1112         if (rc)
1113                 return rc;
1114
1115         while ((opt = getopt_long(argc, argv, short_options,
1116                                    long_options, NULL)) != -1) {
1117                 switch (opt) {
1118                 case 'n':
1119                         nid = optarg;
1120                         break;
1121                 case 't':
1122                         if (parse_long(optarg, &healthv) != 0)
1123                                 healthv = -1;
1124                         break;
1125                 case 'a':
1126                         all = true;
1127                         break;
1128                 default:
1129                         return 0;
1130                 }
1131         }
1132
1133         rc = cb(healthv, all, nid, -1, &err_rc);
1134
1135         if (rc != LUSTRE_CFG_RC_NO_ERR)
1136                 cYAML_print_tree2file(stderr, err_rc);
1137
1138         cYAML_free_tree(err_rc);
1139
1140         return rc;
1141 }
1142
1143 static int jt_set_ni_value(int argc, char **argv)
1144 {
1145         return set_value_helper(argc, argv, lustre_lnet_config_ni_healthv);
1146 }
1147
1148 static int jt_set_peer_ni_value(int argc, char **argv)
1149 {
1150         return set_value_helper(argc, argv, lustre_lnet_config_peer_ni_healthv);
1151 }
1152
1153 static int jt_show_recovery(int argc, char **argv)
1154 {
1155         int rc, opt;
1156         struct cYAML *err_rc = NULL, *show_rc = NULL;
1157
1158         const char *const short_options = "lp";
1159         static const struct option long_options[] = {
1160                 { .name = "local", .has_arg = no_argument, .val = 'l' },
1161                 { .name = "peer", .has_arg = no_argument, .val = 'p' },
1162                 { .name = NULL } };
1163
1164         rc = check_cmd(debug_cmds, "recovery", NULL, 0, argc, argv);
1165         if (rc)
1166                 return rc;
1167
1168         while ((opt = getopt_long(argc, argv, short_options,
1169                                    long_options, NULL)) != -1) {
1170                 switch (opt) {
1171                 case 'l':
1172                         rc = lustre_lnet_show_local_ni_recovq(-1, &show_rc, &err_rc);
1173                         break;
1174                 case 'p':
1175                         rc = lustre_lnet_show_peer_ni_recovq(-1, &show_rc, &err_rc);
1176                         break;
1177                 default:
1178                         return 0;
1179                 }
1180         }
1181
1182         if (rc != LUSTRE_CFG_RC_NO_ERR)
1183                 cYAML_print_tree2file(stderr, err_rc);
1184         else if (show_rc)
1185                 cYAML_print_tree(show_rc);
1186
1187         cYAML_free_tree(err_rc);
1188         cYAML_free_tree(show_rc);
1189
1190         return rc;
1191 }
1192
1193 static int jt_show_net(int argc, char **argv)
1194 {
1195         char *network = NULL;
1196         int rc, opt;
1197         struct cYAML *err_rc = NULL, *show_rc = NULL;
1198         long int detail = 0;
1199
1200         const char *const short_options = "n:v";
1201         static const struct option long_options[] = {
1202                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
1203                 { .name = "verbose", .has_arg = optional_argument, .val = 'v' },
1204                 { .name = NULL } };
1205
1206         rc = check_cmd(net_cmds, "net", "show", 0, argc, argv);
1207         if (rc)
1208                 return rc;
1209
1210         while ((opt = getopt_long(argc, argv, short_options,
1211                                    long_options, NULL)) != -1) {
1212                 switch (opt) {
1213                 case 'n':
1214                         network = optarg;
1215                         break;
1216                 case 'v':
1217                         if ((!optarg) && (argv[optind] != NULL) &&
1218                             (argv[optind][0] != '-')) {
1219                                 if (parse_long(argv[optind++], &detail) != 0)
1220                                         detail = 1;
1221                         } else {
1222                                 detail = 1;
1223                         }
1224                         break;
1225                 case '?':
1226                         print_help(net_cmds, "net", "show");
1227                 default:
1228                         return 0;
1229                 }
1230         }
1231
1232         rc = lustre_lnet_show_net(network, (int) detail, -1, &show_rc, &err_rc,
1233                                   false);
1234
1235         if (rc != LUSTRE_CFG_RC_NO_ERR)
1236                 cYAML_print_tree2file(stderr, err_rc);
1237         else if (show_rc)
1238                 cYAML_print_tree(show_rc);
1239
1240         cYAML_free_tree(err_rc);
1241         cYAML_free_tree(show_rc);
1242
1243         return rc;
1244 }
1245
1246 static int jt_show_routing(int argc, char **argv)
1247 {
1248         struct cYAML *err_rc = NULL, *show_rc = NULL;
1249         int rc;
1250
1251         rc = check_cmd(routing_cmds, "routing", "show", 0, argc, argv);
1252         if (rc)
1253                 return rc;
1254
1255         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, false);
1256
1257         if (rc != LUSTRE_CFG_RC_NO_ERR)
1258                 cYAML_print_tree2file(stderr, err_rc);
1259         else if (show_rc)
1260                 cYAML_print_tree(show_rc);
1261
1262         cYAML_free_tree(err_rc);
1263         cYAML_free_tree(show_rc);
1264
1265         return rc;
1266 }
1267
1268 static int jt_show_stats(int argc, char **argv)
1269 {
1270         int rc;
1271         struct cYAML *show_rc = NULL, *err_rc = NULL;
1272
1273         rc = check_cmd(stats_cmds, "stats", "show", 0, argc, argv);
1274         if (rc)
1275                 return rc;
1276
1277         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
1278
1279         if (rc != LUSTRE_CFG_RC_NO_ERR)
1280                 cYAML_print_tree2file(stderr, err_rc);
1281         else if (show_rc)
1282                 cYAML_print_tree(show_rc);
1283
1284         cYAML_free_tree(err_rc);
1285         cYAML_free_tree(show_rc);
1286
1287         return rc;
1288 }
1289
1290 static int jt_show_global(int argc, char **argv)
1291 {
1292         int rc;
1293         struct cYAML *show_rc = NULL, *err_rc = NULL;
1294
1295         rc = check_cmd(global_cmds, "global", "show", 0, argc, argv);
1296         if (rc)
1297                 return rc;
1298
1299         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1300         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1301                 cYAML_print_tree2file(stderr, err_rc);
1302                 goto out;
1303         }
1304
1305         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1306         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1307                 cYAML_print_tree2file(stderr, err_rc);
1308                 goto out;
1309         }
1310
1311         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1312         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1313                 cYAML_print_tree2file(stderr, err_rc);
1314                 goto out;
1315         }
1316
1317         rc = lustre_lnet_show_drop_asym_route(-1, &show_rc, &err_rc);
1318         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1319                 cYAML_print_tree2file(stderr, err_rc);
1320                 goto out;
1321         }
1322
1323         rc = lustre_lnet_show_retry_count(-1, &show_rc, &err_rc);
1324         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1325                 cYAML_print_tree2file(stderr, err_rc);
1326                 goto out;
1327         }
1328
1329         rc = lustre_lnet_show_transaction_to(-1, &show_rc, &err_rc);
1330         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1331                 cYAML_print_tree2file(stderr, err_rc);
1332                 goto out;
1333         }
1334
1335         rc = lustre_lnet_show_hsensitivity(-1, &show_rc, &err_rc);
1336         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1337                 cYAML_print_tree2file(stderr, err_rc);
1338                 goto out;
1339         }
1340
1341         rc = lustre_lnet_show_recov_intrv(-1, &show_rc, &err_rc);
1342         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1343                 cYAML_print_tree2file(stderr, err_rc);
1344                 goto out;
1345         }
1346
1347         rc = lustre_lnet_show_rtr_sensitivity(-1, &show_rc, &err_rc);
1348         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1349                 cYAML_print_tree2file(stderr, err_rc);
1350                 goto out;
1351         }
1352
1353         if (show_rc)
1354                 cYAML_print_tree(show_rc);
1355
1356 out:
1357         cYAML_free_tree(err_rc);
1358         cYAML_free_tree(show_rc);
1359
1360         return rc;
1361 }
1362
1363 static int jt_lnet(int argc, char **argv)
1364 {
1365         int rc;
1366
1367         rc = check_cmd(lnet_cmds, "lnet", NULL, 2, argc, argv);
1368         if (rc)
1369                 return rc;
1370
1371         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
1372 }
1373
1374 static int jt_route(int argc, char **argv)
1375 {
1376         int rc;
1377
1378         rc = check_cmd(route_cmds, "route", NULL, 2, argc, argv);
1379         if (rc)
1380                 return rc;
1381
1382         return Parser_execarg(argc - 1, &argv[1], route_cmds);
1383 }
1384
1385 static int jt_net(int argc, char **argv)
1386 {
1387         int rc;
1388
1389         rc = check_cmd(net_cmds, "net", NULL, 2, argc, argv);
1390         if (rc)
1391                 return rc;
1392
1393         return Parser_execarg(argc - 1, &argv[1], net_cmds);
1394 }
1395
1396 static int jt_routing(int argc, char **argv)
1397 {
1398         int rc;
1399
1400         rc = check_cmd(routing_cmds, "routing", NULL, 2, argc, argv);
1401         if (rc)
1402                 return rc;
1403
1404         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
1405 }
1406
1407 static int jt_stats(int argc, char **argv)
1408 {
1409         int rc;
1410
1411         rc = check_cmd(stats_cmds, "stats", NULL, 2, argc, argv);
1412         if (rc)
1413                 return rc;
1414
1415         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
1416 }
1417
1418 static int jt_debug(int argc, char **argv)
1419 {
1420         int rc;
1421
1422         rc = check_cmd(debug_cmds, "recovery", NULL, 2, argc, argv);
1423         if (rc)
1424                 return rc;
1425
1426         return Parser_execarg(argc - 1, &argv[1], debug_cmds);
1427 }
1428
1429 static int jt_global(int argc, char **argv)
1430 {
1431         int rc;
1432
1433         rc = check_cmd(global_cmds, "global", NULL, 2, argc, argv);
1434         if (rc)
1435                 return rc;
1436
1437         return Parser_execarg(argc - 1, &argv[1], global_cmds);
1438 }
1439
1440 static int jt_peers(int argc, char **argv)
1441 {
1442         int rc;
1443
1444         rc = check_cmd(peer_cmds, "peer", NULL, 2, argc, argv);
1445         if (rc)
1446                 return rc;
1447
1448         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
1449 }
1450
1451 static int jt_set(int argc, char **argv)
1452 {
1453         int rc;
1454
1455         rc = check_cmd(set_cmds, "set", NULL, 2, argc, argv);
1456         if (rc)
1457                 return rc;
1458
1459         return Parser_execarg(argc - 1, &argv[1], set_cmds);
1460 }
1461
1462 static int jt_import(int argc, char **argv)
1463 {
1464         char *file = NULL;
1465         struct cYAML *err_rc = NULL;
1466         struct cYAML *show_rc = NULL;
1467         int rc = 0, return_rc = 0, opt, opt_found = 0;
1468         char cmd = 'a';
1469
1470         const char *const short_options = "adseh";
1471         static const struct option long_options[] = {
1472                 { .name = "add",  .has_arg = no_argument, .val = 'a' },
1473                 { .name = "del",  .has_arg = no_argument, .val = 'd' },
1474                 { .name = "show", .has_arg = no_argument, .val = 's' },
1475                 { .name = "exec", .has_arg = no_argument, .val = 'e' },
1476                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1477                 { .name = NULL } };
1478
1479         while ((opt = getopt_long(argc, argv, short_options,
1480                                    long_options, NULL)) != -1) {
1481                 opt_found = 1;
1482                 switch (opt) {
1483                 case 'a':
1484                         cmd = opt;
1485                         break;
1486                 case 'd':
1487                 case 's':
1488                         cmd = opt;
1489                         break;
1490                 case 'e':
1491                         cmd = opt;
1492                         break;
1493                 case 'h':
1494                         printf("import FILE\n"
1495                                "import < FILE : import a file\n"
1496                                "\t--add: add configuration\n"
1497                                "\t--del: delete configuration\n"
1498                                "\t--show: show configuration\n"
1499                                "\t--exec: execute command\n"
1500                                "\t--help: display this help\n"
1501                                "If no command option is given then --add"
1502                                " is assumed by default\n");
1503                         return 0;
1504                 default:
1505                         return 0;
1506                 }
1507         }
1508
1509         /* grab the file name if one exists */
1510         if (opt_found && argc == 3)
1511                 file = argv[2];
1512         else if (!opt_found && argc == 2)
1513                 file = argv[1];
1514
1515         switch (cmd) {
1516         case 'a':
1517                 rc = lustre_yaml_config(file, &err_rc);
1518                 return_rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1519                 cYAML_print_tree(show_rc);
1520                 cYAML_free_tree(show_rc);
1521                 break;
1522         case 'd':
1523                 rc = lustre_yaml_del(file, &err_rc);
1524                 break;
1525         case 's':
1526                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1527                 cYAML_print_tree(show_rc);
1528                 cYAML_free_tree(show_rc);
1529                 break;
1530         case 'e':
1531                 rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1532                 cYAML_print_tree(show_rc);
1533                 cYAML_free_tree(show_rc);
1534                 break;
1535         }
1536
1537         if (rc || return_rc) {
1538                 cYAML_print_tree2file(stderr, err_rc);
1539                 cYAML_free_tree(err_rc);
1540         }
1541
1542         return rc;
1543 }
1544
1545 static int jt_export(int argc, char **argv)
1546 {
1547         struct cYAML *show_rc = NULL;
1548         struct cYAML *err_rc = NULL;
1549         int rc;
1550         FILE *f = NULL;
1551         int opt;
1552         bool backup = false;
1553         char *file = NULL;
1554
1555         const char *const short_options = "bh";
1556         static const struct option long_options[] = {
1557                 { .name = "backup", .has_arg = no_argument, .val = 'b' },
1558                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1559                 { .name = NULL } };
1560
1561         while ((opt = getopt_long(argc, argv, short_options,
1562                                    long_options, NULL)) != -1) {
1563                 switch (opt) {
1564                 case 'b':
1565                         backup = true;
1566                         break;
1567                 case 'h':
1568                 default:
1569                         printf("export > FILE.yaml : export configuration\n"
1570                                "\t--backup: export only what's necessary for reconfig\n"
1571                                "\t--help: display this help\n");
1572                         return 0;
1573                 }
1574         }
1575
1576         if (backup && argc >= 3)
1577                 file = argv[2];
1578         else if (!backup && argc >= 2)
1579                 file = argv[1];
1580         else
1581                 f = stdout;
1582
1583         if (file) {
1584                 f = fopen(file, "w");
1585                 if (f == NULL)
1586                         return -1;
1587         }
1588
1589         rc = lustre_lnet_show_net(NULL, 2, -1, &show_rc, &err_rc, backup);
1590         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1591                 cYAML_print_tree2file(stderr, err_rc);
1592                 cYAML_free_tree(err_rc);
1593                 err_rc = NULL;
1594         }
1595
1596         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
1597                                     &err_rc, backup);
1598         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1599                 cYAML_print_tree2file(stderr, err_rc);
1600                 cYAML_free_tree(err_rc);
1601                 err_rc = NULL;
1602         }
1603
1604         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, backup);
1605         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1606                 cYAML_print_tree2file(stderr, err_rc);
1607                 cYAML_free_tree(err_rc);
1608                 err_rc = NULL;
1609         }
1610
1611         rc = lustre_lnet_show_peer(NULL, 2, -1, &show_rc, &err_rc, backup);
1612         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1613                 cYAML_print_tree2file(stderr, err_rc);
1614                 cYAML_free_tree(err_rc);
1615                 err_rc = NULL;
1616         }
1617
1618         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1619         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1620                 cYAML_print_tree2file(stderr, err_rc);
1621                 cYAML_free_tree(err_rc);
1622                 err_rc = NULL;
1623         }
1624
1625         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1626         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1627                 cYAML_print_tree2file(stderr, err_rc);
1628                 cYAML_free_tree(err_rc);
1629                 err_rc = NULL;
1630         }
1631
1632         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1633         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1634                 cYAML_print_tree2file(stderr, err_rc);
1635                 cYAML_free_tree(err_rc);
1636                 err_rc = NULL;
1637         }
1638
1639         rc = lustre_lnet_show_drop_asym_route(-1, &show_rc, &err_rc);
1640         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1641                 cYAML_print_tree2file(stderr, err_rc);
1642                 cYAML_free_tree(err_rc);
1643                 err_rc = NULL;
1644         }
1645
1646         if (show_rc != NULL) {
1647                 cYAML_print_tree2file(f, show_rc);
1648                 cYAML_free_tree(show_rc);
1649         }
1650
1651         if (argc >= 2)
1652                 fclose(f);
1653
1654         return 0;
1655 }
1656
1657 static int jt_peer_nid_common(int argc, char **argv, int cmd)
1658 {
1659         int rc = LUSTRE_CFG_RC_NO_ERR, opt, num_nids, num_nidstrs, i;
1660         bool is_mr = true;
1661         char *prim_nid = NULL, *nidstr = NULL;
1662         char err_str[LNET_MAX_STR_LEN] = "Error";
1663         char *nidstrarray[LNET_MAX_STR_LEN];
1664         struct cYAML *err_rc = NULL;
1665         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
1666
1667         const char *const short_opts = "k:mn:";
1668         const struct option long_opts[] = {
1669         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
1670         { .name = "non_mr",     .has_arg = no_argument,         .val = 'm' },
1671         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1672         { .name = NULL } };
1673
1674         rc = check_cmd(peer_cmds, "peer", "add", 2, argc, argv);
1675         if (rc)
1676                 return rc;
1677
1678         while ((opt = getopt_long(argc, argv, short_opts,
1679                                   long_opts, NULL)) != -1) {
1680                 switch (opt) {
1681                 case 'k':
1682                         prim_nid = optarg;
1683                         break;
1684                 case 'n':
1685                         nidstr = optarg;
1686                         break;
1687                 case 'm':
1688                         if (cmd == LNETCTL_DEL_CMD) {
1689                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1690                                 snprintf(err_str, LNET_MAX_STR_LEN,
1691                                          "Unrecognized option '-%c'", opt);
1692                                 goto out;
1693                         }
1694                         is_mr = false;
1695                         break;
1696                 case '?':
1697                         print_help(peer_cmds, "peer",
1698                                    cmd == LNETCTL_ADD_CMD ? "add" : "del");
1699                 default:
1700                         return 0;
1701                 }
1702         }
1703
1704         if (!(nidstr || prim_nid)) {
1705                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1706                 snprintf(err_str, LNET_MAX_STR_LEN,
1707                          "--prim_nid or --nid (or both) must be specified");
1708                 goto out;
1709         }
1710
1711         if (!nidstr) {
1712                 /* We were only provided a primary nid */
1713                 num_nids = 0;
1714                 if (cmd == LNETCTL_ADD_CMD)
1715                         rc = lustre_lnet_config_peer_nidlist(prim_nid,
1716                                                              lnet_nidlist,
1717                                                              num_nids, is_mr,
1718                                                              -1, &err_rc);
1719                 else
1720                         rc = lustre_lnet_del_peer_nidlist(prim_nid,
1721                                                           lnet_nidlist,
1722                                                           num_nids, -1,
1723                                                           &err_rc);
1724
1725                 goto out;
1726         }
1727
1728         rc = tokenize_nidstr(nidstr, &nidstrarray[0], err_str);
1729         if (rc <= 0)
1730                 goto out;
1731
1732         num_nidstrs = rc;
1733
1734         for (i = 0; i < num_nidstrs; i++) {
1735                 rc = lustre_lnet_parse_nidstr(nidstrarray[i], lnet_nidlist,
1736                                               LNET_MAX_NIDS_PER_PEER, err_str);
1737                 if (rc < 0)
1738                         goto out;
1739
1740                 num_nids = rc;
1741
1742                 if (cmd == LNETCTL_ADD_CMD)
1743                         rc = lustre_lnet_config_peer_nidlist(prim_nid,
1744                                                              lnet_nidlist,
1745                                                              num_nids, is_mr,
1746                                                              -1, &err_rc);
1747                 else
1748                         rc = lustre_lnet_del_peer_nidlist(prim_nid,
1749                                                           lnet_nidlist,
1750                                                           num_nids, -1,
1751                                                           &err_rc);
1752         }
1753
1754 out:
1755         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1756                 cYAML_build_error(rc, -1, "peer",
1757                                   cmd == LNETCTL_ADD_CMD ? "add" : "del",
1758                                   err_str, &err_rc);
1759                 cYAML_print_tree2file(stderr, err_rc);
1760         }
1761
1762         cYAML_free_tree(err_rc);
1763
1764         return rc;
1765 }
1766
1767 static int jt_add_peer_nid(int argc, char **argv)
1768 {
1769         return jt_peer_nid_common(argc, argv, LNETCTL_ADD_CMD);
1770 }
1771
1772 static int jt_del_peer_nid(int argc, char **argv)
1773 {
1774         return jt_peer_nid_common(argc, argv, LNETCTL_DEL_CMD);
1775 }
1776
1777 static int jt_show_peer(int argc, char **argv)
1778 {
1779         char *nid = NULL;
1780         int rc, opt;
1781         struct cYAML *err_rc = NULL, *show_rc = NULL;
1782         long int detail = 0;
1783
1784         const char *const short_opts = "hn:v::";
1785         const struct option long_opts[] = {
1786         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1787         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1788         { .name = "verbose",    .has_arg = optional_argument,   .val = 'v' },
1789         { .name = NULL } };
1790
1791         rc = check_cmd(peer_cmds, "peer", "show", 1, argc, argv);
1792         if (rc)
1793                 return rc;
1794
1795         while ((opt = getopt_long(argc, argv, short_opts,
1796                                   long_opts, NULL)) != -1) {
1797                 switch (opt) {
1798                 case 'n':
1799                         nid = optarg;
1800                         break;
1801                 case 'v':
1802                         if ((!optarg) && (argv[optind] != NULL) &&
1803                             (argv[optind][0] != '-')) {
1804                                 if (parse_long(argv[optind++], &detail) != 0)
1805                                         detail = 1;
1806                         } else {
1807                                 detail = 1;
1808                         }
1809                         break;
1810                 case '?':
1811                         print_help(peer_cmds, "peer", "show");
1812                 default:
1813                         return 0;
1814                 }
1815         }
1816
1817         rc = lustre_lnet_show_peer(nid, (int) detail, -1, &show_rc, &err_rc,
1818                                    false);
1819
1820         if (rc != LUSTRE_CFG_RC_NO_ERR)
1821                 cYAML_print_tree2file(stderr, err_rc);
1822         else if (show_rc)
1823                 cYAML_print_tree(show_rc);
1824
1825         cYAML_free_tree(err_rc);
1826         cYAML_free_tree(show_rc);
1827
1828         return rc;
1829 }
1830
1831 static int jt_list_peer(int argc, char **argv)
1832 {
1833         int rc;
1834         struct cYAML *err_rc = NULL, *list_rc = NULL;
1835
1836         rc = check_cmd(peer_cmds, "peer", "list", 0, argc, argv);
1837         if (rc)
1838                 return rc;
1839
1840         rc = lustre_lnet_list_peer(-1, &list_rc, &err_rc);
1841
1842         if (rc != LUSTRE_CFG_RC_NO_ERR)
1843                 cYAML_print_tree2file(stderr, err_rc);
1844         else if (list_rc)
1845                 cYAML_print_tree(list_rc);
1846
1847         cYAML_free_tree(err_rc);
1848         cYAML_free_tree(list_rc);
1849
1850         return rc;
1851 }
1852
1853 static int jt_ping(int argc, char **argv)
1854 {
1855         struct cYAML *err_rc = NULL;
1856         struct cYAML *show_rc = NULL;
1857         int timeout = 1000;
1858         int rc = 0, opt;
1859
1860         const char *const short_options = "ht:";
1861         const struct option long_options[] = {
1862         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1863         { .name = "timeout",    .has_arg = required_argument,   .val = 't' },
1864         { .name = NULL } };
1865
1866         while ((opt = getopt_long(argc, argv, short_options,
1867                                   long_options, NULL)) != -1) {
1868                 switch (opt) {
1869                 case 't':
1870                         timeout = 1000 * atol(optarg);
1871                         break;
1872                 case 'h':
1873                         printf("ping nid[,nid,...]\n"
1874                                "\t --timeout: ping timeout\n"
1875                                "\t --help: display this help\n");
1876                         return 0;
1877                 default:
1878                         return 0;
1879                 }
1880         }
1881
1882         for (; optind < argc; optind++)
1883                 rc = lustre_lnet_ping_nid(argv[optind], timeout, -1, &show_rc, &err_rc);
1884
1885         if (show_rc)
1886                 cYAML_print_tree(show_rc);
1887
1888         if (err_rc)
1889                 cYAML_print_tree2file(stderr, err_rc);
1890
1891         cYAML_free_tree(err_rc);
1892         cYAML_free_tree(show_rc);
1893
1894         return rc;
1895 }
1896
1897 static int jt_discover(int argc, char **argv)
1898 {
1899         struct cYAML *err_rc = NULL;
1900         struct cYAML *show_rc = NULL;
1901         int force = 0;
1902         int rc = 0, opt;
1903
1904         const char *const short_options = "fh";
1905         const struct option long_options[] = {
1906                 { .name = "force",      .has_arg = no_argument, .val = 'f' },
1907                 { .name = "help",       .has_arg = no_argument, .val = 'h' },
1908                 { .name = NULL } };
1909
1910         while ((opt = getopt_long(argc, argv, short_options,
1911                                   long_options, NULL)) != -1) {
1912                 switch (opt) {
1913                 case 'f':
1914                         force = 1;
1915                         break;
1916                 case 'h':
1917                         printf("discover nid[,nid,...]\n"
1918                                "\t --force: force discovery\n"
1919                                "\t --help: display this help\n");
1920                         return 0;
1921                 default:
1922                         return 0;
1923                 }
1924         }
1925
1926         for (; optind < argc; optind++)
1927                 rc = lustre_lnet_discover_nid(argv[optind], force, -1, &show_rc,
1928                                               &err_rc);
1929
1930         if (show_rc)
1931                 cYAML_print_tree(show_rc);
1932
1933         if (err_rc)
1934                 cYAML_print_tree2file(stderr, err_rc);
1935
1936         cYAML_free_tree(err_rc);
1937         cYAML_free_tree(show_rc);
1938
1939         return rc;
1940 }
1941
1942 static int lnetctl_list_commands(int argc, char **argv)
1943 {
1944         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
1945
1946         Parser_list_commands(cmd_list, buffer, sizeof(buffer), NULL, 0, 4);
1947
1948         return 0;
1949 }
1950
1951 int main(int argc, char **argv)
1952 {
1953         int rc = 0;
1954         struct cYAML *err_rc = NULL;
1955
1956         rc = lustre_lnet_config_lib_init();
1957         if (rc < 0) {
1958                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1959                                   "cannot register LNet device", &err_rc);
1960                 cYAML_print_tree2file(stderr, err_rc);
1961                 return rc;
1962         }
1963
1964         Parser_init("lnetctl > ", cmd_list);
1965         if (argc > 1) {
1966                 rc = Parser_execarg(argc - 1, &argv[1], cmd_list);
1967                 goto errorout;
1968         }
1969
1970         Parser_commands();
1971
1972 errorout:
1973         return rc;
1974 }