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