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