Whamcloud - gitweb
LU-14979 lnet: set max recovery interval duration
[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 "lnetconfig/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_show_udsp(int argc, char **argv);
54 static int jt_set_tiny(int argc, char **argv);
55 static int jt_set_small(int argc, char **argv);
56 static int jt_set_large(int argc, char **argv);
57 static int jt_set_numa(int argc, char **argv);
58 static int jt_set_retry_count(int argc, char **argv);
59 static int jt_set_transaction_to(int argc, char **argv);
60 static int jt_set_recov_intrv(int argc, char **argv);
61 static int jt_set_rtr_sensitivity(int argc, char **argv);
62 static int jt_set_hsensitivity(int argc, char **argv);
63 static int jt_set_max_recovery_ping_interval(int argc, char **argv);
64 static int jt_reset_stats(int argc, char **argv);
65 static int jt_add_peer_nid(int argc, char **argv);
66 static int jt_del_peer_nid(int argc, char **argv);
67 static int jt_set_max_intf(int argc, char **argv);
68 static int jt_set_discovery(int argc, char **argv);
69 static int jt_set_drop_asym_route(int argc, char **argv);
70 static int jt_list_peer(int argc, char **argv);
71 static int jt_add_udsp(int argc, char **argv);
72 static int jt_del_udsp(int argc, char **argv);
73 /*static int jt_show_peer(int argc, char **argv);*/
74 static int lnetctl_list_commands(int argc, char **argv);
75 static int jt_import(int argc, char **argv);
76 static int jt_export(int argc, char **argv);
77 static int jt_ping(int argc, char **argv);
78 static int jt_discover(int argc, char **argv);
79 static int jt_lnet(int argc, char **argv);
80 static int jt_route(int argc, char **argv);
81 static int jt_net(int argc, char **argv);
82 static int jt_routing(int argc, char **argv);
83 static int jt_set(int argc, char **argv);
84 static int jt_debug(int argc, char **argv);
85 static int jt_stats(int argc, char **argv);
86 static int jt_global(int argc, char **argv);
87 static int jt_peers(int argc, char **argv);
88 static int jt_set_ni_value(int argc, char **argv);
89 static int jt_set_peer_ni_value(int argc, char **argv);
90 static int jt_calc_service_id(int argc, char **argv);
91 static int jt_set_response_tracking(int argc, char **argv);
92 static int jt_set_recovery_limit(int argc, char **argv);
93 static int jt_udsp(int argc, char **argv);
94 static int jt_setup_mrrouting(int argc, char **argv);
95 static int jt_calc_cpt_of_nid(int argc, char **argv);
96
97 command_t cmd_list[] = {
98         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
99         {"route", jt_route, 0, "route {add | del | show | help}"},
100         {"net", jt_net, 0, "net {add | del | show | set | help}"},
101         {"routing", jt_routing, 0, "routing {show | help}"},
102         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
103                            " | routing | numa_range | max_interfaces"
104                            " | discovery | drop_asym_route | retry_count"
105                            " | transaction_timeout | health_sensitivity"
106                            " | recovery_interval | router_sensitivity"
107                            " | response_tracking | recovery_limit}"},
108         {"import", jt_import, 0, "import FILE.yaml"},
109         {"export", jt_export, 0, "export FILE.yaml"},
110         {"stats", jt_stats, 0, "stats {show | help}"},
111         {"debug", jt_debug, 0, "debug recovery {local | peer}"},
112         {"global", jt_global, 0, "global {show | help}"},
113         {"peer", jt_peers, 0, "peer {add | del | show | list | set | help}"},
114         {"ping", jt_ping, 0, "ping nid,[nid,...]"},
115         {"discover", jt_discover, 0, "discover nid[,nid,...]"},
116         {"service-id", jt_calc_service_id, 0, "Calculate IB Lustre service ID\n"},
117         {"udsp", jt_udsp, 0, "udsp {add | del | help}"},
118         {"setup-mrrouting", jt_setup_mrrouting, 0,
119          "setup linux routing tables\n"},
120         {"cpt-of-nid", jt_calc_cpt_of_nid, 0, "Calculate the CPT associated with NID\n"
121          "\t--nid: NID to calculate the CPT of\n"
122          "\t--ncpt: Number of CPTs to consider in the calculation\n"},
123         {"help", Parser_help, 0, "help"},
124         {"exit", Parser_quit, 0, "quit"},
125         {"quit", Parser_quit, 0, "quit"},
126         {"--list-commands", lnetctl_list_commands, 0, "list commands"},
127         { 0, 0, 0, NULL }
128 };
129
130 command_t lnet_cmds[] = {
131         {"configure", jt_config_lnet, 0, "configure lnet\n"
132          "\t--all: load NI configuration from module parameters\n"},
133         {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
134         { 0, 0, 0, NULL }
135 };
136
137 command_t route_cmds[] = {
138         {"add", jt_add_route, 0, "add a route\n"
139          "\t--net: net name (e.g. tcp0)\n"
140          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
141          "\t--hop: number to final destination (1 < hops < 255)\n"
142          "\t--priority: priority of route (0 - highest prio\n"
143          "\t--health_sensitivity: gateway health sensitivity (>= 1)\n"},
144         {"del", jt_del_route, 0, "delete a route\n"
145          "\t--net: net name (e.g. tcp0)\n"
146          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
147         {"show", jt_show_route, 0, "show routes\n"
148          "\t--net: net name (e.g. tcp0) to filter on\n"
149          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
150          "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
151          "\t--priority: priority of route (0 - highest prio to filter on\n"
152          "\t--health_sensitivity: gateway health sensitivity (>= 1)\n"
153          "\t--verbose: display detailed output per route\n"},
154         { 0, 0, 0, NULL }
155 };
156
157 command_t net_cmds[] = {
158         {"add", jt_add_ni, 0, "add a network\n"
159          "\t--net: net name (e.g. tcp0)\n"
160          "\t--if: physical interface (e.g. eth0)\n"
161          "\t--ip2net: specify networks based on IP address patterns\n"
162          "\t--peer-timeout: time to wait before declaring a peer dead\n"
163          "\t--peer-credits: define the max number of inflight messages\n"
164          "\t--peer-buffer-credits: the number of buffer credits per peer\n"
165          "\t--credits: Network Interface credits\n"
166          "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"
167          "\t--conns-per-peer: number of connections per peer\n"
168          "\t--skip-mr-route-setup: do not add linux route for the ni\n"},
169         {"del", jt_del_ni, 0, "delete a network\n"
170          "\t--net: net name (e.g. tcp0)\n"
171          "\t--if: physical interface (e.g. eth0)\n"},
172         {"show", jt_show_net, 0, "show networks\n"
173          "\t--net: net name (e.g. tcp0) to filter on\n"
174          "\t--verbose: display detailed output per network."
175                        " Optional argument of '2' outputs more stats\n"},
176         {"set", jt_set_ni_value, 0, "set local NI specific parameter\n"
177          "\t--nid: NI NID to set the\n"
178          "\t--health: specify health value to set\n"
179          "\t--conns-per-peer: number of connections per peer\n"
180          "\t--all: set all NIs value to the one specified\n"},
181         { 0, 0, 0, NULL }
182 };
183
184 command_t routing_cmds[] = {
185         {"show", jt_show_routing, 0, "show routing information\n"},
186         { 0, 0, 0, NULL }
187 };
188
189 command_t stats_cmds[] = {
190         {"show", jt_show_stats, 0, "show LNET statistics\n"},
191         {"reset", jt_reset_stats, 0, "reset LNET statistics\n"},
192         { 0, 0, 0, NULL }
193 };
194
195 command_t debug_cmds[] = {
196         {"recovery", jt_show_recovery, 0, "list recovery queues\n"
197                 "\t--local : list local recovery queue\n"
198                 "\t--peer : list peer recovery queue\n"},
199         { 0, 0, 0, NULL }
200 };
201
202 command_t global_cmds[] = {
203         {"show", jt_show_global, 0, "show global variables\n"},
204         { 0, 0, 0, NULL }
205 };
206
207 command_t set_cmds[] = {
208         {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
209          "\tVALUE must be greater than 0\n"},
210         {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
211          "\tVALUE must be greater than 0\n"},
212         {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
213          "\tVALUE must be greater than 0\n"},
214         {"routing", jt_set_routing, 0, "enable/disable routing\n"
215          "\t0 - disable routing\n"
216          "\t1 - enable routing\n"},
217         {"numa_range", jt_set_numa, 0, "set NUMA range for NI selection\n"
218          "\tVALUE must be at least 0\n"},
219         {"max_interfaces", jt_set_max_intf, 0, "set the default value for "
220                 "max interfaces\n"
221          "\tValue must be greater than 16\n"},
222         {"discovery", jt_set_discovery, 0, "enable/disable peer discovery\n"
223          "\t0 - disable peer discovery\n"
224          "\t1 - enable peer discovery (default)\n"},
225         {"drop_asym_route", jt_set_drop_asym_route, 0,
226          "drop/accept asymmetrical route messages\n"
227          "\t0 - accept asymmetrical route messages (default)\n"
228          "\t1 - drop asymmetrical route messages\n"},
229         {"retry_count", jt_set_retry_count, 0, "number of retries\n"
230          "\t0 - turn of retries\n"
231          "\t>0 - number of retries\n"},
232         {"transaction_timeout", jt_set_transaction_to, 0, "Message/Response timeout\n"
233          "\t>0 - timeout in seconds\n"},
234         {"health_sensitivity", jt_set_hsensitivity, 0, "sensitivity to failure\n"
235          "\t0 - turn off health evaluation\n"
236          "\t>0 - sensitivity value not more than 1000\n"},
237         {"recovery_interval", jt_set_recov_intrv, 0, "interval to ping in seconds (at least 1)\n"
238          "\t>0 - time in seconds between pings\n"},
239         {"router_sensitivity", jt_set_rtr_sensitivity, 0, "router sensitivity %\n"
240          "\t100 - router interfaces need to be fully healthy to be used\n"
241          "\t<100 - router interfaces can be used even if not healthy\n"},
242         {"response_tracking", jt_set_response_tracking, 0,
243          "Set the behavior of response tracking\n"
244          "\t0 - Only LNet pings and discovery pushes utilize response tracking\n"
245          "\t1 - GETs are eligible for response tracking\n"
246          "\t2 - PUTs are eligible for response tracking\n"
247          "\t3 - Both PUTs and GETs are eligible for response tracking (default)\n"
248          "\tNote: Regardless of the value of the response_tracking parameter LNet\n"
249          "\t      pings and discovery pushes always utilize response tracking\n"},
250         {"recovery_limit", jt_set_recovery_limit, 0,
251          "Set how long LNet will attempt to recover unhealthy interfaces.\n"
252          "\t0 - Recover indefinitely (default)\n"
253          "\t>0 - Recover for the specified number of seconds.\n"},
254         {"max_recovery_ping_interval", jt_set_max_recovery_ping_interval, 0,
255          "maximum recovery ping interval\n"
256          "\t>0 - maximum recovery ping interval in seconds\n"},
257         { 0, 0, 0, NULL }
258 };
259
260 command_t peer_cmds[] = {
261         {"add", jt_add_peer_nid, 0, "add a peer NID\n"
262          "\t--prim_nid: Primary NID of the peer.\n"
263          "\t--nid: one or more peer NIDs\n"
264          "\t--non_mr: create this peer as not Multi-Rail capable\n"
265          "\t--ip2nets: specify a range of nids per peer"},
266         {"del", jt_del_peer_nid, 0, "delete a peer NID\n"
267          "\t--prim_nid: Primary NID of the peer.\n"
268          "\t--nid: list of NIDs to remove. If none provided,\n"
269          "\t       peer is deleted\n"
270          "\t--ip2nets: specify a range of nids per peer"},
271         {"show", jt_show_peer, 0, "show peer information\n"
272          "\t--nid: NID of peer to filter on.\n"
273          "\t--verbose: display detailed output per peer."
274                        " Optional argument of '2' outputs more stats\n"},
275         {"list", jt_list_peer, 0, "list all peers\n"},
276         {"set", jt_set_peer_ni_value, 0, "set peer ni specific parameter\n"
277          "\t--nid: Peer NI NID to set the\n"
278          "\t--health: specify health value to set\n"
279          "\t--all: set all peer_nis values to the one specified\n"},
280         { 0, 0, 0, NULL }
281 };
282
283 command_t udsp_cmds[] = {
284         {"add", jt_add_udsp, 0, "add a udsp\n"
285          "\t--src: ip2nets syntax specifying the local NID to match\n"
286          "\t--dst: ip2nets syntax specifying the remote NID to match\n"
287          "\t--rte: ip2nets syntax specifying the router NID to match\n"
288          "\t--priority: priority value (0 - highest priority)\n"
289          "\t--idx: index of where to insert the rule.\n"
290          "\t       By default, appends to the end of the rule list.\n"},
291         {"del", jt_del_udsp, 0, "delete a udsp\n"
292         "\t--idx: index of the Policy.\n"},
293         {"show", jt_show_udsp, 0, "show udsps\n"
294          "\t --idx: index of the policy to show.\n"},
295         { 0, 0, 0, NULL }
296 };
297
298 static int parse_long(const char *number, long int *value)
299 {
300         char *end;
301
302         if (!number)
303                 return -1;
304
305         *value = strtol(number,  &end, 0);
306         if (end != NULL && *end != 0)
307                 return -1;
308
309         return 0;
310 }
311
312 static int jt_setup_mrrouting(int argc, char **argv)
313 {
314         int rc;
315         struct cYAML *err_rc = NULL;
316
317         rc = lustre_lnet_setup_mrrouting(&err_rc);
318
319         if (rc != LUSTRE_CFG_RC_NO_ERR)
320                 cYAML_print_tree2file(stderr, err_rc);
321
322         cYAML_free_tree(err_rc);
323
324         return rc;
325 }
326
327 static inline void print_help(const command_t cmds[], const char *cmd_type,
328                               const char *pc_name)
329 {
330         const command_t *cmd;
331
332         for (cmd = cmds; cmd->pc_name; cmd++) {
333                 if (pc_name != NULL &&
334                     strcmp(cmd->pc_name, pc_name) == 0) {
335                         printf("%s %s: %s\n", cmd_type, cmd->pc_name,
336                                cmd->pc_help);
337                         return;
338                 } else if (pc_name != NULL) {
339                         continue;
340                 }
341                 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
342         }
343 }
344
345 static int check_cmd(const command_t *cmd_list, const char *cmd,
346                      const char *sub_cmd, const int min_args,
347                      int argc, char **argv)
348 {
349         int opt;
350         int rc = 0;
351         optind = 0;
352         opterr = 0;
353
354         const char *const short_options = "h";
355         static const struct option long_options[] = {
356                 { .name = "help", .has_arg = no_argument, .val = 'h' },
357                 { .name = NULL }
358         };
359
360         if (argc < min_args) {
361                 print_help(cmd_list, cmd, sub_cmd);
362                 rc = -1;
363                 goto out;
364         } else if (argc > 2) {
365                 return 0;
366         }
367
368         while ((opt = getopt_long(argc, argv, short_options,
369                                   long_options, NULL)) != -1) {
370                 switch (opt) {
371                 case 'h':
372                         print_help(cmd_list, cmd, sub_cmd);
373                         rc = 1;
374                         break;
375                 default:
376                         rc = 0;
377                         break;
378                 }
379         }
380
381 out:
382         opterr = 1;
383         optind = 0;
384         return rc;
385 }
386
387 static int jt_set_response_tracking(int argc, char **argv)
388 {
389         long int value;
390         int rc;
391         struct cYAML *err_rc = NULL;
392
393         rc = check_cmd(set_cmds, "set", "response_tracking", 2, argc, argv);
394         if (rc)
395                 return rc;
396
397         rc = parse_long(argv[1], &value);
398         if (rc != 0) {
399                 cYAML_build_error(-1, -1, "parser", "set",
400                                   "cannot parse response_tracking value",
401                                   &err_rc);
402                 cYAML_print_tree2file(stderr, err_rc);
403                 cYAML_free_tree(err_rc);
404                 return -1;
405         }
406
407         rc = lustre_lnet_config_response_tracking(value, -1, &err_rc);
408         if (rc != LUSTRE_CFG_RC_NO_ERR)
409                 cYAML_print_tree2file(stderr, err_rc);
410
411         cYAML_free_tree(err_rc);
412
413         return rc;
414 }
415
416 static int jt_calc_service_id(int argc, char **argv)
417 {
418         int rc;
419         __u64 service_id;
420
421         rc = lustre_lnet_calc_service_id(&service_id);
422         if (rc != LUSTRE_CFG_RC_NO_ERR)
423                 return rc;
424
425         /* cYAML currently doesn't support printing hex values.
426          * Therefore just print it locally here
427          */
428         printf("service_id:\n    value: 0x%llx\n",
429                (unsigned long long)(service_id));
430
431         return rc;
432 }
433
434 static int jt_calc_cpt_of_nid(int argc, char **argv)
435 {
436         int rc, opt;
437         int cpt;
438         long int ncpts = -1;
439         char *nid = NULL;
440         struct cYAML *err_rc = NULL;
441         const char *const short_options = "n:c:h";
442         static const struct option long_options[] = {
443         { .name = "nid",       .has_arg = required_argument, .val = 'n' },
444         { .name = "ncpt",     .has_arg = required_argument, .val = 'c' },
445         { .name = NULL } };
446
447         rc = check_cmd(cmd_list, "", "cpt-of-nid", 0, argc, argv);
448         if (rc)
449                 return rc;
450
451         while ((opt = getopt_long(argc, argv, short_options,
452                                    long_options, NULL)) != -1) {
453                 switch (opt) {
454                 case 'n':
455                         nid = optarg;
456                         break;
457                 case 'c':
458                         rc = parse_long(optarg, &ncpts);
459                         if (rc != 0) {
460                                 cYAML_build_error(-1, -1, "cpt", "get",
461                                                 "cannot parse input", &err_rc);
462                                 cYAML_print_tree2file(stderr, err_rc);
463                                 cYAML_free_tree(err_rc);
464                                 return -1;
465                         }
466                         break;
467                 case '?':
468                         print_help(cmd_list, "", "cpt-of-nid");
469                 default:
470                         return 0;
471                 }
472         }
473
474         cpt = lustre_lnet_calc_cpt_of_nid(nid, ncpts);
475         if (cpt < 0)
476                 return -1;
477
478         printf("cpt:\n    value: %d\n", cpt);
479
480         return 0;
481 }
482
483 static int jt_set_recovery_limit(int argc, char **argv)
484 {
485         long int value;
486         int rc;
487         struct cYAML *err_rc = NULL;
488
489         rc = check_cmd(set_cmds, "set", "recovery_limit", 2, argc, argv);
490         if (rc)
491                 return rc;
492
493         rc = parse_long(argv[1], &value);
494         if (rc != 0) {
495                 cYAML_build_error(-1, -1, "parser", "set",
496                                   "cannot parse recovery_limit value",
497                                   &err_rc);
498                 cYAML_print_tree2file(stderr, err_rc);
499                 cYAML_free_tree(err_rc);
500                 return -1;
501         }
502
503         rc = lustre_lnet_config_recovery_limit(value, -1, &err_rc);
504         if (rc != LUSTRE_CFG_RC_NO_ERR)
505                 cYAML_print_tree2file(stderr, err_rc);
506
507         cYAML_free_tree(err_rc);
508
509         return rc;
510 }
511
512 static int jt_set_max_intf(int argc, char **argv)
513 {
514         long int value;
515         int rc;
516         struct cYAML *err_rc = NULL;
517
518         rc = check_cmd(set_cmds, "set", "max_interfaces", 2, argc, argv);
519         if (rc)
520                 return rc;
521
522         rc = parse_long(argv[1], &value);
523         if (rc != 0) {
524                 cYAML_build_error(-1, -1, "parser", "set",
525                                   "cannot parse max_interfaces value", &err_rc);
526                 cYAML_print_tree2file(stderr, err_rc);
527                 cYAML_free_tree(err_rc);
528                 return -1;
529         }
530
531         rc = lustre_lnet_config_max_intf(value, -1, &err_rc);
532         if (rc != LUSTRE_CFG_RC_NO_ERR)
533                 cYAML_print_tree2file(stderr, err_rc);
534
535         cYAML_free_tree(err_rc);
536
537         return rc;
538 }
539
540 static int jt_set_numa(int argc, char **argv)
541 {
542         long int value;
543         int rc;
544         struct cYAML *err_rc = NULL;
545
546         rc = check_cmd(set_cmds, "set", "numa_range", 2, argc, argv);
547         if (rc)
548                 return rc;
549
550         rc = parse_long(argv[1], &value);
551         if (rc != 0) {
552                 cYAML_build_error(-1, -1, "parser", "set",
553                                   "cannot parse numa_range value", &err_rc);
554                 cYAML_print_tree2file(stderr, err_rc);
555                 cYAML_free_tree(err_rc);
556                 return -1;
557         }
558
559         rc = lustre_lnet_config_numa_range(value, -1, &err_rc);
560         if (rc != LUSTRE_CFG_RC_NO_ERR)
561                 cYAML_print_tree2file(stderr, err_rc);
562
563         cYAML_free_tree(err_rc);
564
565         return rc;
566 }
567
568 static int jt_set_recov_intrv(int argc, char **argv)
569 {
570         long int value;
571         int rc;
572         struct cYAML *err_rc = NULL;
573
574         rc = check_cmd(set_cmds, "set", "recovery_interval", 2, argc, argv);
575         if (rc)
576                 return rc;
577
578         rc = parse_long(argv[1], &value);
579         if (rc != 0) {
580                 cYAML_build_error(-1, -1, "parser", "set",
581                                   "cannot parse recovery interval value", &err_rc);
582                 cYAML_print_tree2file(stderr, err_rc);
583                 cYAML_free_tree(err_rc);
584                 return -1;
585         }
586
587         rc = lustre_lnet_config_recov_intrv(value, -1, &err_rc);
588         if (rc != LUSTRE_CFG_RC_NO_ERR)
589                 cYAML_print_tree2file(stderr, err_rc);
590
591         cYAML_free_tree(err_rc);
592
593         return rc;
594 }
595
596 static int jt_set_rtr_sensitivity(int argc, char **argv)
597 {
598         long int value;
599         int rc;
600         struct cYAML *err_rc = NULL;
601
602         rc = check_cmd(set_cmds, "set", "router_sensitivity", 2, argc, argv);
603         if (rc)
604                 return rc;
605
606         rc = parse_long(argv[1], &value);
607         if (rc != 0) {
608                 cYAML_build_error(-1, -1, "parser", "set",
609                                   "cannot parse router sensitivity value", &err_rc);
610                 cYAML_print_tree2file(stderr, err_rc);
611                 cYAML_free_tree(err_rc);
612                 return -1;
613         }
614
615         rc = lustre_lnet_config_rtr_sensitivity(value, -1, &err_rc);
616         if (rc != LUSTRE_CFG_RC_NO_ERR)
617                 cYAML_print_tree2file(stderr, err_rc);
618
619         cYAML_free_tree(err_rc);
620
621         return rc;
622 }
623
624 static int jt_set_hsensitivity(int argc, char **argv)
625 {
626         long int value;
627         int rc;
628         struct cYAML *err_rc = NULL;
629
630         rc = check_cmd(set_cmds, "set", "health_sensitivity", 2, argc, argv);
631         if (rc)
632                 return rc;
633
634         rc = parse_long(argv[1], &value);
635         if (rc != 0) {
636                 cYAML_build_error(-1, -1, "parser", "set",
637                                   "cannot parse health sensitivity value", &err_rc);
638                 cYAML_print_tree2file(stderr, err_rc);
639                 cYAML_free_tree(err_rc);
640                 return -1;
641         }
642
643         rc = lustre_lnet_config_hsensitivity(value, -1, &err_rc);
644         if (rc != LUSTRE_CFG_RC_NO_ERR)
645                 cYAML_print_tree2file(stderr, err_rc);
646
647         cYAML_free_tree(err_rc);
648
649         return rc;
650 }
651
652 static int jt_reset_stats(int argc, char **argv)
653 {
654         int rc;
655         struct cYAML *err_rc = NULL;
656
657         rc = check_cmd(stats_cmds, "stats", "reset", 0, argc, argv);
658         if (rc)
659                 return rc;
660
661         rc = lustre_lnet_reset_stats(-1, &err_rc);
662         if (rc != LUSTRE_CFG_RC_NO_ERR)
663                 cYAML_print_tree2file(stderr, err_rc);
664
665         cYAML_free_tree(err_rc);
666
667         return rc;
668 }
669
670 static int jt_set_transaction_to(int argc, char **argv)
671 {
672         long int value;
673         int rc;
674         struct cYAML *err_rc = NULL;
675
676         rc = check_cmd(set_cmds, "set", "transaction_timeout", 2, argc, argv);
677         if (rc)
678                 return rc;
679
680         rc = parse_long(argv[1], &value);
681         if (rc != 0) {
682                 cYAML_build_error(-1, -1, "parser", "set",
683                                   "cannot parse transaction timeout value", &err_rc);
684                 cYAML_print_tree2file(stderr, err_rc);
685                 cYAML_free_tree(err_rc);
686                 return -1;
687         }
688
689         rc = lustre_lnet_config_transaction_to(value, -1, &err_rc);
690         if (rc != LUSTRE_CFG_RC_NO_ERR)
691                 cYAML_print_tree2file(stderr, err_rc);
692
693         cYAML_free_tree(err_rc);
694
695         return rc;
696 }
697
698 static int jt_set_retry_count(int argc, char **argv)
699 {
700         long int value;
701         int rc;
702         struct cYAML *err_rc = NULL;
703
704         rc = check_cmd(set_cmds, "set", "retry_count", 2, argc, argv);
705         if (rc)
706                 return rc;
707
708         rc = parse_long(argv[1], &value);
709         if (rc != 0) {
710                 cYAML_build_error(-1, -1, "parser", "set",
711                                   "cannot parse retry_count value", &err_rc);
712                 cYAML_print_tree2file(stderr, err_rc);
713                 cYAML_free_tree(err_rc);
714                 return -1;
715         }
716
717         rc = lustre_lnet_config_retry_count(value, -1, &err_rc);
718         if (rc != LUSTRE_CFG_RC_NO_ERR)
719                 cYAML_print_tree2file(stderr, err_rc);
720
721         cYAML_free_tree(err_rc);
722
723         return rc;
724 }
725
726 static int jt_set_discovery(int argc, char **argv)
727 {
728         long int value;
729         int rc;
730         struct cYAML *err_rc = NULL;
731
732         rc = check_cmd(set_cmds, "set", "discovery", 2, argc, argv);
733         if (rc)
734                 return rc;
735
736         rc = parse_long(argv[1], &value);
737         if (rc != 0) {
738                 cYAML_build_error(-1, -1, "parser", "set",
739                                   "cannot parse discovery value", &err_rc);
740                 cYAML_print_tree2file(stderr, err_rc);
741                 cYAML_free_tree(err_rc);
742                 return -1;
743         }
744
745         rc = lustre_lnet_config_discovery(value, -1, &err_rc);
746         if (rc != LUSTRE_CFG_RC_NO_ERR)
747                 cYAML_print_tree2file(stderr, err_rc);
748
749         cYAML_free_tree(err_rc);
750
751         return rc;
752 }
753
754 static int jt_set_drop_asym_route(int argc, char **argv)
755 {
756         long int value;
757         int rc;
758         struct cYAML *err_rc = NULL;
759
760         rc = check_cmd(set_cmds, "set", "drop_asym_route", 2, argc, argv);
761         if (rc)
762                 return rc;
763
764         rc = parse_long(argv[1], &value);
765         if (rc != 0) {
766                 cYAML_build_error(-1, -1, "parser", "set",
767                                   "cannot parse drop_asym_route value",
768                                   &err_rc);
769                 cYAML_print_tree2file(stderr, err_rc);
770                 cYAML_free_tree(err_rc);
771                 return -1;
772         }
773
774         rc = lustre_lnet_config_drop_asym_route(value, -1, &err_rc);
775         if (rc != LUSTRE_CFG_RC_NO_ERR)
776                 cYAML_print_tree2file(stderr, err_rc);
777
778         cYAML_free_tree(err_rc);
779
780         return rc;
781 }
782
783 static int jt_set_tiny(int argc, char **argv)
784 {
785         long int value;
786         int rc;
787         struct cYAML *err_rc = NULL;
788
789         rc = check_cmd(set_cmds, "set", "tiny_buffers", 2, argc, argv);
790         if (rc)
791                 return rc;
792
793         rc = parse_long(argv[1], &value);
794         if (rc != 0) {
795                 cYAML_build_error(-1, -1, "parser", "set",
796                                   "cannot parse tiny_buffers value", &err_rc);
797                 cYAML_print_tree2file(stderr, err_rc);
798                 cYAML_free_tree(err_rc);
799                 return -1;
800         }
801
802         rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
803         if (rc != LUSTRE_CFG_RC_NO_ERR)
804                 cYAML_print_tree2file(stderr, err_rc);
805
806         cYAML_free_tree(err_rc);
807
808         return rc;
809 }
810
811 static int jt_set_small(int argc, char **argv)
812 {
813         long int value;
814         int rc;
815         struct cYAML *err_rc = NULL;
816
817         rc = check_cmd(set_cmds, "set", "small_buffers", 2, argc, argv);
818         if (rc)
819                 return rc;
820
821         rc = parse_long(argv[1], &value);
822         if (rc != 0) {
823                 cYAML_build_error(-1, -1, "parser", "set",
824                                   "cannot parse small_buffers value", &err_rc);
825                 cYAML_print_tree2file(stderr, err_rc);
826                 cYAML_free_tree(err_rc);
827                 return -1;
828         }
829
830         rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
831         if (rc != LUSTRE_CFG_RC_NO_ERR)
832                 cYAML_print_tree2file(stderr, err_rc);
833
834         cYAML_free_tree(err_rc);
835
836         return rc;
837 }
838
839 static int jt_set_large(int argc, char **argv)
840 {
841         long int value;
842         int rc;
843         struct cYAML *err_rc = NULL;
844
845         rc = check_cmd(set_cmds, "set", "large_buffers", 2, argc, argv);
846         if (rc)
847                 return rc;
848
849         rc = parse_long(argv[1], &value);
850         if (rc != 0) {
851                 cYAML_build_error(-1, -1, "parser", "set",
852                                   "cannot parse large_buffers value", &err_rc);
853                 cYAML_print_tree2file(stderr, err_rc);
854                 cYAML_free_tree(err_rc);
855                 return -1;
856         }
857
858         rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
859         if (rc != LUSTRE_CFG_RC_NO_ERR)
860                 cYAML_print_tree2file(stderr, err_rc);
861
862         cYAML_free_tree(err_rc);
863
864         return rc;
865 }
866
867 static int jt_set_routing(int argc, char **argv)
868 {
869         long int value;
870         struct cYAML *err_rc = NULL;
871         int rc;
872
873         rc = check_cmd(set_cmds, "set", "routing", 2, argc, argv);
874         if (rc)
875                 return rc;
876
877         rc = parse_long(argv[1], &value);
878         if (rc != 0 || (value != 0 && value != 1)) {
879                 cYAML_build_error(-1, -1, "parser", "set",
880                                   "cannot parse routing value.\n"
881                                   "must be 0 for disable or 1 for enable",
882                                   &err_rc);
883                 cYAML_print_tree2file(stderr, err_rc);
884                 cYAML_free_tree(err_rc);
885                 return -1;
886         }
887
888         rc = lustre_lnet_enable_routing(value, -1, &err_rc);
889
890         if (rc != LUSTRE_CFG_RC_NO_ERR)
891                 cYAML_print_tree2file(stderr, err_rc);
892
893         cYAML_free_tree(err_rc);
894
895         return rc;
896 }
897
898 static int jt_set_max_recovery_ping_interval(int argc, char **argv)
899 {
900         long int value;
901         int rc;
902         struct cYAML *err_rc = NULL;
903
904         rc = check_cmd(set_cmds, "set", "maximum recovery_interval", 2, argc, argv);
905         if (rc)
906                 return rc;
907
908         rc = parse_long(argv[1], &value);
909         if (rc != 0) {
910                 cYAML_build_error(-1, -1, "parser", "set",
911                                   "cannot parse maximum recovery interval value",
912                                   &err_rc);
913                 cYAML_print_tree2file(stderr, err_rc);
914                 cYAML_free_tree(err_rc);
915                 return -1;
916         }
917
918         rc = lustre_lnet_config_max_recovery_ping_interval(value, -1, &err_rc);
919         if (rc != LUSTRE_CFG_RC_NO_ERR)
920                 cYAML_print_tree2file(stderr, err_rc);
921
922         cYAML_free_tree(err_rc);
923
924         return rc;
925 }
926
927
928 static int jt_config_lnet(int argc, char **argv)
929 {
930         struct cYAML *err_rc = NULL;
931         bool load_mod_params = false;
932         int rc, opt;
933
934         const char *const short_options = "a";
935         static const struct option long_options[] = {
936                 { .name = "all",  .has_arg = no_argument, .val = 'a' },
937                 { .name = NULL }
938         };
939
940         rc = check_cmd(lnet_cmds, "lnet", "configure", 0, argc, argv);
941         if (rc)
942                 return rc;
943
944         while ((opt = getopt_long(argc, argv, short_options,
945                                    long_options, NULL)) != -1) {
946                 switch (opt) {
947                 case 'a':
948                         load_mod_params = true;
949                         break;
950                 default:
951                         return 0;
952                 }
953         }
954
955         rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
956                                           -1, &err_rc);
957
958         if (rc != LUSTRE_CFG_RC_NO_ERR)
959                 cYAML_print_tree2file(stderr, err_rc);
960
961         cYAML_free_tree(err_rc);
962
963         return rc;
964 }
965
966 static int jt_unconfig_lnet(int argc, char **argv)
967 {
968         struct cYAML *err_rc = NULL;
969         int rc;
970
971         rc = check_cmd(lnet_cmds, "lnet", "unconfigure", 0, argc, argv);
972         if (rc)
973                 return rc;
974
975         rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
976
977         if (rc != LUSTRE_CFG_RC_NO_ERR)
978                 cYAML_print_tree2file(stderr, err_rc);
979
980         cYAML_free_tree(err_rc);
981
982         return rc;
983 }
984 static int jt_add_route(int argc, char **argv)
985 {
986         char *network = NULL, *gateway = NULL;
987         long int hop = -1, prio = -1, sen = -1;
988         struct cYAML *err_rc = NULL;
989         int rc, opt;
990
991         const char *const short_options = "n:g:c:p:";
992         static const struct option long_options[] = {
993         { .name = "net",       .has_arg = required_argument, .val = 'n' },
994         { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
995         { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
996         { .name = "priority",  .has_arg = required_argument, .val = 'p' },
997         { .name = "health_sensitivity",  .has_arg = required_argument, .val = 's' },
998         { .name = NULL } };
999
1000         rc = check_cmd(route_cmds, "route", "add", 0, argc, argv);
1001         if (rc)
1002                 return rc;
1003
1004         while ((opt = getopt_long(argc, argv, short_options,
1005                                    long_options, NULL)) != -1) {
1006                 switch (opt) {
1007                 case 'n':
1008                         network = optarg;
1009                         break;
1010                 case 'g':
1011                         gateway = optarg;
1012                         break;
1013                 case 'c':
1014                         rc = parse_long(optarg, &hop);
1015                         if (rc != 0) {
1016                                 /* ignore option */
1017                                 hop = -1;
1018                                 continue;
1019                         }
1020                         break;
1021                 case 'p':
1022                         rc = parse_long(optarg, &prio);
1023                         if (rc != 0) {
1024                                 /* ingore option */
1025                                 prio = -1;
1026                                 continue;
1027                         }
1028                         break;
1029                 case 's':
1030                         rc = parse_long(optarg, &sen);
1031                         if (rc != 0) {
1032                                 /* ingore option */
1033                                 sen = -1;
1034                                 continue;
1035                         }
1036                         break;
1037
1038                 case '?':
1039                         print_help(route_cmds, "route", "add");
1040                 default:
1041                         return 0;
1042                 }
1043         }
1044
1045         rc = lustre_lnet_config_route(network, gateway, hop, prio, sen, -1,
1046                                       &err_rc);
1047
1048         if (rc != LUSTRE_CFG_RC_NO_ERR)
1049                 cYAML_print_tree2file(stderr, err_rc);
1050
1051         cYAML_free_tree(err_rc);
1052
1053         return rc;
1054 }
1055
1056 static int jt_add_ni(int argc, char **argv)
1057 {
1058         char *ip2net = NULL;
1059         long int pto = -1, pc = -1, pbc = -1, cre = -1, cpp = -1;
1060         struct cYAML *err_rc = NULL;
1061         int rc, opt, cpt_rc = -1;
1062         struct lnet_dlc_network_descr nw_descr;
1063         struct cfs_expr_list *global_cpts = NULL;
1064         struct lnet_ioctl_config_lnd_tunables tunables;
1065         bool found = false;
1066         bool skip_mr_route_setup = false;
1067
1068         memset(&tunables, 0, sizeof(tunables));
1069         lustre_lnet_init_nw_descr(&nw_descr);
1070
1071         const char *const short_options = "b:c:i:k:m:n:p:r:s:t:";
1072         static const struct option long_options[] = {
1073         { .name = "peer-buffer-credits",
1074                                   .has_arg = required_argument, .val = 'b' },
1075         { .name = "peer-credits", .has_arg = required_argument, .val = 'c' },
1076         { .name = "if",           .has_arg = required_argument, .val = 'i' },
1077         { .name = "skip-mr-route-setup",
1078                                   .has_arg = no_argument, .val = 'k' },
1079         { .name = "conns-per-peer",
1080                                   .has_arg = required_argument, .val = 'm' },
1081         { .name = "net",          .has_arg = required_argument, .val = 'n' },
1082         { .name = "ip2net",       .has_arg = required_argument, .val = 'p' },
1083         { .name = "credits",      .has_arg = required_argument, .val = 'r' },
1084         { .name = "cpt",          .has_arg = required_argument, .val = 's' },
1085         { .name = "peer-timeout", .has_arg = required_argument, .val = 't' },
1086         { .name = NULL } };
1087
1088         rc = check_cmd(net_cmds, "net", "add", 0, argc, argv);
1089         if (rc)
1090                 return rc;
1091
1092         while ((opt = getopt_long(argc, argv, short_options,
1093                                    long_options, NULL)) != -1) {
1094                 switch (opt) {
1095                 case 'b':
1096                         rc = parse_long(optarg, &pbc);
1097                         if (rc != 0) {
1098                                 /* ignore option */
1099                                 pbc = -1;
1100                                 continue;
1101                         }
1102                         break;
1103                 case 'c':
1104                         rc = parse_long(optarg, &pc);
1105                         if (rc != 0) {
1106                                 /* ignore option */
1107                                 pc = -1;
1108                                 continue;
1109                         }
1110                         break;
1111                 case 'i':
1112                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
1113                         if (rc != 0) {
1114                                 cYAML_build_error(-1, -1, "ni", "add",
1115                                                 "bad interface list",
1116                                                 &err_rc);
1117                                 goto failed;
1118                         }
1119                         break;
1120                 case 'k':
1121                         skip_mr_route_setup = true;
1122                         break;
1123                 case 'm':
1124                         rc = parse_long(optarg, &cpp);
1125                         if (rc != 0) {
1126                                 /* ignore option */
1127                                 cpp = -1;
1128                                 continue;
1129                         }
1130                         break;
1131
1132                 case 'n':
1133                         nw_descr.nw_id = libcfs_str2net(optarg);
1134                         break;
1135                 case 'p':
1136                         ip2net = optarg;
1137                         break;
1138                 case 'r':
1139                         rc = parse_long(optarg, &cre);
1140                         if (rc != 0) {
1141                                 /* ignore option */
1142                                 cre = -1;
1143                                 continue;
1144                         }
1145                         break;
1146                 case 's':
1147                         cpt_rc = cfs_expr_list_parse(optarg,
1148                                                      strlen(optarg), 0,
1149                                                      UINT_MAX, &global_cpts);
1150                         break;
1151                 case 't':
1152                         rc = parse_long(optarg, &pto);
1153                         if (rc != 0) {
1154                                 /* ignore option */
1155                                 pto = -1;
1156                                 continue;
1157                         }
1158                         break;
1159                 case '?':
1160                         print_help(net_cmds, "net", "add");
1161                 default:
1162                         return 0;
1163                 }
1164         }
1165
1166         if (pto > 0 || pc > 0 || pbc > 0 || cre > 0 || cpp > -1) {
1167                 tunables.lt_cmn.lct_peer_timeout = pto;
1168                 tunables.lt_cmn.lct_peer_tx_credits = pc;
1169                 tunables.lt_cmn.lct_peer_rtr_credits = pbc;
1170                 tunables.lt_cmn.lct_max_tx_credits = cre;
1171                 found = true;
1172         }
1173
1174         rc = lustre_lnet_config_ni(&nw_descr,
1175                                    (cpt_rc == 0) ? global_cpts: NULL,
1176                                    ip2net, (found) ? &tunables : NULL,
1177                                    cpp, -1, &err_rc);
1178
1179         if (global_cpts != NULL)
1180                 cfs_expr_list_free(global_cpts);
1181
1182 failed:
1183         if (rc != LUSTRE_CFG_RC_NO_ERR)
1184                 cYAML_print_tree2file(stderr, err_rc);
1185
1186         cYAML_free_tree(err_rc);
1187
1188         if (rc == LUSTRE_CFG_RC_NO_ERR && !skip_mr_route_setup) {
1189                 err_rc = NULL;
1190                 rc = lustre_lnet_setup_mrrouting(&err_rc);
1191
1192                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1193                         cYAML_print_tree2file(stderr, err_rc);
1194
1195                 cYAML_free_tree(err_rc);
1196         }
1197
1198         return rc;
1199 }
1200
1201 static int jt_del_route(int argc, char **argv)
1202 {
1203         char *network = NULL, *gateway = NULL;
1204         struct cYAML *err_rc = NULL;
1205         int rc, opt;
1206
1207         const char *const short_options = "n:g:";
1208         static const struct option long_options[] = {
1209                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
1210                 { .name = "gateway", .has_arg = required_argument, .val = 'g' },
1211                 { .name = NULL } };
1212
1213         rc = check_cmd(route_cmds, "route", "del", 0, argc, argv);
1214         if (rc)
1215                 return rc;
1216
1217         while ((opt = getopt_long(argc, argv, short_options,
1218                                    long_options, NULL)) != -1) {
1219                 switch (opt) {
1220                 case 'n':
1221                         network = optarg;
1222                         break;
1223                 case 'g':
1224                         gateway = optarg;
1225                         break;
1226                 case '?':
1227                         print_help(route_cmds, "route", "del");
1228                 default:
1229                         return 0;
1230                 }
1231         }
1232
1233         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
1234
1235         if (rc != LUSTRE_CFG_RC_NO_ERR)
1236                 cYAML_print_tree2file(stderr, err_rc);
1237
1238         cYAML_free_tree(err_rc);
1239
1240         return rc;
1241 }
1242
1243 static int jt_del_ni(int argc, char **argv)
1244 {
1245         struct cYAML *err_rc = NULL;
1246         int rc, opt;
1247         struct lnet_dlc_network_descr nw_descr;
1248
1249         lustre_lnet_init_nw_descr(&nw_descr);
1250
1251         const char *const short_options = "n:i:";
1252         static const struct option long_options[] = {
1253         { .name = "net",        .has_arg = required_argument,   .val = 'n' },
1254         { .name = "if",         .has_arg = required_argument,   .val = 'i' },
1255         { .name = NULL } };
1256
1257         rc = check_cmd(net_cmds, "net", "del", 0, argc, argv);
1258         if (rc)
1259                 return rc;
1260
1261         while ((opt = getopt_long(argc, argv, short_options,
1262                                    long_options, NULL)) != -1) {
1263                 switch (opt) {
1264                 case 'n':
1265                         nw_descr.nw_id = libcfs_str2net(optarg);
1266                         break;
1267                 case 'i':
1268                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
1269                         if (rc != 0) {
1270                                 cYAML_build_error(-1, -1, "ni", "add",
1271                                                 "bad interface list",
1272                                                 &err_rc);
1273                                 goto out;
1274                         }
1275                         break;
1276                 case '?':
1277                         print_help(net_cmds, "net", "del");
1278                 default:
1279                         return 0;
1280                 }
1281         }
1282
1283         rc = lustre_lnet_del_ni(&nw_descr, -1, &err_rc);
1284
1285 out:
1286         if (rc != LUSTRE_CFG_RC_NO_ERR)
1287                 cYAML_print_tree2file(stderr, err_rc);
1288
1289         cYAML_free_tree(err_rc);
1290
1291         return rc;
1292 }
1293
1294 static int jt_show_route(int argc, char **argv)
1295 {
1296         char *network = NULL, *gateway = NULL;
1297         long int hop = -1, prio = -1;
1298         int detail = 0, rc, opt;
1299         struct cYAML *err_rc = NULL, *show_rc = NULL;
1300
1301         const char *const short_options = "n:g:h:p:v";
1302         static const struct option long_options[] = {
1303         { .name = "net",       .has_arg = required_argument, .val = 'n' },
1304         { .name = "gateway",   .has_arg = required_argument, .val = 'g' },
1305         { .name = "hop-count", .has_arg = required_argument, .val = 'c' },
1306         { .name = "priority",  .has_arg = required_argument, .val = 'p' },
1307         { .name = "verbose",   .has_arg = no_argument,       .val = 'v' },
1308         { .name = NULL } };
1309
1310         rc = check_cmd(route_cmds, "route", "show", 0, argc, argv);
1311         if (rc)
1312                 return rc;
1313
1314         while ((opt = getopt_long(argc, argv, short_options,
1315                                    long_options, NULL)) != -1) {
1316                 switch (opt) {
1317                 case 'n':
1318                         network = optarg;
1319                         break;
1320                 case 'g':
1321                         gateway = optarg;
1322                         break;
1323                 case 'c':
1324                         rc = parse_long(optarg, &hop);
1325                         if (rc != 0) {
1326                                 /* ignore option */
1327                                 hop = -1;
1328                                 continue;
1329                         }
1330                         break;
1331                 case 'p':
1332                         rc = parse_long(optarg, &prio);
1333                         if (rc != 0) {
1334                                 /* ignore option */
1335                                 prio = -1;
1336                                 continue;
1337                         }
1338                         break;
1339                 case 'v':
1340                         detail = 1;
1341                         break;
1342                 case '?':
1343                         print_help(route_cmds, "route", "show");
1344                 default:
1345                         return 0;
1346                 }
1347         }
1348
1349         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
1350                                     &show_rc, &err_rc, false);
1351
1352         if (rc != LUSTRE_CFG_RC_NO_ERR)
1353                 cYAML_print_tree2file(stderr, err_rc);
1354         else if (show_rc)
1355                 cYAML_print_tree(show_rc);
1356
1357         cYAML_free_tree(err_rc);
1358         cYAML_free_tree(show_rc);
1359
1360         return rc;
1361 }
1362
1363 static int set_value_helper(int argc, char **argv,
1364                             int (*cb)(int, bool, char*, int, struct cYAML**))
1365 {
1366         char *nid = NULL;
1367         long int healthv = -1;
1368         bool all = false;
1369         int rc, opt;
1370         struct cYAML *err_rc = NULL;
1371
1372         const char *const short_options = "t:n:a";
1373         static const struct option long_options[] = {
1374                 { .name = "nid", .has_arg = required_argument, .val = 'n' },
1375                 { .name = "health", .has_arg = required_argument, .val = 't' },
1376                 { .name = "all", .has_arg = no_argument, .val = 'a' },
1377                 { .name = NULL } };
1378
1379         rc = check_cmd(net_cmds, "net", "set", 0, argc, argv);
1380         if (rc)
1381                 return rc;
1382
1383         while ((opt = getopt_long(argc, argv, short_options,
1384                                    long_options, NULL)) != -1) {
1385                 switch (opt) {
1386                 case 'n':
1387                         nid = optarg;
1388                         break;
1389                 case 't':
1390                         if (parse_long(optarg, &healthv) != 0)
1391                                 healthv = -1;
1392                         break;
1393                 case 'a':
1394                         all = true;
1395                         break;
1396                 default:
1397                         return 0;
1398                 }
1399         }
1400
1401         rc = cb(healthv, all, nid, -1, &err_rc);
1402
1403         if (rc != LUSTRE_CFG_RC_NO_ERR)
1404                 cYAML_print_tree2file(stderr, err_rc);
1405
1406         cYAML_free_tree(err_rc);
1407
1408         return rc;
1409 }
1410
1411 static int jt_set_ni_value(int argc, char **argv)
1412 {
1413         char *nid = NULL;
1414         long int healthv = -1, cpp = -1;
1415         bool all = false;
1416         int rc, opt;
1417         struct cYAML *err_rc = NULL;
1418
1419         const char *const short_options = "a:m:n:t:";
1420         static const struct option long_options[] = {
1421         { .name = "all",            .has_arg = no_argument,       .val = 'a' },
1422         { .name = "conns-per-peer", .has_arg = required_argument, .val = 'm' },
1423         { .name = "nid",            .has_arg = required_argument, .val = 'n' },
1424         { .name = "health",         .has_arg = required_argument, .val = 't' },
1425         { .name = NULL } };
1426
1427         rc = check_cmd(net_cmds, "net", "set", 0, argc, argv);
1428         if (rc)
1429                 return rc;
1430
1431         while ((opt = getopt_long(argc, argv, short_options,
1432                                    long_options, NULL)) != -1) {
1433                 switch (opt) {
1434                 case 'a':
1435                         all = true;
1436                         break;
1437                 case 'm':
1438                         rc = parse_long(optarg, &cpp);
1439                         if (rc != 0) {
1440                                 /* ignore option */
1441                                 cpp = -1;
1442                                 continue;
1443                         }
1444                         break;
1445                 case 'n':
1446                         nid = optarg;
1447                         break;
1448                 case 't':
1449                         if (parse_long(optarg, &healthv) != 0) {
1450                                 /* ignore option */
1451                                 healthv = -1;
1452                                 continue;
1453                         }
1454                         break;
1455                 default:
1456                         return 0;
1457                 }
1458         }
1459
1460         if (cpp > -1)
1461                 rc = lustre_lnet_config_ni_conns_per_peer(cpp, all, nid,
1462                                                           -1, &err_rc);
1463         if (healthv > -1)
1464                 rc = lustre_lnet_config_ni_healthv(healthv, all, nid,
1465                                                    -1, &err_rc);
1466
1467         if (rc != LUSTRE_CFG_RC_NO_ERR)
1468                 cYAML_print_tree2file(stderr, err_rc);
1469
1470         cYAML_free_tree(err_rc);
1471
1472         return rc;
1473 }
1474
1475 static int jt_set_peer_ni_value(int argc, char **argv)
1476 {
1477         return set_value_helper(argc, argv, lustre_lnet_config_peer_ni_healthv);
1478 }
1479
1480 static int jt_show_recovery(int argc, char **argv)
1481 {
1482         int rc, opt;
1483         struct cYAML *err_rc = NULL, *show_rc = NULL;
1484
1485         const char *const short_options = "lp";
1486         static const struct option long_options[] = {
1487                 { .name = "local", .has_arg = no_argument, .val = 'l' },
1488                 { .name = "peer", .has_arg = no_argument, .val = 'p' },
1489                 { .name = NULL } };
1490
1491         rc = check_cmd(debug_cmds, "recovery", NULL, 0, argc, argv);
1492         if (rc)
1493                 return rc;
1494
1495         while ((opt = getopt_long(argc, argv, short_options,
1496                                    long_options, NULL)) != -1) {
1497                 switch (opt) {
1498                 case 'l':
1499                         rc = lustre_lnet_show_local_ni_recovq(-1, &show_rc, &err_rc);
1500                         break;
1501                 case 'p':
1502                         rc = lustre_lnet_show_peer_ni_recovq(-1, &show_rc, &err_rc);
1503                         break;
1504                 default:
1505                         return 0;
1506                 }
1507         }
1508
1509         if (rc != LUSTRE_CFG_RC_NO_ERR)
1510                 cYAML_print_tree2file(stderr, err_rc);
1511         else if (show_rc)
1512                 cYAML_print_tree(show_rc);
1513
1514         cYAML_free_tree(err_rc);
1515         cYAML_free_tree(show_rc);
1516
1517         return rc;
1518 }
1519
1520 static int jt_show_net(int argc, char **argv)
1521 {
1522         char *network = NULL;
1523         int rc, opt;
1524         struct cYAML *err_rc = NULL, *show_rc = NULL;
1525         long int detail = 0;
1526
1527         const char *const short_options = "n:v";
1528         static const struct option long_options[] = {
1529                 { .name = "net",     .has_arg = required_argument, .val = 'n' },
1530                 { .name = "verbose", .has_arg = optional_argument, .val = 'v' },
1531                 { .name = NULL } };
1532
1533         rc = check_cmd(net_cmds, "net", "show", 0, argc, argv);
1534         if (rc)
1535                 return rc;
1536
1537         while ((opt = getopt_long(argc, argv, short_options,
1538                                    long_options, NULL)) != -1) {
1539                 switch (opt) {
1540                 case 'n':
1541                         network = optarg;
1542                         break;
1543                 case 'v':
1544                         if ((!optarg) && (argv[optind] != NULL) &&
1545                             (argv[optind][0] != '-')) {
1546                                 if (parse_long(argv[optind++], &detail) != 0)
1547                                         detail = 1;
1548                         } else {
1549                                 detail = 1;
1550                         }
1551                         break;
1552                 case '?':
1553                         print_help(net_cmds, "net", "show");
1554                 default:
1555                         return 0;
1556                 }
1557         }
1558
1559         rc = lustre_lnet_show_net(network, (int) detail, -1, &show_rc, &err_rc,
1560                                   false);
1561
1562         if (rc != LUSTRE_CFG_RC_NO_ERR)
1563                 cYAML_print_tree2file(stderr, err_rc);
1564         else if (show_rc)
1565                 cYAML_print_tree(show_rc);
1566
1567         cYAML_free_tree(err_rc);
1568         cYAML_free_tree(show_rc);
1569
1570         return rc;
1571 }
1572
1573 static int jt_show_routing(int argc, char **argv)
1574 {
1575         struct cYAML *err_rc = NULL, *show_rc = NULL;
1576         int rc;
1577
1578         rc = check_cmd(routing_cmds, "routing", "show", 0, argc, argv);
1579         if (rc)
1580                 return rc;
1581
1582         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, false);
1583
1584         if (rc != LUSTRE_CFG_RC_NO_ERR)
1585                 cYAML_print_tree2file(stderr, err_rc);
1586         else if (show_rc)
1587                 cYAML_print_tree(show_rc);
1588
1589         cYAML_free_tree(err_rc);
1590         cYAML_free_tree(show_rc);
1591
1592         return rc;
1593 }
1594
1595 static int jt_show_stats(int argc, char **argv)
1596 {
1597         int rc;
1598         struct cYAML *show_rc = NULL, *err_rc = NULL;
1599
1600         rc = check_cmd(stats_cmds, "stats", "show", 0, argc, argv);
1601         if (rc)
1602                 return rc;
1603
1604         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
1605
1606         if (rc != LUSTRE_CFG_RC_NO_ERR)
1607                 cYAML_print_tree2file(stderr, err_rc);
1608         else if (show_rc)
1609                 cYAML_print_tree(show_rc);
1610
1611         cYAML_free_tree(err_rc);
1612         cYAML_free_tree(show_rc);
1613
1614         return rc;
1615 }
1616
1617 static int jt_show_udsp(int argc, char **argv)
1618 {
1619         int idx = -1;
1620         int rc, opt;
1621         struct cYAML *err_rc = NULL, *show_rc = NULL;
1622
1623         const char *const short_options = "i:";
1624         static const struct option long_options[] = {
1625                 { .name = "idx", .has_arg = required_argument, .val = 'i' },
1626                 { .name = NULL }
1627         };
1628
1629         rc = check_cmd(udsp_cmds, "udsp", "show", 0, argc, argv);
1630         if (rc)
1631                 return rc;
1632
1633         while ((opt = getopt_long(argc, argv, short_options,
1634                                    long_options, NULL)) != -1) {
1635                 switch (opt) {
1636                 case 'i':
1637                         idx = atoi(optarg);
1638                         break;
1639                 case '?':
1640                         print_help(net_cmds, "net", "show");
1641                 default:
1642                         return 0;
1643                 }
1644         }
1645
1646         rc = lustre_lnet_show_udsp(idx, -1, &show_rc, &err_rc);
1647
1648         if (rc != LUSTRE_CFG_RC_NO_ERR)
1649                 cYAML_print_tree2file(stderr, err_rc);
1650         else if (show_rc)
1651                 cYAML_print_tree(show_rc);
1652
1653         cYAML_free_tree(err_rc);
1654         cYAML_free_tree(show_rc);
1655
1656         return rc;
1657 }
1658
1659 static int jt_show_global(int argc, char **argv)
1660 {
1661         int rc;
1662         struct cYAML *show_rc = NULL, *err_rc = NULL;
1663
1664         rc = check_cmd(global_cmds, "global", "show", 0, argc, argv);
1665         if (rc)
1666                 return rc;
1667
1668         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1669         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1670                 cYAML_print_tree2file(stderr, err_rc);
1671                 goto out;
1672         }
1673
1674         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
1675         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1676                 cYAML_print_tree2file(stderr, err_rc);
1677                 goto out;
1678         }
1679
1680         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
1681         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1682                 cYAML_print_tree2file(stderr, err_rc);
1683                 goto out;
1684         }
1685
1686         rc = lustre_lnet_show_drop_asym_route(-1, &show_rc, &err_rc);
1687         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1688                 cYAML_print_tree2file(stderr, err_rc);
1689                 goto out;
1690         }
1691
1692         rc = lustre_lnet_show_retry_count(-1, &show_rc, &err_rc);
1693         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1694                 cYAML_print_tree2file(stderr, err_rc);
1695                 goto out;
1696         }
1697
1698         rc = lustre_lnet_show_transaction_to(-1, &show_rc, &err_rc);
1699         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1700                 cYAML_print_tree2file(stderr, err_rc);
1701                 goto out;
1702         }
1703
1704         rc = lustre_lnet_show_hsensitivity(-1, &show_rc, &err_rc);
1705         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1706                 cYAML_print_tree2file(stderr, err_rc);
1707                 goto out;
1708         }
1709
1710         rc = lustre_lnet_show_recov_intrv(-1, &show_rc, &err_rc);
1711         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1712                 cYAML_print_tree2file(stderr, err_rc);
1713                 goto out;
1714         }
1715
1716         rc = lustre_lnet_show_rtr_sensitivity(-1, &show_rc, &err_rc);
1717         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1718                 cYAML_print_tree2file(stderr, err_rc);
1719                 goto out;
1720         }
1721
1722         rc = lustre_lnet_show_lnd_timeout(-1, &show_rc, &err_rc);
1723         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1724                 cYAML_print_tree2file(stderr, err_rc);
1725                 goto out;
1726         }
1727
1728         rc = lustre_lnet_show_response_tracking(-1, &show_rc, &err_rc);
1729         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1730                 cYAML_print_tree2file(stderr, err_rc);
1731                 goto out;
1732         }
1733
1734         rc = lustre_lnet_show_recovery_limit(-1, &show_rc, &err_rc);
1735         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1736                 cYAML_print_tree2file(stderr, err_rc);
1737                 goto out;
1738         }
1739
1740         rc = lustre_lnet_show_max_recovery_ping_interval(-1, &show_rc, &err_rc);
1741         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1742                 cYAML_print_tree2file(stderr, err_rc);
1743                 goto out;
1744         }
1745
1746         if (show_rc)
1747                 cYAML_print_tree(show_rc);
1748
1749 out:
1750         cYAML_free_tree(err_rc);
1751         cYAML_free_tree(show_rc);
1752
1753         return rc;
1754 }
1755
1756 static int jt_lnet(int argc, char **argv)
1757 {
1758         int rc;
1759
1760         rc = check_cmd(lnet_cmds, "lnet", NULL, 2, argc, argv);
1761         if (rc)
1762                 return rc;
1763
1764         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
1765 }
1766
1767 static int jt_route(int argc, char **argv)
1768 {
1769         int rc;
1770
1771         rc = check_cmd(route_cmds, "route", NULL, 2, argc, argv);
1772         if (rc)
1773                 return rc;
1774
1775         return Parser_execarg(argc - 1, &argv[1], route_cmds);
1776 }
1777
1778 static int jt_net(int argc, char **argv)
1779 {
1780         int rc;
1781
1782         rc = check_cmd(net_cmds, "net", NULL, 2, argc, argv);
1783         if (rc)
1784                 return rc;
1785
1786         return Parser_execarg(argc - 1, &argv[1], net_cmds);
1787 }
1788
1789 static int jt_routing(int argc, char **argv)
1790 {
1791         int rc;
1792
1793         rc = check_cmd(routing_cmds, "routing", NULL, 2, argc, argv);
1794         if (rc)
1795                 return rc;
1796
1797         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
1798 }
1799
1800 static int jt_stats(int argc, char **argv)
1801 {
1802         int rc;
1803
1804         rc = check_cmd(stats_cmds, "stats", NULL, 2, argc, argv);
1805         if (rc)
1806                 return rc;
1807
1808         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
1809 }
1810
1811 static int jt_debug(int argc, char **argv)
1812 {
1813         int rc;
1814
1815         rc = check_cmd(debug_cmds, "recovery", NULL, 2, argc, argv);
1816         if (rc)
1817                 return rc;
1818
1819         return Parser_execarg(argc - 1, &argv[1], debug_cmds);
1820 }
1821
1822 static int jt_global(int argc, char **argv)
1823 {
1824         int rc;
1825
1826         rc = check_cmd(global_cmds, "global", NULL, 2, argc, argv);
1827         if (rc)
1828                 return rc;
1829
1830         return Parser_execarg(argc - 1, &argv[1], global_cmds);
1831 }
1832
1833 static int jt_peers(int argc, char **argv)
1834 {
1835         int rc;
1836
1837         rc = check_cmd(peer_cmds, "peer", NULL, 2, argc, argv);
1838         if (rc)
1839                 return rc;
1840
1841         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
1842 }
1843
1844 static int jt_set(int argc, char **argv)
1845 {
1846         int rc;
1847
1848         rc = check_cmd(set_cmds, "set", NULL, 2, argc, argv);
1849         if (rc)
1850                 return rc;
1851
1852         return Parser_execarg(argc - 1, &argv[1], set_cmds);
1853 }
1854
1855 static int jt_udsp(int argc, char **argv)
1856 {
1857         int rc;
1858
1859         rc = check_cmd(udsp_cmds, "udsp", NULL, 2, argc, argv);
1860         if (rc)
1861                 return rc;
1862
1863         return Parser_execarg(argc - 1, &argv[1], udsp_cmds);
1864 }
1865
1866 static int jt_import(int argc, char **argv)
1867 {
1868         char *file = NULL;
1869         struct cYAML *err_rc = NULL;
1870         struct cYAML *show_rc = NULL;
1871         int rc = 0, return_rc = 0, opt, opt_found = 0;
1872         char cmd = 'a';
1873
1874         const char *const short_options = "adseh";
1875         static const struct option long_options[] = {
1876                 { .name = "add",  .has_arg = no_argument, .val = 'a' },
1877                 { .name = "del",  .has_arg = no_argument, .val = 'd' },
1878                 { .name = "show", .has_arg = no_argument, .val = 's' },
1879                 { .name = "exec", .has_arg = no_argument, .val = 'e' },
1880                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1881                 { .name = NULL } };
1882
1883         while ((opt = getopt_long(argc, argv, short_options,
1884                                    long_options, NULL)) != -1) {
1885                 opt_found = 1;
1886                 switch (opt) {
1887                 case 'a':
1888                         cmd = opt;
1889                         break;
1890                 case 'd':
1891                 case 's':
1892                         cmd = opt;
1893                         break;
1894                 case 'e':
1895                         cmd = opt;
1896                         break;
1897                 case 'h':
1898                         printf("import FILE\n"
1899                                "import < FILE : import a file\n"
1900                                "\t--add: add configuration\n"
1901                                "\t--del: delete configuration\n"
1902                                "\t--show: show configuration\n"
1903                                "\t--exec: execute command\n"
1904                                "\t--help: display this help\n"
1905                                "If no command option is given then --add"
1906                                " is assumed by default\n");
1907                         return 0;
1908                 default:
1909                         return 0;
1910                 }
1911         }
1912
1913         /* grab the file name if one exists */
1914         if (opt_found && argc == 3)
1915                 file = argv[2];
1916         else if (!opt_found && argc == 2)
1917                 file = argv[1];
1918
1919         switch (cmd) {
1920         case 'a':
1921                 rc = lustre_yaml_config(file, &err_rc);
1922                 return_rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1923                 cYAML_print_tree(show_rc);
1924                 cYAML_free_tree(show_rc);
1925                 break;
1926         case 'd':
1927                 rc = lustre_yaml_del(file, &err_rc);
1928                 break;
1929         case 's':
1930                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1931                 cYAML_print_tree(show_rc);
1932                 cYAML_free_tree(show_rc);
1933                 break;
1934         case 'e':
1935                 rc = lustre_yaml_exec(file, &show_rc, &err_rc);
1936                 cYAML_print_tree(show_rc);
1937                 cYAML_free_tree(show_rc);
1938                 break;
1939         }
1940
1941         if (rc || return_rc) {
1942                 cYAML_print_tree2file(stderr, err_rc);
1943                 cYAML_free_tree(err_rc);
1944         }
1945
1946         return rc;
1947 }
1948
1949 static int jt_export(int argc, char **argv)
1950 {
1951         struct cYAML *show_rc = NULL;
1952         struct cYAML *err_rc = NULL;
1953         int rc;
1954         FILE *f = NULL;
1955         int opt;
1956         bool backup = false;
1957         char *file = NULL;
1958
1959         const char *const short_options = "bh";
1960         static const struct option long_options[] = {
1961                 { .name = "backup", .has_arg = no_argument, .val = 'b' },
1962                 { .name = "help", .has_arg = no_argument, .val = 'h' },
1963                 { .name = NULL } };
1964
1965         while ((opt = getopt_long(argc, argv, short_options,
1966                                    long_options, NULL)) != -1) {
1967                 switch (opt) {
1968                 case 'b':
1969                         backup = true;
1970                         break;
1971                 case 'h':
1972                 default:
1973                         printf("export > FILE.yaml : export configuration\n"
1974                                "\t--backup: export only what's necessary for reconfig\n"
1975                                "\t--help: display this help\n");
1976                         return 0;
1977                 }
1978         }
1979
1980         if (backup && argc >= 3)
1981                 file = argv[2];
1982         else if (!backup && argc >= 2)
1983                 file = argv[1];
1984         else
1985                 f = stdout;
1986
1987         if (file) {
1988                 f = fopen(file, "w");
1989                 if (f == NULL)
1990                         return -1;
1991         }
1992
1993         rc = lustre_lnet_show_net(NULL, 2, -1, &show_rc, &err_rc, backup);
1994         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1995                 cYAML_print_tree2file(stderr, err_rc);
1996                 cYAML_free_tree(err_rc);
1997                 err_rc = NULL;
1998         }
1999
2000         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
2001                                     &err_rc, backup);
2002         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2003                 cYAML_print_tree2file(stderr, err_rc);
2004                 cYAML_free_tree(err_rc);
2005                 err_rc = NULL;
2006         }
2007
2008         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc, backup);
2009         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2010                 cYAML_print_tree2file(stderr, err_rc);
2011                 cYAML_free_tree(err_rc);
2012                 err_rc = NULL;
2013         }
2014
2015         rc = lustre_lnet_show_peer(NULL, 2, -1, &show_rc, &err_rc, backup);
2016         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2017                 cYAML_print_tree2file(stderr, err_rc);
2018                 cYAML_free_tree(err_rc);
2019                 err_rc = NULL;
2020         }
2021
2022         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
2023         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2024                 cYAML_print_tree2file(stderr, err_rc);
2025                 cYAML_free_tree(err_rc);
2026                 err_rc = NULL;
2027         }
2028
2029         rc = lustre_lnet_show_max_intf(-1, &show_rc, &err_rc);
2030         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2031                 cYAML_print_tree2file(stderr, err_rc);
2032                 cYAML_free_tree(err_rc);
2033                 err_rc = NULL;
2034         }
2035
2036         rc = lustre_lnet_show_discovery(-1, &show_rc, &err_rc);
2037         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2038                 cYAML_print_tree2file(stderr, err_rc);
2039                 cYAML_free_tree(err_rc);
2040                 err_rc = NULL;
2041         }
2042
2043         rc = lustre_lnet_show_drop_asym_route(-1, &show_rc, &err_rc);
2044         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2045                 cYAML_print_tree2file(stderr, err_rc);
2046                 cYAML_free_tree(err_rc);
2047                 err_rc = NULL;
2048         }
2049
2050         rc = lustre_lnet_show_retry_count(-1, &show_rc, &err_rc);
2051         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2052                 cYAML_print_tree2file(stderr, err_rc);
2053                 err_rc = NULL;
2054         }
2055
2056         rc = lustre_lnet_show_transaction_to(-1, &show_rc, &err_rc);
2057         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2058                 cYAML_print_tree2file(stderr, err_rc);
2059                 err_rc = NULL;
2060         }
2061
2062         rc = lustre_lnet_show_hsensitivity(-1, &show_rc, &err_rc);
2063         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2064                 cYAML_print_tree2file(stderr, err_rc);
2065                 err_rc = NULL;
2066         }
2067
2068         rc = lustre_lnet_show_recov_intrv(-1, &show_rc, &err_rc);
2069         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2070                 cYAML_print_tree2file(stderr, err_rc);
2071                 err_rc = NULL;
2072         }
2073
2074         rc = lustre_lnet_show_rtr_sensitivity(-1, &show_rc, &err_rc);
2075         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2076                 cYAML_print_tree2file(stderr, err_rc);
2077                 err_rc = NULL;
2078         }
2079
2080         rc = lustre_lnet_show_lnd_timeout(-1, &show_rc, &err_rc);
2081         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2082                 cYAML_print_tree2file(stderr, err_rc);
2083                 err_rc = NULL;
2084         }
2085
2086         rc = lustre_lnet_show_response_tracking(-1, &show_rc, &err_rc);
2087         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2088                 cYAML_print_tree2file(stderr, err_rc);
2089                 cYAML_free_tree(err_rc);
2090                 err_rc = NULL;
2091         }
2092
2093         rc = lustre_lnet_show_recovery_limit(-1, &show_rc, &err_rc);
2094         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2095                 cYAML_print_tree2file(stderr, err_rc);
2096                 cYAML_free_tree(err_rc);
2097                 err_rc = NULL;
2098         }
2099
2100         rc = lustre_lnet_show_max_recovery_ping_interval(-1, &show_rc, &err_rc);
2101         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2102                 cYAML_print_tree2file(stderr, err_rc);
2103                 cYAML_free_tree(err_rc);
2104                 err_rc = NULL;
2105         }
2106
2107         rc = lustre_lnet_show_udsp(-1, -1, &show_rc, &err_rc);
2108         if (rc != LUSTRE_CFG_RC_NO_ERR) {
2109                 cYAML_print_tree2file(stderr, err_rc);
2110                 cYAML_free_tree(err_rc);
2111                 err_rc = NULL;
2112         }
2113
2114         if (show_rc != NULL) {
2115                 cYAML_print_tree2file(f, show_rc);
2116                 cYAML_free_tree(show_rc);
2117         }
2118
2119         if (argc >= 2)
2120                 fclose(f);
2121
2122         return 0;
2123 }
2124
2125 static int jt_peer_nid_common(int argc, char **argv, int cmd)
2126 {
2127         int rc = LUSTRE_CFG_RC_NO_ERR, opt;
2128         bool is_mr = true;
2129         char *prim_nid = NULL, *nidstr = NULL;
2130         char err_str[LNET_MAX_STR_LEN] = "Error";
2131         struct cYAML *err_rc = NULL;
2132
2133         const char *const short_opts = "k:mn:";
2134         const struct option long_opts[] = {
2135         { .name = "prim_nid",   .has_arg = required_argument,   .val = 'k' },
2136         { .name = "non_mr",     .has_arg = no_argument,         .val = 'm' },
2137         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
2138         { .name = NULL } };
2139
2140         rc = check_cmd(peer_cmds, "peer", "add", 2, argc, argv);
2141         if (rc)
2142                 return rc;
2143
2144         while ((opt = getopt_long(argc, argv, short_opts,
2145                                   long_opts, NULL)) != -1) {
2146                 switch (opt) {
2147                 case 'k':
2148                         prim_nid = optarg;
2149                         break;
2150                 case 'n':
2151                         nidstr = optarg;
2152                         break;
2153                 case 'm':
2154                         if (cmd == LNETCTL_DEL_CMD) {
2155                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2156                                 snprintf(err_str, LNET_MAX_STR_LEN,
2157                                          "Unrecognized option '-%c'", opt);
2158                                 goto build_error;
2159                         }
2160                         is_mr = false;
2161                         break;
2162                 case '?':
2163                         print_help(peer_cmds, "peer",
2164                                    cmd == LNETCTL_ADD_CMD ? "add" : "del");
2165                 default:
2166                         return 0;
2167                 }
2168         }
2169
2170         rc = lustre_lnet_modify_peer(prim_nid, nidstr, is_mr, cmd,
2171                                      -1, &err_rc);
2172         if (rc != LUSTRE_CFG_RC_NO_ERR)
2173                 goto out;
2174
2175 build_error:
2176         cYAML_build_error(rc, -1, "peer",
2177                           cmd == LNETCTL_ADD_CMD ? "add" : "del",
2178                           err_str, &err_rc);
2179
2180 out:
2181         if (rc != LUSTRE_CFG_RC_NO_ERR)
2182                 cYAML_print_tree2file(stderr, err_rc);
2183
2184         cYAML_free_tree(err_rc);
2185
2186         return rc;
2187 }
2188
2189 static int jt_add_peer_nid(int argc, char **argv)
2190 {
2191         return jt_peer_nid_common(argc, argv, LNETCTL_ADD_CMD);
2192 }
2193
2194 static int jt_del_peer_nid(int argc, char **argv)
2195 {
2196         return jt_peer_nid_common(argc, argv, LNETCTL_DEL_CMD);
2197 }
2198
2199 static int jt_show_peer(int argc, char **argv)
2200 {
2201         char *nid = NULL;
2202         int rc, opt;
2203         struct cYAML *err_rc = NULL, *show_rc = NULL;
2204         long int detail = 0;
2205
2206         const char *const short_opts = "hn:v::";
2207         const struct option long_opts[] = {
2208         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
2209         { .name = "nid",        .has_arg = required_argument,   .val = 'n' },
2210         { .name = "verbose",    .has_arg = optional_argument,   .val = 'v' },
2211         { .name = NULL } };
2212
2213         rc = check_cmd(peer_cmds, "peer", "show", 1, argc, argv);
2214         if (rc)
2215                 return rc;
2216
2217         while ((opt = getopt_long(argc, argv, short_opts,
2218                                   long_opts, NULL)) != -1) {
2219                 switch (opt) {
2220                 case 'n':
2221                         nid = optarg;
2222                         break;
2223                 case 'v':
2224                         if ((!optarg) && (argv[optind] != NULL) &&
2225                             (argv[optind][0] != '-')) {
2226                                 if (parse_long(argv[optind++], &detail) != 0)
2227                                         detail = 1;
2228                         } else {
2229                                 detail = 1;
2230                         }
2231                         break;
2232                 case '?':
2233                         print_help(peer_cmds, "peer", "show");
2234                 default:
2235                         return 0;
2236                 }
2237         }
2238
2239         rc = lustre_lnet_show_peer(nid, (int) detail, -1, &show_rc, &err_rc,
2240                                    false);
2241
2242         if (rc != LUSTRE_CFG_RC_NO_ERR)
2243                 cYAML_print_tree2file(stderr, err_rc);
2244         else if (show_rc)
2245                 cYAML_print_tree(show_rc);
2246
2247         cYAML_free_tree(err_rc);
2248         cYAML_free_tree(show_rc);
2249
2250         return rc;
2251 }
2252
2253 static int jt_list_peer(int argc, char **argv)
2254 {
2255         int rc;
2256         struct cYAML *err_rc = NULL, *list_rc = NULL;
2257
2258         rc = check_cmd(peer_cmds, "peer", "list", 0, argc, argv);
2259         if (rc)
2260                 return rc;
2261
2262         rc = lustre_lnet_list_peer(-1, &list_rc, &err_rc);
2263
2264         if (rc != LUSTRE_CFG_RC_NO_ERR)
2265                 cYAML_print_tree2file(stderr, err_rc);
2266         else if (list_rc)
2267                 cYAML_print_tree(list_rc);
2268
2269         cYAML_free_tree(err_rc);
2270         cYAML_free_tree(list_rc);
2271
2272         return rc;
2273 }
2274
2275 static int jt_ping(int argc, char **argv)
2276 {
2277         struct cYAML *err_rc = NULL;
2278         struct cYAML *show_rc = NULL;
2279         int timeout = 1000;
2280         int rc = 0, opt;
2281         char *src_nidstr = NULL;
2282
2283         const char *const short_options = "hs:t:";
2284         const struct option long_options[] = {
2285         { .name = "help",       .has_arg = no_argument,         .val = 'h' },
2286         { .name = "timeout",    .has_arg = required_argument,   .val = 't' },
2287         { .name = "source",     .has_arg = required_argument,   .val = 's' },
2288         { .name = NULL } };
2289
2290         while ((opt = getopt_long(argc, argv, short_options,
2291                                   long_options, NULL)) != -1) {
2292                 switch (opt) {
2293                 case 's':
2294                         src_nidstr = optarg;
2295                         break;
2296                 case 't':
2297                         timeout = 1000 * atol(optarg);
2298                         break;
2299                 case 'h':
2300                         printf("ping nid[,nid,...]\n"
2301                                "\t --source: source nid\n"
2302                                "\t --timeout: ping timeout\n"
2303                                "\t --help: display this help\n");
2304                         return 0;
2305                 default:
2306                         return 0;
2307                 }
2308         }
2309
2310         for (; optind < argc; optind++)
2311                 rc = lustre_lnet_ping_nid(argv[optind], src_nidstr, timeout, -1,
2312                                           &show_rc, &err_rc);
2313
2314         if (show_rc)
2315                 cYAML_print_tree(show_rc);
2316
2317         if (err_rc)
2318                 cYAML_print_tree2file(stderr, err_rc);
2319
2320         cYAML_free_tree(err_rc);
2321         cYAML_free_tree(show_rc);
2322
2323         return rc;
2324 }
2325
2326 static int jt_discover(int argc, char **argv)
2327 {
2328         struct cYAML *err_rc = NULL;
2329         struct cYAML *show_rc = NULL;
2330         int force = 0;
2331         int rc = 0, opt;
2332
2333         const char *const short_options = "fh";
2334         const struct option long_options[] = {
2335                 { .name = "force",      .has_arg = no_argument, .val = 'f' },
2336                 { .name = "help",       .has_arg = no_argument, .val = 'h' },
2337                 { .name = NULL } };
2338
2339         while ((opt = getopt_long(argc, argv, short_options,
2340                                   long_options, NULL)) != -1) {
2341                 switch (opt) {
2342                 case 'f':
2343                         force = 1;
2344                         break;
2345                 case 'h':
2346                         printf("discover nid[,nid,...]\n"
2347                                "\t --force: force discovery\n"
2348                                "\t --help: display this help\n");
2349                         return 0;
2350                 default:
2351                         return 0;
2352                 }
2353         }
2354
2355         for (; optind < argc; optind++)
2356                 rc = lustre_lnet_discover_nid(argv[optind], force, -1, &show_rc,
2357                                               &err_rc);
2358
2359         if (show_rc)
2360                 cYAML_print_tree(show_rc);
2361
2362         if (err_rc)
2363                 cYAML_print_tree2file(stderr, err_rc);
2364
2365         cYAML_free_tree(err_rc);
2366         cYAML_free_tree(show_rc);
2367
2368         return rc;
2369 }
2370
2371 static int jt_add_udsp(int argc, char **argv)
2372 {
2373         char *src = NULL, *dst = NULL, *rte = NULL;
2374         struct cYAML *err_rc = NULL;
2375         union lnet_udsp_action udsp_action;
2376         long int idx = -1, priority = -1;
2377         int opt, rc = 0;
2378         char *action_type = "pref";
2379
2380         const char *const short_options = "s:d:r:p:i:";
2381         static const struct option long_options[] = {
2382         { .name = "src",         .has_arg = required_argument, .val = 's' },
2383         { .name = "dst",         .has_arg = required_argument, .val = 'd' },
2384         { .name = "rte",         .has_arg = required_argument, .val = 'r' },
2385         { .name = "priority",    .has_arg = required_argument, .val = 'p' },
2386         { .name = "idx",         .has_arg = required_argument, .val = 'i' },
2387         { .name = NULL } };
2388
2389         rc = check_cmd(udsp_cmds, "udsp", "add", 0, argc, argv);
2390         if (rc)
2391                 return rc;
2392
2393         while ((opt = getopt_long(argc, argv, short_options,
2394                                   long_options, NULL)) != -1) {
2395                 switch (opt) {
2396                 case 's':
2397                         src = optarg;
2398                         break;
2399                 case 'd':
2400                         dst = optarg;
2401                         break;
2402                 case 'r':
2403                         rte = optarg;
2404                         break;
2405                 case 'p':
2406                         rc = parse_long(optarg, &priority);
2407                         if (rc != 0)
2408                                 priority = -1;
2409                         action_type = "priority";
2410                         udsp_action.udsp_priority = priority;
2411                         break;
2412                 case 'i':
2413                         rc = parse_long(optarg, &idx);
2414                         if (rc != 0)
2415                                 idx = 0;
2416                         break;
2417                 case '?':
2418                         print_help(udsp_cmds, "udsp", "add");
2419                 default:
2420                         return 0;
2421                 }
2422         }
2423
2424         rc = lustre_lnet_add_udsp(src, dst, rte, action_type, &udsp_action,
2425                                   idx, -1, &err_rc);
2426
2427         if (rc != LUSTRE_CFG_RC_NO_ERR)
2428                 cYAML_print_tree2file(stderr, err_rc);
2429
2430         cYAML_free_tree(err_rc);
2431
2432         return rc;
2433 }
2434
2435 static int jt_del_udsp(int argc, char **argv)
2436 {
2437         struct cYAML *err_rc = NULL;
2438         long int idx = 0;
2439         int opt, rc = 0;
2440
2441         const char *const short_options = "i:";
2442         static const struct option long_options[] = {
2443         { .name = "idx",        .has_arg = required_argument, .val = 'i' },
2444         { .name = NULL } };
2445
2446         rc = check_cmd(udsp_cmds, "udsp", "del", 0, argc, argv);
2447         if (rc)
2448                 return rc;
2449
2450         while ((opt = getopt_long(argc, argv, short_options,
2451                                   long_options, NULL)) != -1) {
2452                 switch (opt) {
2453                 case 'i':
2454                         rc = parse_long(optarg, &idx);
2455                         if (rc != 0)
2456                                 idx = 0;
2457                         break;
2458                 case '?':
2459                         print_help(udsp_cmds, "udsp", "add");
2460                 default:
2461                         return 0;
2462                 }
2463         }
2464
2465         rc = lustre_lnet_del_udsp(idx, -1, &err_rc);
2466         if (rc != LUSTRE_CFG_RC_NO_ERR)
2467                 cYAML_print_tree2file(stderr, err_rc);
2468
2469         cYAML_free_tree(err_rc);
2470
2471         return rc;
2472 }
2473
2474 static int lnetctl_list_commands(int argc, char **argv)
2475 {
2476         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
2477
2478         Parser_list_commands(cmd_list, buffer, sizeof(buffer), NULL, 0, 4);
2479
2480         return 0;
2481 }
2482
2483 int main(int argc, char **argv)
2484 {
2485         int rc = 0;
2486         struct cYAML *err_rc = NULL;
2487
2488         rc = lustre_lnet_config_lib_init();
2489         if (rc < 0) {
2490                 cYAML_build_error(-1, -1, "lnetctl", "startup",
2491                                   "cannot register LNet device", &err_rc);
2492                 cYAML_print_tree2file(stderr, err_rc);
2493                 return rc;
2494         }
2495
2496         Parser_init("lnetctl > ", cmd_list);
2497         if (argc > 1) {
2498                 rc = Parser_execarg(argc - 1, &argv[1], cmd_list);
2499                 goto errorout;
2500         }
2501
2502         Parser_commands();
2503
2504 errorout:
2505         return rc;
2506 }