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