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