Whamcloud - gitweb
LU-12222 lnet: Introduce constant for the lolnd NID
[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, 2017, 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 <rdma/rdma_user_cm.h>
54 #include "liblnetconfig.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 MAX_NUM_IPS             128
65
66 #define modparam_path "/sys/module/lnet/parameters/"
67 #define o2ib_modparam_path "/sys/module/ko2iblnd/parameters/"
68 #define gni_nid_path "/proc/cray_xt/"
69
70 #ifndef HAVE_USRSPC_RDMA_PS_TCP
71 #define RDMA_PS_TCP 0x0106
72 #endif
73
74 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
75                                  "dropped_stats"};
76
77 /*
78  * lustre_lnet_ip_range_descr
79  *      Describes an IP range.
80  *      Each octect is an expression
81  */
82 struct lustre_lnet_ip_range_descr {
83         struct list_head ipr_entry;
84         struct list_head ipr_expr;
85 };
86
87 /*
88  * lustre_lnet_ip2nets
89  *      Describes an ip2nets rule. This can be on a list of rules.
90  */
91 struct lustre_lnet_ip2nets {
92         struct lnet_dlc_network_descr ip2nets_net;
93         struct list_head ip2nets_ip_ranges;
94 };
95
96 int open_sysfs_file(const char *path, const char *attr, const int mode)
97 {
98         int fd;
99         char filename[LNET_MAX_STR_LEN];
100
101         if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
102                 return -1;
103
104         snprintf(filename, sizeof(filename), "%s%s",
105                  path, attr);
106
107         fd = open(filename, mode);
108
109         return fd;
110 }
111
112 static int read_sysfs_file(const char *path, const char *attr,
113                            void *val, const size_t size, const int nelem)
114 {
115         int fd;
116         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
117
118         fd = open_sysfs_file(path, attr, O_RDONLY);
119         if (fd == -1)
120                 return LUSTRE_CFG_RC_NO_MATCH;
121
122         if (read(fd, val, size * nelem) == -1)
123                 goto close_fd;
124
125         rc = LUSTRE_CFG_RC_NO_ERR;
126
127 close_fd:
128         close(fd);
129         return rc;
130 }
131
132 static int write_sysfs_file(const char *path, const char *attr,
133                             void *val, const size_t size, const int nelem)
134 {
135         int fd;
136         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
137
138         fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
139         if (fd == -1)
140                 return LUSTRE_CFG_RC_NO_MATCH;
141
142         if (write(fd, val, size * nelem) == -1)
143                 goto close_fd;
144
145         rc = LUSTRE_CFG_RC_NO_ERR;
146
147 close_fd:
148         close(fd);
149         return rc;
150 }
151
152 /*
153  * free_intf_descr
154  *      frees the memory allocated for an intf descriptor.
155  */
156 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
157 {
158         if (!intf_descr)
159                 return;
160
161         if (intf_descr->cpt_expr != NULL)
162                 cfs_expr_list_free(intf_descr->cpt_expr);
163         free(intf_descr);
164 }
165
166 /*
167  * lustre_lnet_add_ip_range
168  * Formatting:
169  *      given a string of the format:
170  *      <expr.expr.expr.expr> parse each expr into
171  *      a lustre_lnet_ip_range_descr structure and insert on the list.
172  *
173  *      This function is called from
174  *              YAML on each ip-range.
175  *              As a result of lnetctl command
176  *              When building a NID or P2P selection rules
177  */
178 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
179 {
180         struct lustre_lnet_ip_range_descr *ip_range;
181         int rc;
182
183         ip_range = calloc(1, sizeof(*ip_range));
184         if (ip_range == NULL)
185                 return LUSTRE_CFG_RC_OUT_OF_MEM;
186
187         INIT_LIST_HEAD(&ip_range->ipr_entry);
188         INIT_LIST_HEAD(&ip_range->ipr_expr);
189
190         rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
191                                &ip_range->ipr_expr);
192         if (rc != 0)
193                 return LUSTRE_CFG_RC_BAD_PARAM;
194
195         list_add_tail(&ip_range->ipr_entry, list);
196
197         return LUSTRE_CFG_RC_NO_ERR;
198 }
199
200 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
201 {
202         char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
203              *intf_name;
204         struct lnet_dlc_intf_descr *intf_descr = NULL;
205         int rc;
206         char intf_string[LNET_MAX_STR_LEN];
207
208         if (len >= LNET_MAX_STR_LEN)
209                 return LUSTRE_CFG_RC_BAD_PARAM;
210
211         strncpy(intf_string, intf, len);
212         intf_string[len] = '\0';
213
214         intf_descr = calloc(1, sizeof(*intf_descr));
215         if (intf_descr == NULL)
216                 return LUSTRE_CFG_RC_OUT_OF_MEM;
217
218         INIT_LIST_HEAD(&intf_descr->intf_on_network);
219
220         intf_name = intf_string;
221         open_sq_bracket = strchr(intf_string, '[');
222         if (open_sq_bracket != NULL) {
223                 close_sq_bracket = strchr(intf_string, ']');
224                 if (close_sq_bracket == NULL) {
225                         free(intf_descr);
226                         return LUSTRE_CFG_RC_BAD_PARAM;
227                 }
228                 rc = cfs_expr_list_parse(open_sq_bracket,
229                                          strlen(open_sq_bracket), 0, UINT_MAX,
230                                          &intf_descr->cpt_expr);
231                 if (rc < 0) {
232                         free(intf_descr);
233                         return LUSTRE_CFG_RC_BAD_PARAM;
234                 }
235                 strncpy(intf_descr->intf_name, intf_name,
236                         open_sq_bracket - intf_name);
237                 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
238         } else {
239                 strcpy(intf_descr->intf_name, intf_name);
240                 intf_descr->cpt_expr = NULL;
241         }
242
243         list_add_tail(&intf_descr->intf_on_network, list);
244
245         return LUSTRE_CFG_RC_NO_ERR;
246 }
247
248 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
249 {
250         if (nw_descr != NULL) {
251                 nw_descr->nw_id = 0;
252                 INIT_LIST_HEAD(&nw_descr->network_on_rule);
253                 INIT_LIST_HEAD(&nw_descr->nw_intflist);
254         }
255 }
256
257 int tokenize_nidstr(char *nidstr, char *out[LNET_MAX_STR_LEN], char *err_str)
258 {
259         int bracket = 0, num_str = 0;
260         char *strstart, *chptr;
261
262         if (!nidstr) {
263                 snprintf(err_str, LNET_MAX_STR_LEN, "supplied nidstr is NULL");
264                 return LUSTRE_CFG_RC_BAD_PARAM;
265         }
266
267         /* comma-separated nidranges -> space-separated nidranges */
268         chptr = &nidstr[0];
269         strstart = chptr;
270         while (true) {
271                 if (*chptr == '\0') {
272                         out[num_str] = strstart;
273                         num_str++;
274                         break;
275                 }
276
277                 if (*chptr == '[')
278                         bracket++;
279                 else if (*chptr == ']')
280                         bracket--;
281
282                 if (bracket < 0) {
283                         snprintf(err_str, LNET_MAX_STR_LEN,
284                                  "Mismatched brackets in nidstring \"%s\"",
285                                  nidstr);
286                         return LUSTRE_CFG_RC_BAD_PARAM;
287                 }
288
289                 if (bracket == 0 && *chptr == ',') {
290                         out[num_str] = strstart;
291                         *chptr = '\0';
292                         num_str++;
293                         strstart = chptr + 1;
294                 }
295
296                 chptr++;
297         }
298
299         if (bracket != 0) {
300                 snprintf(err_str, LNET_MAX_STR_LEN,
301                          "Mismatched brackets in nidstring \"%s\"", nidstr);
302                 return LUSTRE_CFG_RC_BAD_PARAM;
303         }
304
305         return num_str;
306 }
307
308 int lustre_lnet_parse_nidstr(char *nidstr, lnet_nid_t *lnet_nidlist,
309                              int max_nids, char *err_str)
310 {
311         int rc, num_nids = 0;
312         struct list_head nidlist;
313
314         if (!nidstr) {
315                 snprintf(err_str, LNET_MAX_STR_LEN, "supplied nidstr is NULL");
316                 return LUSTRE_CFG_RC_BAD_PARAM;
317         }
318
319         if (strchr(nidstr, '*')) {
320                 snprintf(err_str, LNET_MAX_STR_LEN,
321                          "asterisk not allowed in nidstring \"%s\"", nidstr);
322                 return LUSTRE_CFG_RC_BAD_PARAM;
323         }
324
325         INIT_LIST_HEAD(&nidlist);
326         rc = cfs_parse_nidlist(nidstr, strlen(nidstr), &nidlist);
327         if (rc == 0) {
328                 snprintf(err_str, LNET_MAX_STR_LEN,
329                          "Unable to parse nidlist from: %s\n", nidstr);
330                 return LUSTRE_CFG_RC_BAD_PARAM;
331         }
332
333         if (list_empty(&nidlist)) {
334                 snprintf(err_str, LNET_MAX_STR_LEN,
335                          "\"%s\" does not specify any valid nid lists", nidstr);
336                 return LUSTRE_CFG_RC_BAD_PARAM;
337         }
338
339         num_nids = cfs_expand_nidlist(&nidlist, lnet_nidlist, max_nids);
340
341         if (num_nids == -1) {
342                 snprintf(err_str, LNET_MAX_STR_LEN,
343                          "\"%s\" specifies more than the %d NIDs allowed by this operation.",
344                          nidstr, max_nids);
345                 return LUSTRE_CFG_RC_BAD_PARAM;
346         }
347
348         if (num_nids < 0) {
349                 snprintf(err_str, LNET_MAX_STR_LEN,
350                          "Failed to expand nidstr: %s", strerror(num_nids));
351                 return LUSTRE_CFG_RC_OUT_OF_MEM;
352         }
353
354         if (num_nids == 0) {
355                 snprintf(err_str, LNET_MAX_STR_LEN,
356                          "\"%s\" did not expand to any nids", nidstr);
357                 return LUSTRE_CFG_RC_BAD_PARAM;
358         }
359
360         return num_nids;
361 }
362
363 /*
364  * format expected:
365  *      <intf>[<expr>], <intf>[<expr>],..
366  */
367 int lustre_lnet_parse_interfaces(char *intf_str,
368                                  struct lnet_dlc_network_descr *nw_descr)
369 {
370         char *open_square;
371         char *close_square;
372         char *comma;
373         char *cur = intf_str, *next = NULL;
374         char *end = intf_str + strlen(intf_str);
375         int rc, len;
376         struct lnet_dlc_intf_descr *intf_descr, *tmp;
377
378         if (nw_descr == NULL)
379                 return LUSTRE_CFG_RC_BAD_PARAM;
380
381         while (cur < end) {
382                 open_square = strchr(cur, '[');
383                 if (open_square != NULL) {
384                         close_square = strchr(cur, ']');
385                         if (close_square == NULL) {
386                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
387                                 goto failed;
388                         }
389
390                         comma = strchr(cur, ',');
391                         if (comma != NULL && comma > close_square) {
392                                 next = comma + 1;
393                                 len = next - close_square;
394                         } else {
395                                 len = strlen(cur);
396                                 next = cur + len;
397                         }
398                 } else {
399                         comma = strchr(cur, ',');
400                         if (comma != NULL) {
401                                 next = comma + 1;
402                                 len = comma - cur;
403                         } else {
404                                 len = strlen(cur);
405                                 next = cur + len;
406                         }
407                 }
408
409                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
410                 if (rc != LUSTRE_CFG_RC_NO_ERR)
411                         goto failed;
412
413                 cur = next;
414         }
415
416         return LUSTRE_CFG_RC_NO_ERR;
417
418 failed:
419         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
420                                  intf_on_network) {
421                 list_del(&intf_descr->intf_on_network);
422                 free_intf_descr(intf_descr);
423         }
424
425         return rc;
426 }
427
428 int lustre_lnet_config_lib_init(void)
429 {
430         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
431 }
432
433 void lustre_lnet_config_lib_uninit(void)
434 {
435         unregister_ioc_dev(LNET_DEV_ID);
436 }
437
438 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
439                                  int seq_no, struct cYAML **err_rc)
440 {
441         struct libcfs_ioctl_data data;
442         unsigned int opc;
443         int rc;
444         char err_str[LNET_MAX_STR_LEN];
445
446         snprintf(err_str, sizeof(err_str), "\"Success\"");
447
448         LIBCFS_IOC_INIT(data);
449
450         /* Reverse logic is used here in order not to change
451          * the lctl utility */
452         data.ioc_flags = load_ni_from_mod ? 0 : 1;
453
454         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
455
456         rc = l_ioctl(LNET_DEV_ID, opc, &data);
457         if (rc != 0) {
458                 snprintf(err_str,
459                         sizeof(err_str),
460                         "\"LNet %s error: %s\"", (up) ? "configure" :
461                         "unconfigure", strerror(errno));
462                 rc = -errno;
463         }
464
465         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
466                           "lnet", err_str, err_rc);
467
468         return rc;
469 }
470
471 static int dispatch_peer_ni_cmd(__u32 cmd, struct lnet_ioctl_peer_cfg *data,
472                                 char *err_str, char *cmd_str)
473 {
474         int rc;
475
476         rc = l_ioctl(LNET_DEV_ID, cmd, data);
477         if (rc) {
478                 rc = -errno;
479                 snprintf(err_str, LNET_MAX_STR_LEN,
480                          "\"%s peer ni operation failed: %s\"",
481                          cmd_str, strerror(errno));
482         }
483
484         return rc;
485 }
486
487 static int infra_ping_nid(char *ping_nids, char *oper, int param, int ioc_call,
488                           int seq_no, struct cYAML **show_rc,
489                           struct cYAML **err_rc)
490 {
491         void *data = NULL;
492         struct lnet_ioctl_ping_data ping;
493         struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
494                      *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
495         struct lnet_process_id id;
496         char err_str[LNET_MAX_STR_LEN] = {0};
497         char *sep, *token, *end;
498         char buf[6];
499         size_t len;
500         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
501         int i;
502         bool flag = false;
503
504         len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
505
506         data = calloc(1, len);
507         if (data == NULL)
508                 goto out;
509
510         /* create struct cYAML root object */
511         root = cYAML_create_object(NULL, NULL);
512         if (root == NULL)
513                 goto out;
514
515         ping_node = cYAML_create_seq(root, oper);
516         if (ping_node == NULL)
517                 goto out;
518
519         /* tokenise each nid in string ping_nids */
520         token = strtok(ping_nids, ",");
521
522         do {
523                 item = cYAML_create_seq_item(ping_node);
524                 if (item == NULL)
525                         goto out;
526
527                 if (first_seq == NULL)
528                         first_seq = item;
529
530                 /* check if '-' is a part of NID, token */
531                 sep = strchr(token, '-');
532                 if (sep == NULL) {
533                         id.pid = LNET_PID_ANY;
534                         /* if no net is specified, libcfs_str2nid() will assume tcp */
535                         id.nid = libcfs_str2nid(token);
536                         if (id.nid == LNET_NID_ANY) {
537                                 snprintf(err_str, sizeof(err_str),
538                                          "\"cannot parse NID '%s'\"",
539                                          token);
540                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
541                                 cYAML_build_error(rc, seq_no, MANAGE_CMD,
542                                                   oper, err_str, err_rc);
543                                 continue;
544                         }
545                 } else {
546                         if (token[0] == 'u' || token[0] == 'U')
547                                 id.pid = (strtoul(&token[1], &end, 0) |
548                                           (LNET_PID_USERFLAG));
549                         else
550                                 id.pid = strtoul(token, &end, 0);
551
552                         /* assuming '-' is part of hostname */
553                         if (end != sep) {
554                                 id.pid = LNET_PID_ANY;
555                                 id.nid = libcfs_str2nid(token);
556                                 if (id.nid == LNET_NID_ANY) {
557                                         snprintf(err_str, sizeof(err_str),
558                                                  "\"cannot parse NID '%s'\"",
559                                                  token);
560                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
561                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
562                                                           oper, err_str,
563                                                           err_rc);
564                                         continue;
565                                 }
566                         } else {
567                                 id.nid = libcfs_str2nid(sep + 1);
568                                 if (id.nid == LNET_NID_ANY) {
569                                         snprintf(err_str, sizeof(err_str),
570                                                  "\"cannot parse NID '%s'\"",
571                                                  token);
572                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
573                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
574                                                           oper, err_str,
575                                                           err_rc);
576                                         continue;
577                                 }
578                         }
579                 }
580                 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
581                 ping.ping_hdr.ioc_len = sizeof(ping);
582                 ping.ping_id          = id;
583                 ping.op_param         = param;
584                 ping.ping_count       = LNET_INTERFACES_MAX_DEFAULT;
585                 ping.ping_buf         = data;
586
587                 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
588                 if (rc != 0) {
589                         snprintf(err_str,
590                                  sizeof(err_str), "failed to %s %s: %s\n", oper,
591                                  id.pid == LNET_PID_ANY ?
592                                  libcfs_nid2str(id.nid) :
593                                  libcfs_id2str(id), strerror(errno));
594                         rc = LUSTRE_CFG_RC_BAD_PARAM;
595                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
596                                           oper, err_str, err_rc);
597                         continue;
598                 }
599
600                 if (cYAML_create_string(item, "primary nid",
601                                         libcfs_nid2str(ping.ping_id.nid)) == NULL)
602                         goto out;
603
604                 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
605                                         "True" : "False") == NULL)
606                         goto out;
607
608                 tmp = cYAML_create_seq(item, "peer ni");
609                 if (tmp == NULL)
610                         goto out;
611
612                 for (i = 0; i < ping.ping_count; i++) {
613                         if (ping.ping_buf[i].nid == LNET_NID_LO_0)
614                                 continue;
615                         peer_ni = cYAML_create_seq_item(tmp);
616                         if (peer_ni == NULL)
617                                 goto out;
618                         memset(buf, 0, sizeof buf);
619                         snprintf(buf, sizeof buf, "nid");
620                         if (cYAML_create_string(peer_ni, buf,
621                                                 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
622                                 goto out;
623                 }
624
625                 flag = true;
626
627         } while ((token = strtok(NULL, ",")) != NULL);
628
629         if (flag)
630                 rc = LUSTRE_CFG_RC_NO_ERR;
631
632 out:
633         if (data)
634                 free(data);
635         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
636                 cYAML_free_tree(root);
637         } else if (show_rc != NULL && *show_rc != NULL) {
638                 struct cYAML *show_node;
639                 show_node = cYAML_get_object_item(*show_rc, oper);
640                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
641                         cYAML_insert_child(show_node, first_seq);
642                         free(ping_node);
643                         free(root);
644                 } else if (show_node == NULL) {
645                         cYAML_insert_sibling((*show_rc)->cy_child,
646                                              ping_node);
647                         free(root);
648                 } else {
649                         cYAML_free_tree(root);
650                 }
651         } else {
652                 *show_rc = root;
653         }
654
655         return rc;
656 }
657
658 int lustre_lnet_ping_nid(char *ping_nids, int timeout, int seq_no,
659                          struct cYAML **show_rc, struct cYAML **err_rc)
660 {
661         int rc;
662
663         rc = infra_ping_nid(ping_nids, "ping", timeout, IOC_LIBCFS_PING_PEER,
664                             seq_no, show_rc, err_rc);
665         return rc;
666 }
667
668 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
669                          struct cYAML **show_rc, struct cYAML **err_rc)
670 {
671         int rc;
672
673         rc = infra_ping_nid(ping_nids, "discover", force, IOC_LIBCFS_DISCOVER,
674                             seq_no, show_rc, err_rc);
675         return rc;
676 }
677
678 static int lustre_lnet_handle_peer_nidlist(lnet_nid_t *nidlist, int num_nids,
679                                            bool is_mr, __u32 cmd,
680                                            char *cmd_type, char *err_str)
681 {
682         struct lnet_ioctl_peer_cfg data;
683         int rc, nid_idx;
684
685         if (cmd == IOC_LIBCFS_ADD_PEER_NI) {
686                 /* When adding a peer we first need to create the peer using the
687                  * specified (or implied) primary nid. Then we can add
688                  * additional nids to this peer using the primary nid as a key
689                  */
690                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
691                 data.prcfg_mr = is_mr;
692                 data.prcfg_prim_nid = nidlist[0];
693                 data.prcfg_cfg_nid = LNET_NID_ANY;
694
695                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
696
697                 if (rc)
698                         return rc;
699         }
700
701         /* Add or delete any specified NIs associated with the specified
702          * (or implied) primary nid
703          */
704         for (nid_idx = 1; nid_idx < num_nids; nid_idx++) {
705                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
706                 data.prcfg_mr = is_mr;
707                 data.prcfg_prim_nid = nidlist[0];
708                 data.prcfg_cfg_nid = nidlist[nid_idx];
709
710                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
711
712                 if (rc)
713                         return rc;
714         }
715
716         if (cmd == IOC_LIBCFS_DEL_PEER_NI && num_nids == 1) {
717                 /* In the delete case we may have been given just the
718                  * primary nid of the peer. This tells us to delete the peer
719                  * completely (rather than just delete some of its NIs)
720                  */
721                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
722                 data.prcfg_prim_nid = nidlist[0];
723                 data.prcfg_cfg_nid = LNET_NID_ANY;
724
725                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
726         }
727
728         return rc;
729 }
730
731 int lustre_lnet_config_peer_nidlist(char *pnidstr, lnet_nid_t *lnet_nidlist,
732                                     int num_nids, bool is_mr, int seq_no,
733                                     struct cYAML **err_rc)
734 {
735         int rc = LUSTRE_CFG_RC_NO_ERR;
736         char err_str[LNET_MAX_STR_LEN];
737         lnet_nid_t *lnet_nidlist2 = NULL;
738         lnet_nid_t pnid;
739
740         if (pnidstr) {
741                 pnid = libcfs_str2nid(pnidstr);
742                 if (pnid == LNET_NID_ANY) {
743                         snprintf(err_str, LNET_MAX_STR_LEN,
744                                  "bad primary NID: '%s'", pnidstr);
745                         rc = LUSTRE_CFG_RC_MISSING_PARAM;
746                         goto out;
747                 }
748
749                 num_nids++;
750
751                 lnet_nidlist2 = calloc(sizeof(*lnet_nidlist2), num_nids);
752                 if (!lnet_nidlist2) {
753                         snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
754                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
755                         goto out;
756                 }
757                 lnet_nidlist2[0] = pnid;
758                 memcpy(&lnet_nidlist2[1], lnet_nidlist, sizeof(*lnet_nidlist) *
759                                                     (num_nids - 1));
760         }
761
762         rc = lustre_lnet_handle_peer_nidlist((pnidstr) ? lnet_nidlist2 :
763                                                          lnet_nidlist,
764                                              num_nids, is_mr,
765                                              IOC_LIBCFS_ADD_PEER_NI, ADD_CMD,
766                                              err_str);
767 out:
768         if (lnet_nidlist2)
769                 free(lnet_nidlist2);
770
771         cYAML_build_error(rc, seq_no, ADD_CMD, "peer_ni", err_str, err_rc);
772         return rc;
773 }
774
775 int lustre_lnet_del_peer_nidlist(char *pnidstr, lnet_nid_t *lnet_nidlist,
776                                  int num_nids, int seq_no,
777                                  struct cYAML **err_rc)
778 {
779         int rc = LUSTRE_CFG_RC_NO_ERR;
780         char err_str[LNET_MAX_STR_LEN];
781         lnet_nid_t *lnet_nidlist2 = NULL;
782         lnet_nid_t pnid;
783
784         if (!pnidstr) {
785                 snprintf(err_str, sizeof(err_str),
786                          "\"Primary nid is not provided\"");
787                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
788                 goto out;
789         }
790
791         pnid = libcfs_str2nid(pnidstr);
792         if (pnid == LNET_NID_ANY) {
793                 rc = LUSTRE_CFG_RC_BAD_PARAM;
794                 snprintf(err_str, sizeof(err_str),
795                          "bad key NID: '%s'",
796                          pnidstr);
797                 goto out;
798         }
799
800         num_nids++;
801         lnet_nidlist2 = calloc(sizeof(*lnet_nidlist2), num_nids);
802         if (!lnet_nidlist2) {
803                 snprintf(err_str, sizeof(err_str),
804                                 "out of memory");
805                 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
806                 goto out;
807         }
808         lnet_nidlist2[0] = pnid;
809         memcpy(&lnet_nidlist2[1], lnet_nidlist, sizeof(*lnet_nidlist) *
810                                                 (num_nids - 1));
811
812         rc = lustre_lnet_handle_peer_nidlist(lnet_nidlist2, num_nids, false,
813                                              IOC_LIBCFS_DEL_PEER_NI, DEL_CMD,
814                                              err_str);
815 out:
816         if (lnet_nidlist2)
817                 free(lnet_nidlist2);
818
819         cYAML_build_error(rc, seq_no, DEL_CMD, "peer_ni", err_str, err_rc);
820         return rc;
821 }
822
823 int lustre_lnet_route_common(char *nw, char *nidstr, int hops, int prio,
824                              int sen, int seq_no, struct cYAML **err_rc,
825                              int cmd)
826 {
827         int rc, num_nids, num_nidstrs, nid_idx, str_idx;
828         __u32 rnet;
829         char err_str[LNET_MAX_STR_LEN];
830         char *nidstrarray[LNET_MAX_STR_LEN];
831         struct lnet_ioctl_config_data data;
832         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
833
834         if (nw == NULL || nidstr == NULL) {
835                 snprintf(err_str, LNET_MAX_STR_LEN,
836                          "\"missing mandatory parameter:'%s'\"",
837                          (nw == NULL && nidstr == NULL) ? "network, gateway" :
838                          (nw == NULL) ? "network" : "gateway");
839                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
840                 goto out;
841         }
842
843         rnet = libcfs_str2net(nw);
844         if (rnet == LNET_NIDNET(LNET_NID_ANY)) {
845                 snprintf(err_str, LNET_MAX_STR_LEN,
846                          "\"cannot parse remote net %s\"", nw);
847                 rc = LUSTRE_CFG_RC_BAD_PARAM;
848                 goto out;
849         }
850
851         rc = tokenize_nidstr(nidstr, &nidstrarray[0], err_str);
852         if (rc < 0)
853                 goto out;
854
855         num_nidstrs = rc;
856
857         for (str_idx = 0; str_idx < num_nidstrs; str_idx++) {
858                 rc = lustre_lnet_parse_nidstr(nidstrarray[str_idx],
859                                               lnet_nidlist,
860                                               LNET_MAX_NIDS_PER_PEER, err_str);
861                 if (rc < 0)
862                         goto out;
863
864                 num_nids = rc;
865
866                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
867                 data.cfg_net = rnet;
868                 if (cmd == LNETCTL_ADD_CMD) {
869                         data.cfg_config_u.cfg_route.rtr_hop = hops;
870                         data.cfg_config_u.cfg_route.rtr_priority = prio;
871                         data.cfg_config_u.cfg_route.rtr_sensitivity = sen;
872                 }
873
874                 for (nid_idx = 0; nid_idx < num_nids; nid_idx++) {
875                         data.cfg_nid = lnet_nidlist[nid_idx];
876
877                         if (cmd == LNETCTL_ADD_CMD)
878                                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE,
879                                              &data);
880                         else
881                                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE,
882                                              &data);
883
884                         if (rc != 0 && errno != EEXIST &&
885                             errno != EHOSTUNREACH) {
886                                 rc = -errno;
887                                 snprintf(err_str, LNET_MAX_STR_LEN,
888                                          "route operation failed: %s",
889                                          strerror(errno));
890                                 goto out;
891                         }
892                 }
893         }
894
895 out:
896         cYAML_build_error(rc, seq_no,
897                           cmd == LNETCTL_ADD_CMD ? ADD_CMD : DEL_CMD, "route",
898                           err_str, err_rc);
899
900         return rc;
901 }
902
903 int lustre_lnet_config_route(char *nw, char *nidstr, int hops, int prio,
904                              int sen, int seq_no, struct cYAML **err_rc)
905 {
906         int rc;
907         char err_str[LNET_MAX_STR_LEN];
908
909         if (hops == -1) {
910                 hops = LNET_UNDEFINED_HOPS;
911         } else if (hops < 1 || hops > 255) {
912                 snprintf(err_str, LNET_MAX_STR_LEN,
913                          "\"invalid hop count %d, must be between 1 and 255\"",
914                          hops);
915                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
916                 goto out;
917         }
918
919         if (prio == -1) {
920                 prio = 0;
921         } else if (prio < 0) {
922                 snprintf(err_str, LNET_MAX_STR_LEN,
923                          "\"invalid priority %d, must be greater than 0\"",
924                          prio);
925                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
926                 goto out;
927         }
928
929         if (sen == -1) {
930                 sen = 1;
931         } else if (sen < 1) {
932                 snprintf(err_str, LNET_MAX_STR_LEN,
933                          "\"invalid health sensitivity %d, must be 1 or greater\"",
934                          sen);
935                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
936                 goto out;
937         }
938
939         rc = lustre_lnet_route_common(nw, nidstr, hops, prio, sen, seq_no,
940                                       err_rc, LNETCTL_ADD_CMD);
941 out:
942         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
943
944         return rc;
945 }
946
947 int lustre_lnet_del_route(char *nw, char *nidstr, int seq_no,
948                           struct cYAML **err_rc)
949 {
950         return lustre_lnet_route_common(nw, nidstr, 0, 0, 0, seq_no, err_rc,
951                                         LNETCTL_DEL_CMD);
952 }
953
954 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
955                            int seq_no, struct cYAML **show_rc,
956                            struct cYAML **err_rc, bool backup)
957 {
958         struct lnet_ioctl_config_data data;
959         lnet_nid_t gateway_nid;
960         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
961         int l_errno = 0;
962         __u32 net = LNET_NIDNET(LNET_NID_ANY);
963         int i;
964         struct cYAML *root = NULL, *route = NULL, *item = NULL;
965         struct cYAML *first_seq = NULL;
966         char err_str[LNET_MAX_STR_LEN];
967         bool exist = false;
968
969         snprintf(err_str, sizeof(err_str),
970                  "\"out of memory\"");
971
972         if (nw != NULL) {
973                 net = libcfs_str2net(nw);
974                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
975                         snprintf(err_str,
976                                  sizeof(err_str),
977                                  "\"cannot parse net '%s'\"", nw);
978                         rc = LUSTRE_CFG_RC_BAD_PARAM;
979                         goto out;
980                 }
981
982         } else {
983                 /* show all routes without filtering on net */
984                 net = LNET_NIDNET(LNET_NID_ANY);
985         }
986
987         if (gw != NULL) {
988                 gateway_nid = libcfs_str2nid(gw);
989                 if (gateway_nid == LNET_NID_ANY) {
990                         snprintf(err_str,
991                                  sizeof(err_str),
992                                  "\"cannot parse gateway NID '%s'\"", gw);
993                         rc = LUSTRE_CFG_RC_BAD_PARAM;
994                         goto out;
995                 }
996         } else
997                 /* show all routes with out filtering on gateway */
998                 gateway_nid = LNET_NID_ANY;
999
1000         if ((hops < 1 && hops != -1) || hops > 255) {
1001                 snprintf(err_str,
1002                          sizeof(err_str),
1003                          "\"invalid hop count %d, must be between 0 and 256\"",
1004                          hops);
1005                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1006                 goto out;
1007         }
1008
1009         /* create struct cYAML root object */
1010         root = cYAML_create_object(NULL, NULL);
1011         if (root == NULL)
1012                 goto out;
1013
1014         route = cYAML_create_seq(root, "route");
1015         if (route == NULL)
1016                 goto out;
1017
1018         for (i = 0;; i++) {
1019                 __u32 rt_alive;
1020                 __u32 rt_multi_hop;
1021
1022                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
1023                 data.cfg_count = i;
1024
1025                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
1026                 if (rc != 0) {
1027                         l_errno = errno;
1028                         break;
1029                 }
1030
1031                 /* filter on provided data */
1032                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
1033                     net != data.cfg_net)
1034                         continue;
1035
1036                 if (gateway_nid != LNET_NID_ANY &&
1037                     gateway_nid != data.cfg_nid)
1038                         continue;
1039
1040                 if (hops != -1 &&
1041                     hops != data.cfg_config_u.cfg_route.rtr_hop)
1042                         continue;
1043
1044                 if (prio != -1 &&
1045                     prio != data.cfg_config_u.cfg_route.rtr_priority)
1046                         continue;
1047
1048                 /* default rc to -1 incase we hit the goto */
1049                 rc = -1;
1050                 exist = true;
1051
1052                 item = cYAML_create_seq_item(route);
1053                 if (item == NULL)
1054                         goto out;
1055
1056                 if (first_seq == NULL)
1057                         first_seq = item;
1058
1059                 if (cYAML_create_string(item, "net",
1060                                         libcfs_net2str(data.cfg_net)) == NULL)
1061                         goto out;
1062
1063                 if (cYAML_create_string(item, "gateway",
1064                                         libcfs_nid2str(data.cfg_nid)) == NULL)
1065                         goto out;
1066
1067                 if (detail) {
1068                         if (cYAML_create_number(item, "hop",
1069                                                 (int) data.cfg_config_u.
1070                                                 cfg_route.rtr_hop) ==
1071                             NULL)
1072                                 goto out;
1073
1074                         if (cYAML_create_number(item, "priority",
1075                                                 data.cfg_config_u.
1076                                                 cfg_route.rtr_priority) == NULL)
1077                                 goto out;
1078
1079                         if (cYAML_create_number(item, "health_sensitivity",
1080                                                 data.cfg_config_u.
1081                                                 cfg_route.rtr_sensitivity) == NULL)
1082                                 goto out;
1083
1084                         rt_alive = data.cfg_config_u.cfg_route.rtr_flags &
1085                                         LNET_RT_ALIVE;
1086                         rt_multi_hop = data.cfg_config_u.cfg_route.rtr_flags &
1087                                         LNET_RT_MULTI_HOP;
1088
1089                         if (!backup &&
1090                             cYAML_create_string(item, "state",
1091                                                 rt_alive ?
1092                                                 "up" : "down") == NULL)
1093                                 goto out;
1094
1095                         if (!backup &&
1096                             cYAML_create_string(item, "type",
1097                                                 rt_multi_hop?
1098                                                 "multi-hop" : "single-hop") == NULL)
1099                                 goto out;
1100                 }
1101         }
1102
1103         /* print output iff show_rc is not provided */
1104         if (show_rc == NULL)
1105                 cYAML_print_tree(root);
1106
1107         if (l_errno != ENOENT) {
1108                 snprintf(err_str,
1109                          sizeof(err_str),
1110                          "\"cannot get routes: %s\"",
1111                          strerror(l_errno));
1112                 rc = -l_errno;
1113                 goto out;
1114         } else
1115                 rc = LUSTRE_CFG_RC_NO_ERR;
1116
1117         snprintf(err_str, sizeof(err_str), "\"success\"");
1118 out:
1119         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1120                 cYAML_free_tree(root);
1121         } else if (show_rc != NULL && *show_rc != NULL) {
1122                 struct cYAML *show_node;
1123                 /* find the route node, if one doesn't exist then
1124                  * insert one.  Otherwise add to the one there
1125                  */
1126                 show_node = cYAML_get_object_item(*show_rc, "route");
1127                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1128                         cYAML_insert_child(show_node, first_seq);
1129                         free(route);
1130                         free(root);
1131                 } else if (show_node == NULL) {
1132                         cYAML_insert_sibling((*show_rc)->cy_child,
1133                                                 route);
1134                         free(root);
1135                 } else {
1136                         cYAML_free_tree(root);
1137                 }
1138         } else {
1139                 *show_rc = root;
1140         }
1141
1142         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1143
1144         return rc;
1145 }
1146
1147 static int socket_intf_query(int request, char *intf,
1148                              struct ifreq *ifr)
1149 {
1150         int rc = 0;
1151         int sockfd;
1152
1153         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1154                 return LUSTRE_CFG_RC_BAD_PARAM;
1155
1156         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1157         if (sockfd < 0)
1158                 return LUSTRE_CFG_RC_BAD_PARAM;
1159
1160         strcpy(ifr->ifr_name, intf);
1161         rc = ioctl(sockfd, request, ifr);
1162         if (rc != 0)
1163                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1164
1165         close(sockfd);
1166
1167         return rc;
1168 }
1169
1170 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1171 {
1172         struct ifreq ifr;
1173         int rc;
1174
1175         memset(&ifr, 0, sizeof(ifr));
1176         rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1177         if (rc != 0)
1178                 return LUSTRE_CFG_RC_BAD_PARAM;
1179
1180         if ((ifr.ifr_flags & IFF_UP) == 0)
1181                 return LUSTRE_CFG_RC_BAD_PARAM;
1182
1183         memset(&ifr, 0, sizeof(ifr));
1184         rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1185         if (rc != 0)
1186                 return LUSTRE_CFG_RC_BAD_PARAM;
1187
1188         *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1189         *ip = bswap_32(*ip);
1190
1191         return LUSTRE_CFG_RC_NO_ERR;
1192 }
1193
1194 /*
1195  * for each interface in the array of interfaces find the IP address of
1196  * that interface, create its nid and add it to an array of NIDs.
1197  * Stop if any of the interfaces is down
1198  */
1199 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1200                                  lnet_nid_t **nids, __u32 *nnids,
1201                                  char *err_str, size_t str_len)
1202 {
1203         int i = 0, count = 0, rc;
1204         struct lnet_dlc_intf_descr *intf;
1205         char val[LNET_MAX_STR_LEN];
1206         __u32 ip;
1207         int gni_num;
1208         char *endp;
1209         unsigned int num;
1210
1211
1212         if (nw == NULL || nids == NULL) {
1213                 snprintf(err_str, str_len,
1214                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1215                 err_str[str_len - 1] = '\0';
1216                 return LUSTRE_CFG_RC_BAD_PARAM;
1217         }
1218
1219         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1220                 count = 1;
1221         } else {
1222                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1223                         count++;
1224         }
1225
1226         *nids = calloc(count, sizeof(lnet_nid_t));
1227         if (*nids == NULL) {
1228                 snprintf(err_str, str_len,
1229                          "\"out of memory\"");
1230                 err_str[str_len - 1] = '\0';
1231                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1232         }
1233         /*
1234          * special case the GNI interface since it doesn't have an IP
1235          * address. The assumption is that there can only be one GNI
1236          * interface in the system. No interface name is provided.
1237          */
1238         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1239                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1240                                 1, sizeof(val));
1241                 if (rc) {
1242                         snprintf(err_str, str_len,
1243                                  "\"cannot read gni nid\"");
1244                         err_str[str_len - 1] = '\0';
1245                         goto failed;
1246                 }
1247                 gni_num = atoi(val);
1248
1249                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1250
1251                 goto out;
1252         }
1253
1254         /* look at the other interfaces */
1255         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1256                 if (LNET_NETTYP(nw->nw_id) == PTL4LND) {
1257                         /* handle LNDs with numeric interface name */
1258                         num = strtoul(intf->intf_name, &endp, 0);
1259                         if (endp == intf->intf_name || *endp != '\0') {
1260                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1261                                 snprintf(err_str, str_len,
1262                                          "\"couldn't query intf %s\"",
1263                                          intf->intf_name);
1264                                 err_str[str_len - 1] = '\0';
1265                                 goto failed;
1266                         }
1267                         (*nids)[i] = LNET_MKNID(nw->nw_id, num);
1268                         i++;
1269                 } else {
1270                         /* handle LNDs with ip interface name */
1271                         rc = lustre_lnet_queryip(intf, &ip);
1272                         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1273                                 snprintf(err_str, str_len,
1274                                          "\"couldn't query intf %s\"",
1275                                          intf->intf_name);
1276                                 err_str[str_len - 1] = '\0';
1277                                 goto failed;
1278                         }
1279                         (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1280                         i++;
1281                 }
1282         }
1283
1284 out:
1285         *nnids = count;
1286
1287         return 0;
1288
1289 failed:
1290         free(*nids);
1291         *nids = NULL;
1292         return rc;
1293 }
1294
1295 /*
1296  * called repeatedly until a match or no more ip range
1297  * What do you have?
1298  *      ip_range expression
1299  *      interface list with all the interface names.
1300  *      all the interfaces in the system.
1301  *
1302  *      try to match the ip_range expr to one of the interfaces' IPs in
1303  *      the system. If we hit a patch for an interface. Check if that
1304  *      interface name is in the list.
1305  *
1306  *      If there are more than one interface in the list, then make sure
1307  *      that the IPs for all of these interfaces match the ip ranges
1308  *      given.
1309  *
1310  *      for each interface in intf_list
1311  *              look up the intf name in ifa
1312  *              if not there then no match
1313  *              check ip obtained from ifa against a match to any of the
1314  *              ip_ranges given.
1315  *              If no match, then fail
1316  *
1317  *      The result is that all the interfaces have to match.
1318  */
1319 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1320                                  struct list_head *intf_list,
1321                                  struct list_head *ip_ranges)
1322 {
1323         int rc;
1324         __u32 ip;
1325         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1326         struct ifaddrs *ifaddr = ifa;
1327         struct lustre_lnet_ip_range_descr *ip_range;
1328         int family;
1329
1330         /*
1331          * if there are no explicit interfaces, and no ip ranges, then
1332          * configure the first tcp interface we encounter.
1333          */
1334         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1335                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1336                         if (ifaddr->ifa_addr == NULL)
1337                                 continue;
1338
1339                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1340                                 continue;
1341
1342                         family = ifaddr->ifa_addr->sa_family;
1343                         if (family == AF_INET &&
1344                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1345                                 rc = lustre_lnet_add_intf_descr
1346                                         (intf_list, ifaddr->ifa_name,
1347                                         strlen(ifaddr->ifa_name));
1348
1349                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1350                                         return rc;
1351
1352                                 return LUSTRE_CFG_RC_MATCH;
1353                         }
1354                 }
1355                 return LUSTRE_CFG_RC_NO_MATCH;
1356         }
1357
1358         /*
1359          * First interface which matches an IP pattern will be used
1360          */
1361         if (list_empty(intf_list)) {
1362                 /*
1363                  * no interfaces provided in the rule, but an ip range is
1364                  * provided, so try and match an interface to the ip
1365                  * range.
1366                  */
1367                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1368                         if (ifaddr->ifa_addr == NULL)
1369                                 continue;
1370
1371                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1372                                 continue;
1373
1374                         family = ifaddr->ifa_addr->sa_family;
1375                         if (family == AF_INET) {
1376                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1377                                         sin_addr.s_addr;
1378
1379                                 list_for_each_entry(ip_range, ip_ranges,
1380                                                     ipr_entry) {
1381                                         rc = cfs_ip_addr_match(bswap_32(ip),
1382                                                         &ip_range->ipr_expr);
1383                                         if (!rc)
1384                                                 continue;
1385
1386                                         rc = lustre_lnet_add_intf_descr
1387                                           (intf_list, ifaddr->ifa_name,
1388                                            strlen(ifaddr->ifa_name));
1389
1390                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1391                                                 return rc;
1392                                 }
1393                         }
1394                 }
1395
1396                 if (!list_empty(intf_list))
1397                         return LUSTRE_CFG_RC_MATCH;
1398
1399                 return LUSTRE_CFG_RC_NO_MATCH;
1400         }
1401
1402         /*
1403          * If an interface is explicitly specified the ip-range might or
1404          * might not be specified. if specified the interface needs to match the
1405          * ip-range. If no ip-range then the interfaces are
1406          * automatically matched if they are all up.
1407          * If > 1 interfaces all the interfaces must match for the NI to
1408          * be configured.
1409          */
1410         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1411                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1412                         if (ifaddr->ifa_addr == NULL)
1413                                 continue;
1414
1415                         family = ifaddr->ifa_addr->sa_family;
1416                         if (family == AF_INET &&
1417                             strcmp(intf_descr->intf_name,
1418                                    ifaddr->ifa_name) == 0)
1419                                 break;
1420                 }
1421
1422                 if (ifaddr == NULL) {
1423                         list_del(&intf_descr->intf_on_network);
1424                         free_intf_descr(intf_descr);
1425                         continue;
1426                 }
1427
1428                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1429                         list_del(&intf_descr->intf_on_network);
1430                         free_intf_descr(intf_descr);
1431                         continue;
1432                 }
1433
1434                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1435
1436                 rc = 1;
1437                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1438                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1439                         if (rc)
1440                                 break;
1441                 }
1442
1443                 if (!rc) {
1444                         /* no match for this interface */
1445                         list_del(&intf_descr->intf_on_network);
1446                         free_intf_descr(intf_descr);
1447                 }
1448         }
1449
1450         return LUSTRE_CFG_RC_MATCH;
1451 }
1452
1453 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1454                                             lnet_nid_t **nids, __u32 *nnids,
1455                                             char *err_str, size_t str_len)
1456 {
1457         struct ifaddrs *ifa;
1458         int rc = LUSTRE_CFG_RC_NO_ERR;
1459
1460         rc = getifaddrs(&ifa);
1461         if (rc < 0) {
1462                 snprintf(err_str, str_len,
1463                          "\"failed to get interface addresses: %d\"", -errno);
1464                 err_str[str_len - 1] = '\0';
1465                 return -errno;
1466         }
1467
1468         rc = lustre_lnet_match_ip_to_intf(ifa,
1469                                           &ip2nets->ip2nets_net.nw_intflist,
1470                                           &ip2nets->ip2nets_ip_ranges);
1471         if (rc != LUSTRE_CFG_RC_MATCH) {
1472                 snprintf(err_str, str_len,
1473                          "\"couldn't match ip to existing interfaces\"");
1474                 err_str[str_len - 1] = '\0';
1475                 freeifaddrs(ifa);
1476                 return rc;
1477         }
1478
1479         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1480                                    err_str, sizeof(err_str));
1481         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1482                 *nids = NULL;
1483                 *nnids = 0;
1484         }
1485
1486         freeifaddrs(ifa);
1487
1488         return rc;
1489 }
1490
1491 static int
1492 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1493                             struct lnet_ioctl_config_lnd_tunables *tunables,
1494                             struct cfs_expr_list *global_cpts,
1495                             lnet_nid_t *nids, char *err_str)
1496 {
1497         char *data;
1498         struct lnet_ioctl_config_ni *conf;
1499         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1500         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1501         size_t len;
1502         int count;
1503         struct lnet_dlc_intf_descr *intf_descr;
1504         __u32 *cpt_array;
1505         struct cfs_expr_list *cpt_expr;
1506
1507         list_for_each_entry(intf_descr, intf_list,
1508                             intf_on_network) {
1509                 if (tunables != NULL)
1510                         len = sizeof(struct lnet_ioctl_config_ni) +
1511                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1512                 else
1513                         len = sizeof(struct lnet_ioctl_config_ni);
1514
1515                 data = calloc(1, len);
1516                 if (!data)
1517                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1518                 conf = (struct lnet_ioctl_config_ni*) data;
1519                 if (tunables != NULL)
1520                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1521                                 conf->lic_bulk;
1522
1523                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1524                 conf->lic_cfg_hdr.ioc_len = len;
1525                 conf->lic_nid = nids[i];
1526                 strncpy(conf->lic_ni_intf[0], intf_descr->intf_name,
1527                         LNET_MAX_STR_LEN);
1528
1529                 if (intf_descr->cpt_expr != NULL)
1530                         cpt_expr = intf_descr->cpt_expr;
1531                 else if (global_cpts != NULL)
1532                         cpt_expr = global_cpts;
1533                 else
1534                         cpt_expr = NULL;
1535
1536                 if (cpt_expr != NULL) {
1537                         count = cfs_expr_list_values(cpt_expr,
1538                                                      LNET_MAX_SHOW_NUM_CPT,
1539                                                      &cpt_array);
1540                         if (count > 0) {
1541                                 memcpy(conf->lic_cpts, cpt_array,
1542                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1543                                 free(cpt_array);
1544                         } else {
1545                                 count = 0;
1546                         }
1547                 } else {
1548                         count = 0;
1549                 }
1550
1551                 conf->lic_ncpts = count;
1552
1553                 if (tunables != NULL)
1554                         memcpy(tun, tunables, sizeof(*tunables));
1555
1556                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1557                 if (rc < 0) {
1558                         rc = -errno;
1559                         snprintf(err_str,
1560                                  LNET_MAX_STR_LEN,
1561                                  "\"cannot add network: %s\"", strerror(errno));
1562                         free(data);
1563                         return rc;
1564                 }
1565                 free(data);
1566                 i++;
1567         }
1568
1569         return LUSTRE_CFG_RC_NO_ERR;
1570 }
1571
1572 int
1573 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1574                            struct lnet_ioctl_config_lnd_tunables *tunables,
1575                            struct cfs_expr_list *global_cpts,
1576                            int seq_no, struct cYAML **err_rc)
1577 {
1578         lnet_nid_t *nids = NULL;
1579         __u32 nnids = 0;
1580         int rc;
1581         char err_str[LNET_MAX_STR_LEN];
1582
1583         snprintf(err_str, sizeof(err_str), "\"success\"");
1584
1585         if (!ip2nets) {
1586                 snprintf(err_str,
1587                          sizeof(err_str),
1588                          "\"incomplete ip2nets information\"");
1589                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1590                 goto out;
1591         }
1592
1593         /*
1594          * call below function to resolve the rules into a list of nids.
1595          * The memory is allocated in that function then freed here when
1596          * it's no longer needed.
1597          */
1598         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1599                                               sizeof(err_str));
1600         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1601                 goto out;
1602
1603         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1604                 snprintf(err_str, sizeof(err_str),
1605                          "\"no interfaces match ip2nets rules\"");
1606                 goto free_nids_out;
1607         }
1608
1609         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1610                                          tunables, global_cpts, nids,
1611                                          err_str);
1612
1613 free_nids_out:
1614         free(nids);
1615
1616 out:
1617         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1618         return rc;
1619 }
1620
1621 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1622                           struct cfs_expr_list *global_cpts,
1623                           char *ip2net,
1624                           struct lnet_ioctl_config_lnd_tunables *tunables,
1625                           int seq_no, struct cYAML **err_rc)
1626 {
1627         char *data = NULL;
1628         struct lnet_ioctl_config_ni *conf;
1629         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1630         char buf[LNET_MAX_STR_LEN];
1631         int rc = LUSTRE_CFG_RC_NO_ERR;
1632         char err_str[LNET_MAX_STR_LEN];
1633         lnet_nid_t *nids = NULL;
1634         __u32 nnids = 0;
1635         size_t len;
1636         int count;
1637         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1638         __u32 *cpt_array;
1639
1640         snprintf(err_str, sizeof(err_str), "\"success\"");
1641
1642         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1643             (list_empty(&nw_descr->nw_intflist) &&
1644              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1645                 snprintf(err_str,
1646                          sizeof(err_str),
1647                          "\"missing mandatory parameters in NI config: '%s'\"",
1648                          (nw_descr == NULL) ? "network , interface" :
1649                          (nw_descr->nw_id == 0) ? "network" : "interface");
1650                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1651                 goto out;
1652         }
1653
1654         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1655                 snprintf(err_str,
1656                          sizeof(err_str),
1657                          "\"ip2net string too long %d\"",
1658                                 (int)strlen(ip2net));
1659                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1660                 goto out;
1661         }
1662
1663         if (ip2net != NULL) {
1664                 if (tunables != NULL)
1665                         len = sizeof(struct lnet_ioctl_config_ni) +
1666                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1667                 else
1668                         len = sizeof(struct lnet_ioctl_config_ni);
1669                 data = calloc(1, len);
1670                 if (!data) {
1671                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1672                         goto out;
1673                 }
1674                 conf = (struct lnet_ioctl_config_ni*) data;
1675                 if (tunables != NULL)
1676                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1677                                 (data + sizeof(*conf));
1678
1679                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1680                 conf->lic_cfg_hdr.ioc_len = len;
1681                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1682                         LNET_MAX_STR_LEN);
1683
1684                 if (global_cpts != NULL) {
1685                         count = cfs_expr_list_values(global_cpts,
1686                                                      LNET_MAX_SHOW_NUM_CPT,
1687                                                      &cpt_array);
1688                         if (count > 0) {
1689                                 memcpy(conf->lic_cpts, cpt_array,
1690                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1691                                 free(cpt_array);
1692                         } else {
1693                                 count = 0;
1694                         }
1695                 } else {
1696                         count = 0;
1697                 }
1698
1699                 conf->lic_ncpts = count;
1700
1701                 if (tunables != NULL)
1702                         memcpy(tun, tunables, sizeof(*tunables));
1703
1704                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1705                 if (rc < 0) {
1706                         rc = -errno;
1707                         snprintf(err_str,
1708                                 sizeof(err_str),
1709                                 "\"cannot add network: %s\"", strerror(errno));
1710                         goto out;
1711                 }
1712
1713                 goto out;
1714         }
1715
1716         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1717                 rc = LUSTRE_CFG_RC_NO_ERR;
1718                 goto out;
1719         }
1720
1721         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1722                 snprintf(err_str,
1723                         sizeof(err_str),
1724                         "\"cannot parse net '%s'\"",
1725                         libcfs_net2str(nw_descr->nw_id));
1726                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1727                 goto out;
1728         }
1729
1730         /*
1731          * special case the GNI since no interface name is expected
1732          */
1733         if (list_empty(&nw_descr->nw_intflist) &&
1734             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1735                 snprintf(err_str,
1736                         sizeof(err_str),
1737                         "\"no interface name provided\"");
1738                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1739                 goto out;
1740         }
1741
1742         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1743                                    err_str, sizeof(err_str));
1744         if (rc != 0) {
1745                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1746                 goto out;
1747         }
1748
1749         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1750                                          tunables, global_cpts, nids,
1751                                          err_str);
1752
1753 out:
1754         if (nw_descr != NULL) {
1755                 list_for_each_entry_safe(intf_descr, tmp,
1756                                          &nw_descr->nw_intflist,
1757                                          intf_on_network) {
1758                         list_del(&intf_descr->intf_on_network);
1759                         free_intf_descr(intf_descr);
1760                 }
1761         }
1762
1763         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1764
1765         if (nids)
1766                 free(nids);
1767
1768         if (data)
1769                 free(data);
1770
1771         return rc;
1772 }
1773
1774 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1775                        int seq_no, struct cYAML **err_rc)
1776 {
1777         struct lnet_ioctl_config_ni data;
1778         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1779         char err_str[LNET_MAX_STR_LEN];
1780         lnet_nid_t *nids = NULL;
1781         __u32 nnids = 0;
1782         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1783
1784         snprintf(err_str, sizeof(err_str), "\"success\"");
1785
1786         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1787                 snprintf(err_str,
1788                          sizeof(err_str),
1789                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1790                          (nw_descr == NULL) ? "network , interface" :
1791                          (nw_descr->nw_id == 0) ? "network" : "interface");
1792                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1793                 goto out;
1794         }
1795
1796         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1797                 return LUSTRE_CFG_RC_NO_ERR;
1798
1799         if (nw_descr->nw_id == LNET_NIDNET(LNET_NID_ANY)) {
1800                 snprintf(err_str,
1801                          sizeof(err_str),
1802                          "\"cannot parse net '%s'\"",
1803                          libcfs_net2str(nw_descr->nw_id));
1804                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1805                 goto out;
1806         }
1807
1808         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1809                                    err_str, sizeof(err_str));
1810         if (rc != 0) {
1811                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1812                 goto out;
1813         }
1814
1815         /*
1816          * no interfaces just the nw_id is specified
1817          */
1818         if (nnids == 0) {
1819                 nids = calloc(1, sizeof(*nids));
1820                 if (nids == NULL) {
1821                         snprintf(err_str, sizeof(err_str),
1822                                 "\"out of memory\"");
1823                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1824                         goto out;
1825                 }
1826                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1827                 nnids = 1;
1828         }
1829
1830         for (i = 0; i < nnids; i++) {
1831                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1832                 data.lic_nid = nids[i];
1833
1834                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1835                 if (rc < 0) {
1836                         rc = -errno;
1837                         snprintf(err_str,
1838                                 sizeof(err_str),
1839                                 "\"cannot del network: %s\"", strerror(errno));
1840                 }
1841         }
1842
1843         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1844                                  intf_on_network) {
1845                 list_del(&intf_descr->intf_on_network);
1846                 free_intf_descr(intf_descr);
1847         }
1848
1849 out:
1850         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1851
1852         if (nids != NULL)
1853                 free(nids);
1854
1855         return rc;
1856 }
1857
1858 static int
1859 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
1860                            enum lnet_health_type type, char *name,
1861                            int seq_no, struct cYAML **err_rc)
1862 {
1863         struct lnet_ioctl_reset_health_cfg data;
1864         int rc = LUSTRE_CFG_RC_NO_ERR;
1865         char err_str[LNET_MAX_STR_LEN];
1866
1867         snprintf(err_str, sizeof(err_str), "\"success\"");
1868
1869         LIBCFS_IOC_INIT_V2(data, rh_hdr);
1870         data.rh_type = type;
1871         data.rh_all = all;
1872         data.rh_value = value;
1873         data.rh_nid = nid;
1874
1875         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
1876         if (rc != 0) {
1877                 rc = -errno;
1878                 snprintf(err_str,
1879                          sizeof(err_str), "Can not configure health value: %s",
1880                          strerror(errno));
1881         }
1882
1883         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
1884
1885         return rc;
1886 }
1887
1888 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
1889                                   struct cYAML **err_rc)
1890 {
1891         lnet_nid_t nid;
1892         if (ni_nid)
1893                 nid = libcfs_str2nid(ni_nid);
1894         else
1895                 nid = LNET_NID_ANY;
1896         return lustre_lnet_config_healthv(value, all, nid,
1897                                           LNET_HEALTH_TYPE_LOCAL_NI,
1898                                           "ni healthv", seq_no, err_rc);
1899 }
1900
1901 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
1902                                        int seq_no, struct cYAML **err_rc)
1903 {
1904         lnet_nid_t nid;
1905         if (lpni_nid)
1906                 nid = libcfs_str2nid(lpni_nid);
1907         else
1908                 nid = LNET_NID_ANY;
1909         return lustre_lnet_config_healthv(value, all, nid,
1910                                           LNET_HEALTH_TYPE_PEER_NI,
1911                                           "peer_ni healthv", seq_no, err_rc);
1912 }
1913
1914 static bool
1915 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1916                           struct lnet_ioctl_comm_count *counts)
1917 {
1918         if (cYAML_create_number(yaml, "put",
1919                                 counts->ico_put_count)
1920                                         == NULL)
1921                 return false;
1922         if (cYAML_create_number(yaml, "get",
1923                                 counts->ico_get_count)
1924                                         == NULL)
1925                 return false;
1926         if (cYAML_create_number(yaml, "reply",
1927                                 counts->ico_reply_count)
1928                                         == NULL)
1929                 return false;
1930         if (cYAML_create_number(yaml, "ack",
1931                                 counts->ico_ack_count)
1932                                         == NULL)
1933                 return false;
1934         if (cYAML_create_number(yaml, "hello",
1935                                 counts->ico_hello_count)
1936                                         == NULL)
1937                 return false;
1938
1939         return true;
1940 }
1941
1942 static struct lnet_ioctl_comm_count *
1943 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1944 {
1945         if (idx == 0)
1946                 return &msg_stats->im_send_stats;
1947         if (idx == 1)
1948                 return &msg_stats->im_recv_stats;
1949         if (idx == 2)
1950                 return &msg_stats->im_drop_stats;
1951
1952         return NULL;
1953 }
1954
1955 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
1956                          struct cYAML **show_rc, struct cYAML **err_rc,
1957                          bool backup)
1958 {
1959         char *buf;
1960         struct lnet_ioctl_config_ni *ni_data;
1961         struct lnet_ioctl_config_lnd_tunables *lnd;
1962         struct lnet_ioctl_element_stats *stats;
1963         struct lnet_ioctl_element_msg_stats msg_stats;
1964         struct lnet_ioctl_local_ni_hstats hstats;
1965         __u32 net = LNET_NIDNET(LNET_NID_ANY);
1966         __u32 prev_net = LNET_NIDNET(LNET_NID_ANY);
1967         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
1968         int l_errno = 0;
1969         struct cYAML *root = NULL, *tunables = NULL,
1970                 *net_node = NULL, *interfaces = NULL,
1971                 *item = NULL, *first_seq = NULL,
1972                 *tmp = NULL, *statistics = NULL,
1973                 *yhstats = NULL;
1974         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
1975         char str_buf[str_buf_len];
1976         char *pos;
1977         char err_str[LNET_MAX_STR_LEN];
1978         bool exist = false, new_net = true;
1979         int net_num = 0;
1980         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
1981
1982         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
1983
1984         buf = calloc(1, buf_size);
1985         if (buf == NULL)
1986                 goto out;
1987
1988         ni_data = (struct lnet_ioctl_config_ni *)buf;
1989
1990         if (nw != NULL) {
1991                 net = libcfs_str2net(nw);
1992                 if (net == LNET_NIDNET(LNET_NID_ANY)) {
1993                         snprintf(err_str,
1994                                  sizeof(err_str),
1995                                  "\"cannot parse net '%s'\"", nw);
1996                         rc = LUSTRE_CFG_RC_BAD_PARAM;
1997                         goto out;
1998                 }
1999         }
2000
2001         root = cYAML_create_object(NULL, NULL);
2002         if (root == NULL)
2003                 goto out;
2004
2005         net_node = cYAML_create_seq(root, "net");
2006         if (net_node == NULL)
2007                 goto out;
2008
2009         for (i = 0;; i++) {
2010                 __u32 rc_net;
2011
2012                 memset(buf, 0, buf_size);
2013
2014                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
2015                 /*
2016                  * set the ioc_len to the proper value since INIT assumes
2017                  * size of data
2018                  */
2019                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
2020                 ni_data->lic_idx = i;
2021
2022                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
2023                 if (rc != 0) {
2024                         l_errno = errno;
2025                         break;
2026                 }
2027
2028                 rc_net = LNET_NIDNET(ni_data->lic_nid);
2029
2030                 /* filter on provided data */
2031                 if (net != LNET_NIDNET(LNET_NID_ANY) &&
2032                     net != rc_net)
2033                         continue;
2034
2035                 /* if we're backing up don't store lo */
2036                 if (backup && LNET_NETTYP(rc_net) == LOLND)
2037                         continue;
2038
2039                 /* default rc to -1 in case we hit the goto */
2040                 rc = -1;
2041                 exist = true;
2042
2043                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
2044                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
2045                         (ni_data->lic_bulk + sizeof(*stats));
2046
2047                 if (rc_net != prev_net) {
2048                         prev_net = rc_net;
2049                         new_net = true;
2050                         net_num++;
2051                 }
2052
2053                 if (new_net) {
2054                         if (!cYAML_create_string(net_node, "net type",
2055                                                  libcfs_net2str(rc_net)))
2056                                 goto out;
2057
2058                         tmp = cYAML_create_seq(net_node, "local NI(s)");
2059                         if (tmp == NULL)
2060                                 goto out;
2061                         new_net = false;
2062                 }
2063
2064                 /* create the tree to be printed. */
2065                 item = cYAML_create_seq_item(tmp);
2066                 if (item == NULL)
2067                         goto out;
2068
2069                 if (first_seq == NULL)
2070                         first_seq = item;
2071
2072                 if (!backup &&
2073                     cYAML_create_string(item, "nid",
2074                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2075                         goto out;
2076
2077                 if (!backup &&
2078                     cYAML_create_string(item,
2079                                         "status",
2080                                         (ni_data->lic_status ==
2081                                           LNET_NI_STATUS_UP) ?
2082                                             "up" : "down") == NULL)
2083                         goto out;
2084
2085                 /* don't add interfaces unless there is at least one
2086                  * interface */
2087                 if (strlen(ni_data->lic_ni_intf[0]) > 0) {
2088                         interfaces = cYAML_create_object(item, "interfaces");
2089                         if (interfaces == NULL)
2090                                 goto out;
2091
2092                         for (j = 0; j < LNET_INTERFACES_NUM; j++) {
2093                                 if (strlen(ni_data->lic_ni_intf[j]) > 0) {
2094                                         snprintf(str_buf,
2095                                                  sizeof(str_buf), "%d", j);
2096                                         if (cYAML_create_string(interfaces,
2097                                                 str_buf,
2098                                                 ni_data->lic_ni_intf[j]) ==
2099                                                         NULL)
2100                                                 goto out;
2101                                 }
2102                         }
2103                 }
2104
2105                 if (detail) {
2106                         char *limit;
2107                         int k;
2108
2109                         if (backup)
2110                                 goto continue_without_msg_stats;
2111
2112                         statistics = cYAML_create_object(item, "statistics");
2113                         if (statistics == NULL)
2114                                 goto out;
2115
2116                         if (cYAML_create_number(statistics, "send_count",
2117                                                 stats->iel_send_count)
2118                                                         == NULL)
2119                                 goto out;
2120
2121                         if (cYAML_create_number(statistics, "recv_count",
2122                                                 stats->iel_recv_count)
2123                                                         == NULL)
2124                                 goto out;
2125
2126                         if (cYAML_create_number(statistics, "drop_count",
2127                                                 stats->iel_drop_count)
2128                                                         == NULL)
2129                                 goto out;
2130
2131                         if (detail < 2)
2132                                 goto continue_without_msg_stats;
2133
2134                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2135                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2136                         msg_stats.im_idx = i;
2137
2138                         rc = l_ioctl(LNET_DEV_ID,
2139                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2140                                      &msg_stats);
2141                         if (rc != 0) {
2142                                 l_errno = errno;
2143                                 goto continue_without_msg_stats;
2144                         }
2145
2146                         for (k = 0; k < 3; k++) {
2147                                 struct lnet_ioctl_comm_count *counts;
2148                                 struct cYAML *msg_statistics = NULL;
2149
2150                                 msg_statistics = cYAML_create_object(item,
2151                                                  (char *)gmsg_stat_names[k]);
2152                                 if (msg_statistics == NULL)
2153                                         goto out;
2154
2155                                 counts = get_counts(&msg_stats, k);
2156                                 if (counts == NULL)
2157                                         goto out;
2158
2159                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2160                                                                counts))
2161                                         goto out;
2162                         }
2163
2164                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2165                         hstats.hlni_nid = ni_data->lic_nid;
2166                         /* grab health stats */
2167                         rc = l_ioctl(LNET_DEV_ID,
2168                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2169                                      &hstats);
2170                         if (rc != 0) {
2171                                 l_errno = errno;
2172                                 goto continue_without_msg_stats;
2173                         }
2174                         yhstats = cYAML_create_object(item, "health stats");
2175                         if (!yhstats)
2176                                 goto out;
2177                         if (cYAML_create_number(yhstats, "health value",
2178                                                 hstats.hlni_health_value)
2179                                                         == NULL)
2180                                 goto out;
2181                         if (cYAML_create_number(yhstats, "interrupts",
2182                                                 hstats.hlni_local_interrupt)
2183                                                         == NULL)
2184                                 goto out;
2185                         if (cYAML_create_number(yhstats, "dropped",
2186                                                 hstats.hlni_local_dropped)
2187                                                         == NULL)
2188                                 goto out;
2189                         if (cYAML_create_number(yhstats, "aborted",
2190                                                 hstats.hlni_local_aborted)
2191                                                         == NULL)
2192                                 goto out;
2193                         if (cYAML_create_number(yhstats, "no route",
2194                                                 hstats.hlni_local_no_route)
2195                                                         == NULL)
2196                                 goto out;
2197                         if (cYAML_create_number(yhstats, "timeouts",
2198                                                 hstats.hlni_local_timeout)
2199                                                         == NULL)
2200                                 goto out;
2201                         if (cYAML_create_number(yhstats, "error",
2202                                                 hstats.hlni_local_error)
2203                                                         == NULL)
2204                                 goto out;
2205
2206 continue_without_msg_stats:
2207                         tunables = cYAML_create_object(item, "tunables");
2208                         if (!tunables)
2209                                 goto out;
2210
2211                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2212                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2213                                 goto out;
2214
2215                         rc = lustre_ni_show_tunables(tunables, LNET_NETTYP(rc_net),
2216                                                      &lnd->lt_tun);
2217                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2218                             rc != LUSTRE_CFG_RC_NO_MATCH)
2219                                 goto out;
2220
2221                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2222                                 tunables = cYAML_create_object(item,
2223                                                                "lnd tunables");
2224                                 if (tunables == NULL)
2225                                         goto out;
2226                         }
2227
2228                         if (!backup &&
2229                             cYAML_create_number(item, "dev cpt",
2230                                                 ni_data->lic_dev_cpt) == NULL)
2231                                 goto out;
2232
2233                         if (!backup &&
2234                             cYAML_create_number(item, "tcp bonding",
2235                                                 ni_data->lic_tcp_bonding)
2236                                                         == NULL)
2237                                 goto out;
2238
2239                         /* out put the CPTs in the format: "[x,x,x,...]" */
2240                         pos = str_buf;
2241                         limit = str_buf + str_buf_len - 3;
2242                         pos += scnprintf(pos, limit - pos, "\"[");
2243                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2244                                 j < ni_data->lic_ncpts &&
2245                                 pos < limit; j++) {
2246                                 pos += scnprintf(pos, limit - pos,
2247                                                  "%d", ni_data->lic_cpts[j]);
2248                                 if ((j + 1) < ni_data->lic_ncpts)
2249                                         pos += scnprintf(pos, limit - pos, ",");
2250                         }
2251                         snprintf(pos, 3, "]\"");
2252
2253                         if (ni_data->lic_ncpts >= 1 &&
2254                             cYAML_create_string(item, "CPT",
2255                                                 str_buf) == NULL)
2256                                 goto out;
2257                 }
2258         }
2259
2260         /* Print out the net information only if show_rc is not provided */
2261         if (show_rc == NULL)
2262                 cYAML_print_tree(root);
2263
2264         if (l_errno != ENOENT) {
2265                 snprintf(err_str,
2266                          sizeof(err_str),
2267                          "\"cannot get networks: %s\"",
2268                          strerror(l_errno));
2269                 rc = -l_errno;
2270                 goto out;
2271         } else
2272                 rc = LUSTRE_CFG_RC_NO_ERR;
2273
2274         snprintf(err_str, sizeof(err_str), "\"success\"");
2275 out:
2276         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2277                 cYAML_free_tree(root);
2278         } else if (show_rc != NULL && *show_rc != NULL) {
2279                 struct cYAML *show_node;
2280                 /* find the net node, if one doesn't exist
2281                  * then insert one.  Otherwise add to the one there
2282                  */
2283                 show_node = cYAML_get_object_item(*show_rc, "net");
2284                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2285                         cYAML_insert_child(show_node, first_seq);
2286                         free(net_node);
2287                         free(root);
2288                 } else if (show_node == NULL) {
2289                         cYAML_insert_sibling((*show_rc)->cy_child,
2290                                                 net_node);
2291                         free(root);
2292                 } else {
2293                         cYAML_free_tree(root);
2294                 }
2295         } else {
2296                 *show_rc = root;
2297         }
2298
2299         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2300
2301         return rc;
2302 }
2303
2304 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2305 {
2306         struct lnet_ioctl_config_data data;
2307         int rc = LUSTRE_CFG_RC_NO_ERR;
2308         char err_str[LNET_MAX_STR_LEN];
2309
2310         snprintf(err_str, sizeof(err_str), "\"success\"");
2311
2312         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2313         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2314
2315         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2316         if (rc != 0) {
2317                 rc = -errno;
2318                 snprintf(err_str,
2319                          sizeof(err_str),
2320                          "\"cannot %s routing %s\"",
2321                          (enable) ? "enable" : "disable", strerror(errno));
2322                 goto out;
2323         }
2324
2325 out:
2326         cYAML_build_error(rc, seq_no,
2327                          (enable) ? ADD_CMD : DEL_CMD,
2328                          "routing", err_str, err_rc);
2329
2330         return rc;
2331 }
2332
2333 int ioctl_set_value(__u32 val, int ioc, char *name,
2334                     int seq_no, struct cYAML **err_rc)
2335 {
2336         struct lnet_ioctl_set_value data;
2337         int rc = LUSTRE_CFG_RC_NO_ERR;
2338         char err_str[LNET_MAX_STR_LEN];
2339
2340         snprintf(err_str, sizeof(err_str), "\"success\"");
2341
2342         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2343         data.sv_value = val;
2344
2345         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2346         if (rc != 0) {
2347                 rc = -errno;
2348                 snprintf(err_str,
2349                          sizeof(err_str),
2350                          "\"cannot configure %s to %d: %s\"", name,
2351                          val, strerror(errno));
2352         }
2353
2354         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2355
2356         return rc;
2357 }
2358
2359 int lustre_lnet_config_recov_intrv(int intrv, int seq_no, struct cYAML **err_rc)
2360 {
2361         int rc = LUSTRE_CFG_RC_NO_ERR;
2362         char err_str[LNET_MAX_STR_LEN];
2363         char val[LNET_MAX_STR_LEN];
2364
2365         snprintf(err_str, sizeof(err_str), "\"success\"");
2366
2367         snprintf(val, sizeof(val), "%d", intrv);
2368
2369         rc = write_sysfs_file(modparam_path, "lnet_recovery_interval", val,
2370                               1, strlen(val) + 1);
2371         if (rc)
2372                 snprintf(err_str, sizeof(err_str),
2373                          "\"cannot configure recovery interval: %s\"",
2374                          strerror(errno));
2375
2376         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_interval", err_str, err_rc);
2377
2378         return rc;
2379 }
2380
2381 int lustre_lnet_config_rtr_sensitivity(int sen, int seq_no, struct cYAML **err_rc)
2382 {
2383         int rc = LUSTRE_CFG_RC_NO_ERR;
2384         char err_str[LNET_MAX_STR_LEN];
2385         char val[LNET_MAX_STR_LEN];
2386
2387         snprintf(err_str, sizeof(err_str), "\"success\"");
2388
2389         snprintf(val, sizeof(val), "%d", sen);
2390
2391         rc = write_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
2392                               1, strlen(val) + 1);
2393         if (rc)
2394                 snprintf(err_str, sizeof(err_str),
2395                          "\"cannot configure router health sensitivity: %s\"",
2396                          strerror(errno));
2397
2398         cYAML_build_error(rc, seq_no, ADD_CMD, "router_sensitivity", err_str, err_rc);
2399
2400         return rc;
2401 }
2402
2403 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2404 {
2405         int rc = LUSTRE_CFG_RC_NO_ERR;
2406         char err_str[LNET_MAX_STR_LEN];
2407         char val[LNET_MAX_STR_LEN];
2408
2409         snprintf(err_str, sizeof(err_str), "\"success\"");
2410
2411         snprintf(val, sizeof(val), "%d", sen);
2412
2413         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2414                               1, strlen(val) + 1);
2415         if (rc)
2416                 snprintf(err_str, sizeof(err_str),
2417                          "\"cannot configure health sensitivity: %s\"",
2418                          strerror(errno));
2419
2420         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2421
2422         return rc;
2423 }
2424
2425 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2426 {
2427         int rc = LUSTRE_CFG_RC_NO_ERR;
2428         char err_str[LNET_MAX_STR_LEN];
2429         char val[LNET_MAX_STR_LEN];
2430
2431         snprintf(err_str, sizeof(err_str), "\"success\"");
2432
2433         snprintf(val, sizeof(val), "%d", timeout);
2434
2435         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2436                               1, strlen(val) + 1);
2437         if (rc)
2438                 snprintf(err_str, sizeof(err_str),
2439                          "\"cannot configure transaction timeout: %s\"",
2440                          strerror(errno));
2441
2442         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2443
2444         return rc;
2445 }
2446
2447 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2448 {
2449         int rc = LUSTRE_CFG_RC_NO_ERR;
2450         char err_str[LNET_MAX_STR_LEN];
2451         char val[LNET_MAX_STR_LEN];
2452
2453         snprintf(err_str, sizeof(err_str), "\"success\"");
2454
2455         snprintf(val, sizeof(val), "%d", count);
2456
2457         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2458                               1, strlen(val) + 1);
2459         if (rc)
2460                 snprintf(err_str, sizeof(err_str),
2461                          "\"cannot configure retry count: %s\"",
2462                          strerror(errno));
2463
2464         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2465
2466         return rc;
2467 }
2468
2469 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2470 {
2471         int rc = LUSTRE_CFG_RC_NO_ERR;
2472         char err_str[LNET_MAX_STR_LEN];
2473         char val[LNET_MAX_STR_LEN];
2474
2475         snprintf(err_str, sizeof(err_str), "\"success\"");
2476
2477         snprintf(val, sizeof(val), "%d", max);
2478
2479         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2480                               1, strlen(val) + 1);
2481         if (rc)
2482                 snprintf(err_str, sizeof(err_str),
2483                          "\"cannot configure max interfaces: %s\"",
2484                          strerror(errno));
2485
2486         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2487
2488         return rc;
2489 }
2490
2491 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2492 {
2493         int rc = LUSTRE_CFG_RC_NO_ERR;
2494         char err_str[LNET_MAX_STR_LEN];
2495         char val[LNET_MAX_STR_LEN];
2496
2497         snprintf(err_str, sizeof(err_str), "\"success\"");
2498
2499         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2500
2501         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2502                               1, strlen(val) + 1);
2503         if (rc)
2504                 snprintf(err_str, sizeof(err_str),
2505                          "\"cannot configure discovery: %s\"",
2506                          strerror(errno));
2507
2508         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2509
2510         return rc;
2511
2512 }
2513
2514 int lustre_lnet_config_drop_asym_route(int drop, int seq_no,
2515                                        struct cYAML **err_rc)
2516 {
2517         int rc = LUSTRE_CFG_RC_NO_ERR;
2518         char err_str[LNET_MAX_STR_LEN];
2519         char val[LNET_MAX_STR_LEN];
2520
2521         snprintf(err_str, sizeof(err_str), "\"success\"");
2522
2523         snprintf(val, sizeof(val), "%u", (drop) ? 1 : 0);
2524
2525         rc = write_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
2526                               1, strlen(val) + 1);
2527         if (rc)
2528                 snprintf(err_str, sizeof(err_str),
2529                          "\"cannot configure drop asym route: %s\"",
2530                          strerror(errno));
2531
2532         cYAML_build_error(rc, seq_no, ADD_CMD, "drop_asym_route",
2533                           err_str, err_rc);
2534
2535         return rc;
2536
2537 }
2538
2539 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2540 {
2541         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2542                                "numa_range", seq_no, err_rc);
2543 }
2544
2545 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2546                                struct cYAML **err_rc)
2547 {
2548         struct lnet_ioctl_config_data data;
2549         int rc = LUSTRE_CFG_RC_NO_ERR;
2550         char err_str[LNET_MAX_STR_LEN];
2551
2552         snprintf(err_str, sizeof(err_str), "\"success\"");
2553
2554         /* -1 indicates to ignore changes to this field */
2555         if (tiny < -1 || small < -1 || large < -1) {
2556                 snprintf(err_str,
2557                          sizeof(err_str),
2558                          "\"tiny, small and large must be >= 0\"");
2559                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2560                 goto out;
2561         }
2562
2563         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2564         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2565         data.cfg_config_u.cfg_buffers.buf_small = small;
2566         data.cfg_config_u.cfg_buffers.buf_large = large;
2567
2568         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2569         if (rc != 0) {
2570                 rc = -errno;
2571                 snprintf(err_str,
2572                          sizeof(err_str),
2573                          "\"cannot configure buffers: %s\"", strerror(errno));
2574                 goto out;
2575         }
2576
2577 out:
2578         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2579
2580         return rc;
2581 }
2582
2583 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2584                              struct cYAML **err_rc, bool backup)
2585 {
2586         struct lnet_ioctl_config_data *data;
2587         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2588         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2589         int l_errno = 0;
2590         char *buf;
2591         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2592         int buf_count[LNET_NRBPOOLS] = {0};
2593         struct cYAML *root = NULL, *pools_node = NULL,
2594                      *type_node = NULL, *item = NULL, *cpt = NULL,
2595                      *first_seq = NULL, *buffers = NULL;
2596         int i, j;
2597         char err_str[LNET_MAX_STR_LEN];
2598         char node_name[LNET_MAX_STR_LEN];
2599         bool exist = false;
2600
2601         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
2602
2603         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2604         if (buf == NULL)
2605                 goto out;
2606
2607         data = (struct lnet_ioctl_config_data *)buf;
2608
2609         root = cYAML_create_object(NULL, NULL);
2610         if (root == NULL)
2611                 goto out;
2612
2613         if (backup)
2614                 pools_node = cYAML_create_object(root, "routing");
2615         else
2616                 pools_node = cYAML_create_seq(root, "routing");
2617         if (pools_node == NULL)
2618                 goto out;
2619
2620         for (i = 0;; i++) {
2621                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2622                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2623                                         sizeof(struct lnet_ioctl_pool_cfg);
2624                 data->cfg_count = i;
2625
2626                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2627                 if (rc != 0) {
2628                         l_errno = errno;
2629                         break;
2630                 }
2631
2632                 exist = true;
2633
2634                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2635
2636                 if (backup)
2637                         goto calculate_buffers;
2638
2639                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2640                 item = cYAML_create_seq_item(pools_node);
2641                 if (item == NULL)
2642                         goto out;
2643
2644                 if (first_seq == NULL)
2645                         first_seq = item;
2646
2647                 cpt = cYAML_create_object(item, node_name);
2648                 if (cpt == NULL)
2649                         goto out;
2650
2651 calculate_buffers:
2652                 /* create the tree  and print */
2653                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2654                         if (!backup) {
2655                                 type_node = cYAML_create_object(cpt, pools[j]);
2656                                 if (type_node == NULL)
2657                                         goto out;
2658                         }
2659                         if (!backup &&
2660                             cYAML_create_number(type_node, "npages",
2661                                                 pool_cfg->pl_pools[j].pl_npages)
2662                             == NULL)
2663                                 goto out;
2664                         if (!backup &&
2665                             cYAML_create_number(type_node, "nbuffers",
2666                                                 pool_cfg->pl_pools[j].
2667                                                   pl_nbuffers) == NULL)
2668                                 goto out;
2669                         if (!backup &&
2670                             cYAML_create_number(type_node, "credits",
2671                                                 pool_cfg->pl_pools[j].
2672                                                    pl_credits) == NULL)
2673                                 goto out;
2674                         if (!backup &&
2675                             cYAML_create_number(type_node, "mincredits",
2676                                                 pool_cfg->pl_pools[j].
2677                                                    pl_mincredits) == NULL)
2678                                 goto out;
2679                         /* keep track of the total count for each of the
2680                          * tiny, small and large buffers */
2681                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2682                 }
2683         }
2684
2685         if (pool_cfg != NULL) {
2686                 if (backup) {
2687                         if (cYAML_create_number(pools_node, "enable",
2688                                                 pool_cfg->pl_routing) ==
2689                         NULL)
2690                                 goto out;
2691
2692                         goto add_buffer_section;
2693                 }
2694
2695                 item = cYAML_create_seq_item(pools_node);
2696                 if (item == NULL)
2697                         goto out;
2698
2699                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2700                     NULL)
2701                         goto out;
2702         }
2703
2704 add_buffer_section:
2705         /* create a buffers entry in the show. This is necessary so that
2706          * if the YAML output is used to configure a node, the buffer
2707          * configuration takes hold */
2708         buffers = cYAML_create_object(root, "buffers");
2709         if (buffers == NULL)
2710                 goto out;
2711
2712         for (i = 0; i < LNET_NRBPOOLS; i++) {
2713                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2714                         goto out;
2715         }
2716
2717         if (show_rc == NULL)
2718                 cYAML_print_tree(root);
2719
2720         if (l_errno != ENOENT) {
2721                 snprintf(err_str,
2722                          sizeof(err_str),
2723                          "\"cannot get routing information: %s\"",
2724                          strerror(l_errno));
2725                 rc = -l_errno;
2726                 goto out;
2727         } else
2728                 rc = LUSTRE_CFG_RC_NO_ERR;
2729
2730         snprintf(err_str, sizeof(err_str), "\"success\"");
2731         rc = LUSTRE_CFG_RC_NO_ERR;
2732
2733 out:
2734         free(buf);
2735         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2736                 cYAML_free_tree(root);
2737         } else if (show_rc != NULL && *show_rc != NULL) {
2738                 struct cYAML *routing_node;
2739                 /* there should exist only one routing block and one
2740                  * buffers block. If there already exists a previous one
2741                  * then don't add another */
2742                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2743                 if (routing_node == NULL) {
2744                         cYAML_insert_sibling((*show_rc)->cy_child,
2745                                                 root->cy_child);
2746                         free(root);
2747                 } else {
2748                         cYAML_free_tree(root);
2749                 }
2750         } else {
2751                 *show_rc = root;
2752         }
2753
2754         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2755
2756         return rc;
2757 }
2758
2759 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2760                           struct cYAML **show_rc, struct cYAML **err_rc,
2761                           bool backup)
2762 {
2763         /*
2764          * TODO: This function is changing in a future patch to accommodate
2765          * PEER_LIST and proper filtering on any nid of the peer
2766          */
2767         struct lnet_ioctl_peer_cfg peer_info;
2768         struct lnet_peer_ni_credit_info *lpni_cri;
2769         struct lnet_ioctl_element_stats *lpni_stats;
2770         struct lnet_ioctl_element_msg_stats *msg_stats;
2771         struct lnet_ioctl_peer_ni_hstats *hstats;
2772         lnet_nid_t *nidp;
2773         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2774         int i, j, k;
2775         int l_errno = 0;
2776         __u32 count;
2777         __u32 size;
2778         struct cYAML *root = NULL, *peer = NULL, *peer_ni = NULL,
2779                      *first_seq = NULL, *peer_root = NULL, *tmp = NULL,
2780                      *msg_statistics = NULL, *statistics = NULL,
2781                      *yhstats;
2782         char err_str[LNET_MAX_STR_LEN];
2783         struct lnet_process_id *list = NULL;
2784         void *data = NULL;
2785         void *lpni_data;
2786         bool exist = false;
2787
2788         snprintf(err_str, sizeof(err_str),
2789                  "\"out of memory\"");
2790
2791         /* create struct cYAML root object */
2792         root = cYAML_create_object(NULL, NULL);
2793         if (root == NULL)
2794                 goto out;
2795
2796         peer_root = cYAML_create_seq(root, "peer");
2797         if (peer_root == NULL)
2798                 goto out;
2799
2800         count = 1000;
2801         size = count * sizeof(struct lnet_process_id);
2802         list = malloc(size);
2803         if (list == NULL) {
2804                 l_errno = ENOMEM;
2805                 goto out;
2806         }
2807         if (knid != NULL) {
2808                 list[0].nid = libcfs_str2nid(knid);
2809                 count = 1;
2810         } else {
2811                 for (;;) {
2812                         memset(&peer_info, 0, sizeof(peer_info));
2813                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2814                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2815                         peer_info.prcfg_size = size;
2816                         peer_info.prcfg_bulk = list;
2817
2818                         l_errno = 0;
2819                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST,
2820                                      &peer_info);
2821                         count = peer_info.prcfg_count;
2822                         if (rc == 0)
2823                                 break;
2824                         l_errno = errno;
2825                         if (l_errno != E2BIG) {
2826                                 snprintf(err_str,
2827                                         sizeof(err_str),
2828                                         "\"cannot get peer list: %s\"",
2829                                         strerror(l_errno));
2830                                 rc = -l_errno;
2831                                 goto out;
2832                         }
2833                         free(list);
2834                         size = peer_info.prcfg_size;
2835                         list = malloc(size);
2836                         if (list == NULL) {
2837                                 l_errno = ENOMEM;
2838                                 goto out;
2839                         }
2840                 }
2841         }
2842
2843         size = 4096;
2844         data = malloc(size);
2845         if (data == NULL) {
2846                 l_errno = ENOMEM;
2847                 goto out;
2848         }
2849
2850         for (i = 0; i < count; i++) {
2851                 for (;;) {
2852                         memset(&peer_info, 0, sizeof(peer_info));
2853                         LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
2854                         peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
2855                         peer_info.prcfg_prim_nid = list[i].nid;
2856                         peer_info.prcfg_size = size;
2857                         peer_info.prcfg_bulk = data;
2858
2859                         l_errno = 0;
2860                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_NI,
2861                                      &peer_info);
2862                         if (rc == 0)
2863                                 break;
2864                         l_errno = errno;
2865                         if (l_errno != E2BIG) {
2866                                 snprintf(err_str,
2867                                         sizeof(err_str),
2868                                         "\"cannot get peer information: %s\"",
2869                                         strerror(l_errno));
2870                                 rc = -l_errno;
2871                                 goto out;
2872                         }
2873                         free(data);
2874                         size = peer_info.prcfg_size;
2875                         data = malloc(size);
2876                         if (data == NULL) {
2877                                 l_errno = ENOMEM;
2878                                 goto out;
2879                         }
2880                 }
2881                 exist = true;
2882
2883                 peer = cYAML_create_seq_item(peer_root);
2884                 if (peer == NULL)
2885                         goto out;
2886
2887                 if (first_seq == NULL)
2888                         first_seq = peer;
2889
2890                 lnet_nid_t pnid = peer_info.prcfg_prim_nid;
2891                 if (cYAML_create_string(peer, "primary nid",
2892                                         libcfs_nid2str(pnid))
2893                     == NULL)
2894                         goto out;
2895                 if (cYAML_create_string(peer, "Multi-Rail",
2896                                         peer_info.prcfg_mr ? "True" : "False")
2897                     == NULL)
2898                         goto out;
2899                 /*
2900                  * print out the state of the peer only if details are
2901                  * requested
2902                  */
2903                 if (detail >= 3) {
2904                         if (!backup &&
2905                             cYAML_create_number(peer, "peer state",
2906                                                 peer_info.prcfg_state)
2907                                 == NULL)
2908                                 goto out;
2909                 }
2910
2911                 tmp = cYAML_create_seq(peer, "peer ni");
2912                 if (tmp == NULL)
2913                         goto out;
2914
2915                 lpni_data = data;
2916                 for (j = 0; j < peer_info.prcfg_count; j++) {
2917                         nidp = lpni_data;
2918                         lpni_cri = (void*)nidp + sizeof(nidp);
2919                         lpni_stats = (void *)lpni_cri + sizeof(*lpni_cri);
2920                         msg_stats = (void *)lpni_stats + sizeof(*lpni_stats);
2921                         hstats = (void *)msg_stats + sizeof(*msg_stats);
2922                         lpni_data = (void *)hstats + sizeof(*hstats);
2923
2924                         peer_ni = cYAML_create_seq_item(tmp);
2925                         if (peer_ni == NULL)
2926                                 goto out;
2927
2928                         if (cYAML_create_string(peer_ni, "nid",
2929                                                 libcfs_nid2str(*nidp))
2930                             == NULL)
2931                                 goto out;
2932
2933                         if (backup)
2934                                 continue;
2935
2936                         if (cYAML_create_string(peer_ni, "state",
2937                                                 lpni_cri->cr_aliveness)
2938                             == NULL)
2939                                 goto out;
2940
2941                         if (!detail)
2942                                 continue;
2943
2944                         if (cYAML_create_number(peer_ni, "max_ni_tx_credits",
2945                                                 lpni_cri->cr_ni_peer_tx_credits)
2946                             == NULL)
2947                                 goto out;
2948
2949                         if (cYAML_create_number(peer_ni, "available_tx_credits",
2950                                                 lpni_cri->cr_peer_tx_credits)
2951                             == NULL)
2952                                 goto out;
2953
2954                         if (cYAML_create_number(peer_ni, "min_tx_credits",
2955                                                 lpni_cri->cr_peer_min_tx_credits)
2956                             == NULL)
2957                                 goto out;
2958
2959                         if (cYAML_create_number(peer_ni, "tx_q_num_of_buf",
2960                                                 lpni_cri->cr_peer_tx_qnob)
2961                             == NULL)
2962                                 goto out;
2963
2964                         if (cYAML_create_number(peer_ni, "available_rtr_credits",
2965                                                 lpni_cri->cr_peer_rtr_credits)
2966                             == NULL)
2967                                 goto out;
2968
2969                         if (cYAML_create_number(peer_ni, "min_rtr_credits",
2970                                                 lpni_cri->cr_peer_min_rtr_credits)
2971                             == NULL)
2972                                 goto out;
2973
2974                         if (cYAML_create_number(peer_ni, "refcount",
2975                                                 lpni_cri->cr_refcount) == NULL)
2976                                 goto out;
2977
2978                         statistics = cYAML_create_object(peer_ni, "statistics");
2979                         if (statistics == NULL)
2980                                 goto out;
2981
2982                         if (cYAML_create_number(statistics, "send_count",
2983                                                 lpni_stats->iel_send_count)
2984                             == NULL)
2985                                 goto out;
2986
2987                         if (cYAML_create_number(statistics, "recv_count",
2988                                                 lpni_stats->iel_recv_count)
2989                             == NULL)
2990                                 goto out;
2991
2992                         if (cYAML_create_number(statistics, "drop_count",
2993                                                 lpni_stats->iel_drop_count)
2994                             == NULL)
2995                                 goto out;
2996
2997                         if (detail < 2)
2998                                 continue;
2999
3000                         for (k = 0; k < 3; k++) {
3001                                 struct lnet_ioctl_comm_count *counts;
3002
3003                                 msg_statistics = cYAML_create_object(peer_ni,
3004                                                  (char *) gmsg_stat_names[k]);
3005                                 if (msg_statistics == NULL)
3006                                         goto out;
3007
3008                                 counts = get_counts(msg_stats, k);
3009                                 if (counts == NULL)
3010                                         goto out;
3011
3012                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
3013                                                                counts))
3014                                         goto out;
3015                         }
3016
3017                         yhstats = cYAML_create_object(peer_ni, "health stats");
3018                         if (!yhstats)
3019                                 goto out;
3020                         if (cYAML_create_number(yhstats, "health value",
3021                                                 hstats->hlpni_health_value)
3022                                                         == NULL)
3023                                 goto out;
3024                         if (cYAML_create_number(yhstats, "dropped",
3025                                                 hstats->hlpni_remote_dropped)
3026                                                         == NULL)
3027                                 goto out;
3028                         if (cYAML_create_number(yhstats, "timeout",
3029                                                 hstats->hlpni_remote_timeout)
3030                                                         == NULL)
3031                                 goto out;
3032                         if (cYAML_create_number(yhstats, "error",
3033                                                 hstats->hlpni_remote_error)
3034                                                         == NULL)
3035                                 goto out;
3036                         if (cYAML_create_number(yhstats, "network timeout",
3037                                                 hstats->hlpni_network_timeout)
3038                                                         == NULL)
3039                                 goto out;
3040                 }
3041         }
3042
3043         /* print output iff show_rc is not provided */
3044         if (show_rc == NULL)
3045                 cYAML_print_tree(root);
3046
3047         snprintf(err_str, sizeof(err_str), "\"success\"");
3048         rc = LUSTRE_CFG_RC_NO_ERR;
3049
3050 out:
3051         free(list);
3052         free(data);
3053         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
3054                 cYAML_free_tree(root);
3055         } else if (show_rc != NULL && *show_rc != NULL) {
3056                 struct cYAML *show_node;
3057                 /* find the peer node, if one doesn't exist then
3058                  * insert one.  Otherwise add to the one there
3059                  */
3060                 show_node = cYAML_get_object_item(*show_rc,
3061                                                   "peer");
3062                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3063                         cYAML_insert_child(show_node, first_seq);
3064                         free(peer_root);
3065                         free(root);
3066                 } else if (show_node == NULL) {
3067                         cYAML_insert_sibling((*show_rc)->cy_child,
3068                                              peer_root);
3069                         free(root);
3070                 } else {
3071                         cYAML_free_tree(root);
3072                 }
3073         } else {
3074                 *show_rc = root;
3075         }
3076
3077         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3078                           err_rc);
3079
3080         return rc;
3081 }
3082
3083 int lustre_lnet_list_peer(int seq_no,
3084                           struct cYAML **show_rc, struct cYAML **err_rc)
3085 {
3086         struct lnet_ioctl_peer_cfg peer_info;
3087         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3088         __u32 count;
3089         __u32 size;
3090         int i = 0;
3091         int l_errno = 0;
3092         struct cYAML *root = NULL, *list_root = NULL, *first_seq = NULL;
3093         char err_str[LNET_MAX_STR_LEN];
3094         struct lnet_process_id *list = NULL;
3095
3096         snprintf(err_str, sizeof(err_str),
3097                  "\"out of memory\"");
3098
3099         memset(&peer_info, 0, sizeof(peer_info));
3100
3101         /* create struct cYAML root object */
3102         root = cYAML_create_object(NULL, NULL);
3103         if (root == NULL)
3104                 goto out;
3105
3106         list_root = cYAML_create_seq(root, "peer list");
3107         if (list_root == NULL)
3108                 goto out;
3109
3110         count = 1000;
3111         size = count * sizeof(struct lnet_process_id);
3112         list = malloc(size);
3113         if (list == NULL) {
3114                 l_errno = ENOMEM;
3115                 goto out;
3116         }
3117         for (;;) {
3118                 LIBCFS_IOC_INIT_V2(peer_info, prcfg_hdr);
3119                 peer_info.prcfg_hdr.ioc_len = sizeof(peer_info);
3120                 peer_info.prcfg_size = size;
3121                 peer_info.prcfg_bulk = list;
3122
3123                 l_errno = 0;
3124                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_PEER_LIST, &peer_info);
3125                 count = peer_info.prcfg_count;
3126                 if (rc == 0)
3127                         break;
3128                 l_errno = errno;
3129                 if (l_errno != E2BIG) {
3130                         snprintf(err_str,
3131                                 sizeof(err_str),
3132                                 "\"cannot get peer list: %s\"",
3133                                 strerror(l_errno));
3134                         rc = -l_errno;
3135                         goto out;
3136                 }
3137                 free(list);
3138                 size = peer_info.prcfg_size;
3139                 list = malloc(size);
3140                 if (list == NULL) {
3141                         l_errno = ENOMEM;
3142                         goto out;
3143                 }
3144         }
3145
3146         /* count is now the actual number of ids in the list. */
3147         for (i = 0; i < count; i++) {
3148                 if (cYAML_create_string(list_root, "nid",
3149                                         libcfs_nid2str(list[i].nid))
3150                     == NULL)
3151                         goto out;
3152         }
3153
3154         /* print output iff show_rc is not provided */
3155         if (show_rc == NULL)
3156                 cYAML_print_tree(root);
3157
3158         snprintf(err_str, sizeof(err_str), "\"success\"");
3159         rc = LUSTRE_CFG_RC_NO_ERR;
3160
3161 out:
3162         if (list != NULL)
3163                 free(list);
3164         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3165                 cYAML_free_tree(root);
3166         } else if (show_rc != NULL && *show_rc != NULL) {
3167                 struct cYAML *show_node;
3168                 /* find the peer node, if one doesn't exist then
3169                  * insert one.  Otherwise add to the one there
3170                  */
3171                 show_node = cYAML_get_object_item(*show_rc,
3172                                                   "peer");
3173                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3174                         cYAML_insert_child(show_node, first_seq);
3175                         free(list_root);
3176                         free(root);
3177                 } else if (show_node == NULL) {
3178                         cYAML_insert_sibling((*show_rc)->cy_child,
3179                                              list_root);
3180                         free(root);
3181                 } else {
3182                         cYAML_free_tree(root);
3183                 }
3184         } else {
3185                 *show_rc = root;
3186         }
3187
3188         cYAML_build_error(rc, seq_no, SHOW_CMD, "peer", err_str,
3189                           err_rc);
3190
3191         return rc;
3192 }
3193
3194 static void add_to_global(struct cYAML *show_rc, struct cYAML *node,
3195                           struct cYAML *root)
3196 {
3197         struct cYAML *show_node;
3198
3199         show_node = cYAML_get_object_item(show_rc, "global");
3200         if (show_node != NULL)
3201                 cYAML_insert_sibling(show_node->cy_child,
3202                                      node->cy_child);
3203         else
3204                 cYAML_insert_sibling(show_rc->cy_child,
3205                                      node);
3206         free(root);
3207 }
3208
3209 static int build_global_yaml_entry(char *err_str, int err_len, int seq_no,
3210                                    char *name, __u64 value,
3211                                    struct cYAML **show_rc,
3212                                    struct cYAML **err_rc, int err)
3213 {
3214         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3215         struct cYAML *root = NULL, *global = NULL;
3216
3217         if (err) {
3218                 rc = err;
3219                 goto out;
3220         }
3221
3222         root = cYAML_create_object(NULL, NULL);
3223         if (root == NULL)
3224                 goto out;
3225
3226         global = cYAML_create_object(root, "global");
3227         if (global == NULL)
3228                 goto out;
3229
3230         if (cYAML_create_number(global, name,
3231                                 value) == NULL)
3232                 goto out;
3233
3234         if (show_rc == NULL)
3235                 cYAML_print_tree(root);
3236
3237         snprintf(err_str, err_len, "\"success\"");
3238
3239         rc = LUSTRE_CFG_RC_NO_ERR;
3240
3241 out:
3242         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3243                 cYAML_free_tree(root);
3244         } else if (show_rc != NULL && *show_rc != NULL) {
3245                 add_to_global(*show_rc, global, root);
3246         } else {
3247                 *show_rc = root;
3248         }
3249
3250         cYAML_build_error(rc, seq_no, SHOW_CMD, "global", err_str, err_rc);
3251
3252         return rc;
3253 }
3254
3255 static int ioctl_show_global_values(int ioc, int seq_no, char *name,
3256                                     struct cYAML **show_rc,
3257                                     struct cYAML **err_rc)
3258 {
3259         struct lnet_ioctl_set_value data;
3260         int rc;
3261         int l_errno = 0;
3262         char err_str[LNET_MAX_STR_LEN];
3263
3264         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3265
3266         LIBCFS_IOC_INIT_V2(data, sv_hdr);
3267
3268         rc = l_ioctl(LNET_DEV_ID, ioc, &data);
3269         if (rc != 0) {
3270                 l_errno = -errno;
3271                 snprintf(err_str,
3272                          sizeof(err_str),
3273                          "\"cannot get %s: %s\"",
3274                          name, strerror(l_errno));
3275         }
3276
3277         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no, name,
3278                                        data.sv_value, show_rc, err_rc, l_errno);
3279 }
3280
3281 int lustre_lnet_show_recov_intrv(int seq_no, struct cYAML **show_rc,
3282                                  struct cYAML **err_rc)
3283 {
3284         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3285         char val[LNET_MAX_STR_LEN];
3286         int intrv = -1, l_errno = 0;
3287         char err_str[LNET_MAX_STR_LEN];
3288
3289         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3290
3291         rc = read_sysfs_file(modparam_path, "lnet_recovery_interval", val,
3292                              1, sizeof(val));
3293         if (rc) {
3294                 l_errno = -errno;
3295                 snprintf(err_str, sizeof(err_str),
3296                          "\"cannot get recovery interval: %d\"", rc);
3297         } else {
3298                 intrv = atoi(val);
3299         }
3300
3301         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3302                                        "recovery_interval", intrv, show_rc,
3303                                        err_rc, l_errno);
3304 }
3305
3306 int lustre_lnet_show_hsensitivity(int seq_no, struct cYAML **show_rc,
3307                                   struct cYAML **err_rc)
3308 {
3309         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3310         char val[LNET_MAX_STR_LEN];
3311         int sen = -1, l_errno = 0;
3312         char err_str[LNET_MAX_STR_LEN];
3313
3314         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3315
3316         rc = read_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
3317                              1, sizeof(val));
3318         if (rc) {
3319                 l_errno = -errno;
3320                 snprintf(err_str, sizeof(err_str),
3321                          "\"cannot get health sensitivity: %d\"", rc);
3322         } else {
3323                 sen = atoi(val);
3324         }
3325
3326         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3327                                        "health_sensitivity", sen, show_rc,
3328                                        err_rc, l_errno);
3329 }
3330
3331 int lustre_lnet_show_rtr_sensitivity(int seq_no, struct cYAML **show_rc,
3332                                      struct cYAML **err_rc)
3333 {
3334         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3335         char val[LNET_MAX_STR_LEN];
3336         int sen = -1, l_errno = 0;
3337         char err_str[LNET_MAX_STR_LEN];
3338
3339         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3340
3341         rc = read_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
3342                              1, sizeof(val));
3343         if (rc) {
3344                 l_errno = -errno;
3345                 snprintf(err_str, sizeof(err_str),
3346                          "\"cannot get router sensitivity percentage: %d\"", rc);
3347         } else {
3348                 sen = atoi(val);
3349         }
3350
3351         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3352                                        "router_sensitivity", sen, show_rc,
3353                                        err_rc, l_errno);
3354 }
3355
3356 int lustre_lnet_show_transaction_to(int seq_no, struct cYAML **show_rc,
3357                                     struct cYAML **err_rc)
3358 {
3359         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3360         char val[LNET_MAX_STR_LEN];
3361         int tto = -1, l_errno = 0;
3362         char err_str[LNET_MAX_STR_LEN];
3363
3364         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3365
3366         rc = read_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
3367                              1, sizeof(val));
3368         if (rc) {
3369                 l_errno = -errno;
3370                 snprintf(err_str, sizeof(err_str),
3371                          "\"cannot get transaction timeout: %d\"", rc);
3372         } else {
3373                 tto = atoi(val);
3374         }
3375
3376         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3377                                        "transaction_timeout", tto, show_rc,
3378                                        err_rc, l_errno);
3379 }
3380
3381 int lustre_lnet_show_retry_count(int seq_no, struct cYAML **show_rc,
3382                                  struct cYAML **err_rc)
3383 {
3384         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3385         char val[LNET_MAX_STR_LEN];
3386         int retry_count = -1, l_errno = 0;
3387         char err_str[LNET_MAX_STR_LEN];
3388
3389         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3390
3391         rc = read_sysfs_file(modparam_path, "lnet_retry_count", val,
3392                              1, sizeof(val));
3393         if (rc) {
3394                 l_errno = -errno;
3395                 snprintf(err_str, sizeof(err_str),
3396                          "\"cannot get retry count: %d\"", rc);
3397         } else {
3398                 retry_count = atoi(val);
3399         }
3400
3401         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3402                                        "retry_count", retry_count, show_rc,
3403                                        err_rc, l_errno);
3404 }
3405
3406 int lustre_lnet_calc_service_id(__u64 *service_id)
3407 {
3408         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3409         char val[LNET_MAX_STR_LEN];
3410         int service_port = -1, l_errno = 0;
3411
3412         rc = read_sysfs_file(o2ib_modparam_path, "service", val,
3413                              1, sizeof(val));
3414         if (rc) {
3415                 l_errno = errno;
3416                 fprintf(stderr, "error:\n    msg: \"cannot get service port: %s (%d)\"\n",
3417                         strerror(l_errno), -l_errno);
3418                 return rc;
3419         } else {
3420                 service_port = atoi(val);
3421         }
3422
3423         *service_id = htobe64(((__u64)RDMA_PS_TCP << 16) + service_port);
3424
3425         return LUSTRE_CFG_RC_NO_ERR;
3426 }
3427
3428 int show_recovery_queue(enum lnet_health_type type, char *name, int seq_no,
3429                         struct cYAML **show_rc, struct cYAML **err_rc)
3430 {
3431         struct lnet_ioctl_recovery_list nid_list;
3432         struct cYAML *root = NULL, *nids = NULL;
3433         int rc, i;
3434         char err_str[LNET_MAX_STR_LEN];
3435
3436         snprintf(err_str, sizeof(err_str), "failed to print recovery queue\n");
3437
3438         LIBCFS_IOC_INIT_V2(nid_list, rlst_hdr);
3439         nid_list.rlst_type = type;
3440
3441         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_RECOVERY_QUEUE, &nid_list);
3442         if (rc) {
3443                 rc = errno;
3444                 goto out;
3445         }
3446
3447         if (nid_list.rlst_num_nids == 0)
3448                 goto out;
3449
3450         root = cYAML_create_object(NULL, NULL);
3451         if (root == NULL)
3452                 goto out;
3453
3454         nids = cYAML_create_object(root, name);
3455         if (nids == NULL)
3456                 goto out;
3457
3458         rc = -EINVAL;
3459
3460         for (i = 0; i < nid_list.rlst_num_nids; i++) {
3461                 char nidenum[LNET_MAX_STR_LEN];
3462                 snprintf(nidenum, sizeof(nidenum), "nid-%d", i);
3463                 if (!cYAML_create_string(nids, nidenum,
3464                         libcfs_nid2str(nid_list.rlst_nid_array[i])))
3465                         goto out;
3466         }
3467
3468         snprintf(err_str, sizeof(err_str), "success\n");
3469
3470         rc = 0;
3471
3472 out:
3473         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3474                 cYAML_free_tree(root);
3475         } else if (show_rc != NULL && *show_rc != NULL) {
3476                 struct cYAML *show_node;
3477                 /* find the net node, if one doesn't exist
3478                  * then insert one.  Otherwise add to the one there
3479                  */
3480                 show_node = cYAML_get_object_item(*show_rc, name);
3481                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
3482                         cYAML_insert_child(show_node, nids);
3483                         free(nids);
3484                         free(root);
3485                 } else if (show_node == NULL) {
3486                         cYAML_insert_sibling((*show_rc)->cy_child,
3487                                                 nids);
3488                         free(root);
3489                 } else {
3490                         cYAML_free_tree(root);
3491                 }
3492         } else {
3493                 *show_rc = root;
3494         }
3495
3496         cYAML_build_error(rc, seq_no, SHOW_CMD, name, err_str, err_rc);
3497
3498         return rc;
3499 }
3500
3501 int lustre_lnet_show_local_ni_recovq(int seq_no, struct cYAML **show_rc,
3502                                      struct cYAML **err_rc)
3503 {
3504         return show_recovery_queue(LNET_HEALTH_TYPE_LOCAL_NI, "local NI recovery",
3505                                    seq_no, show_rc, err_rc);
3506 }
3507
3508 int lustre_lnet_show_peer_ni_recovq(int seq_no, struct cYAML **show_rc,
3509                                     struct cYAML **err_rc)
3510 {
3511         return show_recovery_queue(LNET_HEALTH_TYPE_PEER_NI, "peer NI recovery",
3512                                    seq_no, show_rc, err_rc);
3513 }
3514
3515 int lustre_lnet_show_max_intf(int seq_no, struct cYAML **show_rc,
3516                               struct cYAML **err_rc)
3517 {
3518         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3519         char val[LNET_MAX_STR_LEN];
3520         int max_intf = -1, l_errno = 0;
3521         char err_str[LNET_MAX_STR_LEN];
3522
3523         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3524
3525         rc = read_sysfs_file(modparam_path, "lnet_interfaces_max", val,
3526                              1, sizeof(val));
3527         if (rc) {
3528                 l_errno = -errno;
3529                 snprintf(err_str, sizeof(err_str),
3530                          "\"cannot get max interfaces: %d\"", rc);
3531         } else {
3532                 max_intf = atoi(val);
3533         }
3534
3535         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3536                                        "max_intf", max_intf, show_rc,
3537                                        err_rc, l_errno);
3538 }
3539
3540 int lustre_lnet_show_discovery(int seq_no, struct cYAML **show_rc,
3541                                struct cYAML **err_rc)
3542 {
3543         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3544         char val[LNET_MAX_STR_LEN];
3545         int discovery = -1, l_errno = 0;
3546         char err_str[LNET_MAX_STR_LEN];
3547
3548         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3549
3550         rc = read_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
3551                              1, sizeof(val));
3552         if (rc) {
3553                 l_errno = -errno;
3554                 snprintf(err_str, sizeof(err_str),
3555                          "\"cannot get discovery setting: %d\"", rc);
3556         } else {
3557                 /*
3558                  * The kernel stores a discovery disabled value. User space
3559                  * shows whether discovery is enabled. So the value must be
3560                  * inverted.
3561                  */
3562                 discovery = !atoi(val);
3563         }
3564
3565         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3566                                        "discovery", discovery, show_rc,
3567                                        err_rc, l_errno);
3568 }
3569
3570 int lustre_lnet_show_drop_asym_route(int seq_no, struct cYAML **show_rc,
3571                                      struct cYAML **err_rc)
3572 {
3573         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3574         char val[LNET_MAX_STR_LEN];
3575         int drop_asym_route = -1, l_errno = 0;
3576         char err_str[LNET_MAX_STR_LEN];
3577
3578         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3579
3580         rc = read_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
3581                              1, sizeof(val));
3582         if (rc) {
3583                 l_errno = -errno;
3584                 snprintf(err_str, sizeof(err_str),
3585                          "\"cannot get drop asym route setting: %d\"", rc);
3586         } else {
3587                 drop_asym_route = atoi(val);
3588         }
3589
3590         return build_global_yaml_entry(err_str, sizeof(err_str), seq_no,
3591                                        "drop_asym_route", drop_asym_route,
3592                                        show_rc, err_rc, l_errno);
3593 }
3594
3595 int lustre_lnet_show_numa_range(int seq_no, struct cYAML **show_rc,
3596                                 struct cYAML **err_rc)
3597 {
3598         return ioctl_show_global_values(IOC_LIBCFS_GET_NUMA_RANGE, seq_no,
3599                                         "numa_range", show_rc, err_rc);
3600 }
3601
3602 int lustre_lnet_show_stats(int seq_no, struct cYAML **show_rc,
3603                            struct cYAML **err_rc)
3604 {
3605         struct lnet_ioctl_lnet_stats data;
3606         struct lnet_counters *cntrs;
3607         int rc;
3608         int l_errno;
3609         char err_str[LNET_MAX_STR_LEN];
3610         struct cYAML *root = NULL, *stats = NULL;
3611
3612         snprintf(err_str, sizeof(err_str), "\"out of memory\"");
3613
3614         LIBCFS_IOC_INIT_V2(data, st_hdr);
3615
3616         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LNET_STATS, &data);
3617         if (rc) {
3618                 l_errno = errno;
3619                 snprintf(err_str,
3620                          sizeof(err_str),
3621                          "\"cannot get lnet statistics: %s\"",
3622                          strerror(l_errno));
3623                 rc = -l_errno;
3624                 goto out;
3625         }
3626
3627         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
3628
3629         cntrs = &data.st_cntrs;
3630
3631         root = cYAML_create_object(NULL, NULL);
3632         if (!root)
3633                 goto out;
3634
3635         stats = cYAML_create_object(root, "statistics");
3636         if (!stats)
3637                 goto out;
3638
3639         if (!cYAML_create_number(stats, "msgs_alloc",
3640                                  cntrs->lct_common.lcc_msgs_alloc))
3641                 goto out;
3642
3643         if (!cYAML_create_number(stats, "msgs_max",
3644                                  cntrs->lct_common.lcc_msgs_max))
3645                 goto out;
3646
3647         if (!cYAML_create_number(stats, "rst_alloc",
3648                                  cntrs->lct_health.lch_rst_alloc))
3649                 goto out;
3650
3651         if (!cYAML_create_number(stats, "errors",
3652                                  cntrs->lct_common.lcc_errors))
3653                 goto out;
3654
3655         if (!cYAML_create_number(stats, "send_count",
3656                                  cntrs->lct_common.lcc_send_count))
3657                 goto out;
3658
3659         if (!cYAML_create_number(stats, "resend_count",
3660                                  cntrs->lct_health.lch_resend_count))
3661                 goto out;
3662
3663         if (!cYAML_create_number(stats, "response_timeout_count",
3664                                  cntrs->lct_health.lch_response_timeout_count))
3665                 goto out;
3666
3667         if (!cYAML_create_number(stats, "local_interrupt_count",
3668                                  cntrs->lct_health.lch_local_interrupt_count))
3669                 goto out;
3670
3671         if (!cYAML_create_number(stats, "local_dropped_count",
3672                                  cntrs->lct_health.lch_local_dropped_count))
3673                 goto out;
3674
3675         if (!cYAML_create_number(stats, "local_aborted_count",
3676                                  cntrs->lct_health.lch_local_aborted_count))
3677                 goto out;
3678
3679         if (!cYAML_create_number(stats, "local_no_route_count",
3680                                  cntrs->lct_health.lch_local_no_route_count))
3681                 goto out;
3682
3683         if (!cYAML_create_number(stats, "local_timeout_count",
3684                                  cntrs->lct_health.lch_local_timeout_count))
3685                 goto out;
3686
3687         if (!cYAML_create_number(stats, "local_error_count",
3688                                  cntrs->lct_health.lch_local_error_count))
3689                 goto out;
3690
3691         if (!cYAML_create_number(stats, "remote_dropped_count",
3692                                  cntrs->lct_health.lch_remote_dropped_count))
3693                 goto out;
3694
3695         if (!cYAML_create_number(stats, "remote_error_count",
3696                                  cntrs->lct_health.lch_remote_error_count))
3697                 goto out;
3698
3699         if (!cYAML_create_number(stats, "remote_timeout_count",
3700                                  cntrs->lct_health.lch_remote_timeout_count))
3701                 goto out;
3702
3703         if (!cYAML_create_number(stats, "network_timeout_count",
3704                                  cntrs->lct_health.lch_network_timeout_count))
3705                 goto out;
3706
3707         if (!cYAML_create_number(stats, "recv_count",
3708                                  cntrs->lct_common.lcc_recv_count))
3709                 goto out;
3710
3711         if (!cYAML_create_number(stats, "route_count",
3712                                  cntrs->lct_common.lcc_route_count))
3713                 goto out;
3714
3715         if (!cYAML_create_number(stats, "drop_count",
3716                                  cntrs->lct_common.lcc_drop_count))
3717                 goto out;
3718
3719         if (!cYAML_create_number(stats, "send_length",
3720                                  cntrs->lct_common.lcc_send_length))
3721                 goto out;
3722
3723         if (!cYAML_create_number(stats, "recv_length",
3724                                  cntrs->lct_common.lcc_recv_length))
3725                 goto out;
3726
3727         if (!cYAML_create_number(stats, "route_length",
3728                                  cntrs->lct_common.lcc_route_length))
3729                 goto out;
3730
3731         if (!cYAML_create_number(stats, "drop_length",
3732                                  cntrs->lct_common.lcc_drop_length))
3733                 goto out;
3734
3735         if (!show_rc)
3736                 cYAML_print_tree(root);
3737
3738         snprintf(err_str, sizeof(err_str), "\"success\"");
3739         rc = LUSTRE_CFG_RC_NO_ERR;
3740 out:
3741         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
3742                 cYAML_free_tree(root);
3743         } else if (show_rc != NULL && *show_rc != NULL) {
3744                 cYAML_insert_sibling((*show_rc)->cy_child,
3745                                         root->cy_child);
3746                 free(root);
3747         } else {
3748                 *show_rc = root;
3749         }
3750
3751         cYAML_build_error(rc, seq_no, SHOW_CMD, "statistics", err_str, err_rc);
3752
3753         return rc;
3754 }
3755
3756 typedef int (*cmd_handler_t)(struct cYAML *tree,
3757                              struct cYAML **show_rc,
3758                              struct cYAML **err_rc);
3759
3760 static int handle_yaml_config_route(struct cYAML *tree, struct cYAML **show_rc,
3761                                     struct cYAML **err_rc)
3762 {
3763         struct cYAML *net, *gw, *hop, *prio, *sen, *seq_no;
3764
3765         net = cYAML_get_object_item(tree, "net");
3766         gw = cYAML_get_object_item(tree, "gateway");
3767         hop = cYAML_get_object_item(tree, "hop");
3768         prio = cYAML_get_object_item(tree, "priority");
3769         sen = cYAML_get_object_item(tree, "health_sensitivity");
3770         seq_no = cYAML_get_object_item(tree, "seq_no");
3771
3772         return lustre_lnet_config_route((net) ? net->cy_valuestring : NULL,
3773                                         (gw) ? gw->cy_valuestring : NULL,
3774                                         (hop) ? hop->cy_valueint : -1,
3775                                         (prio) ? prio->cy_valueint : -1,
3776                                         (sen) ? sen->cy_valueint : -1,
3777                                         (seq_no) ? seq_no->cy_valueint : -1,
3778                                         err_rc);
3779 }
3780
3781 /*
3782  *    interfaces:
3783  *        0: <intf_name>['['<expr>']']
3784  *        1: <intf_name>['['<expr>']']
3785  */
3786 static int yaml_copy_intf_info(struct cYAML *intf_tree,
3787                                struct lnet_dlc_network_descr *nw_descr)
3788 {
3789         struct cYAML *child = NULL;
3790         int intf_num = 0, rc = LUSTRE_CFG_RC_NO_ERR;
3791         struct lnet_dlc_intf_descr *intf_descr, *tmp;
3792
3793         if (intf_tree == NULL || nw_descr == NULL)
3794                 return LUSTRE_CFG_RC_BAD_PARAM;
3795
3796         /* now grab all the interfaces and their cpts */
3797         child = intf_tree->cy_child;
3798         while (child != NULL) {
3799                 if (child->cy_valuestring == NULL) {
3800                         child = child->cy_next;
3801                         continue;
3802                 }
3803
3804                 if (strlen(child->cy_valuestring) >= LNET_MAX_STR_LEN)
3805                         goto failed;
3806
3807                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist,
3808                                                 child->cy_valuestring,
3809                                                 strlen(child->cy_valuestring));
3810                 if (rc != LUSTRE_CFG_RC_NO_ERR)
3811                         goto failed;
3812
3813                 intf_num++;
3814                 child = child->cy_next;
3815         }
3816
3817         if (intf_num == 0)
3818                 return LUSTRE_CFG_RC_MISSING_PARAM;
3819
3820         return intf_num;
3821
3822 failed:
3823         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
3824                                  intf_on_network) {
3825                 list_del(&intf_descr->intf_on_network);
3826                 free_intf_descr(intf_descr);
3827         }
3828
3829         return rc;
3830 }
3831
3832 static bool
3833 yaml_extract_cmn_tunables(struct cYAML *tree,
3834                           struct lnet_ioctl_config_lnd_cmn_tunables *tunables,
3835                           struct cfs_expr_list **global_cpts)
3836 {
3837         struct cYAML *tun, *item, *smp;
3838         int rc;
3839
3840         tun = cYAML_get_object_item(tree, "tunables");
3841         if (tun != NULL) {
3842                 item = cYAML_get_object_item(tun, "peer_timeout");
3843                 if (item != NULL)
3844                         tunables->lct_peer_timeout = item->cy_valueint;
3845                 item = cYAML_get_object_item(tun, "peer_credits");
3846                 if (item != NULL)
3847                         tunables->lct_peer_tx_credits = item->cy_valueint;
3848                 item = cYAML_get_object_item(tun, "peer_buffer_credits");
3849                 if (item != NULL)
3850                         tunables->lct_peer_rtr_credits = item->cy_valueint;
3851                 item = cYAML_get_object_item(tun, "credits");
3852                 if (item != NULL)
3853                         tunables->lct_max_tx_credits = item->cy_valueint;
3854                 smp = cYAML_get_object_item(tun, "CPT");
3855                 if (smp != NULL) {
3856                         rc = cfs_expr_list_parse(smp->cy_valuestring,
3857                                                  strlen(smp->cy_valuestring),
3858                                                  0, UINT_MAX, global_cpts);
3859                         if (rc != 0)
3860                                 *global_cpts = NULL;
3861                 }
3862
3863                 return true;
3864         }
3865
3866         return false;
3867 }
3868
3869 static bool
3870 yaml_extract_tunables(struct cYAML *tree,
3871                       struct lnet_ioctl_config_lnd_tunables *tunables,
3872                       struct cfs_expr_list **global_cpts,
3873                       __u32 net_type)
3874 {
3875         bool rc;
3876
3877         rc = yaml_extract_cmn_tunables(tree, &tunables->lt_cmn,
3878                                        global_cpts);
3879
3880         if (!rc)
3881                 return rc;
3882
3883         lustre_yaml_extract_lnd_tunables(tree, net_type,
3884                                          &tunables->lt_tun);
3885
3886         return rc;
3887 }
3888
3889 /*
3890  * net:
3891  *    - net type: <net>[<NUM>]
3892   *      local NI(s):
3893  *        - nid: <ip>@<net>[<NUM>]
3894  *          status: up
3895  *          interfaces:
3896  *               0: <intf_name>['['<expr>']']
3897  *               1: <intf_name>['['<expr>']']
3898  *        tunables:
3899  *               peer_timeout: <NUM>
3900  *               peer_credits: <NUM>
3901  *               peer_buffer_credits: <NUM>
3902  *               credits: <NUM>
3903 *         lnd tunables:
3904  *               peercredits_hiw: <NUM>
3905  *               map_on_demand: <NUM>
3906  *               concurrent_sends: <NUM>
3907  *               fmr_pool_size: <NUM>
3908  *               fmr_flush_trigger: <NUM>
3909  *               fmr_cache: <NUM>
3910  *
3911  * At least one interface is required. If no interfaces are provided the
3912  * network interface can not be configured.
3913  */
3914 static int handle_yaml_config_ni(struct cYAML *tree, struct cYAML **show_rc,
3915                                  struct cYAML **err_rc)
3916 {
3917         struct cYAML *net, *intf, *seq_no, *ip2net = NULL, *local_nis = NULL,
3918                      *item = NULL;
3919         int num_entries = 0, rc;
3920         struct lnet_dlc_network_descr nw_descr;
3921         struct cfs_expr_list *global_cpts = NULL;
3922         struct lnet_ioctl_config_lnd_tunables tunables;
3923         bool found = false;
3924
3925         memset(&tunables, 0, sizeof(tunables));
3926
3927         INIT_LIST_HEAD(&nw_descr.network_on_rule);
3928         INIT_LIST_HEAD(&nw_descr.nw_intflist);
3929
3930         ip2net = cYAML_get_object_item(tree, "ip2net");
3931         net = cYAML_get_object_item(tree, "net type");
3932         if (net)
3933                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
3934         else
3935                 nw_descr.nw_id = LOLND;
3936
3937         /*
3938          * if neither net nor ip2nets are present, then we can not
3939          * configure the network.
3940          */
3941         if (!net && !ip2net)
3942                 return LUSTRE_CFG_RC_MISSING_PARAM;
3943
3944         local_nis = cYAML_get_object_item(tree, "local NI(s)");
3945         if (local_nis == NULL)
3946                 return LUSTRE_CFG_RC_MISSING_PARAM;
3947
3948         if (!cYAML_is_sequence(local_nis))
3949                 return LUSTRE_CFG_RC_BAD_PARAM;
3950
3951         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
3952                 intf = cYAML_get_object_item(item, "interfaces");
3953                 if (intf == NULL)
3954                         continue;
3955                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
3956                 if (num_entries <= 0) {
3957                         cYAML_build_error(num_entries, -1, "ni", "add",
3958                                         "bad interface list",
3959                                         err_rc);
3960                         return LUSTRE_CFG_RC_BAD_PARAM;
3961                 }
3962         }
3963
3964         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
3965                                       LNET_NETTYP(nw_descr.nw_id));
3966         seq_no = cYAML_get_object_item(tree, "seq_no");
3967
3968         rc = lustre_lnet_config_ni(&nw_descr,
3969                                    global_cpts,
3970                                    (ip2net) ? ip2net->cy_valuestring : NULL,
3971                                    (found) ? &tunables: NULL,
3972                                    (seq_no) ? seq_no->cy_valueint : -1,
3973                                    err_rc);
3974
3975         if (global_cpts != NULL)
3976                 cfs_expr_list_free(global_cpts);
3977
3978         return rc;
3979 }
3980
3981 /*
3982  * ip2nets:
3983  *  - net-spec: <tcp|o2ib|gni>[NUM]
3984  *    interfaces:
3985  *        0: <intf name>['['<expr>']']
3986  *        1: <intf name>['['<expr>']']
3987  *    ip-range:
3988  *        0: <expr.expr.expr.expr>
3989  *        1: <expr.expr.expr.expr>
3990  */
3991 static int handle_yaml_config_ip2nets(struct cYAML *tree,
3992                                       struct cYAML **show_rc,
3993                                       struct cYAML **err_rc)
3994 {
3995         struct cYAML *net, *ip_range, *item = NULL, *intf = NULL,
3996                      *seq_no = NULL;
3997         struct lustre_lnet_ip2nets ip2nets;
3998         struct lustre_lnet_ip_range_descr *ip_range_descr = NULL,
3999                                           *tmp = NULL;
4000         int rc = LUSTRE_CFG_RC_NO_ERR;
4001         struct cfs_expr_list *global_cpts = NULL;
4002         struct cfs_expr_list *el, *el_tmp;
4003         struct lnet_ioctl_config_lnd_tunables tunables;
4004         struct lnet_dlc_intf_descr *intf_descr, *intf_tmp;
4005         bool found = false;
4006
4007         memset(&tunables, 0, sizeof(tunables));
4008
4009         /* initialize all lists */
4010         INIT_LIST_HEAD(&ip2nets.ip2nets_ip_ranges);
4011         INIT_LIST_HEAD(&ip2nets.ip2nets_net.network_on_rule);
4012         INIT_LIST_HEAD(&ip2nets.ip2nets_net.nw_intflist);
4013
4014         net = cYAML_get_object_item(tree, "net-spec");
4015         if (net == NULL)
4016                 return LUSTRE_CFG_RC_BAD_PARAM;
4017
4018         if (net != NULL && net->cy_valuestring == NULL)
4019                 return LUSTRE_CFG_RC_BAD_PARAM;
4020
4021         /* assign the network id */
4022         ip2nets.ip2nets_net.nw_id = libcfs_str2net(net->cy_valuestring);
4023         if (ip2nets.ip2nets_net.nw_id == LNET_NID_ANY)
4024                 return LUSTRE_CFG_RC_BAD_PARAM;
4025
4026         seq_no = cYAML_get_object_item(tree, "seq_no");
4027
4028         intf = cYAML_get_object_item(tree, "interfaces");
4029         if (intf != NULL) {
4030                 rc = yaml_copy_intf_info(intf, &ip2nets.ip2nets_net);
4031                 if (rc <= 0)
4032                         return LUSTRE_CFG_RC_BAD_PARAM;
4033         }
4034
4035         ip_range = cYAML_get_object_item(tree, "ip-range");
4036         if (ip_range != NULL) {
4037                 item = ip_range->cy_child;
4038                 while (item != NULL) {
4039                         if (item->cy_valuestring == NULL) {
4040                                 item = item->cy_next;
4041                                 continue;
4042                         }
4043
4044                         rc = lustre_lnet_add_ip_range(&ip2nets.ip2nets_ip_ranges,
4045                                                       item->cy_valuestring);
4046
4047                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4048                                 goto out;
4049
4050                         item = item->cy_next;
4051                 }
4052         }
4053
4054         found = yaml_extract_tunables(tree, &tunables, &global_cpts,
4055                                       LNET_NETTYP(ip2nets.ip2nets_net.nw_id));
4056
4057         rc = lustre_lnet_config_ip2nets(&ip2nets,
4058                         (found) ? &tunables : NULL,
4059                         global_cpts,
4060                         (seq_no) ? seq_no->cy_valueint : -1,
4061                         err_rc);
4062
4063         /*
4064          * don't stop because there was no match. Continue processing the
4065          * rest of the rules. If non-match then nothing is configured
4066          */
4067         if (rc == LUSTRE_CFG_RC_NO_MATCH)
4068                 rc = LUSTRE_CFG_RC_NO_ERR;
4069 out:
4070         list_for_each_entry_safe(intf_descr, intf_tmp,
4071                                  &ip2nets.ip2nets_net.nw_intflist,
4072                                  intf_on_network) {
4073                 list_del(&intf_descr->intf_on_network);
4074                 free_intf_descr(intf_descr);
4075         }
4076
4077         list_for_each_entry_safe(ip_range_descr, tmp,
4078                                  &ip2nets.ip2nets_ip_ranges,
4079                                  ipr_entry) {
4080                 list_del(&ip_range_descr->ipr_entry);
4081                 list_for_each_entry_safe(el, el_tmp, &ip_range_descr->ipr_expr,
4082                                          el_link) {
4083                         list_del(&el->el_link);
4084                         cfs_expr_list_free(el);
4085                 }
4086                 free(ip_range_descr);
4087         }
4088
4089         return rc;
4090 }
4091
4092 static int handle_yaml_del_ni(struct cYAML *tree, struct cYAML **show_rc,
4093                               struct cYAML **err_rc)
4094 {
4095         struct cYAML *net = NULL, *intf = NULL, *seq_no = NULL, *item = NULL,
4096                      *local_nis = NULL;
4097         int num_entries, rc;
4098         struct lnet_dlc_network_descr nw_descr;
4099
4100         INIT_LIST_HEAD(&nw_descr.network_on_rule);
4101         INIT_LIST_HEAD(&nw_descr.nw_intflist);
4102
4103         net = cYAML_get_object_item(tree, "net type");
4104         if (net != NULL)
4105                 nw_descr.nw_id = libcfs_str2net(net->cy_valuestring);
4106
4107         local_nis = cYAML_get_object_item(tree, "local NI(s)");
4108         if (local_nis == NULL)
4109                 return LUSTRE_CFG_RC_MISSING_PARAM;
4110
4111         if (!cYAML_is_sequence(local_nis))
4112                 return LUSTRE_CFG_RC_BAD_PARAM;
4113
4114         while (cYAML_get_next_seq_item(local_nis, &item) != NULL) {
4115                 intf = cYAML_get_object_item(item, "interfaces");
4116                 if (intf == NULL)
4117                         continue;
4118                 num_entries = yaml_copy_intf_info(intf, &nw_descr);
4119                 if (num_entries <= 0) {
4120                         cYAML_build_error(num_entries, -1, "ni", "add",
4121                                         "bad interface list",
4122                                         err_rc);
4123                         return LUSTRE_CFG_RC_BAD_PARAM;
4124                 }
4125         }
4126
4127         seq_no = cYAML_get_object_item(tree, "seq_no");
4128
4129         rc = lustre_lnet_del_ni((net) ? &nw_descr : NULL,
4130                                 (seq_no) ? seq_no->cy_valueint : -1,
4131                                 err_rc);
4132
4133         return rc;
4134 }
4135
4136 /* Create a nidstring parseable by the nidstrings library from the nid
4137  * information encoded in the CYAML structure.
4138  * NOTE: Caller must free memory allocated to nidstr
4139  */
4140 static int yaml_nids2nidstr(struct cYAML *nids_entry, char **nidstr,
4141                             char *prim_nid, int cmd)
4142 {
4143         int num_strs = 0, rc;
4144         size_t buf_size, buf_pos, nidstr_len = 0;
4145         char *buffer;
4146         struct cYAML *child = NULL, *entry = NULL;
4147
4148         if (cYAML_is_sequence(nids_entry)) {
4149                 while (cYAML_get_next_seq_item(nids_entry, &child)) {
4150                         entry = cYAML_get_object_item(child, "nid");
4151                         /* don't count an empty entry */
4152                         if (!entry || !entry->cy_valuestring)
4153                                 continue;
4154
4155                         if (prim_nid &&
4156                             (strcmp(entry->cy_valuestring, prim_nid) == 0)) {
4157                                 if (cmd == LNETCTL_DEL_CMD) {
4158                                         /*
4159                                          * primary nid is present in the list of
4160                                          * nids so that means we want to delete
4161                                          * the entire peer, so no need to go
4162                                          * further. Just delete the entire peer.
4163                                          */
4164                                         return LUSTRE_CFG_RC_NO_ERR;
4165                                 } else {
4166                                         continue;
4167                                 }
4168                         }
4169
4170                         /*
4171                          * + 1 for the space separating each string, and
4172                          * accounts for the terminating null char
4173                          */
4174                         nidstr_len += strlen(entry->cy_valuestring) + 1;
4175                         num_strs++;
4176                 }
4177         }
4178
4179         if (num_strs == 0 && !prim_nid)
4180                 return LUSTRE_CFG_RC_MISSING_PARAM;
4181         else if (num_strs == 0) /* Only the primary nid was given to add/del */
4182                 return LUSTRE_CFG_RC_NO_ERR;
4183
4184         buffer = malloc(nidstr_len);
4185         if (!buffer)
4186                 return LUSTRE_CFG_RC_OUT_OF_MEM;
4187
4188         /* now grab all the nids */
4189         rc = 0;
4190         buf_pos = 0;
4191         buf_size = nidstr_len;
4192         child = NULL;
4193         while (cYAML_get_next_seq_item(nids_entry, &child)) {
4194                 entry = cYAML_get_object_item(child, "nid");
4195                 if (!entry || !entry->cy_valuestring)
4196                         continue;
4197
4198                 if (prim_nid &&
4199                     (strcmp(entry->cy_valuestring, prim_nid) == 0))
4200                         continue;
4201
4202                 if (buf_pos) {
4203                         rc = snprintf(buffer + buf_pos, buf_size, " ");
4204                         buf_pos += (rc < buf_size) ? rc : buf_size;
4205                         buf_size = nidstr_len - buf_pos;
4206                 }
4207
4208                 rc = snprintf(buffer + buf_pos, buf_size, "%s",
4209                               entry->cy_valuestring);
4210                 buf_pos += (rc < buf_size) ? rc : buf_size;
4211                 buf_size = nidstr_len - buf_pos;
4212         }
4213
4214         *nidstr = buffer;
4215
4216         return LUSTRE_CFG_RC_NO_ERR;
4217 }
4218
4219 static int handle_yaml_peer_common(struct cYAML *tree, struct cYAML **show_rc,
4220                                    struct cYAML **err_rc, int cmd)
4221 {
4222         int rc, num_nids = 0, seqn;
4223         bool mr_value;
4224         char *nidstr = NULL, *prim_nidstr;
4225         char err_str[LNET_MAX_STR_LEN];
4226         struct cYAML *seq_no, *prim_nid, *mr, *peer_nis;
4227         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
4228
4229         seq_no = cYAML_get_object_item(tree, "seq_no");
4230         seqn = seq_no ? seq_no->cy_valueint : -1;
4231
4232         prim_nid = cYAML_get_object_item(tree, "primary nid");
4233         prim_nidstr = prim_nid ? prim_nid->cy_valuestring : NULL;
4234
4235         peer_nis = cYAML_get_object_item(tree, "peer ni");
4236         if (!(prim_nid || peer_nis)) {
4237                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4238                 snprintf(err_str, LNET_MAX_STR_LEN,
4239                          "Neither \"primary nid\" nor \"peer ni\" are defined");
4240                 goto failed;
4241         }
4242
4243         rc = yaml_nids2nidstr(peer_nis, &nidstr, prim_nidstr, cmd);
4244         if (rc == LUSTRE_CFG_RC_MISSING_PARAM) {
4245                 snprintf(err_str, LNET_MAX_STR_LEN,
4246                          "No nids defined in YAML block");
4247                 goto failed;
4248         } else if (rc == LUSTRE_CFG_RC_OUT_OF_MEM) {
4249                 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
4250                 goto failed;
4251         } else if (rc != LUSTRE_CFG_RC_NO_ERR) {
4252                 snprintf(err_str, LNET_MAX_STR_LEN,
4253                          "Unrecognized error %d", rc);
4254                 goto failed;
4255         }
4256
4257         num_nids = 0;
4258         if (nidstr) {
4259                 num_nids = lustre_lnet_parse_nidstr(nidstr, lnet_nidlist,
4260                                                     LNET_MAX_NIDS_PER_PEER,
4261                                                     err_str);
4262                 if (num_nids < 0) {
4263                         rc = num_nids;
4264                         goto failed;
4265                 }
4266         }
4267
4268         if (cmd == LNETCTL_ADD_CMD) {
4269                 mr = cYAML_get_object_item(tree, "Multi-Rail");
4270                 mr_value = true;
4271                 if (mr && mr->cy_valuestring) {
4272                         if (strcmp(mr->cy_valuestring, "False") == 0)
4273                                 mr_value = false;
4274                         else if (strcmp(mr->cy_valuestring, "True") != 0) {
4275                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
4276                                 snprintf(err_str, LNET_MAX_STR_LEN,
4277                                          "Multi-Rail must be set to \"True\" or \"False\" found \"%s\"",
4278                                          mr->cy_valuestring);
4279                                 goto failed;
4280                         }
4281                 }
4282
4283                 rc = lustre_lnet_config_peer_nidlist(prim_nidstr, lnet_nidlist,
4284                                                      num_nids, mr_value, seqn,
4285                                                      err_rc);
4286         } else
4287                 rc = lustre_lnet_del_peer_nidlist(prim_nidstr, lnet_nidlist,
4288                                                   num_nids, seqn, err_rc);
4289
4290 failed:
4291         if (nidstr)
4292                 free(nidstr);
4293
4294         if (rc != LUSTRE_CFG_RC_NO_ERR)
4295                 cYAML_build_error(rc, seqn, "peer",
4296                                   cmd == LNETCTL_ADD_CMD ? ADD_CMD : DEL_CMD,
4297                                   err_str, err_rc);
4298
4299         return rc;
4300 }
4301
4302 static int handle_yaml_config_peer(struct cYAML *tree, struct cYAML **show_rc,
4303                                    struct cYAML **err_rc)
4304 {
4305         return handle_yaml_peer_common(tree, show_rc, err_rc, LNETCTL_ADD_CMD);
4306 }
4307
4308 static int handle_yaml_del_peer(struct cYAML *tree, struct cYAML **show_rc,
4309                                 struct cYAML **err_rc)
4310 {
4311         return handle_yaml_peer_common(tree, show_rc, err_rc, LNETCTL_DEL_CMD);
4312 }
4313
4314 static int handle_yaml_config_buffers(struct cYAML *tree,
4315                                       struct cYAML **show_rc,
4316                                       struct cYAML **err_rc)
4317 {
4318         int rc;
4319         struct cYAML *tiny, *small, *large, *seq_no;
4320
4321         tiny = cYAML_get_object_item(tree, "tiny");
4322         small = cYAML_get_object_item(tree, "small");
4323         large = cYAML_get_object_item(tree, "large");
4324         seq_no = cYAML_get_object_item(tree, "seq_no");
4325
4326         rc = lustre_lnet_config_buffers((tiny) ? tiny->cy_valueint : -1,
4327                                         (small) ? small->cy_valueint : -1,
4328                                         (large) ? large->cy_valueint : -1,
4329                                         (seq_no) ? seq_no->cy_valueint : -1,
4330                                         err_rc);
4331
4332         return rc;
4333 }
4334
4335 static int handle_yaml_config_routing(struct cYAML *tree,
4336                                       struct cYAML **show_rc,
4337                                       struct cYAML **err_rc)
4338 {
4339         int rc = LUSTRE_CFG_RC_NO_ERR;
4340         struct cYAML *seq_no, *enable;
4341
4342         seq_no = cYAML_get_object_item(tree, "seq_no");
4343         enable = cYAML_get_object_item(tree, "enable");
4344
4345         if (enable) {
4346                 rc = lustre_lnet_enable_routing(enable->cy_valueint,
4347                                                 (seq_no) ?
4348                                                     seq_no->cy_valueint : -1,
4349                                                 err_rc);
4350         }
4351
4352         return rc;
4353 }
4354
4355 static int handle_yaml_del_route(struct cYAML *tree, struct cYAML **show_rc,
4356                                  struct cYAML **err_rc)
4357 {
4358         struct cYAML *net;
4359         struct cYAML *gw;
4360         struct cYAML *seq_no;
4361
4362         net = cYAML_get_object_item(tree, "net");
4363         gw = cYAML_get_object_item(tree, "gateway");
4364         seq_no = cYAML_get_object_item(tree, "seq_no");
4365
4366         return lustre_lnet_del_route((net) ? net->cy_valuestring : NULL,
4367                                      (gw) ? gw->cy_valuestring : NULL,
4368                                      (seq_no) ? seq_no->cy_valueint : -1,
4369                                      err_rc);
4370 }
4371
4372 static int handle_yaml_del_routing(struct cYAML *tree, struct cYAML **show_rc,
4373                                    struct cYAML **err_rc)
4374 {
4375         struct cYAML *seq_no;
4376
4377         seq_no = cYAML_get_object_item(tree, "seq_no");
4378
4379         return lustre_lnet_enable_routing(0, (seq_no) ?
4380                                                 seq_no->cy_valueint : -1,
4381                                         err_rc);
4382 }
4383
4384 static int handle_yaml_show_route(struct cYAML *tree, struct cYAML **show_rc,
4385                                   struct cYAML **err_rc)
4386 {
4387         struct cYAML *net;
4388         struct cYAML *gw;
4389         struct cYAML *hop;
4390         struct cYAML *prio;
4391         struct cYAML *detail;
4392         struct cYAML *seq_no;
4393
4394         net = cYAML_get_object_item(tree, "net");
4395         gw = cYAML_get_object_item(tree, "gateway");
4396         hop = cYAML_get_object_item(tree, "hop");
4397         prio = cYAML_get_object_item(tree, "priority");
4398         detail = cYAML_get_object_item(tree, "detail");
4399         seq_no = cYAML_get_object_item(tree, "seq_no");
4400
4401         return lustre_lnet_show_route((net) ? net->cy_valuestring : NULL,
4402                                       (gw) ? gw->cy_valuestring : NULL,
4403                                       (hop) ? hop->cy_valueint : -1,
4404                                       (prio) ? prio->cy_valueint : -1,
4405                                       (detail) ? detail->cy_valueint : 0,
4406                                       (seq_no) ? seq_no->cy_valueint : -1,
4407                                       show_rc, err_rc, false);
4408 }
4409
4410 static int handle_yaml_show_net(struct cYAML *tree, struct cYAML **show_rc,
4411                                 struct cYAML **err_rc)
4412 {
4413         struct cYAML *net, *detail, *seq_no;
4414
4415         net = cYAML_get_object_item(tree, "net");
4416         detail = cYAML_get_object_item(tree, "detail");
4417         seq_no = cYAML_get_object_item(tree, "seq_no");
4418
4419         return lustre_lnet_show_net((net) ? net->cy_valuestring : NULL,
4420                                     (detail) ? detail->cy_valueint : 0,
4421                                     (seq_no) ? seq_no->cy_valueint : -1,
4422                                     show_rc, err_rc, false);
4423 }
4424
4425 static int handle_yaml_show_routing(struct cYAML *tree, struct cYAML **show_rc,
4426                                     struct cYAML **err_rc)
4427 {
4428         struct cYAML *seq_no;
4429
4430         seq_no = cYAML_get_object_item(tree, "seq_no");
4431
4432         return lustre_lnet_show_routing((seq_no) ? seq_no->cy_valueint : -1,
4433                                         show_rc, err_rc, false);
4434 }
4435
4436 static int handle_yaml_show_peers(struct cYAML *tree, struct cYAML **show_rc,
4437                                   struct cYAML **err_rc)
4438 {
4439         struct cYAML *seq_no, *nid, *detail;
4440
4441         seq_no = cYAML_get_object_item(tree, "seq_no");
4442         detail = cYAML_get_object_item(tree, "detail");
4443         nid = cYAML_get_object_item(tree, "nid");
4444
4445         return lustre_lnet_show_peer((nid) ? nid->cy_valuestring : NULL,
4446                                      (detail) ? detail->cy_valueint : 0,
4447                                      (seq_no) ? seq_no->cy_valueint : -1,
4448                                      show_rc, err_rc, false);
4449 }
4450
4451 static int handle_yaml_show_stats(struct cYAML *tree, struct cYAML **show_rc,
4452                                   struct cYAML **err_rc)
4453 {
4454         struct cYAML *seq_no;
4455
4456         seq_no = cYAML_get_object_item(tree, "seq_no");
4457
4458         return lustre_lnet_show_stats((seq_no) ? seq_no->cy_valueint : -1,
4459                                       show_rc, err_rc);
4460 }
4461
4462 static int handle_yaml_config_numa(struct cYAML *tree, struct cYAML **show_rc,
4463                                   struct cYAML **err_rc)
4464 {
4465         struct cYAML *seq_no, *range;
4466
4467         seq_no = cYAML_get_object_item(tree, "seq_no");
4468         range = cYAML_get_object_item(tree, "range");
4469
4470         return lustre_lnet_config_numa_range(range ? range->cy_valueint : -1,
4471                                              seq_no ? seq_no->cy_valueint : -1,
4472                                              err_rc);
4473 }
4474
4475 static int handle_yaml_del_numa(struct cYAML *tree, struct cYAML **show_rc,
4476                                struct cYAML **err_rc)
4477 {
4478         struct cYAML *seq_no;
4479
4480         seq_no = cYAML_get_object_item(tree, "seq_no");
4481
4482         return lustre_lnet_config_numa_range(0, seq_no ? seq_no->cy_valueint : -1,
4483                                              err_rc);
4484 }
4485
4486 static int handle_yaml_show_numa(struct cYAML *tree, struct cYAML **show_rc,
4487                                 struct cYAML **err_rc)
4488 {
4489         struct cYAML *seq_no;
4490
4491         seq_no = cYAML_get_object_item(tree, "seq_no");
4492
4493         return lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint : -1,
4494                                            show_rc, err_rc);
4495 }
4496
4497 static int handle_yaml_config_global_settings(struct cYAML *tree,
4498                                               struct cYAML **show_rc,
4499                                               struct cYAML **err_rc)
4500 {
4501         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4502                      *sen, *recov, *rsen, *drop_asym_route;
4503         int rc = 0;
4504
4505         seq_no = cYAML_get_object_item(tree, "seq_no");
4506         max_intf = cYAML_get_object_item(tree, "max_intf");
4507         if (max_intf)
4508                 rc = lustre_lnet_config_max_intf(max_intf->cy_valueint,
4509                                                  seq_no ? seq_no->cy_valueint
4510                                                         : -1,
4511                                                  err_rc);
4512
4513         numa = cYAML_get_object_item(tree, "numa_range");
4514         if (numa)
4515                 rc = lustre_lnet_config_numa_range(numa->cy_valueint,
4516                                                    seq_no ? seq_no->cy_valueint
4517                                                         : -1,
4518                                                    err_rc);
4519
4520         discovery = cYAML_get_object_item(tree, "discovery");
4521         if (discovery)
4522                 rc = lustre_lnet_config_discovery(discovery->cy_valueint,
4523                                                   seq_no ? seq_no->cy_valueint
4524                                                         : -1,
4525                                                   err_rc);
4526
4527         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4528         if (drop_asym_route)
4529                 rc = lustre_lnet_config_drop_asym_route(
4530                         drop_asym_route->cy_valueint,
4531                         seq_no ? seq_no->cy_valueint : -1,
4532                         err_rc);
4533
4534         retry = cYAML_get_object_item(tree, "retry_count");
4535         if (retry)
4536                 rc = lustre_lnet_config_retry_count(retry->cy_valueint,
4537                                                     seq_no ? seq_no->cy_valueint
4538                                                         : -1,
4539                                                     err_rc);
4540
4541         tto = cYAML_get_object_item(tree, "transaction_timeout");
4542         if (tto)
4543                 rc = lustre_lnet_config_transaction_to(tto->cy_valueint,
4544                                                        seq_no ? seq_no->cy_valueint
4545                                                                 : -1,
4546                                                        err_rc);
4547
4548         sen = cYAML_get_object_item(tree, "health_sensitivity");
4549         if (sen)
4550                 rc = lustre_lnet_config_hsensitivity(sen->cy_valueint,
4551                                                      seq_no ? seq_no->cy_valueint
4552                                                         : -1,
4553                                                      err_rc);
4554
4555         recov = cYAML_get_object_item(tree, "recovery_interval");
4556         if (recov)
4557                 rc = lustre_lnet_config_recov_intrv(recov->cy_valueint,
4558                                                     seq_no ? seq_no->cy_valueint
4559                                                         : -1,
4560                                                     err_rc);
4561
4562         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4563         if (rsen)
4564                 rc = lustre_lnet_config_rtr_sensitivity(rsen->cy_valueint,
4565                                                      seq_no ? seq_no->cy_valueint
4566                                                         : -1,
4567                                                      err_rc);
4568
4569         return rc;
4570 }
4571
4572 static int handle_yaml_del_global_settings(struct cYAML *tree,
4573                                            struct cYAML **show_rc,
4574                                            struct cYAML **err_rc)
4575 {
4576         struct cYAML *max_intf, *numa, *discovery, *seq_no, *drop_asym_route;
4577         int rc = 0;
4578
4579         seq_no = cYAML_get_object_item(tree, "seq_no");
4580         max_intf = cYAML_get_object_item(tree, "max_intf");
4581         if (max_intf)
4582                 rc = lustre_lnet_config_max_intf(LNET_INTERFACES_MAX_DEFAULT,
4583                                                  seq_no ? seq_no->cy_valueint
4584                                                         : -1,
4585                                                  err_rc);
4586
4587         numa = cYAML_get_object_item(tree, "numa_range");
4588         if (numa)
4589                 rc = lustre_lnet_config_numa_range(0,
4590                                                    seq_no ? seq_no->cy_valueint
4591                                                         : -1,
4592                                                    err_rc);
4593
4594         /* peer discovery is enabled by default */
4595         discovery = cYAML_get_object_item(tree, "discovery");
4596         if (discovery)
4597                 rc = lustre_lnet_config_discovery(1,
4598                                                   seq_no ? seq_no->cy_valueint
4599                                                         : -1,
4600                                                   err_rc);
4601
4602         /* asymmetrical route messages are accepted by default */
4603         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4604         if (drop_asym_route)
4605                 rc = lustre_lnet_config_drop_asym_route(
4606                         0, seq_no ? seq_no->cy_valueint : -1, err_rc);
4607
4608         return rc;
4609 }
4610
4611 static int handle_yaml_show_global_settings(struct cYAML *tree,
4612                                             struct cYAML **show_rc,
4613                                             struct cYAML **err_rc)
4614 {
4615         struct cYAML *max_intf, *numa, *discovery, *retry, *tto, *seq_no,
4616                      *sen, *recov, *rsen, *drop_asym_route;
4617         int rc = 0;
4618
4619         seq_no = cYAML_get_object_item(tree, "seq_no");
4620         max_intf = cYAML_get_object_item(tree, "max_intf");
4621         if (max_intf)
4622                 rc = lustre_lnet_show_max_intf(seq_no ? seq_no->cy_valueint
4623                                                         : -1,
4624                                                 show_rc, err_rc);
4625
4626         numa = cYAML_get_object_item(tree, "numa_range");
4627         if (numa)
4628                 rc = lustre_lnet_show_numa_range(seq_no ? seq_no->cy_valueint
4629                                                         : -1,
4630                                                  show_rc, err_rc);
4631
4632         discovery = cYAML_get_object_item(tree, "discovery");
4633         if (discovery)
4634                 rc = lustre_lnet_show_discovery(seq_no ? seq_no->cy_valueint
4635                                                         : -1,
4636                                                 show_rc, err_rc);
4637
4638         drop_asym_route = cYAML_get_object_item(tree, "drop_asym_route");
4639         if (drop_asym_route)
4640                 rc = lustre_lnet_show_drop_asym_route(
4641                         seq_no ? seq_no->cy_valueint : -1,
4642                         show_rc, err_rc);
4643
4644         retry = cYAML_get_object_item(tree, "retry_count");
4645         if (retry)
4646                 rc = lustre_lnet_show_retry_count(seq_no ? seq_no->cy_valueint
4647                                                         : -1,
4648                                                   show_rc, err_rc);
4649
4650         tto = cYAML_get_object_item(tree, "transaction_timeout");
4651         if (tto)
4652                 rc = lustre_lnet_show_transaction_to(seq_no ? seq_no->cy_valueint
4653                                                         : -1,
4654                                                      show_rc, err_rc);
4655
4656         sen = cYAML_get_object_item(tree, "health_sensitivity");
4657         if (sen)
4658                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4659                                                         : -1,
4660                                                      show_rc, err_rc);
4661
4662         recov = cYAML_get_object_item(tree, "recovery_interval");
4663         if (recov)
4664                 rc = lustre_lnet_show_recov_intrv(seq_no ? seq_no->cy_valueint
4665                                                         : -1,
4666                                                   show_rc, err_rc);
4667
4668         rsen = cYAML_get_object_item(tree, "router_sensitivity");
4669         if (rsen)
4670                 rc = lustre_lnet_show_hsensitivity(seq_no ? seq_no->cy_valueint
4671                                                         : -1,
4672                                                      show_rc, err_rc);
4673
4674         return rc;
4675 }
4676
4677 static int handle_yaml_ping(struct cYAML *tree, struct cYAML **show_rc,
4678                             struct cYAML **err_rc)
4679 {
4680         struct cYAML *seq_no, *nid, *timeout;
4681
4682         seq_no = cYAML_get_object_item(tree, "seq_no");
4683         nid = cYAML_get_object_item(tree, "primary nid");
4684         timeout = cYAML_get_object_item(tree, "timeout");
4685
4686         return lustre_lnet_ping_nid((nid) ? nid->cy_valuestring : NULL,
4687                                     (timeout) ? timeout->cy_valueint : 1000,
4688                                     (seq_no) ? seq_no->cy_valueint : -1,
4689                                     show_rc, err_rc);
4690 }
4691
4692 static int handle_yaml_discover(struct cYAML *tree, struct cYAML **show_rc,
4693                                 struct cYAML **err_rc)
4694 {
4695         struct cYAML *seq_no, *nid, *force;
4696
4697         seq_no = cYAML_get_object_item(tree, "seq_no");
4698         nid = cYAML_get_object_item(tree, "primary nid");
4699         force = cYAML_get_object_item(tree, "force");
4700
4701         return lustre_lnet_discover_nid((nid) ? nid->cy_valuestring : NULL,
4702                                         (force) ? force->cy_valueint : 0,
4703                                         (seq_no) ? seq_no->cy_valueint : -1,
4704                                         show_rc, err_rc);
4705 }
4706
4707 static int handle_yaml_no_op()
4708 {
4709         return LUSTRE_CFG_RC_NO_ERR;
4710 }
4711
4712 struct lookup_cmd_hdlr_tbl {
4713         char *name;
4714         cmd_handler_t cb;
4715 };
4716
4717 static struct lookup_cmd_hdlr_tbl lookup_config_tbl[] = {
4718         { .name = "route",      .cb = handle_yaml_config_route },
4719         { .name = "net",        .cb = handle_yaml_config_ni },
4720         { .name = "ip2nets",    .cb = handle_yaml_config_ip2nets },
4721         { .name = "peer",       .cb = handle_yaml_config_peer },
4722         { .name = "routing",    .cb = handle_yaml_config_routing },
4723         { .name = "buffers",    .cb = handle_yaml_config_buffers },
4724         { .name = "statistics", .cb = handle_yaml_no_op },
4725         { .name = "global",     .cb = handle_yaml_config_global_settings},
4726         { .name = "numa",       .cb = handle_yaml_config_numa },
4727         { .name = "ping",       .cb = handle_yaml_no_op },
4728         { .name = "discover",   .cb = handle_yaml_no_op },
4729         { .name = NULL } };
4730
4731 static struct lookup_cmd_hdlr_tbl lookup_del_tbl[] = {
4732         { .name = "route",      .cb = handle_yaml_del_route },
4733         { .name = "net",        .cb = handle_yaml_del_ni },
4734         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4735         { .name = "peer",       .cb = handle_yaml_del_peer },
4736         { .name = "routing",    .cb = handle_yaml_del_routing },
4737         { .name = "buffers",    .cb = handle_yaml_no_op },
4738         { .name = "statistics", .cb = handle_yaml_no_op },
4739         { .name = "global",     .cb = handle_yaml_del_global_settings},
4740         { .name = "numa",       .cb = handle_yaml_del_numa },
4741         { .name = "ping",       .cb = handle_yaml_no_op },
4742         { .name = "discover",   .cb = handle_yaml_no_op },
4743         { .name = NULL } };
4744
4745 static struct lookup_cmd_hdlr_tbl lookup_show_tbl[] = {
4746         { .name = "route",      .cb = handle_yaml_show_route },
4747         { .name = "net",        .cb = handle_yaml_show_net },
4748         { .name = "peer",       .cb = handle_yaml_show_peers },
4749         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4750         { .name = "routing",    .cb = handle_yaml_show_routing },
4751         { .name = "buffers",    .cb = handle_yaml_show_routing },
4752         { .name = "statistics", .cb = handle_yaml_show_stats },
4753         { .name = "global",     .cb = handle_yaml_show_global_settings},
4754         { .name = "numa",       .cb = handle_yaml_show_numa },
4755         { .name = "ping",       .cb = handle_yaml_no_op },
4756         { .name = "discover",   .cb = handle_yaml_no_op },
4757         { .name = NULL } };
4758
4759 static struct lookup_cmd_hdlr_tbl lookup_exec_tbl[] = {
4760         { .name = "route",      .cb = handle_yaml_no_op },
4761         { .name = "net",        .cb = handle_yaml_no_op },
4762         { .name = "peer",       .cb = handle_yaml_no_op },
4763         { .name = "ip2nets",    .cb = handle_yaml_no_op },
4764         { .name = "routing",    .cb = handle_yaml_no_op },
4765         { .name = "buffers",    .cb = handle_yaml_no_op },
4766         { .name = "statistics", .cb = handle_yaml_no_op },
4767         { .name = "global",     .cb = handle_yaml_no_op },
4768         { .name = "numa",       .cb = handle_yaml_no_op },
4769         { .name = "ping",       .cb = handle_yaml_ping },
4770         { .name = "discover",   .cb = handle_yaml_discover },
4771         { .name = NULL } };
4772
4773 static cmd_handler_t lookup_fn(char *key,
4774                                struct lookup_cmd_hdlr_tbl *tbl)
4775 {
4776         int i;
4777         if (key == NULL)
4778                 return NULL;
4779
4780         for (i = 0; tbl[i].name != NULL; i++) {
4781                 if (strncmp(key, tbl[i].name, strlen(tbl[i].name)) == 0)
4782                         return tbl[i].cb;
4783         }
4784
4785         return NULL;
4786 }
4787
4788 static int lustre_yaml_cb_helper(char *f, struct lookup_cmd_hdlr_tbl *table,
4789                                  struct cYAML **show_rc, struct cYAML **err_rc)
4790 {
4791         struct cYAML *tree, *item = NULL, *head, *child;
4792         cmd_handler_t cb;
4793         char err_str[LNET_MAX_STR_LEN];
4794         int rc = LUSTRE_CFG_RC_NO_ERR, return_rc = LUSTRE_CFG_RC_NO_ERR;
4795
4796         tree = cYAML_build_tree(f, NULL, 0, err_rc, false);
4797         if (tree == NULL)
4798                 return LUSTRE_CFG_RC_BAD_PARAM;
4799
4800         child = tree->cy_child;
4801         while (child != NULL) {
4802                 cb = lookup_fn(child->cy_string, table);
4803                 if (cb == NULL) {
4804                         snprintf(err_str, sizeof(err_str),
4805                                 "\"call back for '%s' not found\"",
4806                                 child->cy_string);
4807                         cYAML_build_error(LUSTRE_CFG_RC_BAD_PARAM, -1,
4808                                         "yaml", "helper", err_str, err_rc);
4809                         goto out;
4810                 }
4811
4812                 if (cYAML_is_sequence(child)) {
4813                         while ((head = cYAML_get_next_seq_item(child, &item))
4814                                != NULL) {
4815                                 rc = cb(head, show_rc, err_rc);
4816                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
4817                                         return_rc = rc;
4818                         }
4819                 } else {
4820                         rc = cb(child, show_rc, err_rc);
4821                         if (rc != LUSTRE_CFG_RC_NO_ERR)
4822                                 return_rc = rc;
4823                 }
4824                 item = NULL;
4825                 child = child->cy_next;
4826         }
4827
4828 out:
4829         cYAML_free_tree(tree);
4830
4831         return return_rc;
4832 }
4833
4834 int lustre_yaml_config(char *f, struct cYAML **err_rc)
4835 {
4836         return lustre_yaml_cb_helper(f, lookup_config_tbl,
4837                                      NULL, err_rc);
4838 }
4839
4840 int lustre_yaml_del(char *f, struct cYAML **err_rc)
4841 {
4842         return lustre_yaml_cb_helper(f, lookup_del_tbl,
4843                                      NULL, err_rc);
4844 }
4845
4846 int lustre_yaml_show(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4847 {
4848         return lustre_yaml_cb_helper(f, lookup_show_tbl,
4849                                      show_rc, err_rc);
4850 }
4851
4852 int lustre_yaml_exec(char *f, struct cYAML **show_rc, struct cYAML **err_rc)
4853 {
4854         return lustre_yaml_cb_helper(f, lookup_exec_tbl,
4855                                      show_rc, err_rc);
4856 }