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