Whamcloud - gitweb
29d286c1dff51e04c96876767997955a27fe460b
[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) 2013, 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
268         snprintf(err_str, sizeof(err_str),
269                  "\"out of memory\"");
270
271         if (nw != NULL) {
272                 net = libcfs_str2net(nw);
273                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
274                         snprintf(err_str,
275                                  sizeof(err_str),
276                                  "\"cannot parse net '%s'\"", nw);
277                         rc = LUSTRE_CFG_RC_BAD_PARAM;
278                         goto out;
279                 }
280
281                 if (LNET_NETTYP(net) == CIBLND    ||
282                     LNET_NETTYP(net) == OPENIBLND ||
283                     LNET_NETTYP(net) == IIBLND    ||
284                     LNET_NETTYP(net) == VIBLND) {
285                         snprintf(err_str,
286                                  sizeof(err_str),
287                                  "\"obsolete LNet type '%s'\"",
288                                  libcfs_lnd2str(net));
289                         rc = LUSTRE_CFG_RC_BAD_PARAM;
290                         goto out;
291                 }
292         } else {
293                 /* show all routes without filtering on net */
294                 net = LNET_NIDNET(LNET_NID_ANY);
295         }
296
297         if (gw != NULL) {
298                 gateway_nid = libcfs_str2nid(gw);
299                 if (gateway_nid == LNET_NID_ANY) {
300                         snprintf(err_str,
301                                  sizeof(err_str),
302                                  "\"cannot parse gateway NID '%s'\"", gw);
303                         rc = LUSTRE_CFG_RC_BAD_PARAM;
304                         goto out;
305                 }
306         } else
307                 /* show all routes with out filtering on gateway */
308                 gateway_nid = LNET_NID_ANY;
309
310         if ((hops < 1 && hops != -1) || hops > 255) {
311                 snprintf(err_str,
312                          sizeof(err_str),
313                          "\"invalid hop count %d, must be between 0 and 256\"",
314                          hops);
315                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
316                 goto out;
317         }
318
319         /* create struct cYAML root object */
320         root = cYAML_create_object(NULL, NULL);
321         if (root == NULL)
322                 goto out;
323
324         route = cYAML_create_seq(root, "route");
325         if (route == NULL)
326                 goto out;
327
328         for (i = 0;; i++) {
329                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
330                 data.cfg_count = i;
331
332                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
333                 if (rc != 0)
334                         break;
335
336                 /* filter on provided data */
337                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
338                     net != data.cfg_net)
339                         continue;
340
341                 if (gateway_nid != LNET_NID_ANY &&
342                     gateway_nid != data.cfg_nid)
343                         continue;
344
345                 if (hops != -1 &&
346                     hops != data.cfg_config_u.cfg_route.rtr_hop)
347                         continue;
348
349                 if (prio != -1 &&
350                     prio != data.cfg_config_u.cfg_route.rtr_priority)
351                         continue;
352
353                 /* default rc to -1 incase we hit the goto */
354                 rc = -1;
355
356                 item = cYAML_create_seq_item(route);
357                 if (item == NULL)
358                         goto out;
359
360                 if (first_seq == NULL)
361                         first_seq = item;
362
363                 if (cYAML_create_string(item, "net",
364                                         libcfs_net2str(data.cfg_net)) == NULL)
365                         goto out;
366
367                 if (cYAML_create_string(item, "gateway",
368                                         libcfs_nid2str(data.cfg_nid)) == NULL)
369                         goto out;
370
371                 if (detail) {
372                         if (cYAML_create_number(item, "hop",
373                                                 data.cfg_config_u.cfg_route.
374                                                         rtr_hop) ==
375                             NULL)
376                                 goto out;
377
378                         if (cYAML_create_number(item, "priority",
379                                                 data.cfg_config_u.
380                                                 cfg_route.rtr_priority) == NULL)
381                                 goto out;
382
383                         if (cYAML_create_string(item, "state",
384                                                 data.cfg_config_u.cfg_route.
385                                                         rtr_flags ?
386                                                 "up" : "down") == NULL)
387                                 goto out;
388                 }
389         }
390
391         /* print output iff show_rc is not provided */
392         if (show_rc == NULL)
393                 cYAML_print_tree(root);
394
395         if (errno != ENOENT) {
396                 snprintf(err_str,
397                          sizeof(err_str),
398                          "\"cannot get routes: %s\"",
399                          strerror(errno));
400                 rc = -errno;
401                 goto out;
402         } else
403                 rc = LUSTRE_CFG_RC_NO_ERR;
404
405         snprintf(err_str, sizeof(err_str), "\"success\"");
406 out:
407         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
408                 cYAML_free_tree(root);
409         } else if (show_rc != NULL && *show_rc != NULL) {
410                 struct cYAML *show_node;
411                 /* find the route node, if one doesn't exist then
412                  * insert one.  Otherwise add to the one there
413                  */
414                 show_node = cYAML_get_object_item(*show_rc, "route");
415                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
416                         cYAML_insert_child(show_node, first_seq);
417                         free(route);
418                         free(root);
419                 } else if (show_node == NULL) {
420                         cYAML_insert_sibling((*show_rc)->cy_child,
421                                                 route);
422                         free(root);
423                 } else {
424                         cYAML_free_tree(root);
425                 }
426         } else {
427                 *show_rc = root;
428         }
429
430         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
431
432         return rc;
433 }
434
435 int lustre_lnet_config_net(char *net, char *intf, char *ip2net,
436                            int peer_to, int peer_cr, int peer_buf_cr,
437                            int credits, char *smp, int seq_no,
438                            struct cYAML **err_rc)
439 {
440         struct lnet_ioctl_config_data data;
441         char buf[LNET_MAX_STR_LEN];
442         int rc = LUSTRE_CFG_RC_NO_ERR, num_of_nets = 0;
443         char err_str[LNET_MAX_STR_LEN];
444
445         snprintf(err_str, sizeof(err_str), "\"success\"");
446
447         if (ip2net == NULL && (intf == NULL || net == NULL)) {
448                 snprintf(err_str,
449                          sizeof(err_str),
450                          "\"mandatory parameter '%s' not specified."
451                          " Optionally specify ip2net parameter\"",
452                          (intf == NULL && net == NULL) ? "net, if" :
453                          (intf == NULL) ? "if" : "net");
454                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
455                 goto out;
456         }
457
458         if (peer_to != -1 && peer_to <= 0) {
459                 snprintf(err_str,
460                          sizeof(err_str),
461                          "\"peer timeout %d, must be greater than 0\"",
462                          peer_to);
463                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
464                 goto out;
465         }
466
467         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
468                 snprintf(err_str,
469                          sizeof(err_str),
470                          "\"ip2net string too long %d\"",
471                                 (int)strlen(ip2net));
472                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
473                 goto out;
474         }
475
476         if (ip2net == NULL)
477                 snprintf(buf, sizeof(buf) - 1, "%s(%s)%s",
478                         net, intf,
479                         (smp) ? smp : "");
480
481         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
482         strncpy(data.cfg_config_u.cfg_net.net_intf,
483                 (ip2net != NULL) ? ip2net : buf, sizeof(buf));
484         data.cfg_config_u.cfg_net.net_peer_timeout = peer_to;
485         data.cfg_config_u.cfg_net.net_peer_tx_credits = peer_cr;
486         data.cfg_config_u.cfg_net.net_peer_rtr_credits = peer_buf_cr;
487         data.cfg_config_u.cfg_net.net_max_tx_credits = credits;
488
489         num_of_nets = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_NET, &data);
490         if (num_of_nets < 0) {
491                 snprintf(err_str,
492                          sizeof(err_str),
493                          "\"cannot add network: %s\"", strerror(errno));
494                 rc = -errno;
495         }
496
497 out:
498         cYAML_build_error((num_of_nets > 0) ? num_of_nets : rc,
499                          seq_no, ADD_CMD, "net", err_str, err_rc);
500
501         return rc;
502 }
503
504 int lustre_lnet_del_net(char *nw, int seq_no, struct cYAML **err_rc)
505 {
506         struct lnet_ioctl_config_data data;
507         __u32 net = LNET_NIDNET(LNET_NID_ANY);
508         int rc = LUSTRE_CFG_RC_NO_ERR;
509         char err_str[LNET_MAX_STR_LEN];
510
511         snprintf(err_str, sizeof(err_str), "\"success\"");
512
513         if (nw == NULL) {
514                 snprintf(err_str,
515                          sizeof(err_str),
516                          "\"missing mandatory parameter\"");
517                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
518                 goto out;
519         }
520
521         net = libcfs_str2net(nw);
522         if (net == LNET_NIDNET(LNET_NID_ANY)) {
523                 snprintf(err_str,
524                          sizeof(err_str),
525                          "\"cannot parse net '%s'\"", nw);
526                 rc = LUSTRE_CFG_RC_BAD_PARAM;
527                 goto out;
528         }
529
530         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
531         data.cfg_net = net;
532
533         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_NET, &data);
534         if (rc != 0) {
535                 snprintf(err_str,
536                          sizeof(err_str),
537                          "\"cannot delete network: %s\"", strerror(errno));
538                 rc = -errno;
539                 goto out;
540         }
541
542 out:
543         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
544
545         return rc;
546 }
547
548 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
549                          struct cYAML **show_rc, struct cYAML **err_rc)
550 {
551         char *buf;
552         struct lnet_ioctl_config_data *data;
553         struct lnet_ioctl_net_config *net_config;
554         __u32 net = LNET_NIDNET(LNET_NID_ANY);
555         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
556         struct cYAML *root = NULL, *tunables = NULL,
557                 *net_node = NULL, *interfaces = NULL,
558                 *item = NULL, *first_seq = NULL;
559         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
560         char str_buf[str_buf_len];
561         char *pos;
562         char err_str[LNET_MAX_STR_LEN];
563
564         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
565
566         buf = calloc(1, sizeof(*data) + sizeof(*net_config));
567         if (buf == NULL)
568                 goto out;
569
570         data = (struct lnet_ioctl_config_data *)buf;
571
572         if (nw != NULL) {
573                 net = libcfs_str2net(nw);
574                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
575                         snprintf(err_str,
576                                  sizeof(err_str),
577                                  "\"cannot parse net '%s'\"", nw);
578                         rc = LUSTRE_CFG_RC_BAD_PARAM;
579                         goto out;
580                 }
581         }
582
583         root = cYAML_create_object(NULL, NULL);
584         if (root == NULL)
585                 goto out;
586
587         net_node = cYAML_create_seq(root, "net");
588         if (net_node == NULL)
589                 goto out;
590
591         for (i = 0;; i++) {
592                 pos = str_buf;
593
594                 memset(buf, 0, sizeof(*data) + sizeof(*net_config));
595
596                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
597                 /*
598                  * set the ioc_len to the proper value since INIT assumes
599                  * size of data
600                  */
601                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
602                   sizeof(struct lnet_ioctl_net_config);
603                 data->cfg_count = i;
604
605                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NET, data);
606                 if (rc != 0)
607                         break;
608
609                 /* filter on provided data */
610                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
611                     net != LNET_NIDNET(data->cfg_nid))
612                         continue;
613
614                 /* default rc to -1 in case we hit the goto */
615                 rc = -1;
616
617                 net_config = (struct lnet_ioctl_net_config *)data->cfg_bulk;
618
619                 /* create the tree to be printed. */
620                 item = cYAML_create_seq_item(net_node);
621                 if (item == NULL)
622                         goto out;
623
624                 if (first_seq == NULL)
625                         first_seq = item;
626
627                 if (cYAML_create_string(item,
628                                         "net",
629                                         libcfs_net2str(
630                                                 LNET_NIDNET(data->cfg_nid)))
631                     == NULL)
632                         goto out;
633
634                 if (cYAML_create_string(item, "nid",
635                                         libcfs_nid2str(data->cfg_nid)) == NULL)
636                         goto out;
637
638                 if (cYAML_create_string(item,
639                                         "status",
640                                         (net_config->ni_status ==
641                                           LNET_NI_STATUS_UP) ?
642                                             "up" : "down") == NULL)
643                         goto out;
644
645                 /* don't add interfaces unless there is at least one
646                  * interface */
647                 if (strlen(net_config->ni_interfaces[0]) > 0) {
648                         interfaces = cYAML_create_object(item, "interfaces");
649                         if (interfaces == NULL)
650                                 goto out;
651
652                         for (j = 0; j < LNET_MAX_INTERFACES; j++) {
653                                 if (strlen(net_config->ni_interfaces[j]) > 0) {
654                                         snprintf(str_buf,
655                                                  sizeof(str_buf), "%d", j);
656                                         if (cYAML_create_string(interfaces,
657                                                 str_buf,
658                                                 net_config->ni_interfaces[j]) ==
659                                             NULL)
660                                                 goto out;
661                                 }
662                         }
663                 }
664
665                 if (detail) {
666                         tunables = cYAML_create_object(item, "tunables");
667                         if (tunables == NULL)
668                                 goto out;
669
670                         if (cYAML_create_number(tunables, "peer_timeout",
671                                                 data->cfg_config_u.cfg_net.
672                                                  net_peer_timeout) == NULL)
673                                 goto out;
674
675                         if (cYAML_create_number(tunables, "peer_credits",
676                                                 data->cfg_config_u.cfg_net.
677                                                   net_peer_tx_credits) == NULL)
678                                 goto out;
679
680                         if (cYAML_create_number(tunables,
681                                                 "peer_buffer_credits",
682                                                 data->cfg_config_u.cfg_net.
683                                                   net_peer_rtr_credits) == NULL)
684                                 goto out;
685
686                         if (cYAML_create_number(tunables, "credits",
687                                                 data->cfg_config_u.cfg_net.
688                                                   net_max_tx_credits) == NULL)
689                                 goto out;
690
691                         /* out put the CPTs in the format: "[x,x,x,...]" */
692                         pos += snprintf(pos, str_buf + str_buf_len - pos, "[");
693                         for (j = 0 ; data->cfg_ncpts > 1 &&
694                                 j < data->cfg_ncpts; j++) {
695                                 pos += snprintf(pos,
696                                                 str_buf + str_buf_len - pos,
697                                                 "%d", net_config->ni_cpts[j]);
698                                 if ((j + 1) < data->cfg_ncpts)
699                                         pos += snprintf(pos,
700                                                         str_buf +
701                                                          str_buf_len - pos,
702                                                         ",");
703                         }
704                         if (str_buf + str_buf_len - pos <= 0)
705                                 pos += snprintf(str_buf + str_buf_len - 2,
706                                                 2, "]");
707                         else
708                                 pos += snprintf(pos,
709                                                 str_buf + str_buf_len - pos,
710                                                 "]");
711
712                         if (data->cfg_ncpts > 1 &&
713                             cYAML_create_string(tunables, "CPT",
714                                                 str_buf) == NULL)
715                                 goto out;
716                 }
717         }
718
719         /* Print out the net information only if show_rc is not provided */
720         if (show_rc == NULL)
721                 cYAML_print_tree(root);
722
723         if (errno != ENOENT) {
724                 snprintf(err_str,
725                          sizeof(err_str),
726                          "\"cannot get networks: %s\"",
727                          strerror(errno));
728                 rc = -errno;
729                 goto out;
730         } else
731                 rc = LUSTRE_CFG_RC_NO_ERR;
732
733         snprintf(err_str, sizeof(err_str), "\"success\"");
734 out:
735         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
736                 cYAML_free_tree(root);
737         } else if (show_rc != NULL && *show_rc != NULL) {
738                 struct cYAML *show_node;
739                 /* find the net node, if one doesn't exist
740                  * then insert one.  Otherwise add to the one there
741                  */
742                 show_node = cYAML_get_object_item(*show_rc, "net");
743                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
744                         cYAML_insert_child(show_node, first_seq);
745                         free(net_node);
746                         free(root);
747                 } else if (show_node == NULL) {
748                         cYAML_insert_sibling((*show_rc)->cy_child,
749                                                 net_node);
750                         free(root);
751                 } else {
752                         cYAML_free_tree(root);
753                 }
754         } else {
755                 *show_rc = root;
756         }
757
758         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
759
760         return rc;
761 }
762
763 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
764 {
765         struct lnet_ioctl_config_data data;
766         int rc = LUSTRE_CFG_RC_NO_ERR;
767         char err_str[LNET_MAX_STR_LEN];
768
769         snprintf(err_str, sizeof(err_str), "\"success\"");
770
771         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
772         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
773
774         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
775         if (rc != 0) {
776                 snprintf(err_str,
777                          sizeof(err_str),
778                          "\"cannot %s routing %s\"",
779                          (enable) ? "enable" : "disable", strerror(errno));
780                 rc = -errno;
781                 goto out;
782         }
783
784 out:
785         cYAML_build_error(rc, seq_no,
786                          (enable) ? ADD_CMD : DEL_CMD,
787                          "routing", err_str, err_rc);
788
789         return rc;
790 }
791
792 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
793                                struct cYAML **err_rc)
794 {
795         struct lnet_ioctl_config_data data;
796         int rc = LUSTRE_CFG_RC_NO_ERR;
797         char err_str[LNET_MAX_STR_LEN];
798
799         snprintf(err_str, sizeof(err_str), "\"success\"");
800
801         /* -1 indicates to ignore changes to this field */
802         if (tiny < -1 || small < -1 || large < -1) {
803                 snprintf(err_str,
804                          sizeof(err_str),
805                          "\"tiny, small and large must be >= 0\"");
806                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
807                 goto out;
808         }
809
810         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
811         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
812         data.cfg_config_u.cfg_buffers.buf_small = small;
813         data.cfg_config_u.cfg_buffers.buf_large = large;
814
815         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
816         if (rc != 0) {
817                 snprintf(err_str,
818                          sizeof(err_str),
819                          "\"cannot configure buffers: %s\"", strerror(errno));
820                 rc = -errno;
821                 goto out;
822         }
823
824 out:
825         cYAML_build_error(rc, seq_no, DEL_CMD, "buf", err_str, err_rc);
826
827         return rc;
828 }
829
830 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
831                              struct cYAML **err_rc)
832 {
833         struct lnet_ioctl_config_data *data;
834         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
835         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
836         char *buf;
837         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
838         struct cYAML *root = NULL, *pools_node = NULL,
839                      *type_node = NULL, *item = NULL, *cpt = NULL,
840                      *first_seq = NULL;
841         int i, j;
842         char err_str[LNET_MAX_STR_LEN];
843         char node_name[LNET_MAX_STR_LEN];
844
845         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
846
847         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
848         if (buf == NULL)
849                 goto out;
850
851         data = (struct lnet_ioctl_config_data *)buf;
852
853         root = cYAML_create_object(NULL, NULL);
854         if (root == NULL)
855                 goto out;
856
857         pools_node = cYAML_create_seq(root, "routing");
858         if (pools_node == NULL)
859                 goto out;
860
861         for (i = 0;; i++) {
862                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
863                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
864                                         sizeof(struct lnet_ioctl_pool_cfg);
865                 data->cfg_count = i;
866
867                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
868                 if (rc != 0)
869                         break;
870
871                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
872
873                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
874                 item = cYAML_create_seq_item(pools_node);
875                 if (item == NULL)
876                         goto out;
877
878                 if (first_seq == NULL)
879                         first_seq = item;
880
881                 cpt = cYAML_create_object(item, node_name);
882                 if (cpt == NULL)
883                         goto out;
884
885                 /* create the tree  and print */
886                 for (j = 0; j < LNET_NRBPOOLS; j++) {
887                         type_node = cYAML_create_object(cpt, pools[j]);
888                         if (type_node == NULL)
889                                 goto out;
890                         if (cYAML_create_number(type_node, "npages",
891                                                 pool_cfg->pl_pools[j].pl_npages)
892                             == NULL)
893                                 goto out;
894                         if (cYAML_create_number(type_node, "nbuffers",
895                                                 pool_cfg->pl_pools[j].
896                                                   pl_nbuffers) == NULL)
897                                 goto out;
898                         if (cYAML_create_number(type_node, "credits",
899                                                 pool_cfg->pl_pools[j].
900                                                    pl_credits) == NULL)
901                                 goto out;
902                         if (cYAML_create_number(type_node, "mincredits",
903                                                 pool_cfg->pl_pools[j].
904                                                    pl_mincredits) == NULL)
905                                 goto out;
906                 }
907         }
908
909         if (pool_cfg != NULL) {
910                 item = cYAML_create_seq_item(pools_node);
911                 if (item == NULL)
912                         goto out;
913
914                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
915                     NULL)
916                         goto out;
917         }
918
919         if (show_rc == NULL)
920                 cYAML_print_tree(root);
921
922         if (errno != ENOENT) {
923                 snprintf(err_str,
924                          sizeof(err_str),
925                          "\"cannot get routing information: %s\"",
926                          strerror(errno));
927                 rc = -errno;
928                 goto out;
929         } else
930                 rc = LUSTRE_CFG_RC_NO_ERR;
931
932         snprintf(err_str, sizeof(err_str), "\"success\"");
933         rc = LUSTRE_CFG_RC_NO_ERR;
934
935 out:
936         free(buf);
937         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
938                 cYAML_free_tree(root);
939         } else if (show_rc != NULL && *show_rc != NULL) {
940                 struct cYAML *show_node;
941                 /* find the routing node, if one doesn't exist then
942                  * insert one.  Otherwise add to the one there
943                  */
944                 show_node = cYAML_get_object_item(*show_rc, "routing");
945                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
946                         cYAML_insert_child(show_node, first_seq);
947                         free(pools_node);
948                         free(root);
949                 } else if (show_node == NULL) {
950                         cYAML_insert_sibling((*show_rc)->cy_child,
951                                                 pools_node);
952                         free(root);
953                 } else {
954                         cYAML_free_tree(root);
955                 }
956         } else {
957                 *show_rc = root;
958         }
959
960         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
961
962         return rc;
963 }
964
965 int lustre_lnet_show_peer_credits(int seq_no, struct cYAML **show_rc,
966                                   struct cYAML **err_rc)
967 {
968         struct lnet_ioctl_peer peer_info;
969         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
970         struct cYAML *root = NULL, *peer = NULL, *first_seq = NULL,
971                      *peer_root = NULL;
972         char err_str[LNET_MAX_STR_LEN];
973         bool ncpt_set = false;
974
975         snprintf(err_str, sizeof(err_str),
976                  "\"out of memory\"");
977
978         /* create struct cYAML root object */
979         root = cYAML_create_object(NULL, NULL);
980         if (root == NULL)
981                 goto out;
982
983         peer_root = cYAML_create_seq(root, "peer");
984                 if (peer_root == NULL)
985                         goto out;
986
987         do {
988                 for (i = 0;; i++) {
989                         LIBCFS_IOC_INIT_V2(peer_info, pr_hdr);
990                         peer_info.pr_count = i;
991                         peer_info.pr_lnd_u.pr_peer_credits.cr_ncpt = j;
992                         rc = l_ioctl(LNET_DEV_ID,
993                                      IOC_LIBCFS_GET_PEER_INFO, &peer_info);
994                         if (rc != 0)
995                                 break;
996
997                         if (ncpt_set != 0) {
998                                 ncpt = peer_info.pr_lnd_u.pr_peer_credits.
999                                         cr_ncpt;
1000                                 ncpt_set = true;
1001                         }
1002
1003                         peer = cYAML_create_seq_item(peer_root);
1004                         if (peer == NULL)
1005                                 goto out;
1006
1007                         if (first_seq == NULL)
1008                                 first_seq = peer;
1009
1010                         if (cYAML_create_string(peer, "nid",
1011                                                 libcfs_nid2str
1012                                                  (peer_info.pr_nid)) == NULL)
1013                                 goto out;
1014
1015                         if (cYAML_create_string(peer, "state",
1016                                                 peer_info.pr_lnd_u.
1017                                                   pr_peer_credits.
1018                                                         cr_aliveness) ==
1019                             NULL)
1020                                 goto out;
1021
1022                         if (cYAML_create_number(peer, "refcount",
1023                                                 peer_info.pr_lnd_u.
1024                                                   pr_peer_credits.
1025                                                         cr_refcount) == NULL)
1026                                 goto out;
1027
1028                         if (cYAML_create_number(peer, "max_ni_tx_credits",
1029                                                 peer_info.pr_lnd_u.
1030                                                   pr_peer_credits.
1031                                                     cr_ni_peer_tx_credits)
1032                             == NULL)
1033                                 goto out;
1034
1035                         if (cYAML_create_number(peer, "available_tx_credits",
1036                                                 peer_info.pr_lnd_u.
1037                                                   pr_peer_credits.
1038                                                     cr_peer_tx_credits)
1039                             == NULL)
1040                                 goto out;
1041
1042                         if (cYAML_create_number(peer, "available_rtr_credits",
1043                                                 peer_info.pr_lnd_u.
1044                                                   pr_peer_credits.
1045                                                     cr_peer_rtr_credits)
1046                             == NULL)
1047                                 goto out;
1048
1049                         if (cYAML_create_number(peer, "min_rtr_credits",
1050                                                 peer_info.pr_lnd_u.
1051                                                   pr_peer_credits.
1052                                                     cr_peer_min_rtr_credits)
1053                             == NULL)
1054                                 goto out;
1055
1056                         if (cYAML_create_number(peer, "tx_q_num_of_buf",
1057                                                 peer_info.pr_lnd_u.
1058                                                   pr_peer_credits.
1059                                                     cr_peer_tx_qnob)
1060                             == NULL)
1061                                 goto out;
1062                 }
1063
1064                 if (errno != ENOENT) {
1065                         snprintf(err_str,
1066                                 sizeof(err_str),
1067                                 "\"cannot get peer information: %s\"",
1068                                 strerror(errno));
1069                         rc = -errno;
1070                         goto out;
1071                 }
1072
1073                 j++;
1074         } while (j < ncpt);
1075
1076         /* print output iff show_rc is not provided */
1077         if (show_rc == NULL)
1078                 cYAML_print_tree(root);
1079
1080         snprintf(err_str, sizeof(err_str), "\"success\"");
1081         rc = LUSTRE_CFG_RC_NO_ERR;
1082
1083 out:
1084         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1085                 cYAML_free_tree(root);
1086         } else if (show_rc != NULL && *show_rc != NULL) {
1087                 struct cYAML *show_node;
1088                 /* find the peer node, if one doesn't exist then
1089                  * insert one.  Otherwise add to the one there
1090                  */
1091                 show_node = cYAML_get_object_item(*show_rc,
1092                                                   "peer_credits");
1093                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1094                         cYAML_insert_child(show_node, first_seq);
1095                         free(peer_root);
1096                         free(root);
1097                 } else if (show_node == NULL) {
1098                         cYAML_insert_sibling((*show_rc)->cy_child,
1099                                              peer_root);
1100                         free(root);
1101                 } else {
1102                         cYAML_free_tree(root);
1103                 }
1104         } else {
1105                 *show_rc = root;
1106         }
1107
1108         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer_credits", err_str,
1109                           err_rc);
1110
1111         return rc;
1112 }
1113
1114 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
1115                            struct cYAML **err_rc)
1116 {
1117         struct lnet_ioctl_lnet_stats data;
1118         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1119         char err_str[LNET_MAX_STR_LEN];
1120         struct cYAML *root = NULL, *stats = NULL;
1121
1122         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1123
1124         LIBCFS_IOC_INIT_V2(data, st_hdr);
1125
1126         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
1127         if (rc != 0) {
1128                 snprintf(err_str,
1129                          sizeof(err_str),
1130                          "\"cannot get lnet statistics: %s\"",
1131                          strerror(errno));
1132                 rc = -errno;
1133                 goto out;
1134         }
1135
1136         root = cYAML_create_object(NULL, NULL);
1137         if (root == NULL)
1138                 goto out;
1139
1140         stats = cYAML_create_object(root, "statistics");
1141         if (stats == NULL)
1142                 goto out;
1143
1144         if (cYAML_create_number(stats, "msgs_alloc",
1145                                 data.st_cntrs.msgs_alloc) == NULL)
1146                 goto out;
1147
1148         if (cYAML_create_number(stats, "msgs_max",
1149                                 data.st_cntrs.msgs_max) == NULL)
1150                 goto out;
1151
1152         if (cYAML_create_number(stats, "errors",
1153                                 data.st_cntrs.errors) == NULL)
1154                 goto out;
1155
1156         if (cYAML_create_number(stats, "send_count",
1157                                 data.st_cntrs.send_count) == NULL)
1158                 goto out;
1159
1160         if (cYAML_create_number(stats, "recv_count",
1161                                 data.st_cntrs.recv_count) == NULL)
1162                 goto out;
1163
1164         if (cYAML_create_number(stats, "route_count",
1165                                 data.st_cntrs.route_count) == NULL)
1166                 goto out;
1167
1168         if (cYAML_create_number(stats, "drop_count",
1169                                 data.st_cntrs.drop_count) == NULL)
1170                 goto out;
1171
1172         if (cYAML_create_number(stats, "send_length",
1173                                 data.st_cntrs.send_length) == NULL)
1174                 goto out;
1175
1176         if (cYAML_create_number(stats, "recv_length",
1177                                 data.st_cntrs.recv_length) == NULL)
1178                 goto out;
1179
1180         if (cYAML_create_number(stats, "route_length",
1181                                 data.st_cntrs.route_length) == NULL)
1182                 goto out;
1183
1184         if (cYAML_create_number(stats, "drop_length",
1185                                 data.st_cntrs.drop_length) == NULL)
1186                 goto out;
1187
1188         if (show_rc == NULL)
1189                 cYAML_print_tree(root);
1190
1191         snprintf(err_str, sizeof(err_str), "\"success\"");
1192 out:
1193         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
1194                 cYAML_free_tree(root);
1195         } else if (show_rc != NULL && *show_rc != NULL) {
1196                 cYAML_insert_sibling((*show_rc)->cy_child,
1197                                         root->cy_child);
1198                 free(root);
1199         } else {
1200                 *show_rc = root;
1201         }
1202
1203         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
1204
1205         return rc;
1206 }
1207
1208 typedef int (*cmd_handler_t)(struct cYAML *tree,
1209                              struct cYAML **show_rc,
1210                              struct cYAML **err_rc);
1211
1212 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
1213                                     struct cYAML **err_rc)
1214 {
1215         struct cYAML *net, *gw, *hop, *prio, *seq_no;
1216
1217         net = cYAML_get_object_item(tree, "net");
1218         gw = cYAML_get_object_item(tree, "gateway");
1219         hop = cYAML_get_object_item(tree, "hop");
1220         prio = cYAML_get_object_item(tree, "priority");
1221         seq_no = cYAML_get_object_item(tree, "seq_no");
1222
1223         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
1224                                         (gw) ? gw->cy_valuestring : NULL,
1225                                         (hop) ? hop->cy_valueint : -1,
1226                                         (prio) ? prio->cy_valueint : -1,
1227                                         (seq_no) ? seq_no->cy_valueint : -1,
1228                                         err_rc);
1229 }
1230
1231 static int handle_yaml_config_net(struct cYAML *tree, struct cYAML **show_rc,
1232                                   struct cYAML **err_rc)
1233 {
1234         struct cYAML *net, *intf, *tunables, *seq_no,
1235               *peer_to = NULL, *peer_buf_cr = NULL, *peer_cr = NULL,
1236               *credits = NULL, *ip2net = NULL, *smp = NULL, *child;
1237         char devs[LNET_MAX_STR_LEN];
1238         char *loc = devs;
1239         int size = LNET_MAX_STR_LEN;
1240         int num;
1241         bool intf_found = false;
1242
1243         ip2net = cYAML_get_object_item(tree, "ip2net");
1244         net = cYAML_get_object_item(tree, "net");
1245         intf = cYAML_get_object_item(tree, "interfaces");
1246         if (intf != NULL) {
1247                 /* grab all the interfaces */
1248                 child = intf->cy_child;
1249                 while (child != NULL && size > 0) {
1250                         if (loc > devs)
1251                                 num  = snprintf(loc, size, ",%s",
1252                                                 child->cy_valuestring);
1253                         else
1254                                 num = snprintf(loc, size, "%s",
1255                                                child->cy_valuestring);
1256                         size -= num;
1257                         loc += num;
1258                         intf_found = true;
1259                         child = child->cy_next;
1260                 }
1261         }
1262
1263         tunables = cYAML_get_object_item(tree, "tunables");
1264         if (tunables != NULL) {
1265                 peer_to = cYAML_get_object_item(tunables, "peer_timeout");
1266                 peer_cr = cYAML_get_object_item(tunables, "peer_credits");
1267                 peer_buf_cr = cYAML_get_object_item(tunables,
1268                                                     "peer_buffer_credits");
1269                 credits = cYAML_get_object_item(tunables, "credits");
1270                 smp = cYAML_get_object_item(tunables, "CPT");
1271         }
1272         seq_no = cYAML_get_object_item(tree, "seq_no");
1273
1274         return lustre_lnet_config_net((net) ? net->cy_valuestring : NULL,
1275                                       (intf_found) ? devs : NULL,
1276                                       (ip2net) ? ip2net->cy_valuestring : NULL,
1277                                       (peer_to) ? peer_to->cy_valueint : -1,
1278                                       (peer_cr) ? peer_cr->cy_valueint : -1,
1279                                       (peer_buf_cr) ?
1280                                         peer_buf_cr->cy_valueint : -1,
1281                                       (credits) ? credits->cy_valueint : -1,
1282                                       (smp) ? smp->cy_valuestring : NULL,
1283                                       (seq_no) ? seq_no->cy_valueint : -1,
1284                                       err_rc);
1285 }
1286
1287 static int handle_yaml_config_buffers(struct cYAML *tree,
1288                                       struct cYAML **show_rc,
1289                                       struct cYAML **err_rc)
1290 {
1291         int rc;
1292         struct cYAML *tiny, *small, *large, *seq_no;
1293
1294         tiny = cYAML_get_object_item(tree, "tiny");
1295         small = cYAML_get_object_item(tree, "small");
1296         large = cYAML_get_object_item(tree, "large");
1297         seq_no = cYAML_get_object_item(tree, "seq_no");
1298
1299         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
1300                                         (small) ? small->cy_valueint : -1,
1301                                         (large) ? large->cy_valueint : -1,
1302                                         (seq_no) ? seq_no->cy_valueint : -1,
1303                                         err_rc);
1304
1305         return rc;
1306 }
1307
1308 static int handle_yaml_config_routing(struct cYAML *tree,
1309                                       struct cYAML **show_rc,
1310                                       struct cYAML **err_rc)
1311 {
1312         int rc = LUSTRE_CFG_RC_NO_ERR;
1313         struct cYAML *seq_no, *enable;
1314
1315         seq_no = cYAML_get_object_item(tree, "seq_no");
1316         enable = cYAML_get_object_item(tree, "enable");
1317
1318         if (enable) {
1319                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
1320                                                 (seq_no) ?
1321                                                     seq_no->cy_valueint : -1,
1322                                                 err_rc);
1323         }
1324
1325         return rc;
1326 }
1327
1328 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
1329                                  struct cYAML **err_rc)
1330 {
1331         struct cYAML *net;
1332         struct cYAML *gw;
1333         struct cYAML *seq_no;
1334
1335         net = cYAML_get_object_item(tree, "net");
1336         gw = cYAML_get_object_item(tree, "gateway");
1337         seq_no = cYAML_get_object_item(tree, "seq_no");
1338
1339         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
1340                                      (gw) ? gw->cy_valuestring : NULL,
1341                                      (seq_no) ? seq_no->cy_valueint : -1,
1342                                      err_rc);
1343 }
1344
1345 static int handle_yaml_del_net(struct cYAML *tree, struct cYAML **show_rc,
1346                                struct cYAML **err_rc)
1347 {
1348         struct cYAML *net, *seq_no;
1349
1350         net = cYAML_get_object_item(tree, "net");
1351         seq_no = cYAML_get_object_item(tree, "seq_no");
1352
1353         return lustre_lnet_del_net((net) ? net->cy_valuestring : NULL,
1354                                    (seq_no) ? seq_no->cy_valueint : -1,
1355                                    err_rc);
1356 }
1357
1358 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
1359                                    struct cYAML **err_rc)
1360 {
1361         struct cYAML *seq_no;
1362
1363         seq_no = cYAML_get_object_item(tree, "seq_no");
1364
1365         return lustre_lnet_enable_routing(0, (seq_no) ?
1366                                                 seq_no->cy_valueint : -1,
1367                                         err_rc);
1368 }
1369
1370 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
1371                                   struct cYAML **err_rc)
1372 {
1373         struct cYAML *net;
1374         struct cYAML *gw;
1375         struct cYAML *hop;
1376         struct cYAML *prio;
1377         struct cYAML *detail;
1378         struct cYAML *seq_no;
1379
1380         net = cYAML_get_object_item(tree, "net");
1381         gw = cYAML_get_object_item(tree, "gateway");
1382         hop = cYAML_get_object_item(tree, "hop");
1383         prio = cYAML_get_object_item(tree, "priority");
1384         detail = cYAML_get_object_item(tree, "detail");
1385         seq_no = cYAML_get_object_item(tree, "seq_no");
1386
1387         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
1388                                       (gw) ? gw->cy_valuestring : NULL,
1389                                       (hop) ? hop->cy_valueint : -1,
1390                                       (prio) ? prio->cy_valueint : -1,
1391                                       (detail) ? detail->cy_valueint : 0,
1392                                       (seq_no) ? seq_no->cy_valueint : -1,
1393                                       show_rc,
1394                                       err_rc);
1395 }
1396
1397 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
1398                                 struct cYAML **err_rc)
1399 {
1400         struct cYAML *net, *detail, *seq_no;
1401
1402         net = cYAML_get_object_item(tree, "net");
1403         detail = cYAML_get_object_item(tree, "detail");
1404         seq_no = cYAML_get_object_item(tree, "seq_no");
1405
1406         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
1407                                     (detail) ? detail->cy_valueint : 0,
1408                                     (seq_no) ? seq_no->cy_valueint : -1,
1409                                     show_rc,
1410                                     err_rc);
1411 }
1412
1413 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
1414                                     struct cYAML **err_rc)
1415 {
1416         struct cYAML *seq_no;
1417
1418         seq_no = cYAML_get_object_item(tree, "seq_no");
1419
1420         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
1421                                         show_rc, err_rc);
1422 }
1423
1424 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
1425                                     struct cYAML **err_rc)
1426 {
1427         struct cYAML *seq_no;
1428
1429         seq_no = cYAML_get_object_item(tree, "seq_no");
1430
1431         return lustre_lnet_show_peer_credits((seq_no) ?
1432                                                 seq_no->cy_valueint : -1,
1433                                              show_rc, err_rc);
1434 }
1435
1436 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
1437                                   struct cYAML **err_rc)
1438 {
1439         struct cYAML *seq_no;
1440
1441         seq_no = cYAML_get_object_item(tree, "seq_no");
1442
1443         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
1444                                       show_rc, err_rc);
1445 }
1446
1447 struct lookup_cmd_hdlr_tbl {
1448         char *name;
1449         cmd_handler_t cb;
1450 };
1451
1452 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
1453         {"route", handle_yaml_config_route},
1454         {"net", handle_yaml_config_net},
1455         {"routing", handle_yaml_config_routing},
1456         {"buffers", handle_yaml_config_buffers},
1457         {NULL, NULL}
1458 };
1459
1460 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
1461         {"route", handle_yaml_del_route},
1462         {"net", handle_yaml_del_net},
1463         {"routing", handle_yaml_del_routing},
1464         {NULL, NULL}
1465 };
1466
1467 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
1468         {"route", handle_yaml_show_route},
1469         {"net", handle_yaml_show_net},
1470         {"buffers", handle_yaml_show_routing},
1471         {"routing", handle_yaml_show_routing},
1472         {"credits", handle_yaml_show_credits},
1473         {"statistics", handle_yaml_show_stats},
1474         {NULL, NULL}
1475 };
1476
1477 static cmd_handler_t lookup_fn(char *key,
1478                                struct lookup_cmd_hdlr_tbl *tbl)
1479 {
1480         int i;
1481         if (key == NULL)
1482                 return NULL;
1483
1484         for (i = 0; tbl[i].name != NULL; i++) {
1485                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
1486                         return tbl[i].cb;
1487         }
1488
1489         return NULL;
1490 }
1491
1492 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
1493                                  struct cYAML **show_rc, struct cYAML **err_rc)
1494 {
1495         struct cYAML *tree, *item = NULL, *head, *child;
1496         cmd_handler_t cb;
1497         char err_str[LNET_MAX_STR_LEN];
1498         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
1499
1500         tree = cYAML_build_tree(f, NULL, 0, err_rc);
1501         if (tree == NULL)
1502                 return LUSTRE_CFG_RC_BAD_PARAM;
1503
1504         child = tree->cy_child;
1505         while (child != NULL) {
1506                 cb = lookup_fn(child->cy_string, table);
1507                 if (cb == NULL) {
1508                         snprintf(err_str, sizeof(err_str),
1509                                 "\"call back for '%s' not found\"",
1510                                 child->cy_string);
1511                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
1512                                         "yaml", "helper", err_str, err_rc);
1513                         goto out;
1514                 }
1515
1516                 if (cYAML_is_sequence(child)) {
1517                         while ((head = cYAML_get_next_seq_item(child, &item))
1518                                != NULL) {
1519                                 rc = cb(head, show_rc, err_rc);
1520                                 /* if processing fails or no cb is found
1521                                  * then fail */
1522                                 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1523                                         snprintf(err_str, sizeof(err_str),
1524                                                 "\"Failed to process request:  "
1525                                                 "'%s' [%d, %p]\"",
1526                                                 head->cy_string, rc, cb);
1527                                         cYAML_build_error(
1528                                                 LUSTRE_CFG_RC_BAD_PARAM, -1,
1529                                                 "yaml", "helper", err_str,
1530                                                 err_rc);
1531                                         return_rc = rc;
1532                                 }
1533                         }
1534                 } else {
1535                         rc = cb(child, show_rc, err_rc);
1536                         /* if processing fails or no cb is found then fail */
1537                         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1538                                 snprintf(err_str, sizeof(err_str),
1539                                         "\"Failed to process request: '%s'"
1540                                         " [%d, %p]\"",
1541                                         child->cy_string, rc, cb);
1542                                 cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
1543                                         "yaml", "helper", err_str, err_rc);
1544                                 return_rc = rc;
1545                         }
1546                 }
1547                 item = NULL;
1548                 child = child->cy_next;
1549         }
1550
1551 out:
1552         cYAML_free_tree(tree);
1553
1554         return return_rc;
1555 }
1556
1557 int lustre_yaml_config(char *f, struct cYAML **err_rc)
1558 {
1559         return lustre_yaml_cb_helper(f, lookup_config_tbl,
1560                                      NULL, err_rc);
1561 }
1562
1563 int lustre_yaml_del(char *f, struct cYAML **err_rc)
1564 {
1565         return lustre_yaml_cb_helper(f, lookup_del_tbl,
1566                                      NULL, err_rc);
1567 }
1568
1569 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
1570 {
1571         return lustre_yaml_cb_helper(f, lookup_show_tbl,
1572                                      show_rc, err_rc);
1573 }