Whamcloud - gitweb
LU-6245 libcfs: remove libcfsutil.h
[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, 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 <libcfs/util/parser.h>
31 #include <lnet/lnetctl.h>
32 #include "cyaml/cyaml.h"
33 #include "lnetconfig/liblnetconfig.h"
34
35 #define LNET_CONFIGURE          true
36 #define LNET_UNCONFIGURE        false
37
38 static int jt_config_lnet(int argc, char **argv);
39 static int jt_unconfig_lnet(int argc, char **argv);
40 static int jt_add_route(int argc, char **argv);
41 static int jt_add_net(int argc, char **argv);
42 static int jt_set_routing(int argc, char **argv);
43 static int jt_del_route(int argc, char **argv);
44 static int jt_del_net(int argc, char **argv);
45 static int jt_show_route(int argc, char **argv);
46 static int jt_show_net(int argc, char **argv);
47 static int jt_show_routing(int argc, char **argv);
48 static int jt_show_stats(int argc, char **argv);
49 static int jt_show_peer_credits(int argc, char **argv);
50 static int jt_set_tiny(int argc, char **argv);
51 static int jt_set_small(int argc, char **argv);
52 static int jt_set_large(int argc, char **argv);
53
54 command_t lnet_cmds[] = {
55         {"configure", jt_config_lnet, 0, "configure lnet\n"
56          "\t--all: load NI configuration from module parameters\n"},
57         {"unconfigure", jt_unconfig_lnet, 0, "unconfigure lnet\n"},
58         { 0, 0, 0, NULL }
59 };
60
61 command_t route_cmds[] = {
62         {"add", jt_add_route, 0, "add a route\n"
63          "\t--net: net name (e.g. tcp0)\n"
64          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"
65          "\t--hop: number to final destination (1 < hops < 255)\n"
66          "\t--priority: priority of route (0 - highest prio\n"},
67         {"del", jt_del_route, 0, "delete a route\n"
68          "\t--net: net name (e.g. tcp0)\n"
69          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp)\n"},
70         {"show", jt_show_route, 0, "show routes\n"
71          "\t--net: net name (e.g. tcp0) to filter on\n"
72          "\t--gateway: gateway nid (e.g. 10.1.1.2@tcp) to filter on\n"
73          "\t--hop: number to final destination (1 < hops < 255) to filter on\n"
74          "\t--priority: priority of route (0 - highest prio to filter on\n"
75          "\t--verbose: display detailed output per route\n"},
76         { 0, 0, 0, NULL }
77 };
78
79 command_t net_cmds[] = {
80         {"add", jt_add_net, 0, "add a network\n"
81          "\t--net: net name (e.g. tcp0)\n"
82          "\t--if: physical interface (e.g. eth0)\n"
83          "\t--ip2net: specify networks based on IP address patterns\n"
84          "\t--peer-timeout: time to wait before declaring a peer dead\n"
85          "\t--peer-credits: define the max number of inflight messages\n"
86          "\t--peer-buffer-credits: the number of buffer credits per peer\n"
87          "\t--credits: Network Interface credits\n"
88          "\t--cpt: CPU Partitions configured net uses (e.g. [0,1]\n"},
89         {"del", jt_del_net, 0, "delete a network\n"
90          "\t--net: net name (e.g. tcp0)\n"},
91         {"show", jt_show_net, 0, "show networks\n"
92          "\t--net: net name (e.g. tcp0) to filter on\n"
93          "\t--verbose: display detailed output per network\n"},
94         { 0, 0, 0, NULL }
95 };
96
97 command_t routing_cmds[] = {
98         {"show", jt_show_routing, 0, "show routing information\n"},
99         { 0, 0, 0, NULL }
100 };
101
102 command_t stats_cmds[] = {
103         {"show", jt_show_stats, 0, "show LNET statistics\n"},
104         { 0, 0, 0, NULL }
105 };
106
107 command_t credits_cmds[] = {
108         {"show", jt_show_peer_credits, 0, "show peer credits\n"},
109         { 0, 0, 0, NULL }
110 };
111
112 command_t set_cmds[] = {
113         {"tiny_buffers", jt_set_tiny, 0, "set tiny routing buffers\n"
114          "\tVALUE must be greater than 0\n"},
115         {"small_buffers", jt_set_small, 0, "set small routing buffers\n"
116          "\tVALUE must be greater than 0\n"},
117         {"large_buffers", jt_set_large, 0, "set large routing buffers\n"
118          "\tVALUE must be greater than 0\n"},
119         {"routing", jt_set_routing, 0, "enable/disable routing\n"
120          "\t0 - disable routing\n"
121          "\t1 - enable routing\n"},
122         { 0, 0, 0, NULL }
123 };
124
125 static inline void print_help(const command_t cmds[], const char *cmd_type,
126                               const char *pc_name)
127 {
128         const command_t *cmd;
129
130         for (cmd = cmds; cmd->pc_name; cmd++) {
131                 if (pc_name != NULL &&
132                     strcmp(cmd->pc_name, pc_name) == 0) {
133                         printf("%s %s: %s\n", cmd_type, cmd->pc_name,
134                                cmd->pc_help);
135                         return;
136                 } else if (pc_name != NULL) {
137                         continue;
138                 }
139                 printf("%s %s: %s\n", cmd_type, cmd->pc_name, cmd->pc_help);
140         }
141 }
142
143 static int parse_long(const char *number, long int *value)
144 {
145         char *end;
146
147         *value = strtol(number,  &end, 0);
148         if (end != NULL && *end != 0)
149                 return -1;
150
151         return 0;
152 }
153
154 static int handle_help(const command_t *cmd_list, const char *cmd,
155                        const char *sub_cmd, int argc, char **argv)
156 {
157         int opt;
158         int rc = -1;
159         optind = 0;
160         opterr = 0;
161
162         const char *const short_options = "h";
163         const struct option long_options[] = {
164                 { "help", 0, NULL, 'h' },
165                 { NULL, 0, NULL, 0 },
166         };
167
168         while ((opt = getopt_long(argc, argv, short_options,
169                                   long_options, NULL)) != -1) {
170                 switch (opt) {
171                 case 'h':
172                         print_help(cmd_list, cmd, sub_cmd);
173                         rc = 0;
174                         break;
175                 default:
176                         rc = -1;
177                         break;
178                 }
179         }
180
181         opterr = 1;
182         optind = 0;
183         return rc;
184 }
185
186 static int jt_set_tiny(int argc, char **argv)
187 {
188         long int value;
189         int rc;
190         struct cYAML *err_rc = NULL;
191
192         if (handle_help(set_cmds, "set", "tiny_buffers", argc, argv) == 0)
193                 return 0;
194
195         rc = parse_long(argv[1], &value);
196         if (rc != 0) {
197                 cYAML_build_error(-1, -1, "parser", "set",
198                                   "cannot parse tiny_buffers value", &err_rc);
199                 cYAML_print_tree2file(stderr, err_rc);
200                 cYAML_free_tree(err_rc);
201                 return -1;
202         }
203
204         rc = lustre_lnet_config_buffers(value, -1, -1, -1, &err_rc);
205         if (rc != LUSTRE_CFG_RC_NO_ERR)
206                 cYAML_print_tree2file(stderr, err_rc);
207
208         cYAML_free_tree(err_rc);
209
210         return rc;
211 }
212
213 static int jt_set_small(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", "small_buffers", 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 small_buffers 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(-1, value, -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_large(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", "large_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 large_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(-1, -1, value, -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_routing(int argc, char **argv)
268 {
269         long int value;
270         struct cYAML *err_rc = NULL;
271         int rc;
272
273         if (handle_help(set_cmds, "set", "routing", argc, argv) == 0)
274                 return 0;
275
276         rc = parse_long(argv[1], &value);
277         if (rc != 0 || (value != 0 && value != 1)) {
278                 cYAML_build_error(-1, -1, "parser", "set",
279                                   "cannot parse routing value.\n"
280                                   "must be 0 for disable or 1 for enable",
281                                   &err_rc);
282                 cYAML_print_tree2file(stderr, err_rc);
283                 cYAML_free_tree(err_rc);
284                 return -1;
285         }
286
287         rc = lustre_lnet_enable_routing(value, -1, &err_rc);
288
289         if (rc != LUSTRE_CFG_RC_NO_ERR)
290                 cYAML_print_tree2file(stderr, err_rc);
291
292         cYAML_free_tree(err_rc);
293
294         return rc;
295 }
296
297 static int jt_config_lnet(int argc, char **argv)
298 {
299         struct cYAML *err_rc = NULL;
300         bool load_mod_params = false;
301         int rc, opt;
302
303         const char *const short_options = "ah";
304         const struct option long_options[] = {
305                 { "all", 0, NULL, 'a' },
306                 { "help", 0, NULL, 'h' },
307                 { NULL, 0, NULL, 0 },
308         };
309
310         while ((opt = getopt_long(argc, argv, short_options,
311                                    long_options, NULL)) != -1) {
312                 switch (opt) {
313                 case 'a':
314                         load_mod_params = true;
315                         break;
316                 case 'h':
317                         print_help(lnet_cmds, "lnet", "configure");
318                         return 0;
319                 default:
320                         return 0;
321                 }
322         }
323
324         rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
325                                           -1, &err_rc);
326
327         if (rc != LUSTRE_CFG_RC_NO_ERR)
328                 cYAML_print_tree2file(stderr, err_rc);
329
330         cYAML_free_tree(err_rc);
331
332         return rc;
333 }
334
335 static int jt_unconfig_lnet(int argc, char **argv)
336 {
337         struct cYAML *err_rc = NULL;
338         int rc;
339
340         if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
341                 return 0;
342
343         rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
344
345         if (rc != LUSTRE_CFG_RC_NO_ERR)
346                 cYAML_print_tree2file(stderr, err_rc);
347
348         cYAML_free_tree(err_rc);
349
350         return rc;
351 }
352 static int jt_add_route(int argc, char **argv)
353 {
354         char *network = NULL, *gateway = NULL;
355         long int hop = -1, prio = -1;
356         struct cYAML *err_rc = NULL;
357         int rc, opt;
358
359         const char *const short_options = "n:g:c:p:h";
360         const struct option long_options[] = {
361                 { "net", 1, NULL, 'n' },
362                 { "gateway", 1, NULL, 'g' },
363                 { "hop-count", 1, NULL, 'c' },
364                 { "priority", 1, NULL, 'p' },
365                 { "help", 0, NULL, 'h' },
366                 { NULL, 0, NULL, 0 },
367         };
368
369         while ((opt = getopt_long(argc, argv, short_options,
370                                    long_options, NULL)) != -1) {
371                 switch (opt) {
372                 case 'n':
373                         network = optarg;
374                         break;
375                 case 'g':
376                         gateway = optarg;
377                         break;
378                 case 'c':
379                         rc = parse_long(optarg, &hop);
380                         if (rc != 0) {
381                                 /* ignore option */
382                                 hop = -1;
383                                 continue;
384                         }
385                         break;
386                 case 'p':
387                         rc = parse_long(optarg, &prio);
388                         if (rc != 0) {
389                                 /* ingore option */
390                                 prio = -1;
391                                 continue;
392                         }
393                         break;
394                 case 'h':
395                         print_help(route_cmds, "route", "add");
396                         return 0;
397                 default:
398                         return 0;
399                 }
400         }
401
402         rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
403
404         if (rc != LUSTRE_CFG_RC_NO_ERR)
405                 cYAML_print_tree2file(stderr, err_rc);
406
407         cYAML_free_tree(err_rc);
408
409         return rc;
410 }
411
412 static int jt_add_net(int argc, char **argv)
413 {
414         char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
415         long int pto = -1, pc = -1, pbc = -1, cre = -1;
416         struct cYAML *err_rc = NULL;
417         int rc, opt;
418
419         const char *const short_options = "n:i:p:t:c:b:r:s:h";
420         const struct option long_options[] = {
421                 { "net", 1, NULL, 'n' },
422                 { "if", 1, NULL, 'i' },
423                 { "ip2net", 1, NULL, 'p' },
424                 { "peer-timeout", 1, NULL, 't' },
425                 { "peer-credits", 1, NULL, 'c' },
426                 { "peer-buffer-credits", 1, NULL, 'b' },
427                 { "credits", 1, NULL, 'r' },
428                 { "cpt", 1, NULL, 's' },
429                 { "help", 0, NULL, 'h' },
430                 { NULL, 0, NULL, 0 },
431         };
432
433         while ((opt = getopt_long(argc, argv, short_options,
434                                    long_options, NULL)) != -1) {
435                 switch (opt) {
436                 case 'n':
437                         network = optarg;
438                         break;
439                 case 'i':
440                         intf = optarg;
441                         break;
442                 case 'p':
443                         ip2net = optarg;
444                         break;
445                 case 't':
446                         rc = parse_long(optarg, &pto);
447                         if (rc != 0) {
448                                 /* ignore option */
449                                 pto = -1;
450                                 continue;
451                         }
452                         break;
453                 case 'c':
454                         rc = parse_long(optarg, &pc);
455                         if (rc != 0) {
456                                 /* ignore option */
457                                 pc = -1;
458                                 continue;
459                         }
460                         break;
461                 case 'b':
462                         rc = parse_long(optarg, &pbc);
463                         if (rc != 0) {
464                                 /* ignore option */
465                                 pbc = -1;
466                                 continue;
467                         }
468                         break;
469                 case 'r':
470                         rc = parse_long(optarg, &cre);
471                         if (rc != 0) {
472                                 /* ignore option */
473                                 cre = -1;
474                                 continue;
475                         }
476                         break;
477                 case 's':
478                         cpt = optarg;
479                         break;
480                 case 'h':
481                         print_help(net_cmds, "net", "add");
482                         return 0;
483                 default:
484                         return 0;
485                 }
486         }
487
488         rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
489                                     cre, cpt, -1, &err_rc);
490
491         if (rc != LUSTRE_CFG_RC_NO_ERR)
492                 cYAML_print_tree2file(stderr, err_rc);
493
494         cYAML_free_tree(err_rc);
495
496         return rc;
497 }
498
499 static int jt_del_route(int argc, char **argv)
500 {
501         char *network = NULL, *gateway = NULL;
502         struct cYAML *err_rc = NULL;
503         int rc, opt;
504
505         const char *const short_options = "n:g:h";
506         const struct option long_options[] = {
507                 { "net", 1, NULL, 'n' },
508                 { "gateway", 1, NULL, 'g' },
509                 { "help", 0, NULL, 'h' },
510                 { NULL, 0, NULL, 0 },
511         };
512
513         while ((opt = getopt_long(argc, argv, short_options,
514                                    long_options, NULL)) != -1) {
515                 switch (opt) {
516                 case 'n':
517                         network = optarg;
518                         break;
519                 case 'g':
520                         gateway = optarg;
521                         break;
522                 case 'h':
523                         print_help(route_cmds, "route", "del");
524                         return 0;
525                 default:
526                         return 0;
527                 }
528         }
529
530         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
531
532         if (rc != LUSTRE_CFG_RC_NO_ERR)
533                 cYAML_print_tree2file(stderr, err_rc);
534
535         cYAML_free_tree(err_rc);
536
537         return rc;
538 }
539
540 static int jt_del_net(int argc, char **argv)
541 {
542         char *network = NULL;
543         struct cYAML *err_rc = NULL;
544         int rc, opt;
545
546         const char *const short_options = "n:h";
547         const struct option long_options[] = {
548                 { "net", 1, NULL, 'n' },
549                 { "help", 0, NULL, 'h' },
550                 { NULL, 0, NULL, 0 },
551         };
552
553         while ((opt = getopt_long(argc, argv, short_options,
554                                    long_options, NULL)) != -1) {
555                 switch (opt) {
556                 case 'n':
557                         network = optarg;
558                         break;
559                 case 'h':
560                         print_help(net_cmds, "net", "del");
561                         return 0;
562                 default:
563                         return 0;
564                 }
565         }
566
567         rc = lustre_lnet_del_net(network, -1, &err_rc);
568
569         if (rc != LUSTRE_CFG_RC_NO_ERR)
570                 cYAML_print_tree2file(stderr, err_rc);
571
572         cYAML_free_tree(err_rc);
573
574         return rc;
575 }
576
577 static int jt_show_route(int argc, char **argv)
578 {
579         char *network = NULL, *gateway = NULL;
580         long int hop = -1, prio = -1;
581         int detail = 0, rc, opt;
582         struct cYAML *err_rc = NULL, *show_rc = NULL;
583
584         const char *const short_options = "n:g:h:p:vh";
585         const struct option long_options[] = {
586                 { "net", 1, NULL, 'n' },
587                 { "gateway", 1, NULL, 'g' },
588                 { "hop-count", 1, NULL, 'c' },
589                 { "priority", 1, NULL, 'p' },
590                 { "verbose", 0, NULL, 'v' },
591                 { "help", 0, NULL, 'h' },
592                 { NULL, 0, NULL, 0 },
593         };
594
595         while ((opt = getopt_long(argc, argv, short_options,
596                                    long_options, NULL)) != -1) {
597                 switch (opt) {
598                 case 'n':
599                         network = optarg;
600                         break;
601                 case 'g':
602                         gateway = optarg;
603                         break;
604                 case 'c':
605                         rc = parse_long(optarg, &hop);
606                         if (rc != 0) {
607                                 /* ignore option */
608                                 hop = -1;
609                                 continue;
610                         }
611                         break;
612                 case 'p':
613                         rc = parse_long(optarg, &prio);
614                         if (rc != 0) {
615                                 /* ignore option */
616                                 prio = -1;
617                                 continue;
618                         }
619                         break;
620                 case 'v':
621                         detail = 1;
622                         break;
623                 case 'h':
624                         print_help(route_cmds, "route", "show");
625                         return 0;
626                 default:
627                         return 0;
628                 }
629         }
630
631         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
632                                     &show_rc, &err_rc);
633
634         if (rc != LUSTRE_CFG_RC_NO_ERR)
635                 cYAML_print_tree2file(stderr, err_rc);
636         else if (show_rc)
637                 cYAML_print_tree(show_rc);
638
639         cYAML_free_tree(err_rc);
640         cYAML_free_tree(show_rc);
641
642         return rc;
643 }
644
645 static int jt_show_net(int argc, char **argv)
646 {
647         char *network = NULL;
648         int detail = 0, rc, opt;
649         struct cYAML *err_rc = NULL, *show_rc = NULL;
650
651         const char *const short_options = "n:vh";
652         const struct option long_options[] = {
653                 { "net", 1, NULL, 'n' },
654                 { "verbose", 0, NULL, 'v' },
655                 { "help", 0, NULL, 'h' },
656                 { NULL, 0, NULL, 0 },
657         };
658
659         while ((opt = getopt_long(argc, argv, short_options,
660                                    long_options, NULL)) != -1) {
661                 switch (opt) {
662                 case 'n':
663                         network = optarg;
664                         break;
665                 case 'v':
666                         detail = 1;
667                         break;
668                 case 'h':
669                         print_help(net_cmds, "net", "show");
670                         return 0;
671                 default:
672                         return 0;
673                 }
674         }
675
676         rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
677
678         if (rc != LUSTRE_CFG_RC_NO_ERR)
679                 cYAML_print_tree2file(stderr, err_rc);
680         else if (show_rc)
681                 cYAML_print_tree(show_rc);
682
683         cYAML_free_tree(err_rc);
684         cYAML_free_tree(show_rc);
685
686         return rc;
687 }
688
689 static int jt_show_routing(int argc, char **argv)
690 {
691         struct cYAML *err_rc = NULL, *show_rc = NULL;
692         int rc;
693
694         if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
695                 return 0;
696
697         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
698
699         if (rc != LUSTRE_CFG_RC_NO_ERR)
700                 cYAML_print_tree2file(stderr, err_rc);
701         else if (show_rc)
702                 cYAML_print_tree(show_rc);
703
704         cYAML_free_tree(err_rc);
705         cYAML_free_tree(show_rc);
706
707         return rc;
708 }
709
710 static int jt_show_stats(int argc, char **argv)
711 {
712         int rc;
713         struct cYAML *show_rc = NULL, *err_rc = NULL;
714
715         if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
716                 return 0;
717
718         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
719
720         if (rc != LUSTRE_CFG_RC_NO_ERR)
721                 cYAML_print_tree2file(stderr, err_rc);
722         else if (show_rc)
723                 cYAML_print_tree(show_rc);
724
725         cYAML_free_tree(err_rc);
726         cYAML_free_tree(show_rc);
727
728         return rc;
729 }
730
731 static int jt_show_peer_credits(int argc, char **argv)
732 {
733         int rc;
734         struct cYAML *show_rc = NULL, *err_rc = NULL;
735
736         if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
737                 return 0;
738
739         rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
740
741         if (rc != LUSTRE_CFG_RC_NO_ERR)
742                 cYAML_print_tree2file(stderr, err_rc);
743         else if (show_rc)
744                 cYAML_print_tree(show_rc);
745
746         cYAML_free_tree(err_rc);
747         cYAML_free_tree(show_rc);
748
749         return rc;
750 }
751
752 static inline int jt_lnet(int argc, char **argv)
753 {
754         if (argc < 2)
755                 return CMD_HELP;
756
757         if (argc == 2 &&
758             handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
759                 return 0;
760
761         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
762 }
763
764 static inline int jt_route(int argc, char **argv)
765 {
766         if (argc < 2)
767                 return CMD_HELP;
768
769         if (argc == 2 &&
770             handle_help(route_cmds, "route", NULL, argc, argv) == 0)
771                 return 0;
772
773         return Parser_execarg(argc - 1, &argv[1], route_cmds);
774 }
775
776 static inline int jt_net(int argc, char **argv)
777 {
778         if (argc < 2)
779                 return CMD_HELP;
780
781         if (argc == 2 &&
782             handle_help(net_cmds, "net", NULL, argc, argv) == 0)
783                 return 0;
784
785         return Parser_execarg(argc - 1, &argv[1], net_cmds);
786 }
787
788 static inline int jt_routing(int argc, char **argv)
789 {
790         if (argc < 2)
791                 return CMD_HELP;
792
793         if (argc == 2 &&
794             handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
795                 return 0;
796
797         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
798 }
799
800 static inline int jt_stats(int argc, char **argv)
801 {
802         if (argc < 2)
803                 return CMD_HELP;
804
805         if (argc == 2 &&
806             handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
807                 return 0;
808
809         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
810 }
811
812 static inline int jt_peer_credits(int argc, char **argv)
813 {
814         if (argc < 2)
815                 return CMD_HELP;
816
817         if (argc == 2 &&
818             handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
819                 return 0;
820
821         return Parser_execarg(argc - 1, &argv[1], credits_cmds);
822 }
823
824 static inline int jt_set(int argc, char **argv)
825 {
826         if (argc < 2)
827                 return CMD_HELP;
828
829         if (argc == 2  &&
830             handle_help(set_cmds, "set", NULL, argc, argv) == 0)
831                 return 0;
832
833         return Parser_execarg(argc - 1, &argv[1], set_cmds);
834 }
835
836 static int jt_import(int argc, char **argv)
837 {
838         char *file = NULL;
839         struct cYAML *err_rc = NULL;
840         struct cYAML *show_rc = NULL;
841         int rc = 0, opt, opt_found = 0;
842         char cmd = 'a';
843
844         const char *const short_options = "adsh";
845         const struct option long_options[] = {
846                 { "add", 0, NULL, 'a' },
847                 { "del", 0, NULL, 'd' },
848                 { "show", 0, NULL, 's' },
849                 { "help", 0, NULL, 'h' },
850                 { NULL, 0, NULL, 0 },
851         };
852
853         while ((opt = getopt_long(argc, argv, short_options,
854                                    long_options, NULL)) != -1) {
855                 opt_found = 1;
856                 switch (opt) {
857                 case 'a':
858                 case 'd':
859                 case 's':
860                         cmd = opt;
861                         break;
862                 case 'h':
863                         printf("import FILE\n"
864                                "import < FILE : import a file\n"
865                                "\t--add: add configuration\n"
866                                "\t--del: delete configuration\n"
867                                "\t--show: show configuration\n"
868                                "\t--help: display this help\n"
869                                "If no command option is given then --add"
870                                " is assumed by default\n");
871                         return 0;
872                 default:
873                         return 0;
874                 }
875         }
876
877         /* grab the file name if one exists */
878         if (opt_found && argc == 3)
879                 file = argv[2];
880         else if (!opt_found && argc == 2)
881                 file = argv[1];
882
883         switch (cmd) {
884         case 'a':
885                 rc = lustre_yaml_config(file, &err_rc);
886                 break;
887         case 'd':
888                 rc = lustre_yaml_del(file, &err_rc);
889                 break;
890         case 's':
891                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
892                 cYAML_print_tree(show_rc);
893                 cYAML_free_tree(show_rc);
894                 break;
895         }
896
897         if (rc != LUSTRE_CFG_RC_NO_ERR)
898                 cYAML_print_tree2file(stderr, err_rc);
899
900         cYAML_free_tree(err_rc);
901
902         return rc;
903 }
904
905 static int jt_export(int argc, char **argv)
906 {
907         struct cYAML *show_rc = NULL;
908         struct cYAML *err_rc = NULL;
909         int rc, opt;
910         FILE *f = NULL;
911
912         const char *const short_options = "h";
913         const struct option long_options[] = {
914                 { "help", 0, NULL, 'h' },
915                 { NULL, 0, NULL, 0 },
916         };
917
918         while ((opt = getopt_long(argc, argv, short_options,
919                                    long_options, NULL)) != -1) {
920                 switch (opt) {
921                 case 'h':
922                         printf("export FILE\n"
923                                "export > FILE : export configuration\n"
924                                "\t--help: display this help\n");
925                         return 0;
926                 default:
927                         return 0;
928                 }
929         }
930
931         if (argc >= 2) {
932                 f = fopen(argv[1], "w");
933                 if (f == NULL)
934                         return -1;
935         } else
936                 f = stdout;
937
938         rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
939         if (rc != LUSTRE_CFG_RC_NO_ERR) {
940                 cYAML_print_tree2file(stderr, err_rc);
941                 cYAML_free_tree(err_rc);
942         }
943
944         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
945                                     &err_rc);
946         if (rc != LUSTRE_CFG_RC_NO_ERR) {
947                 cYAML_print_tree2file(stderr, err_rc);
948                 cYAML_free_tree(err_rc);
949         }
950
951         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
952         if (rc != LUSTRE_CFG_RC_NO_ERR) {
953                 cYAML_print_tree2file(stderr, err_rc);
954                 cYAML_free_tree(err_rc);
955         }
956
957         if (show_rc != NULL) {
958                 cYAML_print_tree2file(f, show_rc);
959                 cYAML_free_tree(show_rc);
960         }
961
962         if (argc >= 2)
963                 fclose(f);
964
965         return 0;
966 }
967
968 command_t list[] = {
969         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
970         {"route", jt_route, 0, "route {add | del | show | help}"},
971         {"net", jt_net, 0, "net {add | del | show | help}"},
972         {"routing", jt_routing, 0, "routing {show | help}"},
973         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
974                            " | routing}"},
975         {"import", jt_import, 0, "import {--add | --del | --show | "
976                                  "--help} FILE.yaml"},
977         {"export", jt_export, 0, "export {--help} FILE.yaml"},
978         {"stats", jt_stats, 0, "stats {show | help}"},
979         {"peer_credits", jt_peer_credits, 0, "peer_credits {show | help}"},
980         {"help", Parser_help, 0, "help"},
981         {"exit", Parser_quit, 0, "quit"},
982         {"quit", Parser_quit, 0, "quit"},
983         { 0, 0, 0, NULL }
984 };
985
986 int main(int argc, char **argv)
987 {
988         int rc = 0;
989         struct cYAML *err_rc = NULL;
990
991         rc = lustre_lnet_config_lib_init();
992         if (rc < 0) {
993                 cYAML_build_error(-1, -1, "lnetctl", "startup",
994                                   "cannot register LNet device", &err_rc);
995                 cYAML_print_tree2file(stderr, err_rc);
996                 return rc;
997         }
998
999         Parser_init("lnetctl > ", list);
1000         if (argc > 1) {
1001                 rc = Parser_execarg(argc - 1, &argv[1], list);
1002                 goto errorout;
1003         }
1004
1005         Parser_commands();
1006
1007 errorout:
1008         return rc;
1009 }