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