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