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