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