Whamcloud - gitweb
1c6c73137f57a7a405bcc05e7e4f7926eca8cf03
[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--prim_nid: Primary NID of the peer. If not provided then the first\n"
139          "\t            NID in the list becomes the Primary 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--prim_nid: Primary NID of the 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--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, cpt_rc = -1;
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                         cpt_rc = cfs_expr_list_parse(optarg,
546                                                      strlen(optarg), 0,
547                                                      UINT_MAX, &global_cpts);
548                         break;
549                 case 'h':
550                         print_help(net_cmds, "net", "add");
551                         return 0;
552                 default:
553                         return 0;
554                 }
555         }
556
557         if (pto > 0 || pc > 0 || pbc > 0 || cre > 0) {
558                 tunables.lt_cmn.lct_peer_timeout = pto;
559                 tunables.lt_cmn.lct_peer_tx_credits = pc;
560                 tunables.lt_cmn.lct_peer_rtr_credits = pbc;
561                 tunables.lt_cmn.lct_max_tx_credits = cre;
562                 found = true;
563         }
564
565         rc = lustre_lnet_config_ni(&nw_descr,
566                                    (cpt_rc == 0) ? global_cpts: NULL,
567                                    ip2net, (found) ? &tunables : NULL,
568                                    -1, &err_rc);
569
570         if (global_cpts != NULL)
571                 cfs_expr_list_free(global_cpts);
572
573 failed:
574         if (rc != LUSTRE_CFG_RC_NO_ERR)
575                 cYAML_print_tree2file(stderr, err_rc);
576
577         cYAML_free_tree(err_rc);
578
579         return rc;
580 }
581
582 static int jt_del_route(int argc, char **argv)
583 {
584         char *network = NULL, *gateway = NULL;
585         struct cYAML *err_rc = NULL;
586         int rc, opt;
587
588         const char *const short_options = "n:g:h";
589         const struct option long_options[] = {
590                 { "net", 1, NULL, 'n' },
591                 { "gateway", 1, NULL, 'g' },
592                 { "help", 0, NULL, 'h' },
593                 { NULL, 0, NULL, 0 },
594         };
595
596         while ((opt = getopt_long(argc, argv, short_options,
597                                    long_options, NULL)) != -1) {
598                 switch (opt) {
599                 case 'n':
600                         network = optarg;
601                         break;
602                 case 'g':
603                         gateway = optarg;
604                         break;
605                 case 'h':
606                         print_help(route_cmds, "route", "del");
607                         return 0;
608                 default:
609                         return 0;
610                 }
611         }
612
613         rc = lustre_lnet_del_route(network, gateway, -1, &err_rc);
614
615         if (rc != LUSTRE_CFG_RC_NO_ERR)
616                 cYAML_print_tree2file(stderr, err_rc);
617
618         cYAML_free_tree(err_rc);
619
620         return rc;
621 }
622
623 static int jt_del_ni(int argc, char **argv)
624 {
625         struct cYAML *err_rc = NULL;
626         int rc, opt;
627         struct lnet_dlc_network_descr nw_descr;
628
629         lustre_lnet_init_nw_descr(&nw_descr);
630
631         const char *const short_options = "n:i:h";
632         const struct option long_options[] = {
633                 { "net", 1, NULL, 'n' },
634                 { "if", 1, NULL, 'i' },
635                 { "help", 0, NULL, 'h' },
636                 { NULL, 0, NULL, 0 },
637         };
638
639         while ((opt = getopt_long(argc, argv, short_options,
640                                    long_options, NULL)) != -1) {
641                 switch (opt) {
642                 case 'n':
643                         nw_descr.nw_id = libcfs_str2net(optarg);
644                         break;
645                 case 'i':
646                         rc = lustre_lnet_parse_interfaces(optarg, &nw_descr);
647                         if (rc != 0) {
648                                 cYAML_build_error(-1, -1, "ni", "add",
649                                                 "bad interface list",
650                                                 &err_rc);
651                                 goto out;
652                         }
653                         break;
654                 case 'h':
655                         print_help(net_cmds, "net", "del");
656                         return 0;
657                 default:
658                         return 0;
659                 }
660         }
661
662         rc = lustre_lnet_del_ni(&nw_descr, -1, &err_rc);
663
664 out:
665         if (rc != LUSTRE_CFG_RC_NO_ERR)
666                 cYAML_print_tree2file(stderr, err_rc);
667
668         cYAML_free_tree(err_rc);
669
670         return rc;
671 }
672
673 static int jt_show_route(int argc, char **argv)
674 {
675         char *network = NULL, *gateway = NULL;
676         long int hop = -1, prio = -1;
677         int detail = 0, rc, opt;
678         struct cYAML *err_rc = NULL, *show_rc = NULL;
679
680         const char *const short_options = "n:g:h:p:vh";
681         const struct option long_options[] = {
682                 { "net", 1, NULL, 'n' },
683                 { "gateway", 1, NULL, 'g' },
684                 { "hop-count", 1, NULL, 'c' },
685                 { "priority", 1, NULL, 'p' },
686                 { "verbose", 0, NULL, 'v' },
687                 { "help", 0, NULL, 'h' },
688                 { NULL, 0, NULL, 0 },
689         };
690
691         while ((opt = getopt_long(argc, argv, short_options,
692                                    long_options, NULL)) != -1) {
693                 switch (opt) {
694                 case 'n':
695                         network = optarg;
696                         break;
697                 case 'g':
698                         gateway = optarg;
699                         break;
700                 case 'c':
701                         rc = parse_long(optarg, &hop);
702                         if (rc != 0) {
703                                 /* ignore option */
704                                 hop = -1;
705                                 continue;
706                         }
707                         break;
708                 case 'p':
709                         rc = parse_long(optarg, &prio);
710                         if (rc != 0) {
711                                 /* ignore option */
712                                 prio = -1;
713                                 continue;
714                         }
715                         break;
716                 case 'v':
717                         detail = 1;
718                         break;
719                 case 'h':
720                         print_help(route_cmds, "route", "show");
721                         return 0;
722                 default:
723                         return 0;
724                 }
725         }
726
727         rc = lustre_lnet_show_route(network, gateway, hop, prio, detail, -1,
728                                     &show_rc, &err_rc);
729
730         if (rc != LUSTRE_CFG_RC_NO_ERR)
731                 cYAML_print_tree2file(stderr, err_rc);
732         else if (show_rc)
733                 cYAML_print_tree(show_rc);
734
735         cYAML_free_tree(err_rc);
736         cYAML_free_tree(show_rc);
737
738         return rc;
739 }
740
741 static int jt_show_net(int argc, char **argv)
742 {
743         char *network = NULL;
744         int detail = 0, rc, opt;
745         struct cYAML *err_rc = NULL, *show_rc = NULL;
746
747         const char *const short_options = "n:vh";
748         const struct option long_options[] = {
749                 { "net", 1, NULL, 'n' },
750                 { "verbose", 0, NULL, 'v' },
751                 { "help", 0, NULL, 'h' },
752                 { NULL, 0, NULL, 0 },
753         };
754
755         while ((opt = getopt_long(argc, argv, short_options,
756                                    long_options, NULL)) != -1) {
757                 switch (opt) {
758                 case 'n':
759                         network = optarg;
760                         break;
761                 case 'v':
762                         detail = 1;
763                         break;
764                 case 'h':
765                         print_help(net_cmds, "net", "show");
766                         return 0;
767                 default:
768                         return 0;
769                 }
770         }
771
772         rc = lustre_lnet_show_net(network, detail, -1, &show_rc, &err_rc);
773
774         if (rc != LUSTRE_CFG_RC_NO_ERR)
775                 cYAML_print_tree2file(stderr, err_rc);
776         else if (show_rc)
777                 cYAML_print_tree(show_rc);
778
779         cYAML_free_tree(err_rc);
780         cYAML_free_tree(show_rc);
781
782         return rc;
783 }
784
785 static int jt_show_routing(int argc, char **argv)
786 {
787         struct cYAML *err_rc = NULL, *show_rc = NULL;
788         int rc;
789
790         if (handle_help(routing_cmds, "routing", "show", argc, argv) == 0)
791                 return 0;
792
793         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
794
795         if (rc != LUSTRE_CFG_RC_NO_ERR)
796                 cYAML_print_tree2file(stderr, err_rc);
797         else if (show_rc)
798                 cYAML_print_tree(show_rc);
799
800         cYAML_free_tree(err_rc);
801         cYAML_free_tree(show_rc);
802
803         return rc;
804 }
805
806 static int jt_show_stats(int argc, char **argv)
807 {
808         int rc;
809         struct cYAML *show_rc = NULL, *err_rc = NULL;
810
811         if (handle_help(stats_cmds, "stats", "show", argc, argv) == 0)
812                 return 0;
813
814         rc = lustre_lnet_show_stats(-1, &show_rc, &err_rc);
815
816         if (rc != LUSTRE_CFG_RC_NO_ERR)
817                 cYAML_print_tree2file(stderr, err_rc);
818         else if (show_rc)
819                 cYAML_print_tree(show_rc);
820
821         cYAML_free_tree(err_rc);
822         cYAML_free_tree(show_rc);
823
824         return rc;
825 }
826
827 static int jt_show_numa(int argc, char **argv)
828 {
829         int rc;
830         struct cYAML *show_rc = NULL, *err_rc = NULL;
831
832         if (handle_help(numa_cmds, "numa", "show", argc, argv) == 0)
833                 return 0;
834
835         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
836
837         if (rc != LUSTRE_CFG_RC_NO_ERR)
838                 cYAML_print_tree2file(stderr, err_rc);
839         else if (show_rc)
840                 cYAML_print_tree(show_rc);
841
842         cYAML_free_tree(err_rc);
843         cYAML_free_tree(show_rc);
844
845         return rc;
846 }
847
848 static inline int jt_lnet(int argc, char **argv)
849 {
850         if (argc < 2)
851                 return CMD_HELP;
852
853         if (argc == 2 &&
854             handle_help(lnet_cmds, "lnet", NULL, argc, argv) == 0)
855                 return 0;
856
857         return Parser_execarg(argc - 1, &argv[1], lnet_cmds);
858 }
859
860 static inline int jt_route(int argc, char **argv)
861 {
862         if (argc < 2)
863                 return CMD_HELP;
864
865         if (argc == 2 &&
866             handle_help(route_cmds, "route", NULL, argc, argv) == 0)
867                 return 0;
868
869         return Parser_execarg(argc - 1, &argv[1], route_cmds);
870 }
871
872 static inline int jt_net(int argc, char **argv)
873 {
874         if (argc < 2)
875                 return CMD_HELP;
876
877         if (argc == 2 &&
878             handle_help(net_cmds, "net", NULL, argc, argv) == 0)
879                 return 0;
880
881         return Parser_execarg(argc - 1, &argv[1], net_cmds);
882 }
883
884 static inline int jt_routing(int argc, char **argv)
885 {
886         if (argc < 2)
887                 return CMD_HELP;
888
889         if (argc == 2 &&
890             handle_help(routing_cmds, "routing", NULL, argc, argv) == 0)
891                 return 0;
892
893         return Parser_execarg(argc - 1, &argv[1], routing_cmds);
894 }
895
896 static inline int jt_stats(int argc, char **argv)
897 {
898         if (argc < 2)
899                 return CMD_HELP;
900
901         if (argc == 2 &&
902             handle_help(stats_cmds, "stats", NULL, argc, argv) == 0)
903                 return 0;
904
905         return Parser_execarg(argc - 1, &argv[1], stats_cmds);
906 }
907
908 static inline int jt_numa(int argc, char **argv)
909 {
910         if (argc < 2)
911                 return CMD_HELP;
912
913         if (argc == 2 &&
914             handle_help(numa_cmds, "numa", NULL, argc, argv) == 0)
915                 return 0;
916
917         return Parser_execarg(argc - 1, &argv[1], numa_cmds);
918 }
919
920 static inline int jt_peers(int argc, char **argv)
921 {
922         if (argc < 2)
923                 return CMD_HELP;
924
925         if (argc == 2 &&
926             handle_help(peer_cmds, "peer", NULL, argc, argv) == 0)
927                 return 0;
928
929         return Parser_execarg(argc - 1, &argv[1], peer_cmds);
930 }
931
932 static inline int jt_set(int argc, char **argv)
933 {
934         if (argc < 2)
935                 return CMD_HELP;
936
937         if (argc == 2  &&
938             handle_help(set_cmds, "set", NULL, argc, argv) == 0)
939                 return 0;
940
941         return Parser_execarg(argc - 1, &argv[1], set_cmds);
942 }
943
944 static int jt_import(int argc, char **argv)
945 {
946         char *file = NULL;
947         struct cYAML *err_rc = NULL;
948         struct cYAML *show_rc = NULL;
949         int rc = 0, opt, opt_found = 0;
950         char cmd = 'a';
951
952         const char *const short_options = "adsh";
953         const struct option long_options[] = {
954                 { "add", 0, NULL, 'a' },
955                 { "del", 0, NULL, 'd' },
956                 { "show", 0, NULL, 's' },
957                 { "help", 0, NULL, 'h' },
958                 { NULL, 0, NULL, 0 },
959         };
960
961         while ((opt = getopt_long(argc, argv, short_options,
962                                    long_options, NULL)) != -1) {
963                 opt_found = 1;
964                 switch (opt) {
965                 case 'a':
966                 case 'd':
967                 case 's':
968                         cmd = opt;
969                         break;
970                 case 'h':
971                         printf("import FILE\n"
972                                "import < FILE : import a file\n"
973                                "\t--add: add configuration\n"
974                                "\t--del: delete configuration\n"
975                                "\t--show: show configuration\n"
976                                "\t--help: display this help\n"
977                                "If no command option is given then --add"
978                                " is assumed by default\n");
979                         return 0;
980                 default:
981                         return 0;
982                 }
983         }
984
985         /* grab the file name if one exists */
986         if (opt_found && argc == 3)
987                 file = argv[2];
988         else if (!opt_found && argc == 2)
989                 file = argv[1];
990
991         switch (cmd) {
992         case 'a':
993                 rc = lustre_yaml_config(file, &err_rc);
994                 break;
995         case 'd':
996                 rc = lustre_yaml_del(file, &err_rc);
997                 break;
998         case 's':
999                 rc = lustre_yaml_show(file, &show_rc, &err_rc);
1000                 cYAML_print_tree(show_rc);
1001                 cYAML_free_tree(show_rc);
1002                 break;
1003         }
1004
1005         cYAML_print_tree2file(stderr, err_rc);
1006
1007         cYAML_free_tree(err_rc);
1008
1009         return rc;
1010 }
1011
1012 static int jt_export(int argc, char **argv)
1013 {
1014         struct cYAML *show_rc = NULL;
1015         struct cYAML *err_rc = NULL;
1016         int rc, opt;
1017         FILE *f = NULL;
1018
1019         const char *const short_options = "h";
1020         const struct option long_options[] = {
1021                 { "help", 0, NULL, 'h' },
1022                 { NULL, 0, NULL, 0 },
1023         };
1024
1025         while ((opt = getopt_long(argc, argv, short_options,
1026                                    long_options, NULL)) != -1) {
1027                 switch (opt) {
1028                 case 'h':
1029                         printf("export FILE\n"
1030                                "export > FILE : export configuration\n"
1031                                "\t--help: display this help\n");
1032                         return 0;
1033                 default:
1034                         return 0;
1035                 }
1036         }
1037
1038         if (argc >= 2) {
1039                 f = fopen(argv[1], "w");
1040                 if (f == NULL)
1041                         return -1;
1042         } else
1043                 f = stdout;
1044
1045         rc = lustre_lnet_show_net(NULL, 1, -1, &show_rc, &err_rc);
1046         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1047                 cYAML_print_tree2file(stderr, err_rc);
1048                 cYAML_free_tree(err_rc);
1049         }
1050
1051         rc = lustre_lnet_show_route(NULL, NULL, -1, -1, 1, -1, &show_rc,
1052                                     &err_rc);
1053         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1054                 cYAML_print_tree2file(stderr, err_rc);
1055                 cYAML_free_tree(err_rc);
1056         }
1057
1058         rc = lustre_lnet_show_routing(-1, &show_rc, &err_rc);
1059         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1060                 cYAML_print_tree2file(stderr, err_rc);
1061                 cYAML_free_tree(err_rc);
1062         }
1063
1064         rc = lustre_lnet_show_peer(NULL, 1, -1, &show_rc, &err_rc);
1065         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1066                 cYAML_print_tree2file(stderr, err_rc);
1067                 cYAML_free_tree(err_rc);
1068         }
1069
1070         rc = lustre_lnet_show_numa_range(-1, &show_rc, &err_rc);
1071         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1072                 cYAML_print_tree2file(stderr, err_rc);
1073                 cYAML_free_tree(err_rc);
1074         }
1075
1076         if (show_rc != NULL) {
1077                 cYAML_print_tree2file(f, show_rc);
1078                 cYAML_free_tree(show_rc);
1079         }
1080
1081         if (argc >= 2)
1082                 fclose(f);
1083
1084         return 0;
1085 }
1086
1087 static int jt_add_peer_nid(int argc, char **argv)
1088 {
1089         char *prim_nid = NULL;
1090         char **nids = NULL, **nids2 = NULL;
1091         int size = 0;
1092         struct cYAML *err_rc = NULL;
1093         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i;
1094         bool non_mr = false;
1095
1096         const char *const short_options = "k:n:mh";
1097         const struct option long_options[] = {
1098                 { "prim_nid", 1, NULL, 'k' },
1099                 { "nid", 1, NULL, 'n' },
1100                 { "non_mr", 0, NULL, 'm'},
1101                 { "help", 0, NULL, 'h' },
1102                 { NULL, 0, NULL, 0 },
1103         };
1104
1105         while ((opt = getopt_long(argc, argv, short_options,
1106                                   long_options, NULL)) != -1) {
1107                 switch (opt) {
1108                 case 'k':
1109                         prim_nid = optarg;
1110                         break;
1111                 case 'n':
1112                         size = lustre_lnet_parse_nids(optarg, nids, size,
1113                                                       &nids2);
1114                         if (nids2 == NULL)
1115                                 goto failed;
1116                         nids = nids2;
1117                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1118                         break;
1119                 case 'm':
1120                         non_mr = true;
1121                         break;
1122                 case 'h':
1123                         print_help(peer_cmds, "peer", "add");
1124                         return 0;
1125                 default:
1126                         return 0;
1127                 }
1128         }
1129
1130         rc = lustre_lnet_config_peer_nid(prim_nid, nids, size,
1131                                          !non_mr, -1, &err_rc);
1132
1133 failed:
1134         for (i = 0; i < size; i++)
1135                 free(nids[i]);
1136         free(nids);
1137
1138         if (rc != LUSTRE_CFG_RC_NO_ERR)
1139                 cYAML_print_tree2file(stderr, err_rc);
1140
1141         cYAML_free_tree(err_rc);
1142
1143         return rc;
1144 }
1145
1146 static int jt_del_peer_nid(int argc, char **argv)
1147 {
1148         char *prim_nid = NULL;
1149         char **nids = NULL, **nids2 = NULL;
1150         struct cYAML *err_rc = NULL;
1151         int rc = LUSTRE_CFG_RC_NO_ERR, opt, i, size = 0;
1152
1153         const char *const short_options = "k:n:h";
1154         const struct option long_options[] = {
1155                 { "prim_nid", 1, NULL, 'k' },
1156                 { "nid", 1, NULL, 'n' },
1157                 { "help", 0, NULL, 'h' },
1158                 { NULL, 0, NULL, 0 },
1159         };
1160
1161         while ((opt = getopt_long(argc, argv, short_options,
1162                                   long_options, NULL)) != -1) {
1163                 switch (opt) {
1164                 case 'k':
1165                         prim_nid = optarg;
1166                         break;
1167                 case 'n':
1168                         size = lustre_lnet_parse_nids(optarg, nids, size,
1169                                                       &nids2);
1170                         if (nids2 == NULL)
1171                                 goto failed;
1172                         nids = nids2;
1173                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1174                         break;
1175                 case 'h':
1176                         print_help(peer_cmds, "peer", "del");
1177                         return 0;
1178                 default:
1179                         return 0;
1180                 }
1181         }
1182
1183         rc = lustre_lnet_del_peer_nid(prim_nid, nids, size, -1, &err_rc);
1184
1185 failed:
1186         for (i = 0; i < size; i++)
1187                 free(nids[i]);
1188         free(nids);
1189
1190         if (rc != LUSTRE_CFG_RC_NO_ERR)
1191                 cYAML_print_tree2file(stderr, err_rc);
1192
1193         cYAML_free_tree(err_rc);
1194
1195         return rc;
1196 }
1197
1198 static int jt_show_peer(int argc, char **argv)
1199 {
1200         char *nid = NULL;
1201         int rc, opt;
1202         struct cYAML *err_rc = NULL, *show_rc = NULL;
1203         int detail = 0;
1204
1205         const char *const short_options = "k:vh";
1206         const struct option long_options[] = {
1207                 { "nid", 1, NULL, 'n' },
1208                 { "verbose", 0, NULL, 'v' },
1209                 { "help", 0, NULL, 'h' },
1210                 { NULL, 0, NULL, 0 },
1211         };
1212
1213         while ((opt = getopt_long(argc, argv, short_options,
1214                                   long_options, NULL)) != -1) {
1215                 switch (opt) {
1216                 case 'k':
1217                         nid = optarg;
1218                         break;
1219                 case 'v':
1220                         detail = 1;
1221                         break;
1222                 case 'h':
1223                         print_help(peer_cmds, "peer", "show");
1224                         return 0;
1225                 default:
1226                         return 0;
1227                 }
1228         }
1229
1230         rc = lustre_lnet_show_peer(nid, detail, -1, &show_rc, &err_rc);
1231
1232         if (rc != LUSTRE_CFG_RC_NO_ERR)
1233                 cYAML_print_tree2file(stderr, err_rc);
1234         else if (show_rc)
1235                 cYAML_print_tree(show_rc);
1236
1237         cYAML_free_tree(err_rc);
1238         cYAML_free_tree(show_rc);
1239
1240         return rc;
1241 }
1242
1243 command_t list[] = {
1244         {"lnet", jt_lnet, 0, "lnet {configure | unconfigure} [--all]"},
1245         {"route", jt_route, 0, "route {add | del | show | help}"},
1246         {"net", jt_net, 0, "net {add | del | show | help}"},
1247         {"routing", jt_routing, 0, "routing {show | help}"},
1248         {"set", jt_set, 0, "set {tiny_buffers | small_buffers | large_buffers"
1249                            " | routing}"},
1250         {"import", jt_import, 0, "import {--add | --del | --show | "
1251                                  "--help} FILE.yaml"},
1252         {"export", jt_export, 0, "export {--help} FILE.yaml"},
1253         {"stats", jt_stats, 0, "stats {show | help}"},
1254         {"numa", jt_numa, 0, "numa {show | help}"},
1255         {"peer", jt_peers, 0, "peer {add | del | show | help}"},
1256         {"help", Parser_help, 0, "help"},
1257         {"exit", Parser_quit, 0, "quit"},
1258         {"quit", Parser_quit, 0, "quit"},
1259         { 0, 0, 0, NULL }
1260 };
1261
1262 int main(int argc, char **argv)
1263 {
1264         int rc = 0;
1265         struct cYAML *err_rc = NULL;
1266
1267         rc = lustre_lnet_config_lib_init();
1268         if (rc < 0) {
1269                 cYAML_build_error(-1, -1, "lnetctl", "startup",
1270                                   "cannot register LNet device", &err_rc);
1271                 cYAML_print_tree2file(stderr, err_rc);
1272                 return rc;
1273         }
1274
1275         Parser_init("lnetctl > ", list);
1276         if (argc > 1) {
1277                 rc = Parser_execarg(argc - 1, &argv[1], list);
1278                 goto errorout;
1279         }
1280
1281         Parser_commands();
1282
1283 errorout:
1284         return rc;
1285 }