Whamcloud - gitweb
LU-11300 lnet: configure lnet router senstivity
[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 MAX_NUM_IPS             128
64
65 #define modparam_path "/sys/module/lnet/parameters/"
66 #define gni_nid_path "/proc/cray_xt/"
67
68 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
69                                  "dropped_stats"};
70
71 /*
72  * lustre_lnet_ip_range_descr
73  *      Describes an IP range.
74  *      Each octect is an expression
75  */
76 struct lustre_lnet_ip_range_descr {
77         struct list_head ipr_entry;
78         struct list_head ipr_expr;
79 };
80
81 /*
82  * lustre_lnet_ip2nets
83  *      Describes an ip2nets rule. This can be on a list of rules.
84  */
85 struct lustre_lnet_ip2nets {
86         struct lnet_dlc_network_descr ip2nets_net;
87         struct list_head ip2nets_ip_ranges;
88 };
89
90 int open_sysfs_file(const char *path, const char *attr, const int mode)
91 {
92         int fd;
93         char filename[LNET_MAX_STR_LEN];
94
95         if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
96                 return -1;
97
98         snprintf(filename, sizeof(filename), "%s%s",
99                  path, attr);
100
101         fd = open(filename, mode);
102
103         return fd;
104 }
105
106 static int read_sysfs_file(const char *path, const char *attr,
107                            void *val, const size_t size, const int nelem)
108 {
109         int fd;
110         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
111
112         fd = open_sysfs_file(path, attr, O_RDONLY);
113         if (fd == -1)
114                 return LUSTRE_CFG_RC_NO_MATCH;
115
116         if (read(fd, val, size * nelem) == -1)
117                 goto close_fd;
118
119         rc = LUSTRE_CFG_RC_NO_ERR;
120
121 close_fd:
122         close(fd);
123         return rc;
124 }
125
126 static int write_sysfs_file(const char *path, const char *attr,
127                             void *val, const size_t size, const int nelem)
128 {
129         int fd;
130         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
131
132         fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
133         if (fd == -1)
134                 return LUSTRE_CFG_RC_NO_MATCH;
135
136         if (write(fd, val, size * nelem) == -1)
137                 goto close_fd;
138
139         rc = LUSTRE_CFG_RC_NO_ERR;
140
141 close_fd:
142         close(fd);
143         return rc;
144 }
145
146 /*
147  * free_intf_descr
148  *      frees the memory allocated for an intf descriptor.
149  */
150 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
151 {
152         if (!intf_descr)
153                 return;
154
155         if (intf_descr->cpt_expr != NULL)
156                 cfs_expr_list_free(intf_descr->cpt_expr);
157         free(intf_descr);
158 }
159
160 /*
161  * lustre_lnet_add_ip_range
162  * Formatting:
163  *      given a string of the format:
164  *      <expr.expr.expr.expr> parse each expr into
165  *      a lustre_lnet_ip_range_descr structure and insert on the list.
166  *
167  *      This function is called from
168  *              YAML on each ip-range.
169  *              As a result of lnetctl command
170  *              When building a NID or P2P selection rules
171  */
172 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
173 {
174         struct lustre_lnet_ip_range_descr *ip_range;
175         int rc;
176
177         ip_range = calloc(1, sizeof(*ip_range));
178         if (ip_range == NULL)
179                 return LUSTRE_CFG_RC_OUT_OF_MEM;
180
181         INIT_LIST_HEAD(&ip_range->ipr_entry);
182         INIT_LIST_HEAD(&ip_range->ipr_expr);
183
184         rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
185                                &ip_range->ipr_expr);
186         if (rc != 0)
187                 return LUSTRE_CFG_RC_BAD_PARAM;
188
189         list_add_tail(&ip_range->ipr_entry, list);
190
191         return LUSTRE_CFG_RC_NO_ERR;
192 }
193
194 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
195 {
196         char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
197              *intf_name;
198         struct lnet_dlc_intf_descr *intf_descr = NULL;
199         int rc;
200         char intf_string[LNET_MAX_STR_LEN];
201
202         if (len >= LNET_MAX_STR_LEN)
203                 return LUSTRE_CFG_RC_BAD_PARAM;
204
205         strncpy(intf_string, intf, len);
206         intf_string[len] = '\0';
207
208         intf_descr = calloc(1, sizeof(*intf_descr));
209         if (intf_descr == NULL)
210                 return LUSTRE_CFG_RC_OUT_OF_MEM;
211
212         INIT_LIST_HEAD(&intf_descr->intf_on_network);
213
214         intf_name = intf_string;
215         open_sq_bracket = strchr(intf_string, '[');
216         if (open_sq_bracket != NULL) {
217                 close_sq_bracket = strchr(intf_string, ']');
218                 if (close_sq_bracket == NULL) {
219                         free(intf_descr);
220                         return LUSTRE_CFG_RC_BAD_PARAM;
221                 }
222                 rc = cfs_expr_list_parse(open_sq_bracket,
223                                          strlen(open_sq_bracket), 0, UINT_MAX,
224                                          &intf_descr->cpt_expr);
225                 if (rc < 0) {
226                         free(intf_descr);
227                         return LUSTRE_CFG_RC_BAD_PARAM;
228                 }
229                 strncpy(intf_descr->intf_name, intf_name,
230                         open_sq_bracket - intf_name);
231                 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
232         } else {
233                 strcpy(intf_descr->intf_name, intf_name);
234                 intf_descr->cpt_expr = NULL;
235         }
236
237         list_add_tail(&intf_descr->intf_on_network, list);
238
239         return LUSTRE_CFG_RC_NO_ERR;
240 }
241
242 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
243 {
244         if (nw_descr != NULL) {
245                 nw_descr->nw_id = 0;
246                 INIT_LIST_HEAD(&nw_descr->network_on_rule);
247                 INIT_LIST_HEAD(&nw_descr->nw_intflist);
248         }
249 }
250
251 static char *get_next_delimiter_in_nid(char *str, char sep)
252 {
253         char *at, *comma;
254
255         /* first find the '@' */
256         at = strchr(str, '@');
257         if (!at)
258                 return str;
259
260         /* now that you found the at find the sep after */
261         comma = strchr(at, sep);
262         return comma;
263 }
264
265 int lustre_lnet_parse_nids(char *nids, char **array, int size,
266                            char ***out_array)
267 {
268         int num_nids = 0;
269         char *comma = nids, *cur, *entry;
270         char **new_array;
271         int i, len, start = 0, finish = 0;
272
273         if (nids == NULL || strlen(nids) == 0)
274                 return size;
275
276         /* count the number or new nids, by counting the number of comma*/
277         while (comma) {
278                 comma = get_next_delimiter_in_nid(comma, ',');
279                 if (comma) {
280                         comma++;
281                         num_nids++;
282                 } else {
283                         num_nids++;
284                 }
285         }
286
287         /*
288          * if the array is not NULL allocate a large enough array to house
289          * the old and new entries
290          */
291         new_array = calloc(sizeof(char*),
292                            (size > 0) ? size + num_nids : num_nids);
293
294         if (!new_array)
295                 goto failed;
296
297         /* parse our the new nids and add them to the tail of the array */
298         comma = nids;
299         cur = nids;
300         start = (size > 0) ? size: 0;
301         finish = (size > 0) ? size + num_nids : num_nids;
302         for (i = start; i < finish; i++) {
303                 comma = get_next_delimiter_in_nid(comma, ',');
304                 if (!comma)
305                         /*
306                          * the length of the string to be parsed out is
307                          * from cur to end of string. So it's good enough
308                          * to strlen(cur)
309                          */
310                         len = strlen(cur) + 1;
311                 else
312                         /* length of the string is comma - cur */
313                         len = (comma - cur) + 1;
314
315                 entry = calloc(1, len);
316                 if (!entry) {
317                         finish = i > 0 ? i - 1: 0;
318                         goto failed;
319                 }
320                 strncpy(entry, cur, len - 1);
321                 entry[len] = '\0';
322                 new_array[i] = entry;
323                 if (comma) {
324                         comma++;
325                         cur = comma;
326                 }
327         }
328
329         /* add the old entries in the array and delete the old array*/
330         for (i = 0; i < size; i++)
331                 new_array[i] = array[i];
332
333         if (array)
334                 free(array);
335
336         *out_array = new_array;
337
338         return finish;
339
340 failed:
341         for (i = start; i < finish; i++)
342                 free(new_array[i]);
343         if (new_array)
344                 free(new_array);
345
346         return size;
347 }
348
349 /*
350  * format expected:
351  *      <intf>[<expr>], <intf>[<expr>],..
352  */
353 int lustre_lnet_parse_interfaces(char *intf_str,
354                                  struct lnet_dlc_network_descr *nw_descr)
355 {
356         char *open_square;
357         char *close_square;
358         char *comma;
359         char *cur = intf_str, *next = NULL;
360         char *end = intf_str + strlen(intf_str);
361         int rc, len;
362         struct lnet_dlc_intf_descr *intf_descr, *tmp;
363
364         if (nw_descr == NULL)
365                 return LUSTRE_CFG_RC_BAD_PARAM;
366
367         while (cur < end) {
368                 open_square = strchr(cur, '[');
369                 if (open_square != NULL) {
370                         close_square = strchr(cur, ']');
371                         if (close_square == NULL) {
372                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
373                                 goto failed;
374                         }
375
376                         comma = strchr(cur, ',');
377                         if (comma != NULL && comma > close_square) {
378                                 next = comma + 1;
379                                 len = next - close_square;
380                         } else {
381                                 len = strlen(cur);
382                                 next = cur + len;
383                         }
384                 } else {
385                         comma = strchr(cur, ',');
386                         if (comma != NULL) {
387                                 next = comma + 1;
388                                 len = comma - cur;
389                         } else {
390                                 len = strlen(cur);
391                                 next = cur + len;
392                         }
393                 }
394
395                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
396                 if (rc != LUSTRE_CFG_RC_NO_ERR)
397                         goto failed;
398
399                 cur = next;
400         }
401
402         return LUSTRE_CFG_RC_NO_ERR;
403
404 failed:
405         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
406                                  intf_on_network) {
407                 list_del(&intf_descr->intf_on_network);
408                 free_intf_descr(intf_descr);
409         }
410
411         return rc;
412 }
413
414 int lustre_lnet_config_lib_init(void)
415 {
416         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
417 }
418
419 void lustre_lnet_config_lib_uninit(void)
420 {
421         unregister_ioc_dev(LNET_DEV_ID);
422 }
423
424 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
425                                  int seq_no, struct cYAML **err_rc)
426 {
427         struct libcfs_ioctl_data data;
428         unsigned int opc;
429         int rc;
430         char err_str[LNET_MAX_STR_LEN];
431
432         snprintf(err_str, sizeof(err_str), "\"Success\"");
433
434         LIBCFS_IOC_INIT(data);
435
436         /* Reverse logic is used here in order not to change
437          * the lctl utility */
438         data.ioc_flags = load_ni_from_mod ? 0 : 1;
439
440         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
441
442         rc = l_ioctl(LNET_DEV_ID, opc, &data);
443         if (rc != 0) {
444                 snprintf(err_str,
445                         sizeof(err_str),
446                         "\"LNet %s error: %s\"", (up) ? "configure" :
447                         "unconfigure", strerror(errno));
448                 rc = -errno;
449         }
450
451         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
452                           "lnet", err_str, err_rc);
453
454         return rc;
455 }
456
457 static int dispatch_peer_ni_cmd(lnet_nid_t pnid, lnet_nid_t nid, __u32 cmd,
458                                 struct lnet_ioctl_peer_cfg *data,
459                                 char *err_str, char *cmd_str)
460 {
461         int rc;
462
463         data->prcfg_prim_nid = pnid;
464         data->prcfg_cfg_nid = nid;
465
466         rc = l_ioctl(LNET_DEV_ID, cmd, data);
467         if (rc != 0) {
468                 rc = -errno;
469                 snprintf(err_str,
470                         LNET_MAX_STR_LEN,
471                         "\"cannot %s peer ni: %s\"",
472                         (cmd_str) ? cmd_str : "add", strerror(errno));
473                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
474         }
475
476         return rc;
477 }
478
479 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
480                           int seq_no, struct cYAML **show_rc,
481                           struct cYAML **err_rc)
482 {
483         void *data = NULL;
484         struct lnet_ioctl_ping_data ping;
485         struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
486                      *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
487         struct lnet_process_id id;
488         char err_str[LNET_MAX_STR_LEN] = {0};
489         char *sep, *token, *end;
490         char buf[6];
491         size_t len;
492         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
493         int i;
494         bool flag = false;
495
496         len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
497
498         data = calloc(1, len);
499         if (data == NULL)
500                 goto out;
501
502         /* create struct cYAML root object */
503         root = cYAML_create_object(NULL, NULL);
504         if (root == NULL)
505                 goto out;
506
507         ping_node = cYAML_create_seq(root, oper);
508         if (ping_node == NULL)
509                 goto out;
510
511         /* tokenise each nid in string ping_nids */
512         token = strtok(ping_nids, ",");
513
514         do {
515                 item = cYAML_create_seq_item(ping_node);
516                 if (item == NULL)
517                         goto out;
518
519                 if (first_seq == NULL)
520                         first_seq = item;
521
522                 /* check if '-' is a part of NID, token */
523                 sep = strchr(token, '-');
524                 if (sep == NULL) {
525                         id.pid = LNET_PID_ANY;
526                         /* if no net is specified, libcfs_str2nid() will assume tcp */
527                         id.nid = libcfs_str2nid(token);
528                         if (id.nid == LNET_NID_ANY) {
529                                 snprintf(err_str, sizeof(err_str),
530                                          "\"cannot parse NID '%s'\"",
531                                          token);
532                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
533                                 cYAML_build_error(rc, seq_no, MANAGE_CMD,
534                                                   oper, err_str, err_rc);
535                                 continue;
536                         }
537                 } else {
538                         if (token[0] == 'u' || token[0] == 'U')
539                                 id.pid = (strtoul(&token[1], &end, 0) |
540                                           (LNET_PID_USERFLAG));
541                         else
542                                 id.pid = strtoul(token, &end, 0);
543
544                         /* assuming '-' is part of hostname */
545                         if (end != sep) {
546                                 id.pid = LNET_PID_ANY;
547                                 id.nid = libcfs_str2nid(token);
548                                 if (id.nid == LNET_NID_ANY) {
549                                         snprintf(err_str, sizeof(err_str),
550                                                  "\"cannot parse NID '%s'\"",
551                                                  token);
552                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
553                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
554                                                           oper, err_str,
555                                                           err_rc);
556                                         continue;
557                                 }
558                         } else {
559                                 id.nid = libcfs_str2nid(sep + 1);
560                                 if (id.nid == LNET_NID_ANY) {
561                                         snprintf(err_str, sizeof(err_str),
562                                                  "\"cannot parse NID '%s'\"",
563                                                  token);
564                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
565                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
566                                                           oper, err_str,
567                                                           err_rc);
568                                         continue;
569                                 }
570                         }
571                 }
572                 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
573                 ping.ping_hdr.ioc_len = sizeof(ping);
574                 ping.ping_id          = id;
575                 ping.op_param         = param;
576                 ping.ping_count       = LNET_INTERFACES_MAX_DEFAULT;
577                 ping.ping_buf         = data;
578
579                 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
580                 if (rc != 0) {
581                         snprintf(err_str,
582                                  sizeof(err_str), "failed to %s %s: %s\n", oper,
583                                  id.pid == LNET_PID_ANY ?
584                                  libcfs_nid2str(id.nid) :
585                                  libcfs_id2str(id), strerror(errno));
586                         rc = LUSTRE_CFG_RC_BAD_PARAM;
587                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
588                                           oper, err_str, err_rc);
589                         continue;
590                 }
591
592                 if (cYAML_create_string(item, "primary nid",
593                                         libcfs_nid2str(ping.ping_id.nid)) == NULL)
594                         goto out;
595
596                 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
597                                         "True" : "False") == NULL)
598                         goto out;
599
600                 tmp = cYAML_create_seq(item, "peer ni");
601                 if (tmp == NULL)
602                         goto out;
603
604                 for (i = 0; i < ping.ping_count; i++) {
605                         if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
606                                     "0@lo"))
607                                 continue;
608                         peer_ni = cYAML_create_seq_item(tmp);
609                         if (peer_ni == NULL)
610                                 goto out;
611                         memset(buf, 0, sizeof buf);
612                         snprintf(buf, sizeof buf, "nid");
613                         if (cYAML_create_string(peer_ni, buf,
614                                                 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
615                                 goto out;
616                 }
617
618                 flag = true;
619
620         } while ((token = strtok(NULL, ",")) != NULL);
621
622         if (flag)
623                 rc = LUSTRE_CFG_RC_NO_ERR;
624
625 out:
626         if (data)
627                 free(data);
628         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
629                 cYAML_free_tree(root);
630         } else if (show_rc != NULL && *show_rc != NULL) {
631                 struct cYAML *show_node;
632                 show_node = cYAML_get_object_item(*show_rc, oper);
633                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
634                         cYAML_insert_child(show_node, first_seq);
635                         free(ping_node);
636                         free(root);
637                 } else if (show_node == NULL) {
638                         cYAML_insert_sibling((*show_rc)->cy_child,
639                                              ping_node);
640                         free(root);
641                 } else {
642                         cYAML_free_tree(root);
643                 }
644         } else {
645                 *show_rc = root;
646         }
647
648         return rc;
649 }
650
651 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
652                          struct cYAML **show_rc, struct cYAML **err_rc)
653 {
654         int rc;
655
656         rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
657                             seq_no, show_rc, err_rc);
658         return rc;
659 }
660
661 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
662                          struct cYAML **show_rc, struct cYAML **err_rc)
663 {
664         int rc;
665
666         rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
667                             seq_no, show_rc, err_rc);
668         return rc;
669 }
670
671 static void lustre_lnet_clean_ip2nets(struct lustre_lnet_ip2nets *ip2nets)
672 {
673         struct lustre_lnet_ip_range_descr *ipr, *tmp;
674         struct cfs_expr_list *el, *el_tmp;
675
676         list_for_each_entry_safe(ipr, tmp,
677                                  &ip2nets->ip2nets_ip_ranges,
678                                  ipr_entry) {
679                 list_del(&ipr->ipr_entry);
680                 list_for_each_entry_safe(el, el_tmp, &ipr->ipr_expr,
681                                          el_link) {
682                         list_del(&el->el_link);
683                         cfs_expr_list_free(el);
684                 }
685                 free(ipr);
686         }
687 }
688
689 /*
690  * returns an rc < 0 if there is an error
691  * otherwise it returns the number IPs generated
692  *  it also has out params: net - network name
693  */
694 static int lnet_expr2ips(char *nidstr, __u32 *ip_list,
695                          struct lustre_lnet_ip2nets *ip2nets,
696                          __u32 *net, char *err_str)
697 {
698         struct lustre_lnet_ip_range_descr *ipr;
699         char *comp1, *comp2;
700         int ip_idx = MAX_NUM_IPS - 1;
701         int ip_range_len, rc = LUSTRE_CFG_RC_NO_ERR;
702         __u32 net_type;
703         char ip_range[LNET_MAX_STR_LEN];
704
705         /* separate the two components of the NID */
706         comp1 = nidstr;
707         comp2 = strchr(nidstr, '@');
708         if (comp2 == NULL) {
709                 snprintf(err_str,
710                         LNET_MAX_STR_LEN,
711                         "\"cannot parse NID %s\"", nidstr);
712                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
713                 rc = LUSTRE_CFG_RC_BAD_PARAM;
714                 goto out;
715         }
716
717         /* length of the expected ip-range */
718         ip_range_len = comp2 - comp1;
719         if (ip_range_len >= LNET_MAX_STR_LEN) {
720                 snprintf(err_str,
721                         LNET_MAX_STR_LEN,
722                         "\"too long ip_range '%s'\"", nidstr);
723                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
724                 rc = LUSTRE_CFG_RC_BAD_PARAM;
725                 goto out;
726         }
727
728         /* move beyond '@' */
729         comp2++;
730
731         /*
732          * if the net component is either o2ib or tcp then we expect
733          * an IP range which could only be a single IP address.
734          * Parse that.
735          */
736         *net = libcfs_str2net(comp2);
737         net_type = LNET_NETTYP(*net);
738         /* expression support is for o2iblnd and socklnd only */
739         if (net_type != O2IBLND && net_type != SOCKLND)
740                 return LUSTRE_CFG_RC_SKIP;
741
742         strncpy(ip_range, comp1, ip_range_len);
743         ip_range[ip_range_len] = '\0';
744         ip2nets->ip2nets_net.nw_id = *net;
745
746         rc = lustre_lnet_add_ip_range(&ip2nets->ip2nets_ip_ranges, ip_range);
747         if (rc != LUSTRE_CFG_RC_NO_ERR) {
748                 snprintf(err_str,
749                         LNET_MAX_STR_LEN,
750                         "\"cannot parse ip_range '%.100s'\"", ip_range);
751                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
752                 rc = LUSTRE_CFG_RC_BAD_PARAM;
753                 goto out;
754         }
755
756         /*
757         * Generate all the IP Addresses from the parsed range. For sanity
758         * we allow only a max of MAX_NUM_IPS nids to be configured for
759         * a single peer.
760         */
761         list_for_each_entry(ipr, &ip2nets->ip2nets_ip_ranges, ipr_entry)
762                 ip_idx = cfs_ip_addr_range_gen(ip_list, MAX_NUM_IPS,
763                                                 &ipr->ipr_expr);
764
765         if (ip_idx == MAX_NUM_IPS - 1) {
766                 snprintf(err_str, LNET_MAX_STR_LEN,
767                                 "no NIDs provided for configuration");
768                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
769                 rc = LUSTRE_CFG_RC_NO_MATCH;
770                 goto out;
771         } else if (ip_idx == -1) {
772                 rc = LUSTRE_CFG_RC_LAST_ELEM;
773         } else {
774                 rc = ip_idx;
775         }
776
777 out:
778         return rc;
779 }
780
781 static int lustre_lnet_handle_peer_ip2nets(char **nid, int num_nids, bool mr,
782                                            bool range, __u32 cmd,
783                                            char *cmd_type, char *err_str)
784 {
785         __u32 net = LNET_NIDNET(LNET_NID_ANY);
786         int ip_idx;
787         int i, j, rc = LUSTRE_CFG_RC_NO_ERR;
788         __u32 ip_list[MAX_NUM_IPS];
789         struct lustre_lnet_ip2nets ip2nets;
790         struct lnet_ioctl_peer_cfg data;
791         lnet_nid_t peer_nid;
792         lnet_nid_t prim_nid = LNET_NID_ANY;
793
794         /* initialize all lists */
795         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
796         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
797         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
798
799         /* each nid entry is an expression */
800         for (i = 0; i < num_nids; i++) {
801                 if (!range && i == 0)
802                         prim_nid = libcfs_str2nid(nid[0]);
803                 else if (range)
804                         prim_nid = LNET_NID_ANY;
805
806                 rc = lnet_expr2ips(nid[i], ip_list, &ip2nets, &net, err_str);
807                 if (rc == LUSTRE_CFG_RC_SKIP)
808                         continue;
809                 else if (rc == LUSTRE_CFG_RC_LAST_ELEM)
810                         rc = -1;
811                 else if (rc < LUSTRE_CFG_RC_NO_ERR)
812                         goto out;
813
814                 ip_idx = rc;
815
816                 for (j = MAX_NUM_IPS - 1; j > ip_idx; j--) {
817                         peer_nid = LNET_MKNID(net, ip_list[j]);
818                         if (peer_nid == LNET_NID_ANY) {
819                                 snprintf(err_str,
820                                         LNET_MAX_STR_LEN,
821                                         "\"cannot parse NID\"");
822                                 err_str[LNET_MAX_STR_LEN - 1] = '\0';
823                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
824                                 goto out;
825                         }
826
827                         LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
828                         data.prcfg_mr = mr;
829
830                         if (prim_nid == LNET_NID_ANY && j == MAX_NUM_IPS - 1) {
831                                 prim_nid = peer_nid;
832                                 peer_nid = LNET_NID_ANY;
833                         }
834
835                         if (!range && num_nids > 1 && i == 0 &&
836                             cmd == IOC_LIBCFS_DEL_PEER_NI)
837                                 continue;
838                         else if (!range && i == 0)
839                                 peer_nid = LNET_NID_ANY;
840
841                         /*
842                         * If prim_nid is not provided then the first nid in the
843                         * list becomes the prim_nid. First time round the loop
844                         * use LNET_NID_ANY for the first parameter, then use
845                         * nid[0] as the key nid after wards
846                         */
847                         rc = dispatch_peer_ni_cmd(prim_nid, peer_nid, cmd,
848                                                   &data, err_str, cmd_type);
849                         if (rc != 0)
850                                 goto out;
851
852                         /*
853                          * we just deleted the entire peer using the
854                          * primary_nid. So don't bother iterating through
855                          * the rest of the nids
856                          */
857                         if (prim_nid != LNET_NID_ANY &&
858                             peer_nid == LNET_NID_ANY &&
859                             cmd == IOC_LIBCFS_DEL_PEER_NI)
860                                 goto next_nid;
861                 }
862 next_nid:
863                 lustre_lnet_clean_ip2nets(&ip2nets);
864         }
865
866 out:
867         lustre_lnet_clean_ip2nets(&ip2nets);
868         return rc;
869 }
870
871 int lustre_lnet_config_peer_nid(char *pnid, char **nid, int num_nids,
872                                 bool mr, bool ip2nets, int seq_no,
873                                 struct cYAML **err_rc)
874 {
875         int rc = LUSTRE_CFG_RC_NO_ERR;
876         char err_str[LNET_MAX_STR_LEN] = {0};
877         char **nid_array = NULL;
878
879         snprintf(err_str, sizeof(err_str), "\"Success\"");
880
881         if (ip2nets) {
882                 rc = lustre_lnet_handle_peer_ip2nets(nid, num_nids, mr,
883                                                 ip2nets, IOC_LIBCFS_ADD_PEER_NI,
884                                                 ADD_CMD, err_str);
885                 goto out;
886         }
887
888         if (pnid) {
889                 if (libcfs_str2nid(pnid) == LNET_NID_ANY) {
890                         snprintf(err_str, sizeof(err_str),
891                                  "bad primary NID: '%s'",
892                                  pnid);
893                         rc = LUSTRE_CFG_RC_MISSING_PARAM;
894                         goto out;
895                 }
896
897                 num_nids++;
898
899                 nid_array = calloc(sizeof(*nid_array), num_nids);
900                 if (!nid_array) {
901                         snprintf(err_str, sizeof(err_str),
902                                         "out of memory");
903                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
904                         goto out;
905                 }
906                 nid_array[0] = pnid;
907                 memcpy(&nid_array[1], nid, sizeof(*nid) * (num_nids - 1));
908         }
909
910         rc = lustre_lnet_handle_peer_ip2nets((pnid) ? nid_array : nid,
911                                              num_nids, mr, ip2nets,
912                                              IOC_LIBCFS_ADD_PEER_NI, ADD_CMD,
913                                              err_str);
914         if (rc)
915                 goto out;
916
917 out:
918         if (nid_array)
919                 free(nid_array);
920
921         cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
922         return rc;
923 }
924
925 int lustre_lnet_del_peer_nid(char *pnid, char **nid, int num_nids,
926                              bool ip2nets, int seq_no, struct cYAML **err_rc)
927 {
928         int rc = LUSTRE_CFG_RC_NO_ERR;
929         char err_str[LNET_MAX_STR_LEN] = {0};
930         char **nid_array = NULL;
931
932         snprintf(err_str, sizeof(err_str), "\"Success\"");
933
934         if (ip2nets) {
935                 rc = lustre_lnet_handle_peer_ip2nets(nid, num_nids, false,
936                                                 ip2nets, IOC_LIBCFS_DEL_PEER_NI,
937                                                 DEL_CMD, err_str);
938                 goto out;
939         }
940
941         if (pnid == NULL) {
942                 snprintf(err_str, sizeof(err_str),
943                          "\"Primary nid is not provided\"");
944                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
945                 goto out;
946         } else if (!ip2nets) {
947                 if (libcfs_str2nid(pnid) == LNET_NID_ANY) {
948                         rc = LUSTRE_CFG_RC_BAD_PARAM;
949                         snprintf(err_str, sizeof(err_str),
950                                  "bad key NID: '%s'",
951                                  pnid);
952                         goto out;
953                 }
954         }
955
956         num_nids++;
957         nid_array = calloc(sizeof(*nid_array), num_nids);
958         if (!nid_array) {
959                 snprintf(err_str, sizeof(err_str),
960                                 "out of memory");
961                 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
962                 goto out;
963         }
964         nid_array[0] = pnid;
965         memcpy(&nid_array[1], nid, sizeof(*nid) * (num_nids - 1));
966
967         rc = lustre_lnet_handle_peer_ip2nets(nid_array, num_nids, false,
968                                              ip2nets, IOC_LIBCFS_DEL_PEER_NI,
969                                              DEL_CMD, err_str);
970         if (rc)
971                 goto out;
972
973 out:
974         if (nid_array)
975                 free(nid_array);
976
977         cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
978         return rc;
979 }
980
981 int lustre_lnet_config_route(char *nw, char *gw, int hops, int prio,
982                              int seq_no, struct cYAML **err_rc)
983 {
984         struct lnet_ioctl_config_data data;
985         lnet_nid_t gateway_nid;
986         int rc = LUSTRE_CFG_RC_NO_ERR;
987         int ip_idx, i;
988         __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
989         __u32 net = LNET_NIDNET(LNET_NID_ANY);
990         char err_str[LNET_MAX_STR_LEN];
991         __u32 ip_list[MAX_NUM_IPS];
992         struct lustre_lnet_ip2nets ip2nets;
993
994         /* initialize all lists */
995         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
996         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
997         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
998
999         snprintf(err_str, sizeof(err_str), "\"Success\"");
1000
1001         if (nw == NULL || gw == NULL) {
1002                 snprintf(err_str,
1003                          sizeof(err_str),
1004                          "\"missing mandatory parameter in route config:'%s'\"",
1005                          (nw == NULL && gw == NULL) ? "network, gateway" :
1006                          (nw == NULL) ? "network" : "gateway");
1007                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1008                 goto out;
1009         }
1010
1011         rnet = libcfs_str2net(nw);
1012         if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
1013                 snprintf(err_str,
1014                          sizeof(err_str),
1015                          "\"cannot parse remote net %s\"", nw);
1016                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1017                 goto out;
1018         }
1019
1020         if (hops == -1) {
1021                 /* hops is undefined */
1022                 hops = LNET_UNDEFINED_HOPS;
1023         } else if (hops < 1 || hops > 255) {
1024                 snprintf(err_str,
1025                         sizeof(err_str),
1026                         "\"invalid hop count %d, must be between 1 and 255\"",
1027                         hops);
1028                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1029                 goto out;
1030         }
1031
1032         if (prio == -1) {
1033                 prio = 0;
1034         } else if (prio < 0) {
1035                 snprintf(err_str,
1036                          sizeof(err_str),
1037                         "\"invalid priority %d, must be greater than 0\"",
1038                         prio);
1039                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1040                 goto out;
1041         }
1042
1043         rc = lnet_expr2ips(gw, ip_list,
1044                            &ip2nets, &net, err_str);
1045         if (rc == LUSTRE_CFG_RC_LAST_ELEM)
1046                 rc = -1;
1047         else if (rc < LUSTRE_CFG_RC_NO_ERR)
1048                 goto out;
1049
1050         ip_idx = rc;
1051
1052         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1053         data.cfg_net = rnet;
1054         data.cfg_config_u.cfg_route.rtr_hop = hops;
1055         data.cfg_config_u.cfg_route.rtr_priority = prio;
1056
1057         for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
1058                 gateway_nid = LNET_MKNID(net, ip_list[i]);
1059                 if (gateway_nid == LNET_NID_ANY) {
1060                         snprintf(err_str,
1061                                 LNET_MAX_STR_LEN,
1062                                 "\"cannot form gateway NID: %u\"",
1063                                 ip_list[i]);
1064                         err_str[LNET_MAX_STR_LEN - 1] = '\0';
1065                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1066                         goto out;
1067                 }
1068                 data.cfg_nid = gateway_nid;
1069
1070                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE, &data);
1071                 if (rc != 0) {
1072                         rc = -errno;
1073                         snprintf(err_str,
1074                                 sizeof(err_str),
1075                                 "\"cannot add route: %s\"", strerror(errno));
1076                         goto out;
1077                 }
1078         }
1079 out:
1080         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
1081
1082         return rc;
1083 }
1084
1085 int lustre_lnet_del_route(char *nw, char *gw,
1086                           int seq_no, struct cYAML **err_rc)
1087 {
1088         struct lnet_ioctl_config_data data;
1089         lnet_nid_t gateway_nid;
1090         int rc = LUSTRE_CFG_RC_NO_ERR;
1091         __u32 rnet = LNET_NIDNET(LNET_NID_ANY);
1092         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1093         char err_str[LNET_MAX_STR_LEN];
1094         int ip_idx, i;
1095         __u32 ip_list[MAX_NUM_IPS];
1096         struct lustre_lnet_ip2nets ip2nets;
1097
1098         /* initialize all lists */
1099         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
1100         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
1101         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
1102
1103         snprintf(err_str, sizeof(err_str), "\"Success\"");
1104
1105         if (nw == NULL || gw == NULL) {
1106                 snprintf(err_str,
1107                          sizeof(err_str),
1108                          "\"missing mandatory parameter in route delete: '%s'\"",
1109                          (nw == NULL && gw == NULL) ? "network, gateway" :
1110                          (nw == NULL) ? "network" : "gateway");
1111                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1112                 goto out;
1113         }
1114
1115         rnet = libcfs_str2net(nw);
1116         if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
1117                 snprintf(err_str,
1118                          sizeof(err_str),
1119                          "\"cannot parse remote net '%s'\"", nw);
1120                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1121                 goto out;
1122         }
1123
1124         rc = lnet_expr2ips(gw, ip_list,
1125                            &ip2nets, &net, err_str);
1126         if (rc == LUSTRE_CFG_RC_LAST_ELEM)
1127                 rc = -1;
1128         else if (rc < LUSTRE_CFG_RC_NO_ERR)
1129                 goto out;
1130
1131         ip_idx = rc;
1132
1133         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1134         data.cfg_net = rnet;
1135
1136         for (i = MAX_NUM_IPS - 1; i > ip_idx; i--) {
1137                 gateway_nid = LNET_MKNID(net, ip_list[i]);
1138                 if (gateway_nid == LNET_NID_ANY) {
1139                         snprintf(err_str,
1140                                 LNET_MAX_STR_LEN,
1141                                 "\"cannot form gateway NID: %u\"",
1142                                 ip_list[i]);
1143                         err_str[LNET_MAX_STR_LEN - 1] = '\0';
1144                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1145                         goto out;
1146                 }
1147                 data.cfg_nid = gateway_nid;
1148
1149                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE, &data);
1150                 if (rc != 0) {
1151                         rc = -errno;
1152                         snprintf(err_str,
1153                                 sizeof(err_str),
1154                                 "\"cannot delete route: %s\"", strerror(errno));
1155                         goto out;
1156                 }
1157         }
1158 out:
1159         cYAML_build_error(rc, seq_no, DEL_CMD, "route", err_str, err_rc);
1160
1161         return rc;
1162 }
1163
1164 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
1165                            int seq_no, struct cYAML **show_rc,
1166                            struct cYAML **err_rc, bool backup)
1167 {
1168         struct lnet_ioctl_config_data data;
1169         lnet_nid_t gateway_nid;
1170         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1171         int l_errno = 0;
1172         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1173         int i;
1174         struct cYAML *root = NULL, *route = NULL, *item = NULL;
1175         struct cYAML *first_seq = NULL;
1176         char err_str[LNET_MAX_STR_LEN];
1177         bool exist = false;
1178
1179         snprintf(err_str, sizeof(err_str),
1180                  "\"out of memory\"");
1181
1182         if (nw != NULL) {
1183                 net = libcfs_str2net(nw);
1184                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1185                         snprintf(err_str,
1186                                  sizeof(err_str),
1187                                  "\"cannot parse net '%s'\"", nw);
1188                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1189                         goto out;
1190                 }
1191
1192         } else {
1193                 /* show all routes without filtering on net */
1194                 net = LNET_NIDNET(LNET_NID_ANY);
1195         }
1196
1197         if (gw != NULL) {
1198                 gateway_nid = libcfs_str2nid(gw);
1199                 if (gateway_nid == LNET_NID_ANY) {
1200                         snprintf(err_str,
1201                                  sizeof(err_str),
1202                                  "\"cannot parse gateway NID '%s'\"", gw);
1203                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1204                         goto out;
1205                 }
1206         } else
1207                 /* show all routes with out filtering on gateway */
1208                 gateway_nid = LNET_NID_ANY;
1209
1210         if ((hops < 1 && hops != -1) || hops > 255) {
1211                 snprintf(err_str,
1212                          sizeof(err_str),
1213                          "\"invalid hop count %d, must be between 0 and 256\"",
1214                          hops);
1215                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1216                 goto out;
1217         }
1218
1219         /* create struct cYAML root object */
1220         root = cYAML_create_object(NULL, NULL);
1221         if (root == NULL)
1222                 goto out;
1223
1224         route = cYAML_create_seq(root, "route");
1225         if (route == NULL)
1226                 goto out;
1227
1228         for (i = 0;; i++) {
1229                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1230                 data.cfg_count = i;
1231
1232                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1233                 if (rc != 0) {
1234                         l_errno = errno;
1235                         break;
1236                 }
1237
1238                 /* filter on provided data */
1239                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1240                     net != data.cfg_net)
1241                         continue;
1242
1243                 if (gateway_nid != LNET_NID_ANY &&
1244                     gateway_nid != data.cfg_nid)
1245                         continue;
1246
1247                 if (hops != -1 &&
1248                     hops != data.cfg_config_u.cfg_route.rtr_hop)
1249                         continue;
1250
1251                 if (prio != -1 &&
1252                     prio != data.cfg_config_u.cfg_route.rtr_priority)
1253                         continue;
1254
1255                 /* default rc to -1 incase we hit the goto */
1256                 rc = -1;
1257                 exist = true;
1258
1259                 item = cYAML_create_seq_item(route);
1260                 if (item == NULL)
1261                         goto out;
1262
1263                 if (first_seq == NULL)
1264                         first_seq = item;
1265
1266                 if (cYAML_create_string(item, "net",
1267                                         libcfs_net2str(data.cfg_net)) == NULL)
1268                         goto out;
1269
1270                 if (cYAML_create_string(item, "gateway",
1271                                         libcfs_nid2str(data.cfg_nid)) == NULL)
1272                         goto out;
1273
1274                 if (detail) {
1275                         if (cYAML_create_number(item, "hop",
1276                                                 (int) data.cfg_config_u.
1277                                                 cfg_route.rtr_hop) ==
1278                             NULL)
1279                                 goto out;
1280
1281                         if (cYAML_create_number(item, "priority",
1282                                                 data.cfg_config_u.
1283                                                 cfg_route.rtr_priority) == NULL)
1284                                 goto out;
1285
1286                         if (!backup &&
1287                             cYAML_create_string(item, "state",
1288                                                 data.cfg_config_u.cfg_route.
1289                                                         rtr_flags ?
1290                                                 "up" : "down") == NULL)
1291                                 goto out;
1292                 }
1293         }
1294
1295         /* print output iff show_rc is not provided */
1296         if (show_rc == NULL)
1297                 cYAML_print_tree(root);
1298
1299         if (l_errno != ENOENT) {
1300                 snprintf(err_str,
1301                          sizeof(err_str),
1302                          "\"cannot get routes: %s\"",
1303                          strerror(l_errno));
1304                 rc = -l_errno;
1305                 goto out;
1306         } else
1307                 rc = LUSTRE_CFG_RC_NO_ERR;
1308
1309         snprintf(err_str, sizeof(err_str), "\"success\"");
1310 out:
1311         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1312                 cYAML_free_tree(root);
1313         } else if (show_rc != NULL && *show_rc != NULL) {
1314                 struct cYAML *show_node;
1315                 /* find the route node, if one doesn't exist then
1316                  * insert one.  Otherwise add to the one there
1317                  */
1318                 show_node = cYAML_get_object_item(*show_rc, "route");
1319                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1320                         cYAML_insert_child(show_node, first_seq);
1321                         free(route);
1322                         free(root);
1323                 } else if (show_node == NULL) {
1324                         cYAML_insert_sibling((*show_rc)->cy_child,
1325                                                 route);
1326                         free(root);
1327                 } else {
1328                         cYAML_free_tree(root);
1329                 }
1330         } else {
1331                 *show_rc = root;
1332         }
1333
1334         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1335
1336         return rc;
1337 }
1338
1339 static int socket_intf_query(int request, char *intf,
1340                              struct ifreq *ifr)
1341 {
1342         int rc = 0;
1343         int sockfd;
1344
1345         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1346                 return LUSTRE_CFG_RC_BAD_PARAM;
1347
1348         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1349         if (sockfd < 0)
1350                 return LUSTRE_CFG_RC_BAD_PARAM;
1351
1352         strcpy(ifr->ifr_name, intf);
1353         rc = ioctl(sockfd, request, ifr);
1354         if (rc != 0)
1355                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1356
1357         close(sockfd);
1358
1359         return rc;
1360 }
1361
1362 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1363 {
1364         struct ifreq ifr;
1365         int rc;
1366
1367         memset(&ifr, 0, sizeof(ifr));
1368         rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1369         if (rc != 0)
1370                 return LUSTRE_CFG_RC_BAD_PARAM;
1371
1372         if ((ifr.ifr_flags & IFF_UP) == 0)
1373                 return LUSTRE_CFG_RC_BAD_PARAM;
1374
1375         memset(&ifr, 0, sizeof(ifr));
1376         rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1377         if (rc != 0)
1378                 return LUSTRE_CFG_RC_BAD_PARAM;
1379
1380         *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1381         *ip = bswap_32(*ip);
1382
1383         return LUSTRE_CFG_RC_NO_ERR;
1384 }
1385
1386 /*
1387  * for each interface in the array of interfaces find the IP address of
1388  * that interface, create its nid and add it to an array of NIDs.
1389  * Stop if any of the interfaces is down
1390  */
1391 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1392                                  lnet_nid_t **nids, __u32 *nnids,
1393                                  char *err_str, size_t str_len)
1394 {
1395         int i = 0, count = 0, rc;
1396         struct lnet_dlc_intf_descr *intf;
1397         char val[LNET_MAX_STR_LEN];
1398         __u32 ip;
1399         int gni_num;
1400         char *endp;
1401         unsigned int num;
1402
1403
1404         if (nw == NULL || nids == NULL) {
1405                 snprintf(err_str, str_len,
1406                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1407                 err_str[str_len - 1] = '\0';
1408                 return LUSTRE_CFG_RC_BAD_PARAM;
1409         }
1410
1411         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1412                 count = 1;
1413         } else {
1414                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1415                         count++;
1416         }
1417
1418         *nids = calloc(count, sizeof(lnet_nid_t));
1419         if (*nids == NULL) {
1420                 snprintf(err_str, str_len,
1421                          "\"out of memory\"");
1422                 err_str[str_len - 1] = '\0';
1423                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1424         }
1425         /*
1426          * special case the GNI interface since it doesn't have an IP
1427          * address. The assumption is that there can only be one GNI
1428          * interface in the system. No interface name is provided.
1429          */
1430         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1431                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1432                                 1, sizeof(val));
1433                 if (rc) {
1434                         snprintf(err_str, str_len,
1435                                  "\"cannot read gni nid\"");
1436                         err_str[str_len - 1] = '\0';
1437                         goto failed;
1438                 }
1439                 gni_num = atoi(val);
1440
1441                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1442
1443                 goto out;
1444         }
1445
1446         /* look at the other interfaces */
1447         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1448                 if (LNET_NETTYP(nw->nw_id) == PTL4LND) {
1449                         /* handle LNDs with numeric interface name */
1450                         num = strtoul(intf->intf_name, &endp, 0);
1451                         if (endp == intf->intf_name || *endp != '\0') {
1452                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1453                                 snprintf(err_str, str_len,
1454                                          "\"couldn't query intf %s\"",
1455                                          intf->intf_name);
1456                                 err_str[str_len - 1] = '\0';
1457                                 goto failed;
1458                         }
1459                         (*nids)[i] = LNET_MKNID(nw->nw_id, num);
1460                         i++;
1461                 } else {
1462                         /* handle LNDs with ip interface name */
1463                         rc = lustre_lnet_queryip(intf, &ip);
1464                         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1465                                 snprintf(err_str, str_len,
1466                                          "\"couldn't query intf %s\"",
1467                                          intf->intf_name);
1468                                 err_str[str_len - 1] = '\0';
1469                                 goto failed;
1470                         }
1471                         (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1472                         i++;
1473                 }
1474         }
1475
1476 out:
1477         *nnids = count;
1478
1479         return 0;
1480
1481 failed:
1482         free(*nids);
1483         *nids = NULL;
1484         return rc;
1485 }
1486
1487 /*
1488  * called repeatedly until a match or no more ip range
1489  * What do you have?
1490  *      ip_range expression
1491  *      interface list with all the interface names.
1492  *      all the interfaces in the system.
1493  *
1494  *      try to match the ip_range expr to one of the interfaces' IPs in
1495  *      the system. If we hit a patch for an interface. Check if that
1496  *      interface name is in the list.
1497  *
1498  *      If there are more than one interface in the list, then make sure
1499  *      that the IPs for all of these interfaces match the ip ranges
1500  *      given.
1501  *
1502  *      for each interface in intf_list
1503  *              look up the intf name in ifa
1504  *              if not there then no match
1505  *              check ip obtained from ifa against a match to any of the
1506  *              ip_ranges given.
1507  *              If no match, then fail
1508  *
1509  *      The result is that all the interfaces have to match.
1510  */
1511 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1512                                  struct list_head *intf_list,
1513                                  struct list_head *ip_ranges)
1514 {
1515         int rc;
1516         __u32 ip;
1517         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1518         struct ifaddrs *ifaddr = ifa;
1519         struct lustre_lnet_ip_range_descr *ip_range;
1520         int family;
1521
1522         /*
1523          * if there are no explicit interfaces, and no ip ranges, then
1524          * configure the first tcp interface we encounter.
1525          */
1526         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1527                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1528                         if (ifaddr->ifa_addr == NULL)
1529                                 continue;
1530
1531                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1532                                 continue;
1533
1534                         family = ifaddr->ifa_addr->sa_family;
1535                         if (family == AF_INET &&
1536                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1537                                 rc = lustre_lnet_add_intf_descr
1538                                         (intf_list, ifaddr->ifa_name,
1539                                         strlen(ifaddr->ifa_name));
1540
1541                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1542                                         return rc;
1543
1544                                 return LUSTRE_CFG_RC_MATCH;
1545                         }
1546                 }
1547                 return LUSTRE_CFG_RC_NO_MATCH;
1548         }
1549
1550         /*
1551          * First interface which matches an IP pattern will be used
1552          */
1553         if (list_empty(intf_list)) {
1554                 /*
1555                  * no interfaces provided in the rule, but an ip range is
1556                  * provided, so try and match an interface to the ip
1557                  * range.
1558                  */
1559                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1560                         if (ifaddr->ifa_addr == NULL)
1561                                 continue;
1562
1563                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1564                                 continue;
1565
1566                         family = ifaddr->ifa_addr->sa_family;
1567                         if (family == AF_INET) {
1568                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1569                                         sin_addr.s_addr;
1570
1571                                 list_for_each_entry(ip_range, ip_ranges,
1572                                                     ipr_entry) {
1573                                         rc = cfs_ip_addr_match(bswap_32(ip),
1574                                                         &ip_range->ipr_expr);
1575                                         if (!rc)
1576                                                 continue;
1577
1578                                         rc = lustre_lnet_add_intf_descr
1579                                           (intf_list, ifaddr->ifa_name,
1580                                            strlen(ifaddr->ifa_name));
1581
1582                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1583                                                 return rc;
1584                                 }
1585                         }
1586                 }
1587
1588                 if (!list_empty(intf_list))
1589                         return LUSTRE_CFG_RC_MATCH;
1590
1591                 return LUSTRE_CFG_RC_NO_MATCH;
1592         }
1593
1594         /*
1595          * If an interface is explicitly specified the ip-range might or
1596          * might not be specified. if specified the interface needs to match the
1597          * ip-range. If no ip-range then the interfaces are
1598          * automatically matched if they are all up.
1599          * If > 1 interfaces all the interfaces must match for the NI to
1600          * be configured.
1601          */
1602         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1603                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1604                         if (ifaddr->ifa_addr == NULL)
1605                                 continue;
1606
1607                         family = ifaddr->ifa_addr->sa_family;
1608                         if (family == AF_INET &&
1609                             strcmp(intf_descr->intf_name,
1610                                    ifaddr->ifa_name) == 0)
1611                                 break;
1612                 }
1613
1614                 if (ifaddr == NULL) {
1615                         list_del(&intf_descr->intf_on_network);
1616                         free_intf_descr(intf_descr);
1617                         continue;
1618                 }
1619
1620                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1621                         list_del(&intf_descr->intf_on_network);
1622                         free_intf_descr(intf_descr);
1623                         continue;
1624                 }
1625
1626                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1627
1628                 rc = 1;
1629                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1630                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1631                         if (rc)
1632                                 break;
1633                 }
1634
1635                 if (!rc) {
1636                         /* no match for this interface */
1637                         list_del(&intf_descr->intf_on_network);
1638                         free_intf_descr(intf_descr);
1639                 }
1640         }
1641
1642         return LUSTRE_CFG_RC_MATCH;
1643 }
1644
1645 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1646                                             lnet_nid_t **nids, __u32 *nnids,
1647                                             char *err_str, size_t str_len)
1648 {
1649         struct ifaddrs *ifa;
1650         int rc = LUSTRE_CFG_RC_NO_ERR;
1651
1652         rc = getifaddrs(&ifa);
1653         if (rc < 0) {
1654                 snprintf(err_str, str_len,
1655                          "\"failed to get interface addresses: %d\"", -errno);
1656                 err_str[str_len - 1] = '\0';
1657                 return -errno;
1658         }
1659
1660         rc = lustre_lnet_match_ip_to_intf(ifa,
1661                                           &ip2nets->ip2nets_net.nw_intflist,
1662                                           &ip2nets->ip2nets_ip_ranges);
1663         if (rc != LUSTRE_CFG_RC_MATCH) {
1664                 snprintf(err_str, str_len,
1665                          "\"couldn't match ip to existing interfaces\"");
1666                 err_str[str_len - 1] = '\0';
1667                 freeifaddrs(ifa);
1668                 return rc;
1669         }
1670
1671         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1672                                    err_str, sizeof(err_str));
1673         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1674                 *nids = NULL;
1675                 *nnids = 0;
1676         }
1677
1678         freeifaddrs(ifa);
1679
1680         return rc;
1681 }
1682
1683 static int
1684 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1685                             struct lnet_ioctl_config_lnd_tunables *tunables,
1686                             struct cfs_expr_list *global_cpts,
1687                             lnet_nid_t *nids, char *err_str)
1688 {
1689         char *data;
1690         struct lnet_ioctl_config_ni *conf;
1691         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1692         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1693         size_t len;
1694         int count;
1695         struct lnet_dlc_intf_descr *intf_descr;
1696         __u32 *cpt_array;
1697         struct cfs_expr_list *cpt_expr;
1698
1699         list_for_each_entry(intf_descr, intf_list,
1700                             intf_on_network) {
1701                 if (tunables != NULL)
1702                         len = sizeof(struct lnet_ioctl_config_ni) +
1703                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1704                 else
1705                         len = sizeof(struct lnet_ioctl_config_ni);
1706
1707                 data = calloc(1, len);
1708                 if (!data)
1709                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1710                 conf = (struct lnet_ioctl_config_ni*) data;
1711                 if (tunables != NULL)
1712                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1713                                 conf->lic_bulk;
1714
1715                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1716                 conf->lic_cfg_hdr.ioc_len = len;
1717                 conf->lic_nid = nids[i];
1718                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1719                         LNET_MAX_STR_LEN);
1720
1721                 if (intf_descr->cpt_expr != NULL)
1722                         cpt_expr = intf_descr->cpt_expr;
1723                 else if (global_cpts != NULL)
1724                         cpt_expr = global_cpts;
1725                 else
1726                         cpt_expr = NULL;
1727
1728                 if (cpt_expr != NULL) {
1729                         count = cfs_expr_list_values(cpt_expr,
1730                                                      LNET_MAX_SHOW_NUM_CPT,
1731                                                      &cpt_array);
1732                         if (count > 0) {
1733                                 memcpy(conf->lic_cpts, cpt_array,
1734                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1735                                 free(cpt_array);
1736                         } else {
1737                                 count = 0;
1738                         }
1739                 } else {
1740                         count = 0;
1741                 }
1742
1743                 conf->lic_ncpts = count;
1744
1745                 if (tunables != NULL)
1746                         memcpy(tun, tunables, sizeof(*tunables));
1747
1748                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1749                 if (rc < 0) {
1750                         rc = -errno;
1751                         snprintf(err_str,
1752                                  LNET_MAX_STR_LEN,
1753                                  "\"cannot add network: %s\"", strerror(errno));
1754                         free(data);
1755                         return rc;
1756                 }
1757                 free(data);
1758                 i++;
1759         }
1760
1761         return LUSTRE_CFG_RC_NO_ERR;
1762 }
1763
1764 int
1765 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1766                            struct lnet_ioctl_config_lnd_tunables *tunables,
1767                            struct cfs_expr_list *global_cpts,
1768                            int seq_no, struct cYAML **err_rc)
1769 {
1770         lnet_nid_t *nids = NULL;
1771         __u32 nnids = 0;
1772         int rc;
1773         char err_str[LNET_MAX_STR_LEN];
1774
1775         snprintf(err_str, sizeof(err_str), "\"success\"");
1776
1777         if (!ip2nets) {
1778                 snprintf(err_str,
1779                          sizeof(err_str),
1780                          "\"incomplete ip2nets information\"");
1781                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1782                 goto out;
1783         }
1784
1785         /*
1786          * call below function to resolve the rules into a list of nids.
1787          * The memory is allocated in that function then freed here when
1788          * it's no longer needed.
1789          */
1790         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1791                                               sizeof(err_str));
1792         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1793                 goto out;
1794
1795         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1796                 snprintf(err_str, sizeof(err_str),
1797                          "\"no interfaces match ip2nets rules\"");
1798                 goto free_nids_out;
1799         }
1800
1801         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1802                                          tunables, global_cpts, nids,
1803                                          err_str);
1804
1805 free_nids_out:
1806         free(nids);
1807
1808 out:
1809         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1810         return rc;
1811 }
1812
1813 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1814                           struct cfs_expr_list *global_cpts,
1815                           char *ip2net,
1816                           struct lnet_ioctl_config_lnd_tunables *tunables,
1817                           int seq_no, struct cYAML **err_rc)
1818 {
1819         char *data = NULL;
1820         struct lnet_ioctl_config_ni *conf;
1821         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1822         char buf[LNET_MAX_STR_LEN];
1823         int rc = LUSTRE_CFG_RC_NO_ERR;
1824         char err_str[LNET_MAX_STR_LEN];
1825         lnet_nid_t *nids = NULL;
1826         __u32 nnids = 0;
1827         size_t len;
1828         int count;
1829         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1830         __u32 *cpt_array;
1831
1832         snprintf(err_str, sizeof(err_str), "\"success\"");
1833
1834         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1835             (list_empty(&nw_descr->nw_intflist) &&
1836              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1837                 snprintf(err_str,
1838                          sizeof(err_str),
1839                          "\"missing mandatory parameters in NI config: '%s'\"",
1840                          (nw_descr == NULL) ? "network , interface" :
1841                          (nw_descr->nw_id == 0) ? "network" : "interface");
1842                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1843                 goto out;
1844         }
1845
1846         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1847                 snprintf(err_str,
1848                          sizeof(err_str),
1849                          "\"ip2net string too long %d\"",
1850                                 (int)strlen(ip2net));
1851                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1852                 goto out;
1853         }
1854
1855         if (ip2net != NULL) {
1856                 if (tunables != NULL)
1857                         len = sizeof(struct lnet_ioctl_config_ni) +
1858                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1859                 else
1860                         len = sizeof(struct lnet_ioctl_config_ni);
1861                 data = calloc(1, len);
1862                 if (!data) {
1863                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1864                         goto out;
1865                 }
1866                 conf = (struct lnet_ioctl_config_ni*) data;
1867                 if (tunables != NULL)
1868                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1869                                 (data + sizeof(*conf));
1870
1871                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1872                 conf->lic_cfg_hdr.ioc_len = len;
1873                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1874                         LNET_MAX_STR_LEN);
1875
1876                 if (global_cpts != NULL) {
1877                         count = cfs_expr_list_values(global_cpts,
1878                                                      LNET_MAX_SHOW_NUM_CPT,
1879                                                      &cpt_array);
1880                         if (count > 0) {
1881                                 memcpy(conf->lic_cpts, cpt_array,
1882                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1883                                 free(cpt_array);
1884                         } else {
1885                                 count = 0;
1886                         }
1887                 } else {
1888                         count = 0;
1889                 }
1890
1891                 conf->lic_ncpts = count;
1892
1893                 if (tunables != NULL)
1894                         memcpy(tun, tunables, sizeof(*tunables));
1895
1896                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1897                 if (rc < 0) {
1898                         rc = -errno;
1899                         snprintf(err_str,
1900                                 sizeof(err_str),
1901                                 "\"cannot add network: %s\"", strerror(errno));
1902                         goto out;
1903                 }
1904
1905                 goto out;
1906         }
1907
1908         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1909                 rc = LUSTRE_CFG_RC_NO_ERR;
1910                 goto out;
1911         }
1912
1913         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1914                 snprintf(err_str,
1915                         sizeof(err_str),
1916                         "\"cannot parse net '%s'\"",
1917                         libcfs_net2str(nw_descr->nw_id));
1918                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1919                 goto out;
1920         }
1921
1922         /*
1923          * special case the GNI since no interface name is expected
1924          */
1925         if (list_empty(&nw_descr->nw_intflist) &&
1926             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1927                 snprintf(err_str,
1928                         sizeof(err_str),
1929                         "\"no interface name provided\"");
1930                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1931                 goto out;
1932         }
1933
1934         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1935                                    err_str, sizeof(err_str));
1936         if (rc != 0) {
1937                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1938                 goto out;
1939         }
1940
1941         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1942                                          tunables, global_cpts, nids,
1943                                          err_str);
1944
1945 out:
1946         if (nw_descr != NULL) {
1947                 list_for_each_entry_safe(intf_descr, tmp,
1948                                          &nw_descr->nw_intflist,
1949                                          intf_on_network) {
1950                         list_del(&intf_descr->intf_on_network);
1951                         free_intf_descr(intf_descr);
1952                 }
1953         }
1954
1955         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1956
1957         if (nids)
1958                 free(nids);
1959
1960         if (data)
1961                 free(data);
1962
1963         return rc;
1964 }
1965
1966 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1967                        int seq_no, struct cYAML **err_rc)
1968 {
1969         struct lnet_ioctl_config_ni data;
1970         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1971         char err_str[LNET_MAX_STR_LEN];
1972         lnet_nid_t *nids = NULL;
1973         __u32 nnids = 0;
1974         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1975
1976         snprintf(err_str, sizeof(err_str), "\"success\"");
1977
1978         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1979                 snprintf(err_str,
1980                          sizeof(err_str),
1981                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1982                          (nw_descr == NULL) ? "network , interface" :
1983                          (nw_descr->nw_id == 0) ? "network" : "interface");
1984                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1985                 goto out;
1986         }
1987
1988         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1989                 return LUSTRE_CFG_RC_NO_ERR;
1990
1991         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1992                 snprintf(err_str,
1993                          sizeof(err_str),
1994                          "\"cannot parse net '%s'\"",
1995                          libcfs_net2str(nw_descr->nw_id));
1996                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1997                 goto out;
1998         }
1999
2000         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
2001                                    err_str, sizeof(err_str));
2002         if (rc != 0) {
2003                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2004                 goto out;
2005         }
2006
2007         /*
2008          * no interfaces just the nw_id is specified
2009          */
2010         if (nnids == 0) {
2011                 nids = calloc(1, sizeof(*nids));
2012                 if (nids == NULL) {
2013                         snprintf(err_str, sizeof(err_str),
2014                                 "\"out of memory\"");
2015                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2016                         goto out;
2017                 }
2018                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
2019                 nnids = 1;
2020         }
2021
2022         for (i = 0; i < nnids; i++) {
2023                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
2024                 data.lic_nid = nids[i];
2025
2026                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
2027                 if (rc < 0) {
2028                         rc = -errno;
2029                         snprintf(err_str,
2030                                 sizeof(err_str),
2031                                 "\"cannot del network: %s\"", strerror(errno));
2032                 }
2033         }
2034
2035         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2036                                  intf_on_network) {
2037                 list_del(&intf_descr->intf_on_network);
2038                 free_intf_descr(intf_descr);
2039         }
2040
2041 out:
2042         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
2043
2044         if (nids != NULL)
2045                 free(nids);
2046
2047         return rc;
2048 }
2049
2050 static int
2051 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
2052                            enum lnet_health_type type, char *name,
2053                            int seq_no, struct cYAML **err_rc)
2054 {
2055         struct lnet_ioctl_reset_health_cfg data;
2056         int rc = LUSTRE_CFG_RC_NO_ERR;
2057         char err_str[LNET_MAX_STR_LEN];
2058
2059         snprintf(err_str, sizeof(err_str), "\"success\"");
2060
2061         LIBCFS_IOC_INIT_V2(data, rh_hdr);
2062         data.rh_type = type;
2063         data.rh_all = all;
2064         data.rh_value = value;
2065         data.rh_nid = nid;
2066
2067         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
2068         if (rc != 0) {
2069                 rc = -errno;
2070                 snprintf(err_str,
2071                          sizeof(err_str), "Can not configure health value: %s",
2072                          strerror(errno));
2073         }
2074
2075         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2076
2077         return rc;
2078 }
2079
2080 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
2081                                   struct cYAML **err_rc)
2082 {
2083         lnet_nid_t nid;
2084         if (ni_nid)
2085                 nid = libcfs_str2nid(ni_nid);
2086         else
2087                 nid = LNET_NID_ANY;
2088         return lustre_lnet_config_healthv(value, all, nid,
2089                                           LNET_HEALTH_TYPE_LOCAL_NI,
2090                                           "ni healthv", seq_no, err_rc);
2091 }
2092
2093 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
2094                                        int seq_no, struct cYAML **err_rc)
2095 {
2096         lnet_nid_t nid;
2097         if (lpni_nid)
2098                 nid = libcfs_str2nid(lpni_nid);
2099         else
2100                 nid = LNET_NID_ANY;
2101         return lustre_lnet_config_healthv(value, all, nid,
2102                                           LNET_HEALTH_TYPE_PEER_NI,
2103                                           "peer_ni healthv", seq_no, err_rc);
2104 }
2105
2106 static bool
2107 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
2108                           struct lnet_ioctl_comm_count *counts)
2109 {
2110         if (cYAML_create_number(yaml, "put",
2111                                 counts->ico_put_count)
2112                                         == NULL)
2113                 return false;
2114         if (cYAML_create_number(yaml, "get",
2115                                 counts->ico_get_count)
2116                                         == NULL)
2117                 return false;
2118         if (cYAML_create_number(yaml, "reply",
2119                                 counts->ico_reply_count)
2120                                         == NULL)
2121                 return false;
2122         if (cYAML_create_number(yaml, "ack",
2123                                 counts->ico_ack_count)
2124                                         == NULL)
2125                 return false;
2126         if (cYAML_create_number(yaml, "hello",
2127                                 counts->ico_hello_count)
2128                                         == NULL)
2129                 return false;
2130
2131         return true;
2132 }
2133
2134 static struct lnet_ioctl_comm_count *
2135 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
2136 {
2137         if (idx == 0)
2138                 return &msg_stats->im_send_stats;
2139         if (idx == 1)
2140                 return &msg_stats->im_recv_stats;
2141         if (idx == 2)
2142                 return &msg_stats->im_drop_stats;
2143
2144         return NULL;
2145 }
2146
2147 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
2148                          struct cYAML **show_rc, struct cYAML **err_rc,
2149                          bool backup)
2150 {
2151         char *buf;
2152         struct lnet_ioctl_config_ni *ni_data;
2153         struct lnet_ioctl_config_lnd_tunables *lnd;
2154         struct lnet_ioctl_element_stats *stats;
2155         struct lnet_ioctl_element_msg_stats msg_stats;
2156         struct lnet_ioctl_local_ni_hstats hstats;
2157         __u32 net = LNET_NIDNET(LNET_NID_ANY);
2158         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
2159         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
2160         int l_errno = 0;
2161         struct cYAML *root = NULL, *tunables = NULL,
2162                 *net_node = NULL, *interfaces = NULL,
2163                 *item = NULL, *first_seq = NULL,
2164                 *tmp = NULL, *statistics = NULL,
2165                 *yhstats = NULL;
2166         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
2167         char str_buf[str_buf_len];
2168         char *pos;
2169         char err_str[LNET_MAX_STR_LEN];
2170         bool exist = false, new_net = true;
2171         int net_num = 0;
2172         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
2173
2174         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2175
2176         buf = calloc(1, buf_size);
2177         if (buf == NULL)
2178                 goto out;
2179
2180         ni_data = (struct lnet_ioctl_config_ni *)buf;
2181
2182         if (nw != NULL) {
2183                 net = libcfs_str2net(nw);
2184                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
2185                         snprintf(err_str,
2186                                  sizeof(err_str),
2187                                  "\"cannot parse net '%s'\"", nw);
2188                         rc = LUSTRE_CFG_RC_BAD_PARAM;
2189                         goto out;
2190                 }
2191         }
2192
2193         root = cYAML_create_object(NULL, NULL);
2194         if (root == NULL)
2195                 goto out;
2196
2197         net_node = cYAML_create_seq(root, "net");
2198         if (net_node == NULL)
2199                 goto out;
2200
2201         for (i = 0;; i++) {
2202                 pos = str_buf;
2203                 __u32 rc_net;
2204
2205                 memset(buf, 0, buf_size);
2206
2207                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
2208                 /*
2209                  * set the ioc_len to the proper value since INIT assumes
2210                  * size of data
2211                  */
2212                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
2213                 ni_data->lic_idx = i;
2214
2215                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
2216                 if (rc != 0) {
2217                         l_errno = errno;
2218                         break;
2219                 }
2220
2221                 rc_net = LNET_NIDNET(ni_data->lic_nid);
2222
2223                 /* filter on provided data */
2224                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
2225                     net != rc_net)
2226                         continue;
2227
2228                 /* if we're backing up don't store lo */
2229                 if (backup && LNET_NETTYP(rc_net) == LOLND)
2230                         continue;
2231
2232                 /* default rc to -1 in case we hit the goto */
2233                 rc = -1;
2234                 exist = true;
2235
2236                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
2237                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
2238                         (ni_data->lic_bulk + sizeof(*stats));
2239
2240                 if (rc_net != prev_net) {
2241                         prev_net = rc_net;
2242                         new_net = true;
2243                         net_num++;
2244                 }
2245
2246                 if (new_net) {
2247                         if (!cYAML_create_string(net_node, "net type",
2248                                                  libcfs_net2str(rc_net)))
2249                                 goto out;
2250
2251                         tmp = cYAML_create_seq(net_node, "local NI(s)");
2252                         if (tmp == NULL)
2253                                 goto out;
2254                         new_net = false;
2255                 }
2256
2257                 /* create the tree to be printed. */
2258                 item = cYAML_create_seq_item(tmp);
2259                 if (item == NULL)
2260                         goto out;
2261
2262                 if (first_seq == NULL)
2263                         first_seq = item;
2264
2265                 if (!backup &&
2266                     cYAML_create_string(item, "nid",
2267                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2268                         goto out;
2269
2270                 if (!backup &&
2271                     cYAML_create_string(item,
2272                                         "status",
2273                                         (ni_data->lic_status ==
2274                                           LNET_NI_STATUS_UP) ?
2275                                             "up" : "down") == NULL)
2276                         goto out;
2277
2278                 /* don't add interfaces unless there is at least one
2279                  * interface */
2280                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2281                         interfaces = cYAML_create_object(item, "interfaces");
2282                         if (interfaces == NULL)
2283                                 goto out;
2284
2285                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2286                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2287                                         snprintf(str_buf,
2288                                                  sizeof(str_buf), "%d", j);
2289                                         if (cYAML_create_string(interfaces,
2290                                                 str_buf,
2291                                                 ni_data->lic_ni_intf[j]) ==
2292                                                         NULL)
2293                                                 goto out;
2294                                 }
2295                         }
2296                 }
2297
2298                 if (detail) {
2299                         char *limit;
2300                         int k;
2301
2302                         if (backup)
2303                                 goto continue_without_msg_stats;
2304
2305                         statistics = cYAML_create_object(item, "statistics");
2306                         if (statistics == NULL)
2307                                 goto out;
2308
2309                         if (cYAML_create_number(statistics, "send_count",
2310                                                 stats->iel_send_count)
2311                                                         == NULL)
2312                                 goto out;
2313
2314                         if (cYAML_create_number(statistics, "recv_count",
2315                                                 stats->iel_recv_count)
2316                                                         == NULL)
2317                                 goto out;
2318
2319                         if (cYAML_create_number(statistics, "drop_count",
2320                                                 stats->iel_drop_count)
2321                                                         == NULL)
2322                                 goto out;
2323
2324                         if (detail < 2)
2325                                 goto continue_without_msg_stats;
2326
2327                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2328                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2329                         msg_stats.im_idx = i;
2330
2331                         rc = l_ioctl(LNET_DEV_ID,
2332                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2333                                      &msg_stats);
2334                         if (rc != 0) {
2335                                 l_errno = errno;
2336                                 goto continue_without_msg_stats;
2337                         }
2338
2339                         for (k = 0; k < 3; k++) {
2340                                 struct lnet_ioctl_comm_count *counts;
2341                                 struct cYAML *msg_statistics = NULL;
2342
2343                                 msg_statistics = cYAML_create_object(item,
2344                                                  (char *)gmsg_stat_names[k]);
2345                                 if (msg_statistics == NULL)
2346                                         goto out;
2347
2348                                 counts = get_counts(&msg_stats, k);
2349                                 if (counts == NULL)
2350                                         goto out;
2351
2352                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2353                                                                counts))
2354                                         goto out;
2355                         }
2356
2357                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2358                         hstats.hlni_nid = ni_data->lic_nid;
2359                         /* grab health stats */
2360                         rc = l_ioctl(LNET_DEV_ID,
2361                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2362                                      &hstats);
2363                         if (rc != 0) {
2364                                 l_errno = errno;
2365                                 goto continue_without_msg_stats;
2366                         }
2367                         yhstats = cYAML_create_object(item, "health stats");
2368                         if (!yhstats)
2369                                 goto out;
2370                         if (cYAML_create_number(yhstats, "health value",
2371                                                 hstats.hlni_health_value)
2372                                                         == NULL)
2373                                 goto out;
2374                         if (cYAML_create_number(yhstats, "interrupts",
2375                                                 hstats.hlni_local_interrupt)
2376                                                         == NULL)
2377                                 goto out;
2378                         if (cYAML_create_number(yhstats, "dropped",
2379                                                 hstats.hlni_local_dropped)
2380                                                         == NULL)
2381                                 goto out;
2382                         if (cYAML_create_number(yhstats, "aborted",
2383                                                 hstats.hlni_local_aborted)
2384                                                         == NULL)
2385                                 goto out;
2386                         if (cYAML_create_number(yhstats, "no route",
2387                                                 hstats.hlni_local_no_route)
2388                                                         == NULL)
2389                                 goto out;
2390                         if (cYAML_create_number(yhstats, "timeouts",
2391                                                 hstats.hlni_local_timeout)
2392                                                         == NULL)
2393                                 goto out;
2394                         if (cYAML_create_number(yhstats, "error",
2395                                                 hstats.hlni_local_error)
2396                                                         == NULL)
2397                                 goto out;
2398
2399 continue_without_msg_stats:
2400                         tunables = cYAML_create_object(item, "tunables");
2401                         if (!tunables)
2402                                 goto out;
2403
2404                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2405                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2406                                 goto out;
2407
2408                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2409                                                      &lnd->lt_tun);
2410                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2411                             rc != LUSTRE_CFG_RC_NO_MATCH)
2412                                 goto out;
2413
2414                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2415                                 tunables = cYAML_create_object(item,
2416                                                                "lnd tunables");
2417                                 if (tunables == NULL)
2418                                         goto out;
2419                         }
2420
2421                         if (!backup &&
2422                             cYAML_create_number(item, "dev cpt",
2423                                                 ni_data->lic_dev_cpt) == NULL)
2424                                 goto out;
2425
2426                         if (!backup &&
2427                             cYAML_create_number(item, "tcp bonding",
2428                                                 ni_data->lic_tcp_bonding)
2429                                                         == NULL)
2430                                 goto out;
2431
2432                         /* out put the CPTs in the format: "[x,x,x,...]" */
2433                         limit = str_buf + str_buf_len - 3;
2434                         pos += snprintf(pos, limit - pos, "\"[");
2435                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2436                                 j < ni_data->lic_ncpts &&
2437                                 pos < limit; j++) {
2438                                 pos += snprintf(pos, limit - pos,
2439                                                 "%d", ni_data->lic_cpts[j]);
2440                                 if ((j + 1) < ni_data->lic_ncpts)
2441                                         pos += snprintf(pos, limit - pos, ",");
2442                         }
2443                         pos += snprintf(pos, 3, "]\"");
2444
2445                         if (ni_data->lic_ncpts >= 1 &&
2446                             cYAML_create_string(item, "CPT",
2447                                                 str_buf) == NULL)
2448                                 goto out;
2449                 }
2450         }
2451
2452         /* Print out the net information only if show_rc is not provided */
2453         if (show_rc == NULL)
2454                 cYAML_print_tree(root);
2455
2456         if (l_errno != ENOENT) {
2457                 snprintf(err_str,
2458                          sizeof(err_str),
2459                          "\"cannot get networks: %s\"",
2460                          strerror(l_errno));
2461                 rc = -l_errno;
2462                 goto out;
2463         } else
2464                 rc = LUSTRE_CFG_RC_NO_ERR;
2465
2466         snprintf(err_str, sizeof(err_str), "\"success\"");
2467 out:
2468         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2469                 cYAML_free_tree(root);
2470         } else if (show_rc != NULL && *show_rc != NULL) {
2471                 struct cYAML *show_node;
2472                 /* find the net node, if one doesn't exist
2473                  * then insert one.  Otherwise add to the one there
2474                  */
2475                 show_node = cYAML_get_object_item(*show_rc, "net");
2476                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2477                         cYAML_insert_child(show_node, first_seq);
2478                         free(net_node);
2479                         free(root);
2480                 } else if (show_node == NULL) {
2481                         cYAML_insert_sibling((*show_rc)->cy_child,
2482                                                 net_node);
2483                         free(root);
2484                 } else {
2485                         cYAML_free_tree(root);
2486                 }
2487         } else {
2488                 *show_rc = root;
2489         }
2490
2491         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2492
2493         return rc;
2494 }
2495
2496 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2497 {
2498         struct lnet_ioctl_config_data data;
2499         int rc = LUSTRE_CFG_RC_NO_ERR;
2500         char err_str[LNET_MAX_STR_LEN];
2501
2502         snprintf(err_str, sizeof(err_str), "\"success\"");
2503
2504         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2505         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2506
2507         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2508         if (rc != 0) {
2509                 rc = -errno;
2510                 snprintf(err_str,
2511                          sizeof(err_str),
2512                          "\"cannot %s routing %s\"",
2513                          (enable) ? "enable" : "disable", strerror(errno));
2514                 goto out;
2515         }
2516
2517 out:
2518         cYAML_build_error(rc, seq_no,
2519                          (enable) ? ADD_CMD : DEL_CMD,
2520                          "routing", err_str, err_rc);
2521
2522         return rc;
2523 }
2524
2525 int ioctl_set_value(__u32 val, int ioc, char *name,
2526                     int seq_no, struct cYAML **err_rc)
2527 {
2528         struct lnet_ioctl_set_value data;
2529         int rc = LUSTRE_CFG_RC_NO_ERR;
2530         char err_str[LNET_MAX_STR_LEN];
2531
2532         snprintf(err_str, sizeof(err_str), "\"success\"");
2533
2534         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2535         data.sv_value = val;
2536
2537         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2538         if (rc != 0) {
2539                 rc = -errno;
2540                 snprintf(err_str,
2541                          sizeof(err_str),
2542                          "\"cannot configure %s to %d: %s\"", name,
2543                          val, strerror(errno));
2544         }
2545
2546         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2547
2548         return rc;
2549 }
2550
2551 int lustre_lnet_config_recov_intrv(int intrv, int seq_no, struct cYAML **err_rc)
2552 {
2553         int rc = LUSTRE_CFG_RC_NO_ERR;
2554         char err_str[LNET_MAX_STR_LEN];
2555         char val[LNET_MAX_STR_LEN];
2556
2557         snprintf(err_str, sizeof(err_str), "\"success\"");
2558
2559         snprintf(val, sizeof(val), "%d", intrv);
2560
2561         rc = write_sysfs_file(modparam_path, "lnet_recovery_interval", val,
2562                               1, strlen(val) + 1);
2563         if (rc)
2564                 snprintf(err_str, sizeof(err_str),
2565                          "\"cannot configure recovery interval: %s\"",
2566                          strerror(errno));
2567
2568         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_interval", err_str, err_rc);
2569
2570         return rc;
2571 }
2572
2573 int lustre_lnet_config_rtr_sensitivity(int sen, int seq_no, struct cYAML **err_rc)
2574 {
2575         int rc = LUSTRE_CFG_RC_NO_ERR;
2576         char err_str[LNET_MAX_STR_LEN];
2577         char val[LNET_MAX_STR_LEN];
2578
2579         snprintf(err_str, sizeof(err_str), "\"success\"");
2580
2581         snprintf(val, sizeof(val), "%d", sen);
2582
2583         rc = write_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
2584                               1, strlen(val) + 1);
2585         if (rc)
2586                 snprintf(err_str, sizeof(err_str),
2587                          "\"cannot configure router health sensitivity: %s\"",
2588                          strerror(errno));
2589
2590         cYAML_build_error(rc, seq_no, ADD_CMD, "router_sensitivity", err_str, err_rc);
2591
2592         return rc;
2593 }
2594
2595 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2596 {
2597         int rc = LUSTRE_CFG_RC_NO_ERR;
2598         char err_str[LNET_MAX_STR_LEN];
2599         char val[LNET_MAX_STR_LEN];
2600
2601         snprintf(err_str, sizeof(err_str), "\"success\"");
2602
2603         snprintf(val, sizeof(val), "%d", sen);
2604
2605         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2606                               1, strlen(val) + 1);
2607         if (rc)
2608                 snprintf(err_str, sizeof(err_str),
2609                          "\"cannot configure health sensitivity: %s\"",
2610                          strerror(errno));
2611
2612         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2613
2614         return rc;
2615 }
2616
2617 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2618 {
2619         int rc = LUSTRE_CFG_RC_NO_ERR;
2620         char err_str[LNET_MAX_STR_LEN];
2621         char val[LNET_MAX_STR_LEN];
2622
2623         snprintf(err_str, sizeof(err_str), "\"success\"");
2624
2625         snprintf(val, sizeof(val), "%d", timeout);
2626
2627         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2628                               1, strlen(val) + 1);
2629         if (rc)
2630                 snprintf(err_str, sizeof(err_str),
2631                          "\"cannot configure transaction timeout: %s\"",
2632                          strerror(errno));
2633
2634         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2635
2636         return rc;
2637 }
2638
2639 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2640 {
2641         int rc = LUSTRE_CFG_RC_NO_ERR;
2642         char err_str[LNET_MAX_STR_LEN];
2643         char val[LNET_MAX_STR_LEN];
2644
2645         snprintf(err_str, sizeof(err_str), "\"success\"");
2646
2647         snprintf(val, sizeof(val), "%d", count);
2648
2649         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2650                               1, strlen(val) + 1);
2651         if (rc)
2652                 snprintf(err_str, sizeof(err_str),
2653                          "\"cannot configure retry count: %s\"",
2654                          strerror(errno));
2655
2656         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2657
2658         return rc;
2659 }
2660
2661 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2662 {
2663         int rc = LUSTRE_CFG_RC_NO_ERR;
2664         char err_str[LNET_MAX_STR_LEN];
2665         char val[LNET_MAX_STR_LEN];
2666
2667         snprintf(err_str, sizeof(err_str), "\"success\"");
2668
2669         snprintf(val, sizeof(val), "%d", max);
2670
2671         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2672                               1, strlen(val) + 1);
2673         if (rc)
2674                 snprintf(err_str, sizeof(err_str),
2675                          "\"cannot configure max interfaces: %s\"",
2676                          strerror(errno));
2677
2678         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2679
2680         return rc;
2681 }
2682
2683 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2684 {
2685         int rc = LUSTRE_CFG_RC_NO_ERR;
2686         char err_str[LNET_MAX_STR_LEN];
2687         char val[LNET_MAX_STR_LEN];
2688
2689         snprintf(err_str, sizeof(err_str), "\"success\"");
2690
2691         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2692
2693         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2694                               1, strlen(val) + 1);
2695         if (rc)
2696                 snprintf(err_str, sizeof(err_str),
2697                          "\"cannot configure discovery: %s\"",
2698                          strerror(errno));
2699
2700         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2701
2702         return rc;
2703
2704 }
2705
2706 int lustre_lnet_config_drop_asym_route(int drop, int seq_no,
2707                                        struct cYAML **err_rc)
2708 {
2709         int rc = LUSTRE_CFG_RC_NO_ERR;
2710         char err_str[LNET_MAX_STR_LEN];
2711         char val[LNET_MAX_STR_LEN];
2712
2713         snprintf(err_str, sizeof(err_str), "\"success\"");
2714
2715         snprintf(val, sizeof(val), "%u", (drop) ? 1 : 0);
2716
2717         rc = write_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
2718                               1, strlen(val) + 1);
2719         if (rc)
2720                 snprintf(err_str, sizeof(err_str),
2721                          "\"cannot configure drop asym route: %s\"",
2722                          strerror(errno));
2723
2724         cYAML_build_error(rc, seq_no, ADD_CMD, "drop_asym_route",
2725                           err_str, err_rc);
2726
2727         return rc;
2728
2729 }
2730
2731 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2732 {
2733         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2734                                "numa_range", seq_no, err_rc);
2735 }
2736
2737 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2738                                struct cYAML **err_rc)
2739 {
2740         struct lnet_ioctl_config_data data;
2741         int rc = LUSTRE_CFG_RC_NO_ERR;
2742         char err_str[LNET_MAX_STR_LEN];
2743
2744         snprintf(err_str, sizeof(err_str), "\"success\"");
2745
2746         /* -1 indicates to ignore changes to this field */
2747         if (tiny < -1 || small < -1 || large < -1) {
2748                 snprintf(err_str,
2749                          sizeof(err_str),
2750                          "\"tiny, small and large must be >= 0\"");
2751                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2752                 goto out;
2753         }
2754
2755         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2756         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2757         data.cfg_config_u.cfg_buffers.buf_small = small;
2758         data.cfg_config_u.cfg_buffers.buf_large = large;
2759
2760         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2761         if (rc != 0) {
2762                 rc = -errno;
2763                 snprintf(err_str,
2764                          sizeof(err_str),
2765                          "\"cannot configure buffers: %s\"", strerror(errno));
2766                 goto out;
2767         }
2768
2769 out:
2770         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2771
2772         return rc;
2773 }
2774
2775 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2776                              struct cYAML **err_rc, bool backup)
2777 {
2778         struct lnet_ioctl_config_data *data;
2779         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2780         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2781         int l_errno = 0;
2782         char *buf;
2783         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2784         int buf_count[LNET_NRBPOOLS] = {0};
2785         struct cYAML *root = NULL, *pools_node = NULL,
2786                      *type_node = NULL, *item = NULL, *cpt = NULL,
2787                      *first_seq = NULL, *buffers = NULL;
2788         int i, j;
2789         char err_str[LNET_MAX_STR_LEN];
2790         char node_name[LNET_MAX_STR_LEN];
2791         bool exist = false;
2792
2793         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2794
2795         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2796         if (buf == NULL)
2797                 goto out;
2798
2799         data = (struct lnet_ioctl_config_data *)buf;
2800
2801         root = cYAML_create_object(NULL, NULL);
2802         if (root == NULL)
2803                 goto out;
2804
2805         if (backup)
2806                 pools_node = cYAML_create_object(root, "routing");
2807         else
2808                 pools_node = cYAML_create_seq(root, "routing");
2809         if (pools_node == NULL)
2810                 goto out;
2811
2812         for (i = 0;; i++) {
2813                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2814                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2815                                         sizeof(struct lnet_ioctl_pool_cfg);
2816                 data->cfg_count = i;
2817
2818                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2819                 if (rc != 0) {
2820                         l_errno = errno;
2821                         break;
2822                 }
2823
2824                 exist = true;
2825
2826                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2827
2828                 if (backup)
2829                         goto calculate_buffers;
2830
2831                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2832                 item = cYAML_create_seq_item(pools_node);
2833                 if (item == NULL)
2834                         goto out;
2835
2836                 if (first_seq == NULL)
2837                         first_seq = item;
2838
2839                 cpt = cYAML_create_object(item, node_name);
2840                 if (cpt == NULL)
2841                         goto out;
2842
2843 calculate_buffers:
2844                 /* create the tree  and print */
2845                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2846                         if (!backup) {
2847                                 type_node = cYAML_create_object(cpt, pools[j]);
2848                                 if (type_node == NULL)
2849                                         goto out;
2850                         }
2851                         if (!backup &&
2852                             cYAML_create_number(type_node, "npages",
2853                                                 pool_cfg->pl_pools[j].pl_npages)
2854                             == NULL)
2855                                 goto out;
2856                         if (!backup &&
2857                             cYAML_create_number(type_node, "nbuffers",
2858                                                 pool_cfg->pl_pools[j].
2859                                                   pl_nbuffers) == NULL)
2860                                 goto out;
2861                         if (!backup &&
2862                             cYAML_create_number(type_node, "credits",
2863                                                 pool_cfg->pl_pools[j].
2864                                                    pl_credits) == NULL)
2865                                 goto out;
2866                         if (!backup &&
2867                             cYAML_create_number(type_node, "mincredits",
2868                                                 pool_cfg->pl_pools[j].
2869                                                    pl_mincredits) == NULL)
2870                                 goto out;
2871                         /* keep track of the total count for each of the
2872                          * tiny, small and large buffers */
2873                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2874                 }
2875         }
2876
2877         if (pool_cfg != NULL) {
2878                 if (backup) {
2879                         if (cYAML_create_number(pools_node, "enable",
2880                                                 pool_cfg->pl_routing) ==
2881                         NULL)
2882                                 goto out;
2883
2884                         goto add_buffer_section;
2885                 }
2886
2887                 item = cYAML_create_seq_item(pools_node);
2888                 if (item == NULL)
2889                         goto out;
2890
2891                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2892                     NULL)
2893                         goto out;
2894         }
2895
2896 add_buffer_section:
2897         /* create a buffers entry in the show. This is necessary so that
2898          * if the YAML output is used to configure a node, the buffer
2899          * configuration takes hold */
2900         buffers = cYAML_create_object(root, "buffers");
2901         if (buffers == NULL)
2902                 goto out;
2903
2904         for (i = 0; i < LNET_NRBPOOLS; i++) {
2905                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2906                         goto out;
2907         }
2908
2909         if (show_rc == NULL)
2910                 cYAML_print_tree(root);
2911
2912         if (l_errno != ENOENT) {
2913                 snprintf(err_str,
2914                          sizeof(err_str),
2915                          "\"cannot get routing information: %s\"",
2916                          strerror(l_errno));
2917                 rc = -l_errno;
2918                 goto out;
2919         } else
2920                 rc = LUSTRE_CFG_RC_NO_ERR;
2921
2922         snprintf(err_str, sizeof(err_str), "\"success\"");
2923         rc = LUSTRE_CFG_RC_NO_ERR;
2924
2925 out:
2926         free(buf);
2927         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2928                 cYAML_free_tree(root);
2929         } else if (show_rc != NULL && *show_rc != NULL) {
2930                 struct cYAML *routing_node;
2931                 /* there should exist only one routing block and one
2932                  * buffers block. If there already exists a previous one
2933                  * then don't add another */
2934                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2935                 if (routing_node == NULL) {
2936                         cYAML_insert_sibling((*show_rc)->cy_child,
2937                                                 root->cy_child);
2938                         free(root);
2939                 } else {
2940                         cYAML_free_tree(root);
2941                 }
2942         } else {
2943                 *show_rc = root;
2944         }
2945
2946         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2947
2948         return rc;
2949 }
2950
2951 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2952                           struct cYAML **show_rc, struct cYAML **err_rc,
2953                           bool backup)
2954 {
2955         /*
2956          * TODO: This function is changing in a future patch to accommodate
2957          * PEER_LIST and proper filtering on any nid of the peer
2958          */
2959         struct lnet_ioctl_peer_cfg peer_info;
2960         struct lnet_peer_ni_credit_info *lpni_cri;
2961         struct lnet_ioctl_element_stats *lpni_stats;
2962         struct lnet_ioctl_element_msg_stats *msg_stats;
2963         struct lnet_ioctl_peer_ni_hstats *hstats;
2964         lnet_nid_t *nidp;
2965         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2966         int i, j, k;
2967         int l_errno = 0;
2968         __u32 count;
2969         __u32 size;
2970         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2971                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2972                      *msg_statistics = NULL, *statistics = NULL,
2973                      *yhstats;
2974         char err_str[LNET_MAX_STR_LEN];
2975         struct lnet_process_id *list = NULL;
2976         void *data = NULL;
2977         void *lpni_data;
2978         bool exist = false;
2979
2980         snprintf(err_str, sizeof(err_str),
2981                  "\"out of memory\"");
2982
2983         /* create struct cYAML root object */
2984         root = cYAML_create_object(NULL, NULL);
2985         if (root == NULL)
2986                 goto out;
2987
2988         peer_root = cYAML_create_seq(root, "peer");
2989         if (peer_root == NULL)
2990                 goto out;
2991
2992         count = 1000;
2993         size = count * sizeof(struct lnet_process_id);
2994         list = malloc(size);
2995         if (list == NULL) {
2996                 l_errno = ENOMEM;
2997                 goto out;
2998         }
2999         if (knid != NULL) {
3000                 list[0].nid = libcfs_str2nid(knid);
3001                 count = 1;
3002         } else {
3003                 for (;;) {
3004                         memset(&peer_info, 0, sizeof(peer_info));
3005                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3006                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3007                         peer_info.prcfg_size = size;
3008                         peer_info.prcfg_bulk = list;
3009
3010                         l_errno = 0;
3011                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
3012                                      &peer_info);
3013                         count = peer_info.prcfg_count;
3014                         if (rc == 0)
3015                                 break;
3016                         l_errno = errno;
3017                         if (l_errno != E2BIG) {
3018                                 snprintf(err_str,
3019                                         sizeof(err_str),
3020                                         "\"cannot get peer list: %s\"",
3021                                         strerror(l_errno));
3022                                 rc = -l_errno;
3023                                 goto out;
3024                         }
3025                         free(list);
3026                         size = peer_info.prcfg_size;
3027                         list = malloc(size);
3028                         if (list == NULL) {
3029                                 l_errno = ENOMEM;
3030                                 goto out;
3031                         }
3032                 }
3033         }
3034
3035         size = 4096;
3036         data = malloc(size);
3037         if (data == NULL) {
3038                 l_errno = ENOMEM;
3039                 goto out;
3040         }
3041
3042         for (i = 0; i < count; i++) {
3043                 for (;;) {
3044                         memset(&peer_info, 0, sizeof(peer_info));
3045                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3046                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3047                         peer_info.prcfg_prim_nid = list[i].nid;
3048                         peer_info.prcfg_size = size;
3049                         peer_info.prcfg_bulk = data;
3050
3051                         l_errno = 0;
3052                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
3053                                      &peer_info);
3054                         if (rc == 0)
3055                                 break;
3056                         l_errno = errno;
3057                         if (l_errno != E2BIG) {
3058                                 snprintf(err_str,
3059                                         sizeof(err_str),
3060                                         "\"cannot get peer information: %s\"",
3061                                         strerror(l_errno));
3062                                 rc = -l_errno;
3063                                 goto out;
3064                         }
3065                         free(data);
3066                         size = peer_info.prcfg_size;
3067                         data = malloc(size);
3068                         if (data == NULL) {
3069                                 l_errno = ENOMEM;
3070                                 goto out;
3071                         }
3072                 }
3073                 exist = true;
3074
3075                 peer = cYAML_create_seq_item(peer_root);
3076                 if (peer == NULL)
3077                         goto out;
3078
3079                 if (first_seq == NULL)
3080                         first_seq = peer;
3081
3082                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
3083                 if (cYAML_create_string(peer, "primary nid",
3084                                         libcfs_nid2str(pnid))
3085                     == NULL)
3086                         goto out;
3087                 if (cYAML_create_string(peer, "Multi-Rail",
3088                                         peer_info.prcfg_mr ? "True" : "False")
3089                     == NULL)
3090                         goto out;
3091                 /*
3092                  * print out the state of the peer only if details are
3093                  * requested
3094                  */
3095                 if (detail >= 3) {
3096                         if (!backup &&
3097                             cYAML_create_number(peer, "peer state",
3098                                                 peer_info.prcfg_state)
3099                                 == NULL)
3100                                 goto out;
3101                 }
3102
3103                 tmp = cYAML_create_seq(peer, "peer ni");
3104                 if (tmp == NULL)
3105                         goto out;
3106
3107                 lpni_data = data;
3108                 for (j = 0; j < peer_info.prcfg_count; j++) {
3109                         nidp = lpni_data;
3110                         lpni_cri = (void*)nidp + sizeof(nidp);
3111                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
3112                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
3113                         hstats = (void *)msg_stats + sizeof(*msg_stats);
3114                         lpni_data = (void *)hstats + sizeof(*hstats);
3115
3116                         peer_ni = cYAML_create_seq_item(tmp);
3117                         if (peer_ni == NULL)
3118                                 goto out;
3119
3120                         if (cYAML_create_string(peer_ni, "nid",
3121                                                 libcfs_nid2str(*nidp))
3122                             == NULL)
3123                                 goto out;
3124
3125                         if (backup)
3126                                 continue;
3127
3128                         if (cYAML_create_string(peer_ni, "state",
3129                                                 lpni_cri->cr_aliveness)
3130                             == NULL)
3131                                 goto out;
3132
3133                         if (!detail)
3134                                 continue;
3135
3136                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
3137                                                 lpni_cri->cr_ni_peer_tx_credits)
3138                             == NULL)
3139                                 goto out;
3140
3141                         if (cYAML_create_number(peer_ni, "available_tx_credits",
3142                                                 lpni_cri->cr_peer_tx_credits)
3143                             == NULL)
3144                                 goto out;
3145
3146                         if (cYAML_create_number(peer_ni, "min_tx_credits",
3147                                                 lpni_cri->cr_peer_min_tx_credits)
3148                             == NULL)
3149                                 goto out;
3150
3151                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
3152                                                 lpni_cri->cr_peer_tx_qnob)
3153                             == NULL)
3154                                 goto out;
3155
3156                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
3157                                                 lpni_cri->cr_peer_rtr_credits)
3158                             == NULL)
3159                                 goto out;
3160
3161                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
3162                                                 lpni_cri->cr_peer_min_rtr_credits)
3163                             == NULL)
3164                                 goto out;
3165
3166                         if (cYAML_create_number(peer_ni, "refcount",
3167                                                 lpni_cri->cr_refcount) == NULL)
3168                                 goto out;
3169
3170                         statistics = cYAML_create_object(peer_ni, "statistics");
3171                         if (statistics == NULL)
3172                                 goto out;
3173
3174                         if (cYAML_create_number(statistics, "send_count",
3175                                                 lpni_stats->iel_send_count)
3176                             == NULL)
3177                                 goto out;
3178
3179                         if (cYAML_create_number(statistics, "recv_count",
3180                                                 lpni_stats->iel_recv_count)
3181                             == NULL)
3182                                 goto out;
3183
3184                         if (cYAML_create_number(statistics, "drop_count",
3185                                                 lpni_stats->iel_drop_count)
3186                             == NULL)
3187                                 goto out;
3188
3189                         if (detail < 2)
3190                                 continue;
3191
3192                         for (k = 0; k < 3; k++) {
3193                                 struct lnet_ioctl_comm_count *counts;
3194
3195                                 msg_statistics = cYAML_create_object(peer_ni,
3196                                                  (char *) gmsg_stat_names[k]);
3197                                 if (msg_statistics == NULL)
3198                                         goto out;
3199
3200                                 counts = get_counts(msg_stats, k);
3201                                 if (counts == NULL)
3202                                         goto out;
3203
3204                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
3205                                                                counts))
3206                                         goto out;
3207                         }
3208
3209                         yhstats = cYAML_create_object(peer_ni, "health stats");
3210                         if (!yhstats)
3211                                 goto out;
3212                         if (cYAML_create_number(yhstats, "health value",
3213                                                 hstats->hlpni_health_value)
3214                                                         == NULL)
3215                                 goto out;
3216                         if (cYAML_create_number(yhstats, "dropped",
3217                                                 hstats->hlpni_remote_dropped)
3218                                                         == NULL)
3219                                 goto out;
3220                         if (cYAML_create_number(yhstats, "timeout",
3221                                                 hstats->hlpni_remote_timeout)
3222                                                         == NULL)
3223                                 goto out;
3224                         if (cYAML_create_number(yhstats, "error",
3225                                                 hstats->hlpni_remote_error)
3226                                                         == NULL)
3227                                 goto out;
3228                         if (cYAML_create_number(yhstats, "network timeout",
3229                                                 hstats->hlpni_network_timeout)
3230                                                         == NULL)
3231                                 goto out;
3232                 }
3233         }
3234
3235         /* print output iff show_rc is not provided */
3236         if (show_rc == NULL)
3237                 cYAML_print_tree(root);
3238
3239         snprintf(err_str, sizeof(err_str), "\"success\"");
3240         rc = LUSTRE_CFG_RC_NO_ERR;
3241
3242 out:
3243         free(list);
3244         free(data);
3245         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
3246                 cYAML_free_tree(root);
3247         } else if (show_rc != NULL && *show_rc != NULL) {
3248                 struct cYAML *show_node;
3249                 /* find the peer node, if one doesn't exist then
3250                  * insert one.  Otherwise add to the one there
3251                  */
3252                 show_node = cYAML_get_object_item(*show_rc,
3253                                                   "peer");
3254                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3255                         cYAML_insert_child(show_node, first_seq);
3256                         free(peer_root);
3257                         free(root);
3258                 } else if (show_node == NULL) {
3259                         cYAML_insert_sibling((*show_rc)->cy_child,
3260                                              peer_root);
3261                         free(root);
3262                 } else {
3263                         cYAML_free_tree(root);
3264                 }
3265         } else {
3266                 *show_rc = root;
3267         }
3268
3269         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3270                           err_rc);
3271
3272         return rc;
3273 }
3274
3275 int lustre_lnet_list_peer(int seq_no,
3276                           struct cYAML **show_rc, struct cYAML **err_rc)
3277 {
3278         struct lnet_ioctl_peer_cfg peer_info;
3279         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3280         __u32 count;
3281         __u32 size;
3282         int i = 0;
3283         int l_errno = 0;
3284         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
3285         char err_str[LNET_MAX_STR_LEN];
3286         struct lnet_process_id *list = NULL;
3287
3288         snprintf(err_str, sizeof(err_str),
3289                  "\"out of memory\"");
3290
3291         memset(&peer_info, 0, sizeof(peer_info));
3292
3293         /* create struct cYAML root object */
3294         root = cYAML_create_object(NULL, NULL);
3295         if (root == NULL)
3296                 goto out;
3297
3298         list_root = cYAML_create_seq(root, "peer list");
3299         if (list_root == NULL)
3300                 goto out;
3301
3302         count = 1000;
3303         size = count * sizeof(struct lnet_process_id);
3304         list = malloc(size);
3305         if (list == NULL) {
3306                 l_errno = ENOMEM;
3307                 goto out;
3308         }
3309         for (;;) {
3310                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3311                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3312                 peer_info.prcfg_size = size;
3313                 peer_info.prcfg_bulk = list;
3314
3315                 l_errno = 0;
3316                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
3317                 count = peer_info.prcfg_count;
3318                 if (rc == 0)
3319                         break;
3320                 l_errno = errno;
3321                 if (l_errno != E2BIG) {
3322                         snprintf(err_str,
3323                                 sizeof(err_str),
3324                                 "\"cannot get peer list: %s\"",
3325                                 strerror(l_errno));
3326                         rc = -l_errno;
3327                         goto out;
3328                 }
3329                 free(list);
3330                 size = peer_info.prcfg_size;
3331                 list = malloc(size);
3332                 if (list == NULL) {
3333                         l_errno = ENOMEM;
3334                         goto out;
3335                 }
3336         }
3337
3338         /* count is now the actual number of ids in the list. */
3339         for (i = 0; i < count; i++) {
3340                 if (cYAML_create_string(list_root, "nid",
3341                                         libcfs_nid2str(list[i].nid))
3342                     == NULL)
3343                         goto out;
3344         }
3345
3346         /* print output iff show_rc is not provided */
3347         if (show_rc == NULL)
3348                 cYAML_print_tree(root);
3349
3350         snprintf(err_str, sizeof(err_str), "\"success\"");
3351         rc = LUSTRE_CFG_RC_NO_ERR;
3352
3353 out:
3354         if (list != NULL)
3355                 free(list);
3356         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3357                 cYAML_free_tree(root);
3358         } else if (show_rc != NULL && *show_rc != NULL) {
3359                 struct cYAML *show_node;
3360                 /* find the peer node, if one doesn't exist then
3361                  * insert one.  Otherwise add to the one there
3362                  */
3363                 show_node = cYAML_get_object_item(*show_rc,
3364                                                   "peer");
3365                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3366                         cYAML_insert_child(show_node, first_seq);
3367                         free(list_root);
3368                         free(root);
3369                 } else if (show_node == NULL) {
3370                         cYAML_insert_sibling((*show_rc)->cy_child,
3371                                              list_root);
3372                         free(root);
3373                 } else {
3374                         cYAML_free_tree(root);
3375                 }
3376         } else {
3377                 *show_rc = root;
3378         }
3379
3380         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3381                           err_rc);
3382
3383         return rc;
3384 }
3385
3386 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
3387                           struct cYAML *root)
3388 {
3389         struct cYAML *show_node;
3390
3391         show_node = cYAML_get_object_item(show_rc, "global");
3392         if (show_node != NULL)
3393                 cYAML_insert_sibling(show_node->cy_child,
3394                                      node->cy_child);
3395         else
3396                 cYAML_insert_sibling(show_rc->cy_child,
3397                                      node);
3398         free(root);
3399 }
3400
3401 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
3402                                    char *name, __u32 value,
3403                                    struct cYAML **show_rc,
3404                                    struct cYAML **err_rc, int err)
3405 {
3406         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3407         struct cYAML *root = NULL, *global = NULL;
3408
3409         if (err) {
3410                 rc = err;
3411                 goto out;
3412         }
3413
3414         root = cYAML_create_object(NULL, NULL);
3415         if (root == NULL)
3416                 goto out;
3417
3418         global = cYAML_create_object(root, "global");
3419         if (global == NULL)
3420                 goto out;
3421
3422         if (cYAML_create_number(global, name,
3423                                 value) == NULL)
3424                 goto out;
3425
3426         if (show_rc == NULL)
3427                 cYAML_print_tree(root);
3428
3429         snprintf(err_str, err_len, "\"success\"");
3430
3431         rc = LUSTRE_CFG_RC_NO_ERR;
3432
3433 out:
3434         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3435                 cYAML_free_tree(root);
3436         } else if (show_rc != NULL && *show_rc != NULL) {
3437                 add_to_global(*show_rc, global, root);
3438         } else {
3439                 *show_rc = root;
3440         }
3441
3442         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
3443
3444         return rc;
3445 }
3446
3447 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
3448                                     struct cYAML **show_rc,
3449                                     struct cYAML **err_rc)
3450 {
3451         struct lnet_ioctl_set_value data;
3452         int rc;
3453         int l_errno = 0;
3454         char err_str[LNET_MAX_STR_LEN];
3455
3456         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3457
3458         LIBCFS_IOC_INIT_V2(data, sv_hdr);
3459
3460         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3461         if (rc != 0) {
3462                 l_errno = -errno;
3463                 snprintf(err_str,
3464                          sizeof(err_str),
3465                          "\"cannot get %s: %s\"",
3466                          name, strerror(l_errno));
3467         }
3468
3469         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3470                                        data.sv_value, show_rc, err_rc, l_errno);
3471 }
3472
3473 int lustre_lnet_show_recov_intrv(int seq_no, struct cYAML **show_rc,
3474                                  struct cYAML **err_rc)
3475 {
3476         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3477         char val[LNET_MAX_STR_LEN];
3478         int intrv = -1, l_errno = 0;
3479         char err_str[LNET_MAX_STR_LEN];
3480
3481         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3482
3483         rc = read_sysfs_file(modparam_path, "lnet_recovery_interval", val,
3484                              1, sizeof(val));
3485         if (rc) {
3486                 l_errno = -errno;
3487                 snprintf(err_str, sizeof(err_str),
3488                          "\"cannot get recovery interval: %d\"", rc);
3489         } else {
3490                 intrv = atoi(val);
3491         }
3492
3493         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3494                                        "recovery_interval", intrv, show_rc,
3495                                        err_rc, l_errno);
3496 }
3497
3498 int lustre_lnet_show_hsensitivity(int seq_no, struct cYAML **show_rc,
3499                                   struct cYAML **err_rc)
3500 {
3501         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3502         char val[LNET_MAX_STR_LEN];
3503         int sen = -1, l_errno = 0;
3504         char err_str[LNET_MAX_STR_LEN];
3505
3506         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3507
3508         rc = read_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
3509                              1, sizeof(val));
3510         if (rc) {
3511                 l_errno = -errno;
3512                 snprintf(err_str, sizeof(err_str),
3513                          "\"cannot get health sensitivity: %d\"", rc);
3514         } else {
3515                 sen = atoi(val);
3516         }
3517
3518         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3519                                        "health_sensitivity", sen, show_rc,
3520                                        err_rc, l_errno);
3521 }
3522
3523 int lustre_lnet_show_rtr_sensitivity(int seq_no, struct cYAML **show_rc,
3524                                      struct cYAML **err_rc)
3525 {
3526         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3527         char val[LNET_MAX_STR_LEN];
3528         int sen = -1, l_errno = 0;
3529         char err_str[LNET_MAX_STR_LEN];
3530
3531         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3532
3533         rc = read_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
3534                              1, sizeof(val));
3535         if (rc) {
3536                 l_errno = -errno;
3537                 snprintf(err_str, sizeof(err_str),
3538                          "\"cannot get router sensitivity percentage: %d\"", rc);
3539         } else {
3540                 sen = atoi(val);
3541         }
3542
3543         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3544                                        "router_sensitivity", sen, show_rc,
3545                                        err_rc, l_errno);
3546 }
3547
3548 int lustre_lnet_show_transaction_to(int seq_no, struct cYAML **show_rc,
3549                                     struct cYAML **err_rc)
3550 {
3551         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3552         char val[LNET_MAX_STR_LEN];
3553         int tto = -1, l_errno = 0;
3554         char err_str[LNET_MAX_STR_LEN];
3555
3556         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3557
3558         rc = read_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
3559                              1, sizeof(val));
3560         if (rc) {
3561                 l_errno = -errno;
3562                 snprintf(err_str, sizeof(err_str),
3563                          "\"cannot get transaction timeout: %d\"", rc);
3564         } else {
3565                 tto = atoi(val);
3566         }
3567
3568         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3569                                        "transaction_timeout", tto, show_rc,
3570                                        err_rc, l_errno);
3571 }
3572
3573 int lustre_lnet_show_retry_count(int seq_no, struct cYAML **show_rc,
3574                                  struct cYAML **err_rc)
3575 {
3576         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3577         char val[LNET_MAX_STR_LEN];
3578         int retry_count = -1, l_errno = 0;
3579         char err_str[LNET_MAX_STR_LEN];
3580
3581         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3582
3583         rc = read_sysfs_file(modparam_path, "lnet_retry_count", val,
3584                              1, sizeof(val));
3585         if (rc) {
3586                 l_errno = -errno;
3587                 snprintf(err_str, sizeof(err_str),
3588                          "\"cannot get retry count: %d\"", rc);
3589         } else {
3590                 retry_count = atoi(val);
3591         }
3592
3593         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3594                                        "retry_count", retry_count, show_rc,
3595                                        err_rc, l_errno);
3596 }
3597
3598 int show_recovery_queue(enum lnet_health_type type, char *name, int seq_no,
3599                         struct cYAML **show_rc, struct cYAML **err_rc)
3600 {
3601         struct lnet_ioctl_recovery_list nid_list;
3602         struct cYAML *root = NULL, *nids = NULL;
3603         int rc, i;
3604         char err_str[LNET_MAX_STR_LEN];
3605
3606         snprintf(err_str, sizeof(err_str), "failed to print recovery queue\n");
3607
3608         LIBCFS_IOC_INIT_V2(nid_list, rlst_hdr);
3609         nid_list.rlst_type = type;
3610
3611         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_RECOVERY_QUEUE, &nid_list);
3612         if (rc) {
3613                 rc = errno;
3614                 goto out;
3615         }
3616
3617         if (nid_list.rlst_num_nids == 0)
3618                 goto out;
3619
3620         root = cYAML_create_object(NULL, NULL);
3621         if (root == NULL)
3622                 goto out;
3623
3624         nids = cYAML_create_object(root, name);
3625         if (nids == NULL)
3626                 goto out;
3627
3628         rc = -EINVAL;
3629
3630         for (i = 0; i < nid_list.rlst_num_nids; i++) {
3631                 char nidenum[LNET_MAX_STR_LEN];
3632                 snprintf(nidenum, sizeof(nidenum), "nid-%d", i);
3633                 if (!cYAML_create_string(nids, nidenum,
3634                         libcfs_nid2str(nid_list.rlst_nid_array[i])))
3635                         goto out;
3636         }
3637
3638         snprintf(err_str, sizeof(err_str), "success\n");
3639
3640         rc = 0;
3641
3642 out:
3643         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3644                 cYAML_free_tree(root);
3645         } else if (show_rc != NULL && *show_rc != NULL) {
3646                 struct cYAML *show_node;
3647                 /* find the net node, if one doesn't exist
3648                  * then insert one.  Otherwise add to the one there
3649                  */
3650                 show_node = cYAML_get_object_item(*show_rc, name);
3651                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3652                         cYAML_insert_child(show_node, nids);
3653                         free(nids);
3654                         free(root);
3655                 } else if (show_node == NULL) {
3656                         cYAML_insert_sibling((*show_rc)->cy_child,
3657                                                 nids);
3658                         free(root);
3659                 } else {
3660                         cYAML_free_tree(root);
3661                 }
3662         } else {
3663                 *show_rc = root;
3664         }
3665
3666         cYAML_build_error(rc, seq_no, SHOW_CMD, name, err_str, err_rc);
3667
3668         return rc;
3669 }
3670
3671 int lustre_lnet_show_local_ni_recovq(int seq_no, struct cYAML **show_rc,
3672                                      struct cYAML **err_rc)
3673 {
3674         return show_recovery_queue(LNET_HEALTH_TYPE_LOCAL_NI, "local NI recovery",
3675                                    seq_no, show_rc, err_rc);
3676 }
3677
3678 int lustre_lnet_show_peer_ni_recovq(int seq_no, struct cYAML **show_rc,
3679                                     struct cYAML **err_rc)
3680 {
3681         return show_recovery_queue(LNET_HEALTH_TYPE_PEER_NI, "peer NI recovery",
3682                                    seq_no, show_rc, err_rc);
3683 }
3684
3685 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
3686                               struct cYAML **err_rc)
3687 {
3688         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3689         char val[LNET_MAX_STR_LEN];
3690         int max_intf = -1, l_errno = 0;
3691         char err_str[LNET_MAX_STR_LEN];
3692
3693         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3694
3695         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
3696                              1, sizeof(val));
3697         if (rc) {
3698                 l_errno = -errno;
3699                 snprintf(err_str, sizeof(err_str),
3700                          "\"cannot get max interfaces: %d\"", rc);
3701         } else {
3702                 max_intf = atoi(val);
3703         }
3704
3705         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3706                                        "max_intf", max_intf, show_rc,
3707                                        err_rc, l_errno);
3708 }
3709
3710 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3711                                struct cYAML **err_rc)
3712 {
3713         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3714         char val[LNET_MAX_STR_LEN];
3715         int discovery = -1, l_errno = 0;
3716         char err_str[LNET_MAX_STR_LEN];
3717
3718         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3719
3720         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3721                              1, sizeof(val));
3722         if (rc) {
3723                 l_errno = -errno;
3724                 snprintf(err_str, sizeof(err_str),
3725                          "\"cannot get discovery setting: %d\"", rc);
3726         } else {
3727                 /*
3728                  * The kernel stores a discovery disabled value. User space
3729                  * shows whether discovery is enabled. So the value must be
3730                  * inverted.
3731                  */
3732                 discovery = !atoi(val);
3733         }
3734
3735         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3736                                        "discovery", discovery, show_rc,
3737                                        err_rc, l_errno);
3738 }
3739
3740 int lustre_lnet_show_drop_asym_route(int seq_no, struct cYAML **show_rc,
3741                                      struct cYAML **err_rc)
3742 {
3743         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3744         char val[LNET_MAX_STR_LEN];
3745         int drop_asym_route = -1, l_errno = 0;
3746         char err_str[LNET_MAX_STR_LEN];
3747
3748         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3749
3750         rc = read_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
3751                              1, sizeof(val));
3752         if (rc) {
3753                 l_errno = -errno;
3754                 snprintf(err_str, sizeof(err_str),
3755                          "\"cannot get drop asym route setting: %d\"", rc);
3756         } else {
3757                 drop_asym_route = atoi(val);
3758         }
3759
3760         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3761                                        "drop_asym_route", drop_asym_route,
3762                                        show_rc, err_rc, l_errno);
3763 }
3764
3765 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3766                                 struct cYAML **err_rc)
3767 {
3768         return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3769                                         "numa_range", show_rc, err_rc);
3770 }
3771
3772 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3773                            struct cYAML **err_rc)
3774 {
3775         struct lnet_ioctl_lnet_stats data;
3776         struct lnet_counters *cntrs;
3777         int rc;
3778         int l_errno;
3779         char err_str[LNET_MAX_STR_LEN];
3780         struct cYAML *root = NULL, *stats = NULL;
3781
3782         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3783
3784         LIBCFS_IOC_INIT_V2(data, st_hdr);
3785
3786         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3787         if (rc) {
3788                 l_errno = errno;
3789                 snprintf(err_str,
3790                          sizeof(err_str),
3791                          "\"cannot get lnet statistics: %s\"",
3792                          strerror(l_errno));
3793                 rc = -l_errno;
3794                 goto out;
3795         }
3796
3797         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3798
3799         cntrs = &data.st_cntrs;
3800
3801         root = cYAML_create_object(NULL, NULL);
3802         if (!root)
3803                 goto out;
3804
3805         stats = cYAML_create_object(root, "statistics");
3806         if (!stats)
3807                 goto out;
3808
3809         if (!cYAML_create_number(stats, "msgs_alloc",
3810                                  cntrs->lct_common.lcc_msgs_alloc))
3811                 goto out;
3812
3813         if (!cYAML_create_number(stats, "msgs_max",
3814                                  cntrs->lct_common.lcc_msgs_max))
3815                 goto out;
3816
3817         if (!cYAML_create_number(stats, "rst_alloc",
3818                                  cntrs->lct_health.lch_rst_alloc))
3819                 goto out;
3820
3821         if (!cYAML_create_number(stats, "errors",
3822                                  cntrs->lct_common.lcc_errors))
3823                 goto out;
3824
3825         if (!cYAML_create_number(stats, "send_count",
3826                                  cntrs->lct_common.lcc_send_count))
3827                 goto out;
3828
3829         if (!cYAML_create_number(stats, "resend_count",
3830                                  cntrs->lct_health.lch_resend_count))
3831                 goto out;
3832
3833         if (!cYAML_create_number(stats, "response_timeout_count",
3834                                  cntrs->lct_health.lch_response_timeout_count))
3835                 goto out;
3836
3837         if (!cYAML_create_number(stats, "local_interrupt_count",
3838                                  cntrs->lct_health.lch_local_interrupt_count))
3839                 goto out;
3840
3841         if (!cYAML_create_number(stats, "local_dropped_count",
3842                                  cntrs->lct_health.lch_local_dropped_count))
3843                 goto out;
3844
3845         if (!cYAML_create_number(stats, "local_aborted_count",
3846                                  cntrs->lct_health.lch_local_aborted_count))
3847                 goto out;
3848
3849         if (!cYAML_create_number(stats, "local_no_route_count",
3850                                  cntrs->lct_health.lch_local_no_route_count))
3851                 goto out;
3852
3853         if (!cYAML_create_number(stats, "local_timeout_count",
3854                                  cntrs->lct_health.lch_local_timeout_count))
3855                 goto out;
3856
3857         if (!cYAML_create_number(stats, "local_error_count",
3858                                  cntrs->lct_health.lch_local_error_count))
3859                 goto out;
3860
3861         if (!cYAML_create_number(stats, "remote_dropped_count",
3862                                  cntrs->lct_health.lch_remote_dropped_count))
3863                 goto out;
3864
3865         if (!cYAML_create_number(stats, "remote_error_count",
3866                                  cntrs->lct_health.lch_remote_error_count))
3867                 goto out;
3868
3869         if (!cYAML_create_number(stats, "remote_timeout_count",
3870                                  cntrs->lct_health.lch_remote_timeout_count))
3871                 goto out;
3872
3873         if (!cYAML_create_number(stats, "network_timeout_count",
3874                                  cntrs->lct_health.lch_network_timeout_count))
3875                 goto out;
3876
3877         if (!cYAML_create_number(stats, "recv_count",
3878                                  cntrs->lct_common.lcc_recv_count))
3879                 goto out;
3880
3881         if (!cYAML_create_number(stats, "route_count",
3882                                  cntrs->lct_common.lcc_route_count))
3883                 goto out;
3884
3885         if (!cYAML_create_number(stats, "drop_count",
3886                                  cntrs->lct_common.lcc_drop_count))
3887                 goto out;
3888
3889         if (!cYAML_create_number(stats, "send_length",
3890                                  cntrs->lct_common.lcc_send_length))
3891                 goto out;
3892
3893         if (!cYAML_create_number(stats, "recv_length",
3894                                  cntrs->lct_common.lcc_recv_length))
3895                 goto out;
3896
3897         if (!cYAML_create_number(stats, "route_length",
3898                                  cntrs->lct_common.lcc_route_length))
3899                 goto out;
3900
3901         if (!cYAML_create_number(stats, "drop_length",
3902                                  cntrs->lct_common.lcc_drop_length))
3903                 goto out;
3904
3905         if (!show_rc)
3906                 cYAML_print_tree(root);
3907
3908         snprintf(err_str, sizeof(err_str), "\"success\"");
3909         rc = LUSTRE_CFG_RC_NO_ERR;
3910 out:
3911         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3912                 cYAML_free_tree(root);
3913         } else if (show_rc != NULL && *show_rc != NULL) {
3914                 cYAML_insert_sibling((*show_rc)->cy_child,
3915                                         root->cy_child);
3916                 free(root);
3917         } else {
3918                 *show_rc = root;
3919         }
3920
3921         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3922
3923         return rc;
3924 }
3925
3926 typedef int (*cmd_handler_t)(struct cYAML *tree,
3927                              struct cYAML **show_rc,
3928                              struct cYAML **err_rc);
3929
3930 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3931                                     struct cYAML **err_rc)
3932 {
3933         struct cYAML *net, *gw, *hop, *prio, *seq_no;
3934
3935         net = cYAML_get_object_item(tree, "net");
3936         gw = cYAML_get_object_item(tree, "gateway");
3937         hop = cYAML_get_object_item(tree, "hop");
3938         prio = cYAML_get_object_item(tree, "priority");
3939         seq_no = cYAML_get_object_item(tree, "seq_no");
3940
3941         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3942                                         (gw) ? gw->cy_valuestring : NULL,
3943                                         (hop) ? hop->cy_valueint : -1,
3944                                         (prio) ? prio->cy_valueint : -1,
3945                                         (seq_no) ? seq_no->cy_valueint : -1,
3946                                         err_rc);
3947 }
3948
3949 static void yaml_free_string_array(char **array, int num)
3950 {
3951         int i;
3952         char **sub_array = array;
3953
3954         for (i = 0; i < num; i++) {
3955                 if (*sub_array != NULL)
3956                         free(*sub_array);
3957                 sub_array++;
3958         }
3959         if (array)
3960                 free(array);
3961 }
3962
3963 /*
3964  *    interfaces:
3965  *        0: <intf_name>['['<expr>']']
3966  *        1: <intf_name>['['<expr>']']
3967  */
3968 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3969                                struct lnet_dlc_network_descr *nw_descr)
3970 {
3971         struct cYAML *child = NULL;
3972         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3973         struct lnet_dlc_intf_descr *intf_descr, *tmp;
3974
3975         if (intf_tree == NULL || nw_descr == NULL)
3976                 return LUSTRE_CFG_RC_BAD_PARAM;
3977
3978         /* now grab all the interfaces and their cpts */
3979         child = intf_tree->cy_child;
3980         while (child != NULL) {
3981                 if (child->cy_valuestring == NULL) {
3982                         child = child->cy_next;
3983                         continue;
3984                 }
3985
3986                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3987                         goto failed;
3988
3989                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3990                                                 child->cy_valuestring,
3991                                                 strlen(child->cy_valuestring));
3992                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3993                         goto failed;
3994
3995                 intf_num++;
3996                 child = child->cy_next;
3997         }
3998
3999         if (intf_num == 0)
4000                 return LUSTRE_CFG_RC_MISSING_PARAM;
4001
4002         return intf_num;
4003
4004 failed:
4005         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
4006                                  intf_on_network) {
4007                 list_del(&intf_descr->intf_on_network);
4008                 free_intf_descr(intf_descr);
4009         }
4010
4011         return rc;
4012 }
4013
4014 static bool
4015 yaml_extract_cmn_tunables(struct cYAML *tree,
4016                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
4017                           struct cfs_expr_list **global_cpts)
4018 {
4019         struct cYAML *tun, *item, *smp;
4020         int rc;
4021
4022         tun = cYAML_get_object_item(tree, "tunables");
4023         if (tun != NULL) {
4024                 item = cYAML_get_object_item(tun, "peer_timeout");
4025                 if (item != NULL)
4026                         tunables->lct_peer_timeout = item->cy_valueint;
4027                 item = cYAML_get_object_item(tun, "peer_credits");
4028                 if (item != NULL)
4029                         tunables->lct_peer_tx_credits = item->cy_valueint;
4030                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
4031                 if (item != NULL)
4032                         tunables->lct_peer_rtr_credits = item->cy_valueint;
4033                 item = cYAML_get_object_item(tun, "credits");
4034                 if (item != NULL)
4035                         tunables->lct_max_tx_credits = item->cy_valueint;
4036                 smp = cYAML_get_object_item(tun, "CPT");
4037                 if (smp != NULL) {
4038                         rc = cfs_expr_list_parse(smp->cy_valuestring,
4039                                                  strlen(smp->cy_valuestring),
4040                                                  0, UINT_MAX, global_cpts);
4041                         if (rc != 0)
4042                                 *global_cpts = NULL;
4043                 }
4044
4045                 return true;
4046         }
4047
4048         return false;
4049 }
4050
4051 static bool
4052 yaml_extract_tunables(struct cYAML *tree,
4053                       struct lnet_ioctl_config_lnd_tunables *tunables,
4054                       struct cfs_expr_list **global_cpts,
4055                       __u32 net_type)
4056 {
4057         bool rc;
4058
4059         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
4060                                        global_cpts);
4061
4062         if (!rc)
4063                 return rc;
4064
4065         lustre_yaml_extract_lnd_tunables(tree, net_type,
4066                                          &tunables->lt_tun);
4067
4068         return rc;
4069 }
4070
4071 /*
4072  * net:
4073  *    - net type: <net>[<NUM>]
4074   *      local NI(s):
4075  *        - nid: <ip>@<net>[<NUM>]
4076  *          status: up
4077  *          interfaces:
4078  *               0: <intf_name>['['<expr>']']
4079  *               1: <intf_name>['['<expr>']']
4080  *        tunables:
4081  *               peer_timeout: <NUM>
4082  *               peer_credits: <NUM>
4083  *               peer_buffer_credits: <NUM>
4084  *               credits: <NUM>
4085 *         lnd tunables:
4086  *               peercredits_hiw: <NUM>
4087  *               map_on_demand: <NUM>
4088  *               concurrent_sends: <NUM>
4089  *               fmr_pool_size: <NUM>
4090  *               fmr_flush_trigger: <NUM>
4091  *               fmr_cache: <NUM>
4092  *
4093  * At least one interface is required. If no interfaces are provided the
4094  * network interface can not be configured.
4095  */
4096 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
4097                                  struct cYAML **err_rc)
4098 {
4099         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
4100                      *item = NULL;
4101         int num_entries = 0, rc;
4102         struct lnet_dlc_network_descr nw_descr;
4103         struct cfs_expr_list *global_cpts = NULL;
4104         struct lnet_ioctl_config_lnd_tunables tunables;
4105         bool found = false;
4106
4107         memset(&tunables, 0, sizeof(tunables));
4108
4109         INIT_LIST_HEAD(&nw_descr.network_on_rule);
4110         INIT_LIST_HEAD(&nw_descr.nw_intflist);
4111
4112         ip2net = cYAML_get_object_item(tree, "ip2net");
4113         net = cYAML_get_object_item(tree, "net type");
4114         if (net)
4115                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
4116         else
4117                 nw_descr.nw_id = LOLND;
4118
4119         /*
4120          * if neither net nor ip2nets are present, then we can not
4121          * configure the network.
4122          */
4123         if (!net && !ip2net)
4124                 return LUSTRE_CFG_RC_MISSING_PARAM;
4125
4126         local_nis = cYAML_get_object_item(tree, "local NI(s)");
4127         if (local_nis == NULL)
4128                 return LUSTRE_CFG_RC_MISSING_PARAM;
4129
4130         if (!cYAML_is_sequence(local_nis))
4131                 return LUSTRE_CFG_RC_BAD_PARAM;
4132
4133         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
4134                 intf = cYAML_get_object_item(item, "interfaces");
4135                 if (intf == NULL)
4136                         continue;
4137                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
4138                 if (num_entries <= 0) {
4139                         cYAML_build_error(num_entries, -1, "ni", "add",
4140                                         "bad interface list",
4141                                         err_rc);
4142                         return LUSTRE_CFG_RC_BAD_PARAM;
4143                 }
4144         }
4145
4146         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
4147                                       LNET_NETTYP(nw_descr.nw_id));
4148         seq_no = cYAML_get_object_item(tree, "seq_no");
4149
4150         rc = lustre_lnet_config_ni(&nw_descr,
4151                                    global_cpts,
4152                                    (ip2net) ? ip2net->cy_valuestring : NULL,
4153                                    (found) ? &tunables: NULL,
4154                                    (seq_no) ? seq_no->cy_valueint : -1,
4155                                    err_rc);
4156
4157         if (global_cpts != NULL)
4158                 cfs_expr_list_free(global_cpts);
4159
4160         return rc;
4161 }
4162
4163 /*
4164  * ip2nets:
4165  *  - net-spec: <tcp|o2ib|gni>[NUM]
4166  *    interfaces:
4167  *        0: <intf name>['['<expr>']']
4168  *        1: <intf name>['['<expr>']']
4169  *    ip-range:
4170  *        0: <expr.expr.expr.expr>
4171  *        1: <expr.expr.expr.expr>
4172  */
4173 static int handle_yaml_config_ip2nets(struct cYAML *tree,
4174                                       struct cYAML **show_rc,
4175                                       struct cYAML **err_rc)
4176 {
4177         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
4178                      *seq_no = NULL;
4179         struct lustre_lnet_ip2nets ip2nets;
4180         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
4181                                           *tmp = NULL;
4182         int rc = LUSTRE_CFG_RC_NO_ERR;
4183         struct cfs_expr_list *global_cpts = NULL;
4184         struct cfs_expr_list *el, *el_tmp;
4185         struct lnet_ioctl_config_lnd_tunables tunables;
4186         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
4187         bool found = false;
4188
4189         memset(&tunables, 0, sizeof(tunables));
4190
4191         /* initialize all lists */
4192         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
4193         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
4194         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
4195
4196         net = cYAML_get_object_item(tree, "net-spec");
4197         if (net == NULL)
4198                 return LUSTRE_CFG_RC_BAD_PARAM;
4199
4200         if (net != NULL && net->cy_valuestring == NULL)
4201                 return LUSTRE_CFG_RC_BAD_PARAM;
4202
4203         /* assign the network id */
4204         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
4205         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
4206                 return LUSTRE_CFG_RC_BAD_PARAM;
4207
4208         seq_no = cYAML_get_object_item(tree, "seq_no");
4209
4210         intf = cYAML_get_object_item(tree, "interfaces");
4211         if (intf != NULL) {
4212                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
4213                 if (rc <= 0)
4214                         return LUSTRE_CFG_RC_BAD_PARAM;
4215         }
4216
4217         ip_range = cYAML_get_object_item(tree, "ip-range");
4218         if (ip_range != NULL) {
4219                 item = ip_range->cy_child;
4220                 while (item != NULL) {
4221                         if (item->cy_valuestring == NULL) {
4222                                 item = item->cy_next;
4223                                 continue;
4224                         }
4225
4226                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
4227                                                       item->cy_valuestring);
4228
4229                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4230                                 goto out;
4231
4232                         item = item->cy_next;
4233                 }
4234         }
4235
4236         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
4237                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
4238
4239         rc = lustre_lnet_config_ip2nets(&ip2nets,
4240                         (found) ? &tunables : NULL,
4241                         global_cpts,
4242                         (seq_no) ? seq_no->cy_valueint : -1,
4243                         err_rc);
4244
4245         /*
4246          * don't stop because there was no match. Continue processing the
4247          * rest of the rules. If non-match then nothing is configured
4248          */
4249         if (rc == LUSTRE_CFG_RC_NO_MATCH)
4250                 rc = LUSTRE_CFG_RC_NO_ERR;
4251 out:
4252         list_for_each_entry_safe(intf_descr, intf_tmp,
4253                                  &ip2nets.ip2nets_net.nw_intflist,
4254                                  intf_on_network) {
4255                 list_del(&intf_descr->intf_on_network);
4256                 free_intf_descr(intf_descr);
4257         }
4258
4259         list_for_each_entry_safe(ip_range_descr, tmp,
4260                                  &ip2nets.ip2nets_ip_ranges,
4261                                  ipr_entry) {
4262                 list_del(&ip_range_descr->ipr_entry);
4263                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
4264                                          el_link) {
4265                         list_del(&el->el_link);
4266                         cfs_expr_list_free(el);
4267                 }
4268                 free(ip_range_descr);
4269         }
4270
4271         return rc;
4272 }
4273
4274 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
4275                               struct cYAML **err_rc)
4276 {
4277         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
4278                      *local_nis = NULL;
4279         int num_entries, rc;
4280         struct lnet_dlc_network_descr nw_descr;
4281
4282         INIT_LIST_HEAD(&nw_descr.network_on_rule);
4283         INIT_LIST_HEAD(&nw_descr.nw_intflist);
4284
4285         net = cYAML_get_object_item(tree, "net type");
4286         if (net != NULL)
4287                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
4288
4289         local_nis = cYAML_get_object_item(tree, "local NI(s)");
4290         if (local_nis == NULL)
4291                 return LUSTRE_CFG_RC_MISSING_PARAM;
4292
4293         if (!cYAML_is_sequence(local_nis))
4294                 return LUSTRE_CFG_RC_BAD_PARAM;
4295
4296         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
4297                 intf = cYAML_get_object_item(item, "interfaces");
4298                 if (intf == NULL)
4299                         continue;
4300                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
4301                 if (num_entries <= 0) {
4302                         cYAML_build_error(num_entries, -1, "ni", "add",
4303                                         "bad interface list",
4304                                         err_rc);
4305                         return LUSTRE_CFG_RC_BAD_PARAM;
4306                 }
4307         }
4308
4309         seq_no = cYAML_get_object_item(tree, "seq_no");
4310
4311         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
4312                                 (seq_no) ? seq_no->cy_valueint : -1,
4313                                 err_rc);
4314
4315         return rc;
4316 }
4317
4318 static int yaml_copy_peer_nids(struct cYAML *nids_entry, char ***nidsppp,
4319                                char *prim_nid, bool del)
4320 {
4321         struct cYAML *child = NULL, *entry = NULL;
4322         char **nids = NULL;
4323         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
4324
4325         if (cYAML_is_sequence(nids_entry)) {
4326                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
4327                         entry = cYAML_get_object_item(child, "nid");
4328                         /* don't count an empty entry */
4329                         if (!entry || !entry->cy_valuestring)
4330                                 continue;
4331
4332                         if (prim_nid &&
4333                             (strcmp(entry->cy_valuestring, prim_nid)
4334                                         == 0) && del) {
4335                                 /*
4336                                  * primary nid is present in the list of
4337                                  * nids so that means we want to delete
4338                                  * the entire peer, so no need to go
4339                                  * further. Just delete the entire peer.
4340                                  */
4341                                 return 0;
4342                         }
4343
4344                         num++;
4345                 }
4346         }
4347
4348         if (num == 0)
4349                 return LUSTRE_CFG_RC_MISSING_PARAM;
4350
4351         nids = calloc(sizeof(*nids), num);
4352         if (!nids)
4353                 return LUSTRE_CFG_RC_OUT_OF_MEM;
4354
4355         /* now grab all the nids */
4356         num = 0;
4357         child = NULL;
4358         while (cYAML_get_next_seq_item(nids_entry, &child)) {
4359                 entry = cYAML_get_object_item(child, "nid");
4360                 if (!entry || !entry->cy_valuestring)
4361                         continue;
4362
4363                 nids[num] = strdup(entry->cy_valuestring);
4364                 if (!nids[num]) {
4365                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
4366                         goto failed;
4367                 }
4368                 num++;
4369         }
4370         rc = num;
4371
4372         *nidsppp = nids;
4373         return rc;
4374
4375 failed:
4376         if (nids != NULL)
4377                 yaml_free_string_array(nids, num);
4378         *nidsppp = NULL;
4379         return rc;
4380 }
4381
4382 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
4383                                    struct cYAML **err_rc)
4384 {
4385         char **nids = NULL;
4386         int num, rc;
4387         struct cYAML *seq_no, *prim_nid, *mr, *ip2nets, *peer_nis;
4388         char err_str[LNET_MAX_STR_LEN];
4389         bool mr_value;
4390
4391         seq_no = cYAML_get_object_item(tree, "seq_no");
4392         prim_nid = cYAML_get_object_item(tree, "primary nid");
4393         mr = cYAML_get_object_item(tree, "Multi-Rail");
4394         ip2nets = cYAML_get_object_item(tree, "ip2nets");
4395         peer_nis = cYAML_get_object_item(tree, "peer ni");
4396
4397         if (ip2nets && (prim_nid || peer_nis)) {
4398                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4399                 snprintf(err_str, sizeof(err_str),
4400                          "ip2nets can not be specified along side prim_nid"
4401                          " or peer ni fields");
4402                 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
4403                                   ADD_CMD, "peer", err_str, err_rc);
4404                 return rc;
4405         }
4406
4407         if (!mr)
4408                 mr_value = true;
4409         else {
4410                 if (!mr->cy_valuestring || !strcmp(mr->cy_valuestring, "True"))
4411                         mr_value = true;
4412                 else if (!strcmp(mr->cy_valuestring, "False"))
4413                         mr_value = false;
4414                 else {
4415                         rc = LUSTRE_CFG_RC_BAD_PARAM;
4416                         snprintf(err_str, sizeof(err_str), "Bad MR value");
4417                         cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
4418                                           ADD_CMD, "peer", err_str, err_rc);
4419                         return rc;
4420                 }
4421         }
4422
4423         num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis, &nids,
4424                                   (prim_nid) ? prim_nid->cy_valuestring : NULL,
4425                                    false);
4426
4427         if (num < 0) {
4428                 snprintf(err_str, sizeof(err_str),
4429                          "error copying nids from YAML block");
4430                 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
4431                                   ADD_CMD, "peer", err_str, err_rc);
4432                 return num;
4433         }
4434
4435         rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
4436                                          nids, num, mr_value,
4437                                          (ip2nets) ? true : false,
4438                                          (seq_no) ? seq_no->cy_valueint : -1,
4439                                          err_rc);
4440
4441         yaml_free_string_array(nids, num);
4442         return rc;
4443 }
4444
4445 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
4446                                 struct cYAML **err_rc)
4447 {
4448         char **nids = NULL;
4449         int num, rc;
4450         struct cYAML *seq_no, *prim_nid, *ip2nets, *peer_nis;
4451         char err_str[LNET_MAX_STR_LEN];
4452
4453         seq_no = cYAML_get_object_item(tree, "seq_no");
4454         prim_nid = cYAML_get_object_item(tree, "primary nid");
4455         ip2nets = cYAML_get_object_item(tree, "ip2nets");
4456         peer_nis = cYAML_get_object_item(tree, "peer ni");
4457
4458         if (ip2nets && (prim_nid || peer_nis)) {
4459                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4460                 snprintf(err_str, sizeof(err_str),
4461                          "ip2nets can not be specified along side prim_nid"
4462                          " or peer ni fields");
4463                 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
4464                                   DEL_CMD, "peer", err_str, err_rc);
4465                 return rc;
4466         }
4467
4468         num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis , &nids,
4469                                   (prim_nid) ? prim_nid->cy_valuestring : NULL,
4470                                   true);
4471         if (num < 0) {
4472                 snprintf(err_str, sizeof(err_str),
4473                          "error copying nids from YAML block");
4474                 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
4475                                   ADD_CMD, "peer", err_str, err_rc);
4476                 return num;
4477         }
4478
4479         rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
4480                                       nids, num, (ip2nets) ? true : false,
4481                                       (seq_no) ? seq_no->cy_valueint : -1,
4482                                       err_rc);
4483
4484         yaml_free_string_array(nids, num);
4485         return rc;
4486 }
4487
4488 static int handle_yaml_config_buffers(struct cYAML *tree,
4489                                       struct cYAML **show_rc,
4490                                       struct cYAML **err_rc)
4491 {
4492         int rc;
4493         struct cYAML *tiny, *small, *large, *seq_no;
4494
4495         tiny = cYAML_get_object_item(tree, "tiny");
4496         small = cYAML_get_object_item(tree, "small");
4497         large = cYAML_get_object_item(tree, "large");
4498         seq_no = cYAML_get_object_item(tree, "seq_no");
4499
4500         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
4501                                         (small) ? small->cy_valueint : -1,
4502                                         (large) ? large->cy_valueint : -1,
4503                                         (seq_no) ? seq_no->cy_valueint : -1,
4504                                         err_rc);
4505
4506         return rc;
4507 }
4508
4509 static int handle_yaml_config_routing(struct cYAML *tree,
4510                                       struct cYAML **show_rc,
4511                                       struct cYAML **err_rc)
4512 {
4513         int rc = LUSTRE_CFG_RC_NO_ERR;
4514         struct cYAML *seq_no, *enable;
4515
4516         seq_no = cYAML_get_object_item(tree, "seq_no");
4517         enable = cYAML_get_object_item(tree, "enable");
4518
4519         if (enable) {
4520                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
4521                                                 (seq_no) ?
4522                                                     seq_no->cy_valueint : -1,
4523                                                 err_rc);
4524         }
4525
4526         return rc;
4527 }
4528
4529 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
4530                                  struct cYAML **err_rc)
4531 {
4532         struct cYAML *net;
4533         struct cYAML *gw;
4534         struct cYAML *seq_no;
4535
4536         net = cYAML_get_object_item(tree, "net");
4537         gw = cYAML_get_object_item(tree, "gateway");
4538         seq_no = cYAML_get_object_item(tree, "seq_no");
4539
4540         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
4541                                      (gw) ? gw->cy_valuestring : NULL,
4542                                      (seq_no) ? seq_no->cy_valueint : -1,
4543                                      err_rc);
4544 }
4545
4546 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
4547                                    struct cYAML **err_rc)
4548 {
4549         struct cYAML *seq_no;
4550
4551         seq_no = cYAML_get_object_item(tree, "seq_no");
4552
4553         return lustre_lnet_enable_routing(0, (seq_no) ?
4554                                                 seq_no->cy_valueint : -1,
4555                                         err_rc);
4556 }
4557
4558 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
4559                                   struct cYAML **err_rc)
4560 {
4561         struct cYAML *net;
4562         struct cYAML *gw;
4563         struct cYAML *hop;
4564         struct cYAML *prio;
4565         struct cYAML *detail;
4566         struct cYAML *seq_no;
4567
4568         net = cYAML_get_object_item(tree, "net");
4569         gw = cYAML_get_object_item(tree, "gateway");
4570         hop = cYAML_get_object_item(tree, "hop");
4571         prio = cYAML_get_object_item(tree, "priority");
4572         detail = cYAML_get_object_item(tree, "detail");
4573         seq_no = cYAML_get_object_item(tree, "seq_no");
4574
4575         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
4576                                       (gw) ? gw->cy_valuestring : NULL,
4577                                       (hop) ? hop->cy_valueint : -1,
4578                                       (prio) ? prio->cy_valueint : -1,
4579                                       (detail) ? detail->cy_valueint : 0,
4580                                       (seq_no) ? seq_no->cy_valueint : -1,
4581                                       show_rc, err_rc, false);
4582 }
4583
4584 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
4585                                 struct cYAML **err_rc)
4586 {
4587         struct cYAML *net, *detail, *seq_no;
4588
4589         net = cYAML_get_object_item(tree, "net");
4590         detail = cYAML_get_object_item(tree, "detail");
4591         seq_no = cYAML_get_object_item(tree, "seq_no");
4592
4593         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
4594                                     (detail) ? detail->cy_valueint : 0,
4595                                     (seq_no) ? seq_no->cy_valueint : -1,
4596                                     show_rc, err_rc, false);
4597 }
4598
4599 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
4600                                     struct cYAML **err_rc)
4601 {
4602         struct cYAML *seq_no;
4603
4604         seq_no = cYAML_get_object_item(tree, "seq_no");
4605
4606         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
4607                                         show_rc, err_rc, false);
4608 }
4609
4610 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
4611                                   struct cYAML **err_rc)
4612 {
4613         struct cYAML *seq_no, *nid, *detail;
4614
4615         seq_no = cYAML_get_object_item(tree, "seq_no");
4616         detail = cYAML_get_object_item(tree, "detail");
4617         nid = cYAML_get_object_item(tree, "nid");
4618
4619         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
4620                                      (detail) ? detail->cy_valueint : 0,
4621                                      (seq_no) ? seq_no->cy_valueint : -1,
4622                                      show_rc, err_rc, false);
4623 }
4624
4625 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
4626                                   struct cYAML **err_rc)
4627 {
4628         struct cYAML *seq_no;
4629
4630         seq_no = cYAML_get_object_item(tree, "seq_no");
4631
4632         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
4633                                       show_rc, err_rc);
4634 }
4635
4636 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
4637                                   struct cYAML **err_rc)
4638 {
4639         struct cYAML *seq_no, *range;
4640
4641         seq_no = cYAML_get_object_item(tree, "seq_no");
4642         range = cYAML_get_object_item(tree, "range");
4643
4644         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
4645                                              seq_no ? seq_no->cy_valueint : -1,
4646                                              err_rc);
4647 }
4648
4649 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
4650                                struct cYAML **err_rc)
4651 {
4652         struct cYAML *seq_no;
4653
4654         seq_no = cYAML_get_object_item(tree, "seq_no");
4655
4656         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
4657                                              err_rc);
4658 }
4659
4660 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
4661                                 struct cYAML **err_rc)
4662 {
4663         struct cYAML *seq_no;
4664
4665         seq_no = cYAML_get_object_item(tree, "seq_no");
4666
4667         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
4668                                            show_rc, err_rc);
4669 }
4670
4671 static int handle_yaml_config_global_settings(struct cYAML *tree,
4672                                               struct cYAML **show_rc,
4673                                               struct cYAML **err_rc)
4674 {
4675         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4676                      *sen, *recov, *rsen, *drop_asym_route;
4677         int rc = 0;
4678
4679         seq_no = cYAML_get_object_item(tree, "seq_no");
4680         max_intf = cYAML_get_object_item(tree, "max_intf");
4681         if (max_intf)
4682                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
4683                                                  seq_no ? seq_no->cy_valueint
4684                                                         : -1,
4685                                                  err_rc);
4686
4687         numa = cYAML_get_object_item(tree, "numa_range");
4688         if (numa)
4689                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
4690                                                    seq_no ? seq_no->cy_valueint
4691                                                         : -1,
4692                                                    err_rc);
4693
4694         discovery = cYAML_get_object_item(tree, "discovery");
4695         if (discovery)
4696                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
4697                                                   seq_no ? seq_no->cy_valueint
4698                                                         : -1,
4699                                                   err_rc);
4700
4701         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4702         if (drop_asym_route)
4703                 rc = lustre_lnet_config_drop_asym_route(
4704                         drop_asym_route->cy_valueint,
4705                         seq_no ? seq_no->cy_valueint : -1,
4706                         err_rc);
4707
4708         retry = cYAML_get_object_item(tree, "retry_count");
4709         if (retry)
4710                 rc = lustre_lnet_config_retry_count(retry->cy_valueint,
4711                                                     seq_no ? seq_no->cy_valueint
4712                                                         : -1,
4713                                                     err_rc);
4714
4715         tto = cYAML_get_object_item(tree, "transaction_timeout");
4716         if (tto)
4717                 rc = lustre_lnet_config_transaction_to(tto->cy_valueint,
4718                                                        seq_no ? seq_no->cy_valueint
4719                                                                 : -1,
4720                                                        err_rc);
4721
4722         sen = cYAML_get_object_item(tree, "health_sensitivity");
4723         if (sen)
4724                 rc = lustre_lnet_config_hsensitivity(sen->cy_valueint,
4725                                                      seq_no ? seq_no->cy_valueint
4726                                                         : -1,
4727                                                      err_rc);
4728
4729         recov = cYAML_get_object_item(tree, "recovery_interval");
4730         if (recov)
4731                 rc = lustre_lnet_config_recov_intrv(recov->cy_valueint,
4732                                                     seq_no ? seq_no->cy_valueint
4733                                                         : -1,
4734                                                     err_rc);
4735
4736         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4737         if (rsen)
4738                 rc = lustre_lnet_config_rtr_sensitivity(rsen->cy_valueint,
4739                                                      seq_no ? seq_no->cy_valueint
4740                                                         : -1,
4741                                                      err_rc);
4742
4743         return rc;
4744 }
4745
4746 static int handle_yaml_del_global_settings(struct cYAML *tree,
4747                                            struct cYAML **show_rc,
4748                                            struct cYAML **err_rc)
4749 {
4750         struct cYAML *max_intf, *numa, *discovery, *seq_no, *drop_asym_route;
4751         int rc = 0;
4752
4753         seq_no = cYAML_get_object_item(tree, "seq_no");
4754         max_intf = cYAML_get_object_item(tree, "max_intf");
4755         if (max_intf)
4756                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
4757                                                  seq_no ? seq_no->cy_valueint
4758                                                         : -1,
4759                                                  err_rc);
4760
4761         numa = cYAML_get_object_item(tree, "numa_range");
4762         if (numa)
4763                 rc = lustre_lnet_config_numa_range(0,
4764                                                    seq_no ? seq_no->cy_valueint
4765                                                         : -1,
4766                                                    err_rc);
4767
4768         /* peer discovery is enabled by default */
4769         discovery = cYAML_get_object_item(tree, "discovery");
4770         if (discovery)
4771                 rc = lustre_lnet_config_discovery(1,
4772                                                   seq_no ? seq_no->cy_valueint
4773                                                         : -1,
4774                                                   err_rc);
4775
4776         /* asymmetrical route messages are accepted by default */
4777         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4778         if (drop_asym_route)
4779                 rc = lustre_lnet_config_drop_asym_route(
4780                         0, seq_no ? seq_no->cy_valueint : -1, err_rc);
4781
4782         return rc;
4783 }
4784
4785 static int handle_yaml_show_global_settings(struct cYAML *tree,
4786                                             struct cYAML **show_rc,
4787                                             struct cYAML **err_rc)
4788 {
4789         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4790                      *sen, *recov, *rsen, *drop_asym_route;
4791         int rc = 0;
4792
4793         seq_no = cYAML_get_object_item(tree, "seq_no");
4794         max_intf = cYAML_get_object_item(tree, "max_intf");
4795         if (max_intf)
4796                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
4797                                                         : -1,
4798                                                 show_rc, err_rc);
4799
4800         numa = cYAML_get_object_item(tree, "numa_range");
4801         if (numa)
4802                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
4803                                                         : -1,
4804                                                  show_rc, err_rc);
4805
4806         discovery = cYAML_get_object_item(tree, "discovery");
4807         if (discovery)
4808                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
4809                                                         : -1,
4810                                                 show_rc, err_rc);
4811
4812         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4813         if (drop_asym_route)
4814                 rc = lustre_lnet_show_drop_asym_route(
4815                         seq_no ? seq_no->cy_valueint : -1,
4816                         show_rc, err_rc);
4817
4818         retry = cYAML_get_object_item(tree, "retry_count");
4819         if (retry)
4820                 rc = lustre_lnet_show_retry_count(seq_no ? seq_no->cy_valueint
4821                                                         : -1,
4822                                                   show_rc, err_rc);
4823
4824         tto = cYAML_get_object_item(tree, "transaction_timeout");
4825         if (tto)
4826                 rc = lustre_lnet_show_transaction_to(seq_no ? seq_no->cy_valueint
4827                                                         : -1,
4828                                                      show_rc, err_rc);
4829
4830         sen = cYAML_get_object_item(tree, "health_sensitivity");
4831         if (sen)
4832                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4833                                                         : -1,
4834                                                      show_rc, err_rc);
4835
4836         recov = cYAML_get_object_item(tree, "recovery_interval");
4837         if (recov)
4838                 rc = lustre_lnet_show_recov_intrv(seq_no ? seq_no->cy_valueint
4839                                                         : -1,
4840                                                   show_rc, err_rc);
4841
4842         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4843         if (rsen)
4844                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4845                                                         : -1,
4846                                                      show_rc, err_rc);
4847
4848         return rc;
4849 }
4850
4851 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
4852                             struct cYAML **err_rc)
4853 {
4854         struct cYAML *seq_no, *nid, *timeout;
4855
4856         seq_no = cYAML_get_object_item(tree, "seq_no");
4857         nid = cYAML_get_object_item(tree, "primary nid");
4858         timeout = cYAML_get_object_item(tree, "timeout");
4859
4860         return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
4861                                     (timeout) ? timeout->cy_valueint : 1000,
4862                                     (seq_no) ? seq_no->cy_valueint : -1,
4863                                     show_rc, err_rc);
4864 }
4865
4866 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
4867                                 struct cYAML **err_rc)
4868 {
4869         struct cYAML *seq_no, *nid, *force;
4870
4871         seq_no = cYAML_get_object_item(tree, "seq_no");
4872         nid = cYAML_get_object_item(tree, "primary nid");
4873         force = cYAML_get_object_item(tree, "force");
4874
4875         return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
4876                                         (force) ? force->cy_valueint : 0,
4877                                         (seq_no) ? seq_no->cy_valueint : -1,
4878                                         show_rc, err_rc);
4879 }
4880
4881 static int handle_yaml_no_op()
4882 {
4883         return LUSTRE_CFG_RC_NO_ERR;
4884 }
4885
4886 struct lookup_cmd_hdlr_tbl {
4887         char *name;
4888         cmd_handler_t cb;
4889 };
4890
4891 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
4892         { .name = "route",      .cb = handle_yaml_config_route },
4893         { .name = "net",        .cb = handle_yaml_config_ni },
4894         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
4895         { .name = "peer",       .cb = handle_yaml_config_peer },
4896         { .name = "routing",    .cb = handle_yaml_config_routing },
4897         { .name = "buffers",    .cb = handle_yaml_config_buffers },
4898         { .name = "statistics", .cb = handle_yaml_no_op },
4899         { .name = "global",     .cb = handle_yaml_config_global_settings},
4900         { .name = "numa",       .cb = handle_yaml_config_numa },
4901         { .name = "ping",       .cb = handle_yaml_no_op },
4902         { .name = "discover",   .cb = handle_yaml_no_op },
4903         { .name = NULL } };
4904
4905 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
4906         { .name = "route",      .cb = handle_yaml_del_route },
4907         { .name = "net",        .cb = handle_yaml_del_ni },
4908         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4909         { .name = "peer",       .cb = handle_yaml_del_peer },
4910         { .name = "routing",    .cb = handle_yaml_del_routing },
4911         { .name = "buffers",    .cb = handle_yaml_no_op },
4912         { .name = "statistics", .cb = handle_yaml_no_op },
4913         { .name = "global",     .cb = handle_yaml_del_global_settings},
4914         { .name = "numa",       .cb = handle_yaml_del_numa },
4915         { .name = "ping",       .cb = handle_yaml_no_op },
4916         { .name = "discover",   .cb = handle_yaml_no_op },
4917         { .name = NULL } };
4918
4919 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
4920         { .name = "route",      .cb = handle_yaml_show_route },
4921         { .name = "net",        .cb = handle_yaml_show_net },
4922         { .name = "peer",       .cb = handle_yaml_show_peers },
4923         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4924         { .name = "routing",    .cb = handle_yaml_show_routing },
4925         { .name = "buffers",    .cb = handle_yaml_show_routing },
4926         { .name = "statistics", .cb = handle_yaml_show_stats },
4927         { .name = "global",     .cb = handle_yaml_show_global_settings},
4928         { .name = "numa",       .cb = handle_yaml_show_numa },
4929         { .name = "ping",       .cb = handle_yaml_no_op },
4930         { .name = "discover",   .cb = handle_yaml_no_op },
4931         { .name = NULL } };
4932
4933 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4934         { .name = "route",      .cb = handle_yaml_no_op },
4935         { .name = "net",        .cb = handle_yaml_no_op },
4936         { .name = "peer",       .cb = handle_yaml_no_op },
4937         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4938         { .name = "routing",    .cb = handle_yaml_no_op },
4939         { .name = "buffers",    .cb = handle_yaml_no_op },
4940         { .name = "statistics", .cb = handle_yaml_no_op },
4941         { .name = "global",     .cb = handle_yaml_no_op },
4942         { .name = "numa",       .cb = handle_yaml_no_op },
4943         { .name = "ping",       .cb = handle_yaml_ping },
4944         { .name = "discover",   .cb = handle_yaml_discover },
4945         { .name = NULL } };
4946
4947 static cmd_handler_t lookup_fn(char *key,
4948                                struct lookup_cmd_hdlr_tbl *tbl)
4949 {
4950         int i;
4951         if (key == NULL)
4952                 return NULL;
4953
4954         for (i = 0; tbl[i].name != NULL; i++) {
4955                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4956                         return tbl[i].cb;
4957         }
4958
4959         return NULL;
4960 }
4961
4962 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4963                                  struct cYAML **show_rc, struct cYAML **err_rc)
4964 {
4965         struct cYAML *tree, *item = NULL, *head, *child;
4966         cmd_handler_t cb;
4967         char err_str[LNET_MAX_STR_LEN];
4968         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4969
4970         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4971         if (tree == NULL)
4972                 return LUSTRE_CFG_RC_BAD_PARAM;
4973
4974         child = tree->cy_child;
4975         while (child != NULL) {
4976                 cb = lookup_fn(child->cy_string, table);
4977                 if (cb == NULL) {
4978                         snprintf(err_str, sizeof(err_str),
4979                                 "\"call back for '%s' not found\"",
4980                                 child->cy_string);
4981                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4982                                         "yaml", "helper", err_str, err_rc);
4983                         goto out;
4984                 }
4985
4986                 if (cYAML_is_sequence(child)) {
4987                         while ((head = cYAML_get_next_seq_item(child, &item))
4988                                != NULL) {
4989                                 rc = cb(head, show_rc, err_rc);
4990                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
4991                                         return_rc = rc;
4992                         }
4993                 } else {
4994                         rc = cb(child, show_rc, err_rc);
4995                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4996                                 return_rc = rc;
4997                 }
4998                 item = NULL;
4999                 child = child->cy_next;
5000         }
5001
5002 out:
5003         cYAML_free_tree(tree);
5004
5005         return return_rc;
5006 }
5007
5008 int lustre_yaml_config(char *f, struct cYAML **err_rc)
5009 {
5010         return lustre_yaml_cb_helper(f, lookup_config_tbl,
5011                                      NULL, err_rc);
5012 }
5013
5014 int lustre_yaml_del(char *f, struct cYAML **err_rc)
5015 {
5016         return lustre_yaml_cb_helper(f, lookup_del_tbl,
5017                                      NULL, err_rc);
5018 }
5019
5020 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
5021 {
5022         return lustre_yaml_cb_helper(f, lookup_show_tbl,
5023                                      show_rc, err_rc);
5024 }
5025
5026 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
5027 {
5028         return lustre_yaml_cb_helper(f, lookup_exec_tbl,
5029                                      show_rc, err_rc);
5030 }