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