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