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