Whamcloud - gitweb
LU-11006 lnet: fix show peer yaml tree with no peer
[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                         "\"cannot parse ip_range '%s'\"", ip_range);
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 '%s'\"", 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
1401
1402         if (nw == NULL || nids == NULL) {
1403                 snprintf(err_str, str_len,
1404                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1405                 err_str[str_len - 1] = '\0';
1406                 return LUSTRE_CFG_RC_BAD_PARAM;
1407         }
1408
1409         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1410                 count = 1;
1411         } else {
1412                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1413                         count++;
1414         }
1415
1416         *nids = calloc(count, sizeof(lnet_nid_t));
1417         if (*nids == NULL) {
1418                 snprintf(err_str, str_len,
1419                          "\"out of memory\"");
1420                 err_str[str_len - 1] = '\0';
1421                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1422         }
1423         /*
1424          * special case the GNI interface since it doesn't have an IP
1425          * address. The assumption is that there can only be one GNI
1426          * interface in the system. No interface name is provided.
1427          */
1428         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1429                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1430                                 1, sizeof(val));
1431                 if (rc) {
1432                         snprintf(err_str, str_len,
1433                                  "\"cannot read gni nid\"");
1434                         err_str[str_len - 1] = '\0';
1435                         goto failed;
1436                 }
1437                 gni_num = atoi(val);
1438
1439                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1440
1441                 goto out;
1442         }
1443
1444         /* look at the other interfaces */
1445         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1446                 rc = lustre_lnet_queryip(intf, &ip);
1447                 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1448                         snprintf(err_str, str_len,
1449                                  "\"couldn't query intf %s\"", intf->intf_name);
1450                         err_str[str_len - 1] = '\0';
1451                         goto failed;
1452                 }
1453                 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1454                 i++;
1455         }
1456
1457 out:
1458         *nnids = count;
1459
1460         return 0;
1461
1462 failed:
1463         free(*nids);
1464         *nids = NULL;
1465         return rc;
1466 }
1467
1468 /*
1469  * called repeatedly until a match or no more ip range
1470  * What do you have?
1471  *      ip_range expression
1472  *      interface list with all the interface names.
1473  *      all the interfaces in the system.
1474  *
1475  *      try to match the ip_range expr to one of the interfaces' IPs in
1476  *      the system. If we hit a patch for an interface. Check if that
1477  *      interface name is in the list.
1478  *
1479  *      If there are more than one interface in the list, then make sure
1480  *      that the IPs for all of these interfaces match the ip ranges
1481  *      given.
1482  *
1483  *      for each interface in intf_list
1484  *              look up the intf name in ifa
1485  *              if not there then no match
1486  *              check ip obtained from ifa against a match to any of the
1487  *              ip_ranges given.
1488  *              If no match, then fail
1489  *
1490  *      The result is that all the interfaces have to match.
1491  */
1492 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1493                                  struct list_head *intf_list,
1494                                  struct list_head *ip_ranges)
1495 {
1496         int rc;
1497         __u32 ip;
1498         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1499         struct ifaddrs *ifaddr = ifa;
1500         struct lustre_lnet_ip_range_descr *ip_range;
1501         int family;
1502
1503         /*
1504          * if there are no explicit interfaces, and no ip ranges, then
1505          * configure the first tcp interface we encounter.
1506          */
1507         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1508                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1509                         if (ifaddr->ifa_addr == NULL)
1510                                 continue;
1511
1512                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1513                                 continue;
1514
1515                         family = ifaddr->ifa_addr->sa_family;
1516                         if (family == AF_INET &&
1517                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1518                                 rc = lustre_lnet_add_intf_descr
1519                                         (intf_list, ifaddr->ifa_name,
1520                                         strlen(ifaddr->ifa_name));
1521
1522                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1523                                         return rc;
1524
1525                                 return LUSTRE_CFG_RC_MATCH;
1526                         }
1527                 }
1528                 return LUSTRE_CFG_RC_NO_MATCH;
1529         }
1530
1531         /*
1532          * First interface which matches an IP pattern will be used
1533          */
1534         if (list_empty(intf_list)) {
1535                 /*
1536                  * no interfaces provided in the rule, but an ip range is
1537                  * provided, so try and match an interface to the ip
1538                  * range.
1539                  */
1540                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1541                         if (ifaddr->ifa_addr == NULL)
1542                                 continue;
1543
1544                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1545                                 continue;
1546
1547                         family = ifaddr->ifa_addr->sa_family;
1548                         if (family == AF_INET) {
1549                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1550                                         sin_addr.s_addr;
1551
1552                                 list_for_each_entry(ip_range, ip_ranges,
1553                                                     ipr_entry) {
1554                                         rc = cfs_ip_addr_match(bswap_32(ip),
1555                                                         &ip_range->ipr_expr);
1556                                         if (!rc)
1557                                                 continue;
1558
1559                                         rc = lustre_lnet_add_intf_descr
1560                                           (intf_list, ifaddr->ifa_name,
1561                                            strlen(ifaddr->ifa_name));
1562
1563                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1564                                                 return rc;
1565                                 }
1566                         }
1567                 }
1568
1569                 if (!list_empty(intf_list))
1570                         return LUSTRE_CFG_RC_MATCH;
1571
1572                 return LUSTRE_CFG_RC_NO_MATCH;
1573         }
1574
1575         /*
1576          * If an interface is explicitly specified the ip-range might or
1577          * might not be specified. if specified the interface needs to match the
1578          * ip-range. If no ip-range then the interfaces are
1579          * automatically matched if they are all up.
1580          * If > 1 interfaces all the interfaces must match for the NI to
1581          * be configured.
1582          */
1583         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1584                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1585                         if (ifaddr->ifa_addr == NULL)
1586                                 continue;
1587
1588                         family = ifaddr->ifa_addr->sa_family;
1589                         if (family == AF_INET &&
1590                             strcmp(intf_descr->intf_name,
1591                                    ifaddr->ifa_name) == 0)
1592                                 break;
1593                 }
1594
1595                 if (ifaddr == NULL) {
1596                         list_del(&intf_descr->intf_on_network);
1597                         free_intf_descr(intf_descr);
1598                         continue;
1599                 }
1600
1601                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1602                         list_del(&intf_descr->intf_on_network);
1603                         free_intf_descr(intf_descr);
1604                         continue;
1605                 }
1606
1607                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1608
1609                 rc = 1;
1610                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1611                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1612                         if (rc)
1613                                 break;
1614                 }
1615
1616                 if (!rc) {
1617                         /* no match for this interface */
1618                         list_del(&intf_descr->intf_on_network);
1619                         free_intf_descr(intf_descr);
1620                 }
1621         }
1622
1623         return LUSTRE_CFG_RC_MATCH;
1624 }
1625
1626 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1627                                             lnet_nid_t **nids, __u32 *nnids,
1628                                             char *err_str, size_t str_len)
1629 {
1630         struct ifaddrs *ifa;
1631         int rc = LUSTRE_CFG_RC_NO_ERR;
1632
1633         rc = getifaddrs(&ifa);
1634         if (rc < 0) {
1635                 snprintf(err_str, str_len,
1636                          "\"failed to get interface addresses: %d\"", -errno);
1637                 err_str[str_len - 1] = '\0';
1638                 return -errno;
1639         }
1640
1641         rc = lustre_lnet_match_ip_to_intf(ifa,
1642                                           &ip2nets->ip2nets_net.nw_intflist,
1643                                           &ip2nets->ip2nets_ip_ranges);
1644         if (rc != LUSTRE_CFG_RC_MATCH) {
1645                 snprintf(err_str, str_len,
1646                          "\"couldn't match ip to existing interfaces\"");
1647                 err_str[str_len - 1] = '\0';
1648                 freeifaddrs(ifa);
1649                 return rc;
1650         }
1651
1652         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1653                                    err_str, sizeof(err_str));
1654         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1655                 *nids = NULL;
1656                 *nnids = 0;
1657         }
1658
1659         freeifaddrs(ifa);
1660
1661         return rc;
1662 }
1663
1664 static int
1665 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1666                             struct lnet_ioctl_config_lnd_tunables *tunables,
1667                             struct cfs_expr_list *global_cpts,
1668                             lnet_nid_t *nids, char *err_str)
1669 {
1670         char *data;
1671         struct lnet_ioctl_config_ni *conf;
1672         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1673         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1674         size_t len;
1675         int count;
1676         struct lnet_dlc_intf_descr *intf_descr;
1677         __u32 *cpt_array;
1678         struct cfs_expr_list *cpt_expr;
1679
1680         list_for_each_entry(intf_descr, intf_list,
1681                             intf_on_network) {
1682                 if (tunables != NULL)
1683                         len = sizeof(struct lnet_ioctl_config_ni) +
1684                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1685                 else
1686                         len = sizeof(struct lnet_ioctl_config_ni);
1687
1688                 data = calloc(1, len);
1689                 if (!data)
1690                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1691                 conf = (struct lnet_ioctl_config_ni*) data;
1692                 if (tunables != NULL)
1693                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1694                                 conf->lic_bulk;
1695
1696                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1697                 conf->lic_cfg_hdr.ioc_len = len;
1698                 conf->lic_nid = nids[i];
1699                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1700                         LNET_MAX_STR_LEN);
1701
1702                 if (intf_descr->cpt_expr != NULL)
1703                         cpt_expr = intf_descr->cpt_expr;
1704                 else if (global_cpts != NULL)
1705                         cpt_expr = global_cpts;
1706                 else
1707                         cpt_expr = NULL;
1708
1709                 if (cpt_expr != NULL) {
1710                         count = cfs_expr_list_values(cpt_expr,
1711                                                      LNET_MAX_SHOW_NUM_CPT,
1712                                                      &cpt_array);
1713                         if (count > 0) {
1714                                 memcpy(conf->lic_cpts, cpt_array,
1715                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1716                                 free(cpt_array);
1717                         } else {
1718                                 count = 0;
1719                         }
1720                 } else {
1721                         count = 0;
1722                 }
1723
1724                 conf->lic_ncpts = count;
1725
1726                 if (tunables != NULL)
1727                         memcpy(tun, tunables, sizeof(*tunables));
1728
1729                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1730                 if (rc < 0) {
1731                         rc = -errno;
1732                         snprintf(err_str,
1733                                  LNET_MAX_STR_LEN,
1734                                  "\"cannot add network: %s\"", strerror(errno));
1735                         free(data);
1736                         return rc;
1737                 }
1738                 free(data);
1739                 i++;
1740         }
1741
1742         return LUSTRE_CFG_RC_NO_ERR;
1743 }
1744
1745 int
1746 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1747                            struct lnet_ioctl_config_lnd_tunables *tunables,
1748                            struct cfs_expr_list *global_cpts,
1749                            int seq_no, struct cYAML **err_rc)
1750 {
1751         lnet_nid_t *nids = NULL;
1752         __u32 nnids = 0;
1753         int rc;
1754         char err_str[LNET_MAX_STR_LEN];
1755
1756         snprintf(err_str, sizeof(err_str), "\"success\"");
1757
1758         if (!ip2nets) {
1759                 snprintf(err_str,
1760                          sizeof(err_str),
1761                          "\"incomplete ip2nets information\"");
1762                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1763                 goto out;
1764         }
1765
1766         /*
1767          * call below function to resolve the rules into a list of nids.
1768          * The memory is allocated in that function then freed here when
1769          * it's no longer needed.
1770          */
1771         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1772                                               sizeof(err_str));
1773         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1774                 goto out;
1775
1776         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1777                 snprintf(err_str, sizeof(err_str),
1778                          "\"no interfaces match ip2nets rules\"");
1779                 goto free_nids_out;
1780         }
1781
1782         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1783                                          tunables, global_cpts, nids,
1784                                          err_str);
1785
1786 free_nids_out:
1787         free(nids);
1788
1789 out:
1790         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1791         return rc;
1792 }
1793
1794 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1795                           struct cfs_expr_list *global_cpts,
1796                           char *ip2net,
1797                           struct lnet_ioctl_config_lnd_tunables *tunables,
1798                           int seq_no, struct cYAML **err_rc)
1799 {
1800         char *data = NULL;
1801         struct lnet_ioctl_config_ni *conf;
1802         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1803         char buf[LNET_MAX_STR_LEN];
1804         int rc = LUSTRE_CFG_RC_NO_ERR;
1805         char err_str[LNET_MAX_STR_LEN];
1806         lnet_nid_t *nids = NULL;
1807         __u32 nnids = 0;
1808         size_t len;
1809         int count;
1810         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1811         __u32 *cpt_array;
1812
1813         snprintf(err_str, sizeof(err_str), "\"success\"");
1814
1815         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1816             (list_empty(&nw_descr->nw_intflist) &&
1817              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1818                 snprintf(err_str,
1819                          sizeof(err_str),
1820                          "\"missing mandatory parameters in NI config: '%s'\"",
1821                          (nw_descr == NULL) ? "network , interface" :
1822                          (nw_descr->nw_id == 0) ? "network" : "interface");
1823                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1824                 goto out;
1825         }
1826
1827         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1828                 snprintf(err_str,
1829                          sizeof(err_str),
1830                          "\"ip2net string too long %d\"",
1831                                 (int)strlen(ip2net));
1832                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1833                 goto out;
1834         }
1835
1836         if (ip2net != NULL) {
1837                 if (tunables != NULL)
1838                         len = sizeof(struct lnet_ioctl_config_ni) +
1839                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1840                 else
1841                         len = sizeof(struct lnet_ioctl_config_ni);
1842                 data = calloc(1, len);
1843                 if (!data) {
1844                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1845                         goto out;
1846                 }
1847                 conf = (struct lnet_ioctl_config_ni*) data;
1848                 if (tunables != NULL)
1849                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1850                                 (data + sizeof(*conf));
1851
1852                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1853                 conf->lic_cfg_hdr.ioc_len = len;
1854                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1855                         LNET_MAX_STR_LEN);
1856
1857                 if (global_cpts != NULL) {
1858                         count = cfs_expr_list_values(global_cpts,
1859                                                      LNET_MAX_SHOW_NUM_CPT,
1860                                                      &cpt_array);
1861                         if (count > 0) {
1862                                 memcpy(conf->lic_cpts, cpt_array,
1863                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1864                                 free(cpt_array);
1865                         } else {
1866                                 count = 0;
1867                         }
1868                 } else {
1869                         count = 0;
1870                 }
1871
1872                 conf->lic_ncpts = count;
1873
1874                 if (tunables != NULL)
1875                         memcpy(tun, tunables, sizeof(*tunables));
1876
1877                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1878                 if (rc < 0) {
1879                         rc = -errno;
1880                         snprintf(err_str,
1881                                 sizeof(err_str),
1882                                 "\"cannot add network: %s\"", strerror(errno));
1883                         goto out;
1884                 }
1885
1886                 goto out;
1887         }
1888
1889         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1890                 rc = LUSTRE_CFG_RC_NO_ERR;
1891                 goto out;
1892         }
1893
1894         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1895                 snprintf(err_str,
1896                         sizeof(err_str),
1897                         "\"cannot parse net '%s'\"",
1898                         libcfs_net2str(nw_descr->nw_id));
1899                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1900                 goto out;
1901         }
1902
1903         /*
1904          * special case the GNI since no interface name is expected
1905          */
1906         if (list_empty(&nw_descr->nw_intflist) &&
1907             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1908                 snprintf(err_str,
1909                         sizeof(err_str),
1910                         "\"no interface name provided\"");
1911                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1912                 goto out;
1913         }
1914
1915         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1916                                    err_str, sizeof(err_str));
1917         if (rc != 0) {
1918                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1919                 goto out;
1920         }
1921
1922         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1923                                          tunables, global_cpts, nids,
1924                                          err_str);
1925
1926 out:
1927         if (nw_descr != NULL) {
1928                 list_for_each_entry_safe(intf_descr, tmp,
1929                                          &nw_descr->nw_intflist,
1930                                          intf_on_network) {
1931                         list_del(&intf_descr->intf_on_network);
1932                         free_intf_descr(intf_descr);
1933                 }
1934         }
1935
1936         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1937
1938         if (nids)
1939                 free(nids);
1940
1941         if (data)
1942                 free(data);
1943
1944         return rc;
1945 }
1946
1947 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1948                        int seq_no, struct cYAML **err_rc)
1949 {
1950         struct lnet_ioctl_config_ni data;
1951         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1952         char err_str[LNET_MAX_STR_LEN];
1953         lnet_nid_t *nids = NULL;
1954         __u32 nnids = 0;
1955         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1956
1957         snprintf(err_str, sizeof(err_str), "\"success\"");
1958
1959         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1960                 snprintf(err_str,
1961                          sizeof(err_str),
1962                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1963                          (nw_descr == NULL) ? "network , interface" :
1964                          (nw_descr->nw_id == 0) ? "network" : "interface");
1965                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1966                 goto out;
1967         }
1968
1969         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1970                 return LUSTRE_CFG_RC_NO_ERR;
1971
1972         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1973                 snprintf(err_str,
1974                          sizeof(err_str),
1975                          "\"cannot parse net '%s'\"",
1976                          libcfs_net2str(nw_descr->nw_id));
1977                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1978                 goto out;
1979         }
1980
1981         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1982                                    err_str, sizeof(err_str));
1983         if (rc != 0) {
1984                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1985                 goto out;
1986         }
1987
1988         /*
1989          * no interfaces just the nw_id is specified
1990          */
1991         if (nnids == 0) {
1992                 nids = calloc(1, sizeof(*nids));
1993                 if (nids == NULL) {
1994                         snprintf(err_str, sizeof(err_str),
1995                                 "\"out of memory\"");
1996                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1997                         goto out;
1998                 }
1999                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
2000                 nnids = 1;
2001         }
2002
2003         for (i = 0; i < nnids; i++) {
2004                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
2005                 data.lic_nid = nids[i];
2006
2007                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
2008                 if (rc < 0) {
2009                         rc = -errno;
2010                         snprintf(err_str,
2011                                 sizeof(err_str),
2012                                 "\"cannot del network: %s\"", strerror(errno));
2013                 }
2014         }
2015
2016         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2017                                  intf_on_network) {
2018                 list_del(&intf_descr->intf_on_network);
2019                 free_intf_descr(intf_descr);
2020         }
2021
2022 out:
2023         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
2024
2025         if (nids != NULL)
2026                 free(nids);
2027
2028         return rc;
2029 }
2030
2031 static int
2032 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
2033                            enum lnet_health_type type, char *name,
2034                            int seq_no, struct cYAML **err_rc)
2035 {
2036         struct lnet_ioctl_reset_health_cfg data;
2037         int rc = LUSTRE_CFG_RC_NO_ERR;
2038         char err_str[LNET_MAX_STR_LEN];
2039
2040         snprintf(err_str, sizeof(err_str), "\"success\"");
2041
2042         LIBCFS_IOC_INIT_V2(data, rh_hdr);
2043         data.rh_type = type;
2044         data.rh_all = all;
2045         data.rh_value = value;
2046         data.rh_nid = nid;
2047
2048         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
2049         if (rc != 0) {
2050                 rc = -errno;
2051                 snprintf(err_str,
2052                          sizeof(err_str), "Can not configure health value");
2053         }
2054
2055         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2056
2057         return rc;
2058 }
2059
2060 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
2061                                   struct cYAML **err_rc)
2062 {
2063         lnet_nid_t nid;
2064         if (ni_nid)
2065                 nid = libcfs_str2nid(ni_nid);
2066         else
2067                 nid = LNET_NID_ANY;
2068         return lustre_lnet_config_healthv(value, all, nid,
2069                                           LNET_HEALTH_TYPE_LOCAL_NI,
2070                                           "ni healthv", seq_no, err_rc);
2071 }
2072
2073 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
2074                                        int seq_no, struct cYAML **err_rc)
2075 {
2076         lnet_nid_t nid;
2077         if (lpni_nid)
2078                 nid = libcfs_str2nid(lpni_nid);
2079         else
2080                 nid = LNET_NID_ANY;
2081         return lustre_lnet_config_healthv(value, all, nid,
2082                                           LNET_HEALTH_TYPE_PEER_NI,
2083                                           "peer_ni healthv", seq_no, err_rc);
2084 }
2085
2086 static bool
2087 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
2088                           struct lnet_ioctl_comm_count *counts)
2089 {
2090         if (cYAML_create_number(yaml, "put",
2091                                 counts->ico_put_count)
2092                                         == NULL)
2093                 return false;
2094         if (cYAML_create_number(yaml, "get",
2095                                 counts->ico_get_count)
2096                                         == NULL)
2097                 return false;
2098         if (cYAML_create_number(yaml, "reply",
2099                                 counts->ico_reply_count)
2100                                         == NULL)
2101                 return false;
2102         if (cYAML_create_number(yaml, "ack",
2103                                 counts->ico_ack_count)
2104                                         == NULL)
2105                 return false;
2106         if (cYAML_create_number(yaml, "hello",
2107                                 counts->ico_hello_count)
2108                                         == NULL)
2109                 return false;
2110
2111         return true;
2112 }
2113
2114 static struct lnet_ioctl_comm_count *
2115 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
2116 {
2117         if (idx == 0)
2118                 return &msg_stats->im_send_stats;
2119         if (idx == 1)
2120                 return &msg_stats->im_recv_stats;
2121         if (idx == 2)
2122                 return &msg_stats->im_drop_stats;
2123
2124         return NULL;
2125 }
2126
2127 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
2128                          struct cYAML **show_rc, struct cYAML **err_rc,
2129                          bool backup)
2130 {
2131         char *buf;
2132         struct lnet_ioctl_config_ni *ni_data;
2133         struct lnet_ioctl_config_lnd_tunables *lnd;
2134         struct lnet_ioctl_element_stats *stats;
2135         struct lnet_ioctl_element_msg_stats msg_stats;
2136         struct lnet_ioctl_local_ni_hstats hstats;
2137         __u32 net = LNET_NIDNET(LNET_NID_ANY);
2138         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
2139         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
2140         int l_errno = 0;
2141         struct cYAML *root = NULL, *tunables = NULL,
2142                 *net_node = NULL, *interfaces = NULL,
2143                 *item = NULL, *first_seq = NULL,
2144                 *tmp = NULL, *statistics = NULL,
2145                 *yhstats = NULL;
2146         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
2147         char str_buf[str_buf_len];
2148         char *pos;
2149         char err_str[LNET_MAX_STR_LEN];
2150         bool exist = false, new_net = true;
2151         int net_num = 0;
2152         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
2153
2154         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2155
2156         buf = calloc(1, buf_size);
2157         if (buf == NULL)
2158                 goto out;
2159
2160         ni_data = (struct lnet_ioctl_config_ni *)buf;
2161
2162         if (nw != NULL) {
2163                 net = libcfs_str2net(nw);
2164                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
2165                         snprintf(err_str,
2166                                  sizeof(err_str),
2167                                  "\"cannot parse net '%s'\"", nw);
2168                         rc = LUSTRE_CFG_RC_BAD_PARAM;
2169                         goto out;
2170                 }
2171         }
2172
2173         root = cYAML_create_object(NULL, NULL);
2174         if (root == NULL)
2175                 goto out;
2176
2177         net_node = cYAML_create_seq(root, "net");
2178         if (net_node == NULL)
2179                 goto out;
2180
2181         for (i = 0;; i++) {
2182                 pos = str_buf;
2183                 __u32 rc_net;
2184
2185                 memset(buf, 0, buf_size);
2186
2187                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
2188                 /*
2189                  * set the ioc_len to the proper value since INIT assumes
2190                  * size of data
2191                  */
2192                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
2193                 ni_data->lic_idx = i;
2194
2195                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
2196                 if (rc != 0) {
2197                         l_errno = errno;
2198                         break;
2199                 }
2200
2201                 rc_net = LNET_NIDNET(ni_data->lic_nid);
2202
2203                 /* filter on provided data */
2204                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
2205                     net != rc_net)
2206                         continue;
2207
2208                 /* if we're backing up don't store lo */
2209                 if (backup && LNET_NETTYP(rc_net) == LOLND)
2210                         continue;
2211
2212                 /* default rc to -1 in case we hit the goto */
2213                 rc = -1;
2214                 exist = true;
2215
2216                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
2217                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
2218                         (ni_data->lic_bulk + sizeof(*stats));
2219
2220                 if (rc_net != prev_net) {
2221                         prev_net = rc_net;
2222                         new_net = true;
2223                         net_num++;
2224                 }
2225
2226                 if (new_net) {
2227                         if (!cYAML_create_string(net_node, "net type",
2228                                                  libcfs_net2str(rc_net)))
2229                                 goto out;
2230
2231                         tmp = cYAML_create_seq(net_node, "local NI(s)");
2232                         if (tmp == NULL)
2233                                 goto out;
2234                         new_net = false;
2235                 }
2236
2237                 /* create the tree to be printed. */
2238                 item = cYAML_create_seq_item(tmp);
2239                 if (item == NULL)
2240                         goto out;
2241
2242                 if (first_seq == NULL)
2243                         first_seq = item;
2244
2245                 if (!backup &&
2246                     cYAML_create_string(item, "nid",
2247                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2248                         goto out;
2249
2250                 if (!backup &&
2251                     cYAML_create_string(item,
2252                                         "status",
2253                                         (ni_data->lic_status ==
2254                                           LNET_NI_STATUS_UP) ?
2255                                             "up" : "down") == NULL)
2256                         goto out;
2257
2258                 /* don't add interfaces unless there is at least one
2259                  * interface */
2260                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2261                         interfaces = cYAML_create_object(item, "interfaces");
2262                         if (interfaces == NULL)
2263                                 goto out;
2264
2265                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2266                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2267                                         snprintf(str_buf,
2268                                                  sizeof(str_buf), "%d", j);
2269                                         if (cYAML_create_string(interfaces,
2270                                                 str_buf,
2271                                                 ni_data->lic_ni_intf[j]) ==
2272                                                         NULL)
2273                                                 goto out;
2274                                 }
2275                         }
2276                 }
2277
2278                 if (detail) {
2279                         char *limit;
2280                         int k;
2281
2282                         if (backup)
2283                                 goto continue_without_msg_stats;
2284
2285                         statistics = cYAML_create_object(item, "statistics");
2286                         if (statistics == NULL)
2287                                 goto out;
2288
2289                         if (cYAML_create_number(statistics, "send_count",
2290                                                 stats->iel_send_count)
2291                                                         == NULL)
2292                                 goto out;
2293
2294                         if (cYAML_create_number(statistics, "recv_count",
2295                                                 stats->iel_recv_count)
2296                                                         == NULL)
2297                                 goto out;
2298
2299                         if (cYAML_create_number(statistics, "drop_count",
2300                                                 stats->iel_drop_count)
2301                                                         == NULL)
2302                                 goto out;
2303
2304                         if (detail < 2)
2305                                 goto continue_without_msg_stats;
2306
2307                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2308                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2309                         msg_stats.im_idx = i;
2310
2311                         rc = l_ioctl(LNET_DEV_ID,
2312                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2313                                      &msg_stats);
2314                         if (rc != 0) {
2315                                 l_errno = errno;
2316                                 goto continue_without_msg_stats;
2317                         }
2318
2319                         for (k = 0; k < 3; k++) {
2320                                 struct lnet_ioctl_comm_count *counts;
2321                                 struct cYAML *msg_statistics = NULL;
2322
2323                                 msg_statistics = cYAML_create_object(item,
2324                                                  (char *)gmsg_stat_names[k]);
2325                                 if (msg_statistics == NULL)
2326                                         goto out;
2327
2328                                 counts = get_counts(&msg_stats, k);
2329                                 if (counts == NULL)
2330                                         goto out;
2331
2332                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2333                                                                counts))
2334                                         goto out;
2335                         }
2336
2337                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2338                         hstats.hlni_nid = ni_data->lic_nid;
2339                         /* grab health stats */
2340                         rc = l_ioctl(LNET_DEV_ID,
2341                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2342                                      &hstats);
2343                         if (rc != 0) {
2344                                 l_errno = errno;
2345                                 goto continue_without_msg_stats;
2346                         }
2347                         yhstats = cYAML_create_object(item, "health stats");
2348                         if (!yhstats)
2349                                 goto out;
2350                         if (cYAML_create_number(yhstats, "health value",
2351                                                 hstats.hlni_health_value)
2352                                                         == NULL)
2353                                 goto out;
2354                         if (cYAML_create_number(yhstats, "interrupts",
2355                                                 hstats.hlni_local_interrupt)
2356                                                         == NULL)
2357                                 goto out;
2358                         if (cYAML_create_number(yhstats, "dropped",
2359                                                 hstats.hlni_local_dropped)
2360                                                         == NULL)
2361                                 goto out;
2362                         if (cYAML_create_number(yhstats, "aborted",
2363                                                 hstats.hlni_local_aborted)
2364                                                         == NULL)
2365                                 goto out;
2366                         if (cYAML_create_number(yhstats, "no route",
2367                                                 hstats.hlni_local_no_route)
2368                                                         == NULL)
2369                                 goto out;
2370                         if (cYAML_create_number(yhstats, "timeouts",
2371                                                 hstats.hlni_local_timeout)
2372                                                         == NULL)
2373                                 goto out;
2374                         if (cYAML_create_number(yhstats, "error",
2375                                                 hstats.hlni_local_error)
2376                                                         == NULL)
2377                                 goto out;
2378
2379 continue_without_msg_stats:
2380                         tunables = cYAML_create_object(item, "tunables");
2381                         if (!tunables)
2382                                 goto out;
2383
2384                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2385                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2386                                 goto out;
2387
2388                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2389                                                      &lnd->lt_tun);
2390                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2391                             rc != LUSTRE_CFG_RC_NO_MATCH)
2392                                 goto out;
2393
2394                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2395                                 tunables = cYAML_create_object(item,
2396                                                                "lnd tunables");
2397                                 if (tunables == NULL)
2398                                         goto out;
2399                         }
2400
2401                         if (!backup &&
2402                             cYAML_create_number(item, "dev cpt",
2403                                                 ni_data->lic_dev_cpt) == NULL)
2404                                 goto out;
2405
2406                         if (!backup &&
2407                             cYAML_create_number(item, "tcp bonding",
2408                                                 ni_data->lic_tcp_bonding)
2409                                                         == NULL)
2410                                 goto out;
2411
2412                         /* out put the CPTs in the format: "[x,x,x,...]" */
2413                         limit = str_buf + str_buf_len - 3;
2414                         pos += snprintf(pos, limit - pos, "\"[");
2415                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2416                                 j < ni_data->lic_ncpts &&
2417                                 pos < limit; j++) {
2418                                 pos += snprintf(pos, limit - pos,
2419                                                 "%d", ni_data->lic_cpts[j]);
2420                                 if ((j + 1) < ni_data->lic_ncpts)
2421                                         pos += snprintf(pos, limit - pos, ",");
2422                         }
2423                         pos += snprintf(pos, 3, "]\"");
2424
2425                         if (ni_data->lic_ncpts >= 1 &&
2426                             cYAML_create_string(item, "CPT",
2427                                                 str_buf) == NULL)
2428                                 goto out;
2429                 }
2430         }
2431
2432         /* Print out the net information only if show_rc is not provided */
2433         if (show_rc == NULL)
2434                 cYAML_print_tree(root);
2435
2436         if (l_errno != ENOENT) {
2437                 snprintf(err_str,
2438                          sizeof(err_str),
2439                          "\"cannot get networks: %s\"",
2440                          strerror(l_errno));
2441                 rc = -l_errno;
2442                 goto out;
2443         } else
2444                 rc = LUSTRE_CFG_RC_NO_ERR;
2445
2446         snprintf(err_str, sizeof(err_str), "\"success\"");
2447 out:
2448         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2449                 cYAML_free_tree(root);
2450         } else if (show_rc != NULL && *show_rc != NULL) {
2451                 struct cYAML *show_node;
2452                 /* find the net node, if one doesn't exist
2453                  * then insert one.  Otherwise add to the one there
2454                  */
2455                 show_node = cYAML_get_object_item(*show_rc, "net");
2456                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2457                         cYAML_insert_child(show_node, first_seq);
2458                         free(net_node);
2459                         free(root);
2460                 } else if (show_node == NULL) {
2461                         cYAML_insert_sibling((*show_rc)->cy_child,
2462                                                 net_node);
2463                         free(root);
2464                 } else {
2465                         cYAML_free_tree(root);
2466                 }
2467         } else {
2468                 *show_rc = root;
2469         }
2470
2471         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2472
2473         return rc;
2474 }
2475
2476 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2477 {
2478         struct lnet_ioctl_config_data data;
2479         int rc = LUSTRE_CFG_RC_NO_ERR;
2480         char err_str[LNET_MAX_STR_LEN];
2481
2482         snprintf(err_str, sizeof(err_str), "\"success\"");
2483
2484         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2485         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2486
2487         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2488         if (rc != 0) {
2489                 rc = -errno;
2490                 snprintf(err_str,
2491                          sizeof(err_str),
2492                          "\"cannot %s routing %s\"",
2493                          (enable) ? "enable" : "disable", strerror(errno));
2494                 goto out;
2495         }
2496
2497 out:
2498         cYAML_build_error(rc, seq_no,
2499                          (enable) ? ADD_CMD : DEL_CMD,
2500                          "routing", err_str, err_rc);
2501
2502         return rc;
2503 }
2504
2505 int ioctl_set_value(__u32 val, int ioc, char *name,
2506                     int seq_no, struct cYAML **err_rc)
2507 {
2508         struct lnet_ioctl_set_value data;
2509         int rc = LUSTRE_CFG_RC_NO_ERR;
2510         char err_str[LNET_MAX_STR_LEN];
2511
2512         snprintf(err_str, sizeof(err_str), "\"success\"");
2513
2514         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2515         data.sv_value = val;
2516
2517         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2518         if (rc != 0) {
2519                 rc = -errno;
2520                 snprintf(err_str,
2521                          sizeof(err_str),
2522                          "\"cannot configure %s to %d: %s\"", name,
2523                          val, strerror(errno));
2524         }
2525
2526         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2527
2528         return rc;
2529 }
2530
2531 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2532 {
2533         int rc = LUSTRE_CFG_RC_NO_ERR;
2534         char err_str[LNET_MAX_STR_LEN];
2535         char val[LNET_MAX_STR_LEN];
2536
2537         snprintf(err_str, sizeof(err_str), "\"success\"");
2538
2539         snprintf(val, sizeof(val), "%d", sen);
2540
2541         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2542                               1, strlen(val) + 1);
2543         if (rc)
2544                 snprintf(err_str, sizeof(err_str),
2545                          "\"cannot configure health sensitivity: %s\"",
2546                          strerror(errno));
2547
2548         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2549
2550         return rc;
2551 }
2552
2553 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2554 {
2555         int rc = LUSTRE_CFG_RC_NO_ERR;
2556         char err_str[LNET_MAX_STR_LEN];
2557         char val[LNET_MAX_STR_LEN];
2558
2559         snprintf(err_str, sizeof(err_str), "\"success\"");
2560
2561         snprintf(val, sizeof(val), "%d", timeout);
2562
2563         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2564                               1, strlen(val) + 1);
2565         if (rc)
2566                 snprintf(err_str, sizeof(err_str),
2567                          "\"cannot configure transaction timeout: %s\"",
2568                          strerror(errno));
2569
2570         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2571
2572         return rc;
2573 }
2574
2575 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2576 {
2577         int rc = LUSTRE_CFG_RC_NO_ERR;
2578         char err_str[LNET_MAX_STR_LEN];
2579         char val[LNET_MAX_STR_LEN];
2580
2581         snprintf(err_str, sizeof(err_str), "\"success\"");
2582
2583         snprintf(val, sizeof(val), "%d", count);
2584
2585         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2586                               1, strlen(val) + 1);
2587         if (rc)
2588                 snprintf(err_str, sizeof(err_str),
2589                          "\"cannot configure retry count: %s\"",
2590                          strerror(errno));
2591
2592         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2593
2594         return rc;
2595 }
2596
2597 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2598 {
2599         int rc = LUSTRE_CFG_RC_NO_ERR;
2600         char err_str[LNET_MAX_STR_LEN];
2601         char val[LNET_MAX_STR_LEN];
2602
2603         snprintf(err_str, sizeof(err_str), "\"success\"");
2604
2605         snprintf(val, sizeof(val), "%d", max);
2606
2607         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2608                               1, strlen(val) + 1);
2609         if (rc)
2610                 snprintf(err_str, sizeof(err_str),
2611                          "\"cannot configure max interfaces: %s\"",
2612                          strerror(errno));
2613
2614         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2615
2616         return rc;
2617 }
2618
2619 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2620 {
2621         int rc = LUSTRE_CFG_RC_NO_ERR;
2622         char err_str[LNET_MAX_STR_LEN];
2623         char val[LNET_MAX_STR_LEN];
2624
2625         snprintf(err_str, sizeof(err_str), "\"success\"");
2626
2627         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2628
2629         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2630                               1, strlen(val) + 1);
2631         if (rc)
2632                 snprintf(err_str, sizeof(err_str),
2633                          "\"cannot configure discovery: %s\"",
2634                          strerror(errno));
2635
2636         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2637
2638         return rc;
2639
2640 }
2641
2642 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2643 {
2644         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2645                                "numa_range", seq_no, err_rc);
2646 }
2647
2648 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2649                                struct cYAML **err_rc)
2650 {
2651         struct lnet_ioctl_config_data data;
2652         int rc = LUSTRE_CFG_RC_NO_ERR;
2653         char err_str[LNET_MAX_STR_LEN];
2654
2655         snprintf(err_str, sizeof(err_str), "\"success\"");
2656
2657         /* -1 indicates to ignore changes to this field */
2658         if (tiny < -1 || small < -1 || large < -1) {
2659                 snprintf(err_str,
2660                          sizeof(err_str),
2661                          "\"tiny, small and large must be >= 0\"");
2662                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2663                 goto out;
2664         }
2665
2666         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2667         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2668         data.cfg_config_u.cfg_buffers.buf_small = small;
2669         data.cfg_config_u.cfg_buffers.buf_large = large;
2670
2671         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2672         if (rc != 0) {
2673                 rc = -errno;
2674                 snprintf(err_str,
2675                          sizeof(err_str),
2676                          "\"cannot configure buffers: %s\"", strerror(errno));
2677                 goto out;
2678         }
2679
2680 out:
2681         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2682
2683         return rc;
2684 }
2685
2686 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2687                              struct cYAML **err_rc, bool backup)
2688 {
2689         struct lnet_ioctl_config_data *data;
2690         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2691         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2692         int l_errno = 0;
2693         char *buf;
2694         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2695         int buf_count[LNET_NRBPOOLS] = {0};
2696         struct cYAML *root = NULL, *pools_node = NULL,
2697                      *type_node = NULL, *item = NULL, *cpt = NULL,
2698                      *first_seq = NULL, *buffers = NULL;
2699         int i, j;
2700         char err_str[LNET_MAX_STR_LEN];
2701         char node_name[LNET_MAX_STR_LEN];
2702         bool exist = false;
2703
2704         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2705
2706         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2707         if (buf == NULL)
2708                 goto out;
2709
2710         data = (struct lnet_ioctl_config_data *)buf;
2711
2712         root = cYAML_create_object(NULL, NULL);
2713         if (root == NULL)
2714                 goto out;
2715
2716         if (backup)
2717                 pools_node = cYAML_create_object(root, "routing");
2718         else
2719                 pools_node = cYAML_create_seq(root, "routing");
2720         if (pools_node == NULL)
2721                 goto out;
2722
2723         for (i = 0;; i++) {
2724                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2725                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2726                                         sizeof(struct lnet_ioctl_pool_cfg);
2727                 data->cfg_count = i;
2728
2729                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2730                 if (rc != 0) {
2731                         l_errno = errno;
2732                         break;
2733                 }
2734
2735                 exist = true;
2736
2737                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2738
2739                 if (backup)
2740                         goto calculate_buffers;
2741
2742                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2743                 item = cYAML_create_seq_item(pools_node);
2744                 if (item == NULL)
2745                         goto out;
2746
2747                 if (first_seq == NULL)
2748                         first_seq = item;
2749
2750                 cpt = cYAML_create_object(item, node_name);
2751                 if (cpt == NULL)
2752                         goto out;
2753
2754 calculate_buffers:
2755                 /* create the tree  and print */
2756                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2757                         if (!backup) {
2758                                 type_node = cYAML_create_object(cpt, pools[j]);
2759                                 if (type_node == NULL)
2760                                         goto out;
2761                         }
2762                         if (!backup &&
2763                             cYAML_create_number(type_node, "npages",
2764                                                 pool_cfg->pl_pools[j].pl_npages)
2765                             == NULL)
2766                                 goto out;
2767                         if (!backup &&
2768                             cYAML_create_number(type_node, "nbuffers",
2769                                                 pool_cfg->pl_pools[j].
2770                                                   pl_nbuffers) == NULL)
2771                                 goto out;
2772                         if (!backup &&
2773                             cYAML_create_number(type_node, "credits",
2774                                                 pool_cfg->pl_pools[j].
2775                                                    pl_credits) == NULL)
2776                                 goto out;
2777                         if (!backup &&
2778                             cYAML_create_number(type_node, "mincredits",
2779                                                 pool_cfg->pl_pools[j].
2780                                                    pl_mincredits) == NULL)
2781                                 goto out;
2782                         /* keep track of the total count for each of the
2783                          * tiny, small and large buffers */
2784                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2785                 }
2786         }
2787
2788         if (pool_cfg != NULL) {
2789                 if (backup) {
2790                         if (cYAML_create_number(pools_node, "enable",
2791                                                 pool_cfg->pl_routing) ==
2792                         NULL)
2793                                 goto out;
2794
2795                         goto add_buffer_section;
2796                 }
2797
2798                 item = cYAML_create_seq_item(pools_node);
2799                 if (item == NULL)
2800                         goto out;
2801
2802                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2803                     NULL)
2804                         goto out;
2805         }
2806
2807 add_buffer_section:
2808         /* create a buffers entry in the show. This is necessary so that
2809          * if the YAML output is used to configure a node, the buffer
2810          * configuration takes hold */
2811         buffers = cYAML_create_object(root, "buffers");
2812         if (buffers == NULL)
2813                 goto out;
2814
2815         for (i = 0; i < LNET_NRBPOOLS; i++) {
2816                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2817                         goto out;
2818         }
2819
2820         if (show_rc == NULL)
2821                 cYAML_print_tree(root);
2822
2823         if (l_errno != ENOENT) {
2824                 snprintf(err_str,
2825                          sizeof(err_str),
2826                          "\"cannot get routing information: %s\"",
2827                          strerror(l_errno));
2828                 rc = -l_errno;
2829                 goto out;
2830         } else
2831                 rc = LUSTRE_CFG_RC_NO_ERR;
2832
2833         snprintf(err_str, sizeof(err_str), "\"success\"");
2834         rc = LUSTRE_CFG_RC_NO_ERR;
2835
2836 out:
2837         free(buf);
2838         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2839                 cYAML_free_tree(root);
2840         } else if (show_rc != NULL && *show_rc != NULL) {
2841                 struct cYAML *routing_node;
2842                 /* there should exist only one routing block and one
2843                  * buffers block. If there already exists a previous one
2844                  * then don't add another */
2845                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2846                 if (routing_node == NULL) {
2847                         cYAML_insert_sibling((*show_rc)->cy_child,
2848                                                 root->cy_child);
2849                         free(root);
2850                 } else {
2851                         cYAML_free_tree(root);
2852                 }
2853         } else {
2854                 *show_rc = root;
2855         }
2856
2857         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2858
2859         return rc;
2860 }
2861
2862 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2863                           struct cYAML **show_rc, struct cYAML **err_rc,
2864                           bool backup)
2865 {
2866         /*
2867          * TODO: This function is changing in a future patch to accommodate
2868          * PEER_LIST and proper filtering on any nid of the peer
2869          */
2870         struct lnet_ioctl_peer_cfg peer_info;
2871         struct lnet_peer_ni_credit_info *lpni_cri;
2872         struct lnet_ioctl_element_stats *lpni_stats;
2873         struct lnet_ioctl_element_msg_stats *msg_stats;
2874         struct lnet_ioctl_peer_ni_hstats *hstats;
2875         lnet_nid_t *nidp;
2876         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2877         int i, j, k;
2878         int l_errno = 0;
2879         __u32 count;
2880         __u32 size;
2881         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2882                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2883                      *msg_statistics = NULL, *statistics = NULL,
2884                      *yhstats;
2885         char err_str[LNET_MAX_STR_LEN];
2886         struct lnet_process_id *list = NULL;
2887         void *data = NULL;
2888         void *lpni_data;
2889         bool exist = false;
2890
2891         snprintf(err_str, sizeof(err_str),
2892                  "\"out of memory\"");
2893
2894         /* create struct cYAML root object */
2895         root = cYAML_create_object(NULL, NULL);
2896         if (root == NULL)
2897                 goto out;
2898
2899         peer_root = cYAML_create_seq(root, "peer");
2900         if (peer_root == NULL)
2901                 goto out;
2902
2903         count = 1000;
2904         size = count * sizeof(struct lnet_process_id);
2905         list = malloc(size);
2906         if (list == NULL) {
2907                 l_errno = ENOMEM;
2908                 goto out;
2909         }
2910         if (knid != NULL) {
2911                 list[0].nid = libcfs_str2nid(knid);
2912                 count = 1;
2913         } else {
2914                 for (;;) {
2915                         memset(&peer_info, 0, sizeof(peer_info));
2916                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2917                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2918                         peer_info.prcfg_size = size;
2919                         peer_info.prcfg_bulk = list;
2920
2921                         l_errno = 0;
2922                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2923                                      &peer_info);
2924                         count = peer_info.prcfg_count;
2925                         if (rc == 0)
2926                                 break;
2927                         l_errno = errno;
2928                         if (l_errno != E2BIG) {
2929                                 snprintf(err_str,
2930                                         sizeof(err_str),
2931                                         "\"cannot get peer list: %s\"",
2932                                         strerror(l_errno));
2933                                 rc = -l_errno;
2934                                 goto out;
2935                         }
2936                         free(list);
2937                         size = peer_info.prcfg_size;
2938                         list = malloc(size);
2939                         if (list == NULL) {
2940                                 l_errno = ENOMEM;
2941                                 goto out;
2942                         }
2943                 }
2944         }
2945
2946         size = 4096;
2947         data = malloc(size);
2948         if (data == NULL) {
2949                 l_errno = ENOMEM;
2950                 goto out;
2951         }
2952
2953         for (i = 0; i < count; i++) {
2954                 for (;;) {
2955                         memset(&peer_info, 0, sizeof(peer_info));
2956                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2957                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2958                         peer_info.prcfg_prim_nid = list[i].nid;
2959                         peer_info.prcfg_size = size;
2960                         peer_info.prcfg_bulk = data;
2961
2962                         l_errno = 0;
2963                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2964                                      &peer_info);
2965                         if (rc == 0)
2966                                 break;
2967                         l_errno = errno;
2968                         if (l_errno != E2BIG) {
2969                                 snprintf(err_str,
2970                                         sizeof(err_str),
2971                                         "\"cannot get peer information: %s\"",
2972                                         strerror(l_errno));
2973                                 rc = -l_errno;
2974                                 goto out;
2975                         }
2976                         free(data);
2977                         size = peer_info.prcfg_size;
2978                         data = malloc(size);
2979                         if (data == NULL) {
2980                                 l_errno = ENOMEM;
2981                                 goto out;
2982                         }
2983                 }
2984                 exist = true;
2985
2986                 peer = cYAML_create_seq_item(peer_root);
2987                 if (peer == NULL)
2988                         goto out;
2989
2990                 if (first_seq == NULL)
2991                         first_seq = peer;
2992
2993                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2994                 if (cYAML_create_string(peer, "primary nid",
2995                                         libcfs_nid2str(pnid))
2996                     == NULL)
2997                         goto out;
2998                 if (cYAML_create_string(peer, "Multi-Rail",
2999                                         peer_info.prcfg_mr ? "True" : "False")
3000                     == NULL)
3001                         goto out;
3002                 /*
3003                  * print out the state of the peer only if details are
3004                  * requested
3005                  */
3006                 if (detail >= 3) {
3007                         if (!backup &&
3008                             cYAML_create_number(peer, "peer state",
3009                                                 peer_info.prcfg_state)
3010                                 == NULL)
3011                                 goto out;
3012                 }
3013
3014                 tmp = cYAML_create_seq(peer, "peer ni");
3015                 if (tmp == NULL)
3016                         goto out;
3017
3018                 lpni_data = data;
3019                 for (j = 0; j < peer_info.prcfg_count; j++) {
3020                         nidp = lpni_data;
3021                         lpni_cri = (void*)nidp + sizeof(nidp);
3022                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
3023                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
3024                         hstats = (void *)msg_stats + sizeof(*msg_stats);
3025                         lpni_data = (void *)hstats + sizeof(*hstats);
3026
3027                         peer_ni = cYAML_create_seq_item(tmp);
3028                         if (peer_ni == NULL)
3029                                 goto out;
3030
3031                         if (cYAML_create_string(peer_ni, "nid",
3032                                                 libcfs_nid2str(*nidp))
3033                             == NULL)
3034                                 goto out;
3035
3036                         if (backup)
3037                                 continue;
3038
3039                         if (cYAML_create_string(peer_ni, "state",
3040                                                 lpni_cri->cr_aliveness)
3041                             == NULL)
3042                                 goto out;
3043
3044                         if (!detail)
3045                                 continue;
3046
3047                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
3048                                                 lpni_cri->cr_ni_peer_tx_credits)
3049                             == NULL)
3050                                 goto out;
3051
3052                         if (cYAML_create_number(peer_ni, "available_tx_credits",
3053                                                 lpni_cri->cr_peer_tx_credits)
3054                             == NULL)
3055                                 goto out;
3056
3057                         if (cYAML_create_number(peer_ni, "min_tx_credits",
3058                                                 lpni_cri->cr_peer_min_tx_credits)
3059                             == NULL)
3060                                 goto out;
3061
3062                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
3063                                                 lpni_cri->cr_peer_tx_qnob)
3064                             == NULL)
3065                                 goto out;
3066
3067                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
3068                                                 lpni_cri->cr_peer_rtr_credits)
3069                             == NULL)
3070                                 goto out;
3071
3072                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
3073                                                 lpni_cri->cr_peer_min_rtr_credits)
3074                             == NULL)
3075                                 goto out;
3076
3077                         if (cYAML_create_number(peer_ni, "refcount",
3078                                                 lpni_cri->cr_refcount) == NULL)
3079                                 goto out;
3080
3081                         statistics = cYAML_create_object(peer_ni, "statistics");
3082                         if (statistics == NULL)
3083                                 goto out;
3084
3085                         if (cYAML_create_number(statistics, "send_count",
3086                                                 lpni_stats->iel_send_count)
3087                             == NULL)
3088                                 goto out;
3089
3090                         if (cYAML_create_number(statistics, "recv_count",
3091                                                 lpni_stats->iel_recv_count)
3092                             == NULL)
3093                                 goto out;
3094
3095                         if (cYAML_create_number(statistics, "drop_count",
3096                                                 lpni_stats->iel_drop_count)
3097                             == NULL)
3098                                 goto out;
3099
3100                         if (detail < 2)
3101                                 continue;
3102
3103                         for (k = 0; k < 3; k++) {
3104                                 struct lnet_ioctl_comm_count *counts;
3105
3106                                 msg_statistics = cYAML_create_object(peer_ni,
3107                                                  (char *) gmsg_stat_names[k]);
3108                                 if (msg_statistics == NULL)
3109                                         goto out;
3110
3111                                 counts = get_counts(msg_stats, k);
3112                                 if (counts == NULL)
3113                                         goto out;
3114
3115                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
3116                                                                counts))
3117                                         goto out;
3118                         }
3119
3120                         yhstats = cYAML_create_object(peer_ni, "health stats");
3121                         if (!yhstats)
3122                                 goto out;
3123                         if (cYAML_create_number(yhstats, "health value",
3124                                                 hstats->hlpni_health_value)
3125                                                         == NULL)
3126                                 goto out;
3127                         if (cYAML_create_number(yhstats, "dropped",
3128                                                 hstats->hlpni_remote_dropped)
3129                                                         == NULL)
3130                                 goto out;
3131                         if (cYAML_create_number(yhstats, "timeout",
3132                                                 hstats->hlpni_remote_timeout)
3133                                                         == NULL)
3134                                 goto out;
3135                         if (cYAML_create_number(yhstats, "error",
3136                                                 hstats->hlpni_remote_error)
3137                                                         == NULL)
3138                                 goto out;
3139                         if (cYAML_create_number(yhstats, "network timeout",
3140                                                 hstats->hlpni_network_timeout)
3141                                                         == NULL)
3142                                 goto out;
3143                 }
3144         }
3145
3146         /* print output iff show_rc is not provided */
3147         if (show_rc == NULL)
3148                 cYAML_print_tree(root);
3149
3150         snprintf(err_str, sizeof(err_str), "\"success\"");
3151         rc = LUSTRE_CFG_RC_NO_ERR;
3152
3153 out:
3154         free(list);
3155         free(data);
3156         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
3157                 cYAML_free_tree(root);
3158         } else if (show_rc != NULL && *show_rc != NULL) {
3159                 struct cYAML *show_node;
3160                 /* find the peer node, if one doesn't exist then
3161                  * insert one.  Otherwise add to the one there
3162                  */
3163                 show_node = cYAML_get_object_item(*show_rc,
3164                                                   "peer");
3165                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3166                         cYAML_insert_child(show_node, first_seq);
3167                         free(peer_root);
3168                         free(root);
3169                 } else if (show_node == NULL) {
3170                         cYAML_insert_sibling((*show_rc)->cy_child,
3171                                              peer_root);
3172                         free(root);
3173                 } else {
3174                         cYAML_free_tree(root);
3175                 }
3176         } else {
3177                 *show_rc = root;
3178         }
3179
3180         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3181                           err_rc);
3182
3183         return rc;
3184 }
3185
3186 int lustre_lnet_list_peer(int seq_no,
3187                           struct cYAML **show_rc, struct cYAML **err_rc)
3188 {
3189         struct lnet_ioctl_peer_cfg peer_info;
3190         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3191         __u32 count;
3192         __u32 size;
3193         int i = 0;
3194         int l_errno = 0;
3195         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
3196         char err_str[LNET_MAX_STR_LEN];
3197         struct lnet_process_id *list = NULL;
3198
3199         snprintf(err_str, sizeof(err_str),
3200                  "\"out of memory\"");
3201
3202         memset(&peer_info, 0, sizeof(peer_info));
3203
3204         /* create struct cYAML root object */
3205         root = cYAML_create_object(NULL, NULL);
3206         if (root == NULL)
3207                 goto out;
3208
3209         list_root = cYAML_create_seq(root, "peer list");
3210         if (list_root == NULL)
3211                 goto out;
3212
3213         count = 1000;
3214         size = count * sizeof(struct lnet_process_id);
3215         list = malloc(size);
3216         if (list == NULL) {
3217                 l_errno = ENOMEM;
3218                 goto out;
3219         }
3220         for (;;) {
3221                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3222                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3223                 peer_info.prcfg_size = size;
3224                 peer_info.prcfg_bulk = list;
3225
3226                 l_errno = 0;
3227                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
3228                 count = peer_info.prcfg_count;
3229                 if (rc == 0)
3230                         break;
3231                 l_errno = errno;
3232                 if (l_errno != E2BIG) {
3233                         snprintf(err_str,
3234                                 sizeof(err_str),
3235                                 "\"cannot get peer list: %s\"",
3236                                 strerror(l_errno));
3237                         rc = -l_errno;
3238                         goto out;
3239                 }
3240                 free(list);
3241                 size = peer_info.prcfg_size;
3242                 list = malloc(size);
3243                 if (list == NULL) {
3244                         l_errno = ENOMEM;
3245                         goto out;
3246                 }
3247         }
3248
3249         /* count is now the actual number of ids in the list. */
3250         for (i = 0; i < count; i++) {
3251                 if (cYAML_create_string(list_root, "nid",
3252                                         libcfs_nid2str(list[i].nid))
3253                     == NULL)
3254                         goto out;
3255         }
3256
3257         /* print output iff show_rc is not provided */
3258         if (show_rc == NULL)
3259                 cYAML_print_tree(root);
3260
3261         snprintf(err_str, sizeof(err_str), "\"success\"");
3262         rc = LUSTRE_CFG_RC_NO_ERR;
3263
3264 out:
3265         if (list != NULL)
3266                 free(list);
3267         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3268                 cYAML_free_tree(root);
3269         } else if (show_rc != NULL && *show_rc != NULL) {
3270                 struct cYAML *show_node;
3271                 /* find the peer node, if one doesn't exist then
3272                  * insert one.  Otherwise add to the one there
3273                  */
3274                 show_node = cYAML_get_object_item(*show_rc,
3275                                                   "peer");
3276                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3277                         cYAML_insert_child(show_node, first_seq);
3278                         free(list_root);
3279                         free(root);
3280                 } else if (show_node == NULL) {
3281                         cYAML_insert_sibling((*show_rc)->cy_child,
3282                                              list_root);
3283                         free(root);
3284                 } else {
3285                         cYAML_free_tree(root);
3286                 }
3287         } else {
3288                 *show_rc = root;
3289         }
3290
3291         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3292                           err_rc);
3293
3294         return rc;
3295 }
3296
3297 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
3298                           struct cYAML *root)
3299 {
3300         struct cYAML *show_node;
3301
3302         show_node = cYAML_get_object_item(show_rc, "global");
3303         if (show_node != NULL)
3304                 cYAML_insert_sibling(show_node->cy_child,
3305                                      node->cy_child);
3306         else
3307                 cYAML_insert_sibling(show_rc->cy_child,
3308                                      node);
3309         free(root);
3310 }
3311
3312 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
3313                                    char *name, __u32 value,
3314                                    struct cYAML **show_rc,
3315                                    struct cYAML **err_rc, int err)
3316 {
3317         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3318         struct cYAML *root = NULL, *global = NULL;
3319
3320         if (err) {
3321                 rc = err;
3322                 goto out;
3323         }
3324
3325         root = cYAML_create_object(NULL, NULL);
3326         if (root == NULL)
3327                 goto out;
3328
3329         global = cYAML_create_object(root, "global");
3330         if (global == NULL)
3331                 goto out;
3332
3333         if (cYAML_create_number(global, name,
3334                                 value) == NULL)
3335                 goto out;
3336
3337         if (show_rc == NULL)
3338                 cYAML_print_tree(root);
3339
3340         snprintf(err_str, err_len, "\"success\"");
3341
3342         rc = LUSTRE_CFG_RC_NO_ERR;
3343
3344 out:
3345         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3346                 cYAML_free_tree(root);
3347         } else if (show_rc != NULL && *show_rc != NULL) {
3348                 add_to_global(*show_rc, global, root);
3349         } else {
3350                 *show_rc = root;
3351         }
3352
3353         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
3354
3355         return rc;
3356 }
3357
3358 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
3359                                     struct cYAML **show_rc,
3360                                     struct cYAML **err_rc)
3361 {
3362         struct lnet_ioctl_set_value data;
3363         int rc;
3364         int l_errno = 0;
3365         char err_str[LNET_MAX_STR_LEN];
3366
3367         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3368
3369         LIBCFS_IOC_INIT_V2(data, sv_hdr);
3370
3371         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3372         if (rc != 0) {
3373                 l_errno = -errno;
3374                 snprintf(err_str,
3375                          sizeof(err_str),
3376                          "\"cannot get %s: %s\"",
3377                          name, strerror(l_errno));
3378         }
3379
3380         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3381                                        data.sv_value, show_rc, err_rc, l_errno);
3382 }
3383
3384 int lustre_lnet_show_hsensitivity(int seq_no, struct cYAML **show_rc,
3385                                   struct cYAML **err_rc)
3386 {
3387         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3388         char val[LNET_MAX_STR_LEN];
3389         int sen = -1, l_errno = 0;
3390         char err_str[LNET_MAX_STR_LEN];
3391
3392         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3393
3394         rc = read_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
3395                              1, sizeof(val));
3396         if (rc) {
3397                 l_errno = -errno;
3398                 snprintf(err_str, sizeof(err_str),
3399                          "\"cannot get health sensitivity: %d\"", rc);
3400         } else {
3401                 sen = atoi(val);
3402         }
3403
3404         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3405                                        "health_sensitivity", sen, show_rc,
3406                                        err_rc, l_errno);
3407 }
3408
3409 int lustre_lnet_show_transaction_to(int seq_no, struct cYAML **show_rc,
3410                                     struct cYAML **err_rc)
3411 {
3412         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3413         char val[LNET_MAX_STR_LEN];
3414         int tto = -1, l_errno = 0;
3415         char err_str[LNET_MAX_STR_LEN];
3416
3417         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3418
3419         rc = read_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
3420                              1, sizeof(val));
3421         if (rc) {
3422                 l_errno = -errno;
3423                 snprintf(err_str, sizeof(err_str),
3424                          "\"cannot get transaction timeout: %d\"", rc);
3425         } else {
3426                 tto = atoi(val);
3427         }
3428
3429         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3430                                        "transaction_timeout", tto, show_rc,
3431                                        err_rc, l_errno);
3432 }
3433
3434 int lustre_lnet_show_retry_count(int seq_no, struct cYAML **show_rc,
3435                                  struct cYAML **err_rc)
3436 {
3437         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3438         char val[LNET_MAX_STR_LEN];
3439         int retry_count = -1, l_errno = 0;
3440         char err_str[LNET_MAX_STR_LEN];
3441
3442         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3443
3444         rc = read_sysfs_file(modparam_path, "lnet_retry_count", val,
3445                              1, sizeof(val));
3446         if (rc) {
3447                 l_errno = -errno;
3448                 snprintf(err_str, sizeof(err_str),
3449                          "\"cannot get retry count: %d\"", rc);
3450         } else {
3451                 retry_count = atoi(val);
3452         }
3453
3454         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3455                                        "retry_count", retry_count, show_rc,
3456                                        err_rc, l_errno);
3457 }
3458
3459 int show_recovery_queue(enum lnet_health_type type, char *name, int seq_no,
3460                         struct cYAML **show_rc, struct cYAML **err_rc)
3461 {
3462         struct lnet_ioctl_recovery_list nid_list;
3463         struct cYAML *root = NULL, *nids = NULL;
3464         int rc, i;
3465         char err_str[LNET_MAX_STR_LEN];
3466
3467         snprintf(err_str, sizeof(err_str), "failed to print recovery queue\n");
3468
3469         LIBCFS_IOC_INIT_V2(nid_list, rlst_hdr);
3470         nid_list.rlst_type = type;
3471
3472         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_RECOVERY_QUEUE, &nid_list);
3473         if (rc) {
3474                 rc = errno;
3475                 goto out;
3476         }
3477
3478         root = cYAML_create_object(NULL, NULL);
3479         if (root == NULL)
3480                 goto out;
3481
3482         nids = cYAML_create_object(root, name);
3483         if (nids == NULL)
3484                 goto out;
3485
3486         rc = -EINVAL;
3487
3488         for (i = 0; i < nid_list.rlst_num_nids; i++) {
3489                 char nidenum[LNET_MAX_STR_LEN];
3490                 snprintf(nidenum, sizeof(nidenum), "nid-%d", i);
3491                 if (!cYAML_create_string(nids, nidenum,
3492                         libcfs_nid2str(nid_list.rlst_nid_array[i])))
3493                         goto out;
3494         }
3495
3496         snprintf(err_str, sizeof(err_str), "success\n");
3497
3498         rc = 0;
3499
3500 out:
3501         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3502                 cYAML_free_tree(root);
3503         } else if (show_rc != NULL && *show_rc != NULL) {
3504                 struct cYAML *show_node;
3505                 /* find the net node, if one doesn't exist
3506                  * then insert one.  Otherwise add to the one there
3507                  */
3508                 show_node = cYAML_get_object_item(*show_rc, name);
3509                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3510                         cYAML_insert_child(show_node, nids);
3511                         free(nids);
3512                         free(root);
3513                 } else if (show_node == NULL) {
3514                         cYAML_insert_sibling((*show_rc)->cy_child,
3515                                                 nids);
3516                         free(root);
3517                 } else {
3518                         cYAML_free_tree(root);
3519                 }
3520         } else {
3521                 *show_rc = root;
3522         }
3523
3524         cYAML_build_error(rc, seq_no, SHOW_CMD, name, err_str, err_rc);
3525
3526         return rc;
3527 }
3528
3529 int lustre_lnet_show_local_ni_recovq(int seq_no, struct cYAML **show_rc,
3530                                      struct cYAML **err_rc)
3531 {
3532         return show_recovery_queue(LNET_HEALTH_TYPE_LOCAL_NI, "local NI recovery",
3533                                    seq_no, show_rc, err_rc);
3534 }
3535
3536 int lustre_lnet_show_peer_ni_recovq(int seq_no, struct cYAML **show_rc,
3537                                     struct cYAML **err_rc)
3538 {
3539         return show_recovery_queue(LNET_HEALTH_TYPE_PEER_NI, "peer NI recovery",
3540                                    seq_no, show_rc, err_rc);
3541 }
3542
3543 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
3544                               struct cYAML **err_rc)
3545 {
3546         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3547         char val[LNET_MAX_STR_LEN];
3548         int max_intf = -1, l_errno = 0;
3549         char err_str[LNET_MAX_STR_LEN];
3550
3551         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3552
3553         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
3554                              1, sizeof(val));
3555         if (rc) {
3556                 l_errno = -errno;
3557                 snprintf(err_str, sizeof(err_str),
3558                          "\"cannot get max interfaces: %d\"", rc);
3559         } else {
3560                 max_intf = atoi(val);
3561         }
3562
3563         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3564                                        "max_intf", max_intf, show_rc,
3565                                        err_rc, l_errno);
3566 }
3567
3568 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3569                                struct cYAML **err_rc)
3570 {
3571         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3572         char val[LNET_MAX_STR_LEN];
3573         int discovery = -1, l_errno = 0;
3574         char err_str[LNET_MAX_STR_LEN];
3575
3576         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3577
3578         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3579                              1, sizeof(val));
3580         if (rc) {
3581                 l_errno = -errno;
3582                 snprintf(err_str, sizeof(err_str),
3583                          "\"cannot get discovery setting: %d\"", rc);
3584         } else {
3585                 /*
3586                  * The kernel stores a discovery disabled value. User space
3587                  * shows whether discovery is enabled. So the value must be
3588                  * inverted.
3589                  */
3590                 discovery = !atoi(val);
3591         }
3592
3593         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3594                                        "discovery", discovery, show_rc,
3595                                        err_rc, l_errno);
3596 }
3597
3598 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3599                                 struct cYAML **err_rc)
3600 {
3601         return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3602                                         "numa_range", show_rc, err_rc);
3603 }
3604
3605 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3606                            struct cYAML **err_rc)
3607 {
3608         struct lnet_ioctl_lnet_stats data;
3609         int rc;
3610         int l_errno;
3611         char err_str[LNET_MAX_STR_LEN];
3612         struct cYAML *root = NULL, *stats = NULL;
3613
3614         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3615
3616         LIBCFS_IOC_INIT_V2(data, st_hdr);
3617
3618         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3619         if (rc != 0) {
3620                 l_errno = errno;
3621                 snprintf(err_str,
3622                          sizeof(err_str),
3623                          "\"cannot get lnet statistics: %s\"",
3624                          strerror(l_errno));
3625                 rc = -l_errno;
3626                 goto out;
3627         }
3628
3629         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3630
3631         root = cYAML_create_object(NULL, NULL);
3632         if (root == NULL)
3633                 goto out;
3634
3635         stats = cYAML_create_object(root, "statistics");
3636         if (stats == NULL)
3637                 goto out;
3638
3639         if (cYAML_create_number(stats, "msgs_alloc",
3640                                 data.st_cntrs.msgs_alloc) == NULL)
3641                 goto out;
3642
3643         if (cYAML_create_number(stats, "msgs_max",
3644                                 data.st_cntrs.msgs_max) == NULL)
3645                 goto out;
3646
3647         if (cYAML_create_number(stats, "rst_alloc",
3648                                 data.st_cntrs.rst_alloc) == NULL)
3649                 goto out;
3650
3651         if (cYAML_create_number(stats, "errors",
3652                                 data.st_cntrs.errors) == NULL)
3653                 goto out;
3654
3655         if (cYAML_create_number(stats, "send_count",
3656                                 data.st_cntrs.send_count) == NULL)
3657                 goto out;
3658
3659         if (cYAML_create_number(stats, "resend_count",
3660                                 data.st_cntrs.resend_count) == NULL)
3661                 goto out;
3662
3663         if (cYAML_create_number(stats, "response_timeout_count",
3664                                 data.st_cntrs.response_timeout_count) == NULL)
3665                 goto out;
3666
3667         if (cYAML_create_number(stats, "local_interrupt_count",
3668                                 data.st_cntrs.local_interrupt_count) == NULL)
3669                 goto out;
3670
3671         if (cYAML_create_number(stats, "local_dropped_count",
3672                                 data.st_cntrs.local_dropped_count) == NULL)
3673                 goto out;
3674
3675         if (cYAML_create_number(stats, "local_aborted_count",
3676                                 data.st_cntrs.local_aborted_count) == NULL)
3677                 goto out;
3678
3679         if (cYAML_create_number(stats, "local_no_route_count",
3680                                 data.st_cntrs.local_no_route_count) == NULL)
3681                 goto out;
3682
3683         if (cYAML_create_number(stats, "local_timeout_count",
3684                                 data.st_cntrs.local_timeout_count) == NULL)
3685                 goto out;
3686
3687         if (cYAML_create_number(stats, "local_error_count",
3688                                 data.st_cntrs.local_error_count) == NULL)
3689                 goto out;
3690
3691         if (cYAML_create_number(stats, "remote_dropped_count",
3692                                 data.st_cntrs.remote_dropped_count) == NULL)
3693                 goto out;
3694
3695         if (cYAML_create_number(stats, "remote_error_count",
3696                                 data.st_cntrs.remote_error_count) == NULL)
3697                 goto out;
3698
3699         if (cYAML_create_number(stats, "remote_timeout_count",
3700                                 data.st_cntrs.remote_timeout_count) == NULL)
3701                 goto out;
3702
3703         if (cYAML_create_number(stats, "network_timeout_count",
3704                                 data.st_cntrs.network_timeout_count) == NULL)
3705                 goto out;
3706
3707         if (cYAML_create_number(stats, "recv_count",
3708                                 data.st_cntrs.recv_count) == NULL)
3709                 goto out;
3710
3711         if (cYAML_create_number(stats, "route_count",
3712                                 data.st_cntrs.route_count) == NULL)
3713                 goto out;
3714
3715         if (cYAML_create_number(stats, "drop_count",
3716                                 data.st_cntrs.drop_count) == NULL)
3717                 goto out;
3718
3719         if (cYAML_create_number(stats, "send_length",
3720                                 data.st_cntrs.send_length) == NULL)
3721                 goto out;
3722
3723         if (cYAML_create_number(stats, "recv_length",
3724                                 data.st_cntrs.recv_length) == NULL)
3725                 goto out;
3726
3727         if (cYAML_create_number(stats, "route_length",
3728                                 data.st_cntrs.route_length) == NULL)
3729                 goto out;
3730
3731         if (cYAML_create_number(stats, "drop_length",
3732                                 data.st_cntrs.drop_length) == NULL)
3733                 goto out;
3734
3735         if (show_rc == NULL)
3736                 cYAML_print_tree(root);
3737
3738         snprintf(err_str, sizeof(err_str), "\"success\"");
3739         rc = LUSTRE_CFG_RC_NO_ERR;
3740 out:
3741         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3742                 cYAML_free_tree(root);
3743         } else if (show_rc != NULL && *show_rc != NULL) {
3744                 cYAML_insert_sibling((*show_rc)->cy_child,
3745                                         root->cy_child);
3746                 free(root);
3747         } else {
3748                 *show_rc = root;
3749         }
3750
3751         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3752
3753         return rc;
3754 }
3755
3756 typedef int (*cmd_handler_t)(struct cYAML *tree,
3757                              struct cYAML **show_rc,
3758                              struct cYAML **err_rc);
3759
3760 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3761                                     struct cYAML **err_rc)
3762 {
3763         struct cYAML *net, *gw, *hop, *prio, *seq_no;
3764
3765         net = cYAML_get_object_item(tree, "net");
3766         gw = cYAML_get_object_item(tree, "gateway");
3767         hop = cYAML_get_object_item(tree, "hop");
3768         prio = cYAML_get_object_item(tree, "priority");
3769         seq_no = cYAML_get_object_item(tree, "seq_no");
3770
3771         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3772                                         (gw) ? gw->cy_valuestring : NULL,
3773                                         (hop) ? hop->cy_valueint : -1,
3774                                         (prio) ? prio->cy_valueint : -1,
3775                                         (seq_no) ? seq_no->cy_valueint : -1,
3776                                         err_rc);
3777 }
3778
3779 static void yaml_free_string_array(char **array, int num)
3780 {
3781         int i;
3782         char **sub_array = array;
3783
3784         for (i = 0; i < num; i++) {
3785                 if (*sub_array != NULL)
3786                         free(*sub_array);
3787                 sub_array++;
3788         }
3789         if (array)
3790                 free(array);
3791 }
3792
3793 /*
3794  *    interfaces:
3795  *        0: <intf_name>['['<expr>']']
3796  *        1: <intf_name>['['<expr>']']
3797  */
3798 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3799                                struct lnet_dlc_network_descr *nw_descr)
3800 {
3801         struct cYAML *child = NULL;
3802         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3803         struct lnet_dlc_intf_descr *intf_descr, *tmp;
3804
3805         if (intf_tree == NULL || nw_descr == NULL)
3806                 return LUSTRE_CFG_RC_BAD_PARAM;
3807
3808         /* now grab all the interfaces and their cpts */
3809         child = intf_tree->cy_child;
3810         while (child != NULL) {
3811                 if (child->cy_valuestring == NULL) {
3812                         child = child->cy_next;
3813                         continue;
3814                 }
3815
3816                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3817                         goto failed;
3818
3819                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3820                                                 child->cy_valuestring,
3821                                                 strlen(child->cy_valuestring));
3822                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3823                         goto failed;
3824
3825                 intf_num++;
3826                 child = child->cy_next;
3827         }
3828
3829         if (intf_num == 0)
3830                 return LUSTRE_CFG_RC_MISSING_PARAM;
3831
3832         return intf_num;
3833
3834 failed:
3835         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3836                                  intf_on_network) {
3837                 list_del(&intf_descr->intf_on_network);
3838                 free_intf_descr(intf_descr);
3839         }
3840
3841         return rc;
3842 }
3843
3844 static bool
3845 yaml_extract_cmn_tunables(struct cYAML *tree,
3846                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3847                           struct cfs_expr_list **global_cpts)
3848 {
3849         struct cYAML *tun, *item, *smp;
3850         int rc;
3851
3852         tun = cYAML_get_object_item(tree, "tunables");
3853         if (tun != NULL) {
3854                 item = cYAML_get_object_item(tun, "peer_timeout");
3855                 if (item != NULL)
3856                         tunables->lct_peer_timeout = item->cy_valueint;
3857                 item = cYAML_get_object_item(tun, "peer_credits");
3858                 if (item != NULL)
3859                         tunables->lct_peer_tx_credits = item->cy_valueint;
3860                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3861                 if (item != NULL)
3862                         tunables->lct_peer_rtr_credits = item->cy_valueint;
3863                 item = cYAML_get_object_item(tun, "credits");
3864                 if (item != NULL)
3865                         tunables->lct_max_tx_credits = item->cy_valueint;
3866                 smp = cYAML_get_object_item(tun, "CPT");
3867                 if (smp != NULL) {
3868                         rc = cfs_expr_list_parse(smp->cy_valuestring,
3869                                                  strlen(smp->cy_valuestring),
3870                                                  0, UINT_MAX, global_cpts);
3871                         if (rc != 0)
3872                                 *global_cpts = NULL;
3873                 }
3874
3875                 return true;
3876         }
3877
3878         return false;
3879 }
3880
3881 static bool
3882 yaml_extract_tunables(struct cYAML *tree,
3883                       struct lnet_ioctl_config_lnd_tunables *tunables,
3884                       struct cfs_expr_list **global_cpts,
3885                       __u32 net_type)
3886 {
3887         bool rc;
3888
3889         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3890                                        global_cpts);
3891
3892         if (!rc)
3893                 return rc;
3894
3895         lustre_yaml_extract_lnd_tunables(tree, net_type,
3896                                          &tunables->lt_tun);
3897
3898         return rc;
3899 }
3900
3901 /*
3902  * net:
3903  *    - net type: <net>[<NUM>]
3904   *      local NI(s):
3905  *        - nid: <ip>@<net>[<NUM>]
3906  *          status: up
3907  *          interfaces:
3908  *               0: <intf_name>['['<expr>']']
3909  *               1: <intf_name>['['<expr>']']
3910  *        tunables:
3911  *               peer_timeout: <NUM>
3912  *               peer_credits: <NUM>
3913  *               peer_buffer_credits: <NUM>
3914  *               credits: <NUM>
3915 *         lnd tunables:
3916  *               peercredits_hiw: <NUM>
3917  *               map_on_demand: <NUM>
3918  *               concurrent_sends: <NUM>
3919  *               fmr_pool_size: <NUM>
3920  *               fmr_flush_trigger: <NUM>
3921  *               fmr_cache: <NUM>
3922  *
3923  * At least one interface is required. If no interfaces are provided the
3924  * network interface can not be configured.
3925  */
3926 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3927                                  struct cYAML **err_rc)
3928 {
3929         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3930                      *item = NULL;
3931         int num_entries = 0, rc;
3932         struct lnet_dlc_network_descr nw_descr;
3933         struct cfs_expr_list *global_cpts = NULL;
3934         struct lnet_ioctl_config_lnd_tunables tunables;
3935         bool found = false;
3936
3937         memset(&tunables, 0, sizeof(tunables));
3938
3939         INIT_LIST_HEAD(&nw_descr.network_on_rule);
3940         INIT_LIST_HEAD(&nw_descr.nw_intflist);
3941
3942         ip2net = cYAML_get_object_item(tree, "ip2net");
3943         net = cYAML_get_object_item(tree, "net type");
3944         if (net)
3945                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3946         else
3947                 nw_descr.nw_id = LOLND;
3948
3949         /*
3950          * if neither net nor ip2nets are present, then we can not
3951          * configure the network.
3952          */
3953         if (!net && !ip2net)
3954                 return LUSTRE_CFG_RC_MISSING_PARAM;
3955
3956         local_nis = cYAML_get_object_item(tree, "local NI(s)");
3957         if (local_nis == NULL)
3958                 return LUSTRE_CFG_RC_MISSING_PARAM;
3959
3960         if (!cYAML_is_sequence(local_nis))
3961                 return LUSTRE_CFG_RC_BAD_PARAM;
3962
3963         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3964                 intf = cYAML_get_object_item(item, "interfaces");
3965                 if (intf == NULL)
3966                         continue;
3967                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3968                 if (num_entries <= 0) {
3969                         cYAML_build_error(num_entries, -1, "ni", "add",
3970                                         "bad interface list",
3971                                         err_rc);
3972                         return LUSTRE_CFG_RC_BAD_PARAM;
3973                 }
3974         }
3975
3976         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3977                                       LNET_NETTYP(nw_descr.nw_id));
3978         seq_no = cYAML_get_object_item(tree, "seq_no");
3979
3980         rc = lustre_lnet_config_ni(&nw_descr,
3981                                    global_cpts,
3982                                    (ip2net) ? ip2net->cy_valuestring : NULL,
3983                                    (found) ? &tunables: NULL,
3984                                    (seq_no) ? seq_no->cy_valueint : -1,
3985                                    err_rc);
3986
3987         if (global_cpts != NULL)
3988                 cfs_expr_list_free(global_cpts);
3989
3990         return rc;
3991 }
3992
3993 /*
3994  * ip2nets:
3995  *  - net-spec: <tcp|o2ib|gni>[NUM]
3996  *    interfaces:
3997  *        0: <intf name>['['<expr>']']
3998  *        1: <intf name>['['<expr>']']
3999  *    ip-range:
4000  *        0: <expr.expr.expr.expr>
4001  *        1: <expr.expr.expr.expr>
4002  */
4003 static int handle_yaml_config_ip2nets(struct cYAML *tree,
4004                                       struct cYAML **show_rc,
4005                                       struct cYAML **err_rc)
4006 {
4007         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
4008                      *seq_no = NULL;
4009         struct lustre_lnet_ip2nets ip2nets;
4010         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
4011                                           *tmp = NULL;
4012         int rc = LUSTRE_CFG_RC_NO_ERR;
4013         struct cfs_expr_list *global_cpts = NULL;
4014         struct cfs_expr_list *el, *el_tmp;
4015         struct lnet_ioctl_config_lnd_tunables tunables;
4016         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
4017         bool found = false;
4018
4019         memset(&tunables, 0, sizeof(tunables));
4020
4021         /* initialize all lists */
4022         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
4023         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
4024         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
4025
4026         net = cYAML_get_object_item(tree, "net-spec");
4027         if (net == NULL)
4028                 return LUSTRE_CFG_RC_BAD_PARAM;
4029
4030         if (net != NULL && net->cy_valuestring == NULL)
4031                 return LUSTRE_CFG_RC_BAD_PARAM;
4032
4033         /* assign the network id */
4034         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
4035         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
4036                 return LUSTRE_CFG_RC_BAD_PARAM;
4037
4038         seq_no = cYAML_get_object_item(tree, "seq_no");
4039
4040         intf = cYAML_get_object_item(tree, "interfaces");
4041         if (intf != NULL) {
4042                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
4043                 if (rc <= 0)
4044                         return LUSTRE_CFG_RC_BAD_PARAM;
4045         }
4046
4047         ip_range = cYAML_get_object_item(tree, "ip-range");
4048         if (ip_range != NULL) {
4049                 item = ip_range->cy_child;
4050                 while (item != NULL) {
4051                         if (item->cy_valuestring == NULL) {
4052                                 item = item->cy_next;
4053                                 continue;
4054                         }
4055
4056                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
4057                                                       item->cy_valuestring);
4058
4059                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4060                                 goto out;
4061
4062                         item = item->cy_next;
4063                 }
4064         }
4065
4066         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
4067                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
4068
4069         rc = lustre_lnet_config_ip2nets(&ip2nets,
4070                         (found) ? &tunables : NULL,
4071                         global_cpts,
4072                         (seq_no) ? seq_no->cy_valueint : -1,
4073                         err_rc);
4074
4075         /*
4076          * don't stop because there was no match. Continue processing the
4077          * rest of the rules. If non-match then nothing is configured
4078          */
4079         if (rc == LUSTRE_CFG_RC_NO_MATCH)
4080                 rc = LUSTRE_CFG_RC_NO_ERR;
4081 out:
4082         list_for_each_entry_safe(intf_descr, intf_tmp,
4083                                  &ip2nets.ip2nets_net.nw_intflist,
4084                                  intf_on_network) {
4085                 list_del(&intf_descr->intf_on_network);
4086                 free_intf_descr(intf_descr);
4087         }
4088
4089         list_for_each_entry_safe(ip_range_descr, tmp,
4090                                  &ip2nets.ip2nets_ip_ranges,
4091                                  ipr_entry) {
4092                 list_del(&ip_range_descr->ipr_entry);
4093                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
4094                                          el_link) {
4095                         list_del(&el->el_link);
4096                         cfs_expr_list_free(el);
4097                 }
4098                 free(ip_range_descr);
4099         }
4100
4101         return rc;
4102 }
4103
4104 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
4105                               struct cYAML **err_rc)
4106 {
4107         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
4108                      *local_nis = NULL;
4109         int num_entries, rc;
4110         struct lnet_dlc_network_descr nw_descr;
4111
4112         INIT_LIST_HEAD(&nw_descr.network_on_rule);
4113         INIT_LIST_HEAD(&nw_descr.nw_intflist);
4114
4115         net = cYAML_get_object_item(tree, "net type");
4116         if (net != NULL)
4117                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
4118
4119         local_nis = cYAML_get_object_item(tree, "local NI(s)");
4120         if (local_nis == NULL)
4121                 return LUSTRE_CFG_RC_MISSING_PARAM;
4122
4123         if (!cYAML_is_sequence(local_nis))
4124                 return LUSTRE_CFG_RC_BAD_PARAM;
4125
4126         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
4127                 intf = cYAML_get_object_item(item, "interfaces");
4128                 if (intf == NULL)
4129                         continue;
4130                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
4131                 if (num_entries <= 0) {
4132                         cYAML_build_error(num_entries, -1, "ni", "add",
4133                                         "bad interface list",
4134                                         err_rc);
4135                         return LUSTRE_CFG_RC_BAD_PARAM;
4136                 }
4137         }
4138
4139         seq_no = cYAML_get_object_item(tree, "seq_no");
4140
4141         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
4142                                 (seq_no) ? seq_no->cy_valueint : -1,
4143                                 err_rc);
4144
4145         return rc;
4146 }
4147
4148 static int yaml_copy_peer_nids(struct cYAML *nids_entry, char ***nidsppp,
4149                                char *prim_nid, bool del)
4150 {
4151         struct cYAML *child = NULL, *entry = NULL;
4152         char **nids = NULL;
4153         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
4154
4155         if (cYAML_is_sequence(nids_entry)) {
4156                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
4157                         entry = cYAML_get_object_item(child, "nid");
4158                         /* don't count an empty entry */
4159                         if (!entry || !entry->cy_valuestring)
4160                                 continue;
4161
4162                         if (prim_nid &&
4163                             (strcmp(entry->cy_valuestring, prim_nid)
4164                                         == 0) && del) {
4165                                 /*
4166                                  * primary nid is present in the list of
4167                                  * nids so that means we want to delete
4168                                  * the entire peer, so no need to go
4169                                  * further. Just delete the entire peer.
4170                                  */
4171                                 return 0;
4172                         }
4173
4174                         num++;
4175                 }
4176         }
4177
4178         if (num == 0)
4179                 return LUSTRE_CFG_RC_MISSING_PARAM;
4180
4181         nids = calloc(sizeof(*nids), num);
4182         if (!nids)
4183                 return LUSTRE_CFG_RC_OUT_OF_MEM;
4184
4185         /* now grab all the nids */
4186         num = 0;
4187         child = NULL;
4188         while (cYAML_get_next_seq_item(nids_entry, &child)) {
4189                 entry = cYAML_get_object_item(child, "nid");
4190                 if (!entry || !entry->cy_valuestring)
4191                         continue;
4192
4193                 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
4194                 if (!nids[num]) {
4195                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
4196                         goto failed;
4197                 }
4198                 strncpy(nids[num], entry->cy_valuestring,
4199                         strlen(entry->cy_valuestring));
4200                 num++;
4201         }
4202         rc = num;
4203
4204         *nidsppp = nids;
4205         return rc;
4206
4207 failed:
4208         if (nids != NULL)
4209                 yaml_free_string_array(nids, num);
4210         *nidsppp = NULL;
4211         return rc;
4212 }
4213
4214 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
4215                                    struct cYAML **err_rc)
4216 {
4217         char **nids = NULL;
4218         int num, rc;
4219         struct cYAML *seq_no, *prim_nid, *non_mr, *ip2nets, *peer_nis;
4220         char err_str[LNET_MAX_STR_LEN];
4221
4222         seq_no = cYAML_get_object_item(tree, "seq_no");
4223         prim_nid = cYAML_get_object_item(tree, "primary nid");
4224         non_mr = cYAML_get_object_item(tree, "non_mr");
4225         ip2nets = cYAML_get_object_item(tree, "ip2nets");
4226         peer_nis = cYAML_get_object_item(tree, "peer ni");
4227
4228         if (ip2nets && (prim_nid || peer_nis)) {
4229                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4230                 snprintf(err_str, sizeof(err_str),
4231                          "ip2nets can not be specified along side prim_nid"
4232                          " or peer ni fields");
4233                 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
4234                                   ADD_CMD, "peer", err_str, err_rc);
4235                 return rc;
4236         }
4237
4238         num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis, &nids,
4239                                   (prim_nid) ? prim_nid->cy_valuestring : NULL,
4240                                    false);
4241
4242         if (num < 0) {
4243                 snprintf(err_str, sizeof(err_str),
4244                          "error copying nids from YAML block");
4245                 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
4246                                   ADD_CMD, "peer", err_str, err_rc);
4247                 return num;
4248         }
4249
4250         rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
4251                                          nids, num,
4252                                          (non_mr) ? false : true,
4253                                          (ip2nets) ? true : false,
4254                                          (seq_no) ? seq_no->cy_valueint : -1,
4255                                          err_rc);
4256
4257         yaml_free_string_array(nids, num);
4258         return rc;
4259 }
4260
4261 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
4262                                 struct cYAML **err_rc)
4263 {
4264         char **nids = NULL;
4265         int num, rc;
4266         struct cYAML *seq_no, *prim_nid, *ip2nets, *peer_nis;
4267         char err_str[LNET_MAX_STR_LEN];
4268
4269         seq_no = cYAML_get_object_item(tree, "seq_no");
4270         prim_nid = cYAML_get_object_item(tree, "primary nid");
4271         ip2nets = cYAML_get_object_item(tree, "ip2nets");
4272         peer_nis = cYAML_get_object_item(tree, "peer ni");
4273
4274         if (ip2nets && (prim_nid || peer_nis)) {
4275                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4276                 snprintf(err_str, sizeof(err_str),
4277                          "ip2nets can not be specified along side prim_nid"
4278                          " or peer ni fields");
4279                 cYAML_build_error(rc, (seq_no) ? seq_no->cy_valueint : -1,
4280                                   DEL_CMD, "peer", err_str, err_rc);
4281                 return rc;
4282         }
4283
4284         num = yaml_copy_peer_nids((ip2nets) ? ip2nets : peer_nis , &nids,
4285                                   (prim_nid) ? prim_nid->cy_valuestring : NULL,
4286                                   true);
4287         if (num < 0) {
4288                 snprintf(err_str, sizeof(err_str),
4289                          "error copying nids from YAML block");
4290                 cYAML_build_error(num, (seq_no) ? seq_no->cy_valueint : -1,
4291                                   ADD_CMD, "peer", err_str, err_rc);
4292                 return num;
4293         }
4294
4295         rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
4296                                       nids, num, (ip2nets) ? true : false,
4297                                       (seq_no) ? seq_no->cy_valueint : -1,
4298                                       err_rc);
4299
4300         yaml_free_string_array(nids, num);
4301         return rc;
4302 }
4303
4304 static int handle_yaml_config_buffers(struct cYAML *tree,
4305                                       struct cYAML **show_rc,
4306                                       struct cYAML **err_rc)
4307 {
4308         int rc;
4309         struct cYAML *tiny, *small, *large, *seq_no;
4310
4311         tiny = cYAML_get_object_item(tree, "tiny");
4312         small = cYAML_get_object_item(tree, "small");
4313         large = cYAML_get_object_item(tree, "large");
4314         seq_no = cYAML_get_object_item(tree, "seq_no");
4315
4316         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
4317                                         (small) ? small->cy_valueint : -1,
4318                                         (large) ? large->cy_valueint : -1,
4319                                         (seq_no) ? seq_no->cy_valueint : -1,
4320                                         err_rc);
4321
4322         return rc;
4323 }
4324
4325 static int handle_yaml_config_routing(struct cYAML *tree,
4326                                       struct cYAML **show_rc,
4327                                       struct cYAML **err_rc)
4328 {
4329         int rc = LUSTRE_CFG_RC_NO_ERR;
4330         struct cYAML *seq_no, *enable;
4331
4332         seq_no = cYAML_get_object_item(tree, "seq_no");
4333         enable = cYAML_get_object_item(tree, "enable");
4334
4335         if (enable) {
4336                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
4337                                                 (seq_no) ?
4338                                                     seq_no->cy_valueint : -1,
4339                                                 err_rc);
4340         }
4341
4342         return rc;
4343 }
4344
4345 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
4346                                  struct cYAML **err_rc)
4347 {
4348         struct cYAML *net;
4349         struct cYAML *gw;
4350         struct cYAML *seq_no;
4351
4352         net = cYAML_get_object_item(tree, "net");
4353         gw = cYAML_get_object_item(tree, "gateway");
4354         seq_no = cYAML_get_object_item(tree, "seq_no");
4355
4356         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
4357                                      (gw) ? gw->cy_valuestring : NULL,
4358                                      (seq_no) ? seq_no->cy_valueint : -1,
4359                                      err_rc);
4360 }
4361
4362 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
4363                                    struct cYAML **err_rc)
4364 {
4365         struct cYAML *seq_no;
4366
4367         seq_no = cYAML_get_object_item(tree, "seq_no");
4368
4369         return lustre_lnet_enable_routing(0, (seq_no) ?
4370                                                 seq_no->cy_valueint : -1,
4371                                         err_rc);
4372 }
4373
4374 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
4375                                   struct cYAML **err_rc)
4376 {
4377         struct cYAML *net;
4378         struct cYAML *gw;
4379         struct cYAML *hop;
4380         struct cYAML *prio;
4381         struct cYAML *detail;
4382         struct cYAML *seq_no;
4383
4384         net = cYAML_get_object_item(tree, "net");
4385         gw = cYAML_get_object_item(tree, "gateway");
4386         hop = cYAML_get_object_item(tree, "hop");
4387         prio = cYAML_get_object_item(tree, "priority");
4388         detail = cYAML_get_object_item(tree, "detail");
4389         seq_no = cYAML_get_object_item(tree, "seq_no");
4390
4391         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
4392                                       (gw) ? gw->cy_valuestring : NULL,
4393                                       (hop) ? hop->cy_valueint : -1,
4394                                       (prio) ? prio->cy_valueint : -1,
4395                                       (detail) ? detail->cy_valueint : 0,
4396                                       (seq_no) ? seq_no->cy_valueint : -1,
4397                                       show_rc, err_rc, false);
4398 }
4399
4400 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
4401                                 struct cYAML **err_rc)
4402 {
4403         struct cYAML *net, *detail, *seq_no;
4404
4405         net = cYAML_get_object_item(tree, "net");
4406         detail = cYAML_get_object_item(tree, "detail");
4407         seq_no = cYAML_get_object_item(tree, "seq_no");
4408
4409         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
4410                                     (detail) ? detail->cy_valueint : 0,
4411                                     (seq_no) ? seq_no->cy_valueint : -1,
4412                                     show_rc, err_rc, false);
4413 }
4414
4415 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
4416                                     struct cYAML **err_rc)
4417 {
4418         struct cYAML *seq_no;
4419
4420         seq_no = cYAML_get_object_item(tree, "seq_no");
4421
4422         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
4423                                         show_rc, err_rc, false);
4424 }
4425
4426 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
4427                                   struct cYAML **err_rc)
4428 {
4429         struct cYAML *seq_no, *nid, *detail;
4430
4431         seq_no = cYAML_get_object_item(tree, "seq_no");
4432         detail = cYAML_get_object_item(tree, "detail");
4433         nid = cYAML_get_object_item(tree, "nid");
4434
4435         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
4436                                      (detail) ? detail->cy_valueint : 0,
4437                                      (seq_no) ? seq_no->cy_valueint : -1,
4438                                      show_rc, err_rc, false);
4439 }
4440
4441 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
4442                                   struct cYAML **err_rc)
4443 {
4444         struct cYAML *seq_no;
4445
4446         seq_no = cYAML_get_object_item(tree, "seq_no");
4447
4448         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
4449                                       show_rc, err_rc);
4450 }
4451
4452 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
4453                                   struct cYAML **err_rc)
4454 {
4455         struct cYAML *seq_no, *range;
4456
4457         seq_no = cYAML_get_object_item(tree, "seq_no");
4458         range = cYAML_get_object_item(tree, "range");
4459
4460         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
4461                                              seq_no ? seq_no->cy_valueint : -1,
4462                                              err_rc);
4463 }
4464
4465 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
4466                                struct cYAML **err_rc)
4467 {
4468         struct cYAML *seq_no;
4469
4470         seq_no = cYAML_get_object_item(tree, "seq_no");
4471
4472         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
4473                                              err_rc);
4474 }
4475
4476 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
4477                                 struct cYAML **err_rc)
4478 {
4479         struct cYAML *seq_no;
4480
4481         seq_no = cYAML_get_object_item(tree, "seq_no");
4482
4483         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
4484                                            show_rc, err_rc);
4485 }
4486
4487 static int handle_yaml_config_global_settings(struct cYAML *tree,
4488                                               struct cYAML **show_rc,
4489                                               struct cYAML **err_rc)
4490 {
4491         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4492                      *sen;
4493         int rc = 0;
4494
4495         seq_no = cYAML_get_object_item(tree, "seq_no");
4496         max_intf = cYAML_get_object_item(tree, "max_intf");
4497         if (max_intf)
4498                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
4499                                                  seq_no ? seq_no->cy_valueint
4500                                                         : -1,
4501                                                  err_rc);
4502
4503         numa = cYAML_get_object_item(tree, "numa_range");
4504         if (numa)
4505                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
4506                                                    seq_no ? seq_no->cy_valueint
4507                                                         : -1,
4508                                                    err_rc);
4509
4510         discovery = cYAML_get_object_item(tree, "discovery");
4511         if (discovery)
4512                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
4513                                                   seq_no ? seq_no->cy_valueint
4514                                                         : -1,
4515                                                   err_rc);
4516
4517         retry = cYAML_get_object_item(tree, "retry_count");
4518         if (retry)
4519                 rc = lustre_lnet_config_retry_count(retry->cy_valueint,
4520                                                     seq_no ? seq_no->cy_valueint
4521                                                         : -1,
4522                                                     err_rc);
4523
4524         tto = cYAML_get_object_item(tree, "transaction_timeout");
4525         if (tto)
4526                 rc = lustre_lnet_config_transaction_to(tto->cy_valueint,
4527                                                        seq_no ? seq_no->cy_valueint
4528                                                                 : -1,
4529                                                        err_rc);
4530
4531         sen = cYAML_get_object_item(tree, "health_sensitivity");
4532         if (sen)
4533                 rc = lustre_lnet_config_hsensitivity(sen->cy_valueint,
4534                                                      seq_no ? seq_no->cy_valueint
4535                                                         : -1,
4536                                                      err_rc);
4537
4538         return rc;
4539 }
4540
4541 static int handle_yaml_del_global_settings(struct cYAML *tree,
4542                                            struct cYAML **show_rc,
4543                                            struct cYAML **err_rc)
4544 {
4545         struct cYAML *max_intf, *numa, *discovery, *seq_no;
4546         int rc = 0;
4547
4548         seq_no = cYAML_get_object_item(tree, "seq_no");
4549         max_intf = cYAML_get_object_item(tree, "max_intf");
4550         if (max_intf)
4551                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
4552                                                  seq_no ? seq_no->cy_valueint
4553                                                         : -1,
4554                                                  err_rc);
4555
4556         numa = cYAML_get_object_item(tree, "numa_range");
4557         if (numa)
4558                 rc = lustre_lnet_config_numa_range(0,
4559                                                    seq_no ? seq_no->cy_valueint
4560                                                         : -1,
4561                                                    err_rc);
4562
4563         /* peer discovery is enabled by default */
4564         discovery = cYAML_get_object_item(tree, "discovery");
4565         if (discovery)
4566                 rc = lustre_lnet_config_discovery(1,
4567                                                   seq_no ? seq_no->cy_valueint
4568                                                         : -1,
4569                                                   err_rc);
4570
4571         return rc;
4572 }
4573
4574 static int handle_yaml_show_global_settings(struct cYAML *tree,
4575                                             struct cYAML **show_rc,
4576                                             struct cYAML **err_rc)
4577 {
4578         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4579                      *sen;
4580         int rc = 0;
4581
4582         seq_no = cYAML_get_object_item(tree, "seq_no");
4583         max_intf = cYAML_get_object_item(tree, "max_intf");
4584         if (max_intf)
4585                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
4586                                                         : -1,
4587                                                 show_rc, err_rc);
4588
4589         numa = cYAML_get_object_item(tree, "numa_range");
4590         if (numa)
4591                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
4592                                                         : -1,
4593                                                  show_rc, err_rc);
4594
4595         discovery = cYAML_get_object_item(tree, "discovery");
4596         if (discovery)
4597                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
4598                                                         : -1,
4599                                                 show_rc, err_rc);
4600
4601         retry = cYAML_get_object_item(tree, "retry_count");
4602         if (retry)
4603                 rc = lustre_lnet_show_retry_count(seq_no ? seq_no->cy_valueint
4604                                                         : -1,
4605                                                   show_rc, err_rc);
4606
4607         tto = cYAML_get_object_item(tree, "transaction_timeout");
4608         if (tto)
4609                 rc = lustre_lnet_show_transaction_to(seq_no ? seq_no->cy_valueint
4610                                                         : -1,
4611                                                      show_rc, err_rc);
4612
4613         sen = cYAML_get_object_item(tree, "health_sensitivity");
4614         if (sen)
4615                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4616                                                         : -1,
4617                                                      show_rc, err_rc);
4618
4619         return rc;
4620 }
4621
4622 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
4623                             struct cYAML **err_rc)
4624 {
4625         struct cYAML *seq_no, *nid, *timeout;
4626
4627         seq_no = cYAML_get_object_item(tree, "seq_no");
4628         nid = cYAML_get_object_item(tree, "primary nid");
4629         timeout = cYAML_get_object_item(tree, "timeout");
4630
4631         return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
4632                                     (timeout) ? timeout->cy_valueint : 1000,
4633                                     (seq_no) ? seq_no->cy_valueint : -1,
4634                                     show_rc, err_rc);
4635 }
4636
4637 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
4638                                 struct cYAML **err_rc)
4639 {
4640         struct cYAML *seq_no, *nid, *force;
4641
4642         seq_no = cYAML_get_object_item(tree, "seq_no");
4643         nid = cYAML_get_object_item(tree, "primary nid");
4644         force = cYAML_get_object_item(tree, "force");
4645
4646         return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
4647                                         (force) ? force->cy_valueint : 0,
4648                                         (seq_no) ? seq_no->cy_valueint : -1,
4649                                         show_rc, err_rc);
4650 }
4651
4652 static int handle_yaml_no_op()
4653 {
4654         return LUSTRE_CFG_RC_NO_ERR;
4655 }
4656
4657 struct lookup_cmd_hdlr_tbl {
4658         char *name;
4659         cmd_handler_t cb;
4660 };
4661
4662 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
4663         { .name = "route",      .cb = handle_yaml_config_route },
4664         { .name = "net",        .cb = handle_yaml_config_ni },
4665         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
4666         { .name = "peer",       .cb = handle_yaml_config_peer },
4667         { .name = "routing",    .cb = handle_yaml_config_routing },
4668         { .name = "buffers",    .cb = handle_yaml_config_buffers },
4669         { .name = "statistics", .cb = handle_yaml_no_op },
4670         { .name = "global",     .cb = handle_yaml_config_global_settings},
4671         { .name = "numa",       .cb = handle_yaml_config_numa },
4672         { .name = "ping",       .cb = handle_yaml_no_op },
4673         { .name = "discover",   .cb = handle_yaml_no_op },
4674         { .name = NULL } };
4675
4676 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
4677         { .name = "route",      .cb = handle_yaml_del_route },
4678         { .name = "net",        .cb = handle_yaml_del_ni },
4679         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4680         { .name = "peer",       .cb = handle_yaml_del_peer },
4681         { .name = "routing",    .cb = handle_yaml_del_routing },
4682         { .name = "buffers",    .cb = handle_yaml_no_op },
4683         { .name = "statistics", .cb = handle_yaml_no_op },
4684         { .name = "global",     .cb = handle_yaml_del_global_settings},
4685         { .name = "numa",       .cb = handle_yaml_del_numa },
4686         { .name = "ping",       .cb = handle_yaml_no_op },
4687         { .name = "discover",   .cb = handle_yaml_no_op },
4688         { .name = NULL } };
4689
4690 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
4691         { .name = "route",      .cb = handle_yaml_show_route },
4692         { .name = "net",        .cb = handle_yaml_show_net },
4693         { .name = "peer",       .cb = handle_yaml_show_peers },
4694         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4695         { .name = "routing",    .cb = handle_yaml_show_routing },
4696         { .name = "buffers",    .cb = handle_yaml_show_routing },
4697         { .name = "statistics", .cb = handle_yaml_show_stats },
4698         { .name = "global",     .cb = handle_yaml_show_global_settings},
4699         { .name = "numa",       .cb = handle_yaml_show_numa },
4700         { .name = "ping",       .cb = handle_yaml_no_op },
4701         { .name = "discover",   .cb = handle_yaml_no_op },
4702         { .name = NULL } };
4703
4704 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4705         { .name = "route",      .cb = handle_yaml_no_op },
4706         { .name = "net",        .cb = handle_yaml_no_op },
4707         { .name = "peer",       .cb = handle_yaml_no_op },
4708         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4709         { .name = "routing",    .cb = handle_yaml_no_op },
4710         { .name = "buffers",    .cb = handle_yaml_no_op },
4711         { .name = "statistics", .cb = handle_yaml_no_op },
4712         { .name = "global",     .cb = handle_yaml_no_op },
4713         { .name = "numa",       .cb = handle_yaml_no_op },
4714         { .name = "ping",       .cb = handle_yaml_ping },
4715         { .name = "discover",   .cb = handle_yaml_discover },
4716         { .name = NULL } };
4717
4718 static cmd_handler_t lookup_fn(char *key,
4719                                struct lookup_cmd_hdlr_tbl *tbl)
4720 {
4721         int i;
4722         if (key == NULL)
4723                 return NULL;
4724
4725         for (i = 0; tbl[i].name != NULL; i++) {
4726                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4727                         return tbl[i].cb;
4728         }
4729
4730         return NULL;
4731 }
4732
4733 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4734                                  struct cYAML **show_rc, struct cYAML **err_rc)
4735 {
4736         struct cYAML *tree, *item = NULL, *head, *child;
4737         cmd_handler_t cb;
4738         char err_str[LNET_MAX_STR_LEN];
4739         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4740
4741         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4742         if (tree == NULL)
4743                 return LUSTRE_CFG_RC_BAD_PARAM;
4744
4745         child = tree->cy_child;
4746         while (child != NULL) {
4747                 cb = lookup_fn(child->cy_string, table);
4748                 if (cb == NULL) {
4749                         snprintf(err_str, sizeof(err_str),
4750                                 "\"call back for '%s' not found\"",
4751                                 child->cy_string);
4752                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4753                                         "yaml", "helper", err_str, err_rc);
4754                         goto out;
4755                 }
4756
4757                 if (cYAML_is_sequence(child)) {
4758                         while ((head = cYAML_get_next_seq_item(child, &item))
4759                                != NULL) {
4760                                 rc = cb(head, show_rc, err_rc);
4761                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
4762                                         return_rc = rc;
4763                         }
4764                 } else {
4765                         rc = cb(child, show_rc, err_rc);
4766                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4767                                 return_rc = rc;
4768                 }
4769                 item = NULL;
4770                 child = child->cy_next;
4771         }
4772
4773 out:
4774         cYAML_free_tree(tree);
4775
4776         return return_rc;
4777 }
4778
4779 int lustre_yaml_config(char *f, struct cYAML **err_rc)
4780 {
4781         return lustre_yaml_cb_helper(f, lookup_config_tbl,
4782                                      NULL, err_rc);
4783 }
4784
4785 int lustre_yaml_del(char *f, struct cYAML **err_rc)
4786 {
4787         return lustre_yaml_cb_helper(f, lookup_del_tbl,
4788                                      NULL, err_rc);
4789 }
4790
4791 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4792 {
4793         return lustre_yaml_cb_helper(f, lookup_show_tbl,
4794                                      show_rc, err_rc);
4795 }
4796
4797 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4798 {
4799         return lustre_yaml_cb_helper(f, lookup_exec_tbl,
4800                                      show_rc, err_rc);
4801 }