Whamcloud - gitweb
LU-10452 lnet: cleanup YAML output
[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, bool backup)
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 (!backup &&
1076                             cYAML_create_string(item, "state",
1077                                                 data.cfg_config_u.cfg_route.
1078                                                         rtr_flags ?
1079                                                 "up" : "down") == NULL)
1080                                 goto out;
1081                 }
1082         }
1083
1084         /* print output iff show_rc is not provided */
1085         if (show_rc == NULL)
1086                 cYAML_print_tree(root);
1087
1088         if (l_errno != ENOENT) {
1089                 snprintf(err_str,
1090                          sizeof(err_str),
1091                          "\"cannot get routes: %s\"",
1092                          strerror(l_errno));
1093                 rc = -l_errno;
1094                 goto out;
1095         } else
1096                 rc = LUSTRE_CFG_RC_NO_ERR;
1097
1098         snprintf(err_str, sizeof(err_str), "\"success\"");
1099 out:
1100         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1101                 cYAML_free_tree(root);
1102         } else if (show_rc != NULL && *show_rc != NULL) {
1103                 struct cYAML *show_node;
1104                 /* find the route node, if one doesn't exist then
1105                  * insert one.  Otherwise add to the one there
1106                  */
1107                 show_node = cYAML_get_object_item(*show_rc, "route");
1108                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1109                         cYAML_insert_child(show_node, first_seq);
1110                         free(route);
1111                         free(root);
1112                 } else if (show_node == NULL) {
1113                         cYAML_insert_sibling((*show_rc)->cy_child,
1114                                                 route);
1115                         free(root);
1116                 } else {
1117                         cYAML_free_tree(root);
1118                 }
1119         } else {
1120                 *show_rc = root;
1121         }
1122
1123         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1124
1125         return rc;
1126 }
1127
1128 static int socket_intf_query(int request, char *intf,
1129                              struct ifreq *ifr)
1130 {
1131         int rc = 0;
1132         int sockfd;
1133
1134         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1135                 return LUSTRE_CFG_RC_BAD_PARAM;
1136
1137         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1138         if (sockfd < 0)
1139                 return LUSTRE_CFG_RC_BAD_PARAM;
1140
1141         strcpy(ifr->ifr_name, intf);
1142         rc = ioctl(sockfd, request, ifr);
1143         if (rc != 0)
1144                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1145
1146         close(sockfd);
1147
1148         return rc;
1149 }
1150
1151 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1152 {
1153         struct ifreq ifr;
1154         int rc;
1155
1156         memset(&ifr, 0, sizeof(ifr));
1157         rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1158         if (rc != 0)
1159                 return LUSTRE_CFG_RC_BAD_PARAM;
1160
1161         if ((ifr.ifr_flags & IFF_UP) == 0)
1162                 return LUSTRE_CFG_RC_BAD_PARAM;
1163
1164         memset(&ifr, 0, sizeof(ifr));
1165         rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1166         if (rc != 0)
1167                 return LUSTRE_CFG_RC_BAD_PARAM;
1168
1169         *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1170         *ip = bswap_32(*ip);
1171
1172         return LUSTRE_CFG_RC_NO_ERR;
1173 }
1174
1175 /*
1176  * for each interface in the array of interfaces find the IP address of
1177  * that interface, create its nid and add it to an array of NIDs.
1178  * Stop if any of the interfaces is down
1179  */
1180 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1181                                  lnet_nid_t **nids, __u32 *nnids,
1182                                  char *err_str, size_t str_len)
1183 {
1184         int i = 0, count = 0, rc;
1185         struct lnet_dlc_intf_descr *intf;
1186         char val[LNET_MAX_STR_LEN];
1187         __u32 ip;
1188         int gni_num;
1189
1190
1191         if (nw == NULL || nids == NULL) {
1192                 snprintf(err_str, str_len,
1193                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1194                 err_str[str_len - 1] = '\0';
1195                 return LUSTRE_CFG_RC_BAD_PARAM;
1196         }
1197
1198         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1199                 count = 1;
1200         } else {
1201                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1202                         count++;
1203         }
1204
1205         *nids = calloc(count, sizeof(lnet_nid_t));
1206         if (*nids == NULL) {
1207                 snprintf(err_str, str_len,
1208                          "\"out of memory\"");
1209                 err_str[str_len - 1] = '\0';
1210                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1211         }
1212         /*
1213          * special case the GNI interface since it doesn't have an IP
1214          * address. The assumption is that there can only be one GNI
1215          * interface in the system. No interface name is provided.
1216          */
1217         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1218                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1219                                 1, sizeof(val));
1220                 if (rc) {
1221                         snprintf(err_str, str_len,
1222                                  "\"cannot read gni nid\"");
1223                         err_str[str_len - 1] = '\0';
1224                         goto failed;
1225                 }
1226                 gni_num = atoi(val);
1227
1228                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1229
1230                 goto out;
1231         }
1232
1233         /* look at the other interfaces */
1234         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1235                 rc = lustre_lnet_queryip(intf, &ip);
1236                 if (rc != LUSTRE_CFG_RC_NO_ERR) {
1237                         snprintf(err_str, str_len,
1238                                  "\"couldn't query intf %s\"", intf->intf_name);
1239                         err_str[str_len - 1] = '\0';
1240                         goto failed;
1241                 }
1242                 (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1243                 i++;
1244         }
1245
1246 out:
1247         *nnids = count;
1248
1249         return 0;
1250
1251 failed:
1252         free(*nids);
1253         *nids = NULL;
1254         return rc;
1255 }
1256
1257 /*
1258  * called repeatedly until a match or no more ip range
1259  * What do you have?
1260  *      ip_range expression
1261  *      interface list with all the interface names.
1262  *      all the interfaces in the system.
1263  *
1264  *      try to match the ip_range expr to one of the interfaces' IPs in
1265  *      the system. If we hit a patch for an interface. Check if that
1266  *      interface name is in the list.
1267  *
1268  *      If there are more than one interface in the list, then make sure
1269  *      that the IPs for all of these interfaces match the ip ranges
1270  *      given.
1271  *
1272  *      for each interface in intf_list
1273  *              look up the intf name in ifa
1274  *              if not there then no match
1275  *              check ip obtained from ifa against a match to any of the
1276  *              ip_ranges given.
1277  *              If no match, then fail
1278  *
1279  *      The result is that all the interfaces have to match.
1280  */
1281 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1282                                  struct list_head *intf_list,
1283                                  struct list_head *ip_ranges)
1284 {
1285         int rc;
1286         __u32 ip;
1287         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1288         struct ifaddrs *ifaddr = ifa;
1289         struct lustre_lnet_ip_range_descr *ip_range;
1290         int family;
1291
1292         /*
1293          * if there are no explicit interfaces, and no ip ranges, then
1294          * configure the first tcp interface we encounter.
1295          */
1296         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1297                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1298                         if (ifaddr->ifa_addr == NULL)
1299                                 continue;
1300
1301                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1302                                 continue;
1303
1304                         family = ifaddr->ifa_addr->sa_family;
1305                         if (family == AF_INET &&
1306                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1307                                 rc = lustre_lnet_add_intf_descr
1308                                         (intf_list, ifaddr->ifa_name,
1309                                         strlen(ifaddr->ifa_name));
1310
1311                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1312                                         return rc;
1313
1314                                 return LUSTRE_CFG_RC_MATCH;
1315                         }
1316                 }
1317                 return LUSTRE_CFG_RC_NO_MATCH;
1318         }
1319
1320         /*
1321          * First interface which matches an IP pattern will be used
1322          */
1323         if (list_empty(intf_list)) {
1324                 /*
1325                  * no interfaces provided in the rule, but an ip range is
1326                  * provided, so try and match an interface to the ip
1327                  * range.
1328                  */
1329                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1330                         if (ifaddr->ifa_addr == NULL)
1331                                 continue;
1332
1333                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1334                                 continue;
1335
1336                         family = ifaddr->ifa_addr->sa_family;
1337                         if (family == AF_INET) {
1338                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1339                                         sin_addr.s_addr;
1340
1341                                 list_for_each_entry(ip_range, ip_ranges,
1342                                                     ipr_entry) {
1343                                         rc = cfs_ip_addr_match(bswap_32(ip),
1344                                                         &ip_range->ipr_expr);
1345                                         if (!rc)
1346                                                 continue;
1347
1348                                         rc = lustre_lnet_add_intf_descr
1349                                           (intf_list, ifaddr->ifa_name,
1350                                            strlen(ifaddr->ifa_name));
1351
1352                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1353                                                 return rc;
1354                                 }
1355                         }
1356                 }
1357
1358                 if (!list_empty(intf_list))
1359                         return LUSTRE_CFG_RC_MATCH;
1360
1361                 return LUSTRE_CFG_RC_NO_MATCH;
1362         }
1363
1364         /*
1365          * If an interface is explicitly specified the ip-range might or
1366          * might not be specified. if specified the interface needs to match the
1367          * ip-range. If no ip-range then the interfaces are
1368          * automatically matched if they are all up.
1369          * If > 1 interfaces all the interfaces must match for the NI to
1370          * be configured.
1371          */
1372         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1373                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1374                         if (ifaddr->ifa_addr == NULL)
1375                                 continue;
1376
1377                         family = ifaddr->ifa_addr->sa_family;
1378                         if (family == AF_INET &&
1379                             strcmp(intf_descr->intf_name,
1380                                    ifaddr->ifa_name) == 0)
1381                                 break;
1382                 }
1383
1384                 if (ifaddr == NULL) {
1385                         list_del(&intf_descr->intf_on_network);
1386                         free_intf_descr(intf_descr);
1387                         continue;
1388                 }
1389
1390                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1391                         list_del(&intf_descr->intf_on_network);
1392                         free_intf_descr(intf_descr);
1393                         continue;
1394                 }
1395
1396                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1397
1398                 rc = 1;
1399                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1400                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1401                         if (rc)
1402                                 break;
1403                 }
1404
1405                 if (!rc) {
1406                         /* no match for this interface */
1407                         list_del(&intf_descr->intf_on_network);
1408                         free_intf_descr(intf_descr);
1409                 }
1410         }
1411
1412         return LUSTRE_CFG_RC_MATCH;
1413 }
1414
1415 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1416                                             lnet_nid_t **nids, __u32 *nnids,
1417                                             char *err_str, size_t str_len)
1418 {
1419         struct ifaddrs *ifa;
1420         int rc = LUSTRE_CFG_RC_NO_ERR;
1421
1422         rc = getifaddrs(&ifa);
1423         if (rc < 0) {
1424                 snprintf(err_str, str_len,
1425                          "\"failed to get interface addresses: %d\"", -errno);
1426                 err_str[str_len - 1] = '\0';
1427                 return -errno;
1428         }
1429
1430         rc = lustre_lnet_match_ip_to_intf(ifa,
1431                                           &ip2nets->ip2nets_net.nw_intflist,
1432                                           &ip2nets->ip2nets_ip_ranges);
1433         if (rc != LUSTRE_CFG_RC_MATCH) {
1434                 snprintf(err_str, str_len,
1435                          "\"couldn't match ip to existing interfaces\"");
1436                 err_str[str_len - 1] = '\0';
1437                 freeifaddrs(ifa);
1438                 return rc;
1439         }
1440
1441         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1442                                    err_str, sizeof(err_str));
1443         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1444                 *nids = NULL;
1445                 *nnids = 0;
1446         }
1447
1448         freeifaddrs(ifa);
1449
1450         return rc;
1451 }
1452
1453 static int
1454 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1455                             struct lnet_ioctl_config_lnd_tunables *tunables,
1456                             struct cfs_expr_list *global_cpts,
1457                             lnet_nid_t *nids, char *err_str)
1458 {
1459         char *data;
1460         struct lnet_ioctl_config_ni *conf;
1461         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1462         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1463         size_t len;
1464         int count;
1465         struct lnet_dlc_intf_descr *intf_descr;
1466         __u32 *cpt_array;
1467         struct cfs_expr_list *cpt_expr;
1468
1469         list_for_each_entry(intf_descr, intf_list,
1470                             intf_on_network) {
1471                 if (tunables != NULL)
1472                         len = sizeof(struct lnet_ioctl_config_ni) +
1473                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1474                 else
1475                         len = sizeof(struct lnet_ioctl_config_ni);
1476
1477                 data = calloc(1, len);
1478                 if (!data)
1479                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1480                 conf = (struct lnet_ioctl_config_ni*) data;
1481                 if (tunables != NULL)
1482                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1483                                 conf->lic_bulk;
1484
1485                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1486                 conf->lic_cfg_hdr.ioc_len = len;
1487                 conf->lic_nid = nids[i];
1488                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1489                         LNET_MAX_STR_LEN);
1490
1491                 if (intf_descr->cpt_expr != NULL)
1492                         cpt_expr = intf_descr->cpt_expr;
1493                 else if (global_cpts != NULL)
1494                         cpt_expr = global_cpts;
1495                 else
1496                         cpt_expr = NULL;
1497
1498                 if (cpt_expr != NULL) {
1499                         count = cfs_expr_list_values(cpt_expr,
1500                                                      LNET_MAX_SHOW_NUM_CPT,
1501                                                      &cpt_array);
1502                         if (count > 0) {
1503                                 memcpy(conf->lic_cpts, cpt_array,
1504                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1505                                 free(cpt_array);
1506                         } else {
1507                                 count = 0;
1508                         }
1509                 } else {
1510                         count = 0;
1511                 }
1512
1513                 conf->lic_ncpts = count;
1514
1515                 if (tunables != NULL)
1516                         memcpy(tun, tunables, sizeof(*tunables));
1517
1518                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1519                 if (rc < 0) {
1520                         rc = -errno;
1521                         snprintf(err_str,
1522                                  LNET_MAX_STR_LEN,
1523                                  "\"cannot add network: %s\"", strerror(errno));
1524                         free(data);
1525                         return rc;
1526                 }
1527                 free(data);
1528                 i++;
1529         }
1530
1531         return LUSTRE_CFG_RC_NO_ERR;
1532 }
1533
1534 int
1535 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1536                            struct lnet_ioctl_config_lnd_tunables *tunables,
1537                            struct cfs_expr_list *global_cpts,
1538                            int seq_no, struct cYAML **err_rc)
1539 {
1540         lnet_nid_t *nids = NULL;
1541         __u32 nnids = 0;
1542         int rc;
1543         char err_str[LNET_MAX_STR_LEN];
1544
1545         snprintf(err_str, sizeof(err_str), "\"success\"");
1546
1547         if (!ip2nets) {
1548                 snprintf(err_str,
1549                          sizeof(err_str),
1550                          "\"incomplete ip2nets information\"");
1551                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1552                 goto out;
1553         }
1554
1555         /*
1556          * call below function to resolve the rules into a list of nids.
1557          * The memory is allocated in that function then freed here when
1558          * it's no longer needed.
1559          */
1560         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1561                                               sizeof(err_str));
1562         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1563                 goto out;
1564
1565         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1566                 snprintf(err_str, sizeof(err_str),
1567                          "\"no interfaces match ip2nets rules\"");
1568                 goto free_nids_out;
1569         }
1570
1571         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1572                                          tunables, global_cpts, nids,
1573                                          err_str);
1574
1575 free_nids_out:
1576         free(nids);
1577
1578 out:
1579         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1580         return rc;
1581 }
1582
1583 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1584                           struct cfs_expr_list *global_cpts,
1585                           char *ip2net,
1586                           struct lnet_ioctl_config_lnd_tunables *tunables,
1587                           int seq_no, struct cYAML **err_rc)
1588 {
1589         char *data = NULL;
1590         struct lnet_ioctl_config_ni *conf;
1591         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1592         char buf[LNET_MAX_STR_LEN];
1593         int rc = LUSTRE_CFG_RC_NO_ERR;
1594         char err_str[LNET_MAX_STR_LEN];
1595         lnet_nid_t *nids = NULL;
1596         __u32 nnids = 0;
1597         size_t len;
1598         int count;
1599         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1600         __u32 *cpt_array;
1601
1602         snprintf(err_str, sizeof(err_str), "\"success\"");
1603
1604         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1605             (list_empty(&nw_descr->nw_intflist) &&
1606              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1607                 snprintf(err_str,
1608                          sizeof(err_str),
1609                          "\"missing mandatory parameters in NI config: '%s'\"",
1610                          (nw_descr == NULL) ? "network , interface" :
1611                          (nw_descr->nw_id == 0) ? "network" : "interface");
1612                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1613                 goto out;
1614         }
1615
1616         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1617                 snprintf(err_str,
1618                          sizeof(err_str),
1619                          "\"ip2net string too long %d\"",
1620                                 (int)strlen(ip2net));
1621                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1622                 goto out;
1623         }
1624
1625         if (ip2net != NULL) {
1626                 if (tunables != NULL)
1627                         len = sizeof(struct lnet_ioctl_config_ni) +
1628                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1629                 else
1630                         len = sizeof(struct lnet_ioctl_config_ni);
1631                 data = calloc(1, len);
1632                 if (!data) {
1633                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1634                         goto out;
1635                 }
1636                 conf = (struct lnet_ioctl_config_ni*) data;
1637                 if (tunables != NULL)
1638                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1639                                 (data + sizeof(*conf));
1640
1641                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1642                 conf->lic_cfg_hdr.ioc_len = len;
1643                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1644                         LNET_MAX_STR_LEN);
1645
1646                 if (global_cpts != NULL) {
1647                         count = cfs_expr_list_values(global_cpts,
1648                                                      LNET_MAX_SHOW_NUM_CPT,
1649                                                      &cpt_array);
1650                         if (count > 0) {
1651                                 memcpy(conf->lic_cpts, cpt_array,
1652                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1653                                 free(cpt_array);
1654                         } else {
1655                                 count = 0;
1656                         }
1657                 } else {
1658                         count = 0;
1659                 }
1660
1661                 conf->lic_ncpts = count;
1662
1663                 if (tunables != NULL)
1664                         memcpy(tun, tunables, sizeof(*tunables));
1665
1666                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1667                 if (rc < 0) {
1668                         rc = -errno;
1669                         snprintf(err_str,
1670                                 sizeof(err_str),
1671                                 "\"cannot add network: %s\"", strerror(errno));
1672                         goto out;
1673                 }
1674
1675                 goto out;
1676         }
1677
1678         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1679                 rc = LUSTRE_CFG_RC_NO_ERR;
1680                 goto out;
1681         }
1682
1683         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1684                 snprintf(err_str,
1685                         sizeof(err_str),
1686                         "\"cannot parse net '%s'\"",
1687                         libcfs_net2str(nw_descr->nw_id));
1688                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1689                 goto out;
1690         }
1691
1692         /*
1693          * special case the GNI since no interface name is expected
1694          */
1695         if (list_empty(&nw_descr->nw_intflist) &&
1696             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1697                 snprintf(err_str,
1698                         sizeof(err_str),
1699                         "\"no interface name provided\"");
1700                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1701                 goto out;
1702         }
1703
1704         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1705                                    err_str, sizeof(err_str));
1706         if (rc != 0) {
1707                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1708                 goto out;
1709         }
1710
1711         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1712                                          tunables, global_cpts, nids,
1713                                          err_str);
1714
1715 out:
1716         if (nw_descr != NULL) {
1717                 list_for_each_entry_safe(intf_descr, tmp,
1718                                          &nw_descr->nw_intflist,
1719                                          intf_on_network) {
1720                         list_del(&intf_descr->intf_on_network);
1721                         free_intf_descr(intf_descr);
1722                 }
1723         }
1724
1725         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1726
1727         if (nids)
1728                 free(nids);
1729
1730         if (data)
1731                 free(data);
1732
1733         return rc;
1734 }
1735
1736 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1737                        int seq_no, struct cYAML **err_rc)
1738 {
1739         struct lnet_ioctl_config_ni data;
1740         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1741         char err_str[LNET_MAX_STR_LEN];
1742         lnet_nid_t *nids = NULL;
1743         __u32 nnids = 0;
1744         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1745
1746         snprintf(err_str, sizeof(err_str), "\"success\"");
1747
1748         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1749                 snprintf(err_str,
1750                          sizeof(err_str),
1751                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1752                          (nw_descr == NULL) ? "network , interface" :
1753                          (nw_descr->nw_id == 0) ? "network" : "interface");
1754                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1755                 goto out;
1756         }
1757
1758         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1759                 return LUSTRE_CFG_RC_NO_ERR;
1760
1761         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1762                 snprintf(err_str,
1763                          sizeof(err_str),
1764                          "\"cannot parse net '%s'\"",
1765                          libcfs_net2str(nw_descr->nw_id));
1766                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1767                 goto out;
1768         }
1769
1770         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1771                                    err_str, sizeof(err_str));
1772         if (rc != 0) {
1773                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1774                 goto out;
1775         }
1776
1777         /*
1778          * no interfaces just the nw_id is specified
1779          */
1780         if (nnids == 0) {
1781                 nids = calloc(1, sizeof(*nids));
1782                 if (nids == NULL) {
1783                         snprintf(err_str, sizeof(err_str),
1784                                 "\"out of memory\"");
1785                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1786                         goto out;
1787                 }
1788                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1789                 nnids = 1;
1790         }
1791
1792         for (i = 0; i < nnids; i++) {
1793                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1794                 data.lic_nid = nids[i];
1795
1796                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1797                 if (rc < 0) {
1798                         rc = -errno;
1799                         snprintf(err_str,
1800                                 sizeof(err_str),
1801                                 "\"cannot del network: %s\"", strerror(errno));
1802                 }
1803         }
1804
1805         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1806                                  intf_on_network) {
1807                 list_del(&intf_descr->intf_on_network);
1808                 free_intf_descr(intf_descr);
1809         }
1810
1811 out:
1812         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1813
1814         if (nids != NULL)
1815                 free(nids);
1816
1817         return rc;
1818 }
1819
1820 static bool
1821 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1822                           struct lnet_ioctl_comm_count *counts)
1823 {
1824         if (cYAML_create_number(yaml, "put",
1825                                 counts->ico_put_count)
1826                                         == NULL)
1827                 return false;
1828         if (cYAML_create_number(yaml, "get",
1829                                 counts->ico_get_count)
1830                                         == NULL)
1831                 return false;
1832         if (cYAML_create_number(yaml, "reply",
1833                                 counts->ico_reply_count)
1834                                         == NULL)
1835                 return false;
1836         if (cYAML_create_number(yaml, "ack",
1837                                 counts->ico_ack_count)
1838                                         == NULL)
1839                 return false;
1840         if (cYAML_create_number(yaml, "hello",
1841                                 counts->ico_hello_count)
1842                                         == NULL)
1843                 return false;
1844
1845         return true;
1846 }
1847
1848 static struct lnet_ioctl_comm_count *
1849 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1850 {
1851         if (idx == 0)
1852                 return &msg_stats->im_send_stats;
1853         if (idx == 1)
1854                 return &msg_stats->im_recv_stats;
1855         if (idx == 2)
1856                 return &msg_stats->im_drop_stats;
1857
1858         return NULL;
1859 }
1860
1861 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1862                          struct cYAML **show_rc, struct cYAML **err_rc,
1863                          bool backup)
1864 {
1865         char *buf;
1866         struct lnet_ioctl_config_ni *ni_data;
1867         struct lnet_ioctl_config_lnd_tunables *lnd;
1868         struct lnet_ioctl_element_stats *stats;
1869         struct lnet_ioctl_element_msg_stats msg_stats;
1870         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1871         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1872         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1873         int l_errno = 0;
1874         struct cYAML *root = NULL, *tunables = NULL,
1875                 *net_node = NULL, *interfaces = NULL,
1876                 *item = NULL, *first_seq = NULL,
1877                 *tmp = NULL, *statistics = NULL;
1878         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1879         char str_buf[str_buf_len];
1880         char *pos;
1881         char err_str[LNET_MAX_STR_LEN];
1882         bool exist = false, new_net = true;
1883         int net_num = 0;
1884         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1885
1886         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1887
1888         buf = calloc(1, buf_size);
1889         if (buf == NULL)
1890                 goto out;
1891
1892         ni_data = (struct lnet_ioctl_config_ni *)buf;
1893
1894         if (nw != NULL) {
1895                 net = libcfs_str2net(nw);
1896                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1897                         snprintf(err_str,
1898                                  sizeof(err_str),
1899                                  "\"cannot parse net '%s'\"", nw);
1900                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1901                         goto out;
1902                 }
1903         }
1904
1905         root = cYAML_create_object(NULL, NULL);
1906         if (root == NULL)
1907                 goto out;
1908
1909         net_node = cYAML_create_seq(root, "net");
1910         if (net_node == NULL)
1911                 goto out;
1912
1913         for (i = 0;; i++) {
1914                 pos = str_buf;
1915                 __u32 rc_net;
1916
1917                 memset(buf, 0, buf_size);
1918
1919                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1920                 /*
1921                  * set the ioc_len to the proper value since INIT assumes
1922                  * size of data
1923                  */
1924                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1925                 ni_data->lic_idx = i;
1926
1927                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1928                 if (rc != 0) {
1929                         l_errno = errno;
1930                         break;
1931                 }
1932
1933                 rc_net = LNET_NIDNET(ni_data->lic_nid);
1934
1935                 /* filter on provided data */
1936                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1937                     net != rc_net)
1938                         continue;
1939
1940                 /* if we're backing up don't store lo */
1941                 if (backup && LNET_NETTYP(rc_net) == LOLND)
1942                         continue;
1943
1944                 /* default rc to -1 in case we hit the goto */
1945                 rc = -1;
1946                 exist = true;
1947
1948                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1949                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1950                         (ni_data->lic_bulk + sizeof(*stats));
1951
1952                 if (rc_net != prev_net) {
1953                         prev_net = rc_net;
1954                         new_net = true;
1955                         net_num++;
1956                 }
1957
1958                 if (new_net) {
1959                         if (!cYAML_create_string(net_node, "net type",
1960                                                  libcfs_net2str(rc_net)))
1961                                 goto out;
1962
1963                         tmp = cYAML_create_seq(net_node, "local NI(s)");
1964                         if (tmp == NULL)
1965                                 goto out;
1966                         new_net = false;
1967                 }
1968
1969                 /* create the tree to be printed. */
1970                 item = cYAML_create_seq_item(tmp);
1971                 if (item == NULL)
1972                         goto out;
1973
1974                 if (first_seq == NULL)
1975                         first_seq = item;
1976
1977                 if (!backup &&
1978                     cYAML_create_string(item, "nid",
1979                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
1980                         goto out;
1981
1982                 if (!backup &&
1983                     cYAML_create_string(item,
1984                                         "status",
1985                                         (ni_data->lic_status ==
1986                                           LNET_NI_STATUS_UP) ?
1987                                             "up" : "down") == NULL)
1988                         goto out;
1989
1990                 /* don't add interfaces unless there is at least one
1991                  * interface */
1992                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1993                         interfaces = cYAML_create_object(item, "interfaces");
1994                         if (interfaces == NULL)
1995                                 goto out;
1996
1997                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
1998                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1999                                         snprintf(str_buf,
2000                                                  sizeof(str_buf), "%d", j);
2001                                         if (cYAML_create_string(interfaces,
2002                                                 str_buf,
2003                                                 ni_data->lic_ni_intf[j]) ==
2004                                                         NULL)
2005                                                 goto out;
2006                                 }
2007                         }
2008                 }
2009
2010                 if (detail) {
2011                         char *limit;
2012                         int k;
2013
2014                         if (backup)
2015                                 goto continue_without_msg_stats;
2016
2017                         statistics = cYAML_create_object(item, "statistics");
2018                         if (statistics == NULL)
2019                                 goto out;
2020
2021                         if (cYAML_create_number(statistics, "send_count",
2022                                                 stats->iel_send_count)
2023                                                         == NULL)
2024                                 goto out;
2025
2026                         if (cYAML_create_number(statistics, "recv_count",
2027                                                 stats->iel_recv_count)
2028                                                         == NULL)
2029                                 goto out;
2030
2031                         if (cYAML_create_number(statistics, "drop_count",
2032                                                 stats->iel_drop_count)
2033                                                         == NULL)
2034                                 goto out;
2035
2036                         if (detail < 2)
2037                                 goto continue_without_msg_stats;
2038
2039                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2040                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2041                         msg_stats.im_idx = i;
2042
2043                         rc = l_ioctl(LNET_DEV_ID,
2044                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2045                                      &msg_stats);
2046                         if (rc != 0) {
2047                                 l_errno = errno;
2048                                 goto continue_without_msg_stats;
2049                         }
2050
2051                         for (k = 0; k < 3; k++) {
2052                                 struct lnet_ioctl_comm_count *counts;
2053                                 struct cYAML *msg_statistics = NULL;
2054
2055                                 msg_statistics = cYAML_create_object(item,
2056                                                  (char *)gmsg_stat_names[k]);
2057                                 if (msg_statistics == NULL)
2058                                         goto out;
2059
2060                                 counts = get_counts(&msg_stats, k);
2061                                 if (counts == NULL)
2062                                         goto out;
2063
2064                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2065                                                                counts))
2066                                         goto out;
2067                         }
2068
2069 continue_without_msg_stats:
2070                         tunables = cYAML_create_object(item, "tunables");
2071                         if (!tunables)
2072                                 goto out;
2073
2074                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2075                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2076                                 goto out;
2077
2078                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2079                                                      &lnd->lt_tun);
2080                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2081                             rc != LUSTRE_CFG_RC_NO_MATCH)
2082                                 goto out;
2083
2084                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2085                                 tunables = cYAML_create_object(item,
2086                                                                "lnd tunables");
2087                                 if (tunables == NULL)
2088                                         goto out;
2089                         }
2090
2091                         if (!backup &&
2092                             cYAML_create_number(item, "dev cpt",
2093                                                 ni_data->lic_dev_cpt) == NULL)
2094                                 goto out;
2095
2096                         if (!backup &&
2097                             cYAML_create_number(item, "tcp bonding",
2098                                                 ni_data->lic_tcp_bonding)
2099                                                         == NULL)
2100                                 goto out;
2101
2102                         /* out put the CPTs in the format: "[x,x,x,...]" */
2103                         limit = str_buf + str_buf_len - 3;
2104                         pos += snprintf(pos, limit - pos, "\"[");
2105                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2106                                 j < ni_data->lic_ncpts &&
2107                                 pos < limit; j++) {
2108                                 pos += snprintf(pos, limit - pos,
2109                                                 "%d", ni_data->lic_cpts[j]);
2110                                 if ((j + 1) < ni_data->lic_ncpts)
2111                                         pos += snprintf(pos, limit - pos, ",");
2112                         }
2113                         pos += snprintf(pos, 3, "]\"");
2114
2115                         if (ni_data->lic_ncpts >= 1 &&
2116                             cYAML_create_string(item, "CPT",
2117                                                 str_buf) == NULL)
2118                                 goto out;
2119                 }
2120         }
2121
2122         /* Print out the net information only if show_rc is not provided */
2123         if (show_rc == NULL)
2124                 cYAML_print_tree(root);
2125
2126         if (l_errno != ENOENT) {
2127                 snprintf(err_str,
2128                          sizeof(err_str),
2129                          "\"cannot get networks: %s\"",
2130                          strerror(l_errno));
2131                 rc = -l_errno;
2132                 goto out;
2133         } else
2134                 rc = LUSTRE_CFG_RC_NO_ERR;
2135
2136         snprintf(err_str, sizeof(err_str), "\"success\"");
2137 out:
2138         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2139                 cYAML_free_tree(root);
2140         } else if (show_rc != NULL && *show_rc != NULL) {
2141                 struct cYAML *show_node;
2142                 /* find the net node, if one doesn't exist
2143                  * then insert one.  Otherwise add to the one there
2144                  */
2145                 show_node = cYAML_get_object_item(*show_rc, "net");
2146                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2147                         cYAML_insert_child(show_node, first_seq);
2148                         free(net_node);
2149                         free(root);
2150                 } else if (show_node == NULL) {
2151                         cYAML_insert_sibling((*show_rc)->cy_child,
2152                                                 net_node);
2153                         free(root);
2154                 } else {
2155                         cYAML_free_tree(root);
2156                 }
2157         } else {
2158                 *show_rc = root;
2159         }
2160
2161         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2162
2163         return rc;
2164 }
2165
2166 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2167 {
2168         struct lnet_ioctl_config_data data;
2169         int rc = LUSTRE_CFG_RC_NO_ERR;
2170         char err_str[LNET_MAX_STR_LEN];
2171
2172         snprintf(err_str, sizeof(err_str), "\"success\"");
2173
2174         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2175         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2176
2177         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2178         if (rc != 0) {
2179                 rc = -errno;
2180                 snprintf(err_str,
2181                          sizeof(err_str),
2182                          "\"cannot %s routing %s\"",
2183                          (enable) ? "enable" : "disable", strerror(errno));
2184                 goto out;
2185         }
2186
2187 out:
2188         cYAML_build_error(rc, seq_no,
2189                          (enable) ? ADD_CMD : DEL_CMD,
2190                          "routing", err_str, err_rc);
2191
2192         return rc;
2193 }
2194
2195 int ioctl_set_value(__u32 val, int ioc, char *name,
2196                     int seq_no, struct cYAML **err_rc)
2197 {
2198         struct lnet_ioctl_set_value data;
2199         int rc = LUSTRE_CFG_RC_NO_ERR;
2200         char err_str[LNET_MAX_STR_LEN];
2201
2202         snprintf(err_str, sizeof(err_str), "\"success\"");
2203
2204         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2205         data.sv_value = val;
2206
2207         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2208         if (rc != 0) {
2209                 rc = -errno;
2210                 snprintf(err_str,
2211                          sizeof(err_str),
2212                          "\"cannot configure %s to %d: %s\"", name,
2213                          val, strerror(errno));
2214         }
2215
2216         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2217
2218         return rc;
2219 }
2220
2221 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2222 {
2223         int rc = LUSTRE_CFG_RC_NO_ERR;
2224         char err_str[LNET_MAX_STR_LEN];
2225         char val[LNET_MAX_STR_LEN];
2226
2227         snprintf(err_str, sizeof(err_str), "\"success\"");
2228
2229         snprintf(val, sizeof(val), "%d", max);
2230
2231         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2232                               1, strlen(val) + 1);
2233         if (rc)
2234                 snprintf(err_str, sizeof(err_str),
2235                          "\"cannot configure max interfaces: %s\"",
2236                          strerror(errno));
2237
2238         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2239
2240         return rc;
2241 }
2242
2243 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2244 {
2245         int rc = LUSTRE_CFG_RC_NO_ERR;
2246         char err_str[LNET_MAX_STR_LEN];
2247         char val[LNET_MAX_STR_LEN];
2248
2249         snprintf(err_str, sizeof(err_str), "\"success\"");
2250
2251         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2252
2253         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2254                               1, strlen(val) + 1);
2255         if (rc)
2256                 snprintf(err_str, sizeof(err_str),
2257                          "\"cannot configure discovery: %s\"",
2258                          strerror(errno));
2259
2260         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2261
2262         return rc;
2263
2264 }
2265
2266 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2267 {
2268         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2269                                "numa_range", seq_no, err_rc);
2270 }
2271
2272 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2273                                struct cYAML **err_rc)
2274 {
2275         struct lnet_ioctl_config_data data;
2276         int rc = LUSTRE_CFG_RC_NO_ERR;
2277         char err_str[LNET_MAX_STR_LEN];
2278
2279         snprintf(err_str, sizeof(err_str), "\"success\"");
2280
2281         /* -1 indicates to ignore changes to this field */
2282         if (tiny < -1 || small < -1 || large < -1) {
2283                 snprintf(err_str,
2284                          sizeof(err_str),
2285                          "\"tiny, small and large must be >= 0\"");
2286                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2287                 goto out;
2288         }
2289
2290         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2291         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2292         data.cfg_config_u.cfg_buffers.buf_small = small;
2293         data.cfg_config_u.cfg_buffers.buf_large = large;
2294
2295         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2296         if (rc != 0) {
2297                 rc = -errno;
2298                 snprintf(err_str,
2299                          sizeof(err_str),
2300                          "\"cannot configure buffers: %s\"", strerror(errno));
2301                 goto out;
2302         }
2303
2304 out:
2305         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2306
2307         return rc;
2308 }
2309
2310 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2311                              struct cYAML **err_rc, bool backup)
2312 {
2313         struct lnet_ioctl_config_data *data;
2314         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2315         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2316         int l_errno = 0;
2317         char *buf;
2318         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2319         int buf_count[LNET_NRBPOOLS] = {0};
2320         struct cYAML *root = NULL, *pools_node = NULL,
2321                      *type_node = NULL, *item = NULL, *cpt = NULL,
2322                      *first_seq = NULL, *buffers = NULL;
2323         int i, j;
2324         char err_str[LNET_MAX_STR_LEN];
2325         char node_name[LNET_MAX_STR_LEN];
2326         bool exist = false;
2327
2328         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2329
2330         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2331         if (buf == NULL)
2332                 goto out;
2333
2334         data = (struct lnet_ioctl_config_data *)buf;
2335
2336         root = cYAML_create_object(NULL, NULL);
2337         if (root == NULL)
2338                 goto out;
2339
2340         if (backup)
2341                 pools_node = cYAML_create_object(root, "routing");
2342         else
2343                 pools_node = cYAML_create_seq(root, "routing");
2344         if (pools_node == NULL)
2345                 goto out;
2346
2347         for (i = 0;; i++) {
2348                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2349                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2350                                         sizeof(struct lnet_ioctl_pool_cfg);
2351                 data->cfg_count = i;
2352
2353                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2354                 if (rc != 0) {
2355                         l_errno = errno;
2356                         break;
2357                 }
2358
2359                 exist = true;
2360
2361                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2362
2363                 if (backup)
2364                         goto calculate_buffers;
2365
2366                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2367                 item = cYAML_create_seq_item(pools_node);
2368                 if (item == NULL)
2369                         goto out;
2370
2371                 if (first_seq == NULL)
2372                         first_seq = item;
2373
2374                 cpt = cYAML_create_object(item, node_name);
2375                 if (cpt == NULL)
2376                         goto out;
2377
2378 calculate_buffers:
2379                 /* create the tree  and print */
2380                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2381                         if (!backup) {
2382                                 type_node = cYAML_create_object(cpt, pools[j]);
2383                                 if (type_node == NULL)
2384                                         goto out;
2385                         }
2386                         if (!backup &&
2387                             cYAML_create_number(type_node, "npages",
2388                                                 pool_cfg->pl_pools[j].pl_npages)
2389                             == NULL)
2390                                 goto out;
2391                         if (!backup &&
2392                             cYAML_create_number(type_node, "nbuffers",
2393                                                 pool_cfg->pl_pools[j].
2394                                                   pl_nbuffers) == NULL)
2395                                 goto out;
2396                         if (!backup &&
2397                             cYAML_create_number(type_node, "credits",
2398                                                 pool_cfg->pl_pools[j].
2399                                                    pl_credits) == NULL)
2400                                 goto out;
2401                         if (!backup &&
2402                             cYAML_create_number(type_node, "mincredits",
2403                                                 pool_cfg->pl_pools[j].
2404                                                    pl_mincredits) == NULL)
2405                                 goto out;
2406                         /* keep track of the total count for each of the
2407                          * tiny, small and large buffers */
2408                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2409                 }
2410         }
2411
2412         if (pool_cfg != NULL) {
2413                 if (backup) {
2414                         if (cYAML_create_number(pools_node, "enable",
2415                                                 pool_cfg->pl_routing) ==
2416                         NULL)
2417                                 goto out;
2418
2419                         goto add_buffer_section;
2420                 }
2421
2422                 item = cYAML_create_seq_item(pools_node);
2423                 if (item == NULL)
2424                         goto out;
2425
2426                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2427                     NULL)
2428                         goto out;
2429         }
2430
2431 add_buffer_section:
2432         /* create a buffers entry in the show. This is necessary so that
2433          * if the YAML output is used to configure a node, the buffer
2434          * configuration takes hold */
2435         buffers = cYAML_create_object(root, "buffers");
2436         if (buffers == NULL)
2437                 goto out;
2438
2439         for (i = 0; i < LNET_NRBPOOLS; i++) {
2440                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2441                         goto out;
2442         }
2443
2444         if (show_rc == NULL)
2445                 cYAML_print_tree(root);
2446
2447         if (l_errno != ENOENT) {
2448                 snprintf(err_str,
2449                          sizeof(err_str),
2450                          "\"cannot get routing information: %s\"",
2451                          strerror(l_errno));
2452                 rc = -l_errno;
2453                 goto out;
2454         } else
2455                 rc = LUSTRE_CFG_RC_NO_ERR;
2456
2457         snprintf(err_str, sizeof(err_str), "\"success\"");
2458         rc = LUSTRE_CFG_RC_NO_ERR;
2459
2460 out:
2461         free(buf);
2462         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2463                 cYAML_free_tree(root);
2464         } else if (show_rc != NULL && *show_rc != NULL) {
2465                 struct cYAML *routing_node;
2466                 /* there should exist only one routing block and one
2467                  * buffers block. If there already exists a previous one
2468                  * then don't add another */
2469                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2470                 if (routing_node == NULL) {
2471                         cYAML_insert_sibling((*show_rc)->cy_child,
2472                                                 root->cy_child);
2473                         free(root);
2474                 } else {
2475                         cYAML_free_tree(root);
2476                 }
2477         } else {
2478                 *show_rc = root;
2479         }
2480
2481         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2482
2483         return rc;
2484 }
2485
2486 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2487                           struct cYAML **show_rc, struct cYAML **err_rc,
2488                           bool backup)
2489 {
2490         /*
2491          * TODO: This function is changing in a future patch to accommodate
2492          * PEER_LIST and proper filtering on any nid of the peer
2493          */
2494         struct lnet_ioctl_peer_cfg peer_info;
2495         struct lnet_peer_ni_credit_info *lpni_cri;
2496         struct lnet_ioctl_element_stats *lpni_stats;
2497         struct lnet_ioctl_element_msg_stats *msg_stats;
2498         lnet_nid_t *nidp;
2499         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2500         int i, j, k;
2501         int l_errno = 0;
2502         __u32 count;
2503         __u32 size;
2504         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2505                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2506                      *msg_statistics = NULL, *statistics = NULL;
2507         char err_str[LNET_MAX_STR_LEN];
2508         struct lnet_process_id *list = NULL;
2509         void *data = NULL;
2510         void *lpni_data;
2511
2512         snprintf(err_str, sizeof(err_str),
2513                  "\"out of memory\"");
2514
2515         /* create struct cYAML root object */
2516         root = cYAML_create_object(NULL, NULL);
2517         if (root == NULL)
2518                 goto out;
2519
2520         peer_root = cYAML_create_seq(root, "peer");
2521         if (peer_root == NULL)
2522                 goto out;
2523
2524         count = 1000;
2525         size = count * sizeof(struct lnet_process_id);
2526         list = malloc(size);
2527         if (list == NULL) {
2528                 l_errno = ENOMEM;
2529                 goto out;
2530         }
2531         if (knid != NULL) {
2532                 list[0].nid = libcfs_str2nid(knid);
2533                 count = 1;
2534         } else {
2535                 for (;;) {
2536                         memset(&peer_info, 0, sizeof(peer_info));
2537                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2538                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2539                         peer_info.prcfg_size = size;
2540                         peer_info.prcfg_bulk = list;
2541
2542                         l_errno = 0;
2543                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2544                                      &peer_info);
2545                         count = peer_info.prcfg_count;
2546                         if (rc == 0)
2547                                 break;
2548                         l_errno = errno;
2549                         if (l_errno != E2BIG) {
2550                                 snprintf(err_str,
2551                                         sizeof(err_str),
2552                                         "\"cannot get peer list: %s\"",
2553                                         strerror(l_errno));
2554                                 rc = -l_errno;
2555                                 goto out;
2556                         }
2557                         free(list);
2558                         size = peer_info.prcfg_size;
2559                         list = malloc(size);
2560                         if (list == NULL) {
2561                                 l_errno = ENOMEM;
2562                                 goto out;
2563                         }
2564                 }
2565         }
2566
2567         size = 4096;
2568         data = malloc(size);
2569         if (data == NULL) {
2570                 l_errno = ENOMEM;
2571                 goto out;
2572         }
2573         for (i = 0; i < count; i++) {
2574                 for (;;) {
2575                         memset(&peer_info, 0, sizeof(peer_info));
2576                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2577                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2578                         peer_info.prcfg_prim_nid = list[i].nid;
2579                         peer_info.prcfg_size = size;
2580                         peer_info.prcfg_bulk = data;
2581
2582                         l_errno = 0;
2583                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2584                                      &peer_info);
2585                         if (rc == 0)
2586                                 break;
2587                         l_errno = errno;
2588                         if (l_errno != E2BIG) {
2589                                 snprintf(err_str,
2590                                         sizeof(err_str),
2591                                         "\"cannot get peer information: %s\"",
2592                                         strerror(l_errno));
2593                                 rc = -l_errno;
2594                                 goto out;
2595                         }
2596                         free(data);
2597                         size = peer_info.prcfg_size;
2598                         data = malloc(size);
2599                         if (data == NULL) {
2600                                 l_errno = ENOMEM;
2601                                 goto out;
2602                         }
2603                 }
2604
2605                 peer = cYAML_create_seq_item(peer_root);
2606                 if (peer == NULL)
2607                         goto out;
2608
2609                 if (first_seq == NULL)
2610                         first_seq = peer;
2611
2612                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2613                 if (cYAML_create_string(peer, "primary nid",
2614                                         libcfs_nid2str(pnid))
2615                     == NULL)
2616                         goto out;
2617                 if (cYAML_create_string(peer, "Multi-Rail",
2618                                         peer_info.prcfg_mr ? "True" : "False")
2619                     == NULL)
2620                         goto out;
2621                 /*
2622                  * print out the state of the peer only if details are
2623                  * requested
2624                  */
2625                 if (detail >= 3) {
2626                         if (!backup &&
2627                             cYAML_create_number(peer, "peer state",
2628                                                 peer_info.prcfg_state)
2629                                 == NULL)
2630                                 goto out;
2631                 }
2632
2633                 tmp = cYAML_create_seq(peer, "peer ni");
2634                 if (tmp == NULL)
2635                         goto out;
2636
2637                 lpni_data = data;
2638                 for (j = 0; j < peer_info.prcfg_count; j++) {
2639                         nidp = lpni_data;
2640                         lpni_cri = (void*)nidp + sizeof(nidp);
2641                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2642                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2643                         lpni_data = (void *)msg_stats + sizeof(*msg_stats);
2644
2645                         peer_ni = cYAML_create_seq_item(tmp);
2646                         if (peer_ni == NULL)
2647                                 goto out;
2648
2649                         if (cYAML_create_string(peer_ni, "nid",
2650                                                 libcfs_nid2str(*nidp))
2651                             == NULL)
2652                                 goto out;
2653
2654                         if (backup)
2655                                 continue;
2656
2657                         if (cYAML_create_string(peer_ni, "state",
2658                                                 lpni_cri->cr_aliveness)
2659                             == NULL)
2660                                 goto out;
2661
2662                         if (!detail)
2663                                 continue;
2664
2665                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2666                                                 lpni_cri->cr_ni_peer_tx_credits)
2667                             == NULL)
2668                                 goto out;
2669
2670                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2671                                                 lpni_cri->cr_peer_tx_credits)
2672                             == NULL)
2673                                 goto out;
2674
2675                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2676                                                 lpni_cri->cr_peer_min_tx_credits)
2677                             == NULL)
2678                                 goto out;
2679
2680                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2681                                                 lpni_cri->cr_peer_tx_qnob)
2682                             == NULL)
2683                                 goto out;
2684
2685                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2686                                                 lpni_cri->cr_peer_rtr_credits)
2687                             == NULL)
2688                                 goto out;
2689
2690                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2691                                                 lpni_cri->cr_peer_min_rtr_credits)
2692                             == NULL)
2693                                 goto out;
2694
2695                         if (cYAML_create_number(peer_ni, "refcount",
2696                                                 lpni_cri->cr_refcount) == NULL)
2697                                 goto out;
2698
2699                         statistics = cYAML_create_object(peer_ni, "statistics");
2700                         if (statistics == NULL)
2701                                 goto out;
2702
2703                         if (cYAML_create_number(statistics, "send_count",
2704                                                 lpni_stats->iel_send_count)
2705                             == NULL)
2706                                 goto out;
2707
2708                         if (cYAML_create_number(statistics, "recv_count",
2709                                                 lpni_stats->iel_recv_count)
2710                             == NULL)
2711                                 goto out;
2712
2713                         if (cYAML_create_number(statistics, "drop_count",
2714                                                 lpni_stats->iel_drop_count)
2715                             == NULL)
2716                                 goto out;
2717
2718                         if (detail < 2)
2719                                 continue;
2720
2721                         for (k = 0; k < 3; k++) {
2722                                 struct lnet_ioctl_comm_count *counts;
2723
2724                                 msg_statistics = cYAML_create_object(peer_ni,
2725                                                  (char *) gmsg_stat_names[k]);
2726                                 if (msg_statistics == NULL)
2727                                         goto out;
2728
2729                                 counts = get_counts(msg_stats, k);
2730                                 if (counts == NULL)
2731                                         goto out;
2732
2733                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2734                                                                counts))
2735                                         goto out;
2736                         }
2737
2738                 }
2739         }
2740
2741         /* print output iff show_rc is not provided */
2742         if (show_rc == NULL)
2743                 cYAML_print_tree(root);
2744
2745         snprintf(err_str, sizeof(err_str), "\"success\"");
2746         rc = LUSTRE_CFG_RC_NO_ERR;
2747
2748 out:
2749         free(list);
2750         free(data);
2751         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2752                 cYAML_free_tree(root);
2753         } else if (show_rc != NULL && *show_rc != NULL) {
2754                 struct cYAML *show_node;
2755                 /* find the peer node, if one doesn't exist then
2756                  * insert one.  Otherwise add to the one there
2757                  */
2758                 show_node = cYAML_get_object_item(*show_rc,
2759                                                   "peer");
2760                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2761                         cYAML_insert_child(show_node, first_seq);
2762                         free(peer_root);
2763                         free(root);
2764                 } else if (show_node == NULL) {
2765                         cYAML_insert_sibling((*show_rc)->cy_child,
2766                                              peer_root);
2767                         free(root);
2768                 } else {
2769                         cYAML_free_tree(root);
2770                 }
2771         } else {
2772                 *show_rc = root;
2773         }
2774
2775         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2776                           err_rc);
2777
2778         return rc;
2779 }
2780
2781 int lustre_lnet_list_peer(int seq_no,
2782                           struct cYAML **show_rc, struct cYAML **err_rc)
2783 {
2784         struct lnet_ioctl_peer_cfg peer_info;
2785         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2786         __u32 count;
2787         __u32 size;
2788         int i = 0;
2789         int l_errno = 0;
2790         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
2791         char err_str[LNET_MAX_STR_LEN];
2792         struct lnet_process_id *list = NULL;
2793
2794         snprintf(err_str, sizeof(err_str),
2795                  "\"out of memory\"");
2796
2797         memset(&peer_info, 0, sizeof(peer_info));
2798
2799         /* create struct cYAML root object */
2800         root = cYAML_create_object(NULL, NULL);
2801         if (root == NULL)
2802                 goto out;
2803
2804         list_root = cYAML_create_seq(root, "peer list");
2805         if (list_root == NULL)
2806                 goto out;
2807
2808         count = 1000;
2809         size = count * sizeof(struct lnet_process_id);
2810         list = malloc(size);
2811         if (list == NULL) {
2812                 l_errno = ENOMEM;
2813                 goto out;
2814         }
2815         for (;;) {
2816                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2817                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2818                 peer_info.prcfg_size = size;
2819                 peer_info.prcfg_bulk = list;
2820
2821                 l_errno = 0;
2822                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
2823                 count = peer_info.prcfg_count;
2824                 if (rc == 0)
2825                         break;
2826                 l_errno = errno;
2827                 if (l_errno != E2BIG) {
2828                         snprintf(err_str,
2829                                 sizeof(err_str),
2830                                 "\"cannot get peer list: %s\"",
2831                                 strerror(l_errno));
2832                         rc = -l_errno;
2833                         goto out;
2834                 }
2835                 free(list);
2836                 size = peer_info.prcfg_size;
2837                 list = malloc(size);
2838                 if (list == NULL) {
2839                         l_errno = ENOMEM;
2840                         goto out;
2841                 }
2842         }
2843
2844         /* count is now the actual number of ids in the list. */
2845         for (i = 0; i < count; i++) {
2846                 if (cYAML_create_string(list_root, "nid",
2847                                         libcfs_nid2str(list[i].nid))
2848                     == NULL)
2849                         goto out;
2850         }
2851
2852         /* print output iff show_rc is not provided */
2853         if (show_rc == NULL)
2854                 cYAML_print_tree(root);
2855
2856         snprintf(err_str, sizeof(err_str), "\"success\"");
2857         rc = LUSTRE_CFG_RC_NO_ERR;
2858
2859 out:
2860         if (list != NULL)
2861                 free(list);
2862         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2863                 cYAML_free_tree(root);
2864         } else if (show_rc != NULL && *show_rc != NULL) {
2865                 struct cYAML *show_node;
2866                 /* find the peer node, if one doesn't exist then
2867                  * insert one.  Otherwise add to the one there
2868                  */
2869                 show_node = cYAML_get_object_item(*show_rc,
2870                                                   "peer");
2871                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2872                         cYAML_insert_child(show_node, first_seq);
2873                         free(list_root);
2874                         free(root);
2875                 } else if (show_node == NULL) {
2876                         cYAML_insert_sibling((*show_rc)->cy_child,
2877                                              list_root);
2878                         free(root);
2879                 } else {
2880                         cYAML_free_tree(root);
2881                 }
2882         } else {
2883                 *show_rc = root;
2884         }
2885
2886         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2887                           err_rc);
2888
2889         return rc;
2890 }
2891
2892 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2893                           struct cYAML *root)
2894 {
2895         struct cYAML *show_node;
2896
2897         show_node = cYAML_get_object_item(show_rc, "global");
2898         if (show_node != NULL)
2899                 cYAML_insert_sibling(show_node->cy_child,
2900                                      node->cy_child);
2901         else
2902                 cYAML_insert_sibling(show_rc->cy_child,
2903                                      node);
2904         free(root);
2905 }
2906
2907 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
2908                                    char *name, __u32 value,
2909                                    struct cYAML **show_rc,
2910                                    struct cYAML **err_rc, int err)
2911 {
2912         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2913         struct cYAML *root = NULL, *global = NULL;
2914
2915         if (err) {
2916                 rc = err;
2917                 goto out;
2918         }
2919
2920         root = cYAML_create_object(NULL, NULL);
2921         if (root == NULL)
2922                 goto out;
2923
2924         global = cYAML_create_object(root, "global");
2925         if (global == NULL)
2926                 goto out;
2927
2928         if (cYAML_create_number(global, name,
2929                                 value) == NULL)
2930                 goto out;
2931
2932         if (show_rc == NULL)
2933                 cYAML_print_tree(root);
2934
2935         snprintf(err_str, err_len, "\"success\"");
2936
2937         rc = LUSTRE_CFG_RC_NO_ERR;
2938
2939 out:
2940         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2941                 cYAML_free_tree(root);
2942         } else if (show_rc != NULL && *show_rc != NULL) {
2943                 add_to_global(*show_rc, global, root);
2944         } else {
2945                 *show_rc = root;
2946         }
2947
2948         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2949
2950         return rc;
2951 }
2952
2953 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
2954                                     struct cYAML **show_rc,
2955                                     struct cYAML **err_rc)
2956 {
2957         struct lnet_ioctl_set_value data;
2958         int rc;
2959         int l_errno = 0;
2960         char err_str[LNET_MAX_STR_LEN];
2961
2962         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2963
2964         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2965
2966         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
2967         if (rc != 0) {
2968                 l_errno = -errno;
2969