Whamcloud - gitweb
LU-14875 import: fix bad CPT read
[fs/lustre-release.git] / lnet / utils / lnetconfig / liblnetconfig.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of the
9  * License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * LGPL HEADER END
20  *
21  * Copyright (c) 2014, 2017, Intel Corporation.
22  *
23  * Author:
24  *   Amir Shehata <amir.shehata@intel.com>
25  */
26
27 /*
28  * There are two APIs:
29  *  1. APIs that take the actual parameters expanded.  This is for other
30  *  entities that would like to link against the library and call the APIs
31  *  directly without having to form an intermediate representation.
32  *  2. APIs that take a YAML file and parses out the information there and
33  *  calls the APIs mentioned in 1
34  */
35
36 #include <errno.h>
37 #include <limits.h>
38 #include <byteswap.h>
39 #include <netdb.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <net/if.h>
46 #include <libcfs/util/ioctl.h>
47 #include <linux/lnet/lnetctl.h>
48 #include "liblnd.h"
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <ifaddrs.h>
53 #include <rdma/rdma_user_cm.h>
54 #include "liblnetconfig.h"
55 #include <glob.h>
56 #include <libcfs/util/param.h>
57
58 #ifndef HAVE_USRSPC_RDMA_PS_TCP
59 #define RDMA_PS_TCP 0x0106
60 #endif
61
62 const char *gmsg_stat_names[] = {"sent_stats", "received_stats",
63                                  "dropped_stats"};
64
65 /*
66  * lustre_lnet_ip_range_descr
67  *      Describes an IP range.
68  *      Each octect is an expression
69  */
70 struct lustre_lnet_ip_range_descr {
71         struct list_head ipr_entry;
72         struct list_head ipr_expr;
73 };
74
75 /*
76  * lustre_lnet_ip2nets
77  *      Describes an ip2nets rule. This can be on a list of rules.
78  */
79 struct lustre_lnet_ip2nets {
80         struct lnet_dlc_network_descr ip2nets_net;
81         struct list_head ip2nets_ip_ranges;
82 };
83
84 int open_sysfs_file(const char *path, const char *attr, const int mode)
85 {
86         int fd;
87         char filename[LNET_MAX_STR_LEN];
88
89         if (strlen(path) + strlen(attr) >= LNET_MAX_STR_LEN)
90                 return -1;
91
92         snprintf(filename, sizeof(filename), "%s%s",
93                  path, attr);
94
95         fd = open(filename, mode);
96
97         return fd;
98 }
99
100 static int read_sysfs_file(const char *path, const char *attr,
101                            void *val, const size_t size, const int nelem)
102 {
103         int fd;
104         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
105
106         fd = open_sysfs_file(path, attr, O_RDONLY);
107         if (fd == -1)
108                 return LUSTRE_CFG_RC_NO_MATCH;
109
110         if (read(fd, val, size * nelem) == -1)
111                 goto close_fd;
112
113         rc = LUSTRE_CFG_RC_NO_ERR;
114
115 close_fd:
116         close(fd);
117         return rc;
118 }
119
120 static int write_sysfs_file(const char *path, const char *attr,
121                             void *val, const size_t size, const int nelem)
122 {
123         int fd;
124         int rc = LUSTRE_CFG_RC_GENERIC_ERR;
125
126         fd = open_sysfs_file(path, attr, O_WRONLY | O_TRUNC);
127         if (fd == -1)
128                 return LUSTRE_CFG_RC_NO_MATCH;
129
130         if (write(fd, val, size * nelem) == -1)
131                 goto close_fd;
132
133         rc = LUSTRE_CFG_RC_NO_ERR;
134
135 close_fd:
136         close(fd);
137         return rc;
138 }
139
140 /*
141  * free_intf_descr
142  *      frees the memory allocated for an intf descriptor.
143  */
144 void free_intf_descr(struct lnet_dlc_intf_descr *intf_descr)
145 {
146         if (!intf_descr)
147                 return;
148
149         if (intf_descr->cpt_expr != NULL)
150                 cfs_expr_list_free(intf_descr->cpt_expr);
151         free(intf_descr);
152 }
153
154 /*
155  * lustre_lnet_add_ip_range
156  * Formatting:
157  *      given a string of the format:
158  *      <expr.expr.expr.expr> parse each expr into
159  *      a lustre_lnet_ip_range_descr structure and insert on the list.
160  *
161  *      This function is called from
162  *              YAML on each ip-range.
163  *              As a result of lnetctl command
164  *              When building a NID or P2P selection rules
165  */
166 int lustre_lnet_add_ip_range(struct list_head *list, char *str_ip_range)
167 {
168         struct lustre_lnet_ip_range_descr *ip_range;
169         int rc;
170
171         ip_range = calloc(1, sizeof(*ip_range));
172         if (ip_range == NULL)
173                 return LUSTRE_CFG_RC_OUT_OF_MEM;
174
175         INIT_LIST_HEAD(&ip_range->ipr_entry);
176         INIT_LIST_HEAD(&ip_range->ipr_expr);
177
178         rc = cfs_ip_addr_parse(str_ip_range, strlen(str_ip_range),
179                                &ip_range->ipr_expr);
180         if (rc != 0)
181                 return LUSTRE_CFG_RC_BAD_PARAM;
182
183         list_add_tail(&ip_range->ipr_entry, list);
184
185         return LUSTRE_CFG_RC_NO_ERR;
186 }
187
188 int lustre_lnet_add_intf_descr(struct list_head *list, char *intf, int len)
189 {
190         char *open_sq_bracket = NULL, *close_sq_bracket = NULL,
191              *intf_name;
192         struct lnet_dlc_intf_descr *intf_descr = NULL;
193         int rc;
194         char intf_string[LNET_MAX_STR_LEN];
195
196         if (len >= LNET_MAX_STR_LEN)
197                 return LUSTRE_CFG_RC_BAD_PARAM;
198
199         strncpy(intf_string, intf, len);
200         intf_string[len] = '\0';
201
202         intf_descr = calloc(1, sizeof(*intf_descr));
203         if (intf_descr == NULL)
204                 return LUSTRE_CFG_RC_OUT_OF_MEM;
205
206         INIT_LIST_HEAD(&intf_descr->intf_on_network);
207
208         intf_name = intf_string;
209         open_sq_bracket = strchr(intf_string, '[');
210         if (open_sq_bracket != NULL) {
211                 close_sq_bracket = strchr(intf_string, ']');
212                 if (close_sq_bracket == NULL) {
213                         free(intf_descr);
214                         return LUSTRE_CFG_RC_BAD_PARAM;
215                 }
216                 rc = cfs_expr_list_parse(open_sq_bracket,
217                                          strlen(open_sq_bracket), 0, UINT_MAX,
218                                          &intf_descr->cpt_expr);
219                 if (rc < 0) {
220                         free(intf_descr);
221                         return LUSTRE_CFG_RC_BAD_PARAM;
222                 }
223                 strncpy(intf_descr->intf_name, intf_name,
224                         open_sq_bracket - intf_name);
225                 intf_descr->intf_name[open_sq_bracket - intf_name] = '\0';
226         } else {
227                 strcpy(intf_descr->intf_name, intf_name);
228                 intf_descr->cpt_expr = NULL;
229         }
230
231         list_add_tail(&intf_descr->intf_on_network, list);
232
233         return LUSTRE_CFG_RC_NO_ERR;
234 }
235
236 void lustre_lnet_init_nw_descr(struct lnet_dlc_network_descr *nw_descr)
237 {
238         if (nw_descr != NULL) {
239                 nw_descr->nw_id = 0;
240                 INIT_LIST_HEAD(&nw_descr->network_on_rule);
241                 INIT_LIST_HEAD(&nw_descr->nw_intflist);
242         }
243 }
244
245 int lustre_lnet_parse_nidstr(char *nidstr, lnet_nid_t *lnet_nidlist,
246                              int max_nids, char *err_str)
247 {
248         int rc, num_nids = 0;
249         struct list_head nidlist;
250
251         if (!nidstr) {
252                 snprintf(err_str, LNET_MAX_STR_LEN, "supplied nidstr is NULL");
253                 return LUSTRE_CFG_RC_BAD_PARAM;
254         }
255
256         if (strchr(nidstr, '*')) {
257                 snprintf(err_str, LNET_MAX_STR_LEN,
258                          "asterisk not allowed in nidstring \"%s\"", nidstr);
259                 return LUSTRE_CFG_RC_BAD_PARAM;
260         }
261
262         INIT_LIST_HEAD(&nidlist);
263         rc = cfs_parse_nidlist(nidstr, strlen(nidstr), &nidlist);
264         if (rc == 0) {
265                 snprintf(err_str, LNET_MAX_STR_LEN,
266                          "Unable to parse nidlist from: %s\n", nidstr);
267                 return LUSTRE_CFG_RC_BAD_PARAM;
268         }
269
270         if (list_empty(&nidlist)) {
271                 snprintf(err_str, LNET_MAX_STR_LEN,
272                          "\"%s\" does not specify any valid nid lists", nidstr);
273                 return LUSTRE_CFG_RC_BAD_PARAM;
274         }
275
276         num_nids = cfs_expand_nidlist(&nidlist, lnet_nidlist, max_nids);
277         cfs_free_nidlist(&nidlist);
278
279         if (num_nids == -1) {
280                 snprintf(err_str, LNET_MAX_STR_LEN,
281                          "\"%s\" specifies more than the %d NIDs allowed by this operation.",
282                          nidstr, max_nids);
283                 return LUSTRE_CFG_RC_BAD_PARAM;
284         }
285
286         if (num_nids < 0) {
287                 snprintf(err_str, LNET_MAX_STR_LEN,
288                          "Failed to expand nidstr: %s", strerror(num_nids));
289                 return LUSTRE_CFG_RC_OUT_OF_MEM;
290         }
291
292         if (num_nids == 0) {
293                 snprintf(err_str, LNET_MAX_STR_LEN,
294                          "\"%s\" did not expand to any nids", nidstr);
295                 return LUSTRE_CFG_RC_BAD_PARAM;
296         }
297
298         return num_nids;
299 }
300
301 /*
302  * format expected:
303  *      <intf>[<expr>], <intf>[<expr>],..
304  */
305 int lustre_lnet_parse_interfaces(char *intf_str,
306                                  struct lnet_dlc_network_descr *nw_descr)
307 {
308         char *open_square;
309         char *close_square;
310         char *comma;
311         char *cur = intf_str, *next = NULL;
312         char *end = intf_str + strlen(intf_str);
313         int rc, len;
314         struct lnet_dlc_intf_descr *intf_descr, *tmp;
315
316         if (nw_descr == NULL)
317                 return LUSTRE_CFG_RC_BAD_PARAM;
318
319         while (cur < end) {
320                 open_square = strchr(cur, '[');
321                 if (open_square != NULL) {
322                         close_square = strchr(cur, ']');
323                         if (close_square == NULL) {
324                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
325                                 goto failed;
326                         }
327
328                         comma = strchr(cur, ',');
329                         if (comma != NULL && comma > close_square) {
330                                 next = comma + 1;
331                                 len = next - close_square;
332                         } else {
333                                 len = strlen(cur);
334                                 next = cur + len;
335                         }
336                 } else {
337                         comma = strchr(cur, ',');
338                         if (comma != NULL) {
339                                 next = comma + 1;
340                                 len = comma - cur;
341                         } else {
342                                 len = strlen(cur);
343                                 next = cur + len;
344                         }
345                 }
346
347                 rc = lustre_lnet_add_intf_descr(&nw_descr->nw_intflist, cur, len);
348                 if (rc != LUSTRE_CFG_RC_NO_ERR)
349                         goto failed;
350
351                 cur = next;
352         }
353
354         return LUSTRE_CFG_RC_NO_ERR;
355
356 failed:
357         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
358                                  intf_on_network) {
359                 list_del(&intf_descr->intf_on_network);
360                 free_intf_descr(intf_descr);
361         }
362
363         return rc;
364 }
365
366 int lustre_lnet_config_lib_init(void)
367 {
368         return register_ioc_dev(LNET_DEV_ID, LNET_DEV_PATH);
369 }
370
371 void lustre_lnet_config_lib_uninit(void)
372 {
373         unregister_ioc_dev(LNET_DEV_ID);
374 }
375
376 int lustre_lnet_config_ni_system(bool up, bool load_ni_from_mod,
377                                  int seq_no, struct cYAML **err_rc)
378 {
379         struct libcfs_ioctl_data data;
380         unsigned int opc;
381         int rc;
382         char err_str[LNET_MAX_STR_LEN] = "\"Success\"";
383
384         LIBCFS_IOC_INIT(data);
385
386         /* Reverse logic is used here in order not to change
387          * the lctl utility */
388         data.ioc_flags = load_ni_from_mod ? 0 : 1;
389
390         opc = up ? IOC_LIBCFS_CONFIGURE : IOC_LIBCFS_UNCONFIGURE;
391
392         rc = l_ioctl(LNET_DEV_ID, opc, &data);
393         if (rc != 0) {
394                 snprintf(err_str,
395                         sizeof(err_str),
396                         "\"LNet %s error: %s\"", (up) ? "configure" :
397                         "unconfigure", strerror(errno));
398                 rc = -errno;
399         }
400
401         cYAML_build_error(rc, seq_no, (up) ? CONFIG_CMD : UNCONFIG_CMD,
402                           "lnet", err_str, err_rc);
403
404         return rc;
405 }
406
407 static int dispatch_peer_ni_cmd(__u32 cmd, struct lnet_ioctl_peer_cfg *data,
408                                 char *err_str, char *cmd_str)
409 {
410         int rc;
411
412         rc = l_ioctl(LNET_DEV_ID, cmd, data);
413         if (rc) {
414                 rc = -errno;
415                 snprintf(err_str, LNET_MAX_STR_LEN,
416                          "\"%s peer ni operation failed: %s\"",
417                          cmd_str, strerror(errno));
418         }
419
420         return rc;
421 }
422
423 static int infra_ping_nid(char *ping_nids, char *src_nidstr, char *oper,
424                           int param, int ioc_call, int seq_no,
425                           struct cYAML **show_rc, struct cYAML **err_rc)
426 {
427         void *data = NULL;
428         struct lnet_ioctl_ping_data ping;
429         struct cYAML *root = NULL, *ping_node = NULL, *item = NULL,
430                      *first_seq = NULL, *tmp = NULL, *peer_ni = NULL;
431         struct lnet_process_id id;
432         char err_str[LNET_MAX_STR_LEN] = {0};
433         char *sep, *token, *end;
434         char buf[6];
435         size_t len;
436         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
437         int i;
438         bool flag = false;
439         lnet_nid_t src;
440
441         len = (sizeof(struct lnet_process_id) * LNET_INTERFACES_MAX_DEFAULT);
442
443         data = calloc(1, len);
444         if (data == NULL)
445                 goto out;
446
447         /* create struct cYAML root object */
448         root = cYAML_create_object(NULL, NULL);
449         if (root == NULL)
450                 goto out;
451
452         ping_node = cYAML_create_seq(root, oper);
453         if (ping_node == NULL)
454                 goto out;
455
456         if (src_nidstr) {
457                 src = libcfs_str2nid(src_nidstr);
458                 if (src == LNET_NID_ANY) {
459                         snprintf(err_str, sizeof(err_str),
460                                  "\"cannot parse source NID '%s'\"",
461                                  src_nidstr);
462                         rc = LUSTRE_CFG_RC_BAD_PARAM;
463                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
464                                           oper, err_str, err_rc);
465                         goto out;
466                 }
467         } else {
468                 src = LNET_NID_ANY;
469         }
470
471         /* tokenise each nid in string ping_nids */
472         token = strtok(ping_nids, ",");
473
474         do {
475                 item = cYAML_create_seq_item(ping_node);
476                 if (item == NULL)
477                         goto out;
478
479                 if (first_seq == NULL)
480                         first_seq = item;
481
482                 /* check if '-' is a part of NID, token */
483                 sep = strchr(token, '-');
484                 if (sep == NULL) {
485                         id.pid = LNET_PID_ANY;
486                         /* if no net is specified, libcfs_str2nid() will assume tcp */
487                         id.nid = libcfs_str2nid(token);
488                         if (id.nid == LNET_NID_ANY) {
489                                 snprintf(err_str, sizeof(err_str),
490                                          "\"cannot parse NID '%s'\"",
491                                          token);
492                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
493                                 cYAML_build_error(rc, seq_no, MANAGE_CMD,
494                                                   oper, err_str, err_rc);
495                                 continue;
496                         }
497                 } else {
498                         if (token[0] == 'u' || token[0] == 'U')
499                                 id.pid = (strtoul(&token[1], &end, 0) |
500                                           (LNET_PID_USERFLAG));
501                         else
502                                 id.pid = strtoul(token, &end, 0);
503
504                         /* assuming '-' is part of hostname */
505                         if (end != sep) {
506                                 id.pid = LNET_PID_ANY;
507                                 id.nid = libcfs_str2nid(token);
508                                 if (id.nid == LNET_NID_ANY) {
509                                         snprintf(err_str, sizeof(err_str),
510                                                  "\"cannot parse NID '%s'\"",
511                                                  token);
512                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
513                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
514                                                           oper, err_str,
515                                                           err_rc);
516                                         continue;
517                                 }
518                         } else {
519                                 id.nid = libcfs_str2nid(sep + 1);
520                                 if (id.nid == LNET_NID_ANY) {
521                                         snprintf(err_str, sizeof(err_str),
522                                                  "\"cannot parse NID '%s'\"",
523                                                  token);
524                                         rc = LUSTRE_CFG_RC_BAD_PARAM;
525                                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
526                                                           oper, err_str,
527                                                           err_rc);
528                                         continue;
529                                 }
530                         }
531                 }
532                 LIBCFS_IOC_INIT_V2(ping, ping_hdr);
533                 ping.ping_hdr.ioc_len = sizeof(ping);
534                 ping.ping_id          = id;
535                 ping.ping_src         = src;
536                 ping.op_param         = param;
537                 ping.ping_count       = LNET_INTERFACES_MAX_DEFAULT;
538                 ping.ping_buf         = data;
539
540                 rc = l_ioctl(LNET_DEV_ID, ioc_call, &ping);
541                 if (rc != 0) {
542                         snprintf(err_str,
543                                  sizeof(err_str), "failed to %s %s: %s\n", oper,
544                                  id.pid == LNET_PID_ANY ?
545                                  libcfs_nid2str(id.nid) :
546                                  libcfs_id2str(id), strerror(errno));
547                         rc = LUSTRE_CFG_RC_BAD_PARAM;
548                         cYAML_build_error(rc, seq_no, MANAGE_CMD,
549                                           oper, err_str, err_rc);
550                         continue;
551                 }
552
553                 if (cYAML_create_string(item, "primary nid",
554                                         libcfs_nid2str(ping.ping_id.nid)) == NULL)
555                         goto out;
556
557                 if (cYAML_create_string(item, "Multi-Rail", ping.mr_info ?
558                                         "True" : "False") == NULL)
559                         goto out;
560
561                 tmp = cYAML_create_seq(item, "peer ni");
562                 if (tmp == NULL)
563                         goto out;
564
565                 for (i = 0; i < ping.ping_count; i++) {
566                         if (ping.ping_buf[i].nid == LNET_NID_LO_0)
567                                 continue;
568                         peer_ni = cYAML_create_seq_item(tmp);
569                         if (peer_ni == NULL)
570                                 goto out;
571                         memset(buf, 0, sizeof buf);
572                         snprintf(buf, sizeof buf, "nid");
573                         if (cYAML_create_string(peer_ni, buf,
574                                                 libcfs_nid2str(ping.ping_buf[i].nid)) == NULL)
575                                 goto out;
576                 }
577
578                 flag = true;
579
580         } while ((token = strtok(NULL, ",")) != NULL);
581
582         if (flag)
583                 rc = LUSTRE_CFG_RC_NO_ERR;
584
585 out:
586         if (data)
587                 free(data);
588         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR) {
589                 cYAML_free_tree(root);
590         } else if (show_rc != NULL && *show_rc != NULL) {
591                 struct cYAML *show_node;
592                 show_node = cYAML_get_object_item(*show_rc, oper);
593                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
594                         cYAML_insert_child(show_node, first_seq);
595                         free(ping_node);
596                         free(root);
597                 } else if (show_node == NULL) {
598                         cYAML_insert_sibling((*show_rc)->cy_child,
599                                              ping_node);
600                         free(root);
601                 } else {
602                         cYAML_free_tree(root);
603                 }
604         } else {
605                 *show_rc = root;
606         }
607
608         return rc;
609 }
610
611 int lustre_lnet_ping_nid(char *ping_nids, char *src_nidstr, int timeout,
612                          int seq_no, struct cYAML **show_rc,
613                          struct cYAML **err_rc)
614 {
615         int rc;
616
617         rc = infra_ping_nid(ping_nids, src_nidstr, "ping", timeout,
618                             IOC_LIBCFS_PING_PEER, seq_no, show_rc, err_rc);
619         return rc;
620 }
621
622 int lustre_lnet_discover_nid(char *ping_nids, int force, int seq_no,
623                          struct cYAML **show_rc, struct cYAML **err_rc)
624 {
625         int rc;
626
627         rc = infra_ping_nid(ping_nids, NULL, "discover", force,
628                             IOC_LIBCFS_DISCOVER, seq_no, show_rc, err_rc);
629         return rc;
630 }
631
632 static int lustre_lnet_handle_peer_nidlist(lnet_nid_t *nidlist, int num_nids,
633                                            bool is_mr, __u32 cmd,
634                                            char *cmd_type, char *err_str)
635 {
636         struct lnet_ioctl_peer_cfg data;
637         int rc, nid_idx;
638
639         if (cmd == IOC_LIBCFS_ADD_PEER_NI) {
640                 /* When adding a peer we first need to create the peer using the
641                  * specified (or implied) primary nid. Then we can add
642                  * additional nids to this peer using the primary nid as a key
643                  */
644                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
645                 data.prcfg_mr = is_mr;
646                 data.prcfg_prim_nid = nidlist[0];
647                 data.prcfg_cfg_nid = LNET_NID_ANY;
648
649                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
650
651                 if (rc)
652                         return rc;
653         }
654
655         /* Add or delete any specified NIs associated with the specified
656          * (or implied) primary nid
657          */
658         for (nid_idx = 1; nid_idx < num_nids; nid_idx++) {
659                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
660                 data.prcfg_mr = is_mr;
661                 data.prcfg_prim_nid = nidlist[0];
662                 data.prcfg_cfg_nid = nidlist[nid_idx];
663
664                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
665
666                 if (rc)
667                         return rc;
668         }
669
670         if (cmd == IOC_LIBCFS_DEL_PEER_NI && num_nids == 1) {
671                 /* In the delete case we may have been given just the
672                  * primary nid of the peer. This tells us to delete the peer
673                  * completely (rather than just delete some of its NIs)
674                  */
675                 LIBCFS_IOC_INIT_V2(data, prcfg_hdr);
676                 data.prcfg_prim_nid = nidlist[0];
677                 data.prcfg_cfg_nid = LNET_NID_ANY;
678
679                 rc = dispatch_peer_ni_cmd(cmd, &data, err_str, cmd_type);
680         }
681
682         return rc;
683 }
684
685 static int
686 lustre_lnet_mod_peer_nidlist(lnet_nid_t pnid, lnet_nid_t *lnet_nidlist,
687                              int cmd, int num_nids, bool is_mr, int seq_no,
688                              struct cYAML **err_rc)
689 {
690         int rc = LUSTRE_CFG_RC_NO_ERR;
691         char err_str[LNET_MAX_STR_LEN];
692         lnet_nid_t *lnet_nidlist2 = NULL;
693         int ioc_cmd = (cmd == LNETCTL_ADD_CMD) ? IOC_LIBCFS_ADD_PEER_NI :
694                 IOC_LIBCFS_DEL_PEER_NI;
695         char *cmd_str = (cmd == LNETCTL_ADD_CMD) ? ADD_CMD : DEL_CMD;
696
697         num_nids++;
698         lnet_nidlist2 = calloc(sizeof(*lnet_nidlist2), num_nids);
699         if (!lnet_nidlist2) {
700                 snprintf(err_str, LNET_MAX_STR_LEN, "out of memory");
701                 rc = LUSTRE_CFG_RC_OUT_OF_MEM;
702                 goto out;
703         }
704         lnet_nidlist2[0] = pnid;
705         memcpy(&lnet_nidlist2[1], lnet_nidlist, sizeof(*lnet_nidlist) *
706                                                 (num_nids - 1));
707
708         rc = lustre_lnet_handle_peer_nidlist(lnet_nidlist2,
709                                              num_nids, is_mr, ioc_cmd,
710                                              cmd_str, err_str);
711 out:
712         if (lnet_nidlist2)
713                 free(lnet_nidlist2);
714
715         cYAML_build_error(rc, seq_no, cmd_str, "peer_ni", err_str, err_rc);
716         return rc;
717 }
718
719 static void
720 replace_sep(char *str, char sep, char newsep)
721 {
722         int bracket = 0;
723         int i;
724         if (!str)
725                 return;
726         for (i = 0; i < strlen(str); i++) {
727                 /* don't replace ',' within [] */
728                 if (str[i] == '[')
729                         bracket++;
730                 else if (str[i] == ']')
731                         bracket--;
732                 else if (str[i] == sep && bracket == 0)
733                         str[i] = newsep;
734         }
735 }
736
737 int lustre_lnet_modify_peer(char *prim_nid, char *nids, bool is_mr,
738                             int cmd, int seq_no, struct cYAML **err_rc)
739 {
740         int num_nids, rc;
741         char err_str[LNET_MAX_STR_LEN] = "Error";
742         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
743         lnet_nid_t pnid = LNET_NID_ANY;
744
745         if (!prim_nid) {
746                 rc = LUSTRE_CFG_RC_BAD_PARAM;
747                 snprintf(err_str, LNET_MAX_STR_LEN,
748                          "--prim_nid must be specified");
749                 goto out;
750         }
751
752         pnid = libcfs_str2nid(prim_nid);
753         if (pnid == LNET_NID_ANY) {
754                 rc = LUSTRE_CFG_RC_BAD_PARAM;
755                 snprintf(err_str, LNET_MAX_STR_LEN,
756                         "badly formatted primary NID: %s", prim_nid);
757                 goto out;
758         }
759
760         num_nids = 0;
761         if (nids) {
762                 /*
763                 * if there is no primary nid we need to make the first nid in the
764                 * nids list the primary nid
765                 */
766                 replace_sep(nids, ',', ' ');
767                 rc = lustre_lnet_parse_nidstr(nids, lnet_nidlist,
768                                         LNET_MAX_NIDS_PER_PEER, err_str);
769                 if (rc < 0)
770                         goto out;
771
772                 num_nids = rc;
773         }
774
775         rc = lustre_lnet_mod_peer_nidlist(pnid, lnet_nidlist,
776                                           cmd, num_nids, is_mr,
777                                           -1, err_rc);
778
779 out:
780         if (rc != LUSTRE_CFG_RC_NO_ERR)
781                 cYAML_build_error(rc, -1, "peer",
782                                 cmd == LNETCTL_ADD_CMD ? "add" : "del",
783                                 err_str, err_rc);
784
785         return rc;
786 }
787
788 int lustre_lnet_route_common(char *nw, char *nidstr, int hops, int prio,
789                              int sen, int seq_no, struct cYAML **err_rc,
790                              int cmd)
791 {
792         int rc, num_nids, idx;
793         __u32 rnet;
794         char err_str[LNET_MAX_STR_LEN] = "\"generic error\"";
795         struct lnet_ioctl_config_data data;
796         lnet_nid_t lnet_nidlist[LNET_MAX_NIDS_PER_PEER];
797
798         if (nw == NULL || nidstr == NULL) {
799                 snprintf(err_str, LNET_MAX_STR_LEN,
800                          "\"missing mandatory parameter:'%s'\"",
801                          (nw == NULL && nidstr == NULL) ? "network, gateway" :
802                          (nw == NULL) ? "network" : "gateway");
803                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
804                 goto out;
805         }
806
807         rnet = libcfs_str2net(nw);
808         if (rnet == LNET_NET_ANY) {
809                 snprintf(err_str, LNET_MAX_STR_LEN,
810                          "\"cannot parse remote net %s\"", nw);
811                 rc = LUSTRE_CFG_RC_BAD_PARAM;
812                 goto out;
813         }
814
815         replace_sep(nidstr, ',', ' ');
816         rc = lustre_lnet_parse_nidstr(nidstr, lnet_nidlist,
817                                       LNET_MAX_NIDS_PER_PEER, err_str);
818         if (rc < 0)
819                 goto out;
820
821         num_nids = rc;
822
823         for (idx = 0; idx < num_nids; idx++) {
824                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
825                 data.cfg_net = rnet;
826                 if (cmd == LNETCTL_ADD_CMD) {
827                         data.cfg_config_u.cfg_route.rtr_hop = hops;
828                         data.cfg_config_u.cfg_route.rtr_priority = prio;
829                         data.cfg_config_u.cfg_route.rtr_sensitivity = sen;
830                 }
831
832                 data.cfg_nid = lnet_nidlist[idx];
833
834                 if (cmd == LNETCTL_ADD_CMD)
835                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_ROUTE,
836                                         &data);
837                 else
838                         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_ROUTE,
839                                         &data);
840
841                 if (rc != 0 && errno != EEXIST &&
842                         errno != EHOSTUNREACH) {
843                         rc = -errno;
844                         snprintf(err_str, LNET_MAX_STR_LEN,
845                                         "route operation failed: %s",
846                                         strerror(errno));
847                         goto out;
848                 } else if (errno == EEXIST) {
849                         /*
850                          * continue chugging along if one of the
851                          * routes already exists
852                          */
853                         rc = 0;
854                 }
855         }
856
857 out:
858         cYAML_build_error(rc, seq_no,
859                           cmd == LNETCTL_ADD_CMD ? ADD_CMD : DEL_CMD, "route",
860                           err_str, err_rc);
861
862         return rc;
863 }
864
865 int lustre_lnet_config_route(char *nw, char *nidstr, int hops, int prio,
866                              int sen, int seq_no, struct cYAML **err_rc)
867 {
868         int rc;
869         char err_str[LNET_MAX_STR_LEN] = "\"generic error\"";
870
871         if (hops == -1) {
872                 hops = LNET_UNDEFINED_HOPS;
873         } else if (hops < 1 || hops > 255) {
874                 snprintf(err_str, LNET_MAX_STR_LEN,
875                          "\"invalid hop count %d, must be between 1 and 255\"",
876                          hops);
877                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
878                 goto out;
879         }
880
881         if (prio == -1) {
882                 prio = 0;
883         } else if (prio < 0) {
884                 snprintf(err_str, LNET_MAX_STR_LEN,
885                          "\"invalid priority %d, must be greater than 0\"",
886                          prio);
887                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
888                 goto out;
889         }
890
891         if (sen == -1) {
892                 sen = 1;
893         } else if (sen < 1) {
894                 snprintf(err_str, LNET_MAX_STR_LEN,
895                          "\"invalid health sensitivity %d, must be 1 or greater\"",
896                          sen);
897                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
898                 goto out;
899         }
900
901         rc = lustre_lnet_route_common(nw, nidstr, hops, prio, sen, seq_no,
902                                       err_rc, LNETCTL_ADD_CMD);
903         return rc;
904 out:
905         cYAML_build_error(rc, seq_no, ADD_CMD, "route", err_str, err_rc);
906
907         return rc;
908 }
909
910 int lustre_lnet_del_route(char *nw, char *nidstr, int seq_no,
911                           struct cYAML **err_rc)
912 {
913         return lustre_lnet_route_common(nw, nidstr, 0, 0, 0, seq_no, err_rc,
914                                         LNETCTL_DEL_CMD);
915 }
916
917 int lustre_lnet_show_route(char *nw, char *gw, int hops, int prio, int detail,
918                            int seq_no, struct cYAML **show_rc,
919                            struct cYAML **err_rc, bool backup)
920 {
921         struct lnet_ioctl_config_data data;
922         lnet_nid_t gateway_nid;
923         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
924         int l_errno = 0;
925         __u32 net = LNET_NET_ANY;
926         int i;
927         struct cYAML *root = NULL, *route = NULL, *item = NULL;
928         struct cYAML *first_seq = NULL;
929         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
930         bool exist = false;
931
932         if (nw != NULL) {
933                 net = libcfs_str2net(nw);
934                 if (net == LNET_NET_ANY) {
935                         snprintf(err_str,
936                                  sizeof(err_str),
937                                  "\"cannot parse net '%s'\"", nw);
938                         rc = LUSTRE_CFG_RC_BAD_PARAM;
939                         goto out;
940                 }
941
942         } else {
943                 /* show all routes without filtering on net */
944                 net = LNET_NET_ANY;
945         }
946
947         if (gw != NULL) {
948                 gateway_nid = libcfs_str2nid(gw);
949                 if (gateway_nid == LNET_NID_ANY) {
950                         snprintf(err_str,
951                                  sizeof(err_str),
952                                  "\"cannot parse gateway NID '%s'\"", gw);
953                         rc = LUSTRE_CFG_RC_BAD_PARAM;
954                         goto out;
955                 }
956         } else
957                 /* show all routes with out filtering on gateway */
958                 gateway_nid = LNET_NID_ANY;
959
960         if ((hops < 1 && hops != -1) || hops > 255) {
961                 snprintf(err_str,
962                          sizeof(err_str),
963                          "\"invalid hop count %d, must be between 0 and 256\"",
964                          hops);
965                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
966                 goto out;
967         }
968
969         /* create struct cYAML root object */
970         root = cYAML_create_object(NULL, NULL);
971         if (root == NULL)
972                 goto out;
973
974         route = cYAML_create_seq(root, "route");
975         if (route == NULL)
976                 goto out;
977
978         for (i = 0;; i++) {
979                 __u32 rt_alive;
980                 __u32 rt_multi_hop;
981
982                 LIBCFS_IOC_INIT_V2(data, cfg_hdr);
983                 data.cfg_count = i;
984
985                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_ROUTE, &data);
986                 if (rc != 0) {
987                         l_errno = errno;
988                         break;
989                 }
990
991                 /* filter on provided data */
992                 if (net != LNET_NET_ANY &&
993                     net != data.cfg_net)
994                         continue;
995
996                 if (gateway_nid != LNET_NID_ANY &&
997                     gateway_nid != data.cfg_nid)
998                         continue;
999
1000                 if (hops != -1 &&
1001                     hops != data.cfg_config_u.cfg_route.rtr_hop)
1002                         continue;
1003
1004                 if (prio != -1 &&
1005                     prio != data.cfg_config_u.cfg_route.rtr_priority)
1006                         continue;
1007
1008                 /* default rc to -1 incase we hit the goto */
1009                 rc = -1;
1010                 exist = true;
1011
1012                 item = cYAML_create_seq_item(route);
1013                 if (item == NULL)
1014                         goto out;
1015
1016                 if (first_seq == NULL)
1017                         first_seq = item;
1018
1019                 if (cYAML_create_string(item, "net",
1020                                         libcfs_net2str(data.cfg_net)) == NULL)
1021                         goto out;
1022
1023                 if (cYAML_create_string(item, "gateway",
1024                                         libcfs_nid2str(data.cfg_nid)) == NULL)
1025                         goto out;
1026
1027                 if (detail) {
1028                         if (cYAML_create_number(item, "hop",
1029                                                 (int) data.cfg_config_u.
1030                                                 cfg_route.rtr_hop) ==
1031                             NULL)
1032                                 goto out;
1033
1034                         if (cYAML_create_number(item, "priority",
1035                                                 data.cfg_config_u.
1036                                                 cfg_route.rtr_priority) == NULL)
1037                                 goto out;
1038
1039                         if (cYAML_create_number(item, "health_sensitivity",
1040                                                 data.cfg_config_u.
1041                                                 cfg_route.rtr_sensitivity) == NULL)
1042                                 goto out;
1043
1044                         rt_alive = data.cfg_config_u.cfg_route.rtr_flags &
1045                                         LNET_RT_ALIVE;
1046                         rt_multi_hop = data.cfg_config_u.cfg_route.rtr_flags &
1047                                         LNET_RT_MULTI_HOP;
1048
1049                         if (!backup &&
1050                             cYAML_create_string(item, "state",
1051                                                 rt_alive ?
1052                                                 "up" : "down") == NULL)
1053                                 goto out;
1054
1055                         if (!backup &&
1056                             cYAML_create_string(item, "type",
1057                                                 rt_multi_hop?
1058                                                 "multi-hop" : "single-hop") == NULL)
1059                                 goto out;
1060                 }
1061         }
1062
1063         /* print output iff show_rc is not provided */
1064         if (show_rc == NULL)
1065                 cYAML_print_tree(root);
1066
1067         if (l_errno != ENOENT) {
1068                 snprintf(err_str,
1069                          sizeof(err_str),
1070                          "\"cannot get routes: %s\"",
1071                          strerror(l_errno));
1072                 rc = -l_errno;
1073                 goto out;
1074         } else
1075                 rc = LUSTRE_CFG_RC_NO_ERR;
1076
1077         snprintf(err_str, sizeof(err_str), "\"success\"");
1078 out:
1079         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
1080                 cYAML_free_tree(root);
1081         } else if (show_rc != NULL && *show_rc != NULL) {
1082                 struct cYAML *show_node;
1083                 /* find the route node, if one doesn't exist then
1084                  * insert one.  Otherwise add to the one there
1085                  */
1086                 show_node = cYAML_get_object_item(*show_rc, "route");
1087                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
1088                         cYAML_insert_child(show_node, first_seq);
1089                         free(route);
1090                         free(root);
1091                 } else if (show_node == NULL) {
1092                         cYAML_insert_sibling((*show_rc)->cy_child,
1093                                                 route);
1094                         free(root);
1095                 } else {
1096                         cYAML_free_tree(root);
1097                 }
1098         } else {
1099                 *show_rc = root;
1100         }
1101
1102         cYAML_build_error(rc, seq_no, SHOW_CMD, "route", err_str, err_rc);
1103
1104         return rc;
1105 }
1106
1107 static int socket_intf_query(int request, char *intf,
1108                              struct ifreq *ifr)
1109 {
1110         int rc = 0;
1111         int sockfd;
1112
1113         if (strlen(intf) >= IFNAMSIZ || ifr == NULL)
1114                 return LUSTRE_CFG_RC_BAD_PARAM;
1115
1116         sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1117         if (sockfd < 0)
1118                 return LUSTRE_CFG_RC_BAD_PARAM;
1119
1120         strcpy(ifr->ifr_name, intf);
1121         rc = ioctl(sockfd, request, ifr);
1122         if (rc != 0)
1123                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1124
1125         close(sockfd);
1126
1127         return rc;
1128 }
1129
1130 static int lustre_lnet_queryip(struct lnet_dlc_intf_descr *intf, __u32 *ip)
1131 {
1132         struct ifreq ifr;
1133         int rc;
1134
1135         memset(&ifr, 0, sizeof(ifr));
1136         rc = socket_intf_query(SIOCGIFFLAGS, intf->intf_name, &ifr);
1137         if (rc != 0)
1138                 return LUSTRE_CFG_RC_BAD_PARAM;
1139
1140         if ((ifr.ifr_flags & IFF_UP) == 0)
1141                 return LUSTRE_CFG_RC_BAD_PARAM;
1142
1143         memset(&ifr, 0, sizeof(ifr));
1144         rc = socket_intf_query(SIOCGIFADDR, intf->intf_name, &ifr);
1145         if (rc != 0)
1146                 return LUSTRE_CFG_RC_BAD_PARAM;
1147
1148         *ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
1149         *ip = bswap_32(*ip);
1150
1151         return LUSTRE_CFG_RC_NO_ERR;
1152 }
1153
1154 /*
1155  * for each interface in the array of interfaces find the IP address of
1156  * that interface, create its nid and add it to an array of NIDs.
1157  * Stop if any of the interfaces is down
1158  */
1159 static int lustre_lnet_intf2nids(struct lnet_dlc_network_descr *nw,
1160                                  lnet_nid_t **nids, __u32 *nnids,
1161                                  char *err_str, size_t str_len)
1162 {
1163         int i = 0, count = 0, rc;
1164         struct lnet_dlc_intf_descr *intf;
1165         char val[LNET_MAX_STR_LEN];
1166         __u32 ip;
1167         int gni_num;
1168         char *endp;
1169         unsigned int num;
1170
1171
1172         if (nw == NULL || nids == NULL) {
1173                 snprintf(err_str, str_len,
1174                          "\"unexpected parameters to lustre_lnet_intf2nids()\"");
1175                 return LUSTRE_CFG_RC_BAD_PARAM;
1176         }
1177
1178         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1179                 count = 1;
1180         } else {
1181                 list_for_each_entry(intf, &nw->nw_intflist, intf_on_network)
1182                         count++;
1183         }
1184
1185         *nids = calloc(count, sizeof(lnet_nid_t));
1186         if (*nids == NULL) {
1187                 snprintf(err_str, str_len,
1188                          "\"out of memory\"");
1189                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1190         }
1191         /*
1192          * special case the GNI interface since it doesn't have an IP
1193          * address. The assumption is that there can only be one GNI
1194          * interface in the system. No interface name is provided.
1195          */
1196         if (LNET_NETTYP(nw->nw_id) == GNILND) {
1197                 rc = read_sysfs_file(gni_nid_path, "nid", val,
1198                                 1, sizeof(val));
1199                 if (rc) {
1200                         snprintf(err_str, str_len,
1201                                  "\"cannot read gni nid\"");
1202                         goto failed;
1203                 }
1204                 gni_num = atoi(val);
1205
1206                 (*nids)[i] = LNET_MKNID(nw->nw_id, gni_num);
1207
1208                 goto out;
1209         }
1210
1211         /* look at the other interfaces */
1212         list_for_each_entry(intf, &nw->nw_intflist, intf_on_network) {
1213                 if (LNET_NETTYP(nw->nw_id) == PTL4LND) {
1214                         /* handle LNDs with numeric interface name */
1215                         num = strtoul(intf->intf_name, &endp, 0);
1216                         if (endp == intf->intf_name || *endp != '\0') {
1217                                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1218                                 snprintf(err_str, str_len,
1219                                          "\"couldn't query intf %s\"",
1220                                          intf->intf_name);
1221                                 goto failed;
1222                         }
1223                         (*nids)[i] = LNET_MKNID(nw->nw_id, num);
1224                         i++;
1225                 } else {
1226                         /* handle LNDs with ip interface name */
1227                         rc = lustre_lnet_queryip(intf, &ip);
1228                         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1229                                 snprintf(err_str, str_len,
1230                                          "\"couldn't query intf %s\"",
1231                                          intf->intf_name);
1232                                 goto failed;
1233                         }
1234                         (*nids)[i] = LNET_MKNID(nw->nw_id, ip);
1235                         i++;
1236                 }
1237         }
1238
1239 out:
1240         *nnids = count;
1241
1242         return 0;
1243
1244 failed:
1245         free(*nids);
1246         *nids = NULL;
1247         return rc;
1248 }
1249
1250 /*
1251  * called repeatedly until a match or no more ip range
1252  * What do you have?
1253  *      ip_range expression
1254  *      interface list with all the interface names.
1255  *      all the interfaces in the system.
1256  *
1257  *      try to match the ip_range expr to one of the interfaces' IPs in
1258  *      the system. If we hit a patch for an interface. Check if that
1259  *      interface name is in the list.
1260  *
1261  *      If there are more than one interface in the list, then make sure
1262  *      that the IPs for all of these interfaces match the ip ranges
1263  *      given.
1264  *
1265  *      for each interface in intf_list
1266  *              look up the intf name in ifa
1267  *              if not there then no match
1268  *              check ip obtained from ifa against a match to any of the
1269  *              ip_ranges given.
1270  *              If no match, then fail
1271  *
1272  *      The result is that all the interfaces have to match.
1273  */
1274 int lustre_lnet_match_ip_to_intf(struct ifaddrs *ifa,
1275                                  struct list_head *intf_list,
1276                                  struct list_head *ip_ranges)
1277 {
1278         int rc;
1279         __u32 ip;
1280         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1281         struct ifaddrs *ifaddr = ifa;
1282         struct lustre_lnet_ip_range_descr *ip_range;
1283         int family;
1284
1285         /*
1286          * if there are no explicit interfaces, and no ip ranges, then
1287          * configure the first tcp interface we encounter.
1288          */
1289         if (list_empty(intf_list) && list_empty(ip_ranges)) {
1290                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1291                         if (ifaddr->ifa_addr == NULL)
1292                                 continue;
1293
1294                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1295                                 continue;
1296
1297                         family = ifaddr->ifa_addr->sa_family;
1298                         if (family == AF_INET &&
1299                             strcmp(ifaddr->ifa_name, "lo") != 0) {
1300                                 rc = lustre_lnet_add_intf_descr
1301                                         (intf_list, ifaddr->ifa_name,
1302                                         strlen(ifaddr->ifa_name));
1303
1304                                 if (rc != LUSTRE_CFG_RC_NO_ERR)
1305                                         return rc;
1306
1307                                 return LUSTRE_CFG_RC_MATCH;
1308                         }
1309                 }
1310                 return LUSTRE_CFG_RC_NO_MATCH;
1311         }
1312
1313         /*
1314          * First interface which matches an IP pattern will be used
1315          */
1316         if (list_empty(intf_list)) {
1317                 /*
1318                  * no interfaces provided in the rule, but an ip range is
1319                  * provided, so try and match an interface to the ip
1320                  * range.
1321                  */
1322                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1323                         if (ifaddr->ifa_addr == NULL)
1324                                 continue;
1325
1326                         if ((ifaddr->ifa_flags & IFF_UP) == 0)
1327                                 continue;
1328
1329                         family = ifaddr->ifa_addr->sa_family;
1330                         if (family == AF_INET) {
1331                                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->
1332                                         sin_addr.s_addr;
1333
1334                                 list_for_each_entry(ip_range, ip_ranges,
1335                                                     ipr_entry) {
1336                                         rc = cfs_ip_addr_match(bswap_32(ip),
1337                                                         &ip_range->ipr_expr);
1338                                         if (!rc)
1339                                                 continue;
1340
1341                                         rc = lustre_lnet_add_intf_descr
1342                                           (intf_list, ifaddr->ifa_name,
1343                                            strlen(ifaddr->ifa_name));
1344
1345                                         if (rc != LUSTRE_CFG_RC_NO_ERR)
1346                                                 return rc;
1347                                 }
1348                         }
1349                 }
1350
1351                 if (!list_empty(intf_list))
1352                         return LUSTRE_CFG_RC_MATCH;
1353
1354                 return LUSTRE_CFG_RC_NO_MATCH;
1355         }
1356
1357         /*
1358          * If an interface is explicitly specified the ip-range might or
1359          * might not be specified. if specified the interface needs to match the
1360          * ip-range. If no ip-range then the interfaces are
1361          * automatically matched if they are all up.
1362          * If > 1 interfaces all the interfaces must match for the NI to
1363          * be configured.
1364          */
1365         list_for_each_entry_safe(intf_descr, tmp, intf_list, intf_on_network) {
1366                 for (ifaddr = ifa; ifaddr != NULL; ifaddr = ifaddr->ifa_next) {
1367                         if (ifaddr->ifa_addr == NULL)
1368                                 continue;
1369
1370                         family = ifaddr->ifa_addr->sa_family;
1371                         if (family == AF_INET &&
1372                             strcmp(intf_descr->intf_name,
1373                                    ifaddr->ifa_name) == 0)
1374                                 break;
1375                 }
1376
1377                 if (ifaddr == NULL) {
1378                         list_del(&intf_descr->intf_on_network);
1379                         free_intf_descr(intf_descr);
1380                         continue;
1381                 }
1382
1383                 if ((ifaddr->ifa_flags & IFF_UP) == 0) {
1384                         list_del(&intf_descr->intf_on_network);
1385                         free_intf_descr(intf_descr);
1386                         continue;
1387                 }
1388
1389                 ip = ((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr.s_addr;
1390
1391                 rc = 1;
1392                 list_for_each_entry(ip_range, ip_ranges, ipr_entry) {
1393                         rc = cfs_ip_addr_match(bswap_32(ip), &ip_range->ipr_expr);
1394                         if (rc)
1395                                 break;
1396                 }
1397
1398                 if (!rc) {
1399                         /* no match for this interface */
1400                         list_del(&intf_descr->intf_on_network);
1401                         free_intf_descr(intf_descr);
1402                 }
1403         }
1404
1405         return LUSTRE_CFG_RC_MATCH;
1406 }
1407
1408 static int lustre_lnet_resolve_ip2nets_rule(struct lustre_lnet_ip2nets *ip2nets,
1409                                             lnet_nid_t **nids, __u32 *nnids,
1410                                             char *err_str, size_t str_len)
1411 {
1412         struct ifaddrs *ifa;
1413         int rc = LUSTRE_CFG_RC_NO_ERR;
1414
1415         rc = getifaddrs(&ifa);
1416         if (rc < 0) {
1417                 snprintf(err_str, str_len,
1418                          "\"failed to get interface addresses: %d\"", -errno);
1419                 return -errno;
1420         }
1421
1422         rc = lustre_lnet_match_ip_to_intf(ifa,
1423                                           &ip2nets->ip2nets_net.nw_intflist,
1424                                           &ip2nets->ip2nets_ip_ranges);
1425         if (rc != LUSTRE_CFG_RC_MATCH) {
1426                 snprintf(err_str, str_len,
1427                          "\"couldn't match ip to existing interfaces\"");
1428                 freeifaddrs(ifa);
1429                 return rc;
1430         }
1431
1432         rc = lustre_lnet_intf2nids(&ip2nets->ip2nets_net, nids, nnids,
1433                                    err_str, str_len);
1434         if (rc != LUSTRE_CFG_RC_NO_ERR) {
1435                 *nids = NULL;
1436                 *nnids = 0;
1437         }
1438
1439         freeifaddrs(ifa);
1440
1441         return rc;
1442 }
1443
1444 static int
1445 lustre_lnet_ioctl_config_ni(struct list_head *intf_list,
1446                             struct lnet_ioctl_config_lnd_tunables *tunables,
1447                             struct cfs_expr_list *global_cpts,
1448                             lnet_nid_t *nids, char *err_str)
1449 {
1450         char *data;
1451         struct lnet_ioctl_config_ni *conf;
1452         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1453         int rc = LUSTRE_CFG_RC_NO_ERR, i = 0;
1454         size_t len;
1455         int count;
1456         struct lnet_dlc_intf_descr *intf_descr;
1457         __u32 *cpt_array;
1458         struct cfs_expr_list *cpt_expr;
1459
1460         list_for_each_entry(intf_descr, intf_list,
1461                             intf_on_network) {
1462                 if (tunables != NULL)
1463                         len = sizeof(struct lnet_ioctl_config_ni) +
1464                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1465                 else
1466                         len = sizeof(struct lnet_ioctl_config_ni);
1467
1468                 data = calloc(1, len);
1469                 if (!data)
1470                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1471                 conf = (struct lnet_ioctl_config_ni*) data;
1472                 if (tunables != NULL)
1473                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1474                                 conf->lic_bulk;
1475
1476                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1477                 conf->lic_cfg_hdr.ioc_len = len;
1478                 conf->lic_nid = nids[i];
1479                 strncpy(conf->lic_ni_intf, intf_descr->intf_name,
1480                         LNET_MAX_STR_LEN);
1481
1482                 if (intf_descr->cpt_expr != NULL)
1483                         cpt_expr = intf_descr->cpt_expr;
1484                 else if (global_cpts != NULL)
1485                         cpt_expr = global_cpts;
1486                 else
1487                         cpt_expr = NULL;
1488
1489                 if (cpt_expr != NULL) {
1490                         count = cfs_expr_list_values(cpt_expr,
1491                                                      LNET_MAX_SHOW_NUM_CPT,
1492                                                      &cpt_array);
1493                         if (count > 0) {
1494                                 memcpy(conf->lic_cpts, cpt_array,
1495                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1496                                 free(cpt_array);
1497                         } else {
1498                                 count = 0;
1499                         }
1500                 } else {
1501                         count = 0;
1502                 }
1503
1504                 conf->lic_ncpts = count;
1505
1506                 if (tunables != NULL)
1507                         memcpy(tun, tunables, sizeof(*tunables));
1508
1509                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1510                 if (rc < 0) {
1511                         rc = -errno;
1512                         snprintf(err_str,
1513                                  LNET_MAX_STR_LEN,
1514                                  "\"cannot add network: %s\"", strerror(errno));
1515                         free(data);
1516                         return rc;
1517                 }
1518                 free(data);
1519                 i++;
1520         }
1521
1522         return LUSTRE_CFG_RC_NO_ERR;
1523 }
1524
1525 int
1526 lustre_lnet_config_ip2nets(struct lustre_lnet_ip2nets *ip2nets,
1527                            struct lnet_ioctl_config_lnd_tunables *tunables,
1528                            struct cfs_expr_list *global_cpts,
1529                            int seq_no, struct cYAML **err_rc)
1530 {
1531         lnet_nid_t *nids = NULL;
1532         __u32 nnids = 0;
1533         int rc;
1534         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1535
1536         if (!ip2nets) {
1537                 snprintf(err_str,
1538                          sizeof(err_str),
1539                          "\"incomplete ip2nets information\"");
1540                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1541                 goto out;
1542         }
1543
1544         /*
1545          * call below function to resolve the rules into a list of nids.
1546          * The memory is allocated in that function then freed here when
1547          * it's no longer needed.
1548          */
1549         rc = lustre_lnet_resolve_ip2nets_rule(ip2nets, &nids, &nnids, err_str,
1550                                               sizeof(err_str));
1551         if (rc != LUSTRE_CFG_RC_NO_ERR && rc != LUSTRE_CFG_RC_MATCH)
1552                 goto out;
1553
1554         if (list_empty(&ip2nets->ip2nets_net.nw_intflist)) {
1555                 snprintf(err_str, sizeof(err_str),
1556                          "\"no interfaces match ip2nets rules\"");
1557                 goto free_nids_out;
1558         }
1559
1560         rc = lustre_lnet_ioctl_config_ni(&ip2nets->ip2nets_net.nw_intflist,
1561                                          tunables, global_cpts, nids,
1562                                          err_str);
1563
1564 free_nids_out:
1565         free(nids);
1566
1567 out:
1568         cYAML_build_error(rc, seq_no, ADD_CMD, "ip2nets", err_str, err_rc);
1569         return rc;
1570 }
1571
1572 int lustre_lnet_config_ni(struct lnet_dlc_network_descr *nw_descr,
1573                           struct cfs_expr_list *global_cpts,
1574                           char *ip2net,
1575                           struct lnet_ioctl_config_lnd_tunables *tunables,
1576                           long int cpp, int seq_no, struct cYAML **err_rc)
1577 {
1578         char *data = NULL;
1579         struct lnet_ioctl_config_ni *conf;
1580         struct lnet_ioctl_config_lnd_tunables *tun = NULL;
1581         char buf[LNET_MAX_STR_LEN];
1582         int rc = LUSTRE_CFG_RC_NO_ERR;
1583         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1584         lnet_nid_t *nids = NULL;
1585         __u32 nnids = 0;
1586         size_t len;
1587         int count;
1588         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1589         __u32 *cpt_array;
1590
1591         if (ip2net == NULL && (nw_descr == NULL || nw_descr->nw_id == 0 ||
1592             (list_empty(&nw_descr->nw_intflist) &&
1593              LNET_NETTYP(nw_descr->nw_id) != GNILND))) {
1594                 snprintf(err_str,
1595                          sizeof(err_str),
1596                          "\"missing mandatory parameters in NI config: '%s'\"",
1597                          (nw_descr == NULL) ? "network , interface" :
1598                          (nw_descr->nw_id == 0) ? "network" : "interface");
1599                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1600                 goto out;
1601         }
1602
1603         if (ip2net != NULL && strlen(ip2net) >= sizeof(buf)) {
1604                 snprintf(err_str,
1605                          sizeof(err_str),
1606                          "\"ip2net string too long %d\"",
1607                                 (int)strlen(ip2net));
1608                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
1609                 goto out;
1610         }
1611
1612         if (ip2net != NULL) {
1613                 if (tunables != NULL)
1614                         len = sizeof(struct lnet_ioctl_config_ni) +
1615                               sizeof(struct lnet_ioctl_config_lnd_tunables);
1616                 else
1617                         len = sizeof(struct lnet_ioctl_config_ni);
1618                 data = calloc(1, len);
1619                 if (!data) {
1620                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1621                         goto out;
1622                 }
1623                 conf = (struct lnet_ioctl_config_ni*) data;
1624                 if (tunables != NULL)
1625                         tun = (struct lnet_ioctl_config_lnd_tunables*)
1626                                 (data + sizeof(*conf));
1627
1628                 LIBCFS_IOC_INIT_V2(*conf, lic_cfg_hdr);
1629                 conf->lic_cfg_hdr.ioc_len = len;
1630                 strncpy(conf->lic_legacy_ip2nets, ip2net,
1631                         LNET_MAX_STR_LEN);
1632
1633                 if (global_cpts != NULL) {
1634                         count = cfs_expr_list_values(global_cpts,
1635                                                      LNET_MAX_SHOW_NUM_CPT,
1636                                                      &cpt_array);
1637                         if (count > 0) {
1638                                 memcpy(conf->lic_cpts, cpt_array,
1639                                        sizeof(cpt_array[0]) * LNET_MAX_STR_LEN);
1640                                 free(cpt_array);
1641                         } else {
1642                                 count = 0;
1643                         }
1644                 } else {
1645                         count = 0;
1646                 }
1647
1648                 conf->lic_ncpts = count;
1649
1650                 if (tunables != NULL)
1651                         memcpy(tun, tunables, sizeof(*tunables));
1652
1653                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_LOCAL_NI, data);
1654                 if (rc < 0) {
1655                         rc = -errno;
1656                         snprintf(err_str,
1657                                 sizeof(err_str),
1658                                 "\"cannot add network: %s\"", strerror(errno));
1659                         goto out;
1660                 }
1661
1662                 goto out;
1663         }
1664
1665         if (LNET_NETTYP(nw_descr->nw_id) == LOLND) {
1666                 rc = LUSTRE_CFG_RC_NO_ERR;
1667                 goto out;
1668         }
1669
1670         if (nw_descr->nw_id == LNET_NET_ANY) {
1671                 snprintf(err_str,
1672                         sizeof(err_str),
1673                         "\"cannot parse net '%s'\"",
1674                         libcfs_net2str(nw_descr->nw_id));
1675                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1676                 goto out;
1677         }
1678
1679         /*
1680          * special case the GNI since no interface name is expected
1681          */
1682         if (list_empty(&nw_descr->nw_intflist) &&
1683             (LNET_NETTYP(nw_descr->nw_id) != GNILND)) {
1684                 snprintf(err_str,
1685                         sizeof(err_str),
1686                         "\"no interface name provided\"");
1687                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1688                 goto out;
1689         }
1690
1691         if (LNET_NETTYP(nw_descr->nw_id) == SOCKLND && (cpp > -1))
1692                 tunables->lt_tun.lnd_tun_u.lnd_sock.lnd_conns_per_peer = cpp;
1693         else if (LNET_NETTYP(nw_descr->nw_id) == O2IBLND && (cpp > -1))
1694                 tunables->lt_tun.lnd_tun_u.lnd_o2ib.lnd_conns_per_peer = cpp;
1695
1696         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1697                                    err_str, sizeof(err_str));
1698         if (rc != 0) {
1699                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1700                 goto out;
1701         }
1702
1703         rc = lustre_lnet_ioctl_config_ni(&nw_descr->nw_intflist,
1704                                          tunables, global_cpts, nids,
1705                                          err_str);
1706
1707 out:
1708         if (nw_descr != NULL) {
1709                 list_for_each_entry_safe(intf_descr, tmp,
1710                                          &nw_descr->nw_intflist,
1711                                          intf_on_network) {
1712                         list_del(&intf_descr->intf_on_network);
1713                         free_intf_descr(intf_descr);
1714                 }
1715         }
1716
1717         cYAML_build_error(rc, seq_no, ADD_CMD, "net", err_str, err_rc);
1718
1719         if (nids)
1720                 free(nids);
1721
1722         if (data)
1723                 free(data);
1724
1725         return rc;
1726 }
1727
1728 int lustre_lnet_del_ni(struct lnet_dlc_network_descr *nw_descr,
1729                        int seq_no, struct cYAML **err_rc)
1730 {
1731         struct lnet_ioctl_config_ni data;
1732         int rc = LUSTRE_CFG_RC_NO_ERR, i;
1733         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1734         lnet_nid_t *nids = NULL;
1735         __u32 nnids = 0;
1736         struct lnet_dlc_intf_descr *intf_descr, *tmp;
1737
1738         if (nw_descr == NULL || nw_descr->nw_id == 0) {
1739                 snprintf(err_str,
1740                          sizeof(err_str),
1741                          "\"missing mandatory parameter in deleting NI: '%s'\"",
1742                          (nw_descr == NULL) ? "network , interface" :
1743                          (nw_descr->nw_id == 0) ? "network" : "interface");
1744                 rc = LUSTRE_CFG_RC_MISSING_PARAM;
1745                 goto out;
1746         }
1747
1748         if (LNET_NETTYP(nw_descr->nw_id) == LOLND)
1749                 return LUSTRE_CFG_RC_NO_ERR;
1750
1751         if (nw_descr->nw_id == LNET_NET_ANY) {
1752                 snprintf(err_str,
1753                          sizeof(err_str),
1754                          "\"cannot parse net '%s'\"",
1755                          libcfs_net2str(nw_descr->nw_id));
1756                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1757                 goto out;
1758         }
1759
1760         rc = lustre_lnet_intf2nids(nw_descr, &nids, &nnids,
1761                                    err_str, sizeof(err_str));
1762         if (rc != 0) {
1763                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1764                 goto out;
1765         }
1766
1767         /*
1768          * no interfaces just the nw_id is specified
1769          */
1770         if (nnids == 0) {
1771                 nids = calloc(1, sizeof(*nids));
1772                 if (nids == NULL) {
1773                         snprintf(err_str, sizeof(err_str),
1774                                 "\"out of memory\"");
1775                         rc = LUSTRE_CFG_RC_OUT_OF_MEM;
1776                         goto out;
1777                 }
1778                 nids[0] = LNET_MKNID(nw_descr->nw_id, 0);
1779                 nnids = 1;
1780         }
1781
1782         for (i = 0; i < nnids; i++) {
1783                 LIBCFS_IOC_INIT_V2(data, lic_cfg_hdr);
1784                 data.lic_nid = nids[i];
1785
1786                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_DEL_LOCAL_NI, &data);
1787                 if (rc < 0) {
1788                         rc = -errno;
1789                         snprintf(err_str,
1790                                 sizeof(err_str),
1791                                 "\"cannot del network: %s\"", strerror(errno));
1792                 }
1793         }
1794
1795         list_for_each_entry_safe(intf_descr, tmp, &nw_descr->nw_intflist,
1796                                  intf_on_network) {
1797                 list_del(&intf_descr->intf_on_network);
1798                 free_intf_descr(intf_descr);
1799         }
1800
1801 out:
1802         cYAML_build_error(rc, seq_no, DEL_CMD, "net", err_str, err_rc);
1803
1804         if (nids != NULL)
1805                 free(nids);
1806
1807         return rc;
1808 }
1809
1810 static int
1811 lustre_lnet_config_healthv(int value, bool all, lnet_nid_t nid,
1812                            enum lnet_health_type type, char *name,
1813                            int seq_no, struct cYAML **err_rc)
1814 {
1815         struct lnet_ioctl_reset_health_cfg data;
1816         int rc = LUSTRE_CFG_RC_NO_ERR;
1817         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1818
1819         LIBCFS_IOC_INIT_V2(data, rh_hdr);
1820         data.rh_type = type;
1821         data.rh_all = all;
1822         data.rh_value = value;
1823         data.rh_nid = nid;
1824
1825         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_SET_HEALHV, &data);
1826         if (rc != 0) {
1827                 rc = -errno;
1828                 snprintf(err_str,
1829                          sizeof(err_str), "Can not configure health value: %s",
1830                          strerror(errno));
1831         }
1832
1833         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
1834
1835         return rc;
1836 }
1837
1838
1839 static int
1840 lustre_lnet_config_conns_per_peer(int value, bool all, lnet_nid_t nid,
1841                                   char *name, int seq_no,
1842                                   struct cYAML **err_rc)
1843 {
1844         struct lnet_ioctl_reset_conns_per_peer_cfg data;
1845         int rc = LUSTRE_CFG_RC_NO_ERR;
1846         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
1847
1848         LIBCFS_IOC_INIT_V2(data, rcpp_hdr);
1849         data.rcpp_all = all;
1850         data.rcpp_value = value;
1851         data.rcpp_nid = nid;
1852
1853         if (value < 0 || value > 127) {
1854                 rc = LUSTRE_CFG_RC_BAD_PARAM;
1855                 snprintf(err_str, sizeof(err_str),
1856                          "\"Valid values are: 0-127\"");
1857         } else {
1858
1859                 rc = l_ioctl(LNET_DEV_ID,
1860                              IOC_LIBCFS_SET_CONNS_PER_PEER, &data);
1861                 if (rc != 0) {
1862                         rc = -errno;
1863                         snprintf(err_str,
1864                                  sizeof(err_str),
1865                                  "Can not configure conns_per_peer value: %s",
1866                                  strerror(errno));
1867                 }
1868         }
1869         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
1870
1871         return rc;
1872 }
1873
1874 int lustre_lnet_config_ni_healthv(int value, bool all, char *ni_nid, int seq_no,
1875                                   struct cYAML **err_rc)
1876 {
1877         lnet_nid_t nid;
1878         if (ni_nid)
1879                 nid = libcfs_str2nid(ni_nid);
1880         else
1881                 nid = LNET_NID_ANY;
1882         return lustre_lnet_config_healthv(value, all, nid,
1883                                           LNET_HEALTH_TYPE_LOCAL_NI,
1884                                           "ni healthv", seq_no, err_rc);
1885 }
1886
1887 int lustre_lnet_config_peer_ni_healthv(int value, bool all, char *lpni_nid,
1888                                        int seq_no, struct cYAML **err_rc)
1889 {
1890         lnet_nid_t nid;
1891         if (lpni_nid)
1892                 nid = libcfs_str2nid(lpni_nid);
1893         else
1894                 nid = LNET_NID_ANY;
1895         return lustre_lnet_config_healthv(value, all, nid,
1896                                           LNET_HEALTH_TYPE_PEER_NI,
1897                                           "peer_ni healthv", seq_no, err_rc);
1898 }
1899
1900 int lustre_lnet_config_ni_conns_per_peer(int value, bool all, char *ni_nid,
1901                                          int seq_no, struct cYAML **err_rc)
1902 {
1903         lnet_nid_t nid;
1904
1905         if (ni_nid)
1906                 nid = libcfs_str2nid(ni_nid);
1907         else
1908                 nid = LNET_NID_ANY;
1909         return lustre_lnet_config_conns_per_peer(value, all, nid,
1910                                                  "ni conns_per_peer",
1911                                                  seq_no, err_rc);
1912 }
1913
1914 static bool
1915 add_msg_stats_to_yaml_blk(struct cYAML *yaml,
1916                           struct lnet_ioctl_comm_count *counts)
1917 {
1918         if (cYAML_create_number(yaml, "put",
1919                                 counts->ico_put_count)
1920                                         == NULL)
1921                 return false;
1922         if (cYAML_create_number(yaml, "get",
1923                                 counts->ico_get_count)
1924                                         == NULL)
1925                 return false;
1926         if (cYAML_create_number(yaml, "reply",
1927                                 counts->ico_reply_count)
1928                                         == NULL)
1929                 return false;
1930         if (cYAML_create_number(yaml, "ack",
1931                                 counts->ico_ack_count)
1932                                         == NULL)
1933                 return false;
1934         if (cYAML_create_number(yaml, "hello",
1935                                 counts->ico_hello_count)
1936                                         == NULL)
1937                 return false;
1938
1939         return true;
1940 }
1941
1942 static struct lnet_ioctl_comm_count *
1943 get_counts(struct lnet_ioctl_element_msg_stats *msg_stats, int idx)
1944 {
1945         if (idx == 0)
1946                 return &msg_stats->im_send_stats;
1947         if (idx == 1)
1948                 return &msg_stats->im_recv_stats;
1949         if (idx == 2)
1950                 return &msg_stats->im_drop_stats;
1951
1952         return NULL;
1953 }
1954
1955 static int
1956 create_local_udsp_info(struct lnet_ioctl_construct_udsp_info *udsp_info,
1957                        struct cYAML *net_node)
1958 {
1959         char tmp[LNET_MAX_STR_LEN];
1960         struct cYAML *udsp_net;
1961         bool created = false;
1962         struct cYAML *pref;
1963         int i;
1964
1965         /* add the UDSP info */
1966         udsp_net = cYAML_create_object(net_node, "udsp info");
1967         if (!udsp_net)
1968                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1969
1970         if (!cYAML_create_number(udsp_net, "net priority",
1971                                  (int) udsp_info->cud_net_priority))
1972                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1973
1974         if (!cYAML_create_number(udsp_net, "nid priority",
1975                                  (int)udsp_info->cud_nid_priority))
1976                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1977
1978         pref = udsp_net;
1979
1980         for (i = 0; i < LNET_MAX_SHOW_NUM_NID; i++) {
1981                 memset(tmp, 0, LNET_MAX_STR_LEN);
1982                 if (udsp_info->cud_pref_rtr_nid[i] == 0)
1983                         break;
1984                 if (!created) {
1985                         pref = cYAML_create_object(udsp_net,
1986                                         "Preferred gateway NIDs");
1987                         if (!pref)
1988                                 return LUSTRE_CFG_RC_OUT_OF_MEM;
1989                         created = true;
1990                 }
1991                 snprintf(tmp, sizeof(tmp), "NID-%d", i);
1992                 if (!cYAML_create_string(pref, tmp,
1993                         libcfs_nid2str(udsp_info->cud_pref_rtr_nid[i])))
1994                         return LUSTRE_CFG_RC_OUT_OF_MEM;
1995         }
1996
1997         return LUSTRE_CFG_RC_NO_ERR;
1998 }
1999
2000 static int
2001 create_remote_udsp_info(struct lnet_ioctl_construct_udsp_info *udsp_info,
2002                         struct cYAML *nid_node)
2003 {
2004         char tmp[LNET_MAX_STR_LEN];
2005         struct cYAML *udsp_nid;
2006         bool created = false;
2007         struct cYAML *pref;
2008         int i;
2009
2010         /* add the UDSP info */
2011         udsp_nid = cYAML_create_object(nid_node, "udsp info");
2012         if (!udsp_nid)
2013                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2014
2015         if (!cYAML_create_number(udsp_nid, "net priority",
2016                                  (int) udsp_info->cud_net_priority))
2017                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2018
2019         if (!cYAML_create_number(udsp_nid, "nid priority",
2020                                  (int) udsp_info->cud_nid_priority))
2021                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2022
2023         pref = udsp_nid;
2024         for (i = 0; i < LNET_MAX_SHOW_NUM_NID; i++) {
2025                 memset(tmp, 0, LNET_MAX_STR_LEN);
2026                 if (udsp_info->cud_pref_rtr_nid[i] == 0)
2027                         break;
2028                 if (!created) {
2029                         pref = cYAML_create_object(udsp_nid,
2030                                         "Preferred gateway NIDs");
2031                         if (!pref)
2032                                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2033                         created = true;
2034                 }
2035                 snprintf(tmp, sizeof(tmp), "NID-%d", i);
2036                 if (!cYAML_create_string(pref, tmp,
2037                         libcfs_nid2str(udsp_info->cud_pref_rtr_nid[i])))
2038                         return LUSTRE_CFG_RC_OUT_OF_MEM;
2039         }
2040
2041         pref = udsp_nid;
2042         created = false;
2043         for (i = 0; i < LNET_MAX_SHOW_NUM_NID; i++) {
2044                 memset(tmp, 0, LNET_MAX_STR_LEN);
2045                 if (udsp_info->cud_pref_nid[i] == 0)
2046                         break;
2047                 if (!created) {
2048                         pref = cYAML_create_object(udsp_nid,
2049                                         "Preferred source NIDs");
2050                         if (!pref)
2051                                 return LUSTRE_CFG_RC_OUT_OF_MEM;
2052                         created = true;
2053                 }
2054                 snprintf(tmp, sizeof(tmp), "NID-%d", i);
2055                 if (!cYAML_create_string(pref, tmp,
2056                         libcfs_nid2str(udsp_info->cud_pref_nid[i])))
2057                         return LUSTRE_CFG_RC_OUT_OF_MEM;
2058         }
2059
2060         return LUSTRE_CFG_RC_NO_ERR;
2061 }
2062
2063 int lustre_lnet_show_net(char *nw, int detail, int seq_no,
2064                          struct cYAML **show_rc, struct cYAML **err_rc,
2065                          bool backup)
2066 {
2067         char *buf;
2068         struct lnet_ioctl_config_ni *ni_data;
2069         struct lnet_ioctl_config_lnd_tunables *lnd;
2070         struct lnet_ioctl_element_stats *stats;
2071         struct lnet_ioctl_element_msg_stats msg_stats;
2072         struct lnet_ioctl_local_ni_hstats hstats;
2073         struct lnet_ioctl_construct_udsp_info udsp_info;
2074         __u32 net = LNET_NET_ANY;
2075         __u32 prev_net = LNET_NET_ANY;
2076         int rc = LUSTRE_CFG_RC_OUT_OF_MEM, i, j;
2077         int l_errno = 0;
2078         struct cYAML *root = NULL, *tunables = NULL,
2079                 *net_node = NULL, *interfaces = NULL,
2080                 *item = NULL, *first_seq = NULL,
2081                 *tmp = NULL, *statistics = NULL,
2082                 *yhstats = NULL;
2083         int str_buf_len = LNET_MAX_SHOW_NUM_CPT * 2;
2084         char str_buf[str_buf_len];
2085         char *pos;
2086         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
2087         bool exist = false, new_net = true;
2088         int net_num = 0;
2089         size_t buf_size = sizeof(*ni_data) + sizeof(*lnd) + sizeof(*stats);
2090
2091         buf = calloc(1, buf_size);
2092         if (buf == NULL)
2093                 goto out;
2094
2095         ni_data = (struct lnet_ioctl_config_ni *)buf;
2096
2097         if (nw != NULL) {
2098                 net = libcfs_str2net(nw);
2099                 if (net == LNET_NET_ANY) {
2100                         snprintf(err_str,
2101                                  sizeof(err_str),
2102                                  "\"cannot parse net '%s'\"", nw);
2103                         rc = LUSTRE_CFG_RC_BAD_PARAM;
2104                         goto out;
2105                 }
2106         }
2107
2108         root = cYAML_create_object(NULL, NULL);
2109         if (root == NULL)
2110                 goto out;
2111
2112         net_node = cYAML_create_seq(root, "net");
2113         if (net_node == NULL)
2114                 goto out;
2115
2116         for (i = 0;; i++) {
2117                 __u32 rc_net;
2118
2119                 memset(buf, 0, buf_size);
2120
2121                 LIBCFS_IOC_INIT_V2(*ni_data, lic_cfg_hdr);
2122                 /*
2123                  * set the ioc_len to the proper value since INIT assumes
2124                  * size of data
2125                  */
2126                 ni_data->lic_cfg_hdr.ioc_len = buf_size;
2127                 ni_data->lic_idx = i;
2128
2129                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_LOCAL_NI, ni_data);
2130                 if (rc != 0) {
2131                         l_errno = errno;
2132                         break;
2133                 }
2134
2135                 rc_net = LNET_NIDNET(ni_data->lic_nid);
2136
2137                 /* filter on provided data */
2138                 if (net != LNET_NET_ANY &&
2139                     net != rc_net)
2140                         continue;
2141
2142                 /* if we're backing up don't store lo */
2143                 if (backup && LNET_NETTYP(rc_net) == LOLND)
2144                         continue;
2145
2146                 /* default rc to -1 in case we hit the goto */
2147                 rc = -1;
2148                 exist = true;
2149
2150                 stats = (struct lnet_ioctl_element_stats *)ni_data->lic_bulk;
2151                 lnd = (struct lnet_ioctl_config_lnd_tunables *)
2152                         (ni_data->lic_bulk + sizeof(*stats));
2153
2154                 if (rc_net != prev_net) {
2155                         prev_net = rc_net;
2156                         new_net = true;
2157                         net_num++;
2158                 }
2159
2160                 if (new_net) {
2161                         if (!cYAML_create_string(net_node, "net type",
2162                                                  libcfs_net2str(rc_net)))
2163                                 goto out;
2164
2165                         tmp = cYAML_create_seq(net_node, "local NI(s)");
2166                         if (tmp == NULL)
2167                                 goto out;
2168                         new_net = false;
2169                 }
2170
2171                 /* create the tree to be printed. */
2172                 item = cYAML_create_seq_item(tmp);
2173                 if (item == NULL)
2174                         goto out;
2175
2176                 if (first_seq == NULL)
2177                         first_seq = item;
2178
2179                 if (!backup &&
2180                     cYAML_create_string(item, "nid",
2181                                         libcfs_nid2str(ni_data->lic_nid)) == NULL)
2182                         goto out;
2183
2184                 if (!backup &&
2185                     cYAML_create_string(item,
2186                                         "status",
2187                                         (ni_data->lic_status ==
2188                                           LNET_NI_STATUS_UP) ?
2189                                             "up" : "down") == NULL)
2190                         goto out;
2191
2192                 /* don't add interfaces unless there is at least one
2193                  * interface */
2194                 if (strlen(ni_data->lic_ni_intf) > 0) {
2195                         interfaces = cYAML_create_object(item, "interfaces");
2196                         if (interfaces == NULL)
2197                                 goto out;
2198
2199                         snprintf(str_buf, sizeof(str_buf), "%d", 0);
2200                         if (cYAML_create_string(interfaces, str_buf,
2201                                                 ni_data->lic_ni_intf) == NULL)
2202                                 goto out;
2203                 }
2204
2205                 if (detail) {
2206                         char *limit;
2207                         int k;
2208
2209                         if (backup)
2210                                 goto continue_without_msg_stats;
2211
2212                         statistics = cYAML_create_object(item, "statistics");
2213                         if (statistics == NULL)
2214                                 goto out;
2215
2216                         if (cYAML_create_number(statistics, "send_count",
2217                                                 stats->iel_send_count)
2218                                                         == NULL)
2219                                 goto out;
2220
2221                         if (cYAML_create_number(statistics, "recv_count",
2222                                                 stats->iel_recv_count)
2223                                                         == NULL)
2224                                 goto out;
2225
2226                         if (cYAML_create_number(statistics, "drop_count",
2227                                                 stats->iel_drop_count)
2228                                                         == NULL)
2229                                 goto out;
2230
2231                         if (detail < 4)
2232                                 goto continue_without_udsp_info;
2233
2234                         LIBCFS_IOC_INIT_V2(udsp_info, cud_hdr);
2235                         udsp_info.cud_nid = ni_data->lic_nid;
2236                         udsp_info.cud_peer = false;
2237                         rc = l_ioctl(LNET_DEV_ID,
2238                                      IOC_LIBCFS_GET_CONST_UDSP_INFO,
2239                                      &udsp_info);
2240                         if (rc != 0) {
2241                                 l_errno = errno;
2242                                 goto continue_without_udsp_info;
2243                         }
2244
2245                         rc = create_local_udsp_info(&udsp_info, item);
2246                         if (rc) {
2247                                 l_errno = errno;
2248                                 goto out;
2249                         }
2250
2251 continue_without_udsp_info:
2252                         if (detail < 2)
2253                                 goto continue_without_msg_stats;
2254
2255                         LIBCFS_IOC_INIT_V2(msg_stats, im_hdr);
2256                         msg_stats.im_hdr.ioc_len = sizeof(msg_stats);
2257                         msg_stats.im_idx = i;
2258
2259                         rc = l_ioctl(LNET_DEV_ID,
2260                                      IOC_LIBCFS_GET_LOCAL_NI_MSG_STATS,
2261                                      &msg_stats);
2262                         if (rc != 0) {
2263                                 l_errno = errno;
2264                                 goto continue_without_msg_stats;
2265                         }
2266
2267                         for (k = 0; k < 3; k++) {
2268                                 struct lnet_ioctl_comm_count *counts;
2269                                 struct cYAML *msg_statistics = NULL;
2270
2271                                 msg_statistics = cYAML_create_object(item,
2272                                                  (char *)gmsg_stat_names[k]);
2273                                 if (msg_statistics == NULL)
2274                                         goto out;
2275
2276                                 counts = get_counts(&msg_stats, k);
2277                                 if (counts == NULL)
2278                                         goto out;
2279
2280                                 if (!add_msg_stats_to_yaml_blk(msg_statistics,
2281                                                                counts))
2282                                         goto out;
2283                         }
2284
2285                         LIBCFS_IOC_INIT_V2(hstats, hlni_hdr);
2286                         hstats.hlni_nid = ni_data->lic_nid;
2287                         /* grab health stats */
2288                         rc = l_ioctl(LNET_DEV_ID,
2289                                      IOC_LIBCFS_GET_LOCAL_HSTATS,
2290                                      &hstats);
2291                         if (rc != 0) {
2292                                 l_errno = errno;
2293                                 goto continue_without_msg_stats;
2294                         }
2295                         yhstats = cYAML_create_object(item, "health stats");
2296                         if (!yhstats)
2297                                 goto out;
2298                         if (cYAML_create_number(yhstats, "fatal_error",
2299                                                 hstats.hlni_fatal_error)
2300                                                         == NULL)
2301                                 goto out;
2302                         if (cYAML_create_number(yhstats, "health value",
2303                                                 hstats.hlni_health_value)
2304                                                         == NULL)
2305                                 goto out;
2306                         if (cYAML_create_number(yhstats, "interrupts",
2307                                                 hstats.hlni_local_interrupt)
2308                                                         == NULL)
2309                                 goto out;
2310                         if (cYAML_create_number(yhstats, "dropped",
2311                                                 hstats.hlni_local_dropped)
2312                                                         == NULL)
2313                                 goto out;
2314                         if (cYAML_create_number(yhstats, "aborted",
2315                                                 hstats.hlni_local_aborted)
2316                                                         == NULL)
2317                                 goto out;
2318                         if (cYAML_create_number(yhstats, "no route",
2319                                                 hstats.hlni_local_no_route)
2320                                                         == NULL)
2321                                 goto out;
2322                         if (cYAML_create_number(yhstats, "timeouts",
2323                                                 hstats.hlni_local_timeout)
2324                                                         == NULL)
2325                                 goto out;
2326                         if (cYAML_create_number(yhstats, "error",
2327                                                 hstats.hlni_local_error)
2328                                                         == NULL)
2329                                 goto out;
2330                         if (cYAML_create_number(yhstats, "ping_count",
2331                                                 hstats.hlni_ping_count)
2332                                                         == NULL)
2333                                 goto out;
2334                         if (cYAML_create_number(yhstats, "next_ping",
2335                                                 hstats.hlni_next_ping)
2336                                                         == NULL)
2337                                 goto out;
2338
2339 continue_without_msg_stats:
2340                         tunables = cYAML_create_object(item, "tunables");
2341                         if (!tunables)
2342                                 goto out;
2343
2344                         rc = lustre_net_show_tunables(tunables, &lnd->lt_cmn);
2345                         if (rc != LUSTRE_CFG_RC_NO_ERR)
2346                                 goto out;
2347
2348                         if (rc != LUSTRE_CFG_RC_NO_MATCH) {
2349                                 tunables = cYAML_create_object(item,
2350                                                                "lnd tunables");
2351                                 if (tunables == NULL)
2352                                         goto out;
2353                         }
2354
2355                         rc = lustre_ni_show_tunables(tunables,
2356                                                      LNET_NETTYP(rc_net),
2357                                                      &lnd->lt_tun);
2358                         if (rc != LUSTRE_CFG_RC_NO_ERR &&
2359                             rc != LUSTRE_CFG_RC_NO_MATCH)
2360                                 goto out;
2361
2362                         if (!backup &&
2363                             cYAML_create_number(item, "dev cpt",
2364                                                 ni_data->lic_dev_cpt) == NULL)
2365                                 goto out;
2366
2367                         /* out put the CPTs in the format: "[x,x,x,...]" */
2368                         pos = str_buf;
2369                         limit = str_buf + str_buf_len - 3;
2370                         pos += scnprintf(pos, limit - pos, "\"[");
2371                         for (j = 0 ; ni_data->lic_ncpts >= 1 &&
2372                                 j < ni_data->lic_ncpts &&
2373                                 pos < limit; j++) {
2374                                 pos += scnprintf(pos, limit - pos,
2375                                                  "%d", ni_data->lic_cpts[j]);
2376                                 if ((j + 1) < ni_data->lic_ncpts)
2377                                         pos += scnprintf(pos, limit - pos, ",");
2378                         }
2379                         snprintf(pos, 3, "]\"");
2380
2381                         if (ni_data->lic_ncpts >= 1 &&
2382                             cYAML_create_string(item, "CPT",
2383                                                 str_buf) == NULL)
2384                                 goto out;
2385                 }
2386         }
2387
2388         /* Print out the net information only if show_rc is not provided */
2389         if (show_rc == NULL)
2390                 cYAML_print_tree(root);
2391
2392         if (l_errno != ENOENT) {
2393                 snprintf(err_str,
2394                          sizeof(err_str),
2395                          "\"cannot get networks: %s\"",
2396                          strerror(l_errno));
2397                 rc = -l_errno;
2398                 goto out;
2399         } else
2400                 rc = LUSTRE_CFG_RC_NO_ERR;
2401
2402         snprintf(err_str, sizeof(err_str), "\"success\"");
2403 out:
2404         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2405                 cYAML_free_tree(root);
2406         } else if (show_rc != NULL && *show_rc != NULL) {
2407                 struct cYAML *show_node;
2408                 /* find the net node, if one doesn't exist
2409                  * then insert one.  Otherwise add to the one there
2410                  */
2411                 show_node = cYAML_get_object_item(*show_rc, "net");
2412                 if (show_node != NULL && cYAML_is_sequence(show_node)) {
2413                         cYAML_insert_child(show_node, first_seq);
2414                         free(net_node);
2415                         free(root);
2416                 } else if (show_node == NULL) {
2417                         cYAML_insert_sibling((*show_rc)->cy_child,
2418                                                 net_node);
2419                         free(root);
2420                 } else {
2421                         cYAML_free_tree(root);
2422                 }
2423         } else {
2424                 *show_rc = root;
2425         }
2426
2427         cYAML_build_error(rc, seq_no, SHOW_CMD, "net", err_str, err_rc);
2428
2429         return rc;
2430 }
2431
2432 int lustre_lnet_enable_routing(int enable, int seq_no, struct cYAML **err_rc)
2433 {
2434         struct lnet_ioctl_config_data data;
2435         int rc = LUSTRE_CFG_RC_NO_ERR;
2436         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2437
2438         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2439         data.cfg_config_u.cfg_buffers.buf_enable = (enable) ? 1 : 0;
2440
2441         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_CONFIG_RTR, &data);
2442         if (rc != 0) {
2443                 rc = -errno;
2444                 snprintf(err_str,
2445                          sizeof(err_str),
2446                          "\"cannot %s routing %s\"",
2447                          (enable) ? "enable" : "disable", strerror(errno));
2448                 goto out;
2449         }
2450
2451 out:
2452         cYAML_build_error(rc, seq_no,
2453                          (enable) ? ADD_CMD : DEL_CMD,
2454                          "routing", err_str, err_rc);
2455
2456         return rc;
2457 }
2458
2459 int ioctl_set_value(__u32 val, int ioc, char *name,
2460                     int seq_no, struct cYAML **err_rc)
2461 {
2462         struct lnet_ioctl_set_value data;
2463         int rc = LUSTRE_CFG_RC_NO_ERR;
2464         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2465
2466         LIBCFS_IOC_INIT_V2(data, sv_hdr);
2467         data.sv_value = val;
2468
2469         rc = l_ioctl(LNET_DEV_ID, ioc , &data);
2470         if (rc != 0) {
2471                 rc = -errno;
2472                 snprintf(err_str,
2473                          sizeof(err_str),
2474                          "\"cannot configure %s to %d: %s\"", name,
2475                          val, strerror(errno));
2476         }
2477
2478         cYAML_build_error(rc, seq_no, ADD_CMD, name, err_str, err_rc);
2479
2480         return rc;
2481 }
2482
2483 int lustre_lnet_config_recov_intrv(int intrv, int seq_no, struct cYAML **err_rc)
2484 {
2485         int rc = LUSTRE_CFG_RC_NO_ERR;
2486         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2487         char val[LNET_MAX_STR_LEN];
2488
2489         snprintf(val, sizeof(val), "%d", intrv);
2490
2491         rc = write_sysfs_file(modparam_path, "lnet_recovery_interval", val,
2492                               1, strlen(val) + 1);
2493         if (rc)
2494                 snprintf(err_str, sizeof(err_str),
2495                          "\"cannot configure recovery interval: %s\"",
2496                          strerror(errno));
2497
2498         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_interval", err_str, err_rc);
2499
2500         return rc;
2501 }
2502
2503 int lustre_lnet_config_rtr_sensitivity(int sen, int seq_no, struct cYAML **err_rc)
2504 {
2505         int rc = LUSTRE_CFG_RC_NO_ERR;
2506         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2507         char val[LNET_MAX_STR_LEN];
2508
2509         snprintf(val, sizeof(val), "%d", sen);
2510
2511         rc = write_sysfs_file(modparam_path, "router_sensitivity_percentage", val,
2512                               1, strlen(val) + 1);
2513         if (rc)
2514                 snprintf(err_str, sizeof(err_str),
2515                          "\"cannot configure router health sensitivity: %s\"",
2516                          strerror(errno));
2517
2518         cYAML_build_error(rc, seq_no, ADD_CMD, "router_sensitivity", err_str, err_rc);
2519
2520         return rc;
2521 }
2522
2523 int lustre_lnet_config_hsensitivity(int sen, int seq_no, struct cYAML **err_rc)
2524 {
2525         int rc = LUSTRE_CFG_RC_NO_ERR;
2526         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2527         char val[LNET_MAX_STR_LEN];
2528
2529         snprintf(val, sizeof(val), "%d", sen);
2530
2531         rc = write_sysfs_file(modparam_path, "lnet_health_sensitivity", val,
2532                               1, strlen(val) + 1);
2533         if (rc)
2534                 snprintf(err_str, sizeof(err_str),
2535                          "\"cannot configure health sensitivity: %s\"",
2536                          strerror(errno));
2537
2538         cYAML_build_error(rc, seq_no, ADD_CMD, "health_sensitivity", err_str, err_rc);
2539
2540         return rc;
2541 }
2542
2543 int lustre_lnet_config_transaction_to(int timeout, int seq_no, struct cYAML **err_rc)
2544 {
2545         int rc = LUSTRE_CFG_RC_NO_ERR;
2546         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2547         char val[LNET_MAX_STR_LEN];
2548
2549         snprintf(val, sizeof(val), "%d", timeout);
2550
2551         rc = write_sysfs_file(modparam_path, "lnet_transaction_timeout", val,
2552                               1, strlen(val) + 1);
2553         if (rc)
2554                 snprintf(err_str, sizeof(err_str),
2555                          "\"cannot configure transaction timeout: %s\"",
2556                          strerror(errno));
2557
2558         cYAML_build_error(rc, seq_no, ADD_CMD, "transaction_timeout", err_str, err_rc);
2559
2560         return rc;
2561 }
2562
2563 int lustre_lnet_config_retry_count(int count, int seq_no, struct cYAML **err_rc)
2564 {
2565         int rc = LUSTRE_CFG_RC_NO_ERR;
2566         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2567         char val[LNET_MAX_STR_LEN];
2568
2569         snprintf(val, sizeof(val), "%d", count);
2570
2571         rc = write_sysfs_file(modparam_path, "lnet_retry_count", val,
2572                               1, strlen(val) + 1);
2573         if (rc)
2574                 snprintf(err_str, sizeof(err_str),
2575                          "\"cannot configure retry count: %s\"",
2576                          strerror(errno));
2577
2578         cYAML_build_error(rc, seq_no, ADD_CMD, "retry_count", err_str, err_rc);
2579
2580         return rc;
2581 }
2582
2583 int lustre_lnet_config_response_tracking(int val, int seq_no,
2584                                          struct cYAML **err_rc)
2585 {
2586         int rc = LUSTRE_CFG_RC_NO_ERR;
2587         char err_str[LNET_MAX_STR_LEN];
2588         char val_str[LNET_MAX_STR_LEN];
2589
2590         if (val < 0 || val > 3) {
2591                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2592                 snprintf(err_str, sizeof(err_str),
2593                          "\"Valid values are: 0, 1, 2, or 3\"");
2594         } else {
2595                 snprintf(err_str, sizeof(err_str), "\"success\"");
2596
2597                 snprintf(val_str, sizeof(val_str), "%d", val);
2598
2599                 rc = write_sysfs_file(modparam_path, "lnet_response_tracking",
2600                                       val_str, 1, strlen(val_str) + 1);
2601                 if (rc)
2602                         snprintf(err_str, sizeof(err_str),
2603                                  "\"cannot configure response tracking: %s\"",
2604                                  strerror(errno));
2605         }
2606
2607         cYAML_build_error(rc, seq_no, ADD_CMD, "response_tracking", err_str,
2608                           err_rc);
2609
2610         return rc;
2611 }
2612
2613 int lustre_lnet_config_recovery_limit(int val, int seq_no,
2614                                       struct cYAML **err_rc)
2615 {
2616         int rc = LUSTRE_CFG_RC_NO_ERR;
2617         char err_str[LNET_MAX_STR_LEN];
2618         char val_str[LNET_MAX_STR_LEN];
2619
2620         if (val < 0) {
2621                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2622                 snprintf(err_str, sizeof(err_str),
2623                          "\"Must be greater than or equal to 0\"");
2624         } else {
2625                 snprintf(err_str, sizeof(err_str), "\"success\"");
2626
2627                 snprintf(val_str, sizeof(val_str), "%d", val);
2628
2629                 rc = write_sysfs_file(modparam_path, "lnet_recovery_limit",
2630                                       val_str, 1, strlen(val_str) + 1);
2631                 if (rc)
2632                         snprintf(err_str, sizeof(err_str),
2633                                  "\"cannot configure recovery limit: %s\"",
2634                                  strerror(errno));
2635         }
2636
2637         cYAML_build_error(rc, seq_no, ADD_CMD, "recovery_limit", err_str,
2638                           err_rc);
2639
2640         return rc;
2641 }
2642
2643 int lustre_lnet_config_max_intf(int max, int seq_no, struct cYAML **err_rc)
2644 {
2645         int rc = LUSTRE_CFG_RC_NO_ERR;
2646         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2647         char val[LNET_MAX_STR_LEN];
2648
2649         snprintf(val, sizeof(val), "%d", max);
2650
2651         rc = write_sysfs_file(modparam_path, "lnet_interfaces_max", val,
2652                               1, strlen(val) + 1);
2653         if (rc)
2654                 snprintf(err_str, sizeof(err_str),
2655                          "\"cannot configure max interfaces: %s\"",
2656                          strerror(errno));
2657
2658         cYAML_build_error(rc, seq_no, ADD_CMD, "max_interfaces", err_str, err_rc);
2659
2660         return rc;
2661 }
2662
2663 int lustre_lnet_config_discovery(int enable, int seq_no, struct cYAML **err_rc)
2664 {
2665         int rc = LUSTRE_CFG_RC_NO_ERR;
2666         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2667         char val[LNET_MAX_STR_LEN];
2668
2669         snprintf(val, sizeof(val), "%u", (enable) ? 0 : 1);
2670
2671         rc = write_sysfs_file(modparam_path, "lnet_peer_discovery_disabled", val,
2672                               1, strlen(val) + 1);
2673         if (rc)
2674                 snprintf(err_str, sizeof(err_str),
2675                          "\"cannot configure discovery: %s\"",
2676                          strerror(errno));
2677
2678         cYAML_build_error(rc, seq_no, ADD_CMD, "discovery", err_str, err_rc);
2679
2680         return rc;
2681
2682 }
2683
2684 int lustre_lnet_config_drop_asym_route(int drop, int seq_no,
2685                                        struct cYAML **err_rc)
2686 {
2687         int rc = LUSTRE_CFG_RC_NO_ERR;
2688         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2689         char val[LNET_MAX_STR_LEN];
2690
2691         snprintf(val, sizeof(val), "%u", (drop) ? 1 : 0);
2692
2693         rc = write_sysfs_file(modparam_path, "lnet_drop_asym_route", val,
2694                               1, strlen(val) + 1);
2695         if (rc)
2696                 snprintf(err_str, sizeof(err_str),
2697                          "\"cannot configure drop asym route: %s\"",
2698                          strerror(errno));
2699
2700         cYAML_build_error(rc, seq_no, ADD_CMD, "drop_asym_route",
2701                           err_str, err_rc);
2702
2703         return rc;
2704
2705 }
2706
2707 int lustre_lnet_config_numa_range(int range, int seq_no, struct cYAML **err_rc)
2708 {
2709         return ioctl_set_value(range, IOC_LIBCFS_SET_NUMA_RANGE,
2710                                "numa_range", seq_no, err_rc);
2711 }
2712
2713 int lustre_lnet_config_buffers(int tiny, int small, int large, int seq_no,
2714                                struct cYAML **err_rc)
2715 {
2716         struct lnet_ioctl_config_data data;
2717         int rc = LUSTRE_CFG_RC_NO_ERR;
2718         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2719
2720         /* -1 indicates to ignore changes to this field */
2721         if (tiny < -1 || small < -1 || large < -1) {
2722                 snprintf(err_str,
2723                          sizeof(err_str),
2724                          "\"tiny, small and large must be >= 0\"");
2725                 rc = LUSTRE_CFG_RC_OUT_OF_RANGE_PARAM;
2726                 goto out;
2727         }
2728
2729         LIBCFS_IOC_INIT_V2(data, cfg_hdr);
2730         data.cfg_config_u.cfg_buffers.buf_tiny = tiny;
2731         data.cfg_config_u.cfg_buffers.buf_small = small;
2732         data.cfg_config_u.cfg_buffers.buf_large = large;
2733
2734         rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_ADD_BUF, &data);
2735         if (rc != 0) {
2736                 rc = -errno;
2737                 snprintf(err_str,
2738                          sizeof(err_str),
2739                          "\"cannot configure buffers: %s\"", strerror(errno));
2740                 goto out;
2741         }
2742
2743 out:
2744         cYAML_build_error(rc, seq_no, ADD_CMD, "buf", err_str, err_rc);
2745
2746         return rc;
2747 }
2748
2749 int lustre_lnet_config_max_recovery_ping_interval(int interval, int seq_no,
2750                                                   struct cYAML **err_rc)
2751 {
2752         int rc = LUSTRE_CFG_RC_NO_ERR;
2753         char err_str[LNET_MAX_STR_LEN] = "\"success\"";
2754         char interval_str[LNET_MAX_STR_LEN];
2755
2756         if (interval <= 0) {
2757                 rc = LUSTRE_CFG_RC_BAD_PARAM;
2758                 snprintf(err_str, sizeof(err_str),
2759                          "\"must be strictly positive\"");
2760
2761         } else {
2762                 snprintf(interval_str, sizeof(interval_str), "%d", interval);
2763
2764                 rc = write_sysfs_file(modparam_path,
2765                                       "lnet_max_recovery_ping_interval",
2766                                       interval_str, 1,
2767                                       strlen(interval_str) + 1);
2768                 if (rc)
2769                         snprintf(err_str, sizeof(err_str),
2770                                  "\"cannot configure maximum recovery ping interval: %s\"",
2771                                  strerror(errno));
2772         }
2773
2774         cYAML_build_error(rc, seq_no, ADD_CMD, "maximum recovery ping interval",
2775                           err_str, err_rc);
2776
2777         return rc;
2778 }
2779
2780
2781 int lustre_lnet_show_routing(int seq_no, struct cYAML **show_rc,
2782                              struct cYAML **err_rc, bool backup)
2783 {
2784         struct lnet_ioctl_config_data *data;
2785         struct lnet_ioctl_pool_cfg *pool_cfg = NULL;
2786         int rc = LUSTRE_CFG_RC_OUT_OF_MEM;
2787         int l_errno = 0;
2788         char *buf;
2789         char *pools[LNET_NRBPOOLS] = {"tiny", "small", "large"};
2790         int buf_count[LNET_NRBPOOLS] = {0};
2791         struct cYAML *root = NULL, *pools_node = NULL,
2792                      *type_node = NULL, *item = NULL, *cpt = NULL,
2793                      *first_seq = NULL, *buffers = NULL;
2794         int i, j;
2795         char err_str[LNET_MAX_STR_LEN] = "\"out of memory\"";
2796         char node_name[LNET_MAX_STR_LEN];
2797         bool exist = false;
2798
2799         buf = calloc(1, sizeof(*data) + sizeof(*pool_cfg));
2800         if (buf == NULL)
2801                 goto out;
2802
2803         data = (struct lnet_ioctl_config_data *)buf;
2804
2805         root = cYAML_create_object(NULL, NULL);
2806         if (root == NULL)
2807                 goto out;
2808
2809         if (backup)
2810                 pools_node = cYAML_create_object(root, "routing");
2811         else
2812                 pools_node = cYAML_create_seq(root, "routing");
2813         if (pools_node == NULL)
2814                 goto out;
2815
2816         for (i = 0;; i++) {
2817                 LIBCFS_IOC_INIT_V2(*data, cfg_hdr);
2818                 data->cfg_hdr.ioc_len = sizeof(struct lnet_ioctl_config_data) +
2819                                         sizeof(struct lnet_ioctl_pool_cfg);
2820                 data->cfg_count = i;
2821
2822                 rc = l_ioctl(LNET_DEV_ID, IOC_LIBCFS_GET_BUF, data);
2823                 if (rc != 0) {
2824                         l_errno = errno;
2825                         break;
2826                 }
2827
2828                 exist = true;
2829
2830                 pool_cfg = (struct lnet_ioctl_pool_cfg *)data->cfg_bulk;
2831
2832                 if (backup)
2833                         goto calculate_buffers;
2834
2835                 snprintf(node_name, sizeof(node_name), "cpt[%d]", i);
2836                 item = cYAML_create_seq_item(pools_node);
2837                 if (item == NULL)
2838                         goto out;
2839
2840                 if (first_seq == NULL)
2841                         first_seq = item;
2842
2843                 cpt = cYAML_create_object(item, node_name);
2844                 if (cpt == NULL)
2845                         goto out;
2846
2847 calculate_buffers:
2848                 /* create the tree  and print */
2849                 for (j = 0; j < LNET_NRBPOOLS; j++) {
2850                         if (!backup) {
2851                                 type_node = cYAML_create_object(cpt, pools[j]);
2852                                 if (type_node == NULL)
2853                                         goto out;
2854                         }
2855                         if (!backup &&
2856                             cYAML_create_number(type_node, "npages",
2857                                                 pool_cfg->pl_pools[j].pl_npages)
2858                             == NULL)
2859                                 goto out;
2860                         if (!backup &&
2861                             cYAML_create_number(type_node, "nbuffers",
2862                                                 pool_cfg->pl_pools[j].
2863                                                   pl_nbuffers) == NULL)
2864                                 goto out;
2865                         if (!backup &&
2866                             cYAML_create_number(type_node, "credits",
2867                                                 pool_cfg->pl_pools[j].
2868                                                    pl_credits) == NULL)
2869                                 goto out;
2870                         if (!backup &&
2871                             cYAML_create_number(type_node, "mincredits",
2872                                                 pool_cfg->pl_pools[j].
2873                                                    pl_mincredits) == NULL)
2874                                 goto out;
2875                         /* keep track of the total count for each of the
2876                          * tiny, small and large buffers */
2877                         buf_count[j] += pool_cfg->pl_pools[j].pl_nbuffers;
2878                 }
2879         }
2880
2881         if (pool_cfg != NULL) {
2882                 if (backup) {
2883                         if (cYAML_create_number(pools_node, "enable",
2884                                                 pool_cfg->pl_routing) ==
2885                         NULL)
2886                                 goto out;
2887
2888                         goto add_buffer_section;
2889                 }
2890
2891                 item = cYAML_create_seq_item(pools_node);
2892                 if (item == NULL)
2893                         goto out;
2894
2895                 if (cYAML_create_number(item, "enable", pool_cfg->pl_routing) ==
2896                     NULL)
2897                         goto out;
2898         }
2899
2900 add_buffer_section:
2901         /* create a buffers entry in the show. This is necessary so that
2902          * if the YAML output is used to configure a node, the buffer
2903          * configuration takes hold */
2904         buffers = cYAML_create_object(root, "buffers");
2905         if (buffers == NULL)
2906                 goto out;
2907
2908         for (i = 0; i < LNET_NRBPOOLS; i++) {
2909                 if (cYAML_create_number(buffers, pools[i], buf_count[i]) == NULL)
2910                         goto out;
2911         }
2912
2913         if (show_rc == NULL)
2914                 cYAML_print_tree(root);
2915
2916         if (l_errno != ENOENT) {
2917                 snprintf(err_str,
2918                          sizeof(err_str),
2919                          "\"cannot get routing information: %s\"",
2920                          strerror(l_errno));
2921                 rc = -l_errno;
2922                 goto out;
2923         } else
2924                 rc = LUSTRE_CFG_RC_NO_ERR;
2925
2926         snprintf(err_str, sizeof(err_str), "\"success\"");
2927         rc = LUSTRE_CFG_RC_NO_ERR;
2928
2929 out:
2930         free(buf);
2931         if (show_rc == NULL || rc != LUSTRE_CFG_RC_NO_ERR || !exist) {
2932                 cYAML_free_tree(root);
2933         } else if (show_rc != NULL && *show_rc != NULL) {
2934                 struct cYAML *routing_node;
2935                 /* there should exist only one routing block and one
2936                  * buffers block. If there already exists a previous one
2937                  * then don't add another */
2938                 routing_node = cYAML_get_object_item(*show_rc, "routing");
2939                 if (routing_node == NULL) {
2940                         cYAML_insert_sibling((*show_rc)->cy_child,
2941                                                 root->cy_child);
2942                         free(root);
2943                 } else {
2944                         cYAML_free_tree(root);
2945                 }
2946         } else {
2947                 *show_rc = root;
2948         }
2949
2950         cYAML_build_error(rc, seq_no, SHOW_CMD, "routing", err_str, err_rc);
2951
2952         return rc;
2953 }
2954
2955 int lustre_lnet_show_peer(char *knid, int detail, int seq_no,
2956                           struct cYAML **show_rc, struct cYAML **err_rc,
2957                           bool backup)
2958 {
2959         /*
2960          * TODO: This function is changing in a future patch to accommodate
2961          * PEER_LIST and proper filtering on any nid of the peer
2962          */
2963         struct lnet_ioctl_peer_cfg peer_info;
2964         struct lnet_peer_ni_credit_info *lpni_cri;
2965         struct lnet_ioctl_element_stats *lpni_stats;
2966         struct lnet_ioctl_element_msg_stats *msg_stats;
2967         struct lnet_ioctl_peer_ni_hstats *hstats;
2968         struct lnet_ioctl_construct_udsp_info udsp_info;
2969         lnet_nid_t *nidp;