Whamcloud - gitweb
LU-7734 lnet: peer/peer_ni handling adjustments
[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                                 (data + sizeof(*conf));
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, "available_rtr_credits",
2110                                                 lpni_cri->cr_peer_rtr_credits)
2111                             == NULL)
2112                                 goto out;
2113
2114                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2115                                                 lpni_cri->cr_peer_min_rtr_credits)
2116                             == NULL)
2117                                 goto out;
2118
2119                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2120                                                 lpni_cri->cr_peer_tx_qnob)
2121                             == NULL)
2122                                 goto out;
2123
2124                         if (cYAML_create_number(peer_ni, "send_count",
2125                                                 lpni_stats->send_count)
2126                             == NULL)
2127                                 goto out;
2128
2129                         if (cYAML_create_number(peer_ni, "recv_count",
2130                                                 lpni_stats->recv_count)
2131                             == NULL)
2132                                 goto out;
2133
2134                         if (cYAML_create_number(peer_ni, "drop_count",
2135                                                 lpni_stats->drop_count)
2136                             == NULL)
2137                                 goto out;
2138
2139                         if (cYAML_create_number(peer_ni, "refcount",
2140                                                 lpni_cri->cr_refcount) == NULL)
2141                                 goto out;
2142                 }
2143
2144                 if (l_errno != ENOENT) {
2145                         snprintf(err_str,
2146                                 sizeof(err_str),
2147                                 "\"cannot get peer information: %s\"",
2148                                 strerror(l_errno));
2149                         rc = -l_errno;
2150                         goto out;
2151                 }
2152
2153                 j++;
2154         } while (j < ncpt);
2155
2156         /* print output iff show_rc is not provided */
2157         if (show_rc == NULL)
2158                 cYAML_print_tree(root);
2159
2160         snprintf(err_str, sizeof(err_str), "\"success\"");
2161         rc = LUSTRE_CFG_RC_NO_ERR;
2162
2163 out:
2164         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2165                 cYAML_free_tree(root);
2166         } else if (show_rc != NULL && *show_rc != NULL) {
2167                 struct cYAML *show_node;
2168                 /* find the peer node, if one doesn't exist then
2169                  * insert one.  Otherwise add to the one there
2170                  */
2171                 show_node = cYAML_get_object_item(*show_rc,
2172                                                   "peer");
2173                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2174                         cYAML_insert_child(show_node, first_seq);
2175                         free(peer_root);
2176                         free(root);
2177                 } else if (show_node == NULL) {
2178                         cYAML_insert_sibling((*show_rc)->cy_child,
2179                                              peer_root);
2180                         free(root);
2181                 } else {
2182                         cYAML_free_tree(root);
2183                 }
2184         } else {
2185                 *show_rc = root;
2186         }
2187
2188         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
2189                           err_rc);
2190
2191         return rc;
2192 }
2193
2194 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
2195                                 struct cYAML **err_rc)
2196 {
2197         struct lnet_ioctl_numa_range data;
2198         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2199         int l_errno;
2200         char err_str[LNET_MAX_STR_LEN];
2201         struct cYAML *root = NULL, *range = NULL;
2202
2203         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2204
2205         LIBCFS_IOC_INIT_V2(data, nr_hdr);
2206
2207         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_NUMA_RANGE, &data);
2208         if (rc != 0) {
2209                 l_errno = errno;
2210                 snprintf(err_str,
2211                          sizeof(err_str),
2212                          "\"cannot get numa range: %s\"",
2213                          strerror(l_errno));
2214                 rc = -l_errno;
2215                 goto out;
2216         }
2217
2218         root = cYAML_create_object(NULL, NULL);
2219         if (root == NULL)
2220                 goto out;
2221
2222         range = cYAML_create_object(root, "numa");
2223         if (range == NULL)
2224                 goto out;
2225
2226         if (cYAML_create_number(range, "range",
2227                                 data.nr_range) == NULL)
2228                 goto out;
2229
2230         if (show_rc == NULL)
2231                 cYAML_print_tree(root);
2232
2233         snprintf(err_str, sizeof(err_str), "\"success\"");
2234 out:
2235         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2236                 cYAML_free_tree(root);
2237         } else if (show_rc != NULL && *show_rc != NULL) {
2238                 cYAML_insert_sibling((*show_rc)->cy_child,
2239                                         root->cy_child);
2240                 free(root);
2241         } else {
2242                 *show_rc = root;
2243         }
2244
2245         cYAML_build_error(rc, seq_no, SHOW_CMD, "numa", err_str, err_rc);
2246
2247         return rc;
2248 }
2249
2250 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
2251                            struct cYAML **err_rc)
2252 {
2253         struct lnet_ioctl_lnet_stats data;
2254         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2255         int l_errno;
2256         char err_str[LNET_MAX_STR_LEN];
2257         struct cYAML *root = NULL, *stats = NULL;
2258
2259         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2260
2261         LIBCFS_IOC_INIT_V2(data, st_hdr);
2262
2263         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
2264         if (rc != 0) {
2265                 l_errno = errno;
2266                 snprintf(err_str,
2267                          sizeof(err_str),
2268                          "\"cannot get lnet statistics: %s\"",
2269                          strerror(l_errno));
2270                 rc = -l_errno;
2271                 goto out;
2272         }
2273
2274         root = cYAML_create_object(NULL, NULL);
2275         if (root == NULL)
2276                 goto out;
2277
2278         stats = cYAML_create_object(root, "statistics");
2279         if (stats == NULL)
2280                 goto out;
2281
2282         if (cYAML_create_number(stats, "msgs_alloc",
2283                                 data.st_cntrs.msgs_alloc) == NULL)
2284                 goto out;
2285
2286         if (cYAML_create_number(stats, "msgs_max",
2287                                 data.st_cntrs.msgs_max) == NULL)
2288                 goto out;
2289
2290         if (cYAML_create_number(stats, "errors",
2291                                 data.st_cntrs.errors) == NULL)
2292                 goto out;
2293
2294         if (cYAML_create_number(stats, "send_count",
2295                                 data.st_cntrs.send_count) == NULL)
2296                 goto out;
2297
2298         if (cYAML_create_number(stats, "recv_count",
2299                                 data.st_cntrs.recv_count) == NULL)
2300                 goto out;
2301
2302         if (cYAML_create_number(stats, "route_count",
2303                                 data.st_cntrs.route_count) == NULL)
2304                 goto out;
2305
2306         if (cYAML_create_number(stats, "drop_count",
2307                                 data.st_cntrs.drop_count) == NULL)
2308                 goto out;
2309
2310         if (cYAML_create_number(stats, "send_length",
2311                                 data.st_cntrs.send_length) == NULL)
2312                 goto out;
2313
2314         if (cYAML_create_number(stats, "recv_length",
2315                                 data.st_cntrs.recv_length) == NULL)
2316                 goto out;
2317
2318         if (cYAML_create_number(stats, "route_length",
2319                                 data.st_cntrs.route_length) == NULL)
2320                 goto out;
2321
2322         if (cYAML_create_number(stats, "drop_length",
2323                                 data.st_cntrs.drop_length) == NULL)
2324                 goto out;
2325
2326         if (show_rc == NULL)
2327                 cYAML_print_tree(root);
2328
2329         snprintf(err_str, sizeof(err_str), "\"success\"");
2330 out:
2331         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
2332                 cYAML_free_tree(root);
2333         } else if (show_rc != NULL && *show_rc != NULL) {
2334                 cYAML_insert_sibling((*show_rc)->cy_child,
2335                                         root->cy_child);
2336                 free(root);
2337         } else {
2338                 *show_rc = root;
2339         }
2340
2341         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
2342
2343         return rc;
2344 }
2345
2346 typedef int (*cmd_handler_t)(struct cYAML *tree,
2347                              struct cYAML **show_rc,
2348                              struct cYAML **err_rc);
2349
2350 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
2351                                     struct cYAML **err_rc)
2352 {
2353         struct cYAML *net, *gw, *hop, *prio, *seq_no;
2354
2355         net = cYAML_get_object_item(tree, "net");
2356         gw = cYAML_get_object_item(tree, "gateway");
2357         hop = cYAML_get_object_item(tree, "hop");
2358         prio = cYAML_get_object_item(tree, "priority");
2359         seq_no = cYAML_get_object_item(tree, "seq_no");
2360
2361         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
2362                                         (gw) ? gw->cy_valuestring : NULL,
2363                                         (hop) ? hop->cy_valueint : -1,
2364                                         (prio) ? prio->cy_valueint : -1,
2365                                         (seq_no) ? seq_no->cy_valueint : -1,
2366                                         err_rc);
2367 }
2368
2369 static void yaml_free_string_array(char **array, int num)
2370 {
2371         int i;
2372         char **sub_array = array;
2373
2374         for (i = 0; i < num; i++) {
2375                 if (*sub_array != NULL)
2376                         free(*sub_array);
2377                 sub_array++;
2378         }
2379         if (array)
2380                 free(array);
2381 }
2382
2383 /*
2384  *    interfaces:
2385  *        0: <intf_name>['['<expr>']']
2386  *        1: <intf_name>['['<expr>']']
2387  */
2388 static int yaml_copy_intf_info(struct cYAML *intf_tree,
2389                                struct lnet_dlc_network_descr *nw_descr)
2390 {
2391         struct cYAML *child = NULL;
2392         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2393         struct lnet_dlc_intf_descr *intf_descr, *tmp;
2394
2395         if (intf_tree == NULL || nw_descr == NULL)
2396                 return LUSTRE_CFG_RC_BAD_PARAM;
2397
2398         /* now grab all the interfaces and their cpts */
2399         child = intf_tree->cy_child;
2400         while (child != NULL) {
2401                 if (child->cy_valuestring == NULL) {
2402                         child = child->cy_next;
2403                         continue;
2404                 }
2405
2406                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
2407                         goto failed;
2408
2409                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
2410                                                 child->cy_valuestring,
2411                                                 strlen(child->cy_valuestring));
2412                 if (rc != LUSTRE_CFG_RC_NO_ERR)
2413                         goto failed;
2414
2415                 intf_num++;
2416                 child = child->cy_next;
2417         }
2418
2419         if (intf_num == 0)
2420                 return LUSTRE_CFG_RC_MISSING_PARAM;
2421
2422         return intf_num;
2423
2424 failed:
2425         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
2426                                  intf_on_network) {
2427                 if (intf_descr->cpt_expr != NULL)
2428                         cfs_expr_list_free(intf_descr->cpt_expr);
2429                 free(intf_descr);
2430         }
2431
2432         return rc;
2433 }
2434
2435 static bool
2436 yaml_extract_cmn_tunables(struct cYAML *tree,
2437                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
2438                           int *num_global_cpts,
2439                           struct cfs_expr_list **global_cpts)
2440 {
2441         struct cYAML *tun, *item, *smp;
2442
2443         tun = cYAML_get_object_item(tree, "tunables");
2444         if (tun != NULL) {
2445                 item = cYAML_get_object_item(tun, "peer_timeout");
2446                 if (item != NULL)
2447                         tunables->lct_peer_timeout = item->cy_valueint;
2448                 item = cYAML_get_object_item(tun, "peer_credits");
2449                 if (item != NULL)
2450                         tunables->lct_peer_tx_credits = item->cy_valueint;
2451                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
2452                 if (item != NULL)
2453                         tunables->lct_peer_rtr_credits = item->cy_valueint;
2454                 item = cYAML_get_object_item(tun, "credits");
2455                 if (item != NULL)
2456                         tunables->lct_max_tx_credits = item->cy_valueint;
2457                 smp = cYAML_get_object_item(tun, "CPT");
2458                 if (smp != NULL) {
2459                         *num_global_cpts =
2460                                 cfs_expr_list_parse(smp->cy_valuestring,
2461                                                  strlen(smp->cy_valuestring),
2462                                                  0, UINT_MAX, global_cpts);
2463                 }
2464
2465                 return true;
2466         }
2467
2468         return false;
2469 }
2470
2471 static bool
2472 yaml_extract_tunables(struct cYAML *tree,
2473                       struct lnet_ioctl_config_lnd_tunables *tunables,
2474                       int *num_global_cpts,
2475                       struct cfs_expr_list **global_cpts,
2476                       __u32 net_type)
2477 {
2478         bool rc;
2479
2480         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
2481                                        num_global_cpts, global_cpts);
2482
2483         if (!rc)
2484                 return rc;
2485
2486         lustre_yaml_extract_lnd_tunables(tree, net_type,
2487                                          &tunables->lt_tun);
2488
2489         return rc;
2490 }
2491
2492 /*
2493  * net:
2494  *    - net type: <net>[<NUM>]
2495   *      local NI(s):
2496  *        - nid: <ip>@<net>[<NUM>]
2497  *          status: up
2498  *          interfaces:
2499  *               0: <intf_name>['['<expr>']']
2500  *               1: <intf_name>['['<expr>']']
2501  *        tunables:
2502  *               peer_timeout: <NUM>
2503  *               peer_credits: <NUM>
2504  *               peer_buffer_credits: <NUM>
2505  *               credits: <NUM>
2506 *         lnd tunables:
2507  *               peercredits_hiw: <NUM>
2508  *               map_on_demand: <NUM>
2509  *               concurrent_sends: <NUM>
2510  *               fmr_pool_size: <NUM>
2511  *               fmr_flush_trigger: <NUM>
2512  *               fmr_cache: <NUM>
2513  *
2514  * At least one interface is required. If no interfaces are provided the
2515  * network interface can not be configured.
2516  */
2517 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
2518                                  struct cYAML **err_rc)
2519 {
2520         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
2521                      *item = NULL;
2522         int num_entries = 0, num_global_cpts = 0, rc;
2523         struct lnet_dlc_network_descr nw_descr;
2524         struct cfs_expr_list *global_cpts = NULL;
2525         struct lnet_ioctl_config_lnd_tunables tunables;
2526         bool found = false;
2527
2528         memset(&tunables, 0, sizeof(tunables));
2529
2530         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2531         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2532
2533         ip2net = cYAML_get_object_item(tree, "ip2net");
2534         net = cYAML_get_object_item(tree, "net type");
2535         if (net)
2536                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2537
2538         /*
2539          * if neither net nor ip2nets are present, then we can not
2540          * configure the network.
2541          */
2542         if (!net && !ip2net)
2543                 return LUSTRE_CFG_RC_MISSING_PARAM;
2544
2545         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2546         if (local_nis == NULL)
2547                 return LUSTRE_CFG_RC_MISSING_PARAM;
2548
2549         if (!cYAML_is_sequence(local_nis))
2550                 return LUSTRE_CFG_RC_BAD_PARAM;
2551
2552         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2553                 intf = cYAML_get_object_item(item, "interfaces");
2554                 if (intf == NULL)
2555                         continue;
2556                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2557                 if (num_entries <= 0) {
2558                         cYAML_build_error(num_entries, -1, "ni", "add",
2559                                         "bad interface list",
2560                                         err_rc);
2561                         return LUSTRE_CFG_RC_BAD_PARAM;
2562                 }
2563         }
2564
2565         found = yaml_extract_tunables(tree, &tunables, &num_global_cpts,
2566                                       &global_cpts,
2567                                       LNET_NETTYP(nw_descr.nw_id));
2568         seq_no = cYAML_get_object_item(tree, "seq_no");
2569
2570         rc = lustre_lnet_config_ni(&nw_descr,
2571                                    (num_global_cpts > 0) ? global_cpts: NULL,
2572                                    (ip2net) ? ip2net->cy_valuestring : NULL,
2573                                    (found) ? &tunables: NULL,
2574                                    (seq_no) ? seq_no->cy_valueint : -1,
2575                                    err_rc);
2576
2577         if (global_cpts != NULL)
2578                 cfs_expr_list_free(global_cpts);
2579
2580         return rc;
2581 }
2582
2583 /*
2584  * ip2nets:
2585  *  - net-spec: <tcp|o2ib|gni>[NUM]
2586  *    interfaces:
2587  *        0: <intf name>['['<expr>']']
2588  *        1: <intf name>['['<expr>']']
2589  *    ip-range:
2590  *        0: <expr.expr.expr.expr>
2591  *        1: <expr.expr.expr.expr>
2592  */
2593 static int handle_yaml_config_ip2nets(struct cYAML *tree,
2594                                       struct cYAML **show_rc,
2595                                       struct cYAML **err_rc)
2596 {
2597         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
2598                      *seq_no = NULL;
2599         struct lustre_lnet_ip2nets ip2nets;
2600         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
2601                                           *tmp = NULL;
2602         int rc = LUSTRE_CFG_RC_NO_ERR, num_global_cpts = 0;
2603         struct cfs_expr_list *global_cpts = NULL;
2604         struct cfs_expr_list *el, *el_tmp;
2605         struct lnet_ioctl_config_lnd_tunables tunables;
2606         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
2607         bool found = false;
2608
2609         memset(&tunables, 0, sizeof(tunables));
2610
2611         /* initialize all lists */
2612         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
2613         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
2614         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
2615
2616         net = cYAML_get_object_item(tree, "net-spec");
2617         if (net == NULL)
2618                 return LUSTRE_CFG_RC_BAD_PARAM;
2619
2620         if (net != NULL && net->cy_valuestring == NULL)
2621                 return LUSTRE_CFG_RC_BAD_PARAM;
2622
2623         /* assign the network id */
2624         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
2625         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
2626                 return LUSTRE_CFG_RC_BAD_PARAM;
2627
2628         seq_no = cYAML_get_object_item(tree, "seq_no");
2629
2630         intf = cYAML_get_object_item(tree, "interfaces");
2631         if (intf != NULL) {
2632                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
2633                 if (rc <= 0)
2634                         return LUSTRE_CFG_RC_BAD_PARAM;
2635         }
2636
2637         ip_range = cYAML_get_object_item(tree, "ip-range");
2638         if (ip_range != NULL) {
2639                 item = ip_range->cy_child;
2640                 while (item != NULL) {
2641                         if (item->cy_valuestring == NULL) {
2642                                 item = item->cy_next;
2643                                 continue;
2644                         }
2645
2646                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
2647                                                       item->cy_valuestring);
2648
2649                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2650                                 goto out;
2651
2652                         item = item->cy_next;
2653                 }
2654         }
2655
2656         found = yaml_extract_tunables(tree, &tunables, &num_global_cpts,
2657                                       &global_cpts,
2658                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
2659
2660         rc = lustre_lnet_config_ip2nets(&ip2nets,
2661                         (found) ? &tunables : NULL,
2662                         (num_global_cpts > 0) ? global_cpts : NULL,
2663                         (seq_no) ? seq_no->cy_valueint : -1,
2664                         err_rc);
2665
2666         /*
2667          * don't stop because there was no match. Continue processing the
2668          * rest of the rules. If non-match then nothing is configured
2669          */
2670         if (rc == LUSTRE_CFG_RC_NO_MATCH)
2671                 rc = LUSTRE_CFG_RC_NO_ERR;
2672 out:
2673         list_for_each_entry_safe(intf_descr, intf_tmp,
2674                                  &ip2nets.ip2nets_net.nw_intflist,
2675                                  intf_on_network) {
2676                 if (intf_descr->cpt_expr != NULL)
2677                         cfs_expr_list_free(intf_descr->cpt_expr);
2678                 free(intf_descr);
2679         }
2680
2681         list_for_each_entry_safe(ip_range_descr, tmp,
2682                                  &ip2nets.ip2nets_ip_ranges,
2683                                  ipr_entry) {
2684                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
2685                                          el_link)
2686                         cfs_expr_list_free(el);
2687                 free(ip_range_descr);
2688         }
2689
2690         return rc;
2691 }
2692
2693 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
2694                               struct cYAML **err_rc)
2695 {
2696         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
2697                      *local_nis = NULL;
2698         int num_entries, rc;
2699         struct lnet_dlc_network_descr nw_descr;
2700
2701         INIT_LIST_HEAD(&nw_descr.network_on_rule);
2702         INIT_LIST_HEAD(&nw_descr.nw_intflist);
2703
2704         net = cYAML_get_object_item(tree, "net type");
2705         if (net != NULL)
2706                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
2707
2708         local_nis = cYAML_get_object_item(tree, "local NI(s)");
2709         if (local_nis == NULL)
2710                 return LUSTRE_CFG_RC_MISSING_PARAM;
2711
2712         if (!cYAML_is_sequence(local_nis))
2713                 return LUSTRE_CFG_RC_BAD_PARAM;
2714
2715         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
2716                 intf = cYAML_get_object_item(item, "interfaces");
2717                 if (intf == NULL)
2718                         continue;
2719                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
2720                 if (num_entries <= 0) {
2721                         cYAML_build_error(num_entries, -1, "ni", "add",
2722                                         "bad interface list",
2723                                         err_rc);
2724                         return LUSTRE_CFG_RC_BAD_PARAM;
2725                 }
2726         }
2727
2728         seq_no = cYAML_get_object_item(tree, "seq_no");
2729
2730         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
2731                                 (seq_no) ? seq_no->cy_valueint : -1,
2732                                 err_rc);
2733
2734         return rc;
2735 }
2736
2737 static int yaml_copy_peer_nids(struct cYAML *tree, char ***nidsppp)
2738 {
2739         struct cYAML *nids_entry = NULL, *child = NULL, *entry = NULL;
2740         char **nids = NULL;
2741         int num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
2742
2743         nids_entry = cYAML_get_object_item(tree, "peer ni");
2744         if (cYAML_is_sequence(nids_entry)) {
2745                 while (cYAML_get_next_seq_item(nids_entry, &child))
2746                         num++;
2747         }
2748
2749         if (num == 0)
2750                 return LUSTRE_CFG_RC_MISSING_PARAM;
2751
2752         nids = calloc(sizeof(*nids) * num, 1);
2753         if (nids == NULL)
2754                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2755
2756         /* now grab all the nids */
2757         num = 0;
2758         child = NULL;
2759         while (cYAML_get_next_seq_item(nids_entry, &child)) {
2760                 entry = cYAML_get_object_item(child, "nid");
2761                 if (!entry)
2762                         continue;
2763                 nids[num] = calloc(strlen(entry->cy_valuestring) + 1, 1);
2764                 if (!nids[num]) {
2765                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2766                         goto failed;
2767                 }
2768                 strncpy(nids[num], entry->cy_valuestring,
2769                         strlen(entry->cy_valuestring));
2770                 num++;
2771         }
2772         rc = num;
2773
2774         *nidsppp = nids;
2775         return rc;
2776
2777 failed:
2778         if (nids != NULL)
2779                 yaml_free_string_array(nids, num);
2780         *nidsppp = NULL;
2781         return rc;
2782 }
2783
2784 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
2785                                    struct cYAML **err_rc)
2786 {
2787         char **nids = NULL;
2788         int num, rc;
2789         struct cYAML *seq_no, *key_nid, *non_mr;
2790
2791         num = yaml_copy_peer_nids(tree, &nids);
2792         if (num < 0)
2793                 return num;
2794
2795         seq_no = cYAML_get_object_item(tree, "seq_no");
2796         key_nid = cYAML_get_object_item(tree, "primary nid");
2797         non_mr = cYAML_get_object_item(tree, "non_mr");
2798
2799         rc = lustre_lnet_config_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2800                                          nids, num,
2801                                          (non_mr) ? false : true,
2802                                          (seq_no) ? seq_no->cy_valueint : -1,
2803                                          err_rc);
2804
2805         yaml_free_string_array(nids, num);
2806         return rc;
2807 }
2808
2809 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
2810                                 struct cYAML **err_rc)
2811 {
2812         char **nids = NULL;
2813         int num, rc;
2814         struct cYAML *seq_no, *key_nid;
2815
2816         num = yaml_copy_peer_nids(tree, &nids);
2817         if (num < 0)
2818                 return num;
2819
2820         seq_no = cYAML_get_object_item(tree, "seq_no");
2821         key_nid = cYAML_get_object_item(tree, "primary nid");
2822
2823         rc = lustre_lnet_del_peer_nid((key_nid) ? key_nid->cy_valuestring : NULL,
2824                                       nids, num,
2825                                       (seq_no) ? seq_no->cy_valueint : -1,
2826                                       err_rc);
2827
2828         yaml_free_string_array(nids, num);
2829         return rc;
2830 }
2831
2832 static int handle_yaml_config_buffers(struct cYAML *tree,
2833                                       struct cYAML **show_rc,
2834                                       struct cYAML **err_rc)
2835 {
2836         int rc;
2837         struct cYAML *tiny, *small, *large, *seq_no;
2838
2839         tiny = cYAML_get_object_item(tree, "tiny");
2840         small = cYAML_get_object_item(tree, "small");
2841         large = cYAML_get_object_item(tree, "large");
2842         seq_no = cYAML_get_object_item(tree, "seq_no");
2843
2844         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
2845                                         (small) ? small->cy_valueint : -1,
2846                                         (large) ? large->cy_valueint : -1,
2847                                         (seq_no) ? seq_no->cy_valueint : -1,
2848                                         err_rc);
2849
2850         return rc;
2851 }
2852
2853 static int handle_yaml_config_routing(struct cYAML *tree,
2854                                       struct cYAML **show_rc,
2855                                       struct cYAML **err_rc)
2856 {
2857         int rc = LUSTRE_CFG_RC_NO_ERR;
2858         struct cYAML *seq_no, *enable;
2859
2860         seq_no = cYAML_get_object_item(tree, "seq_no");
2861         enable = cYAML_get_object_item(tree, "enable");
2862
2863         if (enable) {
2864                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
2865                                                 (seq_no) ?
2866                                                     seq_no->cy_valueint : -1,
2867                                                 err_rc);
2868         }
2869
2870         return rc;
2871 }
2872
2873 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
2874                                  struct cYAML **err_rc)
2875 {
2876         struct cYAML *net;
2877         struct cYAML *gw;
2878         struct cYAML *seq_no;
2879
2880         net = cYAML_get_object_item(tree, "net");
2881         gw = cYAML_get_object_item(tree, "gateway");
2882         seq_no = cYAML_get_object_item(tree, "seq_no");
2883
2884         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
2885                                      (gw) ? gw->cy_valuestring : NULL,
2886                                      (seq_no) ? seq_no->cy_valueint : -1,
2887                                      err_rc);
2888 }
2889
2890 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
2891                                    struct cYAML **err_rc)
2892 {
2893         struct cYAML *seq_no;
2894
2895         seq_no = cYAML_get_object_item(tree, "seq_no");
2896
2897         return lustre_lnet_enable_routing(0, (seq_no) ?
2898                                                 seq_no->cy_valueint : -1,
2899                                         err_rc);
2900 }
2901
2902 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
2903                                   struct cYAML **err_rc)
2904 {
2905         struct cYAML *net;
2906         struct cYAML *gw;
2907         struct cYAML *hop;
2908         struct cYAML *prio;
2909         struct cYAML *detail;
2910         struct cYAML *seq_no;
2911
2912         net = cYAML_get_object_item(tree, "net");
2913         gw = cYAML_get_object_item(tree, "gateway");
2914         hop = cYAML_get_object_item(tree, "hop");
2915         prio = cYAML_get_object_item(tree, "priority");
2916         detail = cYAML_get_object_item(tree, "detail");
2917         seq_no = cYAML_get_object_item(tree, "seq_no");
2918
2919         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
2920                                       (gw) ? gw->cy_valuestring : NULL,
2921                                       (hop) ? hop->cy_valueint : -1,
2922                                       (prio) ? prio->cy_valueint : -1,
2923                                       (detail) ? detail->cy_valueint : 0,
2924                                       (seq_no) ? seq_no->cy_valueint : -1,
2925                                       show_rc,
2926                                       err_rc);
2927 }
2928
2929 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
2930                                 struct cYAML **err_rc)
2931 {
2932         struct cYAML *net, *detail, *seq_no;
2933
2934         net = cYAML_get_object_item(tree, "net");
2935         detail = cYAML_get_object_item(tree, "detail");
2936         seq_no = cYAML_get_object_item(tree, "seq_no");
2937
2938         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
2939                                     (detail) ? detail->cy_valueint : 0,
2940                                     (seq_no) ? seq_no->cy_valueint : -1,
2941                                     show_rc,
2942                                     err_rc);
2943 }
2944
2945 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
2946                                     struct cYAML **err_rc)
2947 {
2948         struct cYAML *seq_no;
2949
2950         seq_no = cYAML_get_object_item(tree, "seq_no");
2951
2952         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
2953                                         show_rc, err_rc);
2954 }
2955
2956 static int handle_yaml_show_credits(struct cYAML *tree, struct cYAML **show_rc,
2957                                     struct cYAML **err_rc)
2958 {
2959         struct cYAML *seq_no, *key_nid, *detail;
2960
2961         seq_no = cYAML_get_object_item(tree, "seq_no");
2962         detail = cYAML_get_object_item(tree, "detail");
2963         key_nid = cYAML_get_object_item(tree, "key_nid");
2964
2965         return lustre_lnet_show_peer((key_nid) ? key_nid->cy_valuestring : NULL,
2966                                      (detail) ? detail->cy_valueint : 0,
2967                                      (seq_no) ? seq_no->cy_valueint : -1,
2968                                      show_rc, err_rc);
2969 }
2970
2971 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
2972                                   struct cYAML **err_rc)
2973 {
2974         struct cYAML *seq_no;
2975
2976         seq_no = cYAML_get_object_item(tree, "seq_no");
2977
2978         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
2979                                       show_rc, err_rc);
2980 }
2981
2982 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
2983                                   struct cYAML **err_rc)
2984 {
2985         struct cYAML *seq_no, *range;
2986
2987         seq_no = cYAML_get_object_item(tree, "seq_no");
2988         range = cYAML_get_object_item(tree, "range");
2989
2990         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
2991                                              seq_no ? seq_no->cy_valueint : -1,
2992                                              err_rc);
2993 }
2994
2995 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
2996                                struct cYAML **err_rc)
2997 {
2998         struct cYAML *seq_no;
2999
3000         seq_no = cYAML_get_object_item(tree, "seq_no");
3001
3002         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
3003                                              err_rc);
3004 }
3005
3006 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
3007                                 struct cYAML **err_rc)
3008 {
3009         struct cYAML *seq_no;
3010
3011         seq_no = cYAML_get_object_item(tree, "seq_no");
3012
3013         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
3014                                            show_rc, err_rc);
3015 }
3016
3017 struct lookup_cmd_hdlr_tbl {
3018         char *name;
3019         cmd_handler_t cb;
3020 };
3021
3022 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
3023         {"route", handle_yaml_config_route},
3024         {"net", handle_yaml_config_ni},
3025         {"ip2nets", handle_yaml_config_ip2nets},
3026         {"peer", handle_yaml_config_peer},
3027         {"routing", handle_yaml_config_routing},
3028         {"buffers", handle_yaml_config_buffers},
3029         {"numa", handle_yaml_config_numa},
3030         {NULL, NULL}
3031 };
3032
3033 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
3034         {"route", handle_yaml_del_route},
3035         {"net", handle_yaml_del_ni},
3036         {"peer", handle_yaml_del_peer},
3037         {"routing", handle_yaml_del_routing},
3038         {"numa", handle_yaml_del_numa},
3039         {NULL, NULL}
3040 };
3041
3042 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
3043         {"route", handle_yaml_show_route},
3044         {"net", handle_yaml_show_net},
3045         {"buffers", handle_yaml_show_routing},
3046         {"routing", handle_yaml_show_routing},
3047         {"credits", handle_yaml_show_credits},
3048         {"statistics", handle_yaml_show_stats},
3049         {"numa", handle_yaml_show_numa},
3050         {NULL, NULL}
3051 };
3052
3053 static cmd_handler_t lookup_fn(char *key,
3054                                struct lookup_cmd_hdlr_tbl *tbl)
3055 {
3056         int i;
3057         if (key == NULL)
3058                 return NULL;
3059
3060         for (i = 0; tbl[i].name != NULL; i++) {
3061                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
3062                         return tbl[i].cb;
3063         }
3064
3065         return NULL;
3066 }
3067
3068 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
3069                                  struct cYAML **show_rc, struct cYAML **err_rc)
3070 {
3071         struct cYAML *tree, *item = NULL, *head, *child;
3072         cmd_handler_t cb;
3073         char err_str[LNET_MAX_STR_LEN];
3074         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
3075
3076         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
3077         if (tree == NULL)
3078                 return LUSTRE_CFG_RC_BAD_PARAM;
3079
3080         child = tree->cy_child;
3081         while (child != NULL) {
3082                 cb = lookup_fn(child->cy_string, table);
3083                 if (cb == NULL) {
3084                         snprintf(err_str, sizeof(err_str),
3085                                 "\"call back for '%s' not found\"",
3086                                 child->cy_string);
3087                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
3088                                         "yaml", "helper", err_str, err_rc);
3089                         goto out;
3090                 }
3091
3092                 if (cYAML_is_sequence(child)) {
3093                         while ((head = cYAML_get_next_seq_item(child, &item))
3094                                != NULL) {
3095                                 rc = cb(head, show_rc, err_rc);
3096                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3097                                         return_rc = rc;
3098                         }
3099                 } else {
3100                         rc = cb(child, show_rc, err_rc);
3101                         if (rc != LUSTRE_CFG_RC_NO_ERR)
3102                                 return_rc = rc;
3103                 }
3104                 item = NULL;
3105                 child = child->cy_next;
3106         }
3107
3108 out:
3109         cYAML_free_tree(tree);
3110
3111         return return_rc;
3112 }
3113
3114 int lustre_yaml_config(char *f, struct cYAML **err_rc)
3115 {
3116         return lustre_yaml_cb_helper(f, lookup_config_tbl,
3117                                      NULL, err_rc);
3118 }
3119
3120 int lustre_yaml_del(char *f, struct cYAML **err_rc)
3121 {
3122         return lustre_yaml_cb_helper(f, lookup_del_tbl,
3123                                      NULL, err_rc);
3124 }
3125
3126 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
3127 {
3128         return lustre_yaml_cb_helper(f, lookup_show_tbl,
3129                                      show_rc, err_rc);
3130 }
3131
3132 int lustre_lnet_send_dbg_task(enum lnet_dbg_task dbg_task,
3133                               struct lnet_dbg_task_info *dbg_info,
3134                               struct cYAML **show_rc,
3135                               struct cYAML **err_rc)
3136 {
3137         struct lnet_ioctl_dbg *dbg;
3138         struct lnet_dbg_task_info *info;
3139         int rc = LUSTRE_CFG_RC_NO_ERR;
3140         char err_str[LNET_MAX_STR_LEN];
3141
3142         snprintf(err_str, sizeof(err_str), "\"success\"");
3143
3144         dbg = calloc(1, sizeof(*dbg) + sizeof(*info));
3145         if (!dbg) {
3146                 snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3147                 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3148                 goto out;
3149         }
3150
3151         info = (struct lnet_dbg_task_info *)dbg->dbg_bulk;
3152
3153         LIBCFS_IOC_INIT_V2(*dbg, dbg_hdr);
3154
3155         dbg->dbg_task = dbg_task;
3156         if (dbg_info)
3157                 memcpy(info, dbg_info, sizeof(*info));
3158
3159         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DBG, dbg);
3160         if (rc != 0) {
3161                 rc = -errno;
3162                 snprintf(err_str,
3163                          sizeof(err_str),
3164                          "\"debug task failed %s\"", strerror(errno));
3165                 goto out;
3166         }
3167
3168 out:
3169         cYAML_build_error(rc, -1, DBG_CMD,
3170                          "debug", err_str, err_rc);
3171
3172         return rc;
3173 }
3174