Whamcloud - gitweb
LU-1154 clio: rename coo_attr_set to coo_attr_update
[fs/lustre-release.git] / lnet / utils / lnetconfig / liblnetconfig.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * LGPL HEADER END
20  *
21  * Copyright (c) 2014, Intel Corporation.
22  *
23  * Author:
24  *   Amir Shehata <amir.shehata@intel.com>
25  */
26
27 /*
28  * There are two APIs:
29  *  1. APIs that take the actual parameters expanded.  This is for other
30  *  entities that would like to link against the library and call the APIs
31  *  directly without having to form an intermediate representation.
32  *  2. APIs that take a YAML file and parses out the information there and
33  *  calls the APIs mentioned in 1
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <libcfs/libcfsutil.h>
39 #include <lnet/lnetctl.h>
40 #include <lnet/socklnd.h>
41 #include <lnet/lib-dlc.h>
42 #include <lnet/nidstr.h>
43 #include "liblnetconfig.h"
44 #include "cyaml.h"
45
46 #define CONFIG_CMD              "configure"
47 #define UNCONFIG_CMD            "unconfigure"
48 #define ADD_CMD                 "add"
49 #define DEL_CMD                 "del"
50 #define SHOW_CMD                "show"
51
52 int lustre_lnet_config_lib_init(void)
53 {
54         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH,
55                                 LNET_DEV_MAJOR, LNET_DEV_MINOR);
56 }
57
58 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
59                                  int seq_no, struct cYAML **err_rc)
60 {
61         struct libcfs_ioctl_data data;
62         unsigned int opc;
63         int rc;
64         char err_str[LNET_MAX_STR_LEN];
65
66         snprintf(err_str, sizeof(err_str), "\"Success\"");
67
68         LIBCFS_IOC_INIT(data);
69
70         /* Reverse logic is used here in order not to change
71          * the lctl utility */
72         data.ioc_flags = load_ni_from_mod ? 0 : 1;
73
74         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
75
76         rc = l_ioctl(LNET_DEV_ID, opc, &data);
77
78         if (rc != 0) {
79                 snprintf(err_str,
80                         sizeof(err_str),
81                         "\"LNet %s error: %s\"", (up) ? "configure" :
82                         "unconfigure", strerror(errno));
83                 rc = -errno;
84         }
85
86         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
87                           "lnet", err_str, err_rc);
88
89         return rc;
90 }
91
92 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
93                              int seq_no, struct cYAML **err_rc)
94 {
95         struct lnet_ioctl_config_data data;
96         lnet_nid_t gateway_nid;
97         int rc = LUSTRE_CFG_RC_NO_ERR;
98         __u32 net = LNET_NIDNET(LNET_NID_ANY);
99         char err_str[LNET_MAX_STR_LEN];
100
101         snprintf(err_str, sizeof(err_str), "\"Success\"");
102
103         if (nw == NULL || gw == NULL) {
104                 snprintf(err_str,
105                          sizeof(err_str),
106                          "\"missing mandatory parameter(s): '%s'\"",
107                          (nw == NULL && gw == NULL) ? "network, gateway" :
108                          (nw == NULL) ? "network" : "gateway");
109                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
110                 goto out;
111         }
112
113         net = libcfs_str2net(nw);
114         if (net == LNET_NIDNET(LNET_NID_ANY)) {
115                 snprintf(err_str,
116                          sizeof(err_str),
117                          "\"cannot parse net %s\"", nw);
118                 rc = LUSTRE_CFG_RC_BAD_PARAM;
119                 goto out;
120         }
121
122         if (LNET_NETTYP(net) == CIBLND    ||
123             LNET_NETTYP(net) == OPENIBLND ||
124             LNET_NETTYP(net) == IIBLND    ||
125             LNET_NETTYP(net) == VIBLND) {
126                 snprintf(err_str,
127                          sizeof(err_str),
128                          "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
129                 rc = LUSTRE_CFG_RC_BAD_PARAM;
130                 goto out;
131         }
132
133         gateway_nid = libcfs_str2nid(gw);
134         if (gateway_nid == LNET_NID_ANY) {
135                 snprintf(err_str,
136                         sizeof(err_str),
137                         "\"cannot parse gateway NID '%s'\"", gw);
138                 rc = LUSTRE_CFG_RC_BAD_PARAM;
139                 goto out;
140         }
141
142         if (hops == -1) {
143                 /* -1 indicates to use the default hop value */
144                 hops = 1;
145         } else if (hops < 1 || hops > 255) {
146                 snprintf(err_str,
147                         sizeof(err_str),
148                         "\"invalid hop count %d, must be between 0 and 256\"",
149                         hops);
150                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
151                 goto out;
152         }
153
154         if (prio == -1) {
155                 prio = 0;
156         } else if (prio < 0) {
157                 snprintf(err_str,
158                          sizeof(err_str),
159                         "\"invalid priority %d, must be greater than 0\"",
160                         prio);
161                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
162                 goto out;
163         }
164
165         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
166         data.cfg_net = net;
167         data.cfg_config_u.cfg_route.rtr_hop = hops;
168         data.cfg_config_u.cfg_route.rtr_priority = prio;
169         data.cfg_nid = gateway_nid;
170
171         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
172         if (rc != 0) {
173                 snprintf(err_str,
174                          sizeof(err_str),
175                          "\"cannot add route: %s\"", strerror(errno));
176                 rc = -errno;
177                 goto out;
178         }
179
180 out:
181         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
182
183         return rc;
184 }
185
186 int lustre_lnet_del_route(char *nw, char *gw,
187                           int seq_no, struct cYAML **err_rc)
188 {
189         struct lnet_ioctl_config_data data;
190         lnet_nid_t gateway_nid;
191         int rc = LUSTRE_CFG_RC_NO_ERR;
192         __u32 net = LNET_NIDNET(LNET_NID_ANY);
193         char err_str[LNET_MAX_STR_LEN];
194
195         snprintf(err_str, sizeof(err_str), "\"Success\"");
196
197         if (nw == NULL || gw == NULL) {
198                 snprintf(err_str,
199                          sizeof(err_str),
200                          "\"missing mandatory parameter(s): '%s'\"",
201                          (nw == NULL && gw == NULL) ? "network, gateway" :
202                          (nw == NULL) ? "network" : "gateway");
203                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
204                 goto out;
205         }
206
207         net = libcfs_str2net(nw);
208         if (net == LNET_NIDNET(LNET_NID_ANY)) {
209                 snprintf(err_str,
210                          sizeof(err_str),
211                          "\"cannot parse net '%s'\"", nw);
212                 rc = LUSTRE_CFG_RC_BAD_PARAM;
213                 goto out;
214         }
215
216         if (LNET_NETTYP(net) == CIBLND    ||
217             LNET_NETTYP(net) == OPENIBLND ||
218             LNET_NETTYP(net) == IIBLND    ||
219             LNET_NETTYP(net) == VIBLND) {
220                 snprintf(err_str,
221                          sizeof(err_str),
222                          "\"obselete LNet type '%s'\"", libcfs_lnd2str(net));
223                 rc = LUSTRE_CFG_RC_BAD_PARAM;
224                 goto out;
225         }
226
227         gateway_nid = libcfs_str2nid(gw);
228         if (gateway_nid == LNET_NID_ANY) {
229                 snprintf(err_str,
230                          sizeof(err_str),
231                          "\"cannot parse gateway NID '%s'\"", gw);
232                 rc = LUSTRE_CFG_RC_BAD_PARAM;
233                 goto out;
234         }
235
236         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
237         data.cfg_net = net;
238         data.cfg_nid = gateway_nid;
239
240         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
241         if (rc != 0) {
242                 snprintf(err_str,
243                          sizeof(err_str),
244                          "\"cannot delete route: %s\"", strerror(errno));
245                 rc = -errno;
246                 goto out;
247         }
248
249 out:
250         cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
251
252         return rc;
253 }
254
255 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
256                            int seq_no, struct cYAML **show_rc,
257                            struct cYAML **err_rc)
258 {
259         struct lnet_ioctl_config_data data;
260         lnet_nid_t gateway_nid;
261         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
262         __u32 net = LNET_NIDNET(LNET_NID_ANY);
263         int i;
264         struct cYAML *root = NULL, *route = NULL, *item = NULL;
265         struct cYAML *first_seq = NULL;
266         char err_str[LNET_MAX_STR_LEN];
267         bool exist = false;
268
269         snprintf(err_str, sizeof(err_str),
270                  "\"out of memory\"");
271
272         if (nw != NULL) {
273                 net = libcfs_str2net(nw);
274                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
275                         snprintf(err_str,
276                                  sizeof(err_str),
277                                  "\"cannot parse net '%s'\"", nw);
278                         rc = LUSTRE_CFG_RC_BAD_PARAM;
279                         goto out;
280                 }
281
282                 if (LNET_NETTYP(net) == CIBLND    ||
283                     LNET_NETTYP(net) == OPENIBLND ||
284                     LNET_NETTYP(net) == IIBLND    ||
285                     LNET_NETTYP(net) == VIBLND) {
286                         snprintf(err_str,
287                                  sizeof(err_str),
288                                  "\"obsolete LNet type '%s'\"",
289                                  libcfs_lnd2str(net));
290                         rc = LUSTRE_CFG_RC_BAD_PARAM;
291                         goto out;
292                 }
293         } else {
294                 /* show all routes without filtering on net */
295                 net = LNET_NIDNET(LNET_NID_ANY);
296         }
297
298         if (gw != NULL) {
299                 gateway_nid = libcfs_str2nid(gw);
300                 if (gateway_nid == LNET_NID_ANY) {
301                         snprintf(err_str,
302                                  sizeof(err_str),
303                                  "\"cannot parse gateway NID '%s'\"", gw);
304                         rc = LUSTRE_CFG_RC_BAD_PARAM;
305                         goto out;
306                 }
307         } else
308                 /* show all routes with out filtering on gateway */
309                 gateway_nid = LNET_NID_ANY;
310
311         if ((hops < 1 && hops != -1) || hops > 255) {
312                 snprintf(err_str,
313                          sizeof(err_str),
314                          "\"invalid hop count %d, must be between 0 and 256\"",
315                          hops);
316                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
317                 goto out;
318         }
319
320         /* create struct cYAML root object */
321         root = cYAML_create_object(NULL, NULL);
322         if (root == NULL)
323                 goto out;
324
325         route = cYAML_create_seq(root, "route");
326         if (route == NULL)
327                 goto out;
328
329         for (i = 0;; i++) {
330                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
331                 data.cfg_count = i;
332
333                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
334                 if (rc != 0)
335                         break;
336
337                 /* filter on provided data */
338                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
339                     net != data.cfg_net)
340                         continue;
341
342                 if (gateway_nid != LNET_NID_ANY &&
343                     gateway_nid != data.cfg_nid)
344                         continue;
345
346                 if (hops != -1 &&
347                     hops != data.cfg_config_u.cfg_route.rtr_hop)
348                         continue;
349
350                 if (prio != -1 &&
351                     prio != data.cfg_config_u.cfg_route.rtr_priority)
352                         continue;
353
354                 /* default rc to -1 incase we hit the goto */
355                 rc = -1;
356                 exist = true;
357
358                 item = cYAML_create_seq_item(route);
359                 if (item == NULL)
360                         goto out;
361
362                 if (first_seq == NULL)
363                         first_seq = item;
364
365                 if (cYAML_create_string(item, "net",
366                                         libcfs_net2str(data.cfg_net)) == NULL)
367                         goto out;
368
369                 if (cYAML_create_string(item, "gateway",
370                                         libcfs_nid2str(data.cfg_nid)) == NULL)
371                         goto out;
372
373                 if (detail) {
374                         if (cYAML_create_number(item, "hop",
375                                                 data.cfg_config_u.cfg_route.
376                                                         rtr_hop) ==
377                             NULL)
378                                 goto out;
379
380                         if (cYAML_create_number(item, "priority",
381                                                 data.cfg_config_u.
382                                                 cfg_route.rtr_priority) == NULL)
383                                 goto out;
384
385                         if (cYAML_create_string(item, "state",
386                                                 data.cfg_config_u.cfg_route.
387                                                         rtr_flags ?
388                                                 "up" : "down") == NULL)
389                                 goto out;
390                 }
391         }
392
393         /* print output iff show_rc is not provided */
394         if (show_rc == NULL)
395                 cYAML_print_tree(root);
396
397         if (errno != ENOENT) {
398                 snprintf(err_str,
399                          sizeof(err_str),
400                          "\"cannot get routes: %s\"",
401                          strerror(errno));
402                 rc = -errno;
403                 goto out;
404         } else
405                 rc = LUSTRE_CFG_RC_NO_ERR;
406
407         snprintf(err_str, sizeof(err_str), "\"success\"");
408 out:
409         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
410                 cYAML_free_tree(root);
411         } else if (show_rc != NULL && *show_rc != NULL) {
412                 struct cYAML *show_node;
413                 /* find the route node, if one doesn't exist then
414                  * insert one.  Otherwise add to the one there
415                  */
416                 show_node = cYAML_get_object_item(*show_rc, "route");
417                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
418                         cYAML_insert_child(show_node, first_seq);
419                         free(route);
420                         free(root);
421                 } else if (show_node == NULL) {
422                         cYAML_insert_sibling((*show_rc)->cy_child,
423                                                 route);
424                         free(root);
425                 } else {
426                         cYAML_free_tree(root);
427                 }
428         } else {
429                 *show_rc = root;
430         }
431
432         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
433
434         return rc;
435 }
436
437 int lustre_lnet_config_net(char *net, char *intf, char *ip2net,
438                            int peer_to, int peer_cr, int peer_buf_cr,
439                            int credits, char *smp, int seq_no,
440                            struct cYAML **err_rc)
441 {
442         struct lnet_ioctl_config_data data;
443         char buf[LNET_MAX_STR_LEN];
444         int rc = LUSTRE_CFG_RC_NO_ERR, num_of_nets = 0;
445         char err_str[LNET_MAX_STR_LEN];
446
447         snprintf(err_str, sizeof(err_str), "\"success\"");
448
449         if (ip2net == NULL && (intf == NULL || net == NULL)) {
450                 snprintf(err_str,
451                          sizeof(err_str),
452                          "\"mandatory parameter '%s' not specified."
453                          " Optionally specify ip2net parameter\"",
454                          (intf == NULL && net == NULL) ? "net, if" :
455                          (intf == NULL) ? "if" : "net");
456                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
457                 goto out;
458         }
459
460         if (peer_to != -1 && peer_to <= 0) {
461                 snprintf(err_str,
462                          sizeof(err_str),
463                          "\"peer timeout %d, must be greater than 0\"",
464                          peer_to);
465                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
466                 goto out;
467         }
468
469         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
470                 snprintf(err_str,
471                          sizeof(err_str),
472                          "\"ip2net string too long %d\"",
473                                 (int)strlen(ip2net));
474                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
475                 goto out;
476         }
477
478         if (ip2net == NULL)
479                 snprintf(buf, sizeof(buf) - 1, "%s(%s)%s",
480                         net, intf,
481                         (smp) ? smp : "");
482
483         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
484         strncpy(data.cfg_config_u.cfg_net.net_intf,
485                 (ip2net != NULL) ? ip2net : buf, sizeof(buf));
486         data.cfg_config_u.cfg_net.net_peer_timeout = peer_to;
487         data.cfg_config_u.cfg_net.net_peer_tx_credits = peer_cr;
488         data.cfg_config_u.cfg_net.net_peer_rtr_credits = peer_buf_cr;
489         data.cfg_config_u.cfg_net.net_max_tx_credits = credits;
490
491         num_of_nets = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_NET, &data);
492         if (num_of_nets < 0) {
493                 snprintf(err_str,
494                          sizeof(err_str),
495                          "\"cannot add network: %s\"", strerror(errno));
496                 rc = -errno;
497         }
498
499 out:
500         cYAML_build_error((num_of_nets > 0) ? num_of_nets : rc,
501                          seq_no, ADD_CMD, "net", err_str, err_rc);
502
503         return rc;
504 }
505
506 int lustre_lnet_del_net(char *nw, int seq_no, struct cYAML **err_rc)
507 {
508         struct lnet_ioctl_config_data data;
509         __u32 net = LNET_NIDNET(LNET_NID_ANY);
510         int rc = LUSTRE_CFG_RC_NO_ERR;
511         char err_str[LNET_MAX_STR_LEN];
512
513         snprintf(err_str, sizeof(err_str), "\"success\"");
514
515         if (nw == NULL) {
516                 snprintf(err_str,
517                          sizeof(err_str),
518                          "\"missing mandatory parameter\"");
519                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
520                 goto out;
521         }
522
523         net = libcfs_str2net(nw);
524         if (net == LNET_NIDNET(LNET_NID_ANY)) {
525                 snprintf(err_str,
526                          sizeof(err_str),
527                          "\"cannot parse net '%s'\"", nw);
528                 rc = LUSTRE_CFG_RC_BAD_PARAM;
529                 goto out;
530         }
531
532         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
533         data.cfg_net = net;
534
535         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_NET, &data);
536         if (rc != 0) {
537                 snprintf(err_str,
538                          sizeof(err_str),
539                          "\"cannot delete network: %s\"", strerror(errno));
540                 rc = -errno;
541                 goto out;
542         }
543
544 out:
545         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
546
547         return rc;
548 }
549
550 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
551                          struct cYAML **show_rc, struct cYAML **err_rc)
552 {
553         char *buf;
554         struct lnet_ioctl_config_data *data;
555         struct lnet_ioctl_net_config *net_config;
556         __u32 net = LNET_NIDNET(LNET_NID_ANY);
557         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
558         struct cYAML *root = NULL, *tunables = NULL,
559                 *net_node = NULL, *interfaces = NULL,
560                 *item = NULL, *first_seq = NULL;
561         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
562         char str_buf[str_buf_len];
563         char *pos;
564         char err_str[LNET_MAX_STR_LEN];
565         bool exist = false;
566
567         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
568
569         buf = calloc(1, sizeof(*data) + sizeof(*net_config));
570         if (buf == NULL)
571                 goto out;
572
573         data = (struct lnet_ioctl_config_data *)buf;
574
575         if (nw != NULL) {
576                 net = libcfs_str2net(nw);
577                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
578                         snprintf(err_str,
579                                  sizeof(err_str),
580                                  "\"cannot parse net '%s'\"", nw);
581                         rc = LUSTRE_CFG_RC_BAD_PARAM;
582                         goto out;
583                 }
584         }
585
586         root = cYAML_create_object(NULL, NULL);
587         if (root == NULL)
588                 goto out;
589
590         net_node = cYAML_create_seq(root, "net");
591         if (net_node == NULL)
592                 goto out;
593
594         for (i = 0;; i++) {
595                 pos = str_buf;
596
597                 memset(buf, 0, sizeof(*data) + sizeof(*net_config));
598
599                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
600                 /*
601                  * set the ioc_len to the proper value since INIT assumes
602                  * size of data
603                  */
604                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
605                   sizeof(struct lnet_ioctl_net_config);
606                 data->cfg_count = i;
607
608                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NET, data);
609                 if (rc != 0)
610                         break;
611
612                 /* filter on provided data */
613                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
614                     net != LNET_NIDNET(data->cfg_nid))
615                         continue;
616
617                 /* default rc to -1 in case we hit the goto */
618                 rc = -1;
619                 exist = true;
620
621                 net_config = (struct lnet_ioctl_net_config *)data->cfg_bulk;
622
623                 /* create the tree to be printed. */
624                 item = cYAML_create_seq_item(net_node);
625                 if (item == NULL)
626                         goto out;
627
628                 if (first_seq == NULL)
629                         first_seq = item;
630
631                 if (cYAML_create_string(item,
632                                         "net",
633                                         libcfs_net2str(
634                                                 LNET_NIDNET(data->cfg_nid)))
635                     == NULL)
636                         goto out;
637
638                 if (cYAML_create_string(item, "nid",
639                                         libcfs_nid2str(data->cfg_nid)) == NULL)
640                         goto out;
641
642                 if (cYAML_create_string(item,
643                                         "status",
644                                         (net_config->ni_status ==
645                                           LNET_NI_STATUS_UP) ?
646                                             "up" : "down") == NULL)
647                         goto out;
648
649                 /* don't add interfaces unless there is at least one
650                  * interface */
651                 if (strlen(net_config->ni_interfaces[0]) > 0) {
652                         interfaces = cYAML_create_object(item, "interfaces");
653                         if (interfaces == NULL)
654                                 goto out;
655
656                         for (j = 0; j < LNET_MAX_INTERFACES; j++) {
657                                 if (strlen(net_config->ni_interfaces[j]) > 0) {
658                                         snprintf(str_buf,
659                                                  sizeof(str_buf), "%d", j);
660                                         if (cYAML_create_string(interfaces,
661                                                 str_buf,
662                                                 net_config->ni_interfaces[j]) ==
663                                             NULL)
664                                                 goto out;
665                                 }
666                         }
667                 }
668
669                 if (detail) {
670                         char *limit;
671
672                         tunables = cYAML_create_object(item, "tunables");
673                         if (tunables == NULL)
674                                 goto out;
675
676                         if (cYAML_create_number(tunables, "peer_timeout",
677                                                 data->cfg_config_u.cfg_net.
678                                                  net_peer_timeout) == NULL)
679                                 goto out;
680
681                         if (cYAML_create_number(tunables, "peer_credits",
682                                                 data->cfg_config_u.cfg_net.
683                                                   net_peer_tx_credits) == NULL)
684                                 goto out;
685
686                         if (cYAML_create_number(tunables,
687                                                 "peer_buffer_credits",
688                                                 data->cfg_config_u.cfg_net.
689                                                   net_peer_rtr_credits) == NULL)
690                                 goto out;
691
692                         if (cYAML_create_number(tunables, "credits",
693                                                 data->cfg_config_u.cfg_net.
694                                                   net_max_tx_credits) == NULL)
695                                 goto out;
696
697                         /* out put the CPTs in the format: "[x,x,x,...]" */
698                         limit = str_buf + str_buf_len - 3;
699                         pos += snprintf(pos, limit - pos, "\"[");
700                         for (j = 0 ; data->cfg_ncpts > 1 &&
701                                 j < data->cfg_ncpts &&
702                                 pos < limit; j++) {
703                                 pos += snprintf(pos, limit - pos,
704                                                 "%d", net_config->ni_cpts[j]);
705                                 if ((j + 1) < data->cfg_ncpts)
706                                         pos += snprintf(pos, limit - pos, ",");
707                         }
708                         pos += snprintf(pos, 3, "]\"");
709
710                         if (data->cfg_ncpts > 1 &&
711                             cYAML_create_string(tunables, "CPT",
712                                                 str_buf) == NULL)
713                                 goto out;
714                 }
715         }
716
717         /* Print out the net information only if show_rc is not provided */
718         if (show_rc == NULL)
719                 cYAML_print_tree(root);
720
721         if (errno != ENOENT) {
722                 snprintf(err_str,
723                          sizeof(err_str),
724                          "\"cannot get networks: %s\"",
725                          strerror(errno));
726                 rc = -errno;
727                 goto out;
728         } else
729                 rc = LUSTRE_CFG_RC_NO_ERR;
730
731         snprintf(err_str, sizeof(err_str), "\"success\"");
732 out:
733         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
734                 cYAML_free_tree(root);
735         } else if (show_rc != NULL && *show_rc != NULL) {
736                 struct cYAML *show_node;
737                 /* find the net node, if one doesn't exist
738                  * then insert one.  Otherwise add to the one there
739                  */
740                 show_node = cYAML_get_object_item(*show_rc, "net");
741                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
742                         cYAML_insert_child(show_node, first_seq);
743                         free(net_node);
744                         free(root);
745                 } else if (show_node == NULL) {
746                         cYAML_insert_sibling((*show_rc)->cy_child,
747                                                 net_node);
748                         free(root);
749                 } else {
750                         cYAML_free_tree(root);
751                 }
752         } else {
753                 *show_rc = root;
754         }
755
756         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
757
758         return rc;
759 }
760
761 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
762 {
763         struct lnet_ioctl_config_data data;
764         int rc = LUSTRE_CFG_RC_NO_ERR;
765         char err_str[LNET_MAX_STR_LEN];
766
767         snprintf(err_str, sizeof(err_str), "\"success\"");
768
769         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
770         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
771
772         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
773         if (rc != 0) {
774                 snprintf(err_str,
775                          sizeof(err_str),
776                          "\"cannot %s routing %s\"",
777                          (enable) ? "enable" : "disable", strerror(errno));
778                 rc = -errno;
779                 goto out;
780         }
781
782 out:
783         cYAML_build_error(rc, seq_no,
784                          (enable) ? ADD_CMD : DEL_CMD,
785                          "routing", err_str, err_rc);
786
787         return rc;
788 }
789
790 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
791                                struct cYAML **err_rc)
792 {
793         struct lnet_ioctl_config_data data;
794         int rc = LUSTRE_CFG_RC_NO_ERR;
795         char err_str[LNET_MAX_STR_LEN];
796
797         snprintf(err_str, sizeof(err_str), "\"success\"");
798
799         /* -1 indicates to ignore changes to this field */
800         if (tiny < -1 || small < -1 || large < -1) {
801                 snprintf(err_str,
802                          sizeof(err_str),
803                          "\"tiny, small and large must be >= 0\"");
804                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
805                 goto out;
806         }
807
808         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
809         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
810         data.cfg_config_u.cfg_buffers.buf_small = small;
811         data.cfg_config_u.cfg_buffers.buf_large = large;
812
813         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
814         if (rc != 0) {
815                 snprintf(err_str,
816                          sizeof(err_str),
817                          "\"cannot configure buffers: %s\"", strerror(errno));
818                 rc = -errno;
819                 goto out;
820         }
821
822 out:
823         cYAML_build_error(rc, seq_no, DEL_CMD, "buf", err_str, err_rc);
824
825         return rc;
826 }
827
828 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
829                              struct cYAML **err_rc)
830 {
831         struct lnet_ioctl_config_data *data;
832         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
833         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
834         char *buf;
835         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
836         struct cYAML *root = NULL, *pools_node = NULL,
837                      *type_node = NULL, *item = NULL, *cpt = NULL,
838                      *first_seq = NULL;
839         int i, j;
840         char err_str[LNET_MAX_STR_LEN];
841         char node_name[LNET_MAX_STR_LEN];
842         bool exist = false;
843
844         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
845
846         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
847         if (buf == NULL)
848                 goto out;
849
850         data = (struct lnet_ioctl_config_data *)buf;
851
852         root = cYAML_create_object(NULL, NULL);
853         if (root == NULL)
854                 goto out;
855
856         pools_node = cYAML_create_seq(root, "routing");
857         if (pools_node == NULL)
858                 goto out;
859
860         for (i = 0;; i++) {
861                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
862                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
863                                         sizeof(struct lnet_ioctl_pool_cfg);
864                 data->cfg_count = i;
865
866                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
867                 if (rc != 0)
868                         break;
869
870                 exist = true;
871
872                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
873
874                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
875                 item = cYAML_create_seq_item(pools_node);
876                 if (item == NULL)
877                         goto out;
878
879                 if (first_seq == NULL)
880                         first_seq = item;
881
882                 cpt = cYAML_create_object(item, node_name);
883                 if (cpt == NULL)
884                         goto out;
885
886                 /* create the tree  and print */
887                 for (j = 0; j < LNET_NRBPOOLS; j++) {
888                         type_node = cYAML_create_object(cpt, pools[j]);
889                         if (type_node == NULL)
890                                 goto out;
891                         if (cYAML_create_number(type_node, "npages",
892                                                 pool_cfg->pl_pools[j].pl_npages)
893                             == NULL)
894                                 goto out;
895                         if (cYAML_create_number(type_node, "nbuffers",
896                                                 pool_cfg->pl_pools[j].
897                                                   pl_nbuffers) == NULL)
898                                 goto out;
899                         if (cYAML_create_number(type_node, "credits",
900                                                 pool_cfg->pl_pools[j].
901                                                    pl_credits) == NULL)
902                                 goto out;
903                         if (cYAML_create_number(type_node, "mincredits",
904                                                 pool_cfg->pl_pools[j].
905                                                    pl_mincredits) == NULL)
906                                 goto out;
907                 }
908         }
909
910         if (pool_cfg != NULL) {
911                 item = cYAML_create_seq_item(pools_node);
912                 if (item == NULL)
913                         goto out;
914
915                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
916                     NULL)
917                         goto out;
918         }
919
920         if (show_rc == NULL)
921                 cYAML_print_tree(root);
922
923         if (errno != ENOENT) {
924                 snprintf(err_str,
925                          sizeof(err_str),
926                          "\"cannot get routing information: %s\"",
927                          strerror(errno));
928                 rc = -errno;
929                 goto out;
930         } else
931                 rc = LUSTRE_CFG_RC_NO_ERR;
932
933         snprintf(err_str, sizeof(err_str), "\"success\"");
934         rc = LUSTRE_CFG_RC_NO_ERR;
935
936 out:
937         free(buf);
938         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
939                 cYAML_free_tree(root);
940         } else if (show_rc != NULL && *show_rc != NULL) {
941                 struct cYAML *show_node;
942                 /* find the routing node, if one doesn't exist then
943                  * insert one.  Otherwise add to the one there
944                  */
945                 show_node = cYAML_get_object_item(*show_rc, "routing");
946                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
947                         cYAML_insert_child(show_node, first_seq);
948                         free(pools_node);
949                         free(root);
950                 } else if (show_node == NULL) {
951                         cYAML_insert_sibling((*show_rc)->cy_child,
952                                                 pools_node);
953                         free(root);
954                 } else {
955                         cYAML_free_tree(root);
956                 }
957         } else {
958                 *show_rc = root;
959         }
960
961         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
962
963         return rc;
964 }
965
966 int lustre_lnet_show_peer_credits(int seq_no, struct cYAML **show_rc,
967                                   struct cYAML **err_rc)
968 {
969         struct lnet_ioctl_peer peer_info;
970         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
971         struct cYAML *root = NULL, *peer = NULL, *first_seq = NULL,
972                      *peer_root = NULL;
973         char err_str[LNET_MAX_STR_LEN];
974         bool ncpt_set = false;
975
976         snprintf(err_str, sizeof(err_str),
977                  "\"out of memory\"");
978
979         /* create struct cYAML root object */
980         root = cYAML_create_object(NULL, NULL);
981         if (root == NULL)
982                 goto out;
983
984         peer_root = cYAML_create_seq(root, "peer");
985         if (peer_root == NULL)
986                 goto out;
987
988         do {
989                 for (i = 0;; i++) {
990                         LIBCFS_IOC_INIT_V2(peer_info, pr_hdr);
991                         peer_info.pr_count = i;
992                         peer_info.pr_lnd_u.pr_peer_credits.cr_ncpt = j;
993                         rc = l_ioctl(LNET_DEV_ID,
994                                      IOC_LIBCFS_GET_PEER_INFO, &peer_info);
995                         if (rc != 0)
996                                 break;
997
998                         if (ncpt_set != 0) {
999                                 ncpt = peer_info.pr_lnd_u.pr_peer_credits.
1000                                         cr_ncpt;
1001                                 ncpt_set = true;
1002                         }
1003
1004                         peer = cYAML_create_seq_item(peer_root);
1005                         if (peer == NULL)
1006                                 goto out;
1007
1008                         if (first_seq == NULL)
1009                                 first_seq = peer;
1010
1011                         if (cYAML_create_string(peer, "nid",
1012                                                 libcfs_nid2str
1013                                                  (peer_info.pr_nid)) == NULL)
1014                                 goto out;
1015
1016                         if (cYAML_create_string(peer, "state",
1017                                                 peer_info.pr_lnd_u.
1018                                                   pr_peer_credits.
1019                                                         cr_aliveness) ==
1020                             NULL)
1021                                 goto out;
1022
1023                         if (cYAML_create_number(peer, "refcount",
1024                                                 peer_info.pr_lnd_u.
1025                                                   pr_peer_credits.
1026                                                         cr_refcount) == NULL)
1027                                 goto out;
1028
1029                         if (cYAML_create_number(peer, "max_ni_tx_credits",
1030                                                 peer_info.pr_lnd_u.
1031                                                   pr_peer_credits.
1032                                                     cr_ni_peer_tx_credits)
1033                             == NULL)
1034                                 goto out;
1035
1036                         if (cYAML_create_number(peer, "available_tx_credits",
1037                                                 peer_info.pr_lnd_u.
1038                                                   pr_peer_credits.
1039                                                     cr_peer_tx_credits)
1040                             == NULL)
1041                                 goto out;
1042
1043                         if (cYAML_create_number(peer, "available_rtr_credits",
1044                                                 peer_info.pr_lnd_u.
1045                                                   pr_peer_credits.
1046                                                     cr_peer_rtr_credits)
1047                             == NULL)
1048                                 goto out;
1049
1050                         if (cYAML_create_number(peer, "min_rtr_credits",
1051                                                 peer_info.pr_lnd_u.
1052                                                   pr_peer_credits.
1053                                                     cr_peer_min_rtr_credits)
1054                             == NULL)
1055                                 goto out;
1056
1057                         if (cYAML_create_number(peer, "tx_q_num_of_buf",
1058                                                 peer_info.pr_lnd_u.
1059                                                   pr_peer_credits.
1060                                                     cr_peer_tx_qnob)
1061                             == NULL)
1062                                 goto out;
1063                 }
1064
1065                 if (errno != ENOENT) {
1066                         snprintf(err_str,
1067                                 sizeof(err_str),
1068                                 "\"cannot get peer information: %s\"",
1069                                 strerror(errno));
1070                         rc = -errno;
1071                         goto out;
1072                 }
1073
1074                 j++;
1075         } while (j < ncpt);
1076
1077         /* print output iff show_rc is not provided */
1078         if (show_rc == NULL)
1079                 cYAML_print_tree(root);
1080
1081         snprintf(err_str, sizeof(err_str), "\"success\"");
1082         rc = LUSTRE_CFG_RC_NO_ERR;
1083
1084 out:
1085         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1086                 cYAML_free_tree(root);
1087         } else if (show_rc != NULL && *show_rc != NULL) {
1088                 struct cYAML *show_node;
1089                 /* find the peer node, if one doesn't exist then
1090                  * insert one.  Otherwise add to the one there
1091                  */
1092                 show_node = cYAML_get_object_item(*show_rc,
1093                                                   "peer_credits");
1094                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1095                         cYAML_insert_child(show_node, first_seq);
1096                         free(peer_root);
1097                         free(root);
1098                 } else if (show_node == NULL) {
1099                         cYAML_insert_sibling((*show_rc)->cy_child,
1100                                              peer_root);
1101                         free(root);
1102                 } else {
1103                         cYAML_free_tree(root);
1104                 }
1105         } else {
1106                 *show_rc = root;
1107         }
1108
1109         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer_credits", err_str,
1110                           err_rc);
1111
1112         return rc;
1113 }
1114
1115 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
1116                            struct cYAML **err_rc)
1117 {
1118         struct lnet_ioctl_lnet_stats data;
1119         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1120         char err_str[LNET_MAX_STR_LEN];
1121         struct cYAML *root = NULL, *stats = NULL;
1122
1123         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1124
1125         LIBCFS_IOC_INIT_V2(data, st_hdr);
1126
1127         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
1128         if (rc != 0) {
1129                 snprintf(err_str,
1130                          sizeof(err_str),
1131                          "\"cannot get lnet statistics: %s\"",
1132                          strerror(errno));
1133                 rc = -errno;
1134                 goto out;
1135         }
1136
1137         root = cYAML_create_object(NULL, NULL);
1138         if (root == NULL)
1139                 goto out;
1140
1141         stats = cYAML_create_object(root, "statistics");
1142         if (stats == NULL)
1143                 goto out;
1144
1145         if (cYAML_create_number(stats, "msgs_alloc",
1146                                 data.st_cntrs.msgs_alloc) == NULL)
1147                 goto out;
1148
1149         if (cYAML_create_number(stats, "msgs_max",
1150                                 data.st_cntrs.msgs_max) == NULL)
1151                 goto out;
1152
1153         if (cYAML_create_number(stats, "errors",
1154                                 data.st_cntrs.errors) == NULL)
1155                 goto out;
1156
1157         if (cYAML_create_number(stats, "send_count",
1158                                 data.st_cntrs.send_count) == NULL)
1159                 goto out;
1160
1161         if (cYAML_create_number(stats, "recv_count",
1162                                 data.st_cntrs.recv_count) == NULL)
1163                 goto out;
1164
1165         if (cYAML_create_number(stats, "route_count",
1166                                 data.st_cntrs.route_count) == NULL)
1167                 goto out;
1168
1169         if (cYAML_create_number(stats, "drop_count",
1170                                 data.st_cntrs.drop_count) == NULL)
1171                 goto out;
1172
1173         if (cYAML_create_number(stats, "send_length",
1174                                 data.st_cntrs.send_length) == NULL)
1175                 goto out;
1176
1177         if (cYAML_create_number(stats, "recv_length",
1178                                 data.st_cntrs.recv_length) == NULL)
1179                 goto out;
1180
1181         if (cYAML_create_number(stats, "route_length",
1182                                 data.st_cntrs.route_length) == NULL)
1183                 goto out;
1184
1185         if (cYAML_create_number(stats, "drop_length",
1186                                 data.st_cntrs.drop_length) == NULL)
1187                 goto out;
1188
1189         if (show_rc == NULL)
1190                 cYAML_print_tree(root);
1191
1192         snprintf(err_str, sizeof(err_str), "\"success\"");
1193 out:
1194         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1195                 cYAML_free_tree(root);
1196         } else if (show_rc != NULL && *show_rc != NULL) {
1197                 cYAML_insert_sibling((*show_rc)->cy_child,
1198                                         root->cy_child);
1199                 free(root);
1200         } else {
1201                 *show_rc = root;
1202         }
1203
1204         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
1205
1206         return rc;
1207 }
1208
1209 typedef int (*cmd_handler_t)(struct cYAML *tree,
1210                              struct cYAML **show_rc,
1211                              struct cYAML **err_rc);
1212
1213 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
1214                                     struct cYAML **err_rc)
1215 {
1216         struct cYAML *net, *gw, *hop, *prio, *seq_no;
1217
1218         net = cYAML_get_object_item(tree, "net");
1219         gw = cYAML_get_object_item(tree, "gateway");
1220         hop = cYAML_get_object_item(tree, "hop");
1221         prio = cYAML_get_object_item(tree, "priority");
1222         seq_no = cYAML_get_object_item(tree, "seq_no");
1223
1224         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
1225                                         (gw) ? gw->cy_valuestring : NULL,
1226                                         (hop) ? hop->cy_valueint : -1,
1227                                         (prio) ? prio->cy_valueint : -1,
1228                                         (seq_no) ? seq_no->cy_valueint : -1,
1229                                         err_rc);
1230 }
1231
1232 static int handle_yaml_config_net(struct cYAML *tree, struct cYAML **show_rc,
1233                                   struct cYAML **err_rc)
1234 {
1235         struct cYAML *net, *intf, *tunables, *seq_no,
1236               *peer_to = NULL, *peer_buf_cr = NULL, *peer_cr = NULL,
1237               *credits = NULL, *ip2net = NULL, *smp = NULL, *child;
1238         char devs[LNET_MAX_STR_LEN];
1239         char *loc = devs;
1240         int size = LNET_MAX_STR_LEN;
1241         int num;
1242         bool intf_found = false;
1243
1244         ip2net = cYAML_get_object_item(tree, "ip2net");
1245         net = cYAML_get_object_item(tree, "net");
1246         intf = cYAML_get_object_item(tree, "interfaces");
1247         if (intf != NULL) {
1248                 /* grab all the interfaces */
1249                 child = intf->cy_child;
1250                 while (child != NULL && size > 0) {
1251                         if (loc > devs)
1252                                 num  = snprintf(loc, size, ",%s",
1253                                                 child->cy_valuestring);
1254                         else
1255                                 num = snprintf(loc, size, "%s",
1256                                                child->cy_valuestring);
1257                         size -= num;
1258                         loc += num;
1259                         intf_found = true;
1260                         child = child->cy_next;
1261                 }
1262         }
1263
1264         tunables = cYAML_get_object_item(tree, "tunables");
1265         if (tunables != NULL) {
1266                 peer_to = cYAML_get_object_item(tunables, "peer_timeout");
1267                 peer_cr = cYAML_get_object_item(tunables, "peer_credits");
1268                 peer_buf_cr = cYAML_get_object_item(tunables,
1269                                                     "peer_buffer_credits");
1270                 credits = cYAML_get_object_item(tunables, "credits");
1271                 smp = cYAML_get_object_item(tunables, "CPT");
1272         }
1273         seq_no = cYAML_get_object_item(tree, "seq_no");
1274
1275         return lustre_lnet_config_net((net) ? net->cy_valuestring : NULL,
1276                                       (intf_found) ? devs : NULL,
1277                                       (ip2net) ? ip2net->cy_valuestring : NULL,
1278                                       (peer_to) ? peer_to->cy_valueint : -1,
1279                                       (peer_cr) ? peer_cr->cy_valueint : -1,
1280                                       (peer_buf_cr) ?
1281                                         peer_buf_cr->cy_valueint : -1,
1282                                       (credits) ? credits->cy_valueint : -1,
1283                                       (smp) ? smp->cy_valuestring : NULL,
1284                                       (seq_no) ? seq_no->cy_valueint : -1,
1285                                       err_rc);
1286 }
1287
1288 static int handle_yaml_config_buffers(struct cYAML *tree,
1289                                       struct cYAML **show_rc,
1290                                       struct cYAML **err_rc)
1291 {
1292         int rc;
1293         struct cYAML *tiny, *small, *large, *seq_no;
1294
1295         tiny = cYAML_get_object_item(tree, "tiny");
1296         small = cYAML_get_object_item(tree, "small");
1297         large = cYAML_get_object_item(tree, "large");
1298         seq_no = cYAML_get_object_item(tree, "seq_no");
1299
1300         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
1301                                         (small) ? small->cy_valueint : -1,
1302                                         (large) ? large->cy_valueint : -1,
1303                                         (seq_no) ? seq_no->cy_valueint : -1,
1304                                         err_rc);
1305
1306         return rc;
1307 }
1308
1309 static int handle_yaml_config_routing(struct cYAML *tree,
1310                                       struct cYAML **show_rc,
1311                                       struct cYAML **err_rc)
1312 {
1313         int rc = LUSTRE_CFG_RC_NO_ERR;
1314         struct cYAML *seq_no, *enable;
1315
1316         seq_no = cYAML_get_object_item(tree, "seq_no");
1317         enable = cYAML_get_object_item(tree, "enable");
1318
1319         if (enable) {
1320                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
1321                                                 (seq_no) ?
1322                                                     seq_no->cy_valueint : -1,
1323                                                 err_rc);
1324         }
1325
1326         return rc;
1327 }
1328
1329 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
1330                                  struct cYAML **err_rc)
1331 {
1332         struct cYAML *net;
1333         struct cYAML *gw;
1334         struct cYAML *seq_no;
1335
1336         net = cYAML_get_object_item(tree, "net");
1337         gw = cYAML_get_object_item(tree, "gateway");
1338         seq_no = cYAML_get_object_item(tree, "seq_no");
1339
1340         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
1341                                      (gw) ? gw->cy_valuestring : NULL,
1342                                      (seq_no) ? seq_no->cy_valueint : -1,
1343                                      err_rc);
1344 }
1345
1346 static int handle_yaml_del_net(struct cYAML *tree, struct cYAML **show_rc,
1347                                struct cYAML **err_rc)
1348 {
1349         struct cYAML *net, *seq_no;
1350
1351         net = cYAML_get_object_item(tree, "net");
1352         seq_no = cYAML_get_object_item(tree, "seq_no");
1353
1354         return lustre_lnet_del_net((net) ? net->cy_valuestring : NULL,
1355                                    (seq_no) ? seq_no->cy_valueint : -1,
1356                                    err_rc);
1357 }
1358
1359 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
1360                                    struct cYAML **err_rc)
1361 {
1362         struct cYAML *seq_no;
1363
1364         seq_no = cYAML_get_object_item(tree, "seq_no");
1365
1366         return lustre_lnet_enable_routing(0, (seq_no) ?
1367                                                 seq_no->cy_valueint : -1,
1368                                         err_rc);
1369 }
1370
1371 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
1372                                   struct cYAML **err_rc)
1373 {
1374         struct cYAML *net;
1375         struct cYAML *gw;
1376         struct cYAML *hop;
1377         struct cYAML *prio;
1378         struct cYAML *detail;
1379         struct cYAML *seq_no;
1380
1381         net = cYAML_get_object_item(tree, "net");
1382         gw = cYAML_get_object_item(tree, "gateway");
1383         hop = cYAML_get_object_item(tree, "hop");
1384         prio = cYAML_get_object_item(tree, "priority");
1385         detail = cYAML_get_object_item(tree, "detail");
1386         seq_no = cYAML_get_object_item(tree, "seq_no");
1387
1388         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
1389                                       (gw) ? gw->cy_valuestring : NULL,
1390                                       (hop) ? hop->cy_valueint : -1,
1391                                       (prio) ? prio->cy_valueint : -1,
1392                                       (detail) ? detail->cy_valueint : 0,
1393                                       (seq_no) ? seq_no->cy_valueint : -1,
1394                                       show_rc,
1395                                       err_rc);
1396 }
1397
1398 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
1399                                 struct cYAML **err_rc)
1400 {
1401         struct cYAML *net, *detail, *seq_no;
1402
1403         net = cYAML_get_object_item(tree, "net");
1404         detail = cYAML_get_object_item(tree, "detail");
1405         seq_no = cYAML_get_object_item(tree, "seq_no");
1406
1407         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
1408                                     (detail) ? detail->cy_valueint : 0,
1409                                     (seq_no) ? seq_no->cy_valueint : -1,
1410                                     show_rc,
1411                                     err_rc);
1412 }
1413
1414 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
1415                                     struct cYAML **err_rc)
1416 {
1417         struct cYAML *seq_no;
1418
1419         seq_no = cYAML_get_object_item(tree, "seq_no");
1420
1421         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
1422                                         show_rc, err_rc);
1423 }
1424
1425 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
1426                                     struct cYAML **err_rc)
1427 {
1428         struct cYAML *seq_no;
1429
1430         seq_no = cYAML_get_object_item(tree, "seq_no");
1431
1432         return lustre_lnet_show_peer_credits((seq_no) ?
1433                                                 seq_no->cy_valueint : -1,
1434                                              show_rc, err_rc);
1435 }
1436
1437 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
1438                                   struct cYAML **err_rc)
1439 {
1440         struct cYAML *seq_no;
1441
1442         seq_no = cYAML_get_object_item(tree, "seq_no");
1443
1444         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
1445                                       show_rc, err_rc);
1446 }
1447
1448 struct lookup_cmd_hdlr_tbl {
1449         char *name;
1450         cmd_handler_t cb;
1451 };
1452
1453 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
1454         {"route", handle_yaml_config_route},
1455         {"net", handle_yaml_config_net},
1456         {"routing", handle_yaml_config_routing},
1457         {"buffers", handle_yaml_config_buffers},
1458         {NULL, NULL}
1459 };
1460
1461 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
1462         {"route", handle_yaml_del_route},
1463         {"net", handle_yaml_del_net},
1464         {"routing", handle_yaml_del_routing},
1465         {NULL, NULL}
1466 };
1467
1468 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
1469         {"route", handle_yaml_show_route},
1470         {"net", handle_yaml_show_net},
1471         {"buffers", handle_yaml_show_routing},
1472         {"routing", handle_yaml_show_routing},
1473         {"credits", handle_yaml_show_credits},
1474         {"statistics", handle_yaml_show_stats},
1475         {NULL, NULL}
1476 };
1477
1478 static cmd_handler_t lookup_fn(char *key,
1479                                struct lookup_cmd_hdlr_tbl *tbl)
1480 {
1481         int i;
1482         if (key == NULL)
1483                 return NULL;
1484
1485         for (i = 0; tbl[i].name != NULL; i++) {
1486                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
1487                         return tbl[i].cb;
1488         }
1489
1490         return NULL;
1491 }
1492
1493 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
1494                                  struct cYAML **show_rc, struct cYAML **err_rc)
1495 {
1496         struct cYAML *tree, *item = NULL, *head, *child;
1497         cmd_handler_t cb;
1498         char err_str[LNET_MAX_STR_LEN];
1499         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
1500
1501         tree = cYAML_build_tree(f, NULL, 0, err_rc);
1502         if (tree == NULL)
1503                 return LUSTRE_CFG_RC_BAD_PARAM;
1504
1505         child = tree->cy_child;
1506         while (child != NULL) {
1507                 cb = lookup_fn(child->cy_string, table);
1508                 if (cb == NULL) {
1509                         snprintf(err_str, sizeof(err_str),
1510                                 "\"call back for '%s' not found\"",
1511                                 child->cy_string);
1512                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
1513                                         "yaml", "helper", err_str, err_rc);
1514                         goto out;
1515                 }
1516
1517                 if (cYAML_is_sequence(child)) {
1518                         while ((head = cYAML_get_next_seq_item(child, &item))
1519                                != NULL) {
1520                                 rc = cb(head, show_rc, err_rc);
1521                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1522                                         return_rc = rc;
1523                         }
1524                 } else {
1525                         rc = cb(child, show_rc, err_rc);
1526                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1527                                 return_rc = rc;
1528                 }
1529                 item = NULL;
1530                 child = child->cy_next;
1531         }
1532
1533 out:
1534         cYAML_free_tree(tree);
1535
1536         return return_rc;
1537 }
1538
1539 int lustre_yaml_config(char *f, struct cYAML **err_rc)
1540 {
1541         return lustre_yaml_cb_helper(f, lookup_config_tbl,
1542                                      NULL, err_rc);
1543 }
1544
1545 int lustre_yaml_del(char *f, struct cYAML **err_rc)
1546 {
1547         return lustre_yaml_cb_helper(f, lookup_del_tbl,
1548                                      NULL, err_rc);
1549 }
1550
1551 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
1552 {
1553         return lustre_yaml_cb_helper(f, lookup_show_tbl,
1554                                      show_rc, err_rc);
1555 }