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