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