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