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