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