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