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