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