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