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