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