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