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