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