Whamcloud - gitweb
969143b2f6f807a1ddfcc005d39eed04dab73c93
[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, 2016, 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 <errno.h>
37 #include <limits.h>
38 #include <byteswap.h>
39 #include <netdb.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <net/if.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
48 #include "liblnd.h"
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <ifaddrs.h>
53 #include "liblnetconfig.h"
54
55 #define CONFIG_CMD              "configure"
56 #define UNCONFIG_CMD            "unconfigure"
57 #define ADD_CMD                 "add"
58 #define DEL_CMD                 "del"
59 #define SHOW_CMD                "show"
60 #define DBG_CMD                 "dbg"
61 #define MANAGE_CMD              "manage"
62
63 #define modparam_path "/sys/module/lnet/parameters/"
64
65 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
66                                  "dropped_stats"};
67
68 /*
69  * lustre_lnet_ip_range_descr
70  *      Describes an IP range.
71  *      Each octect is an expression
72  */
73 struct lustre_lnet_ip_range_descr {
74         struct list_head ipr_entry;
75         struct list_head ipr_expr;
76 };
77
78 /*
79  * lustre_lnet_ip2nets
80  *      Describes an ip2nets rule. This can be on a list of rules.
81  */
82 struct lustre_lnet_ip2nets {
83         struct lnet_dlc_network_descr ip2nets_net;
84         struct list_head ip2nets_ip_ranges;
85 };
86
87 int open_sysfs_file(const char *path, const char *attr, const int mode)
88 {
89         int fd;
90         char filename[LNET_MAX_STR_LEN];
91
92         if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
93                 return -1;
94
95         snprintf(filename, sizeof(filename), "%s%s",
96                  path, attr);
97
98         fd = open(filename, mode);
99
100         return fd;
101 }
102
103 static int read_sysfs_file(const char *path, const char *attr,
104                            void *val, const size_t size, const int nelem)
105 {
106         int fd;
107         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
108
109         fd = open_sysfs_file(path, attr, O_RDONLY);
110         if (fd == -1)
111                 return LUSTRE_CFG_RC_NO_MATCH;
112
113         if (read(fd, val, size * nelem) == -1)
114                 goto close_fd;
115
116         rc = LUSTRE_CFG_RC_NO_ERR;
117
118 close_fd:
119         close(fd);
120         return rc;
121 }
122
123 static int write_sysfs_file(const char *path, const char *attr,
124                             void *val, const size_t size, const int nelem)
125 {
126         int fd;
127         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
128
129         fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
130         if (fd == -1)
131                 return LUSTRE_CFG_RC_NO_MATCH;
132
133         if (write(fd, val, size * nelem) == -1)
134                 goto close_fd;
135
136         rc = LUSTRE_CFG_RC_NO_ERR;
137
138 close_fd:
139         close(fd);
140         return rc;
141 }
142
143 /*
144  * free_intf_descr
145  *      frees the memory allocated for an intf descriptor.
146  */
147 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
148 {
149         if (!intf_descr)
150                 return;
151
152         if (intf_descr->cpt_expr != NULL)
153                 cfs_expr_list_free(intf_descr->cpt_expr);
154         free(intf_descr);
155 }
156
157 /*
158  * lustre_lnet_add_ip_range
159  * Formatting:
160  *      given a string of the format:
161  *      <expr.expr.expr.expr> parse each expr into
162  *      a lustre_lnet_ip_range_descr structure and insert on the list.
163  *
164  *      This function is called from
165  *              YAML on each ip-range.
166  *              As a result of lnetctl command
167  *              When building a NID or P2P selection rules
168  */
169 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
170 {
171         struct lustre_lnet_ip_range_descr *ip_range;
172         int rc;
173
174         ip_range = calloc(1, sizeof(*ip_range));
175         if (ip_range == NULL)
176                 return LUSTRE_CFG_RC_OUT_OF_MEM;
177
178         INIT_LIST_HEAD(&ip_range->ipr_entry);
179         INIT_LIST_HEAD(&ip_range->ipr_expr);
180
181         rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
182                                &ip_range->ipr_expr);
183         if (rc != 0)
184                 return LUSTRE_CFG_RC_BAD_PARAM;
185
186         list_add_tail(&ip_range->ipr_entry, list);
187
188         return LUSTRE_CFG_RC_NO_ERR;
189 }
190
191 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
192 {
193         char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
194              *intf_name;
195         struct lnet_dlc_intf_descr *intf_descr = NULL;
196         int rc;
197         char intf_string[LNET_MAX_STR_LEN];
198
199         if (len >= LNET_MAX_STR_LEN)
200                 return LUSTRE_CFG_RC_BAD_PARAM;
201
202         strncpy(intf_string, intf, len);
203         intf_string[len] = '\0';
204
205         intf_descr = calloc(1, sizeof(*intf_descr));
206         if (intf_descr == NULL)
207                 return LUSTRE_CFG_RC_OUT_OF_MEM;
208
209         INIT_LIST_HEAD(&intf_descr->intf_on_network);
210
211         intf_name = intf_string;
212         open_sq_bracket = strchr(intf_string, '[');
213         if (open_sq_bracket != NULL) {
214                 close_sq_bracket = strchr(intf_string, ']');
215                 if (close_sq_bracket == NULL) {
216                         free(intf_descr);
217                         return LUSTRE_CFG_RC_BAD_PARAM;
218                 }
219                 rc = cfs_expr_list_parse(open_sq_bracket,
220                                          strlen(open_sq_bracket), 0, UINT_MAX,
221                                          &intf_descr->cpt_expr);
222                 if (rc < 0) {
223                         free(intf_descr);
224                         return LUSTRE_CFG_RC_BAD_PARAM;
225                 }
226                 strncpy(intf_descr->intf_name, intf_name,
227                         open_sq_bracket - intf_name);
228                 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
229         } else {
230                 strcpy(intf_descr->intf_name, intf_name);
231                 intf_descr->cpt_expr = NULL;
232         }
233
234         list_add_tail(&intf_descr->intf_on_network, list);
235
236         return LUSTRE_CFG_RC_NO_ERR;
237 }
238
239 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
240 {
241         if (nw_descr != NULL) {
242                 nw_descr->nw_id = 0;
243                 INIT_LIST_HEAD(&nw_descr->network_on_rule);
244                 INIT_LIST_HEAD(&nw_descr->nw_intflist);
245         }
246 }
247
248 int lustre_lnet_parse_nids(char *nids, char **array, int size,
249                            char ***out_array)
250 {
251         int num_nids = 0;
252         char *comma = nids, *cur, *entry;
253         char **new_array;
254         int i, len, start = 0, finish = 0;
255
256         if (nids == NULL || strlen(nids) == 0)
257                 return size;
258
259         /* count the number or new nids, by counting the number of commas */
260         while (comma) {
261                 comma = strchr(comma, ',');
262                 if (comma) {
263                         comma++;
264                         num_nids++;
265                 } else {
266                         num_nids++;
267                 }
268         }
269
270         /*
271          * if the array is not NULL allocate a large enough array to house
272          * the old and new entries
273          */
274         new_array = calloc(sizeof(char*),
275                            (size > 0) ? size + num_nids : num_nids);
276
277         if (!new_array)
278                 goto failed;
279
280         /* parse our the new nids and add them to the tail of the array */
281         comma = nids;
282         cur = nids;
283         start = (size > 0) ? size: 0;
284         finish = (size > 0) ? size + num_nids : num_nids;
285         for (i = start; i < finish; i++) {
286                 comma = strchr(comma, ',');
287                 if (!comma)
288                         /*
289                          * the length of the string to be parsed out is
290                          * from cur to end of string. So it's good enough
291                          * to strlen(cur)
292                          */
293                         len = strlen(cur) + 1;
294                 else
295                         /* length of the string is comma - cur */
296                         len = (comma - cur) + 1;
297
298                 entry = calloc(1, len);
299                 if (!entry) {
300                         finish = i > 0 ? i - 1: 0;
301                         goto failed;
302                 }
303                 strncpy(entry, cur, len - 1);
304                 entry[len] = '\0';
305                 new_array[i] = entry;
306                 if (comma) {
307                         comma++;
308                         cur = comma;
309                 }
310         }
311
312         /* add the old entries in the array and delete the old array*/
313         for (i = 0; i < size; i++)
314                 new_array[i] = array[i];
315
316         if (array)
317                 free(array);
318
319         *out_array = new_array;
320
321         return finish;
322
323 failed:
324         for (i = start; i < finish; i++)
325                 free(new_array[i]);
326         if (new_array)
327                 free(new_array);
328
329         return size;
330 }
331
332 /*
333  * format expected:
334  *      <intf>[<expr>], <intf>[<expr>],..
335  */
336 int lustre_lnet_parse_interfaces(char *intf_str,
337                                  struct lnet_dlc_network_descr *nw_descr)
338 {
339         char *open_square;
340         char *close_square;
341         char *comma;
342         char *cur = intf_str, *next = NULL;
343         char *end = intf_str + strlen(intf_str);
344         int rc, len;
345         struct lnet_dlc_intf_descr *intf_descr, *tmp;
346
347         if (nw_descr == NULL)
348                 return LUSTRE_CFG_RC_BAD_PARAM;
349
350         while (cur < end) {
351                 open_square = strchr(cur, '[');
352                 if (open_square != NULL) {
353                         close_square = strchr(cur, ']');
354                         if (close_square == NULL) {
355                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
356                                 goto failed;
357                         }
358
359                         comma = strchr(cur, ',');
360                         if (comma != NULL && comma > close_square) {
361                                 next = comma + 1;
362                                 len = next - close_square;
363                         } else {
364                                 len = strlen(cur);
365                                 next = cur + len;
366                         }
367                 } else {
368                         comma = strchr(cur, ',');
369                         if (comma != NULL) {
370                                 next = comma + 1;
371                                 len = comma - cur;
372                         } else {
373                                 len = strlen(cur);
374                                 next = cur + len;
375                         }
376                 }
377
378                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
379                 if (rc != LUSTRE_CFG_RC_NO_ERR)
380                         goto failed;
381
382                 cur = next;
383         }
384
385         return LUSTRE_CFG_RC_NO_ERR;
386
387 failed:
388         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
389                                  intf_on_network) {
390                 list_del(&intf_descr->intf_on_network);
391                 free_intf_descr(intf_descr);
392         }
393
394         return rc;
395 }
396
397 int lustre_lnet_config_lib_init(void)
398 {
399         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
400 }
401
402 void lustre_lnet_config_lib_uninit(void)
403 {
404         unregister_ioc_dev(LNET_DEV_ID);
405 }
406
407 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
408                                  int seq_no, struct cYAML **err_rc)
409 {
410         struct libcfs_ioctl_data data;
411         unsigned int opc;
412         int rc;
413         char err_str[LNET_MAX_STR_LEN];
414
415         snprintf(err_str, sizeof(err_str), "\"Success\"");
416
417         LIBCFS_IOC_INIT(data);
418
419         /* Reverse logic is used here in order not to change
420          * the lctl utility */
421         data.ioc_flags = load_ni_from_mod ? 0 : 1;
422
423         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
424
425         rc = l_ioctl(LNET_DEV_ID, opc, &data);
426         if (rc != 0) {
427                 snprintf(err_str,
428                         sizeof(err_str),
429                         "\"LNet %s error: %s\"", (up) ? "configure" :
430                         "unconfigure", strerror(errno));
431                 rc = -errno;
432         }
433
434         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
435                           "lnet", err_str, err_rc);
436
437         return rc;
438 }
439
440 static lnet_nid_t *allocate_create_nid_array(char **nids, __u32 num_nids,
441                                              char *err_str)
442 {
443         lnet_nid_t *array = NULL;
444         __u32 i;
445
446         if (!nids || num_nids == 0) {
447                 snprintf(err_str, LNET_MAX_STR_LEN, "no NIDs to add");
448                 return NULL;
449         }
450
451         array = calloc(sizeof(*array) * num_nids, 1);
452         if (array == NULL) {
453                 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
454                 return NULL;
455         }
456
457         for (i = 0; i < num_nids; i++) {
458                 array[i] = libcfs_str2nid(nids[i]);
459                 if (array[i] == LNET_NID_ANY) {
460                         free(array);
461                         snprintf(err_str, LNET_MAX_STR_LEN,
462                                  "bad NID: '%s'",
463                                  nids[i]);
464                         return NULL;
465                 }
466         }
467
468         return array;
469 }
470
471 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
472                                 struct lnet_ioctl_peer_cfg *data,
473                                 char *err_str, char *cmd_str)
474 {
475         int rc;
476
477         data->prcfg_prim_nid = pnid;
478         data->prcfg_cfg_nid = nid;
479
480         rc = l_ioctl(LNET_DEV_ID, cmd, data);
481         if (rc != 0) {
482                 rc = -errno;
483                 snprintf(err_str,
484                         LNET_MAX_STR_LEN,
485                         "\"cannot %s peer ni: %s\"",
486                         (cmd_str) ? cmd_str : "add", strerror(errno));
487         }
488
489         return rc;
490 }
491
492 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
493                           int seq_no, struct cYAML **show_rc,
494                           struct cYAML **err_rc)
495 {
496         void *data = NULL;
497         struct lnet_ioctl_ping_data ping;
498         struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
499                      *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
500         lnet_process_id_t id;
501         char err_str[LNET_MAX_STR_LEN] = {0};
502         char *sep, *token, *end;
503         char buf[6];
504         size_t len;
505         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
506         int i;
507         bool flag = false;
508
509         len = (sizeof(lnet_process_id_t) * LNET_INTERFACES_MAX_DEFAULT);
510
511         data = calloc(1, len);
512         if (data == NULL)
513                 goto out;
514
515         /* create struct cYAML root object */
516         root = cYAML_create_object(NULL, NULL);
517         if (root == NULL)
518                 goto out;
519
520         ping_node = cYAML_create_seq(root, oper);
521         if (ping_node == NULL)
522                 goto out;
523
524         /* tokenise each nid in string ping_nids */
525         token = strtok(ping_nids, ",");
526
527         do {
528                 item = cYAML_create_seq_item(ping_node);
529                 if (item == NULL)
530                         goto out;
531
532                 if (first_seq == NULL)
533                         first_seq = item;
534
535                 /* check if '-' is a part of NID, token */
536                 sep = strchr(token, '-');
537                 if (sep == NULL) {
538                         id.pid = LNET_PID_ANY;
539                         /* if no net is specified, libcfs_str2nid() will assume tcp */
540                         id.nid = libcfs_str2nid(token);
541                         if (id.nid == LNET_NID_ANY) {
542                                 snprintf(err_str, sizeof(err_str),
543                                          "\"cannot parse NID '%s'\"",
544                                          token);
545                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
546                                 cYAML_build_error(rc, seq_no, MANAGE_CMD,
547                                                   oper, err_str, err_rc);
548                                 continue;
549                         }
550                 } else {
551                         if (token[0] == 'u' || token[0] == 'U')
552                                 id.pid = (strtoul(&token[1], &end, 0) |
553                                           (LNET_PID_USERFLAG));
554                         else
555                                 id.pid = strtoul(token, &end, 0);
556
557                         /* assuming '-' is part of hostname */
558                         if (end != sep) {
559                                 id.pid = LNET_PID_ANY;
560                                 id.nid = libcfs_str2nid(token);
561                                 if (id.nid == LNET_NID_ANY) {
562                                         snprintf(err_str, sizeof(err_str),
563                                                  "\"cannot parse NID '%s'\"",
564                                                  token);
565                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
566                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
567                                                           oper, err_str,
568                                                           err_rc);
569                                         continue;
570                                 }
571                         } else {
572                                 id.nid = libcfs_str2nid(sep + 1);
573                                 if (id.nid == LNET_NID_ANY) {
574                                         snprintf(err_str, sizeof(err_str),
575                                                  "\"cannot parse NID '%s'\"",
576                                                  token);
577                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
578                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
579                                                           oper, err_str,
580                                                           err_rc);
581                                         continue;
582                                 }
583                         }
584                 }
585                 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
586                 ping.ping_hdr.ioc_len = sizeof(ping);
587                 ping.ping_id          = id;
588                 ping.op_param         = param;
589                 ping.ping_count       = LNET_INTERFACES_MAX_DEFAULT;
590                 ping.ping_buf         = data;
591
592                 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
593                 if (rc != 0) {
594                         snprintf(err_str,
595                                  sizeof(err_str), "failed to %s %s: %s\n", oper,
596                                  id.pid == LNET_PID_ANY ?
597                                  libcfs_nid2str(id.nid) :
598                                  libcfs_id2str(id), strerror(errno));
599                         rc = LUSTRE_CFG_RC_BAD_PARAM;
600                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
601                                           oper, err_str, err_rc);
602                         continue;
603                 }
604
605                 if (cYAML_create_string(item, "primary nid",
606                                         libcfs_nid2str(ping.ping_id.nid)) == NULL)
607                         goto out;
608
609                 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
610                                         "True" : "False") == NULL)
611                         goto out;
612
613                 tmp = cYAML_create_seq(item, "peer ni");
614                 if (tmp == NULL)
615                         goto out;
616
617                 for (i = 0; i < ping.ping_count; i++) {
618                         if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
619                                     "0@lo"))
620                                 continue;
621                         peer_ni = cYAML_create_seq_item(tmp);
622                         if (peer_ni == NULL)
623                                 goto out;
624                         memset(buf, 0, sizeof buf);
625                         snprintf(buf, sizeof buf, "nid");
626                         if (cYAML_create_string(peer_ni, buf,
627                                                 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
628                                 goto out;
629                 }
630
631                 flag = true;
632
633         } while ((token = strtok(NULL, ",")) != NULL);
634
635         if (flag)
636                 rc = LUSTRE_CFG_RC_NO_ERR;
637
638 out:
639         if (data)
640                 free(data);
641         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
642                 cYAML_free_tree(root);
643         } else if (show_rc != NULL && *show_rc != NULL) {
644                 struct cYAML *show_node;
645                 show_node = cYAML_get_object_item(*show_rc, oper);
646                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
647                         cYAML_insert_child(show_node, first_seq);
648                         free(ping_node);
649                         free(root);
650                 } else if (show_node == NULL) {
651                         cYAML_insert_sibling((*show_rc)->cy_child,
652                                              ping_node);
653                         free(root);
654                 } else {
655                         cYAML_free_tree(root);
656                 }
657         } else {
658                 *show_rc = root;
659         }
660
661         return rc;
662 }
663
664 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
665                          struct cYAML **show_rc, struct cYAML **err_rc)
666 {
667         int rc;
668
669         rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
670                             seq_no, show_rc, err_rc);
671         return rc;
672 }
673
674 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
675                          struct cYAML **show_rc, struct cYAML **err_rc)
676 {
677         int rc;
678
679         rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
680                             seq_no, show_rc, err_rc);
681         return rc;
682 }
683
684 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
685                                 bool mr, int seq_no, struct cYAML **err_rc)
686 {
687         struct lnet_ioctl_peer_cfg data;
688         lnet_nid_t prim_nid = LNET_NID_ANY;
689         int rc = LUSTRE_CFG_RC_NO_ERR;
690         int idx = 0;
691         bool nid0_used = false;
692         char err_str[LNET_MAX_STR_LEN] = {0};
693         lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
694
695         if (pnid) {
696                 prim_nid = libcfs_str2nid(pnid);
697                 if (prim_nid == LNET_NID_ANY) {
698                         snprintf(err_str, sizeof(err_str),
699                                  "bad key NID: '%s'",
700                                  pnid);
701                         rc = LUSTRE_CFG_RC_MISSING_PARAM;
702                         goto out;
703                 }
704         } else if (!nids || nids[0] == LNET_NID_ANY) {
705                 snprintf(err_str, sizeof(err_str),
706                          "no NIDs provided for configuration");
707                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
708                 goto out;
709         } else {
710                 prim_nid = LNET_NID_ANY;
711         }
712
713         snprintf(err_str, sizeof(err_str), "\"Success\"");
714
715         LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
716         data.prcfg_mr = mr;
717
718         /*
719          * if prim_nid is not specified use the first nid in the list of
720          * nids provided as the prim_nid. NOTE: on entering 'if' we must
721          * have at least 1 NID
722          */
723         if (prim_nid == LNET_NID_ANY) {
724                 nid0_used = true;
725                 prim_nid = nids[0];
726         }
727
728         /* Create the prim_nid first */
729         rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
730                                   IOC_LIBCFS_ADD_PEER_NI,
731                                   &data, err_str, "add");
732
733         if (rc != 0)
734                 goto out;
735
736         /* add the rest of the nids to the key nid if any are available */
737         for (idx = nid0_used ? 1 : 0 ; nids && idx < num_nids; idx++) {
738                 /*
739                  * If prim_nid is not provided then the first nid in the
740                  * list becomes the prim_nid. First time round the loop use
741                  * LNET_NID_ANY for the first parameter, then use nid[0]
742                  * as the key nid after wards
743                  */
744                 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
745                                           IOC_LIBCFS_ADD_PEER_NI, &data,
746                                           err_str, "add");
747
748                 if (rc != 0)
749                         goto out;
750         }
751
752 out:
753         if (nids != NULL)
754                 free(nids);
755         cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
756         return rc;
757 }
758
759 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
760                              int seq_no, struct cYAML **err_rc)
761 {
762         struct lnet_ioctl_peer_cfg data;
763         lnet_nid_t prim_nid;
764         int rc = LUSTRE_CFG_RC_NO_ERR;
765         int idx = 0;
766         char err_str[LNET_MAX_STR_LEN] = {0};
767         lnet_nid_t *nids = allocate_create_nid_array(nid, num_nids, err_str);
768
769         if (pnid == NULL) {
770                 snprintf(err_str, sizeof(err_str),
771                          "\"Primary nid is not provided\"");
772                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
773                 goto out;
774         } else {
775                 prim_nid = libcfs_str2nid(pnid);
776                 if (prim_nid == LNET_NID_ANY) {
777                         rc = LUSTRE_CFG_RC_BAD_PARAM;
778                         snprintf(err_str, sizeof(err_str),
779                                  "bad key NID: '%s'",
780                                  pnid);
781                         goto out;
782                 }
783         }
784
785         snprintf(err_str, sizeof(err_str), "\"Success\"");
786
787         LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
788         if (!nids || nids[0] == LNET_NID_ANY) {
789                 rc = dispatch_peer_ni_cmd(prim_nid, LNET_NID_ANY,
790                                           IOC_LIBCFS_DEL_PEER_NI,
791                                           &data, err_str, "del");
792                 goto out;
793         }
794
795         for (idx = 0; nids && idx < num_nids; idx++) {
796                 rc = dispatch_peer_ni_cmd(prim_nid, nids[idx],
797                                           IOC_LIBCFS_DEL_PEER_NI, &data,
798                                           err_str, "del");
799
800                 if (rc != 0)
801                         goto out;
802         }
803
804 out:
805         if (nids != NULL)
806                 free(nids);
807         cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
808         return rc;
809 }
810
811 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
812                              int seq_no, struct cYAML **err_rc)
813 {
814         struct lnet_ioctl_config_data data;
815         lnet_nid_t gateway_nid;
816         int rc = LUSTRE_CFG_RC_NO_ERR;
817         __u32 net = LNET_NIDNET(LNET_NID_ANY);
818         char err_str[LNET_MAX_STR_LEN];
819
820         snprintf(err_str, sizeof(err_str), "\"Success\"");
821
822         if (nw == NULL || gw == NULL) {
823                 snprintf(err_str,
824                          sizeof(err_str),
825                          "\"missing mandatory parameter(s): '%s'\"",
826                          (nw == NULL && gw == NULL) ? "network, gateway" :
827                          (nw == NULL) ? "network" : "gateway");
828                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
829                 goto out;
830         }
831
832         net = libcfs_str2net(nw);
833         if (net == LNET_NIDNET(LNET_NID_ANY)) {
834                 snprintf(err_str,
835                          sizeof(err_str),
836                          "\"cannot parse net %s\"", nw);
837                 rc = LUSTRE_CFG_RC_BAD_PARAM;
838                 goto out;
839         }
840
841         gateway_nid = libcfs_str2nid(gw);
842         if (gateway_nid == LNET_NID_ANY) {
843                 snprintf(err_str,
844                         sizeof(err_str),
845                         "\"cannot parse gateway NID '%s'\"", gw);
846                 rc = LUSTRE_CFG_RC_BAD_PARAM;
847                 goto out;
848         }
849
850         if (hops == -1) {
851                 /* hops is undefined */
852                 hops = LNET_UNDEFINED_HOPS;
853         } else if (hops < 1 || hops > 255) {
854                 snprintf(err_str,
855                         sizeof(err_str),
856                         "\"invalid hop count %d, must be between 1 and 255\"",
857                         hops);
858                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
859                 goto out;
860         }
861
862         if (prio == -1) {
863                 prio = 0;
864         } else if (prio < 0) {
865                 snprintf(err_str,
866                          sizeof(err_str),
867                         "\"invalid priority %d, must be greater than 0\"",
868                         prio);
869                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
870                 goto out;
871         }
872
873         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
874         data.cfg_net = net;
875         data.cfg_config_u.cfg_route.rtr_hop = hops;
876         data.cfg_config_u.cfg_route.rtr_priority = prio;
877         data.cfg_nid = gateway_nid;
878
879         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
880         if (rc != 0) {
881                 rc = -errno;
882                 snprintf(err_str,
883                          sizeof(err_str),
884                          "\"cannot add route: %s\"", strerror(errno));
885                 goto out;
886         }
887
888 out:
889         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
890
891         return rc;
892 }
893
894 int lustre_lnet_del_route(char *nw, char *gw,
895                           int seq_no, struct cYAML **err_rc)
896 {
897         struct lnet_ioctl_config_data data;
898         lnet_nid_t gateway_nid;
899         int rc = LUSTRE_CFG_RC_NO_ERR;
900         __u32 net = LNET_NIDNET(LNET_NID_ANY);
901         char err_str[LNET_MAX_STR_LEN];
902
903         snprintf(err_str, sizeof(err_str), "\"Success\"");
904
905         if (nw == NULL || gw == NULL) {
906                 snprintf(err_str,
907                          sizeof(err_str),
908                          "\"missing mandatory parameter(s): '%s'\"",
909                          (nw == NULL && gw == NULL) ? "network, gateway" :
910                          (nw == NULL) ? "network" : "gateway");
911                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
912                 goto out;
913         }
914
915         net = libcfs_str2net(nw);
916         if (net == LNET_NIDNET(LNET_NID_ANY)) {
917                 snprintf(err_str,
918                          sizeof(err_str),
919                          "\"cannot parse net '%s'\"", nw);
920                 rc = LUSTRE_CFG_RC_BAD_PARAM;
921                 goto out;
922         }
923
924         gateway_nid = libcfs_str2nid(gw);
925         if (gateway_nid == LNET_NID_ANY) {
926                 snprintf(err_str,
927                          sizeof(err_str),
928                          "\"cannot parse gateway NID '%s'\"", gw);
929                 rc = LUSTRE_CFG_RC_BAD_PARAM;
930                 goto out;
931         }
932
933         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
934         data.cfg_net = net;
935         data.cfg_nid = gateway_nid;
936
937         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
938         if (rc != 0) {
939                 rc = -errno;
940                 snprintf(err_str,
941                          sizeof(err_str),
942                          "\"cannot delete route: %s\"", strerror(errno));
943                 goto out;
944         }
945
946 out:
947         cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
948
949         return rc;
950 }
951
952 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
953                            int seq_no, struct cYAML **show_rc,
954                            struct cYAML **err_rc)
955 {
956         struct lnet_ioctl_config_data data;
957         lnet_nid_t gateway_nid;
958         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
959         int l_errno = 0;
960         __u32 net = LNET_NIDNET(LNET_NID_ANY);
961         int i;
962         struct cYAML *root = NULL, *route = NULL, *item = NULL;
963         struct cYAML *first_seq = NULL;
964         char err_str[LNET_MAX_STR_LEN];
965         bool exist = false;
966
967         snprintf(err_str, sizeof(err_str),
968                  "\"out of memory\"");
969
970         if (nw != NULL) {
971                 net = libcfs_str2net(nw);
972                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
973                         snprintf(err_str,
974                                  sizeof(err_str),
975                                  "\"cannot parse net '%s'\"", nw);
976                         rc = LUSTRE_CFG_RC_BAD_PARAM;
977                         goto out;
978                 }
979
980         } else {
981                 /* show all routes without filtering on net */
982                 net = LNET_NIDNET(LNET_NID_ANY);
983         }
984
985         if (gw != NULL) {
986                 gateway_nid = libcfs_str2nid(gw);
987                 if (gateway_nid == LNET_NID_ANY) {
988                         snprintf(err_str,
989                                  sizeof(err_str),
990                                  "\"cannot parse gateway NID '%s'\"", gw);
991                         rc = LUSTRE_CFG_RC_BAD_PARAM;
992                         goto out;
993                 }
994         } else
995                 /* show all routes with out filtering on gateway */
996                 gateway_nid = LNET_NID_ANY;
997
998         if ((hops < 1 && hops != -1) || hops > 255) {
999                 snprintf(err_str,
1000                          sizeof(err_str),
1001                          "\"invalid hop count %d, must be between 0 and 256\"",
1002                          hops);
1003                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1004                 goto out;
1005         }
1006
1007         /* create struct cYAML root object */
1008         root = cYAML_create_object(NULL, NULL);
1009         if (root == NULL)
1010                 goto out;
1011
1012         route = cYAML_create_seq(root, "route");
1013         if (route == NULL)
1014                 goto out;
1015
1016         for (i = 0;; i++) {
1017                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1018                 data.cfg_count = i;
1019
1020                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1021                 if (rc != 0) {
1022                         l_errno = errno;
1023                         break;
1024                 }
1025
1026                 /* filter on provided data */
1027                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1028                     net != data.cfg_net)
1029                         continue;
1030
1031                 if (gateway_nid != LNET_NID_ANY &&
1032                     gateway_nid != data.cfg_nid)
1033                         continue;
1034
1035                 if (hops != -1 &&
1036                     hops != data.cfg_config_u.cfg_route.rtr_hop)
1037                         continue;
1038
1039                 if (prio != -1 &&
1040                     prio != data.cfg_config_u.cfg_route.rtr_priority)
1041                         continue;
1042
1043                 /* default rc to -1 incase we hit the goto */
1044                 rc = -1;
1045                 exist = true;
1046
1047                 item = cYAML_create_seq_item(route);
1048                 if (item == NULL)
1049                         goto out;
1050
1051                 if (first_seq == NULL)
1052                         first_seq = item;
1053
1054                 if (cYAML_create_string(item, "net",
1055                                         libcfs_net2str(data.cfg_net)) == NULL)
1056                         goto out;
1057
1058                 if (cYAML_create_string(item, "gateway",
1059                                         libcfs_nid2str(data.cfg_nid)) == NULL)
1060                         goto out;
1061
1062                 if (detail) {
1063                         if (cYAML_create_number(item, "hop",
1064                                                 (int) data.cfg_config_u.
1065                                                 cfg_route.rtr_hop) ==
1066                             NULL)
1067                                 goto out;
1068
1069                         if (cYAML_create_number(item, "priority",
1070                                                 data.cfg_config_u.
1071                                                 cfg_route.rtr_priority) == NULL)
1072                                 goto out;
1073
1074                         if (cYAML_create_string(item, "state",
1075                                                 data.cfg_config_u.cfg_route.
1076                                                         rtr_flags ?
1077                                                 "up" : "down") == NULL)
1078                                 goto out;
1079                 }
1080         }
1081
1082         /* print output iff show_rc is not provided */
1083         if (show_rc == NULL)
1084                 cYAML_print_tree(root);
1085
1086         if (l_errno != ENOENT) {
1087                 snprintf(err_str,
1088                          sizeof(err_str),
1089                          "\"cannot get routes: %s\"",
1090                          strerror(l_errno));
1091                 rc = -l_errno;
1092                 goto out;
1093         } else
1094                 rc = LUSTRE_CFG_RC_NO_ERR;
1095
1096         snprintf(err_str, sizeof(err_str), "\"success\"");
1097 out:
1098         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1099                 cYAML_free_tree(root);
1100         } else if (show_rc != NULL && *show_rc != NULL) {
1101                 struct cYAML *show_node;
1102                 /* find the route node, if one doesn't exist then
1103                  * insert one.  Otherwise add to the one there
1104                  */
1105                 show_node = cYAML_get_object_item(*show_rc, "route");
1106                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1107                         cYAML_insert_child(show_node, first_seq);
1108                         free(route);
1109                         free(root);
1110                 } else if (show_node == NULL) {
1111                         cYAML_insert_sibling((*show_rc)->cy_child,
1112                                                 route);
1113                         free(root);
1114                 } else {
1115                         cYAML_free_tree(root);
1116                 }
1117         } else {
1118                 *show_rc = root;
1119         }
1120
1121         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1122
1123         return rc;
1124 }
1125
1126 static int socket_intf_query(int request, char *intf,
1127                              struct ifreq *ifr)
1128 {
1129         int rc = 0;
1130         int sockfd;
1131
1132         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1133                 return LUSTRE_CFG_RC_BAD_PARAM;
1134
1135         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1136         if (sockfd < 0)
1137                 return LUSTRE_CFG_RC_BAD_PARAM;
1138
1139         strcpy(ifr->ifr_name, intf);
1140         rc = ioctl(sockfd, request, ifr);
1141         if (rc != 0)
1142                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1143
1144         close(sockfd);
1145
1146         return rc;
1147 }
1148
1149 /*
1150  * for each interface in the array of interfaces find the IP address of
1151  * that interface, create its nid and add it to an array of NIDs.
1152  * Stop if any of the interfaces is down
1153  */
1154 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1155                                  lnet_nid_t **nids, __u32 *nnids)
1156 {
1157         int i = 0, count = 0, rc;
1158         struct ifreq ifr;
1159         __u32 ip;
1160         struct lnet_dlc_intf_descr *intf;
1161
1162         if (nw == NULL || nids == NULL)
1163                 return LUSTRE_CFG_RC_BAD_PARAM;
1164
1165         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1166                 count++;
1167
1168         *nids = calloc(count, sizeof(lnet_nid_t));
1169         if (*nids == NULL)
1170                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1171
1172         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1173                 memset(&ifr, 0, sizeof(ifr));
1174                 rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1175                 if (rc != 0)
1176                         goto failed;
1177
1178                 if ((ifr.ifr_flags & IFF_UP) == 0) {
1179                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1180                         goto failed;
1181                 }
1182
1183                 memset(&ifr, 0, sizeof(ifr));
1184                 rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1185                 if (rc != 0)
1186                         goto failed;
1187
1188                 ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1189                 ip = bswap_32(ip);
1190                 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1191                 i++;
1192         }
1193
1194         *nnids = count;
1195
1196         return 0;
1197
1198 failed:
1199         free(*nids);
1200         *nids = NULL;
1201         return rc;
1202 }
1203
1204 /*
1205  * called repeatedly until a match or no more ip range
1206  * What do you have?
1207  *      ip_range expression
1208  *      interface list with all the interface names.
1209  *      all the interfaces in the system.
1210  *
1211  *      try to match the ip_range expr to one of the interfaces' IPs in
1212  *      the system. If we hit a patch for an interface. Check if that
1213  *      interface name is in the list.
1214  *
1215  *      If there are more than one interface in the list, then make sure
1216  *      that the IPs for all of these interfaces match the ip ranges
1217  *      given.
1218  *
1219  *      for each interface in intf_list
1220  *              look up the intf name in ifa
1221  *              if not there then no match
1222  *              check ip obtained from ifa against a match to any of the
1223  *              ip_ranges given.
1224  *              If no match, then fail
1225  *
1226  *      The result is that all the interfaces have to match.
1227  */
1228 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1229                                  struct list_head *intf_list,
1230                                  struct list_head *ip_ranges)
1231 {
1232         int rc;
1233         __u32 ip;
1234         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1235         struct ifaddrs *ifaddr = ifa;
1236         struct lustre_lnet_ip_range_descr *ip_range;
1237         int family;
1238
1239         /*
1240          * if there are no explicit interfaces, and no ip ranges, then
1241          * configure the first tcp interface we encounter.
1242          */
1243         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1244                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1245                         if (ifaddr->ifa_addr == NULL)
1246                                 continue;
1247
1248                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1249                                 continue;
1250
1251                         family = ifaddr->ifa_addr->sa_family;
1252                         if (family == AF_INET &&
1253                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1254                                 rc = lustre_lnet_add_intf_descr
1255                                         (intf_list, ifaddr->ifa_name,
1256                                         strlen(ifaddr->ifa_name));
1257
1258                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1259                                         return rc;
1260
1261                                 return LUSTRE_CFG_RC_MATCH;
1262                         }
1263                 }
1264                 return LUSTRE_CFG_RC_NO_MATCH;
1265         }
1266
1267         /*
1268          * First interface which matches an IP pattern will be used
1269          */
1270         if (list_empty(intf_list)) {
1271                 /*
1272                  * no interfaces provided in the rule, but an ip range is
1273                  * provided, so try and match an interface to the ip
1274                  * range.
1275                  */
1276                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1277                         if (ifaddr->ifa_addr == NULL)
1278                                 continue;
1279
1280                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1281                                 continue;
1282
1283                         family = ifaddr->ifa_addr->sa_family;
1284                         if (family == AF_INET) {
1285                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1286                                         sin_addr.s_addr;
1287
1288                                 list_for_each_entry(ip_range, ip_ranges,
1289                                                     ipr_entry) {
1290                                         rc = cfs_ip_addr_match(bswap_32(ip),
1291                                                         &ip_range->ipr_expr);
1292                                         if (!rc)
1293                                                 continue;
1294
1295                                         rc = lustre_lnet_add_intf_descr
1296                                           (intf_list, ifaddr->ifa_name,
1297                                            strlen(ifaddr->ifa_name));
1298
1299                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1300                                                 return rc;
1301                                 }
1302                         }
1303                 }
1304
1305                 if (!list_empty(intf_list))
1306                         return LUSTRE_CFG_RC_MATCH;
1307
1308                 return LUSTRE_CFG_RC_NO_MATCH;
1309         }
1310
1311         /*
1312          * If an interface is explicitly specified the ip-range might or
1313          * might not be specified. if specified the interface needs to match the
1314          * ip-range. If no ip-range then the interfaces are
1315          * automatically matched if they are all up.
1316          * If > 1 interfaces all the interfaces must match for the NI to
1317          * be configured.
1318          */
1319         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1320                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1321                         if (ifaddr->ifa_addr == NULL)
1322                                 continue;
1323
1324                         family = ifaddr->ifa_addr->sa_family;
1325                         if (family == AF_INET &&
1326                             strcmp(intf_descr->intf_name,
1327                                    ifaddr->ifa_name) == 0)
1328                                 break;
1329                 }
1330
1331                 if (ifaddr == NULL) {
1332                         list_del(&intf_descr->intf_on_network);
1333                         free_intf_descr(intf_descr);
1334                         continue;
1335                 }
1336
1337                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1338                         list_del(&intf_descr->intf_on_network);
1339                         free_intf_descr(intf_descr);
1340                         continue;
1341                 }
1342
1343                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1344
1345                 rc = 1;
1346                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1347                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1348                         if (rc)
1349                                 break;
1350                 }
1351
1352                 if (!rc) {
1353                         /* no match for this interface */
1354                         list_del(&intf_descr->intf_on_network);
1355                         free_intf_descr(intf_descr);
1356                 }
1357         }
1358
1359         return LUSTRE_CFG_RC_MATCH;
1360 }
1361
1362 int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1363                                      lnet_nid_t **nids, __u32 *nnids)
1364 {
1365         struct ifaddrs *ifa;
1366         int rc = LUSTRE_CFG_RC_NO_ERR;
1367
1368         rc = getifaddrs(&ifa);
1369         if (rc < 0)
1370                 return -errno;
1371
1372         rc = lustre_lnet_match_ip_to_intf(ifa,
1373                                           &ip2nets->ip2nets_net.nw_intflist,
1374                                           &ip2nets->ip2nets_ip_ranges);
1375         if (rc != LUSTRE_CFG_RC_MATCH) {
1376                 freeifaddrs(ifa);
1377                 return rc;
1378         }
1379
1380         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids);
1381         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1382                 *nids = NULL;
1383                 *nnids = 0;
1384         }
1385
1386         freeifaddrs(ifa);
1387
1388         return rc;
1389 }
1390
1391 static int
1392 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1393                             struct lnet_ioctl_config_lnd_tunables *tunables,
1394                             struct cfs_expr_list *global_cpts,
1395                             lnet_nid_t *nids, char *err_str)
1396 {
1397         char *data;
1398         struct lnet_ioctl_config_ni *conf;
1399         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1400         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1401         size_t len;
1402         int count;
1403         struct lnet_dlc_intf_descr *intf_descr;
1404         __u32 *cpt_array;
1405         struct cfs_expr_list *cpt_expr;
1406
1407         list_for_each_entry(intf_descr, intf_list,
1408                             intf_on_network) {
1409                 if (tunables != NULL)
1410                         len = sizeof(struct lnet_ioctl_config_ni) +
1411                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1412                 else
1413                         len = sizeof(struct lnet_ioctl_config_ni);
1414
1415                 data = calloc(1, len);
1416                 if (!data)
1417                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1418                 conf = (struct lnet_ioctl_config_ni*) data;
1419                 if (tunables != NULL)
1420                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1421                                 conf->lic_bulk;
1422
1423                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1424                 conf->lic_cfg_hdr.ioc_len = len;
1425                 conf->lic_nid = nids[i];
1426                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1427                         LNET_MAX_STR_LEN);
1428
1429                 if (intf_descr->cpt_expr != NULL)
1430                         cpt_expr = intf_descr->cpt_expr;
1431                 else if (global_cpts != NULL)
1432                         cpt_expr = global_cpts;
1433                 else
1434                         cpt_expr = NULL;
1435
1436                 if (cpt_expr != NULL) {
1437                         count = cfs_expr_list_values(cpt_expr,
1438                                                      LNET_MAX_SHOW_NUM_CPT,
1439                                                      &cpt_array);
1440                         if (count > 0) {
1441                                 memcpy(conf->lic_cpts, cpt_array,
1442                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1443                                 free(cpt_array);
1444                         } else {
1445                                 count = 0;
1446                         }
1447                 } else {
1448                         count = 0;
1449                 }
1450
1451                 conf->lic_ncpts = count;
1452
1453                 if (tunables != NULL)
1454                         memcpy(tun, tunables, sizeof(*tunables));
1455
1456                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1457                 if (rc < 0) {
1458                         rc = -errno;
1459                         snprintf(err_str,
1460                                  LNET_MAX_STR_LEN,
1461                                  "\"cannot add network: %s\"", strerror(errno));
1462                         free(data);
1463                         return rc;
1464                 }
1465                 free(data);
1466                 i++;
1467         }
1468
1469         return LUSTRE_CFG_RC_NO_ERR;
1470 }
1471
1472 int
1473 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1474                            struct lnet_ioctl_config_lnd_tunables *tunables,
1475                            struct cfs_expr_list *global_cpts,
1476                            int seq_no, struct cYAML **err_rc)
1477 {
1478         lnet_nid_t *nids = NULL;
1479         __u32 nnids = 0;
1480         int rc;
1481         char err_str[LNET_MAX_STR_LEN];
1482
1483         snprintf(err_str, sizeof(err_str), "\"success\"");
1484
1485         if (!ip2nets) {
1486                 snprintf(err_str,
1487                          sizeof(err_str),
1488                          "\"incomplete ip2nets information\"");
1489                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1490                 goto out;
1491         }
1492
1493         /*
1494          * call below function to resolve the rules into a list of nids.
1495          * The memory is allocated in that function then freed here when
1496          * it's no longer needed.
1497          */
1498         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1499         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1500                 snprintf(err_str,
1501                          sizeof(err_str),
1502                          "\"cannot resolve ip2nets rule\"");
1503                 goto out;
1504         }
1505
1506         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1507                 snprintf(err_str, sizeof(err_str),
1508                          "\"no interfaces match ip2nets rules\"");
1509                 goto free_nids_out;
1510         }
1511
1512         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1513                                          tunables, global_cpts, nids,
1514                                          err_str);
1515
1516 free_nids_out:
1517         free(nids);
1518
1519 out:
1520         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1521         return rc;
1522 }
1523
1524 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1525                           struct cfs_expr_list *global_cpts,
1526                           char *ip2net,
1527                           struct lnet_ioctl_config_lnd_tunables *tunables,
1528                           int seq_no, struct cYAML **err_rc)
1529 {
1530         char *data = NULL;
1531         struct lnet_ioctl_config_ni *conf;
1532         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1533         char buf[LNET_MAX_STR_LEN];
1534         int rc = LUSTRE_CFG_RC_NO_ERR;
1535         char err_str[LNET_MAX_STR_LEN];
1536         lnet_nid_t *nids = NULL;
1537         __u32 nnids = 0;
1538         size_t len;
1539         int count;
1540         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1541         __u32 *cpt_array;
1542
1543         snprintf(err_str, sizeof(err_str), "\"success\"");
1544
1545         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1546             list_empty(&nw_descr->nw_intflist))) {
1547                 snprintf(err_str,
1548                          sizeof(err_str),
1549                          "\"missing mandatory parameters\"");
1550                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1551                 goto out;
1552         }
1553
1554         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1555                 snprintf(err_str,
1556                          sizeof(err_str),
1557                          "\"ip2net string too long %d\"",
1558                                 (int)strlen(ip2net));
1559                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1560                 goto out;
1561         }
1562
1563         if (ip2net != NULL) {
1564                 if (tunables != NULL)
1565                         len = sizeof(struct lnet_ioctl_config_ni) +
1566                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1567                 else
1568                         len = sizeof(struct lnet_ioctl_config_ni);
1569                 data = calloc(1, len);
1570                 if (!data) {
1571                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1572                         goto out;
1573                 }
1574                 conf = (struct lnet_ioctl_config_ni*) data;
1575                 if (tunables != NULL)
1576                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1577                                 (data + sizeof(*conf));
1578
1579                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1580                 conf->lic_cfg_hdr.ioc_len = len;
1581                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1582                         LNET_MAX_STR_LEN);
1583
1584                 if (global_cpts != NULL) {
1585                         count = cfs_expr_list_values(global_cpts,
1586                                                      LNET_MAX_SHOW_NUM_CPT,
1587                                                      &cpt_array);
1588                         if (count > 0) {
1589                                 memcpy(conf->lic_cpts, cpt_array,
1590                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1591                                 free(cpt_array);
1592                         } else {
1593                                 count = 0;
1594                         }
1595                 } else {
1596                         count = 0;
1597                 }
1598
1599                 conf->lic_ncpts = count;
1600
1601                 if (tunables != NULL)
1602                         memcpy(tun, tunables, sizeof(*tunables));
1603
1604                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1605                 if (rc < 0) {
1606                         rc = -errno;
1607                         snprintf(err_str,
1608                                 sizeof(err_str),
1609                                 "\"cannot add network: %s\"", strerror(errno));
1610                         goto out;
1611                 }
1612
1613                 goto out;
1614         }
1615
1616         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1617                 rc = LUSTRE_CFG_RC_NO_ERR;
1618                 goto out;
1619         }
1620
1621         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1622                 snprintf(err_str,
1623                         sizeof(err_str),
1624                         "\"cannot parse net '%s'\"",
1625                         libcfs_net2str(nw_descr->nw_id));
1626                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1627                 goto out;
1628         }
1629
1630         if (list_empty(&nw_descr->nw_intflist)) {
1631                 snprintf(err_str,
1632                         sizeof(err_str),
1633                         "\"no interface name provided\"");
1634                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1635                 goto out;
1636         }
1637
1638         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1639         if (rc != 0) {
1640                 snprintf(err_str, sizeof(err_str),
1641                          "\"bad parameter\"");
1642                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1643                 goto out;
1644         }
1645
1646         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1647                                          tunables, global_cpts, nids,
1648                                          err_str);
1649
1650 out:
1651         if (nw_descr != NULL) {
1652                 list_for_each_entry_safe(intf_descr, tmp,
1653                                          &nw_descr->nw_intflist,
1654                                          intf_on_network) {
1655                         list_del(&intf_descr->intf_on_network);
1656                         free_intf_descr(intf_descr);
1657                 }
1658         }
1659
1660         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1661
1662         if (nids)
1663                 free(nids);
1664
1665         if (data)
1666                 free(data);
1667
1668         return rc;
1669 }
1670
1671 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1672                        int seq_no, struct cYAML **err_rc)
1673 {
1674         struct lnet_ioctl_config_ni data;
1675         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1676         char err_str[LNET_MAX_STR_LEN];
1677         lnet_nid_t *nids = NULL;
1678         __u32 nnids = 0;
1679         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1680
1681         snprintf(err_str, sizeof(err_str), "\"success\"");
1682
1683         if (nw_descr == NULL || nw_descr->nw_id == 0 ||
1684             list_empty(&nw_descr->nw_intflist)) {
1685                 snprintf(err_str,
1686                          sizeof(err_str),
1687                          "\"missing mandatory parameter\"");
1688                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1689                 goto out;
1690         }
1691
1692         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1693                 return LUSTRE_CFG_RC_NO_ERR;
1694
1695         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1696                 snprintf(err_str,
1697                          sizeof(err_str),
1698                          "\"cannot parse net '%s'\"",
1699                          libcfs_net2str(nw_descr->nw_id));
1700                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1701                 goto out;
1702         }
1703
1704         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1705         if (rc != 0) {
1706                 snprintf(err_str, sizeof(err_str),
1707                          "\"bad parameter\"");
1708                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1709                 goto out;
1710         }
1711
1712         /*
1713          * no interfaces just the nw_id is specified
1714          */
1715         if (nnids == 0) {
1716                 nids = calloc(1, sizeof(*nids));
1717                 if (nids == NULL) {
1718                         snprintf(err_str, sizeof(err_str),
1719                                 "\"out of memory\"");
1720                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1721                         goto out;
1722                 }
1723                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1724                 nnids = 1;
1725         }
1726
1727         for (i = 0; i < nnids; i++) {
1728                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1729                 data.lic_nid = nids[i];
1730
1731                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1732                 if (rc < 0) {
1733                         rc = -errno;
1734                         snprintf(err_str,
1735                                 sizeof(err_str),
1736                                 "\"cannot del network: %s\"", strerror(errno));
1737                 }
1738         }
1739
1740         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1741                                  intf_on_network) {
1742                 list_del(&intf_descr->intf_on_network);
1743                 free_intf_descr(intf_descr);
1744         }
1745
1746 out:
1747         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1748
1749         if (nids != NULL)
1750                 free(nids);
1751
1752         return rc;
1753 }
1754
1755 static bool
1756 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1757                           struct lnet_ioctl_comm_count *counts)
1758 {
1759         if (cYAML_create_number(yaml, "put",
1760                                 counts->ico_put_count)
1761                                         == NULL)
1762                 return false;
1763         if (cYAML_create_number(yaml, "get",
1764                                 counts->ico_get_count)
1765                                         == NULL)
1766                 return false;
1767         if (cYAML_create_number(yaml, "reply",
1768                                 counts->ico_reply_count)
1769                                         == NULL)
1770                 return false;
1771         if (cYAML_create_number(yaml, "ack",
1772                                 counts->ico_ack_count)
1773                                         == NULL)
1774                 return false;
1775         if (cYAML_create_number(yaml, "hello",
1776                                 counts->ico_hello_count)
1777                                         == NULL)
1778                 return false;
1779
1780         return true;
1781 }
1782
1783 static struct lnet_ioctl_comm_count *
1784 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1785 {
1786         if (idx == 0)
1787                 return &msg_stats->im_send_stats;
1788         if (idx == 1)
1789                 return &msg_stats->im_recv_stats;
1790         if (idx == 2)
1791                 return &msg_stats->im_drop_stats;
1792
1793         return NULL;
1794 }
1795
1796 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1797                          struct cYAML **show_rc, struct cYAML **err_rc)
1798 {
1799         char *buf;
1800         struct lnet_ioctl_config_ni *ni_data;
1801         struct lnet_ioctl_config_lnd_tunables *lnd;
1802         struct lnet_ioctl_element_stats *stats;
1803         struct lnet_ioctl_element_msg_stats msg_stats;
1804         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1805         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1806         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1807         int l_errno = 0;
1808         struct cYAML *root = NULL, *tunables = NULL,
1809                 *net_node = NULL, *interfaces = NULL,
1810                 *item = NULL, *first_seq = NULL,
1811                 *tmp = NULL, *statistics = NULL;
1812         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1813         char str_buf[str_buf_len];
1814         char *pos;
1815         char err_str[LNET_MAX_STR_LEN];
1816         bool exist = false, new_net = true;
1817         int net_num = 0;
1818         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1819
1820         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1821
1822         buf = calloc(1, buf_size);
1823         if (buf == NULL)
1824                 goto out;
1825
1826         ni_data = (struct lnet_ioctl_config_ni *)buf;
1827
1828         if (nw != NULL) {
1829                 net = libcfs_str2net(nw);
1830                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1831                         snprintf(err_str,
1832                                  sizeof(err_str),
1833                                  "\"cannot parse net '%s'\"", nw);
1834                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1835                         goto out;
1836                 }
1837         }
1838
1839         root = cYAML_create_object(NULL, NULL);
1840         if (root == NULL)
1841                 goto out;
1842
1843         net_node = cYAML_create_seq(root, "net");
1844         if (net_node == NULL)
1845                 goto out;
1846
1847         for (i = 0;; i++) {
1848                 pos = str_buf;
1849                 __u32 rc_net;
1850
1851                 memset(buf, 0, buf_size);
1852
1853                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1854                 /*
1855                  * set the ioc_len to the proper value since INIT assumes
1856                  * size of data
1857                  */
1858                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1859                 ni_data->lic_idx = i;
1860
1861                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1862                 if (rc != 0) {
1863                         l_errno = errno;
1864                         break;
1865                 }
1866
1867                 rc_net = LNET_NIDNET(ni_data->lic_nid);
1868
1869                 /* filter on provided data */
1870                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1871                     net != rc_net)
1872                         continue;
1873
1874                 /* default rc to -1 in case we hit the goto */
1875                 rc = -1;
1876                 exist = true;
1877
1878                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1879                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1880                         (ni_data->lic_bulk + sizeof(*stats));
1881
1882                 if (rc_net != prev_net) {
1883                         prev_net = rc_net;
1884                         new_net = true;
1885                         net_num++;
1886                 }
1887
1888                 if (new_net) {
1889                         if (!cYAML_create_string(net_node, "net type",
1890                                                  libcfs_net2str(rc_net)))
1891                                 goto out;
1892
1893                         tmp = cYAML_create_seq(net_node, "local NI(s)");
1894                         if (tmp == NULL)
1895                                 goto out;
1896                         new_net = false;
1897                 }
1898
1899                 /* create the tree to be printed. */
1900                 item = cYAML_create_seq_item(tmp);
1901                 if (item == NULL)
1902                         goto out;
1903
1904                 if (first_seq == NULL)
1905                         first_seq = item;
1906
1907                 if (cYAML_create_string(item, "nid",
1908                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
1909                         goto out;
1910
1911                 if (cYAML_create_string(item,
1912                                         "status",
1913                                         (ni_data->lic_status ==
1914                                           LNET_NI_STATUS_UP) ?
1915                                             "up" : "down") == NULL)
1916                         goto out;
1917
1918                 /* don't add interfaces unless there is at least one
1919                  * interface */
1920                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1921                         interfaces = cYAML_create_object(item, "interfaces");
1922                         if (interfaces == NULL)
1923                                 goto out;
1924
1925                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1926                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1927                                         snprintf(str_buf,
1928                                                  sizeof(str_buf), "%d", j);
1929                                         if (cYAML_create_string(interfaces,
1930                                                 str_buf,
1931                                                 ni_data->lic_ni_intf[j]) ==
1932                                                         NULL)
1933                                                 goto out;
1934                                 }
1935                         }
1936                 }
1937
1938                 if (detail) {
1939                         char *limit;
1940                         int k;
1941
1942                         statistics = cYAML_create_object(item, "statistics");
1943                         if (statistics == NULL)
1944                                 goto out;
1945
1946                         if (cYAML_create_number(statistics, "send_count",
1947                                                 stats->iel_send_count)
1948                                                         == NULL)
1949                                 goto out;
1950
1951                         if (cYAML_create_number(statistics, "recv_count",
1952                                                 stats->iel_recv_count)
1953                                                         == NULL)
1954                                 goto out;
1955
1956                         if (cYAML_create_number(statistics, "drop_count",
1957                                                 stats->iel_drop_count)
1958                                                         == NULL)
1959                                 goto out;
1960
1961                         if (detail < 2)
1962                                 goto continue_without_msg_stats;
1963
1964                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
1965                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
1966                         msg_stats.im_idx = i;
1967
1968                         rc = l_ioctl(LNET_DEV_ID,
1969                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
1970                                      &msg_stats);
1971                         if (rc != 0) {
1972                                 l_errno = errno;
1973                                 goto continue_without_msg_stats;
1974                         }
1975
1976                         for (k = 0; k < 3; k++) {
1977                                 struct lnet_ioctl_comm_count *counts;
1978                                 struct cYAML *msg_statistics = NULL;
1979
1980                                 msg_statistics = cYAML_create_object(item,
1981                                                  (char *)gmsg_stat_names[k]);
1982                                 if (msg_statistics == NULL)
1983                                         goto out;
1984
1985                                 counts = get_counts(&msg_stats, k);
1986                                 if (counts == NULL)
1987                                         goto out;
1988
1989                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
1990                                                                counts))
1991                                         goto out;
1992                         }
1993
1994 continue_without_msg_stats:
1995                         tunables = cYAML_create_object(item, "tunables");
1996                         if (!tunables)
1997                                 goto out;
1998
1999                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2000                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2001                                 goto out;
2002
2003                         tunables = cYAML_create_object(item, "lnd tunables");
2004                         if (tunables == NULL)
2005                                 goto out;
2006
2007                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2008                                                      &lnd->lt_tun);
2009                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2010                                 goto out;
2011
2012                         if (cYAML_create_number(item, "tcp bonding",
2013                                                 ni_data->lic_tcp_bonding)
2014                                                         == NULL)
2015                                 goto out;
2016
2017                         if (cYAML_create_number(item, "dev cpt",
2018                                                 ni_data->lic_dev_cpt) == NULL)
2019                                 goto out;
2020
2021                         /* out put the CPTs in the format: "[x,x,x,...]" */
2022                         limit = str_buf + str_buf_len - 3;
2023                         pos += snprintf(pos, limit - pos, "\"[");
2024                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2025                                 j < ni_data->lic_ncpts &&
2026                                 pos < limit; j++) {
2027                                 pos += snprintf(pos, limit - pos,
2028                                                 "%d", ni_data->lic_cpts[j]);
2029                                 if ((j + 1) < ni_data->lic_ncpts)
2030                                         pos += snprintf(pos, limit - pos, ",");
2031                         }
2032                         pos += snprintf(pos, 3, "]\"");
2033
2034                         if (ni_data->lic_ncpts >= 1 &&
2035                             cYAML_create_string(item, "CPT",
2036                                                 str_buf) == NULL)
2037                                 goto out;
2038                 }
2039         }
2040
2041         /* Print out the net information only if show_rc is not provided */
2042         if (show_rc == NULL)
2043                 cYAML_print_tree(root);
2044
2045         if (l_errno != ENOENT) {
2046                 snprintf(err_str,
2047                          sizeof(err_str),
2048                          "\"cannot get networks: %s\"",
2049                          strerror(l_errno));
2050                 rc = -l_errno;
2051                 goto out;
2052         } else
2053                 rc = LUSTRE_CFG_RC_NO_ERR;
2054
2055         snprintf(err_str, sizeof(err_str), "\"success\"");
2056 out:
2057         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2058                 cYAML_free_tree(root);
2059         } else if (show_rc != NULL && *show_rc != NULL) {
2060                 struct cYAML *show_node;
2061                 /* find the net node, if one doesn't exist
2062                  * then insert one.  Otherwise add to the one there
2063                  */
2064                 show_node = cYAML_get_object_item(*show_rc, "net");
2065                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2066                         cYAML_insert_child(show_node, first_seq);
2067                         free(net_node);
2068                         free(root);
2069                 } else if (show_node == NULL) {
2070                         cYAML_insert_sibling((*show_rc)->cy_child,
2071                                                 net_node);
2072                         free(root);
2073                 } else {
2074                         cYAML_free_tree(root);
2075                 }
2076         } else {
2077                 *show_rc = root;
2078         }
2079
2080         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2081
2082         return rc;
2083 }
2084
2085 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2086 {
2087         struct lnet_ioctl_config_data data;
2088         int rc = LUSTRE_CFG_RC_NO_ERR;
2089         char err_str[LNET_MAX_STR_LEN];
2090
2091         snprintf(err_str, sizeof(err_str), "\"success\"");
2092
2093         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2094         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2095
2096         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2097         if (rc != 0) {
2098                 rc = -errno;
2099                 snprintf(err_str,
2100                          sizeof(err_str),
2101                          "\"cannot %s routing %s\"",
2102                          (enable) ? "enable" : "disable", strerror(errno));
2103                 goto out;
2104         }
2105
2106 out:
2107         cYAML_build_error(rc, seq_no,
2108                          (enable) ? ADD_CMD : DEL_CMD,
2109                          "routing", err_str, err_rc);
2110
2111         return rc;
2112 }
2113
2114 int ioctl_set_value(__u32 val, int ioc, char *name,
2115                     int seq_no, struct cYAML **err_rc)
2116 {
2117         struct lnet_ioctl_set_value data;
2118         int rc = LUSTRE_CFG_RC_NO_ERR;
2119         char err_str[LNET_MAX_STR_LEN];
2120
2121         snprintf(err_str, sizeof(err_str), "\"success\"");
2122
2123         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2124         data.sv_value = val;
2125
2126         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2127         if (rc != 0) {
2128                 rc = -errno;
2129                 snprintf(err_str,
2130                          sizeof(err_str),
2131                          "\"cannot configure %s to %d: %s\"", name,
2132                          val, strerror(errno));
2133         }
2134
2135         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2136
2137         return rc;
2138 }
2139
2140 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2141 {
2142         int rc = LUSTRE_CFG_RC_NO_ERR;
2143         char err_str[LNET_MAX_STR_LEN];
2144         char val[LNET_MAX_STR_LEN];
2145
2146         snprintf(err_str, sizeof(err_str), "\"success\"");
2147
2148         snprintf(val, sizeof(val), "%d", max);
2149
2150         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2151                               1, strlen(val) + 1);
2152         if (rc)
2153                 snprintf(err_str, sizeof(err_str),
2154                          "\"cannot configure max interfaces: %s\"",
2155                          strerror(errno));
2156
2157         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2158
2159         return rc;
2160 }
2161
2162 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2163 {
2164         int rc = LUSTRE_CFG_RC_NO_ERR;
2165         char err_str[LNET_MAX_STR_LEN];
2166         char val[LNET_MAX_STR_LEN];
2167
2168         snprintf(err_str, sizeof(err_str), "\"success\"");
2169
2170         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2171
2172         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2173                               1, strlen(val) + 1);
2174         if (rc)
2175                 snprintf(err_str, sizeof(err_str),
2176                          "\"cannot configure discovery: %s\"",
2177                          strerror(errno));
2178
2179         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2180
2181         return rc;
2182
2183 }
2184
2185 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2186 {
2187         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2188                                "numa_range", seq_no, err_rc);
2189 }
2190
2191 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2192                                struct cYAML **err_rc)
2193 {
2194         struct lnet_ioctl_config_data data;
2195         int rc = LUSTRE_CFG_RC_NO_ERR;
2196         char err_str[LNET_MAX_STR_LEN];
2197
2198         snprintf(err_str, sizeof(err_str), "\"success\"");
2199
2200         /* -1 indicates to ignore changes to this field */
2201         if (tiny < -1 || small < -1 || large < -1) {
2202                 snprintf(err_str,
2203                          sizeof(err_str),
2204                          "\"tiny, small and large must be >= 0\"");
2205                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2206                 goto out;
2207         }
2208
2209         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2210         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2211         data.cfg_config_u.cfg_buffers.buf_small = small;
2212         data.cfg_config_u.cfg_buffers.buf_large = large;
2213
2214         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2215         if (rc != 0) {
2216                 rc = -errno;
2217                 snprintf(err_str,
2218                          sizeof(err_str),
2219                          "\"cannot configure buffers: %s\"", strerror(errno));
2220                 goto out;
2221         }
2222
2223 out:
2224         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2225
2226         return rc;
2227 }
2228
2229 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2230                              struct cYAML **err_rc)
2231 {
2232         struct lnet_ioctl_config_data *data;
2233         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2234         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2235         int l_errno = 0;
2236         char *buf;
2237         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2238         int buf_count[LNET_NRBPOOLS] = {0};
2239         struct cYAML *root = NULL, *pools_node = NULL,
2240                      *type_node = NULL, *item = NULL, *cpt = NULL,
2241                      *first_seq = NULL, *buffers = NULL;
2242         int i, j;
2243         char err_str[LNET_MAX_STR_LEN];
2244         char node_name[LNET_MAX_STR_LEN];
2245         bool exist = false;
2246
2247         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2248
2249         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2250         if (buf == NULL)
2251                 goto out;
2252
2253         data = (struct lnet_ioctl_config_data *)buf;
2254
2255         root = cYAML_create_object(NULL, NULL);
2256         if (root == NULL)
2257                 goto out;
2258
2259         pools_node = cYAML_create_seq(root, "routing");
2260         if (pools_node == NULL)
2261                 goto out;
2262
2263         for (i = 0;; i++) {
2264                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2265                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2266                                         sizeof(struct lnet_ioctl_pool_cfg);
2267                 data->cfg_count = i;
2268
2269                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2270                 if (rc != 0) {
2271                         l_errno = errno;
2272                         break;
2273                 }
2274
2275                 exist = true;
2276
2277                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2278
2279                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2280                 item = cYAML_create_seq_item(pools_node);
2281                 if (item == NULL)
2282                         goto out;
2283
2284                 if (first_seq == NULL)
2285                         first_seq = item;
2286
2287                 cpt = cYAML_create_object(item, node_name);
2288                 if (cpt == NULL)
2289                         goto out;
2290
2291                 /* create the tree  and print */
2292                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2293                         type_node = cYAML_create_object(cpt, pools[j]);
2294                         if (type_node == NULL)
2295                                 goto out;
2296                         if (cYAML_create_number(type_node, "npages",
2297                                                 pool_cfg->pl_pools[j].pl_npages)
2298                             == NULL)
2299                                 goto out;
2300                         if (cYAML_create_number(type_node, "nbuffers",
2301                                                 pool_cfg->pl_pools[j].
2302                                                   pl_nbuffers) == NULL)
2303                                 goto out;
2304                         if (cYAML_create_number(type_node, "credits",
2305                                                 pool_cfg->pl_pools[j].
2306                                                    pl_credits) == NULL)
2307                                 goto out;
2308                         if (cYAML_create_number(type_node, "mincredits",
2309                                                 pool_cfg->pl_pools[j].
2310                                                    pl_mincredits) == NULL)
2311                                 goto out;
2312                         /* keep track of the total count for each of the
2313                          * tiny, small and large buffers */
2314                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2315                 }
2316         }
2317
2318         if (pool_cfg != NULL) {
2319                 item = cYAML_create_seq_item(pools_node);
2320                 if (item == NULL)
2321                         goto out;
2322
2323                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2324                     NULL)
2325                         goto out;
2326         }
2327
2328         /* create a buffers entry in the show. This is necessary so that
2329          * if the YAML output is used to configure a node, the buffer
2330          * configuration takes hold */
2331         buffers = cYAML_create_object(root, "buffers");
2332         if (buffers == NULL)
2333                 goto out;
2334
2335         for (i = 0; i < LNET_NRBPOOLS; i++) {
2336                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2337                         goto out;
2338         }
2339
2340         if (show_rc == NULL)
2341                 cYAML_print_tree(root);
2342
2343         if (l_errno != ENOENT) {
2344                 snprintf(err_str,
2345                          sizeof(err_str),
2346                          "\"cannot get routing information: %s\"",
2347                          strerror(l_errno));
2348                 rc = -l_errno;
2349                 goto out;
2350         } else
2351                 rc = LUSTRE_CFG_RC_NO_ERR;
2352
2353         snprintf(err_str, sizeof(err_str), "\"success\"");
2354         rc = LUSTRE_CFG_RC_NO_ERR;
2355
2356 out:
2357         free(buf);
2358         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2359                 cYAML_free_tree(root);
2360         } else if (show_rc != NULL && *show_rc != NULL) {
2361                 struct cYAML *routing_node;
2362                 /* there should exist only one routing block and one
2363                  * buffers block. If there already exists a previous one
2364                  * then don't add another */
2365                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2366                 if (routing_node == NULL) {
2367                         cYAML_insert_sibling((*show_rc)->cy_child,
2368                                                 root->cy_child);
2369                         free(root);
2370                 } else {
2371                         cYAML_free_tree(root);
2372                 }
2373         } else {
2374                 *show_rc = root;
2375         }
2376
2377         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2378
2379         return rc;
2380 }
2381
2382 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2383                           struct cYAML **show_rc, struct cYAML **err_rc)
2384 {
2385         /*
2386          * TODO: This function is changing in a future patch to accommodate
2387          * PEER_LIST and proper filtering on any nid of the peer
2388          */
2389         struct lnet_ioctl_peer_cfg peer_info;
2390         struct lnet_peer_ni_credit_info *lpni_cri;
2391         struct lnet_ioctl_element_stats *lpni_stats;
2392         struct lnet_ioctl_element_msg_stats *msg_stats;
2393         lnet_nid_t *nidp;
2394         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2395         int i, j, k;
2396         int l_errno = 0;
2397         __u32 count;
2398         __u32 size;
2399         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2400                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2401                      *msg_statistics = NULL, *statistics = NULL;
2402         char err_str[LNET_MAX_STR_LEN];
2403         lnet_process_id_t *list = NULL;
2404         void *data = NULL;
2405         void *lpni_data;
2406
2407         snprintf(err_str, sizeof(err_str),
2408                  "\"out of memory\"");
2409
2410         /* create struct cYAML root object */
2411         root = cYAML_create_object(NULL, NULL);
2412         if (root == NULL)
2413                 goto out;
2414
2415         peer_root = cYAML_create_seq(root, "peer");
2416         if (peer_root == NULL)
2417                 goto out;
2418
2419         count = 1000;
2420         size = count * sizeof(lnet_process_id_t);
2421         list = malloc(size);
2422         if (list == NULL) {
2423                 l_errno = ENOMEM;
2424                 goto out;
2425         }
2426         if (knid != NULL) {
2427                 list[0].nid = libcfs_str2nid(knid);
2428                 count = 1;
2429         } else {
2430                 for (;;) {
2431                         memset(&peer_info, 0, sizeof(peer_info));
2432                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2433                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2434                         peer_info.prcfg_size = size;
2435                         peer_info.prcfg_bulk = list;
2436
2437                         l_errno = 0;
2438                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2439                                      &peer_info);
2440                         count = peer_info.prcfg_count;
2441                         if (rc == 0)
2442                                 break;
2443                         l_errno = errno;
2444                         if (l_errno != E2BIG) {
2445                                 snprintf(err_str,
2446                                         sizeof(err_str),
2447                                         "\"cannot get peer list: %s\"",
2448                                         strerror(l_errno));
2449                                 rc = -l_errno;
2450                                 goto out;
2451                         }
2452                         free(list);
2453                         size = peer_info.prcfg_size;
2454                         list = malloc(size);
2455                         if (list == NULL) {
2456                                 l_errno = ENOMEM;
2457                                 goto out;
2458                         }
2459                 }
2460         }
2461
2462         size = 4096;
2463         data = malloc(size);
2464         if (data == NULL) {
2465                 l_errno = ENOMEM;
2466                 goto out;
2467         }
2468         for (i = 0; i < count; i++) {
2469                 for (;;) {
2470                         memset(&peer_info, 0, sizeof(peer_info));
2471                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2472                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2473                         peer_info.prcfg_prim_nid = list[i].nid;
2474                         peer_info.prcfg_size = size;
2475                         peer_info.prcfg_bulk = data;
2476
2477                         l_errno = 0;
2478                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2479                                      &peer_info);
2480                         if (rc == 0)
2481                                 break;
2482                         l_errno = errno;
2483                         if (l_errno != E2BIG) {
2484                                 snprintf(err_str,
2485                                         sizeof(err_str),
2486                                         "\"cannot get peer information: %s\"",
2487                                         strerror(l_errno));
2488                                 rc = -l_errno;
2489                                 goto out;
2490                         }
2491                         free(data);
2492                         size = peer_info.prcfg_size;
2493                         data = malloc(size);
2494                         if (data == NULL) {
2495                                 l_errno = ENOMEM;
2496                                 goto out;
2497                         }
2498                 }
2499
2500                 peer = cYAML_create_seq_item(peer_root);
2501                 if (peer == NULL)
2502                         goto out;
2503
2504                 if (first_seq == NULL)
2505                         first_seq = peer;
2506
2507                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2508                 if (cYAML_create_string(peer, "primary nid",
2509                                         libcfs_nid2str(pnid))
2510                     == NULL)
2511                         goto out;
2512                 if (cYAML_create_string(peer, "Multi-Rail",
2513                                         peer_info.prcfg_mr ? "True" : "False")
2514                     == NULL)
2515                         goto out;
2516                 /*
2517                  * print out the state of the peer only if details are
2518                  * requested
2519                  */
2520                 if (detail >= 3) {
2521                         if (cYAML_create_number(peer, "peer state",
2522                                                 peer_info.prcfg_state)
2523                                 == NULL)
2524                                 goto out;
2525                 }
2526
2527                 tmp = cYAML_create_seq(peer, "peer ni");
2528                 if (tmp == NULL)
2529                         goto out;
2530
2531                 lpni_data = data;
2532                 for (j = 0; j < peer_info.prcfg_count; j++) {
2533                         nidp = lpni_data;
2534                         lpni_cri = (void*)nidp + sizeof(nidp);
2535                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2536                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2537                         lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2538
2539                         peer_ni = cYAML_create_seq_item(tmp);
2540                         if (peer_ni == NULL)
2541                                 goto out;
2542
2543                         if (cYAML_create_string(peer_ni, "nid",
2544                                                 libcfs_nid2str(*nidp))
2545                             == NULL)
2546                                 goto out;
2547
2548                         if (cYAML_create_string(peer_ni, "state",
2549                                                 lpni_cri->cr_aliveness)
2550                             == NULL)
2551                                 goto out;
2552
2553                         if (!detail)
2554                                 continue;
2555
2556                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2557                                                 lpni_cri->cr_ni_peer_tx_credits)
2558                             == NULL)
2559                                 goto out;
2560
2561                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2562                                                 lpni_cri->cr_peer_tx_credits)
2563                             == NULL)
2564                                 goto out;
2565
2566                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2567                                                 lpni_cri->cr_peer_min_tx_credits)
2568                             == NULL)
2569                                 goto out;
2570
2571                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2572                                                 lpni_cri->cr_peer_tx_qnob)
2573                             == NULL)
2574                                 goto out;
2575
2576                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2577                                                 lpni_cri->cr_peer_rtr_credits)
2578                             == NULL)
2579                                 goto out;
2580
2581                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2582                                                 lpni_cri->cr_peer_min_rtr_credits)
2583                             == NULL)
2584                                 goto out;
2585
2586                         if (cYAML_create_number(peer_ni, "refcount",
2587                                                 lpni_cri->cr_refcount) == NULL)
2588                                 goto out;
2589
2590                         statistics = cYAML_create_object(peer_ni, "statistics");
2591                         if (statistics == NULL)
2592                                 goto out;
2593
2594                         if (cYAML_create_number(statistics, "send_count",
2595                                                 lpni_stats->iel_send_count)
2596                             == NULL)
2597                                 goto out;
2598
2599                         if (cYAML_create_number(statistics, "recv_count",
2600                                                 lpni_stats->iel_recv_count)
2601                             == NULL)
2602                                 goto out;
2603
2604                         if (cYAML_create_number(statistics, "drop_count",
2605                                                 lpni_stats->iel_drop_count)
2606                             == NULL)
2607                                 goto out;
2608
2609                         if (detail < 2)
2610                                 continue;
2611
2612                         for (k = 0; k < 3; k++) {
2613                                 struct lnet_ioctl_comm_count *counts;
2614
2615                                 msg_statistics = cYAML_create_object(peer_ni,
2616                                                  (char *) gmsg_stat_names[k]);
2617                                 if (msg_statistics == NULL)
2618                                         goto out;
2619
2620                                 counts = get_counts(msg_stats, k);
2621                                 if (counts == NULL)
2622                                         goto out;
2623
2624                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2625                                                                counts))
2626                                         goto out;
2627                         }
2628
2629                 }
2630         }
2631
2632         /* print output iff show_rc is not provided */
2633         if (show_rc == NULL)
2634                 cYAML_print_tree(root);
2635
2636         snprintf(err_str, sizeof(err_str), "\"success\"");
2637         rc = LUSTRE_CFG_RC_NO_ERR;
2638
2639 out:
2640         free(list);
2641         free(data);
2642         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2643                 cYAML_free_tree(root);
2644         } else if (show_rc != NULL && *show_rc != NULL) {
2645                 struct cYAML *show_node;
2646                 /* find the peer node, if one doesn't exist then
2647                  * insert one.  Otherwise add to the one there
2648                  */
2649                 show_node = cYAML_get_object_item(*show_rc,
2650                                                   "peer");
2651                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2652                         cYAML_insert_child(show_node, first_seq);
2653                         free(peer_root);
2654                         free(root);
2655                 } else if (show_node == NULL) {
2656                         cYAML_insert_sibling((*show_rc)->cy_child,
2657                                              peer_root);
2658                         free(root);
2659                 } else {
2660                         cYAML_free_tree(root);
2661                 }
2662         } else {
2663                 *show_rc = root;
2664         }
2665
2666         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2667                           err_rc);
2668
2669         return rc;
2670 }
2671
2672 int lustre_lnet_list_peer(int seq_no,
2673                           struct cYAML **show_rc, struct cYAML **err_rc)
2674 {
2675         struct lnet_ioctl_peer_cfg peer_info;
2676         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2677         __u32 count;
2678         __u32 size;
2679         int i = 0;
2680         int l_errno = 0;
2681         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2682         char err_str[LNET_MAX_STR_LEN];
2683         lnet_process_id_t *list = NULL;
2684
2685         snprintf(err_str, sizeof(err_str),
2686                  "\"out of memory\"");
2687
2688         memset(&peer_info, 0, sizeof(peer_info));
2689
2690         /* create struct cYAML root object */
2691         root = cYAML_create_object(NULL, NULL);
2692         if (root == NULL)
2693                 goto out;
2694
2695         list_root = cYAML_create_seq(root, "peer list");
2696         if (list_root == NULL)
2697                 goto out;
2698
2699         count = 1000;
2700         size = count * sizeof(lnet_process_id_t);
2701         list = malloc(size);
2702         if (list == NULL) {
2703                 l_errno = ENOMEM;
2704                 goto out;
2705         }
2706         for (;;) {
2707                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2708                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2709                 peer_info.prcfg_size = size;
2710                 peer_info.prcfg_bulk = list;
2711
2712                 l_errno = 0;
2713                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2714                 count = peer_info.prcfg_count;
2715                 if (rc == 0)
2716                         break;
2717                 l_errno = errno;
2718                 if (l_errno != E2BIG) {
2719                         snprintf(err_str,
2720                                 sizeof(err_str),
2721                                 "\"cannot get peer list: %s\"",
2722                                 strerror(l_errno));
2723                         rc = -l_errno;
2724                         goto out;
2725                 }
2726                 free(list);
2727                 size = peer_info.prcfg_size;
2728                 list = malloc(size);
2729                 if (list == NULL) {
2730                         l_errno = ENOMEM;
2731                         goto out;
2732                 }
2733         }
2734
2735         /* count is now the actual number of ids in the list. */
2736         for (i = 0; i < count; i++) {
2737                 if (cYAML_create_string(list_root, "nid",
2738                                         libcfs_nid2str(list[i].nid))
2739                     == NULL)
2740                         goto out;
2741         }
2742
2743         /* print output iff show_rc is not provided */
2744         if (show_rc == NULL)
2745                 cYAML_print_tree(root);
2746
2747         snprintf(err_str, sizeof(err_str), "\"success\"");
2748         rc = LUSTRE_CFG_RC_NO_ERR;
2749
2750 out:
2751         if (list != NULL)
2752                 free(list);
2753         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2754                 cYAML_free_tree(root);
2755         } else if (show_rc != NULL && *show_rc != NULL) {
2756                 struct cYAML *show_node;
2757                 /* find the peer node, if one doesn't exist then
2758                  * insert one.  Otherwise add to the one there
2759                  */
2760                 show_node = cYAML_get_object_item(*show_rc,
2761                                                   "peer");
2762                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2763                         cYAML_insert_child(show_node, first_seq);
2764                         free(list_root);
2765                         free(root);
2766                 } else if (show_node == NULL) {
2767                         cYAML_insert_sibling((*show_rc)->cy_child,
2768                                              list_root);
2769                         free(root);
2770                 } else {
2771                         cYAML_free_tree(root);
2772                 }
2773         } else {
2774                 *show_rc = root;
2775         }
2776
2777         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2778                           err_rc);
2779
2780         return rc;
2781 }
2782
2783 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2784                           struct cYAML *root)
2785 {
2786         struct cYAML *show_node;
2787
2788         show_node = cYAML_get_object_item(show_rc, "global");
2789         if (show_node != NULL)
2790                 cYAML_insert_sibling(show_node->cy_child,
2791                                      node->cy_child);
2792         else
2793                 cYAML_insert_sibling(show_rc->cy_child,
2794                                      node);
2795         free(root);
2796 }
2797
2798 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2799                                    char *name, __u32 value,
2800                                    struct cYAML **show_rc,
2801                                    struct cYAML **err_rc, int err)
2802 {
2803         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2804         struct cYAML *root = NULL, *global = NULL;
2805
2806         if (err) {
2807                 rc = err;
2808                 goto out;
2809         }
2810
2811         root = cYAML_create_object(NULL, NULL);
2812         if (root == NULL)
2813                 goto out;
2814
2815         global = cYAML_create_object(root, "global");
2816         if (global == NULL)
2817                 goto out;
2818
2819         if (cYAML_create_number(global, name,
2820                                 value) == NULL)
2821                 goto out;
2822
2823         if (show_rc == NULL)
2824                 cYAML_print_tree(root);
2825
2826         snprintf(err_str, err_len, "\"success\"");
2827
2828         rc = LUSTRE_CFG_RC_NO_ERR;
2829
2830 out:
2831         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2832                 cYAML_free_tree(root);
2833         } else if (show_rc != NULL && *show_rc != NULL) {
2834                 add_to_global(*show_rc, global, root);
2835         } else {
2836                 *show_rc = root;
2837         }
2838
2839         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2840
2841         return rc;
2842 }
2843
2844 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2845                                     struct cYAML **show_rc,
2846                                     struct cYAML **err_rc)
2847 {
2848         struct lnet_ioctl_set_value data;
2849         int rc;
2850         int l_errno = 0;
2851         char err_str[LNET_MAX_STR_LEN];
2852
2853         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2854
2855         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2856
2857         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2858         if (rc != 0) {
2859                 l_errno = -errno;
2860                 snprintf(err_str,
2861                          sizeof(err_str),
2862                          "\"cannot get %s: %s\"",
2863                          name, strerror(l_errno));
2864         }
2865
2866         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
2867                                        data.sv_value, show_rc, err_rc, l_errno);
2868 }
2869
2870 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2871                               struct cYAML **err_rc)
2872 {
2873         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2874         char val[LNET_MAX_STR_LEN];
2875         int max_intf = -1, l_errno = 0;
2876         char err_str[LNET_MAX_STR_LEN];
2877
2878         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2879
2880         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2881                              1, sizeof(val));
2882         if (rc) {
2883                 l_errno = -errno;
2884                 snprintf(err_str, sizeof(err_str),
2885                          "\"cannot get max interfaces: %d\"", rc);
2886         } else {
2887                 max_intf = atoi(val);
2888         }
2889
2890         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2891                                        "max_intf", max_intf, show_rc,
2892                                        err_rc, l_errno);
2893 }
2894
2895 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2896                                struct cYAML **err_rc)
2897 {
2898         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2899         char val[LNET_MAX_STR_LEN];
2900         int discovery = -1, l_errno = 0;
2901         char err_str[LNET_MAX_STR_LEN];
2902
2903         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2904
2905         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2906                              1, sizeof(val));
2907         if (rc) {
2908                 l_errno = -errno;
2909                 snprintf(err_str, sizeof(err_str),
2910                          "\"cannot get discovery setting: %d\"", rc);
2911         } else {
2912                 /*
2913                  * The kernel stores a discovery disabled value. User space
2914                  * shows whether discovery is enabled. So the value must be
2915                  * inverted.
2916                  */
2917                 discovery = !atoi(val);
2918         }
2919
2920         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
2921                                        "discovery", discovery, show_rc,
2922                                        err_rc, l_errno);
2923 }
2924
2925 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2926                                 struct cYAML **err_rc)
2927 {
2928         return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
2929                                         "numa_range", show_rc, err_rc);
2930 }
2931
2932 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2933                            struct cYAML **err_rc)
2934 {
2935         struct lnet_ioctl_lnet_stats data;
2936         int rc;
2937         int l_errno;
2938         char err_str[LNET_MAX_STR_LEN];
2939         struct cYAML *root = NULL, *stats = NULL;
2940
2941         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2942
2943         LIBCFS_IOC_INIT_V2(data, st_hdr);
2944
2945         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2946         if (rc != 0) {
2947                 l_errno = errno;
2948                 snprintf(err_str,
2949                          sizeof(err_str),
2950                          "\"cannot get lnet statistics: %s\"",
2951                          strerror(l_errno));
2952                 rc = -l_errno;
2953                 goto out;
2954         }
2955
2956         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2957
2958         root = cYAML_create_object(NULL, NULL);
2959         if (root == NULL)
2960                 goto out;
2961
2962         stats = cYAML_create_object(root, "statistics");
2963         if (stats == NULL)
2964                 goto out;
2965
2966         if (cYAML_create_number(stats, "msgs_alloc",
2967                                 data.st_cntrs.msgs_alloc) == NULL)
2968                 goto out;
2969
2970         if (cYAML_create_number(stats, "msgs_max",
2971                                 data.st_cntrs.msgs_max) == NULL)
2972                 goto out;
2973
2974         if (cYAML_create_number(stats, "errors",
2975                                 data.st_cntrs.errors) == NULL)
2976                 goto out;
2977
2978         if (cYAML_create_number(stats, "send_count",
2979                                 data.st_cntrs.send_count) == NULL)
2980                 goto out;
2981
2982         if (cYAML_create_number(stats, "recv_count",
2983                                 data.st_cntrs.recv_count) == NULL)
2984                 goto out;
2985
2986         if (cYAML_create_number(stats, "route_count",
2987                                 data.st_cntrs.route_count) == NULL)
2988                 goto out;
2989
2990         if (cYAML_create_number(stats, "drop_count",
2991                                 data.st_cntrs.drop_count) == NULL)
2992                 goto out;
2993
2994         if (cYAML_create_number(stats, "send_length",
2995                                 data.st_cntrs.send_length) == NULL)
2996                 goto out;
2997
2998         if (cYAML_create_number(stats, "recv_length",
2999                                 data.st_cntrs.recv_length) == NULL)
3000                 goto out;
3001
3002         if (cYAML_create_number(stats, "route_length",
3003                                 data.st_cntrs.route_length) == NULL)
3004                 goto out;
3005
3006         if (cYAML_create_number(stats, "drop_length",
3007                                 data.st_cntrs.drop_length) == NULL)
3008                 goto out;
3009
3010         if (show_rc == NULL)
3011                 cYAML_print_tree(root);
3012
3013         snprintf(err_str, sizeof(err_str), "\"success\"");
3014         rc = LUSTRE_CFG_RC_NO_ERR;
3015 out:
3016         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3017                 cYAML_free_tree(root);
3018         } else if (show_rc != NULL && *show_rc != NULL) {
3019                 cYAML_insert_sibling((*show_rc)->cy_child,
3020                                         root->cy_child);
3021                 free(root);
3022         } else {
3023                 *show_rc = root;
3024         }
3025
3026         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3027
3028         return rc;
3029 }
3030
3031 typedef int (*cmd_handler_t)(struct cYAML *tree,
3032                              struct cYAML **show_rc,
3033                              struct cYAML **err_rc);
3034
3035 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3036                                     struct cYAML **err_rc)
3037 {
3038         struct cYAML *net, *gw, *hop, *prio, *seq_no;
3039
3040         net = cYAML_get_object_item(tree, "net");
3041         gw = cYAML_get_object_item(tree, "gateway");
3042         hop = cYAML_get_object_item(tree, "hop");
3043         prio = cYAML_get_object_item(tree, "priority");
3044         seq_no = cYAML_get_object_item(tree, "seq_no");
3045
3046         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3047                                         (gw) ? gw->cy_valuestring : NULL,
3048                                         (hop) ? hop->cy_valueint : -1,
3049                                         (prio) ? prio->cy_valueint : -1,
3050                                         (seq_no) ? seq_no->cy_valueint : -1,
3051                                         err_rc);
3052 }
3053
3054 static void yaml_free_string_array(char **array, int num)
3055 {
3056         int i;
3057         char **sub_array = array;
3058
3059         for (i = 0; i < num; i++) {
3060                 if (*sub_array != NULL)
3061                         free(*sub_array);
3062                 sub_array++;
3063         }
3064         if (array)
3065                 free(array);
3066 }
3067
3068 /*
3069  *    interfaces:
3070  *        0: <intf_name>['['<expr>']']
3071  *        1: <intf_name>['['<expr>']']
3072  */
3073 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3074                                struct lnet_dlc_network_descr *nw_descr)
3075 {
3076         struct cYAML *child = NULL;
3077         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3078         struct lnet_dlc_intf_descr *intf_descr, *tmp;
3079
3080         if (intf_tree == NULL || nw_descr == NULL)
3081                 return LUSTRE_CFG_RC_BAD_PARAM;
3082
3083         /* now grab all the interfaces and their cpts */
3084         child = intf_tree->cy_child;
3085         while (child != NULL) {
3086                 if (child->cy_valuestring == NULL) {
3087                         child = child->cy_next;
3088                         continue;
3089                 }
3090
3091                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3092                         goto failed;
3093
3094                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3095                                                 child->cy_valuestring,
3096                                                 strlen(child->cy_valuestring));
3097                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3098                         goto failed;
3099
3100                 intf_num++;
3101                 child = child->cy_next;
3102         }
3103
3104         if (intf_num == 0)
3105                 return LUSTRE_CFG_RC_MISSING_PARAM;
3106
3107         return intf_num;
3108
3109 failed:
3110         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3111                                  intf_on_network) {
3112                 list_del(&intf_descr->intf_on_network);
3113                 free_intf_descr(intf_descr);
3114         }
3115
3116         return rc;
3117 }
3118
3119 static bool
3120 yaml_extract_cmn_tunables(struct cYAML *tree,
3121                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3122                           struct cfs_expr_list **global_cpts)
3123 {
3124         struct cYAML *tun, *item, *smp;
3125         int rc;
3126
3127         tun = cYAML_get_object_item(tree, "tunables");
3128         if (tun != NULL) {
3129                 item = cYAML_get_object_item(tun, "peer_timeout");
3130                 if (item != NULL)
3131                         tunables->lct_peer_timeout = item->cy_valueint;
3132                 item = cYAML_get_object_item(tun, "peer_credits");
3133                 if (item != NULL)
3134                         tunables->lct_peer_tx_credits = item->cy_valueint;
3135                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3136                 if (item != NULL)
3137                         tunables->lct_peer_rtr_credits = item->cy_valueint;
3138                 item = cYAML_get_object_item(tun, "credits");
3139                 if (item != NULL)
3140                         tunables->lct_max_tx_credits = item->cy_valueint;
3141                 smp = cYAML_get_object_item(tun, "CPT");
3142                 if (smp != NULL) {
3143                         rc = cfs_expr_list_parse(smp->cy_valuestring,
3144                                                  strlen(smp->cy_valuestring),
3145                                                  0, UINT_MAX, global_cpts);
3146                         if (rc != 0)
3147                                 *global_cpts = NULL;
3148                 }
3149
3150                 return true;
3151         }
3152
3153         return false;
3154 }
3155
3156 static bool
3157 yaml_extract_tunables(struct cYAML *tree,
3158                       struct lnet_ioctl_config_lnd_tunables *tunables,
3159                       struct cfs_expr_list **global_cpts,
3160                       __u32 net_type)
3161 {
3162         bool rc;
3163
3164         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3165                                        global_cpts);
3166
3167         if (!rc)
3168                 return rc;
3169
3170         lustre_yaml_extract_lnd_tunables(tree, net_type,
3171                                          &tunables->lt_tun);
3172
3173         return rc;
3174 }
3175
3176 /*
3177  * net:
3178  *    - net type: <net>[<NUM>]
3179   *      local NI(s):
3180  *        - nid: <ip>@<net>[<NUM>]
3181  *          status: up
3182  *          interfaces:
3183  *               0: <intf_name>['['<expr>']']
3184  *               1: <intf_name>['['<expr>']']
3185  *        tunables:
3186  *               peer_timeout: <NUM>
3187  *               peer_credits: <NUM>
3188  *               peer_buffer_credits: <NUM>
3189  *               credits: <NUM>
3190 *         lnd tunables:
3191  *               peercredits_hiw: <NUM>
3192  *               map_on_demand: <NUM>
3193  *               concurrent_sends: <NUM>
3194  *               fmr_pool_size: <NUM>
3195  *               fmr_flush_trigger: <NUM>
3196  *               fmr_cache: <NUM>
3197  *
3198  * At least one interface is required. If no interfaces are provided the
3199  * network interface can not be configured.
3200  */
3201 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3202                                  struct cYAML **err_rc)
3203 {
3204         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3205                      *item = NULL;
3206         int num_entries = 0, rc;
3207         struct lnet_dlc_network_descr nw_descr;
3208         struct cfs_expr_list *global_cpts = NULL;
3209         struct lnet_ioctl_config_lnd_tunables tunables;
3210         bool found = false;
3211
3212         memset(&tunables, 0, sizeof(tunables));
3213
3214         INIT_LIST_HEAD(&nw_descr.network_on_rule);
3215         INIT_LIST_HEAD(&nw_descr.nw_intflist);
3216
3217         ip2net = cYAML_get_object_item(tree, "ip2net");
3218         net = cYAML_get_object_item(tree, "net type");
3219         if (net)
3220                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3221         else
3222                 nw_descr.nw_id = LOLND;
3223
3224         /*
3225          * if neither net nor ip2nets are present, then we can not
3226          * configure the network.
3227          */
3228         if (!net && !ip2net)
3229                 return LUSTRE_CFG_RC_MISSING_PARAM;
3230
3231         local_nis = cYAML_get_object_item(tree, "local NI(s)");
3232         if (local_nis == NULL)
3233                 return LUSTRE_CFG_RC_MISSING_PARAM;
3234
3235         if (!cYAML_is_sequence(local_nis))
3236                 return LUSTRE_CFG_RC_BAD_PARAM;
3237
3238         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3239                 intf = cYAML_get_object_item(item, "interfaces");
3240                 if (intf == NULL)
3241                         continue;
3242                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3243                 if (num_entries <= 0) {
3244                         cYAML_build_error(num_entries, -1, "ni", "add",
3245                                         "bad interface list",
3246                                         err_rc);
3247                         return LUSTRE_CFG_RC_BAD_PARAM;
3248                 }
3249         }
3250
3251         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3252                                       LNET_NETTYP(nw_descr.nw_id));
3253         seq_no = cYAML_get_object_item(tree, "seq_no");
3254
3255         rc = lustre_lnet_config_ni(&nw_descr,
3256                                    global_cpts,
3257                                    (ip2net) ? ip2net->cy_valuestring : NULL,
3258                                    (found) ? &tunables: NULL,
3259                                    (seq_no) ? seq_no->cy_valueint : -1,
3260                                    err_rc);
3261
3262         if (global_cpts != NULL)
3263                 cfs_expr_list_free(global_cpts);
3264
3265         return rc;
3266 }
3267
3268 /*
3269  * ip2nets:
3270  *  - net-spec: <tcp|o2ib|gni>[NUM]
3271  *    interfaces:
3272  *        0: <intf name>['['<expr>']']
3273  *        1: <intf name>['['<expr>']']
3274  *    ip-range:
3275  *        0: <expr.expr.expr.expr>
3276  *        1: <expr.expr.expr.expr>
3277  */
3278 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3279                                       struct cYAML **show_rc,
3280                                       struct cYAML **err_rc)
3281 {
3282         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3283                      *seq_no = NULL;
3284         struct lustre_lnet_ip2nets ip2nets;
3285         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3286                                           *tmp = NULL;
3287         int rc = LUSTRE_CFG_RC_NO_ERR;
3288         struct cfs_expr_list *global_cpts = NULL;
3289         struct cfs_expr_list *el, *el_tmp;
3290         struct lnet_ioctl_config_lnd_tunables tunables;
3291         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
3292         bool found = false;
3293
3294         memset(&tunables, 0, sizeof(tunables));
3295
3296         /* initialize all lists */
3297         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
3298         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
3299         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
3300
3301         net = cYAML_get_object_item(tree, "net-spec");
3302         if (net == NULL)
3303                 return LUSTRE_CFG_RC_BAD_PARAM;
3304
3305         if (net != NULL && net->cy_valuestring == NULL)
3306                 return LUSTRE_CFG_RC_BAD_PARAM;
3307
3308         /* assign the network id */
3309         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
3310         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
3311                 return LUSTRE_CFG_RC_BAD_PARAM;
3312
3313         seq_no = cYAML_get_object_item(tree, "seq_no");
3314
3315         intf = cYAML_get_object_item(tree, "interfaces");
3316         if (intf != NULL) {
3317                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
3318                 if (rc <= 0)
3319                         return LUSTRE_CFG_RC_BAD_PARAM;
3320         }
3321
3322         ip_range = cYAML_get_object_item(tree, "ip-range");
3323         if (ip_range != NULL) {
3324                 item = ip_range->cy_child;
3325                 while (item != NULL) {
3326                         if (item->cy_valuestring == NULL) {
3327                                 item = item->cy_next;
3328                                 continue;
3329                         }
3330
3331                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
3332                                                       item->cy_valuestring);
3333
3334                         if (rc != LUSTRE_CFG_RC_NO_ERR)
3335                                 goto out;
3336
3337                         item = item->cy_next;
3338                 }
3339         }
3340
3341         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3342                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
3343
3344         rc = lustre_lnet_config_ip2nets(&ip2nets,
3345                         (found) ? &tunables : NULL,
3346                         global_cpts,
3347                         (seq_no) ? seq_no->cy_valueint : -1,
3348                         err_rc);
3349
3350         /*
3351          * don't stop because there was no match. Continue processing the
3352          * rest of the rules. If non-match then nothing is configured
3353          */
3354         if (rc == LUSTRE_CFG_RC_NO_MATCH)
3355                 rc = LUSTRE_CFG_RC_NO_ERR;
3356 out:
3357         list_for_each_entry_safe(intf_descr, intf_tmp,
3358                                  &ip2nets.ip2nets_net.nw_intflist,
3359                                  intf_on_network) {
3360                 list_del(&intf_descr->intf_on_network);
3361                 free_intf_descr(intf_descr);
3362         }
3363
3364         list_for_each_entry_safe(ip_range_descr, tmp,
3365                                  &ip2nets.ip2nets_ip_ranges,
3366                                  ipr_entry) {
3367                 list_del(&ip_range_descr->ipr_entry);
3368                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
3369                                          el_link) {
3370                         list_del(&el->el_link);
3371                         cfs_expr_list_free(el);
3372                 }
3373                 free(ip_range_descr);
3374         }
3375
3376         return rc;
3377 }
3378
3379 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
3380                               struct cYAML **err_rc)
3381 {
3382         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
3383                      *local_nis = NULL;
3384         int num_entries, rc;
3385         struct lnet_dlc_network_descr nw_descr;
3386
3387         INIT_LIST_HEAD(&nw_descr.network_on_rule);
3388         INIT_LIST_HEAD(&nw_descr.nw_intflist);
3389
3390         net = cYAML_get_object_item(tree, "net type");
3391         if (net != NULL)
3392                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3393
3394         local_nis = cYAML_get_object_item(tree, "local NI(s)");
3395         if (local_nis == NULL)
3396                 return LUSTRE_CFG_RC_MISSING_PARAM;
3397
3398         if (!cYAML_is_sequence(local_nis))
3399                 return LUSTRE_CFG_RC_BAD_PARAM;
3400
3401         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3402                 intf = cYAML_get_object_item(item, "interfaces");
3403                 if (intf == NULL)
3404                         continue;
3405                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3406                 if (num_entries <= 0) {
3407                         cYAML_build_error(num_entries, -1, "ni", "add",
3408                                         "bad interface list",
3409                                         err_rc);
3410                         return LUSTRE_CFG_RC_BAD_PARAM;
3411                 }
3412         }
3413
3414         seq_no = cYAML_get_object_item(tree, "seq_no");
3415
3416         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
3417                                 (seq_no) ? seq_no->cy_valueint : -1,
3418                                 err_rc);
3419
3420         return rc;
3421 }
3422
3423 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
3424 {
3425         struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
3426                      *prim_nid = NULL;
3427         char **nids = NULL;
3428         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3429
3430         prim_nid = cYAML_get_object_item(tree, "primary nid");
3431         if (!prim_nid || !prim_nid->cy_valuestring)
3432                 return LUSTRE_CFG_RC_MISSING_PARAM;
3433
3434         nids_entry = cYAML_get_object_item(tree, "peer ni");
3435         if (cYAML_is_sequence(nids_entry)) {
3436                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3437                         entry = cYAML_get_object_item(child, "nid");
3438                         /* don't count an empty entry */
3439                         if (!entry || !entry->cy_valuestring)
3440                                 continue;
3441
3442                         if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3443                                         == 0) && del) {
3444                                 /*
3445                                  * primary nid is present in the list of
3446                                  * nids so that means we want to delete
3447                                  * the entire peer, so no need to go
3448                                  * further. Just delete the entire peer.
3449                                  */
3450                                 return 0;
3451                         }
3452
3453                         num++;
3454                 }
3455         }
3456
3457         if (num == 0)
3458                 return LUSTRE_CFG_RC_MISSING_PARAM;
3459
3460         nids = calloc(sizeof(*nids) * num, 1);
3461         if (nids == NULL)
3462                 return LUSTRE_CFG_RC_OUT_OF_MEM;
3463
3464         /* now grab all the nids */
3465         num = 0;
3466         child = NULL;
3467         while (cYAML_get_next_seq_item(nids_entry, &child)) {
3468                 entry = cYAML_get_object_item(child, "nid");
3469                 if (!entry || !entry->cy_valuestring)
3470                         continue;
3471
3472                 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3473                 if (!nids[num]) {
3474                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3475                         goto failed;
3476                 }
3477                 strncpy(nids[num], entry->cy_valuestring,
3478                         strlen(entry->cy_valuestring));
3479                 num++;
3480         }
3481         rc = num;
3482
3483         *nidsppp = nids;
3484         return rc;
3485
3486 failed:
3487         if (nids != NULL)
3488                 yaml_free_string_array(nids, num);
3489         *nidsppp = NULL;
3490         return rc;
3491 }
3492
3493 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3494                                    struct cYAML **err_rc)
3495 {
3496         char **nids = NULL;
3497         int num, rc;
3498         struct cYAML *seq_no, *prim_nid, *non_mr;
3499
3500         num = yaml_copy_peer_nids(tree, &nids, false);
3501         if (num < 0)
3502                 return num;
3503
3504         seq_no = cYAML_get_object_item(tree, "seq_no");
3505         prim_nid = cYAML_get_object_item(tree, "primary nid");
3506         non_mr = cYAML_get_object_item(tree, "non_mr");
3507
3508         rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3509                                          nids, num,
3510                                          (non_mr) ? false : true,
3511                                          (seq_no) ? seq_no->cy_valueint : -1,
3512                                          err_rc);
3513
3514         yaml_free_string_array(nids, num);
3515         return rc;
3516 }
3517
3518 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3519                                 struct cYAML **err_rc)
3520 {
3521         char **nids = NULL;
3522         int num, rc;
3523         struct cYAML *seq_no, *prim_nid;
3524
3525         num = yaml_copy_peer_nids(tree, &nids, true);
3526         if (num < 0)
3527                 return num;
3528
3529         seq_no = cYAML_get_object_item(tree, "seq_no");
3530         prim_nid = cYAML_get_object_item(tree, "primary nid");
3531
3532         rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3533                                       nids, num,
3534                                       (seq_no) ? seq_no->cy_valueint : -1,
3535                                       err_rc);
3536
3537         yaml_free_string_array(nids, num);
3538         return rc;
3539 }
3540
3541 static int handle_yaml_config_buffers(struct cYAML *tree,
3542                                       struct cYAML **show_rc,
3543                                       struct cYAML **err_rc)
3544 {
3545         int rc;
3546         struct cYAML *tiny, *small, *large, *seq_no;
3547
3548         tiny = cYAML_get_object_item(tree, "tiny");
3549         small = cYAML_get_object_item(tree, "small");
3550         large = cYAML_get_object_item(tree, "large");
3551         seq_no = cYAML_get_object_item(tree, "seq_no");
3552
3553         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3554                                         (small) ? small->cy_valueint : -1,
3555                                         (large) ? large->cy_valueint : -1,
3556                                         (seq_no) ? seq_no->cy_valueint : -1,
3557                                         err_rc);
3558
3559         return rc;
3560 }
3561
3562 static int handle_yaml_config_routing(struct cYAML *tree,
3563                                       struct cYAML **show_rc,
3564                                       struct cYAML **err_rc)
3565 {
3566         int rc = LUSTRE_CFG_RC_NO_ERR;
3567         struct cYAML *seq_no, *enable;
3568
3569         seq_no = cYAML_get_object_item(tree, "seq_no");
3570         enable = cYAML_get_object_item(tree, "enable");
3571
3572         if (enable) {
3573                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3574                                                 (seq_no) ?
3575                                                     seq_no->cy_valueint : -1,
3576                                                 err_rc);
3577         }
3578
3579         return rc;
3580 }
3581
3582 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3583                                  struct cYAML **err_rc)
3584 {
3585         struct cYAML *net;
3586         struct cYAML *gw;
3587         struct cYAML *seq_no;
3588
3589         net = cYAML_get_object_item(tree, "net");
3590         gw = cYAML_get_object_item(tree, "gateway");
3591         seq_no = cYAML_get_object_item(tree, "seq_no");
3592
3593         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3594                                      (gw) ? gw->cy_valuestring : NULL,
3595                                      (seq_no) ? seq_no->cy_valueint : -1,
3596                                      err_rc);
3597 }
3598
3599 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3600                                    struct cYAML **err_rc)
3601 {
3602         struct cYAML *seq_no;
3603
3604         seq_no = cYAML_get_object_item(tree, "seq_no");
3605
3606         return lustre_lnet_enable_routing(0, (seq_no) ?
3607                                                 seq_no->cy_valueint : -1,
3608                                         err_rc);
3609 }
3610
3611 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3612                                   struct cYAML **err_rc)
3613 {
3614         struct cYAML *net;
3615         struct cYAML *gw;
3616         struct cYAML *hop;
3617         struct cYAML *prio;
3618         struct cYAML *detail;
3619         struct cYAML *seq_no;
3620
3621         net = cYAML_get_object_item(tree, "net");
3622         gw = cYAML_get_object_item(tree, "gateway");
3623         hop = cYAML_get_object_item(tree, "hop");
3624         prio = cYAML_get_object_item(tree, "priority");
3625         detail = cYAML_get_object_item(tree, "detail");
3626         seq_no = cYAML_get_object_item(tree, "seq_no");
3627
3628         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3629                                       (gw) ? gw->cy_valuestring : NULL,
3630                                       (hop) ? hop->cy_valueint : -1,
3631                                       (prio) ? prio->cy_valueint : -1,
3632                                       (detail) ? detail->cy_valueint : 0,
3633                                       (seq_no) ? seq_no->cy_valueint : -1,
3634                                       show_rc,
3635                                       err_rc);
3636 }
3637
3638 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3639                                 struct cYAML **err_rc)
3640 {
3641         struct cYAML *net, *detail, *seq_no;
3642
3643         net = cYAML_get_object_item(tree, "net");
3644         detail = cYAML_get_object_item(tree, "detail");
3645         seq_no = cYAML_get_object_item(tree, "seq_no");
3646
3647         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3648                                     (detail) ? detail->cy_valueint : 0,
3649                                     (seq_no) ? seq_no->cy_valueint : -1,
3650                                     show_rc,
3651                                     err_rc);
3652 }
3653
3654 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3655                                     struct cYAML **err_rc)
3656 {
3657         struct cYAML *seq_no;
3658
3659         seq_no = cYAML_get_object_item(tree, "seq_no");
3660
3661         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3662                                         show_rc, err_rc);
3663 }
3664
3665 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3666                                   struct cYAML **err_rc)
3667 {
3668         struct cYAML *seq_no, *nid, *detail;
3669
3670         seq_no = cYAML_get_object_item(tree, "seq_no");
3671         detail = cYAML_get_object_item(tree, "detail");
3672         nid = cYAML_get_object_item(tree, "nid");
3673
3674         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3675                                      (detail) ? detail->cy_valueint : 0,
3676                                      (seq_no) ? seq_no->cy_valueint : -1,
3677                                      show_rc, err_rc);
3678 }
3679
3680 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3681                                   struct cYAML **err_rc)
3682 {
3683         struct cYAML *seq_no;
3684
3685         seq_no = cYAML_get_object_item(tree, "seq_no");
3686
3687         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3688                                       show_rc, err_rc);
3689 }
3690
3691 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3692                                   struct cYAML **err_rc)
3693 {
3694         struct cYAML *seq_no, *range;
3695
3696         seq_no = cYAML_get_object_item(tree, "seq_no");
3697         range = cYAML_get_object_item(tree, "range");
3698
3699         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3700                                              seq_no ? seq_no->cy_valueint : -1,
3701                                              err_rc);
3702 }
3703
3704 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3705                                struct cYAML **err_rc)
3706 {
3707         struct cYAML *seq_no;
3708
3709         seq_no = cYAML_get_object_item(tree, "seq_no");
3710
3711         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3712                                              err_rc);
3713 }
3714
3715 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3716                                 struct cYAML **err_rc)
3717 {
3718         struct cYAML *seq_no;
3719
3720         seq_no = cYAML_get_object_item(tree, "seq_no");
3721
3722         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3723                                            show_rc, err_rc);
3724 }
3725
3726 static int handle_yaml_config_global_settings(struct cYAML *tree,
3727                                               struct cYAML **show_rc,
3728                                               struct cYAML **err_rc)
3729 {
3730         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3731         int rc = 0;
3732
3733         seq_no = cYAML_get_object_item(tree, "seq_no");
3734         max_intf = cYAML_get_object_item(tree, "max_intf");
3735         if (max_intf)
3736                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3737                                                  seq_no ? seq_no->cy_valueint
3738                                                         : -1,
3739                                                  err_rc);
3740
3741         numa = cYAML_get_object_item(tree, "numa_range");
3742         if (numa)
3743                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3744                                                    seq_no ? seq_no->cy_valueint
3745                                                         : -1,
3746                                                    err_rc);
3747
3748         discovery = cYAML_get_object_item(tree, "discovery");
3749         if (discovery)
3750                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3751                                                   seq_no ? seq_no->cy_valueint
3752                                                         : -1,
3753                                                   err_rc);
3754
3755         return rc;
3756 }
3757
3758 static int handle_yaml_del_global_settings(struct cYAML *tree,
3759                                            struct cYAML **show_rc,
3760                                            struct cYAML **err_rc)
3761 {
3762         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3763         int rc = 0;
3764
3765         seq_no = cYAML_get_object_item(tree, "seq_no");
3766         max_intf = cYAML_get_object_item(tree, "max_intf");
3767         if (max_intf)
3768                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3769                                                  seq_no ? seq_no->cy_valueint
3770                                                         : -1,
3771                                                  err_rc);
3772
3773         numa = cYAML_get_object_item(tree, "numa_range");
3774         if (numa)
3775                 rc = lustre_lnet_config_numa_range(0,
3776                                                    seq_no ? seq_no->cy_valueint
3777                                                         : -1,
3778                                                    err_rc);
3779
3780         /* peer discovery is enabled by default */
3781         discovery = cYAML_get_object_item(tree, "discovery");
3782         if (discovery)
3783                 rc = lustre_lnet_config_discovery(1,
3784                                                   seq_no ? seq_no->cy_valueint
3785                                                         : -1,
3786                                                   err_rc);
3787
3788         return rc;
3789 }
3790
3791 static int handle_yaml_show_global_settings(struct cYAML *tree,
3792                                             struct cYAML **show_rc,
3793                                             struct cYAML **err_rc)
3794 {
3795         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3796         int rc = 0;
3797
3798         seq_no = cYAML_get_object_item(tree, "seq_no");
3799         max_intf = cYAML_get_object_item(tree, "max_intf");
3800         if (max_intf)
3801                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3802                                                         : -1,
3803                                                 show_rc, err_rc);
3804
3805         numa = cYAML_get_object_item(tree, "numa_range");
3806         if (numa)
3807                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3808                                                         : -1,
3809                                                  show_rc, err_rc);
3810
3811         discovery = cYAML_get_object_item(tree, "discovery");
3812         if (discovery)
3813                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3814                                                         : -1,
3815                                                 show_rc, err_rc);
3816
3817         return rc;
3818 }
3819
3820 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
3821                             struct cYAML **err_rc)
3822 {
3823         struct cYAML *seq_no, *nid, *timeout;
3824
3825         seq_no = cYAML_get_object_item(tree, "seq_no");
3826         nid = cYAML_get_object_item(tree, "primary nid");
3827         timeout = cYAML_get_object_item(tree, "timeout");
3828
3829         return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
3830                                     (timeout) ? timeout->cy_valueint : 1000,
3831                                     (seq_no) ? seq_no->cy_valueint : -1,
3832                                     show_rc, err_rc);
3833 }
3834
3835 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
3836                                 struct cYAML **err_rc)
3837 {
3838         struct cYAML *seq_no, *nid, *force;
3839
3840         seq_no = cYAML_get_object_item(tree, "seq_no");
3841         nid = cYAML_get_object_item(tree, "primary nid");
3842         force = cYAML_get_object_item(tree, "force");
3843
3844         return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
3845                                         (force) ? force->cy_valueint : 0,
3846                                         (seq_no) ? seq_no->cy_valueint : -1,
3847                                         show_rc, err_rc);
3848 }
3849
3850 static int handle_yaml_no_op()
3851 {
3852         return LUSTRE_CFG_RC_NO_ERR;
3853 }
3854
3855 struct lookup_cmd_hdlr_tbl {
3856         char *name;
3857         cmd_handler_t cb;
3858 };
3859
3860 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3861         { .name = "route",      .cb = handle_yaml_config_route },
3862         { .name = "net",        .cb = handle_yaml_config_ni },
3863         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
3864         { .name = "peer",       .cb = handle_yaml_config_peer },
3865         { .name = "routing",    .cb = handle_yaml_config_routing },
3866         { .name = "buffers",    .cb = handle_yaml_config_buffers },
3867         { .name = "statistics", .cb = handle_yaml_no_op },
3868         { .name = "global",     .cb = handle_yaml_config_global_settings},
3869         { .name = "numa",       .cb = handle_yaml_config_numa },
3870         { .name = "ping",       .cb = handle_yaml_no_op },
3871         { .name = "discover",   .cb = handle_yaml_no_op },
3872         { .name = NULL } };
3873
3874 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3875         { .name = "route",      .cb = handle_yaml_del_route },
3876         { .name = "net",        .cb = handle_yaml_del_ni },
3877         { .name = "ip2nets",    .cb = handle_yaml_no_op },
3878         { .name = "peer",       .cb = handle_yaml_del_peer },
3879         { .name = "routing",    .cb = handle_yaml_del_routing },
3880         { .name = "buffers",    .cb = handle_yaml_no_op },
3881         { .name = "statistics", .cb = handle_yaml_no_op },
3882         { .name = "global",     .cb = handle_yaml_del_global_settings},
3883         { .name = "numa",       .cb = handle_yaml_del_numa },
3884         { .name = "ping",       .cb = handle_yaml_no_op },
3885         { .name = "discover",   .cb = handle_yaml_no_op },
3886         { .name = NULL } };
3887
3888 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3889         { .name = "route",      .cb = handle_yaml_show_route },
3890         { .name = "net",        .cb = handle_yaml_show_net },
3891         { .name = "peer",       .cb = handle_yaml_show_peers },
3892         { .name = "ip2nets",    .cb = handle_yaml_no_op },
3893         { .name = "routing",    .cb = handle_yaml_show_routing },
3894         { .name = "buffers",    .cb = handle_yaml_show_routing },
3895         { .name = "statistics", .cb = handle_yaml_show_stats },
3896         { .name = "global",     .cb = handle_yaml_show_global_settings},
3897         { .name = "numa",       .cb = handle_yaml_show_numa },
3898         { .name = "ping",       .cb = handle_yaml_no_op },
3899         { .name = "discover",   .cb = handle_yaml_no_op },
3900         { .name = NULL } };
3901
3902 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
3903         { .name = "route",      .cb = handle_yaml_no_op },
3904         { .name = "net",        .cb = handle_yaml_no_op },
3905         { .name = "peer",       .cb = handle_yaml_no_op },
3906         { .name = "ip2nets",    .cb = handle_yaml_no_op },
3907         { .name = "routing",    .cb = handle_yaml_no_op },
3908         { .name = "buffers",    .cb = handle_yaml_no_op },
3909         { .name = "statistics", .cb = handle_yaml_no_op },
3910         { .name = "global",     .cb = handle_yaml_no_op },
3911         { .name = "numa",       .cb = handle_yaml_no_op },
3912         { .name = "ping",       .cb = handle_yaml_ping },
3913         { .name = "discover",   .cb = handle_yaml_discover },
3914         { .name = NULL } };
3915
3916 static cmd_handler_t lookup_fn(char *key,
3917                                struct lookup_cmd_hdlr_tbl *tbl)
3918 {
3919         int i;
3920         if (key == NULL)
3921                 return NULL;
3922
3923         for (i = 0; tbl[i].name != NULL; i++) {
3924                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3925                         return tbl[i].cb;
3926         }
3927
3928         return NULL;
3929 }
3930
3931 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3932                                  struct cYAML **show_rc, struct cYAML **err_rc)
3933 {
3934         struct cYAML *tree, *item = NULL, *head, *child;
3935         cmd_handler_t cb;
3936         char err_str[LNET_MAX_STR_LEN];
3937         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3938
3939         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3940         if (tree == NULL)
3941                 return LUSTRE_CFG_RC_BAD_PARAM;
3942
3943         child = tree->cy_child;
3944         while (child != NULL) {
3945                 cb = lookup_fn(child->cy_string, table);
3946                 if (cb == NULL) {
3947                         snprintf(err_str, sizeof(err_str),
3948                                 "\"call back for '%s' not found\"",
3949                                 child->cy_string);
3950                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3951                                         "yaml", "helper", err_str, err_rc);
3952                         goto out;
3953                 }
3954
3955                 if (cYAML_is_sequence(child)) {
3956                         while ((head = cYAML_get_next_seq_item(child, &item))
3957                                != NULL) {
3958                                 rc = cb(head, show_rc, err_rc);
3959                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3960                                         return_rc = rc;
3961                         }
3962                 } else {
3963                         rc = cb(child, show_rc, err_rc);
3964                         if (rc != LUSTRE_CFG_RC_NO_ERR)
3965                                 return_rc = rc;
3966                 }
3967                 item = NULL;
3968                 child = child->cy_next;
3969         }
3970
3971 out:
3972         cYAML_free_tree(tree);
3973
3974         return return_rc;
3975 }
3976
3977 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3978 {
3979         return lustre_yaml_cb_helper(f, lookup_config_tbl,
3980                                      NULL, err_rc);
3981 }
3982
3983 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3984 {
3985         return lustre_yaml_cb_helper(f, lookup_del_tbl,
3986                                      NULL, err_rc);
3987 }
3988
3989 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3990 {
3991         return lustre_yaml_cb_helper(f, lookup_show_tbl,
3992                                      show_rc, err_rc);
3993 }
3994
3995 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3996 {
3997         return lustre_yaml_cb_helper(f, lookup_exec_tbl,
3998                                      show_rc, err_rc);
3999 }