Whamcloud - gitweb
1c253c5191e7a07e82310232285b327f27f3ee7f
[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[0], 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                           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         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1674                                    err_str, sizeof(err_str));
1675         if (rc != 0) {
1676                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1677                 goto out;
1678         }
1679
1680         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1681                                          tunables, global_cpts, nids,
1682                                          err_str);
1683
1684 out:
1685         if (nw_descr != NULL) {
1686                 list_for_each_entry_safe(intf_descr, tmp,
1687                                          &nw_descr->nw_intflist,
1688                                          intf_on_network) {
1689                         list_del(&intf_descr->intf_on_network);
1690                         free_intf_descr(intf_descr);
1691                 }
1692         }
1693
1694         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1695
1696         if (nids)
1697                 free(nids);
1698
1699         if (data)
1700                 free(data);
1701
1702         return rc;
1703 }
1704
1705 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1706                        int seq_no, struct cYAML **err_rc)
1707 {
1708         struct lnet_ioctl_config_ni data;
1709         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1710         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1711         lnet_nid_t *nids = NULL;
1712         __u32 nnids = 0;
1713         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1714
1715         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1716                 snprintf(err_str,
1717                          sizeof(err_str),
1718                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1719                          (nw_descr == NULL) ? "network , interface" :
1720                          (nw_descr->nw_id == 0) ? "network" : "interface");
1721                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1722                 goto out;
1723         }
1724
1725         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1726                 return LUSTRE_CFG_RC_NO_ERR;
1727
1728         if (nw_descr->nw_id == LNET_NET_ANY) {
1729                 snprintf(err_str,
1730                          sizeof(err_str),
1731                          "\"cannot parse net '%s'\"",
1732                          libcfs_net2str(nw_descr->nw_id));
1733                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1734                 goto out;
1735         }
1736
1737         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1738                                    err_str, sizeof(err_str));
1739         if (rc != 0) {
1740                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1741                 goto out;
1742         }
1743
1744         /*
1745          * no interfaces just the nw_id is specified
1746          */
1747         if (nnids == 0) {
1748                 nids = calloc(1, sizeof(*nids));
1749                 if (nids == NULL) {
1750                         snprintf(err_str, sizeof(err_str),
1751                                 "\"out of memory\"");
1752                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1753                         goto out;
1754                 }
1755                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1756                 nnids = 1;
1757         }
1758
1759         for (i = 0; i < nnids; i++) {
1760                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1761                 data.lic_nid = nids[i];
1762
1763                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1764                 if (rc < 0) {
1765                         rc = -errno;
1766                         snprintf(err_str,
1767                                 sizeof(err_str),
1768                                 "\"cannot del network: %s\"", strerror(errno));
1769                 }
1770         }
1771
1772         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1773                                  intf_on_network) {
1774                 list_del(&intf_descr->intf_on_network);
1775                 free_intf_descr(intf_descr);
1776         }
1777
1778 out:
1779         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1780
1781         if (nids != NULL)
1782                 free(nids);
1783
1784         return rc;
1785 }
1786
1787 static int
1788 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
1789                            enum lnet_health_type type, char *name,
1790                            int seq_no, struct cYAML **err_rc)
1791 {
1792         struct lnet_ioctl_reset_health_cfg data;
1793         int rc = LUSTRE_CFG_RC_NO_ERR;
1794         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1795
1796         LIBCFS_IOC_INIT_V2(data, rh_hdr);
1797         data.rh_type = type;
1798         data.rh_all = all;
1799         data.rh_value = value;
1800         data.rh_nid = nid;
1801
1802         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
1803         if (rc != 0) {
1804                 rc = -errno;
1805                 snprintf(err_str,
1806                          sizeof(err_str), "Can not configure health value: %s",
1807                          strerror(errno));
1808         }
1809
1810         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
1811
1812         return rc;
1813 }
1814
1815 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
1816                                   struct cYAML **err_rc)
1817 {
1818         lnet_nid_t nid;
1819         if (ni_nid)
1820                 nid = libcfs_str2nid(ni_nid);
1821         else
1822                 nid = LNET_NID_ANY;
1823         return lustre_lnet_config_healthv(value, all, nid,
1824                                           LNET_HEALTH_TYPE_LOCAL_NI,
1825                                           "ni healthv", seq_no, err_rc);
1826 }
1827
1828 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
1829                                        int seq_no, struct cYAML **err_rc)
1830 {
1831         lnet_nid_t nid;
1832         if (lpni_nid)
1833                 nid = libcfs_str2nid(lpni_nid);
1834         else
1835                 nid = LNET_NID_ANY;
1836         return lustre_lnet_config_healthv(value, all, nid,
1837                                           LNET_HEALTH_TYPE_PEER_NI,
1838                                           "peer_ni healthv", seq_no, err_rc);
1839 }
1840
1841 static bool
1842 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1843                           struct lnet_ioctl_comm_count *counts)
1844 {
1845         if (cYAML_create_number(yaml, "put",
1846                                 counts->ico_put_count)
1847                                         == NULL)
1848                 return false;
1849         if (cYAML_create_number(yaml, "get",
1850                                 counts->ico_get_count)
1851                                         == NULL)
1852                 return false;
1853         if (cYAML_create_number(yaml, "reply",
1854                                 counts->ico_reply_count)
1855                                         == NULL)
1856                 return false;
1857         if (cYAML_create_number(yaml, "ack",
1858                                 counts->ico_ack_count)
1859                                         == NULL)
1860                 return false;
1861         if (cYAML_create_number(yaml, "hello",
1862                                 counts->ico_hello_count)
1863                                         == NULL)
1864                 return false;
1865
1866         return true;
1867 }
1868
1869 static struct lnet_ioctl_comm_count *
1870 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1871 {
1872         if (idx == 0)
1873                 return &msg_stats->im_send_stats;
1874         if (idx == 1)
1875                 return &msg_stats->im_recv_stats;
1876         if (idx == 2)
1877                 return &msg_stats->im_drop_stats;
1878
1879         return NULL;
1880 }
1881
1882 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1883                          struct cYAML **show_rc, struct cYAML **err_rc,
1884                          bool backup)
1885 {
1886         char *buf;
1887         struct lnet_ioctl_config_ni *ni_data;
1888         struct lnet_ioctl_config_lnd_tunables *lnd;
1889         struct lnet_ioctl_element_stats *stats;
1890         struct lnet_ioctl_element_msg_stats msg_stats;
1891         struct lnet_ioctl_local_ni_hstats hstats;
1892         __u32 net = LNET_NET_ANY;
1893         __u32 prev_net = LNET_NET_ANY;
1894         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1895         int l_errno = 0;
1896         struct cYAML *root = NULL, *tunables = NULL,
1897                 *net_node = NULL, *interfaces = NULL,
1898                 *item = NULL, *first_seq = NULL,
1899                 *tmp = NULL, *statistics = NULL,
1900                 *yhstats = NULL;
1901         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1902         char str_buf[str_buf_len];
1903         char *pos;
1904         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
1905         bool exist = false, new_net = true;
1906         int net_num = 0;
1907         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1908
1909         buf = calloc(1, buf_size);
1910         if (buf == NULL)
1911                 goto out;
1912
1913         ni_data = (struct lnet_ioctl_config_ni *)buf;
1914
1915         if (nw != NULL) {
1916                 net = libcfs_str2net(nw);
1917                 if (net == LNET_NET_ANY) {
1918                         snprintf(err_str,
1919                                  sizeof(err_str),
1920                                  "\"cannot parse net '%s'\"", nw);
1921                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1922                         goto out;
1923                 }
1924         }
1925
1926         root = cYAML_create_object(NULL, NULL);
1927         if (root == NULL)
1928                 goto out;
1929
1930         net_node = cYAML_create_seq(root, "net");
1931         if (net_node == NULL)
1932                 goto out;
1933
1934         for (i = 0;; i++) {
1935                 __u32 rc_net;
1936
1937                 memset(buf, 0, buf_size);
1938
1939                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1940                 /*
1941                  * set the ioc_len to the proper value since INIT assumes
1942                  * size of data
1943                  */
1944                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1945                 ni_data->lic_idx = i;
1946
1947                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1948                 if (rc != 0) {
1949                         l_errno = errno;
1950                         break;
1951                 }
1952
1953                 rc_net = LNET_NIDNET(ni_data->lic_nid);
1954
1955                 /* filter on provided data */
1956                 if (net != LNET_NET_ANY &&
1957                     net != rc_net)
1958                         continue;
1959
1960                 /* if we're backing up don't store lo */
1961                 if (backup && LNET_NETTYP(rc_net) == LOLND)
1962                         continue;
1963
1964                 /* default rc to -1 in case we hit the goto */
1965                 rc = -1;
1966                 exist = true;
1967
1968                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1969                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1970                         (ni_data->lic_bulk + sizeof(*stats));
1971
1972                 if (rc_net != prev_net) {
1973                         prev_net = rc_net;
1974                         new_net = true;
1975                         net_num++;
1976                 }
1977
1978                 if (new_net) {
1979                         if (!cYAML_create_string(net_node, "net type",
1980                                                  libcfs_net2str(rc_net)))
1981                                 goto out;
1982
1983                         tmp = cYAML_create_seq(net_node, "local NI(s)");
1984                         if (tmp == NULL)
1985                                 goto out;
1986                         new_net = false;
1987                 }
1988
1989                 /* create the tree to be printed. */
1990                 item = cYAML_create_seq_item(tmp);
1991                 if (item == NULL)
1992                         goto out;
1993
1994                 if (first_seq == NULL)
1995                         first_seq = item;
1996
1997                 if (!backup &&
1998                     cYAML_create_string(item, "nid",
1999                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2000                         goto out;
2001
2002                 if (!backup &&
2003                     cYAML_create_string(item,
2004                                         "status",
2005                                         (ni_data->lic_status ==
2006                                           LNET_NI_STATUS_UP) ?
2007                                             "up" : "down") == NULL)
2008                         goto out;
2009
2010                 /* don't add interfaces unless there is at least one
2011                  * interface */
2012                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2013                         interfaces = cYAML_create_object(item, "interfaces");
2014                         if (interfaces == NULL)
2015                                 goto out;
2016
2017                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2018                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2019                                         snprintf(str_buf,
2020                                                  sizeof(str_buf), "%d", j);
2021                                         if (cYAML_create_string(interfaces,
2022                                                 str_buf,
2023                                                 ni_data->lic_ni_intf[j]) ==
2024                                                         NULL)
2025                                                 goto out;
2026                                 }
2027                         }
2028                 }
2029
2030                 if (detail) {
2031                         char *limit;
2032                         int k;
2033
2034                         if (backup)
2035                                 goto continue_without_msg_stats;
2036
2037                         statistics = cYAML_create_object(item, "statistics");
2038                         if (statistics == NULL)
2039                                 goto out;
2040
2041                         if (cYAML_create_number(statistics, "send_count",
2042                                                 stats->iel_send_count)
2043                                                         == NULL)
2044                                 goto out;
2045
2046                         if (cYAML_create_number(statistics, "recv_count",
2047                                                 stats->iel_recv_count)
2048                                                         == NULL)
2049                                 goto out;
2050
2051                         if (cYAML_create_number(statistics, "drop_count",
2052                                                 stats->iel_drop_count)
2053                                                         == NULL)
2054                                 goto out;
2055
2056                         if (detail < 2)
2057                                 goto continue_without_msg_stats;
2058
2059                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2060                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2061                         msg_stats.im_idx = i;
2062
2063                         rc = l_ioctl(LNET_DEV_ID,
2064                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2065                                      &msg_stats);
2066                         if (rc != 0) {
2067                                 l_errno = errno;
2068                                 goto continue_without_msg_stats;
2069                         }
2070
2071                         for (k = 0; k < 3; k++) {
2072                                 struct lnet_ioctl_comm_count *counts;
2073                                 struct cYAML *msg_statistics = NULL;
2074
2075                                 msg_statistics = cYAML_create_object(item,
2076                                                  (char *)gmsg_stat_names[k]);
2077                                 if (msg_statistics == NULL)
2078                                         goto out;
2079
2080                                 counts = get_counts(&msg_stats, k);
2081                                 if (counts == NULL)
2082                                         goto out;
2083
2084                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2085                                                                counts))
2086                                         goto out;
2087                         }
2088
2089                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2090                         hstats.hlni_nid = ni_data->lic_nid;
2091                         /* grab health stats */
2092                         rc = l_ioctl(LNET_DEV_ID,
2093                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2094                                      &hstats);
2095                         if (rc != 0) {
2096                                 l_errno = errno;
2097                                 goto continue_without_msg_stats;
2098                         }
2099                         yhstats = cYAML_create_object(item, "health stats");
2100                         if (!yhstats)
2101                                 goto out;
2102                         if (cYAML_create_number(yhstats, "health value",
2103                                                 hstats.hlni_health_value)
2104                                                         == NULL)
2105                                 goto out;
2106                         if (cYAML_create_number(yhstats, "interrupts",
2107                                                 hstats.hlni_local_interrupt)
2108                                                         == NULL)
2109                                 goto out;
2110                         if (cYAML_create_number(yhstats, "dropped",
2111                                                 hstats.hlni_local_dropped)
2112                                                         == NULL)
2113                                 goto out;
2114                         if (cYAML_create_number(yhstats, "aborted",
2115                                                 hstats.hlni_local_aborted)
2116                                                         == NULL)
2117                                 goto out;
2118                         if (cYAML_create_number(yhstats, "no route",
2119                                                 hstats.hlni_local_no_route)
2120                                                         == NULL)
2121                                 goto out;
2122                         if (cYAML_create_number(yhstats, "timeouts",
2123                                                 hstats.hlni_local_timeout)
2124                                                         == NULL)
2125                                 goto out;
2126                         if (cYAML_create_number(yhstats, "error",
2127                                                 hstats.hlni_local_error)
2128                                                         == NULL)
2129                                 goto out;
2130
2131 continue_without_msg_stats:
2132                         tunables = cYAML_create_object(item, "tunables");
2133                         if (!tunables)
2134                                 goto out;
2135
2136                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2137                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2138                                 goto out;
2139
2140                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2141                                                      &lnd->lt_tun);
2142                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2143                             rc != LUSTRE_CFG_RC_NO_MATCH)
2144                                 goto out;
2145
2146                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2147                                 tunables = cYAML_create_object(item,
2148                                                                "lnd tunables");
2149                                 if (tunables == NULL)
2150                                         goto out;
2151                         }
2152
2153                         if (!backup &&
2154                             cYAML_create_number(item, "dev cpt",
2155                                                 ni_data->lic_dev_cpt) == NULL)
2156                                 goto out;
2157
2158                         if (!backup &&
2159                             cYAML_create_number(item, "tcp bonding",
2160                                                 ni_data->lic_tcp_bonding)
2161                                                         == NULL)
2162                                 goto out;
2163
2164                         /* out put the CPTs in the format: "[x,x,x,...]" */
2165                         pos = str_buf;
2166                         limit = str_buf + str_buf_len - 3;
2167                         pos += scnprintf(pos, limit - pos, "\"[");
2168                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2169                                 j < ni_data->lic_ncpts &&
2170                                 pos < limit; j++) {
2171                                 pos += scnprintf(pos, limit - pos,
2172                                                  "%d", ni_data->lic_cpts[j]);
2173                                 if ((j + 1) < ni_data->lic_ncpts)
2174                                         pos += scnprintf(pos, limit - pos, ",");
2175                         }
2176                         snprintf(pos, 3, "]\"");
2177
2178                         if (ni_data->lic_ncpts >= 1 &&
2179                             cYAML_create_string(item, "CPT",
2180                                                 str_buf) == NULL)
2181                                 goto out;
2182                 }
2183         }
2184
2185         /* Print out the net information only if show_rc is not provided */
2186         if (show_rc == NULL)
2187                 cYAML_print_tree(root);
2188
2189         if (l_errno != ENOENT) {
2190                 snprintf(err_str,
2191                          sizeof(err_str),
2192                          "\"cannot get networks: %s\"",
2193                          strerror(l_errno));
2194                 rc = -l_errno;
2195                 goto out;
2196         } else
2197                 rc = LUSTRE_CFG_RC_NO_ERR;
2198
2199         snprintf(err_str, sizeof(err_str), "\"success\"");
2200 out:
2201         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2202                 cYAML_free_tree(root);
2203         } else if (show_rc != NULL && *show_rc != NULL) {
2204                 struct cYAML *show_node;
2205                 /* find the net node, if one doesn't exist
2206                  * then insert one.  Otherwise add to the one there
2207                  */
2208                 show_node = cYAML_get_object_item(*show_rc, "net");
2209                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2210                         cYAML_insert_child(show_node, first_seq);
2211                         free(net_node);
2212                         free(root);
2213                 } else if (show_node == NULL) {
2214                         cYAML_insert_sibling((*show_rc)->cy_child,
2215                                                 net_node);
2216                         free(root);
2217                 } else {
2218                         cYAML_free_tree(root);
2219                 }
2220         } else {
2221                 *show_rc = root;
2222         }
2223
2224         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2225
2226         return rc;
2227 }
2228
2229 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2230 {
2231         struct lnet_ioctl_config_data data;
2232         int rc = LUSTRE_CFG_RC_NO_ERR;
2233         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2234
2235         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2236         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2237
2238         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2239         if (rc != 0) {
2240                 rc = -errno;
2241                 snprintf(err_str,
2242                          sizeof(err_str),
2243                          "\"cannot %s routing %s\"",
2244                          (enable) ? "enable" : "disable", strerror(errno));
2245                 goto out;
2246         }
2247
2248 out:
2249         cYAML_build_error(rc, seq_no,
2250                          (enable) ? ADD_CMD : DEL_CMD,
2251                          "routing", err_str, err_rc);
2252
2253         return rc;
2254 }
2255
2256 int ioctl_set_value(__u32 val, int ioc, char *name,
2257                     int seq_no, struct cYAML **err_rc)
2258 {
2259         struct lnet_ioctl_set_value data;
2260         int rc = LUSTRE_CFG_RC_NO_ERR;
2261         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2262
2263         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2264         data.sv_value = val;
2265
2266         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2267         if (rc != 0) {
2268                 rc = -errno;
2269                 snprintf(err_str,
2270                          sizeof(err_str),
2271                          "\"cannot configure %s to %d: %s\"", name,
2272                          val, strerror(errno));
2273         }
2274
2275         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2276
2277         return rc;
2278 }
2279
2280 int lustre_lnet_config_recov_intrv(int intrv, int seq_no, struct cYAML **err_rc)
2281 {
2282         int rc = LUSTRE_CFG_RC_NO_ERR;
2283         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2284         char val[LNET_MAX_STR_LEN];
2285
2286         snprintf(val, sizeof(val), "%d", intrv);
2287
2288         rc = write_sysfs_file(modparam_path, "lnet_recovery_interval", val,
2289                               1, strlen(val) + 1);
2290         if (rc)
2291                 snprintf(err_str, sizeof(err_str),
2292                          "\"cannot configure recovery interval: %s\"",
2293                          strerror(errno));
2294
2295         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_interval", err_str, err_rc);
2296
2297         return rc;
2298 }
2299
2300 int lustre_lnet_config_rtr_sensitivity(int sen, int seq_no, struct cYAML **err_rc)
2301 {
2302         int rc = LUSTRE_CFG_RC_NO_ERR;
2303         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2304         char val[LNET_MAX_STR_LEN];
2305
2306         snprintf(val, sizeof(val), "%d", sen);
2307
2308         rc = write_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
2309                               1, strlen(val) + 1);
2310         if (rc)
2311                 snprintf(err_str, sizeof(err_str),
2312                          "\"cannot configure router health sensitivity: %s\"",
2313                          strerror(errno));
2314
2315         cYAML_build_error(rc, seq_no, ADD_CMD, "router_sensitivity", err_str, err_rc);
2316
2317         return rc;
2318 }
2319
2320 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2321 {
2322         int rc = LUSTRE_CFG_RC_NO_ERR;
2323         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2324         char val[LNET_MAX_STR_LEN];
2325
2326         snprintf(val, sizeof(val), "%d", sen);
2327
2328         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2329                               1, strlen(val) + 1);
2330         if (rc)
2331                 snprintf(err_str, sizeof(err_str),
2332                          "\"cannot configure health sensitivity: %s\"",
2333                          strerror(errno));
2334
2335         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2336
2337         return rc;
2338 }
2339
2340 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2341 {
2342         int rc = LUSTRE_CFG_RC_NO_ERR;
2343         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2344         char val[LNET_MAX_STR_LEN];
2345
2346         snprintf(val, sizeof(val), "%d", timeout);
2347
2348         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2349                               1, strlen(val) + 1);
2350         if (rc)
2351                 snprintf(err_str, sizeof(err_str),
2352                          "\"cannot configure transaction timeout: %s\"",
2353                          strerror(errno));
2354
2355         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2356
2357         return rc;
2358 }
2359
2360 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2361 {
2362         int rc = LUSTRE_CFG_RC_NO_ERR;
2363         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2364         char val[LNET_MAX_STR_LEN];
2365
2366         snprintf(val, sizeof(val), "%d", count);
2367
2368         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2369                               1, strlen(val) + 1);
2370         if (rc)
2371                 snprintf(err_str, sizeof(err_str),
2372                          "\"cannot configure retry count: %s\"",
2373                          strerror(errno));
2374
2375         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2376
2377         return rc;
2378 }
2379
2380 int lustre_lnet_config_response_tracking(int val, int seq_no,
2381                                          struct cYAML **err_rc)
2382 {
2383         int rc = LUSTRE_CFG_RC_NO_ERR;
2384         char err_str[LNET_MAX_STR_LEN];
2385         char val_str[LNET_MAX_STR_LEN];
2386
2387         if (val < 0 || val > 3) {
2388                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2389                 snprintf(err_str, sizeof(err_str),
2390                          "\"Valid values are: 0, 1, 2, or 3\"");
2391         } else {
2392                 snprintf(err_str, sizeof(err_str), "\"success\"");
2393
2394                 snprintf(val_str, sizeof(val_str), "%d", val);
2395
2396                 rc = write_sysfs_file(modparam_path, "lnet_response_tracking",
2397                                       val_str, 1, strlen(val_str) + 1);
2398                 if (rc)
2399                         snprintf(err_str, sizeof(err_str),
2400                                  "\"cannot configure response tracking: %s\"",
2401                                  strerror(errno));
2402         }
2403
2404         cYAML_build_error(rc, seq_no, ADD_CMD, "response_tracking", err_str,
2405                           err_rc);
2406
2407         return rc;
2408 }
2409
2410 int lustre_lnet_config_recovery_limit(int val, int seq_no,
2411                                       struct cYAML **err_rc)
2412 {
2413         int rc = LUSTRE_CFG_RC_NO_ERR;
2414         char err_str[LNET_MAX_STR_LEN];
2415         char val_str[LNET_MAX_STR_LEN];
2416
2417         if (val < 0) {
2418                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2419                 snprintf(err_str, sizeof(err_str),
2420                          "\"Must be greater than or equal to 0\"");
2421         } else {
2422                 snprintf(err_str, sizeof(err_str), "\"success\"");
2423
2424                 snprintf(val_str, sizeof(val_str), "%d", val);
2425
2426                 rc = write_sysfs_file(modparam_path, "lnet_recovery_limit",
2427                                       val_str, 1, strlen(val_str) + 1);
2428                 if (rc)
2429                         snprintf(err_str, sizeof(err_str),
2430                                  "\"cannot configure recovery limit: %s\"",
2431                                  strerror(errno));
2432         }
2433
2434         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_limit", err_str,
2435                           err_rc);
2436
2437         return rc;
2438 }
2439
2440 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2441 {
2442         int rc = LUSTRE_CFG_RC_NO_ERR;
2443         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2444         char val[LNET_MAX_STR_LEN];
2445
2446         snprintf(val, sizeof(val), "%d", max);
2447
2448         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2449                               1, strlen(val) + 1);
2450         if (rc)
2451                 snprintf(err_str, sizeof(err_str),
2452                          "\"cannot configure max interfaces: %s\"",
2453                          strerror(errno));
2454
2455         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2456
2457         return rc;
2458 }
2459
2460 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2461 {
2462         int rc = LUSTRE_CFG_RC_NO_ERR;
2463         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2464         char val[LNET_MAX_STR_LEN];
2465
2466         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2467
2468         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2469                               1, strlen(val) + 1);
2470         if (rc)
2471                 snprintf(err_str, sizeof(err_str),
2472                          "\"cannot configure discovery: %s\"",
2473                          strerror(errno));
2474
2475         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2476
2477         return rc;
2478
2479 }
2480
2481 int lustre_lnet_config_drop_asym_route(int drop, int seq_no,
2482                                        struct cYAML **err_rc)
2483 {
2484         int rc = LUSTRE_CFG_RC_NO_ERR;
2485         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2486         char val[LNET_MAX_STR_LEN];
2487
2488         snprintf(val, sizeof(val), "%u", (drop) ? 1 : 0);
2489
2490         rc = write_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
2491                               1, strlen(val) + 1);
2492         if (rc)
2493                 snprintf(err_str, sizeof(err_str),
2494                          "\"cannot configure drop asym route: %s\"",
2495                          strerror(errno));
2496
2497         cYAML_build_error(rc, seq_no, ADD_CMD, "drop_asym_route",
2498                           err_str, err_rc);
2499
2500         return rc;
2501
2502 }
2503
2504 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2505 {
2506         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2507                                "numa_range", seq_no, err_rc);
2508 }
2509
2510 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2511                                struct cYAML **err_rc)
2512 {
2513         struct lnet_ioctl_config_data data;
2514         int rc = LUSTRE_CFG_RC_NO_ERR;
2515         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2516
2517         /* -1 indicates to ignore changes to this field */
2518         if (tiny < -1 || small < -1 || large < -1) {
2519                 snprintf(err_str,
2520                          sizeof(err_str),
2521                          "\"tiny, small and large must be >= 0\"");
2522                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2523                 goto out;
2524         }
2525
2526         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2527         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2528         data.cfg_config_u.cfg_buffers.buf_small = small;
2529         data.cfg_config_u.cfg_buffers.buf_large = large;
2530
2531         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2532         if (rc != 0) {
2533                 rc = -errno;
2534                 snprintf(err_str,
2535                          sizeof(err_str),
2536                          "\"cannot configure buffers: %s\"", strerror(errno));
2537                 goto out;
2538         }
2539
2540 out:
2541         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2542
2543         return rc;
2544 }
2545
2546 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2547                              struct cYAML **err_rc, bool backup)
2548 {
2549         struct lnet_ioctl_config_data *data;
2550         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2551         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2552         int l_errno = 0;
2553         char *buf;
2554         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2555         int buf_count[LNET_NRBPOOLS] = {0};
2556         struct cYAML *root = NULL, *pools_node = NULL,
2557                      *type_node = NULL, *item = NULL, *cpt = NULL,
2558                      *first_seq = NULL, *buffers = NULL;
2559         int i, j;
2560         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
2561         char node_name[LNET_MAX_STR_LEN];
2562         bool exist = false;
2563
2564         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2565         if (buf == NULL)
2566                 goto out;
2567
2568         data = (struct lnet_ioctl_config_data *)buf;
2569
2570         root = cYAML_create_object(NULL, NULL);
2571         if (root == NULL)
2572                 goto out;
2573
2574         if (backup)
2575                 pools_node = cYAML_create_object(root, "routing");
2576         else
2577                 pools_node = cYAML_create_seq(root, "routing");
2578         if (pools_node == NULL)
2579                 goto out;
2580
2581         for (i = 0;; i++) {
2582                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2583                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2584                                         sizeof(struct lnet_ioctl_pool_cfg);
2585                 data->cfg_count = i;
2586
2587                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2588                 if (rc != 0) {
2589                         l_errno = errno;
2590                         break;
2591                 }
2592
2593                 exist = true;
2594
2595                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2596
2597                 if (backup)
2598                         goto calculate_buffers;
2599
2600                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2601                 item = cYAML_create_seq_item(pools_node);
2602                 if (item == NULL)
2603                         goto out;
2604
2605                 if (first_seq == NULL)
2606                         first_seq = item;
2607
2608                 cpt = cYAML_create_object(item, node_name);
2609                 if (cpt == NULL)
2610                         goto out;
2611
2612 calculate_buffers:
2613                 /* create the tree  and print */
2614                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2615                         if (!backup) {
2616                                 type_node = cYAML_create_object(cpt, pools[j]);
2617                                 if (type_node == NULL)
2618                                         goto out;
2619                         }
2620                         if (!backup &&
2621                             cYAML_create_number(type_node, "npages",
2622                                                 pool_cfg->pl_pools[j].pl_npages)
2623                             == NULL)
2624                                 goto out;
2625                         if (!backup &&
2626                             cYAML_create_number(type_node, "nbuffers",
2627                                                 pool_cfg->pl_pools[j].
2628                                                   pl_nbuffers) == NULL)
2629                                 goto out;
2630                         if (!backup &&
2631                             cYAML_create_number(type_node, "credits",
2632                                                 pool_cfg->pl_pools[j].
2633                                                    pl_credits) == NULL)
2634                                 goto out;
2635                         if (!backup &&
2636                             cYAML_create_number(type_node, "mincredits",
2637                                                 pool_cfg->pl_pools[j].
2638                                                    pl_mincredits) == NULL)
2639                                 goto out;
2640                         /* keep track of the total count for each of the
2641                          * tiny, small and large buffers */
2642                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2643                 }
2644         }
2645
2646         if (pool_cfg != NULL) {
2647                 if (backup) {
2648                         if (cYAML_create_number(pools_node, "enable",
2649                                                 pool_cfg->pl_routing) ==
2650                         NULL)
2651                                 goto out;
2652
2653                         goto add_buffer_section;
2654                 }
2655
2656                 item = cYAML_create_seq_item(pools_node);
2657                 if (item == NULL)
2658                         goto out;
2659
2660                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2661                     NULL)
2662                         goto out;
2663         }
2664
2665 add_buffer_section:
2666         /* create a buffers entry in the show. This is necessary so that
2667          * if the YAML output is used to configure a node, the buffer
2668          * configuration takes hold */
2669         buffers = cYAML_create_object(root, "buffers");
2670         if (buffers == NULL)
2671                 goto out;
2672
2673         for (i = 0; i < LNET_NRBPOOLS; i++) {
2674                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2675                         goto out;
2676         }
2677
2678         if (show_rc == NULL)
2679                 cYAML_print_tree(root);
2680
2681         if (l_errno != ENOENT) {
2682                 snprintf(err_str,
2683                          sizeof(err_str),
2684                          "\"cannot get routing information: %s\"",
2685                          strerror(l_errno));
2686                 rc = -l_errno;
2687                 goto out;
2688         } else
2689                 rc = LUSTRE_CFG_RC_NO_ERR;
2690
2691         snprintf(err_str, sizeof(err_str), "\"success\"");
2692         rc = LUSTRE_CFG_RC_NO_ERR;
2693
2694 out:
2695         free(buf);
2696         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2697                 cYAML_free_tree(root);
2698         } else if (show_rc != NULL && *show_rc != NULL) {
2699                 struct cYAML *routing_node;
2700                 /* there should exist only one routing block and one
2701                  * buffers block. If there already exists a previous one
2702                  * then don't add another */
2703                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2704                 if (routing_node == NULL) {
2705                         cYAML_insert_sibling((*show_rc)->cy_child,
2706                                                 root->cy_child);
2707                         free(root);
2708                 } else {
2709                         cYAML_free_tree(root);
2710                 }
2711         } else {
2712                 *show_rc = root;
2713         }
2714
2715         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2716
2717         return rc;
2718 }
2719
2720 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2721                           struct cYAML **show_rc, struct cYAML **err_rc,
2722                           bool backup)
2723 {
2724         /*
2725          * TODO: This function is changing in a future patch to accommodate
2726          * PEER_LIST and proper filtering on any nid of the peer
2727          */
2728         struct lnet_ioctl_peer_cfg peer_info;
2729         struct lnet_peer_ni_credit_info *lpni_cri;
2730         struct lnet_ioctl_element_stats *lpni_stats;
2731         struct lnet_ioctl_element_msg_stats *msg_stats;
2732         struct lnet_ioctl_peer_ni_hstats *hstats;
2733         lnet_nid_t *nidp;
2734         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2735         int i, j, k;
2736         int l_errno = 0;
2737         __u32 count;
2738         __u32 size;
2739         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2740                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2741                      *msg_statistics = NULL, *statistics = NULL,
2742                      *yhstats;
2743         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
2744         struct lnet_process_id *list = NULL;
2745         void *data = NULL;
2746         void *lpni_data;
2747         bool exist = false;
2748
2749         /* create struct cYAML root object */
2750         root = cYAML_create_object(NULL, NULL);
2751         if (root == NULL)
2752                 goto out;
2753
2754         peer_root = cYAML_create_seq(root, "peer");
2755         if (peer_root == NULL)
2756                 goto out;
2757
2758         count = 1000;
2759         size = count * sizeof(struct lnet_process_id);
2760         list = malloc(size);
2761         if (list == NULL) {
2762                 l_errno = ENOMEM;
2763                 goto out;
2764         }
2765         if (knid != NULL) {
2766                 list[0].nid = libcfs_str2nid(knid);
2767                 count = 1;
2768         } else {
2769                 for (;;) {
2770                         memset(&peer_info, 0, sizeof(peer_info));
2771                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2772                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2773                         peer_info.prcfg_size = size;
2774                         peer_info.prcfg_bulk = list;
2775
2776                         l_errno = 0;
2777                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2778                                      &peer_info);
2779                         count = peer_info.prcfg_count;
2780                         if (rc == 0)
2781                                 break;
2782                         l_errno = errno;
2783                         if (l_errno != E2BIG) {
2784                                 snprintf(err_str,
2785                                         sizeof(err_str),
2786                                         "\"cannot get peer list: %s\"",
2787                                         strerror(l_errno));
2788                                 rc = -l_errno;
2789                                 goto out;
2790                         }
2791                         free(list);
2792                         size = peer_info.prcfg_size;
2793                         list = malloc(size);
2794                         if (list == NULL) {
2795                                 l_errno = ENOMEM;
2796                                 goto out;
2797                         }
2798                 }
2799         }
2800
2801         size = 4096;
2802         data = malloc(size);
2803         if (data == NULL) {
2804                 l_errno = ENOMEM;
2805                 goto out;
2806         }
2807
2808         for (i = 0; i < count; i++) {
2809                 for (;;) {
2810                         memset(&peer_info, 0, sizeof(peer_info));
2811                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2812                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2813                         peer_info.prcfg_prim_nid = list[i].nid;
2814                         peer_info.prcfg_size = size;
2815                         peer_info.prcfg_bulk = data;
2816
2817                         l_errno = 0;
2818                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2819                                      &peer_info);
2820                         if (rc == 0)
2821                                 break;
2822                         l_errno = errno;
2823                         if (l_errno != E2BIG) {
2824                                 snprintf(err_str,
2825                                         sizeof(err_str),
2826                                         "\"cannot get peer information: %s\"",
2827                                         strerror(l_errno));
2828                                 rc = -l_errno;
2829                                 goto out;
2830                         }
2831                         free(data);
2832                         size = peer_info.prcfg_size;
2833                         data = malloc(size);
2834                         if (data == NULL) {
2835                                 l_errno = ENOMEM;
2836                                 goto out;
2837                         }
2838                 }
2839                 exist = true;
2840
2841                 peer = cYAML_create_seq_item(peer_root);
2842                 if (peer == NULL)
2843                         goto out;
2844
2845                 if (first_seq == NULL)
2846                         first_seq = peer;
2847
2848                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2849                 if (cYAML_create_string(peer, "primary nid",
2850                                         libcfs_nid2str(pnid))
2851                     == NULL)
2852                         goto out;
2853                 if (cYAML_create_string(peer, "Multi-Rail",
2854                                         peer_info.prcfg_mr ? "True" : "False")
2855                     == NULL)
2856                         goto out;
2857                 /*
2858                  * print out the state of the peer only if details are
2859                  * requested
2860                  */
2861                 if (detail >= 3) {
2862                         if (!backup &&
2863                             cYAML_create_number(peer, "peer state",
2864                                                 peer_info.prcfg_state)
2865                                 == NULL)
2866                                 goto out;
2867                 }
2868
2869                 tmp = cYAML_create_seq(peer, "peer ni");
2870                 if (tmp == NULL)
2871                         goto out;
2872
2873                 lpni_data = data;
2874                 for (j = 0; j < peer_info.prcfg_count; j++) {
2875                         nidp = lpni_data;
2876                         lpni_cri = (void*)nidp + sizeof(nidp);
2877                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2878                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2879                         hstats = (void *)msg_stats + sizeof(*msg_stats);
2880                         lpni_data = (void *)hstats + sizeof(*hstats);
2881
2882                         peer_ni = cYAML_create_seq_item(tmp);
2883                         if (peer_ni == NULL)
2884                                 goto out;
2885
2886                         if (cYAML_create_string(peer_ni, "nid",
2887                                                 libcfs_nid2str(*nidp))
2888                             == NULL)
2889                                 goto out;
2890
2891                         if (backup)
2892                                 continue;
2893
2894                         if (cYAML_create_string(peer_ni, "state",
2895                                                 lpni_cri->cr_aliveness)
2896                             == NULL)
2897                                 goto out;
2898
2899                         if (!detail)
2900                                 continue;
2901
2902                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2903                                                 lpni_cri->cr_ni_peer_tx_credits)
2904                             == NULL)
2905                                 goto out;
2906
2907                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2908                                                 lpni_cri->cr_peer_tx_credits)
2909                             == NULL)
2910                                 goto out;
2911
2912                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2913                                                 lpni_cri->cr_peer_min_tx_credits)
2914                             == NULL)
2915                                 goto out;
2916
2917                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2918                                                 lpni_cri->cr_peer_tx_qnob)
2919                             == NULL)
2920                                 goto out;
2921
2922                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2923                                                 lpni_cri->cr_peer_rtr_credits)
2924                             == NULL)
2925                                 goto out;
2926
2927                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2928                                                 lpni_cri->cr_peer_min_rtr_credits)
2929                             == NULL)
2930                                 goto out;
2931
2932                         if (cYAML_create_number(peer_ni, "refcount",
2933                                                 lpni_cri->cr_refcount) == NULL)
2934                                 goto out;
2935
2936                         statistics = cYAML_create_object(peer_ni, "statistics");
2937                         if (statistics == NULL)
2938                                 goto out;
2939
2940                         if (cYAML_create_number(statistics, "send_count",
2941                                                 lpni_stats->iel_send_count)
2942                             == NULL)
2943                                 goto out;
2944
2945                         if (cYAML_create_number(statistics, "recv_count",
2946                                                 lpni_stats->iel_recv_count)
2947                             == NULL)
2948                                 goto out;
2949
2950                         if (cYAML_create_number(statistics, "drop_count",
2951                                                 lpni_stats->iel_drop_count)
2952                             == NULL)
2953                                 goto out;
2954
2955                         if (detail < 2)
2956                                 continue;
2957
2958                         for (k = 0; k < 3; k++) {
2959                                 struct lnet_ioctl_comm_count *counts;
2960
2961                                 msg_statistics = cYAML_create_object(peer_ni,
2962                                                  (char *) gmsg_stat_names[k]);
2963                                 if (msg_statistics == NULL)
2964                                         goto out;
2965
2966                                 counts = get_counts(msg_stats, k);
2967                                 if (counts == NULL)
2968                                         goto out;
2969
2970                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2971                                                                counts))
2972                                         goto out;
2973                         }
2974
2975                         yhstats = cYAML_create_object(peer_ni, "health stats");
2976                         if (!yhstats)
2977                                 goto out;
2978                         if (cYAML_create_number(yhstats, "health value",
2979                                                 hstats->hlpni_health_value)
2980                                                         == NULL)
2981                                 goto out;
2982                         if (cYAML_create_number(yhstats, "dropped",
2983                                                 hstats->hlpni_remote_dropped)
2984                                                         == NULL)
2985                                 goto out;
2986                         if (cYAML_create_number(yhstats, "timeout",
2987                                                 hstats->hlpni_remote_timeout)
2988                                                         == NULL)
2989                                 goto out;
2990                         if (cYAML_create_number(yhstats, "error",
2991                                                 hstats->hlpni_remote_error)
2992                                                         == NULL)
2993                                 goto out;
2994                         if (cYAML_create_number(yhstats, "network timeout",
2995                                                 hstats->hlpni_network_timeout)
2996                                                         == NULL)
2997                                 goto out;
2998                 }
2999         }
3000
3001         /* print output iff show_rc is not provided */
3002         if (show_rc == NULL)
3003                 cYAML_print_tree(root);
3004
3005         snprintf(err_str, sizeof(err_str), "\"success\"");
3006         rc = LUSTRE_CFG_RC_NO_ERR;
3007
3008 out:
3009         free(list);
3010         free(data);
3011         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
3012                 cYAML_free_tree(root);
3013         } else if (show_rc != NULL && *show_rc != NULL) {
3014                 struct cYAML *show_node;
3015                 /* find the peer node, if one doesn't exist then
3016                  * insert one.  Otherwise add to the one there
3017                  */
3018                 show_node = cYAML_get_object_item(*show_rc,
3019                                                   "peer");
3020                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3021                         cYAML_insert_child(show_node, first_seq);
3022                         free(peer_root);
3023                         free(root);
3024                 } else if (show_node == NULL) {
3025                         cYAML_insert_sibling((*show_rc)->cy_child,
3026                                              peer_root);
3027                         free(root);
3028                 } else {
3029                         cYAML_free_tree(root);
3030                 }
3031         } else {
3032                 *show_rc = root;
3033         }
3034
3035         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3036                           err_rc);
3037
3038         return rc;
3039 }
3040
3041 int lustre_lnet_list_peer(int seq_no,
3042                           struct cYAML **show_rc, struct cYAML **err_rc)
3043 {
3044         struct lnet_ioctl_peer_cfg peer_info;
3045         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3046         __u32 count;
3047         __u32 size;
3048         int i = 0;
3049         int l_errno = 0;
3050         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
3051         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3052         struct lnet_process_id *list = NULL;
3053
3054         memset(&peer_info, 0, sizeof(peer_info));
3055
3056         /* create struct cYAML root object */
3057         root = cYAML_create_object(NULL, NULL);
3058         if (root == NULL)
3059                 goto out;
3060
3061         list_root = cYAML_create_seq(root, "peer list");
3062         if (list_root == NULL)
3063                 goto out;
3064
3065         count = 1000;
3066         size = count * sizeof(struct lnet_process_id);
3067         list = malloc(size);
3068         if (list == NULL) {
3069                 l_errno = ENOMEM;
3070                 goto out;
3071         }
3072         for (;;) {
3073                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3074                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3075                 peer_info.prcfg_size = size;
3076                 peer_info.prcfg_bulk = list;
3077
3078                 l_errno = 0;
3079                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
3080                 count = peer_info.prcfg_count;
3081                 if (rc == 0)
3082                         break;
3083                 l_errno = errno;
3084                 if (l_errno != E2BIG) {
3085                         snprintf(err_str,
3086                                 sizeof(err_str),
3087                                 "\"cannot get peer list: %s\"",
3088                                 strerror(l_errno));
3089                         rc = -l_errno;
3090                         goto out;
3091                 }
3092                 free(list);
3093                 size = peer_info.prcfg_size;
3094                 list = malloc(size);
3095                 if (list == NULL) {
3096                         l_errno = ENOMEM;
3097                         goto out;
3098                 }
3099         }
3100
3101         /* count is now the actual number of ids in the list. */
3102         for (i = 0; i < count; i++) {
3103                 if (cYAML_create_string(list_root, "nid",
3104                                         libcfs_nid2str(list[i].nid))
3105                     == NULL)
3106                         goto out;
3107         }
3108
3109         /* print output iff show_rc is not provided */
3110         if (show_rc == NULL)
3111                 cYAML_print_tree(root);
3112
3113         snprintf(err_str, sizeof(err_str), "\"success\"");
3114         rc = LUSTRE_CFG_RC_NO_ERR;
3115
3116 out:
3117         if (list != NULL)
3118                 free(list);
3119         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3120                 cYAML_free_tree(root);
3121         } else if (show_rc != NULL && *show_rc != NULL) {
3122                 struct cYAML *show_node;
3123                 /* find the peer node, if one doesn't exist then
3124                  * insert one.  Otherwise add to the one there
3125                  */
3126                 show_node = cYAML_get_object_item(*show_rc,
3127                                                   "peer");
3128                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3129                         cYAML_insert_child(show_node, first_seq);
3130                         free(list_root);
3131                         free(root);
3132                 } else if (show_node == NULL) {
3133                         cYAML_insert_sibling((*show_rc)->cy_child,
3134                                              list_root);
3135                         free(root);
3136                 } else {
3137                         cYAML_free_tree(root);
3138                 }
3139         } else {
3140                 *show_rc = root;
3141         }
3142
3143         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3144                           err_rc);
3145
3146         return rc;
3147 }
3148
3149 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
3150                           struct cYAML *root)
3151 {
3152         struct cYAML *show_node;
3153
3154         show_node = cYAML_get_object_item(show_rc, "global");
3155         if (show_node != NULL)
3156                 cYAML_insert_sibling(show_node->cy_child,
3157                                      node->cy_child);
3158         else
3159                 cYAML_insert_sibling(show_rc->cy_child,
3160                                      node);
3161         free(root);
3162 }
3163
3164 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
3165                                    char *name, __u64 value,
3166                                    struct cYAML **show_rc,
3167                                    struct cYAML **err_rc, int err)
3168 {
3169         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3170         struct cYAML *root = NULL, *global = NULL;
3171
3172         if (err) {
3173                 rc = err;
3174                 goto out;
3175         }
3176
3177         root = cYAML_create_object(NULL, NULL);
3178         if (root == NULL)
3179                 goto out;
3180
3181         global = cYAML_create_object(root, "global");
3182         if (global == NULL)
3183                 goto out;
3184
3185         if (cYAML_create_number(global, name,
3186                                 value) == NULL)
3187                 goto out;
3188
3189         if (show_rc == NULL)
3190                 cYAML_print_tree(root);
3191
3192         snprintf(err_str, err_len, "\"success\"");
3193
3194         rc = LUSTRE_CFG_RC_NO_ERR;
3195
3196 out:
3197         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3198                 cYAML_free_tree(root);
3199         } else if (show_rc != NULL && *show_rc != NULL) {
3200                 add_to_global(*show_rc, global, root);
3201         } else {
3202                 *show_rc = root;
3203         }
3204
3205         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
3206
3207         return rc;
3208 }
3209
3210 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
3211                                     struct cYAML **show_rc,
3212                                     struct cYAML **err_rc)
3213 {
3214         struct lnet_ioctl_set_value data;
3215         int rc;
3216         int l_errno = 0;
3217         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3218
3219         LIBCFS_IOC_INIT_V2(data, sv_hdr);
3220
3221         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3222         if (rc != 0) {
3223                 l_errno = -errno;
3224                 snprintf(err_str,
3225                          sizeof(err_str),
3226                          "\"cannot get %s: %s\"",
3227                          name, strerror(l_errno));
3228         }
3229
3230         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3231                                        data.sv_value, show_rc, err_rc, l_errno);
3232 }
3233
3234 int lustre_lnet_show_recov_intrv(int seq_no, struct cYAML **show_rc,
3235                                  struct cYAML **err_rc)
3236 {
3237         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3238         char val[LNET_MAX_STR_LEN];
3239         int intrv = -1, l_errno = 0;
3240         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3241
3242         rc = read_sysfs_file(modparam_path, "lnet_recovery_interval", val,
3243                              1, sizeof(val));
3244         if (rc) {
3245                 l_errno = -errno;
3246                 snprintf(err_str, sizeof(err_str),
3247                          "\"cannot get recovery interval: %d\"", rc);
3248         } else {
3249                 intrv = atoi(val);
3250         }
3251
3252         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3253                                        "recovery_interval", intrv, show_rc,
3254                                        err_rc, l_errno);
3255 }
3256
3257 int lustre_lnet_show_hsensitivity(int seq_no, struct cYAML **show_rc,
3258                                   struct cYAML **err_rc)
3259 {
3260         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3261         char val[LNET_MAX_STR_LEN];
3262         int sen = -1, l_errno = 0;
3263         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3264
3265         rc = read_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
3266                              1, sizeof(val));
3267         if (rc) {
3268                 l_errno = -errno;
3269                 snprintf(err_str, sizeof(err_str),
3270                          "\"cannot get health sensitivity: %d\"", rc);
3271         } else {
3272                 sen = atoi(val);
3273         }
3274
3275         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3276                                        "health_sensitivity", sen, show_rc,
3277                                        err_rc, l_errno);
3278 }
3279
3280 int lustre_lnet_show_rtr_sensitivity(int seq_no, struct cYAML **show_rc,
3281                                      struct cYAML **err_rc)
3282 {
3283         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3284         char val[LNET_MAX_STR_LEN];
3285         int sen = -1, l_errno = 0;
3286         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3287
3288         rc = read_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
3289                              1, sizeof(val));
3290         if (rc) {
3291                 l_errno = -errno;
3292                 snprintf(err_str, sizeof(err_str),
3293                          "\"cannot get router sensitivity percentage: %d\"", rc);
3294         } else {
3295                 sen = atoi(val);
3296         }
3297
3298         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3299                                        "router_sensitivity", sen, show_rc,
3300                                        err_rc, l_errno);
3301 }
3302
3303 int lustre_lnet_show_lnd_timeout(int seq_no, struct cYAML **show_rc,
3304                                  struct cYAML **err_rc)
3305 {
3306         char val[LNET_MAX_STR_LEN];
3307         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3308         int lnd_to = -1;
3309         int l_errno = 0;
3310         int rc;
3311         int fd;
3312         glob_t path;
3313
3314         rc = cfs_get_param_paths(&path, "lnet_lnd_timeout");
3315         if (rc < 0) {
3316                 l_errno = -errno;
3317                 snprintf(err_str, sizeof(err_str),
3318                          "\"cannot get LND timeout: %d\"", rc);
3319                 return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3320                                                "lnd_timeout", lnd_to, show_rc,
3321                                                err_rc, l_errno);
3322         }
3323
3324         fd = open(path.gl_pathv[0], O_RDONLY);
3325         if (fd < 0) {
3326                 l_errno = -errno;
3327                 snprintf(err_str, sizeof(err_str),
3328                          "\"error opening %s\"", path.gl_pathv[0]);
3329                 goto failed;
3330         }
3331
3332         rc = read(fd, val, sizeof(val));
3333         if (rc < 0)
3334                 l_errno = -errno;
3335
3336         close(fd);
3337
3338         if (rc < 0) {
3339                 snprintf(err_str, sizeof(err_str),
3340                          "\"error reading %s\"", path.gl_pathv[0]);
3341                 goto failed;
3342         }
3343
3344         lnd_to = atoi(val);
3345
3346 failed:
3347         cfs_free_param_data(&path);
3348
3349         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3350                                        "lnd_timeout", lnd_to, show_rc,
3351                                        err_rc, l_errno);
3352 }
3353
3354 int lustre_lnet_show_transaction_to(int seq_no, struct cYAML **show_rc,
3355                                     struct cYAML **err_rc)
3356 {
3357         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3358         char val[LNET_MAX_STR_LEN];
3359         int tto = -1, l_errno = 0;
3360         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3361
3362         rc = read_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
3363                              1, sizeof(val));
3364         if (rc) {
3365                 l_errno = -errno;
3366                 snprintf(err_str, sizeof(err_str),
3367                          "\"cannot get transaction timeout: %d\"", rc);
3368         } else {
3369                 tto = atoi(val);
3370         }
3371
3372         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3373                                        "transaction_timeout", tto, show_rc,
3374                                        err_rc, l_errno);
3375 }
3376
3377 int lustre_lnet_show_retry_count(int seq_no, struct cYAML **show_rc,
3378                                  struct cYAML **err_rc)
3379 {
3380         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3381         char val[LNET_MAX_STR_LEN];
3382         int retry_count = -1, l_errno = 0;
3383         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3384
3385         rc = read_sysfs_file(modparam_path, "lnet_retry_count", val,
3386                              1, sizeof(val));
3387         if (rc) {
3388                 l_errno = -errno;
3389                 snprintf(err_str, sizeof(err_str),
3390                          "\"cannot get retry count: %d\"", rc);
3391         } else {
3392                 retry_count = atoi(val);
3393         }
3394
3395         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3396                                        "retry_count", retry_count, show_rc,
3397                                        err_rc, l_errno);
3398 }
3399
3400 int lustre_lnet_calc_service_id(__u64 *service_id)
3401 {
3402         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3403         char val[LNET_MAX_STR_LEN];
3404         int service_port = -1, l_errno = 0;
3405
3406         rc = read_sysfs_file(o2ib_modparam_path, "service", val,
3407                              1, sizeof(val));
3408         if (rc) {
3409                 l_errno = errno;
3410                 fprintf(stderr, "error:\n    msg: \"cannot get service port: %s (%d)\"\n",
3411                         strerror(l_errno), -l_errno);
3412                 return rc;
3413         } else {
3414                 service_port = atoi(val);
3415         }
3416
3417         *service_id = htobe64(((__u64)RDMA_PS_TCP << 16) + service_port);
3418
3419         return LUSTRE_CFG_RC_NO_ERR;
3420 }
3421
3422 int show_recovery_queue(enum lnet_health_type type, char *name, int seq_no,
3423                         struct cYAML **show_rc, struct cYAML **err_rc)
3424 {
3425         struct lnet_ioctl_recovery_list nid_list;
3426         struct cYAML *root = NULL, *nids = NULL;
3427         int rc, i;
3428         char err_str[LNET_MAX_STR_LEN] = "failed to print recovery queue\n";
3429
3430         LIBCFS_IOC_INIT_V2(nid_list, rlst_hdr);
3431         nid_list.rlst_type = type;
3432
3433         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_RECOVERY_QUEUE, &nid_list);
3434         if (rc) {
3435                 rc = errno;
3436                 goto out;
3437         }
3438
3439         if (nid_list.rlst_num_nids == 0)
3440                 goto out;
3441
3442         root = cYAML_create_object(NULL, NULL);
3443         if (root == NULL)
3444                 goto out;
3445
3446         nids = cYAML_create_object(root, name);
3447         if (nids == NULL)
3448                 goto out;
3449
3450         rc = -EINVAL;
3451
3452         for (i = 0; i < nid_list.rlst_num_nids; i++) {
3453                 char nidenum[LNET_MAX_STR_LEN];
3454                 snprintf(nidenum, sizeof(nidenum), "nid-%d", i);
3455                 if (!cYAML_create_string(nids, nidenum,
3456                         libcfs_nid2str(nid_list.rlst_nid_array[i])))
3457                         goto out;
3458         }
3459
3460         snprintf(err_str, sizeof(err_str), "success\n");
3461
3462         rc = 0;
3463
3464 out:
3465         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3466                 cYAML_free_tree(root);
3467         } else if (show_rc != NULL && *show_rc != NULL) {
3468                 struct cYAML *show_node;
3469                 /* find the net node, if one doesn't exist
3470                  * then insert one.  Otherwise add to the one there
3471                  */
3472                 show_node = cYAML_get_object_item(*show_rc, name);
3473                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3474                         cYAML_insert_child(show_node, nids);
3475                         free(nids);
3476                         free(root);
3477                 } else if (show_node == NULL) {
3478                         cYAML_insert_sibling((*show_rc)->cy_child,
3479                                                 nids);
3480                         free(root);
3481                 } else {
3482                         cYAML_free_tree(root);
3483                 }
3484         } else {
3485                 *show_rc = root;
3486         }
3487
3488         cYAML_build_error(rc, seq_no, SHOW_CMD, name, err_str, err_rc);
3489
3490         return rc;
3491 }
3492
3493 int lustre_lnet_show_local_ni_recovq(int seq_no, struct cYAML **show_rc,
3494                                      struct cYAML **err_rc)
3495 {
3496         return show_recovery_queue(LNET_HEALTH_TYPE_LOCAL_NI, "local NI recovery",
3497                                    seq_no, show_rc, err_rc);
3498 }
3499
3500 int lustre_lnet_show_peer_ni_recovq(int seq_no, struct cYAML **show_rc,
3501                                     struct cYAML **err_rc)
3502 {
3503         return show_recovery_queue(LNET_HEALTH_TYPE_PEER_NI, "peer NI recovery",
3504                                    seq_no, show_rc, err_rc);
3505 }
3506
3507 int lustre_lnet_show_response_tracking(int seq_no, struct cYAML **show_rc,
3508                                        struct cYAML **err_rc)
3509 {
3510         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3511         char val[LNET_MAX_STR_LEN];
3512         int rsp_tracking = -1, l_errno = 0;
3513         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3514
3515         rc = read_sysfs_file(modparam_path, "lnet_response_tracking", val,
3516                              1, sizeof(val));
3517         if (rc) {
3518                 l_errno = -errno;
3519                 snprintf(err_str, sizeof(err_str),
3520                          "\"cannot get lnet_response_tracking value: %d\"", rc);
3521         } else {
3522                 rsp_tracking = atoi(val);
3523         }
3524
3525         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3526                                        "response_tracking", rsp_tracking,
3527                                        show_rc, err_rc, l_errno);
3528 }
3529
3530 int lustre_lnet_show_recovery_limit(int seq_no, struct cYAML **show_rc,
3531                                     struct cYAML **err_rc)
3532 {
3533         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3534         char val[LNET_MAX_STR_LEN];
3535         int recov_limit = -1, l_errno = 0;
3536         char err_str[LNET_MAX_STR_LEN];
3537
3538         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3539
3540         rc = read_sysfs_file(modparam_path, "lnet_recovery_limit", val,
3541                              1, sizeof(val));
3542         if (rc) {
3543                 l_errno = -errno;
3544                 snprintf(err_str, sizeof(err_str),
3545                          "\"cannot get lnet_recovery_limit value: %d\"", rc);
3546         } else {
3547                 recov_limit = atoi(val);
3548         }
3549
3550         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3551                                        "recovery_limit", recov_limit,
3552                                        show_rc, err_rc, l_errno);
3553 }
3554
3555 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
3556                               struct cYAML **err_rc)
3557 {
3558         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3559         char val[LNET_MAX_STR_LEN];
3560         int max_intf = -1, l_errno = 0;
3561         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3562
3563         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
3564                              1, sizeof(val));
3565         if (rc) {
3566                 l_errno = -errno;
3567                 snprintf(err_str, sizeof(err_str),
3568                          "\"cannot get max interfaces: %d\"", rc);
3569         } else {
3570                 max_intf = atoi(val);
3571         }
3572
3573         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3574                                        "max_intf", max_intf, show_rc,
3575                                        err_rc, l_errno);
3576 }
3577
3578 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3579                                struct cYAML **err_rc)
3580 {
3581         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3582         char val[LNET_MAX_STR_LEN];
3583         int discovery = -1, l_errno = 0;
3584         char err_str[LNET_MAX_STR_LEN]  = "\"out of memory\"";
3585
3586         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3587                              1, sizeof(val));
3588         if (rc) {
3589                 l_errno = -errno;
3590                 snprintf(err_str, sizeof(err_str),
3591                          "\"cannot get discovery setting: %d\"", rc);
3592         } else {
3593                 /*
3594                  * The kernel stores a discovery disabled value. User space
3595                  * shows whether discovery is enabled. So the value must be
3596                  * inverted.
3597                  */
3598                 discovery = !atoi(val);
3599         }
3600
3601         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3602                                        "discovery", discovery, show_rc,
3603                                        err_rc, l_errno);
3604 }
3605
3606 int lustre_lnet_show_drop_asym_route(int seq_no, struct cYAML **show_rc,
3607                                      struct cYAML **err_rc)
3608 {
3609         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3610         char val[LNET_MAX_STR_LEN];
3611         int drop_asym_route = -1, l_errno = 0;
3612         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3613
3614         rc = read_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
3615                              1, sizeof(val));
3616         if (rc) {
3617                 l_errno = -errno;
3618                 snprintf(err_str, sizeof(err_str),
3619                          "\"cannot get drop asym route setting: %d\"", rc);
3620         } else {
3621                 drop_asym_route = atoi(val);
3622         }
3623
3624         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3625                                        "drop_asym_route", drop_asym_route,
3626                                        show_rc, err_rc, l_errno);
3627 }
3628
3629 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3630                                 struct cYAML **err_rc)
3631 {
3632         return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3633                                         "numa_range", show_rc, err_rc);
3634 }
3635
3636 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3637                            struct cYAML **err_rc)
3638 {
3639         struct lnet_ioctl_lnet_stats data;
3640         struct lnet_counters *cntrs;
3641         int rc;
3642         int l_errno;
3643         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
3644         struct cYAML *root = NULL, *stats = NULL;
3645
3646         LIBCFS_IOC_INIT_V2(data, st_hdr);
3647
3648         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3649         if (rc) {
3650                 l_errno = errno;
3651                 snprintf(err_str,
3652                          sizeof(err_str),
3653                          "\"cannot get lnet statistics: %s\"",
3654                          strerror(l_errno));
3655                 rc = -l_errno;
3656                 goto out;
3657         }
3658
3659         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3660
3661         cntrs = &data.st_cntrs;
3662
3663         root = cYAML_create_object(NULL, NULL);
3664         if (!root)
3665                 goto out;
3666
3667         stats = cYAML_create_object(root, "statistics");
3668         if (!stats)
3669                 goto out;
3670
3671         if (!cYAML_create_number(stats, "msgs_alloc",
3672                                  cntrs->lct_common.lcc_msgs_alloc))
3673                 goto out;
3674
3675         if (!cYAML_create_number(stats, "msgs_max",
3676                                  cntrs->lct_common.lcc_msgs_max))
3677                 goto out;
3678
3679         if (!cYAML_create_number(stats, "rst_alloc",
3680                                  cntrs->lct_health.lch_rst_alloc))
3681                 goto out;
3682
3683         if (!cYAML_create_number(stats, "errors",
3684                                  cntrs->lct_common.lcc_errors))
3685                 goto out;
3686
3687         if (!cYAML_create_number(stats, "send_count",
3688                                  cntrs->lct_common.lcc_send_count))
3689                 goto out;
3690
3691         if (!cYAML_create_number(stats, "resend_count",
3692                                  cntrs->lct_health.lch_resend_count))
3693                 goto out;
3694
3695         if (!cYAML_create_number(stats, "response_timeout_count",
3696                                  cntrs->lct_health.lch_response_timeout_count))
3697                 goto out;
3698
3699         if (!cYAML_create_number(stats, "local_interrupt_count",
3700                                  cntrs->lct_health.lch_local_interrupt_count))
3701                 goto out;
3702
3703         if (!cYAML_create_number(stats, "local_dropped_count",
3704                                  cntrs->lct_health.lch_local_dropped_count))
3705                 goto out;
3706
3707         if (!cYAML_create_number(stats, "local_aborted_count",
3708                                  cntrs->lct_health.lch_local_aborted_count))
3709                 goto out;
3710
3711         if (!cYAML_create_number(stats, "local_no_route_count",
3712                                  cntrs->lct_health.lch_local_no_route_count))
3713                 goto out;
3714
3715         if (!cYAML_create_number(stats, "local_timeout_count",
3716                                  cntrs->lct_health.lch_local_timeout_count))
3717                 goto out;
3718
3719         if (!cYAML_create_number(stats, "local_error_count",
3720                                  cntrs->lct_health.lch_local_error_count))
3721                 goto out;
3722
3723         if (!cYAML_create_number(stats, "remote_dropped_count",
3724                                  cntrs->lct_health.lch_remote_dropped_count))
3725                 goto out;
3726
3727         if (!cYAML_create_number(stats, "remote_error_count",
3728                                  cntrs->lct_health.lch_remote_error_count))
3729                 goto out;
3730
3731         if (!cYAML_create_number(stats, "remote_timeout_count",
3732                                  cntrs->lct_health.lch_remote_timeout_count))
3733                 goto out;
3734
3735         if (!cYAML_create_number(stats, "network_timeout_count",
3736                                  cntrs->lct_health.lch_network_timeout_count))
3737                 goto out;
3738
3739         if (!cYAML_create_number(stats, "recv_count",
3740                                  cntrs->lct_common.lcc_recv_count))
3741                 goto out;
3742
3743         if (!cYAML_create_number(stats, "route_count",
3744                                  cntrs->lct_common.lcc_route_count))
3745                 goto out;
3746
3747         if (!cYAML_create_number(stats, "drop_count",
3748                                  cntrs->lct_common.lcc_drop_count))
3749                 goto out;
3750
3751         if (!cYAML_create_number(stats, "send_length",
3752                                  cntrs->lct_common.lcc_send_length))
3753                 goto out;
3754
3755         if (!cYAML_create_number(stats, "recv_length",
3756                                  cntrs->lct_common.lcc_recv_length))
3757                 goto out;
3758
3759         if (!cYAML_create_number(stats, "route_length",
3760                                  cntrs->lct_common.lcc_route_length))
3761                 goto out;
3762
3763         if (!cYAML_create_number(stats, "drop_length",
3764                                  cntrs->lct_common.lcc_drop_length))
3765                 goto out;
3766
3767         if (!show_rc)
3768                 cYAML_print_tree(root);
3769
3770         snprintf(err_str, sizeof(err_str), "\"success\"");
3771         rc = LUSTRE_CFG_RC_NO_ERR;
3772 out:
3773         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3774                 cYAML_free_tree(root);
3775         } else if (show_rc != NULL && *show_rc != NULL) {
3776                 cYAML_insert_sibling((*show_rc)->cy_child,
3777                                         root->cy_child);
3778                 free(root);
3779         } else {
3780                 *show_rc = root;
3781         }
3782
3783         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3784
3785         return rc;
3786 }
3787
3788 typedef int (*cmd_handler_t)(struct cYAML *tree,
3789                              struct cYAML **show_rc,
3790                              struct cYAML **err_rc);
3791
3792 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3793                                     struct cYAML **err_rc)
3794 {
3795         struct cYAML *net, *gw, *hop, *prio, *sen, *seq_no;
3796
3797         net = cYAML_get_object_item(tree, "net");
3798         gw = cYAML_get_object_item(tree, "gateway");
3799         hop = cYAML_get_object_item(tree, "hop");
3800         prio = cYAML_get_object_item(tree, "priority");
3801         sen = cYAML_get_object_item(tree, "health_sensitivity");
3802         seq_no = cYAML_get_object_item(tree, "seq_no");
3803
3804         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3805                                         (gw) ? gw->cy_valuestring : NULL,
3806                                         (hop) ? hop->cy_valueint : -1,
3807                                         (prio) ? prio->cy_valueint : -1,
3808                                         (sen) ? sen->cy_valueint : -1,
3809                                         (seq_no) ? seq_no->cy_valueint : -1,
3810                                         err_rc);
3811 }
3812
3813 /*
3814  *    interfaces:
3815  *        0: <intf_name>['['<expr>']']
3816  *        1: <intf_name>['['<expr>']']
3817  */
3818 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3819                                struct lnet_dlc_network_descr *nw_descr)
3820 {
3821         struct cYAML *child = NULL;
3822         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3823         struct lnet_dlc_intf_descr *intf_descr, *tmp;
3824
3825         if (intf_tree == NULL || nw_descr == NULL)
3826                 return LUSTRE_CFG_RC_BAD_PARAM;
3827
3828         /* now grab all the interfaces and their cpts */
3829         child = intf_tree->cy_child;
3830         while (child != NULL) {
3831                 if (child->cy_valuestring == NULL) {
3832                         child = child->cy_next;
3833                         continue;
3834                 }
3835
3836                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3837                         goto failed;
3838
3839                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3840                                                 child->cy_valuestring,
3841                                                 strlen(child->cy_valuestring));
3842                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3843                         goto failed;
3844
3845                 intf_num++;
3846                 child = child->cy_next;
3847         }
3848
3849         if (intf_num == 0)
3850                 return LUSTRE_CFG_RC_MISSING_PARAM;
3851
3852         return intf_num;
3853
3854 failed:
3855         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3856                                  intf_on_network) {
3857                 list_del(&intf_descr->intf_on_network);
3858                 free_intf_descr(intf_descr);
3859         }
3860
3861         return rc;
3862 }
3863
3864 static bool
3865 yaml_extract_cmn_tunables(struct cYAML *tree,
3866                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3867                           struct cfs_expr_list **global_cpts)
3868 {
3869         struct cYAML *tun, *item, *smp;
3870         int rc;
3871
3872         tun = cYAML_get_object_item(tree, "tunables");
3873         if (tun != NULL) {
3874                 item = cYAML_get_object_item(tun, "peer_timeout");
3875                 if (item != NULL)
3876                         tunables->lct_peer_timeout = item->cy_valueint;
3877                 item = cYAML_get_object_item(tun, "peer_credits");
3878                 if (item != NULL)
3879                         tunables->lct_peer_tx_credits = item->cy_valueint;
3880                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3881                 if (item != NULL)
3882                         tunables->lct_peer_rtr_credits = item->cy_valueint;
3883                 item = cYAML_get_object_item(tun, "credits");
3884                 if (item != NULL)
3885                         tunables->lct_max_tx_credits = item->cy_valueint;
3886                 smp = cYAML_get_object_item(tun, "CPT");
3887                 if (smp != NULL) {
3888                         rc = cfs_expr_list_parse(smp->cy_valuestring,
3889                                                  strlen(smp->cy_valuestring),
3890                                                  0, UINT_MAX, global_cpts);
3891                         if (rc != 0)
3892                                 *global_cpts = NULL;
3893                 }
3894
3895                 return true;
3896         }
3897
3898         return false;
3899 }
3900
3901 static bool
3902 yaml_extract_tunables(struct cYAML *tree,
3903                       struct lnet_ioctl_config_lnd_tunables *tunables,
3904                       struct cfs_expr_list **global_cpts,
3905                       __u32 net_type)
3906 {
3907         bool rc;
3908
3909         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3910                                        global_cpts);
3911
3912         if (!rc)
3913                 return rc;
3914
3915         lustre_yaml_extract_lnd_tunables(tree, net_type,
3916                                          &tunables->lt_tun);
3917
3918         return rc;
3919 }
3920
3921 /*
3922  * net:
3923  *    - net type: <net>[<NUM>]
3924   *      local NI(s):
3925  *        - nid: <ip>@<net>[<NUM>]
3926  *          status: up
3927  *          interfaces:
3928  *               0: <intf_name>['['<expr>']']
3929  *               1: <intf_name>['['<expr>']']
3930  *        tunables:
3931  *               peer_timeout: <NUM>
3932  *               peer_credits: <NUM>
3933  *               peer_buffer_credits: <NUM>
3934  *               credits: <NUM>
3935 *         lnd tunables:
3936  *               peercredits_hiw: <NUM>
3937  *               map_on_demand: <NUM>
3938  *               concurrent_sends: <NUM>
3939  *               fmr_pool_size: <NUM>
3940  *               fmr_flush_trigger: <NUM>
3941  *               fmr_cache: <NUM>
3942  *
3943  * At least one interface is required. If no interfaces are provided the
3944  * network interface can not be configured.
3945  */
3946 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3947                                  struct cYAML **err_rc)
3948 {
3949         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3950                      *item = NULL;
3951         int num_entries = 0, rc;
3952         struct lnet_dlc_network_descr nw_descr;
3953         struct cfs_expr_list *global_cpts = NULL;
3954         struct lnet_ioctl_config_lnd_tunables tunables;
3955         bool found = false;
3956
3957         memset(&tunables, 0, sizeof(tunables));
3958
3959         INIT_LIST_HEAD(&nw_descr.network_on_rule);
3960         INIT_LIST_HEAD(&nw_descr.nw_intflist);
3961
3962         ip2net = cYAML_get_object_item(tree, "ip2net");
3963         net = cYAML_get_object_item(tree, "net type");
3964         if (net)
3965                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3966         else
3967                 nw_descr.nw_id = LOLND;
3968
3969         /*
3970          * if neither net nor ip2nets are present, then we can not
3971          * configure the network.
3972          */
3973         if (!net && !ip2net)
3974                 return LUSTRE_CFG_RC_MISSING_PARAM;
3975
3976         local_nis = cYAML_get_object_item(tree, "local NI(s)");
3977         if (local_nis == NULL)
3978                 return LUSTRE_CFG_RC_MISSING_PARAM;
3979
3980         if (!cYAML_is_sequence(local_nis))
3981                 return LUSTRE_CFG_RC_BAD_PARAM;
3982
3983         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3984                 intf = cYAML_get_object_item(item, "interfaces");
3985                 if (intf == NULL)
3986                         continue;
3987                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3988                 if (num_entries <= 0) {
3989                         cYAML_build_error(num_entries, -1, "ni", "add",
3990                                         "bad interface list",
3991                                         err_rc);
3992                         return LUSTRE_CFG_RC_BAD_PARAM;
3993                 }
3994         }
3995
3996         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3997                                       LNET_NETTYP(nw_descr.nw_id));
3998         seq_no = cYAML_get_object_item(tree, "seq_no");
3999
4000         rc = lustre_lnet_config_ni(&nw_descr,
4001                                    global_cpts,
4002                                    (ip2net) ? ip2net->cy_valuestring : NULL,
4003                                    (found) ? &tunables: NULL,
4004                                    (seq_no) ? seq_no->cy_valueint : -1,
4005                                    err_rc);
4006
4007         if (global_cpts != NULL)
4008                 cfs_expr_list_free(global_cpts);
4009
4010         return rc;
4011 }
4012
4013 /*
4014  * ip2nets:
4015  *  - net-spec: <tcp|o2ib|gni>[NUM]
4016  *    interfaces:
4017  *        0: <intf name>['['<expr>']']
4018  *        1: <intf name>['['<expr>']']
4019  *    ip-range:
4020  *        0: <expr.expr.expr.expr>
4021  *        1: <expr.expr.expr.expr>
4022  */
4023 static int handle_yaml_config_ip2nets(struct cYAML *tree,
4024                                       struct cYAML **show_rc,
4025                                       struct cYAML **err_rc)
4026 {
4027         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
4028                      *seq_no = NULL;
4029         struct lustre_lnet_ip2nets ip2nets;
4030         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
4031                                           *tmp = NULL;
4032         int rc = LUSTRE_CFG_RC_NO_ERR;
4033         struct cfs_expr_list *global_cpts = NULL;
4034         struct cfs_expr_list *el, *el_tmp;
4035         struct lnet_ioctl_config_lnd_tunables tunables;
4036         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
4037         bool found = false;
4038
4039         memset(&tunables, 0, sizeof(tunables));
4040
4041         /* initialize all lists */
4042         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
4043         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
4044         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
4045
4046         net = cYAML_get_object_item(tree, "net-spec");
4047         if (net == NULL)
4048                 return LUSTRE_CFG_RC_BAD_PARAM;
4049
4050         if (net != NULL && net->cy_valuestring == NULL)
4051                 return LUSTRE_CFG_RC_BAD_PARAM;
4052
4053         /* assign the network id */
4054         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
4055         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
4056                 return LUSTRE_CFG_RC_BAD_PARAM;
4057
4058         seq_no = cYAML_get_object_item(tree, "seq_no");
4059
4060         intf = cYAML_get_object_item(tree, "interfaces");
4061         if (intf != NULL) {
4062                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
4063                 if (rc <= 0)
4064                         return LUSTRE_CFG_RC_BAD_PARAM;
4065         }
4066
4067         ip_range = cYAML_get_object_item(tree, "ip-range");
4068         if (ip_range != NULL) {
4069                 item = ip_range->cy_child;
4070                 while (item != NULL) {
4071                         if (item->cy_valuestring == NULL) {
4072                                 item = item->cy_next;
4073                                 continue;
4074                         }
4075
4076                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
4077                                                       item->cy_valuestring);
4078
4079                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4080                                 goto out;
4081
4082                         item = item->cy_next;
4083                 }
4084         }
4085
4086         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
4087                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
4088
4089         rc = lustre_lnet_config_ip2nets(&ip2nets,
4090                         (found) ? &tunables : NULL,
4091                         global_cpts,
4092                         (seq_no) ? seq_no->cy_valueint : -1,
4093                         err_rc);
4094
4095         /*
4096          * don't stop because there was no match. Continue processing the
4097          * rest of the rules. If non-match then nothing is configured
4098          */
4099         if (rc == LUSTRE_CFG_RC_NO_MATCH)
4100                 rc = LUSTRE_CFG_RC_NO_ERR;
4101 out:
4102         list_for_each_entry_safe(intf_descr, intf_tmp,
4103                                  &ip2nets.ip2nets_net.nw_intflist,
4104                                  intf_on_network) {
4105                 list_del(&intf_descr->intf_on_network);
4106                 free_intf_descr(intf_descr);
4107         }
4108
4109         list_for_each_entry_safe(ip_range_descr, tmp,
4110                                  &ip2nets.ip2nets_ip_ranges,
4111                                  ipr_entry) {
4112                 list_del(&ip_range_descr->ipr_entry);
4113                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
4114                                          el_link) {
4115                         list_del(&el->el_link);
4116                         cfs_expr_list_free(el);
4117                 }
4118                 free(ip_range_descr);
4119         }
4120
4121         return rc;
4122 }
4123
4124 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
4125                               struct cYAML **err_rc)
4126 {
4127         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
4128                      *local_nis = NULL;
4129         int num_entries, rc;
4130         struct lnet_dlc_network_descr nw_descr;
4131
4132         INIT_LIST_HEAD(&nw_descr.network_on_rule);
4133         INIT_LIST_HEAD(&nw_descr.nw_intflist);
4134
4135         net = cYAML_get_object_item(tree, "net type");
4136         if (net != NULL)
4137                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
4138
4139         local_nis = cYAML_get_object_item(tree, "local NI(s)");
4140         if (local_nis == NULL)
4141                 return LUSTRE_CFG_RC_MISSING_PARAM;
4142
4143         if (!cYAML_is_sequence(local_nis))
4144                 return LUSTRE_CFG_RC_BAD_PARAM;
4145
4146         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
4147                 intf = cYAML_get_object_item(item, "interfaces");
4148                 if (intf == NULL)
4149                         continue;
4150                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
4151                 if (num_entries <= 0) {
4152                         cYAML_build_error(num_entries, -1, "ni", "add",
4153                                         "bad interface list",
4154                                         err_rc);
4155                         return LUSTRE_CFG_RC_BAD_PARAM;
4156                 }
4157         }
4158
4159         seq_no = cYAML_get_object_item(tree, "seq_no");
4160
4161         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
4162                                 (seq_no) ? seq_no->cy_valueint : -1,
4163                                 err_rc);
4164
4165         return rc;
4166 }
4167
4168 /* Create a nidstring parseable by the nidstrings library from the nid
4169  * information encoded in the CYAML structure.
4170  * NOTE: Caller must free memory allocated to nidstr
4171  */
4172 static int yaml_nids2nidstr(struct cYAML *nids_entry, char **nidstr,
4173                             char *prim_nid, int cmd)
4174 {
4175         int num_strs = 0, rc;
4176         size_t buf_size, buf_pos, nidstr_len = 0;
4177         char *buffer;
4178         struct cYAML *child = NULL, *entry = NULL;
4179
4180         if (cYAML_is_sequence(nids_entry)) {
4181                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
4182                         entry = cYAML_get_object_item(child, "nid");
4183                         /* don't count an empty entry */
4184                         if (!entry || !entry->cy_valuestring)
4185                                 continue;
4186
4187                         if (prim_nid &&
4188                             (strcmp(entry->cy_valuestring, prim_nid) == 0)) {
4189                                 if (cmd == LNETCTL_DEL_CMD) {
4190                                         /*
4191                                          * primary nid is present in the list of
4192                                          * nids so that means we want to delete
4193                                          * the entire peer, so no need to go
4194                                          * further. Just delete the entire peer.
4195                                          */
4196                                         return LUSTRE_CFG_RC_NO_ERR;
4197                                 } else {
4198                                         continue;
4199                                 }
4200                         }
4201
4202                         /*
4203                          * + 1 for the space separating each string, and
4204                          * accounts for the terminating null char
4205                          */
4206                         nidstr_len += strlen(entry->cy_valuestring) + 1;
4207                         num_strs++;
4208                 }
4209         }
4210
4211         if (num_strs == 0 && !prim_nid)
4212                 return LUSTRE_CFG_RC_MISSING_PARAM;
4213         else if (num_strs == 0) /* Only the primary nid was given to add/del */
4214                 return LUSTRE_CFG_RC_NO_ERR;
4215
4216         buffer = malloc(nidstr_len);
4217         if (!buffer)
4218                 return LUSTRE_CFG_RC_OUT_OF_MEM;
4219
4220         /* now grab all the nids */
4221         rc = 0;
4222         buf_pos = 0;
4223         buf_size = nidstr_len;
4224         child = NULL;
4225         while (cYAML_get_next_seq_item(nids_entry, &child)) {
4226                 entry = cYAML_get_object_item(child, "nid");
4227                 if (!entry || !entry->cy_valuestring)
4228                         continue;
4229
4230                 if (prim_nid &&
4231                     (strcmp(entry->cy_valuestring, prim_nid) == 0))
4232                         continue;
4233
4234                 if (buf_pos) {
4235                         rc = snprintf(buffer + buf_pos, buf_size, " ");
4236                         buf_pos += (rc < buf_size) ? rc : buf_size;
4237                         buf_size = nidstr_len - buf_pos;
4238                 }
4239
4240                 rc = snprintf(buffer + buf_pos, buf_size, "%s",
4241                               entry->cy_valuestring);
4242                 buf_pos += (rc < buf_size) ? rc : buf_size;
4243                 buf_size = nidstr_len - buf_pos;
4244         }
4245
4246         *nidstr = buffer;
4247
4248         return LUSTRE_CFG_RC_NO_ERR;
4249 }
4250
4251 static int handle_yaml_peer_common(struct cYAML *tree, struct cYAML **show_rc,
4252                                    struct cYAML **err_rc, int cmd)
4253 {
4254         int rc, num_nids = 0, seqn;
4255         bool mr_value = false;
4256         char *nidstr = NULL, *prim_nidstr;
4257         char err_str[LNET_MAX_STR_LEN];
4258         struct cYAML *seq_no, *prim_nid, *mr, *peer_nis;
4259         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
4260         lnet_nid_t pnid = LNET_NID_ANY;
4261
4262         seq_no = cYAML_get_object_item(tree, "seq_no");
4263         seqn = seq_no ? seq_no->cy_valueint : -1;
4264
4265         prim_nid = cYAML_get_object_item(tree, "primary nid");
4266         peer_nis = cYAML_get_object_item(tree, "peer ni");
4267         if (!prim_nid) {
4268                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4269                 snprintf(err_str, LNET_MAX_STR_LEN,
4270                          "\"primary nid\" must be specified");
4271                 goto failed;
4272         }
4273
4274         prim_nidstr = prim_nid->cy_valuestring;
4275
4276         /* if the provided primary NID is bad, no need to go any further */
4277         pnid = libcfs_str2nid(prim_nidstr);
4278         if (pnid == LNET_NID_ANY) {
4279                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4280                 snprintf(err_str, LNET_MAX_STR_LEN,
4281                         "badly formatted primary NID: %s", prim_nidstr);
4282                 goto failed;
4283         }
4284
4285         rc = yaml_nids2nidstr(peer_nis, &nidstr, prim_nidstr, cmd);
4286         if (rc == LUSTRE_CFG_RC_MISSING_PARAM) {
4287                 snprintf(err_str, LNET_MAX_STR_LEN,
4288                          "No nids defined in YAML block");
4289                 goto failed;
4290         } else if (rc == LUSTRE_CFG_RC_OUT_OF_MEM) {
4291                 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
4292                 goto failed;
4293         } else if (rc != LUSTRE_CFG_RC_NO_ERR) {
4294                 snprintf(err_str, LNET_MAX_STR_LEN,
4295                          "Unrecognized error %d", rc);
4296                 goto failed;
4297         }
4298
4299         num_nids = 0;
4300         if (nidstr) {
4301                 num_nids = lustre_lnet_parse_nidstr(nidstr, lnet_nidlist,
4302                                                     LNET_MAX_NIDS_PER_PEER,
4303                                                     err_str);
4304                 if (num_nids < 0) {
4305                         rc = num_nids;
4306                         goto failed;
4307                 }
4308         }
4309
4310         if (cmd == LNETCTL_ADD_CMD) {
4311                 mr = cYAML_get_object_item(tree, "Multi-Rail");
4312                 mr_value = true;
4313                 if (mr && mr->cy_valuestring) {
4314                         if (strcmp(mr->cy_valuestring, "False") == 0)
4315                                 mr_value = false;
4316                         else if (strcmp(mr->cy_valuestring, "True") != 0) {
4317                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4318                                 snprintf(err_str, LNET_MAX_STR_LEN,
4319                                          "Multi-Rail must be set to \"True\" or \"False\" found \"%s\"",
4320                                          mr->cy_valuestring);
4321                                 goto failed;
4322                         }
4323                 }
4324         }
4325
4326         rc = lustre_lnet_mod_peer_nidlist(pnid, lnet_nidlist, cmd,
4327                                           num_nids, mr_value, seqn,
4328                                           err_rc);
4329
4330 failed:
4331         if (nidstr)
4332                 free(nidstr);
4333
4334         if (rc != LUSTRE_CFG_RC_NO_ERR)
4335                 cYAML_build_error(rc, seqn, "peer",
4336                                   cmd == LNETCTL_ADD_CMD ? ADD_CMD : DEL_CMD,
4337                                   err_str, err_rc);
4338
4339         return rc;
4340 }
4341
4342 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
4343                                    struct cYAML **err_rc)
4344 {
4345         return handle_yaml_peer_common(tree, show_rc, err_rc, LNETCTL_ADD_CMD);
4346 }
4347
4348 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
4349                                 struct cYAML **err_rc)
4350 {
4351         return handle_yaml_peer_common(tree, show_rc, err_rc, LNETCTL_DEL_CMD);
4352 }
4353
4354 static int handle_yaml_config_buffers(struct cYAML *tree,
4355                                       struct cYAML **show_rc,
4356                                       struct cYAML **err_rc)
4357 {
4358         int rc;
4359         struct cYAML *tiny, *small, *large, *seq_no;
4360
4361         tiny = cYAML_get_object_item(tree, "tiny");
4362         small = cYAML_get_object_item(tree, "small");
4363         large = cYAML_get_object_item(tree, "large");
4364         seq_no = cYAML_get_object_item(tree, "seq_no");
4365
4366         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
4367                                         (small) ? small->cy_valueint : -1,
4368                                         (large) ? large->cy_valueint : -1,
4369                                         (seq_no) ? seq_no->cy_valueint : -1,
4370                                         err_rc);
4371
4372         return rc;
4373 }
4374
4375 static int handle_yaml_config_routing(struct cYAML *tree,
4376                                       struct cYAML **show_rc,
4377                                       struct cYAML **err_rc)
4378 {
4379         int rc = LUSTRE_CFG_RC_NO_ERR;
4380         struct cYAML *seq_no, *enable;
4381
4382         seq_no = cYAML_get_object_item(tree, "seq_no");
4383         enable = cYAML_get_object_item(tree, "enable");
4384
4385         if (enable) {
4386                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
4387                                                 (seq_no) ?
4388                                                     seq_no->cy_valueint : -1,
4389                                                 err_rc);
4390         }
4391
4392         return rc;
4393 }
4394
4395 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
4396                                  struct cYAML **err_rc)
4397 {
4398         struct cYAML *net;
4399         struct cYAML *gw;
4400         struct cYAML *seq_no;
4401
4402         net = cYAML_get_object_item(tree, "net");
4403         gw = cYAML_get_object_item(tree, "gateway");
4404         seq_no = cYAML_get_object_item(tree, "seq_no");
4405
4406         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
4407                                      (gw) ? gw->cy_valuestring : NULL,
4408                                      (seq_no) ? seq_no->cy_valueint : -1,
4409                                      err_rc);
4410 }
4411
4412 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
4413                                    struct cYAML **err_rc)
4414 {
4415         struct cYAML *seq_no;
4416
4417         seq_no = cYAML_get_object_item(tree, "seq_no");
4418
4419         return lustre_lnet_enable_routing(0, (seq_no) ?
4420                                                 seq_no->cy_valueint : -1,
4421                                         err_rc);
4422 }
4423
4424 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
4425                                   struct cYAML **err_rc)
4426 {
4427         struct cYAML *net;
4428         struct cYAML *gw;
4429         struct cYAML *hop;
4430         struct cYAML *prio;
4431         struct cYAML *detail;
4432         struct cYAML *seq_no;
4433
4434         net = cYAML_get_object_item(tree, "net");
4435         gw = cYAML_get_object_item(tree, "gateway");
4436         hop = cYAML_get_object_item(tree, "hop");
4437         prio = cYAML_get_object_item(tree, "priority");
4438         detail = cYAML_get_object_item(tree, "detail");
4439         seq_no = cYAML_get_object_item(tree, "seq_no");
4440
4441         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
4442                                       (gw) ? gw->cy_valuestring : NULL,
4443                                       (hop) ? hop->cy_valueint : -1,
4444                                       (prio) ? prio->cy_valueint : -1,
4445                                       (detail) ? detail->cy_valueint : 0,
4446                                       (seq_no) ? seq_no->cy_valueint : -1,
4447                                       show_rc, err_rc, false);
4448 }
4449
4450 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
4451                                 struct cYAML **err_rc)
4452 {
4453         struct cYAML *net, *detail, *seq_no;
4454
4455         net = cYAML_get_object_item(tree, "net");
4456         detail = cYAML_get_object_item(tree, "detail");
4457         seq_no = cYAML_get_object_item(tree, "seq_no");
4458
4459         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
4460                                     (detail) ? detail->cy_valueint : 0,
4461                                     (seq_no) ? seq_no->cy_valueint : -1,
4462                                     show_rc, err_rc, false);
4463 }
4464
4465 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
4466                                     struct cYAML **err_rc)
4467 {
4468         struct cYAML *seq_no;
4469
4470         seq_no = cYAML_get_object_item(tree, "seq_no");
4471
4472         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
4473                                         show_rc, err_rc, false);
4474 }
4475
4476 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
4477                                   struct cYAML **err_rc)
4478 {
4479         struct cYAML *seq_no, *nid, *detail;
4480
4481         seq_no = cYAML_get_object_item(tree, "seq_no");
4482         detail = cYAML_get_object_item(tree, "detail");
4483         nid = cYAML_get_object_item(tree, "nid");
4484
4485         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
4486                                      (detail) ? detail->cy_valueint : 0,
4487                                      (seq_no) ? seq_no->cy_valueint : -1,
4488                                      show_rc, err_rc, false);
4489 }
4490
4491 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
4492                                   struct cYAML **err_rc)
4493 {
4494         struct cYAML *seq_no;
4495
4496         seq_no = cYAML_get_object_item(tree, "seq_no");
4497
4498         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
4499                                       show_rc, err_rc);
4500 }
4501
4502 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
4503                                   struct cYAML **err_rc)
4504 {
4505         struct cYAML *seq_no, *range;
4506
4507         seq_no = cYAML_get_object_item(tree, "seq_no");
4508         range = cYAML_get_object_item(tree, "range");
4509
4510         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
4511                                              seq_no ? seq_no->cy_valueint : -1,
4512                                              err_rc);
4513 }
4514
4515 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
4516                                struct cYAML **err_rc)
4517 {
4518         struct cYAML *seq_no;
4519
4520         seq_no = cYAML_get_object_item(tree, "seq_no");
4521
4522         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
4523                                              err_rc);
4524 }
4525
4526 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
4527                                 struct cYAML **err_rc)
4528 {
4529         struct cYAML *seq_no;
4530
4531         seq_no = cYAML_get_object_item(tree, "seq_no");
4532
4533         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
4534                                            show_rc, err_rc);
4535 }
4536
4537 static int handle_yaml_config_udsp(struct cYAML *tree, struct cYAML **show_rc,
4538                                    struct cYAML **err_rc)
4539 {
4540         struct cYAML *seq_no, *src, *rte, *dst, *prio, *idx;
4541         union lnet_udsp_action action;
4542
4543         seq_no = cYAML_get_object_item(tree, "seq_no");
4544         src = cYAML_get_object_item(tree, "src");
4545         rte = cYAML_get_object_item(tree, "rte");
4546         dst = cYAML_get_object_item(tree, "dst");
4547         prio = cYAML_get_object_item(tree, "priority");
4548         idx = cYAML_get_object_item(tree, "idx");
4549
4550         action.udsp_priority = prio ? prio->cy_valueint : -1;
4551
4552         return lustre_lnet_add_udsp(src ? src->cy_valuestring : NULL,
4553                                     dst ? dst->cy_valuestring : NULL,
4554                                     rte ? rte->cy_valuestring : NULL,
4555                                     prio ? "priority" : "",
4556                                     &action,
4557                                     idx ? idx->cy_valueint : -1,
4558                                     seq_no ? seq_no->cy_valueint : -1,
4559                                     err_rc);
4560 }
4561
4562 static int handle_yaml_config_global_settings(struct cYAML *tree,
4563                                               struct cYAML **show_rc,
4564                                               struct cYAML **err_rc)
4565 {
4566         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4567                      *sen, *recov, *rsen, *drop_asym_route, *rsp_tracking,
4568                      *recov_limit;
4569         int rc = 0;
4570
4571         seq_no = cYAML_get_object_item(tree, "seq_no");
4572         max_intf = cYAML_get_object_item(tree, "max_intf");
4573         if (max_intf)
4574                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
4575                                                  seq_no ? seq_no->cy_valueint
4576                                                         : -1,
4577                                                  err_rc);
4578
4579         numa = cYAML_get_object_item(tree, "numa_range");
4580         if (numa)
4581                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
4582                                                    seq_no ? seq_no->cy_valueint
4583                                                         : -1,
4584                                                    err_rc);
4585
4586         discovery = cYAML_get_object_item(tree, "discovery");
4587         if (discovery)
4588                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
4589                                                   seq_no ? seq_no->cy_valueint
4590                                                         : -1,
4591                                                   err_rc);
4592
4593         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4594         if (drop_asym_route)
4595                 rc = lustre_lnet_config_drop_asym_route(
4596                         drop_asym_route->cy_valueint,
4597                         seq_no ? seq_no->cy_valueint : -1,
4598                         err_rc);
4599
4600         retry = cYAML_get_object_item(tree, "retry_count");
4601         if (retry)
4602                 rc = lustre_lnet_config_retry_count(retry->cy_valueint,
4603                                                     seq_no ? seq_no->cy_valueint
4604                                                         : -1,
4605                                                     err_rc);
4606
4607         tto = cYAML_get_object_item(tree, "transaction_timeout");
4608         if (tto)
4609                 rc = lustre_lnet_config_transaction_to(tto->cy_valueint,
4610                                                        seq_no ? seq_no->cy_valueint
4611                                                                 : -1,
4612                                                        err_rc);
4613
4614         sen = cYAML_get_object_item(tree, "health_sensitivity");
4615         if (sen)
4616                 rc = lustre_lnet_config_hsensitivity(sen->cy_valueint,
4617                                                      seq_no ? seq_no->cy_valueint
4618                                                         : -1,
4619                                                      err_rc);
4620
4621         recov = cYAML_get_object_item(tree, "recovery_interval");
4622         if (recov)
4623                 rc = lustre_lnet_config_recov_intrv(recov->cy_valueint,
4624                                                     seq_no ? seq_no->cy_valueint
4625                                                         : -1,
4626                                                     err_rc);
4627
4628         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4629         if (rsen)
4630                 rc = lustre_lnet_config_rtr_sensitivity(rsen->cy_valueint,
4631                                                      seq_no ? seq_no->cy_valueint
4632                                                         : -1,
4633                                                      err_rc);
4634
4635         rsp_tracking = cYAML_get_object_item(tree, "response_tracking");
4636         if (rsp_tracking)
4637                 rc = lustre_lnet_config_response_tracking(rsp_tracking->cy_valueint,
4638                                                      seq_no ? seq_no->cy_valueint
4639                                                         : -1,
4640                                                      err_rc);
4641
4642         recov_limit = cYAML_get_object_item(tree, "recovery_limit");
4643         if (recov_limit)
4644                 rc = lustre_lnet_config_recovery_limit(recov_limit->cy_valueint,
4645                                                        seq_no ? seq_no->cy_valueint
4646                                                         : -1,
4647                                                        err_rc);
4648
4649         return rc;
4650 }
4651
4652 static int handle_yaml_del_global_settings(struct cYAML *tree,
4653                                            struct cYAML **show_rc,
4654                                            struct cYAML **err_rc)
4655 {
4656         struct cYAML *max_intf, *numa, *discovery, *seq_no, *drop_asym_route;
4657         int rc = 0;
4658
4659         seq_no = cYAML_get_object_item(tree, "seq_no");
4660         max_intf = cYAML_get_object_item(tree, "max_intf");
4661         if (max_intf)
4662                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
4663                                                  seq_no ? seq_no->cy_valueint
4664                                                         : -1,
4665                                                  err_rc);
4666
4667         numa = cYAML_get_object_item(tree, "numa_range");
4668         if (numa)
4669                 rc = lustre_lnet_config_numa_range(0,
4670                                                    seq_no ? seq_no->cy_valueint
4671                                                         : -1,
4672                                                    err_rc);
4673
4674         /* peer discovery is enabled by default */
4675         discovery = cYAML_get_object_item(tree, "discovery");
4676         if (discovery)
4677                 rc = lustre_lnet_config_discovery(1,
4678                                                   seq_no ? seq_no->cy_valueint
4679                                                         : -1,
4680                                                   err_rc);
4681
4682         /* asymmetrical route messages are accepted by default */
4683         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4684         if (drop_asym_route)
4685                 rc = lustre_lnet_config_drop_asym_route(
4686                         0, seq_no ? seq_no->cy_valueint : -1, err_rc);
4687
4688         return rc;
4689 }
4690
4691 static int handle_yaml_show_global_settings(struct cYAML *tree,
4692                                             struct cYAML **show_rc,
4693                                             struct cYAML **err_rc)
4694 {
4695         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4696                      *sen, *recov, *rsen, *drop_asym_route, *rsp_tracking,
4697                      *recov_limit;
4698         int rc = 0;
4699
4700         seq_no = cYAML_get_object_item(tree, "seq_no");
4701         max_intf = cYAML_get_object_item(tree, "max_intf");
4702         if (max_intf)
4703                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
4704                                                         : -1,
4705                                                 show_rc, err_rc);
4706
4707         numa = cYAML_get_object_item(tree, "numa_range");
4708         if (numa)
4709                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
4710                                                         : -1,
4711                                                  show_rc, err_rc);
4712
4713         discovery = cYAML_get_object_item(tree, "discovery");
4714         if (discovery)
4715                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
4716                                                         : -1,
4717                                                 show_rc, err_rc);
4718
4719         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4720         if (drop_asym_route)
4721                 rc = lustre_lnet_show_drop_asym_route(
4722                         seq_no ? seq_no->cy_valueint : -1,
4723                         show_rc, err_rc);
4724
4725         retry = cYAML_get_object_item(tree, "retry_count");
4726         if (retry)
4727                 rc = lustre_lnet_show_retry_count(seq_no ? seq_no->cy_valueint
4728                                                         : -1,
4729                                                   show_rc, err_rc);
4730
4731         tto = cYAML_get_object_item(tree, "transaction_timeout");
4732         if (tto)
4733                 rc = lustre_lnet_show_transaction_to(seq_no ? seq_no->cy_valueint
4734                                                         : -1,
4735                                                      show_rc, err_rc);
4736
4737         sen = cYAML_get_object_item(tree, "health_sensitivity");
4738         if (sen)
4739                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4740                                                         : -1,
4741                                                      show_rc, err_rc);
4742
4743         recov = cYAML_get_object_item(tree, "recovery_interval");
4744         if (recov)
4745                 rc = lustre_lnet_show_recov_intrv(seq_no ? seq_no->cy_valueint
4746                                                         : -1,
4747                                                   show_rc, err_rc);
4748
4749         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4750         if (rsen)
4751                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4752                                                         : -1,
4753                                                      show_rc, err_rc);
4754
4755         rsp_tracking = cYAML_get_object_item(tree, "response_tracking");
4756         if (rsp_tracking)
4757                 rc = lustre_lnet_show_response_tracking(seq_no ?
4758                                                         seq_no->cy_valueint :
4759                                                         -1,
4760                                                         show_rc, err_rc);
4761
4762         recov_limit = cYAML_get_object_item(tree, "recovery_limit");
4763         if (recov_limit)
4764                 rc = lustre_lnet_show_recovery_limit(seq_no ?
4765                                                      seq_no->cy_valueint :
4766                                                      -1,
4767                                                      show_rc, err_rc);
4768
4769         return rc;
4770 }
4771
4772 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
4773                             struct cYAML **err_rc)
4774 {
4775         struct cYAML *seq_no, *nid, *timeout;
4776
4777         seq_no = cYAML_get_object_item(tree, "seq_no");
4778         nid = cYAML_get_object_item(tree, "primary nid");
4779         timeout = cYAML_get_object_item(tree, "timeout");
4780
4781         return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
4782                                     (timeout) ? timeout->cy_valueint : 1000,
4783                                     (seq_no) ? seq_no->cy_valueint : -1,
4784                                     show_rc, err_rc);
4785 }
4786
4787 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
4788                                 struct cYAML **err_rc)
4789 {
4790         struct cYAML *seq_no, *nid, *force;
4791
4792         seq_no = cYAML_get_object_item(tree, "seq_no");
4793         nid = cYAML_get_object_item(tree, "primary nid");
4794         force = cYAML_get_object_item(tree, "force");
4795
4796         return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
4797                                         (force) ? force->cy_valueint : 0,
4798                                         (seq_no) ? seq_no->cy_valueint : -1,
4799                                         show_rc, err_rc);
4800 }
4801
4802 static int handle_yaml_no_op()
4803 {
4804         return LUSTRE_CFG_RC_NO_ERR;
4805 }
4806
4807 struct lookup_cmd_hdlr_tbl {
4808         char *name;
4809         cmd_handler_t cb;
4810 };
4811
4812 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
4813         { .name = "route",      .cb = handle_yaml_config_route },
4814         { .name = "net",        .cb = handle_yaml_config_ni },
4815         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
4816         { .name = "peer",       .cb = handle_yaml_config_peer },
4817         { .name = "routing",    .cb = handle_yaml_config_routing },
4818         { .name = "buffers",    .cb = handle_yaml_config_buffers },
4819         { .name = "statistics", .cb = handle_yaml_no_op },
4820         { .name = "global",     .cb = handle_yaml_config_global_settings},
4821         { .name = "numa",       .cb = handle_yaml_config_numa },
4822         { .name = "ping",       .cb = handle_yaml_no_op },
4823         { .name = "discover",   .cb = handle_yaml_no_op },
4824         { .name = "udsp",       .cb = handle_yaml_config_udsp },
4825         { .name = NULL } };
4826
4827 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
4828         { .name = "route",      .cb = handle_yaml_del_route },
4829         { .name = "net",        .cb = handle_yaml_del_ni },
4830         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4831         { .name = "peer",       .cb = handle_yaml_del_peer },
4832         { .name = "routing",    .cb = handle_yaml_del_routing },
4833         { .name = "buffers",    .cb = handle_yaml_no_op },
4834         { .name = "statistics", .cb = handle_yaml_no_op },
4835         { .name = "global",     .cb = handle_yaml_del_global_settings},
4836         { .name = "numa",       .cb = handle_yaml_del_numa },
4837         { .name = "ping",       .cb = handle_yaml_no_op },
4838         { .name = "discover",   .cb = handle_yaml_no_op },
4839         { .name = NULL } };
4840
4841 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
4842         { .name = "route",      .cb = handle_yaml_show_route },
4843         { .name = "net",        .cb = handle_yaml_show_net },
4844         { .name = "peer",       .cb = handle_yaml_show_peers },
4845         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4846         { .name = "routing",    .cb = handle_yaml_show_routing },
4847         { .name = "buffers",    .cb = handle_yaml_show_routing },
4848         { .name = "statistics", .cb = handle_yaml_show_stats },
4849         { .name = "global",     .cb = handle_yaml_show_global_settings},
4850         { .name = "numa",       .cb = handle_yaml_show_numa },
4851         { .name = "ping",       .cb = handle_yaml_no_op },
4852         { .name = "discover",   .cb = handle_yaml_no_op },
4853         { .name = NULL } };
4854
4855 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4856         { .name = "route",      .cb = handle_yaml_no_op },
4857         { .name = "net",        .cb = handle_yaml_no_op },
4858         { .name = "peer",       .cb = handle_yaml_no_op },
4859         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4860         { .name = "routing",    .cb = handle_yaml_no_op },
4861         { .name = "buffers",    .cb = handle_yaml_no_op },
4862         { .name = "statistics", .cb = handle_yaml_no_op },
4863         { .name = "global",     .cb = handle_yaml_no_op },
4864         { .name = "numa",       .cb = handle_yaml_no_op },
4865         { .name = "ping",       .cb = handle_yaml_ping },
4866         { .name = "discover",   .cb = handle_yaml_discover },
4867         { .name = NULL } };
4868
4869 static cmd_handler_t lookup_fn(char *key,
4870                                struct lookup_cmd_hdlr_tbl *tbl)
4871 {
4872         int i;
4873         if (key == NULL)
4874                 return NULL;
4875
4876         for (i = 0; tbl[i].name != NULL; i++) {
4877                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4878                         return tbl[i].cb;
4879         }
4880
4881         return NULL;
4882 }
4883
4884 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4885                                  struct cYAML **show_rc, struct cYAML **err_rc)
4886 {
4887         struct cYAML *tree, *item = NULL, *head, *child;
4888         cmd_handler_t cb;
4889         char err_str[LNET_MAX_STR_LEN];
4890         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4891
4892         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4893         if (tree == NULL)
4894                 return LUSTRE_CFG_RC_BAD_PARAM;
4895
4896         child = tree->cy_child;
4897         while (child != NULL) {
4898                 cb = lookup_fn(child->cy_string, table);
4899                 if (cb == NULL) {
4900                         snprintf(err_str, sizeof(err_str),
4901                                 "\"call back for '%s' not found\"",
4902                                 child->cy_string);
4903                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4904                                         "yaml", "helper", err_str, err_rc);
4905                         goto out;
4906                 }
4907
4908                 if (cYAML_is_sequence(child)) {
4909                         while ((head = cYAML_get_next_seq_item(child, &item))
4910                                != NULL) {
4911                                 rc = cb(head, show_rc, err_rc);
4912                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
4913                                         return_rc = rc;
4914                         }
4915                 } else {
4916                         rc = cb(child, show_rc, err_rc);
4917                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4918                                 return_rc = rc;
4919                 }
4920                 item = NULL;
4921                 child = child->cy_next;
4922         }
4923
4924 out:
4925         cYAML_free_tree(tree);
4926
4927         return return_rc;
4928 }
4929
4930 int lustre_yaml_config(char *f, struct cYAML **err_rc)
4931 {
4932         return lustre_yaml_cb_helper(f, lookup_config_tbl,
4933                                      NULL, err_rc);
4934 }
4935
4936 int lustre_yaml_del(char *f, struct cYAML **err_rc)
4937 {
4938         return lustre_yaml_cb_helper(f, lookup_del_tbl,
4939                                      NULL, err_rc);
4940 }
4941
4942 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4943 {
4944         return lustre_yaml_cb_helper(f, lookup_show_tbl,
4945                                      show_rc, err_rc);
4946 }
4947
4948 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4949 {
4950         return lustre_yaml_cb_helper(f, lookup_exec_tbl,
4951                                      show_rc, err_rc);
4952 }