Whamcloud - gitweb
LU-3963 libcfs: Use kernel's strncasecmp and remove cfs_get_blocked_sigs
[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) 2013, Intel Corporation.
22  *
23  * Author:
24  *   Amir Shehata <amir.shehata@intel.com>
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <getopt.h>
30 #include <lnet/lnetctl.h>
31 #include <libcfs/libcfsutil.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         optind = 0;
303
304         const char *const short_options = "ah";
305         const struct option long_options[] = {
306                 { "all", 0, NULL, 'a' },
307                 { "help", 0, NULL, 'h' },
308                 { NULL, 0, NULL, 0 },
309         };
310
311         while ((opt = getopt_long(argc, argv, short_options,
312                                    long_options, NULL)) != -1) {
313                 switch (opt) {
314                 case 'a':
315                         load_mod_params = true;
316                         break;
317                 case 'h':
318                         print_help(lnet_cmds, "lnet", "configure");
319                         return 0;
320                 default:
321                         return 0;
322                 }
323         }
324
325         rc = lustre_lnet_config_ni_system(LNET_CONFIGURE, load_mod_params,
326                                           -1, &err_rc);
327
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_unconfig_lnet(int argc, char **argv)
337 {
338         struct cYAML *err_rc = NULL;
339         int rc;
340         optind = 0;
341
342         if (handle_help(lnet_cmds, "lnet", "unconfigure", argc, argv) == 0)
343                 return 0;
344
345         rc = lustre_lnet_config_ni_system(LNET_UNCONFIGURE, 0, -1, &err_rc);
346
347         if (rc != LUSTRE_CFG_RC_NO_ERR)
348                 cYAML_print_tree2file(stderr, err_rc);
349
350         cYAML_free_tree(err_rc);
351
352         return rc;
353 }
354 static int jt_add_route(int argc, char **argv)
355 {
356         char *network = NULL, *gateway = NULL;
357         long int hop = -1, prio = -1;
358         struct cYAML *err_rc = NULL;
359         int rc, opt;
360         optind = 0;
361
362         const char *const short_options = "n:g:c:p:h";
363         const struct option long_options[] = {
364                 { "net", 1, NULL, 'n' },
365                 { "gateway", 1, NULL, 'g' },
366                 { "hop-count", 1, NULL, 'c' },
367                 { "priority", 1, NULL, 'p' },
368                 { "help", 0, NULL, 'h' },
369                 { NULL, 0, NULL, 0 },
370         };
371
372         while ((opt = getopt_long(argc, argv, short_options,
373                                    long_options, NULL)) != -1) {
374                 switch (opt) {
375                 case 'n':
376                         network = optarg;
377                         break;
378                 case 'g':
379                         gateway = optarg;
380                         break;
381                 case 'c':
382                         rc = parse_long(optarg, &hop);
383                         if (rc != 0) {
384                                 /* ignore option */
385                                 hop = -1;
386                                 continue;
387                         }
388                         break;
389                 case 'p':
390                         rc = parse_long(optarg, &prio);
391                         if (rc != 0) {
392                                 /* ingore option */
393                                 prio = -1;
394                                 continue;
395                         }
396                         break;
397                 case 'h':
398                         print_help(route_cmds, "route", "add");
399                         return 0;
400                 default:
401                         return 0;
402                 }
403         }
404
405         rc = lustre_lnet_config_route(network, gateway, hop, prio, -1, &err_rc);
406
407         if (rc != LUSTRE_CFG_RC_NO_ERR)
408                 cYAML_print_tree2file(stderr, err_rc);
409
410         cYAML_free_tree(err_rc);
411
412         return rc;
413 }
414
415 static int jt_add_net(int argc, char **argv)
416 {
417         char *network = NULL, *intf = NULL, *ip2net = NULL, *cpt = NULL;
418         long int pto = -1, pc = -1, pbc = -1, cre = -1;
419         struct cYAML *err_rc = NULL;
420         int rc, opt;
421         optind = 0;
422
423         const char *const short_options = "n:i:p:t:c:b:r:s:h";
424         const struct option long_options[] = {
425                 { "net", 1, NULL, 'n' },
426                 { "if", 1, NULL, 'i' },
427                 { "ip2net", 1, NULL, 'p' },
428                 { "peer-timeout", 1, NULL, 't' },
429                 { "peer-credits", 1, NULL, 'c' },
430                 { "peer-buffer-credits", 1, NULL, 'b' },
431                 { "credits", 1, NULL, 'r' },
432                 { "cpt", 1, NULL, 's' },
433                 { "help", 0, NULL, 'h' },
434                 { NULL, 0, NULL, 0 },
435         };
436
437         while ((opt = getopt_long(argc, argv, short_options,
438                                    long_options, NULL)) != -1) {
439                 switch (opt) {
440                 case 'n':
441                         network = optarg;
442                         break;
443                 case 'i':
444                         intf = optarg;
445                         break;
446                 case 'p':
447                         ip2net = optarg;
448                         break;
449                 case 't':
450                         rc = parse_long(optarg, &pto);
451                         if (rc != 0) {
452                                 /* ignore option */
453                                 pto = -1;
454                                 continue;
455                         }
456                         break;
457                 case 'c':
458                         rc = parse_long(optarg, &pc);
459                         if (rc != 0) {
460                                 /* ignore option */
461                                 pc = -1;
462                                 continue;
463                         }
464                         break;
465                 case 'b':
466                         rc = parse_long(optarg, &pbc);
467                         if (rc != 0) {
468                                 /* ignore option */
469                                 pbc = -1;
470                                 continue;
471                         }
472                         break;
473                 case 'r':
474                         rc = parse_long(optarg, &cre);
475                         if (rc != 0) {
476                                 /* ignore option */
477                                 cre = -1;
478                                 continue;
479                         }
480                         break;
481                 case 's':
482                         cpt = optarg;
483                         break;
484                 case 'h':
485                         print_help(net_cmds, "net", "add");
486                         return 0;
487                 default:
488                         return 0;
489                 }
490         }
491
492         rc = lustre_lnet_config_net(network, intf, ip2net, pto, pc, pbc,
493                                     cre, cpt, -1, &err_rc);
494
495         if (rc != LUSTRE_CFG_RC_NO_ERR)
496                 cYAML_print_tree2file(stderr, err_rc);
497
498         cYAML_free_tree(err_rc);
499
500         return rc;
501 }
502
503 static int jt_del_route(int argc, char **argv)
504 {
505         char *network = NULL, *gateway = NULL;
506         struct cYAML *err_rc = NULL;
507         int rc, opt;
508         optind = 0;
509
510         const char *const short_options = "n:g:h";
511         const struct option long_options[] = {
512                 { "net", 1, NULL, 'n' },
513                 { "gateway", 1, NULL, 'g' },
514                 { "help", 0, NULL, 'h' },
515                 { NULL, 0, NULL, 0 },
516         };
517
518         while ((opt = getopt_long(argc, argv, short_options,
519                                    long_options, NULL)) != -1) {
520                 switch (opt) {
521                 case 'n':
522                         network = optarg;
523                         break;
524                 case 'g':
525                         gateway = optarg;
526                         break;
527                 case 'h':
528                         print_help(route_cmds, "route", "del");
529                         return 0;
530                 default:
531                         return 0;
532                 }
533         }
534
535         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
536
537         if (rc != LUSTRE_CFG_RC_NO_ERR)
538                 cYAML_print_tree2file(stderr, err_rc);
539
540         cYAML_free_tree(err_rc);
541
542         return rc;
543 }
544
545 static int jt_del_net(int argc, char **argv)
546 {
547         char *network = NULL;
548         struct cYAML *err_rc = NULL;
549         int rc, opt;
550         optind = 0;
551
552         const char *const short_options = "n:h";
553         const struct option long_options[] = {
554                 { "net", 1, NULL, 'n' },
555                 { "help", 0, NULL, 'h' },
556                 { NULL, 0, NULL, 0 },
557         };
558
559         while ((opt = getopt_long(argc, argv, short_options,
560                                    long_options, NULL)) != -1) {
561                 switch (opt) {
562                 case 'n':
563                         network = optarg;
564                         break;
565                 case 'h':
566                         print_help(net_cmds, "net", "del");
567                         return 0;
568                 default:
569                         return 0;
570                 }
571         }
572
573         rc = lustre_lnet_del_net(network, -1, &err_rc);
574
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_show_route(int argc, char **argv)
584 {
585         char *network = NULL, *gateway = NULL;
586         long int hop = -1, prio = -1;
587         int detail = 0, rc, opt;
588         struct cYAML *err_rc = NULL, *show_rc = NULL;
589         optind = 0;
590
591         const char *const short_options = "n:g:h:p:vh";
592         const struct option long_options[] = {
593                 { "net", 1, NULL, 'n' },
594                 { "gateway", 1, NULL, 'g' },
595                 { "hop-count", 1, NULL, 'c' },
596                 { "priority", 1, NULL, 'p' },
597                 { "verbose", 0, NULL, 'v' },
598                 { "help", 0, NULL, 'h' },
599                 { NULL, 0, NULL, 0 },
600         };
601
602         while ((opt = getopt_long(argc, argv, short_options,
603                                    long_options, NULL)) != -1) {
604                 switch (opt) {
605                 case 'n':
606                         network = optarg;
607                         break;
608                 case 'g':
609                         gateway = optarg;
610                         break;
611                 case 'c':
612                         rc = parse_long(optarg, &hop);
613                         if (rc != 0) {
614                                 /* ignore option */
615                                 hop = -1;
616                                 continue;
617                         }
618                         break;
619                 case 'p':
620                         rc = parse_long(optarg, &prio);
621                         if (rc != 0) {
622                                 /* ignore option */
623                                 prio = -1;
624                                 continue;
625                         }
626                         break;
627                 case 'v':
628                         detail = 1;
629                         break;
630                 case 'h':
631                         print_help(route_cmds, "route", "show");
632                         return 0;
633                 default:
634                         return 0;
635                 }
636         }
637
638         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
639                                     &show_rc, &err_rc);
640
641         if (rc != LUSTRE_CFG_RC_NO_ERR)
642                 cYAML_print_tree2file(stderr, err_rc);
643         else if (show_rc)
644                 cYAML_print_tree(show_rc);
645
646         cYAML_free_tree(err_rc);
647         cYAML_free_tree(show_rc);
648
649         return rc;
650 }
651
652 static int jt_show_net(int argc, char **argv)
653 {
654         char *network = NULL;
655         int detail = 0, rc, opt;
656         struct cYAML *err_rc = NULL, *show_rc = NULL;
657         optind = 0;
658
659         const char *const short_options = "n:vh";
660         const struct option long_options[] = {
661                 { "net", 1, NULL, 'n' },
662                 { "verbose", 0, NULL, 'v' },
663                 { "help", 0, NULL, 'h' },
664                 { NULL, 0, NULL, 0 },
665         };
666
667         while ((opt = getopt_long(argc, argv, short_options,
668                                    long_options, NULL)) != -1) {
669                 switch (opt) {
670                 case 'n':
671                         network = optarg;
672                         break;
673                 case 'v':
674                         detail = 1;
675                         break;
676                 case 'h':
677                         print_help(net_cmds, "net", "show");
678                         return 0;
679                 default:
680                         return 0;
681                 }
682         }
683
684         rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
685
686         if (rc != LUSTRE_CFG_RC_NO_ERR)
687                 cYAML_print_tree2file(stderr, err_rc);
688         else if (show_rc)
689                 cYAML_print_tree(show_rc);
690
691         cYAML_free_tree(err_rc);
692         cYAML_free_tree(show_rc);
693
694         return rc;
695 }
696
697 static int jt_show_routing(int argc, char **argv)
698 {
699         struct cYAML *err_rc = NULL, *show_rc = NULL;
700         int rc;
701
702         if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
703                 return 0;
704
705         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
706
707         if (rc != LUSTRE_CFG_RC_NO_ERR)
708                 cYAML_print_tree2file(stderr, err_rc);
709         else if (show_rc)
710                 cYAML_print_tree(show_rc);
711
712         cYAML_free_tree(err_rc);
713         cYAML_free_tree(show_rc);
714
715         return rc;
716 }
717
718 static int jt_show_stats(int argc, char **argv)
719 {
720         int rc;
721         struct cYAML *show_rc = NULL, *err_rc = NULL;
722
723         if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
724                 return 0;
725
726         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
727
728         if (rc != LUSTRE_CFG_RC_NO_ERR)
729                 cYAML_print_tree2file(stderr, err_rc);
730         else if (show_rc)
731                 cYAML_print_tree(show_rc);
732
733         cYAML_free_tree(err_rc);
734         cYAML_free_tree(show_rc);
735
736         return rc;
737 }
738
739 static int jt_show_peer_credits(int argc, char **argv)
740 {
741         int rc;
742         struct cYAML *show_rc = NULL, *err_rc = NULL;
743         optind = 0;
744
745         if (handle_help(credits_cmds, "peer_credits", "show", argc, argv) == 0)
746                 return 0;
747
748         rc = lustre_lnet_show_peer_credits(-1, &show_rc, &err_rc);
749
750         if (rc != LUSTRE_CFG_RC_NO_ERR)
751                 cYAML_print_tree2file(stderr, err_rc);
752         else if (show_rc)
753                 cYAML_print_tree(show_rc);
754
755         cYAML_free_tree(err_rc);
756         cYAML_free_tree(show_rc);
757
758         return rc;
759 }
760
761 static inline int jt_lnet(int argc, char **argv)
762 {
763         if (argc < 2)
764                 return CMD_HELP;
765
766         if (argc == 2 &&
767             handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
768                 return 0;
769
770         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
771 }
772
773 static inline int jt_route(int argc, char **argv)
774 {
775         if (argc < 2)
776                 return CMD_HELP;
777
778         if (argc == 2 &&
779             handle_help(route_cmds, "route", NULL, argc, argv) == 0)
780                 return 0;
781
782         return Parser_execarg(argc - 1, &argv[1], route_cmds);
783 }
784
785 static inline int jt_net(int argc, char **argv)
786 {
787         if (argc < 2)
788                 return CMD_HELP;
789
790         if (argc == 2 &&
791             handle_help(net_cmds, "net", NULL, argc, argv) == 0)
792                 return 0;
793
794         return Parser_execarg(argc - 1, &argv[1], net_cmds);
795 }
796
797 static inline int jt_routing(int argc, char **argv)
798 {
799         if (argc < 2)
800                 return CMD_HELP;
801
802         if (argc == 2 &&
803             handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
804                 return 0;
805
806         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
807 }
808
809 static inline int jt_stats(int argc, char **argv)
810 {
811         if (argc < 2)
812                 return CMD_HELP;
813
814         if (argc == 2 &&
815             handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
816                 return 0;
817
818         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
819 }
820
821 static inline int jt_peer_credits(int argc, char **argv)
822 {
823         if (argc < 2)
824                 return CMD_HELP;
825
826         if (argc == 2 &&
827             handle_help(credits_cmds, "peer_credits", NULL, argc, argv) == 0)
828                 return 0;
829
830         return Parser_execarg(argc - 1, &argv[1], credits_cmds);
831 }
832
833 static inline int jt_set(int argc, char **argv)
834 {
835         if (argc < 2)
836                 return CMD_HELP;
837
838         if (argc == 2  &&
839             handle_help(set_cmds, "set", NULL, argc, argv) == 0)
840                 return 0;
841
842         return Parser_execarg(argc - 1, &argv[1], set_cmds);
843 }
844
845 static int jt_import(int argc, char **argv)
846 {
847         char *file = NULL;
848         struct cYAML *err_rc = NULL;
849         struct cYAML *show_rc = NULL;
850         int rc = 0, opt, opt_found = 0;
851         optind = 0;
852         char cmd = 'a';
853
854         const char *const short_options = "adsh";
855         const struct option long_options[] = {
856                 { "add", 0, NULL, 'a' },
857                 { "del", 0, NULL, 'd' },
858                 { "show", 0, NULL, 's' },
859                 { "help", 0, NULL, 'h' },
860                 { NULL, 0, NULL, 0 },
861         };
862
863         while ((opt = getopt_long(argc, argv, short_options,
864                                    long_options, NULL)) != -1) {
865                 opt_found = 1;
866                 switch (opt) {
867                 case 'a':
868                 case 'd':
869                 case 's':
870                         cmd = opt;
871                         break;
872                 case 'h':
873                         printf("import FILE\n"
874                                "import < FILE : import a file\n"
875                                "\t--add: add configuration\n"
876                                "\t--del: delete configuration\n"
877                                "\t--show: show configuration\n"
878                                "\t--help: display this help\n"
879                                "If no command option is given then --add"
880                                " is assumed by default\n");
881                         return 0;
882                 default:
883                         return 0;
884                 }
885         }
886
887         /* grab the file name if one exists */
888         if (opt_found && argc == 3)
889                 file = argv[2];
890         else if (!opt_found && argc == 2)
891                 file = argv[1];
892
893         switch (cmd) {
894         case 'a':
895                 rc = lustre_yaml_config(file, &err_rc);
896                 break;
897         case 'd':
898                 rc = lustre_yaml_del(file, &err_rc);
899                 break;
900         case 's':
901                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
902                 cYAML_print_tree(show_rc);
903                 cYAML_free_tree(show_rc);
904                 break;
905         }
906
907         if (rc != LUSTRE_CFG_RC_NO_ERR)
908                 cYAML_print_tree2file(stderr, err_rc);
909
910         cYAML_free_tree(err_rc);
911
912         return rc;
913 }
914
915 static int jt_export(int argc, char **argv)
916 {
917         struct cYAML *show_rc = NULL;
918         struct cYAML *err_rc = NULL;
919         int rc, opt;
920         FILE *f = NULL;
921         optind = 0;
922
923         const char *const short_options = "h";
924         const struct option long_options[] = {
925                 { "help", 0, NULL, 'h' },
926                 { NULL, 0, NULL, 0 },
927         };
928
929         while ((opt = getopt_long(argc, argv, short_options,
930                                    long_options, NULL)) != -1) {
931                 switch (opt) {
932                 case 'h':
933                         printf("export FILE\n"
934                                "export > FILE : export configuration\n"
935                                "\t--help: display this help\n");
936                         return 0;
937                 default:
938                         return 0;
939                 }
940         }
941
942         if (argc >= 2) {
943                 f = fopen(argv[1], "w");
944                 if (f == NULL)
945                         return -1;
946         } else
947                 f = stdout;
948
949         rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
950         if (rc != LUSTRE_CFG_RC_NO_ERR) {
951                 cYAML_print_tree2file(stderr, err_rc);
952                 cYAML_free_tree(err_rc);
953         }
954
955         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
956                                     &err_rc);
957         if (rc != LUSTRE_CFG_RC_NO_ERR) {
958                 cYAML_print_tree2file(stderr, err_rc);
959                 cYAML_free_tree(err_rc);
960         }
961
962         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
963         if (rc != LUSTRE_CFG_RC_NO_ERR) {
964                 cYAML_print_tree2file(stderr, err_rc);
965                 cYAML_free_tree(err_rc);
966         }
967
968         if (show_rc != NULL) {
969                 cYAML_print_tree2file(f, show_rc);
970                 cYAML_free_tree(show_rc);
971         }
972
973         if (argc >= 2)
974                 fclose(f);
975
976         return 0;
977 }
978
979 command_t list[] = {
980         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
981         {"route", jt_route, 0, "route {add | del | show | help}"},
982         {"net", jt_net, 0, "net {add | del | show | help}"},
983         {"routing", jt_routing, 0, "routing {show | help}"},
984         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
985                            " | routing}"},
986         {"import", jt_import, 0, "import {--add | --del | --show | "
987                                  "--help} FILE.yaml"},
988         {"export", jt_export, 0, "export {--help} FILE.yaml"},
989         {"stats", jt_stats, 0, "stats {show | help}"},
990         {"peer_credits", jt_peer_credits, 0, "stats {show | help}"},
991         {"help", Parser_help, 0, "help"},
992         {"exit", Parser_quit, 0, "quit"},
993         {"quit", Parser_quit, 0, "quit"},
994         { 0, 0, 0, NULL }
995 };
996
997 int main(int argc, char **argv)
998 {
999         int rc = 0;
1000         struct cYAML *err_rc = NULL;
1001
1002         rc = lustre_lnet_config_lib_init();
1003         if (rc < 0) {
1004                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1005                                   "cannot register LNet device", &err_rc);
1006                 cYAML_print_tree2file(stderr, err_rc);
1007                 return rc;
1008         }
1009
1010         rc = libcfs_arch_init();
1011         if (rc < 0) {
1012                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1013                                   "cannot initialize libcfs", &err_rc);
1014                 cYAML_print_tree2file(stderr, err_rc);
1015                 return rc;
1016         }
1017
1018         Parser_init("lnetctl > ", list);
1019         if (argc > 1) {
1020                 rc = Parser_execarg(argc - 1, &argv[1], list);
1021                 goto errorout;
1022         }
1023
1024         Parser_commands();
1025
1026 errorout:
1027         libcfs_arch_cleanup();
1028         return rc;
1029 }