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