Whamcloud - gitweb
LU-9480 lnet: tune lnet_peer_discovery_disabled with lnetctl
[fs/lustre-release.git] / lnet / utils / lnetconfig / liblnetconfig.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * LGPL HEADER END
20  *
21  * Copyright (c) 2014, 2016, Intel Corporation.
22  *
23  * Author:
24  *   Amir Shehata <amir.shehata@intel.com>
25  */
26
27 /*
28  * There are two APIs:
29  *  1. APIs that take the actual parameters expanded.  This is for other
30  *  entities that would like to link against the library and call the APIs
31  *  directly without having to form an intermediate representation.
32  *  2. APIs that take a YAML file and parses out the information there and
33  *  calls the APIs mentioned in 1
34  */
35
36 #include <errno.h>
37 #include <limits.h>
38 #include <byteswap.h>
39 #include <netdb.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <net/if.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
48 #include "liblnd.h"
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <ifaddrs.h>
53 #include "liblnetconfig.h"
54 #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_discovery(int enable, int seq_no, struct cYAML **err_rc)
1863 {
1864         int rc = LUSTRE_CFG_RC_NO_ERR;
1865         char err_str[LNET_MAX_STR_LEN];
1866         char val[LNET_MAX_STR_LEN];
1867
1868         snprintf(err_str, sizeof(err_str), "\"success\"");
1869
1870         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
1871
1872         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
1873                               1, strlen(val) + 1);
1874         if (rc)
1875                 snprintf(err_str, sizeof(err_str),
1876                          "\"cannot configure discovery: %s\"",
1877                          strerror(errno));
1878
1879         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
1880
1881         return rc;
1882
1883 }
1884
1885 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1886 {
1887         struct lnet_ioctl_set_value data;
1888         int rc = LUSTRE_CFG_RC_NO_ERR;
1889         char err_str[LNET_MAX_STR_LEN];
1890
1891         snprintf(err_str, sizeof(err_str), "\"success\"");
1892
1893         if (range < 0) {
1894                 snprintf(err_str,
1895                          sizeof(err_str),
1896                          "\"range must be >= 0\"");
1897                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1898                 goto out;
1899         }
1900
1901         LIBCFS_IOC_INIT_V2(data, sv_hdr);
1902         data.sv_value = range;
1903
1904         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1905         if (rc != 0) {
1906                 rc = -errno;
1907                 snprintf(err_str,
1908                          sizeof(err_str),
1909                          "\"cannot configure numa_range: %s\"", strerror(errno));
1910                 goto out;
1911         }
1912
1913 out:
1914         cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1915
1916         return rc;
1917 }
1918
1919 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1920                                struct cYAML **err_rc)
1921 {
1922         struct lnet_ioctl_config_data data;
1923         int rc = LUSTRE_CFG_RC_NO_ERR;
1924         char err_str[LNET_MAX_STR_LEN];
1925
1926         snprintf(err_str, sizeof(err_str), "\"success\"");
1927
1928         /* -1 indicates to ignore changes to this field */
1929         if (tiny < -1 || small < -1 || large < -1) {
1930                 snprintf(err_str,
1931                          sizeof(err_str),
1932                          "\"tiny, small and large must be >= 0\"");
1933                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1934                 goto out;
1935         }
1936
1937         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1938         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1939         data.cfg_config_u.cfg_buffers.buf_small = small;
1940         data.cfg_config_u.cfg_buffers.buf_large = large;
1941
1942         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1943         if (rc != 0) {
1944                 rc = -errno;
1945                 snprintf(err_str,
1946                          sizeof(err_str),
1947                          "\"cannot configure buffers: %s\"", strerror(errno));
1948                 goto out;
1949         }
1950
1951 out:
1952         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1953
1954         return rc;
1955 }
1956
1957 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1958                              struct cYAML **err_rc)
1959 {
1960         struct lnet_ioctl_config_data *data;
1961         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1962         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1963         int l_errno = 0;
1964         char *buf;
1965         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1966         int buf_count[LNET_NRBPOOLS] = {0};
1967         struct cYAML *root = NULL, *pools_node = NULL,
1968                      *type_node = NULL, *item = NULL, *cpt = NULL,
1969                      *first_seq = NULL, *buffers = NULL;
1970         int i, j;
1971         char err_str[LNET_MAX_STR_LEN];
1972         char node_name[LNET_MAX_STR_LEN];
1973         bool exist = false;
1974
1975         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1976
1977         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1978         if (buf == NULL)
1979                 goto out;
1980
1981         data = (struct lnet_ioctl_config_data *)buf;
1982
1983         root = cYAML_create_object(NULL, NULL);
1984         if (root == NULL)
1985                 goto out;
1986
1987         pools_node = cYAML_create_seq(root, "routing");
1988         if (pools_node == NULL)
1989                 goto out;
1990
1991         for (i = 0;; i++) {
1992                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1993                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1994                                         sizeof(struct lnet_ioctl_pool_cfg);
1995                 data->cfg_count = i;
1996
1997                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1998                 if (rc != 0) {
1999                         l_errno = errno;
2000                         break;
2001                 }
2002
2003                 exist = true;
2004
2005                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2006
2007                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2008                 item = cYAML_create_seq_item(pools_node);
2009                 if (item == NULL)
2010                         goto out;
2011
2012                 if (first_seq == NULL)
2013                         first_seq = item;
2014
2015                 cpt = cYAML_create_object(item, node_name);
2016                 if (cpt == NULL)
2017                         goto out;
2018
2019                 /* create the tree  and print */
2020                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2021                         type_node = cYAML_create_object(cpt, pools[j]);
2022                         if (type_node == NULL)
2023                                 goto out;
2024                         if (cYAML_create_number(type_node, "npages",
2025                                                 pool_cfg->pl_pools[j].pl_npages)
2026                             == NULL)
2027                                 goto out;
2028                         if (cYAML_create_number(type_node, "nbuffers",
2029                                                 pool_cfg->pl_pools[j].
2030                                                   pl_nbuffers) == NULL)
2031                                 goto out;
2032                         if (cYAML_create_number(type_node, "credits",
2033                                                 pool_cfg->pl_pools[j].
2034                                                    pl_credits) == NULL)
2035                                 goto out;
2036                         if (cYAML_create_number(type_node, "mincredits",
2037                                                 pool_cfg->pl_pools[j].
2038                                                    pl_mincredits) == NULL)
2039                                 goto out;
2040                         /* keep track of the total count for each of the
2041                          * tiny, small and large buffers */
2042                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2043                 }
2044         }
2045
2046         if (pool_cfg != NULL) {
2047                 item = cYAML_create_seq_item(pools_node);
2048                 if (item == NULL)
2049                         goto out;
2050
2051                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2052                     NULL)
2053                         goto out;
2054         }
2055
2056         /* create a buffers entry in the show. This is necessary so that
2057          * if the YAML output is used to configure a node, the buffer
2058          * configuration takes hold */
2059         buffers = cYAML_create_object(root, "buffers");
2060         if (buffers == NULL)
2061                 goto out;
2062
2063         for (i = 0; i < LNET_NRBPOOLS; i++) {
2064                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2065                         goto out;
2066         }
2067
2068         if (show_rc == NULL)
2069                 cYAML_print_tree(root);
2070
2071         if (l_errno != ENOENT) {
2072                 snprintf(err_str,
2073                          sizeof(err_str),
2074                          "\"cannot get routing information: %s\"",
2075                          strerror(l_errno));
2076                 rc = -l_errno;
2077                 goto out;
2078         } else
2079                 rc = LUSTRE_CFG_RC_NO_ERR;
2080
2081         snprintf(err_str, sizeof(err_str), "\"success\"");
2082         rc = LUSTRE_CFG_RC_NO_ERR;
2083
2084 out:
2085         free(buf);
2086         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2087                 cYAML_free_tree(root);
2088         } else if (show_rc != NULL && *show_rc != NULL) {
2089                 struct cYAML *routing_node;
2090                 /* there should exist only one routing block and one
2091                  * buffers block. If there already exists a previous one
2092                  * then don't add another */
2093                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2094                 if (routing_node == NULL) {
2095                         cYAML_insert_sibling((*show_rc)->cy_child,
2096                                                 root->cy_child);
2097                         free(root);
2098                 } else {
2099                         cYAML_free_tree(root);
2100                 }
2101         } else {
2102                 *show_rc = root;
2103         }
2104
2105         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2106
2107         return rc;
2108 }
2109
2110 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2111                           struct cYAML **show_rc, struct cYAML **err_rc)
2112 {
2113         /*
2114          * TODO: This function is changing in a future patch to accommodate
2115          * PEER_LIST and proper filtering on any nid of the peer
2116          */
2117         struct lnet_ioctl_peer_cfg peer_info;
2118         struct lnet_peer_ni_credit_info *lpni_cri;
2119         struct lnet_ioctl_element_stats *lpni_stats;
2120         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2121         int l_errno = 0;
2122         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2123                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2124         char err_str[LNET_MAX_STR_LEN];
2125         lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2126         int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2127         char *data = malloc(data_size);
2128         bool new_peer = true;
2129
2130         snprintf(err_str, sizeof(err_str),
2131                  "\"out of memory\"");
2132
2133         if (data == NULL)
2134                 goto out;
2135
2136         /* create struct cYAML root object */
2137         root = cYAML_create_object(NULL, NULL);
2138         if (root == NULL)
2139                 goto out;
2140
2141         peer_root = cYAML_create_seq(root, "peer");
2142         if (peer_root == NULL)
2143                 goto out;
2144
2145         if (knid != NULL)
2146                 primary_nid = libcfs_str2nid(knid);
2147
2148         do {
2149                 for (i = 0;; i++) {
2150                         memset(data, 0, data_size);
2151                         memset(&peer_info, 0, sizeof(peer_info));
2152                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2153                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2154                         peer_info.prcfg_count = i;
2155                         peer_info.prcfg_bulk = (void *)data;
2156                         peer_info.prcfg_size = data_size;
2157
2158                         rc = l_ioctl(LNET_DEV_ID,
2159                                      IOC_LIBCFS_GET_PEER_NI, &peer_info);
2160                         if (rc != 0) {
2161                                 l_errno = errno;
2162                                 break;
2163                         }
2164
2165                         if (primary_nid != LNET_NID_ANY &&
2166                             primary_nid != peer_info.prcfg_prim_nid)
2167                                         continue;
2168
2169                         lpni_cri = peer_info.prcfg_bulk;
2170                         lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2171
2172                         peer = cYAML_create_seq_item(peer_root);
2173                         if (peer == NULL)
2174                                 goto out;
2175
2176                         if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2177                                 prev_primary_nid = peer_info.prcfg_prim_nid;
2178                                 new_peer = true;
2179                         }
2180
2181                         if (new_peer) {
2182                                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2183                                 if (cYAML_create_string(peer, "primary nid",
2184                                                         libcfs_nid2str(pnid))
2185                                     == NULL)
2186                                         goto out;
2187                                 if (cYAML_create_string(peer, "Multi-Rail",
2188                                                         peer_info.prcfg_mr ?
2189                                                         "True" : "False")
2190                                     == NULL)
2191                                         goto out;
2192                                 tmp = cYAML_create_seq(peer, "peer ni");
2193                                 if (tmp == NULL)
2194                                         goto out;
2195                                 new_peer = false;
2196                         }
2197
2198                         if (first_seq == NULL)
2199                                 first_seq = peer;
2200
2201                         peer_ni = cYAML_create_seq_item(tmp);
2202                         if (peer_ni == NULL)
2203                                 goto out;
2204
2205                         if (cYAML_create_string(peer_ni, "nid",
2206                                                 libcfs_nid2str
2207                                                  (peer_info.prcfg_cfg_nid))
2208                             == NULL)
2209                                 goto out;
2210
2211                         if (cYAML_create_string(peer_ni, "state",
2212                                                 lpni_cri->cr_aliveness)
2213                             == NULL)
2214                                 goto out;
2215
2216                         if (!detail)
2217                                 continue;
2218
2219                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2220                                                 lpni_cri->cr_ni_peer_tx_credits)
2221                             == NULL)
2222                                 goto out;
2223
2224                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2225                                                 lpni_cri->cr_peer_tx_credits)
2226                             == NULL)
2227                                 goto out;
2228
2229                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2230                                                 lpni_cri->cr_peer_min_tx_credits)
2231                             == NULL)
2232                                 goto out;
2233
2234                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2235                                                 lpni_cri->cr_peer_tx_qnob)
2236                             == NULL)
2237                                 goto out;
2238
2239                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2240                                                 lpni_cri->cr_peer_rtr_credits)
2241                             == NULL)
2242                                 goto out;
2243
2244                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2245                                                 lpni_cri->cr_peer_min_rtr_credits)
2246                             == NULL)
2247                                 goto out;
2248
2249                         if (cYAML_create_number(peer_ni, "send_count",
2250                                                 lpni_stats->iel_send_count)
2251                             == NULL)
2252                                 goto out;
2253
2254                         if (cYAML_create_number(peer_ni, "recv_count",
2255                                                 lpni_stats->iel_recv_count)
2256                             == NULL)
2257                                 goto out;
2258
2259                         if (cYAML_create_number(peer_ni, "drop_count",
2260                                                 lpni_stats->iel_drop_count)
2261                             == NULL)
2262                                 goto out;
2263
2264                         if (cYAML_create_number(peer_ni, "refcount",
2265                                                 lpni_cri->cr_refcount) == NULL)
2266                                 goto out;
2267                 }
2268
2269                 if (l_errno != ENOENT) {
2270                         snprintf(err_str,
2271                                 sizeof(err_str),
2272                                 "\"cannot get peer information: %s\"",
2273                                 strerror(l_errno));
2274                         rc = -l_errno;
2275                         goto out;
2276                 }
2277
2278                 j++;
2279         } while (j < ncpt);
2280
2281         /* print output iff show_rc is not provided */
2282         if (show_rc == NULL)
2283                 cYAML_print_tree(root);
2284
2285         snprintf(err_str, sizeof(err_str), "\"success\"");
2286         rc = LUSTRE_CFG_RC_NO_ERR;
2287
2288 out:
2289         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2290                 cYAML_free_tree(root);
2291         } else if (show_rc != NULL && *show_rc != NULL) {
2292                 struct cYAML *show_node;
2293                 /* find the peer node, if one doesn't exist then
2294                  * insert one.  Otherwise add to the one there
2295                  */
2296                 show_node = cYAML_get_object_item(*show_rc,
2297                                                   "peer");
2298                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2299                         cYAML_insert_child(show_node, first_seq);
2300                         free(peer_root);
2301                         free(root);
2302                 } else if (show_node == NULL) {
2303                         cYAML_insert_sibling((*show_rc)->cy_child,
2304                                              peer_root);
2305                         free(root);
2306                 } else {
2307                         cYAML_free_tree(root);
2308                 }
2309         } else {
2310                 *show_rc = root;
2311         }
2312
2313         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2314                           err_rc);
2315
2316         return rc;
2317 }
2318
2319 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
2320                           struct cYAML *root)
2321 {
2322         struct cYAML *show_node;
2323
2324         show_node = cYAML_get_object_item(show_rc, "global");
2325         if (show_node != NULL)
2326                 cYAML_insert_sibling(show_node->cy_child,
2327                                      node->cy_child);
2328         else
2329                 cYAML_insert_sibling(show_rc->cy_child,
2330                                      node);
2331         free(root);
2332 }
2333
2334 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
2335                               struct cYAML **err_rc)
2336 {
2337         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2338         char val[LNET_MAX_STR_LEN];
2339         int max_intf;
2340         char err_str[LNET_MAX_STR_LEN];
2341         struct cYAML *root = NULL, *global = NULL;
2342
2343         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2344
2345         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2346                              1, LNET_MAX_STR_LEN);
2347         if (rc) {
2348                 snprintf(err_str, sizeof(err_str),
2349                          "\"cannot get max interfaces: %d\"", rc);
2350                 goto out;
2351         }
2352
2353         max_intf = atoi(val);
2354
2355         root = cYAML_create_object(NULL, NULL);
2356         if (root == NULL)
2357                 goto out;
2358
2359         global = cYAML_create_object(root, "global");
2360         if (global == NULL)
2361                 goto out;
2362
2363         if (cYAML_create_number(global, "max_intf",
2364                                 max_intf) == NULL)
2365                 goto out;
2366
2367         if (show_rc == NULL)
2368                 cYAML_print_tree(root);
2369
2370         snprintf(err_str, sizeof(err_str), "\"success\"");
2371 out:
2372         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR)
2373                 cYAML_free_tree(root);
2374         else if (show_rc != NULL && *show_rc != NULL)
2375                 add_to_global(*show_rc, global, root);
2376         else
2377                 *show_rc = root;
2378
2379         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2380
2381         return rc;
2382 }
2383
2384 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
2385                                struct cYAML **err_rc)
2386 {
2387         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2388         char val[LNET_MAX_STR_LEN];
2389         __u32 discovery;
2390         char err_str[LNET_MAX_STR_LEN];
2391         struct cYAML *root = NULL, *global = NULL;
2392
2393         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2394
2395         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2396                              1, sizeof(val));
2397         if (rc) {
2398                 snprintf(err_str, sizeof(err_str),
2399                          "\"cannot get discovery value: %d\"", rc);
2400                 goto out;
2401         }
2402
2403         /*
2404          * the sysfs parameter read indicates if discovery is disabled,
2405          * but we report if discovery is enabled.
2406          */
2407         discovery = !atoi(val);
2408
2409         root = cYAML_create_object(NULL, NULL);
2410         if (root == NULL)
2411                 goto out;
2412
2413         global = cYAML_create_object(root, "global");
2414         if (global == NULL)
2415                 goto out;
2416
2417         if (cYAML_create_number(global, "discovery",
2418                                 discovery) == NULL)
2419                 goto out;
2420
2421         if (show_rc == NULL)
2422                 cYAML_print_tree(root);
2423
2424         snprintf(err_str, sizeof(err_str), "\"success\"");
2425 out:
2426         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2427                 cYAML_free_tree(root);
2428         } else if (show_rc != NULL && *show_rc != NULL) {
2429                 add_to_global(*show_rc, global, root);
2430         } else {
2431                 *show_rc = root;
2432         }
2433
2434         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2435
2436         return rc;
2437 }
2438
2439 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2440                                 struct cYAML **err_rc)
2441 {
2442         struct lnet_ioctl_set_value data;
2443         int rc;
2444         int l_errno;
2445         char err_str[LNET_MAX_STR_LEN];
2446         struct cYAML *root = NULL, *global = NULL;
2447
2448         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2449
2450         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2451
2452         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2453         if (rc != 0) {
2454                 l_errno = errno;
2455                 snprintf(err_str,
2456                          sizeof(err_str),
2457                          "\"cannot get numa range: %s\"",
2458                          strerror(l_errno));
2459                 rc = -l_errno;
2460                 goto out;
2461         }
2462
2463         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2464
2465         root = cYAML_create_object(NULL, NULL);
2466         if (root == NULL)
2467                 goto out;
2468
2469         global = cYAML_create_object(root, "global");
2470         if (global == NULL)
2471                 goto out;
2472
2473         if (cYAML_create_number(global, "numa_range",
2474                                 data.sv_value) == NULL)
2475                 goto out;
2476
2477         if (show_rc == NULL)
2478                 cYAML_print_tree(root);
2479
2480         snprintf(err_str, sizeof(err_str), "\"success\"");
2481         rc = LUSTRE_CFG_RC_NO_ERR;
2482 out:
2483         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2484                 cYAML_free_tree(root);
2485         } else if (show_rc != NULL && *show_rc != NULL) {
2486                 add_to_global(*show_rc, global, root);
2487         } else {
2488                 *show_rc = root;
2489         }
2490
2491         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
2492
2493         return rc;
2494 }
2495
2496 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2497                            struct cYAML **err_rc)
2498 {
2499         struct lnet_ioctl_lnet_stats data;
2500         int rc;
2501         int l_errno;
2502         char err_str[LNET_MAX_STR_LEN];
2503         struct cYAML *root = NULL, *stats = NULL;
2504
2505         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2506
2507         LIBCFS_IOC_INIT_V2(data, st_hdr);
2508
2509         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2510         if (rc != 0) {
2511                 l_errno = errno;
2512                 snprintf(err_str,
2513                          sizeof(err_str),
2514                          "\"cannot get lnet statistics: %s\"",
2515                          strerror(l_errno));
2516                 rc = -l_errno;
2517                 goto out;
2518         }
2519
2520         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2521
2522         root = cYAML_create_object(NULL, NULL);
2523         if (root == NULL)
2524                 goto out;
2525
2526         stats = cYAML_create_object(root, "statistics");
2527         if (stats == NULL)
2528                 goto out;
2529
2530         if (cYAML_create_number(stats, "msgs_alloc",
2531                                 data.st_cntrs.msgs_alloc) == NULL)
2532                 goto out;
2533
2534         if (cYAML_create_number(stats, "msgs_max",
2535                                 data.st_cntrs.msgs_max) == NULL)
2536                 goto out;
2537
2538         if (cYAML_create_number(stats, "errors",
2539                                 data.st_cntrs.errors) == NULL)
2540                 goto out;
2541
2542         if (cYAML_create_number(stats, "send_count",
2543                                 data.st_cntrs.send_count) == NULL)
2544                 goto out;
2545
2546         if (cYAML_create_number(stats, "recv_count",
2547                                 data.st_cntrs.recv_count) == NULL)
2548                 goto out;
2549
2550         if (cYAML_create_number(stats, "route_count",
2551                                 data.st_cntrs.route_count) == NULL)
2552                 goto out;
2553
2554         if (cYAML_create_number(stats, "drop_count",
2555                                 data.st_cntrs.drop_count) == NULL)
2556                 goto out;
2557
2558         if (cYAML_create_number(stats, "send_length",
2559                                 data.st_cntrs.send_length) == NULL)
2560                 goto out;
2561
2562         if (cYAML_create_number(stats, "recv_length",
2563                                 data.st_cntrs.recv_length) == NULL)
2564                 goto out;
2565
2566         if (cYAML_create_number(stats, "route_length",
2567                                 data.st_cntrs.route_length) == NULL)
2568                 goto out;
2569
2570         if (cYAML_create_number(stats, "drop_length",
2571                                 data.st_cntrs.drop_length) == NULL)
2572                 goto out;
2573
2574         if (show_rc == NULL)
2575                 cYAML_print_tree(root);
2576
2577         snprintf(err_str, sizeof(err_str), "\"success\"");
2578         rc = LUSTRE_CFG_RC_NO_ERR;
2579 out:
2580         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2581                 cYAML_free_tree(root);
2582         } else if (show_rc != NULL && *show_rc != NULL) {
2583                 cYAML_insert_sibling((*show_rc)->cy_child,
2584                                         root->cy_child);
2585                 free(root);
2586         } else {
2587                 *show_rc = root;
2588         }
2589
2590         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2591
2592         return rc;
2593 }
2594
2595 typedef int (*cmd_handler_t)(struct cYAML *tree,
2596                              struct cYAML **show_rc,
2597                              struct cYAML **err_rc);
2598
2599 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2600                                     struct cYAML **err_rc)
2601 {
2602         struct cYAML *net, *gw, *hop, *prio, *seq_no;
2603
2604         net = cYAML_get_object_item(tree, "net");
2605         gw = cYAML_get_object_item(tree, "gateway");
2606         hop = cYAML_get_object_item(tree, "hop");
2607         prio = cYAML_get_object_item(tree, "priority");
2608         seq_no = cYAML_get_object_item(tree, "seq_no");
2609
2610         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2611                                         (gw) ? gw->cy_valuestring : NULL,
2612                                         (hop) ? hop->cy_valueint : -1,
2613                                         (prio) ? prio->cy_valueint : -1,
2614                                         (seq_no) ? seq_no->cy_valueint : -1,
2615                                         err_rc);
2616 }
2617
2618 static void yaml_free_string_array(char **array, int num)
2619 {
2620         int i;
2621         char **sub_array = array;
2622
2623         for (i = 0; i < num; i++) {
2624                 if (*sub_array != NULL)
2625                         free(*sub_array);
2626                 sub_array++;
2627         }
2628         if (array)
2629                 free(array);
2630 }
2631
2632 /*
2633  *    interfaces:
2634  *        0: <intf_name>['['<expr>']']
2635  *        1: <intf_name>['['<expr>']']
2636  */
2637 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2638                                struct lnet_dlc_network_descr *nw_descr)
2639 {
2640         struct cYAML *child = NULL;
2641         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2642         struct lnet_dlc_intf_descr *intf_descr, *tmp;
2643
2644         if (intf_tree == NULL || nw_descr == NULL)
2645                 return LUSTRE_CFG_RC_BAD_PARAM;
2646
2647         /* now grab all the interfaces and their cpts */
2648         child = intf_tree->cy_child;
2649         while (child != NULL) {
2650                 if (child->cy_valuestring == NULL) {
2651                         child = child->cy_next;
2652                         continue;
2653                 }
2654
2655                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2656                         goto failed;
2657
2658                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2659                                                 child->cy_valuestring,
2660                                                 strlen(child->cy_valuestring));
2661                 if (rc != LUSTRE_CFG_RC_NO_ERR)
2662                         goto failed;
2663
2664                 intf_num++;
2665                 child = child->cy_next;
2666         }
2667
2668         if (intf_num == 0)
2669                 return LUSTRE_CFG_RC_MISSING_PARAM;
2670
2671         return intf_num;
2672
2673 failed:
2674         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2675                                  intf_on_network) {
2676                 list_del(&intf_descr->intf_on_network);
2677                 free_intf_descr(intf_descr);
2678         }
2679
2680         return rc;
2681 }
2682
2683 static bool
2684 yaml_extract_cmn_tunables(struct cYAML *tree,
2685                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2686                           struct cfs_expr_list **global_cpts)
2687 {
2688         struct cYAML *tun, *item, *smp;
2689         int rc;
2690
2691         tun = cYAML_get_object_item(tree, "tunables");
2692         if (tun != NULL) {
2693                 item = cYAML_get_object_item(tun, "peer_timeout");
2694                 if (item != NULL)
2695                         tunables->lct_peer_timeout = item->cy_valueint;
2696                 item = cYAML_get_object_item(tun, "peer_credits");
2697                 if (item != NULL)
2698                         tunables->lct_peer_tx_credits = item->cy_valueint;
2699                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2700                 if (item != NULL)
2701                         tunables->lct_peer_rtr_credits = item->cy_valueint;
2702                 item = cYAML_get_object_item(tun, "credits");
2703                 if (item != NULL)
2704                         tunables->lct_max_tx_credits = item->cy_valueint;
2705                 smp = cYAML_get_object_item(tun, "CPT");
2706                 if (smp != NULL) {
2707                         rc = cfs_expr_list_parse(smp->cy_valuestring,
2708                                                  strlen(smp->cy_valuestring),
2709                                                  0, UINT_MAX, global_cpts);
2710                         if (rc != 0)
2711                                 *global_cpts = NULL;
2712                 }
2713
2714                 return true;
2715         }
2716
2717         return false;
2718 }
2719
2720 static bool
2721 yaml_extract_tunables(struct cYAML *tree,
2722                       struct lnet_ioctl_config_lnd_tunables *tunables,
2723                       struct cfs_expr_list **global_cpts,
2724                       __u32 net_type)
2725 {
2726         bool rc;
2727
2728         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2729                                        global_cpts);
2730
2731         if (!rc)
2732                 return rc;
2733
2734         lustre_yaml_extract_lnd_tunables(tree, net_type,
2735                                          &tunables->lt_tun);
2736
2737         return rc;
2738 }
2739
2740 /*
2741  * net:
2742  *    - net type: <net>[<NUM>]
2743   *      local NI(s):
2744  *        - nid: <ip>@<net>[<NUM>]
2745  *          status: up
2746  *          interfaces:
2747  *               0: <intf_name>['['<expr>']']
2748  *               1: <intf_name>['['<expr>']']
2749  *        tunables:
2750  *               peer_timeout: <NUM>
2751  *               peer_credits: <NUM>
2752  *               peer_buffer_credits: <NUM>
2753  *               credits: <NUM>
2754 *         lnd tunables:
2755  *               peercredits_hiw: <NUM>
2756  *               map_on_demand: <NUM>
2757  *               concurrent_sends: <NUM>
2758  *               fmr_pool_size: <NUM>
2759  *               fmr_flush_trigger: <NUM>
2760  *               fmr_cache: <NUM>
2761  *
2762  * At least one interface is required. If no interfaces are provided the
2763  * network interface can not be configured.
2764  */
2765 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2766                                  struct cYAML **err_rc)
2767 {
2768         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2769                      *item = NULL;
2770         int num_entries = 0, rc;
2771         struct lnet_dlc_network_descr nw_descr;
2772         struct cfs_expr_list *global_cpts = NULL;
2773         struct lnet_ioctl_config_lnd_tunables tunables;
2774         bool found = false;
2775
2776         memset(&tunables, 0, sizeof(tunables));
2777
2778         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2779         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2780
2781         ip2net = cYAML_get_object_item(tree, "ip2net");
2782         net = cYAML_get_object_item(tree, "net type");
2783         if (net)
2784                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2785         else
2786                 nw_descr.nw_id = LOLND;
2787
2788         /*
2789          * if neither net nor ip2nets are present, then we can not
2790          * configure the network.
2791          */
2792         if (!net && !ip2net)
2793                 return LUSTRE_CFG_RC_MISSING_PARAM;
2794
2795         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2796         if (local_nis == NULL)
2797                 return LUSTRE_CFG_RC_MISSING_PARAM;
2798
2799         if (!cYAML_is_sequence(local_nis))
2800                 return LUSTRE_CFG_RC_BAD_PARAM;
2801
2802         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2803                 intf = cYAML_get_object_item(item, "interfaces");
2804                 if (intf == NULL)
2805                         continue;
2806                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2807                 if (num_entries <= 0) {
2808                         cYAML_build_error(num_entries, -1, "ni", "add",
2809                                         "bad interface list",
2810                                         err_rc);
2811                         return LUSTRE_CFG_RC_BAD_PARAM;
2812                 }
2813         }
2814
2815         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2816                                       LNET_NETTYP(nw_descr.nw_id));
2817         seq_no = cYAML_get_object_item(tree, "seq_no");
2818
2819         rc = lustre_lnet_config_ni(&nw_descr,
2820                                    global_cpts,
2821                                    (ip2net) ? ip2net->cy_valuestring : NULL,
2822                                    (found) ? &tunables: NULL,
2823                                    (seq_no) ? seq_no->cy_valueint : -1,
2824                                    err_rc);
2825
2826         if (global_cpts != NULL)
2827                 cfs_expr_list_free(global_cpts);
2828
2829         return rc;
2830 }
2831
2832 /*
2833  * ip2nets:
2834  *  - net-spec: <tcp|o2ib|gni>[NUM]
2835  *    interfaces:
2836  *        0: <intf name>['['<expr>']']
2837  *        1: <intf name>['['<expr>']']
2838  *    ip-range:
2839  *        0: <expr.expr.expr.expr>
2840  *        1: <expr.expr.expr.expr>
2841  */
2842 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2843                                       struct cYAML **show_rc,
2844                                       struct cYAML **err_rc)
2845 {
2846         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2847                      *seq_no = NULL;
2848         struct lustre_lnet_ip2nets ip2nets;
2849         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2850                                           *tmp = NULL;
2851         int rc = LUSTRE_CFG_RC_NO_ERR;
2852         struct cfs_expr_list *global_cpts = NULL;
2853         struct cfs_expr_list *el, *el_tmp;
2854         struct lnet_ioctl_config_lnd_tunables tunables;
2855         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2856         bool found = false;
2857
2858         memset(&tunables, 0, sizeof(tunables));
2859
2860         /* initialize all lists */
2861         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2862         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2863         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2864
2865         net = cYAML_get_object_item(tree, "net-spec");
2866         if (net == NULL)
2867                 return LUSTRE_CFG_RC_BAD_PARAM;
2868
2869         if (net != NULL && net->cy_valuestring == NULL)
2870                 return LUSTRE_CFG_RC_BAD_PARAM;
2871
2872         /* assign the network id */
2873         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2874         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2875                 return LUSTRE_CFG_RC_BAD_PARAM;
2876
2877         seq_no = cYAML_get_object_item(tree, "seq_no");
2878
2879         intf = cYAML_get_object_item(tree, "interfaces");
2880         if (intf != NULL) {
2881                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2882                 if (rc <= 0)
2883                         return LUSTRE_CFG_RC_BAD_PARAM;
2884         }
2885
2886         ip_range = cYAML_get_object_item(tree, "ip-range");
2887         if (ip_range != NULL) {
2888                 item = ip_range->cy_child;
2889                 while (item != NULL) {
2890                         if (item->cy_valuestring == NULL) {
2891                                 item = item->cy_next;
2892                                 continue;
2893                         }
2894
2895                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2896                                                       item->cy_valuestring);
2897
2898                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2899                                 goto out;
2900
2901                         item = item->cy_next;
2902                 }
2903         }
2904
2905         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2906                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2907
2908         rc = lustre_lnet_config_ip2nets(&ip2nets,
2909                         (found) ? &tunables : NULL,
2910                         global_cpts,
2911                         (seq_no) ? seq_no->cy_valueint : -1,
2912                         err_rc);
2913
2914         /*
2915          * don't stop because there was no match. Continue processing the
2916          * rest of the rules. If non-match then nothing is configured
2917          */
2918         if (rc == LUSTRE_CFG_RC_NO_MATCH)
2919                 rc = LUSTRE_CFG_RC_NO_ERR;
2920 out:
2921         list_for_each_entry_safe(intf_descr, intf_tmp,
2922                                  &ip2nets.ip2nets_net.nw_intflist,
2923                                  intf_on_network) {
2924                 list_del(&intf_descr->intf_on_network);
2925                 free_intf_descr(intf_descr);
2926         }
2927
2928         list_for_each_entry_safe(ip_range_descr, tmp,
2929                                  &ip2nets.ip2nets_ip_ranges,
2930                                  ipr_entry) {
2931                 list_del(&ip_range_descr->ipr_entry);
2932                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2933                                          el_link) {
2934                         list_del(&el->el_link);
2935                         cfs_expr_list_free(el);
2936                 }
2937                 free(ip_range_descr);
2938         }
2939
2940         return rc;
2941 }
2942
2943 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2944                               struct cYAML **err_rc)
2945 {
2946         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2947                      *local_nis = NULL;
2948         int num_entries, rc;
2949         struct lnet_dlc_network_descr nw_descr;
2950
2951         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2952         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2953
2954         net = cYAML_get_object_item(tree, "net type");
2955         if (net != NULL)
2956                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2957
2958         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2959         if (local_nis == NULL)
2960                 return LUSTRE_CFG_RC_MISSING_PARAM;
2961
2962         if (!cYAML_is_sequence(local_nis))
2963                 return LUSTRE_CFG_RC_BAD_PARAM;
2964
2965         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2966                 intf = cYAML_get_object_item(item, "interfaces");
2967                 if (intf == NULL)
2968                         continue;
2969                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2970                 if (num_entries <= 0) {
2971                         cYAML_build_error(num_entries, -1, "ni", "add",
2972                                         "bad interface list",
2973                                         err_rc);
2974                         return LUSTRE_CFG_RC_BAD_PARAM;
2975                 }
2976         }
2977
2978         seq_no = cYAML_get_object_item(tree, "seq_no");
2979
2980         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2981                                 (seq_no) ? seq_no->cy_valueint : -1,
2982                                 err_rc);
2983
2984         return rc;
2985 }
2986
2987 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
2988 {
2989         struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
2990                      *prim_nid = NULL;
2991         char **nids = NULL;
2992         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2993
2994         prim_nid = cYAML_get_object_item(tree, "primary nid");
2995         if (!prim_nid || !prim_nid->cy_valuestring)
2996                 return LUSTRE_CFG_RC_MISSING_PARAM;
2997
2998         nids_entry = cYAML_get_object_item(tree, "peer ni");
2999         if (cYAML_is_sequence(nids_entry)) {
3000                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
3001                         entry = cYAML_get_object_item(child, "nid");
3002                         /* don't count an empty entry */
3003                         if (!entry || !entry->cy_valuestring)
3004                                 continue;
3005
3006                         if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
3007                                         == 0) && del) {
3008                                 /*
3009                                  * primary nid is present in the list of
3010                                  * nids so that means we want to delete
3011                                  * the entire peer, so no need to go
3012                                  * further. Just delete the entire peer.
3013                                  */
3014                                 return 0;
3015                         }
3016
3017                         num++;
3018                 }
3019         }
3020
3021         if (num == 0)
3022                 return LUSTRE_CFG_RC_MISSING_PARAM;
3023
3024         nids = calloc(sizeof(*nids) * num, 1);
3025         if (nids == NULL)
3026                 return LUSTRE_CFG_RC_OUT_OF_MEM;
3027
3028         /* now grab all the nids */
3029         num = 0;
3030         child = NULL;
3031         while (cYAML_get_next_seq_item(nids_entry, &child)) {
3032                 entry = cYAML_get_object_item(child, "nid");
3033                 if (!entry || !entry->cy_valuestring)
3034                         continue;
3035
3036                 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
3037                 if (!nids[num]) {
3038                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3039                         goto failed;
3040                 }
3041                 strncpy(nids[num], entry->cy_valuestring,
3042                         strlen(entry->cy_valuestring));
3043                 num++;
3044         }
3045         rc = num;
3046
3047         *nidsppp = nids;
3048         return rc;
3049
3050 failed:
3051         if (nids != NULL)
3052                 yaml_free_string_array(nids, num);
3053         *nidsppp = NULL;
3054         return rc;
3055 }
3056
3057 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
3058                                    struct cYAML **err_rc)
3059 {
3060         char **nids = NULL;
3061         int num, rc;
3062         struct cYAML *seq_no, *prim_nid, *non_mr;
3063
3064         num = yaml_copy_peer_nids(tree, &nids, false);
3065         if (num < 0)
3066                 return num;
3067
3068         seq_no = cYAML_get_object_item(tree, "seq_no");
3069         prim_nid = cYAML_get_object_item(tree, "primary nid");
3070         non_mr = cYAML_get_object_item(tree, "non_mr");
3071
3072         rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3073                                          nids, num,
3074                                          (non_mr) ? false : true,
3075                                          (seq_no) ? seq_no->cy_valueint : -1,
3076                                          err_rc);
3077
3078         yaml_free_string_array(nids, num);
3079         return rc;
3080 }
3081
3082 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
3083                                 struct cYAML **err_rc)
3084 {
3085         char **nids = NULL;
3086         int num, rc;
3087         struct cYAML *seq_no, *prim_nid;
3088
3089         num = yaml_copy_peer_nids(tree, &nids, true);
3090         if (num < 0)
3091                 return num;
3092
3093         seq_no = cYAML_get_object_item(tree, "seq_no");
3094         prim_nid = cYAML_get_object_item(tree, "primary nid");
3095
3096         rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
3097                                       nids, num,
3098                                       (seq_no) ? seq_no->cy_valueint : -1,
3099                                       err_rc);
3100
3101         yaml_free_string_array(nids, num);
3102         return rc;
3103 }
3104
3105 static int handle_yaml_config_buffers(struct cYAML *tree,
3106                                       struct cYAML **show_rc,
3107                                       struct cYAML **err_rc)
3108 {
3109         int rc;
3110         struct cYAML *tiny, *small, *large, *seq_no;
3111
3112         tiny = cYAML_get_object_item(tree, "tiny");
3113         small = cYAML_get_object_item(tree, "small");
3114         large = cYAML_get_object_item(tree, "large");
3115         seq_no = cYAML_get_object_item(tree, "seq_no");
3116
3117         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
3118                                         (small) ? small->cy_valueint : -1,
3119                                         (large) ? large->cy_valueint : -1,
3120                                         (seq_no) ? seq_no->cy_valueint : -1,
3121                                         err_rc);
3122
3123         return rc;
3124 }
3125
3126 static int handle_yaml_config_routing(struct cYAML *tree,
3127                                       struct cYAML **show_rc,
3128                                       struct cYAML **err_rc)
3129 {
3130         int rc = LUSTRE_CFG_RC_NO_ERR;
3131         struct cYAML *seq_no, *enable;
3132
3133         seq_no = cYAML_get_object_item(tree, "seq_no");
3134         enable = cYAML_get_object_item(tree, "enable");
3135
3136         if (enable) {
3137                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
3138                                                 (seq_no) ?
3139                                                     seq_no->cy_valueint : -1,
3140                                                 err_rc);
3141         }
3142
3143         return rc;
3144 }
3145
3146 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
3147                                  struct cYAML **err_rc)
3148 {
3149         struct cYAML *net;
3150         struct cYAML *gw;
3151         struct cYAML *seq_no;
3152
3153         net = cYAML_get_object_item(tree, "net");
3154         gw = cYAML_get_object_item(tree, "gateway");
3155         seq_no = cYAML_get_object_item(tree, "seq_no");
3156
3157         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
3158                                      (gw) ? gw->cy_valuestring : NULL,
3159                                      (seq_no) ? seq_no->cy_valueint : -1,
3160                                      err_rc);
3161 }
3162
3163 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
3164                                    struct cYAML **err_rc)
3165 {
3166         struct cYAML *seq_no;
3167
3168         seq_no = cYAML_get_object_item(tree, "seq_no");
3169
3170         return lustre_lnet_enable_routing(0, (seq_no) ?
3171                                                 seq_no->cy_valueint : -1,
3172                                         err_rc);
3173 }
3174
3175 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
3176                                   struct cYAML **err_rc)
3177 {
3178         struct cYAML *net;
3179         struct cYAML *gw;
3180         struct cYAML *hop;
3181         struct cYAML *prio;
3182         struct cYAML *detail;
3183         struct cYAML *seq_no;
3184
3185         net = cYAML_get_object_item(tree, "net");
3186         gw = cYAML_get_object_item(tree, "gateway");
3187         hop = cYAML_get_object_item(tree, "hop");
3188         prio = cYAML_get_object_item(tree, "priority");
3189         detail = cYAML_get_object_item(tree, "detail");
3190         seq_no = cYAML_get_object_item(tree, "seq_no");
3191
3192         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
3193                                       (gw) ? gw->cy_valuestring : NULL,
3194                                       (hop) ? hop->cy_valueint : -1,
3195                                       (prio) ? prio->cy_valueint : -1,
3196                                       (detail) ? detail->cy_valueint : 0,
3197                                       (seq_no) ? seq_no->cy_valueint : -1,
3198                                       show_rc,
3199                                       err_rc);
3200 }
3201
3202 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
3203                                 struct cYAML **err_rc)
3204 {
3205         struct cYAML *net, *detail, *seq_no;
3206
3207         net = cYAML_get_object_item(tree, "net");
3208         detail = cYAML_get_object_item(tree, "detail");
3209         seq_no = cYAML_get_object_item(tree, "seq_no");
3210
3211         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
3212                                     (detail) ? detail->cy_valueint : 0,
3213                                     (seq_no) ? seq_no->cy_valueint : -1,
3214                                     show_rc,
3215                                     err_rc);
3216 }
3217
3218 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
3219                                     struct cYAML **err_rc)
3220 {
3221         struct cYAML *seq_no;
3222
3223         seq_no = cYAML_get_object_item(tree, "seq_no");
3224
3225         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3226                                         show_rc, err_rc);
3227 }
3228
3229 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3230                                   struct cYAML **err_rc)
3231 {
3232         struct cYAML *seq_no, *nid, *detail;
3233
3234         seq_no = cYAML_get_object_item(tree, "seq_no");
3235         detail = cYAML_get_object_item(tree, "detail");
3236         nid = cYAML_get_object_item(tree, "nid");
3237
3238         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3239                                      (detail) ? detail->cy_valueint : 0,
3240                                      (seq_no) ? seq_no->cy_valueint : -1,
3241                                      show_rc, err_rc);
3242 }
3243
3244 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3245                                   struct cYAML **err_rc)
3246 {
3247         struct cYAML *seq_no;
3248
3249         seq_no = cYAML_get_object_item(tree, "seq_no");
3250
3251         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3252                                       show_rc, err_rc);
3253 }
3254
3255 static int handle_yaml_config_global_settings(struct cYAML *tree,
3256                                               struct cYAML **show_rc,
3257                                               struct cYAML **err_rc)
3258 {
3259         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3260         int rc = 0;
3261
3262         seq_no = cYAML_get_object_item(tree, "seq_no");
3263         max_intf = cYAML_get_object_item(tree, "max_intf");
3264         if (max_intf)
3265                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
3266                                                  seq_no ? seq_no->cy_valueint
3267                                                         : -1,
3268                                                  err_rc);
3269
3270         numa = cYAML_get_object_item(tree, "numa_range");
3271         if (numa)
3272                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
3273                                                    seq_no ? seq_no->cy_valueint
3274                                                         : -1,
3275                                                    err_rc);
3276
3277         discovery = cYAML_get_object_item(tree, "discovery");
3278         if (discovery)
3279                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
3280                                                   seq_no ? seq_no->cy_valueint
3281                                                         : -1,
3282                                                   err_rc);
3283
3284         return rc;
3285 }
3286
3287 static int handle_yaml_del_global_settings(struct cYAML *tree,
3288                                            struct cYAML **show_rc,
3289                                            struct cYAML **err_rc)
3290 {
3291         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3292         int rc = 0;
3293
3294         seq_no = cYAML_get_object_item(tree, "seq_no");
3295         max_intf = cYAML_get_object_item(tree, "max_intf");
3296         if (max_intf)
3297                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
3298                                                  seq_no ? seq_no->cy_valueint
3299                                                         : -1,
3300                                                  err_rc);
3301
3302         numa = cYAML_get_object_item(tree, "numa_range");
3303         if (numa)
3304                 rc = lustre_lnet_config_numa_range(0,
3305                                                    seq_no ? seq_no->cy_valueint
3306                                                         : -1,
3307                                                    err_rc);
3308
3309         /* peer discovery is enabled by default */
3310         discovery = cYAML_get_object_item(tree, "discovery");
3311         if (discovery)
3312                 rc = lustre_lnet_config_discovery(1,
3313                                                   seq_no ? seq_no->cy_valueint
3314                                                         : -1,
3315                                                   err_rc);
3316
3317         return rc;
3318 }
3319
3320 static int handle_yaml_show_global_settings(struct cYAML *tree,
3321                                             struct cYAML **show_rc,
3322                                             struct cYAML **err_rc)
3323 {
3324         struct cYAML *max_intf, *numa, *discovery, *seq_no;
3325         int rc = 0;
3326
3327         seq_no = cYAML_get_object_item(tree, "seq_no");
3328         max_intf = cYAML_get_object_item(tree, "max_intf");
3329         if (max_intf)
3330                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
3331                                                         : -1,
3332                                                 show_rc, err_rc);
3333
3334         numa = cYAML_get_object_item(tree, "numa_range");
3335         if (numa)
3336                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
3337                                                         : -1,
3338                                                  show_rc, err_rc);
3339
3340         discovery = cYAML_get_object_item(tree, "discovery");
3341         if (discovery)
3342                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
3343                                                         : -1,
3344                                                 show_rc, err_rc);
3345
3346         return rc;
3347 }
3348
3349 struct lookup_cmd_hdlr_tbl {
3350         char *name;
3351         cmd_handler_t cb;
3352 };
3353
3354 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3355         { .name = "route",      .cb = handle_yaml_config_route },
3356         { .name = "net",        .cb = handle_yaml_config_ni },
3357         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
3358         { .name = "peer",       .cb = handle_yaml_config_peer },
3359         { .name = "routing",    .cb = handle_yaml_config_routing },
3360         { .name = "buffers",    .cb = handle_yaml_config_buffers },
3361         { .name = "global",     .cb = handle_yaml_config_global_settings},
3362         { .name = NULL } };
3363
3364 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3365         { .name = "route",      .cb = handle_yaml_del_route },
3366         { .name = "net",        .cb = handle_yaml_del_ni },
3367         { .name = "peer",       .cb = handle_yaml_del_peer },
3368         { .name = "routing",    .cb = handle_yaml_del_routing },
3369         { .name = "global",     .cb = handle_yaml_del_global_settings},
3370         { .name = NULL } };
3371
3372 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3373         { .name = "route",      .cb = handle_yaml_show_route },
3374         { .name = "net",        .cb = handle_yaml_show_net },
3375         { .name = "buffers",    .cb = handle_yaml_show_routing },
3376         { .name = "routing",    .cb = handle_yaml_show_routing },
3377         { .name = "peer",       .cb = handle_yaml_show_peers },
3378         { .name = "statistics", .cb = handle_yaml_show_stats },
3379         { .name = "global",     .cb = handle_yaml_show_global_settings},
3380         { .name = NULL } };
3381
3382 static cmd_handler_t lookup_fn(char *key,
3383                                struct lookup_cmd_hdlr_tbl *tbl)
3384 {
3385         int i;
3386         if (key == NULL)
3387                 return NULL;
3388
3389         for (i = 0; tbl[i].name != NULL; i++) {
3390                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3391                         return tbl[i].cb;
3392         }
3393
3394         return NULL;
3395 }
3396
3397 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3398                                  struct cYAML **show_rc, struct cYAML **err_rc)
3399 {
3400         struct cYAML *tree, *item = NULL, *head, *child;
3401         cmd_handler_t cb;
3402         char err_str[LNET_MAX_STR_LEN];
3403         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3404
3405         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3406         if (tree == NULL)
3407                 return LUSTRE_CFG_RC_BAD_PARAM;
3408
3409         child = tree->cy_child;
3410         while (child != NULL) {
3411                 cb = lookup_fn(child->cy_string, table);
3412                 if (cb == NULL) {
3413                         snprintf(err_str, sizeof(err_str),
3414                                 "\"call back for '%s' not found\"",
3415                                 child->cy_string);
3416                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3417                                         "yaml", "helper", err_str, err_rc);
3418                         goto out;
3419                 }
3420
3421                 if (cYAML_is_sequence(child)) {
3422                         while ((head = cYAML_get_next_seq_item(child, &item))
3423                                != NULL) {
3424                                 rc = cb(head, show_rc, err_rc);
3425                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3426                                         return_rc = rc;
3427                         }
3428                 } else {
3429                         rc = cb(child, show_rc, err_rc);
3430                         if (rc != LUSTRE_CFG_RC_NO_ERR)
3431                                 return_rc = rc;
3432                 }
3433                 item = NULL;
3434                 child = child->cy_next;
3435         }
3436
3437 out:
3438         cYAML_free_tree(tree);
3439
3440         return return_rc;
3441 }
3442
3443 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3444 {
3445         return lustre_yaml_cb_helper(f, lookup_config_tbl,
3446                                      NULL, err_rc);
3447 }
3448
3449 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3450 {
3451         return lustre_yaml_cb_helper(f, lookup_del_tbl,
3452                                      NULL, err_rc);
3453 }
3454
3455 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3456 {
3457         return lustre_yaml_cb_helper(f, lookup_show_tbl,
3458                                      show_rc, err_rc);
3459 }
3460