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