Whamcloud - gitweb
2630d7ed880471fb5e4c57e6264de72acf15d351
[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 || num_nids == 0) {
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 (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 (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 (tunables != NULL)
1201                         memcpy(tun, tunables, sizeof(*tunables));
1202
1203                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1204                 if (rc < 0) {
1205                         rc = -errno;
1206                         snprintf(err_str,
1207                                  LNET_MAX_STR_LEN,
1208                                  "\"cannot add network: %s\"", strerror(errno));
1209                         free(data);
1210                         return rc;
1211                 }
1212                 free(data);
1213                 i++;
1214         }
1215
1216         return LUSTRE_CFG_RC_NO_ERR;
1217 }
1218
1219 int
1220 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1221                            struct lnet_ioctl_config_lnd_tunables *tunables,
1222                            struct cfs_expr_list *global_cpts,
1223                            int seq_no, struct cYAML **err_rc)
1224 {
1225         lnet_nid_t *nids = NULL;
1226         __u32 nnids = 0;
1227         int rc;
1228         char err_str[LNET_MAX_STR_LEN];
1229
1230         snprintf(err_str, sizeof(err_str), "\"success\"");
1231
1232         if (!ip2nets) {
1233                 snprintf(err_str,
1234                          sizeof(err_str),
1235                          "\"incomplete ip2nets information\"");
1236                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1237                 goto out;
1238         }
1239
1240         /*
1241          * call below function to resolve the rules into a list of nids.
1242          * The memory is allocated in that function then freed here when
1243          * it's no longer needed.
1244          */
1245         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids);
1246         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH) {
1247                 snprintf(err_str,
1248                          sizeof(err_str),
1249                          "\"cannot resolve ip2nets rule\"");
1250                 goto out;
1251         }
1252
1253         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1254                 snprintf(err_str, sizeof(err_str),
1255                          "\"no interfaces match ip2nets rules\"");
1256                 goto free_nids_out;
1257         }
1258
1259         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1260                                          tunables, global_cpts, nids,
1261                                          err_str);
1262
1263 free_nids_out:
1264         free(nids);
1265
1266 out:
1267         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1268         return rc;
1269 }
1270
1271 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1272                           struct cfs_expr_list *global_cpts,
1273                           char *ip2net,
1274                           struct lnet_ioctl_config_lnd_tunables *tunables,
1275                           int seq_no, struct cYAML **err_rc)
1276 {
1277         char *data = NULL;
1278         struct lnet_ioctl_config_ni *conf;
1279         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1280         char buf[LNET_MAX_STR_LEN];
1281         int rc = LUSTRE_CFG_RC_NO_ERR;
1282         char err_str[LNET_MAX_STR_LEN];
1283         lnet_nid_t *nids = NULL;
1284         __u32 nnids = 0;
1285         size_t len;
1286         int count;
1287         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1288         __u32 *cpt_array;
1289
1290         snprintf(err_str, sizeof(err_str), "\"success\"");
1291
1292         if (ip2net == NULL && nw_descr == NULL) {
1293                 snprintf(err_str,
1294                          sizeof(err_str),
1295                          "\"mandatory parameters not specified.\"");
1296                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1297                 goto out;
1298         }
1299
1300         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1301                 snprintf(err_str,
1302                          sizeof(err_str),
1303                          "\"ip2net string too long %d\"",
1304                                 (int)strlen(ip2net));
1305                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1306                 goto out;
1307         }
1308
1309         if (ip2net != NULL) {
1310                 if (tunables != NULL)
1311                         len = sizeof(struct lnet_ioctl_config_ni) +
1312                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1313                 else
1314                         len = sizeof(struct lnet_ioctl_config_ni);
1315                 data = calloc(1, len);
1316                 if (!data) {
1317                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1318                         goto out;
1319                 }
1320                 conf = (struct lnet_ioctl_config_ni*) data;
1321                 if (tunables != NULL)
1322                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1323                                 (data + sizeof(*conf));
1324
1325                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1326                 conf->lic_cfg_hdr.ioc_len = len;
1327                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1328                         LNET_MAX_STR_LEN);
1329
1330                 if (global_cpts != NULL) {
1331                         count = cfs_expr_list_values(global_cpts,
1332                                                      LNET_MAX_SHOW_NUM_CPT,
1333                                                      &cpt_array);
1334                         if (count > 0) {
1335                                 memcpy(conf->lic_cpts, cpt_array,
1336                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1337                                 free(cpt_array);
1338                         } else {
1339                                 count = 0;
1340                         }
1341                 } else {
1342                         count = 0;
1343                 }
1344
1345                 conf->lic_ncpts = count;
1346
1347                 if (tunables != NULL)
1348                         memcpy(tun, tunables, sizeof(*tunables));
1349
1350                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1351                 if (rc < 0) {
1352                         rc = -errno;
1353                         snprintf(err_str,
1354                                 sizeof(err_str),
1355                                 "\"cannot add network: %s\"", strerror(errno));
1356                         goto out;
1357                 }
1358
1359                 goto out;
1360         }
1361
1362         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1363                 rc = LUSTRE_CFG_RC_NO_ERR;
1364                 goto out;
1365         }
1366
1367         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1368                 snprintf(err_str,
1369                         sizeof(err_str),
1370                         "\"cannot parse net '%s'\"",
1371                         libcfs_net2str(nw_descr->nw_id));
1372                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1373                 goto out;
1374         }
1375
1376         if (list_empty(&nw_descr->nw_intflist)) {
1377                 snprintf(err_str,
1378                         sizeof(err_str),
1379                         "\"no interface name provided\"");
1380                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1381                 goto out;
1382         }
1383
1384         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1385         if (rc != 0) {
1386                 snprintf(err_str, sizeof(err_str),
1387                          "\"bad parameter\"");
1388                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1389                 goto out;
1390         }
1391
1392         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1393                                          tunables, global_cpts, nids,
1394                                          err_str);
1395
1396 out:
1397         if (nw_descr != NULL) {
1398                 list_for_each_entry_safe(intf_descr, tmp,
1399                                          &nw_descr->nw_intflist,
1400                                          intf_on_network) {
1401                         list_del(&intf_descr->intf_on_network);
1402                         free_intf_descr(intf_descr);
1403                 }
1404         }
1405
1406         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1407
1408         if (nids)
1409                 free(nids);
1410
1411         if (data)
1412                 free(data);
1413
1414         return rc;
1415 }
1416
1417 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1418                        int seq_no, struct cYAML **err_rc)
1419 {
1420         struct lnet_ioctl_config_ni data;
1421         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1422         char err_str[LNET_MAX_STR_LEN];
1423         lnet_nid_t *nids = NULL;
1424         __u32 nnids = 0;
1425         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1426
1427         snprintf(err_str, sizeof(err_str), "\"success\"");
1428
1429         if (nw_descr == NULL) {
1430                 snprintf(err_str,
1431                          sizeof(err_str),
1432                          "\"missing mandatory parameter\"");
1433                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1434                 goto out;
1435         }
1436
1437         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1438                 return LUSTRE_CFG_RC_NO_ERR;
1439
1440         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1441                 snprintf(err_str,
1442                          sizeof(err_str),
1443                          "\"cannot parse net '%s'\"",
1444                          libcfs_net2str(nw_descr->nw_id));
1445                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1446                 goto out;
1447         }
1448
1449         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids);
1450         if (rc != 0) {
1451                 snprintf(err_str, sizeof(err_str),
1452                          "\"bad parameter\"");
1453                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1454                 goto out;
1455         }
1456
1457         /*
1458          * no interfaces just the nw_id is specified
1459          */
1460         if (nnids == 0) {
1461                 nids = calloc(1, sizeof(*nids));
1462                 if (nids == NULL) {
1463                         snprintf(err_str, sizeof(err_str),
1464                                 "\"out of memory\"");
1465                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1466                         goto out;
1467                 }
1468                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1469                 nnids = 1;
1470         }
1471
1472         for (i = 0; i < nnids; i++) {
1473                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1474                 data.lic_nid = nids[i];
1475
1476                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1477                 if (rc < 0) {
1478                         rc = -errno;
1479                         snprintf(err_str,
1480                                 sizeof(err_str),
1481                                 "\"cannot del network: %s\"", strerror(errno));
1482                 }
1483         }
1484
1485         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1486                                  intf_on_network) {
1487                 list_del(&intf_descr->intf_on_network);
1488                 free_intf_descr(intf_descr);
1489         }
1490
1491 out:
1492         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1493
1494         if (nids != NULL)
1495                 free(nids);
1496
1497         return rc;
1498 }
1499
1500 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1501                          struct cYAML **show_rc, struct cYAML **err_rc)
1502 {
1503         char *buf;
1504         struct lnet_ioctl_config_ni *ni_data;
1505         struct lnet_ioctl_config_lnd_tunables *lnd;
1506         struct lnet_ioctl_element_stats *stats;
1507         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1508         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1509         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1510         int l_errno = 0;
1511         struct cYAML *root = NULL, *tunables = NULL,
1512                 *net_node = NULL, *interfaces = NULL,
1513                 *item = NULL, *first_seq = NULL,
1514                 *tmp = NULL, *statistics = NULL;
1515         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1516         char str_buf[str_buf_len];
1517         char *pos;
1518         char err_str[LNET_MAX_STR_LEN];
1519         bool exist = false, new_net = true;
1520         int net_num = 0;
1521         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1522
1523         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1524
1525         buf = calloc(1, buf_size);
1526         if (buf == NULL)
1527                 goto out;
1528
1529         ni_data = (struct lnet_ioctl_config_ni *)buf;
1530
1531         if (nw != NULL) {
1532                 net = libcfs_str2net(nw);
1533                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1534                         snprintf(err_str,
1535                                  sizeof(err_str),
1536                                  "\"cannot parse net '%s'\"", nw);
1537                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1538                         goto out;
1539                 }
1540         }
1541
1542         root = cYAML_create_object(NULL, NULL);
1543         if (root == NULL)
1544                 goto out;
1545
1546         net_node = cYAML_create_seq(root, "net");
1547         if (net_node == NULL)
1548                 goto out;
1549
1550         for (i = 0;; i++) {
1551                 pos = str_buf;
1552                 __u32 rc_net;
1553
1554                 memset(buf, 0, buf_size);
1555
1556                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
1557                 /*
1558                  * set the ioc_len to the proper value since INIT assumes
1559                  * size of data
1560                  */
1561                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
1562                 ni_data->lic_idx = i;
1563
1564                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
1565                 if (rc != 0) {
1566                         l_errno = errno;
1567                         break;
1568                 }
1569
1570                 rc_net = LNET_NIDNET(ni_data->lic_nid);
1571
1572                 /* filter on provided data */
1573                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1574                     net != rc_net)
1575                         continue;
1576
1577                 /* default rc to -1 in case we hit the goto */
1578                 rc = -1;
1579                 exist = true;
1580
1581                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
1582                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
1583                         (ni_data->lic_bulk + sizeof(*stats));
1584
1585                 if (rc_net != prev_net) {
1586                         prev_net = rc_net;
1587                         new_net = true;
1588                         net_num++;
1589                 }
1590
1591                 if (new_net) {
1592                         if (!cYAML_create_string(net_node, "net type",
1593                                                  libcfs_net2str(rc_net)))
1594                                 goto out;
1595
1596                         tmp = cYAML_create_seq(net_node, "local NI(s)");
1597                         if (tmp == NULL)
1598                                 goto out;
1599                         new_net = false;
1600                 }
1601
1602                 /* create the tree to be printed. */
1603                 item = cYAML_create_seq_item(tmp);
1604                 if (item == NULL)
1605                         goto out;
1606
1607                 if (first_seq == NULL)
1608                         first_seq = item;
1609
1610                 if (cYAML_create_string(item, "nid",
1611                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
1612                         goto out;
1613
1614                 if (cYAML_create_string(item,
1615                                         "status",
1616                                         (ni_data->lic_status ==
1617                                           LNET_NI_STATUS_UP) ?
1618                                             "up" : "down") == NULL)
1619                         goto out;
1620
1621                 /* don't add interfaces unless there is at least one
1622                  * interface */
1623                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
1624                         interfaces = cYAML_create_object(item, "interfaces");
1625                         if (interfaces == NULL)
1626                                 goto out;
1627
1628                         for (j = 0; j < LNET_NUM_INTERFACES; j++) {
1629                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
1630                                         snprintf(str_buf,
1631                                                  sizeof(str_buf), "%d", j);
1632                                         if (cYAML_create_string(interfaces,
1633                                                 str_buf,
1634                                                 ni_data->lic_ni_intf[j]) ==
1635                                                         NULL)
1636                                                 goto out;
1637                                 }
1638                         }
1639                 }
1640
1641                 if (detail) {
1642                         char *limit;
1643
1644                         statistics = cYAML_create_object(item, "statistics");
1645                         if (statistics == NULL)
1646                                 goto out;
1647
1648                         if (cYAML_create_number(statistics, "send_count",
1649                                                 stats->iel_send_count)
1650                                                         == NULL)
1651                                 goto out;
1652
1653                         if (cYAML_create_number(statistics, "recv_count",
1654                                                 stats->iel_recv_count)
1655                                                         == NULL)
1656                                 goto out;
1657
1658                         if (cYAML_create_number(statistics, "drop_count",
1659                                                 stats->iel_drop_count)
1660                                                         == NULL)
1661                                 goto out;
1662
1663                         tunables = cYAML_create_object(item, "tunables");
1664                         if (!tunables)
1665                                 goto out;
1666
1667                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
1668                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1669                                 goto out;
1670
1671                         tunables = cYAML_create_object(item, "lnd tunables");
1672                         if (tunables == NULL)
1673                                 goto out;
1674
1675                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
1676                                                      &lnd->lt_tun);
1677                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1678                                 goto out;
1679
1680                         if (cYAML_create_number(item, "tcp bonding",
1681                                                 ni_data->lic_tcp_bonding)
1682                                                         == NULL)
1683                                 goto out;
1684
1685                         if (cYAML_create_number(item, "dev cpt",
1686                                                 ni_data->lic_dev_cpt) == NULL)
1687                                 goto out;
1688
1689                         /* out put the CPTs in the format: "[x,x,x,...]" */
1690                         limit = str_buf + str_buf_len - 3;
1691                         pos += snprintf(pos, limit - pos, "\"[");
1692                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
1693                                 j < ni_data->lic_ncpts &&
1694                                 pos < limit; j++) {
1695                                 pos += snprintf(pos, limit - pos,
1696                                                 "%d", ni_data->lic_cpts[j]);
1697                                 if ((j + 1) < ni_data->lic_ncpts)
1698                                         pos += snprintf(pos, limit - pos, ",");
1699                         }
1700                         pos += snprintf(pos, 3, "]\"");
1701
1702                         if (ni_data->lic_ncpts >= 1 &&
1703                             cYAML_create_string(item, "CPT",
1704                                                 str_buf) == NULL)
1705                                 goto out;
1706                 }
1707         }
1708
1709         /* Print out the net information only if show_rc is not provided */
1710         if (show_rc == NULL)
1711                 cYAML_print_tree(root);
1712
1713         if (l_errno != ENOENT) {
1714                 snprintf(err_str,
1715                          sizeof(err_str),
1716                          "\"cannot get networks: %s\"",
1717                          strerror(l_errno));
1718                 rc = -l_errno;
1719                 goto out;
1720         } else
1721                 rc = LUSTRE_CFG_RC_NO_ERR;
1722
1723         snprintf(err_str, sizeof(err_str), "\"success\"");
1724 out:
1725         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1726                 cYAML_free_tree(root);
1727         } else if (show_rc != NULL && *show_rc != NULL) {
1728                 struct cYAML *show_node;
1729                 /* find the net node, if one doesn't exist
1730                  * then insert one.  Otherwise add to the one there
1731                  */
1732                 show_node = cYAML_get_object_item(*show_rc, "net");
1733                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1734                         cYAML_insert_child(show_node, first_seq);
1735                         free(net_node);
1736                         free(root);
1737                 } else if (show_node == NULL) {
1738                         cYAML_insert_sibling((*show_rc)->cy_child,
1739                                                 net_node);
1740                         free(root);
1741                 } else {
1742                         cYAML_free_tree(root);
1743                 }
1744         } else {
1745                 *show_rc = root;
1746         }
1747
1748         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
1749
1750         return rc;
1751 }
1752
1753 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
1754 {
1755         struct lnet_ioctl_config_data data;
1756         int rc = LUSTRE_CFG_RC_NO_ERR;
1757         char err_str[LNET_MAX_STR_LEN];
1758
1759         snprintf(err_str, sizeof(err_str), "\"success\"");
1760
1761         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1762         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
1763
1764         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
1765         if (rc != 0) {
1766                 rc = -errno;
1767                 snprintf(err_str,
1768                          sizeof(err_str),
1769                          "\"cannot %s routing %s\"",
1770                          (enable) ? "enable" : "disable", strerror(errno));
1771                 goto out;
1772         }
1773
1774 out:
1775         cYAML_build_error(rc, seq_no,
1776                          (enable) ? ADD_CMD : DEL_CMD,
1777                          "routing", err_str, err_rc);
1778
1779         return rc;
1780 }
1781
1782 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
1783 {
1784         struct lnet_ioctl_numa_range data;
1785         int rc = LUSTRE_CFG_RC_NO_ERR;
1786         char err_str[LNET_MAX_STR_LEN];
1787
1788         snprintf(err_str, sizeof(err_str), "\"success\"");
1789
1790         if (range < 0) {
1791                 snprintf(err_str,
1792                          sizeof(err_str),
1793                          "\"range must be >= 0\"");
1794                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1795                 goto out;
1796         }
1797
1798         LIBCFS_IOC_INIT_V2(data, nr_hdr);
1799         data.nr_range = range;
1800
1801         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_NUMA_RANGE, &data);
1802         if (rc != 0) {
1803                 rc = -errno;
1804                 snprintf(err_str,
1805                          sizeof(err_str),
1806                          "\"cannot configure buffers: %s\"", strerror(errno));
1807                 goto out;
1808         }
1809
1810 out:
1811         cYAML_build_error(rc, seq_no, ADD_CMD, "numa_range", err_str, err_rc);
1812
1813         return rc;
1814 }
1815
1816 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
1817                                struct cYAML **err_rc)
1818 {
1819         struct lnet_ioctl_config_data data;
1820         int rc = LUSTRE_CFG_RC_NO_ERR;
1821         char err_str[LNET_MAX_STR_LEN];
1822
1823         snprintf(err_str, sizeof(err_str), "\"success\"");
1824
1825         /* -1 indicates to ignore changes to this field */
1826         if (tiny < -1 || small < -1 || large < -1) {
1827                 snprintf(err_str,
1828                          sizeof(err_str),
1829                          "\"tiny, small and large must be >= 0\"");
1830                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1831                 goto out;
1832         }
1833
1834         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1835         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
1836         data.cfg_config_u.cfg_buffers.buf_small = small;
1837         data.cfg_config_u.cfg_buffers.buf_large = large;
1838
1839         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
1840         if (rc != 0) {
1841                 rc = -errno;
1842                 snprintf(err_str,
1843                          sizeof(err_str),
1844                          "\"cannot configure buffers: %s\"", strerror(errno));
1845                 goto out;
1846         }
1847
1848 out:
1849         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
1850
1851         return rc;
1852 }
1853
1854 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
1855                              struct cYAML **err_rc)
1856 {
1857         struct lnet_ioctl_config_data *data;
1858         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
1859         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1860         int l_errno = 0;
1861         char *buf;
1862         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
1863         int buf_count[LNET_NRBPOOLS] = {0};
1864         struct cYAML *root = NULL, *pools_node = NULL,
1865                      *type_node = NULL, *item = NULL, *cpt = NULL,
1866                      *first_seq = NULL, *buffers = NULL;
1867         int i, j;
1868         char err_str[LNET_MAX_STR_LEN];
1869         char node_name[LNET_MAX_STR_LEN];
1870         bool exist = false;
1871
1872         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1873
1874         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
1875         if (buf == NULL)
1876                 goto out;
1877
1878         data = (struct lnet_ioctl_config_data *)buf;
1879
1880         root = cYAML_create_object(NULL, NULL);
1881         if (root == NULL)
1882                 goto out;
1883
1884         pools_node = cYAML_create_seq(root, "routing");
1885         if (pools_node == NULL)
1886                 goto out;
1887
1888         for (i = 0;; i++) {
1889                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
1890                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
1891                                         sizeof(struct lnet_ioctl_pool_cfg);
1892                 data->cfg_count = i;
1893
1894                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
1895                 if (rc != 0) {
1896                         l_errno = errno;
1897                         break;
1898                 }
1899
1900                 exist = true;
1901
1902                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
1903
1904                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
1905                 item = cYAML_create_seq_item(pools_node);
1906                 if (item == NULL)
1907                         goto out;
1908
1909                 if (first_seq == NULL)
1910                         first_seq = item;
1911
1912                 cpt = cYAML_create_object(item, node_name);
1913                 if (cpt == NULL)
1914                         goto out;
1915
1916                 /* create the tree  and print */
1917                 for (j = 0; j < LNET_NRBPOOLS; j++) {
1918                         type_node = cYAML_create_object(cpt, pools[j]);
1919                         if (type_node == NULL)
1920                                 goto out;
1921                         if (cYAML_create_number(type_node, "npages",
1922                                                 pool_cfg->pl_pools[j].pl_npages)
1923                             == NULL)
1924                                 goto out;
1925                         if (cYAML_create_number(type_node, "nbuffers",
1926                                                 pool_cfg->pl_pools[j].
1927                                                   pl_nbuffers) == NULL)
1928                                 goto out;
1929                         if (cYAML_create_number(type_node, "credits",
1930                                                 pool_cfg->pl_pools[j].
1931                                                    pl_credits) == NULL)
1932                                 goto out;
1933                         if (cYAML_create_number(type_node, "mincredits",
1934                                                 pool_cfg->pl_pools[j].
1935                                                    pl_mincredits) == NULL)
1936                                 goto out;
1937                         /* keep track of the total count for each of the
1938                          * tiny, small and large buffers */
1939                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
1940                 }
1941         }
1942
1943         if (pool_cfg != NULL) {
1944                 item = cYAML_create_seq_item(pools_node);
1945                 if (item == NULL)
1946                         goto out;
1947
1948                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
1949                     NULL)
1950                         goto out;
1951         }
1952
1953         /* create a buffers entry in the show. This is necessary so that
1954          * if the YAML output is used to configure a node, the buffer
1955          * configuration takes hold */
1956         buffers = cYAML_create_object(root, "buffers");
1957         if (buffers == NULL)
1958                 goto out;
1959
1960         for (i = 0; i < LNET_NRBPOOLS; i++) {
1961                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
1962                         goto out;
1963         }
1964
1965         if (show_rc == NULL)
1966                 cYAML_print_tree(root);
1967
1968         if (l_errno != ENOENT) {
1969                 snprintf(err_str,
1970                          sizeof(err_str),
1971                          "\"cannot get routing information: %s\"",
1972                          strerror(l_errno));
1973                 rc = -l_errno;
1974                 goto out;
1975         } else
1976                 rc = LUSTRE_CFG_RC_NO_ERR;
1977
1978         snprintf(err_str, sizeof(err_str), "\"success\"");
1979         rc = LUSTRE_CFG_RC_NO_ERR;
1980
1981 out:
1982         free(buf);
1983         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1984                 cYAML_free_tree(root);
1985         } else if (show_rc != NULL && *show_rc != NULL) {
1986                 struct cYAML *routing_node;
1987                 /* there should exist only one routing block and one
1988                  * buffers block. If there already exists a previous one
1989                  * then don't add another */
1990                 routing_node = cYAML_get_object_item(*show_rc, "routing");
1991                 if (routing_node == NULL) {
1992                         cYAML_insert_sibling((*show_rc)->cy_child,
1993                                                 root->cy_child);
1994                         free(root);
1995                 } else {
1996                         cYAML_free_tree(root);
1997                 }
1998         } else {
1999                 *show_rc = root;
2000         }
2001
2002         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2003
2004         return rc;
2005 }
2006
2007 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2008                           struct cYAML **show_rc, struct cYAML **err_rc)
2009 {
2010         /*
2011          * TODO: This function is changing in a future patch to accommodate
2012          * PEER_LIST and proper filtering on any nid of the peer
2013          */
2014         struct lnet_ioctl_peer_cfg peer_info;
2015         struct lnet_peer_ni_credit_info *lpni_cri;
2016         struct lnet_ioctl_element_stats *lpni_stats;
2017         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, ncpt = 0, i = 0, j = 0;
2018         int l_errno = 0;
2019         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2020                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL;
2021         char err_str[LNET_MAX_STR_LEN];
2022         lnet_nid_t prev_primary_nid = LNET_NID_ANY, primary_nid = LNET_NID_ANY;
2023         int data_size = sizeof(*lpni_cri) + sizeof(*lpni_stats);
2024         char *data = malloc(data_size);
2025         bool new_peer = true;
2026
2027         snprintf(err_str, sizeof(err_str),
2028                  "\"out of memory\"");
2029
2030         if (data == NULL)
2031                 goto out;
2032
2033         /* create struct cYAML root object */
2034         root = cYAML_create_object(NULL, NULL);
2035         if (root == NULL)
2036                 goto out;
2037
2038         peer_root = cYAML_create_seq(root, "peer");
2039         if (peer_root == NULL)
2040                 goto out;
2041
2042         if (knid != NULL)
2043                 primary_nid = libcfs_str2nid(knid);
2044
2045         do {
2046                 for (i = 0;; i++) {
2047                         memset(data, 0, data_size);
2048                         memset(&peer_info, 0, sizeof(peer_info));
2049                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2050                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2051                         peer_info.prcfg_count = i;
2052                         peer_info.prcfg_bulk = (void *)data;
2053                         peer_info.prcfg_size = data_size;
2054
2055                         rc = l_ioctl(LNET_DEV_ID,
2056                                      IOC_LIBCFS_GET_PEER_NI, &peer_info);
2057                         if (rc != 0) {
2058                                 l_errno = errno;
2059                                 break;
2060                         }
2061
2062                         if (primary_nid != LNET_NID_ANY &&
2063                             primary_nid != peer_info.prcfg_prim_nid)
2064                                         continue;
2065
2066                         lpni_cri = peer_info.prcfg_bulk;
2067                         lpni_stats = peer_info.prcfg_bulk + sizeof(*lpni_cri);
2068
2069                         peer = cYAML_create_seq_item(peer_root);
2070                         if (peer == NULL)
2071                                 goto out;
2072
2073                         if (peer_info.prcfg_prim_nid != prev_primary_nid) {
2074                                 prev_primary_nid = peer_info.prcfg_prim_nid;
2075                                 new_peer = true;
2076                         }
2077
2078                         if (new_peer) {
2079                                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2080                                 if (cYAML_create_string(peer, "primary nid",
2081                                                         libcfs_nid2str(pnid))
2082                                     == NULL)
2083                                         goto out;
2084                                 if (cYAML_create_string(peer, "Multi-Rail",
2085                                                         peer_info.prcfg_mr ?
2086                                                         "True" : "False")
2087                                     == NULL)
2088                                         goto out;
2089                                 tmp = cYAML_create_seq(peer, "peer ni");
2090                                 if (tmp == NULL)
2091                                         goto out;
2092                                 new_peer = false;
2093                         }
2094
2095                         if (first_seq == NULL)
2096                                 first_seq = peer;
2097
2098                         peer_ni = cYAML_create_seq_item(tmp);
2099                         if (peer_ni == NULL)
2100                                 goto out;
2101
2102                         if (cYAML_create_string(peer_ni, "nid",
2103                                                 libcfs_nid2str
2104                                                  (peer_info.prcfg_cfg_nid))
2105                             == NULL)
2106                                 goto out;
2107
2108                         if (cYAML_create_string(peer_ni, "state",
2109                                                 lpni_cri->cr_aliveness)
2110                             == NULL)
2111                                 goto out;
2112
2113                         if (!detail)
2114                                 continue;
2115
2116                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2117                                                 lpni_cri->cr_ni_peer_tx_credits)
2118                             == NULL)
2119                                 goto out;
2120
2121                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2122                                                 lpni_cri->cr_peer_tx_credits)
2123                             == NULL)
2124                                 goto out;
2125
2126                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2127                                                 lpni_cri->cr_peer_min_tx_credits)
2128                             == NULL)
2129                                 goto out;
2130
2131                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2132                                                 lpni_cri->cr_peer_tx_qnob)
2133                             == NULL)
2134                                 goto out;
2135
2136                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2137                                                 lpni_cri->cr_peer_rtr_credits)
2138                             == NULL)
2139                                 goto out;
2140
2141                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2142                                                 lpni_cri->cr_peer_min_rtr_credits)
2143                             == NULL)
2144                                 goto out;
2145
2146                         if (cYAML_create_number(peer_ni, "send_count",
2147                                                 lpni_stats->iel_send_count)
2148                             == NULL)
2149                                 goto out;
2150
2151                         if (cYAML_create_number(peer_ni, "recv_count",
2152                                                 lpni_stats->iel_recv_count)
2153                             == NULL)
2154                                 goto out;
2155
2156                         if (cYAML_create_number(peer_ni, "drop_count",
2157                                                 lpni_stats->iel_drop_count)
2158                             == NULL)
2159                                 goto out;
2160
2161                         if (cYAML_create_number(peer_ni, "refcount",
2162                                                 lpni_cri->cr_refcount) == NULL)
2163                                 goto out;
2164                 }
2165
2166                 if (l_errno != ENOENT) {
2167                         snprintf(err_str,
2168                                 sizeof(err_str),
2169                                 "\"cannot get peer information: %s\"",
2170                                 strerror(l_errno));
2171                         rc = -l_errno;
2172                         goto out;
2173                 }
2174
2175                 j++;
2176         } while (j < ncpt);
2177
2178         /* print output iff show_rc is not provided */
2179         if (show_rc == NULL)
2180                 cYAML_print_tree(root);
2181
2182         snprintf(err_str, sizeof(err_str), "\"success\"");
2183         rc = LUSTRE_CFG_RC_NO_ERR;
2184
2185 out:
2186         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2187                 cYAML_free_tree(root);
2188         } else if (show_rc != NULL && *show_rc != NULL) {
2189                 struct cYAML *show_node;
2190                 /* find the peer node, if one doesn't exist then
2191                  * insert one.  Otherwise add to the one there
2192                  */
2193                 show_node = cYAML_get_object_item(*show_rc,
2194                                                   "peer");
2195                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2196                         cYAML_insert_child(show_node, first_seq);
2197                         free(peer_root);
2198                         free(root);
2199                 } else if (show_node == NULL) {
2200                         cYAML_insert_sibling((*show_rc)->cy_child,
2201                                              peer_root);
2202                         free(root);
2203                 } else {
2204                         cYAML_free_tree(root);
2205                 }
2206         } else {
2207                 *show_rc = root;
2208         }
2209
2210         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2211                           err_rc);
2212
2213         return rc;
2214 }
2215
2216 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2217                                 struct cYAML **err_rc)
2218 {
2219         struct lnet_ioctl_numa_range data;
2220         int rc;
2221         int l_errno;
2222         char err_str[LNET_MAX_STR_LEN];
2223         struct cYAML *root = NULL, *range = NULL;
2224
2225         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2226
2227         LIBCFS_IOC_INIT_V2(data, nr_hdr);
2228
2229         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2230         if (rc != 0) {
2231                 l_errno = errno;
2232                 snprintf(err_str,
2233                          sizeof(err_str),
2234                          "\"cannot get numa range: %s\"",
2235                          strerror(l_errno));
2236                 rc = -l_errno;
2237                 goto out;
2238         }
2239
2240         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2241
2242         root = cYAML_create_object(NULL, NULL);
2243         if (root == NULL)
2244                 goto out;
2245
2246         range = cYAML_create_object(root, "numa");
2247         if (range == NULL)
2248                 goto out;
2249
2250         if (cYAML_create_number(range, "range",
2251                                 data.nr_range) == NULL)
2252                 goto out;
2253
2254         if (show_rc == NULL)
2255                 cYAML_print_tree(root);
2256
2257         snprintf(err_str, sizeof(err_str), "\"success\"");
2258         rc = LUSTRE_CFG_RC_NO_ERR;
2259 out:
2260         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2261                 cYAML_free_tree(root);
2262         } else if (show_rc != NULL && *show_rc != NULL) {
2263                 cYAML_insert_sibling((*show_rc)->cy_child,
2264                                         root->cy_child);
2265                 free(root);
2266         } else {
2267                 *show_rc = root;
2268         }
2269
2270         cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2271
2272         return rc;
2273 }
2274
2275 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2276                            struct cYAML **err_rc)
2277 {
2278         struct lnet_ioctl_lnet_stats data;
2279         int rc;
2280         int l_errno;
2281         char err_str[LNET_MAX_STR_LEN];
2282         struct cYAML *root = NULL, *stats = NULL;
2283
2284         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2285
2286         LIBCFS_IOC_INIT_V2(data, st_hdr);
2287
2288         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2289         if (rc != 0) {
2290                 l_errno = errno;
2291                 snprintf(err_str,
2292                          sizeof(err_str),
2293                          "\"cannot get lnet statistics: %s\"",
2294                          strerror(l_errno));
2295                 rc = -l_errno;
2296                 goto out;
2297         }
2298
2299         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2300
2301         root = cYAML_create_object(NULL, NULL);
2302         if (root == NULL)
2303                 goto out;
2304
2305         stats = cYAML_create_object(root, "statistics");
2306         if (stats == NULL)
2307                 goto out;
2308
2309         if (cYAML_create_number(stats, "msgs_alloc",
2310                                 data.st_cntrs.msgs_alloc) == NULL)
2311                 goto out;
2312
2313         if (cYAML_create_number(stats, "msgs_max",
2314                                 data.st_cntrs.msgs_max) == NULL)
2315                 goto out;
2316
2317         if (cYAML_create_number(stats, "errors",
2318                                 data.st_cntrs.errors) == NULL)
2319                 goto out;
2320
2321         if (cYAML_create_number(stats, "send_count",
2322                                 data.st_cntrs.send_count) == NULL)
2323                 goto out;
2324
2325         if (cYAML_create_number(stats, "recv_count",
2326                                 data.st_cntrs.recv_count) == NULL)
2327                 goto out;
2328
2329         if (cYAML_create_number(stats, "route_count",
2330                                 data.st_cntrs.route_count) == NULL)
2331                 goto out;
2332
2333         if (cYAML_create_number(stats, "drop_count",
2334                                 data.st_cntrs.drop_count) == NULL)
2335                 goto out;
2336
2337         if (cYAML_create_number(stats, "send_length",
2338                                 data.st_cntrs.send_length) == NULL)
2339                 goto out;
2340
2341         if (cYAML_create_number(stats, "recv_length",
2342                                 data.st_cntrs.recv_length) == NULL)
2343                 goto out;
2344
2345         if (cYAML_create_number(stats, "route_length",
2346                                 data.st_cntrs.route_length) == NULL)
2347                 goto out;
2348
2349         if (cYAML_create_number(stats, "drop_length",
2350                                 data.st_cntrs.drop_length) == NULL)
2351                 goto out;
2352
2353         if (show_rc == NULL)
2354                 cYAML_print_tree(root);
2355
2356         snprintf(err_str, sizeof(err_str), "\"success\"");
2357         rc = LUSTRE_CFG_RC_NO_ERR;
2358 out:
2359         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2360                 cYAML_free_tree(root);
2361         } else if (show_rc != NULL && *show_rc != NULL) {
2362                 cYAML_insert_sibling((*show_rc)->cy_child,
2363                                         root->cy_child);
2364                 free(root);
2365         } else {
2366                 *show_rc = root;
2367         }
2368
2369         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2370
2371         return rc;
2372 }
2373
2374 typedef int (*cmd_handler_t)(struct cYAML *tree,
2375                              struct cYAML **show_rc,
2376                              struct cYAML **err_rc);
2377
2378 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2379                                     struct cYAML **err_rc)
2380 {
2381         struct cYAML *net, *gw, *hop, *prio, *seq_no;
2382
2383         net = cYAML_get_object_item(tree, "net");
2384         gw = cYAML_get_object_item(tree, "gateway");
2385         hop = cYAML_get_object_item(tree, "hop");
2386         prio = cYAML_get_object_item(tree, "priority");
2387         seq_no = cYAML_get_object_item(tree, "seq_no");
2388
2389         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2390                                         (gw) ? gw->cy_valuestring : NULL,
2391                                         (hop) ? hop->cy_valueint : -1,
2392                                         (prio) ? prio->cy_valueint : -1,
2393                                         (seq_no) ? seq_no->cy_valueint : -1,
2394                                         err_rc);
2395 }
2396
2397 static void yaml_free_string_array(char **array, int num)
2398 {
2399         int i;
2400         char **sub_array = array;
2401
2402         for (i = 0; i < num; i++) {
2403                 if (*sub_array != NULL)
2404                         free(*sub_array);
2405                 sub_array++;
2406         }
2407         if (array)
2408                 free(array);
2409 }
2410
2411 /*
2412  *    interfaces:
2413  *        0: <intf_name>['['<expr>']']
2414  *        1: <intf_name>['['<expr>']']
2415  */
2416 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2417                                struct lnet_dlc_network_descr *nw_descr)
2418 {
2419         struct cYAML *child = NULL;
2420         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2421         struct lnet_dlc_intf_descr *intf_descr, *tmp;
2422
2423         if (intf_tree == NULL || nw_descr == NULL)
2424                 return LUSTRE_CFG_RC_BAD_PARAM;
2425
2426         /* now grab all the interfaces and their cpts */
2427         child = intf_tree->cy_child;
2428         while (child != NULL) {
2429                 if (child->cy_valuestring == NULL) {
2430                         child = child->cy_next;
2431                         continue;
2432                 }
2433
2434                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2435                         goto failed;
2436
2437                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2438                                                 child->cy_valuestring,
2439                                                 strlen(child->cy_valuestring));
2440                 if (rc != LUSTRE_CFG_RC_NO_ERR)
2441                         goto failed;
2442
2443                 intf_num++;
2444                 child = child->cy_next;
2445         }
2446
2447         if (intf_num == 0)
2448                 return LUSTRE_CFG_RC_MISSING_PARAM;
2449
2450         return intf_num;
2451
2452 failed:
2453         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2454                                  intf_on_network) {
2455                 list_del(&intf_descr->intf_on_network);
2456                 free_intf_descr(intf_descr);
2457         }
2458
2459         return rc;
2460 }
2461
2462 static bool
2463 yaml_extract_cmn_tunables(struct cYAML *tree,
2464                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2465                           struct cfs_expr_list **global_cpts)
2466 {
2467         struct cYAML *tun, *item, *smp;
2468         int rc;
2469
2470         tun = cYAML_get_object_item(tree, "tunables");
2471         if (tun != NULL) {
2472                 item = cYAML_get_object_item(tun, "peer_timeout");
2473                 if (item != NULL)
2474                         tunables->lct_peer_timeout = item->cy_valueint;
2475                 item = cYAML_get_object_item(tun, "peer_credits");
2476                 if (item != NULL)
2477                         tunables->lct_peer_tx_credits = item->cy_valueint;
2478                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2479                 if (item != NULL)
2480                         tunables->lct_peer_rtr_credits = item->cy_valueint;
2481                 item = cYAML_get_object_item(tun, "credits");
2482                 if (item != NULL)
2483                         tunables->lct_max_tx_credits = item->cy_valueint;
2484                 smp = cYAML_get_object_item(tun, "CPT");
2485                 if (smp != NULL) {
2486                         rc = cfs_expr_list_parse(smp->cy_valuestring,
2487                                                  strlen(smp->cy_valuestring),
2488                                                  0, UINT_MAX, global_cpts);
2489                         if (rc != 0)
2490                                 *global_cpts = NULL;
2491                 }
2492
2493                 return true;
2494         }
2495
2496         return false;
2497 }
2498
2499 static bool
2500 yaml_extract_tunables(struct cYAML *tree,
2501                       struct lnet_ioctl_config_lnd_tunables *tunables,
2502                       struct cfs_expr_list **global_cpts,
2503                       __u32 net_type)
2504 {
2505         bool rc;
2506
2507         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2508                                        global_cpts);
2509
2510         if (!rc)
2511                 return rc;
2512
2513         lustre_yaml_extract_lnd_tunables(tree, net_type,
2514                                          &tunables->lt_tun);
2515
2516         return rc;
2517 }
2518
2519 /*
2520  * net:
2521  *    - net type: <net>[<NUM>]
2522   *      local NI(s):
2523  *        - nid: <ip>@<net>[<NUM>]
2524  *          status: up
2525  *          interfaces:
2526  *               0: <intf_name>['['<expr>']']
2527  *               1: <intf_name>['['<expr>']']
2528  *        tunables:
2529  *               peer_timeout: <NUM>
2530  *               peer_credits: <NUM>
2531  *               peer_buffer_credits: <NUM>
2532  *               credits: <NUM>
2533 *         lnd tunables:
2534  *               peercredits_hiw: <NUM>
2535  *               map_on_demand: <NUM>
2536  *               concurrent_sends: <NUM>
2537  *               fmr_pool_size: <NUM>
2538  *               fmr_flush_trigger: <NUM>
2539  *               fmr_cache: <NUM>
2540  *
2541  * At least one interface is required. If no interfaces are provided the
2542  * network interface can not be configured.
2543  */
2544 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2545                                  struct cYAML **err_rc)
2546 {
2547         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2548                      *item = NULL;
2549         int num_entries = 0, rc;
2550         struct lnet_dlc_network_descr nw_descr;
2551         struct cfs_expr_list *global_cpts = NULL;
2552         struct lnet_ioctl_config_lnd_tunables tunables;
2553         bool found = false;
2554
2555         memset(&tunables, 0, sizeof(tunables));
2556
2557         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2558         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2559
2560         ip2net = cYAML_get_object_item(tree, "ip2net");
2561         net = cYAML_get_object_item(tree, "net type");
2562         if (net)
2563                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2564         else
2565                 nw_descr.nw_id = LOLND;
2566
2567         /*
2568          * if neither net nor ip2nets are present, then we can not
2569          * configure the network.
2570          */
2571         if (!net && !ip2net)
2572                 return LUSTRE_CFG_RC_MISSING_PARAM;
2573
2574         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2575         if (local_nis == NULL)
2576                 return LUSTRE_CFG_RC_MISSING_PARAM;
2577
2578         if (!cYAML_is_sequence(local_nis))
2579                 return LUSTRE_CFG_RC_BAD_PARAM;
2580
2581         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2582                 intf = cYAML_get_object_item(item, "interfaces");
2583                 if (intf == NULL)
2584                         continue;
2585                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2586                 if (num_entries <= 0) {
2587                         cYAML_build_error(num_entries, -1, "ni", "add",
2588                                         "bad interface list",
2589                                         err_rc);
2590                         return LUSTRE_CFG_RC_BAD_PARAM;
2591                 }
2592         }
2593
2594         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2595                                       LNET_NETTYP(nw_descr.nw_id));
2596         seq_no = cYAML_get_object_item(tree, "seq_no");
2597
2598         rc = lustre_lnet_config_ni(&nw_descr,
2599                                    global_cpts,
2600                                    (ip2net) ? ip2net->cy_valuestring : NULL,
2601                                    (found) ? &tunables: NULL,
2602                                    (seq_no) ? seq_no->cy_valueint : -1,
2603                                    err_rc);
2604
2605         if (global_cpts != NULL)
2606                 cfs_expr_list_free(global_cpts);
2607
2608         return rc;
2609 }
2610
2611 /*
2612  * ip2nets:
2613  *  - net-spec: <tcp|o2ib|gni>[NUM]
2614  *    interfaces:
2615  *        0: <intf name>['['<expr>']']
2616  *        1: <intf name>['['<expr>']']
2617  *    ip-range:
2618  *        0: <expr.expr.expr.expr>
2619  *        1: <expr.expr.expr.expr>
2620  */
2621 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2622                                       struct cYAML **show_rc,
2623                                       struct cYAML **err_rc)
2624 {
2625         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2626                      *seq_no = NULL;
2627         struct lustre_lnet_ip2nets ip2nets;
2628         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2629                                           *tmp = NULL;
2630         int rc = LUSTRE_CFG_RC_NO_ERR;
2631         struct cfs_expr_list *global_cpts = NULL;
2632         struct cfs_expr_list *el, *el_tmp;
2633         struct lnet_ioctl_config_lnd_tunables tunables;
2634         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2635         bool found = false;
2636
2637         memset(&tunables, 0, sizeof(tunables));
2638
2639         /* initialize all lists */
2640         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2641         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2642         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2643
2644         net = cYAML_get_object_item(tree, "net-spec");
2645         if (net == NULL)
2646                 return LUSTRE_CFG_RC_BAD_PARAM;
2647
2648         if (net != NULL && net->cy_valuestring == NULL)
2649                 return LUSTRE_CFG_RC_BAD_PARAM;
2650
2651         /* assign the network id */
2652         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2653         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2654                 return LUSTRE_CFG_RC_BAD_PARAM;
2655
2656         seq_no = cYAML_get_object_item(tree, "seq_no");
2657
2658         intf = cYAML_get_object_item(tree, "interfaces");
2659         if (intf != NULL) {
2660                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2661                 if (rc <= 0)
2662                         return LUSTRE_CFG_RC_BAD_PARAM;
2663         }
2664
2665         ip_range = cYAML_get_object_item(tree, "ip-range");
2666         if (ip_range != NULL) {
2667                 item = ip_range->cy_child;
2668                 while (item != NULL) {
2669                         if (item->cy_valuestring == NULL) {
2670                                 item = item->cy_next;
2671                                 continue;
2672                         }
2673
2674                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2675                                                       item->cy_valuestring);
2676
2677                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2678                                 goto out;
2679
2680                         item = item->cy_next;
2681                 }
2682         }
2683
2684         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
2685                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2686
2687         rc = lustre_lnet_config_ip2nets(&ip2nets,
2688                         (found) ? &tunables : NULL,
2689                         global_cpts,
2690                         (seq_no) ? seq_no->cy_valueint : -1,
2691                         err_rc);
2692
2693         /*
2694          * don't stop because there was no match. Continue processing the
2695          * rest of the rules. If non-match then nothing is configured
2696          */
2697         if (rc == LUSTRE_CFG_RC_NO_MATCH)
2698                 rc = LUSTRE_CFG_RC_NO_ERR;
2699 out:
2700         list_for_each_entry_safe(intf_descr, intf_tmp,
2701                                  &ip2nets.ip2nets_net.nw_intflist,
2702                                  intf_on_network) {
2703                 list_del(&intf_descr->intf_on_network);
2704                 free_intf_descr(intf_descr);
2705         }
2706
2707         list_for_each_entry_safe(ip_range_descr, tmp,
2708                                  &ip2nets.ip2nets_ip_ranges,
2709                                  ipr_entry) {
2710                 list_del(&ip_range_descr->ipr_entry);
2711                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2712                                          el_link) {
2713                         list_del(&el->el_link);
2714                         cfs_expr_list_free(el);
2715                 }
2716                 free(ip_range_descr);
2717         }
2718
2719         return rc;
2720 }
2721
2722 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2723                               struct cYAML **err_rc)
2724 {
2725         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2726                      *local_nis = NULL;
2727         int num_entries, rc;
2728         struct lnet_dlc_network_descr nw_descr;
2729
2730         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2731         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2732
2733         net = cYAML_get_object_item(tree, "net type");
2734         if (net != NULL)
2735                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2736
2737         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2738         if (local_nis == NULL)
2739                 return LUSTRE_CFG_RC_MISSING_PARAM;
2740
2741         if (!cYAML_is_sequence(local_nis))
2742                 return LUSTRE_CFG_RC_BAD_PARAM;
2743
2744         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2745                 intf = cYAML_get_object_item(item, "interfaces");
2746                 if (intf == NULL)
2747                         continue;
2748                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2749                 if (num_entries <= 0) {
2750                         cYAML_build_error(num_entries, -1, "ni", "add",
2751                                         "bad interface list",
2752                                         err_rc);
2753                         return LUSTRE_CFG_RC_BAD_PARAM;
2754                 }
2755         }
2756
2757         seq_no = cYAML_get_object_item(tree, "seq_no");
2758
2759         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2760                                 (seq_no) ? seq_no->cy_valueint : -1,
2761                                 err_rc);
2762
2763         return rc;
2764 }
2765
2766 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp, bool del)
2767 {
2768         struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL,
2769                      *prim_nid = NULL;
2770         char **nids = NULL;
2771         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2772
2773         prim_nid = cYAML_get_object_item(tree, "primary nid");
2774         if (!prim_nid || !prim_nid->cy_valuestring)
2775                 return LUSTRE_CFG_RC_MISSING_PARAM;
2776
2777         nids_entry = cYAML_get_object_item(tree, "peer ni");
2778         if (cYAML_is_sequence(nids_entry)) {
2779                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
2780                         entry = cYAML_get_object_item(child, "nid");
2781                         /* don't count an empty entry */
2782                         if (!entry || !entry->cy_valuestring)
2783                                 continue;
2784
2785                         if ((strcmp(entry->cy_valuestring, prim_nid->cy_valuestring)
2786                                         == 0) && del) {
2787                                 /*
2788                                  * primary nid is present in the list of
2789                                  * nids so that means we want to delete
2790                                  * the entire peer, so no need to go
2791                                  * further. Just delete the entire peer.
2792                                  */
2793                                 return 0;
2794                         }
2795
2796                         num++;
2797                 }
2798         }
2799
2800         if (num == 0)
2801                 return LUSTRE_CFG_RC_MISSING_PARAM;
2802
2803         nids = calloc(sizeof(*nids) * num, 1);
2804         if (nids == NULL)
2805                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2806
2807         /* now grab all the nids */
2808         num = 0;
2809         child = NULL;
2810         while (cYAML_get_next_seq_item(nids_entry, &child)) {
2811                 entry = cYAML_get_object_item(child, "nid");
2812                 if (!entry || !entry->cy_valuestring)
2813                         continue;
2814
2815                 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2816                 if (!nids[num]) {
2817                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2818                         goto failed;
2819                 }
2820                 strncpy(nids[num], entry->cy_valuestring,
2821                         strlen(entry->cy_valuestring));
2822                 num++;
2823         }
2824         rc = num;
2825
2826         *nidsppp = nids;
2827         return rc;
2828
2829 failed:
2830         if (nids != NULL)
2831                 yaml_free_string_array(nids, num);
2832         *nidsppp = NULL;
2833         return rc;
2834 }
2835
2836 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2837                                    struct cYAML **err_rc)
2838 {
2839         char **nids = NULL;
2840         int num, rc;
2841         struct cYAML *seq_no, *prim_nid, *non_mr;
2842
2843         num = yaml_copy_peer_nids(tree, &nids, false);
2844         if (num < 0)
2845                 return num;
2846
2847         seq_no = cYAML_get_object_item(tree, "seq_no");
2848         prim_nid = cYAML_get_object_item(tree, "primary nid");
2849         non_mr = cYAML_get_object_item(tree, "non_mr");
2850
2851         rc = lustre_lnet_config_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2852                                          nids, num,
2853                                          (non_mr) ? false : true,
2854                                          (seq_no) ? seq_no->cy_valueint : -1,
2855                                          err_rc);
2856
2857         yaml_free_string_array(nids, num);
2858         return rc;
2859 }
2860
2861 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2862                                 struct cYAML **err_rc)
2863 {
2864         char **nids = NULL;
2865         int num, rc;
2866         struct cYAML *seq_no, *prim_nid;
2867
2868         num = yaml_copy_peer_nids(tree, &nids, true);
2869         if (num < 0)
2870                 return num;
2871
2872         seq_no = cYAML_get_object_item(tree, "seq_no");
2873         prim_nid = cYAML_get_object_item(tree, "primary nid");
2874
2875         rc = lustre_lnet_del_peer_nid((prim_nid) ? prim_nid->cy_valuestring : NULL,
2876                                       nids, num,
2877                                       (seq_no) ? seq_no->cy_valueint : -1,
2878                                       err_rc);
2879
2880         yaml_free_string_array(nids, num);
2881         return rc;
2882 }
2883
2884 static int handle_yaml_config_buffers(struct cYAML *tree,
2885                                       struct cYAML **show_rc,
2886                                       struct cYAML **err_rc)
2887 {
2888         int rc;
2889         struct cYAML *tiny, *small, *large, *seq_no;
2890
2891         tiny = cYAML_get_object_item(tree, "tiny");
2892         small = cYAML_get_object_item(tree, "small");
2893         large = cYAML_get_object_item(tree, "large");
2894         seq_no = cYAML_get_object_item(tree, "seq_no");
2895
2896         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2897                                         (small) ? small->cy_valueint : -1,
2898                                         (large) ? large->cy_valueint : -1,
2899                                         (seq_no) ? seq_no->cy_valueint : -1,
2900                                         err_rc);
2901
2902         return rc;
2903 }
2904
2905 static int handle_yaml_config_routing(struct cYAML *tree,
2906                                       struct cYAML **show_rc,
2907                                       struct cYAML **err_rc)
2908 {
2909         int rc = LUSTRE_CFG_RC_NO_ERR;
2910         struct cYAML *seq_no, *enable;
2911
2912         seq_no = cYAML_get_object_item(tree, "seq_no");
2913         enable = cYAML_get_object_item(tree, "enable");
2914
2915         if (enable) {
2916                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2917                                                 (seq_no) ?
2918                                                     seq_no->cy_valueint : -1,
2919                                                 err_rc);
2920         }
2921
2922         return rc;
2923 }
2924
2925 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2926                                  struct cYAML **err_rc)
2927 {
2928         struct cYAML *net;
2929         struct cYAML *gw;
2930         struct cYAML *seq_no;
2931
2932         net = cYAML_get_object_item(tree, "net");
2933         gw = cYAML_get_object_item(tree, "gateway");
2934         seq_no = cYAML_get_object_item(tree, "seq_no");
2935
2936         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2937                                      (gw) ? gw->cy_valuestring : NULL,
2938                                      (seq_no) ? seq_no->cy_valueint : -1,
2939                                      err_rc);
2940 }
2941
2942 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2943                                    struct cYAML **err_rc)
2944 {
2945         struct cYAML *seq_no;
2946
2947         seq_no = cYAML_get_object_item(tree, "seq_no");
2948
2949         return lustre_lnet_enable_routing(0, (seq_no) ?
2950                                                 seq_no->cy_valueint : -1,
2951                                         err_rc);
2952 }
2953
2954 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2955                                   struct cYAML **err_rc)
2956 {
2957         struct cYAML *net;
2958         struct cYAML *gw;
2959         struct cYAML *hop;
2960         struct cYAML *prio;
2961         struct cYAML *detail;
2962         struct cYAML *seq_no;
2963
2964         net = cYAML_get_object_item(tree, "net");
2965         gw = cYAML_get_object_item(tree, "gateway");
2966         hop = cYAML_get_object_item(tree, "hop");
2967         prio = cYAML_get_object_item(tree, "priority");
2968         detail = cYAML_get_object_item(tree, "detail");
2969         seq_no = cYAML_get_object_item(tree, "seq_no");
2970
2971         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2972                                       (gw) ? gw->cy_valuestring : NULL,
2973                                       (hop) ? hop->cy_valueint : -1,
2974                                       (prio) ? prio->cy_valueint : -1,
2975                                       (detail) ? detail->cy_valueint : 0,
2976                                       (seq_no) ? seq_no->cy_valueint : -1,
2977                                       show_rc,
2978                                       err_rc);
2979 }
2980
2981 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2982                                 struct cYAML **err_rc)
2983 {
2984         struct cYAML *net, *detail, *seq_no;
2985
2986         net = cYAML_get_object_item(tree, "net");
2987         detail = cYAML_get_object_item(tree, "detail");
2988         seq_no = cYAML_get_object_item(tree, "seq_no");
2989
2990         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2991                                     (detail) ? detail->cy_valueint : 0,
2992                                     (seq_no) ? seq_no->cy_valueint : -1,
2993                                     show_rc,
2994                                     err_rc);
2995 }
2996
2997 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2998                                     struct cYAML **err_rc)
2999 {
3000         struct cYAML *seq_no;
3001
3002         seq_no = cYAML_get_object_item(tree, "seq_no");
3003
3004         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
3005                                         show_rc, err_rc);
3006 }
3007
3008 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
3009                                   struct cYAML **err_rc)
3010 {
3011         struct cYAML *seq_no, *nid, *detail;
3012
3013         seq_no = cYAML_get_object_item(tree, "seq_no");
3014         detail = cYAML_get_object_item(tree, "detail");
3015         nid = cYAML_get_object_item(tree, "nid");
3016
3017         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
3018                                      (detail) ? detail->cy_valueint : 0,
3019                                      (seq_no) ? seq_no->cy_valueint : -1,
3020                                      show_rc, err_rc);
3021 }
3022
3023 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
3024                                   struct cYAML **err_rc)
3025 {
3026         struct cYAML *seq_no;
3027
3028         seq_no = cYAML_get_object_item(tree, "seq_no");
3029
3030         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
3031                                       show_rc, err_rc);
3032 }
3033
3034 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
3035                                   struct cYAML **err_rc)
3036 {
3037         struct cYAML *seq_no, *range;
3038
3039         seq_no = cYAML_get_object_item(tree, "seq_no");
3040         range = cYAML_get_object_item(tree, "range");
3041
3042         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
3043                                              seq_no ? seq_no->cy_valueint : -1,
3044                                              err_rc);
3045 }
3046
3047 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
3048                                struct cYAML **err_rc)
3049 {
3050         struct cYAML *seq_no;
3051
3052         seq_no = cYAML_get_object_item(tree, "seq_no");
3053
3054         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3055                                              err_rc);
3056 }
3057
3058 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3059                                 struct cYAML **err_rc)
3060 {
3061         struct cYAML *seq_no;
3062
3063         seq_no = cYAML_get_object_item(tree, "seq_no");
3064
3065         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3066                                            show_rc, err_rc);
3067 }
3068
3069 struct lookup_cmd_hdlr_tbl {
3070         char *name;
3071         cmd_handler_t cb;
3072 };
3073
3074 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3075         { .name = "route",      .cb = handle_yaml_config_route },
3076         { .name = "net",        .cb = handle_yaml_config_ni },
3077         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
3078         { .name = "peer",       .cb = handle_yaml_config_peer },
3079         { .name = "routing",    .cb = handle_yaml_config_routing },
3080         { .name = "buffers",    .cb = handle_yaml_config_buffers },
3081         { .name = "numa",       .cb = handle_yaml_config_numa },
3082         { .name = NULL } };
3083
3084 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3085         { .name = "route",      .cb = handle_yaml_del_route },
3086         { .name = "net",        .cb = handle_yaml_del_ni },
3087         { .name = "peer",       .cb = handle_yaml_del_peer },
3088         { .name = "routing",    .cb = handle_yaml_del_routing },
3089         { .name = "numa",       .cb = handle_yaml_del_numa },
3090         { .name = NULL } };
3091
3092 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3093         { .name = "route",      .cb = handle_yaml_show_route },
3094         { .name = "net",        .cb = handle_yaml_show_net },
3095         { .name = "buffers",    .cb = handle_yaml_show_routing },
3096         { .name = "routing",    .cb = handle_yaml_show_routing },
3097         { .name = "peer",       .cb = handle_yaml_show_peers },
3098         { .name = "statistics", .cb = handle_yaml_show_stats },
3099         { .name = "numa",       .cb = handle_yaml_show_numa },
3100         { .name = NULL } };
3101
3102 static cmd_handler_t lookup_fn(char *key,
3103                                struct lookup_cmd_hdlr_tbl *tbl)
3104 {
3105         int i;
3106         if (key == NULL)
3107                 return NULL;
3108
3109         for (i = 0; tbl[i].name != NULL; i++) {
3110                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3111                         return tbl[i].cb;
3112         }
3113
3114         return NULL;
3115 }
3116
3117 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3118                                  struct cYAML **show_rc, struct cYAML **err_rc)
3119 {
3120         struct cYAML *tree, *item = NULL, *head, *child;
3121         cmd_handler_t cb;
3122         char err_str[LNET_MAX_STR_LEN];
3123         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3124
3125         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3126         if (tree == NULL)
3127                 return LUSTRE_CFG_RC_BAD_PARAM;
3128
3129         child = tree->cy_child;
3130         while (child != NULL) {
3131                 cb = lookup_fn(child->cy_string, table);
3132                 if (cb == NULL) {
3133                         snprintf(err_str, sizeof(err_str),
3134                                 "\"call back for '%s' not found\"",
3135                                 child->cy_string);
3136                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3137                                         "yaml", "helper", err_str, err_rc);
3138                         goto out;
3139                 }
3140
3141                 if (cYAML_is_sequence(child)) {
3142                         while ((head = cYAML_get_next_seq_item(child, &item))
3143                                != NULL) {
3144                                 rc = cb(head, show_rc, err_rc);
3145                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3146                                         return_rc = rc;
3147                         }
3148                 } else {
3149                         rc = cb(child, show_rc, err_rc);
3150                         if (rc != LUSTRE_CFG_RC_NO_ERR)
3151                                 return_rc = rc;
3152                 }
3153                 item = NULL;
3154                 child = child->cy_next;
3155         }
3156
3157 out:
3158         cYAML_free_tree(tree);
3159
3160         return return_rc;
3161 }
3162
3163 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3164 {
3165         return lustre_yaml_cb_helper(f, lookup_config_tbl,
3166                                      NULL, err_rc);
3167 }
3168
3169 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3170 {
3171         return lustre_yaml_cb_helper(f, lookup_del_tbl,
3172                                      NULL, err_rc);
3173 }
3174
3175 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3176 {
3177         return lustre_yaml_cb_helper(f, lookup_show_tbl,
3178                                      show_rc, err_rc);
3179 }
3180