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