Whamcloud - gitweb
LU-7734 lnet: peer/peer_ni handling adjustments
[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, 2016, 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 <lnet/lnetctl.h>
34 #include <lnet/nidstr.h>
35 #include "cyaml/cyaml.h"
36 #include "lnetconfig/liblnetconfig.h"
37
38 #define LNET_CONFIGURE          true
39 #define LNET_UNCONFIGURE        false
40
41 static int jt_config_lnet(int argc, char **argv);
42 static int jt_unconfig_lnet(int argc, char **argv);
43 static int jt_add_route(int argc, char **argv);
44 static int jt_add_ni(int argc, char **argv);
45 static int jt_set_routing(int argc, char **argv);
46 static int jt_del_route(int argc, char **argv);
47 static int jt_del_ni(int argc, char **argv);
48 static int jt_show_route(int argc, char **argv);
49 static int jt_show_net(int argc, char **argv);
50 static int jt_show_routing(int argc, char **argv);
51 static int jt_show_stats(int argc, char **argv);
52 static int jt_show_peer(int argc, char **argv);
53 static int jt_show_numa(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_add_peer_nid(int argc, char **argv);
59 static int jt_del_peer_nid(int argc, char **argv);
60 /*static int jt_show_peer(int argc, char **argv);*/
61
62 command_t lnet_cmds[] = {
63         {"configure", jt_config_lnet, 0, "configure lnet\n"
64          "\t--all: load NI configuration from module parameters\n"},
65         {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
66         { 0, 0, 0, NULL }
67 };
68
69 command_t route_cmds[] = {
70         {"add", jt_add_route, 0, "add a route\n"
71          "\t--net: net name (e.g. tcp0)\n"
72          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
73          "\t--hop: number to final destination (1 < hops < 255)\n"
74          "\t--priority: priority of route (0 - highest prio\n"},
75         {"del", jt_del_route, 0, "delete a route\n"
76          "\t--net: net name (e.g. tcp0)\n"
77          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
78         {"show", jt_show_route, 0, "show routes\n"
79          "\t--net: net name (e.g. tcp0) to filter on\n"
80          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
81          "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
82          "\t--priority: priority of route (0 - highest prio to filter on\n"
83          "\t--verbose: display detailed output per route\n"},
84         { 0, 0, 0, NULL }
85 };
86
87 command_t net_cmds[] = {
88         {"add", jt_add_ni, 0, "add a network\n"
89          "\t--net: net name (e.g. tcp0)\n"
90          "\t--if: physical interface (e.g. eth0)\n"
91          "\t--ip2net: specify networks based on IP address patterns\n"
92          "\t--peer-timeout: time to wait before declaring a peer dead\n"
93          "\t--peer-credits: define the max number of inflight messages\n"
94          "\t--peer-buffer-credits: the number of buffer credits per peer\n"
95          "\t--credits: Network Interface credits\n"
96          "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
97         {"del", jt_del_ni, 0, "delete a network\n"
98          "\t--net: net name (e.g. tcp0)\n"
99          "\t--if: physical interface (e.g. eth0)\n"},
100         {"show", jt_show_net, 0, "show networks\n"
101          "\t--net: net name (e.g. tcp0) to filter on\n"
102          "\t--verbose: display detailed output per network\n"},
103         { 0, 0, 0, NULL }
104 };
105
106 command_t routing_cmds[] = {
107         {"show", jt_show_routing, 0, "show routing information\n"},
108         { 0, 0, 0, NULL }
109 };
110
111 command_t stats_cmds[] = {
112         {"show", jt_show_stats, 0, "show LNET statistics\n"},
113         { 0, 0, 0, NULL }
114 };
115
116 command_t numa_cmds[] = {
117         {"show", jt_show_numa, 0, "show NUMA range\n"},
118         { 0, 0, 0, NULL }
119 };
120
121 command_t set_cmds[] = {
122         {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
123          "\tVALUE must be greater than 0\n"},
124         {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
125          "\tVALUE must be greater than 0\n"},
126         {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
127          "\tVALUE must be greater than 0\n"},
128         {"routing", jt_set_routing, 0, "enable/disable routing\n"
129          "\t0 - disable routing\n"
130          "\t1 - enable routing\n"},
131         {"numa_range", jt_set_numa, 0, "set NUMA range for NI selection\n"
132          "\tVALUE must be at least 0\n"},
133         { 0, 0, 0, NULL }
134 };
135
136 command_t peer_cmds[] = {
137         {"add", jt_add_peer_nid, 0, "add a peer NID\n"
138          "\t--key_nid: NID to identify peer. If not provided then the first\n"
139          "\t           NID in the list becomes the key NID of a newly created\n"
140          "\t           peer. \n"
141          "\t--nid: one or more peer NIDs\n"
142          "\t--non_mr: create this peer as not Multi-Rail capable\n"},
143         {"del", jt_del_peer_nid, 0, "delete a peer NID\n"
144          "\t--key_nid: NID to identify peer.\n"
145          "\t--nid: list of NIDs to remove. If none provided,\n"
146          "\t       peer is deleted\n"},
147         {"show", jt_show_peer, 0, "show peer credits\n"
148          "\t--primary_nid: NID of peer to filter on.\n"},
149         { 0, 0, 0, NULL }
150 };
151
152 static inline void print_help(const command_t cmds[], const char *cmd_type,
153                               const char *pc_name)
154 {
155         const command_t *cmd;
156
157         for (cmd = cmds; cmd->pc_name; cmd++) {
158                 if (pc_name != NULL &&
159                     strcmp(cmd->pc_name, pc_name) == 0) {
160                         printf("%s %s: %s\n", cmd_type, cmd->pc_name,
161                                cmd->pc_help);
162                         return;
163                 } else if (pc_name != NULL) {
164                         continue;
165                 }
166                 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
167         }
168 }
169
170 static int parse_long(const char *number, long int *value)
171 {
172         char *end;
173
174         *value = strtol(number,  &end, 0);
175         if (end != NULL && *end != 0)
176                 return -1;
177
178         return 0;
179 }
180
181 static int handle_help(const command_t *cmd_list, const char *cmd,
182                        const char *sub_cmd, int argc, char **argv)
183 {
184         int opt;
185         int rc = -1;
186         optind = 0;
187         opterr = 0;
188
189         const char *const short_options = "h";
190         const struct option long_options[] = {
191                 { "help", 0, NULL, 'h' },
192                 { NULL, 0, NULL, 0 },
193         };
194
195         while ((opt = getopt_long(argc, argv, short_options,
196                                   long_options, NULL)) != -1) {
197                 switch (opt) {
198                 case 'h':
199                         print_help(cmd_list, cmd, sub_cmd);
200                         rc = 0;
201                         break;
202                 default:
203                         rc = -1;
204                         break;
205                 }
206         }
207
208         opterr = 1;
209         optind = 0;
210         return rc;
211 }
212
213 static int jt_set_numa(int argc, char **argv)
214 {
215         long int value;
216         int rc;
217         struct cYAML *err_rc = NULL;
218
219         if (handle_help(set_cmds, "set", "numa_range", argc, argv) == 0)
220                 return 0;
221
222         rc = parse_long(argv[1], &value);
223         if (rc != 0) {
224                 cYAML_build_error(-1, -1, "parser", "set",
225                                   "cannot parse numa_range value", &err_rc);
226                 cYAML_print_tree2file(stderr, err_rc);
227                 cYAML_free_tree(err_rc);
228                 return -1;
229         }
230
231         rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
232         if (rc != LUSTRE_CFG_RC_NO_ERR)
233                 cYAML_print_tree2file(stderr, err_rc);
234
235         cYAML_free_tree(err_rc);
236
237         return rc;
238 }
239
240 static int jt_set_tiny(int argc, char **argv)
241 {
242         long int value;
243         int rc;
244         struct cYAML *err_rc = NULL;
245
246         if (handle_help(set_cmds, "set", "tiny_buffers", argc, argv) == 0)
247                 return 0;
248
249         rc = parse_long(argv[1], &value);
250         if (rc != 0) {
251                 cYAML_build_error(-1, -1, "parser", "set",
252                                   "cannot parse tiny_buffers value", &err_rc);
253                 cYAML_print_tree2file(stderr, err_rc);
254                 cYAML_free_tree(err_rc);
255                 return -1;
256         }
257
258         rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
259         if (rc != LUSTRE_CFG_RC_NO_ERR)
260                 cYAML_print_tree2file(stderr, err_rc);
261
262         cYAML_free_tree(err_rc);
263
264         return rc;
265 }
266
267 static int jt_set_small(int argc, char **argv)
268 {
269         long int value;
270         int rc;
271         struct cYAML *err_rc = NULL;
272
273         if (handle_help(set_cmds, "set", "small_buffers", argc, argv) == 0)
274                 return 0;
275
276         rc = parse_long(argv[1], &value);
277         if (rc != 0) {
278                 cYAML_build_error(-1, -1, "parser", "set",
279                                   "cannot parse small_buffers value", &err_rc);
280                 cYAML_print_tree2file(stderr, err_rc);
281                 cYAML_free_tree(err_rc);
282                 return -1;
283         }
284
285         rc = lustre_lnet_config_buffers(-1, value, -1, -1, &err_rc);
286         if (rc != LUSTRE_CFG_RC_NO_ERR)
287                 cYAML_print_tree2file(stderr, err_rc);
288
289         cYAML_free_tree(err_rc);
290
291         return rc;
292 }
293
294 static int jt_set_large(int argc, char **argv)
295 {
296         long int value;
297         int rc;
298         struct cYAML *err_rc = NULL;
299
300         if (handle_help(set_cmds, "set", "large_buffers", argc, argv) == 0)
301                 return 0;
302
303         rc = parse_long(argv[1], &value);
304         if (rc != 0) {
305                 cYAML_build_error(-1, -1, "parser", "set",
306                                   "cannot parse large_buffers value", &err_rc);
307                 cYAML_print_tree2file(stderr, err_rc);
308                 cYAML_free_tree(err_rc);
309                 return -1;
310         }
311
312         rc = lustre_lnet_config_buffers(-1, -1, value, -1, &err_rc);
313         if (rc != LUSTRE_CFG_RC_NO_ERR)
314                 cYAML_print_tree2file(stderr, err_rc);
315
316         cYAML_free_tree(err_rc);
317
318         return rc;
319 }
320
321 static int jt_set_routing(int argc, char **argv)
322 {
323         long int value;
324         struct cYAML *err_rc = NULL;
325         int rc;
326
327         if (handle_help(set_cmds, "set", "routing", argc, argv) == 0)
328                 return 0;
329
330         rc = parse_long(argv[1], &value);
331         if (rc != 0 || (value != 0 && value != 1)) {
332                 cYAML_build_error(-1, -1, "parser", "set",
333                                   "cannot parse routing value.\n"
334                                   "must be 0 for disable or 1 for enable",
335                                   &err_rc);
336                 cYAML_print_tree2file(stderr, err_rc);
337                 cYAML_free_tree(err_rc);
338                 return -1;
339         }
340
341         rc = lustre_lnet_enable_routing(value, -1, &err_rc);
342
343         if (rc != LUSTRE_CFG_RC_NO_ERR)
344                 cYAML_print_tree2file(stderr, err_rc);
345
346         cYAML_free_tree(err_rc);
347
348         return rc;
349 }
350
351 static int jt_config_lnet(int argc, char **argv)
352 {
353         struct cYAML *err_rc = NULL;
354         bool load_mod_params = false;
355         int rc, opt;
356
357         const char *const short_options = "ah";
358         const struct option long_options[] = {
359                 { "all", 0, NULL, 'a' },
360                 { "help", 0, NULL, 'h' },
361                 { NULL, 0, NULL, 0 },
362         };
363
364         while ((opt = getopt_long(argc, argv, short_options,
365                                    long_options, NULL)) != -1) {
366                 switch (opt) {
367                 case 'a':
368                         load_mod_params = true;
369                         break;
370                 case 'h':
371                         print_help(lnet_cmds, "lnet", "configure");
372                         return 0;
373                 default:
374                         return 0;
375                 }
376         }
377
378         rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
379                                           -1, &err_rc);
380
381         if (rc != LUSTRE_CFG_RC_NO_ERR)
382                 cYAML_print_tree2file(stderr, err_rc);
383
384         cYAML_free_tree(err_rc);
385
386         return rc;
387 }
388
389 static int jt_unconfig_lnet(int argc, char **argv)
390 {
391         struct cYAML *err_rc = NULL;
392         int rc;
393
394         if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
395                 return 0;
396
397         rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
398
399         if (rc != LUSTRE_CFG_RC_NO_ERR)
400                 cYAML_print_tree2file(stderr, err_rc);
401
402         cYAML_free_tree(err_rc);
403
404         return rc;
405 }
406 static int jt_add_route(int argc, char **argv)
407 {
408         char *network = NULL, *gateway = NULL;
409         long int hop = -1, prio = -1;
410         struct cYAML *err_rc = NULL;
411         int rc, opt;
412
413         const char *const short_options = "n:g:c:p:h";
414         const struct option long_options[] = {
415                 { "net", 1, NULL, 'n' },
416                 { "gateway", 1, NULL, 'g' },
417                 { "hop-count", 1, NULL, 'c' },
418                 { "priority", 1, NULL, 'p' },
419                 { "help", 0, NULL, 'h' },
420                 { NULL, 0, NULL, 0 },
421         };
422
423         while ((opt = getopt_long(argc, argv, short_options,
424                                    long_options, NULL)) != -1) {
425                 switch (opt) {
426                 case 'n':
427                         network = optarg;
428                         break;
429                 case 'g':
430                         gateway = optarg;
431                         break;
432                 case 'c':
433                         rc = parse_long(optarg, &hop);
434                         if (rc != 0) {
435                                 /* ignore option */
436                                 hop = -1;
437                                 continue;
438                         }
439                         break;
440                 case 'p':
441                         rc = parse_long(optarg, &prio);
442                         if (rc != 0) {
443                                 /* ingore option */
444                                 prio = -1;
445                                 continue;
446                         }
447                         break;
448                 case 'h':
449                         print_help(route_cmds, "route", "add");
450                         return 0;
451                 default:
452                         return 0;
453                 }
454         }
455
456         rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
457
458         if (rc != LUSTRE_CFG_RC_NO_ERR)
459                 cYAML_print_tree2file(stderr, err_rc);
460
461         cYAML_free_tree(err_rc);
462
463         return rc;
464 }
465
466 static int jt_add_ni(int argc, char **argv)
467 {
468         char *ip2net = NULL;
469         long int pto = -1, pc = -1, pbc = -1, cre = -1;
470         struct cYAML *err_rc = NULL;
471         int rc, opt, num_global_cpts = 0;
472         struct lnet_dlc_network_descr nw_descr;
473         struct cfs_expr_list *global_cpts = NULL;
474         struct lnet_ioctl_config_lnd_tunables tunables;
475         bool found = false;
476
477         memset(&tunables, 0, sizeof(tunables));
478         lustre_lnet_init_nw_descr(&nw_descr);
479
480         const char *const short_options = "n:i:p:t:c:b:r:s:h";
481         const struct option long_options[] = {
482                 { "net", 1, NULL, 'n' },
483                 { "if", 1, NULL, 'i' },
484                 { "ip2net", 1, NULL, 'p' },
485                 { "peer-timeout", 1, NULL, 't' },
486                 { "peer-credits", 1, NULL, 'c' },
487                 { "peer-buffer-credits", 1, NULL, 'b' },
488                 { "credits", 1, NULL, 'r' },
489                 { "cpt", 1, NULL, 's' },
490                 { "help", 0, NULL, 'h' },
491                 { NULL, 0, NULL, 0 },
492         };
493
494         while ((opt = getopt_long(argc, argv, short_options,
495                                    long_options, NULL)) != -1) {
496                 switch (opt) {
497                 case 'n':
498                         nw_descr.nw_id = libcfs_str2net(optarg);
499                         break;
500                 case 'i':
501                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
502                         if (rc != 0) {
503                                 cYAML_build_error(-1, -1, "ni", "add",
504                                                 "bad interface list",
505                                                 &err_rc);
506                                 goto failed;
507                         }
508                         break;
509                 case 'p':
510                         ip2net = optarg;
511                         break;
512                 case 't':
513                         rc = parse_long(optarg, &pto);
514                         if (rc != 0) {
515                                 /* ignore option */
516                                 pto = -1;
517                                 continue;
518                         }
519                         break;
520                 case 'c':
521                         rc = parse_long(optarg, &pc);
522                         if (rc != 0) {
523                                 /* ignore option */
524                                 pc = -1;
525                                 continue;
526                         }
527                         break;
528                 case 'b':
529                         rc = parse_long(optarg, &pbc);
530                         if (rc != 0) {
531                                 /* ignore option */
532                                 pbc = -1;
533                                 continue;
534                         }
535                         break;
536                 case 'r':
537                         rc = parse_long(optarg, &cre);
538                         if (rc != 0) {
539                                 /* ignore option */
540                                 cre = -1;
541                                 continue;
542                         }
543                         break;
544                 case 's':
545                         num_global_cpts =
546                                 cfs_expr_list_parse(optarg,
547                                                  strlen(optarg),
548                                                  0, UINT_MAX, &global_cpts);
549                         break;
550                 case 'h':
551                         print_help(net_cmds, "net", "add");
552                         return 0;
553                 default:
554                         return 0;
555                 }
556         }
557
558         if (pto > 0 || pc > 0 || pbc > 0 || cre > 0) {
559                 tunables.lt_cmn.lct_peer_timeout = pto;
560                 tunables.lt_cmn.lct_peer_tx_credits = pc;
561                 tunables.lt_cmn.lct_peer_rtr_credits = pbc;
562                 tunables.lt_cmn.lct_max_tx_credits = cre;
563                 found = true;
564         }
565
566         rc = lustre_lnet_config_ni(&nw_descr,
567                                    (num_global_cpts > 0) ? global_cpts: NULL,
568                                    ip2net, (found) ? &tunables : NULL,
569                                    -1, &err_rc);
570
571         if (global_cpts != NULL)
572                 cfs_expr_list_free(global_cpts);
573
574 failed:
575         if (rc != LUSTRE_CFG_RC_NO_ERR)
576                 cYAML_print_tree2file(stderr, err_rc);
577
578         cYAML_free_tree(err_rc);
579
580         return rc;
581 }
582
583 static int jt_del_route(int argc, char **argv)
584 {
585         char *network = NULL, *gateway = NULL;
586         struct cYAML *err_rc = NULL;
587         int rc, opt;
588
589         const char *const short_options = "n:g:h";
590         const struct option long_options[] = {
591                 { "net", 1, NULL, 'n' },
592                 { "gateway", 1, NULL, 'g' },
593                 { "help", 0, NULL, 'h' },
594                 { NULL, 0, NULL, 0 },
595         };
596
597         while ((opt = getopt_long(argc, argv, short_options,
598                                    long_options, NULL)) != -1) {
599                 switch (opt) {
600                 case 'n':
601                         network = optarg;
602                         break;
603                 case 'g':
604                         gateway = optarg;
605                         break;
606                 case 'h':
607                         print_help(route_cmds, "route", "del");
608                         return 0;
609                 default:
610                         return 0;
611                 }
612         }
613
614         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
615
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_del_ni(int argc, char **argv)
625 {
626         struct cYAML *err_rc = NULL;
627         int rc, opt;
628         struct lnet_dlc_network_descr nw_descr;
629
630         lustre_lnet_init_nw_descr(&nw_descr);
631
632         const char *const short_options = "n:i:h";
633         const struct option long_options[] = {
634                 { "net", 1, NULL, 'n' },
635                 { "if", 1, NULL, 'i' },
636                 { "help", 0, NULL, 'h' },
637                 { NULL, 0, NULL, 0 },
638         };
639
640         while ((opt = getopt_long(argc, argv, short_options,
641                                    long_options, NULL)) != -1) {
642                 switch (opt) {
643                 case 'n':
644                         nw_descr.nw_id = libcfs_str2net(optarg);
645                         break;
646                 case 'i':
647                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
648                         if (rc != 0) {
649                                 cYAML_build_error(-1, -1, "ni", "add",
650                                                 "bad interface list",
651                                                 &err_rc);
652                                 goto out;
653                         }
654                         break;
655                 case 'h':
656                         print_help(net_cmds, "net", "del");
657                         return 0;
658                 default:
659                         return 0;
660                 }
661         }
662
663         rc = lustre_lnet_del_ni(&nw_descr, -1, &err_rc);
664
665 out:
666         if (rc != LUSTRE_CFG_RC_NO_ERR)
667                 cYAML_print_tree2file(stderr, err_rc);
668
669         cYAML_free_tree(err_rc);
670
671         return rc;
672 }
673
674 static int jt_show_route(int argc, char **argv)
675 {
676         char *network = NULL, *gateway = NULL;
677         long int hop = -1, prio = -1;
678         int detail = 0, rc, opt;
679         struct cYAML *err_rc = NULL, *show_rc = NULL;
680
681         const char *const short_options = "n:g:h:p:vh";
682         const struct option long_options[] = {
683                 { "net", 1, NULL, 'n' },
684                 { "gateway", 1, NULL, 'g' },
685                 { "hop-count", 1, NULL, 'c' },
686                 { "priority", 1, NULL, 'p' },
687                 { "verbose", 0, NULL, 'v' },
688                 { "help", 0, NULL, 'h' },
689                 { NULL, 0, NULL, 0 },
690         };
691
692         while ((opt = getopt_long(argc, argv, short_options,
693                                    long_options, NULL)) != -1) {
694                 switch (opt) {
695                 case 'n':
696                         network = optarg;
697                         break;
698                 case 'g':
699                         gateway = optarg;
700                         break;
701                 case 'c':
702                         rc = parse_long(optarg, &hop);
703                         if (rc != 0) {
704                                 /* ignore option */
705                                 hop = -1;
706                                 continue;
707                         }
708                         break;
709                 case 'p':
710                         rc = parse_long(optarg, &prio);
711                         if (rc != 0) {
712                                 /* ignore option */
713                                 prio = -1;
714                                 continue;
715                         }
716                         break;
717                 case 'v':
718                         detail = 1;
719                         break;
720                 case 'h':
721                         print_help(route_cmds, "route", "show");
722                         return 0;
723                 default:
724                         return 0;
725                 }
726         }
727
728         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
729                                     &show_rc, &err_rc);
730
731         if (rc != LUSTRE_CFG_RC_NO_ERR)
732                 cYAML_print_tree2file(stderr, err_rc);
733         else if (show_rc)
734                 cYAML_print_tree(show_rc);
735
736         cYAML_free_tree(err_rc);
737         cYAML_free_tree(show_rc);
738
739         return rc;
740 }
741
742 static int jt_show_net(int argc, char **argv)
743 {
744         char *network = NULL;
745         int detail = 0, rc, opt;
746         struct cYAML *err_rc = NULL, *show_rc = NULL;
747
748         const char *const short_options = "n:vh";
749         const struct option long_options[] = {
750                 { "net", 1, NULL, 'n' },
751                 { "verbose", 0, NULL, 'v' },
752                 { "help", 0, NULL, 'h' },
753                 { NULL, 0, NULL, 0 },
754         };
755
756         while ((opt = getopt_long(argc, argv, short_options,
757                                    long_options, NULL)) != -1) {
758                 switch (opt) {
759                 case 'n':
760                         network = optarg;
761                         break;
762                 case 'v':
763                         detail = 1;
764                         break;
765                 case 'h':
766                         print_help(net_cmds, "net", "show");
767                         return 0;
768                 default:
769                         return 0;
770                 }
771         }
772
773         rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
774
775         if (rc != LUSTRE_CFG_RC_NO_ERR)
776                 cYAML_print_tree2file(stderr, err_rc);
777         else if (show_rc)
778                 cYAML_print_tree(show_rc);
779
780         cYAML_free_tree(err_rc);
781         cYAML_free_tree(show_rc);
782
783         return rc;
784 }
785
786 static int jt_show_routing(int argc, char **argv)
787 {
788         struct cYAML *err_rc = NULL, *show_rc = NULL;
789         int rc;
790
791         if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
792                 return 0;
793
794         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
795
796         if (rc != LUSTRE_CFG_RC_NO_ERR)
797                 cYAML_print_tree2file(stderr, err_rc);
798         else if (show_rc)
799                 cYAML_print_tree(show_rc);
800
801         cYAML_free_tree(err_rc);
802         cYAML_free_tree(show_rc);
803
804         return rc;
805 }
806
807 static int jt_show_stats(int argc, char **argv)
808 {
809         int rc;
810         struct cYAML *show_rc = NULL, *err_rc = NULL;
811
812         if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
813                 return 0;
814
815         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
816
817         if (rc != LUSTRE_CFG_RC_NO_ERR)
818                 cYAML_print_tree2file(stderr, err_rc);
819         else if (show_rc)
820                 cYAML_print_tree(show_rc);
821
822         cYAML_free_tree(err_rc);
823         cYAML_free_tree(show_rc);
824
825         return rc;
826 }
827
828 static int jt_show_numa(int argc, char **argv)
829 {
830         int rc;
831         struct cYAML *show_rc = NULL, *err_rc = NULL;
832
833         if (handle_help(numa_cmds, "numa", "show", argc, argv) == 0)
834                 return 0;
835
836         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
837
838         if (rc != LUSTRE_CFG_RC_NO_ERR)
839                 cYAML_print_tree2file(stderr, err_rc);
840         else if (show_rc)
841                 cYAML_print_tree(show_rc);
842
843         cYAML_free_tree(err_rc);
844         cYAML_free_tree(show_rc);
845
846         return rc;
847 }
848
849 static inline int jt_lnet(int argc, char **argv)
850 {
851         if (argc < 2)
852                 return CMD_HELP;
853
854         if (argc == 2 &&
855             handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
856                 return 0;
857
858         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
859 }
860
861 static inline int jt_route(int argc, char **argv)
862 {
863         if (argc < 2)
864                 return CMD_HELP;
865
866         if (argc == 2 &&
867             handle_help(route_cmds, "route", NULL, argc, argv) == 0)
868                 return 0;
869
870         return Parser_execarg(argc - 1, &argv[1], route_cmds);
871 }
872
873 static inline int jt_net(int argc, char **argv)
874 {
875         if (argc < 2)
876                 return CMD_HELP;
877
878         if (argc == 2 &&
879             handle_help(net_cmds, "net", NULL, argc, argv) == 0)
880                 return 0;
881
882         return Parser_execarg(argc - 1, &argv[1], net_cmds);
883 }
884
885 static inline int jt_routing(int argc, char **argv)
886 {
887         if (argc < 2)
888                 return CMD_HELP;
889
890         if (argc == 2 &&
891             handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
892                 return 0;
893
894         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
895 }
896
897 static inline int jt_stats(int argc, char **argv)
898 {
899         if (argc < 2)
900                 return CMD_HELP;
901
902         if (argc == 2 &&
903             handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
904                 return 0;
905
906         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
907 }
908
909 static inline int jt_numa(int argc, char **argv)
910 {
911         if (argc < 2)
912                 return CMD_HELP;
913
914         if (argc == 2 &&
915             handle_help(numa_cmds, "numa", NULL, argc, argv) == 0)
916                 return 0;
917
918         return Parser_execarg(argc - 1, &argv[1], numa_cmds);
919 }
920
921 static inline int jt_peers(int argc, char **argv)
922 {
923         if (argc < 2)
924                 return CMD_HELP;
925
926         if (argc == 2 &&
927             handle_help(peer_cmds, "peer", NULL, argc, argv) == 0)
928                 return 0;
929
930         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
931 }
932
933 static inline int jt_set(int argc, char **argv)
934 {
935         if (argc < 2)
936                 return CMD_HELP;
937
938         if (argc == 2  &&
939             handle_help(set_cmds, "set", NULL, argc, argv) == 0)
940                 return 0;
941
942         return Parser_execarg(argc - 1, &argv[1], set_cmds);
943 }
944
945 static int jt_import(int argc, char **argv)
946 {
947         char *file = NULL;
948         struct cYAML *err_rc = NULL;
949         struct cYAML *show_rc = NULL;
950         int rc = 0, opt, opt_found = 0;
951         char cmd = 'a';
952
953         const char *const short_options = "adsh";
954         const struct option long_options[] = {
955                 { "add", 0, NULL, 'a' },
956                 { "del", 0, NULL, 'd' },
957                 { "show", 0, NULL, 's' },
958                 { "help", 0, NULL, 'h' },
959                 { NULL, 0, NULL, 0 },
960         };
961
962         while ((opt = getopt_long(argc, argv, short_options,
963                                    long_options, NULL)) != -1) {
964                 opt_found = 1;
965                 switch (opt) {
966                 case 'a':
967                 case 'd':
968                 case 's':
969                         cmd = opt;
970                         break;
971                 case 'h':
972                         printf("import FILE\n"
973                                "import < FILE : import a file\n"
974                                "\t--add: add configuration\n"
975                                "\t--del: delete configuration\n"
976                                "\t--show: show configuration\n"
977                                "\t--help: display this help\n"
978                                "If no command option is given then --add"
979                                " is assumed by default\n");
980                         return 0;
981                 default:
982                         return 0;
983                 }
984         }
985
986         /* grab the file name if one exists */
987         if (opt_found && argc == 3)
988                 file = argv[2];
989         else if (!opt_found && argc == 2)
990                 file = argv[1];
991
992         switch (cmd) {
993         case 'a':
994                 rc = lustre_yaml_config(file, &err_rc);
995                 break;
996         case 'd':
997                 rc = lustre_yaml_del(file, &err_rc);
998                 break;
999         case 's':
1000                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1001                 cYAML_print_tree(show_rc);
1002                 cYAML_free_tree(show_rc);
1003                 break;
1004         }
1005
1006         if (rc != LUSTRE_CFG_RC_NO_ERR)
1007                 cYAML_print_tree2file(stderr, err_rc);
1008
1009         cYAML_free_tree(err_rc);
1010
1011         return rc;
1012 }
1013
1014 static int jt_export(int argc, char **argv)
1015 {
1016         struct cYAML *show_rc = NULL;
1017         struct cYAML *err_rc = NULL;
1018         int rc, opt;
1019         FILE *f = NULL;
1020
1021         const char *const short_options = "h";
1022         const struct option long_options[] = {
1023                 { "help", 0, NULL, 'h' },
1024                 { NULL, 0, NULL, 0 },
1025         };
1026
1027         while ((opt = getopt_long(argc, argv, short_options,
1028                                    long_options, NULL)) != -1) {
1029                 switch (opt) {
1030                 case 'h':
1031                         printf("export FILE\n"
1032                                "export > FILE : export configuration\n"
1033                                "\t--help: display this help\n");
1034                         return 0;
1035                 default:
1036                         return 0;
1037                 }
1038         }
1039
1040         if (argc >= 2) {
1041                 f = fopen(argv[1], "w");
1042                 if (f == NULL)
1043                         return -1;
1044         } else
1045                 f = stdout;
1046
1047         rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
1048         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1049                 cYAML_print_tree2file(stderr, err_rc);
1050                 cYAML_free_tree(err_rc);
1051         }
1052
1053         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
1054                                     &err_rc);
1055         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1056                 cYAML_print_tree2file(stderr, err_rc);
1057                 cYAML_free_tree(err_rc);
1058         }
1059
1060         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
1061         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1062                 cYAML_print_tree2file(stderr, err_rc);
1063                 cYAML_free_tree(err_rc);
1064         }
1065
1066         rc = lustre_lnet_show_peer(NULL, 1, -1, &show_rc, &err_rc);
1067         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1068                 cYAML_print_tree2file(stderr, err_rc);
1069                 cYAML_free_tree(err_rc);
1070         }
1071
1072         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1073         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1074                 cYAML_print_tree2file(stderr, err_rc);
1075                 cYAML_free_tree(err_rc);
1076         }
1077
1078         if (show_rc != NULL) {
1079                 cYAML_print_tree2file(f, show_rc);
1080                 cYAML_free_tree(show_rc);
1081         }
1082
1083         if (argc >= 2)
1084                 fclose(f);
1085
1086         return 0;
1087 }
1088
1089 static int jt_add_peer_nid(int argc, char **argv)
1090 {
1091         char *key_nid = NULL;
1092         char **nids = NULL, **nids2 = NULL;
1093         int size = 0;
1094         struct cYAML *err_rc = NULL;
1095         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i;
1096         bool non_mr = false;
1097
1098         const char *const short_options = "k:n:mh";
1099         const struct option long_options[] = {
1100                 { "key_nid", 1, NULL, 'k' },
1101                 { "nid", 1, NULL, 'n' },
1102                 { "non_mr", 0, NULL, 'm'},
1103                 { "help", 0, NULL, 'h' },
1104                 { NULL, 0, NULL, 0 },
1105         };
1106
1107         while ((opt = getopt_long(argc, argv, short_options,
1108                                   long_options, NULL)) != -1) {
1109                 switch (opt) {
1110                 case 'k':
1111                         key_nid = optarg;
1112                         break;
1113                 case 'n':
1114                         size = lustre_lnet_parse_nids(optarg, nids, size,
1115                                                       &nids2);
1116                         if (nids2 == NULL)
1117                                 goto failed;
1118                         nids = nids2;
1119                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1120                         break;
1121                 case 'm':
1122                         non_mr = true;
1123                         break;
1124                 case 'h':
1125                         print_help(peer_cmds, "peer", "add");
1126                         return 0;
1127                 default:
1128                         return 0;
1129                 }
1130         }
1131
1132         rc = lustre_lnet_config_peer_nid(key_nid, nids, size,
1133                                          !non_mr, -1, &err_rc);
1134
1135 failed:
1136         for (i = 0; i < size; i++)
1137                 free(nids[i]);
1138         free(nids);
1139
1140         if (rc != LUSTRE_CFG_RC_NO_ERR)
1141                 cYAML_print_tree2file(stderr, err_rc);
1142
1143         cYAML_free_tree(err_rc);
1144
1145         return rc;
1146 }
1147
1148 static int jt_del_peer_nid(int argc, char **argv)
1149 {
1150         char *key_nid = NULL;
1151         char **nids = NULL, **nids2 = NULL;
1152         struct cYAML *err_rc = NULL;
1153         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i, size = 0;
1154
1155         const char *const short_options = "k:n:h";
1156         const struct option long_options[] = {
1157                 { "key_nid", 1, NULL, 'k' },
1158                 { "nid", 1, NULL, 'n' },
1159                 { "help", 0, NULL, 'h' },
1160                 { NULL, 0, NULL, 0 },
1161         };
1162
1163         while ((opt = getopt_long(argc, argv, short_options,
1164                                   long_options, NULL)) != -1) {
1165                 switch (opt) {
1166                 case 'k':
1167                         key_nid = optarg;
1168                         break;
1169                 case 'n':
1170                         size = lustre_lnet_parse_nids(optarg, nids, size,
1171                                                       &nids2);
1172                         if (nids2 == NULL)
1173                                 goto failed;
1174                         nids = nids2;
1175                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1176                         break;
1177                 case 'h':
1178                         print_help(peer_cmds, "peer", "del");
1179                         return 0;
1180                 default:
1181                         return 0;
1182                 }
1183         }
1184
1185         rc = lustre_lnet_del_peer_nid(key_nid, nids, size, -1, &err_rc);
1186
1187 failed:
1188         for (i = 0; i < size; i++)
1189                 free(nids[i]);
1190         free(nids);
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         return rc;
1198 }
1199
1200 static int jt_show_peer(int argc, char **argv)
1201 {
1202         char *key_nid = NULL;
1203         int rc, opt;
1204         struct cYAML *err_rc = NULL, *show_rc = NULL;
1205         int detail = 0;
1206
1207         const char *const short_options = "k:vh";
1208         const struct option long_options[] = {
1209                 { "key_nid", 1, NULL, 'k' },
1210                 { "verbose", 0, NULL, 'v' },
1211                 { "help", 0, NULL, 'h' },
1212                 { NULL, 0, NULL, 0 },
1213         };
1214
1215         while ((opt = getopt_long(argc, argv, short_options,
1216                                   long_options, NULL)) != -1) {
1217                 switch (opt) {
1218                 case 'k':
1219                         key_nid = optarg;
1220                         break;
1221                 case 'v':
1222                         detail = 1;
1223                         break;
1224                 case 'h':
1225                         print_help(peer_cmds, "peer", "add");
1226                         return 0;
1227                 default:
1228                         return 0;
1229                 }
1230         }
1231
1232         rc = lustre_lnet_show_peer(key_nid, detail, -1, &show_rc, &err_rc);
1233
1234         if (rc != LUSTRE_CFG_RC_NO_ERR)
1235                 cYAML_print_tree2file(stderr, err_rc);
1236         else if (show_rc)
1237                 cYAML_print_tree(show_rc);
1238
1239         cYAML_free_tree(err_rc);
1240         cYAML_free_tree(show_rc);
1241
1242         return rc;
1243 }
1244
1245 command_t list[] = {
1246         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
1247         {"route", jt_route, 0, "route {add | del | show | help}"},
1248         {"net", jt_net, 0, "net {add | del | show | help}"},
1249         {"routing", jt_routing, 0, "routing {show | help}"},
1250         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
1251                            " | routing}"},
1252         {"import", jt_import, 0, "import {--add | --del | --show | "
1253                                  "--help} FILE.yaml"},
1254         {"export", jt_export, 0, "export {--help} FILE.yaml"},
1255         {"stats", jt_stats, 0, "stats {show | help}"},
1256         {"numa", jt_numa, 0, "numa {show | help}"},
1257         {"peer", jt_peers, 0, "peer {add | del | show | help}"},
1258         {"help", Parser_help, 0, "help"},
1259         {"exit", Parser_quit, 0, "quit"},
1260         {"quit", Parser_quit, 0, "quit"},
1261         { 0, 0, 0, NULL }
1262 };
1263
1264 int main(int argc, char **argv)
1265 {
1266         int rc = 0;
1267         struct cYAML *err_rc = NULL;
1268
1269         rc = lustre_lnet_config_lib_init();
1270         if (rc < 0) {
1271                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1272                                   "cannot register LNet device", &err_rc);
1273                 cYAML_print_tree2file(stderr, err_rc);
1274                 return rc;
1275         }
1276
1277         Parser_init("lnetctl > ", list);
1278         if (argc > 1) {
1279                 rc = Parser_execarg(argc - 1, &argv[1], list);
1280                 goto errorout;
1281         }
1282
1283         Parser_commands();
1284
1285 errorout:
1286         return rc;
1287 }