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