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