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