Whamcloud - gitweb
da8424e4c0a88ca8232bcfe06a24ad92d109702b
[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 int tokenize_nidstr(char *nidstr, char *out[LNET_MAX_STR_LEN], char *err_str)
252 {
253         int bracket = 0, num_str = 0;
254         char *strstart, *chptr;
255
256         if (!nidstr) {
257                 snprintf(err_str, LNET_MAX_STR_LEN, "supplied nidstr is NULL");
258                 return LUSTRE_CFG_RC_BAD_PARAM;
259         }
260
261         /* comma-separated nidranges -> space-separated nidranges */
262         chptr = &nidstr[0];
263         strstart = chptr;
264         while (true) {
265                 if (*chptr == '\0') {
266                         out[num_str] = strstart;
267                         num_str++;
268                         break;
269                 }
270
271                 if (*chptr == '[')
272                         bracket++;
273                 else if (*chptr == ']')
274                         bracket--;
275
276                 if (bracket < 0) {
277                         snprintf(err_str, LNET_MAX_STR_LEN,
278                                  "Mismatched brackets in nidstring \"%s\"",
279                                  nidstr);
280                         return LUSTRE_CFG_RC_BAD_PARAM;
281                 }
282
283                 if (bracket == 0 && *chptr == ',') {
284                         out[num_str] = strstart;
285                         *chptr = '\0';
286                         num_str++;
287                         strstart = chptr + 1;
288                 }
289
290                 chptr++;
291         }
292
293         if (bracket != 0) {
294                 snprintf(err_str, LNET_MAX_STR_LEN,
295                          "Mismatched brackets in nidstring \"%s\"", nidstr);
296                 return LUSTRE_CFG_RC_BAD_PARAM;
297         }
298
299         return num_str;
300 }
301
302 int lustre_lnet_parse_nidstr(char *nidstr, lnet_nid_t *lnet_nidlist,
303                              int max_nids, char *err_str)
304 {
305         int rc, num_nids = 0;
306         struct list_head nidlist;
307
308         if (!nidstr) {
309                 snprintf(err_str, LNET_MAX_STR_LEN, "supplied nidstr is NULL");
310                 return LUSTRE_CFG_RC_BAD_PARAM;
311         }
312
313         if (strchr(nidstr, '*')) {
314                 snprintf(err_str, LNET_MAX_STR_LEN,
315                          "asterisk not allowed in nidstring \"%s\"", nidstr);
316                 return LUSTRE_CFG_RC_BAD_PARAM;
317         }
318
319         INIT_LIST_HEAD(&nidlist);
320         rc = cfs_parse_nidlist(nidstr, strlen(nidstr), &nidlist);
321         if (rc == 0) {
322                 snprintf(err_str, LNET_MAX_STR_LEN,
323                          "Unable to parse nidlist from: %s\n", nidstr);
324                 return LUSTRE_CFG_RC_BAD_PARAM;
325         }
326
327         if (list_empty(&nidlist)) {
328                 snprintf(err_str, LNET_MAX_STR_LEN,
329                          "\"%s\" does not specify any valid nid lists", nidstr);
330                 return LUSTRE_CFG_RC_BAD_PARAM;
331         }
332
333         num_nids = cfs_expand_nidlist(&nidlist, lnet_nidlist, max_nids);
334
335         if (num_nids == -1) {
336                 snprintf(err_str, LNET_MAX_STR_LEN,
337                          "\"%s\" specifies more than the %d NIDs allowed by this operation.",
338                          nidstr, max_nids);
339                 return LUSTRE_CFG_RC_BAD_PARAM;
340         }
341
342         if (num_nids < 0) {
343                 snprintf(err_str, LNET_MAX_STR_LEN,
344                          "Failed to expand nidstr: %s", strerror(num_nids));
345                 return LUSTRE_CFG_RC_OUT_OF_MEM;
346         }
347
348         if (num_nids == 0) {
349                 snprintf(err_str, LNET_MAX_STR_LEN,
350                          "\"%s\" did not expand to any nids", nidstr);
351                 return LUSTRE_CFG_RC_BAD_PARAM;
352         }
353
354         return num_nids;
355 }
356
357 /*
358  * format expected:
359  *      <intf>[<expr>], <intf>[<expr>],..
360  */
361 int lustre_lnet_parse_interfaces(char *intf_str,
362                                  struct lnet_dlc_network_descr *nw_descr)
363 {
364         char *open_square;
365         char *close_square;
366         char *comma;
367         char *cur = intf_str, *next = NULL;
368         char *end = intf_str + strlen(intf_str);
369         int rc, len;
370         struct lnet_dlc_intf_descr *intf_descr, *tmp;
371
372         if (nw_descr == NULL)
373                 return LUSTRE_CFG_RC_BAD_PARAM;
374
375         while (cur < end) {
376                 open_square = strchr(cur, '[');
377                 if (open_square != NULL) {
378                         close_square = strchr(cur, ']');
379                         if (close_square == NULL) {
380                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
381                                 goto failed;
382                         }
383
384                         comma = strchr(cur, ',');
385                         if (comma != NULL && comma > close_square) {
386                                 next = comma + 1;
387                                 len = next - close_square;
388                         } else {
389                                 len = strlen(cur);
390                                 next = cur + len;
391                         }
392                 } else {
393                         comma = strchr(cur, ',');
394                         if (comma != NULL) {
395                                 next = comma + 1;
396                                 len = comma - cur;
397                         } else {
398                                 len = strlen(cur);
399                                 next = cur + len;
400                         }
401                 }
402
403                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
404                 if (rc != LUSTRE_CFG_RC_NO_ERR)
405                         goto failed;
406
407                 cur = next;
408         }
409
410         return LUSTRE_CFG_RC_NO_ERR;
411
412 failed:
413         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
414                                  intf_on_network) {
415                 list_del(&intf_descr->intf_on_network);
416                 free_intf_descr(intf_descr);
417         }
418
419         return rc;
420 }
421
422 int lustre_lnet_config_lib_init(void)
423 {
424         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
425 }
426
427 void lustre_lnet_config_lib_uninit(void)
428 {
429         unregister_ioc_dev(LNET_DEV_ID);
430 }
431
432 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
433                                  int seq_no, struct cYAML **err_rc)
434 {
435         struct libcfs_ioctl_data data;
436         unsigned int opc;
437         int rc;
438         char err_str[LNET_MAX_STR_LEN];
439
440         snprintf(err_str, sizeof(err_str), "\"Success\"");
441
442         LIBCFS_IOC_INIT(data);
443
444         /* Reverse logic is used here in order not to change
445          * the lctl utility */
446         data.ioc_flags = load_ni_from_mod ? 0 : 1;
447
448         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
449
450         rc = l_ioctl(LNET_DEV_ID, opc, &data);
451         if (rc != 0) {
452                 snprintf(err_str,
453                         sizeof(err_str),
454                         "\"LNet %s error: %s\"", (up) ? "configure" :
455                         "unconfigure", strerror(errno));
456                 rc = -errno;
457         }
458
459         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
460                           "lnet", err_str, err_rc);
461
462         return rc;
463 }
464
465 static int dispatch_peer_ni_cmd(__u32 cmd, struct lnet_ioctl_peer_cfg *data,
466                                 char *err_str, char *cmd_str)
467 {
468         int rc;
469
470         rc = l_ioctl(LNET_DEV_ID, cmd, data);
471         if (rc) {
472                 rc = -errno;
473                 snprintf(err_str, LNET_MAX_STR_LEN,
474                          "\"%s peer ni operation failed: %s\"",
475                          cmd_str, strerror(errno));
476         }
477
478         return rc;
479 }
480
481 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
482                           int seq_no, struct cYAML **show_rc,
483                           struct cYAML **err_rc)
484 {
485         void *data = NULL;
486         struct lnet_ioctl_ping_data ping;
487         struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
488                      *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
489         struct lnet_process_id id;
490         char err_str[LNET_MAX_STR_LEN] = {0};
491         char *sep, *token, *end;
492         char buf[6];
493         size_t len;
494         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
495         int i;
496         bool flag = false;
497
498         len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
499
500         data = calloc(1, len);
501         if (data == NULL)
502                 goto out;
503
504         /* create struct cYAML root object */
505         root = cYAML_create_object(NULL, NULL);
506         if (root == NULL)
507                 goto out;
508
509         ping_node = cYAML_create_seq(root, oper);
510         if (ping_node == NULL)
511                 goto out;
512
513         /* tokenise each nid in string ping_nids */
514         token = strtok(ping_nids, ",");
515
516         do {
517                 item = cYAML_create_seq_item(ping_node);
518                 if (item == NULL)
519                         goto out;
520
521                 if (first_seq == NULL)
522                         first_seq = item;
523
524                 /* check if '-' is a part of NID, token */
525                 sep = strchr(token, '-');
526                 if (sep == NULL) {
527                         id.pid = LNET_PID_ANY;
528                         /* if no net is specified, libcfs_str2nid() will assume tcp */
529                         id.nid = libcfs_str2nid(token);
530                         if (id.nid == LNET_NID_ANY) {
531                                 snprintf(err_str, sizeof(err_str),
532                                          "\"cannot parse NID '%s'\"",
533                                          token);
534                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
535                                 cYAML_build_error(rc, seq_no, MANAGE_CMD,
536                                                   oper, err_str, err_rc);
537                                 continue;
538                         }
539                 } else {
540                         if (token[0] == 'u' || token[0] == 'U')
541                                 id.pid = (strtoul(&token[1], &end, 0) |
542                                           (LNET_PID_USERFLAG));
543                         else
544                                 id.pid = strtoul(token, &end, 0);
545
546                         /* assuming '-' is part of hostname */
547                         if (end != sep) {
548                                 id.pid = LNET_PID_ANY;
549                                 id.nid = libcfs_str2nid(token);
550                                 if (id.nid == LNET_NID_ANY) {
551                                         snprintf(err_str, sizeof(err_str),
552                                                  "\"cannot parse NID '%s'\"",
553                                                  token);
554                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
555                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
556                                                           oper, err_str,
557                                                           err_rc);
558                                         continue;
559                                 }
560                         } else {
561                                 id.nid = libcfs_str2nid(sep + 1);
562                                 if (id.nid == LNET_NID_ANY) {
563                                         snprintf(err_str, sizeof(err_str),
564                                                  "\"cannot parse NID '%s'\"",
565                                                  token);
566                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
567                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
568                                                           oper, err_str,
569                                                           err_rc);
570                                         continue;
571                                 }
572                         }
573                 }
574                 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
575                 ping.ping_hdr.ioc_len = sizeof(ping);
576                 ping.ping_id          = id;
577                 ping.op_param         = param;
578                 ping.ping_count       = LNET_INTERFACES_MAX_DEFAULT;
579                 ping.ping_buf         = data;
580
581                 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
582                 if (rc != 0) {
583                         snprintf(err_str,
584                                  sizeof(err_str), "failed to %s %s: %s\n", oper,
585                                  id.pid == LNET_PID_ANY ?
586                                  libcfs_nid2str(id.nid) :
587                                  libcfs_id2str(id), strerror(errno));
588                         rc = LUSTRE_CFG_RC_BAD_PARAM;
589                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
590                                           oper, err_str, err_rc);
591                         continue;
592                 }
593
594                 if (cYAML_create_string(item, "primary nid",
595                                         libcfs_nid2str(ping.ping_id.nid)) == NULL)
596                         goto out;
597
598                 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
599                                         "True" : "False") == NULL)
600                         goto out;
601
602                 tmp = cYAML_create_seq(item, "peer ni");
603                 if (tmp == NULL)
604                         goto out;
605
606                 for (i = 0; i < ping.ping_count; i++) {
607                         if (!strcmp(libcfs_nid2str(ping.ping_buf[i].nid),
608                                     "0@lo"))
609                                 continue;
610                         peer_ni = cYAML_create_seq_item(tmp);
611                         if (peer_ni == NULL)
612                                 goto out;
613                         memset(buf, 0, sizeof buf);
614                         snprintf(buf, sizeof buf, "nid");
615                         if (cYAML_create_string(peer_ni, buf,
616                                                 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
617                                 goto out;
618                 }
619
620                 flag = true;
621
622         } while ((token = strtok(NULL, ",")) != NULL);
623
624         if (flag)
625                 rc = LUSTRE_CFG_RC_NO_ERR;
626
627 out:
628         if (data)
629                 free(data);
630         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
631                 cYAML_free_tree(root);
632         } else if (show_rc != NULL && *show_rc != NULL) {
633                 struct cYAML *show_node;
634                 show_node = cYAML_get_object_item(*show_rc, oper);
635                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
636                         cYAML_insert_child(show_node, first_seq);
637                         free(ping_node);
638                         free(root);
639                 } else if (show_node == NULL) {
640                         cYAML_insert_sibling((*show_rc)->cy_child,
641                                              ping_node);
642                         free(root);
643                 } else {
644                         cYAML_free_tree(root);
645                 }
646         } else {
647                 *show_rc = root;
648         }
649
650         return rc;
651 }
652
653 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
654                          struct cYAML **show_rc, struct cYAML **err_rc)
655 {
656         int rc;
657
658         rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
659                             seq_no, show_rc, err_rc);
660         return rc;
661 }
662
663 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
664                          struct cYAML **show_rc, struct cYAML **err_rc)
665 {
666         int rc;
667
668         rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
669                             seq_no, show_rc, err_rc);
670         return rc;
671 }
672
673 static int lustre_lnet_handle_peer_nidlist(lnet_nid_t *nidlist, int num_nids,
674                                            bool is_mr, __u32 cmd,
675                                            char *cmd_type, char *err_str)
676 {
677         struct lnet_ioctl_peer_cfg data;
678         int rc, nid_idx;
679
680         if (cmd == IOC_LIBCFS_ADD_PEER_NI) {
681                 /* When adding a peer we first need to create the peer using the
682                  * specified (or implied) primary nid. Then we can add
683                  * additional nids to this peer using the primary nid as a key
684                  */
685                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
686                 data.prcfg_mr = is_mr;
687                 data.prcfg_prim_nid = nidlist[0];
688                 data.prcfg_cfg_nid = LNET_NID_ANY;
689
690                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
691
692                 if (rc)
693                         return rc;
694         }
695
696         /* Add or delete any specified NIs associated with the specified
697          * (or implied) primary nid
698          */
699         for (nid_idx = 1; nid_idx < num_nids; nid_idx++) {
700                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
701                 data.prcfg_mr = is_mr;
702                 data.prcfg_prim_nid = nidlist[0];
703                 data.prcfg_cfg_nid = nidlist[nid_idx];
704
705                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
706
707                 if (rc)
708                         return rc;
709         }
710
711         if (cmd == IOC_LIBCFS_DEL_PEER_NI && num_nids == 1) {
712                 /* In the delete case we may have been given just the
713                  * primary nid of the peer. This tells us to delete the peer
714                  * completely (rather than just delete some of its NIs)
715                  */
716                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
717                 data.prcfg_prim_nid = nidlist[0];
718                 data.prcfg_cfg_nid = LNET_NID_ANY;
719
720                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
721         }
722
723         return rc;
724 }
725
726 int lustre_lnet_config_peer_nidlist(char *pnidstr, lnet_nid_t *lnet_nidlist,
727                                     int num_nids, bool is_mr, int seq_no,
728                                     struct cYAML **err_rc)
729 {
730         int rc = LUSTRE_CFG_RC_NO_ERR;
731         char err_str[LNET_MAX_STR_LEN];
732         lnet_nid_t *lnet_nidlist2 = NULL;
733         lnet_nid_t pnid;
734
735         if (pnidstr) {
736                 pnid = libcfs_str2nid(pnidstr);
737                 if (pnid == LNET_NID_ANY) {
738                         snprintf(err_str, LNET_MAX_STR_LEN,
739                                  "bad primary NID: '%s'", pnidstr);
740                         rc = LUSTRE_CFG_RC_MISSING_PARAM;
741                         goto out;
742                 }
743
744                 num_nids++;
745
746                 lnet_nidlist2 = calloc(sizeof(*lnet_nidlist2), num_nids);
747                 if (!lnet_nidlist2) {
748                         snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
749                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
750                         goto out;
751                 }
752                 lnet_nidlist2[0] = pnid;
753                 memcpy(&lnet_nidlist2[1], lnet_nidlist, sizeof(*lnet_nidlist) *
754                                                     (num_nids - 1));
755         }
756
757         rc = lustre_lnet_handle_peer_nidlist((pnidstr) ? lnet_nidlist2 :
758                                                          lnet_nidlist,
759                                              num_nids, is_mr,
760                                              IOC_LIBCFS_ADD_PEER_NI, ADD_CMD,
761                                              err_str);
762 out:
763         if (lnet_nidlist2)
764                 free(lnet_nidlist2);
765
766         cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
767         return rc;
768 }
769
770 int lustre_lnet_del_peer_nidlist(char *pnidstr, lnet_nid_t *lnet_nidlist,
771                                  int num_nids, int seq_no,
772                                  struct cYAML **err_rc)
773 {
774         int rc = LUSTRE_CFG_RC_NO_ERR;
775         char err_str[LNET_MAX_STR_LEN];
776         lnet_nid_t *lnet_nidlist2 = NULL;
777         lnet_nid_t pnid;
778
779         if (!pnidstr) {
780                 snprintf(err_str, sizeof(err_str),
781                          "\"Primary nid is not provided\"");
782                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
783                 goto out;
784         }
785
786         pnid = libcfs_str2nid(pnidstr);
787         if (pnid == LNET_NID_ANY) {
788                 rc = LUSTRE_CFG_RC_BAD_PARAM;
789                 snprintf(err_str, sizeof(err_str),
790                          "bad key NID: '%s'",
791                          pnidstr);
792                 goto out;
793         }
794
795         num_nids++;
796         lnet_nidlist2 = calloc(sizeof(*lnet_nidlist2), num_nids);
797         if (!lnet_nidlist2) {
798                 snprintf(err_str, sizeof(err_str),
799                                 "out of memory");
800                 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
801                 goto out;
802         }
803         lnet_nidlist2[0] = pnid;
804         memcpy(&lnet_nidlist2[1], lnet_nidlist, sizeof(*lnet_nidlist) *
805                                                 (num_nids - 1));
806
807         rc = lustre_lnet_handle_peer_nidlist(lnet_nidlist2, num_nids, false,
808                                              IOC_LIBCFS_DEL_PEER_NI, DEL_CMD,
809                                              err_str);
810 out:
811         if (lnet_nidlist2)
812                 free(lnet_nidlist2);
813
814         cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
815         return rc;
816 }
817
818 int lustre_lnet_route_common(char *nw, char *nidstr, int hops, int prio,
819                              int sen, int seq_no, struct cYAML **err_rc,
820                              int cmd)
821 {
822         int rc, num_nids, num_nidstrs, nid_idx, str_idx;
823         __u32 rnet;
824         char err_str[LNET_MAX_STR_LEN];
825         char *nidstrarray[LNET_MAX_STR_LEN];
826         struct lnet_ioctl_config_data data;
827         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
828
829         if (nw == NULL || nidstr == NULL) {
830                 snprintf(err_str, LNET_MAX_STR_LEN,
831                          "\"missing mandatory parameter:'%s'\"",
832                          (nw == NULL && nidstr == NULL) ? "network, gateway" :
833                          (nw == NULL) ? "network" : "gateway");
834                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
835                 goto out;
836         }
837
838         rnet = libcfs_str2net(nw);
839         if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
840                 snprintf(err_str, LNET_MAX_STR_LEN,
841                          "\"cannot parse remote net %s\"", nw);
842                 rc = LUSTRE_CFG_RC_BAD_PARAM;
843                 goto out;
844         }
845
846         rc = tokenize_nidstr(nidstr, &nidstrarray[0], err_str);
847         if (rc < 0)
848                 goto out;
849
850         num_nidstrs = rc;
851
852         for (str_idx = 0; str_idx < num_nidstrs; str_idx++) {
853                 rc = lustre_lnet_parse_nidstr(nidstrarray[str_idx],
854                                               lnet_nidlist,
855                                               LNET_MAX_NIDS_PER_PEER, err_str);
856                 if (rc < 0)
857                         goto out;
858
859                 num_nids = rc;
860
861                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
862                 data.cfg_net = rnet;
863                 if (cmd == LNETCTL_ADD_CMD) {
864                         data.cfg_config_u.cfg_route.rtr_hop = hops;
865                         data.cfg_config_u.cfg_route.rtr_priority = prio;
866                         data.cfg_config_u.cfg_route.rtr_sensitivity = sen;
867                 }
868
869                 for (nid_idx = 0; nid_idx < num_nids; nid_idx++) {
870                         data.cfg_nid = lnet_nidlist[nid_idx];
871
872                         if (cmd == LNETCTL_ADD_CMD)
873                                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE,
874                                              &data);
875                         else
876                                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE,
877                                              &data);
878
879                         if (rc != 0 && errno != EEXIST &&
880                             errno != EHOSTUNREACH) {
881                                 rc = -errno;
882                                 snprintf(err_str, LNET_MAX_STR_LEN,
883                                          "route operation failed: %s",
884                                          strerror(errno));
885                                 goto out;
886                         }
887                 }
888         }
889
890 out:
891         cYAML_build_error(rc, seq_no,
892                           cmd == LNETCTL_ADD_CMD ? ADD_CMD : DEL_CMD, "route",
893                           err_str, err_rc);
894
895         return rc;
896 }
897
898 int lustre_lnet_config_route(char *nw, char *nidstr, int hops, int prio,
899                              int sen, int seq_no, struct cYAML **err_rc)
900 {
901         int rc;
902         char err_str[LNET_MAX_STR_LEN];
903
904         if (hops == -1) {
905                 hops = LNET_UNDEFINED_HOPS;
906         } else if (hops < 1 || hops > 255) {
907                 snprintf(err_str, LNET_MAX_STR_LEN,
908                          "\"invalid hop count %d, must be between 1 and 255\"",
909                          hops);
910                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
911                 goto out;
912         }
913
914         if (prio == -1) {
915                 prio = 0;
916         } else if (prio < 0) {
917                 snprintf(err_str, LNET_MAX_STR_LEN,
918                          "\"invalid priority %d, must be greater than 0\"",
919                          prio);
920                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
921                 goto out;
922         }
923
924         if (sen == -1) {
925                 sen = 1;
926         } else if (sen < 1) {
927                 snprintf(err_str, LNET_MAX_STR_LEN,
928                          "\"invalid health sensitivity %d, must be 1 or greater\"",
929                          sen);
930                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
931                 goto out;
932         }
933
934         rc = lustre_lnet_route_common(nw, nidstr, hops, prio, sen, seq_no,
935                                       err_rc, LNETCTL_ADD_CMD);
936 out:
937         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
938
939         return rc;
940 }
941
942 int lustre_lnet_del_route(char *nw, char *nidstr, int seq_no,
943                           struct cYAML **err_rc)
944 {
945         return lustre_lnet_route_common(nw, nidstr, 0, 0, 0, seq_no, err_rc,
946                                         LNETCTL_DEL_CMD);
947 }
948
949 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
950                            int seq_no, struct cYAML **show_rc,
951                            struct cYAML **err_rc, bool backup)
952 {
953         struct lnet_ioctl_config_data data;
954         lnet_nid_t gateway_nid;
955         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
956         int l_errno = 0;
957         __u32 net = LNET_NIDNET(LNET_NID_ANY);
958         int i;
959         struct cYAML *root = NULL, *route = NULL, *item = NULL;
960         struct cYAML *first_seq = NULL;
961         char err_str[LNET_MAX_STR_LEN];
962         bool exist = false;
963
964         snprintf(err_str, sizeof(err_str),
965                  "\"out of memory\"");
966
967         if (nw != NULL) {
968                 net = libcfs_str2net(nw);
969                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
970                         snprintf(err_str,
971                                  sizeof(err_str),
972                                  "\"cannot parse net '%s'\"", nw);
973                         rc = LUSTRE_CFG_RC_BAD_PARAM;
974                         goto out;
975                 }
976
977         } else {
978                 /* show all routes without filtering on net */
979                 net = LNET_NIDNET(LNET_NID_ANY);
980         }
981
982         if (gw != NULL) {
983                 gateway_nid = libcfs_str2nid(gw);
984                 if (gateway_nid == LNET_NID_ANY) {
985                         snprintf(err_str,
986                                  sizeof(err_str),
987                                  "\"cannot parse gateway NID '%s'\"", gw);
988                         rc = LUSTRE_CFG_RC_BAD_PARAM;
989                         goto out;
990                 }
991         } else
992                 /* show all routes with out filtering on gateway */
993                 gateway_nid = LNET_NID_ANY;
994
995         if ((hops < 1 && hops != -1) || hops > 255) {
996                 snprintf(err_str,
997                          sizeof(err_str),
998                          "\"invalid hop count %d, must be between 0 and 256\"",
999                          hops);
1000                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1001                 goto out;
1002         }
1003
1004         /* create struct cYAML root object */
1005         root = cYAML_create_object(NULL, NULL);
1006         if (root == NULL)
1007                 goto out;
1008
1009         route = cYAML_create_seq(root, "route");
1010         if (route == NULL)
1011                 goto out;
1012
1013         for (i = 0;; i++) {
1014                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1015                 data.cfg_count = i;
1016
1017                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1018                 if (rc != 0) {
1019                         l_errno = errno;
1020                         break;
1021                 }
1022
1023                 /* filter on provided data */
1024                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1025                     net != data.cfg_net)
1026                         continue;
1027
1028                 if (gateway_nid != LNET_NID_ANY &&
1029                     gateway_nid != data.cfg_nid)
1030                         continue;
1031
1032                 if (hops != -1 &&
1033                     hops != data.cfg_config_u.cfg_route.rtr_hop)
1034                         continue;
1035
1036                 if (prio != -1 &&
1037                     prio != data.cfg_config_u.cfg_route.rtr_priority)
1038                         continue;
1039
1040                 /* default rc to -1 incase we hit the goto */
1041                 rc = -1;
1042                 exist = true;
1043
1044                 item = cYAML_create_seq_item(route);
1045                 if (item == NULL)
1046                         goto out;
1047
1048                 if (first_seq == NULL)
1049                         first_seq = item;
1050
1051                 if (cYAML_create_string(item, "net",
1052                                         libcfs_net2str(data.cfg_net)) == NULL)
1053                         goto out;
1054
1055                 if (cYAML_create_string(item, "gateway",
1056                                         libcfs_nid2str(data.cfg_nid)) == NULL)
1057                         goto out;
1058
1059                 if (detail) {
1060                         if (cYAML_create_number(item, "hop",
1061                                                 (int) data.cfg_config_u.
1062                                                 cfg_route.rtr_hop) ==
1063                             NULL)
1064                                 goto out;
1065
1066                         if (cYAML_create_number(item, "priority",
1067                                                 data.cfg_config_u.
1068                                                 cfg_route.rtr_priority) == NULL)
1069                                 goto out;
1070
1071                         if (cYAML_create_number(item, "health_sensitivity",
1072                                                 data.cfg_config_u.
1073                                                 cfg_route.rtr_sensitivity) == NULL)
1074                                 goto out;
1075
1076                         if (!backup &&
1077                             cYAML_create_string(item, "state",
1078                                                 data.cfg_config_u.cfg_route.
1079                                                         rtr_flags ?
1080                                                 "up" : "down") == NULL)
1081                                 goto out;
1082                 }
1083         }
1084
1085         /* print output iff show_rc is not provided */
1086         if (show_rc == NULL)
1087                 cYAML_print_tree(root);
1088
1089         if (l_errno != ENOENT) {
1090                 snprintf(err_str,
1091                          sizeof(err_str),
1092                          "\"cannot get routes: %s\"",
1093                          strerror(l_errno));
1094                 rc = -l_errno;
1095                 goto out;
1096         } else
1097                 rc = LUSTRE_CFG_RC_NO_ERR;
1098
1099         snprintf(err_str, sizeof(err_str), "\"success\"");
1100 out:
1101         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1102                 cYAML_free_tree(root);
1103         } else if (show_rc != NULL && *show_rc != NULL) {
1104                 struct cYAML *show_node;
1105                 /* find the route node, if one doesn't exist then
1106                  * insert one.  Otherwise add to the one there
1107                  */
1108                 show_node = cYAML_get_object_item(*show_rc, "route");
1109                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1110                         cYAML_insert_child(show_node, first_seq);
1111                         free(route);
1112                         free(root);
1113                 } else if (show_node == NULL) {
1114                         cYAML_insert_sibling((*show_rc)->cy_child,
1115                                                 route);
1116                         free(root);
1117                 } else {
1118                         cYAML_free_tree(root);
1119                 }
1120         } else {
1121                 *show_rc = root;
1122         }
1123
1124         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1125
1126         return rc;
1127 }
1128
1129 static int socket_intf_query(int request, char *intf,
1130                              struct ifreq *ifr)
1131 {
1132         int rc = 0;
1133         int sockfd;
1134
1135         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1136                 return LUSTRE_CFG_RC_BAD_PARAM;
1137
1138         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1139         if (sockfd < 0)
1140                 return LUSTRE_CFG_RC_BAD_PARAM;
1141
1142         strcpy(ifr->ifr_name, intf);
1143         rc = ioctl(sockfd, request, ifr);
1144         if (rc != 0)
1145                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1146
1147         close(sockfd);
1148
1149         return rc;
1150 }
1151
1152 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1153 {
1154         struct ifreq ifr;
1155         int rc;
1156
1157         memset(&ifr, 0, sizeof(ifr));
1158         rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1159         if (rc != 0)
1160                 return LUSTRE_CFG_RC_BAD_PARAM;
1161
1162         if ((ifr.ifr_flags & IFF_UP) == 0)
1163                 return LUSTRE_CFG_RC_BAD_PARAM;
1164
1165         memset(&ifr, 0, sizeof(ifr));
1166         rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1167         if (rc != 0)
1168                 return LUSTRE_CFG_RC_BAD_PARAM;
1169
1170         *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1171         *ip = bswap_32(*ip);
1172
1173         return LUSTRE_CFG_RC_NO_ERR;
1174 }
1175
1176 /*
1177  * for each interface in the array of interfaces find the IP address of
1178  * that interface, create its nid and add it to an array of NIDs.
1179  * Stop if any of the interfaces is down
1180  */
1181 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1182                                  lnet_nid_t **nids, __u32 *nnids,
1183                                  char *err_str, size_t str_len)
1184 {
1185         int i = 0, count = 0, rc;
1186         struct lnet_dlc_intf_descr *intf;
1187         char val[LNET_MAX_STR_LEN];
1188         __u32 ip;
1189         int gni_num;
1190         char *endp;
1191         unsigned int num;
1192
1193
1194         if (nw == NULL || nids == NULL) {
1195                 snprintf(err_str, str_len,
1196                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1197                 err_str[str_len - 1] = '\0';
1198                 return LUSTRE_CFG_RC_BAD_PARAM;
1199         }
1200
1201         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1202                 count = 1;
1203         } else {
1204                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1205                         count++;
1206         }
1207
1208         *nids = calloc(count, sizeof(lnet_nid_t));
1209         if (*nids == NULL) {
1210                 snprintf(err_str, str_len,
1211                          "\"out of memory\"");
1212                 err_str[str_len - 1] = '\0';
1213                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1214         }
1215         /*
1216          * special case the GNI interface since it doesn't have an IP
1217          * address. The assumption is that there can only be one GNI
1218          * interface in the system. No interface name is provided.
1219          */
1220         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1221                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1222                                 1, sizeof(val));
1223                 if (rc) {
1224                         snprintf(err_str, str_len,
1225                                  "\"cannot read gni nid\"");
1226                         err_str[str_len - 1] = '\0';
1227                         goto failed;
1228                 }
1229                 gni_num = atoi(val);
1230
1231                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1232
1233                 goto out;
1234         }
1235
1236         /* look at the other interfaces */
1237         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1238                 if (LNET_NETTYP(nw->nw_id) == PTL4LND) {
1239                         /* handle LNDs with numeric interface name */
1240                         num = strtoul(intf->intf_name, &endp, 0);
1241                         if (endp == intf->intf_name || *endp != '\0') {
1242                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1243                                 snprintf(err_str, str_len,
1244                                          "\"couldn't query intf %s\"",
1245                                          intf->intf_name);
1246                                 err_str[str_len - 1] = '\0';
1247                                 goto failed;
1248                         }
1249                         (*nids)[i] = LNET_MKNID(nw->nw_id, num);
1250                         i++;
1251                 } else {
1252                         /* handle LNDs with ip interface name */
1253                         rc = lustre_lnet_queryip(intf, &ip);
1254                         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1255                                 snprintf(err_str, str_len,
1256                                          "\"couldn't query intf %s\"",
1257                                          intf->intf_name);
1258                                 err_str[str_len - 1] = '\0';
1259                                 goto failed;
1260                         }
1261                         (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1262                         i++;
1263                 }
1264         }
1265
1266 out:
1267         *nnids = count;
1268
1269         return 0;
1270
1271 failed:
1272         free(*nids);
1273         *nids = NULL;
1274         return rc;
1275 }
1276
1277 /*
1278  * called repeatedly until a match or no more ip range
1279  * What do you have?
1280  *      ip_range expression
1281  *      interface list with all the interface names.
1282  *      all the interfaces in the system.
1283  *
1284  *      try to match the ip_range expr to one of the interfaces' IPs in
1285  *      the system. If we hit a patch for an interface. Check if that
1286  *      interface name is in the list.
1287  *
1288  *      If there are more than one interface in the list, then make sure
1289  *      that the IPs for all of these interfaces match the ip ranges
1290  *      given.
1291  *
1292  *      for each interface in intf_list
1293  *              look up the intf name in ifa
1294  *              if not there then no match
1295  *              check ip obtained from ifa against a match to any of the
1296  *              ip_ranges given.
1297  *              If no match, then fail
1298  *
1299  *      The result is that all the interfaces have to match.
1300  */
1301 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1302                                  struct list_head *intf_list,
1303                                  struct list_head *ip_ranges)
1304 {
1305         int rc;
1306         __u32 ip;
1307         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1308         struct ifaddrs *ifaddr = ifa;
1309         struct lustre_lnet_ip_range_descr *ip_range;
1310         int family;
1311
1312         /*
1313          * if there are no explicit interfaces, and no ip ranges, then
1314          * configure the first tcp interface we encounter.
1315          */
1316         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1317                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1318                         if (ifaddr->ifa_addr == NULL)
1319                                 continue;
1320
1321                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1322                                 continue;
1323
1324                         family = ifaddr->ifa_addr->sa_family;
1325                         if (family == AF_INET &&
1326                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1327                                 rc = lustre_lnet_add_intf_descr
1328                                         (intf_list, ifaddr->ifa_name,
1329                                         strlen(ifaddr->ifa_name));
1330
1331                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1332                                         return rc;
1333
1334                                 return LUSTRE_CFG_RC_MATCH;
1335                         }
1336                 }
1337                 return LUSTRE_CFG_RC_NO_MATCH;
1338         }
1339
1340         /*
1341          * First interface which matches an IP pattern will be used
1342          */
1343         if (list_empty(intf_list)) {
1344                 /*
1345                  * no interfaces provided in the rule, but an ip range is
1346                  * provided, so try and match an interface to the ip
1347                  * range.
1348                  */
1349                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1350                         if (ifaddr->ifa_addr == NULL)
1351                                 continue;
1352
1353                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1354                                 continue;
1355
1356                         family = ifaddr->ifa_addr->sa_family;
1357                         if (family == AF_INET) {
1358                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1359                                         sin_addr.s_addr;
1360
1361                                 list_for_each_entry(ip_range, ip_ranges,
1362                                                     ipr_entry) {
1363                                         rc = cfs_ip_addr_match(bswap_32(ip),
1364                                                         &ip_range->ipr_expr);
1365                                         if (!rc)
1366                                                 continue;
1367
1368                                         rc = lustre_lnet_add_intf_descr
1369                                           (intf_list, ifaddr->ifa_name,
1370                                            strlen(ifaddr->ifa_name));
1371
1372                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1373                                                 return rc;
1374                                 }
1375                         }
1376                 }
1377
1378                 if (!list_empty(intf_list))
1379                         return LUSTRE_CFG_RC_MATCH;
1380
1381                 return LUSTRE_CFG_RC_NO_MATCH;
1382         }
1383
1384         /*
1385          * If an interface is explicitly specified the ip-range might or
1386          * might not be specified. if specified the interface needs to match the
1387          * ip-range. If no ip-range then the interfaces are
1388          * automatically matched if they are all up.
1389          * If > 1 interfaces all the interfaces must match for the NI to
1390          * be configured.
1391          */
1392         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1393                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1394                         if (ifaddr->ifa_addr == NULL)
1395                                 continue;
1396
1397                         family = ifaddr->ifa_addr->sa_family;
1398                         if (family == AF_INET &&
1399                             strcmp(intf_descr->intf_name,
1400                                    ifaddr->ifa_name) == 0)
1401                                 break;
1402                 }
1403
1404                 if (ifaddr == NULL) {
1405                         list_del(&intf_descr->intf_on_network);
1406                         free_intf_descr(intf_descr);
1407                         continue;
1408                 }
1409
1410                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1411                         list_del(&intf_descr->intf_on_network);
1412                         free_intf_descr(intf_descr);
1413                         continue;
1414                 }
1415
1416                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1417
1418                 rc = 1;
1419                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1420                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1421                         if (rc)
1422                                 break;
1423                 }
1424
1425                 if (!rc) {
1426                         /* no match for this interface */
1427                         list_del(&intf_descr->intf_on_network);
1428                         free_intf_descr(intf_descr);
1429                 }
1430         }
1431
1432         return LUSTRE_CFG_RC_MATCH;
1433 }
1434
1435 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1436                                             lnet_nid_t **nids, __u32 *nnids,
1437                                             char *err_str, size_t str_len)
1438 {
1439         struct ifaddrs *ifa;
1440         int rc = LUSTRE_CFG_RC_NO_ERR;
1441
1442         rc = getifaddrs(&ifa);
1443         if (rc < 0) {
1444                 snprintf(err_str, str_len,
1445                          "\"failed to get interface addresses: %d\"", -errno);
1446                 err_str[str_len - 1] = '\0';
1447                 return -errno;
1448         }
1449
1450         rc = lustre_lnet_match_ip_to_intf(ifa,
1451                                           &ip2nets->ip2nets_net.nw_intflist,
1452                                           &ip2nets->ip2nets_ip_ranges);
1453         if (rc != LUSTRE_CFG_RC_MATCH) {
1454                 snprintf(err_str, str_len,
1455                          "\"couldn't match ip to existing interfaces\"");
1456                 err_str[str_len - 1] = '\0';
1457                 freeifaddrs(ifa);
1458                 return rc;
1459         }
1460
1461         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1462                                    err_str, sizeof(err_str));
1463         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1464                 *nids = NULL;
1465                 *nnids = 0;
1466         }
1467
1468         freeifaddrs(ifa);
1469
1470         return rc;
1471 }
1472
1473 static int
1474 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1475                             struct lnet_ioctl_config_lnd_tunables *tunables,
1476                             struct cfs_expr_list *global_cpts,
1477                             lnet_nid_t *nids, char *err_str)
1478 {
1479         char *data;
1480         struct lnet_ioctl_config_ni *conf;
1481         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1482         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1483         size_t len;
1484         int count;
1485         struct lnet_dlc_intf_descr *intf_descr;
1486         __u32 *cpt_array;
1487         struct cfs_expr_list *cpt_expr;
1488
1489         list_for_each_entry(intf_descr, intf_list,
1490                             intf_on_network) {
1491                 if (tunables != NULL)
1492                         len = sizeof(struct lnet_ioctl_config_ni) +
1493                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1494                 else
1495                         len = sizeof(struct lnet_ioctl_config_ni);
1496
1497                 data = calloc(1, len);
1498                 if (!data)
1499                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1500                 conf = (struct lnet_ioctl_config_ni*) data;
1501                 if (tunables != NULL)
1502                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1503                                 conf->lic_bulk;
1504
1505                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1506                 conf->lic_cfg_hdr.ioc_len = len;
1507                 conf->lic_nid = nids[i];
1508                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1509                         LNET_MAX_STR_LEN);
1510
1511                 if (intf_descr->cpt_expr != NULL)
1512                         cpt_expr = intf_descr->cpt_expr;
1513                 else if (global_cpts != NULL)
1514                         cpt_expr = global_cpts;
1515                 else
1516                         cpt_expr = NULL;
1517
1518                 if (cpt_expr != NULL) {
1519                         count = cfs_expr_list_values(cpt_expr,
1520                                                      LNET_MAX_SHOW_NUM_CPT,
1521                                                      &cpt_array);
1522                         if (count > 0) {
1523                                 memcpy(conf->lic_cpts, cpt_array,
1524                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1525                                 free(cpt_array);
1526                         } else {
1527                                 count = 0;
1528                         }
1529                 } else {
1530                         count = 0;
1531                 }
1532
1533                 conf->lic_ncpts = count;
1534
1535                 if (tunables != NULL)
1536                         memcpy(tun, tunables, sizeof(*tunables));
1537
1538                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1539                 if (rc < 0) {
1540                         rc = -errno;
1541                         snprintf(err_str,
1542                                  LNET_MAX_STR_LEN,
1543                                  "\"cannot add network: %s\"", strerror(errno));
1544                         free(data);
1545                         return rc;
1546                 }
1547                 free(data);
1548                 i++;
1549         }
1550
1551         return LUSTRE_CFG_RC_NO_ERR;
1552 }
1553
1554 int
1555 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1556                            struct lnet_ioctl_config_lnd_tunables *tunables,
1557                            struct cfs_expr_list *global_cpts,
1558                            int seq_no, struct cYAML **err_rc)
1559 {
1560         lnet_nid_t *nids = NULL;
1561         __u32 nnids = 0;
1562         int rc;
1563         char err_str[LNET_MAX_STR_LEN];
1564
1565         snprintf(err_str, sizeof(err_str), "\"success\"");
1566
1567         if (!ip2nets) {
1568                 snprintf(err_str,
1569                          sizeof(err_str),
1570                          "\"incomplete ip2nets information\"");
1571                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1572                 goto out;
1573         }
1574
1575         /*
1576          * call below function to resolve the rules into a list of nids.
1577          * The memory is allocated in that function then freed here when
1578          * it's no longer needed.
1579          */
1580         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1581                                               sizeof(err_str));
1582         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1583                 goto out;
1584
1585         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1586                 snprintf(err_str, sizeof(err_str),
1587                          "\"no interfaces match ip2nets rules\"");
1588                 goto free_nids_out;
1589         }
1590
1591         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1592                                          tunables, global_cpts, nids,
1593                                          err_str);
1594
1595 free_nids_out:
1596         free(nids);
1597
1598 out:
1599         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1600         return rc;
1601 }
1602
1603 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1604                           struct cfs_expr_list *global_cpts,
1605                           char *ip2net,
1606                           struct lnet_ioctl_config_lnd_tunables *tunables,
1607                           int seq_no, struct cYAML **err_rc)
1608 {
1609         char *data = NULL;
1610         struct lnet_ioctl_config_ni *conf;
1611         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1612         char buf[LNET_MAX_STR_LEN];
1613         int rc = LUSTRE_CFG_RC_NO_ERR;
1614         char err_str[LNET_MAX_STR_LEN];
1615         lnet_nid_t *nids = NULL;
1616         __u32 nnids = 0;
1617         size_t len;
1618         int count;
1619         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1620         __u32 *cpt_array;
1621
1622         snprintf(err_str, sizeof(err_str), "\"success\"");
1623
1624         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1625             (list_empty(&nw_descr->nw_intflist) &&
1626              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1627                 snprintf(err_str,
1628                          sizeof(err_str),
1629                          "\"missing mandatory parameters in NI config: '%s'\"",
1630                          (nw_descr == NULL) ? "network , interface" :
1631                          (nw_descr->nw_id == 0) ? "network" : "interface");
1632                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1633                 goto out;
1634         }
1635
1636         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1637                 snprintf(err_str,
1638                          sizeof(err_str),
1639                          "\"ip2net string too long %d\"",
1640                                 (int)strlen(ip2net));
1641                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1642                 goto out;
1643         }
1644
1645         if (ip2net != NULL) {
1646                 if (tunables != NULL)
1647                         len = sizeof(struct lnet_ioctl_config_ni) +
1648                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1649                 else
1650                         len = sizeof(struct lnet_ioctl_config_ni);
1651                 data = calloc(1, len);
1652                 if (!data) {
1653                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1654                         goto out;
1655                 }
1656                 conf = (struct lnet_ioctl_config_ni*) data;
1657                 if (tunables != NULL)
1658                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1659                                 (data + sizeof(*conf));
1660
1661                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1662                 conf->lic_cfg_hdr.ioc_len = len;
1663                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1664                         LNET_MAX_STR_LEN);
1665
1666                 if (global_cpts != NULL) {
1667                         count = cfs_expr_list_values(global_cpts,
1668                                                      LNET_MAX_SHOW_NUM_CPT,
1669                                                      &cpt_array);
1670                         if (count > 0) {
1671                                 memcpy(conf->lic_cpts, cpt_array,
1672                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1673                                 free(cpt_array);
1674                         } else {
1675                                 count = 0;
1676                         }
1677                 } else {
1678                         count = 0;
1679                 }
1680
1681                 conf->lic_ncpts = count;
1682
1683                 if (tunables != NULL)
1684                         memcpy(tun, tunables, sizeof(*tunables));
1685
1686                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1687                 if (rc < 0) {
1688                         rc = -errno;
1689                         snprintf(err_str,
1690                                 sizeof(err_str),
1691                                 "\"cannot add network: %s\"", strerror(errno));
1692                         goto out;
1693                 }
1694
1695                 goto out;
1696         }
1697
1698         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1699                 rc = LUSTRE_CFG_RC_NO_ERR;
1700                 goto out;
1701         }
1702
1703         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1704                 snprintf(err_str,
1705                         sizeof(err_str),
1706                         "\"cannot parse net '%s'\"",
1707                         libcfs_net2str(nw_descr->nw_id));
1708                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1709                 goto out;
1710         }
1711
1712         /*
1713          * special case the GNI since no interface name is expected
1714          */
1715         if (list_empty(&nw_descr->nw_intflist) &&
1716             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1717                 snprintf(err_str,
1718                         sizeof(err_str),
1719                         "\"no interface name provided\"");
1720                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1721                 goto out;
1722         }
1723
1724         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1725                                    err_str, sizeof(err_str));
1726         if (rc != 0) {
1727                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1728                 goto out;
1729         }
1730
1731         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1732                                          tunables, global_cpts, nids,
1733                                          err_str);
1734
1735 out:
1736         if (nw_descr != NULL) {
1737                 list_for_each_entry_safe(intf_descr, tmp,
1738                                          &nw_descr->nw_intflist,
1739                                          intf_on_network) {
1740                         list_del(&intf_descr->intf_on_network);
1741                         free_intf_descr(intf_descr);
1742                 }
1743         }
1744
1745         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1746
1747         if (nids)
1748                 free(nids);
1749
1750         if (data)
1751                 free(data);
1752
1753         return rc;
1754 }
1755
1756 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1757                        int seq_no, struct cYAML **err_rc)
1758 {
1759         struct lnet_ioctl_config_ni data;
1760         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1761         char err_str[LNET_MAX_STR_LEN];
1762         lnet_nid_t *nids = NULL;
1763         __u32 nnids = 0;
1764         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1765
1766         snprintf(err_str, sizeof(err_str), "\"success\"");
1767
1768         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1769                 snprintf(err_str,
1770                          sizeof(err_str),
1771                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1772                          (nw_descr == NULL) ? "network , interface" :
1773                          (nw_descr->nw_id == 0) ? "network" : "interface");
1774                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1775                 goto out;
1776         }
1777
1778         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1779                 return LUSTRE_CFG_RC_NO_ERR;
1780
1781         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1782                 snprintf(err_str,
1783                          sizeof(err_str),
1784                          "\"cannot parse net '%s'\"",
1785                          libcfs_net2str(nw_descr->nw_id));
1786                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1787                 goto out;
1788         }
1789
1790         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1791                                    err_str, sizeof(err_str));
1792         if (rc != 0) {
1793                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1794                 goto out;
1795         }
1796
1797         /*
1798          * no interfaces just the nw_id is specified
1799          */
1800         if (nnids == 0) {
1801                 nids = calloc(1, sizeof(*nids));
1802                 if (nids == NULL) {
1803                         snprintf(err_str, sizeof(err_str),
1804                                 "\"out of memory\"");
1805                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1806                         goto out;
1807                 }
1808                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1809                 nnids = 1;
1810         }
1811
1812         for (i = 0; i < nnids; i++) {
1813                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1814                 data.lic_nid = nids[i];
1815
1816                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1817                 if (rc < 0) {
1818                         rc = -errno;
1819                         snprintf(err_str,
1820                                 sizeof(err_str),
1821                                 "\"cannot del network: %s\"", strerror(errno));
1822                 }
1823         }
1824
1825         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1826                                  intf_on_network) {
1827                 list_del(&intf_descr->intf_on_network);
1828                 free_intf_descr(intf_descr);
1829         }
1830
1831 out:
1832         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1833
1834         if (nids != NULL)
1835                 free(nids);
1836
1837         return rc;
1838 }
1839
1840 static int
1841 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
1842                            enum lnet_health_type type, char *name,
1843                            int seq_no, struct cYAML **err_rc)
1844 {
1845         struct lnet_ioctl_reset_health_cfg data;
1846         int rc = LUSTRE_CFG_RC_NO_ERR;
1847         char err_str[LNET_MAX_STR_LEN];
1848
1849         snprintf(err_str, sizeof(err_str), "\"success\"");
1850
1851         LIBCFS_IOC_INIT_V2(data, rh_hdr);
1852         data.rh_type = type;
1853         data.rh_all = all;
1854         data.rh_value = value;
1855         data.rh_nid = nid;
1856
1857         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
1858         if (rc != 0) {
1859                 rc = -errno;
1860                 snprintf(err_str,
1861                          sizeof(err_str), "Can not configure health value: %s",
1862                          strerror(errno));
1863         }
1864
1865         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
1866
1867         return rc;
1868 }
1869
1870 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
1871                                   struct cYAML **err_rc)
1872 {
1873         lnet_nid_t nid;
1874         if (ni_nid)
1875                 nid = libcfs_str2nid(ni_nid);
1876         else
1877                 nid = LNET_NID_ANY;
1878         return lustre_lnet_config_healthv(value, all, nid,
1879                                           LNET_HEALTH_TYPE_LOCAL_NI,
1880                                           "ni healthv", seq_no, err_rc);
1881 }
1882
1883 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
1884                                        int seq_no, struct cYAML **err_rc)
1885 {
1886         lnet_nid_t nid;
1887         if (lpni_nid)
1888                 nid = libcfs_str2nid(lpni_nid);
1889         else
1890                 nid = LNET_NID_ANY;
1891         return lustre_lnet_config_healthv(value, all, nid,
1892                                           LNET_HEALTH_TYPE_PEER_NI,
1893                                           "peer_ni healthv", seq_no, err_rc);
1894 }
1895
1896 static bool
1897 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1898                           struct lnet_ioctl_comm_count *counts)
1899 {
1900         if (cYAML_create_number(yaml, "put",
1901                                 counts->ico_put_count)
1902                                         == NULL)
1903                 return false;
1904         if (cYAML_create_number(yaml, "get",
1905                                 counts->ico_get_count)
1906                                         == NULL)
1907                 return false;
1908         if (cYAML_create_number(yaml, "reply",
1909                                 counts->ico_reply_count)
1910                                         == NULL)
1911                 return false;
1912         if (cYAML_create_number(yaml, "ack",
1913                                 counts->ico_ack_count)
1914                                         == NULL)
1915                 return false;
1916         if (cYAML_create_number(yaml, "hello",
1917                                 counts->ico_hello_count)
1918                                         == NULL)
1919                 return false;
1920
1921         return true;
1922 }
1923
1924 static struct lnet_ioctl_comm_count *
1925 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1926 {
1927         if (idx == 0)
1928                 return &msg_stats->im_send_stats;
1929         if (idx == 1)
1930                 return &msg_stats->im_recv_stats;
1931         if (idx == 2)
1932                 return &msg_stats->im_drop_stats;
1933
1934         return NULL;
1935 }
1936
1937 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1938                          struct cYAML **show_rc, struct cYAML **err_rc,
1939                          bool backup)
1940 {
1941         char *buf;
1942         struct lnet_ioctl_config_ni *ni_data;
1943         struct lnet_ioctl_config_lnd_tunables *lnd;
1944         struct lnet_ioctl_element_stats *stats;
1945         struct lnet_ioctl_element_msg_stats msg_stats;
1946         struct lnet_ioctl_local_ni_hstats hstats;
1947         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1948         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1949         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1950         int l_errno = 0;
1951         struct cYAML *root = NULL, *tunables = NULL,
1952                 *net_node = NULL, *interfaces = NULL,
1953                 *item = NULL, *first_seq = NULL,
1954                 *tmp = NULL, *statistics = NULL,
1955                 *yhstats = NULL;
1956         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1957         char str_buf[str_buf_len];
1958         char *pos;
1959         char err_str[LNET_MAX_STR_LEN];
1960         bool exist = false, new_net = true;
1961         int net_num = 0;
1962         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1963
1964         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1965
1966         buf = calloc(1, buf_size);
1967         if (buf == NULL)
1968                 goto out;
1969
1970         ni_data = (struct lnet_ioctl_config_ni *)buf;
1971
1972         if (nw != NULL) {
1973                 net = libcfs_str2net(nw);
1974                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1975                         snprintf(err_str,
1976                                  sizeof(err_str),
1977                                  "\"cannot parse net '%s'\"", nw);
1978                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1979                         goto out;
1980                 }
1981         }
1982
1983         root = cYAML_create_object(NULL, NULL);
1984         if (root == NULL)
1985                 goto out;
1986
1987         net_node = cYAML_create_seq(root, "net");
1988         if (net_node == NULL)
1989                 goto out;
1990
1991         for (i = 0;; i++) {
1992                 __u32 rc_net;
1993
1994                 memset(buf, 0, buf_size);
1995
1996                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1997                 /*
1998                  * set the ioc_len to the proper value since INIT assumes
1999                  * size of data
2000                  */
2001                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
2002                 ni_data->lic_idx = i;
2003
2004                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
2005                 if (rc != 0) {
2006                         l_errno = errno;
2007                         break;
2008                 }
2009
2010                 rc_net = LNET_NIDNET(ni_data->lic_nid);
2011
2012                 /* filter on provided data */
2013                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
2014                     net != rc_net)
2015                         continue;
2016
2017                 /* if we're backing up don't store lo */
2018                 if (backup && LNET_NETTYP(rc_net) == LOLND)
2019                         continue;
2020
2021                 /* default rc to -1 in case we hit the goto */
2022                 rc = -1;
2023                 exist = true;
2024
2025                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
2026                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
2027                         (ni_data->lic_bulk + sizeof(*stats));
2028
2029                 if (rc_net != prev_net) {
2030                         prev_net = rc_net;
2031                         new_net = true;
2032                         net_num++;
2033                 }
2034
2035                 if (new_net) {
2036                         if (!cYAML_create_string(net_node, "net type",
2037                                                  libcfs_net2str(rc_net)))
2038                                 goto out;
2039
2040                         tmp = cYAML_create_seq(net_node, "local NI(s)");
2041                         if (tmp == NULL)
2042                                 goto out;
2043                         new_net = false;
2044                 }
2045
2046                 /* create the tree to be printed. */
2047                 item = cYAML_create_seq_item(tmp);
2048                 if (item == NULL)
2049                         goto out;
2050
2051                 if (first_seq == NULL)
2052                         first_seq = item;
2053
2054                 if (!backup &&
2055                     cYAML_create_string(item, "nid",
2056                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2057                         goto out;
2058
2059                 if (!backup &&
2060                     cYAML_create_string(item,
2061                                         "status",
2062                                         (ni_data->lic_status ==
2063                                           LNET_NI_STATUS_UP) ?
2064                                             "up" : "down") == NULL)
2065                         goto out;
2066
2067                 /* don't add interfaces unless there is at least one
2068                  * interface */
2069                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2070                         interfaces = cYAML_create_object(item, "interfaces");
2071                         if (interfaces == NULL)
2072                                 goto out;
2073
2074                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2075                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2076                                         snprintf(str_buf,
2077                                                  sizeof(str_buf), "%d", j);
2078                                         if (cYAML_create_string(interfaces,
2079                                                 str_buf,
2080                                                 ni_data->lic_ni_intf[j]) ==
2081                                                         NULL)
2082                                                 goto out;
2083                                 }
2084                         }
2085                 }
2086
2087                 if (detail) {
2088                         char *limit;
2089                         int k;
2090
2091                         if (backup)
2092                                 goto continue_without_msg_stats;
2093
2094                         statistics = cYAML_create_object(item, "statistics");
2095                         if (statistics == NULL)
2096                                 goto out;
2097
2098                         if (cYAML_create_number(statistics, "send_count",
2099                                                 stats->iel_send_count)
2100                                                         == NULL)
2101                                 goto out;
2102
2103                         if (cYAML_create_number(statistics, "recv_count",
2104                                                 stats->iel_recv_count)
2105                                                         == NULL)
2106                                 goto out;
2107
2108                         if (cYAML_create_number(statistics, "drop_count",
2109                                                 stats->iel_drop_count)
2110                                                         == NULL)
2111                                 goto out;
2112
2113                         if (detail < 2)
2114                                 goto continue_without_msg_stats;
2115
2116                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2117                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2118                         msg_stats.im_idx = i;
2119
2120                         rc = l_ioctl(LNET_DEV_ID,
2121                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2122                                      &msg_stats);
2123                         if (rc != 0) {
2124                                 l_errno = errno;
2125                                 goto continue_without_msg_stats;
2126                         }
2127
2128                         for (k = 0; k < 3; k++) {
2129                                 struct lnet_ioctl_comm_count *counts;
2130                                 struct cYAML *msg_statistics = NULL;
2131
2132                                 msg_statistics = cYAML_create_object(item,
2133                                                  (char *)gmsg_stat_names[k]);
2134                                 if (msg_statistics == NULL)
2135                                         goto out;
2136
2137                                 counts = get_counts(&msg_stats, k);
2138                                 if (counts == NULL)
2139                                         goto out;
2140
2141                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2142                                                                counts))
2143                                         goto out;
2144                         }
2145
2146                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2147                         hstats.hlni_nid = ni_data->lic_nid;
2148                         /* grab health stats */
2149                         rc = l_ioctl(LNET_DEV_ID,
2150                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2151                                      &hstats);
2152                         if (rc != 0) {
2153                                 l_errno = errno;
2154                                 goto continue_without_msg_stats;
2155                         }
2156                         yhstats = cYAML_create_object(item, "health stats");
2157                         if (!yhstats)
2158                                 goto out;
2159                         if (cYAML_create_number(yhstats, "health value",
2160                                                 hstats.hlni_health_value)
2161                                                         == NULL)
2162                                 goto out;
2163                         if (cYAML_create_number(yhstats, "interrupts",
2164                                                 hstats.hlni_local_interrupt)
2165                                                         == NULL)
2166                                 goto out;
2167                         if (cYAML_create_number(yhstats, "dropped",
2168                                                 hstats.hlni_local_dropped)
2169                                                         == NULL)
2170                                 goto out;
2171                         if (cYAML_create_number(yhstats, "aborted",
2172                                                 hstats.hlni_local_aborted)
2173                                                         == NULL)
2174                                 goto out;
2175                         if (cYAML_create_number(yhstats, "no route",
2176                                                 hstats.hlni_local_no_route)
2177                                                         == NULL)
2178                                 goto out;
2179                         if (cYAML_create_number(yhstats, "timeouts",
2180                                                 hstats.hlni_local_timeout)
2181                                                         == NULL)
2182                                 goto out;
2183                         if (cYAML_create_number(yhstats, "error",
2184                                                 hstats.hlni_local_error)
2185                                                         == NULL)
2186                                 goto out;
2187
2188 continue_without_msg_stats:
2189                         tunables = cYAML_create_object(item, "tunables");
2190                         if (!tunables)
2191                                 goto out;
2192
2193                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2194                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2195                                 goto out;
2196
2197                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2198                                                      &lnd->lt_tun);
2199                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2200                             rc != LUSTRE_CFG_RC_NO_MATCH)
2201                                 goto out;
2202
2203                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2204                                 tunables = cYAML_create_object(item,
2205                                                                "lnd tunables");
2206                                 if (tunables == NULL)
2207                                         goto out;
2208                         }
2209
2210                         if (!backup &&
2211                             cYAML_create_number(item, "dev cpt",
2212                                                 ni_data->lic_dev_cpt) == NULL)
2213                                 goto out;
2214
2215                         if (!backup &&
2216                             cYAML_create_number(item, "tcp bonding",
2217                                                 ni_data->lic_tcp_bonding)
2218                                                         == NULL)
2219                                 goto out;
2220
2221                         /* out put the CPTs in the format: "[x,x,x,...]" */
2222                         pos = str_buf;
2223                         limit = str_buf + str_buf_len - 3;
2224                         pos += scnprintf(pos, limit - pos, "\"[");
2225                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2226                                 j < ni_data->lic_ncpts &&
2227                                 pos < limit; j++) {
2228                                 pos += scnprintf(pos, limit - pos,
2229                                                  "%d", ni_data->lic_cpts[j]);
2230                                 if ((j + 1) < ni_data->lic_ncpts)
2231                                         pos += scnprintf(pos, limit - pos, ",");
2232                         }
2233                         snprintf(pos, 3, "]\"");
2234
2235                         if (ni_data->lic_ncpts >= 1 &&
2236                             cYAML_create_string(item, "CPT",
2237                                                 str_buf) == NULL)
2238                                 goto out;
2239                 }
2240         }
2241
2242         /* Print out the net information only if show_rc is not provided */
2243         if (show_rc == NULL)
2244                 cYAML_print_tree(root);
2245
2246         if (l_errno != ENOENT) {
2247                 snprintf(err_str,
2248                          sizeof(err_str),
2249                          "\"cannot get networks: %s\"",
2250                          strerror(l_errno));
2251                 rc = -l_errno;
2252                 goto out;
2253         } else
2254                 rc = LUSTRE_CFG_RC_NO_ERR;
2255
2256         snprintf(err_str, sizeof(err_str), "\"success\"");
2257 out:
2258         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2259                 cYAML_free_tree(root);
2260         } else if (show_rc != NULL && *show_rc != NULL) {
2261                 struct cYAML *show_node;
2262                 /* find the net node, if one doesn't exist
2263                  * then insert one.  Otherwise add to the one there
2264                  */
2265                 show_node = cYAML_get_object_item(*show_rc, "net");
2266                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2267                         cYAML_insert_child(show_node, first_seq);
2268                         free(net_node);
2269                         free(root);
2270                 } else if (show_node == NULL) {
2271                         cYAML_insert_sibling((*show_rc)->cy_child,
2272                                                 net_node);
2273                         free(root);
2274                 } else {
2275                         cYAML_free_tree(root);
2276                 }
2277         } else {
2278                 *show_rc = root;
2279         }
2280
2281         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2282
2283         return rc;
2284 }
2285
2286 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2287 {
2288         struct lnet_ioctl_config_data data;
2289         int rc = LUSTRE_CFG_RC_NO_ERR;
2290         char err_str[LNET_MAX_STR_LEN];
2291
2292         snprintf(err_str, sizeof(err_str), "\"success\"");
2293
2294         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2295         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2296
2297         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2298         if (rc != 0) {
2299                 rc = -errno;
2300                 snprintf(err_str,
2301                          sizeof(err_str),
2302                          "\"cannot %s routing %s\"",
2303                          (enable) ? "enable" : "disable", strerror(errno));
2304                 goto out;
2305         }
2306
2307 out:
2308         cYAML_build_error(rc, seq_no,
2309                          (enable) ? ADD_CMD : DEL_CMD,
2310                          "routing", err_str, err_rc);
2311
2312         return rc;
2313 }
2314
2315 int ioctl_set_value(__u32 val, int ioc, char *name,
2316                     int seq_no, struct cYAML **err_rc)
2317 {
2318         struct lnet_ioctl_set_value data;
2319         int rc = LUSTRE_CFG_RC_NO_ERR;
2320         char err_str[LNET_MAX_STR_LEN];
2321
2322         snprintf(err_str, sizeof(err_str), "\"success\"");
2323
2324         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2325         data.sv_value = val;
2326
2327         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2328         if (rc != 0) {
2329                 rc = -errno;
2330                 snprintf(err_str,
2331                          sizeof(err_str),
2332                          "\"cannot configure %s to %d: %s\"", name,
2333                          val, strerror(errno));
2334         }
2335
2336         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2337
2338         return rc;
2339 }
2340
2341 int lustre_lnet_config_recov_intrv(int intrv, int seq_no, struct cYAML **err_rc)
2342 {
2343         int rc = LUSTRE_CFG_RC_NO_ERR;
2344         char err_str[LNET_MAX_STR_LEN];
2345         char val[LNET_MAX_STR_LEN];
2346
2347         snprintf(err_str, sizeof(err_str), "\"success\"");
2348
2349         snprintf(val, sizeof(val), "%d", intrv);
2350
2351         rc = write_sysfs_file(modparam_path, "lnet_recovery_interval", val,
2352                               1, strlen(val) + 1);
2353         if (rc)
2354                 snprintf(err_str, sizeof(err_str),
2355                          "\"cannot configure recovery interval: %s\"",
2356                          strerror(errno));
2357
2358         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_interval", err_str, err_rc);
2359
2360         return rc;
2361 }
2362
2363 int lustre_lnet_config_rtr_sensitivity(int sen, int seq_no, struct cYAML **err_rc)
2364 {
2365         int rc = LUSTRE_CFG_RC_NO_ERR;
2366         char err_str[LNET_MAX_STR_LEN];
2367         char val[LNET_MAX_STR_LEN];
2368
2369         snprintf(err_str, sizeof(err_str), "\"success\"");
2370
2371         snprintf(val, sizeof(val), "%d", sen);
2372
2373         rc = write_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
2374                               1, strlen(val) + 1);
2375         if (rc)
2376                 snprintf(err_str, sizeof(err_str),
2377                          "\"cannot configure router health sensitivity: %s\"",
2378                          strerror(errno));
2379
2380         cYAML_build_error(rc, seq_no, ADD_CMD, "router_sensitivity", err_str, err_rc);
2381
2382         return rc;
2383 }
2384
2385 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2386 {
2387         int rc = LUSTRE_CFG_RC_NO_ERR;
2388         char err_str[LNET_MAX_STR_LEN];
2389         char val[LNET_MAX_STR_LEN];
2390
2391         snprintf(err_str, sizeof(err_str), "\"success\"");
2392
2393         snprintf(val, sizeof(val), "%d", sen);
2394
2395         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2396                               1, strlen(val) + 1);
2397         if (rc)
2398                 snprintf(err_str, sizeof(err_str),
2399                          "\"cannot configure health sensitivity: %s\"",
2400                          strerror(errno));
2401
2402         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2403
2404         return rc;
2405 }
2406
2407 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2408 {
2409         int rc = LUSTRE_CFG_RC_NO_ERR;
2410         char err_str[LNET_MAX_STR_LEN];
2411         char val[LNET_MAX_STR_LEN];
2412
2413         snprintf(err_str, sizeof(err_str), "\"success\"");
2414
2415         snprintf(val, sizeof(val), "%d", timeout);
2416
2417         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2418                               1, strlen(val) + 1);
2419         if (rc)
2420                 snprintf(err_str, sizeof(err_str),
2421                          "\"cannot configure transaction timeout: %s\"",
2422                          strerror(errno));
2423
2424         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2425
2426         return rc;
2427 }
2428
2429 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2430 {
2431         int rc = LUSTRE_CFG_RC_NO_ERR;
2432         char err_str[LNET_MAX_STR_LEN];
2433         char val[LNET_MAX_STR_LEN];
2434
2435         snprintf(err_str, sizeof(err_str), "\"success\"");
2436
2437         snprintf(val, sizeof(val), "%d", count);
2438
2439         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2440                               1, strlen(val) + 1);
2441         if (rc)
2442                 snprintf(err_str, sizeof(err_str),
2443                          "\"cannot configure retry count: %s\"",
2444                          strerror(errno));
2445
2446         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2447
2448         return rc;
2449 }
2450
2451 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2452 {
2453         int rc = LUSTRE_CFG_RC_NO_ERR;
2454         char err_str[LNET_MAX_STR_LEN];
2455         char val[LNET_MAX_STR_LEN];
2456
2457         snprintf(err_str, sizeof(err_str), "\"success\"");
2458
2459         snprintf(val, sizeof(val), "%d", max);
2460
2461         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2462                               1, strlen(val) + 1);
2463         if (rc)
2464                 snprintf(err_str, sizeof(err_str),
2465                          "\"cannot configure max interfaces: %s\"",
2466                          strerror(errno));
2467
2468         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2469
2470         return rc;
2471 }
2472
2473 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2474 {
2475         int rc = LUSTRE_CFG_RC_NO_ERR;
2476         char err_str[LNET_MAX_STR_LEN];
2477         char val[LNET_MAX_STR_LEN];
2478
2479         snprintf(err_str, sizeof(err_str), "\"success\"");
2480
2481         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2482
2483         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2484                               1, strlen(val) + 1);
2485         if (rc)
2486                 snprintf(err_str, sizeof(err_str),
2487                          "\"cannot configure discovery: %s\"",
2488                          strerror(errno));
2489
2490         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2491
2492         return rc;
2493
2494 }
2495
2496 int lustre_lnet_config_drop_asym_route(int drop, int seq_no,
2497                                        struct cYAML **err_rc)
2498 {
2499         int rc = LUSTRE_CFG_RC_NO_ERR;
2500         char err_str[LNET_MAX_STR_LEN];
2501         char val[LNET_MAX_STR_LEN];
2502
2503         snprintf(err_str, sizeof(err_str), "\"success\"");
2504
2505         snprintf(val, sizeof(val), "%u", (drop) ? 1 : 0);
2506
2507         rc = write_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
2508                               1, strlen(val) + 1);
2509         if (rc)
2510                 snprintf(err_str, sizeof(err_str),
2511                          "\"cannot configure drop asym route: %s\"",
2512                          strerror(errno));
2513
2514         cYAML_build_error(rc, seq_no, ADD_CMD, "drop_asym_route",
2515                           err_str, err_rc);
2516
2517         return rc;
2518
2519 }
2520
2521 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2522 {
2523         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2524                                "numa_range", seq_no, err_rc);
2525 }
2526
2527 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2528                                struct cYAML **err_rc)
2529 {
2530         struct lnet_ioctl_config_data data;
2531         int rc = LUSTRE_CFG_RC_NO_ERR;
2532         char err_str[LNET_MAX_STR_LEN];
2533
2534         snprintf(err_str, sizeof(err_str), "\"success\"");
2535
2536         /* -1 indicates to ignore changes to this field */
2537         if (tiny < -1 || small < -1 || large < -1) {
2538                 snprintf(err_str,
2539                          sizeof(err_str),
2540                          "\"tiny, small and large must be >= 0\"");
2541                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2542                 goto out;
2543         }
2544
2545         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2546         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2547         data.cfg_config_u.cfg_buffers.buf_small = small;
2548         data.cfg_config_u.cfg_buffers.buf_large = large;
2549
2550         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2551         if (rc != 0) {
2552                 rc = -errno;
2553                 snprintf(err_str,
2554                          sizeof(err_str),
2555                          "\"cannot configure buffers: %s\"", strerror(errno));
2556                 goto out;
2557         }
2558
2559 out:
2560         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2561
2562         return rc;
2563 }
2564
2565 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2566                              struct cYAML **err_rc, bool backup)
2567 {
2568         struct lnet_ioctl_config_data *data;
2569         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2570         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2571         int l_errno = 0;
2572         char *buf;
2573         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2574         int buf_count[LNET_NRBPOOLS] = {0};
2575         struct cYAML *root = NULL, *pools_node = NULL,
2576                      *type_node = NULL, *item = NULL, *cpt = NULL,
2577                      *first_seq = NULL, *buffers = NULL;
2578         int i, j;
2579         char err_str[LNET_MAX_STR_LEN];
2580         char node_name[LNET_MAX_STR_LEN];
2581         bool exist = false;
2582
2583         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2584
2585         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2586         if (buf == NULL)
2587                 goto out;
2588
2589         data = (struct lnet_ioctl_config_data *)buf;
2590
2591         root = cYAML_create_object(NULL, NULL);
2592         if (root == NULL)
2593                 goto out;
2594
2595         if (backup)
2596                 pools_node = cYAML_create_object(root, "routing");
2597         else
2598                 pools_node = cYAML_create_seq(root, "routing");
2599         if (pools_node == NULL)
2600                 goto out;
2601
2602         for (i = 0;; i++) {
2603                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2604                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2605                                         sizeof(struct lnet_ioctl_pool_cfg);
2606                 data->cfg_count = i;
2607
2608                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2609                 if (rc != 0) {
2610                         l_errno = errno;
2611                         break;
2612                 }
2613
2614                 exist = true;
2615
2616                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2617
2618                 if (backup)
2619                         goto calculate_buffers;
2620
2621                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2622                 item = cYAML_create_seq_item(pools_node);
2623                 if (item == NULL)
2624                         goto out;
2625
2626                 if (first_seq == NULL)
2627                         first_seq = item;
2628
2629                 cpt = cYAML_create_object(item, node_name);
2630                 if (cpt == NULL)
2631                         goto out;
2632
2633 calculate_buffers:
2634                 /* create the tree  and print */
2635                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2636                         if (!backup) {
2637                                 type_node = cYAML_create_object(cpt, pools[j]);
2638                                 if (type_node == NULL)
2639                                         goto out;
2640                         }
2641                         if (!backup &&
2642                             cYAML_create_number(type_node, "npages",
2643                                                 pool_cfg->pl_pools[j].pl_npages)
2644                             == NULL)
2645                                 goto out;
2646                         if (!backup &&
2647                             cYAML_create_number(type_node, "nbuffers",
2648                                                 pool_cfg->pl_pools[j].
2649                                                   pl_nbuffers) == NULL)
2650                                 goto out;
2651                         if (!backup &&
2652                             cYAML_create_number(type_node, "credits",
2653                                                 pool_cfg->pl_pools[j].
2654                                                    pl_credits) == NULL)
2655                                 goto out;
2656                         if (!backup &&
2657                             cYAML_create_number(type_node, "mincredits",
2658                                                 pool_cfg->pl_pools[j].
2659                                                    pl_mincredits) == NULL)
2660                                 goto out;
2661                         /* keep track of the total count for each of the
2662                          * tiny, small and large buffers */
2663                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2664                 }
2665         }
2666
2667         if (pool_cfg != NULL) {
2668                 if (backup) {
2669                         if (cYAML_create_number(pools_node, "enable",
2670                                                 pool_cfg->pl_routing) ==
2671                         NULL)
2672                                 goto out;
2673
2674                         goto add_buffer_section;
2675                 }
2676
2677                 item = cYAML_create_seq_item(pools_node);
2678                 if (item == NULL)
2679                         goto out;
2680
2681                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2682                     NULL)
2683                         goto out;
2684         }
2685
2686 add_buffer_section:
2687         /* create a buffers entry in the show. This is necessary so that
2688          * if the YAML output is used to configure a node, the buffer
2689          * configuration takes hold */
2690         buffers = cYAML_create_object(root, "buffers");
2691         if (buffers == NULL)
2692                 goto out;
2693
2694         for (i = 0; i < LNET_NRBPOOLS; i++) {
2695                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2696                         goto out;
2697         }
2698
2699         if (show_rc == NULL)
2700                 cYAML_print_tree(root);
2701
2702         if (l_errno != ENOENT) {
2703                 snprintf(err_str,
2704                          sizeof(err_str),
2705                          "\"cannot get routing information: %s\"",
2706                          strerror(l_errno));
2707                 rc = -l_errno;
2708                 goto out;
2709         } else
2710                 rc = LUSTRE_CFG_RC_NO_ERR;
2711
2712         snprintf(err_str, sizeof(err_str), "\"success\"");
2713         rc = LUSTRE_CFG_RC_NO_ERR;
2714
2715 out:
2716         free(buf);
2717         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2718                 cYAML_free_tree(root);
2719         } else if (show_rc != NULL && *show_rc != NULL) {
2720                 struct cYAML *routing_node;
2721                 /* there should exist only one routing block and one
2722                  * buffers block. If there already exists a previous one
2723                  * then don't add another */
2724                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2725                 if (routing_node == NULL) {
2726                         cYAML_insert_sibling((*show_rc)->cy_child,
2727                                                 root->cy_child);
2728                         free(root);
2729                 } else {
2730                         cYAML_free_tree(root);
2731                 }
2732         } else {
2733                 *show_rc = root;
2734         }
2735
2736         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2737
2738         return rc;
2739 }
2740
2741 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2742                           struct cYAML **show_rc, struct cYAML **err_rc,
2743                           bool backup)
2744 {
2745         /*
2746          * TODO: This function is changing in a future patch to accommodate
2747          * PEER_LIST and proper filtering on any nid of the peer
2748          */
2749         struct lnet_ioctl_peer_cfg peer_info;
2750         struct lnet_peer_ni_credit_info *lpni_cri;
2751         struct lnet_ioctl_element_stats *lpni_stats;
2752         struct lnet_ioctl_element_msg_stats *msg_stats;
2753         struct lnet_ioctl_peer_ni_hstats *hstats;
2754         lnet_nid_t *nidp;
2755         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2756         int i, j, k;
2757         int l_errno = 0;
2758         __u32 count;
2759         __u32 size;
2760         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2761                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2762                      *msg_statistics = NULL, *statistics = NULL,
2763                      *yhstats;
2764         char err_str[LNET_MAX_STR_LEN];
2765         struct lnet_process_id *list = NULL;
2766         void *data = NULL;
2767         void *lpni_data;
2768         bool exist = false;
2769
2770         snprintf(err_str, sizeof(err_str),
2771                  "\"out of memory\"");
2772
2773         /* create struct cYAML root object */
2774         root = cYAML_create_object(NULL, NULL);
2775         if (root == NULL)
2776                 goto out;
2777
2778         peer_root = cYAML_create_seq(root, "peer");
2779         if (peer_root == NULL)
2780                 goto out;
2781
2782         count = 1000;
2783         size = count * sizeof(struct lnet_process_id);
2784         list = malloc(size);
2785         if (list == NULL) {
2786                 l_errno = ENOMEM;
2787                 goto out;
2788         }
2789         if (knid != NULL) {
2790                 list[0].nid = libcfs_str2nid(knid);
2791                 count = 1;
2792         } else {
2793                 for (;;) {
2794                         memset(&peer_info, 0, sizeof(peer_info));
2795                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2796                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2797                         peer_info.prcfg_size = size;
2798                         peer_info.prcfg_bulk = list;
2799
2800                         l_errno = 0;
2801                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2802                                      &peer_info);
2803                         count = peer_info.prcfg_count;
2804                         if (rc == 0)
2805                                 break;
2806                         l_errno = errno;
2807                         if (l_errno != E2BIG) {
2808                                 snprintf(err_str,
2809                                         sizeof(err_str),
2810                                         "\"cannot get peer list: %s\"",
2811                                         strerror(l_errno));
2812                                 rc = -l_errno;
2813                                 goto out;
2814                         }
2815                         free(list);
2816                         size = peer_info.prcfg_size;
2817                         list = malloc(size);
2818                         if (list == NULL) {
2819                                 l_errno = ENOMEM;
2820                                 goto out;
2821                         }
2822                 }
2823         }
2824
2825         size = 4096;
2826         data = malloc(size);
2827         if (data == NULL) {
2828                 l_errno = ENOMEM;
2829                 goto out;
2830         }
2831
2832         for (i = 0; i < count; i++) {
2833                 for (;;) {
2834                         memset(&peer_info, 0, sizeof(peer_info));
2835                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2836                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2837                         peer_info.prcfg_prim_nid = list[i].nid;
2838                         peer_info.prcfg_size = size;
2839                         peer_info.prcfg_bulk = data;
2840
2841                         l_errno = 0;
2842                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2843                                      &peer_info);
2844                         if (rc == 0)
2845                                 break;
2846                         l_errno = errno;
2847                         if (l_errno != E2BIG) {
2848                                 snprintf(err_str,
2849                                         sizeof(err_str),
2850                                         "\"cannot get peer information: %s\"",
2851                                         strerror(l_errno));
2852                                 rc = -l_errno;
2853                                 goto out;
2854                         }
2855                         free(data);
2856                         size = peer_info.prcfg_size;
2857                         data = malloc(size);
2858                         if (data == NULL) {
2859                                 l_errno = ENOMEM;
2860                                 goto out;
2861                         }
2862                 }
2863                 exist = true;
2864
2865                 peer = cYAML_create_seq_item(peer_root);
2866                 if (peer == NULL)
2867                         goto out;
2868
2869                 if (first_seq == NULL)
2870                         first_seq = peer;
2871
2872                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2873                 if (cYAML_create_string(peer, "primary nid",
2874                                         libcfs_nid2str(pnid))
2875                     == NULL)
2876                         goto out;
2877                 if (cYAML_create_string(peer, "Multi-Rail",
2878                                         peer_info.prcfg_mr ? "True" : "False")
2879                     == NULL)
2880                         goto out;
2881                 /*
2882                  * print out the state of the peer only if details are
2883                  * requested
2884                  */
2885                 if (detail >= 3) {
2886                         if (!backup &&
2887                             cYAML_create_number(peer, "peer state",
2888                                                 peer_info.prcfg_state)
2889                                 == NULL)
2890                                 goto out;
2891                 }
2892
2893                 tmp = cYAML_create_seq(peer, "peer ni");
2894                 if (tmp == NULL)
2895                         goto out;
2896
2897                 lpni_data = data;
2898                 for (j = 0; j < peer_info.prcfg_count; j++) {
2899                         nidp = lpni_data;
2900                         lpni_cri = (void*)nidp + sizeof(nidp);
2901                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2902                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2903                         hstats = (void *)msg_stats + sizeof(*msg_stats);
2904                         lpni_data = (void *)hstats + sizeof(*hstats);
2905
2906                         peer_ni = cYAML_create_seq_item(tmp);
2907                         if (peer_ni == NULL)
2908                                 goto out;
2909
2910                         if (cYAML_create_string(peer_ni, "nid",
2911                                                 libcfs_nid2str(*nidp))
2912                             == NULL)
2913                                 goto out;
2914
2915                         if (backup)
2916                                 continue;
2917
2918                         if (cYAML_create_string(peer_ni, "state",
2919                                                 lpni_cri->cr_aliveness)
2920                             == NULL)
2921                                 goto out;
2922
2923                         if (!detail)
2924                                 continue;
2925
2926                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2927                                                 lpni_cri->cr_ni_peer_tx_credits)
2928                             == NULL)
2929                                 goto out;
2930
2931                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2932                                                 lpni_cri->cr_peer_tx_credits)
2933                             == NULL)
2934                                 goto out;
2935
2936                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2937                                                 lpni_cri->cr_peer_min_tx_credits)
2938                             == NULL)
2939                                 goto out;
2940
2941                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2942                                                 lpni_cri->cr_peer_tx_qnob)
2943                             == NULL)
2944                                 goto out;
2945
2946                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2947                                                 lpni_cri->cr_peer_rtr_credits)
2948                             == NULL)
2949                                 goto out;
2950
2951                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2952                                                 lpni_cri->cr_peer_min_rtr_credits)
2953                             == NULL)
2954                                 goto out;
2955
2956                         if (cYAML_create_number(peer_ni, "refcount",
2957                                                 lpni_cri->cr_refcount) == NULL)
2958                                 goto out;
2959
2960                         statistics = cYAML_create_object(peer_ni, "statistics");
2961                         if (statistics == NULL)
2962                                 goto out;
2963
2964                         if (cYAML_create_number(statistics, "send_count",
2965                                                 lpni_stats->iel_send_count)
2966                             == NULL)
2967                                 goto out;
2968
2969                         if (cYAML_create_number(statistics, "recv_count",
2970                                                 lpni_stats->iel_recv_count)
2971                             == NULL)
2972                                 goto out;
2973
2974                         if (cYAML_create_number(statistics, "drop_count",
2975                                                 lpni_stats->iel_drop_count)
2976                             == NULL)
2977                                 goto out;
2978
2979                         if (detail < 2)
2980                                 continue;
2981
2982                         for (k = 0; k < 3; k++) {
2983                                 struct lnet_ioctl_comm_count *counts;
2984
2985                                 msg_statistics = cYAML_create_object(peer_ni,
2986                                                  (char *) gmsg_stat_names[k]);
2987                                 if (msg_statistics == NULL)
2988                                         goto out;
2989
2990                                 counts = get_counts(msg_stats, k);
2991                                 if (counts == NULL)
2992                                         goto out;
2993
2994                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2995                                                                counts))
2996                                         goto out;
2997                         }
2998
2999                         yhstats = cYAML_create_object(peer_ni, "health stats");
3000                         if (!yhstats)
3001                                 goto out;
3002                         if (cYAML_create_number(yhstats, "health value",
3003                                                 hstats->hlpni_health_value)
3004                                                         == NULL)
3005                                 goto out;
3006                         if (cYAML_create_number(yhstats, "dropped",
3007                                                 hstats->hlpni_remote_dropped)
3008                                                         == NULL)
3009                                 goto out;
3010                         if (cYAML_create_number(yhstats, "timeout",
3011                                                 hstats->hlpni_remote_timeout)
3012                                                         == NULL)
3013                                 goto out;
3014                         if (cYAML_create_number(yhstats, "error",
3015                                                 hstats->hlpni_remote_error)
3016                                                         == NULL)
3017                                 goto out;
3018                         if (cYAML_create_number(yhstats, "network timeout",
3019                                                 hstats->hlpni_network_timeout)
3020                                                         == NULL)
3021                                 goto out;
3022                 }
3023         }
3024
3025         /* print output iff show_rc is not provided */
3026         if (show_rc == NULL)
3027                 cYAML_print_tree(root);
3028
3029         snprintf(err_str, sizeof(err_str), "\"success\"");
3030         rc = LUSTRE_CFG_RC_NO_ERR;
3031
3032 out:
3033         free(list);
3034         free(data);
3035         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
3036                 cYAML_free_tree(root);
3037         } else if (show_rc != NULL && *show_rc != NULL) {
3038                 struct cYAML *show_node;
3039                 /* find the peer node, if one doesn't exist then
3040                  * insert one.  Otherwise add to the one there
3041                  */
3042                 show_node = cYAML_get_object_item(*show_rc,
3043                                                   "peer");
3044                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3045                         cYAML_insert_child(show_node, first_seq);
3046                         free(peer_root);
3047                         free(root);
3048                 } else if (show_node == NULL) {
3049                         cYAML_insert_sibling((*show_rc)->cy_child,
3050                                              peer_root);
3051                         free(root);
3052                 } else {
3053                         cYAML_free_tree(root);
3054                 }
3055         } else {
3056                 *show_rc = root;
3057         }
3058
3059         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3060                           err_rc);
3061
3062         return rc;
3063 }
3064
3065 int lustre_lnet_list_peer(int seq_no,
3066                           struct cYAML **show_rc, struct cYAML **err_rc)
3067 {
3068         struct lnet_ioctl_peer_cfg peer_info;
3069         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3070         __u32 count;
3071         __u32 size;
3072         int i = 0;
3073         int l_errno = 0;
3074         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
3075         char err_str[LNET_MAX_STR_LEN];
3076         struct lnet_process_id *list = NULL;
3077
3078         snprintf(err_str, sizeof(err_str),
3079                  "\"out of memory\"");
3080
3081         memset(&peer_info, 0, sizeof(peer_info));
3082
3083         /* create struct cYAML root object */
3084         root = cYAML_create_object(NULL, NULL);
3085         if (root == NULL)
3086                 goto out;
3087
3088         list_root = cYAML_create_seq(root, "peer list");
3089         if (list_root == NULL)
3090                 goto out;
3091
3092         count = 1000;
3093         size = count * sizeof(struct lnet_process_id);
3094         list = malloc(size);
3095         if (list == NULL) {
3096                 l_errno = ENOMEM;
3097                 goto out;
3098         }
3099         for (;;) {
3100                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3101                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3102                 peer_info.prcfg_size = size;
3103                 peer_info.prcfg_bulk = list;
3104
3105                 l_errno = 0;
3106                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
3107                 count = peer_info.prcfg_count;
3108                 if (rc == 0)
3109                         break;
3110                 l_errno = errno;
3111                 if (l_errno != E2BIG) {
3112                         snprintf(err_str,
3113                                 sizeof(err_str),
3114                                 "\"cannot get peer list: %s\"",
3115                                 strerror(l_errno));
3116                         rc = -l_errno;
3117                         goto out;
3118                 }
3119                 free(list);
3120                 size = peer_info.prcfg_size;
3121                 list = malloc(size);
3122                 if (list == NULL) {
3123                         l_errno = ENOMEM;
3124                         goto out;
3125                 }
3126         }
3127
3128         /* count is now the actual number of ids in the list. */
3129         for (i = 0; i < count; i++) {
3130                 if (cYAML_create_string(list_root, "nid",
3131                                         libcfs_nid2str(list[i].nid))
3132                     == NULL)
3133                         goto out;
3134         }
3135
3136         /* print output iff show_rc is not provided */
3137         if (show_rc == NULL)
3138                 cYAML_print_tree(root);
3139
3140         snprintf(err_str, sizeof(err_str), "\"success\"");
3141         rc = LUSTRE_CFG_RC_NO_ERR;
3142
3143 out:
3144         if (list != NULL)
3145                 free(list);
3146         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3147                 cYAML_free_tree(root);
3148         } else if (show_rc != NULL && *show_rc != NULL) {
3149                 struct cYAML *show_node;
3150                 /* find the peer node, if one doesn't exist then
3151                  * insert one.  Otherwise add to the one there
3152                  */
3153                 show_node = cYAML_get_object_item(*show_rc,
3154                                                   "peer");
3155                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3156                         cYAML_insert_child(show_node, first_seq);
3157                         free(list_root);
3158                         free(root);
3159                 } else if (show_node == NULL) {
3160                         cYAML_insert_sibling((*show_rc)->cy_child,
3161                                              list_root);
3162                         free(root);
3163                 } else {
3164                         cYAML_free_tree(root);
3165                 }
3166         } else {
3167                 *show_rc = root;
3168         }
3169
3170         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3171                           err_rc);
3172
3173         return rc;
3174 }
3175
3176 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
3177                           struct cYAML *root)
3178 {
3179         struct cYAML *show_node;
3180
3181         show_node = cYAML_get_object_item(show_rc, "global");
3182         if (show_node != NULL)
3183                 cYAML_insert_sibling(show_node->cy_child,
3184                                      node->cy_child);
3185         else
3186                 cYAML_insert_sibling(show_rc->cy_child,
3187                                      node);
3188         free(root);
3189 }
3190
3191 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
3192                                    char *name, __u32 value,
3193                                    struct cYAML **show_rc,
3194                                    struct cYAML **err_rc, int err)
3195 {
3196         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3197         struct cYAML *root = NULL, *global = NULL;
3198
3199         if (err) {
3200                 rc = err;
3201                 goto out;
3202         }
3203
3204         root = cYAML_create_object(NULL, NULL);
3205         if (root == NULL)
3206                 goto out;
3207
3208         global = cYAML_create_object(root, "global");
3209         if (global == NULL)
3210                 goto out;
3211
3212         if (cYAML_create_number(global, name,
3213                                 value) == NULL)
3214                 goto out;
3215
3216         if (show_rc == NULL)
3217                 cYAML_print_tree(root);
3218
3219         snprintf(err_str, err_len, "\"success\"");
3220
3221         rc = LUSTRE_CFG_RC_NO_ERR;
3222
3223 out:
3224         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3225                 cYAML_free_tree(root);
3226         } else if (show_rc != NULL && *show_rc != NULL) {
3227                 add_to_global(*show_rc, global, root);
3228         } else {
3229                 *show_rc = root;
3230         }
3231
3232         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
3233
3234         return rc;
3235 }
3236
3237 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
3238                                     struct cYAML **show_rc,
3239                                     struct cYAML **err_rc)
3240 {
3241         struct lnet_ioctl_set_value data;
3242         int rc;
3243         int l_errno = 0;
3244         char err_str[LNET_MAX_STR_LEN];
3245
3246         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3247
3248         LIBCFS_IOC_INIT_V2(data, sv_hdr);
3249
3250         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3251         if (rc != 0) {
3252                 l_errno = -errno;
3253                 snprintf(err_str,
3254                          sizeof(err_str),
3255                          "\"cannot get %s: %s\"",
3256                          name, strerror(l_errno));
3257         }
3258
3259         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3260                                        data.sv_value, show_rc, err_rc, l_errno);
3261 }
3262
3263 int lustre_lnet_show_recov_intrv(int seq_no, struct cYAML **show_rc,
3264                                  struct cYAML **err_rc)
3265 {
3266         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3267         char val[LNET_MAX_STR_LEN];
3268         int intrv = -1, l_errno = 0;
3269         char err_str[LNET_MAX_STR_LEN];
3270
3271         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3272
3273         rc = read_sysfs_file(modparam_path, "lnet_recovery_interval", val,
3274                              1, sizeof(val));
3275         if (rc) {
3276                 l_errno = -errno;
3277                 snprintf(err_str, sizeof(err_str),
3278                          "\"cannot get recovery interval: %d\"", rc);
3279         } else {
3280                 intrv = atoi(val);
3281         }
3282
3283         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3284                                        "recovery_interval", intrv, show_rc,
3285                                        err_rc, l_errno);
3286 }
3287
3288 int lustre_lnet_show_hsensitivity(int seq_no, struct cYAML **show_rc,
3289                                   struct cYAML **err_rc)
3290 {
3291         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3292         char val[LNET_MAX_STR_LEN];
3293         int sen = -1, l_errno = 0;
3294         char err_str[LNET_MAX_STR_LEN];
3295
3296         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3297
3298         rc = read_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
3299                              1, sizeof(val));
3300         if (rc) {
3301                 l_errno = -errno;
3302                 snprintf(err_str, sizeof(err_str),
3303                          "\"cannot get health sensitivity: %d\"", rc);
3304         } else {
3305                 sen = atoi(val);
3306         }
3307
3308         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3309                                        "health_sensitivity", sen, show_rc,
3310                                        err_rc, l_errno);
3311 }
3312
3313 int lustre_lnet_show_rtr_sensitivity(int seq_no, struct cYAML **show_rc,
3314                                      struct cYAML **err_rc)
3315 {
3316         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3317         char val[LNET_MAX_STR_LEN];
3318         int sen = -1, l_errno = 0;
3319         char err_str[LNET_MAX_STR_LEN];
3320
3321         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3322
3323         rc = read_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
3324                              1, sizeof(val));
3325         if (rc) {
3326                 l_errno = -errno;
3327                 snprintf(err_str, sizeof(err_str),
3328                          "\"cannot get router sensitivity percentage: %d\"", rc);
3329         } else {
3330                 sen = atoi(val);
3331         }
3332
3333         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3334                                        "router_sensitivity", sen, show_rc,
3335                                        err_rc, l_errno);
3336 }
3337
3338 int lustre_lnet_show_transaction_to(int seq_no, struct cYAML **show_rc,
3339                                     struct cYAML **err_rc)
3340 {
3341         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3342         char val[LNET_MAX_STR_LEN];
3343         int tto = -1, l_errno = 0;
3344         char err_str[LNET_MAX_STR_LEN];
3345
3346         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3347
3348         rc = read_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
3349                              1, sizeof(val));
3350         if (rc) {
3351                 l_errno = -errno;
3352                 snprintf(err_str, sizeof(err_str),
3353                          "\"cannot get transaction timeout: %d\"", rc);
3354         } else {
3355                 tto = atoi(val);
3356         }
3357
3358         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3359                                        "transaction_timeout", tto, show_rc,
3360                                        err_rc, l_errno);
3361 }
3362
3363 int lustre_lnet_show_retry_count(int seq_no, struct cYAML **show_rc,
3364                                  struct cYAML **err_rc)
3365 {
3366         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3367         char val[LNET_MAX_STR_LEN];
3368         int retry_count = -1, l_errno = 0;
3369         char err_str[LNET_MAX_STR_LEN];
3370
3371         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3372
3373         rc = read_sysfs_file(modparam_path, "lnet_retry_count", val,
3374                              1, sizeof(val));
3375         if (rc) {
3376                 l_errno =