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