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