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