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