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