Whamcloud - gitweb
LU-11283 lnet: fix setting health value manually
[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 = "t: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 = 't' },
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 't':
1012                         if (parse_long(optarg, &healthv) != 0)
1013                                 healthv = -1;
1014                         break;
1015                 case 'a':
1016                         all = true;
1017                         break;
1018                 default:
1019                         return 0;
1020                 }
1021         }
1022
1023         rc = cb(healthv, all, nid, -1, &err_rc);
1024
1025         if (rc != LUSTRE_CFG_RC_NO_ERR)
1026                 cYAML_print_tree2file(stderr, err_rc);
1027
1028         cYAML_free_tree(err_rc);
1029
1030         return rc;
1031 }
1032
1033 static int jt_set_ni_value(int argc, char **argv)
1034 {
1035         return set_value_helper(argc, argv, lustre_lnet_config_ni_healthv);
1036 }
1037
1038 static int jt_set_peer_ni_value(int argc, char **argv)
1039 {
1040         return set_value_helper(argc, argv, lustre_lnet_config_peer_ni_healthv);
1041 }
1042
1043 static int jt_show_recovery(int argc, char **argv)
1044 {
1045         int rc, opt;
1046         struct cYAML *err_rc = NULL, *show_rc = NULL;
1047
1048         const char *const short_options = "lp";
1049         static const struct option long_options[] = {
1050                 { .name = "local", .has_arg = no_argument, .val = 'l' },
1051                 { .name = "peer", .has_arg = no_argument, .val = 'p' },
1052                 { .name = NULL } };
1053
1054         rc = check_cmd(debug_cmds, "recovery", NULL, 0, argc, argv);
1055         if (rc)
1056                 return rc;
1057
1058         while ((opt = getopt_long(argc, argv, short_options,
1059                                    long_options, NULL)) != -1) {
1060                 switch (opt) {
1061                 case 'l':
1062                         rc = lustre_lnet_show_local_ni_recovq(-1, &show_rc, &err_rc);
1063                         break;
1064                 case 'p':
1065                         rc = lustre_lnet_show_peer_ni_recovq(-1, &show_rc, &err_rc);
1066                         break;
1067                 default:
1068                         return 0;
1069                 }
1070         }
1071
1072         if (rc != LUSTRE_CFG_RC_NO_ERR)
1073                 cYAML_print_tree2file(stderr, err_rc);
1074         else if (show_rc)
1075                 cYAML_print_tree(show_rc);
1076
1077         cYAML_free_tree(err_rc);
1078         cYAML_free_tree(show_rc);
1079
1080         return rc;
1081 }
1082
1083 static int jt_show_net(int argc, char **argv)
1084 {
1085         char *network = NULL;
1086         int rc, opt;
1087         struct cYAML *err_rc = NULL, *show_rc = NULL;
1088         long int detail = 0;
1089
1090         const char *const short_options = "n:v";
1091         static const struct option long_options[] = {
1092                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
1093                 { .name = "verbose", .has_arg = optional_argument, .val = 'v' },
1094                 { .name = NULL } };
1095
1096         rc = check_cmd(net_cmds, "net", "show", 0, argc, argv);
1097         if (rc)
1098                 return rc;
1099
1100         while ((opt = getopt_long(argc, argv, short_options,
1101                                    long_options, NULL)) != -1) {
1102                 switch (opt) {
1103                 case 'n':
1104                         network = optarg;
1105                         break;
1106                 case 'v':
1107                         if ((!optarg) && (argv[optind] != NULL) &&
1108                             (argv[optind][0] != '-')) {
1109                                 if (parse_long(argv[optind++], &detail) != 0)
1110                                         detail = 1;
1111                         } else {
1112                                 detail = 1;
1113                         }
1114                         break;
1115                 case '?':
1116                         print_help(net_cmds, "net", "show");
1117                 default:
1118                         return 0;
1119                 }
1120         }
1121
1122         rc = lustre_lnet_show_net(network, (int) detail, -1, &show_rc, &err_rc,
1123                                   false);
1124
1125         if (rc != LUSTRE_CFG_RC_NO_ERR)
1126                 cYAML_print_tree2file(stderr, err_rc);
1127         else if (show_rc)
1128                 cYAML_print_tree(show_rc);
1129
1130         cYAML_free_tree(err_rc);
1131         cYAML_free_tree(show_rc);
1132
1133         return rc;
1134 }
1135
1136 static int jt_show_routing(int argc, char **argv)
1137 {
1138         struct cYAML *err_rc = NULL, *show_rc = NULL;
1139         int rc;
1140
1141         rc = check_cmd(routing_cmds, "routing", "show", 0, argc, argv);
1142         if (rc)
1143                 return rc;
1144
1145         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, false);
1146
1147         if (rc != LUSTRE_CFG_RC_NO_ERR)
1148                 cYAML_print_tree2file(stderr, err_rc);
1149         else if (show_rc)
1150                 cYAML_print_tree(show_rc);
1151
1152         cYAML_free_tree(err_rc);
1153         cYAML_free_tree(show_rc);
1154
1155         return rc;
1156 }
1157
1158 static int jt_show_stats(int argc, char **argv)
1159 {
1160         int rc;
1161         struct cYAML *show_rc = NULL, *err_rc = NULL;
1162
1163         rc = check_cmd(stats_cmds, "stats", "show", 0, argc, argv);
1164         if (rc)
1165                 return rc;
1166
1167         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
1168
1169         if (rc != LUSTRE_CFG_RC_NO_ERR)
1170                 cYAML_print_tree2file(stderr, err_rc);
1171         else if (show_rc)
1172                 cYAML_print_tree(show_rc);
1173
1174         cYAML_free_tree(err_rc);
1175         cYAML_free_tree(show_rc);
1176
1177         return rc;
1178 }
1179
1180 static int jt_show_global(int argc, char **argv)
1181 {
1182         int rc;
1183         struct cYAML *show_rc = NULL, *err_rc = NULL;
1184
1185         rc = check_cmd(global_cmds, "global", "show", 0, argc, argv);
1186         if (rc)
1187                 return rc;
1188
1189         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1190         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1191                 cYAML_print_tree2file(stderr, err_rc);
1192                 goto out;
1193         }
1194
1195         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1196         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1197                 cYAML_print_tree2file(stderr, err_rc);
1198                 goto out;
1199         }
1200
1201         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1202         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1203                 cYAML_print_tree2file(stderr, err_rc);
1204                 goto out;
1205         }
1206
1207         rc = lustre_lnet_show_retry_count(-1, &show_rc, &err_rc);
1208         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1209                 cYAML_print_tree2file(stderr, err_rc);
1210                 goto out;
1211         }
1212
1213         rc = lustre_lnet_show_transaction_to(-1, &show_rc, &err_rc);
1214         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1215                 cYAML_print_tree2file(stderr, err_rc);
1216                 goto out;
1217         }
1218
1219         rc = lustre_lnet_show_hsensitivity(-1, &show_rc, &err_rc);
1220         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1221                 cYAML_print_tree2file(stderr, err_rc);
1222                 goto out;
1223         }
1224
1225         if (show_rc)
1226                 cYAML_print_tree(show_rc);
1227
1228 out:
1229         cYAML_free_tree(err_rc);
1230         cYAML_free_tree(show_rc);
1231
1232         return rc;
1233 }
1234
1235 static int jt_lnet(int argc, char **argv)
1236 {
1237         int rc;
1238
1239         rc = check_cmd(lnet_cmds, "lnet", NULL, 2, argc, argv);
1240         if (rc)
1241                 return rc;
1242
1243         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
1244 }
1245
1246 static int jt_route(int argc, char **argv)
1247 {
1248         int rc;
1249
1250         rc = check_cmd(route_cmds, "route", NULL, 2, argc, argv);
1251         if (rc)
1252                 return rc;
1253
1254         return Parser_execarg(argc - 1, &argv[1], route_cmds);
1255 }
1256
1257 static int jt_net(int argc, char **argv)
1258 {
1259         int rc;
1260
1261         rc = check_cmd(net_cmds, "net", NULL, 2, argc, argv);
1262         if (rc)
1263                 return rc;
1264
1265         return Parser_execarg(argc - 1, &argv[1], net_cmds);
1266 }
1267
1268 static int jt_routing(int argc, char **argv)
1269 {
1270         int rc;
1271
1272         rc = check_cmd(routing_cmds, "routing", NULL, 2, argc, argv);
1273         if (rc)
1274                 return rc;
1275
1276         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
1277 }
1278
1279 static int jt_stats(int argc, char **argv)
1280 {
1281         int rc;
1282
1283         rc = check_cmd(stats_cmds, "stats", NULL, 2, argc, argv);
1284         if (rc)
1285                 return rc;
1286
1287         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
1288 }
1289
1290 static int jt_debug(int argc, char **argv)
1291 {
1292         int rc;
1293
1294         rc = check_cmd(debug_cmds, "recovery", NULL, 2, argc, argv);
1295         if (rc)
1296                 return rc;
1297
1298         return Parser_execarg(argc - 1, &argv[1], debug_cmds);
1299 }
1300
1301 static int jt_global(int argc, char **argv)
1302 {
1303         int rc;
1304
1305         rc = check_cmd(global_cmds, "global", NULL, 2, argc, argv);
1306         if (rc)
1307                 return rc;
1308
1309         return Parser_execarg(argc - 1, &argv[1], global_cmds);
1310 }
1311
1312 static int jt_peers(int argc, char **argv)
1313 {
1314         int rc;
1315
1316         rc = check_cmd(peer_cmds, "peer", NULL, 2, argc, argv);
1317         if (rc)
1318                 return rc;
1319
1320         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
1321 }
1322
1323 static int jt_set(int argc, char **argv)
1324 {
1325         int rc;
1326
1327         rc = check_cmd(set_cmds, "set", NULL, 2, argc, argv);
1328         if (rc)
1329                 return rc;
1330
1331         return Parser_execarg(argc - 1, &argv[1], set_cmds);
1332 }
1333
1334 static int jt_import(int argc, char **argv)
1335 {
1336         char *file = NULL;
1337         struct cYAML *err_rc = NULL;
1338         struct cYAML *show_rc = NULL;
1339         int rc = 0, return_rc = 0, opt, opt_found = 0;
1340         char cmd = 'a';
1341
1342         const char *const short_options = "adseh";
1343         static const struct option long_options[] = {
1344                 { .name = "add",  .has_arg = no_argument, .val = 'a' },
1345                 { .name = "del",  .has_arg = no_argument, .val = 'd' },
1346                 { .name = "show", .has_arg = no_argument, .val = 's' },
1347                 { .name = "exec", .has_arg = no_argument, .val = 'e' },
1348                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1349                 { .name = NULL } };
1350
1351         while ((opt = getopt_long(argc, argv, short_options,
1352                                    long_options, NULL)) != -1) {
1353                 opt_found = 1;
1354                 switch (opt) {
1355                 case 'a':
1356                         cmd = opt;
1357                         break;
1358                 case 'd':
1359                 case 's':
1360                         cmd = opt;
1361                         break;
1362                 case 'e':
1363                         cmd = opt;
1364                         break;
1365                 case 'h':
1366                         printf("import FILE\n"
1367                                "import < FILE : import a file\n"
1368                                "\t--add: add configuration\n"
1369                                "\t--del: delete configuration\n"
1370                                "\t--show: show configuration\n"
1371                                "\t--exec: execute command\n"
1372                                "\t--help: display this help\n"
1373                                "If no command option is given then --add"
1374                                " is assumed by default\n");
1375                         return 0;
1376                 default:
1377                         return 0;
1378                 }
1379         }
1380
1381         /* grab the file name if one exists */
1382         if (opt_found && argc == 3)
1383                 file = argv[2];
1384         else if (!opt_found && argc == 2)
1385                 file = argv[1];
1386
1387         switch (cmd) {
1388         case 'a':
1389                 rc = lustre_yaml_config(file, &err_rc);
1390                 return_rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1391                 cYAML_print_tree(show_rc);
1392                 cYAML_free_tree(show_rc);
1393                 break;
1394         case 'd':
1395                 rc = lustre_yaml_del(file, &err_rc);
1396                 break;
1397         case 's':
1398                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1399                 cYAML_print_tree(show_rc);
1400                 cYAML_free_tree(show_rc);
1401                 break;
1402         case 'e':
1403                 rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1404                 cYAML_print_tree(show_rc);
1405                 cYAML_free_tree(show_rc);
1406                 break;
1407         }
1408
1409         if (rc || return_rc) {
1410                 cYAML_print_tree2file(stderr, err_rc);
1411                 cYAML_free_tree(err_rc);
1412         }
1413
1414         return rc;
1415 }
1416
1417 static int jt_export(int argc, char **argv)
1418 {
1419         struct cYAML *show_rc = NULL;
1420         struct cYAML *err_rc = NULL;
1421         int rc;
1422         FILE *f = NULL;
1423         int opt;
1424         bool backup = false;
1425         char *file = NULL;
1426
1427         const char *const short_options = "bh";
1428         static const struct option long_options[] = {
1429                 { .name = "backup", .has_arg = no_argument, .val = 'b' },
1430                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1431                 { .name = NULL } };
1432
1433         while ((opt = getopt_long(argc, argv, short_options,
1434                                    long_options, NULL)) != -1) {
1435                 switch (opt) {
1436                 case 'b':
1437                         backup = true;
1438                         break;
1439                 case 'h':
1440                 default:
1441                         printf("export > FILE.yaml : export configuration\n"
1442                                "\t--backup: export only what's necessary for reconfig\n"
1443                                "\t--help: display this help\n");
1444                         return 0;
1445                 }
1446         }
1447
1448         if (backup && argc >= 3)
1449                 file = argv[2];
1450         else if (!backup && argc >= 2)
1451                 file = argv[1];
1452         else
1453                 f = stdout;
1454
1455         if (file) {
1456                 f = fopen(file, "w");
1457                 if (f == NULL)
1458                         return -1;
1459         }
1460
1461         rc = lustre_lnet_show_net(NULL, 2, -1, &show_rc, &err_rc, backup);
1462         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1463                 cYAML_print_tree2file(stderr, err_rc);
1464                 cYAML_free_tree(err_rc);
1465                 err_rc = NULL;
1466         }
1467
1468         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
1469                                     &err_rc, backup);
1470         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1471                 cYAML_print_tree2file(stderr, err_rc);
1472                 cYAML_free_tree(err_rc);
1473                 err_rc = NULL;
1474         }
1475
1476         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, backup);
1477         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1478                 cYAML_print_tree2file(stderr, err_rc);
1479                 cYAML_free_tree(err_rc);
1480                 err_rc = NULL;
1481         }
1482
1483         rc = lustre_lnet_show_peer(NULL, 2, -1, &show_rc, &err_rc, backup);
1484         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1485                 cYAML_print_tree2file(stderr, err_rc);
1486                 cYAML_free_tree(err_rc);
1487                 err_rc = NULL;
1488         }
1489
1490         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1491         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1492                 cYAML_print_tree2file(stderr, err_rc);
1493                 cYAML_free_tree(err_rc);
1494                 err_rc = NULL;
1495         }
1496
1497         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1498         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1499                 cYAML_print_tree2file(stderr, err_rc);
1500                 cYAML_free_tree(err_rc);
1501                 err_rc = NULL;
1502         }
1503
1504         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1505         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1506                 cYAML_print_tree2file(stderr, err_rc);
1507                 cYAML_free_tree(err_rc);
1508                 err_rc = NULL;
1509         }
1510
1511         if (show_rc != NULL) {
1512                 cYAML_print_tree2file(f, show_rc);
1513                 cYAML_free_tree(show_rc);
1514         }
1515
1516         if (argc >= 2)
1517                 fclose(f);
1518
1519         return 0;
1520 }
1521
1522 static int jt_add_peer_nid(int argc, char **argv)
1523 {
1524         char *prim_nid = NULL;
1525         char **nids = NULL, **nids2 = NULL;
1526         int size = 0;
1527         struct cYAML *err_rc = NULL;
1528         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i;
1529         bool non_mr = false;
1530         bool ip2nets = false, nid_list = false, prim_nid_present = false;
1531
1532         const char *const short_opts = "k:mn:";
1533         const struct option long_opts[] = {
1534         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
1535         { .name = "non_mr",     .has_arg = no_argument,         .val = 'm' },
1536         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1537         { .name = "ip2nets",    .has_arg = required_argument,   .val = 'i' },
1538         { .name = NULL } };
1539
1540         rc = check_cmd(peer_cmds, "peer", "add", 2, argc, argv);
1541         if (rc)
1542                 return rc;
1543
1544         while ((opt = getopt_long(argc, argv, short_opts,
1545                                   long_opts, NULL)) != -1) {
1546                 switch (opt) {
1547                 case 'k':
1548                         prim_nid_present = true;
1549                         if (ip2nets) {
1550                                 cYAML_build_error(-1, -1, "peer", "add",
1551                                                 "ip2nets can not be specified"
1552                                                 " along side prim_nid parameter.",
1553                                                 &err_rc);
1554                                 goto failed;
1555                         }
1556                         prim_nid = optarg;
1557                         break;
1558                 case 'i':
1559                 case 'n':
1560                         if (opt == 'i')
1561                                 ip2nets = true;
1562
1563                         if (opt == 'n')
1564                                 nid_list = true;
1565
1566                         if (ip2nets && (nid_list || prim_nid_present)) {
1567                                 cYAML_build_error(-1, -1, "peer", "add",
1568                                                 "ip2nets can not be specified"
1569                                                 " along side nid or prim_nid"
1570                                                 " parameters", &err_rc);
1571                                 goto failed;
1572                         }
1573
1574                         size = lustre_lnet_parse_nids(optarg, nids, size,
1575                                                       &nids2);
1576                         if (nids2 == NULL)
1577                                 goto failed;
1578                         nids = nids2;
1579                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1580                         break;
1581                 case 'm':
1582                         non_mr = true;
1583                         break;
1584                 case '?':
1585                         print_help(peer_cmds, "peer", "add");
1586                 default:
1587                         return 0;
1588                 }
1589         }
1590
1591         for (; optind < argc; optind++) {
1592                 size = lustre_lnet_parse_nids(argv[optind], nids, size,
1593                                                 &nids2);
1594                 if (nids2 == NULL)
1595                         goto failed;
1596                 nids = nids2;
1597         }
1598
1599         rc = lustre_lnet_config_peer_nid(prim_nid, nids, size,
1600                                          !non_mr, ip2nets, -1, &err_rc);
1601
1602 failed:
1603         if (nids) {
1604                 /* free the array of nids */
1605                 for (i = 0; i < size; i++)
1606                         free(nids[i]);
1607                 free(nids);
1608         }
1609
1610         if (rc != LUSTRE_CFG_RC_NO_ERR)
1611                 cYAML_print_tree2file(stderr, err_rc);
1612
1613         cYAML_free_tree(err_rc);
1614
1615         return rc;
1616 }
1617
1618 static int jt_del_peer_nid(int argc, char **argv)
1619 {
1620         char *prim_nid = NULL;
1621         char **nids = NULL, **nids2 = NULL;
1622         struct cYAML *err_rc = NULL;
1623         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i, size = 0;
1624         bool ip2nets = false, nid_list = false, prim_nid_present = false;
1625
1626         const char *const short_opts = "k:n:";
1627         const struct option long_opts[] = {
1628         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
1629         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1630         { .name = "ip2nets",    .has_arg = required_argument,   .val = 'i' },
1631         { .name = NULL } };
1632
1633         rc = check_cmd(peer_cmds, "peer", "del", 2, argc, argv);
1634         if (rc)
1635                 return rc;
1636
1637         while ((opt = getopt_long(argc, argv, short_opts,
1638                                   long_opts, NULL)) != -1) {
1639                 switch (opt) {
1640                 case 'k':
1641                         prim_nid_present = true;
1642                         if (ip2nets) {
1643                                 cYAML_build_error(-1, -1, "peer", "add",
1644                                                 "ip2nets can not be specified"
1645                                                 " along side prim_nid parameter.",
1646                                                 &err_rc);
1647                                 goto failed;
1648                         }
1649                         prim_nid = optarg;
1650                         break;
1651                 case 'i':
1652                 case 'n':
1653                         if (opt == 'i')
1654                                 ip2nets = true;
1655
1656                         if (opt == 'n')
1657                                 nid_list = true;
1658
1659                         if (ip2nets && (nid_list || prim_nid_present)) {
1660                                 cYAML_build_error(-1, -1, "peer", "add",
1661                                                 "ip2nets can not be specified"
1662                                                 " along side nid or prim_nid"
1663                                                 " parameters", &err_rc);
1664                                 goto failed;
1665                         }
1666                         size = lustre_lnet_parse_nids(optarg, nids, size,
1667                                                       &nids2);
1668                         if (nids2 == NULL)
1669                                 goto failed;
1670                         nids = nids2;
1671                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1672                         break;
1673                 case '?':
1674                         print_help(peer_cmds, "peer", "del");
1675                 default:
1676                         return 0;
1677                 }
1678         }
1679
1680         for (; optind < argc; optind++) {
1681                 size = lustre_lnet_parse_nids(argv[optind], nids, size,
1682                                                 &nids2);
1683                 if (nids2 == NULL)
1684                         goto failed;
1685                 nids = nids2;
1686         }
1687
1688         rc = lustre_lnet_del_peer_nid(prim_nid, nids, size, ip2nets, -1, &err_rc);
1689
1690 failed:
1691         if (nids) {
1692                 for (i = 0; i < size; i++)
1693                         free(nids[i]);
1694                 free(nids);
1695         }
1696
1697         if (rc != LUSTRE_CFG_RC_NO_ERR)
1698                 cYAML_print_tree2file(stderr, err_rc);
1699
1700         cYAML_free_tree(err_rc);
1701
1702         return rc;
1703 }
1704
1705 static int jt_show_peer(int argc, char **argv)
1706 {
1707         char *nid = NULL;
1708         int rc, opt;
1709         struct cYAML *err_rc = NULL, *show_rc = NULL;
1710         long int detail = 0;
1711
1712         const char *const short_opts = "hn:v::";
1713         const struct option long_opts[] = {
1714         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1715         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
1716         { .name = "verbose",    .has_arg = optional_argument,   .val = 'v' },
1717         { .name = NULL } };
1718
1719         rc = check_cmd(peer_cmds, "peer", "show", 1, argc, argv);
1720         if (rc)
1721                 return rc;
1722
1723         while ((opt = getopt_long(argc, argv, short_opts,
1724                                   long_opts, NULL)) != -1) {
1725                 switch (opt) {
1726                 case 'n':
1727                         nid = optarg;
1728                         break;
1729                 case 'v':
1730                         if ((!optarg) && (argv[optind] != NULL) &&
1731                             (argv[optind][0] != '-')) {
1732                                 if (parse_long(argv[optind++], &detail) != 0)
1733                                         detail = 1;
1734                         } else {
1735                                 detail = 1;
1736                         }
1737                         break;
1738                 case '?':
1739                         print_help(peer_cmds, "peer", "show");
1740                 default:
1741                         return 0;
1742                 }
1743         }
1744
1745         rc = lustre_lnet_show_peer(nid, (int) detail, -1, &show_rc, &err_rc,
1746                                    false);
1747
1748         if (rc != LUSTRE_CFG_RC_NO_ERR)
1749                 cYAML_print_tree2file(stderr, err_rc);
1750         else if (show_rc)
1751                 cYAML_print_tree(show_rc);
1752
1753         cYAML_free_tree(err_rc);
1754         cYAML_free_tree(show_rc);
1755
1756         return rc;
1757 }
1758
1759 static int jt_list_peer(int argc, char **argv)
1760 {
1761         int rc;
1762         struct cYAML *err_rc = NULL, *list_rc = NULL;
1763
1764         rc = check_cmd(peer_cmds, "peer", "list", 0, argc, argv);
1765         if (rc)
1766                 return rc;
1767
1768         rc = lustre_lnet_list_peer(-1, &list_rc, &err_rc);
1769
1770         if (rc != LUSTRE_CFG_RC_NO_ERR)
1771                 cYAML_print_tree2file(stderr, err_rc);
1772         else if (list_rc)
1773                 cYAML_print_tree(list_rc);
1774
1775         cYAML_free_tree(err_rc);
1776         cYAML_free_tree(list_rc);
1777
1778         return rc;
1779 }
1780
1781 static int jt_ping(int argc, char **argv)
1782 {
1783         struct cYAML *err_rc = NULL;
1784         struct cYAML *show_rc = NULL;
1785         int timeout = 1000;
1786         int rc = 0, opt;
1787
1788         const char *const short_options = "ht:";
1789         const struct option long_options[] = {
1790         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
1791         { .name = "timeout",    .has_arg = required_argument,   .val = 't' },
1792         { .name = NULL } };
1793
1794         while ((opt = getopt_long(argc, argv, short_options,
1795                                   long_options, NULL)) != -1) {
1796                 switch (opt) {
1797                 case 't':
1798                         timeout = 1000 * atol(optarg);
1799                         break;
1800                 case 'h':
1801                         printf("ping nid[,nid,...]\n"
1802                                "\t --timeout: ping timeout\n"
1803                                "\t --help: display this help\n");
1804                         return 0;
1805                 default:
1806                         return 0;
1807                 }
1808         }
1809
1810         for (; optind < argc; optind++)
1811                 rc = lustre_lnet_ping_nid(argv[optind], timeout, -1, &show_rc, &err_rc);
1812
1813         if (show_rc)
1814                 cYAML_print_tree(show_rc);
1815
1816         if (err_rc)
1817                 cYAML_print_tree2file(stderr, err_rc);
1818
1819         cYAML_free_tree(err_rc);
1820         cYAML_free_tree(show_rc);
1821
1822         return rc;
1823 }
1824
1825 static int jt_discover(int argc, char **argv)
1826 {
1827         struct cYAML *err_rc = NULL;
1828         struct cYAML *show_rc = NULL;
1829         int force = 0;
1830         int rc = 0, opt;
1831
1832         const char *const short_options = "fh";
1833         const struct option long_options[] = {
1834                 { .name = "force",      .has_arg = no_argument, .val = 'f' },
1835                 { .name = "help",       .has_arg = no_argument, .val = 'h' },
1836                 { .name = NULL } };
1837
1838         while ((opt = getopt_long(argc, argv, short_options,
1839                                   long_options, NULL)) != -1) {
1840                 switch (opt) {
1841                 case 'f':
1842                         force = 1;
1843                         break;
1844                 case 'h':
1845                         printf("discover nid[,nid,...]\n"
1846                                "\t --force: force discovery\n"
1847                                "\t --help: display this help\n");
1848                         return 0;
1849                 default:
1850                         return 0;
1851                 }
1852         }
1853
1854         for (; optind < argc; optind++)
1855                 rc = lustre_lnet_discover_nid(argv[optind], force, -1, &show_rc,
1856                                               &err_rc);
1857
1858         if (show_rc)
1859                 cYAML_print_tree(show_rc);
1860
1861         if (err_rc)
1862                 cYAML_print_tree2file(stderr, err_rc);
1863
1864         cYAML_free_tree(err_rc);
1865         cYAML_free_tree(show_rc);
1866
1867         return rc;
1868 }
1869
1870 static int lnetctl_list_commands(int argc, char **argv)
1871 {
1872         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
1873
1874         Parser_list_commands(cmd_list, buffer, sizeof(buffer), NULL, 0, 4);
1875
1876         return 0;
1877 }
1878
1879 int main(int argc, char **argv)
1880 {
1881         int rc = 0;
1882         struct cYAML *err_rc = NULL;
1883
1884         rc = lustre_lnet_config_lib_init();
1885         if (rc < 0) {
1886                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1887                                   "cannot register LNet device", &err_rc);
1888                 cYAML_print_tree2file(stderr, err_rc);
1889                 return rc;
1890         }
1891
1892         Parser_init("lnetctl > ", cmd_list);
1893         if (argc > 1) {
1894                 rc = Parser_execarg(argc - 1, &argv[1], cmd_list);
1895                 goto errorout;
1896         }
1897
1898         Parser_commands();
1899
1900 errorout:
1901         return rc;
1902 }