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