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