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