Whamcloud - gitweb
LU-10654 lnet: lnetctl doesn't error out on mistyped options
[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                 case '?':
689                         print_help(route_cmds, "route", "add");
690                 default:
691                         return 0;
692                 }
693         }
694
695         rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
696
697         if (rc != LUSTRE_CFG_RC_NO_ERR)
698                 cYAML_print_tree2file(stderr, err_rc);
699
700         cYAML_free_tree(err_rc);
701
702         return rc;
703 }
704
705 static int jt_add_ni(int argc, char **argv)
706 {
707         char *ip2net = NULL;
708         long int pto = -1, pc = -1, pbc = -1, cre = -1;
709         struct cYAML *err_rc = NULL;
710         int rc, opt, cpt_rc = -1;
711         struct lnet_dlc_network_descr nw_descr;
712         struct cfs_expr_list *global_cpts = NULL;
713         struct lnet_ioctl_config_lnd_tunables tunables;
714         bool found = false;
715
716         memset(&tunables, 0, sizeof(tunables));
717         lustre_lnet_init_nw_descr(&nw_descr);
718
719         const char *const short_options = "n:i:p:t:c:b:r:s:";
720         static const struct option long_options[] = {
721         { .name = "net",          .has_arg = required_argument, .val = 'n' },
722         { .name = "if",           .has_arg = required_argument, .val = 'i' },
723         { .name = "ip2net",       .has_arg = required_argument, .val = 'p' },
724         { .name = "peer-timeout", .has_arg = required_argument, .val = 't' },
725         { .name = "peer-credits", .has_arg = required_argument, .val = 'c' },
726         { .name = "peer-buffer-credits",
727                                   .has_arg = required_argument, .val = 'b' },
728         { .name = "credits",      .has_arg = required_argument, .val = 'r' },
729         { .name = "cpt",          .has_arg = required_argument, .val = 's' },
730         { .name = NULL } };
731
732         rc = check_cmd(net_cmds, "net", "add", 0, argc, argv);
733         if (rc)
734                 return rc;
735
736         while ((opt = getopt_long(argc, argv, short_options,
737                                    long_options, NULL)) != -1) {
738                 switch (opt) {
739                 case 'n':
740                         nw_descr.nw_id = libcfs_str2net(optarg);
741                         break;
742                 case 'i':
743                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
744                         if (rc != 0) {
745                                 cYAML_build_error(-1, -1, "ni", "add",
746                                                 "bad interface list",
747                                                 &err_rc);
748                                 goto failed;
749                         }
750                         break;
751                 case 'p':
752                         ip2net = optarg;
753                         break;
754                 case 't':
755                         rc = parse_long(optarg, &pto);
756                         if (rc != 0) {
757                                 /* ignore option */
758                                 pto = -1;
759                                 continue;
760                         }
761                         break;
762                 case 'c':
763                         rc = parse_long(optarg, &pc);
764                         if (rc != 0) {
765                                 /* ignore option */
766                                 pc = -1;
767                                 continue;
768                         }
769                         break;
770                 case 'b':
771                         rc = parse_long(optarg, &pbc);
772                         if (rc != 0) {
773                                 /* ignore option */
774                                 pbc = -1;
775                                 continue;
776                         }
777                         break;
778                 case 'r':
779                         rc = parse_long(optarg, &cre);
780                         if (rc != 0) {
781                                 /* ignore option */
782                                 cre = -1;
783                                 continue;
784                         }
785                         break;
786                 case 's':
787                         cpt_rc = cfs_expr_list_parse(optarg,
788                                                      strlen(optarg), 0,
789                                                      UINT_MAX, &global_cpts);
790                         break;
791                 case '?':
792                         print_help(net_cmds, "net", "add");
793                 default:
794                         return 0;
795                 }
796         }
797
798         if (pto > 0 || pc > 0 || pbc > 0 || cre > 0) {
799                 tunables.lt_cmn.lct_peer_timeout = pto;
800                 tunables.lt_cmn.lct_peer_tx_credits = pc;
801                 tunables.lt_cmn.lct_peer_rtr_credits = pbc;
802                 tunables.lt_cmn.lct_max_tx_credits = cre;
803                 found = true;
804         }
805
806         rc = lustre_lnet_config_ni(&nw_descr,
807                                    (cpt_rc == 0) ? global_cpts: NULL,
808                                    ip2net, (found) ? &tunables : NULL,
809                                    -1, &err_rc);
810
811         if (global_cpts != NULL)
812                 cfs_expr_list_free(global_cpts);
813
814 failed:
815         if (rc != LUSTRE_CFG_RC_NO_ERR)
816                 cYAML_print_tree2file(stderr, err_rc);
817
818         cYAML_free_tree(err_rc);
819
820         return rc;
821 }
822
823 static int jt_del_route(int argc, char **argv)
824 {
825         char *network = NULL, *gateway = NULL;
826         struct cYAML *err_rc = NULL;
827         int rc, opt;
828
829         const char *const short_options = "n:g:";
830         static const struct option long_options[] = {
831                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
832                 { .name = "gateway", .has_arg = required_argument, .val = 'g' },
833                 { .name = NULL } };
834
835         rc = check_cmd(route_cmds, "route", "del", 0, argc, argv);
836         if (rc)
837                 return rc;
838
839         while ((opt = getopt_long(argc, argv, short_options,
840                                    long_options, NULL)) != -1) {
841                 switch (opt) {
842                 case 'n':
843                         network = optarg;
844                         break;
845                 case 'g':
846                         gateway = optarg;
847                         break;
848                 case '?':
849                         print_help(route_cmds, "route", "del");
850                 default:
851                         return 0;
852                 }
853         }
854
855         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
856
857         if (rc != LUSTRE_CFG_RC_NO_ERR)
858                 cYAML_print_tree2file(stderr, err_rc);
859
860         cYAML_free_tree(err_rc);
861
862         return rc;
863 }
864
865 static int jt_del_ni(int argc, char **argv)
866 {
867         struct cYAML *err_rc = NULL;
868         int rc, opt;
869         struct lnet_dlc_network_descr nw_descr;
870
871         lustre_lnet_init_nw_descr(&nw_descr);
872
873         const char *const short_options = "n:i:";
874         static const struct option long_options[] = {
875         { .name = "net",        .has_arg = required_argument,   .val = 'n' },
876         { .name = "if",         .has_arg = required_argument,   .val = 'i' },
877         { .name = NULL } };
878
879         rc = check_cmd(net_cmds, "net", "del", 0, argc, argv);
880         if (rc)
881                 return rc;
882
883         while ((opt = getopt_long(argc, argv, short_options,
884                                    long_options, NULL)) != -1) {
885                 switch (opt) {
886                 case 'n':
887                         nw_descr.nw_id = libcfs_str2net(optarg);
888                         break;
889                 case 'i':
890                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
891                         if (rc != 0) {
892                                 cYAML_build_error(-1, -1, "ni", "add",
893                                                 "bad interface list",
894                                                 &err_rc);
895                                 goto out;
896                         }
897                         break;
898                 case '?':
899                         print_help(net_cmds, "net", "del");
900                 default:
901                         return 0;
902                 }
903         }
904
905         rc = lustre_lnet_del_ni(&nw_descr, -1, &err_rc);
906
907 out:
908         if (rc != LUSTRE_CFG_RC_NO_ERR)
909                 cYAML_print_tree2file(stderr, err_rc);
910
911         cYAML_free_tree(err_rc);
912
913         return rc;
914 }
915
916 static int jt_show_route(int argc, char **argv)
917 {
918         char *network = NULL, *gateway = NULL;
919         long int hop = -1, prio = -1;
920         int detail = 0, rc, opt;
921         struct cYAML *err_rc = NULL, *show_rc = NULL;
922
923         const char *const short_options = "n:g:h:p:v";
924         static const struct option long_options[] = {
925         { .name = "net",       .has_arg = required_argument, .val = 'n' },
926         { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
927         { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
928         { .name = "priority",  .has_arg = required_argument, .val = 'p' },
929         { .name = "verbose",   .has_arg = no_argument,       .val = 'v' },
930         { .name = NULL } };
931
932         rc = check_cmd(route_cmds, "route", "show", 0, argc, argv);
933         if (rc)
934                 return rc;
935
936         while ((opt = getopt_long(argc, argv, short_options,
937                                    long_options, NULL)) != -1) {
938                 switch (opt) {
939                 case 'n':
940                         network = optarg;
941                         break;
942                 case 'g':
943                         gateway = optarg;
944                         break;
945                 case 'c':
946                         rc = parse_long(optarg, &hop);
947                         if (rc != 0) {
948                                 /* ignore option */
949                                 hop = -1;
950                                 continue;
951                         }
952                         break;
953                 case 'p':
954                         rc = parse_long(optarg, &prio);
955                         if (rc != 0) {
956                                 /* ignore option */
957                                 prio = -1;
958                                 continue;
959                         }
960                         break;
961                 case 'v':
962                         detail = 1;
963                         break;
964                 case '?':
965                         print_help(route_cmds, "route", "show");
966                 default:
967                         return 0;
968                 }
969         }
970
971         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
972                                     &show_rc, &err_rc, false);
973
974         if (rc != LUSTRE_CFG_RC_NO_ERR)
975                 cYAML_print_tree2file(stderr, err_rc);
976         else if (show_rc)
977                 cYAML_print_tree(show_rc);
978
979         cYAML_free_tree(err_rc);
980         cYAML_free_tree(show_rc);
981
982         return rc;
983 }
984
985 static int set_value_helper(int argc, char **argv,
986                             int (*cb)(int, bool, char*, int, struct cYAML**))
987 {
988         char *nid = NULL;
989         long int healthv = -1;
990         bool all = false;
991         int rc, opt;
992         struct cYAML *err_rc = NULL;
993
994         const char *const short_options = "h:n:a";
995         static const struct option long_options[] = {
996                 { .name = "nid", .has_arg = required_argument, .val = 'n' },
997                 { .name = "health", .has_arg = required_argument, .val = 'h' },
998                 { .name = "all", .has_arg = no_argument, .val = 'a' },
999                 { .name = NULL } };
1000
1001         rc = check_cmd(net_cmds, "net", "set", 0, argc, argv);
1002         if (rc)
1003                 return rc;
1004
1005         while ((opt = getopt_long(argc, argv, short_options,
1006                                    long_options, NULL)) != -1) {
1007                 switch (opt) {
1008                 case 'n':
1009                         nid = optarg;
1010                         break;
1011                 case 'h':
1012                         if (parse_long(argv[optind++], &healthv) != 0)
1013                                 healthv = -1;
1014                         break;
1015                 case 'a':
1016                         all = true;
1017                 default:
1018                         return 0;
1019                 }
1020         }
1021
1022         rc = cb(healthv, all, nid, -1, &err_rc);
1023
1024         if (rc != LUSTRE_CFG_RC_NO_ERR)
1025                 cYAML_print_tree2file(stderr, err_rc);
1026
1027         cYAML_free_tree(err_rc);
1028
1029         return rc;
1030 }
1031
1032 static int jt_set_ni_value(int argc, char **argv)
1033 {
1034         return set_value_helper(argc, argv, lustre_lnet_config_ni_healthv);
1035 }
1036
1037 static int jt_set_peer_ni_value(int argc, char **argv)
1038 {
1039         return set_value_helper(argc, argv, lustre_lnet_config_peer_ni_healthv);
1040 }
1041
1042 static int jt_show_recovery(int argc, char **argv)
1043 {
1044         int rc, opt;
1045         struct cYAML *err_rc = NULL, *show_rc = NULL;
1046
1047         const char *const short_options = "lp";
1048         static const struct option long_options[] = {
1049                 { .name = "local", .has_arg = no_argument, .val = 'l' },
1050                 { .name = "peer", .has_arg = no_argument, .val = 'p' },
1051                 { .name = NULL } };
1052
1053         rc = check_cmd(debug_cmds, "recovery", NULL, 0, argc, argv);
1054         if (rc)
1055                 return rc;
1056
1057         while ((opt = getopt_long(argc, argv, short_options,
1058                                    long_options, NULL)) != -1) {
1059                 switch (opt) {
1060                 case 'l':
1061                         rc = lustre_lnet_show_local_ni_recovq(-1, &show_rc, &err_rc);
1062                         break;
1063                 case 'p':
1064                         rc = lustre_lnet_show_peer_ni_recovq(-1, &show_rc, &err_rc);
1065                         break;
1066                 default:
1067                         return 0;
1068                 }
1069         }
1070
1071         if (rc != LUSTRE_CFG_RC_NO_ERR)
1072                 cYAML_print_tree2file(stderr, err_rc);
1073         else if (show_rc)
1074                 cYAML_print_tree(show_rc);
1075
1076         cYAML_free_tree(err_rc);
1077         cYAML_free_tree(show_rc);
1078
1079         return rc;
1080 }
1081
1082 static int jt_show_net(int argc, char **argv)
1083 {
1084         char *network = NULL;
1085         int rc, opt;
1086         struct cYAML *err_rc = NULL, *show_rc = NULL;
1087         long int detail = 0;
1088
1089         const char *const short_options = "n:v";
1090         static const struct option long_options[] = {
1091                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
1092                 { .name = "verbose", .has_arg = optional_argument, .val = 'v' },
1093                 { .name = NULL } };
1094
1095         rc = check_cmd(net_cmds, "net", "show", 0, argc, argv);
1096         if (rc)
1097                 return rc;
1098
1099         while ((opt = getopt_long(argc, argv, short_options,
1100                                    long_options, NULL)) != -1) {
1101                 switch (opt) {
1102                 case 'n':
1103                         network = optarg;
1104                         break;
1105                 case 'v':
1106                         if ((!optarg) && (argv[optind] != NULL) &&
1107                             (argv[optind][0] != '-')) {
1108                                 if (parse_long(argv[optind++], &detail) != 0)
1109                                         detail = 1;
1110                         } else {
1111                                 detail = 1;
1112                         }
1113                         break;
1114                 case '?':
1115                         print_help(net_cmds, "net", "show");
1116                 default:
1117                         return 0;
1118                 }
1119         }
1120
1121         rc = lustre_lnet_show_net(network, (int) detail, -1, &show_rc, &err_rc,
1122                                   false);
1123
1124         if (rc != LUSTRE_CFG_RC_NO_ERR)
1125                 cYAML_print_tree2file(stderr, err_rc);
1126         else if (show_rc)
1127                 cYAML_print_tree(show_rc);
1128
1129         cYAML_free_tree(err_rc);
1130         cYAML_free_tree(show_rc);
1131
1132         return rc;
1133 }
1134
1135 static int jt_show_routing(int argc, char **argv)
1136 {
1137         struct cYAML *err_rc = NULL, *show_rc = NULL;
1138         int rc;
1139
1140         rc = check_cmd(routing_cmds, "routing", "show", 0, argc, argv);
1141         if (rc)
1142                 return rc;
1143
1144         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, false);
1145
1146         if (rc != LUSTRE_CFG_RC_NO_ERR)
1147                 cYAML_print_tree2file(stderr, err_rc);
1148         else if (show_rc)
1149                 cYAML_print_tree(show_rc);
1150
1151         cYAML_free_tree(err_rc);
1152         cYAML_free_tree(show_rc);
1153
1154         return rc;
1155 }
1156
1157 static int jt_show_stats(int argc, char **argv)
1158 {
1159         int rc;
1160         struct cYAML *show_rc = NULL, *err_rc = NULL;
1161
1162         rc = check_cmd(stats_cmds, "stats", "show", 0, argc, argv);
1163         if (rc)
1164                 return rc;
1165
1166         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
1167
1168         if (rc != LUSTRE_CFG_RC_NO_ERR)
1169                 cYAML_print_tree2file(stderr, err_rc);
1170         else if (show_rc)
1171                 cYAML_print_tree(show_rc);
1172
1173         cYAML_free_tree(err_rc);
1174         cYAML_free_tree(show_rc);
1175
1176         return rc;
1177 }
1178
1179 static int jt_show_global(int argc, char **argv)
1180 {
1181         int rc;
1182         struct cYAML *show_rc = NULL, *err_rc = NULL;
1183
1184         rc = check_cmd(global_cmds, "global", "show", 0, argc, argv);
1185         if (rc)
1186                 return rc;
1187
1188         rc = lustre_lnet_show_numa_range(-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_max_intf(-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_discovery(-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_retry_count(-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         rc = lustre_lnet_show_transaction_to(-1, &show_rc, &err_rc);
1213         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1214                 cYAML_print_tree2file(stderr, err_rc);
1215                 goto out;
1216         }
1217
1218         rc = lustre_lnet_show_hsensitivity(-1, &show_rc, &err_rc);
1219         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1220                 cYAML_print_tree2file(stderr, err_rc);
1221                 goto out;
1222         }
1223
1224         if (show_rc)
1225                 cYAML_print_tree(show_rc);
1226
1227 out:
1228         cYAML_free_tree(err_rc);
1229         cYAML_free_tree(show_rc);
1230
1231         return rc;
1232 }
1233
1234 static int jt_lnet(int argc, char **argv)
1235 {
1236         int rc;
1237
1238         rc = check_cmd(lnet_cmds, "lnet", NULL, 2, argc, argv);
1239         if (rc)
1240                 return rc;
1241
1242         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
1243 }
1244
1245 static int jt_route(int argc, char **argv)
1246 {
1247         int rc;
1248
1249         rc = check_cmd(route_cmds, "route", NULL, 2, argc, argv);
1250         if (rc)
1251                 return rc;
1252
1253         return Parser_execarg(argc - 1, &argv[1], route_cmds);
1254 }
1255
1256 static int jt_net(int argc, char **argv)
1257 {
1258         int rc;
1259
1260         rc = check_cmd(net_cmds, "net", NULL, 2, argc, argv);
1261         if (rc)
1262                 return rc;
1263
1264         return Parser_execarg(argc - 1, &argv[1], net_cmds);
1265 }
1266
1267 static int jt_routing(int argc, char **argv)
1268 {
1269         int rc;
1270
1271         rc = check_cmd(routing_cmds, "routing", NULL, 2, argc, argv);
1272         if (rc)
1273                 return rc;
1274
1275         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
1276 }
1277
1278 static int jt_stats(int argc, char **argv)
1279 {
1280         int rc;
1281
1282         rc = check_cmd(stats_cmds, "stats", NULL, 2, argc, argv);
1283         if (rc)
1284                 return rc;
1285
1286         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
1287 }
1288
1289 static int jt_debug(int argc, char **argv)
1290 {
1291         int rc;
1292
1293         rc = check_cmd(debug_cmds, "recovery", NULL, 2, argc, argv);
1294         if (rc)
1295                 return rc;
1296
1297         return Parser_execarg(argc - 1, &argv[1], debug_cmds);
1298 }
1299
1300 static int jt_global(int argc, char **argv)
1301 {
1302         int rc;
1303
1304         rc = check_cmd(global_cmds, "global", NULL, 2, argc, argv);
1305         if (rc)
1306                 return rc;
1307
1308         return Parser_execarg(argc - 1, &argv[1], global_cmds);
1309 }
1310
1311 static int jt_peers(int argc, char **argv)
1312 {
1313         int rc;
1314
1315         rc = check_cmd(peer_cmds, "peer", NULL, 2, argc, argv);
1316         if (rc)
1317                 return rc;
1318
1319         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
1320 }
1321
1322 static int jt_set(int argc, char **argv)
1323 {
1324         int rc;
1325
1326         rc = check_cmd(set_cmds, "set", NULL, 2, argc, argv);
1327         if (rc)
1328                 return rc;
1329
1330         return Parser_execarg(argc - 1, &argv[1], set_cmds);
1331 }
1332
1333 static int jt_import(int argc, char **argv)
1334 {
1335         char *file = NULL;
1336         struct cYAML *err_rc = NULL;
1337         struct cYAML *show_rc = NULL;
1338         int rc = 0, return_rc = 0, opt, opt_found = 0;
1339         char cmd = 'a';
1340
1341         const char *const short_options = "adseh";
1342         static const struct option long_options[] = {
1343                 { .name = "add",  .has_arg = no_argument, .val = 'a' },
1344                 { .name = "del",  .has_arg = no_argument, .val = 'd' },
1345                 { .name = "show", .has_arg = no_argument, .val = 's' },
1346                 { .name = "exec", .has_arg = no_argument, .val = 'e' },
1347                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1348                 { .name = NULL } };
1349
1350         while ((opt = getopt_long(argc, argv, short_options,
1351                                    long_options, NULL)) != -1) {
1352                 opt_found = 1;
1353                 switch (opt) {
1354                 case 'a':
1355                         cmd = opt;
1356                         break;
1357                 case 'd':
1358                 case 's':
1359                         cmd = opt;
1360                         break;
1361                 case 'e':
1362                         cmd = opt;
1363                         break;
1364                 case 'h':
1365                         printf("import FILE\n"
1366                                "import < FILE : import a file\n"
1367                                "\t--add: add configuration\n"
1368                                "\t--del: delete configuration\n"
1369                                "\t--show: show configuration\n"
1370                                "\t--exec: execute command\n"
1371                                "\t--help: display this help\n"
1372                                "If no command option is given then --add"
1373                                " is assumed by default\n");
1374                         return 0;
1375                 default:
1376                         return 0;
1377                 }
1378         }
1379
1380         /* grab the file name if one exists */
1381         if (opt_found && argc == 3)
1382                 file = argv[2];
1383         else if (!opt_found && argc == 2)
1384                 file = argv[1];
1385
1386         switch (cmd) {
1387         case 'a':
1388                 rc = lustre_yaml_config(file, &err_rc);
1389                 return_rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1390                 cYAML_print_tree(show_rc);
1391                 cYAML_free_tree(show_rc);
1392                 break;
1393         case 'd':
1394                 rc = lustre_yaml_del(file, &err_rc);
1395                 break;
1396         case 's':
1397                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1398                 cYAML_print_tree(show_rc);
1399                 cYAML_free_tree(show_rc);
1400                 break;
1401         case 'e':
1402                 rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1403                 cYAML_print_tree(show_rc);
1404                 cYAML_free_tree(show_rc);
1405                 break;
1406         }
1407
1408         if (rc || return_rc) {
1409                 cYAML_print_tree2file(stderr, err_rc);
1410                 cYAML_free_tree(err_rc);
1411         }
1412
1413         return rc;
1414 }
1415
1416 static int jt_export(int argc, char **argv)
1417 {
1418         struct cYAML *show_rc = NULL;
1419         struct cYAML *err_rc = NULL;
1420         int rc;
1421         FILE *f = NULL;
1422         int opt;
1423         bool backup = false;
1424         char *file = NULL;
1425
1426         const char *const short_options = "bh";
1427         static const struct option long_options[] = {
1428                 { .name = "backup", .has_arg = no_argument, .val = 'b' },
1429                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1430                 { .name = NULL } };
1431
1432         while ((opt = getopt_long(argc, argv, short_options,
1433                                    long_options, NULL)) != -1) {
1434                 switch (opt) {
1435                 case 'b':
1436                         backup = true;
1437                         break;
1438                 case 'h':
1439                 default:
1440                         printf("export > FILE.yaml : export configuration\n"
1441                                "\t--backup: export only what's necessary for reconfig\n"
1442                                "\t--help: display this help\n");
1443                         return 0;
1444                 }
1445         }
1446
1447         if (backup && argc >= 3)
1448                 file = argv[2];
1449         else if (!backup && argc >= 2)
1450                 file = argv[1];
1451         else
1452                 f = stdout;
1453
1454         if (file) {
1455                 f = fopen(file, "w");
1456                 if (f == NULL)
1457                         return -1;
1458         }
1459
1460         rc = lustre_lnet_show_net(NULL, 2, -1, &show_rc, &err_rc, backup);
1461         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1462                 cYAML_print_tree2file(stderr, err_rc);
1463                 cYAML_free_tree(err_rc);
1464                 err_rc = NULL;
1465         }
1466
1467         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
1468                                     &err_rc, backup);
1469         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1470                 cYAML_print_tree2file(stderr, err_rc);
1471                 cYAML_free_tree(err_rc);
1472                 err_rc = NULL;
1473         }
1474
1475         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, backup);
1476         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1477                 cYAML_print_tree2file(stderr, err_rc);
1478                 cYAML_free_tree(err_rc);
1479                 err_rc = NULL;
1480         }
1481
1482         rc = lustre_lnet_show_peer(NULL, 2, -1, &show_rc, &err_rc, backup);
1483         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1484                 cYAML_print_tree2file(stderr, err_rc);
1485                 cYAML_free_tree(err_rc);
1486                 err_rc = NULL;
1487         }
1488
1489         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1490         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1491                 cYAML_print_tree2file(stderr, err_rc);
1492                 cYAML_free_tree(err_rc);
1493                 err_rc = NULL;
1494         }
1495
1496         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1497         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1498                 cYAML_print_tree2file(stderr, err_rc);
1499                 cYAML_free_tree(err_rc);
1500                 err_rc = NULL;
1501         }
1502
1503         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1504         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1505                 cYAML_print_tree2file(stderr, err_rc);
1506                 cYAML_free_tree(err_rc);
1507                 err_rc = NULL;
1508         }
1509
1510         if (show_rc != NULL) {
1511                 cYAML_print_tree2file(f, show_rc);
1512                 cYAML_free_tree(show_rc);
1513         }
1514
1515         if (argc >= 2)
1516                 fclose(f);
1517
1518         return 0;
1519 }
1520
1521 static int jt_add_peer_nid(int argc, char **argv)
1522 {
1523         char *prim_nid = NULL;
1524         char **nids = NULL, **nids2 = NULL;
1525         int size = 0;
1526         struct cYAML *err_rc = NULL;
1527         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i;
1528         bool non_mr = false;
1529         bool ip2nets = false, nid_list = false, prim_nid_present = false;
1530
1531         const char *const short_opts = "k:mn:";
1532         const struct option long_opts[] = {
1533         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
1534         { .name = "non_mr",     .has_arg = no_argument,         .val = 'm' },
1535         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1536         { .name = "ip2nets",    .has_arg = required_argument,   .val = 'i' },
1537         { .name = NULL } };
1538
1539         rc = check_cmd(peer_cmds, "peer", "add", 2, argc, argv);
1540         if (rc)
1541                 return rc;
1542
1543         while ((opt = getopt_long(argc, argv, short_opts,
1544                                   long_opts, NULL)) != -1) {
1545                 switch (opt) {
1546                 case 'k':
1547                         prim_nid_present = true;
1548                         if (ip2nets) {
1549                                 cYAML_build_error(-1, -1, "peer", "add",
1550                                                 "ip2nets can not be specified"
1551                                                 " along side prim_nid parameter.",
1552                                                 &err_rc);
1553                                 goto failed;
1554                         }
1555                         prim_nid = optarg;
1556                         break;
1557                 case 'i':
1558                 case 'n':
1559                         if (opt == 'i')
1560                                 ip2nets = true;
1561
1562                         if (opt == 'n')
1563                                 nid_list = true;
1564
1565                         if (ip2nets && (nid_list || prim_nid_present)) {
1566                                 cYAML_build_error(-1, -1, "peer", "add",
1567                                                 "ip2nets can not be specified"
1568                                                 " along side nid or prim_nid"
1569                                                 " parameters", &err_rc);
1570                                 goto failed;
1571                         }
1572
1573                         size = lustre_lnet_parse_nids(optarg, nids, size,
1574                                                       &nids2);
1575                         if (nids2 == NULL)
1576                                 goto failed;
1577                         nids = nids2;
1578                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1579                         break;
1580                 case 'm':
1581                         non_mr = true;
1582                         break;
1583                 case '?':
1584                         print_help(peer_cmds, "peer", "add");
1585                 default:
1586                         return 0;
1587                 }
1588         }
1589
1590         for (; optind < argc; optind++) {
1591                 size = lustre_lnet_parse_nids(argv[optind], nids, size,
1592                                                 &nids2);
1593                 if (nids2 == NULL)
1594                         goto failed;
1595                 nids = nids2;
1596         }
1597
1598         rc = lustre_lnet_config_peer_nid(prim_nid, nids, size,
1599                                          !non_mr, ip2nets, -1, &err_rc);
1600
1601 failed:
1602         if (nids) {
1603                 /* free the array of nids */
1604                 for (i = 0; i < size; i++)
1605                         free(nids[i]);
1606                 free(nids);
1607         }
1608
1609         if (rc != LUSTRE_CFG_RC_NO_ERR)
1610                 cYAML_print_tree2file(stderr, err_rc);
1611
1612         cYAML_free_tree(err_rc);
1613
1614         return rc;
1615 }
1616
1617 static int jt_del_peer_nid(int argc, char **argv)
1618 {
1619         char *prim_nid = NULL;
1620         char **nids = NULL, **nids2 = NULL;
1621         struct cYAML *err_rc = NULL;
1622         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i, size = 0;
1623         bool ip2nets = false, nid_list = false, prim_nid_present = false;
1624
1625         const char *const short_opts = "k:n:";
1626         const struct option long_opts[] = {
1627         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
1628         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1629         { .name = "ip2nets",    .has_arg = required_argument,   .val = 'i' },
1630         { .name = NULL } };
1631
1632         rc = check_cmd(peer_cmds, "peer", "del", 2, argc, argv);
1633         if (rc)
1634                 return rc;
1635
1636         while ((opt = getopt_long(argc, argv, short_opts,
1637                                   long_opts, NULL)) != -1) {
1638                 switch (opt) {
1639                 case 'k':
1640                         prim_nid_present = true;
1641                         if (ip2nets) {
1642                                 cYAML_build_error(-1, -1, "peer", "add",
1643                                                 "ip2nets can not be specified"
1644                                                 " along side prim_nid parameter.",
1645                                                 &err_rc);
1646                                 goto failed;
1647                         }
1648                         prim_nid = optarg;
1649                         break;
1650                 case 'i':
1651                 case 'n':
1652                         if (opt == 'i')
1653                                 ip2nets = true;
1654
1655                         if (opt == 'n')
1656                                 nid_list = true;
1657
1658                         if (ip2nets && (nid_list || prim_nid_present)) {
1659                                 cYAML_build_error(-1, -1, "peer", "add",
1660                                                 "ip2nets can not be specified"
1661                                                 " along side nid or prim_nid"
1662                                                 " parameters", &err_rc);
1663                                 goto failed;
1664                         }
1665                         size = lustre_lnet_parse_nids(optarg, nids, size,
1666                                                       &nids2);
1667                         if (nids2 == NULL)
1668                                 goto failed;
1669                         nids = nids2;
1670                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1671                         break;
1672                 case '?':
1673                         print_help(peer_cmds, "peer", "del");
1674                 default:
1675                         return 0;
1676                 }
1677         }
1678
1679         for (; optind < argc; optind++) {
1680                 size = lustre_lnet_parse_nids(argv[optind], nids, size,
1681                                                 &nids2);
1682                 if (nids2 == NULL)
1683                         goto failed;
1684                 nids = nids2;
1685         }
1686
1687         rc = lustre_lnet_del_peer_nid(prim_nid, nids, size, ip2nets, -1, &err_rc);
1688
1689 failed:
1690         if (nids) {
1691                 for (i = 0; i < size; i++)
1692                         free(nids[i]);
1693                 free(nids);
1694         }
1695
1696         if (rc != LUSTRE_CFG_RC_NO_ERR)
1697                 cYAML_print_tree2file(stderr, err_rc);
1698
1699         cYAML_free_tree(err_rc);
1700
1701         return rc;
1702 }
1703
1704 static int jt_show_peer(int argc, char **argv)
1705 {
1706         char *nid = NULL;
1707         int rc, opt;
1708         struct cYAML *err_rc = NULL, *show_rc = NULL;
1709         long int detail = 0;
1710
1711         const char *const short_opts = "hn:v::";
1712         const struct option long_opts[] = {
1713         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1714         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1715         { .name = "verbose",    .has_arg = optional_argument,   .val = 'v' },
1716         { .name = NULL } };
1717
1718         rc = check_cmd(peer_cmds, "peer", "show", 1, argc, argv);
1719         if (rc)
1720                 return rc;
1721
1722         while ((opt = getopt_long(argc, argv, short_opts,
1723                                   long_opts, NULL)) != -1) {
1724                 switch (opt) {
1725                 case 'n':
1726                         nid = optarg;
1727                         break;
1728                 case 'v':
1729                         if ((!optarg) && (argv[optind] != NULL) &&
1730                             (argv[optind][0] != '-')) {
1731                                 if (parse_long(argv[optind++], &detail) != 0)
1732                                         detail = 1;
1733                         } else {
1734                                 detail = 1;
1735                         }
1736                         break;
1737                 case '?':
1738                         print_help(peer_cmds, "peer", "show");
1739                 default:
1740                         return 0;
1741                 }
1742         }
1743
1744         rc = lustre_lnet_show_peer(nid, (int) detail, -1, &show_rc, &err_rc,
1745                                    false);
1746
1747         if (rc != LUSTRE_CFG_RC_NO_ERR)
1748                 cYAML_print_tree2file(stderr, err_rc);
1749         else if (show_rc)
1750                 cYAML_print_tree(show_rc);
1751
1752         cYAML_free_tree(err_rc);
1753         cYAML_free_tree(show_rc);
1754
1755         return rc;
1756 }
1757
1758 static int jt_list_peer(int argc, char **argv)
1759 {
1760         int rc;
1761         struct cYAML *err_rc = NULL, *list_rc = NULL;
1762
1763         rc = check_cmd(peer_cmds, "peer", "list", 0, argc, argv);
1764         if (rc)
1765                 return rc;
1766
1767         rc = lustre_lnet_list_peer(-1, &list_rc, &err_rc);
1768
1769         if (rc != LUSTRE_CFG_RC_NO_ERR)
1770                 cYAML_print_tree2file(stderr, err_rc);
1771         else if (list_rc)
1772                 cYAML_print_tree(list_rc);
1773
1774         cYAML_free_tree(err_rc);
1775         cYAML_free_tree(list_rc);
1776
1777         return rc;
1778 }
1779
1780 static int jt_ping(int argc, char **argv)
1781 {
1782         struct cYAML *err_rc = NULL;
1783         struct cYAML *show_rc = NULL;
1784         int timeout = 1000;
1785         int rc = 0, opt;
1786
1787         const char *const short_options = "ht:";
1788         const struct option long_options[] = {
1789         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1790         { .name = "timeout",    .has_arg = required_argument,   .val = 't' },
1791         { .name = NULL } };
1792
1793         while ((opt = getopt_long(argc, argv, short_options,
1794                                   long_options, NULL)) != -1) {
1795                 switch (opt) {
1796                 case 't':
1797                         timeout = 1000 * atol(optarg);
1798                         break;
1799                 case 'h':
1800                         printf("ping nid[,nid,...]\n"
1801                                "\t --timeout: ping timeout\n"
1802                                "\t --help: display this help\n");
1803                         return 0;
1804                 default:
1805                         return 0;
1806                 }
1807         }
1808
1809         for (; optind < argc; optind++)
1810                 rc = lustre_lnet_ping_nid(argv[optind], timeout, -1, &show_rc, &err_rc);
1811
1812         if (show_rc)
1813                 cYAML_print_tree(show_rc);
1814
1815         if (err_rc)
1816                 cYAML_print_tree2file(stderr, err_rc);
1817
1818         cYAML_free_tree(err_rc);
1819         cYAML_free_tree(show_rc);
1820
1821         return rc;
1822 }
1823
1824 static int jt_discover(int argc, char **argv)
1825 {
1826         struct cYAML *err_rc = NULL;
1827         struct cYAML *show_rc = NULL;
1828         int force = 0;
1829         int rc = 0, opt;
1830
1831         const char *const short_options = "fh";
1832         const struct option long_options[] = {
1833                 { .name = "force",      .has_arg = no_argument, .val = 'f' },
1834                 { .name = "help",       .has_arg = no_argument, .val = 'h' },
1835                 { .name = NULL } };
1836
1837         while ((opt = getopt_long(argc, argv, short_options,
1838                                   long_options, NULL)) != -1) {
1839                 switch (opt) {
1840                 case 'f':
1841                         force = 1;
1842                         break;
1843                 case 'h':
1844                         printf("discover nid[,nid,...]\n"
1845                                "\t --force: force discovery\n"
1846                                "\t --help: display this help\n");
1847                         return 0;
1848                 default:
1849                         return 0;
1850                 }
1851         }
1852
1853         for (; optind < argc; optind++)
1854                 rc = lustre_lnet_discover_nid(argv[optind], force, -1, &show_rc,
1855                                               &err_rc);
1856
1857         if (show_rc)
1858                 cYAML_print_tree(show_rc);
1859
1860         if (err_rc)
1861                 cYAML_print_tree2file(stderr, err_rc);
1862
1863         cYAML_free_tree(err_rc);
1864         cYAML_free_tree(show_rc);
1865
1866         return rc;
1867 }
1868
1869 static int lnetctl_list_commands(int argc, char **argv)
1870 {
1871         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
1872
1873         Parser_list_commands(cmd_list, buffer, sizeof(buffer), NULL, 0, 4);
1874
1875         return 0;
1876 }
1877
1878 int main(int argc, char **argv)
1879 {
1880         int rc = 0;
1881         struct cYAML *err_rc = NULL;
1882
1883         rc = lustre_lnet_config_lib_init();
1884         if (rc < 0) {
1885                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1886                                   "cannot register LNet device", &err_rc);
1887                 cYAML_print_tree2file(stderr, err_rc);
1888                 return rc;
1889         }
1890
1891         Parser_init("lnetctl > ", cmd_list);
1892         if (argc > 1) {
1893                 rc = Parser_execarg(argc - 1, &argv[1], cmd_list);
1894                 goto errorout;
1895         }
1896
1897         Parser_commands();
1898
1899 errorout:
1900         return rc;
1901 }